diff mbox series

[FFmpeg-devel,v7,10/14] avcodec: add helper for configuring AVCodecContext's frame side data

Message ID 20240229164307.3535613-11-jeebjp@gmail.com
State New
Headers show
Series [FFmpeg-devel,v7,01/14] avutil/frame: split side data list wiping out to non-AVFrame function | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Jan Ekström Feb. 29, 2024, 4:42 p.m. UTC
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 | 30 ++++++++++++++++++++++++++++++
 libavcodec/avcodec.h | 20 ++++++++++++++++++++
 2 files changed, 50 insertions(+)

Comments

Anton Khirnov March 1, 2024, 6:25 p.m. UTC | #1
Quoting Jan Ekström (2024-02-29 17:42:57)
> +/**
> + * 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 sd    array of side data to use as input.
> + *              if null, clears out the side data for this context.
> + * @param nb_sd integer containing the number of entries in the array.
> + * @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 AVFrameSideData **sd, const int nb_sd,
> +                                unsigned int flags);

Why should this be tied to avcodec and not just
av_frame_side_data_copy()?
diff mbox series

Patch

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index a13b781996..d3ec41b280 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -720,3 +720,33 @@  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 AVFrameSideData **sd, const int nb_sd,
+                                unsigned int flags)
+{
+    if (!avctx)
+        return AVERROR(EINVAL);
+
+    if (!sd) {
+        av_frame_side_data_free(
+            &avctx->frame_side_data, &avctx->nb_frame_side_data);
+        return 0;
+    }
+
+    if (nb_sd > 0 && nb_sd == avctx->nb_frame_side_data &&
+        sd == (const AVFrameSideData **)avctx->frame_side_data)
+        return AVERROR(EINVAL);
+
+    for (int i = 0; i < nb_sd; i++) {
+        int ret = av_frame_side_data_from_sd(
+            &avctx->frame_side_data, &avctx->nb_frame_side_data, sd[i], flags);
+        if (ret < 0) {
+            av_frame_side_data_free(
+                &avctx->frame_side_data, &avctx->nb_frame_side_data);
+            return ret;
+        }
+    }
+
+    return 0;
+}
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 411f4caad3..3b5ca9c0ef 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -3162,6 +3162,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 sd    array of side data to use as input.
+ *              if null, clears out the side data for this context.
+ * @param nb_sd integer containing the number of entries in the array.
+ * @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 AVFrameSideData **sd, const int nb_sd,
+                                unsigned int flags);
 /**
  * @}
  */