From patchwork Sat Nov 26 08:19:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 39458 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp6013443pzb; Sat, 26 Nov 2022 00:20:49 -0800 (PST) X-Google-Smtp-Source: AA0mqf5QuTQ5/G66wZsFlMlQ60fEkFpAHxBjXaVp3g62k94kMnNSoyKNckg8B0qL+Ejz53nb3cSL X-Received: by 2002:a17:906:3c04:b0:7ae:359a:965 with SMTP id h4-20020a1709063c0400b007ae359a0965mr19441137ejg.621.1669450848926; Sat, 26 Nov 2022 00:20:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669450848; cv=none; d=google.com; s=arc-20160816; b=Dh9NYupSboNWwgb/Ucrcc/52iuuZdDhNeNgnIq5pEAajBGDk6GB3wQMINJ0QFyYnVD mGhhdZxcDjMVUZ7XPByZM4ZxMDjWG1YhAumeHKKiA6wOurceX0HEP4pB1Gu51lbEls0P h/YUZsEETxUsCqPb+YFdQJcD0VVmTEAgEDRKx6Phy3wBDrAIbiJ9nRAEmcSUeUjQ+6wU h0zEdyKK+lhQ0hkTIE50q47DtQRYy251tR4Al1l5cEpRdn4K0veC1xP1WJmvRjstEEIE Qa/xMEiloXEA6xUvr01w6y6U38YgtcP4lLBn04bUj6pJBpcjD2bVWSFHMxWmGfXsWhS4 8Wjw== 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=/BeETU9kb0BTlGkwOnQKad3aTAT271YabjJqsbj99dc=; b=yflRuU6HPzKdPKc9lQafjRcLAwUUOLtQvJx0OZd+j6rOs9DZ05JMJUpQjnCkFOaF+x 8CV92VWkP4bd7A7bszyNummIiMJ1n7pHgSS/Jz5FFpCschDWrm1P5dn5hBrrP5jaY+uZ ZWmAoNyXDK/LuMo3u5ZmjXVIqttLMi/lESw8rG+7eKQlBjpoT996TCpfgw/eY/QfsGYJ +ve8w6ECxXhFnjSAz37PxmdGn9GItpaTgIfYTHSopcaWy7luIGCcvo1yP+gcM+9IIJ7e 6uVB4yFKgNink7+yHLekOZ15QlGyllgPNtYdJfIFX9lcHEIQf43uL1FL3KEe3fd5vm4d P0vQ== 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 hs19-20020a1709073e9300b007ae743c61efsi6118579ejc.1002.2022.11.26.00.20.48; Sat, 26 Nov 2022 00:20:48 -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 13FBF68BA40; Sat, 26 Nov 2022 10:19:39 +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 0AE2568B8BE for ; Sat, 26 Nov 2022 10:19:30 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2674A2406CA for ; Sat, 26 Nov 2022 09:19:27 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id NJCcaJQEBTwW for ; Sat, 26 Nov 2022 09:19:25 +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 A9B0A2405F9 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 792AA3A06F1 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:03 +0100 Message-Id: <20221126081911.31275-5-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 05/13] fftools/ffmpeg: avoid storing full forced keyframe spec 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: goEdvYisgyvz It is not needed after the spec is parsed. Also avoids ugly string comparisons for each video frame. --- fftools/ffmpeg.c | 7 ++----- fftools/ffmpeg.h | 7 ++++++- fftools/ffmpeg_mux.c | 1 - fftools/ffmpeg_mux_init.c | 35 ++++++++++++++++++++--------------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 12ce108cc6..44582e3568 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1067,13 +1067,10 @@ static enum AVPictureType forced_kf_apply(KeyframeForceCtx *kf, AVRational tb, kf->expr_const_values[FKF_N_FORCED] += 1; goto force_keyframe; } - } else if (kf->forced_keyframes && - !strncmp(kf->forced_keyframes, "source", 6) && + } else if (kf->type == KF_FORCE_SOURCE && in_picture->key_frame == 1 && !dup_idx) { goto force_keyframe; - } else if (kf->forced_keyframes && - !strncmp(kf->forced_keyframes, "source_no_drop", 14) && - !dup_idx) { + } else if (kf->type == KF_FORCE_SOURCE_NO_DROP && !dup_idx) { kf->dropped_keyframe = 0; if ((in_picture->key_frame == 1) || kf->dropped_keyframe) goto force_keyframe; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index bf2abf55ee..5527dbe49b 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -487,8 +487,13 @@ typedef enum { MUXER_FINISHED = 2, } OSTFinished ; +enum { + KF_FORCE_SOURCE = 1, + KF_FORCE_SOURCE_NO_DROP = 2, +}; + typedef struct KeyframeForceCtx { - char *forced_keyframes; + int type; int64_t ref_pts; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index de5facbdc0..20524e5a28 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -667,7 +667,6 @@ static void ost_free(OutputStream **post) av_packet_free(&ost->pkt); av_dict_free(&ost->encoder_opts); - av_freep(&ost->kf.forced_keyframes); av_freep(&ost->kf.pts); av_expr_free(ost->kf.pexpr); diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 25e2ab8631..0280759b05 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -604,10 +604,6 @@ static OutputStream *new_video_stream(Muxer *mux, const OptionsContext *o, Input } } - 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); ost->top_field_first = -1; @@ -1759,13 +1755,14 @@ static int compare_int64(const void *a, const void *b) return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b); } -static void parse_forced_key_frames(KeyframeForceCtx *kf, const Muxer *mux) +static void parse_forced_key_frames(KeyframeForceCtx *kf, const Muxer *mux, + const char *spec) { const char *p; int n = 1, i, size, index = 0; int64_t t, *pts; - for (p = kf->forced_keyframes; *p; p++) + for (p = spec; *p; p++) if (*p == ',') n++; size = n; @@ -1773,7 +1770,7 @@ static void parse_forced_key_frames(KeyframeForceCtx *kf, const Muxer *mux) if (!pts) report_and_exit(AVERROR(ENOMEM)); - p = kf->forced_keyframes; + p = spec; for (i = 0; i < n; i++) { char *next = strchr(p, ','); @@ -1812,20 +1809,24 @@ static void parse_forced_key_frames(KeyframeForceCtx *kf, const Muxer *mux) kf->pts = pts; } -static int process_forced_keyframes(Muxer *mux) +static int process_forced_keyframes(Muxer *mux, const OptionsContext *o) { for (int i = 0; i < mux->of.nb_streams; i++) { OutputStream *ost = mux->of.streams[i]; + const char *forced_keyframes = NULL; + + MATCH_PER_STREAM_OPT(forced_key_frames, str, forced_keyframes, mux->fc, ost->st); - if (!ost->kf.forced_keyframes) + if (!(ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + ost->enc_ctx && forced_keyframes)) continue; - if (!strncmp(ost->kf.forced_keyframes, "expr:", 5)) { - int ret = av_expr_parse(&ost->kf.pexpr, ost->kf.forced_keyframes+5, + if (!strncmp(forced_keyframes, "expr:", 5)) { + int ret = av_expr_parse(&ost->kf.pexpr, 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->kf.forced_keyframes+5); + "Invalid force_key_frames expression '%s'\n", forced_keyframes + 5); return ret; } ost->kf.expr_const_values[FKF_N] = 0; @@ -1835,8 +1836,12 @@ static int process_forced_keyframes(Muxer *mux) // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes', // parse it only for static kf timings - } else if (strncmp(ost->kf.forced_keyframes, "source", 6)) { - parse_forced_key_frames(&ost->kf, mux); + } else if (!strcmp(forced_keyframes, "source")) { + ost->kf.type = KF_FORCE_SOURCE; + } else if (!strcmp(forced_keyframes, "source_no_drop")) { + ost->kf.type = KF_FORCE_SOURCE_NO_DROP; + } else { + parse_forced_key_frames(&ost->kf, mux, forced_keyframes); } } @@ -2061,7 +2066,7 @@ int of_open(const OptionsContext *o, const char *filename) // parse forced keyframe specifications; // must be done after chapters are created - err = process_forced_keyframes(mux); + err = process_forced_keyframes(mux, o); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error processing forced keyframes\n"); exit_program(1);