Coverage Report

Created: 2026-05-31 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/avcodec.c
Line
Count
Source
1
/*
2
 * AVCodecContext functions for libavcodec
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * AVCodecContext functions for libavcodec
24
 */
25
26
#include <assert.h>
27
28
#include "config.h"
29
#include "libavutil/avassert.h"
30
#include "libavutil/avstring.h"
31
#include "libavutil/bprint.h"
32
#include "libavutil/channel_layout.h"
33
#include "libavutil/common.h"
34
#include "libavutil/emms.h"
35
#include "libavutil/imgutils.h"
36
#include "libavutil/mem.h"
37
#include "libavutil/opt.h"
38
#include "libavutil/thread.h"
39
#include "avcodec.h"
40
#include "avcodec_internal.h"
41
#include "bsf.h"
42
#include "codec_desc.h"
43
#include "codec_internal.h"
44
#include "decode.h"
45
#include "frame_thread_encoder.h"
46
#include "hwconfig.h"
47
#include "internal.h"
48
#include "libavutil/refstruct.h"
49
50
/**
51
 * Maximum size in bytes of extradata.
52
 * This value was chosen such that every bit of the buffer is
53
 * addressable by a 32-bit signed integer as used by get_bits.
54
 */
55
0
#define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
56
57
const SideDataMap ff_sd_global_map[] = {
58
    { AV_PKT_DATA_REPLAYGAIN ,                AV_FRAME_DATA_REPLAYGAIN },
59
    { AV_PKT_DATA_DISPLAYMATRIX,              AV_FRAME_DATA_DISPLAYMATRIX },
60
    { AV_PKT_DATA_SPHERICAL,                  AV_FRAME_DATA_SPHERICAL },
61
    { AV_PKT_DATA_STEREO3D,                   AV_FRAME_DATA_STEREO3D },
62
    { AV_PKT_DATA_AUDIO_SERVICE_TYPE,         AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
63
    { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
64
    { AV_PKT_DATA_CONTENT_LIGHT_LEVEL,        AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
65
    { AV_PKT_DATA_ICC_PROFILE,                AV_FRAME_DATA_ICC_PROFILE },
66
    { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT },
67
    { AV_PKT_DATA_3D_REFERENCE_DISPLAYS,      AV_FRAME_DATA_3D_REFERENCE_DISPLAYS },
68
    { AV_PKT_DATA_EXIF,                       AV_FRAME_DATA_EXIF },
69
    { AV_PKT_DATA_NB },
70
};
71
72
73
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
74
0
{
75
0
    size_t i;
76
77
0
    for (i = 0; i < count; i++) {
78
0
        size_t offset = i * size;
79
0
        int r = func(c, FF_PTR_ADD((char *)arg, offset));
80
0
        if (ret)
81
0
            ret[i] = r;
82
0
    }
83
0
    emms_c();
84
0
    return 0;
85
0
}
86
87
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
88
0
{
89
0
    int i;
90
91
0
    for (i = 0; i < count; i++) {
92
0
        int r = func(c, arg, i, 0);
93
0
        if (ret)
94
0
            ret[i] = r;
95
0
    }
96
0
    emms_c();
97
0
    return 0;
98
0
}
99
100
static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
101
102
static void lock_avcodec(const FFCodec *codec)
103
0
{
104
0
    if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
105
0
        ff_mutex_lock(&codec_mutex);
106
0
}
107
108
static void unlock_avcodec(const FFCodec *codec)
109
0
{
110
0
    if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && codec->init)
111
0
        ff_mutex_unlock(&codec_mutex);
112
0
}
113
114
static int64_t get_bit_rate(AVCodecContext *ctx)
115
0
{
116
0
    int64_t bit_rate;
117
0
    int bits_per_sample;
118
119
0
    switch (ctx->codec_type) {
120
0
    case AVMEDIA_TYPE_VIDEO:
121
0
    case AVMEDIA_TYPE_DATA:
122
0
    case AVMEDIA_TYPE_SUBTITLE:
123
0
    case AVMEDIA_TYPE_ATTACHMENT:
124
0
        bit_rate = ctx->bit_rate;
125
0
        break;
126
0
    case AVMEDIA_TYPE_AUDIO:
127
0
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
128
0
        if (bits_per_sample) {
129
0
            bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
130
0
            if (bit_rate > INT64_MAX / bits_per_sample) {
131
0
                bit_rate = 0;
132
0
            } else
133
0
                bit_rate *= bits_per_sample;
134
0
        } else
135
0
            bit_rate = ctx->bit_rate;
136
0
        break;
137
0
    default:
138
0
        bit_rate = 0;
139
0
        break;
140
0
    }
141
0
    return bit_rate;
142
0
}
143
144
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
145
0
{
146
0
    int ret = 0;
147
0
    AVCodecInternal *avci;
148
0
    const FFCodec *codec2;
149
0
    const AVDictionaryEntry *e;
150
151
0
    if (avcodec_is_open(avctx))
152
0
        return 0;
153
154
0
    if (!codec && !avctx->codec) {
155
0
        av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
156
0
        return AVERROR(EINVAL);
157
0
    }
158
0
    if (codec && avctx->codec && codec != avctx->codec) {
159
0
        av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
160
0
                                    "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
161
0
        return AVERROR(EINVAL);
162
0
    }
163
0
    if (!codec)
164
0
        codec = avctx->codec;
165
0
    codec2 = ffcodec(codec);
166
167
0
    if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
168
0
        (avctx->codec_id   != AV_CODEC_ID_NONE     && avctx->codec_id   != codec->id)) {
169
0
        av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
170
0
        return AVERROR(EINVAL);
171
0
    }
172
173
0
    avctx->codec_type = codec->type;
174
0
    avctx->codec_id   = codec->id;
175
0
    avctx->codec      = codec;
176
177
0
    if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
178
0
        return AVERROR(EINVAL);
179
180
    // set the whitelist from provided options dict,
181
    // so we can check it immediately
182
0
    e = options ? av_dict_get(*options, "codec_whitelist", NULL, 0) : NULL;
183
0
    if (e) {
184
0
        ret = av_opt_set(avctx, e->key, e->value, 0);
185
0
        if (ret < 0)
186
0
            return ret;
187
0
    }
188
189
0
    if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
190
0
        av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
191
0
        return AVERROR(EINVAL);
192
0
    }
193
194
0
    avci = ff_codec_is_decoder(codec) ?
195
0
        ff_decode_internal_alloc()    :
196
0
        ff_encode_internal_alloc();
197
0
    if (!avci) {
198
0
        ret = AVERROR(ENOMEM);
199
0
        goto end;
200
0
    }
201
0
    avctx->internal = avci;
202
203
0
    avci->buffer_frame = av_frame_alloc();
204
0
    avci->buffer_pkt = av_packet_alloc();
205
0
    if (!avci->buffer_frame || !avci->buffer_pkt) {
206
0
        ret = AVERROR(ENOMEM);
207
0
        goto free_and_end;
208
0
    }
209
210
0
    if (codec2->priv_data_size > 0) {
211
0
        if (!avctx->priv_data) {
212
0
            avctx->priv_data = av_mallocz(codec2->priv_data_size);
213
0
            if (!avctx->priv_data) {
214
0
                ret = AVERROR(ENOMEM);
215
0
                goto free_and_end;
216
0
            }
217
0
            if (codec->priv_class) {
218
0
                *(const AVClass **)avctx->priv_data = codec->priv_class;
219
0
                av_opt_set_defaults(avctx->priv_data);
220
0
            }
221
0
        }
222
0
    } else {
223
0
        avctx->priv_data = NULL;
224
0
    }
225
226
0
    ret = av_opt_set_dict2(avctx, options, AV_OPT_SEARCH_CHILDREN);
227
0
    if (ret < 0)
228
0
        goto free_and_end;
229
230
    // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
231
0
    if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
232
0
          (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
233
0
        if (avctx->coded_width && avctx->coded_height)
234
0
            ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
235
0
        else if (avctx->width && avctx->height)
236
0
            ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
237
0
        if (ret < 0)
238
0
            goto free_and_end;
239
0
    }
240
241
0
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
242
0
        && (  av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
243
0
           || av_image_check_size2(avctx->width,       avctx->height,       avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
244
0
        av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
245
0
        ff_set_dimensions(avctx, 0, 0);
246
0
    }
247
248
0
    if (avctx->width > 0 && avctx->height > 0) {
249
0
        if (av_image_check_sar(avctx->width, avctx->height,
250
0
                               avctx->sample_aspect_ratio) < 0) {
251
0
            av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
252
0
                   avctx->sample_aspect_ratio.num,
253
0
                   avctx->sample_aspect_ratio.den);
254
0
            avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
255
0
        }
256
0
    }
257
258
    /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
259
     * in particular checks that sample_rate is set for all audio encoders. */
260
0
    if (avctx->sample_rate < 0 ||
261
0
        avctx->sample_rate == 0 && avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
262
0
        !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
263
0
        av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
264
0
        ret = AVERROR(EINVAL);
265
0
        goto free_and_end;
266
0
    }
267
0
    if (avctx->block_align < 0) {
268
0
        av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
269
0
        ret = AVERROR(EINVAL);
270
0
        goto free_and_end;
271
0
    }
272
273
    /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
274
     * in particular checks that nb_channels is set for all audio encoders. */
275
0
    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels
276
0
        && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) {
277
0
        av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n",
278
0
               ff_codec_is_decoder(codec) ? "Decoder" : "Encoder");
279
0
        ret = AVERROR(EINVAL);
280
0
        goto free_and_end;
281
0
    }
282
0
    if (avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) {
283
0
        av_log(avctx, AV_LOG_ERROR, "Invalid channel layout\n");
284
0
        ret = AVERROR(EINVAL);
285
0
        goto free_and_end;
286
0
    }
287
0
    if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
288
0
        av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
289
0
        ret = AVERROR(EINVAL);
290
0
        goto free_and_end;
291
0
    }
292
293
0
    avctx->frame_num = 0;
294
0
    avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
295
296
0
    if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
297
0
        avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
298
0
        const char *codec_string = ff_codec_is_encoder(codec) ? "encoder" : "decoder";
299
0
        const AVCodec *codec2;
300
0
        av_log(avctx, AV_LOG_ERROR,
301
0
               "The %s '%s' is experimental but experimental codecs are not enabled, "
302
0
               "add '-strict %d' if you want to use it.\n",
303
0
               codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
304
0
        codec2 = ff_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
305
0
        if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
306
0
            av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
307
0
                codec_string, codec2->name);
308
0
        ret = AVERROR_EXPERIMENTAL;
309
0
        goto free_and_end;
310
0
    }
311
312
0
    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
313
0
        (!avctx->time_base.num || !avctx->time_base.den)) {
314
0
        avctx->time_base.num = 1;
315
0
        avctx->time_base.den = avctx->sample_rate;
316
0
    }
317
318
0
    if (ff_codec_is_encoder(avctx->codec))
319
0
        ret = ff_encode_preinit(avctx);
320
0
    else
321
0
        ret = ff_decode_preinit(avctx);
322
0
    if (ret < 0)
323
0
        goto free_and_end;
324
325
0
    if (HAVE_THREADS && !avci->frame_thread_encoder) {
326
        /* Frame-threaded decoders call FFCodec.init for their child contexts. */
327
0
        lock_avcodec(codec2);
328
0
        ret = ff_thread_init(avctx);
329
0
        unlock_avcodec(codec2);
330
0
        if (ret < 0) {
331
0
            goto free_and_end;
332
0
        }
333
0
    }
334
0
    if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
335
0
        avctx->thread_count = 1;
336
337
0
    if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
338
0
        avci->frame_thread_encoder) {
339
0
        if (codec2->init) {
340
0
            lock_avcodec(codec2);
341
0
            ret = codec2->init(avctx);
342
0
            unlock_avcodec(codec2);
343
0
            if (ret < 0) {
344
0
                avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
345
0
                goto free_and_end;
346
0
            }
347
0
        }
348
0
        avci->needs_close = 1;
349
0
    }
350
351
0
    ret=0;
352
353
0
    if (ff_codec_is_decoder(avctx->codec)) {
354
0
        if (!avctx->bit_rate)
355
0
            avctx->bit_rate = get_bit_rate(avctx);
356
357
358
0
        if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
359
0
            !avctx->frame_size && (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
360
0
            av_log(avctx, AV_LOG_ERROR, "Fixed frame size requested but no frame_size value set\n");
361
0
            ret = AVERROR(EINVAL);
362
0
            goto free_and_end;
363
0
        }
364
365
0
        avci->skip_samples = avctx->delay;
366
367
        /* validate channel layout from the decoder */
368
0
        if ((avctx->ch_layout.nb_channels && !av_channel_layout_check(&avctx->ch_layout)) ||
369
0
            avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
370
0
            ret = AVERROR(EINVAL);
371
0
            goto free_and_end;
372
0
        }
373
0
        if (avctx->bits_per_coded_sample < 0) {
374
0
            ret = AVERROR(EINVAL);
375
0
            goto free_and_end;
376
0
        }
377
0
    }
378
0
    if (codec->priv_class)
379
0
        av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
380
381
0
end:
382
383
0
    return ret;
384
0
free_and_end:
385
0
    ff_codec_close(avctx);
386
0
    goto end;
387
0
}
388
389
void avcodec_flush_buffers(AVCodecContext *avctx)
390
0
{
391
0
    AVCodecInternal *avci = avctx->internal;
392
393
0
    if (av_codec_is_encoder(avctx->codec)) {
394
0
        int caps = avctx->codec->capabilities;
395
396
0
        if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
397
            // Only encoders that explicitly declare support for it can be
398
            // flushed. Otherwise, this is a no-op.
399
0
            av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
400
0
                   "that doesn't support it\n");
401
0
            return;
402
0
        }
403
0
        ff_encode_flush_buffers(avctx);
404
0
    } else
405
0
        ff_decode_flush_buffers(avctx);
406
407
0
    avci->draining      = 0;
408
0
    avci->draining_done = 0;
409
0
    if (avci->buffer_frame)
410
0
        av_frame_unref(avci->buffer_frame);
411
0
    if (avci->buffer_pkt)
412
0
        av_packet_unref(avci->buffer_pkt);
413
414
0
    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME &&
415
0
        !avci->is_frame_mt)
416
0
        ff_thread_flush(avctx);
417
0
    else if (ffcodec(avctx->codec)->flush)
418
0
        ffcodec(avctx->codec)->flush(avctx);
419
0
}
420
421
void avsubtitle_free(AVSubtitle *sub)
422
0
{
423
0
    int i;
424
425
0
    for (i = 0; i < sub->num_rects; i++) {
426
0
        AVSubtitleRect *const rect = sub->rects[i];
427
428
0
        av_freep(&rect->data[0]);
429
0
        av_freep(&rect->data[1]);
430
0
        av_freep(&rect->data[2]);
431
0
        av_freep(&rect->data[3]);
432
0
        av_freep(&rect->text);
433
0
        av_freep(&rect->ass);
434
435
0
        av_freep(&sub->rects[i]);
436
0
    }
437
438
0
    av_freep(&sub->rects);
439
440
0
    memset(sub, 0, sizeof(*sub));
441
0
}
442
443
av_cold void ff_codec_close(AVCodecContext *avctx)
444
0
{
445
0
    int i;
446
447
0
    if (avcodec_is_open(avctx)) {
448
0
        AVCodecInternal *avci = avctx->internal;
449
450
#if CONFIG_FRAME_THREAD_ENCODER
451
        if (avci->frame_thread_encoder && avctx->thread_count > 1) {
452
            ff_frame_thread_encoder_free(avctx);
453
        }
454
#endif
455
0
        if (HAVE_THREADS && avci->thread_ctx)
456
0
            ff_thread_free(avctx);
457
0
        if (avci->needs_close && ffcodec(avctx->codec)->close)
458
0
            ffcodec(avctx->codec)->close(avctx);
459
0
        avci->byte_buffer_size = 0;
460
0
        av_freep(&avci->byte_buffer);
461
0
        av_frame_free(&avci->buffer_frame);
462
0
        av_packet_free(&avci->buffer_pkt);
463
0
        av_packet_free(&avci->last_pkt_props);
464
465
0
        av_packet_free(&avci->in_pkt);
466
0
        av_frame_free(&avci->in_frame);
467
0
        av_frame_free(&avci->recon_frame);
468
469
0
        av_refstruct_unref(&avci->pool);
470
0
        av_refstruct_pool_uninit(&avci->progress_frame_pool);
471
0
        if (av_codec_is_decoder(avctx->codec))
472
0
            ff_decode_internal_uninit(avctx);
473
474
0
        ff_hwaccel_uninit(avctx);
475
476
0
        av_bsf_free(&avci->bsf);
477
478
#if CONFIG_LCMS2
479
        ff_icc_context_uninit(&avci->icc);
480
#endif
481
482
0
        av_freep(&avctx->internal);
483
0
    }
484
485
0
    for (i = 0; i < avctx->nb_coded_side_data; i++)
486
0
        av_freep(&avctx->coded_side_data[i].data);
487
0
    av_freep(&avctx->coded_side_data);
488
0
    avctx->nb_coded_side_data = 0;
489
0
    av_frame_side_data_free(&avctx->decoded_side_data,
490
0
                            &avctx->nb_decoded_side_data);
491
492
0
    av_buffer_unref(&avctx->hw_frames_ctx);
493
0
    av_buffer_unref(&avctx->hw_device_ctx);
494
495
0
    if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
496
0
        av_opt_free(avctx->priv_data);
497
0
    av_opt_free(avctx);
498
0
    av_freep(&avctx->priv_data);
499
0
    if (av_codec_is_encoder(avctx->codec)) {
500
0
        av_freep(&avctx->extradata);
501
0
        avctx->extradata_size = 0;
502
0
    } else if (av_codec_is_decoder(avctx->codec))
503
0
        av_freep(&avctx->subtitle_header);
504
505
0
    avctx->codec = NULL;
506
0
    avctx->active_thread_type = 0;
507
0
}
508
509
static const char *unknown_if_null(const char *str)
510
0
{
511
0
    return str ? str : "unknown";
512
0
}
513
514
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
515
0
{
516
0
    const char *codec_type;
517
0
    const char *codec_name;
518
0
    const char *profile = NULL;
519
0
    AVBPrint bprint;
520
0
    int64_t bitrate;
521
0
    int new_line = 0;
522
0
    AVRational display_aspect_ratio;
523
0
    const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
524
0
    const char *str;
525
526
0
    if (!buf || buf_size <= 0)
527
0
        return;
528
0
    av_bprint_init_for_buffer(&bprint, buf, buf_size);
529
0
    codec_type = av_get_media_type_string(enc->codec_type);
530
0
    codec_name = avcodec_get_name(enc->codec_id);
531
0
    profile = avcodec_profile_name(enc->codec_id, enc->profile);
532
533
0
    av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
534
0
               codec_name);
535
0
    buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
536
537
0
    if (enc->codec && strcmp(enc->codec->name, codec_name))
538
0
        av_bprintf(&bprint, " (%s)", enc->codec->name);
539
540
0
    if (profile)
541
0
        av_bprintf(&bprint, " (%s)", profile);
542
0
    if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
543
0
        && av_log_get_level() >= AV_LOG_VERBOSE
544
0
        && enc->refs)
545
0
        av_bprintf(&bprint, ", %d reference frame%s",
546
0
                   enc->refs, enc->refs > 1 ? "s" : "");
547
548
0
    if (enc->codec_tag)
549
0
        av_bprintf(&bprint, " (%s / 0x%04X)",
550
0
                   av_fourcc2str(enc->codec_tag), enc->codec_tag);
551
552
0
    switch (enc->codec_type) {
553
0
    case AVMEDIA_TYPE_VIDEO:
554
0
        {
555
0
            unsigned len;
556
557
0
            av_bprintf(&bprint, "%s%s", separator,
558
0
                       enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
559
0
                       unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
560
561
0
            av_bprint_chars(&bprint, '(', 1);
562
0
            len = bprint.len;
563
564
            /* The following check ensures that '(' has been written
565
             * and therefore allows us to erase it if it turns out
566
             * to be unnecessary. */
567
0
            if (!av_bprint_is_complete(&bprint))
568
0
                return;
569
570
0
            if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
571
0
                enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
572
0
                av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
573
0
            if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
574
0
                (str = av_color_range_name(enc->color_range)))
575
0
                av_bprintf(&bprint, "%s, ", str);
576
577
0
            if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
578
0
                enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
579
0
                enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
580
0
                const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
581
0
                const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
582
0
                const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
583
0
                if (strcmp(col, pri) || strcmp(col, trc)) {
584
0
                    new_line = 1;
585
0
                    av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
586
0
                } else
587
0
                    av_bprintf(&bprint, "%s, ", col);
588
0
            }
589
590
0
            if (enc->field_order != AV_FIELD_UNKNOWN) {
591
0
                const char *field_order = "progressive";
592
0
                if (enc->field_order == AV_FIELD_TT)
593
0
                    field_order = "top first";
594
0
                else if (enc->field_order == AV_FIELD_BB)
595
0
                    field_order = "bottom first";
596
0
                else if (enc->field_order == AV_FIELD_TB)
597
0
                    field_order = "top coded first (swapped)";
598
0
                else if (enc->field_order == AV_FIELD_BT)
599
0
                    field_order = "bottom coded first (swapped)";
600
601
0
                av_bprintf(&bprint, "%s, ", field_order);
602
0
            }
603
604
0
            if (av_log_get_level() >= AV_LOG_VERBOSE &&
605
0
                enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
606
0
                (str = av_chroma_location_name(enc->chroma_sample_location)))
607
0
                av_bprintf(&bprint, "%s, ", str);
608
609
0
            if (len == bprint.len) {
610
0
                bprint.str[len - 1] = '\0';
611
0
                bprint.len--;
612
0
            } else {
613
0
                if (bprint.len - 2 < bprint.size) {
614
                    /* Erase the last ", " */
615
0
                    bprint.len -= 2;
616
0
                    bprint.str[bprint.len] = '\0';
617
0
                }
618
0
                av_bprint_chars(&bprint, ')', 1);
619
0
            }
620
0
        }
621
622
0
        if (enc->width) {
623
0
            av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
624
0
                       enc->width, enc->height);
625
626
0
            if (av_log_get_level() >= AV_LOG_VERBOSE &&
627
0
                enc->coded_width && enc->coded_height &&
628
0
                (enc->width != enc->coded_width ||
629
0
                 enc->height != enc->coded_height))
630
0
                av_bprintf(&bprint, " (%dx%d)",
631
0
                           enc->coded_width, enc->coded_height);
632
633
0
            if (enc->sample_aspect_ratio.num) {
634
0
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
635
0
                          enc->width * (int64_t)enc->sample_aspect_ratio.num,
636
0
                          enc->height * (int64_t)enc->sample_aspect_ratio.den,
637
0
                          1024 * 1024);
638
0
                av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
639
0
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
640
0
                         display_aspect_ratio.num, display_aspect_ratio.den);
641
0
            }
642
0
            if (av_log_get_level() >= AV_LOG_DEBUG) {
643
0
                int g = av_gcd(enc->time_base.num, enc->time_base.den);
644
0
                av_bprintf(&bprint, ", %d/%d",
645
0
                           enc->time_base.num / g, enc->time_base.den / g);
646
0
            }
647
0
        }
648
0
        if (encode) {
649
0
            av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
650
0
        } else {
651
0
#if FF_API_CODEC_PROPS
652
0
FF_DISABLE_DEPRECATION_WARNINGS
653
0
            if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
654
0
                av_bprintf(&bprint, ", Closed Captions");
655
0
            if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
656
0
                av_bprintf(&bprint, ", Film Grain");
657
0
            if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
658
0
                av_bprintf(&bprint, ", lossless");
659
0
FF_ENABLE_DEPRECATION_WARNINGS
660
0
#endif
661
0
        }
662
0
        break;
663
0
    case AVMEDIA_TYPE_AUDIO:
664
0
        av_bprintf(&bprint, "%s", separator);
665
666
0
        if (enc->sample_rate) {
667
0
            av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
668
0
        }
669
0
        av_channel_layout_describe_bprint(&enc->ch_layout, &bprint);
670
0
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
671
0
            (str = av_get_sample_fmt_name(enc->sample_fmt))) {
672
0
            av_bprintf(&bprint, ", %s", str);
673
0
        }
674
0
        if (   enc->bits_per_raw_sample > 0
675
0
            && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
676
0
            av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
677
0
        if (av_log_get_level() >= AV_LOG_VERBOSE) {
678
0
            if (enc->initial_padding)
679
0
                av_bprintf(&bprint, ", delay %d", enc->initial_padding);
680
0
            if (enc->trailing_padding)
681
0
                av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
682
0
        }
683
0
        break;
684
0
    case AVMEDIA_TYPE_DATA:
685
0
        if (av_log_get_level() >= AV_LOG_DEBUG) {
686
0
            int g = av_gcd(enc->time_base.num, enc->time_base.den);
687
0
            if (g)
688
0
                av_bprintf(&bprint, ", %d/%d",
689
0
                           enc->time_base.num / g, enc->time_base.den / g);
690
0
        }
691
0
        break;
692
0
    case AVMEDIA_TYPE_SUBTITLE:
693
0
        if (enc->width)
694
0
            av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
695
0
        break;
696
0
    default:
697
0
        return;
698
0
    }
699
0
    if (encode) {
700
0
        if (enc->flags & AV_CODEC_FLAG_PASS1)
701
0
            av_bprintf(&bprint, ", pass 1");
702
0
        if (enc->flags & AV_CODEC_FLAG_PASS2)
703
0
            av_bprintf(&bprint, ", pass 2");
704
0
    }
705
0
    bitrate = get_bit_rate(enc);
706
0
    if (bitrate != 0) {
707
0
        av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
708
0
    } else if (enc->rc_max_rate > 0) {
709
0
        av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
710
0
    }
711
0
}
712
713
int avcodec_is_open(AVCodecContext *s)
714
0
{
715
0
    return !!s->internal;
716
0
}
717
718
int attribute_align_arg avcodec_receive_frame_flags(AVCodecContext *avctx,
719
                                               AVFrame *frame, unsigned flags)
720
0
{
721
0
    av_frame_unref(frame);
722
723
0
    if (!avcodec_is_open(avctx) || !avctx->codec)
724
0
        return AVERROR(EINVAL);
725
726
0
    if (ff_codec_is_decoder(avctx->codec))
727
0
        return ff_decode_receive_frame(avctx, frame, flags);
728
0
    return ff_encode_receive_frame(avctx, frame);
729
0
}
730
731
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
732
0
{
733
0
    return avcodec_receive_frame_flags(avctx, frame, 0);
734
0
}
735
736
#define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check)   \
737
0
    do {                                                                    \
738
0
        if (codec->type != (allowed_type))                                  \
739
0
            return AVERROR(EINVAL);                                         \
740
0
        const field_type *ptr = codec->field;                               \
741
0
        *out_configs = ptr;                                                 \
742
0
        if (ptr) {                                                          \
743
0
            for (int i = 0;; i++) {                                         \
744
0
                const field_type var = ptr[i];                              \
745
0
                if (sentinel_check) {                                       \
746
0
                    *out_num_configs = i;                                   \
747
0
                    break;                                                  \
748
0
                }                                                           \
749
0
            }                                                               \
750
0
        } else                                                              \
751
0
            *out_num_configs = 0;                                           \
752
0
        return 0;                                                           \
753
0
    } while (0)
754
755
static const enum AVColorRange color_range_tab[] = {
756
    AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
757
    AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
758
};
759
760
static const enum AVAlphaMode alpha_mode_tab[] = {
761
    AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED,
762
    AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
763
};
764
765
static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values");
766
static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values");
767
static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values");
768
static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values");
769
770
static const uint8_t offset_tab[] = {
771
    [AVCOL_RANGE_MPEG] = 3,
772
    [AVCOL_RANGE_JPEG] = 1,
773
    [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
774
};
775
776
int ff_default_get_supported_config(const AVCodecContext *avctx,
777
                                    const AVCodec *codec,
778
                                    enum AVCodecConfig config,
779
                                    unsigned flags,
780
                                    const void **out_configs,
781
                                    int *out_num_configs)
782
0
{
783
0
    const FFCodec *codec2 = ffcodec(codec);
784
785
0
    switch (config) {
786
0
FF_DISABLE_DEPRECATION_WARNINGS
787
0
    case AV_CODEC_CONFIG_PIX_FORMAT:
788
0
        WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
789
0
    case AV_CODEC_CONFIG_FRAME_RATE:
790
0
        WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
791
0
    case AV_CODEC_CONFIG_SAMPLE_RATE:
792
0
        WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
793
0
    case AV_CODEC_CONFIG_SAMPLE_FORMAT:
794
0
        WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
795
0
    case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
796
0
        WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
797
0
FF_ENABLE_DEPRECATION_WARNINGS
798
799
0
    case AV_CODEC_CONFIG_COLOR_RANGE:
800
0
        if (codec->type != AVMEDIA_TYPE_VIDEO)
801
0
            return AVERROR(EINVAL);
802
0
        unsigned color_ranges = codec2->color_ranges;
803
0
        if (color_ranges)
804
0
            *out_configs = color_range_tab + offset_tab[color_ranges];
805
0
        else
806
0
            *out_configs = NULL;
807
0
        *out_num_configs = av_popcount(color_ranges);
808
0
        return 0;
809
810
0
    case AV_CODEC_CONFIG_COLOR_SPACE:
811
0
        *out_configs = NULL;
812
0
        *out_num_configs = 0;
813
0
        return 0;
814
815
0
    case AV_CODEC_CONFIG_ALPHA_MODE:
816
0
        if (codec->type != AVMEDIA_TYPE_VIDEO)
817
0
            return AVERROR(EINVAL);
818
0
        unsigned alpha_modes = codec2->alpha_modes;
819
0
        if (alpha_modes)
820
0
            *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
821
0
        else
822
0
            *out_configs = NULL;
823
0
        *out_num_configs = av_popcount(alpha_modes);
824
0
        return 0;
825
826
0
    default:
827
0
        return AVERROR(EINVAL);
828
0
    }
829
0
}
830
831
int avcodec_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec,
832
                                 enum AVCodecConfig config, unsigned flags,
833
                                 const void **out, int *out_num)
834
0
{
835
0
    const FFCodec *codec2;
836
0
    int dummy_num = 0;
837
0
    if (!codec)
838
0
        codec = avctx->codec;
839
0
    if (!out_num)
840
0
        out_num = &dummy_num;
841
842
0
    codec2 = ffcodec(codec);
843
0
    if (codec2->get_supported_config) {
844
0
        return codec2->get_supported_config(avctx, codec, config, flags, out, out_num);
845
0
    } else {
846
0
        return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
847
0
    }
848
0
}
849
850
int av_packet_side_data_from_frame(AVPacketSideData **psd, int *pnb_sd,
851
                                   const AVFrameSideData *src, unsigned int flags)
852
0
{
853
0
    AVPacketSideData *sd = NULL;
854
855
0
    for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
856
0
        if (ff_sd_global_map[j].frame != src->type)
857
0
            continue;
858
859
0
        sd = av_packet_side_data_new(psd, pnb_sd, ff_sd_global_map[j].packet,
860
0
                                     src->size, 0);
861
862
0
        if (!sd)
863
0
            return AVERROR(ENOMEM);
864
865
0
        memcpy(sd->data, src->data, src->size);
866
0
        break;
867
0
    }
868
869
0
    if (!sd)
870
0
        return AVERROR(EINVAL);
871
872
0
    return 0;
873
0
}
874
875
int av_packet_side_data_to_frame(AVFrameSideData ***psd, int *pnb_sd,
876
                                 const AVPacketSideData *src, unsigned int flags)
877
0
{
878
0
    AVFrameSideData *sd = NULL;
879
880
0
    for (unsigned j = 0; ff_sd_global_map[j].packet < AV_PKT_DATA_NB; j++) {
881
0
        if (ff_sd_global_map[j].packet != src->type)
882
0
            continue;
883
884
0
        sd = av_frame_side_data_new(psd, pnb_sd, ff_sd_global_map[j].frame,
885
0
                                    src->size, flags);
886
887
0
        if (!sd)
888
0
            return AVERROR(ENOMEM);
889
890
0
        memcpy(sd->data, src->data, src->size);
891
0
        break;
892
0
    }
893
894
0
    if (!sd)
895
0
        return AVERROR(EINVAL);
896
897
0
    return 0;
898
0
}