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