GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mpegaudioenc_template.c Lines: 396 411 96.4 %
Date: 2019-11-22 03:34:36 Branches: 166 183 90.7 %

Line Branch Exec Source
1
/*
2
 * The simplest mpeg audio layer 2 encoder
3
 * Copyright (c) 2000, 2001 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
 * The simplest mpeg audio layer 2 encoder.
25
 */
26
27
#include "libavutil/channel_layout.h"
28
29
#include "avcodec.h"
30
#include "internal.h"
31
#include "put_bits.h"
32
33
#define FRAC_BITS   15   /* fractional bits for sb_samples and dct */
34
#define WFRAC_BITS  14   /* fractional bits for window */
35
36
#include "mpegaudio.h"
37
#include "mpegaudiodsp.h"
38
#include "mpegaudiodata.h"
39
#include "mpegaudiotab.h"
40
41
/* currently, cannot change these constants (need to modify
42
   quantization stage) */
43
#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
44
45
#define SAMPLES_BUF_SIZE 4096
46
47
typedef struct MpegAudioContext {
48
    PutBitContext pb;
49
    int nb_channels;
50
    int lsf;           /* 1 if mpeg2 low bitrate selected */
51
    int bitrate_index; /* bit rate */
52
    int freq_index;
53
    int frame_size; /* frame size, in bits, without padding */
54
    /* padding computation */
55
    int frame_frac, frame_frac_incr, do_padding;
56
    short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
57
    int samples_offset[MPA_MAX_CHANNELS];       /* offset in samples_buf */
58
    int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
59
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
60
    /* code to group 3 scale factors */
61
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
62
    int sblimit; /* number of used subbands */
63
    const unsigned char *alloc_table;
64
    int16_t filter_bank[512];
65
    int scale_factor_table[64];
66
    unsigned char scale_diff_table[128];
67
#if USE_FLOATS
68
    float scale_factor_inv_table[64];
69
#else
70
    int8_t scale_factor_shift[64];
71
    unsigned short scale_factor_mult[64];
72
#endif
73
    unsigned short total_quant_bits[17]; /* total number of bits per allocation group */
74
} MpegAudioContext;
75
76
23
static av_cold int MPA_encode_init(AVCodecContext *avctx)
77
{
78
23
    MpegAudioContext *s = avctx->priv_data;
79
23
    int freq = avctx->sample_rate;
80
23
    int bitrate = avctx->bit_rate;
81
23
    int channels = avctx->channels;
82
    int i, v, table;
83
    float a;
84
85

23
    if (channels <= 0 || channels > 2){
86
        av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
87
        return AVERROR(EINVAL);
88
    }
89
23
    bitrate = bitrate / 1000;
90
23
    s->nb_channels = channels;
91
23
    avctx->frame_size = MPA_FRAME_SIZE;
92
23
    avctx->initial_padding = 512 - 32 + 1;
93
94
    /* encoding freq */
95
23
    s->lsf = 0;
96
23
    for(i=0;i<3;i++) {
97
23
        if (avpriv_mpa_freq_tab[i] == freq)
98
23
            break;
99
        if ((avpriv_mpa_freq_tab[i] / 2) == freq) {
100
            s->lsf = 1;
101
            break;
102
        }
103
    }
104
23
    if (i == 3){
105
        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
106
        return AVERROR(EINVAL);
107
    }
108
23
    s->freq_index = i;
109
110
    /* encoding bitrate & frequency */
111
227
    for(i=1;i<15;i++) {
112
216
        if (avpriv_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
113
12
            break;
114
    }
115

23
    if (i == 15 && !avctx->bit_rate) {
116
11
        i = 14;
117
11
        bitrate = avpriv_mpa_bitrate_tab[s->lsf][1][i];
118
11
        avctx->bit_rate = bitrate * 1000;
119
    }
120
23
    if (i == 15){
121
        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
122
        return AVERROR(EINVAL);
123
    }
124
23
    s->bitrate_index = i;
125
126
    /* compute total header size & pad bit */
127
128
23
    a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
129
23
    s->frame_size = ((int)a) * 8;
130
131
    /* frame fractional size to compute padding */
132
23
    s->frame_frac = 0;
133
23
    s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
134
135
    /* select the right allocation table */
136
23
    table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
137
138
    /* number of used subbands */
139
23
    s->sblimit = ff_mpa_sblimit_table[table];
140
23
    s->alloc_table = ff_mpa_alloc_tables[table];
141
142
    ff_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
143
            bitrate, freq, s->frame_size, table, s->frame_frac_incr);
144
145
48
    for(i=0;i<s->nb_channels;i++)
146
25
        s->samples_offset[i] = 0;
147
148
5934
    for(i=0;i<257;i++) {
149
        int v;
150
5911
        v = ff_mpa_enwindow[i];
151
#if WFRAC_BITS != 16
152
5911
        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
153
#endif
154
5911
        s->filter_bank[i] = v;
155
5911
        if ((i & 63) != 0)
156
5796
            v = -v;
157
5911
        if (i != 0)
158
5888
            s->filter_bank[512 - i] = v;
159
    }
160
161
1495
    for(i=0;i<64;i++) {
162
1472
        v = (int)(exp2((3 - i) / 3.0) * (1 << 20));
163
1472
        if (v <= 0)
164
            v = 1;
165
1472
        s->scale_factor_table[i] = v;
166
#if USE_FLOATS
167
704
        s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
168
#else
169
#define P 15
170
768
        s->scale_factor_shift[i] = 21 - P - (i / 3);
171
768
        s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
172
#endif
173
    }
174
2967
    for(i=0;i<128;i++) {
175
2944
        v = i - 64;
176
2944
        if (v <= -3)
177
1426
            v = 0;
178
1518
        else if (v < 0)
179
46
            v = 1;
180
1472
        else if (v == 0)
181
23
            v = 2;
182
1449
        else if (v < 3)
183
46
            v = 3;
184
        else
185
1403
            v = 4;
186
2944
        s->scale_diff_table[i] = v;
187
    }
188
189
414
    for(i=0;i<17;i++) {
190
391
        v = ff_mpa_quant_bits[i];
191
391
        if (v < 0)
192
69
            v = -v;
193
        else
194
322
            v = v * 3;
195
391
        s->total_quant_bits[i] = 12 * v;
196
    }
197
198
23
    return 0;
199
}
200
201
/* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
202
317448
static void idct32(int *out, int *tab)
203
{
204
    int i, j;
205
    int *t, *t1, xr;
206
317448
    const int *xp = costab32;
207
208
5079168
    for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
209
210
317448
    t = tab + 30;
211
317448
    t1 = tab + 2;
212
    do {
213
2222136
        t[0] += t[-4];
214
2222136
        t[1] += t[1 - 4];
215
2222136
        t -= 4;
216
2222136
    } while (t != t1);
217
218
317448
    t = tab + 28;
219
317448
    t1 = tab + 4;
220
    do {
221
952344
        t[0] += t[-8];
222
952344
        t[1] += t[1-8];
223
952344
        t[2] += t[2-8];
224
952344
        t[3] += t[3-8];
225
952344
        t -= 8;
226
952344
    } while (t != t1);
227
228
317448
    t = tab;
229
317448
    t1 = tab + 32;
230
    do {
231
634896
        t[ 3] = -t[ 3];
232
634896
        t[ 6] = -t[ 6];
233
234
634896
        t[11] = -t[11];
235
634896
        t[12] = -t[12];
236
634896
        t[13] = -t[13];
237
634896
        t[15] = -t[15];
238
634896
        t += 16;
239
634896
    } while (t != t1);
240
241
242
317448
    t = tab;
243
317448
    t1 = tab + 8;
244
    do {
245
        int x1, x2, x3, x4;
246
247
2539584
        x3 = MUL(t[16], FIX(M_SQRT2*0.5));
248
2539584
        x4 = t[0] - x3;
249
2539584
        x3 = t[0] + x3;
250
251
2539584
        x2 = MUL(-(t[24] + t[8]), FIX(M_SQRT2*0.5));
252
2539584
        x1 = MUL((t[8] - x2), xp[0]);
253
2539584
        x2 = MUL((t[8] + x2), xp[1]);
254
255
2539584
        t[ 0] = x3 + x1;
256
2539584
        t[ 8] = x4 - x2;
257
2539584
        t[16] = x4 + x2;
258
2539584
        t[24] = x3 - x1;
259
2539584
        t++;
260
2539584
    } while (t != t1);
261
262
317448
    xp += 2;
263
317448
    t = tab;
264
317448
    t1 = tab + 4;
265
    do {
266
1269792
        xr = MUL(t[28],xp[0]);
267
1269792
        t[28] = (t[0] - xr);
268
1269792
        t[0] = (t[0] + xr);
269
270
1269792
        xr = MUL(t[4],xp[1]);
271
1269792
        t[ 4] = (t[24] - xr);
272
1269792
        t[24] = (t[24] + xr);
273
274
1269792
        xr = MUL(t[20],xp[2]);
275
1269792
        t[20] = (t[8] - xr);
276
1269792
        t[ 8] = (t[8] + xr);
277
278
1269792
        xr = MUL(t[12],xp[3]);
279
1269792
        t[12] = (t[16] - xr);
280
1269792
        t[16] = (t[16] + xr);
281
1269792
        t++;
282
1269792
    } while (t != t1);
283
317448
    xp += 4;
284
285
1587240
    for (i = 0; i < 4; i++) {
286
1269792
        xr = MUL(tab[30-i*4],xp[0]);
287
1269792
        tab[30-i*4] = (tab[i*4] - xr);
288
1269792
        tab[   i*4] = (tab[i*4] + xr);
289
290
1269792
        xr = MUL(tab[ 2+i*4],xp[1]);
291
1269792
        tab[ 2+i*4] = (tab[28-i*4] - xr);
292
1269792
        tab[28-i*4] = (tab[28-i*4] + xr);
293
294
1269792
        xr = MUL(tab[31-i*4],xp[0]);
295
1269792
        tab[31-i*4] = (tab[1+i*4] - xr);
296
1269792
        tab[ 1+i*4] = (tab[1+i*4] + xr);
297
298
1269792
        xr = MUL(tab[ 3+i*4],xp[1]);
299
1269792
        tab[ 3+i*4] = (tab[29-i*4] - xr);
300
1269792
        tab[29-i*4] = (tab[29-i*4] + xr);
301
302
1269792
        xp += 2;
303
    }
304
305
317448
    t = tab + 30;
306
317448
    t1 = tab + 1;
307
    do {
308
5079168
        xr = MUL(t1[0], *xp);
309
5079168
        t1[0] = (t[0] - xr);
310
5079168
        t[0] = (t[0] + xr);
311
5079168
        t -= 2;
312
5079168
        t1 += 2;
313
5079168
        xp++;
314
5079168
    } while (t >= tab);
315
316
10475784
    for(i=0;i<32;i++) {
317
10158336
        out[i] = tab[bitinv32[i]];
318
    }
319
317448
}
320
321
#define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
322
323
8818
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
324
{
325
    short *p, *q;
326
    int sum, offset, i, j;
327
    int tmp[64];
328
    int tmp1[32];
329
    int *out;
330
331
8818
    offset = s->samples_offset[ch];
332
8818
    out = &s->sb_samples[ch][0][0][0];
333
326266
    for(j=0;j<36;j++) {
334
        /* 32 samples at once */
335
10475784
        for(i=0;i<32;i++) {
336
10158336
            s->samples_buf[ch][offset + (31 - i)] = samples[0];
337
10158336
            samples += incr;
338
        }
339
340
        /* filter */
341
317448
        p = s->samples_buf[ch] + offset;
342
317448
        q = s->filter_bank;
343
        /* maxsum = 23169 */
344
20634120
        for(i=0;i<64;i++) {
345
20316672
            sum = p[0*64] * q[0*64];
346
20316672
            sum += p[1*64] * q[1*64];
347
20316672
            sum += p[2*64] * q[2*64];
348
20316672
            sum += p[3*64] * q[3*64];
349
20316672
            sum += p[4*64] * q[4*64];
350
20316672
            sum += p[5*64] * q[5*64];
351
20316672
            sum += p[6*64] * q[6*64];
352
20316672
            sum += p[7*64] * q[7*64];
353
20316672
            tmp[i] = sum;
354
20316672
            p++;
355
20316672
            q++;
356
        }
357
317448
        tmp1[0] = tmp[16] >> WSHIFT;
358
5396616
        for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
359
5079168
        for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
360
361
317448
        idct32(out, tmp1);
362
363
        /* advance of 32 samples */
364
317448
        offset -= 32;
365
317448
        out += 32;
366
        /* handle the wrap around */
367
317448
        if (offset < 0) {
368
2828
            memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
369
2828
                    s->samples_buf[ch], (512 - 32) * 2);
370
2828
            offset = SAMPLES_BUF_SIZE - 512;
371
        }
372
    }
373
8818
    s->samples_offset[ch] = offset;
374
8818
}
375
376
8818
static void compute_scale_factors(MpegAudioContext *s,
377
                                  unsigned char scale_code[SBLIMIT],
378
                                  unsigned char scale_factors[SBLIMIT][3],
379
                                  int sb_samples[3][12][SBLIMIT],
380
                                  int sblimit)
381
{
382
    int *p, vmax, v, n, i, j, k, code;
383
    int index, d1, d2;
384
8818
    unsigned char *sf = &scale_factors[0][0];
385
386
270808
    for(j=0;j<sblimit;j++) {
387
1047960
        for(i=0;i<3;i++) {
388
            /* find the max absolute value */
389
785970
            p = &sb_samples[i][0][j];
390
785970
            vmax = abs(*p);
391
9431640
            for(k=1;k<12;k++) {
392
8645670
                p += SBLIMIT;
393
8645670
                v = abs(*p);
394
8645670
                if (v > vmax)
395
1442693
                    vmax = v;
396
            }
397
            /* compute the scale factor index using log 2 computations */
398
785970
            if (vmax > 1) {
399
785900
                n = av_log2(vmax);
400
                /* n is the position of the MSB of vmax. now
401
                   use at most 2 compares to find the index */
402
785900
                index = (21 - n) * 3 - 3;
403
785900
                if (index >= 0) {
404
1760879
                    while (vmax <= s->scale_factor_table[index+1])
405
974979
                        index++;
406
                } else {
407
                    index = 0; /* very unlikely case of overflow */
408
                }
409
            } else {
410
70
                index = 62; /* value 63 is not allowed */
411
            }
412
413
            ff_dlog(NULL, "%2d:%d in=%x %x %d\n",
414
                    j, i, vmax, s->scale_factor_table[index], index);
415
            /* store the scale factor */
416
            av_assert2(index >=0 && index <= 63);
417
785970
            sf[i] = index;
418
        }
419
420
        /* compute the transmission factor : look if the scale factors
421
           are close enough to each other */
422
261990
        d1 = s->scale_diff_table[sf[0] - sf[1] + 64];
423
261990
        d2 = s->scale_diff_table[sf[1] - sf[2] + 64];
424
425
        /* handle the 25 cases */
426


261990
        switch(d1 * 5 + d2) {
427
4123
        case 0*5+0:
428
        case 0*5+4:
429
        case 3*5+4:
430
        case 4*5+0:
431
        case 4*5+4:
432
4123
            code = 0;
433
4123
            break;
434
4657
        case 0*5+1:
435
        case 0*5+2:
436
        case 4*5+1:
437
        case 4*5+2:
438
4657
            code = 3;
439
4657
            sf[2] = sf[1];
440
4657
            break;
441
3579
        case 0*5+3:
442
        case 4*5+3:
443
3579
            code = 3;
444
3579
            sf[1] = sf[2];
445
3579
            break;
446
4294
        case 1*5+0:
447
        case 1*5+4:
448
        case 2*5+4:
449
4294
            code = 1;
450
4294
            sf[1] = sf[0];
451
4294
            break;
452
122581
        case 1*5+1:
453
        case 1*5+2:
454
        case 2*5+0:
455
        case 2*5+1:
456
        case 2*5+2:
457
122581
            code = 2;
458
122581
            sf[1] = sf[2] = sf[0];
459
122581
            break;
460
34807
        case 2*5+3:
461
        case 3*5+3:
462
34807
            code = 2;
463
34807
            sf[0] = sf[1] = sf[2];
464
34807
            break;
465
57486
        case 3*5+0:
466
        case 3*5+1:
467
        case 3*5+2:
468
57486
            code = 2;
469
57486
            sf[0] = sf[2] = sf[1];
470
57486
            break;
471
30463
        case 1*5+3:
472
30463
            code = 2;
473
30463
            if (sf[0] > sf[2])
474
4873
              sf[0] = sf[2];
475
30463
            sf[1] = sf[2] = sf[0];
476
30463
            break;
477
        default:
478
            av_assert2(0); //cannot happen
479
            code = 0;           /* kill warning */
480
        }
481
482
        ff_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j,
483
                sf[0], sf[1], sf[2], d1, d2, code);
484
261990
        scale_code[j] = code;
485
261990
        sf += 3;
486
    }
487
8818
}
488
489
/* The most important function : psycho acoustic module. In this
490
   encoder there is basically none, so this is the worst you can do,
491
   but also this is the simpler. */
492
8818
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
493
{
494
    int i;
495
496
270808
    for(i=0;i<s->sblimit;i++) {
497
261990
        smr[i] = (int)(fixed_smr[i] * 10);
498
    }
499
8818
}
500
501
502
#define SB_NOTALLOCATED  0
503
#define SB_ALLOCATED     1
504
#define SB_NOMORE        2
505
506
/* Try to maximize the smr while using a number of bits inferior to
507
   the frame size. I tried to make the code simpler, faster and
508
   smaller than other encoders :-) */
509
8358
static void compute_bit_allocation(MpegAudioContext *s,
510
                                   short smr1[MPA_MAX_CHANNELS][SBLIMIT],
511
                                   unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
512
                                   int *padding)
513
{
514
    int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
515
    int incr;
516
    short smr[MPA_MAX_CHANNELS][SBLIMIT];
517
    unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
518
    const unsigned char *alloc;
519
520
8358
    memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
521
8358
    memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
522
8358
    memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
523
524
    /* compute frame size and padding */
525
8358
    max_frame_size = s->frame_size;
526
8358
    s->frame_frac += s->frame_frac_incr;
527
8358
    if (s->frame_frac >= 65536) {
528
7387
        s->frame_frac -= 65536;
529
7387
        s->do_padding = 1;
530
7387
        max_frame_size += 8;
531
    } else {
532
971
        s->do_padding = 0;
533
    }
534
535
    /* compute the header + bit alloc size */
536
8358
    current_frame_size = 32;
537
8358
    alloc = s->alloc_table;
538
257238
    for(i=0;i<s->sblimit;i++) {
539
248880
        incr = alloc[0];
540
248880
        current_frame_size += incr * s->nb_channels;
541
248880
        alloc += 1 << incr;
542
    }
543
    for(;;) {
544
        /* look for the subband with the largest signal to mask ratio */
545
1819479
        max_sb = -1;
546
1819479
        max_ch = -1;
547
1819479
        max_smr = INT_MIN;
548
3732459
        for(ch=0;ch<s->nb_channels;ch++) {
549
59075214
            for(i=0;i<s->sblimit;i++) {
550

57162234
                if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
551
4820286
                    max_smr = smr[ch][i];
552
4820286
                    max_sb = i;
553
4820286
                    max_ch = ch;
554
                }
555
            }
556
        }
557
1819479
        if (max_sb < 0)
558
8358
            break;
559
        ff_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
560
                current_frame_size, max_frame_size, max_sb, max_ch,
561
                bit_alloc[max_ch][max_sb]);
562
563
        /* find alloc table entry (XXX: not optimal, should use
564
           pointer table) */
565
1811121
        alloc = s->alloc_table;
566
18067054
        for(i=0;i<max_sb;i++) {
567
16255933
            alloc += 1 << alloc[0];
568
        }
569
570
1811121
        if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
571
            /* nothing was coded for this band: add the necessary bits */
572
261990
            incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
573
261990
            incr += s->total_quant_bits[alloc[1]];
574
        } else {
575
            /* increments bit allocation */
576
1549131
            b = bit_alloc[max_ch][max_sb];
577
1549131
            incr = s->total_quant_bits[alloc[b + 1]] -
578
1549131
                s->total_quant_bits[alloc[b]];
579
        }
580
581
1811121
        if (current_frame_size + incr <= max_frame_size) {
582
            /* can increase size */
583
1610342
            b = ++bit_alloc[max_ch][max_sb];
584
1610342
            current_frame_size += incr;
585
            /* decrease smr by the resolution we added */
586
1610342
            smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
587
            /* max allocation size reached ? */
588
1610342
            if (b == ((1 << alloc[0]) - 1))
589
61211
                subband_status[max_ch][max_sb] = SB_NOMORE;
590
            else
591
1549131
                subband_status[max_ch][max_sb] = SB_ALLOCATED;
592
        } else {
593
            /* cannot increase the size of this subband */
594
200779
            subband_status[max_ch][max_sb] = SB_NOMORE;
595
        }
596
    }
597
8358
    *padding = max_frame_size - current_frame_size;
598
8358
    av_assert0(*padding >= 0);
599
8358
}
600
601
/*
602
 * Output the MPEG audio layer 2 frame. Note how the code is small
603
 * compared to other encoders :-)
604
 */
605
8358
static void encode_frame(MpegAudioContext *s,
606
                         unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
607
                         int padding)
608
{
609
    int i, j, k, l, bit_alloc_bits, b, ch;
610
    unsigned char *sf;
611
    int q[3];
612
8358
    PutBitContext *p = &s->pb;
613
614
    /* header */
615
616
8358
    put_bits(p, 12, 0xfff);
617
8358
    put_bits(p, 1, 1 - s->lsf); /* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */
618
8358
    put_bits(p, 2, 4-2);  /* layer 2 */
619
8358
    put_bits(p, 1, 1); /* no error protection */
620
8358
    put_bits(p, 4, s->bitrate_index);
621
8358
    put_bits(p, 2, s->freq_index);
622
8358
    put_bits(p, 1, s->do_padding); /* use padding */
623
8358
    put_bits(p, 1, 0);             /* private_bit */
624
8358
    put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
625
8358
    put_bits(p, 2, 0); /* mode_ext */
626
8358
    put_bits(p, 1, 0); /* no copyright */
627
8358
    put_bits(p, 1, 1); /* original */
628
8358
    put_bits(p, 2, 0); /* no emphasis */
629
630
    /* bit allocation */
631
8358
    j = 0;
632
257238
    for(i=0;i<s->sblimit;i++) {
633
248880
        bit_alloc_bits = s->alloc_table[j];
634
510870
        for(ch=0;ch<s->nb_channels;ch++) {
635
261990
            put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
636
        }
637
248880
        j += 1 << bit_alloc_bits;
638
    }
639
640
    /* scale codes */
641
257238
    for(i=0;i<s->sblimit;i++) {
642
510870
        for(ch=0;ch<s->nb_channels;ch++) {
643
261990
            if (bit_alloc[ch][i])
644
187506
                put_bits(p, 2, s->scale_code[ch][i]);
645
        }
646
    }
647
648
    /* scale factors */
649
257238
    for(i=0;i<s->sblimit;i++) {
650
510870
        for(ch=0;ch<s->nb_channels;ch++) {
651
261990
            if (bit_alloc[ch][i]) {
652
187506
                sf = &s->scale_factors[ch][i][0];
653

187506
                switch(s->scale_code[ch][i]) {
654
2625
                case 0:
655
2625
                    put_bits(p, 6, sf[0]);
656
2625
                    put_bits(p, 6, sf[1]);
657
2625
                    put_bits(p, 6, sf[2]);
658
2625
                    break;
659
7938
                case 3:
660
                case 1:
661
7938
                    put_bits(p, 6, sf[0]);
662
7938
                    put_bits(p, 6, sf[2]);
663
7938
                    break;
664
176943
                case 2:
665
176943
                    put_bits(p, 6, sf[0]);
666
176943
                    break;
667
                }
668
261990
            }
669
        }
670
    }
671
672
    /* quantization & write sub band samples */
673
674
33432
    for(k=0;k<3;k++) {
675
125370
        for(l=0;l<12;l+=3) {
676
100296
            j = 0;
677
3086856
            for(i=0;i<s->sblimit;i++) {
678
2986560
                bit_alloc_bits = s->alloc_table[j];
679
6130440
                for(ch=0;ch<s->nb_channels;ch++) {
680
3143880
                    b = bit_alloc[ch][i];
681
3143880
                    if (b) {
682
                        int qindex, steps, m, sample, bits;
683
                        /* we encode 3 sub band samples of the same sub band at a time */
684
2250072
                        qindex = s->alloc_table[j+b];
685
2250072
                        steps = ff_mpa_quant_steps[qindex];
686
9000288
                        for(m=0;m<3;m++) {
687
6750216
                            sample = s->sb_samples[ch][k][l + m][i];
688
                            /* divide by scale factor */
689
#if USE_FLOATS
690
                            {
691
                                float a;
692
489240
                                a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]];
693
489240
                                q[m] = (int)((a + 1.0) * steps * 0.5);
694
                            }
695
#else
696
                            {
697
                                int q1, e, shift, mult;
698
6260976
                                e = s->scale_factors[ch][i][k];
699
6260976
                                shift = s->scale_factor_shift[e];
700
6260976
                                mult = s->scale_factor_mult[e];
701
702
                                /* normalize to P bits */
703
6260976
                                if (shift < 0)
704
5709696
                                    q1 = sample << (-shift);
705
                                else
706
551280
                                    q1 = sample >> shift;
707
6260976
                                q1 = (q1 * mult) >> P;
708
6260976
                                q1 += 1 << P;
709
6260976
                                if (q1 < 0)
710
                                    q1 = 0;
711
6260976
                                q[m] = (q1 * (unsigned)steps) >> (P + 1);
712
                            }
713
#endif
714
6750216
                            if (q[m] >= steps)
715
11490
                                q[m] = steps - 1;
716
                            av_assert2(q[m] >= 0 && q[m] < steps);
717
                        }
718
2250072
                        bits = ff_mpa_quant_bits[qindex];
719
2250072
                        if (bits < 0) {
720
                            /* group the 3 values to save bits */
721
307956
                            put_bits(p, -bits,
722
307956
                                     q[0] + steps * (q[1] + steps * q[2]));
723
                        } else {
724
1942116
                            put_bits(p, bits, q[0]);
725
1942116
                            put_bits(p, bits, q[1]);
726
1942116
                            put_bits(p, bits, q[2]);
727
                        }
728
                    }
729
                }
730
                /* next subband in alloc table */
731
2986560
                j += 1 << bit_alloc_bits;
732
            }
733
        }
734
    }
735
736
    /* padding */
737
74042
    for(i=0;i<padding;i++)
738
65684
        put_bits(p, 1, 0);
739
740
    /* flush */
741
8358
    flush_put_bits(p);
742
8358
}
743
744
8358
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
745
                            const AVFrame *frame, int *got_packet_ptr)
746
{
747
8358
    MpegAudioContext *s = avctx->priv_data;
748
8358
    const int16_t *samples = (const int16_t *)frame->data[0];
749
    short smr[MPA_MAX_CHANNELS][SBLIMIT];
750
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
751
    int padding, i, ret;
752
753
17176
    for(i=0;i<s->nb_channels;i++) {
754
8818
        filter(s, i, samples + i, s->nb_channels);
755
    }
756
757
17176
    for(i=0;i<s->nb_channels;i++) {
758
8818
        compute_scale_factors(s, s->scale_code[i], s->scale_factors[i],
759
8818
                              s->sb_samples[i], s->sblimit);
760
    }
761
17176
    for(i=0;i<s->nb_channels;i++) {
762
8818
        psycho_acoustic_model(s, smr[i]);
763
    }
764
8358
    compute_bit_allocation(s, smr, bit_alloc, &padding);
765
766
8358
    if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE, 0)) < 0)
767
        return ret;
768
769
8358
    init_put_bits(&s->pb, avpkt->data, avpkt->size);
770
771
8358
    encode_frame(s, bit_alloc, padding);
772
773
8358
    if (frame->pts != AV_NOPTS_VALUE)
774
8358
        avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
775
776
8358
    avpkt->size = put_bits_count(&s->pb) / 8;
777
8358
    *got_packet_ptr = 1;
778
8358
    return 0;
779
}
780
781
static const AVCodecDefault mp2_defaults[] = {
782
    { "b", "0" },
783
    { NULL },
784
};
785