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