From ea910e250a0fb3b0ad3ce0cf32dd27b24c17cd1d Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 11 Dec 2013 10:31:18 +0000 Subject: Various work on better seeking (and seeking of audio). --- src/lib/ffmpeg_decoder.cc | 151 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 107 insertions(+), 44 deletions(-) (limited to 'src/lib/ffmpeg_decoder.cc') diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc index 5a1b78762..ed0574c61 100644 --- a/src/lib/ffmpeg_decoder.cc +++ b/src/lib/ffmpeg_decoder.cc @@ -297,70 +297,132 @@ FFmpegDecoder::bytes_per_audio_sample () const return av_get_bytes_per_sample (audio_sample_format ()); } -void -FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate) +int +FFmpegDecoder::minimal_run (boost::function finished) { - double const time_base = av_q2d (_format_context->streams[_video_stream]->time_base); + int frames_read = 0; + + while (!finished (frames_read)) { + int r = av_read_frame (_format_context, &_packet); + if (r < 0) { + return -1; + } - /* If we are doing an accurate seek, our initial shot will be 5 frames (5 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. - */ - int initial = frame; + ++frames_read; - if (accurate) { - initial -= 5; - } + double const time_base = av_q2d (_format_context->streams[_packet.stream_index]->time_base); + + if (_packet.stream_index == _video_stream) { + + avcodec_get_frame_defaults (_frame); + + int finished = 0; + r = avcodec_decode_video2 (video_codec_context(), _frame, &finished, &_packet); + if (r >= 0 && finished) { + _video_position = rint ( + (av_frame_get_best_effort_timestamp (_frame) * time_base + _video_pts_offset) * _ffmpeg_content->video_frame_rate() + ); + } + + } else if (_ffmpeg_content->audio_stream() && _packet.stream_index == _ffmpeg_content->audio_stream()->index (_format_context)) { + + AVPacket copy_packet = _packet; - if (initial < 0) { - initial = 0; + while (copy_packet.size > 0) { + + int finished; + r = avcodec_decode_audio4 (audio_codec_context(), _frame, &finished, ©_packet); + if (r >= 0 && finished) { + _audio_position = rint ( + (av_frame_get_best_effort_timestamp (_frame) * time_base + _audio_pts_offset) * + _ffmpeg_content->audio_stream()->frame_rate + ); + } + + copy_packet.data += r; + copy_packet.size -= r; + } + } + + av_free_packet (&_packet); } - /* Initial seek time in the stream's timebase */ - int64_t const initial_vt = ((initial / _ffmpeg_content->video_frame_rate()) - _video_pts_offset) / time_base; + return frames_read; +} + +bool +FFmpegDecoder::seek_overrun_finished (Time seek) const +{ + return ( + _video_position >= _ffmpeg_content->time_to_content_video_frames (seek) || + _audio_position >= _ffmpeg_content->time_to_content_audio_frames (seek, _ffmpeg_content->position()) + ); +} + +bool +FFmpegDecoder::seek_final_finished (int n, int done) const +{ + return n == done; +} + +void +FFmpegDecoder::seek_and_flush (Time t) +{ + int64_t const initial_v = ((_ffmpeg_content->time_to_content_video_frames (t) / _ffmpeg_content->video_frame_rate()) - _video_pts_offset) / + av_q2d (_format_context->streams[_video_stream]->time_base); + + av_seek_frame (_format_context, _video_stream, initial_v, AVSEEK_FLAG_BACKWARD); - av_seek_frame (_format_context, _video_stream, initial_vt, AVSEEK_FLAG_BACKWARD); + shared_ptr as = _ffmpeg_content->audio_stream (); + if (as) { + int64_t initial_a = ((_ffmpeg_content->time_to_content_audio_frames (t, t) / as->frame_rate) - _audio_pts_offset) / + av_q2d (as->stream(_format_context)->time_base); + + av_seek_frame (_format_context, as->index (_format_context), initial_a, AVSEEK_FLAG_BACKWARD); + } avcodec_flush_buffers (video_codec_context()); + if (audio_codec_context ()) { + avcodec_flush_buffers (audio_codec_context ()); + } if (_subtitle_codec_context) { avcodec_flush_buffers (_subtitle_codec_context); } + _video_position = _ffmpeg_content->time_to_content_video_frames (t); + _audio_position = _ffmpeg_content->time_to_content_audio_frames (t, t); +} + +void +FFmpegDecoder::seek (Time time, bool accurate) +{ + /* If we are doing an accurate seek, our initial shot will be 200ms (200 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. + */ + + Time pre_roll = accurate ? (0.2 * TIME_HZ) : 0; + Time initial_seek = time - pre_roll; + if (initial_seek < 0) { + initial_seek = 0; + } + + /* Initial seek time in the video stream's timebase */ + + seek_and_flush (initial_seek); + _just_sought = true; - _video_position = frame; - if (frame == 0 || !accurate) { + if (time == 0 || !accurate) { /* We're already there, or we're as close as we need to be */ return; } - while (1) { - int r = av_read_frame (_format_context, &_packet); - if (r < 0) { - return; - } + int const N = minimal_run (boost::bind (&FFmpegDecoder::seek_overrun_finished, this, time)); - if (_packet.stream_index != _video_stream) { - av_free_packet (&_packet); - continue; - } - - avcodec_get_frame_defaults (_frame); - - int finished = 0; - r = avcodec_decode_video2 (video_codec_context(), _frame, &finished, &_packet); - if (r >= 0 && finished) { - _video_position = rint ( - (av_frame_get_best_effort_timestamp (_frame) * time_base + _video_pts_offset) * _ffmpeg_content->video_frame_rate() - ); - - if (_video_position >= (frame - 1)) { - av_free_packet (&_packet); - break; - } - } - - av_free_packet (&_packet); + seek_and_flush (initial_seek); + if (N > 0) { + minimal_run (boost::bind (&FFmpegDecoder::seek_final_finished, this, N - 1, _1)); } } @@ -377,6 +439,7 @@ FFmpegDecoder::decode_audio_packet () int frame_finished; int const decode_result = avcodec_decode_audio4 (audio_codec_context(), _frame, &frame_finished, ©_packet); + if (decode_result < 0) { shared_ptr film = _film.lock (); assert (film); -- cgit v1.2.3 From 097a1fb413bbbb89182161d4c1a31daa5419ec96 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 13 Dec 2013 10:06:18 +0000 Subject: Time -> DCPTime. --- src/lib/analyse_audio_job.cc | 2 +- src/lib/analyse_audio_job.h | 2 +- src/lib/audio_content.cc | 4 +- src/lib/audio_content.h | 4 +- src/lib/audio_merger.h | 2 +- src/lib/content.cc | 18 ++++----- src/lib/content.h | 30 +++++++-------- src/lib/decoder.h | 2 +- src/lib/ffmpeg_content.cc | 2 +- src/lib/ffmpeg_content.h | 2 +- src/lib/ffmpeg_decoder.cc | 14 +++---- src/lib/ffmpeg_decoder.h | 4 +- src/lib/film.cc | 12 +++--- src/lib/film.h | 12 +++--- src/lib/image_content.cc | 2 +- src/lib/image_content.h | 2 +- src/lib/image_decoder.cc | 2 +- src/lib/image_decoder.h | 2 +- src/lib/job.cc | 2 +- src/lib/player.cc | 34 ++++++++--------- src/lib/player.h | 30 +++++++-------- src/lib/playlist.cc | 20 +++++----- src/lib/playlist.h | 6 +-- src/lib/sndfile_content.cc | 2 +- src/lib/sndfile_content.h | 2 +- src/lib/sndfile_decoder.cc | 2 +- src/lib/sndfile_decoder.h | 2 +- src/lib/subtitle_decoder.cc | 2 +- src/lib/subtitle_decoder.h | 6 +-- src/lib/timer.cc | 14 +++---- src/lib/timer.h | 18 ++++----- src/lib/types.h | 5 ++- src/lib/video_content.cc | 4 +- src/lib/video_content.h | 4 +- src/tools/server_test.cc | 2 +- src/wx/audio_plot.cc | 2 +- src/wx/film_editor.cc | 4 +- src/wx/film_editor.h | 6 +-- src/wx/film_viewer.cc | 8 ++-- src/wx/film_viewer.h | 4 +- src/wx/timecode.cc | 26 ++++++------- src/wx/timecode.h | 8 ++-- src/wx/timeline.cc | 86 +++++++++++++++++++++---------------------- src/wx/timeline.h | 10 ++--- src/wx/timeline_dialog.cc | 8 ++-- src/wx/timeline_dialog.h | 6 +-- src/wx/timing_panel.cc | 10 ++--- src/wx/timing_panel.h | 12 +++--- test/ffmpeg_seek_test.cc | 12 +++--- test/long_ffmpeg_seek_test.cc | 12 +++--- test/play_test.cc | 6 +-- 51 files changed, 247 insertions(+), 246 deletions(-) (limited to 'src/lib/ffmpeg_decoder.cc') diff --git a/src/lib/analyse_audio_job.cc b/src/lib/analyse_audio_job.cc index 8186f9de4..3f84bf16d 100644 --- a/src/lib/analyse_audio_job.cc +++ b/src/lib/analyse_audio_job.cc @@ -81,7 +81,7 @@ AnalyseAudioJob::run () } void -AnalyseAudioJob::audio (shared_ptr b, Time) +AnalyseAudioJob::audio (shared_ptr b, DCPTime) { for (int i = 0; i < b->frames(); ++i) { for (int j = 0; j < b->channels(); ++j) { diff --git a/src/lib/analyse_audio_job.h b/src/lib/analyse_audio_job.h index 3d4881983..2e93ef500 100644 --- a/src/lib/analyse_audio_job.h +++ b/src/lib/analyse_audio_job.h @@ -33,7 +33,7 @@ public: void run (); private: - void audio (boost::shared_ptr, Time); + void audio (boost::shared_ptr, DCPTime); boost::weak_ptr _content; OutputAudioFrame _done; diff --git a/src/lib/audio_content.cc b/src/lib/audio_content.cc index 0c4586681..de743571b 100644 --- a/src/lib/audio_content.cc +++ b/src/lib/audio_content.cc @@ -39,7 +39,7 @@ int const AudioContentProperty::AUDIO_GAIN = 203; int const AudioContentProperty::AUDIO_DELAY = 204; int const AudioContentProperty::AUDIO_MAPPING = 205; -AudioContent::AudioContent (shared_ptr f, Time s) +AudioContent::AudioContent (shared_ptr f, DCPTime s) : Content (f, s) , _audio_gain (0) , _audio_delay (0) @@ -157,7 +157,7 @@ AudioContent::technical_summary () const * the `controlling' video content is active. */ AudioContent::Frame -AudioContent::time_to_content_audio_frames (Time t, Time at) const +AudioContent::time_to_content_audio_frames (DCPTime t, DCPTime at) const { shared_ptr film = _film.lock (); assert (film); diff --git a/src/lib/audio_content.h b/src/lib/audio_content.h index 10114e10d..e1b38bd97 100644 --- a/src/lib/audio_content.h +++ b/src/lib/audio_content.h @@ -43,7 +43,7 @@ class AudioContent : public virtual Content public: typedef int64_t Frame; - AudioContent (boost::shared_ptr, Time); + AudioContent (boost::shared_ptr, DCPTime); AudioContent (boost::shared_ptr, boost::filesystem::path); AudioContent (boost::shared_ptr, boost::shared_ptr); AudioContent (boost::shared_ptr, std::vector >); @@ -74,7 +74,7 @@ public: return _audio_delay; } - Frame time_to_content_audio_frames (Time, Time) const; + Frame time_to_content_audio_frames (DCPTime, DCPTime) const; private: /** Gain to apply to audio in dB */ diff --git a/src/lib/audio_merger.h b/src/lib/audio_merger.h index 6ad33fb37..2a1cc761b 100644 --- a/src/lib/audio_merger.h +++ b/src/lib/audio_merger.h @@ -102,7 +102,7 @@ public: } void - clear (Time t) + clear (DCPTime t) { _last_pull = t; _buffers.reset (new AudioBuffers (_buffers->channels(), 0)); diff --git a/src/lib/content.cc b/src/lib/content.cc index f09012765..7db349617 100644 --- a/src/lib/content.cc +++ b/src/lib/content.cc @@ -54,7 +54,7 @@ Content::Content (shared_ptr f) } -Content::Content (shared_ptr f, Time p) +Content::Content (shared_ptr f, DCPTime p) : _film (f) , _position (p) , _trim_start (0) @@ -83,9 +83,9 @@ Content::Content (shared_ptr f, shared_ptr node) _paths.push_back ((*i)->content ()); } _digest = node->string_child ("Digest"); - _position = node->number_child