Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

948 lines
26 KiB

  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_INTEL
  26. # define SIZEOF_INT 4
  27. # define SIZEOF_LONG 4
  28. # define SIZEOF_LONG_LONG 8
  29. /* Id: version.h,v 1.24 2003/05/27 22:40:37 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-2003"
  43. # define MAD_AUTHOR "Underbit Technologies, Inc."
  44. # define MAD_EMAIL "[email protected]"
  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.36 2003/05/28 04:36:00 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. # else
  212. # define mad_f_scale64(hi, lo) \
  213. ({ mad_fixed_t __result; \
  214. asm ("shrdl %3,%2,%1" \
  215. : "=rm" (__result) \
  216. : "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
  217. : "cc"); \
  218. __result; \
  219. })
  220. # endif /* OPT_ACCURACY */
  221. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  222. # endif
  223. /* --- ARM ----------------------------------------------------------------- */
  224. # elif defined(FPM_ARM)
  225. /*
  226. * This ARM V4 version is as accurate as FPM_64BIT but much faster. The
  227. * least significant bit is properly rounded at no CPU cycle cost!
  228. */
  229. # if 1
  230. /*
  231. * This is faster than the default implementation via MAD_F_MLX() and
  232. * mad_f_scale64().
  233. */
  234. # define mad_f_mul(x, y) \
  235. ({ mad_fixed64hi_t __hi; \
  236. mad_fixed64lo_t __lo; \
  237. mad_fixed_t __result; \
  238. asm ("smull %0, %1, %3, %4\n\t" \
  239. "movs %0, %0, lsr %5\n\t" \
  240. "adc %2, %0, %1, lsl %6" \
  241. : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
  242. : "%r" (x), "r" (y), \
  243. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  244. : "cc"); \
  245. __result; \
  246. })
  247. # endif
  248. # define MAD_F_MLX(hi, lo, x, y) \
  249. asm ("smull %0, %1, %2, %3" \
  250. : "=&r" (lo), "=&r" (hi) \
  251. : "%r" (x), "r" (y))
  252. # define MAD_F_MLA(hi, lo, x, y) \
  253. asm ("smlal %0, %1, %2, %3" \
  254. : "+r" (lo), "+r" (hi) \
  255. : "%r" (x), "r" (y))
  256. # define MAD_F_MLN(hi, lo) \
  257. asm ("rsbs %0, %2, #0\n\t" \
  258. "rsc %1, %3, #0" \
  259. : "=r" (lo), "=r" (hi) \
  260. : "0" (lo), "1" (hi) \
  261. : "cc")
  262. # define mad_f_scale64(hi, lo) \
  263. ({ mad_fixed_t __result; \
  264. asm ("movs %0, %1, lsr %3\n\t" \
  265. "adc %0, %0, %2, lsl %4" \
  266. : "=&r" (__result) \
  267. : "r" (lo), "r" (hi), \
  268. "M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
  269. : "cc"); \
  270. __result; \
  271. })
  272. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  273. /* --- MIPS ---------------------------------------------------------------- */
  274. # elif defined(FPM_MIPS)
  275. /*
  276. * This MIPS version is fast and accurate; the disposition of the least
  277. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  278. */
  279. # define MAD_F_MLX(hi, lo, x, y) \
  280. asm ("mult %2,%3" \
  281. : "=l" (lo), "=h" (hi) \
  282. : "%r" (x), "r" (y))
  283. # if defined(HAVE_MADD_ASM)
  284. # define MAD_F_MLA(hi, lo, x, y) \
  285. asm ("madd %2,%3" \
  286. : "+l" (lo), "+h" (hi) \
  287. : "%r" (x), "r" (y))
  288. # elif defined(HAVE_MADD16_ASM)
  289. /*
  290. * This loses significant accuracy due to the 16-bit integer limit in the
  291. * multiply/accumulate instruction.
  292. */
  293. # define MAD_F_ML0(hi, lo, x, y) \
  294. asm ("mult %2,%3" \
  295. : "=l" (lo), "=h" (hi) \
  296. : "%r" ((x) >> 12), "r" ((y) >> 16))
  297. # define MAD_F_MLA(hi, lo, x, y) \
  298. asm ("madd16 %2,%3" \
  299. : "+l" (lo), "+h" (hi) \
  300. : "%r" ((x) >> 12), "r" ((y) >> 16))
  301. # define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
  302. # endif
  303. # if defined(OPT_SPEED)
  304. # define mad_f_scale64(hi, lo) \
  305. ((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
  306. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  307. # endif
  308. /* --- SPARC --------------------------------------------------------------- */
  309. # elif defined(FPM_SPARC)
  310. /*
  311. * This SPARC V8 version is fast and accurate; the disposition of the least
  312. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  313. */
  314. # define MAD_F_MLX(hi, lo, x, y) \
  315. asm ("smul %2, %3, %0\n\t" \
  316. "rd %%y, %1" \
  317. : "=r" (lo), "=r" (hi) \
  318. : "%r" (x), "rI" (y))
  319. /* --- PowerPC ------------------------------------------------------------- */
  320. # elif defined(FPM_PPC)
  321. /*
  322. * This PowerPC version is fast and accurate; the disposition of the least
  323. * significant bit depends on OPT_ACCURACY via mad_f_scale64().
  324. */
  325. # define MAD_F_MLX(hi, lo, x, y) \
  326. do { \
  327. asm ("mullw %0,%1,%2" \
  328. : "=r" (lo) \
  329. : "%r" (x), "r" (y)); \
  330. asm ("mulhw %0,%1,%2" \
  331. : "=r" (hi) \
  332. : "%r" (x), "r" (y)); \
  333. } \
  334. while (0)
  335. # if defined(OPT_ACCURACY)
  336. /*
  337. * This gives best accuracy but is not very fast.
  338. */
  339. # define MAD_F_MLA(hi, lo, x, y) \
  340. ({ mad_fixed64hi_t __hi; \
  341. mad_fixed64lo_t __lo; \
  342. MAD_F_MLX(__hi, __lo, (x), (y)); \
  343. asm ("addc %0,%2,%3\n\t" \
  344. "adde %1,%4,%5" \
  345. : "=r" (lo), "=r" (hi) \
  346. : "%r" (lo), "r" (__lo), \
  347. "%r" (hi), "r" (__hi) \
  348. : "xer"); \
  349. })
  350. # endif
  351. # if defined(OPT_ACCURACY)
  352. /*
  353. * This is slower than the truncating version below it.
  354. */
  355. # define mad_f_scale64(hi, lo) \
  356. ({ mad_fixed_t __result, __round; \
  357. asm ("rotrwi %0,%1,%2" \
  358. : "=r" (__result) \
  359. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  360. asm ("extrwi %0,%1,1,0" \
  361. : "=r" (__round) \
  362. : "r" (__result)); \
  363. asm ("insrwi %0,%1,%2,0" \
  364. : "+r" (__result) \
  365. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  366. asm ("add %0,%1,%2" \
  367. : "=r" (__result) \
  368. : "%r" (__result), "r" (__round)); \
  369. __result; \
  370. })
  371. # else
  372. # define mad_f_scale64(hi, lo) \
  373. ({ mad_fixed_t __result; \
  374. asm ("rotrwi %0,%1,%2" \
  375. : "=r" (__result) \
  376. : "r" (lo), "i" (MAD_F_SCALEBITS)); \
  377. asm ("insrwi %0,%1,%2,0" \
  378. : "+r" (__result) \
  379. : "r" (hi), "i" (MAD_F_SCALEBITS)); \
  380. __result; \
  381. })
  382. # endif
  383. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  384. /* --- Default ------------------------------------------------------------- */
  385. # elif defined(FPM_DEFAULT)
  386. /*
  387. * This version is the most portable but it loses significant accuracy.
  388. * Furthermore, accuracy is biased against the second argument, so care
  389. * should be taken when ordering operands.
  390. *
  391. * The scale factors are constant as this is not used with SSO.
  392. *
  393. * Pre-rounding is required to stay within the limits of compliance.
  394. */
  395. # if defined(OPT_SPEED)
  396. # define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
  397. # else
  398. # define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
  399. (((y) + (1L << 15)) >> 16))
  400. # endif
  401. /* ------------------------------------------------------------------------- */
  402. # else
  403. # error "no FPM selected"
  404. # endif
  405. /* default implementations */
  406. # if !defined(mad_f_mul)
  407. # define mad_f_mul(x, y) \
  408. ({ register mad_fixed64hi_t __hi; \
  409. register mad_fixed64lo_t __lo; \
  410. MAD_F_MLX(__hi, __lo, (x), (y)); \
  411. mad_f_scale64(__hi, __lo); \
  412. })
  413. # endif
  414. # if !defined(MAD_F_MLA)
  415. # define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
  416. # define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
  417. # define MAD_F_MLN(hi, lo) ((lo) = -(lo))
  418. # define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
  419. # endif
  420. # if !defined(MAD_F_ML0)
  421. # define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
  422. # endif
  423. # if !defined(MAD_F_MLN)
  424. # define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
  425. # endif
  426. # if !defined(MAD_F_MLZ)
  427. # define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
  428. # endif
  429. # if !defined(mad_f_scale64)
  430. # if defined(OPT_ACCURACY)
  431. # define mad_f_scale64(hi, lo) \
  432. ((((mad_fixed_t) \
  433. (((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
  434. ((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
  435. # else
  436. # define mad_f_scale64(hi, lo) \
  437. ((mad_fixed_t) \
  438. (((hi) << (32 - MAD_F_SCALEBITS)) | \
  439. ((lo) >> MAD_F_SCALEBITS)))
  440. # endif
  441. # define MAD_F_SCALEBITS MAD_F_FRACBITS
  442. # endif
  443. /* C routines */
  444. mad_fixed_t mad_f_abs(mad_fixed_t);
  445. mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
  446. # endif
  447. /* Id: bit.h,v 1.11 2003/05/27 22:40:36 rob Exp */
  448. # ifndef LIBMAD_BIT_H
  449. # define LIBMAD_BIT_H
  450. struct mad_bitptr {
  451. unsigned char const *byte;
  452. unsigned short cache;
  453. unsigned short left;
  454. };
  455. void mad_bit_init(struct mad_bitptr *, unsigned char const *);
  456. # define mad_bit_finish(bitptr) /* nothing */
  457. unsigned int mad_bit_length(struct mad_bitptr const *,
  458. struct mad_bitptr const *);
  459. # define mad_bit_bitsleft(bitptr) ((bitptr)->left)
  460. unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *);
  461. void mad_bit_skip(struct mad_bitptr *, unsigned int);
  462. unsigned long mad_bit_read(struct mad_bitptr *, unsigned int);
  463. void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long);
  464. unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short);
  465. # endif
  466. /* Id: timer.h,v 1.15 2003/05/27 22:40:37 rob Exp */
  467. # ifndef LIBMAD_TIMER_H
  468. # define LIBMAD_TIMER_H
  469. typedef struct {
  470. signed long seconds; /* whole seconds */
  471. unsigned long fraction; /* 1/MAD_TIMER_RESOLUTION seconds */
  472. } mad_timer_t;
  473. extern mad_timer_t const mad_timer_zero;
  474. # define MAD_TIMER_RESOLUTION 352800000UL
  475. enum mad_units {
  476. MAD_UNITS_HOURS = -2,
  477. MAD_UNITS_MINUTES = -1,
  478. MAD_UNITS_SECONDS = 0,
  479. /* metric units */
  480. MAD_UNITS_DECISECONDS = 10,
  481. MAD_UNITS_CENTISECONDS = 100,
  482. MAD_UNITS_MILLISECONDS = 1000,
  483. /* audio sample units */
  484. MAD_UNITS_8000_HZ = 8000,
  485. MAD_UNITS_11025_HZ = 11025,
  486. MAD_UNITS_12000_HZ = 12000,
  487. MAD_UNITS_16000_HZ = 16000,
  488. MAD_UNITS_22050_HZ = 22050,
  489. MAD_UNITS_24000_HZ = 24000,
  490. MAD_UNITS_32000_HZ = 32000,
  491. MAD_UNITS_44100_HZ = 44100,
  492. MAD_UNITS_48000_HZ = 48000,
  493. /* video frame/field units */
  494. MAD_UNITS_24_FPS = 24,
  495. MAD_UNITS_25_FPS = 25,
  496. MAD_UNITS_30_FPS = 30,
  497. MAD_UNITS_48_FPS = 48,
  498. MAD_UNITS_50_FPS = 50,
  499. MAD_UNITS_60_FPS = 60,
  500. /* CD audio frames */
  501. MAD_UNITS_75_FPS = 75,
  502. /* video drop-frame units */
  503. MAD_UNITS_23_976_FPS = -24,
  504. MAD_UNITS_24_975_FPS = -25,
  505. MAD_UNITS_29_97_FPS = -30,
  506. MAD_UNITS_47_952_FPS = -48,
  507. MAD_UNITS_49_95_FPS = -50,
  508. MAD_UNITS_59_94_FPS = -60
  509. };
  510. # define mad_timer_reset(timer) ((void) (*(timer) = mad_timer_zero))
  511. int mad_timer_compare(mad_timer_t, mad_timer_t);
  512. # define mad_timer_sign(timer) mad_timer_compare((timer), mad_timer_zero)
  513. void mad_timer_negate(mad_timer_t *);
  514. mad_timer_t mad_timer_abs(mad_timer_t);
  515. void mad_timer_set(mad_timer_t *, unsigned long, unsigned long, unsigned long);
  516. void mad_timer_add(mad_timer_t *, mad_timer_t);
  517. void mad_timer_multiply(mad_timer_t *, signed long);
  518. signed long mad_timer_count(mad_timer_t, enum mad_units);
  519. unsigned long mad_timer_fraction(mad_timer_t, unsigned long);
  520. void mad_timer_string(mad_timer_t, char *, char const *,
  521. enum mad_units, enum mad_units, unsigned long);
  522. # endif
  523. /* Id: stream.h,v 1.18 2003/05/27 22:40:37 rob Exp */
  524. # ifndef LIBMAD_STREAM_H
  525. # define LIBMAD_STREAM_H
  526. # define MAD_BUFFER_GUARD 8
  527. # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD)
  528. enum mad_error {
  529. MAD_ERROR_NONE = 0x0000, /* no error */
  530. MAD_ERROR_BUFLEN = 0x0001, /* input buffer too small (or EOF) */
  531. MAD_ERROR_BUFPTR = 0x0002, /* invalid (null) buffer pointer */
  532. MAD_ERROR_NOMEM = 0x0031, /* not enough memory */
  533. MAD_ERROR_LOSTSYNC = 0x0101, /* lost synchronization */
  534. MAD_ERROR_BADLAYER = 0x0102, /* reserved header layer value */
  535. MAD_ERROR_BADBITRATE = 0x0103, /* forbidden bitrate value */
  536. MAD_ERROR_BADSAMPLERATE = 0x0104, /* reserved sample frequency value */
  537. MAD_ERROR_BADEMPHASIS = 0x0105, /* reserved emphasis value */
  538. MAD_ERROR_BADCRC = 0x0201, /* CRC check failed */
  539. MAD_ERROR_BADBITALLOC = 0x0211, /* forbidden bit allocation value */
  540. MAD_ERROR_BADSCALEFACTOR = 0x0221, /* bad scalefactor index */
  541. MAD_ERROR_BADFRAMELEN = 0x0231, /* bad frame length */
  542. MAD_ERROR_BADBIGVALUES = 0x0232, /* bad big_values count */
  543. MAD_ERROR_BADBLOCKTYPE = 0x0233, /* reserved block_type */
  544. MAD_ERROR_BADSCFSI = 0x0234, /* bad scalefactor selection info */
  545. MAD_ERROR_BADDATAPTR = 0x0235, /* bad main_data_begin pointer */
  546. MAD_ERROR_BADPART3LEN = 0x0236, /* bad audio data length */
  547. MAD_ERROR_BADHUFFTABLE = 0x0237, /* bad Huffman table select */
  548. MAD_ERROR_BADHUFFDATA = 0x0238, /* Huffman data overrun */
  549. MAD_ERROR_BADSTEREO = 0x0239 /* incompatible block_type for JS */
  550. };
  551. # define MAD_RECOVERABLE(error) ((error) & 0xff00)
  552. struct mad_stream {
  553. unsigned char const *buffer; /* input bitstream buffer */
  554. unsigned char const *bufend; /* end of buffer */
  555. unsigned long skiplen; /* bytes to skip before next frame */
  556. int sync; /* stream sync found */
  557. unsigned long freerate; /* free bitrate (fixed) */
  558. unsigned char const *this_frame; /* start of current frame */
  559. unsigned char const *next_frame; /* start of next frame */
  560. struct mad_bitptr ptr; /* current processing bit pointer */
  561. struct mad_bitptr anc_ptr; /* ancillary bits pointer */
  562. unsigned int anc_bitlen; /* number of ancillary bits */
  563. unsigned char (*main_data)[MAD_BUFFER_MDLEN];
  564. /* Layer III main_data() */
  565. unsigned int md_len; /* bytes in main_data */
  566. int options; /* decoding options (see below) */
  567. enum mad_error error; /* error code (see above) */
  568. };
  569. enum {
  570. MAD_OPTION_IGNORECRC = 0x0001, /* ignore CRC errors */
  571. MAD_OPTION_HALFSAMPLERATE = 0x0002 /* generate PCM at 1/2 sample rate */
  572. # if 0 /* not yet implemented */
  573. MAD_OPTION_LEFTCHANNEL = 0x0010, /* decode left channel only */
  574. MAD_OPTION_RIGHTCHANNEL = 0x0020, /* decode right channel only */
  575. MAD_OPTION_SINGLECHANNEL = 0x0030 /* combine channels */
  576. # endif
  577. };
  578. void mad_stream_init(struct mad_stream *);
  579. void mad_stream_finish(struct mad_stream *);
  580. # define mad_stream_options(stream, opts) \
  581. ((void) ((stream)->options = (opts)))
  582. void mad_stream_buffer(struct mad_stream *,
  583. unsigned char const *, unsigned long);
  584. void mad_stream_skip(struct mad_stream *, unsigned long);
  585. int mad_stream_sync(struct mad_stream *);
  586. char const *mad_stream_errorstr(struct mad_stream const *);
  587. # endif
  588. /* Id: frame.h,v 1.19 2003/05/27 22:40:36 rob Exp */
  589. # ifndef LIBMAD_FRAME_H
  590. # define LIBMAD_FRAME_H
  591. enum mad_layer {
  592. MAD_LAYER_I = 1, /* Layer I */
  593. MAD_LAYER_II = 2, /* Layer II */
  594. MAD_LAYER_III = 3 /* Layer III */
  595. };
  596. enum mad_mode {
  597. MAD_MODE_SINGLE_CHANNEL = 0, /* single channel */
  598. MAD_MODE_DUAL_CHANNEL = 1, /* dual channel */
  599. MAD_MODE_JOINT_STEREO = 2, /* joint (MS/intensity) stereo */
  600. MAD_MODE_STEREO = 3 /* normal LR stereo */
  601. };
  602. enum mad_emphasis {
  603. MAD_EMPHASIS_NONE = 0, /* no emphasis */
  604. MAD_EMPHASIS_50_15_US = 1, /* 50/15 microseconds emphasis */
  605. MAD_EMPHASIS_CCITT_J_17 = 3, /* CCITT J.17 emphasis */
  606. MAD_EMPHASIS_RESERVED = 2 /* unknown emphasis */
  607. };
  608. struct mad_header {
  609. enum mad_layer layer; /* audio layer (1, 2, or 3) */
  610. enum mad_mode mode; /* channel mode (see above) */
  611. int mode_extension; /* additional mode info */
  612. enum mad_emphasis emphasis; /* de-emphasis to use (see above) */
  613. unsigned long bitrate; /* stream bitrate (bps) */
  614. unsigned int samplerate; /* sampling frequency (Hz) */
  615. unsigned short crc_check; /* frame CRC accumulator */
  616. unsigned short crc_target; /* final target CRC checksum */
  617. int flags; /* flags (see below) */
  618. int private_bits; /* private bits (see below) */
  619. mad_timer_t duration; /* audio playing time of frame */
  620. };
  621. struct mad_frame {
  622. struct mad_header header; /* MPEG audio header */
  623. int options; /* decoding options (from stream) */
  624. mad_fixed_t sbsample[2][36][32]; /* synthesis subband filter samples */
  625. mad_fixed_t (*overlap)[2][32][18]; /* Layer III block overlap data */
  626. };
  627. # define MAD_NCHANNELS(header) ((header)->mode ? 2 : 1)
  628. # define MAD_NSBSAMPLES(header) \
  629. ((header)->layer == MAD_LAYER_I ? 12 : \
  630. (((header)->layer == MAD_LAYER_III && \
  631. ((header)->flags & MAD_FLAG_LSF_EXT)) ? 18 : 36))
  632. enum {
  633. MAD_FLAG_NPRIVATE_III = 0x0007, /* number of Layer III private bits */
  634. MAD_FLAG_INCOMPLETE = 0x0008, /* header but not data is decoded */
  635. MAD_FLAG_PROTECTION = 0x0010, /* frame has CRC protection */
  636. MAD_FLAG_COPYRIGHT = 0x0020, /* frame is copyright */
  637. MAD_FLAG_ORIGINAL = 0x0040, /* frame is original (else copy) */
  638. MAD_FLAG_PADDING = 0x0080, /* frame has additional slot */
  639. MAD_FLAG_I_STEREO = 0x0100, /* uses intensity joint stereo */
  640. MAD_FLAG_MS_STEREO = 0x0200, /* uses middle/side joint stereo */
  641. MAD_FLAG_FREEFORMAT = 0x0400, /* uses free format bitrate */
  642. MAD_FLAG_LSF_EXT = 0x1000, /* lower sampling freq. extension */
  643. MAD_FLAG_MC_EXT = 0x2000, /* multichannel audio extension */
  644. MAD_FLAG_MPEG_2_5_EXT = 0x4000 /* MPEG 2.5 (unofficial) extension */
  645. };
  646. enum {
  647. MAD_PRIVATE_HEADER = 0x0100, /* header private bit */
  648. MAD_PRIVATE_III = 0x001f /* Layer III private bits (up to 5) */
  649. };
  650. void mad_header_init(struct mad_header *);
  651. # define mad_header_finish(header) /* nothing */
  652. int mad_header_decode(struct mad_header *, struct mad_stream *);
  653. void mad_frame_init(struct mad_frame *);
  654. void mad_frame_finish(struct mad_frame *);
  655. int mad_frame_decode(struct mad_frame *, struct mad_stream *);
  656. void mad_frame_mute(struct mad_frame *);
  657. # endif
  658. /* Id: synth.h,v 1.14 2003/05/27 22:40:37 rob Exp */
  659. # ifndef LIBMAD_SYNTH_H
  660. # define LIBMAD_SYNTH_H
  661. struct mad_pcm {
  662. unsigned int samplerate; /* sampling frequency (Hz) */
  663. unsigned short channels; /* number of channels */
  664. unsigned short length; /* number of samples per channel */
  665. mad_fixed_t samples[2][1152]; /* PCM output samples [ch][sample] */
  666. };
  667. struct mad_synth {
  668. mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */
  669. /* [ch][eo][peo][s][v] */
  670. unsigned int phase; /* current processing phase */
  671. struct mad_pcm pcm; /* PCM output */
  672. };
  673. /* single channel PCM selector */
  674. enum {
  675. MAD_PCM_CHANNEL_SINGLE = 0
  676. };
  677. /* dual channel PCM selector */
  678. enum {
  679. MAD_PCM_CHANNEL_DUAL_1 = 0,
  680. MAD_PCM_CHANNEL_DUAL_2 = 1
  681. };
  682. /* stereo PCM selector */
  683. enum {
  684. MAD_PCM_CHANNEL_STEREO_LEFT = 0,
  685. MAD_PCM_CHANNEL_STEREO_RIGHT = 1
  686. };
  687. void mad_synth_init(struct mad_synth *);
  688. # define mad_synth_finish(synth) /* nothing */
  689. void mad_synth_mute(struct mad_synth *);
  690. void mad_synth_frame(struct mad_synth *, struct mad_frame const *);
  691. # endif
  692. /* Id: decoder.h,v 1.16 2003/05/27 22:40:36 rob Exp */
  693. # ifndef LIBMAD_DECODER_H
  694. # define LIBMAD_DECODER_H
  695. enum mad_decoder_mode {
  696. MAD_DECODER_MODE_SYNC = 0,
  697. MAD_DECODER_MODE_ASYNC
  698. };
  699. enum mad_flow {
  700. MAD_FLOW_CONTINUE = 0x0000, /* continue normally */
  701. MAD_FLOW_STOP = 0x0010, /* stop decoding normally */
  702. MAD_FLOW_BREAK = 0x0011, /* stop decoding and signal an error */
  703. MAD_FLOW_IGNORE = 0x0020 /* ignore the current frame */
  704. };
  705. struct mad_decoder {
  706. enum mad_decoder_mode mode;
  707. int options;
  708. struct {
  709. long pid;
  710. int in;
  711. int out;
  712. } async;
  713. struct {
  714. struct mad_stream stream;
  715. struct mad_frame frame;
  716. struct mad_synth synth;
  717. } *sync;
  718. void *cb_data;
  719. enum mad_flow (*input_func)(void *, struct mad_stream *);
  720. enum mad_flow (*header_func)(void *, struct mad_header const *);
  721. enum mad_flow (*filter_func)(void *,
  722. struct mad_stream const *, struct mad_frame *);
  723. enum mad_flow (*output_func)(void *,
  724. struct mad_header const *, struct mad_pcm *);
  725. enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
  726. enum mad_flow (*message_func)(void *, void *, unsigned int *);
  727. };
  728. void mad_decoder_init(struct mad_decoder *, void *,
  729. enum mad_flow (*)(void *, struct mad_stream *),
  730. enum mad_flow (*)(void *, struct mad_header const *),
  731. enum mad_flow (*)(void *,
  732. struct mad_stream const *,
  733. struct mad_frame *),
  734. enum mad_flow (*)(void *,
  735. struct mad_header const *,
  736. struct mad_pcm *),
  737. enum mad_flow (*)(void *,
  738. struct mad_stream *,
  739. struct mad_frame *),
  740. enum mad_flow (*)(void *, void *, unsigned int *));
  741. int mad_decoder_finish(struct mad_decoder *);
  742. # define mad_decoder_options(decoder, opts) \
  743. ((void) ((decoder)->options = (opts)))
  744. int mad_decoder_run(struct mad_decoder *, enum mad_decoder_mode);
  745. int mad_decoder_message(struct mad_decoder *, void *, unsigned int *);
  746. # endif
  747. # ifdef __cplusplus
  748. }
  749. # endif