[FFmpeg-devel] [PATCH 1/3] avutil/frame: add AV_FRAME_SIDE_DATA_FLAG_NEW_REF

James Almer jamrial at gmail.com
Thu Jan 2 17:10:17 EET 2025


The flag is documented but did not exist. So introduce it as it can be
useful.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavutil/frame.c | 17 ++++++++---------
 libavutil/frame.h |  5 +++++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/libavutil/frame.c b/libavutil/frame.c
index ab0d8f535f..10b59545f0 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -868,21 +868,20 @@ AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
     AVFrameSideData *sd_dst  = NULL;
     AVBufferRef *buf = *pbuf;
 
+    if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
+        return NULL;
     if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
         remove_side_data(sd, nb_sd, type);
     if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
         (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
         sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
-        if (sd_dst)
-            *pbuf = NULL;
-        return sd_dst;
-    }
-
-    sd_dst = add_side_data_from_buf(sd, nb_sd, type, buf);
-    if (!sd_dst)
-        return NULL;
+    } else
+        sd_dst = add_side_data_from_buf(sd, nb_sd, type, buf);
 
-    *pbuf = NULL;
+    if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
+        *pbuf = NULL;
+    else if (!sd_dst && (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
+        av_buffer_unref(&buf);
     return sd_dst;
 }
 
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 929dbb885e..cfaa53695e 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -1078,6 +1078,11 @@ void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd);
  * Applies only for side data types without the AV_SIDE_DATA_PROP_MULTI prop.
  */
 #define AV_FRAME_SIDE_DATA_FLAG_REPLACE (1 << 1)
+/**
+ * Create a new reference to the passed in buffer instead of taking ownership
+ * of it.
+ */
+#define AV_FRAME_SIDE_DATA_FLAG_NEW_REF (1 << 2)
 
 /**
  * Add new side data entry to an array.
-- 
2.47.1



More information about the ffmpeg-devel mailing list