[PATCH 1/3] fftools/ffmpeg_dec: improve detection of lavf-guessed durations
Will be useful in following commit. --- fftools/ffmpeg_dec.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c index 70de151301..c2bcf784b0 100644 --- a/fftools/ffmpeg_dec.c +++ b/fftools/ffmpeg_dec.c @@ -260,6 +260,10 @@ static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *fra const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE; const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED; int64_t codec_duration = 0; + // difference between this and last frame's timestamps + const int64_t ts_diff = + (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE) ? + frame->pts - dp->last_frame_pts : -1; // XXX lavf currently makes up frame durations when they are not provided by // the container. As there is no way to reliably distinguish real container @@ -267,8 +271,13 @@ static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *fra // the container has timestamps. Eventually lavf should stop making up // durations, then this should be simplified. + // frame duration is unreliable (typically guessed by lavf) when it is equal + // to 1 and the actual duration of the last frame is more than 2x larger + const int duration_unreliable = frame->duration == 1 && ts_diff > 2 * frame->duration; + // prefer frame duration for containers with timestamps - if (frame->duration > 0 && (!ts_unreliable || fr_forced)) + if (fr_forced || + (frame->duration > 0 && !ts_unreliable && !duration_unreliable)) return frame->duration; if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) { @@ -285,9 +294,8 @@ static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *fra // when timestamps are available, repeat last frame's actual duration // (i.e. pts difference between this and last frame) - if (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE && - frame->pts > dp->last_frame_pts) - return frame->pts - dp->last_frame_pts; + if (ts_diff > 0) + return ts_diff; // try frame/codec duration if (frame->duration > 0) -- 2.43.0
Stop trying to invent some "framerate-based" timebase when there is no reason to think the stream is CFR at all. --- fftools/ffmpeg_mux_init.c | 7 +------ tests/fate/ffmpeg.mak | 2 +- tests/ref/fate/copy-trac4914-avi | 4 ++-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 899ee6046f..4a2b5924a4 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -960,17 +960,12 @@ static int streamcopy_init(const Muxer *mux, OutputStream *ost, AVDictionary **e else ost->st->avg_frame_rate = ist->st->avg_frame_rate; - ret = avformat_transfer_internal_stream_timing_info(mux->fc->oformat, - ost->st, ist->st, copy_tb); - if (ret < 0) - goto fail; - // copy timebase while removing common factors if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) { if (fr.num) ost->st->time_base = av_inv_q(fr); else - ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1}); + ost->st->time_base = av_add_q(ist->st->time_base, (AVRational){0, 1}); } if (!ms->copy_prior_start) { diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index dc102750a0..9e0c68da20 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -146,7 +146,7 @@ fate-copy-trac236: CMD = transcode mov $(TARGET_SAMPLES)/mov/fcp_export8-236.mov FATE_STREAMCOPY-$(call TRANSCODE, RAWVIDEO MPEG2VIDEO, MXF, MPEGTS_DEMUXER MPEGVIDEO_PARSER MPEGAUDIO_PARSER MP2_DECODER ARESAMPLE_FILTER PCM_S16LE_DECODER) += fate-copy-trac4914 fate-copy-trac4914: CMD = transcode mpegts $(TARGET_SAMPLES)/mpeg2/xdcam8mp2-1s_small.ts\ - mxf "-c:a pcm_s16le -af aresample -c:v copy" + mxf "-c:a pcm_s16le -af aresample -c:v copy -time_base 1001/30000" FATE_STREAMCOPY-$(call TRANSCODE, RAWVIDEO MPEG2VIDEO, AVI, MPEGTS_DEMUXER MPEGVIDEO_PARSER MPEGAUDIO_PARSER EXTRACT_EXTRADATA_BSF MP2_DECODER ARESAMPLE_FILTER) += fate-copy-trac4914-avi fate-copy-trac4914-avi: CMD = transcode mpegts $(TARGET_SAMPLES)/mpeg2/xdcam8mp2-1s_small.ts\ diff --git a/tests/ref/fate/copy-trac4914-avi b/tests/ref/fate/copy-trac4914-avi index 20006ea05c..d598e0aa65 100644 --- a/tests/ref/fate/copy-trac4914-avi +++ b/tests/ref/fate/copy-trac4914-avi @@ -1,5 +1,5 @@ -26e4202638bc384b82d2b5eb4d33a5f0 *tests/data/fate/copy-trac4914-avi.avi -479494 tests/data/fate/copy-trac4914-avi.avi +3b6f31b806ef421652a066f239536b0d *tests/data/fate/copy-trac4914-avi.avi +492046 tests/data/fate/copy-trac4914-avi.avi #tb 0: 1001/30000 #media_type 0: video #codec_id 0: rawvideo -- 2.43.0
And av_stream_get_codec_timebase(). They were both added for ffmpeg CLI, which no longer calls either of them. Furthermore the notion of "internal stream timing info" that needs to be transferred with a special magic API function is fundamentally flawed and should be removed. --- doc/APIchanges | 4 ++++ libavformat/avformat.c | 2 ++ libavformat/avformat.h | 18 +++++++----------- libavformat/internal.h | 2 ++ libavformat/options.c | 2 ++ libavformat/version_major.h | 1 + 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index ac7953a49c..0dcb17eb35 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,10 @@ The last version increases of all libraries were on 2024-03-07 API changes, most recent first: +2024-07-xx - xxxxxxxxxx - lavf 61 - avformat.h + Deprecate avformat_transfer_internal_stream_timing_info() + and av_stream_get_codec_timebase() without replacement. + 2024-07-02 - xxxxxxxxxx - lavu 59.28.100 - hwcontext_d3d12va.h Add AVD3D12VAFramesContext.flags diff --git a/libavformat/avformat.c b/libavformat/avformat.c index 140fb5b6aa..b4f20502fb 100644 --- a/libavformat/avformat.c +++ b/libavformat/avformat.c @@ -770,6 +770,7 @@ AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *f return fr; } +#if FF_API_INTERNAL_TIMING int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt, AVStream *ost, const AVStream *ist, enum AVTimebaseSource copy_tb) @@ -849,6 +850,7 @@ AVRational av_stream_get_codec_timebase(const AVStream *st) { return cffstream(st)->avctx ? cffstream(st)->avctx->time_base : cffstream(st)->transferred_mux_tb; } +#endif void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 8afdcd9fd0..90920072aa 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -3042,6 +3042,7 @@ int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, int avformat_queue_attached_pictures(AVFormatContext *s); +#if FF_API_INTERNAL_TIMING enum AVTimebaseSource { AVFMT_TBCF_AUTO = -1, AVFMT_TBCF_DECODER, @@ -3052,25 +3053,20 @@ enum AVTimebaseSource { }; /** - * Transfer internal timing information from one stream to another. - * - * This function is useful when doing stream copy. - * - * @param ofmt target output format for ost - * @param ost output stream which needs timings copy and adjustments - * @param ist reference input stream to copy timings from - * @param copy_tb define from where the stream codec timebase needs to be imported + * @deprecated do not call this function */ +attribute_deprecated int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt, AVStream *ost, const AVStream *ist, enum AVTimebaseSource copy_tb); /** - * Get the internal codec timebase from a stream. - * - * @param st input stream to extract the timebase from + * @deprecated do not call this function */ +attribute_deprecated AVRational av_stream_get_codec_timebase(const AVStream *st); +#endif + /** * @} diff --git a/libavformat/internal.h b/libavformat/internal.h index 6bad4fd119..8e8971bfeb 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -411,7 +411,9 @@ typedef struct FFStream { const struct AVCodecDescriptor *codec_desc; +#if FF_API_INTERNAL_TIMING AVRational transferred_mux_tb; +#endif } FFStream; static av_always_inline FFStream *ffstream(AVStream *st) diff --git a/libavformat/options.c b/libavformat/options.c index 60a73d79b9..2f3f8e0c96 100644 --- a/libavformat/options.c +++ b/libavformat/options.c @@ -317,7 +317,9 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c) sti->pts_buffer[i] = AV_NOPTS_VALUE; st->sample_aspect_ratio = (AVRational) { 0, 1 }; +#if FF_API_INTERNAL_TIMING sti->transferred_mux_tb = (AVRational) { 0, 1 };; +#endif #if FF_API_AVSTREAM_SIDE_DATA sti->inject_global_side_data = si->inject_global_side_data; diff --git a/libavformat/version_major.h b/libavformat/version_major.h index 44ad23c6b6..7a9b06703d 100644 --- a/libavformat/version_major.h +++ b/libavformat/version_major.h @@ -47,6 +47,7 @@ #define FF_API_AVSTREAM_SIDE_DATA (LIBAVFORMAT_VERSION_MAJOR < 62) #define FF_API_GET_DUR_ESTIMATE_METHOD (LIBAVFORMAT_VERSION_MAJOR < 62) +#define FF_API_INTERNAL_TIMING (LIBAVFORMAT_VERSION_MAJOR < 62) #define FF_API_R_FRAME_RATE 1 -- 2.43.0
On 7/5/2024 7:42 AM, Anton Khirnov wrote:
And av_stream_get_codec_timebase().
They were both added for ffmpeg CLI, which no longer calls either of them. Furthermore the notion of "internal stream timing info" that needs to be transferred with a special magic API function is fundamentally flawed and should be removed.
LGTM
participants (2)
-
Anton Khirnov -
James Almer