]> git.sesse.net Git - ffmpeg/blob - libavcodec/avcodec.c
1e40e2298bb141a4f46373ce479c77bf38d67221
[ffmpeg] / libavcodec / avcodec.c
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/imgutils.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/thread.h"
33 #include "avcodec.h"
34 #include "decode.h"
35 #include "encode.h"
36 #include "frame_thread_encoder.h"
37 #include "internal.h"
38 #include "thread.h"
39 #if CONFIG_ICONV
40 # include <iconv.h>
41 #endif
42
43 #include "libavutil/ffversion.h"
44 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
45
46 unsigned avcodec_version(void)
47 {
48     av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
49     av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
50     av_assert0(AV_CODEC_ID_SRT==94216);
51     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
52
53     return LIBAVCODEC_VERSION_INT;
54 }
55
56 const char *avcodec_configuration(void)
57 {
58     return FFMPEG_CONFIGURATION;
59 }
60
61 const char *avcodec_license(void)
62 {
63 #define LICENSE_PREFIX "libavcodec license: "
64     return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
65 }
66
67 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
68 {
69     int i;
70
71     for (i = 0; i < count; i++) {
72         int r = func(c, (char *)arg + i * size);
73         if (ret)
74             ret[i] = r;
75     }
76     emms_c();
77     return 0;
78 }
79
80 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
81 {
82     int i;
83
84     for (i = 0; i < count; i++) {
85         int r = func(c, arg, i, 0);
86         if (ret)
87             ret[i] = r;
88     }
89     emms_c();
90     return 0;
91 }
92
93 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
94
95 static void lock_avcodec(const AVCodec *codec)
96 {
97     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
98         ff_mutex_lock(&codec_mutex);
99 }
100
101 static void unlock_avcodec(const AVCodec *codec)
102 {
103     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
104         ff_mutex_unlock(&codec_mutex);
105 }
106
107 #if FF_API_LOCKMGR
108 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
109 {
110     return 0;
111 }
112 #endif
113
114 static int64_t get_bit_rate(AVCodecContext *ctx)
115 {
116     int64_t bit_rate;
117     int bits_per_sample;
118
119     switch (ctx->codec_type) {
120     case AVMEDIA_TYPE_VIDEO:
121     case AVMEDIA_TYPE_DATA:
122     case AVMEDIA_TYPE_SUBTITLE:
123     case AVMEDIA_TYPE_ATTACHMENT:
124         bit_rate = ctx->bit_rate;
125         break;
126     case AVMEDIA_TYPE_AUDIO:
127         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
128         if (bits_per_sample) {
129             bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
130             if (bit_rate > INT64_MAX / bits_per_sample) {
131                 bit_rate = 0;
132             } else
133                 bit_rate *= bits_per_sample;
134         } else
135             bit_rate = ctx->bit_rate;
136         break;
137     default:
138         bit_rate = 0;
139         break;
140     }
141     return bit_rate;
142 }
143
144 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
145 {
146     int ret = 0;
147     int codec_init_ok = 0;
148     AVDictionary *tmp = NULL;
149     AVCodecInternal *avci;
150
151     if (avcodec_is_open(avctx))
152         return 0;
153
154     if (!codec && !avctx->codec) {
155         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
156         return AVERROR(EINVAL);
157     }
158     if (codec && avctx->codec && codec != avctx->codec) {
159         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
160                                     "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
161         return AVERROR(EINVAL);
162     }
163     if (!codec)
164         codec = avctx->codec;
165
166     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
167         avctx->codec_id == AV_CODEC_ID_NONE) {
168         avctx->codec_type = codec->type;
169         avctx->codec_id   = codec->id;
170     }
171     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
172                                          avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
173         av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
174         return AVERROR(EINVAL);
175     }
176     avctx->codec = codec;
177
178     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
179         return AVERROR(EINVAL);
180
181     if (options)
182         av_dict_copy(&tmp, *options, 0);
183
184     lock_avcodec(codec);
185
186     avci = av_mallocz(sizeof(*avci));
187     if (!avci) {
188         ret = AVERROR(ENOMEM);
189         goto end;
190     }
191     avctx->internal = avci;
192
193 #if FF_API_OLD_ENCDEC
194     avci->to_free = av_frame_alloc();
195     avci->compat_decode_frame = av_frame_alloc();
196     avci->compat_encode_packet = av_packet_alloc();
197     if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
198         ret = AVERROR(ENOMEM);
199         goto free_and_end;
200     }
201 #endif
202     avci->buffer_frame = av_frame_alloc();
203     avci->buffer_pkt = av_packet_alloc();
204     avci->es.in_frame = av_frame_alloc();
205     avci->ds.in_pkt = av_packet_alloc();
206     avci->last_pkt_props = av_packet_alloc();
207     avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
208     if (!avci->buffer_frame || !avci->buffer_pkt          ||
209         !avci->es.in_frame  || !avci->ds.in_pkt           ||
210         !avci->last_pkt_props || !avci->pkt_props) {
211         ret = AVERROR(ENOMEM);
212         goto free_and_end;
213     }
214
215     avci->skip_samples_multiplier = 1;
216
217     if (codec->priv_data_size > 0) {
218         if (!avctx->priv_data) {
219             avctx->priv_data = av_mallocz(codec->priv_data_size);
220             if (!avctx->priv_data) {
221                 ret = AVERROR(ENOMEM);
222                 goto free_and_end;
223             }
224             if (codec->priv_class) {
225                 *(const AVClass **)avctx->priv_data = codec->priv_class;
226                 av_opt_set_defaults(avctx->priv_data);
227             }
228         }
229         if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
230             goto free_and_end;
231     } else {
232         avctx->priv_data = NULL;
233     }
234     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
235         goto free_and_end;
236
237     if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
238         av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
239         ret = AVERROR(EINVAL);
240         goto free_and_end;
241     }
242
243     // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
244     if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
245           (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
246         if (avctx->coded_width && avctx->coded_height)
247             ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
248         else if (avctx->width && avctx->height)
249             ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
250         if (ret < 0)
251             goto free_and_end;
252     }
253
254     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
255         && (  av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
256            || av_image_check_size2(avctx->width,       avctx->height,       avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
257         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
258         ff_set_dimensions(avctx, 0, 0);
259     }
260
261     if (avctx->width > 0 && avctx->height > 0) {
262         if (av_image_check_sar(avctx->width, avctx->height,
263                                avctx->sample_aspect_ratio) < 0) {
264             av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
265                    avctx->sample_aspect_ratio.num,
266                    avctx->sample_aspect_ratio.den);
267             avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
268         }
269     }
270
271     if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
272         av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
273         ret = AVERROR(EINVAL);
274         goto free_and_end;
275     }
276     if (av_codec_is_decoder(codec) &&
277         codec->type == AVMEDIA_TYPE_AUDIO &&
278         !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF) &&
279         avctx->channels == 0) {
280         av_log(avctx, AV_LOG_ERROR, "Decoder requires channel count but channels not set\n");
281         ret = AVERROR(EINVAL);
282         goto free_and_end;
283     }
284
285     if (avctx->sample_rate < 0) {
286         av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
287         ret = AVERROR(EINVAL);
288         goto free_and_end;
289     }
290     if (avctx->block_align < 0) {
291         av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
292         ret = AVERROR(EINVAL);
293         goto free_and_end;
294     }
295
296     avctx->frame_number = 0;
297     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
298
299     if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
300         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
301         const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
302         const AVCodec *codec2;
303         av_log(avctx, AV_LOG_ERROR,
304                "The %s '%s' is experimental but experimental codecs are not enabled, "
305                "add '-strict %d' if you want to use it.\n",
306                codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
307         codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
308         if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
309             av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
310                 codec_string, codec2->name);
311         ret = AVERROR_EXPERIMENTAL;
312         goto free_and_end;
313     }
314
315     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
316         (!avctx->time_base.num || !avctx->time_base.den)) {
317         avctx->time_base.num = 1;
318         avctx->time_base.den = avctx->sample_rate;
319     }
320
321     if (!HAVE_THREADS)
322         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
323
324     if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
325         unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
326         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
327         lock_avcodec(codec);
328         if (ret < 0)
329             goto free_and_end;
330     }
331
332     if (HAVE_THREADS
333         && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
334         ret = ff_thread_init(avctx);
335         if (ret < 0) {
336             goto free_and_end;
337         }
338     }
339     if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
340         avctx->thread_count = 1;
341
342     if (av_codec_is_encoder(avctx->codec))
343         ret = ff_encode_preinit(avctx);
344     else
345         ret = ff_decode_preinit(avctx);
346     if (ret < 0)
347         goto free_and_end;
348
349     if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
350         || avci->frame_thread_encoder)) {
351         ret = avctx->codec->init(avctx);
352         if (ret < 0) {
353             codec_init_ok = -1;
354             goto free_and_end;
355         }
356         codec_init_ok = 1;
357     }
358
359     ret=0;
360
361     if (av_codec_is_decoder(avctx->codec)) {
362         if (!avctx->bit_rate)
363             avctx->bit_rate = get_bit_rate(avctx);
364         /* validate channel layout from the decoder */
365         if (avctx->channel_layout) {
366             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
367             if (!avctx->channels)
368                 avctx->channels = channels;
369             else if (channels != avctx->channels) {
370                 char buf[512];
371                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
372                 av_log(avctx, AV_LOG_WARNING,
373                        "Channel layout '%s' with %d channels does not match specified number of channels %d: "
374                        "ignoring specified channel layout\n",
375                        buf, channels, avctx->channels);
376                 avctx->channel_layout = 0;
377             }
378         }
379         if (avctx->channels && avctx->channels < 0 ||
380             avctx->channels > FF_SANE_NB_CHANNELS) {
381             ret = AVERROR(EINVAL);
382             goto free_and_end;
383         }
384         if (avctx->bits_per_coded_sample < 0) {
385             ret = AVERROR(EINVAL);
386             goto free_and_end;
387         }
388         if (avctx->sub_charenc) {
389             if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
390                 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
391                        "supported with subtitles codecs\n");
392                 ret = AVERROR(EINVAL);
393                 goto free_and_end;
394             } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
395                 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
396                        "subtitles character encoding will be ignored\n",
397                        avctx->codec_descriptor->name);
398                 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
399             } else {
400                 /* input character encoding is set for a text based subtitle
401                  * codec at this point */
402                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
403                     avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
404
405                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
406 #if CONFIG_ICONV
407                     iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
408                     if (cd == (iconv_t)-1) {
409                         ret = AVERROR(errno);
410                         av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
411                                "with input character encoding \"%s\"\n", avctx->sub_charenc);
412                         goto free_and_end;
413                     }
414                     iconv_close(cd);
415 #else
416                     av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
417                            "conversion needs a libavcodec built with iconv support "
418                            "for this codec\n");
419                     ret = AVERROR(ENOSYS);
420                     goto free_and_end;
421 #endif
422                 }
423             }
424         }
425
426 #if FF_API_AVCTX_TIMEBASE
427         if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
428             avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
429 #endif
430     }
431     if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
432         av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
433     }
434
435 end:
436     unlock_avcodec(codec);
437     if (options) {
438         av_dict_free(options);
439         *options = tmp;
440     }
441
442     return ret;
443 free_and_end:
444     if (avctx->codec && avctx->codec->close &&
445         (codec_init_ok > 0 || (codec_init_ok < 0 &&
446          avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
447         avctx->codec->close(avctx);
448
449     if (HAVE_THREADS && avci->thread_ctx)
450         ff_thread_free(avctx);
451
452     if (codec->priv_class && avctx->priv_data)
453         av_opt_free(avctx->priv_data);
454     av_opt_free(avctx);
455
456     if (av_codec_is_encoder(avctx->codec)) {
457 #if FF_API_CODED_FRAME
458 FF_DISABLE_DEPRECATION_WARNINGS
459         av_frame_free(&avctx->coded_frame);
460 FF_ENABLE_DEPRECATION_WARNINGS
461 #endif
462         av_freep(&avctx->extradata);
463         avctx->extradata_size = 0;
464     }
465
466     av_dict_free(&tmp);
467     av_freep(&avctx->priv_data);
468     av_freep(&avctx->subtitle_header);
469
470 #if FF_API_OLD_ENCDEC
471     av_frame_free(&avci->to_free);
472     av_frame_free(&avci->compat_decode_frame);
473     av_packet_free(&avci->compat_encode_packet);
474 #endif
475     av_frame_free(&avci->buffer_frame);
476     av_packet_free(&avci->buffer_pkt);
477     av_packet_free(&avci->last_pkt_props);
478     av_fifo_freep(&avci->pkt_props);
479
480     av_packet_free(&avci->ds.in_pkt);
481     av_frame_free(&avci->es.in_frame);
482     av_bsf_free(&avci->bsf);
483
484     av_buffer_unref(&avci->pool);
485     av_freep(&avci);
486     avctx->internal = NULL;
487     avctx->codec = NULL;
488     goto end;
489 }
490
491 void avcodec_flush_buffers(AVCodecContext *avctx)
492 {
493     AVCodecInternal *avci = avctx->internal;
494
495     if (av_codec_is_encoder(avctx->codec)) {
496         int caps = avctx->codec->capabilities;
497
498         if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
499             // Only encoders that explicitly declare support for it can be
500             // flushed. Otherwise, this is a no-op.
501             av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
502                    "that doesn't support it\n");
503             return;
504         }
505
506         // We haven't implemented flushing for frame-threaded encoders.
507         av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
508     }
509
510     avci->draining      = 0;
511     avci->draining_done = 0;
512     avci->nb_draining_errors = 0;
513     av_frame_unref(avci->buffer_frame);
514 #if FF_API_OLD_ENCDEC
515     av_frame_unref(avci->compat_decode_frame);
516     av_packet_unref(avci->compat_encode_packet);
517 #endif
518     av_packet_unref(avci->buffer_pkt);
519
520     av_packet_unref(avci->last_pkt_props);
521     while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
522         av_fifo_generic_read(avci->pkt_props,
523                              avci->last_pkt_props, sizeof(*avci->last_pkt_props),
524                              NULL);
525         av_packet_unref(avci->last_pkt_props);
526     }
527     av_fifo_reset(avci->pkt_props);
528
529     av_frame_unref(avci->es.in_frame);
530     av_packet_unref(avci->ds.in_pkt);
531
532     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
533         ff_thread_flush(avctx);
534     else if (avctx->codec->flush)
535         avctx->codec->flush(avctx);
536
537     avctx->pts_correction_last_pts =
538     avctx->pts_correction_last_dts = INT64_MIN;
539
540     if (av_codec_is_decoder(avctx->codec))
541         av_bsf_flush(avci->bsf);
542
543 #if FF_API_OLD_ENCDEC
544 FF_DISABLE_DEPRECATION_WARNINGS
545     if (!avctx->refcounted_frames)
546         av_frame_unref(avci->to_free);
547 FF_ENABLE_DEPRECATION_WARNINGS
548 #endif
549 }
550
551 void avsubtitle_free(AVSubtitle *sub)
552 {
553     int i;
554
555     for (i = 0; i < sub->num_rects; i++) {
556         av_freep(&sub->rects[i]->data[0]);
557         av_freep(&sub->rects[i]->data[1]);
558         av_freep(&sub->rects[i]->data[2]);
559         av_freep(&sub->rects[i]->data[3]);
560         av_freep(&sub->rects[i]->text);
561         av_freep(&sub->rects[i]->ass);
562         av_freep(&sub->rects[i]);
563     }
564
565     av_freep(&sub->rects);
566
567     memset(sub, 0, sizeof(*sub));
568 }
569
570 av_cold int avcodec_close(AVCodecContext *avctx)
571 {
572     int i;
573
574     if (!avctx)
575         return 0;
576
577     if (avcodec_is_open(avctx)) {
578         if (CONFIG_FRAME_THREAD_ENCODER &&
579             avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
580             ff_frame_thread_encoder_free(avctx);
581         }
582         if (HAVE_THREADS && avctx->internal->thread_ctx)
583             ff_thread_free(avctx);
584         if (avctx->codec && avctx->codec->close)
585             avctx->codec->close(avctx);
586         avctx->internal->byte_buffer_size = 0;
587         av_freep(&avctx->internal->byte_buffer);
588 #if FF_API_OLD_ENCDEC
589         av_frame_free(&avctx->internal->to_free);
590         av_frame_free(&avctx->internal->compat_decode_frame);
591         av_packet_free(&avctx->internal->compat_encode_packet);
592 #endif
593         av_frame_free(&avctx->internal->buffer_frame);
594         av_packet_free(&avctx->internal->buffer_pkt);
595         av_packet_unref(avctx->internal->last_pkt_props);
596         while (av_fifo_size(avctx->internal->pkt_props) >=
597                sizeof(*avctx->internal->last_pkt_props)) {
598             av_fifo_generic_read(avctx->internal->pkt_props,
599                                  avctx->internal->last_pkt_props,
600                                  sizeof(*avctx->internal->last_pkt_props),
601                                  NULL);
602             av_packet_unref(avctx->internal->last_pkt_props);
603         }
604         av_packet_free(&avctx->internal->last_pkt_props);
605         av_fifo_freep(&avctx->internal->pkt_props);
606
607         av_packet_free(&avctx->internal->ds.in_pkt);
608         av_frame_free(&avctx->internal->es.in_frame);
609
610         av_buffer_unref(&avctx->internal->pool);
611
612         if (avctx->hwaccel && avctx->hwaccel->uninit)
613             avctx->hwaccel->uninit(avctx);
614         av_freep(&avctx->internal->hwaccel_priv_data);
615
616         av_bsf_free(&avctx->internal->bsf);
617
618         av_freep(&avctx->internal);
619     }
620
621     for (i = 0; i < avctx->nb_coded_side_data; i++)
622         av_freep(&avctx->coded_side_data[i].data);
623     av_freep(&avctx->coded_side_data);
624     avctx->nb_coded_side_data = 0;
625
626     av_buffer_unref(&avctx->hw_frames_ctx);
627     av_buffer_unref(&avctx->hw_device_ctx);
628
629     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
630         av_opt_free(avctx->priv_data);
631     av_opt_free(avctx);
632     av_freep(&avctx->priv_data);
633     if (av_codec_is_encoder(avctx->codec)) {
634         av_freep(&avctx->extradata);
635 #if FF_API_CODED_FRAME
636 FF_DISABLE_DEPRECATION_WARNINGS
637         av_frame_free(&avctx->coded_frame);
638 FF_ENABLE_DEPRECATION_WARNINGS
639 #endif
640     }
641     avctx->codec = NULL;
642     avctx->active_thread_type = 0;
643
644     return 0;
645 }
646
647 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
648 {
649     const char *codec_type;
650     const char *codec_name;
651     const char *profile = NULL;
652     int64_t bitrate;
653     int new_line = 0;
654     AVRational display_aspect_ratio;
655     const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
656
657     if (!buf || buf_size <= 0)
658         return;
659     codec_type = av_get_media_type_string(enc->codec_type);
660     codec_name = avcodec_get_name(enc->codec_id);
661     profile = avcodec_profile_name(enc->codec_id, enc->profile);
662
663     snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
664              codec_name);
665     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
666
667     if (enc->codec && strcmp(enc->codec->name, codec_name))
668         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
669
670     if (profile)
671         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
672     if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
673         && av_log_get_level() >= AV_LOG_VERBOSE
674         && enc->refs)
675         snprintf(buf + strlen(buf), buf_size - strlen(buf),
676                  ", %d reference frame%s",
677                  enc->refs, enc->refs > 1 ? "s" : "");
678
679     if (enc->codec_tag)
680         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
681                  av_fourcc2str(enc->codec_tag), enc->codec_tag);
682
683     switch (enc->codec_type) {
684     case AVMEDIA_TYPE_VIDEO:
685         {
686             char detail[256] = "(";
687
688             av_strlcat(buf, separator, buf_size);
689
690             snprintf(buf + strlen(buf), buf_size - strlen(buf),
691                  "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
692                      av_get_pix_fmt_name(enc->pix_fmt));
693             if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
694                 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
695                 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
696             if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
697                 av_strlcatf(detail, sizeof(detail), "%s, ",
698                             av_color_range_name(enc->color_range));
699
700             if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
701                 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
702                 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
703                 if (enc->colorspace != (int)enc->color_primaries ||
704                     enc->colorspace != (int)enc->color_trc) {
705                     new_line = 1;
706                     av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
707                                 av_color_space_name(enc->colorspace),
708                                 av_color_primaries_name(enc->color_primaries),
709                                 av_color_transfer_name(enc->color_trc));
710                 } else
711                     av_strlcatf(detail, sizeof(detail), "%s, ",
712                                 av_get_colorspace_name(enc->colorspace));
713             }
714
715             if (enc->field_order != AV_FIELD_UNKNOWN) {
716                 const char *field_order = "progressive";
717                 if (enc->field_order == AV_FIELD_TT)
718                     field_order = "top first";
719                 else if (enc->field_order == AV_FIELD_BB)
720                     field_order = "bottom first";
721                 else if (enc->field_order == AV_FIELD_TB)
722                     field_order = "top coded first (swapped)";
723                 else if (enc->field_order == AV_FIELD_BT)
724                     field_order = "bottom coded first (swapped)";
725
726                 av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
727             }
728
729             if (av_log_get_level() >= AV_LOG_VERBOSE &&
730                 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
731                 av_strlcatf(detail, sizeof(detail), "%s, ",
732                             av_chroma_location_name(enc->chroma_sample_location));
733
734             if (strlen(detail) > 1) {
735                 detail[strlen(detail) - 2] = 0;
736                 av_strlcatf(buf, buf_size, "%s)", detail);
737             }
738         }
739
740         if (enc->width) {
741             av_strlcat(buf, new_line ? separator : ", ", buf_size);
742
743             snprintf(buf + strlen(buf), buf_size - strlen(buf),
744                      "%dx%d",
745                      enc->width, enc->height);
746
747             if (av_log_get_level() >= AV_LOG_VERBOSE &&
748                 (enc->width != enc->coded_width ||
749                  enc->height != enc->coded_height))
750                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
751                          " (%dx%d)", enc->coded_width, enc->coded_height);
752
753             if (enc->sample_aspect_ratio.num) {
754                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
755                           enc->width * (int64_t)enc->sample_aspect_ratio.num,
756                           enc->height * (int64_t)enc->sample_aspect_ratio.den,
757                           1024 * 1024);
758                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
759                          " [SAR %d:%d DAR %d:%d]",
760                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
761                          display_aspect_ratio.num, display_aspect_ratio.den);
762             }
763             if (av_log_get_level() >= AV_LOG_DEBUG) {
764                 int g = av_gcd(enc->time_base.num, enc->time_base.den);
765                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
766                          ", %d/%d",
767                          enc->time_base.num / g, enc->time_base.den / g);
768             }
769         }
770         if (encode) {
771             snprintf(buf + strlen(buf), buf_size - strlen(buf),
772                      ", q=%d-%d", enc->qmin, enc->qmax);
773         } else {
774             if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
775                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
776                          ", Closed Captions");
777             if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
778                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
779                          ", lossless");
780         }
781         break;
782     case AVMEDIA_TYPE_AUDIO:
783         av_strlcat(buf, separator, buf_size);
784
785         if (enc->sample_rate) {
786             snprintf(buf + strlen(buf), buf_size - strlen(buf),
787                      "%d Hz, ", enc->sample_rate);
788         }
789         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
790         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
791             snprintf(buf + strlen(buf), buf_size - strlen(buf),
792                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
793         }
794         if (   enc->bits_per_raw_sample > 0
795             && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
796             snprintf(buf + strlen(buf), buf_size - strlen(buf),
797                      " (%d bit)", enc->bits_per_raw_sample);
798         if (av_log_get_level() >= AV_LOG_VERBOSE) {
799             if (enc->initial_padding)
800                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
801                          ", delay %d", enc->initial_padding);
802             if (enc->trailing_padding)
803                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
804                          ", padding %d", enc->trailing_padding);
805         }
806         break;
807     case AVMEDIA_TYPE_DATA:
808         if (av_log_get_level() >= AV_LOG_DEBUG) {
809             int g = av_gcd(enc->time_base.num, enc->time_base.den);
810             if (g)
811                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
812                          ", %d/%d",
813                          enc->time_base.num / g, enc->time_base.den / g);
814         }
815         break;
816     case AVMEDIA_TYPE_SUBTITLE:
817         if (enc->width)
818             snprintf(buf + strlen(buf), buf_size - strlen(buf),
819                      ", %dx%d", enc->width, enc->height);
820         break;
821     default:
822         return;
823     }
824     if (encode) {
825         if (enc->flags & AV_CODEC_FLAG_PASS1)
826             snprintf(buf + strlen(buf), buf_size - strlen(buf),
827                      ", pass 1");
828         if (enc->flags & AV_CODEC_FLAG_PASS2)
829             snprintf(buf + strlen(buf), buf_size - strlen(buf),
830                      ", pass 2");
831     }
832     bitrate = get_bit_rate(enc);
833     if (bitrate != 0) {
834         snprintf(buf + strlen(buf), buf_size - strlen(buf),
835                  ", %"PRId64" kb/s", bitrate / 1000);
836     } else if (enc->rc_max_rate > 0) {
837         snprintf(buf + strlen(buf), buf_size - strlen(buf),
838                  ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
839     }
840 }
841
842 int avcodec_is_open(AVCodecContext *s)
843 {
844     return !!s->internal;
845 }