av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
[libav.git] / libavcodec / utils.c
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>
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file utils.c
24 * utils.
25 */
26
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "mpegvideo.h"
30 #include <stdarg.h>
31
32 void *av_mallocz(unsigned int size)
33 {
34 void *ptr;
35
36 ptr = av_malloc(size);
37 if (!ptr)
38 return NULL;
39 memset(ptr, 0, size);
40 return ptr;
41 }
42
43 char *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 */
58 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
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
69 /* allocation of static arrays - do not use for normal allocation */
70 static unsigned int last_static = 0;
71 static char*** array_static = NULL;
72 static const unsigned int grow_static = 64; // ^2
73 void *__av_mallocz_static(void** location, unsigned int size)
74 {
75 unsigned int l = (last_static + grow_static) & ~(grow_static - 1);
76 void *ptr = av_mallocz(size);
77 if (!ptr)
78 return NULL;
79
80 if (location)
81 {
82 if (l > last_static)
83 array_static = av_realloc(array_static, l);
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 */
90 void av_free_static(void)
91 {
92 if (array_static)
93 {
94 unsigned i;
95 for (i = 0; i < last_static; i++)
96 {
97 av_free(*array_static[i]);
98 *array_static[i] = NULL;
99 }
100 av_free(array_static);
101 array_static = 0;
102 }
103 last_static = 0;
104 }
105
106 /**
107 * Frees memory and sets the pointer to NULL.
108 * @param arg pointer to the pointer which should be freed
109 */
110 void av_freep(void *arg)
111 {
112 void **ptr= (void**)arg;
113 av_free(*ptr);
114 *ptr = NULL;
115 }
116
117 /* encoder management */
118 AVCodec *first_avcodec;
119
120 void 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
129 typedef struct InternalBuffer{
130 int last_pic_num;
131 uint8_t *base[4];
132 uint8_t *data[4];
133 int linesize[4];
134 }InternalBuffer;
135
136 #define INTERNAL_BUFFER_SIZE 32
137
138 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
139
140 void 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
176 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
177 int i;
178 int w= s->width;
179 int h= s->height;
180 InternalBuffer *buf;
181 int *picture_number;
182
183 assert(pic->data[0]==NULL);
184 assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
185
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];
198 picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
199 (*picture_number)++;
200
201 if(buf->base[0]){
202 pic->age= *picture_number - buf->last_pic_num;
203 buf->last_pic_num= *picture_number;
204 }else{
205 int h_chroma_shift, v_chroma_shift;
206 int s_align, pixel_size;
207
208 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
209
210 switch(s->pix_fmt){
211 case PIX_FMT_RGB555:
212 case PIX_FMT_RGB565:
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;
220 case PIX_FMT_RGBA32:
221 pixel_size=4;
222 break;
223 default:
224 pixel_size=1;
225 }
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
234 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
235 w+= EDGE_WIDTH*2;
236 h+= EDGE_WIDTH*2;
237 }
238
239 buf->last_pic_num= -256*256*256*64;
240
241 for(i=0; i<3; i++){
242 const int h_shift= i==0 ? 0 : h_chroma_shift;
243 const int v_shift= i==0 ? 0 : v_chroma_shift;
244
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));
247
248 buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
249 if(buf->base[i]==NULL) return -1;
250 memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
251
252 if(s->flags&CODEC_FLAG_EMU_EDGE)
253 buf->data[i] = buf->base[i];
254 else
255 buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
256 }
257 pic->age= 256*256*256*64;
258 }
259 pic->type= FF_BUFFER_TYPE_INTERNAL;
260
261 for(i=0; i<4; i++){
262 pic->base[i]= buf->base[i];
263 pic->data[i]= buf->data[i];
264 pic->linesize[i]= buf->linesize[i];
265 }
266 s->internal_buffer_count++;
267
268 return 0;
269 }
270
271 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
272 int i;
273 InternalBuffer *buf, *last, temp;
274
275 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
276 assert(s->internal_buffer_count);
277
278 buf = NULL; /* avoids warning */
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++){
293 pic->data[i]=NULL;
294 // pic->base[i]=NULL;
295 }
296 //printf("R%X\n", pic->opaque);
297 }
298
299 int 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
331 int 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
341 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
342 return fmt[0];
343 }
344
345 void avcodec_get_context_defaults(AVCodecContext *s){
346 memset(s, 0, sizeof(AVCodecContext));
347
348 s->bit_rate= 800*1000;
349 s->bit_rate_tolerance= s->bit_rate*10;
350 s->qmin= 2;
351 s->qmax= 31;
352 s->mb_qmin= 2;
353 s->mb_qmax= 31;
354 s->rc_eq= "tex^qComp";
355 s->qcompress= 0.5;
356 s->max_qdiff= 3;
357 s->b_quant_factor=1.25;
358 s->b_quant_offset=1.25;
359 s->i_quant_factor=-0.8;
360 s->i_quant_offset=0.0;
361 s->error_concealment= 3;
362 s->error_resilience= 1;
363 s->workaround_bugs= FF_BUG_AUTODETECT;
364 s->frame_rate_base= 1;
365 s->frame_rate = 25;
366 s->gop_size= 50;
367 s->me_method= ME_EPZS;
368 s->get_buffer= avcodec_default_get_buffer;
369 s->release_buffer= avcodec_default_release_buffer;
370 s->get_format= avcodec_default_get_format;
371 s->execute= avcodec_default_execute;
372 s->thread_count=1;
373 s->me_subpel_quality=8;
374 s->lmin= FF_QP2LAMBDA * s->qmin;
375 s->lmax= FF_QP2LAMBDA * s->qmax;
376 s->sample_aspect_ratio= (AVRational){0,1};
377 s->ildct_cmp= FF_CMP_VSAD;
378
379 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
380 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
381 s->palctrl = NULL;
382 s->reget_buffer= avcodec_default_reget_buffer;
383 }
384
385 /**
386 * allocates a AVCodecContext and set it to defaults.
387 * this can be deallocated by simply calling free()
388 */
389 static const char* context_to_name(void* class_ptr) { return ((AVCodecContext*) class_ptr)->codec->name; }
390
391 static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
392
393 AVCodecContext *avcodec_alloc_context(void){
394 AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
395
396 if(avctx==NULL) return NULL;
397
398 avctx->class = av_codec_context_class;
399 avcodec_get_context_defaults(avctx);
400
401 return avctx;
402 }
403
404 void avcodec_get_frame_defaults(AVFrame *pic){
405 memset(pic, 0, sizeof(AVFrame));
406
407 pic->pts= AV_NOPTS_VALUE;
408 }
409
410 /**
411 * allocates a AVPFrame and set it to defaults.
412 * this can be deallocated by simply calling free()
413 */
414 AVFrame *avcodec_alloc_frame(void){
415 AVFrame *pic= av_malloc(sizeof(AVFrame));
416
417 if(pic==NULL) return NULL;
418
419 avcodec_get_frame_defaults(pic);
420
421 return pic;
422 }
423
424 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
425 {
426 int ret;
427
428 if(avctx->codec)
429 return -1;
430
431 avctx->codec = codec;
432 avctx->codec_id = codec->id;
433 avctx->frame_number = 0;
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 }
441 ret = avctx->codec->init(avctx);
442 if (ret < 0) {
443 av_freep(&avctx->priv_data);
444 return ret;
445 }
446 return 0;
447 }
448
449 int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
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
459 int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
460 const AVFrame *pict)
461 {
462 int ret;
463
464 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
465
466 emms_c(); //needed to avoid a emms_c() call before every return;
467
468 avctx->frame_number++;
469 return ret;
470 }
471
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 */
481 int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
482 int *got_picture_ptr,
483 uint8_t *buf, int buf_size)
484 {
485 int ret;
486
487 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
488 buf, buf_size);
489
490 emms_c(); //needed to avoid a emms_c() call before every return;
491
492 if (*got_picture_ptr)
493 avctx->frame_number++;
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. */
501 int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
502 int *frame_size_ptr,
503 uint8_t *buf, int buf_size)
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
513 int avcodec_close(AVCodecContext *avctx)
514 {
515 if (avctx->codec->close)
516 avctx->codec->close(avctx);
517 av_freep(&avctx->priv_data);
518 avctx->codec = NULL;
519 return 0;
520 }
521
522 AVCodec *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
534 AVCodec *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
546 AVCodec *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
558 AVCodec *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
570 AVCodec *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
582 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
583 {
584 const char *codec_name;
585 AVCodec *p;
586 char buf1[32];
587 char channels_str[100];
588 int bitrate;
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;
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 }
603 } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
604 /* fake mpeg2 transport stream codec (currently not
605 registered) */
606 codec_name = "mpeg2ts";
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",
627 codec_name, enc->mb_decision ? " (hq)" : "");
628 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
629 snprintf(buf + strlen(buf), buf_size - strlen(buf),
630 ", %s",
631 avcodec_get_pix_fmt_name(enc->pix_fmt));
632 }
633 if (enc->width) {
634 snprintf(buf + strlen(buf), buf_size - strlen(buf),
635 ", %dx%d, %0.2f fps",
636 enc->width, enc->height,
637 (float)enc->frame_rate / enc->frame_rate_base);
638 }
639 if (encode) {
640 snprintf(buf + strlen(buf), buf_size - strlen(buf),
641 ", q=%d-%d", enc->qmin, enc->qmax);
642 }
643 bitrate = enc->bit_rate;
644 break;
645 case CODEC_TYPE_AUDIO:
646 snprintf(buf, buf_size,
647 "Audio: %s",
648 codec_name);
649 switch (enc->channels) {
650 case 1:
651 strcpy(channels_str, "mono");
652 break;
653 case 2:
654 strcpy(channels_str, "stereo");
655 break;
656 case 6:
657 strcpy(channels_str, "5:1");
658 break;
659 default:
660 sprintf(channels_str, "%d channels", enc->channels);
661 break;
662 }
663 if (enc->sample_rate) {
664 snprintf(buf + strlen(buf), buf_size - strlen(buf),
665 ", %d Hz, %s",
666 enc->sample_rate,
667 channels_str);
668 }
669
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:
676 bitrate = enc->sample_rate * enc->channels * 16;
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:
682 bitrate = enc->sample_rate * enc->channels * 8;
683 break;
684 default:
685 bitrate = enc->bit_rate;
686 break;
687 }
688 break;
689 case CODEC_TYPE_DATA:
690 snprintf(buf, buf_size, "Data: %s", codec_name);
691 bitrate = enc->bit_rate;
692 break;
693 default:
694 av_abort();
695 }
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 }
704 if (bitrate != 0) {
705 snprintf(buf + strlen(buf), buf_size - strlen(buf),
706 ", %d kb/s", bitrate / 1000);
707 }
708 }
709
710 unsigned avcodec_version( void )
711 {
712 return LIBAVCODEC_VERSION_INT;
713 }
714
715 unsigned avcodec_build( void )
716 {
717 return LIBAVCODEC_BUILD;
718 }
719
720 /* must be called before any other functions */
721 void avcodec_init(void)
722 {
723 static int inited = 0;
724
725 if (inited != 0)
726 return;
727 inited = 1;
728
729 dsputil_static_init();
730 }
731
732 /**
733 * Flush buffers, should be called when seeking or when swicthing to a different stream.
734 */
735 void avcodec_flush_buffers(AVCodecContext *avctx)
736 {
737 if(avctx->codec->flush)
738 avctx->codec->flush(avctx);
739 }
740
741 void 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
758 char 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
770 int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
771 int exact=1, sign=0;
772 int64_t gcd;
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
786 gcd = ff_gcd(nom, den);
787 nom /= gcd;
788 den /= gcd;
789
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;
810 }
811
812 assert(ff_gcd(nom, den) == 1);
813
814 if(sign) nom= -nom;
815
816 *dst_nom = nom;
817 *dst_den = den;
818
819 return exact;
820 }
821
822 int64_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 }
840
841 /* av_log API */
842
843 static const char* null_to_name(void* class_ptr) { return "NULL"; }
844
845 static AVClass av_null_class = { "NULL", null_to_name };
846
847 static int av_log_level = AV_LOG_DEBUG;
848
849 static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
850 {
851 static int print_prefix=1;
852 AVClass* avcl = ptr;
853 if(!avcl || !avcl->class_name)
854 avcl = &av_null_class;
855 if(level>av_log_level)
856 return;
857 #undef fprintf
858 if(print_prefix) {
859 fprintf(stderr, "[%s:%s @ %p]", avcl->class_name, avcl->item_name(avcl), avcl);
860 }
861 #define fprintf please_use_av_log
862
863 print_prefix= strstr(fmt, "\n") != NULL;
864
865 vfprintf(stderr, fmt, vl);
866 }
867
868 static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
869
870 void av_log(void* avcl, int level, const char *fmt, ...)
871 {
872 va_list vl;
873 va_start(vl, fmt);
874 av_vlog(avcl, level, fmt, vl);
875 va_end(vl);
876 }
877
878 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
879 {
880 av_log_callback(avcl, level, fmt, vl);
881 }
882
883 int av_log_get_level(void)
884 {
885 return av_log_level;
886 }
887
888 void av_log_set_level(int level)
889 {
890 av_log_level = level;
891 }
892
893 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
894 {
895 av_log_callback = callback;
896 }
897