When the player is used in OpenGL mode, pass unscaled XYZ data through to the shader...
authorCarl Hetherington <cth@carlh.net>
Sat, 11 Sep 2021 16:52:05 +0000 (18:52 +0200)
committerCarl Hetherington <cth@carlh.net>
Mon, 27 Sep 2021 11:41:46 +0000 (13:41 +0200)
src/lib/butler.cc
src/lib/butler.h
src/lib/j2k_image_proxy.cc
src/lib/player_video.cc
src/lib/player_video.h
src/tools/dcpomatic_player.cc
src/wx/film_viewer.cc
src/wx/film_viewer.h
src/wx/gl_video_view.cc
src/wx/gl_video_view.h
src/wx/video_view.h

index 5a8e646aa4635556fca90f8791f23e46b6468a9e..cbd5ba15d4f01839349d8759173f9e14ec9e1aa0 100644 (file)
@@ -323,7 +323,7 @@ try
        /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
        if (video) {
                LOG_TIMING("start-prepare in %1", thread_id());
-               video->prepare (_pixel_format, _video_range, _aligned, _fast);
+               video->prepare (_pixel_format, _video_range, _aligned, _fast, _prepare_only_proxy);
                LOG_TIMING("finish-prepare in %1", thread_id());
        }
 }
index a231fd0990bf75f8f4e0f8f5bfe4eab50f9bff26..d31442f6c699b1096a52ba842d22043d571f2b44 100644 (file)
@@ -80,6 +80,9 @@ public:
        boost::optional<TextRingBuffers::Data> get_closed_caption ();
 
        void disable_audio ();
+       void set_prepare_only_proxy (bool p) {
+               _prepare_only_proxy = p;
+       }
 
        std::pair<size_t, std::string> memory_used () const;
 
@@ -127,6 +130,14 @@ private:
        bool _aligned;
        bool _fast;
 
+       /** true to ask PlayerVideo::prepare to only prepare the ImageProxy and not also
+        *  the final image.  We want to do this when the viewer is intending to call
+        *  PlayerVideo::raw_image() and do the things in PlayerVideo::make_imgae() itself:
+        *  this is the case for the GLVideoView which can do scale, pixfmt conversion etc.
+        *  in the shader.
+        */
+       bool _prepare_only_proxy = false;
+
        /** If we are waiting to be refilled following a seek, this is the time we were
            seeking to.
        */
index 144da396d24ee619970c679de49731378a69963c..c98273ad20043ed3cd81fc6ef81e3ffa5ac771fb 100644 (file)
@@ -145,7 +145,7 @@ J2KImageProxy::prepare (optional<dcp::Size> target_size) const
        try {
                /* XXX: should check that potentially trashing _data here doesn't matter */
                auto decompressed = dcp::decompress_j2k (const_cast<uint8_t*>(_data->data()), _data->size(), reduce);
-               _image.reset (new Image (_pixel_format, decompressed->size(), true));
+               _image.reset (new Image (_pixel_format, decompressed->size(), false));
 
                int const shift = 16 - decompressed->precision (0);
 
index b0e75972ce342338c2ce152ddb6f0e0a313db17a..0a6ce0d993cf08ac4f304288db60d42389feb418 100644 (file)
@@ -121,6 +121,14 @@ PlayerVideo::image (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoR
        return _image;
 }
 
+
+shared_ptr<Image>
+PlayerVideo::raw_image () const
+{
+       return _in->image(_inter_size).image;
+}
+
+
 /** Create an image for this frame.  A lock must be held on _mutex.
  *  @param pixel_format Function which is called to decide what pixel format the output image should be;
  *  it is passed the pixel format of the input image from the ImageProxy, and should return the desired
@@ -290,11 +298,11 @@ PlayerVideo::keep_xyz_or_rgb (AVPixelFormat p)
 }
 
 void
-PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool aligned, bool fast)
+PlayerVideo::prepare (function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool aligned, bool fast, bool proxy_only)
 {
        _in->prepare (_inter_size);
        boost::mutex::scoped_lock lm (_mutex);
-       if (!_image) {
+       if (!_image && !proxy_only) {
                make_image (pixel_format, video_range, aligned, fast);
        }
 }
index f296848320a77a053d863d5ded423c05015d7dbc..8134c8d4e564715dc1f296cac4f6334bedd24806 100644 (file)
@@ -71,8 +71,9 @@ public:
 
        void set_text (PositionImage);
 
-       void prepare (std::function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool aligned, bool fast);
+       void prepare (std::function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool aligned, bool fast, bool proxy_only);
        std::shared_ptr<Image> image (std::function<AVPixelFormat (AVPixelFormat)> pixel_format, VideoRange video_range, bool aligned, bool fast) const;
+       std::shared_ptr<Image> raw_image () const;
 
        static AVPixelFormat force (AVPixelFormat, AVPixelFormat);
        static AVPixelFormat keep_xyz_or_rgb (AVPixelFormat);
index 23aabadd83317c55ccdd41d73a3b3dd2417dec47..e409b97311be1fa5c78a5952d6d5a20c51a2102b 100644 (file)
@@ -197,6 +197,7 @@ public:
                        _controls = new StandardControls (_overall_panel, _viewer, false);
                }
                _viewer->set_dcp_decode_reduction (Config::instance()->decode_reduction ());
+               _viewer->set_optimise_for_j2k (true);
                _viewer->PlaybackPermitted.connect (bind(&DOMFrame::playback_permitted, this));
                _viewer->Started.connect (bind(&DOMFrame::playback_started, this, _1));
                _viewer->Stopped.connect (bind(&DOMFrame::playback_stopped, this, _1));
index 77c2e85d690a6cb7055dc02063412812abd1b22b..749e4ceb7c315e73147737c465f2d26e981fba80 100644 (file)
@@ -229,6 +229,10 @@ FilmViewer::recreate_butler ()
                _butler->disable_audio ();
        }
 
+       if (dynamic_pointer_cast<GLVideoView>(_video_view) && _optimise_for_j2k) {
+               _butler->set_prepare_only_proxy (true);
+       }
+
        _closed_captions_dialog->set_butler (_butler);
 
        resume ();
@@ -772,3 +776,12 @@ FilmViewer::image_changed (shared_ptr<PlayerVideo> pv)
 {
        emit (boost::bind(boost::ref(ImageChanged), pv));
 }
+
+
+void
+FilmViewer::set_optimise_for_j2k (bool o)
+{
+       _optimise_for_j2k = o;
+       _video_view->set_optimise_for_j2k (o);
+}
+
index c467eedc1645d806495ea0d01cf1ddbd59cfed6b..5e5bb791660c0ebfe4d295adf03cfb0c1508908b 100644 (file)
@@ -98,6 +98,7 @@ public:
        void set_outline_subtitles (boost::optional<dcpomatic::Rect<double>>);
        void set_eyes (Eyes e);
        void set_pad_black (bool p);
+       void set_optimise_for_j2k (bool o);
 
        void slow_refresh ();
 
@@ -187,6 +188,11 @@ private:
 
        boost::optional<int> _dcp_decode_reduction;
 
+       /** true to assume that this viewer is only being used for JPEG2000 sources
+        *  so it can optimise accordingly.
+        */
+       bool _optimise_for_j2k = false;
+
        ClosedCaptionsDialog* _closed_captions_dialog = nullptr;
 
        bool _outline_content = false;
index 7c26ae6169780660c66ada2c5017a5b4d15b2ee3..8fcdc3c0b21a4f3ae71d5a35af23a5b4a91469ae 100644 (file)
@@ -189,12 +189,22 @@ static constexpr char fragment_source[] =
 "in vec2 TexCoord;\n"
 "\n"
 "uniform sampler2D texture_sampler;\n"
-"uniform int draw_border;\n"
+/* type = 0: draw border
+ * type = 1: draw XYZ image
+ * type = 2: draw RGB image
+ */
+"uniform int type = 0;\n"
 "uniform vec4 border_colour;\n"
+"uniform mat4 colour_conversion;\n"
 "\n"
 "out vec4 FragColor;\n"
 "\n"
 "vec4 cubic(float x)\n"
+"\n"
+"#define IN_GAMMA 2.2\n"
+"#define OUT_GAMMA 0.384615385\n"       //  1 /  2.6
+"#define DCI_COEFFICIENT 0.91655528\n"  // 48 / 53.37
+"\n"
 "{\n"
 "    float x2 = x * x;\n"
 "    float x3 = x2 * x;\n"
@@ -241,10 +251,23 @@ static constexpr char fragment_source[] =
 "\n"
 "void main()\n"
 "{\n"
-"      if (draw_border == 1) {\n"
-"              FragColor = border_colour;\n"
-"      } else {\n"
-"              FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
+"      switch (type) {\n"
+"              case 0:\n"
+"                      FragColor = border_colour;\n"
+"                      break;\n"
+"              case 1:\n"
+"                      FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
+"                      FragColor.x = pow(FragColor.x, IN_GAMMA) / DCI_COEFFICIENT;\n"
+"                      FragColor.y = pow(FragColor.y, IN_GAMMA) / DCI_COEFFICIENT;\n"
+"                      FragColor.z = pow(FragColor.z, IN_GAMMA) / DCI_COEFFICIENT;\n"
+"                      FragColor = colour_conversion * FragColor;\n"
+"                      FragColor.x = pow(FragColor.x, OUT_GAMMA);\n"
+"                      FragColor.y = pow(FragColor.y, OUT_GAMMA);\n"
+"                      FragColor.z = pow(FragColor.z, OUT_GAMMA);\n"
+"                      break;\n"
+"              case 2:\n"
+"                      FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
+"                      break;\n"
 "      }\n"
 "}\n";
 
@@ -383,10 +406,23 @@ GLVideoView::setup_shaders ()
 
        glUseProgram (program);
 
-       _draw_border = glGetUniformLocation (program, "draw_border");
+       _fragment_type = glGetUniformLocation (program, "type");
        check_gl_error ("glGetUniformLocation");
        set_border_colour (program);
 
+       auto conversion = dcp::ColourConversion::rec709_to_xyz();
+       boost::numeric::ublas::matrix<double> matrix = conversion.xyz_to_rgb ();
+       GLfloat gl_matrix[] = {
+               static_cast<float>(matrix(0, 0)), static_cast<float>(matrix(0, 1)), static_cast<float>(matrix(0, 2)), 0.0f,
+               static_cast<float>(matrix(1, 0)), static_cast<float>(matrix(1, 1)), static_cast<float>(matrix(1, 2)), 0.0f,
+               static_cast<float>(matrix(2, 0)), static_cast<float>(matrix(2, 1)), static_cast<float>(matrix(2, 2)), 0.0f,
+               0.0f, 0.0f, 0.0f, 1.0f
+               };
+
+       auto colour_conversion = glGetUniformLocation (program, "colour_conversion");
+       check_gl_error ("glGetUniformLocation");
+       glUniformMatrix4fv (colour_conversion, 1, GL_TRUE, gl_matrix);
+
        glLineWidth (2.0f);
 }
 
@@ -424,10 +460,10 @@ GLVideoView::draw (Position<int>, dcp::Size)
        glBindTexture(GL_TEXTURE_2D, _texture);
        glBindVertexArray(_vao);
        check_gl_error ("glBindVertexArray");
-       glUniform1i(_draw_border, 0);
+       glUniform1i(_fragment_type, _optimise_for_j2k ? 1 : 2);
        glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        if (_viewer->outline_content()) {
-               glUniform1i(_draw_border, 1);
+               glUniform1i(_fragment_type, 1);
                glDrawElements (GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(6 * sizeof(int)));
                check_gl_error ("glDrawElements");
        }
@@ -440,30 +476,35 @@ GLVideoView::draw (Position<int>, dcp::Size)
 
 
 void
-GLVideoView::set_image (shared_ptr<const Image> image)
+GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
 {
-       if (!image) {
-               _size = optional<dcp::Size>();
-               return;
-       }
-
+       auto image = _optimise_for_j2k ? pv->raw_image() : pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
 
-       DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
        DCPOMATIC_ASSERT (!image->aligned());
 
+       /** If _optimise_for_j2k is true we render a XYZ image, doing the colourspace
+        *  conversion, scaling and video range conversion in the GL shader.
+        *  Othewise we render a RGB image without any shader-side processing.
+        */
+
+       /* XXX: video range conversion */
+       /* XXX: subs */
+
        if (image->size() != _size) {
                _have_storage = false;
        }
 
        _size = image->size ();
-       glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+       glPixelStorei (GL_UNPACK_ALIGNMENT, _optimise_for_j2k ? 2 : 1);
        check_gl_error ("glPixelStorei");
 
+       auto const format = _optimise_for_j2k ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
+
        if (_have_storage) {
-               glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
+               glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, format, image->data()[0]);
                check_gl_error ("glTexSubImage2D");
        } else {
-               glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
+               glTexImage2D (GL_TEXTURE_2D, 0, _optimise_for_j2k ? GL_RGBA12 : GL_RGBA8, _size->width, _size->height, 0, GL_RGB, format, image->data()[0]);
                check_gl_error ("glTexImage2D");
 
                auto const canvas_size = _canvas_size.load();
@@ -517,6 +558,7 @@ GLVideoView::set_image (shared_ptr<const Image> image)
        check_gl_error ("glTexParameterf");
 }
 
+
 void
 GLVideoView::start ()
 {
@@ -566,7 +608,7 @@ GLVideoView::set_image_and_draw ()
 {
        auto pv = player_video().first;
        if (pv) {
-               set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
+               set_image (pv);
                draw (pv->inter_position(), pv->inter_size());
                _viewer->image_changed (pv);
        }
index bac195fb18016e5758d07a629bb48b8d71514a90..4bab64348f078d00db84b1f04c1e7166f6e43939 100644 (file)
@@ -58,7 +58,7 @@ public:
        }
 
 private:
-       void set_image (std::shared_ptr<const Image> image);
+       void set_image (std::shared_ptr<const PlayerVideo> pv);
        void set_image_and_draw ();
        void draw (Position<int> inter_position, dcp::Size inter_size);
        void thread ();
@@ -86,7 +86,7 @@ private:
        boost::atomic<bool> _one_shot;
 
        GLuint _vao;
-       GLint _draw_border;
+       GLint _fragment_type;
        bool _setup_shaders_done = false;
 
        std::shared_ptr<wxTimer> _timer;
index 9517a3bf479a2f31b8d9c7fdfbff955fa29e9509..5353f213f8803e3e2a9865c6a0567610edf51fc9 100644 (file)
@@ -127,6 +127,10 @@ public:
                _three_d = t;
        }
 
+       void set_optimise_for_j2k (bool o) {
+               _optimise_for_j2k = o;
+       }
+
 protected:
        NextFrameResult get_next_frame (bool non_blocking);
        boost::optional<int> time_until_next_frame () const;
@@ -168,6 +172,8 @@ protected:
 
        StateTimer _state_timer;
 
+       bool _optimise_for_j2k = false;
+
 private:
        /** Mutex protecting all the state in this class */
        mutable boost::mutex _mutex;