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