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