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