]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
pcmdec: use unique classes for all pcm demuxers.
[ffmpeg] / libavcodec / utils.c
1 /*
2  * utils for libavcodec
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * utils.
26  */
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
40 #include "thread.h"
41 #include "audioconvert.h"
42 #include "internal.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <limits.h>
46 #include <float.h>
47
48 static int volatile entangled_thread_counter=0;
49 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
50 static void *codec_mutex;
51
52 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
53 {
54     if(min_size < *size)
55         return ptr;
56
57     min_size= FFMAX(17*min_size/16 + 32, min_size);
58
59     ptr= av_realloc(ptr, min_size);
60     if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
61         min_size= 0;
62
63     *size= min_size;
64
65     return ptr;
66 }
67
68 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
69 {
70     void **p = ptr;
71     if (min_size < *size)
72         return;
73     min_size= FFMAX(17*min_size/16 + 32, min_size);
74     av_free(*p);
75     *p = av_malloc(min_size);
76     if (!*p) min_size = 0;
77     *size= min_size;
78 }
79
80 /* encoder management */
81 static AVCodec *first_avcodec = NULL;
82
83 AVCodec *av_codec_next(AVCodec *c){
84     if(c) return c->next;
85     else  return first_avcodec;
86 }
87
88 void avcodec_register(AVCodec *codec)
89 {
90     AVCodec **p;
91     avcodec_init();
92     p = &first_avcodec;
93     while (*p != NULL) p = &(*p)->next;
94     *p = codec;
95     codec->next = NULL;
96 }
97
98 unsigned avcodec_get_edge_width(void)
99 {
100     return EDGE_WIDTH;
101 }
102
103 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
104     s->coded_width = width;
105     s->coded_height= height;
106     s->width = -((-width )>>s->lowres);
107     s->height= -((-height)>>s->lowres);
108 }
109
110 typedef struct InternalBuffer{
111     int last_pic_num;
112     uint8_t *base[4];
113     uint8_t *data[4];
114     int linesize[4];
115     int width, height;
116     enum PixelFormat pix_fmt;
117 }InternalBuffer;
118
119 #define INTERNAL_BUFFER_SIZE (32+1)
120
121 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
122     int w_align= 1;
123     int h_align= 1;
124
125     switch(s->pix_fmt){
126     case PIX_FMT_YUV420P:
127     case PIX_FMT_YUYV422:
128     case PIX_FMT_UYVY422:
129     case PIX_FMT_YUV422P:
130     case PIX_FMT_YUV440P:
131     case PIX_FMT_YUV444P:
132     case PIX_FMT_GRAY8:
133     case PIX_FMT_GRAY16BE:
134     case PIX_FMT_GRAY16LE:
135     case PIX_FMT_YUVJ420P:
136     case PIX_FMT_YUVJ422P:
137     case PIX_FMT_YUVJ440P:
138     case PIX_FMT_YUVJ444P:
139     case PIX_FMT_YUVA420P:
140     case PIX_FMT_YUV420P9LE:
141     case PIX_FMT_YUV420P9BE:
142     case PIX_FMT_YUV420P10LE:
143     case PIX_FMT_YUV420P10BE:
144     case PIX_FMT_YUV422P10LE:
145     case PIX_FMT_YUV422P10BE:
146     case PIX_FMT_YUV444P9LE:
147     case PIX_FMT_YUV444P9BE:
148     case PIX_FMT_YUV444P10LE:
149     case PIX_FMT_YUV444P10BE:
150         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
151         h_align= 16;
152         if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
153             h_align= 32; // interlaced is rounded up to 2 MBs
154         break;
155     case PIX_FMT_YUV411P:
156     case PIX_FMT_UYYVYY411:
157         w_align=32;
158         h_align=8;
159         break;
160     case PIX_FMT_YUV410P:
161         if(s->codec_id == CODEC_ID_SVQ1){
162             w_align=64;
163             h_align=64;
164         }
165     case PIX_FMT_RGB555:
166         if(s->codec_id == CODEC_ID_RPZA){
167             w_align=4;
168             h_align=4;
169         }
170     case PIX_FMT_PAL8:
171     case PIX_FMT_BGR8:
172     case PIX_FMT_RGB8:
173         if(s->codec_id == CODEC_ID_SMC){
174             w_align=4;
175             h_align=4;
176         }
177         break;
178     case PIX_FMT_BGR24:
179         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
180             w_align=4;
181             h_align=4;
182         }
183         break;
184     default:
185         w_align= 1;
186         h_align= 1;
187         break;
188     }
189
190     *width = FFALIGN(*width , w_align);
191     *height= FFALIGN(*height, h_align);
192     if(s->codec_id == CODEC_ID_H264 || s->lowres)
193         *height+=2; // some of the optimized chroma MC reads one line too much
194                     // which is also done in mpeg decoders with lowres > 0
195
196     linesize_align[0] =
197     linesize_align[1] =
198     linesize_align[2] =
199     linesize_align[3] = STRIDE_ALIGN;
200 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
201 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
202 //picture size unneccessarily in some cases. The solution here is not
203 //pretty and better ideas are welcome!
204 #if HAVE_MMX
205     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
206        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
207        s->codec_id == CODEC_ID_VP6A) {
208         linesize_align[0] =
209         linesize_align[1] =
210         linesize_align[2] = 16;
211     }
212 #endif
213 }
214
215 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
216     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
217     int linesize_align[4];
218     int align;
219     avcodec_align_dimensions2(s, width, height, linesize_align);
220     align = FFMAX(linesize_align[0], linesize_align[3]);
221     linesize_align[1] <<= chroma_shift;
222     linesize_align[2] <<= chroma_shift;
223     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
224     *width=FFALIGN(*width, align);
225 }
226
227 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
228     int i;
229     int w= s->width;
230     int h= s->height;
231     InternalBuffer *buf;
232     int *picture_number;
233
234     if(pic->data[0]!=NULL) {
235         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
236         return -1;
237     }
238     if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
239         av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
240         return -1;
241     }
242
243     if(av_image_check_size(w, h, 0, s))
244         return -1;
245
246     if(s->internal_buffer==NULL){
247         s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
248     }
249 #if 0
250     s->internal_buffer= av_fast_realloc(
251         s->internal_buffer,
252         &s->internal_buffer_size,
253         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
254         );
255 #endif
256
257     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
258     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
259     (*picture_number)++;
260
261     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
262         if(s->active_thread_type&FF_THREAD_FRAME) {
263             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
264             return -1;
265         }
266
267         for(i=0; i<4; i++){
268             av_freep(&buf->base[i]);
269             buf->data[i]= NULL;
270         }
271     }
272
273     if(buf->base[0]){
274         pic->age= *picture_number - buf->last_pic_num;
275         buf->last_pic_num= *picture_number;
276     }else{
277         int h_chroma_shift, v_chroma_shift;
278         int size[4] = {0};
279         int tmpsize;
280         int unaligned;
281         AVPicture picture;
282         int stride_align[4];
283         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
284
285         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
286
287         avcodec_align_dimensions2(s, &w, &h, stride_align);
288
289         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
290             w+= EDGE_WIDTH*2;
291             h+= EDGE_WIDTH*2;
292         }
293
294         do {
295             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
296             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
297             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
298             // increase alignment of w for next try (rhs gives the lowest bit set in w)
299             w += w & ~(w-1);
300
301             unaligned = 0;
302             for (i=0; i<4; i++){
303                 unaligned |= picture.linesize[i] % stride_align[i];
304             }
305         } while (unaligned);
306
307         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
308         if (tmpsize < 0)
309             return -1;
310
311         for (i=0; i<3 && picture.data[i+1]; i++)
312             size[i] = picture.data[i+1] - picture.data[i];
313         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
314
315         buf->last_pic_num= -256*256*256*64;
316         memset(buf->base, 0, sizeof(buf->base));
317         memset(buf->data, 0, sizeof(buf->data));
318
319         for(i=0; i<4 && size[i]; i++){
320             const int h_shift= i==0 ? 0 : h_chroma_shift;
321             const int v_shift= i==0 ? 0 : v_chroma_shift;
322
323             buf->linesize[i]= picture.linesize[i];
324
325             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
326             if(buf->base[i]==NULL) return -1;
327             memset(buf->base[i], 128, size[i]);
328
329             // no edge if EDGE EMU or not planar YUV
330             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
331                 buf->data[i] = buf->base[i];
332             else
333                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
334         }
335         if(size[1] && !size[2])
336             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
337         buf->width  = s->width;
338         buf->height = s->height;
339         buf->pix_fmt= s->pix_fmt;
340         pic->age= 256*256*256*64;
341     }
342     pic->type= FF_BUFFER_TYPE_INTERNAL;
343
344     for(i=0; i<4; i++){
345         pic->base[i]= buf->base[i];
346         pic->data[i]= buf->data[i];
347         pic->linesize[i]= buf->linesize[i];
348     }
349     s->internal_buffer_count++;
350
351     if(s->pkt) pic->pkt_pts= s->pkt->pts;
352     else       pic->pkt_pts= AV_NOPTS_VALUE;
353     pic->reordered_opaque= s->reordered_opaque;
354
355     if(s->debug&FF_DEBUG_BUFFERS)
356         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
357
358     return 0;
359 }
360
361 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
362     int i;
363     InternalBuffer *buf, *last;
364
365     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
366     assert(s->internal_buffer_count);
367
368     if(s->internal_buffer){
369     buf = NULL; /* avoids warning */
370     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
371         buf= &((InternalBuffer*)s->internal_buffer)[i];
372         if(buf->data[0] == pic->data[0])
373             break;
374     }
375     assert(i < s->internal_buffer_count);
376     s->internal_buffer_count--;
377     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
378
379     FFSWAP(InternalBuffer, *buf, *last);
380     }
381
382     for(i=0; i<4; i++){
383         pic->data[i]=NULL;
384 //        pic->base[i]=NULL;
385     }
386 //printf("R%X\n", pic->opaque);
387
388     if(s->debug&FF_DEBUG_BUFFERS)
389         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
390 }
391
392 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
393     AVFrame temp_pic;
394     int i;
395
396     /* If no picture return a new buffer */
397     if(pic->data[0] == NULL) {
398         /* We will copy from buffer, so must be readable */
399         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
400         return s->get_buffer(s, pic);
401     }
402
403     /* If internal buffer type return the same buffer */
404     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
405         if(s->pkt) pic->pkt_pts= s->pkt->pts;
406         else       pic->pkt_pts= AV_NOPTS_VALUE;
407         pic->reordered_opaque= s->reordered_opaque;
408         return 0;
409     }
410
411     /*
412      * Not internal type and reget_buffer not overridden, emulate cr buffer
413      */
414     temp_pic = *pic;
415     for(i = 0; i < 4; i++)
416         pic->data[i] = pic->base[i] = NULL;
417     pic->opaque = NULL;
418     /* Allocate new frame */
419     if (s->get_buffer(s, pic))
420         return -1;
421     /* Copy image data from old buffer to new buffer */
422     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
423              s->height);
424     s->release_buffer(s, &temp_pic); // Release old frame
425     return 0;
426 }
427
428 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
429     int i;
430
431     for(i=0; i<count; i++){
432         int r= func(c, (char*)arg + i*size);
433         if(ret) ret[i]= r;
434     }
435     return 0;
436 }
437
438 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
439     int i;
440
441     for(i=0; i<count; i++){
442         int r= func(c, arg, i, 0);
443         if(ret) ret[i]= r;
444     }
445     return 0;
446 }
447
448 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
449     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
450         ++fmt;
451     return fmt[0];
452 }
453
454 void avcodec_get_frame_defaults(AVFrame *pic){
455     memset(pic, 0, sizeof(AVFrame));
456
457     pic->pts= AV_NOPTS_VALUE;
458     pic->key_frame= 1;
459 }
460
461 AVFrame *avcodec_alloc_frame(void){
462     AVFrame *pic= av_malloc(sizeof(AVFrame));
463
464     if(pic==NULL) return NULL;
465
466     avcodec_get_frame_defaults(pic);
467
468     return pic;
469 }
470
471 #if FF_API_AVCODEC_OPEN
472 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
473 {
474     return avcodec_open2(avctx, codec, NULL);
475 }
476 #endif
477
478 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
479 {
480     int ret = 0;
481     AVDictionary *tmp = NULL;
482
483     if (options)
484         av_dict_copy(&tmp, *options, 0);
485
486     /* If there is a user-supplied mutex locking routine, call it. */
487     if (ff_lockmgr_cb) {
488         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
489             return -1;
490     }
491
492     entangled_thread_counter++;
493     if(entangled_thread_counter != 1){
494         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
495         ret = -1;
496         goto end;
497     }
498
499     if(avctx->codec || !codec) {
500         ret = AVERROR(EINVAL);
501         goto end;
502     }
503
504     if (codec->priv_data_size > 0) {
505       if(!avctx->priv_data){
506         avctx->priv_data = av_mallocz(codec->priv_data_size);
507         if (!avctx->priv_data) {
508             ret = AVERROR(ENOMEM);
509             goto end;
510         }
511         if (codec->priv_class) {
512             *(AVClass**)avctx->priv_data= codec->priv_class;
513             av_opt_set_defaults(avctx->priv_data);
514         }
515       }
516       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
517           goto free_and_end;
518     } else {
519         avctx->priv_data = NULL;
520     }
521     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
522         goto free_and_end;
523
524     if(avctx->coded_width && avctx->coded_height)
525         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
526     else if(avctx->width && avctx->height)
527         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
528
529     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
530         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
531            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
532         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
533         avcodec_set_dimensions(avctx, 0, 0);
534     }
535
536     /* if the decoder init function was already called previously,
537        free the already allocated subtitle_header before overwriting it */
538     if (codec->decode)
539         av_freep(&avctx->subtitle_header);
540
541 #define SANE_NB_CHANNELS 128U
542     if (avctx->channels > SANE_NB_CHANNELS) {
543         ret = AVERROR(EINVAL);
544         goto free_and_end;
545     }
546
547     avctx->codec = codec;
548     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
549         avctx->codec_id == CODEC_ID_NONE) {
550         avctx->codec_type = codec->type;
551         avctx->codec_id   = codec->id;
552     }
553     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
554                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
555         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
556         ret = AVERROR(EINVAL);
557         goto free_and_end;
558     }
559     avctx->frame_number = 0;
560
561     if (HAVE_THREADS && !avctx->thread_opaque) {
562         ret = ff_thread_init(avctx);
563         if (ret < 0) {
564             goto free_and_end;
565         }
566     }
567
568     if (avctx->codec->max_lowres < avctx->lowres) {
569         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
570                avctx->codec->max_lowres);
571         ret = AVERROR(EINVAL);
572         goto free_and_end;
573     }
574     if (avctx->codec->encode) {
575         int i;
576         if (avctx->codec->sample_fmts) {
577             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
578                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
579                     break;
580             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
581                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
582                 ret = AVERROR(EINVAL);
583                 goto free_and_end;
584             }
585         }
586         if (avctx->codec->supported_samplerates) {
587             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
588                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
589                     break;
590             if (avctx->codec->supported_samplerates[i] == 0) {
591                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
592                 ret = AVERROR(EINVAL);
593                 goto free_and_end;
594             }
595         }
596         if (avctx->codec->channel_layouts) {
597             if (!avctx->channel_layout) {
598                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
599             } else {
600                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
601                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
602                         break;
603                 if (avctx->codec->channel_layouts[i] == 0) {
604                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
605                     ret = AVERROR(EINVAL);
606                     goto free_and_end;
607                 }
608             }
609         }
610         if (avctx->channel_layout && avctx->channels) {
611             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
612                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
613                 ret = AVERROR(EINVAL);
614                 goto free_and_end;
615             }
616         } else if (avctx->channel_layout) {
617             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
618         }
619     }
620
621     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
622         ret = avctx->codec->init(avctx);
623         if (ret < 0) {
624             goto free_and_end;
625         }
626     }
627 end:
628     entangled_thread_counter--;
629
630     /* Release any user-supplied mutex. */
631     if (ff_lockmgr_cb) {
632         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
633     }
634     if (options) {
635         av_dict_free(options);
636         *options = tmp;
637     }
638
639     return ret;
640 free_and_end:
641     av_dict_free(&tmp);
642     av_freep(&avctx->priv_data);
643     avctx->codec= NULL;
644     goto end;
645 }
646
647 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
648                          const short *samples)
649 {
650     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
651         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
652         return -1;
653     }
654     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
655         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
656         avctx->frame_number++;
657         return ret;
658     }else
659         return 0;
660 }
661
662 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
663                          const AVFrame *pict)
664 {
665     if(buf_size < FF_MIN_BUFFER_SIZE){
666         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
667         return -1;
668     }
669     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
670         return -1;
671     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
672         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
673         avctx->frame_number++;
674         emms_c(); //needed to avoid an emms_c() call before every return;
675
676         return ret;
677     }else
678         return 0;
679 }
680
681 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
682                             const AVSubtitle *sub)
683 {
684     int ret;
685     if(sub->start_display_time) {
686         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
687         return -1;
688     }
689     if(sub->num_rects == 0 || !sub->rects)
690         return -1;
691     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
692     avctx->frame_number++;
693     return ret;
694 }
695
696 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
697                          int *got_picture_ptr,
698                          AVPacket *avpkt)
699 {
700     int ret;
701
702     *got_picture_ptr= 0;
703     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
704         return -1;
705
706     avctx->pkt = avpkt;
707
708     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
709         if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
710              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
711                                           avpkt);
712         else {
713             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
714                               avpkt);
715             picture->pkt_dts= avpkt->dts;
716         }
717
718         emms_c(); //needed to avoid an emms_c() call before every return;
719
720         if (*got_picture_ptr)
721             avctx->frame_number++;
722     }else
723         ret= 0;
724
725     return ret;
726 }
727
728 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
729                          int *frame_size_ptr,
730                          AVPacket *avpkt)
731 {
732     int ret;
733
734     avctx->pkt = avpkt;
735
736     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
737         //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
738         if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
739             av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
740             return -1;
741         }
742         if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
743         *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
744             av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
745             return -1;
746         }
747
748         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
749         avctx->frame_number++;
750     }else{
751         ret= 0;
752         *frame_size_ptr=0;
753     }
754     return ret;
755 }
756
757 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
758                             int *got_sub_ptr,
759                             AVPacket *avpkt)
760 {
761     int ret;
762
763     avctx->pkt = avpkt;
764     *got_sub_ptr = 0;
765     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
766     if (*got_sub_ptr)
767         avctx->frame_number++;
768     return ret;
769 }
770
771 void avsubtitle_free(AVSubtitle *sub)
772 {
773     int i;
774
775     for (i = 0; i < sub->num_rects; i++)
776     {
777         av_freep(&sub->rects[i]->pict.data[0]);
778         av_freep(&sub->rects[i]->pict.data[1]);
779         av_freep(&sub->rects[i]->pict.data[2]);
780         av_freep(&sub->rects[i]->pict.data[3]);
781         av_freep(&sub->rects[i]->text);
782         av_freep(&sub->rects[i]->ass);
783         av_freep(&sub->rects[i]);
784     }
785
786     av_freep(&sub->rects);
787
788     memset(sub, 0, sizeof(AVSubtitle));
789 }
790
791 av_cold int avcodec_close(AVCodecContext *avctx)
792 {
793     /* If there is a user-supplied mutex locking routine, call it. */
794     if (ff_lockmgr_cb) {
795         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
796             return -1;
797     }
798
799     entangled_thread_counter++;
800     if(entangled_thread_counter != 1){
801         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
802         entangled_thread_counter--;
803         return -1;
804     }
805
806     if (HAVE_THREADS && avctx->thread_opaque)
807         ff_thread_free(avctx);
808     if (avctx->codec && avctx->codec->close)
809         avctx->codec->close(avctx);
810     avcodec_default_free_buffers(avctx);
811     avctx->coded_frame = NULL;
812     if (avctx->codec && avctx->codec->priv_class)
813         av_opt_free(avctx->priv_data);
814     av_opt_free(avctx);
815     av_freep(&avctx->priv_data);
816     if(avctx->codec && avctx->codec->encode)
817         av_freep(&avctx->extradata);
818     avctx->codec = NULL;
819     avctx->active_thread_type = 0;
820     entangled_thread_counter--;
821
822     /* Release any user-supplied mutex. */
823     if (ff_lockmgr_cb) {
824         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
825     }
826     return 0;
827 }
828
829 AVCodec *avcodec_find_encoder(enum CodecID id)
830 {
831     AVCodec *p, *experimental=NULL;
832     p = first_avcodec;
833     while (p) {
834         if (p->encode != NULL && p->id == id) {
835             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
836                 experimental = p;
837             } else
838                 return p;
839         }
840         p = p->next;
841     }
842     return experimental;
843 }
844
845 AVCodec *avcodec_find_encoder_by_name(const char *name)
846 {
847     AVCodec *p;
848     if (!name)
849         return NULL;
850     p = first_avcodec;
851     while (p) {
852         if (p->encode != NULL && strcmp(name,p->name) == 0)
853             return p;
854         p = p->next;
855     }
856     return NULL;
857 }
858
859 AVCodec *avcodec_find_decoder(enum CodecID id)
860 {
861     AVCodec *p;
862     p = first_avcodec;
863     while (p) {
864         if (p->decode != NULL && p->id == id)
865             return p;
866         p = p->next;
867     }
868     return NULL;
869 }
870
871 AVCodec *avcodec_find_decoder_by_name(const char *name)
872 {
873     AVCodec *p;
874     if (!name)
875         return NULL;
876     p = first_avcodec;
877     while (p) {
878         if (p->decode != NULL && strcmp(name,p->name) == 0)
879             return p;
880         p = p->next;
881     }
882     return NULL;
883 }
884
885 static int get_bit_rate(AVCodecContext *ctx)
886 {
887     int bit_rate;
888     int bits_per_sample;
889
890     switch(ctx->codec_type) {
891     case AVMEDIA_TYPE_VIDEO:
892     case AVMEDIA_TYPE_DATA:
893     case AVMEDIA_TYPE_SUBTITLE:
894     case AVMEDIA_TYPE_ATTACHMENT:
895         bit_rate = ctx->bit_rate;
896         break;
897     case AVMEDIA_TYPE_AUDIO:
898         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
899         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
900         break;
901     default:
902         bit_rate = 0;
903         break;
904     }
905     return bit_rate;
906 }
907
908 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
909 {
910     int i, len, ret = 0;
911
912     for (i = 0; i < 4; i++) {
913         len = snprintf(buf, buf_size,
914                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
915         buf      += len;
916         buf_size  = buf_size > len ? buf_size - len : 0;
917         ret      += len;
918         codec_tag>>=8;
919     }
920     return ret;
921 }
922
923 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
924 {
925     const char *codec_name;
926     const char *profile = NULL;
927     AVCodec *p;
928     char buf1[32];
929     int bitrate;
930     AVRational display_aspect_ratio;
931
932     if (encode)
933         p = avcodec_find_encoder(enc->codec_id);
934     else
935         p = avcodec_find_decoder(enc->codec_id);
936
937     if (p) {
938         codec_name = p->name;
939         profile = av_get_profile_name(p, enc->profile);
940     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
941         /* fake mpeg2 transport stream codec (currently not
942            registered) */
943         codec_name = "mpeg2ts";
944     } else if (enc->codec_name[0] != '\0') {
945         codec_name = enc->codec_name;
946     } else {
947         /* output avi tags */
948         char tag_buf[32];
949         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
950         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
951         codec_name = buf1;
952     }
953
954     switch(enc->codec_type) {
955     case AVMEDIA_TYPE_VIDEO:
956         snprintf(buf, buf_size,
957                  "Video: %s%s",
958                  codec_name, enc->mb_decision ? " (hq)" : "");
959         if (profile)
960             snprintf(buf + strlen(buf), buf_size - strlen(buf),
961                      " (%s)", profile);
962         if (enc->pix_fmt != PIX_FMT_NONE) {
963             snprintf(buf + strlen(buf), buf_size - strlen(buf),
964                      ", %s",
965                      av_get_pix_fmt_name(enc->pix_fmt));
966         }
967         if (enc->width) {
968             snprintf(buf + strlen(buf), buf_size - strlen(buf),
969                      ", %dx%d",
970                      enc->width, enc->height);
971             if (enc->sample_aspect_ratio.num) {
972                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
973                           enc->width*enc->sample_aspect_ratio.num,
974                           enc->height*enc->sample_aspect_ratio.den,
975                           1024*1024);
976                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
977                          " [PAR %d:%d DAR %d:%d]",
978                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
979                          display_aspect_ratio.num, display_aspect_ratio.den);
980             }
981             if(av_log_get_level() >= AV_LOG_DEBUG){
982                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
983                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
984                      ", %d/%d",
985                      enc->time_base.num/g, enc->time_base.den/g);
986             }
987         }
988         if (encode) {
989             snprintf(buf + strlen(buf), buf_size - strlen(buf),
990                      ", q=%d-%d", enc->qmin, enc->qmax);
991         }
992         break;
993     case AVMEDIA_TYPE_AUDIO:
994         snprintf(buf, buf_size,
995                  "Audio: %s",
996                  codec_name);
997         if (profile)
998             snprintf(buf + strlen(buf), buf_size - strlen(buf),
999                      " (%s)", profile);
1000         if (enc->sample_rate) {
1001             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1002                      ", %d Hz", enc->sample_rate);
1003         }
1004         av_strlcat(buf, ", ", buf_size);
1005         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1006         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1007             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1008                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1009         }
1010         break;
1011     case AVMEDIA_TYPE_DATA:
1012         snprintf(buf, buf_size, "Data: %s", codec_name);
1013         break;
1014     case AVMEDIA_TYPE_SUBTITLE:
1015         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1016         break;
1017     case AVMEDIA_TYPE_ATTACHMENT:
1018         snprintf(buf, buf_size, "Attachment: %s", codec_name);
1019         break;
1020     default:
1021         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1022         return;
1023     }
1024     if (encode) {
1025         if (enc->flags & CODEC_FLAG_PASS1)
1026             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1027                      ", pass 1");
1028         if (enc->flags & CODEC_FLAG_PASS2)
1029             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1030                      ", pass 2");
1031     }
1032     bitrate = get_bit_rate(enc);
1033     if (bitrate != 0) {
1034         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1035                  ", %d kb/s", bitrate / 1000);
1036     }
1037 }
1038
1039 const char *av_get_profile_name(const AVCodec *codec, int profile)
1040 {
1041     const AVProfile *p;
1042     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1043         return NULL;
1044
1045     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1046         if (p->profile == profile)
1047             return p->name;
1048
1049     return NULL;
1050 }
1051
1052 unsigned avcodec_version( void )
1053 {
1054   return LIBAVCODEC_VERSION_INT;
1055 }
1056
1057 const char *avcodec_configuration(void)
1058 {
1059     return LIBAV_CONFIGURATION;
1060 }
1061
1062 const char *avcodec_license(void)
1063 {
1064 #define LICENSE_PREFIX "libavcodec license: "
1065     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1066 }
1067
1068 #if !FF_API_AVCODEC_INIT
1069 static
1070 #endif
1071 void avcodec_init(void)
1072 {
1073     static int initialized = 0;
1074
1075     if (initialized != 0)
1076         return;
1077     initialized = 1;
1078
1079     dsputil_static_init();
1080 }
1081
1082 void avcodec_flush_buffers(AVCodecContext *avctx)
1083 {
1084     if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1085         ff_thread_flush(avctx);
1086     else if(avctx->codec->flush)
1087         avctx->codec->flush(avctx);
1088 }
1089
1090 void avcodec_default_free_buffers(AVCodecContext *s){
1091     int i, j;
1092
1093     if(s->internal_buffer==NULL) return;
1094
1095     if (s->internal_buffer_count)
1096         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1097     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1098         InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1099         for(j=0; j<4; j++){
1100             av_freep(&buf->base[j]);
1101             buf->data[j]= NULL;
1102         }
1103     }
1104     av_freep(&s->internal_buffer);
1105
1106     s->internal_buffer_count=0;
1107 }
1108
1109 #if FF_API_OLD_FF_PICT_TYPES
1110 char av_get_pict_type_char(int pict_type){
1111     return av_get_picture_type_char(pict_type);
1112 }
1113 #endif
1114
1115 int av_get_bits_per_sample(enum CodecID codec_id){
1116     switch(codec_id){
1117     case CODEC_ID_ADPCM_SBPRO_2:
1118         return 2;
1119     case CODEC_ID_ADPCM_SBPRO_3:
1120         return 3;
1121     case CODEC_ID_ADPCM_SBPRO_4:
1122     case CODEC_ID_ADPCM_CT:
1123     case CODEC_ID_ADPCM_IMA_WAV:
1124     case CODEC_ID_ADPCM_MS:
1125     case CODEC_ID_ADPCM_YAMAHA:
1126         return 4;
1127     case CODEC_ID_ADPCM_G722:
1128     case CODEC_ID_PCM_ALAW:
1129     case CODEC_ID_PCM_MULAW:
1130     case CODEC_ID_PCM_S8:
1131     case CODEC_ID_PCM_U8:
1132     case CODEC_ID_PCM_ZORK:
1133         return 8;
1134     case CODEC_ID_PCM_S16BE:
1135     case CODEC_ID_PCM_S16LE:
1136     case CODEC_ID_PCM_S16LE_PLANAR:
1137     case CODEC_ID_PCM_U16BE:
1138     case CODEC_ID_PCM_U16LE:
1139         return 16;
1140     case CODEC_ID_PCM_S24DAUD:
1141     case CODEC_ID_PCM_S24BE:
1142     case CODEC_ID_PCM_S24LE:
1143     case CODEC_ID_PCM_U24BE:
1144     case CODEC_ID_PCM_U24LE:
1145         return 24;
1146     case CODEC_ID_PCM_S32BE:
1147     case CODEC_ID_PCM_S32LE:
1148     case CODEC_ID_PCM_U32BE:
1149     case CODEC_ID_PCM_U32LE:
1150     case CODEC_ID_PCM_F32BE:
1151     case CODEC_ID_PCM_F32LE:
1152         return 32;
1153     case CODEC_ID_PCM_F64BE:
1154     case CODEC_ID_PCM_F64LE:
1155         return 64;
1156     default:
1157         return 0;
1158     }
1159 }
1160
1161 #if FF_API_OLD_SAMPLE_FMT
1162 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1163     return av_get_bytes_per_sample(sample_fmt) << 3;
1164 }
1165 #endif
1166
1167 #if !HAVE_THREADS
1168 int ff_thread_init(AVCodecContext *s){
1169     return -1;
1170 }
1171 #endif
1172
1173 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1174 {
1175     unsigned int n = 0;
1176
1177     while(v >= 0xff) {
1178         *s++ = 0xff;
1179         v -= 0xff;
1180         n++;
1181     }
1182     *s = v;
1183     n++;
1184     return n;
1185 }
1186
1187 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1188     int i;
1189     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1190     return i;
1191 }
1192
1193 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1194 {
1195     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1196             "version to the newest one from Git. If the problem still "
1197             "occurs, it means that your file has a feature which has not "
1198             "been implemented.\n", feature);
1199     if(want_sample)
1200         av_log_ask_for_sample(avc, NULL);
1201 }
1202
1203 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1204 {
1205     va_list argument_list;
1206
1207     va_start(argument_list, msg);
1208
1209     if (msg)
1210         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1211     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1212             "of this file to ftp://upload.libav.org/incoming/ "
1213             "and contact the libav-devel mailing list.\n");
1214
1215     va_end(argument_list);
1216 }
1217
1218 static AVHWAccel *first_hwaccel = NULL;
1219
1220 void av_register_hwaccel(AVHWAccel *hwaccel)
1221 {
1222     AVHWAccel **p = &first_hwaccel;
1223     while (*p)
1224         p = &(*p)->next;
1225     *p = hwaccel;
1226     hwaccel->next = NULL;
1227 }
1228
1229 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1230 {
1231     return hwaccel ? hwaccel->next : first_hwaccel;
1232 }
1233
1234 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1235 {
1236     AVHWAccel *hwaccel=NULL;
1237
1238     while((hwaccel= av_hwaccel_next(hwaccel))){
1239         if (   hwaccel->id      == codec_id
1240             && hwaccel->pix_fmt == pix_fmt)
1241             return hwaccel;
1242     }
1243     return NULL;
1244 }
1245
1246 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1247 {
1248     if (ff_lockmgr_cb) {
1249         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1250             return -1;
1251     }
1252
1253     ff_lockmgr_cb = cb;
1254
1255     if (ff_lockmgr_cb) {
1256         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1257             return -1;
1258     }
1259     return 0;
1260 }
1261
1262 unsigned int ff_toupper4(unsigned int x)
1263 {
1264     return     toupper( x     &0xFF)
1265             + (toupper((x>>8 )&0xFF)<<8 )
1266             + (toupper((x>>16)&0xFF)<<16)
1267             + (toupper((x>>24)&0xFF)<<24);
1268 }
1269
1270 #if !HAVE_PTHREADS
1271
1272 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1273 {
1274     f->owner = avctx;
1275     return avctx->get_buffer(avctx, f);
1276 }
1277
1278 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1279 {
1280     f->owner->release_buffer(f->owner, f);
1281 }
1282
1283 void ff_thread_finish_setup(AVCodecContext *avctx)
1284 {
1285 }
1286
1287 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1288 {
1289 }
1290
1291 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1292 {
1293 }
1294
1295 #endif
1296
1297 #if FF_API_THREAD_INIT
1298 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1299 {
1300     s->thread_count = thread_count;
1301     return ff_thread_init(s);
1302 }
1303 #endif
1304
1305 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1306 {
1307     if (codec_id <= CODEC_ID_NONE)
1308         return AVMEDIA_TYPE_UNKNOWN;
1309     else if (codec_id < CODEC_ID_FIRST_AUDIO)
1310         return AVMEDIA_TYPE_VIDEO;
1311     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1312         return AVMEDIA_TYPE_AUDIO;
1313     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1314         return AVMEDIA_TYPE_SUBTITLE;
1315
1316     return AVMEDIA_TYPE_UNKNOWN;
1317 }