Somewhat speculative move to allow -ve seeks, which seem to be necessary for some...
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index dfd8786b37b833621b20d1767279e608ce5335d7..0f2c88fc9f8ce36f06f957b8c4172f6248c2c44c 100644 (file)
@@ -43,8 +43,9 @@ extern "C" {
 #include "filter_graph.h"
 #include "audio_buffers.h"
 #include "ffmpeg_content.h"
-#include "image_proxy.h"
+#include "raw_image_proxy.h"
 #include "film.h"
+#include "timer.h"
 
 #include "i18n.h"
 
@@ -59,6 +60,7 @@ using std::stringstream;
 using std::list;
 using std::min;
 using std::pair;
+using std::make_pair;
 using boost::shared_ptr;
 using boost::optional;
 using boost::dynamic_pointer_cast;
@@ -140,7 +142,7 @@ FFmpegDecoder::pass ()
        }
 
        int const si = _packet.stream_index;
-       
+
        if (si == _video_stream) {
                decode_video_packet ();
        } else if (_ffmpeg_content->audio_stream() && _ffmpeg_content->audio_stream()->uses_index (_format_context, si)) {
@@ -283,9 +285,23 @@ FFmpegDecoder::bytes_per_audio_sample () const
 }
 
 void
-FFmpegDecoder::seek_and_flush (ContentTime t)
+FFmpegDecoder::seek (ContentTime time, bool accurate)
 {
-       ContentTime const u = t - _pts_offset;
+       VideoDecoder::seek (time, accurate);
+       AudioDecoder::seek (time, accurate);
+       
+       /* If we are doing an `accurate' seek, we need to use pre-roll, as
+          we don't really know what the seek will give us.
+       */
+
+       ContentTime pre_roll = accurate ? ContentTime::from_seconds (2) : ContentTime (0);
+       time -= pre_roll;
+
+       /* XXX: it seems debatable whether PTS should be used here...
+          http://www.mjbshaw.com/2012/04/seeking-in-ffmpeg-know-your-timestamp.html
+       */
+       
+       ContentTime const u = time - _pts_offset;
        int64_t s = u.seconds() / av_q2d (_format_context->streams[_video_stream]->time_base);
 
        if (_ffmpeg_content->audio_stream ()) {
@@ -294,12 +310,6 @@ FFmpegDecoder::seek_and_flush (ContentTime t)
                        );
        }
 
-       /* Ridiculous empirical hack */
-       s--;
-       if (s < 0) {
-               s = 0;
-       }
-
        av_seek_frame (_format_context, _video_stream, s, 0);
 
        avcodec_flush_buffers (video_codec_context());
@@ -311,26 +321,6 @@ FFmpegDecoder::seek_and_flush (ContentTime t)
        }
 }
 
-void
-FFmpegDecoder::seek (ContentTime time, bool accurate)
-{
-       VideoDecoder::seek (time, accurate);
-       AudioDecoder::seek (time, accurate);
-       
-       /* If we are doing an accurate seek, our initial shot will be 2s (2 being
-          a number plucked from the air) earlier than we want to end up.  The loop below
-          will hopefully then step through to where we want to be.
-       */
-
-       ContentTime pre_roll = accurate ? ContentTime::from_seconds (2) : ContentTime (0);
-       ContentTime initial_seek = time - pre_roll;
-       if (initial_seek < ContentTime (0)) {
-               initial_seek = ContentTime (0);
-       }
-
-       seek_and_flush (initial_seek);
-}
-
 void
 FFmpegDecoder::decode_audio_packet ()
 {
@@ -486,8 +476,8 @@ FFmpegDecoder::decode_subtitle_packet ()
        avsubtitle_free (&sub);
 }
 
-bool
-FFmpegDecoder::has_subtitle_during (ContentTimePeriod p) const
+list<ContentTimePeriod>
+FFmpegDecoder::subtitles_during (ContentTimePeriod p, bool starting) const
 {
-       return _ffmpeg_content->has_subtitle_during (p);
+       return _ffmpeg_content->subtitles_during (p, starting);
 }