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