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.

2053 lines
67KB

  1. /*
  2. * MPEG Audio decoder
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavutil/channel_layout.h"
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "internal.h"
  29. #include "mathops.h"
  30. #include "mpegaudiodsp.h"
  31. #include "dsputil.h"
  32. /*
  33. * TODO:
  34. * - test lsf / mpeg25 extensively.
  35. */
  36. #include "mpegaudio.h"
  37. #include "mpegaudiodecheader.h"
  38. #define BACKSTEP_SIZE 512
  39. #define EXTRABYTES 24
  40. #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
  41. /* layer 3 "granule" */
  42. typedef struct GranuleDef {
  43. uint8_t scfsi;
  44. int part2_3_length;
  45. int big_values;
  46. int global_gain;
  47. int scalefac_compress;
  48. uint8_t block_type;
  49. uint8_t switch_point;
  50. int table_select[3];
  51. int subblock_gain[3];
  52. uint8_t scalefac_scale;
  53. uint8_t count1table_select;
  54. int region_size[3]; /* number of huffman codes in each region */
  55. int preflag;
  56. int short_start, long_end; /* long/short band indexes */
  57. uint8_t scale_factors[40];
  58. DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
  59. } GranuleDef;
  60. typedef struct MPADecodeContext {
  61. MPA_DECODE_HEADER
  62. uint8_t last_buf[LAST_BUF_SIZE];
  63. int last_buf_size;
  64. /* next header (used in free format parsing) */
  65. uint32_t free_format_next_header;
  66. GetBitContext gb;
  67. GetBitContext in_gb;
  68. DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
  69. int synth_buf_offset[MPA_MAX_CHANNELS];
  70. DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
  71. INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
  72. GranuleDef granules[2][2]; /* Used in Layer 3 */
  73. int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
  74. int dither_state;
  75. int err_recognition;
  76. AVCodecContext* avctx;
  77. MPADSPContext mpadsp;
  78. DSPContext dsp;
  79. AVFrame frame;
  80. } MPADecodeContext;
  81. #if CONFIG_FLOAT
  82. # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
  83. # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
  84. # define FIXR(x) ((float)(x))
  85. # define FIXHR(x) ((float)(x))
  86. # define MULH3(x, y, s) ((s)*(y)*(x))
  87. # define MULLx(x, y, s) ((y)*(x))
  88. # define RENAME(a) a ## _float
  89. # define OUT_FMT AV_SAMPLE_FMT_FLT
  90. # define OUT_FMT_P AV_SAMPLE_FMT_FLTP
  91. #else
  92. # define SHR(a,b) ((a)>>(b))
  93. /* WARNING: only correct for positive numbers */
  94. # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
  95. # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
  96. # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
  97. # define MULH3(x, y, s) MULH((s)*(x), y)
  98. # define MULLx(x, y, s) MULL(x,y,s)
  99. # define RENAME(a) a ## _fixed
  100. # define OUT_FMT AV_SAMPLE_FMT_S16
  101. # define OUT_FMT_P AV_SAMPLE_FMT_S16P
  102. #endif
  103. /****************/
  104. #define HEADER_SIZE 4
  105. #include "mpegaudiodata.h"
  106. #include "mpegaudiodectab.h"
  107. /* vlc structure for decoding layer 3 huffman tables */
  108. static VLC huff_vlc[16];
  109. static VLC_TYPE huff_vlc_tables[
  110. 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
  111. 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
  112. ][2];
  113. static const int huff_vlc_tables_sizes[16] = {
  114. 0, 128, 128, 128, 130, 128, 154, 166,
  115. 142, 204, 190, 170, 542, 460, 662, 414
  116. };
  117. static VLC huff_quad_vlc[2];
  118. static VLC_TYPE huff_quad_vlc_tables[128+16][2];
  119. static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
  120. /* computed from band_size_long */
  121. static uint16_t band_index_long[9][23];
  122. #include "mpegaudio_tablegen.h"
  123. /* intensity stereo coef table */
  124. static INTFLOAT is_table[2][16];
  125. static INTFLOAT is_table_lsf[2][2][16];
  126. static INTFLOAT csa_table[8][4];
  127. static int16_t division_tab3[1<<6 ];
  128. static int16_t division_tab5[1<<8 ];
  129. static int16_t division_tab9[1<<11];
  130. static int16_t * const division_tabs[4] = {
  131. division_tab3, division_tab5, NULL, division_tab9
  132. };
  133. /* lower 2 bits: modulo 3, higher bits: shift */
  134. static uint16_t scale_factor_modshift[64];
  135. /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
  136. static int32_t scale_factor_mult[15][3];
  137. /* mult table for layer 2 group quantization */
  138. #define SCALE_GEN(v) \
  139. { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
  140. static const int32_t scale_factor_mult2[3][3] = {
  141. SCALE_GEN(4.0 / 3.0), /* 3 steps */
  142. SCALE_GEN(4.0 / 5.0), /* 5 steps */
  143. SCALE_GEN(4.0 / 9.0), /* 9 steps */
  144. };
  145. /**
  146. * Convert region offsets to region sizes and truncate
  147. * size to big_values.
  148. */
  149. static void ff_region_offset2size(GranuleDef *g)
  150. {
  151. int i, k, j = 0;
  152. g->region_size[2] = 576 / 2;
  153. for (i = 0; i < 3; i++) {
  154. k = FFMIN(g->region_size[i], g->big_values);
  155. g->region_size[i] = k - j;
  156. j = k;
  157. }
  158. }
  159. static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
  160. {
  161. if (g->block_type == 2) {
  162. if (s->sample_rate_index != 8)
  163. g->region_size[0] = (36 / 2);
  164. else
  165. g->region_size[0] = (72 / 2);
  166. } else {
  167. if (s->sample_rate_index <= 2)
  168. g->region_size[0] = (36 / 2);
  169. else if (s->sample_rate_index != 8)
  170. g->region_size[0] = (54 / 2);
  171. else
  172. g->region_size[0] = (108 / 2);
  173. }
  174. g->region_size[1] = (576 / 2);
  175. }
  176. static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
  177. {
  178. int l;
  179. g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
  180. /* should not overflow */
  181. l = FFMIN(ra1 + ra2 + 2, 22);
  182. g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
  183. }
  184. static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
  185. {
  186. if (g->block_type == 2) {
  187. if (g->switch_point) {
  188. /* if switched mode, we handle the 36 first samples as
  189. long blocks. For 8000Hz, we handle the 72 first
  190. exponents as long blocks */
  191. if (s->sample_rate_index <= 2)
  192. g->long_end = 8;
  193. else
  194. g->long_end = 6;
  195. g->short_start = 3;
  196. } else {
  197. g->long_end = 0;
  198. g->short_start = 0;
  199. }
  200. } else {
  201. g->short_start = 13;
  202. g->long_end = 22;
  203. }
  204. }
  205. /* layer 1 unscaling */
  206. /* n = number of bits of the mantissa minus 1 */
  207. static inline int l1_unscale(int n, int mant, int scale_factor)
  208. {
  209. int shift, mod;
  210. int64_t val;
  211. shift = scale_factor_modshift[scale_factor];
  212. mod = shift & 3;
  213. shift >>= 2;
  214. val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
  215. shift += n;
  216. /* NOTE: at this point, 1 <= shift >= 21 + 15 */
  217. return (int)((val + (1LL << (shift - 1))) >> shift);
  218. }
  219. static inline int l2_unscale_group(int steps, int mant, int scale_factor)
  220. {
  221. int shift, mod, val;
  222. shift = scale_factor_modshift[scale_factor];
  223. mod = shift & 3;
  224. shift >>= 2;
  225. val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
  226. /* NOTE: at this point, 0 <= shift <= 21 */
  227. if (shift > 0)
  228. val = (val + (1 << (shift - 1))) >> shift;
  229. return val;
  230. }
  231. /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
  232. static inline int l3_unscale(int value, int exponent)
  233. {
  234. unsigned int m;
  235. int e;
  236. e = table_4_3_exp [4 * value + (exponent & 3)];
  237. m = table_4_3_value[4 * value + (exponent & 3)];
  238. e -= exponent >> 2;
  239. assert(e >= 1);
  240. if (e > 31)
  241. return 0;
  242. m = (m + (1 << (e - 1))) >> e;
  243. return m;
  244. }
  245. static av_cold void decode_init_static(void)
  246. {
  247. int i, j, k;
  248. int offset;
  249. /* scale factors table for layer 1/2 */
  250. for (i = 0; i < 64; i++) {
  251. int shift, mod;
  252. /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
  253. shift = i / 3;
  254. mod = i % 3;
  255. scale_factor_modshift[i] = mod | (shift << 2);
  256. }
  257. /* scale factor multiply for layer 1 */
  258. for (i = 0; i < 15; i++) {
  259. int n, norm;
  260. n = i + 2;
  261. norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
  262. scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
  263. scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
  264. scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
  265. av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
  266. scale_factor_mult[i][0],
  267. scale_factor_mult[i][1],
  268. scale_factor_mult[i][2]);
  269. }
  270. RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
  271. /* huffman decode tables */
  272. offset = 0;
  273. for (i = 1; i < 16; i++) {
  274. const HuffTable *h = &mpa_huff_tables[i];
  275. int xsize, x, y;
  276. uint8_t tmp_bits [512] = { 0 };
  277. uint16_t tmp_codes[512] = { 0 };
  278. xsize = h->xsize;
  279. j = 0;
  280. for (x = 0; x < xsize; x++) {
  281. for (y = 0; y < xsize; y++) {
  282. tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
  283. tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
  284. }
  285. }
  286. /* XXX: fail test */
  287. huff_vlc[i].table = huff_vlc_tables+offset;
  288. huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
  289. init_vlc(&huff_vlc[i], 7, 512,
  290. tmp_bits, 1, 1, tmp_codes, 2, 2,
  291. INIT_VLC_USE_NEW_STATIC);
  292. offset += huff_vlc_tables_sizes[i];
  293. }
  294. assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
  295. offset = 0;
  296. for (i = 0; i < 2; i++) {
  297. huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
  298. huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
  299. init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
  300. mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
  301. INIT_VLC_USE_NEW_STATIC);
  302. offset += huff_quad_vlc_tables_sizes[i];
  303. }
  304. assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
  305. for (i = 0; i < 9; i++) {
  306. k = 0;
  307. for (j = 0; j < 22; j++) {
  308. band_index_long[i][j] = k;
  309. k += band_size_long[i][j];
  310. }
  311. band_index_long[i][22] = k;
  312. }
  313. /* compute n ^ (4/3) and store it in mantissa/exp format */
  314. mpegaudio_tableinit();
  315. for (i = 0; i < 4; i++) {
  316. if (ff_mpa_quant_bits[i] < 0) {
  317. for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
  318. int val1, val2, val3, steps;
  319. int val = j;
  320. steps = ff_mpa_quant_steps[i];
  321. val1 = val % steps;
  322. val /= steps;
  323. val2 = val % steps;
  324. val3 = val / steps;
  325. division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
  326. }
  327. }
  328. }
  329. for (i = 0; i < 7; i++) {
  330. float f;
  331. INTFLOAT v;
  332. if (i != 6) {
  333. f = tan((double)i * M_PI / 12.0);
  334. v = FIXR(f / (1.0 + f));
  335. } else {
  336. v = FIXR(1.0);
  337. }
  338. is_table[0][ i] = v;
  339. is_table[1][6 - i] = v;
  340. }
  341. /* invalid values */
  342. for (i = 7; i < 16; i++)
  343. is_table[0][i] = is_table[1][i] = 0.0;
  344. for (i = 0; i < 16; i++) {
  345. double f;
  346. int e, k;
  347. for (j = 0; j < 2; j++) {
  348. e = -(j + 1) * ((i + 1) >> 1);
  349. f = pow(2.0, e / 4.0);
  350. k = i & 1;
  351. is_table_lsf[j][k ^ 1][i] = FIXR(f);
  352. is_table_lsf[j][k ][i] = FIXR(1.0);
  353. av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
  354. i, j, (float) is_table_lsf[j][0][i],
  355. (float) is_table_lsf[j][1][i]);
  356. }
  357. }
  358. for (i = 0; i < 8; i++) {
  359. float ci, cs, ca;
  360. ci = ci_table[i];
  361. cs = 1.0 / sqrt(1.0 + ci * ci);
  362. ca = cs * ci;
  363. #if !CONFIG_FLOAT
  364. csa_table[i][0] = FIXHR(cs/4);
  365. csa_table[i][1] = FIXHR(ca/4);
  366. csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
  367. csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
  368. #else
  369. csa_table[i][0] = cs;
  370. csa_table[i][1] = ca;
  371. csa_table[i][2] = ca + cs;
  372. csa_table[i][3] = ca - cs;
  373. #endif
  374. }
  375. }
  376. static av_cold int decode_init(AVCodecContext * avctx)
  377. {
  378. static int initialized_tables = 0;
  379. MPADecodeContext *s = avctx->priv_data;
  380. if (!initialized_tables) {
  381. decode_init_static();
  382. initialized_tables = 1;
  383. }
  384. s->avctx = avctx;
  385. ff_mpadsp_init(&s->mpadsp);
  386. ff_dsputil_init(&s->dsp, avctx);
  387. if (avctx->request_sample_fmt == OUT_FMT &&
  388. avctx->codec_id != AV_CODEC_ID_MP3ON4)
  389. avctx->sample_fmt = OUT_FMT;
  390. else
  391. avctx->sample_fmt = OUT_FMT_P;
  392. s->err_recognition = avctx->err_recognition;
  393. if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
  394. s->adu_mode = 1;
  395. avcodec_get_frame_defaults(&s->frame);
  396. avctx->coded_frame = &s->frame;
  397. return 0;
  398. }
  399. #define C3 FIXHR(0.86602540378443864676/2)
  400. #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
  401. #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
  402. #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
  403. /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
  404. cases. */
  405. static void imdct12(INTFLOAT *out, INTFLOAT *in)
  406. {
  407. INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
  408. in0 = in[0*3];
  409. in1 = in[1*3] + in[0*3];
  410. in2 = in[2*3] + in[1*3];
  411. in3 = in[3*3] + in[2*3];
  412. in4 = in[4*3] + in[3*3];
  413. in5 = in[5*3] + in[4*3];
  414. in5 += in3;
  415. in3 += in1;
  416. in2 = MULH3(in2, C3, 2);
  417. in3 = MULH3(in3, C3, 4);
  418. t1 = in0 - in4;
  419. t2 = MULH3(in1 - in5, C4, 2);
  420. out[ 7] =
  421. out[10] = t1 + t2;
  422. out[ 1] =
  423. out[ 4] = t1 - t2;
  424. in0 += SHR(in4, 1);
  425. in4 = in0 + in2;
  426. in5 += 2*in1;
  427. in1 = MULH3(in5 + in3, C5, 1);
  428. out[ 8] =
  429. out[ 9] = in4 + in1;
  430. out[ 2] =
  431. out[ 3] = in4 - in1;
  432. in0 -= in2;
  433. in5 = MULH3(in5 - in3, C6, 2);
  434. out[ 0] =
  435. out[ 5] = in0 - in5;
  436. out[ 6] =
  437. out[11] = in0 + in5;
  438. }
  439. /* return the number of decoded frames */
  440. static int mp_decode_layer1(MPADecodeContext *s)
  441. {
  442. int bound, i, v, n, ch, j, mant;
  443. uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
  444. uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
  445. if (s->mode == MPA_JSTEREO)
  446. bound = (s->mode_ext + 1) * 4;
  447. else
  448. bound = SBLIMIT;
  449. /* allocation bits */
  450. for (i = 0; i < bound; i++) {
  451. for (ch = 0; ch < s->nb_channels; ch++) {
  452. allocation[ch][i] = get_bits(&s->gb, 4);
  453. }
  454. }
  455. for (i = bound; i < SBLIMIT; i++)
  456. allocation[0][i] = get_bits(&s->gb, 4);
  457. /* scale factors */
  458. for (i = 0; i < bound; i++) {
  459. for (ch = 0; ch < s->nb_channels; ch++) {
  460. if (allocation[ch][i])
  461. scale_factors[ch][i] = get_bits(&s->gb, 6);
  462. }
  463. }
  464. for (i = bound; i < SBLIMIT; i++) {
  465. if (allocation[0][i]) {
  466. scale_factors[0][i] = get_bits(&s->gb, 6);
  467. scale_factors[1][i] = get_bits(&s->gb, 6);
  468. }
  469. }
  470. /* compute samples */
  471. for (j = 0; j < 12; j++) {
  472. for (i = 0; i < bound; i++) {
  473. for (ch = 0; ch < s->nb_channels; ch++) {
  474. n = allocation[ch][i];
  475. if (n) {
  476. mant = get_bits(&s->gb, n + 1);
  477. v = l1_unscale(n, mant, scale_factors[ch][i]);
  478. } else {
  479. v = 0;
  480. }
  481. s->sb_samples[ch][j][i] = v;
  482. }
  483. }
  484. for (i = bound; i < SBLIMIT; i++) {
  485. n = allocation[0][i];
  486. if (n) {
  487. mant = get_bits(&s->gb, n + 1);
  488. v = l1_unscale(n, mant, scale_factors[0][i]);
  489. s->sb_samples[0][j][i] = v;
  490. v = l1_unscale(n, mant, scale_factors[1][i]);
  491. s->sb_samples[1][j][i] = v;
  492. } else {
  493. s->sb_samples[0][j][i] = 0;
  494. s->sb_samples[1][j][i] = 0;
  495. }
  496. }
  497. }
  498. return 12;
  499. }
  500. static int mp_decode_layer2(MPADecodeContext *s)
  501. {
  502. int sblimit; /* number of used subbands */
  503. const unsigned char *alloc_table;
  504. int table, bit_alloc_bits, i, j, ch, bound, v;
  505. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
  506. unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
  507. unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
  508. int scale, qindex, bits, steps, k, l, m, b;
  509. /* select decoding table */
  510. table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
  511. s->sample_rate, s->lsf);
  512. sblimit = ff_mpa_sblimit_table[table];
  513. alloc_table = ff_mpa_alloc_tables[table];
  514. if (s->mode == MPA_JSTEREO)
  515. bound = (s->mode_ext + 1) * 4;
  516. else
  517. bound = sblimit;
  518. av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
  519. /* sanity check */
  520. if (bound > sblimit)
  521. bound = sblimit;
  522. /* parse bit allocation */
  523. j = 0;
  524. for (i = 0; i < bound; i++) {
  525. bit_alloc_bits = alloc_table[j];
  526. for (ch = 0; ch < s->nb_channels; ch++)
  527. bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
  528. j += 1 << bit_alloc_bits;
  529. }
  530. for (i = bound; i < sblimit; i++) {
  531. bit_alloc_bits = alloc_table[j];
  532. v = get_bits(&s->gb, bit_alloc_bits);
  533. bit_alloc[0][i] = v;
  534. bit_alloc[1][i] = v;
  535. j += 1 << bit_alloc_bits;
  536. }
  537. /* scale codes */
  538. for (i = 0; i < sblimit; i++) {
  539. for (ch = 0; ch < s->nb_channels; ch++) {
  540. if (bit_alloc[ch][i])
  541. scale_code[ch][i] = get_bits(&s->gb, 2);
  542. }
  543. }
  544. /* scale factors */
  545. for (i = 0; i < sblimit; i++) {
  546. for (ch = 0; ch < s->nb_channels; ch++) {
  547. if (bit_alloc[ch][i]) {
  548. sf = scale_factors[ch][i];
  549. switch (scale_code[ch][i]) {
  550. default:
  551. case 0:
  552. sf[0] = get_bits(&s->gb, 6);
  553. sf[1] = get_bits(&s->gb, 6);
  554. sf[2] = get_bits(&s->gb, 6);
  555. break;
  556. case 2:
  557. sf[0] = get_bits(&s->gb, 6);
  558. sf[1] = sf[0];
  559. sf[2] = sf[0];
  560. break;
  561. case 1:
  562. sf[0] = get_bits(&s->gb, 6);
  563. sf[2] = get_bits(&s->gb, 6);
  564. sf[1] = sf[0];
  565. break;
  566. case 3:
  567. sf[0] = get_bits(&s->gb, 6);
  568. sf[2] = get_bits(&s->gb, 6);
  569. sf[1] = sf[2];
  570. break;
  571. }
  572. }
  573. }
  574. }
  575. /* samples */
  576. for (k = 0; k < 3; k++) {
  577. for (l = 0; l < 12; l += 3) {
  578. j = 0;
  579. for (i = 0; i < bound; i++) {
  580. bit_alloc_bits = alloc_table[j];
  581. for (ch = 0; ch < s->nb_channels; ch++) {
  582. b = bit_alloc[ch][i];
  583. if (b) {
  584. scale = scale_factors[ch][i][k];
  585. qindex = alloc_table[j+b];
  586. bits = ff_mpa_quant_bits[qindex];
  587. if (bits < 0) {
  588. int v2;
  589. /* 3 values at the same time */
  590. v = get_bits(&s->gb, -bits);
  591. v2 = division_tabs[qindex][v];
  592. steps = ff_mpa_quant_steps[qindex];
  593. s->sb_samples[ch][k * 12 + l + 0][i] =
  594. l2_unscale_group(steps, v2 & 15, scale);
  595. s->sb_samples[ch][k * 12 + l + 1][i] =
  596. l2_unscale_group(steps, (v2 >> 4) & 15, scale);
  597. s->sb_samples[ch][k * 12 + l + 2][i] =
  598. l2_unscale_group(steps, v2 >> 8 , scale);
  599. } else {
  600. for (m = 0; m < 3; m++) {
  601. v = get_bits(&s->gb, bits);
  602. v = l1_unscale(bits - 1, v, scale);
  603. s->sb_samples[ch][k * 12 + l + m][i] = v;
  604. }
  605. }
  606. } else {
  607. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  608. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  609. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  610. }
  611. }
  612. /* next subband in alloc table */
  613. j += 1 << bit_alloc_bits;
  614. }
  615. /* XXX: find a way to avoid this duplication of code */
  616. for (i = bound; i < sblimit; i++) {
  617. bit_alloc_bits = alloc_table[j];
  618. b = bit_alloc[0][i];
  619. if (b) {
  620. int mant, scale0, scale1;
  621. scale0 = scale_factors[0][i][k];
  622. scale1 = scale_factors[1][i][k];
  623. qindex = alloc_table[j+b];
  624. bits = ff_mpa_quant_bits[qindex];
  625. if (bits < 0) {
  626. /* 3 values at the same time */
  627. v = get_bits(&s->gb, -bits);
  628. steps = ff_mpa_quant_steps[qindex];
  629. mant = v % steps;
  630. v = v / steps;
  631. s->sb_samples[0][k * 12 + l + 0][i] =
  632. l2_unscale_group(steps, mant, scale0);
  633. s->sb_samples[1][k * 12 + l + 0][i] =
  634. l2_unscale_group(steps, mant, scale1);
  635. mant = v % steps;
  636. v = v / steps;
  637. s->sb_samples[0][k * 12 + l + 1][i] =
  638. l2_unscale_group(steps, mant, scale0);
  639. s->sb_samples[1][k * 12 + l + 1][i] =
  640. l2_unscale_group(steps, mant, scale1);
  641. s->sb_samples[0][k * 12 + l + 2][i] =
  642. l2_unscale_group(steps, v, scale0);
  643. s->sb_samples[1][k * 12 + l + 2][i] =
  644. l2_unscale_group(steps, v, scale1);
  645. } else {
  646. for (m = 0; m < 3; m++) {
  647. mant = get_bits(&s->gb, bits);
  648. s->sb_samples[0][k * 12 + l + m][i] =
  649. l1_unscale(bits - 1, mant, scale0);
  650. s->sb_samples[1][k * 12 + l + m][i] =
  651. l1_unscale(bits - 1, mant, scale1);
  652. }
  653. }
  654. } else {
  655. s->sb_samples[0][k * 12 + l + 0][i] = 0;
  656. s->sb_samples[0][k * 12 + l + 1][i] = 0;
  657. s->sb_samples[0][k * 12 + l + 2][i] = 0;
  658. s->sb_samples[1][k * 12 + l + 0][i] = 0;
  659. s->sb_samples[1][k * 12 + l + 1][i] = 0;
  660. s->sb_samples[1][k * 12 + l + 2][i] = 0;
  661. }
  662. /* next subband in alloc table */
  663. j += 1 << bit_alloc_bits;
  664. }
  665. /* fill remaining samples to zero */
  666. for (i = sblimit; i < SBLIMIT; i++) {
  667. for (ch = 0; ch < s->nb_channels; ch++) {
  668. s->sb_samples[ch][k * 12 + l + 0][i] = 0;
  669. s->sb_samples[ch][k * 12 + l + 1][i] = 0;
  670. s->sb_samples[ch][k * 12 + l + 2][i] = 0;
  671. }
  672. }
  673. }
  674. }
  675. return 3 * 12;
  676. }
  677. #define SPLIT(dst,sf,n) \
  678. if (n == 3) { \
  679. int m = (sf * 171) >> 9; \
  680. dst = sf - 3 * m; \
  681. sf = m; \
  682. } else if (n == 4) { \
  683. dst = sf & 3; \
  684. sf >>= 2; \
  685. } else if (n == 5) { \
  686. int m = (sf * 205) >> 10; \
  687. dst = sf - 5 * m; \
  688. sf = m; \
  689. } else if (n == 6) { \
  690. int m = (sf * 171) >> 10; \
  691. dst = sf - 6 * m; \
  692. sf = m; \
  693. } else { \
  694. dst = 0; \
  695. }
  696. static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
  697. int n3)
  698. {
  699. SPLIT(slen[3], sf, n3)
  700. SPLIT(slen[2], sf, n2)
  701. SPLIT(slen[1], sf, n1)
  702. slen[0] = sf;
  703. }
  704. static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
  705. int16_t *exponents)
  706. {
  707. const uint8_t *bstab, *pretab;
  708. int len, i, j, k, l, v0, shift, gain, gains[3];
  709. int16_t *exp_ptr;
  710. exp_ptr = exponents;
  711. gain = g->global_gain - 210;
  712. shift = g->scalefac_scale + 1;
  713. bstab = band_size_long[s->sample_rate_index];
  714. pretab = mpa_pretab[g->preflag];
  715. for (i = 0; i < g->long_end; i++) {
  716. v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
  717. len = bstab[i];
  718. for (j = len; j > 0; j--)
  719. *exp_ptr++ = v0;
  720. }
  721. if (g->short_start < 13) {
  722. bstab = band_size_short[s->sample_rate_index];
  723. gains[0] = gain - (g->subblock_gain[0] << 3);
  724. gains[1] = gain - (g->subblock_gain[1] << 3);
  725. gains[2] = gain - (g->subblock_gain[2] << 3);
  726. k = g->long_end;
  727. for (i = g->short_start; i < 13; i++) {
  728. len = bstab[i];
  729. for (l = 0; l < 3; l++) {
  730. v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
  731. for (j = len; j > 0; j--)
  732. *exp_ptr++ = v0;
  733. }
  734. }
  735. }
  736. }
  737. /* handle n = 0 too */
  738. static inline int get_bitsz(GetBitContext *s, int n)
  739. {
  740. return n ? get_bits(s, n) : 0;
  741. }
  742. static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
  743. int *end_pos2)
  744. {
  745. if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
  746. s->gb = s->in_gb;
  747. s->in_gb.buffer = NULL;
  748. assert((get_bits_count(&s->gb) & 7) == 0);
  749. skip_bits_long(&s->gb, *pos - *end_pos);
  750. *end_pos2 =
  751. *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
  752. *pos = get_bits_count(&s->gb);
  753. }
  754. }
  755. /* Following is a optimized code for
  756. INTFLOAT v = *src
  757. if(get_bits1(&s->gb))
  758. v = -v;
  759. *dst = v;
  760. */
  761. #if CONFIG_FLOAT
  762. #define READ_FLIP_SIGN(dst,src) \
  763. v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
  764. AV_WN32A(dst, v);
  765. #else
  766. #define READ_FLIP_SIGN(dst,src) \
  767. v = -get_bits1(&s->gb); \
  768. *(dst) = (*(src) ^ v) - v;
  769. #endif
  770. static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
  771. int16_t *exponents, int end_pos2)
  772. {
  773. int s_index;
  774. int i;
  775. int last_pos, bits_left;
  776. VLC *vlc;
  777. int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
  778. /* low frequencies (called big values) */
  779. s_index = 0;
  780. for (i = 0; i < 3; i++) {
  781. int j, k, l, linbits;
  782. j = g->region_size[i];
  783. if (j == 0)
  784. continue;
  785. /* select vlc table */
  786. k = g->table_select[i];
  787. l = mpa_huff_data[k][0];
  788. linbits = mpa_huff_data[k][1];
  789. vlc = &huff_vlc[l];
  790. if (!l) {
  791. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
  792. s_index += 2 * j;
  793. continue;
  794. }
  795. /* read huffcode and compute each couple */
  796. for (; j > 0; j--) {
  797. int exponent, x, y;
  798. int v;
  799. int pos = get_bits_count(&s->gb);
  800. if (pos >= end_pos){
  801. switch_buffer(s, &pos, &end_pos, &end_pos2);
  802. if (pos >= end_pos)
  803. break;
  804. }
  805. y = get_vlc2(&s->gb, vlc->table, 7, 3);
  806. if (!y) {
  807. g->sb_hybrid[s_index ] =
  808. g->sb_hybrid[s_index+1] = 0;
  809. s_index += 2;
  810. continue;
  811. }
  812. exponent= exponents[s_index];
  813. av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
  814. i, g->region_size[i] - j, x, y, exponent);
  815. if (y & 16) {
  816. x = y >> 5;
  817. y = y & 0x0f;
  818. if (x < 15) {
  819. READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
  820. } else {
  821. x += get_bitsz(&s->gb, linbits);
  822. v = l3_unscale(x, exponent);
  823. if (get_bits1(&s->gb))
  824. v = -v;
  825. g->sb_hybrid[s_index] = v;
  826. }
  827. if (y < 15) {
  828. READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
  829. } else {
  830. y += get_bitsz(&s->gb, linbits);
  831. v = l3_unscale(y, exponent);
  832. if (get_bits1(&s->gb))
  833. v = -v;
  834. g->sb_hybrid[s_index+1] = v;
  835. }
  836. } else {
  837. x = y >> 5;
  838. y = y & 0x0f;
  839. x += y;
  840. if (x < 15) {
  841. READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
  842. } else {
  843. x += get_bitsz(&s->gb, linbits);
  844. v = l3_unscale(x, exponent);
  845. if (get_bits1(&s->gb))
  846. v = -v;
  847. g->sb_hybrid[s_index+!!y] = v;
  848. }
  849. g->sb_hybrid[s_index + !y] = 0;
  850. }
  851. s_index += 2;
  852. }
  853. }
  854. /* high frequencies */
  855. vlc = &huff_quad_vlc[g->count1table_select];
  856. last_pos = 0;
  857. while (s_index <= 572) {
  858. int pos, code;
  859. pos = get_bits_count(&s->gb);
  860. if (pos >= end_pos) {
  861. if (pos > end_pos2 && last_pos) {
  862. /* some encoders generate an incorrect size for this
  863. part. We must go back into the data */
  864. s_index -= 4;
  865. skip_bits_long(&s->gb, last_pos - pos);
  866. av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
  867. if(s->err_recognition & AV_EF_BITSTREAM)
  868. s_index=0;
  869. break;
  870. }
  871. switch_buffer(s, &pos, &end_pos, &end_pos2);
  872. if (pos >= end_pos)
  873. break;
  874. }
  875. last_pos = pos;
  876. code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
  877. av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
  878. g->sb_hybrid[s_index+0] =
  879. g->sb_hybrid[s_index+1] =
  880. g->sb_hybrid[s_index+2] =
  881. g->sb_hybrid[s_index+3] = 0;
  882. while (code) {
  883. static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
  884. int v;
  885. int pos = s_index + idxtab[code];
  886. code ^= 8 >> idxtab[code];
  887. READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
  888. }
  889. s_index += 4;
  890. }
  891. /* skip extension bits */
  892. bits_left = end_pos2 - get_bits_count(&s->gb);
  893. if (bits_left < 0 && (s->err_recognition & AV_EF_BUFFER)) {
  894. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  895. s_index=0;
  896. } else if (bits_left > 0 && (s->err_recognition & AV_EF_BUFFER)) {
  897. av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
  898. s_index = 0;
  899. }
  900. memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
  901. skip_bits_long(&s->gb, bits_left);
  902. i = get_bits_count(&s->gb);
  903. switch_buffer(s, &i, &end_pos, &end_pos2);
  904. return 0;
  905. }
  906. /* Reorder short blocks from bitstream order to interleaved order. It
  907. would be faster to do it in parsing, but the code would be far more
  908. complicated */
  909. static void reorder_block(MPADecodeContext *s, GranuleDef *g)
  910. {
  911. int i, j, len;
  912. INTFLOAT *ptr, *dst, *ptr1;
  913. INTFLOAT tmp[576];
  914. if (g->block_type != 2)
  915. return;
  916. if (g->switch_point) {
  917. if (s->sample_rate_index != 8)
  918. ptr = g->sb_hybrid + 36;
  919. else
  920. ptr = g->sb_hybrid + 72;
  921. } else {
  922. ptr = g->sb_hybrid;
  923. }
  924. for (i = g->short_start; i < 13; i++) {
  925. len = band_size_short[s->sample_rate_index][i];
  926. ptr1 = ptr;
  927. dst = tmp;
  928. for (j = len; j > 0; j--) {
  929. *dst++ = ptr[0*len];
  930. *dst++ = ptr[1*len];
  931. *dst++ = ptr[2*len];
  932. ptr++;
  933. }
  934. ptr += 2 * len;
  935. memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
  936. }
  937. }
  938. #define ISQRT2 FIXR(0.70710678118654752440)
  939. static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
  940. {
  941. int i, j, k, l;
  942. int sf_max, sf, len, non_zero_found;
  943. INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
  944. int non_zero_found_short[3];
  945. /* intensity stereo */
  946. if (s->mode_ext & MODE_EXT_I_STEREO) {
  947. if (!s->lsf) {
  948. is_tab = is_table;
  949. sf_max = 7;
  950. } else {
  951. is_tab = is_table_lsf[g1->scalefac_compress & 1];
  952. sf_max = 16;
  953. }
  954. tab0 = g0->sb_hybrid + 576;
  955. tab1 = g1->sb_hybrid + 576;
  956. non_zero_found_short[0] = 0;
  957. non_zero_found_short[1] = 0;
  958. non_zero_found_short[2] = 0;
  959. k = (13 - g1->short_start) * 3 + g1->long_end - 3;
  960. for (i = 12; i >= g1->short_start; i--) {
  961. /* for last band, use previous scale factor */
  962. if (i != 11)
  963. k -= 3;
  964. len = band_size_short[s->sample_rate_index][i];
  965. for (l = 2; l >= 0; l--) {
  966. tab0 -= len;
  967. tab1 -= len;
  968. if (!non_zero_found_short[l]) {
  969. /* test if non zero band. if so, stop doing i-stereo */
  970. for (j = 0; j < len; j++) {
  971. if (tab1[j] != 0) {
  972. non_zero_found_short[l] = 1;
  973. goto found1;
  974. }
  975. }
  976. sf = g1->scale_factors[k + l];
  977. if (sf >= sf_max)
  978. goto found1;
  979. v1 = is_tab[0][sf];
  980. v2 = is_tab[1][sf];
  981. for (j = 0; j < len; j++) {
  982. tmp0 = tab0[j];
  983. tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
  984. tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
  985. }
  986. } else {
  987. found1:
  988. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  989. /* lower part of the spectrum : do ms stereo
  990. if enabled */
  991. for (j = 0; j < len; j++) {
  992. tmp0 = tab0[j];
  993. tmp1 = tab1[j];
  994. tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  995. tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  996. }
  997. }
  998. }
  999. }
  1000. }
  1001. non_zero_found = non_zero_found_short[0] |
  1002. non_zero_found_short[1] |
  1003. non_zero_found_short[2];
  1004. for (i = g1->long_end - 1;i >= 0;i--) {
  1005. len = band_size_long[s->sample_rate_index][i];
  1006. tab0 -= len;
  1007. tab1 -= len;
  1008. /* test if non zero band. if so, stop doing i-stereo */
  1009. if (!non_zero_found) {
  1010. for (j = 0; j < len; j++) {
  1011. if (tab1[j] != 0) {
  1012. non_zero_found = 1;
  1013. goto found2;
  1014. }
  1015. }
  1016. /* for last band, use previous scale factor */
  1017. k = (i == 21) ? 20 : i;
  1018. sf = g1->scale_factors[k];
  1019. if (sf >= sf_max)
  1020. goto found2;
  1021. v1 = is_tab[0][sf];
  1022. v2 = is_tab[1][sf];
  1023. for (j = 0; j < len; j++) {
  1024. tmp0 = tab0[j];
  1025. tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
  1026. tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
  1027. }
  1028. } else {
  1029. found2:
  1030. if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1031. /* lower part of the spectrum : do ms stereo
  1032. if enabled */
  1033. for (j = 0; j < len; j++) {
  1034. tmp0 = tab0[j];
  1035. tmp1 = tab1[j];
  1036. tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
  1037. tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
  1038. }
  1039. }
  1040. }
  1041. }
  1042. } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
  1043. /* ms stereo ONLY */
  1044. /* NOTE: the 1/sqrt(2) normalization factor is included in the
  1045. global gain */
  1046. #if CONFIG_FLOAT
  1047. s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
  1048. #else
  1049. tab0 = g0->sb_hybrid;
  1050. tab1 = g1->sb_hybrid;
  1051. for (i = 0; i < 576; i++) {
  1052. tmp0 = tab0[i];
  1053. tmp1 = tab1[i];
  1054. tab0[i] = tmp0 + tmp1;
  1055. tab1[i] = tmp0 - tmp1;
  1056. }
  1057. #endif
  1058. }
  1059. }
  1060. #if CONFIG_FLOAT
  1061. #define AA(j) do { \
  1062. float tmp0 = ptr[-1-j]; \
  1063. float tmp1 = ptr[ j]; \
  1064. ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
  1065. ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
  1066. } while (0)
  1067. #else
  1068. #define AA(j) do { \
  1069. int tmp0 = ptr[-1-j]; \
  1070. int tmp1 = ptr[ j]; \
  1071. int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
  1072. ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
  1073. ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
  1074. } while (0)
  1075. #endif
  1076. static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
  1077. {
  1078. INTFLOAT *ptr;
  1079. int n, i;
  1080. /* we antialias only "long" bands */
  1081. if (g->block_type == 2) {
  1082. if (!g->switch_point)
  1083. return;
  1084. /* XXX: check this for 8000Hz case */
  1085. n = 1;
  1086. } else {
  1087. n = SBLIMIT - 1;
  1088. }
  1089. ptr = g->sb_hybrid + 18;
  1090. for (i = n; i > 0; i--) {
  1091. AA(0);
  1092. AA(1);
  1093. AA(2);
  1094. AA(3);
  1095. AA(4);
  1096. AA(5);
  1097. AA(6);
  1098. AA(7);
  1099. ptr += 18;
  1100. }
  1101. }
  1102. static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
  1103. INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
  1104. {
  1105. INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
  1106. INTFLOAT out2[12];
  1107. int i, j, mdct_long_end, sblimit;
  1108. /* find last non zero block */
  1109. ptr = g->sb_hybrid + 576;
  1110. ptr1 = g->sb_hybrid + 2 * 18;
  1111. while (ptr >= ptr1) {
  1112. int32_t *p;
  1113. ptr -= 6;
  1114. p = (int32_t*)ptr;
  1115. if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
  1116. break;
  1117. }
  1118. sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
  1119. if (g->block_type == 2) {
  1120. /* XXX: check for 8000 Hz */
  1121. if (g->switch_point)
  1122. mdct_long_end = 2;
  1123. else
  1124. mdct_long_end = 0;
  1125. } else {
  1126. mdct_long_end = sblimit;
  1127. }
  1128. s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
  1129. mdct_long_end, g->switch_point,
  1130. g->block_type);
  1131. buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
  1132. ptr = g->sb_hybrid + 18 * mdct_long_end;
  1133. for (j = mdct_long_end; j < sblimit; j++) {
  1134. /* select frequency inversion */
  1135. win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
  1136. out_ptr = sb_samples + j;
  1137. for (i = 0; i < 6; i++) {
  1138. *out_ptr = buf[4*i];
  1139. out_ptr += SBLIMIT;
  1140. }
  1141. imdct12(out2, ptr + 0);
  1142. for (i = 0; i < 6; i++) {
  1143. *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
  1144. buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
  1145. out_ptr += SBLIMIT;
  1146. }
  1147. imdct12(out2, ptr + 1);
  1148. for (i = 0; i < 6; i++) {
  1149. *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
  1150. buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
  1151. out_ptr += SBLIMIT;
  1152. }
  1153. imdct12(out2, ptr + 2);
  1154. for (i = 0; i < 6; i++) {
  1155. buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
  1156. buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
  1157. buf[4*(i + 6*2)] = 0;
  1158. }
  1159. ptr += 18;
  1160. buf += (j&3) != 3 ? 1 : (4*18-3);
  1161. }
  1162. /* zero bands */
  1163. for (j = sblimit; j < SBLIMIT; j++) {
  1164. /* overlap */
  1165. out_ptr = sb_samples + j;
  1166. for (i = 0; i < 18; i++) {
  1167. *out_ptr = buf[4*i];
  1168. buf[4*i] = 0;
  1169. out_ptr += SBLIMIT;
  1170. }
  1171. buf += (j&3) != 3 ? 1 : (4*18-3);
  1172. }
  1173. }
  1174. /* main layer3 decoding function */
  1175. static int mp_decode_layer3(MPADecodeContext *s)
  1176. {
  1177. int nb_granules, main_data_begin;
  1178. int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
  1179. GranuleDef *g;
  1180. int16_t exponents[576]; //FIXME try INTFLOAT
  1181. /* read side info */
  1182. if (s->lsf) {
  1183. main_data_begin = get_bits(&s->gb, 8);
  1184. skip_bits(&s->gb, s->nb_channels);
  1185. nb_granules = 1;
  1186. } else {
  1187. main_data_begin = get_bits(&s->gb, 9);
  1188. if (s->nb_channels == 2)
  1189. skip_bits(&s->gb, 3);
  1190. else
  1191. skip_bits(&s->gb, 5);
  1192. nb_granules = 2;
  1193. for (ch = 0; ch < s->nb_channels; ch++) {
  1194. s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
  1195. s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
  1196. }
  1197. }
  1198. for (gr = 0; gr < nb_granules; gr++) {
  1199. for (ch = 0; ch < s->nb_channels; ch++) {
  1200. av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
  1201. g = &s->granules[ch][gr];
  1202. g->part2_3_length = get_bits(&s->gb, 12);
  1203. g->big_values = get_bits(&s->gb, 9);
  1204. if (g->big_values > 288) {
  1205. av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
  1206. return AVERROR_INVALIDDATA;
  1207. }
  1208. g->global_gain = get_bits(&s->gb, 8);
  1209. /* if MS stereo only is selected, we precompute the
  1210. 1/sqrt(2) renormalization factor */
  1211. if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
  1212. MODE_EXT_MS_STEREO)
  1213. g->global_gain -= 2;
  1214. if (s->lsf)
  1215. g->scalefac_compress = get_bits(&s->gb, 9);
  1216. else
  1217. g->scalefac_compress = get_bits(&s->gb, 4);
  1218. blocksplit_flag = get_bits1(&s->gb);
  1219. if (blocksplit_flag) {
  1220. g->block_type = get_bits(&s->gb, 2);
  1221. if (g->block_type == 0) {
  1222. av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
  1223. return AVERROR_INVALIDDATA;
  1224. }
  1225. g->switch_point = get_bits1(&s->gb);
  1226. for (i = 0; i < 2; i++)
  1227. g->table_select[i] = get_bits(&s->gb, 5);
  1228. for (i = 0; i < 3; i++)
  1229. g->subblock_gain[i] = get_bits(&s->gb, 3);
  1230. ff_init_short_region(s, g);
  1231. } else {
  1232. int region_address1, region_address2;
  1233. g->block_type = 0;
  1234. g->switch_point = 0;
  1235. for (i = 0; i < 3; i++)
  1236. g->table_select[i] = get_bits(&s->gb, 5);
  1237. /* compute huffman coded region sizes */
  1238. region_address1 = get_bits(&s->gb, 4);
  1239. region_address2 = get_bits(&s->gb, 3);
  1240. av_dlog(s->avctx, "region1=%d region2=%d\n",
  1241. region_address1, region_address2);
  1242. ff_init_long_region(s, g, region_address1, region_address2);
  1243. }
  1244. ff_region_offset2size(g);
  1245. ff_compute_band_indexes(s, g);
  1246. g->preflag = 0;
  1247. if (!s->lsf)
  1248. g->preflag = get_bits1(&s->gb);
  1249. g->scalefac_scale = get_bits1(&s->gb);
  1250. g->count1table_select = get_bits1(&s->gb);
  1251. av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
  1252. g->block_type, g->switch_point);
  1253. }
  1254. }
  1255. if (!s->adu_mode) {
  1256. int skip;
  1257. const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
  1258. int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0,
  1259. FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
  1260. assert((get_bits_count(&s->gb) & 7) == 0);
  1261. /* now we get bits from the main_data_begin offset */
  1262. av_dlog(s->avctx, "seekback:%d, lastbuf:%d\n",
  1263. main_data_begin, s->last_buf_size);
  1264. memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
  1265. s->in_gb = s->gb;
  1266. init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
  1267. #if !UNCHECKED_BITSTREAM_READER
  1268. s->gb.size_in_bits_plus8 += extrasize * 8;
  1269. #endif
  1270. s->last_buf_size <<= 3;
  1271. for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
  1272. for (ch = 0; ch < s->nb_channels; ch++) {
  1273. g = &s->granules[ch][gr];
  1274. s->last_buf_size += g->part2_3_length;
  1275. memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
  1276. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  1277. }
  1278. }
  1279. skip = s->last_buf_size - 8 * main_data_begin;
  1280. if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
  1281. skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
  1282. s->gb = s->in_gb;
  1283. s->in_gb.buffer = NULL;
  1284. } else {
  1285. skip_bits_long(&s->gb, skip);
  1286. }
  1287. } else {
  1288. gr = 0;
  1289. }
  1290. for (; gr < nb_granules; gr++) {
  1291. for (ch = 0; ch < s->nb_channels; ch++) {
  1292. g = &s->granules[ch][gr];
  1293. bits_pos = get_bits_count(&s->gb);
  1294. if (!s->lsf) {
  1295. uint8_t *sc;
  1296. int slen, slen1, slen2;
  1297. /* MPEG1 scale factors */
  1298. slen1 = slen_table[0][g->scalefac_compress];
  1299. slen2 = slen_table[1][g->scalefac_compress];
  1300. av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
  1301. if (g->block_type == 2) {
  1302. n = g->switch_point ? 17 : 18;
  1303. j = 0;
  1304. if (slen1) {
  1305. for (i = 0; i < n; i++)
  1306. g->scale_factors[j++] = get_bits(&s->gb, slen1);
  1307. } else {
  1308. for (i = 0; i < n; i++)
  1309. g->scale_factors[j++] = 0;
  1310. }
  1311. if (slen2) {
  1312. for (i = 0; i < 18; i++)
  1313. g->scale_factors[j++] = get_bits(&s->gb, slen2);
  1314. for (i = 0; i < 3; i++)
  1315. g->scale_factors[j++] = 0;
  1316. } else {
  1317. for (i = 0; i < 21; i++)
  1318. g->scale_factors[j++] = 0;
  1319. }
  1320. } else {
  1321. sc = s->granules[ch][0].scale_factors;
  1322. j = 0;
  1323. for (k = 0; k < 4; k++) {
  1324. n = k == 0 ? 6 : 5;
  1325. if ((g->scfsi & (0x8 >> k)) == 0) {
  1326. slen = (k < 2) ? slen1 : slen2;
  1327. if (slen) {
  1328. for (i = 0; i < n; i++)
  1329. g->scale_factors[j++] = get_bits(&s->gb, slen);
  1330. } else {
  1331. for (i = 0; i < n; i++)
  1332. g->scale_factors[j++] = 0;
  1333. }
  1334. } else {
  1335. /* simply copy from last granule */
  1336. for (i = 0; i < n; i++) {
  1337. g->scale_factors[j] = sc[j];
  1338. j++;
  1339. }
  1340. }
  1341. }
  1342. g->scale_factors[j++] = 0;
  1343. }
  1344. } else {
  1345. int tindex, tindex2, slen[4], sl, sf;
  1346. /* LSF scale factors */
  1347. if (g->block_type == 2)
  1348. tindex = g->switch_point ? 2 : 1;
  1349. else
  1350. tindex = 0;
  1351. sf = g->scalefac_compress;
  1352. if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
  1353. /* intensity stereo case */
  1354. sf >>= 1;
  1355. if (sf < 180) {
  1356. lsf_sf_expand(slen, sf, 6, 6, 0);
  1357. tindex2 = 3;
  1358. } else if (sf < 244) {
  1359. lsf_sf_expand(slen, sf - 180, 4, 4, 0);
  1360. tindex2 = 4;
  1361. } else {
  1362. lsf_sf_expand(slen, sf - 244, 3, 0, 0);
  1363. tindex2 = 5;
  1364. }
  1365. } else {
  1366. /* normal case */
  1367. if (sf < 400) {
  1368. lsf_sf_expand(slen, sf, 5, 4, 4);
  1369. tindex2 = 0;
  1370. } else if (sf < 500) {
  1371. lsf_sf_expand(slen, sf - 400, 5, 4, 0);
  1372. tindex2 = 1;
  1373. } else {
  1374. lsf_sf_expand(slen, sf - 500, 3, 0, 0);
  1375. tindex2 = 2;
  1376. g->preflag = 1;
  1377. }
  1378. }
  1379. j = 0;
  1380. for (k = 0; k < 4; k++) {
  1381. n = lsf_nsf_table[tindex2][tindex][k];
  1382. sl = slen[k];
  1383. if (sl) {
  1384. for (i = 0; i < n; i++)
  1385. g->scale_factors[j++] = get_bits(&s->gb, sl);
  1386. } else {
  1387. for (i = 0; i < n; i++)
  1388. g->scale_factors[j++] = 0;
  1389. }
  1390. }
  1391. /* XXX: should compute exact size */
  1392. for (; j < 40; j++)
  1393. g->scale_factors[j] = 0;
  1394. }
  1395. exponents_from_scale_factors(s, g, exponents);
  1396. /* read Huffman coded residue */
  1397. huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
  1398. } /* ch */
  1399. if (s->mode == MPA_JSTEREO)
  1400. compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
  1401. for (ch = 0; ch < s->nb_channels; ch++) {
  1402. g = &s->granules[ch][gr];
  1403. reorder_block(s, g);
  1404. compute_antialias(s, g);
  1405. compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
  1406. }
  1407. } /* gr */
  1408. if (get_bits_count(&s->gb) < 0)
  1409. skip_bits_long(&s->gb, -get_bits_count(&s->gb));
  1410. return nb_granules * 18;
  1411. }
  1412. static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
  1413. const uint8_t *buf, int buf_size)
  1414. {
  1415. int i, nb_frames, ch, ret;
  1416. OUT_INT *samples_ptr;
  1417. init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
  1418. /* skip error protection field */
  1419. if (s->error_protection)
  1420. skip_bits(&s->gb, 16);
  1421. switch(s->layer) {
  1422. case 1:
  1423. s->avctx->frame_size = 384;
  1424. nb_frames = mp_decode_layer1(s);
  1425. break;
  1426. case 2:
  1427. s->avctx->frame_size = 1152;
  1428. nb_frames = mp_decode_layer2(s);
  1429. break;
  1430. case 3:
  1431. s->avctx->frame_size = s->lsf ? 576 : 1152;
  1432. default:
  1433. nb_frames = mp_decode_layer3(s);
  1434. if (nb_frames < 0)
  1435. return nb_frames;
  1436. s->last_buf_size=0;
  1437. if (s->in_gb.buffer) {
  1438. align_get_bits(&s->gb);
  1439. i = get_bits_left(&s->gb)>>3;
  1440. if (i >= 0 && i <= BACKSTEP_SIZE) {
  1441. memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
  1442. s->last_buf_size=i;
  1443. } else
  1444. av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
  1445. s->gb = s->in_gb;
  1446. s->in_gb.buffer = NULL;
  1447. }
  1448. align_get_bits(&s->gb);
  1449. assert((get_bits_count(&s->gb) & 7) == 0);
  1450. i = get_bits_left(&s->gb) >> 3;
  1451. if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
  1452. if (i < 0)
  1453. av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
  1454. i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
  1455. }
  1456. assert(i <= buf_size - HEADER_SIZE && i >= 0);
  1457. memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
  1458. s->last_buf_size += i;
  1459. }
  1460. /* get output buffer */
  1461. if (!samples) {
  1462. s->frame.nb_samples = s->avctx->frame_size;
  1463. if ((ret = ff_get_buffer(s->avctx, &s->frame)) < 0) {
  1464. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1465. return ret;
  1466. }
  1467. samples = (OUT_INT **)s->frame.extended_data;
  1468. }
  1469. /* apply the synthesis filter */
  1470. for (ch = 0; ch < s->nb_channels; ch++) {
  1471. int sample_stride;
  1472. if (s->avctx->sample_fmt == OUT_FMT_P) {
  1473. samples_ptr = samples[ch];
  1474. sample_stride = 1;
  1475. } else {
  1476. samples_ptr = samples[0] + ch;
  1477. sample_stride = s->nb_channels;
  1478. }
  1479. for (i = 0; i < nb_frames; i++) {
  1480. RENAME(ff_mpa_synth_filter)(&s->mpadsp, s->synth_buf[ch],
  1481. &(s->synth_buf_offset[ch]),
  1482. RENAME(ff_mpa_synth_window),
  1483. &s->dither_state, samples_ptr,
  1484. sample_stride, s->sb_samples[ch][i]);
  1485. samples_ptr += 32 * sample_stride;
  1486. }
  1487. }
  1488. return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
  1489. }
  1490. static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
  1491. AVPacket *avpkt)
  1492. {
  1493. const uint8_t *buf = avpkt->data;
  1494. int buf_size = avpkt->size;
  1495. MPADecodeContext *s = avctx->priv_data;
  1496. uint32_t header;
  1497. int ret;
  1498. if (buf_size < HEADER_SIZE)
  1499. return AVERROR_INVALIDDATA;
  1500. header = AV_RB32(buf);
  1501. if (ff_mpa_check_header(header) < 0) {
  1502. av_log(avctx, AV_LOG_ERROR, "Header missing\n");
  1503. return AVERROR_INVALIDDATA;
  1504. }
  1505. if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
  1506. /* free format: prepare to compute frame size */
  1507. s->frame_size = -1;
  1508. return AVERROR_INVALIDDATA;
  1509. }
  1510. /* update codec info */
  1511. avctx->channels = s->nb_channels;
  1512. avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  1513. if (!avctx->bit_rate)
  1514. avctx->bit_rate = s->bit_rate;
  1515. if (s->frame_size <= 0 || s->frame_size > buf_size) {
  1516. av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
  1517. return AVERROR_INVALIDDATA;
  1518. } else if (s->frame_size < buf_size) {
  1519. buf_size= s->frame_size;
  1520. }
  1521. ret = mp_decode_frame(s, NULL, buf, buf_size);
  1522. if (ret >= 0) {
  1523. *got_frame_ptr = 1;
  1524. *(AVFrame *)data = s->frame;
  1525. avctx->sample_rate = s->sample_rate;
  1526. //FIXME maybe move the other codec info stuff from above here too
  1527. } else {
  1528. av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
  1529. /* Only return an error if the bad frame makes up the whole packet or
  1530. * the error is related to buffer management.
  1531. * If there is more data in the packet, just consume the bad frame
  1532. * instead of returning an error, which would discard the whole
  1533. * packet. */
  1534. *got_frame_ptr = 0;
  1535. if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA)
  1536. return ret;
  1537. }
  1538. s->frame_size = 0;
  1539. return buf_size;
  1540. }
  1541. static void mp_flush(MPADecodeContext *ctx)
  1542. {
  1543. memset(ctx->synth_buf, 0, sizeof(ctx->synth_buf));
  1544. ctx->last_buf_size = 0;
  1545. }
  1546. static void flush(AVCodecContext *avctx)
  1547. {
  1548. mp_flush(avctx->priv_data);
  1549. }
  1550. #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
  1551. static int decode_frame_adu(AVCodecContext *avctx, void *data,
  1552. int *got_frame_ptr, AVPacket *avpkt)
  1553. {
  1554. const uint8_t *buf = avpkt->data;
  1555. int buf_size = avpkt->size;
  1556. MPADecodeContext *s = avctx->priv_data;
  1557. uint32_t header;
  1558. int len, ret;
  1559. len = buf_size;
  1560. // Discard too short frames
  1561. if (buf_size < HEADER_SIZE) {
  1562. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  1563. return AVERROR_INVALIDDATA;
  1564. }
  1565. if (len > MPA_MAX_CODED_FRAME_SIZE)
  1566. len = MPA_MAX_CODED_FRAME_SIZE;
  1567. // Get header and restore sync word
  1568. header = AV_RB32(buf) | 0xffe00000;
  1569. if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
  1570. av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
  1571. return AVERROR_INVALIDDATA;
  1572. }
  1573. avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
  1574. /* update codec info */
  1575. avctx->sample_rate = s->sample_rate;
  1576. avctx->channels = s->nb_channels;
  1577. if (!avctx->bit_rate)
  1578. avctx->bit_rate = s->bit_rate;
  1579. s->frame_size = len;
  1580. ret = mp_decode_frame(s, NULL, buf, buf_size);
  1581. if (ret < 0) {
  1582. av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
  1583. return ret;
  1584. }
  1585. *got_frame_ptr = 1;
  1586. *(AVFrame *)data = s->frame;
  1587. return buf_size;
  1588. }
  1589. #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
  1590. #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
  1591. /**
  1592. * Context for MP3On4 decoder
  1593. */
  1594. typedef struct MP3On4DecodeContext {
  1595. AVFrame *frame;
  1596. int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
  1597. int syncword; ///< syncword patch
  1598. const uint8_t *coff; ///< channel offsets in output buffer
  1599. MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
  1600. } MP3On4DecodeContext;
  1601. #include "mpeg4audio.h"
  1602. /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
  1603. /* number of mp3 decoder instances */
  1604. static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
  1605. /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
  1606. static const uint8_t chan_offset[8][5] = {
  1607. { 0 },
  1608. { 0 }, // C
  1609. { 0 }, // FLR
  1610. { 2, 0 }, // C FLR
  1611. { 2, 0, 3 }, // C FLR BS
  1612. { 2, 0, 3 }, // C FLR BLRS
  1613. { 2, 0, 4, 3 }, // C FLR BLRS LFE
  1614. { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
  1615. };
  1616. /* mp3on4 channel layouts */
  1617. static const int16_t chan_layout[8] = {
  1618. 0,
  1619. AV_CH_LAYOUT_MONO,
  1620. AV_CH_LAYOUT_STEREO,
  1621. AV_CH_LAYOUT_SURROUND,
  1622. AV_CH_LAYOUT_4POINT0,
  1623. AV_CH_LAYOUT_5POINT0,
  1624. AV_CH_LAYOUT_5POINT1,
  1625. AV_CH_LAYOUT_7POINT1
  1626. };
  1627. static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
  1628. {
  1629. MP3On4DecodeContext *s = avctx->priv_data;
  1630. int i;
  1631. for (i = 0; i < s->frames; i++)
  1632. av_free(s->mp3decctx[i]);
  1633. return 0;
  1634. }
  1635. static int decode_init_mp3on4(AVCodecContext * avctx)
  1636. {
  1637. MP3On4DecodeContext *s = avctx->priv_data;
  1638. MPEG4AudioConfig cfg;
  1639. int i;
  1640. if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
  1641. av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
  1642. return AVERROR_INVALIDDATA;
  1643. }
  1644. avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
  1645. avctx->extradata_size * 8, 1);
  1646. if (!cfg.chan_config || cfg.chan_config > 7) {
  1647. av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
  1648. return AVERROR_INVALIDDATA;
  1649. }
  1650. s->frames = mp3Frames[cfg.chan_config];
  1651. s->coff = chan_offset[cfg.chan_config];
  1652. avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
  1653. avctx->channel_layout = chan_layout[cfg.chan_config];
  1654. if (cfg.sample_rate < 16000)
  1655. s->syncword = 0xffe00000;
  1656. else
  1657. s->syncword = 0xfff00000;
  1658. /* Init the first mp3 decoder in standard way, so that all tables get builded
  1659. * We replace avctx->priv_data with the context of the first decoder so that
  1660. * decode_init() does not have to be changed.
  1661. * Other decoders will be initialized here copying data from the first context
  1662. */
  1663. // Allocate zeroed memory for the first decoder context
  1664. s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
  1665. if (!s->mp3decctx[0])
  1666. goto alloc_fail;
  1667. // Put decoder context in place to make init_decode() happy
  1668. avctx->priv_data = s->mp3decctx[0];
  1669. decode_init(avctx);
  1670. s->frame = avctx->coded_frame;
  1671. // Restore mp3on4 context pointer
  1672. avctx->priv_data = s;
  1673. s->mp3decctx[0]->adu_mode = 1; // Set adu mode
  1674. /* Create a separate codec/context for each frame (first is already ok).
  1675. * Each frame is 1 or 2 channels - up to 5 frames allowed
  1676. */
  1677. for (i = 1; i < s->frames; i++) {
  1678. s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
  1679. if (!s->mp3decctx[i])
  1680. goto alloc_fail;
  1681. s->mp3decctx[i]->adu_mode = 1;
  1682. s->mp3decctx[i]->avctx = avctx;
  1683. s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
  1684. }
  1685. return 0;
  1686. alloc_fail:
  1687. decode_close_mp3on4(avctx);
  1688. return AVERROR(ENOMEM);
  1689. }
  1690. static void flush_mp3on4(AVCodecContext *avctx)
  1691. {
  1692. int i;
  1693. MP3On4DecodeContext *s = avctx->priv_data;
  1694. for (i = 0; i < s->frames; i++)
  1695. mp_flush(s->mp3decctx[i]);
  1696. }
  1697. static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
  1698. int *got_frame_ptr, AVPacket *avpkt)
  1699. {
  1700. const uint8_t *buf = avpkt->data;
  1701. int buf_size = avpkt->size;
  1702. MP3On4DecodeContext *s = avctx->priv_data;
  1703. MPADecodeContext *m;
  1704. int fsize, len = buf_size, out_size = 0;
  1705. uint32_t header;
  1706. OUT_INT **out_samples;
  1707. OUT_INT *outptr[2];
  1708. int fr, ch, ret;
  1709. /* get output buffer */
  1710. s->frame->nb_samples = MPA_FRAME_SIZE;
  1711. if ((ret = ff_get_buffer(avctx, s->frame)) < 0) {
  1712. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1713. return ret;
  1714. }
  1715. out_samples = (OUT_INT **)s->frame->extended_data;
  1716. // Discard too short frames
  1717. if (buf_size < HEADER_SIZE)
  1718. return AVERROR_INVALIDDATA;
  1719. avctx->bit_rate = 0;
  1720. ch = 0;
  1721. for (fr = 0; fr < s->frames; fr++) {
  1722. fsize = AV_RB16(buf) >> 4;
  1723. fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
  1724. m = s->mp3decctx[fr];
  1725. assert(m != NULL);
  1726. if (fsize < HEADER_SIZE) {
  1727. av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
  1728. return AVERROR_INVALIDDATA;
  1729. }
  1730. header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
  1731. if (ff_mpa_check_header(header) < 0) // Bad header, discard block
  1732. break;
  1733. avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
  1734. if (ch + m->nb_channels > avctx->channels) {
  1735. av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
  1736. "channel count\n");
  1737. return AVERROR_INVALIDDATA;
  1738. }
  1739. ch += m->nb_channels;
  1740. outptr[0] = out_samples[s->coff[fr]];
  1741. if (m->nb_channels > 1)
  1742. outptr[1] = out_samples[s->coff[fr] + 1];
  1743. if ((ret = mp_decode_frame(m, outptr, buf, fsize)) < 0)
  1744. return ret;
  1745. out_size += ret;
  1746. buf += fsize;
  1747. len -= fsize;
  1748. avctx->bit_rate += m->bit_rate;
  1749. }
  1750. /* update codec info */
  1751. avctx->sample_rate = s->mp3decctx[0]->sample_rate;
  1752. s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
  1753. *got_frame_ptr = 1;
  1754. *(AVFrame *)data = *s->frame;
  1755. return buf_size;
  1756. }
  1757. #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
  1758. #if !CONFIG_FLOAT
  1759. #if CONFIG_MP1_DECODER
  1760. AVCodec ff_mp1_decoder = {
  1761. .name = "mp1",
  1762. .type = AVMEDIA_TYPE_AUDIO,
  1763. .id = AV_CODEC_ID_MP1,
  1764. .priv_data_size = sizeof(MPADecodeContext),
  1765. .init = decode_init,
  1766. .decode = decode_frame,
  1767. .capabilities = CODEC_CAP_DR1,
  1768. .flush = flush,
  1769. .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
  1770. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  1771. AV_SAMPLE_FMT_S16,
  1772. AV_SAMPLE_FMT_NONE },
  1773. };
  1774. #endif
  1775. #if CONFIG_MP2_DECODER
  1776. AVCodec ff_mp2_decoder = {
  1777. .name = "mp2",
  1778. .type = AVMEDIA_TYPE_AUDIO,
  1779. .id = AV_CODEC_ID_MP2,
  1780. .priv_data_size = sizeof(MPADecodeContext),
  1781. .init = decode_init,
  1782. .decode = decode_frame,
  1783. .capabilities = CODEC_CAP_DR1,
  1784. .flush = flush,
  1785. .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
  1786. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  1787. AV_SAMPLE_FMT_S16,
  1788. AV_SAMPLE_FMT_NONE },
  1789. };
  1790. #endif
  1791. #if CONFIG_MP3_DECODER
  1792. AVCodec ff_mp3_decoder = {
  1793. .name = "mp3",
  1794. .type = AVMEDIA_TYPE_AUDIO,
  1795. .id = AV_CODEC_ID_MP3,
  1796. .priv_data_size = sizeof(MPADecodeContext),
  1797. .init = decode_init,
  1798. .decode = decode_frame,
  1799. .capabilities = CODEC_CAP_DR1,
  1800. .flush = flush,
  1801. .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
  1802. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  1803. AV_SAMPLE_FMT_S16,
  1804. AV_SAMPLE_FMT_NONE },
  1805. };
  1806. #endif
  1807. #if CONFIG_MP3ADU_DECODER
  1808. AVCodec ff_mp3adu_decoder = {
  1809. .name = "mp3adu",
  1810. .type = AVMEDIA_TYPE_AUDIO,
  1811. .id = AV_CODEC_ID_MP3ADU,
  1812. .priv_data_size = sizeof(MPADecodeContext),
  1813. .init = decode_init,
  1814. .decode = decode_frame_adu,
  1815. .capabilities = CODEC_CAP_DR1,
  1816. .flush = flush,
  1817. .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
  1818. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  1819. AV_SAMPLE_FMT_S16,
  1820. AV_SAMPLE_FMT_NONE },
  1821. };
  1822. #endif
  1823. #if CONFIG_MP3ON4_DECODER
  1824. AVCodec ff_mp3on4_decoder = {
  1825. .name = "mp3on4",
  1826. .type = AVMEDIA_TYPE_AUDIO,
  1827. .id = AV_CODEC_ID_MP3ON4,
  1828. .priv_data_size = sizeof(MP3On4DecodeContext),
  1829. .init = decode_init_mp3on4,
  1830. .close = decode_close_mp3on4,
  1831. .decode = decode_frame_mp3on4,
  1832. .capabilities = CODEC_CAP_DR1,
  1833. .flush = flush_mp3on4,
  1834. .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),
  1835. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  1836. AV_SAMPLE_FMT_NONE },
  1837. };
  1838. #endif
  1839. #endif