Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudio.c @ bb54f6ab

History | View | Annotate | Download (22.7 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * The simplest mpeg audio layer 2 encoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 ff4ec49e Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 115329f1 Diego Biurrun
22 983e3246 Michael Niedermayer
/**
23
 * @file mpegaudio.c
24
 * The simplest mpeg audio layer 2 encoder.
25
 */
26 115329f1 Diego Biurrun
27 de6d9b64 Fabrice Bellard
#include "avcodec.h"
28 caa336b4 Michael Niedermayer
#include "bitstream.h"
29 de6d9b64 Fabrice Bellard
#include "mpegaudio.h"
30
31 afa982fd Fabrice Bellard
/* currently, cannot change these constants (need to modify
32
   quantization stage) */
33 0c1a9eda Zdenek Kabelac
#define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
34 afa982fd Fabrice Bellard
#define FIX(a)   ((int)((a) * (1 << FRAC_BITS)))
35 2456e28d Fabrice Bellard
36
#define SAMPLES_BUF_SIZE 4096
37
38
typedef struct MpegAudioContext {
39
    PutBitContext pb;
40
    int nb_channels;
41
    int freq, bit_rate;
42
    int lsf;           /* 1 if mpeg2 low bitrate selected */
43
    int bitrate_index; /* bit rate */
44
    int freq_index;
45
    int frame_size; /* frame size, in bits, without padding */
46 0c1a9eda Zdenek Kabelac
    int64_t nb_samples; /* total number of samples encoded */
47 2456e28d Fabrice Bellard
    /* padding computation */
48
    int frame_frac, frame_frac_incr, do_padding;
49
    short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
50
    int samples_offset[MPA_MAX_CHANNELS];       /* offset in samples_buf */
51
    int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
52
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
53
    /* code to group 3 scale factors */
54 115329f1 Diego Biurrun
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
55 2456e28d Fabrice Bellard
    int sblimit; /* number of used subbands */
56
    const unsigned char *alloc_table;
57
} MpegAudioContext;
58
59 de6d9b64 Fabrice Bellard
/* define it to use floats in quantization (I don't like floats !) */
60
//#define USE_FLOATS
61
62
#include "mpegaudiotab.h"
63
64 5c91a675 Zdenek Kabelac
static int MPA_encode_init(AVCodecContext *avctx)
65 de6d9b64 Fabrice Bellard
{
66
    MpegAudioContext *s = avctx->priv_data;
67
    int freq = avctx->sample_rate;
68
    int bitrate = avctx->bit_rate;
69
    int channels = avctx->channels;
70 2456e28d Fabrice Bellard
    int i, v, table;
71 de6d9b64 Fabrice Bellard
    float a;
72
73
    if (channels > 2)
74
        return -1;
75
    bitrate = bitrate / 1000;
76
    s->nb_channels = channels;
77
    s->freq = freq;
78
    s->bit_rate = bitrate * 1000;
79
    avctx->frame_size = MPA_FRAME_SIZE;
80
81
    /* encoding freq */
82
    s->lsf = 0;
83
    for(i=0;i<3;i++) {
84 115329f1 Diego Biurrun
        if (mpa_freq_tab[i] == freq)
85 de6d9b64 Fabrice Bellard
            break;
86 2456e28d Fabrice Bellard
        if ((mpa_freq_tab[i] / 2) == freq) {
87 de6d9b64 Fabrice Bellard
            s->lsf = 1;
88
            break;
89
        }
90
    }
91 bb0b93ba Michael Niedermayer
    if (i == 3){
92
        av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
93 de6d9b64 Fabrice Bellard
        return -1;
94 bb0b93ba Michael Niedermayer
    }
95 de6d9b64 Fabrice Bellard
    s->freq_index = i;
96
97
    /* encoding bitrate & frequency */
98
    for(i=0;i<15;i++) {
99 115329f1 Diego Biurrun
        if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
100 de6d9b64 Fabrice Bellard
            break;
101
    }
102 bb0b93ba Michael Niedermayer
    if (i == 15){
103
        av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
104 de6d9b64 Fabrice Bellard
        return -1;
105 bb0b93ba Michael Niedermayer
    }
106 de6d9b64 Fabrice Bellard
    s->bitrate_index = i;
107
108
    /* compute total header size & pad bit */
109 115329f1 Diego Biurrun
110 de6d9b64 Fabrice Bellard
    a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
111
    s->frame_size = ((int)a) * 8;
112
113
    /* frame fractional size to compute padding */
114
    s->frame_frac = 0;
115
    s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
116 115329f1 Diego Biurrun
117 de6d9b64 Fabrice Bellard
    /* select the right allocation table */
118 2456e28d Fabrice Bellard
    table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
119
120 de6d9b64 Fabrice Bellard
    /* number of used subbands */
121
    s->sblimit = sblimit_table[table];
122
    s->alloc_table = alloc_tables[table];
123
124
#ifdef DEBUG
125 115329f1 Diego Biurrun
    av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
126 de6d9b64 Fabrice Bellard
           bitrate, freq, s->frame_size, table, s->frame_frac_incr);
127
#endif
128
129
    for(i=0;i<s->nb_channels;i++)
130
        s->samples_offset[i] = 0;
131
132 2456e28d Fabrice Bellard
    for(i=0;i<257;i++) {
133
        int v;
134 afa982fd Fabrice Bellard
        v = mpa_enwindow[i];
135
#if WFRAC_BITS != 16
136
        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
137
#endif
138 2456e28d Fabrice Bellard
        filter_bank[i] = v;
139
        if ((i & 63) != 0)
140
            v = -v;
141
        if (i != 0)
142
            filter_bank[512 - i] = v;
143 de6d9b64 Fabrice Bellard
    }
144 2456e28d Fabrice Bellard
145 de6d9b64 Fabrice Bellard
    for(i=0;i<64;i++) {
146
        v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
147
        if (v <= 0)
148
            v = 1;
149
        scale_factor_table[i] = v;
150
#ifdef USE_FLOATS
151
        scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
152
#else
153
#define P 15
154
        scale_factor_shift[i] = 21 - P - (i / 3);
155
        scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
156
#endif
157
    }
158
    for(i=0;i<128;i++) {
159
        v = i - 64;
160
        if (v <= -3)
161
            v = 0;
162
        else if (v < 0)
163
            v = 1;
164
        else if (v == 0)
165
            v = 2;
166
        else if (v < 3)
167
            v = 3;
168 115329f1 Diego Biurrun
        else
169 de6d9b64 Fabrice Bellard
            v = 4;
170
        scale_diff_table[i] = v;
171
    }
172
173
    for(i=0;i<17;i++) {
174
        v = quant_bits[i];
175 115329f1 Diego Biurrun
        if (v < 0)
176 de6d9b64 Fabrice Bellard
            v = -v;
177
        else
178
            v = v * 3;
179
        total_quant_bits[i] = 12 * v;
180
    }
181
182 492cd3a9 Michael Niedermayer
    avctx->coded_frame= avcodec_alloc_frame();
183
    avctx->coded_frame->key_frame= 1;
184
185 de6d9b64 Fabrice Bellard
    return 0;
186
}
187
188 2456e28d Fabrice Bellard
/* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
189 afa982fd Fabrice Bellard
static void idct32(int *out, int *tab)
190 de6d9b64 Fabrice Bellard
{
191
    int i, j;
192
    int *t, *t1, xr;
193
    const int *xp = costab32;
194
195
    for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
196 115329f1 Diego Biurrun
197 de6d9b64 Fabrice Bellard
    t = tab + 30;
198
    t1 = tab + 2;
199
    do {
200
        t[0] += t[-4];
201
        t[1] += t[1 - 4];
202
        t -= 4;
203
    } while (t != t1);
204
205
    t = tab + 28;
206
    t1 = tab + 4;
207
    do {
208
        t[0] += t[-8];
209
        t[1] += t[1-8];
210
        t[2] += t[2-8];
211
        t[3] += t[3-8];
212
        t -= 8;
213
    } while (t != t1);
214 115329f1 Diego Biurrun
215 de6d9b64 Fabrice Bellard
    t = tab;
216
    t1 = tab + 32;
217
    do {
218 115329f1 Diego Biurrun
        t[ 3] = -t[ 3];
219
        t[ 6] = -t[ 6];
220
221
        t[11] = -t[11];
222
        t[12] = -t[12];
223
        t[13] = -t[13];
224
        t[15] = -t[15];
225 de6d9b64 Fabrice Bellard
        t += 16;
226
    } while (t != t1);
227
228 115329f1 Diego Biurrun
229 de6d9b64 Fabrice Bellard
    t = tab;
230
    t1 = tab + 8;
231
    do {
232
        int x1, x2, x3, x4;
233 115329f1 Diego Biurrun
234 de6d9b64 Fabrice Bellard
        x3 = MUL(t[16], FIX(SQRT2*0.5));
235
        x4 = t[0] - x3;
236
        x3 = t[0] + x3;
237 115329f1 Diego Biurrun
238 de6d9b64 Fabrice Bellard
        x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
239
        x1 = MUL((t[8] - x2), xp[0]);
240
        x2 = MUL((t[8] + x2), xp[1]);
241
242
        t[ 0] = x3 + x1;
243
        t[ 8] = x4 - x2;
244
        t[16] = x4 + x2;
245
        t[24] = x3 - x1;
246
        t++;
247
    } while (t != t1);
248
249
    xp += 2;
250
    t = tab;
251
    t1 = tab + 4;
252
    do {
253
        xr = MUL(t[28],xp[0]);
254
        t[28] = (t[0] - xr);
255
        t[0] = (t[0] + xr);
256
257
        xr = MUL(t[4],xp[1]);
258
        t[ 4] = (t[24] - xr);
259
        t[24] = (t[24] + xr);
260 115329f1 Diego Biurrun
261 de6d9b64 Fabrice Bellard
        xr = MUL(t[20],xp[2]);
262
        t[20] = (t[8] - xr);
263
        t[ 8] = (t[8] + xr);
264 115329f1 Diego Biurrun
265 de6d9b64 Fabrice Bellard
        xr = MUL(t[12],xp[3]);
266
        t[12] = (t[16] - xr);
267
        t[16] = (t[16] + xr);
268
        t++;
269
    } while (t != t1);
270
    xp += 4;
271
272
    for (i = 0; i < 4; i++) {
273
        xr = MUL(tab[30-i*4],xp[0]);
274
        tab[30-i*4] = (tab[i*4] - xr);
275
        tab[   i*4] = (tab[i*4] + xr);
276 115329f1 Diego Biurrun
277 de6d9b64 Fabrice Bellard
        xr = MUL(tab[ 2+i*4],xp[1]);
278
        tab[ 2+i*4] = (tab[28-i*4] - xr);
279
        tab[28-i*4] = (tab[28-i*4] + xr);
280 115329f1 Diego Biurrun
281 de6d9b64 Fabrice Bellard
        xr = MUL(tab[31-i*4],xp[0]);
282
        tab[31-i*4] = (tab[1+i*4] - xr);
283
        tab[ 1+i*4] = (tab[1+i*4] + xr);
284 115329f1 Diego Biurrun
285 de6d9b64 Fabrice Bellard
        xr = MUL(tab[ 3+i*4],xp[1]);
286
        tab[ 3+i*4] = (tab[29-i*4] - xr);
287
        tab[29-i*4] = (tab[29-i*4] + xr);
288 115329f1 Diego Biurrun
289 de6d9b64 Fabrice Bellard
        xp += 2;
290
    }
291
292
    t = tab + 30;
293
    t1 = tab + 1;
294
    do {
295
        xr = MUL(t1[0], *xp);
296
        t1[0] = (t[0] - xr);
297
        t[0] = (t[0] + xr);
298
        t -= 2;
299
        t1 += 2;
300
        xp++;
301
    } while (t >= tab);
302
303
    for(i=0;i<32;i++) {
304 afa982fd Fabrice Bellard
        out[i] = tab[bitinv32[i]];
305 de6d9b64 Fabrice Bellard
    }
306
}
307
308 afa982fd Fabrice Bellard
#define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
309
310 de6d9b64 Fabrice Bellard
static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
311
{
312
    short *p, *q;
313 afa982fd Fabrice Bellard
    int sum, offset, i, j;
314
    int tmp[64];
315 de6d9b64 Fabrice Bellard
    int tmp1[32];
316
    int *out;
317
318
    //    print_pow1(samples, 1152);
319
320
    offset = s->samples_offset[ch];
321
    out = &s->sb_samples[ch][0][0][0];
322
    for(j=0;j<36;j++) {
323
        /* 32 samples at once */
324
        for(i=0;i<32;i++) {
325
            s->samples_buf[ch][offset + (31 - i)] = samples[0];
326
            samples += incr;
327
        }
328
329
        /* filter */
330
        p = s->samples_buf[ch] + offset;
331
        q = filter_bank;
332
        /* maxsum = 23169 */
333
        for(i=0;i<64;i++) {
334
            sum = p[0*64] * q[0*64];
335
            sum += p[1*64] * q[1*64];
336
            sum += p[2*64] * q[2*64];
337
            sum += p[3*64] * q[3*64];
338
            sum += p[4*64] * q[4*64];
339
            sum += p[5*64] * q[5*64];
340
            sum += p[6*64] * q[6*64];
341
            sum += p[7*64] * q[7*64];
342 afa982fd Fabrice Bellard
            tmp[i] = sum;
343 de6d9b64 Fabrice Bellard
            p++;
344
            q++;
345
        }
346 afa982fd Fabrice Bellard
        tmp1[0] = tmp[16] >> WSHIFT;
347
        for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
348
        for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
349 de6d9b64 Fabrice Bellard
350 afa982fd Fabrice Bellard
        idct32(out, tmp1);
351 de6d9b64 Fabrice Bellard
352
        /* advance of 32 samples */
353
        offset -= 32;
354
        out += 32;
355
        /* handle the wrap around */
356
        if (offset < 0) {
357 115329f1 Diego Biurrun
            memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
358 de6d9b64 Fabrice Bellard
                    s->samples_buf[ch], (512 - 32) * 2);
359
            offset = SAMPLES_BUF_SIZE - 512;
360
        }
361
    }
362
    s->samples_offset[ch] = offset;
363
364
    //    print_pow(s->sb_samples, 1152);
365
}
366
367
static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
368 115329f1 Diego Biurrun
                                  unsigned char scale_factors[SBLIMIT][3],
369 de6d9b64 Fabrice Bellard
                                  int sb_samples[3][12][SBLIMIT],
370
                                  int sblimit)
371
{
372
    int *p, vmax, v, n, i, j, k, code;
373
    int index, d1, d2;
374
    unsigned char *sf = &scale_factors[0][0];
375 115329f1 Diego Biurrun
376 de6d9b64 Fabrice Bellard
    for(j=0;j<sblimit;j++) {
377
        for(i=0;i<3;i++) {
378
            /* find the max absolute value */
379
            p = &sb_samples[i][0][j];
380
            vmax = abs(*p);
381
            for(k=1;k<12;k++) {
382
                p += SBLIMIT;
383
                v = abs(*p);
384
                if (v > vmax)
385
                    vmax = v;
386
            }
387
            /* compute the scale factor index using log 2 computations */
388
            if (vmax > 0) {
389 935442b5 Fabrice Bellard
                n = av_log2(vmax);
390 115329f1 Diego Biurrun
                /* n is the position of the MSB of vmax. now
391 de6d9b64 Fabrice Bellard
                   use at most 2 compares to find the index */
392
                index = (21 - n) * 3 - 3;
393
                if (index >= 0) {
394
                    while (vmax <= scale_factor_table[index+1])
395
                        index++;
396
                } else {
397
                    index = 0; /* very unlikely case of overflow */
398
                }
399
            } else {
400 afa982fd Fabrice Bellard
                index = 62; /* value 63 is not allowed */
401 de6d9b64 Fabrice Bellard
            }
402 afa982fd Fabrice Bellard
403 de6d9b64 Fabrice Bellard
#if 0
404 115329f1 Diego Biurrun
            printf("%2d:%d in=%x %x %d\n",
405 de6d9b64 Fabrice Bellard
                   j, i, vmax, scale_factor_table[index], index);
406
#endif
407
            /* store the scale factor */
408
            assert(index >=0 && index <= 63);
409
            sf[i] = index;
410
        }
411
412
        /* compute the transmission factor : look if the scale factors
413
           are close enough to each other */
414
        d1 = scale_diff_table[sf[0] - sf[1] + 64];
415
        d2 = scale_diff_table[sf[1] - sf[2] + 64];
416 115329f1 Diego Biurrun
417 de6d9b64 Fabrice Bellard
        /* handle the 25 cases */
418
        switch(d1 * 5 + d2) {
419
        case 0*5+0:
420
        case 0*5+4:
421
        case 3*5+4:
422
        case 4*5+0:
423
        case 4*5+4:
424
            code = 0;
425
            break;
426
        case 0*5+1:
427
        case 0*5+2:
428
        case 4*5+1:
429
        case 4*5+2:
430
            code = 3;
431
            sf[2] = sf[1];
432
            break;
433
        case 0*5+3:
434
        case 4*5+3:
435
            code = 3;
436
            sf[1] = sf[2];
437
            break;
438
        case 1*5+0:
439
        case 1*5+4:
440
        case 2*5+4:
441
            code = 1;
442
            sf[1] = sf[0];
443
            break;
444
        case 1*5+1:
445
        case 1*5+2:
446
        case 2*5+0:
447
        case 2*5+1:
448
        case 2*5+2:
449
            code = 2;
450
            sf[1] = sf[2] = sf[0];
451
            break;
452
        case 2*5+3:
453
        case 3*5+3:
454
            code = 2;
455
            sf[0] = sf[1] = sf[2];
456
            break;
457
        case 3*5+0:
458
        case 3*5+1:
459
        case 3*5+2:
460
            code = 2;
461
            sf[0] = sf[2] = sf[1];
462
            break;
463
        case 1*5+3:
464
            code = 2;
465
            if (sf[0] > sf[2])
466
              sf[0] = sf[2];
467
            sf[1] = sf[2] = sf[0];
468
            break;
469
        default:
470 9fe5a7b8 Michael Niedermayer
            assert(0); //cant happen
471 88730be6 Måns Rullgård
            code = 0;           /* kill warning */
472 de6d9b64 Fabrice Bellard
        }
473 115329f1 Diego Biurrun
474 de6d9b64 Fabrice Bellard
#if 0
475 115329f1 Diego Biurrun
        printf("%d: %2d %2d %2d %d %d -> %d\n", j,
476 de6d9b64 Fabrice Bellard
               sf[0], sf[1], sf[2], d1, d2, code);
477
#endif
478
        scale_code[j] = code;
479
        sf += 3;
480
    }
481
}
482
483
/* The most important function : psycho acoustic module. In this
484
   encoder there is basically none, so this is the worst you can do,
485
   but also this is the simpler. */
486
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
487
{
488
    int i;
489
490
    for(i=0;i<s->sblimit;i++) {
491
        smr[i] = (int)(fixed_smr[i] * 10);
492
    }
493
}
494
495
496
#define SB_NOTALLOCATED  0
497
#define SB_ALLOCATED     1
498
#define SB_NOMORE        2
499
500
/* Try to maximize the smr while using a number of bits inferior to
501
   the frame size. I tried to make the code simpler, faster and
502
   smaller than other encoders :-) */
503 115329f1 Diego Biurrun
static void compute_bit_allocation(MpegAudioContext *s,
504 de6d9b64 Fabrice Bellard
                                   short smr1[MPA_MAX_CHANNELS][SBLIMIT],
505
                                   unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
506
                                   int *padding)
507
{
508
    int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
509
    int incr;
510
    short smr[MPA_MAX_CHANNELS][SBLIMIT];
511
    unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
512
    const unsigned char *alloc;
513
514
    memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
515
    memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
516
    memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
517 115329f1 Diego Biurrun
518 de6d9b64 Fabrice Bellard
    /* compute frame size and padding */
519
    max_frame_size = s->frame_size;
520
    s->frame_frac += s->frame_frac_incr;
521
    if (s->frame_frac >= 65536) {
522
        s->frame_frac -= 65536;
523
        s->do_padding = 1;
524
        max_frame_size += 8;
525
    } else {
526
        s->do_padding = 0;
527
    }
528
529
    /* compute the header + bit alloc size */
530
    current_frame_size = 32;
531
    alloc = s->alloc_table;
532
    for(i=0;i<s->sblimit;i++) {
533
        incr = alloc[0];
534
        current_frame_size += incr * s->nb_channels;
535
        alloc += 1 << incr;
536
    }
537
    for(;;) {
538
        /* look for the subband with the largest signal to mask ratio */
539
        max_sb = -1;
540
        max_ch = -1;
541
        max_smr = 0x80000000;
542
        for(ch=0;ch<s->nb_channels;ch++) {
543
            for(i=0;i<s->sblimit;i++) {
544
                if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
545
                    max_smr = smr[ch][i];
546
                    max_sb = i;
547
                    max_ch = ch;
548
                }
549
            }
550
        }
551
#if 0
552 115329f1 Diego Biurrun
        printf("current=%d max=%d max_sb=%d alloc=%d\n",
553 de6d9b64 Fabrice Bellard
               current_frame_size, max_frame_size, max_sb,
554
               bit_alloc[max_sb]);
555 115329f1 Diego Biurrun
#endif
556 de6d9b64 Fabrice Bellard
        if (max_sb < 0)
557
            break;
558 115329f1 Diego Biurrun
559 de6d9b64 Fabrice Bellard
        /* find alloc table entry (XXX: not optimal, should use
560
           pointer table) */
561
        alloc = s->alloc_table;
562
        for(i=0;i<max_sb;i++) {
563
            alloc += 1 << alloc[0];
564
        }
565
566
        if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
567
            /* nothing was coded for this band: add the necessary bits */
568
            incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
569
            incr += total_quant_bits[alloc[1]];
570
        } else {
571
            /* increments bit allocation */
572
            b = bit_alloc[max_ch][max_sb];
573 115329f1 Diego Biurrun
            incr = total_quant_bits[alloc[b + 1]] -
574 de6d9b64 Fabrice Bellard
                total_quant_bits[alloc[b]];
575
        }
576
577
        if (current_frame_size + incr <= max_frame_size) {
578
            /* can increase size */
579
            b = ++bit_alloc[max_ch][max_sb];
580
            current_frame_size += incr;
581
            /* decrease smr by the resolution we added */
582
            smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
583
            /* max allocation size reached ? */
584
            if (b == ((1 << alloc[0]) - 1))
585
                subband_status[max_ch][max_sb] = SB_NOMORE;
586
            else
587
                subband_status[max_ch][max_sb] = SB_ALLOCATED;
588
        } else {
589
            /* cannot increase the size of this subband */
590
            subband_status[max_ch][max_sb] = SB_NOMORE;
591
        }
592
    }
593
    *padding = max_frame_size - current_frame_size;
594
    assert(*padding >= 0);
595
596
#if 0
597
    for(i=0;i<s->sblimit;i++) {
598
        printf("%d ", bit_alloc[i]);
599
    }
600
    printf("\n");
601
#endif
602
}
603
604
/*
605
 * Output the mpeg audio layer 2 frame. Note how the code is small
606
 * compared to other encoders :-)
607
 */
608
static void encode_frame(MpegAudioContext *s,
609
                         unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
610
                         int padding)
611
{
612
    int i, j, k, l, bit_alloc_bits, b, ch;
613
    unsigned char *sf;
614
    int q[3];
615
    PutBitContext *p = &s->pb;
616
617
    /* header */
618
619
    put_bits(p, 12, 0xfff);
620
    put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
621
    put_bits(p, 2, 4-2);  /* layer 2 */
622
    put_bits(p, 1, 1); /* no error protection */
623
    put_bits(p, 4, s->bitrate_index);
624
    put_bits(p, 2, s->freq_index);
625
    put_bits(p, 1, s->do_padding); /* use padding */
626
    put_bits(p, 1, 0);             /* private_bit */
627
    put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
628
    put_bits(p, 2, 0); /* mode_ext */
629
    put_bits(p, 1, 0); /* no copyright */
630
    put_bits(p, 1, 1); /* original */
631
    put_bits(p, 2, 0); /* no emphasis */
632
633
    /* bit allocation */
634
    j = 0;
635
    for(i=0;i<s->sblimit;i++) {
636
        bit_alloc_bits = s->alloc_table[j];
637
        for(ch=0;ch<s->nb_channels;ch++) {
638
            put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
639
        }
640
        j += 1 << bit_alloc_bits;
641
    }
642 115329f1 Diego Biurrun
643 de6d9b64 Fabrice Bellard
    /* scale codes */
644
    for(i=0;i<s->sblimit;i++) {
645
        for(ch=0;ch<s->nb_channels;ch++) {
646 115329f1 Diego Biurrun
            if (bit_alloc[ch][i])
647 de6d9b64 Fabrice Bellard
                put_bits(p, 2, s->scale_code[ch][i]);
648
        }
649
    }
650
651
    /* scale factors */
652
    for(i=0;i<s->sblimit;i++) {
653
        for(ch=0;ch<s->nb_channels;ch++) {
654
            if (bit_alloc[ch][i]) {
655
                sf = &s->scale_factors[ch][i][0];
656
                switch(s->scale_code[ch][i]) {
657
                case 0:
658
                    put_bits(p, 6, sf[0]);
659
                    put_bits(p, 6, sf[1]);
660
                    put_bits(p, 6, sf[2]);
661
                    break;
662
                case 3:
663
                case 1:
664
                    put_bits(p, 6, sf[0]);
665
                    put_bits(p, 6, sf[2]);
666
                    break;
667
                case 2:
668
                    put_bits(p, 6, sf[0]);
669
                    break;
670
                }
671
            }
672
        }
673
    }
674 115329f1 Diego Biurrun
675 de6d9b64 Fabrice Bellard
    /* quantization & write sub band samples */
676
677
    for(k=0;k<3;k++) {
678
        for(l=0;l<12;l+=3) {
679
            j = 0;
680
            for(i=0;i<s->sblimit;i++) {
681
                bit_alloc_bits = s->alloc_table[j];
682
                for(ch=0;ch<s->nb_channels;ch++) {
683
                    b = bit_alloc[ch][i];
684
                    if (b) {
685
                        int qindex, steps, m, sample, bits;
686
                        /* we encode 3 sub band samples of the same sub band at a time */
687
                        qindex = s->alloc_table[j+b];
688
                        steps = quant_steps[qindex];
689
                        for(m=0;m<3;m++) {
690
                            sample = s->sb_samples[ch][k][l + m][i];
691
                            /* divide by scale factor */
692
#ifdef USE_FLOATS
693
                            {
694
                                float a;
695
                                a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
696
                                q[m] = (int)((a + 1.0) * steps * 0.5);
697
                            }
698
#else
699
                            {
700
                                int q1, e, shift, mult;
701
                                e = s->scale_factors[ch][i][k];
702
                                shift = scale_factor_shift[e];
703
                                mult = scale_factor_mult[e];
704 115329f1 Diego Biurrun
705 de6d9b64 Fabrice Bellard
                                /* normalize to P bits */
706
                                if (shift < 0)
707
                                    q1 = sample << (-shift);
708
                                else
709
                                    q1 = sample >> shift;
710
                                q1 = (q1 * mult) >> P;
711
                                q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
712
                            }
713
#endif
714
                            if (q[m] >= steps)
715
                                q[m] = steps - 1;
716
                            assert(q[m] >= 0 && q[m] < steps);
717
                        }
718
                        bits = quant_bits[qindex];
719
                        if (bits < 0) {
720
                            /* group the 3 values to save bits */
721 115329f1 Diego Biurrun
                            put_bits(p, -bits,
722 de6d9b64 Fabrice Bellard
                                     q[0] + steps * (q[1] + steps * q[2]));
723
#if 0
724 115329f1 Diego Biurrun
                            printf("%d: gr1 %d\n",
725 de6d9b64 Fabrice Bellard
                                   i, q[0] + steps * (q[1] + steps * q[2]));
726
#endif
727
                        } else {
728
#if 0
729 115329f1 Diego Biurrun
                            printf("%d: gr3 %d %d %d\n",
730 de6d9b64 Fabrice Bellard
                                   i, q[0], q[1], q[2]);
731 115329f1 Diego Biurrun
#endif
732 de6d9b64 Fabrice Bellard
                            put_bits(p, bits, q[0]);
733
                            put_bits(p, bits, q[1]);
734
                            put_bits(p, bits, q[2]);
735
                        }
736
                    }
737
                }
738
                /* next subband in alloc table */
739 115329f1 Diego Biurrun
                j += 1 << bit_alloc_bits;
740 de6d9b64 Fabrice Bellard
            }
741
        }
742
    }
743
744
    /* padding */
745
    for(i=0;i<padding;i++)
746
        put_bits(p, 1, 0);
747
748
    /* flush */
749
    flush_put_bits(p);
750
}
751
752 5c91a675 Zdenek Kabelac
static int MPA_encode_frame(AVCodecContext *avctx,
753 bb270c08 Diego Biurrun
                            unsigned char *frame, int buf_size, void *data)
754 de6d9b64 Fabrice Bellard
{
755
    MpegAudioContext *s = avctx->priv_data;
756
    short *samples = data;
757
    short smr[MPA_MAX_CHANNELS][SBLIMIT];
758
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
759
    int padding, i;
760
761
    for(i=0;i<s->nb_channels;i++) {
762
        filter(s, i, samples + i, s->nb_channels);
763
    }
764
765
    for(i=0;i<s->nb_channels;i++) {
766 115329f1 Diego Biurrun
        compute_scale_factors(s->scale_code[i], s->scale_factors[i],
767 de6d9b64 Fabrice Bellard
                              s->sb_samples[i], s->sblimit);
768
    }
769
    for(i=0;i<s->nb_channels;i++) {
770
        psycho_acoustic_model(s, smr[i]);
771
    }
772
    compute_bit_allocation(s, smr, bit_alloc, &padding);
773
774 ed7debda Alex Beregszaszi
    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
775 de6d9b64 Fabrice Bellard
776
    encode_frame(s, bit_alloc, padding);
777 115329f1 Diego Biurrun
778 de6d9b64 Fabrice Bellard
    s->nb_samples += MPA_FRAME_SIZE;
779 17592475 Michael Niedermayer
    return pbBufPtr(&s->pb) - s->pb.buf;
780 de6d9b64 Fabrice Bellard
}
781
782 492cd3a9 Michael Niedermayer
static int MPA_encode_close(AVCodecContext *avctx)
783
{
784
    av_freep(&avctx->coded_frame);
785 8e1e6f31 Fabrice Bellard
    return 0;
786 492cd3a9 Michael Niedermayer
}
787 de6d9b64 Fabrice Bellard
788 983ea0bc Roine Gustafsson
#ifdef CONFIG_MP2_ENCODER
789 de6d9b64 Fabrice Bellard
AVCodec mp2_encoder = {
790
    "mp2",
791
    CODEC_TYPE_AUDIO,
792
    CODEC_ID_MP2,
793
    sizeof(MpegAudioContext),
794
    MPA_encode_init,
795
    MPA_encode_frame,
796 492cd3a9 Michael Niedermayer
    MPA_encode_close,
797 de6d9b64 Fabrice Bellard
    NULL,
798
};
799 983ea0bc Roine Gustafsson
#endif // CONFIG_MP2_ENCODER
800 cd4af68a Zdenek Kabelac
801
#undef FIX