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