• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * AVCodecContext functions for libavcodec
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * AVCodecContext functions for libavcodec
24  */
25 
26 #include "config.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/fifo.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/thread.h"
36 #include "avcodec.h"
37 #include "bsf.h"
38 #include "codec_internal.h"
39 #include "decode.h"
40 #include "encode.h"
41 #include "frame_thread_encoder.h"
42 #include "internal.h"
43 #include "thread.h"
44 
avcodec_default_execute(AVCodecContext * c,int (* func)(AVCodecContext * c2,void * arg2),void * arg,int * ret,int count,int size)45 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
46 {
47     int i;
48 
49     for (i = 0; i < count; i++) {
50         int r = func(c, (char *)arg + i * size);
51         if (ret)
52             ret[i] = r;
53     }
54     emms_c();
55     return 0;
56 }
57 
avcodec_default_execute2(AVCodecContext * c,int (* func)(AVCodecContext * c2,void * arg2,int jobnr,int threadnr),void * arg,int * ret,int count)58 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
59 {
60     int i;
61 
62     for (i = 0; i < count; i++) {
63         int r = func(c, arg, i, 0);
64         if (ret)
65             ret[i] = r;
66     }
67     emms_c();
68     return 0;
69 }
70 
71 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
72 
lock_avcodec(const FFCodec * codec)73 static void lock_avcodec(const FFCodec *codec)
74 {
75     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
76         ff_mutex_lock(&codec_mutex);
77 }
78 
unlock_avcodec(const FFCodec * codec)79 static void unlock_avcodec(const FFCodec *codec)
80 {
81     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
82         ff_mutex_unlock(&codec_mutex);
83 }
84 
get_bit_rate(AVCodecContext * ctx)85 static int64_t get_bit_rate(AVCodecContext *ctx)
86 {
87     int64_t bit_rate;
88     int bits_per_sample;
89 
90     switch (ctx->codec_type) {
91     case AVMEDIA_TYPE_VIDEO:
92     case AVMEDIA_TYPE_DATA:
93     case AVMEDIA_TYPE_SUBTITLE:
94     case AVMEDIA_TYPE_ATTACHMENT:
95         bit_rate = ctx->bit_rate;
96         break;
97     case AVMEDIA_TYPE_AUDIO:
98         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
99         if (bits_per_sample) {
100             bit_rate = ctx->sample_rate * (int64_t)ctx->ch_layout.nb_channels;
101             if (bit_rate > INT64_MAX / bits_per_sample) {
102                 bit_rate = 0;
103             } else
104                 bit_rate *= bits_per_sample;
105         } else
106             bit_rate = ctx->bit_rate;
107         break;
108     default:
109         bit_rate = 0;
110         break;
111     }
112     return bit_rate;
113 }
114 
avcodec_open2(AVCodecContext * avctx,const AVCodec * codec,AVDictionary ** options)115 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
116 {
117     int ret = 0;
118     AVCodecInternal *avci;
119     const FFCodec *codec2;
120 
121     if (avcodec_is_open(avctx))
122         return 0;
123 
124     if (!codec && !avctx->codec) {
125         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
126         return AVERROR(EINVAL);
127     }
128     if (codec && avctx->codec && codec != avctx->codec) {
129         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
130                                     "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
131         return AVERROR(EINVAL);
132     }
133     if (!codec)
134         codec = avctx->codec;
135     codec2 = ffcodec(codec);
136 
137 #ifdef OHOS_AUXILIARY_TRACK
138     if ((avctx->codec_type != AVMEDIA_TYPE_AUXILIARY && avctx->codec_type != AVMEDIA_TYPE_UNKNOWN &&
139          avctx->codec_type != codec->type) ||
140         (avctx->codec_id   != AV_CODEC_ID_NONE     && avctx->codec_id   != codec->id)) {
141 #else
142     if ((avctx->codec_type != AVMEDIA_TYPE_UNKNOWN && avctx->codec_type != codec->type) ||
143         (avctx->codec_id   != AV_CODEC_ID_NONE     && avctx->codec_id   != codec->id)) {
144 #endif
145         av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
146         return AVERROR(EINVAL);
147     }
148 
149     avctx->codec_type = codec->type;
150     avctx->codec_id   = codec->id;
151     avctx->codec      = codec;
152 
153     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
154         return AVERROR(EINVAL);
155 
156     avci = av_mallocz(sizeof(*avci));
157     if (!avci) {
158         ret = AVERROR(ENOMEM);
159         goto end;
160     }
161     avctx->internal = avci;
162 
163     avci->buffer_frame = av_frame_alloc();
164     avci->buffer_pkt = av_packet_alloc();
165     if (!avci->buffer_frame || !avci->buffer_pkt) {
166         ret = AVERROR(ENOMEM);
167         goto free_and_end;
168     }
169 
170     avci->skip_samples_multiplier = 1;
171 
172     if (codec2->priv_data_size > 0) {
173         if (!avctx->priv_data) {
174             avctx->priv_data = av_mallocz(codec2->priv_data_size);
175             if (!avctx->priv_data) {
176                 ret = AVERROR(ENOMEM);
177                 goto free_and_end;
178             }
179             if (codec->priv_class) {
180                 *(const AVClass **)avctx->priv_data = codec->priv_class;
181                 av_opt_set_defaults(avctx->priv_data);
182             }
183         }
184         if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, options)) < 0)
185             goto free_and_end;
186     } else {
187         avctx->priv_data = NULL;
188     }
189     if ((ret = av_opt_set_dict(avctx, options)) < 0)
190         goto free_and_end;
191 
192     if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
193         av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
194         ret = AVERROR(EINVAL);
195         goto free_and_end;
196     }
197 
198     // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
199     if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
200           (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
201         if (avctx->coded_width && avctx->coded_height)
202             ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
203         else if (avctx->width && avctx->height)
204             ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
205         if (ret < 0)
206             goto free_and_end;
207     }
208 
209     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
210         && (  av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
211            || av_image_check_size2(avctx->width,       avctx->height,       avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
212         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
213         ff_set_dimensions(avctx, 0, 0);
214     }
215 
216     if (avctx->width > 0 && avctx->height > 0) {
217         if (av_image_check_sar(avctx->width, avctx->height,
218                                avctx->sample_aspect_ratio) < 0) {
219             av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
220                    avctx->sample_aspect_ratio.num,
221                    avctx->sample_aspect_ratio.den);
222             avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
223         }
224     }
225 
226     if (avctx->sample_rate < 0) {
227         av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
228         ret = AVERROR(EINVAL);
229         goto free_and_end;
230     }
231     if (avctx->block_align < 0) {
232         av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
233         ret = AVERROR(EINVAL);
234         goto free_and_end;
235     }
236 
237 #if FF_API_OLD_CHANNEL_LAYOUT
238 FF_DISABLE_DEPRECATION_WARNINGS
239     /* compat wrapper for old-style callers */
240     if (avctx->channel_layout && !avctx->channels)
241         avctx->channels = av_popcount64(avctx->channel_layout);
242 
243     if ((avctx->channels > 0 && avctx->ch_layout.nb_channels != avctx->channels) ||
244         (avctx->channel_layout && (avctx->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
245                                    avctx->ch_layout.u.mask != avctx->channel_layout))) {
246         if (avctx->channel_layout) {
247             av_channel_layout_from_mask(&avctx->ch_layout, avctx->channel_layout);
248         } else {
249             avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
250             avctx->ch_layout.nb_channels = avctx->channels;
251         }
252     }
253 FF_ENABLE_DEPRECATION_WARNINGS
254 #endif
255 
256     if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {
257         av_log(avctx, AV_LOG_ERROR, "Too many channels: %d\n", avctx->ch_layout.nb_channels);
258         ret = AVERROR(EINVAL);
259         goto free_and_end;
260     }
261 
262     avctx->frame_number = 0;
263     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
264 
265     if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
266         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
267         const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
268         const AVCodec *codec2;
269         av_log(avctx, AV_LOG_ERROR,
270                "The %s '%s' is experimental but experimental codecs are not enabled, "
271                "add '-strict %d' if you want to use it.\n",
272                codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
273         codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
274         if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
275             av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
276                 codec_string, codec2->name);
277         ret = AVERROR_EXPERIMENTAL;
278         goto free_and_end;
279     }
280 
281     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
282         (!avctx->time_base.num || !avctx->time_base.den)) {
283         avctx->time_base.num = 1;
284         avctx->time_base.den = avctx->sample_rate;
285     }
286 
287     if (av_codec_is_encoder(avctx->codec))
288         ret = ff_encode_preinit(avctx);
289     else
290         ret = ff_decode_preinit(avctx);
291     if (ret < 0)
292         goto free_and_end;
293 
294     if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
295         ret = ff_frame_thread_encoder_init(avctx);
296         if (ret < 0)
297             goto free_and_end;
298     }
299 
300     if (HAVE_THREADS
301         && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
302         /* Frame-threaded decoders call FFCodec.init for their child contexts. */
303         lock_avcodec(codec2);
304         ret = ff_thread_init(avctx);
305         unlock_avcodec(codec2);
306         if (ret < 0) {
307             goto free_and_end;
308         }
309     }
310     if (!HAVE_THREADS && !(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
311         avctx->thread_count = 1;
312 
313     if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
314         avci->frame_thread_encoder) {
315         if (codec2->init) {
316             lock_avcodec(codec2);
317             ret = codec2->init(avctx);
318             unlock_avcodec(codec2);
319             if (ret < 0) {
320                 avci->needs_close = codec2->caps_internal & FF_CODEC_CAP_INIT_CLEANUP;
321                 goto free_and_end;
322             }
323         }
324         avci->needs_close = 1;
325     }
326 
327     ret=0;
328 
329     if (av_codec_is_decoder(avctx->codec)) {
330         if (!avctx->bit_rate)
331             avctx->bit_rate = get_bit_rate(avctx);
332 
333 #if FF_API_OLD_CHANNEL_LAYOUT
334 FF_DISABLE_DEPRECATION_WARNINGS
335         /* update the deprecated fields for old-style callers */
336         avctx->channels = avctx->ch_layout.nb_channels;
337         avctx->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
338                                 avctx->ch_layout.u.mask : 0;
339 
340         /* validate channel layout from the decoder */
341         if (avctx->channel_layout) {
342             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
343             if (!avctx->channels)
344                 avctx->channels = channels;
345             else if (channels != avctx->channels) {
346                 char buf[512];
347                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
348                 av_log(avctx, AV_LOG_WARNING,
349                        "Channel layout '%s' with %d channels does not match specified number of channels %d: "
350                        "ignoring specified channel layout\n",
351                        buf, channels, avctx->channels);
352                 avctx->channel_layout = 0;
353             }
354         }
355         if (avctx->channels && avctx->channels < 0 ||
356             avctx->channels > FF_SANE_NB_CHANNELS) {
357             ret = AVERROR(EINVAL);
358             goto free_and_end;
359         }
360         if (avctx->bits_per_coded_sample < 0) {
361             ret = AVERROR(EINVAL);
362             goto free_and_end;
363         }
364 FF_ENABLE_DEPRECATION_WARNINGS
365 #endif
366 
367 #if FF_API_AVCTX_TIMEBASE
368         if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
369             avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
370 #endif
371     }
372     if (codec->priv_class)
373         av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
374 
375 end:
376 
377     return ret;
378 free_and_end:
379     avcodec_close(avctx);
380     goto end;
381 }
382 
383 void avcodec_flush_buffers(AVCodecContext *avctx)
384 {
385     AVCodecInternal *avci = avctx->internal;
386 
387     if (av_codec_is_encoder(avctx->codec)) {
388         int caps = avctx->codec->capabilities;
389 
390         if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
391             // Only encoders that explicitly declare support for it can be
392             // flushed. Otherwise, this is a no-op.
393             av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
394                    "that doesn't support it\n");
395             return;
396         }
397         if (avci->in_frame)
398             av_frame_unref(avci->in_frame);
399     } else {
400         av_packet_unref(avci->last_pkt_props);
401         while (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) >= 0)
402             av_packet_unref(avci->last_pkt_props);
403 
404         av_packet_unref(avci->in_pkt);
405 
406         avctx->pts_correction_last_pts =
407         avctx->pts_correction_last_dts = INT64_MIN;
408 
409         av_bsf_flush(avci->bsf);
410     }
411 
412     avci->draining      = 0;
413     avci->draining_done = 0;
414     avci->nb_draining_errors = 0;
415     av_frame_unref(avci->buffer_frame);
416     av_packet_unref(avci->buffer_pkt);
417 
418     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
419         ff_thread_flush(avctx);
420     else if (ffcodec(avctx->codec)->flush)
421         ffcodec(avctx->codec)->flush(avctx);
422 }
423 
424 void avsubtitle_free(AVSubtitle *sub)
425 {
426     int i;
427 
428     for (i = 0; i < sub->num_rects; i++) {
429         AVSubtitleRect *const rect = sub->rects[i];
430 
431         av_freep(&rect->data[0]);
432         av_freep(&rect->data[1]);
433         av_freep(&rect->data[2]);
434         av_freep(&rect->data[3]);
435         av_freep(&rect->text);
436         av_freep(&rect->ass);
437 
438         av_freep(&sub->rects[i]);
439     }
440 
441     av_freep(&sub->rects);
442 
443     memset(sub, 0, sizeof(*sub));
444 }
445 
446 av_cold int avcodec_close(AVCodecContext *avctx)
447 {
448     int i;
449 
450     if (!avctx)
451         return 0;
452 
453     if (avcodec_is_open(avctx)) {
454         AVCodecInternal *avci = avctx->internal;
455 
456         if (CONFIG_FRAME_THREAD_ENCODER &&
457             avci->frame_thread_encoder && avctx->thread_count > 1) {
458             ff_frame_thread_encoder_free(avctx);
459         }
460         if (HAVE_THREADS && avci->thread_ctx)
461             ff_thread_free(avctx);
462         if (avci->needs_close && ffcodec(avctx->codec)->close)
463             ffcodec(avctx->codec)->close(avctx);
464         avci->byte_buffer_size = 0;
465         av_freep(&avci->byte_buffer);
466         av_frame_free(&avci->buffer_frame);
467         av_packet_free(&avci->buffer_pkt);
468         if (avci->pkt_props) {
469             while (av_fifo_can_read(avci->pkt_props)) {
470                 av_packet_unref(avci->last_pkt_props);
471                 av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
472             }
473             av_fifo_freep2(&avci->pkt_props);
474         }
475         av_packet_free(&avci->last_pkt_props);
476 
477         av_packet_free(&avci->in_pkt);
478         av_frame_free(&avci->in_frame);
479 
480         av_buffer_unref(&avci->pool);
481 
482         if (avctx->hwaccel && avctx->hwaccel->uninit)
483             avctx->hwaccel->uninit(avctx);
484         av_freep(&avci->hwaccel_priv_data);
485 
486         av_bsf_free(&avci->bsf);
487 
488         av_channel_layout_uninit(&avci->initial_ch_layout);
489 
490         av_freep(&avctx->internal);
491     }
492 
493     for (i = 0; i < avctx->nb_coded_side_data; i++)
494         av_freep(&avctx->coded_side_data[i].data);
495     av_freep(&avctx->coded_side_data);
496     avctx->nb_coded_side_data = 0;
497 
498     av_buffer_unref(&avctx->hw_frames_ctx);
499     av_buffer_unref(&avctx->hw_device_ctx);
500 
501     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
502         av_opt_free(avctx->priv_data);
503     av_opt_free(avctx);
504     av_freep(&avctx->priv_data);
505     if (av_codec_is_encoder(avctx->codec)) {
506         av_freep(&avctx->extradata);
507         avctx->extradata_size = 0;
508     } else if (av_codec_is_decoder(avctx->codec))
509         av_freep(&avctx->subtitle_header);
510 
511     avctx->codec = NULL;
512     avctx->active_thread_type = 0;
513 
514     return 0;
515 }
516 
517 static const char *unknown_if_null(const char *str)
518 {
519     return str ? str : "unknown";
520 }
521 
522 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
523 {
524     const char *codec_type;
525     const char *codec_name;
526     const char *profile = NULL;
527     AVBPrint bprint;
528     int64_t bitrate;
529     int new_line = 0;
530     AVRational display_aspect_ratio;
531     const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
532     const char *str;
533 
534     if (!buf || buf_size <= 0)
535         return;
536     av_bprint_init_for_buffer(&bprint, buf, buf_size);
537     codec_type = av_get_media_type_string(enc->codec_type);
538     codec_name = avcodec_get_name(enc->codec_id);
539     profile = avcodec_profile_name(enc->codec_id, enc->profile);
540 
541     av_bprintf(&bprint, "%s: %s", codec_type ? codec_type : "unknown",
542                codec_name);
543     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
544 
545     if (enc->codec && strcmp(enc->codec->name, codec_name))
546         av_bprintf(&bprint, " (%s)", enc->codec->name);
547 
548     if (profile)
549         av_bprintf(&bprint, " (%s)", profile);
550     if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
551         && av_log_get_level() >= AV_LOG_VERBOSE
552         && enc->refs)
553         av_bprintf(&bprint, ", %d reference frame%s",
554                    enc->refs, enc->refs > 1 ? "s" : "");
555 
556     if (enc->codec_tag)
557         av_bprintf(&bprint, " (%s / 0x%04X)",
558                    av_fourcc2str(enc->codec_tag), enc->codec_tag);
559 
560     switch (enc->codec_type) {
561     case AVMEDIA_TYPE_VIDEO:
562         {
563             unsigned len;
564 
565             av_bprintf(&bprint, "%s%s", separator,
566                        enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
567                        unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
568 
569             av_bprint_chars(&bprint, '(', 1);
570             len = bprint.len;
571 
572             /* The following check ensures that '(' has been written
573              * and therefore allows us to erase it if it turns out
574              * to be unnecessary. */
575             if (!av_bprint_is_complete(&bprint))
576                 return;
577 
578             if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
579                 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
580                 av_bprintf(&bprint, "%d bpc, ", enc->bits_per_raw_sample);
581             if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
582                 (str = av_color_range_name(enc->color_range)))
583                 av_bprintf(&bprint, "%s, ", str);
584 
585             if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
586                 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
587                 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
588                 const char *col = unknown_if_null(av_color_space_name(enc->colorspace));
589                 const char *pri = unknown_if_null(av_color_primaries_name(enc->color_primaries));
590                 const char *trc = unknown_if_null(av_color_transfer_name(enc->color_trc));
591                 if (strcmp(col, pri) || strcmp(col, trc)) {
592                     new_line = 1;
593                     av_bprintf(&bprint, "%s/%s/%s, ", col, pri, trc);
594                 } else
595                     av_bprintf(&bprint, "%s, ", col);
596             }
597 
598             if (enc->field_order != AV_FIELD_UNKNOWN) {
599                 const char *field_order = "progressive";
600                 if (enc->field_order == AV_FIELD_TT)
601                     field_order = "top first";
602                 else if (enc->field_order == AV_FIELD_BB)
603                     field_order = "bottom first";
604                 else if (enc->field_order == AV_FIELD_TB)
605                     field_order = "top coded first (swapped)";
606                 else if (enc->field_order == AV_FIELD_BT)
607                     field_order = "bottom coded first (swapped)";
608 
609                 av_bprintf(&bprint, "%s, ", field_order);
610             }
611 
612             if (av_log_get_level() >= AV_LOG_VERBOSE &&
613                 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
614                 (str = av_chroma_location_name(enc->chroma_sample_location)))
615                 av_bprintf(&bprint, "%s, ", str);
616 
617             if (len == bprint.len) {
618                 bprint.str[len - 1] = '\0';
619                 bprint.len--;
620             } else {
621                 if (bprint.len - 2 < bprint.size) {
622                     /* Erase the last ", " */
623                     bprint.len -= 2;
624                     bprint.str[bprint.len] = '\0';
625                 }
626                 av_bprint_chars(&bprint, ')', 1);
627             }
628         }
629 
630         if (enc->width) {
631             av_bprintf(&bprint, "%s%dx%d", new_line ? separator : ", ",
632                        enc->width, enc->height);
633 
634             if (av_log_get_level() >= AV_LOG_VERBOSE &&
635                 (enc->width != enc->coded_width ||
636                  enc->height != enc->coded_height))
637                 av_bprintf(&bprint, " (%dx%d)",
638                            enc->coded_width, enc->coded_height);
639 
640             if (enc->sample_aspect_ratio.num) {
641                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
642                           enc->width * (int64_t)enc->sample_aspect_ratio.num,
643                           enc->height * (int64_t)enc->sample_aspect_ratio.den,
644                           1024 * 1024);
645                 av_bprintf(&bprint, " [SAR %d:%d DAR %d:%d]",
646                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
647                          display_aspect_ratio.num, display_aspect_ratio.den);
648             }
649             if (av_log_get_level() >= AV_LOG_DEBUG) {
650                 int g = av_gcd(enc->time_base.num, enc->time_base.den);
651                 av_bprintf(&bprint, ", %d/%d",
652                            enc->time_base.num / g, enc->time_base.den / g);
653             }
654         }
655         if (encode) {
656             av_bprintf(&bprint, ", q=%d-%d", enc->qmin, enc->qmax);
657         } else {
658             if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
659                 av_bprintf(&bprint, ", Closed Captions");
660             if (enc->properties & FF_CODEC_PROPERTY_FILM_GRAIN)
661                 av_bprintf(&bprint, ", Film Grain");
662             if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
663                 av_bprintf(&bprint, ", lossless");
664         }
665         break;
666     case AVMEDIA_TYPE_AUDIO:
667         av_bprintf(&bprint, "%s", separator);
668 
669         if (enc->sample_rate) {
670             av_bprintf(&bprint, "%d Hz, ", enc->sample_rate);
671         }
672         {
673             char buf[512];
674             int ret = av_channel_layout_describe(&enc->ch_layout, buf, sizeof(buf));
675             if (ret >= 0)
676                 av_bprintf(&bprint, "%s", buf);
677         }
678         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE &&
679             (str = av_get_sample_fmt_name(enc->sample_fmt))) {
680             av_bprintf(&bprint, ", %s", str);
681         }
682         if (   enc->bits_per_raw_sample > 0
683             && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
684             av_bprintf(&bprint, " (%d bit)", enc->bits_per_raw_sample);
685         if (av_log_get_level() >= AV_LOG_VERBOSE) {
686             if (enc->initial_padding)
687                 av_bprintf(&bprint, ", delay %d", enc->initial_padding);
688             if (enc->trailing_padding)
689                 av_bprintf(&bprint, ", padding %d", enc->trailing_padding);
690         }
691         break;
692     case AVMEDIA_TYPE_DATA:
693         if (av_log_get_level() >= AV_LOG_DEBUG) {
694             int g = av_gcd(enc->time_base.num, enc->time_base.den);
695             if (g)
696                 av_bprintf(&bprint, ", %d/%d",
697                            enc->time_base.num / g, enc->time_base.den / g);
698         }
699         break;
700     case AVMEDIA_TYPE_SUBTITLE:
701         if (enc->width)
702             av_bprintf(&bprint, ", %dx%d", enc->width, enc->height);
703         break;
704     default:
705         return;
706     }
707     if (encode) {
708         if (enc->flags & AV_CODEC_FLAG_PASS1)
709             av_bprintf(&bprint, ", pass 1");
710         if (enc->flags & AV_CODEC_FLAG_PASS2)
711             av_bprintf(&bprint, ", pass 2");
712     }
713     bitrate = get_bit_rate(enc);
714     if (bitrate != 0) {
715         av_bprintf(&bprint, ", %"PRId64" kb/s", bitrate / 1000);
716     } else if (enc->rc_max_rate > 0) {
717         av_bprintf(&bprint, ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
718     }
719 }
720 
721 int avcodec_is_open(AVCodecContext *s)
722 {
723     return !!s->internal;
724 }
725