X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fsubtitle_decoder.cc;h=4e68e4a79ffb596962c518c9250c41f66b5c06fe;hb=2b1f8bb12adc35dcd7f352885cb0f9a80b04093e;hp=a7cf8110f5fc3bd56baa5eff95d62263f8d3f11e;hpb=878e19aabf2278828a3c9b518e0804b2cef0c01e;p=dcpomatic.git diff --git a/src/lib/subtitle_decoder.cc b/src/lib/subtitle_decoder.cc index a7cf8110f..4e68e4a79 100644 --- a/src/lib/subtitle_decoder.cc +++ b/src/lib/subtitle_decoder.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2016 Carl Hetherington + Copyright (C) 2013-2017 Carl Hetherington This file is part of DCP-o-matic. @@ -40,33 +40,29 @@ using boost::function; SubtitleDecoder::SubtitleDecoder ( Decoder* parent, shared_ptr c, - shared_ptr log, - function (ContentTimePeriod, bool)> image_during, - function (ContentTimePeriod, bool)> text_during + shared_ptr log ) : DecoderPart (parent, log) , _content (c) - , _image_during (image_during) - , _text_during (text_during) { } -/** Called by subclasses when an image subtitle is ready. - * @param period Period of the subtitle. +/** Called by subclasses when an image subtitle is starting. + * @param from From time of the subtitle. * @param image Subtitle image. * @param rect Area expressed as a fraction of the video frame that this subtitle * is for (e.g. a width of 0.5 means the width of the subtitle is half the width * of the video frame) */ void -SubtitleDecoder::give_image (ContentTimePeriod period, shared_ptr image, dcpomatic::Rect rect) +SubtitleDecoder::emit_image_start (ContentTime from, shared_ptr image, dcpomatic::Rect rect) { - _decoded_image.push_back (ContentImageSubtitle (period, image, rect)); + ImageStart (ContentImageSubtitle (from, image, rect)); } void -SubtitleDecoder::give_text (ContentTimePeriod period, list s) +SubtitleDecoder::emit_text_start (ContentTime from, list s) { /* We must escape < and > in strings, otherwise they might confuse our subtitle renderer (which uses some HTML-esque markup to do bold/italic etc.) @@ -78,106 +74,12 @@ SubtitleDecoder::give_text (ContentTimePeriod period, list i.set_text (t); } - _decoded_text.push_back (ContentTextSubtitle (period, s)); -} - -/** Get the subtitles that correspond to a given list of periods. - * @param subs Subtitles. - * @param sp Periods for which to extract subtitles from subs. - */ -template -list -SubtitleDecoder::get (list const & subs, list const & sp, ContentTimePeriod period, bool accurate) -{ - if (sp.empty ()) { - return list (); - } - - /* Find the time of the first subtitle we don't have in subs */ - optional missing; - BOOST_FOREACH (ContentTimePeriod i, sp) { - typename list::const_iterator j = subs.begin(); - while (j != subs.end() && j->period() != i) { - ++j; - } - if (j == subs.end ()) { - missing = i.from; - } - } - - /* Suggest to our parent decoder that it might want to seek if we haven't got what we're being asked for */ - if (missing) { - _log->log (String::compose ("SD suggests seek to %1", to_string (*missing)), LogEntry::TYPE_DEBUG_DECODE); - maybe_seek (*missing, true); - } - - /* Now enough pass() calls will either: - * (a) give us what we want, or - * (b) hit the end of the decoder. - */ - while (!_parent->pass(Decoder::PASS_REASON_SUBTITLE, accurate) && (subs.empty() || (subs.back().period().to < sp.back().to))) {} - - /* Now look for what we wanted in the data we have collected */ - /* XXX: inefficient */ - - list out; - BOOST_FOREACH (ContentTimePeriod i, sp) { - typename list::const_iterator j = subs.begin(); - while (j != subs.end() && j->period() != i) { - ++j; - } - if (j != subs.end()) { - out.push_back (*j); - } - } - - /* Discard anything in _decoded_image_subtitles that is outside 5 seconds either side of period */ - - list::iterator i = _decoded_image.begin(); - while (i != _decoded_image.end()) { - list::iterator tmp = i; - ++tmp; - - if ( - i->period().to < (period.from - ContentTime::from_seconds (5)) || - i->period().from > (period.to + ContentTime::from_seconds (5)) - ) { - _decoded_image.erase (i); - } - - i = tmp; - } - - return out; -} - -list -SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate) -{ - return get (_decoded_text, _text_during (period, starting), period, accurate); -} - -list -SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate) -{ - return get (_decoded_image, _image_during (period, starting), period, accurate); -} - -void -SubtitleDecoder::seek (ContentTime, bool) -{ - reset (); + TextStart (ContentTextSubtitle (from, s)); + _position = from; } void -SubtitleDecoder::reset () -{ - _decoded_text.clear (); - _decoded_image.clear (); -} - -void -SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subtitle) +SubtitleDecoder::emit_text_start (ContentTime from, sub::Subtitle const & subtitle) { /* See if our next subtitle needs to be placed on screen by us */ bool needs_placement = false; @@ -268,8 +170,9 @@ SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subt content()->colour(), j.font_size.points (72 * 11), 1.0, - dcp::Time (period.from.seconds(), 1000), - dcp::Time (period.to.seconds(), 1000), + dcp::Time (from.seconds(), 1000), + /* XXX: hmm; this is a bit ugly (we don't know the to time yet) */ + dcp::Time (), 0, dcp::HALIGN_CENTER, v_position, @@ -285,5 +188,31 @@ SubtitleDecoder::give_text (ContentTimePeriod period, sub::Subtitle const & subt } } - give_text (period, out); + emit_text_start (from, out); +} + +void +SubtitleDecoder::emit_stop (ContentTime to) +{ + Stop (to); +} + +void +SubtitleDecoder::emit_text (ContentTimePeriod period, list s) +{ + emit_text_start (period.from, s); + emit_stop (period.to); +} + +void +SubtitleDecoder::emit_text (ContentTimePeriod period, sub::Subtitle const & s) +{ + emit_text_start (period.from, s); + emit_stop (period.to); +} + +void +SubtitleDecoder::seek () +{ + _position = ContentTime (); }