Move deinterleave of OpenJPEGImage to Image into the prepare(), meaning
authorCarl Hetherington <cth@carlh.net>
Tue, 6 Nov 2018 01:32:06 +0000 (01:32 +0000)
committerCarl Hetherington <cth@carlh.net>
Tue, 6 Nov 2018 01:42:52 +0000 (01:42 +0000)
that it can be multi-threaded.  This allows the bit shift for non-12bpp
JPEG2000 sources to be done at the same time as the deinterleave.
Should speed up DCP playback in some cases.

src/lib/j2k_image_proxy.cc
src/lib/j2k_image_proxy.h

index 1ef00d9465feb924cea3d9df48e87f862319dbb0..1422b6948426c9db21858a16d5dec27ae6d3b50e 100644 (file)
@@ -118,7 +118,7 @@ J2KImageProxy::prepare (optional<dcp::Size> target_size) const
 {
        boost::mutex::scoped_lock lm (_mutex);
 
-       if (_decompressed && target_size == _target_size) {
+       if (_image && target_size == _target_size) {
                DCPOMATIC_ASSERT (_reduce);
                return *_reduce;
        }
@@ -136,54 +136,42 @@ J2KImageProxy::prepare (optional<dcp::Size> target_size) const
                reduce = max (0, reduce);
        }
 
-       _decompressed = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), reduce);
-
-       if (_decompressed->precision(0) < 12) {
-               int const shift = 12 - _decompressed->precision (0);
-               for (int c = 0; c < 3; ++c) {
-                       int* p = _decompressed->data (c);
-                       for (int y = 0; y < _decompressed->size().height; ++y) {
-                               for (int x = 0; x < _decompressed->size().width; ++x) {
-                                       *p++ <<= shift;
-                               }
-                       }
-               }
-       }
-
-       _target_size = target_size;
-       _reduce = reduce;
+       shared_ptr<dcp::OpenJPEGImage> decompressed = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), reduce);
 
-       return reduce;
-}
-
-pair<shared_ptr<Image>, int>
-J2KImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size> target_size) const
-{
-       int const reduce = prepare (target_size);
+       _image.reset (new Image (_pixel_format, decompressed->size(), true));
 
-       shared_ptr<Image> image (new Image (_pixel_format, _decompressed->size(), true));
+       int const shift = 16 - decompressed->precision (0);
 
        /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
           the data is 12-bit either way.
        */
 
-       int const width = _decompressed->size().width;
+       int const width = decompressed->size().width;
 
        int p = 0;
-       int* decomp_0 = _decompressed->data (0);
-       int* decomp_1 = _decompressed->data (1);
-       int* decomp_2 = _decompressed->data (2);
-       for (int y = 0; y < _decompressed->size().height; ++y) {
-               uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]);
+       int* decomp_0 = decompressed->data (0);
+       int* decomp_1 = decompressed->data (1);
+       int* decomp_2 = decompressed->data (2);
+       for (int y = 0; y < decompressed->size().height; ++y) {
+               uint16_t* q = (uint16_t *) (_image->data()[0] + y * _image->stride()[0]);
                for (int x = 0; x < width; ++x) {
-                       *q++ = decomp_0[p] << 4;
-                       *q++ = decomp_1[p] << 4;
-                       *q++ = decomp_2[p] << 4;
+                       *q++ = decomp_0[p] << shift;
+                       *q++ = decomp_1[p] << shift;
+                       *q++ = decomp_2[p] << shift;
                        ++p;
                }
        }
 
-       return make_pair (image, reduce);
+       _target_size = target_size;
+       _reduce = reduce;
+
+       return reduce;
+}
+
+pair<shared_ptr<Image>, int>
+J2KImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size> target_size) const
+{
+       return make_pair (_image, prepare(target_size));
 }
 
 void
@@ -232,9 +220,9 @@ size_t
 J2KImageProxy::memory_used () const
 {
        size_t m = _data.size();
-       if (_decompressed) {
+       if (_image) {
                /* 3 components, 16-bits per pixel */
-               m += 3 * 2 * _decompressed->size().width * _decompressed->size().height;
+               m += 3 * 2 * _image->size().width * _image->size().height;
        }
        return m;
 }
index a67cc24deeffea184215ad221e371b3b2c288c90..8e9a0fa99bff546a622bca8bee6fcc03dd4d6b13 100644 (file)
@@ -80,7 +80,7 @@ private:
        dcp::Data _data;
        dcp::Size _size;
        boost::optional<dcp::Eye> _eye;
-       mutable boost::shared_ptr<dcp::OpenJPEGImage> _decompressed;
+       mutable boost::shared_ptr<Image> _image;
        mutable boost::optional<dcp::Size> _target_size;
        mutable boost::optional<int> _reduce;
        AVPixelFormat _pixel_format;