]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
Simplify av_log_missing_feature().
[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/avstring.h"
29 #include "libavutil/integer.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "avcodec.h"
36 #include "dsputil.h"
37 #include "libavutil/opt.h"
38 #include "imgconvert.h"
39 #include "thread.h"
40 #include "audioconvert.h"
41 #include "internal.h"
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <limits.h>
45 #include <float.h>
46
47 static int volatile entangled_thread_counter=0;
48 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
49 static void *codec_mutex;
50
51 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
52 {
53     if(min_size < *size)
54         return ptr;
55
56     min_size= FFMAX(17*min_size/16 + 32, min_size);
57
58     ptr= av_realloc(ptr, min_size);
59     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
60         min_size= 0;
61
62     *size= min_size;
63
64     return ptr;
65 }
66
67 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
68 {
69     void **p = ptr;
70     if (min_size < *size)
71         return;
72     min_size= FFMAX(17*min_size/16 + 32, min_size);
73     av_free(*p);
74     *p = av_malloc(min_size);
75     if (!*p) min_size = 0;
76     *size= min_size;
77 }
78
79 /* encoder management */
80 static AVCodec *first_avcodec = NULL;
81
82 AVCodec *av_codec_next(AVCodec *c){
83     if(c) return c->next;
84     else  return first_avcodec;
85 }
86
87 void avcodec_register(AVCodec *codec)
88 {
89     AVCodec **p;
90     avcodec_init();
91     p = &first_avcodec;
92     while (*p != NULL) p = &(*p)->next;
93     *p = codec;
94     codec->next = NULL;
95 }
96
97 unsigned avcodec_get_edge_width(void)
98 {
99     return EDGE_WIDTH;
100 }
101
102 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
103     s->coded_width = width;
104     s->coded_height= height;
105     s->width = -((-width )>>s->lowres);
106     s->height= -((-height)>>s->lowres);
107 }
108
109 typedef struct InternalBuffer{
110     int last_pic_num;
111     uint8_t *base[4];
112     uint8_t *data[4];
113     int linesize[4];
114     int width, height;
115     enum PixelFormat pix_fmt;
116 }InternalBuffer;
117
118 #define INTERNAL_BUFFER_SIZE 32
119
120 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
121     int w_align= 1;
122     int h_align= 1;
123
124     switch(s->pix_fmt){
125     case PIX_FMT_YUV420P:
126     case PIX_FMT_YUYV422:
127     case PIX_FMT_UYVY422:
128     case PIX_FMT_YUV422P:
129     case PIX_FMT_YUV440P:
130     case PIX_FMT_YUV444P:
131     case PIX_FMT_GRAY8:
132     case PIX_FMT_GRAY16BE:
133     case PIX_FMT_GRAY16LE:
134     case PIX_FMT_YUVJ420P:
135     case PIX_FMT_YUVJ422P:
136     case PIX_FMT_YUVJ440P:
137     case PIX_FMT_YUVJ444P:
138     case PIX_FMT_YUVA420P:
139         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
140         h_align= 16;
141         if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
142             h_align= 32; // interlaced is rounded up to 2 MBs
143         break;
144     case PIX_FMT_YUV411P:
145     case PIX_FMT_UYYVYY411:
146         w_align=32;
147         h_align=8;
148         break;
149     case PIX_FMT_YUV410P:
150         if(s->codec_id == CODEC_ID_SVQ1){
151             w_align=64;
152             h_align=64;
153         }
154     case PIX_FMT_RGB555:
155         if(s->codec_id == CODEC_ID_RPZA){
156             w_align=4;
157             h_align=4;
158         }
159     case PIX_FMT_PAL8:
160     case PIX_FMT_BGR8:
161     case PIX_FMT_RGB8:
162         if(s->codec_id == CODEC_ID_SMC){
163             w_align=4;
164             h_align=4;
165         }
166         break;
167     case PIX_FMT_BGR24:
168         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
169             w_align=4;
170             h_align=4;
171         }
172         break;
173     default:
174         w_align= 1;
175         h_align= 1;
176         break;
177     }
178
179     *width = FFALIGN(*width , w_align);
180     *height= FFALIGN(*height, h_align);
181     if(s->codec_id == CODEC_ID_H264 || s->lowres)
182         *height+=2; // some of the optimized chroma MC reads one line too much
183                     // which is also done in mpeg decoders with lowres > 0
184
185     linesize_align[0] =
186     linesize_align[1] =
187     linesize_align[2] =
188     linesize_align[3] = STRIDE_ALIGN;
189 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
190 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
191 //picture size unneccessarily in some cases. The solution here is not
192 //pretty and better ideas are welcome!
193 #if HAVE_MMX
194     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
195        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
196        s->codec_id == CODEC_ID_VP6A) {
197         linesize_align[0] =
198         linesize_align[1] =
199         linesize_align[2] = 16;
200     }
201 #endif
202 }
203
204 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
205     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
206     int linesize_align[4];
207     int align;
208     avcodec_align_dimensions2(s, width, height, linesize_align);
209     align = FFMAX(linesize_align[0], linesize_align[3]);
210     linesize_align[1] <<= chroma_shift;
211     linesize_align[2] <<= chroma_shift;
212     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
213     *width=FFALIGN(*width, align);
214 }
215
216 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
217     int i;
218     int w= s->width;
219     int h= s->height;
220     InternalBuffer *buf;
221     int *picture_number;
222
223     if(pic->data[0]!=NULL) {
224         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
225         return -1;
226     }
227     if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
228         av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
229         return -1;
230     }
231
232     if(av_image_check_size(w, h, 0, s))
233         return -1;
234
235     if(s->internal_buffer==NULL){
236         s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
237     }
238 #if 0
239     s->internal_buffer= av_fast_realloc(
240         s->internal_buffer,
241         &s->internal_buffer_size,
242         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
243         );
244 #endif
245
246     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
247     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
248     (*picture_number)++;
249
250     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
251         if(s->active_thread_type&FF_THREAD_FRAME) {
252             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
253             return -1;
254         }
255
256         for(i=0; i<4; i++){
257             av_freep(&buf->base[i]);
258             buf->data[i]= NULL;
259         }
260     }
261
262     if(buf->base[0]){
263         pic->age= *picture_number - buf->last_pic_num;
264         buf->last_pic_num= *picture_number;
265     }else{
266         int h_chroma_shift, v_chroma_shift;
267         int size[4] = {0};
268         int tmpsize;
269         int unaligned;
270         AVPicture picture;
271         int stride_align[4];
272
273         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
274
275         avcodec_align_dimensions2(s, &w, &h, stride_align);
276
277         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
278             w+= EDGE_WIDTH*2;
279             h+= EDGE_WIDTH*2;
280         }
281
282         do {
283             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
284             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
285             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
286             // increase alignment of w for next try (rhs gives the lowest bit set in w)
287             w += w & ~(w-1);
288
289             unaligned = 0;
290             for (i=0; i<4; i++){
291                 unaligned |= picture.linesize[i] % stride_align[i];
292             }
293         } while (unaligned);
294
295         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
296         if (tmpsize < 0)
297             return -1;
298
299         for (i=0; i<3 && picture.data[i+1]; i++)
300             size[i] = picture.data[i+1] - picture.data[i];
301         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
302
303         buf->last_pic_num= -256*256*256*64;
304         memset(buf->base, 0, sizeof(buf->base));
305         memset(buf->data, 0, sizeof(buf->data));
306
307         for(i=0; i<4 && size[i]; i++){
308             const int h_shift= i==0 ? 0 : h_chroma_shift;
309             const int v_shift= i==0 ? 0 : v_chroma_shift;
310
311             buf->linesize[i]= picture.linesize[i];
312
313             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
314             if(buf->base[i]==NULL) return -1;
315             memset(buf->base[i], 128, size[i]);
316
317             // no edge if EDGE EMU or not planar YUV
318             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
319                 buf->data[i] = buf->base[i];
320             else
321                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
322         }
323         if(size[1] && !size[2])
324             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
325         buf->width  = s->width;
326         buf->height = s->height;
327         buf->pix_fmt= s->pix_fmt;
328         pic->age= 256*256*256*64;
329     }
330     pic->type= FF_BUFFER_TYPE_INTERNAL;
331
332     for(i=0; i<4; i++){
333         pic->base[i]= buf->base[i];
334         pic->data[i]= buf->data[i];
335         pic->linesize[i]= buf->linesize[i];
336     }
337     s->internal_buffer_count++;
338
339     if(s->pkt) pic->pkt_pts= s->pkt->pts;
340     else       pic->pkt_pts= AV_NOPTS_VALUE;
341     pic->reordered_opaque= s->reordered_opaque;
342
343     if(s->debug&FF_DEBUG_BUFFERS)
344         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
345
346     return 0;
347 }
348
349 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
350     int i;
351     InternalBuffer *buf, *last;
352
353     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
354     assert(s->internal_buffer_count);
355
356     buf = NULL; /* avoids warning */
357     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
358         buf= &((InternalBuffer*)s->internal_buffer)[i];
359         if(buf->data[0] == pic->data[0])
360             break;
361     }
362     assert(i < s->internal_buffer_count);
363     s->internal_buffer_count--;
364     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
365
366     FFSWAP(InternalBuffer, *buf, *last);
367
368     for(i=0; i<4; i++){
369         pic->data[i]=NULL;
370 //        pic->base[i]=NULL;
371     }
372 //printf("R%X\n", pic->opaque);
373
374     if(s->debug&FF_DEBUG_BUFFERS)
375         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
376 }
377
378 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
379     AVFrame temp_pic;
380     int i;
381
382     /* If no picture return a new buffer */
383     if(pic->data[0] == NULL) {
384         /* We will copy from buffer, so must be readable */
385         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
386         return s->get_buffer(s, pic);
387     }
388
389     /* If internal buffer type return the same buffer */
390     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
391         if(s->pkt) pic->pkt_pts= s->pkt->pts;
392         else       pic->pkt_pts= AV_NOPTS_VALUE;
393         pic->reordered_opaque= s->reordered_opaque;
394         return 0;
395     }
396
397     /*
398      * Not internal type and reget_buffer not overridden, emulate cr buffer
399      */
400     temp_pic = *pic;
401     for(i = 0; i < 4; i++)
402         pic->data[i] = pic->base[i] = NULL;
403     pic->opaque = NULL;
404     /* Allocate new frame */
405     if (s->get_buffer(s, pic))
406         return -1;
407     /* Copy image data from old buffer to new buffer */
408     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
409              s->height);
410     s->release_buffer(s, &temp_pic); // Release old frame
411     return 0;
412 }
413
414 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
415     int i;
416
417     for(i=0; i<count; i++){
418         int r= func(c, (char*)arg + i*size);
419         if(ret) ret[i]= r;
420     }
421     return 0;
422 }
423
424 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
425     int i;
426
427     for(i=0; i<count; i++){
428         int r= func(c, arg, i, 0);
429         if(ret) ret[i]= r;
430     }
431     return 0;
432 }
433
434 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
435     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
436         ++fmt;
437     return fmt[0];
438 }
439
440 void avcodec_get_frame_defaults(AVFrame *pic){
441     memset(pic, 0, sizeof(AVFrame));
442
443     pic->pts= AV_NOPTS_VALUE;
444     pic->key_frame= 1;
445 }
446
447 AVFrame *avcodec_alloc_frame(void){
448     AVFrame *pic= av_malloc(sizeof(AVFrame));
449
450     if(pic==NULL) return NULL;
451
452     avcodec_get_frame_defaults(pic);
453
454     return pic;
455 }
456
457 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
458 {
459     int ret = 0;
460
461     /* If there is a user-supplied mutex locking routine, call it. */
462     if (ff_lockmgr_cb) {
463         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
464             return -1;
465     }
466
467     entangled_thread_counter++;
468     if(entangled_thread_counter != 1){
469         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
470         ret = -1;
471         goto end;
472     }
473
474     if(avctx->codec || !codec) {
475         ret = AVERROR(EINVAL);
476         goto end;
477     }
478
479     if (codec->priv_data_size > 0) {
480       if(!avctx->priv_data){
481         avctx->priv_data = av_mallocz(codec->priv_data_size);
482         if (!avctx->priv_data) {
483             ret = AVERROR(ENOMEM);
484             goto end;
485         }
486         if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
487             *(AVClass**)avctx->priv_data= codec->priv_class;
488             av_opt_set_defaults(avctx->priv_data);
489         }
490       }
491     } else {
492         avctx->priv_data = NULL;
493     }
494
495     if(avctx->coded_width && avctx->coded_height)
496         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
497     else if(avctx->width && avctx->height)
498         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
499
500     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
501         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
502            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
503         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
504         avcodec_set_dimensions(avctx, 0, 0);
505     }
506
507     /* if the decoder init function was already called previously,
508        free the already allocated subtitle_header before overwriting it */
509     if (codec->decode)
510         av_freep(&avctx->subtitle_header);
511
512 #define SANE_NB_CHANNELS 128U
513     if (avctx->channels > SANE_NB_CHANNELS) {
514         ret = AVERROR(EINVAL);
515         goto free_and_end;
516     }
517
518     avctx->codec = codec;
519     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
520         avctx->codec_id == CODEC_ID_NONE) {
521         avctx->codec_type = codec->type;
522         avctx->codec_id   = codec->id;
523     }
524     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
525                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
526         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
527         ret = AVERROR(EINVAL);
528         goto free_and_end;
529     }
530     avctx->frame_number = 0;
531
532     if (HAVE_THREADS && !avctx->thread_opaque) {
533         ret = ff_thread_init(avctx);
534         if (ret < 0) {
535             goto free_and_end;
536         }
537     }
538
539     if (avctx->codec->max_lowres < avctx->lowres) {
540         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
541                avctx->codec->max_lowres);
542         ret = AVERROR(EINVAL);
543         goto free_and_end;
544     }
545     if (avctx->codec->encode) {
546         int i;
547         if (avctx->codec->sample_fmts) {
548             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
549                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
550                     break;
551             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
552                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
553                 ret = AVERROR(EINVAL);
554                 goto free_and_end;
555             }
556         }
557         if (avctx->codec->supported_samplerates) {
558             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
559                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
560                     break;
561             if (avctx->codec->supported_samplerates[i] == 0) {
562                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
563                 ret = AVERROR(EINVAL);
564                 goto free_and_end;
565             }
566         }
567         if (avctx->codec->channel_layouts) {
568             if (!avctx->channel_layout) {
569                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
570             } else {
571                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
572                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
573                         break;
574                 if (avctx->codec->channel_layouts[i] == 0) {
575                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
576                     ret = AVERROR(EINVAL);
577                     goto free_and_end;
578                 }
579             }
580         }
581         if (avctx->channel_layout && avctx->channels) {
582             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
583                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
584                 ret = AVERROR(EINVAL);
585                 goto free_and_end;
586             }
587         } else if (avctx->channel_layout) {
588             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
589         }
590     }
591
592     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
593         ret = avctx->codec->init(avctx);
594         if (ret < 0) {
595             goto free_and_end;
596         }
597     }
598 end:
599     entangled_thread_counter--;
600
601     /* Release any user-supplied mutex. */
602     if (ff_lockmgr_cb) {
603         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
604     }
605     return ret;
606 free_and_end:
607     av_freep(&avctx->priv_data);
608     avctx->codec= NULL;
609     goto end;
610 }
611
612 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
613                          const short *samples)
614 {
615     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
616         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
617         return -1;
618     }
619     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
620         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
621         avctx->frame_number++;
622         return ret;
623     }else
624         return 0;
625 }
626
627 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
628                          const AVFrame *pict)
629 {
630     if(buf_size < FF_MIN_BUFFER_SIZE){
631         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
632         return -1;
633     }
634     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
635         return -1;
636     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
637         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
638         avctx->frame_number++;
639         emms_c(); //needed to avoid an emms_c() call before every return;
640
641         return ret;
642     }else
643         return 0;
644 }
645
646 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
647                             const AVSubtitle *sub)
648 {
649     int ret;
650     if(sub->start_display_time) {
651         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
652         return -1;
653     }
654     if(sub->num_rects == 0 || !sub->rects)
655         return -1;
656     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
657     avctx->frame_number++;
658     return ret;
659 }
660
661 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
662                          int *got_picture_ptr,
663                          AVPacket *avpkt)
664 {
665     int ret;
666
667     *got_picture_ptr= 0;
668     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
669         return -1;
670
671     avctx->pkt = avpkt;
672
673     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
674         if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
675              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
676                                           avpkt);
677         else {
678             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
679                               avpkt);
680             picture->pkt_dts= avpkt->dts;
681         }
682
683         emms_c(); //needed to avoid an emms_c() call before every return;
684
685         if (*got_picture_ptr)
686             avctx->frame_number++;
687     }else
688         ret= 0;
689
690     return ret;
691 }
692
693 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
694                          int *frame_size_ptr,
695                          AVPacket *avpkt)
696 {
697     int ret;
698
699     avctx->pkt = avpkt;
700
701     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
702         //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
703         if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
704             av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
705             return -1;
706         }
707         if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
708         *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
709             av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
710             return -1;
711         }
712
713         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
714         avctx->frame_number++;
715     }else{
716         ret= 0;
717         *frame_size_ptr=0;
718     }
719     return ret;
720 }
721
722 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
723                             int *got_sub_ptr,
724                             AVPacket *avpkt)
725 {
726     int ret;
727
728     avctx->pkt = avpkt;
729     *got_sub_ptr = 0;
730     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
731     if (*got_sub_ptr)
732         avctx->frame_number++;
733     return ret;
734 }
735
736 void avsubtitle_free(AVSubtitle *sub)
737 {
738     int i;
739
740     for (i = 0; i < sub->num_rects; i++)
741     {
742         av_freep(&sub->rects[i]->pict.data[0]);
743         av_freep(&sub->rects[i]->pict.data[1]);
744         av_freep(&sub->rects[i]->pict.data[2]);
745         av_freep(&sub->rects[i]->pict.data[3]);
746         av_freep(&sub->rects[i]->text);
747         av_freep(&sub->rects[i]->ass);
748         av_freep(&sub->rects[i]);
749     }
750
751     av_freep(&sub->rects);
752
753     memset(sub, 0, sizeof(AVSubtitle));
754 }
755
756 av_cold int avcodec_close(AVCodecContext *avctx)
757 {
758     /* If there is a user-supplied mutex locking routine, call it. */
759     if (ff_lockmgr_cb) {
760         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
761             return -1;
762     }
763
764     entangled_thread_counter++;
765     if(entangled_thread_counter != 1){
766         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
767         entangled_thread_counter--;
768         return -1;
769     }
770
771     if (HAVE_THREADS && avctx->thread_opaque)
772         ff_thread_free(avctx);
773     if (avctx->codec && avctx->codec->close)
774         avctx->codec->close(avctx);
775     avcodec_default_free_buffers(avctx);
776     avctx->coded_frame = NULL;
777     av_freep(&avctx->priv_data);
778     if(avctx->codec && avctx->codec->encode)
779         av_freep(&avctx->extradata);
780     avctx->codec = NULL;
781     avctx->active_thread_type = 0;
782     entangled_thread_counter--;
783
784     /* Release any user-supplied mutex. */
785     if (ff_lockmgr_cb) {
786         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
787     }
788     return 0;
789 }
790
791 AVCodec *avcodec_find_encoder(enum CodecID id)
792 {
793     AVCodec *p, *experimental=NULL;
794     p = first_avcodec;
795     while (p) {
796         if (p->encode != NULL && p->id == id) {
797             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
798                 experimental = p;
799             } else
800                 return p;
801         }
802         p = p->next;
803     }
804     return experimental;
805 }
806
807 AVCodec *avcodec_find_encoder_by_name(const char *name)
808 {
809     AVCodec *p;
810     if (!name)
811         return NULL;
812     p = first_avcodec;
813     while (p) {
814         if (p->encode != NULL && strcmp(name,p->name) == 0)
815             return p;
816         p = p->next;
817     }
818     return NULL;
819 }
820
821 AVCodec *avcodec_find_decoder(enum CodecID id)
822 {
823     AVCodec *p;
824     p = first_avcodec;
825     while (p) {
826         if (p->decode != NULL && p->id == id)
827             return p;
828         p = p->next;
829     }
830     return NULL;
831 }
832
833 AVCodec *avcodec_find_decoder_by_name(const char *name)
834 {
835     AVCodec *p;
836     if (!name)
837         return NULL;
838     p = first_avcodec;
839     while (p) {
840         if (p->decode != NULL && strcmp(name,p->name) == 0)
841             return p;
842         p = p->next;
843     }
844     return NULL;
845 }
846
847 static int get_bit_rate(AVCodecContext *ctx)
848 {
849     int bit_rate;
850     int bits_per_sample;
851
852     switch(ctx->codec_type) {
853     case AVMEDIA_TYPE_VIDEO:
854     case AVMEDIA_TYPE_DATA:
855     case AVMEDIA_TYPE_SUBTITLE:
856     case AVMEDIA_TYPE_ATTACHMENT:
857         bit_rate = ctx->bit_rate;
858         break;
859     case AVMEDIA_TYPE_AUDIO:
860         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
861         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
862         break;
863     default:
864         bit_rate = 0;
865         break;
866     }
867     return bit_rate;
868 }
869
870 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
871 {
872     int i, len, ret = 0;
873
874     for (i = 0; i < 4; i++) {
875         len = snprintf(buf, buf_size,
876                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
877         buf      += len;
878         buf_size  = buf_size > len ? buf_size - len : 0;
879         ret      += len;
880         codec_tag>>=8;
881     }
882     return ret;
883 }
884
885 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
886 {
887     const char *codec_name;
888     const char *profile = NULL;
889     AVCodec *p;
890     char buf1[32];
891     int bitrate;
892     AVRational display_aspect_ratio;
893
894     if (encode)
895         p = avcodec_find_encoder(enc->codec_id);
896     else
897         p = avcodec_find_decoder(enc->codec_id);
898
899     if (p) {
900         codec_name = p->name;
901         profile = av_get_profile_name(p, enc->profile);
902     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
903         /* fake mpeg2 transport stream codec (currently not
904            registered) */
905         codec_name = "mpeg2ts";
906     } else if (enc->codec_name[0] != '\0') {
907         codec_name = enc->codec_name;
908     } else {
909         /* output avi tags */
910         char tag_buf[32];
911         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
912         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
913         codec_name = buf1;
914     }
915
916     switch(enc->codec_type) {
917     case AVMEDIA_TYPE_VIDEO:
918         snprintf(buf, buf_size,
919                  "Video: %s%s",
920                  codec_name, enc->mb_decision ? " (hq)" : "");
921         if (profile)
922             snprintf(buf + strlen(buf), buf_size - strlen(buf),
923                      " (%s)", profile);
924         if (enc->pix_fmt != PIX_FMT_NONE) {
925             snprintf(buf + strlen(buf), buf_size - strlen(buf),
926                      ", %s",
927                      avcodec_get_pix_fmt_name(enc->pix_fmt));
928         }
929         if (enc->width) {
930             snprintf(buf + strlen(buf), buf_size - strlen(buf),
931                      ", %dx%d",
932                      enc->width, enc->height);
933             if (enc->sample_aspect_ratio.num) {
934                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
935                           enc->width*enc->sample_aspect_ratio.num,
936                           enc->height*enc->sample_aspect_ratio.den,
937                           1024*1024);
938                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
939                          " [PAR %d:%d DAR %d:%d]",
940                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
941                          display_aspect_ratio.num, display_aspect_ratio.den);
942             }
943             if(av_log_get_level() >= AV_LOG_DEBUG){
944                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
945                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
946                      ", %d/%d",
947                      enc->time_base.num/g, enc->time_base.den/g);
948             }
949         }
950         if (encode) {
951             snprintf(buf + strlen(buf), buf_size - strlen(buf),
952                      ", q=%d-%d", enc->qmin, enc->qmax);
953         }
954         break;
955     case AVMEDIA_TYPE_AUDIO:
956         snprintf(buf, buf_size,
957                  "Audio: %s",
958                  codec_name);
959         if (profile)
960             snprintf(buf + strlen(buf), buf_size - strlen(buf),
961                      " (%s)", profile);
962         if (enc->sample_rate) {
963             snprintf(buf + strlen(buf), buf_size - strlen(buf),
964                      ", %d Hz", enc->sample_rate);
965         }
966         av_strlcat(buf, ", ", buf_size);
967         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
968         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
969             snprintf(buf + strlen(buf), buf_size - strlen(buf),
970                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
971         }
972         break;
973     case AVMEDIA_TYPE_DATA:
974         snprintf(buf, buf_size, "Data: %s", codec_name);
975         break;
976     case AVMEDIA_TYPE_SUBTITLE:
977         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
978         break;
979     case AVMEDIA_TYPE_ATTACHMENT:
980         snprintf(buf, buf_size, "Attachment: %s", codec_name);
981         break;
982     default:
983         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
984         return;
985     }
986     if (encode) {
987         if (enc->flags & CODEC_FLAG_PASS1)
988             snprintf(buf + strlen(buf), buf_size - strlen(buf),
989                      ", pass 1");
990         if (enc->flags & CODEC_FLAG_PASS2)
991             snprintf(buf + strlen(buf), buf_size - strlen(buf),
992                      ", pass 2");
993     }
994     bitrate = get_bit_rate(enc);
995     if (bitrate != 0) {
996         snprintf(buf + strlen(buf), buf_size - strlen(buf),
997                  ", %d kb/s", bitrate / 1000);
998     }
999 }
1000
1001 const char *av_get_profile_name(const AVCodec *codec, int profile)
1002 {
1003     const AVProfile *p;
1004     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1005         return NULL;
1006
1007     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1008         if (p->profile == profile)
1009             return p->name;
1010
1011     return NULL;
1012 }
1013
1014 unsigned avcodec_version( void )
1015 {
1016   return LIBAVCODEC_VERSION_INT;
1017 }
1018
1019 const char *avcodec_configuration(void)
1020 {
1021     return LIBAV_CONFIGURATION;
1022 }
1023
1024 const char *avcodec_license(void)
1025 {
1026 #define LICENSE_PREFIX "libavcodec license: "
1027     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1028 }
1029
1030 void avcodec_init(void)
1031 {
1032     static int initialized = 0;
1033
1034     if (initialized != 0)
1035         return;
1036     initialized = 1;
1037
1038     dsputil_static_init();
1039 }
1040
1041 void avcodec_flush_buffers(AVCodecContext *avctx)
1042 {
1043     if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1044         ff_thread_flush(avctx);
1045     if(avctx->codec->flush)
1046         avctx->codec->flush(avctx);
1047 }
1048
1049 void avcodec_default_free_buffers(AVCodecContext *s){
1050     int i, j;
1051
1052     if(s->internal_buffer==NULL) return;
1053
1054     if (s->internal_buffer_count)
1055         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1056     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1057         InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1058         for(j=0; j<4; j++){
1059             av_freep(&buf->base[j]);
1060             buf->data[j]= NULL;
1061         }
1062     }
1063     av_freep(&s->internal_buffer);
1064
1065     s->internal_buffer_count=0;
1066 }
1067
1068 #if FF_API_OLD_FF_PICT_TYPES
1069 char av_get_pict_type_char(int pict_type){
1070     return av_get_picture_type_char(pict_type);
1071 }
1072 #endif
1073
1074 int av_get_bits_per_sample(enum CodecID codec_id){
1075     switch(codec_id){
1076     case CODEC_ID_ADPCM_SBPRO_2:
1077         return 2;
1078     case CODEC_ID_ADPCM_SBPRO_3:
1079         return 3;
1080     case CODEC_ID_ADPCM_SBPRO_4:
1081     case CODEC_ID_ADPCM_CT:
1082     case CODEC_ID_ADPCM_IMA_WAV:
1083     case CODEC_ID_ADPCM_MS:
1084     case CODEC_ID_ADPCM_YAMAHA:
1085         return 4;
1086     case CODEC_ID_ADPCM_G722:
1087     case CODEC_ID_PCM_ALAW:
1088     case CODEC_ID_PCM_MULAW:
1089     case CODEC_ID_PCM_S8:
1090     case CODEC_ID_PCM_U8:
1091     case CODEC_ID_PCM_ZORK:
1092         return 8;
1093     case CODEC_ID_PCM_S16BE:
1094     case CODEC_ID_PCM_S16LE:
1095     case CODEC_ID_PCM_S16LE_PLANAR:
1096     case CODEC_ID_PCM_U16BE:
1097     case CODEC_ID_PCM_U16LE:
1098         return 16;
1099     case CODEC_ID_PCM_S24DAUD:
1100     case CODEC_ID_PCM_S24BE:
1101     case CODEC_ID_PCM_S24LE:
1102     case CODEC_ID_PCM_U24BE:
1103     case CODEC_ID_PCM_U24LE:
1104         return 24;
1105     case CODEC_ID_PCM_S32BE:
1106     case CODEC_ID_PCM_S32LE:
1107     case CODEC_ID_PCM_U32BE:
1108     case CODEC_ID_PCM_U32LE:
1109     case CODEC_ID_PCM_F32BE:
1110     case CODEC_ID_PCM_F32LE:
1111         return 32;
1112     case CODEC_ID_PCM_F64BE:
1113     case CODEC_ID_PCM_F64LE:
1114         return 64;
1115     default:
1116         return 0;
1117     }
1118 }
1119
1120 #if FF_API_OLD_SAMPLE_FMT
1121 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1122     return av_get_bits_per_sample_fmt(sample_fmt);
1123 }
1124 #endif
1125
1126 #if !HAVE_THREADS
1127 int ff_thread_init(AVCodecContext *s){
1128     return -1;
1129 }
1130 #endif
1131
1132 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1133 {
1134     unsigned int n = 0;
1135
1136     while(v >= 0xff) {
1137         *s++ = 0xff;
1138         v -= 0xff;
1139         n++;
1140     }
1141     *s = v;
1142     n++;
1143     return n;
1144 }
1145
1146 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1147     int i;
1148     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1149     return i;
1150 }
1151
1152 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1153 {
1154     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1155             "version to the newest one from Git. If the problem still "
1156             "occurs, it means that your file has a feature which has not "
1157             "been implemented.\n", feature);
1158     if(want_sample)
1159         av_log_ask_for_sample(avc, NULL);
1160 }
1161
1162 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1163 {
1164     va_list argument_list;
1165
1166     va_start(argument_list, msg);
1167
1168     if (msg)
1169         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1170     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1171             "of this file to ftp://upload.libav.org/incoming/ "
1172             "and contact the libav-devel mailing list.\n");
1173
1174     va_end(argument_list);
1175 }
1176
1177 static AVHWAccel *first_hwaccel = NULL;
1178
1179 void av_register_hwaccel(AVHWAccel *hwaccel)
1180 {
1181     AVHWAccel **p = &first_hwaccel;
1182     while (*p)
1183         p = &(*p)->next;
1184     *p = hwaccel;
1185     hwaccel->next = NULL;
1186 }
1187
1188 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1189 {
1190     return hwaccel ? hwaccel->next : first_hwaccel;
1191 }
1192
1193 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1194 {
1195     AVHWAccel *hwaccel=NULL;
1196
1197     while((hwaccel= av_hwaccel_next(hwaccel))){
1198         if (   hwaccel->id      == codec_id
1199             && hwaccel->pix_fmt == pix_fmt)
1200             return hwaccel;
1201     }
1202     return NULL;
1203 }
1204
1205 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1206 {
1207     if (ff_lockmgr_cb) {
1208         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1209             return -1;
1210     }
1211
1212     ff_lockmgr_cb = cb;
1213
1214     if (ff_lockmgr_cb) {
1215         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1216             return -1;
1217     }
1218     return 0;
1219 }
1220
1221 unsigned int ff_toupper4(unsigned int x)
1222 {
1223     return     toupper( x     &0xFF)
1224             + (toupper((x>>8 )&0xFF)<<8 )
1225             + (toupper((x>>16)&0xFF)<<16)
1226             + (toupper((x>>24)&0xFF)<<24);
1227 }
1228
1229 #if !HAVE_PTHREADS
1230
1231 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1232 {
1233     f->owner = avctx;
1234     return avctx->get_buffer(avctx, f);
1235 }
1236
1237 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1238 {
1239     f->owner->release_buffer(f->owner, f);
1240 }
1241
1242 void ff_thread_finish_setup(AVCodecContext *avctx)
1243 {
1244 }
1245
1246 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1247 {
1248 }
1249
1250 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1251 {
1252 }
1253
1254 #endif
1255
1256 #if FF_API_THREAD_INIT
1257 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1258 {
1259     s->thread_count = thread_count;
1260     return ff_thread_init(s);
1261 }
1262 #endif