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.

2697 lines
80KB

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