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.

2779 lines
82KB

  1. /*
  2. * MPEG Audio decoder
  3. * Copyright (c) 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file mpegaudiodec.c
  21. * MPEG Audio decoder.
  22. */
  23. //#define DEBUG
  24. #include "avcodec.h"
  25. #include "bitstream.h"
  26. #include "mpegaudio.h"
  27. #include "dsputil.h"
  28. /*
  29. * TODO:
  30. * - in low precision mode, use more 16 bit multiplies in synth filter
  31. * - test lsf / mpeg25 extensively.
  32. */
  33. /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
  34. audio decoder */
  35. #ifdef CONFIG_MPEGAUDIO_HP
  36. #define USE_HIGHPRECISION
  37. #endif
  38. #ifdef USE_HIGHPRECISION
  39. #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
  40. #define WFRAC_BITS 16 /* fractional bits for window */
  41. #else
  42. #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
  43. #define WFRAC_BITS 14 /* fractional bits for window */
  44. #endif
  45. #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
  46. typedef int32_t OUT_INT;
  47. #define OUT_MAX INT32_MAX
  48. #define OUT_MIN INT32_MIN
  49. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)
  50. #else
  51. typedef int16_t OUT_INT;
  52. #define OUT_MAX INT16_MAX
  53. #define OUT_MIN INT16_MIN
  54. #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
  55. #endif
  56. #define FRAC_ONE (1 << FRAC_BITS)
  57. #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
  58. #define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
  59. #define FIX(a) ((int)((a) * FRAC_ONE))
  60. /* WARNING: only correct for posititive numbers */
  61. #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
  62. #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
  63. #if FRAC_BITS <= 15
  64. typedef int16_t MPA_INT;
  65. #else
  66. typedef int32_t MPA_INT;
  67. #endif
  68. /****************/
  69. #define HEADER_SIZE 4
  70. #define BACKSTEP_SIZE 512
  71. struct GranuleDef;
  72. typedef struct MPADecodeContext {
  73. uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
  74. int inbuf_index;
  75. uint8_t *inbuf_ptr, *inbuf;
  76. int frame_size;
  77. int free_format_frame_size; /* frame size in case of free format
  78. (zero if currently unknown) */
  79. /* next header (used in free format parsing) */
  80. uint32_t free_format_next_header;
  81. int error_protection;
  82. int layer;
  83. int sample_rate;
  84. int sample_rate_index; /* between 0 and 8 */
  85. int bit_rate;
  86. int old_frame_size;
  87. GetBitContext gb;
  88. int nb_channels;
  89. int mode;
  90. int mode_ext;
  91. int lsf;
  92. MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
  93. int synth_buf_offset[MPA_MAX_CHANNELS];
  94. int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
  95. int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
  96. #ifdef DEBUG
  97. int frame_count;
  98. #endif
  99. void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
  100. int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
  101. unsigned int dither_state;
  102. } MPADecodeContext;
  103. /* layer 3 "granule" */
  104. typedef struct GranuleDef {
  105. uint8_t scfsi;
  106. int part2_3_length;
  107. int big_values;
  108. int global_gain;
  109. int scalefac_compress;
  110. uint8_t block_type;
  111. uint8_t switch_point;
  112. int table_select[3];
  113. int subblock_gain[3];
  114. uint8_t scalefac_scale;
  115. uint8_t count1table_select;
  116. int region_size[3]; /* number of huffman codes in each region */
  117. int preflag;
  118. int short_start, long_end; /* long/short band indexes */
  119. uint8_t scale_factors[40];
  120. int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
  121. } GranuleDef;
  122. #define MODE_EXT_MS_STEREO 2
  123. #define MODE_EXT_I_STEREO 1
  124. /* layer 3 huffman tables */
  125. typedef struct HuffTable {
  126. int xsize;
  127. const uint8_t *bits;
  128. const uint16_t *codes;
  129. } HuffTable;
  130. #include "mpegaudiodectab.h"
  131. static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
  132. static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
  133. /* vlc structure for decoding layer 3 huffman tables */
  134. static VLC huff_vlc[16];
  135. static uint8_t *huff_code_table[16];
  136. static VLC huff_quad_vlc[2];
  137. /* computed from band_size_long */
  138. static uint16_t band_index_long[9][23];
  139. /* XXX: free when all decoders are closed */
  140. #define TABLE_4_3_SIZE (8191 + 16)
  141. static int8_t *table_4_3_exp;
  142. #if FRAC_BITS <= 15
  143. static uint16_t *table_4_3_value;
  144. #else
  145. static uint32_t *table_4_3_value;
  146. #endif
  147. /* intensity stereo coef table */
  148. static int32_t is_table[2][16];
  149. static int32_t is_table_lsf[2][2][16];
  150. static int32_t csa_table[8][4];
  151. static float csa_table_float[8][4];
  152. static int32_t mdct_win[8][36];
  153. /* lower 2 bits: modulo 3, higher bits: shift */
  154. static uint16_t scale_factor_modshift[64];
  155. /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
  156. static int32_t scale_factor_mult[15][3];
  157. /* mult table for layer 2 group quantization */
  158. #define SCALE_GEN(v) \
  159. { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
  160. static int32_t scale_factor_mult2[3][3] = {
  161. SCALE_GEN(4.0 / 3.0), /* 3 steps */
  162. SCALE_GEN(4.0 / 5.0), /* 5 steps */
  163. SCALE_GEN(4.0 / 9.0), /* 9 steps */
  164. };
  165. /* 2^(n/4) */
  166. static uint32_t scale_factor_mult3[4] = {
  167. FIXR(1.0),
  168. FIXR(1.18920711500272106671),
  169. FIXR(1.41421356237309504880),
  170. FIXR(1.68179283050742908605),
  171. };
  172. void ff_mpa_synth_init(MPA_INT *window);
  173. static MPA_INT window[512] __attribute__((aligned(16)));
  174. /* layer 1 unscaling */
  175. /* n = number of bits of the mantissa minus 1 */
  176. static inline int l1_unscale(int n, int mant, int scale_factor)
  177. {
  178. int shift, mod;
  179. int64_t val;
  180. shift = scale_factor_modshift[scale_factor];
  181. mod = shift & 3;
  182. shift >>= 2;
  183. val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
  184. shift += n;
  185. /* NOTE: at this point, 1 <= shift >= 21 + 15 */
  186. return (int)((val + (1LL << (shift - 1))) >> shift);
  187. }
  188. static inline int l2_unscale_group(int steps, int mant, int scale_factor)
  189. {
  190. int shift, mod, val;
  191. shift = scale_factor_modshift[scale_factor];
  192. mod = shift & 3;
  193. shift >>= 2;
  194. val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
  195. /* NOTE: at this point, 0 <= shift <= 21 */
  196. if (shift > 0)
  197. val = (val + (1 << (shift - 1))) >> shift;
  198. return val;
  199. }
  200. /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
  201. static inline int l3_unscale(int value, int exponent)
  202. {
  203. #if FRAC_BITS <= 15
  204. unsigned int m;
  205. #else
  206. uint64_t m;
  207. #endif
  208. int e;
  209. e = table_4_3_exp[value];
  210. e += (exponent >> 2);
  211. e = FRAC_BITS - e;
  212. #if FRAC_BITS <= 15
  213. if (e > 31)
  214. #else
  215. if (e > 63)
  216. #endif
  217. return 0;
  218. m = table_4_3_value[value];
  219. #if FRAC_BITS <= 15
  220. m = (m * scale_factor_mult3[exponent & 3]);
  221. m = (m + (1 << (e-1))) >> e;
  222. return m;
  223. #else
  224. m = MUL64(m, scale_factor_mult3[exponent & 3]);
  225. m = (m + (uint64_t_C(1) << (e-1))) >> e;
  226. return m;
  227. #endif
  228. }
  229. /* all integer n^(4/3) computation code */
  230. #define DEV_ORDER 13
  231. #define POW_FRAC_BITS 24
  232. #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
  233. #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
  234. #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
  235. static int dev_4_3_coefs[DEV_ORDER];
  236. static int pow_mult3[3] = {
  237. POW_FIX(1.0),
  238. POW_FIX(1.25992104989487316476),
  239. POW_FIX(1.58740105196819947474),
  240. };
  241. static void int_pow_init(void)
  242. {
  243. int i, a;
  244. a = POW_FIX(1.0);
  245. for(i=0;i<DEV_ORDER;i++) {
  246. a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
  247. dev_4_3_coefs[i] = a;
  248. }
  249. }
  250. /* return the mantissa and the binary exponent */
  251. static int int_pow(int i, int *exp_ptr)
  252. {
  253. int e, er, eq, j;
  254. int a, a1;
  255. /* renormalize */
  256. a = i;
  257. e = POW_FRAC_BITS;
  258. while (a < (1 << (POW_FRAC_BITS - 1))) {
  259. a = a << 1;
  260. e--;
  261. }
  262. a -= (1 << POW_FRAC_BITS);
  263. a1 = 0;
  264. for(j = DEV_ORDER - 1; j >= 0; j--)
  265. a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
  266. a = (1 << POW_FRAC_BITS) + a1;
  267. /* exponent compute (exact) */
  268. e = e * 4;
  269. er = e % 3;
  270. eq = e / 3;
  271. a = POW_MULL(a, pow_mult3[er]);
  272. while (a >= 2 * POW_FRAC_ONE) {
  273. a = a >> 1;
  274. eq++;
  275. }
  276. /* convert to float */
  277. while (a < POW_FRAC_ONE) {
  278. a = a << 1;
  279. eq--;
  280. }
  281. /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
  282. #if POW_FRAC_BITS > FRAC_BITS
  283. a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
  284. /* correct overflow */
  285. if (a >= 2 * (1 << FRAC_BITS)) {
  286. a = a >> 1;
  287. eq++;
  288. }
  289. #endif
  290. *exp_ptr = eq;
  291. return a;
  292. }
  293. static int decode_init(AVCodecContext * avctx)
  294. {
  295. MPADecodeContext *s = avctx->priv_data;
  296. static int init=0;
  297. int i, j, k;
  298. #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
  299. avctx->sample_fmt= SAMPLE_FMT_S32;
  300. #else
  301. avctx->sample_fmt= SAMPLE_FMT_S16;
  302. #endif
  303. if(avctx->antialias_algo == FF_AA_INT)
  304. s->compute_antialias= compute_antialias_integer;
  305. else
  306. s->compute_antialias= compute_antialias_float;
  307. if (!init && !avctx->parse_only) {
  308. /* scale factors table for layer 1/2 */
  309. for(i=0;i<64;i++) {
  310. int shift, mod;
  311. /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
  312. shift = (i / 3);
  313. mod = i % 3;
  314. scale_factor_modshift[i] = mod | (shift << 2);
  315. }
  316. /* scale factor multiply for layer 1 */
  317. for(i=0;i<15;i++) {
  318. int n, norm;
  319. n = i + 2;
  320. norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
  321. scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
  322. scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
  323. scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
  324. dprintf("%d: norm=%x s=%x %x %x\n",
  325. i, norm,
  326. scale_factor_mult[i][0],
  327. scale_factor_mult[i][1],
  328. scale_factor_mult[i][2]);
  329. }
  330. ff_mpa_synth_init(window);
  331. /* huffman decode tables */
  332. huff_code_table[0] = NULL;
  333. for(i=1;i<16;i++) {
  334. const HuffTable *h = &mpa_huff_tables[i];
  335. int xsize, x, y;
  336. unsigned int n;
  337. uint8_t *code_table;
  338. xsize = h->xsize;
  339. n = xsize * xsize;
  340. /* XXX: fail test */
  341. init_vlc(&huff_vlc[i], 8, n,
  342. h->bits, 1, 1, h->codes, 2, 2, 1);
  343. code_table = av_mallocz(n);
  344. j = 0;
  345. for(x=0;x<xsize;x++) {
  346. for(y=0;y<xsize;y++)
  347. code_table[j++] = (x << 4) | y;
  348. }
  349. huff_code_table[i] = code_table;
  350. }
  351. for(i=0;i<2;i++) {
  352. init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
  353. mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
  354. }
  355. for(i=0;i<9;i++) {
  356. k = 0;
  357. for(j=0;j<22;j++) {
  358. band_index_long[i][j] = k;
  359. k += band_size_long[i][j];
  360. }
  361. band_index_long[i][22] = k;
  362. }
  363. /* compute n ^ (4/3) and store it in mantissa/exp format */
  364. table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
  365. if(!table_4_3_exp)
  366. return -1;
  367. table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
  368. if(!table_4_3_value)
  369. return -1;
  370. int_pow_init();
  371. for(i=1;i<TABLE_4_3_SIZE;i++) {
  372. int e, m;
  373. m = int_pow(i, &e);
  374. #if 0
  375. /* test code */
  376. {
  377. double f, fm;
  378. int e1, m1;
  379. f = pow((double)i, 4.0 / 3.0);
  380. fm = frexp(f, &e1);
  381. m1 = FIXR(2 * fm);
  382. #if FRAC_BITS <= 15
  383. if ((unsigned short)m1 != m1) {
  384. m1 = m1 >> 1;
  385. e1++;
  386. }
  387. #endif
  388. e1--;
  389. if (m != m1 || e != e1) {
  390. printf("%4d: m=%x m1=%x e=%d e1=%d\n",
  391. i, m, m1, e, e1);
  392. }
  393. }
  394. #endif
  395. /* normalized to FRAC_BITS */
  396. table_4_3_value[i] = m;
  397. table_4_3_exp[i] = e;
  398. }
  399. for(i=0;i<7;i++) {
  400. float f;
  401. int v;
  402. if (i != 6) {
  403. f = tan((double)i * M_PI / 12.0);
  404. v = FIXR(f / (1.0 + f));
  405. } else {
  406. v = FIXR(1.0);
  407. }
  408. is_table[0][i] = v;
  409. is_table[1][6 - i] = v;
  410. }
  411. /* invalid values */
  412. for(i=7;i<16;i++)
  413. is_table[0][i] = is_table[1][i] = 0.0;
  414. for(i=0;i<16;i++) {
  415. double f;
  416. int e, k;
  417. for(j=0;j<2;j++) {
  418. e = -(j + 1) * ((i + 1) >> 1);
  419. f = pow(2.0, e / 4.0);
  420. k = i & 1;
  421. is_table_lsf[j][k ^ 1][i] = FIXR(f);
  422. is_table_lsf[j][k][i] = FIXR(1.0);
  423. dprintf("is_table_lsf %d %d: %x %x\n",
  424. i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
  425. }
  426. }
  427. for(i=0;i<8;i++) {
  428. float ci, cs, ca;
  429. ci = ci_table[i];
  430. cs = 1.0 / sqrt(1.0 + ci * ci);
  431. ca = cs * ci;
  432. csa_table[i][0] = FIX(cs);
  433. csa_table[i][1] = FIX(ca);
  434. csa_table[i][2] = FIX(ca) + FIX(cs);
  435. csa_table[i][3] = FIX(ca) - FIX(cs);
  436. csa_table_float[i][0] = cs;
  437. csa_table_float[i][1] = ca;
  438. csa_table_float[i][2] = ca + cs;
  439. csa_table_float[i][3] = ca - cs;
  440. // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
  441. }
  442. /* compute mdct windows */
  443. for(i=0;i<36;i++) {
  444. int v;
  445. v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
  446. mdct_win[0][i] = v;
  447. mdct_win[1][i] = v;
  448. mdct_win[3][i] = v;
  449. }
  450. for(i=0;i<6;i++) {
  451. mdct_win[1][18 + i] = FIXR(1.0);
  452. mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
  453. mdct_win[1][30 + i] = FIXR(0.0);
  454. mdct_win[3][i] = FIXR(0.0);
  455. mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
  456. mdct_win[3][12 + i] = FIXR(1.0);
  457. }
  458. for(i=0;i<12;i++)
  459. mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
  460. /* NOTE: we do frequency inversion adter the MDCT by changing
  461. the sign of the right window coefs */
  462. for(j=0;j<4;j++) {
  463. for(i=0;i<36;i+=2) {
  464. mdct_win[j + 4][i] = mdct_win[j][i];
  465. mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
  466. }
  467. }
  468. #if defined(DEBUG)
  469. for(j=0;j<8;j++) {
  470. printf("win%d=\n", j);
  471. for(i=0;i<36;i++)
  472. printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
  473. printf("\n");
  474. }
  475. #endif
  476. init = 1;
  477. }
  478. s->inbuf_index = 0;
  479. s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
  480. s->inbuf_ptr = s->inbuf;
  481. #ifdef DEBUG
  482. s->frame_count = 0;
  483. #endif
  484. if (avctx->codec_id == CODEC_ID_MP3ADU)
  485. s->adu_mode = 1;
  486. return 0;
  487. }
  488. /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
  489. /* cos(i*pi/64) */
  490. #define COS0_0 FIXR(0.50060299823519630134)
  491. #define COS0_1 FIXR(0.50547095989754365998)
  492. #define COS0_2 FIXR(0.51544730992262454697)
  493. #define COS0_3 FIXR(0.53104259108978417447)
  494. #define COS0_4 FIXR(0.55310389603444452782)
  495. #define COS0_5 FIXR(0.58293496820613387367)
  496. #define COS0_6 FIXR(0.62250412303566481615)
  497. #define COS0_7 FIXR(0.67480834145500574602)
  498. #define COS0_8 FIXR(0.74453627100229844977)
  499. #define COS0_9 FIXR(0.83934964541552703873)
  500. #define COS0_10 FIXR(0.97256823786196069369)
  501. #define COS0_11 FIXR(1.16943993343288495515)
  502. #define COS0_12 FIXR(1.48416461631416627724)
  503. #define COS0_13 FIXR(2.05778100995341155085)
  504. #define COS0_14 FIXR(3.40760841846871878570)
  505. #define COS0_15 FIXR(10.19000812354805681150)
  506. #define COS1_0 FIXR(0.50241928618815570551)
  507. #define COS1_1 FIXR(0.52249861493968888062)
  508. #define COS1_2 FIXR(0.56694403481635770368)
  509. #define COS1_3 FIXR(0.64682178335999012954)
  510. #define COS1_4 FIXR(0.78815462345125022473)
  511. #define COS1_5 FIXR(1.06067768599034747134)
  512. #define COS1_6 FIXR(1.72244709823833392782)
  513. #define COS1_7 FIXR(5.10114861868916385802)
  514. #define COS2_0 FIXR(0.50979557910415916894)
  515. #define COS2_1 FIXR(0.60134488693504528054)
  516. #define COS2_2 FIXR(0.89997622313641570463)
  517. #define COS2_3 FIXR(2.56291544774150617881)
  518. #define COS3_0 FIXR(0.54119610014619698439)
  519. #define COS3_1 FIXR(1.30656296487637652785)
  520. #define COS4_0 FIXR(0.70710678118654752439)
  521. /* butterfly operator */
  522. #define BF(a, b, c)\
  523. {\
  524. tmp0 = tab[a] + tab[b];\
  525. tmp1 = tab[a] - tab[b];\
  526. tab[a] = tmp0;\
  527. tab[b] = MULL(tmp1, c);\
  528. }
  529. #define BF1(a, b, c, d)\
  530. {\
  531. BF(a, b, COS4_0);\
  532. BF(c, d, -COS4_0);\
  533. tab[c] += tab[d];\
  534. }
  535. #define BF2(a, b, c, d)\
  536. {\
  537. BF(a, b, COS4_0);\
  538. BF(c, d, -COS4_0);\
  539. tab[c] += tab[d];\
  540. tab[a] += tab[c];\
  541. tab[c] += tab[b];\
  542. tab[b] += tab[d];\
  543. }
  544. #define ADD(a, b) tab[a] += tab[b]
  545. /* DCT32 without 1/sqrt(2) coef zero scaling. */
  546. static void dct32(int32_t *out, int32_t *tab)
  547. {
  548. int tmp0, tmp1;
  549. /* pass 1 */
  550. BF(0, 31, COS0_0);
  551. BF(1, 30, COS0_1);
  552. BF(2, 29, COS0_2);
  553. BF(3, 28, COS0_3);
  554. BF(4, 27, COS0_4);
  555. BF(5, 26, COS0_5);
  556. BF(6, 25, COS0_6);
  557. BF(7, 24, COS0_7);
  558. BF(8, 23, COS0_8);
  559. BF(9, 22, COS0_9);
  560. BF(10, 21, COS0_10);
  561. BF(11, 20, COS0_11);
  562. BF(12, 19, COS0_12);
  563. BF(13, 18, COS0_13);
  564. BF(14, 17, COS0_14);
  565. BF(15, 16, COS0_15);
  566. /* pass 2 */
  567. BF(0, 15, COS1_0);
  568. BF(1, 14, COS1_1);
  569. BF(2, 13, COS1_2);
  570. BF(3, 12, COS1_3);
  571. BF(4, 11, COS1_4);
  572. BF(5, 10, COS1_5);
  573. BF(6, 9, COS1_6);
  574. BF(7, 8, COS1_7);
  575. BF(16, 31, -COS1_0);
  576. BF(17, 30, -COS1_1);
  577. BF(18, 29, -COS1_2);
  578. BF(19, 28, -COS1_3);
  579. BF(20, 27, -COS1_4);
  580. BF(21, 26, -COS1_5);
  581. BF(22, 25, -COS1_6);
  582. BF(23, 24, -COS1_7);
  583. /* pass 3 */
  584. BF(0, 7, COS2_0);
  585. BF(1, 6, COS2_1);
  586. BF(2, 5, COS2_2);
  587. BF(3, 4, COS2_3);
  588. BF(8, 15, -COS2_0);
  589. BF(9, 14, -COS2_1);
  590. BF(10, 13, -COS2_2);
  591. BF(11, 12, -COS2_3);
  592. BF(16, 23, COS2_0);
  593. BF(17, 22, COS2_1);
  594. BF(18, 21, COS2_2);
  595. BF(19, 20, COS2_3);
  596. BF(24, 31, -COS2_0);
  597. BF(25, 30, -COS2_1);
  598. BF(26, 29, -COS2_2);
  599. BF(27, 28, -COS2_3);
  600. /* pass 4 */
  601. BF(0, 3, COS3_0);
  602. BF(1, 2, COS3_1);
  603. BF(4, 7, -COS3_0);
  604. BF(5, 6, -COS3_1);
  605. BF(8, 11, COS3_0);
  606. BF(9, 10, COS3_1);
  607. BF(12, 15, -COS3_0);
  608. BF(13, 14, -COS3_1);
  609. BF(16, 19, COS3_0);
  610. BF(17, 18, COS3_1);
  611. BF(20, 23, -COS3_0);
  612. BF(21, 22, -COS3_1);
  613. BF(24, 27, COS3_0);
  614. BF(25, 26, COS3_1);
  615. BF(28, 31, -COS3_0);
  616. BF(29, 30, -COS3_1);
  617. /* pass 5 */
  618. BF1(0, 1, 2, 3);
  619. BF2(4, 5, 6, 7);
  620. BF1(8, 9, 10, 11);
  621. BF2(12, 13, 14, 15);
  622. BF1(16, 17, 18, 19);
  623. BF2(20, 21, 22, 23);
  624. BF1(24, 25, 26, 27);
  625. BF2(28, 29, 30, 31);
  626. /* pass 6 */
  627. ADD( 8, 12);
  628. ADD(12, 10);
  629. ADD(10, 14);
  630. ADD(14, 9);
  631. ADD( 9, 13);
  632. ADD(13, 11);
  633. ADD(11, 15);
  634. out[ 0] = tab[0];
  635. out[16] = tab[1];
  636. out[ 8] = tab[2];
  637. out[24] = tab[3];
  638. out[ 4] = tab[4];
  639. out[20] = tab[5];
  640. out[12] = tab[6];
  641. out[28] = tab[7];
  642. out[ 2] = tab[8];
  643. out[18] = tab[9];
  644. out[10] = tab[10];
  645. out[26] = tab[11];
  646. out[ 6] = tab[12];
  647. out[22] = tab[13];
  648. out[14] = tab[14];
  649. out[30] = tab[15];
  650. ADD(24, 28);
  651. ADD(28, 26);
  652. ADD(26, 30);
  653. ADD(30, 25);
  654. ADD(25, 29);
  655. ADD(29, 27);
  656. ADD(27, 31);
  657. out[ 1] = tab[16] + tab[24];
  658. out[17] = tab[17] + tab[25];
  659. out[ 9] = tab[18] + tab[26];
  660. out[25] = tab[19] + tab[27];
  661. out[ 5] = tab[20] + tab[28];
  662. out[21] = tab[21] + tab[29];
  663. out[13] = tab[22] + tab[30];
  664. out[29] = tab[23] + tab[31];
  665. out[ 3] = tab[24] + tab[20];
  666. out[19] = tab[25] + tab[21];
  667. out[11] = tab[26] + tab[22];
  668. out[27] = tab[27] + tab[23];
  669. out[ 7] = tab[28] + tab[18];
  670. out[23] = tab[29] + tab[19];
  671. out[15] = tab[30] + tab[17];
  672. out[31] = tab[31];
  673. }
  674. #if FRAC_BITS <= 15
  675. static inline int round_sample(int *sum)
  676. {
  677. int sum1;
  678. sum1 = (*sum) >> OUT_SHIFT;
  679. *sum &= (1<<OUT_SHIFT)-1;
  680. if (sum1 < OUT_MIN)
  681. sum1 = OUT_MIN;
  682. else if (sum1 > OUT_MAX)
  683. sum1 = OUT_MAX;
  684. return sum1;
  685. }
  686. #if defined(ARCH_POWERPC_405)
  687. /* signed 16x16 -> 32 multiply add accumulate */
  688. #define MACS(rt, ra, rb) \
  689. asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
  690. /* signed 16x16 -> 32 multiply */
  691. #define MULS(ra, rb) \
  692. ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
  693. #else
  694. /* signed 16x16 -> 32 multiply add accumulate */
  695. #define MACS(rt, ra, rb) rt += (ra) * (rb)
  696. /* signed 16x16 -> 32 multiply */
  697. #define MULS(ra, rb) ((ra) * (rb))
  698. #endif
  699. #else
  700. static inline int round_sample(int64_t *sum)
  701. {
  702. int sum1;
  703. sum1 = (int)((*sum) >> OUT_SHIFT);
  704. *sum &= (1<<OUT_SHIFT)-1;
  705. if (sum1 < OUT_MIN)
  706. sum1 = OUT_MIN;
  707. else if (sum1 > OUT_MAX)
  708. sum1 = OUT_MAX;
  709. return sum1;
  710. }
  711. #define MULS(ra, rb) MUL64(ra, rb)
  712. #endif
  713. #define SUM8(sum, op, w, p) \
  714. { \
  715. sum op MULS((w)[0 * 64], p[0 * 64]);\
  716. sum op MULS((w)[1 * 64], p[1 * 64]);\
  717. sum op MULS((w)[2 * 64], p[2 * 64]);\
  718. sum op MULS((w)[3 * 64], p[3 * 64]);\
  719. sum op MULS((w)[4 * 64], p[4 * 64]);\
  720. sum op MULS((w)[5 * 64], p[5 * 64]);\
  721. sum op MULS((w)[6 * 64], p[6 * 64]);\
  722. sum op MULS((w)[7 * 64], p[7 * 64]);\
  723. }
  724. #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
  725. { \
  726. int tmp;\
  727. tmp = p[0 * 64];\
  728. sum1 op1 MULS((w1)[0 * 64], tmp);\
  729. sum2 op2 MULS((w2)[0 * 64], tmp);\
  730. tmp = p[1 * 64];\
  731. sum1 op1 MULS((w1)[1 * 64], tmp);\
  732. sum2 op2 MULS((w2)[1 * 64], tmp);\
  733. tmp = p[2 * 64];\
  734. sum1 op1 MULS((w1)[2 * 64], tmp);\
  735. sum2 op2 MULS((w2)[2 * 64], tmp);\
  736. tmp = p[3 * 64];\
  737. sum1 op1 MULS((w1)[3 * 64], tmp);\
  738. sum2 op2 MULS((w2)[3 * 64], tmp);\
  739. tmp = p[4 * 64];\
  740. sum1 op1 MULS((w1)[4 * 64], tmp);\
  741. sum2 op2 MULS((w2)[4 * 64], tmp);\
  742. tmp = p[5 * 64];\
  743. sum1 op1 MULS((w1)[5 * 64], tmp);\
  744. sum2 op2 MULS((w2)[5 * 64], tmp);\
  745. tmp = p[6 * 64];\
  746. sum1 op1 MULS((w1)[6 * 64], tmp);\
  747. sum2 op2 MULS((w2)[6 * 64], tmp);\
  748. tmp = p[7 * 64];\
  749. sum1 op1 MULS((w1)[7 * 64], tmp);\
  750. sum2 op2 MULS((w2)[7 * 64], tmp);\
  751. }
  752. void ff_mpa_synth_init(MPA_INT *window)
  753. {
  754. int i;
  755. /* max = 18760, max sum over all 16 coefs : 44736 */
  756. for(i=0;i<257;i++) {
  757. int v;
  758. v = mpa_enwindow[i];
  759. #if WFRAC_BITS < 16
  760. v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
  761. #endif
  762. window[i] = v;
  763. if ((i & 63) != 0)
  764. v = -v;
  765. if (i != 0)
  766. window[512 - i] = v;
  767. }
  768. }
  769. /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
  770. 32 samples. */
  771. /* XXX: optimize by avoiding ring buffer usage */
  772. void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
  773. MPA_INT *window, int *dither_state,
  774. OUT_INT *samples, int incr,
  775. int32_t sb_samples[SBLIMIT])
  776. {
  777. int32_t tmp[32];
  778. register MPA_INT *synth_buf;
  779. register const MPA_INT *w, *w2, *p;
  780. int j, offset, v;
  781. OUT_INT *samples2;
  782. #if FRAC_BITS <= 15
  783. int sum, sum2;
  784. #else
  785. int64_t sum, sum2;
  786. #endif
  787. dct32(tmp, sb_samples);
  788. offset = *synth_buf_offset;
  789. synth_buf = synth_buf_ptr + offset;
  790. for(j=0;j<32;j++) {
  791. v = tmp[j];
  792. #if FRAC_BITS <= 15
  793. /* NOTE: can cause a loss in precision if very high amplitude
  794. sound */
  795. if (v > 32767)
  796. v = 32767;
  797. else if (v < -32768)
  798. v = -32768;
  799. #endif
  800. synth_buf[j] = v;
  801. }
  802. /* copy to avoid wrap */
  803. memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
  804. samples2 = samples + 31 * incr;
  805. w = window;
  806. w2 = window + 31;
  807. sum = *dither_state;
  808. p = synth_buf + 16;
  809. SUM8(sum, +=, w, p);
  810. p = synth_buf + 48;
  811. SUM8(sum, -=, w + 32, p);
  812. *samples = round_sample(&sum);
  813. samples += incr;
  814. w++;
  815. /* we calculate two samples at the same time to avoid one memory
  816. access per two sample */
  817. for(j=1;j<16;j++) {
  818. sum2 = 0;
  819. p = synth_buf + 16 + j;
  820. SUM8P2(sum, +=, sum2, -=, w, w2, p);
  821. p = synth_buf + 48 - j;
  822. SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
  823. *samples = round_sample(&sum);
  824. samples += incr;
  825. sum += sum2;
  826. *samples2 = round_sample(&sum);
  827. samples2 -= incr;
  828. w++;
  829. w2--;
  830. }
  831. p = synth_buf + 32;
  832. SUM8(sum, -=, w + 32, p);
  833. *samples = round_sample(&sum);
  834. *dither_state= sum;
  835. offset = (offset - 32) & 511;
  836. *synth_buf_offset = offset;
  837. }
  838. /* cos(pi*i/24) */
  839. #define C1 FIXR(0.99144486137381041114)
  840. #define C3 FIXR(0.92387953251128675612)
  841. #define C5 FIXR(0.79335334029123516458)
  842. #define C7 FIXR(0.60876142900872063941)
  843. #define C9 FIXR(0.38268343236508977173)
  844. #define C11 FIXR(0.13052619222005159154)
  845. /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
  846. cases. */
  847. static void imdct12(int *out, int *in)
  848. {
  849. int tmp;
  850. int64_t in1_3, in1_9, in4_3, in4_9;
  851. in1_3 = MUL64(in[1], C3);
  852. in1_9 = MUL64(in[1], C9);
  853. in4_3 = MUL64(in[4], C3);
  854. in4_9 = MUL64(in[4], C9);
  855. tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) +
  856. MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
  857. out[0] = tmp;
  858. out[5] = -tmp;
  859. tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 +
  860. MUL64(in[2] + in[5], C3) - in4_9);
  861. out[1] = tmp;
  862. out[4] = -tmp;
  863. tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
  864. MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
  865. out[2] = tmp;
  866. out[3] = -tmp;
  867. tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) +
  868. MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
  869. out[6] = tmp;
  870. out[11] = tmp;
  871. tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 +
  872. MUL64(in[2] + in[5], C9) + in4_3);
  873. out[7] = tmp;
  874. out[10] = tmp;
  875. tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
  876. MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
  877. out[8] = tmp;
  878. out[9] = tmp;
  879. }
  880. #undef C1
  881. #undef C3
  882. #undef C5
  883. #undef C7
  884. #undef C9
  885. #undef C11
  886. /* cos(pi*i/18) */
  887. #define C1 FIXR(0.98480775301220805936)
  888. #define C2 FIXR(0.93969262078590838405)
  889. #define C3 FIXR(0.86602540378443864676)
  890. #define C4 FIXR(0.76604444311897803520)
  891. #define C5 FIXR(0.64278760968653932632)
  892. #define C6 FIXR(0.5)
  893. #define C7 FIXR(0.34202014332566873304)
  894. #define C8 FIXR(0.17364817766693034885)
  895. /* 0.5 / cos(pi*(2*i+1)/36) */
  896. static const int icos36[9] = {
  897. FIXR(0.50190991877167369479),
  898. FIXR(0.51763809020504152469),
  899. FIXR(0.55168895948124587824),
  900. FIXR(0.61038729438072803416),
  901. FIXR(0.70710678118654752439),
  902. FIXR(0.87172339781054900991),
  903. FIXR(1.18310079157624925896),
  904. FIXR(1.93185165257813657349),
  905. FIXR(5.73685662283492756461),
  906. };
  907. static const int icos72[18] = {
  908. /* 0.5 / cos(pi*(2*i+19)/72) */
  909. FIXR(0.74009361646113053152),
  910. FIXR(0.82133981585229078570),
  911. FIXR(0.93057949835178895673),
  912. FIXR(1.08284028510010010928),
  913. FIXR(1.30656296487637652785),
  914. FIXR(1.66275476171152078719),
  915. FIXR(2.31011315767264929558),
  916. FIXR(3.83064878777019433457),
  917. FIXR(11.46279281302667383546),
  918. /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
  919. FIXR(-0.67817085245462840086),
  920. FIXR(-0.63023620700513223342),
  921. FIXR(-0.59284452371708034528),
  922. FIXR(-0.56369097343317117734),
  923. FIXR(-0.54119610014619698439),
  924. FIXR(-0.52426456257040533932),
  925. FIXR(-0.51213975715725461845),
  926. FIXR(-0.50431448029007636036),
  927. FIXR(-0.50047634258165998492),
  928. };
  929. /* using Lee like decomposition followed by hand coded 9 points DCT */
  930. static void imdct36(int *out, int *in)
  931. {
  932. int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
  933. int tmp[18], *tmp1, *in1;
  934. int64_t in3_3, in6_6;
  935. for(i=17;i>=1;i--)
  936. in[i] += in[i-1];
  937. for(i=17;i>=3;i-=2)
  938. in[i] += in[i-2];
  939. for(j=0;j<2;j++) {
  940. tmp1 = tmp + j;
  941. in1 = in + j;
  942. in3_3 = MUL64(in1[2*3], C3);
  943. in6_6 = MUL64(in1[2*6], C6);
  944. tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 +
  945. MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
  946. tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) +
  947. MUL64(in1[2*4], C4) + in6_6 +
  948. MUL64(in1[2*8], C8));
  949. tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
  950. tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) -
  951. in1[2*6] + in1[2*0];
  952. tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 -
  953. MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
  954. tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) -
  955. MUL64(in1[2*4], C2) + in6_6 +
  956. MUL64(in1[2*8], C4));
  957. tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 +
  958. MUL64(in1[2*5], C1) -
  959. MUL64(in1[2*7], C5));
  960. tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) +
  961. MUL64(in1[2*4], C8) + in6_6 -
  962. MUL64(in1[2*8], C2));
  963. tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
  964. }
  965. i = 0;
  966. for(j=0;j<4;j++) {
  967. t0 = tmp[i];
  968. t1 = tmp[i + 2];
  969. s0 = t1 + t0;
  970. s2 = t1 - t0;
  971. t2 = tmp[i + 1];
  972. t3 = tmp[i + 3];
  973. s1 = MULL(t3 + t2, icos36[j]);
  974. s3 = MULL(t3 - t2, icos36[8 - j]);
  975. t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
  976. t1 = MULL(s0 - s1, icos72[8 - j]);
  977. out[18 + 9 + j] = t0;
  978. out[18 + 8 - j] = t0;
  979. out[9 + j] = -t1;
  980. out[8 - j] = t1;
  981. t0 = MULL(s2 + s3, icos72[9+j]);
  982. t1 = MULL(s2 - s3, icos72[j]);
  983. out[18 + 9 + (8 - j)] = t0;
  984. out[18 + j] = t0;
  985. out[9 + (8 - j)] = -t1;
  986. out[j] = t1;
  987. i += 4;
  988. }
  989. s0 = tmp[16];
  990. s1 = MULL(tmp[17], icos36[4]);
  991. t0 = MULL(s0 + s1, icos72[9 + 4]);
  992. t1 = MULL(s0 - s1, icos72[4]);
  993. out[18 + 9 + 4] = t0;
  994. out[18 + 8 - 4] = t0;
  995. out[9 + 4] = -t1;
  996. out[8 - 4] = t1;
  997. }
  998. /* header decoding. MUST check the header before because no
  999. consistency check is done there. Return 1 if free format found and
  1000. that the frame size must be computed externally */
  1001. static int decode_header(MPADecodeContext *s, uint32_t header)
  1002. {
  1003. int sample_rate, frame_size, mpeg25, padding;
  1004. int sample_rate_index, bitrate_index;
  1005. if (header & (1<<20)) {
  1006. s->lsf = (header & (1<<19)) ? 0 : 1;
  1007. mpeg25 = 0;
  1008. } else {
  1009. s->lsf = 1;
  1010. mpeg25 = 1;
  1011. }
  1012. s->layer = 4 - ((header >> 17) & 3);
  1013. /* extract frequency */
  1014. sample_rate_index = (header >> 10) & 3;
  1015. sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
  1016. sample_rate_index += 3 * (s->lsf + mpeg25);
  1017. s->sample_rate_index = sample_rate_index;
  1018. s->error_protection = ((header >> 16) & 1) ^ 1;
  1019. s->sample_rate = sample_rate;
  1020. bitrate_index = (header >> 12) & 0xf;
  1021. padding = (header >> 9) & 1;
  1022. //extension = (header >> 8) & 1;
  1023. s->mode = (header >> 6) & 3;
  1024. s->mode_ext = (header >> 4) & 3;
  1025. //copyright = (header >> 3) & 1;
  1026. //original = (header >> 2) & 1;
  1027. //emphasis = header & 3;
  1028. if (s->mode == MPA_MONO)
  1029. s->nb_channels = 1;
  1030. else
  1031. s->nb_channels = 2;
  1032. if (bitrate_index != 0) {
  1033. frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
  1034. s->bit_rate = frame_size * 1000;
  1035. switch(s->layer) {
  1036. case 1:
  1037. frame_size = (frame_size * 12000) / sample_rate;
  1038. frame_size = (frame_size + padding) * 4;
  1039. break;
  1040. case 2:
  1041. frame_size = (frame_size * 144000) / sample_rate;
  1042. frame_size += padding;
  1043. break;
  1044. default:
  1045. case 3:
  1046. frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
  1047. frame_size += padding;
  1048. break;
  1049. }
  1050. s->frame_size = frame_size;
  1051. } else {
  1052. /* if no frame size computed, signal it */
  1053. if (!s->free_format_frame_size)
  1054. return 1;
  1055. /* free format: compute bitrate and real frame size from the
  1056. frame size we extracted by reading the bitstream */
  1057. s->frame_size = s->free_format_frame_size;
  1058. switch(s->layer) {
  1059. case 1:
  1060. s->frame_size += padding * 4;
  1061. s->bit_rate = (s->frame_size * sample_rate) / 48000;
  1062. break;
  1063. case 2:
  1064. s->frame_size += padding;
  1065. s->bit_rate = (s->frame_size * sample_rate) / 144000;
  1066. break;
  1067. default:
  1068. case 3:
  1069. s->frame_size += padding;
  1070. s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
  1071. break;
  1072. }
  1073. }
  1074. #if defined(DEBUG)
  1075. printf("layer%d, %d Hz, %d kbits/s, ",
  1076. s->layer, s->sample_rate, s->bit_rate);
  1077. if (s->nb_channels == 2) {
  1078. if (s->layer == 3) {
  1079. if (s->mode_ext & MODE_EXT_MS_STEREO)
  1080. printf("ms-");
  1081. if (s->mode_ext & MODE_EXT_I_STEREO)
  1082. printf("i-");
  1083. }
  1084. printf("stereo");
  1085. } else {
  1086. printf("mono");
  1087. }
  1088. printf("\n");
  1089. #endif
  1090. return 0;
  1091. }
  1092. /* useful helper to get mpeg audio stream infos. Return -1 if error in
  1093. header, otherwise the coded frame size in bytes */
  1094. int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
  1095. {
  1096. MPADecodeContext s1, *s = &s1;
  1097. memset( s, 0, sizeof(MPADecodeContext) );
  1098. if (ff_mpa_check_header(head) != 0)
  1099. return -1;
  1100. if (decode_header(s, head) != 0) {
  1101. return -1;
  1102. }
  1103. switch(s->layer) {
  1104. case 1:
  1105. avctx->frame_size = 384;
  1106. break;
  1107. case 2:
  1108. avctx->frame_size = 1152;
  1109. break;
  1110. default:
  1111. case 3:
  1112. if (s->lsf)
  1113. avctx->frame_size = 576;
  1114. else
  1115. avctx->frame_size = 1152;
  1116. break;
  1117. }
  1118. avctx->sample_rate = s->sample_rate;
  1119. avctx->channels = s->nb_channels;
  1120. avctx->bit_rate = s->bit_rate;
  1121. avctx->sub_id = s->layer;
  1122. return s->frame_size;
  1123. }
  1124. /* return the number of decoded frames */
  1125. static int mp_decode_layer1(MPADecodeContext *s)
  1126. {
  1127. int bound, i, v, n, ch, j, mant;
  1128. uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
  1129. uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
  1130. if (s->mode == MPA_JSTEREO)
  1131. bound = (s->mode_ext + 1) * 4;
  1132. else
  1133. bound = SBLIMIT;
  1134. /* allocation bits */
  1135. for(i=0;i<bound;i++) {
  1136. for(ch=0;ch<s->nb_channels;ch++) {
  1137. allocation[ch][i] = get_bits(&s->gb, 4);
  1138. }
  1139. }
  1140. for(i=bound;i<SBLIMIT;i++) {
  1141. allocation[0][i] = get_bits(&s->gb, 4);
  1142. }
  1143. /* scale factors */
  1144. for(i=0;i<bound;i++) {
  1145. for(ch=0;ch<s->nb_channels;ch++) {
  1146. if (allocation[ch][i])
  1147. scale_factors[ch][i] = get_bits(&s->gb, 6);
  1148. }
  1149. }
  1150. for(i=bound;i<SBLIMIT;i++) {
  1151. if (allocation[0][i]) {
  1152. scale_factors[0][i] = get_bits(&s->gb, 6);
  1153. scale_factors[1][i] = get_bits(&s->gb, 6);
  1154. }
  1155. }
  1156. /* compute samples */
  1157. for(j=0;j<12;j++) {
  1158. for(i=0;i<bound;i++) {
  1159. for(ch=0;ch<s->nb_channels;ch++) {
  1160. n = allocation[ch][i];
  1161. if (n) {
  1162. mant = get_bits(&s->gb, n + 1);
  1163. v = l1_unscale(n, mant, scale_factors[ch][i]);
  1164. } else {
  1165. v = 0;
  1166. }
  1167. s->sb_samples[ch][j][i] = v;
  1168. }
  1169. }
  1170. for(i=bound;i<SBLIMIT;i++) {
  1171. n = allocation[0][i];
  1172. if (n) {
  1173. mant = get_bits(&s->gb, n + 1);
  1174. v = l1_unscale(n, mant, scale_factors[0][i]);
  1175. s->sb_samples[0][j][i] = v;
  1176. v = l1_unscale(n, mant, scale_factors[1][i]);
  1177. s->sb_samples[1][j][i] = v;
  1178. } else {
  1179. s->sb_samples[0][j][i] = 0;
  1180. s->sb_samples[1][j][i] = 0;
  1181. }
  1182. }
  1183. }
  1184. return 12;
  1185. }
  1186. /* bitrate is in kb/s */
  1187. int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
  1188. {
  1189. int ch_bitrate, table;
  1190. ch_bitrate = bitrate / nb_channels;
  1191. if (!lsf) {
  1192. if ((freq == 48000 && ch_bitrate >= 56) ||
  1193. (ch_bitrate >= 56 && ch_bitrate <= 80))
  1194. table = 0;
  1195. else if (freq != 48000 && ch_bitrate >= 96)
  1196. table = 1;
  1197. else if (freq != 32000 && ch_bitrate <= 48)
  1198. table = 2;
  1199. else
  1200. table = 3;
  1201. } else {
  1202. table = 4;
  1203. }
  1204. return table;
  1205. }
  1206. static int mp_decode_layer2(MPADecodeContext *s)
  1207. {
  1208. int sblimit; /* number of used subbands */
  1209. const unsigned char *alloc_table;
  1210. int table, bit_alloc_bits, i, j, ch, bound, v;
  1211. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
  1212. unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
  1213. unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
  1214. int scale, qindex, bits, steps, k, l, m, b;
  1215. /* select decoding table */
  1216. table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
  1217. s->sample_rate, s->lsf);
  1218. sblimit = sblimit_table[table];
  1219. alloc_table = alloc_tables[table];
  1220. if (s->mode == MPA_JSTEREO)
  1221. bound = (s->mode_ext + 1) * 4;
  1222. else
  1223. bound = sblimit;
  1224. dprintf("bound=%d sblimit=%d\n", bound, sblimit);
  1225. /* sanity check */
  1226. if( bound > sblimit ) bound = sblimit;
  1227. /* parse bit allocation */
  1228. j = 0;
  1229. for(i=0;i<bound;i++) {
  1230. bit_alloc_bits = alloc_table[j];
  1231. for(ch=0;ch<s->nb_channels;ch++) {
  1232. bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
  1233. }
  1234. j += 1 << bit_alloc_bits;
  1235. }
  1236. for(i=bound;i<sblimit;i++) {
  1237. bit_alloc_bits = alloc_table[j];
  1238. v = get_bits(&s->gb, bit_alloc_bits);
  1239. bit_alloc[0][i] = v;
  1240. bit_alloc[1][i] = v;
  1241. j += 1 << bit_alloc_bits;
  1242. }
  1243. #ifdef DEBUG
  1244. {
  1245. for(ch=0;ch<s->nb_channels;ch++) {
  1246. for(i=0;i<sblimit;i++)
  1247. printf(" %d", bit_alloc[ch][i]);
  1248. printf("\n");
  1249. }
  1250. }
  1251. #endif
  1252. /* scale codes */
  1253. for(i=0;i<sblimit;i++) {
  1254. for(ch=0;ch<s->nb_channels;ch++) {
  1255. if (bit_alloc[ch][i])
  1256. scale_code[ch][i] = get_bits(&s->gb, 2);
  1257. }
  1258. }
  1259. /* scale factors */
  1260. for(i=0;i<sblimit;i++) {
  1261. for(ch=0;ch<s->nb_channels;ch++) {
  1262. if (bit_alloc[ch][i]) {
  1263. sf = scale_factors[ch][i];
  1264. switch(scale_code[ch][i]) {
  1265. default:
  1266. case 0:
  1267. sf[0] = get_bits(&s->gb, 6);
  1268. sf[1] = get_bits(&s->gb, 6);
  1269. sf[2] = get_bits(&s->gb, 6);
  1270. break;
  1271. case 2:
  1272. sf[0] = get_bits(&s->gb, 6);
  1273. sf[1] = sf[0];
  1274. sf[2] = sf[0];
  1275. break;
  1276. case 1:
  1277. sf[0] = get_bits(&s->gb, 6);
  1278. sf[2] = get_bits(&s->gb, 6);
  1279. sf[1] = sf[0];
  1280. break;
  1281. case 3:
  1282. sf[0] = get_bits(&s->gb, 6);
  1283. sf[2] = get_bits(&s->gb, 6);
  1284. sf[1] = sf[2];
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. #ifdef DEBUG
  1291. for(ch=0;ch<s->nb_channels;ch++) {
  1292. for(i=0;i<sblimit;i++) {
  1293. if (bit_alloc[ch][i]) {
  1294. sf = scale_factors[ch][i];
  1295. printf(" %d %d %d", sf[0], sf[1], sf[2]);
  1296. } else {
  1297. printf(" -");
  1298. }
  1299. }
  1300. printf("\n");
  1301. }
  1302. #endif
  1303. /* samples */
  1304. for(k=0;k<3;k++) {
  1305. for(l=0;l<12;l+=3) {
  1306. j = 0;
  1307. for(i=0;i<bound;i++) {
  1308. bit_alloc_bits = alloc_table[j];
  1309. for(ch=0;ch<s->nb_channels;ch++) {
  1310. b = bit_alloc[ch][i];
  1311. if (b) {
  1312. scale = scale_factors[ch][i][k];
  1313. qindex = alloc_table[j+b];
  1314. bits = quant_bits[qindex];
  1315. if (bits < 0) {
  1316. /* 3 values at the same time */
  1317. v = get_bits(&s->gb, -bits);
  1318. steps = quant_steps[qindex];
  1319. s->sb_samples[ch][k * 12 + l + 0][i] =
  1320. l2_unscale_group(steps, v % steps, scale);
  1321. v = v / steps;
  1322. s->sb_samples[ch][k * 12 + l + 1][i] =
  1323. l2_unscale_group(steps, v % steps, scale);
  1324. v = v / steps;
  1325. s->sb_samples[ch][k * 12 + l + 2][i] =
  1326. l2_unscale_group(steps, v, scale);
  1327. } else {
  1328. for(m=0;m<3;m++) {
  1329. v = get_bits(&s->gb, bits);
  1330. v = l1_unscale(bits - 1, v, scale);
  1331. s->sb_samples[ch][k * 12 + l + m][i] = v;
  1332. }
  1333. }
  1334. } else {
  1335. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  1336. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  1337. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  1338. }
  1339. }
  1340. /* next subband in alloc table */
  1341. j += 1 << bit_alloc_bits;
  1342. }
  1343. /* XXX: find a way to avoid this duplication of code */
  1344. for(i=bound;i<sblimit;i++) {
  1345. bit_alloc_bits = alloc_table[j];
  1346. b = bit_alloc[0][i];
  1347. if (b) {
  1348. int mant, scale0, scale1;
  1349. scale0 = scale_factors[0][i][k];
  1350. scale1 = scale_factors[1][i][k];
  1351. qindex = alloc_table[j+b];
  1352. bits = quant_bits[qindex];
  1353. if (bits < 0) {
  1354. /* 3 values at the same time */
  1355. v = get_bits(&s->gb, -bits);
  1356. steps = quant_steps[qindex];
  1357. mant = v % steps;
  1358. v = v / steps;
  1359. s->sb_samples[0][k * 12 + l + 0][i] =
  1360. l2_unscale_group(steps, mant, scale0);
  1361. s->sb_samples[1][k * 12 + l + 0][i] =
  1362. l2_unscale_group(steps, mant, scale1);
  1363. mant = v % steps;
  1364. v = v / steps;
  1365. s->sb_samples[0][k * 12 + l + 1][i] =
  1366. l2_unscale_group(steps, mant, scale0);
  1367. s->sb_samples[1][k * 12 + l + 1][i] =
  1368. l2_unscale_group(steps, mant, scale1);
  1369. s->sb_samples[0][k * 12 + l + 2][i] =
  1370. l2_unscale_group(steps, v, scale0);
  1371. s->sb_samples[1][k * 12 + l + 2][i] =
  1372. l2_unscale_group(steps, v, scale1);
  1373. } else {
  1374. for(m=0;m<3;m++) {
  1375. mant = get_bits(&s->gb, bits);
  1376. s->sb_samples[0][k * 12 + l + m][i] =
  1377. l1_unscale(bits - 1, mant, scale0);
  1378. s->sb_samples[1][k * 12 + l + m][i] =
  1379. l1_unscale(bits - 1, mant, scale1);
  1380. }
  1381. }
  1382. } else {
  1383. s->sb_samples[0][k * 12 + l + 0][i] = 0;
  1384. s->sb_samples[0][k * 12 + l + 1][i] = 0;
  1385. s->sb_samples[0][k * 12 + l + 2][i] = 0;
  1386. s->sb_samples[1][k * 12 + l + 0][i] = 0;
  1387. s->sb_samples[1][k * 12 + l + 1][i] = 0;
  1388. s->sb_samples[1][k * 12 + l + 2][i] = 0;
  1389. }
  1390. /* next subband in alloc table */
  1391. j += 1 << bit_alloc_bits;
  1392. }
  1393. /* fill remaining samples to zero */
  1394. for(i=sblimit;i<SBLIMIT;i++) {
  1395. for(ch=0;ch<s->nb_channels;ch++) {
  1396. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  1397. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  1398. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  1399. }
  1400. }
  1401. }
  1402. }
  1403. return 3 * 12;
  1404. }
  1405. /*
  1406. * Seek back in the stream for backstep bytes (at most 511 bytes)
  1407. */
  1408. static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
  1409. {
  1410. uint8_t *ptr;
  1411. /* compute current position in stream */
  1412. ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
  1413. /* copy old data before current one */
  1414. ptr -= backstep;
  1415. memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
  1416. BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
  1417. /* init get bits again */
  1418. init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
  1419. /* prepare next buffer */
  1420. s->inbuf_index ^= 1;
  1421. s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
  1422. s->old_frame_size = s->frame_size;
  1423. }
  1424. static inline void lsf_sf_expand(int *slen,
  1425. int sf, int n1, int n2, int n3)
  1426. {
  1427. if (n3) {
  1428. slen[3] = sf % n3;
  1429. sf /= n3;
  1430. } else {
  1431. slen[3] = 0;
  1432. }
  1433. if (n2) {
  1434. slen[2] = sf % n2;
  1435. sf /= n2;
  1436. } else {
  1437. slen[2] = 0;
  1438. }
  1439. slen[1] = sf % n1;
  1440. sf /= n1;
  1441. slen[0] = sf;
  1442. }
  1443. static void exponents_from_scale_factors(MPADecodeContext *s,
  1444. GranuleDef *g,
  1445. int16_t *exponents)
  1446. {
  1447. const uint8_t *bstab, *pretab;
  1448. int len, i, j, k, l, v0, shift, gain, gains[3];
  1449. int16_t *exp_ptr;
  1450. exp_ptr = exponents;
  1451. gain = g->global_gain - 210;
  1452. shift = g->scalefac_scale + 1;
  1453. bstab = band_size_long[s->sample_rate_index];
  1454. pretab = mpa_pretab[g->preflag];
  1455. for(i=0;i<g->long_end;i++) {
  1456. v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
  1457. len = bstab[i];
  1458. for(j=len;j>0;j--)
  1459. *exp_ptr++ = v0;
  1460. }
  1461. if (g->short_start < 13) {
  1462. bstab = band_size_short[s->sample_rate_index];
  1463. gains[0] = gain - (g->subblock_gain[0] << 3);
  1464. gains[1] = gain - (g->subblock_gain[1] << 3);
  1465. gains[2] = gain - (g->subblock_gain[2] << 3);
  1466. k = g->long_end;
  1467. for(i=g->short_start;i<13;i++) {
  1468. len = bstab[i];
  1469. for(l=0;l<3;l++) {
  1470. v0 = gains[l] - (g->scale_factors[k++] << shift);
  1471. for(j=len;j>0;j--)
  1472. *exp_ptr++ = v0;
  1473. }
  1474. }
  1475. }
  1476. }
  1477. /* handle n = 0 too */
  1478. static inline int get_bitsz(GetBitContext *s, int n)
  1479. {
  1480. if (n == 0)
  1481. return 0;
  1482. else
  1483. return get_bits(s, n);
  1484. }
  1485. static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
  1486. int16_t *exponents, int end_pos)
  1487. {
  1488. int s_index;
  1489. int linbits, code, x, y, l, v, i, j, k, pos;
  1490. GetBitContext last_gb;
  1491. VLC *vlc;
  1492. uint8_t *code_table;
  1493. /* low frequencies (called big values) */
  1494. s_index = 0;
  1495. for(i=0;i<3;i++) {
  1496. j = g->region_size[i];
  1497. if (j == 0)
  1498. continue;
  1499. /* select vlc table */
  1500. k = g->table_select[i];
  1501. l = mpa_huff_data[k][0];
  1502. linbits = mpa_huff_data[k][1];
  1503. vlc = &huff_vlc[l];
  1504. code_table = huff_code_table[l];
  1505. /* read huffcode and compute each couple */
  1506. for(;j>0;j--) {
  1507. if (get_bits_count(&s->gb) >= end_pos)
  1508. break;
  1509. if (code_table) {
  1510. code = get_vlc(&s->gb, vlc);
  1511. if (code < 0)
  1512. return -1;
  1513. y = code_table[code];
  1514. x = y >> 4;
  1515. y = y & 0x0f;
  1516. } else {
  1517. x = 0;
  1518. y = 0;
  1519. }
  1520. dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
  1521. i, g->region_size[i] - j, x, y, exponents[s_index]);
  1522. if (x) {
  1523. if (x == 15)
  1524. x += get_bitsz(&s->gb, linbits);
  1525. v = l3_unscale(x, exponents[s_index]);
  1526. if (get_bits1(&s->gb))
  1527. v = -v;
  1528. } else {
  1529. v = 0;
  1530. }
  1531. g->sb_hybrid[s_index++] = v;
  1532. if (y) {
  1533. if (y == 15)
  1534. y += get_bitsz(&s->gb, linbits);
  1535. v = l3_unscale(y, exponents[s_index]);
  1536. if (get_bits1(&s->gb))
  1537. v = -v;
  1538. } else {
  1539. v = 0;
  1540. }
  1541. g->sb_hybrid[s_index++] = v;
  1542. }
  1543. }
  1544. /* high frequencies */
  1545. vlc = &huff_quad_vlc[g->count1table_select];
  1546. last_gb.buffer = NULL;
  1547. while (s_index <= 572) {
  1548. pos = get_bits_count(&s->gb);
  1549. if (pos >= end_pos) {
  1550. if (pos > end_pos && last_gb.buffer != NULL) {
  1551. /* some encoders generate an incorrect size for this
  1552. part. We must go back into the data */
  1553. s_index -= 4;
  1554. s->gb = last_gb;
  1555. }
  1556. break;
  1557. }
  1558. last_gb= s->gb;
  1559. code = get_vlc(&s->gb, vlc);
  1560. dprintf("t=%d code=%d\n", g->count1table_select, code);
  1561. if (code < 0)
  1562. return -1;
  1563. for(i=0;i<4;i++) {
  1564. if (code & (8 >> i)) {
  1565. /* non zero value. Could use a hand coded function for
  1566. 'one' value */
  1567. v = l3_unscale(1, exponents[s_index]);
  1568. if(get_bits1(&s->gb))
  1569. v = -v;
  1570. } else {
  1571. v = 0;
  1572. }
  1573. g->sb_hybrid[s_index++] = v;
  1574. }
  1575. }
  1576. while (s_index < 576)
  1577. g->sb_hybrid[s_index++] = 0;
  1578. return 0;
  1579. }
  1580. /* Reorder short blocks from bitstream order to interleaved order. It
  1581. would be faster to do it in parsing, but the code would be far more
  1582. complicated */
  1583. static void reorder_block(MPADecodeContext *s, GranuleDef *g)
  1584. {
  1585. int i, j, k, len;
  1586. int32_t *ptr, *dst, *ptr1;
  1587. int32_t tmp[576];
  1588. if (g->block_type != 2)
  1589. return;
  1590. if (g->switch_point) {
  1591. if (s->sample_rate_index != 8) {
  1592. ptr = g->sb_hybrid + 36;
  1593. } else {
  1594. ptr = g->sb_hybrid + 48;
  1595. }
  1596. } else {
  1597. ptr = g->sb_hybrid;
  1598. }
  1599. for(i=g->short_start;i<13;i++) {
  1600. len = band_size_short[s->sample_rate_index][i];
  1601. ptr1 = ptr;
  1602. for(k=0;k<3;k++) {
  1603. dst = tmp + k;
  1604. for(j=len;j>0;j--) {
  1605. *dst = *ptr++;
  1606. dst += 3;
  1607. }
  1608. }
  1609. memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
  1610. }
  1611. }
  1612. #define ISQRT2 FIXR(0.70710678118654752440)
  1613. static void compute_stereo(MPADecodeContext *s,
  1614. GranuleDef *g0, GranuleDef *g1)
  1615. {
  1616. int i, j, k, l;
  1617. int32_t v1, v2;
  1618. int sf_max, tmp0, tmp1, sf, len, non_zero_found;
  1619. int32_t (*is_tab)[16];
  1620. int32_t *tab0, *tab1;
  1621. int non_zero_found_short[3];
  1622. /* intensity stereo */
  1623. if (s->mode_ext & MODE_EXT_I_STEREO) {
  1624. if (!s->lsf) {
  1625. is_tab = is_table;
  1626. sf_max = 7;
  1627. } else {
  1628. is_tab = is_table_lsf[g1->scalefac_compress & 1];
  1629. sf_max = 16;
  1630. }
  1631. tab0 = g0->sb_hybrid + 576;
  1632. tab1 = g1->sb_hybrid + 576;
  1633. non_zero_found_short[0] = 0;
  1634. non_zero_found_short[1] = 0;
  1635. non_zero_found_short[2] = 0;
  1636. k = (13 - g1->short_start) * 3 + g1->long_end - 3;
  1637. for(i = 12;i >= g1->short_start;i--) {
  1638. /* for last band, use previous scale factor */
  1639. if (i != 11)
  1640. k -= 3;
  1641. len = band_size_short[s->sample_rate_index][i];
  1642. for(l=2;l>=0;l--) {
  1643. tab0 -= len;
  1644. tab1 -= len;
  1645. if (!non_zero_found_short[l]) {
  1646. /* test if non zero band. if so, stop doing i-stereo */
  1647. for(j=0;j<len;j++) {
  1648. if (tab1[j] != 0) {
  1649. non_zero_found_short[l] = 1;
  1650. goto found1;
  1651. }
  1652. }
  1653. sf = g1->scale_factors[k + l];
  1654. if (sf >= sf_max)
  1655. goto found1;
  1656. v1 = is_tab[0][sf];
  1657. v2 = is_tab[1][sf];
  1658. for(j=0;j<len;j++) {
  1659. tmp0 = tab0[j];
  1660. tab0[j] = MULL(tmp0, v1);
  1661. tab1[j] = MULL(tmp0, v2);
  1662. }
  1663. } else {
  1664. found1:
  1665. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1666. /* lower part of the spectrum : do ms stereo
  1667. if enabled */
  1668. for(j=0;j<len;j++) {
  1669. tmp0 = tab0[j];
  1670. tmp1 = tab1[j];
  1671. tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
  1672. tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
  1673. }
  1674. }
  1675. }
  1676. }
  1677. }
  1678. non_zero_found = non_zero_found_short[0] |
  1679. non_zero_found_short[1] |
  1680. non_zero_found_short[2];
  1681. for(i = g1->long_end - 1;i >= 0;i--) {
  1682. len = band_size_long[s->sample_rate_index][i];
  1683. tab0 -= len;
  1684. tab1 -= len;
  1685. /* test if non zero band. if so, stop doing i-stereo */
  1686. if (!non_zero_found) {
  1687. for(j=0;j<len;j++) {
  1688. if (tab1[j] != 0) {
  1689. non_zero_found = 1;
  1690. goto found2;
  1691. }
  1692. }
  1693. /* for last band, use previous scale factor */
  1694. k = (i == 21) ? 20 : i;
  1695. sf = g1->scale_factors[k];
  1696. if (sf >= sf_max)
  1697. goto found2;
  1698. v1 = is_tab[0][sf];
  1699. v2 = is_tab[1][sf];
  1700. for(j=0;j<len;j++) {
  1701. tmp0 = tab0[j];
  1702. tab0[j] = MULL(tmp0, v1);
  1703. tab1[j] = MULL(tmp0, v2);
  1704. }
  1705. } else {
  1706. found2:
  1707. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1708. /* lower part of the spectrum : do ms stereo
  1709. if enabled */
  1710. for(j=0;j<len;j++) {
  1711. tmp0 = tab0[j];
  1712. tmp1 = tab1[j];
  1713. tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
  1714. tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
  1715. }
  1716. }
  1717. }
  1718. }
  1719. } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1720. /* ms stereo ONLY */
  1721. /* NOTE: the 1/sqrt(2) normalization factor is included in the
  1722. global gain */
  1723. tab0 = g0->sb_hybrid;
  1724. tab1 = g1->sb_hybrid;
  1725. for(i=0;i<576;i++) {
  1726. tmp0 = tab0[i];
  1727. tmp1 = tab1[i];
  1728. tab0[i] = tmp0 + tmp1;
  1729. tab1[i] = tmp0 - tmp1;
  1730. }
  1731. }
  1732. }
  1733. static void compute_antialias_integer(MPADecodeContext *s,
  1734. GranuleDef *g)
  1735. {
  1736. int32_t *ptr, *p0, *p1, *csa;
  1737. int n, i, j;
  1738. /* we antialias only "long" bands */
  1739. if (g->block_type == 2) {
  1740. if (!g->switch_point)
  1741. return;
  1742. /* XXX: check this for 8000Hz case */
  1743. n = 1;
  1744. } else {
  1745. n = SBLIMIT - 1;
  1746. }
  1747. ptr = g->sb_hybrid + 18;
  1748. for(i = n;i > 0;i--) {
  1749. p0 = ptr - 1;
  1750. p1 = ptr;
  1751. csa = &csa_table[0][0];
  1752. for(j=0;j<4;j++) {
  1753. int tmp0 = *p0;
  1754. int tmp1 = *p1;
  1755. #if 0
  1756. *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
  1757. *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
  1758. #else
  1759. int64_t tmp2= MUL64(tmp0 + tmp1, csa[0]);
  1760. *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
  1761. *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
  1762. #endif
  1763. p0--; p1++;
  1764. csa += 4;
  1765. tmp0 = *p0;
  1766. tmp1 = *p1;
  1767. #if 0
  1768. *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
  1769. *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
  1770. #else
  1771. tmp2= MUL64(tmp0 + tmp1, csa[0]);
  1772. *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
  1773. *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
  1774. #endif
  1775. p0--; p1++;
  1776. csa += 4;
  1777. }
  1778. ptr += 18;
  1779. }
  1780. }
  1781. static void compute_antialias_float(MPADecodeContext *s,
  1782. GranuleDef *g)
  1783. {
  1784. int32_t *ptr, *p0, *p1;
  1785. int n, i, j;
  1786. /* we antialias only "long" bands */
  1787. if (g->block_type == 2) {
  1788. if (!g->switch_point)
  1789. return;
  1790. /* XXX: check this for 8000Hz case */
  1791. n = 1;
  1792. } else {
  1793. n = SBLIMIT - 1;
  1794. }
  1795. ptr = g->sb_hybrid + 18;
  1796. for(i = n;i > 0;i--) {
  1797. float *csa = &csa_table_float[0][0];
  1798. p0 = ptr - 1;
  1799. p1 = ptr;
  1800. for(j=0;j<4;j++) {
  1801. float tmp0 = *p0;
  1802. float tmp1 = *p1;
  1803. #if 1
  1804. *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
  1805. *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
  1806. #else
  1807. float tmp2= (tmp0 + tmp1) * csa[0];
  1808. *p0 = lrintf(tmp2 - tmp1 * csa[2]);
  1809. *p1 = lrintf(tmp2 + tmp0 * csa[3]);
  1810. #endif
  1811. p0--; p1++;
  1812. csa += 4;
  1813. tmp0 = *p0;
  1814. tmp1 = *p1;
  1815. #if 1
  1816. *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
  1817. *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
  1818. #else
  1819. tmp2= (tmp0 + tmp1) * csa[0];
  1820. *p0 = lrintf(tmp2 - tmp1 * csa[2]);
  1821. *p1 = lrintf(tmp2 + tmp0 * csa[3]);
  1822. #endif
  1823. p0--; p1++;
  1824. csa += 4;
  1825. }
  1826. ptr += 18;
  1827. }
  1828. }
  1829. static void compute_imdct(MPADecodeContext *s,
  1830. GranuleDef *g,
  1831. int32_t *sb_samples,
  1832. int32_t *mdct_buf)
  1833. {
  1834. int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
  1835. int32_t in[6];
  1836. int32_t out[36];
  1837. int32_t out2[12];
  1838. int i, j, k, mdct_long_end, v, sblimit;
  1839. /* find last non zero block */
  1840. ptr = g->sb_hybrid + 576;
  1841. ptr1 = g->sb_hybrid + 2 * 18;
  1842. while (ptr >= ptr1) {
  1843. ptr -= 6;
  1844. v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
  1845. if (v != 0)
  1846. break;
  1847. }
  1848. sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
  1849. if (g->block_type == 2) {
  1850. /* XXX: check for 8000 Hz */
  1851. if (g->switch_point)
  1852. mdct_long_end = 2;
  1853. else
  1854. mdct_long_end = 0;
  1855. } else {
  1856. mdct_long_end = sblimit;
  1857. }
  1858. buf = mdct_buf;
  1859. ptr = g->sb_hybrid;
  1860. for(j=0;j<mdct_long_end;j++) {
  1861. imdct36(out, ptr);
  1862. /* apply window & overlap with previous buffer */
  1863. out_ptr = sb_samples + j;
  1864. /* select window */
  1865. if (g->switch_point && j < 2)
  1866. win1 = mdct_win[0];
  1867. else
  1868. win1 = mdct_win[g->block_type];
  1869. /* select frequency inversion */
  1870. win = win1 + ((4 * 36) & -(j & 1));
  1871. for(i=0;i<18;i++) {
  1872. *out_ptr = MULL(out[i], win[i]) + buf[i];
  1873. buf[i] = MULL(out[i + 18], win[i + 18]);
  1874. out_ptr += SBLIMIT;
  1875. }
  1876. ptr += 18;
  1877. buf += 18;
  1878. }
  1879. for(j=mdct_long_end;j<sblimit;j++) {
  1880. for(i=0;i<6;i++) {
  1881. out[i] = 0;
  1882. out[6 + i] = 0;
  1883. out[30+i] = 0;
  1884. }
  1885. /* select frequency inversion */
  1886. win = mdct_win[2] + ((4 * 36) & -(j & 1));
  1887. buf2 = out + 6;
  1888. for(k=0;k<3;k++) {
  1889. /* reorder input for short mdct */
  1890. ptr1 = ptr + k;
  1891. for(i=0;i<6;i++) {
  1892. in[i] = *ptr1;
  1893. ptr1 += 3;
  1894. }
  1895. imdct12(out2, in);
  1896. /* apply 12 point window and do small overlap */
  1897. for(i=0;i<6;i++) {
  1898. buf2[i] = MULL(out2[i], win[i]) + buf2[i];
  1899. buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
  1900. }
  1901. buf2 += 6;
  1902. }
  1903. /* overlap */
  1904. out_ptr = sb_samples + j;
  1905. for(i=0;i<18;i++) {
  1906. *out_ptr = out[i] + buf[i];
  1907. buf[i] = out[i + 18];
  1908. out_ptr += SBLIMIT;
  1909. }
  1910. ptr += 18;
  1911. buf += 18;
  1912. }
  1913. /* zero bands */
  1914. for(j=sblimit;j<SBLIMIT;j++) {
  1915. /* overlap */
  1916. out_ptr = sb_samples + j;
  1917. for(i=0;i<18;i++) {
  1918. *out_ptr = buf[i];
  1919. buf[i] = 0;
  1920. out_ptr += SBLIMIT;
  1921. }
  1922. buf += 18;
  1923. }
  1924. }
  1925. #if defined(DEBUG)
  1926. void sample_dump(int fnum, int32_t *tab, int n)
  1927. {
  1928. static FILE *files[16], *f;
  1929. char buf[512];
  1930. int i;
  1931. int32_t v;
  1932. f = files[fnum];
  1933. if (!f) {
  1934. snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
  1935. fnum,
  1936. #ifdef USE_HIGHPRECISION
  1937. "hp"
  1938. #else
  1939. "lp"
  1940. #endif
  1941. );
  1942. f = fopen(buf, "w");
  1943. if (!f)
  1944. return;
  1945. files[fnum] = f;
  1946. }
  1947. if (fnum == 0) {
  1948. static int pos = 0;
  1949. av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
  1950. for(i=0;i<n;i++) {
  1951. av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
  1952. if ((i % 18) == 17)
  1953. av_log(NULL, AV_LOG_DEBUG, "\n");
  1954. }
  1955. pos += n;
  1956. }
  1957. for(i=0;i<n;i++) {
  1958. /* normalize to 23 frac bits */
  1959. v = tab[i] << (23 - FRAC_BITS);
  1960. fwrite(&v, 1, sizeof(int32_t), f);
  1961. }
  1962. }
  1963. #endif
  1964. /* main layer3 decoding function */
  1965. static int mp_decode_layer3(MPADecodeContext *s)
  1966. {
  1967. int nb_granules, main_data_begin, private_bits;
  1968. int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
  1969. GranuleDef granules[2][2], *g;
  1970. int16_t exponents[576];
  1971. /* read side info */
  1972. if (s->lsf) {
  1973. main_data_begin = get_bits(&s->gb, 8);
  1974. if (s->nb_channels == 2)
  1975. private_bits = get_bits(&s->gb, 2);
  1976. else
  1977. private_bits = get_bits(&s->gb, 1);
  1978. nb_granules = 1;
  1979. } else {
  1980. main_data_begin = get_bits(&s->gb, 9);
  1981. if (s->nb_channels == 2)
  1982. private_bits = get_bits(&s->gb, 3);
  1983. else
  1984. private_bits = get_bits(&s->gb, 5);
  1985. nb_granules = 2;
  1986. for(ch=0;ch<s->nb_channels;ch++) {
  1987. granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
  1988. granules[ch][1].scfsi = get_bits(&s->gb, 4);
  1989. }
  1990. }
  1991. for(gr=0;gr<nb_granules;gr++) {
  1992. for(ch=0;ch<s->nb_channels;ch++) {
  1993. dprintf("gr=%d ch=%d: side_info\n", gr, ch);
  1994. g = &granules[ch][gr];
  1995. g->part2_3_length = get_bits(&s->gb, 12);
  1996. g->big_values = get_bits(&s->gb, 9);
  1997. g->global_gain = get_bits(&s->gb, 8);
  1998. /* if MS stereo only is selected, we precompute the
  1999. 1/sqrt(2) renormalization factor */
  2000. if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
  2001. MODE_EXT_MS_STEREO)
  2002. g->global_gain -= 2;
  2003. if (s->lsf)
  2004. g->scalefac_compress = get_bits(&s->gb, 9);
  2005. else
  2006. g->scalefac_compress = get_bits(&s->gb, 4);
  2007. blocksplit_flag = get_bits(&s->gb, 1);
  2008. if (blocksplit_flag) {
  2009. g->block_type = get_bits(&s->gb, 2);
  2010. if (g->block_type == 0)
  2011. return -1;
  2012. g->switch_point = get_bits(&s->gb, 1);
  2013. for(i=0;i<2;i++)
  2014. g->table_select[i] = get_bits(&s->gb, 5);
  2015. for(i=0;i<3;i++)
  2016. g->subblock_gain[i] = get_bits(&s->gb, 3);
  2017. /* compute huffman coded region sizes */
  2018. if (g->block_type == 2)
  2019. g->region_size[0] = (36 / 2);
  2020. else {
  2021. if (s->sample_rate_index <= 2)
  2022. g->region_size[0] = (36 / 2);
  2023. else if (s->sample_rate_index != 8)
  2024. g->region_size[0] = (54 / 2);
  2025. else
  2026. g->region_size[0] = (108 / 2);
  2027. }
  2028. g->region_size[1] = (576 / 2);
  2029. } else {
  2030. int region_address1, region_address2, l;
  2031. g->block_type = 0;
  2032. g->switch_point = 0;
  2033. for(i=0;i<3;i++)
  2034. g->table_select[i] = get_bits(&s->gb, 5);
  2035. /* compute huffman coded region sizes */
  2036. region_address1 = get_bits(&s->gb, 4);
  2037. region_address2 = get_bits(&s->gb, 3);
  2038. dprintf("region1=%d region2=%d\n",
  2039. region_address1, region_address2);
  2040. g->region_size[0] =
  2041. band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
  2042. l = region_address1 + region_address2 + 2;
  2043. /* should not overflow */
  2044. if (l > 22)
  2045. l = 22;
  2046. g->region_size[1] =
  2047. band_index_long[s->sample_rate_index][l] >> 1;
  2048. }
  2049. /* convert region offsets to region sizes and truncate
  2050. size to big_values */
  2051. g->region_size[2] = (576 / 2);
  2052. j = 0;
  2053. for(i=0;i<3;i++) {
  2054. k = g->region_size[i];
  2055. if (k > g->big_values)
  2056. k = g->big_values;
  2057. g->region_size[i] = k - j;
  2058. j = k;
  2059. }
  2060. /* compute band indexes */
  2061. if (g->block_type == 2) {
  2062. if (g->switch_point) {
  2063. /* if switched mode, we handle the 36 first samples as
  2064. long blocks. For 8000Hz, we handle the 48 first
  2065. exponents as long blocks (XXX: check this!) */
  2066. if (s->sample_rate_index <= 2)
  2067. g->long_end = 8;
  2068. else if (s->sample_rate_index != 8)
  2069. g->long_end = 6;
  2070. else
  2071. g->long_end = 4; /* 8000 Hz */
  2072. if (s->sample_rate_index != 8)
  2073. g->short_start = 3;
  2074. else
  2075. g->short_start = 2;
  2076. } else {
  2077. g->long_end = 0;
  2078. g->short_start = 0;
  2079. }
  2080. } else {
  2081. g->short_start = 13;
  2082. g->long_end = 22;
  2083. }
  2084. g->preflag = 0;
  2085. if (!s->lsf)
  2086. g->preflag = get_bits(&s->gb, 1);
  2087. g->scalefac_scale = get_bits(&s->gb, 1);
  2088. g->count1table_select = get_bits(&s->gb, 1);
  2089. dprintf("block_type=%d switch_point=%d\n",
  2090. g->block_type, g->switch_point);
  2091. }
  2092. }
  2093. if (!s->adu_mode) {
  2094. /* now we get bits from the main_data_begin offset */
  2095. dprintf("seekback: %d\n", main_data_begin);
  2096. seek_to_maindata(s, main_data_begin);
  2097. }
  2098. for(gr=0;gr<nb_granules;gr++) {
  2099. for(ch=0;ch<s->nb_channels;ch++) {
  2100. g = &granules[ch][gr];
  2101. bits_pos = get_bits_count(&s->gb);
  2102. if (!s->lsf) {
  2103. uint8_t *sc;
  2104. int slen, slen1, slen2;
  2105. /* MPEG1 scale factors */
  2106. slen1 = slen_table[0][g->scalefac_compress];
  2107. slen2 = slen_table[1][g->scalefac_compress];
  2108. dprintf("slen1=%d slen2=%d\n", slen1, slen2);
  2109. if (g->block_type == 2) {
  2110. n = g->switch_point ? 17 : 18;
  2111. j = 0;
  2112. for(i=0;i<n;i++)
  2113. g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
  2114. for(i=0;i<18;i++)
  2115. g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
  2116. for(i=0;i<3;i++)
  2117. g->scale_factors[j++] = 0;
  2118. } else {
  2119. sc = granules[ch][0].scale_factors;
  2120. j = 0;
  2121. for(k=0;k<4;k++) {
  2122. n = (k == 0 ? 6 : 5);
  2123. if ((g->scfsi & (0x8 >> k)) == 0) {
  2124. slen = (k < 2) ? slen1 : slen2;
  2125. for(i=0;i<n;i++)
  2126. g->scale_factors[j++] = get_bitsz(&s->gb, slen);
  2127. } else {
  2128. /* simply copy from last granule */
  2129. for(i=0;i<n;i++) {
  2130. g->scale_factors[j] = sc[j];
  2131. j++;
  2132. }
  2133. }
  2134. }
  2135. g->scale_factors[j++] = 0;
  2136. }
  2137. #if defined(DEBUG)
  2138. {
  2139. printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
  2140. g->scfsi, gr, ch);
  2141. for(i=0;i<j;i++)
  2142. printf(" %d", g->scale_factors[i]);
  2143. printf("\n");
  2144. }
  2145. #endif
  2146. } else {
  2147. int tindex, tindex2, slen[4], sl, sf;
  2148. /* LSF scale factors */
  2149. if (g->block_type == 2) {
  2150. tindex = g->switch_point ? 2 : 1;
  2151. } else {
  2152. tindex = 0;
  2153. }
  2154. sf = g->scalefac_compress;
  2155. if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
  2156. /* intensity stereo case */
  2157. sf >>= 1;
  2158. if (sf < 180) {
  2159. lsf_sf_expand(slen, sf, 6, 6, 0);
  2160. tindex2 = 3;
  2161. } else if (sf < 244) {
  2162. lsf_sf_expand(slen, sf - 180, 4, 4, 0);
  2163. tindex2 = 4;
  2164. } else {
  2165. lsf_sf_expand(slen, sf - 244, 3, 0, 0);
  2166. tindex2 = 5;
  2167. }
  2168. } else {
  2169. /* normal case */
  2170. if (sf < 400) {
  2171. lsf_sf_expand(slen, sf, 5, 4, 4);
  2172. tindex2 = 0;
  2173. } else if (sf < 500) {
  2174. lsf_sf_expand(slen, sf - 400, 5, 4, 0);
  2175. tindex2 = 1;
  2176. } else {
  2177. lsf_sf_expand(slen, sf - 500, 3, 0, 0);
  2178. tindex2 = 2;
  2179. g->preflag = 1;
  2180. }
  2181. }
  2182. j = 0;
  2183. for(k=0;k<4;k++) {
  2184. n = lsf_nsf_table[tindex2][tindex][k];
  2185. sl = slen[k];
  2186. for(i=0;i<n;i++)
  2187. g->scale_factors[j++] = get_bitsz(&s->gb, sl);
  2188. }
  2189. /* XXX: should compute exact size */
  2190. for(;j<40;j++)
  2191. g->scale_factors[j] = 0;
  2192. #if defined(DEBUG)
  2193. {
  2194. printf("gr=%d ch=%d scale_factors:\n",
  2195. gr, ch);
  2196. for(i=0;i<40;i++)
  2197. printf(" %d", g->scale_factors[i]);
  2198. printf("\n");
  2199. }
  2200. #endif
  2201. }
  2202. exponents_from_scale_factors(s, g, exponents);
  2203. /* read Huffman coded residue */
  2204. if (huffman_decode(s, g, exponents,
  2205. bits_pos + g->part2_3_length) < 0)
  2206. return -1;
  2207. #if defined(DEBUG)
  2208. sample_dump(0, g->sb_hybrid, 576);
  2209. #endif
  2210. /* skip extension bits */
  2211. bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
  2212. if (bits_left < 0) {
  2213. dprintf("bits_left=%d\n", bits_left);
  2214. return -1;
  2215. }
  2216. while (bits_left >= 16) {
  2217. skip_bits(&s->gb, 16);
  2218. bits_left -= 16;
  2219. }
  2220. if (bits_left > 0)
  2221. skip_bits(&s->gb, bits_left);
  2222. } /* ch */
  2223. if (s->nb_channels == 2)
  2224. compute_stereo(s, &granules[0][gr], &granules[1][gr]);
  2225. for(ch=0;ch<s->nb_channels;ch++) {
  2226. g = &granules[ch][gr];
  2227. reorder_block(s, g);
  2228. #if defined(DEBUG)
  2229. sample_dump(0, g->sb_hybrid, 576);
  2230. #endif
  2231. s->compute_antialias(s, g);
  2232. #if defined(DEBUG)
  2233. sample_dump(1, g->sb_hybrid, 576);
  2234. #endif
  2235. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  2236. #if defined(DEBUG)
  2237. sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
  2238. #endif
  2239. }
  2240. } /* gr */
  2241. return nb_granules * 18;
  2242. }
  2243. static int mp_decode_frame(MPADecodeContext *s,
  2244. OUT_INT *samples)
  2245. {
  2246. int i, nb_frames, ch;
  2247. OUT_INT *samples_ptr;
  2248. init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
  2249. (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
  2250. /* skip error protection field */
  2251. if (s->error_protection)
  2252. get_bits(&s->gb, 16);
  2253. dprintf("frame %d:\n", s->frame_count);
  2254. switch(s->layer) {
  2255. case 1:
  2256. nb_frames = mp_decode_layer1(s);
  2257. break;
  2258. case 2:
  2259. nb_frames = mp_decode_layer2(s);
  2260. break;
  2261. case 3:
  2262. default:
  2263. nb_frames = mp_decode_layer3(s);
  2264. break;
  2265. }
  2266. #if defined(DEBUG)
  2267. for(i=0;i<nb_frames;i++) {
  2268. for(ch=0;ch<s->nb_channels;ch++) {
  2269. int j;
  2270. printf("%d-%d:", i, ch);
  2271. for(j=0;j<SBLIMIT;j++)
  2272. printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
  2273. printf("\n");
  2274. }
  2275. }
  2276. #endif
  2277. /* apply the synthesis filter */
  2278. for(ch=0;ch<s->nb_channels;ch++) {
  2279. samples_ptr = samples + ch;
  2280. for(i=0;i<nb_frames;i++) {
  2281. ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
  2282. window, &s->dither_state,
  2283. samples_ptr, s->nb_channels,
  2284. s->sb_samples[ch][i]);
  2285. samples_ptr += 32 * s->nb_channels;
  2286. }
  2287. }
  2288. #ifdef DEBUG
  2289. s->frame_count++;
  2290. #endif
  2291. return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
  2292. }
  2293. static int decode_frame(AVCodecContext * avctx,
  2294. void *data, int *data_size,
  2295. uint8_t * buf, int buf_size)
  2296. {
  2297. MPADecodeContext *s = avctx->priv_data;
  2298. uint32_t header;
  2299. uint8_t *buf_ptr;
  2300. int len, out_size;
  2301. OUT_INT *out_samples = data;
  2302. buf_ptr = buf;
  2303. while (buf_size > 0) {
  2304. len = s->inbuf_ptr - s->inbuf;
  2305. if (s->frame_size == 0) {
  2306. /* special case for next header for first frame in free
  2307. format case (XXX: find a simpler method) */
  2308. if (s->free_format_next_header != 0) {
  2309. s->inbuf[0] = s->free_format_next_header >> 24;
  2310. s->inbuf[1] = s->free_format_next_header >> 16;
  2311. s->inbuf[2] = s->free_format_next_header >> 8;
  2312. s->inbuf[3] = s->free_format_next_header;
  2313. s->inbuf_ptr = s->inbuf + 4;
  2314. s->free_format_next_header = 0;
  2315. goto got_header;
  2316. }
  2317. /* no header seen : find one. We need at least HEADER_SIZE
  2318. bytes to parse it */
  2319. len = HEADER_SIZE - len;
  2320. if (len > buf_size)
  2321. len = buf_size;
  2322. if (len > 0) {
  2323. memcpy(s->inbuf_ptr, buf_ptr, len);
  2324. buf_ptr += len;
  2325. buf_size -= len;
  2326. s->inbuf_ptr += len;
  2327. }
  2328. if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
  2329. got_header:
  2330. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  2331. (s->inbuf[2] << 8) | s->inbuf[3];
  2332. if (ff_mpa_check_header(header) < 0) {
  2333. /* no sync found : move by one byte (inefficient, but simple!) */
  2334. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  2335. s->inbuf_ptr--;
  2336. dprintf("skip %x\n", header);
  2337. /* reset free format frame size to give a chance
  2338. to get a new bitrate */
  2339. s->free_format_frame_size = 0;
  2340. } else {
  2341. if (decode_header(s, header) == 1) {
  2342. /* free format: prepare to compute frame size */
  2343. s->frame_size = -1;
  2344. }
  2345. /* update codec info */
  2346. avctx->sample_rate = s->sample_rate;
  2347. avctx->channels = s->nb_channels;
  2348. avctx->bit_rate = s->bit_rate;
  2349. avctx->sub_id = s->layer;
  2350. switch(s->layer) {
  2351. case 1:
  2352. avctx->frame_size = 384;
  2353. break;
  2354. case 2:
  2355. avctx->frame_size = 1152;
  2356. break;
  2357. case 3:
  2358. if (s->lsf)
  2359. avctx->frame_size = 576;
  2360. else
  2361. avctx->frame_size = 1152;
  2362. break;
  2363. }
  2364. }
  2365. }
  2366. } else if (s->frame_size == -1) {
  2367. /* free format : find next sync to compute frame size */
  2368. len = MPA_MAX_CODED_FRAME_SIZE - len;
  2369. if (len > buf_size)
  2370. len = buf_size;
  2371. if (len == 0) {
  2372. /* frame too long: resync */
  2373. s->frame_size = 0;
  2374. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  2375. s->inbuf_ptr--;
  2376. } else {
  2377. uint8_t *p, *pend;
  2378. uint32_t header1;
  2379. int padding;
  2380. memcpy(s->inbuf_ptr, buf_ptr, len);
  2381. /* check for header */
  2382. p = s->inbuf_ptr - 3;
  2383. pend = s->inbuf_ptr + len - 4;
  2384. while (p <= pend) {
  2385. header = (p[0] << 24) | (p[1] << 16) |
  2386. (p[2] << 8) | p[3];
  2387. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  2388. (s->inbuf[2] << 8) | s->inbuf[3];
  2389. /* check with high probability that we have a
  2390. valid header */
  2391. if ((header & SAME_HEADER_MASK) ==
  2392. (header1 & SAME_HEADER_MASK)) {
  2393. /* header found: update pointers */
  2394. len = (p + 4) - s->inbuf_ptr;
  2395. buf_ptr += len;
  2396. buf_size -= len;
  2397. s->inbuf_ptr = p;
  2398. /* compute frame size */
  2399. s->free_format_next_header = header;
  2400. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  2401. padding = (header1 >> 9) & 1;
  2402. if (s->layer == 1)
  2403. s->free_format_frame_size -= padding * 4;
  2404. else
  2405. s->free_format_frame_size -= padding;
  2406. dprintf("free frame size=%d padding=%d\n",
  2407. s->free_format_frame_size, padding);
  2408. decode_header(s, header1);
  2409. goto next_data;
  2410. }
  2411. p++;
  2412. }
  2413. /* not found: simply increase pointers */
  2414. buf_ptr += len;
  2415. s->inbuf_ptr += len;
  2416. buf_size -= len;
  2417. }
  2418. } else if (len < s->frame_size) {
  2419. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  2420. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  2421. len = s->frame_size - len;
  2422. if (len > buf_size)
  2423. len = buf_size;
  2424. memcpy(s->inbuf_ptr, buf_ptr, len);
  2425. buf_ptr += len;
  2426. s->inbuf_ptr += len;
  2427. buf_size -= len;
  2428. }
  2429. next_data:
  2430. if (s->frame_size > 0 &&
  2431. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  2432. if (avctx->parse_only) {
  2433. /* simply return the frame data */
  2434. *(uint8_t **)data = s->inbuf;
  2435. out_size = s->inbuf_ptr - s->inbuf;
  2436. } else {
  2437. out_size = mp_decode_frame(s, out_samples);
  2438. }
  2439. s->inbuf_ptr = s->inbuf;
  2440. s->frame_size = 0;
  2441. *data_size = out_size;
  2442. break;
  2443. }
  2444. }
  2445. return buf_ptr - buf;
  2446. }
  2447. static int decode_frame_adu(AVCodecContext * avctx,
  2448. void *data, int *data_size,
  2449. uint8_t * buf, int buf_size)
  2450. {
  2451. MPADecodeContext *s = avctx->priv_data;
  2452. uint32_t header;
  2453. int len, out_size;
  2454. OUT_INT *out_samples = data;
  2455. len = buf_size;
  2456. // Discard too short frames
  2457. if (buf_size < HEADER_SIZE) {
  2458. *data_size = 0;
  2459. return buf_size;
  2460. }
  2461. if (len > MPA_MAX_CODED_FRAME_SIZE)
  2462. len = MPA_MAX_CODED_FRAME_SIZE;
  2463. memcpy(s->inbuf, buf, len);
  2464. s->inbuf_ptr = s->inbuf + len;
  2465. // Get header and restore sync word
  2466. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  2467. (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
  2468. if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
  2469. *data_size = 0;
  2470. return buf_size;
  2471. }
  2472. decode_header(s, header);
  2473. /* update codec info */
  2474. avctx->sample_rate = s->sample_rate;
  2475. avctx->channels = s->nb_channels;
  2476. avctx->bit_rate = s->bit_rate;
  2477. avctx->sub_id = s->layer;
  2478. avctx->frame_size=s->frame_size = len;
  2479. if (avctx->parse_only) {
  2480. /* simply return the frame data */
  2481. *(uint8_t **)data = s->inbuf;
  2482. out_size = s->inbuf_ptr - s->inbuf;
  2483. } else {
  2484. out_size = mp_decode_frame(s, out_samples);
  2485. }
  2486. *data_size = out_size;
  2487. return buf_size;
  2488. }
  2489. AVCodec mp2_decoder =
  2490. {
  2491. "mp2",
  2492. CODEC_TYPE_AUDIO,
  2493. CODEC_ID_MP2,
  2494. sizeof(MPADecodeContext),
  2495. decode_init,
  2496. NULL,
  2497. NULL,
  2498. decode_frame,
  2499. CODEC_CAP_PARSE_ONLY,
  2500. };
  2501. AVCodec mp3_decoder =
  2502. {
  2503. "mp3",
  2504. CODEC_TYPE_AUDIO,
  2505. CODEC_ID_MP3,
  2506. sizeof(MPADecodeContext),
  2507. decode_init,
  2508. NULL,
  2509. NULL,
  2510. decode_frame,
  2511. CODEC_CAP_PARSE_ONLY,
  2512. };
  2513. AVCodec mp3adu_decoder =
  2514. {
  2515. "mp3adu",
  2516. CODEC_TYPE_AUDIO,
  2517. CODEC_ID_MP3ADU,
  2518. sizeof(MPADecodeContext),
  2519. decode_init,
  2520. NULL,
  2521. NULL,
  2522. decode_frame_adu,
  2523. CODEC_CAP_PARSE_ONLY,
  2524. };