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