- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# mad.h

    00001 /*
    00002  * libmad - MPEG audio decoder library
    00003  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
    00004  *
    00005  * This program is free software; you can redistribute it and/or modify
    00006  * it under the terms of the GNU General Public License as published by
    00007  * the Free Software Foundation; either version 2 of the License, or
    00008  * (at your option) any later version.
    00009  *
    00010  * This program is distributed in the hope that it will be useful,
    00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013  * GNU General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU General Public License
    00016  * along with this program; if not, write to the Free Software
    00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018  *
    00019  * If you would like to negotiate alternate licensing terms, you may do
    00020  * so by contacting: Underbit Technologies, Inc. <info@underbit.com>
    00021  */
    00022
    00023 # ifdef __cplusplus
    00024 extern "C" {
    00025 # endif
    00026
    00027 # define FPM_INTEL
    00028
    00029
    00030
    00031 # define SIZEOF_INT 4
    00032 # define SIZEOF_LONG 4
    00033 # define SIZEOF_LONG_LONG 8
    00034
    00035
    00036 /* Id: version.h,v 1.24 2003/05/27 22:40:37 rob Exp */
    00037
    00038 # ifndef LIBMAD_VERSION_H
    00039 # define LIBMAD_VERSION_H
    00040
    00041 # define MAD_VERSION_MAJOR      0
    00042 # define MAD_VERSION_MINOR      15
    00043 # define MAD_VERSION_PATCH      0
    00044 # define MAD_VERSION_EXTRA      " (beta)"
    00045
    00046 # define MAD_VERSION_STRINGIZE(str)     #str
    00047 # define MAD_VERSION_STRING(num)        MAD_VERSION_STRINGIZE(num)
    00048
    00049 # define MAD_VERSION            MAD_VERSION_STRING(MAD_VERSION_MAJOR) "."  \
    00050                                 MAD_VERSION_STRING(MAD_VERSION_MINOR) "."  \
    00051                                 MAD_VERSION_STRING(MAD_VERSION_PATCH)  \
    00052                                 MAD_VERSION_EXTRA
    00053
    00054 # define MAD_PUBLISHYEAR        "2000-2003"
    00055 # define MAD_AUTHOR             "Underbit Technologies, Inc."
    00056 # define MAD_EMAIL              "info@underbit.com"
    00057
    00058 extern char const mad_version[];
    00059 extern char const mad_copyright[];
    00060 extern char const mad_author[];
    00061 extern char const mad_build[];
    00062
    00063 # endif
    00064
    00065 /* Id: fixed.h,v 1.36 2003/05/28 04:36:00 rob Exp */
    00066
    00067 # ifndef LIBMAD_FIXED_H
    00068 # define LIBMAD_FIXED_H
    00069
    00070 # if SIZEOF_INT >= 4
    00071 typedef   signed int mad_fixed_t;
    00072
    00073 typedef   signed int mad_fixed64hi_t;
    00074 typedef unsigned int mad_fixed64lo_t;
    00075 # else
    00076 typedef   signed long mad_fixed_t;
    00077
    00078 typedef   signed long mad_fixed64hi_t;
    00079 typedef unsigned long mad_fixed64lo_t;
    00080 # endif
    00081
    00082 # if defined(_MSC_VER)
    00083 #  define mad_fixed64_t  signed __int64
    00084 # elif 1 || defined(__GNUC__)
    00085 #  define mad_fixed64_t  signed long long
    00086 # endif
    00087
    00088 # if defined(FPM_FLOAT)
    00089 typedef double mad_sample_t;
    00090 # else
    00091 typedef mad_fixed_t mad_sample_t;
    00092 # endif
    00093
    00094 /*
    00095  * Fixed-point format: 0xABBBBBBB
    00096  * A == whole part      (sign + 3 bits)
    00097  * B == fractional part (28 bits)
    00098  *
    00099  * Values are signed two's complement, so the effective range is:
    00100  * 0x80000000 to 0x7fffffff
    00101  *       -8.0 to +7.9999999962747097015380859375
    00102  *
    00103  * The smallest representable value is:
    00104  * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
    00105  *
    00106  * 28 bits of fractional accuracy represent about
    00107  * 8.6 digits of decimal accuracy.
    00108  *
    00109  * Fixed-point numbers can be added or subtracted as normal
    00110  * integers, but multiplication requires shifting the 64-bit result
    00111  * from 56 fractional bits back to 28 (and rounding.)
    00112  *
    00113  * Changing the definition of MAD_F_FRACBITS is only partially
    00114  * supported, and must be done with care.
    00115  */
    00116
    00117 # define MAD_F_FRACBITS         28
    00118
    00119 # if MAD_F_FRACBITS == 28
    00120 #  define MAD_F(x)              ((mad_fixed_t) (x##L))
    00121 # else
    00122 #  if MAD_F_FRACBITS < 28
    00123 #   warning "MAD_F_FRACBITS < 28"
    00124 #   define MAD_F(x)             ((mad_fixed_t)  \
    00125                                  (((x##L) +  \
    00126                                    (1L << (28 - MAD_F_FRACBITS - 1))) >>  \
    00127                                   (28 - MAD_F_FRACBITS)))
    00128 #  elif MAD_F_FRACBITS > 28
    00129 #   error "MAD_F_FRACBITS > 28 not currently supported"
    00130 #   define MAD_F(x)             ((mad_fixed_t)  \
    00131                                  ((x##L) << (MAD_F_FRACBITS - 28)))
    00132 #  endif
    00133 # endif
    00134
    00135 # define MAD_F_MIN              ((mad_fixed_t) -0x80000000L)
    00136 # define MAD_F_MAX              ((mad_fixed_t) +0x7fffffffL)
    00137
    00138 # define MAD_F_ONE              MAD_F(0x10000000)
    00139
    00140 # define mad_f_tofixed(x)       ((mad_fixed_t)  \
    00141                                  ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
    00142 # define mad_f_todouble(x)      ((double)  \
    00143                                  ((x) / (double) (1L << MAD_F_FRACBITS)))
    00144
    00145 # define mad_f_intpart(x)       ((x) >> MAD_F_FRACBITS)
    00146 # define mad_f_fracpart(x)      ((x) & ((1L << MAD_F_FRACBITS) - 1))
    00147                                 /* (x should be positive) */
    00148
    00149 # define mad_f_fromint(x)       ((x) << MAD_F_FRACBITS)
    00150
    00151 # define mad_f_add(x, y)        ((x) + (y))
    00152 # define mad_f_sub(x, y)        ((x) - (y))
    00153
    00154 # if defined(FPM_FLOAT)
    00155 #  error "FPM_FLOAT not yet supported"
    00156
    00157 #  undef MAD_F
    00158 #  define MAD_F(x)              mad_f_todouble(x)
    00159
    00160 #  define mad_f_mul(x, y)       ((x) * (y))
    00161 #  define mad_f_scale64
    00162
    00163 #  undef ASO_ZEROCHECK
    00164
    00165 # elif defined(FPM_64BIT)
    00166
    00167 /*
    00168  * This version should be the most accurate if 64-bit types are supported by
    00169  * the compiler, although it may not be the most efficient.
    00170  */
    00171 #  if defined(OPT_ACCURACY)
    00172 #   define mad_f_mul(x, y)  \
    00173     ((mad_fixed_t)  \
    00174      ((((mad_fixed64_t) (x) * (y)) +  \
    00175        (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
    00176 #  else
    00177 #   define mad_f_mul(x, y)  \
    00178     ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
    00179 #  endif
    00180
    00181 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00182
    00183 /* --- Intel --------------------------------------------------------------- */
    00184
    00185 # elif defined(FPM_INTEL)
    00186
    00187 #  if defined(_MSC_VER)
    00188 #   pragma warning(push)
    00189 #   pragma warning(disable: 4035)  /* no return value */
    00190 static __forceinline
    00191 mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
    00192 {
    00193   enum {
    00194     fracbits = MAD_F_FRACBITS
    00195   };
    00196
    00197   __asm {
    00198     mov eax, x
    00199     imul y
    00200     shrd eax, edx, fracbits
    00201   }
    00202
    00203   /* implicit return of eax */
    00204 }
    00205 #   pragma warning(pop)
    00206
    00207 #   define mad_f_mul            mad_f_mul_inline
    00208 #   define mad_f_scale64
    00209 #  else
    00210 /*
    00211  * This Intel version is fast and accurate; the disposition of the least
    00212  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00213  */
    00214 #   define MAD_F_MLX(hi, lo, x, y)  \
    00215     asm ("imull %3"  \
    00216          : "=a" (lo), "=d" (hi)  \
    00217          : "%a" (x), "rm" (y)  \
    00218          : "cc")
    00219
    00220 #   if defined(OPT_ACCURACY)
    00221 /*
    00222  * This gives best accuracy but is not very fast.
    00223  */
    00224 #    define MAD_F_MLA(hi, lo, x, y)  \
    00225     ({ mad_fixed64hi_t __hi;  \
    00226        mad_fixed64lo_t __lo;  \
    00227        MAD_F_MLX(__hi, __lo, (x), (y));  \
    00228        asm ("addl %2,%0\n\t"  \
    00229             "adcl %3,%1"  \
    00230             : "=rm" (lo), "=rm" (hi)  \
    00231             : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi)  \
    00232             : "cc");  \
    00233     })
    00234 #   endif  /* OPT_ACCURACY */
    00235
    00236 #   if defined(OPT_ACCURACY)
    00237 /*
    00238  * Surprisingly, this is faster than SHRD followed by ADC.
    00239  */
    00240 #    define mad_f_scale64(hi, lo)  \
    00241     ({ mad_fixed64hi_t __hi_;  \
    00242        mad_fixed64lo_t __lo_;  \
    00243        mad_fixed_t __result;  \
    00244        asm ("addl %4,%2\n\t"  \
    00245             "adcl %5,%3"  \
    00246             : "=rm" (__lo_), "=rm" (__hi_)  \
    00247             : "0" (lo), "1" (hi),  \
    00248               "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0)  \
    00249             : "cc");  \
    00250        asm ("shrdl %3,%2,%1"  \
    00251             : "=rm" (__result)  \
    00252             : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS)  \
    00253             : "cc");  \
    00254        __result;  \
    00255     })
    00256 #    else
    00257 #    define mad_f_scale64(hi, lo)  \
    00258     ({ mad_fixed_t __result;  \
    00259        asm ("shrdl %3,%2,%1"  \
    00260             : "=rm" (__result)  \
    00261             : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS)  \
    00262             : "cc");  \
    00263        __result;  \
    00264     })
    00265 #   endif  /* OPT_ACCURACY */
    00266
    00267 #   define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00268 #  endif
    00269
    00270 /* --- ARM ----------------------------------------------------------------- */
    00271
    00272 # elif defined(FPM_ARM)
    00273
    00274 /*
    00275  * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
    00276  * least significant bit is properly rounded at no CPU cycle cost!
    00277  */
    00278 # if 1
    00279 /*
    00280  * This is faster than the default implementation via MAD_F_MLX() and
    00281  * mad_f_scale64().
    00282  */
    00283 #  define mad_f_mul(x, y)  \
    00284     ({ mad_fixed64hi_t __hi;  \
    00285        mad_fixed64lo_t __lo;  \
    00286        mad_fixed_t __result;  \
    00287        asm ("smull      %0, %1, %3, %4\n\t"  \
    00288             "movs       %0, %0, lsr %5\n\t"  \
    00289             "adc        %2, %0, %1, lsl %6"  \
    00290             : "=&r" (__lo), "=&r" (__hi), "=r" (__result)  \
    00291             : "%r" (x), "r" (y),  \
    00292               "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
    00293             : "cc");  \
    00294        __result;  \
    00295     })
    00296 # endif
    00297
    00298 #  define MAD_F_MLX(hi, lo, x, y)  \
    00299     asm ("smull %0, %1, %2, %3"  \
    00300          : "=&r" (lo), "=&r" (hi)  \
    00301          : "%r" (x), "r" (y))
    00302
    00303 #  define MAD_F_MLA(hi, lo, x, y)  \
    00304     asm ("smlal %0, %1, %2, %3"  \
    00305          : "+r" (lo), "+r" (hi)  \
    00306          : "%r" (x), "r" (y))
    00307
    00308 #  define MAD_F_MLN(hi, lo)  \
    00309     asm ("rsbs  %0, %2, #0\n\t"  \
    00310          "rsc   %1, %3, #0"  \
    00311          : "=r" (lo), "=r" (hi)  \
    00312          : "0" (lo), "1" (hi)  \
    00313          : "cc")
    00314
    00315 #  define mad_f_scale64(hi, lo)  \
    00316     ({ mad_fixed_t __result;  \
    00317        asm ("movs       %0, %1, lsr %3\n\t"  \
    00318             "adc        %0, %0, %2, lsl %4"  \
    00319             : "=&r" (__result)  \
    00320             : "r" (lo), "r" (hi),  \
    00321               "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS)  \
    00322             : "cc");  \
    00323        __result;  \
    00324     })
    00325
    00326 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00327
    00328 /* --- MIPS ---------------------------------------------------------------- */
    00329
    00330 # elif defined(FPM_MIPS)
    00331
    00332 /*
    00333  * This MIPS version is fast and accurate; the disposition of the least
    00334  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00335  */
    00336 #  define MAD_F_MLX(hi, lo, x, y)  \
    00337     asm ("mult  %2,%3"  \
    00338          : "=l" (lo), "=h" (hi)  \
    00339          : "%r" (x), "r" (y))
    00340
    00341 # if defined(HAVE_MADD_ASM)
    00342 #  define MAD_F_MLA(hi, lo, x, y)  \
    00343     asm ("madd  %2,%3"  \
    00344          : "+l" (lo), "+h" (hi)  \
    00345          : "%r" (x), "r" (y))
    00346 # elif defined(HAVE_MADD16_ASM)
    00347 /*
    00348  * This loses significant accuracy due to the 16-bit integer limit in the
    00349  * multiply/accumulate instruction.
    00350  */
    00351 #  define MAD_F_ML0(hi, lo, x, y)  \
    00352     asm ("mult  %2,%3"  \
    00353          : "=l" (lo), "=h" (hi)  \
    00354          : "%r" ((x) >> 12), "r" ((y) >> 16))
    00355 #  define MAD_F_MLA(hi, lo, x, y)  \
    00356     asm ("madd16        %2,%3"  \
    00357          : "+l" (lo), "+h" (hi)  \
    00358          : "%r" ((x) >> 12), "r" ((y) >> 16))
    00359 #  define MAD_F_MLZ(hi, lo)  ((mad_fixed_t) (lo))
    00360 # endif
    00361
    00362 # if defined(OPT_SPEED)
    00363 #  define mad_f_scale64(hi, lo)  \
    00364     ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
    00365 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00366 # endif
    00367
    00368 /* --- SPARC --------------------------------------------------------------- */
    00369
    00370 # elif defined(FPM_SPARC)
    00371
    00372 /*
    00373  * This SPARC V8 version is fast and accurate; the disposition of the least
    00374  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00375  */
    00376 #  define MAD_F_MLX(hi, lo, x, y)  \
    00377     asm ("smul %2, %3, %0\n\t"  \
    00378          "rd %%y, %1"  \
    00379          : "=r" (lo), "=r" (hi)  \
    00380          : "%r" (x), "rI" (y))
    00381
    00382 /* --- PowerPC ------------------------------------------------------------- */
    00383
    00384 # elif defined(FPM_PPC)
    00385
    00386 /*
    00387  * This PowerPC version is fast and accurate; the disposition of the least
    00388  * significant bit depends on OPT_ACCURACY via mad_f_scale64().
    00389  */
    00390 #  define MAD_F_MLX(hi, lo, x, y)  \
    00391     do {  \
    00392       asm ("mullw %0,%1,%2"  \
    00393            : "=r" (lo)  \
    00394            : "%r" (x), "r" (y));  \
    00395       asm ("mulhw %0,%1,%2"  \
    00396            : "=r" (hi)  \
    00397            : "%r" (x), "r" (y));  \
    00398     }  \
    00399     while (0)
    00400
    00401 #  if defined(OPT_ACCURACY)
    00402 /*
    00403  * This gives best accuracy but is not very fast.
    00404  */
    00405 #   define MAD_F_MLA(hi, lo, x, y)  \
    00406     ({ mad_fixed64hi_t __hi;  \
    00407        mad_fixed64lo_t __lo;  \
    00408        MAD_F_MLX(__hi, __lo, (x), (y));  \
    00409        asm ("addc %0,%2,%3\n\t"  \
    00410             "adde %1,%4,%5"  \
    00411             : "=r" (lo), "=r" (hi)  \
    00412             : "%r" (lo), "r" (__lo),  \
    00413               "%r" (hi), "r" (__hi)  \
    00414             : "xer");  \
    00415     })
    00416 #  endif
    00417
    00418 #  if defined(OPT_ACCURACY)
    00419 /*
    00420  * This is slower than the truncating version below it.
    00421  */
    00422 #   define mad_f_scale64(hi, lo)  \
    00423     ({ mad_fixed_t __result, __round;  \
    00424        asm ("rotrwi %0,%1,%2"  \
    00425             : "=r" (__result)  \
    00426             : "r" (lo), "i" (MAD_F_SCALEBITS));  \
    00427        asm ("extrwi %0,%1,1,0"  \
    00428             : "=r" (__round)  \
    00429             : "r" (__result));  \
    00430        asm ("insrwi %0,%1,%2,0"  \
    00431             : "+r" (__result)  \
    00432             : "r" (hi), "i" (MAD_F_SCALEBITS));  \
    00433        asm ("add %0,%1,%2"  \
    00434             : "=r" (__result)  \
    00435             : "%r" (__result), "r" (__round));  \
    00436        __result;  \
    00437     })
    00438 #  else
    00439 #   define mad_f_scale64(hi, lo)  \
    00440     ({ mad_fixed_t __result;  \
    00441        asm ("rotrwi %0,%1,%2"  \
    00442             : "=r" (__result)  \
    00443             : "r" (lo), "i" (MAD_F_SCALEBITS));  \
    00444        asm ("insrwi %0,%1,%2,0"  \
    00445             : "+r" (__result)  \
    00446             : "r" (hi), "i" (MAD_F_SCALEBITS));  \
    00447        __result;  \
    00448     })
    00449 #  endif
    00450
    00451 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00452
    00453 /* --- Default ------------------------------------------------------------- */
    00454
    00455 # elif defined(FPM_DEFAULT)
    00456
    00457 /*
    00458  * This version is the most portable but it loses significant accuracy.
    00459  * Furthermore, accuracy is biased against the second argument, so care
    00460  * should be taken when ordering operands.
    00461  *
    00462  * The scale factors are constant as this is not used with SSO.
    00463  *
    00464  * Pre-rounding is required to stay within the limits of compliance.
    00465  */
    00466 #  if defined(OPT_SPEED)
    00467 #   define mad_f_mul(x, y)      (((x) >> 12) * ((y) >> 16))
    00468 #  else
    00469 #   define mad_f_mul(x, y)      ((((x) + (1L << 11)) >> 12) *  \
    00470                                  (((y) + (1L << 15)) >> 16))
    00471 #  endif
    00472
    00473 /* ------------------------------------------------------------------------- */
    00474
    00475 # else
    00476 #  error "no FPM selected"
    00477 # endif
    00478
    00479 /* default implementations */
    00480
    00481 # if !defined(mad_f_mul)
    00482 #  define mad_f_mul(x, y)  \
    00483     ({ register mad_fixed64hi_t __hi;  \
    00484        register mad_fixed64lo_t __lo;  \
    00485        MAD_F_MLX(__hi, __lo, (x), (y));  \
    00486        mad_f_scale64(__hi, __lo);  \
    00487     })
    00488 # endif
    00489
    00490 # if !defined(MAD_F_MLA)
    00491 #  define MAD_F_ML0(hi, lo, x, y)       ((lo)  = mad_f_mul((x), (y)))
    00492 #  define MAD_F_MLA(hi, lo, x, y)       ((lo) += mad_f_mul((x), (y)))
    00493 #  define MAD_F_MLN(hi, lo)             ((lo)  = -(lo))
    00494 #  define MAD_F_MLZ(hi, lo)             ((void) (hi), (mad_fixed_t) (lo))
    00495 # endif
    00496
    00497 # if !defined(MAD_F_ML0)
    00498 #  define MAD_F_ML0(hi, lo, x, y)       MAD_F_MLX((hi), (lo), (x), (y))
    00499 # endif
    00500
    00501 # if !defined(MAD_F_MLN)
    00502 #  define MAD_F_MLN(hi, lo)             ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
    00503 # endif
    00504
    00505 # if !defined(MAD_F_MLZ)
    00506 #  define MAD_F_MLZ(hi, lo)             mad_f_scale64((hi), (lo))
    00507 # endif
    00508
    00509 # if !defined(mad_f_scale64)
    00510 #  if defined(OPT_ACCURACY)
    00511 #   define mad_f_scale64(hi, lo)  \
    00512     ((((mad_fixed_t)  \
    00513        (((hi) << (32 - (MAD_F_SCALEBITS - 1))) |  \
    00514         ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
    00515 #  else
    00516 #   define mad_f_scale64(hi, lo)  \
    00517     ((mad_fixed_t)  \
    00518      (((hi) << (32 - MAD_F_SCALEBITS)) |  \
    00519       ((lo) >> MAD_F_SCALEBITS)))
    00520 #  endif
    00521 #  define MAD_F_SCALEBITS  MAD_F_FRACBITS
    00522 # endif
    00523
    00524 /* C routines */
    00525
    00526 mad_fixed_t mad_f_abs(mad_fixed_t);
    00527 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
    00528
    00529 # endif
    00530
    00531 /* Id: bit.h,v 1.11 2003/05/27 22:40:36 rob Exp */
    00532
    00533 # ifndef LIBMAD_BIT_H
    00534 # define LIBMAD_BIT_H
    00535
    00536 struct mad_bitptr {
    00537   unsigned char const *byte;
    00538   unsigned short cache;
    00539   unsigned short left;
    00540 };
    00541
    00542 void mad_bit_init(struct mad_bitptr *, unsigned char const *);
    00543
    00544 # define mad_bit_finish(bitptr)         /* nothing */
    00545
    00546 unsigned int mad_bit_length(struct mad_bitptr const *,
    00547                             struct mad_bitptr const *);
    00548
    00549 # define mad_bit_bitsleft(bitptr)  ((bitptr)->left)
    00550 unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *);
    00551
    00552 void mad_bit_skip(struct mad_bitptr *, unsigned int);
    00553 unsigned long mad_bit_read(struct mad_bitptr *, unsigned int);
    00554 void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long);
    00555
    00556 unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short);
    00557
    00558 # endif
    00559
    00560 /* Id: timer.h,v 1.15 2003/05/27 22:40:37 rob Exp */
    00561
    00562 # ifndef LIBMAD_TIMER_H
    00563 # define LIBMAD_TIMER_H
    00564
    00565 typedef struct {
    00566   signed long seconds;          /* whole seconds */
    00567   unsigned long fraction;       /* 1/MAD_TIMER_RESOLUTION seconds */
    00568 } mad_timer_t;
    00569
    00570 extern mad_timer_t const mad_timer_zero;
    00571
    00572 # define MAD_TIMER_RESOLUTION   352800000UL
    00573
    00574 enum mad_units {
    00575   MAD_UNITS_HOURS        =    -2,
    00576   MAD_UNITS_MINUTES      =    -1,
    00577   MAD_UNITS_SECONDS      =     0,
    00578
    00579   /* metric units */
    00580
    00581   MAD_UNITS_DECISECONDS  =    10,
    00582   MAD_UNITS_CENTISECONDS =   100,
    00583   MAD_UNITS_MILLISECONDS =  1000,
    00584
    00585   /* audio sample units */
    00586
    00587   MAD_UNITS_8000_HZ      =  8000,
    00588   MAD_UNITS_11025_HZ     = 11025,
    00589   MAD_UNITS_12000_HZ     = 12000,
    00590
    00591   MAD_UNITS_16000_HZ     = 16000,
    00592   MAD_UNITS_22050_HZ     = 22050,
    00593   MAD_UNITS_24000_HZ     = 24000,
    00594
    00595   MAD_UNITS_32000_HZ     = 32000,
    00596   MAD_UNITS_44100_HZ     = 44100,
    00597   MAD_UNITS_48000_HZ     = 48000,
    00598
    00599   /* video frame/field units */
    00600
    00601   MAD_UNITS_24_FPS       =    24,
    00602   MAD_UNITS_25_FPS       =    25,
    00603   MAD_UNITS_30_FPS       =    30,
    00604   MAD_UNITS_48_FPS       =    48,
    00605   MAD_UNITS_50_FPS       =    50,
    00606   MAD_UNITS_60_FPS       =    60,
    00607
    00608   /* CD audio frames */
    00609
    00610   MAD_UNITS_75_FPS       =    75,
    00611
    00612   /* video drop-frame units */
    00613
    00614   MAD_UNITS_23_976_FPS   =   -24,
    00615   MAD_UNITS_24_975_FPS   =   -25,
    00616   MAD_UNITS_29_97_FPS    =   -30,
    00617   MAD_UNITS_47_952_FPS   =   -48,
    00618   MAD_UNITS_49_95_FPS    =   -50,
    00619   MAD_UNITS_59_94_FPS    =   -60
    00620 };
    00621
    00622 # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero))
    00623
    00624 int mad_timer_compare(mad_timer_t, mad_timer_t);
    00625
    00626 # define mad_timer_sign(timer)  mad_timer_compare((timer), mad_timer_zero)
    00627
    00628 void mad_timer_negate(mad_timer_t *);
    00629 mad_timer_t mad_timer_abs(mad_timer_t);
    00630
    00631 void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long);
    00632 void mad_timer_add(mad_timer_t *, mad_timer_t);
    00633 void mad_timer_multiply(mad_timer_t *, signed long);
    00634
    00635 signed long mad_timer_count(mad_timer_t, enum mad_units);
    00636 unsigned long mad_timer_fraction(mad_timer_t, unsigned long);
    00637 void mad_timer_string(mad_timer_t, char *, char const *,
    00638                       enum mad_units, enum mad_units, unsigned long);
    00639
    00640 # endif
    00641
    00642 /* Id: stream.h,v 1.18 2003/05/27 22:40:37 rob Exp */
    00643
    00644 # ifndef LIBMAD_STREAM_H
    00645 # define LIBMAD_STREAM_H
    00646
    00647
    00648 # define MAD_BUFFER_GUARD       8
    00649 # define MAD_BUFFER_MDLEN       (511 + 2048 + MAD_BUFFER_GUARD)
    00650
    00651 enum mad_error {
    00652   MAD_ERROR_NONE           = 0x0000,    /* no error */
    00653
    00654   MAD_ERROR_BUFLEN         = 0x0001,    /* input buffer too small (or EOF) */
    00655   MAD_ERROR_BUFPTR         = 0x0002,    /* invalid (null) buffer pointer */
    00656
    00657   MAD_ERROR_NOMEM          = 0x0031,    /* not enough memory */
    00658
    00659   MAD_ERROR_LOSTSYNC       = 0x0101,    /* lost synchronization */
    00660   MAD_ERROR_BADLAYER       = 0x0102,    /* reserved header layer value */
    00661   MAD_ERROR_BADBITRATE     = 0x0103,    /* forbidden bitrate value */
    00662   MAD_ERROR_BADSAMPLERATE  = 0x0104,    /* reserved sample frequency value */
    00663   MAD_ERROR_BADEMPHASIS    = 0x0105,    /* reserved emphasis value */
    00664
    00665   MAD_ERROR_BADCRC         = 0x0201,    /* CRC check failed */
    00666   MAD_ERROR_BADBITALLOC    = 0x0211,    /* forbidden bit allocation value */
    00667   MAD_ERROR_BADSCALEFACTOR = 0x0221,    /* bad scalefactor index */
    00668   MAD_ERROR_BADFRAMELEN    = 0x0231,    /* bad frame length */
    00669   MAD_ERROR_BADBIGVALUES   = 0x0232,    /* bad big_values count */
    00670   MAD_ERROR_BADBLOCKTYPE   = 0x0233,    /* reserved block_type */
    00671   MAD_ERROR_BADSCFSI       = 0x0234,    /* bad scalefactor selection info */
    00672   MAD_ERROR_BADDATAPTR     = 0x0235,    /* bad main_data_begin pointer */
    00673   MAD_ERROR_BADPART3LEN    = 0x0236,    /* bad audio data length */
    00674   MAD_ERROR_BADHUFFTABLE   = 0x0237,    /* bad Huffman table select */
    00675   MAD_ERROR_BADHUFFDATA    = 0x0238,    /* Huffman data overrun */
    00676   MAD_ERROR_BADSTEREO      = 0x0239     /* incompatible block_type for JS */
    00677 };
    00678
    00679 # define MAD_RECOVERABLE(error) ((error) & 0xff00)
    00680
    00681 struct mad_stream {
    00682   unsigned char const *buffer;          /* input bitstream buffer */
    00683   unsigned char const *bufend;          /* end of buffer */
    00684   unsigned long skiplen;                /* bytes to skip before next frame */
    00685
    00686   int sync;                             /* stream sync found */
    00687   unsigned long freerate;               /* free bitrate (fixed) */
    00688
    00689   unsigned char const *this_frame;      /* start of current frame */
    00690   unsigned char const *next_frame;      /* start of next frame */
    00691   struct mad_bitptr ptr;                /* current processing bit pointer */
    00692
    00693   struct mad_bitptr anc_ptr;            /* ancillary bits pointer */
    00694   unsigned int anc_bitlen;              /* number of ancillary bits */
    00695
    00696   unsigned char (*main_data)[MAD_BUFFER_MDLEN];
    00697                                         /* Layer III main_data() */
    00698   unsigned int md_len;                  /* bytes in main_data */
    00699
    00700   int options;                          /* decoding options (see below) */
    00701   enum mad_error error;                 /* error code (see above) */
    00702 };
    00703
    00704 enum {
    00705   MAD_OPTION_IGNORECRC      = 0x0001,   /* ignore CRC errors */
    00706   MAD_OPTION_HALFSAMPLERATE = 0x0002    /* generate PCM at 1/2 sample rate */
    00707 # if 0  /* not yet implemented */
    00708   MAD_OPTION_LEFTCHANNEL    = 0x0010,   /* decode left channel only */
    00709   MAD_OPTION_RIGHTCHANNEL   = 0x0020,   /* decode right channel only */
    00710   MAD_OPTION_SINGLECHANNEL  = 0x0030    /* combine channels */
    00711 # endif
    00712 };
    00713
    00714 void mad_stream_init(struct mad_stream *);
    00715 void mad_stream_finish(struct mad_stream *);
    00716
    00717 # define mad_stream_options(stream, opts)  \
    00718     ((void) ((stream)->options = (opts)))
    00719
    00720 void mad_stream_buffer(struct mad_stream *,
    00721                        unsigned char const *, unsigned long);
    00722 void mad_stream_skip(struct mad_stream *, unsigned long);
    00723
    00724 int mad_stream_sync(struct mad_stream *);
    00725
    00726 char const *mad_stream_errorstr(struct mad_stream const *);
    00727
    00728 # endif
    00729
    00730 /* Id: frame.h,v 1.19 2003/05/27 22:40:36 rob Exp */
    00731
    00732 # ifndef LIBMAD_FRAME_H
    00733 # define LIBMAD_FRAME_H
    00734
    00735
    00736 enum mad_layer {
    00737   MAD_LAYER_I   = 1,                    /* Layer I */
    00738   MAD_LAYER_II  = 2,                    /* Layer II */
    00739   MAD_LAYER_III = 3                     /* Layer III */
    00740 };
    00741
    00742 enum mad_mode {
    00743   MAD_MODE_SINGLE_CHANNEL = 0,          /* single channel */
    00744   MAD_MODE_DUAL_CHANNEL   = 1,          /* dual channel */
    00745   MAD_MODE_JOINT_STEREO   = 2,          /* joint (MS/intensity) stereo */
    00746   MAD_MODE_STEREO         = 3           /* normal LR stereo */
    00747 };
    00748
    00749 enum mad_emphasis {
    00750   MAD_EMPHASIS_NONE       = 0,          /* no emphasis */
    00751   MAD_EMPHASIS_50_15_US   = 1,          /* 50/15 microseconds emphasis */
    00752   MAD_EMPHASIS_CCITT_J_17 = 3,          /* CCITT J.17 emphasis */
    00753   MAD_EMPHASIS_RESERVED   = 2           /* unknown emphasis */
    00754 };
    00755
    00756 struct mad_header {
    00757   enum mad_layer layer;                 /* audio layer (1, 2, or 3) */
    00758   enum mad_mode mode;                   /* channel mode (see above) */
    00759   int mode_extension;                   /* additional mode info */
    00760   enum mad_emphasis emphasis;           /* de-emphasis to use (see above) */
    00761
    00762   unsigned long bitrate;                /* stream bitrate (bps) */
    00763   unsigned int samplerate;              /* sampling frequency (Hz) */
    00764
    00765   unsigned short crc_check;             /* frame CRC accumulator */
    00766   unsigned short crc_target;            /* final target CRC checksum */
    00767
    00768   int flags;                            /* flags (see below) */
    00769   int private_bits;                     /* private bits (see below) */
    00770
    00771   mad_timer_t duration;                 /* audio playing time of frame */
    00772 };
    00773
    00774 struct mad_frame {
    00775   struct mad_header header;             /* MPEG audio header */
    00776
    00777   int options;                          /* decoding options (from stream) */
    00778
    00779   mad_fixed_t sbsample[2][36][32];      /* synthesis subband filter samples */
    00780   mad_fixed_t (*overlap)[2][32][18];    /* Layer III block overlap data */
    00781 };
    00782
    00783 # define MAD_NCHANNELS(header)          ((header)->mode ? 2 : 1)
    00784 # define MAD_NSBSAMPLES(header)  \
    00785   ((header)->layer == MAD_LAYER_I ? 12 :  \
    00786    (((header)->layer == MAD_LAYER_III &&  \
    00787      ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36))
    00788
    00789 enum {
    00790   MAD_FLAG_NPRIVATE_III = 0x0007,       /* number of Layer III private bits */
    00791   MAD_FLAG_INCOMPLETE   = 0x0008,       /* header but not data is decoded */
    00792
    00793   MAD_FLAG_PROTECTION   = 0x0010,       /* frame has CRC protection */
    00794   MAD_FLAG_COPYRIGHT    = 0x0020,       /* frame is copyright */
    00795   MAD_FLAG_ORIGINAL     = 0x0040,       /* frame is original (else copy) */
    00796   MAD_FLAG_PADDING      = 0x0080,       /* frame has additional slot */
    00797
    00798   MAD_FLAG_I_STEREO     = 0x0100,       /* uses intensity joint stereo */
    00799   MAD_FLAG_MS_STEREO    = 0x0200,       /* uses middle/side joint stereo */
    00800   MAD_FLAG_FREEFORMAT   = 0x0400,       /* uses free format bitrate */
    00801
    00802   MAD_FLAG_LSF_EXT      = 0x1000,       /* lower sampling freq. extension */
    00803   MAD_FLAG_MC_EXT       = 0x2000,       /* multichannel audio extension */
    00804   MAD_FLAG_MPEG_2_5_EXT = 0x4000        /* MPEG 2.5 (unofficial) extension */
    00805 };
    00806
    00807 enum {
    00808   MAD_PRIVATE_HEADER    = 0x0100,       /* header private bit */
    00809   MAD_PRIVATE_III       = 0x001f        /* Layer III private bits (up to 5) */
    00810 };
    00811
    00812 void mad_header_init(struct mad_header *);
    00813
    00814 # define mad_header_finish(header)  /* nothing */
    00815
    00816 int mad_header_decode(struct mad_header *, struct mad_stream *);
    00817
    00818 void mad_frame_init(struct mad_frame *);
    00819 void mad_frame_finish(struct mad_frame *);
    00820
    00821 int mad_frame_decode(struct mad_frame *, struct mad_stream *);
    00822
    00823 void mad_frame_mute(struct mad_frame *);
    00824
    00825 # endif
    00826
    00827 /* Id: synth.h,v 1.14 2003/05/27 22:40:37 rob Exp */
    00828
    00829 # ifndef LIBMAD_SYNTH_H
    00830 # define LIBMAD_SYNTH_H
    00831
    00832
    00833 struct mad_pcm {
    00834   unsigned int samplerate;              /* sampling frequency (Hz) */
    00835   unsigned short channels;              /* number of channels */
    00836   unsigned short length;                /* number of samples per channel */
    00837   mad_fixed_t samples[2][1152];         /* PCM output samples [ch][sample] */
    00838 };
    00839
    00840 struct mad_synth {
    00841   mad_fixed_t filter[2][2][2][16][8];   /* polyphase filterbank outputs */
    00842                                         /* [ch][eo][peo][s][v] */
    00843
    00844   unsigned int phase;                   /* current processing phase */
    00845
    00846   struct mad_pcm pcm;                   /* PCM output */
    00847 };
    00848
    00849 /* single channel PCM selector */
    00850 enum {
    00851   MAD_PCM_CHANNEL_SINGLE = 0
    00852 };
    00853
    00854 /* dual channel PCM selector */
    00855 enum {
    00856   MAD_PCM_CHANNEL_DUAL_1 = 0,
    00857   MAD_PCM_CHANNEL_DUAL_2 = 1
    00858 };
    00859
    00860 /* stereo PCM selector */
    00861 enum {
    00862   MAD_PCM_CHANNEL_STEREO_LEFT  = 0,
    00863   MAD_PCM_CHANNEL_STEREO_RIGHT = 1
    00864 };
    00865
    00866 void mad_synth_init(struct mad_synth *);
    00867
    00868 # define mad_synth_finish(synth)  /* nothing */
    00869
    00870 void mad_synth_mute(struct mad_synth *);
    00871
    00872 void mad_synth_frame(struct mad_synth *, struct mad_frame const *);
    00873
    00874 # endif
    00875
    00876 /* Id: decoder.h,v 1.16 2003/05/27 22:40:36 rob Exp */
    00877
    00878 # ifndef LIBMAD_DECODER_H
    00879 # define LIBMAD_DECODER_H
    00880
    00881
    00882 enum mad_decoder_mode {
    00883   MAD_DECODER_MODE_SYNC  = 0,
    00884   MAD_DECODER_MODE_ASYNC
    00885 };
    00886
    00887 enum mad_flow {
    00888   MAD_FLOW_CONTINUE = 0x0000,   /* continue normally */
    00889   MAD_FLOW_STOP     = 0x0010,   /* stop decoding normally */
    00890   MAD_FLOW_BREAK    = 0x0011,   /* stop decoding and signal an error */
    00891   MAD_FLOW_IGNORE   = 0x0020    /* ignore the current frame */
    00892 };
    00893
    00894 struct mad_decoder {
    00895   enum mad_decoder_mode mode;
    00896
    00897   int options;
    00898
    00899   struct {
    00900     long pid;
    00901     int in;
    00902     int out;
    00903   } async;
    00904
    00905   struct {
    00906     struct mad_stream stream;
    00907     struct mad_frame frame;
    00908     struct mad_synth synth;
    00909   } *sync;
    00910
    00911   void *cb_data;
    00912
    00913   enum mad_flow (*input_func)(void *, struct mad_stream *);
    00914   enum mad_flow (*header_func)(void *, struct mad_header const *);
    00915   enum mad_flow (*filter_func)(void *,
    00916                                struct mad_stream const *, struct mad_frame *);
    00917   enum mad_flow (*output_func)(void *,
    00918                                struct mad_header const *, struct mad_pcm *);
    00919   enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
    00920   enum mad_flow (*message_func)(void *, void *, unsigned int *);
    00921 };
    00922
    00923 void mad_decoder_init(struct mad_decoder *, void *,
    00924                       enum mad_flow (*)(void *, struct mad_stream *),
    00925                       enum mad_flow (*)(void *, struct mad_header const *),
    00926                       enum mad_flow (*)(void *,
    00927                                         struct mad_stream const *,
    00928                                         struct mad_frame *),
    00929                       enum mad_flow (*)(void *,
    00930                                         struct mad_header const *,
    00931                                         struct mad_pcm *),
    00932                       enum mad_flow (*)(void *,
    00933                                         struct mad_stream *,
    00934                                         struct mad_frame *),
    00935                       enum mad_flow (*)(void *, void *, unsigned int *));
    00936 int mad_decoder_finish(struct mad_decoder *);
    00937
    00938 # define mad_decoder_options(decoder, opts)  \
    00939     ((void) ((decoder)->options = (opts)))
    00940
    00941 int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode);
    00942 int mad_decoder_message(struct mad_decoder *, void *, unsigned int *);
    00943
    00944 # endif
    00945
    00946 # ifdef __cplusplus
    00947 }
    00948 # endif