check for a disc before mounting to avoid IOS breakage
[libogc.git] / gc / mad / mad.h
blob34d966c5d37dde4b3a8ec222476fc6e8538368ad
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2003 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 # ifdef __cplusplus
24 extern "C" {
25 # endif
27 #include <gctypes.h>
29 # define FPM_PPC
31 # define SIZEOF_INT 4
32 # define SIZEOF_LONG 4
33 # define SIZEOF_LONG_LONG 8
36 # ifndef LIBMAD_VERSION_H
37 # define LIBMAD_VERSION_H
39 # define MAD_VERSION_MAJOR 0
40 # define MAD_VERSION_MINOR 15
41 # define MAD_VERSION_PATCH 0
42 # define MAD_VERSION_EXTRA " (beta)"
44 # define MAD_VERSION_STRINGIZE(str) #str
45 # define MAD_VERSION_STRING(num) MAD_VERSION_STRINGIZE(num)
47 # define MAD_VERSION MAD_VERSION_STRING(MAD_VERSION_MAJOR) "." \
48 MAD_VERSION_STRING(MAD_VERSION_MINOR) "." \
49 MAD_VERSION_STRING(MAD_VERSION_PATCH) \
50 MAD_VERSION_EXTRA
52 # define MAD_PUBLISHYEAR "2000-2003"
53 # define MAD_AUTHOR "Underbit Technologies, Inc."
54 # define MAD_EMAIL "info@underbit.com"
56 extern s8 const mad_version[];
57 extern s8 const mad_copyright[];
58 extern s8 const mad_author[];
59 extern s8 const mad_build[];
61 # endif
63 # ifndef LIBMAD_FIXED_H
64 # define LIBMAD_FIXED_H
66 # if SIZEOF_INT >= 4
67 typedef s32 mad_fixed_t;
69 typedef s32 mad_fixed64hi_t;
70 typedef u32 mad_fixed64lo_t;
71 # else
72 typedef s32 mad_fixed_t;
74 typedef s32 mad_fixed64hi_t;
75 typedef u32 mad_fixed64lo_t;
76 # endif
78 # if defined(_MSC_VER)
79 # define mad_fixed64_t signed __int64
80 # elif 1 || defined(__GNUC__)
81 # define mad_fixed64_t s64
82 # endif
84 # if defined(FPM_FLOAT)
85 typedef double mad_sample_t;
86 # else
87 typedef mad_fixed_t mad_sample_t;
88 # endif
91 * Fixed-point format: 0xABBBBBBB
92 * A == whole part (sign + 3 bits)
93 * B == fractional part (28 bits)
95 * Values are signed two's complement, so the effective range is:
96 * 0x80000000 to 0x7fffffff
97 * -8.0 to +7.9999999962747097015380859375
99 * The smallest representable value is:
100 * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
102 * 28 bits of fractional accuracy represent about
103 * 8.6 digits of decimal accuracy.
105 * Fixed-point numbers can be added or subtracted as normal
106 * integers, but multiplication requires shifting the 64-bit result
107 * from 56 fractional bits back to 28 (and rounding.)
109 * Changing the definition of MAD_F_FRACBITS is only partially
110 * supported, and must be done with care.
113 # define MAD_F_FRACBITS 28
115 # if MAD_F_FRACBITS == 28
116 # define MAD_F(x) ((mad_fixed_t) (x##L))
117 # else
118 # if MAD_F_FRACBITS < 28
119 # warning "MAD_F_FRACBITS < 28"
120 # define MAD_F(x) ((mad_fixed_t) \
121 (((x##L) + \
122 (1L << (28 - MAD_F_FRACBITS - 1))) >> \
123 (28 - MAD_F_FRACBITS)))
124 # elif MAD_F_FRACBITS > 28
125 # error "MAD_F_FRACBITS > 28 not currently supported"
126 # define MAD_F(x) ((mad_fixed_t) \
127 ((x##L) << (MAD_F_FRACBITS - 28)))
128 # endif
129 # endif
131 # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
132 # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
134 # define MAD_F_ONE MAD_F(0x10000000)
136 # define mad_f_tofixed(x) ((mad_fixed_t) \
137 ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
138 # define mad_f_todouble(x) ((double) \
139 ((x) / (double) (1L << MAD_F_FRACBITS)))
141 # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
142 # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
143 /* (x should be positive) */
145 # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
147 # define mad_f_add(x, y) ((x) + (y))
148 # define mad_f_sub(x, y) ((x) - (y))
150 # if defined(FPM_FLOAT)
151 # error "FPM_FLOAT not yet supported"
153 # undef MAD_F
154 # define MAD_F(x) mad_f_todouble(x)
156 # define mad_f_mul(x, y) ((x) * (y))
157 # define mad_f_scale64
159 # undef ASO_ZEROCHECK
161 # elif defined(FPM_64BIT)
164 * This version should be the most accurate if 64-bit types are supported by
165 * the compiler, although it may not be the most efficient.
167 # if defined(OPT_ACCURACY)
168 # define mad_f_mul(x, y) \
169 ((mad_fixed_t) \
170 ((((mad_fixed64_t) (x) * (y)) + \
171 (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
172 # else
173 # define mad_f_mul(x, y) \
174 ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
175 # endif
177 # define MAD_F_SCALEBITS MAD_F_FRACBITS
179 /* --- Intel --------------------------------------------------------------- */
181 # elif defined(FPM_INTEL)
183 # if defined(_MSC_VER)
184 # pragma warning(push)
185 # pragma warning(disable: 4035) /* no return value */
186 static __forceinline
187 mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
189 enum {
190 fracbits = MAD_F_FRACBITS
193 __asm {
194 mov eax, x
195 imul y
196 shrd eax, edx, fracbits
199 /* implicit return of eax */
201 # pragma warning(pop)
203 # define mad_f_mul mad_f_mul_inline
204 # define mad_f_scale64
205 # else
207 * This Intel version is fast and accurate; the disposition of the least
208 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
210 # define MAD_F_MLX(hi, lo, x, y) \
211 asm ("imull %3" \
212 : "=a" (lo), "=d" (hi) \
213 : "%a" (x), "rm" (y) \
214 : "cc")
216 # if defined(OPT_ACCURACY)
218 * This gives best accuracy but is not very fast.
220 # define MAD_F_MLA(hi, lo, x, y) \
221 ({ mad_fixed64hi_t __hi; \
222 mad_fixed64lo_t __lo; \
223 MAD_F_MLX(__hi, __lo, (x), (y)); \
224 asm ("addl %2,%0\n\t" \
225 "adcl %3,%1" \
226 : "=rm" (lo), "=rm" (hi) \
227 : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
228 : "cc"); \
230 # endif /* OPT_ACCURACY */
232 # if defined(OPT_ACCURACY)
234 * Surprisingly, this is faster than SHRD followed by ADC.
236 # define mad_f_scale64(hi, lo) \
237 ({ mad_fixed64hi_t __hi_; \
238 mad_fixed64lo_t __lo_; \
239 mad_fixed_t __result; \
240 asm ("addl %4,%2\n\t" \
241 "adcl %5,%3" \
242 : "=rm" (__lo_), "=rm" (__hi_) \
243 : "0" (lo), "1" (hi), \
244 "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
245 : "cc"); \
246 asm ("shrdl %3,%2,%1" \
247 : "=rm" (__result) \
248 : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
249 : "cc"); \
250 __result; \
252 # else
253 # define mad_f_scale64(hi, lo) \
254 ({ mad_fixed_t __result; \
255 asm ("shrdl %3,%2,%1" \
256 : "=rm" (__result) \
257 : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
258 : "cc"); \
259 __result; \
261 # endif /* OPT_ACCURACY */
263 # define MAD_F_SCALEBITS MAD_F_FRACBITS
264 # endif
266 /* --- ARM ----------------------------------------------------------------- */
268 # elif defined(FPM_ARM)
271 * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
272 * least significant bit is properly rounded at no CPU cycle cost!
274 # if 1
276 * This is faster than the default implementation via MAD_F_MLX() and
277 * mad_f_scale64().
279 # define mad_f_mul(x, y) \
280 ({ mad_fixed64hi_t __hi; \
281 mad_fixed64lo_t __lo; \
282 mad_fixed_t __result; \
283 asm ("smull %0, %1, %3, %4\n\t" \
284 "movs %0, %0, lsr %5\n\t" \
285 "adc %2, %0, %1, lsl %6" \
286 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
287 : "%r" (x), "r" (y), \
288 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
289 : "cc"); \
290 __result; \
292 # endif
294 # define MAD_F_MLX(hi, lo, x, y) \
295 asm ("smull %0, %1, %2, %3" \
296 : "=&r" (lo), "=&r" (hi) \
297 : "%r" (x), "r" (y))
299 # define MAD_F_MLA(hi, lo, x, y) \
300 asm ("smlal %0, %1, %2, %3" \
301 : "+r" (lo), "+r" (hi) \
302 : "%r" (x), "r" (y))
304 # define MAD_F_MLN(hi, lo) \
305 asm ("rsbs %0, %2, #0\n\t" \
306 "rsc %1, %3, #0" \
307 : "=r" (lo), "=r" (hi) \
308 : "0" (lo), "1" (hi) \
309 : "cc")
311 # define mad_f_scale64(hi, lo) \
312 ({ mad_fixed_t __result; \
313 asm ("movs %0, %1, lsr %3\n\t" \
314 "adc %0, %0, %2, lsl %4" \
315 : "=&r" (__result) \
316 : "r" (lo), "r" (hi), \
317 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
318 : "cc"); \
319 __result; \
322 # define MAD_F_SCALEBITS MAD_F_FRACBITS
324 /* --- MIPS ---------------------------------------------------------------- */
326 # elif defined(FPM_MIPS)
329 * This MIPS version is fast and accurate; the disposition of the least
330 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
332 # define MAD_F_MLX(hi, lo, x, y) \
333 asm ("mult %2,%3" \
334 : "=l" (lo), "=h" (hi) \
335 : "%r" (x), "r" (y))
337 # if defined(HAVE_MADD_ASM)
338 # define MAD_F_MLA(hi, lo, x, y) \
339 asm ("madd %2,%3" \
340 : "+l" (lo), "+h" (hi) \
341 : "%r" (x), "r" (y))
342 # elif defined(HAVE_MADD16_ASM)
344 * This loses significant accuracy due to the 16-bit integer limit in the
345 * multiply/accumulate instruction.
347 # define MAD_F_ML0(hi, lo, x, y) \
348 asm ("mult %2,%3" \
349 : "=l" (lo), "=h" (hi) \
350 : "%r" ((x) >> 12), "r" ((y) >> 16))
351 # define MAD_F_MLA(hi, lo, x, y) \
352 asm ("madd16 %2,%3" \
353 : "+l" (lo), "+h" (hi) \
354 : "%r" ((x) >> 12), "r" ((y) >> 16))
355 # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
356 # endif
358 # if defined(OPT_SPEED)
359 # define mad_f_scale64(hi, lo) \
360 ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
361 # define MAD_F_SCALEBITS MAD_F_FRACBITS
362 # endif
364 /* --- SPARC --------------------------------------------------------------- */
366 # elif defined(FPM_SPARC)
369 * This SPARC V8 version is fast and accurate; the disposition of the least
370 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
372 # define MAD_F_MLX(hi, lo, x, y) \
373 asm ("smul %2, %3, %0\n\t" \
374 "rd %%y, %1" \
375 : "=r" (lo), "=r" (hi) \
376 : "%r" (x), "rI" (y))
378 /* --- PowerPC ------------------------------------------------------------- */
380 # elif defined(FPM_PPC)
383 * This PowerPC version is fast and accurate; the disposition of the least
384 * significant bit depends on OPT_ACCURACY via mad_f_scale64().
386 # define MAD_F_MLX(hi, lo, x, y) \
387 do { \
388 asm ("mullw %0,%1,%2" \
389 : "=r" (lo) \
390 : "%r" (x), "r" (y)); \
391 asm ("mulhw %0,%1,%2" \
392 : "=r" (hi) \
393 : "%r" (x), "r" (y)); \
395 while (0)
397 # if defined(OPT_ACCURACY)
399 * This gives best accuracy but is not very fast.
401 # define MAD_F_MLA(hi, lo, x, y) \
402 ({ mad_fixed64hi_t __hi; \
403 mad_fixed64lo_t __lo; \
404 MAD_F_MLX(__hi, __lo, (x), (y)); \
405 asm ("addc %0,%2,%3\n\t" \
406 "adde %1,%4,%5" \
407 : "=r" (lo), "=r" (hi) \
408 : "%r" (lo), "r" (__lo), \
409 "%r" (hi), "r" (__hi) \
410 : "xer"); \
412 # endif
414 # if defined(OPT_ACCURACY)
416 * This is slower than the truncating version below it.
418 # define mad_f_scale64(hi, lo) \
419 ({ mad_fixed_t __result, __round; \
420 asm ("rotrwi %0,%1,%2" \
421 : "=r" (__result) \
422 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
423 asm ("extrwi %0,%1,1,0" \
424 : "=r" (__round) \
425 : "r" (__result)); \
426 asm ("insrwi %0,%1,%2,0" \
427 : "+r" (__result) \
428 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
429 asm ("add %0,%1,%2" \
430 : "=r" (__result) \
431 : "%r" (__result), "r" (__round)); \
432 __result; \
434 # else
435 # define mad_f_scale64(hi, lo) \
436 ({ mad_fixed_t __result; \
437 asm ("rotrwi %0,%1,%2" \
438 : "=r" (__result) \
439 : "r" (lo), "i" (MAD_F_SCALEBITS)); \
440 asm ("insrwi %0,%1,%2,0" \
441 : "+r" (__result) \
442 : "r" (hi), "i" (MAD_F_SCALEBITS)); \
443 __result; \
445 # endif
447 # define MAD_F_SCALEBITS MAD_F_FRACBITS
449 /* --- Default ------------------------------------------------------------- */
451 # elif defined(FPM_DEFAULT)
454 * This version is the most portable but it loses significant accuracy.
455 * Furthermore, accuracy is biased against the second argument, so care
456 * should be taken when ordering operands.
458 * The scale factors are constant as this is not used with SSO.
460 * Pre-rounding is required to stay within the limits of compliance.
462 # if defined(OPT_SPEED)
463 # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
464 # else
465 # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
466 (((y) + (1L << 15)) >> 16))
467 # endif
469 /* ------------------------------------------------------------------------- */
471 # else
472 # error "no FPM selected"
473 # endif
475 /* default implementations */
477 # if !defined(mad_f_mul)
478 # define mad_f_mul(x, y) \
479 ({ register mad_fixed64hi_t __hi; \
480 register mad_fixed64lo_t __lo; \
481 MAD_F_MLX(__hi, __lo, (x), (y)); \
482 mad_f_scale64(__hi, __lo); \
484 # endif
486 # if !defined(MAD_F_MLA)
487 # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
488 # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
489 # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
490 # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
491 # endif
493 # if !defined(MAD_F_ML0)
494 # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
495 # endif
497 # if !defined(MAD_F_MLN)
498 # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
499 # endif
501 # if !defined(MAD_F_MLZ)
502 # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
503 # endif
505 # if !defined(mad_f_scale64)
506 # if defined(OPT_ACCURACY)
507 # define mad_f_scale64(hi, lo) \
508 ((((mad_fixed_t) \
509 (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
510 ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
511 # else
512 # define mad_f_scale64(hi, lo) \
513 ((mad_fixed_t) \
514 (((hi) << (32 - MAD_F_SCALEBITS)) | \
515 ((lo) >> MAD_F_SCALEBITS)))
516 # endif
517 # define MAD_F_SCALEBITS MAD_F_FRACBITS
518 # endif
520 /* C routines */
522 mad_fixed_t mad_f_abs(mad_fixed_t);
523 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
525 # endif
527 # ifndef LIBMAD_BIT_H
528 # define LIBMAD_BIT_H
530 struct mad_bitptr {
531 u8 const *byte;
532 u16 cache;
533 u16 left;
536 void mad_bit_init(struct mad_bitptr *, u8 const *);
538 # define mad_bit_finish(bitptr) /* nothing */
540 u32 mad_bit_length(struct mad_bitptr const *,
541 struct mad_bitptr const *);
543 # define mad_bit_bitsleft(bitptr) ((bitptr)->left)
544 u8 const *mad_bit_nextbyte(struct mad_bitptr const *);
546 void mad_bit_skip(struct mad_bitptr *, u32);
547 u32 mad_bit_read(struct mad_bitptr *, u32);
548 void mad_bit_write(struct mad_bitptr *, u32, u32);
550 u16 mad_bit_crc(struct mad_bitptr, u32, u16);
552 # endif
554 # ifndef LIBMAD_TIMER_H
555 # define LIBMAD_TIMER_H
557 typedef struct {
558 signed long seconds; /* whole seconds */
559 u32 fraction; /* 1/MAD_TIMER_RESOLUTION seconds */
560 } mad_timer_t;
562 extern mad_timer_t const mad_timer_zero;
564 # define MAD_TIMER_RESOLUTION 352800000UL
566 enum mad_units {
567 MAD_UNITS_HOURS = -2,
568 MAD_UNITS_MINUTES = -1,
569 MAD_UNITS_SECONDS = 0,
571 /* metric units */
573 MAD_UNITS_DECISECONDS = 10,
574 MAD_UNITS_CENTISECONDS = 100,
575 MAD_UNITS_MILLISECONDS = 1000,
577 /* audio sample units */
579 MAD_UNITS_8000_HZ = 8000,
580 MAD_UNITS_11025_HZ = 11025,
581 MAD_UNITS_12000_HZ = 12000,
583 MAD_UNITS_16000_HZ = 16000,
584 MAD_UNITS_22050_HZ = 22050,
585 MAD_UNITS_24000_HZ = 24000,
587 MAD_UNITS_32000_HZ = 32000,
588 MAD_UNITS_44100_HZ = 44100,
589 MAD_UNITS_48000_HZ = 48000,
591 /* video frame/field units */
593 MAD_UNITS_24_FPS = 24,
594 MAD_UNITS_25_FPS = 25,
595 MAD_UNITS_30_FPS = 30,
596 MAD_UNITS_48_FPS = 48,
597 MAD_UNITS_50_FPS = 50,
598 MAD_UNITS_60_FPS = 60,
600 /* CD audio frames */
602 MAD_UNITS_75_FPS = 75,
604 /* video drop-frame units */
606 MAD_UNITS_23_976_FPS = -24,
607 MAD_UNITS_24_975_FPS = -25,
608 MAD_UNITS_29_97_FPS = -30,
609 MAD_UNITS_47_952_FPS = -48,
610 MAD_UNITS_49_95_FPS = -50,
611 MAD_UNITS_59_94_FPS = -60
614 # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero))
616 s32 mad_timer_compare(mad_timer_t, mad_timer_t);
618 # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero)
620 void mad_timer_negate(mad_timer_t *);
621 mad_timer_t mad_timer_abs(mad_timer_t);
623 void mad_timer_set(mad_timer_t *, u32, u32, u32);
624 void mad_timer_add(mad_timer_t *, mad_timer_t);
625 void mad_timer_multiply(mad_timer_t *, signed long);
627 signed long mad_timer_count(mad_timer_t, enum mad_units);
628 u32 mad_timer_fraction(mad_timer_t, u32);
629 void mad_timer_string(mad_timer_t, s8 *, s8 const *,
630 enum mad_units, enum mad_units, u32);
632 # endif
634 # ifndef LIBMAD_STREAM_H
635 # define LIBMAD_STREAM_H
638 # define MAD_BUFFER_GUARD 8
639 # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD)
641 enum mad_error {
642 MAD_ERROR_NONE = 0x0000, /* no error */
644 MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */
645 MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */
647 MAD_ERROR_NOMEM = 0x0031, /* not enough memory */
649 MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */
650 MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */
651 MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */
652 MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */
653 MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */
655 MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */
656 MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */
657 MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */
658 MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */
659 MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */
660 MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */
661 MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */
662 MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */
663 MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */
664 MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */
665 MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */
666 MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */
669 # define MAD_RECOVERABLE(error) ((error) & 0xff00)
671 struct mad_stream {
672 u8 const *buffer; /* input bitstream buffer */
673 u8 const *bufend; /* end of buffer */
674 u32 skiplen; /* bytes to skip before next frame */
676 s32 sync; /* stream sync found */
677 u32 freerate; /* free bitrate (fixed) */
679 u8 const *this_frame; /* start of current frame */
680 u8 const *next_frame; /* start of next frame */
681 struct mad_bitptr ptr; /* current processing bit pointer */
683 struct mad_bitptr anc_ptr; /* ancillary bits pointer */
684 u32 anc_bitlen; /* number of ancillary bits */
686 u8 (*main_data)[MAD_BUFFER_MDLEN];
687 /* Layer III main_data() */
688 u32 md_len; /* bytes in main_data */
690 s32 options; /* decoding options (see below) */
691 enum mad_error error; /* error code (see above) */
694 enum {
695 MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */
696 MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */
697 # if 0 /* not yet implemented */
698 MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */
699 MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */
700 MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */
701 # endif
704 void mad_stream_init(struct mad_stream *);
705 void mad_stream_finish(struct mad_stream *);
707 # define mad_stream_options(stream, opts) \
708 ((void) ((stream)->options = (opts)))
710 void mad_stream_buffer(struct mad_stream *,
711 u8 const *, u32);
712 void mad_stream_skip(struct mad_stream *, u32);
714 s32 mad_stream_sync(struct mad_stream *);
716 s8 const *mad_stream_errorstr(struct mad_stream const *);
718 # endif
720 # ifndef LIBMAD_FRAME_H
721 # define LIBMAD_FRAME_H
724 enum mad_layer {
725 MAD_LAYER_I = 1, /* Layer I */
726 MAD_LAYER_II = 2, /* Layer II */
727 MAD_LAYER_III = 3 /* Layer III */
730 enum mad_mode {
731 MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */
732 MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */
733 MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */
734 MAD_MODE_STEREO = 3 /* normal LR stereo */
737 enum mad_emphasis {
738 MAD_EMPHASIS_NONE = 0, /* no emphasis */
739 MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */
740 MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */
741 MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */
744 struct mad_header {
745 enum mad_layer layer; /* audio layer (1, 2, or 3) */
746 enum mad_mode mode; /* channel mode (see above) */
747 s32 mode_extension; /* additional mode info */
748 enum mad_emphasis emphasis; /* de-emphasis to use (see above) */
750 u32 bitrate; /* stream bitrate (bps) */
751 u32 samplerate; /* sampling frequency (Hz) */
753 u16 crc_check; /* frame CRC accumulator */
754 u16 crc_target; /* final target CRC checksum */
756 s32 flags; /* flags (see below) */
757 s32 private_bits; /* private bits (see below) */
759 mad_timer_t duration; /* audio playing time of frame */
762 struct mad_frame {
763 struct mad_header header; /* MPEG audio header */
765 s32 options; /* decoding options (from stream) */
767 mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */
768 mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */
771 # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1)
772 # define MAD_NSBSAMPLES(header) \
773 ((header)->layer == MAD_LAYER_I ? 12 : \
774 (((header)->layer == MAD_LAYER_III && \
775 ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36))
777 enum {
778 MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */
779 MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */
781 MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */
782 MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */
783 MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */
784 MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */
786 MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */
787 MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */
788 MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */
790 MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */
791 MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */
792 MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */
795 enum {
796 MAD_PRIVATE_HEADER = 0x0100, /* header private bit */
797 MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */
800 void mad_header_init(struct mad_header *);
802 # define mad_header_finish(header) /* nothing */
804 s32 mad_header_decode(struct mad_header *, struct mad_stream *);
806 void mad_frame_init(struct mad_frame *);
807 void mad_frame_finish(struct mad_frame *);
809 s32 mad_frame_decode(struct mad_frame *, struct mad_stream *);
811 void mad_frame_mute(struct mad_frame *);
813 # endif
815 # ifndef LIBMAD_SYNTH_H
816 # define LIBMAD_SYNTH_H
819 struct mad_pcm {
820 u32 samplerate; /* sampling frequency (Hz) */
821 u16 channels; /* number of channels */
822 u16 length; /* number of samples per channel */
823 mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */
826 struct mad_synth {
827 mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */
828 /* [ch][eo][peo][s][v] */
830 u32 phase; /* current processing phase */
832 struct mad_pcm pcm; /* PCM output */
835 /* single channel PCM selector */
836 enum {
837 MAD_PCM_CHANNEL_SINGLE = 0
840 /* dual channel PCM selector */
841 enum {
842 MAD_PCM_CHANNEL_DUAL_1 = 0,
843 MAD_PCM_CHANNEL_DUAL_2 = 1
846 /* stereo PCM selector */
847 enum {
848 MAD_PCM_CHANNEL_STEREO_LEFT = 0,
849 MAD_PCM_CHANNEL_STEREO_RIGHT = 1
852 void mad_synth_init(struct mad_synth *);
854 # define mad_synth_finish(synth) /* nothing */
856 void mad_synth_mute(struct mad_synth *);
858 void mad_synth_frame(struct mad_synth *, struct mad_frame const *);
860 # endif
862 # ifndef LIBMAD_DECODER_H
863 # define LIBMAD_DECODER_H
866 enum mad_decoder_mode {
867 MAD_DECODER_MODE_SYNC = 0,
868 MAD_DECODER_MODE_ASYNC
871 enum mad_flow {
872 MAD_FLOW_CONTINUE = 0x0000, /* continue normally */
873 MAD_FLOW_STOP = 0x0010, /* stop decoding normally */
874 MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */
875 MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */
878 struct mad_decoder {
879 enum mad_decoder_mode mode;
881 s32 options;
883 struct {
884 long pid;
885 s32 in;
886 s32 out;
887 } async;
889 struct {
890 struct mad_stream stream;
891 struct mad_frame frame;
892 struct mad_synth synth;
893 } *sync;
895 void *cb_data;
897 enum mad_flow (*input_func)(void *, struct mad_stream *);
898 enum mad_flow (*header_func)(void *, struct mad_header const *);
899 enum mad_flow (*filter_func)(void *,
900 struct mad_stream const *, struct mad_frame *);
901 enum mad_flow (*output_func)(void *,
902 struct mad_header const *, struct mad_pcm *);
903 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
904 enum mad_flow (*message_func)(void *, void *, u32 *);
907 void mad_decoder_init(struct mad_decoder *, void *,
908 enum mad_flow (*)(void *, struct mad_stream *),
909 enum mad_flow (*)(void *, struct mad_header const *),
910 enum mad_flow (*)(void *,
911 struct mad_stream const *,
912 struct mad_frame *),
913 enum mad_flow (*)(void *,
914 struct mad_header const *,
915 struct mad_pcm *),
916 enum mad_flow (*)(void *,
917 struct mad_stream *,
918 struct mad_frame *),
919 enum mad_flow (*)(void *, void *, u32 *));
920 s32 mad_decoder_finish(struct mad_decoder *);
922 # define mad_decoder_options(decoder, opts) \
923 ((void) ((decoder)->options = (opts)))
925 s32 mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode);
926 s32 mad_decoder_message(struct mad_decoder *, void *, u32 *);
928 # endif
930 # ifdef __cplusplus
932 # endif