[FFmpeg-devel] [PATCH 03/11] avutil/frame: add functions to check and ensure a side data entry is writable

James Almer jamrial at gmail.com
Thu Feb 20 19:24:46 EET 2025


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavutil/frame.h     | 19 +++++++++++++++++++
 libavutil/side_data.c | 25 +++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/libavutil/frame.h b/libavutil/frame.h
index 49260ae2dd..a707b35087 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -1211,6 +1211,25 @@ void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
 void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
                                         int props);
 
+/**
+ * Check whether the data described by a given AVFrameSideData can be
+ * written to.
+ *
+ * @return 1 if the caller may write to the data, 0 otherwise.
+ */
+int av_frame_side_data_is_writable(const AVFrameSideData *sd);
+
+/**
+ * Create a writable reference for the data described by a given
+ * AVFrameSideData, avoiding data copy if possible.
+ *
+ * @param sd Side data whose data should be made writable.
+ *
+ * @return 0 on success, a negative AVERROR on failure. On failure, the
+ *         side data is unchanged.
+ */
+int av_frame_side_data_make_writable(AVFrameSideData *sd);
+
 /**
  * @}
  */
diff --git a/libavutil/side_data.c b/libavutil/side_data.c
index 8c57fd838a..beb8ea3212 100644
--- a/libavutil/side_data.c
+++ b/libavutil/side_data.c
@@ -315,3 +315,28 @@ const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *s
     }
     return NULL;
 }
+
+int av_frame_side_data_is_writable(const AVFrameSideData *sd)
+{
+    return !!av_buffer_is_writable(sd->buf);
+}
+
+int av_frame_side_data_make_writable(AVFrameSideData *sd)
+{
+    AVBufferRef *buf = NULL;
+
+    if (av_buffer_is_writable(sd->buf))
+        return 0;
+
+    buf = av_buffer_alloc(sd->size);
+    if (!buf)
+        return AVERROR(ENOMEM);
+
+    if (sd->size)
+        memcpy(buf->data, sd->data, sd->size);
+    av_buffer_unref(&sd->buf);
+    sd->buf  = buf;
+    sd->data = buf->data;
+
+    return 0;
+}
-- 
2.48.1



More information about the ffmpeg-devel mailing list