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.

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