Chris@5: /* Chris@5: * libmad - MPEG audio decoder library Chris@5: * Copyright (C) 2000-2004 Underbit Technologies, Inc. Chris@5: * Chris@5: * This program is free software; you can redistribute it and/or modify Chris@5: * it under the terms of the GNU General Public License as published by Chris@5: * the Free Software Foundation; either version 2 of the License, or Chris@5: * (at your option) any later version. Chris@5: * Chris@5: * This program is distributed in the hope that it will be useful, Chris@5: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@5: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@5: * GNU General Public License for more details. Chris@5: * Chris@5: * You should have received a copy of the GNU General Public License Chris@5: * along with this program; if not, write to the Free Software Chris@5: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@5: * Chris@5: * If you would like to negotiate alternate licensing terms, you may do Chris@5: * so by contacting: Underbit Technologies, Inc. Chris@5: */ Chris@5: Chris@5: # ifdef __cplusplus Chris@5: extern "C" { Chris@5: # endif Chris@5: Chris@5: # define FPM_INTEL Chris@5: Chris@5: Chris@5: Chris@5: # define SIZEOF_INT 4 Chris@5: # define SIZEOF_LONG 4 Chris@5: # define SIZEOF_LONG_LONG 8 Chris@5: Chris@5: Chris@5: /* Id: version.h,v 1.26 2004/01/23 09:41:33 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_VERSION_H Chris@5: # define LIBMAD_VERSION_H Chris@5: Chris@5: # define MAD_VERSION_MAJOR 0 Chris@5: # define MAD_VERSION_MINOR 15 Chris@5: # define MAD_VERSION_PATCH 1 Chris@5: # define MAD_VERSION_EXTRA " (beta)" Chris@5: Chris@5: # define MAD_VERSION_STRINGIZE(str) #str Chris@5: # define MAD_VERSION_STRING(num) MAD_VERSION_STRINGIZE(num) Chris@5: Chris@5: # define MAD_VERSION MAD_VERSION_STRING(MAD_VERSION_MAJOR) "." \ Chris@5: MAD_VERSION_STRING(MAD_VERSION_MINOR) "." \ Chris@5: MAD_VERSION_STRING(MAD_VERSION_PATCH) \ Chris@5: MAD_VERSION_EXTRA Chris@5: Chris@5: # define MAD_PUBLISHYEAR "2000-2004" Chris@5: # define MAD_AUTHOR "Underbit Technologies, Inc." Chris@5: # define MAD_EMAIL "info@underbit.com" Chris@5: Chris@5: extern char const mad_version[]; Chris@5: extern char const mad_copyright[]; Chris@5: extern char const mad_author[]; Chris@5: extern char const mad_build[]; Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_FIXED_H Chris@5: # define LIBMAD_FIXED_H Chris@5: Chris@5: # if SIZEOF_INT >= 4 Chris@5: typedef signed int mad_fixed_t; Chris@5: Chris@5: typedef signed int mad_fixed64hi_t; Chris@5: typedef unsigned int mad_fixed64lo_t; Chris@5: # else Chris@5: typedef signed long mad_fixed_t; Chris@5: Chris@5: typedef signed long mad_fixed64hi_t; Chris@5: typedef unsigned long mad_fixed64lo_t; Chris@5: # endif Chris@5: Chris@5: # if defined(_MSC_VER) Chris@5: # define mad_fixed64_t signed __int64 Chris@5: # elif 1 || defined(__GNUC__) Chris@5: # define mad_fixed64_t signed long long Chris@5: # endif Chris@5: Chris@5: # if defined(FPM_FLOAT) Chris@5: typedef double mad_sample_t; Chris@5: # else Chris@5: typedef mad_fixed_t mad_sample_t; Chris@5: # endif Chris@5: Chris@5: /* Chris@5: * Fixed-point format: 0xABBBBBBB Chris@5: * A == whole part (sign + 3 bits) Chris@5: * B == fractional part (28 bits) Chris@5: * Chris@5: * Values are signed two's complement, so the effective range is: Chris@5: * 0x80000000 to 0x7fffffff Chris@5: * -8.0 to +7.9999999962747097015380859375 Chris@5: * Chris@5: * The smallest representable value is: Chris@5: * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9) Chris@5: * Chris@5: * 28 bits of fractional accuracy represent about Chris@5: * 8.6 digits of decimal accuracy. Chris@5: * Chris@5: * Fixed-point numbers can be added or subtracted as normal Chris@5: * integers, but multiplication requires shifting the 64-bit result Chris@5: * from 56 fractional bits back to 28 (and rounding.) Chris@5: * Chris@5: * Changing the definition of MAD_F_FRACBITS is only partially Chris@5: * supported, and must be done with care. Chris@5: */ Chris@5: Chris@5: # define MAD_F_FRACBITS 28 Chris@5: Chris@5: # if MAD_F_FRACBITS == 28 Chris@5: # define MAD_F(x) ((mad_fixed_t) (x##L)) Chris@5: # else Chris@5: # if MAD_F_FRACBITS < 28 Chris@5: # warning "MAD_F_FRACBITS < 28" Chris@5: # define MAD_F(x) ((mad_fixed_t) \ Chris@5: (((x##L) + \ Chris@5: (1L << (28 - MAD_F_FRACBITS - 1))) >> \ Chris@5: (28 - MAD_F_FRACBITS))) Chris@5: # elif MAD_F_FRACBITS > 28 Chris@5: # error "MAD_F_FRACBITS > 28 not currently supported" Chris@5: # define MAD_F(x) ((mad_fixed_t) \ Chris@5: ((x##L) << (MAD_F_FRACBITS - 28))) Chris@5: # endif Chris@5: # endif Chris@5: Chris@5: # define MAD_F_MIN ((mad_fixed_t) -0x80000000L) Chris@5: # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL) Chris@5: Chris@5: # define MAD_F_ONE MAD_F(0x10000000) Chris@5: Chris@5: # define mad_f_tofixed(x) ((mad_fixed_t) \ Chris@5: ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5)) Chris@5: # define mad_f_todouble(x) ((double) \ Chris@5: ((x) / (double) (1L << MAD_F_FRACBITS))) Chris@5: Chris@5: # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS) Chris@5: # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1)) Chris@5: /* (x should be positive) */ Chris@5: Chris@5: # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS) Chris@5: Chris@5: # define mad_f_add(x, y) ((x) + (y)) Chris@5: # define mad_f_sub(x, y) ((x) - (y)) Chris@5: Chris@5: # if defined(FPM_FLOAT) Chris@5: # error "FPM_FLOAT not yet supported" Chris@5: Chris@5: # undef MAD_F Chris@5: # define MAD_F(x) mad_f_todouble(x) Chris@5: Chris@5: # define mad_f_mul(x, y) ((x) * (y)) Chris@5: # define mad_f_scale64 Chris@5: Chris@5: # undef ASO_ZEROCHECK Chris@5: Chris@5: # elif defined(FPM_64BIT) Chris@5: Chris@5: /* Chris@5: * This version should be the most accurate if 64-bit types are supported by Chris@5: * the compiler, although it may not be the most efficient. Chris@5: */ Chris@5: # if defined(OPT_ACCURACY) Chris@5: # define mad_f_mul(x, y) \ Chris@5: ((mad_fixed_t) \ Chris@5: ((((mad_fixed64_t) (x) * (y)) + \ Chris@5: (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS)) Chris@5: # else Chris@5: # define mad_f_mul(x, y) \ Chris@5: ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS)) Chris@5: # endif Chris@5: Chris@5: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@5: Chris@5: /* --- Intel --------------------------------------------------------------- */ Chris@5: Chris@5: # elif defined(FPM_INTEL) Chris@5: Chris@5: # if defined(_MSC_VER) Chris@5: # pragma warning(push) Chris@5: # pragma warning(disable: 4035) /* no return value */ Chris@5: static __forceinline Chris@5: mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y) Chris@5: { Chris@5: enum { Chris@5: fracbits = MAD_F_FRACBITS Chris@5: }; Chris@5: Chris@5: __asm { Chris@5: mov eax, x Chris@5: imul y Chris@5: shrd eax, edx, fracbits Chris@5: } Chris@5: Chris@5: /* implicit return of eax */ Chris@5: } Chris@5: # pragma warning(pop) Chris@5: Chris@5: # define mad_f_mul mad_f_mul_inline Chris@5: # define mad_f_scale64 Chris@5: # else Chris@5: /* Chris@5: * This Intel version is fast and accurate; the disposition of the least Chris@5: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@5: */ Chris@5: # define MAD_F_MLX(hi, lo, x, y) \ Chris@5: asm ("imull %3" \ Chris@5: : "=a" (lo), "=d" (hi) \ Chris@5: : "%a" (x), "rm" (y) \ Chris@5: : "cc") Chris@5: Chris@5: # if defined(OPT_ACCURACY) Chris@5: /* Chris@5: * This gives best accuracy but is not very fast. Chris@5: */ Chris@5: # define MAD_F_MLA(hi, lo, x, y) \ Chris@5: ({ mad_fixed64hi_t __hi; \ Chris@5: mad_fixed64lo_t __lo; \ Chris@5: MAD_F_MLX(__hi, __lo, (x), (y)); \ Chris@5: asm ("addl %2,%0\n\t" \ Chris@5: "adcl %3,%1" \ Chris@5: : "=rm" (lo), "=rm" (hi) \ Chris@5: : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \ Chris@5: : "cc"); \ Chris@5: }) Chris@5: # endif /* OPT_ACCURACY */ Chris@5: Chris@5: # if defined(OPT_ACCURACY) Chris@5: /* Chris@5: * Surprisingly, this is faster than SHRD followed by ADC. Chris@5: */ Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ({ mad_fixed64hi_t __hi_; \ Chris@5: mad_fixed64lo_t __lo_; \ Chris@5: mad_fixed_t __result; \ Chris@5: asm ("addl %4,%2\n\t" \ Chris@5: "adcl %5,%3" \ Chris@5: : "=rm" (__lo_), "=rm" (__hi_) \ Chris@5: : "0" (lo), "1" (hi), \ Chris@5: "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \ Chris@5: : "cc"); \ Chris@5: asm ("shrdl %3,%2,%1" \ Chris@5: : "=rm" (__result) \ Chris@5: : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \ Chris@5: : "cc"); \ Chris@5: __result; \ Chris@5: }) Chris@5: # elif defined(OPT_INTEL) Chris@5: /* Chris@5: * Alternate Intel scaling that may or may not perform better. Chris@5: */ Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ({ mad_fixed_t __result; \ Chris@5: asm ("shrl %3,%1\n\t" \ Chris@5: "shll %4,%2\n\t" \ Chris@5: "orl %2,%1" \ Chris@5: : "=rm" (__result) \ Chris@5: : "0" (lo), "r" (hi), \ Chris@5: "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \ Chris@5: : "cc"); \ Chris@5: __result; \ Chris@5: }) Chris@5: # else Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ({ mad_fixed_t __result; \ Chris@5: asm ("shrdl %3,%2,%1" \ Chris@5: : "=rm" (__result) \ Chris@5: : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \ Chris@5: : "cc"); \ Chris@5: __result; \ Chris@5: }) Chris@5: # endif /* OPT_ACCURACY */ Chris@5: Chris@5: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@5: # endif Chris@5: Chris@5: /* --- ARM ----------------------------------------------------------------- */ Chris@5: Chris@5: # elif defined(FPM_ARM) Chris@5: Chris@5: /* Chris@5: * This ARM V4 version is as accurate as FPM_64BIT but much faster. The Chris@5: * least significant bit is properly rounded at no CPU cycle cost! Chris@5: */ Chris@5: # if 1 Chris@5: /* Chris@5: * This is faster than the default implementation via MAD_F_MLX() and Chris@5: * mad_f_scale64(). Chris@5: */ Chris@5: # define mad_f_mul(x, y) \ Chris@5: ({ mad_fixed64hi_t __hi; \ Chris@5: mad_fixed64lo_t __lo; \ Chris@5: mad_fixed_t __result; \ Chris@5: asm ("smull %0, %1, %3, %4\n\t" \ Chris@5: "movs %0, %0, lsr %5\n\t" \ Chris@5: "adc %2, %0, %1, lsl %6" \ Chris@5: : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \ Chris@5: : "%r" (x), "r" (y), \ Chris@5: "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ Chris@5: : "cc"); \ Chris@5: __result; \ Chris@5: }) Chris@5: # endif Chris@5: Chris@5: # define MAD_F_MLX(hi, lo, x, y) \ Chris@5: asm ("smull %0, %1, %2, %3" \ Chris@5: : "=&r" (lo), "=&r" (hi) \ Chris@5: : "%r" (x), "r" (y)) Chris@5: Chris@5: # define MAD_F_MLA(hi, lo, x, y) \ Chris@5: asm ("smlal %0, %1, %2, %3" \ Chris@5: : "+r" (lo), "+r" (hi) \ Chris@5: : "%r" (x), "r" (y)) Chris@5: Chris@5: # define MAD_F_MLN(hi, lo) \ Chris@5: asm ("rsbs %0, %2, #0\n\t" \ Chris@5: "rsc %1, %3, #0" \ Chris@5: : "=r" (lo), "=r" (hi) \ Chris@5: : "0" (lo), "1" (hi) \ Chris@5: : "cc") Chris@5: Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ({ mad_fixed_t __result; \ Chris@5: asm ("movs %0, %1, lsr %3\n\t" \ Chris@5: "adc %0, %0, %2, lsl %4" \ Chris@5: : "=&r" (__result) \ Chris@5: : "r" (lo), "r" (hi), \ Chris@5: "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ Chris@5: : "cc"); \ Chris@5: __result; \ Chris@5: }) Chris@5: Chris@5: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@5: Chris@5: /* --- MIPS ---------------------------------------------------------------- */ Chris@5: Chris@5: # elif defined(FPM_MIPS) Chris@5: Chris@5: /* Chris@5: * This MIPS version is fast and accurate; the disposition of the least Chris@5: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@5: */ Chris@5: # define MAD_F_MLX(hi, lo, x, y) \ Chris@5: asm ("mult %2,%3" \ Chris@5: : "=l" (lo), "=h" (hi) \ Chris@5: : "%r" (x), "r" (y)) Chris@5: Chris@5: # if defined(HAVE_MADD_ASM) Chris@5: # define MAD_F_MLA(hi, lo, x, y) \ Chris@5: asm ("madd %2,%3" \ Chris@5: : "+l" (lo), "+h" (hi) \ Chris@5: : "%r" (x), "r" (y)) Chris@5: # elif defined(HAVE_MADD16_ASM) Chris@5: /* Chris@5: * This loses significant accuracy due to the 16-bit integer limit in the Chris@5: * multiply/accumulate instruction. Chris@5: */ Chris@5: # define MAD_F_ML0(hi, lo, x, y) \ Chris@5: asm ("mult %2,%3" \ Chris@5: : "=l" (lo), "=h" (hi) \ Chris@5: : "%r" ((x) >> 12), "r" ((y) >> 16)) Chris@5: # define MAD_F_MLA(hi, lo, x, y) \ Chris@5: asm ("madd16 %2,%3" \ Chris@5: : "+l" (lo), "+h" (hi) \ Chris@5: : "%r" ((x) >> 12), "r" ((y) >> 16)) Chris@5: # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo)) Chris@5: # endif Chris@5: Chris@5: # if defined(OPT_SPEED) Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS))) Chris@5: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@5: # endif Chris@5: Chris@5: /* --- SPARC --------------------------------------------------------------- */ Chris@5: Chris@5: # elif defined(FPM_SPARC) Chris@5: Chris@5: /* Chris@5: * This SPARC V8 version is fast and accurate; the disposition of the least Chris@5: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@5: */ Chris@5: # define MAD_F_MLX(hi, lo, x, y) \ Chris@5: asm ("smul %2, %3, %0\n\t" \ Chris@5: "rd %%y, %1" \ Chris@5: : "=r" (lo), "=r" (hi) \ Chris@5: : "%r" (x), "rI" (y)) Chris@5: Chris@5: /* --- PowerPC ------------------------------------------------------------- */ Chris@5: Chris@5: # elif defined(FPM_PPC) Chris@5: Chris@5: /* Chris@5: * This PowerPC version is fast and accurate; the disposition of the least Chris@5: * significant bit depends on OPT_ACCURACY via mad_f_scale64(). Chris@5: */ Chris@5: # define MAD_F_MLX(hi, lo, x, y) \ Chris@5: do { \ Chris@5: asm ("mullw %0,%1,%2" \ Chris@5: : "=r" (lo) \ Chris@5: : "%r" (x), "r" (y)); \ Chris@5: asm ("mulhw %0,%1,%2" \ Chris@5: : "=r" (hi) \ Chris@5: : "%r" (x), "r" (y)); \ Chris@5: } \ Chris@5: while (0) Chris@5: Chris@5: # if defined(OPT_ACCURACY) Chris@5: /* Chris@5: * This gives best accuracy but is not very fast. Chris@5: */ Chris@5: # define MAD_F_MLA(hi, lo, x, y) \ Chris@5: ({ mad_fixed64hi_t __hi; \ Chris@5: mad_fixed64lo_t __lo; \ Chris@5: MAD_F_MLX(__hi, __lo, (x), (y)); \ Chris@5: asm ("addc %0,%2,%3\n\t" \ Chris@5: "adde %1,%4,%5" \ Chris@5: : "=r" (lo), "=r" (hi) \ Chris@5: : "%r" (lo), "r" (__lo), \ Chris@5: "%r" (hi), "r" (__hi) \ Chris@5: : "xer"); \ Chris@5: }) Chris@5: # endif Chris@5: Chris@5: # if defined(OPT_ACCURACY) Chris@5: /* Chris@5: * This is slower than the truncating version below it. Chris@5: */ Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ({ mad_fixed_t __result, __round; \ Chris@5: asm ("rotrwi %0,%1,%2" \ Chris@5: : "=r" (__result) \ Chris@5: : "r" (lo), "i" (MAD_F_SCALEBITS)); \ Chris@5: asm ("extrwi %0,%1,1,0" \ Chris@5: : "=r" (__round) \ Chris@5: : "r" (__result)); \ Chris@5: asm ("insrwi %0,%1,%2,0" \ Chris@5: : "+r" (__result) \ Chris@5: : "r" (hi), "i" (MAD_F_SCALEBITS)); \ Chris@5: asm ("add %0,%1,%2" \ Chris@5: : "=r" (__result) \ Chris@5: : "%r" (__result), "r" (__round)); \ Chris@5: __result; \ Chris@5: }) Chris@5: # else Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ({ mad_fixed_t __result; \ Chris@5: asm ("rotrwi %0,%1,%2" \ Chris@5: : "=r" (__result) \ Chris@5: : "r" (lo), "i" (MAD_F_SCALEBITS)); \ Chris@5: asm ("insrwi %0,%1,%2,0" \ Chris@5: : "+r" (__result) \ Chris@5: : "r" (hi), "i" (MAD_F_SCALEBITS)); \ Chris@5: __result; \ Chris@5: }) Chris@5: # endif Chris@5: Chris@5: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@5: Chris@5: /* --- Default ------------------------------------------------------------- */ Chris@5: Chris@5: # elif defined(FPM_DEFAULT) Chris@5: Chris@5: /* Chris@5: * This version is the most portable but it loses significant accuracy. Chris@5: * Furthermore, accuracy is biased against the second argument, so care Chris@5: * should be taken when ordering operands. Chris@5: * Chris@5: * The scale factors are constant as this is not used with SSO. Chris@5: * Chris@5: * Pre-rounding is required to stay within the limits of compliance. Chris@5: */ Chris@5: # if defined(OPT_SPEED) Chris@5: # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16)) Chris@5: # else Chris@5: # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \ Chris@5: (((y) + (1L << 15)) >> 16)) Chris@5: # endif Chris@5: Chris@5: /* ------------------------------------------------------------------------- */ Chris@5: Chris@5: # else Chris@5: # error "no FPM selected" Chris@5: # endif Chris@5: Chris@5: /* default implementations */ Chris@5: Chris@5: # if !defined(mad_f_mul) Chris@5: # define mad_f_mul(x, y) \ Chris@5: ({ register mad_fixed64hi_t __hi; \ Chris@5: register mad_fixed64lo_t __lo; \ Chris@5: MAD_F_MLX(__hi, __lo, (x), (y)); \ Chris@5: mad_f_scale64(__hi, __lo); \ Chris@5: }) Chris@5: # endif Chris@5: Chris@5: # if !defined(MAD_F_MLA) Chris@5: # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y))) Chris@5: # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y))) Chris@5: # define MAD_F_MLN(hi, lo) ((lo) = -(lo)) Chris@5: # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo)) Chris@5: # endif Chris@5: Chris@5: # if !defined(MAD_F_ML0) Chris@5: # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y)) Chris@5: # endif Chris@5: Chris@5: # if !defined(MAD_F_MLN) Chris@5: # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi)) Chris@5: # endif Chris@5: Chris@5: # if !defined(MAD_F_MLZ) Chris@5: # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo)) Chris@5: # endif Chris@5: Chris@5: # if !defined(mad_f_scale64) Chris@5: # if defined(OPT_ACCURACY) Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ((((mad_fixed_t) \ Chris@5: (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \ Chris@5: ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1) Chris@5: # else Chris@5: # define mad_f_scale64(hi, lo) \ Chris@5: ((mad_fixed_t) \ Chris@5: (((hi) << (32 - MAD_F_SCALEBITS)) | \ Chris@5: ((lo) >> MAD_F_SCALEBITS))) Chris@5: # endif Chris@5: # define MAD_F_SCALEBITS MAD_F_FRACBITS Chris@5: # endif Chris@5: Chris@5: /* C routines */ Chris@5: Chris@5: mad_fixed_t mad_f_abs(mad_fixed_t); Chris@5: mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t); Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: bit.h,v 1.12 2004/01/23 09:41:32 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_BIT_H Chris@5: # define LIBMAD_BIT_H Chris@5: Chris@5: struct mad_bitptr { Chris@5: unsigned char const *byte; Chris@5: unsigned short cache; Chris@5: unsigned short left; Chris@5: }; Chris@5: Chris@5: void mad_bit_init(struct mad_bitptr *, unsigned char const *); Chris@5: Chris@5: # define mad_bit_finish(bitptr) /* nothing */ Chris@5: Chris@5: unsigned int mad_bit_length(struct mad_bitptr const *, Chris@5: struct mad_bitptr const *); Chris@5: Chris@5: # define mad_bit_bitsleft(bitptr) ((bitptr)->left) Chris@5: unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *); Chris@5: Chris@5: void mad_bit_skip(struct mad_bitptr *, unsigned int); Chris@5: unsigned long mad_bit_read(struct mad_bitptr *, unsigned int); Chris@5: void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long); Chris@5: Chris@5: unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short); Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: timer.h,v 1.16 2004/01/23 09:41:33 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_TIMER_H Chris@5: # define LIBMAD_TIMER_H Chris@5: Chris@5: typedef struct { Chris@5: signed long seconds; /* whole seconds */ Chris@5: unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */ Chris@5: } mad_timer_t; Chris@5: Chris@5: extern mad_timer_t const mad_timer_zero; Chris@5: Chris@5: # define MAD_TIMER_RESOLUTION 352800000UL Chris@5: Chris@5: enum mad_units { Chris@5: MAD_UNITS_HOURS = -2, Chris@5: MAD_UNITS_MINUTES = -1, Chris@5: MAD_UNITS_SECONDS = 0, Chris@5: Chris@5: /* metric units */ Chris@5: Chris@5: MAD_UNITS_DECISECONDS = 10, Chris@5: MAD_UNITS_CENTISECONDS = 100, Chris@5: MAD_UNITS_MILLISECONDS = 1000, Chris@5: Chris@5: /* audio sample units */ Chris@5: Chris@5: MAD_UNITS_8000_HZ = 8000, Chris@5: MAD_UNITS_11025_HZ = 11025, Chris@5: MAD_UNITS_12000_HZ = 12000, Chris@5: Chris@5: MAD_UNITS_16000_HZ = 16000, Chris@5: MAD_UNITS_22050_HZ = 22050, Chris@5: MAD_UNITS_24000_HZ = 24000, Chris@5: Chris@5: MAD_UNITS_32000_HZ = 32000, Chris@5: MAD_UNITS_44100_HZ = 44100, Chris@5: MAD_UNITS_48000_HZ = 48000, Chris@5: Chris@5: /* video frame/field units */ Chris@5: Chris@5: MAD_UNITS_24_FPS = 24, Chris@5: MAD_UNITS_25_FPS = 25, Chris@5: MAD_UNITS_30_FPS = 30, Chris@5: MAD_UNITS_48_FPS = 48, Chris@5: MAD_UNITS_50_FPS = 50, Chris@5: MAD_UNITS_60_FPS = 60, Chris@5: Chris@5: /* CD audio frames */ Chris@5: Chris@5: MAD_UNITS_75_FPS = 75, Chris@5: Chris@5: /* video drop-frame units */ Chris@5: Chris@5: MAD_UNITS_23_976_FPS = -24, Chris@5: MAD_UNITS_24_975_FPS = -25, Chris@5: MAD_UNITS_29_97_FPS = -30, Chris@5: MAD_UNITS_47_952_FPS = -48, Chris@5: MAD_UNITS_49_95_FPS = -50, Chris@5: MAD_UNITS_59_94_FPS = -60 Chris@5: }; Chris@5: Chris@5: # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero)) Chris@5: Chris@5: int mad_timer_compare(mad_timer_t, mad_timer_t); Chris@5: Chris@5: # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero) Chris@5: Chris@5: void mad_timer_negate(mad_timer_t *); Chris@5: mad_timer_t mad_timer_abs(mad_timer_t); Chris@5: Chris@5: void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long); Chris@5: void mad_timer_add(mad_timer_t *, mad_timer_t); Chris@5: void mad_timer_multiply(mad_timer_t *, signed long); Chris@5: Chris@5: signed long mad_timer_count(mad_timer_t, enum mad_units); Chris@5: unsigned long mad_timer_fraction(mad_timer_t, unsigned long); Chris@5: void mad_timer_string(mad_timer_t, char *, char const *, Chris@5: enum mad_units, enum mad_units, unsigned long); Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: stream.h,v 1.20 2004/02/05 09:02:39 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_STREAM_H Chris@5: # define LIBMAD_STREAM_H Chris@5: Chris@5: Chris@5: # define MAD_BUFFER_GUARD 8 Chris@5: # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD) Chris@5: Chris@5: enum mad_error { Chris@5: MAD_ERROR_NONE = 0x0000, /* no error */ Chris@5: Chris@5: MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */ Chris@5: MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */ Chris@5: Chris@5: MAD_ERROR_NOMEM = 0x0031, /* not enough memory */ Chris@5: Chris@5: MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */ Chris@5: MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */ Chris@5: MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */ Chris@5: MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */ Chris@5: MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */ Chris@5: Chris@5: MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */ Chris@5: MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */ Chris@5: MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */ Chris@5: MAD_ERROR_BADMODE = 0x0222, /* bad bitrate/mode combination */ Chris@5: MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */ Chris@5: MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */ Chris@5: MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */ Chris@5: MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */ Chris@5: MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */ Chris@5: MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */ Chris@5: MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */ Chris@5: MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */ Chris@5: MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */ Chris@5: }; Chris@5: Chris@5: # define MAD_RECOVERABLE(error) ((error) & 0xff00) Chris@5: Chris@5: struct mad_stream { Chris@5: unsigned char const *buffer; /* input bitstream buffer */ Chris@5: unsigned char const *bufend; /* end of buffer */ Chris@5: unsigned long skiplen; /* bytes to skip before next frame */ Chris@5: Chris@5: int sync; /* stream sync found */ Chris@5: unsigned long freerate; /* free bitrate (fixed) */ Chris@5: Chris@5: unsigned char const *this_frame; /* start of current frame */ Chris@5: unsigned char const *next_frame; /* start of next frame */ Chris@5: struct mad_bitptr ptr; /* current processing bit pointer */ Chris@5: Chris@5: struct mad_bitptr anc_ptr; /* ancillary bits pointer */ Chris@5: unsigned int anc_bitlen; /* number of ancillary bits */ Chris@5: Chris@5: unsigned char (*main_data)[MAD_BUFFER_MDLEN]; Chris@5: /* Layer III main_data() */ Chris@5: unsigned int md_len; /* bytes in main_data */ Chris@5: Chris@5: int options; /* decoding options (see below) */ Chris@5: enum mad_error error; /* error code (see above) */ Chris@5: }; Chris@5: Chris@5: enum { Chris@5: MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */ Chris@5: MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */ Chris@5: # if 0 /* not yet implemented */ Chris@5: MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */ Chris@5: MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */ Chris@5: MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */ Chris@5: # endif Chris@5: }; Chris@5: Chris@5: void mad_stream_init(struct mad_stream *); Chris@5: void mad_stream_finish(struct mad_stream *); Chris@5: Chris@5: # define mad_stream_options(stream, opts) \ Chris@5: ((void) ((stream)->options = (opts))) Chris@5: Chris@5: void mad_stream_buffer(struct mad_stream *, Chris@5: unsigned char const *, unsigned long); Chris@5: void mad_stream_skip(struct mad_stream *, unsigned long); Chris@5: Chris@5: int mad_stream_sync(struct mad_stream *); Chris@5: Chris@5: char const *mad_stream_errorstr(struct mad_stream const *); Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: frame.h,v 1.20 2004/01/23 09:41:32 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_FRAME_H Chris@5: # define LIBMAD_FRAME_H Chris@5: Chris@5: Chris@5: enum mad_layer { Chris@5: MAD_LAYER_I = 1, /* Layer I */ Chris@5: MAD_LAYER_II = 2, /* Layer II */ Chris@5: MAD_LAYER_III = 3 /* Layer III */ Chris@5: }; Chris@5: Chris@5: enum mad_mode { Chris@5: MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */ Chris@5: MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */ Chris@5: MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */ Chris@5: MAD_MODE_STEREO = 3 /* normal LR stereo */ Chris@5: }; Chris@5: Chris@5: enum mad_emphasis { Chris@5: MAD_EMPHASIS_NONE = 0, /* no emphasis */ Chris@5: MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */ Chris@5: MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */ Chris@5: MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */ Chris@5: }; Chris@5: Chris@5: struct mad_header { Chris@5: enum mad_layer layer; /* audio layer (1, 2, or 3) */ Chris@5: enum mad_mode mode; /* channel mode (see above) */ Chris@5: int mode_extension; /* additional mode info */ Chris@5: enum mad_emphasis emphasis; /* de-emphasis to use (see above) */ Chris@5: Chris@5: unsigned long bitrate; /* stream bitrate (bps) */ Chris@5: unsigned int samplerate; /* sampling frequency (Hz) */ Chris@5: Chris@5: unsigned short crc_check; /* frame CRC accumulator */ Chris@5: unsigned short crc_target; /* final target CRC checksum */ Chris@5: Chris@5: int flags; /* flags (see below) */ Chris@5: int private_bits; /* private bits (see below) */ Chris@5: Chris@5: mad_timer_t duration; /* audio playing time of frame */ Chris@5: }; Chris@5: Chris@5: struct mad_frame { Chris@5: struct mad_header header; /* MPEG audio header */ Chris@5: Chris@5: int options; /* decoding options (from stream) */ Chris@5: Chris@5: mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */ Chris@5: mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */ Chris@5: }; Chris@5: Chris@5: # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1) Chris@5: # define MAD_NSBSAMPLES(header) \ Chris@5: ((header)->layer == MAD_LAYER_I ? 12 : \ Chris@5: (((header)->layer == MAD_LAYER_III && \ Chris@5: ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36)) Chris@5: Chris@5: enum { Chris@5: MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */ Chris@5: MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */ Chris@5: Chris@5: MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */ Chris@5: MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */ Chris@5: MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */ Chris@5: MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */ Chris@5: Chris@5: MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */ Chris@5: MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */ Chris@5: MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */ Chris@5: Chris@5: MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */ Chris@5: MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */ Chris@5: MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */ Chris@5: }; Chris@5: Chris@5: enum { Chris@5: MAD_PRIVATE_HEADER = 0x0100, /* header private bit */ Chris@5: MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */ Chris@5: }; Chris@5: Chris@5: void mad_header_init(struct mad_header *); Chris@5: Chris@5: # define mad_header_finish(header) /* nothing */ Chris@5: Chris@5: int mad_header_decode(struct mad_header *, struct mad_stream *); Chris@5: Chris@5: void mad_frame_init(struct mad_frame *); Chris@5: void mad_frame_finish(struct mad_frame *); Chris@5: Chris@5: int mad_frame_decode(struct mad_frame *, struct mad_stream *); Chris@5: Chris@5: void mad_frame_mute(struct mad_frame *); Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: synth.h,v 1.15 2004/01/23 09:41:33 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_SYNTH_H Chris@5: # define LIBMAD_SYNTH_H Chris@5: Chris@5: Chris@5: struct mad_pcm { Chris@5: unsigned int samplerate; /* sampling frequency (Hz) */ Chris@5: unsigned short channels; /* number of channels */ Chris@5: unsigned short length; /* number of samples per channel */ Chris@5: mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */ Chris@5: }; Chris@5: Chris@5: struct mad_synth { Chris@5: mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */ Chris@5: /* [ch][eo][peo][s][v] */ Chris@5: Chris@5: unsigned int phase; /* current processing phase */ Chris@5: Chris@5: struct mad_pcm pcm; /* PCM output */ Chris@5: }; Chris@5: Chris@5: /* single channel PCM selector */ Chris@5: enum { Chris@5: MAD_PCM_CHANNEL_SINGLE = 0 Chris@5: }; Chris@5: Chris@5: /* dual channel PCM selector */ Chris@5: enum { Chris@5: MAD_PCM_CHANNEL_DUAL_1 = 0, Chris@5: MAD_PCM_CHANNEL_DUAL_2 = 1 Chris@5: }; Chris@5: Chris@5: /* stereo PCM selector */ Chris@5: enum { Chris@5: MAD_PCM_CHANNEL_STEREO_LEFT = 0, Chris@5: MAD_PCM_CHANNEL_STEREO_RIGHT = 1 Chris@5: }; Chris@5: Chris@5: void mad_synth_init(struct mad_synth *); Chris@5: Chris@5: # define mad_synth_finish(synth) /* nothing */ Chris@5: Chris@5: void mad_synth_mute(struct mad_synth *); Chris@5: Chris@5: void mad_synth_frame(struct mad_synth *, struct mad_frame const *); Chris@5: Chris@5: # endif Chris@5: Chris@5: /* Id: decoder.h,v 1.17 2004/01/23 09:41:32 rob Exp */ Chris@5: Chris@5: # ifndef LIBMAD_DECODER_H Chris@5: # define LIBMAD_DECODER_H Chris@5: Chris@5: Chris@5: enum mad_decoder_mode { Chris@5: MAD_DECODER_MODE_SYNC = 0, Chris@5: MAD_DECODER_MODE_ASYNC Chris@5: }; Chris@5: Chris@5: enum mad_flow { Chris@5: MAD_FLOW_CONTINUE = 0x0000, /* continue normally */ Chris@5: MAD_FLOW_STOP = 0x0010, /* stop decoding normally */ Chris@5: MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */ Chris@5: MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */ Chris@5: }; Chris@5: Chris@5: struct mad_decoder { Chris@5: enum mad_decoder_mode mode; Chris@5: Chris@5: int options; Chris@5: Chris@5: struct { Chris@5: long pid; Chris@5: int in; Chris@5: int out; Chris@5: } async; Chris@5: Chris@5: struct { Chris@5: struct mad_stream stream; Chris@5: struct mad_frame frame; Chris@5: struct mad_synth synth; Chris@5: } *sync; Chris@5: Chris@5: void *cb_data; Chris@5: Chris@5: enum mad_flow (*input_func)(void *, struct mad_stream *); Chris@5: enum mad_flow (*header_func)(void *, struct mad_header const *); Chris@5: enum mad_flow (*filter_func)(void *, Chris@5: struct mad_stream const *, struct mad_frame *); Chris@5: enum mad_flow (*output_func)(void *, Chris@5: struct mad_header const *, struct mad_pcm *); Chris@5: enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *); Chris@5: enum mad_flow (*message_func)(void *, void *, unsigned int *); Chris@5: }; Chris@5: Chris@5: void mad_decoder_init(struct mad_decoder *, void *, Chris@5: enum mad_flow (*)(void *, struct mad_stream *), Chris@5: enum mad_flow (*)(void *, struct mad_header const *), Chris@5: enum mad_flow (*)(void *, Chris@5: struct mad_stream const *, Chris@5: struct mad_frame *), Chris@5: enum mad_flow (*)(void *, Chris@5: struct mad_header const *, Chris@5: struct mad_pcm *), Chris@5: enum mad_flow (*)(void *, Chris@5: struct mad_stream *, Chris@5: struct mad_frame *), Chris@5: enum mad_flow (*)(void *, void *, unsigned int *)); Chris@5: int mad_decoder_finish(struct mad_decoder *); Chris@5: Chris@5: # define mad_decoder_options(decoder, opts) \ Chris@5: ((void) ((decoder)->options = (opts))) Chris@5: Chris@5: int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode); Chris@5: int mad_decoder_message(struct mad_decoder *, void *, unsigned int *); Chris@5: Chris@5: # endif Chris@5: Chris@5: # ifdef __cplusplus Chris@5: } Chris@5: # endif