r1004: Fix http://article.gmane.org/gmane.comp.video.ffmpeg.devel/26558
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / utils.c
bloba5e353110f40b0cf1266a138bd395609b3a0bfb4
1 /*
2 * utils for libavcodec
3 * Copyright (c) 2001 Fabrice Bellard.
4 * Copyright (c) 2003 Michel Bardiaux for the av_log API
5 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /**
23 * @file utils.c
24 * utils.
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "mpegvideo.h"
30 #include "integer.h"
31 #include <stdarg.h>
32 #include <limits.h>
34 const uint8_t ff_sqrt_tab[128]={
35 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
36 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
37 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
38 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
41 const uint8_t ff_log2_tab[256]={
42 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
43 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
44 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
45 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
46 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
47 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
48 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
49 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
52 const uint8_t ff_reverse[256]={
53 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
54 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
55 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
56 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
57 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
58 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
59 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
60 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
61 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
62 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
63 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
64 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
65 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
66 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
67 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
68 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
71 static int volatile entangled_thread_counter=0;
73 void avcodec_default_free_buffers(AVCodecContext *s);
75 void *av_mallocz(unsigned int size)
77 void *ptr;
79 ptr = av_malloc(size);
80 if (!ptr)
81 return NULL;
82 memset(ptr, 0, size);
83 return ptr;
86 char *av_strdup(const char *s)
88 char *ptr;
89 int len;
90 len = strlen(s) + 1;
91 ptr = av_malloc(len);
92 if (!ptr)
93 return NULL;
94 memcpy(ptr, s, len);
95 return ptr;
98 /**
99 * realloc which does nothing if the block is large enough
101 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
103 if(min_size < *size)
104 return ptr;
106 *size= FFMAX(17*min_size/16 + 32, min_size);
108 return av_realloc(ptr, *size);
112 static unsigned int last_static = 0;
113 static unsigned int allocated_static = 0;
114 static void** array_static = NULL;
117 * allocation of static arrays - do not use for normal allocation.
119 void *av_mallocz_static(unsigned int size)
121 void *ptr = av_mallocz(size);
123 if(ptr){
124 array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
125 if(!array_static)
126 return NULL;
127 array_static[last_static++] = ptr;
130 return ptr;
134 * same as above, but does realloc
137 void *av_realloc_static(void *ptr, unsigned int size)
139 int i;
140 if(!ptr)
141 return av_mallocz_static(size);
142 /* Look for the old ptr */
143 for(i = 0; i < last_static; i++) {
144 if(array_static[i] == ptr) {
145 array_static[i] = av_realloc(array_static[i], size);
146 return array_static[i];
149 return NULL;
154 * free all static arrays and reset pointers to 0.
156 void av_free_static(void)
158 while(last_static){
159 av_freep(&array_static[--last_static]);
161 av_freep(&array_static);
165 * Frees memory and sets the pointer to NULL.
166 * @param arg pointer to the pointer which should be freed
168 void av_freep(void *arg)
170 void **ptr= (void**)arg;
171 av_free(*ptr);
172 *ptr = NULL;
175 /* encoder management */
176 AVCodec *first_avcodec = NULL;
178 void register_avcodec(AVCodec *format)
180 AVCodec **p;
181 p = &first_avcodec;
182 while (*p != NULL) p = &(*p)->next;
183 *p = format;
184 format->next = NULL;
187 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
188 s->coded_width = width;
189 s->coded_height= height;
190 s->width = -((-width )>>s->lowres);
191 s->height= -((-height)>>s->lowres);
194 typedef struct InternalBuffer{
195 int last_pic_num;
196 uint8_t *base[4];
197 uint8_t *data[4];
198 int linesize[4];
199 }InternalBuffer;
201 #define INTERNAL_BUFFER_SIZE 32
203 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
205 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
206 int w_align= 1;
207 int h_align= 1;
209 switch(s->pix_fmt){
210 case PIX_FMT_YUV420P:
211 case PIX_FMT_YUV422:
212 case PIX_FMT_UYVY422:
213 case PIX_FMT_YUV422P:
214 case PIX_FMT_YUV444P:
215 case PIX_FMT_GRAY8:
216 case PIX_FMT_YUVJ420P:
217 case PIX_FMT_YUVJ422P:
218 case PIX_FMT_YUVJ444P:
219 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
220 h_align= 16;
221 break;
222 case PIX_FMT_YUV411P:
223 case PIX_FMT_UYVY411:
224 w_align=32;
225 h_align=8;
226 break;
227 case PIX_FMT_YUV410P:
228 if(s->codec_id == CODEC_ID_SVQ1){
229 w_align=64;
230 h_align=64;
232 case PIX_FMT_RGB555:
233 if(s->codec_id == CODEC_ID_RPZA){
234 w_align=4;
235 h_align=4;
237 case PIX_FMT_PAL8:
238 if(s->codec_id == CODEC_ID_SMC){
239 w_align=4;
240 h_align=4;
242 break;
243 case PIX_FMT_BGR24:
244 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
245 w_align=4;
246 h_align=4;
248 break;
249 default:
250 w_align= 1;
251 h_align= 1;
252 break;
255 *width = ALIGN(*width , w_align);
256 *height= ALIGN(*height, h_align);
259 int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
260 if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
261 return 0;
263 av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
264 return -1;
267 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
268 int i;
269 int w= s->width;
270 int h= s->height;
271 InternalBuffer *buf;
272 int *picture_number;
274 assert(pic->data[0]==NULL);
275 assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
277 if(avcodec_check_dimensions(s,w,h))
278 return -1;
280 if(s->internal_buffer==NULL){
281 s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
283 #if 0
284 s->internal_buffer= av_fast_realloc(
285 s->internal_buffer,
286 &s->internal_buffer_size,
287 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
289 #endif
291 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
292 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
293 (*picture_number)++;
295 if(buf->base[0]){
296 pic->age= *picture_number - buf->last_pic_num;
297 buf->last_pic_num= *picture_number;
298 }else{
299 int h_chroma_shift, v_chroma_shift;
300 int pixel_size;
302 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
304 switch(s->pix_fmt){
305 case PIX_FMT_RGB555:
306 case PIX_FMT_RGB565:
307 case PIX_FMT_YUV422:
308 case PIX_FMT_UYVY422:
309 pixel_size=2;
310 break;
311 case PIX_FMT_RGB24:
312 case PIX_FMT_BGR24:
313 pixel_size=3;
314 break;
315 case PIX_FMT_RGBA32:
316 pixel_size=4;
317 break;
318 default:
319 pixel_size=1;
322 avcodec_align_dimensions(s, &w, &h);
324 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
325 w+= EDGE_WIDTH*2;
326 h+= EDGE_WIDTH*2;
329 buf->last_pic_num= -256*256*256*64;
331 for(i=0; i<3; i++){
332 const int h_shift= i==0 ? 0 : h_chroma_shift;
333 const int v_shift= i==0 ? 0 : v_chroma_shift;
335 if(s->pix_fmt == PIX_FMT_PAL8 && i == 1)
337 buf->base[i] = av_malloc(256 * 4);
338 if(buf->base[i] == NULL)
339 return -1;
340 buf->data[i] = buf->base[i];
341 continue;
344 //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
345 buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, STRIDE_ALIGN<<(h_chroma_shift-h_shift));
347 buf->base[i]= av_malloc((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
348 if(buf->base[i]==NULL) return -1;
349 memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
351 if(s->flags&CODEC_FLAG_EMU_EDGE)
352 buf->data[i] = buf->base[i];
353 else
354 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
356 pic->age= 256*256*256*64;
358 pic->type= FF_BUFFER_TYPE_INTERNAL;
360 for(i=0; i<4; i++){
361 pic->base[i]= buf->base[i];
362 pic->data[i]= buf->data[i];
363 pic->linesize[i]= buf->linesize[i];
365 s->internal_buffer_count++;
367 return 0;
370 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
371 int i;
372 InternalBuffer *buf, *last, temp;
374 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
375 assert(s->internal_buffer_count);
377 buf = NULL; /* avoids warning */
378 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
379 buf= &((InternalBuffer*)s->internal_buffer)[i];
380 if(buf->data[0] == pic->data[0])
381 break;
383 assert(i < s->internal_buffer_count);
384 s->internal_buffer_count--;
385 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
387 temp= *buf;
388 *buf= *last;
389 *last= temp;
391 for(i=0; i<3; i++){
392 pic->data[i]=NULL;
393 // pic->base[i]=NULL;
395 //printf("R%X\n", pic->opaque);
398 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
399 AVFrame temp_pic;
400 int i;
402 /* If no picture return a new buffer */
403 if(pic->data[0] == NULL) {
404 /* We will copy from buffer, so must be readable */
405 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
406 return s->get_buffer(s, pic);
409 /* If internal buffer type return the same buffer */
410 if(pic->type == FF_BUFFER_TYPE_INTERNAL)
411 return 0;
414 * Not internal type and reget_buffer not overridden, emulate cr buffer
416 temp_pic = *pic;
417 for(i = 0; i < 4; i++)
418 pic->data[i] = pic->base[i] = NULL;
419 pic->opaque = NULL;
420 /* Allocate new frame */
421 if (s->get_buffer(s, pic))
422 return -1;
423 /* Copy image data from old buffer to new buffer */
424 img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
425 s->height);
426 s->release_buffer(s, &temp_pic); // Release old frame
427 return 0;
430 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
431 int i;
433 for(i=0; i<count; i++){
434 int r= func(c, arg[i]);
435 if(ret) ret[i]= r;
437 return 0;
440 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
441 return fmt[0];
444 static const char* context_to_name(void* ptr) {
445 AVCodecContext *avc= ptr;
447 if(avc && avc->codec && avc->codec->name)
448 return avc->codec->name;
449 else
450 return "NULL";
453 static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
455 void avcodec_get_context_defaults(AVCodecContext *s){
456 memset(s, 0, sizeof(AVCodecContext));
458 s->av_class= &av_codec_context_class;
459 s->bit_rate= 800*1000;
460 s->bit_rate_tolerance= s->bit_rate*10;
461 s->qmin= 2;
462 s->qmax= 31;
463 s->mb_lmin= FF_QP2LAMBDA * 2;
464 s->mb_lmax= FF_QP2LAMBDA * 31;
465 s->rc_eq= "tex^qComp";
466 s->qcompress= 0.5;
467 s->max_qdiff= 3;
468 s->b_quant_factor=1.25;
469 s->b_quant_offset=1.25;
470 s->i_quant_factor=-0.8;
471 s->i_quant_offset=0.0;
472 s->error_concealment= 3;
473 s->error_resilience= 1;
474 s->workaround_bugs= FF_BUG_AUTODETECT;
475 s->time_base= (AVRational){0,1};
476 s->gop_size= 50;
477 s->me_method= ME_EPZS;
478 s->get_buffer= avcodec_default_get_buffer;
479 s->release_buffer= avcodec_default_release_buffer;
480 s->get_format= avcodec_default_get_format;
481 s->execute= avcodec_default_execute;
482 s->thread_count=1;
483 s->me_subpel_quality=8;
484 s->lmin= FF_QP2LAMBDA * s->qmin;
485 s->lmax= FF_QP2LAMBDA * s->qmax;
486 s->sample_aspect_ratio= (AVRational){0,1};
487 s->ildct_cmp= FF_CMP_VSAD;
488 s->profile= FF_PROFILE_UNKNOWN;
489 s->level= FF_LEVEL_UNKNOWN;
490 s->me_penalty_compensation= 256;
491 s->pix_fmt= PIX_FMT_NONE;
493 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
494 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
495 s->palctrl = NULL;
496 s->reget_buffer= avcodec_default_reget_buffer;
500 * allocates a AVCodecContext and set it to defaults.
501 * this can be deallocated by simply calling free()
503 AVCodecContext *avcodec_alloc_context(void){
504 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
506 if(avctx==NULL) return NULL;
508 avcodec_get_context_defaults(avctx);
510 return avctx;
513 void avcodec_get_frame_defaults(AVFrame *pic){
514 memset(pic, 0, sizeof(AVFrame));
516 pic->pts= AV_NOPTS_VALUE;
517 pic->key_frame= 1;
521 * allocates a AVPFrame and set it to defaults.
522 * this can be deallocated by simply calling free()
524 AVFrame *avcodec_alloc_frame(void){
525 AVFrame *pic= av_malloc(sizeof(AVFrame));
527 if(pic==NULL) return NULL;
529 avcodec_get_frame_defaults(pic);
531 return pic;
534 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
536 int ret= -1;
538 entangled_thread_counter++;
539 if(entangled_thread_counter != 1){
540 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
541 goto end;
544 if(avctx->codec)
545 goto end;
547 avctx->codec = codec;
548 avctx->codec_id = codec->id;
549 avctx->frame_number = 0;
550 if (codec->priv_data_size > 0) {
551 avctx->priv_data = av_mallocz(codec->priv_data_size);
552 if (!avctx->priv_data)
553 goto end;
554 } else {
555 avctx->priv_data = NULL;
558 if(avctx->coded_width && avctx->coded_height)
559 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
560 else if(avctx->width && avctx->height)
561 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
563 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
564 av_freep(&avctx->priv_data);
565 goto end;
568 ret = avctx->codec->init(avctx);
569 if (ret < 0) {
570 av_freep(&avctx->priv_data);
571 goto end;
573 ret=0;
574 end:
575 entangled_thread_counter--;
576 return ret;
579 int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
580 const short *samples)
582 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
583 av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
584 return -1;
586 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
587 int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
588 avctx->frame_number++;
589 return ret;
590 }else
591 return 0;
594 int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
595 const AVFrame *pict)
597 if(buf_size < FF_MIN_BUFFER_SIZE){
598 av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
599 return -1;
601 if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
602 return -1;
603 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
604 int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
605 avctx->frame_number++;
606 emms_c(); //needed to avoid an emms_c() call before every return;
608 return ret;
609 }else
610 return 0;
613 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
614 const AVSubtitle *sub)
616 int ret;
617 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
618 avctx->frame_number++;
619 return ret;
622 /**
623 * decode a frame.
624 * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
625 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
626 * @param buf_size the size of the buffer in bytes
627 * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
628 * @return -1 if error, otherwise return the number of
629 * bytes used.
631 int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
632 int *got_picture_ptr,
633 uint8_t *buf, int buf_size)
635 int ret;
637 *got_picture_ptr= 0;
638 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
639 return -1;
640 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
641 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
642 buf, buf_size);
644 emms_c(); //needed to avoid an emms_c() call before every return;
646 if (*got_picture_ptr)
647 avctx->frame_number++;
648 }else
649 ret= 0;
651 return ret;
654 /* decode an audio frame. return -1 if error, otherwise return the
655 *number of bytes used. If no frame could be decompressed,
656 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
657 *size in BYTES. */
658 int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
659 int *frame_size_ptr,
660 uint8_t *buf, int buf_size)
662 int ret;
664 *frame_size_ptr= 0;
665 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
666 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
667 buf, buf_size);
668 avctx->frame_number++;
669 }else
670 ret= 0;
671 return ret;
674 /* decode a subtitle message. return -1 if error, otherwise return the
675 *number of bytes used. If no subtitle could be decompressed,
676 *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */
677 int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
678 int *got_sub_ptr,
679 const uint8_t *buf, int buf_size)
681 int ret;
683 *got_sub_ptr = 0;
684 ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
685 (uint8_t *)buf, buf_size);
686 if (*got_sub_ptr)
687 avctx->frame_number++;
688 return ret;
691 int avcodec_close(AVCodecContext *avctx)
693 entangled_thread_counter++;
694 if(entangled_thread_counter != 1){
695 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
696 entangled_thread_counter--;
697 return -1;
700 if (avctx->codec->close)
701 avctx->codec->close(avctx);
702 avcodec_default_free_buffers(avctx);
703 av_freep(&avctx->priv_data);
704 avctx->codec = NULL;
705 entangled_thread_counter--;
706 return 0;
709 AVCodec *avcodec_find_encoder(enum CodecID id)
711 AVCodec *p;
712 p = first_avcodec;
713 while (p) {
714 if (p->encode != NULL && p->id == id)
715 return p;
716 p = p->next;
718 return NULL;
721 AVCodec *avcodec_find_encoder_by_name(const char *name)
723 AVCodec *p;
724 p = first_avcodec;
725 while (p) {
726 if (p->encode != NULL && strcmp(name,p->name) == 0)
727 return p;
728 p = p->next;
730 return NULL;
733 AVCodec *avcodec_find_decoder(enum CodecID id)
735 AVCodec *p;
736 p = first_avcodec;
737 while (p) {
738 if (p->decode != NULL && p->id == id)
739 return p;
740 p = p->next;
742 return NULL;
745 AVCodec *avcodec_find_decoder_by_name(const char *name)
747 AVCodec *p;
748 p = first_avcodec;
749 while (p) {
750 if (p->decode != NULL && strcmp(name,p->name) == 0)
751 return p;
752 p = p->next;
754 return NULL;
757 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
759 const char *codec_name;
760 AVCodec *p;
761 char buf1[32];
762 char channels_str[100];
763 int bitrate;
765 if (encode)
766 p = avcodec_find_encoder(enc->codec_id);
767 else
768 p = avcodec_find_decoder(enc->codec_id);
770 if (p) {
771 codec_name = p->name;
772 if (!encode && enc->codec_id == CODEC_ID_MP3) {
773 if (enc->sub_id == 2)
774 codec_name = "mp2";
775 else if (enc->sub_id == 1)
776 codec_name = "mp1";
778 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
779 /* fake mpeg2 transport stream codec (currently not
780 registered) */
781 codec_name = "mpeg2ts";
782 } else if (enc->codec_name[0] != '\0') {
783 codec_name = enc->codec_name;
784 } else {
785 /* output avi tags */
786 if (enc->codec_type == CODEC_TYPE_VIDEO) {
787 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
788 enc->codec_tag & 0xff,
789 (enc->codec_tag >> 8) & 0xff,
790 (enc->codec_tag >> 16) & 0xff,
791 (enc->codec_tag >> 24) & 0xff);
792 } else {
793 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
795 codec_name = buf1;
798 switch(enc->codec_type) {
799 case CODEC_TYPE_VIDEO:
800 snprintf(buf, buf_size,
801 "Video: %s%s",
802 codec_name, enc->mb_decision ? " (hq)" : "");
803 if (enc->pix_fmt != PIX_FMT_NONE) {
804 snprintf(buf + strlen(buf), buf_size - strlen(buf),
805 ", %s",
806 avcodec_get_pix_fmt_name(enc->pix_fmt));
808 if (enc->width) {
809 snprintf(buf + strlen(buf), buf_size - strlen(buf),
810 ", %dx%d, %0.2f fps",
811 enc->width, enc->height,
812 1/av_q2d(enc->time_base));
814 if (encode) {
815 snprintf(buf + strlen(buf), buf_size - strlen(buf),
816 ", q=%d-%d", enc->qmin, enc->qmax);
818 bitrate = enc->bit_rate;
819 break;
820 case CODEC_TYPE_AUDIO:
821 snprintf(buf, buf_size,
822 "Audio: %s",
823 codec_name);
824 switch (enc->channels) {
825 case 1:
826 strcpy(channels_str, "mono");
827 break;
828 case 2:
829 strcpy(channels_str, "stereo");
830 break;
831 case 6:
832 strcpy(channels_str, "5:1");
833 break;
834 default:
835 snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
836 break;
838 if (enc->sample_rate) {
839 snprintf(buf + strlen(buf), buf_size - strlen(buf),
840 ", %d Hz, %s",
841 enc->sample_rate,
842 channels_str);
845 /* for PCM codecs, compute bitrate directly */
846 switch(enc->codec_id) {
847 case CODEC_ID_PCM_S16LE:
848 case CODEC_ID_PCM_S16BE:
849 case CODEC_ID_PCM_U16LE:
850 case CODEC_ID_PCM_U16BE:
851 bitrate = enc->sample_rate * enc->channels * 16;
852 break;
853 case CODEC_ID_PCM_S8:
854 case CODEC_ID_PCM_U8:
855 case CODEC_ID_PCM_ALAW:
856 case CODEC_ID_PCM_MULAW:
857 bitrate = enc->sample_rate * enc->channels * 8;
858 break;
859 default:
860 bitrate = enc->bit_rate;
861 break;
863 break;
864 case CODEC_TYPE_DATA:
865 snprintf(buf, buf_size, "Data: %s", codec_name);
866 bitrate = enc->bit_rate;
867 break;
868 case CODEC_TYPE_SUBTITLE:
869 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
870 bitrate = enc->bit_rate;
871 break;
872 default:
873 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
874 return;
876 if (encode) {
877 if (enc->flags & CODEC_FLAG_PASS1)
878 snprintf(buf + strlen(buf), buf_size - strlen(buf),
879 ", pass 1");
880 if (enc->flags & CODEC_FLAG_PASS2)
881 snprintf(buf + strlen(buf), buf_size - strlen(buf),
882 ", pass 2");
884 if (bitrate != 0) {
885 snprintf(buf + strlen(buf), buf_size - strlen(buf),
886 ", %d kb/s", bitrate / 1000);
890 unsigned avcodec_version( void )
892 return LIBAVCODEC_VERSION_INT;
895 unsigned avcodec_build( void )
897 return LIBAVCODEC_BUILD;
900 /* must be called before any other functions */
901 void avcodec_init(void)
903 static int inited = 0;
905 if (inited != 0)
906 return;
907 inited = 1;
909 dsputil_static_init();
913 * Flush buffers, should be called when seeking or when swicthing to a different stream.
915 void avcodec_flush_buffers(AVCodecContext *avctx)
917 if(avctx->codec->flush)
918 avctx->codec->flush(avctx);
921 void avcodec_default_free_buffers(AVCodecContext *s){
922 int i, j;
924 if(s->internal_buffer==NULL) return;
926 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
927 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
928 for(j=0; j<4; j++){
929 av_freep(&buf->base[j]);
930 buf->data[j]= NULL;
933 av_freep(&s->internal_buffer);
935 s->internal_buffer_count=0;
938 char av_get_pict_type_char(int pict_type){
939 switch(pict_type){
940 case I_TYPE: return 'I';
941 case P_TYPE: return 'P';
942 case B_TYPE: return 'B';
943 case S_TYPE: return 'S';
944 case SI_TYPE:return 'i';
945 case SP_TYPE:return 'p';
946 default: return '?';
950 int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
951 AVRational a0={0,1}, a1={1,0};
952 int sign= (nom<0) ^ (den<0);
953 int64_t gcd= ff_gcd(ABS(nom), ABS(den));
955 nom = ABS(nom)/gcd;
956 den = ABS(den)/gcd;
957 if(nom<=max && den<=max){
958 a1= (AVRational){nom, den};
959 den=0;
962 while(den){
963 int64_t x = nom / den;
964 int64_t next_den= nom - den*x;
965 int64_t a2n= x*a1.num + a0.num;
966 int64_t a2d= x*a1.den + a0.den;
968 if(a2n > max || a2d > max) break;
970 a0= a1;
971 a1= (AVRational){a2n, a2d};
972 nom= den;
973 den= next_den;
975 assert(ff_gcd(a1.num, a1.den) == 1);
977 *dst_nom = sign ? -a1.num : a1.num;
978 *dst_den = a1.den;
980 return den==0;
983 int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
984 AVInteger ai;
985 int64_t r=0;
986 assert(c > 0);
987 assert(b >=0);
988 assert(rnd >=0 && rnd<=5 && rnd!=4);
990 if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1));
992 if(rnd==AV_ROUND_NEAR_INF) r= c/2;
993 else if(rnd&1) r= c-1;
995 if(b<=INT_MAX && c<=INT_MAX){
996 if(a<=INT_MAX)
997 return (a * b + r)/c;
998 else
999 return a/c*b + (a%c*b + r)/c;
1002 ai= av_mul_i(av_int2i(a), av_int2i(b));
1003 ai= av_add_i(ai, av_int2i(r));
1005 return av_i2int(av_div_i(ai, av_int2i(c)));
1008 int64_t av_rescale(int64_t a, int64_t b, int64_t c){
1009 return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
1012 int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
1013 int64_t b= bq.num * (int64_t)cq.den;
1014 int64_t c= cq.num * (int64_t)bq.den;
1015 return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
1018 int64_t ff_gcd(int64_t a, int64_t b){
1019 if(b) return ff_gcd(b, a%b);
1020 else return a;
1023 double av_int2dbl(int64_t v){
1024 if(v+v > 0xFFELLU<<52)
1025 return 0.0/0.0;
1026 return ldexp(((v&(1LL<<52)-1) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
1029 float av_int2flt(int32_t v){
1030 if(v+v > 0xFF000000U)
1031 return 0.0/0.0;
1032 return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
1035 int64_t av_dbl2int(double d){
1036 int e;
1037 if ( !d) return 0;
1038 else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
1039 d= frexp(d, &e);
1040 return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
1043 int32_t av_flt2int(float d){
1044 int e;
1045 if ( !d) return 0;
1046 else if(d-d) return 0x7F800000 + ((d<0)<<31) + (d!=d);
1047 d= frexp(d, &e);
1048 return (d<0)<<31 | (e+126)<<23 | (int64_t)((fabs(d)-0.5)*(1<<24));
1051 /* av_log API */
1053 static int av_log_level = AV_LOG_INFO;
1055 static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
1057 static int print_prefix=1;
1058 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
1059 if(level>av_log_level)
1060 return;
1061 #undef fprintf
1062 if(print_prefix && avc) {
1063 fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
1065 #define fprintf please_use_av_log
1067 print_prefix= strstr(fmt, "\n") != NULL;
1069 vfprintf(stderr, fmt, vl);
1072 static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
1074 void av_log(void* avcl, int level, const char *fmt, ...)
1076 va_list vl;
1077 va_start(vl, fmt);
1078 av_vlog(avcl, level, fmt, vl);
1079 va_end(vl);
1082 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
1084 av_log_callback(avcl, level, fmt, vl);
1087 int av_log_get_level(void)
1089 return av_log_level;
1092 void av_log_set_level(int level)
1094 av_log_level = level;
1097 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
1099 av_log_callback = callback;
1102 #if !defined(HAVE_THREADS)
1103 int avcodec_thread_init(AVCodecContext *s, int thread_count){
1104 return -1;
1106 #endif
1108 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1110 unsigned int n = 0;
1112 while(v >= 0xff) {
1113 *s++ = 0xff;
1114 v -= 0xff;
1115 n++;
1117 *s = v;
1118 n++;
1119 return n;