FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/side_data.c
Date: 2026-10-15 16:30:37
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 6296 const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
70 {
71 6296 unsigned t = type;
72
3/4
✓ Branch 0 taken 6294 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6294 times.
✗ Branch 3 not taken.
6296 if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
73 6294 return &sd_props[t];
74 2 return NULL;
75 }
76
77 659 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
78 {
79 659 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
80
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 1 times.
659 return desc ? desc->name : NULL;
81 }
82
83 67627 static void free_side_data_entry(AVFrameSideData **ptr_sd)
84 {
85 67627 AVFrameSideData *sd = *ptr_sd;
86
87 67627 av_buffer_unref(&sd->buf);
88 67627 av_dict_free(&sd->metadata);
89 67627 av_freep(ptr_sd);
90 67627 }
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 471995 void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
110 enum AVFrameSideDataType type)
111 {
112
2/2
✓ Branch 0 taken 16945 times.
✓ Branch 1 taken 471995 times.
488940 for (int i = *nb_sd - 1; i >= 0; i--) {
113 16945 AVFrameSideData *entry = ((*sd)[i]);
114
2/2
✓ Branch 0 taken 16212 times.
✓ Branch 1 taken 733 times.
16945 if (entry->type != type)
115 16212 continue;
116
117 733 free_side_data_entry(&entry);
118
119 733 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
120 733 (*nb_sd)--;
121 }
122 471995 }
123
124 7460 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 7460 times.
7600 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 7460 }
139
140 11195370 void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
141 {
142
2/2
✓ Branch 0 taken 66870 times.
✓ Branch 1 taken 11195370 times.
11262240 for (int i = 0; i < *nb_sd; i++)
143 66870 free_side_data_entry(&((*sd)[i]));
144 11195370 *nb_sd = 0;
145
146 11195370 av_freep(sd);
147 11195370 }
148
149 67627 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 67627 times.
67627 if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
159 ✗ return NULL;
160
161 67627 tmp = av_realloc_array(*sd, *nb_sd + 1, sizeof(**sd));
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67627 times.
67627 if (!tmp)
163 ✗ return NULL;
164 67627 *sd = tmp;
165
166 67627 ret = av_mallocz(sizeof(*ret));
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67627 times.
67627 if (!ret)
168 ✗ return NULL;
169
170 67627 ret->buf = buf;
171 67627 ret->data = data;
172 67627 ret->size = size;
173 67627 ret->type = type;
174
175 67627 (*sd)[(*nb_sd)++] = ret;
176
177 67627 return ret;
178 }
179
180 65118 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 65113 times.
65118 if (!buf)
186 5 return NULL;
187
188 65113 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 704 AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
206 enum AVFrameSideDataType type,
207 size_t size, unsigned int flags)
208 {
209 704 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
210 704 AVBufferRef *buf = av_buffer_alloc(size);
211 704 AVFrameSideData *ret = NULL;
212
213
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 703 times.
704 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 704 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 693 times.
1399 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
216 695 (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 702 ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 if (!ret)
225 ✗ av_buffer_unref(&buf);
226
227 702 return ret;
228 }
229
230 1633 AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
231 enum AVFrameSideDataType type,
232 AVBufferRef **pbuf, unsigned int flags)
233 {
234 1633 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
235 1633 AVFrameSideData *sd_dst = NULL;
236 1633 AVBufferRef *buf = *pbuf;
237
238
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1631 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
1633 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 1632 times.
1633 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 1633 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1292 times.
✓ Branch 3 taken 341 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1291 times.
2925 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 1632 sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
247
248
3/4
✓ Branch 0 taken 1633 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1631 times.
✓ Branch 3 taken 2 times.
1633 if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
249 1631 *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 1633 return sd_dst;
253 }
254
255 2517 int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
256 const AVFrameSideData *src, unsigned int flags)
257 {
258 const AVSideDataDescriptor *desc;
259 2517 AVBufferRef *buf = NULL;
260 2517 AVFrameSideData *sd_dst = NULL;
261 2517 int ret = AVERROR_BUG;
262
263
7/10
✓ Branch 0 taken 2516 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2516 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2516 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 256 times.
✓ Branch 7 taken 2260 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 256 times.
2517 if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
264 1 return AVERROR(EINVAL);
265
266 2516 desc = av_frame_side_data_desc(src->type);
267
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 2226 times.
2516 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 2516 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2439 times.
✓ Branch 3 taken 77 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2437 times.
4955 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
270 2439 (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 2514 buf = av_buffer_ref(src->buf);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2514 times.
2514 if (!buf)
295 ✗ return AVERROR(ENOMEM);
296
297 2514 sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
298 2514 src->data, src->size);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2514 times.
2514 if (!sd_dst) {
300 ✗ av_buffer_unref(&buf);
301 ✗ return AVERROR(ENOMEM);
302 }
303
304 2514 ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2514 times.
2514 if (ret < 0) {
306 ✗ remove_side_data_by_entry(sd, nb_sd, sd_dst);
307 ✗ return ret;
308 }
309
310 2514 return 0;
311 }
312
313 4797185 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 89754 times.
✓ Branch 1 taken 4792011 times.
4881765 for (int i = 0; i < nb_sd; i++) {
318
2/2
✓ Branch 0 taken 5174 times.
✓ Branch 1 taken 84580 times.
89754 if (sd[i]->type == type)
319 5174 return sd[i];
320 }
321 4792011 return NULL;
322 }
323