[FFmpeg-devel] [PATCH v5 10/14] avcodec: add helper for configuring AVCodecContext's side data set

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


This allows API clients that wish to configure multiple entries
at a time to do so without writing the looping code themselves.
---
 libavcodec/avcodec.c | 28 ++++++++++++++++++++++++++++
 libavcodec/avcodec.h | 20 ++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 2dda310e91..9b56af8114 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -718,3 +718,31 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
         return ff_decode_receive_frame(avctx, frame);
     return ff_encode_receive_frame(avctx, frame);
 }
+
+int avcodec_configure_side_data(AVCodecContext *avctx,
+                                const AVFrameSideDataSet *set,
+                                unsigned int flags)
+{
+    if (!avctx)
+        return AVERROR(EINVAL);
+
+    if (!set) {
+        av_frame_side_data_set_uninit(&avctx->frame_sd_set);
+        return 0;
+    }
+
+    if (set->nb_sd > 0 && set->nb_sd == avctx->frame_sd_set.nb_sd &&
+        set->sd == avctx->frame_sd_set.sd)
+        return AVERROR(EINVAL);
+
+    for (int i = 0; i < set->nb_sd; i++) {
+        int ret = av_frame_side_data_set_entry_from_sd(
+            &avctx->frame_sd_set, set->sd[i], flags);
+        if (ret < 0) {
+            av_frame_side_data_set_uninit(&avctx->frame_sd_set);
+            return ret;
+        }
+    }
+
+    return 0;
+}
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 0c1a8ade49..40261594f1 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -3154,6 +3154,26 @@ void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size);
  */
 int avcodec_is_open(AVCodecContext *s);
 
+/**
+ * Add multiple side data entries to an AVCodecContext set in one go, for
+ * example from an AVFrame.
+ *
+ * In case the function fails to add a side data entry, it will clear the
+ * whole side data set.
+ *
+ * @param avctx context to which the side data should be added
+ * @param set a set from which the side data should be added from.
+ *            if null, clears out the side data for this context.
+ * @param flags Some combination of AV_FRAME_SIDE_DATA_SET_FLAG_* flags, or 0.
+ *
+ * @return negative error code on failure, >=0 on success.
+ *
+ * @see av_frame_side_data_set_new_entry regarding the flags.
+ */
+int avcodec_configure_side_data(AVCodecContext *avctx,
+                                const AVFrameSideDataSet *set,
+                                unsigned int flags);
+
 /**
  * @}
  */
-- 
2.43.0



More information about the ffmpeg-devel mailing list