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.

2698 lines
68 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. * $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
  20. */
  21. # ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. # endif
  24. # include "global.h"
  25. # include <stdlib.h>
  26. # include <string.h>
  27. # ifdef HAVE_ASSERT_H
  28. # include <assert.h>
  29. # endif
  30. # ifdef HAVE_LIMITS_H
  31. # include <limits.h>
  32. # else
  33. # define CHAR_BIT 8
  34. # endif
  35. # include "fixed.h"
  36. # include "bit.h"
  37. # include "stream.h"
  38. # include "frame.h"
  39. # include "huffman.h"
  40. # include "layer3.h"
  41. /* --- Layer III ----------------------------------------------------------- */
  42. enum {
  43. count1table_select = 0x01,
  44. scalefac_scale = 0x02,
  45. preflag = 0x04,
  46. mixed_block_flag = 0x08
  47. };
  48. enum {
  49. I_STEREO = 0x1,
  50. MS_STEREO = 0x2
  51. };
  52. struct sideinfo {
  53. unsigned int main_data_begin;
  54. unsigned int private_bits;
  55. unsigned char scfsi[2];
  56. struct granule {
  57. struct channel {
  58. /* from side info */
  59. unsigned short part2_3_length;
  60. unsigned short big_values;
  61. unsigned short global_gain;
  62. unsigned short scalefac_compress;
  63. unsigned char flags;
  64. unsigned char block_type;
  65. unsigned char table_select[3];
  66. unsigned char subblock_gain[3];
  67. unsigned char region0_count;
  68. unsigned char region1_count;
  69. /* from main_data */
  70. unsigned char scalefac[39]; /* scalefac_l and/or scalefac_s */
  71. } ch[2];
  72. } gr[2];
  73. };
  74. /*
  75. * scalefactor bit lengths
  76. * derived from section 2.4.2.7 of ISO/IEC 11172-3
  77. */
  78. static
  79. struct {
  80. unsigned char slen1;
  81. unsigned char slen2;
  82. } const sflen_table[16] = {
  83. { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
  84. { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
  85. { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
  86. { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
  87. };
  88. /*
  89. * number of LSF scalefactor band values
  90. * derived from section 2.4.3.2 of ISO/IEC 13818-3
  91. */
  92. static
  93. unsigned char const nsfb_table[6][3][4] = {
  94. { { 6, 5, 5, 5 },
  95. { 9, 9, 9, 9 },
  96. { 6, 9, 9, 9 } },
  97. { { 6, 5, 7, 3 },
  98. { 9, 9, 12, 6 },
  99. { 6, 9, 12, 6 } },
  100. { { 11, 10, 0, 0 },
  101. { 18, 18, 0, 0 },
  102. { 15, 18, 0, 0 } },
  103. { { 7, 7, 7, 0 },
  104. { 12, 12, 12, 0 },
  105. { 6, 15, 12, 0 } },
  106. { { 6, 6, 6, 3 },
  107. { 12, 9, 9, 6 },
  108. { 6, 12, 9, 6 } },
  109. { { 8, 8, 5, 0 },
  110. { 15, 12, 9, 0 },
  111. { 6, 18, 9, 0 } }
  112. };
  113. /*
  114. * MPEG-1 scalefactor band widths
  115. * derived from Table B.8 of ISO/IEC 11172-3
  116. */
  117. static
  118. unsigned char const sfb_48000_long[] = {
  119. 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
  120. 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192
  121. };
  122. static
  123. unsigned char const sfb_44100_long[] = {
  124. 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
  125. 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158
  126. };
  127. static
  128. unsigned char const sfb_32000_long[] = {
  129. 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
  130. 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26
  131. };
  132. static
  133. unsigned char const sfb_48000_short[] = {
  134. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
  135. 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14, 14,
  136. 14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
  137. };
  138. static
  139. unsigned char const sfb_44100_short[] = {
  140. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
  141. 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14,
  142. 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
  143. };
  144. static
  145. unsigned char const sfb_32000_short[] = {
  146. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
  147. 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20,
  148. 20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
  149. };
  150. static
  151. unsigned char const sfb_48000_mixed[] = {
  152. /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
  153. /* short */ 4, 4, 4, 6, 6, 6, 6, 6, 6, 10,
  154. 10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
  155. 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
  156. };
  157. static
  158. unsigned char const sfb_44100_mixed[] = {
  159. /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
  160. /* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 10,
  161. 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
  162. 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
  163. };
  164. static
  165. unsigned char const sfb_32000_mixed[] = {
  166. /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
  167. /* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 12,
  168. 12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
  169. 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
  170. };
  171. /*
  172. * MPEG-2 scalefactor band widths
  173. * derived from Table B.2 of ISO/IEC 13818-3
  174. */
  175. static
  176. unsigned char const sfb_24000_long[] = {
  177. 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  178. 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36
  179. };
  180. static
  181. unsigned char const sfb_22050_long[] = {
  182. 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
  183. 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54
  184. };
  185. # define sfb_16000_long sfb_22050_long
  186. static
  187. unsigned char const sfb_24000_short[] = {
  188. 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
  189. 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
  190. 18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
  191. };
  192. static
  193. unsigned char const sfb_22050_short[] = {
  194. 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6,
  195. 6, 6, 8, 8, 8, 10, 10, 10, 14, 14, 14, 18, 18,
  196. 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
  197. };
  198. static
  199. unsigned char const sfb_16000_short[] = {
  200. 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
  201. 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
  202. 18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
  203. };
  204. static
  205. unsigned char const sfb_24000_mixed[] = {
  206. /* long */ 6, 6, 6, 6, 6, 6,
  207. /* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
  208. 12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
  209. 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
  210. };
  211. static
  212. unsigned char const sfb_22050_mixed[] = {
  213. /* long */ 6, 6, 6, 6, 6, 6,
  214. /* short */ 6, 6, 6, 6, 6, 6, 8, 8, 8, 10,
  215. 10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
  216. 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
  217. };
  218. static
  219. unsigned char const sfb_16000_mixed[] = {
  220. /* long */ 6, 6, 6, 6, 6, 6,
  221. /* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
  222. 12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
  223. 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
  224. };
  225. /*
  226. * MPEG 2.5 scalefactor band widths
  227. * derived from public sources
  228. */
  229. # define sfb_12000_long sfb_16000_long
  230. # define sfb_11025_long sfb_12000_long
  231. static
  232. unsigned char const sfb_8000_long[] = {
  233. 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
  234. 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2
  235. };
  236. # define sfb_12000_short sfb_16000_short
  237. # define sfb_11025_short sfb_12000_short
  238. static
  239. unsigned char const sfb_8000_short[] = {
  240. 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 16,
  241. 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
  242. 36, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
  243. };
  244. # define sfb_12000_mixed sfb_16000_mixed
  245. # define sfb_11025_mixed sfb_12000_mixed
  246. /* the 8000 Hz short block scalefactor bands do not break after
  247. the first 36 frequency lines, so this is probably wrong */
  248. static
  249. unsigned char const sfb_8000_mixed[] = {
  250. /* long */ 12, 12, 12,
  251. /* short */ 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16,
  252. 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
  253. 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
  254. };
  255. static
  256. struct {
  257. unsigned char const *l;
  258. unsigned char const *s;
  259. unsigned char const *m;
  260. } const sfbwidth_table[9] = {
  261. { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
  262. { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
  263. { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
  264. { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
  265. { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
  266. { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
  267. { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
  268. { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
  269. { sfb_8000_long, sfb_8000_short, sfb_8000_mixed }
  270. };
  271. /*
  272. * scalefactor band preemphasis (used only when preflag is set)
  273. * derived from Table B.6 of ISO/IEC 11172-3
  274. */
  275. static
  276. unsigned char const pretab[22] = {
  277. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
  278. };
  279. /*
  280. * table for requantization
  281. *
  282. * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
  283. */
  284. static
  285. struct fixedfloat {
  286. unsigned long mantissa : 27;
  287. unsigned short exponent : 5;
  288. } const rq_table[8207] = {
  289. # include "rq_table.dat"
  290. };
  291. /*
  292. * fractional powers of two
  293. * used for requantization and joint stereo decoding
  294. *
  295. * root_table[3 + x] = 2^(x/4)
  296. */
  297. static
  298. mad_fixed_t const root_table[7] = {
  299. MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
  300. MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
  301. MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
  302. MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
  303. MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
  304. MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
  305. MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
  306. };
  307. /*
  308. * coefficients for aliasing reduction
  309. * derived from Table B.9 of ISO/IEC 11172-3
  310. *
  311. * c[] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
  312. * cs[i] = 1 / sqrt(1 + c[i]^2)
  313. * ca[i] = c[i] / sqrt(1 + c[i]^2)
  314. */
  315. static
  316. mad_fixed_t const cs[8] = {
  317. +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
  318. +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
  319. +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
  320. +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
  321. };
  322. static
  323. mad_fixed_t const ca[8] = {
  324. -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
  325. -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
  326. -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
  327. -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
  328. };
  329. /*
  330. * IMDCT coefficients for short blocks
  331. * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
  332. *
  333. * imdct_s[i/even][k] = cos((PI / 24) * (2 * (i / 2) + 7) * (2 * k + 1))
  334. * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
  335. */
  336. static
  337. mad_fixed_t const imdct_s[6][6] = {
  338. # include "imdct_s.dat"
  339. };
  340. # if !defined(ASO_IMDCT)
  341. /*
  342. * windowing coefficients for long blocks
  343. * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
  344. *
  345. * window_l[i] = sin((PI / 36) * (i + 1/2))
  346. */
  347. static
  348. mad_fixed_t const window_l[36] = {
  349. MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
  350. MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
  351. MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
  352. MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
  353. MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
  354. MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
  355. MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
  356. MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
  357. MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
  358. MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
  359. MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
  360. MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
  361. MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
  362. MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
  363. MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
  364. MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
  365. MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
  366. MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
  367. };
  368. # endif /* ASO_IMDCT */
  369. /*
  370. * windowing coefficients for short blocks
  371. * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
  372. *
  373. * window_s[i] = sin((PI / 12) * (i + 1/2))
  374. */
  375. static
  376. mad_fixed_t const window_s[12] = {
  377. MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
  378. MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
  379. MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
  380. MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
  381. MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
  382. MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
  383. };
  384. /*
  385. * coefficients for intensity stereo processing
  386. * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
  387. *
  388. * is_ratio[i] = tan(i * (PI / 12))
  389. * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
  390. */
  391. static
  392. mad_fixed_t const is_table[7] = {
  393. MAD_F(0x00000000) /* 0.000000000 */,
  394. MAD_F(0x0361962f) /* 0.211324865 */,
  395. MAD_F(0x05db3d74) /* 0.366025404 */,
  396. MAD_F(0x08000000) /* 0.500000000 */,
  397. MAD_F(0x0a24c28c) /* 0.633974596 */,
  398. MAD_F(0x0c9e69d1) /* 0.788675135 */,
  399. MAD_F(0x10000000) /* 1.000000000 */
  400. };
  401. /*
  402. * coefficients for LSF intensity stereo processing
  403. * derived from section 2.4.3.2 of ISO/IEC 13818-3
  404. *
  405. * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
  406. * is_lsf_table[1][i] = (1 / sqrt(2)) ^(i + 1)
  407. */
  408. static
  409. mad_fixed_t const is_lsf_table[2][15] = {
  410. {
  411. MAD_F(0x0d744fcd) /* 0.840896415 */,
  412. MAD_F(0x0b504f33) /* 0.707106781 */,
  413. MAD_F(0x09837f05) /* 0.594603558 */,
  414. MAD_F(0x08000000) /* 0.500000000 */,
  415. MAD_F(0x06ba27e6) /* 0.420448208 */,
  416. MAD_F(0x05a8279a) /* 0.353553391 */,
  417. MAD_F(0x04c1bf83) /* 0.297301779 */,
  418. MAD_F(0x04000000) /* 0.250000000 */,
  419. MAD_F(0x035d13f3) /* 0.210224104 */,
  420. MAD_F(0x02d413cd) /* 0.176776695 */,
  421. MAD_F(0x0260dfc1) /* 0.148650889 */,
  422. MAD_F(0x02000000) /* 0.125000000 */,
  423. MAD_F(0x01ae89fa) /* 0.105112052 */,
  424. MAD_F(0x016a09e6) /* 0.088388348 */,
  425. MAD_F(0x01306fe1) /* 0.074325445 */
  426. }, {
  427. MAD_F(0x0b504f33) /* 0.707106781 */,
  428. MAD_F(0x08000000) /* 0.500000000 */,
  429. MAD_F(0x05a8279a) /* 0.353553391 */,
  430. MAD_F(0x04000000) /* 0.250000000 */,
  431. MAD_F(0x02d413cd) /* 0.176776695 */,
  432. MAD_F(0x02000000) /* 0.125000000 */,
  433. MAD_F(0x016a09e6) /* 0.088388348 */,
  434. MAD_F(0x01000000) /* 0.062500000 */,
  435. MAD_F(0x00b504f3) /* 0.044194174 */,
  436. MAD_F(0x00800000) /* 0.031250000 */,
  437. MAD_F(0x005a827a) /* 0.022097087 */,
  438. MAD_F(0x00400000) /* 0.015625000 */,
  439. MAD_F(0x002d413d) /* 0.011048543 */,
  440. MAD_F(0x00200000) /* 0.007812500 */,
  441. MAD_F(0x0016a09e) /* 0.005524272 */
  442. }
  443. };
  444. /*
  445. * NAME: III_sideinfo()
  446. * DESCRIPTION: decode frame side information from a bitstream
  447. */
  448. static
  449. enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
  450. int lsf, struct sideinfo *si,
  451. unsigned int *data_bitlen,
  452. unsigned int *priv_bitlen)
  453. {
  454. unsigned int ngr, gr, ch, i;
  455. enum mad_error result = MAD_ERROR_NONE;
  456. *data_bitlen = 0;
  457. *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
  458. si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
  459. si->private_bits = mad_bit_read(ptr, *priv_bitlen);
  460. ngr = 1;
  461. if (!lsf) {
  462. ngr = 2;
  463. for (ch = 0; ch < nch; ++ch)
  464. si->scfsi[ch] = mad_bit_read(ptr, 4);
  465. }
  466. for (gr = 0; gr < ngr; ++gr) {
  467. struct granule *granule = &si->gr[gr];
  468. for (ch = 0; ch < nch; ++ch) {
  469. struct channel *channel = &granule->ch[ch];
  470. channel->part2_3_length = mad_bit_read(ptr, 12);
  471. channel->big_values = mad_bit_read(ptr, 9);
  472. channel->global_gain = mad_bit_read(ptr, 8);
  473. channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
  474. *data_bitlen += channel->part2_3_length;
  475. if (channel->big_values > 288 && result == 0)
  476. result = MAD_ERROR_BADBIGVALUES;
  477. channel->flags = 0;
  478. /* window_switching_flag */
  479. if (mad_bit_read(ptr, 1)) {
  480. channel->block_type = mad_bit_read(ptr, 2);
  481. if (channel->block_type == 0 && result == 0)
  482. result = MAD_ERROR_BADBLOCKTYPE;
  483. if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
  484. result = MAD_ERROR_BADSCFSI;
  485. channel->region0_count = 7;
  486. channel->region1_count = 36;
  487. if (mad_bit_read(ptr, 1))
  488. channel->flags |= mixed_block_flag;
  489. else if (channel->block_type == 2)
  490. channel->region0_count = 8;
  491. for (i = 0; i < 2; ++i)
  492. channel->table_select[i] = mad_bit_read(ptr, 5);
  493. # if defined(DEBUG)
  494. channel->table_select[2] = 4; /* not used */
  495. # endif
  496. for (i = 0; i < 3; ++i)
  497. channel->subblock_gain[i] = mad_bit_read(ptr, 3);
  498. }
  499. else {
  500. channel->block_type = 0;
  501. for (i = 0; i < 3; ++i)
  502. channel->table_select[i] = mad_bit_read(ptr, 5);
  503. channel->region0_count = mad_bit_read(ptr, 4);
  504. channel->region1_count = mad_bit_read(ptr, 3);
  505. }
  506. /* [preflag,] scalefac_scale, count1table_select */
  507. channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
  508. }
  509. }
  510. return result;
  511. }
  512. /*
  513. * NAME: III_scalefactors_lsf()
  514. * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
  515. */
  516. static
  517. unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
  518. struct channel *channel,
  519. struct channel *gr1ch, int mode_extension)
  520. {
  521. struct mad_bitptr start;
  522. unsigned int scalefac_compress, index, slen[4], part, n, i;
  523. unsigned char const *nsfb;
  524. start = *ptr;
  525. scalefac_compress = channel->scalefac_compress;
  526. index = (channel->block_type == 2) ?
  527. ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
  528. if (!((mode_extension & I_STEREO) && gr1ch)) {
  529. if (scalefac_compress < 400) {
  530. slen[0] = (scalefac_compress >> 4) / 5;
  531. slen[1] = (scalefac_compress >> 4) % 5;
  532. slen[2] = (scalefac_compress % 16) >> 2;
  533. slen[3] = scalefac_compress % 4;
  534. nsfb = nsfb_table[0][index];
  535. }
  536. else if (scalefac_compress < 500) {
  537. scalefac_compress -= 400;
  538. slen[0] = (scalefac_compress >> 2) / 5;
  539. slen[1] = (scalefac_compress >> 2) % 5;
  540. slen[2] = scalefac_compress % 4;
  541. slen[3] = 0;
  542. nsfb = nsfb_table[1][index];
  543. }
  544. else {
  545. scalefac_compress -= 500;
  546. slen[0] = scalefac_compress / 3;
  547. slen[1] = scalefac_compress % 3;
  548. slen[2] = 0;
  549. slen[3] = 0;
  550. channel->flags |= preflag;
  551. nsfb = nsfb_table[2][index];
  552. }
  553. n = 0;
  554. for (part = 0; part < 4; ++part) {
  555. for (i = 0; i < nsfb[part]; ++i)
  556. channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
  557. }
  558. while (n < 39)
  559. channel->scalefac[n++] = 0;
  560. }
  561. else { /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
  562. scalefac_compress >>= 1;
  563. if (scalefac_compress < 180) {
  564. slen[0] = scalefac_compress / 36;
  565. slen[1] = (scalefac_compress % 36) / 6;
  566. slen[2] = (scalefac_compress % 36) % 6;
  567. slen[3] = 0;
  568. nsfb = nsfb_table[3][index];
  569. }
  570. else if (scalefac_compress < 244) {
  571. scalefac_compress -= 180;
  572. slen[0] = (scalefac_compress % 64) >> 4;
  573. slen[1] = (scalefac_compress % 16) >> 2;
  574. slen[2] = scalefac_compress % 4;
  575. slen[3] = 0;
  576. nsfb = nsfb_table[4][index];
  577. }
  578. else {
  579. scalefac_compress -= 244;
  580. slen[0] = scalefac_compress / 3;
  581. slen[1] = scalefac_compress % 3;
  582. slen[2] = 0;
  583. slen[3] = 0;
  584. nsfb = nsfb_table[5][index];
  585. }
  586. n = 0;
  587. for (part = 0; part < 4; ++part) {
  588. unsigned int max, is_pos;
  589. max = (1 << slen[part]) - 1;
  590. for (i = 0; i < nsfb[part]; ++i) {
  591. is_pos = mad_bit_read(ptr, slen[part]);
  592. channel->scalefac[n] = is_pos;
  593. gr1ch->scalefac[n++] = (is_pos == max);
  594. }
  595. }
  596. while (n < 39) {
  597. channel->scalefac[n] = 0;
  598. gr1ch->scalefac[n++] = 0; /* apparently not illegal */
  599. }
  600. }
  601. return mad_bit_length(&start, ptr);
  602. }
  603. /*
  604. * NAME: III_scalefactors()
  605. * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
  606. */
  607. static
  608. unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
  609. struct channel const *gr0ch, unsigned int scfsi)
  610. {
  611. struct mad_bitptr start;
  612. unsigned int slen1, slen2, sfbi;
  613. start = *ptr;
  614. slen1 = sflen_table[channel->scalefac_compress].slen1;
  615. slen2 = sflen_table[channel->scalefac_compress].slen2;
  616. if (channel->block_type == 2) {
  617. unsigned int nsfb;
  618. sfbi = 0;
  619. nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
  620. while (nsfb--)
  621. channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
  622. nsfb = 6 * 3;
  623. while (nsfb--)
  624. channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
  625. nsfb = 1 * 3;
  626. while (nsfb--)
  627. channel->scalefac[sfbi++] = 0;
  628. }
  629. else { /* channel->block_type != 2 */
  630. if (scfsi & 0x8) {
  631. for (sfbi = 0; sfbi < 6; ++sfbi)
  632. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  633. }
  634. else {
  635. for (sfbi = 0; sfbi < 6; ++sfbi)
  636. channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
  637. }
  638. if (scfsi & 0x4) {
  639. for (sfbi = 6; sfbi < 11; ++sfbi)
  640. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  641. }
  642. else {
  643. for (sfbi = 6; sfbi < 11; ++sfbi)
  644. channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
  645. }
  646. if (scfsi & 0x2) {
  647. for (sfbi = 11; sfbi < 16; ++sfbi)
  648. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  649. }
  650. else {
  651. for (sfbi = 11; sfbi < 16; ++sfbi)
  652. channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
  653. }
  654. if (scfsi & 0x1) {
  655. for (sfbi = 16; sfbi < 21; ++sfbi)
  656. channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
  657. }
  658. else {
  659. for (sfbi = 16; sfbi < 21; ++sfbi)
  660. channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
  661. }
  662. channel->scalefac[21] = 0;
  663. }
  664. return mad_bit_length(&start, ptr);
  665. }
  666. /*
  667. * The Layer III formula for requantization and scaling is defined by
  668. * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
  669. *
  670. * long blocks:
  671. * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
  672. * 2^((1/4) * (global_gain - 210)) *
  673. * 2^-(scalefac_multiplier *
  674. * (scalefac_l[sfb] + preflag * pretab[sfb]))
  675. *
  676. * short blocks:
  677. * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
  678. * 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
  679. * 2^-(scalefac_multiplier * scalefac_s[sfb][w])
  680. *
  681. * where:
  682. * scalefac_multiplier = (scalefac_scale + 1) / 2
  683. *
  684. * The routines III_exponents() and III_requantize() facilitate this
  685. * calculation.
  686. */
  687. /*
  688. * NAME: III_exponents()
  689. * DESCRIPTION: calculate scalefactor exponents
  690. */
  691. static
  692. void III_exponents(struct channel const *channel,
  693. unsigned char const *sfbwidth, signed int exponents[39])
  694. {
  695. signed int gain;
  696. unsigned int scalefac_multiplier, sfbi;
  697. gain = (signed int) channel->global_gain - 210;
  698. scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
  699. if (channel->block_type == 2) {
  700. unsigned int l;
  701. signed int gain0, gain1, gain2;
  702. sfbi = l = 0;
  703. if (channel->flags & mixed_block_flag) {
  704. unsigned int premask;
  705. premask = (channel->flags & preflag) ? ~0 : 0;
  706. /* long block subbands 0-1 */
  707. while (l < 36) {
  708. exponents[sfbi] = gain -
  709. (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
  710. scalefac_multiplier);
  711. l += sfbwidth[sfbi++];
  712. }
  713. }
  714. /* this is probably wrong for 8000 Hz short/mixed blocks */
  715. gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
  716. gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
  717. gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
  718. while (l < 576) {
  719. exponents[sfbi + 0] = gain0 -
  720. (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
  721. exponents[sfbi + 1] = gain1 -
  722. (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
  723. exponents[sfbi + 2] = gain2 -
  724. (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
  725. l += 3 * sfbwidth[sfbi];
  726. sfbi += 3;
  727. }
  728. }
  729. else { /* channel->block_type != 2 */
  730. if (channel->flags & preflag) {
  731. for (sfbi = 0; sfbi < 22; ++sfbi) {
  732. exponents[sfbi] = gain -
  733. (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
  734. scalefac_multiplier);
  735. }
  736. }
  737. else {
  738. for (sfbi = 0; sfbi < 22; ++sfbi) {
  739. exponents[sfbi] = gain -
  740. (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
  741. }
  742. }
  743. }
  744. }
  745. /*
  746. * NAME: III_requantize()
  747. * DESCRIPTION: requantize one (positive) value
  748. */
  749. static
  750. mad_fixed_t III_requantize(unsigned int value, signed int exp)
  751. {
  752. mad_fixed_t requantized;
  753. signed int frac;
  754. struct fixedfloat const *power;
  755. frac = exp % 4; /* assumes sign(frac) == sign(exp) */
  756. exp /= 4;
  757. power = &rq_table[value];
  758. requantized = power->mantissa;
  759. exp += power->exponent;
  760. if (exp < 0) {
  761. if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
  762. /* underflow */
  763. requantized = 0;
  764. }
  765. else {
  766. requantized += 1L << (-exp - 1);
  767. requantized >>= -exp;
  768. }
  769. }
  770. else {
  771. if (exp >= 5) {
  772. /* overflow */
  773. # if defined(DEBUG)
  774. fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
  775. mad_f_todouble(requantized), exp);
  776. # endif
  777. requantized = MAD_F_MAX;
  778. }
  779. else
  780. requantized <<= exp;
  781. }
  782. return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
  783. }
  784. /* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
  785. # define MASK(cache, sz, bits) \
  786. (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
  787. # define MASK1BIT(cache, sz) \
  788. ((cache) & (1 << ((sz) - 1)))
  789. /*
  790. * NAME: III_huffdecode()
  791. * DESCRIPTION: decode Huffman code words of one channel of one granule
  792. */
  793. static
  794. enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
  795. struct channel *channel,
  796. unsigned char const *sfbwidth,
  797. unsigned int part2_length)
  798. {
  799. signed int exponents[39], exp;
  800. signed int const *expptr;
  801. struct mad_bitptr peek;
  802. signed int bits_left, cachesz;
  803. register mad_fixed_t *xrptr;
  804. mad_fixed_t const *sfbound;
  805. register unsigned long bitcache;
  806. bits_left = (signed) channel->part2_3_length - (signed) part2_length;
  807. if (bits_left < 0)
  808. return MAD_ERROR_BADPART3LEN;
  809. III_exponents(channel, sfbwidth, exponents);
  810. peek = *ptr;
  811. mad_bit_skip(ptr, bits_left);
  812. /* align bit reads to byte boundaries */
  813. cachesz = mad_bit_bitsleft(&peek);
  814. cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
  815. bitcache = mad_bit_read(&peek, cachesz);
  816. bits_left -= cachesz;
  817. xrptr = &xr[0];
  818. /* big_values */
  819. {
  820. unsigned int region, rcount;
  821. struct hufftable const *entry;
  822. union huffpair const *table;
  823. unsigned int linbits, startbits, big_values, reqhits;
  824. mad_fixed_t reqcache[16];
  825. sfbound = xrptr + *sfbwidth++;
  826. rcount = channel->region0_count + 1;
  827. entry = &mad_huff_pair_table[channel->table_select[region = 0]];
  828. table = entry->table;
  829. linbits = entry->linbits;
  830. startbits = entry->startbits;
  831. if (table == 0)
  832. return MAD_ERROR_BADHUFFTABLE;
  833. expptr = &exponents[0];
  834. exp = *expptr++;
  835. reqhits = 0;
  836. big_values = channel->big_values;
  837. while (big_values-- && cachesz + bits_left > 0) {
  838. union huffpair const *pair;
  839. unsigned int clumpsz, value;
  840. register mad_fixed_t requantized;
  841. if (xrptr == sfbound) {
  842. sfbound += *sfbwidth++;
  843. /* change table if region boundary */
  844. if (--rcount == 0) {
  845. if (region == 0)
  846. rcount = channel->region1_count + 1;
  847. else
  848. rcount = 0; /* all remaining */
  849. entry = &mad_huff_pair_table[channel->table_select[++region]];
  850. table = entry->table;
  851. linbits = entry->linbits;
  852. startbits = entry->startbits;
  853. if (table == 0)
  854. return MAD_ERROR_BADHUFFTABLE;
  855. }
  856. if (exp != *expptr) {
  857. exp = *expptr;
  858. reqhits = 0;
  859. }
  860. ++expptr;
  861. }
  862. if (cachesz < 21) {
  863. unsigned int bits;
  864. bits = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
  865. bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
  866. cachesz += bits;
  867. bits_left -= bits;
  868. }
  869. /* hcod (0..19) */
  870. clumpsz = startbits;
  871. pair = &table[MASK(bitcache, cachesz, clumpsz)];
  872. while (!pair->final) {
  873. cachesz -= clumpsz;
  874. clumpsz = pair->ptr.bits;
  875. pair = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
  876. }
  877. cachesz -= pair->value.hlen;
  878. if (linbits) {
  879. /* x (0..14) */
  880. value = pair->value.x;
  881. switch (value) {
  882. case 0:
  883. xrptr[0] = 0;
  884. break;
  885. case 15:
  886. if (cachesz < linbits + 2) {
  887. bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  888. cachesz += 16;
  889. bits_left -= 16;
  890. }
  891. value += MASK(bitcache, cachesz, linbits);
  892. cachesz -= linbits;
  893. requantized = III_requantize(value, exp);
  894. goto x_final;
  895. default:
  896. if (reqhits & (1 << value))
  897. requantized = reqcache[value];
  898. else {
  899. reqhits |= (1 << value);
  900. requantized = reqcache[value] = III_requantize(value, exp);
  901. }
  902. x_final:
  903. xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
  904. -requantized : requantized;
  905. }
  906. /* y (0..14) */
  907. value = pair->value.y;
  908. switch (value) {
  909. case 0:
  910. xrptr[1] = 0;
  911. break;
  912. case 15:
  913. if (cachesz < linbits + 1) {
  914. bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  915. cachesz += 16;
  916. bits_left -= 16;
  917. }
  918. value += MASK(bitcache, cachesz, linbits);
  919. cachesz -= linbits;
  920. requantized = III_requantize(value, exp);
  921. goto y_final;
  922. default:
  923. if (reqhits & (1 << value))
  924. requantized = reqcache[value];
  925. else {
  926. reqhits |= (1 << value);
  927. requantized = reqcache[value] = III_requantize(value, exp);
  928. }
  929. y_final:
  930. xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
  931. -requantized : requantized;
  932. }
  933. }
  934. else {
  935. /* x (0..1) */
  936. value = pair->value.x;
  937. if (value == 0)
  938. xrptr[0] = 0;
  939. else {
  940. if (reqhits & (1 << value))
  941. requantized = reqcache[value];
  942. else {
  943. reqhits |= (1 << value);
  944. requantized = reqcache[value] = III_requantize(value, exp);
  945. }
  946. xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
  947. -requantized : requantized;
  948. }
  949. /* y (0..1) */
  950. value = pair->value.y;
  951. if (value == 0)
  952. xrptr[1] = 0;
  953. else {
  954. if (reqhits & (1 << value))
  955. requantized = reqcache[value];
  956. else {
  957. reqhits |= (1 << value);
  958. requantized = reqcache[value] = III_requantize(value, exp);
  959. }
  960. xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
  961. -requantized : requantized;
  962. }
  963. }
  964. xrptr += 2;
  965. }
  966. }
  967. if (cachesz + bits_left < 0)
  968. return MAD_ERROR_BADHUFFDATA; /* big_values overrun */
  969. /* count1 */
  970. {
  971. union huffquad const *table;
  972. register mad_fixed_t requantized;
  973. table = mad_huff_quad_table[channel->flags & count1table_select];
  974. requantized = III_requantize(1, exp);
  975. while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
  976. union huffquad const *quad;
  977. /* hcod (1..6) */
  978. if (cachesz < 10) {
  979. bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
  980. cachesz += 16;
  981. bits_left -= 16;
  982. }
  983. quad = &table[MASK(bitcache, cachesz, 4)];
  984. /* quad tables guaranteed to have at most one extra lookup */
  985. if (!quad->final) {
  986. cachesz -= 4;
  987. quad = &table[quad->ptr.offset +
  988. MASK(bitcache, cachesz, quad->ptr.bits)];
  989. }
  990. cachesz -= quad->value.hlen;
  991. if (xrptr == sfbound) {
  992. sfbound += *sfbwidth++;
  993. if (exp != *expptr) {
  994. exp = *expptr;
  995. requantized = III_requantize(1, exp);
  996. }
  997. ++expptr;
  998. }
  999. /* v (0..1) */
  1000. xrptr[0] = quad->value.v ?
  1001. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1002. /* w (0..1) */
  1003. xrptr[1] = quad->value.w ?
  1004. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1005. xrptr += 2;
  1006. if (xrptr == sfbound) {
  1007. sfbound += *sfbwidth++;
  1008. if (exp != *expptr) {
  1009. exp = *expptr;
  1010. requantized = III_requantize(1, exp);
  1011. }
  1012. ++expptr;
  1013. }
  1014. /* x (0..1) */
  1015. xrptr[0] = quad->value.x ?
  1016. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1017. /* y (0..1) */
  1018. xrptr[1] = quad->value.y ?
  1019. (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
  1020. xrptr += 2;
  1021. }
  1022. if (cachesz + bits_left < 0) {
  1023. # if 0 && defined(DEBUG)
  1024. fprintf(stderr, "huffman count1 overrun (%d bits)\n",
  1025. -(cachesz + bits_left));
  1026. # endif
  1027. /* technically the bitstream is misformatted, but apparently
  1028. some encoders are just a bit sloppy with stuffing bits */
  1029. xrptr -= 4;
  1030. }
  1031. }
  1032. assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
  1033. # if 0 && defined(DEBUG)
  1034. if (bits_left < 0)
  1035. fprintf(stderr, "read %d bits too many\n", -bits_left);
  1036. else if (cachesz + bits_left > 0)
  1037. fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
  1038. # endif
  1039. /* rzero */
  1040. while (xrptr < &xr[576]) {
  1041. xrptr[0] = 0;
  1042. xrptr[1] = 0;
  1043. xrptr += 2;
  1044. }
  1045. return MAD_ERROR_NONE;
  1046. }
  1047. # undef MASK
  1048. # undef MASK1BIT
  1049. /*
  1050. * NAME: III_reorder()
  1051. * DESCRIPTION: reorder frequency lines of a short block into subband order
  1052. */
  1053. static
  1054. void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
  1055. unsigned char const sfbwidth[39])
  1056. {
  1057. mad_fixed_t tmp[32][3][6];
  1058. unsigned int sb, l, f, w, sbw[3], sw[3];
  1059. /* this is probably wrong for 8000 Hz mixed blocks */
  1060. sb = 0;
  1061. if (channel->flags & mixed_block_flag) {
  1062. sb = 2;
  1063. l = 0;
  1064. while (l < 36)
  1065. l += *sfbwidth++;
  1066. }
  1067. for (w = 0; w < 3; ++w) {
  1068. sbw[w] = sb;
  1069. sw[w] = 0;
  1070. }
  1071. f = *sfbwidth++;
  1072. w = 0;
  1073. for (l = 18 * sb; l < 576; ++l) {
  1074. if (f-- == 0) {
  1075. f = *sfbwidth++ - 1;
  1076. w = (w + 1) % 3;
  1077. }
  1078. tmp[sbw[w]][w][sw[w]++] = xr[l];
  1079. if (sw[w] == 6) {
  1080. sw[w] = 0;
  1081. ++sbw[w];
  1082. }
  1083. }
  1084. memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
  1085. }
  1086. /*
  1087. * NAME: III_stereo()
  1088. * DESCRIPTION: perform joint stereo processing on a granule
  1089. */
  1090. static
  1091. enum mad_error III_stereo(mad_fixed_t xr[2][576],
  1092. struct granule const *granule,
  1093. struct mad_header *header,
  1094. unsigned char const *sfbwidth)
  1095. {
  1096. short modes[39];
  1097. unsigned int sfbi, l, n, i;
  1098. if (granule->ch[0].block_type !=
  1099. granule->ch[1].block_type ||
  1100. (granule->ch[0].flags & mixed_block_flag) !=
  1101. (granule->ch[1].flags & mixed_block_flag))
  1102. return MAD_ERROR_BADSTEREO;
  1103. for (i = 0; i < 39; ++i)
  1104. modes[i] = header->mode_extension;
  1105. /* intensity stereo */
  1106. if (header->mode_extension & I_STEREO) {
  1107. struct channel const *right_ch = &granule->ch[1];
  1108. mad_fixed_t const *right_xr = xr[1];
  1109. unsigned int is_pos;
  1110. header->flags |= MAD_FLAG_I_STEREO;
  1111. /* first determine which scalefactor bands are to be processed */
  1112. if (right_ch->block_type == 2) {
  1113. unsigned int lower, start, max, bound[3], w;
  1114. lower = start = max = bound[0] = bound[1] = bound[2] = 0;
  1115. sfbi = l = 0;
  1116. if (right_ch->flags & mixed_block_flag) {
  1117. while (l < 36) {
  1118. n = sfbwidth[sfbi++];
  1119. for (i = 0; i < n; ++i) {
  1120. if (right_xr[i]) {
  1121. lower = sfbi;
  1122. break;
  1123. }
  1124. }
  1125. right_xr += n;
  1126. l += n;
  1127. }
  1128. start = sfbi;
  1129. }
  1130. w = 0;
  1131. while (l < 576) {
  1132. n = sfbwidth[sfbi++];
  1133. for (i = 0; i < n; ++i) {
  1134. if (right_xr[i]) {
  1135. max = bound[w] = sfbi;
  1136. break;
  1137. }
  1138. }
  1139. right_xr += n;
  1140. l += n;
  1141. w = (w + 1) % 3;
  1142. }
  1143. if (max)
  1144. lower = start;
  1145. /* long blocks */
  1146. for (i = 0; i < lower; ++i)
  1147. modes[i] = header->mode_extension & ~I_STEREO;
  1148. /* short blocks */
  1149. w = 0;
  1150. for (i = start; i < max; ++i) {
  1151. if (i < bound[w])
  1152. modes[i] = header->mode_extension & ~I_STEREO;
  1153. w = (w + 1) % 3;
  1154. }
  1155. }
  1156. else { /* right_ch->block_type != 2 */
  1157. unsigned int bound;
  1158. bound = 0;
  1159. for (sfbi = l = 0; l < 576; l += n) {
  1160. n = sfbwidth[sfbi++];
  1161. for (i = 0; i < n; ++i) {
  1162. if (right_xr[i]) {
  1163. bound = sfbi;
  1164. break;
  1165. }
  1166. }
  1167. right_xr += n;
  1168. }
  1169. for (i = 0; i < bound; ++i)
  1170. modes[i] = header->mode_extension & ~I_STEREO;
  1171. }
  1172. /* now do the actual processing */
  1173. if (header->flags & MAD_FLAG_LSF_EXT) {
  1174. unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
  1175. mad_fixed_t const *lsf_scale;
  1176. /* intensity_scale */
  1177. lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
  1178. for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
  1179. n = sfbwidth[sfbi];
  1180. if (!(modes[sfbi] & I_STEREO))
  1181. continue;
  1182. if (illegal_pos[sfbi]) {
  1183. modes[sfbi] &= ~I_STEREO;
  1184. continue;
  1185. }
  1186. is_pos = right_ch->scalefac[sfbi];
  1187. for (i = 0; i < n; ++i) {
  1188. register mad_fixed_t left;
  1189. left = xr[0][l + i];
  1190. if (is_pos == 0)
  1191. xr[1][l + i] = left;
  1192. else {
  1193. register mad_fixed_t opposite;
  1194. opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
  1195. if (is_pos & 1) {
  1196. xr[0][l + i] = opposite;
  1197. xr[1][l + i] = left;
  1198. }
  1199. else
  1200. xr[1][l + i] = opposite;
  1201. }
  1202. }
  1203. }
  1204. }
  1205. else { /* !(header->flags & MAD_FLAG_LSF_EXT) */
  1206. for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
  1207. n = sfbwidth[sfbi];
  1208. if (!(modes[sfbi] & I_STEREO))
  1209. continue;
  1210. is_pos = right_ch->scalefac[sfbi];
  1211. if (is_pos >= 7) { /* illegal intensity position */
  1212. modes[sfbi] &= ~I_STEREO;
  1213. continue;
  1214. }
  1215. for (i = 0; i < n; ++i) {
  1216. register mad_fixed_t left;
  1217. left = xr[0][l + i];
  1218. xr[0][l + i] = mad_f_mul(left, is_table[ is_pos]);
  1219. xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]);
  1220. }
  1221. }
  1222. }
  1223. }
  1224. /* middle/side stereo */
  1225. if (header->mode_extension & MS_STEREO) {
  1226. register mad_fixed_t invsqrt2;
  1227. header->flags |= MAD_FLAG_MS_STEREO;
  1228. invsqrt2 = root_table[3 + -2];
  1229. for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
  1230. n = sfbwidth[sfbi];
  1231. if (modes[sfbi] != MS_STEREO)
  1232. continue;
  1233. for (i = 0; i < n; ++i) {
  1234. register mad_fixed_t m, s;
  1235. m = xr[0][l + i];
  1236. s = xr[1][l + i];
  1237. xr[0][l + i] = mad_f_mul(m + s, invsqrt2); /* l = (m + s) / sqrt(2) */
  1238. xr[1][l + i] = mad_f_mul(m - s, invsqrt2); /* r = (m - s) / sqrt(2) */
  1239. }
  1240. }
  1241. }
  1242. return MAD_ERROR_NONE;
  1243. }
  1244. /*
  1245. * NAME: III_aliasreduce()
  1246. * DESCRIPTION: perform frequency line alias reduction
  1247. */
  1248. static
  1249. void III_aliasreduce(mad_fixed_t xr[576], int lines)
  1250. {
  1251. mad_fixed_t const *bound;
  1252. int i;
  1253. bound = &xr[lines];
  1254. for (xr += 18; xr < bound; xr += 18) {
  1255. for (i = 0; i < 8; ++i) {
  1256. register mad_fixed_t a, b;
  1257. register mad_fixed64hi_t hi;
  1258. register mad_fixed64lo_t lo;
  1259. a = xr[-1 - i];
  1260. b = xr[ i];
  1261. # if defined(ASO_ZEROCHECK)
  1262. if (a | b) {
  1263. # endif
  1264. MAD_F_ML0(hi, lo, a, cs[i]);
  1265. MAD_F_MLA(hi, lo, -b, ca[i]);
  1266. xr[-1 - i] = MAD_F_MLZ(hi, lo);
  1267. MAD_F_ML0(hi, lo, b, cs[i]);
  1268. MAD_F_MLA(hi, lo, a, ca[i]);
  1269. xr[ i] = MAD_F_MLZ(hi, lo);
  1270. # if defined(ASO_ZEROCHECK)
  1271. }
  1272. # endif
  1273. }
  1274. }
  1275. }
  1276. # if defined(ASO_IMDCT)
  1277. void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
  1278. # else
  1279. # if 1
  1280. static
  1281. void fastsdct(mad_fixed_t const x[9], mad_fixed_t y[18])
  1282. {
  1283. mad_fixed_t a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
  1284. mad_fixed_t a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25;
  1285. mad_fixed_t m0, m1, m2, m3, m4, m5, m6, m7;
  1286. enum {
  1287. c0 = MAD_F(0x1f838b8d), /* 2 * cos( 1 * PI / 18) */
  1288. c1 = MAD_F(0x1bb67ae8), /* 2 * cos( 3 * PI / 18) */
  1289. c2 = MAD_F(0x18836fa3), /* 2 * cos( 4 * PI / 18) */
  1290. c3 = MAD_F(0x1491b752), /* 2 * cos( 5 * PI / 18) */
  1291. c4 = MAD_F(0x0af1d43a), /* 2 * cos( 7 * PI / 18) */
  1292. c5 = MAD_F(0x058e86a0), /* 2 * cos( 8 * PI / 18) */
  1293. c6 = -MAD_F(0x1e11f642) /* 2 * cos(16 * PI / 18) */
  1294. };
  1295. a0 = x[3] + x[5];
  1296. a1 = x[3] - x[5];
  1297. a2 = x[6] + x[2];
  1298. a3 = x[6] - x[2];
  1299. a4 = x[1] + x[7];
  1300. a5 = x[1] - x[7];
  1301. a6 = x[8] + x[0];
  1302. a7 = x[8] - x[0];
  1303. a8 = a0 + a2;
  1304. a9 = a0 - a2;
  1305. a10 = a0 - a6;
  1306. a11 = a2 - a6;
  1307. a12 = a8 + a6;
  1308. a13 = a1 - a3;
  1309. a14 = a13 + a7;
  1310. a15 = a3 + a7;
  1311. a16 = a1 - a7;
  1312. a17 = a1 + a3;
  1313. m0 = mad_f_mul(a17, -c3);
  1314. m1 = mad_f_mul(a16, -c0);
  1315. m2 = mad_f_mul(a15, -c4);
  1316. m3 = mad_f_mul(a14, -c1);
  1317. m4 = mad_f_mul(a5, -c1);
  1318. m5 = mad_f_mul(a11, -c6);
  1319. m6 = mad_f_mul(a10, -c5);
  1320. m7 = mad_f_mul(a9, -c2);
  1321. a18 = x[4] + a4;
  1322. a19 = 2 * x[4] - a4;
  1323. a20 = a19 + m5;
  1324. a21 = a19 - m5;
  1325. a22 = a19 + m6;
  1326. a23 = m4 + m2;
  1327. a24 = m4 - m2;
  1328. a25 = m4 + m1;
  1329. /* output to every other slot for convenience */
  1330. y[ 0] = a18 + a12;
  1331. y[ 2] = m0 - a25;
  1332. y[ 4] = m7 - a20;
  1333. y[ 6] = m3;
  1334. y[ 8] = a21 - m6;
  1335. y[10] = a24 - m1;
  1336. y[12] = a12 - 2 * a18;
  1337. y[14] = a23 + m0;
  1338. y[16] = a22 + m7;
  1339. }
  1340. static inline
  1341. void sdctII(mad_fixed_t const x[18], mad_fixed_t X[18])
  1342. {
  1343. mad_fixed_t tmp[9];
  1344. int i;
  1345. /* scale[i] = 2 * cos(PI * (2 * i + 1) / (2 * 18)) */
  1346. static mad_fixed_t const scale[9] = {
  1347. MAD_F(0x1fe0d3b4), MAD_F(0x1ee8dd47), MAD_F(0x1d007930),
  1348. MAD_F(0x1a367e59), MAD_F(0x16a09e66), MAD_F(0x125abcf8),
  1349. MAD_F(0x0d8616bc), MAD_F(0x08483ee1), MAD_F(0x02c9fad7)
  1350. };
  1351. /* divide the 18-point SDCT-II into two 9-point SDCT-IIs */
  1352. /* even input butterfly */
  1353. for (i = 0; i < 9; i += 3) {
  1354. tmp[i + 0] = x[i + 0] + x[18 - (i + 0) - 1];
  1355. tmp[i + 1] = x[i + 1] + x[18 - (i + 1) - 1];
  1356. tmp[i + 2] = x[i + 2] + x[18 - (i + 2) - 1];
  1357. }
  1358. fastsdct(tmp, &X[0]);
  1359. /* odd input butterfly and scaling */
  1360. for (i = 0; i < 9; i += 3) {
  1361. tmp[i + 0] = mad_f_mul(x[i + 0] - x[18 - (i + 0) - 1], scale[i + 0]);
  1362. tmp[i + 1] = mad_f_mul(x[i + 1] - x[18 - (i + 1) - 1], scale[i + 1]);
  1363. tmp[i + 2] = mad_f_mul(x[i + 2] - x[18 - (i + 2) - 1], scale[i + 2]);
  1364. }
  1365. fastsdct(tmp, &X[1]);
  1366. /* output accumulation */
  1367. for (i = 3; i < 18; i += 8) {
  1368. X[i + 0] -= X[(i + 0) - 2];
  1369. X[i + 2] -= X[(i + 2) - 2];
  1370. X[i + 4] -= X[(i + 4) - 2];
  1371. X[i + 6] -= X[(i + 6) - 2];
  1372. }
  1373. }
  1374. static inline
  1375. void dctIV(mad_fixed_t const y[18], mad_fixed_t X[18])
  1376. {
  1377. mad_fixed_t tmp[18];
  1378. int i;
  1379. /* scale[i] = 2 * cos(PI * (2 * i + 1) / (4 * 18)) */
  1380. static mad_fixed_t const scale[18] = {
  1381. MAD_F(0x1ff833fa), MAD_F(0x1fb9ea93), MAD_F(0x1f3dd120),
  1382. MAD_F(0x1e84d969), MAD_F(0x1d906bcf), MAD_F(0x1c62648b),
  1383. MAD_F(0x1afd100f), MAD_F(0x1963268b), MAD_F(0x1797c6a4),
  1384. MAD_F(0x159e6f5b), MAD_F(0x137af940), MAD_F(0x11318ef3),
  1385. MAD_F(0x0ec6a507), MAD_F(0x0c3ef153), MAD_F(0x099f61c5),
  1386. MAD_F(0x06ed12c5), MAD_F(0x042d4544), MAD_F(0x0165547c)
  1387. };
  1388. /* scaling */
  1389. for (i = 0; i < 18; i += 3) {
  1390. tmp[i + 0] = mad_f_mul(y[i + 0], scale[i + 0]);
  1391. tmp[i + 1] = mad_f_mul(y[i + 1], scale[i + 1]);
  1392. tmp[i + 2] = mad_f_mul(y[i + 2], scale[i + 2]);
  1393. }
  1394. /* SDCT-II */
  1395. sdctII(tmp, X);
  1396. /* scale reduction and output accumulation */
  1397. X[0] /= 2;
  1398. for (i = 1; i < 17; i += 4) {
  1399. X[i + 0] = X[i + 0] / 2 - X[(i + 0) - 1];
  1400. X[i + 1] = X[i + 1] / 2 - X[(i + 1) - 1];
  1401. X[i + 2] = X[i + 2] / 2 - X[(i + 2) - 1];
  1402. X[i + 3] = X[i + 3] / 2 - X[(i + 3) - 1];
  1403. }
  1404. X[17] = X[17] / 2 - X[16];
  1405. }
  1406. /*
  1407. * NAME: imdct36
  1408. * DESCRIPTION: perform X[18]->x[36] IMDCT using Szu-Wei Lee's fast algorithm
  1409. */
  1410. static inline
  1411. void imdct36(mad_fixed_t const x[18], mad_fixed_t y[36])
  1412. {
  1413. mad_fixed_t tmp[18];
  1414. int i;
  1415. /* DCT-IV */
  1416. dctIV(x, tmp);
  1417. /* convert 18-point DCT-IV to 36-point IMDCT */
  1418. for (i = 0; i < 9; i += 3) {
  1419. y[i + 0] = tmp[9 + (i + 0)];
  1420. y[i + 1] = tmp[9 + (i + 1)];
  1421. y[i + 2] = tmp[9 + (i + 2)];
  1422. }
  1423. for (i = 9; i < 27; i += 3) {
  1424. y[i + 0] = -tmp[36 - (9 + (i + 0)) - 1];
  1425. y[i + 1] = -tmp[36 - (9 + (i + 1)) - 1];
  1426. y[i + 2] = -tmp[36 - (9 + (i + 2)) - 1];
  1427. }
  1428. for (i = 27; i < 36; i += 3) {
  1429. y[i + 0] = -tmp[(i + 0) - 27];
  1430. y[i + 1] = -tmp[(i + 1) - 27];
  1431. y[i + 2] = -tmp[(i + 2) - 27];
  1432. }
  1433. }
  1434. # else
  1435. /*
  1436. * NAME: imdct36
  1437. * DESCRIPTION: perform X[18]->x[36] IMDCT
  1438. */
  1439. static inline
  1440. void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
  1441. {
  1442. mad_fixed_t t0, t1, t2, t3, t4, t5, t6, t7;
  1443. mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
  1444. register mad_fixed64hi_t hi;
  1445. register mad_fixed64lo_t lo;
  1446. MAD_F_ML0(hi, lo, X[4], MAD_F(0x0ec835e8));
  1447. MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
  1448. t6 = MAD_F_MLZ(hi, lo);
  1449. MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
  1450. MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
  1451. t0 = MAD_F_MLZ(hi, lo);
  1452. MAD_F_MLA(hi, lo, (t8 = X[0] - X[11] - X[12]), MAD_F(0x0216a2a2));
  1453. MAD_F_MLA(hi, lo, (t9 = X[2] - X[9] - X[14]), MAD_F(0x09bd7ca0));
  1454. MAD_F_MLA(hi, lo, (t10 = X[3] - X[8] - X[15]), -MAD_F(0x0cb19346));
  1455. MAD_F_MLA(hi, lo, (t11 = X[5] - X[6] - X[17]), -MAD_F(0x0fdcf549));
  1456. x[7] = MAD_F_MLZ(hi, lo);
  1457. x[10] = -x[7];
  1458. MAD_F_ML0(hi, lo, t8, -MAD_F(0x0cb19346));
  1459. MAD_F_MLA(hi, lo, t9, MAD_F(0x0fdcf549));
  1460. MAD_F_MLA(hi, lo, t10, MAD_F(0x0216a2a2));
  1461. MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
  1462. x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
  1463. t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
  1464. t13 = X[2] + X[5] - X[6] - X[9] - X[14] - X[17];
  1465. MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
  1466. MAD_F_MLA(hi, lo, t13, MAD_F(0x061f78aa));
  1467. x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
  1468. MAD_F_ML0(hi, lo, X[1], -MAD_F(0x09bd7ca0));
  1469. MAD_F_MLA(hi, lo, X[7], MAD_F(0x0216a2a2));
  1470. MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
  1471. MAD_F_MLA(hi, lo, X[16], MAD_F(0x0cb19346));
  1472. t1 = MAD_F_MLZ(hi, lo) + t6;
  1473. MAD_F_ML0(hi, lo, X[0], MAD_F(0x03768962));
  1474. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0e313245));
  1475. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0ffc19fd));
  1476. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0acf37ad));
  1477. MAD_F_MLA(hi, lo, X[6], MAD_F(0x04cfb0e2));
  1478. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0898c779));
  1479. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0d7e8807));
  1480. MAD_F_MLA(hi, lo, X[11], MAD_F(0x0f426cb5));
  1481. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
  1482. MAD_F_MLA(hi, lo, X[14], MAD_F(0x00b2aa3e));
  1483. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
  1484. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
  1485. x[6] = MAD_F_MLZ(hi, lo) + t1;
  1486. x[11] = -x[6];
  1487. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0f426cb5));
  1488. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x00b2aa3e));
  1489. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0898c779));
  1490. MAD_F_MLA(hi, lo, X[5], MAD_F(0x0f9ee890));
  1491. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0acf37ad));
  1492. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x07635284));
  1493. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0e313245));
  1494. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
  1495. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
  1496. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0d7e8807));
  1497. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0ffc19fd));
  1498. MAD_F_MLA(hi, lo, X[17], MAD_F(0x04cfb0e2));
  1499. x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
  1500. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0bcbe352));
  1501. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0d7e8807));
  1502. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x07635284));
  1503. MAD_F_MLA(hi, lo, X[5], MAD_F(0x04cfb0e2));
  1504. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0f9ee890));
  1505. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0ffc19fd));
  1506. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x00b2aa3e));
  1507. MAD_F_MLA(hi, lo, X[11], MAD_F(0x03768962));
  1508. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
  1509. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0e313245));
  1510. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0898c779));
  1511. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
  1512. x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
  1513. MAD_F_ML0(hi, lo, X[4], MAD_F(0x061f78aa));
  1514. MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
  1515. t7 = MAD_F_MLZ(hi, lo);
  1516. MAD_F_MLA(hi, lo, X[1], -MAD_F(0x0cb19346));
  1517. MAD_F_MLA(hi, lo, X[7], MAD_F(0x0fdcf549));
  1518. MAD_F_MLA(hi, lo, X[10], MAD_F(0x0216a2a2));
  1519. MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
  1520. t2 = MAD_F_MLZ(hi, lo);
  1521. MAD_F_MLA(hi, lo, X[0], MAD_F(0x04cfb0e2));
  1522. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0ffc19fd));
  1523. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0d7e8807));
  1524. MAD_F_MLA(hi, lo, X[5], MAD_F(0x03768962));
  1525. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0bcbe352));
  1526. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0e313245));
  1527. MAD_F_MLA(hi, lo, X[9], MAD_F(0x07635284));
  1528. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
  1529. MAD_F_MLA(hi, lo, X[12], MAD_F(0x0f9ee890));
  1530. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0898c779));
  1531. MAD_F_MLA(hi, lo, X[15], MAD_F(0x00b2aa3e));
  1532. MAD_F_MLA(hi, lo, X[17], MAD_F(0x0f426cb5));
  1533. x[5] = MAD_F_MLZ(hi, lo);
  1534. x[12] = -x[5];
  1535. MAD_F_ML0(hi, lo, X[0], MAD_F(0x0acf37ad));
  1536. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x0898c779));
  1537. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0e313245));
  1538. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0f426cb5));
  1539. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x03768962));
  1540. MAD_F_MLA(hi, lo, X[8], MAD_F(0x00b2aa3e));
  1541. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0ffc19fd));
  1542. MAD_F_MLA(hi, lo, X[11], MAD_F(0x0f9ee890));
  1543. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
  1544. MAD_F_MLA(hi, lo, X[14], MAD_F(0x07635284));
  1545. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0d7e8807));
  1546. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
  1547. x[0] = MAD_F_MLZ(hi, lo) + t2;
  1548. x[17] = -x[0];
  1549. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0f9ee890));
  1550. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x07635284));
  1551. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x00b2aa3e));
  1552. MAD_F_MLA(hi, lo, X[5], MAD_F(0x0bcbe352));
  1553. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0f426cb5));
  1554. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0d7e8807));
  1555. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0898c779));
  1556. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
  1557. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
  1558. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
  1559. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
  1560. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
  1561. x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
  1562. MAD_F_ML0(hi, lo, X[1], -MAD_F(0x0216a2a2));
  1563. MAD_F_MLA(hi, lo, X[7], -MAD_F(0x09bd7ca0));
  1564. MAD_F_MLA(hi, lo, X[10], MAD_F(0x0cb19346));
  1565. MAD_F_MLA(hi, lo, X[16], MAD_F(0x0fdcf549));
  1566. t3 = MAD_F_MLZ(hi, lo) + t7;
  1567. MAD_F_ML0(hi, lo, X[0], MAD_F(0x00b2aa3e));
  1568. MAD_F_MLA(hi, lo, X[2], MAD_F(0x03768962));
  1569. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x04cfb0e2));
  1570. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x07635284));
  1571. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0898c779));
  1572. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0acf37ad));
  1573. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0bcbe352));
  1574. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
  1575. MAD_F_MLA(hi, lo, X[12], MAD_F(0x0e313245));
  1576. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0f426cb5));
  1577. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
  1578. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
  1579. x[8] = MAD_F_MLZ(hi, lo) + t3;
  1580. x[9] = -x[8];
  1581. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0e313245));
  1582. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0bcbe352));
  1583. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0f9ee890));
  1584. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0898c779));
  1585. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0ffc19fd));
  1586. MAD_F_MLA(hi, lo, X[8], MAD_F(0x04cfb0e2));
  1587. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0f426cb5));
  1588. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
  1589. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
  1590. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
  1591. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0acf37ad));
  1592. MAD_F_MLA(hi, lo, X[17], MAD_F(0x07635284));
  1593. x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
  1594. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0d7e8807));
  1595. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0f426cb5));
  1596. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0acf37ad));
  1597. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0ffc19fd));
  1598. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x07635284));
  1599. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0f9ee890));
  1600. MAD_F_MLA(hi, lo, X[9], MAD_F(0x03768962));
  1601. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
  1602. MAD_F_MLA(hi, lo, X[12], MAD_F(0x00b2aa3e));
  1603. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0bcbe352));
  1604. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
  1605. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
  1606. x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
  1607. MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
  1608. MAD_F_MLA(hi, lo, t15, MAD_F(0x061f78aa));
  1609. t4 = MAD_F_MLZ(hi, lo) - t7;
  1610. MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
  1611. MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
  1612. x[4] = MAD_F_MLZ(hi, lo) + t4;
  1613. x[13] = -x[4];
  1614. MAD_F_ML0(hi, lo, t8, MAD_F(0x09bd7ca0));
  1615. MAD_F_MLA(hi, lo, t9, -MAD_F(0x0216a2a2));
  1616. MAD_F_MLA(hi, lo, t10, MAD_F(0x0fdcf549));
  1617. MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
  1618. x[1] = MAD_F_MLZ(hi, lo) + t4;
  1619. x[16] = -x[1];
  1620. MAD_F_ML0(hi, lo, t8, -MAD_F(0x0fdcf549));
  1621. MAD_F_MLA(hi, lo, t9, -MAD_F(0x0cb19346));
  1622. MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
  1623. MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
  1624. x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
  1625. MAD_F_ML0(hi, lo, X[1], -MAD_F(0x0fdcf549));
  1626. MAD_F_MLA(hi, lo, X[7], -MAD_F(0x0cb19346));
  1627. MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
  1628. MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
  1629. t5 = MAD_F_MLZ(hi, lo) - t6;
  1630. MAD_F_ML0(hi, lo, X[0], MAD_F(0x0898c779));
  1631. MAD_F_MLA(hi, lo, X[2], MAD_F(0x04cfb0e2));
  1632. MAD_F_MLA(hi, lo, X[3], MAD_F(0x0bcbe352));
  1633. MAD_F_MLA(hi, lo, X[5], MAD_F(0x00b2aa3e));
  1634. MAD_F_MLA(hi, lo, X[6], MAD_F(0x0e313245));
  1635. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x03768962));
  1636. MAD_F_MLA(hi, lo, X[9], MAD_F(0x0f9ee890));
  1637. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
  1638. MAD_F_MLA(hi, lo, X[12], MAD_F(0x0ffc19fd));
  1639. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
  1640. MAD_F_MLA(hi, lo, X[15], MAD_F(0x0f426cb5));
  1641. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
  1642. x[2] = MAD_F_MLZ(hi, lo) + t5;
  1643. x[15] = -x[2];
  1644. MAD_F_ML0(hi, lo, X[0], MAD_F(0x07635284));
  1645. MAD_F_MLA(hi, lo, X[2], MAD_F(0x0acf37ad));
  1646. MAD_F_MLA(hi, lo, X[3], MAD_F(0x03768962));
  1647. MAD_F_MLA(hi, lo, X[5], MAD_F(0x0d7e8807));
  1648. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x00b2aa3e));
  1649. MAD_F_MLA(hi, lo, X[8], MAD_F(0x0f426cb5));
  1650. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x04cfb0e2));
  1651. MAD_F_MLA(hi, lo, X[11], MAD_F(0x0ffc19fd));
  1652. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
  1653. MAD_F_MLA(hi, lo, X[14], MAD_F(0x0f9ee890));
  1654. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
  1655. MAD_F_MLA(hi, lo, X[17], MAD_F(0x0e313245));
  1656. x[3] = MAD_F_MLZ(hi, lo) + t5;
  1657. x[14] = -x[3];
  1658. MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0ffc19fd));
  1659. MAD_F_MLA(hi, lo, X[2], -MAD_F(0x0f9ee890));
  1660. MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0f426cb5));
  1661. MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0e313245));
  1662. MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0d7e8807));
  1663. MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0bcbe352));
  1664. MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0acf37ad));
  1665. MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
  1666. MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
  1667. MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
  1668. MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
  1669. MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
  1670. x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
  1671. }
  1672. # endif
  1673. /*
  1674. * NAME: III_imdct_l()
  1675. * DESCRIPTION: perform IMDCT and windowing for long blocks
  1676. */
  1677. static
  1678. void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
  1679. unsigned int block_type)
  1680. {
  1681. unsigned int i;
  1682. /* IMDCT */
  1683. imdct36(X, z);
  1684. /* windowing */
  1685. switch (block_type) {
  1686. case 0: /* normal window */
  1687. # if defined(ASO_INTERLEAVE1)
  1688. {
  1689. register mad_fixed_t tmp1, tmp2;
  1690. tmp1 = window_l[0];
  1691. tmp2 = window_l[1];
  1692. for (i = 0; i < 34; i += 2) {
  1693. z[i + 0] = mad_f_mul(z[i + 0], tmp1);
  1694. tmp1 = window_l[i + 2];
  1695. z[i + 1] = mad_f_mul(z[i + 1], tmp2);
  1696. tmp2 = window_l[i + 3];
  1697. }
  1698. z[34] = mad_f_mul(z[34], tmp1);
  1699. z[35] = mad_f_mul(z[35], tmp2);
  1700. }
  1701. # elif defined(ASO_INTERLEAVE2)
  1702. {
  1703. register mad_fixed_t tmp1, tmp2;
  1704. tmp1 = z[0];
  1705. tmp2 = window_l[0];
  1706. for (i = 0; i < 35; ++i) {
  1707. z[i] = mad_f_mul(tmp1, tmp2);
  1708. tmp1 = z[i + 1];
  1709. tmp2 = window_l[i + 1];
  1710. }
  1711. z[35] = mad_f_mul(tmp1, tmp2);
  1712. }
  1713. # elif 1
  1714. for (i = 0; i < 36; i += 4) {
  1715. z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
  1716. z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
  1717. z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
  1718. z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
  1719. }
  1720. # else
  1721. for (i = 0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
  1722. # endif
  1723. break;
  1724. case 1: /* start block */
  1725. for (i = 0; i < 18; i += 3) {
  1726. z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
  1727. z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
  1728. z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
  1729. }
  1730. /* (i = 18; i < 24; ++i) z[i] unchanged */
  1731. for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
  1732. for (i = 30; i < 36; ++i) z[i] = 0;
  1733. break;
  1734. case 3: /* stop block */
  1735. for (i = 0; i < 6; ++i) z[i] = 0;
  1736. for (i = 6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
  1737. /* (i = 12; i < 18; ++i) z[i] unchanged */
  1738. for (i = 18; i < 36; i += 3) {
  1739. z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
  1740. z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
  1741. z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
  1742. }
  1743. break;
  1744. }
  1745. }
  1746. # endif /* ASO_IMDCT */
  1747. /*
  1748. * NAME: III_imdct_s()
  1749. * DESCRIPTION: perform IMDCT and windowing for short blocks
  1750. */
  1751. static
  1752. void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
  1753. {
  1754. mad_fixed_t y[36], *yptr;
  1755. mad_fixed_t const *wptr;
  1756. int w, i;
  1757. register mad_fixed64hi_t hi;
  1758. register mad_fixed64lo_t lo;
  1759. /* IMDCT */
  1760. yptr = &y[0];
  1761. for (w = 0; w < 3; ++w) {
  1762. register mad_fixed_t const (*s)[6];
  1763. s = imdct_s;
  1764. for (i = 0; i < 3; ++i) {
  1765. MAD_F_ML0(hi, lo, X[0], (*s)[0]);
  1766. MAD_F_MLA(hi, lo, X[1], (*s)[1]);
  1767. MAD_F_MLA(hi, lo, X[2], (*s)[2]);
  1768. MAD_F_MLA(hi, lo, X[3], (*s)[3]);
  1769. MAD_F_MLA(hi, lo, X[4], (*s)[4]);
  1770. MAD_F_MLA(hi, lo, X[5], (*s)[5]);
  1771. yptr[i + 0] = MAD_F_MLZ(hi, lo);
  1772. yptr[5 - i] = -yptr[i + 0];
  1773. ++s;
  1774. MAD_F_ML0(hi, lo, X[0], (*s)[0]);
  1775. MAD_F_MLA(hi, lo, X[1], (*s)[1]);
  1776. MAD_F_MLA(hi, lo, X[2], (*s)[2]);
  1777. MAD_F_MLA(hi, lo, X[3], (*s)[3]);
  1778. MAD_F_MLA(hi, lo, X[4], (*s)[4]);
  1779. MAD_F_MLA(hi, lo, X[5], (*s)[5]);
  1780. yptr[ i + 6] = MAD_F_MLZ(hi, lo);
  1781. yptr[11 - i] = yptr[i + 6];
  1782. ++s;
  1783. }
  1784. yptr += 12;
  1785. X += 6;
  1786. }
  1787. /* windowing, overlapping and concatenation */
  1788. yptr = &y[0];
  1789. wptr = &window_s[0];
  1790. for (i = 0; i < 6; ++i) {
  1791. z[i + 0] = 0;
  1792. z[i + 6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
  1793. MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
  1794. MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
  1795. z[i + 12] = MAD_F_MLZ(hi, lo);
  1796. MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
  1797. MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
  1798. z[i + 18] = MAD_F_MLZ(hi, lo);
  1799. z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
  1800. z[i + 30] = 0;
  1801. ++yptr;
  1802. ++wptr;
  1803. }
  1804. }
  1805. /*
  1806. * NAME: III_overlap()
  1807. * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
  1808. */
  1809. static
  1810. void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
  1811. mad_fixed_t sample[18][32], unsigned int sb)
  1812. {
  1813. unsigned int i;
  1814. # if defined(ASO_INTERLEAVE2)
  1815. {
  1816. register mad_fixed_t tmp1, tmp2;
  1817. tmp1 = overlap[0];
  1818. tmp2 = overlap[1];
  1819. for (i = 0; i < 16; i += 2) {
  1820. sample[i + 0][sb] = output[i + 0 + 0] + tmp1;
  1821. overlap[i + 0] = output[i + 0 + 18];
  1822. tmp1 = overlap[i + 2];
  1823. sample[i + 1][sb] = output[i + 1 + 0] + tmp2;
  1824. overlap[i + 1] = output[i + 1 + 18];
  1825. tmp2 = overlap[i + 3];
  1826. }
  1827. sample[16][sb] = output[16 + 0] + tmp1;
  1828. overlap[16] = output[16 + 18];
  1829. sample[17][sb] = output[17 + 0] + tmp2;
  1830. overlap[17] = output[17 + 18];
  1831. }
  1832. # elif 0
  1833. for (i = 0; i < 18; i += 2) {
  1834. sample[i + 0][sb] = output[i + 0 + 0] + overlap[i + 0];
  1835. overlap[i + 0] = output[i + 0 + 18];
  1836. sample[i + 1][sb] = output[i + 1 + 0] + overlap[i + 1];
  1837. overlap[i + 1] = output[i + 1 + 18];
  1838. }
  1839. # else
  1840. for (i = 0; i < 18; ++i) {
  1841. sample[i][sb] = output[i + 0] + overlap[i];
  1842. overlap[i] = output[i + 18];
  1843. }
  1844. # endif
  1845. }
  1846. /*
  1847. * NAME: III_overlap_z()
  1848. * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
  1849. */
  1850. static inline
  1851. void III_overlap_z(mad_fixed_t overlap[18],
  1852. mad_fixed_t sample[18][32], unsigned int sb)
  1853. {
  1854. unsigned int i;
  1855. # if defined(ASO_INTERLEAVE2)
  1856. {
  1857. register mad_fixed_t tmp1, tmp2;
  1858. tmp1 = overlap[0];
  1859. tmp2 = overlap[1];
  1860. for (i = 0; i < 16; i += 2) {
  1861. sample[i + 0][sb] = tmp1;
  1862. overlap[i + 0] = 0;
  1863. tmp1 = overlap[i + 2];
  1864. sample[i + 1][sb] = tmp2;
  1865. overlap[i + 1] = 0;
  1866. tmp2 = overlap[i + 3];
  1867. }
  1868. sample[16][sb] = tmp1;
  1869. overlap[16] = 0;
  1870. sample[17][sb] = tmp2;
  1871. overlap[17] = 0;
  1872. }
  1873. # else
  1874. for (i = 0; i < 18; ++i) {
  1875. sample[i][sb] = overlap[i];
  1876. overlap[i] = 0;
  1877. }
  1878. # endif
  1879. }
  1880. /*
  1881. * NAME: III_freqinver()
  1882. * DESCRIPTION: perform subband frequency inversion for odd sample lines
  1883. */
  1884. static
  1885. void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
  1886. {
  1887. unsigned int i;
  1888. # if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
  1889. {
  1890. register mad_fixed_t tmp1, tmp2;
  1891. tmp1 = sample[1][sb];
  1892. tmp2 = sample[3][sb];
  1893. for (i = 1; i < 13; i += 4) {
  1894. sample[i + 0][sb] = -tmp1;
  1895. tmp1 = sample[i + 4][sb];
  1896. sample[i + 2][sb] = -tmp2;
  1897. tmp2 = sample[i + 6][sb];
  1898. }
  1899. sample[13][sb] = -tmp1;
  1900. tmp1 = sample[17][sb];
  1901. sample[15][sb] = -tmp2;
  1902. sample[17][sb] = -tmp1;
  1903. }
  1904. # else
  1905. for (i = 1; i < 18; i += 2)
  1906. sample[i][sb] = -sample[i][sb];
  1907. # endif
  1908. }
  1909. /*
  1910. * NAME: III_decode()
  1911. * DESCRIPTION: decode frame main_data
  1912. */
  1913. static
  1914. enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
  1915. struct sideinfo *si, unsigned int nch)
  1916. {
  1917. struct mad_header *header = &frame->header;
  1918. unsigned int sfreqi, ngr, gr;
  1919. {
  1920. unsigned int sfreq;
  1921. sfreq = header->samplerate;
  1922. if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
  1923. sfreq *= 2;
  1924. /* 48000 => 0, 44100 => 1, 32000 => 2,
  1925. 24000 => 3, 22050 => 4, 16000 => 5 */
  1926. sfreqi = ((sfreq >> 7) & 0x000f) +
  1927. ((sfreq >> 15) & 0x0001) - 8;
  1928. if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
  1929. sfreqi += 3;
  1930. }
  1931. /* scalefactors, Huffman decoding, requantization */
  1932. ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
  1933. for (gr = 0; gr < ngr; ++gr) {
  1934. struct granule *granule = &si->gr[gr];
  1935. unsigned char const *sfbwidth[2];
  1936. mad_fixed_t xr[2][576];
  1937. unsigned int ch;
  1938. enum mad_error error;
  1939. for (ch = 0; ch < nch; ++ch) {
  1940. struct channel *channel = &granule->ch[ch];
  1941. unsigned int part2_length;
  1942. sfbwidth[ch] = sfbwidth_table[sfreqi].l;
  1943. if (channel->block_type == 2) {
  1944. sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
  1945. sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
  1946. }
  1947. if (header->flags & MAD_FLAG_LSF_EXT) {
  1948. part2_length = III_scalefactors_lsf(ptr, channel,
  1949. ch == 0 ? 0 : &si->gr[1].ch[1],
  1950. header->mode_extension);
  1951. }
  1952. else {
  1953. part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
  1954. gr == 0 ? 0 : si->scfsi[ch]);
  1955. }
  1956. error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
  1957. if (error)
  1958. return error;
  1959. }
  1960. /* joint stereo processing */
  1961. if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
  1962. error = III_stereo(xr, granule, header, sfbwidth[0]);
  1963. if (error)
  1964. return error;
  1965. }
  1966. /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
  1967. for (ch = 0; ch < nch; ++ch) {
  1968. struct channel const *channel = &granule->ch[ch];
  1969. mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
  1970. unsigned int sb, l, i, sblimit;
  1971. mad_fixed_t output[36];
  1972. if (channel->block_type == 2) {
  1973. III_reorder(xr[ch], channel, sfbwidth[ch]);
  1974. # if !defined(OPT_STRICT)
  1975. /*
  1976. * According to ISO/IEC 11172-3, "Alias reduction is not applied for
  1977. * granules with block_type == 2 (short block)." However, other
  1978. * sources suggest alias reduction should indeed be performed on the
  1979. * lower two subbands of mixed blocks. Most other implementations do
  1980. * this, so by default we will too.
  1981. */
  1982. if (channel->flags & mixed_block_flag)
  1983. III_aliasreduce(xr[ch], 36);
  1984. # endif
  1985. }
  1986. else
  1987. III_aliasreduce(xr[ch], 576);
  1988. l = 0;
  1989. /* subbands 0-1 */
  1990. if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
  1991. unsigned int block_type;
  1992. block_type = channel->block_type;
  1993. if (channel->flags & mixed_block_flag)
  1994. block_type = 0;
  1995. /* long blocks */
  1996. for (sb = 0; sb < 2; ++sb, l += 18) {
  1997. III_imdct_l(&xr[ch][l], output, block_type);
  1998. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  1999. }
  2000. }
  2001. else {
  2002. /* short blocks */
  2003. for (sb = 0; sb < 2; ++sb, l += 18) {
  2004. III_imdct_s(&xr[ch][l], output);
  2005. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  2006. }
  2007. }
  2008. III_freqinver(sample, 1);
  2009. /* (nonzero) subbands 2-31 */
  2010. i = 576;
  2011. while (i > 36 && xr[ch][i - 1] == 0)
  2012. --i;
  2013. sblimit = 32 - (576 - i) / 18;
  2014. if (channel->block_type != 2) {
  2015. /* long blocks */
  2016. for (sb = 2; sb < sblimit; ++sb, l += 18) {
  2017. III_imdct_l(&xr[ch][l], output, channel->block_type);
  2018. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  2019. if (sb & 1)
  2020. III_freqinver(sample, sb);
  2021. }
  2022. }
  2023. else {
  2024. /* short blocks */
  2025. for (sb = 2; sb < sblimit; ++sb, l += 18) {
  2026. III_imdct_s(&xr[ch][l], output);
  2027. III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
  2028. if (sb & 1)
  2029. III_freqinver(sample, sb);
  2030. }
  2031. }
  2032. /* remaining (zero) subbands */
  2033. for (sb = sblimit; sb < 32; ++sb) {
  2034. III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
  2035. if (sb & 1)
  2036. III_freqinver(sample, sb);
  2037. }
  2038. }
  2039. }
  2040. return MAD_ERROR_NONE;
  2041. }
  2042. /*
  2043. * NAME: layer->III()
  2044. * DESCRIPTION: decode a single Layer III frame
  2045. */
  2046. int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
  2047. {
  2048. struct mad_header *header = &frame->header;
  2049. unsigned int nch, priv_bitlen, next_md_begin = 0;
  2050. unsigned int si_len, data_bitlen, md_len;
  2051. unsigned int frame_space, frame_used, frame_free;
  2052. struct mad_bitptr ptr;
  2053. struct sideinfo si;
  2054. enum mad_error error;
  2055. int result = 0;
  2056. /* allocate Layer III dynamic structures */
  2057. if (stream->main_data == 0) {
  2058. stream->main_data = malloc(MAD_BUFFER_MDLEN);
  2059. if (stream->main_data == 0) {
  2060. stream->error = MAD_ERROR_NOMEM;
  2061. return -1;
  2062. }
  2063. }
  2064. if (frame->overlap == 0) {
  2065. frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
  2066. if (frame->overlap == 0) {
  2067. stream->error = MAD_ERROR_NOMEM;
  2068. return -1;
  2069. }
  2070. }
  2071. nch = MAD_NCHANNELS(header);
  2072. si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
  2073. (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
  2074. /* check frame sanity */
  2075. if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
  2076. (signed int) si_len) {
  2077. stream->error = MAD_ERROR_BADFRAMELEN;
  2078. stream->md_len = 0;
  2079. return -1;
  2080. }
  2081. /* check CRC word */
  2082. if (header->flags & MAD_FLAG_PROTECTION) {
  2083. header->crc_check =
  2084. mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
  2085. if (header->crc_check != header->crc_target &&
  2086. !(frame->options & MAD_OPTION_IGNORECRC)) {
  2087. stream->error = MAD_ERROR_BADCRC;
  2088. result = -1;
  2089. }
  2090. }
  2091. /* decode frame side information */
  2092. error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
  2093. &si, &data_bitlen, &priv_bitlen);
  2094. if (error && result == 0) {
  2095. stream->error = error;
  2096. result = -1;
  2097. }
  2098. header->flags |= priv_bitlen;
  2099. header->private_bits |= si.private_bits;
  2100. /* find main_data of next frame */
  2101. {
  2102. struct mad_bitptr peek;
  2103. unsigned long header;
  2104. mad_bit_init(&peek, stream->next_frame);
  2105. header = mad_bit_read(&peek, 32);
  2106. if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
  2107. if (!(header & 0x00010000L)) /* protection_bit */
  2108. mad_bit_skip(&peek, 16); /* crc_check */
  2109. next_md_begin =
  2110. mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
  2111. }
  2112. mad_bit_finish(&peek);
  2113. }
  2114. /* find main_data of this frame */
  2115. frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
  2116. if (next_md_begin > si.main_data_begin + frame_space)
  2117. next_md_begin = 0;
  2118. md_len = si.main_data_begin + frame_space - next_md_begin;
  2119. frame_used = 0;
  2120. if (si.main_data_begin == 0) {
  2121. ptr = stream->ptr;
  2122. stream->md_len = 0;
  2123. frame_used = md_len;
  2124. }
  2125. else {
  2126. if (si.main_data_begin > stream->md_len) {
  2127. if (result == 0) {
  2128. stream->error = MAD_ERROR_BADDATAPTR;
  2129. result = -1;
  2130. }
  2131. }
  2132. else {
  2133. mad_bit_init(&ptr,
  2134. *stream->main_data + stream->md_len - si.main_data_begin);
  2135. if (md_len > si.main_data_begin) {
  2136. assert(stream->md_len + md_len -
  2137. si.main_data_begin <= MAD_BUFFER_MDLEN);
  2138. memcpy(*stream->main_data + stream->md_len,
  2139. mad_bit_nextbyte(&stream->ptr),
  2140. frame_used = md_len - si.main_data_begin);
  2141. stream->md_len += frame_used;
  2142. }
  2143. }
  2144. }
  2145. frame_free = frame_space - frame_used;
  2146. /* decode main_data */
  2147. if (result == 0) {
  2148. error = III_decode(&ptr, frame, &si, nch);
  2149. if (error) {
  2150. stream->error = error;
  2151. result = -1;
  2152. }
  2153. /* designate ancillary bits */
  2154. stream->anc_ptr = ptr;
  2155. stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
  2156. }
  2157. # if 0 && defined(DEBUG)
  2158. fprintf(stderr,
  2159. "main_data_begin:%u, md_len:%u, frame_free:%u, "
  2160. "data_bitlen:%u, anc_bitlen: %u\n",
  2161. si.main_data_begin, md_len, frame_free,
  2162. data_bitlen, stream->anc_bitlen);
  2163. # endif
  2164. /* preload main_data buffer with up to 511 bytes for next frame(s) */
  2165. if (frame_free >= next_md_begin) {
  2166. memcpy(*stream->main_data,
  2167. stream->next_frame - next_md_begin, next_md_begin);
  2168. stream->md_len = next_md_begin;
  2169. }
  2170. else {
  2171. if (md_len < si.main_data_begin) {
  2172. unsigned int extra;
  2173. extra = si.main_data_begin - md_len;
  2174. if (extra + frame_free > next_md_begin)
  2175. extra = next_md_begin - frame_free;
  2176. if (extra < stream->md_len) {
  2177. memmove(*stream->main_data,
  2178. *stream->main_data + stream->md_len - extra, extra);
  2179. stream->md_len = extra;
  2180. }
  2181. }
  2182. else
  2183. stream->md_len = 0;
  2184. memcpy(*stream->main_data + stream->md_len,
  2185. stream->next_frame - frame_free, frame_free);
  2186. stream->md_len += frame_free;
  2187. }
  2188. return result;
  2189. }