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