[FFmpeg-devel] [PATCH 7/7] avutil/frame: Treat frame as uninitialized in get_frame_defaults()

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Dec 3 22:45:48 EET 2021


Currently, it also tests whether extended_data points to something
different than the AVFrame's data array and frees extended_data
if it is different. Yet this is only necessary for one of its three
callers, namely av_frame_unref(); meanwhile the other two callers
took measures to avoid this (or rather, to make it to an av_free(NULL)).

This commit moves this chunk to av_frame_unref() (so that
get_frame_defaults() now treats its input as uninitialized)
and removes the now superfluous code in the other two callers.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
No code actually tests the scenario in which data != extended_data.
Of the 7984267 calls to get_frame_defaults() during FATE, 2784297
freed the extended_data, but in all these scenarios (which all came
from av_frame_alloc() and av_frame_move_ref()) extended_data was NULL.

 libavutil/frame.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/libavutil/frame.c b/libavutil/frame.c
index d4d3ad6988..95bdc0be72 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -52,9 +52,6 @@ const char *av_get_colorspace_name(enum AVColorSpace val)
 #endif
 static void get_frame_defaults(AVFrame *frame)
 {
-    if (frame->extended_data != frame->data)
-        av_freep(&frame->extended_data);
-
     memset(frame, 0, sizeof(*frame));
 
     frame->pts                   =
@@ -98,12 +95,11 @@ static void wipe_side_data(AVFrame *frame)
 
 AVFrame *av_frame_alloc(void)
 {
-    AVFrame *frame = av_mallocz(sizeof(*frame));
+    AVFrame *frame = av_malloc(sizeof(*frame));
 
     if (!frame)
         return NULL;
 
-    frame->extended_data = NULL;
     get_frame_defaults(frame);
 
     return frame;
@@ -455,6 +451,9 @@ void av_frame_unref(AVFrame *frame)
     av_buffer_unref(&frame->opaque_ref);
     av_buffer_unref(&frame->private_ref);
 
+    if (frame->extended_data != frame->data)
+        av_freep(&frame->extended_data);
+
     get_frame_defaults(frame);
 }
 
@@ -466,7 +465,6 @@ void av_frame_move_ref(AVFrame *dst, AVFrame *src)
     *dst = *src;
     if (src->extended_data == src->data)
         dst->extended_data = dst->data;
-    memset(src, 0, sizeof(*src));
     get_frame_defaults(src);
 }
 
-- 
2.32.0



More information about the ffmpeg-devel mailing list