From: Anton Khirnov <anton@khirnov.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 5/8] fftools/ffmpeg: move video frame dup/drop logic into its own function Date: Wed, 4 Jan 2023 17:42:43 +0100 Message-ID: <20230104164246.6133-5-anton@khirnov.net> (raw) In-Reply-To: <20230104164246.6133-1-anton@khirnov.net> --- fftools/ffmpeg.c | 137 ++++++++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 62 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 66a24a1a9c..04de57e41e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1030,6 +1030,79 @@ static void do_subtitle_out(OutputFile *of, } } +/* Convert frame timestamps to the encoder timebase and decide how many times + * should this (and possibly previous) frame be repeated in order to conform to + * desired target framerate (if any). + */ +static void video_sync_process(OutputFile *of, OutputStream *ost, + AVFrame *next_picture, double duration, + int64_t *nb_frames, int64_t *nb_frames_prev) +{ + double delta0, delta; + + double sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture); + /* delta0 is the "drift" between the input frame (next_picture) and + * where it would fall in the output. */ + delta0 = sync_ipts - ost->next_pts; + delta = delta0 + duration; + + // tracks the number of times the PREVIOUS frame should be duplicated, + // mostly for variable framerate (VFR) + *nb_frames_prev = 0; + /* by default, we output a single frame */ + *nb_frames = 1; + + if (delta0 < 0 && + delta > 0 && + ost->vsync_method != VSYNC_PASSTHROUGH && + ost->vsync_method != VSYNC_DROP) { + if (delta0 < -0.6) { + av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0); + } else + av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0); + sync_ipts = ost->next_pts; + duration += delta0; + delta0 = 0; + } + + switch (ost->vsync_method) { + case VSYNC_VSCFR: + if (ost->vsync_frame_number == 0 && delta0 >= 0.5) { + av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); + delta = duration; + delta0 = 0; + ost->next_pts = llrint(sync_ipts); + } + case VSYNC_CFR: + // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c + if (frame_drop_threshold && delta < frame_drop_threshold && ost->vsync_frame_number) { + *nb_frames = 0; + } else if (delta < -1.1) + *nb_frames = 0; + else if (delta > 1.1) { + *nb_frames = llrintf(delta); + if (delta0 > 1.1) + *nb_frames_prev = llrintf(delta0 - 0.6); + } + next_picture->duration = 1; + break; + case VSYNC_VFR: + if (delta <= -0.6) + *nb_frames = 0; + else if (delta > 0.6) + ost->next_pts = llrint(sync_ipts); + next_picture->duration = duration; + break; + case VSYNC_DROP: + case VSYNC_PASSTHROUGH: + next_picture->duration = duration; + ost->next_pts = llrint(sync_ipts); + break; + default: + av_assert0(0); + } +} + static enum AVPictureType forced_kf_apply(KeyframeForceCtx *kf, AVRational tb, const AVFrame *in_picture, int dup_idx) { @@ -1089,7 +1162,6 @@ static void do_video_out(OutputFile *of, AVCodecContext *enc = ost->enc_ctx; AVRational frame_rate; int64_t nb_frames, nb_frames_prev, i; - double delta, delta0; double duration = 0; InputStream *ist = ost->ist; AVFilterContext *filter = ost->filter->filter; @@ -1118,67 +1190,8 @@ static void do_video_out(OutputFile *of, ost->last_nb0_frames[1], ost->last_nb0_frames[2]); } else { - double sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture); - /* delta0 is the "drift" between the input frame (next_picture) and - * where it would fall in the output. */ - delta0 = sync_ipts - ost->next_pts; - delta = delta0 + duration; - - // tracks the number of times the PREVIOUS frame should be duplicated, - // mostly for variable framerate (VFR) - nb_frames_prev = 0; - /* by default, we output a single frame */ - nb_frames = 1; - - if (delta0 < 0 && - delta > 0 && - ost->vsync_method != VSYNC_PASSTHROUGH && - ost->vsync_method != VSYNC_DROP) { - if (delta0 < -0.6) { - av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0); - } else - av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0); - sync_ipts = ost->next_pts; - duration += delta0; - delta0 = 0; - } - - switch (ost->vsync_method) { - case VSYNC_VSCFR: - if (ost->vsync_frame_number == 0 && delta0 >= 0.5) { - av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); - delta = duration; - delta0 = 0; - ost->next_pts = llrint(sync_ipts); - } - case VSYNC_CFR: - // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c - if (frame_drop_threshold && delta < frame_drop_threshold && ost->vsync_frame_number) { - nb_frames = 0; - } else if (delta < -1.1) - nb_frames = 0; - else if (delta > 1.1) { - nb_frames = llrintf(delta); - if (delta0 > 1.1) - nb_frames_prev = llrintf(delta0 - 0.6); - } - next_picture->duration = 1; - break; - case VSYNC_VFR: - if (delta <= -0.6) - nb_frames = 0; - else if (delta > 0.6) - ost->next_pts = llrint(sync_ipts); - next_picture->duration = duration; - break; - case VSYNC_DROP: - case VSYNC_PASSTHROUGH: - next_picture->duration = duration; - ost->next_pts = llrint(sync_ipts); - break; - default: - av_assert0(0); - } + video_sync_process(of, ost, next_picture, duration, + &nb_frames, &nb_frames_prev); } memmove(ost->last_nb0_frames + 1, -- 2.35.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2023-01-04 16:45 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-01-04 16:42 [FFmpeg-devel] [PATCH 1/8] doc/ffmpeg.texi: drop a non-existent option Anton Khirnov 2023-01-04 16:42 ` [FFmpeg-devel] [PATCH 2/8] fftools/ffmpeg: stop using AVCodecContext.sample_rate in decode_audio() Anton Khirnov 2023-01-05 0:46 ` Michael Niedermayer 2023-01-05 11:09 ` Anton Khirnov 2023-01-04 16:42 ` [FFmpeg-devel] [PATCH 3/8] fftools/ffmpeg: fix stream id in an error message Anton Khirnov 2023-01-04 16:42 ` [FFmpeg-devel] [PATCH 4/8] fftools/ffmpeg: rename a variable to be more descriptive Anton Khirnov 2023-01-04 16:42 ` Anton Khirnov [this message] 2023-01-04 16:42 ` [FFmpeg-devel] [PATCH 6/8] fftools/ffmpeg: reindent after previous commit Anton Khirnov 2023-01-04 16:42 ` [FFmpeg-devel] [PATCH 7/8] doc/ffmpeg: improve -r documentation Anton Khirnov 2023-01-04 16:42 ` [FFmpeg-devel] [PATCH 8/8] fftools/ffmpeg: always generate CFR output when -r is used Anton Khirnov 2023-01-04 17:37 ` Paul B Mahol 2023-01-04 17:41 ` Anton Khirnov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230104164246.6133-5-anton@khirnov.net \ --to=anton@khirnov.net \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git