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