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