From 509afedaafa1c6796dac6c5654a1229d6af945bb Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 1 Apr 2024 06:29:16 +0200 Subject: [PATCH] fftools/ffmpeg_filter: pass vsync method through OutputFilterOptions Do not read it from OutputStream directly. Will allow decoupling filtering from encoding in future commits. --- fftools/ffmpeg.h | 3 ++- fftools/ffmpeg_filter.c | 9 +++++--- fftools/ffmpeg_mux.c | 2 +- fftools/ffmpeg_mux.h | 3 +++ fftools/ffmpeg_mux_init.c | 45 ++++++++++++++++++++++----------------- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 598ca2fa96..fa8f7d8324 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -287,6 +287,8 @@ typedef struct OutputFilterOptions { int width; int height; + enum VideoSyncMethod vsync_method; + int sample_rate; AVChannelLayout ch_layout; } OutputFilterOptions; @@ -549,7 +551,6 @@ typedef struct OutputStream { /* video only */ AVRational frame_rate; AVRational max_frame_rate; - enum VideoSyncMethod vsync_method; int is_cfr; int force_fps; #if FFMPEG_OPT_TOP diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 3c25d2ed65..d906b72576 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -175,6 +175,8 @@ typedef struct FPSConvContext { int last_dropped; int dropped_keyframe; + enum VideoSyncMethod vsync_method; + AVRational framerate; AVRational framerate_max; const AVRational *framerate_supported; @@ -799,6 +801,7 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost, if (!ofp->fps.last_frame) return AVERROR(ENOMEM); + ofp->fps.vsync_method = opts->vsync_method; ofp->fps.framerate = ost->frame_rate; ofp->fps.framerate_max = ost->max_frame_rate; ofp->fps.framerate_supported = ost->force_fps && opts->enc ? @@ -2072,9 +2075,9 @@ static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame, if (delta0 < 0 && delta > 0 && - ost->vsync_method != VSYNC_PASSTHROUGH + fps->vsync_method != VSYNC_PASSTHROUGH #if FFMPEG_OPT_VSYNC_DROP - && ost->vsync_method != VSYNC_DROP + && fps->vsync_method != VSYNC_DROP #endif ) { if (delta0 < -0.6) { @@ -2086,7 +2089,7 @@ static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame, delta0 = 0; } - switch (ost->vsync_method) { + switch (fps->vsync_method) { case VSYNC_VSCFR: if (fps->frame_number == 0 && delta0 >= 0.5) { av_log(ost, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index e8e5c677b8..253c2e58d4 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -140,7 +140,7 @@ static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt) OutputStream *ost = &ms->ost; #if FFMPEG_OPT_VSYNC_DROP - if (ost->type == AVMEDIA_TYPE_VIDEO && ost->vsync_method == VSYNC_DROP) + if (ost->type == AVMEDIA_TYPE_VIDEO && ms->ts_drop) pkt->pts = pkt->dts = AV_NOPTS_VALUE; #endif diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h index 16af6d38ba..f8b6f7a790 100644 --- a/fftools/ffmpeg_mux.h +++ b/fftools/ffmpeg_mux.h @@ -75,6 +75,9 @@ typedef struct MuxStream { int copy_initial_nonkeyframes; int copy_prior_start; int streamcopy_started; +#if FFMPEG_OPT_VSYNC_DROP + int ts_drop; +#endif } MuxStream; typedef struct Muxer { diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index b031cc59d2..6ffa4b7491 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -580,8 +580,10 @@ static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name) } static int new_stream_video(Muxer *mux, const OptionsContext *o, - OutputStream *ost, int *keep_pix_fmt) + OutputStream *ost, int *keep_pix_fmt, + enum VideoSyncMethod *vsync_method) { + MuxStream *ms = ms_from_ost(ost); AVFormatContext *oc = mux->fc; AVStream *st; char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL; @@ -773,49 +775,52 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o, #endif #if FFMPEG_OPT_VSYNC - ost->vsync_method = video_sync_method; + *vsync_method = video_sync_method; #else - ost->vsync_method = VSYNC_AUTO; + *vsync_method = VSYNC_AUTO; #endif MATCH_PER_STREAM_OPT(fps_mode, str, fps_mode, oc, st); if (fps_mode) { - ret = parse_and_set_vsync(fps_mode, &ost->vsync_method, ost->file->index, ost->index, 0); + ret = parse_and_set_vsync(fps_mode, vsync_method, ost->file->index, ost->index, 0); if (ret < 0) return ret; } if ((ost->frame_rate.num || ost->max_frame_rate.num) && - !(ost->vsync_method == VSYNC_AUTO || - ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR)) { + !(*vsync_method == VSYNC_AUTO || + *vsync_method == VSYNC_CFR || *vsync_method == VSYNC_VSCFR)) { av_log(ost, AV_LOG_FATAL, "One of -r/-fpsmax was specified " "together a non-CFR -vsync/-fps_mode. This is contradictory.\n"); return AVERROR(EINVAL); } - if (ost->vsync_method == VSYNC_AUTO) { + if (*vsync_method == VSYNC_AUTO) { if (ost->frame_rate.num || ost->max_frame_rate.num) { - ost->vsync_method = VSYNC_CFR; + *vsync_method = VSYNC_CFR; } else if (!strcmp(oc->oformat->name, "avi")) { - ost->vsync_method = VSYNC_VFR; + *vsync_method = VSYNC_VFR; } else { - ost->vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ? - ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ? - VSYNC_PASSTHROUGH : VSYNC_VFR) : - VSYNC_CFR; + *vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ? + ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ? + VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR; } - if (ost->ist && ost->vsync_method == VSYNC_CFR) { + if (ost->ist && *vsync_method == VSYNC_CFR) { const InputFile *ifile = ost->ist->file; if (ifile->nb_streams == 1 && ifile->input_ts_offset == 0) - ost->vsync_method = VSYNC_VSCFR; + *vsync_method = VSYNC_VSCFR; } - if (ost->vsync_method == VSYNC_CFR && copy_ts) { - ost->vsync_method = VSYNC_VSCFR; + if (*vsync_method == VSYNC_CFR && copy_ts) { + *vsync_method = VSYNC_VSCFR; } } - ost->is_cfr = (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR); + ost->is_cfr = (*vsync_method == VSYNC_CFR || *vsync_method == VSYNC_VSCFR); +#if FFMPEG_OPT_VSYNC_DROP + if (*vsync_method == VSYNC_DROP) + ms->ts_drop = 1; +#endif } return 0; @@ -1043,6 +1048,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, AVStream *st; int ret = 0, keep_pix_fmt = 0; AVRational enc_tb = { 0, 0 }; + enum VideoSyncMethod vsync_method = VSYNC_AUTO; const char *bsfs = NULL, *time_base = NULL; char *filters = NULL, *next, *codec_tag = NULL; double qscale = -1; @@ -1361,7 +1367,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, ms->copy_initial_nonkeyframes, oc, st); switch (type) { - case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost, &keep_pix_fmt); break; + case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost, &keep_pix_fmt, &vsync_method); break; case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break; case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break; } @@ -1382,6 +1388,7 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, ost->enc_ctx->pix_fmt : ost->enc_ctx->sample_fmt, .width = ost->enc_ctx->width, .height = ost->enc_ctx->height, + .vsync_method = vsync_method, .sample_rate = ost->enc_ctx->sample_rate, .ch_layout = ost->enc_ctx->ch_layout, .output_tb = enc_tb,