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.

2631 lines
79KB

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