]> 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/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avassert.h"
38 #include "avcodec.h"
39 #include "dsputil.h"
40 #include "libavutil/opt.h"
41 #include "imgconvert.h"
42 #include "thread.h"
43 #include "audioconvert.h"
44 #include "internal.h"
45 #include "bytestream.h"
46 #include <stdlib.h>
47 #include <stdarg.h>
48 #include <limits.h>
49 #include <float.h>
50
51 static int volatile entangled_thread_counter=0;
52 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
53 static void *codec_mutex;
54 static void *avformat_mutex;
55
56 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
57 {
58     if(min_size < *size)
59         return ptr;
60
61     min_size= FFMAX(17*min_size/16 + 32, min_size);
62
63     ptr= av_realloc(ptr, min_size);
64     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
65         min_size= 0;
66
67     *size= min_size;
68
69     return ptr;
70 }
71
72 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
73 {
74     void **p = ptr;
75     if (min_size < *size)
76         return 0;
77     min_size= FFMAX(17*min_size/16 + 32, min_size);
78     av_free(*p);
79     *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
80     if (!*p) min_size = 0;
81     *size= min_size;
82     return 1;
83 }
84
85 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
86 {
87     ff_fast_malloc(ptr, size, min_size, 0);
88 }
89
90 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
91 {
92     uint8_t **p = ptr;
93     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
94         av_freep(p);
95         *size = 0;
96         return;
97     }
98     if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
99         memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
100 }
101
102 /* encoder management */
103 static AVCodec *first_avcodec = NULL;
104
105 AVCodec *av_codec_next(AVCodec *c){
106     if(c) return c->next;
107     else  return first_avcodec;
108 }
109
110 static void avcodec_init(void)
111 {
112     static int initialized = 0;
113
114     if (initialized != 0)
115         return;
116     initialized = 1;
117
118     dsputil_static_init();
119 }
120
121 static av_always_inline int codec_is_encoder(AVCodec *codec)
122 {
123     return codec && (codec->encode || codec->encode2);
124 }
125
126 static av_always_inline int codec_is_decoder(AVCodec *codec)
127 {
128     return codec && codec->decode;
129 }
130
131 void avcodec_register(AVCodec *codec)
132 {
133     AVCodec **p;
134     avcodec_init();
135     p = &first_avcodec;
136     while (*p != NULL) p = &(*p)->next;
137     *p = codec;
138     codec->next = NULL;
139
140     if (codec->init_static_data)
141         codec->init_static_data(codec);
142 }
143
144 unsigned avcodec_get_edge_width(void)
145 {
146     return EDGE_WIDTH;
147 }
148
149 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
150     s->coded_width = width;
151     s->coded_height= height;
152     s->width = -((-width )>>s->lowres);
153     s->height= -((-height)>>s->lowres);
154 }
155
156 #define INTERNAL_BUFFER_SIZE (32+1)
157
158 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
159                                int linesize_align[AV_NUM_DATA_POINTERS])
160 {
161     int i;
162     int w_align= 1;
163     int h_align= 1;
164
165     switch(s->pix_fmt){
166     case PIX_FMT_YUV420P:
167     case PIX_FMT_YUYV422:
168     case PIX_FMT_UYVY422:
169     case PIX_FMT_YUV422P:
170     case PIX_FMT_YUV440P:
171     case PIX_FMT_YUV444P:
172     case PIX_FMT_GBRP:
173     case PIX_FMT_GRAY8:
174     case PIX_FMT_GRAY16BE:
175     case PIX_FMT_GRAY16LE:
176     case PIX_FMT_YUVJ420P:
177     case PIX_FMT_YUVJ422P:
178     case PIX_FMT_YUVJ440P:
179     case PIX_FMT_YUVJ444P:
180     case PIX_FMT_YUVA420P:
181     case PIX_FMT_YUV420P9LE:
182     case PIX_FMT_YUV420P9BE:
183     case PIX_FMT_YUV420P10LE:
184     case PIX_FMT_YUV420P10BE:
185     case PIX_FMT_YUV422P9LE:
186     case PIX_FMT_YUV422P9BE:
187     case PIX_FMT_YUV422P10LE:
188     case PIX_FMT_YUV422P10BE:
189     case PIX_FMT_YUV444P9LE:
190     case PIX_FMT_YUV444P9BE:
191     case PIX_FMT_YUV444P10LE:
192     case PIX_FMT_YUV444P10BE:
193     case PIX_FMT_GBRP9LE:
194     case PIX_FMT_GBRP9BE:
195     case PIX_FMT_GBRP10LE:
196     case PIX_FMT_GBRP10BE:
197         w_align = 16; //FIXME assume 16 pixel per macroblock
198         h_align = 16 * 2; // interlaced needs 2 macroblocks height
199         break;
200     case PIX_FMT_YUV411P:
201     case PIX_FMT_UYYVYY411:
202         w_align=32;
203         h_align=8;
204         break;
205     case PIX_FMT_YUV410P:
206         if(s->codec_id == CODEC_ID_SVQ1){
207             w_align=64;
208             h_align=64;
209         }
210     case PIX_FMT_RGB555:
211         if(s->codec_id == CODEC_ID_RPZA){
212             w_align=4;
213             h_align=4;
214         }
215     case PIX_FMT_PAL8:
216     case PIX_FMT_BGR8:
217     case PIX_FMT_RGB8:
218         if(s->codec_id == CODEC_ID_SMC){
219             w_align=4;
220             h_align=4;
221         }
222         break;
223     case PIX_FMT_BGR24:
224         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
225             w_align=4;
226             h_align=4;
227         }
228         break;
229     default:
230         w_align= 1;
231         h_align= 1;
232         break;
233     }
234
235     if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
236         w_align= FFMAX(w_align, 8);
237     }
238
239     *width = FFALIGN(*width , w_align);
240     *height= FFALIGN(*height, h_align);
241     if(s->codec_id == CODEC_ID_H264 || s->lowres)
242         *height+=2; // some of the optimized chroma MC reads one line too much
243                     // which is also done in mpeg decoders with lowres > 0
244
245     for (i = 0; i < 4; i++)
246         linesize_align[i] = STRIDE_ALIGN;
247 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
248 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
249 //picture size unneccessarily in some cases. The solution here is not
250 //pretty and better ideas are welcome!
251 #if HAVE_MMX
252     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
253        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
254        s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
255         for (i = 0; i < 4; i++)
256             linesize_align[i] = 16;
257     }
258 #endif
259 }
260
261 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
262     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
263     int linesize_align[AV_NUM_DATA_POINTERS];
264     int align;
265     avcodec_align_dimensions2(s, width, height, linesize_align);
266     align = FFMAX(linesize_align[0], linesize_align[3]);
267     linesize_align[1] <<= chroma_shift;
268     linesize_align[2] <<= chroma_shift;
269     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
270     *width=FFALIGN(*width, align);
271 }
272
273 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
274 {
275     if (s->pkt) {
276         pic->pkt_pts = s->pkt->pts;
277         pic->pkt_pos = s->pkt->pos;
278     } else {
279         pic->pkt_pts = AV_NOPTS_VALUE;
280         pic->pkt_pos = -1;
281     }
282     pic->reordered_opaque= s->reordered_opaque;
283     pic->sample_aspect_ratio = s->sample_aspect_ratio;
284     pic->width               = s->width;
285     pic->height              = s->height;
286     pic->format              = s->pix_fmt;
287 }
288
289 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
290                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
291                              int buf_size, int align)
292 {
293     int ch, planar, needed_size, ret = 0;
294
295     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
296                                              frame->nb_samples, sample_fmt,
297                                              align);
298     if (buf_size < needed_size)
299         return AVERROR(EINVAL);
300
301     planar = av_sample_fmt_is_planar(sample_fmt);
302     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
303         if (!(frame->extended_data = av_mallocz(nb_channels *
304                                                 sizeof(*frame->extended_data))))
305             return AVERROR(ENOMEM);
306     } else {
307         frame->extended_data = frame->data;
308     }
309
310     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
311                                       buf, nb_channels, frame->nb_samples,
312                                       sample_fmt, align)) < 0) {
313         if (frame->extended_data != frame->data)
314             av_freep(&frame->extended_data);
315         return ret;
316     }
317     if (frame->extended_data != frame->data) {
318         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
319             frame->data[ch] = frame->extended_data[ch];
320     }
321
322     return ret;
323 }
324
325 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
326 {
327     AVCodecInternal *avci = avctx->internal;
328     InternalBuffer *buf;
329     int buf_size, ret;
330
331     buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
332                                           frame->nb_samples, avctx->sample_fmt,
333                                           32);
334     if (buf_size < 0)
335         return AVERROR(EINVAL);
336
337     /* allocate InternalBuffer if needed */
338     if (!avci->buffer) {
339         avci->buffer = av_mallocz(sizeof(InternalBuffer));
340         if (!avci->buffer)
341             return AVERROR(ENOMEM);
342     }
343     buf = avci->buffer;
344
345     /* if there is a previously-used internal buffer, check its size and
346        channel count to see if we can reuse it */
347     if (buf->extended_data) {
348         /* if current buffer is too small, free it */
349         if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
350             av_free(buf->extended_data[0]);
351             if (buf->extended_data != buf->data)
352                 av_freep(&buf->extended_data);
353             buf->extended_data = NULL;
354             buf->data[0] = NULL;
355         }
356         /* if number of channels has changed, reset and/or free extended data
357            pointers but leave data buffer in buf->data[0] for reuse */
358         if (buf->nb_channels != avctx->channels) {
359             if (buf->extended_data != buf->data)
360                 av_free(buf->extended_data);
361             buf->extended_data = NULL;
362         }
363     }
364
365     /* if there is no previous buffer or the previous buffer cannot be used
366        as-is, allocate a new buffer and/or rearrange the channel pointers */
367     if (!buf->extended_data) {
368         if (!buf->data[0]) {
369             if (!(buf->data[0] = av_mallocz(buf_size)))
370                 return AVERROR(ENOMEM);
371             buf->audio_data_size = buf_size;
372         }
373         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
374                                             avctx->sample_fmt, buf->data[0],
375                                             buf->audio_data_size, 32)))
376             return ret;
377
378         if (frame->extended_data == frame->data)
379             buf->extended_data = buf->data;
380         else
381             buf->extended_data = frame->extended_data;
382         memcpy(buf->data, frame->data, sizeof(frame->data));
383         buf->linesize[0] = frame->linesize[0];
384         buf->nb_channels = avctx->channels;
385     } else {
386         /* copy InternalBuffer info to the AVFrame */
387         frame->extended_data = buf->extended_data;
388         frame->linesize[0]   = buf->linesize[0];
389         memcpy(frame->data, buf->data, sizeof(frame->data));
390     }
391
392     frame->type          = FF_BUFFER_TYPE_INTERNAL;
393
394     if (avctx->pkt) {
395         frame->pkt_pts = avctx->pkt->pts;
396         frame->pkt_pos = avctx->pkt->pos;
397     } else {
398         frame->pkt_pts = AV_NOPTS_VALUE;
399         frame->pkt_pos = -1;
400     }
401
402     frame->reordered_opaque = avctx->reordered_opaque;
403
404     if (avctx->debug & FF_DEBUG_BUFFERS)
405         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
406                "internal audio buffer used\n", frame);
407
408     return 0;
409 }
410
411 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
412 {
413     int i;
414     int w= s->width;
415     int h= s->height;
416     InternalBuffer *buf;
417     AVCodecInternal *avci = s->internal;
418
419     if(pic->data[0]!=NULL) {
420         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
421         return -1;
422     }
423     if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
424         av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
425         return -1;
426     }
427
428     if(av_image_check_size(w, h, 0, s))
429         return -1;
430
431     if (!avci->buffer) {
432         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
433                                   sizeof(InternalBuffer));
434     }
435
436     buf = &avci->buffer[avci->buffer_count];
437
438     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
439         if(s->active_thread_type&FF_THREAD_FRAME) {
440             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
441             return -1;
442         }
443
444         for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
445             av_freep(&buf->base[i]);
446             buf->data[i]= NULL;
447         }
448     }
449
450     if (!buf->base[0]) {
451         int h_chroma_shift, v_chroma_shift;
452         int size[4] = {0};
453         int tmpsize;
454         int unaligned;
455         AVPicture picture;
456         int stride_align[AV_NUM_DATA_POINTERS];
457         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
458
459         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
460
461         avcodec_align_dimensions2(s, &w, &h, stride_align);
462
463         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
464             w+= EDGE_WIDTH*2;
465             h+= EDGE_WIDTH*2;
466         }
467
468         do {
469             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
470             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
471             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
472             // increase alignment of w for next try (rhs gives the lowest bit set in w)
473             w += w & ~(w-1);
474
475             unaligned = 0;
476             for (i=0; i<4; i++){
477                 unaligned |= picture.linesize[i] % stride_align[i];
478             }
479         } while (unaligned);
480
481         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
482         if (tmpsize < 0)
483             return -1;
484
485         for (i=0; i<3 && picture.data[i+1]; i++)
486             size[i] = picture.data[i+1] - picture.data[i];
487         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
488
489         memset(buf->base, 0, sizeof(buf->base));
490         memset(buf->data, 0, sizeof(buf->data));
491
492         for(i=0; i<4 && size[i]; i++){
493             const int h_shift= i==0 ? 0 : h_chroma_shift;
494             const int v_shift= i==0 ? 0 : v_chroma_shift;
495
496             buf->linesize[i]= picture.linesize[i];
497
498             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
499             if(buf->base[i]==NULL) return -1;
500             memset(buf->base[i], 128, size[i]);
501
502             // no edge if EDGE EMU or not planar YUV
503             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
504                 buf->data[i] = buf->base[i];
505             else
506                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
507         }
508         for (; i < AV_NUM_DATA_POINTERS; i++) {
509             buf->base[i] = buf->data[i] = NULL;
510             buf->linesize[i] = 0;
511         }
512         if(size[1] && !size[2])
513             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
514         buf->width  = s->width;
515         buf->height = s->height;
516         buf->pix_fmt= s->pix_fmt;
517     }
518     pic->type= FF_BUFFER_TYPE_INTERNAL;
519
520     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
521         pic->base[i]= buf->base[i];
522         pic->data[i]= buf->data[i];
523         pic->linesize[i]= buf->linesize[i];
524     }
525     pic->extended_data = pic->data;
526     avci->buffer_count++;
527
528     if (s->pkt) {
529         pic->pkt_pts = s->pkt->pts;
530         pic->pkt_pos = s->pkt->pos;
531     } else {
532         pic->pkt_pts = AV_NOPTS_VALUE;
533         pic->pkt_pos = -1;
534     }
535     pic->reordered_opaque= s->reordered_opaque;
536     pic->sample_aspect_ratio = s->sample_aspect_ratio;
537     pic->width               = s->width;
538     pic->height              = s->height;
539     pic->format              = s->pix_fmt;
540
541     if(s->debug&FF_DEBUG_BUFFERS)
542         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
543                "buffers used\n", pic, avci->buffer_count);
544
545     return 0;
546 }
547
548 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
549 {
550     switch (avctx->codec_type) {
551     case AVMEDIA_TYPE_VIDEO:
552         return video_get_buffer(avctx, frame);
553     case AVMEDIA_TYPE_AUDIO:
554         return audio_get_buffer(avctx, frame);
555     default:
556         return -1;
557     }
558 }
559
560 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
561     int i;
562     InternalBuffer *buf, *last;
563     AVCodecInternal *avci = s->internal;
564
565     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
566
567     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
568     assert(avci->buffer_count);
569
570     if (avci->buffer) {
571         buf = NULL; /* avoids warning */
572         for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
573             buf = &avci->buffer[i];
574             if (buf->data[0] == pic->data[0])
575                 break;
576         }
577         assert(i < avci->buffer_count);
578         avci->buffer_count--;
579         last = &avci->buffer[avci->buffer_count];
580
581         if (buf != last)
582             FFSWAP(InternalBuffer, *buf, *last);
583     }
584
585     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
586         pic->data[i]=NULL;
587 //        pic->base[i]=NULL;
588     }
589 //printf("R%X\n", pic->opaque);
590
591     if(s->debug&FF_DEBUG_BUFFERS)
592         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
593                "buffers used\n", pic, avci->buffer_count);
594 }
595
596 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
597     AVFrame temp_pic;
598     int i;
599
600     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
601
602     if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
603         av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
604                pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
605         s->release_buffer(s, pic);
606     }
607
608     ff_init_buffer_info(s, pic);
609
610     /* If no picture return a new buffer */
611     if(pic->data[0] == NULL) {
612         /* We will copy from buffer, so must be readable */
613         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
614         return s->get_buffer(s, pic);
615     }
616
617     /* If internal buffer type return the same buffer */
618     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
619         return 0;
620     }
621
622     /*
623      * Not internal type and reget_buffer not overridden, emulate cr buffer
624      */
625     temp_pic = *pic;
626     for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
627         pic->data[i] = pic->base[i] = NULL;
628     pic->opaque = NULL;
629     /* Allocate new frame */
630     if (s->get_buffer(s, pic))
631         return -1;
632     /* Copy image data from old buffer to new buffer */
633     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
634              s->height);
635     s->release_buffer(s, &temp_pic); // Release old frame
636     return 0;
637 }
638
639 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
640     int i;
641
642     for(i=0; i<count; i++){
643         int r= func(c, (char*)arg + i*size);
644         if(ret) ret[i]= r;
645     }
646     return 0;
647 }
648
649 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
650     int i;
651
652     for(i=0; i<count; i++){
653         int r= func(c, arg, i, 0);
654         if(ret) ret[i]= r;
655     }
656     return 0;
657 }
658
659 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
660     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
661         ++fmt;
662     return fmt[0];
663 }
664
665 void avcodec_get_frame_defaults(AVFrame *pic){
666     memset(pic, 0, sizeof(AVFrame));
667
668     pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
669     pic->pkt_pos = -1;
670     pic->key_frame= 1;
671     pic->sample_aspect_ratio = (AVRational){0, 1};
672     pic->format = -1;           /* unknown */
673 }
674
675 AVFrame *avcodec_alloc_frame(void){
676     AVFrame *pic= av_malloc(sizeof(AVFrame));
677
678     if(pic==NULL) return NULL;
679
680     avcodec_get_frame_defaults(pic);
681
682     return pic;
683 }
684
685 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
686 {
687     memset(sub, 0, sizeof(*sub));
688     sub->pts = AV_NOPTS_VALUE;
689 }
690
691 #if FF_API_AVCODEC_OPEN
692 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
693 {
694     return avcodec_open2(avctx, codec, NULL);
695 }
696 #endif
697
698 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
699 {
700     int ret = 0;
701     AVDictionary *tmp = NULL;
702
703     if (avcodec_is_open(avctx))
704         return 0;
705
706     if ((!codec && !avctx->codec)) {
707         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
708         return AVERROR(EINVAL);
709     }
710     if ((codec && avctx->codec && codec != avctx->codec)) {
711         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
712                "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
713         return AVERROR(EINVAL);
714     }
715     if (!codec)
716         codec = avctx->codec;
717
718     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
719         return AVERROR(EINVAL);
720
721     if (options)
722         av_dict_copy(&tmp, *options, 0);
723
724     /* If there is a user-supplied mutex locking routine, call it. */
725     if (ff_lockmgr_cb) {
726         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
727             return -1;
728     }
729
730     entangled_thread_counter++;
731     if(entangled_thread_counter != 1){
732         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
733         ret = -1;
734         goto end;
735     }
736
737     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
738     if (!avctx->internal) {
739         ret = AVERROR(ENOMEM);
740         goto end;
741     }
742
743     if (codec->priv_data_size > 0) {
744       if(!avctx->priv_data){
745         avctx->priv_data = av_mallocz(codec->priv_data_size);
746         if (!avctx->priv_data) {
747             ret = AVERROR(ENOMEM);
748             goto end;
749         }
750         if (codec->priv_class) {
751             *(AVClass**)avctx->priv_data= codec->priv_class;
752             av_opt_set_defaults(avctx->priv_data);
753         }
754       }
755       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
756           goto free_and_end;
757     } else {
758         avctx->priv_data = NULL;
759     }
760     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
761         goto free_and_end;
762
763     if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
764         if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
765             av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
766             ret = -1;
767             goto free_and_end;
768         }
769
770     //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
771     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
772     if(avctx->coded_width && avctx->coded_height)
773         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
774     else if(avctx->width && avctx->height)
775         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
776     }
777
778     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
779         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
780            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
781         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
782         avcodec_set_dimensions(avctx, 0, 0);
783     }
784
785     /* if the decoder init function was already called previously,
786        free the already allocated subtitle_header before overwriting it */
787     if (codec_is_decoder(codec))
788         av_freep(&avctx->subtitle_header);
789
790 #define SANE_NB_CHANNELS 128U
791     if (avctx->channels > SANE_NB_CHANNELS) {
792         ret = AVERROR(EINVAL);
793         goto free_and_end;
794     }
795
796     avctx->codec = codec;
797     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
798         avctx->codec_id == CODEC_ID_NONE) {
799         avctx->codec_type = codec->type;
800         avctx->codec_id   = codec->id;
801     }
802     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
803                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
804         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
805         ret = AVERROR(EINVAL);
806         goto free_and_end;
807     }
808     avctx->frame_number = 0;
809
810     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
811         (!avctx->time_base.num || !avctx->time_base.den)) {
812         avctx->time_base.num = 1;
813         avctx->time_base.den = avctx->sample_rate;
814     }
815
816     if (!HAVE_THREADS)
817         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
818
819     if (HAVE_THREADS && !avctx->thread_opaque) {
820         ret = ff_thread_init(avctx);
821         if (ret < 0) {
822             goto free_and_end;
823         }
824     }
825     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
826         avctx->thread_count = 1;
827
828     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
829         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
830                avctx->codec->max_lowres);
831         ret = AVERROR(EINVAL);
832         goto free_and_end;
833     }
834     if (codec_is_encoder(avctx->codec)) {
835         int i;
836         if (avctx->codec->sample_fmts) {
837             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
838                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
839                     break;
840             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
841                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
842                 ret = AVERROR(EINVAL);
843                 goto free_and_end;
844             }
845         }
846         if (avctx->codec->supported_samplerates) {
847             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
848                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
849                     break;
850             if (avctx->codec->supported_samplerates[i] == 0) {
851                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
852                 ret = AVERROR(EINVAL);
853                 goto free_and_end;
854             }
855         }
856         if (avctx->codec->channel_layouts) {
857             if (!avctx->channel_layout) {
858                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
859             } else {
860                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
861                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
862                         break;
863                 if (avctx->codec->channel_layouts[i] == 0) {
864                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
865                     ret = AVERROR(EINVAL);
866                     goto free_and_end;
867                 }
868             }
869         }
870         if (avctx->channel_layout && avctx->channels) {
871             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
872                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
873                 ret = AVERROR(EINVAL);
874                 goto free_and_end;
875             }
876         } else if (avctx->channel_layout) {
877             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
878         }
879     }
880
881     avctx->pts_correction_num_faulty_pts =
882     avctx->pts_correction_num_faulty_dts = 0;
883     avctx->pts_correction_last_pts =
884     avctx->pts_correction_last_dts = INT64_MIN;
885
886     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
887         ret = avctx->codec->init(avctx);
888         if (ret < 0) {
889             goto free_and_end;
890         }
891     }
892
893     ret=0;
894 end:
895     entangled_thread_counter--;
896
897     /* Release any user-supplied mutex. */
898     if (ff_lockmgr_cb) {
899         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
900     }
901     if (options) {
902         av_dict_free(options);
903         *options = tmp;
904     }
905
906     return ret;
907 free_and_end:
908     av_dict_free(&tmp);
909     av_freep(&avctx->priv_data);
910     av_freep(&avctx->internal);
911     avctx->codec= NULL;
912     goto end;
913 }
914
915 int ff_alloc_packet(AVPacket *avpkt, int size)
916 {
917     if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
918         return AVERROR(EINVAL);
919
920     if (avpkt->data) {
921         uint8_t *pkt_data;
922
923         if (avpkt->size < size)
924             return AVERROR(EINVAL);
925
926         pkt_data = avpkt->data;
927         av_init_packet(avpkt);
928         avpkt->data = pkt_data;
929         avpkt->size = size;
930         return 0;
931     } else {
932         return av_new_packet(avpkt, size);
933     }
934 }
935
936 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
937                                               AVPacket *avpkt,
938                                               const AVFrame *frame,
939                                               int *got_packet_ptr)
940 {
941     int ret;
942     int user_packet = !!avpkt->data;
943     int nb_samples;
944
945     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
946         av_init_packet(avpkt);
947         avpkt->size = 0;
948         return 0;
949     }
950
951     /* check for valid frame size */
952     if (frame) {
953         nb_samples = frame->nb_samples;
954         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
955             if (nb_samples > avctx->frame_size)
956                 return AVERROR(EINVAL);
957         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
958             if (nb_samples != avctx->frame_size)
959                 return AVERROR(EINVAL);
960         }
961     } else {
962         nb_samples = avctx->frame_size;
963     }
964
965     if (avctx->codec->encode2) {
966         *got_packet_ptr = 0;
967         ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
968         if (!ret && *got_packet_ptr &&
969             !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
970             avpkt->pts = frame->pts;
971             avpkt->duration = av_rescale_q(frame->nb_samples,
972                                            (AVRational){ 1, avctx->sample_rate },
973                                            avctx->time_base);
974         }
975     } else {
976         /* for compatibility with encoders not supporting encode2(), we need to
977            allocate a packet buffer if the user has not provided one or check
978            the size otherwise */
979         int fs_tmp   = 0;
980         int buf_size = avpkt->size;
981         if (!user_packet) {
982             if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
983                 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
984                 if (!frame)
985                     return AVERROR(EINVAL);
986                 buf_size = nb_samples * avctx->channels *
987                            av_get_bits_per_sample(avctx->codec_id) / 8;
988             } else {
989                 /* this is a guess as to the required size.
990                    if an encoder needs more than this, it should probably
991                    implement encode2() */
992                 buf_size = 2 * avctx->frame_size * avctx->channels *
993                            av_get_bytes_per_sample(avctx->sample_fmt);
994                 buf_size += FF_MIN_BUFFER_SIZE;
995             }
996         }
997         if ((ret = ff_alloc_packet(avpkt, buf_size)))
998             return ret;
999
1000         /* Encoders using AVCodec.encode() that support
1001            CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
1002            the smaller size when encoding the last frame.
1003            This code can be removed once all encoders supporting
1004            CODEC_CAP_SMALL_LAST_FRAME use encode2() */
1005         if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
1006             nb_samples < avctx->frame_size) {
1007             fs_tmp = avctx->frame_size;
1008             avctx->frame_size = nb_samples;
1009         }
1010
1011         /* encode the frame */
1012         ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1013                                    frame ? frame->data[0] : NULL);
1014         if (ret >= 0) {
1015             if (!ret) {
1016                 /* no output. if the packet data was allocated by libavcodec,
1017                    free it */
1018                 if (!user_packet)
1019                     av_freep(&avpkt->data);
1020             } else {
1021                 if (avctx->coded_frame)
1022                     avpkt->pts = avctx->coded_frame->pts;
1023                 /* Set duration for final small packet. This can be removed
1024                    once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1025                    encode2() */
1026                 if (fs_tmp) {
1027                     avpkt->duration = av_rescale_q(avctx->frame_size,
1028                                                    (AVRational){ 1, avctx->sample_rate },
1029                                                    avctx->time_base);
1030                 }
1031             }
1032             avpkt->size = ret;
1033             *got_packet_ptr = (ret > 0);
1034             ret = 0;
1035         }
1036
1037         if (fs_tmp)
1038             avctx->frame_size = fs_tmp;
1039     }
1040     if (!ret)
1041         avctx->frame_number++;
1042
1043     /* NOTE: if we add any audio encoders which output non-keyframe packets,
1044              this needs to be moved to the encoders, but for now we can do it
1045              here to simplify things */
1046     avpkt->flags |= AV_PKT_FLAG_KEY;
1047
1048     return ret;
1049 }
1050
1051 #if FF_API_OLD_DECODE_AUDIO
1052 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1053                                              uint8_t *buf, int buf_size,
1054                                              const short *samples)
1055 {
1056     AVPacket pkt;
1057     AVFrame frame0;
1058     AVFrame *frame;
1059     int ret, samples_size, got_packet;
1060
1061     av_init_packet(&pkt);
1062     pkt.data = buf;
1063     pkt.size = buf_size;
1064
1065     if (samples) {
1066         frame = &frame0;
1067         avcodec_get_frame_defaults(frame);
1068
1069         if (avctx->frame_size) {
1070             frame->nb_samples = avctx->frame_size;
1071         } else {
1072             /* if frame_size is not set, the number of samples must be
1073                calculated from the buffer size */
1074             int64_t nb_samples;
1075             if (!av_get_bits_per_sample(avctx->codec_id)) {
1076                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1077                        "support this codec\n");
1078                 return AVERROR(EINVAL);
1079             }
1080             nb_samples = (int64_t)buf_size * 8 /
1081                          (av_get_bits_per_sample(avctx->codec_id) *
1082                          avctx->channels);
1083             if (nb_samples >= INT_MAX)
1084                 return AVERROR(EINVAL);
1085             frame->nb_samples = nb_samples;
1086         }
1087
1088         /* it is assumed that the samples buffer is large enough based on the
1089            relevant parameters */
1090         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1091                                                   frame->nb_samples,
1092                                                   avctx->sample_fmt, 1);
1093         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1094                                             avctx->sample_fmt,
1095                                             samples, samples_size, 1)))
1096             return ret;
1097
1098         /* fabricate frame pts from sample count.
1099            this is needed because the avcodec_encode_audio() API does not have
1100            a way for the user to provide pts */
1101         if(avctx->sample_rate && avctx->time_base.num)
1102             frame->pts = av_rescale_q(avctx->internal->sample_count,
1103                                   (AVRational){ 1, avctx->sample_rate },
1104                                   avctx->time_base);
1105         else
1106             frame->pts = AV_NOPTS_VALUE;
1107         avctx->internal->sample_count += frame->nb_samples;
1108     } else {
1109         frame = NULL;
1110     }
1111
1112     got_packet = 0;
1113     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1114     if (!ret && got_packet && avctx->coded_frame) {
1115         avctx->coded_frame->pts       = pkt.pts;
1116         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1117     }
1118     /* free any side data since we cannot return it */
1119     ff_packet_free_side_data(&pkt);
1120
1121     if (frame && frame->extended_data != frame->data)
1122         av_freep(&frame->extended_data);
1123
1124     return ret ? ret : pkt.size;
1125 }
1126 #endif
1127
1128 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1129                          const AVFrame *pict)
1130 {
1131     if(buf_size < FF_MIN_BUFFER_SIZE){
1132         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1133         return -1;
1134     }
1135     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
1136         return -1;
1137     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
1138         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
1139         avctx->frame_number++;
1140         emms_c(); //needed to avoid an emms_c() call before every return;
1141
1142         return ret;
1143     }else
1144         return 0;
1145 }
1146
1147 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1148                             const AVSubtitle *sub)
1149 {
1150     int ret;
1151     if(sub->start_display_time) {
1152         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1153         return -1;
1154     }
1155
1156     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1157     avctx->frame_number++;
1158     return ret;
1159 }
1160
1161 /**
1162  * Attempt to guess proper monotonic timestamps for decoded video frames
1163  * which might have incorrect times. Input timestamps may wrap around, in
1164  * which case the output will as well.
1165  *
1166  * @param pts the pts field of the decoded AVPacket, as passed through
1167  * AVFrame.pkt_pts
1168  * @param dts the dts field of the decoded AVPacket
1169  * @return one of the input values, may be AV_NOPTS_VALUE
1170  */
1171 static int64_t guess_correct_pts(AVCodecContext *ctx,
1172                                  int64_t reordered_pts, int64_t dts)
1173 {
1174     int64_t pts = AV_NOPTS_VALUE;
1175
1176     if (dts != AV_NOPTS_VALUE) {
1177         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1178         ctx->pts_correction_last_dts = dts;
1179     }
1180     if (reordered_pts != AV_NOPTS_VALUE) {
1181         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1182         ctx->pts_correction_last_pts = reordered_pts;
1183     }
1184     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1185        && reordered_pts != AV_NOPTS_VALUE)
1186         pts = reordered_pts;
1187     else
1188         pts = dts;
1189
1190     return pts;
1191 }
1192
1193 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1194 {
1195     int size = 0;
1196     const uint8_t *data;
1197     uint32_t flags;
1198
1199     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1200         return;
1201
1202     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1203     if (!data || size < 4)
1204         return;
1205     flags = bytestream_get_le32(&data);
1206     size -= 4;
1207     if (size < 4) /* Required for any of the changes */
1208         return;
1209     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1210         avctx->channels = bytestream_get_le32(&data);
1211         size -= 4;
1212     }
1213     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1214         if (size < 8)
1215             return;
1216         avctx->channel_layout = bytestream_get_le64(&data);
1217         size -= 8;
1218     }
1219     if (size < 4)
1220         return;
1221     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1222         avctx->sample_rate = bytestream_get_le32(&data);
1223         size -= 4;
1224     }
1225     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1226         if (size < 8)
1227             return;
1228         avctx->width  = bytestream_get_le32(&data);
1229         avctx->height = bytestream_get_le32(&data);
1230         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1231         size -= 8;
1232     }
1233 }
1234
1235 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1236                          int *got_picture_ptr,
1237                          const AVPacket *avpkt)
1238 {
1239     int ret;
1240     // copy to ensure we do not change avpkt
1241     AVPacket tmp = *avpkt;
1242
1243     *got_picture_ptr= 0;
1244     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1245         return -1;
1246
1247     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1248         int did_split = av_packet_split_side_data(&tmp);
1249         apply_param_change(avctx, &tmp);
1250         avctx->pkt = &tmp;
1251         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1252              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1253                                           &tmp);
1254         else {
1255             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1256                               &tmp);
1257             picture->pkt_dts= avpkt->dts;
1258
1259             if(!avctx->has_b_frames){
1260             picture->pkt_pos= avpkt->pos;
1261             }
1262             //FIXME these should be under if(!avctx->has_b_frames)
1263             if (!picture->sample_aspect_ratio.num)
1264                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1265             if (!picture->width)
1266                 picture->width = avctx->width;
1267             if (!picture->height)
1268                 picture->height = avctx->height;
1269             if (picture->format == PIX_FMT_NONE)
1270                 picture->format = avctx->pix_fmt;
1271         }
1272
1273         emms_c(); //needed to avoid an emms_c() call before every return;
1274
1275         avctx->pkt = NULL;
1276         if (did_split)
1277             ff_packet_free_side_data(&tmp);
1278
1279         if (*got_picture_ptr){
1280             avctx->frame_number++;
1281             picture->best_effort_timestamp = guess_correct_pts(avctx,
1282                                                             picture->pkt_pts,
1283                                                             picture->pkt_dts);
1284         }
1285     }else
1286         ret= 0;
1287
1288     return ret;
1289 }
1290
1291 #if FF_API_OLD_DECODE_AUDIO
1292 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1293                          int *frame_size_ptr,
1294                          AVPacket *avpkt)
1295 {
1296     AVFrame frame;
1297     int ret, got_frame = 0;
1298
1299     if (avctx->get_buffer != avcodec_default_get_buffer) {
1300         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1301                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1302         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1303                "avcodec_decode_audio4()\n");
1304         avctx->get_buffer = avcodec_default_get_buffer;
1305         avctx->release_buffer = avcodec_default_release_buffer;
1306     }
1307
1308     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1309
1310     if (ret >= 0 && got_frame) {
1311         int ch, plane_size;
1312         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1313         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1314                                                    frame.nb_samples,
1315                                                    avctx->sample_fmt, 1);
1316         if (*frame_size_ptr < data_size) {
1317             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1318                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1319             return AVERROR(EINVAL);
1320         }
1321
1322         memcpy(samples, frame.extended_data[0], plane_size);
1323
1324         if (planar && avctx->channels > 1) {
1325             uint8_t *out = ((uint8_t *)samples) + plane_size;
1326             for (ch = 1; ch < avctx->channels; ch++) {
1327                 memcpy(out, frame.extended_data[ch], plane_size);
1328                 out += plane_size;
1329             }
1330         }
1331         *frame_size_ptr = data_size;
1332     } else {
1333         *frame_size_ptr = 0;
1334     }
1335     return ret;
1336 }
1337 #endif
1338
1339 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1340                                               AVFrame *frame,
1341                                               int *got_frame_ptr,
1342                                               AVPacket *avpkt)
1343 {
1344     int ret = 0;
1345
1346     *got_frame_ptr = 0;
1347
1348     if (!avpkt->data && avpkt->size) {
1349         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1350         return AVERROR(EINVAL);
1351     }
1352
1353     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1354         av_packet_split_side_data(avpkt);
1355         apply_param_change(avctx, avpkt);
1356
1357         avctx->pkt = avpkt;
1358         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1359         if (ret >= 0 && *got_frame_ptr) {
1360             avctx->frame_number++;
1361             frame->pkt_dts = avpkt->dts;
1362             if (frame->format == AV_SAMPLE_FMT_NONE)
1363                 frame->format = avctx->sample_fmt;
1364         }
1365     }
1366     return ret;
1367 }
1368
1369 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1370                             int *got_sub_ptr,
1371                             AVPacket *avpkt)
1372 {
1373     int ret;
1374
1375     avctx->pkt = avpkt;
1376     *got_sub_ptr = 0;
1377     avcodec_get_subtitle_defaults(sub);
1378     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1379     if (*got_sub_ptr)
1380         avctx->frame_number++;
1381     return ret;
1382 }
1383
1384 void avsubtitle_free(AVSubtitle *sub)
1385 {
1386     int i;
1387
1388     for (i = 0; i < sub->num_rects; i++)
1389     {
1390         av_freep(&sub->rects[i]->pict.data[0]);
1391         av_freep(&sub->rects[i]->pict.data[1]);
1392         av_freep(&sub->rects[i]->pict.data[2]);
1393         av_freep(&sub->rects[i]->pict.data[3]);
1394         av_freep(&sub->rects[i]->text);
1395         av_freep(&sub->rects[i]->ass);
1396         av_freep(&sub->rects[i]);
1397     }
1398
1399     av_freep(&sub->rects);
1400
1401     memset(sub, 0, sizeof(AVSubtitle));
1402 }
1403
1404 av_cold int avcodec_close(AVCodecContext *avctx)
1405 {
1406     /* If there is a user-supplied mutex locking routine, call it. */
1407     if (ff_lockmgr_cb) {
1408         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1409             return -1;
1410     }
1411
1412     entangled_thread_counter++;
1413     if(entangled_thread_counter != 1){
1414         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1415         entangled_thread_counter--;
1416         return -1;
1417     }
1418
1419     if (avcodec_is_open(avctx)) {
1420         if (HAVE_THREADS && avctx->thread_opaque)
1421             ff_thread_free(avctx);
1422         if (avctx->codec && avctx->codec->close)
1423             avctx->codec->close(avctx);
1424         avcodec_default_free_buffers(avctx);
1425         avctx->coded_frame = NULL;
1426         av_freep(&avctx->internal);
1427     }
1428
1429     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1430         av_opt_free(avctx->priv_data);
1431     av_opt_free(avctx);
1432     av_freep(&avctx->priv_data);
1433     if (codec_is_encoder(avctx->codec))
1434         av_freep(&avctx->extradata);
1435     avctx->codec = NULL;
1436     avctx->active_thread_type = 0;
1437     entangled_thread_counter--;
1438
1439     /* Release any user-supplied mutex. */
1440     if (ff_lockmgr_cb) {
1441         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1442     }
1443     return 0;
1444 }
1445
1446 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1447 {
1448     switch(id){
1449         //This is for future deprecatec codec ids, its empty since
1450         //last major bump but will fill up again over time, please dont remove it
1451 //         case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1452         default                         : return id;
1453     }
1454 }
1455
1456 AVCodec *avcodec_find_encoder(enum CodecID id)
1457 {
1458     AVCodec *p, *experimental=NULL;
1459     p = first_avcodec;
1460     id= remap_deprecated_codec_id(id);
1461     while (p) {
1462         if (codec_is_encoder(p) && p->id == id) {
1463             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1464                 experimental = p;
1465             } else
1466                 return p;
1467         }
1468         p = p->next;
1469     }
1470     return experimental;
1471 }
1472
1473 AVCodec *avcodec_find_encoder_by_name(const char *name)
1474 {
1475     AVCodec *p;
1476     if (!name)
1477         return NULL;
1478     p = first_avcodec;
1479     while (p) {
1480         if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1481             return p;
1482         p = p->next;
1483     }
1484     return NULL;
1485 }
1486
1487 AVCodec *avcodec_find_decoder(enum CodecID id)
1488 {
1489     AVCodec *p, *experimental=NULL;
1490     p = first_avcodec;
1491     id= remap_deprecated_codec_id(id);
1492     while (p) {
1493         if (codec_is_decoder(p) && p->id == id) {
1494             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1495                 experimental = p;
1496             } else
1497                 return p;
1498         }
1499         p = p->next;
1500     }
1501     return experimental;
1502 }
1503
1504 AVCodec *avcodec_find_decoder_by_name(const char *name)
1505 {
1506     AVCodec *p;
1507     if (!name)
1508         return NULL;
1509     p = first_avcodec;
1510     while (p) {
1511         if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1512             return p;
1513         p = p->next;
1514     }
1515     return NULL;
1516 }
1517
1518 static int get_bit_rate(AVCodecContext *ctx)
1519 {
1520     int bit_rate;
1521     int bits_per_sample;
1522
1523     switch(ctx->codec_type) {
1524     case AVMEDIA_TYPE_VIDEO:
1525     case AVMEDIA_TYPE_DATA:
1526     case AVMEDIA_TYPE_SUBTITLE:
1527     case AVMEDIA_TYPE_ATTACHMENT:
1528         bit_rate = ctx->bit_rate;
1529         break;
1530     case AVMEDIA_TYPE_AUDIO:
1531         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1532         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1533         break;
1534     default:
1535         bit_rate = 0;
1536         break;
1537     }
1538     return bit_rate;
1539 }
1540
1541 const char *avcodec_get_name(enum CodecID id)
1542 {
1543     AVCodec *codec;
1544
1545 #if !CONFIG_SMALL
1546     switch (id) {
1547 #include "libavcodec/codec_names.h"
1548     }
1549     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1550 #endif
1551     codec = avcodec_find_decoder(id);
1552     if (codec)
1553         return codec->name;
1554     codec = avcodec_find_encoder(id);
1555     if (codec)
1556         return codec->name;
1557     return "unknown_codec";
1558 }
1559
1560 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1561 {
1562     int i, len, ret = 0;
1563
1564     for (i = 0; i < 4; i++) {
1565         len = snprintf(buf, buf_size,
1566                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1567         buf      += len;
1568         buf_size  = buf_size > len ? buf_size - len : 0;
1569         ret      += len;
1570         codec_tag>>=8;
1571     }
1572     return ret;
1573 }
1574
1575 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1576 {
1577     const char *codec_type;
1578     const char *codec_name;
1579     const char *profile = NULL;
1580     AVCodec *p;
1581     int bitrate;
1582     AVRational display_aspect_ratio;
1583
1584     if (!buf || buf_size <= 0)
1585         return;
1586     codec_type = av_get_media_type_string(enc->codec_type);
1587     codec_name = avcodec_get_name(enc->codec_id);
1588     if (enc->profile != FF_PROFILE_UNKNOWN) {
1589         p = encode ? avcodec_find_encoder(enc->codec_id) :
1590                      avcodec_find_decoder(enc->codec_id);
1591         if (p)
1592             profile = av_get_profile_name(p, enc->profile);
1593     }
1594
1595     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1596              codec_name, enc->mb_decision ? " (hq)" : "");
1597     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1598     if (profile)
1599         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1600     if (enc->codec_tag) {
1601         char tag_buf[32];
1602         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1603         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1604                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1605     }
1606     switch(enc->codec_type) {
1607     case AVMEDIA_TYPE_VIDEO:
1608         if (enc->pix_fmt != PIX_FMT_NONE) {
1609             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1610                      ", %s",
1611                      av_get_pix_fmt_name(enc->pix_fmt));
1612         }
1613         if (enc->width) {
1614             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1615                      ", %dx%d",
1616                      enc->width, enc->height);
1617             if (enc->sample_aspect_ratio.num) {
1618                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1619                           enc->width*enc->sample_aspect_ratio.num,
1620                           enc->height*enc->sample_aspect_ratio.den,
1621                           1024*1024);
1622                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1623                          " [SAR %d:%d DAR %d:%d]",
1624                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1625                          display_aspect_ratio.num, display_aspect_ratio.den);
1626             }
1627             if(av_log_get_level() >= AV_LOG_DEBUG){
1628                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1629                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1630                      ", %d/%d",
1631                      enc->time_base.num/g, enc->time_base.den/g);
1632             }
1633         }
1634         if (encode) {
1635             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1636                      ", q=%d-%d", enc->qmin, enc->qmax);
1637         }
1638         break;
1639     case AVMEDIA_TYPE_AUDIO:
1640         if (enc->sample_rate) {
1641             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1642                      ", %d Hz", enc->sample_rate);
1643         }
1644         av_strlcat(buf, ", ", buf_size);
1645         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1646         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1647             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1648                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1649         }
1650         break;
1651     default:
1652         return;
1653     }
1654     if (encode) {
1655         if (enc->flags & CODEC_FLAG_PASS1)
1656             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1657                      ", pass 1");
1658         if (enc->flags & CODEC_FLAG_PASS2)
1659             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1660                      ", pass 2");
1661     }
1662     bitrate = get_bit_rate(enc);
1663     if (bitrate != 0) {
1664         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1665                  ", %d kb/s", bitrate / 1000);
1666     }
1667 }
1668
1669 const char *av_get_profile_name(const AVCodec *codec, int profile)
1670 {
1671     const AVProfile *p;
1672     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1673         return NULL;
1674
1675     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1676         if (p->profile == profile)
1677             return p->name;
1678
1679     return NULL;
1680 }
1681
1682 unsigned avcodec_version( void )
1683 {
1684 //    av_assert0(CODEC_ID_V410==164);
1685     av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1686     av_assert0(CODEC_ID_ADPCM_G722==69660);
1687 //     av_assert0(CODEC_ID_BMV_AUDIO==86071);
1688     av_assert0(CODEC_ID_SRT==94216);
1689     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1690
1691   return LIBAVCODEC_VERSION_INT;
1692 }
1693
1694 const char *avcodec_configuration(void)
1695 {
1696     return FFMPEG_CONFIGURATION;
1697 }
1698
1699 const char *avcodec_license(void)
1700 {
1701 #define LICENSE_PREFIX "libavcodec license: "
1702     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1703 }
1704
1705 void avcodec_flush_buffers(AVCodecContext *avctx)
1706 {
1707     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1708         ff_thread_flush(avctx);
1709     else if(avctx->codec->flush)
1710         avctx->codec->flush(avctx);
1711 }
1712
1713 static void video_free_buffers(AVCodecContext *s)
1714 {
1715     AVCodecInternal *avci = s->internal;
1716     int i, j;
1717
1718     if (!avci->buffer)
1719         return;
1720
1721     if (avci->buffer_count)
1722         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1723                avci->buffer_count);
1724     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1725         InternalBuffer *buf = &avci->buffer[i];
1726         for(j=0; j<4; j++){
1727             av_freep(&buf->base[j]);
1728             buf->data[j]= NULL;
1729         }
1730     }
1731     av_freep(&avci->buffer);
1732
1733     avci->buffer_count=0;
1734 }
1735
1736 static void audio_free_buffers(AVCodecContext *avctx)
1737 {
1738     AVCodecInternal *avci = avctx->internal;
1739     InternalBuffer *buf;
1740
1741     if (!avci->buffer)
1742         return;
1743     buf = avci->buffer;
1744
1745     if (buf->extended_data) {
1746         av_free(buf->extended_data[0]);
1747         if (buf->extended_data != buf->data)
1748             av_freep(&buf->extended_data);
1749     }
1750     av_freep(&avci->buffer);
1751 }
1752
1753 void avcodec_default_free_buffers(AVCodecContext *avctx)
1754 {
1755     switch (avctx->codec_type) {
1756     case AVMEDIA_TYPE_VIDEO:
1757         video_free_buffers(avctx);
1758         break;
1759     case AVMEDIA_TYPE_AUDIO:
1760         audio_free_buffers(avctx);
1761         break;
1762     default:
1763         break;
1764     }
1765 }
1766
1767 int av_get_bits_per_sample(enum CodecID codec_id){
1768     switch(codec_id){
1769     case CODEC_ID_ADPCM_SBPRO_2:
1770         return 2;
1771     case CODEC_ID_ADPCM_SBPRO_3:
1772         return 3;
1773     case CODEC_ID_ADPCM_SBPRO_4:
1774     case CODEC_ID_ADPCM_CT:
1775     case CODEC_ID_ADPCM_IMA_APC:
1776     case CODEC_ID_ADPCM_IMA_WAV:
1777     case CODEC_ID_ADPCM_IMA_QT:
1778     case CODEC_ID_ADPCM_SWF:
1779     case CODEC_ID_ADPCM_MS:
1780     case CODEC_ID_ADPCM_YAMAHA:
1781     case CODEC_ID_ADPCM_G722:
1782         return 4;
1783     case CODEC_ID_PCM_ALAW:
1784     case CODEC_ID_PCM_MULAW:
1785     case CODEC_ID_PCM_S8:
1786     case CODEC_ID_PCM_U8:
1787     case CODEC_ID_PCM_ZORK:
1788         return 8;
1789     case CODEC_ID_PCM_S16BE:
1790     case CODEC_ID_PCM_S16LE:
1791     case CODEC_ID_PCM_S16LE_PLANAR:
1792     case CODEC_ID_PCM_U16BE:
1793     case CODEC_ID_PCM_U16LE:
1794         return 16;
1795     case CODEC_ID_PCM_S24DAUD:
1796     case CODEC_ID_PCM_S24BE:
1797     case CODEC_ID_PCM_S24LE:
1798     case CODEC_ID_PCM_U24BE:
1799     case CODEC_ID_PCM_U24LE:
1800         return 24;
1801     case CODEC_ID_PCM_S32BE:
1802     case CODEC_ID_PCM_S32LE:
1803     case CODEC_ID_PCM_U32BE:
1804     case CODEC_ID_PCM_U32LE:
1805     case CODEC_ID_PCM_F32BE:
1806     case CODEC_ID_PCM_F32LE:
1807         return 32;
1808     case CODEC_ID_PCM_F64BE:
1809     case CODEC_ID_PCM_F64LE:
1810         return 64;
1811     default:
1812         return 0;
1813     }
1814 }
1815
1816 #if !HAVE_THREADS
1817 int ff_thread_init(AVCodecContext *s){
1818     return -1;
1819 }
1820 #endif
1821
1822 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1823 {
1824     unsigned int n = 0;
1825
1826     while(v >= 0xff) {
1827         *s++ = 0xff;
1828         v -= 0xff;
1829         n++;
1830     }
1831     *s = v;
1832     n++;
1833     return n;
1834 }
1835
1836 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1837     int i;
1838     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1839     return i;
1840 }
1841
1842 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1843 {
1844     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1845             "version to the newest one from Git. If the problem still "
1846             "occurs, it means that your file has a feature which has not "
1847             "been implemented.\n", feature);
1848     if(want_sample)
1849         av_log_ask_for_sample(avc, NULL);
1850 }
1851
1852 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1853 {
1854     va_list argument_list;
1855
1856     va_start(argument_list, msg);
1857
1858     if (msg)
1859         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1860     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1861             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1862             "and contact the ffmpeg-devel mailing list.\n");
1863
1864     va_end(argument_list);
1865 }
1866
1867 static AVHWAccel *first_hwaccel = NULL;
1868
1869 void av_register_hwaccel(AVHWAccel *hwaccel)
1870 {
1871     AVHWAccel **p = &first_hwaccel;
1872     while (*p)
1873         p = &(*p)->next;
1874     *p = hwaccel;
1875     hwaccel->next = NULL;
1876 }
1877
1878 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1879 {
1880     return hwaccel ? hwaccel->next : first_hwaccel;
1881 }
1882
1883 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1884 {
1885     AVHWAccel *hwaccel=NULL;
1886
1887     while((hwaccel= av_hwaccel_next(hwaccel))){
1888         if (   hwaccel->id      == codec_id
1889             && hwaccel->pix_fmt == pix_fmt)
1890             return hwaccel;
1891     }
1892     return NULL;
1893 }
1894
1895 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1896 {
1897     if (ff_lockmgr_cb) {
1898         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1899             return -1;
1900         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1901             return -1;
1902     }
1903
1904     ff_lockmgr_cb = cb;
1905
1906     if (ff_lockmgr_cb) {
1907         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1908             return -1;
1909         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1910             return -1;
1911     }
1912     return 0;
1913 }
1914
1915 int avpriv_lock_avformat(void)
1916 {
1917     if (ff_lockmgr_cb) {
1918         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1919             return -1;
1920     }
1921     return 0;
1922 }
1923
1924 int avpriv_unlock_avformat(void)
1925 {
1926     if (ff_lockmgr_cb) {
1927         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1928             return -1;
1929     }
1930     return 0;
1931 }
1932
1933 unsigned int avpriv_toupper4(unsigned int x)
1934 {
1935     return     toupper( x     &0xFF)
1936             + (toupper((x>>8 )&0xFF)<<8 )
1937             + (toupper((x>>16)&0xFF)<<16)
1938             + (toupper((x>>24)&0xFF)<<24);
1939 }
1940
1941 #if !HAVE_THREADS
1942
1943 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1944 {
1945     f->owner = avctx;
1946
1947     ff_init_buffer_info(avctx, f);
1948
1949     return avctx->get_buffer(avctx, f);
1950 }
1951
1952 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1953 {
1954     f->owner->release_buffer(f->owner, f);
1955 }
1956
1957 void ff_thread_finish_setup(AVCodecContext *avctx)
1958 {
1959 }
1960
1961 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1962 {
1963 }
1964
1965 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1966 {
1967 }
1968
1969 #endif
1970
1971 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1972 {
1973     AVCodec *c= avcodec_find_decoder(codec_id);
1974     if(!c)
1975         c= avcodec_find_encoder(codec_id);
1976     if(c)
1977         return c->type;
1978
1979     if (codec_id <= CODEC_ID_NONE)
1980         return AVMEDIA_TYPE_UNKNOWN;
1981     else if (codec_id < CODEC_ID_FIRST_AUDIO)
1982         return AVMEDIA_TYPE_VIDEO;
1983     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1984         return AVMEDIA_TYPE_AUDIO;
1985     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1986         return AVMEDIA_TYPE_SUBTITLE;
1987
1988     return AVMEDIA_TYPE_UNKNOWN;
1989 }
1990
1991 int avcodec_is_open(AVCodecContext *s)
1992 {
1993     return !!s->internal;
1994 }