FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/side_data.c
Date: 2025-07-12 02:25:37
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_SEI_UNREGISTERED] = { "H.26[45] User Data Unregistered SEI message", AV_SIDE_DATA_PROP_MULTI },
57 [AV_FRAME_DATA_VIDEO_HINT] = { "Encoding video hint", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
58 [AV_FRAME_DATA_3D_REFERENCE_DISPLAYS] = { "3D Reference Displays Information", AV_SIDE_DATA_PROP_GLOBAL },
59 };
60
61 4773 const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
62 {
63 4773 unsigned t = type;
64
2/4
✓ Branch 0 taken 4773 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4773 times.
✗ Branch 3 not taken.
4773 if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
65 4773 return &sd_props[t];
66 return NULL;
67 }
68
69 486 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
70 {
71 486 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
72
1/2
✓ Branch 0 taken 486 times.
✗ Branch 1 not taken.
486 return desc ? desc->name : NULL;
73 }
74
75 51017 static void free_side_data_entry(AVFrameSideData **ptr_sd)
76 {
77 51017 AVFrameSideData *sd = *ptr_sd;
78
79 51017 av_buffer_unref(&sd->buf);
80 51017 av_dict_free(&sd->metadata);
81 51017 av_freep(ptr_sd);
82 51017 }
83
84 static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd,
85 const AVFrameSideData *target)
86 {
87 for (int i = *nb_sd - 1; i >= 0; i--) {
88 AVFrameSideData *entry = ((*sd)[i]);
89 if (entry != target)
90 continue;
91
92 free_side_data_entry(&entry);
93
94 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
95 (*nb_sd)--;
96
97 return;
98 }
99 }
100
101 411693 void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
102 enum AVFrameSideDataType type)
103 {
104
2/2
✓ Branch 0 taken 12931 times.
✓ Branch 1 taken 411693 times.
424624 for (int i = *nb_sd - 1; i >= 0; i--) {
105 12931 AVFrameSideData *entry = ((*sd)[i]);
106
2/2
✓ Branch 0 taken 12677 times.
✓ Branch 1 taken 254 times.
12931 if (entry->type != type)
107 12677 continue;
108
109 254 free_side_data_entry(&entry);
110
111 254 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
112 254 (*nb_sd)--;
113 }
114 411693 }
115
116 7309 void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
117 int props)
118 {
119
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7309 times.
7420 for (int i = *nb_sd - 1; i >= 0; i--) {
120 111 AVFrameSideData *entry = ((*sd)[i]);
121 111 const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type);
122
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))
123 111 continue;
124
125 free_side_data_entry(&entry);
126
127 ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
128 (*nb_sd)--;
129 }
130 7309 }
131
132 7650810 void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
133 {
134
2/2
✓ Branch 0 taken 50763 times.
✓ Branch 1 taken 7650810 times.
7701573 for (int i = 0; i < *nb_sd; i++)
135 50763 free_side_data_entry(&((*sd)[i]));
136 7650810 *nb_sd = 0;
137
138 7650810 av_freep(sd);
139 7650810 }
140
141 51017 static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd,
142 int *nb_sd,
143 enum AVFrameSideDataType type,
144 AVBufferRef *buf, uint8_t *data,
145 size_t size)
146 {
147 AVFrameSideData *ret, **tmp;
148
149 // *nb_sd + 1 needs to fit into an int and a size_t.
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51017 times.
51017 if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
151 return NULL;
152
153 51017 tmp = av_realloc_array(*sd, sizeof(**sd), *nb_sd + 1);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51017 times.
51017 if (!tmp)
155 return NULL;
156 51017 *sd = tmp;
157
158 51017 ret = av_mallocz(sizeof(*ret));
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51017 times.
51017 if (!ret)
160 return NULL;
161
162 51017 ret->buf = buf;
163 51017 ret->data = data;
164 51017 ret->size = size;
165 51017 ret->type = type;
166
167 51017 (*sd)[(*nb_sd)++] = ret;
168
169 51017 return ret;
170 }
171
172 49502 AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd,
173 int *nb_sd,
174 enum AVFrameSideDataType type,
175 AVBufferRef *buf)
176 {
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49502 times.
49502 if (!buf)
178 return NULL;
179
180 49502 return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
181 }
182
183 2 static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst,
184 AVBufferRef *buf, int flags)
185 {
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
187 return NULL;
188
189 2 av_dict_free(&dst->metadata);
190 2 av_buffer_unref(&dst->buf);
191 2 dst->buf = buf;
192 2 dst->data = buf->data;
193 2 dst->size = buf->size;
194 2 return dst;
195 }
196
197 570 AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
198 enum AVFrameSideDataType type,
199 size_t size, unsigned int flags)
200 {
201 570 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
202 570 AVBufferRef *buf = av_buffer_alloc(size);
203 570 AVFrameSideData *ret = NULL;
204
205
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 569 times.
570 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
206 1 av_frame_side_data_remove(sd, nb_sd, type);
207
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)) &&
208 563 (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
209 2 ret = replace_side_data_from_buf(ret, buf, flags);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ret)
211 av_buffer_unref(&buf);
212 2 return ret;
213 }
214
215 568 ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 568 times.
568 if (!ret)
217 av_buffer_unref(&buf);
218
219 568 return ret;
220 }
221
222 1108 AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
223 enum AVFrameSideDataType type,
224 AVBufferRef **pbuf, unsigned int flags)
225 {
226 1108 const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
227 1108 AVFrameSideData *sd_dst = NULL;
228 1108 AVBufferRef *buf = *pbuf;
229
230
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1106 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
1108 if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
231 return NULL;
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
1108 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
233 av_frame_side_data_remove(sd, nb_sd, type);
234
4/6
✓ Branch 0 taken 1108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 797 times.
✓ Branch 3 taken 311 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 797 times.
1905 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
235 797 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
236 sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
237 } else
238 1108 sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
239
240
3/4
✓ Branch 0 taken 1108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1106 times.
✓ Branch 3 taken 2 times.
1108 if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
241 1106 *pbuf = NULL;
242
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))
243 av_buffer_unref(&buf);
244 1108 return sd_dst;
245 }
246
247 1515 int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
248 const AVFrameSideData *src, unsigned int flags)
249 {
250 const AVSideDataDescriptor *desc;
251 1515 AVBufferRef *buf = NULL;
252 1515 AVFrameSideData *sd_dst = NULL;
253 1515 int ret = AVERROR_BUG;
254
255
6/10
✓ Branch 0 taken 1515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1515 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1515 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 68 times.
✓ Branch 7 taken 1447 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 68 times.
1515 if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
256 return AVERROR(EINVAL);
257
258 1515 desc = av_frame_side_data_desc(src->type);
259
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 1281 times.
1515 if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
260 234 av_frame_side_data_remove(sd, nb_sd, src->type);
261
4/6
✓ Branch 0 taken 1515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1463 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1463 times.
2978 if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
262 1463 (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
263 AVDictionary *dict = NULL;
264
265 if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
266 return AVERROR(EEXIST);
267
268 ret = av_dict_copy(&dict, src->metadata, 0);
269 if (ret < 0)
270 return ret;
271
272 ret = av_buffer_replace(&sd_dst->buf, src->buf);
273 if (ret < 0) {
274 av_dict_free(&dict);
275 return ret;
276 }
277
278 av_dict_free(&sd_dst->metadata);
279 sd_dst->metadata = dict;
280 sd_dst->data = src->data;
281 sd_dst->size = src->size;
282 return 0;
283 }
284
285 1515 buf = av_buffer_ref(src->buf);
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1515 times.
1515 if (!buf)
287 return AVERROR(ENOMEM);
288
289 1515 sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
290 1515 src->data, src->size);
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1515 times.
1515 if (!sd_dst) {
292 av_buffer_unref(&buf);
293 return AVERROR(ENOMEM);
294 }
295
296 1515 ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1515 times.
1515 if (ret < 0) {
298 remove_side_data_by_entry(sd, nb_sd, sd_dst);
299 return ret;
300 }
301
302 1515 return 0;
303 }
304
305 2404830 const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
306 const int nb_sd,
307 enum AVFrameSideDataType type)
308 {
309
2/2
✓ Branch 0 taken 40919 times.
✓ Branch 1 taken 2402355 times.
2443274 for (int i = 0; i < nb_sd; i++) {
310
2/2
✓ Branch 0 taken 2475 times.
✓ Branch 1 taken 38444 times.
40919 if (sd[i]->type == type)
311 2475 return sd[i];
312 }
313 2402355 return NULL;
314 }
315