FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/side_data.c
Date: 2026-04-20 20:24:43
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_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 };
62
63 5261 const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
64 {
65 5261 unsigned t = type;
66
2/4
✓ Branch 0 taken 5261 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5261 times.
✗ Branch 3 not taken.
5261 if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
67 5261 return &sd_props[t];
68 return NULL;
69 }
70
71 500 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
72 {
73 500 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
74
1/2
✓ Branch 0 taken 500 times.
✗ Branch 1 not taken.
500 return desc ? desc->name : NULL;
75 }
76
77 54952 static void free_side_data_entry(AVFrameSideData **ptr_sd)
78 {
79 54952 AVFrameSideData *sd = *ptr_sd;
80
81 54952 av_buffer_unref(&sd->buf);
82 54952 av_dict_free(&sd->metadata);
83 54952 av_freep(ptr_sd);
84 54952 }
85
86 static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd,
87 const AVFrameSideData *target)
88 {
89 for (int i = *nb_sd - 1; i >= 0; i--) {
90 AVFrameSideData *entry = ((*sd)[i]);
91 if (entry != target)
92 continue;
93
94 free_side_data_entry(&entry);
95
96 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
97 (*nb_sd)--;
98
99 return;
100 }
101 }
102
103 457037 void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
104 enum AVFrameSideDataType type)
105 {
106
2/2
✓ Branch 0 taken 12980 times.
✓ Branch 1 taken 457037 times.
470017 for (int i = *nb_sd - 1; i >= 0; i--) {
107 12980 AVFrameSideData *entry = ((*sd)[i]);
108
2/2
✓ Branch 0 taken 12668 times.
✓ Branch 1 taken 312 times.
12980 if (entry->type != type)
109 12668 continue;
110
111 312 free_side_data_entry(&entry);
112
113 312 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
114 312 (*nb_sd)--;
115 }
116 457037 }
117
118 7311 void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
119 int props)
120 {
121
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7311 times.
7422 for (int i = *nb_sd - 1; i >= 0; i--) {
122 111 AVFrameSideData *entry = ((*sd)[i]);
123 111 const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type);
124
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))
125 111 continue;
126
127 free_side_data_entry(&entry);
128
129 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
130 (*nb_sd)--;
131 }
132 7311 }
133
134 10544324 void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
135 {
136
2/2
✓ Branch 0 taken 54640 times.
✓ Branch 1 taken 10544324 times.
10598964 for (int i = 0; i < *nb_sd; i++)
137 54640 free_side_data_entry(&((*sd)[i]));
138 10544324 *nb_sd = 0;
139
140 10544324 av_freep(sd);
141 10544324 }
142
143 54952 static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd,
144 int *nb_sd,
145 enum AVFrameSideDataType type,
146 AVBufferRef *buf, uint8_t *data,
147 size_t size)
148 {
149 AVFrameSideData *ret, **tmp;
150
151 // *nb_sd + 1 needs to fit into an int and a size_t.
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54952 times.
54952 if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
153 return NULL;
154
155 54952 tmp = av_realloc_array(*sd, *nb_sd + 1, sizeof(**sd));
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54952 times.
54952 if (!tmp)
157 return NULL;
158 54952 *sd = tmp;
159
160 54952 ret = av_mallocz(sizeof(*ret));
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54952 times.
54952 if (!ret)
162 return NULL;
163
164 54952 ret->buf = buf;
165 54952 ret->data = data;
166 54952 ret->size = size;
167 54952 ret->type = type;
168
169 54952 (*sd)[(*nb_sd)++] = ret;
170
171 54952 return ret;
172 }
173
174 52925 AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd,
175 int *nb_sd,
176 enum AVFrameSideDataType type,
177 AVBufferRef *buf)
178 {
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52925 times.
52925 if (!buf)
180 return NULL;
181
182 52925 return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
183 }
184
185 2 static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst,
186 AVBufferRef *buf, int flags)
187 {
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
189 return NULL;
190
191 2 av_dict_free(&dst->metadata);
192 2 av_buffer_unref(&dst->buf);
193 2 dst->buf = buf;
194 2 dst->data = buf->data;
195 2 dst->size = buf->size;
196 2 return dst;
197 }
198
199 697 AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
200 enum AVFrameSideDataType type,
201 size_t size, unsigned int flags)
202 {
203 697 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
204 697 AVBufferRef *buf = av_buffer_alloc(size);
205 697 AVFrameSideData *ret = NULL;
206
207
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 696 times.
697 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
208 1 av_frame_side_data_remove(sd, nb_sd, type);
209
5/6
✓ Branch 0 taken 697 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 690 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 688 times.
1387 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
210 690 (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
211 2 ret = replace_side_data_from_buf(ret, buf, flags);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ret)
213 av_buffer_unref(&buf);
214 2 return ret;
215 }
216
217 695 ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 if (!ret)
219 av_buffer_unref(&buf);
220
221 695 return ret;
222 }
223
224 1352 AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
225 enum AVFrameSideDataType type,
226 AVBufferRef **pbuf, unsigned int flags)
227 {
228 1352 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
229 1352 AVFrameSideData *sd_dst = NULL;
230 1352 AVBufferRef *buf = *pbuf;
231
232
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1350 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
1352 if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
233 return NULL;
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1352 times.
1352 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
235 av_frame_side_data_remove(sd, nb_sd, type);
236
4/6
✓ Branch 0 taken 1352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1036 times.
✓ Branch 3 taken 316 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1036 times.
2388 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
237 1036 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
238 sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
239 } else
240 1352 sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
241
242
3/4
✓ Branch 0 taken 1352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1350 times.
✓ Branch 3 taken 2 times.
1352 if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
243 1350 *pbuf = NULL;
244
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))
245 av_buffer_unref(&buf);
246 1352 return sd_dst;
247 }
248
249 2027 int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
250 const AVFrameSideData *src, unsigned int flags)
251 {
252 const AVSideDataDescriptor *desc;
253 2027 AVBufferRef *buf = NULL;
254 2027 AVFrameSideData *sd_dst = NULL;
255 2027 int ret = AVERROR_BUG;
256
257
6/10
✓ Branch 0 taken 2027 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2027 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2027 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 1942 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 85 times.
2027 if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
258 return AVERROR(EINVAL);
259
260 2027 desc = av_frame_side_data_desc(src->type);
261
2/2
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 1780 times.
2027 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
262 247 av_frame_side_data_remove(sd, nb_sd, src->type);
263
4/6
✓ Branch 0 taken 2027 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1975 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1975 times.
4002 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
264 1975 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
265 AVDictionary *dict = NULL;
266
267 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
268 return AVERROR(EEXIST);
269
270 ret = av_dict_copy(&dict, src->metadata, 0);
271 if (ret < 0)
272 return ret;
273
274 ret = av_buffer_replace(&sd_dst->buf, src->buf);
275 if (ret < 0) {
276 av_dict_free(&dict);
277 return ret;
278 }
279
280 av_dict_free(&sd_dst->metadata);
281 sd_dst->metadata = dict;
282 sd_dst->data = src->data;
283 sd_dst->size = src->size;
284 return 0;
285 }
286
287 2027 buf = av_buffer_ref(src->buf);
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2027 times.
2027 if (!buf)
289 return AVERROR(ENOMEM);
290
291 2027 sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
292 2027 src->data, src->size);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2027 times.
2027 if (!sd_dst) {
294 av_buffer_unref(&buf);
295 return AVERROR(ENOMEM);
296 }
297
298 2027 ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2027 times.
2027 if (ret < 0) {
300 remove_side_data_by_entry(sd, nb_sd, sd_dst);
301 return ret;
302 }
303
304 2027 return 0;
305 }
306
307 2874479 const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
308 const int nb_sd,
309 enum AVFrameSideDataType type)
310 {
311
2/2
✓ Branch 0 taken 43571 times.
✓ Branch 1 taken 2871497 times.
2915068 for (int i = 0; i < nb_sd; i++) {
312
2/2
✓ Branch 0 taken 2982 times.
✓ Branch 1 taken 40589 times.
43571 if (sd[i]->type == type)
313 2982 return sd[i];
314 }
315 2871497 return NULL;
316 }
317