2 * AVCodecContext functions for libavcodec
4 * This file is part of FFmpeg.
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.
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.
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
23 * AVCodecContext functions for libavcodec
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"
36 #include "frame_thread_encoder.h"
40 #include "libavutil/ffversion.h"
41 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
43 unsigned avcodec_version(void)
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);
50 return LIBAVCODEC_VERSION_INT;
53 const char *avcodec_configuration(void)
55 return FFMPEG_CONFIGURATION;
58 const char *avcodec_license(void)
60 #define LICENSE_PREFIX "libavcodec license: "
61 return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
64 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
68 for (i = 0; i < count; i++) {
69 int r = func(c, (char *)arg + i * size);
77 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
81 for (i = 0; i < count; i++) {
82 int r = func(c, arg, i, 0);
90 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
92 static void lock_avcodec(const AVCodec *codec)
94 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
95 ff_mutex_lock(&codec_mutex);
98 static void unlock_avcodec(const AVCodec *codec)
100 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
101 ff_mutex_unlock(&codec_mutex);
105 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
111 static int64_t get_bit_rate(AVCodecContext *ctx)
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;
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) {
130 bit_rate *= bits_per_sample;
132 bit_rate = ctx->bit_rate;
141 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
144 int codec_init_ok = 0;
145 AVDictionary *tmp = NULL;
146 AVCodecInternal *avci;
148 if (avcodec_is_open(avctx))
151 if (!codec && !avctx->codec) {
152 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
153 return AVERROR(EINVAL);
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);
161 codec = avctx->codec;
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;
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);
173 avctx->codec = codec;
175 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
176 return AVERROR(EINVAL);
179 av_dict_copy(&tmp, *options, 0);
183 avci = av_mallocz(sizeof(*avci));
185 ret = AVERROR(ENOMEM);
188 avctx->internal = avci;
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);
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);
212 avci->skip_samples_multiplier = 1;
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);
221 if (codec->priv_class) {
222 *(const AVClass **)avctx->priv_data = codec->priv_class;
223 av_opt_set_defaults(avctx->priv_data);
226 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
229 avctx->priv_data = NULL;
231 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
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);
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);
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);
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 };
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);
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);
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);
285 avctx->frame_number = 0;
286 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
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;
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;
310 if (av_codec_is_encoder(avctx->codec))
311 ret = ff_encode_preinit(avctx);
313 ret = ff_decode_preinit(avctx);
318 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
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);
329 && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
330 ret = ff_thread_init(avctx);
335 if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
336 avctx->thread_count = 1;
338 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
339 || avci->frame_thread_encoder)) {
340 ret = avctx->codec->init(avctx);
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) {
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;
368 if (avctx->channels && avctx->channels < 0 ||
369 avctx->channels > FF_SANE_NB_CHANNELS) {
370 ret = AVERROR(EINVAL);
373 if (avctx->bits_per_coded_sample < 0) {
374 ret = AVERROR(EINVAL);
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}));
383 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
384 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
388 unlock_avcodec(codec);
390 av_dict_free(options);
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);
401 if (HAVE_THREADS && avci->thread_ctx)
402 ff_thread_free(avctx);
404 if (codec->priv_class && avctx->priv_data)
405 av_opt_free(avctx->priv_data);
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
414 av_freep(&avctx->extradata);
415 avctx->extradata_size = 0;
419 av_freep(&avctx->priv_data);
420 av_freep(&avctx->subtitle_header);
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);
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);
432 av_packet_free(&avci->ds.in_pkt);
433 av_frame_free(&avci->es.in_frame);
434 av_bsf_free(&avci->bsf);
436 av_buffer_unref(&avci->pool);
438 avctx->internal = NULL;
443 void avcodec_flush_buffers(AVCodecContext *avctx)
445 AVCodecInternal *avci = avctx->internal;
447 if (av_codec_is_encoder(avctx->codec)) {
448 int caps = avctx->codec->capabilities;
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");
458 // We haven't implemented flushing for frame-threaded encoders.
459 av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
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);
470 av_packet_unref(avci->buffer_pkt);
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),
477 av_packet_unref(avci->last_pkt_props);
479 av_fifo_reset(avci->pkt_props);
481 av_frame_unref(avci->es.in_frame);
482 av_packet_unref(avci->ds.in_pkt);
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);
489 avctx->pts_correction_last_pts =
490 avctx->pts_correction_last_dts = INT64_MIN;
492 if (av_codec_is_decoder(avctx->codec))
493 av_bsf_flush(avci->bsf);
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
503 void avsubtitle_free(AVSubtitle *sub)
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]);
517 av_freep(&sub->rects);
519 memset(sub, 0, sizeof(*sub));
522 av_cold int avcodec_close(AVCodecContext *avctx)
529 if (avcodec_is_open(avctx)) {
530 if (CONFIG_FRAME_THREAD_ENCODER &&
531 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
532 ff_frame_thread_encoder_free(avctx);
534 if (HAVE_THREADS && avctx->internal->thread_ctx)
535 ff_thread_free(avctx);
536 if (avctx->codec && avctx->codec->close)
537 avctx->codec->close(avctx);
538 avctx->internal->byte_buffer_size = 0;
539 av_freep(&avctx->internal->byte_buffer);
540 #if FF_API_OLD_ENCDEC
541 av_frame_free(&avctx->internal->to_free);
542 av_frame_free(&avctx->internal->compat_decode_frame);
543 av_packet_free(&avctx->internal->compat_encode_packet);
545 av_frame_free(&avctx->internal->buffer_frame);
546 av_packet_free(&avctx->internal->buffer_pkt);
547 av_packet_unref(avctx->internal->last_pkt_props);
548 while (av_fifo_size(avctx->internal->pkt_props) >=
549 sizeof(*avctx->internal->last_pkt_props)) {
550 av_fifo_generic_read(avctx->internal->pkt_props,
551 avctx->internal->last_pkt_props,
552 sizeof(*avctx->internal->last_pkt_props),
554 av_packet_unref(avctx->internal->last_pkt_props);
556 av_packet_free(&avctx->internal->last_pkt_props);
557 av_fifo_freep(&avctx->internal->pkt_props);
559 av_packet_free(&avctx->internal->ds.in_pkt);
560 av_frame_free(&avctx->internal->es.in_frame);
562 av_buffer_unref(&avctx->internal->pool);
564 if (avctx->hwaccel && avctx->hwaccel->uninit)
565 avctx->hwaccel->uninit(avctx);
566 av_freep(&avctx->internal->hwaccel_priv_data);
568 av_bsf_free(&avctx->internal->bsf);
570 av_freep(&avctx->internal);
573 for (i = 0; i < avctx->nb_coded_side_data; i++)
574 av_freep(&avctx->coded_side_data[i].data);
575 av_freep(&avctx->coded_side_data);
576 avctx->nb_coded_side_data = 0;
578 av_buffer_unref(&avctx->hw_frames_ctx);
579 av_buffer_unref(&avctx->hw_device_ctx);
581 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
582 av_opt_free(avctx->priv_data);
584 av_freep(&avctx->priv_data);
585 if (av_codec_is_encoder(avctx->codec)) {
586 av_freep(&avctx->extradata);
587 #if FF_API_CODED_FRAME
588 FF_DISABLE_DEPRECATION_WARNINGS
589 av_frame_free(&avctx->coded_frame);
590 FF_ENABLE_DEPRECATION_WARNINGS
594 avctx->active_thread_type = 0;
599 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
601 const char *codec_type;
602 const char *codec_name;
603 const char *profile = NULL;
606 AVRational display_aspect_ratio;
607 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
609 if (!buf || buf_size <= 0)
611 codec_type = av_get_media_type_string(enc->codec_type);
612 codec_name = avcodec_get_name(enc->codec_id);
613 profile = avcodec_profile_name(enc->codec_id, enc->profile);
615 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
617 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
619 if (enc->codec && strcmp(enc->codec->name, codec_name))
620 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
623 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
624 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
625 && av_log_get_level() >= AV_LOG_VERBOSE
627 snprintf(buf + strlen(buf), buf_size - strlen(buf),
628 ", %d reference frame%s",
629 enc->refs, enc->refs > 1 ? "s" : "");
632 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
633 av_fourcc2str(enc->codec_tag), enc->codec_tag);
635 switch (enc->codec_type) {
636 case AVMEDIA_TYPE_VIDEO:
638 char detail[256] = "(";
640 av_strlcat(buf, separator, buf_size);
642 snprintf(buf + strlen(buf), buf_size - strlen(buf),
643 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
644 av_get_pix_fmt_name(enc->pix_fmt));
645 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
646 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
647 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
648 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
649 av_strlcatf(detail, sizeof(detail), "%s, ",
650 av_color_range_name(enc->color_range));
652 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
653 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
654 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
655 if (enc->colorspace != (int)enc->color_primaries ||
656 enc->colorspace != (int)enc->color_trc) {
658 av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
659 av_color_space_name(enc->colorspace),
660 av_color_primaries_name(enc->color_primaries),
661 av_color_transfer_name(enc->color_trc));
663 av_strlcatf(detail, sizeof(detail), "%s, ",
664 av_get_colorspace_name(enc->colorspace));
667 if (enc->field_order != AV_FIELD_UNKNOWN) {
668 const char *field_order = "progressive";
669 if (enc->field_order == AV_FIELD_TT)
670 field_order = "top first";
671 else if (enc->field_order == AV_FIELD_BB)
672 field_order = "bottom first";
673 else if (enc->field_order == AV_FIELD_TB)
674 field_order = "top coded first (swapped)";
675 else if (enc->field_order == AV_FIELD_BT)
676 field_order = "bottom coded first (swapped)";
678 av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
681 if (av_log_get_level() >= AV_LOG_VERBOSE &&
682 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
683 av_strlcatf(detail, sizeof(detail), "%s, ",
684 av_chroma_location_name(enc->chroma_sample_location));
686 if (strlen(detail) > 1) {
687 detail[strlen(detail) - 2] = 0;
688 av_strlcatf(buf, buf_size, "%s)", detail);
693 av_strlcat(buf, new_line ? separator : ", ", buf_size);
695 snprintf(buf + strlen(buf), buf_size - strlen(buf),
697 enc->width, enc->height);
699 if (av_log_get_level() >= AV_LOG_VERBOSE &&
700 (enc->width != enc->coded_width ||
701 enc->height != enc->coded_height))
702 snprintf(buf + strlen(buf), buf_size - strlen(buf),
703 " (%dx%d)", enc->coded_width, enc->coded_height);
705 if (enc->sample_aspect_ratio.num) {
706 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
707 enc->width * (int64_t)enc->sample_aspect_ratio.num,
708 enc->height * (int64_t)enc->sample_aspect_ratio.den,
710 snprintf(buf + strlen(buf), buf_size - strlen(buf),
711 " [SAR %d:%d DAR %d:%d]",
712 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
713 display_aspect_ratio.num, display_aspect_ratio.den);
715 if (av_log_get_level() >= AV_LOG_DEBUG) {
716 int g = av_gcd(enc->time_base.num, enc->time_base.den);
717 snprintf(buf + strlen(buf), buf_size - strlen(buf),
719 enc->time_base.num / g, enc->time_base.den / g);
723 snprintf(buf + strlen(buf), buf_size - strlen(buf),
724 ", q=%d-%d", enc->qmin, enc->qmax);
726 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
727 snprintf(buf + strlen(buf), buf_size - strlen(buf),
728 ", Closed Captions");
729 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
730 snprintf(buf + strlen(buf), buf_size - strlen(buf),
734 case AVMEDIA_TYPE_AUDIO:
735 av_strlcat(buf, separator, buf_size);
737 if (enc->sample_rate) {
738 snprintf(buf + strlen(buf), buf_size - strlen(buf),
739 "%d Hz, ", enc->sample_rate);
741 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
742 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
743 snprintf(buf + strlen(buf), buf_size - strlen(buf),
744 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
746 if ( enc->bits_per_raw_sample > 0
747 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
748 snprintf(buf + strlen(buf), buf_size - strlen(buf),
749 " (%d bit)", enc->bits_per_raw_sample);
750 if (av_log_get_level() >= AV_LOG_VERBOSE) {
751 if (enc->initial_padding)
752 snprintf(buf + strlen(buf), buf_size - strlen(buf),
753 ", delay %d", enc->initial_padding);
754 if (enc->trailing_padding)
755 snprintf(buf + strlen(buf), buf_size - strlen(buf),
756 ", padding %d", enc->trailing_padding);
759 case AVMEDIA_TYPE_DATA:
760 if (av_log_get_level() >= AV_LOG_DEBUG) {
761 int g = av_gcd(enc->time_base.num, enc->time_base.den);
763 snprintf(buf + strlen(buf), buf_size - strlen(buf),
765 enc->time_base.num / g, enc->time_base.den / g);
768 case AVMEDIA_TYPE_SUBTITLE:
770 snprintf(buf + strlen(buf), buf_size - strlen(buf),
771 ", %dx%d", enc->width, enc->height);
777 if (enc->flags & AV_CODEC_FLAG_PASS1)
778 snprintf(buf + strlen(buf), buf_size - strlen(buf),
780 if (enc->flags & AV_CODEC_FLAG_PASS2)
781 snprintf(buf + strlen(buf), buf_size - strlen(buf),
784 bitrate = get_bit_rate(enc);
786 snprintf(buf + strlen(buf), buf_size - strlen(buf),
787 ", %"PRId64" kb/s", bitrate / 1000);
788 } else if (enc->rc_max_rate > 0) {
789 snprintf(buf + strlen(buf), buf_size - strlen(buf),
790 ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
794 int avcodec_is_open(AVCodecContext *s)
796 return !!s->internal;