GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avcodec.c Lines: 375 477 78.6 %
Date: 2021-04-20 04:37:23 Branches: 285 408 69.9 %

Line Branch Exec 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 "config.h"
27
#include "libavutil/avassert.h"
28
#include "libavutil/avstring.h"
29
#include "libavutil/bprint.h"
30
#include "libavutil/imgutils.h"
31
#include "libavutil/mem.h"
32
#include "libavutil/opt.h"
33
#include "libavutil/thread.h"
34
#include "avcodec.h"
35
#include "decode.h"
36
#include "encode.h"
37
#include "frame_thread_encoder.h"
38
#include "internal.h"
39
#include "thread.h"
40
41
#include "libavutil/ffversion.h"
42
const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
43
44
6104
unsigned avcodec_version(void)
45
{
46
    av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
47
    av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
48
    av_assert0(AV_CODEC_ID_SRT==94216);
49
    av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
50
51
6104
    return LIBAVCODEC_VERSION_INT;
52
}
53
54
6104
const char *avcodec_configuration(void)
55
{
56
6104
    return FFMPEG_CONFIGURATION;
57
}
58
59
const char *avcodec_license(void)
60
{
61
#define LICENSE_PREFIX "libavcodec license: "
62
    return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
63
}
64
65
47031
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
66
{
67
    int i;
68
69
1388693
    for (i = 0; i < count; i++) {
70
1341662
        int r = func(c, (char *)arg + i * size);
71
1341662
        if (ret)
72
28403
            ret[i] = r;
73
    }
74
47031
    emms_c();
75
47031
    return 0;
76
}
77
78
6860
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
79
{
80
    int i;
81
82
213702
    for (i = 0; i < count; i++) {
83
206842
        int r = func(c, arg, i, 0);
84
206842
        if (ret)
85
50
            ret[i] = r;
86
    }
87
6860
    emms_c();
88
6860
    return 0;
89
}
90
91
static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
92
93
24809
static void lock_avcodec(const AVCodec *codec)
94
{
95

24809
    if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
96
12870
        ff_mutex_lock(&codec_mutex);
97
24809
}
98
99
24809
static void unlock_avcodec(const AVCodec *codec)
100
{
101

24809
    if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
102
12870
        ff_mutex_unlock(&codec_mutex);
103
24809
}
104
105
#if FF_API_LOCKMGR
106
int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
107
{
108
    return 0;
109
}
110
#endif
111
112
21418
static int64_t get_bit_rate(AVCodecContext *ctx)
113
{
114
    int64_t bit_rate;
115
    int bits_per_sample;
116
117
21418
    switch (ctx->codec_type) {
118
17836
    case AVMEDIA_TYPE_VIDEO:
119
    case AVMEDIA_TYPE_DATA:
120
    case AVMEDIA_TYPE_SUBTITLE:
121
    case AVMEDIA_TYPE_ATTACHMENT:
122
17836
        bit_rate = ctx->bit_rate;
123
17836
        break;
124
3582
    case AVMEDIA_TYPE_AUDIO:
125
3582
        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
126
3582
        if (bits_per_sample) {
127
2128
            bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
128
2128
            if (bit_rate > INT64_MAX / bits_per_sample) {
129
                bit_rate = 0;
130
            } else
131
2128
                bit_rate *= bits_per_sample;
132
        } else
133
1454
            bit_rate = ctx->bit_rate;
134
3582
        break;
135
    default:
136
        bit_rate = 0;
137
        break;
138
    }
139
21418
    return bit_rate;
140
}
141
142
18815
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
143
{
144
18815
    int ret = 0;
145
18815
    int codec_init_ok = 0;
146
18815
    AVDictionary *tmp = NULL;
147
    AVCodecInternal *avci;
148
149
18815
    if (avcodec_is_open(avctx))
150
        return 0;
151
152

18815
    if (!codec && !avctx->codec) {
153
        av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
154
        return AVERROR(EINVAL);
155
    }
156

18815
    if (codec && avctx->codec && codec != avctx->codec) {
157
        av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
158
                                    "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
159
        return AVERROR(EINVAL);
160
    }
161
18815
    if (!codec)
162
2
        codec = avctx->codec;
163
164

18815
    if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
165
18815
        avctx->codec_id == AV_CODEC_ID_NONE) {
166
        avctx->codec_type = codec->type;
167
        avctx->codec_id   = codec->id;
168
    }
169

18815
    if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
170
                                         avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
171
        av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
172
        return AVERROR(EINVAL);
173
    }
174
18815
    avctx->codec = codec;
175
176

18815
    if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
177
        return AVERROR(EINVAL);
178
179
18815
    if (options)
180
18757
        av_dict_copy(&tmp, *options, 0);
181
182
18815
    lock_avcodec(codec);
183
184
18815
    avci = av_mallocz(sizeof(*avci));
185
18815
    if (!avci) {
186
        ret = AVERROR(ENOMEM);
187
        goto end;
188
    }
189
18815
    avctx->internal = avci;
190
191
#if FF_API_OLD_ENCDEC
192
18815
    avci->to_free = av_frame_alloc();
193
18815
    avci->compat_decode_frame = av_frame_alloc();
194
18815
    avci->compat_encode_packet = av_packet_alloc();
195

18815
    if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
196
        ret = AVERROR(ENOMEM);
197
        goto free_and_end;
198
    }
199
#endif
200
18815
    avci->buffer_frame = av_frame_alloc();
201
18815
    avci->buffer_pkt = av_packet_alloc();
202
18815
    avci->es.in_frame = av_frame_alloc();
203
18815
    avci->ds.in_pkt = av_packet_alloc();
204
18815
    avci->last_pkt_props = av_packet_alloc();
205
18815
    avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
206

18815
    if (!avci->buffer_frame || !avci->buffer_pkt          ||
207

18815
        !avci->es.in_frame  || !avci->ds.in_pkt           ||
208

18815
        !avci->last_pkt_props || !avci->pkt_props) {
209
        ret = AVERROR(ENOMEM);
210
        goto free_and_end;
211
    }
212
213
18815
    avci->skip_samples_multiplier = 1;
214
215
18815
    if (codec->priv_data_size > 0) {
216
13398
        if (!avctx->priv_data) {
217
6697
            avctx->priv_data = av_mallocz(codec->priv_data_size);
218
6697
            if (!avctx->priv_data) {
219
                ret = AVERROR(ENOMEM);
220
                goto free_and_end;
221
            }
222
6697
            if (codec->priv_class) {
223
1733
                *(const AVClass **)avctx->priv_data = codec->priv_class;
224
1733
                av_opt_set_defaults(avctx->priv_data);
225
            }
226
        }
227

13398
        if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
228
            goto free_and_end;
229
    } else {
230
5417
        avctx->priv_data = NULL;
231
    }
232
18815
    if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
233
        goto free_and_end;
234
235

18815
    if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
236
        av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
237
        ret = AVERROR(EINVAL);
238
        goto free_and_end;
239
    }
240
241
    // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
242


18815
    if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
243

138
          (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
244

18815
        if (avctx->coded_width && avctx->coded_height)
245
148
            ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
246

18667
        else if (avctx->width && avctx->height)
247
11235
            ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
248
18815
        if (ret < 0)
249
            goto free_and_end;
250
    }
251
252


18815
    if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
253
11383
        && (  av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
254
11383
           || av_image_check_size2(avctx->width,       avctx->height,       avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
255
        av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
256
        ff_set_dimensions(avctx, 0, 0);
257
    }
258
259

18815
    if (avctx->width > 0 && avctx->height > 0) {
260
11383
        if (av_image_check_sar(avctx->width, avctx->height,
261
                               avctx->sample_aspect_ratio) < 0) {
262
            av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
263
                   avctx->sample_aspect_ratio.num,
264
                   avctx->sample_aspect_ratio.den);
265
            avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
266
        }
267
    }
268
269

18815
    if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
270
        av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
271
        ret = AVERROR(EINVAL);
272
        goto free_and_end;
273
    }
274
275
18815
    if (avctx->sample_rate < 0) {
276
        av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
277
        ret = AVERROR(EINVAL);
278
        goto free_and_end;
279
    }
280
18815
    if (avctx->block_align < 0) {
281
        av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
282
        ret = AVERROR(EINVAL);
283
        goto free_and_end;
284
    }
285
286
18815
    avctx->frame_number = 0;
287
18815
    avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
288
289
18815
    if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
290
7
        avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
291
        const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
292
        const AVCodec *codec2;
293
        av_log(avctx, AV_LOG_ERROR,
294
               "The %s '%s' is experimental but experimental codecs are not enabled, "
295
               "add '-strict %d' if you want to use it.\n",
296
               codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
297
        codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
298
        if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
299
            av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
300
                codec_string, codec2->name);
301
        ret = AVERROR_EXPERIMENTAL;
302
        goto free_and_end;
303
    }
304
305
18815
    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
306

3964
        (!avctx->time_base.num || !avctx->time_base.den)) {
307
2836
        avctx->time_base.num = 1;
308
2836
        avctx->time_base.den = avctx->sample_rate;
309
    }
310
311
18815
    if (av_codec_is_encoder(avctx->codec))
312
5994
        ret = ff_encode_preinit(avctx);
313
    else
314
12821
        ret = ff_decode_preinit(avctx);
315
18815
    if (ret < 0)
316
        goto free_and_end;
317
318
    if (!HAVE_THREADS)
319
        av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
320
321
18815
    if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
322
5994
        unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
323
5994
        ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
324
5994
        lock_avcodec(codec);
325
5994
        if (ret < 0)
326
            goto free_and_end;
327
    }
328
329
18815
    if (HAVE_THREADS
330

18815
        && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
331
18797
        ret = ff_thread_init(avctx);
332
18797
        if (ret < 0) {
333
            goto free_and_end;
334
        }
335
    }
336
    if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
337
        avctx->thread_count = 1;
338
339

18815
    if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
340
30
        || avci->frame_thread_encoder)) {
341
13756
        ret = avctx->codec->init(avctx);
342
13756
        if (ret < 0) {
343
8
            codec_init_ok = -1;
344
8
            goto free_and_end;
345
        }
346
13748
        codec_init_ok = 1;
347
    }
348
349
18807
    ret=0;
350
351
18807
    if (av_codec_is_decoder(avctx->codec)) {
352
12813
        if (!avctx->bit_rate)
353
8635
            avctx->bit_rate = get_bit_rate(avctx);
354
        /* validate channel layout from the decoder */
355
12813
        if (avctx->channel_layout) {
356
1694
            int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
357
1694
            if (!avctx->channels)
358
16
                avctx->channels = channels;
359
1678
            else if (channels != avctx->channels) {
360
                char buf[512];
361
7
                av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
362
7
                av_log(avctx, AV_LOG_WARNING,
363
                       "Channel layout '%s' with %d channels does not match specified number of channels %d: "
364
                       "ignoring specified channel layout\n",
365
                       buf, channels, avctx->channels);
366
7
                avctx->channel_layout = 0;
367
            }
368
        }
369

12813
        if (avctx->channels && avctx->channels < 0 ||
370
12813
            avctx->channels > FF_SANE_NB_CHANNELS) {
371
            ret = AVERROR(EINVAL);
372
            goto free_and_end;
373
        }
374
12813
        if (avctx->bits_per_coded_sample < 0) {
375
            ret = AVERROR(EINVAL);
376
            goto free_and_end;
377
        }
378
379
#if FF_API_AVCTX_TIMEBASE
380

12813
        if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
381
4596
            avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
382
#endif
383
    }
384

18807
    if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
385
3934
        av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
386
    }
387
388
18807
end:
389
18815
    unlock_avcodec(codec);
390
18815
    if (options) {
391
18757
        av_dict_free(options);
392
18757
        *options = tmp;
393
    }
394
395
18815
    return ret;
396
8
free_and_end:
397

8
    if (avctx->codec && avctx->codec->close &&
398
6
        (codec_init_ok > 0 || (codec_init_ok < 0 &&
399
6
         avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
400
        avctx->codec->close(avctx);
401
402
8
    if (HAVE_THREADS && avci->thread_ctx)
403
        ff_thread_free(avctx);
404
405

8
    if (codec->priv_class && avctx->priv_data)
406
        av_opt_free(avctx->priv_data);
407
8
    av_opt_free(avctx);
408
409
8
    if (av_codec_is_encoder(avctx->codec)) {
410
#if FF_API_CODED_FRAME
411
FF_DISABLE_DEPRECATION_WARNINGS
412
        av_frame_free(&avctx->coded_frame);
413
FF_ENABLE_DEPRECATION_WARNINGS
414
#endif
415
        av_freep(&avctx->extradata);
416
        avctx->extradata_size = 0;
417
    }
418
419
8
    av_dict_free(&tmp);
420
8
    av_freep(&avctx->priv_data);
421
8
    av_freep(&avctx->subtitle_header);
422
423
#if FF_API_OLD_ENCDEC
424
8
    av_frame_free(&avci->to_free);
425
8
    av_frame_free(&avci->compat_decode_frame);
426
8
    av_packet_free(&avci->compat_encode_packet);
427
#endif
428
8
    av_frame_free(&avci->buffer_frame);
429
8
    av_packet_free(&avci->buffer_pkt);
430
8
    av_packet_free(&avci->last_pkt_props);
431
8
    av_fifo_freep(&avci->pkt_props);
432
433
8
    av_packet_free(&avci->ds.in_pkt);
434
8
    av_frame_free(&avci->es.in_frame);
435
8
    av_bsf_free(&avci->bsf);
436
437
8
    av_buffer_unref(&avci->pool);
438
8
    av_freep(&avci);
439
8
    avctx->internal = NULL;
440
8
    avctx->codec = NULL;
441
8
    goto end;
442
}
443
444
28
void avcodec_flush_buffers(AVCodecContext *avctx)
445
{
446
28
    AVCodecInternal *avci = avctx->internal;
447
448
28
    if (av_codec_is_encoder(avctx->codec)) {
449
        int caps = avctx->codec->capabilities;
450
451
        if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
452
            // Only encoders that explicitly declare support for it can be
453
            // flushed. Otherwise, this is a no-op.
454
            av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
455
                   "that doesn't support it\n");
456
            return;
457
        }
458
459
        // We haven't implemented flushing for frame-threaded encoders.
460
        av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
461
    }
462
463
28
    avci->draining      = 0;
464
28
    avci->draining_done = 0;
465
28
    avci->nb_draining_errors = 0;
466
28
    av_frame_unref(avci->buffer_frame);
467
#if FF_API_OLD_ENCDEC
468
28
    av_frame_unref(avci->compat_decode_frame);
469
28
    av_packet_unref(avci->compat_encode_packet);
470
#endif
471
28
    av_packet_unref(avci->buffer_pkt);
472
473
28
    av_packet_unref(avci->last_pkt_props);
474
28
    while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
475
        av_fifo_generic_read(avci->pkt_props,
476
                             avci->last_pkt_props, sizeof(*avci->last_pkt_props),
477
                             NULL);
478
        av_packet_unref(avci->last_pkt_props);
479
    }
480
28
    av_fifo_reset(avci->pkt_props);
481
482
28
    av_frame_unref(avci->es.in_frame);
483
28
    av_packet_unref(avci->ds.in_pkt);
484
485
28
    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
486
        ff_thread_flush(avctx);
487
28
    else if (avctx->codec->flush)
488
        avctx->codec->flush(avctx);
489
490
28
    avctx->pts_correction_last_pts =
491
28
    avctx->pts_correction_last_dts = INT64_MIN;
492
493
28
    if (av_codec_is_decoder(avctx->codec))
494
28
        av_bsf_flush(avci->bsf);
495
496
#if FF_API_OLD_ENCDEC
497
FF_DISABLE_DEPRECATION_WARNINGS
498
28
    if (!avctx->refcounted_frames)
499
28
        av_frame_unref(avci->to_free);
500
FF_ENABLE_DEPRECATION_WARNINGS
501
#endif
502
}
503
504
7126
void avsubtitle_free(AVSubtitle *sub)
505
{
506
    int i;
507
508
7870
    for (i = 0; i < sub->num_rects; i++) {
509
744
        av_freep(&sub->rects[i]->data[0]);
510
744
        av_freep(&sub->rects[i]->data[1]);
511
744
        av_freep(&sub->rects[i]->data[2]);
512
744
        av_freep(&sub->rects[i]->data[3]);
513
744
        av_freep(&sub->rects[i]->text);
514
744
        av_freep(&sub->rects[i]->ass);
515
744
        av_freep(&sub->rects[i]);
516
    }
517
518
7126
    av_freep(&sub->rects);
519
520
7126
    memset(sub, 0, sizeof(*sub));
521
7126
}
522
523
64702
av_cold int avcodec_close(AVCodecContext *avctx)
524
{
525
    int i;
526
527
64702
    if (!avctx)
528
        return 0;
529
530
64702
    if (avcodec_is_open(avctx)) {
531
18807
        AVCodecInternal *avci = avctx->internal;
532
533
18807
        if (CONFIG_FRAME_THREAD_ENCODER &&
534

18807
            avci->frame_thread_encoder && avctx->thread_count > 1) {
535
18
            ff_frame_thread_encoder_free(avctx);
536
        }
537
18807
        if (HAVE_THREADS && avci->thread_ctx)
538
28
            ff_thread_free(avctx);
539

18807
        if (avctx->codec && avctx->codec->close)
540
6161
            avctx->codec->close(avctx);
541
18807
        avci->byte_buffer_size = 0;
542
18807
        av_freep(&avci->byte_buffer);
543
#if FF_API_OLD_ENCDEC
544
18807
        av_frame_free(&avci->to_free);
545
18807
        av_frame_free(&avci->compat_decode_frame);
546
18807
        av_packet_free(&avci->compat_encode_packet);
547
#endif
548
18807
        av_frame_free(&avci->buffer_frame);
549
18807
        av_packet_free(&avci->buffer_pkt);
550
18807
        av_packet_unref(avci->last_pkt_props);
551
18807
        while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
552
            av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
553
                                 sizeof(*avci->last_pkt_props), NULL);
554
            av_packet_unref(avci->last_pkt_props);
555
        }
556
18807
        av_packet_free(&avci->last_pkt_props);
557
18807
        av_fifo_freep(&avci->pkt_props);
558
559
18807
        av_packet_free(&avci->ds.in_pkt);
560
18807
        av_frame_free(&avci->es.in_frame);
561
562
18807
        av_buffer_unref(&avci->pool);
563
564

18807
        if (avctx->hwaccel && avctx->hwaccel->uninit)
565
            avctx->hwaccel->uninit(avctx);
566
18807
        av_freep(&avci->hwaccel_priv_data);
567
568
18807
        av_bsf_free(&avci->bsf);
569
570
18807
        av_freep(&avctx->internal);
571
    }
572
573
65098
    for (i = 0; i < avctx->nb_coded_side_data; i++)
574
396
        av_freep(&avctx->coded_side_data[i].data);
575
64702
    av_freep(&avctx->coded_side_data);
576
64702
    avctx->nb_coded_side_data = 0;
577
578
64702
    av_buffer_unref(&avctx->hw_frames_ctx);
579
64702
    av_buffer_unref(&avctx->hw_device_ctx);
580
581

64702
    if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
582
4117
        av_opt_free(avctx->priv_data);
583
64702
    av_opt_free(avctx);
584
64702
    av_freep(&avctx->priv_data);
585
64702
    if (av_codec_is_encoder(avctx->codec)) {
586
5996
        av_freep(&avctx->extradata);
587
#if FF_API_CODED_FRAME
588
FF_DISABLE_DEPRECATION_WARNINGS
589
5996
        av_frame_free(&avctx->coded_frame);
590
FF_ENABLE_DEPRECATION_WARNINGS
591
#endif
592
    }
593
64702
    avctx->codec = NULL;
594
64702
    avctx->active_thread_type = 0;
595
596
64702
    return 0;
597
}
598
599
11575
static const char *unknown_if_null(const char *str)
600
{
601
11575
    return str ? str : "unknown";
602
}
603
604
12786
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
605
{
606
    const char *codec_type;
607
    const char *codec_name;
608
12786
    const char *profile = NULL;
609
    AVBPrint bprint;
610
    int64_t bitrate;
611
12786
    int new_line = 0;
612
    AVRational display_aspect_ratio;
613
12786
    const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
614
    const char *str;
615
616

12786
    if (!buf || buf_size <= 0)
617
3
        return;
618
12786
    av_bprint_init_for_buffer(&bprint, buf, buf_size);
619
12786
    codec_type = av_get_media_type_string(enc->codec_type);
620
12786
    codec_name = avcodec_get_name(enc->codec_id);
621
12786
    profile = avcodec_profile_name(enc->codec_id, enc->profile);
622
623
12786
    av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
624
               codec_name);
625
12786
    buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
626
627

12786
    if (enc->codec && strcmp(enc->codec->name, codec_name))
628
1
        av_bprintf(&bprint, " (%s)", enc->codec->name);
629
630
12786
    if (profile)
631
1601
        av_bprintf(&bprint, " (%s)", profile);
632
12786
    if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
633
9880
        && av_log_get_level() >= AV_LOG_VERBOSE
634
        && enc->refs)
635
        av_bprintf(&bprint, ", %d reference frame%s",
636
                   enc->refs, enc->refs > 1 ? "s" : "");
637
638
12786
    if (enc->codec_tag)
639
7352
        av_bprintf(&bprint, " (%s / 0x%04X)",
640
7352
                   av_fourcc2str(enc->codec_tag), enc->codec_tag);
641
642

12786
    switch (enc->codec_type) {
643
9880
    case AVMEDIA_TYPE_VIDEO:
644
        {
645
            unsigned len;
646
647
9880
            av_bprintf(&bprint, "%s%s", separator,
648
9880
                       enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
649
9799
                       unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
650
651
9880
            av_bprint_chars(&bprint, '(', 1);
652
9880
            len = bprint.len;
653
654
            /* The following check ensures that '(' has been written
655
             * and therefore allows us to erase it if it turns out
656
             * to be unnecessary. */
657
9880
            if (!av_bprint_is_complete(&bprint))
658
                return;
659
660

9880
            if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
661
3460
                enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
662
9
                av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
663

14429
            if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
664
4549
                (str = av_color_range_name(enc->color_range)))
665
4549
                av_bprintf(&bprint, "%s, ", str);
666
667
9880
            if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
668
9306
                enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
669
9306
                enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
670
592
                const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
671
592
                const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
672
592
                const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
673

592
                if (strcmp(col, pri) || strcmp(col, trc)) {
674
462
                    new_line = 1;
675
462
                    av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
676
                } else
677
130
                    av_bprintf(&bprint, "%s, ", col);
678
            }
679
680
9880
            if (enc->field_order != AV_FIELD_UNKNOWN) {
681
5236
                const char *field_order = "progressive";
682
5236
                if (enc->field_order == AV_FIELD_TT)
683
108
                    field_order = "top first";
684
5128
                else if (enc->field_order == AV_FIELD_BB)
685
35
                    field_order = "bottom first";
686
5093
                else if (enc->field_order == AV_FIELD_TB)
687
319
                    field_order = "top coded first (swapped)";
688
4774
                else if (enc->field_order == AV_FIELD_BT)
689
73
                    field_order = "bottom coded first (swapped)";
690
691
5236
                av_bprintf(&bprint, "%s, ", field_order);
692
            }
693
694
9880
            if (av_log_get_level() >= AV_LOG_VERBOSE &&
695
                enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
696
                (str = av_chroma_location_name(enc->chroma_sample_location)))
697
                av_bprintf(&bprint, "%s, ", str);
698
699
9880
            if (len == bprint.len) {
700
3790
                bprint.str[len - 1] = '\0';
701
3790
                bprint.len--;
702
            } else {
703
6090
                if (bprint.len - 2 < bprint.size) {
704
                    /* Erase the last ", " */
705
6090
                    bprint.len -= 2;
706
6090
                    bprint.str[bprint.len] = '\0';
707
                }
708
6090
                av_bprint_chars(&bprint, ')', 1);
709
            }
710
        }
711
712
9880
        if (enc->width) {
713
9870
            av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
714
                       enc->width, enc->height);
715
716
9870
            if (av_log_get_level() >= AV_LOG_VERBOSE &&
717
                (enc->width != enc->coded_width ||
718
                 enc->height != enc->coded_height))
719
                av_bprintf(&bprint, " (%dx%d)",
720
                           enc->coded_width, enc->coded_height);
721
722
9870
            if (enc->sample_aspect_ratio.num) {
723
1280
                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
724
1280
                          enc->width * (int64_t)enc->sample_aspect_ratio.num,
725
1280
                          enc->height * (int64_t)enc->sample_aspect_ratio.den,
726
                          1024 * 1024);
727
1280
                av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
728
                         enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
729
                         display_aspect_ratio.num, display_aspect_ratio.den);
730
            }
731
9870
            if (av_log_get_level() >= AV_LOG_DEBUG) {
732
                int g = av_gcd(enc->time_base.num, enc->time_base.den);
733
                av_bprintf(&bprint, ", %d/%d",
734
                           enc->time_base.num / g, enc->time_base.den / g);
735
            }
736
        }
737
9880
        if (encode) {
738
4879
            av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
739
        } else {
740
5001
            if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
741
2
                av_bprintf(&bprint, ", Closed Captions");
742
5001
            if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
743
19
                av_bprintf(&bprint, ", lossless");
744
        }
745
9880
        break;
746
2729
    case AVMEDIA_TYPE_AUDIO:
747
2729
        av_bprintf(&bprint, "%s", separator);
748
749
2729
        if (enc->sample_rate) {
750
2723
            av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
751
        }
752
2729
        av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout);
753

5457
        if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
754
2728
            (str = av_get_sample_fmt_name(enc->sample_fmt))) {
755
2728
            av_bprintf(&bprint, ", %s", str);
756
        }
757
2729
        if (   enc->bits_per_raw_sample > 0
758
414
            && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
759
123
            av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
760
2729
        if (av_log_get_level() >= AV_LOG_VERBOSE) {
761
4
            if (enc->initial_padding)
762
2
                av_bprintf(&bprint, ", delay %d", enc->initial_padding);
763
4
            if (enc->trailing_padding)
764
                av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
765
        }
766
2729
        break;
767
66
    case AVMEDIA_TYPE_DATA:
768
66
        if (av_log_get_level() >= AV_LOG_DEBUG) {
769
            int g = av_gcd(enc->time_base.num, enc->time_base.den);
770
            if (g)
771
                av_bprintf(&bprint, ", %d/%d",
772
                           enc->time_base.num / g, enc->time_base.den / g);
773
        }
774
66
        break;
775
108
    case AVMEDIA_TYPE_SUBTITLE:
776
108
        if (enc->width)
777
17
            av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
778
108
        break;
779
3
    default:
780
3
        return;
781
    }
782
12783
    if (encode) {
783
6190
        if (enc->flags & AV_CODEC_FLAG_PASS1)
784
            av_bprintf(&bprint, ", pass 1");
785
6190
        if (enc->flags & AV_CODEC_FLAG_PASS2)
786
            av_bprintf(&bprint, ", pass 2");
787
    }
788
12783
    bitrate = get_bit_rate(enc);
789
12783
    if (bitrate != 0) {
790
8294
        av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
791
4489
    } else if (enc->rc_max_rate > 0) {
792
        av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
793
    }
794
}
795
796
2619533
int avcodec_is_open(AVCodecContext *s)
797
{
798
2619533
    return !!s->internal;
799
}