X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=video_stream.cpp;h=a733d46e6820b3fe32150724cd0eb92aef94cc4c;hb=1aab0848ee4807a0369656edd9eb42ce5adb56ae;hp=8d485ad02b20e79ab3c2a2ef578c7395835d2e9b;hpb=8deea0eee4f9b18540463ba253747c232873527b;p=nageru diff --git a/video_stream.cpp b/video_stream.cpp index 8d485ad..a733d46 100644 --- a/video_stream.cpp +++ b/video_stream.cpp @@ -8,6 +8,7 @@ extern "C" { #include #include +#include "chroma_subsampler.h" #include "context.h" #include "flow.h" #include "httpd.h" @@ -98,7 +99,7 @@ struct VectorDestinationManager { }; static_assert(std::is_standard_layout::value, ""); -vector encode_jpeg(const uint8_t *pixel_data, unsigned width, unsigned height) +vector encode_jpeg(const uint8_t *y_data, const uint8_t *cb_data, const uint8_t *cr_data, unsigned width, unsigned height) { VectorDestinationManager dest; @@ -127,31 +128,13 @@ vector encode_jpeg(const uint8_t *pixel_data, unsigned width, unsigned cinfo.CCIR601_sampling = true; // Seems to be mostly ignored by libjpeg, though. jpeg_start_compress(&cinfo, true); - // TODO: Subsample and deinterleave on the GPU. - - unique_ptr ydata(new uint8_t[width * 8]); - unique_ptr cbdata(new uint8_t[(width/2) * 8]); - unique_ptr crdata(new uint8_t[(width/2) * 8]); JSAMPROW yptr[8], cbptr[8], crptr[8]; JSAMPARRAY data[3] = { yptr, cbptr, crptr }; - for (unsigned yy = 0; yy < 8; ++yy) { - yptr[yy] = ydata.get() + yy * width; - cbptr[yy] = cbdata.get() + yy * (width / 2); - crptr[yy] = crdata.get() + yy * (width / 2); - } for (unsigned y = 0; y < height; y += 8) { - uint8_t *yptr = ydata.get(); - uint8_t *cbptr = cbdata.get(); - uint8_t *crptr = crdata.get(); for (unsigned yy = 0; yy < 8; ++yy) { - const uint8_t *sptr = &pixel_data[(height - y - yy - 1) * width * 4]; - for (unsigned x = 0; x < width; x += 2) { - *yptr++ = sptr[0]; - *yptr++ = sptr[4]; - *cbptr++ = (sptr[1] + sptr[5]) / 2; - *crptr++ = (sptr[2] + sptr[6]) / 2; - sptr += 8; - } + yptr[yy] = const_cast(&y_data[(height - y - yy - 1) * width]); + cbptr[yy] = const_cast(&cb_data[(height - y - yy - 1) * width/2]); + crptr[yy] = const_cast(&cr_data[(height - y - yy - 1) * width/2]); } jpeg_write_raw_data(&cinfo, data, /*num_lines=*/8); @@ -203,9 +186,11 @@ VideoStream::VideoStream() ycbcr_convert_chain->finalize(); check_error(); - GLuint input_tex[num_interpolate_slots], gray_tex[num_interpolate_slots]; + GLuint input_tex[num_interpolate_slots], gray_tex[num_interpolate_slots], cb_tex[num_interpolate_slots], cr_tex[num_interpolate_slots]; glCreateTextures(GL_TEXTURE_2D_ARRAY, 10, input_tex); glCreateTextures(GL_TEXTURE_2D_ARRAY, 10, gray_tex); + glCreateTextures(GL_TEXTURE_2D, 10, cb_tex); + glCreateTextures(GL_TEXTURE_2D, 10, cr_tex); check_error(); constexpr size_t width = 1280, height = 720; // FIXME: adjustable width, height int levels = find_num_levels(width, height); @@ -214,10 +199,16 @@ VideoStream::VideoStream() check_error(); glTextureStorage3D(gray_tex[i], levels, GL_R8, width, height, 2); check_error(); + glTextureStorage2D(cb_tex[i], 1, GL_R8, width / 2, height); + check_error(); + glTextureStorage2D(cr_tex[i], 1, GL_R8, width / 2, height); + check_error(); InterpolatedFrameResources resource; resource.input_tex = input_tex[i]; resource.gray_tex = gray_tex[i]; + resource.cb_tex = cb_tex[i]; + resource.cr_tex = cr_tex[i]; glCreateFramebuffers(2, resource.input_fbos); check_error(); @@ -246,8 +237,9 @@ VideoStream::VideoStream() check_error(); - compute_flow.reset(new DISComputeFlow(width, height, operating_point3)); - interpolate.reset(new Interpolate(width, height, operating_point3)); + compute_flow.reset(new DISComputeFlow(width, height, operating_point2)); + interpolate.reset(new Interpolate(width, height, operating_point2, /*split_ycbcr_output=*/true)); + chroma_subsampler.reset(new ChromaSubsampler); check_error(); } @@ -307,6 +299,7 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea unique_lock lock(queue_lock); if (interpolate_resources.empty()) { fprintf(stderr, "WARNING: Too many interpolated frames already in transit; dropping one.\n"); + JPEGFrameView::insert_interpolated_frame(stream_idx, output_pts, nullptr); return; } resources = interpolate_resources.front(); @@ -327,6 +320,7 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea JPEGID jpeg_id; jpeg_id.stream_idx = stream_idx; jpeg_id.pts = frame_no == 1 ? input_second_pts : input_first_pts; + jpeg_id.interpolated = false; bool did_decode; shared_ptr frame = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode); ycbcr_format.chroma_subsampling_x = frame->chroma_subsampling_x; @@ -351,9 +345,12 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea // Compute the interpolated frame. qf.flow_tex = compute_flow->exec(resources.gray_tex, DISComputeFlow::FORWARD_AND_BACKWARD, DISComputeFlow::DO_NOT_RESIZE_FLOW); check_error(); - qf.output_tex = interpolate->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha); + tie(qf.output_tex, qf.cbcr_tex) = interpolate->exec(resources.input_tex, resources.gray_tex, qf.flow_tex, 1280, 720, alpha); check_error(); + // Subsample and split Cb/Cr. + chroma_subsampler->subsample_chroma(qf.cbcr_tex, 1280, 720, resources.cb_tex, resources.cr_tex); + // We could have released qf.flow_tex here, but to make sure we don't cause a stall // when trying to reuse it for the next frame, we can just as well hold on to it // and release it only when the readback is done. @@ -362,7 +359,11 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea glPixelStorei(GL_PACK_ROW_LENGTH, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, resources.pbo); check_error(); - glGetTextureImage(qf.output_tex, 0, GL_RGBA, GL_UNSIGNED_BYTE, 1280 * 720 * 4, nullptr); + glGetTextureImage(qf.output_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 4, BUFFER_OFFSET(0)); + check_error(); + glGetTextureImage(resources.cb_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3, BUFFER_OFFSET(1280 * 720)); + check_error(); + glGetTextureImage(resources.cr_tex, 0, GL_RED, GL_UNSIGNED_BYTE, 1280 * 720 * 3 - 640 * 720, BUFFER_OFFSET(1280 * 720 + 640 * 720)); check_error(); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); @@ -411,9 +412,33 @@ void VideoStream::encode_thread_func() } else if (qf.type == QueuedFrame::INTERPOLATED) { glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED); - vector jpeg = encode_jpeg((const uint8_t *)qf.resources.pbo_contents, 1280, 720); + const uint8_t *y = (const uint8_t *)qf.resources.pbo_contents; + const uint8_t *cb = (const uint8_t *)qf.resources.pbo_contents + 1280 * 720; + const uint8_t *cr = (const uint8_t *)qf.resources.pbo_contents + 1280 * 720 + 640 * 720; + + // Send a copy of the frame on to display. + shared_ptr frame(new Frame); + frame->y.reset(new uint8_t[1280 * 720]); + frame->cb.reset(new uint8_t[640 * 720]); + frame->cr.reset(new uint8_t[640 * 720]); + for (unsigned yy = 0; yy < 720; ++yy) { + memcpy(frame->y.get() + 1280 * yy, y + 1280 * (719 - yy), 1280); + memcpy(frame->cb.get() + 640 * yy, cb + 640 * (719 - yy), 640); + memcpy(frame->cr.get() + 640 * yy, cr + 640 * (719 - yy), 640); + } + frame->width = 1280; + frame->height = 720; + frame->chroma_subsampling_x = 2; + frame->chroma_subsampling_y = 1; + frame->pitch_y = 1280; + frame->pitch_chroma = 640; + JPEGFrameView::insert_interpolated_frame(qf.stream_idx, qf.output_pts, std::move(frame)); + + // Now JPEG encode it, and send it on to the stream. + vector jpeg = encode_jpeg(y, cb, cr, 1280, 720); compute_flow->release_texture(qf.flow_tex); interpolate->release_texture(qf.output_tex); + interpolate->release_texture(qf.cbcr_tex); AVPacket pkt; av_init_packet(&pkt);