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