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