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