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.

2620 lines
78KB

  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(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
  789. MPA_INT *window, int *dither_state,
  790. OUT_INT *samples, int incr,
  791. INTFLOAT sb_samples[SBLIMIT])
  792. {
  793. register MPA_INT *synth_buf;
  794. int offset;
  795. #if FRAC_BITS <= 15
  796. int32_t tmp[32];
  797. int j;
  798. #endif
  799. offset = *synth_buf_offset;
  800. synth_buf = synth_buf_ptr + offset;
  801. #if FRAC_BITS <= 15
  802. dct32(tmp, sb_samples);
  803. for(j=0;j<32;j++) {
  804. /* NOTE: can cause a loss in precision if very high amplitude
  805. sound */
  806. synth_buf[j] = av_clip_int16(tmp[j]);
  807. }
  808. #else
  809. dct32(synth_buf, sb_samples);
  810. #endif
  811. apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
  812. offset = (offset - 32) & 511;
  813. *synth_buf_offset = offset;
  814. }
  815. #endif
  816. #define C3 FIXHR(0.86602540378443864676/2)
  817. /* 0.5 / cos(pi*(2*i+1)/36) */
  818. static const INTFLOAT icos36[9] = {
  819. FIXR(0.50190991877167369479),
  820. FIXR(0.51763809020504152469), //0
  821. FIXR(0.55168895948124587824),
  822. FIXR(0.61038729438072803416),
  823. FIXR(0.70710678118654752439), //1
  824. FIXR(0.87172339781054900991),
  825. FIXR(1.18310079157624925896),
  826. FIXR(1.93185165257813657349), //2
  827. FIXR(5.73685662283492756461),
  828. };
  829. /* 0.5 / cos(pi*(2*i+1)/36) */
  830. static const INTFLOAT icos36h[9] = {
  831. FIXHR(0.50190991877167369479/2),
  832. FIXHR(0.51763809020504152469/2), //0
  833. FIXHR(0.55168895948124587824/2),
  834. FIXHR(0.61038729438072803416/2),
  835. FIXHR(0.70710678118654752439/2), //1
  836. FIXHR(0.87172339781054900991/2),
  837. FIXHR(1.18310079157624925896/4),
  838. FIXHR(1.93185165257813657349/4), //2
  839. // FIXHR(5.73685662283492756461),
  840. };
  841. /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
  842. cases. */
  843. static void imdct12(INTFLOAT *out, INTFLOAT *in)
  844. {
  845. INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
  846. in0= in[0*3];
  847. in1= in[1*3] + in[0*3];
  848. in2= in[2*3] + in[1*3];
  849. in3= in[3*3] + in[2*3];
  850. in4= in[4*3] + in[3*3];
  851. in5= in[5*3] + in[4*3];
  852. in5 += in3;
  853. in3 += in1;
  854. in2= MULH3(in2, C3, 2);
  855. in3= MULH3(in3, C3, 4);
  856. t1 = in0 - in4;
  857. t2 = MULH3(in1 - in5, icos36h[4], 2);
  858. out[ 7]=
  859. out[10]= t1 + t2;
  860. out[ 1]=
  861. out[ 4]= t1 - t2;
  862. in0 += SHR(in4, 1);
  863. in4 = in0 + in2;
  864. in5 += 2*in1;
  865. in1 = MULH3(in5 + in3, icos36h[1], 1);
  866. out[ 8]=
  867. out[ 9]= in4 + in1;
  868. out[ 2]=
  869. out[ 3]= in4 - in1;
  870. in0 -= in2;
  871. in5 = MULH3(in5 - in3, icos36h[7], 2);
  872. out[ 0]=
  873. out[ 5]= in0 - in5;
  874. out[ 6]=
  875. out[11]= in0 + in5;
  876. }
  877. /* cos(pi*i/18) */
  878. #define C1 FIXHR(0.98480775301220805936/2)
  879. #define C2 FIXHR(0.93969262078590838405/2)
  880. #define C3 FIXHR(0.86602540378443864676/2)
  881. #define C4 FIXHR(0.76604444311897803520/2)
  882. #define C5 FIXHR(0.64278760968653932632/2)
  883. #define C6 FIXHR(0.5/2)
  884. #define C7 FIXHR(0.34202014332566873304/2)
  885. #define C8 FIXHR(0.17364817766693034885/2)
  886. /* using Lee like decomposition followed by hand coded 9 points DCT */
  887. static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
  888. {
  889. int i, j;
  890. INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
  891. INTFLOAT tmp[18], *tmp1, *in1;
  892. for(i=17;i>=1;i--)
  893. in[i] += in[i-1];
  894. for(i=17;i>=3;i-=2)
  895. in[i] += in[i-2];
  896. for(j=0;j<2;j++) {
  897. tmp1 = tmp + j;
  898. in1 = in + j;
  899. t2 = in1[2*4] + in1[2*8] - in1[2*2];
  900. t3 = in1[2*0] + SHR(in1[2*6],1);
  901. t1 = in1[2*0] - in1[2*6];
  902. tmp1[ 6] = t1 - SHR(t2,1);
  903. tmp1[16] = t1 + t2;
  904. t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
  905. t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
  906. t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
  907. tmp1[10] = t3 - t0 - t2;
  908. tmp1[ 2] = t3 + t0 + t1;
  909. tmp1[14] = t3 + t2 - t1;
  910. tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
  911. t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
  912. t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
  913. t0 = MULH3(in1[2*3], C3, 2);
  914. t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
  915. tmp1[ 0] = t2 + t3 + t0;
  916. tmp1[12] = t2 + t1 - t0;
  917. tmp1[ 8] = t3 - t1 - t0;
  918. }
  919. i = 0;
  920. for(j=0;j<4;j++) {
  921. t0 = tmp[i];
  922. t1 = tmp[i + 2];
  923. s0 = t1 + t0;
  924. s2 = t1 - t0;
  925. t2 = tmp[i + 1];
  926. t3 = tmp[i + 3];
  927. s1 = MULH3(t3 + t2, icos36h[j], 2);
  928. s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
  929. t0 = s0 + s1;
  930. t1 = s0 - s1;
  931. out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
  932. out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
  933. buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
  934. buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
  935. t0 = s2 + s3;
  936. t1 = s2 - s3;
  937. out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
  938. out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
  939. buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
  940. buf[ + j] = MULH3(t0, win[18 + j], 1);
  941. i += 4;
  942. }
  943. s0 = tmp[16];
  944. s1 = MULH3(tmp[17], icos36h[4], 2);
  945. t0 = s0 + s1;
  946. t1 = s0 - s1;
  947. out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
  948. out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
  949. buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
  950. buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
  951. }
  952. /* return the number of decoded frames */
  953. static int mp_decode_layer1(MPADecodeContext *s)
  954. {
  955. int bound, i, v, n, ch, j, mant;
  956. uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
  957. uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
  958. if (s->mode == MPA_JSTEREO)
  959. bound = (s->mode_ext + 1) * 4;
  960. else
  961. bound = SBLIMIT;
  962. /* allocation bits */
  963. for(i=0;i<bound;i++) {
  964. for(ch=0;ch<s->nb_channels;ch++) {
  965. allocation[ch][i] = get_bits(&s->gb, 4);
  966. }
  967. }
  968. for(i=bound;i<SBLIMIT;i++) {
  969. allocation[0][i] = get_bits(&s->gb, 4);
  970. }
  971. /* scale factors */
  972. for(i=0;i<bound;i++) {
  973. for(ch=0;ch<s->nb_channels;ch++) {
  974. if (allocation[ch][i])
  975. scale_factors[ch][i] = get_bits(&s->gb, 6);
  976. }
  977. }
  978. for(i=bound;i<SBLIMIT;i++) {
  979. if (allocation[0][i]) {
  980. scale_factors[0][i] = get_bits(&s->gb, 6);
  981. scale_factors[1][i] = get_bits(&s->gb, 6);
  982. }
  983. }
  984. /* compute samples */
  985. for(j=0;j<12;j++) {
  986. for(i=0;i<bound;i++) {
  987. for(ch=0;ch<s->nb_channels;ch++) {
  988. n = allocation[ch][i];
  989. if (n) {
  990. mant = get_bits(&s->gb, n + 1);
  991. v = l1_unscale(n, mant, scale_factors[ch][i]);
  992. } else {
  993. v = 0;
  994. }
  995. s->sb_samples[ch][j][i] = v;
  996. }
  997. }
  998. for(i=bound;i<SBLIMIT;i++) {
  999. n = allocation[0][i];
  1000. if (n) {
  1001. mant = get_bits(&s->gb, n + 1);
  1002. v = l1_unscale(n, mant, scale_factors[0][i]);
  1003. s->sb_samples[0][j][i] = v;
  1004. v = l1_unscale(n, mant, scale_factors[1][i]);
  1005. s->sb_samples[1][j][i] = v;
  1006. } else {
  1007. s->sb_samples[0][j][i] = 0;
  1008. s->sb_samples[1][j][i] = 0;
  1009. }
  1010. }
  1011. }
  1012. return 12;
  1013. }
  1014. static int mp_decode_layer2(MPADecodeContext *s)
  1015. {
  1016. int sblimit; /* number of used subbands */
  1017. const unsigned char *alloc_table;
  1018. int table, bit_alloc_bits, i, j, ch, bound, v;
  1019. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
  1020. unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
  1021. unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
  1022. int scale, qindex, bits, steps, k, l, m, b;
  1023. /* select decoding table */
  1024. table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
  1025. s->sample_rate, s->lsf);
  1026. sblimit = ff_mpa_sblimit_table[table];
  1027. alloc_table = ff_mpa_alloc_tables[table];
  1028. if (s->mode == MPA_JSTEREO)
  1029. bound = (s->mode_ext + 1) * 4;
  1030. else
  1031. bound = sblimit;
  1032. dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
  1033. /* sanity check */
  1034. if( bound > sblimit ) bound = sblimit;
  1035. /* parse bit allocation */
  1036. j = 0;
  1037. for(i=0;i<bound;i++) {
  1038. bit_alloc_bits = alloc_table[j];
  1039. for(ch=0;ch<s->nb_channels;ch++) {
  1040. bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
  1041. }
  1042. j += 1 << bit_alloc_bits;
  1043. }
  1044. for(i=bound;i<sblimit;i++) {
  1045. bit_alloc_bits = alloc_table[j];
  1046. v = get_bits(&s->gb, bit_alloc_bits);
  1047. bit_alloc[0][i] = v;
  1048. bit_alloc[1][i] = v;
  1049. j += 1 << bit_alloc_bits;
  1050. }
  1051. /* scale codes */
  1052. for(i=0;i<sblimit;i++) {
  1053. for(ch=0;ch<s->nb_channels;ch++) {
  1054. if (bit_alloc[ch][i])
  1055. scale_code[ch][i] = get_bits(&s->gb, 2);
  1056. }
  1057. }
  1058. /* scale factors */
  1059. for(i=0;i<sblimit;i++) {
  1060. for(ch=0;ch<s->nb_channels;ch++) {
  1061. if (bit_alloc[ch][i]) {
  1062. sf = scale_factors[ch][i];
  1063. switch(scale_code[ch][i]) {
  1064. default:
  1065. case 0:
  1066. sf[0] = get_bits(&s->gb, 6);
  1067. sf[1] = get_bits(&s->gb, 6);
  1068. sf[2] = get_bits(&s->gb, 6);
  1069. break;
  1070. case 2:
  1071. sf[0] = get_bits(&s->gb, 6);
  1072. sf[1] = sf[0];
  1073. sf[2] = sf[0];
  1074. break;
  1075. case 1:
  1076. sf[0] = get_bits(&s->gb, 6);
  1077. sf[2] = get_bits(&s->gb, 6);
  1078. sf[1] = sf[0];
  1079. break;
  1080. case 3:
  1081. sf[0] = get_bits(&s->gb, 6);
  1082. sf[2] = get_bits(&s->gb, 6);
  1083. sf[1] = sf[2];
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. }
  1089. /* samples */
  1090. for(k=0;k<3;k++) {
  1091. for(l=0;l<12;l+=3) {
  1092. j = 0;
  1093. for(i=0;i<bound;i++) {
  1094. bit_alloc_bits = alloc_table[j];
  1095. for(ch=0;ch<s->nb_channels;ch++) {
  1096. b = bit_alloc[ch][i];
  1097. if (b) {
  1098. scale = scale_factors[ch][i][k];
  1099. qindex = alloc_table[j+b];
  1100. bits = ff_mpa_quant_bits[qindex];
  1101. if (bits < 0) {
  1102. /* 3 values at the same time */
  1103. v = get_bits(&s->gb, -bits);
  1104. steps = ff_mpa_quant_steps[qindex];
  1105. s->sb_samples[ch][k * 12 + l + 0][i] =
  1106. l2_unscale_group(steps, v % steps, scale);
  1107. v = v / steps;
  1108. s->sb_samples[ch][k * 12 + l + 1][i] =
  1109. l2_unscale_group(steps, v % steps, scale);
  1110. v = v / steps;
  1111. s->sb_samples[ch][k * 12 + l + 2][i] =
  1112. l2_unscale_group(steps, v, scale);
  1113. } else {
  1114. for(m=0;m<3;m++) {
  1115. v = get_bits(&s->gb, bits);
  1116. v = l1_unscale(bits - 1, v, scale);
  1117. s->sb_samples[ch][k * 12 + l + m][i] = v;
  1118. }
  1119. }
  1120. } else {
  1121. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  1122. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  1123. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  1124. }
  1125. }
  1126. /* next subband in alloc table */
  1127. j += 1 << bit_alloc_bits;
  1128. }
  1129. /* XXX: find a way to avoid this duplication of code */
  1130. for(i=bound;i<sblimit;i++) {
  1131. bit_alloc_bits = alloc_table[j];
  1132. b = bit_alloc[0][i];
  1133. if (b) {
  1134. int mant, scale0, scale1;
  1135. scale0 = scale_factors[0][i][k];
  1136. scale1 = scale_factors[1][i][k];
  1137. qindex = alloc_table[j+b];
  1138. bits = ff_mpa_quant_bits[qindex];
  1139. if (bits < 0) {
  1140. /* 3 values at the same time */
  1141. v = get_bits(&s->gb, -bits);
  1142. steps = ff_mpa_quant_steps[qindex];
  1143. mant = v % steps;
  1144. v = v / steps;
  1145. s->sb_samples[0][k * 12 + l + 0][i] =
  1146. l2_unscale_group(steps, mant, scale0);
  1147. s->sb_samples[1][k * 12 + l + 0][i] =
  1148. l2_unscale_group(steps, mant, scale1);
  1149. mant = v % steps;
  1150. v = v / steps;
  1151. s->sb_samples[0][k * 12 + l + 1][i] =
  1152. l2_unscale_group(steps, mant, scale0);
  1153. s->sb_samples[1][k * 12 + l + 1][i] =
  1154. l2_unscale_group(steps, mant, scale1);
  1155. s->sb_samples[0][k * 12 + l + 2][i] =
  1156. l2_unscale_group(steps, v, scale0);
  1157. s->sb_samples[1][k * 12 + l + 2][i] =
  1158. l2_unscale_group(steps, v, scale1);
  1159. } else {
  1160. for(m=0;m<3;m++) {
  1161. mant = get_bits(&s->gb, bits);
  1162. s->sb_samples[0][k * 12 + l + m][i] =
  1163. l1_unscale(bits - 1, mant, scale0);
  1164. s->sb_samples[1][k * 12 + l + m][i] =
  1165. l1_unscale(bits - 1, mant, scale1);
  1166. }
  1167. }
  1168. } else {
  1169. s->sb_samples[0][k * 12 + l + 0][i] = 0;
  1170. s->sb_samples[0][k * 12 + l + 1][i] = 0;
  1171. s->sb_samples[0][k * 12 + l + 2][i] = 0;
  1172. s->sb_samples[1][k * 12 + l + 0][i] = 0;
  1173. s->sb_samples[1][k * 12 + l + 1][i] = 0;
  1174. s->sb_samples[1][k * 12 + l + 2][i] = 0;
  1175. }
  1176. /* next subband in alloc table */
  1177. j += 1 << bit_alloc_bits;
  1178. }
  1179. /* fill remaining samples to zero */
  1180. for(i=sblimit;i<SBLIMIT;i++) {
  1181. for(ch=0;ch<s->nb_channels;ch++) {
  1182. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  1183. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  1184. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. return 3 * 12;
  1190. }
  1191. #define SPLIT(dst,sf,n)\
  1192. if(n==3){\
  1193. int m= (sf*171)>>9;\
  1194. dst= sf - 3*m;\
  1195. sf=m;\
  1196. }else if(n==4){\
  1197. dst= sf&3;\
  1198. sf>>=2;\
  1199. }else if(n==5){\
  1200. int m= (sf*205)>>10;\
  1201. dst= sf - 5*m;\
  1202. sf=m;\
  1203. }else if(n==6){\
  1204. int m= (sf*171)>>10;\
  1205. dst= sf - 6*m;\
  1206. sf=m;\
  1207. }else{\
  1208. dst=0;\
  1209. }
  1210. static av_always_inline void lsf_sf_expand(int *slen,
  1211. int sf, int n1, int n2, int n3)
  1212. {
  1213. SPLIT(slen[3], sf, n3)
  1214. SPLIT(slen[2], sf, n2)
  1215. SPLIT(slen[1], sf, n1)
  1216. slen[0] = sf;
  1217. }
  1218. static void exponents_from_scale_factors(MPADecodeContext *s,
  1219. GranuleDef *g,
  1220. int16_t *exponents)
  1221. {
  1222. const uint8_t *bstab, *pretab;
  1223. int len, i, j, k, l, v0, shift, gain, gains[3];
  1224. int16_t *exp_ptr;
  1225. exp_ptr = exponents;
  1226. gain = g->global_gain - 210;
  1227. shift = g->scalefac_scale + 1;
  1228. bstab = band_size_long[s->sample_rate_index];
  1229. pretab = mpa_pretab[g->preflag];
  1230. for(i=0;i<g->long_end;i++) {
  1231. v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
  1232. len = bstab[i];
  1233. for(j=len;j>0;j--)
  1234. *exp_ptr++ = v0;
  1235. }
  1236. if (g->short_start < 13) {
  1237. bstab = band_size_short[s->sample_rate_index];
  1238. gains[0] = gain - (g->subblock_gain[0] << 3);
  1239. gains[1] = gain - (g->subblock_gain[1] << 3);
  1240. gains[2] = gain - (g->subblock_gain[2] << 3);
  1241. k = g->long_end;
  1242. for(i=g->short_start;i<13;i++) {
  1243. len = bstab[i];
  1244. for(l=0;l<3;l++) {
  1245. v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
  1246. for(j=len;j>0;j--)
  1247. *exp_ptr++ = v0;
  1248. }
  1249. }
  1250. }
  1251. }
  1252. /* handle n = 0 too */
  1253. static inline int get_bitsz(GetBitContext *s, int n)
  1254. {
  1255. if (n == 0)
  1256. return 0;
  1257. else
  1258. return get_bits(s, n);
  1259. }
  1260. static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
  1261. if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
  1262. s->gb= s->in_gb;
  1263. s->in_gb.buffer=NULL;
  1264. assert((get_bits_count(&s->gb) & 7) == 0);
  1265. skip_bits_long(&s->gb, *pos - *end_pos);
  1266. *end_pos2=
  1267. *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
  1268. *pos= get_bits_count(&s->gb);
  1269. }
  1270. }
  1271. /* Following is a optimized code for
  1272. INTFLOAT v = *src
  1273. if(get_bits1(&s->gb))
  1274. v = -v;
  1275. *dst = v;
  1276. */
  1277. #if CONFIG_FLOAT
  1278. #define READ_FLIP_SIGN(dst,src)\
  1279. v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
  1280. AV_WN32A(dst, v);
  1281. #else
  1282. #define READ_FLIP_SIGN(dst,src)\
  1283. v= -get_bits1(&s->gb);\
  1284. *(dst) = (*(src) ^ v) - v;
  1285. #endif
  1286. static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
  1287. int16_t *exponents, int end_pos2)
  1288. {
  1289. int s_index;
  1290. int i;
  1291. int last_pos, bits_left;
  1292. VLC *vlc;
  1293. int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
  1294. /* low frequencies (called big values) */
  1295. s_index = 0;
  1296. for(i=0;i<3;i++) {
  1297. int j, k, l, linbits;
  1298. j = g->region_size[i];
  1299. if (j == 0)
  1300. continue;
  1301. /* select vlc table */
  1302. k = g->table_select[i];
  1303. l = mpa_huff_data[k][0];
  1304. linbits = mpa_huff_data[k][1];
  1305. vlc = &huff_vlc[l];
  1306. if(!l){
  1307. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
  1308. s_index += 2*j;
  1309. continue;
  1310. }
  1311. /* read huffcode and compute each couple */
  1312. for(;j>0;j--) {
  1313. int exponent, x, y;
  1314. int v;
  1315. int pos= get_bits_count(&s->gb);
  1316. if (pos >= end_pos){
  1317. // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
  1318. switch_buffer(s, &pos, &end_pos, &end_pos2);
  1319. // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
  1320. if(pos >= end_pos)
  1321. break;
  1322. }
  1323. y = get_vlc2(&s->gb, vlc->table, 7, 3);
  1324. if(!y){
  1325. g->sb_hybrid[s_index ] =
  1326. g->sb_hybrid[s_index+1] = 0;
  1327. s_index += 2;
  1328. continue;
  1329. }
  1330. exponent= exponents[s_index];
  1331. dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
  1332. i, g->region_size[i] - j, x, y, exponent);
  1333. if(y&16){
  1334. x = y >> 5;
  1335. y = y & 0x0f;
  1336. if (x < 15){
  1337. READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
  1338. }else{
  1339. x += get_bitsz(&s->gb, linbits);
  1340. v = l3_unscale(x, exponent);
  1341. if (get_bits1(&s->gb))
  1342. v = -v;
  1343. g->sb_hybrid[s_index] = v;
  1344. }
  1345. if (y < 15){
  1346. READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
  1347. }else{
  1348. y += get_bitsz(&s->gb, linbits);
  1349. v = l3_unscale(y, exponent);
  1350. if (get_bits1(&s->gb))
  1351. v = -v;
  1352. g->sb_hybrid[s_index+1] = v;
  1353. }
  1354. }else{
  1355. x = y >> 5;
  1356. y = y & 0x0f;
  1357. x += y;
  1358. if (x < 15){
  1359. READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
  1360. }else{
  1361. x += get_bitsz(&s->gb, linbits);
  1362. v = l3_unscale(x, exponent);
  1363. if (get_bits1(&s->gb))
  1364. v = -v;
  1365. g->sb_hybrid[s_index+!!y] = v;
  1366. }
  1367. g->sb_hybrid[s_index+ !y] = 0;
  1368. }
  1369. s_index+=2;
  1370. }
  1371. }
  1372. /* high frequencies */
  1373. vlc = &huff_quad_vlc[g->count1table_select];
  1374. last_pos=0;
  1375. while (s_index <= 572) {
  1376. int pos, code;
  1377. pos = get_bits_count(&s->gb);
  1378. if (pos >= end_pos) {
  1379. if (pos > end_pos2 && last_pos){
  1380. /* some encoders generate an incorrect size for this
  1381. part. We must go back into the data */
  1382. s_index -= 4;
  1383. skip_bits_long(&s->gb, last_pos - pos);
  1384. av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
  1385. if(s->error_recognition >= FF_ER_COMPLIANT)
  1386. s_index=0;
  1387. break;
  1388. }
  1389. // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
  1390. switch_buffer(s, &pos, &end_pos, &end_pos2);
  1391. // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
  1392. if(pos >= end_pos)
  1393. break;
  1394. }
  1395. last_pos= pos;
  1396. code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
  1397. dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
  1398. g->sb_hybrid[s_index+0]=
  1399. g->sb_hybrid[s_index+1]=
  1400. g->sb_hybrid[s_index+2]=
  1401. g->sb_hybrid[s_index+3]= 0;
  1402. while(code){
  1403. static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
  1404. int v;
  1405. int pos= s_index+idxtab[code];
  1406. code ^= 8>>idxtab[code];
  1407. READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
  1408. }
  1409. s_index+=4;
  1410. }
  1411. /* skip extension bits */
  1412. bits_left = end_pos2 - get_bits_count(&s->gb);
  1413. //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
  1414. if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
  1415. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  1416. s_index=0;
  1417. }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
  1418. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  1419. s_index=0;
  1420. }
  1421. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
  1422. skip_bits_long(&s->gb, bits_left);
  1423. i= get_bits_count(&s->gb);
  1424. switch_buffer(s, &i, &end_pos, &end_pos2);
  1425. return 0;
  1426. }
  1427. /* Reorder short blocks from bitstream order to interleaved order. It
  1428. would be faster to do it in parsing, but the code would be far more
  1429. complicated */
  1430. static void reorder_block(MPADecodeContext *s, GranuleDef *g)
  1431. {
  1432. int i, j, len;
  1433. INTFLOAT *ptr, *dst, *ptr1;
  1434. INTFLOAT tmp[576];
  1435. if (g->block_type != 2)
  1436. return;
  1437. if (g->switch_point) {
  1438. if (s->sample_rate_index != 8) {
  1439. ptr = g->sb_hybrid + 36;
  1440. } else {
  1441. ptr = g->sb_hybrid + 48;
  1442. }
  1443. } else {
  1444. ptr = g->sb_hybrid;
  1445. }
  1446. for(i=g->short_start;i<13;i++) {
  1447. len = band_size_short[s->sample_rate_index][i];
  1448. ptr1 = ptr;
  1449. dst = tmp;
  1450. for(j=len;j>0;j--) {
  1451. *dst++ = ptr[0*len];
  1452. *dst++ = ptr[1*len];
  1453. *dst++ = ptr[2*len];
  1454. ptr++;
  1455. }
  1456. ptr+=2*len;
  1457. memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
  1458. }
  1459. }
  1460. #define ISQRT2 FIXR(0.70710678118654752440)
  1461. static void compute_stereo(MPADecodeContext *s,
  1462. GranuleDef *g0, GranuleDef *g1)
  1463. {
  1464. int i, j, k, l;
  1465. int sf_max, sf, len, non_zero_found;
  1466. INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
  1467. int non_zero_found_short[3];
  1468. /* intensity stereo */
  1469. if (s->mode_ext & MODE_EXT_I_STEREO) {
  1470. if (!s->lsf) {
  1471. is_tab = is_table;
  1472. sf_max = 7;
  1473. } else {
  1474. is_tab = is_table_lsf[g1->scalefac_compress & 1];
  1475. sf_max = 16;
  1476. }
  1477. tab0 = g0->sb_hybrid + 576;
  1478. tab1 = g1->sb_hybrid + 576;
  1479. non_zero_found_short[0] = 0;
  1480. non_zero_found_short[1] = 0;
  1481. non_zero_found_short[2] = 0;
  1482. k = (13 - g1->short_start) * 3 + g1->long_end - 3;
  1483. for(i = 12;i >= g1->short_start;i--) {
  1484. /* for last band, use previous scale factor */
  1485. if (i != 11)
  1486. k -= 3;
  1487. len = band_size_short[s->sample_rate_index][i];
  1488. for(l=2;l>=0;l--) {
  1489. tab0 -= len;
  1490. tab1 -= len;
  1491. if (!non_zero_found_short[l]) {
  1492. /* test if non zero band. if so, stop doing i-stereo */
  1493. for(j=0;j<len;j++) {
  1494. if (tab1[j] != 0) {
  1495. non_zero_found_short[l] = 1;
  1496. goto found1;
  1497. }
  1498. }
  1499. sf = g1->scale_factors[k + l];
  1500. if (sf >= sf_max)
  1501. goto found1;
  1502. v1 = is_tab[0][sf];
  1503. v2 = is_tab[1][sf];
  1504. for(j=0;j<len;j++) {
  1505. tmp0 = tab0[j];
  1506. tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
  1507. tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
  1508. }
  1509. } else {
  1510. found1:
  1511. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1512. /* lower part of the spectrum : do ms stereo
  1513. if enabled */
  1514. for(j=0;j<len;j++) {
  1515. tmp0 = tab0[j];
  1516. tmp1 = tab1[j];
  1517. tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  1518. tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  1519. }
  1520. }
  1521. }
  1522. }
  1523. }
  1524. non_zero_found = non_zero_found_short[0] |
  1525. non_zero_found_short[1] |
  1526. non_zero_found_short[2];
  1527. for(i = g1->long_end - 1;i >= 0;i--) {
  1528. len = band_size_long[s->sample_rate_index][i];
  1529. tab0 -= len;
  1530. tab1 -= len;
  1531. /* test if non zero band. if so, stop doing i-stereo */
  1532. if (!non_zero_found) {
  1533. for(j=0;j<len;j++) {
  1534. if (tab1[j] != 0) {
  1535. non_zero_found = 1;
  1536. goto found2;
  1537. }
  1538. }
  1539. /* for last band, use previous scale factor */
  1540. k = (i == 21) ? 20 : i;
  1541. sf = g1->scale_factors[k];
  1542. if (sf >= sf_max)
  1543. goto found2;
  1544. v1 = is_tab[0][sf];
  1545. v2 = is_tab[1][sf];
  1546. for(j=0;j<len;j++) {
  1547. tmp0 = tab0[j];
  1548. tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
  1549. tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
  1550. }
  1551. } else {
  1552. found2:
  1553. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1554. /* lower part of the spectrum : do ms stereo
  1555. if enabled */
  1556. for(j=0;j<len;j++) {
  1557. tmp0 = tab0[j];
  1558. tmp1 = tab1[j];
  1559. tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  1560. tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  1561. }
  1562. }
  1563. }
  1564. }
  1565. } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1566. /* ms stereo ONLY */
  1567. /* NOTE: the 1/sqrt(2) normalization factor is included in the
  1568. global gain */
  1569. tab0 = g0->sb_hybrid;
  1570. tab1 = g1->sb_hybrid;
  1571. for(i=0;i<576;i++) {
  1572. tmp0 = tab0[i];
  1573. tmp1 = tab1[i];
  1574. tab0[i] = tmp0 + tmp1;
  1575. tab1[i] = tmp0 - tmp1;
  1576. }
  1577. }
  1578. }
  1579. static void compute_antialias_integer(MPADecodeContext *s,
  1580. GranuleDef *g)
  1581. {
  1582. int32_t *ptr, *csa;
  1583. int n, i;
  1584. /* we antialias only "long" bands */
  1585. if (g->block_type == 2) {
  1586. if (!g->switch_point)
  1587. return;
  1588. /* XXX: check this for 8000Hz case */
  1589. n = 1;
  1590. } else {
  1591. n = SBLIMIT - 1;
  1592. }
  1593. ptr = g->sb_hybrid + 18;
  1594. for(i = n;i > 0;i--) {
  1595. int tmp0, tmp1, tmp2;
  1596. csa = &csa_table[0][0];
  1597. #define INT_AA(j) \
  1598. tmp0 = ptr[-1-j];\
  1599. tmp1 = ptr[ j];\
  1600. tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
  1601. ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
  1602. ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
  1603. INT_AA(0)
  1604. INT_AA(1)
  1605. INT_AA(2)
  1606. INT_AA(3)
  1607. INT_AA(4)
  1608. INT_AA(5)
  1609. INT_AA(6)
  1610. INT_AA(7)
  1611. ptr += 18;
  1612. }
  1613. }
  1614. static void compute_antialias_float(MPADecodeContext *s,
  1615. GranuleDef *g)
  1616. {
  1617. float *ptr;
  1618. int n, i;
  1619. /* we antialias only "long" bands */
  1620. if (g->block_type == 2) {
  1621. if (!g->switch_point)
  1622. return;
  1623. /* XXX: check this for 8000Hz case */
  1624. n = 1;
  1625. } else {
  1626. n = SBLIMIT - 1;
  1627. }
  1628. ptr = g->sb_hybrid + 18;
  1629. for(i = n;i > 0;i--) {
  1630. float tmp0, tmp1;
  1631. float *csa = &csa_table_float[0][0];
  1632. #define FLOAT_AA(j)\
  1633. tmp0= ptr[-1-j];\
  1634. tmp1= ptr[ j];\
  1635. ptr[-1-j] = tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j];\
  1636. ptr[ j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j];
  1637. FLOAT_AA(0)
  1638. FLOAT_AA(1)
  1639. FLOAT_AA(2)
  1640. FLOAT_AA(3)
  1641. FLOAT_AA(4)
  1642. FLOAT_AA(5)
  1643. FLOAT_AA(6)
  1644. FLOAT_AA(7)
  1645. ptr += 18;
  1646. }
  1647. }
  1648. static void compute_imdct(MPADecodeContext *s,
  1649. GranuleDef *g,
  1650. INTFLOAT *sb_samples,
  1651. INTFLOAT *mdct_buf)
  1652. {
  1653. INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
  1654. INTFLOAT out2[12];
  1655. int i, j, mdct_long_end, sblimit;
  1656. /* find last non zero block */
  1657. ptr = g->sb_hybrid + 576;
  1658. ptr1 = g->sb_hybrid + 2 * 18;
  1659. while (ptr >= ptr1) {
  1660. int32_t *p;
  1661. ptr -= 6;
  1662. p= (int32_t*)ptr;
  1663. if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
  1664. break;
  1665. }
  1666. sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
  1667. if (g->block_type == 2) {
  1668. /* XXX: check for 8000 Hz */
  1669. if (g->switch_point)
  1670. mdct_long_end = 2;
  1671. else
  1672. mdct_long_end = 0;
  1673. } else {
  1674. mdct_long_end = sblimit;
  1675. }
  1676. buf = mdct_buf;
  1677. ptr = g->sb_hybrid;
  1678. for(j=0;j<mdct_long_end;j++) {
  1679. /* apply window & overlap with previous buffer */
  1680. out_ptr = sb_samples + j;
  1681. /* select window */
  1682. if (g->switch_point && j < 2)
  1683. win1 = mdct_win[0];
  1684. else
  1685. win1 = mdct_win[g->block_type];
  1686. /* select frequency inversion */
  1687. win = win1 + ((4 * 36) & -(j & 1));
  1688. imdct36(out_ptr, buf, ptr, win);
  1689. out_ptr += 18*SBLIMIT;
  1690. ptr += 18;
  1691. buf += 18;
  1692. }
  1693. for(j=mdct_long_end;j<sblimit;j++) {
  1694. /* select frequency inversion */
  1695. win = mdct_win[2] + ((4 * 36) & -(j & 1));
  1696. out_ptr = sb_samples + j;
  1697. for(i=0; i<6; i++){
  1698. *out_ptr = buf[i];
  1699. out_ptr += SBLIMIT;
  1700. }
  1701. imdct12(out2, ptr + 0);
  1702. for(i=0;i<6;i++) {
  1703. *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
  1704. buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
  1705. out_ptr += SBLIMIT;
  1706. }
  1707. imdct12(out2, ptr + 1);
  1708. for(i=0;i<6;i++) {
  1709. *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
  1710. buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
  1711. out_ptr += SBLIMIT;
  1712. }
  1713. imdct12(out2, ptr + 2);
  1714. for(i=0;i<6;i++) {
  1715. buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
  1716. buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
  1717. buf[i + 6*2] = 0;
  1718. }
  1719. ptr += 18;
  1720. buf += 18;
  1721. }
  1722. /* zero bands */
  1723. for(j=sblimit;j<SBLIMIT;j++) {
  1724. /* overlap */
  1725. out_ptr = sb_samples + j;
  1726. for(i=0;i<18;i++) {
  1727. *out_ptr = buf[i];
  1728. buf[i] = 0;
  1729. out_ptr += SBLIMIT;
  1730. }
  1731. buf += 18;
  1732. }
  1733. }
  1734. /* main layer3 decoding function */
  1735. static int mp_decode_layer3(MPADecodeContext *s)
  1736. {
  1737. int nb_granules, main_data_begin, private_bits;
  1738. int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
  1739. GranuleDef *g;
  1740. int16_t exponents[576]; //FIXME try INTFLOAT
  1741. /* read side info */
  1742. if (s->lsf) {
  1743. main_data_begin = get_bits(&s->gb, 8);
  1744. private_bits = get_bits(&s->gb, s->nb_channels);
  1745. nb_granules = 1;
  1746. } else {
  1747. main_data_begin = get_bits(&s->gb, 9);
  1748. if (s->nb_channels == 2)
  1749. private_bits = get_bits(&s->gb, 3);
  1750. else
  1751. private_bits = get_bits(&s->gb, 5);
  1752. nb_granules = 2;
  1753. for(ch=0;ch<s->nb_channels;ch++) {
  1754. s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
  1755. s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
  1756. }
  1757. }
  1758. for(gr=0;gr<nb_granules;gr++) {
  1759. for(ch=0;ch<s->nb_channels;ch++) {
  1760. dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
  1761. g = &s->granules[ch][gr];
  1762. g->part2_3_length = get_bits(&s->gb, 12);
  1763. g->big_values = get_bits(&s->gb, 9);
  1764. if(g->big_values > 288){
  1765. av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
  1766. return -1;
  1767. }
  1768. g->global_gain = get_bits(&s->gb, 8);
  1769. /* if MS stereo only is selected, we precompute the
  1770. 1/sqrt(2) renormalization factor */
  1771. if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
  1772. MODE_EXT_MS_STEREO)
  1773. g->global_gain -= 2;
  1774. if (s->lsf)
  1775. g->scalefac_compress = get_bits(&s->gb, 9);
  1776. else
  1777. g->scalefac_compress = get_bits(&s->gb, 4);
  1778. blocksplit_flag = get_bits1(&s->gb);
  1779. if (blocksplit_flag) {
  1780. g->block_type = get_bits(&s->gb, 2);
  1781. if (g->block_type == 0){
  1782. av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
  1783. return -1;
  1784. }
  1785. g->switch_point = get_bits1(&s->gb);
  1786. for(i=0;i<2;i++)
  1787. g->table_select[i] = get_bits(&s->gb, 5);
  1788. for(i=0;i<3;i++)
  1789. g->subblock_gain[i] = get_bits(&s->gb, 3);
  1790. ff_init_short_region(s, g);
  1791. } else {
  1792. int region_address1, region_address2;
  1793. g->block_type = 0;
  1794. g->switch_point = 0;
  1795. for(i=0;i<3;i++)
  1796. g->table_select[i] = get_bits(&s->gb, 5);
  1797. /* compute huffman coded region sizes */
  1798. region_address1 = get_bits(&s->gb, 4);
  1799. region_address2 = get_bits(&s->gb, 3);
  1800. dprintf(s->avctx, "region1=%d region2=%d\n",
  1801. region_address1, region_address2);
  1802. ff_init_long_region(s, g, region_address1, region_address2);
  1803. }
  1804. ff_region_offset2size(g);
  1805. ff_compute_band_indexes(s, g);
  1806. g->preflag = 0;
  1807. if (!s->lsf)
  1808. g->preflag = get_bits1(&s->gb);
  1809. g->scalefac_scale = get_bits1(&s->gb);
  1810. g->count1table_select = get_bits1(&s->gb);
  1811. dprintf(s->avctx, "block_type=%d switch_point=%d\n",
  1812. g->block_type, g->switch_point);
  1813. }
  1814. }
  1815. if (!s->adu_mode) {
  1816. const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
  1817. assert((get_bits_count(&s->gb) & 7) == 0);
  1818. /* now we get bits from the main_data_begin offset */
  1819. dprintf(s->avctx, "seekback: %d\n", main_data_begin);
  1820. //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
  1821. memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
  1822. s->in_gb= s->gb;
  1823. init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
  1824. skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
  1825. }
  1826. for(gr=0;gr<nb_granules;gr++) {
  1827. for(ch=0;ch<s->nb_channels;ch++) {
  1828. g = &s->granules[ch][gr];
  1829. if(get_bits_count(&s->gb)<0){
  1830. av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
  1831. main_data_begin, s->last_buf_size, gr);
  1832. skip_bits_long(&s->gb, g->part2_3_length);
  1833. memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
  1834. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
  1835. skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
  1836. s->gb= s->in_gb;
  1837. s->in_gb.buffer=NULL;
  1838. }
  1839. continue;
  1840. }
  1841. bits_pos = get_bits_count(&s->gb);
  1842. if (!s->lsf) {
  1843. uint8_t *sc;
  1844. int slen, slen1, slen2;
  1845. /* MPEG1 scale factors */
  1846. slen1 = slen_table[0][g->scalefac_compress];
  1847. slen2 = slen_table[1][g->scalefac_compress];
  1848. dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
  1849. if (g->block_type == 2) {
  1850. n = g->switch_point ? 17 : 18;
  1851. j = 0;
  1852. if(slen1){
  1853. for(i=0;i<n;i++)
  1854. g->scale_factors[j++] = get_bits(&s->gb, slen1);
  1855. }else{
  1856. for(i=0;i<n;i++)
  1857. g->scale_factors[j++] = 0;
  1858. }
  1859. if(slen2){
  1860. for(i=0;i<18;i++)
  1861. g->scale_factors[j++] = get_bits(&s->gb, slen2);
  1862. for(i=0;i<3;i++)
  1863. g->scale_factors[j++] = 0;
  1864. }else{
  1865. for(i=0;i<21;i++)
  1866. g->scale_factors[j++] = 0;
  1867. }
  1868. } else {
  1869. sc = s->granules[ch][0].scale_factors;
  1870. j = 0;
  1871. for(k=0;k<4;k++) {
  1872. n = (k == 0 ? 6 : 5);
  1873. if ((g->scfsi & (0x8 >> k)) == 0) {
  1874. slen = (k < 2) ? slen1 : slen2;
  1875. if(slen){
  1876. for(i=0;i<n;i++)
  1877. g->scale_factors[j++] = get_bits(&s->gb, slen);
  1878. }else{
  1879. for(i=0;i<n;i++)
  1880. g->scale_factors[j++] = 0;
  1881. }
  1882. } else {
  1883. /* simply copy from last granule */
  1884. for(i=0;i<n;i++) {
  1885. g->scale_factors[j] = sc[j];
  1886. j++;
  1887. }
  1888. }
  1889. }
  1890. g->scale_factors[j++] = 0;
  1891. }
  1892. } else {
  1893. int tindex, tindex2, slen[4], sl, sf;
  1894. /* LSF scale factors */
  1895. if (g->block_type == 2) {
  1896. tindex = g->switch_point ? 2 : 1;
  1897. } else {
  1898. tindex = 0;
  1899. }
  1900. sf = g->scalefac_compress;
  1901. if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
  1902. /* intensity stereo case */
  1903. sf >>= 1;
  1904. if (sf < 180) {
  1905. lsf_sf_expand(slen, sf, 6, 6, 0);
  1906. tindex2 = 3;
  1907. } else if (sf < 244) {
  1908. lsf_sf_expand(slen, sf - 180, 4, 4, 0);
  1909. tindex2 = 4;
  1910. } else {
  1911. lsf_sf_expand(slen, sf - 244, 3, 0, 0);
  1912. tindex2 = 5;
  1913. }
  1914. } else {
  1915. /* normal case */
  1916. if (sf < 400) {
  1917. lsf_sf_expand(slen, sf, 5, 4, 4);
  1918. tindex2 = 0;
  1919. } else if (sf < 500) {
  1920. lsf_sf_expand(slen, sf - 400, 5, 4, 0);
  1921. tindex2 = 1;
  1922. } else {
  1923. lsf_sf_expand(slen, sf - 500, 3, 0, 0);
  1924. tindex2 = 2;
  1925. g->preflag = 1;
  1926. }
  1927. }
  1928. j = 0;
  1929. for(k=0;k<4;k++) {
  1930. n = lsf_nsf_table[tindex2][tindex][k];
  1931. sl = slen[k];
  1932. if(sl){
  1933. for(i=0;i<n;i++)
  1934. g->scale_factors[j++] = get_bits(&s->gb, sl);
  1935. }else{
  1936. for(i=0;i<n;i++)
  1937. g->scale_factors[j++] = 0;
  1938. }
  1939. }
  1940. /* XXX: should compute exact size */
  1941. for(;j<40;j++)
  1942. g->scale_factors[j] = 0;
  1943. }
  1944. exponents_from_scale_factors(s, g, exponents);
  1945. /* read Huffman coded residue */
  1946. huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
  1947. } /* ch */
  1948. if (s->nb_channels == 2)
  1949. compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
  1950. for(ch=0;ch<s->nb_channels;ch++) {
  1951. g = &s->granules[ch][gr];
  1952. reorder_block(s, g);
  1953. compute_antialias(s, g);
  1954. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  1955. }
  1956. } /* gr */
  1957. if(get_bits_count(&s->gb)<0)
  1958. skip_bits_long(&s->gb, -get_bits_count(&s->gb));
  1959. return nb_granules * 18;
  1960. }
  1961. static int mp_decode_frame(MPADecodeContext *s,
  1962. OUT_INT *samples, const uint8_t *buf, int buf_size)
  1963. {
  1964. int i, nb_frames, ch;
  1965. OUT_INT *samples_ptr;
  1966. init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
  1967. /* skip error protection field */
  1968. if (s->error_protection)
  1969. skip_bits(&s->gb, 16);
  1970. dprintf(s->avctx, "frame %d:\n", s->frame_count);
  1971. switch(s->layer) {
  1972. case 1:
  1973. s->avctx->frame_size = 384;
  1974. nb_frames = mp_decode_layer1(s);
  1975. break;
  1976. case 2:
  1977. s->avctx->frame_size = 1152;
  1978. nb_frames = mp_decode_layer2(s);
  1979. break;
  1980. case 3:
  1981. s->avctx->frame_size = s->lsf ? 576 : 1152;
  1982. default:
  1983. nb_frames = mp_decode_layer3(s);
  1984. s->last_buf_size=0;
  1985. if(s->in_gb.buffer){
  1986. align_get_bits(&s->gb);
  1987. i= get_bits_left(&s->gb)>>3;
  1988. if(i >= 0 && i <= BACKSTEP_SIZE){
  1989. memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
  1990. s->last_buf_size=i;
  1991. }else
  1992. av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
  1993. s->gb= s->in_gb;
  1994. s->in_gb.buffer= NULL;
  1995. }
  1996. align_get_bits(&s->gb);
  1997. assert((get_bits_count(&s->gb) & 7) == 0);
  1998. i= get_bits_left(&s->gb)>>3;
  1999. if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
  2000. if(i<0)
  2001. av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
  2002. i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
  2003. }
  2004. assert(i <= buf_size - HEADER_SIZE && i>= 0);
  2005. memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
  2006. s->last_buf_size += i;
  2007. break;
  2008. }
  2009. /* apply the synthesis filter */
  2010. for(ch=0;ch<s->nb_channels;ch++) {
  2011. samples_ptr = samples + ch;
  2012. for(i=0;i<nb_frames;i++) {
  2013. RENAME(ff_mpa_synth_filter)(
  2014. #if CONFIG_FLOAT
  2015. s,
  2016. #endif
  2017. s->synth_buf[ch], &(s->synth_buf_offset[ch]),
  2018. RENAME(ff_mpa_synth_window), &s->dither_state,
  2019. samples_ptr, s->nb_channels,
  2020. s->sb_samples[ch][i]);
  2021. samples_ptr += 32 * s->nb_channels;
  2022. }
  2023. }
  2024. return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
  2025. }
  2026. static int decode_frame(AVCodecContext * avctx,
  2027. void *data, int *data_size,
  2028. AVPacket *avpkt)
  2029. {
  2030. const uint8_t *buf = avpkt->data;
  2031. int buf_size = avpkt->size;
  2032. MPADecodeContext *s = avctx->priv_data;
  2033. uint32_t header;
  2034. int out_size;
  2035. OUT_INT *out_samples = data;
  2036. if(buf_size < HEADER_SIZE)
  2037. return -1;
  2038. header = AV_RB32(buf);
  2039. if(ff_mpa_check_header(header) < 0){
  2040. av_log(avctx, AV_LOG_ERROR, "Header missing\n");
  2041. return -1;
  2042. }
  2043. if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
  2044. /* free format: prepare to compute frame size */
  2045. s->frame_size = -1;
  2046. return -1;
  2047. }
  2048. /* update codec info */
  2049. avctx->channels = s->nb_channels;
  2050. avctx->bit_rate = s->bit_rate;
  2051. avctx->sub_id = s->layer;
  2052. if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
  2053. return -1;
  2054. *data_size = 0;
  2055. if(s->frame_size<=0 || s->frame_size > buf_size){
  2056. av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
  2057. return -1;
  2058. }else if(s->frame_size < buf_size){
  2059. av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
  2060. buf_size= s->frame_size;
  2061. }
  2062. out_size = mp_decode_frame(s, out_samples, buf, buf_size);
  2063. if(out_size>=0){
  2064. *data_size = out_size;
  2065. avctx->sample_rate = s->sample_rate;
  2066. //FIXME maybe move the other codec info stuff from above here too
  2067. }else
  2068. av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
  2069. s->frame_size = 0;
  2070. return buf_size;
  2071. }
  2072. static void flush(AVCodecContext *avctx){
  2073. MPADecodeContext *s = avctx->priv_data;
  2074. memset(s->synth_buf, 0, sizeof(s->synth_buf));
  2075. s->last_buf_size= 0;
  2076. }
  2077. #if CONFIG_MP3ADU_DECODER
  2078. static int decode_frame_adu(AVCodecContext * avctx,
  2079. void *data, int *data_size,
  2080. AVPacket *avpkt)
  2081. {
  2082. const uint8_t *buf = avpkt->data;
  2083. int buf_size = avpkt->size;
  2084. MPADecodeContext *s = avctx->priv_data;
  2085. uint32_t header;
  2086. int len, out_size;
  2087. OUT_INT *out_samples = data;
  2088. len = buf_size;
  2089. // Discard too short frames
  2090. if (buf_size < HEADER_SIZE) {
  2091. *data_size = 0;
  2092. return buf_size;
  2093. }
  2094. if (len > MPA_MAX_CODED_FRAME_SIZE)
  2095. len = MPA_MAX_CODED_FRAME_SIZE;
  2096. // Get header and restore sync word
  2097. header = AV_RB32(buf) | 0xffe00000;
  2098. if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
  2099. *data_size = 0;
  2100. return buf_size;
  2101. }
  2102. ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
  2103. /* update codec info */
  2104. avctx->sample_rate = s->sample_rate;
  2105. avctx->channels = s->nb_channels;
  2106. avctx->bit_rate = s->bit_rate;
  2107. avctx->sub_id = s->layer;
  2108. s->frame_size = len;
  2109. if (avctx->parse_only) {
  2110. out_size = buf_size;
  2111. } else {
  2112. out_size = mp_decode_frame(s, out_samples, buf, buf_size);
  2113. }
  2114. *data_size = out_size;
  2115. return buf_size;
  2116. }
  2117. #endif /* CONFIG_MP3ADU_DECODER */
  2118. #if CONFIG_MP3ON4_DECODER
  2119. /**
  2120. * Context for MP3On4 decoder
  2121. */
  2122. typedef struct MP3On4DecodeContext {
  2123. int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
  2124. int syncword; ///< syncword patch
  2125. const uint8_t *coff; ///< channels offsets in output buffer
  2126. MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
  2127. } MP3On4DecodeContext;
  2128. #include "mpeg4audio.h"
  2129. /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
  2130. static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
  2131. /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
  2132. static const uint8_t chan_offset[8][5] = {
  2133. {0},
  2134. {0}, // C
  2135. {0}, // FLR
  2136. {2,0}, // C FLR
  2137. {2,0,3}, // C FLR BS
  2138. {4,0,2}, // C FLR BLRS
  2139. {4,0,2,5}, // C FLR BLRS LFE
  2140. {4,0,2,6,5}, // C FLR BLRS BLR LFE
  2141. };
  2142. static int decode_init_mp3on4(AVCodecContext * avctx)
  2143. {
  2144. MP3On4DecodeContext *s = avctx->priv_data;
  2145. MPEG4AudioConfig cfg;
  2146. int i;
  2147. if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
  2148. av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
  2149. return -1;
  2150. }
  2151. ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
  2152. if (!cfg.chan_config || cfg.chan_config > 7) {
  2153. av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
  2154. return -1;
  2155. }
  2156. s->frames = mp3Frames[cfg.chan_config];
  2157. s->coff = chan_offset[cfg.chan_config];
  2158. avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
  2159. if (cfg.sample_rate < 16000)
  2160. s->syncword = 0xffe00000;
  2161. else
  2162. s->syncword = 0xfff00000;
  2163. /* Init the first mp3 decoder in standard way, so that all tables get builded
  2164. * We replace avctx->priv_data with the context of the first decoder so that
  2165. * decode_init() does not have to be changed.
  2166. * Other decoders will be initialized here copying data from the first context
  2167. */
  2168. // Allocate zeroed memory for the first decoder context
  2169. s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
  2170. // Put decoder context in place to make init_decode() happy
  2171. avctx->priv_data = s->mp3decctx[0];
  2172. decode_init(avctx);
  2173. // Restore mp3on4 context pointer
  2174. avctx->priv_data = s;
  2175. s->mp3decctx[0]->adu_mode = 1; // Set adu mode
  2176. /* Create a separate codec/context for each frame (first is already ok).
  2177. * Each frame is 1 or 2 channels - up to 5 frames allowed
  2178. */
  2179. for (i = 1; i < s->frames; i++) {
  2180. s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
  2181. s->mp3decctx[i]->adu_mode = 1;
  2182. s->mp3decctx[i]->avctx = avctx;
  2183. }
  2184. return 0;
  2185. }
  2186. static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
  2187. {
  2188. MP3On4DecodeContext *s = avctx->priv_data;
  2189. int i;
  2190. for (i = 0; i < s->frames; i++)
  2191. if (s->mp3decctx[i])
  2192. av_free(s->mp3decctx[i]);
  2193. return 0;
  2194. }
  2195. static int decode_frame_mp3on4(AVCodecContext * avctx,
  2196. void *data, int *data_size,
  2197. AVPacket *avpkt)
  2198. {
  2199. const uint8_t *buf = avpkt->data;
  2200. int buf_size = avpkt->size;
  2201. MP3On4DecodeContext *s = avctx->priv_data;
  2202. MPADecodeContext *m;
  2203. int fsize, len = buf_size, out_size = 0;
  2204. uint32_t header;
  2205. OUT_INT *out_samples = data;
  2206. OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
  2207. OUT_INT *outptr, *bp;
  2208. int fr, j, n;
  2209. if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
  2210. return -1;
  2211. *data_size = 0;
  2212. // Discard too short frames
  2213. if (buf_size < HEADER_SIZE)
  2214. return -1;
  2215. // If only one decoder interleave is not needed
  2216. outptr = s->frames == 1 ? out_samples : decoded_buf;
  2217. avctx->bit_rate = 0;
  2218. for (fr = 0; fr < s->frames; fr++) {
  2219. fsize = AV_RB16(buf) >> 4;
  2220. fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
  2221. m = s->mp3decctx[fr];
  2222. assert (m != NULL);
  2223. header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
  2224. if (ff_mpa_check_header(header) < 0) // Bad header, discard block
  2225. break;
  2226. ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
  2227. out_size += mp_decode_frame(m, outptr, buf, fsize);
  2228. buf += fsize;
  2229. len -= fsize;
  2230. if(s->frames > 1) {
  2231. n = m->avctx->frame_size*m->nb_channels;
  2232. /* interleave output data */
  2233. bp = out_samples + s->coff[fr];
  2234. if(m->nb_channels == 1) {
  2235. for(j = 0; j < n; j++) {
  2236. *bp = decoded_buf[j];
  2237. bp += avctx->channels;
  2238. }
  2239. } else {
  2240. for(j = 0; j < n; j++) {
  2241. bp[0] = decoded_buf[j++];
  2242. bp[1] = decoded_buf[j];
  2243. bp += avctx->channels;
  2244. }
  2245. }
  2246. }
  2247. avctx->bit_rate += m->bit_rate;
  2248. }
  2249. /* update codec info */
  2250. avctx->sample_rate = s->mp3decctx[0]->sample_rate;
  2251. *data_size = out_size;
  2252. return buf_size;
  2253. }
  2254. #endif /* CONFIG_MP3ON4_DECODER */
  2255. #if !CONFIG_FLOAT
  2256. #if CONFIG_MP1_DECODER
  2257. AVCodec mp1_decoder =
  2258. {
  2259. "mp1",
  2260. AVMEDIA_TYPE_AUDIO,
  2261. CODEC_ID_MP1,
  2262. sizeof(MPADecodeContext),
  2263. decode_init,
  2264. NULL,
  2265. NULL,
  2266. decode_frame,
  2267. CODEC_CAP_PARSE_ONLY,
  2268. .flush= flush,
  2269. .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
  2270. };
  2271. #endif
  2272. #if CONFIG_MP2_DECODER
  2273. AVCodec mp2_decoder =
  2274. {
  2275. "mp2",
  2276. AVMEDIA_TYPE_AUDIO,
  2277. CODEC_ID_MP2,
  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("MP2 (MPEG audio layer 2)"),
  2286. };
  2287. #endif
  2288. #if CONFIG_MP3_DECODER
  2289. AVCodec mp3_decoder =
  2290. {
  2291. "mp3",
  2292. AVMEDIA_TYPE_AUDIO,
  2293. CODEC_ID_MP3,
  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("MP3 (MPEG audio layer 3)"),
  2302. };
  2303. #endif
  2304. #if CONFIG_MP3ADU_DECODER
  2305. AVCodec mp3adu_decoder =
  2306. {
  2307. "mp3adu",
  2308. AVMEDIA_TYPE_AUDIO,
  2309. CODEC_ID_MP3ADU,
  2310. sizeof(MPADecodeContext),
  2311. decode_init,
  2312. NULL,
  2313. NULL,
  2314. decode_frame_adu,
  2315. CODEC_CAP_PARSE_ONLY,
  2316. .flush= flush,
  2317. .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
  2318. };
  2319. #endif
  2320. #if CONFIG_MP3ON4_DECODER
  2321. AVCodec mp3on4_decoder =
  2322. {
  2323. "mp3on4",
  2324. AVMEDIA_TYPE_AUDIO,
  2325. CODEC_ID_MP3ON4,
  2326. sizeof(MP3On4DecodeContext),
  2327. decode_init_mp3on4,
  2328. NULL,
  2329. decode_close_mp3on4,
  2330. decode_frame_mp3on4,
  2331. .flush= flush,
  2332. .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
  2333. };
  2334. #endif
  2335. #endif