[FFmpeg-devel] [PATCH 05/14] avutil/frame: add functions to check and ensure a side data entry is writable
James Almer
jamrial at gmail.com
Sat Jan 25 22:21:33 EET 2025
Signed-off-by: James Almer <jamrial at gmail.com>
---
libavutil/frame.h | 19 ++++++++++++++++++
libavutil/side_data.c | 45 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 019adaac0e..aa4e619ad2 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -1207,6 +1207,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 7be1c346cd..8fb9dfed2b 100644
--- a/libavutil/side_data.c
+++ b/libavutil/side_data.c
@@ -369,3 +369,48 @@ const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *s
}
return NULL;
}
+
+int av_frame_side_data_is_writable(const AVFrameSideData *sd)
+{
+#if FF_API_SIDE_DATA_BUF
+FF_DISABLE_DEPRECATION_WARNINGS
+ return !!av_buffer_is_writable(sd->buf);
+#else
+ const FFFrameSideData *sdp = csdp_from_sd(sd);
+ return !!av_buffer_is_writable(sdp->buf);
+#endif
+}
+
+int av_frame_side_data_make_writable(AVFrameSideData *sd)
+{
+ FFFrameSideData *sdp = sdp_from_sd(sd);
+ AVBufferRef *buf = NULL;
+
+#if FF_API_SIDE_DATA_BUF
+FF_DISABLE_DEPRECATION_WARNINGS
+ if (av_buffer_is_writable(sd->buf))
+FF_ENABLE_DEPRECATION_WARNINGS
+#else
+ if (av_buffer_is_writable(sdp->buf))
+#endif
+ return 0;
+
+ buf = av_buffer_alloc(sd->size);
+ if (!buf)
+ return AVERROR(ENOMEM);
+
+ if (sd->size)
+ memcpy(buf->data, sd->data, sd->size);
+#if FF_API_SIDE_DATA_BUF
+FF_DISABLE_DEPRECATION_WARNINGS
+ av_buffer_unref(&sd->buf);
+ sd->buf = buf;
+FF_ENABLE_DEPRECATION_WARNINGS
+#else
+ av_buffer_unref(&sdp->buf);
+ sdp->buf = buf;
+#endif
+ sd->data = buf->data;
+
+ return 0;
+}
--
2.48.1
More information about the ffmpeg-devel
mailing list