Mercurial > audlegacy
annotate Plugins/Input/wma/libffwma/utils.c @ 142:5ce1d36e9ff5 trunk
[svn] Don't build wma by default, it's not working.
author | nenolod |
---|---|
date | Thu, 10 Nov 2005 15:56:32 -0800 |
parents | b8d4c1faa6d7 |
children | 12004b385a96 |
rev | line source |
---|---|
137 | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 = 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, unsigned int *size, unsigned int min_size) | |
44 { | |
45 if(min_size < *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 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1)) | |
120 | |
121 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){ | |
122 int w_align= 1; | |
123 int h_align= 1; | |
124 | |
125 switch(s->pix_fmt){ | |
126 case PIX_FMT_YUV420P: | |
127 case PIX_FMT_YUV422: | |
128 case PIX_FMT_YUV422P: | |
129 case PIX_FMT_YUV444P: | |
130 case PIX_FMT_GRAY8: | |
131 case PIX_FMT_YUVJ420P: | |
132 case PIX_FMT_YUVJ422P: | |
133 case PIX_FMT_YUVJ444P: | |
134 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment | |
135 h_align= 16; | |
136 break; | |
137 case PIX_FMT_YUV411P: | |
138 w_align=32; | |
139 h_align=8; | |
140 break; | |
141 case PIX_FMT_YUV410P: | |
142 default: | |
143 w_align= 1; | |
144 h_align= 1; | |
145 break; | |
146 } | |
147 | |
148 *width = ALIGN(*width , w_align); | |
149 *height= ALIGN(*height, h_align); | |
150 } | |
151 | |
152 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){ | |
153 int i; | |
154 InternalBuffer *buf, *last, temp; | |
155 | |
156 assert(pic->type==FF_BUFFER_TYPE_INTERNAL); | |
157 assert(s->internal_buffer_count); | |
158 | |
159 buf = NULL; /* avoids warning */ | |
160 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize | |
161 buf= &((InternalBuffer*)s->internal_buffer)[i]; | |
162 if(buf->data[0] == pic->data[0]) | |
163 break; | |
164 } | |
165 assert(i < s->internal_buffer_count); | |
166 s->internal_buffer_count--; | |
167 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count]; | |
168 | |
169 temp= *buf; | |
170 *buf= *last; | |
171 *last= temp; | |
172 | |
173 for(i=0; i<3; i++){ | |
174 pic->data[i]=NULL; | |
175 // pic->base[i]=NULL; | |
176 } | |
177 } | |
178 | |
179 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){ | |
180 return fmt[0]; | |
181 } | |
182 | |
183 void avcodec_get_context_defaults(AVCodecContext *s){ | |
184 s->bit_rate= 800*1000; | |
185 s->bit_rate_tolerance= s->bit_rate*10; | |
186 s->qmin= 2; | |
187 s->qmax= 31; | |
188 s->mb_qmin= 2; | |
189 s->mb_qmax= 31; | |
190 s->rc_eq= "tex^qComp"; | |
191 s->qcompress= 0.5; | |
192 s->max_qdiff= 3; | |
193 s->b_quant_factor=1.25; | |
194 s->b_quant_offset=1.25; | |
195 s->i_quant_factor=-0.8; | |
196 s->i_quant_offset=0.0; | |
197 s->error_concealment= 3; | |
198 s->error_resilience= 1; | |
199 s->workaround_bugs= FF_BUG_AUTODETECT; | |
200 s->frame_rate_base= 1; | |
201 s->frame_rate = 25; | |
202 s->gop_size= 50; | |
203 s->me_method= ME_EPZS; | |
204 //s->get_buffer= avcodec_default_get_buffer; | |
205 s->release_buffer= avcodec_default_release_buffer; | |
206 s->get_format= avcodec_default_get_format; | |
207 s->me_subpel_quality=8; | |
208 s->lmin= FF_QP2LAMBDA * s->qmin; | |
209 s->lmax= FF_QP2LAMBDA * s->qmax; | |
210 //s->sample_aspect_ratio= (AVRational){0,1}; | |
211 s->ildct_cmp= FF_CMP_VSAD; | |
212 | |
213 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS; | |
214 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS; | |
215 s->palctrl = NULL; | |
216 //s->reget_buffer= avcodec_default_reget_buffer; | |
217 } | |
218 | |
219 /** | |
220 * allocates a AVCodecContext and set it to defaults. | |
221 * this can be deallocated by simply calling free() | |
222 */ | |
223 AVCodecContext *avcodec_alloc_context(void){ | |
142
5ce1d36e9ff5
[svn] Don't build wma by default, it's not working.
nenolod
parents:
137
diff
changeset
|
224 AVCodecContext *avctx= malloc(sizeof(AVCodecContext)); |
137 | 225 |
226 if(avctx==NULL) return NULL; | |
227 | |
228 avcodec_get_context_defaults(avctx); | |
229 | |
230 return avctx; | |
231 } | |
232 | |
233 /** | |
234 * allocates a AVPFrame and set it to defaults. | |
235 * this can be deallocated by simply calling free() | |
236 */ | |
237 AVFrame *avcodec_alloc_frame(void){ | |
142
5ce1d36e9ff5
[svn] Don't build wma by default, it's not working.
nenolod
parents:
137
diff
changeset
|
238 AVFrame *pic= malloc(sizeof(AVFrame)); |
137 | 239 |
240 return pic; | |
241 } | |
242 | |
243 int avcodec_open(AVCodecContext *avctx, AVCodec *codec) | |
244 { | |
245 int ret; | |
246 | |
247 if(avctx->codec) | |
248 return -1; | |
249 | |
250 avctx->codec = codec; | |
251 avctx->codec_id = codec->id; | |
252 avctx->frame_number = 0; | |
253 if (codec->priv_data_size > 0) { | |
142
5ce1d36e9ff5
[svn] Don't build wma by default, it's not working.
nenolod
parents:
137
diff
changeset
|
254 avctx->priv_data = malloc(codec->priv_data_size); |
137 | 255 if (!avctx->priv_data) |
256 return -ENOMEM; | |
257 } else { | |
258 avctx->priv_data = NULL; | |
259 } | |
260 ret = avctx->codec->init(avctx); | |
261 if (ret < 0) { | |
262 av_freep(&avctx->priv_data); | |
263 return ret; | |
264 } | |
265 return 0; | |
266 } | |
267 | |
268 int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, | |
269 const short *samples) | |
270 { | |
271 int ret; | |
272 | |
273 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples); | |
274 avctx->frame_number++; | |
275 return ret; | |
276 } | |
277 | |
278 /* decode an audio frame. return -1 if error, otherwise return the | |
279 *number of bytes used. If no frame could be decompressed, | |
280 *frame_size_ptr is zero. Otherwise, it is the decompressed frame | |
281 *size in BYTES. */ | |
282 int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples, | |
283 int *frame_size_ptr, | |
284 uint8_t *buf, int buf_size) | |
285 { | |
286 int ret; | |
287 ret = avctx->codec->decode(avctx, samples, frame_size_ptr, | |
288 buf, buf_size); | |
289 avctx->frame_number++; | |
290 return ret; | |
291 } | |
292 | |
293 int avcodec_close(AVCodecContext *avctx) | |
294 { | |
295 if (avctx->codec->close) | |
296 avctx->codec->close(avctx); | |
297 av_freep(&avctx->priv_data); | |
298 avctx->codec = NULL; | |
299 return 0; | |
300 } | |
301 | |
302 AVCodec *avcodec_find_encoder(enum CodecID id) | |
303 { | |
304 AVCodec *p; | |
305 p = first_avcodec; | |
306 while (p) { | |
307 if (p->encode != NULL && p->id == id) | |
308 return p; | |
309 p = p->next; | |
310 } | |
311 return NULL; | |
312 } | |
313 | |
314 AVCodec *avcodec_find_encoder_by_name(const char *name) | |
315 { | |
316 AVCodec *p; | |
317 p = first_avcodec; | |
318 while (p) { | |
319 if (p->encode != NULL && strcmp(name,p->name) == 0) | |
320 return p; | |
321 p = p->next; | |
322 } | |
323 return NULL; | |
324 } | |
325 | |
326 AVCodec *avcodec_find_decoder(enum CodecID id) | |
327 { | |
328 AVCodec *p; | |
329 p = first_avcodec; | |
330 while (p) { | |
331 if (p->decode != NULL && p->id == id) | |
332 return p; | |
333 p = p->next; | |
334 } | |
335 return NULL; | |
336 } | |
337 | |
338 AVCodec *avcodec_find_decoder_by_name(const char *name) | |
339 { | |
340 AVCodec *p; | |
341 p = first_avcodec; | |
342 while (p) { | |
343 if (p->decode != NULL && strcmp(name,p->name) == 0) | |
344 return p; | |
345 p = p->next; | |
346 } | |
347 return NULL; | |
348 } | |
349 | |
350 AVCodec *avcodec_find(enum CodecID id) | |
351 { | |
352 AVCodec *p; | |
353 p = first_avcodec; | |
354 while (p) { | |
355 if (p->id == id) | |
356 return p; | |
357 p = p->next; | |
358 } | |
359 return NULL; | |
360 } | |
361 | |
362 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) | |
363 { | |
364 const char *codec_name; | |
365 AVCodec *p; | |
366 char buf1[32]; | |
367 char channels_str[100]; | |
368 int bitrate; | |
369 | |
370 if (encode) | |
371 p = avcodec_find_encoder(enc->codec_id); | |
372 else | |
373 p = avcodec_find_decoder(enc->codec_id); | |
374 | |
375 if (p) { | |
376 codec_name = p->name; | |
377 } else if (enc->codec_name[0] != '\0') { | |
378 codec_name = enc->codec_name; | |
379 } else { | |
380 /* output avi tags */ | |
381 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag); | |
382 codec_name = buf1; | |
383 } | |
384 | |
385 switch(enc->codec_type) { | |
386 case CODEC_TYPE_AUDIO: | |
387 snprintf(buf, buf_size, | |
388 "Audio: %s", | |
389 codec_name); | |
390 switch (enc->channels) { | |
391 case 1: | |
392 strcpy(channels_str, "mono"); | |
393 break; | |
394 case 2: | |
395 strcpy(channels_str, "stereo"); | |
396 break; | |
397 case 6: | |
398 strcpy(channels_str, "5:1"); | |
399 break; | |
400 default: | |
401 sprintf(channels_str, "%d channels", enc->channels); | |
402 break; | |
403 } | |
404 if (enc->sample_rate) { | |
405 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
406 ", %d Hz, %s", | |
407 enc->sample_rate, | |
408 channels_str); | |
409 } | |
410 | |
411 /* for PCM codecs, compute bitrate directly */ | |
412 switch(enc->codec_id) { | |
413 case CODEC_ID_PCM_S16LE: | |
414 case CODEC_ID_PCM_S16BE: | |
415 case CODEC_ID_PCM_U16LE: | |
416 case CODEC_ID_PCM_U16BE: | |
417 bitrate = enc->sample_rate * enc->channels * 16; | |
418 break; | |
419 case CODEC_ID_PCM_S8: | |
420 case CODEC_ID_PCM_U8: | |
421 case CODEC_ID_PCM_ALAW: | |
422 case CODEC_ID_PCM_MULAW: | |
423 bitrate = enc->sample_rate * enc->channels * 8; | |
424 break; | |
425 default: | |
426 bitrate = enc->bit_rate; | |
427 break; | |
428 } | |
429 break; | |
430 case CODEC_TYPE_DATA: | |
431 snprintf(buf, buf_size, "Data: %s", codec_name); | |
432 bitrate = enc->bit_rate; | |
433 break; | |
434 default: | |
435 av_abort(); | |
436 } | |
437 if (encode) { | |
438 if (enc->flags & CODEC_FLAG_PASS1) | |
439 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
440 ", pass 1"); | |
441 if (enc->flags & CODEC_FLAG_PASS2) | |
442 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
443 ", pass 2"); | |
444 } | |
445 if (bitrate != 0) { | |
446 snprintf(buf + strlen(buf), buf_size - strlen(buf), | |
447 ", %d kb/s", bitrate / 1000); | |
448 } | |
449 } | |
450 | |
451 unsigned avcodec_version( void ) | |
452 { | |
453 return LIBAVCODEC_VERSION_INT; | |
454 } | |
455 | |
456 unsigned avcodec_build( void ) | |
457 { | |
458 return LIBAVCODEC_BUILD; | |
459 } | |
460 | |
461 /* must be called before any other functions */ | |
462 void avcodec_init(void) | |
463 { | |
464 static int inited = 0; | |
465 | |
466 if (inited != 0) | |
467 return; | |
468 inited = 1; | |
469 | |
470 dsputil_static_init(); | |
471 } | |
472 | |
473 /** | |
474 * Flush buffers, should be called when seeking or when swicthing to a different stream. | |
475 */ | |
476 void avcodec_flush_buffers(AVCodecContext *avctx) | |
477 { | |
478 if(avctx->codec->flush) | |
479 avctx->codec->flush(avctx); | |
480 } | |
481 | |
482 void avcodec_default_free_buffers(AVCodecContext *s){ | |
483 int i, j; | |
484 | |
485 if(s->internal_buffer==NULL) return; | |
486 | |
487 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){ | |
488 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i]; | |
489 for(j=0; j<4; j++){ | |
490 av_freep(&buf->base[j]); | |
491 buf->data[j]= NULL; | |
492 } | |
493 } | |
494 av_freep(&s->internal_buffer); | |
495 | |
496 s->internal_buffer_count=0; | |
497 } | |
498 #if 0 | |
499 char av_get_pict_type_char(int pict_type){ | |
500 switch(pict_type){ | |
501 case I_TYPE: return 'I'; | |
502 case P_TYPE: return 'P'; | |
503 case B_TYPE: return 'B'; | |
504 case S_TYPE: return 'S'; | |
505 case SI_TYPE:return 'i'; | |
506 case SP_TYPE:return 'p'; | |
507 default: return '?'; | |
508 } | |
509 } | |
510 | |
511 int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){ | |
512 int exact=1, sign=0; | |
513 int64_t gcd; | |
514 | |
515 assert(den != 0); | |
516 | |
517 if(den < 0){ | |
518 den= -den; | |
519 nom= -nom; | |
520 } | |
521 | |
522 if(nom < 0){ | |
523 nom= -nom; | |
524 sign= 1; | |
525 } | |
526 | |
527 gcd = ff_gcd(nom, den); | |
528 nom /= gcd; | |
529 den /= gcd; | |
530 | |
531 if(nom > max || den > max){ | |
532 AVRational a0={0,1}, a1={1,0}; | |
533 exact=0; | |
534 | |
535 for(;;){ | |
536 int64_t x= nom / den; | |
537 int64_t a2n= x*a1.num + a0.num; | |
538 int64_t a2d= x*a1.den + a0.den; | |
539 | |
540 if(a2n > max || a2d > max) break; | |
541 | |
542 nom %= den; | |
543 | |
544 a0= a1; | |
545 a1= (AVRational){a2n, a2d}; | |
546 if(nom==0) break; | |
547 x= nom; nom=den; den=x; | |
548 } | |
549 nom= a1.num; | |
550 den= a1.den; | |
551 } | |
552 | |
553 assert(ff_gcd(nom, den) == 1); | |
554 | |
555 if(sign) nom= -nom; | |
556 | |
557 *dst_nom = nom; | |
558 *dst_den = den; | |
559 | |
560 return exact; | |
561 } | |
562 #endif | |
563 int64_t av_rescale(int64_t a, int b, int c){ | |
564 uint64_t h, l; | |
565 assert(c > 0); | |
566 assert(b >=0); | |
567 | |
568 if(a<0) return -av_rescale(-a, b, c); | |
569 | |
570 h= a>>32; | |
571 if(h==0) return a*b/c; | |
572 | |
573 l= a&0xFFFFFFFF; | |
574 l *= b; | |
575 h *= b; | |
576 | |
577 l += (h%c)<<32; | |
578 | |
579 return ((h/c)<<32) + l/c; | |
580 } | |
581 | |
582 /* av_log API */ | |
583 | |
584 #ifdef AV_LOG_TRAP_PRINTF | |
585 #undef stderr | |
586 #undef fprintf | |
587 #endif | |
588 | |
589 static int av_log_level = AV_LOG_DEBUG; | |
590 | |
591 static void av_log_default_callback(AVCodecContext* avctx, int level, const char* fmt, va_list vl) | |
592 { | |
593 static int print_prefix=1; | |
594 | |
595 if(level>av_log_level) | |
596 return; | |
597 if(avctx && print_prefix) | |
598 fprintf(stderr, "[%s @ %p]", avctx->codec ? avctx->codec->name : "?", avctx); | |
599 | |
600 print_prefix= strstr(fmt, "\n") != NULL; | |
601 | |
602 vfprintf(stderr, fmt, vl); | |
603 } | |
604 | |
605 static void (*av_log_callback)(AVCodecContext*, int, const char*, va_list) = av_log_default_callback; | |
606 | |
607 void av_log(AVCodecContext* avctx, int level, const char *fmt, ...) | |
608 { | |
609 va_list vl; | |
610 va_start(vl, fmt); | |
611 av_vlog(avctx, level, fmt, vl); | |
612 va_end(vl); | |
613 } | |
614 | |
615 void av_vlog(AVCodecContext* avctx, int level, const char *fmt, va_list vl) | |
616 { | |
617 av_log_callback(avctx, level, fmt, vl); | |
618 } | |
619 | |
620 int av_log_get_level(void) | |
621 { | |
622 return av_log_level; | |
623 } | |
624 | |
625 void av_log_set_level(int level) | |
626 { | |
627 av_log_level = level; | |
628 } | |
629 | |
630 void av_log_set_callback(void (*callback)(AVCodecContext*, int, const char*, va_list)) | |
631 { | |
632 av_log_callback = callback; | |
633 } | |
634 |