From patchwork Sat Nov 26 08:19:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 39451 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp6013100pzb; Sat, 26 Nov 2022 00:19:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf6RPkgE9obXlQTwsAGWpcaLoUtpQOHhD6aSJVFlzLMv7Sep1pEyccUykwZM6DasdIS20K+t X-Received: by 2002:a05:6402:25c6:b0:461:b825:d7cb with SMTP id x6-20020a05640225c600b00461b825d7cbmr16180615edb.167.1669450787524; Sat, 26 Nov 2022 00:19:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669450787; cv=none; d=google.com; s=arc-20160816; b=yIy37q7awBqPlp8vt1r6NBt2Dox/VRUtTaEdbX50QsDFIUOr+JJCy8jK2GAAj70qoj UbkmZUb1n0QFp1Xvijf3hcKC18gZuohKCbCbQtNE4ATalbwFA11CVQKgRucUOSXzBjjc p0qTVAEajfsTxZFkCdh+5xtGVERlHdJU9UcwArDt6bfMprLefwJLq2I+9hQNY+134wp/ CEO5NmkFAvX1wrCc79q97LmT74URKzO4EiAht5DDemayq6akfRuh2c1iJWFgmI862qQH 3fEfXrBVZToZbtVU7iA4QhGMdCwJKr9lPoVsZnaseY0kgYI6LpdWTWvT1vvDiR8P9IZ7 kQZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=noFfpeTiUqsPKrnA5CvlIHaPfOOO7FcL4MHlsjd/a3U=; b=q5uvNWrOGSpICQ03ATg7acbJqhl1xVU3Dke8OwE2A5RP05WdxGGfJP7/sHCszYRXFr BuNsUnx79GdG28eXeew8RAG3R7vF0p9KOfQwUgVK4C7z7y8k4KGP8YMB7Su5QN5kHGgM Qs7CMJUiuik3GdkQnz53+fuyU9zuMZjufJHkCMFjwqCCaj5GjLn8YB4mQXR7powfVK5m +IpUIDcdhr5AEY/keLPC9w7iPEzrs7UZleU5oLiTya2hQWTZxgJWotuGjlcIeQeKKOYR CHeyqUFaLznKQTqDkHe1XWZuuBbVZ/Aj7EaMInz4RZ1rTkIAr6fxWsmUCySCFc6z6a0V rbEw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id em7-20020a056402364700b004597671e0ddsi4878043edb.338.2022.11.26.00.19.47; Sat, 26 Nov 2022 00:19:47 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 72FB968B721; Sat, 26 Nov 2022 10:19:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B08BE68B48E for ; Sat, 26 Nov 2022 10:19:24 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7D07A2400F5 for ; Sat, 26 Nov 2022 09:19:24 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id XZro7c5hZvpS for ; Sat, 26 Nov 2022 09:19:23 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 907F8240499 for ; Sat, 26 Nov 2022 09:19:23 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 57CC53A040A for ; Sat, 26 Nov 2022 09:19:23 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 26 Nov 2022 09:19:00 +0100 Message-Id: <20221126081911.31275-2-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221126081911.31275-1-anton@khirnov.net> References: <20221126081911.31275-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/13] fftools/ffmpeg: move force-keyframe-related vars to a separate struct X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: LIXRStH68I5U There are 8 of them and they are typically used together. Allows to pass just this struct to forced_kf_apply(), which makes it clear that the rest of the OutputStream is not accessed there. --- fftools/ffmpeg.c | 92 +++++++++++++++++++-------------------- fftools/ffmpeg.h | 25 +++++++---- fftools/ffmpeg_mux.c | 7 +-- fftools/ffmpeg_mux_init.c | 8 ++-- 4 files changed, 68 insertions(+), 64 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index ba91af2c95..3d10ffc3b2 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1042,52 +1042,49 @@ static void do_subtitle_out(OutputFile *of, } } -static enum AVPictureType forced_kf_apply(OutputStream *ost, +static enum AVPictureType forced_kf_apply(KeyframeForceCtx *kf, AVRational tb, const AVFrame *in_picture, int dup_idx) { - AVCodecContext *enc = ost->enc_ctx; double pts_time; - if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE) - ost->forced_kf_ref_pts = in_picture->pts; + if (kf->ref_pts == AV_NOPTS_VALUE) + kf->ref_pts = in_picture->pts; - pts_time = (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base); - if (ost->forced_kf_index < ost->forced_kf_count && - in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) { - ost->forced_kf_index++; + pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb); + if (kf->index < kf->nb_pts && + in_picture->pts >= kf->pts[kf->index]) { + kf->index++; goto force_keyframe; - } else if (ost->forced_keyframes_pexpr) { + } else if (kf->pexpr) { double res; - ost->forced_keyframes_expr_const_values[FKF_T] = pts_time; - res = av_expr_eval(ost->forced_keyframes_pexpr, - ost->forced_keyframes_expr_const_values, NULL); + kf->expr_const_values[FKF_T] = pts_time; + res = av_expr_eval(kf->pexpr, + kf->expr_const_values, NULL); ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n", - ost->forced_keyframes_expr_const_values[FKF_N], - ost->forced_keyframes_expr_const_values[FKF_N_FORCED], - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N], - ost->forced_keyframes_expr_const_values[FKF_T], - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T], + kf->expr_const_values[FKF_N], + kf->expr_const_values[FKF_N_FORCED], + kf->expr_const_values[FKF_PREV_FORCED_N], + kf->expr_const_values[FKF_T], + kf->expr_const_values[FKF_PREV_FORCED_T], res); - ost->forced_keyframes_expr_const_values[FKF_N] += 1; + kf->expr_const_values[FKF_N] += 1; if (res) { - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = - ost->forced_keyframes_expr_const_values[FKF_N] - 1; - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = - ost->forced_keyframes_expr_const_values[FKF_T]; - ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1; + kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1; + kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T]; + kf->expr_const_values[FKF_N_FORCED] += 1; goto force_keyframe; } - } else if (ost->forced_keyframes && - !strncmp(ost->forced_keyframes, "source", 6) && + } else if (kf->forced_keyframes && + !strncmp(kf->forced_keyframes, "source", 6) && in_picture->key_frame == 1 && !dup_idx) { goto force_keyframe; - } else if (ost->forced_keyframes && - !strncmp(ost->forced_keyframes, "source_no_drop", 14) && + } else if (kf->forced_keyframes && + !strncmp(kf->forced_keyframes, "source_no_drop", 14) && !dup_idx) { - ost->dropped_keyframe = 0; - if ((in_picture->key_frame == 1) || ost->dropped_keyframe) + kf->dropped_keyframe = 0; + if ((in_picture->key_frame == 1) || kf->dropped_keyframe) goto force_keyframe; } @@ -1223,7 +1220,7 @@ static void do_video_out(OutputFile *of, } } ost->last_dropped = nb_frames == nb0_frames && next_picture; - ost->dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame; + ost->kf.dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame; /* duplicates frame if needed */ for (i = 0; i < nb_frames; i++) { @@ -1243,7 +1240,7 @@ static void do_video_out(OutputFile *of, return; in_picture->quality = enc->global_quality; - in_picture->pict_type = forced_kf_apply(ost, in_picture, i); + in_picture->pict_type = forced_kf_apply(&ost->kf, enc->time_base, in_picture, i); ret = submit_encode_frame(of, ost, in_picture); if (ret == AVERROR_EOF) @@ -2751,14 +2748,14 @@ static void set_encoder_id(OutputFile *of, OutputStream *ost) AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE); } -static void parse_forced_key_frames(char *kf, OutputStream *ost, +static void parse_forced_key_frames(KeyframeForceCtx *kf, OutputFile *of, AVCodecContext *avctx) { - char *p; + const char *p; int n = 1, i, size, index = 0; int64_t t, *pts; - for (p = kf; *p; p++) + for (p = kf->forced_keyframes; *p; p++) if (*p == ',') n++; size = n; @@ -2766,7 +2763,7 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, if (!pts) report_and_exit(AVERROR(ENOMEM)); - p = kf; + p = kf->forced_keyframes; for (i = 0; i < n; i++) { char *next = strchr(p, ','); @@ -2774,7 +2771,6 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, *next++ = 0; if (!memcmp(p, "chapters", 8)) { - OutputFile *of = output_files[ost->file_index]; AVChapter * const *ch; unsigned int nb_ch; int j; @@ -2808,8 +2804,8 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, av_assert0(index == size); qsort(pts, size, sizeof(*pts), compare_int64); - ost->forced_kf_count = size; - ost->forced_kf_pts = pts; + kf->nb_pts = size; + kf->pts = pts; } static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base) @@ -2958,24 +2954,24 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) enc_ctx->field_order = AV_FIELD_TT; } - if (ost->forced_keyframes) { - if (!strncmp(ost->forced_keyframes, "expr:", 5)) { - ret = av_expr_parse(&ost->forced_keyframes_pexpr, ost->forced_keyframes+5, + if (ost->kf.forced_keyframes) { + if (!strncmp(ost->kf.forced_keyframes, "expr:", 5)) { + ret = av_expr_parse(&ost->kf.pexpr, ost->kf.forced_keyframes+5, forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, - "Invalid force_key_frames expression '%s'\n", ost->forced_keyframes+5); + "Invalid force_key_frames expression '%s'\n", ost->kf.forced_keyframes+5); return ret; } - ost->forced_keyframes_expr_const_values[FKF_N] = 0; - ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0; - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN; - ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN; + ost->kf.expr_const_values[FKF_N] = 0; + ost->kf.expr_const_values[FKF_N_FORCED] = 0; + ost->kf.expr_const_values[FKF_PREV_FORCED_N] = NAN; + ost->kf.expr_const_values[FKF_PREV_FORCED_T] = NAN; // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes', // parse it only for static kf timings - } else if(strncmp(ost->forced_keyframes, "source", 6)) { - parse_forced_key_frames(ost->forced_keyframes, ost, ost->enc_ctx); + } else if(strncmp(ost->kf.forced_keyframes, "source", 6)) { + parse_forced_key_frames(&ost->kf, of, ost->enc_ctx); } } break; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 4b76b1d0d7..db1f00b259 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -487,6 +487,21 @@ typedef enum { MUXER_FINISHED = 2, } OSTFinished ; +typedef struct KeyframeForceCtx { + char *forced_keyframes; + + int64_t ref_pts; + + int64_t *pts; + int nb_pts; + int index; + + AVExpr *pexpr; + double expr_const_values[FKF_NB]; + + int dropped_keyframe; +} KeyframeForceCtx; + typedef struct OutputStream { int file_index; /* file index */ int index; /* stream index in the output file */ @@ -543,15 +558,7 @@ typedef struct OutputStream { AVRational frame_aspect_ratio; - /* forced key frames */ - int64_t forced_kf_ref_pts; - int64_t *forced_kf_pts; - int forced_kf_count; - int forced_kf_index; - char *forced_keyframes; - AVExpr *forced_keyframes_pexpr; - double forced_keyframes_expr_const_values[FKF_NB]; - int dropped_keyframe; + KeyframeForceCtx kf; /* audio only */ #if FFMPEG_OPT_MAP_CHANNEL diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 2b885f7ab0..7da29db8f4 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -667,11 +667,12 @@ static void ost_free(OutputStream **post) av_packet_free(&ost->pkt); av_dict_free(&ost->encoder_opts); - av_freep(&ost->forced_keyframes); - av_expr_free(ost->forced_keyframes_pexpr); + av_freep(&ost->kf.forced_keyframes); + av_freep(&ost->kf.pts); + av_expr_free(ost->kf.pexpr); + av_freep(&ost->avfilter); av_freep(&ost->logfile_prefix); - av_freep(&ost->forced_kf_pts); av_freep(&ost->apad); #if FFMPEG_OPT_MAP_CHANNEL diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 7aa2e030d8..bdec0744a9 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -202,7 +202,7 @@ static OutputStream *new_output_stream(Muxer *mux, const OptionsContext *o, ost->file_index = nb_output_files - 1; ost->index = idx; ost->st = st; - ost->forced_kf_ref_pts = AV_NOPTS_VALUE; + ost->kf.ref_pts = AV_NOPTS_VALUE; st->codecpar->codec_type = type; ret = choose_encoder(o, oc, ost, &enc); @@ -604,9 +604,9 @@ static OutputStream *new_video_stream(Muxer *mux, const OptionsContext *o, Input } } - MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->forced_keyframes, oc, st); - if (ost->forced_keyframes) - ost->forced_keyframes = av_strdup(ost->forced_keyframes); + MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->kf.forced_keyframes, oc, st); + if (ost->kf.forced_keyframes) + ost->kf.forced_keyframes = av_strdup(ost->kf.forced_keyframes); MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);