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/bprint.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/thread.h"
37 #include "frame_thread_encoder.h"
41 #include "libavutil/ffversion.h"
42 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
44 unsigned avcodec_version(void)
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);
51 return LIBAVCODEC_VERSION_INT;
54 const char *avcodec_configuration(void)
56 return FFMPEG_CONFIGURATION;
59 const char *avcodec_license(void)
61 #define LICENSE_PREFIX "libavcodec license: "
62 return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
65 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
69 for (i = 0; i < count; i++) {
70 int r = func(c, (char *)arg + i * size);
78 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
82 for (i = 0; i < count; i++) {
83 int r = func(c, arg, i, 0);
91 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
93 static void lock_avcodec(const AVCodec *codec)
95 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
96 ff_mutex_lock(&codec_mutex);
99 static void unlock_avcodec(const AVCodec *codec)
101 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
102 ff_mutex_unlock(&codec_mutex);
106 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
112 static int64_t get_bit_rate(AVCodecContext *ctx)
117 switch (ctx->codec_type) {
118 case AVMEDIA_TYPE_VIDEO:
119 case AVMEDIA_TYPE_DATA:
120 case AVMEDIA_TYPE_SUBTITLE:
121 case AVMEDIA_TYPE_ATTACHMENT:
122 bit_rate = ctx->bit_rate;
124 case AVMEDIA_TYPE_AUDIO:
125 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
126 if (bits_per_sample) {
127 bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
128 if (bit_rate > INT64_MAX / bits_per_sample) {
131 bit_rate *= bits_per_sample;
133 bit_rate = ctx->bit_rate;
142 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
145 int codec_init_ok = 0;
146 AVDictionary *tmp = NULL;
147 AVCodecInternal *avci;
149 if (avcodec_is_open(avctx))
152 if (!codec && !avctx->codec) {
153 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
154 return AVERROR(EINVAL);
156 if (codec && avctx->codec && codec != avctx->codec) {
157 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
158 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
159 return AVERROR(EINVAL);
162 codec = avctx->codec;
164 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
165 avctx->codec_id == AV_CODEC_ID_NONE) {
166 avctx->codec_type = codec->type;
167 avctx->codec_id = codec->id;
169 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
170 avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
171 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
172 return AVERROR(EINVAL);
174 avctx->codec = codec;
176 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
177 return AVERROR(EINVAL);
180 av_dict_copy(&tmp, *options, 0);
184 avci = av_mallocz(sizeof(*avci));
186 ret = AVERROR(ENOMEM);
189 avctx->internal = avci;
191 #if FF_API_OLD_ENCDEC
192 avci->to_free = av_frame_alloc();
193 avci->compat_decode_frame = av_frame_alloc();
194 avci->compat_encode_packet = av_packet_alloc();
195 if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
196 ret = AVERROR(ENOMEM);
200 avci->buffer_frame = av_frame_alloc();
201 avci->buffer_pkt = av_packet_alloc();
202 avci->es.in_frame = av_frame_alloc();
203 avci->ds.in_pkt = av_packet_alloc();
204 avci->last_pkt_props = av_packet_alloc();
205 avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
206 if (!avci->buffer_frame || !avci->buffer_pkt ||
207 !avci->es.in_frame || !avci->ds.in_pkt ||
208 !avci->last_pkt_props || !avci->pkt_props) {
209 ret = AVERROR(ENOMEM);
213 avci->skip_samples_multiplier = 1;
215 if (codec->priv_data_size > 0) {
216 if (!avctx->priv_data) {
217 avctx->priv_data = av_mallocz(codec->priv_data_size);
218 if (!avctx->priv_data) {
219 ret = AVERROR(ENOMEM);
222 if (codec->priv_class) {
223 *(const AVClass **)avctx->priv_data = codec->priv_class;
224 av_opt_set_defaults(avctx->priv_data);
227 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
230 avctx->priv_data = NULL;
232 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
235 if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
236 av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
237 ret = AVERROR(EINVAL);
241 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
242 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
243 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
244 if (avctx->coded_width && avctx->coded_height)
245 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
246 else if (avctx->width && avctx->height)
247 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
252 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
253 && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
254 || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
255 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
256 ff_set_dimensions(avctx, 0, 0);
259 if (avctx->width > 0 && avctx->height > 0) {
260 if (av_image_check_sar(avctx->width, avctx->height,
261 avctx->sample_aspect_ratio) < 0) {
262 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
263 avctx->sample_aspect_ratio.num,
264 avctx->sample_aspect_ratio.den);
265 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
269 if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
270 av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
271 ret = AVERROR(EINVAL);
275 if (avctx->sample_rate < 0) {
276 av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
277 ret = AVERROR(EINVAL);
280 if (avctx->block_align < 0) {
281 av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
282 ret = AVERROR(EINVAL);
286 avctx->frame_number = 0;
287 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
289 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
290 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
291 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
292 const AVCodec *codec2;
293 av_log(avctx, AV_LOG_ERROR,
294 "The %s '%s' is experimental but experimental codecs are not enabled, "
295 "add '-strict %d' if you want to use it.\n",
296 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
297 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
298 if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
299 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
300 codec_string, codec2->name);
301 ret = AVERROR_EXPERIMENTAL;
305 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
306 (!avctx->time_base.num || !avctx->time_base.den)) {
307 avctx->time_base.num = 1;
308 avctx->time_base.den = avctx->sample_rate;
311 if (av_codec_is_encoder(avctx->codec))
312 ret = ff_encode_preinit(avctx);
314 ret = ff_decode_preinit(avctx);
319 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
321 if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
322 unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
323 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
330 && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
331 ret = ff_thread_init(avctx);
336 if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
337 avctx->thread_count = 1;
339 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
340 || avci->frame_thread_encoder)) {
341 ret = avctx->codec->init(avctx);
351 if (av_codec_is_decoder(avctx->codec)) {
352 if (!avctx->bit_rate)
353 avctx->bit_rate = get_bit_rate(avctx);
354 /* validate channel layout from the decoder */
355 if (avctx->channel_layout) {
356 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
357 if (!avctx->channels)
358 avctx->channels = channels;
359 else if (channels != avctx->channels) {
361 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
362 av_log(avctx, AV_LOG_WARNING,
363 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
364 "ignoring specified channel layout\n",
365 buf, channels, avctx->channels);
366 avctx->channel_layout = 0;
369 if (avctx->channels && avctx->channels < 0 ||
370 avctx->channels > FF_SANE_NB_CHANNELS) {
371 ret = AVERROR(EINVAL);
374 if (avctx->bits_per_coded_sample < 0) {
375 ret = AVERROR(EINVAL);
379 #if FF_API_AVCTX_TIMEBASE
380 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
381 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
384 if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
385 av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
389 unlock_avcodec(codec);
391 av_dict_free(options);
397 if (avctx->codec && avctx->codec->close &&
398 (codec_init_ok > 0 || (codec_init_ok < 0 &&
399 avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
400 avctx->codec->close(avctx);
402 if (HAVE_THREADS && avci->thread_ctx)
403 ff_thread_free(avctx);
405 if (codec->priv_class && avctx->priv_data)
406 av_opt_free(avctx->priv_data);
409 if (av_codec_is_encoder(avctx->codec)) {
410 #if FF_API_CODED_FRAME
411 FF_DISABLE_DEPRECATION_WARNINGS
412 av_frame_free(&avctx->coded_frame);
413 FF_ENABLE_DEPRECATION_WARNINGS
415 av_freep(&avctx->extradata);
416 avctx->extradata_size = 0;
420 av_freep(&avctx->priv_data);
421 if (av_codec_is_decoder(avctx->codec))
422 av_freep(&avctx->subtitle_header);
424 #if FF_API_OLD_ENCDEC
425 av_frame_free(&avci->to_free);
426 av_frame_free(&avci->compat_decode_frame);
427 av_packet_free(&avci->compat_encode_packet);
429 av_frame_free(&avci->buffer_frame);
430 av_packet_free(&avci->buffer_pkt);
431 av_packet_free(&avci->last_pkt_props);
432 av_fifo_freep(&avci->pkt_props);
434 av_packet_free(&avci->ds.in_pkt);
435 av_frame_free(&avci->es.in_frame);
436 av_bsf_free(&avci->bsf);
438 av_buffer_unref(&avci->pool);
440 avctx->internal = NULL;
445 void avcodec_flush_buffers(AVCodecContext *avctx)
447 AVCodecInternal *avci = avctx->internal;
449 if (av_codec_is_encoder(avctx->codec)) {
450 int caps = avctx->codec->capabilities;
452 if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
453 // Only encoders that explicitly declare support for it can be
454 // flushed. Otherwise, this is a no-op.
455 av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
456 "that doesn't support it\n");
460 // We haven't implemented flushing for frame-threaded encoders.
461 av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
465 avci->draining_done = 0;
466 avci->nb_draining_errors = 0;
467 av_frame_unref(avci->buffer_frame);
468 #if FF_API_OLD_ENCDEC
469 av_frame_unref(avci->compat_decode_frame);
470 av_packet_unref(avci->compat_encode_packet);
472 av_packet_unref(avci->buffer_pkt);
474 av_packet_unref(avci->last_pkt_props);
475 while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
476 av_fifo_generic_read(avci->pkt_props,
477 avci->last_pkt_props, sizeof(*avci->last_pkt_props),
479 av_packet_unref(avci->last_pkt_props);
481 av_fifo_reset(avci->pkt_props);
483 av_frame_unref(avci->es.in_frame);
484 av_packet_unref(avci->ds.in_pkt);
486 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
487 ff_thread_flush(avctx);
488 else if (avctx->codec->flush)
489 avctx->codec->flush(avctx);
491 avctx->pts_correction_last_pts =
492 avctx->pts_correction_last_dts = INT64_MIN;
494 if (av_codec_is_decoder(avctx->codec))
495 av_bsf_flush(avci->bsf);
497 #if FF_API_OLD_ENCDEC
498 FF_DISABLE_DEPRECATION_WARNINGS
499 if (!avctx->refcounted_frames)
500 av_frame_unref(avci->to_free);
501 FF_ENABLE_DEPRECATION_WARNINGS
505 void avsubtitle_free(AVSubtitle *sub)
509 for (i = 0; i < sub->num_rects; i++) {
510 av_freep(&sub->rects[i]->data[0]);
511 av_freep(&sub->rects[i]->data[1]);
512 av_freep(&sub->rects[i]->data[2]);
513 av_freep(&sub->rects[i]->data[3]);
514 av_freep(&sub->rects[i]->text);
515 av_freep(&sub->rects[i]->ass);
516 av_freep(&sub->rects[i]);
519 av_freep(&sub->rects);
521 memset(sub, 0, sizeof(*sub));
524 av_cold int avcodec_close(AVCodecContext *avctx)
531 if (avcodec_is_open(avctx)) {
532 AVCodecInternal *avci = avctx->internal;
534 if (CONFIG_FRAME_THREAD_ENCODER &&
535 avci->frame_thread_encoder && avctx->thread_count > 1) {
536 ff_frame_thread_encoder_free(avctx);
538 if (HAVE_THREADS && avci->thread_ctx)
539 ff_thread_free(avctx);
540 if (avctx->codec && avctx->codec->close)
541 avctx->codec->close(avctx);
542 avci->byte_buffer_size = 0;
543 av_freep(&avci->byte_buffer);
544 #if FF_API_OLD_ENCDEC
545 av_frame_free(&avci->to_free);
546 av_frame_free(&avci->compat_decode_frame);
547 av_packet_free(&avci->compat_encode_packet);
549 av_frame_free(&avci->buffer_frame);
550 av_packet_free(&avci->buffer_pkt);
551 av_packet_unref(avci->last_pkt_props);
552 while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
553 av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
554 sizeof(*avci->last_pkt_props), NULL);
555 av_packet_unref(avci->last_pkt_props);
557 av_packet_free(&avci->last_pkt_props);
558 av_fifo_freep(&avci->pkt_props);
560 av_packet_free(&avci->ds.in_pkt);
561 av_frame_free(&avci->es.in_frame);
563 av_buffer_unref(&avci->pool);
565 if (avctx->hwaccel && avctx->hwaccel->uninit)
566 avctx->hwaccel->uninit(avctx);
567 av_freep(&avci->hwaccel_priv_data);
569 av_bsf_free(&avci->bsf);
571 av_freep(&avctx->internal);
574 for (i = 0; i < avctx->nb_coded_side_data; i++)
575 av_freep(&avctx->coded_side_data[i].data);
576 av_freep(&avctx->coded_side_data);
577 avctx->nb_coded_side_data = 0;
579 av_buffer_unref(&avctx->hw_frames_ctx);
580 av_buffer_unref(&avctx->hw_device_ctx);
582 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
583 av_opt_free(avctx->priv_data);
585 av_freep(&avctx->priv_data);
586 if (av_codec_is_encoder(avctx->codec)) {
587 av_freep(&avctx->extradata);
588 #if FF_API_CODED_FRAME
589 FF_DISABLE_DEPRECATION_WARNINGS
590 av_frame_free(&avctx->coded_frame);
591 FF_ENABLE_DEPRECATION_WARNINGS
593 } else if (av_codec_is_decoder(avctx->codec))
594 av_freep(&avctx->subtitle_header);
597 avctx->active_thread_type = 0;
602 static const char *unknown_if_null(const char *str)
604 return str ? str : "unknown";
607 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
609 const char *codec_type;
610 const char *codec_name;
611 const char *profile = NULL;
615 AVRational display_aspect_ratio;
616 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
619 if (!buf || buf_size <= 0)
621 av_bprint_init_for_buffer(&bprint, buf, buf_size);
622 codec_type = av_get_media_type_string(enc->codec_type);
623 codec_name = avcodec_get_name(enc->codec_id);
624 profile = avcodec_profile_name(enc->codec_id, enc->profile);
626 av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
628 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
630 if (enc->codec && strcmp(enc->codec->name, codec_name))
631 av_bprintf(&bprint, " (%s)", enc->codec->name);
634 av_bprintf(&bprint, " (%s)", profile);
635 if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
636 && av_log_get_level() >= AV_LOG_VERBOSE
638 av_bprintf(&bprint, ", %d reference frame%s",
639 enc->refs, enc->refs > 1 ? "s" : "");
642 av_bprintf(&bprint, " (%s / 0x%04X)",
643 av_fourcc2str(enc->codec_tag), enc->codec_tag);
645 switch (enc->codec_type) {
646 case AVMEDIA_TYPE_VIDEO:
650 av_bprintf(&bprint, "%s%s", separator,
651 enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
652 unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
654 av_bprint_chars(&bprint, '(', 1);
657 /* The following check ensures that '(' has been written
658 * and therefore allows us to erase it if it turns out
659 * to be unnecessary. */
660 if (!av_bprint_is_complete(&bprint))
663 if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
664 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
665 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
666 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
667 (str = av_color_range_name(enc->color_range)))
668 av_bprintf(&bprint, "%s, ", str);
670 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
671 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
672 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
673 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
674 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
675 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
676 if (strcmp(col, pri) || strcmp(col, trc)) {
678 av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
680 av_bprintf(&bprint, "%s, ", col);
683 if (enc->field_order != AV_FIELD_UNKNOWN) {
684 const char *field_order = "progressive";
685 if (enc->field_order == AV_FIELD_TT)
686 field_order = "top first";
687 else if (enc->field_order == AV_FIELD_BB)
688 field_order = "bottom first";
689 else if (enc->field_order == AV_FIELD_TB)
690 field_order = "top coded first (swapped)";
691 else if (enc->field_order == AV_FIELD_BT)
692 field_order = "bottom coded first (swapped)";
694 av_bprintf(&bprint, "%s, ", field_order);
697 if (av_log_get_level() >= AV_LOG_VERBOSE &&
698 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
699 (str = av_chroma_location_name(enc->chroma_sample_location)))
700 av_bprintf(&bprint, "%s, ", str);
702 if (len == bprint.len) {
703 bprint.str[len - 1] = '\0';
706 if (bprint.len - 2 < bprint.size) {
707 /* Erase the last ", " */
709 bprint.str[bprint.len] = '\0';
711 av_bprint_chars(&bprint, ')', 1);
716 av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
717 enc->width, enc->height);
719 if (av_log_get_level() >= AV_LOG_VERBOSE &&
720 (enc->width != enc->coded_width ||
721 enc->height != enc->coded_height))
722 av_bprintf(&bprint, " (%dx%d)",
723 enc->coded_width, enc->coded_height);
725 if (enc->sample_aspect_ratio.num) {
726 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
727 enc->width * (int64_t)enc->sample_aspect_ratio.num,
728 enc->height * (int64_t)enc->sample_aspect_ratio.den,
730 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
731 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
732 display_aspect_ratio.num, display_aspect_ratio.den);
734 if (av_log_get_level() >= AV_LOG_DEBUG) {
735 int g = av_gcd(enc->time_base.num, enc->time_base.den);
736 av_bprintf(&bprint, ", %d/%d",
737 enc->time_base.num / g, enc->time_base.den / g);
741 av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
743 if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
744 av_bprintf(&bprint, ", Closed Captions");
745 if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
746 av_bprintf(&bprint, ", lossless");
749 case AVMEDIA_TYPE_AUDIO:
750 av_bprintf(&bprint, "%s", separator);
752 if (enc->sample_rate) {
753 av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
755 av_bprint_channel_layout(&bprint, enc->channels, enc->channel_layout);
756 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
757 (str = av_get_sample_fmt_name(enc->sample_fmt))) {
758 av_bprintf(&bprint, ", %s", str);
760 if ( enc->bits_per_raw_sample > 0
761 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
762 av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
763 if (av_log_get_level() >= AV_LOG_VERBOSE) {
764 if (enc->initial_padding)
765 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
766 if (enc->trailing_padding)
767 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
770 case AVMEDIA_TYPE_DATA:
771 if (av_log_get_level() >= AV_LOG_DEBUG) {
772 int g = av_gcd(enc->time_base.num, enc->time_base.den);
774 av_bprintf(&bprint, ", %d/%d",
775 enc->time_base.num / g, enc->time_base.den / g);
778 case AVMEDIA_TYPE_SUBTITLE:
780 av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
786 if (enc->flags & AV_CODEC_FLAG_PASS1)
787 av_bprintf(&bprint, ", pass 1");
788 if (enc->flags & AV_CODEC_FLAG_PASS2)
789 av_bprintf(&bprint, ", pass 2");
791 bitrate = get_bit_rate(enc);
793 av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
794 } else if (enc->rc_max_rate > 0) {
795 av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
799 int avcodec_is_open(AVCodecContext *s)
801 return !!s->internal;