From c9a1e2040bf5600aeafdefe56a3bf46e15419a16 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 17 Oct 2022 23:36:02 +0200 Subject: [PATCH] Fix colour primaries when previewing Rec.2020 content with OpenGL. --- src/wx/gl_video_view.cc | 30 +++++++++++++++++++++++++++++- src/wx/gl_video_view.h | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wx/gl_video_view.cc b/src/wx/gl_video_view.cc index 16b01d25c..4fc620936 100644 --- a/src/wx/gl_video_view.cc +++ b/src/wx/gl_video_view.cc @@ -198,12 +198,14 @@ static constexpr char fragment_source[] = * type = 1: draw crop guess rectangle * type = 2: draw XYZ image * type = 3: draw RGB image (with sRGB/Rec709 primaries) + * type = 4: draw RGB image (converting from Rec2020 primaries) * See FragmentType enum below. */ "uniform int type = 0;\n" "uniform vec4 outline_content_colour;\n" "uniform vec4 crop_guess_colour;\n" "uniform mat4 xyz_rec709_colour_conversion;\n" +"uniform mat4 rec2020_rec709_colour_conversion;\n" "\n" "out vec4 FragColor;\n" "\n" @@ -278,6 +280,10 @@ static constexpr char fragment_source[] = " break;\n" " case 3:\n" " FragColor = texture_bicubic(texture_sampler, TexCoord);\n" +" break;\n" +" case 4:\n" +" FragColor = texture_bicubic(texture_sampler, TexCoord);\n" +" FragColor = rec2020_rec709_colour_conversion * FragColor;\n" " break;\n" " }\n" "}\n"; @@ -289,6 +295,7 @@ enum class FragmentType CROP_GUESS = 1, XYZ_IMAGE = 2, REC709_IMAGE = 3, + REC2020_IMAGE = 4, }; @@ -483,6 +490,19 @@ GLVideoView::setup_shaders () glUniformMatrix4fv(xyz_rec709_colour_conversion, 1, GL_TRUE, gl_matrix); } + { + auto xyz_rec709 = dcp::ColourConversion::rec709_to_xyz().xyz_to_rgb(); + auto rec2020_xyz = dcp::ColourConversion::rec2020_to_xyz().rgb_to_xyz(); + auto product = boost::numeric::ublas::prod(xyz_rec709, rec2020_xyz); + + GLfloat gl_matrix[16]; + ublas_to_gl(product, gl_matrix); + + auto rec2020_rec709_colour_conversion = glGetUniformLocation(program, "rec2020_rec709_colour_conversion"); + check_gl_error("glGetUniformLocation"); + glUniformMatrix4fv(rec2020_rec709_colour_conversion, 1, GL_TRUE, gl_matrix); + } + glLineWidth (1.0f); check_gl_error ("glLineWidth"); glEnable (GL_BLEND); @@ -539,7 +559,13 @@ GLVideoView::draw () glBindVertexArray(_vao); check_gl_error ("glBindVertexArray"); - glUniform1i(_fragment_type, static_cast(_optimise_for_j2k ? FragmentType::XYZ_IMAGE : FragmentType::REC709_IMAGE)); + if (_optimise_for_j2k) { + glUniform1i(_fragment_type, static_cast(FragmentType::XYZ_IMAGE)); + } else if (_rec2020) { + glUniform1i(_fragment_type, static_cast(FragmentType::REC2020_IMAGE)); + } else { + glUniform1i(_fragment_type, static_cast(FragmentType::REC709_IMAGE)); + } _video_texture->bind(); glDrawElements (GL_TRIANGLES, indices_video_texture_number, GL_UNSIGNED_INT, reinterpret_cast(indices_video_texture_offset * sizeof(int))); if (_have_subtitle_to_render) { @@ -709,6 +735,8 @@ GLVideoView::set_image (shared_ptr pv) check_gl_error ("glBufferSubData (subtitle)"); } + _rec2020 = pv->colour_conversion() && pv->colour_conversion()->about_equal(dcp::ColourConversion::rec2020_to_xyz(), 1e-6); + /* opt: where should these go? */ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); diff --git a/src/wx/gl_video_view.h b/src/wx/gl_video_view.h index fd9154a73..964f6f1ab 100644 --- a/src/wx/gl_video_view.h +++ b/src/wx/gl_video_view.h @@ -134,6 +134,7 @@ private: Last>> _last_crop_guess; boost::atomic _canvas_size; + boost::atomic _rec2020; std::unique_ptr _video_texture; std::unique_ptr _subtitle_texture; bool _have_subtitle_to_render = false; -- 2.30.2