]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
lavc: adjust timestamp in case of skiping initial samples
[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 "frame_thread_encoder.h"
44 #include "audioconvert.h"
45 #include "internal.h"
46 #include "bytestream.h"
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <limits.h>
50 #include <float.h>
51
52 static int volatile entangled_thread_counter=0;
53 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
54 static void *codec_mutex;
55 static void *avformat_mutex;
56
57 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
58 {
59     if(min_size < *size)
60         return ptr;
61
62     min_size= FFMAX(17*min_size/16 + 32, min_size);
63
64     ptr= av_realloc(ptr, min_size);
65     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
66         min_size= 0;
67
68     *size= min_size;
69
70     return ptr;
71 }
72
73 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
74 {
75     void **p = ptr;
76     if (min_size < *size)
77         return 0;
78     min_size= FFMAX(17*min_size/16 + 32, min_size);
79     av_free(*p);
80     *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
81     if (!*p) min_size = 0;
82     *size= min_size;
83     return 1;
84 }
85
86 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
87 {
88     ff_fast_malloc(ptr, size, min_size, 0);
89 }
90
91 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
92 {
93     uint8_t **p = ptr;
94     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
95         av_freep(p);
96         *size = 0;
97         return;
98     }
99     if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
100         memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
101 }
102
103 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
104 {
105     uint8_t **p = ptr;
106     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
107         av_freep(p);
108         *size = 0;
109         return;
110     }
111     if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
112         memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
113 }
114
115 /* encoder management */
116 static AVCodec *first_avcodec = NULL;
117
118 AVCodec *av_codec_next(AVCodec *c){
119     if(c) return c->next;
120     else  return first_avcodec;
121 }
122
123 static void avcodec_init(void)
124 {
125     static int initialized = 0;
126
127     if (initialized != 0)
128         return;
129     initialized = 1;
130
131     ff_dsputil_static_init();
132 }
133
134 int av_codec_is_encoder(AVCodec *codec)
135 {
136     return codec && (codec->encode || codec->encode2);
137 }
138
139 int av_codec_is_decoder(AVCodec *codec)
140 {
141     return codec && codec->decode;
142 }
143
144 void avcodec_register(AVCodec *codec)
145 {
146     AVCodec **p;
147     avcodec_init();
148     p = &first_avcodec;
149     while (*p != NULL) p = &(*p)->next;
150     *p = codec;
151     codec->next = NULL;
152
153     if (codec->init_static_data)
154         codec->init_static_data(codec);
155 }
156
157 unsigned avcodec_get_edge_width(void)
158 {
159     return EDGE_WIDTH;
160 }
161
162 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
163     s->coded_width = width;
164     s->coded_height= height;
165     s->width = -((-width )>>s->lowres);
166     s->height= -((-height)>>s->lowres);
167 }
168
169 #define INTERNAL_BUFFER_SIZE (32+1)
170
171 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
172                                int linesize_align[AV_NUM_DATA_POINTERS])
173 {
174     int i;
175     int w_align= 1;
176     int h_align= 1;
177
178     switch(s->pix_fmt){
179     case PIX_FMT_YUV420P:
180     case PIX_FMT_YUYV422:
181     case PIX_FMT_UYVY422:
182     case PIX_FMT_YUV422P:
183     case PIX_FMT_YUV440P:
184     case PIX_FMT_YUV444P:
185     case PIX_FMT_GBRP:
186     case PIX_FMT_GRAY8:
187     case PIX_FMT_GRAY16BE:
188     case PIX_FMT_GRAY16LE:
189     case PIX_FMT_YUVJ420P:
190     case PIX_FMT_YUVJ422P:
191     case PIX_FMT_YUVJ440P:
192     case PIX_FMT_YUVJ444P:
193     case PIX_FMT_YUVA420P:
194     case PIX_FMT_YUVA422P:
195     case PIX_FMT_YUVA444P:
196     case PIX_FMT_YUV420P9LE:
197     case PIX_FMT_YUV420P9BE:
198     case PIX_FMT_YUV420P10LE:
199     case PIX_FMT_YUV420P10BE:
200     case PIX_FMT_YUV420P12LE:
201     case PIX_FMT_YUV420P12BE:
202     case PIX_FMT_YUV420P14LE:
203     case PIX_FMT_YUV420P14BE:
204     case PIX_FMT_YUV422P9LE:
205     case PIX_FMT_YUV422P9BE:
206     case PIX_FMT_YUV422P10LE:
207     case PIX_FMT_YUV422P10BE:
208     case PIX_FMT_YUV422P12LE:
209     case PIX_FMT_YUV422P12BE:
210     case PIX_FMT_YUV422P14LE:
211     case PIX_FMT_YUV422P14BE:
212     case PIX_FMT_YUV444P9LE:
213     case PIX_FMT_YUV444P9BE:
214     case PIX_FMT_YUV444P10LE:
215     case PIX_FMT_YUV444P10BE:
216     case PIX_FMT_YUV444P12LE:
217     case PIX_FMT_YUV444P12BE:
218     case PIX_FMT_YUV444P14LE:
219     case PIX_FMT_YUV444P14BE:
220     case PIX_FMT_GBRP9LE:
221     case PIX_FMT_GBRP9BE:
222     case PIX_FMT_GBRP10LE:
223     case PIX_FMT_GBRP10BE:
224     case PIX_FMT_GBRP12LE:
225     case PIX_FMT_GBRP12BE:
226     case PIX_FMT_GBRP14LE:
227     case PIX_FMT_GBRP14BE:
228         w_align = 16; //FIXME assume 16 pixel per macroblock
229         h_align = 16 * 2; // interlaced needs 2 macroblocks height
230         break;
231     case PIX_FMT_YUV411P:
232     case PIX_FMT_UYYVYY411:
233         w_align=32;
234         h_align=8;
235         break;
236     case PIX_FMT_YUV410P:
237         if(s->codec_id == CODEC_ID_SVQ1){
238             w_align=64;
239             h_align=64;
240         }
241     case PIX_FMT_RGB555:
242         if(s->codec_id == CODEC_ID_RPZA){
243             w_align=4;
244             h_align=4;
245         }
246     case PIX_FMT_PAL8:
247     case PIX_FMT_BGR8:
248     case PIX_FMT_RGB8:
249         if(s->codec_id == CODEC_ID_SMC){
250             w_align=4;
251             h_align=4;
252         }
253         break;
254     case PIX_FMT_BGR24:
255         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
256             w_align=4;
257             h_align=4;
258         }
259         break;
260     default:
261         w_align= 1;
262         h_align= 1;
263         break;
264     }
265
266     if(s->codec_id == CODEC_ID_IFF_ILBM || s->codec_id == CODEC_ID_IFF_BYTERUN1){
267         w_align= FFMAX(w_align, 8);
268     }
269
270     *width = FFALIGN(*width , w_align);
271     *height= FFALIGN(*height, h_align);
272     if(s->codec_id == CODEC_ID_H264 || s->lowres)
273         *height+=2; // some of the optimized chroma MC reads one line too much
274                     // which is also done in mpeg decoders with lowres > 0
275
276     for (i = 0; i < 4; i++)
277         linesize_align[i] = STRIDE_ALIGN;
278 }
279
280 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
281     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
282     int linesize_align[AV_NUM_DATA_POINTERS];
283     int align;
284     avcodec_align_dimensions2(s, width, height, linesize_align);
285     align = FFMAX(linesize_align[0], linesize_align[3]);
286     linesize_align[1] <<= chroma_shift;
287     linesize_align[2] <<= chroma_shift;
288     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
289     *width=FFALIGN(*width, align);
290 }
291
292 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
293 {
294     if (s->pkt) {
295         pic->pkt_pts = s->pkt->pts;
296         pic->pkt_pos = s->pkt->pos;
297         pic->pkt_duration = s->pkt->duration;
298     } else {
299         pic->pkt_pts = AV_NOPTS_VALUE;
300         pic->pkt_pos = -1;
301         pic->pkt_duration = 0;
302     }
303     pic->reordered_opaque= s->reordered_opaque;
304     pic->sample_aspect_ratio = s->sample_aspect_ratio;
305     pic->width               = s->width;
306     pic->height              = s->height;
307     pic->format              = s->pix_fmt;
308 }
309
310 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
311                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
312                              int buf_size, int align)
313 {
314     int ch, planar, needed_size, ret = 0;
315
316     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
317                                              frame->nb_samples, sample_fmt,
318                                              align);
319     if (buf_size < needed_size)
320         return AVERROR(EINVAL);
321
322     planar = av_sample_fmt_is_planar(sample_fmt);
323     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
324         if (!(frame->extended_data = av_mallocz(nb_channels *
325                                                 sizeof(*frame->extended_data))))
326             return AVERROR(ENOMEM);
327     } else {
328         frame->extended_data = frame->data;
329     }
330
331     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
332                                       (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
333                                       sample_fmt, align)) < 0) {
334         if (frame->extended_data != frame->data)
335             av_freep(&frame->extended_data);
336         return ret;
337     }
338     if (frame->extended_data != frame->data) {
339         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
340             frame->data[ch] = frame->extended_data[ch];
341     }
342
343     return ret;
344 }
345
346 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
347 {
348     AVCodecInternal *avci = avctx->internal;
349     InternalBuffer *buf;
350     int buf_size, ret;
351
352     buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
353                                           frame->nb_samples, avctx->sample_fmt,
354                                           0);
355     if (buf_size < 0)
356         return AVERROR(EINVAL);
357
358     /* allocate InternalBuffer if needed */
359     if (!avci->buffer) {
360         avci->buffer = av_mallocz(sizeof(InternalBuffer));
361         if (!avci->buffer)
362             return AVERROR(ENOMEM);
363     }
364     buf = avci->buffer;
365
366     /* if there is a previously-used internal buffer, check its size and
367        channel count to see if we can reuse it */
368     if (buf->extended_data) {
369         /* if current buffer is too small, free it */
370         if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
371             av_free(buf->extended_data[0]);
372             if (buf->extended_data != buf->data)
373                 av_freep(&buf->extended_data);
374             buf->extended_data = NULL;
375             buf->data[0] = NULL;
376         }
377         /* if number of channels has changed, reset and/or free extended data
378            pointers but leave data buffer in buf->data[0] for reuse */
379         if (buf->nb_channels != avctx->channels) {
380             if (buf->extended_data != buf->data)
381                 av_free(buf->extended_data);
382             buf->extended_data = NULL;
383         }
384     }
385
386     /* if there is no previous buffer or the previous buffer cannot be used
387        as-is, allocate a new buffer and/or rearrange the channel pointers */
388     if (!buf->extended_data) {
389         if (!buf->data[0]) {
390             if (!(buf->data[0] = av_mallocz(buf_size)))
391                 return AVERROR(ENOMEM);
392             buf->audio_data_size = buf_size;
393         }
394         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
395                                             avctx->sample_fmt, buf->data[0],
396                                             buf->audio_data_size, 0)))
397             return ret;
398
399         if (frame->extended_data == frame->data)
400             buf->extended_data = buf->data;
401         else
402             buf->extended_data = frame->extended_data;
403         memcpy(buf->data, frame->data, sizeof(frame->data));
404         buf->linesize[0] = frame->linesize[0];
405         buf->nb_channels = avctx->channels;
406     } else {
407         /* copy InternalBuffer info to the AVFrame */
408         frame->extended_data = buf->extended_data;
409         frame->linesize[0]   = buf->linesize[0];
410         memcpy(frame->data, buf->data, sizeof(frame->data));
411     }
412
413     frame->type          = FF_BUFFER_TYPE_INTERNAL;
414
415     if (avctx->pkt) {
416         frame->pkt_pts = avctx->pkt->pts;
417         frame->pkt_pos = avctx->pkt->pos;
418         frame->pkt_duration = avctx->pkt->duration;
419     } else {
420         frame->pkt_pts = AV_NOPTS_VALUE;
421         frame->pkt_pos = -1;
422         frame->pkt_duration = 0;
423     }
424
425     frame->reordered_opaque = avctx->reordered_opaque;
426
427     frame->sample_rate    = avctx->sample_rate;
428     frame->format         = avctx->sample_fmt;
429     frame->channel_layout = avctx->channel_layout;
430
431     if (avctx->debug & FF_DEBUG_BUFFERS)
432         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
433                "internal audio buffer used\n", frame);
434
435     return 0;
436 }
437
438 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
439 {
440     int i;
441     int w= s->width;
442     int h= s->height;
443     InternalBuffer *buf;
444     AVCodecInternal *avci = s->internal;
445
446     if(pic->data[0]!=NULL) {
447         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
448         return -1;
449     }
450     if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
451         av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
452         return -1;
453     }
454
455     if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
456         av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
457         return -1;
458     }
459
460     if (!avci->buffer) {
461         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
462                                   sizeof(InternalBuffer));
463     }
464
465     buf = &avci->buffer[avci->buffer_count];
466
467     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
468         for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
469             av_freep(&buf->base[i]);
470             buf->data[i]= NULL;
471         }
472     }
473
474     if (!buf->base[0]) {
475         int h_chroma_shift, v_chroma_shift;
476         int size[4] = {0};
477         int tmpsize;
478         int unaligned;
479         AVPicture picture;
480         int stride_align[AV_NUM_DATA_POINTERS];
481         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
482
483         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
484
485         avcodec_align_dimensions2(s, &w, &h, stride_align);
486
487         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
488             w+= EDGE_WIDTH*2;
489             h+= EDGE_WIDTH*2;
490         }
491
492         do {
493             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
494             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
495             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
496             // increase alignment of w for next try (rhs gives the lowest bit set in w)
497             w += w & ~(w-1);
498
499             unaligned = 0;
500             for (i=0; i<4; i++){
501                 unaligned |= picture.linesize[i] % stride_align[i];
502             }
503         } while (unaligned);
504
505         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
506         if (tmpsize < 0)
507             return -1;
508
509         for (i=0; i<3 && picture.data[i+1]; i++)
510             size[i] = picture.data[i+1] - picture.data[i];
511         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
512
513         memset(buf->base, 0, sizeof(buf->base));
514         memset(buf->data, 0, sizeof(buf->data));
515
516         for(i=0; i<4 && size[i]; i++){
517             const int h_shift= i==0 ? 0 : h_chroma_shift;
518             const int v_shift= i==0 ? 0 : v_chroma_shift;
519
520             buf->linesize[i]= picture.linesize[i];
521
522             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
523             if(buf->base[i]==NULL)
524                 return AVERROR(ENOMEM);
525             memset(buf->base[i], 128, size[i]);
526
527             // no edge if EDGE EMU or not planar YUV
528             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
529                 buf->data[i] = buf->base[i];
530             else
531                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
532         }
533         for (; i < AV_NUM_DATA_POINTERS; i++) {
534             buf->base[i] = buf->data[i] = NULL;
535             buf->linesize[i] = 0;
536         }
537         if(size[1] && !size[2])
538             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
539         buf->width  = s->width;
540         buf->height = s->height;
541         buf->pix_fmt= s->pix_fmt;
542     }
543     pic->type= FF_BUFFER_TYPE_INTERNAL;
544
545     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
546         pic->base[i]= buf->base[i];
547         pic->data[i]= buf->data[i];
548         pic->linesize[i]= buf->linesize[i];
549     }
550     pic->extended_data = pic->data;
551     avci->buffer_count++;
552     pic->width  = buf->width;
553     pic->height = buf->height;
554     pic->format = buf->pix_fmt;
555     pic->sample_aspect_ratio = s->sample_aspect_ratio;
556
557     if (s->pkt) {
558         pic->pkt_pts = s->pkt->pts;
559         pic->pkt_pos = s->pkt->pos;
560         pic->pkt_duration = s->pkt->duration;
561     } else {
562         pic->pkt_pts = AV_NOPTS_VALUE;
563         pic->pkt_pos = -1;
564         pic->pkt_duration = 0;
565     }
566     pic->reordered_opaque= s->reordered_opaque;
567     pic->sample_aspect_ratio = s->sample_aspect_ratio;
568     pic->width               = s->width;
569     pic->height              = s->height;
570     pic->format              = s->pix_fmt;
571
572     if(s->debug&FF_DEBUG_BUFFERS)
573         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
574                "buffers used\n", pic, avci->buffer_count);
575
576     return 0;
577 }
578
579 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
580 {
581     switch (avctx->codec_type) {
582     case AVMEDIA_TYPE_VIDEO:
583         return video_get_buffer(avctx, frame);
584     case AVMEDIA_TYPE_AUDIO:
585         return audio_get_buffer(avctx, frame);
586     default:
587         return -1;
588     }
589 }
590
591 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
592     int i;
593     InternalBuffer *buf, *last;
594     AVCodecInternal *avci = s->internal;
595
596     av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
597
598     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
599     assert(avci->buffer_count);
600
601     if (avci->buffer) {
602         buf = NULL; /* avoids warning */
603         for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
604             buf = &avci->buffer[i];
605             if (buf->data[0] == pic->data[0])
606                 break;
607         }
608         av_assert0(i < avci->buffer_count);
609         avci->buffer_count--;
610         last = &avci->buffer[avci->buffer_count];
611
612         if (buf != last)
613             FFSWAP(InternalBuffer, *buf, *last);
614     }
615
616     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
617         pic->data[i]=NULL;
618 //        pic->base[i]=NULL;
619     }
620 //printf("R%X\n", pic->opaque);
621
622     if(s->debug&FF_DEBUG_BUFFERS)
623         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
624                "buffers used\n", pic, avci->buffer_count);
625 }
626
627 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
628     AVFrame temp_pic;
629     int i;
630
631     av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
632
633     if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
634         av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
635                pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
636         s->release_buffer(s, pic);
637     }
638
639     ff_init_buffer_info(s, pic);
640
641     /* If no picture return a new buffer */
642     if(pic->data[0] == NULL) {
643         /* We will copy from buffer, so must be readable */
644         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
645         return s->get_buffer(s, pic);
646     }
647
648     assert(s->pix_fmt == pic->format);
649
650     /* If internal buffer type return the same buffer */
651     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
652         return 0;
653     }
654
655     /*
656      * Not internal type and reget_buffer not overridden, emulate cr buffer
657      */
658     temp_pic = *pic;
659     for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
660         pic->data[i] = pic->base[i] = NULL;
661     pic->opaque = NULL;
662     /* Allocate new frame */
663     if (s->get_buffer(s, pic))
664         return -1;
665     /* Copy image data from old buffer to new buffer */
666     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
667              s->height);
668     s->release_buffer(s, &temp_pic); // Release old frame
669     return 0;
670 }
671
672 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
673     int i;
674
675     for(i=0; i<count; i++){
676         int r= func(c, (char*)arg + i*size);
677         if(ret) ret[i]= r;
678     }
679     return 0;
680 }
681
682 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
683     int i;
684
685     for(i=0; i<count; i++){
686         int r= func(c, arg, i, 0);
687         if(ret) ret[i]= r;
688     }
689     return 0;
690 }
691
692 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
693     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
694         ++fmt;
695     return fmt[0];
696 }
697
698 void avcodec_get_frame_defaults(AVFrame *pic){
699     memset(pic, 0, sizeof(AVFrame));
700
701     pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
702     pic->pkt_duration = 0;
703     pic->pkt_pos = -1;
704     pic->key_frame= 1;
705     pic->sample_aspect_ratio = (AVRational){0, 1};
706     pic->format = -1;           /* unknown */
707 }
708
709 AVFrame *avcodec_alloc_frame(void){
710     AVFrame *pic= av_malloc(sizeof(AVFrame));
711
712     if(pic==NULL) return NULL;
713
714     avcodec_get_frame_defaults(pic);
715
716     return pic;
717 }
718
719 #define MAKE_ACCESSORS(str, name, type, field) \
720     type av_##name##_get_##field(const str *s) { return s->field; } \
721     void av_##name##_set_##field(str *s, type v) { s->field = v; }
722
723 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
724 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
725 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
726 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
727 MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
728
729 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
730
731 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
732 {
733     memset(sub, 0, sizeof(*sub));
734     sub->pts = AV_NOPTS_VALUE;
735 }
736
737 static int get_bit_rate(AVCodecContext *ctx)
738 {
739     int bit_rate;
740     int bits_per_sample;
741
742     switch(ctx->codec_type) {
743     case AVMEDIA_TYPE_VIDEO:
744     case AVMEDIA_TYPE_DATA:
745     case AVMEDIA_TYPE_SUBTITLE:
746     case AVMEDIA_TYPE_ATTACHMENT:
747         bit_rate = ctx->bit_rate;
748         break;
749     case AVMEDIA_TYPE_AUDIO:
750         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
751         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
752         break;
753     default:
754         bit_rate = 0;
755         break;
756     }
757     return bit_rate;
758 }
759
760 #if FF_API_AVCODEC_OPEN
761 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
762 {
763     return avcodec_open2(avctx, codec, NULL);
764 }
765 #endif
766
767 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
768 {
769     int ret = 0;
770     AVDictionary *tmp = NULL;
771
772     if (avcodec_is_open(avctx))
773         return 0;
774
775     if ((!codec && !avctx->codec)) {
776         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
777         return AVERROR(EINVAL);
778     }
779     if ((codec && avctx->codec && codec != avctx->codec)) {
780         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
781                "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
782         return AVERROR(EINVAL);
783     }
784     if (!codec)
785         codec = avctx->codec;
786
787     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
788         return AVERROR(EINVAL);
789
790     if (options)
791         av_dict_copy(&tmp, *options, 0);
792
793     /* If there is a user-supplied mutex locking routine, call it. */
794     if (ff_lockmgr_cb) {
795         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
796             return -1;
797     }
798
799     entangled_thread_counter++;
800     if(entangled_thread_counter != 1){
801         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
802         ret = -1;
803         goto end;
804     }
805
806     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
807     if (!avctx->internal) {
808         ret = AVERROR(ENOMEM);
809         goto end;
810     }
811
812     if (codec->priv_data_size > 0) {
813       if(!avctx->priv_data){
814         avctx->priv_data = av_mallocz(codec->priv_data_size);
815         if (!avctx->priv_data) {
816             ret = AVERROR(ENOMEM);
817             goto end;
818         }
819         if (codec->priv_class) {
820             *(const AVClass**)avctx->priv_data= codec->priv_class;
821             av_opt_set_defaults(avctx->priv_data);
822         }
823       }
824       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
825           goto free_and_end;
826     } else {
827         avctx->priv_data = NULL;
828     }
829     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
830         goto free_and_end;
831
832     if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
833         if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
834             av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
835             ret = -1;
836             goto free_and_end;
837         }
838
839     //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
840     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
841     if(avctx->coded_width && avctx->coded_height)
842         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
843     else if(avctx->width && avctx->height)
844         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
845     }
846
847     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
848         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
849            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
850         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
851         avcodec_set_dimensions(avctx, 0, 0);
852     }
853
854     /* if the decoder init function was already called previously,
855        free the already allocated subtitle_header before overwriting it */
856     if (av_codec_is_decoder(codec))
857         av_freep(&avctx->subtitle_header);
858
859 #define SANE_NB_CHANNELS 128U
860     if (avctx->channels > SANE_NB_CHANNELS) {
861         ret = AVERROR(EINVAL);
862         goto free_and_end;
863     }
864
865     avctx->codec = codec;
866     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
867         avctx->codec_id == CODEC_ID_NONE) {
868         avctx->codec_type = codec->type;
869         avctx->codec_id   = codec->id;
870     }
871     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
872                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
873         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
874         ret = AVERROR(EINVAL);
875         goto free_and_end;
876     }
877     avctx->frame_number = 0;
878
879     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
880         (!avctx->time_base.num || !avctx->time_base.den)) {
881         avctx->time_base.num = 1;
882         avctx->time_base.den = avctx->sample_rate;
883     }
884
885     if (!HAVE_THREADS)
886         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
887
888     entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
889     ret = ff_frame_thread_encoder_init(avctx);
890     entangled_thread_counter++;
891     if (ret < 0)
892         goto free_and_end;
893
894     if (HAVE_THREADS && !avctx->thread_opaque
895         && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
896         ret = ff_thread_init(avctx);
897         if (ret < 0) {
898             goto free_and_end;
899         }
900     }
901     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
902         avctx->thread_count = 1;
903
904     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
905         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
906                avctx->codec->max_lowres);
907         ret = AVERROR(EINVAL);
908         goto free_and_end;
909     }
910
911     if (av_codec_is_encoder(avctx->codec)) {
912         int i;
913         if (avctx->codec->sample_fmts) {
914             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
915                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
916                     break;
917             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
918                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
919                 ret = AVERROR(EINVAL);
920                 goto free_and_end;
921             }
922         }
923         if (avctx->codec->pix_fmts) {
924             for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
925                 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
926                     break;
927             if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
928                 && !((avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG)
929                      && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
930                 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
931                 ret = AVERROR(EINVAL);
932                 goto free_and_end;
933             }
934         }
935         if (avctx->codec->supported_samplerates) {
936             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
937                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
938                     break;
939             if (avctx->codec->supported_samplerates[i] == 0) {
940                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
941                 ret = AVERROR(EINVAL);
942                 goto free_and_end;
943             }
944         }
945         if (avctx->codec->channel_layouts) {
946             if (!avctx->channel_layout) {
947                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
948             } else {
949                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
950                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
951                         break;
952                 if (avctx->codec->channel_layouts[i] == 0) {
953                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
954                     ret = AVERROR(EINVAL);
955                     goto free_and_end;
956                 }
957             }
958         }
959         if (avctx->channel_layout && avctx->channels) {
960             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
961                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
962                 ret = AVERROR(EINVAL);
963                 goto free_and_end;
964             }
965         } else if (avctx->channel_layout) {
966             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
967         }
968     }
969
970     avctx->pts_correction_num_faulty_pts =
971     avctx->pts_correction_num_faulty_dts = 0;
972     avctx->pts_correction_last_pts =
973     avctx->pts_correction_last_dts = INT64_MIN;
974
975     if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
976         ret = avctx->codec->init(avctx);
977         if (ret < 0) {
978             goto free_and_end;
979         }
980     }
981
982     ret=0;
983
984     if (av_codec_is_decoder(avctx->codec)) {
985         if (!avctx->bit_rate)
986             avctx->bit_rate = get_bit_rate(avctx);
987         /* validate channel layout from the decoder */
988         if (avctx->channel_layout &&
989             av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
990             av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
991             avctx->channel_layout = 0;
992         }
993     }
994 end:
995     entangled_thread_counter--;
996
997     /* Release any user-supplied mutex. */
998     if (ff_lockmgr_cb) {
999         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1000     }
1001     if (options) {
1002         av_dict_free(options);
1003         *options = tmp;
1004     }
1005
1006     return ret;
1007 free_and_end:
1008     av_dict_free(&tmp);
1009     av_freep(&avctx->priv_data);
1010     av_freep(&avctx->internal);
1011     avctx->codec= NULL;
1012     goto end;
1013 }
1014
1015 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1016 {
1017     if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1018         av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1019         return AVERROR(EINVAL);
1020     }
1021
1022     if (avctx) {
1023     av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1024     if (!avpkt->data || avpkt->size < size) {
1025         av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1026         avpkt->data = avctx->internal->byte_buffer;
1027         avpkt->size = avctx->internal->byte_buffer_size;
1028         avpkt->destruct = NULL;
1029     }
1030     }
1031
1032     if (avpkt->data) {
1033         void *destruct = avpkt->destruct;
1034
1035         if (avpkt->size < size) {
1036             av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1037             return AVERROR(EINVAL);
1038         }
1039
1040         av_init_packet(avpkt);
1041         avpkt->destruct = destruct;
1042         avpkt->size = size;
1043         return 0;
1044     } else {
1045         int ret = av_new_packet(avpkt, size);
1046         if (ret < 0)
1047             av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1048         return ret;
1049     }
1050 }
1051
1052 int ff_alloc_packet(AVPacket *avpkt, int size)
1053 {
1054     return ff_alloc_packet2(NULL, avpkt, size);
1055 }
1056
1057 /**
1058  * Pad last frame with silence.
1059  */
1060 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1061 {
1062     AVFrame *frame = NULL;
1063     uint8_t *buf   = NULL;
1064     int ret;
1065
1066     if (!(frame = avcodec_alloc_frame()))
1067         return AVERROR(ENOMEM);
1068     *frame = *src;
1069
1070     if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1071                                           s->frame_size, s->sample_fmt, 0)) < 0)
1072         goto fail;
1073
1074     if (!(buf = av_malloc(ret))) {
1075         ret = AVERROR(ENOMEM);
1076         goto fail;
1077     }
1078
1079     frame->nb_samples = s->frame_size;
1080     if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1081                                         buf, ret, 0)) < 0)
1082         goto fail;
1083     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1084                                src->nb_samples, s->channels, s->sample_fmt)) < 0)
1085         goto fail;
1086     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1087                                       frame->nb_samples - src->nb_samples,
1088                                       s->channels, s->sample_fmt)) < 0)
1089         goto fail;
1090
1091     *dst = frame;
1092
1093     return 0;
1094
1095 fail:
1096     if (frame->extended_data != frame->data)
1097         av_freep(&frame->extended_data);
1098     av_freep(&buf);
1099     av_freep(&frame);
1100     return ret;
1101 }
1102
1103 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1104                                               AVPacket *avpkt,
1105                                               const AVFrame *frame,
1106                                               int *got_packet_ptr)
1107 {
1108     AVFrame tmp;
1109     AVFrame *padded_frame = NULL;
1110     int ret;
1111     AVPacket user_pkt = *avpkt;
1112     int needs_realloc = !user_pkt.data;
1113
1114     *got_packet_ptr = 0;
1115
1116     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1117         av_free_packet(avpkt);
1118         av_init_packet(avpkt);
1119         return 0;
1120     }
1121
1122     /* ensure that extended_data is properly set */
1123     if (frame && !frame->extended_data) {
1124         if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1125             avctx->channels > AV_NUM_DATA_POINTERS) {
1126             av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1127                    "with more than %d channels, but extended_data is not set.\n",
1128                    AV_NUM_DATA_POINTERS);
1129             return AVERROR(EINVAL);
1130         }
1131         av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1132
1133         tmp = *frame;
1134         tmp.extended_data = tmp.data;
1135         frame = &tmp;
1136     }
1137
1138     /* check for valid frame size */
1139     if (frame) {
1140         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1141             if (frame->nb_samples > avctx->frame_size) {
1142                 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1143                 return AVERROR(EINVAL);
1144             }
1145         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1146             if (frame->nb_samples < avctx->frame_size &&
1147                 !avctx->internal->last_audio_frame) {
1148                 ret = pad_last_frame(avctx, &padded_frame, frame);
1149                 if (ret < 0)
1150                     return ret;
1151
1152                 frame = padded_frame;
1153                 avctx->internal->last_audio_frame = 1;
1154             }
1155
1156             if (frame->nb_samples != avctx->frame_size) {
1157                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1158                 return AVERROR(EINVAL);
1159             }
1160         }
1161     }
1162
1163     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1164     if (!ret) {
1165         if (*got_packet_ptr) {
1166             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1167                 if (avpkt->pts == AV_NOPTS_VALUE)
1168                     avpkt->pts = frame->pts;
1169                 if (!avpkt->duration)
1170                     avpkt->duration = ff_samples_to_time_base(avctx,
1171                                                               frame->nb_samples);
1172             }
1173             avpkt->dts = avpkt->pts;
1174         } else {
1175             avpkt->size = 0;
1176         }
1177     }
1178     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1179         needs_realloc = 0;
1180         if (user_pkt.data) {
1181             if (user_pkt.size >= avpkt->size) {
1182                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1183             } else {
1184                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1185                 avpkt->size = user_pkt.size;
1186                 ret = -1;
1187             }
1188             avpkt->data     = user_pkt.data;
1189             avpkt->destruct = user_pkt.destruct;
1190         } else {
1191             if (av_dup_packet(avpkt) < 0) {
1192                 ret = AVERROR(ENOMEM);
1193             }
1194         }
1195     }
1196
1197     if (!ret) {
1198         if (needs_realloc && avpkt->data) {
1199             uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1200             if (new_data)
1201                 avpkt->data = new_data;
1202         }
1203
1204         avctx->frame_number++;
1205     }
1206
1207     if (ret < 0 || !*got_packet_ptr) {
1208         av_free_packet(avpkt);
1209         av_init_packet(avpkt);
1210         return ret;
1211     }
1212
1213     /* NOTE: if we add any audio encoders which output non-keyframe packets,
1214              this needs to be moved to the encoders, but for now we can do it
1215              here to simplify things */
1216     avpkt->flags |= AV_PKT_FLAG_KEY;
1217
1218     if (padded_frame) {
1219         av_freep(&padded_frame->data[0]);
1220         if (padded_frame->extended_data != padded_frame->data)
1221             av_freep(&padded_frame->extended_data);
1222         av_freep(&padded_frame);
1223     }
1224
1225     return ret;
1226 }
1227
1228 #if FF_API_OLD_DECODE_AUDIO
1229 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1230                                              uint8_t *buf, int buf_size,
1231                                              const short *samples)
1232 {
1233     AVPacket pkt;
1234     AVFrame frame0;
1235     AVFrame *frame;
1236     int ret, samples_size, got_packet;
1237
1238     av_init_packet(&pkt);
1239     pkt.data = buf;
1240     pkt.size = buf_size;
1241
1242     if (samples) {
1243         frame = &frame0;
1244         avcodec_get_frame_defaults(frame);
1245
1246         if (avctx->frame_size) {
1247             frame->nb_samples = avctx->frame_size;
1248         } else {
1249             /* if frame_size is not set, the number of samples must be
1250                calculated from the buffer size */
1251             int64_t nb_samples;
1252             if (!av_get_bits_per_sample(avctx->codec_id)) {
1253                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1254                        "support this codec\n");
1255                 return AVERROR(EINVAL);
1256             }
1257             nb_samples = (int64_t)buf_size * 8 /
1258                          (av_get_bits_per_sample(avctx->codec_id) *
1259                          avctx->channels);
1260             if (nb_samples >= INT_MAX)
1261                 return AVERROR(EINVAL);
1262             frame->nb_samples = nb_samples;
1263         }
1264
1265         /* it is assumed that the samples buffer is large enough based on the
1266            relevant parameters */
1267         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1268                                                   frame->nb_samples,
1269                                                   avctx->sample_fmt, 1);
1270         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1271                                             avctx->sample_fmt,
1272                                             (const uint8_t *) samples,
1273                                             samples_size, 1)))
1274             return ret;
1275
1276         /* fabricate frame pts from sample count.
1277            this is needed because the avcodec_encode_audio() API does not have
1278            a way for the user to provide pts */
1279         if(avctx->sample_rate && avctx->time_base.num)
1280             frame->pts = ff_samples_to_time_base(avctx,
1281                                                 avctx->internal->sample_count);
1282         else
1283             frame->pts = AV_NOPTS_VALUE;
1284         avctx->internal->sample_count += frame->nb_samples;
1285     } else {
1286         frame = NULL;
1287     }
1288
1289     got_packet = 0;
1290     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1291     if (!ret && got_packet && avctx->coded_frame) {
1292         avctx->coded_frame->pts       = pkt.pts;
1293         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1294     }
1295     /* free any side data since we cannot return it */
1296     ff_packet_free_side_data(&pkt);
1297
1298     if (frame && frame->extended_data != frame->data)
1299         av_freep(&frame->extended_data);
1300
1301     return ret ? ret : pkt.size;
1302 }
1303 #endif
1304
1305 #if FF_API_OLD_ENCODE_VIDEO
1306 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1307                          const AVFrame *pict)
1308 {
1309     AVPacket pkt;
1310     int ret, got_packet = 0;
1311
1312     if(buf_size < FF_MIN_BUFFER_SIZE){
1313         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1314         return -1;
1315     }
1316
1317     av_init_packet(&pkt);
1318     pkt.data = buf;
1319     pkt.size = buf_size;
1320
1321     ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1322     if (!ret && got_packet && avctx->coded_frame) {
1323         avctx->coded_frame->pts       = pkt.pts;
1324         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1325     }
1326
1327     /* free any side data since we cannot return it */
1328     if (pkt.side_data_elems > 0) {
1329         int i;
1330         for (i = 0; i < pkt.side_data_elems; i++)
1331             av_free(pkt.side_data[i].data);
1332         av_freep(&pkt.side_data);
1333         pkt.side_data_elems = 0;
1334     }
1335
1336     return ret ? ret : pkt.size;
1337 }
1338 #endif
1339
1340 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1341                                               AVPacket *avpkt,
1342                                               const AVFrame *frame,
1343                                               int *got_packet_ptr)
1344 {
1345     int ret;
1346     AVPacket user_pkt = *avpkt;
1347     int needs_realloc = !user_pkt.data;
1348
1349     *got_packet_ptr = 0;
1350
1351     if(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1352         return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1353
1354     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1355         av_free_packet(avpkt);
1356         av_init_packet(avpkt);
1357         avpkt->size     = 0;
1358         return 0;
1359     }
1360
1361     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1362         return AVERROR(EINVAL);
1363
1364     av_assert0(avctx->codec->encode2);
1365
1366     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1367     av_assert0(ret <= 0);
1368
1369     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1370         needs_realloc = 0;
1371         if (user_pkt.data) {
1372             if (user_pkt.size >= avpkt->size) {
1373                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1374             } else {
1375                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1376                 avpkt->size = user_pkt.size;
1377                 ret = -1;
1378             }
1379             avpkt->data     = user_pkt.data;
1380             avpkt->destruct = user_pkt.destruct;
1381         } else {
1382             if (av_dup_packet(avpkt) < 0) {
1383                 ret = AVERROR(ENOMEM);
1384             }
1385         }
1386     }
1387
1388     if (!ret) {
1389         if (!*got_packet_ptr)
1390             avpkt->size = 0;
1391         else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1392             avpkt->pts = avpkt->dts = frame->pts;
1393
1394         if (needs_realloc && avpkt->data &&
1395             avpkt->destruct == av_destruct_packet) {
1396             uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1397             if (new_data)
1398                 avpkt->data = new_data;
1399         }
1400
1401         avctx->frame_number++;
1402     }
1403
1404     if (ret < 0 || !*got_packet_ptr)
1405         av_free_packet(avpkt);
1406
1407     emms_c();
1408     return ret;
1409 }
1410
1411 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1412                             const AVSubtitle *sub)
1413 {
1414     int ret;
1415     if(sub->start_display_time) {
1416         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1417         return -1;
1418     }
1419
1420     ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
1421     avctx->frame_number++;
1422     return ret;
1423 }
1424
1425 /**
1426  * Attempt to guess proper monotonic timestamps for decoded video frames
1427  * which might have incorrect times. Input timestamps may wrap around, in
1428  * which case the output will as well.
1429  *
1430  * @param pts the pts field of the decoded AVPacket, as passed through
1431  * AVFrame.pkt_pts
1432  * @param dts the dts field of the decoded AVPacket
1433  * @return one of the input values, may be AV_NOPTS_VALUE
1434  */
1435 static int64_t guess_correct_pts(AVCodecContext *ctx,
1436                                  int64_t reordered_pts, int64_t dts)
1437 {
1438     int64_t pts = AV_NOPTS_VALUE;
1439
1440     if (dts != AV_NOPTS_VALUE) {
1441         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1442         ctx->pts_correction_last_dts = dts;
1443     }
1444     if (reordered_pts != AV_NOPTS_VALUE) {
1445         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1446         ctx->pts_correction_last_pts = reordered_pts;
1447     }
1448     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1449        && reordered_pts != AV_NOPTS_VALUE)
1450         pts = reordered_pts;
1451     else
1452         pts = dts;
1453
1454     return pts;
1455 }
1456
1457 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1458 {
1459     int size = 0;
1460     const uint8_t *data;
1461     uint32_t flags;
1462
1463     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1464         return;
1465
1466     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1467     if (!data || size < 4)
1468         return;
1469     flags = bytestream_get_le32(&data);
1470     size -= 4;
1471     if (size < 4) /* Required for any of the changes */
1472         return;
1473     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1474         avctx->channels = bytestream_get_le32(&data);
1475         size -= 4;
1476     }
1477     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1478         if (size < 8)
1479             return;
1480         avctx->channel_layout = bytestream_get_le64(&data);
1481         size -= 8;
1482     }
1483     if (size < 4)
1484         return;
1485     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1486         avctx->sample_rate = bytestream_get_le32(&data);
1487         size -= 4;
1488     }
1489     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1490         if (size < 8)
1491             return;
1492         avctx->width  = bytestream_get_le32(&data);
1493         avctx->height = bytestream_get_le32(&data);
1494         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1495         size -= 8;
1496     }
1497 }
1498
1499 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1500                          int *got_picture_ptr,
1501                          const AVPacket *avpkt)
1502 {
1503     int ret;
1504     // copy to ensure we do not change avpkt
1505     AVPacket tmp = *avpkt;
1506
1507     if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1508         av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1509         return AVERROR(EINVAL);
1510     }
1511
1512     *got_picture_ptr= 0;
1513     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1514         return AVERROR(EINVAL);
1515
1516     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1517         int did_split = av_packet_split_side_data(&tmp);
1518         apply_param_change(avctx, &tmp);
1519         avctx->pkt = &tmp;
1520         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1521              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1522                                           &tmp);
1523         else {
1524             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1525                               &tmp);
1526             picture->pkt_dts= avpkt->dts;
1527
1528             if(!avctx->has_b_frames){
1529             picture->pkt_pos= avpkt->pos;
1530             }
1531             //FIXME these should be under if(!avctx->has_b_frames)
1532             if (!picture->sample_aspect_ratio.num)
1533                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1534             if (!picture->width)
1535                 picture->width = avctx->width;
1536             if (!picture->height)
1537                 picture->height = avctx->height;
1538             if (picture->format == PIX_FMT_NONE)
1539                 picture->format = avctx->pix_fmt;
1540         }
1541
1542         emms_c(); //needed to avoid an emms_c() call before every return;
1543
1544         avctx->pkt = NULL;
1545         if (did_split) {
1546             ff_packet_free_side_data(&tmp);
1547             if(ret == tmp.size)
1548                 ret = avpkt->size;
1549         }
1550
1551         if (*got_picture_ptr){
1552             avctx->frame_number++;
1553             picture->best_effort_timestamp = guess_correct_pts(avctx,
1554                                                             picture->pkt_pts,
1555                                                             picture->pkt_dts);
1556         }
1557     }else
1558         ret= 0;
1559
1560     return ret;
1561 }
1562
1563 #if FF_API_OLD_DECODE_AUDIO
1564 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1565                          int *frame_size_ptr,
1566                          AVPacket *avpkt)
1567 {
1568     AVFrame frame;
1569     int ret, got_frame = 0;
1570
1571     if (avctx->get_buffer != avcodec_default_get_buffer) {
1572         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1573                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1574         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1575                "avcodec_decode_audio4()\n");
1576         avctx->get_buffer = avcodec_default_get_buffer;
1577         avctx->release_buffer = avcodec_default_release_buffer;
1578     }
1579
1580     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1581
1582     if (ret >= 0 && got_frame) {
1583         int ch, plane_size;
1584         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1585         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1586                                                    frame.nb_samples,
1587                                                    avctx->sample_fmt, 1);
1588         if (*frame_size_ptr < data_size) {
1589             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1590                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1591             return AVERROR(EINVAL);
1592         }
1593
1594         memcpy(samples, frame.extended_data[0], plane_size);
1595
1596         if (planar && avctx->channels > 1) {
1597             uint8_t *out = ((uint8_t *)samples) + plane_size;
1598             for (ch = 1; ch < avctx->channels; ch++) {
1599                 memcpy(out, frame.extended_data[ch], plane_size);
1600                 out += plane_size;
1601             }
1602         }
1603         *frame_size_ptr = data_size;
1604     } else {
1605         *frame_size_ptr = 0;
1606     }
1607     return ret;
1608 }
1609 #endif
1610
1611 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1612                                               AVFrame *frame,
1613                                               int *got_frame_ptr,
1614                                               const AVPacket *avpkt)
1615 {
1616     int ret = 0;
1617
1618     *got_frame_ptr = 0;
1619
1620     if (!avpkt->data && avpkt->size) {
1621         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1622         return AVERROR(EINVAL);
1623     }
1624     if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1625         av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1626         return AVERROR(EINVAL);
1627     }
1628
1629     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1630         uint8_t *side;
1631         int side_size;
1632         // copy to ensure we do not change avpkt
1633         AVPacket tmp = *avpkt;
1634         int did_split = av_packet_split_side_data(&tmp);
1635         apply_param_change(avctx, &tmp);
1636
1637         avctx->pkt = &tmp;
1638         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1639         if (ret >= 0 && *got_frame_ptr) {
1640             avctx->frame_number++;
1641             frame->pkt_dts = avpkt->dts;
1642             if (frame->format == AV_SAMPLE_FMT_NONE)
1643                 frame->format = avctx->sample_fmt;
1644             if (!frame->channel_layout)
1645                 frame->channel_layout = avctx->channel_layout;
1646             if (!frame->sample_rate)
1647                 frame->sample_rate = avctx->sample_rate;
1648         }
1649
1650         side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1651         if(side && side_size>=10) {
1652             avctx->internal->skip_samples = AV_RL32(side);
1653         }
1654         if (avctx->internal->skip_samples) {
1655             if(frame->nb_samples <= avctx->internal->skip_samples){
1656                 *got_frame_ptr = 0;
1657                 avctx->internal->skip_samples -= frame->nb_samples;
1658             } else {
1659                 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1660                                 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1661                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1662                     if(frame->pkt_pts!=AV_NOPTS_VALUE)
1663                         frame->pkt_pts += av_rescale_q(avctx->internal->skip_samples,(AVRational){1, avctx->sample_rate}, avctx->pkt_timebase);
1664                     if(frame->pkt_dts!=AV_NOPTS_VALUE)
1665                         frame->pkt_dts += av_rescale_q(avctx->internal->skip_samples,(AVRational){1, avctx->sample_rate}, avctx->pkt_timebase);
1666                 }
1667                 frame->nb_samples -= avctx->internal->skip_samples;
1668                 avctx->internal->skip_samples = 0;
1669             }
1670         }
1671
1672         avctx->pkt = NULL;
1673         if (did_split) {
1674             ff_packet_free_side_data(&tmp);
1675             if(ret == tmp.size)
1676                 ret = avpkt->size;
1677         }
1678     }
1679     return ret;
1680 }
1681
1682 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1683                             int *got_sub_ptr,
1684                             AVPacket *avpkt)
1685 {
1686     int ret;
1687
1688     if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1689         av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1690         return AVERROR(EINVAL);
1691     }
1692
1693     avctx->pkt = avpkt;
1694     *got_sub_ptr = 0;
1695     avcodec_get_subtitle_defaults(sub);
1696     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1697     if (*got_sub_ptr)
1698         avctx->frame_number++;
1699     return ret;
1700 }
1701
1702 void avsubtitle_free(AVSubtitle *sub)
1703 {
1704     int i;
1705
1706     for (i = 0; i < sub->num_rects; i++)
1707     {
1708         av_freep(&sub->rects[i]->pict.data[0]);
1709         av_freep(&sub->rects[i]->pict.data[1]);
1710         av_freep(&sub->rects[i]->pict.data[2]);
1711         av_freep(&sub->rects[i]->pict.data[3]);
1712         av_freep(&sub->rects[i]->text);
1713         av_freep(&sub->rects[i]->ass);
1714         av_freep(&sub->rects[i]);
1715     }
1716
1717     av_freep(&sub->rects);
1718
1719     memset(sub, 0, sizeof(AVSubtitle));
1720 }
1721
1722 av_cold int avcodec_close(AVCodecContext *avctx)
1723 {
1724     /* If there is a user-supplied mutex locking routine, call it. */
1725     if (ff_lockmgr_cb) {
1726         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1727             return -1;
1728     }
1729
1730     entangled_thread_counter++;
1731     if(entangled_thread_counter != 1){
1732         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1733         entangled_thread_counter--;
1734         return -1;
1735     }
1736
1737     if (avcodec_is_open(avctx)) {
1738         if (avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1739             entangled_thread_counter --;
1740             ff_frame_thread_encoder_free(avctx);
1741             entangled_thread_counter ++;
1742         }
1743         if (HAVE_THREADS && avctx->thread_opaque)
1744             ff_thread_free(avctx);
1745         if (avctx->codec && avctx->codec->close)
1746             avctx->codec->close(avctx);
1747         avcodec_default_free_buffers(avctx);
1748         avctx->coded_frame = NULL;
1749         avctx->internal->byte_buffer_size = 0;
1750         av_freep(&avctx->internal->byte_buffer);
1751         av_freep(&avctx->internal);
1752     }
1753
1754     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1755         av_opt_free(avctx->priv_data);
1756     av_opt_free(avctx);
1757     av_freep(&avctx->priv_data);
1758     if (av_codec_is_encoder(avctx->codec))
1759         av_freep(&avctx->extradata);
1760     avctx->codec = NULL;
1761     avctx->active_thread_type = 0;
1762     entangled_thread_counter--;
1763
1764     /* Release any user-supplied mutex. */
1765     if (ff_lockmgr_cb) {
1766         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1767     }
1768     return 0;
1769 }
1770
1771 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
1772 {
1773     switch(id){
1774         //This is for future deprecatec codec ids, its empty since
1775         //last major bump but will fill up again over time, please don't remove it
1776 //         case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
1777         default                         : return id;
1778     }
1779 }
1780
1781 AVCodec *avcodec_find_encoder(enum CodecID id)
1782 {
1783     AVCodec *p, *experimental=NULL;
1784     p = first_avcodec;
1785     id= remap_deprecated_codec_id(id);
1786     while (p) {
1787         if (av_codec_is_encoder(p) && p->id == id) {
1788             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1789                 experimental = p;
1790             } else
1791                 return p;
1792         }
1793         p = p->next;
1794     }
1795     return experimental;
1796 }
1797
1798 AVCodec *avcodec_find_encoder_by_name(const char *name)
1799 {
1800     AVCodec *p;
1801     if (!name)
1802         return NULL;
1803     p = first_avcodec;
1804     while (p) {
1805         if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1806             return p;
1807         p = p->next;
1808     }
1809     return NULL;
1810 }
1811
1812 AVCodec *avcodec_find_decoder(enum CodecID id)
1813 {
1814     AVCodec *p, *experimental=NULL;
1815     p = first_avcodec;
1816     id= remap_deprecated_codec_id(id);
1817     while (p) {
1818         if (av_codec_is_decoder(p) && p->id == id) {
1819             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1820                 experimental = p;
1821             } else
1822                 return p;
1823         }
1824         p = p->next;
1825     }
1826     return experimental;
1827 }
1828
1829 AVCodec *avcodec_find_decoder_by_name(const char *name)
1830 {
1831     AVCodec *p;
1832     if (!name)
1833         return NULL;
1834     p = first_avcodec;
1835     while (p) {
1836         if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1837             return p;
1838         p = p->next;
1839     }
1840     return NULL;
1841 }
1842
1843 const char *avcodec_get_name(enum CodecID id)
1844 {
1845     AVCodec *codec;
1846
1847 #if !CONFIG_SMALL
1848     switch (id) {
1849 #include "libavcodec/codec_names.h"
1850     }
1851     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1852 #endif
1853     codec = avcodec_find_decoder(id);
1854     if (codec)
1855         return codec->name;
1856     codec = avcodec_find_encoder(id);
1857     if (codec)
1858         return codec->name;
1859     return "unknown_codec";
1860 }
1861
1862 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1863 {
1864     int i, len, ret = 0;
1865
1866 #define IS_PRINT(x)                                               \
1867     (((x) >= '0' && (x) <= '9') ||                                \
1868      ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
1869      ((x) == '.' || (x) == ' ' || (x) == '-'))
1870
1871     for (i = 0; i < 4; i++) {
1872         len = snprintf(buf, buf_size,
1873                        IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1874         buf      += len;
1875         buf_size  = buf_size > len ? buf_size - len : 0;
1876         ret      += len;
1877         codec_tag>>=8;
1878     }
1879     return ret;
1880 }
1881
1882 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1883 {
1884     const char *codec_type;
1885     const char *codec_name;
1886     const char *profile = NULL;
1887     AVCodec *p;
1888     int bitrate;
1889     AVRational display_aspect_ratio;
1890
1891     if (!buf || buf_size <= 0)
1892         return;
1893     codec_type = av_get_media_type_string(enc->codec_type);
1894     codec_name = avcodec_get_name(enc->codec_id);
1895     if (enc->profile != FF_PROFILE_UNKNOWN) {
1896         if (enc->codec)
1897             p = enc->codec;
1898         else
1899             p = encode ? avcodec_find_encoder(enc->codec_id) :
1900                         avcodec_find_decoder(enc->codec_id);
1901         if (p)
1902             profile = av_get_profile_name(p, enc->profile);
1903     }
1904
1905     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1906              codec_name, enc->mb_decision ? " (hq)" : "");
1907     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1908     if (profile)
1909         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1910     if (enc->codec_tag) {
1911         char tag_buf[32];
1912         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1913         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1914                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1915     }
1916     switch(enc->codec_type) {
1917     case AVMEDIA_TYPE_VIDEO:
1918         if (enc->pix_fmt != PIX_FMT_NONE) {
1919             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1920                      ", %s",
1921                      av_get_pix_fmt_name(enc->pix_fmt));
1922         }
1923         if (enc->width) {
1924             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1925                      ", %dx%d",
1926                      enc->width, enc->height);
1927             if (enc->sample_aspect_ratio.num) {
1928                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1929                           enc->width*enc->sample_aspect_ratio.num,
1930                           enc->height*enc->sample_aspect_ratio.den,
1931                           1024*1024);
1932                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1933                          " [SAR %d:%d DAR %d:%d]",
1934                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1935                          display_aspect_ratio.num, display_aspect_ratio.den);
1936             }
1937             if(av_log_get_level() >= AV_LOG_DEBUG){
1938                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1939                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1940                      ", %d/%d",
1941                      enc->time_base.num/g, enc->time_base.den/g);
1942             }
1943         }
1944         if (encode) {
1945             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1946                      ", q=%d-%d", enc->qmin, enc->qmax);
1947         }
1948         break;
1949     case AVMEDIA_TYPE_AUDIO:
1950         if (enc->sample_rate) {
1951             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1952                      ", %d Hz", enc->sample_rate);
1953         }
1954         av_strlcat(buf, ", ", buf_size);
1955         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1956         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1957             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1958                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1959         }
1960         break;
1961     default:
1962         return;
1963     }
1964     if (encode) {
1965         if (enc->flags & CODEC_FLAG_PASS1)
1966             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1967                      ", pass 1");
1968         if (enc->flags & CODEC_FLAG_PASS2)
1969             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1970                      ", pass 2");
1971     }
1972     bitrate = get_bit_rate(enc);
1973     if (bitrate != 0) {
1974         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1975                  ", %d kb/s", bitrate / 1000);
1976     }
1977 }
1978
1979 const char *av_get_profile_name(const AVCodec *codec, int profile)
1980 {
1981     const AVProfile *p;
1982     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1983         return NULL;
1984
1985     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1986         if (p->profile == profile)
1987             return p->name;
1988
1989     return NULL;
1990 }
1991
1992 unsigned avcodec_version( void )
1993 {
1994 //    av_assert0(CODEC_ID_V410==164);
1995     av_assert0(CODEC_ID_PCM_S8_PLANAR==65563);
1996     av_assert0(CODEC_ID_ADPCM_G722==69660);
1997 //     av_assert0(CODEC_ID_BMV_AUDIO==86071);
1998     av_assert0(CODEC_ID_SRT==94216);
1999     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2000
2001   return LIBAVCODEC_VERSION_INT;
2002 }
2003
2004 const char *avcodec_configuration(void)
2005 {
2006     return FFMPEG_CONFIGURATION;
2007 }
2008
2009 const char *avcodec_license(void)
2010 {
2011 #define LICENSE_PREFIX "libavcodec license: "
2012     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2013 }
2014
2015 void avcodec_flush_buffers(AVCodecContext *avctx)
2016 {
2017     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
2018         ff_thread_flush(avctx);
2019     else if(avctx->codec->flush)
2020         avctx->codec->flush(avctx);
2021
2022     avctx->pts_correction_last_pts =
2023     avctx->pts_correction_last_dts = INT64_MIN;
2024 }
2025
2026 static void video_free_buffers(AVCodecContext *s)
2027 {
2028     AVCodecInternal *avci = s->internal;
2029     int i, j;
2030
2031     if (!avci->buffer)
2032         return;
2033
2034     if (avci->buffer_count)
2035         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2036                avci->buffer_count);
2037     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
2038         InternalBuffer *buf = &avci->buffer[i];
2039         for(j=0; j<4; j++){
2040             av_freep(&buf->base[j]);
2041             buf->data[j]= NULL;
2042         }
2043     }
2044     av_freep(&avci->buffer);
2045
2046     avci->buffer_count=0;
2047 }
2048
2049 static void audio_free_buffers(AVCodecContext *avctx)
2050 {
2051     AVCodecInternal *avci = avctx->internal;
2052     InternalBuffer *buf;
2053
2054     if (!avci->buffer)
2055         return;
2056     buf = avci->buffer;
2057
2058     if (buf->extended_data) {
2059         av_free(buf->extended_data[0]);
2060         if (buf->extended_data != buf->data)
2061             av_freep(&buf->extended_data);
2062     }
2063     av_freep(&avci->buffer);
2064 }
2065
2066 void avcodec_default_free_buffers(AVCodecContext *avctx)
2067 {
2068     switch (avctx->codec_type) {
2069     case AVMEDIA_TYPE_VIDEO:
2070         video_free_buffers(avctx);
2071         break;
2072     case AVMEDIA_TYPE_AUDIO:
2073         audio_free_buffers(avctx);
2074         break;
2075     default:
2076         break;
2077     }
2078 }
2079
2080 int av_get_exact_bits_per_sample(enum CodecID codec_id)
2081 {
2082     switch(codec_id){
2083     case CODEC_ID_ADPCM_CT:
2084     case CODEC_ID_ADPCM_IMA_APC:
2085     case CODEC_ID_ADPCM_IMA_EA_SEAD:
2086     case CODEC_ID_ADPCM_IMA_WS:
2087     case CODEC_ID_ADPCM_G722:
2088     case CODEC_ID_ADPCM_YAMAHA:
2089         return 4;
2090     case CODEC_ID_PCM_ALAW:
2091     case CODEC_ID_PCM_MULAW:
2092     case CODEC_ID_PCM_S8:
2093     case CODEC_ID_PCM_U8:
2094     case CODEC_ID_PCM_ZORK:
2095         return 8;
2096     case CODEC_ID_PCM_S16BE:
2097     case CODEC_ID_PCM_S16LE:
2098     case CODEC_ID_PCM_S16LE_PLANAR:
2099     case CODEC_ID_PCM_U16BE:
2100     case CODEC_ID_PCM_U16LE:
2101         return 16;
2102     case CODEC_ID_PCM_S24DAUD:
2103     case CODEC_ID_PCM_S24BE:
2104     case CODEC_ID_PCM_S24LE:
2105     case CODEC_ID_PCM_U24BE:
2106     case CODEC_ID_PCM_U24LE:
2107         return 24;
2108     case CODEC_ID_PCM_S32BE:
2109     case CODEC_ID_PCM_S32LE:
2110     case CODEC_ID_PCM_U32BE:
2111     case CODEC_ID_PCM_U32LE:
2112     case CODEC_ID_PCM_F32BE:
2113     case CODEC_ID_PCM_F32LE:
2114         return 32;
2115     case CODEC_ID_PCM_F64BE:
2116     case CODEC_ID_PCM_F64LE:
2117         return 64;
2118     default:
2119         return 0;
2120     }
2121 }
2122
2123 enum CodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2124 {
2125     static const enum CodecID map[AV_SAMPLE_FMT_NB][2] = {
2126         [AV_SAMPLE_FMT_U8  ] = { CODEC_ID_PCM_U8,    CODEC_ID_PCM_U8    },
2127         [AV_SAMPLE_FMT_S16 ] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
2128         [AV_SAMPLE_FMT_S32 ] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
2129         [AV_SAMPLE_FMT_FLT ] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
2130         [AV_SAMPLE_FMT_DBL ] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
2131         [AV_SAMPLE_FMT_U8P ] = { CODEC_ID_PCM_U8,    CODEC_ID_PCM_U8    },
2132         [AV_SAMPLE_FMT_S16P] = { CODEC_ID_PCM_S16LE, CODEC_ID_PCM_S16BE },
2133         [AV_SAMPLE_FMT_S32P] = { CODEC_ID_PCM_S32LE, CODEC_ID_PCM_S32BE },
2134         [AV_SAMPLE_FMT_FLTP] = { CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F32BE },
2135         [AV_SAMPLE_FMT_DBLP] = { CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F64BE },
2136     };
2137     if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2138         return CODEC_ID_NONE;
2139     if (be < 0 || be > 1)
2140         be = AV_NE(1, 0);
2141     return map[fmt][be];
2142 }
2143
2144 int av_get_bits_per_sample(enum CodecID codec_id)
2145 {
2146     switch (codec_id) {
2147     case CODEC_ID_ADPCM_SBPRO_2:
2148         return 2;
2149     case CODEC_ID_ADPCM_SBPRO_3:
2150         return 3;
2151     case CODEC_ID_ADPCM_SBPRO_4:
2152     case CODEC_ID_ADPCM_IMA_WAV:
2153     case CODEC_ID_ADPCM_IMA_QT:
2154     case CODEC_ID_ADPCM_SWF:
2155     case CODEC_ID_ADPCM_MS:
2156         return 4;
2157     default:
2158         return av_get_exact_bits_per_sample(codec_id);
2159     }
2160 }
2161
2162 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2163 {
2164     int id, sr, ch, ba, tag, bps;
2165
2166     id  = avctx->codec_id;
2167     sr  = avctx->sample_rate;
2168     ch  = avctx->channels;
2169     ba  = avctx->block_align;
2170     tag = avctx->codec_tag;
2171     bps = av_get_exact_bits_per_sample(avctx->codec_id);
2172
2173     /* codecs with an exact constant bits per sample */
2174     if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2175         return (frame_bytes * 8LL) / (bps * ch);
2176     bps = avctx->bits_per_coded_sample;
2177
2178     /* codecs with a fixed packet duration */
2179     switch (id) {
2180     case CODEC_ID_ADPCM_ADX:    return   32;
2181     case CODEC_ID_ADPCM_IMA_QT: return   64;
2182     case CODEC_ID_ADPCM_EA_XAS: return  128;
2183     case CODEC_ID_AMR_NB:
2184     case CODEC_ID_GSM:
2185     case CODEC_ID_QCELP:
2186     case CODEC_ID_RA_144:
2187     case CODEC_ID_RA_288:       return  160;
2188     case CODEC_ID_IMC:          return  256;
2189     case CODEC_ID_AMR_WB:
2190     case CODEC_ID_GSM_MS:       return  320;
2191     case CODEC_ID_MP1:          return  384;
2192     case CODEC_ID_ATRAC1:       return  512;
2193     case CODEC_ID_ATRAC3:       return 1024;
2194     case CODEC_ID_MP2:
2195     case CODEC_ID_MUSEPACK7:    return 1152;
2196     case CODEC_ID_AC3:          return 1536;
2197     }
2198
2199     if (sr > 0) {
2200         /* calc from sample rate */
2201         if (id == CODEC_ID_TTA)
2202             return 256 * sr / 245;
2203
2204         if (ch > 0) {
2205             /* calc from sample rate and channels */
2206             if (id == CODEC_ID_BINKAUDIO_DCT)
2207                 return (480 << (sr / 22050)) / ch;
2208         }
2209     }
2210
2211     if (ba > 0) {
2212         /* calc from block_align */
2213         if (id == CODEC_ID_SIPR) {
2214             switch (ba) {
2215             case 20: return 160;
2216             case 19: return 144;
2217             case 29: return 288;
2218             case 37: return 480;
2219             }
2220         } else if (id == CODEC_ID_ILBC) {
2221             switch (ba) {
2222             case 38: return 160;
2223             case 50: return 240;
2224             }
2225         }
2226     }
2227
2228     if (frame_bytes > 0) {
2229         /* calc from frame_bytes only */
2230         if (id == CODEC_ID_TRUESPEECH)
2231             return 240 * (frame_bytes / 32);
2232         if (id == CODEC_ID_NELLYMOSER)
2233             return 256 * (frame_bytes / 64);
2234
2235         if (bps > 0) {
2236             /* calc from frame_bytes and bits_per_coded_sample */
2237             if (id == CODEC_ID_ADPCM_G726)
2238                 return frame_bytes * 8 / bps;
2239         }
2240
2241         if (ch > 0) {
2242             /* calc from frame_bytes and channels */
2243             switch (id) {
2244             case CODEC_ID_ADPCM_4XM:
2245             case CODEC_ID_ADPCM_IMA_ISS:
2246                 return (frame_bytes - 4 * ch) * 2 / ch;
2247             case CODEC_ID_ADPCM_IMA_SMJPEG:
2248                 return (frame_bytes - 4) * 2 / ch;
2249             case CODEC_ID_ADPCM_IMA_AMV:
2250                 return (frame_bytes - 8) * 2 / ch;
2251             case CODEC_ID_ADPCM_XA:
2252                 return (frame_bytes / 128) * 224 / ch;
2253             case CODEC_ID_INTERPLAY_DPCM:
2254                 return (frame_bytes - 6 - ch) / ch;
2255             case CODEC_ID_ROQ_DPCM:
2256                 return (frame_bytes - 8) / ch;
2257             case CODEC_ID_XAN_DPCM:
2258                 return (frame_bytes - 2 * ch) / ch;
2259             case CODEC_ID_MACE3:
2260                 return 3 * frame_bytes / ch;
2261             case CODEC_ID_MACE6:
2262                 return 6 * frame_bytes / ch;
2263             case CODEC_ID_PCM_LXF:
2264                 return 2 * (frame_bytes / (5 * ch));
2265             }
2266
2267             if (tag) {
2268                 /* calc from frame_bytes, channels, and codec_tag */
2269                 if (id == CODEC_ID_SOL_DPCM) {
2270                     if (tag == 3)
2271                         return frame_bytes / ch;
2272                     else
2273                         return frame_bytes * 2 / ch;
2274                 }
2275             }
2276
2277             if (ba > 0) {
2278                 /* calc from frame_bytes, channels, and block_align */
2279                 int blocks = frame_bytes / ba;
2280                 switch (avctx->codec_id) {
2281                 case CODEC_ID_ADPCM_IMA_WAV:
2282                     return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2283                 case CODEC_ID_ADPCM_IMA_DK3:
2284                     return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2285                 case CODEC_ID_ADPCM_IMA_DK4:
2286                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2287                 case CODEC_ID_ADPCM_MS:
2288                     return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2289                 }
2290             }
2291
2292             if (bps > 0) {
2293                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2294                 switch (avctx->codec_id) {
2295                 case CODEC_ID_PCM_DVD:
2296                     if(bps<4)
2297                         return 0;
2298                     return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2299                 case CODEC_ID_PCM_BLURAY:
2300                     if(bps<4)
2301                         return 0;
2302                     return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2303                 case CODEC_ID_S302M:
2304                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2305                 }
2306             }
2307         }
2308     }
2309
2310     return 0;
2311 }
2312
2313 #if !HAVE_THREADS
2314 int ff_thread_init(AVCodecContext *s){
2315     return -1;
2316 }
2317 #endif
2318
2319 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2320 {
2321     unsigned int n = 0;
2322
2323     while(v >= 0xff) {
2324         *s++ = 0xff;
2325         v -= 0xff;
2326         n++;
2327     }
2328     *s = v;
2329     n++;
2330     return n;
2331 }
2332
2333 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
2334     int i;
2335     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
2336     return i;
2337 }
2338
2339 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2340 {
2341     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2342             "version to the newest one from Git. If the problem still "
2343             "occurs, it means that your file has a feature which has not "
2344             "been implemented.\n", feature);
2345     if(want_sample)
2346         av_log_ask_for_sample(avc, NULL);
2347 }
2348
2349 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2350 {
2351     va_list argument_list;
2352
2353     va_start(argument_list, msg);
2354
2355     if (msg)
2356         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2357     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2358             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2359             "and contact the ffmpeg-devel mailing list.\n");
2360
2361     va_end(argument_list);
2362 }
2363
2364 static AVHWAccel *first_hwaccel = NULL;
2365
2366 void av_register_hwaccel(AVHWAccel *hwaccel)
2367 {
2368     AVHWAccel **p = &first_hwaccel;
2369     while (*p)
2370         p = &(*p)->next;
2371     *p = hwaccel;
2372     hwaccel->next = NULL;
2373 }
2374
2375 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2376 {
2377     return hwaccel ? hwaccel->next : first_hwaccel;
2378 }
2379
2380 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
2381 {
2382     AVHWAccel *hwaccel=NULL;
2383
2384     while((hwaccel= av_hwaccel_next(hwaccel))){
2385         if (   hwaccel->id      == codec_id
2386             && hwaccel->pix_fmt == pix_fmt)
2387             return hwaccel;
2388     }
2389     return NULL;
2390 }
2391
2392 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2393 {
2394     if (ff_lockmgr_cb) {
2395         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2396             return -1;
2397         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2398             return -1;
2399     }
2400
2401     ff_lockmgr_cb = cb;
2402
2403     if (ff_lockmgr_cb) {
2404         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2405             return -1;
2406         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2407             return -1;
2408     }
2409     return 0;
2410 }
2411
2412 int avpriv_lock_avformat(void)
2413 {
2414     if (ff_lockmgr_cb) {
2415         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2416             return -1;
2417     }
2418     return 0;
2419 }
2420
2421 int avpriv_unlock_avformat(void)
2422 {
2423     if (ff_lockmgr_cb) {
2424         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2425             return -1;
2426     }
2427     return 0;
2428 }
2429
2430 unsigned int avpriv_toupper4(unsigned int x)
2431 {
2432     return     toupper( x     &0xFF)
2433             + (toupper((x>>8 )&0xFF)<<8 )
2434             + (toupper((x>>16)&0xFF)<<16)
2435             + (toupper((x>>24)&0xFF)<<24);
2436 }
2437
2438 #if !HAVE_THREADS
2439
2440 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2441 {
2442     f->owner = avctx;
2443
2444     ff_init_buffer_info(avctx, f);
2445
2446     return avctx->get_buffer(avctx, f);
2447 }
2448
2449 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2450 {
2451     f->owner->release_buffer(f->owner, f);
2452 }
2453
2454 void ff_thread_finish_setup(AVCodecContext *avctx)
2455 {
2456 }
2457
2458 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2459 {
2460 }
2461
2462 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2463 {
2464 }
2465
2466 int ff_thread_can_start_frame(AVCodecContext *avctx)
2467 {
2468     return 1;
2469 }
2470
2471 #endif
2472
2473 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2474 {
2475     AVCodec *c= avcodec_find_decoder(codec_id);
2476     if(!c)
2477         c= avcodec_find_encoder(codec_id);
2478     if(c)
2479         return c->type;
2480
2481     if (codec_id <= CODEC_ID_NONE)
2482         return AVMEDIA_TYPE_UNKNOWN;
2483     else if (codec_id < CODEC_ID_FIRST_AUDIO)
2484         return AVMEDIA_TYPE_VIDEO;
2485     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2486         return AVMEDIA_TYPE_AUDIO;
2487     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2488         return AVMEDIA_TYPE_SUBTITLE;
2489
2490     return AVMEDIA_TYPE_UNKNOWN;
2491 }
2492
2493 int avcodec_is_open(AVCodecContext *s)
2494 {
2495     return !!s->internal;
2496 }