mad.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * If you would like to negotiate alternate licensing terms, you may do
  20. * so by contacting: Underbit Technologies, Inc. <info@underbit.com>
  21. */
  22. # ifdef __cplusplus
  23. extern "C" {
  24. # endif
  25. # define FPM_DEFAULT
  26. # define SIZEOF_INT 4
  27. # define SIZEOF_LONG 4
  28. # define SIZEOF_LONG_LONG 8
  29. /* Id: version.h,v 1.26 2004/01/23 09:41:33 rob Exp */
  30. # ifndef LIBMAD_VERSION_H
  31. # define LIBMAD_VERSION_H
  32. # define MAD_VERSION_MAJOR 0
  33. # define MAD_VERSION_MINOR 15
  34. # define MAD_VERSION_PATCH 1
  35. # define MAD_VERSION_EXTRA " (beta)"
  36. # define MAD_VERSION_STRINGIZE(str) #str
  37. # define MAD_VERSION_STRING(num) MAD_VERSION_STRINGIZE(num)
  38. # define MAD_VERSION MAD_VERSION_STRING(MAD_VERSION_MAJOR) "." \
  39. MAD_VERSION_STRING(MAD_VERSION_MINOR) "." \
  40. MAD_VERSION_STRING(MAD_VERSION_PATCH) \
  41. MAD_VERSION_EXTRA
  42. # define MAD_PUBLISHYEAR "2000-2004"
  43. # define MAD_AUTHOR "Underbit Technologies, Inc."
  44. # define MAD_EMAIL "info@underbit.com"
  45. extern char const mad_version[];
  46. extern char const mad_copyright[];
  47. extern char const mad_author[];
  48. extern char const mad_build[];
  49. # endif
  50. /* Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp */
  51. # ifndef LIBMAD_FIXED_H
  52. # define LIBMAD_FIXED_H
  53. # if SIZEOF_INT >= 4
  54. typedef signed int mad_fixed_t;
  55. typedef signed int mad_fixed64hi_t;
  56. typedef unsigned int mad_fixed64lo_t;
  57. # else
  58. typedef signed long mad_fixed_t;
  59. typedef signed long mad_fixed64hi_t;
  60. typedef unsigned long mad_fixed64lo_t;
  61. # endif
  62. # if defined(_MSC_VER)
  63. # define mad_fixed64_t signed __int64
  64. # elif 1 || defined(__GNUC__)
  65. # define mad_fixed64_t signed long long
  66. # endif
  67. # if defined(FPM_FLOAT)
  68. typedef double mad_sample_t;
  69. # else
  70. typedef mad_fixed_t mad_sample_t;
  71. # endif
  72. /*
  73. * Fixed-point format: 0xABBBBBBB
  74. * A == whole part (sign + 3 bits)
  75. * B == fractional part (28 bits)
  76. *
  77. * Values are signed two's complement, so the effective range is:
  78. * 0x80000000 to 0x7fffffff
  79. * -8.0 to +7.9999999962747097015380859375
  80. *
  81. * The smallest representable value is:
  82. * 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
  83. *
  84. * 28 bits of fractional accuracy represent about
  85. * 8.6 digits of decimal accuracy.
  86. *
  87. * Fixed-point numbers can be added or subtracted as normal
  88. * integers, but multiplication requires shifting the 64-bit result
  89. * from 56 fractional bits back to 28 (and rounding.)
  90. *
  91. * Changing the definition of MAD_F_FRACBITS is only partially
  92. * supported, and must be done with care.
  93. */
  94. # define MAD_F_FRACBITS 28
  95. # if MAD_F_FRACBITS == 28
  96. # define MAD_F(x) ((mad_fixed_t) (x##L))
  97. # else
  98. # if MAD_F_FRACBITS < 28
  99. # warning "MAD_F_FRACBITS < 28"
  100. # define MAD_F(x) ((mad_fixed_t) \
  101. (((x##L) + \
  102. (1L << (28 - MAD_F_FRACBITS - 1))) >> \
  103. (28 - MAD_F_FRACBITS)))
  104. # elif MAD_F_FRACBITS > 28
  105. # error "MAD_F_FRACBITS > 28 not currently supported"
  106. # define MAD_F(x) ((mad_fixed_t) \
  107. ((x##L) << (MAD_F_FRACBITS - 28)))
  108. # endif
  109. # endif
  110. # define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
  111. # define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
  112. # define MAD_F_ONE MAD_F(0x10000000)
  113. # define mad_f_tofixed(x) ((mad_fixed_t) \
  114. ((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
  115. # define mad_f_todouble(x) ((double) \
  116. ((x) / (double) (1L << MAD_F_FRACBITS)))
  117. # define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
  118. # define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
  119. /* (x should be positive) */
  120. # define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
  121. # define mad_f_add(x, y) ((x) + (y))
  122. # define mad_f_sub(x, y) ((x) - (y))
  123. # if defined(FPM_FLOAT)
  124. # error "FPM_FLOAT not yet supported"
  125. # undef MAD_F
  126. # define MAD_F(x) mad_f_todouble(x)
  127. # define mad_f_mul(x, y) ((x) * (y))
  128. # define mad_f_scale64
  129. # undef ASO_ZEROCHECK
  130. # elif defined(FPM_64BIT)
  131. /*
  132. * This version should be the most accurate if 64-bit types are supported by
  133. * the compiler, although it may not be the most efficient.
  134. */
  135. # if defined(OPT_ACCURACY)
  136. # define mad_f_mul(x, y) \
  137. ((mad_fixed_t) \
  138. ((((mad_fixed64_t) (x) * (y)) + \
  139. (1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
  140. # else
  141. # define mad_f_mul(x, y) \
  142. ((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
  143. # endif
  144. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  145. /* --- Intel --------------------------------------------------------------- */
  146. # elif defined(FPM_INTEL)
  147. # if defined(_MSC_VER)
  148. # pragma warning(push)
  149. # pragma warning(disable: 4035) /* no return value */
  150. static __forceinline
  151. mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
  152. {
  153. enum {
  154. fracbits = MAD_F_FRACBITS
  155. };
  156. __asm {
  157. mov eax, x
  158. imul y
  159. shrd eax, edx, fracbits
  160. }
  161. /* implicit return of eax */
  162. }
  163. # pragma warning(pop)
  164. # define mad_f_mul mad_f_mul_inline
  165. # define mad_f_scale64
  166. # else
  167. /*
  168. * This Intel version is fast and accurate; the disposition of the least
  169. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  170. */
  171. # define MAD_F_MLX(hi, lo, x, y) \
  172. asm ("imull %3" \
  173. : "=a" (lo), "=d" (hi) \
  174. : "%a" (x), "rm" (y) \
  175. : "cc")
  176. # if defined(OPT_ACCURACY)
  177. /*
  178. * This gives best accuracy but is not very fast.
  179. */
  180. # define MAD_F_MLA(hi, lo, x, y) \
  181. ({ mad_fixed64hi_t __hi; \
  182. mad_fixed64lo_t __lo; \
  183. MAD_F_MLX(__hi, __lo, (x), (y)); \
  184. asm ("addl %2,%0\n\t" \
  185. "adcl %3,%1" \
  186. : "=rm" (lo), "=rm" (hi) \
  187. : "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
  188. : "cc"); \
  189. })
  190. # endif /* OPT_ACCURACY */
  191. # if defined(OPT_ACCURACY)
  192. /*
  193. * Surprisingly, this is faster than SHRD followed by ADC.
  194. */
  195. # define mad_f_scale64(hi, lo) \
  196. ({ mad_fixed64hi_t __hi_; \
  197. mad_fixed64lo_t __lo_; \
  198. mad_fixed_t __result; \
  199. asm ("addl %4,%2\n\t" \
  200. "adcl %5,%3" \
  201. : "=rm" (__lo_), "=rm" (__hi_) \
  202. : "0" (lo), "1" (hi), \
  203. "ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
  204. : "cc"); \
  205. asm ("shrdl %3,%2,%1" \
  206. : "=rm" (__result) \
  207. : "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
  208. : "cc"); \
  209. __result; \
  210. })
  211. # elif defined(OPT_INTEL)
  212. /*
  213. * Alternate Intel scaling that may or may not perform better.
  214. */
  215. # define mad_f_scale64(hi, lo) \
  216. ({ mad_fixed_t __result; \
  217. asm ("shrl %3,%1\n\t" \
  218. "shll %4,%2\n\t" \
  219. "orl %2,%1" \
  220. : "=rm" (__result) \
  221. : "0" (lo), "r" (hi), \
  222. "I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
  223. : "cc"); \
  224. __result; \
  225. })
  226. # else
  227. # define mad_f_scale64(hi, lo) \
  228. ({ mad_fixed_t __result; \
  229. asm ("shrdl %3,%2,%1" \
  230. : "=rm" (__result) \
  231. : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
  232. : "cc"); \
  233. __result; \
  234. })
  235. # endif /* OPT_ACCURACY */
  236. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  237. # endif
  238. /* --- ARM ----------------------------------------------------------------- */
  239. # elif defined(FPM_ARM)
  240. /*
  241. * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
  242. * least significant bit is properly rounded at no CPU cycle cost!
  243. */
  244. # if 1
  245. /*
  246. * This is faster than the default implementation via MAD_F_MLX() and
  247. * mad_f_scale64().
  248. */
  249. # define mad_f_mul(x, y) \
  250. ({ mad_fixed64hi_t __hi; \
  251. mad_fixed64lo_t __lo; \
  252. mad_fixed_t __result; \
  253. asm ("smull %0, %1, %3, %4\n\t" \
  254. "movs %0, %0, lsr %5\n\t" \
  255. "adc %2, %0, %1, lsl %6" \
  256. : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
  257. : "%r" (x), "r" (y), \
  258. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  259. : "cc"); \
  260. __result; \
  261. })
  262. # endif
  263. # define MAD_F_MLX(hi, lo, x, y) \
  264. asm ("smull %0, %1, %2, %3" \
  265. : "=&r" (lo), "=&r" (hi) \
  266. : "%r" (x), "r" (y))
  267. # define MAD_F_MLA(hi, lo, x, y) \
  268. asm ("smlal %0, %1, %2, %3" \
  269. : "+r" (lo), "+r" (hi) \
  270. : "%r" (x), "r" (y))
  271. # define MAD_F_MLN(hi, lo) \
  272. asm ("rsbs %0, %2, #0\n\t" \
  273. "rsc %1, %3, #0" \
  274. : "=r" (lo), "=r" (hi) \
  275. : "0" (lo), "1" (hi) \
  276. : "cc")
  277. # define mad_f_scale64(hi, lo) \
  278. ({ mad_fixed_t __result; \
  279. asm ("movs %0, %1, lsr %3\n\t" \
  280. "adc %0, %0, %2, lsl %4" \
  281. : "=&r" (__result) \
  282. : "r" (lo), "r" (hi), \
  283. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  284. : "cc"); \
  285. __result; \
  286. })
  287. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  288. /* --- MIPS ---------------------------------------------------------------- */
  289. # elif defined(FPM_MIPS)
  290. /*
  291. * This MIPS version is fast and accurate; the disposition of the least
  292. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  293. */
  294. # define MAD_F_MLX(hi, lo, x, y) \
  295. asm ("mult %2,%3" \
  296. : "=l" (lo), "=h" (hi) \
  297. : "%r" (x), "r" (y))
  298. # if defined(HAVE_MADD_ASM)
  299. # define MAD_F_MLA(hi, lo, x, y) \
  300. asm ("madd %2,%3" \
  301. : "+l" (lo), "+h" (hi) \
  302. : "%r" (x), "r" (y))
  303. # elif defined(HAVE_MADD16_ASM)
  304. /*
  305. * This loses significant accuracy due to the 16-bit integer limit in the
  306. * multiply/accumulate instruction.
  307. */
  308. # define MAD_F_ML0(hi, lo, x, y) \
  309. asm ("mult %2,%3" \
  310. : "=l" (lo), "=h" (hi) \
  311. : "%r" ((x) >> 12), "r" ((y) >> 16))
  312. # define MAD_F_MLA(hi, lo, x, y) \
  313. asm ("madd16 %2,%3" \
  314. : "+l" (lo), "+h" (hi) \
  315. : "%r" ((x) >> 12), "r" ((y) >> 16))
  316. # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
  317. # endif
  318. # if defined(OPT_SPEED)
  319. # define mad_f_scale64(hi, lo) \
  320. ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
  321. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  322. # endif
  323. /* --- SPARC --------------------------------------------------------------- */
  324. # elif defined(FPM_SPARC)
  325. /*
  326. * This SPARC V8 version is fast and accurate; the disposition of the least
  327. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  328. */
  329. # define MAD_F_MLX(hi, lo, x, y) \
  330. asm ("smul %2, %3, %0\n\t" \
  331. "rd %%y, %1" \
  332. : "=r" (lo), "=r" (hi) \
  333. : "%r" (x), "rI" (y))
  334. /* --- PowerPC ------------------------------------------------------------- */
  335. # elif defined(FPM_PPC)
  336. /*
  337. * This PowerPC version is fast and accurate; the disposition of the least
  338. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  339. */
  340. # define MAD_F_MLX(hi, lo, x, y) \
  341. do { \
  342. asm ("mullw %0,%1,%2" \
  343. : "=r" (lo) \
  344. : "%r" (x), "r" (y)); \
  345. asm ("mulhw %0,%1,%2" \
  346. : "=r" (hi) \
  347. : "%r" (x), "r" (y)); \
  348. } \
  349. while (0)
  350. # if defined(OPT_ACCURACY)
  351. /*
  352. * This gives best accuracy but is not very fast.
  353. */
  354. # define MAD_F_MLA(hi, lo, x, y) \
  355. ({ mad_fixed64hi_t __hi; \
  356. mad_fixed64lo_t __lo; \
  357. MAD_F_MLX(__hi, __lo, (x), (y)); \
  358. asm ("addc %0,%2,%3\n\t" \
  359. "adde %1,%4,%5" \
  360. : "=r" (lo), "=r" (hi) \
  361. : "%r" (lo), "r" (__lo), \
  362. "%r" (hi), "r" (__hi) \
  363. : "xer"); \
  364. })
  365. # endif
  366. # if defined(OPT_ACCURACY)
  367. /*
  368. * This is slower than the truncating version below it.
  369. */
  370. # define mad_f_scale64(hi, lo) \
  371. ({ mad_fixed_t __result, __round; \
  372. asm ("rotrwi %0,%1,%2" \
  373. : "=r" (__result) \
  374. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  375. asm ("extrwi %0,%1,1,0" \
  376. : "=r" (__round) \
  377. : "r" (__result)); \
  378. asm ("insrwi %0,%1,%2,0" \
  379. : "+r" (__result) \
  380. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  381. asm ("add %0,%1,%2" \
  382. : "=r" (__result) \
  383. : "%r" (__result), "r" (__round)); \
  384. __result; \
  385. })
  386. # else
  387. # define mad_f_scale64(hi, lo) \
  388. ({ mad_fixed_t __result; \
  389. asm ("rotrwi %0,%1,%2" \
  390. : "=r" (__result) \
  391. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  392. asm ("insrwi %0,%1,%2,0" \
  393. : "+r" (__result) \
  394. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  395. __result; \
  396. })
  397. # endif
  398. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  399. /* --- Default ------------------------------------------------------------- */
  400. # elif defined(FPM_DEFAULT)
  401. /*
  402. * This version is the most portable but it loses significant accuracy.
  403. * Furthermore, accuracy is biased against the second argument, so care
  404. * should be taken when ordering operands.
  405. *
  406. * The scale factors are constant as this is not used with SSO.
  407. *
  408. * Pre-rounding is required to stay within the limits of compliance.
  409. */
  410. # if defined(OPT_SPEED)
  411. # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
  412. # else
  413. # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
  414. (((y) + (1L << 15)) >> 16))
  415. # endif
  416. /* ------------------------------------------------------------------------- */
  417. # else
  418. # error "no FPM selected"
  419. # endif
  420. /* default implementations */
  421. # if !defined(mad_f_mul)
  422. # define mad_f_mul(x, y) \
  423. ({ register mad_fixed64hi_t __hi; \
  424. register mad_fixed64lo_t __lo; \
  425. MAD_F_MLX(__hi, __lo, (x), (y)); \
  426. mad_f_scale64(__hi, __lo); \
  427. })
  428. # endif
  429. # if !defined(MAD_F_MLA)
  430. # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
  431. # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
  432. # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
  433. # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
  434. # endif
  435. # if !defined(MAD_F_ML0)
  436. # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
  437. # endif
  438. # if !defined(MAD_F_MLN)
  439. # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
  440. # endif
  441. # if !defined(MAD_F_MLZ)
  442. # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
  443. # endif
  444. # if !defined(mad_f_scale64)
  445. # if defined(OPT_ACCURACY)
  446. # define mad_f_scale64(hi, lo) \
  447. ((((mad_fixed_t) \
  448. (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
  449. ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
  450. # else
  451. # define mad_f_scale64(hi, lo) \
  452. ((mad_fixed_t) \
  453. (((hi) << (32 - MAD_F_SCALEBITS)) | \
  454. ((lo) >> MAD_F_SCALEBITS)))
  455. # endif
  456. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  457. # endif
  458. /* C routines */
  459. mad_fixed_t mad_f_abs(mad_fixed_t);
  460. mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
  461. # endif
  462. /* Id: bit.h,v 1.12 2004/01/23 09:41:32 rob Exp */
  463. # ifndef LIBMAD_BIT_H
  464. # define LIBMAD_BIT_H
  465. struct mad_bitptr {
  466. unsigned char const *byte;
  467. unsigned short cache;
  468. unsigned short left;
  469. };
  470. void mad_bit_init(struct mad_bitptr *, unsigned char const *);
  471. # define mad_bit_finish(bitptr) /* nothing */
  472. unsigned int mad_bit_length(struct mad_bitptr const *,
  473. struct mad_bitptr const *);
  474. # define mad_bit_bitsleft(bitptr) ((bitptr)->left)
  475. unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *);
  476. void mad_bit_skip(struct mad_bitptr *, unsigned int);
  477. unsigned long mad_bit_read(struct mad_bitptr *, unsigned int);
  478. void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long);
  479. unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short);
  480. # endif
  481. /* Id: timer.h,v 1.16 2004/01/23 09:41:33 rob Exp */
  482. # ifndef LIBMAD_TIMER_H
  483. # define LIBMAD_TIMER_H
  484. typedef struct {
  485. signed long seconds; /* whole seconds */
  486. unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */
  487. } mad_timer_t;
  488. extern mad_timer_t const mad_timer_zero;
  489. # define MAD_TIMER_RESOLUTION 352800000UL
  490. enum mad_units {
  491. MAD_UNITS_HOURS = -2,
  492. MAD_UNITS_MINUTES = -1,
  493. MAD_UNITS_SECONDS = 0,
  494. /* metric units */
  495. MAD_UNITS_DECISECONDS = 10,
  496. MAD_UNITS_CENTISECONDS = 100,
  497. MAD_UNITS_MILLISECONDS = 1000,
  498. /* audio sample units */
  499. MAD_UNITS_8000_HZ = 8000,
  500. MAD_UNITS_11025_HZ = 11025,
  501. MAD_UNITS_12000_HZ = 12000,
  502. MAD_UNITS_16000_HZ = 16000,
  503. MAD_UNITS_22050_HZ = 22050,
  504. MAD_UNITS_24000_HZ = 24000,
  505. MAD_UNITS_32000_HZ = 32000,
  506. MAD_UNITS_44100_HZ = 44100,
  507. MAD_UNITS_48000_HZ = 48000,
  508. /* video frame/field units */
  509. MAD_UNITS_24_FPS = 24,
  510. MAD_UNITS_25_FPS = 25,
  511. MAD_UNITS_30_FPS = 30,
  512. MAD_UNITS_48_FPS = 48,
  513. MAD_UNITS_50_FPS = 50,
  514. MAD_UNITS_60_FPS = 60,
  515. /* CD audio frames */
  516. MAD_UNITS_75_FPS = 75,
  517. /* video drop-frame units */
  518. MAD_UNITS_23_976_FPS = -24,
  519. MAD_UNITS_24_975_FPS = -25,
  520. MAD_UNITS_29_97_FPS = -30,
  521. MAD_UNITS_47_952_FPS = -48,
  522. MAD_UNITS_49_95_FPS = -50,
  523. MAD_UNITS_59_94_FPS = -60
  524. };
  525. # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero))
  526. int mad_timer_compare(mad_timer_t, mad_timer_t);
  527. # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero)
  528. void mad_timer_negate(mad_timer_t *);
  529. mad_timer_t mad_timer_abs(mad_timer_t);
  530. void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long);
  531. void mad_timer_add(mad_timer_t *, mad_timer_t);
  532. void mad_timer_multiply(mad_timer_t *, signed long);
  533. signed long mad_timer_count(mad_timer_t, enum mad_units);
  534. unsigned long mad_timer_fraction(mad_timer_t, unsigned long);
  535. void mad_timer_string(mad_timer_t, char *, char const *,
  536. enum mad_units, enum mad_units, unsigned long);
  537. # endif
  538. /* Id: stream.h,v 1.20 2004/02/05 09:02:39 rob Exp */
  539. # ifndef LIBMAD_STREAM_H
  540. # define LIBMAD_STREAM_H
  541. # define MAD_BUFFER_GUARD 8
  542. # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD)
  543. enum mad_error {
  544. MAD_ERROR_NONE = 0x0000, /* no error */
  545. MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */
  546. MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */
  547. MAD_ERROR_NOMEM = 0x0031, /* not enough memory */
  548. MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */
  549. MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */
  550. MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */
  551. MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */
  552. MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */
  553. MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */
  554. MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */
  555. MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */
  556. MAD_ERROR_BADMODE = 0x0222, /* bad bitrate/mode combination */
  557. MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */
  558. MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */
  559. MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */
  560. MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */
  561. MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */
  562. MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */
  563. MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */
  564. MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */
  565. MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */
  566. };
  567. # define MAD_RECOVERABLE(error) ((error) & 0xff00)
  568. struct mad_stream {
  569. unsigned char const *buffer; /* input bitstream buffer */
  570. unsigned char const *bufend; /* end of buffer */
  571. unsigned long skiplen; /* bytes to skip before next frame */
  572. int sync; /* stream sync found */
  573. unsigned long freerate; /* free bitrate (fixed) */
  574. unsigned char const *this_frame; /* start of current frame */
  575. unsigned char const *next_frame; /* start of next frame */
  576. struct mad_bitptr ptr; /* current processing bit pointer */
  577. struct mad_bitptr anc_ptr; /* ancillary bits pointer */
  578. unsigned int anc_bitlen; /* number of ancillary bits */
  579. unsigned char main_data[MAD_BUFFER_MDLEN];
  580. /* Layer III main_data() */
  581. unsigned int md_len; /* bytes in main_data */
  582. int options; /* decoding options (see below) */
  583. enum mad_error error; /* error code (see above) */
  584. };
  585. enum {
  586. MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */
  587. MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */
  588. # if 0 /* not yet implemented */
  589. MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */
  590. MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */
  591. MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */
  592. # endif
  593. };
  594. void mad_stream_init(struct mad_stream *);
  595. void mad_stream_finish(struct mad_stream *);
  596. # define mad_stream_options(stream, opts) \
  597. ((void) ((stream)->options = (opts)))
  598. void mad_stream_buffer(struct mad_stream *,
  599. unsigned char const *, unsigned long);
  600. void mad_stream_skip(struct mad_stream *, unsigned long);
  601. int mad_stream_sync(struct mad_stream *);
  602. char const *mad_stream_errorstr(struct mad_stream const *);
  603. # endif
  604. /* Id: frame.h,v 1.20 2004/01/23 09:41:32 rob Exp */
  605. # ifndef LIBMAD_FRAME_H
  606. # define LIBMAD_FRAME_H
  607. enum mad_layer {
  608. MAD_LAYER_I = 1, /* Layer I */
  609. MAD_LAYER_II = 2, /* Layer II */
  610. MAD_LAYER_III = 3 /* Layer III */
  611. };
  612. enum mad_mode {
  613. MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */
  614. MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */
  615. MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */
  616. MAD_MODE_STEREO = 3 /* normal LR stereo */
  617. };
  618. enum mad_emphasis {
  619. MAD_EMPHASIS_NONE = 0, /* no emphasis */
  620. MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */
  621. MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */
  622. MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */
  623. };
  624. struct mad_header {
  625. enum mad_layer layer; /* audio layer (1, 2, or 3) */
  626. enum mad_mode mode; /* channel mode (see above) */
  627. int mode_extension; /* additional mode info */
  628. enum mad_emphasis emphasis; /* de-emphasis to use (see above) */
  629. unsigned long bitrate; /* stream bitrate (bps) */
  630. unsigned int samplerate; /* sampling frequency (Hz) */
  631. unsigned short crc_check; /* frame CRC accumulator */
  632. unsigned short crc_target; /* final target CRC checksum */
  633. int flags; /* flags (see below) */
  634. int private_bits; /* private bits (see below) */
  635. mad_timer_t duration; /* audio playing time of frame */
  636. };
  637. struct mad_frame {
  638. struct mad_header header; /* MPEG audio header */
  639. int options; /* decoding options (from stream) */
  640. mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */
  641. mad_fixed_t overlap[2][32][18]; /* Layer III block overlap data */
  642. mad_fixed_t xr[2][576];
  643. mad_fixed_t tmp[32][3][6];
  644. };
  645. # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1)
  646. # define MAD_NSBSAMPLES(header) \
  647. ((header)->layer == MAD_LAYER_I ? 12 : \
  648. (((header)->layer == MAD_LAYER_III && \
  649. ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36))
  650. enum {
  651. MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */
  652. MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */
  653. MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */
  654. MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */
  655. MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */
  656. MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */
  657. MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */
  658. MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */
  659. MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */
  660. MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */
  661. MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */
  662. MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */
  663. };
  664. enum {
  665. MAD_PRIVATE_HEADER = 0x0100, /* header private bit */
  666. MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */
  667. };
  668. void mad_header_init(struct mad_header *);
  669. # define mad_header_finish(header) /* nothing */
  670. int mad_header_decode(struct mad_header *, struct mad_stream *);
  671. void mad_frame_init(struct mad_frame *);
  672. void mad_frame_finish(struct mad_frame *);
  673. int mad_frame_decode(struct mad_frame *, struct mad_stream *);
  674. void mad_frame_mute(struct mad_frame *);
  675. # endif
  676. /* Id: synth.h,v 1.15 2004/01/23 09:41:33 rob Exp */
  677. # ifndef LIBMAD_SYNTH_H
  678. # define LIBMAD_SYNTH_H
  679. struct mad_pcm {
  680. unsigned int samplerate; /* sampling frequency (Hz) */
  681. unsigned short channels; /* number of channels */
  682. unsigned short length; /* number of samples per channel */
  683. mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */
  684. };
  685. struct mad_synth {
  686. mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */
  687. /* [ch][eo][peo][s][v] */
  688. unsigned int phase; /* current processing phase */
  689. struct mad_pcm pcm; /* PCM output */
  690. };
  691. /* single channel PCM selector */
  692. enum {
  693. MAD_PCM_CHANNEL_SINGLE = 0
  694. };
  695. /* dual channel PCM selector */
  696. enum {
  697. MAD_PCM_CHANNEL_DUAL_1 = 0,
  698. MAD_PCM_CHANNEL_DUAL_2 = 1
  699. };
  700. /* stereo PCM selector */
  701. enum {
  702. MAD_PCM_CHANNEL_STEREO_LEFT = 0,
  703. MAD_PCM_CHANNEL_STEREO_RIGHT = 1
  704. };
  705. void mad_synth_init(struct mad_synth *);
  706. # define mad_synth_finish(synth) /* nothing */
  707. void mad_synth_mute(struct mad_synth *);
  708. void mad_synth_frame(struct mad_synth *, struct mad_frame const *);
  709. # endif
  710. /* Id: decoder.h,v 1.17 2004/01/23 09:41:32 rob Exp */
  711. # ifndef LIBMAD_DECODER_H
  712. # define LIBMAD_DECODER_H
  713. enum mad_decoder_mode {
  714. MAD_DECODER_MODE_SYNC = 0,
  715. MAD_DECODER_MODE_ASYNC
  716. };
  717. enum mad_flow {
  718. MAD_FLOW_CONTINUE = 0x0000, /* continue normally */
  719. MAD_FLOW_STOP = 0x0010, /* stop decoding normally */
  720. MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */
  721. MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */
  722. };
  723. struct mad_decoder {
  724. enum mad_decoder_mode mode;
  725. int options;
  726. struct {
  727. long pid;
  728. int in;
  729. int out;
  730. } async;
  731. struct {
  732. struct mad_stream stream;
  733. struct mad_frame frame;
  734. struct mad_synth synth;
  735. } *sync;
  736. void *cb_data;
  737. enum mad_flow (*input_func)(void *, struct mad_stream *);
  738. enum mad_flow (*header_func)(void *, struct mad_header const *);
  739. enum mad_flow (*filter_func)(void *,
  740. struct mad_stream const *, struct mad_frame *);
  741. enum mad_flow (*output_func)(void *,
  742. struct mad_header const *, struct mad_pcm *);
  743. enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
  744. enum mad_flow (*message_func)(void *, void *, unsigned int *);
  745. };
  746. void mad_decoder_init(struct mad_decoder *, void *,
  747. enum mad_flow (*)(void *, struct mad_stream *),
  748. enum mad_flow (*)(void *, struct mad_header const *),
  749. enum mad_flow (*)(void *,
  750. struct mad_stream const *,
  751. struct mad_frame *),
  752. enum mad_flow (*)(void *,
  753. struct mad_header const *,
  754. struct mad_pcm *),
  755. enum mad_flow (*)(void *,
  756. struct mad_stream *,
  757. struct mad_frame *),
  758. enum mad_flow (*)(void *, void *, unsigned int *));
  759. int mad_decoder_finish(struct mad_decoder *);
  760. # define mad_decoder_options(decoder, opts) \
  761. ((void) ((decoder)->options = (opts)))
  762. int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode);
  763. int mad_decoder_message(struct mad_decoder *, void *, unsigned int *);
  764. # endif
  765. # ifdef __cplusplus
  766. }
  767. # endif