[FFmpeg-devel] [PATCH v5 07/14] avutil/frame: add helper for adding existing side data to set

Jan Ekström jeebjp at gmail.com
Sun Nov 26 21:58:26 EET 2023


---
 libavutil/frame.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/frame.h | 16 +++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/libavutil/frame.c b/libavutil/frame.c
index 9ac3db430f..e42d9cb758 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -110,6 +110,24 @@ static void remove_side_data(AVFrameSideData ***sd, int *nb_side_data,
     }
 }
 
+static void remove_side_data_by_entry(AVFrameSideData ***sd,
+                                      int *nb_side_data,
+                                      const AVFrameSideData *target)
+{
+    for (int i = *nb_side_data - 1; i >= 0; i--) {
+        AVFrameSideData *entry = ((*sd)[i]);
+        if (entry != target)
+            continue;
+
+        free_side_data(&entry);
+
+        ((*sd)[i]) = ((*sd)[*nb_side_data - 1]);
+        (*nb_side_data)--;
+
+        return;
+    }
+}
+
 AVFrame *av_frame_alloc(void)
 {
     AVFrame *frame = av_malloc(sizeof(*frame));
@@ -879,6 +897,38 @@ AVFrameSideData *av_frame_side_data_set_new_entry(AVFrameSideDataSet *set,
     return ret;
 }
 
+int av_frame_side_data_set_entry_from_sd(AVFrameSideDataSet *dst,
+                                         const AVFrameSideData *src,
+                                         unsigned int flags)
+{
+    if (!dst || !src)
+        return AVERROR(EINVAL);
+
+    {
+        AVBufferRef           *buf    = av_buffer_ref(src->buf);
+        AVFrameSideData       *sd_dst = NULL;
+
+        if (flags & AV_FRAME_SIDE_DATA_SET_FLAG_NO_DUPLICATES)
+            remove_side_data(&dst->sd, &dst->nb_sd, src->type);
+
+        sd_dst = add_side_data_to_set_from_buf(dst, src->type, buf);
+        if (!sd_dst) {
+            av_buffer_unref(&buf);
+            return AVERROR(ENOMEM);
+        }
+
+        {
+            int ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
+            if (ret < 0) {
+                remove_side_data_by_entry(&dst->sd, &dst->nb_sd, sd_dst);
+                return ret;
+            }
+        }
+
+        return 0;
+    }
+}
+
 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
                                         enum AVFrameSideDataType type)
 {
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 093a76e30d..9295c868ef 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -1085,6 +1085,22 @@ AVFrameSideData *av_frame_side_data_set_new_entry(AVFrameSideDataSet *set,
                                                   size_t size,
                                                   unsigned int flags);
 
+/**
+ * Add a new side data entry to a set based on existing side data.
+ *
+ * @param dst a set to which the side data should be added
+ * @param src side data which should be added to the set
+ * @param flags Some combination of AV_FRAME_SIDE_DATA_SET_FLAG_* flags, or 0.
+ *
+ * @return negative error code on failure, >=0 on success. In case of
+ *         AV_FRAME_SIDE_DATA_SET_FLAG_NO_DUPLICATES being set, entries
+ *         of matching AVFrameSideDataType will be removed before the
+ *         addition is attempted.
+ */
+int av_frame_side_data_set_entry_from_sd(AVFrameSideDataSet *dst,
+                                         const AVFrameSideData *src,
+                                         unsigned int flags);
+
 /**
  * @}
  */
-- 
2.43.0



More information about the ffmpeg-devel mailing list