Fix incorrect container size when loading a VF/OV combination into the player.
[dcpomatic.git] / src / lib / j2k_image_proxy.cc
index 9e68951e95b8e8823ab12d46346e96ac8ff1ab51..9100414e442760ce1c532026afbd5b945e367e5c 100644 (file)
@@ -21,6 +21,7 @@
 #include "j2k_image_proxy.h"
 #include "dcpomatic_socket.h"
 #include "image.h"
+#include "dcpomatic_assert.h"
 #include <dcp/raw_convert.h>
 #include <dcp/openjpeg_image.h>
 #include <dcp/mono_picture_frame.h>
@@ -38,6 +39,8 @@
 using std::string;
 using std::cout;
 using std::max;
+using std::pair;
+using std::make_pair;
 using boost::shared_ptr;
 using boost::optional;
 using boost::dynamic_pointer_cast;
@@ -53,18 +56,31 @@ J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size, AVPi
 
 }
 
-J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size, AVPixelFormat pixel_format)
+J2KImageProxy::J2KImageProxy (
+       shared_ptr<const dcp::MonoPictureFrame> frame,
+       dcp::Size size,
+       AVPixelFormat pixel_format,
+       optional<int> forced_reduction
+       )
        : _data (frame->j2k_size ())
        , _size (size)
        , _pixel_format (pixel_format)
+       , _forced_reduction (forced_reduction)
 {
        memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
 }
 
-J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye, AVPixelFormat pixel_format)
+J2KImageProxy::J2KImageProxy (
+       shared_ptr<const dcp::StereoPictureFrame> frame,
+       dcp::Size size,
+       dcp::Eye eye,
+       AVPixelFormat pixel_format,
+       optional<int> forced_reduction
+       )
        : _size (size)
        , _eye (eye)
        , _pixel_format (pixel_format)
+       , _forced_reduction (forced_reduction)
 {
        switch (eye) {
        case dcp::EYE_LEFT:
@@ -93,23 +109,29 @@ J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> soc
        socket->read (_data.data().get (), _data.size ());
 }
 
-void
+int
 J2KImageProxy::prepare (optional<dcp::Size> target_size) const
 {
        boost::mutex::scoped_lock lm (_mutex);
 
        if (_decompressed && target_size == _target_size) {
-               return;
+               DCPOMATIC_ASSERT (_reduce);
+               return *_reduce;
        }
 
        int reduce = 0;
 
-       while (target_size && (_size.width / pow(2, reduce)) > target_size->width && (_size.height / pow(2, reduce)) > target_size->height) {
-               ++reduce;
+       if (_forced_reduction) {
+               reduce = *_forced_reduction;
+       } else {
+               while (target_size && (_size.width / pow(2, reduce)) > target_size->width && (_size.height / pow(2, reduce)) > target_size->height) {
+                       ++reduce;
+               }
+
+               --reduce;
+               reduce = max (0, reduce);
        }
 
-       --reduce;
-       reduce = max (0, reduce);
        _decompressed = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), reduce);
 
        if (_decompressed->precision(0) < 12) {
@@ -125,12 +147,15 @@ J2KImageProxy::prepare (optional<dcp::Size> target_size) const
        }
 
        _target_size = target_size;
+       _reduce = reduce;
+
+       return reduce;
 }
 
-shared_ptr<Image>
+pair<shared_ptr<Image>, int>
 J2KImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size> target_size) const
 {
-       prepare (target_size);
+       int const reduce = prepare (target_size);
 
        shared_ptr<Image> image (new Image (_pixel_format, _decompressed->size(), true));
 
@@ -151,7 +176,7 @@ J2KImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size> target_siz
                }
        }
 
-       return image;
+       return make_pair (image, reduce);
 }
 
 void
@@ -194,3 +219,14 @@ J2KImageProxy::J2KImageProxy (Data data, dcp::Size size, AVPixelFormat pixel_for
 {
 
 }
+
+size_t
+J2KImageProxy::memory_used () const
+{
+       size_t m = _data.size();
+       if (_decompressed) {
+               /* 3 components, 16-bits per pixel */
+               m += 3 * 2 * _decompressed->size().width * _decompressed->size().height;
+       }
+       return m;
+}