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.

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