Untested; allow viewing of subtitles or closed captions in the preview.
[dcpomatic.git] / src / lib / player_video.cc
index c24f9ccaa06b4d70b0b552c62ad4d31876d17c55..c8fb044aa191d9c8b766fd35a64d5c0c8e07463d 100644 (file)
@@ -34,6 +34,7 @@ extern "C" {
 
 using std::string;
 using std::cout;
+using std::pair;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
@@ -91,14 +92,14 @@ PlayerVideo::PlayerVideo (shared_ptr<cxml::Node> node, shared_ptr<Socket> socket
 
                image->read_from_socket (socket);
 
-               _subtitle = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
+               _caption = PositionImage (image, Position<int> (node->number_child<int> ("SubtitleX"), node->number_child<int> ("SubtitleY")));
        }
 }
 
 void
-PlayerVideo::set_subtitle (PositionImage image)
+PlayerVideo::set_caption (PositionImage image)
 {
-       _subtitle = image;
+       _caption = image;
 }
 
 /** Create an image for this frame.
@@ -112,7 +113,9 @@ PlayerVideo::set_subtitle (PositionImage image)
 shared_ptr<Image>
 PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast) const
 {
-       shared_ptr<Image> im = _in->image (optional<dcp::NoteHandler> (note), _inter_size);
+       pair<shared_ptr<Image>, int> prox = _in->image (optional<dcp::NoteHandler> (note), _inter_size);
+       shared_ptr<Image> im = prox.first;
+       int const reduce = prox.second;
 
        Crop total_crop = _crop;
        switch (_part) {
@@ -132,6 +135,15 @@ PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat
                break;
        }
 
+       if (reduce > 0) {
+               /* Scale the crop down to account for the scaling that has already happened in ImageProxy::image */
+               int const r = pow(2, reduce);
+               total_crop.left /= r;
+               total_crop.right /= r;
+               total_crop.top /= r;
+               total_crop.bottom /= r;
+       }
+
        dcp::YUVToRGB yuv_to_rgb = dcp::YUV_TO_RGB_REC601;
        if (_colour_conversion) {
                yuv_to_rgb = _colour_conversion.get().yuv_to_rgb();
@@ -141,8 +153,8 @@ PlayerVideo::image (dcp::NoteHandler note, function<AVPixelFormat (AVPixelFormat
                total_crop, _inter_size, _out_size, yuv_to_rgb, pixel_format (_in->pixel_format()), aligned, fast
                );
 
-       if (_subtitle) {
-               out->alpha_blend (Image::ensure_aligned (_subtitle->image), _subtitle->position);
+       if (_caption) {
+               out->alpha_blend (Image::ensure_aligned (_caption->image), _caption->position);
        }
 
        if (_fade) {
@@ -169,11 +181,11 @@ PlayerVideo::add_metadata (xmlpp::Node* node) const
        if (_colour_conversion) {
                _colour_conversion.get().as_xml (node);
        }
-       if (_subtitle) {
-               node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_subtitle->image->size().width));
-               node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_subtitle->image->size().height));
-               node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_subtitle->position.x));
-               node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_subtitle->position.y));
+       if (_caption) {
+               node->add_child ("SubtitleWidth")->add_child_text (raw_convert<string> (_caption->image->size().width));
+               node->add_child ("SubtitleHeight")->add_child_text (raw_convert<string> (_caption->image->size().height));
+               node->add_child ("SubtitleX")->add_child_text (raw_convert<string> (_caption->position.x));
+               node->add_child ("SubtitleY")->add_child_text (raw_convert<string> (_caption->position.y));
        }
 }
 
@@ -181,8 +193,8 @@ void
 PlayerVideo::send_binary (shared_ptr<Socket> socket) const
 {
        _in->send_binary (socket);
-       if (_subtitle) {
-               _subtitle->image->write_to_socket (socket);
+       if (_caption) {
+               _caption->image->write_to_socket (socket);
        }
 }
 
@@ -196,7 +208,7 @@ PlayerVideo::has_j2k () const
                return false;
        }
 
-       return _crop == Crop () && _out_size == j2k->size() && !_subtitle && !_fade && !_colour_conversion;
+       return _crop == Crop () && _out_size == j2k->size() && !_caption && !_fade && !_colour_conversion;
 }
 
 Data
@@ -227,13 +239,13 @@ PlayerVideo::same (shared_ptr<const PlayerVideo> other) const
                return false;
        }
 
-       if ((!_subtitle && other->_subtitle) || (_subtitle && !other->_subtitle)) {
-               /* One has a subtitle and the other doesn't */
+       if ((!_caption && other->_caption) || (_caption && !other->_caption)) {
+               /* One has a caption and the other doesn't */
                return false;
        }
 
-       if (_subtitle && other->_subtitle && !_subtitle->same (other->_subtitle.get ())) {
-               /* They both have subtitles but they are different */
+       if (_caption && other->_caption && !_caption->same (other->_caption.get ())) {
+               /* They both have captions but they are different */
                return false;
        }
 
@@ -266,7 +278,7 @@ PlayerVideo::memory_used () const
        return _in->memory_used();
 }
 
-/** @return Shallow copy of this; _in and _subtitle are shared between the original and the copy */
+/** @return Shallow copy of this; _in and _caption are shared between the original and the copy */
 shared_ptr<PlayerVideo>
 PlayerVideo::shallow_copy () const
 {
@@ -286,17 +298,22 @@ PlayerVideo::shallow_copy () const
                );
 }
 
-/** Re-read crop, fade, inter/out size and colour conversion from our content */
-void
+/** Re-read crop, fade, inter/out size and colour conversion from our content.
+ *  @return true if this was possible, false if not.
+ */
+bool
 PlayerVideo::reset_metadata (dcp::Size video_container_size, dcp::Size film_frame_size)
 {
        shared_ptr<Content> content = _content.lock();
-       DCPOMATIC_ASSERT (content);
-       DCPOMATIC_ASSERT (_video_frame);
+       if (!content || !_video_frame) {
+               return false;
+       }
 
        _crop = content->video->crop();
        _fade = content->video->fade(_video_frame.get());
        _inter_size = content->video->scale().size(content->video, video_container_size, film_frame_size);
        _out_size = video_container_size;
        _colour_conversion = content->video->colour_conversion();
+
+       return true;
 }