diff options
author | James Almer <jamrial@gmail.com> | 2025-01-23 11:23:50 -0300 |
---|---|---|
committer | James Almer <jamrial@gmail.com> | 2025-03-03 18:10:10 -0300 |
commit | 964d28e83c59adbfb282c251490d1b033755379e (patch) | |
tree | 39f58fe0c8938ccbc4487bfa21dd7c734dccf4a8 /libavutil/frame.c | |
parent | 8631990f22b7c0637531916748daf18eea6d442c (diff) | |
download | ffmpeg-964d28e83c59adbfb282c251490d1b033755379e.tar.gz |
avutil/frame: move side data helpers to a new file
Should reduce clutter in frame.c, plus allow us to make opaque changes to
side data handling.
Signed-off-by: James Almer <jamrial@gmail.com>
Diffstat (limited to 'libavutil/frame.c')
-rw-r--r-- | libavutil/frame.c | 318 |
1 files changed, 7 insertions, 311 deletions
diff --git a/libavutil/frame.c b/libavutil/frame.c index 492b467ebd..71bde48f8b 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -26,41 +26,9 @@ #include "imgutils.h" #include "mem.h" #include "samplefmt.h" +#include "side_data.h" #include "hwcontext.h" -static const AVSideDataDescriptor sd_props[] = { - [AV_FRAME_DATA_PANSCAN] = { "AVPanScan", AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, - [AV_FRAME_DATA_A53_CC] = { "ATSC A53 Part 4 Closed Captions" }, - [AV_FRAME_DATA_MATRIXENCODING] = { "AVMatrixEncoding", AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT }, - [AV_FRAME_DATA_DOWNMIX_INFO] = { "Metadata relevant to a downmix procedure", AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT }, - [AV_FRAME_DATA_AFD] = { "Active format description" }, - [AV_FRAME_DATA_MOTION_VECTORS] = { "Motion vectors", AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, - [AV_FRAME_DATA_SKIP_SAMPLES] = { "Skip samples" }, - [AV_FRAME_DATA_GOP_TIMECODE] = { "GOP timecode" }, - [AV_FRAME_DATA_S12M_TIMECODE] = { "SMPTE 12-1 timecode" }, - [AV_FRAME_DATA_DYNAMIC_HDR_PLUS] = { "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_DYNAMIC_HDR_VIVID] = { "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_REGIONS_OF_INTEREST] = { "Regions Of Interest", AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, - [AV_FRAME_DATA_VIDEO_ENC_PARAMS] = { "Video encoding parameters" }, - [AV_FRAME_DATA_FILM_GRAIN_PARAMS] = { "Film grain parameters" }, - [AV_FRAME_DATA_DETECTION_BBOXES] = { "Bounding boxes for object detection and classification", AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, - [AV_FRAME_DATA_DOVI_RPU_BUFFER] = { "Dolby Vision RPU Data", AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_DOVI_METADATA] = { "Dolby Vision Metadata", AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_LCEVC] = { "LCEVC NAL data", AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, - [AV_FRAME_DATA_VIEW_ID] = { "View ID" }, - [AV_FRAME_DATA_STEREO3D] = { "Stereo 3D", AV_SIDE_DATA_PROP_GLOBAL }, - [AV_FRAME_DATA_REPLAYGAIN] = { "AVReplayGain", AV_SIDE_DATA_PROP_GLOBAL }, - [AV_FRAME_DATA_DISPLAYMATRIX] = { "3x3 displaymatrix", AV_SIDE_DATA_PROP_GLOBAL }, - [AV_FRAME_DATA_AUDIO_SERVICE_TYPE] = { "Audio service type", AV_SIDE_DATA_PROP_GLOBAL }, - [AV_FRAME_DATA_MASTERING_DISPLAY_METADATA] = { "Mastering display metadata", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_CONTENT_LIGHT_LEVEL] = { "Content light level metadata", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT] = { "Ambient viewing environment", AV_SIDE_DATA_PROP_GLOBAL }, - [AV_FRAME_DATA_SPHERICAL] = { "Spherical Mapping", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, - [AV_FRAME_DATA_ICC_PROFILE] = { "ICC profile", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT }, - [AV_FRAME_DATA_SEI_UNREGISTERED] = { "H.26[45] User Data Unregistered SEI message", AV_SIDE_DATA_PROP_MULTI }, - [AV_FRAME_DATA_VIDEO_HINT] = { "Encoding video hint", AV_SIDE_DATA_PROP_SIZE_DEPENDENT }, -}; - static void get_frame_defaults(AVFrame *frame) { memset(frame, 0, sizeof(*frame)); @@ -87,67 +55,6 @@ FF_ENABLE_DEPRECATION_WARNINGS frame->flags = 0; } -static void free_side_data(AVFrameSideData **ptr_sd) -{ - AVFrameSideData *sd = *ptr_sd; - - av_buffer_unref(&sd->buf); - av_dict_free(&sd->metadata); - av_freep(ptr_sd); -} - -static void wipe_side_data(AVFrameSideData ***sd, int *nb_side_data) -{ - for (int i = 0; i < *nb_side_data; i++) { - free_side_data(&((*sd)[i])); - } - *nb_side_data = 0; - - av_freep(sd); -} - -static void frame_side_data_wipe(AVFrame *frame) -{ - wipe_side_data(&frame->side_data, &frame->nb_side_data); -} - -void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd) -{ - wipe_side_data(sd, nb_sd); -} - -static void remove_side_data(AVFrameSideData ***sd, int *nb_side_data, - const enum AVFrameSideDataType type) -{ - for (int i = *nb_side_data - 1; i >= 0; i--) { - AVFrameSideData *entry = ((*sd)[i]); - if (entry->type != type) - continue; - - free_side_data(&entry); - - ((*sd)[i]) = ((*sd)[*nb_side_data - 1]); - (*nb_side_data)--; - } -} - -static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd, - const AVFrameSideData *target) -{ - for (int i = *nb_sd - 1; i >= 0; i--) { - AVFrameSideData *entry = ((*sd)[i]); - if (entry != target) - continue; - - free_side_data(&entry); - - ((*sd)[i]) = ((*sd)[*nb_sd - 1]); - (*nb_sd)--; - - return; - } -} - AVFrame *av_frame_alloc(void) { AVFrame *frame = av_malloc(sizeof(*frame)); @@ -377,7 +284,7 @@ FF_ENABLE_DEPRECATION_WARNINGS sd_dst = av_frame_new_side_data(dst, sd_src->type, sd_src->size); if (!sd_dst) { - frame_side_data_wipe(dst); + av_frame_side_data_free(&dst->side_data, &dst->nb_side_data); return AVERROR(ENOMEM); } memcpy(sd_dst->data, sd_src->data, sd_src->size); @@ -386,7 +293,7 @@ FF_ENABLE_DEPRECATION_WARNINGS sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref); if (!sd_dst) { av_buffer_unref(&ref); - frame_side_data_wipe(dst); + av_frame_side_data_free(&dst->side_data, &dst->nb_side_data); return AVERROR(ENOMEM); } } @@ -526,7 +433,7 @@ int av_frame_replace(AVFrame *dst, const AVFrame *src) if (ret < 0) goto fail; - frame_side_data_wipe(dst); + av_frame_side_data_free(&dst->side_data, &dst->nb_side_data); av_dict_free(&dst->metadata); ret = frame_copy_props(dst, src, 0); if (ret < 0) @@ -625,7 +532,7 @@ void av_frame_unref(AVFrame *frame) if (!frame) return; - frame_side_data_wipe(frame); + av_frame_side_data_free(&frame->side_data, &frame->nb_side_data); for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) av_buffer_unref(&frame->buf[i]); @@ -762,54 +669,12 @@ AVBufferRef *av_frame_get_plane_buffer(const AVFrame *frame, int plane) return NULL; } -static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd, - int *nb_sd, - enum AVFrameSideDataType type, - AVBufferRef *buf, uint8_t *data, - size_t size) -{ - AVFrameSideData *ret, **tmp; - - // *nb_sd + 1 needs to fit into an int and a size_t. - if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX)) - return NULL; - - tmp = av_realloc_array(*sd, sizeof(**sd), *nb_sd + 1); - if (!tmp) - return NULL; - *sd = tmp; - - ret = av_mallocz(sizeof(*ret)); - if (!ret) - return NULL; - - ret->buf = buf; - ret->data = data; - ret->size = size; - ret->type = type; - - (*sd)[(*nb_sd)++] = ret; - - return ret; -} - -static AVFrameSideData *add_side_data_from_buf(AVFrameSideData ***sd, - int *nb_sd, - enum AVFrameSideDataType type, - AVBufferRef *buf) -{ - if (!buf) - return NULL; - - return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size); -} - AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame, enum AVFrameSideDataType type, AVBufferRef *buf) { return - add_side_data_from_buf( + ff_frame_side_data_add_from_buf( &frame->side_data, &frame->nb_side_data, type, buf); } @@ -825,161 +690,6 @@ AVFrameSideData *av_frame_new_side_data(AVFrame *frame, return ret; } -static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst, - AVBufferRef *buf, int flags) -{ - if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE)) - return NULL; - - av_dict_free(&dst->metadata); - av_buffer_unref(&dst->buf); - dst->buf = buf; - dst->data = buf->data; - dst->size = buf->size; - return dst; -} - -AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, - enum AVFrameSideDataType type, - size_t size, unsigned int flags) -{ - const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); - AVBufferRef *buf = av_buffer_alloc(size); - AVFrameSideData *ret = NULL; - - if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) - remove_side_data(sd, nb_sd, type); - if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) && - (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) { - ret = replace_side_data_from_buf(ret, buf, flags); - if (!ret) - av_buffer_unref(&buf); - return ret; - } - - ret = add_side_data_from_buf(sd, nb_sd, type, buf); - if (!ret) - av_buffer_unref(&buf); - - return ret; -} - -AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd, - enum AVFrameSideDataType type, - AVBufferRef **pbuf, unsigned int flags) -{ - const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); - AVFrameSideData *sd_dst = NULL; - AVBufferRef *buf = *pbuf; - - if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf))) - return NULL; - if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) - remove_side_data(sd, nb_sd, type); - if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) && - (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) { - sd_dst = replace_side_data_from_buf(sd_dst, buf, flags); - } else - sd_dst = add_side_data_from_buf(sd, nb_sd, type, buf); - - if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF)) - *pbuf = NULL; - else if (!sd_dst && (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF)) - av_buffer_unref(&buf); - return sd_dst; -} - -int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, - const AVFrameSideData *src, unsigned int flags) -{ - const AVSideDataDescriptor *desc; - AVBufferRef *buf = NULL; - AVFrameSideData *sd_dst = NULL; - int ret = AVERROR_BUG; - - if (!sd || !src || !nb_sd || (*nb_sd && !*sd)) - return AVERROR(EINVAL); - - desc = av_frame_side_data_desc(src->type); - if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) - remove_side_data(sd, nb_sd, src->type); - if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) && - (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) { - AVDictionary *dict = NULL; - - if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE)) - return AVERROR(EEXIST); - - ret = av_dict_copy(&dict, src->metadata, 0); - if (ret < 0) - return ret; - - ret = av_buffer_replace(&sd_dst->buf, src->buf); - if (ret < 0) { - av_dict_free(&dict); - return ret; - } - - av_dict_free(&sd_dst->metadata); - sd_dst->metadata = dict; - sd_dst->data = src->data; - sd_dst->size = src->size; - return 0; - } - - buf = av_buffer_ref(src->buf); - if (!buf) - return AVERROR(ENOMEM); - - sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf, - src->data, src->size); - if (!sd_dst) { - av_buffer_unref(&buf); - return AVERROR(ENOMEM); - } - - ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0); - if (ret < 0) { - remove_side_data_by_entry(sd, nb_sd, sd_dst); - return ret; - } - - return 0; -} - -const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd, - const int nb_sd, - enum AVFrameSideDataType type) -{ - for (int i = 0; i < nb_sd; i++) { - if (sd[i]->type == type) - return sd[i]; - } - return NULL; -} - -void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd, - enum AVFrameSideDataType type) -{ - remove_side_data(sd, nb_sd, type); -} - -void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd, - int props) -{ - for (int i = *nb_sd - 1; i >= 0; i--) { - AVFrameSideData *entry = ((*sd)[i]); - const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type); - if (!desc || !(desc->props & props)) - continue; - - free_side_data(&entry); - - ((*sd)[i]) = ((*sd)[*nb_sd - 1]); - (*nb_sd)--; - } -} - AVFrameSideData *av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type) { @@ -1048,21 +758,7 @@ int av_frame_copy(AVFrame *dst, const AVFrame *src) void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type) { - remove_side_data(&frame->side_data, &frame->nb_side_data, type); -} - -const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type) -{ - unsigned t = type; - if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name) - return &sd_props[t]; - return NULL; -} - -const char *av_frame_side_data_name(enum AVFrameSideDataType type) -{ - const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); - return desc ? desc->name : NULL; + av_frame_side_data_remove(&frame->side_data, &frame->nb_side_data, type); } static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame, |