Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mad.h
00001 /* 00002 * libmad - MPEG audio decoder library 00003 * Copyright (C) 2000-2004 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.26 2004/01/23 09:41:33 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 1 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-2004" 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.38 2004/02/17 02:02:03 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 # elif defined(OPT_INTEL) 00257 /* 00258 * Alternate Intel scaling that may or may not perform better. 00259 */ 00260 # define mad_f_scale64(hi, lo) \ 00261 ({ mad_fixed_t __result; \ 00262 asm ("shrl %3,%1\n\t" \ 00263 "shll %4,%2\n\t" \ 00264 "orl %2,%1" \ 00265 : "=rm" (__result) \ 00266 : "0" (lo), "r" (hi), \ 00267 "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \ 00268 : "cc"); \ 00269 __result; \ 00270 }) 00271 # else 00272 # define mad_f_scale64(hi, lo) \ 00273 ({ mad_fixed_t __result; \ 00274 asm ("shrdl %3,%2,%1" \ 00275 : "=rm" (__result) \ 00276 : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \ 00277 : "cc"); \ 00278 __result; \ 00279 }) 00280 # endif /* OPT_ACCURACY */ 00281 00282 # define MAD_F_SCALEBITS MAD_F_FRACBITS 00283 # endif 00284 00285 /* --- ARM ----------------------------------------------------------------- */ 00286 00287 # elif defined(FPM_ARM) 00288 00289 /* 00290 * This ARM V4 version is as accurate as FPM_64BIT but much faster. The 00291 * least significant bit is properly rounded at no CPU cycle cost! 00292 */ 00293 # if 1 00294 /* 00295 * This is faster than the default implementation via MAD_F_MLX() and 00296 * mad_f_scale64(). 00297 */ 00298 # define mad_f_mul(x, y) \ 00299 ({ mad_fixed64hi_t __hi; \ 00300 mad_fixed64lo_t __lo; \ 00301 mad_fixed_t __result; \ 00302 asm ("smull %0, %1, %3, %4\n\t" \ 00303 "movs %0, %0, lsr %5\n\t" \ 00304 "adc %2, %0, %1, lsl %6" \ 00305 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \ 00306 : "%r" (x), "r" (y), \ 00307 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ 00308 : "cc"); \ 00309 __result; \ 00310 }) 00311 # endif 00312 00313 # define MAD_F_MLX(hi, lo, x, y) \ 00314 asm ("smull %0, %1, %2, %3" \ 00315 : "=&r" (lo), "=&r" (hi) \ 00316 : "%r" (x), "r" (y)) 00317 00318 # define MAD_F_MLA(hi, lo, x, y) \ 00319 asm ("smlal %0, %1, %2, %3" \ 00320 : "+r" (lo), "+r" (hi) \ 00321 : "%r" (x), "r" (y)) 00322 00323 # define MAD_F_MLN(hi, lo) \ 00324 asm ("rsbs %0, %2, #0\n\t" \ 00325 "rsc %1, %3, #0" \ 00326 : "=r" (lo), "=r" (hi) \ 00327 : "0" (lo), "1" (hi) \ 00328 : "cc") 00329 00330 # define mad_f_scale64(hi, lo) \ 00331 ({ mad_fixed_t __result; \ 00332 asm ("movs %0, %1, lsr %3\n\t" \ 00333 "adc %0, %0, %2, lsl %4" \ 00334 : "=&r" (__result) \ 00335 : "r" (lo), "r" (hi), \ 00336 "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \ 00337 : "cc"); \ 00338 __result; \ 00339 }) 00340 00341 # define MAD_F_SCALEBITS MAD_F_FRACBITS 00342 00343 /* --- MIPS ---------------------------------------------------------------- */ 00344 00345 # elif defined(FPM_MIPS) 00346 00347 /* 00348 * This MIPS version is fast and accurate; the disposition of the least 00349 * significant bit depends on OPT_ACCURACY via mad_f_scale64(). 00350 */ 00351 # define MAD_F_MLX(hi, lo, x, y) \ 00352 asm ("mult %2,%3" \ 00353 : "=l" (lo), "=h" (hi) \ 00354 : "%r" (x), "r" (y)) 00355 00356 # if defined(HAVE_MADD_ASM) 00357 # define MAD_F_MLA(hi, lo, x, y) \ 00358 asm ("madd %2,%3" \ 00359 : "+l" (lo), "+h" (hi) \ 00360 : "%r" (x), "r" (y)) 00361 # elif defined(HAVE_MADD16_ASM) 00362 /* 00363 * This loses significant accuracy due to the 16-bit integer limit in the 00364 * multiply/accumulate instruction. 00365 */ 00366 # define MAD_F_ML0(hi, lo, x, y) \ 00367 asm ("mult %2,%3" \ 00368 : "=l" (lo), "=h" (hi) \ 00369 : "%r" ((x) >> 12), "r" ((y) >> 16)) 00370 # define MAD_F_MLA(hi, lo, x, y) \ 00371 asm ("madd16 %2,%3" \ 00372 : "+l" (lo), "+h" (hi) \ 00373 : "%r" ((x) >> 12), "r" ((y) >> 16)) 00374 # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo)) 00375 # endif 00376 00377 # if defined(OPT_SPEED) 00378 # define mad_f_scale64(hi, lo) \ 00379 ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS))) 00380 # define MAD_F_SCALEBITS MAD_F_FRACBITS 00381 # endif 00382 00383 /* --- SPARC --------------------------------------------------------------- */ 00384 00385 # elif defined(FPM_SPARC) 00386 00387 /* 00388 * This SPARC V8 version is fast and accurate; the disposition of the least 00389 * significant bit depends on OPT_ACCURACY via mad_f_scale64(). 00390 */ 00391 # define MAD_F_MLX(hi, lo, x, y) \ 00392 asm ("smul %2, %3, %0\n\t" \ 00393 "rd %%y, %1" \ 00394 : "=r" (lo), "=r" (hi) \ 00395 : "%r" (x), "rI" (y)) 00396 00397 /* --- PowerPC ------------------------------------------------------------- */ 00398 00399 # elif defined(FPM_PPC) 00400 00401 /* 00402 * This PowerPC version is fast and accurate; the disposition of the least 00403 * significant bit depends on OPT_ACCURACY via mad_f_scale64(). 00404 */ 00405 # define MAD_F_MLX(hi, lo, x, y) \ 00406 do { \ 00407 asm ("mullw %0,%1,%2" \ 00408 : "=r" (lo) \ 00409 : "%r" (x), "r" (y)); \ 00410 asm ("mulhw %0,%1,%2" \ 00411 : "=r" (hi) \ 00412 : "%r" (x), "r" (y)); \ 00413 } \ 00414 while (0) 00415 00416 # if defined(OPT_ACCURACY) 00417 /* 00418 * This gives best accuracy but is not very fast. 00419 */ 00420 # define MAD_F_MLA(hi, lo, x, y) \ 00421 ({ mad_fixed64hi_t __hi; \ 00422 mad_fixed64lo_t __lo; \ 00423 MAD_F_MLX(__hi, __lo, (x), (y)); \ 00424 asm ("addc %0,%2,%3\n\t" \ 00425 "adde %1,%4,%5" \ 00426 : "=r" (lo), "=r" (hi) \ 00427 : "%r" (lo), "r" (__lo), \ 00428 "%r" (hi), "r" (__hi) \ 00429 : "xer"); \ 00430 }) 00431 # endif 00432 00433 # if defined(OPT_ACCURACY) 00434 /* 00435 * This is slower than the truncating version below it. 00436 */ 00437 # define mad_f_scale64(hi, lo) \ 00438 ({ mad_fixed_t __result, __round; \ 00439 asm ("rotrwi %0,%1,%2" \ 00440 : "=r" (__result) \ 00441 : "r" (lo), "i" (MAD_F_SCALEBITS)); \ 00442 asm ("extrwi %0,%1,1,0" \ 00443 : "=r" (__round) \ 00444 : "r" (__result)); \ 00445 asm ("insrwi %0,%1,%2,0" \ 00446 : "+r" (__result) \ 00447 : "r" (hi), "i" (MAD_F_SCALEBITS)); \ 00448 asm ("add %0,%1,%2" \ 00449 : "=r" (__result) \ 00450 : "%r" (__result), "r" (__round)); \ 00451 __result; \ 00452 }) 00453 # else 00454 # define mad_f_scale64(hi, lo) \ 00455 ({ mad_fixed_t __result; \ 00456 asm ("rotrwi %0,%1,%2" \ 00457 : "=r" (__result) \ 00458 : "r" (lo), "i" (MAD_F_SCALEBITS)); \ 00459 asm ("insrwi %0,%1,%2,0" \ 00460 : "+r" (__result) \ 00461 : "r" (hi), "i" (MAD_F_SCALEBITS)); \ 00462 __result; \ 00463 }) 00464 # endif 00465 00466 # define MAD_F_SCALEBITS MAD_F_FRACBITS 00467 00468 /* --- Default ------------------------------------------------------------- */ 00469 00470 # elif defined(FPM_DEFAULT) 00471 00472 /* 00473 * This version is the most portable but it loses significant accuracy. 00474 * Furthermore, accuracy is biased against the second argument, so care 00475 * should be taken when ordering operands. 00476 * 00477 * The scale factors are constant as this is not used with SSO. 00478 * 00479 * Pre-rounding is required to stay within the limits of compliance. 00480 */ 00481 # if defined(OPT_SPEED) 00482 # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16)) 00483 # else 00484 # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \ 00485 (((y) + (1L << 15)) >> 16)) 00486 # endif 00487 00488 /* ------------------------------------------------------------------------- */ 00489 00490 # else 00491 # error "no FPM selected" 00492 # endif 00493 00494 /* default implementations */ 00495 00496 # if !defined(mad_f_mul) 00497 # define mad_f_mul(x, y) \ 00498 ({ register mad_fixed64hi_t __hi; \ 00499 register mad_fixed64lo_t __lo; \ 00500 MAD_F_MLX(__hi, __lo, (x), (y)); \ 00501 mad_f_scale64(__hi, __lo); \ 00502 }) 00503 # endif 00504 00505 # if !defined(MAD_F_MLA) 00506 # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y))) 00507 # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y))) 00508 # define MAD_F_MLN(hi, lo) ((lo) = -(lo)) 00509 # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo)) 00510 # endif 00511 00512 # if !defined(MAD_F_ML0) 00513 # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y)) 00514 # endif 00515 00516 # if !defined(MAD_F_MLN) 00517 # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi)) 00518 # endif 00519 00520 # if !defined(MAD_F_MLZ) 00521 # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo)) 00522 # endif 00523 00524 # if !defined(mad_f_scale64) 00525 # if defined(OPT_ACCURACY) 00526 # define mad_f_scale64(hi, lo) \ 00527 ((((mad_fixed_t) \ 00528 (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \ 00529 ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1) 00530 # else 00531 # define mad_f_scale64(hi, lo) \ 00532 ((mad_fixed_t) \ 00533 (((hi) << (32 - MAD_F_SCALEBITS)) | \ 00534 ((lo) >> MAD_F_SCALEBITS))) 00535 # endif 00536 # define MAD_F_SCALEBITS MAD_F_FRACBITS 00537 # endif 00538 00539 /* C routines */ 00540 00541 mad_fixed_t mad_f_abs(mad_fixed_t); 00542 mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t); 00543 00544 # endif 00545 00546 /* Id: bit.h,v 1.12 2004/01/23 09:41:32 rob Exp */ 00547 00548 # ifndef LIBMAD_BIT_H 00549 # define LIBMAD_BIT_H 00550 00551 struct mad_bitptr { 00552 unsigned char const *byte; 00553 unsigned short cache; 00554 unsigned short left; 00555 }; 00556 00557 void mad_bit_init(struct mad_bitptr *, unsigned char const *); 00558 00559 # define mad_bit_finish(bitptr) /* nothing */ 00560 00561 unsigned int mad_bit_length(struct mad_bitptr const *, 00562 struct mad_bitptr const *); 00563 00564 # define mad_bit_bitsleft(bitptr) ((bitptr)->left) 00565 unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *); 00566 00567 void mad_bit_skip(struct mad_bitptr *, unsigned int); 00568 unsigned long mad_bit_read(struct mad_bitptr *, unsigned int); 00569 void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long); 00570 00571 unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short); 00572 00573 # endif 00574 00575 /* Id: timer.h,v 1.16 2004/01/23 09:41:33 rob Exp */ 00576 00577 # ifndef LIBMAD_TIMER_H 00578 # define LIBMAD_TIMER_H 00579 00580 typedef struct { 00581 signed long seconds; /* whole seconds */ 00582 unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */ 00583 } mad_timer_t; 00584 00585 extern mad_timer_t const mad_timer_zero; 00586 00587 # define MAD_TIMER_RESOLUTION 352800000UL 00588 00589 enum mad_units { 00590 MAD_UNITS_HOURS = -2, 00591 MAD_UNITS_MINUTES = -1, 00592 MAD_UNITS_SECONDS = 0, 00593 00594 /* metric units */ 00595 00596 MAD_UNITS_DECISECONDS = 10, 00597 MAD_UNITS_CENTISECONDS = 100, 00598 MAD_UNITS_MILLISECONDS = 1000, 00599 00600 /* audio sample units */ 00601 00602 MAD_UNITS_8000_HZ = 8000, 00603 MAD_UNITS_11025_HZ = 11025, 00604 MAD_UNITS_12000_HZ = 12000, 00605 00606 MAD_UNITS_16000_HZ = 16000, 00607 MAD_UNITS_22050_HZ = 22050, 00608 MAD_UNITS_24000_HZ = 24000, 00609 00610 MAD_UNITS_32000_HZ = 32000, 00611 MAD_UNITS_44100_HZ = 44100, 00612 MAD_UNITS_48000_HZ = 48000, 00613 00614 /* video frame/field units */ 00615 00616 MAD_UNITS_24_FPS = 24, 00617 MAD_UNITS_25_FPS = 25, 00618 MAD_UNITS_30_FPS = 30, 00619 MAD_UNITS_48_FPS = 48, 00620 MAD_UNITS_50_FPS = 50, 00621 MAD_UNITS_60_FPS = 60, 00622 00623 /* CD audio frames */ 00624 00625 MAD_UNITS_75_FPS = 75, 00626 00627 /* video drop-frame units */ 00628 00629 MAD_UNITS_23_976_FPS = -24, 00630 MAD_UNITS_24_975_FPS = -25, 00631 MAD_UNITS_29_97_FPS = -30, 00632 MAD_UNITS_47_952_FPS = -48, 00633 MAD_UNITS_49_95_FPS = -50, 00634 MAD_UNITS_59_94_FPS = -60 00635 }; 00636 00637 # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero)) 00638 00639 int mad_timer_compare(mad_timer_t, mad_timer_t); 00640 00641 # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero) 00642 00643 void mad_timer_negate(mad_timer_t *); 00644 mad_timer_t mad_timer_abs(mad_timer_t); 00645 00646 void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long); 00647 void mad_timer_add(mad_timer_t *, mad_timer_t); 00648 void mad_timer_multiply(mad_timer_t *, signed long); 00649 00650 signed long mad_timer_count(mad_timer_t, enum mad_units); 00651 unsigned long mad_timer_fraction(mad_timer_t, unsigned long); 00652 void mad_timer_string(mad_timer_t, char *, char const *, 00653 enum mad_units, enum mad_units, unsigned long); 00654 00655 # endif 00656 00657 /* Id: stream.h,v 1.20 2004/02/05 09:02:39 rob Exp */ 00658 00659 # ifndef LIBMAD_STREAM_H 00660 # define LIBMAD_STREAM_H 00661 00662 00663 # define MAD_BUFFER_GUARD 8 00664 # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD) 00665 00666 enum mad_error { 00667 MAD_ERROR_NONE = 0x0000, /* no error */ 00668 00669 MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */ 00670 MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */ 00671 00672 MAD_ERROR_NOMEM = 0x0031, /* not enough memory */ 00673 00674 MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */ 00675 MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */ 00676 MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */ 00677 MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */ 00678 MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */ 00679 00680 MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */ 00681 MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */ 00682 MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */ 00683 MAD_ERROR_BADMODE = 0x0222, /* bad bitrate/mode combination */ 00684 MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */ 00685 MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */ 00686 MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */ 00687 MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */ 00688 MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */ 00689 MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */ 00690 MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */ 00691 MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */ 00692 MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */ 00693 }; 00694 00695 # define MAD_RECOVERABLE(error) ((error) & 0xff00) 00696 00697 struct mad_stream { 00698 unsigned char const *buffer; /* input bitstream buffer */ 00699 unsigned char const *bufend; /* end of buffer */ 00700 unsigned long skiplen; /* bytes to skip before next frame */ 00701 00702 int sync; /* stream sync found */ 00703 unsigned long freerate; /* free bitrate (fixed) */ 00704 00705 unsigned char const *this_frame; /* start of current frame */ 00706 unsigned char const *next_frame; /* start of next frame */ 00707 struct mad_bitptr ptr; /* current processing bit pointer */ 00708 00709 struct mad_bitptr anc_ptr; /* ancillary bits pointer */ 00710 unsigned int anc_bitlen; /* number of ancillary bits */ 00711 00712 unsigned char (*main_data)[MAD_BUFFER_MDLEN]; 00713 /* Layer III main_data() */ 00714 unsigned int md_len; /* bytes in main_data */ 00715 00716 int options; /* decoding options (see below) */ 00717 enum mad_error error; /* error code (see above) */ 00718 }; 00719 00720 enum { 00721 MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */ 00722 MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */ 00723 # if 0 /* not yet implemented */ 00724 MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */ 00725 MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */ 00726 MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */ 00727 # endif 00728 }; 00729 00730 void mad_stream_init(struct mad_stream *); 00731 void mad_stream_finish(struct mad_stream *); 00732 00733 # define mad_stream_options(stream, opts) \ 00734 ((void) ((stream)->options = (opts))) 00735 00736 void mad_stream_buffer(struct mad_stream *, 00737 unsigned char const *, unsigned long); 00738 void mad_stream_skip(struct mad_stream *, unsigned long); 00739 00740 int mad_stream_sync(struct mad_stream *); 00741 00742 char const *mad_stream_errorstr(struct mad_stream const *); 00743 00744 # endif 00745 00746 /* Id: frame.h,v 1.20 2004/01/23 09:41:32 rob Exp */ 00747 00748 # ifndef LIBMAD_FRAME_H 00749 # define LIBMAD_FRAME_H 00750 00751 00752 enum mad_layer { 00753 MAD_LAYER_I = 1, /* Layer I */ 00754 MAD_LAYER_II = 2, /* Layer II */ 00755 MAD_LAYER_III = 3 /* Layer III */ 00756 }; 00757 00758 enum mad_mode { 00759 MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */ 00760 MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */ 00761 MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */ 00762 MAD_MODE_STEREO = 3 /* normal LR stereo */ 00763 }; 00764 00765 enum mad_emphasis { 00766 MAD_EMPHASIS_NONE = 0, /* no emphasis */ 00767 MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */ 00768 MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */ 00769 MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */ 00770 }; 00771 00772 struct mad_header { 00773 enum mad_layer layer; /* audio layer (1, 2, or 3) */ 00774 enum mad_mode mode; /* channel mode (see above) */ 00775 int mode_extension; /* additional mode info */ 00776 enum mad_emphasis emphasis; /* de-emphasis to use (see above) */ 00777 00778 unsigned long bitrate; /* stream bitrate (bps) */ 00779 unsigned int samplerate; /* sampling frequency (Hz) */ 00780 00781 unsigned short crc_check; /* frame CRC accumulator */ 00782 unsigned short crc_target; /* final target CRC checksum */ 00783 00784 int flags; /* flags (see below) */ 00785 int private_bits; /* private bits (see below) */ 00786 00787 mad_timer_t duration; /* audio playing time of frame */ 00788 }; 00789 00790 struct mad_frame { 00791 struct mad_header header; /* MPEG audio header */ 00792 00793 int options; /* decoding options (from stream) */ 00794 00795 mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */ 00796 mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */ 00797 }; 00798 00799 # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1) 00800 # define MAD_NSBSAMPLES(header) \ 00801 ((header)->layer == MAD_LAYER_I ? 12 : \ 00802 (((header)->layer == MAD_LAYER_III && \ 00803 ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36)) 00804 00805 enum { 00806 MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */ 00807 MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */ 00808 00809 MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */ 00810 MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */ 00811 MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */ 00812 MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */ 00813 00814 MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */ 00815 MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */ 00816 MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */ 00817 00818 MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */ 00819 MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */ 00820 MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */ 00821 }; 00822 00823 enum { 00824 MAD_PRIVATE_HEADER = 0x0100, /* header private bit */ 00825 MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */ 00826 }; 00827 00828 void mad_header_init(struct mad_header *); 00829 00830 # define mad_header_finish(header) /* nothing */ 00831 00832 int mad_header_decode(struct mad_header *, struct mad_stream *); 00833 00834 void mad_frame_init(struct mad_frame *); 00835 void mad_frame_finish(struct mad_frame *); 00836 00837 int mad_frame_decode(struct mad_frame *, struct mad_stream *); 00838 00839 void mad_frame_mute(struct mad_frame *); 00840 00841 # endif 00842 00843 /* Id: synth.h,v 1.15 2004/01/23 09:41:33 rob Exp */ 00844 00845 # ifndef LIBMAD_SYNTH_H 00846 # define LIBMAD_SYNTH_H 00847 00848 00849 struct mad_pcm { 00850 unsigned int samplerate; /* sampling frequency (Hz) */ 00851 unsigned short channels; /* number of channels */ 00852 unsigned short length; /* number of samples per channel */ 00853 mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */ 00854 }; 00855 00856 struct mad_synth { 00857 mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */ 00858 /* [ch][eo][peo][s][v] */ 00859 00860 unsigned int phase; /* current processing phase */ 00861 00862 struct mad_pcm pcm; /* PCM output */ 00863 }; 00864 00865 /* single channel PCM selector */ 00866 enum { 00867 MAD_PCM_CHANNEL_SINGLE = 0 00868 }; 00869 00870 /* dual channel PCM selector */ 00871 enum { 00872 MAD_PCM_CHANNEL_DUAL_1 = 0, 00873 MAD_PCM_CHANNEL_DUAL_2 = 1 00874 }; 00875 00876 /* stereo PCM selector */ 00877 enum { 00878 MAD_PCM_CHANNEL_STEREO_LEFT = 0, 00879 MAD_PCM_CHANNEL_STEREO_RIGHT = 1 00880 }; 00881 00882 void mad_synth_init(struct mad_synth *); 00883 00884 # define mad_synth_finish(synth) /* nothing */ 00885 00886 void mad_synth_mute(struct mad_synth *); 00887 00888 void mad_synth_frame(struct mad_synth *, struct mad_frame const *); 00889 00890 # endif 00891 00892 /* Id: decoder.h,v 1.17 2004/01/23 09:41:32 rob Exp */ 00893 00894 # ifndef LIBMAD_DECODER_H 00895 # define LIBMAD_DECODER_H 00896 00897 00898 enum mad_decoder_mode { 00899 MAD_DECODER_MODE_SYNC = 0, 00900 MAD_DECODER_MODE_ASYNC 00901 }; 00902 00903 enum mad_flow { 00904 MAD_FLOW_CONTINUE = 0x0000, /* continue normally */ 00905 MAD_FLOW_STOP = 0x0010, /* stop decoding normally */ 00906 MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */ 00907 MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */ 00908 }; 00909 00910 struct mad_sync_s { 00911 struct mad_stream stream; 00912 struct mad_frame frame; 00913 struct mad_synth *synth; 00914 }; 00915 00916 struct mad_decoder { 00917 enum mad_decoder_mode mode; 00918 00919 int options; 00920 00921 struct { 00922 long pid; 00923 int in; 00924 int out; 00925 } async; 00926 00927 struct mad_sync_s *sync; 00928 00929 void *cb_data; 00930 00931 enum mad_flow (*input_func)(void *, struct mad_stream *); 00932 enum mad_flow (*header_func)(void *, struct mad_header const *); 00933 enum mad_flow (*filter_func)(void *, 00934 struct mad_stream const *, struct mad_frame *); 00935 enum mad_flow (*output_func)(void *, 00936 struct mad_header const *, struct mad_pcm *); 00937 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *); 00938 enum mad_flow (*message_func)(void *, void *, unsigned int *); 00939 }; 00940 00941 void mad_decoder_init(struct mad_decoder *, void *, 00942 enum mad_flow (*)(void *, struct mad_stream *), 00943 enum mad_flow (*)(void *, struct mad_header const *), 00944 enum mad_flow (*)(void *, 00945 struct mad_stream const *, 00946 struct mad_frame *), 00947 enum mad_flow (*)(void *, 00948 struct mad_header const *, 00949 struct mad_pcm *), 00950 enum mad_flow (*)(void *, 00951 struct mad_stream *, 00952 struct mad_frame *), 00953 enum mad_flow (*)(void *, void *, unsigned int *)); 00954 int mad_decoder_finish(struct mad_decoder *); 00955 00956 # define mad_decoder_options(decoder, opts) \ 00957 ((void) ((decoder)->options = (opts))) 00958 00959 int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode); 00960 int mad_decoder_message(struct mad_decoder *, void *, unsigned int *); 00961 00962 # endif 00963 00964 # ifdef __cplusplus 00965 } 00966 # endif
Generated on Tue Jul 12 2022 23:11:09 by
1.7.2