]> git.sesse.net Git - nageru/blobdiff - video_stream.cpp
Move Y'CbCr conversion into a common utility class.
[nageru] / video_stream.cpp
index 625771fe410a2539cd53de0adad8feeb0eaeb50b..c01ec5ce1e557ca236db22c26f532900553a89b7 100644 (file)
@@ -8,6 +8,7 @@ extern "C" {
 #include <jpeglib.h>
 #include <unistd.h>
 
+#include "chroma_subsampler.h"
 #include "context.h"
 #include "flow.h"
 #include "httpd.h"
@@ -16,6 +17,7 @@ extern "C" {
 #include "mux.h"
 #include "player.h"
 #include "util.h"
+#include "ycbcr_converter.h"
 
 #include <epoxy/glx.h>
 
@@ -98,7 +100,7 @@ struct VectorDestinationManager {
 };
 static_assert(std::is_standard_layout<VectorDestinationManager>::value, "");
 
-vector<uint8_t> encode_jpeg(const uint8_t *pixel_data, unsigned width, unsigned height)
+vector<uint8_t> encode_jpeg(const uint8_t *y_data, const uint8_t *cb_data, const uint8_t *cr_data, unsigned width, unsigned height)
 {
        VectorDestinationManager dest;
 
@@ -116,28 +118,27 @@ vector<uint8_t> encode_jpeg(const uint8_t *pixel_data, unsigned width, unsigned
 
        cinfo.image_width = width;
        cinfo.image_height = height;
-       cinfo.input_components = 3;
+       cinfo.raw_data_in = true;
+       jpeg_set_colorspace(&cinfo, JCS_YCbCr);
        cinfo.comp_info[0].h_samp_factor = 2;
        cinfo.comp_info[0].v_samp_factor = 1;
        cinfo.comp_info[1].h_samp_factor = 1;
        cinfo.comp_info[1].v_samp_factor = 1;
        cinfo.comp_info[2].h_samp_factor = 1;
        cinfo.comp_info[2].v_samp_factor = 1;
-       // cinfo.CCIR601_sampling = true;  // TODO: Subsample ourselves.
+       cinfo.CCIR601_sampling = true;  // Seems to be mostly ignored by libjpeg, though.
        jpeg_start_compress(&cinfo, true);
 
-       unique_ptr<uint8_t[]> row(new uint8_t[width * 3]);
-       JSAMPROW row_pointer[1] = { row.get() };
-       for (unsigned y = 0; y < height; ++y) {
-               const uint8_t *sptr = &pixel_data[(height - cinfo.next_scanline - 1) * width * 4];
-               uint8_t *dptr = row.get();
-               for (unsigned x = 0; x < width; ++x) {
-                       *dptr++ = *sptr++;
-                       *dptr++ = *sptr++;
-                       *dptr++ = *sptr++;
-                       ++sptr;
+       JSAMPROW yptr[8], cbptr[8], crptr[8];
+       JSAMPARRAY data[3] = { yptr, cbptr, crptr };
+       for (unsigned y = 0; y < height; y += 8) {
+               for (unsigned yy = 0; yy < 8; ++yy) {
+                       yptr[yy] = const_cast<JSAMPROW>(&y_data[(y + yy) * width]);
+                       cbptr[yy] = const_cast<JSAMPROW>(&cb_data[(y + yy) * width/2]);
+                       crptr[yy] = const_cast<JSAMPROW>(&cr_data[(y + yy) * width/2]);
                }
-               (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
+
+               jpeg_write_raw_data(&cinfo, data, /*num_lines=*/8);
        }
 
        jpeg_finish_compress(&cinfo);
@@ -148,38 +149,15 @@ vector<uint8_t> encode_jpeg(const uint8_t *pixel_data, unsigned width, unsigned
 
 VideoStream::VideoStream()
 {
-       using namespace movit;
-       // TODO: deduplicate code against JPEGFrameView?
-       ycbcr_convert_chain.reset(new EffectChain(1280, 720));
-       ImageFormat image_format;
-       image_format.color_space = COLORSPACE_sRGB;
-       image_format.gamma_curve = GAMMA_sRGB;
-       ycbcr_format.luma_coefficients = YCBCR_REC_709;
-       ycbcr_format.full_range = true;  // JPEG.
-       ycbcr_format.num_levels = 256;
-       ycbcr_format.chroma_subsampling_x = 2;
-       ycbcr_format.chroma_subsampling_y = 1;
-       ycbcr_format.cb_x_position = 0.0f;  // H.264 -- _not_ JPEG, even though our input is MJPEG-encoded
-       ycbcr_format.cb_y_position = 0.5f;  // Irrelevant.
-       ycbcr_format.cr_x_position = 0.0f;
-       ycbcr_format.cr_y_position = 0.5f;
-       ycbcr_input = (movit::YCbCrInput *)ycbcr_convert_chain->add_input(new YCbCrInput(image_format, ycbcr_format, 1280, 720));
-
-       ImageFormat inout_format;
-       inout_format.color_space = COLORSPACE_sRGB;
-       inout_format.gamma_curve = GAMMA_sRGB;
-
-       check_error();
-       ycbcr_convert_chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
-       check_error();
-       ycbcr_convert_chain->set_dither_bits(8);
-       check_error();
-       ycbcr_convert_chain->finalize();
-       check_error();
+       ycbcr_converter.reset(new YCbCrConverter(YCbCrConverter::OUTPUT_TO_DUAL_YCBCR, /*resource_pool=*/nullptr));
 
        GLuint input_tex[num_interpolate_slots], gray_tex[num_interpolate_slots];
+       GLuint 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);
@@ -188,22 +166,32 @@ 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();
 
                glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 0);
                check_error();
+               glNamedFramebufferTextureLayer(resource.input_fbos[0], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 0);
+               check_error();
                glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT0, input_tex[i], 0, 1);
                check_error();
+               glNamedFramebufferTextureLayer(resource.input_fbos[1], GL_COLOR_ATTACHMENT1, gray_tex[i], 0, 1);
+               check_error();
 
-               GLuint buf = GL_COLOR_ATTACHMENT0;
-               glNamedFramebufferDrawBuffers(resource.input_fbos[0], 1, &buf);
+               GLuint bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
+               glNamedFramebufferDrawBuffers(resource.input_fbos[0], 2, bufs);
                check_error();
-               glNamedFramebufferDrawBuffers(resource.input_fbos[1], 1, &buf);
+               glNamedFramebufferDrawBuffers(resource.input_fbos[1], 2, bufs);
                check_error();
 
                glCreateBuffers(1, &resource.pbo);
@@ -216,9 +204,9 @@ VideoStream::VideoStream()
 
        check_error();
 
-       compute_flow.reset(new DISComputeFlow(width, height, operating_point3));
-       gray.reset(new GrayscaleConversion);  // NOTE: Must come after DISComputeFlow, since it sets up the VBO!
-       interpolate.reset(new Interpolate(width, height, operating_point3));
+       compute_flow.reset(new DISComputeFlow(width, height, operating_point2));
+       interpolate.reset(new Interpolate(operating_point2, /*split_ycbcr_output=*/true));
+       chroma_subsampler.reset(new ChromaSubsampler);
        check_error();
 }
 
@@ -278,6 +266,7 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea
                unique_lock<mutex> 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();
@@ -293,41 +282,30 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea
        check_error();
 
        // Convert frame0 and frame1 to OpenGL textures.
-       // TODO: Deduplicate against JPEGFrameView::setDecodedFrame?
        for (size_t frame_no = 0; frame_no < 2; ++frame_no) {
                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> frame = decode_jpeg_with_cache(jpeg_id, DECODE_IF_NOT_IN_CACHE, &did_decode);
-               ycbcr_format.chroma_subsampling_x = frame->chroma_subsampling_x;
-               ycbcr_format.chroma_subsampling_y = frame->chroma_subsampling_y;
-               ycbcr_input->change_ycbcr_format(ycbcr_format);
-               ycbcr_input->set_width(frame->width);
-               ycbcr_input->set_height(frame->height);
-               ycbcr_input->set_pixel_data(0, frame->y.get());
-               ycbcr_input->set_pixel_data(1, frame->cb.get());
-               ycbcr_input->set_pixel_data(2, frame->cr.get());
-               ycbcr_input->set_pitch(0, frame->pitch_y);
-               ycbcr_input->set_pitch(1, frame->pitch_chroma);
-               ycbcr_input->set_pitch(2, frame->pitch_chroma);
-               ycbcr_convert_chain->render_to_fbo(resources.input_fbos[frame_no], 1280, 720);
+               ycbcr_converter->prepare_chain_for_conversion(frame)->render_to_fbo(resources.input_fbos[frame_no], 1280, 720);
        }
 
        glGenerateTextureMipmap(resources.input_tex);
-
-       // Compute the interpolated frame.
-       check_error();
-       gray->exec(resources.input_tex, resources.gray_tex, 1280, 720, /*num_layers=*/2);
        check_error();
        glGenerateTextureMipmap(resources.gray_tex);
        check_error();
+
+       // 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, 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.
@@ -336,7 +314,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);
 
@@ -351,8 +333,40 @@ void VideoStream::schedule_interpolated_frame(int64_t output_pts, unsigned strea
        queue_nonempty.notify_all();
 }
 
+namespace {
+
+shared_ptr<Frame> frame_from_pbo(void *contents, size_t width, size_t height)
+{
+       size_t chroma_width = width / 2;
+
+       const uint8_t *y = (const uint8_t *)contents;
+       const uint8_t *cb = (const uint8_t *)contents + width * height;
+       const uint8_t *cr = (const uint8_t *)contents + width * height + chroma_width * height;
+
+       shared_ptr<Frame> frame(new Frame);
+       frame->y.reset(new uint8_t[width * height]);
+       frame->cb.reset(new uint8_t[chroma_width * height]);
+       frame->cr.reset(new uint8_t[chroma_width * height]);
+       for (unsigned yy = 0; yy < height; ++yy) {
+               memcpy(frame->y.get() + width * yy, y + width * yy, width);
+               memcpy(frame->cb.get() + chroma_width * yy, cb + chroma_width * yy, chroma_width);
+               memcpy(frame->cr.get() + chroma_width * yy, cr + chroma_width * yy, chroma_width);
+       }
+       frame->is_semiplanar = false;
+       frame->width = width;
+       frame->height = height;
+       frame->chroma_subsampling_x = 2;
+       frame->chroma_subsampling_y = 1;
+       frame->pitch_y = width;
+       frame->pitch_chroma = chroma_width;
+       return frame;
+}
+
+}  // namespace
+
 void VideoStream::encode_thread_func()
 {
+       pthread_setname_np(pthread_self(), "VideoStream");
        QSurface *surface = create_surface();
        QOpenGLContext *context = create_context(surface);
        bool ok = make_current(context, surface);
@@ -384,9 +398,16 @@ void VideoStream::encode_thread_func()
                } else if (qf.type == QueuedFrame::INTERPOLATED) {
                        glClientWaitSync(qf.fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
 
-                       vector<uint8_t> jpeg = encode_jpeg((const uint8_t *)qf.resources.pbo_contents, 1280, 720);
+
+                       // Send a copy of the frame on to display.
+                       shared_ptr<Frame> frame = frame_from_pbo(qf.resources.pbo_contents, 1280, 720);
+                       JPEGFrameView::insert_interpolated_frame(qf.stream_idx, qf.output_pts, frame);
+
+                       // Now JPEG encode it, and send it on to the stream.
+                       vector<uint8_t> jpeg = encode_jpeg(frame->y.get(), frame->cb.get(), frame->cr.get(), 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);