From 238f439992f7e77609c2dc7ab6230da756c460b8 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 10 Sep 2024 10:55:28 +0200 Subject: [PATCH] fftools/ffmpeg_enc: do not set AVStream timebase directly Instead, pass the encoder context to of_stream_init() and have the muxer take the timebase from there. Note that the muxer can currently access the codec context directly, but that will change in future commits. This is a step towards decoupling encoders from muxers. --- fftools/ffmpeg.h | 3 ++- fftools/ffmpeg_enc.c | 6 +----- fftools/ffmpeg_mux.c | 9 ++++++++- fftools/ffmpeg_mux_init.c | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 122372440f..98080213fd 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -871,7 +871,8 @@ int enc_loopback(Encoder *enc); * * Open the muxer once all the streams have been initialized. */ -int of_stream_init(OutputFile *of, OutputStream *ost); +int of_stream_init(OutputFile *of, OutputStream *ost, + const AVCodecContext *enc_ctx); int of_write_trailer(OutputFile *of); int of_open(const OptionsContext *o, const char *filename, Scheduler *sch); void of_free(OutputFile **pof); diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index c11ec218d7..ba79f6a3fc 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -367,11 +367,7 @@ int enc_open(void *opaque, const AVFrame *frame) return ret; } - // copy timebase while removing common factors - if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) - ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1}); - - ret = of_stream_init(of, ost); + ret = of_stream_init(of, ost, enc_ctx); if (ret < 0) return ret; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 71ff9b45ab..71a771052f 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -608,12 +608,19 @@ static int bsf_init(MuxStream *ms) return 0; } -int of_stream_init(OutputFile *of, OutputStream *ost) +int of_stream_init(OutputFile *of, OutputStream *ost, + const AVCodecContext *enc_ctx) { Muxer *mux = mux_from_of(of); MuxStream *ms = ms_from_ost(ost); int ret; + if (enc_ctx) { + // use upstream time base unless it has been overridden previously + if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) + ost->st->time_base = av_add_q(enc_ctx->time_base, (AVRational){0, 1}); + } + /* initialize bitstream filters for the output stream * needs to be done here, because the codec id for streamcopy is not * known until now */ diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 1b75430e4e..b2351de177 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -3385,7 +3385,7 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) OutputStream *ost = of->streams[i]; if (!ost->enc) { - err = of_stream_init(of, ost); + err = of_stream_init(of, ost, NULL); if (err < 0) return err; }