Coverage Report

Created: 2026-10-14 06:52

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