X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fffmpeg_decoder.cc;h=7c846349f466d225cd190601446a917bd0e02eab;hb=54c0378137091de604b8c0d6d98959b55b0cddb6;hp=15443c346b3d302cce263165588044e641879160;hpb=f4030653da8f5a0b3daebe640f485b3daf9ef091;p=dcpomatic.git diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc index 15443c346..7c846349f 100644 --- a/src/lib/ffmpeg_decoder.cc +++ b/src/lib/ffmpeg_decoder.cc @@ -50,7 +50,8 @@ extern "C" { #define LOG_GENERAL(...) _video_content->film()->log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL); #define LOG_ERROR(...) _video_content->film()->log()->log (String::compose (__VA_ARGS__), Log::TYPE_ERROR); -#define LOG_WARNING(...) _video_content->film()->log()->log (__VA_ARGS__, Log::TYPE_WARNING); +#define LOG_WARNING_NC(...) _video_content->film()->log()->log (__VA_ARGS__, Log::TYPE_WARNING); +#define LOG_WARNING(...) _video_content->film()->log()->log (String::compose (__VA_ARGS__), Log::TYPE_WARNING); using std::cout; using std::string; @@ -96,6 +97,14 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr c, shared_ptr _pts_offset = - c->audio_stream()->first_audio.get(); } + /* If _pts_offset is positive we would be pushing things from a -ve PTS to be played. + I don't think we ever want to do that, as it seems things at -ve PTS are not meant + to be seen (use for alignment bars etc.); see mantis #418. + */ + if (_pts_offset > ContentTime ()) { + _pts_offset = ContentTime (); + } + /* Now adjust both so that the video pts starts on a frame */ if (have_video && have_audio) { ContentTime first_video = c->first_video().get() + _pts_offset; @@ -127,21 +136,25 @@ FFmpegDecoder::pass () { int r = av_read_frame (_format_context, &_packet); - if (r < 0) { + /* AVERROR_INVALIDDATA can apparently be returned sometimes even when av_read_frame + has pretty-much succeeded (and hence generated data which should be processed). + Hence it makes sense to continue here in that case. + */ + if (r < 0 && r != AVERROR_INVALIDDATA) { if (r != AVERROR_EOF) { /* Maybe we should fail here, but for now we'll just finish off instead */ char buf[256]; av_strerror (r, buf, sizeof(buf)); LOG_ERROR (N_("error on av_read_frame (%1) (%2)"), buf, r); } - + flush (); return true; } int const si = _packet.stream_index; - if (si == _video_stream) { + if (si == _video_stream && !_ignore_video) { decode_video_packet (); } else if (_ffmpeg_content->audio_stream() && _ffmpeg_content->audio_stream()->uses_index (_format_context, si)) { decode_audio_packet (); @@ -159,13 +172,14 @@ FFmpegDecoder::pass () shared_ptr FFmpegDecoder::deinterleave_audio (uint8_t** data, int size) { - assert (_ffmpeg_content->audio_channels()); - assert (bytes_per_audio_sample()); + DCPOMATIC_ASSERT (_ffmpeg_content->audio_channels()); + DCPOMATIC_ASSERT (bytes_per_audio_sample()); /* Deinterleave and convert to float */ - assert ((size % (bytes_per_audio_sample() * _ffmpeg_content->audio_channels())) == 0); - + /* total_samples and frames will be rounded down here, so if there are stray samples at the end + of the block that do not form a complete sample or frame they will be dropped. + */ int const total_samples = size / bytes_per_audio_sample(); int const frames = total_samples / _ffmpeg_content->audio_channels(); shared_ptr audio (new AudioBuffers (_ffmpeg_content->audio_channels(), frames)); @@ -331,11 +345,21 @@ FFmpegDecoder::decode_audio_packet () while (copy_packet.size > 0) { int frame_finished; - int const decode_result = avcodec_decode_audio4 (audio_codec_context(), _frame, &frame_finished, ©_packet); - + int decode_result = avcodec_decode_audio4 (audio_codec_context(), _frame, &frame_finished, ©_packet); if (decode_result < 0) { - LOG_ERROR ("avcodec_decode_audio4 failed (%1)", decode_result); - return; + /* avcodec_decode_audio4 can sometimes return an error even though it has decoded + some valid data; for example dca_subframe_footer can return AVERROR_INVALIDDATA + if it overreads the auxiliary data. ffplay carries on if frame_finished is true, + even in the face of such an error, so I think we should too. + + Returning from the method here caused mantis #352. + */ + LOG_WARNING ("avcodec_decode_audio4 failed (%1)", decode_result); + + /* Fudge decode_result so that we come out of the while loop when + we've processed this data. + */ + decode_result = copy_packet.size; } if (frame_finished) { @@ -390,11 +414,11 @@ FFmpegDecoder::decode_video_packet () if (i->second != AV_NOPTS_VALUE) { double const pts = i->second * av_q2d (_format_context->streams[_video_stream]->time_base) + _pts_offset.seconds (); video ( - shared_ptr (new RawImageProxy (image, _video_content->film()->log())), + shared_ptr (new RawImageProxy (image)), rint (pts * _ffmpeg_content->video_frame_rate ()) ); } else { - LOG_WARNING ("Dropping frame without PTS"); + LOG_WARNING_NC ("Dropping frame without PTS"); } }