From c88a8a6ec6b396dc90d40a4843160d616a45db76 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 2 Jul 2014 18:10:58 +0100 Subject: [PATCH] Hopefully improve subtitle decoder seeking etc. --- src/lib/dcpomatic_time.cc | 2 +- src/lib/ffmpeg_content.cc | 14 ++++++++------ src/lib/ffmpeg_content.h | 2 +- src/lib/ffmpeg_decoder.cc | 6 +++--- src/lib/ffmpeg_decoder.h | 2 +- src/lib/subrip_decoder.cc | 10 ++++++---- src/lib/subrip_decoder.h | 2 +- src/lib/subtitle_decoder.cc | 13 ++++++++----- src/lib/subtitle_decoder.h | 2 +- 9 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/lib/dcpomatic_time.cc b/src/lib/dcpomatic_time.cc index ae4dea44f..dcdfd97da 100644 --- a/src/lib/dcpomatic_time.cc +++ b/src/lib/dcpomatic_time.cc @@ -53,5 +53,5 @@ operator<< (ostream& s, DCPTime t) bool ContentTimePeriod::overlaps (ContentTimePeriod const & other) const { - return (from < other.to && to > other.from); + return (from < other.to && to >= other.from); } diff --git a/src/lib/ffmpeg_content.cc b/src/lib/ffmpeg_content.cc index a12f45a30..f97e324bb 100644 --- a/src/lib/ffmpeg_content.cc +++ b/src/lib/ffmpeg_content.cc @@ -396,20 +396,22 @@ FFmpegContent::audio_analysis_path () const return p; } -bool -FFmpegContent::has_subtitle_during (ContentTimePeriod period) const +list +FFmpegContent::subtitles_during (ContentTimePeriod period) const { + list d; + shared_ptr stream = subtitle_stream (); if (!stream) { - return false; + return d; } /* XXX: inefficient */ for (vector::const_iterator i = stream->periods.begin(); i != stream->periods.end(); ++i) { - if (i->from <= period.to && i->to >= period.from) { - return true; + if (period.overlaps (*i)) { + d.push_back (*i); } } - return false; + return d; } diff --git a/src/lib/ffmpeg_content.h b/src/lib/ffmpeg_content.h index 367c30103..dbb207959 100644 --- a/src/lib/ffmpeg_content.h +++ b/src/lib/ffmpeg_content.h @@ -108,7 +108,7 @@ public: return _first_video; } - bool has_subtitle_during (ContentTimePeriod) const; + std::list subtitles_during (ContentTimePeriod) const; private: friend class ffmpeg_pts_offset_test; diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc index 885bd0e85..668cab885 100644 --- a/src/lib/ffmpeg_decoder.cc +++ b/src/lib/ffmpeg_decoder.cc @@ -481,8 +481,8 @@ FFmpegDecoder::decode_subtitle_packet () avsubtitle_free (&sub); } -bool -FFmpegDecoder::has_subtitle_during (ContentTimePeriod p) const +list +FFmpegDecoder::subtitles_during (ContentTimePeriod p) const { - return _ffmpeg_content->has_subtitle_during (p); + return _ffmpeg_content->subtitles_during (p); } diff --git a/src/lib/ffmpeg_decoder.h b/src/lib/ffmpeg_decoder.h index d6cb8c246..15811d5b9 100644 --- a/src/lib/ffmpeg_decoder.h +++ b/src/lib/ffmpeg_decoder.h @@ -66,7 +66,7 @@ private: void maybe_add_subtitle (); boost::shared_ptr deinterleave_audio (uint8_t** data, int size); - bool has_subtitle_during (ContentTimePeriod) const; + std::list subtitles_during (ContentTimePeriod) const; boost::shared_ptr _log; diff --git a/src/lib/subrip_decoder.cc b/src/lib/subrip_decoder.cc index 3d971fd4b..370118d30 100644 --- a/src/lib/subrip_decoder.cc +++ b/src/lib/subrip_decoder.cc @@ -79,16 +79,18 @@ SubRipDecoder::pass () return false; } -bool -SubRipDecoder::has_subtitle_during (ContentTimePeriod p) const +list +SubRipDecoder::subtitles_during (ContentTimePeriod p) const { /* XXX: inefficient */ + list d; + for (vector::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) { if (p.overlaps (i->period)) { - return true; + d.push_back (i->period); } } - return false; + return d; } diff --git a/src/lib/subrip_decoder.h b/src/lib/subrip_decoder.h index d6c6e6bc2..927ccc728 100644 --- a/src/lib/subrip_decoder.h +++ b/src/lib/subrip_decoder.h @@ -35,7 +35,7 @@ protected: bool pass (); private: - bool has_subtitle_during (ContentTimePeriod) const; + std::list subtitles_during (ContentTimePeriod) const; size_t _next; }; diff --git a/src/lib/subtitle_decoder.cc b/src/lib/subtitle_decoder.cc index 1b789fdab..b8cb3e5c8 100644 --- a/src/lib/subtitle_decoder.cc +++ b/src/lib/subtitle_decoder.cc @@ -51,20 +51,23 @@ template list SubtitleDecoder::get (list const & subs, ContentTimePeriod period) { - if (!has_subtitle_during (period)) { + /* Get the full periods of the subtitles that are showing during the specified period */ + list sp = subtitles_during (period); + if (sp.empty ()) { + /* Nothing in this period */ return list (); } - if (subs.empty() || period.from < subs.front().period().from || period.to > (subs.back().period().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 (period.from, true); + /* Seek if what we want is before what we have, or more than a reasonable amount after */ + if (subs.empty() || sp.back().to < subs.front().period().from || sp.front().from > (subs.back().period().to + ContentTime::from_seconds (5))) { + seek (sp.front().from, true); } /* Now enough pass() calls will either: * (a) give us what we want, or * (b) hit the end of the decoder. */ - while (!pass() && (subs.empty() || (subs.front().period().from > period.from || period.to < subs.back().period().to))) {} + while (!pass() && (subs.empty() || (subs.back().period().to < sp.back().to))) {} /* Now look for what we wanted in the data we have collected */ /* XXX: inefficient */ diff --git a/src/lib/subtitle_decoder.h b/src/lib/subtitle_decoder.h index 3f06afae3..51faca97f 100644 --- a/src/lib/subtitle_decoder.h +++ b/src/lib/subtitle_decoder.h @@ -51,7 +51,7 @@ private: template std::list get (std::list const & subs, ContentTimePeriod period); - virtual bool has_subtitle_during (ContentTimePeriod) const = 0; + virtual std::list subtitles_during (ContentTimePeriod) const = 0; boost::shared_ptr _subtitle_content; }; -- 2.30.2