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