Hopefully fix seek back/forward.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index 3b7d727b88eec115b508da254d806da5f90b9f27..bcd1e738a46bbff94976afbc2fe178b8f3ff2cd3 100644 (file)
@@ -281,26 +281,11 @@ FFmpegDecoder::bytes_per_audio_sample () const
 }
 
 void
-FFmpegDecoder::seek (VideoContent::Frame frame)
+FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
 {
-       do_seek (frame, false, false);
-}
-
-void
-FFmpegDecoder::seek_back ()
-{
-       if (_video_position == 0) {
-               return;
-       }
-       
-       do_seek (_video_position - 1, true, true);
-}
-
-void
-FFmpegDecoder::do_seek (VideoContent::Frame frame, bool backwards, bool accurate)
-{
-       int64_t const vt = frame / (_ffmpeg_content->video_frame_rate() * av_q2d (_format_context->streams[_video_stream]->time_base));
-       av_seek_frame (_format_context, _video_stream, vt, backwards ? AVSEEK_FLAG_BACKWARD : 0);
+       double const time_base = av_q2d (_format_context->streams[_video_stream]->time_base);
+       int64_t const vt = frame / (_ffmpeg_content->video_frame_rate() * time_base);
+       av_seek_frame (_format_context, _video_stream, vt, AVSEEK_FLAG_BACKWARD);
 
        avcodec_flush_buffers (video_codec_context());
        if (_subtitle_codec_context) {
@@ -323,9 +308,11 @@ FFmpegDecoder::do_seek (VideoContent::Frame frame, bool backwards, bool accurate
                                int const r = avcodec_decode_video2 (video_codec_context(), _frame, &finished, &_packet);
                                if (r >= 0 && finished) {
                                        int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
-                                       if (bet > vt) {
-                                               _video_position = (bet * av_q2d (_format_context->streams[_video_stream]->time_base) + _pts_offset)
-                                                       * _ffmpeg_content->video_frame_rate();
+                                       if (bet >= vt) {
+                                               _video_position = rint (
+                                                       (bet * time_base + _pts_offset) * _ffmpeg_content->video_frame_rate()
+                                                       );
+                                               av_free_packet (&_packet);
                                                break;
                                        }
                                }
@@ -427,10 +414,10 @@ FFmpegDecoder::decode_video_packet ()
                                /* We just did a seek, so disable any attempts to correct for where we
                                   are / should be.
                                */
-                               _video_position = pts * _ffmpeg_content->video_frame_rate ();
+                               _video_position = rint (pts * _ffmpeg_content->video_frame_rate ());
                                _just_sought = false;
                        }
-                       
+
                        double const next = _video_position / _ffmpeg_content->video_frame_rate();
                        double const one_frame = 1 / _ffmpeg_content->video_frame_rate ();
                        double delta = pts - next;