]> git.sesse.net Git - ffmpeg/blob - libavcodec/avcodec.c
avcodec/avcodec: Move decoder channel count check to ff_decode_preinit
[ffmpeg] / 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/imgutils.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/thread.h"
33 #include "avcodec.h"
34 #include "decode.h"
35 #include "encode.h"
36 #include "frame_thread_encoder.h"
37 #include "internal.h"
38 #include "thread.h"
39
40 #include "libavutil/ffversion.h"
41 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
42
43 unsigned avcodec_version(void)
44 {
45     av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
46     av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
47     av_assert0(AV_CODEC_ID_SRT==94216);
48     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
49
50     return LIBAVCODEC_VERSION_INT;
51 }
52
53 const char *avcodec_configuration(void)
54 {
55     return FFMPEG_CONFIGURATION;
56 }
57
58 const char *avcodec_license(void)
59 {
60 #define LICENSE_PREFIX "libavcodec license: "
61     return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
62 }
63
64 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
65 {
66     int i;
67
68     for (i = 0; i < count; i++) {
69         int r = func(c, (char *)arg + i * size);
70         if (ret)
71             ret[i] = r;
72     }
73     emms_c();
74     return 0;
75 }
76
77 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
78 {
79     int i;
80
81     for (i = 0; i < count; i++) {
82         int r = func(c, arg, i, 0);
83         if (ret)
84             ret[i] = r;
85     }
86     emms_c();
87     return 0;
88 }
89
90 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
91
92 static void lock_avcodec(const AVCodec *codec)
93 {
94     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
95         ff_mutex_lock(&codec_mutex);
96 }
97
98 static void unlock_avcodec(const AVCodec *codec)
99 {
100     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
101         ff_mutex_unlock(&codec_mutex);
102 }
103
104 #if FF_API_LOCKMGR
105 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
106 {
107     return 0;
108 }
109 #endif
110
111 static int64_t get_bit_rate(AVCodecContext *ctx)
112 {
113     int64_t bit_rate;
114     int bits_per_sample;
115
116     switch (ctx->codec_type) {
117     case AVMEDIA_TYPE_VIDEO:
118     case AVMEDIA_TYPE_DATA:
119     case AVMEDIA_TYPE_SUBTITLE:
120     case AVMEDIA_TYPE_ATTACHMENT:
121         bit_rate = ctx->bit_rate;
122         break;
123     case AVMEDIA_TYPE_AUDIO:
124         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
125         if (bits_per_sample) {
126             bit_rate = ctx->sample_rate * (int64_t)ctx->channels;
127             if (bit_rate > INT64_MAX / bits_per_sample) {
128                 bit_rate = 0;
129             } else
130                 bit_rate *= bits_per_sample;
131         } else
132             bit_rate = ctx->bit_rate;
133         break;
134     default:
135         bit_rate = 0;
136         break;
137     }
138     return bit_rate;
139 }
140
141 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
142 {
143     int ret = 0;
144     int codec_init_ok = 0;
145     AVDictionary *tmp = NULL;
146     AVCodecInternal *avci;
147
148     if (avcodec_is_open(avctx))
149         return 0;
150
151     if (!codec && !avctx->codec) {
152         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
153         return AVERROR(EINVAL);
154     }
155     if (codec && avctx->codec && codec != avctx->codec) {
156         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
157                                     "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
158         return AVERROR(EINVAL);
159     }
160     if (!codec)
161         codec = avctx->codec;
162
163     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
164         avctx->codec_id == AV_CODEC_ID_NONE) {
165         avctx->codec_type = codec->type;
166         avctx->codec_id   = codec->id;
167     }
168     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type &&
169                                          avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
170         av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
171         return AVERROR(EINVAL);
172     }
173     avctx->codec = codec;
174
175     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
176         return AVERROR(EINVAL);
177
178     if (options)
179         av_dict_copy(&tmp, *options, 0);
180
181     lock_avcodec(codec);
182
183     avci = av_mallocz(sizeof(*avci));
184     if (!avci) {
185         ret = AVERROR(ENOMEM);
186         goto end;
187     }
188     avctx->internal = avci;
189
190 #if FF_API_OLD_ENCDEC
191     avci->to_free = av_frame_alloc();
192     avci->compat_decode_frame = av_frame_alloc();
193     avci->compat_encode_packet = av_packet_alloc();
194     if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
195         ret = AVERROR(ENOMEM);
196         goto free_and_end;
197     }
198 #endif
199     avci->buffer_frame = av_frame_alloc();
200     avci->buffer_pkt = av_packet_alloc();
201     avci->es.in_frame = av_frame_alloc();
202     avci->ds.in_pkt = av_packet_alloc();
203     avci->last_pkt_props = av_packet_alloc();
204     avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
205     if (!avci->buffer_frame || !avci->buffer_pkt          ||
206         !avci->es.in_frame  || !avci->ds.in_pkt           ||
207         !avci->last_pkt_props || !avci->pkt_props) {
208         ret = AVERROR(ENOMEM);
209         goto free_and_end;
210     }
211
212     avci->skip_samples_multiplier = 1;
213
214     if (codec->priv_data_size > 0) {
215         if (!avctx->priv_data) {
216             avctx->priv_data = av_mallocz(codec->priv_data_size);
217             if (!avctx->priv_data) {
218                 ret = AVERROR(ENOMEM);
219                 goto free_and_end;
220             }
221             if (codec->priv_class) {
222                 *(const AVClass **)avctx->priv_data = codec->priv_class;
223                 av_opt_set_defaults(avctx->priv_data);
224             }
225         }
226         if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
227             goto free_and_end;
228     } else {
229         avctx->priv_data = NULL;
230     }
231     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
232         goto free_and_end;
233
234     if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
235         av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
236         ret = AVERROR(EINVAL);
237         goto free_and_end;
238     }
239
240     // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
241     if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
242           (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
243         if (avctx->coded_width && avctx->coded_height)
244             ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
245         else if (avctx->width && avctx->height)
246             ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
247         if (ret < 0)
248             goto free_and_end;
249     }
250
251     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
252         && (  av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
253            || av_image_check_size2(avctx->width,       avctx->height,       avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
254         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
255         ff_set_dimensions(avctx, 0, 0);
256     }
257
258     if (avctx->width > 0 && avctx->height > 0) {
259         if (av_image_check_sar(avctx->width, avctx->height,
260                                avctx->sample_aspect_ratio) < 0) {
261             av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
262                    avctx->sample_aspect_ratio.num,
263                    avctx->sample_aspect_ratio.den);
264             avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
265         }
266     }
267
268     if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
269         av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
270         ret = AVERROR(EINVAL);
271         goto free_and_end;
272     }
273
274     if (avctx->sample_rate < 0) {
275         av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
276         ret = AVERROR(EINVAL);
277         goto free_and_end;
278     }
279     if (avctx->block_align < 0) {
280         av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
281         ret = AVERROR(EINVAL);
282         goto free_and_end;
283     }
284
285     avctx->frame_number = 0;
286     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
287
288     if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
289         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
290         const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
291         const AVCodec *codec2;
292         av_log(avctx, AV_LOG_ERROR,
293                "The %s '%s' is experimental but experimental codecs are not enabled, "
294                "add '-strict %d' if you want to use it.\n",
295                codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
296         codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
297         if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
298             av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
299                 codec_string, codec2->name);
300         ret = AVERROR_EXPERIMENTAL;
301         goto free_and_end;
302     }
303
304     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
305         (!avctx->time_base.num || !avctx->time_base.den)) {
306         avctx->time_base.num = 1;
307         avctx->time_base.den = avctx->sample_rate;
308     }
309
310     if (av_codec_is_encoder(avctx->codec))
311         ret = ff_encode_preinit(avctx);
312     else
313         ret = ff_decode_preinit(avctx);
314     if (ret < 0)
315         goto free_and_end;
316
317     if (!HAVE_THREADS)
318         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
319
320     if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
321         unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
322         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
323         lock_avcodec(codec);
324         if (ret < 0)
325             goto free_and_end;
326     }
327
328     if (HAVE_THREADS
329         && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
330         ret = ff_thread_init(avctx);
331         if (ret < 0) {
332             goto free_and_end;
333         }
334     }
335     if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
336         avctx->thread_count = 1;
337
338     if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
339         || avci->frame_thread_encoder)) {
340         ret = avctx->codec->init(avctx);
341         if (ret < 0) {
342             codec_init_ok = -1;
343             goto free_and_end;
344         }
345         codec_init_ok = 1;
346     }
347
348     ret=0;
349
350     if (av_codec_is_decoder(avctx->codec)) {
351         if (!avctx->bit_rate)
352             avctx->bit_rate = get_bit_rate(avctx);
353         /* validate channel layout from the decoder */
354         if (avctx->channel_layout) {
355             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
356             if (!avctx->channels)
357                 avctx->channels = channels;
358             else if (channels != avctx->channels) {
359                 char buf[512];
360                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
361                 av_log(avctx, AV_LOG_WARNING,
362                        "Channel layout '%s' with %d channels does not match specified number of channels %d: "
363                        "ignoring specified channel layout\n",
364                        buf, channels, avctx->channels);
365                 avctx->channel_layout = 0;
366             }
367         }
368         if (avctx->channels && avctx->channels < 0 ||
369             avctx->channels > FF_SANE_NB_CHANNELS) {
370             ret = AVERROR(EINVAL);
371             goto free_and_end;
372         }
373         if (avctx->bits_per_coded_sample < 0) {
374             ret = AVERROR(EINVAL);
375             goto free_and_end;
376         }
377
378 #if FF_API_AVCTX_TIMEBASE
379         if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
380             avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
381 #endif
382     }
383     if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
384         av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
385     }
386
387 end:
388     unlock_avcodec(codec);
389     if (options) {
390         av_dict_free(options);
391         *options = tmp;
392     }
393
394     return ret;
395 free_and_end:
396     if (avctx->codec && avctx->codec->close &&
397         (codec_init_ok > 0 || (codec_init_ok < 0 &&
398          avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
399         avctx->codec->close(avctx);
400
401     if (HAVE_THREADS && avci->thread_ctx)
402         ff_thread_free(avctx);
403
404     if (codec->priv_class && avctx->priv_data)
405         av_opt_free(avctx->priv_data);
406     av_opt_free(avctx);
407
408     if (av_codec_is_encoder(avctx->codec)) {
409 #if FF_API_CODED_FRAME
410 FF_DISABLE_DEPRECATION_WARNINGS
411         av_frame_free(&avctx->coded_frame);
412 FF_ENABLE_DEPRECATION_WARNINGS
413 #endif
414         av_freep(&avctx->extradata);
415         avctx->extradata_size = 0;
416     }
417
418     av_dict_free(&tmp);
419     av_freep(&avctx->priv_data);
420     av_freep(&avctx->subtitle_header);
421
422 #if FF_API_OLD_ENCDEC
423     av_frame_free(&avci->to_free);
424     av_frame_free(&avci->compat_decode_frame);
425     av_packet_free(&avci->compat_encode_packet);
426 #endif
427     av_frame_free(&avci->buffer_frame);
428     av_packet_free(&avci->buffer_pkt);
429     av_packet_free(&avci->last_pkt_props);
430     av_fifo_freep(&avci->pkt_props);
431
432     av_packet_free(&avci->ds.in_pkt);
433     av_frame_free(&avci->es.in_frame);
434     av_bsf_free(&avci->bsf);
435
436     av_buffer_unref(&avci->pool);
437     av_freep(&avci);
438     avctx->internal = NULL;
439     avctx->codec = NULL;
440     goto end;
441 }
442
443 void avcodec_flush_buffers(AVCodecContext *avctx)
444 {
445     AVCodecInternal *avci = avctx->internal;
446
447     if (av_codec_is_encoder(avctx->codec)) {
448         int caps = avctx->codec->capabilities;
449
450         if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
451             // Only encoders that explicitly declare support for it can be
452             // flushed. Otherwise, this is a no-op.
453             av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
454                    "that doesn't support it\n");
455             return;
456         }
457
458         // We haven't implemented flushing for frame-threaded encoders.
459         av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
460     }
461
462     avci->draining      = 0;
463     avci->draining_done = 0;
464     avci->nb_draining_errors = 0;
465     av_frame_unref(avci->buffer_frame);
466 #if FF_API_OLD_ENCDEC
467     av_frame_unref(avci->compat_decode_frame);
468     av_packet_unref(avci->compat_encode_packet);
469 #endif
470     av_packet_unref(avci->buffer_pkt);
471
472     av_packet_unref(avci->last_pkt_props);
473     while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
474         av_fifo_generic_read(avci->pkt_props,
475                              avci->last_pkt_props, sizeof(*avci->last_pkt_props),
476                              NULL);
477         av_packet_unref(avci->last_pkt_props);
478     }
479     av_fifo_reset(avci->pkt_props);
480
481     av_frame_unref(avci->es.in_frame);
482     av_packet_unref(avci->ds.in_pkt);
483
484     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
485         ff_thread_flush(avctx);
486     else if (avctx->codec->flush)
487         avctx->codec->flush(avctx);
488
489     avctx->pts_correction_last_pts =
490     avctx->pts_correction_last_dts = INT64_MIN;
491
492     if (av_codec_is_decoder(avctx->codec))
493         av_bsf_flush(avci->bsf);
494
495 #if FF_API_OLD_ENCDEC
496 FF_DISABLE_DEPRECATION_WARNINGS
497     if (!avctx->refcounted_frames)
498         av_frame_unref(avci->to_free);
499 FF_ENABLE_DEPRECATION_WARNINGS
500 #endif
501 }
502
503 void avsubtitle_free(AVSubtitle *sub)
504 {
505     int i;
506
507     for (i = 0; i < sub->num_rects; i++) {
508         av_freep(&sub->rects[i]->data[0]);
509         av_freep(&sub->rects[i]->data[1]);
510         av_freep(&sub->rects[i]->data[2]);
511         av_freep(&sub->rects[i]->data[3]);
512         av_freep(&sub->rects[i]->text);
513         av_freep(&sub->rects[i]->ass);
514         av_freep(&sub->rects[i]);
515     }
516
517     av_freep(&sub->rects);
518
519     memset(sub, 0, sizeof(*sub));
520 }
521
522 av_cold int avcodec_close(AVCodecContext *avctx)
523 {
524     int i;
525
526     if (!avctx)
527         return 0;
528
529     if (avcodec_is_open(avctx)) {
530         if (CONFIG_FRAME_THREAD_ENCODER &&
531             avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
532             ff_frame_thread_encoder_free(avctx);
533         }
534         if (HAVE_THREADS && avctx->internal->thread_ctx)
535             ff_thread_free(avctx);
536         if (avctx->codec && avctx->codec->close)
537             avctx->codec->close(avctx);
538         avctx->internal->byte_buffer_size = 0;
539         av_freep(&avctx->internal->byte_buffer);
540 #if FF_API_OLD_ENCDEC
541         av_frame_free(&avctx->internal->to_free);
542         av_frame_free(&avctx->internal->compat_decode_frame);
543         av_packet_free(&avctx->internal->compat_encode_packet);
544 #endif
545         av_frame_free(&avctx->internal->buffer_frame);
546         av_packet_free(&avctx->internal->buffer_pkt);
547         av_packet_unref(avctx->internal->last_pkt_props);
548         while (av_fifo_size(avctx->internal->pkt_props) >=
549                sizeof(*avctx->internal->last_pkt_props)) {
550             av_fifo_generic_read(avctx->internal->pkt_props,
551                                  avctx->internal->last_pkt_props,
552                                  sizeof(*avctx->internal->last_pkt_props),
553                                  NULL);
554             av_packet_unref(avctx->internal->last_pkt_props);
555         }
556         av_packet_free(&avctx->internal->last_pkt_props);
557         av_fifo_freep(&avctx->internal->pkt_props);
558
559         av_packet_free(&avctx->internal->ds.in_pkt);
560         av_frame_free(&avctx->internal->es.in_frame);
561
562         av_buffer_unref(&avctx->internal->pool);
563
564         if (avctx->hwaccel && avctx->hwaccel->uninit)
565             avctx->hwaccel->uninit(avctx);
566         av_freep(&avctx->internal->hwaccel_priv_data);
567
568         av_bsf_free(&avctx->internal->bsf);
569
570         av_freep(&avctx->internal);
571     }
572
573     for (i = 0; i < avctx->nb_coded_side_data; i++)
574         av_freep(&avctx->coded_side_data[i].data);
575     av_freep(&avctx->coded_side_data);
576     avctx->nb_coded_side_data = 0;
577
578     av_buffer_unref(&avctx->hw_frames_ctx);
579     av_buffer_unref(&avctx->hw_device_ctx);
580
581     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
582         av_opt_free(avctx->priv_data);
583     av_opt_free(avctx);
584     av_freep(&avctx->priv_data);
585     if (av_codec_is_encoder(avctx->codec)) {
586         av_freep(&avctx->extradata);
587 #if FF_API_CODED_FRAME
588 FF_DISABLE_DEPRECATION_WARNINGS
589         av_frame_free(&avctx->coded_frame);
590 FF_ENABLE_DEPRECATION_WARNINGS
591 #endif
592     }
593     avctx->codec = NULL;
594     avctx->active_thread_type = 0;
595
596     return 0;
597 }
598
599 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
600 {
601     const char *codec_type;
602     const char *codec_name;
603     const char *profile = NULL;
604     int64_t bitrate;
605     int new_line = 0;
606     AVRational display_aspect_ratio;
607     const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
608
609     if (!buf || buf_size <= 0)
610         return;
611     codec_type = av_get_media_type_string(enc->codec_type);
612     codec_name = avcodec_get_name(enc->codec_id);
613     profile = avcodec_profile_name(enc->codec_id, enc->profile);
614
615     snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
616              codec_name);
617     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
618
619     if (enc->codec && strcmp(enc->codec->name, codec_name))
620         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
621
622     if (profile)
623         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
624     if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
625         && av_log_get_level() >= AV_LOG_VERBOSE
626         && enc->refs)
627         snprintf(buf + strlen(buf), buf_size - strlen(buf),
628                  ", %d reference frame%s",
629                  enc->refs, enc->refs > 1 ? "s" : "");
630
631     if (enc->codec_tag)
632         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
633                  av_fourcc2str(enc->codec_tag), enc->codec_tag);
634
635     switch (enc->codec_type) {
636     case AVMEDIA_TYPE_VIDEO:
637         {
638             char detail[256] = "(";
639
640             av_strlcat(buf, separator, buf_size);
641
642             snprintf(buf + strlen(buf), buf_size - strlen(buf),
643                  "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
644                      av_get_pix_fmt_name(enc->pix_fmt));
645             if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
646                 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
647                 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
648             if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
649                 av_strlcatf(detail, sizeof(detail), "%s, ",
650                             av_color_range_name(enc->color_range));
651
652             if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
653                 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
654                 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
655                 if (enc->colorspace != (int)enc->color_primaries ||
656                     enc->colorspace != (int)enc->color_trc) {
657                     new_line = 1;
658                     av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
659                                 av_color_space_name(enc->colorspace),
660                                 av_color_primaries_name(enc->color_primaries),
661                                 av_color_transfer_name(enc->color_trc));
662                 } else
663                     av_strlcatf(detail, sizeof(detail), "%s, ",
664                                 av_get_colorspace_name(enc->colorspace));
665             }
666
667             if (enc->field_order != AV_FIELD_UNKNOWN) {
668                 const char *field_order = "progressive";
669                 if (enc->field_order == AV_FIELD_TT)
670                     field_order = "top first";
671                 else if (enc->field_order == AV_FIELD_BB)
672                     field_order = "bottom first";
673                 else if (enc->field_order == AV_FIELD_TB)
674                     field_order = "top coded first (swapped)";
675                 else if (enc->field_order == AV_FIELD_BT)
676                     field_order = "bottom coded first (swapped)";
677
678                 av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
679             }
680
681             if (av_log_get_level() >= AV_LOG_VERBOSE &&
682                 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
683                 av_strlcatf(detail, sizeof(detail), "%s, ",
684                             av_chroma_location_name(enc->chroma_sample_location));
685
686             if (strlen(detail) > 1) {
687                 detail[strlen(detail) - 2] = 0;
688                 av_strlcatf(buf, buf_size, "%s)", detail);
689             }
690         }
691
692         if (enc->width) {
693             av_strlcat(buf, new_line ? separator : ", ", buf_size);
694
695             snprintf(buf + strlen(buf), buf_size - strlen(buf),
696                      "%dx%d",
697                      enc->width, enc->height);
698
699             if (av_log_get_level() >= AV_LOG_VERBOSE &&
700                 (enc->width != enc->coded_width ||
701                  enc->height != enc->coded_height))
702                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
703                          " (%dx%d)", enc->coded_width, enc->coded_height);
704
705             if (enc->sample_aspect_ratio.num) {
706                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
707                           enc->width * (int64_t)enc->sample_aspect_ratio.num,
708                           enc->height * (int64_t)enc->sample_aspect_ratio.den,
709                           1024 * 1024);
710                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
711                          " [SAR %d:%d DAR %d:%d]",
712                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
713                          display_aspect_ratio.num, display_aspect_ratio.den);
714             }
715             if (av_log_get_level() >= AV_LOG_DEBUG) {
716                 int g = av_gcd(enc->time_base.num, enc->time_base.den);
717                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
718                          ", %d/%d",
719                          enc->time_base.num / g, enc->time_base.den / g);
720             }
721         }
722         if (encode) {
723             snprintf(buf + strlen(buf), buf_size - strlen(buf),
724                      ", q=%d-%d", enc->qmin, enc->qmax);
725         } else {
726             if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
727                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
728                          ", Closed Captions");
729             if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
730                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
731                          ", lossless");
732         }
733         break;
734     case AVMEDIA_TYPE_AUDIO:
735         av_strlcat(buf, separator, buf_size);
736
737         if (enc->sample_rate) {
738             snprintf(buf + strlen(buf), buf_size - strlen(buf),
739                      "%d Hz, ", enc->sample_rate);
740         }
741         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
742         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
743             snprintf(buf + strlen(buf), buf_size - strlen(buf),
744                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
745         }
746         if (   enc->bits_per_raw_sample > 0
747             && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
748             snprintf(buf + strlen(buf), buf_size - strlen(buf),
749                      " (%d bit)", enc->bits_per_raw_sample);
750         if (av_log_get_level() >= AV_LOG_VERBOSE) {
751             if (enc->initial_padding)
752                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
753                          ", delay %d", enc->initial_padding);
754             if (enc->trailing_padding)
755                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
756                          ", padding %d", enc->trailing_padding);
757         }
758         break;
759     case AVMEDIA_TYPE_DATA:
760         if (av_log_get_level() >= AV_LOG_DEBUG) {
761             int g = av_gcd(enc->time_base.num, enc->time_base.den);
762             if (g)
763                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
764                          ", %d/%d",
765                          enc->time_base.num / g, enc->time_base.den / g);
766         }
767         break;
768     case AVMEDIA_TYPE_SUBTITLE:
769         if (enc->width)
770             snprintf(buf + strlen(buf), buf_size - strlen(buf),
771                      ", %dx%d", enc->width, enc->height);
772         break;
773     default:
774         return;
775     }
776     if (encode) {
777         if (enc->flags & AV_CODEC_FLAG_PASS1)
778             snprintf(buf + strlen(buf), buf_size - strlen(buf),
779                      ", pass 1");
780         if (enc->flags & AV_CODEC_FLAG_PASS2)
781             snprintf(buf + strlen(buf), buf_size - strlen(buf),
782                      ", pass 2");
783     }
784     bitrate = get_bit_rate(enc);
785     if (bitrate != 0) {
786         snprintf(buf + strlen(buf), buf_size - strlen(buf),
787                  ", %"PRId64" kb/s", bitrate / 1000);
788     } else if (enc->rc_max_rate > 0) {
789         snprintf(buf + strlen(buf), buf_size - strlen(buf),
790                  ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
791     }
792 }
793
794 int avcodec_is_open(AVCodecContext *s)
795 {
796     return !!s->internal;
797 }