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