Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 05/13] fftools/ffmpeg: avoid storing full forced keyframe spec
Date: Sat, 26 Nov 2022 09:19:03 +0100
Message-ID: <20221126081911.31275-5-anton@khirnov.net> (raw)
In-Reply-To: <20221126081911.31275-1-anton@khirnov.net>

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);
-- 
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".

  parent reply	other threads:[~2022-11-26  8:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-26  8:18 [FFmpeg-devel] [PATCH 01/13] fftools/ffmpeg: stop explicitly closing decoders Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 02/13] fftools/ffmpeg: move force-keyframe-related vars to a separate struct Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 03/13] fftools/ffmpeg: store forced keyframe pts in AV_TIME_BASE_Q Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 04/13] fftools/ffmpeg: parse forced keyframes in of_open() Anton Khirnov
2022-11-26  8:19 ` Anton Khirnov [this message]
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 06/13] fftools/ffmpeg: move logging filtered frame timestamps Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 07/13] fftools/ffmpeg: set AVFrame.time_base after filtering Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 08/13] fftools/ffmpeg: stop calling adjust_frame_pts_to_encoder_tb() for audio Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 09/13] fftools/ffmpeg: call check_recording_time() with actual frame pts Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 10/13] fftools/ffmpeg: only convert video frame pts if we have a frame Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 11/13] fftools/ffmpeg: drop an always-false check Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 12/13] fftools/ffmpeg: remove a useless inner block Anton Khirnov
2022-11-26  8:19 ` [FFmpeg-devel] [PATCH 13/13] fftools/ffmpeg: cosmetics 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=20221126081911.31275-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