From 568d414074ddb02976d1bf5a491dba3571eafeba Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 27 May 2023 12:32:38 +0200 Subject: [PATCH] fftools/ffmpeg: attach filter framerate to frames This way the encoder does not need to reach backward into the filter. --- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_enc.c | 19 +++++++++++-------- fftools/ffmpeg_filter.c | 10 ++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 6f71e85658..49c07ede95 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -655,6 +655,8 @@ typedef struct FrameData { uint64_t idx; int64_t pts; AVRational tb; + + AVRational frame_rate_filter; } FrameData; extern InputFile **input_files; diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index f6431b29d1..cd2faccf4e 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -217,8 +217,10 @@ int enc_open(OutputStream *ost, AVFrame *frame) case AVMEDIA_TYPE_VIDEO: { AVRational fr = ost->frame_rate; - if (!fr.num) - fr = av_buffersink_get_frame_rate(ost->filter->filter); + if (!fr.num && frame) { + FrameData *fd = frame_data(frame); + fr = fd->frame_rate_filter; + } if (!fr.num && !ost->max_frame_rate.num) { fr = (AVRational){25, 1}; av_log(ost, AV_LOG_WARNING, @@ -1024,17 +1026,18 @@ static void do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame) int ret; Encoder *e = ost->enc; AVCodecContext *enc = ost->enc_ctx; - AVRational frame_rate; int64_t nb_frames, nb_frames_prev, i; double duration = 0; - AVFilterContext *filter = ost->filter->filter; - if (frame) + if (frame) { + FrameData *fd = frame_data(frame); + duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base)); - frame_rate = av_buffersink_get_frame_rate(filter); - if (duration <= 0 && frame_rate.num > 0 && frame_rate.den > 0) - duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base)); + if (duration <= 0 && + fd->frame_rate_filter.num > 0 && fd->frame_rate_filter.den > 0) + duration = 1 / (av_q2d(fd->frame_rate_filter) * av_q2d(enc->time_base)); + } video_sync_process(of, ost, frame, duration, &nb_frames, &nb_frames_prev); diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 1150f6fc65..a2b45a14b8 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -1711,6 +1711,16 @@ int reap_filters(int flush) tb.num, tb.den); } + if (ost->type == AVMEDIA_TYPE_VIDEO) { + FrameData *fd = frame_data(filtered_frame); + if (!fd) { + av_frame_unref(filtered_frame); + report_and_exit(AVERROR(ENOMEM)); + } + + fd->frame_rate_filter = av_buffersink_get_frame_rate(filter); + } + enc_frame(ost, filtered_frame); av_frame_unref(filtered_frame); }