Add missing seek stuff to SubtitleDecoder.
authorCarl Hetherington <cth@carlh.net>
Fri, 2 May 2014 09:42:06 +0000 (10:42 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 2 May 2014 09:42:06 +0000 (10:42 +0100)
src/lib/subrip_decoder.cc
src/lib/subtitle_decoder.cc
src/lib/subtitle_decoder.h

index 013c6fab7e6d2eab6f93b0ea290ac27fb3dae553..cdc8ccbfe4613f9a745966121101be82fb640a1c 100644 (file)
@@ -31,8 +31,10 @@ SubRipDecoder::SubRipDecoder (shared_ptr<const SubRipContent> content)
 }
 
 void
-SubRipDecoder::seek (ContentTime time, bool)
+SubRipDecoder::seek (ContentTime time, bool accurate)
 {
+       SubtitleDecoder::seek (time, accurate);
+       
        _next = 0;
        list<SubRipSubtitlePiece>::const_iterator i = _subtitles[_next].pieces.begin();
        while (i != _subtitles[_next].pieces.end() && _subtitles[_next].from < time) {
index 3daa6e431820ef5b249c1a86ceacc27cf5d85f84..92355ad62fed6f38a362960373716a918868dada 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@
 #include "subtitle_decoder.h"
 
 using std::list;
+using std::cout;
 using boost::shared_ptr;
 using boost::optional;
 
@@ -46,9 +47,24 @@ SubtitleDecoder::text_subtitle (list<dcp::SubtitleString> s)
 
 template <class T>
 list<shared_ptr<T> >
-get (list<shared_ptr<T> > const & subs, ContentTime from, ContentTime to)
+SubtitleDecoder::get (list<shared_ptr<T> > const & subs, ContentTime from, ContentTime to)
 {
+       if (subs.empty() || from < subs.front()->from() || to > (subs.back()->to() + ContentTime::from_seconds (10))) {
+               /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
+               seek (from, true);
+       }
+
+       /* Now enough pass() calls will either:
+        *  (a) give us what we want, or
+        *  (b) hit the end of the decoder.
+        *
+        *  XXX: with subs being sparse, this may need more care...
+        */
+       while (!pass() && (subs.front()->from() > from || to < subs.back()->to())) {}
+
+       /* Now look for what we wanted in the data we have collected */
        /* XXX: inefficient */
+       
        list<shared_ptr<T> > out;
        for (typename list<shared_ptr<T> >::const_iterator i = subs.begin(); i != subs.end(); ++i) {
                if ((*i)->from() <= to && (*i)->to() >= from) {
@@ -70,3 +86,10 @@ SubtitleDecoder::get_image_subtitles (ContentTime from, ContentTime to)
 {
        return get<ContentImageSubtitle> (_decoded_image_subtitles, from, to);
 }
+
+void
+SubtitleDecoder::seek (ContentTime, bool)
+{
+       _decoded_text_subtitles.clear ();
+       _decoded_image_subtitles.clear ();
+}
index efa90fd92c7950280e8c900b42ccf3ce591a1b1d..a26348ee69a61b4842dd173446b2846c96fc0c70 100644 (file)
@@ -39,11 +39,17 @@ public:
        std::list<boost::shared_ptr<ContentTextSubtitle> > get_text_subtitles (ContentTime from, ContentTime to);
 
 protected:
+       void seek (ContentTime, bool);
+       
        void image_subtitle (ContentTime from, ContentTime to, boost::shared_ptr<Image>, dcpomatic::Rect<double>);
        void text_subtitle (std::list<dcp::SubtitleString>);
 
        std::list<boost::shared_ptr<ContentImageSubtitle> > _decoded_image_subtitles;
        std::list<boost::shared_ptr<ContentTextSubtitle> > _decoded_text_subtitles;
+
+private:
+       template <class T>
+       std::list<boost::shared_ptr<T> > get (std::list<boost::shared_ptr<T> > const & subs, ContentTime from, ContentTime to);
 };
 
 #endif