Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavutil/frame.c
Line
Count
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 "dict.h"
23
#include "frame.h"
24
#include "imgutils.h"
25
#include "mem.h"
26
#include "refstruct.h"
27
#include "samplefmt.h"
28
#include "side_data.h"
29
#include "hwcontext.h"
30
31
static void get_frame_defaults(AVFrame *frame)
32
875M
{
33
875M
    memset(frame, 0, sizeof(*frame));
34
35
875M
    frame->pts                   =
36
875M
    frame->pkt_dts               = AV_NOPTS_VALUE;
37
875M
    frame->best_effort_timestamp = AV_NOPTS_VALUE;
38
875M
    frame->duration            = 0;
39
875M
    frame->time_base           = (AVRational){ 0, 1 };
40
875M
    frame->sample_aspect_ratio = (AVRational){ 0, 1 };
41
875M
    frame->format              = -1; /* unknown */
42
875M
    frame->extended_data       = frame->data;
43
875M
    frame->color_primaries     = AVCOL_PRI_UNSPECIFIED;
44
875M
    frame->color_trc           = AVCOL_TRC_UNSPECIFIED;
45
875M
    frame->colorspace          = AVCOL_SPC_UNSPECIFIED;
46
875M
    frame->color_range         = AVCOL_RANGE_UNSPECIFIED;
47
875M
    frame->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
48
875M
    frame->alpha_mode          = AVALPHA_MODE_UNSPECIFIED;
49
875M
    frame->flags               = 0;
50
875M
}
51
52
AVFrame *av_frame_alloc(void)
53
176M
{
54
176M
    AVFrame *frame = av_malloc(sizeof(*frame));
55
56
176M
    if (!frame)
57
0
        return NULL;
58
59
176M
    get_frame_defaults(frame);
60
61
176M
    return frame;
62
176M
}
63
64
void av_frame_free(AVFrame **frame)
65
179M
{
66
179M
    if (!frame || !*frame)
67
2.79M
        return;
68
69
176M
    av_frame_unref(*frame);
70
176M
    av_freep(frame);
71
176M
}
72
73
976k
#define ALIGN (HAVE_SIMD_ALIGN_64 ? 64 : 32)
74
75
static int get_video_buffer(AVFrame *frame, int align)
76
1.08M
{
77
1.08M
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
78
1.08M
    int ret, padded_height;
79
1.08M
    int plane_padding;
80
1.08M
    ptrdiff_t linesizes[4];
81
1.08M
    size_t total_size, sizes[4];
82
83
1.08M
    if (!desc)
84
0
        return AVERROR(EINVAL);
85
86
1.08M
    if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
87
0
        return ret;
88
89
1.08M
    if (align <= 0)
90
976k
        align = ALIGN;
91
1.08M
    plane_padding = FFMAX(ALIGN, align);
92
93
1.08M
    if (!frame->linesize[0]) {
94
392k
        for (int i = 1; i <= align; i += i) {
95
390k
            ret = av_image_fill_linesizes(frame->linesize, frame->format,
96
390k
                                          FFALIGN(frame->width, i));
97
390k
            if (ret < 0)
98
0
                return ret;
99
390k
            if (!(frame->linesize[0] & (align-1)))
100
187k
                break;
101
390k
        }
102
103
461k
        for (int i = 0; i < 4 && frame->linesize[i]; i++)
104
271k
            frame->linesize[i] = FFALIGN(frame->linesize[i], align);
105
189k
    }
106
107
5.41M
    for (int i = 0; i < 4; i++)
108
4.32M
        linesizes[i] = frame->linesize[i];
109
110
1.08M
    padded_height = FFALIGN(frame->height, 32);
111
1.08M
    if ((ret = av_image_fill_plane_sizes(sizes, frame->format,
112
1.08M
                                         padded_height, linesizes)) < 0)
113
0
        return ret;
114
115
1.08M
    total_size = 4 * plane_padding + 4 * align;
116
5.41M
    for (int i = 0; i < 4; i++) {
117
4.32M
        if (sizes[i] > SIZE_MAX - total_size)
118
0
            return AVERROR(EINVAL);
119
4.32M
        total_size += sizes[i];
120
4.32M
    }
121
122
1.08M
    frame->buf[0] = av_buffer_alloc(total_size);
123
1.08M
    if (!frame->buf[0]) {
124
0
        ret = AVERROR(ENOMEM);
125
0
        goto fail;
126
0
    }
127
128
1.08M
    if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
129
1.08M
                                      frame->buf[0]->data, frame->linesize)) < 0)
130
0
        goto fail;
131
132
4.32M
    for (int i = 1; i < 4; i++) {
133
3.24M
        if (frame->data[i])
134
980k
            frame->data[i] += i * plane_padding;
135
3.24M
        frame->data[i] = (uint8_t *)FFALIGN((uintptr_t)frame->data[i], align);
136
3.24M
    }
137
138
1.08M
    frame->extended_data = frame->data;
139
140
1.08M
    return 0;
141
0
fail:
142
0
    av_frame_unref(frame);
143
0
    return ret;
144
1.08M
}
145
146
static int get_audio_buffer(AVFrame *frame, int align)
147
0
{
148
0
    int planar   = av_sample_fmt_is_planar(frame->format);
149
0
    int channels, planes;
150
0
    size_t size;
151
0
    int ret;
152
153
0
    channels = frame->ch_layout.nb_channels;
154
0
    planes   = planar ? channels : 1;
155
0
    if (!frame->linesize[0]) {
156
0
        ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
157
0
                                         frame->nb_samples, frame->format,
158
0
                                         align);
159
0
        if (ret < 0)
160
0
            return ret;
161
0
    }
162
163
0
    if (align <= 0)
164
0
        align = ALIGN;
165
166
0
    if (planes > AV_NUM_DATA_POINTERS) {
167
0
        frame->extended_data = av_calloc(planes,
168
0
                                          sizeof(*frame->extended_data));
169
0
        frame->extended_buf  = av_calloc(planes - AV_NUM_DATA_POINTERS,
170
0
                                          sizeof(*frame->extended_buf));
171
0
        if (!frame->extended_data || !frame->extended_buf) {
172
0
            av_freep(&frame->extended_data);
173
0
            av_freep(&frame->extended_buf);
174
0
            return AVERROR(ENOMEM);
175
0
        }
176
0
        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
177
0
    } else
178
0
        frame->extended_data = frame->data;
179
180
0
    if (frame->linesize[0] > SIZE_MAX - align)
181
0
        return AVERROR(EINVAL);
182
0
    size = frame->linesize[0] + (size_t)align;
183
184
0
    for (int i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
185
0
        frame->buf[i] = av_buffer_alloc(size);
186
0
        if (!frame->buf[i]) {
187
0
            av_frame_unref(frame);
188
0
            return AVERROR(ENOMEM);
189
0
        }
190
0
        frame->extended_data[i] = frame->data[i] =
191
0
            (uint8_t *)FFALIGN((uintptr_t)frame->buf[i]->data, align);
192
0
    }
193
0
    for (int i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
194
0
        frame->extended_buf[i] = av_buffer_alloc(size);
195
0
        if (!frame->extended_buf[i]) {
196
0
            av_frame_unref(frame);
197
0
            return AVERROR(ENOMEM);
198
0
        }
199
0
        frame->extended_data[i + AV_NUM_DATA_POINTERS] =
200
0
            (uint8_t *)FFALIGN((uintptr_t)frame->extended_buf[i]->data, align);
201
0
    }
202
0
    return 0;
203
204
0
}
205
206
int av_frame_get_buffer(AVFrame *frame, int align)
207
1.08M
{
208
1.08M
    if (frame->format < 0)
209
326
        return AVERROR(EINVAL);
210
211
1.08M
    if (frame->width > 0 && frame->height > 0)
212
1.08M
        return get_video_buffer(frame, align);
213
0
    else if (frame->nb_samples > 0 &&
214
0
             (av_channel_layout_check(&frame->ch_layout)))
215
0
        return get_audio_buffer(frame, align);
216
217
0
    return AVERROR(EINVAL);
218
1.08M
}
219
220
static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
221
19.4M
{
222
19.4M
    dst->pict_type              = src->pict_type;
223
19.4M
    dst->sample_aspect_ratio    = src->sample_aspect_ratio;
224
19.4M
    dst->crop_top               = src->crop_top;
225
19.4M
    dst->crop_bottom            = src->crop_bottom;
226
19.4M
    dst->crop_left              = src->crop_left;
227
19.4M
    dst->crop_right             = src->crop_right;
228
19.4M
    dst->pts                    = src->pts;
229
19.4M
    dst->duration               = src->duration;
230
19.4M
    dst->repeat_pict            = src->repeat_pict;
231
19.4M
    dst->sample_rate            = src->sample_rate;
232
19.4M
    dst->opaque                 = src->opaque;
233
19.4M
    dst->pkt_dts                = src->pkt_dts;
234
19.4M
    dst->time_base              = src->time_base;
235
19.4M
    dst->quality                = src->quality;
236
19.4M
    dst->best_effort_timestamp  = src->best_effort_timestamp;
237
19.4M
    dst->flags                  = src->flags;
238
19.4M
    dst->decode_error_flags     = src->decode_error_flags;
239
19.4M
    dst->color_primaries        = src->color_primaries;
240
19.4M
    dst->color_trc              = src->color_trc;
241
19.4M
    dst->colorspace             = src->colorspace;
242
19.4M
    dst->color_range            = src->color_range;
243
19.4M
    dst->chroma_location        = src->chroma_location;
244
19.4M
    dst->alpha_mode             = src->alpha_mode;
245
246
19.4M
    av_dict_copy(&dst->metadata, src->metadata, 0);
247
248
19.7M
    for (int i = 0; i < src->nb_side_data; i++) {
249
329k
        const AVFrameSideData *sd_src = src->side_data[i];
250
329k
        AVFrameSideData *sd_dst;
251
329k
        if (   sd_src->type == AV_FRAME_DATA_PANSCAN
252
105k
            && (src->width != dst->width || src->height != dst->height))
253
0
            continue;
254
329k
        if (force_copy) {
255
2.90k
            sd_dst = av_frame_new_side_data(dst, sd_src->type,
256
2.90k
                                            sd_src->size);
257
2.90k
            if (!sd_dst) {
258
0
                av_frame_side_data_free(&dst->side_data, &dst->nb_side_data);
259
0
                return AVERROR(ENOMEM);
260
0
            }
261
2.90k
            memcpy(sd_dst->data, sd_src->data, sd_src->size);
262
326k
        } else {
263
326k
            AVBufferRef *ref = av_buffer_ref(sd_src->buf);
264
326k
            sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
265
326k
            if (!sd_dst) {
266
0
                av_buffer_unref(&ref);
267
0
                av_frame_side_data_free(&dst->side_data, &dst->nb_side_data);
268
0
                return AVERROR(ENOMEM);
269
0
            }
270
326k
        }
271
329k
        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
272
329k
    }
273
274
19.4M
    av_refstruct_replace(&dst->private_ref, src->private_ref);
275
19.4M
    return av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
276
19.4M
}
277
278
int av_frame_ref(AVFrame *dst, const AVFrame *src)
279
17.7M
{
280
17.7M
    int ret = 0;
281
282
17.7M
    av_assert1(dst->width == 0 && dst->height == 0);
283
17.7M
    av_assert1(dst->ch_layout.nb_channels == 0 &&
284
17.7M
               dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
285
286
17.7M
    dst->format         = src->format;
287
17.7M
    dst->width          = src->width;
288
17.7M
    dst->height         = src->height;
289
17.7M
    dst->nb_samples     = src->nb_samples;
290
291
17.7M
    ret = frame_copy_props(dst, src, 0);
292
17.7M
    if (ret < 0)
293
0
        goto fail;
294
295
17.7M
    ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
296
17.7M
    if (ret < 0)
297
0
        goto fail;
298
299
    /* duplicate the frame data if it's not refcounted */
300
17.7M
    if (!src->buf[0]) {
301
326
        ret = av_frame_get_buffer(dst, 0);
302
326
        if (ret < 0)
303
326
            goto fail;
304
305
0
        ret = av_frame_copy(dst, src);
306
0
        if (ret < 0)
307
0
            goto fail;
308
309
0
        return 0;
310
0
    }
311
312
    /* ref the buffers */
313
159M
    for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
314
141M
        if (!src->buf[i])
315
96.4M
            continue;
316
45.5M
        dst->buf[i] = av_buffer_ref(src->buf[i]);
317
45.5M
        if (!dst->buf[i]) {
318
0
            ret = AVERROR(ENOMEM);
319
0
            goto fail;
320
0
        }
321
45.5M
    }
322
323
17.7M
    if (src->extended_buf) {
324
0
        dst->extended_buf = av_calloc(src->nb_extended_buf,
325
0
                                      sizeof(*dst->extended_buf));
326
0
        if (!dst->extended_buf) {
327
0
            ret = AVERROR(ENOMEM);
328
0
            goto fail;
329
0
        }
330
0
        dst->nb_extended_buf = src->nb_extended_buf;
331
332
0
        for (int i = 0; i < src->nb_extended_buf; i++) {
333
0
            dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
334
0
            if (!dst->extended_buf[i]) {
335
0
                ret = AVERROR(ENOMEM);
336
0
                goto fail;
337
0
            }
338
0
        }
339
0
    }
340
341
17.7M
    if (src->hw_frames_ctx) {
342
0
        dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
343
0
        if (!dst->hw_frames_ctx) {
344
0
            ret = AVERROR(ENOMEM);
345
0
            goto fail;
346
0
        }
347
0
    }
348
349
    /* duplicate extended data */
350
17.7M
    if (src->extended_data != src->data) {
351
0
        int ch = dst->ch_layout.nb_channels;
352
353
0
        if (ch <= 0 || ch > SIZE_MAX / sizeof(*dst->extended_data)) {
354
0
            ret = AVERROR(EINVAL);
355
0
            goto fail;
356
0
        }
357
358
0
        dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch);
359
0
        if (!dst->extended_data) {
360
0
            ret = AVERROR(ENOMEM);
361
0
            goto fail;
362
0
        }
363
0
    } else
364
17.7M
        dst->extended_data = dst->data;
365
366
17.7M
    memcpy(dst->data,     src->data,     sizeof(src->data));
367
17.7M
    memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
368
369
17.7M
    return 0;
370
371
326
fail:
372
326
    av_frame_unref(dst);
373
326
    return ret;
374
17.7M
}
375
376
int av_frame_replace(AVFrame *dst, const AVFrame *src)
377
1.68M
{
378
1.68M
    int ret = 0;
379
380
1.68M
    if (dst == src)
381
0
        return AVERROR(EINVAL);
382
383
1.68M
    if (!src->buf[0]) {
384
0
        av_frame_unref(dst);
385
386
        /* duplicate the frame data if it's not refcounted */
387
0
        if (   src->data[0] || src->data[1]
388
0
            || src->data[2] || src->data[3])
389
0
            return av_frame_ref(dst, src);
390
391
0
        ret = frame_copy_props(dst, src, 0);
392
0
        if (ret < 0)
393
0
            goto fail;
394
0
    }
395
396
1.68M
    dst->format         = src->format;
397
1.68M
    dst->width          = src->width;
398
1.68M
    dst->height         = src->height;
399
1.68M
    dst->nb_samples     = src->nb_samples;
400
401
1.68M
    ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
402
1.68M
    if (ret < 0)
403
0
        goto fail;
404
405
1.68M
    av_frame_side_data_free(&dst->side_data, &dst->nb_side_data);
406
1.68M
    av_dict_free(&dst->metadata);
407
1.68M
    ret = frame_copy_props(dst, src, 0);
408
1.68M
    if (ret < 0)
409
0
        goto fail;
410
411
    /* replace the buffers */
412
15.1M
    for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
413
13.5M
        ret = av_buffer_replace(&dst->buf[i], src->buf[i]);
414
13.5M
        if (ret < 0)
415
0
            goto fail;
416
13.5M
    }
417
418
1.68M
    if (src->extended_buf) {
419
0
        if (dst->nb_extended_buf != src->nb_extended_buf) {
420
0
            int nb_extended_buf = FFMIN(dst->nb_extended_buf, src->nb_extended_buf);
421
0
            void *tmp;
422
423
0
            for (int i = nb_extended_buf; i < dst->nb_extended_buf; i++)
424
0
                av_buffer_unref(&dst->extended_buf[i]);
425
426
0
            tmp = av_realloc_array(dst->extended_buf, src->nb_extended_buf,
427
0
                                   sizeof(*dst->extended_buf));
428
0
            if (!tmp) {
429
0
                ret = AVERROR(ENOMEM);
430
0
                goto fail;
431
0
            }
432
0
            dst->extended_buf = tmp;
433
0
            dst->nb_extended_buf = src->nb_extended_buf;
434
435
0
            memset(&dst->extended_buf[nb_extended_buf], 0,
436
0
                   (src->nb_extended_buf - nb_extended_buf) * sizeof(*dst->extended_buf));
437
0
        }
438
439
0
        for (int i = 0; i < src->nb_extended_buf; i++) {
440
0
            ret = av_buffer_replace(&dst->extended_buf[i], src->extended_buf[i]);
441
0
            if (ret < 0)
442
0
                goto fail;
443
0
        }
444
1.68M
    } else if (dst->extended_buf) {
445
0
        for (int i = 0; i < dst->nb_extended_buf; i++)
446
0
            av_buffer_unref(&dst->extended_buf[i]);
447
0
        av_freep(&dst->extended_buf);
448
0
    }
449
450
1.68M
    ret = av_buffer_replace(&dst->hw_frames_ctx, src->hw_frames_ctx);
451
1.68M
    if (ret < 0)
452
0
        goto fail;
453
454
1.68M
    if (dst->extended_data != dst->data)
455
0
        av_freep(&dst->extended_data);
456
457
1.68M
    if (src->extended_data != src->data) {
458
0
        int ch = dst->ch_layout.nb_channels;
459
460
0
        if (ch <= 0 || ch > SIZE_MAX / sizeof(*dst->extended_data)) {
461
0
            ret = AVERROR(EINVAL);
462
0
            goto fail;
463
0
        }
464
465
0
        dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch);
466
0
        if (!dst->extended_data) {
467
0
            ret = AVERROR(ENOMEM);
468
0
            goto fail;
469
0
        }
470
0
    } else
471
1.68M
        dst->extended_data = dst->data;
472
473
1.68M
    memcpy(dst->data,     src->data,     sizeof(src->data));
474
1.68M
    memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
475
476
1.68M
    return 0;
477
478
0
fail:
479
0
    av_frame_unref(dst);
480
0
    return ret;
481
1.68M
}
482
483
AVFrame *av_frame_clone(const AVFrame *src)
484
7.58k
{
485
7.58k
    AVFrame *ret = av_frame_alloc();
486
487
7.58k
    if (!ret)
488
0
        return NULL;
489
490
7.58k
    if (av_frame_ref(ret, src) < 0)
491
0
        av_frame_free(&ret);
492
493
7.58k
    return ret;
494
7.58k
}
495
496
void av_frame_unref(AVFrame *frame)
497
659M
{
498
659M
    if (!frame)
499
273k
        return;
500
501
659M
    av_frame_side_data_free(&frame->side_data, &frame->nb_side_data);
502
503
5.93G
    for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
504
5.27G
        av_buffer_unref(&frame->buf[i]);
505
685M
    for (int i = 0; i < frame->nb_extended_buf; i++)
506
26.0M
        av_buffer_unref(&frame->extended_buf[i]);
507
659M
    av_freep(&frame->extended_buf);
508
659M
    av_dict_free(&frame->metadata);
509
510
659M
    av_buffer_unref(&frame->hw_frames_ctx);
511
512
659M
    av_buffer_unref(&frame->opaque_ref);
513
659M
    av_refstruct_unref(&frame->private_ref);
514
515
659M
    if (frame->extended_data != frame->data)
516
584k
        av_freep(&frame->extended_data);
517
518
659M
    av_channel_layout_uninit(&frame->ch_layout);
519
520
659M
    get_frame_defaults(frame);
521
659M
}
522
523
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
524
39.9M
{
525
39.9M
    av_assert1(dst->width == 0 && dst->height == 0);
526
39.9M
    av_assert1(dst->ch_layout.nb_channels == 0 &&
527
39.9M
               dst->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
528
529
39.9M
    *dst = *src;
530
39.9M
    if (src->extended_data == src->data)
531
39.8M
        dst->extended_data = dst->data;
532
39.9M
    get_frame_defaults(src);
533
39.9M
}
534
535
int av_frame_is_writable(AVFrame *frame)
536
10.8M
{
537
10.8M
    int ret = 1;
538
539
    /* assume non-refcounted frames are not writable */
540
10.8M
    if (!frame->buf[0])
541
0
        return 0;
542
543
97.6M
    for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
544
86.7M
        if (frame->buf[i])
545
21.4M
            ret &= !!av_buffer_is_writable(frame->buf[i]);
546
10.8M
    for (int i = 0; i < frame->nb_extended_buf; i++)
547
0
        ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
548
549
10.8M
    return ret;
550
10.8M
}
551
552
int av_frame_make_writable(AVFrame *frame)
553
16.6k
{
554
16.6k
    AVFrame tmp;
555
16.6k
    int ret;
556
557
16.6k
    if (av_frame_is_writable(frame))
558
12.7k
        return 0;
559
560
3.92k
    memset(&tmp, 0, sizeof(tmp));
561
3.92k
    tmp.format         = frame->format;
562
3.92k
    tmp.width          = frame->width;
563
3.92k
    tmp.height         = frame->height;
564
3.92k
    tmp.nb_samples     = frame->nb_samples;
565
3.92k
    ret = av_channel_layout_copy(&tmp.ch_layout, &frame->ch_layout);
566
3.92k
    if (ret < 0) {
567
0
        av_frame_unref(&tmp);
568
0
        return ret;
569
0
    }
570
571
3.92k
    if (frame->hw_frames_ctx)
572
0
        ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
573
3.92k
    else
574
3.92k
        ret = av_frame_get_buffer(&tmp, 0);
575
3.92k
    if (ret < 0)
576
0
        return ret;
577
578
3.92k
    ret = av_frame_copy(&tmp, frame);
579
3.92k
    if (ret < 0) {
580
0
        av_frame_unref(&tmp);
581
0
        return ret;
582
0
    }
583
584
3.92k
    ret = av_frame_copy_props(&tmp, frame);
585
3.92k
    if (ret < 0) {
586
0
        av_frame_unref(&tmp);
587
0
        return ret;
588
0
    }
589
590
3.92k
    av_frame_unref(frame);
591
592
3.92k
    *frame = tmp;
593
3.92k
    if (tmp.data == tmp.extended_data)
594
3.92k
        frame->extended_data = frame->data;
595
596
3.92k
    return 0;
597
3.92k
}
598
599
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
600
12.6k
{
601
12.6k
    return frame_copy_props(dst, src, 1);
602
12.6k
}
603
604
AVBufferRef *av_frame_get_plane_buffer(const AVFrame *frame, int plane)
605
2
{
606
2
    uintptr_t data;
607
2
    int planes;
608
609
2
    if (frame->nb_samples) {
610
0
        int channels = frame->ch_layout.nb_channels;
611
0
        if (!channels)
612
0
            return NULL;
613
0
        planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
614
0
    } else
615
2
        planes = 4;
616
617
2
    if (plane < 0 || plane >= planes || !frame->extended_data[plane])
618
0
        return NULL;
619
2
    data = (uintptr_t)frame->extended_data[plane];
620
621
8
    for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
622
8
        AVBufferRef *buf = frame->buf[i];
623
8
        uintptr_t buf_begin = (uintptr_t)buf->data;
624
625
8
        if (data >= buf_begin && data < buf_begin + buf->size)
626
2
            return buf;
627
8
    }
628
0
    for (int i = 0; i < frame->nb_extended_buf; i++) {
629
0
        AVBufferRef *buf = frame->extended_buf[i];
630
0
        uintptr_t buf_begin = (uintptr_t)buf->data;
631
632
0
        if (data >= buf_begin && data < buf_begin + buf->size)
633
0
            return buf;
634
0
    }
635
0
    return NULL;
636
0
}
637
638
AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
639
                                                 enum AVFrameSideDataType type,
640
                                                 AVBufferRef *buf)
641
2.74M
{
642
2.74M
    return
643
2.74M
        ff_frame_side_data_add_from_buf(
644
2.74M
            &frame->side_data, &frame->nb_side_data, type, buf);
645
2.74M
}
646
647
AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
648
                                        enum AVFrameSideDataType type,
649
                                        size_t size)
650
1.56M
{
651
1.56M
    AVFrameSideData *ret;
652
1.56M
    AVBufferRef *buf = av_buffer_alloc(size);
653
1.56M
    ret = av_frame_new_side_data_from_buf(frame, type, buf);
654
1.56M
    if (!ret)
655
0
        av_buffer_unref(&buf);
656
1.56M
    return ret;
657
1.56M
}
658
659
AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
660
                                        enum AVFrameSideDataType type)
661
66.5M
{
662
66.5M
    return (AVFrameSideData *)av_frame_side_data_get(
663
66.5M
        frame->side_data, frame->nb_side_data,
664
66.5M
        type
665
66.5M
    );
666
66.5M
}
667
668
static int frame_copy_video(AVFrame *dst, const AVFrame *src)
669
236k
{
670
236k
    int planes;
671
672
236k
    if (dst->width  < src->width ||
673
236k
        dst->height < src->height)
674
0
        return AVERROR(EINVAL);
675
676
236k
    if (src->hw_frames_ctx || dst->hw_frames_ctx)
677
0
        return av_hwframe_transfer_data(dst, src, 0);
678
679
236k
    planes = av_pix_fmt_count_planes(dst->format);
680
500k
    for (int i = 0; i < planes; i++)
681
271k
        if (!dst->data[i] || !src->data[i])
682
7.05k
            return AVERROR(EINVAL);
683
684
229k
    av_image_copy2(dst->data, dst->linesize,
685
229k
                   src->data, src->linesize,
686
229k
                   dst->format, src->width, src->height);
687
688
229k
    return 0;
689
236k
}
690
691
static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
692
0
{
693
0
    int planar   = av_sample_fmt_is_planar(dst->format);
694
0
    int channels = dst->ch_layout.nb_channels;
695
0
    int planes   = planar ? channels : 1;
696
697
0
    if (dst->nb_samples != src->nb_samples ||
698
0
        av_channel_layout_compare(&dst->ch_layout, &src->ch_layout))
699
0
        return AVERROR(EINVAL);
700
701
0
    for (int i = 0; i < planes; i++)
702
0
        if (!dst->extended_data[i] || !src->extended_data[i])
703
0
            return AVERROR(EINVAL);
704
705
0
    av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
706
0
                    dst->nb_samples, channels, dst->format);
707
708
0
    return 0;
709
0
}
710
711
int av_frame_copy(AVFrame *dst, const AVFrame *src)
712
238k
{
713
238k
    if (dst->format != src->format || dst->format < 0)
714
1.62k
        return AVERROR(EINVAL);
715
716
236k
    if (dst->width > 0 && dst->height > 0)
717
236k
        return frame_copy_video(dst, src);
718
0
    else if (dst->nb_samples > 0 &&
719
0
             (av_channel_layout_check(&dst->ch_layout)))
720
0
        return frame_copy_audio(dst, src);
721
722
0
    return AVERROR(EINVAL);
723
236k
}
724
725
void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
726
66.0M
{
727
66.0M
    av_frame_side_data_remove(&frame->side_data, &frame->nb_side_data, type);
728
66.0M
}
729
730
static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
731
                                 const AVPixFmtDescriptor *desc)
732
13.9M
{
733
41.2M
    for (int i = 0; frame->data[i]; i++) {
734
31.0M
        const AVComponentDescriptor *comp = NULL;
735
31.0M
        int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
736
31.0M
        int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
737
738
31.0M
        if (desc->flags & AV_PIX_FMT_FLAG_PAL && i == 1) {
739
3.69M
            offsets[i] = 0;
740
3.69M
            break;
741
3.69M
        }
742
743
        /* find any component descriptor for this plane */
744
48.0M
        for (int j = 0; j < desc->nb_components; j++) {
745
48.0M
            if (desc->comp[j].plane == i) {
746
27.3M
                comp = &desc->comp[j];
747
27.3M
                break;
748
27.3M
            }
749
48.0M
        }
750
27.3M
        if (!comp)
751
0
            return AVERROR_BUG;
752
753
27.3M
        offsets[i] = (frame->crop_top  >> shift_y) * frame->linesize[i] +
754
27.3M
                     (frame->crop_left >> shift_x) * comp->step;
755
27.3M
    }
756
757
13.9M
    return 0;
758
13.9M
}
759
760
int av_frame_apply_cropping(AVFrame *frame, int flags)
761
15.6M
{
762
15.6M
    const AVPixFmtDescriptor *desc;
763
15.6M
    size_t offsets[4];
764
15.6M
    int ret;
765
766
15.6M
    if (!(frame->width > 0 && frame->height > 0))
767
0
        return AVERROR(EINVAL);
768
769
15.6M
    if (frame->crop_left >= INT_MAX - frame->crop_right        ||
770
15.6M
        frame->crop_top  >= INT_MAX - frame->crop_bottom       ||
771
15.6M
        (frame->crop_left + frame->crop_right) >= frame->width ||
772
15.6M
        (frame->crop_top + frame->crop_bottom) >= frame->height)
773
0
        return AVERROR(ERANGE);
774
775
15.6M
    desc = av_pix_fmt_desc_get(frame->format);
776
15.6M
    if (!desc)
777
0
        return AVERROR_BUG;
778
779
    /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
780
     * formats cannot be easily handled here either (and corresponding decoders
781
     * should not export any cropping anyway), so do the same for those as well.
782
     * */
783
15.6M
    if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
784
1.78M
        frame->width      -= frame->crop_right;
785
1.78M
        frame->height     -= frame->crop_bottom;
786
1.78M
        frame->crop_right  = 0;
787
1.78M
        frame->crop_bottom = 0;
788
1.78M
        return 0;
789
1.78M
    }
790
791
    /* calculate the offsets for each plane */
792
13.8M
    ret = calc_cropping_offsets(offsets, frame, desc);
793
13.8M
    if (ret < 0)
794
0
        return ret;
795
796
    /* adjust the offsets to avoid breaking alignment */
797
13.8M
    if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
798
13.8M
        int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
799
13.8M
        int min_log2_align = INT_MAX;
800
801
44.8M
        for (int i = 0; frame->data[i]; i++) {
802
30.9M
            int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
803
30.9M
            min_log2_align = FFMIN(log2_align, min_log2_align);
804
30.9M
        }
805
806
        /* we assume, and it should always be true, that the data alignment is
807
         * related to the cropping alignment by a constant power-of-2 factor */
808
13.8M
        if (log2_crop_align < min_log2_align)
809
0
            return AVERROR_BUG;
810
811
13.8M
        if (min_log2_align < 5 && log2_crop_align != INT_MAX) {
812
40.4k
            frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
813
40.4k
            ret = calc_cropping_offsets(offsets, frame, desc);
814
40.4k
            if (ret < 0)
815
0
                return ret;
816
40.4k
        }
817
13.8M
    }
818
819
44.8M
    for (int i = 0; frame->data[i]; i++)
820
30.9M
        frame->data[i] += offsets[i];
821
822
13.8M
    frame->width      -= (frame->crop_left + frame->crop_right);
823
13.8M
    frame->height     -= (frame->crop_top  + frame->crop_bottom);
824
13.8M
    frame->crop_left   = 0;
825
13.8M
    frame->crop_right  = 0;
826
13.8M
    frame->crop_top    = 0;
827
13.8M
    frame->crop_bottom = 0;
828
829
13.8M
    return 0;
830
13.8M
}