FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/frame.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 382 605 63.1%
Functions: 27 28 96.4%
Branches: 198 406 48.8%

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 "channel_layout.h"
20 #include "avassert.h"
21 #include "buffer.h"
22 #include "common.h"
23 #include "cpu.h"
24 #include "dict.h"
25 #include "frame.h"
26 #include "imgutils.h"
27 #include "mem.h"
28 #include "samplefmt.h"
29 #include "hwcontext.h"
30
31 #if FF_API_OLD_CHANNEL_LAYOUT
32 #define CHECK_CHANNELS_CONSISTENCY(frame) \
33 av_assert2(!(frame)->channel_layout || \
34 (frame)->channels == \
35 av_get_channel_layout_nb_channels((frame)->channel_layout))
36 #endif
37
38 13254125 static void get_frame_defaults(AVFrame *frame)
39 {
40 13254125 memset(frame, 0, sizeof(*frame));
41
42 13254125 frame->pts =
43 13254125 frame->pkt_dts = AV_NOPTS_VALUE;
44 13254125 frame->best_effort_timestamp = AV_NOPTS_VALUE;
45 13254125 frame->duration = 0;
46 #if FF_API_PKT_DURATION
47 FF_DISABLE_DEPRECATION_WARNINGS
48 13254125 frame->pkt_duration = 0;
49 FF_ENABLE_DEPRECATION_WARNINGS
50 #endif
51 #if FF_API_FRAME_PKT
52 FF_DISABLE_DEPRECATION_WARNINGS
53 13254125 frame->pkt_pos = -1;
54 13254125 frame->pkt_size = -1;
55 FF_ENABLE_DEPRECATION_WARNINGS
56 #endif
57 13254125 frame->time_base = (AVRational){ 0, 1 };
58 13254125 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
59 13254125 frame->format = -1; /* unknown */
60 13254125 frame->extended_data = frame->data;
61 13254125 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
62 13254125 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
63 13254125 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
64 13254125 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
65 13254125 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
66 13254125 frame->flags = 0;
67 13254125 }
68
69 101743 static void free_side_data(AVFrameSideData **ptr_sd)
70 {
71 101743 AVFrameSideData *sd = *ptr_sd;
72
73 101743 av_buffer_unref(&sd->buf);
74 101743 av_dict_free(&sd->metadata);
75 101743 av_freep(ptr_sd);
76 101743 }
77
78 8315274 static void wipe_side_data(AVFrame *frame)
79 {
80
2/2
✓ Branch 0 taken 101631 times.
✓ Branch 1 taken 8315274 times.
8416905 for (int i = 0; i < frame->nb_side_data; i++) {
81 101631 free_side_data(&frame->side_data[i]);
82 }
83 8315274 frame->nb_side_data = 0;
84
85 8315274 av_freep(&frame->side_data);
86 8315274 }
87
88 1283951 AVFrame *av_frame_alloc(void)
89 {
90 1283951 AVFrame *frame = av_malloc(sizeof(*frame));
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1283951 times.
1283951 if (!frame)
93 return NULL;
94
95 1283951 get_frame_defaults(frame);
96
97 1283951 return frame;
98 }
99
100 1392540 void av_frame_free(AVFrame **frame)
101 {
102
3/4
✓ Branch 0 taken 1392540 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108525 times.
✓ Branch 3 taken 1284015 times.
1392540 if (!frame || !*frame)
103 108525 return;
104
105 1284015 av_frame_unref(*frame);
106 1284015 av_freep(frame);
107 }
108
109 406 static int get_video_buffer(AVFrame *frame, int align)
110 {
111 406 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
112 int ret, padded_height, total_size;
113 406 int plane_padding = FFMAX(16 + 16/*STRIDE_ALIGN*/, align);
114 ptrdiff_t linesizes[4];
115 size_t sizes[4];
116
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
406 if (!desc)
118 return AVERROR(EINVAL);
119
120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 406 times.
406 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
121 return ret;
122
123
1/2
✓ Branch 0 taken 406 times.
✗ Branch 1 not taken.
406 if (!frame->linesize[0]) {
124
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 20 times.
406 if (align <= 0)
125 386 align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
126
127
1/2
✓ Branch 0 taken 918 times.
✗ Branch 1 not taken.
918 for (int i = 1; i <= align; i += i) {
128 918 ret = av_image_fill_linesizes(frame->linesize, frame->format,
129 918 FFALIGN(frame->width, i));
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 918 times.
918 if (ret < 0)
131 return ret;
132
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 512 times.
918 if (!(frame->linesize[0] & (align-1)))
133 406 break;
134 }
135
136
3/4
✓ Branch 0 taken 1014 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 608 times.
✓ Branch 3 taken 406 times.
1014 for (int i = 0; i < 4 && frame->linesize[i]; i++)
137 608 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
138 }
139
140
2/2
✓ Branch 0 taken 1624 times.
✓ Branch 1 taken 406 times.
2030 for (int i = 0; i < 4; i++)
141 1624 linesizes[i] = frame->linesize[i];
142
143 406 padded_height = FFALIGN(frame->height, 32);
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 406 times.
406 if ((ret = av_image_fill_plane_sizes(sizes, frame->format,
145 padded_height, linesizes)) < 0)
146 return ret;
147
148 406 total_size = 4*plane_padding;
149
2/2
✓ Branch 0 taken 1624 times.
✓ Branch 1 taken 406 times.
2030 for (int i = 0; i < 4; i++) {
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1624 times.
1624 if (sizes[i] > INT_MAX - total_size)
151 return AVERROR(EINVAL);
152 1624 total_size += sizes[i];
153 }
154
155 406 frame->buf[0] = av_buffer_alloc(total_size);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
406 if (!frame->buf[0]) {
157 ret = AVERROR(ENOMEM);
158 goto fail;
159 }
160
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
406 if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
162 406 frame->buf[0]->data, frame->linesize)) < 0)
163 goto fail;
164
165
2/2
✓ Branch 0 taken 1218 times.
✓ Branch 1 taken 406 times.
1624 for (int i = 1; i < 4; i++) {
166
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 1016 times.
1218 if (frame->data[i])
167 202 frame->data[i] += i * plane_padding;
168 }
169
170 406 frame->extended_data = frame->data;
171
172 406 return 0;
173 fail:
174 av_frame_unref(frame);
175 return ret;
176 }
177
178 19928 static int get_audio_buffer(AVFrame *frame, int align)
179 {
180 19928 int planar = av_sample_fmt_is_planar(frame->format);
181 int channels, planes;
182 int ret;
183
184 #if FF_API_OLD_CHANNEL_LAYOUT
185 FF_DISABLE_DEPRECATION_WARNINGS
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19928 times.
19928 if (!frame->ch_layout.nb_channels) {
187 if (frame->channel_layout) {
188 av_channel_layout_from_mask(&frame->ch_layout, frame->channel_layout);
189 } else {
190 frame->ch_layout.nb_channels = frame->channels;
191 frame->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
192 }
193 }
194 19928 frame->channels = frame->ch_layout.nb_channels;
195 39856 frame->channel_layout = frame->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
196
2/2
✓ Branch 0 taken 19899 times.
✓ Branch 1 taken 29 times.
19928 frame->ch_layout.u.mask : 0;
197 FF_ENABLE_DEPRECATION_WARNINGS
198 #endif
199 19928 channels = frame->ch_layout.nb_channels;
200
2/2
✓ Branch 0 taken 4701 times.
✓ Branch 1 taken 15227 times.
19928 planes = planar ? channels : 1;
201
1/2
✓ Branch 0 taken 19928 times.
✗ Branch 1 not taken.
19928 if (!frame->linesize[0]) {
202 19928 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
203 19928 frame->nb_samples, frame->format,
204 align);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19928 times.
19928 if (ret < 0)
206 return ret;
207 }
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19928 times.
19928 if (planes > AV_NUM_DATA_POINTERS) {
210 frame->extended_data = av_calloc(planes,
211 sizeof(*frame->extended_data));
212 frame->extended_buf = av_calloc(planes - AV_NUM_DATA_POINTERS,
213 sizeof(*frame->extended_buf));
214 if (!frame->extended_data || !frame->extended_buf) {
215 av_freep(&frame->extended_data);
216 av_freep(&frame->extended_buf);
217 return AVERROR(ENOMEM);
218 }
219 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
220 } else
221 19928 frame->extended_data = frame->data;
222
223
2/2
✓ Branch 0 taken 24316 times.
✓ Branch 1 taken 19928 times.
44244 for (int i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
224 24316 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24316 times.
24316 if (!frame->buf[i]) {
226 av_frame_unref(frame);
227 return AVERROR(ENOMEM);
228 }
229 24316 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
230 }
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19928 times.
19928 for (int i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
232 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
233 if (!frame->extended_buf[i]) {
234 av_frame_unref(frame);
235 return AVERROR(ENOMEM);
236 }
237 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
238 }
239 19928 return 0;
240
241 }
242
243 20334 int av_frame_get_buffer(AVFrame *frame, int align)
244 {
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20334 times.
20334 if (frame->format < 0)
246 return AVERROR(EINVAL);
247
248 FF_DISABLE_DEPRECATION_WARNINGS
249
3/4
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 19928 times.
✓ Branch 2 taken 406 times.
✗ Branch 3 not taken.
20334 if (frame->width > 0 && frame->height > 0)
250 406 return get_video_buffer(frame, align);
251
2/4
✓ Branch 0 taken 19928 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19928 times.
39856 else if (frame->nb_samples > 0 &&
252 19928 (av_channel_layout_check(&frame->ch_layout)
253 #if FF_API_OLD_CHANNEL_LAYOUT
254 || frame->channel_layout || frame->channels > 0
255 #endif
256 ))
257 19928 return get_audio_buffer(frame, align);
258 FF_ENABLE_DEPRECATION_WARNINGS
259
260 return AVERROR(EINVAL);
261 }
262
263 1266181 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
264 {
265 int ret;
266
267 #if FF_API_FRAME_KEY
268 FF_DISABLE_DEPRECATION_WARNINGS
269 1266181 dst->key_frame = src->key_frame;
270 FF_ENABLE_DEPRECATION_WARNINGS
271 #endif
272 1266181 dst->pict_type = src->pict_type;
273 1266181 dst->sample_aspect_ratio = src->sample_aspect_ratio;
274 1266181 dst->crop_top = src->crop_top;
275 1266181 dst->crop_bottom = src->crop_bottom;
276 1266181 dst->crop_left = src->crop_left;
277 1266181 dst->crop_right = src->crop_right;
278 1266181 dst->pts = src->pts;
279 1266181 dst->duration = src->duration;
280 1266181 dst->repeat_pict = src->repeat_pict;
281 #if FF_API_INTERLACED_FRAME
282 FF_DISABLE_DEPRECATION_WARNINGS
283 1266181 dst->interlaced_frame = src->interlaced_frame;
284 1266181 dst->top_field_first = src->top_field_first;
285 FF_ENABLE_DEPRECATION_WARNINGS
286 #endif
287 #if FF_API_PALETTE_HAS_CHANGED
288 FF_DISABLE_DEPRECATION_WARNINGS
289 1266181 dst->palette_has_changed = src->palette_has_changed;
290 FF_ENABLE_DEPRECATION_WARNINGS
291 #endif
292 1266181 dst->sample_rate = src->sample_rate;
293 1266181 dst->opaque = src->opaque;
294 1266181 dst->pkt_dts = src->pkt_dts;
295 #if FF_API_FRAME_PKT
296 FF_DISABLE_DEPRECATION_WARNINGS
297 1266181 dst->pkt_pos = src->pkt_pos;
298 1266181 dst->pkt_size = src->pkt_size;
299 FF_ENABLE_DEPRECATION_WARNINGS
300 #endif
301 #if FF_API_PKT_DURATION
302 FF_DISABLE_DEPRECATION_WARNINGS
303 1266181 dst->pkt_duration = src->pkt_duration;
304 FF_ENABLE_DEPRECATION_WARNINGS
305 #endif
306 1266181 dst->time_base = src->time_base;
307 #if FF_API_REORDERED_OPAQUE
308 FF_DISABLE_DEPRECATION_WARNINGS
309 1266181 dst->reordered_opaque = src->reordered_opaque;
310 FF_ENABLE_DEPRECATION_WARNINGS
311 #endif
312 1266181 dst->quality = src->quality;
313 1266181 dst->best_effort_timestamp = src->best_effort_timestamp;
314 #if FF_API_FRAME_PICTURE_NUMBER
315 FF_DISABLE_DEPRECATION_WARNINGS
316 1266181 dst->coded_picture_number = src->coded_picture_number;
317 1266181 dst->display_picture_number = src->display_picture_number;
318 FF_ENABLE_DEPRECATION_WARNINGS
319 #endif
320 1266181 dst->flags = src->flags;
321 1266181 dst->decode_error_flags = src->decode_error_flags;
322 1266181 dst->color_primaries = src->color_primaries;
323 1266181 dst->color_trc = src->color_trc;
324 1266181 dst->colorspace = src->colorspace;
325 1266181 dst->color_range = src->color_range;
326 1266181 dst->chroma_location = src->chroma_location;
327
328 1266181 av_dict_copy(&dst->metadata, src->metadata, 0);
329
330
2/2
✓ Branch 0 taken 85546 times.
✓ Branch 1 taken 1266181 times.
1351727 for (int i = 0; i < src->nb_side_data; i++) {
331 85546 const AVFrameSideData *sd_src = src->side_data[i];
332 AVFrameSideData *sd_dst;
333
2/2
✓ Branch 0 taken 19672 times.
✓ Branch 1 taken 65874 times.
85546 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
334
2/4
✓ Branch 0 taken 19672 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19672 times.
19672 && (src->width != dst->width || src->height != dst->height))
335 continue;
336
2/2
✓ Branch 0 taken 8354 times.
✓ Branch 1 taken 77192 times.
85546 if (force_copy) {
337 8354 sd_dst = av_frame_new_side_data(dst, sd_src->type,
338 8354 sd_src->size);
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8354 times.
8354 if (!sd_dst) {
340 wipe_side_data(dst);
341 return AVERROR(ENOMEM);
342 }
343 8354 memcpy(sd_dst->data, sd_src->data, sd_src->size);
344 } else {
345 77192 AVBufferRef *ref = av_buffer_ref(sd_src->buf);
346 77192 sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77192 times.
77192 if (!sd_dst) {
348 av_buffer_unref(&ref);
349 wipe_side_data(dst);
350 return AVERROR(ENOMEM);
351 }
352 }
353 85546 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
354 }
355
356 1266181 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
357 1266181 ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
358 1266181 return ret;
359 }
360
361 923520 int av_frame_ref(AVFrame *dst, const AVFrame *src)
362 {
363 923520 int ret = 0;
364
365 av_assert1(dst->width == 0 && dst->height == 0);
366 #if FF_API_OLD_CHANNEL_LAYOUT
367 FF_DISABLE_DEPRECATION_WARNINGS
368 av_assert1(dst->channels == 0);
369 FF_ENABLE_DEPRECATION_WARNINGS
370 #endif
371 av_assert1(dst->ch_layout.nb_channels == 0 &&
372 dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
373
374 923520 dst->format = src->format;
375 923520 dst->width = src->width;
376 923520 dst->height = src->height;
377 923520 dst->nb_samples = src->nb_samples;
378 #if FF_API_OLD_CHANNEL_LAYOUT
379 FF_DISABLE_DEPRECATION_WARNINGS
380 923520 dst->channels = src->channels;
381 923520 dst->channel_layout = src->channel_layout;
382
2/2
✓ Branch 1 taken 527307 times.
✓ Branch 2 taken 396213 times.
923520 if (!av_channel_layout_check(&src->ch_layout)) {
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 527307 times.
527307 if (src->channel_layout)
384 av_channel_layout_from_mask(&dst->ch_layout, src->channel_layout);
385 else {
386 527307 dst->ch_layout.nb_channels = src->channels;
387 527307 dst->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
388 }
389 }
390 FF_ENABLE_DEPRECATION_WARNINGS
391 #endif
392
393 923520 ret = frame_copy_props(dst, src, 0);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923520 times.
923520 if (ret < 0)
395 goto fail;
396
397 // this check is needed only until FF_API_OLD_CHANNEL_LAYOUT is out
398
2/2
✓ Branch 1 taken 396213 times.
✓ Branch 2 taken 527307 times.
923520 if (av_channel_layout_check(&src->ch_layout)) {
399 396213 ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396213 times.
396213 if (ret < 0)
401 goto fail;
402 }
403
404 /* duplicate the frame data if it's not refcounted */
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923520 times.
923520 if (!src->buf[0]) {
406 ret = av_frame_get_buffer(dst, 0);
407 if (ret < 0)
408 goto fail;
409
410 ret = av_frame_copy(dst, src);
411 if (ret < 0)
412 goto fail;
413
414 return 0;
415 }
416
417 /* ref the buffers */
418
2/2
✓ Branch 0 taken 7388160 times.
✓ Branch 1 taken 923520 times.
8311680 for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
419
2/2
✓ Branch 0 taken 5533491 times.
✓ Branch 1 taken 1854669 times.
7388160 if (!src->buf[i])
420 5533491 continue;
421 1854669 dst->buf[i] = av_buffer_ref(src->buf[i]);
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1854669 times.
1854669 if (!dst->buf[i]) {
423 ret = AVERROR(ENOMEM);
424 goto fail;
425 }
426 }
427
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923520 times.
923520 if (src->extended_buf) {
429 dst->extended_buf = av_calloc(src->nb_extended_buf,
430 sizeof(*dst->extended_buf));
431 if (!dst->extended_buf) {
432 ret = AVERROR(ENOMEM);
433 goto fail;
434 }
435 dst->nb_extended_buf = src->nb_extended_buf;
436
437 for (int i = 0; i < src->nb_extended_buf; i++) {
438 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
439 if (!dst->extended_buf[i]) {
440 ret = AVERROR(ENOMEM);
441 goto fail;
442 }
443 }
444 }
445
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923520 times.
923520 if (src->hw_frames_ctx) {
447 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
448 if (!dst->hw_frames_ctx) {
449 ret = AVERROR(ENOMEM);
450 goto fail;
451 }
452 }
453
454 /* duplicate extended data */
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923520 times.
923520 if (src->extended_data != src->data) {
456 int ch = dst->ch_layout.nb_channels;
457
458 if (!ch) {
459 ret = AVERROR(EINVAL);
460 goto fail;
461 }
462
463 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
464 if (!dst->extended_data) {
465 ret = AVERROR(ENOMEM);
466 goto fail;
467 }
468 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
469 } else
470 923520 dst->extended_data = dst->data;
471
472 923520 memcpy(dst->data, src->data, sizeof(src->data));
473 923520 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
474
475 923520 return 0;
476
477 fail:
478 av_frame_unref(dst);
479 return ret;
480 }
481
482 4077 int av_frame_replace(AVFrame *dst, const AVFrame *src)
483 {
484 4077 int ret = 0;
485
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (dst == src)
487 return AVERROR(EINVAL);
488
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (!src->buf[0]) {
490 av_frame_unref(dst);
491
492 /* duplicate the frame data if it's not refcounted */
493 if ( src->data[0] || src->data[1]
494 || src->data[2] || src->data[3])
495 return av_frame_ref(dst, src);
496
497 ret = frame_copy_props(dst, src, 0);
498 if (ret < 0)
499 goto fail;
500 }
501
502 4077 dst->format = src->format;
503 4077 dst->width = src->width;
504 4077 dst->height = src->height;
505 4077 dst->nb_samples = src->nb_samples;
506 #if FF_API_OLD_CHANNEL_LAYOUT
507 FF_DISABLE_DEPRECATION_WARNINGS
508 4077 dst->channels = src->channels;
509 4077 dst->channel_layout = src->channel_layout;
510
1/2
✓ Branch 1 taken 4077 times.
✗ Branch 2 not taken.
4077 if (!av_channel_layout_check(&src->ch_layout)) {
511 4077 av_channel_layout_uninit(&dst->ch_layout);
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (src->channel_layout)
513 av_channel_layout_from_mask(&dst->ch_layout, src->channel_layout);
514 else {
515 4077 dst->ch_layout.nb_channels = src->channels;
516 4077 dst->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
517 }
518 } else {
519 #endif
520 ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
521 if (ret < 0)
522 goto fail;
523 #if FF_API_OLD_CHANNEL_LAYOUT
524 }
525 FF_ENABLE_DEPRECATION_WARNINGS
526 #endif
527
528 4077 wipe_side_data(dst);
529 4077 av_dict_free(&dst->metadata);
530 4077 ret = frame_copy_props(dst, src, 0);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (ret < 0)
532 goto fail;
533
534 /* replace the buffers */
535
2/2
✓ Branch 0 taken 32616 times.
✓ Branch 1 taken 4077 times.
36693 for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
536 32616 ret = av_buffer_replace(&dst->buf[i], src->buf[i]);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32616 times.
32616 if (ret < 0)
538 goto fail;
539 }
540
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (src->extended_buf) {
542 if (dst->nb_extended_buf != src->nb_extended_buf) {
543 int nb_extended_buf = FFMIN(dst->nb_extended_buf, src->nb_extended_buf);
544 void *tmp;
545
546 for (int i = nb_extended_buf; i < dst->nb_extended_buf; i++)
547 av_buffer_unref(&dst->extended_buf[i]);
548
549 tmp = av_realloc_array(dst->extended_buf, sizeof(*dst->extended_buf),
550 src->nb_extended_buf);
551 if (!tmp) {
552 ret = AVERROR(ENOMEM);
553 goto fail;
554 }
555 dst->extended_buf = tmp;
556 dst->nb_extended_buf = src->nb_extended_buf;
557
558 memset(&dst->extended_buf[nb_extended_buf], 0,
559 (src->nb_extended_buf - nb_extended_buf) * sizeof(*dst->extended_buf));
560 }
561
562 for (int i = 0; i < src->nb_extended_buf; i++) {
563 ret = av_buffer_replace(&dst->extended_buf[i], src->extended_buf[i]);
564 if (ret < 0)
565 goto fail;
566 }
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 } else if (dst->extended_buf) {
568 for (int i = 0; i < dst->nb_extended_buf; i++)
569 av_buffer_unref(&dst->extended_buf[i]);
570 av_freep(&dst->extended_buf);
571 }
572
573 4077 ret = av_buffer_replace(&dst->hw_frames_ctx, src->hw_frames_ctx);
574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (ret < 0)
575 goto fail;
576
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (dst->extended_data != dst->data)
578 av_freep(&dst->extended_data);
579
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4077 times.
4077 if (src->extended_data != src->data) {
581 int ch = dst->ch_layout.nb_channels;
582
583 if (!ch) {
584 ret = AVERROR(EINVAL);
585 goto fail;
586 }
587
588 if (ch > SIZE_MAX / sizeof(*dst->extended_data))
589 goto fail;
590
591 dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch);
592 if (!dst->extended_data) {
593 ret = AVERROR(ENOMEM);
594 goto fail;
595 }
596 } else
597 4077 dst->extended_data = dst->data;
598
599 4077 memcpy(dst->data, src->data, sizeof(src->data));
600 4077 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
601
602 4077 return 0;
603
604 fail:
605 av_frame_unref(dst);
606 return ret;
607 }
608
609 18997 AVFrame *av_frame_clone(const AVFrame *src)
610 {
611 18997 AVFrame *ret = av_frame_alloc();
612
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18997 times.
18997 if (!ret)
614 return NULL;
615
616
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18997 times.
18997 if (av_frame_ref(ret, src) < 0)
617 av_frame_free(&ret);
618
619 18997 return ret;
620 }
621
622 8311201 void av_frame_unref(AVFrame *frame)
623 {
624
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8311197 times.
8311201 if (!frame)
625 4 return;
626
627 8311197 wipe_side_data(frame);
628
629
2/2
✓ Branch 0 taken 66489576 times.
✓ Branch 1 taken 8311197 times.
74800773 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
630 66489576 av_buffer_unref(&frame->buf[i]);
631
2/2
✓ Branch 0 taken 9924 times.
✓ Branch 1 taken 8311197 times.
8321121 for (int i = 0; i < frame->nb_extended_buf; i++)
632 9924 av_buffer_unref(&frame->extended_buf[i]);
633 8311197 av_freep(&frame->extended_buf);
634 8311197 av_dict_free(&frame->metadata);
635
636 8311197 av_buffer_unref(&frame->hw_frames_ctx);
637
638 8311197 av_buffer_unref(&frame->opaque_ref);
639 8311197 av_buffer_unref(&frame->private_ref);
640
641
2/2
✓ Branch 0 taken 3135 times.
✓ Branch 1 taken 8308062 times.
8311197 if (frame->extended_data != frame->data)
642 3135 av_freep(&frame->extended_data);
643
644 8311197 av_channel_layout_uninit(&frame->ch_layout);
645
646 8311197 get_frame_defaults(frame);
647 }
648
649 3658977 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
650 {
651 av_assert1(dst->width == 0 && dst->height == 0);
652 #if FF_API_OLD_CHANNEL_LAYOUT
653 FF_DISABLE_DEPRECATION_WARNINGS
654 av_assert1(dst->channels == 0);
655 FF_ENABLE_DEPRECATION_WARNINGS
656 #endif
657 av_assert1(dst->ch_layout.nb_channels == 0 &&
658 dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
659
660 3658977 *dst = *src;
661
1/2
✓ Branch 0 taken 3658977 times.
✗ Branch 1 not taken.
3658977 if (src->extended_data == src->data)
662 3658977 dst->extended_data = dst->data;
663 3658977 get_frame_defaults(src);
664 3658977 }
665
666 25553 int av_frame_is_writable(AVFrame *frame)
667 {
668 25553 int ret = 1;
669
670 /* assume non-refcounted frames are not writable */
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25553 times.
25553 if (!frame->buf[0])
672 return 0;
673
674
2/2
✓ Branch 0 taken 204424 times.
✓ Branch 1 taken 25553 times.
229977 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
675
2/2
✓ Branch 0 taken 70023 times.
✓ Branch 1 taken 134401 times.
204424 if (frame->buf[i])
676 70023 ret &= !!av_buffer_is_writable(frame->buf[i]);
677
2/2
✓ Branch 0 taken 9924 times.
✓ Branch 1 taken 25553 times.
35477 for (int i = 0; i < frame->nb_extended_buf; i++)
678 9924 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
679
680 25553 return ret;
681 }
682
683 3200 int av_frame_make_writable(AVFrame *frame)
684 {
685 AVFrame tmp;
686 int ret;
687
688
1/2
✓ Branch 1 taken 3200 times.
✗ Branch 2 not taken.
3200 if (av_frame_is_writable(frame))
689 3200 return 0;
690
691 memset(&tmp, 0, sizeof(tmp));
692 tmp.format = frame->format;
693 tmp.width = frame->width;
694 tmp.height = frame->height;
695 #if FF_API_OLD_CHANNEL_LAYOUT
696 FF_DISABLE_DEPRECATION_WARNINGS
697 tmp.channels = frame->channels;
698 tmp.channel_layout = frame->channel_layout;
699 FF_ENABLE_DEPRECATION_WARNINGS
700 #endif
701 tmp.nb_samples = frame->nb_samples;
702 ret = av_channel_layout_copy(&tmp.ch_layout, &frame->ch_layout);
703 if (ret < 0) {
704 av_frame_unref(&tmp);
705 return ret;
706 }
707
708 if (frame->hw_frames_ctx)
709 ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
710 else
711 ret = av_frame_get_buffer(&tmp, 0);
712 if (ret < 0)
713 return ret;
714
715 ret = av_frame_copy(&tmp, frame);
716 if (ret < 0) {
717 av_frame_unref(&tmp);
718 return ret;
719 }
720
721 ret = av_frame_copy_props(&tmp, frame);
722 if (ret < 0) {
723 av_frame_unref(&tmp);
724 return ret;
725 }
726
727 av_frame_unref(frame);
728
729 *frame = tmp;
730 if (tmp.data == tmp.extended_data)
731 frame->extended_data = frame->data;
732
733 return 0;
734 }
735
736 338584 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
737 {
738 338584 return frame_copy_props(dst, src, 1);
739 }
740
741 6375 AVBufferRef *av_frame_get_plane_buffer(const AVFrame *frame, int plane)
742 {
743 uint8_t *data;
744 int planes;
745
746
2/2
✓ Branch 0 taken 1295 times.
✓ Branch 1 taken 5080 times.
6375 if (frame->nb_samples) {
747 1295 int channels = frame->ch_layout.nb_channels;
748
749 #if FF_API_OLD_CHANNEL_LAYOUT
750 FF_DISABLE_DEPRECATION_WARNINGS
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1295 times.
1295 if (!channels) {
752 channels = frame->channels;
753 CHECK_CHANNELS_CONSISTENCY(frame);
754 }
755 FF_ENABLE_DEPRECATION_WARNINGS
756 #endif
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1295 times.
1295 if (!channels)
758 return NULL;
759
1/2
✓ Branch 1 taken 1295 times.
✗ Branch 2 not taken.
1295 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
760 } else
761 5080 planes = 4;
762
763
3/6
✓ Branch 0 taken 6375 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6375 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6375 times.
6375 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
764 return NULL;
765 6375 data = frame->extended_data[plane];
766
767
2/4
✓ Branch 0 taken 14543 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14543 times.
✗ Branch 3 not taken.
14543 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
768 14543 AVBufferRef *buf = frame->buf[i];
769
4/4
✓ Branch 0 taken 13190 times.
✓ Branch 1 taken 1353 times.
✓ Branch 2 taken 6375 times.
✓ Branch 3 taken 6815 times.
14543 if (data >= buf->data && data < buf->data + buf->size)
770 6375 return buf;
771 }
772 for (int i = 0; i < frame->nb_extended_buf; i++) {
773 AVBufferRef *buf = frame->extended_buf[i];
774 if (data >= buf->data && data < buf->data + buf->size)
775 return buf;
776 }
777 return NULL;
778 }
779
780 101743 AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
781 enum AVFrameSideDataType type,
782 AVBufferRef *buf)
783 {
784 AVFrameSideData *ret, **tmp;
785
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101743 times.
101743 if (!buf)
787 return NULL;
788
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101743 times.
101743 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
790 return NULL;
791
792 101743 tmp = av_realloc(frame->side_data,
793 101743 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101743 times.
101743 if (!tmp)
795 return NULL;
796 101743 frame->side_data = tmp;
797
798 101743 ret = av_mallocz(sizeof(*ret));
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101743 times.
101743 if (!ret)
800 return NULL;
801
802 101743 ret->buf = buf;
803 101743 ret->data = ret->buf->data;
804 101743 ret->size = buf->size;
805 101743 ret->type = type;
806
807 101743 frame->side_data[frame->nb_side_data++] = ret;
808
809 101743 return ret;
810 }
811
812 23446 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
813 enum AVFrameSideDataType type,
814 size_t size)
815 {
816 AVFrameSideData *ret;
817 23446 AVBufferRef *buf = av_buffer_alloc(size);
818 23446 ret = av_frame_new_side_data_from_buf(frame, type, buf);
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23446 times.
23446 if (!ret)
820 av_buffer_unref(&buf);
821 23446 return ret;
822 }
823
824 1071257 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
825 enum AVFrameSideDataType type)
826 {
827
2/2
✓ Branch 0 taken 33028 times.
✓ Branch 1 taken 1070075 times.
1103103 for (int i = 0; i < frame->nb_side_data; i++) {
828
2/2
✓ Branch 0 taken 1182 times.
✓ Branch 1 taken 31846 times.
33028 if (frame->side_data[i]->type == type)
829 1182 return frame->side_data[i];
830 }
831 1070075 return NULL;
832 }
833
834 9415 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
835 {
836 int planes;
837
838
1/2
✓ Branch 0 taken 9415 times.
✗ Branch 1 not taken.
9415 if (dst->width < src->width ||
839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9415 times.
9415 dst->height < src->height)
840 return AVERROR(EINVAL);
841
842
2/4
✓ Branch 0 taken 9415 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9415 times.
9415 if (src->hw_frames_ctx || dst->hw_frames_ctx)
843 return av_hwframe_transfer_data(dst, src, 0);
844
845 9415 planes = av_pix_fmt_count_planes(dst->format);
846
2/2
✓ Branch 0 taken 15733 times.
✓ Branch 1 taken 9415 times.
25148 for (int i = 0; i < planes; i++)
847
2/4
✓ Branch 0 taken 15733 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15733 times.
15733 if (!dst->data[i] || !src->data[i])
848 return AVERROR(EINVAL);
849
850 9415 av_image_copy2(dst->data, dst->linesize,
851 9415 src->data, src->linesize,
852 9415 dst->format, src->width, src->height);
853
854 9415 return 0;
855 }
856
857 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
858 {
859 int planar = av_sample_fmt_is_planar(dst->format);
860 int channels = dst->ch_layout.nb_channels;
861 int planes = planar ? channels : 1;
862
863 #if FF_API_OLD_CHANNEL_LAYOUT
864 FF_DISABLE_DEPRECATION_WARNINGS
865 if (!channels || !src->ch_layout.nb_channels) {
866 if (dst->channels != src->channels ||
867 dst->channel_layout != src->channel_layout)
868 return AVERROR(EINVAL);
869 CHECK_CHANNELS_CONSISTENCY(src);
870 }
871 if (!channels) {
872 channels = dst->channels;
873 planes = planar ? channels : 1;
874 }
875 FF_ENABLE_DEPRECATION_WARNINGS
876 #endif
877
878 if (dst->nb_samples != src->nb_samples ||
879 #if FF_API_OLD_CHANNEL_LAYOUT
880 (av_channel_layout_check(&dst->ch_layout) &&
881 av_channel_layout_check(&src->ch_layout) &&
882 #endif
883 av_channel_layout_compare(&dst->ch_layout, &src->ch_layout))
884 #if FF_API_OLD_CHANNEL_LAYOUT
885 )
886 #endif
887 return AVERROR(EINVAL);
888
889 for (int i = 0; i < planes; i++)
890 if (!dst->extended_data[i] || !src->extended_data[i])
891 return AVERROR(EINVAL);
892
893 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
894 dst->nb_samples, channels, dst->format);
895
896 return 0;
897 }
898
899 9415 int av_frame_copy(AVFrame *dst, const AVFrame *src)
900 {
901
2/4
✓ Branch 0 taken 9415 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9415 times.
9415 if (dst->format != src->format || dst->format < 0)
902 return AVERROR(EINVAL);
903
904 FF_DISABLE_DEPRECATION_WARNINGS
905
2/4
✓ Branch 0 taken 9415 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9415 times.
✗ Branch 3 not taken.
9415 if (dst->width > 0 && dst->height > 0)
906 9415 return frame_copy_video(dst, src);
907 else if (dst->nb_samples > 0 &&
908 (av_channel_layout_check(&dst->ch_layout)
909 #if FF_API_OLD_CHANNEL_LAYOUT
910 || dst->channels > 0
911 #endif
912 ))
913 return frame_copy_audio(dst, src);
914 FF_ENABLE_DEPRECATION_WARNINGS
915
916 return AVERROR(EINVAL);
917 }
918
919 341921 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
920 {
921
2/2
✓ Branch 0 taken 8569 times.
✓ Branch 1 taken 341921 times.
350490 for (int i = frame->nb_side_data - 1; i >= 0; i--) {
922 8569 AVFrameSideData *sd = frame->side_data[i];
923
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 8457 times.
8569 if (sd->type == type) {
924 112 free_side_data(&frame->side_data[i]);
925 112 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
926 112 frame->nb_side_data--;
927 }
928 }
929 341921 }
930
931 402 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
932 {
933
13/28
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 132 times.
✓ Branch 7 taken 64 times.
✓ Branch 8 taken 36 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 18 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 137 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 24 taken 2 times.
✓ Branch 25 taken 2 times.
✓ Branch 26 taken 2 times.
✗ Branch 27 not taken.
402 switch(type) {
934 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
935 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
936 case AV_FRAME_DATA_STEREO3D: return "Stereo 3D";
937 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
938 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
939 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
940 132 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
941 64 case AV_FRAME_DATA_AFD: return "Active format description";
942 36 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
943 case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
944 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
945 2 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
946 2 case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
947 case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
948 18 case AV_FRAME_DATA_S12M_TIMECODE: return "SMPTE 12-1 timecode";
949 case AV_FRAME_DATA_SPHERICAL: return "Spherical Mapping";
950 3 case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
951 1 case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)";
952 1 case AV_FRAME_DATA_DYNAMIC_HDR_VIVID: return "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)";
953 case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest";
954 case AV_FRAME_DATA_VIDEO_ENC_PARAMS: return "Video encoding parameters";
955 137 case AV_FRAME_DATA_SEI_UNREGISTERED: return "H.26[45] User Data Unregistered SEI message";
956 case AV_FRAME_DATA_FILM_GRAIN_PARAMS: return "Film grain parameters";
957 case AV_FRAME_DATA_DETECTION_BBOXES: return "Bounding boxes for object detection and classification";
958 2 case AV_FRAME_DATA_DOVI_RPU_BUFFER: return "Dolby Vision RPU Data";
959 2 case AV_FRAME_DATA_DOVI_METADATA: return "Dolby Vision Metadata";
960 2 case AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT: return "Ambient viewing environment";
961 }
962 return NULL;
963 }
964
965 136651 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
966 const AVPixFmtDescriptor *desc)
967 {
968
2/2
✓ Branch 0 taken 385215 times.
✓ Branch 1 taken 131920 times.
517135 for (int i = 0; frame->data[i]; i++) {
969 385215 const AVComponentDescriptor *comp = NULL;
970
4/4
✓ Branch 0 taken 259013 times.
✓ Branch 1 taken 126202 times.
✓ Branch 2 taken 121471 times.
✓ Branch 3 taken 137542 times.
385215 int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
971
4/4
✓ Branch 0 taken 259013 times.
✓ Branch 1 taken 126202 times.
✓ Branch 2 taken 121471 times.
✓ Branch 3 taken 137542 times.
385215 int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
972
973
4/4
✓ Branch 0 taken 9462 times.
✓ Branch 1 taken 375753 times.
✓ Branch 2 taken 4731 times.
✓ Branch 3 taken 4731 times.
385215 if (desc->flags & AV_PIX_FMT_FLAG_PAL && i == 1) {
974 4731 offsets[i] = 0;
975 4731 break;
976 }
977
978 /* find any component descriptor for this plane */
979
1/2
✓ Branch 0 taken 747570 times.
✗ Branch 1 not taken.
747570 for (int j = 0; j < desc->nb_components; j++) {
980
2/2
✓ Branch 0 taken 380484 times.
✓ Branch 1 taken 367086 times.
747570 if (desc->comp[j].plane == i) {
981 380484 comp = &desc->comp[j];
982 380484 break;
983 }
984 }
985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380484 times.
380484 if (!comp)
986 return AVERROR_BUG;
987
988 380484 offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
989 380484 (frame->crop_left >> shift_x) * comp->step;
990 }
991
992 136651 return 0;
993 }
994
995 136962 int av_frame_apply_cropping(AVFrame *frame, int flags)
996 {
997 const AVPixFmtDescriptor *desc;
998 size_t offsets[4];
999
1000
2/4
✓ Branch 0 taken 136962 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 136962 times.
136962 if (!(frame->width > 0 && frame->height > 0))
1001 return AVERROR(EINVAL);
1002
1003
1/2
✓ Branch 0 taken 136962 times.
✗ Branch 1 not taken.
136962 if (frame->crop_left >= INT_MAX - frame->crop_right ||
1004
1/2
✓ Branch 0 taken 136962 times.
✗ Branch 1 not taken.
136962 frame->crop_top >= INT_MAX - frame->crop_bottom ||
1005
1/2
✓ Branch 0 taken 136962 times.
✗ Branch 1 not taken.
136962 (frame->crop_left + frame->crop_right) >= frame->width ||
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136962 times.
136962 (frame->crop_top + frame->crop_bottom) >= frame->height)
1007 return AVERROR(ERANGE);
1008
1009 136962 desc = av_pix_fmt_desc_get(frame->format);
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136962 times.
136962 if (!desc)
1011 return AVERROR_BUG;
1012
1013 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
1014 * formats cannot be easily handled here either (and corresponding decoders
1015 * should not export any cropping anyway), so do the same for those as well.
1016 * */
1017
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 136644 times.
136962 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
1018 318 frame->width -= frame->crop_right;
1019 318 frame->height -= frame->crop_bottom;
1020 318 frame->crop_right = 0;
1021 318 frame->crop_bottom = 0;
1022 318 return 0;
1023 }
1024
1025 /* calculate the offsets for each plane */
1026 136644 calc_cropping_offsets(offsets, frame, desc);
1027
1028 /* adjust the offsets to avoid breaking alignment */
1029
2/2
✓ Branch 0 taken 126838 times.
✓ Branch 1 taken 9806 times.
136644 if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
1030
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 126821 times.
126838 int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
1031 126838 int min_log2_align = INT_MAX;
1032
1033
2/2
✓ Branch 0 taken 355904 times.
✓ Branch 1 taken 126838 times.
482742 for (int i = 0; frame->data[i]; i++) {
1034
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 355793 times.
355904 int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
1035 355904 min_log2_align = FFMIN(log2_align, min_log2_align);
1036 }
1037
1038 /* we assume, and it should always be true, that the data alignment is
1039 * related to the cropping alignment by a constant power-of-2 factor */
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126838 times.
126838 if (log2_crop_align < min_log2_align)
1041 return AVERROR_BUG;
1042
1043
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 126831 times.
126838 if (min_log2_align < 5) {
1044 7 frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
1045 7 calc_cropping_offsets(offsets, frame, desc);
1046 }
1047 }
1048
1049
2/2
✓ Branch 0 taken 385194 times.
✓ Branch 1 taken 136644 times.
521838 for (int i = 0; frame->data[i]; i++)
1050 385194 frame->data[i] += offsets[i];
1051
1052 136644 frame->width -= (frame->crop_left + frame->crop_right);
1053 136644 frame->height -= (frame->crop_top + frame->crop_bottom);
1054 136644 frame->crop_left = 0;
1055 136644 frame->crop_right = 0;
1056 136644 frame->crop_top = 0;
1057 136644 frame->crop_bottom = 0;
1058
1059 136644 return 0;
1060 }
1061