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