Gigabeat S: Will run pacbox at 60fps. Set FPS accordingly.
[kugel-rb.git] / apps / codecs / libmad / mad.h
blob81ac20c3733800b27af5a8be5a80c7c7685c0eb6
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * If you would like to negotiate alternate licensing terms, you may do
20 * so by contacting: Underbit Technologies, Inc. <info@underbit.com>
23 #include <inttypes.h>
25 # ifdef __cplusplus
26 extern "C" {
27 # endif
29 # define FPM_DEFAULT
31 /* Id: version.h,v 1.26 2004/01/23 09:41:33 rob Exp */
33 # ifndef LIBMAD_VERSION_H
34 # define LIBMAD_VERSION_H
36 # define MAD_VERSION_MAJOR 0
37 # define MAD_VERSION_MINOR 15
38 # define MAD_VERSION_PATCH 1
39 # define MAD_VERSION_EXTRA " (beta)"
41 # define MAD_VERSION_STRINGIZE(str) #str
42 # define MAD_VERSION_STRING(num) MAD_VERSION_STRINGIZE(num)
44 # define MAD_VERSION MAD_VERSION_STRING(MAD_VERSION_MAJOR) "." \
45 MAD_VERSION_STRING(MAD_VERSION_MINOR) "." \
46 MAD_VERSION_STRING(MAD_VERSION_PATCH) \
47 MAD_VERSION_EXTRA
49 # define MAD_PUBLISHYEAR "2000-2004"
50 # define MAD_AUTHOR "Underbit Technologies, Inc."
51 # define MAD_EMAIL "info@underbit.com"
53 extern char const mad_version[];
54 extern char const mad_copyright[];
55 extern char const mad_author[];
56 extern char const mad_build[];
58 # endif
60 /* Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp */
62 # ifndef LIBMAD_FIXED_H
63 # define LIBMAD_FIXED_H
65 typedef int32_t mad_fixed_t;
67 typedef int32_t mad_fixed64hi_t;
68 typedef uint32_t mad_fixed64lo_t;
70 # if defined(_MSC_VER)
71 # define mad_fixed64_t signed __int64
72 # elif 1 || defined(__GNUC__)
73 # define mad_fixed64_t signed long long
74 # endif
76 # if defined(FPM_FLOAT)
77 typedef double mad_sample_t;
78 # else
79 typedef mad_fixed_t mad_sample_t;
80 # endif
83 * Fixed-point format: 0xABBBBBBB
84 * A == whole part (sign + 3 bits)
85 * B == fractional part (28 bits)
87 * Values are signed two's complement, so the effective range is:
88 * 0x80000000 to 0x7fffffff
89 * -8.0 to +7.9999999962747097015380859375
91 * The smallest representable value is:
92 * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
94 * 28 bits of fractional accuracy represent about
95 * 8.6 digits of decimal accuracy.
97 * Fixed-point numbers can be added or subtracted as normal
98 * integers, but multiplication requires shifting the 64-bit result
99 * from 56 fractional bits back to 28 (and rounding.)
101 * Changing the definition of MAD_F_FRACBITS is only partially
102 * supported, and must be done with care.
105 # define MAD_F_FRACBITS 28
107 # if MAD_F_FRACBITS == 28
108 # define MAD_F(x) ((mad_fixed_t) (x##L))
109 # else
110 # if MAD_F_FRACBITS < 28
111 # warning "MAD_F_FRACBITS < 28"
112 # define MAD_F(x) ((mad_fixed_t) \
113 (((x##L) + \
114 (1L << (28 - MAD_F_FRACBITS - 1))) >> \
115 (28 - MAD_F_FRACBITS)))
116 # elif MAD_F_FRACBITS > 28
117 # error "MAD_F_FRACBITS > 28 not currently supported"
118 # define MAD_F(x) ((mad_fixed_t) \
119 ((x##L) << (MAD_F_FRACBITS - 28)))
120 # endif
121 # endif
123 # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
124 # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
126 # define MAD_F_ONE MAD_F(0x10000000)
128 # define mad_f_tofixed(x) ((mad_fixed_t) \
129 ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
130 # define mad_f_todouble(x) ((double) \
131 ((x) / (double) (1L << MAD_F_FRACBITS)))
133 # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
134 # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
135 /* (x should be positive) */
137 # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
139 # define mad_f_add(x, y) ((x) + (y))
140 # define mad_f_sub(x, y) ((x) - (y))
142 # if defined(FPM_FLOAT)
143 # error "FPM_FLOAT not yet supported"
145 # undef MAD_F
146 # define MAD_F(x) mad_f_todouble(x)
148 # define mad_f_mul(x, y) ((x) * (y))
149 # define mad_f_scale64
151 # undef ASO_ZEROCHECK
153 # elif defined(FPM_64BIT)
156 * This version should be the most accurate if 64-bit types are supported by
157 * the compiler, although it may not be the most efficient.
159 # if defined(OPT_ACCURACY)
160 # define mad_f_mul(x, y) \
161 ((mad_fixed_t) \
162 ((((mad_fixed64_t) (x) * (y)) + \
163 (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
164 # else
165 # define mad_f_mul(x, y) \
166 ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
167 # endif
169 # define MAD_F_SCALEBITS MAD_F_FRACBITS
171 /* --- Intel --------------------------------------------------------------- */
173 # elif defined(FPM_INTEL)
175 # if defined(_MSC_VER)
176 # pragma warning(push)
177 # pragma warning(disable: 4035) /* no return value */
178 static __forceinline
179 mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
181 enum {
182 fracbits = MAD_F_FRACBITS
185 __asm {
186 mov eax, x
187 imul y
188 shrd eax, edx, fracbits
191 /* implicit return of eax */
193 # pragma warning(pop)
195 # define mad_f_mul mad_f_mul_inline
196 # define mad_f_scale64
197 # else
199 * This Intel version is fast and accurate; the disposition of the least
200 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
202 # define MAD_F_MLX(hi, lo, x, y) \
203 asm ("imull %3" \
204 : "=a" (lo), "=d" (hi) \
205 : "%a" (x), "rm" (y) \
206 : "cc")
208 # if defined(OPT_ACCURACY)
210 * This gives best accuracy but is not very fast.
212 # define MAD_F_MLA(hi, lo, x, y) \
213 ({ mad_fixed64hi_t __hi; \
214 mad_fixed64lo_t __lo; \
215 MAD_F_MLX(__hi, __lo, (x), (y)); \
216 asm ("addl %2,%0\n\t" \
217 "adcl %3,%1" \
218 : "=rm" (lo), "=rm" (hi) \
219 : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
220 : "cc"); \
222 # endif /* OPT_ACCURACY */
224 # if defined(OPT_ACCURACY)
226 * Surprisingly, this is faster than SHRD followed by ADC.
228 # define mad_f_scale64(hi, lo) \
229 ({ mad_fixed64hi_t __hi_; \
230 mad_fixed64lo_t __lo_; \
231 mad_fixed_t __result; \
232 asm ("addl %4,%2\n\t" \
233 "adcl %5,%3" \
234 : "=rm" (__lo_), "=rm" (__hi_) \
235 : "0" (lo), "1" (hi), \
236 "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
237 : "cc"); \
238 asm ("shrdl %3,%2,%1" \
239 : "=rm" (__result) \
240 : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
241 : "cc"); \
242 __result; \
244 # elif defined(OPT_INTEL)
246 * Alternate Intel scaling that may or may not perform better.
248 # define mad_f_scale64(hi, lo) \
249 ({ mad_fixed_t __result; \
250 asm ("shrl %3,%1\n\t" \
251 "shll %4,%2\n\t" \
252 "orl %2,%1" \
253 : "=rm" (__result) \
254 : "0" (lo), "r" (hi), \
255 "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
256 : "cc"); \
257 __result; \
259 # else
260 # define mad_f_scale64(hi, lo) \
261 ({ mad_fixed_t __result; \
262 asm ("shrdl %3,%2,%1" \
263 : "=rm" (__result) \
264 : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
265 : "cc"); \
266 __result; \
268 # endif /* OPT_ACCURACY */
270 # define MAD_F_SCALEBITS MAD_F_FRACBITS
271 # endif
273 /* --- ARM ----------------------------------------------------------------- */
275 # elif defined(FPM_ARM)
278 * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
279 * least significant bit is properly rounded at no CPU cycle cost!
281 # if 1
283 * This is faster than the default implementation via MAD_F_MLX() and
284 * mad_f_scale64().
286 # define mad_f_mul(x, y) \
287 ({ mad_fixed64hi_t __hi; \
288 mad_fixed64lo_t __lo; \
289 mad_fixed_t __result; \
290 asm ("smull %0, %1, %3, %4\n\t" \
291 "movs %0, %0, lsr %5\n\t" \
292 "adc %2, %0, %1, lsl %6" \
293 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
294 : "%r" (x), "r" (y), \
295 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
296 : "cc"); \
297 __result; \
299 # endif
301 # define MAD_F_MLX(hi, lo, x, y) \
302 asm ("smull %0, %1, %2, %3" \
303 : "=&r" (lo), "=&r" (hi) \
304 : "%r" (x), "r" (y))
306 # define MAD_F_MLA(hi, lo, x, y) \
307 asm ("smlal %0, %1, %2, %3" \
308 : "+r" (lo), "+r" (hi) \
309 : "%r" (x), "r" (y))
311 # define MAD_F_MLN(hi, lo) \
312 asm ("rsbs %0, %2, #0\n\t" \
313 "rsc %1, %3, #0" \
314 : "=r" (lo), "=r" (hi) \
315 : "0" (lo), "1" (hi) \
316 : "cc")
318 # define mad_f_scale64(hi, lo) \
319 ({ mad_fixed_t __result; \
320 asm ("movs %0, %1, lsr %3\n\t" \
321 "adc %0, %0, %2, lsl %4" \
322 : "=&r" (__result) \
323 : "r" (lo), "r" (hi), \
324 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
325 : "cc"); \
326 __result; \
329 # define MAD_F_SCALEBITS MAD_F_FRACBITS
331 /* --- MIPS ---------------------------------------------------------------- */
333 # elif defined(FPM_MIPS)
336 * This MIPS version is fast and accurate; the disposition of the least
337 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
339 # define MAD_F_MLX(hi, lo, x, y) \
340 asm ("mult %2,%3" \
341 : "=l" (lo), "=h" (hi) \
342 : "%r" (x), "r" (y))
344 # if defined(HAVE_MADD_ASM)
345 # define MAD_F_MLA(hi, lo, x, y) \
346 asm ("madd %2,%3" \
347 : "+l" (lo), "+h" (hi) \
348 : "%r" (x), "r" (y))
349 # elif defined(HAVE_MADD16_ASM)
351 * This loses significant accuracy due to the 16-bit integer limit in the
352 * multiply/accumulate instruction.
354 # define MAD_F_ML0(hi, lo, x, y) \
355 asm ("mult %2,%3" \
356 : "=l" (lo), "=h" (hi) \
357 : "%r" ((x) >> 12), "r" ((y) >> 16))
358 # define MAD_F_MLA(hi, lo, x, y) \
359 asm ("madd16 %2,%3" \
360 : "+l" (lo), "+h" (hi) \
361 : "%r" ((x) >> 12), "r" ((y) >> 16))
362 # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
363 # endif
365 # if defined(OPT_SPEED)
366 # define mad_f_scale64(hi, lo) \
367 ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
368 # define MAD_F_SCALEBITS MAD_F_FRACBITS
369 # endif
371 /* --- SPARC --------------------------------------------------------------- */
373 # elif defined(FPM_SPARC)
376 * This SPARC V8 version is fast and accurate; the disposition of the least
377 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
379 # define MAD_F_MLX(hi, lo, x, y) \
380 asm ("smul %2, %3, %0\n\t" \
381 "rd %%y, %1" \
382 : "=r" (lo), "=r" (hi) \
383 : "%r" (x), "rI" (y))
385 /* --- PowerPC ------------------------------------------------------------- */
387 # elif defined(FPM_PPC)
390 * This PowerPC version is fast and accurate; the disposition of the least
391 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
393 # define MAD_F_MLX(hi, lo, x, y) \
394 do { \
395 asm ("mullw %0,%1,%2" \
396 : "=r" (lo) \
397 : "%r" (x), "r" (y)); \
398 asm ("mulhw %0,%1,%2" \
399 : "=r" (hi) \
400 : "%r" (x), "r" (y)); \
402 while (0)
404 # if defined(OPT_ACCURACY)
406 * This gives best accuracy but is not very fast.
408 # define MAD_F_MLA(hi, lo, x, y) \
409 ({ mad_fixed64hi_t __hi; \
410 mad_fixed64lo_t __lo; \
411 MAD_F_MLX(__hi, __lo, (x), (y)); \
412 asm ("addc %0,%2,%3\n\t" \
413 "adde %1,%4,%5" \
414 : "=r" (lo), "=r" (hi) \
415 : "%r" (lo), "r" (__lo), \
416 "%r" (hi), "r" (__hi) \
417 : "xer"); \
419 # endif
421 # if defined(OPT_ACCURACY)
423 * This is slower than the truncating version below it.
425 # define mad_f_scale64(hi, lo) \
426 ({ mad_fixed_t __result, __round; \
427 asm ("rotrwi %0,%1,%2" \
428 : "=r" (__result) \
429 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
430 asm ("extrwi %0,%1,1,0" \
431 : "=r" (__round) \
432 : "r" (__result)); \
433 asm ("insrwi %0,%1,%2,0" \
434 : "+r" (__result) \
435 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
436 asm ("add %0,%1,%2" \
437 : "=r" (__result) \
438 : "%r" (__result), "r" (__round)); \
439 __result; \
441 # else
442 # define mad_f_scale64(hi, lo) \
443 ({ mad_fixed_t __result; \
444 asm ("rotrwi %0,%1,%2" \
445 : "=r" (__result) \
446 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
447 asm ("insrwi %0,%1,%2,0" \
448 : "+r" (__result) \
449 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
450 __result; \
452 # endif
454 # define MAD_F_SCALEBITS MAD_F_FRACBITS
456 /* --- Default ------------------------------------------------------------- */
458 # elif defined(FPM_DEFAULT)
461 * This version is the most portable but it loses significant accuracy.
462 * Furthermore, accuracy is biased against the second argument, so care
463 * should be taken when ordering operands.
465 * The scale factors are constant as this is not used with SSO.
467 * Pre-rounding is required to stay within the limits of compliance.
469 # if defined(OPT_SPEED)
470 # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
471 # else
472 # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
473 (((y) + (1L << 15)) >> 16))
474 # endif
476 /* ------------------------------------------------------------------------- */
478 # else
479 # error "no FPM selected"
480 # endif
482 /* default implementations */
484 # if !defined(mad_f_mul)
485 # define mad_f_mul(x, y) \
486 ({ register mad_fixed64hi_t __hi; \
487 register mad_fixed64lo_t __lo; \
488 MAD_F_MLX(__hi, __lo, (x), (y)); \
489 mad_f_scale64(__hi, __lo); \
491 # endif
493 # if !defined(MAD_F_MLA)
494 # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
495 # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
496 # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
497 # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
498 # endif
500 # if !defined(MAD_F_ML0)
501 # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
502 # endif
504 # if !defined(MAD_F_MLN)
505 # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
506 # endif
508 # if !defined(MAD_F_MLZ)
509 # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
510 # endif
512 # if !defined(mad_f_scale64)
513 # if defined(OPT_ACCURACY)
514 # define mad_f_scale64(hi, lo) \
515 ((((mad_fixed_t) \
516 (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
517 ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
518 # else
519 # define mad_f_scale64(hi, lo) \
520 ((mad_fixed_t) \
521 (((hi) << (32 - MAD_F_SCALEBITS)) | \
522 ((lo) >> MAD_F_SCALEBITS)))
523 # endif
524 # define MAD_F_SCALEBITS MAD_F_FRACBITS
525 # endif
527 /* C routines */
529 mad_fixed_t mad_f_abs(mad_fixed_t);
530 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
532 # endif
534 /* Id: bit.h,v 1.12 2004/01/23 09:41:32 rob Exp */
536 # ifndef LIBMAD_BIT_H
537 # define LIBMAD_BIT_H
539 struct mad_bitptr {
540 unsigned char const *byte;
541 unsigned short cache;
542 unsigned short left;
545 void mad_bit_init(struct mad_bitptr *, unsigned char const *);
547 # define mad_bit_finish(bitptr) /* nothing */
549 unsigned int mad_bit_length(struct mad_bitptr const *,
550 struct mad_bitptr const *);
552 # define mad_bit_bitsleft(bitptr) ((bitptr)->left)
553 unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *);
555 void mad_bit_skip(struct mad_bitptr *, unsigned int);
556 unsigned long mad_bit_read(struct mad_bitptr *, unsigned int);
557 void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long);
559 unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short);
561 # endif
563 /* Id: timer.h,v 1.16 2004/01/23 09:41:33 rob Exp */
565 # ifndef LIBMAD_TIMER_H
566 # define LIBMAD_TIMER_H
568 typedef struct {
569 signed long seconds; /* whole seconds */
570 unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */
571 } mad_timer_t;
573 extern mad_timer_t const mad_timer_zero;
575 # define MAD_TIMER_RESOLUTION 352800000UL
577 enum mad_units {
578 MAD_UNITS_HOURS = -2,
579 MAD_UNITS_MINUTES = -1,
580 MAD_UNITS_SECONDS = 0,
582 /* metric units */
584 MAD_UNITS_DECISECONDS = 10,
585 MAD_UNITS_CENTISECONDS = 100,
586 MAD_UNITS_MILLISECONDS = 1000,
588 /* audio sample units */
590 MAD_UNITS_8000_HZ = 8000,
591 MAD_UNITS_11025_HZ = 11025,
592 MAD_UNITS_12000_HZ = 12000,
594 MAD_UNITS_16000_HZ = 16000,
595 MAD_UNITS_22050_HZ = 22050,
596 MAD_UNITS_24000_HZ = 24000,
598 MAD_UNITS_32000_HZ = 32000,
599 MAD_UNITS_44100_HZ = 44100,
600 MAD_UNITS_48000_HZ = 48000,
602 /* video frame/field units */
604 MAD_UNITS_24_FPS = 24,
605 MAD_UNITS_25_FPS = 25,
606 MAD_UNITS_30_FPS = 30,
607 MAD_UNITS_48_FPS = 48,
608 MAD_UNITS_50_FPS = 50,
609 MAD_UNITS_60_FPS = 60,
611 /* CD audio frames */
613 MAD_UNITS_75_FPS = 75,
615 /* video drop-frame units */
617 MAD_UNITS_23_976_FPS = -24,
618 MAD_UNITS_24_975_FPS = -25,
619 MAD_UNITS_29_97_FPS = -30,
620 MAD_UNITS_47_952_FPS = -48,
621 MAD_UNITS_49_95_FPS = -50,
622 MAD_UNITS_59_94_FPS = -60
625 # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero))
627 int mad_timer_compare(mad_timer_t, mad_timer_t);
629 # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero)
631 void mad_timer_negate(mad_timer_t *);
632 mad_timer_t mad_timer_abs(mad_timer_t);
634 void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long);
635 void mad_timer_add(mad_timer_t *, mad_timer_t);
636 void mad_timer_multiply(mad_timer_t *, signed long);
638 signed long mad_timer_count(mad_timer_t, enum mad_units);
639 unsigned long mad_timer_fraction(mad_timer_t, unsigned long);
640 void mad_timer_string(mad_timer_t, char *, char const *,
641 enum mad_units, enum mad_units, unsigned long);
643 # endif
645 /* Id: stream.h,v 1.20 2004/02/05 09:02:39 rob Exp */
647 # ifndef LIBMAD_STREAM_H
648 # define LIBMAD_STREAM_H
651 # define MAD_BUFFER_GUARD 8
652 # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD)
654 enum mad_error {
655 MAD_ERROR_NONE = 0x0000, /* no error */
657 MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */
658 MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */
660 MAD_ERROR_NOMEM = 0x0031, /* not enough memory */
662 MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */
663 MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */
664 MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */
665 MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */
666 MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */
668 MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */
669 MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */
670 MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */
671 MAD_ERROR_BADMODE = 0x0222, /* bad bitrate/mode combination */
672 MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */
673 MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */
674 MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */
675 MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */
676 MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */
677 MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */
678 MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */
679 MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */
680 MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */
683 # define MAD_RECOVERABLE(error) ((error) & 0xff00)
685 struct mad_stream {
686 unsigned char const *buffer; /* input bitstream buffer */
687 unsigned char const *bufend; /* end of buffer */
688 unsigned long skiplen; /* bytes to skip before next frame */
690 int sync; /* stream sync found */
691 unsigned long freerate; /* free bitrate (fixed) */
693 unsigned char const *this_frame; /* start of current frame */
694 unsigned char const *next_frame; /* start of next frame */
695 struct mad_bitptr ptr; /* current processing bit pointer */
697 struct mad_bitptr anc_ptr; /* ancillary bits pointer */
698 unsigned int anc_bitlen; /* number of ancillary bits */
700 unsigned char (*main_data)[MAD_BUFFER_MDLEN];
701 /* Layer III main_data() */
702 unsigned int md_len; /* bytes in main_data */
704 int options; /* decoding options (see below) */
705 enum mad_error error; /* error code (see above) */
708 enum {
709 MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */
710 MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */
711 # if 0 /* not yet implemented */
712 MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */
713 MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */
714 MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */
715 # endif
718 void mad_stream_init(struct mad_stream *);
719 void mad_stream_finish(struct mad_stream *);
721 # define mad_stream_options(stream, opts) \
722 ((void) ((stream)->options = (opts)))
724 void mad_stream_buffer(struct mad_stream *,
725 unsigned char const *, unsigned long);
726 void mad_stream_skip(struct mad_stream *, unsigned long);
728 int mad_stream_sync(struct mad_stream *);
730 char const *mad_stream_errorstr(struct mad_stream const *);
732 # endif
734 /* Id: frame.h,v 1.20 2004/01/23 09:41:32 rob Exp */
736 # ifndef LIBMAD_FRAME_H
737 # define LIBMAD_FRAME_H
740 enum mad_layer {
741 MAD_LAYER_I = 1, /* Layer I */
742 MAD_LAYER_II = 2, /* Layer II */
743 MAD_LAYER_III = 3 /* Layer III */
746 enum mad_mode {
747 MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */
748 MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */
749 MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */
750 MAD_MODE_STEREO = 3 /* normal LR stereo */
753 enum mad_emphasis {
754 MAD_EMPHASIS_NONE = 0, /* no emphasis */
755 MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */
756 MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */
757 MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */
760 struct mad_header {
761 enum mad_layer layer; /* audio layer (1, 2, or 3) */
762 enum mad_mode mode; /* channel mode (see above) */
763 int mode_extension; /* additional mode info */
764 enum mad_emphasis emphasis; /* de-emphasis to use (see above) */
766 unsigned long bitrate; /* stream bitrate (bps) */
767 unsigned int samplerate; /* sampling frequency (Hz) */
769 unsigned short crc_check; /* frame CRC accumulator */
770 unsigned short crc_target; /* final target CRC checksum */
772 int flags; /* flags (see below) */
773 int private_bits; /* private bits (see below) */
775 mad_timer_t duration; /* audio playing time of frame */
778 struct mad_frame {
779 struct mad_header header; /* MPEG audio header */
780 int options; /* decoding options (from stream) */
782 mad_fixed_t (*sbsample)[2][36][32]; /* synthesis subband filter samples */
783 mad_fixed_t (*sbsample_prev)[2][36][32]; /* synthesis subband filter samples
784 from previous frame only needed
785 when synthesis is on cop */
787 mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */
790 # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1)
791 # define MAD_NSBSAMPLES(header) \
792 ((header)->layer == MAD_LAYER_I ? 12 : \
793 (((header)->layer == MAD_LAYER_III && \
794 ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36))
796 enum {
797 MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */
798 MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */
800 MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */
801 MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */
802 MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */
803 MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */
805 MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */
806 MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */
807 MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */
809 MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */
810 MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */
811 MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */
814 enum {
815 MAD_PRIVATE_HEADER = 0x0100, /* header private bit */
816 MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */
819 void mad_header_init(struct mad_header *);
821 # define mad_header_finish(header) /* nothing */
823 int mad_header_decode(struct mad_header *, struct mad_stream *);
825 void mad_frame_init(struct mad_frame *);
826 void mad_frame_finish(struct mad_frame *);
828 int mad_frame_decode(struct mad_frame *, struct mad_stream *);
830 void mad_frame_mute(struct mad_frame *);
832 # endif
834 /* Id: synth.h,v 1.15 2004/01/23 09:41:33 rob Exp */
836 # ifndef LIBMAD_SYNTH_H
837 # define LIBMAD_SYNTH_H
840 struct mad_pcm {
841 unsigned int samplerate; /* sampling frequency (Hz) */
842 unsigned short channels; /* number of channels */
843 unsigned short length; /* number of samples per channel */
844 mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */
847 struct mad_synth {
848 mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */
849 /* [ch][eo][peo][s][v] */
851 unsigned int phase; /* current processing phase */
853 struct mad_pcm pcm; /* PCM output */
856 /* single channel PCM selector */
857 enum {
858 MAD_PCM_CHANNEL_SINGLE = 0
861 /* dual channel PCM selector */
862 enum {
863 MAD_PCM_CHANNEL_DUAL_1 = 0,
864 MAD_PCM_CHANNEL_DUAL_2 = 1
867 /* stereo PCM selector */
868 enum {
869 MAD_PCM_CHANNEL_STEREO_LEFT = 0,
870 MAD_PCM_CHANNEL_STEREO_RIGHT = 1
873 void mad_synth_init(struct mad_synth *);
875 # define mad_synth_finish(synth) /* nothing */
877 void mad_synth_mute(struct mad_synth *);
879 void mad_synth_frame(struct mad_synth *, struct mad_frame const *);
881 # endif
883 /* Id: decoder.h,v 1.17 2004/01/23 09:41:32 rob Exp */
885 # ifndef LIBMAD_DECODER_H
886 # define LIBMAD_DECODER_H
889 enum mad_decoder_mode {
890 MAD_DECODER_MODE_SYNC = 0,
891 MAD_DECODER_MODE_ASYNC
894 enum mad_flow {
895 MAD_FLOW_CONTINUE = 0x0000, /* continue normally */
896 MAD_FLOW_STOP = 0x0010, /* stop decoding normally */
897 MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */
898 MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */
901 struct mad_decoder {
902 enum mad_decoder_mode mode;
904 int options;
906 struct {
907 long pid;
908 int in;
909 int out;
910 } async;
912 struct {
913 struct mad_stream stream;
914 struct mad_frame frame;
915 struct mad_synth synth;
916 } *sync;
918 void *cb_data;
920 enum mad_flow (*input_func)(void *, struct mad_stream *);
921 enum mad_flow (*header_func)(void *, struct mad_header const *);
922 enum mad_flow (*filter_func)(void *,
923 struct mad_stream const *, struct mad_frame *);
924 enum mad_flow (*output_func)(void *,
925 struct mad_header const *, struct mad_pcm *);
926 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
927 enum mad_flow (*message_func)(void *, void *, unsigned int *);
930 void mad_decoder_init(struct mad_decoder *, void *,
931 enum mad_flow (*)(void *, struct mad_stream *),
932 enum mad_flow (*)(void *, struct mad_header const *),
933 enum mad_flow (*)(void *,
934 struct mad_stream const *,
935 struct mad_frame *),
936 enum mad_flow (*)(void *,
937 struct mad_header const *,
938 struct mad_pcm *),
939 enum mad_flow (*)(void *,
940 struct mad_stream *,
941 struct mad_frame *),
942 enum mad_flow (*)(void *, void *, unsigned int *));
943 int mad_decoder_finish(struct mad_decoder *);
945 # define mad_decoder_options(decoder, opts) \
946 ((void) ((decoder)->options = (opts)))
948 int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode);
949 int mad_decoder_message(struct mad_decoder *, void *, unsigned int *);
951 # endif
953 # ifdef __cplusplus
955 # endif