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