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