From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id D956B4CC7E for ; Sat, 25 Jan 2025 20:22:11 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA88368B860; Sat, 25 Jan 2025 22:22:07 +0200 (EET) Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0E11768ADF9 for ; Sat, 25 Jan 2025 22:22:01 +0200 (EET) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2163b0c09afso60111355ad.0 for ; Sat, 25 Jan 2025 12:22:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737836518; x=1738441318; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=lzqVyApg6W3x63iJZBBCuuPwB/CP7662LVYF8qmcP60=; b=FPvv++t1uU/Cf9oUUEKd4HJ/Hx42U/NFHGmBIApeQ6HXOKndT2u7KKWiFrMUk0uaUj 0eojRkob/py9XuIhW3yXyn1LvidigSKkj45ExKcNXTmh68bhrkrH9WvRWn3bvvVipMf/ 0TYC9I8LgNfhEDYV0sNZFUjeD/sV8/YFEWP6ro06BSa1Y8yqw5OWl1ZPbllfAoGaRVv9 9gFESpkm4/xkN4jmFqrkV3lktG4XOYfZCw3TJFyWiVwpBq6TA4pVby/lFNYKMQbauja9 SNTXOBBcV4pAHXDYIXHyckxTzYRJhcs1Fy15XLDHB47UBRiLmWRgK9hzJbegPLJoWaO8 IGBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737836518; x=1738441318; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lzqVyApg6W3x63iJZBBCuuPwB/CP7662LVYF8qmcP60=; b=RTxzNd9xxpLu3NIEA8QnYJgCt76P7groxov9QxPq3eNQv/Hd68l2LdM9Ai1x3nHZM/ q22eBG3oKDxmANBGRfapytTpKRiiJYdy1Zk0KCcBFNUnf/ZUGs/k0g51HPJ4WHfaT1Am HU6oStNUhVbN1yU5zKzvLQq45ikBVEiF/3nf9qx9YHbqd1EVETRxK/v0mTGHMplsgEyT FT52Hy0/pd/nC1QLlJy++MsSM20mM6AzIkvwUvbnHfVDTYPl79ByA998dtprIVoVXpiC kcGAVk+aoyK/3ojYcovUwapftVbDsmgoLbsSQ+F+4P/ESmQoX+4pTBTqJpikQrfmwixN ADFQ== X-Gm-Message-State: AOJu0Yx59jaXbhzZwW1il3Sb/lFwGNqk9KrpnizpwY/JOruKvIt0IrS4 ENr7Fwy4rNcDQygwWDjS++nkrQ0Y4e40G9l15Mfh9Lq+HYESMqFE3dIJYg== X-Gm-Gg: ASbGncvl0OU/PrWEm+GYZcca1lxvuvPcrkk0HPeGD+q5yCpAoDghUpL8qaA6TWyzcdt nZZF9GSVVyKwr5/abm4u6XCAl4C63zfsj4xMF4apYhQJoF59y2TKlEjWLI9TsxeCNUc+3AYbKc1 js/xn9RlFB42JTMdTCRT8VW8c6ktBlgznrpERgE3h3/r/+VvfDZBIB64jZ7GWbvI2nmofiiSUTx g8ATbbwJi41vKK0VmBIfhrqy8m9hYznCFKmlGzzhttNp5uCKnHX/MTRQ66rtm85AXS5ukS5NQS3 2RoTfRER3hBi6JXX2UpuF3bV0JwMCAAW X-Google-Smtp-Source: AGHT+IEfSgARd5veG7Xzk1Htwp2eQM5dZNzgunEn/t+mrhKg2BWhLt2D2SY8D5wvsUbUaTjGBNNXzg== X-Received: by 2002:a17:903:1246:b0:216:2abc:194f with SMTP id d9443c01a7336-21c355c6546mr548980085ad.40.1737836517812; Sat, 25 Jan 2025 12:21:57 -0800 (PST) Received: from localhost.localdomain ([2800:2121:b040:c:f188:b333:21cc:e83f]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21da3ea396asm36155335ad.91.2025.01.25.12.21.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jan 2025 12:21:57 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Jan 2025 17:21:29 -0300 Message-ID: <20250125202143.12126-1-jamrial@gmail.com> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/14] avutil/frame: move side data helpers to a new file X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Should reduce clutter in frame.c, plus allow us to make opaque changes to side data handling. Signed-off-by: James Almer --- libavutil/Makefile | 1 + libavutil/frame.c | 318 +----------------------------------------- libavutil/side_data.c | 313 +++++++++++++++++++++++++++++++++++++++++ libavutil/side_data.h | 30 ++++ 4 files changed, 351 insertions(+), 311 deletions(-) create mode 100644 libavutil/side_data.c create mode 100644 libavutil/side_data.h diff --git a/libavutil/Makefile b/libavutil/Makefile index f8031815bd..7677a04d12 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -171,6 +171,7 @@ OBJS = adler32.o \ rc4.o \ ripemd.o \ samplefmt.o \ + side_data.o \ sha.o \ sha512.o \ slicethread.o \ diff --git a/libavutil/frame.c b/libavutil/frame.c index 10b59545f0..70a3b59123 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_FRAME_DATA_DOWNMIX_INFO] = { "Metadata relevant to a downmix procedure" }, - [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]); @@ -758,54 +665,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); } @@ -821,161 +686,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) { @@ -1044,21 +754,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, diff --git a/libavutil/side_data.c b/libavutil/side_data.c new file mode 100644 index 0000000000..b071971c9d --- /dev/null +++ b/libavutil/side_data.c @@ -0,0 +1,313 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avassert.h" +#include "buffer.h" +#include "common.h" +#include "dict.h" +#include "frame.h" +#include "mem.h" +#include "side_data.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_FRAME_DATA_DOWNMIX_INFO] = { "Metadata relevant to a downmix procedure" }, + [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 }, +}; + +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; +} + +static void free_side_data_entry(AVFrameSideData **ptr_sd) +{ + AVFrameSideData *sd = *ptr_sd; + + av_buffer_unref(&sd->buf); + av_dict_free(&sd->metadata); + av_freep(ptr_sd); +} + +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(&entry); + + ((*sd)[i]) = ((*sd)[*nb_sd - 1]); + (*nb_sd)--; + + return; + } +} + +void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd, + enum AVFrameSideDataType type) +{ + for (int i = *nb_sd - 1; i >= 0; i--) { + AVFrameSideData *entry = ((*sd)[i]); + if (entry->type != type) + continue; + + free_side_data_entry(&entry); + + ((*sd)[i]) = ((*sd)[*nb_sd - 1]); + (*nb_sd)--; + } +} + +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(&entry); + + ((*sd)[i]) = ((*sd)[*nb_sd - 1]); + (*nb_sd)--; + } +} + +void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd) +{ + for (int i = 0; i < *nb_sd; i++) + free_side_data_entry(&((*sd)[i])); + *nb_sd = 0; + + av_freep(sd); +} + +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; +} + +AVFrameSideData *ff_frame_side_data_add_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); +} + +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) + av_frame_side_data_remove(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 = ff_frame_side_data_add_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) + av_frame_side_data_remove(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 = ff_frame_side_data_add_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) + av_frame_side_data_remove(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; +} diff --git a/libavutil/side_data.h b/libavutil/side_data.h new file mode 100644 index 0000000000..8275aa35a5 --- /dev/null +++ b/libavutil/side_data.h @@ -0,0 +1,30 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_SIDE_DATA_H +#define AVUTIL_SIDE_DATA_H + +#include "buffer.h" +#include "frame.h" + +AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd, + int *nb_sd, + enum AVFrameSideDataType type, + AVBufferRef *buf); + +#endif // AVUTIL_SIDE_DATA_H -- 2.48.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".