comparison src/ffmpeg/libffwma/utils.c @ 806:74abcb9cafae trunk

[svn] - fork wma plugin
author nenolod
date Mon, 12 Mar 2007 10:59:21 -0700
parents src/wma/libffwma/utils.c@3da1b8942b8b
children
comparison
equal deleted inserted replaced
805:1ba5f86aeac9 806:74abcb9cafae
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 * Copyright (c) 2004 Roman Bogorodskiy (bmp-wma specific stuff)
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "avcodec.h"
24 #include "dsputil.h"
25 #include <stdarg.h>
26
27 void *av_mallocz(unsigned int size)
28 {
29 void *ptr;
30
31 ptr = av_malloc(size);
32 if (!ptr)
33 return NULL;
34
35 memset(ptr, 0, size);
36
37 return ptr;
38 }
39
40 /**
41 * realloc which does nothing if the block is large enough
42 */
43 void *av_fast_realloc(void *ptr, int *size, unsigned int min_size)
44 {
45 if(min_size < (unsigned int)*size)
46 return ptr;
47
48 *size= min_size + 10*1024;
49
50 return realloc(ptr, *size);
51 }
52
53
54 /* allocation of static arrays - do not use for normal allocation */
55 static unsigned int last_static = 0;
56 static char*** array_static = NULL;
57 static const unsigned int grow_static = 64; // ^2
58 void *__av_mallocz_static(void** location, unsigned int size)
59 {
60 unsigned int l = (last_static + grow_static) & ~(grow_static - 1);
61 void *ptr = av_mallocz(size);
62 if (!ptr)
63 return NULL;
64
65 if (location)
66 {
67 if (l > last_static)
68 array_static = realloc(array_static, l);
69 array_static[last_static++] = (char**) location;
70 *location = ptr;
71 }
72 return ptr;
73 }
74 /* free all static arrays and reset pointers to 0 */
75 void av_free_static(void)
76 {
77 if (array_static)
78 {
79 unsigned i;
80 for (i = 0; i < last_static; i++)
81 {
82 free(*array_static[i]);
83 *array_static[i] = NULL;
84 }
85 free(array_static);
86 array_static = 0;
87 }
88 last_static = 0;
89 }
90
91 /* cannot call it directly because of 'void **' casting is not automatic */
92 void __av_freep(void **ptr)
93 {
94 free(*ptr);
95 *ptr = NULL;
96 }
97
98 /* encoder management */
99 AVCodec *first_avcodec;
100
101 void register_avcodec(AVCodec *format)
102 {
103 AVCodec **p;
104 p = &first_avcodec;
105 while (*p != NULL) p = &(*p)->next;
106 *p = format;
107 format->next = NULL;
108 }
109
110 typedef struct InternalBuffer{
111 int last_pic_num;
112 uint8_t *base[4];
113 uint8_t *data[4];
114 int linesize[4];
115 }InternalBuffer;
116
117 #define INTERNAL_BUFFER_SIZE 32
118
119 #undef ALIGN
120 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
121
122 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
123 int w_align= 1;
124 int h_align= 1;
125
126 switch(s->pix_fmt){
127 case PIX_FMT_YUV420P:
128 case PIX_FMT_YUV422:
129 case PIX_FMT_YUV422P:
130 case PIX_FMT_YUV444P:
131 case PIX_FMT_GRAY8:
132 case PIX_FMT_YUVJ420P:
133 case PIX_FMT_YUVJ422P:
134 case PIX_FMT_YUVJ444P:
135 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
136 h_align= 16;
137 break;
138 case PIX_FMT_YUV411P:
139 w_align=32;
140 h_align=8;
141 break;
142 case PIX_FMT_YUV410P:
143 default:
144 w_align= 1;
145 h_align= 1;
146 break;
147 }
148
149 *width = ALIGN(*width , w_align);
150 *height= ALIGN(*height, h_align);
151 }
152
153 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
154 int i;
155 InternalBuffer *buf, *last, temp;
156
157 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
158 assert(s->internal_buffer_count);
159
160 buf = NULL; /* avoids warning */
161 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
162 buf= &((InternalBuffer*)s->internal_buffer)[i];
163 if(buf->data[0] == pic->data[0])
164 break;
165 }
166 assert(i < s->internal_buffer_count);
167 s->internal_buffer_count--;
168 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
169
170 temp= *buf;
171 *buf= *last;
172 *last= temp;
173
174 for(i=0; i<3; i++){
175 pic->data[i]=NULL;
176 // pic->base[i]=NULL;
177 }
178 }
179
180 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
181 return fmt[0];
182 }
183
184 void avcodec_get_context_defaults(AVCodecContext *s){
185 s->bit_rate= 800*1000;
186 s->bit_rate_tolerance= s->bit_rate*10;
187 s->qmin= 2;
188 s->qmax= 31;
189 s->mb_qmin= 2;
190 s->mb_qmax= 31;
191 s->rc_eq= "tex^qComp";
192 s->qcompress= 0.5;
193 s->max_qdiff= 3;
194 s->b_quant_factor=1.25;
195 s->b_quant_offset=1.25;
196 s->i_quant_factor=-0.8;
197 s->i_quant_offset=0.0;
198 s->error_concealment= 3;
199 s->error_resilience= 1;
200 s->workaround_bugs= FF_BUG_AUTODETECT;
201 s->frame_rate_base= 1;
202 s->frame_rate = 25;
203 s->gop_size= 50;
204 s->me_method= ME_EPZS;
205 //s->get_buffer= avcodec_default_get_buffer;
206 s->release_buffer= avcodec_default_release_buffer;
207 s->get_format= avcodec_default_get_format;
208 s->me_subpel_quality=8;
209 s->lmin= FF_QP2LAMBDA * s->qmin;
210 s->lmax= FF_QP2LAMBDA * s->qmax;
211 //s->sample_aspect_ratio= (AVRational){0,1};
212 s->ildct_cmp= FF_CMP_VSAD;
213
214 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
215 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
216 s->palctrl = NULL;
217 //s->reget_buffer= avcodec_default_reget_buffer;
218 }
219
220 /**
221 * allocates a AVCodecContext and set it to defaults.
222 * this can be deallocated by simply calling free()
223 */
224 AVCodecContext *avcodec_alloc_context(void){
225 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
226
227 if(avctx==NULL) return NULL;
228
229 avcodec_get_context_defaults(avctx);
230
231 return avctx;
232 }
233
234 /**
235 * allocates a AVPFrame and set it to defaults.
236 * this can be deallocated by simply calling free()
237 */
238 AVFrame *avcodec_alloc_frame(void){
239 AVFrame *pic= av_mallocz(sizeof(AVFrame));
240
241 return pic;
242 }
243
244 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
245 {
246 int ret;
247
248 if(avctx->codec)
249 return -1;
250
251 avctx->codec = codec;
252 avctx->codec_id = codec->id;
253 avctx->frame_number = 0;
254 if (codec->priv_data_size > 0) {
255 avctx->priv_data = av_mallocz(codec->priv_data_size);
256 if (!avctx->priv_data)
257 return -ENOMEM;
258 } else {
259 avctx->priv_data = NULL;
260 }
261 ret = avctx->codec->init(avctx);
262 if (ret < 0) {
263 av_freep(&avctx->priv_data);
264 return ret;
265 }
266 return 0;
267 }
268
269 int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
270 const short *samples)
271 {
272 int ret;
273
274 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
275 avctx->frame_number++;
276 return ret;
277 }
278
279 /* decode an audio frame. return -1 if error, otherwise return the
280 *number of bytes used. If no frame could be decompressed,
281 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
282 *size in BYTES. */
283 int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
284 int *frame_size_ptr,
285 uint8_t *buf, int buf_size)
286 {
287 int ret;
288 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
289 buf, buf_size);
290 avctx->frame_number++;
291 return ret;
292 }
293
294 int avcodec_close(AVCodecContext *avctx)
295 {
296 if (avctx->codec->close)
297 avctx->codec->close(avctx);
298 av_freep(&avctx->priv_data);
299 avctx->codec = NULL;
300 return 0;
301 }
302
303 AVCodec *avcodec_find_encoder(enum CodecID id)
304 {
305 AVCodec *p;
306 p = first_avcodec;
307 while (p) {
308 if (p->encode != NULL && (enum CodecID)p->id == id)
309 return p;
310 p = p->next;
311 }
312 return NULL;
313 }
314
315 AVCodec *avcodec_find_encoder_by_name(const char *name)
316 {
317 AVCodec *p;
318 p = first_avcodec;
319 while (p) {
320 if (p->encode != NULL && strcmp(name,p->name) == 0)
321 return p;
322 p = p->next;
323 }
324 return NULL;
325 }
326
327 AVCodec *avcodec_find_decoder(enum CodecID id)
328 {
329 AVCodec *p;
330 p = first_avcodec;
331 while (p) {
332 if (p->decode != NULL && (enum CodecID)p->id == id)
333 return p;
334 p = p->next;
335 }
336 return NULL;
337 }
338
339 AVCodec *avcodec_find_decoder_by_name(const char *name)
340 {
341 AVCodec *p;
342 p = first_avcodec;
343 while (p) {
344 if (p->decode != NULL && strcmp(name,p->name) == 0)
345 return p;
346 p = p->next;
347 }
348 return NULL;
349 }
350
351 AVCodec *avcodec_find(enum CodecID id)
352 {
353 AVCodec *p;
354 p = first_avcodec;
355 while (p) {
356 if ((enum CodecID)p->id == id)
357 return p;
358 p = p->next;
359 }
360 return NULL;
361 }
362
363 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
364 {
365 const char *codec_name;
366 AVCodec *p;
367 char buf1[32];
368 char channels_str[100];
369 int bitrate;
370
371 if (encode)
372 p = avcodec_find_encoder(enc->codec_id);
373 else
374 p = avcodec_find_decoder(enc->codec_id);
375
376 if (p) {
377 codec_name = p->name;
378 } else if (enc->codec_name[0] != '\0') {
379 codec_name = enc->codec_name;
380 } else {
381 /* output avi tags */
382 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
383 codec_name = buf1;
384 }
385
386 switch(enc->codec_type) {
387 case CODEC_TYPE_AUDIO:
388 snprintf(buf, buf_size,
389 "Audio: %s",
390 codec_name);
391 switch (enc->channels) {
392 case 1:
393 strcpy(channels_str, "mono");
394 break;
395 case 2:
396 strcpy(channels_str, "stereo");
397 break;
398 case 6:
399 strcpy(channels_str, "5:1");
400 break;
401 default:
402 sprintf(channels_str, "%d channels", enc->channels);
403 break;
404 }
405 if (enc->sample_rate) {
406 snprintf(buf + strlen(buf), buf_size - strlen(buf),
407 ", %d Hz, %s",
408 enc->sample_rate,
409 channels_str);
410 }
411
412 /* for PCM codecs, compute bitrate directly */
413 switch(enc->codec_id) {
414 case CODEC_ID_PCM_S16LE:
415 case CODEC_ID_PCM_S16BE:
416 case CODEC_ID_PCM_U16LE:
417 case CODEC_ID_PCM_U16BE:
418 bitrate = enc->sample_rate * enc->channels * 16;
419 break;
420 case CODEC_ID_PCM_S8:
421 case CODEC_ID_PCM_U8:
422 case CODEC_ID_PCM_ALAW:
423 case CODEC_ID_PCM_MULAW:
424 bitrate = enc->sample_rate * enc->channels * 8;
425 break;
426 default:
427 bitrate = enc->bit_rate;
428 break;
429 }
430 break;
431 case CODEC_TYPE_DATA:
432 snprintf(buf, buf_size, "Data: %s", codec_name);
433 bitrate = enc->bit_rate;
434 break;
435 default:
436 av_abort();
437 }
438 if (encode) {
439 if (enc->flags & CODEC_FLAG_PASS1)
440 snprintf(buf + strlen(buf), buf_size - strlen(buf),
441 ", pass 1");
442 if (enc->flags & CODEC_FLAG_PASS2)
443 snprintf(buf + strlen(buf), buf_size - strlen(buf),
444 ", pass 2");
445 }
446 if (bitrate != 0) {
447 snprintf(buf + strlen(buf), buf_size - strlen(buf),
448 ", %d kb/s", bitrate / 1000);
449 }
450 }
451
452 unsigned avcodec_version( void )
453 {
454 return LIBAVCODEC_VERSION_INT;
455 }
456
457 unsigned avcodec_build( void )
458 {
459 return LIBAVCODEC_BUILD;
460 }
461
462 /* must be called before any other functions */
463 void avcodec_init(void)
464 {
465 static int inited = 0;
466
467 if (inited != 0)
468 return;
469 inited = 1;
470
471 dsputil_static_init();
472 }
473
474 /**
475 * Flush buffers, should be called when seeking or when swicthing to a different stream.
476 */
477 void avcodec_flush_buffers(AVCodecContext *avctx)
478 {
479 if(avctx->codec->flush)
480 avctx->codec->flush(avctx);
481 }
482
483 void avcodec_default_free_buffers(AVCodecContext *s){
484 int i, j;
485
486 if(s->internal_buffer==NULL) return;
487
488 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
489 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
490 for(j=0; j<4; j++){
491 av_freep(&buf->base[j]);
492 buf->data[j]= NULL;
493 }
494 }
495 av_freep(&s->internal_buffer);
496
497 s->internal_buffer_count=0;
498 }
499 #if 0
500 char av_get_pict_type_char(int pict_type){
501 switch(pict_type){
502 case I_TYPE: return 'I';
503 case P_TYPE: return 'P';
504 case B_TYPE: return 'B';
505 case S_TYPE: return 'S';
506 case SI_TYPE:return 'i';
507 case SP_TYPE:return 'p';
508 default: return '?';
509 }
510 }
511
512 int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
513 int exact=1, sign=0;
514 int64_t gcd;
515
516 assert(den != 0);
517
518 if(den < 0){
519 den= -den;
520 nom= -nom;
521 }
522
523 if(nom < 0){
524 nom= -nom;
525 sign= 1;
526 }
527
528 gcd = ff_gcd(nom, den);
529 nom /= gcd;
530 den /= gcd;
531
532 if(nom > max || den > max){
533 AVRational a0={0,1}, a1={1,0};
534 exact=0;
535
536 for(;;){
537 int64_t x= nom / den;
538 int64_t a2n= x*a1.num + a0.num;
539 int64_t a2d= x*a1.den + a0.den;
540
541 if(a2n > max || a2d > max) break;
542
543 nom %= den;
544
545 a0= a1;
546 a1= (AVRational){a2n, a2d};
547 if(nom==0) break;
548 x= nom; nom=den; den=x;
549 }
550 nom= a1.num;
551 den= a1.den;
552 }
553
554 assert(ff_gcd(nom, den) == 1);
555
556 if(sign) nom= -nom;
557
558 *dst_nom = nom;
559 *dst_den = den;
560
561 return exact;
562 }
563 #endif
564 int64_t av_rescale(int64_t a, int b, int c){
565 uint64_t h, l;
566 assert(c > 0);
567 assert(b >=0);
568
569 if(a<0) return -av_rescale(-a, b, c);
570
571 h= a>>32;
572 if(h==0) return a*b/c;
573
574 l= a&0xFFFFFFFF;
575 l *= b;
576 h *= b;
577
578 l += (h%c)<<32;
579
580 return ((h/c)<<32) + l/c;
581 }
582
583 /* av_log API */
584
585 #ifdef AV_LOG_TRAP_PRINTF
586 #undef stderr
587 #undef fprintf
588 #endif
589
590 static int av_log_level = AV_LOG_DEBUG;
591
592 static void av_log_default_callback(AVCodecContext* avctx, int level, const char* fmt, va_list vl)
593 {
594 static int print_prefix=1;
595
596 if(level>av_log_level)
597 return;
598 if(avctx && print_prefix)
599 fprintf(stderr, "[%s @ %p]", avctx->codec ? avctx->codec->name : "?", avctx);
600
601 print_prefix= strstr(fmt, "\n") != NULL;
602
603 vfprintf(stderr, fmt, vl);
604 }
605
606 static void (*av_log_callback)(AVCodecContext*, int, const char*, va_list) = av_log_default_callback;
607
608 void av_log(AVCodecContext* avctx, int level, const char *fmt, ...)
609 {
610 va_list vl;
611 va_start(vl, fmt);
612 av_vlog(avctx, level, fmt, vl);
613 va_end(vl);
614 }
615
616 void av_vlog(AVCodecContext* avctx, int level, const char *fmt, va_list vl)
617 {
618 av_log_callback(avctx, level, fmt, vl);
619 }
620
621 int av_log_get_level(void)
622 {
623 return av_log_level;
624 }
625
626 void av_log_set_level(int level)
627 {
628 av_log_level = level;
629 }
630
631 void av_log_set_callback(void (*callback)(AVCodecContext*, int, const char*, va_list))
632 {
633 av_log_callback = callback;
634 }
635