GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mpegaudiodec_template.c Lines: 737 1061 69.5 %
Date: 2020-07-11 02:49:52 Branches: 382 584 65.4 %

Line Branch Exec Source
1
/*
2
 * MPEG Audio decoder
3
 * Copyright (c) 2001, 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23
 * @file
24
 * MPEG Audio decoder
25
 */
26
27
#include "libavutil/attributes.h"
28
#include "libavutil/avassert.h"
29
#include "libavutil/channel_layout.h"
30
#include "libavutil/crc.h"
31
#include "libavutil/float_dsp.h"
32
#include "libavutil/libm.h"
33
#include "avcodec.h"
34
#include "get_bits.h"
35
#include "internal.h"
36
#include "mathops.h"
37
#include "mpegaudiodsp.h"
38
39
/*
40
 * TODO:
41
 *  - test lsf / mpeg25 extensively.
42
 */
43
44
#include "mpegaudio.h"
45
#include "mpegaudiodecheader.h"
46
47
#define BACKSTEP_SIZE 512
48
#define EXTRABYTES 24
49
#define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
50
51
/* layer 3 "granule" */
52
typedef struct GranuleDef {
53
    uint8_t scfsi;
54
    int part2_3_length;
55
    int big_values;
56
    int global_gain;
57
    int scalefac_compress;
58
    uint8_t block_type;
59
    uint8_t switch_point;
60
    int table_select[3];
61
    int subblock_gain[3];
62
    uint8_t scalefac_scale;
63
    uint8_t count1table_select;
64
    int region_size[3]; /* number of huffman codes in each region */
65
    int preflag;
66
    int short_start, long_end; /* long/short band indexes */
67
    uint8_t scale_factors[40];
68
    DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
69
} GranuleDef;
70
71
typedef struct MPADecodeContext {
72
    MPA_DECODE_HEADER
73
    uint8_t last_buf[LAST_BUF_SIZE];
74
    int last_buf_size;
75
    int extrasize;
76
    /* next header (used in free format parsing) */
77
    uint32_t free_format_next_header;
78
    GetBitContext gb;
79
    GetBitContext in_gb;
80
    DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
81
    int synth_buf_offset[MPA_MAX_CHANNELS];
82
    DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
83
    INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
84
    GranuleDef granules[2][2]; /* Used in Layer 3 */
85
    int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
86
    int dither_state;
87
    int err_recognition;
88
    AVCodecContext* avctx;
89
    MPADSPContext mpadsp;
90
    AVFloatDSPContext *fdsp;
91
    AVFrame *frame;
92
} MPADecodeContext;
93
94
#define HEADER_SIZE 4
95
96
#include "mpegaudiodata.h"
97
#include "mpegaudiodectab.h"
98
99
/* vlc structure for decoding layer 3 huffman tables */
100
static VLC huff_vlc[16];
101
static VLC_TYPE huff_vlc_tables[
102
    0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
103
  142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
104
  ][2];
105
static const int huff_vlc_tables_sizes[16] = {
106
    0,  128,  128,  128,  130,  128,  154,  166,
107
  142,  204,  190,  170,  542,  460,  662,  414
108
};
109
static VLC huff_quad_vlc[2];
110
static VLC_TYPE  huff_quad_vlc_tables[128+16][2];
111
static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
112
/* computed from band_size_long */
113
static uint16_t band_index_long[9][23];
114
#include "mpegaudio_tablegen.h"
115
/* intensity stereo coef table */
116
static INTFLOAT is_table[2][16];
117
static INTFLOAT is_table_lsf[2][2][16];
118
static INTFLOAT csa_table[8][4];
119
120
static int16_t division_tab3[1<<6 ];
121
static int16_t division_tab5[1<<8 ];
122
static int16_t division_tab9[1<<11];
123
124
static int16_t * const division_tabs[4] = {
125
    division_tab3, division_tab5, NULL, division_tab9
126
};
127
128
/* lower 2 bits: modulo 3, higher bits: shift */
129
static uint16_t scale_factor_modshift[64];
130
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
131
static int32_t scale_factor_mult[15][3];
132
/* mult table for layer 2 group quantization */
133
134
#define SCALE_GEN(v) \
135
{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
136
137
static const int32_t scale_factor_mult2[3][3] = {
138
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
139
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
140
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
141
};
142
143
/**
144
 * Convert region offsets to region sizes and truncate
145
 * size to big_values.
146
 */
147
8116
static void region_offset2size(GranuleDef *g)
148
{
149
8116
    int i, k, j = 0;
150
8116
    g->region_size[2] = 576 / 2;
151
32464
    for (i = 0; i < 3; i++) {
152
24348
        k = FFMIN(g->region_size[i], g->big_values);
153
24348
        g->region_size[i] = k - j;
154
24348
        j = k;
155
    }
156
8116
}
157
158
845
static void init_short_region(MPADecodeContext *s, GranuleDef *g)
159
{
160
845
    if (g->block_type == 2) {
161
305
        if (s->sample_rate_index != 8)
162
305
            g->region_size[0] = (36 / 2);
163
        else
164
            g->region_size[0] = (72 / 2);
165
    } else {
166
540
        if (s->sample_rate_index <= 2)
167
539
            g->region_size[0] = (36 / 2);
168
1
        else if (s->sample_rate_index != 8)
169
1
            g->region_size[0] = (54 / 2);
170
        else
171
            g->region_size[0] = (108 / 2);
172
    }
173
845
    g->region_size[1] = (576 / 2);
174
845
}
175
176
7271
static void init_long_region(MPADecodeContext *s, GranuleDef *g,
177
                             int ra1, int ra2)
178
{
179
    int l;
180
7271
    g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
181
    /* should not overflow */
182
7271
    l = FFMIN(ra1 + ra2 + 2, 22);
183
7271
    g->region_size[1] = band_index_long[s->sample_rate_index][      l] >> 1;
184
7271
}
185
186
8116
static void compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
187
{
188
8116
    if (g->block_type == 2) {
189
305
        if (g->switch_point) {
190
13
            if(s->sample_rate_index == 8)
191
                avpriv_request_sample(s->avctx, "switch point in 8khz");
192
            /* if switched mode, we handle the 36 first samples as
193
                long blocks.  For 8000Hz, we handle the 72 first
194
                exponents as long blocks */
195
13
            if (s->sample_rate_index <= 2)
196
13
                g->long_end = 8;
197
            else
198
                g->long_end = 6;
199
200
13
            g->short_start = 3;
201
        } else {
202
292
            g->long_end    = 0;
203
292
            g->short_start = 0;
204
        }
205
    } else {
206
7811
        g->short_start = 13;
207
7811
        g->long_end    = 22;
208
    }
209
8116
}
210
211
/* layer 1 unscaling */
212
/* n = number of bits of the mantissa minus 1 */
213
5010012
static inline int l1_unscale(int n, int mant, int scale_factor)
214
{
215
    int shift, mod;
216
    int64_t val;
217
218
5010012
    shift   = scale_factor_modshift[scale_factor];
219
5010012
    mod     = shift & 3;
220
5010012
    shift >>= 2;
221
5010012
    val     = MUL64((int)(mant + (-1U << n) + 1), scale_factor_mult[n-1][mod]);
222
5010012
    shift  += n;
223
    /* NOTE: at this point, 1 <= shift >= 21 + 15 */
224
5010012
    return (int)((val + (1LL << (shift - 1))) >> shift);
225
}
226
227
882756
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
228
{
229
    int shift, mod, val;
230
231
882756
    shift   = scale_factor_modshift[scale_factor];
232
882756
    mod     = shift & 3;
233
882756
    shift >>= 2;
234
235
882756
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
236
    /* NOTE: at this point, 0 <= shift <= 21 */
237
882756
    if (shift > 0)
238
882756
        val = (val + (1 << (shift - 1))) >> shift;
239
882756
    return val;
240
}
241
242
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
243
56735
static inline int l3_unscale(int value, int exponent)
244
{
245
    unsigned int m;
246
    int e;
247
248
56735
    e  = table_4_3_exp  [4 * value + (exponent & 3)];
249
56735
    m  = table_4_3_value[4 * value + (exponent & 3)];
250
56735
    e -= exponent >> 2;
251
#ifdef DEBUG
252
    if(e < 1)
253
        av_log(NULL, AV_LOG_WARNING, "l3_unscale: e is %d\n", e);
254
#endif
255
56735
    if (e > (SUINT)31)
256
10
        return 0;
257
56725
    m = (m + ((1U << e)>>1)) >> e;
258
259
56725
    return m;
260
}
261
262
89
static av_cold void decode_init_static(void)
263
{
264
    int i, j, k;
265
    int offset;
266
267
    /* scale factors table for layer 1/2 */
268
5785
    for (i = 0; i < 64; i++) {
269
        int shift, mod;
270
        /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
271
5696
        shift = i / 3;
272
5696
        mod   = i % 3;
273
5696
        scale_factor_modshift[i] = mod | (shift << 2);
274
    }
275
276
    /* scale factor multiply for layer 1 */
277
1424
    for (i = 0; i < 15; i++) {
278
        int n, norm;
279
1335
        n = i + 2;
280
1335
        norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
281
1335
        scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0          * 2.0), FRAC_BITS);
282
1335
        scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
283
1335
        scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
284
        ff_dlog(NULL, "%d: norm=%x s=%"PRIx32" %"PRIx32" %"PRIx32"\n", i,
285
                (unsigned)norm,
286
                scale_factor_mult[i][0],
287
                scale_factor_mult[i][1],
288
                scale_factor_mult[i][2]);
289
    }
290
291
89
    RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
292
293
    /* huffman decode tables */
294
89
    offset = 0;
295
1424
    for (i = 1; i < 16; i++) {
296
1335
        const HuffTable *h = &mpa_huff_tables[i];
297
        int xsize, x, y;
298
1335
        uint8_t  tmp_bits [512] = { 0 };
299
1335
        uint16_t tmp_codes[512] = { 0 };
300
301
1335
        xsize = h->xsize;
302
303
1335
        j = 0;
304
12193
        for (x = 0; x < xsize; x++) {
305
133500
            for (y = 0; y < xsize; y++) {
306

122642
                tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
307

122642
                tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
308
            }
309
        }
310
311
        /* XXX: fail test */
312
1335
        huff_vlc[i].table = huff_vlc_tables+offset;
313
1335
        huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
314
1335
        init_vlc(&huff_vlc[i], 7, 512,
315
                 tmp_bits, 1, 1, tmp_codes, 2, 2,
316
                 INIT_VLC_USE_NEW_STATIC);
317
1335
        offset += huff_vlc_tables_sizes[i];
318
    }
319
89
    av_assert0(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
320
321
89
    offset = 0;
322
267
    for (i = 0; i < 2; i++) {
323
178
        huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
324
178
        huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
325
178
        init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
326
                 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
327
                 INIT_VLC_USE_NEW_STATIC);
328
178
        offset += huff_quad_vlc_tables_sizes[i];
329
    }
330
89
    av_assert0(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
331
332
890
    for (i = 0; i < 9; i++) {
333
801
        k = 0;
334
18423
        for (j = 0; j < 22; j++) {
335
17622
            band_index_long[i][j] = k;
336
17622
            k += band_size_long[i][j];
337
        }
338
801
        band_index_long[i][22] = k;
339
    }
340
341
    /* compute n ^ (4/3) and store it in mantissa/exp format */
342
343
89
    mpegaudio_tableinit();
344
345
445
    for (i = 0; i < 4; i++) {
346
356
        if (ff_mpa_quant_bits[i] < 0) {
347
211019
            for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
348
                int val1, val2, val3, steps;
349
210752
                int val = j;
350
210752
                steps   = ff_mpa_quant_steps[i];
351
210752
                val1    = val % steps;
352
210752
                val    /= steps;
353
210752
                val2    = val % steps;
354
210752
                val3    = val / steps;
355
210752
                division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
356
            }
357
        }
358
    }
359
360
361
712
    for (i = 0; i < 7; i++) {
362
        float f;
363
        INTFLOAT v;
364
623
        if (i != 6) {
365
534
            f = tan((double)i * M_PI / 12.0);
366
534
            v = FIXR(f / (1.0 + f));
367
        } else {
368
89
            v = FIXR(1.0);
369
        }
370
623
        is_table[0][    i] = v;
371
623
        is_table[1][6 - i] = v;
372
    }
373
    /* invalid values */
374
890
    for (i = 7; i < 16; i++)
375
801
        is_table[0][i] = is_table[1][i] = 0.0;
376
377
1513
    for (i = 0; i < 16; i++) {
378
        double f;
379
        int e, k;
380
381
4272
        for (j = 0; j < 2; j++) {
382
2848
            e = -(j + 1) * ((i + 1) >> 1);
383
2848
            f = exp2(e / 4.0);
384
2848
            k = i & 1;
385
2848
            is_table_lsf[j][k ^ 1][i] = FIXR(f);
386
2848
            is_table_lsf[j][k    ][i] = FIXR(1.0);
387
            ff_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
388
                    i, j, (float) is_table_lsf[j][0][i],
389
                    (float) is_table_lsf[j][1][i]);
390
        }
391
    }
392
393
801
    for (i = 0; i < 8; i++) {
394
        double ci, cs, ca;
395
712
        ci = ci_table[i];
396
712
        cs = 1.0 / sqrt(1.0 + ci * ci);
397
712
        ca = cs * ci;
398
#if !USE_FLOATS
399
352
        csa_table[i][0] = FIXHR(cs/4);
400
352
        csa_table[i][1] = FIXHR(ca/4);
401
352
        csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
402
352
        csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
403
#else
404
360
        csa_table[i][0] = cs;
405
360
        csa_table[i][1] = ca;
406
360
        csa_table[i][2] = ca + cs;
407
360
        csa_table[i][3] = ca - cs;
408
#endif
409
    }
410
89
}
411
412
#if USE_FLOATS
413
62
static av_cold int decode_close(AVCodecContext * avctx)
414
{
415
62
    MPADecodeContext *s = avctx->priv_data;
416
62
    av_freep(&s->fdsp);
417
418
62
    return 0;
419
}
420
#endif
421
422
131
static av_cold int decode_init(AVCodecContext * avctx)
423
{
424
    static int initialized_tables = 0;
425
131
    MPADecodeContext *s = avctx->priv_data;
426
427
131
    if (!initialized_tables) {
428
89
        decode_init_static();
429
89
        initialized_tables = 1;
430
    }
431
432
131
    s->avctx = avctx;
433
434
#if USE_FLOATS
435
62
    s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
436
62
    if (!s->fdsp)
437
        return AVERROR(ENOMEM);
438
#endif
439
440
131
    ff_mpadsp_init(&s->mpadsp);
441
442
131
    if (avctx->request_sample_fmt == OUT_FMT &&
443
        avctx->codec_id != AV_CODEC_ID_MP3ON4)
444
        avctx->sample_fmt = OUT_FMT;
445
    else
446
131
        avctx->sample_fmt = OUT_FMT_P;
447
131
    s->err_recognition = avctx->err_recognition;
448
449
131
    if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
450
        s->adu_mode = 1;
451
452
131
    return 0;
453
}
454
455
#define C3 FIXHR(0.86602540378443864676/2)
456
#define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
457
#define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
458
#define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
459
460
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
461
   cases. */
462
19371
static void imdct12(INTFLOAT *out, SUINTFLOAT *in)
463
{
464
    SUINTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
465
466
19371
    in0  = in[0*3];
467
19371
    in1  = in[1*3] + in[0*3];
468
19371
    in2  = in[2*3] + in[1*3];
469
19371
    in3  = in[3*3] + in[2*3];
470
19371
    in4  = in[4*3] + in[3*3];
471
19371
    in5  = in[5*3] + in[4*3];
472
19371
    in5 += in3;
473
19371
    in3 += in1;
474
475
19371
    in2  = MULH3(in2, C3, 2);
476
19371
    in3  = MULH3(in3, C3, 4);
477
478
19371
    t1   = in0 - in4;
479
19371
    t2   = MULH3(in1 - in5, C4, 2);
480
481
19371
    out[ 7] =
482
19371
    out[10] = t1 + t2;
483
19371
    out[ 1] =
484
19371
    out[ 4] = t1 - t2;
485
486
19371
    in0    += SHR(in4, 1);
487
19371
    in4     = in0 + in2;
488
19371
    in5    += 2*in1;
489
19371
    in1     = MULH3(in5 + in3, C5, 1);
490
19371
    out[ 8] =
491
19371
    out[ 9] = in4 + in1;
492
19371
    out[ 2] =
493
19371
    out[ 3] = in4 - in1;
494
495
19371
    in0    -= in2;
496
19371
    in5     = MULH3(in5 - in3, C6, 2);
497
19371
    out[ 0] =
498
19371
    out[ 5] = in0 - in5;
499
19371
    out[ 6] =
500
19371
    out[11] = in0 + in5;
501
19371
}
502
503
/* return the number of decoded frames */
504
static int mp_decode_layer1(MPADecodeContext *s)
505
{
506
    int bound, i, v, n, ch, j, mant;
507
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
508
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
509
510
    if (s->mode == MPA_JSTEREO)
511
        bound = (s->mode_ext + 1) * 4;
512
    else
513
        bound = SBLIMIT;
514
515
    /* allocation bits */
516
    for (i = 0; i < bound; i++) {
517
        for (ch = 0; ch < s->nb_channels; ch++) {
518
            allocation[ch][i] = get_bits(&s->gb, 4);
519
        }
520
    }
521
    for (i = bound; i < SBLIMIT; i++)
522
        allocation[0][i] = get_bits(&s->gb, 4);
523
524
    /* scale factors */
525
    for (i = 0; i < bound; i++) {
526
        for (ch = 0; ch < s->nb_channels; ch++) {
527
            if (allocation[ch][i])
528
                scale_factors[ch][i] = get_bits(&s->gb, 6);
529
        }
530
    }
531
    for (i = bound; i < SBLIMIT; i++) {
532
        if (allocation[0][i]) {
533
            scale_factors[0][i] = get_bits(&s->gb, 6);
534
            scale_factors[1][i] = get_bits(&s->gb, 6);
535
        }
536
    }
537
538
    /* compute samples */
539
    for (j = 0; j < 12; j++) {
540
        for (i = 0; i < bound; i++) {
541
            for (ch = 0; ch < s->nb_channels; ch++) {
542
                n = allocation[ch][i];
543
                if (n) {
544
                    mant = get_bits(&s->gb, n + 1);
545
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
546
                } else {
547
                    v = 0;
548
                }
549
                s->sb_samples[ch][j][i] = v;
550
            }
551
        }
552
        for (i = bound; i < SBLIMIT; i++) {
553
            n = allocation[0][i];
554
            if (n) {
555
                mant = get_bits(&s->gb, n + 1);
556
                v = l1_unscale(n, mant, scale_factors[0][i]);
557
                s->sb_samples[0][j][i] = v;
558
                v = l1_unscale(n, mant, scale_factors[1][i]);
559
                s->sb_samples[1][j][i] = v;
560
            } else {
561
                s->sb_samples[0][j][i] = 0;
562
                s->sb_samples[1][j][i] = 0;
563
            }
564
        }
565
    }
566
    return 12;
567
}
568
569
7237
static int mp_decode_layer2(MPADecodeContext *s)
570
{
571
    int sblimit; /* number of used subbands */
572
    const unsigned char *alloc_table;
573
    int table, bit_alloc_bits, i, j, ch, bound, v;
574
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
575
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
576
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
577
    int scale, qindex, bits, steps, k, l, m, b;
578
579
    /* select decoding table */
580
7237
    table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
581
                                   s->sample_rate, s->lsf);
582
7237
    sblimit     = ff_mpa_sblimit_table[table];
583
7237
    alloc_table = ff_mpa_alloc_tables[table];
584
585
7237
    if (s->mode == MPA_JSTEREO)
586
        bound = (s->mode_ext + 1) * 4;
587
    else
588
7237
        bound = sblimit;
589
590
    ff_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
591
592
    /* sanity check */
593
7237
    if (bound > sblimit)
594
        bound = sblimit;
595
596
    /* parse bit allocation */
597
7237
    j = 0;
598
221869
    for (i = 0; i < bound; i++) {
599
214632
        bit_alloc_bits = alloc_table[j];
600
447402
        for (ch = 0; ch < s->nb_channels; ch++)
601
232770
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
602
214632
        j += 1 << bit_alloc_bits;
603
    }
604
7237
    for (i = bound; i < sblimit; i++) {
605
        bit_alloc_bits = alloc_table[j];
606
        v = get_bits(&s->gb, bit_alloc_bits);
607
        bit_alloc[0][i] = v;
608
        bit_alloc[1][i] = v;
609
        j += 1 << bit_alloc_bits;
610
    }
611
612
    /* scale codes */
613
221869
    for (i = 0; i < sblimit; i++) {
614
447402
        for (ch = 0; ch < s->nb_channels; ch++) {
615
232770
            if (bit_alloc[ch][i])
616
163688
                scale_code[ch][i] = get_bits(&s->gb, 2);
617
        }
618
    }
619
620
    /* scale factors */
621
221869
    for (i = 0; i < sblimit; i++) {
622
447402
        for (ch = 0; ch < s->nb_channels; ch++) {
623
232770
            if (bit_alloc[ch][i]) {
624
163688
                sf = scale_factors[ch][i];
625

163688
                switch (scale_code[ch][i]) {
626
2929
                default:
627
                case 0:
628
2929
                    sf[0] = get_bits(&s->gb, 6);
629
2929
                    sf[1] = get_bits(&s->gb, 6);
630
2929
                    sf[2] = get_bits(&s->gb, 6);
631
2929
                    break;
632
153192
                case 2:
633
153192
                    sf[0] = get_bits(&s->gb, 6);
634
153192
                    sf[1] = sf[0];
635
153192
                    sf[2] = sf[0];
636
153192
                    break;
637
2542
                case 1:
638
2542
                    sf[0] = get_bits(&s->gb, 6);
639
2542
                    sf[2] = get_bits(&s->gb, 6);
640
2542
                    sf[1] = sf[0];
641
2542
                    break;
642
5025
                case 3:
643
5025
                    sf[0] = get_bits(&s->gb, 6);
644
5025
                    sf[2] = get_bits(&s->gb, 6);
645
5025
                    sf[1] = sf[2];
646
5025
                    break;
647
                }
648
69082
            }
649
        }
650
    }
651
652
    /* samples */
653
28948
    for (k = 0; k < 3; k++) {
654
108555
        for (l = 0; l < 12; l += 3) {
655
86844
            j = 0;
656
2662428
            for (i = 0; i < bound; i++) {
657
2575584
                bit_alloc_bits = alloc_table[j];
658
5368824
                for (ch = 0; ch < s->nb_channels; ch++) {
659
2793240
                    b = bit_alloc[ch][i];
660
2793240
                    if (b) {
661
1964256
                        scale = scale_factors[ch][i][k];
662
1964256
                        qindex = alloc_table[j+b];
663
1964256
                        bits = ff_mpa_quant_bits[qindex];
664
1964256
                        if (bits < 0) {
665
                            int v2;
666
                            /* 3 values at the same time */
667
294252
                            v = get_bits(&s->gb, -bits);
668
294252
                            v2 = division_tabs[qindex][v];
669
294252
                            steps  = ff_mpa_quant_steps[qindex];
670
671
586416
                            s->sb_samples[ch][k * 12 + l + 0][i] =
672
294252
                                l2_unscale_group(steps,  v2       & 15, scale);
673
586416
                            s->sb_samples[ch][k * 12 + l + 1][i] =
674
294252
                                l2_unscale_group(steps, (v2 >> 4) & 15, scale);
675
294252
                            s->sb_samples[ch][k * 12 + l + 2][i] =
676
294252
                                l2_unscale_group(steps,  v2 >> 8      , scale);
677
                        } else {
678
6680016
                            for (m = 0; m < 3; m++) {
679
5010012
                                v = get_bits(&s->gb, bits);
680
5010012
                                v = l1_unscale(bits - 1, v, scale);
681
5010012
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
682
                            }
683
                        }
684
                    } else {
685
828984
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
686
828984
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
687
828984
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
688
                    }
689
                }
690
                /* next subband in alloc table */
691
2575584
                j += 1 << bit_alloc_bits;
692
            }
693
            /* XXX: find a way to avoid this duplication of code */
694
86844
            for (i = bound; i < sblimit; i++) {
695
                bit_alloc_bits = alloc_table[j];
696
                b = bit_alloc[0][i];
697
                if (b) {
698
                    int mant, scale0, scale1;
699
                    scale0 = scale_factors[0][i][k];
700
                    scale1 = scale_factors[1][i][k];
701
                    qindex = alloc_table[j+b];
702
                    bits = ff_mpa_quant_bits[qindex];
703
                    if (bits < 0) {
704
                        /* 3 values at the same time */
705
                        v = get_bits(&s->gb, -bits);
706
                        steps = ff_mpa_quant_steps[qindex];
707
                        mant = v % steps;
708
                        v = v / steps;
709
                        s->sb_samples[0][k * 12 + l + 0][i] =
710
                            l2_unscale_group(steps, mant, scale0);
711
                        s->sb_samples[1][k * 12 + l + 0][i] =
712
                            l2_unscale_group(steps, mant, scale1);
713
                        mant = v % steps;
714
                        v = v / steps;
715
                        s->sb_samples[0][k * 12 + l + 1][i] =
716
                            l2_unscale_group(steps, mant, scale0);
717
                        s->sb_samples[1][k * 12 + l + 1][i] =
718
                            l2_unscale_group(steps, mant, scale1);
719
                        s->sb_samples[0][k * 12 + l + 2][i] =
720
                            l2_unscale_group(steps, v, scale0);
721
                        s->sb_samples[1][k * 12 + l + 2][i] =
722
                            l2_unscale_group(steps, v, scale1);
723
                    } else {
724
                        for (m = 0; m < 3; m++) {
725
                            mant = get_bits(&s->gb, bits);
726
                            s->sb_samples[0][k * 12 + l + m][i] =
727
                                l1_unscale(bits - 1, mant, scale0);
728
                            s->sb_samples[1][k * 12 + l + m][i] =
729
                                l1_unscale(bits - 1, mant, scale1);
730
                        }
731
                    }
732
                } else {
733
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
734
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
735
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
736
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
737
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
738
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
739
                }
740
                /* next subband in alloc table */
741
                j += 1 << bit_alloc_bits;
742
            }
743
            /* fill remaining samples to zero */
744
290268
            for (i = sblimit; i < SBLIMIT; i++) {
745
437256
                for (ch = 0; ch < s->nb_channels; ch++) {
746
233832
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
747
233832
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
748
233832
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
749
                }
750
            }
751
        }
752
    }
753
7237
    return 3 * 12;
754
}
755
756
#define SPLIT(dst,sf,n)             \
757
    if (n == 3) {                   \
758
        int m = (sf * 171) >> 9;    \
759
        dst   = sf - 3 * m;         \
760
        sf    = m;                  \
761
    } else if (n == 4) {            \
762
        dst  = sf & 3;              \
763
        sf >>= 2;                   \
764
    } else if (n == 5) {            \
765
        int m = (sf * 205) >> 10;   \
766
        dst   = sf - 5 * m;         \
767
        sf    = m;                  \
768
    } else if (n == 6) {            \
769
        int m = (sf * 171) >> 10;   \
770
        dst   = sf - 6 * m;         \
771
        sf    = m;                  \
772
    } else {                        \
773
        dst = 0;                    \
774
    }
775
776
4
static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
777
                                           int n3)
778
{
779


4
    SPLIT(slen[3], sf, n3)
780


4
    SPLIT(slen[2], sf, n2)
781


4
    SPLIT(slen[1], sf, n1)
782
4
    slen[0] = sf;
783
4
}
784
785
8112
static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
786
                                         int16_t *exponents)
787
{
788
    const uint8_t *bstab, *pretab;
789
    int len, i, j, k, l, v0, shift, gain, gains[3];
790
    int16_t *exp_ptr;
791
792
8112
    exp_ptr = exponents;
793
8112
    gain    = g->global_gain - 210;
794
8112
    shift   = g->scalefac_scale + 1;
795
796
8112
    bstab  = band_size_long[s->sample_rate_index];
797
8112
    pretab = mpa_pretab[g->preflag];
798
179992
    for (i = 0; i < g->long_end; i++) {
799
171880
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
800
171880
        len = bstab[i];
801
4669756
        for (j = len; j > 0; j--)
802
4497876
            *exp_ptr++ = v0;
803
    }
804
805
8112
    if (g->short_start < 13) {
806
304
        bstab    = band_size_short[s->sample_rate_index];
807
304
        gains[0] = gain - (g->subblock_gain[0] << 3);
808
304
        gains[1] = gain - (g->subblock_gain[1] << 3);
809
304
        gains[2] = gain - (g->subblock_gain[2] << 3);
810
304
        k        = g->long_end;
811
4217
        for (i = g->short_start; i < 13; i++) {
812
3913
            len = bstab[i];
813
15652
            for (l = 0; l < 3; l++) {
814
11739
                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
815
186375
                for (j = len; j > 0; j--)
816
174636
                    *exp_ptr++ = v0;
817
            }
818
        }
819
    }
820
8112
}
821
822
16799
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
823
                          int *end_pos2)
824
{
825

16799
    if (s->in_gb.buffer && *pos >= s->gb.size_in_bits - s->extrasize * 8) {
826
939
        s->gb           = s->in_gb;
827
939
        s->in_gb.buffer = NULL;
828
939
        s->extrasize    = 0;
829
        av_assert2((get_bits_count(&s->gb) & 7) == 0);
830
939
        skip_bits_long(&s->gb, *pos - *end_pos);
831
939
        *end_pos2 =
832
939
        *end_pos  = *end_pos2 + get_bits_count(&s->gb) - *pos;
833
939
        *pos      = get_bits_count(&s->gb);
834
    }
835
16799
}
836
837
/* Following is an optimized code for
838
            INTFLOAT v = *src
839
            if(get_bits1(&s->gb))
840
                v = -v;
841
            *dst = v;
842
*/
843
#if USE_FLOATS
844
#define READ_FLIP_SIGN(dst,src)                     \
845
    v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31);  \
846
    AV_WN32A(dst, v);
847
#else
848
#define READ_FLIP_SIGN(dst,src)     \
849
    v      = -get_bits1(&s->gb);    \
850
    *(dst) = (*(src) ^ v) - v;
851
#endif
852
853
8112
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
854
                          int16_t *exponents, int end_pos2)
855
{
856
    int s_index;
857
    int i;
858
    int last_pos, bits_left;
859
    VLC *vlc;
860
8112
    int end_pos = FFMIN(end_pos2, s->gb.size_in_bits - s->extrasize * 8);
861
862
    /* low frequencies (called big values) */
863
8112
    s_index = 0;
864
32448
    for (i = 0; i < 3; i++) {
865
        int j, k, l, linbits;
866
24336
        j = g->region_size[i];
867
24336
        if (j == 0)
868
1292
            continue;
869
        /* select vlc table */
870
23044
        k       = g->table_select[i];
871
23044
        l       = mpa_huff_data[k][0];
872
23044
        linbits = mpa_huff_data[k][1];
873
23044
        vlc     = &huff_vlc[l];
874
875
23044
        if (!l) {
876
92
            memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
877
92
            s_index += 2 * j;
878
92
            continue;
879
        }
880
881
        /* read huffcode and compute each couple */
882
909472
        for (; j > 0; j--) {
883
            int exponent, x, y;
884
            int v;
885
886520
            int pos = get_bits_count(&s->gb);
886
887
886520
            if (pos >= end_pos){
888
789
                switch_buffer(s, &pos, &end_pos, &end_pos2);
889
789
                if (pos >= end_pos)
890
                    break;
891
            }
892
886520
            y = get_vlc2(&s->gb, vlc->table, 7, 3);
893
894
886520
            if (!y) {
895
215662
                g->sb_hybrid[s_index  ] =
896
215662
                g->sb_hybrid[s_index+1] = 0;
897
215662
                s_index += 2;
898
215662
                continue;
899
            }
900
901
670858
            exponent= exponents[s_index];
902
903
            ff_dlog(s->avctx, "region=%d n=%d y=%d exp=%d\n",
904
                    i, g->region_size[i] - j, y, exponent);
905
670858
            if (y & 16) {
906
426226
                x = y >> 5;
907
426226
                y = y & 0x0f;
908
426226
                if (x < 15) {
909
400278
                    READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
910
                } else {
911
25948
                    x += get_bitsz(&s->gb, linbits);
912
25948
                    v  = l3_unscale(x, exponent);
913
25948
                    if (get_bits1(&s->gb))
914
12440
                        v = -v;
915
25948
                    g->sb_hybrid[s_index] = v;
916
                }
917
426226
                if (y < 15) {
918
401244
                    READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
919
                } else {
920
24982
                    y += get_bitsz(&s->gb, linbits);
921
24982
                    v  = l3_unscale(y, exponent);
922
24982
                    if (get_bits1(&s->gb))
923
11889
                        v = -v;
924
24982
                    g->sb_hybrid[s_index+1] = v;
925
                }
926
            } else {
927
244632
                x = y >> 5;
928
244632
                y = y & 0x0f;
929
244632
                x += y;
930
244632
                if (x < 15) {
931
238827
                    READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
932
                } else {
933
5805
                    x += get_bitsz(&s->gb, linbits);
934
5805
                    v  = l3_unscale(x, exponent);
935
5805
                    if (get_bits1(&s->gb))
936
2114
                        v = -v;
937
5805
                    g->sb_hybrid[s_index+!!y] = v;
938
                }
939
244632
                g->sb_hybrid[s_index + !y] = 0;
940
            }
941
670858
            s_index += 2;
942
        }
943
    }
944
945
    /* high frequencies */
946
8112
    vlc = &huff_quad_vlc[g->count1table_select];
947
8112
    last_pos = 0;
948
226739
    while (s_index <= 572) {
949
        int pos, code;
950
226438
        pos = get_bits_count(&s->gb);
951
226438
        if (pos >= end_pos) {
952

7898
            if (pos > end_pos2 && last_pos) {
953
                /* some encoders generate an incorrect size for this
954
                   part. We must go back into the data */
955
                s_index -= 4;
956
                skip_bits_long(&s->gb, last_pos - pos);
957
                av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
958
                if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
959
                    s_index=0;
960
7811
                break;
961
            }
962
7898
            switch_buffer(s, &pos, &end_pos, &end_pos2);
963
7898
            if (pos >= end_pos)
964
7811
                break;
965
        }
966
218627
        last_pos = pos;
967
968
218627
        code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
969
        ff_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
970
218627
        g->sb_hybrid[s_index+0] =
971
218627
        g->sb_hybrid[s_index+1] =
972
218627
        g->sb_hybrid[s_index+2] =
973
218627
        g->sb_hybrid[s_index+3] = 0;
974
366945
        while (code) {
975
            static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
976
            int v;
977
148318
            int pos = s_index + idxtab[code];
978
148318
            code   ^= 8 >> idxtab[code];
979
148318
            READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
980
        }
981
218627
        s_index += 4;
982
    }
983
    /* skip extension bits */
984
8112
    bits_left = end_pos2 - get_bits_count(&s->gb);
985

8112
    if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
986
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
987
        s_index=0;
988

8112
    } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
989
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
990
        s_index = 0;
991
    }
992
8112
    memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
993
8112
    skip_bits_long(&s->gb, bits_left);
994
995
8112
    i = get_bits_count(&s->gb);
996
8112
    switch_buffer(s, &i, &end_pos, &end_pos2);
997
998
8112
    return 0;
999
}
1000
1001
/* Reorder short blocks from bitstream order to interleaved order. It
1002
   would be faster to do it in parsing, but the code would be far more
1003
   complicated */
1004
8112
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1005
{
1006
    int i, j, len;
1007
    INTFLOAT *ptr, *dst, *ptr1;
1008
    INTFLOAT tmp[576];
1009
1010
8112
    if (g->block_type != 2)
1011
7808
        return;
1012
1013
304
    if (g->switch_point) {
1014
13
        if (s->sample_rate_index != 8)
1015
13
            ptr = g->sb_hybrid + 36;
1016
        else
1017
            ptr = g->sb_hybrid + 72;
1018
    } else {
1019
291
        ptr = g->sb_hybrid;
1020
    }
1021
1022
4217
    for (i = g->short_start; i < 13; i++) {
1023
3913
        len  = band_size_short[s->sample_rate_index][i];
1024
3913
        ptr1 = ptr;
1025
3913
        dst  = tmp;
1026
62125
        for (j = len; j > 0; j--) {
1027
58212
            *dst++ = ptr[0*len];
1028
58212
            *dst++ = ptr[1*len];
1029
58212
            *dst++ = ptr[2*len];
1030
58212
            ptr++;
1031
        }
1032
3913
        ptr += 2 * len;
1033
3913
        memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1034
    }
1035
}
1036
1037
#define ISQRT2 FIXR(0.70710678118654752440)
1038
1039
2447
static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1040
{
1041
    int i, j, k, l;
1042
    int sf_max, sf, len, non_zero_found;
1043
    INTFLOAT (*is_tab)[16], *tab0, *tab1, v1, v2;
1044
    SUINTFLOAT tmp0, tmp1;
1045
    int non_zero_found_short[3];
1046
1047
    /* intensity stereo */
1048
2447
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1049
1
        if (!s->lsf) {
1050
            is_tab = is_table;
1051
            sf_max = 7;
1052
        } else {
1053
1
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1054
1
            sf_max = 16;
1055
        }
1056
1057
1
        tab0 = g0->sb_hybrid + 576;
1058
1
        tab1 = g1->sb_hybrid + 576;
1059
1060
1
        non_zero_found_short[0] = 0;
1061
1
        non_zero_found_short[1] = 0;
1062
1
        non_zero_found_short[2] = 0;
1063
1
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1064
1
        for (i = 12; i >= g1->short_start; i--) {
1065
            /* for last band, use previous scale factor */
1066
            if (i != 11)
1067
                k -= 3;
1068
            len = band_size_short[s->sample_rate_index][i];
1069
            for (l = 2; l >= 0; l--) {
1070
                tab0 -= len;
1071
                tab1 -= len;
1072
                if (!non_zero_found_short[l]) {
1073
                    /* test if non zero band. if so, stop doing i-stereo */
1074
                    for (j = 0; j < len; j++) {
1075
                        if (tab1[j] != 0) {
1076
                            non_zero_found_short[l] = 1;
1077
                            goto found1;
1078
                        }
1079
                    }
1080
                    sf = g1->scale_factors[k + l];
1081
                    if (sf >= sf_max)
1082
                        goto found1;
1083
1084
                    v1 = is_tab[0][sf];
1085
                    v2 = is_tab[1][sf];
1086
                    for (j = 0; j < len; j++) {
1087
                        tmp0    = tab0[j];
1088
                        tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1089
                        tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1090
                    }
1091
                } else {
1092
found1:
1093
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1094
                        /* lower part of the spectrum : do ms stereo
1095
                           if enabled */
1096
                        for (j = 0; j < len; j++) {
1097
                            tmp0    = tab0[j];
1098
                            tmp1    = tab1[j];
1099
                            tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1100
                            tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1101
                        }
1102
                    }
1103
                }
1104
            }
1105
        }
1106
1107
1
        non_zero_found = non_zero_found_short[0] |
1108
1
                         non_zero_found_short[1] |
1109
1
                         non_zero_found_short[2];
1110
1111
23
        for (i = g1->long_end - 1;i >= 0;i--) {
1112
22
            len   = band_size_long[s->sample_rate_index][i];
1113
22
            tab0 -= len;
1114
22
            tab1 -= len;
1115
            /* test if non zero band. if so, stop doing i-stereo */
1116
22
            if (!non_zero_found) {
1117
598
                for (j = 0; j < len; j++) {
1118
576
                    if (tab1[j] != 0) {
1119
                        non_zero_found = 1;
1120
                        goto found2;
1121
                    }
1122
                }
1123
                /* for last band, use previous scale factor */
1124
22
                k  = (i == 21) ? 20 : i;
1125
22
                sf = g1->scale_factors[k];
1126
22
                if (sf >= sf_max)
1127
                    goto found2;
1128
22
                v1 = is_tab[0][sf];
1129
22
                v2 = is_tab[1][sf];
1130
598
                for (j = 0; j < len; j++) {
1131
576
                    tmp0    = tab0[j];
1132
576
                    tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1133
576
                    tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1134
                }
1135
            } else {
1136
found2:
1137
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1138
                    /* lower part of the spectrum : do ms stereo
1139
                       if enabled */
1140
                    for (j = 0; j < len; j++) {
1141
                        tmp0    = tab0[j];
1142
                        tmp1    = tab1[j];
1143
                        tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1144
                        tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1145
                    }
1146
                }
1147
            }
1148
        }
1149
2446
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1150
        /* ms stereo ONLY */
1151
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1152
           global gain */
1153
#if USE_FLOATS
1154
52
       s->fdsp->butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1155
#else
1156
2176
        tab0 = g0->sb_hybrid;
1157
2176
        tab1 = g1->sb_hybrid;
1158
1255552
        for (i = 0; i < 576; i++) {
1159
1253376
            tmp0    = tab0[i];
1160
1253376
            tmp1    = tab1[i];
1161
1253376
            tab0[i] = tmp0 + tmp1;
1162
1253376
            tab1[i] = tmp0 - tmp1;
1163
        }
1164
#endif
1165
    }
1166
2447
}
1167
1168
#if USE_FLOATS
1169
#if HAVE_MIPSFPU
1170
#   include "mips/compute_antialias_float.h"
1171
#endif /* HAVE_MIPSFPU */
1172
#else
1173
#if HAVE_MIPSDSP
1174
#   include "mips/compute_antialias_fixed.h"
1175
#endif /* HAVE_MIPSDSP */
1176
#endif /* USE_FLOATS */
1177
1178
#ifndef compute_antialias
1179
#if USE_FLOATS
1180
#define AA(j) do {                                                      \
1181
        float tmp0 = ptr[-1-j];                                         \
1182
        float tmp1 = ptr[   j];                                         \
1183
        ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1];    \
1184
        ptr[   j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0];    \
1185
    } while (0)
1186
#else
1187
#define AA(j) do {                                              \
1188
        SUINT tmp0 = ptr[-1-j];                                   \
1189
        SUINT tmp1 = ptr[   j];                                   \
1190
        SUINT tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]);          \
1191
        ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2]));   \
1192
        ptr[   j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3]));   \
1193
    } while (0)
1194
#endif
1195
1196
8112
static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1197
{
1198
    INTFLOAT *ptr;
1199
    int n, i;
1200
1201
    /* we antialias only "long" bands */
1202
8112
    if (g->block_type == 2) {
1203
304
        if (!g->switch_point)
1204
291
            return;
1205
        /* XXX: check this for 8000Hz case */
1206
13
        n = 1;
1207
    } else {
1208
7808
        n = SBLIMIT - 1;
1209
    }
1210
1211
7821
    ptr = g->sb_hybrid + 18;
1212
249882
    for (i = n; i > 0; i--) {
1213
242061
        AA(0);
1214
242061
        AA(1);
1215
242061
        AA(2);
1216
242061
        AA(3);
1217
242061
        AA(4);
1218
242061
        AA(5);
1219
242061
        AA(6);
1220
242061
        AA(7);
1221
1222
242061
        ptr += 18;
1223
    }
1224
}
1225
#endif /* compute_antialias */
1226
1227
8116
static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1228
                          INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1229
{
1230
    INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1231
    INTFLOAT out2[12];
1232
    int i, j, mdct_long_end, sblimit;
1233
1234
    /* find last non zero block */
1235
8116
    ptr  = g->sb_hybrid + 576;
1236
8116
    ptr1 = g->sb_hybrid + 2 * 18;
1237
312005
    while (ptr >= ptr1) {
1238
        int32_t *p;
1239
311823
        ptr -= 6;
1240
311823
        p    = (int32_t*)ptr;
1241
311823
        if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1242
7934
            break;
1243
    }
1244
8116
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1245
1246
8116
    if (g->block_type == 2) {
1247
        /* XXX: check for 8000 Hz */
1248
305
        if (g->switch_point)
1249
13
            mdct_long_end = 2;
1250
        else
1251
292
            mdct_long_end = 0;
1252
    } else {
1253
7811
        mdct_long_end = sblimit;
1254
    }
1255
1256
8116
    s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1257
8116
                                     mdct_long_end, g->switch_point,
1258
8116
                                     g->block_type);
1259
1260
8116
    buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1261
8116
    ptr = g->sb_hybrid + 18 * mdct_long_end;
1262
1263
14573
    for (j = mdct_long_end; j < sblimit; j++) {
1264
        /* select frequency inversion */
1265
6457
        win     = RENAME(ff_mdct_win)[2 + (4  & -(j & 1))];
1266
6457
        out_ptr = sb_samples + j;
1267
1268
45199
        for (i = 0; i < 6; i++) {
1269
38742
            *out_ptr = buf[4*i];
1270
38742
            out_ptr += SBLIMIT;
1271
        }
1272
6457
        imdct12(out2, ptr + 0);
1273
45199
        for (i = 0; i < 6; i++) {
1274
38742
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[4*(i + 6*1)];
1275
38742
            buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1276
38742
            out_ptr += SBLIMIT;
1277
        }
1278
6457
        imdct12(out2, ptr + 1);
1279
45199
        for (i = 0; i < 6; i++) {
1280
38742
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[4*(i + 6*2)];
1281
38742
            buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1282
38742
            out_ptr += SBLIMIT;
1283
        }
1284
6457
        imdct12(out2, ptr + 2);
1285
45199
        for (i = 0; i < 6; i++) {
1286
38742
            buf[4*(i + 6*0)] = MULH3(out2[i    ], win[i    ], 1) + buf[4*(i + 6*0)];
1287
38742
            buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1288
38742
            buf[4*(i + 6*2)] = 0;
1289
        }
1290
6457
        ptr += 18;
1291
6457
        buf += (j&3) != 3 ? 1 : (4*18-3);
1292
    }
1293
    /* zero bands */
1294
105676
    for (j = sblimit; j < SBLIMIT; j++) {
1295
        /* overlap */
1296
97560
        out_ptr = sb_samples + j;
1297
1853640
        for (i = 0; i < 18; i++) {
1298
1756080
            *out_ptr = buf[4*i];
1299
1756080
            buf[4*i]   = 0;
1300
1756080
            out_ptr += SBLIMIT;
1301
        }
1302
97560
        buf += (j&3) != 3 ? 1 : (4*18-3);
1303
    }
1304
8116
}
1305
1306
/* main layer3 decoding function */
1307
2803
static int mp_decode_layer3(MPADecodeContext *s)
1308
{
1309
    int nb_granules, main_data_begin;
1310
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1311
    GranuleDef *g;
1312
    int16_t exponents[576]; //FIXME try INTFLOAT
1313
1314
    /* read side info */
1315
2803
    if (s->lsf) {
1316
3
        main_data_begin = get_bits(&s->gb, 8);
1317
3
        skip_bits(&s->gb, s->nb_channels);
1318
3
        nb_granules = 1;
1319
    } else {
1320
2800
        main_data_begin = get_bits(&s->gb, 9);
1321
2800
        if (s->nb_channels == 2)
1322
1256
            skip_bits(&s->gb, 3);
1323
        else
1324
1544
            skip_bits(&s->gb, 5);
1325
2800
        nb_granules = 2;
1326
6856
        for (ch = 0; ch < s->nb_channels; ch++) {
1327
4056
            s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1328
4056
            s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1329
        }
1330
    }
1331
1332
8406
    for (gr = 0; gr < nb_granules; gr++) {
1333
13719
        for (ch = 0; ch < s->nb_channels; ch++) {
1334
            ff_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1335
8116
            g = &s->granules[ch][gr];
1336
8116
            g->part2_3_length = get_bits(&s->gb, 12);
1337
8116
            g->big_values     = get_bits(&s->gb,  9);
1338
8116
            if (g->big_values > 288) {
1339
                av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1340
                return AVERROR_INVALIDDATA;
1341
            }
1342
1343
8116
            g->global_gain = get_bits(&s->gb, 8);
1344
            /* if MS stereo only is selected, we precompute the
1345
               1/sqrt(2) renormalization factor */
1346
8116
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1347
                MODE_EXT_MS_STEREO)
1348
4456
                g->global_gain -= 2;
1349
8116
            if (s->lsf)
1350
4
                g->scalefac_compress = get_bits(&s->gb, 9);
1351
            else
1352
8112
                g->scalefac_compress = get_bits(&s->gb, 4);
1353
8116
            blocksplit_flag = get_bits1(&s->gb);
1354
8116
            if (blocksplit_flag) {
1355
845
                g->block_type = get_bits(&s->gb, 2);
1356
845
                if (g->block_type == 0) {
1357
                    av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1358
                    return AVERROR_INVALIDDATA;
1359
                }
1360
845
                g->switch_point = get_bits1(&s->gb);
1361
2535
                for (i = 0; i < 2; i++)
1362
1690
                    g->table_select[i] = get_bits(&s->gb, 5);
1363
3380
                for (i = 0; i < 3; i++)
1364
2535
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
1365
845
                init_short_region(s, g);
1366
            } else {
1367
                int region_address1, region_address2;
1368
7271
                g->block_type = 0;
1369
7271
                g->switch_point = 0;
1370
29084
                for (i = 0; i < 3; i++)
1371
21813
                    g->table_select[i] = get_bits(&s->gb, 5);
1372
                /* compute huffman coded region sizes */
1373
7271
                region_address1 = get_bits(&s->gb, 4);
1374
7271
                region_address2 = get_bits(&s->gb, 3);
1375
                ff_dlog(s->avctx, "region1=%d region2=%d\n",
1376
                        region_address1, region_address2);
1377
7271
                init_long_region(s, g, region_address1, region_address2);
1378
            }
1379
8116
            region_offset2size(g);
1380
8116
            compute_band_indexes(s, g);
1381
1382
8116
            g->preflag = 0;
1383
8116
            if (!s->lsf)
1384
8112
                g->preflag = get_bits1(&s->gb);
1385
8116
            g->scalefac_scale     = get_bits1(&s->gb);
1386
8116
            g->count1table_select = get_bits1(&s->gb);
1387
            ff_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1388
                    g->block_type, g->switch_point);
1389
        }
1390
    }
1391
1392
2803
    if (!s->adu_mode) {
1393
        int skip;
1394
2803
        const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1395
2803
        s->extrasize = av_clip((get_bits_left(&s->gb) >> 3) - s->extrasize, 0,
1396
2803
                               FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
1397
        av_assert1((get_bits_count(&s->gb) & 7) == 0);
1398
        /* now we get bits from the main_data_begin offset */
1399
        ff_dlog(s->avctx, "seekback:%d, lastbuf:%d\n",
1400
                main_data_begin, s->last_buf_size);
1401
1402
2803
        memcpy(s->last_buf + s->last_buf_size, ptr, s->extrasize);
1403
2803
        s->in_gb = s->gb;
1404
2803
        init_get_bits(&s->gb, s->last_buf, (s->last_buf_size + s->extrasize) * 8);
1405
2803
        s->last_buf_size <<= 3;
1406

2807
        for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1407
8
            for (ch = 0; ch < s->nb_channels; ch++) {
1408
4
                g = &s->granules[ch][gr];
1409
4
                s->last_buf_size += g->part2_3_length;
1410
4
                memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1411
4
                compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1412
            }
1413
        }
1414
2803
        skip = s->last_buf_size - 8 * main_data_begin;
1415

2803
        if (skip >= s->gb.size_in_bits - s->extrasize * 8 && s->in_gb.buffer) {
1416
145
            skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits + s->extrasize * 8);
1417
145
            s->gb           = s->in_gb;
1418
145
            s->in_gb.buffer = NULL;
1419
145
            s->extrasize    = 0;
1420
        } else {
1421
2658
            skip_bits_long(&s->gb, skip);
1422
        }
1423
    } else {
1424
        gr = 0;
1425
        s->extrasize = 0;
1426
    }
1427
1428
8402
    for (; gr < nb_granules; gr++) {
1429
13711
        for (ch = 0; ch < s->nb_channels; ch++) {
1430
8112
            g = &s->granules[ch][gr];
1431
8112
            bits_pos = get_bits_count(&s->gb);
1432
1433
8112
            if (!s->lsf) {
1434
                uint8_t *sc;
1435
                int slen, slen1, slen2;
1436
1437
                /* MPEG-1 scale factors */
1438
8108
                slen1 = slen_table[0][g->scalefac_compress];
1439
8108
                slen2 = slen_table[1][g->scalefac_compress];
1440
                ff_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1441
8108
                if (g->block_type == 2) {
1442
304
                    n = g->switch_point ? 17 : 18;
1443
304
                    j = 0;
1444
304
                    if (slen1) {
1445
756
                        for (i = 0; i < n; i++)
1446
716
                            g->scale_factors[j++] = get_bits(&s->gb, slen1);
1447
                    } else {
1448
5007
                        for (i = 0; i < n; i++)
1449
4743
                            g->scale_factors[j++] = 0;
1450
                    }
1451
304
                    if (slen2) {
1452
4826
                        for (i = 0; i < 18; i++)
1453
4572
                            g->scale_factors[j++] = get_bits(&s->gb, slen2);
1454
1016
                        for (i = 0; i < 3; i++)
1455
762
                            g->scale_factors[j++] = 0;
1456
                    } else {
1457
1100
                        for (i = 0; i < 21; i++)
1458
1050
                            g->scale_factors[j++] = 0;
1459
                    }
1460
                } else {
1461
7804
                    sc = s->granules[ch][0].scale_factors;
1462
7804
                    j = 0;
1463
39020
                    for (k = 0; k < 4; k++) {
1464
31216
                        n = k == 0 ? 6 : 5;
1465
31216
                        if ((g->scfsi & (0x8 >> k)) == 0) {
1466
26870
                            slen = (k < 2) ? slen1 : slen2;
1467
26870
                            if (slen) {
1468
88829
                                for (i = 0; i < n; i++)
1469
74467
                                    g->scale_factors[j++] = get_bits(&s->gb, slen);
1470
                            } else {
1471
78786
                                for (i = 0; i < n; i++)
1472
66278
                                    g->scale_factors[j++] = 0;
1473
                            }
1474
                        } else {
1475
                            /* simply copy from last granule */
1476
27485
                            for (i = 0; i < n; i++) {
1477
23139
                                g->scale_factors[j] = sc[j];
1478
23139
                                j++;
1479
                            }
1480
                        }
1481
                    }
1482
7804
                    g->scale_factors[j++] = 0;
1483
                }
1484
            } else {
1485
                int tindex, tindex2, slen[4], sl, sf;
1486
1487
                /* LSF scale factors */
1488
4
                if (g->block_type == 2)
1489
                    tindex = g->switch_point ? 2 : 1;
1490
                else
1491
4
                    tindex = 0;
1492
1493
4
                sf = g->scalefac_compress;
1494

4
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1495
                    /* intensity stereo case */
1496
1
                    sf >>= 1;
1497
1
                    if (sf < 180) {
1498
1
                        lsf_sf_expand(slen, sf, 6, 6, 0);
1499
1
                        tindex2 = 3;
1500
                    } else if (sf < 244) {
1501
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1502
                        tindex2 = 4;
1503
                    } else {
1504
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1505
                        tindex2 = 5;
1506
                    }
1507
                } else {
1508
                    /* normal case */
1509
3
                    if (sf < 400) {
1510
1
                        lsf_sf_expand(slen, sf, 5, 4, 4);
1511
1
                        tindex2 = 0;
1512
2
                    } else if (sf < 500) {
1513
2
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1514
2
                        tindex2 = 1;
1515
                    } else {
1516
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1517
                        tindex2 = 2;
1518
                        g->preflag = 1;
1519
                    }
1520
                }
1521
1522
4
                j = 0;
1523
20
                for (k = 0; k < 4; k++) {
1524
16
                    n  = lsf_nsf_table[tindex2][tindex][k];
1525
16
                    sl = slen[k];
1526
16
                    if (sl) {
1527
45
                        for (i = 0; i < n; i++)
1528
39
                            g->scale_factors[j++] = get_bits(&s->gb, sl);
1529
                    } else {
1530
55
                        for (i = 0; i < n; i++)
1531
45
                            g->scale_factors[j++] = 0;
1532
                    }
1533
                }
1534
                /* XXX: should compute exact size */
1535
80
                for (; j < 40; j++)
1536
76
                    g->scale_factors[j] = 0;
1537
            }
1538
1539
8112
            exponents_from_scale_factors(s, g, exponents);
1540
1541
            /* read Huffman coded residue */
1542
8112
            huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1543
        } /* ch */
1544
1545
5599
        if (s->mode == MPA_JSTEREO)
1546
2447
            compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1547
1548
13711
        for (ch = 0; ch < s->nb_channels; ch++) {
1549
8112
            g = &s->granules[ch][gr];
1550
1551
8112
            reorder_block(s, g);
1552
8112
            compute_antialias(s, g);
1553
8112
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1554
        }
1555
    } /* gr */
1556
2803
    if (get_bits_count(&s->gb) < 0)
1557
        skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1558
2803
    return nb_granules * 18;
1559
}
1560
1561
10040
static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples,
1562
                           const uint8_t *buf, int buf_size)
1563
{
1564
    int i, nb_frames, ch, ret;
1565
    OUT_INT *samples_ptr;
1566
1567
10040
    init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1568
1569
10040
    if (s->error_protection) {
1570
117
        uint16_t crc = get_bits(&s->gb, 16);
1571
117
        if (s->err_recognition & AV_EF_CRCCHECK) {
1572
            const int sec_len = s->lsf ? ((s->nb_channels == 1) ? 9  : 17) :
1573
                                         ((s->nb_channels == 1) ? 17 : 32);
1574
            const AVCRC *crc_tab = av_crc_get_table(AV_CRC_16_ANSI);
1575
            uint32_t crc_cal = av_crc(crc_tab, UINT16_MAX, &buf[2], 2);
1576
            crc_cal = av_crc(crc_tab, crc_cal, &buf[6], sec_len);
1577
1578
            if (av_bswap16(crc) ^ crc_cal) {
1579
                av_log(s->avctx, AV_LOG_ERROR, "CRC mismatch!\n");
1580
                if (s->err_recognition & AV_EF_EXPLODE)
1581
                    return AVERROR_INVALIDDATA;
1582
            }
1583
        }
1584
    }
1585
1586

10040
    switch(s->layer) {
1587
    case 1:
1588
        s->avctx->frame_size = 384;
1589
        nb_frames = mp_decode_layer1(s);
1590
        break;
1591
7237
    case 2:
1592
7237
        s->avctx->frame_size = 1152;
1593
7237
        nb_frames = mp_decode_layer2(s);
1594
7237
        break;
1595
2803
    case 3:
1596
2803
        s->avctx->frame_size = s->lsf ? 576 : 1152;
1597
2803
    default:
1598
2803
        nb_frames = mp_decode_layer3(s);
1599
1600
2803
        s->last_buf_size=0;
1601
2803
        if (s->in_gb.buffer) {
1602
1719
            align_get_bits(&s->gb);
1603
1719
            i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
1604

1719
            if (i >= 0 && i <= BACKSTEP_SIZE) {
1605
1719
                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1606
1719
                s->last_buf_size=i;
1607
            } else
1608
                av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1609
1719
            s->gb           = s->in_gb;
1610
1719
            s->in_gb.buffer = NULL;
1611
1719
            s->extrasize    = 0;
1612
        }
1613
1614
2803
        align_get_bits(&s->gb);
1615
        av_assert1((get_bits_count(&s->gb) & 7) == 0);
1616
2803
        i = (get_bits_left(&s->gb) >> 3) - s->extrasize;
1617

2803
        if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1618
230
            if (i < 0)
1619
                av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1620
230
            i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1621
        }
1622
        av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1623
2803
        memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1624
2803
        s->last_buf_size += i;
1625
    }
1626
1627
10040
    if(nb_frames < 0)
1628
        return nb_frames;
1629
1630
    /* get output buffer */
1631
10040
    if (!samples) {
1632
10040
        av_assert0(s->frame);
1633
10040
        s->frame->nb_samples = s->avctx->frame_size;
1634
10040
        if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0)
1635
            return ret;
1636
10040
        samples = (OUT_INT **)s->frame->extended_data;
1637
    }
1638
1639
    /* apply the synthesis filter */
1640
21983
    for (ch = 0; ch < s->nb_channels; ch++) {
1641
        int sample_stride;
1642
11943
        if (s->avctx->sample_fmt == OUT_FMT_P) {
1643
11943
            samples_ptr   = samples[ch];
1644
11943
            sample_stride = 1;
1645
        } else {
1646
            samples_ptr   = samples[0] + ch;
1647
            sample_stride = s->nb_channels;
1648
        }
1649
441819
        for (i = 0; i < nb_frames; i++) {
1650
429876
            RENAME(ff_mpa_synth_filter)(&s->mpadsp, s->synth_buf[ch],
1651
                                        &(s->synth_buf_offset[ch]),
1652
                                        RENAME(ff_mpa_synth_window),
1653
                                        &s->dither_state, samples_ptr,
1654
429876
                                        sample_stride, s->sb_samples[ch][i]);
1655
429876
            samples_ptr += 32 * sample_stride;
1656
        }
1657
    }
1658
1659
10040
    return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1660
}
1661
1662
10042
static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1663
                        AVPacket *avpkt)
1664
{
1665
10042
    const uint8_t *buf  = avpkt->data;
1666
10042
    int buf_size        = avpkt->size;
1667
10042
    MPADecodeContext *s = avctx->priv_data;
1668
    uint32_t header;
1669
    int ret;
1670
1671
10042
    int skipped = 0;
1672

10042
    while(buf_size && !*buf){
1673
        buf++;
1674
        buf_size--;
1675
        skipped++;
1676
    }
1677
1678
10042
    if (buf_size < HEADER_SIZE)
1679
        return AVERROR_INVALIDDATA;
1680
1681
10042
    header = AV_RB32(buf);
1682
10042
    if (header>>8 == AV_RB32("TAG")>>8) {
1683
        av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
1684
        return buf_size + skipped;
1685
    }
1686
10042
    ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1687
10042
    if (ret < 0) {
1688
2
        av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1689
2
        return AVERROR_INVALIDDATA;
1690
10040
    } else if (ret == 1) {
1691
        /* free format: prepare to compute frame size */
1692
        s->frame_size = -1;
1693
        return AVERROR_INVALIDDATA;
1694
    }
1695
    /* update codec info */
1696
10040
    avctx->channels       = s->nb_channels;
1697
10040
    avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1698
10040
    if (!avctx->bit_rate)
1699
4
        avctx->bit_rate = s->bit_rate;
1700
1701
10040
    if (s->frame_size <= 0) {
1702
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1703
        return AVERROR_INVALIDDATA;
1704
10040
    } else if (s->frame_size < buf_size) {
1705
        av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1706
        buf_size= s->frame_size;
1707
    }
1708
1709
10040
    s->frame = data;
1710
1711
10040
    ret = mp_decode_frame(s, NULL, buf, buf_size);
1712
10040
    if (ret >= 0) {
1713
10040
        s->frame->nb_samples = avctx->frame_size;
1714
10040
        *got_frame_ptr       = 1;
1715
10040
        avctx->sample_rate   = s->sample_rate;
1716
        //FIXME maybe move the other codec info stuff from above here too
1717
    } else {
1718
        av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1719
        /* Only return an error if the bad frame makes up the whole packet or
1720
         * the error is related to buffer management.
1721
         * If there is more data in the packet, just consume the bad frame
1722
         * instead of returning an error, which would discard the whole
1723
         * packet. */
1724
        *got_frame_ptr = 0;
1725
        if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA)
1726
            return ret;
1727
    }
1728
10040
    s->frame_size = 0;
1729
10040
    return buf_size + skipped;
1730
}
1731
1732
static void mp_flush(MPADecodeContext *ctx)
1733
{
1734
    memset(ctx->synth_buf, 0, sizeof(ctx->synth_buf));
1735
    memset(ctx->mdct_buf, 0, sizeof(ctx->mdct_buf));
1736
    ctx->last_buf_size = 0;
1737
    ctx->dither_state = 0;
1738
}
1739
1740
static void flush(AVCodecContext *avctx)
1741
{
1742
    mp_flush(avctx->priv_data);
1743
}
1744
1745
#if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1746
static int decode_frame_adu(AVCodecContext *avctx, void *data,
1747
                            int *got_frame_ptr, AVPacket *avpkt)
1748
{
1749
    const uint8_t *buf  = avpkt->data;
1750
    int buf_size        = avpkt->size;
1751
    MPADecodeContext *s = avctx->priv_data;
1752
    uint32_t header;
1753
    int len, ret;
1754
    int av_unused out_size;
1755
1756
    len = buf_size;
1757
1758
    // Discard too short frames
1759
    if (buf_size < HEADER_SIZE) {
1760
        av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1761
        return AVERROR_INVALIDDATA;
1762
    }
1763
1764
1765
    if (len > MPA_MAX_CODED_FRAME_SIZE)
1766
        len = MPA_MAX_CODED_FRAME_SIZE;
1767
1768
    // Get header and restore sync word
1769
    header = AV_RB32(buf) | 0xffe00000;
1770
1771
    ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1772
    if (ret < 0) {
1773
        av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1774
        return ret;
1775
    }
1776
    /* update codec info */
1777
    avctx->sample_rate = s->sample_rate;
1778
    avctx->channels    = s->nb_channels;
1779
    avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1780
    if (!avctx->bit_rate)
1781
        avctx->bit_rate = s->bit_rate;
1782
1783
    s->frame_size = len;
1784
1785
    s->frame = data;
1786
1787
    ret = mp_decode_frame(s, NULL, buf, buf_size);
1788
    if (ret < 0) {
1789
        av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1790
        return ret;
1791
    }
1792
1793
    *got_frame_ptr = 1;
1794
1795
    return buf_size;
1796
}
1797
#endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1798
1799
#if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1800
1801
/**
1802
 * Context for MP3On4 decoder
1803
 */
1804
typedef struct MP3On4DecodeContext {
1805
    int frames;                     ///< number of mp3 frames per block (number of mp3 decoder instances)
1806
    int syncword;                   ///< syncword patch
1807
    const uint8_t *coff;            ///< channel offsets in output buffer
1808
    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1809
} MP3On4DecodeContext;
1810
1811
#include "mpeg4audio.h"
1812
1813
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1814
1815
/* number of mp3 decoder instances */
1816
static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1817
1818
/* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1819
static const uint8_t chan_offset[8][5] = {
1820
    { 0             },
1821
    { 0             },  // C
1822
    { 0             },  // FLR
1823
    { 2, 0          },  // C FLR
1824
    { 2, 0, 3       },  // C FLR BS
1825
    { 2, 0, 3       },  // C FLR BLRS
1826
    { 2, 0, 4, 3    },  // C FLR BLRS LFE
1827
    { 2, 0, 6, 4, 3 },  // C FLR BLRS BLR LFE
1828
};
1829
1830
/* mp3on4 channel layouts */
1831
static const int16_t chan_layout[8] = {
1832
    0,
1833
    AV_CH_LAYOUT_MONO,
1834
    AV_CH_LAYOUT_STEREO,
1835
    AV_CH_LAYOUT_SURROUND,
1836
    AV_CH_LAYOUT_4POINT0,
1837
    AV_CH_LAYOUT_5POINT0,
1838
    AV_CH_LAYOUT_5POINT1,
1839
    AV_CH_LAYOUT_7POINT1
1840
};
1841
1842
static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1843
{
1844
    MP3On4DecodeContext *s = avctx->priv_data;
1845
    int i;
1846
1847
    if (s->mp3decctx[0])
1848
        av_freep(&s->mp3decctx[0]->fdsp);
1849
1850
    for (i = 0; i < s->frames; i++)
1851
        av_freep(&s->mp3decctx[i]);
1852
1853
    return 0;
1854
}
1855
1856
1857
static av_cold int decode_init_mp3on4(AVCodecContext * avctx)
1858
{
1859
    MP3On4DecodeContext *s = avctx->priv_data;
1860
    MPEG4AudioConfig cfg;
1861
    int i;
1862
1863
    if ((avctx->extradata_size < 2) || !avctx->extradata) {
1864
        av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1865
        return AVERROR_INVALIDDATA;
1866
    }
1867
1868
    avpriv_mpeg4audio_get_config2(&cfg, avctx->extradata,
1869
                                  avctx->extradata_size, 1, avctx);
1870
    if (!cfg.chan_config || cfg.chan_config > 7) {
1871
        av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1872
        return AVERROR_INVALIDDATA;
1873
    }
1874
    s->frames             = mp3Frames[cfg.chan_config];
1875
    s->coff               = chan_offset[cfg.chan_config];
1876
    avctx->channels       = ff_mpeg4audio_channels[cfg.chan_config];
1877
    avctx->channel_layout = chan_layout[cfg.chan_config];
1878
1879
    if (cfg.sample_rate < 16000)
1880
        s->syncword = 0xffe00000;
1881
    else
1882
        s->syncword = 0xfff00000;
1883
1884
    /* Init the first mp3 decoder in standard way, so that all tables get builded
1885
     * We replace avctx->priv_data with the context of the first decoder so that
1886
     * decode_init() does not have to be changed.
1887
     * Other decoders will be initialized here copying data from the first context
1888
     */
1889
    // Allocate zeroed memory for the first decoder context
1890
    s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1891
    if (!s->mp3decctx[0])
1892
        goto alloc_fail;
1893
    // Put decoder context in place to make init_decode() happy
1894
    avctx->priv_data = s->mp3decctx[0];
1895
    decode_init(avctx);
1896
    // Restore mp3on4 context pointer
1897
    avctx->priv_data = s;
1898
    s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1899
1900
    /* Create a separate codec/context for each frame (first is already ok).
1901
     * Each frame is 1 or 2 channels - up to 5 frames allowed
1902
     */
1903
    for (i = 1; i < s->frames; i++) {
1904
        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1905
        if (!s->mp3decctx[i])
1906
            goto alloc_fail;
1907
        s->mp3decctx[i]->adu_mode = 1;
1908
        s->mp3decctx[i]->avctx = avctx;
1909
        s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1910
        s->mp3decctx[i]->fdsp = s->mp3decctx[0]->fdsp;
1911
    }
1912
1913
    return 0;
1914
alloc_fail:
1915
    decode_close_mp3on4(avctx);
1916
    return AVERROR(ENOMEM);
1917
}
1918
1919
1920
static void flush_mp3on4(AVCodecContext *avctx)
1921
{
1922
    int i;
1923
    MP3On4DecodeContext *s = avctx->priv_data;
1924
1925
    for (i = 0; i < s->frames; i++)
1926
        mp_flush(s->mp3decctx[i]);
1927
}
1928
1929
1930
static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1931
                               int *got_frame_ptr, AVPacket *avpkt)
1932
{
1933
    AVFrame *frame         = data;
1934
    const uint8_t *buf     = avpkt->data;
1935
    int buf_size           = avpkt->size;
1936
    MP3On4DecodeContext *s = avctx->priv_data;
1937
    MPADecodeContext *m;
1938
    int fsize, len = buf_size, out_size = 0;
1939
    uint32_t header;
1940
    OUT_INT **out_samples;
1941
    OUT_INT *outptr[2];
1942
    int fr, ch, ret;
1943
1944
    /* get output buffer */
1945
    frame->nb_samples = MPA_FRAME_SIZE;
1946
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1947
        return ret;
1948
    out_samples = (OUT_INT **)frame->extended_data;
1949
1950
    // Discard too short frames
1951
    if (buf_size < HEADER_SIZE)
1952
        return AVERROR_INVALIDDATA;
1953
1954
    avctx->bit_rate = 0;
1955
1956
    ch = 0;
1957
    for (fr = 0; fr < s->frames; fr++) {
1958
        fsize = AV_RB16(buf) >> 4;
1959
        fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1960
        m     = s->mp3decctx[fr];
1961
        av_assert1(m);
1962
1963
        if (fsize < HEADER_SIZE) {
1964
            av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1965
            return AVERROR_INVALIDDATA;
1966
        }
1967
        header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1968
1969
        ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1970
        if (ret < 0) {
1971
            av_log(avctx, AV_LOG_ERROR, "Bad header, discard block\n");
1972
            return AVERROR_INVALIDDATA;
1973
        }
1974
1975
        if (ch + m->nb_channels > avctx->channels ||
1976
            s->coff[fr] + m->nb_channels > avctx->channels) {
1977
            av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1978
                                        "channel count\n");
1979
            return AVERROR_INVALIDDATA;
1980
        }
1981
        ch += m->nb_channels;
1982
1983
        outptr[0] = out_samples[s->coff[fr]];
1984
        if (m->nb_channels > 1)
1985
            outptr[1] = out_samples[s->coff[fr] + 1];
1986
1987
        if ((ret = mp_decode_frame(m, outptr, buf, fsize)) < 0) {
1988
            av_log(avctx, AV_LOG_ERROR, "failed to decode channel %d\n", ch);
1989
            memset(outptr[0], 0, MPA_FRAME_SIZE*sizeof(OUT_INT));
1990
            if (m->nb_channels > 1)
1991
                memset(outptr[1], 0, MPA_FRAME_SIZE*sizeof(OUT_INT));
1992
            ret = m->nb_channels * MPA_FRAME_SIZE*sizeof(OUT_INT);
1993
        }
1994
1995
        out_size += ret;
1996
        buf      += fsize;
1997
        len      -= fsize;
1998
1999
        avctx->bit_rate += m->bit_rate;
2000
    }
2001
    if (ch != avctx->channels) {
2002
        av_log(avctx, AV_LOG_ERROR, "failed to decode all channels\n");
2003
        return AVERROR_INVALIDDATA;
2004
    }
2005
2006
    /* update codec info */
2007
    avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2008
2009
    frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2010
    *got_frame_ptr    = 1;
2011
2012
    return buf_size;
2013
}
2014
#endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */