setting timebase in the encoder doesnt seem like a good idea
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
ff4ec49e 3 * Copyright (c) 2001 Fabrice Bellard.
9b879566 4 * Copyright (c) 2003 Michel Bardiaux for the av_log API
8f2ab833 5 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 6 *
ff4ec49e
FB
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.
de6d9b64 11 *
ff4ec49e 12 * This library is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e
FB
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
de6d9b64 20 */
983e3246
MN
21
22/**
23 * @file utils.c
24 * utils.
25 */
26
de6d9b64 27#include "avcodec.h"
3123dd79 28#include "dsputil.h"
1c2a8c7f 29#include "mpegvideo.h"
4c263142 30#include "integer.h"
9b879566 31#include <stdarg.h>
4c263142 32#include <limits.h>
de6d9b64 33
44608ac7
RG
34const uint8_t ff_reverse[256]={
350x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
360x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
370x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
380x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
390x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
400x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
410x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
420x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
430x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
440x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
450x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
460x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
470x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
480x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
490x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
500x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
51};
52
ddebfb15
MN
53static int volatile entangled_thread_counter=0;
54
b100eab8 55void avcodec_default_free_buffers(AVCodecContext *s);
eea8c08f 56
18f77016 57void *av_mallocz(unsigned int size)
3123dd79
FB
58{
59 void *ptr;
5b20b732 60
3123dd79
FB
61 ptr = av_malloc(size);
62 if (!ptr)
63 return NULL;
64 memset(ptr, 0, size);
65 return ptr;
66}
67
8e1e6f31
FB
68char *av_strdup(const char *s)
69{
70 char *ptr;
71 int len;
72 len = strlen(s) + 1;
73 ptr = av_malloc(len);
74 if (!ptr)
75 return NULL;
76 memcpy(ptr, s, len);
77 return ptr;
78}
79
80/**
81 * realloc which does nothing if the block is large enough
82 */
5c91a675 83void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
8e1e6f31
FB
84{
85 if(min_size < *size)
86 return ptr;
87
0ecca7a4 88 *size= FFMAX(17*min_size/16 + 32, min_size);
8e1e6f31
FB
89
90 return av_realloc(ptr, *size);
91}
92
93
855ea723 94static unsigned int last_static = 0;
8d1f2ba5
MN
95static unsigned int allocated_static = 0;
96static void** array_static = NULL;
8d1f2ba5
MN
97
98/**
99 * allocation of static arrays - do not use for normal allocation.
100 */
101void *av_mallocz_static(unsigned int size)
855ea723 102{
855ea723 103 void *ptr = av_mallocz(size);
855ea723 104
8d1f2ba5 105 if(ptr){
4e39ab4c 106 array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
0ecca7a4
MN
107 if(!array_static)
108 return NULL;
8d1f2ba5 109 array_static[last_static++] = ptr;
855ea723 110 }
8d1f2ba5 111
855ea723
ZK
112 return ptr;
113}
8d1f2ba5
MN
114
115/**
073c2593
BP
116 * same as above, but does realloc
117 */
118
119void *av_realloc_static(void *ptr, unsigned int size)
120{
121 int i;
122 if(!ptr)
123 return av_mallocz_static(size);
124 /* Look for the old ptr */
125 for(i = 0; i < last_static; i++) {
126 if(array_static[i] == ptr) {
127 array_static[i] = av_realloc(array_static[i], size);
128 return array_static[i];
129 }
130 }
131 return NULL;
132
133}
134
135/**
8d1f2ba5
MN
136 * free all static arrays and reset pointers to 0.
137 */
4cfbf61b 138void av_free_static(void)
855ea723 139{
8d1f2ba5
MN
140 while(last_static){
141 av_freep(&array_static[--last_static]);
855ea723 142 }
8d1f2ba5 143 av_freep(&array_static);
855ea723
ZK
144}
145
81c5f887 146/**
32fbf33e
BP
147 * Call av_free_static automatically before it's too late
148 */
149
150static void do_free() __attribute__ ((destructor));
151
152static void do_free()
153{
154 av_free_static();
155}
156
157/**
81c5f887
MN
158 * Frees memory and sets the pointer to NULL.
159 * @param arg pointer to the pointer which should be freed
160 */
161void av_freep(void *arg)
4d7a0a05 162{
81c5f887 163 void **ptr= (void**)arg;
4d7a0a05
FB
164 av_free(*ptr);
165 *ptr = NULL;
166}
167
de6d9b64 168/* encoder management */
8b69867f 169AVCodec *first_avcodec = NULL;
de6d9b64
FB
170
171void register_avcodec(AVCodec *format)
172{
173 AVCodec **p;
174 p = &first_avcodec;
175 while (*p != NULL) p = &(*p)->next;
176 *p = format;
177 format->next = NULL;
178}
179
21adafec
MN
180void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
181 s->coded_width = width;
182 s->coded_height= height;
183 s->width = -((-width )>>s->lowres);
184 s->height= -((-height)>>s->lowres);
185}
186
d90cf87b 187typedef struct InternalBuffer{
1e491e29 188 int last_pic_num;
d90cf87b 189 uint8_t *base[4];
1e491e29 190 uint8_t *data[4];
237e4938 191 int linesize[4];
d90cf87b
MN
192}InternalBuffer;
193
194#define INTERNAL_BUFFER_SIZE 32
1e491e29 195
f0bbfc4a
MN
196#define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
197
198void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
199 int w_align= 1;
200 int h_align= 1;
201
202 switch(s->pix_fmt){
203 case PIX_FMT_YUV420P:
204 case PIX_FMT_YUV422:
ebb177dd 205 case PIX_FMT_UYVY422:
f0bbfc4a
MN
206 case PIX_FMT_YUV422P:
207 case PIX_FMT_YUV444P:
208 case PIX_FMT_GRAY8:
209 case PIX_FMT_YUVJ420P:
210 case PIX_FMT_YUVJ422P:
211 case PIX_FMT_YUVJ444P:
212 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
213 h_align= 16;
214 break;
215 case PIX_FMT_YUV411P:
f02be79d 216 case PIX_FMT_UYVY411:
f0bbfc4a
MN
217 w_align=32;
218 h_align=8;
219 break;
220 case PIX_FMT_YUV410P:
221 if(s->codec_id == CODEC_ID_SVQ1){
222 w_align=64;
223 h_align=64;
224 }
d99fbbf4
RT
225 case PIX_FMT_RGB555:
226 if(s->codec_id == CODEC_ID_RPZA){
227 w_align=4;
228 h_align=4;
229 }
230 case PIX_FMT_PAL8:
231 if(s->codec_id == CODEC_ID_SMC){
232 w_align=4;
233 h_align=4;
234 }
f0bbfc4a 235 break;
c31b8121
RT
236 case PIX_FMT_BGR24:
237 if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
238 w_align=4;
239 h_align=4;
240 }
241 break;
f0bbfc4a
MN
242 default:
243 w_align= 1;
244 h_align= 1;
245 break;
246 }
247
248 *width = ALIGN(*width , w_align);
249 *height= ALIGN(*height, h_align);
250}
251
0ecca7a4
MN
252int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
253 if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/4)
254 return 0;
255
256 av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
257 return -1;
258}
259
492cd3a9 260int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 261 int i;
f0bbfc4a
MN
262 int w= s->width;
263 int h= s->height;
d90cf87b 264 InternalBuffer *buf;
237e4938 265 int *picture_number;
0ecca7a4 266
4e00e76b 267 assert(pic->data[0]==NULL);
d90cf87b 268 assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
1e491e29 269
0ecca7a4
MN
270 if(avcodec_check_dimensions(s,w,h))
271 return -1;
272
d90cf87b
MN
273 if(s->internal_buffer==NULL){
274 s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
275 }
276#if 0
277 s->internal_buffer= av_fast_realloc(
278 s->internal_buffer,
279 &s->internal_buffer_size,
280 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
281 );
282#endif
283
284 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
237e4938
MN
285 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
286 (*picture_number)++;
287
d90cf87b 288 if(buf->base[0]){
237e4938
MN
289 pic->age= *picture_number - buf->last_pic_num;
290 buf->last_pic_num= *picture_number;
1e491e29 291 }else{
f0bbfc4a 292 int h_chroma_shift, v_chroma_shift;
3237f731 293 int pixel_size;
1e491e29
MN
294
295 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
f0bbfc4a 296
1e491e29 297 switch(s->pix_fmt){
ba88675b
MN
298 case PIX_FMT_RGB555:
299 case PIX_FMT_RGB565:
1e491e29 300 case PIX_FMT_YUV422:
ebb177dd 301 case PIX_FMT_UYVY422:
1e491e29
MN
302 pixel_size=2;
303 break;
304 case PIX_FMT_RGB24:
305 case PIX_FMT_BGR24:
306 pixel_size=3;
307 break;
1e491e29
MN
308 case PIX_FMT_RGBA32:
309 pixel_size=4;
310 break;
311 default:
312 pixel_size=1;
313 }
f0bbfc4a
MN
314
315 avcodec_align_dimensions(s, &w, &h);
f0bbfc4a 316
1e491e29
MN
317 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
318 w+= EDGE_WIDTH*2;
319 h+= EDGE_WIDTH*2;
320 }
321
d90cf87b 322 buf->last_pic_num= -256*256*256*64;
1e491e29
MN
323
324 for(i=0; i<3; i++){
2c19981a
MN
325 const int h_shift= i==0 ? 0 : h_chroma_shift;
326 const int v_shift= i==0 ? 0 : v_chroma_shift;
1e491e29 327
7984082a 328 //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
3237f731 329 buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, STRIDE_ALIGN<<(h_chroma_shift-h_shift));
1e491e29 330
f41c1fac 331 buf->base[i]= av_malloc((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
d90cf87b 332 if(buf->base[i]==NULL) return -1;
237e4938 333 memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
1e491e29
MN
334
335 if(s->flags&CODEC_FLAG_EMU_EDGE)
d90cf87b 336 buf->data[i] = buf->base[i];
1e491e29 337 else
3237f731 338 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), STRIDE_ALIGN);
1e491e29
MN
339 }
340 pic->age= 256*256*256*64;
341 }
237e4938 342 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29 343
d90cf87b
MN
344 for(i=0; i<4; i++){
345 pic->base[i]= buf->base[i];
346 pic->data[i]= buf->data[i];
237e4938 347 pic->linesize[i]= buf->linesize[i];
d90cf87b
MN
348 }
349 s->internal_buffer_count++;
350
1e491e29
MN
351 return 0;
352}
353
492cd3a9 354void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 355 int i;
d90cf87b
MN
356 InternalBuffer *buf, *last, temp;
357
4e00e76b 358 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
59c673d5 359 assert(s->internal_buffer_count);
d90cf87b 360
b1609412 361 buf = NULL; /* avoids warning */
d90cf87b
MN
362 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
363 buf= &((InternalBuffer*)s->internal_buffer)[i];
364 if(buf->data[0] == pic->data[0])
365 break;
366 }
367 assert(i < s->internal_buffer_count);
368 s->internal_buffer_count--;
369 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
370
371 temp= *buf;
372 *buf= *last;
373 *last= temp;
374
375 for(i=0; i<3; i++){
1e491e29 376 pic->data[i]=NULL;
d90cf87b
MN
377// pic->base[i]=NULL;
378 }
1e491e29
MN
379//printf("R%X\n", pic->opaque);
380}
381
e1c2a5a0
RT
382int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
383 AVFrame temp_pic;
384 int i;
385
386 /* If no picture return a new buffer */
387 if(pic->data[0] == NULL) {
388 /* We will copy from buffer, so must be readable */
389 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
390 return s->get_buffer(s, pic);
391 }
392
393 /* If internal buffer type return the same buffer */
394 if(pic->type == FF_BUFFER_TYPE_INTERNAL)
395 return 0;
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 img_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
9c3d33d6
MN
414int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
415 int i;
416
417 for(i=0; i<count; i++){
418 int r= func(c, arg[i]);
419 if(ret) ret[i]= r;
420 }
421 return 0;
422}
423
494c56d3 424enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
a33c7159
MN
425 return fmt[0];
426}
427
43465395
MN
428static const char* context_to_name(void* ptr) {
429 AVCodecContext *avc= ptr;
430
431 if(avc && avc->codec && avc->codec->name)
432 return avc->codec->name;
433 else
434 return "NULL";
435}
436
437static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
438
a949d72e 439void avcodec_get_context_defaults(AVCodecContext *s){
9740beff
MN
440 memset(s, 0, sizeof(AVCodecContext));
441
43465395 442 s->av_class= &av_codec_context_class;
e8b62df6
MN
443 s->bit_rate= 800*1000;
444 s->bit_rate_tolerance= s->bit_rate*10;
a949d72e
MN
445 s->qmin= 2;
446 s->qmax= 31;
6e0d8c06
MN
447 s->mb_lmin= FF_QP2LAMBDA * 2;
448 s->mb_lmax= FF_QP2LAMBDA * 31;
a949d72e
MN
449 s->rc_eq= "tex^qComp";
450 s->qcompress= 0.5;
e8b62df6
MN
451 s->max_qdiff= 3;
452 s->b_quant_factor=1.25;
453 s->b_quant_offset=1.25;
b3a391e8 454 s->i_quant_factor=-0.8;
e8b62df6 455 s->i_quant_offset=0.0;
4d2858de 456 s->error_concealment= 3;
8d0e42ca 457 s->error_resilience= 1;
4d2858de 458 s->workaround_bugs= FF_BUG_AUTODETECT;
c0df9d75 459 s->time_base= (AVRational){0,1};
8d0e42ca
MN
460 s->gop_size= 50;
461 s->me_method= ME_EPZS;
1e491e29
MN
462 s->get_buffer= avcodec_default_get_buffer;
463 s->release_buffer= avcodec_default_release_buffer;
a33c7159 464 s->get_format= avcodec_default_get_format;
9c3d33d6
MN
465 s->execute= avcodec_default_execute;
466 s->thread_count=1;
826f429a 467 s->me_subpel_quality=8;
158c7f05
MN
468 s->lmin= FF_QP2LAMBDA * s->qmin;
469 s->lmax= FF_QP2LAMBDA * s->qmax;
5ff85f1d 470 s->sample_aspect_ratio= (AVRational){0,1};
620fe604 471 s->ildct_cmp= FF_CMP_VSAD;
baced9f5
MN
472 s->profile= FF_PROFILE_UNKNOWN;
473 s->level= FF_LEVEL_UNKNOWN;
09c3e44e 474 s->me_penalty_compensation= 256;
644a9262 475 s->pix_fmt= PIX_FMT_NONE;
1984f635
MN
476
477 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
478 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
2a2bbcb0 479 s->palctrl = NULL;
e1c2a5a0 480 s->reget_buffer= avcodec_default_reget_buffer;
a949d72e
MN
481}
482
483/**
484 * allocates a AVCodecContext and set it to defaults.
485 * this can be deallocated by simply calling free()
486 */
7ffbb60e 487AVCodecContext *avcodec_alloc_context(void){
9740beff 488 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
a949d72e
MN
489
490 if(avctx==NULL) return NULL;
491
492 avcodec_get_context_defaults(avctx);
493
494 return avctx;
495}
496
9740beff
MN
497void avcodec_get_frame_defaults(AVFrame *pic){
498 memset(pic, 0, sizeof(AVFrame));
499
500 pic->pts= AV_NOPTS_VALUE;
c342499d 501 pic->key_frame= 1;
9740beff
MN
502}
503
1e491e29 504/**
492cd3a9 505 * allocates a AVPFrame and set it to defaults.
1e491e29
MN
506 * this can be deallocated by simply calling free()
507 */
492cd3a9 508AVFrame *avcodec_alloc_frame(void){
9740beff
MN
509 AVFrame *pic= av_malloc(sizeof(AVFrame));
510
511 if(pic==NULL) return NULL;
512
513 avcodec_get_frame_defaults(pic);
1e491e29
MN
514
515 return pic;
516}
517
de6d9b64
FB
518int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
519{
ddebfb15
MN
520 int ret= -1;
521
522 entangled_thread_counter++;
523 if(entangled_thread_counter != 1){
524 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
525 goto end;
526 }
de6d9b64 527
09770af8 528 if(avctx->codec)
ddebfb15 529 goto end;
09770af8 530
de6d9b64 531 avctx->codec = codec;
4cb3ca72 532 avctx->codec_id = codec->id;
de6d9b64 533 avctx->frame_number = 0;
0edf8a7a
PG
534 if (codec->priv_data_size > 0) {
535 avctx->priv_data = av_mallocz(codec->priv_data_size);
536 if (!avctx->priv_data)
ddebfb15 537 goto end;
0edf8a7a
PG
538 } else {
539 avctx->priv_data = NULL;
540 }
21adafec
MN
541
542 if(avctx->coded_width && avctx->coded_height)
543 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
544 else if(avctx->width && avctx->height)
545 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
546
0ecca7a4
MN
547 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
548 av_freep(&avctx->priv_data);
ddebfb15 549 goto end;
0ecca7a4
MN
550 }
551
de6d9b64
FB
552 ret = avctx->codec->init(avctx);
553 if (ret < 0) {
3123dd79 554 av_freep(&avctx->priv_data);
ddebfb15 555 goto end;
de6d9b64 556 }
ddebfb15
MN
557 ret=0;
558end:
559 entangled_thread_counter--;
560 return ret;
de6d9b64
FB
561}
562
0c1a9eda 563int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
de6d9b64
FB
564 const short *samples)
565{
0ecca7a4
MN
566 if(buf_size < FF_MIN_BUFFER_SIZE && 0){
567 av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
568 return -1;
569 }
6f824977
MN
570 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
571 int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
572 avctx->frame_number++;
573 return ret;
574 }else
575 return 0;
de6d9b64
FB
576}
577
0c1a9eda 578int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 579 const AVFrame *pict)
de6d9b64 580{
0ecca7a4
MN
581 if(buf_size < FF_MIN_BUFFER_SIZE){
582 av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");
583 return -1;
584 }
585 if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
586 return -1;
6f824977
MN
587 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
588 int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
589 avctx->frame_number++;
bb628dae 590 emms_c(); //needed to avoid an emms_c() call before every return;
6bb925f4 591
6f824977
MN
592 return ret;
593 }else
594 return 0;
de6d9b64
FB
595}
596
240c1657
FB
597int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
598 const AVSubtitle *sub)
599{
600 int ret;
601 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);
602 avctx->frame_number++;
603 return ret;
604}
605
f138f883
MN
606/**
607 * decode a frame.
608 * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
609 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
610 * @param buf_size the size of the buffer in bytes
611 * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
612 * @return -1 if error, otherwise return the number of
613 * bytes used.
614 */
492cd3a9 615int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
de6d9b64 616 int *got_picture_ptr,
0c1a9eda 617 uint8_t *buf, int buf_size)
de6d9b64
FB
618{
619 int ret;
1e491e29 620
53db1cae 621 *got_picture_ptr= 0;
0ecca7a4
MN
622 if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
623 return -1;
934982c4
MN
624 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
625 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
626 buf, buf_size);
6bb925f4 627
bb628dae 628 emms_c(); //needed to avoid an emms_c() call before every return;
1e491e29 629
934982c4
MN
630 if (*got_picture_ptr)
631 avctx->frame_number++;
632 }else
633 ret= 0;
634
de6d9b64
FB
635 return ret;
636}
637
638/* decode an audio frame. return -1 if error, otherwise return the
639 *number of bytes used. If no frame could be decompressed,
640 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
641 *size in BYTES. */
0c1a9eda 642int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
de6d9b64 643 int *frame_size_ptr,
0c1a9eda 644 uint8_t *buf, int buf_size)
de6d9b64
FB
645{
646 int ret;
647
53db1cae 648 *frame_size_ptr= 0;
fec9ccb7
MN
649 if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
650 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
651 buf, buf_size);
652 avctx->frame_number++;
653 }else
654 ret= 0;
de6d9b64
FB
655 return ret;
656}
657
240c1657
FB
658/* decode a subtitle message. return -1 if error, otherwise return the
659 *number of bytes used. If no subtitle could be decompressed,
660 *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */
661int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
662 int *got_sub_ptr,
663 const uint8_t *buf, int buf_size)
664{
665 int ret;
666
667 *got_sub_ptr = 0;
668 ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
669 (uint8_t *)buf, buf_size);
670 if (*got_sub_ptr)
671 avctx->frame_number++;
672 return ret;
673}
674
de6d9b64
FB
675int avcodec_close(AVCodecContext *avctx)
676{
ddebfb15
MN
677 entangled_thread_counter++;
678 if(entangled_thread_counter != 1){
679 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
680 entangled_thread_counter--;
681 return -1;
682 }
683
de6d9b64
FB
684 if (avctx->codec->close)
685 avctx->codec->close(avctx);
eea8c08f 686 avcodec_default_free_buffers(avctx);
3123dd79 687 av_freep(&avctx->priv_data);
de6d9b64 688 avctx->codec = NULL;
ddebfb15 689 entangled_thread_counter--;
de6d9b64
FB
690 return 0;
691}
692
693AVCodec *avcodec_find_encoder(enum CodecID id)
694{
695 AVCodec *p;
696 p = first_avcodec;
697 while (p) {
698 if (p->encode != NULL && p->id == id)
699 return p;
700 p = p->next;
701 }
702 return NULL;
703}
704
98f3b098
A
705AVCodec *avcodec_find_encoder_by_name(const char *name)
706{
707 AVCodec *p;
708 p = first_avcodec;
709 while (p) {
710 if (p->encode != NULL && strcmp(name,p->name) == 0)
711 return p;
712 p = p->next;
713 }
714 return NULL;
715}
716
de6d9b64
FB
717AVCodec *avcodec_find_decoder(enum CodecID id)
718{
719 AVCodec *p;
720 p = first_avcodec;
721 while (p) {
722 if (p->decode != NULL && p->id == id)
723 return p;
724 p = p->next;
725 }
726 return NULL;
727}
728
729AVCodec *avcodec_find_decoder_by_name(const char *name)
730{
731 AVCodec *p;
732 p = first_avcodec;
733 while (p) {
734 if (p->decode != NULL && strcmp(name,p->name) == 0)
735 return p;
736 p = p->next;
737 }
738 return NULL;
739}
740
de6d9b64
FB
741void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
742{
743 const char *codec_name;
744 AVCodec *p;
745 char buf1[32];
623563c0 746 char channels_str[100];
a96b68b7 747 int bitrate;
de6d9b64
FB
748
749 if (encode)
750 p = avcodec_find_encoder(enc->codec_id);
751 else
752 p = avcodec_find_decoder(enc->codec_id);
753
754 if (p) {
755 codec_name = p->name;
98ce5991
FB
756 if (!encode && enc->codec_id == CODEC_ID_MP3) {
757 if (enc->sub_id == 2)
758 codec_name = "mp2";
759 else if (enc->sub_id == 1)
760 codec_name = "mp1";
761 }
985180a1
FB
762 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
763 /* fake mpeg2 transport stream codec (currently not
764 registered) */
765 codec_name = "mpeg2ts";
de6d9b64
FB
766 } else if (enc->codec_name[0] != '\0') {
767 codec_name = enc->codec_name;
768 } else {
769 /* output avi tags */
770 if (enc->codec_type == CODEC_TYPE_VIDEO) {
771 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
772 enc->codec_tag & 0xff,
773 (enc->codec_tag >> 8) & 0xff,
774 (enc->codec_tag >> 16) & 0xff,
775 (enc->codec_tag >> 24) & 0xff);
776 } else {
777 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
778 }
779 codec_name = buf1;
780 }
781
782 switch(enc->codec_type) {
783 case CODEC_TYPE_VIDEO:
784 snprintf(buf, buf_size,
785 "Video: %s%s",
7d1c3fc1 786 codec_name, enc->mb_decision ? " (hq)" : "");
82c0c4ba 787 if (enc->pix_fmt != PIX_FMT_NONE) {
cf087595
FB
788 snprintf(buf + strlen(buf), buf_size - strlen(buf),
789 ", %s",
9bbffbb1 790 avcodec_get_pix_fmt_name(enc->pix_fmt));
cf087595 791 }
de6d9b64
FB
792 if (enc->width) {
793 snprintf(buf + strlen(buf), buf_size - strlen(buf),
794 ", %dx%d, %0.2f fps",
795 enc->width, enc->height,
c0df9d75 796 1/av_q2d(enc->time_base));
de6d9b64 797 }
4bfad535
FB
798 if (encode) {
799 snprintf(buf + strlen(buf), buf_size - strlen(buf),
800 ", q=%d-%d", enc->qmin, enc->qmax);
801 }
a96b68b7 802 bitrate = enc->bit_rate;
de6d9b64
FB
803 break;
804 case CODEC_TYPE_AUDIO:
805 snprintf(buf, buf_size,
806 "Audio: %s",
807 codec_name);
e0d2714a
J
808 switch (enc->channels) {
809 case 1:
623563c0 810 strcpy(channels_str, "mono");
e0d2714a
J
811 break;
812 case 2:
623563c0 813 strcpy(channels_str, "stereo");
e0d2714a
J
814 break;
815 case 6:
623563c0 816 strcpy(channels_str, "5:1");
e0d2714a
J
817 break;
818 default:
2fc8ea24 819 snprintf(channels_str, sizeof(channels_str), "%d channels", enc->channels);
e0d2714a
J
820 break;
821 }
de6d9b64
FB
822 if (enc->sample_rate) {
823 snprintf(buf + strlen(buf), buf_size - strlen(buf),
824 ", %d Hz, %s",
825 enc->sample_rate,
e0d2714a 826 channels_str);
de6d9b64 827 }
e0d2714a 828
a96b68b7
FB
829 /* for PCM codecs, compute bitrate directly */
830 switch(enc->codec_id) {
831 case CODEC_ID_PCM_S16LE:
832 case CODEC_ID_PCM_S16BE:
833 case CODEC_ID_PCM_U16LE:
834 case CODEC_ID_PCM_U16BE:
a190b7e9 835 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
836 break;
837 case CODEC_ID_PCM_S8:
838 case CODEC_ID_PCM_U8:
839 case CODEC_ID_PCM_ALAW:
840 case CODEC_ID_PCM_MULAW:
a190b7e9 841 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
842 break;
843 default:
844 bitrate = enc->bit_rate;
845 break;
846 }
de6d9b64 847 break;
985180a1
FB
848 case CODEC_TYPE_DATA:
849 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657
FB
850 bitrate = enc->bit_rate;
851 break;
852 case CODEC_TYPE_SUBTITLE:
853 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1
FB
854 bitrate = enc->bit_rate;
855 break;
de6d9b64 856 default:
9fe5a7b8
MN
857 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
858 return;
de6d9b64 859 }
4bfad535
FB
860 if (encode) {
861 if (enc->flags & CODEC_FLAG_PASS1)
862 snprintf(buf + strlen(buf), buf_size - strlen(buf),
863 ", pass 1");
864 if (enc->flags & CODEC_FLAG_PASS2)
865 snprintf(buf + strlen(buf), buf_size - strlen(buf),
866 ", pass 2");
867 }
a96b68b7 868 if (bitrate != 0) {
de6d9b64 869 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 870 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
871 }
872}
873
156e5023
NK
874unsigned avcodec_version( void )
875{
876 return LIBAVCODEC_VERSION_INT;
877}
cf087595 878
8bceb6af
NK
879unsigned avcodec_build( void )
880{
881 return LIBAVCODEC_BUILD;
882}
883
de6d9b64
FB
884/* must be called before any other functions */
885void avcodec_init(void)
886{
0344cd0a
AB
887 static int inited = 0;
888
889 if (inited != 0)
890 return;
891 inited = 1;
892
59cf08ce 893 dsputil_static_init();
de6d9b64
FB
894}
895
7a06ff14
MN
896/**
897 * Flush buffers, should be called when seeking or when swicthing to a different stream.
898 */
1c2a8c7f
MN
899void avcodec_flush_buffers(AVCodecContext *avctx)
900{
7a06ff14
MN
901 if(avctx->codec->flush)
902 avctx->codec->flush(avctx);
1c2a8c7f
MN
903}
904
b100eab8 905void avcodec_default_free_buffers(AVCodecContext *s){
d90cf87b
MN
906 int i, j;
907
908 if(s->internal_buffer==NULL) return;
909
910 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
911 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
912 for(j=0; j<4; j++){
913 av_freep(&buf->base[j]);
914 buf->data[j]= NULL;
915 }
916 }
917 av_freep(&s->internal_buffer);
918
919 s->internal_buffer_count=0;
920}
921
d8085ea7
MN
922char av_get_pict_type_char(int pict_type){
923 switch(pict_type){
924 case I_TYPE: return 'I';
925 case P_TYPE: return 'P';
926 case B_TYPE: return 'B';
927 case S_TYPE: return 'S';
928 case SI_TYPE:return 'i';
929 case SP_TYPE:return 'p';
930 default: return '?';
931 }
932}
933
9b879566
MB
934/* av_log API */
935
e18c1f19 936static int av_log_level = AV_LOG_INFO;
9b879566 937
bc874dae 938static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
9b879566 939{
89b9441a 940 static int print_prefix=1;
43465395 941 AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
9b879566 942 if(level>av_log_level)
bc874dae 943 return;
d705e4a6 944#undef fprintf
43465395
MN
945 if(print_prefix && avc) {
946 fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
bc874dae 947 }
d705e4a6 948#define fprintf please_use_av_log
89b9441a 949
95ba2c8f 950 print_prefix= strstr(fmt, "\n") != NULL;
89b9441a 951
9b879566
MB
952 vfprintf(stderr, fmt, vl);
953}
954
bc874dae 955static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
9b879566 956
bc874dae 957void av_log(void* avcl, int level, const char *fmt, ...)
9b879566
MB
958{
959 va_list vl;
960 va_start(vl, fmt);
bc874dae 961 av_vlog(avcl, level, fmt, vl);
9b879566
MB
962 va_end(vl);
963}
964
bc874dae 965void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
9b879566 966{
bc874dae 967 av_log_callback(avcl, level, fmt, vl);
9b879566
MB
968}
969
970int av_log_get_level(void)
971{
972 return av_log_level;
973}
974
975void av_log_set_level(int level)
976{
977 av_log_level = level;
978}
979
bc874dae 980void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
9b879566
MB
981{
982 av_log_callback = callback;
983}
984
2450cff2 985#if !defined(HAVE_THREADS)
ca8ad847
MN
986int avcodec_thread_init(AVCodecContext *s, int thread_count){
987 return -1;
988}
989#endif
ad2b531d
MR
990
991unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
992{
993 unsigned int n = 0;
994
995 while(v >= 0xff) {
996 *s++ = 0xff;
997 v -= 0xff;
998 n++;
999 }
1000 *s = v;
1001 n++;
1002 return n;
1003}