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