FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/side_data.c
Date: 2026-10-06 20:08:58
Exec Total Coverage
Lines: 129 154 83.8%
Functions: 13 14 92.9%
Branches: 70 98 71.4%

Line Branch Exec Source
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
27 static const AVSideDataDescriptor sd_props[] = {
28 [AV_FRAME_DATA_PANSCAN] = { "AVPanScan", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
29 [AV_FRAME_DATA_A53_CC] = { "ATSC A53 Part 4 Closed Captions" },
30 [AV_FRAME_DATA_MATRIXENCODING] = { "AVMatrixEncoding", AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
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" },
33 [AV_FRAME_DATA_MOTION_VECTORS] = { "Motion vectors", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
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 },
40 [AV_FRAME_DATA_REGIONS_OF_INTEREST] = { "Regions Of Interest", AV_SIDE_DATA_PROP_SIZE_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 },
44 [AV_FRAME_DATA_DOVI_RPU_BUFFER] = { "Dolby Vision RPU Data", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
45 [AV_FRAME_DATA_DOVI_METADATA] = { "Dolby Vision Metadata", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
46 [AV_FRAME_DATA_LCEVC] = { "LCEVC NAL data", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
47 [AV_FRAME_DATA_VIEW_ID] = { "View ID" },
48 [AV_FRAME_DATA_STEREO3D] = { "Stereo 3D", AV_SIDE_DATA_PROP_GLOBAL },
49 [AV_FRAME_DATA_REPLAYGAIN] = { "AVReplayGain", AV_SIDE_DATA_PROP_GLOBAL },
50 [AV_FRAME_DATA_DISPLAYMATRIX] = { "3x3 displaymatrix", AV_SIDE_DATA_PROP_GLOBAL },
51 [AV_FRAME_DATA_AUDIO_SERVICE_TYPE] = { "Audio service type", AV_SIDE_DATA_PROP_GLOBAL },
52 [AV_FRAME_DATA_MASTERING_DISPLAY_METADATA] = { "Mastering display metadata", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
53 [AV_FRAME_DATA_CONTENT_LIGHT_LEVEL] = { "Content light level metadata", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
54 [AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT] = { "Ambient viewing environment", AV_SIDE_DATA_PROP_GLOBAL },
55 [AV_FRAME_DATA_SPHERICAL] = { "Spherical Mapping", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
56 [AV_FRAME_DATA_ICC_PROFILE] = { "ICC profile", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
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 },
59 [AV_FRAME_DATA_VIDEO_HINT] = { "Encoding video hint", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
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" },
64 [AV_FRAME_DATA_RAW_COLOR_PARAMS] = { "RAW camera color parameters", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
65 [AV_FRAME_DATA_DOWNMIX_MATRIX] = { "Downmix Matrix", AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
66 [AV_FRAME_DATA_GAIN_MAP_PARAMS] = { "Gain map parameters", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
67 };
68
69 6280 const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
70 {
71 6280 unsigned t = type;
72
3/4
✓ Branch 0 taken 6278 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6278 times.
✗ Branch 3 not taken.
6280 if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
73 6278 return &sd_props[t];
74 2 return NULL;
75 }
76
77 651 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
78 {
79 651 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
80
2/2
✓ Branch 0 taken 650 times.
✓ Branch 1 taken 1 times.
651 return desc ? desc->name : NULL;
81 }
82
83 67610 static void free_side_data_entry(AVFrameSideData **ptr_sd)
84 {
85 67610 AVFrameSideData *sd = *ptr_sd;
86
87 67610 av_buffer_unref(&sd->buf);
88 67610 av_dict_free(&sd->metadata);
89 67610 av_freep(ptr_sd);
90 67610 }
91
92 ✗ static 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
100 ✗ free_side_data_entry(&entry);
101
102 ✗ ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
103 ✗ (*nb_sd)--;
104
105 ✗ return;
106 }
107 }
108
109 471517 void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
110 enum AVFrameSideDataType type)
111 {
112
2/2
✓ Branch 0 taken 16941 times.
✓ Branch 1 taken 471517 times.
488458 for (int i = *nb_sd - 1; i >= 0; i--) {
113 16941 AVFrameSideData *entry = ((*sd)[i]);
114
2/2
✓ Branch 0 taken 16208 times.
✓ Branch 1 taken 733 times.
16941 if (entry->type != type)
115 16208 continue;
116
117 733 free_side_data_entry(&entry);
118
119 733 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
120 733 (*nb_sd)--;
121 }
122 471517 }
123
124 7391 void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
125 int props)
126 {
127
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 7391 times.
7531 for (int i = *nb_sd - 1; i >= 0; i--) {
128 140 AVFrameSideData *entry = ((*sd)[i]);
129 140 const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type);
130
3/4
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 24 times.
140 if (!desc || !(desc->props & props))
131 116 continue;
132
133 24 free_side_data_entry(&entry);
134
135 24 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
136 24 (*nb_sd)--;
137 }
138 7391 }
139
140 11233429 void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
141 {
142
2/2
✓ Branch 0 taken 66853 times.
✓ Branch 1 taken 11233429 times.
11300282 for (int i = 0; i < *nb_sd; i++)
143 66853 free_side_data_entry(&((*sd)[i]));
144 11233429 *nb_sd = 0;
145
146 11233429 av_freep(sd);
147 11233429 }
148
149 67610 static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd,
150 int *nb_sd,
151 enum AVFrameSideDataType type,
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67610 times.
67610 if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
159 ✗ return NULL;
160
161 67610 tmp = av_realloc_array(*sd, *nb_sd + 1, sizeof(**sd));
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67610 times.
67610 if (!tmp)
163 ✗ return NULL;
164 67610 *sd = tmp;
165
166 67610 ret = av_mallocz(sizeof(*ret));
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67610 times.
67610 if (!ret)
168 ✗ return NULL;
169
170 67610 ret->buf = buf;
171 67610 ret->data = data;
172 67610 ret->size = size;
173 67610 ret->type = type;
174
175 67610 (*sd)[(*nb_sd)++] = ret;
176
177 67610 return ret;
178 }
179
180 65102 AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd,
181 int *nb_sd,
182 enum AVFrameSideDataType type,
183 AVBufferRef *buf)
184 {
185
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 65097 times.
65102 if (!buf)
186 5 return NULL;
187
188 65097 return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
189 }
190
191 3 static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst,
192 AVBufferRef *buf, int flags)
193 {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
195 ✗ return NULL;
196
197 3 av_dict_free(&dst->metadata);
198 3 av_buffer_unref(&dst->buf);
199 3 dst->buf = buf;
200 3 dst->data = buf->data;
201 3 dst->size = buf->size;
202 3 return dst;
203 }
204
205 702 AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
206 enum AVFrameSideDataType type,
207 size_t size, unsigned int flags)
208 {
209 702 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
210 702 AVBufferRef *buf = av_buffer_alloc(size);
211 702 AVFrameSideData *ret = NULL;
212
213
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 701 times.
702 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
214 1 av_frame_side_data_remove(sd, nb_sd, type);
215
5/6
✓ Branch 0 taken 702 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 693 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 691 times.
1395 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
216 693 (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
217 2 ret = replace_side_data_from_buf(ret, buf, flags);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ret)
219 ✗ av_buffer_unref(&buf);
220 2 return ret;
221 }
222
223 700 ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700 times.
700 if (!ret)
225 ✗ av_buffer_unref(&buf);
226
227 700 return ret;
228 }
229
230 1628 AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
231 enum AVFrameSideDataType type,
232 AVBufferRef **pbuf, unsigned int flags)
233 {
234 1628 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
235 1628 AVFrameSideData *sd_dst = NULL;
236 1628 AVBufferRef *buf = *pbuf;
237
238
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1626 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
1628 if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
239 ✗ return NULL;
240
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1627 times.
1628 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
241 1 av_frame_side_data_remove(sd, nb_sd, type);
242
5/6
✓ Branch 0 taken 1628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1292 times.
✓ Branch 3 taken 336 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1291 times.
2920 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
243 1292 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
244 1 sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
245 } else
246 1627 sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
247
248
3/4
✓ Branch 0 taken 1628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1626 times.
✓ Branch 3 taken 2 times.
1628 if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
249 1626 *pbuf = NULL;
250
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 else if (!sd_dst && (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
251 ✗ av_buffer_unref(&buf);
252 1628 return sd_dst;
253 }
254
255 2516 int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
256 const AVFrameSideData *src, unsigned int flags)
257 {
258 const AVSideDataDescriptor *desc;
259 2516 AVBufferRef *buf = NULL;
260 2516 AVFrameSideData *sd_dst = NULL;
261 2516 int ret = AVERROR_BUG;
262
263
7/10
✓ Branch 0 taken 2515 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2515 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2515 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 256 times.
✓ Branch 7 taken 2259 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 256 times.
2516 if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
264 1 return AVERROR(EINVAL);
265
266 2515 desc = av_frame_side_data_desc(src->type);
267
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 2225 times.
2515 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
268 290 av_frame_side_data_remove(sd, nb_sd, src->type);
269
5/6
✓ Branch 0 taken 2515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2438 times.
✓ Branch 3 taken 77 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2436 times.
4953 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
270 2438 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
271 2 AVDictionary *dict = NULL;
272
273
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
274 1 return AVERROR(EEXIST);
275
276 1 ret = av_dict_copy(&dict, src->metadata, 0);
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
278 ✗ return ret;
279
280 1 ret = av_buffer_replace(&sd_dst->buf, src->buf);
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
282 ✗ av_dict_free(&dict);
283 ✗ return ret;
284 }
285
286 1 av_dict_free(&sd_dst->metadata);
287 1 sd_dst->metadata = dict;
288 1 sd_dst->data = src->data;
289 1 sd_dst->size = src->size;
290 1 return 0;
291 }
292
293 2513 buf = av_buffer_ref(src->buf);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2513 times.
2513 if (!buf)
295 ✗ return AVERROR(ENOMEM);
296
297 2513 sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
298 2513 src->data, src->size);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2513 times.
2513 if (!sd_dst) {
300 ✗ av_buffer_unref(&buf);
301 ✗ return AVERROR(ENOMEM);
302 }
303
304 2513 ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2513 times.
2513 if (ret < 0) {
306 ✗ remove_side_data_by_entry(sd, nb_sd, sd_dst);
307 ✗ return ret;
308 }
309
310 2513 return 0;
311 }
312
313 4799489 const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
314 const int nb_sd,
315 enum AVFrameSideDataType type)
316 {
317
2/2
✓ Branch 0 taken 89459 times.
✓ Branch 1 taken 4794316 times.
4883775 for (int i = 0; i < nb_sd; i++) {
318
2/2
✓ Branch 0 taken 5173 times.
✓ Branch 1 taken 84286 times.
89459 if (sd[i]->type == type)
319 5173 return sd[i];
320 }
321 4794316 return NULL;
322 }
323