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/channel_layout.h"
31 #include "libavutil/common.h"
32 #include "libavutil/emms.h"
33 #include "libavutil/fifo.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/thread.h"
39 #include "avcodec_internal.h"
41 #include "codec_desc.h"
42 #include "codec_internal.h"
45 #include "frame_thread_encoder.h"
48 #include "libavutil/refstruct.h"
52 * Maximum size in bytes of extradata.
53 * This value was chosen such that every bit of the buffer is
54 * addressable by a 32-bit signed integer as used by get_bits.
56 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
58 const SideDataMap ff_sd_global_map
[] = {
59 { AV_PKT_DATA_REPLAYGAIN
, AV_FRAME_DATA_REPLAYGAIN
},
60 { AV_PKT_DATA_DISPLAYMATRIX
, AV_FRAME_DATA_DISPLAYMATRIX
},
61 { AV_PKT_DATA_SPHERICAL
, AV_FRAME_DATA_SPHERICAL
},
62 { AV_PKT_DATA_STEREO3D
, AV_FRAME_DATA_STEREO3D
},
63 { AV_PKT_DATA_AUDIO_SERVICE_TYPE
, AV_FRAME_DATA_AUDIO_SERVICE_TYPE
},
64 { AV_PKT_DATA_MASTERING_DISPLAY_METADATA
, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
},
65 { AV_PKT_DATA_CONTENT_LIGHT_LEVEL
, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
},
66 { AV_PKT_DATA_ICC_PROFILE
, AV_FRAME_DATA_ICC_PROFILE
},
67 { AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT
,AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT
},
72 int avcodec_default_execute(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
), void *arg
, int *ret
, int count
, int size
)
76 for (i
= 0; i
< count
; i
++) {
77 size_t offset
= i
* size
;
78 int r
= func(c
, FF_PTR_ADD((char *)arg
, offset
));
86 int avcodec_default_execute2(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
, int jobnr
, int threadnr
), void *arg
, int *ret
, int count
)
90 for (i
= 0; i
< count
; i
++) {
91 int r
= func(c
, arg
, i
, 0);
99 static AVMutex codec_mutex
= AV_MUTEX_INITIALIZER
;
101 static void lock_avcodec(const FFCodec
*codec
)
103 if (codec
->caps_internal
& FF_CODEC_CAP_NOT_INIT_THREADSAFE
&& codec
->init
)
104 ff_mutex_lock(&codec_mutex
);
107 static void unlock_avcodec(const FFCodec
*codec
)
109 if (codec
->caps_internal
& FF_CODEC_CAP_NOT_INIT_THREADSAFE
&& codec
->init
)
110 ff_mutex_unlock(&codec_mutex
);
113 static int64_t get_bit_rate(AVCodecContext
*ctx
)
118 switch (ctx
->codec_type
) {
119 case AVMEDIA_TYPE_VIDEO
:
120 case AVMEDIA_TYPE_DATA
:
121 case AVMEDIA_TYPE_SUBTITLE
:
122 case AVMEDIA_TYPE_ATTACHMENT
:
123 bit_rate
= ctx
->bit_rate
;
125 case AVMEDIA_TYPE_AUDIO
:
126 bits_per_sample
= av_get_bits_per_sample(ctx
->codec_id
);
127 if (bits_per_sample
) {
128 bit_rate
= ctx
->sample_rate
* (int64_t)ctx
->ch_layout
.nb_channels
;
129 if (bit_rate
> INT64_MAX
/ bits_per_sample
) {
132 bit_rate
*= bits_per_sample
;
134 bit_rate
= ctx
->bit_rate
;
143 int attribute_align_arg
avcodec_open2(AVCodecContext
*avctx
, const AVCodec
*codec
, AVDictionary
**options
)
146 AVCodecInternal
*avci
;
147 const FFCodec
*codec2
;
148 const AVDictionaryEntry
*e
;
150 if (avcodec_is_open(avctx
))
153 if (!codec
&& !avctx
->codec
) {
154 av_log(avctx
, AV_LOG_ERROR
, "No codec provided to avcodec_open2()\n");
155 return AVERROR(EINVAL
);
157 if (codec
&& avctx
->codec
&& codec
!= avctx
->codec
) {
158 av_log(avctx
, AV_LOG_ERROR
, "This AVCodecContext was allocated for %s, "
159 "but %s passed to avcodec_open2()\n", avctx
->codec
->name
, codec
->name
);
160 return AVERROR(EINVAL
);
163 codec
= avctx
->codec
;
164 codec2
= ffcodec(codec
);
166 if ((avctx
->codec_type
!= AVMEDIA_TYPE_UNKNOWN
&& avctx
->codec_type
!= codec
->type
) ||
167 (avctx
->codec_id
!= AV_CODEC_ID_NONE
&& avctx
->codec_id
!= codec
->id
)) {
168 av_log(avctx
, AV_LOG_ERROR
, "Codec type or id mismatches\n");
169 return AVERROR(EINVAL
);
172 avctx
->codec_type
= codec
->type
;
173 avctx
->codec_id
= codec
->id
;
174 avctx
->codec
= codec
;
176 if (avctx
->extradata_size
< 0 || avctx
->extradata_size
>= FF_MAX_EXTRADATA_SIZE
)
177 return AVERROR(EINVAL
);
179 // set the whitelist from provided options dict,
180 // so we can check it immediately
181 e
= options
? av_dict_get(*options
, "codec_whitelist", NULL
, 0) : NULL
;
183 ret
= av_opt_set(avctx
, e
->key
, e
->value
, 0);
188 if (avctx
->codec_whitelist
&& av_match_list(codec
->name
, avctx
->codec_whitelist
, ',') <= 0) {
189 av_log(avctx
, AV_LOG_ERROR
, "Codec (%s) not on whitelist \'%s\'\n", codec
->name
, avctx
->codec_whitelist
);
190 return AVERROR(EINVAL
);
193 avci
= av_codec_is_decoder(codec
) ?
194 ff_decode_internal_alloc() :
195 ff_encode_internal_alloc();
197 ret
= AVERROR(ENOMEM
);
200 avctx
->internal
= avci
;
202 avci
->buffer_frame
= av_frame_alloc();
203 avci
->buffer_pkt
= av_packet_alloc();
204 if (!avci
->buffer_frame
|| !avci
->buffer_pkt
) {
205 ret
= AVERROR(ENOMEM
);
209 if (codec2
->priv_data_size
> 0) {
210 if (!avctx
->priv_data
) {
211 avctx
->priv_data
= av_mallocz(codec2
->priv_data_size
);
212 if (!avctx
->priv_data
) {
213 ret
= AVERROR(ENOMEM
);
216 if (codec
->priv_class
) {
217 *(const AVClass
**)avctx
->priv_data
= codec
->priv_class
;
218 av_opt_set_defaults(avctx
->priv_data
);
222 avctx
->priv_data
= NULL
;
225 ret
= av_opt_set_dict2(avctx
, options
, AV_OPT_SEARCH_CHILDREN
);
229 // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
230 if (!(avctx
->coded_width
&& avctx
->coded_height
&& avctx
->width
&& avctx
->height
&&
231 (avctx
->codec_id
== AV_CODEC_ID_H264
|| avctx
->codec_id
== AV_CODEC_ID_VP6F
|| avctx
->codec_id
== AV_CODEC_ID_DXV
))) {
232 if (avctx
->coded_width
&& avctx
->coded_height
)
233 ret
= ff_set_dimensions(avctx
, avctx
->coded_width
, avctx
->coded_height
);
234 else if (avctx
->width
&& avctx
->height
)
235 ret
= ff_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
240 if ((avctx
->coded_width
|| avctx
->coded_height
|| avctx
->width
|| avctx
->height
)
241 && ( av_image_check_size2(avctx
->coded_width
, avctx
->coded_height
, avctx
->max_pixels
, AV_PIX_FMT_NONE
, 0, avctx
) < 0
242 || av_image_check_size2(avctx
->width
, avctx
->height
, avctx
->max_pixels
, AV_PIX_FMT_NONE
, 0, avctx
) < 0)) {
243 av_log(avctx
, AV_LOG_WARNING
, "Ignoring invalid width/height values\n");
244 ff_set_dimensions(avctx
, 0, 0);
247 if (avctx
->width
> 0 && avctx
->height
> 0) {
248 if (av_image_check_sar(avctx
->width
, avctx
->height
,
249 avctx
->sample_aspect_ratio
) < 0) {
250 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid SAR: %u/%u\n",
251 avctx
->sample_aspect_ratio
.num
,
252 avctx
->sample_aspect_ratio
.den
);
253 avctx
->sample_aspect_ratio
= (AVRational
){ 0, 1 };
257 if (avctx
->sample_rate
< 0) {
258 av_log(avctx
, AV_LOG_ERROR
, "Invalid sample rate: %d\n", avctx
->sample_rate
);
259 ret
= AVERROR(EINVAL
);
262 if (avctx
->block_align
< 0) {
263 av_log(avctx
, AV_LOG_ERROR
, "Invalid block align: %d\n", avctx
->block_align
);
264 ret
= AVERROR(EINVAL
);
268 /* AV_CODEC_CAP_CHANNEL_CONF is a decoder-only flag; so the code below
269 * in particular checks that nb_channels is set for all audio encoders. */
270 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&& !avctx
->ch_layout
.nb_channels
271 && !(codec
->capabilities
& AV_CODEC_CAP_CHANNEL_CONF
)) {
272 av_log(avctx
, AV_LOG_ERROR
, "%s requires channel layout to be set\n",
273 av_codec_is_decoder(codec
) ? "Decoder" : "Encoder");
274 ret
= AVERROR(EINVAL
);
277 if (avctx
->ch_layout
.nb_channels
&& !av_channel_layout_check(&avctx
->ch_layout
)) {
278 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel layout\n");
279 ret
= AVERROR(EINVAL
);
282 if (avctx
->ch_layout
.nb_channels
> FF_SANE_NB_CHANNELS
) {
283 av_log(avctx
, AV_LOG_ERROR
, "Too many channels: %d\n", avctx
->ch_layout
.nb_channels
);
284 ret
= AVERROR(EINVAL
);
288 avctx
->frame_num
= 0;
289 avctx
->codec_descriptor
= avcodec_descriptor_get(avctx
->codec_id
);
291 if ((avctx
->codec
->capabilities
& AV_CODEC_CAP_EXPERIMENTAL
) &&
292 avctx
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
293 const char *codec_string
= av_codec_is_encoder(codec
) ? "encoder" : "decoder";
294 const AVCodec
*codec2
;
295 av_log(avctx
, AV_LOG_ERROR
,
296 "The %s '%s' is experimental but experimental codecs are not enabled, "
297 "add '-strict %d' if you want to use it.\n",
298 codec_string
, codec
->name
, FF_COMPLIANCE_EXPERIMENTAL
);
299 codec2
= av_codec_is_encoder(codec
) ? avcodec_find_encoder(codec
->id
) : avcodec_find_decoder(codec
->id
);
300 if (!(codec2
->capabilities
& AV_CODEC_CAP_EXPERIMENTAL
))
301 av_log(avctx
, AV_LOG_ERROR
, "Alternatively use the non experimental %s '%s'.\n",
302 codec_string
, codec2
->name
);
303 ret
= AVERROR_EXPERIMENTAL
;
307 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&&
308 (!avctx
->time_base
.num
|| !avctx
->time_base
.den
)) {
309 avctx
->time_base
.num
= 1;
310 avctx
->time_base
.den
= avctx
->sample_rate
;
313 if (av_codec_is_encoder(avctx
->codec
))
314 ret
= ff_encode_preinit(avctx
);
316 ret
= ff_decode_preinit(avctx
);
320 if (HAVE_THREADS
&& !avci
->frame_thread_encoder
) {
321 /* Frame-threaded decoders call FFCodec.init for their child contexts. */
322 lock_avcodec(codec2
);
323 ret
= ff_thread_init(avctx
);
324 unlock_avcodec(codec2
);
329 if (!HAVE_THREADS
&& !(codec2
->caps_internal
& FF_CODEC_CAP_AUTO_THREADS
))
330 avctx
->thread_count
= 1;
332 if (!(avctx
->active_thread_type
& FF_THREAD_FRAME
) ||
333 avci
->frame_thread_encoder
) {
335 lock_avcodec(codec2
);
336 ret
= codec2
->init(avctx
);
337 unlock_avcodec(codec2
);
339 avci
->needs_close
= codec2
->caps_internal
& FF_CODEC_CAP_INIT_CLEANUP
;
343 avci
->needs_close
= 1;
348 if (av_codec_is_decoder(avctx
->codec
)) {
349 if (!avctx
->bit_rate
)
350 avctx
->bit_rate
= get_bit_rate(avctx
);
352 /* validate channel layout from the decoder */
353 if ((avctx
->ch_layout
.nb_channels
&& !av_channel_layout_check(&avctx
->ch_layout
)) ||
354 avctx
->ch_layout
.nb_channels
> FF_SANE_NB_CHANNELS
) {
355 ret
= AVERROR(EINVAL
);
358 if (avctx
->bits_per_coded_sample
< 0) {
359 ret
= AVERROR(EINVAL
);
363 if (codec
->priv_class
)
364 av_assert0(*(const AVClass
**)avctx
->priv_data
== codec
->priv_class
);
370 ff_codec_close(avctx
);
374 void avcodec_flush_buffers(AVCodecContext
*avctx
)
376 AVCodecInternal
*avci
= avctx
->internal
;
378 if (av_codec_is_encoder(avctx
->codec
)) {
379 int caps
= avctx
->codec
->capabilities
;
381 if (!(caps
& AV_CODEC_CAP_ENCODER_FLUSH
)) {
382 // Only encoders that explicitly declare support for it can be
383 // flushed. Otherwise, this is a no-op.
384 av_log(avctx
, AV_LOG_WARNING
, "Ignoring attempt to flush encoder "
385 "that doesn't support it\n");
388 ff_encode_flush_buffers(avctx
);
390 ff_decode_flush_buffers(avctx
);
393 avci
->draining_done
= 0;
394 if (avci
->buffer_frame
)
395 av_frame_unref(avci
->buffer_frame
);
396 if (avci
->buffer_pkt
)
397 av_packet_unref(avci
->buffer_pkt
);
399 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
&&
401 ff_thread_flush(avctx
);
402 else if (ffcodec(avctx
->codec
)->flush
)
403 ffcodec(avctx
->codec
)->flush(avctx
);
406 void avsubtitle_free(AVSubtitle
*sub
)
410 for (i
= 0; i
< sub
->num_rects
; i
++) {
411 AVSubtitleRect
*const rect
= sub
->rects
[i
];
413 av_freep(&rect
->data
[0]);
414 av_freep(&rect
->data
[1]);
415 av_freep(&rect
->data
[2]);
416 av_freep(&rect
->data
[3]);
417 av_freep(&rect
->text
);
418 av_freep(&rect
->ass
);
420 av_freep(&sub
->rects
[i
]);
423 av_freep(&sub
->rects
);
425 memset(sub
, 0, sizeof(*sub
));
428 av_cold
void ff_codec_close(AVCodecContext
*avctx
)
435 if (avcodec_is_open(avctx
)) {
436 AVCodecInternal
*avci
= avctx
->internal
;
438 if (CONFIG_FRAME_THREAD_ENCODER
&&
439 avci
->frame_thread_encoder
&& avctx
->thread_count
> 1) {
440 ff_frame_thread_encoder_free(avctx
);
442 if (HAVE_THREADS
&& avci
->thread_ctx
)
443 ff_thread_free(avctx
);
444 if (avci
->needs_close
&& ffcodec(avctx
->codec
)->close
)
445 ffcodec(avctx
->codec
)->close(avctx
);
446 avci
->byte_buffer_size
= 0;
447 av_freep(&avci
->byte_buffer
);
448 av_frame_free(&avci
->buffer_frame
);
449 av_packet_free(&avci
->buffer_pkt
);
450 av_packet_free(&avci
->last_pkt_props
);
452 av_packet_free(&avci
->in_pkt
);
453 av_frame_free(&avci
->in_frame
);
454 av_frame_free(&avci
->recon_frame
);
456 av_refstruct_unref(&avci
->pool
);
457 av_refstruct_pool_uninit(&avci
->progress_frame_pool
);
458 if (av_codec_is_decoder(avctx
->codec
))
459 ff_decode_internal_uninit(avctx
);
461 ff_hwaccel_uninit(avctx
);
463 av_bsf_free(&avci
->bsf
);
465 #if FF_API_DROPCHANGED
466 av_channel_layout_uninit(&avci
->initial_ch_layout
);
470 ff_icc_context_uninit(&avci
->icc
);
473 av_freep(&avctx
->internal
);
476 for (i
= 0; i
< avctx
->nb_coded_side_data
; i
++)
477 av_freep(&avctx
->coded_side_data
[i
].data
);
478 av_freep(&avctx
->coded_side_data
);
479 avctx
->nb_coded_side_data
= 0;
480 av_frame_side_data_free(&avctx
->decoded_side_data
,
481 &avctx
->nb_decoded_side_data
);
483 av_buffer_unref(&avctx
->hw_frames_ctx
);
484 av_buffer_unref(&avctx
->hw_device_ctx
);
486 if (avctx
->priv_data
&& avctx
->codec
&& avctx
->codec
->priv_class
)
487 av_opt_free(avctx
->priv_data
);
489 av_freep(&avctx
->priv_data
);
490 if (av_codec_is_encoder(avctx
->codec
)) {
491 av_freep(&avctx
->extradata
);
492 avctx
->extradata_size
= 0;
493 } else if (av_codec_is_decoder(avctx
->codec
))
494 av_freep(&avctx
->subtitle_header
);
497 avctx
->active_thread_type
= 0;
500 #if FF_API_AVCODEC_CLOSE
501 int avcodec_close(AVCodecContext
*avctx
)
503 ff_codec_close(avctx
);
508 static const char *unknown_if_null(const char *str
)
510 return str
? str
: "unknown";
513 void avcodec_string(char *buf
, int buf_size
, AVCodecContext
*enc
, int encode
)
515 const char *codec_type
;
516 const char *codec_name
;
517 const char *profile
= NULL
;
521 AVRational display_aspect_ratio
;
522 const char *separator
= enc
->dump_separator
? (const char *)enc
->dump_separator
: ", ";
525 if (!buf
|| buf_size
<= 0)
527 av_bprint_init_for_buffer(&bprint
, buf
, buf_size
);
528 codec_type
= av_get_media_type_string(enc
->codec_type
);
529 codec_name
= avcodec_get_name(enc
->codec_id
);
530 profile
= avcodec_profile_name(enc
->codec_id
, enc
->profile
);
532 av_bprintf(&bprint
, "%s: %s", codec_type
? codec_type
: "unknown",
534 buf
[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
536 if (enc
->codec
&& strcmp(enc
->codec
->name
, codec_name
))
537 av_bprintf(&bprint
, " (%s)", enc
->codec
->name
);
540 av_bprintf(&bprint
, " (%s)", profile
);
541 if ( enc
->codec_type
== AVMEDIA_TYPE_VIDEO
542 && av_log_get_level() >= AV_LOG_VERBOSE
544 av_bprintf(&bprint
, ", %d reference frame%s",
545 enc
->refs
, enc
->refs
> 1 ? "s" : "");
548 av_bprintf(&bprint
, " (%s / 0x%04X)",
549 av_fourcc2str(enc
->codec_tag
), enc
->codec_tag
);
551 switch (enc
->codec_type
) {
552 case AVMEDIA_TYPE_VIDEO
:
556 av_bprintf(&bprint
, "%s%s", separator
,
557 enc
->pix_fmt
== AV_PIX_FMT_NONE
? "none" :
558 unknown_if_null(av_get_pix_fmt_name(enc
->pix_fmt
)));
560 av_bprint_chars(&bprint
, '(', 1);
563 /* The following check ensures that '(' has been written
564 * and therefore allows us to erase it if it turns out
565 * to be unnecessary. */
566 if (!av_bprint_is_complete(&bprint
))
569 if (enc
->bits_per_raw_sample
&& enc
->pix_fmt
!= AV_PIX_FMT_NONE
&&
570 enc
->bits_per_raw_sample
< av_pix_fmt_desc_get(enc
->pix_fmt
)->comp
[0].depth
)
571 av_bprintf(&bprint
, "%d bpc, ", enc
->bits_per_raw_sample
);
572 if (enc
->color_range
!= AVCOL_RANGE_UNSPECIFIED
&&
573 (str
= av_color_range_name(enc
->color_range
)))
574 av_bprintf(&bprint
, "%s, ", str
);
576 if (enc
->colorspace
!= AVCOL_SPC_UNSPECIFIED
||
577 enc
->color_primaries
!= AVCOL_PRI_UNSPECIFIED
||
578 enc
->color_trc
!= AVCOL_TRC_UNSPECIFIED
) {
579 const char *col
= unknown_if_null(av_color_space_name(enc
->colorspace
));
580 const char *pri
= unknown_if_null(av_color_primaries_name(enc
->color_primaries
));
581 const char *trc
= unknown_if_null(av_color_transfer_name(enc
->color_trc
));
582 if (strcmp(col
, pri
) || strcmp(col
, trc
)) {
584 av_bprintf(&bprint
, "%s/%s/%s, ", col
, pri
, trc
);
586 av_bprintf(&bprint
, "%s, ", col
);
589 if (enc
->field_order
!= AV_FIELD_UNKNOWN
) {
590 const char *field_order
= "progressive";
591 if (enc
->field_order
== AV_FIELD_TT
)
592 field_order
= "top first";
593 else if (enc
->field_order
== AV_FIELD_BB
)
594 field_order
= "bottom first";
595 else if (enc
->field_order
== AV_FIELD_TB
)
596 field_order
= "top coded first (swapped)";
597 else if (enc
->field_order
== AV_FIELD_BT
)
598 field_order
= "bottom coded first (swapped)";
600 av_bprintf(&bprint
, "%s, ", field_order
);
603 if (av_log_get_level() >= AV_LOG_VERBOSE
&&
604 enc
->chroma_sample_location
!= AVCHROMA_LOC_UNSPECIFIED
&&
605 (str
= av_chroma_location_name(enc
->chroma_sample_location
)))
606 av_bprintf(&bprint
, "%s, ", str
);
608 if (len
== bprint
.len
) {
609 bprint
.str
[len
- 1] = '\0';
612 if (bprint
.len
- 2 < bprint
.size
) {
613 /* Erase the last ", " */
615 bprint
.str
[bprint
.len
] = '\0';
617 av_bprint_chars(&bprint
, ')', 1);
622 av_bprintf(&bprint
, "%s%dx%d", new_line
? separator
: ", ",
623 enc
->width
, enc
->height
);
625 if (av_log_get_level() >= AV_LOG_VERBOSE
&&
626 enc
->coded_width
&& enc
->coded_height
&&
627 (enc
->width
!= enc
->coded_width
||
628 enc
->height
!= enc
->coded_height
))
629 av_bprintf(&bprint
, " (%dx%d)",
630 enc
->coded_width
, enc
->coded_height
);
632 if (enc
->sample_aspect_ratio
.num
) {
633 av_reduce(&display_aspect_ratio
.num
, &display_aspect_ratio
.den
,
634 enc
->width
* (int64_t)enc
->sample_aspect_ratio
.num
,
635 enc
->height
* (int64_t)enc
->sample_aspect_ratio
.den
,
637 av_bprintf(&bprint
, " [SAR %d:%d DAR %d:%d]",
638 enc
->sample_aspect_ratio
.num
, enc
->sample_aspect_ratio
.den
,
639 display_aspect_ratio
.num
, display_aspect_ratio
.den
);
641 if (av_log_get_level() >= AV_LOG_DEBUG
) {
642 int g
= av_gcd(enc
->time_base
.num
, enc
->time_base
.den
);
643 av_bprintf(&bprint
, ", %d/%d",
644 enc
->time_base
.num
/ g
, enc
->time_base
.den
/ g
);
648 av_bprintf(&bprint
, ", q=%d-%d", enc
->qmin
, enc
->qmax
);
650 #if FF_API_CODEC_PROPS
651 FF_DISABLE_DEPRECATION_WARNINGS
652 if (enc
->properties
& FF_CODEC_PROPERTY_CLOSED_CAPTIONS
)
653 av_bprintf(&bprint
, ", Closed Captions");
654 if (enc
->properties
& FF_CODEC_PROPERTY_FILM_GRAIN
)
655 av_bprintf(&bprint
, ", Film Grain");
656 if (enc
->properties
& FF_CODEC_PROPERTY_LOSSLESS
)
657 av_bprintf(&bprint
, ", lossless");
658 FF_ENABLE_DEPRECATION_WARNINGS
662 case AVMEDIA_TYPE_AUDIO
:
663 av_bprintf(&bprint
, "%s", separator
);
665 if (enc
->sample_rate
) {
666 av_bprintf(&bprint
, "%d Hz, ", enc
->sample_rate
);
668 av_channel_layout_describe_bprint(&enc
->ch_layout
, &bprint
);
669 if (enc
->sample_fmt
!= AV_SAMPLE_FMT_NONE
&&
670 (str
= av_get_sample_fmt_name(enc
->sample_fmt
))) {
671 av_bprintf(&bprint
, ", %s", str
);
673 if ( enc
->bits_per_raw_sample
> 0
674 && enc
->bits_per_raw_sample
!= av_get_bytes_per_sample(enc
->sample_fmt
) * 8)
675 av_bprintf(&bprint
, " (%d bit)", enc
->bits_per_raw_sample
);
676 if (av_log_get_level() >= AV_LOG_VERBOSE
) {
677 if (enc
->initial_padding
)
678 av_bprintf(&bprint
, ", delay %d", enc
->initial_padding
);
679 if (enc
->trailing_padding
)
680 av_bprintf(&bprint
, ", padding %d", enc
->trailing_padding
);
683 case AVMEDIA_TYPE_DATA
:
684 if (av_log_get_level() >= AV_LOG_DEBUG
) {
685 int g
= av_gcd(enc
->time_base
.num
, enc
->time_base
.den
);
687 av_bprintf(&bprint
, ", %d/%d",
688 enc
->time_base
.num
/ g
, enc
->time_base
.den
/ g
);
691 case AVMEDIA_TYPE_SUBTITLE
:
693 av_bprintf(&bprint
, ", %dx%d", enc
->width
, enc
->height
);
699 if (enc
->flags
& AV_CODEC_FLAG_PASS1
)
700 av_bprintf(&bprint
, ", pass 1");
701 if (enc
->flags
& AV_CODEC_FLAG_PASS2
)
702 av_bprintf(&bprint
, ", pass 2");
704 bitrate
= get_bit_rate(enc
);
706 av_bprintf(&bprint
, ", %"PRId64
" kb/s", bitrate
/ 1000);
707 } else if (enc
->rc_max_rate
> 0) {
708 av_bprintf(&bprint
, ", max. %"PRId64
" kb/s", enc
->rc_max_rate
/ 1000);
712 int avcodec_is_open(AVCodecContext
*s
)
714 return !!s
->internal
;
717 int attribute_align_arg
avcodec_receive_frame(AVCodecContext
*avctx
, AVFrame
*frame
)
719 av_frame_unref(frame
);
721 if (av_codec_is_decoder(avctx
->codec
))
722 return ff_decode_receive_frame(avctx
, frame
);
723 return ff_encode_receive_frame(avctx
, frame
);
726 #define WRAP_CONFIG(allowed_type, field, field_type, terminator) \
728 static const field_type end = terminator; \
729 if (codec->type != (allowed_type)) \
730 return AVERROR(EINVAL); \
731 *out_configs = (field); \
732 if (out_num_configs) { \
733 for (int i = 0;; i++) { \
734 if (!(field) || !memcmp(&(field)[i], &end, sizeof(end))) { \
735 *out_num_configs = i; \
743 static const enum AVColorRange color_range_jpeg
[] = {
744 AVCOL_RANGE_JPEG
, AVCOL_RANGE_UNSPECIFIED
747 static const enum AVColorRange color_range_mpeg
[] = {
748 AVCOL_RANGE_MPEG
, AVCOL_RANGE_UNSPECIFIED
751 static const enum AVColorRange color_range_all
[] = {
752 AVCOL_RANGE_MPEG
, AVCOL_RANGE_JPEG
, AVCOL_RANGE_UNSPECIFIED
755 static const enum AVColorRange
*color_range_table
[] = {
756 [AVCOL_RANGE_MPEG
] = color_range_mpeg
,
757 [AVCOL_RANGE_JPEG
] = color_range_jpeg
,
758 [AVCOL_RANGE_MPEG
| AVCOL_RANGE_JPEG
] = color_range_all
,
761 int ff_default_get_supported_config(const AVCodecContext
*avctx
,
762 const AVCodec
*codec
,
763 enum AVCodecConfig config
,
765 const void **out_configs
,
766 int *out_num_configs
)
769 FF_DISABLE_DEPRECATION_WARNINGS
770 case AV_CODEC_CONFIG_PIX_FORMAT
:
771 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO
, codec
->pix_fmts
, enum AVPixelFormat
, AV_PIX_FMT_NONE
);
772 case AV_CODEC_CONFIG_FRAME_RATE
:
773 WRAP_CONFIG(AVMEDIA_TYPE_VIDEO
, codec
->supported_framerates
, AVRational
, {0});
774 case AV_CODEC_CONFIG_SAMPLE_RATE
:
775 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO
, codec
->supported_samplerates
, int, 0);
776 case AV_CODEC_CONFIG_SAMPLE_FORMAT
:
777 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO
, codec
->sample_fmts
, enum AVSampleFormat
, AV_SAMPLE_FMT_NONE
);
778 case AV_CODEC_CONFIG_CHANNEL_LAYOUT
:
779 WRAP_CONFIG(AVMEDIA_TYPE_AUDIO
, codec
->ch_layouts
, AVChannelLayout
, {0});
780 FF_ENABLE_DEPRECATION_WARNINGS
782 case AV_CODEC_CONFIG_COLOR_RANGE
:
783 if (codec
->type
!= AVMEDIA_TYPE_VIDEO
)
784 return AVERROR(EINVAL
);
785 *out_configs
= color_range_table
[ffcodec(codec
)->color_ranges
];
787 *out_num_configs
= av_popcount(ffcodec(codec
)->color_ranges
);
790 case AV_CODEC_CONFIG_COLOR_SPACE
:
793 *out_num_configs
= 0;
796 return AVERROR(EINVAL
);
800 int avcodec_get_supported_config(const AVCodecContext
*avctx
, const AVCodec
*codec
,
801 enum AVCodecConfig config
, unsigned flags
,
802 const void **out
, int *out_num
)
804 const FFCodec
*codec2
;
807 codec
= avctx
->codec
;
809 out_num
= &dummy_num
;
811 codec2
= ffcodec(codec
);
812 if (codec2
->get_supported_config
) {
813 return codec2
->get_supported_config(avctx
, codec
, config
, flags
, out
, out_num
);
815 return ff_default_get_supported_config(avctx
, codec
, config
, flags
, out
, out_num
);