FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/side_data.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 109 154 70.8%
Functions: 13 14 92.9%
Branches: 58 98 59.2%

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_REGIONS_OF_INTEREST] = { "Regions Of Interest", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
40 [AV_FRAME_DATA_VIDEO_ENC_PARAMS] = { "Video encoding parameters" },
41 [AV_FRAME_DATA_FILM_GRAIN_PARAMS] = { "Film grain parameters" },
42 [AV_FRAME_DATA_DETECTION_BBOXES] = { "Bounding boxes for object detection and classification", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
43 [AV_FRAME_DATA_DOVI_RPU_BUFFER] = { "Dolby Vision RPU Data", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
44 [AV_FRAME_DATA_DOVI_METADATA] = { "Dolby Vision Metadata", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
45 [AV_FRAME_DATA_LCEVC] = { "LCEVC NAL data", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
46 [AV_FRAME_DATA_VIEW_ID] = { "View ID" },
47 [AV_FRAME_DATA_STEREO3D] = { "Stereo 3D", AV_SIDE_DATA_PROP_GLOBAL },
48 [AV_FRAME_DATA_REPLAYGAIN] = { "AVReplayGain", AV_SIDE_DATA_PROP_GLOBAL },
49 [AV_FRAME_DATA_DISPLAYMATRIX] = { "3x3 displaymatrix", AV_SIDE_DATA_PROP_GLOBAL },
50 [AV_FRAME_DATA_AUDIO_SERVICE_TYPE] = { "Audio service type", AV_SIDE_DATA_PROP_GLOBAL },
51 [AV_FRAME_DATA_MASTERING_DISPLAY_METADATA] = { "Mastering display metadata", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
52 [AV_FRAME_DATA_CONTENT_LIGHT_LEVEL] = { "Content light level metadata", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
53 [AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT] = { "Ambient viewing environment", AV_SIDE_DATA_PROP_GLOBAL },
54 [AV_FRAME_DATA_SPHERICAL] = { "Spherical Mapping", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
55 [AV_FRAME_DATA_ICC_PROFILE] = { "ICC profile", AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
56 [AV_FRAME_DATA_EXIF] = { "EXIF metadata", AV_SIDE_DATA_PROP_GLOBAL },
57 [AV_FRAME_DATA_SEI_UNREGISTERED] = { "H.26[45] User Data Unregistered SEI message", AV_SIDE_DATA_PROP_MULTI },
58 [AV_FRAME_DATA_VIDEO_HINT] = { "Encoding video hint", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
59 [AV_FRAME_DATA_3D_REFERENCE_DISPLAYS] = { "3D Reference Displays Information", AV_SIDE_DATA_PROP_GLOBAL },
60 };
61
62 5080 const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
63 {
64 5080 unsigned t = type;
65
2/4
✓ Branch 0 taken 5080 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5080 times.
✗ Branch 3 not taken.
5080 if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
66 5080 return &sd_props[t];
67 return NULL;
68 }
69
70 490 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
71 {
72 490 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
73
1/2
✓ Branch 0 taken 490 times.
✗ Branch 1 not taken.
490 return desc ? desc->name : NULL;
74 }
75
76 53621 static void free_side_data_entry(AVFrameSideData **ptr_sd)
77 {
78 53621 AVFrameSideData *sd = *ptr_sd;
79
80 53621 av_buffer_unref(&sd->buf);
81 53621 av_dict_free(&sd->metadata);
82 53621 av_freep(ptr_sd);
83 53621 }
84
85 static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd,
86 const AVFrameSideData *target)
87 {
88 for (int i = *nb_sd - 1; i >= 0; i--) {
89 AVFrameSideData *entry = ((*sd)[i]);
90 if (entry != target)
91 continue;
92
93 free_side_data_entry(&entry);
94
95 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
96 (*nb_sd)--;
97
98 return;
99 }
100 }
101
102 412555 void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
103 enum AVFrameSideDataType type)
104 {
105
2/2
✓ Branch 0 taken 12953 times.
✓ Branch 1 taken 412555 times.
425508 for (int i = *nb_sd - 1; i >= 0; i--) {
106 12953 AVFrameSideData *entry = ((*sd)[i]);
107
2/2
✓ Branch 0 taken 12699 times.
✓ Branch 1 taken 254 times.
12953 if (entry->type != type)
108 12699 continue;
109
110 254 free_side_data_entry(&entry);
111
112 254 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
113 254 (*nb_sd)--;
114 }
115 412555 }
116
117 7309 void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
118 int props)
119 {
120
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7309 times.
7420 for (int i = *nb_sd - 1; i >= 0; i--) {
121 111 AVFrameSideData *entry = ((*sd)[i]);
122 111 const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type);
123
2/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✗ Branch 3 not taken.
111 if (!desc || !(desc->props & props))
124 111 continue;
125
126 free_side_data_entry(&entry);
127
128 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
129 (*nb_sd)--;
130 }
131 7309 }
132
133 7671654 void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
134 {
135
2/2
✓ Branch 0 taken 53367 times.
✓ Branch 1 taken 7671654 times.
7725021 for (int i = 0; i < *nb_sd; i++)
136 53367 free_side_data_entry(&((*sd)[i]));
137 7671654 *nb_sd = 0;
138
139 7671654 av_freep(sd);
140 7671654 }
141
142 53621 static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd,
143 int *nb_sd,
144 enum AVFrameSideDataType type,
145 AVBufferRef *buf, uint8_t *data,
146 size_t size)
147 {
148 AVFrameSideData *ret, **tmp;
149
150 // *nb_sd + 1 needs to fit into an int and a size_t.
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53621 times.
53621 if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
152 return NULL;
153
154 53621 tmp = av_realloc_array(*sd, sizeof(**sd), *nb_sd + 1);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53621 times.
53621 if (!tmp)
156 return NULL;
157 53621 *sd = tmp;
158
159 53621 ret = av_mallocz(sizeof(*ret));
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53621 times.
53621 if (!ret)
161 return NULL;
162
163 53621 ret->buf = buf;
164 53621 ret->data = data;
165 53621 ret->size = size;
166 53621 ret->type = type;
167
168 53621 (*sd)[(*nb_sd)++] = ret;
169
170 53621 return ret;
171 }
172
173 52031 AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd,
174 int *nb_sd,
175 enum AVFrameSideDataType type,
176 AVBufferRef *buf)
177 {
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52031 times.
52031 if (!buf)
179 return NULL;
180
181 52031 return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
182 }
183
184 2 static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst,
185 AVBufferRef *buf, int flags)
186 {
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
188 return NULL;
189
190 2 av_dict_free(&dst->metadata);
191 2 av_buffer_unref(&dst->buf);
192 2 dst->buf = buf;
193 2 dst->data = buf->data;
194 2 dst->size = buf->size;
195 2 return dst;
196 }
197
198 570 AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
199 enum AVFrameSideDataType type,
200 size_t size, unsigned int flags)
201 {
202 570 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
203 570 AVBufferRef *buf = av_buffer_alloc(size);
204 570 AVFrameSideData *ret = NULL;
205
206
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 569 times.
570 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
207 1 av_frame_side_data_remove(sd, nb_sd, type);
208
5/6
✓ Branch 0 taken 570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 563 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 561 times.
1133 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
209 563 (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
210 2 ret = replace_side_data_from_buf(ret, buf, flags);
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ret)
212 av_buffer_unref(&buf);
213 2 return ret;
214 }
215
216 568 ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 568 times.
568 if (!ret)
218 av_buffer_unref(&buf);
219
220 568 return ret;
221 }
222
223 1312 AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
224 enum AVFrameSideDataType type,
225 AVBufferRef **pbuf, unsigned int flags)
226 {
227 1312 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
228 1312 AVFrameSideData *sd_dst = NULL;
229 1312 AVBufferRef *buf = *pbuf;
230
231
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1310 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
1312 if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
232 return NULL;
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312 times.
1312 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
234 av_frame_side_data_remove(sd, nb_sd, type);
235
4/6
✓ Branch 0 taken 1312 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 999 times.
✓ Branch 3 taken 313 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 999 times.
2311 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
236 999 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
237 sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
238 } else
239 1312 sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
240
241
3/4
✓ Branch 0 taken 1312 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1310 times.
✓ Branch 3 taken 2 times.
1312 if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
242 1310 *pbuf = NULL;
243
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))
244 av_buffer_unref(&buf);
245 1312 return sd_dst;
246 }
247
248 1590 int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
249 const AVFrameSideData *src, unsigned int flags)
250 {
251 const AVSideDataDescriptor *desc;
252 1590 AVBufferRef *buf = NULL;
253 1590 AVFrameSideData *sd_dst = NULL;
254 1590 int ret = AVERROR_BUG;
255
256
6/10
✓ Branch 0 taken 1590 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1590 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1590 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 68 times.
✓ Branch 7 taken 1522 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 68 times.
1590 if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
257 return AVERROR(EINVAL);
258
259 1590 desc = av_frame_side_data_desc(src->type);
260
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 1344 times.
1590 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
261 246 av_frame_side_data_remove(sd, nb_sd, src->type);
262
4/6
✓ Branch 0 taken 1590 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1538 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1538 times.
3128 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
263 1538 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
264 AVDictionary *dict = NULL;
265
266 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
267 return AVERROR(EEXIST);
268
269 ret = av_dict_copy(&dict, src->metadata, 0);
270 if (ret < 0)
271 return ret;
272
273 ret = av_buffer_replace(&sd_dst->buf, src->buf);
274 if (ret < 0) {
275 av_dict_free(&dict);
276 return ret;
277 }
278
279 av_dict_free(&sd_dst->metadata);
280 sd_dst->metadata = dict;
281 sd_dst->data = src->data;
282 sd_dst->size = src->size;
283 return 0;
284 }
285
286 1590 buf = av_buffer_ref(src->buf);
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1590 times.
1590 if (!buf)
288 return AVERROR(ENOMEM);
289
290 1590 sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
291 1590 src->data, src->size);
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1590 times.
1590 if (!sd_dst) {
293 av_buffer_unref(&buf);
294 return AVERROR(ENOMEM);
295 }
296
297 1590 ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1590 times.
1590 if (ret < 0) {
299 remove_side_data_by_entry(sd, nb_sd, sd_dst);
300 return ret;
301 }
302
303 1590 return 0;
304 }
305
306 2409724 const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
307 const int nb_sd,
308 enum AVFrameSideDataType type)
309 {
310
2/2
✓ Branch 0 taken 42361 times.
✓ Branch 1 taken 2407045 times.
2449406 for (int i = 0; i < nb_sd; i++) {
311
2/2
✓ Branch 0 taken 2679 times.
✓ Branch 1 taken 39682 times.
42361 if (sd[i]->type == type)
312 2679 return sd[i];
313 }
314 2407045 return NULL;
315 }
316