[FFmpeg-cvslog] fftools/cmdutils: Make allocate_array_elem() return ptr to new element

Andreas Rheinhardt git at videolan.org
Sun Dec 5 19:00:08 EET 2021


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Fri Dec  3 14:34:47 2021 +0100| [2e7ef008e312bde7c151034628adc2da04313566] | committer: Andreas Rheinhardt

fftools/cmdutils: Make allocate_array_elem() return ptr to new element

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=2e7ef008e312bde7c151034628adc2da04313566
---

 fftools/cmdutils.c      |  6 ++++--
 fftools/cmdutils.h      |  4 ++--
 fftools/ffmpeg_filter.c | 13 ++++---------
 fftools/ffmpeg_opt.c    |  9 +++------
 4 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 1464b122df..0b57552e5c 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -2216,8 +2216,9 @@ void *grow_array(void *array, int elem_size, int *size, int new_size)
 
 void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
 {
-    void *new_elem, **array = (void**)ptr;
+    void *new_elem, **array;
 
+    memcpy(&array, ptr, sizeof(array));
     if (*nb_elems == INT_MAX) {
         av_log(NULL, AV_LOG_ERROR, "Array too big.\n");
         exit_program(1);
@@ -2226,8 +2227,9 @@ void *allocate_array_elem(void *ptr, size_t elem_size, int *nb_elems)
     if (!new_elem)
         exit_program(1);
     GROW_ARRAY(array, *nb_elems);
+    memcpy(ptr, &array, sizeof(array));
     array[*nb_elems - 1] = new_elem;
-    return array;
+    return new_elem;
 }
 
 double get_rotation(int32_t *displaymatrix)
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index ae78e60f4c..82cda208be 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -638,7 +638,7 @@ void *grow_array(void *array, int elem_size, int *size, int new_size);
  * @param elem_size size of the new element to allocate
  * @param nb_elems  pointer to the number of elements of the array array;
  *                  *nb_elems will be incremented by one by this function.
- * @return reallocated array
+ * @return pointer to the newly allocated entry
  */
 void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
 
@@ -648,7 +648,7 @@ void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
     array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
 
 #define ALLOC_ARRAY_ELEM(array, nb_elems)\
-    array = allocate_array_elem(array, sizeof(*array[0]), &nb_elems)
+    allocate_array_elem(&array, sizeof(*array[0]), &nb_elems)
 
 #define GET_PIX_FMT_NAME(pix_fmt)\
     const char *name = av_get_pix_fmt_name(pix_fmt);
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 7263ea5f03..501a0acd61 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -166,16 +166,14 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
         exit_program(1);
     fg->index = nb_filtergraphs;
 
-    ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
-    ofilter = fg->outputs[0];
+    ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
     ofilter->ost    = ost;
     ofilter->graph  = fg;
     ofilter->format = -1;
 
     ost->filter = ofilter;
 
-    ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
-    ifilter = fg->inputs[0];
+    ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
     ifilter->ist    = ist;
     ifilter->graph  = fg;
     ifilter->format = -1;
@@ -281,8 +279,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     ist->decoding_needed |= DECODING_FOR_FILTER;
     ist->st->discard = AVDISCARD_NONE;
 
-    ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
-    ifilter = fg->inputs[fg->nb_inputs - 1];
+    ifilter = ALLOC_ARRAY_ELEM(fg->inputs, fg->nb_inputs);
     ifilter->ist    = ist;
     ifilter->graph  = fg;
     ifilter->format = -1;
@@ -318,9 +315,7 @@ int init_complex_filtergraph(FilterGraph *fg)
         init_input_filter(fg, cur);
 
     for (cur = outputs; cur;) {
-        OutputFilter *ofilter;
-        ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
-        ofilter = fg->outputs[fg->nb_outputs - 1];
+        OutputFilter *const ofilter = ALLOC_ARRAY_ELEM(fg->outputs, fg->nb_outputs);
 
         ofilter->graph   = fg;
         ofilter->out_tmp = cur;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index bd1d451ff7..eea57a6974 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1265,8 +1265,7 @@ static int open_input_file(OptionsContext *o, const char *filename)
     /* dump the file content */
     av_dump_format(ic, nb_input_files, filename, 0);
 
-    ALLOC_ARRAY_ELEM(input_files, nb_input_files);
-    f = input_files[nb_input_files - 1];
+    f = ALLOC_ARRAY_ELEM(input_files, nb_input_files);
 
     f->ctx        = ic;
     f->ist_index  = nb_input_streams - ic->nb_streams;
@@ -2260,8 +2259,7 @@ static int open_output_file(OptionsContext *o, const char *filename)
         }
     }
 
-    ALLOC_ARRAY_ELEM(output_files, nb_output_files);
-    of = output_files[nb_output_files - 1];
+    of = ALLOC_ARRAY_ELEM(output_files, nb_output_files);
 
     of->ost_index      = nb_output_streams;
     of->recording_time = o->recording_time;
@@ -3278,8 +3276,7 @@ static int opt_filter_complex_script(void *optctx, const char *opt, const char *
     if (!graph_desc)
         return AVERROR(EINVAL);
 
-    ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
-    fg = filtergraphs[nb_filtergraphs - 1];
+    fg = ALLOC_ARRAY_ELEM(filtergraphs, nb_filtergraphs);
     fg->index      = nb_filtergraphs - 1;
     fg->graph_desc = graph_desc;
 



More information about the ffmpeg-cvslog mailing list