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