Initial work on removing storage of subtitle times.
[dcpomatic.git] / src / lib / subtitle_decoder.cc
index c59406c3659c80659d10b3bd11ce39ec6d77aeab..43ee4c457c040c658f415180aef2db4536b58549 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -40,34 +40,29 @@ using boost::function;
 SubtitleDecoder::SubtitleDecoder (
        Decoder* parent,
        shared_ptr<const SubtitleContent> c,
-       shared_ptr<Log> log,
-       function<list<ContentTimePeriod> (ContentTimePeriod, bool)> image_during,
-       function<list<ContentTimePeriod> (ContentTimePeriod, bool)> text_during
+       shared_ptr<Log> 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> image, dcpomatic::Rect<double> rect)
+SubtitleDecoder::emit_image_start (ContentTime from, shared_ptr<Image> image, dcpomatic::Rect<double> rect)
 {
-       _decoded_image.push_back (ContentImageSubtitle (period, image, rect));
-       _position = period.from;
+       ImageStart (ContentImageSubtitle (from, image, rect));
 }
 
 void
-SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString> s)
+SubtitleDecoder::emit_text_start (ContentTime from, list<dcp::SubtitleString> 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.)
@@ -79,109 +74,12 @@ SubtitleDecoder::give_text (ContentTimePeriod period, list<dcp::SubtitleString>
                i.set_text (t);
        }
 
-       _decoded_text.push_back (ContentTextSubtitle (period, s));
-       _position = period.from;
-}
-
-/** 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 <class T>
-list<T>
-SubtitleDecoder::get (list<T> const & subs, list<ContentTimePeriod> const & sp, ContentTimePeriod period, bool accurate)
-{
-       if (sp.empty ()) {
-               return list<T> ();
-       }
-
-       /* Find the time of the first subtitle we don't have in subs */
-       optional<ContentTime> missing;
-       BOOST_FOREACH (ContentTimePeriod i, sp) {
-               typename list<T>::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<T> out;
-       BOOST_FOREACH (ContentTimePeriod i, sp) {
-               typename list<T>::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<ContentImageSubtitle>::iterator i = _decoded_image.begin();
-       while (i != _decoded_image.end()) {
-               list<ContentImageSubtitle>::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<ContentTextSubtitle>
-SubtitleDecoder::get_text (ContentTimePeriod period, bool starting, bool accurate)
-{
-       return get<ContentTextSubtitle> (_decoded_text, _text_during (period, starting), period, accurate);
-}
-
-list<ContentImageSubtitle>
-SubtitleDecoder::get_image (ContentTimePeriod period, bool starting, bool accurate)
-{
-       return get<ContentImageSubtitle> (_decoded_image, _image_during (period, starting), period, accurate);
-}
-
-void
-SubtitleDecoder::seek (ContentTime t, bool)
-{
-       _log->log (String::compose ("SD seek to %1", to_string(t)), LogEntry::TYPE_DEBUG_DECODE);
-       reset ();
-       _position.reset ();
-}
-
-void
-SubtitleDecoder::reset ()
-{
-       _decoded_text.clear ();
-       _decoded_image.clear ();
+       TextStart (ContentTextSubtitle (from, s));
+       _position = from;
 }
 
 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;
@@ -272,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,
@@ -289,5 +188,25 @@ 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<dcp::SubtitleString> 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);
 }