X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fsubtitle_decoder.cc;h=0f5a1992c46bad7d9e43d58bf4048a601a0bc0a3;hb=308488324dbc4d8b709d3fb1dc9fee0479346c21;hp=e5cadb7b48b4e513c3b11a7ad47babc10ecda689;hpb=015fe447cfe25babc55cf8ed282bb909e4713aa0;p=dcpomatic.git diff --git a/src/lib/subtitle_decoder.cc b/src/lib/subtitle_decoder.cc index e5cadb7b4..0f5a1992c 100644 --- a/src/lib/subtitle_decoder.cc +++ b/src/lib/subtitle_decoder.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2014 Carl Hetherington 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,27 +21,75 @@ #include "subtitle_decoder.h" using std::list; +using std::cout; using boost::shared_ptr; using boost::optional; -SubtitleDecoder::SubtitleDecoder (shared_ptr f) - : Decoder (f) +SubtitleDecoder::SubtitleDecoder () { } - /** Called by subclasses when an image subtitle is ready. * Image may be 0 to say that there is no current subtitle. */ void -SubtitleDecoder::image_subtitle (shared_ptr image, dcpomatic::Rect rect, ContentTime from, ContentTime to) +SubtitleDecoder::image_subtitle (ContentTime from, ContentTime to, shared_ptr image, dcpomatic::Rect rect) +{ + _decoded_image_subtitles.push_back (shared_ptr (new ContentImageSubtitle (from, to, image, rect))); +} + +void +SubtitleDecoder::text_subtitle (list s) +{ + _decoded_text_subtitles.push_back (shared_ptr (new ContentTextSubtitle (s))); +} + +template +list > +SubtitleDecoder::get (list > 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.empty() || (subs.front()->from() > from || to < subs.back()->to()))) {} + + /* Now look for what we wanted in the data we have collected */ + /* XXX: inefficient */ + + list > out; + for (typename list >::const_iterator i = subs.begin(); i != subs.end(); ++i) { + if ((*i)->from() <= to && (*i)->to() >= from) { + out.push_back (*i); + } + } + + return out; +} + +list > +SubtitleDecoder::get_text_subtitles (ContentTime from, ContentTime to) +{ + return get (_decoded_text_subtitles, from, to); +} + +list > +SubtitleDecoder::get_image_subtitles (ContentTime from, ContentTime to) { - _pending.push_back (shared_ptr (new DecodedImageSubtitle (image, rect, from, to))); + return get (_decoded_image_subtitles, from, to); } void -SubtitleDecoder::text_subtitle (list s) +SubtitleDecoder::seek (ContentTime, bool) { - _pending.push_back (shared_ptr (new DecodedTextSubtitle (s))); + _decoded_text_subtitles.clear (); + _decoded_image_subtitles.clear (); }