[FFmpeg-devel] [PATCH] fftools/ffmpeg_opt: Don't duplicate array unnecessarily

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Nov 26 21:24:05 EET 2021


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 fftools/ffmpeg.c     |  3 ---
 fftools/ffmpeg.h     |  7 ++++---
 fftools/ffmpeg_opt.c | 43 ++++++++-----------------------------------
 3 files changed, 12 insertions(+), 41 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c838e2604c..0bc11949f2 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -555,9 +555,6 @@ static void ffmpeg_cleanup(int ret)
 
             avfilter_inout_free(&ofilter->out_tmp);
             av_freep(&ofilter->name);
-            av_freep(&ofilter->formats);
-            av_freep(&ofilter->channel_layouts);
-            av_freep(&ofilter->sample_rates);
             av_freep(&fg->outputs[j]);
         }
         av_freep(&fg->outputs);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 1728010f56..f10b63c21f 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -281,9 +281,10 @@ typedef struct OutputFilter {
     uint64_t channel_layout;
 
     // those are only set if no format is specified and the encoder gives us multiple options
-    int *formats;
-    uint64_t *channel_layouts;
-    int *sample_rates;
+    // They point directly to the relevant lists of the encoder.
+    const int *formats;
+    const uint64_t *channel_layouts;
+    const int *sample_rates;
 } OutputFilter;
 
 typedef struct FilterGraph {
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index feed772452..8a0cad3f62 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2631,7 +2631,6 @@ loop_end:
         /* set the filter output constraints */
         if (ost->filter) {
             OutputFilter *f = ost->filter;
-            int count;
             switch (ost->enc_ctx->codec_type) {
             case AVMEDIA_TYPE_VIDEO:
                 f->frame_rate = ost->frame_rate;
@@ -2639,51 +2638,25 @@ loop_end:
                 f->height     = ost->enc_ctx->height;
                 if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
                     f->format = ost->enc_ctx->pix_fmt;
-                } else if (ost->enc->pix_fmts) {
-                    count = 0;
-                    while (ost->enc->pix_fmts[count] != AV_PIX_FMT_NONE)
-                        count++;
-                    f->formats = av_calloc(count + 1, sizeof(*f->formats));
-                    if (!f->formats)
-                        exit_program(1);
-                    memcpy(f->formats, ost->enc->pix_fmts, (count + 1) * sizeof(*f->formats));
+                } else {
+                    f->formats = ost->enc->pix_fmts;
                 }
                 break;
             case AVMEDIA_TYPE_AUDIO:
                 if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
                     f->format = ost->enc_ctx->sample_fmt;
-                } else if (ost->enc->sample_fmts) {
-                    count = 0;
-                    while (ost->enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
-                        count++;
-                    f->formats = av_calloc(count + 1, sizeof(*f->formats));
-                    if (!f->formats)
-                        exit_program(1);
-                    memcpy(f->formats, ost->enc->sample_fmts, (count + 1) * sizeof(*f->formats));
+                } else {
+                    f->formats = ost->enc->sample_fmts;
                 }
                 if (ost->enc_ctx->sample_rate) {
                     f->sample_rate = ost->enc_ctx->sample_rate;
-                } else if (ost->enc->supported_samplerates) {
-                    count = 0;
-                    while (ost->enc->supported_samplerates[count])
-                        count++;
-                    f->sample_rates = av_calloc(count + 1, sizeof(*f->sample_rates));
-                    if (!f->sample_rates)
-                        exit_program(1);
-                    memcpy(f->sample_rates, ost->enc->supported_samplerates,
-                           (count + 1) * sizeof(*f->sample_rates));
+                } else {
+                    f->sample_rates = ost->enc->supported_samplerates;
                 }
                 if (ost->enc_ctx->channels) {
                     f->channel_layout = av_get_default_channel_layout(ost->enc_ctx->channels);
-                } else if (ost->enc->channel_layouts) {
-                    count = 0;
-                    while (ost->enc->channel_layouts[count])
-                        count++;
-                    f->channel_layouts = av_calloc(count + 1, sizeof(*f->channel_layouts));
-                    if (!f->channel_layouts)
-                        exit_program(1);
-                    memcpy(f->channel_layouts, ost->enc->channel_layouts,
-                           (count + 1) * sizeof(*f->channel_layouts));
+                } else {
+                    f->channel_layouts = ost->enc->channel_layouts;
                 }
                 break;
             }
-- 
2.30.2



More information about the ffmpeg-devel mailing list