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