mad.h 27 KB

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