FFmpeg
Loading...
Searching...
No Matches
side_data.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "avassert.h"
20#include "buffer.h"
21#include "common.h"
22#include "dict.h"
23#include "frame.h"
24#include "mem.h"
25#include "side_data.h"
26
29 [AV_FRAME_DATA_A53_CC] = { "ATSC A53 Part 4 Closed Captions" },
31 [AV_FRAME_DATA_DOWNMIX_INFO] = { "Metadata relevant to a downmix procedure", AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
32 [AV_FRAME_DATA_AFD] = { "Active format description" },
34 [AV_FRAME_DATA_SKIP_SAMPLES] = { "Skip samples" },
35 [AV_FRAME_DATA_GOP_TIMECODE] = { "GOP timecode" },
36 [AV_FRAME_DATA_S12M_TIMECODE] = { "SMPTE 12-1 timecode" },
37 [AV_FRAME_DATA_DYNAMIC_HDR_PLUS] = { "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
38 [AV_FRAME_DATA_DYNAMIC_HDR_VIVID] = { "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
39 [AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5] = { "HDR Dynamic Metadata SMPTE2094-50", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
41 [AV_FRAME_DATA_VIDEO_ENC_PARAMS] = { "Video encoding parameters" },
42 [AV_FRAME_DATA_FILM_GRAIN_PARAMS] = { "Film grain parameters" },
43 [AV_FRAME_DATA_DETECTION_BBOXES] = { "Bounding boxes for object detection and classification", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
47 [AV_FRAME_DATA_VIEW_ID] = { "View ID" },
50 [AV_FRAME_DATA_DISPLAYMATRIX] = { "3x3 displaymatrix", AV_SIDE_DATA_PROP_GLOBAL },
54 [AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT] = { "Ambient viewing environment", AV_SIDE_DATA_PROP_GLOBAL },
57 [AV_FRAME_DATA_EXIF] = { "EXIF metadata", AV_SIDE_DATA_PROP_GLOBAL },
58 [AV_FRAME_DATA_SEI_UNREGISTERED] = { "H.26[45] User Data Unregistered SEI message", AV_SIDE_DATA_PROP_MULTI },
60 [AV_FRAME_DATA_3D_REFERENCE_DISPLAYS] = { "3D Reference Displays Information", AV_SIDE_DATA_PROP_GLOBAL },
61 [AV_FRAME_DATA_IAMF_MIX_GAIN_PARAM] = { "IAMF Mix Gain Parameter Data" },
62 [AV_FRAME_DATA_IAMF_DEMIXING_INFO_PARAM] = { "IAMF Demixing Info Parameter Data", AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
63 [AV_FRAME_DATA_IAMF_RECON_GAIN_INFO_PARAM] = { "IAMF Recon Gain Info Parameter Data" },
67};
68
70{
71 unsigned t = type;
72 if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
73 return &sd_props[t];
74 return NULL;
75}
76
82
84{
85 AVFrameSideData *sd = *ptr_sd;
86
87 av_buffer_unref(&sd->buf);
89 av_freep(ptr_sd);
90}
91
92static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd,
93 const AVFrameSideData *target)
94{
95 for (int i = *nb_sd - 1; i >= 0; i--) {
96 AVFrameSideData *entry = ((*sd)[i]);
97 if (entry != target)
98 continue;
99
101
102 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
103 (*nb_sd)--;
104
105 return;
106 }
107}
108
111{
112 for (int i = *nb_sd - 1; i >= 0; i--) {
113 AVFrameSideData *entry = ((*sd)[i]);
114 if (entry->type != type)
115 continue;
116
118
119 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
120 (*nb_sd)--;
121 }
122}
123
125 int props)
126{
127 for (int i = *nb_sd - 1; i >= 0; i--) {
128 AVFrameSideData *entry = ((*sd)[i]);
130 if (!desc || !(desc->props & props))
131 continue;
132
134
135 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
136 (*nb_sd)--;
137 }
138}
139
141{
142 for (int i = 0; i < *nb_sd; i++)
143 free_side_data_entry(&((*sd)[i]));
144 *nb_sd = 0;
145
146 av_freep(sd);
147}
148
150 int *nb_sd,
152 AVBufferRef *buf, uint8_t *data,
153 size_t size)
154{
155 AVFrameSideData *ret, **tmp;
156
157 // *nb_sd + 1 needs to fit into an int and a size_t.
158 if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
159 return NULL;
160
161 tmp = av_realloc_array(*sd, *nb_sd + 1, sizeof(**sd));
162 if (!tmp)
163 return NULL;
164 *sd = tmp;
165
166 ret = av_mallocz(sizeof(*ret));
167 if (!ret)
168 return NULL;
169
170 ret->buf = buf;
171 ret->data = data;
172 ret->size = size;
173 ret->type = type;
174
175 (*sd)[(*nb_sd)++] = ret;
176
177 return ret;
178}
179
181 int *nb_sd,
183 AVBufferRef *buf)
184{
185 if (!buf)
186 return NULL;
187
188 return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
189}
190
192 AVBufferRef *buf, int flags)
193{
195 return NULL;
196
197 av_dict_free(&dst->metadata);
198 av_buffer_unref(&dst->buf);
199 dst->buf = buf;
200 dst->data = buf->data;
201 dst->size = buf->size;
202 return dst;
203}
204
207 size_t size, unsigned int flags)
208{
211 AVFrameSideData *ret = NULL;
212
215 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
216 (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
217 ret = replace_side_data_from_buf(ret, buf, flags);
218 if (!ret)
219 av_buffer_unref(&buf);
220 return ret;
221 }
222
223 ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
224 if (!ret)
225 av_buffer_unref(&buf);
226
227 return ret;
228}
229
232 AVBufferRef **pbuf, unsigned int flags)
233{
235 AVFrameSideData *sd_dst = NULL;
236 AVBufferRef *buf = *pbuf;
237
238 if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
239 return NULL;
242 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
243 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
244 sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
245 } else
246 sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
247
248 if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
249 *pbuf = NULL;
250 else if (!sd_dst && (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
251 av_buffer_unref(&buf);
252 return sd_dst;
253}
254
256 const AVFrameSideData *src, unsigned int flags)
257{
259 AVBufferRef *buf = NULL;
260 AVFrameSideData *sd_dst = NULL;
261 int ret = AVERROR_BUG;
262
263 if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
264 return AVERROR(EINVAL);
265
268 av_frame_side_data_remove(sd, nb_sd, src->type);
269 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
270 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
271 AVDictionary *dict = NULL;
272
274 return AVERROR(EEXIST);
275
276 ret = av_dict_copy(&dict, src->metadata, 0);
277 if (ret < 0)
278 return ret;
279
280 ret = av_buffer_replace(&sd_dst->buf, src->buf);
281 if (ret < 0) {
282 av_dict_free(&dict);
283 return ret;
284 }
285
286 av_dict_free(&sd_dst->metadata);
287 sd_dst->metadata = dict;
288 sd_dst->data = src->data;
289 sd_dst->size = src->size;
290 return 0;
291 }
292
293 buf = av_buffer_ref(src->buf);
294 if (!buf)
295 return AVERROR(ENOMEM);
296
297 sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
298 src->data, src->size);
299 if (!sd_dst) {
300 av_buffer_unref(&buf);
301 return AVERROR(ENOMEM);
302 }
303
304 ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
305 if (ret < 0) {
306 remove_side_data_by_entry(sd, nb_sd, sd_dst);
307 return ret;
308 }
309
310 return 0;
311}
312
314 const int nb_sd,
316{
317 for (int i = 0; i < nb_sd; i++) {
318 if (sd[i]->type == type)
319 return sd[i];
320 }
321 return NULL;
322}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define entry
simple assert() macros that are a bit more flexible than ISO C assert().
refcounted data buffer API
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define NULL
Definition coverity.c:32
Public dictionary API.
reference-counted frame API
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition buffer.c:234
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:104
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition buffer.c:78
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:247
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
AVFrameSideData * av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type, size_t size, unsigned int flags)
Add new side data entry to an array.
Definition side_data.c:205
void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
Free all side data entries and their contents, then zeroes out the values which the pointers are poin...
Definition side_data.c:140
#define AV_FRAME_SIDE_DATA_FLAG_REPLACE
Don't add a new entry if another of the same type exists.
Definition frame.h:1105
#define AV_FRAME_SIDE_DATA_FLAG_NEW_REF
Create a new reference to the passed in buffer instead of taking ownership of it.
Definition frame.h:1110
AVFrameSideData * av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type, AVBufferRef **pbuf, unsigned int flags)
Add a new side data entry to an array from an existing AVBufferRef.
Definition side_data.c:230
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd, int props)
Remove and free all side data instances that match any of the given side data properties.
Definition side_data.c:124
void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type)
Remove and free all side data instances of the given type from an array.
Definition side_data.c:109
const AVFrameSideData * av_frame_side_data_get_c(const AVFrameSideData *const *sd, const int nb_sd, enum AVFrameSideDataType type)
Get a side data entry of a specific type from an array.
Definition side_data.c:313
const AVSideDataDescriptor * av_frame_side_data_desc(enum AVFrameSideDataType type)
Definition side_data.c:69
int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *src, unsigned int flags)
Add a new side data entry to an array based on existing side data, taking a reference towards the con...
Definition side_data.c:255
AVFrameSideDataType
Definition frame.h:49
#define AV_FRAME_SIDE_DATA_FLAG_UNIQUE
Remove existing entries before adding new ones.
Definition frame.h:1100
static const AVFrameSideData * av_frame_side_data_get(AVFrameSideData *const *sd, const int nb_sd, enum AVFrameSideDataType type)
Wrapper around av_frame_side_data_get_c() to workaround the limitation that for any type T the conver...
Definition frame.h:1203
const char * av_frame_side_data_name(enum AVFrameSideDataType type)
Definition side_data.c:77
@ AV_SIDE_DATA_PROP_GLOBAL
The side data type can be used in stream-global structures.
Definition frame.h:348
@ AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT
Side data depends on the channel layout.
Definition frame.h:375
@ AV_SIDE_DATA_PROP_SIZE_DEPENDENT
Side data depends on the video dimensions.
Definition frame.h:361
@ AV_SIDE_DATA_PROP_COLOR_DEPENDENT
Side data depends on the video color space.
Definition frame.h:368
@ AV_SIDE_DATA_PROP_MULTI
Multiple instances of this side data type can be meaningfully present in a single side data array.
Definition frame.h:354
@ AV_FRAME_DATA_GOP_TIMECODE
The GOP timecode in 25 bit timecode format.
Definition frame.h:125
@ AV_FRAME_DATA_EXIF
Exchangeable image file format metadata.
Definition frame.h:263
@ AV_FRAME_DATA_DOVI_METADATA
Parsed Dolby Vision metadata, suitable for passing to a software implementation.
Definition frame.h:208
@ AV_FRAME_DATA_VIEW_ID
This side data must be associated with a video frame.
Definition frame.h:245
@ AV_FRAME_DATA_SPHERICAL
The data represents the AVSphericalMapping structure defined in libavutil/spherical....
Definition frame.h:131
@ AV_FRAME_DATA_LCEVC
Raw LCEVC payload data, as a uint8_t array, with NAL emulation bytes intact.
Definition frame.h:236
@ AV_FRAME_DATA_DYNAMIC_HDR_VIVID
HDR Vivid dynamic metadata associated with a video frame.
Definition frame.h:215
@ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition frame.h:137
@ AV_FRAME_DATA_DOWNMIX_MATRIX
Metadata relevant to a downmix procedure in the form of a remixig matrix.
Definition frame.h:307
@ AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT
Ambient viewing environment metadata, as defined by H.274.
Definition frame.h:220
@ AV_FRAME_DATA_PANSCAN
The data is the AVPanScan struct defined in libavcodec.
Definition frame.h:53
@ AV_FRAME_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition frame.h:85
@ AV_FRAME_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition frame.h:59
@ AV_FRAME_DATA_SEI_UNREGISTERED
User data unregistered metadata associated with a video frame.
Definition frame.h:178
@ AV_FRAME_DATA_AUDIO_SERVICE_TYPE
This side data must be associated with an audio frame and corresponds to enum AVAudioServiceType defi...
Definition frame.h:114
@ AV_FRAME_DATA_DYNAMIC_HDR_PLUS
HDR dynamic metadata associated with a video frame.
Definition frame.h:159
@ AV_FRAME_DATA_REPLAYGAIN
ReplayGain information in the form of the AVReplayGain struct.
Definition frame.h:77
@ AV_FRAME_DATA_IAMF_RECON_GAIN_INFO_PARAM
IAMF Recon Gain Info Parameter Data associated with the audio frame.
Definition frame.h:294
@ AV_FRAME_DATA_IAMF_MIX_GAIN_PARAM
IAMF Mix Gain Parameter Data associated with the audio frame.
Definition frame.h:278
@ AV_FRAME_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition frame.h:109
@ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata associated with a video frame.
Definition frame.h:120
@ AV_FRAME_DATA_3D_REFERENCE_DISPLAYS
This side data contains information about the reference display width(s) and reference viewing distan...
Definition frame.h:256
@ AV_FRAME_DATA_DOWNMIX_INFO
Metadata relevant to a downmix procedure.
Definition frame.h:73
@ AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5
HDR dynamic metadata associated with a video frame.
Definition frame.h:270
@ AV_FRAME_DATA_FILM_GRAIN_PARAMS
Film grain parameters for a frame, described by AVFilmGrainParams.
Definition frame.h:188
@ AV_FRAME_DATA_VIDEO_ENC_PARAMS
Encoding parameters for a video frame, as described by AVVideoEncParams.
Definition frame.h:170
@ AV_FRAME_DATA_DETECTION_BBOXES
Bounding boxes for object detection and classification, as described by AVDetectionBBoxHeader.
Definition frame.h:194
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition frame.h:144
@ AV_FRAME_DATA_AFD
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
Definition frame.h:90
@ AV_FRAME_DATA_S12M_TIMECODE
Timecode which conforms to SMPTE ST 12-1.
Definition frame.h:152
@ AV_FRAME_DATA_MATRIXENCODING
The data is the AVMatrixEncoding enum defined in libavutil/channel_layout.h.
Definition frame.h:68
@ AV_FRAME_DATA_IAMF_DEMIXING_INFO_PARAM
IAMF Demixing Info Parameter Data associated with the audio frame.
Definition frame.h:286
@ AV_FRAME_DATA_DOVI_RPU_BUFFER
Dolby Vision RPU raw data, suitable for passing to x265 or other libraries.
Definition frame.h:201
@ AV_FRAME_DATA_STEREO3D
Stereoscopic 3d metadata.
Definition frame.h:64
@ AV_FRAME_DATA_GAIN_MAP_PARAMS
Parameters describing how to combine this gain map with its base image to form an alternate rendition...
Definition frame.h:314
@ AV_FRAME_DATA_VIDEO_HINT
Provide encoder-specific hinting information about changed/unchanged portions of a frame.
Definition frame.h:230
@ AV_FRAME_DATA_RAW_COLOR_PARAMS
Color information from a RAW camera codecs, needed to correctly process the video data.
Definition frame.h:301
@ AV_FRAME_DATA_MOTION_VECTORS
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
Definition frame.h:97
@ AV_FRAME_DATA_REGIONS_OF_INTEREST
Regions Of Interest, the data is an array of AVRegionOfInterest type, the number of array element is ...
Definition frame.h:165
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
uint32_t type
Definition jpegmpfenc.c:80
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
const char data[16]
Definition mxf.c:149
const char * name
Definition qsvenc.c:142
static void free_side_data_entry(AVFrameSideData **ptr_sd)
Definition side_data.c:83
static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *target)
Definition side_data.c:92
AVFrameSideData * ff_frame_side_data_add_from_buf(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type, AVBufferRef *buf)
Definition side_data.c:180
static AVFrameSideData * replace_side_data_from_buf(AVFrameSideData *dst, AVBufferRef *buf, int flags)
Definition side_data.c:191
static const AVSideDataDescriptor sd_props[]
Definition side_data.c:27
static AVFrameSideData * add_side_data_from_buf_ext(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type, AVBufferRef *buf, uint8_t *data, size_t size)
Definition side_data.c:149
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
size_t size
Size of data in bytes.
Definition buffer.h:94
Structure to hold side data for an AVFrame.
Definition frame.h:334
enum AVFrameSideDataType type
Definition frame.h:335
AVDictionary * metadata
Definition frame.h:338
size_t size
Definition frame.h:337
uint8_t * data
Definition frame.h:336
AVBufferRef * buf
Definition frame.h:339
This struct describes the properties of a side data type.
Definition frame.h:382
#define av_mallocz(s)
#define av_freep(p)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
int size