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