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