]> 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         *p = NULL;
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 < AV_NUM_DATA_POINTERS; 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 < AV_NUM_DATA_POINTERS; 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 (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
704         return AVERROR(EINVAL);
705
706     if (options)
707         av_dict_copy(&tmp, *options, 0);
708
709     /* If there is a user-supplied mutex locking routine, call it. */
710     if (ff_lockmgr_cb) {
711         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
712             return -1;
713     }
714
715     entangled_thread_counter++;
716     if(entangled_thread_counter != 1){
717         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
718         ret = -1;
719         goto end;
720     }
721
722     if(avctx->codec || !codec) {
723         ret = AVERROR(EINVAL);
724         goto end;
725     }
726
727     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
728     if (!avctx->internal) {
729         ret = AVERROR(ENOMEM);
730         goto end;
731     }
732
733     if (codec->priv_data_size > 0) {
734       if(!avctx->priv_data){
735         avctx->priv_data = av_mallocz(codec->priv_data_size);
736         if (!avctx->priv_data) {
737             ret = AVERROR(ENOMEM);
738             goto end;
739         }
740         if (codec->priv_class) {
741             *(AVClass**)avctx->priv_data= codec->priv_class;
742             av_opt_set_defaults(avctx->priv_data);
743         }
744       }
745       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
746           goto free_and_end;
747     } else {
748         avctx->priv_data = NULL;
749     }
750     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
751         goto free_and_end;
752
753     if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
754         if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
755             av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, see -strict -2\n");
756             ret = -1;
757             goto free_and_end;
758         }
759
760     //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
761     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
762     if(avctx->coded_width && avctx->coded_height)
763         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
764     else if(avctx->width && avctx->height)
765         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
766     }
767
768     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
769         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
770            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
771         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
772         avcodec_set_dimensions(avctx, 0, 0);
773     }
774
775     /* if the decoder init function was already called previously,
776        free the already allocated subtitle_header before overwriting it */
777     if (codec_is_decoder(codec))
778         av_freep(&avctx->subtitle_header);
779
780 #define SANE_NB_CHANNELS 128U
781     if (avctx->channels > SANE_NB_CHANNELS) {
782         ret = AVERROR(EINVAL);
783         goto free_and_end;
784     }
785
786     avctx->codec = codec;
787     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
788         avctx->codec_id == CODEC_ID_NONE) {
789         avctx->codec_type = codec->type;
790         avctx->codec_id   = codec->id;
791     }
792     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
793                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
794         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
795         ret = AVERROR(EINVAL);
796         goto free_and_end;
797     }
798     avctx->frame_number = 0;
799
800     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
801         (!avctx->time_base.num || !avctx->time_base.den)) {
802         avctx->time_base.num = 1;
803         avctx->time_base.den = avctx->sample_rate;
804     }
805
806     if (!HAVE_THREADS)
807         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
808
809     if (HAVE_THREADS && !avctx->thread_opaque) {
810         ret = ff_thread_init(avctx);
811         if (ret < 0) {
812             goto free_and_end;
813         }
814     }
815     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
816         avctx->thread_count = 1;
817
818     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
819         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
820                avctx->codec->max_lowres);
821         ret = AVERROR(EINVAL);
822         goto free_and_end;
823     }
824     if (codec_is_encoder(avctx->codec)) {
825         int i;
826         if (avctx->codec->sample_fmts) {
827             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
828                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
829                     break;
830             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
831                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
832                 ret = AVERROR(EINVAL);
833                 goto free_and_end;
834             }
835         }
836         if (avctx->codec->supported_samplerates) {
837             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
838                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
839                     break;
840             if (avctx->codec->supported_samplerates[i] == 0) {
841                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
842                 ret = AVERROR(EINVAL);
843                 goto free_and_end;
844             }
845         }
846         if (avctx->codec->channel_layouts) {
847             if (!avctx->channel_layout) {
848                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
849             } else {
850                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
851                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
852                         break;
853                 if (avctx->codec->channel_layouts[i] == 0) {
854                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
855                     ret = AVERROR(EINVAL);
856                     goto free_and_end;
857                 }
858             }
859         }
860         if (avctx->channel_layout && avctx->channels) {
861             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
862                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
863                 ret = AVERROR(EINVAL);
864                 goto free_and_end;
865             }
866         } else if (avctx->channel_layout) {
867             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
868         }
869     }
870
871     avctx->pts_correction_num_faulty_pts =
872     avctx->pts_correction_num_faulty_dts = 0;
873     avctx->pts_correction_last_pts =
874     avctx->pts_correction_last_dts = INT64_MIN;
875
876     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
877         ret = avctx->codec->init(avctx);
878         if (ret < 0) {
879             goto free_and_end;
880         }
881     }
882
883     ret=0;
884 end:
885     entangled_thread_counter--;
886
887     /* Release any user-supplied mutex. */
888     if (ff_lockmgr_cb) {
889         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
890     }
891     if (options) {
892         av_dict_free(options);
893         *options = tmp;
894     }
895
896     return ret;
897 free_and_end:
898     av_dict_free(&tmp);
899     av_freep(&avctx->priv_data);
900     av_freep(&avctx->internal);
901     avctx->codec= NULL;
902     goto end;
903 }
904
905 int ff_alloc_packet(AVPacket *avpkt, int size)
906 {
907     if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
908         return AVERROR(EINVAL);
909
910     if (avpkt->data) {
911         uint8_t *pkt_data;
912         int pkt_size;
913
914         if (avpkt->size < size)
915             return AVERROR(EINVAL);
916
917         pkt_data = avpkt->data;
918         pkt_size = avpkt->size;
919         av_init_packet(avpkt);
920         avpkt->data = pkt_data;
921         avpkt->size = pkt_size;
922         return 0;
923     } else {
924         return av_new_packet(avpkt, size);
925     }
926 }
927
928 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
929                                               AVPacket *avpkt,
930                                               const AVFrame *frame,
931                                               int *got_packet_ptr)
932 {
933     int ret;
934     int user_packet = !!avpkt->data;
935     int nb_samples;
936
937     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
938         av_init_packet(avpkt);
939         avpkt->size = 0;
940         return 0;
941     }
942
943     /* check for valid frame size */
944     if (frame) {
945         nb_samples = frame->nb_samples;
946         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
947             if (nb_samples > avctx->frame_size)
948                 return AVERROR(EINVAL);
949         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
950             if (nb_samples != avctx->frame_size)
951                 return AVERROR(EINVAL);
952         }
953     } else {
954         nb_samples = avctx->frame_size;
955     }
956
957     if (avctx->codec->encode2) {
958         *got_packet_ptr = 0;
959         ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
960         if (!ret && *got_packet_ptr &&
961             !(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
962             avpkt->pts = frame->pts;
963             avpkt->duration = av_rescale_q(frame->nb_samples,
964                                            (AVRational){ 1, avctx->sample_rate },
965                                            avctx->time_base);
966         }
967     } else {
968         /* for compatibility with encoders not supporting encode2(), we need to
969            allocate a packet buffer if the user has not provided one or check
970            the size otherwise */
971         int fs_tmp   = 0;
972         int buf_size = avpkt->size;
973         if (!user_packet) {
974             if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
975                 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
976                 if (!frame)
977                     return AVERROR(EINVAL);
978                 buf_size = nb_samples * avctx->channels *
979                            av_get_bits_per_sample(avctx->codec_id) / 8;
980             } else {
981                 /* this is a guess as to the required size.
982                    if an encoder needs more than this, it should probably
983                    implement encode2() */
984                 buf_size = 2 * avctx->frame_size * avctx->channels *
985                            av_get_bytes_per_sample(avctx->sample_fmt);
986                 buf_size += FF_MIN_BUFFER_SIZE;
987             }
988         }
989         if ((ret = ff_alloc_packet(avpkt, buf_size)))
990             return ret;
991
992         /* Encoders using AVCodec.encode() that support
993            CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
994            the smaller size when encoding the last frame.
995            This code can be removed once all encoders supporting
996            CODEC_CAP_SMALL_LAST_FRAME use encode2() */
997         if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
998             nb_samples < avctx->frame_size) {
999             fs_tmp = avctx->frame_size;
1000             avctx->frame_size = nb_samples;
1001         }
1002
1003         /* encode the frame */
1004         ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
1005                                    frame ? frame->data[0] : NULL);
1006         if (ret >= 0) {
1007             if (!ret) {
1008                 /* no output. if the packet data was allocated by libavcodec,
1009                    free it */
1010                 if (!user_packet)
1011                     av_freep(&avpkt->data);
1012             } else {
1013                 if (avctx->coded_frame)
1014                     avpkt->pts = avctx->coded_frame->pts;
1015                 /* Set duration for final small packet. This can be removed
1016                    once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1017                    encode2() */
1018                 if (fs_tmp) {
1019                     avpkt->duration = av_rescale_q(avctx->frame_size,
1020                                                    (AVRational){ 1, avctx->sample_rate },
1021                                                    avctx->time_base);
1022                 }
1023             }
1024             avpkt->size = ret;
1025             *got_packet_ptr = (ret > 0);
1026             ret = 0;
1027         }
1028
1029         if (fs_tmp)
1030             avctx->frame_size = fs_tmp;
1031     }
1032     if (!ret)
1033         avctx->frame_number++;
1034
1035     /* NOTE: if we add any audio encoders which output non-keyframe packets,
1036              this needs to be moved to the encoders, but for now we can do it
1037              here to simplify things */
1038     avpkt->flags |= AV_PKT_FLAG_KEY;
1039
1040     return ret;
1041 }
1042
1043 #if FF_API_OLD_DECODE_AUDIO
1044 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1045                                              uint8_t *buf, int buf_size,
1046                                              const short *samples)
1047 {
1048     AVPacket pkt;
1049     AVFrame frame0;
1050     AVFrame *frame;
1051     int ret, samples_size, got_packet;
1052
1053     av_init_packet(&pkt);
1054     pkt.data = buf;
1055     pkt.size = buf_size;
1056
1057     if (samples) {
1058         frame = &frame0;
1059         avcodec_get_frame_defaults(frame);
1060
1061         if (avctx->frame_size) {
1062             frame->nb_samples = avctx->frame_size;
1063         } else {
1064             /* if frame_size is not set, the number of samples must be
1065                calculated from the buffer size */
1066             int64_t nb_samples;
1067             if (!av_get_bits_per_sample(avctx->codec_id)) {
1068                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1069                        "support this codec\n");
1070                 return AVERROR(EINVAL);
1071             }
1072             nb_samples = (int64_t)buf_size * 8 /
1073                          (av_get_bits_per_sample(avctx->codec_id) *
1074                          avctx->channels);
1075             if (nb_samples >= INT_MAX)
1076                 return AVERROR(EINVAL);
1077             frame->nb_samples = nb_samples;
1078         }
1079
1080         /* it is assumed that the samples buffer is large enough based on the
1081            relevant parameters */
1082         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1083                                                   frame->nb_samples,
1084                                                   avctx->sample_fmt, 1);
1085         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1086                                             avctx->sample_fmt,
1087                                             samples, samples_size, 1)))
1088             return ret;
1089
1090         /* fabricate frame pts from sample count.
1091            this is needed because the avcodec_encode_audio() API does not have
1092            a way for the user to provide pts */
1093         if(avctx->sample_rate && avctx->time_base.num)
1094             frame->pts = av_rescale_q(avctx->internal->sample_count,
1095                                   (AVRational){ 1, avctx->sample_rate },
1096                                   avctx->time_base);
1097         else
1098             frame->pts = AV_NOPTS_VALUE;
1099         avctx->internal->sample_count += frame->nb_samples;
1100     } else {
1101         frame = NULL;
1102     }
1103
1104     got_packet = 0;
1105     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1106     if (!ret && got_packet && avctx->coded_frame) {
1107         avctx->coded_frame->pts       = pkt.pts;
1108         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1109     }
1110     /* free any side data since we cannot return it */
1111     ff_packet_free_side_data(&pkt);
1112
1113     if (frame && frame->extended_data != frame->data)
1114         av_freep(&frame->extended_data);
1115
1116     return ret ? ret : pkt.size;
1117 }
1118 #endif
1119
1120 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1121                          const AVFrame *pict)
1122 {
1123     if(buf_size < FF_MIN_BUFFER_SIZE){
1124         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1125         return -1;
1126     }
1127     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
1128         return -1;
1129     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
1130         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
1131         avctx->frame_number++;
1132         emms_c(); //needed to avoid an emms_c() call before every return;
1133
1134         return ret;
1135     }else
1136         return 0;
1137 }
1138
1139 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1140                             const AVSubtitle *sub)
1141 {
1142     int ret;
1143     if(sub->start_display_time) {
1144         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1145         return -1;
1146     }
1147
1148     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1149     avctx->frame_number++;
1150     return ret;
1151 }
1152
1153 /**
1154  * Attempt to guess proper monotonic timestamps for decoded video frames
1155  * which might have incorrect times. Input timestamps may wrap around, in
1156  * which case the output will as well.
1157  *
1158  * @param pts the pts field of the decoded AVPacket, as passed through
1159  * AVFrame.pkt_pts
1160  * @param dts the dts field of the decoded AVPacket
1161  * @return one of the input values, may be AV_NOPTS_VALUE
1162  */
1163 static int64_t guess_correct_pts(AVCodecContext *ctx,
1164                                  int64_t reordered_pts, int64_t dts)
1165 {
1166     int64_t pts = AV_NOPTS_VALUE;
1167
1168     if (dts != AV_NOPTS_VALUE) {
1169         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1170         ctx->pts_correction_last_dts = dts;
1171     }
1172     if (reordered_pts != AV_NOPTS_VALUE) {
1173         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1174         ctx->pts_correction_last_pts = reordered_pts;
1175     }
1176     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1177        && reordered_pts != AV_NOPTS_VALUE)
1178         pts = reordered_pts;
1179     else
1180         pts = dts;
1181
1182     return pts;
1183 }
1184
1185 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1186 {
1187     int size = 0;
1188     const uint8_t *data;
1189     uint32_t flags;
1190
1191     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1192         return;
1193
1194     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1195     if (!data || size < 4)
1196         return;
1197     flags = bytestream_get_le32(&data);
1198     size -= 4;
1199     if (size < 4) /* Required for any of the changes */
1200         return;
1201     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1202         avctx->channels = bytestream_get_le32(&data);
1203         size -= 4;
1204     }
1205     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1206         if (size < 8)
1207             return;
1208         avctx->channel_layout = bytestream_get_le64(&data);
1209         size -= 8;
1210     }
1211     if (size < 4)
1212         return;
1213     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1214         avctx->sample_rate = bytestream_get_le32(&data);
1215         size -= 4;
1216     }
1217     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1218         if (size < 8)
1219             return;
1220         avctx->width  = bytestream_get_le32(&data);
1221         avctx->height = bytestream_get_le32(&data);
1222         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1223         size -= 8;
1224     }
1225 }
1226
1227 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1228                          int *got_picture_ptr,
1229                          const AVPacket *avpkt)
1230 {
1231     int ret;
1232     // copy to ensure we do not change avpkt
1233     AVPacket tmp = *avpkt;
1234
1235     *got_picture_ptr= 0;
1236     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1237         return -1;
1238
1239     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1240         int did_split = av_packet_split_side_data(&tmp);
1241         apply_param_change(avctx, &tmp);
1242         avctx->pkt = &tmp;
1243         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1244              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1245                                           &tmp);
1246         else {
1247             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1248                               &tmp);
1249             picture->pkt_dts= avpkt->dts;
1250
1251             if(!avctx->has_b_frames){
1252             picture->pkt_pos= avpkt->pos;
1253             }
1254             //FIXME these should be under if(!avctx->has_b_frames)
1255             if (!picture->sample_aspect_ratio.num)
1256                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1257             if (!picture->width)
1258                 picture->width = avctx->width;
1259             if (!picture->height)
1260                 picture->height = avctx->height;
1261             if (picture->format == PIX_FMT_NONE)
1262                 picture->format = avctx->pix_fmt;
1263         }
1264
1265         emms_c(); //needed to avoid an emms_c() call before every return;
1266
1267         avctx->pkt = NULL;
1268         if (did_split)
1269             ff_packet_free_side_data(&tmp);
1270
1271         if (*got_picture_ptr){
1272             avctx->frame_number++;
1273             picture->best_effort_timestamp = guess_correct_pts(avctx,
1274                                                             picture->pkt_pts,
1275                                                             picture->pkt_dts);
1276         }
1277     }else
1278         ret= 0;
1279
1280     return ret;
1281 }
1282
1283 #if FF_API_OLD_DECODE_AUDIO
1284 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1285                          int *frame_size_ptr,
1286                          AVPacket *avpkt)
1287 {
1288     AVFrame frame;
1289     int ret, got_frame = 0;
1290
1291     if (avctx->get_buffer != avcodec_default_get_buffer) {
1292         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1293                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1294         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1295                "avcodec_decode_audio4()\n");
1296         avctx->get_buffer = avcodec_default_get_buffer;
1297         avctx->release_buffer = avcodec_default_release_buffer;
1298     }
1299
1300     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1301
1302     if (ret >= 0 && got_frame) {
1303         int ch, plane_size;
1304         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1305         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1306                                                    frame.nb_samples,
1307                                                    avctx->sample_fmt, 1);
1308         if (*frame_size_ptr < data_size) {
1309             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1310                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1311             return AVERROR(EINVAL);
1312         }
1313
1314         memcpy(samples, frame.extended_data[0], plane_size);
1315
1316         if (planar && avctx->channels > 1) {
1317             uint8_t *out = ((uint8_t *)samples) + plane_size;
1318             for (ch = 1; ch < avctx->channels; ch++) {
1319                 memcpy(out, frame.extended_data[ch], plane_size);
1320                 out += plane_size;
1321             }
1322         }
1323         *frame_size_ptr = data_size;
1324     } else {
1325         *frame_size_ptr = 0;
1326     }
1327     return ret;
1328 }
1329 #endif
1330
1331 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1332                                               AVFrame *frame,
1333                                               int *got_frame_ptr,
1334                                               AVPacket *avpkt)
1335 {
1336     int ret = 0;
1337
1338     *got_frame_ptr = 0;
1339
1340     if (!avpkt->data && avpkt->size) {
1341         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1342         return AVERROR(EINVAL);
1343     }
1344
1345     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1346         av_packet_split_side_data(avpkt);
1347         apply_param_change(avctx, avpkt);
1348
1349         avctx->pkt = avpkt;
1350         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1351         if (ret >= 0 && *got_frame_ptr) {
1352             avctx->frame_number++;
1353             frame->pkt_dts = avpkt->dts;
1354             if (frame->format == AV_SAMPLE_FMT_NONE)
1355                 frame->format = avctx->sample_fmt;
1356         }
1357     }
1358     return ret;
1359 }
1360
1361 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1362                             int *got_sub_ptr,
1363                             AVPacket *avpkt)
1364 {
1365     int ret;
1366
1367     avctx->pkt = avpkt;
1368     *got_sub_ptr = 0;
1369     avcodec_get_subtitle_defaults(sub);
1370     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1371     if (*got_sub_ptr)
1372         avctx->frame_number++;
1373     return ret;
1374 }
1375
1376 void avsubtitle_free(AVSubtitle *sub)
1377 {
1378     int i;
1379
1380     for (i = 0; i < sub->num_rects; i++)
1381     {
1382         av_freep(&sub->rects[i]->pict.data[0]);
1383         av_freep(&sub->rects[i]->pict.data[1]);
1384         av_freep(&sub->rects[i]->pict.data[2]);
1385         av_freep(&sub->rects[i]->pict.data[3]);
1386         av_freep(&sub->rects[i]->text);
1387         av_freep(&sub->rects[i]->ass);
1388         av_freep(&sub->rects[i]);
1389     }
1390
1391     av_freep(&sub->rects);
1392
1393     memset(sub, 0, sizeof(AVSubtitle));
1394 }
1395
1396 av_cold int avcodec_close(AVCodecContext *avctx)
1397 {
1398     /* If there is a user-supplied mutex locking routine, call it. */
1399     if (ff_lockmgr_cb) {
1400         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1401             return -1;
1402     }
1403
1404     entangled_thread_counter++;
1405     if(entangled_thread_counter != 1){
1406         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1407         entangled_thread_counter--;
1408         return -1;
1409     }
1410
1411     if (HAVE_THREADS && avctx->thread_opaque)
1412         ff_thread_free(avctx);
1413     if (avctx->codec && avctx->codec->close)
1414         avctx->codec->close(avctx);
1415     avcodec_default_free_buffers(avctx);
1416     avctx->coded_frame = NULL;
1417     av_freep(&avctx->internal);
1418     if (avctx->codec && avctx->codec->priv_class)
1419         av_opt_free(avctx->priv_data);
1420     av_opt_free(avctx);
1421     av_freep(&avctx->priv_data);
1422     if (codec_is_encoder(avctx->codec))
1423         av_freep(&avctx->extradata);
1424     avctx->codec = NULL;
1425     avctx->active_thread_type = 0;
1426     entangled_thread_counter--;
1427
1428     /* Release any user-supplied mutex. */
1429     if (ff_lockmgr_cb) {
1430         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1431     }
1432     return 0;
1433 }
1434
1435 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1436 {
1437     switch(id){
1438         case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
1439         case CODEC_ID_G729_DEPRECATED   : return CODEC_ID_G729;
1440         case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1441         default                         : return id;
1442     }
1443 }
1444
1445 AVCodec *avcodec_find_encoder(enum CodecID id)
1446 {
1447     AVCodec *p, *experimental=NULL;
1448     p = first_avcodec;
1449     id= remap_deprecated_codec_id(id);
1450     while (p) {
1451         if (codec_is_encoder(p) && p->id == id) {
1452             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1453                 experimental = p;
1454             } else
1455                 return p;
1456         }
1457         p = p->next;
1458     }
1459     return experimental;
1460 }
1461
1462 AVCodec *avcodec_find_encoder_by_name(const char *name)
1463 {
1464     AVCodec *p;
1465     if (!name)
1466         return NULL;
1467     p = first_avcodec;
1468     while (p) {
1469         if (codec_is_encoder(p) && strcmp(name,p->name) == 0)
1470             return p;
1471         p = p->next;
1472     }
1473     return NULL;
1474 }
1475
1476 AVCodec *avcodec_find_decoder(enum CodecID id)
1477 {
1478     AVCodec *p, *experimental=NULL;
1479     p = first_avcodec;
1480     id= remap_deprecated_codec_id(id);
1481     while (p) {
1482         if (codec_is_decoder(p) && p->id == id) {
1483             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1484                 experimental = p;
1485             } else
1486                 return p;
1487         }
1488         p = p->next;
1489     }
1490     return experimental;
1491 }
1492
1493 AVCodec *avcodec_find_decoder_by_name(const char *name)
1494 {
1495     AVCodec *p;
1496     if (!name)
1497         return NULL;
1498     p = first_avcodec;
1499     while (p) {
1500         if (codec_is_decoder(p) && strcmp(name,p->name) == 0)
1501             return p;
1502         p = p->next;
1503     }
1504     return NULL;
1505 }
1506
1507 static int get_bit_rate(AVCodecContext *ctx)
1508 {
1509     int bit_rate;
1510     int bits_per_sample;
1511
1512     switch(ctx->codec_type) {
1513     case AVMEDIA_TYPE_VIDEO:
1514     case AVMEDIA_TYPE_DATA:
1515     case AVMEDIA_TYPE_SUBTITLE:
1516     case AVMEDIA_TYPE_ATTACHMENT:
1517         bit_rate = ctx->bit_rate;
1518         break;
1519     case AVMEDIA_TYPE_AUDIO:
1520         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1521         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1522         break;
1523     default:
1524         bit_rate = 0;
1525         break;
1526     }
1527     return bit_rate;
1528 }
1529
1530 const char *avcodec_get_name(enum CodecID id)
1531 {
1532     AVCodec *codec;
1533
1534 #if !CONFIG_SMALL
1535     switch (id) {
1536 #include "libavcodec/codec_names.h"
1537     }
1538     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1539 #endif
1540     codec = avcodec_find_decoder(id);
1541     if (codec)
1542         return codec->name;
1543     codec = avcodec_find_encoder(id);
1544     if (codec)
1545         return codec->name;
1546     return "unknown_codec";
1547 }
1548
1549 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1550 {
1551     int i, len, ret = 0;
1552
1553     for (i = 0; i < 4; i++) {
1554         len = snprintf(buf, buf_size,
1555                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1556         buf      += len;
1557         buf_size  = buf_size > len ? buf_size - len : 0;
1558         ret      += len;
1559         codec_tag>>=8;
1560     }
1561     return ret;
1562 }
1563
1564 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1565 {
1566     const char *codec_type;
1567     const char *codec_name;
1568     const char *profile = NULL;
1569     AVCodec *p;
1570     int bitrate;
1571     AVRational display_aspect_ratio;
1572
1573     if (!buf || buf_size <= 0)
1574         return;
1575     codec_type = av_get_media_type_string(enc->codec_type);
1576     codec_name = avcodec_get_name(enc->codec_id);
1577     if (enc->profile != FF_PROFILE_UNKNOWN) {
1578         p = encode ? avcodec_find_encoder(enc->codec_id) :
1579                      avcodec_find_decoder(enc->codec_id);
1580         if (p)
1581             profile = av_get_profile_name(p, enc->profile);
1582     }
1583
1584     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1585              codec_name, enc->mb_decision ? " (hq)" : "");
1586     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1587     if (profile)
1588         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1589     if (enc->codec_tag) {
1590         char tag_buf[32];
1591         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1592         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1593                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1594     }
1595     switch(enc->codec_type) {
1596     case AVMEDIA_TYPE_VIDEO:
1597         if (enc->pix_fmt != PIX_FMT_NONE) {
1598             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1599                      ", %s",
1600                      av_get_pix_fmt_name(enc->pix_fmt));
1601         }
1602         if (enc->width) {
1603             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1604                      ", %dx%d",
1605                      enc->width, enc->height);
1606             if (enc->sample_aspect_ratio.num) {
1607                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1608                           enc->width*enc->sample_aspect_ratio.num,
1609                           enc->height*enc->sample_aspect_ratio.den,
1610                           1024*1024);
1611                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1612                          " [SAR %d:%d DAR %d:%d]",
1613                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1614                          display_aspect_ratio.num, display_aspect_ratio.den);
1615             }
1616             if(av_log_get_level() >= AV_LOG_DEBUG){
1617                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1618                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1619                      ", %d/%d",
1620                      enc->time_base.num/g, enc->time_base.den/g);
1621             }
1622         }
1623         if (encode) {
1624             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1625                      ", q=%d-%d", enc->qmin, enc->qmax);
1626         }
1627         break;
1628     case AVMEDIA_TYPE_AUDIO:
1629         if (enc->sample_rate) {
1630             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1631                      ", %d Hz", enc->sample_rate);
1632         }
1633         av_strlcat(buf, ", ", buf_size);
1634         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1635         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1636             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1637                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1638         }
1639         break;
1640     default:
1641         return;
1642     }
1643     if (encode) {
1644         if (enc->flags & CODEC_FLAG_PASS1)
1645             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1646                      ", pass 1");
1647         if (enc->flags & CODEC_FLAG_PASS2)
1648             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1649                      ", pass 2");
1650     }
1651     bitrate = get_bit_rate(enc);
1652     if (bitrate != 0) {
1653         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1654                  ", %d kb/s", bitrate / 1000);
1655     }
1656 }
1657
1658 const char *av_get_profile_name(const AVCodec *codec, int profile)
1659 {
1660     const AVProfile *p;
1661     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1662         return NULL;
1663
1664     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1665         if (p->profile == profile)
1666             return p->name;
1667
1668     return NULL;
1669 }
1670
1671 unsigned avcodec_version( void )
1672 {
1673 //    av_assert0(CODEC_ID_V410==164);
1674     av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1675     av_assert0(CODEC_ID_ADPCM_G722==69660);
1676 //     av_assert0(CODEC_ID_BMV_AUDIO==86071);
1677     av_assert0(CODEC_ID_SRT==94216);
1678     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1679
1680   return LIBAVCODEC_VERSION_INT;
1681 }
1682
1683 const char *avcodec_configuration(void)
1684 {
1685     return FFMPEG_CONFIGURATION;
1686 }
1687
1688 const char *avcodec_license(void)
1689 {
1690 #define LICENSE_PREFIX "libavcodec license: "
1691     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1692 }
1693
1694 void avcodec_flush_buffers(AVCodecContext *avctx)
1695 {
1696     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1697         ff_thread_flush(avctx);
1698     else if(avctx->codec->flush)
1699         avctx->codec->flush(avctx);
1700 }
1701
1702 static void video_free_buffers(AVCodecContext *s)
1703 {
1704     AVCodecInternal *avci = s->internal;
1705     int i, j;
1706
1707     if (!avci->buffer)
1708         return;
1709
1710     if (avci->buffer_count)
1711         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1712                avci->buffer_count);
1713     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1714         InternalBuffer *buf = &avci->buffer[i];
1715         for(j=0; j<4; j++){
1716             av_freep(&buf->base[j]);
1717             buf->data[j]= NULL;
1718         }
1719     }
1720     av_freep(&avci->buffer);
1721
1722     avci->buffer_count=0;
1723 }
1724
1725 static void audio_free_buffers(AVCodecContext *avctx)
1726 {
1727     AVCodecInternal *avci = avctx->internal;
1728     InternalBuffer *buf;
1729
1730     if (!avci->buffer)
1731         return;
1732     buf = avci->buffer;
1733
1734     if (buf->extended_data) {
1735         av_free(buf->extended_data[0]);
1736         if (buf->extended_data != buf->data)
1737             av_freep(&buf->extended_data);
1738     }
1739     av_freep(&avci->buffer);
1740 }
1741
1742 void avcodec_default_free_buffers(AVCodecContext *avctx)
1743 {
1744     switch (avctx->codec_type) {
1745     case AVMEDIA_TYPE_VIDEO:
1746         video_free_buffers(avctx);
1747         break;
1748     case AVMEDIA_TYPE_AUDIO:
1749         audio_free_buffers(avctx);
1750         break;
1751     default:
1752         break;
1753     }
1754 }
1755
1756 int av_get_bits_per_sample(enum CodecID codec_id){
1757     switch(codec_id){
1758     case CODEC_ID_ADPCM_SBPRO_2:
1759         return 2;
1760     case CODEC_ID_ADPCM_SBPRO_3:
1761         return 3;
1762     case CODEC_ID_ADPCM_SBPRO_4:
1763     case CODEC_ID_ADPCM_CT:
1764     case CODEC_ID_ADPCM_IMA_APC:
1765     case CODEC_ID_ADPCM_IMA_WAV:
1766     case CODEC_ID_ADPCM_IMA_QT:
1767     case CODEC_ID_ADPCM_SWF:
1768     case CODEC_ID_ADPCM_MS:
1769     case CODEC_ID_ADPCM_YAMAHA:
1770     case CODEC_ID_ADPCM_G722:
1771         return 4;
1772     case CODEC_ID_PCM_ALAW:
1773     case CODEC_ID_PCM_MULAW:
1774     case CODEC_ID_PCM_S8:
1775     case CODEC_ID_PCM_U8:
1776     case CODEC_ID_PCM_ZORK:
1777         return 8;
1778     case CODEC_ID_PCM_S16BE:
1779     case CODEC_ID_PCM_S16LE:
1780     case CODEC_ID_PCM_S16LE_PLANAR:
1781     case CODEC_ID_PCM_U16BE:
1782     case CODEC_ID_PCM_U16LE:
1783         return 16;
1784     case CODEC_ID_PCM_S24DAUD:
1785     case CODEC_ID_PCM_S24BE:
1786     case CODEC_ID_PCM_S24LE:
1787     case CODEC_ID_PCM_U24BE:
1788     case CODEC_ID_PCM_U24LE:
1789         return 24;
1790     case CODEC_ID_PCM_S32BE:
1791     case CODEC_ID_PCM_S32LE:
1792     case CODEC_ID_PCM_U32BE:
1793     case CODEC_ID_PCM_U32LE:
1794     case CODEC_ID_PCM_F32BE:
1795     case CODEC_ID_PCM_F32LE:
1796         return 32;
1797     case CODEC_ID_PCM_F64BE:
1798     case CODEC_ID_PCM_F64LE:
1799         return 64;
1800     default:
1801         return 0;
1802     }
1803 }
1804
1805 #if !HAVE_THREADS
1806 int ff_thread_init(AVCodecContext *s){
1807     return -1;
1808 }
1809 #endif
1810
1811 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1812 {
1813     unsigned int n = 0;
1814
1815     while(v >= 0xff) {
1816         *s++ = 0xff;
1817         v -= 0xff;
1818         n++;
1819     }
1820     *s = v;
1821     n++;
1822     return n;
1823 }
1824
1825 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1826     int i;
1827     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1828     return i;
1829 }
1830
1831 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1832 {
1833     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1834             "version to the newest one from Git. If the problem still "
1835             "occurs, it means that your file has a feature which has not "
1836             "been implemented.\n", feature);
1837     if(want_sample)
1838         av_log_ask_for_sample(avc, NULL);
1839 }
1840
1841 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1842 {
1843     va_list argument_list;
1844
1845     va_start(argument_list, msg);
1846
1847     if (msg)
1848         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1849     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1850             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1851             "and contact the ffmpeg-devel mailing list.\n");
1852
1853     va_end(argument_list);
1854 }
1855
1856 static AVHWAccel *first_hwaccel = NULL;
1857
1858 void av_register_hwaccel(AVHWAccel *hwaccel)
1859 {
1860     AVHWAccel **p = &first_hwaccel;
1861     while (*p)
1862         p = &(*p)->next;
1863     *p = hwaccel;
1864     hwaccel->next = NULL;
1865 }
1866
1867 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1868 {
1869     return hwaccel ? hwaccel->next : first_hwaccel;
1870 }
1871
1872 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1873 {
1874     AVHWAccel *hwaccel=NULL;
1875
1876     while((hwaccel= av_hwaccel_next(hwaccel))){
1877         if (   hwaccel->id      == codec_id
1878             && hwaccel->pix_fmt == pix_fmt)
1879             return hwaccel;
1880     }
1881     return NULL;
1882 }
1883
1884 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1885 {
1886     if (ff_lockmgr_cb) {
1887         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1888             return -1;
1889         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1890             return -1;
1891     }
1892
1893     ff_lockmgr_cb = cb;
1894
1895     if (ff_lockmgr_cb) {
1896         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1897             return -1;
1898         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1899             return -1;
1900     }
1901     return 0;
1902 }
1903
1904 int avpriv_lock_avformat(void)
1905 {
1906     if (ff_lockmgr_cb) {
1907         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1908             return -1;
1909     }
1910     return 0;
1911 }
1912
1913 int avpriv_unlock_avformat(void)
1914 {
1915     if (ff_lockmgr_cb) {
1916         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1917             return -1;
1918     }
1919     return 0;
1920 }
1921
1922 unsigned int avpriv_toupper4(unsigned int x)
1923 {
1924     return     toupper( x     &0xFF)
1925             + (toupper((x>>8 )&0xFF)<<8 )
1926             + (toupper((x>>16)&0xFF)<<16)
1927             + (toupper((x>>24)&0xFF)<<24);
1928 }
1929
1930 #if !HAVE_THREADS
1931
1932 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1933 {
1934     f->owner = avctx;
1935
1936     ff_init_buffer_info(avctx, f);
1937
1938     return avctx->get_buffer(avctx, f);
1939 }
1940
1941 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1942 {
1943     f->owner->release_buffer(f->owner, f);
1944 }
1945
1946 void ff_thread_finish_setup(AVCodecContext *avctx)
1947 {
1948 }
1949
1950 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1951 {
1952 }
1953
1954 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1955 {
1956 }
1957
1958 #endif
1959
1960 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1961 {
1962     AVCodec *c= avcodec_find_decoder(codec_id);
1963     if(!c)
1964         c= avcodec_find_encoder(codec_id);
1965     if(c)
1966         return c->type;
1967
1968     if (codec_id <= CODEC_ID_NONE)
1969         return AVMEDIA_TYPE_UNKNOWN;
1970     else if (codec_id < CODEC_ID_FIRST_AUDIO)
1971         return AVMEDIA_TYPE_VIDEO;
1972     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1973         return AVMEDIA_TYPE_AUDIO;
1974     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1975         return AVMEDIA_TYPE_SUBTITLE;
1976
1977     return AVMEDIA_TYPE_UNKNOWN;
1978 }