av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
[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"
9b879566 30#include <stdarg.h>
de6d9b64 31
18f77016 32void *av_mallocz(unsigned int size)
3123dd79
FB
33{
34 void *ptr;
5b20b732 35
3123dd79
FB
36 ptr = av_malloc(size);
37 if (!ptr)
38 return NULL;
39 memset(ptr, 0, size);
40 return ptr;
41}
42
8e1e6f31
FB
43char *av_strdup(const char *s)
44{
45 char *ptr;
46 int len;
47 len = strlen(s) + 1;
48 ptr = av_malloc(len);
49 if (!ptr)
50 return NULL;
51 memcpy(ptr, s, len);
52 return ptr;
53}
54
55/**
56 * realloc which does nothing if the block is large enough
57 */
5c91a675 58void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
8e1e6f31
FB
59{
60 if(min_size < *size)
61 return ptr;
62
63 *size= min_size + 10*1024;
64
65 return av_realloc(ptr, *size);
66}
67
68
855ea723
ZK
69/* allocation of static arrays - do not use for normal allocation */
70static unsigned int last_static = 0;
71static char*** array_static = NULL;
72static const unsigned int grow_static = 64; // ^2
73void *__av_mallocz_static(void** location, unsigned int size)
74{
5c91a675 75 unsigned int l = (last_static + grow_static) & ~(grow_static - 1);
855ea723
ZK
76 void *ptr = av_mallocz(size);
77 if (!ptr)
78 return NULL;
79
80 if (location)
81 {
82 if (l > last_static)
8e1e6f31 83 array_static = av_realloc(array_static, l);
855ea723
ZK
84 array_static[last_static++] = (char**) location;
85 *location = ptr;
86 }
87 return ptr;
88}
89/* free all static arrays and reset pointers to 0 */
4cfbf61b 90void av_free_static(void)
855ea723
ZK
91{
92 if (array_static)
93 {
94 unsigned i;
95 for (i = 0; i < last_static; i++)
96 {
8e1e6f31 97 av_free(*array_static[i]);
855ea723
ZK
98 *array_static[i] = NULL;
99 }
8e1e6f31 100 av_free(array_static);
855ea723
ZK
101 array_static = 0;
102 }
103 last_static = 0;
104}
105
81c5f887
MN
106/**
107 * Frees memory and sets the pointer to NULL.
108 * @param arg pointer to the pointer which should be freed
109 */
110void av_freep(void *arg)
4d7a0a05 111{
81c5f887 112 void **ptr= (void**)arg;
4d7a0a05
FB
113 av_free(*ptr);
114 *ptr = NULL;
115}
116
de6d9b64
FB
117/* encoder management */
118AVCodec *first_avcodec;
119
120void register_avcodec(AVCodec *format)
121{
122 AVCodec **p;
123 p = &first_avcodec;
124 while (*p != NULL) p = &(*p)->next;
125 *p = format;
126 format->next = NULL;
127}
128
d90cf87b 129typedef struct InternalBuffer{
1e491e29 130 int last_pic_num;
d90cf87b 131 uint8_t *base[4];
1e491e29 132 uint8_t *data[4];
237e4938 133 int linesize[4];
d90cf87b
MN
134}InternalBuffer;
135
136#define INTERNAL_BUFFER_SIZE 32
1e491e29 137
f0bbfc4a
MN
138#define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
139
140void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
141 int w_align= 1;
142 int h_align= 1;
143
144 switch(s->pix_fmt){
145 case PIX_FMT_YUV420P:
146 case PIX_FMT_YUV422:
147 case PIX_FMT_YUV422P:
148 case PIX_FMT_YUV444P:
149 case PIX_FMT_GRAY8:
150 case PIX_FMT_YUVJ420P:
151 case PIX_FMT_YUVJ422P:
152 case PIX_FMT_YUVJ444P:
153 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
154 h_align= 16;
155 break;
156 case PIX_FMT_YUV411P:
157 w_align=32;
158 h_align=8;
159 break;
160 case PIX_FMT_YUV410P:
161 if(s->codec_id == CODEC_ID_SVQ1){
162 w_align=64;
163 h_align=64;
164 }
165 break;
166 default:
167 w_align= 1;
168 h_align= 1;
169 break;
170 }
171
172 *width = ALIGN(*width , w_align);
173 *height= ALIGN(*height, h_align);
174}
175
492cd3a9 176int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 177 int i;
f0bbfc4a
MN
178 int w= s->width;
179 int h= s->height;
d90cf87b 180 InternalBuffer *buf;
237e4938 181 int *picture_number;
4e00e76b
MN
182
183 assert(pic->data[0]==NULL);
d90cf87b 184 assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
1e491e29 185
d90cf87b
MN
186 if(s->internal_buffer==NULL){
187 s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
188 }
189#if 0
190 s->internal_buffer= av_fast_realloc(
191 s->internal_buffer,
192 &s->internal_buffer_size,
193 sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
194 );
195#endif
196
197 buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
237e4938
MN
198 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
199 (*picture_number)++;
200
d90cf87b 201 if(buf->base[0]){
237e4938
MN
202 pic->age= *picture_number - buf->last_pic_num;
203 buf->last_pic_num= *picture_number;
1e491e29 204 }else{
f0bbfc4a
MN
205 int h_chroma_shift, v_chroma_shift;
206 int s_align, pixel_size;
1e491e29
MN
207
208 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
f0bbfc4a 209
1e491e29 210 switch(s->pix_fmt){
ba88675b
MN
211 case PIX_FMT_RGB555:
212 case PIX_FMT_RGB565:
1e491e29
MN
213 case PIX_FMT_YUV422:
214 pixel_size=2;
215 break;
216 case PIX_FMT_RGB24:
217 case PIX_FMT_BGR24:
218 pixel_size=3;
219 break;
1e491e29
MN
220 case PIX_FMT_RGBA32:
221 pixel_size=4;
222 break;
223 default:
224 pixel_size=1;
225 }
f0bbfc4a
MN
226
227 avcodec_align_dimensions(s, &w, &h);
228#if defined(ARCH_POWERPC) || defined(HAVE_MMI) //FIXME some cleaner check
229 s_align= 16;
230#else
231 s_align= 8;
232#endif
233
1e491e29
MN
234 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
235 w+= EDGE_WIDTH*2;
236 h+= EDGE_WIDTH*2;
237 }
238
d90cf87b 239 buf->last_pic_num= -256*256*256*64;
1e491e29
MN
240
241 for(i=0; i<3; i++){
2c19981a
MN
242 const int h_shift= i==0 ? 0 : h_chroma_shift;
243 const int v_shift= i==0 ? 0 : v_chroma_shift;
1e491e29 244
7984082a
MN
245 //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
246 buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, s_align<<(h_chroma_shift-h_shift));
1e491e29 247
237e4938 248 buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
d90cf87b 249 if(buf->base[i]==NULL) return -1;
237e4938 250 memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
1e491e29
MN
251
252 if(s->flags&CODEC_FLAG_EMU_EDGE)
d90cf87b 253 buf->data[i] = buf->base[i];
1e491e29 254 else
237e4938 255 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
1e491e29
MN
256 }
257 pic->age= 256*256*256*64;
258 }
237e4938 259 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29 260
d90cf87b
MN
261 for(i=0; i<4; i++){
262 pic->base[i]= buf->base[i];
263 pic->data[i]= buf->data[i];
237e4938 264 pic->linesize[i]= buf->linesize[i];
d90cf87b
MN
265 }
266 s->internal_buffer_count++;
267
1e491e29
MN
268 return 0;
269}
270
492cd3a9 271void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29 272 int i;
d90cf87b
MN
273 InternalBuffer *buf, *last, temp;
274
4e00e76b 275 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
59c673d5 276 assert(s->internal_buffer_count);
d90cf87b 277
b1609412 278 buf = NULL; /* avoids warning */
d90cf87b
MN
279 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
280 buf= &((InternalBuffer*)s->internal_buffer)[i];
281 if(buf->data[0] == pic->data[0])
282 break;
283 }
284 assert(i < s->internal_buffer_count);
285 s->internal_buffer_count--;
286 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
287
288 temp= *buf;
289 *buf= *last;
290 *last= temp;
291
292 for(i=0; i<3; i++){
1e491e29 293 pic->data[i]=NULL;
d90cf87b
MN
294// pic->base[i]=NULL;
295 }
1e491e29
MN
296//printf("R%X\n", pic->opaque);
297}
298
e1c2a5a0
RT
299int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
300 AVFrame temp_pic;
301 int i;
302
303 /* If no picture return a new buffer */
304 if(pic->data[0] == NULL) {
305 /* We will copy from buffer, so must be readable */
306 pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
307 return s->get_buffer(s, pic);
308 }
309
310 /* If internal buffer type return the same buffer */
311 if(pic->type == FF_BUFFER_TYPE_INTERNAL)
312 return 0;
313
314 /*
315 * Not internal type and reget_buffer not overridden, emulate cr buffer
316 */
317 temp_pic = *pic;
318 for(i = 0; i < 4; i++)
319 pic->data[i] = pic->base[i] = NULL;
320 pic->opaque = NULL;
321 /* Allocate new frame */
322 if (s->get_buffer(s, pic))
323 return -1;
324 /* Copy image data from old buffer to new buffer */
325 img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
326 s->height);
327 s->release_buffer(s, &temp_pic); // Release old frame
328 return 0;
329}
330
9c3d33d6
MN
331int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
332 int i;
333
334 for(i=0; i<count; i++){
335 int r= func(c, arg[i]);
336 if(ret) ret[i]= r;
337 }
338 return 0;
339}
340
a33c7159
MN
341enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
342 return fmt[0];
343}
344
a949d72e 345void avcodec_get_context_defaults(AVCodecContext *s){
9740beff
MN
346 memset(s, 0, sizeof(AVCodecContext));
347
e8b62df6
MN
348 s->bit_rate= 800*1000;
349 s->bit_rate_tolerance= s->bit_rate*10;
a949d72e
MN
350 s->qmin= 2;
351 s->qmax= 31;
17a70fde
MN
352 s->mb_qmin= 2;
353 s->mb_qmax= 31;
a949d72e
MN
354 s->rc_eq= "tex^qComp";
355 s->qcompress= 0.5;
e8b62df6
MN
356 s->max_qdiff= 3;
357 s->b_quant_factor=1.25;
358 s->b_quant_offset=1.25;
b3a391e8 359 s->i_quant_factor=-0.8;
e8b62df6 360 s->i_quant_offset=0.0;
4d2858de 361 s->error_concealment= 3;
8d0e42ca 362 s->error_resilience= 1;
4d2858de 363 s->workaround_bugs= FF_BUG_AUTODETECT;
14bea432
MN
364 s->frame_rate_base= 1;
365 s->frame_rate = 25;
8d0e42ca
MN
366 s->gop_size= 50;
367 s->me_method= ME_EPZS;
1e491e29
MN
368 s->get_buffer= avcodec_default_get_buffer;
369 s->release_buffer= avcodec_default_release_buffer;
a33c7159 370 s->get_format= avcodec_default_get_format;
9c3d33d6
MN
371 s->execute= avcodec_default_execute;
372 s->thread_count=1;
826f429a 373 s->me_subpel_quality=8;
158c7f05
MN
374 s->lmin= FF_QP2LAMBDA * s->qmin;
375 s->lmax= FF_QP2LAMBDA * s->qmax;
5ff85f1d 376 s->sample_aspect_ratio= (AVRational){0,1};
620fe604 377 s->ildct_cmp= FF_CMP_VSAD;
1984f635
MN
378
379 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
380 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
2a2bbcb0 381 s->palctrl = NULL;
e1c2a5a0 382 s->reget_buffer= avcodec_default_reget_buffer;
a949d72e
MN
383}
384
385/**
386 * allocates a AVCodecContext and set it to defaults.
387 * this can be deallocated by simply calling free()
388 */
bc874dae
MB
389static const char* context_to_name(void* class_ptr) { return ((AVCodecContext*) class_ptr)->codec->name; }
390
391static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
392
7ffbb60e 393AVCodecContext *avcodec_alloc_context(void){
9740beff 394 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
a949d72e
MN
395
396 if(avctx==NULL) return NULL;
397
bc874dae 398 avctx->class = av_codec_context_class;
a949d72e
MN
399 avcodec_get_context_defaults(avctx);
400
401 return avctx;
402}
403
9740beff
MN
404void avcodec_get_frame_defaults(AVFrame *pic){
405 memset(pic, 0, sizeof(AVFrame));
406
407 pic->pts= AV_NOPTS_VALUE;
408}
409
1e491e29 410/**
492cd3a9 411 * allocates a AVPFrame and set it to defaults.
1e491e29
MN
412 * this can be deallocated by simply calling free()
413 */
492cd3a9 414AVFrame *avcodec_alloc_frame(void){
9740beff
MN
415 AVFrame *pic= av_malloc(sizeof(AVFrame));
416
417 if(pic==NULL) return NULL;
418
419 avcodec_get_frame_defaults(pic);
1e491e29
MN
420
421 return pic;
422}
423
de6d9b64
FB
424int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
425{
426 int ret;
427
09770af8
MN
428 if(avctx->codec)
429 return -1;
430
de6d9b64 431 avctx->codec = codec;
4cb3ca72 432 avctx->codec_id = codec->id;
de6d9b64 433 avctx->frame_number = 0;
0edf8a7a
PG
434 if (codec->priv_data_size > 0) {
435 avctx->priv_data = av_mallocz(codec->priv_data_size);
436 if (!avctx->priv_data)
437 return -ENOMEM;
438 } else {
439 avctx->priv_data = NULL;
440 }
de6d9b64
FB
441 ret = avctx->codec->init(avctx);
442 if (ret < 0) {
3123dd79 443 av_freep(&avctx->priv_data);
de6d9b64
FB
444 return ret;
445 }
446 return 0;
447}
448
0c1a9eda 449int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
de6d9b64
FB
450 const short *samples)
451{
452 int ret;
453
454 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
455 avctx->frame_number++;
456 return ret;
457}
458
0c1a9eda 459int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 460 const AVFrame *pict)
de6d9b64
FB
461{
462 int ret;
463
464 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
6bb925f4
MN
465
466 emms_c(); //needed to avoid a emms_c() call before every return;
467
de6d9b64
FB
468 avctx->frame_number++;
469 return ret;
470}
471
f138f883
MN
472/**
473 * decode a frame.
474 * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
475 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
476 * @param buf_size the size of the buffer in bytes
477 * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
478 * @return -1 if error, otherwise return the number of
479 * bytes used.
480 */
492cd3a9 481int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
de6d9b64 482 int *got_picture_ptr,
0c1a9eda 483 uint8_t *buf, int buf_size)
de6d9b64
FB
484{
485 int ret;
1e491e29 486
de6d9b64
FB
487 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
488 buf, buf_size);
6bb925f4
MN
489
490 emms_c(); //needed to avoid a emms_c() call before every return;
1e491e29 491
1cb0edb4
J
492 if (*got_picture_ptr)
493 avctx->frame_number++;
de6d9b64
FB
494 return ret;
495}
496
497/* decode an audio frame. return -1 if error, otherwise return the
498 *number of bytes used. If no frame could be decompressed,
499 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
500 *size in BYTES. */
0c1a9eda 501int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
de6d9b64 502 int *frame_size_ptr,
0c1a9eda 503 uint8_t *buf, int buf_size)
de6d9b64
FB
504{
505 int ret;
506
507 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
508 buf, buf_size);
509 avctx->frame_number++;
510 return ret;
511}
512
513int avcodec_close(AVCodecContext *avctx)
514{
515 if (avctx->codec->close)
516 avctx->codec->close(avctx);
3123dd79 517 av_freep(&avctx->priv_data);
de6d9b64
FB
518 avctx->codec = NULL;
519 return 0;
520}
521
522AVCodec *avcodec_find_encoder(enum CodecID id)
523{
524 AVCodec *p;
525 p = first_avcodec;
526 while (p) {
527 if (p->encode != NULL && p->id == id)
528 return p;
529 p = p->next;
530 }
531 return NULL;
532}
533
98f3b098
A
534AVCodec *avcodec_find_encoder_by_name(const char *name)
535{
536 AVCodec *p;
537 p = first_avcodec;
538 while (p) {
539 if (p->encode != NULL && strcmp(name,p->name) == 0)
540 return p;
541 p = p->next;
542 }
543 return NULL;
544}
545
de6d9b64
FB
546AVCodec *avcodec_find_decoder(enum CodecID id)
547{
548 AVCodec *p;
549 p = first_avcodec;
550 while (p) {
551 if (p->decode != NULL && p->id == id)
552 return p;
553 p = p->next;
554 }
555 return NULL;
556}
557
558AVCodec *avcodec_find_decoder_by_name(const char *name)
559{
560 AVCodec *p;
561 p = first_avcodec;
562 while (p) {
563 if (p->decode != NULL && strcmp(name,p->name) == 0)
564 return p;
565 p = p->next;
566 }
567 return NULL;
568}
569
570AVCodec *avcodec_find(enum CodecID id)
571{
572 AVCodec *p;
573 p = first_avcodec;
574 while (p) {
575 if (p->id == id)
576 return p;
577 p = p->next;
578 }
579 return NULL;
580}
581
582void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
583{
584 const char *codec_name;
585 AVCodec *p;
586 char buf1[32];
623563c0 587 char channels_str[100];
a96b68b7 588 int bitrate;
de6d9b64
FB
589
590 if (encode)
591 p = avcodec_find_encoder(enc->codec_id);
592 else
593 p = avcodec_find_decoder(enc->codec_id);
594
595 if (p) {
596 codec_name = p->name;
98ce5991
FB
597 if (!encode && enc->codec_id == CODEC_ID_MP3) {
598 if (enc->sub_id == 2)
599 codec_name = "mp2";
600 else if (enc->sub_id == 1)
601 codec_name = "mp1";
602 }
985180a1
FB
603 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
604 /* fake mpeg2 transport stream codec (currently not
605 registered) */
606 codec_name = "mpeg2ts";
de6d9b64
FB
607 } else if (enc->codec_name[0] != '\0') {
608 codec_name = enc->codec_name;
609 } else {
610 /* output avi tags */
611 if (enc->codec_type == CODEC_TYPE_VIDEO) {
612 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
613 enc->codec_tag & 0xff,
614 (enc->codec_tag >> 8) & 0xff,
615 (enc->codec_tag >> 16) & 0xff,
616 (enc->codec_tag >> 24) & 0xff);
617 } else {
618 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
619 }
620 codec_name = buf1;
621 }
622
623 switch(enc->codec_type) {
624 case CODEC_TYPE_VIDEO:
625 snprintf(buf, buf_size,
626 "Video: %s%s",
7d1c3fc1 627 codec_name, enc->mb_decision ? " (hq)" : "");
cf087595
FB
628 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
629 snprintf(buf + strlen(buf), buf_size - strlen(buf),
630 ", %s",
9bbffbb1 631 avcodec_get_pix_fmt_name(enc->pix_fmt));
cf087595 632 }
de6d9b64
FB
633 if (enc->width) {
634 snprintf(buf + strlen(buf), buf_size - strlen(buf),
635 ", %dx%d, %0.2f fps",
636 enc->width, enc->height,
14bea432 637 (float)enc->frame_rate / enc->frame_rate_base);
de6d9b64 638 }
4bfad535
FB
639 if (encode) {
640 snprintf(buf + strlen(buf), buf_size - strlen(buf),
641 ", q=%d-%d", enc->qmin, enc->qmax);
642 }
a96b68b7 643 bitrate = enc->bit_rate;
de6d9b64
FB
644 break;
645 case CODEC_TYPE_AUDIO:
646 snprintf(buf, buf_size,
647 "Audio: %s",
648 codec_name);
e0d2714a
J
649 switch (enc->channels) {
650 case 1:
623563c0 651 strcpy(channels_str, "mono");
e0d2714a
J
652 break;
653 case 2:
623563c0 654 strcpy(channels_str, "stereo");
e0d2714a
J
655 break;
656 case 6:
623563c0 657 strcpy(channels_str, "5:1");
e0d2714a
J
658 break;
659 default:
660 sprintf(channels_str, "%d channels", enc->channels);
661 break;
662 }
de6d9b64
FB
663 if (enc->sample_rate) {
664 snprintf(buf + strlen(buf), buf_size - strlen(buf),
665 ", %d Hz, %s",
666 enc->sample_rate,
e0d2714a 667 channels_str);
de6d9b64 668 }
e0d2714a 669
a96b68b7
FB
670 /* for PCM codecs, compute bitrate directly */
671 switch(enc->codec_id) {
672 case CODEC_ID_PCM_S16LE:
673 case CODEC_ID_PCM_S16BE:
674 case CODEC_ID_PCM_U16LE:
675 case CODEC_ID_PCM_U16BE:
a190b7e9 676 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
677 break;
678 case CODEC_ID_PCM_S8:
679 case CODEC_ID_PCM_U8:
680 case CODEC_ID_PCM_ALAW:
681 case CODEC_ID_PCM_MULAW:
a190b7e9 682 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
683 break;
684 default:
685 bitrate = enc->bit_rate;
686 break;
687 }
de6d9b64 688 break;
985180a1
FB
689 case CODEC_TYPE_DATA:
690 snprintf(buf, buf_size, "Data: %s", codec_name);
691 bitrate = enc->bit_rate;
692 break;
de6d9b64 693 default:
b71472eb 694 av_abort();
de6d9b64 695 }
4bfad535
FB
696 if (encode) {
697 if (enc->flags & CODEC_FLAG_PASS1)
698 snprintf(buf + strlen(buf), buf_size - strlen(buf),
699 ", pass 1");
700 if (enc->flags & CODEC_FLAG_PASS2)
701 snprintf(buf + strlen(buf), buf_size - strlen(buf),
702 ", pass 2");
703 }
a96b68b7 704 if (bitrate != 0) {
de6d9b64 705 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 706 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
707 }
708}
709
156e5023
NK
710unsigned avcodec_version( void )
711{
712 return LIBAVCODEC_VERSION_INT;
713}
cf087595 714
8bceb6af
NK
715unsigned avcodec_build( void )
716{
717 return LIBAVCODEC_BUILD;
718}
719
de6d9b64
FB
720/* must be called before any other functions */
721void avcodec_init(void)
722{
0344cd0a
AB
723 static int inited = 0;
724
725 if (inited != 0)
726 return;
727 inited = 1;
728
59cf08ce 729 dsputil_static_init();
de6d9b64
FB
730}
731
7a06ff14
MN
732/**
733 * Flush buffers, should be called when seeking or when swicthing to a different stream.
734 */
1c2a8c7f
MN
735void avcodec_flush_buffers(AVCodecContext *avctx)
736{
7a06ff14
MN
737 if(avctx->codec->flush)
738 avctx->codec->flush(avctx);
1c2a8c7f
MN
739}
740
d90cf87b
MN
741void avcodec_default_free_buffers(AVCodecContext *s){
742 int i, j;
743
744 if(s->internal_buffer==NULL) return;
745
746 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
747 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
748 for(j=0; j<4; j++){
749 av_freep(&buf->base[j]);
750 buf->data[j]= NULL;
751 }
752 }
753 av_freep(&s->internal_buffer);
754
755 s->internal_buffer_count=0;
756}
757
d8085ea7
MN
758char av_get_pict_type_char(int pict_type){
759 switch(pict_type){
760 case I_TYPE: return 'I';
761 case P_TYPE: return 'P';
762 case B_TYPE: return 'B';
763 case S_TYPE: return 'S';
764 case SI_TYPE:return 'i';
765 case SP_TYPE:return 'p';
766 default: return '?';
767 }
768}
769
14bea432
MN
770int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
771 int exact=1, sign=0;
5ed9f2e5 772 int64_t gcd;
14bea432
MN
773
774 assert(den != 0);
775
776 if(den < 0){
777 den= -den;
778 nom= -nom;
779 }
780
781 if(nom < 0){
782 nom= -nom;
783 sign= 1;
784 }
785
5ed9f2e5
MN
786 gcd = ff_gcd(nom, den);
787 nom /= gcd;
788 den /= gcd;
14bea432 789
5ed9f2e5
MN
790 if(nom > max || den > max){
791 AVRational a0={0,1}, a1={1,0};
792 exact=0;
793
794 for(;;){
795 int64_t x= nom / den;
796 int64_t a2n= x*a1.num + a0.num;
797 int64_t a2d= x*a1.den + a0.den;
798
799 if(a2n > max || a2d > max) break;
800
801 nom %= den;
802
803 a0= a1;
804 a1= (AVRational){a2n, a2d};
805 if(nom==0) break;
806 x= nom; nom=den; den=x;
807 }
808 nom= a1.num;
809 den= a1.den;
14bea432
MN
810 }
811
5ed9f2e5
MN
812 assert(ff_gcd(nom, den) == 1);
813
14bea432
MN
814 if(sign) nom= -nom;
815
816 *dst_nom = nom;
817 *dst_den = den;
818
819 return exact;
820}
821
822int64_t av_rescale(int64_t a, int b, int c){
823 uint64_t h, l;
824 assert(c > 0);
825 assert(b >=0);
826
827 if(a<0) return -av_rescale(-a, b, c);
828
829 h= a>>32;
830 if(h==0) return a*b/c;
831
832 l= a&0xFFFFFFFF;
833 l *= b;
834 h *= b;
835
836 l += (h%c)<<32;
837
838 return ((h/c)<<32) + l/c;
839}
9b879566
MB
840
841/* av_log API */
842
bc874dae
MB
843static const char* null_to_name(void* class_ptr) { return "NULL"; }
844
845static AVClass av_null_class = { "NULL", null_to_name };
9b879566
MB
846
847static int av_log_level = AV_LOG_DEBUG;
848
bc874dae 849static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
9b879566 850{
89b9441a 851 static int print_prefix=1;
bc874dae
MB
852 AVClass* avcl = ptr;
853 if(!avcl || !avcl->class_name)
854 avcl = &av_null_class;
9b879566 855 if(level>av_log_level)
bc874dae 856 return;
d705e4a6 857#undef fprintf
bc874dae
MB
858 if(print_prefix) {
859 fprintf(stderr, "[%s:%s @ %p]", avcl->class_name, avcl->item_name(avcl), avcl);
860 }
d705e4a6 861#define fprintf please_use_av_log
89b9441a 862
95ba2c8f 863 print_prefix= strstr(fmt, "\n") != NULL;
89b9441a 864
9b879566
MB
865 vfprintf(stderr, fmt, vl);
866}
867
bc874dae 868static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
9b879566 869
bc874dae 870void av_log(void* avcl, int level, const char *fmt, ...)
9b879566
MB
871{
872 va_list vl;
873 va_start(vl, fmt);
bc874dae 874 av_vlog(avcl, level, fmt, vl);
9b879566
MB
875 va_end(vl);
876}
877
bc874dae 878void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
9b879566 879{
bc874dae 880 av_log_callback(avcl, level, fmt, vl);
9b879566
MB
881}
882
883int av_log_get_level(void)
884{
885 return av_log_level;
886}
887
888void av_log_set_level(int level)
889{
890 av_log_level = level;
891}
892
bc874dae 893void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
9b879566
MB
894{
895 av_log_callback = callback;
896}
897