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