diff options
Diffstat (limited to 'src/lib/ffmpeg_decoder.cc')
| -rw-r--r-- | src/lib/ffmpeg_decoder.cc | 144 |
1 files changed, 91 insertions, 53 deletions
diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc index f1d984ee1..2d1792390 100644 --- a/src/lib/ffmpeg_decoder.cc +++ b/src/lib/ffmpeg_decoder.cc @@ -59,15 +59,37 @@ using libdcp::Size; FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<const FFmpegContent> c, bool video, bool audio) : Decoder (f) - , VideoDecoder (f, c) - , AudioDecoder (f, c) + , VideoDecoder (f) + , AudioDecoder (f) , FFmpeg (c) , _subtitle_codec_context (0) , _subtitle_codec (0) , _decode_video (video) , _decode_audio (audio) + , _pts_offset (0) { setup_subtitle (); + + if (video && audio && c->audio_stream() && c->first_video() && c->audio_stream()->first_audio) { + _pts_offset = compute_pts_offset (c->first_video().get(), c->audio_stream()->first_audio.get(), c->video_frame_rate()); + } +} + +double +FFmpegDecoder::compute_pts_offset (double first_video, double first_audio, float video_frame_rate) +{ + assert (first_video >= 0); + assert (first_audio >= 0); + + double const old_first_video = first_video; + + /* Round the first video to a frame boundary */ + if (fabs (rint (first_video * video_frame_rate) - first_video * video_frame_rate) > 1e-6) { + first_video = ceil (first_video * video_frame_rate) / video_frame_rate; + } + + /* Compute the required offset (also removing any common start delay) */ + return first_video - old_first_video - min (first_video, first_audio); } FFmpegDecoder::~FFmpegDecoder () @@ -108,7 +130,8 @@ FFmpegDecoder::pass () } /* Stop us being asked for any more data */ - _next_video = _next_audio = _ffmpeg_content->length (); + _video_position = _ffmpeg_content->video_length (); + _audio_position = _ffmpeg_content->audio_length (); return; } @@ -119,6 +142,7 @@ FFmpegDecoder::pass () } else if (_ffmpeg_content->audio_stream() && _packet.stream_index == _ffmpeg_content->audio_stream()->id && _decode_audio) { decode_audio_packet (); } else if (_ffmpeg_content->subtitle_stream() && _packet.stream_index == _ffmpeg_content->subtitle_stream()->id) { +#if 0 int got_subtitle; AVSubtitle sub; @@ -138,6 +162,7 @@ FFmpegDecoder::pass () } avsubtitle_free (&sub); } +#endif } av_free_packet (&_packet); @@ -256,38 +281,25 @@ FFmpegDecoder::bytes_per_audio_sample () const } void -FFmpegDecoder::seek (Time t) +FFmpegDecoder::seek (VideoContent::Frame frame) { - do_seek (t, false, false); - VideoDecoder::seek (t); + do_seek (frame, false, false); } void FFmpegDecoder::seek_back () { - if (position() < (2.5 * TIME_HZ / _ffmpeg_content->video_frame_rate())) { - return; - } - - do_seek (position() - 2.5 * TIME_HZ / _ffmpeg_content->video_frame_rate(), true, true); - VideoDecoder::seek_back (); -} - -void -FFmpegDecoder::seek_forward () -{ - if (position() >= (_ffmpeg_content->length() - 0.5 * TIME_HZ / _ffmpeg_content->video_frame_rate())) { + if (_video_position == 0) { return; } - do_seek (position() - 0.5 * TIME_HZ / _ffmpeg_content->video_frame_rate(), true, true); - VideoDecoder::seek_forward (); + do_seek (_video_position - 1, true, true); } void -FFmpegDecoder::do_seek (Time t, bool backwards, bool accurate) +FFmpegDecoder::do_seek (VideoContent::Frame frame, bool backwards, bool accurate) { - int64_t const vt = t / (av_q2d (_format_context->streams[_video_stream]->time_base) * TIME_HZ); + 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); avcodec_flush_buffers (video_codec_context()); @@ -337,17 +349,33 @@ FFmpegDecoder::decode_audio_packet () int const decode_result = avcodec_decode_audio4 (audio_codec_context(), _frame, &frame_finished, ©_packet); if (decode_result >= 0) { if (frame_finished) { - - /* Where we are in the source, in seconds */ - double const source_pts_seconds = av_q2d (_format_context->streams[copy_packet.stream_index]->time_base) - * av_frame_get_best_effort_timestamp(_frame); + + if (_audio_position == 0) { + /* Where we are in the source, in seconds */ + double const pts = av_q2d (_format_context->streams[copy_packet.stream_index]->time_base) + * av_frame_get_best_effort_timestamp(_frame) - _pts_offset; + + if (pts > 0) { + /* Emit some silence */ + shared_ptr<AudioBuffers> silence ( + new AudioBuffers ( + _ffmpeg_content->audio_channels(), + pts * _ffmpeg_content->content_audio_frame_rate() + ) + ); + + silence->make_silent (); + audio (silence, _audio_position); + } + } + int const data_size = av_samples_get_buffer_size ( 0, audio_codec_context()->channels, _frame->nb_samples, audio_sample_format (), 1 ); assert (audio_codec_context()->channels == _ffmpeg_content->audio_channels()); - audio (deinterleave_audio (_frame->data, data_size), source_pts_seconds * TIME_HZ); + audio (deinterleave_audio (_frame->data, data_size), _audio_position); } copy_packet.data += decode_result; @@ -398,11 +426,33 @@ FFmpegDecoder::decode_video_packet () int64_t const bet = av_frame_get_best_effort_timestamp (_frame); if (bet != AV_NOPTS_VALUE) { - /* XXX: may need to insert extra frames / remove frames here ... - (as per old Matcher) - */ - Time const t = bet * av_q2d (_format_context->streams[_video_stream]->time_base) * TIME_HZ; - video (image, false, t); + + double const pts = bet * av_q2d (_format_context->streams[_video_stream]->time_base) - _pts_offset; + double const next = _video_position / _ffmpeg_content->video_frame_rate(); + double const one_frame = 1 / _ffmpeg_content->video_frame_rate (); + double delta = pts - next; + + while (delta > one_frame) { + /* This PTS is more than one frame forward in time of where we think we should be; emit + a black frame. + */ + boost::shared_ptr<Image> black ( + new SimpleImage ( + static_cast<AVPixelFormat> (_frame->format), + libdcp::Size (video_codec_context()->width, video_codec_context()->height), + true + ) + ); + + black->make_black (); + video (image, false, _video_position); + delta -= one_frame; + } + + if (delta > -one_frame) { + /* This PTS is within a frame of being right; emit this (otherwise it will be dropped) */ + video (image, false, _video_position); + } } else { shared_ptr<const Film> film = _film.lock (); assert (film); @@ -413,27 +463,6 @@ FFmpegDecoder::decode_video_packet () return true; } -Time -FFmpegDecoder::position () const -{ - if (_decode_video && _decode_audio && _ffmpeg_content->audio_stream()) { - return min (_next_video, _next_audio); - } - - if (_decode_audio && _ffmpeg_content->audio_stream()) { - return _next_audio; - } - - return _next_video; -} - -bool -FFmpegDecoder::done () const -{ - bool const ad = !_decode_audio || !_ffmpeg_content->audio_stream() || audio_done(); - bool const vd = !_decode_video || video_done(); - return ad && vd; -} void FFmpegDecoder::setup_subtitle () @@ -455,3 +484,12 @@ FFmpegDecoder::setup_subtitle () throw DecodeError (N_("could not open subtitle decoder")); } } + +bool +FFmpegDecoder::done () const +{ + bool const vd = !_decode_video || (_video_position >= _ffmpeg_content->video_length()); + bool const ad = !_decode_audio || !_ffmpeg_content->audio_stream() || (_audio_position >= _ffmpeg_content->audio_length()); + return vd && ad; +} + |
