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