From 5bbf16e87ca09369174c9d2bb7f7fe94b6d70275 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sat, 26 Nov 2016 15:14:11 +0000 Subject: Reduce during J2K decode where possible for playback (#986). --- src/lib/image_proxy.h | 12 +++++++++-- src/lib/j2k_image_proxy.cc | 49 +++++++++++++++++++++++++------------------ src/lib/j2k_image_proxy.h | 8 +++++-- src/lib/magick_image_proxy.cc | 2 +- src/lib/magick_image_proxy.h | 6 +++++- src/lib/player_video.cc | 2 +- src/lib/raw_image_proxy.cc | 2 +- src/lib/raw_image_proxy.h | 6 +++++- 8 files changed, 58 insertions(+), 29 deletions(-) (limited to 'src/lib') diff --git a/src/lib/image_proxy.h b/src/lib/image_proxy.h index b0ec16110..d7bd7b0e6 100644 --- a/src/lib/image_proxy.h +++ b/src/lib/image_proxy.h @@ -60,8 +60,16 @@ class ImageProxy : public boost::noncopyable public: virtual ~ImageProxy () {} - /** @return Image (which must be aligned) */ - virtual boost::shared_ptr image (boost::optional note = boost::optional ()) const = 0; + /** @param note Handler for any notes that occur. + * @param size Size that the returned image will be scaled to, in case this + * can be used as an optimisation. + * @return Image (which must be aligned) + */ + virtual boost::shared_ptr image ( + boost::optional note = boost::optional (), + boost::optional size = boost::optional () + ) const = 0; + virtual void add_metadata (xmlpp::Node *) const = 0; virtual void send_binary (boost::shared_ptr) const = 0; /** @return true if our image is definitely the same as another, false if it is probably not */ diff --git a/src/lib/j2k_image_proxy.cc b/src/lib/j2k_image_proxy.cc index fb38c9e99..4e38213c8 100644 --- a/src/lib/j2k_image_proxy.cc +++ b/src/lib/j2k_image_proxy.cc @@ -30,12 +30,14 @@ #include #include #include +#include #include #include "i18n.h" using std::string; using std::cout; +using std::max; using boost::shared_ptr; using boost::optional; using boost::dynamic_pointer_cast; @@ -91,46 +93,53 @@ J2KImageProxy::J2KImageProxy (shared_ptr xml, shared_ptr soc socket->read (_data.data().get (), _data.size ()); } -void -J2KImageProxy::ensure_j2k () const -{ - if (!_j2k) { - _j2k = dcp::decompress_j2k (const_cast (_data.data().get()), _data.size (), 0); - } -} - shared_ptr -J2KImageProxy::image (optional) const +J2KImageProxy::image (optional, optional target_size) const { - ensure_j2k (); - - if (_j2k->precision(0) < 12) { - int const shift = 12 - _j2k->precision (0); - for (int c = 0; c < 3; ++c) { - int* p = _j2k->data (c); - for (int y = 0; y < _j2k->size().height; ++y) { - for (int x = 0; x < _j2k->size().width; ++x) { - *p++ <<= shift; + if (!_j2k || target_size != _j2k_target_size) { + int reduce = 0; + + 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); + _j2k = dcp::decompress_j2k (const_cast (_data.data().get()), _data.size (), reduce); + + if (_j2k->precision(0) < 12) { + int const shift = 12 - _j2k->precision (0); + for (int c = 0; c < 3; ++c) { + int* p = _j2k->data (c); + for (int y = 0; y < _j2k->size().height; ++y) { + for (int x = 0; x < _j2k->size().width; ++x) { + *p++ <<= shift; + } } } } + + _j2k_target_size = target_size; } - shared_ptr image (new Image (_pixel_format, _size, true)); + shared_ptr image (new Image (_pixel_format, _j2k->size(), true)); /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming the data is 12-bit either way. */ + int const width = _j2k->size().width; + int p = 0; for (int y = 0; y < _j2k->size().height; ++y) { uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]); - for (int x = 0; x < _j2k->size().width; ++x) { + for (int x = 0; x < width; ++x) { for (int c = 0; c < 3; ++c) { *q++ = _j2k->data(c)[p] << 4; } ++p; } + p += _j2k->factor(0) * width; } return image; diff --git a/src/lib/j2k_image_proxy.h b/src/lib/j2k_image_proxy.h index 96a776f2a..8fc9040d5 100644 --- a/src/lib/j2k_image_proxy.h +++ b/src/lib/j2k_image_proxy.h @@ -35,7 +35,11 @@ public: J2KImageProxy (boost::shared_ptr frame, dcp::Size, dcp::Eye, AVPixelFormat pixel_format); J2KImageProxy (boost::shared_ptr xml, boost::shared_ptr socket); - boost::shared_ptr image (boost::optional note = boost::optional ()) const; + boost::shared_ptr image ( + boost::optional note = boost::optional (), + boost::optional size = boost::optional () + ) const; + void add_metadata (xmlpp::Node *) const; void send_binary (boost::shared_ptr) const; /** @return true if our image is definitely the same as another, false if it is probably not */ @@ -57,11 +61,11 @@ private: /* For tests */ J2KImageProxy (dcp::Data data, dcp::Size size, AVPixelFormat pixel_format); - void ensure_j2k () const; dcp::Data _data; dcp::Size _size; boost::optional _eye; mutable boost::shared_ptr _j2k; + mutable boost::optional _j2k_target_size; AVPixelFormat _pixel_format; }; diff --git a/src/lib/magick_image_proxy.cc b/src/lib/magick_image_proxy.cc index 2d1867fcc..b8255c9ad 100644 --- a/src/lib/magick_image_proxy.cc +++ b/src/lib/magick_image_proxy.cc @@ -67,7 +67,7 @@ MagickImageProxy::MagickImageProxy (shared_ptr, shared_ptr s } shared_ptr -MagickImageProxy::image (optional) const +MagickImageProxy::image (optional, optional) const { boost::mutex::scoped_lock lm (_mutex); diff --git a/src/lib/magick_image_proxy.h b/src/lib/magick_image_proxy.h index e255b1336..5c4532add 100644 --- a/src/lib/magick_image_proxy.h +++ b/src/lib/magick_image_proxy.h @@ -29,7 +29,11 @@ public: MagickImageProxy (boost::filesystem::path); MagickImageProxy (boost::shared_ptr xml, boost::shared_ptr socket); - boost::shared_ptr image (boost::optional note = boost::optional ()) const; + boost::shared_ptr image ( + boost::optional note = boost::optional (), + boost::optional size = boost::optional () + ) const; + void add_metadata (xmlpp::Node *) const; void send_binary (boost::shared_ptr) const; bool same (boost::shared_ptr other) const; diff --git a/src/lib/player_video.cc b/src/lib/player_video.cc index 8075b7b7d..5e34ddf97 100644 --- a/src/lib/player_video.cc +++ b/src/lib/player_video.cc @@ -106,7 +106,7 @@ PlayerVideo::set_subtitle (PositionImage image) shared_ptr PlayerVideo::image (dcp::NoteHandler note, function pixel_format, bool aligned, bool fast) const { - shared_ptr im = _in->image (optional (note)); + shared_ptr im = _in->image (optional (note), _inter_size); Crop total_crop = _crop; switch (_part) { diff --git a/src/lib/raw_image_proxy.cc b/src/lib/raw_image_proxy.cc index 6ebcee60c..ea702f138 100644 --- a/src/lib/raw_image_proxy.cc +++ b/src/lib/raw_image_proxy.cc @@ -53,7 +53,7 @@ RawImageProxy::RawImageProxy (shared_ptr xml, shared_ptr soc } shared_ptr -RawImageProxy::image (optional) const +RawImageProxy::image (optional, optional) const { return _image; } diff --git a/src/lib/raw_image_proxy.h b/src/lib/raw_image_proxy.h index 379c06f32..28fd7f263 100644 --- a/src/lib/raw_image_proxy.h +++ b/src/lib/raw_image_proxy.h @@ -29,7 +29,11 @@ public: RawImageProxy (boost::shared_ptr); RawImageProxy (boost::shared_ptr xml, boost::shared_ptr socket); - boost::shared_ptr image (boost::optional note = boost::optional ()) const; + boost::shared_ptr image ( + boost::optional note = boost::optional (), + boost::optional size = boost::optional () + ) const; + void add_metadata (xmlpp::Node *) const; void send_binary (boost::shared_ptr) const; bool same (boost::shared_ptr) const; -- cgit v1.2.3