summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiklas Haas <git@haasn.dev>2024-02-17 21:35:25 +0100
committerAnton Khirnov <anton@khirnov.net>2024-03-04 13:59:21 +0100
commit046d4e4f357f849b50ca0be82c5c4768a1d4e083 (patch)
treee9d9d3de7e7a8ea29fe3a095e1dbae79c5bc4797
parent2ee64ee76f3d48a947adf67373be54f9ba42a5be (diff)
avcodec: add internal side data wrappers
The signature of these wrappers is more complicated due to a need to distinguish between "failed allocating side data" and "side data was already present". Signed-off-by: Anton Khirnov <anton@khirnov.net>
-rw-r--r--libavcodec/decode.c66
-rw-r--r--libavcodec/decode.h20
2 files changed, 86 insertions, 0 deletions
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 5c80ef9cd0..f3e8b72be7 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1857,6 +1857,72 @@ int ff_decode_preinit(AVCodecContext *avctx)
return 0;
}
+/**
+ * Check side data preference and clear existing side data from frame
+ * if needed.
+ *
+ * @retval 0 side data of this type can be added to frame
+ * @retval 1 side data of this type should not be added to frame
+ */
+static int side_data_pref(const AVCodecContext *avctx, AVFrame *frame,
+ enum AVFrameSideDataType type)
+{
+ DecodeContext *dc = decode_ctx(avctx->internal);
+
+ // Note: could be skipped for `type` without corresponding packet sd
+ if (av_frame_get_side_data(frame, type)) {
+ if (dc->side_data_pref_mask & (1ULL << type))
+ return 1;
+ av_frame_remove_side_data(frame, type);
+ }
+
+ return 0;
+}
+
+
+int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame,
+ enum AVFrameSideDataType type, size_t size,
+ AVFrameSideData **psd)
+{
+ AVFrameSideData *sd;
+
+ if (side_data_pref(avctx, frame, type)) {
+ if (psd)
+ *psd = NULL;
+ return 0;
+ }
+
+ sd = av_frame_new_side_data(frame, type, size);
+ if (psd)
+ *psd = sd;
+
+ return sd ? 0 : AVERROR(ENOMEM);
+}
+
+int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx,
+ AVFrame *frame, enum AVFrameSideDataType type,
+ AVBufferRef **buf, AVFrameSideData **psd)
+{
+ AVFrameSideData *sd = NULL;
+ int ret = 0;
+
+ if (side_data_pref(avctx, frame, type))
+ goto finish;
+
+ sd = av_frame_new_side_data_from_buf(frame, type, *buf);
+ if (sd)
+ *buf = NULL;
+ else
+ ret = AVERROR(ENOMEM);
+
+finish:
+ av_buffer_unref(buf);
+ if (psd)
+ *psd = sd;
+
+ return ret;
+}
+
int ff_copy_palette(void *dst, const AVPacket *src, void *logctx)
{
size_t size;
diff --git a/libavcodec/decode.h b/libavcodec/decode.h
index daf1a67444..b269b5a43b 100644
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@ -155,4 +155,24 @@ int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_pr
const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx,
enum AVPacketSideDataType type);
+/**
+ * Wrapper around av_frame_new_side_data, which rejects side data overridden by
+ * the demuxer. Returns 0 on success, and a negative error code otherwise.
+ * If successful and sd is not NULL, *sd may either contain a pointer to the new
+ * side data, or NULL in case the side data was already present.
+ */
+int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame,
+ enum AVFrameSideDataType type, size_t size,
+ AVFrameSideData **sd);
+
+/**
+ * Similar to `ff_frame_new_side_data`, but using an existing buffer ref.
+ *
+ * *buf is ALWAYS consumed by this function and NULL written in its place, even
+ * on failure.
+ */
+int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx,
+ AVFrame *frame, enum AVFrameSideDataType type,
+ AVBufferRef **buf, AVFrameSideData **sd);
+
#endif /* AVCODEC_DECODE_H */