Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3enc.c @ 0a0a8500

History | View | Annotate | Download (48.2 KB)

1
/*
2
 * The simplest AC-3 encoder
3
 * Copyright (c) 2000 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 AC-3 encoder.
25
 */
26

    
27
//#define DEBUG
28

    
29
#include "libavcore/audioconvert.h"
30
#include "libavutil/crc.h"
31
#include "avcodec.h"
32
#include "put_bits.h"
33
#include "ac3.h"
34
#include "audioconvert.h"
35

    
36

    
37
#define MDCT_NBITS 9
38
#define MDCT_SAMPLES (1 << MDCT_NBITS)
39

    
40
/** Scale a float value by 2^bits and convert to an integer. */
41
#define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
42

    
43
/** Scale a float value by 2^15, convert to an integer, and clip to int16_t range. */
44
#define FIX15(a) av_clip_int16(SCALE_FLOAT(a, 15))
45

    
46

    
47
/**
48
 * Compex number.
49
 * Used in fixed-point MDCT calculation.
50
 */
51
typedef struct IComplex {
52
    int16_t re,im;
53
} IComplex;
54

    
55
/**
56
 * AC-3 encoder private context.
57
 */
58
typedef struct AC3EncodeContext {
59
    PutBitContext pb;                       ///< bitstream writer context
60

    
61
    int bitstream_id;                       ///< bitstream id                           (bsid)
62
    int bitstream_mode;                     ///< bitstream mode                         (bsmod)
63

    
64
    int bit_rate;                           ///< target bit rate, in bits-per-second
65
    int sample_rate;                        ///< sampling frequency, in Hz
66

    
67
    int frame_size_min;                     ///< minimum frame size in case rounding is necessary
68
    int frame_size;                         ///< current frame size in bytes
69
    int frame_size_code;                    ///< frame size code                        (frmsizecod)
70
    int bits_written;                       ///< bit count    (used to avg. bitrate)
71
    int samples_written;                    ///< sample count (used to avg. bitrate)
72

    
73
    int fbw_channels;                       ///< number of full-bandwidth channels      (nfchans)
74
    int channels;                           ///< total number of channels               (nchans)
75
    int lfe_on;                             ///< indicates if there is an LFE channel   (lfeon)
76
    int lfe_channel;                        ///< channel index of the LFE channel
77
    int channel_mode;                       ///< channel mode                           (acmod)
78
    const uint8_t *channel_map;             ///< channel map used to reorder channels
79

    
80
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
81
    int nb_coefs[AC3_MAX_CHANNELS];
82

    
83
    /* bitrate allocation control */
84
    int slow_gain_code;                     ///< slow gain code                         (sgaincod)
85
    int slow_decay_code;                    ///< slow decay code                        (sdcycod)
86
    int fast_decay_code;                    ///< fast decay code                        (fdcycod)
87
    int db_per_bit_code;                    ///< dB/bit code                            (dbpbcod)
88
    int floor_code;                         ///< floor code                             (floorcod)
89
    AC3BitAllocParameters bit_alloc;        ///< bit allocation parameters
90
    int coarse_snr_offset;                  ///< coarse SNR offsets                     (csnroffst)
91
    int fast_gain_code[AC3_MAX_CHANNELS];   ///< fast gain codes (signal-to-mask ratio) (fgaincod)
92
    int fine_snr_offset[AC3_MAX_CHANNELS];  ///< fine SNR offsets                       (fsnroffst)
93

    
94
    /* mantissa encoding */
95
    int mant1_cnt, mant2_cnt, mant4_cnt;    ///< mantissa counts for bap=1,2,4
96

    
97
    int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; ///< last 256 samples from previous frame
98
} AC3EncodeContext;
99

    
100

    
101
/** MDCT and FFT tables */
102
static int16_t costab[64];
103
static int16_t sintab[64];
104
static int16_t xcos1[128];
105
static int16_t xsin1[128];
106

    
107

    
108
/**
109
 * Deinterleave input samples.
110
 * Channels are reordered from FFmpeg's default order to AC-3 order.
111
 */
112
static void deinterleave_input_samples(AC3EncodeContext *s,
113
                                       const int16_t *samples,
114
                                       int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE])
115
{
116
    int ch, i;
117

    
118
    /* deinterleave and remap input samples */
119
    for (ch = 0; ch < s->channels; ch++) {
120
        const int16_t *sptr;
121
        int sinc;
122

    
123
        /* copy last 256 samples of previous frame to the start of the current frame */
124
        memcpy(&planar_samples[ch][0], s->last_samples[ch],
125
               AC3_BLOCK_SIZE * sizeof(planar_samples[0][0]));
126

    
127
        /* deinterleave */
128
        sinc = s->channels;
129
        sptr = samples + s->channel_map[ch];
130
        for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
131
            planar_samples[ch][i] = *sptr;
132
            sptr += sinc;
133
        }
134

    
135
        /* save last 256 samples for next frame */
136
        memcpy(s->last_samples[ch], &planar_samples[ch][6* AC3_BLOCK_SIZE],
137
               AC3_BLOCK_SIZE * sizeof(planar_samples[0][0]));
138
    }
139
}
140

    
141

    
142
/**
143
 * Initialize FFT tables.
144
 * @param ln log2(FFT size)
145
 */
146
static av_cold void fft_init(int ln)
147
{
148
    int i, n, n2;
149
    float alpha;
150

    
151
    n  = 1 << ln;
152
    n2 = n >> 1;
153

    
154
    for (i = 0; i < n2; i++) {
155
        alpha     = 2.0 * M_PI * i / n;
156
        costab[i] = FIX15(cos(alpha));
157
        sintab[i] = FIX15(sin(alpha));
158
    }
159
}
160

    
161

    
162
/**
163
 * Initialize MDCT tables.
164
 * @param nbits log2(MDCT size)
165
 */
166
static av_cold void mdct_init(int nbits)
167
{
168
    int i, n, n4;
169

    
170
    n  = 1 << nbits;
171
    n4 = n >> 2;
172

    
173
    fft_init(nbits - 2);
174

    
175
    for (i = 0; i < n4; i++) {
176
        float alpha = 2.0 * M_PI * (i + 1.0 / 8.0) / n;
177
        xcos1[i] = FIX15(-cos(alpha));
178
        xsin1[i] = FIX15(-sin(alpha));
179
    }
180
}
181

    
182

    
183
/** Butterfly op */
184
#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1)  \
185
{                                                       \
186
  int ax, ay, bx, by;                                   \
187
  bx  = pre1;                                           \
188
  by  = pim1;                                           \
189
  ax  = qre1;                                           \
190
  ay  = qim1;                                           \
191
  pre = (bx + ax) >> 1;                                 \
192
  pim = (by + ay) >> 1;                                 \
193
  qre = (bx - ax) >> 1;                                 \
194
  qim = (by - ay) >> 1;                                 \
195
}
196

    
197

    
198
/** Complex multiply */
199
#define CMUL(pre, pim, are, aim, bre, bim)              \
200
{                                                       \
201
   pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;     \
202
   pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;     \
203
}
204

    
205

    
206
/**
207
 * Calculate a 2^n point complex FFT on 2^ln points.
208
 * @param z  complex input/output samples
209
 * @param ln log2(FFT size)
210
 */
211
static void fft(IComplex *z, int ln)
212
{
213
    int j, l, np, np2;
214
    int nblocks, nloops;
215
    register IComplex *p,*q;
216
    int tmp_re, tmp_im;
217

    
218
    np = 1 << ln;
219

    
220
    /* reverse */
221
    for (j = 0; j < np; j++) {
222
        int k = av_reverse[j] >> (8 - ln);
223
        if (k < j)
224
            FFSWAP(IComplex, z[k], z[j]);
225
    }
226

    
227
    /* pass 0 */
228

    
229
    p = &z[0];
230
    j = np >> 1;
231
    do {
232
        BF(p[0].re, p[0].im, p[1].re, p[1].im,
233
           p[0].re, p[0].im, p[1].re, p[1].im);
234
        p += 2;
235
    } while (--j);
236

    
237
    /* pass 1 */
238

    
239
    p = &z[0];
240
    j = np >> 2;
241
    do {
242
        BF(p[0].re, p[0].im, p[2].re,  p[2].im,
243
           p[0].re, p[0].im, p[2].re,  p[2].im);
244
        BF(p[1].re, p[1].im, p[3].re,  p[3].im,
245
           p[1].re, p[1].im, p[3].im, -p[3].re);
246
        p+=4;
247
    } while (--j);
248

    
249
    /* pass 2 .. ln-1 */
250

    
251
    nblocks = np >> 3;
252
    nloops  =  1 << 2;
253
    np2     = np >> 1;
254
    do {
255
        p = z;
256
        q = z + nloops;
257
        for (j = 0; j < nblocks; j++) {
258
            BF(p->re, p->im, q->re, q->im,
259
               p->re, p->im, q->re, q->im);
260
            p++;
261
            q++;
262
            for(l = nblocks; l < np2; l += nblocks) {
263
                CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
264
                BF(p->re, p->im, q->re,  q->im,
265
                   p->re, p->im, tmp_re, tmp_im);
266
                p++;
267
                q++;
268
            }
269
            p += nloops;
270
            q += nloops;
271
        }
272
        nblocks = nblocks >> 1;
273
        nloops  = nloops  << 1;
274
    } while (nblocks);
275
}
276

    
277

    
278
/**
279
 * Calculate a 512-point MDCT
280
 * @param out 256 output frequency coefficients
281
 * @param in  512 windowed input audio samples
282
 */
283
static void mdct512(int32_t *out, int16_t *in)
284
{
285
    int i, re, im, re1, im1;
286
    int16_t rot[MDCT_SAMPLES];
287
    IComplex x[MDCT_SAMPLES/4];
288

    
289
    /* shift to simplify computations */
290
    for (i = 0; i < MDCT_SAMPLES/4; i++)
291
        rot[i] = -in[i + 3*MDCT_SAMPLES/4];
292
    for (;i < MDCT_SAMPLES; i++)
293
        rot[i] =  in[i -   MDCT_SAMPLES/4];
294

    
295
    /* pre rotation */
296
    for (i = 0; i < MDCT_SAMPLES/4; i++) {
297
        re =  ((int)rot[               2*i] - (int)rot[MDCT_SAMPLES  -1-2*i]) >> 1;
298
        im = -((int)rot[MDCT_SAMPLES/2+2*i] - (int)rot[MDCT_SAMPLES/2-1-2*i]) >> 1;
299
        CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
300
    }
301

    
302
    fft(x, MDCT_NBITS - 2);
303

    
304
    /* post rotation */
305
    for (i = 0; i < MDCT_SAMPLES/4; i++) {
306
        re = x[i].re;
307
        im = x[i].im;
308
        CMUL(re1, im1, re, im, xsin1[i], xcos1[i]);
309
        out[                 2*i] = im1;
310
        out[MDCT_SAMPLES/2-1-2*i] = re1;
311
    }
312
}
313

    
314

    
315
/**
316
 * Apply KBD window to input samples prior to MDCT.
317
 */
318
static void apply_window(int16_t *output, const int16_t *input,
319
                         const int16_t *window, int n)
320
{
321
    int i;
322
    int n2 = n >> 1;
323

    
324
    for (i = 0; i < n2; i++) {
325
        output[i]     = MUL16(input[i],     window[i]) >> 15;
326
        output[n-i-1] = MUL16(input[n-i-1], window[i]) >> 15;
327
    }
328
}
329

    
330

    
331
/**
332
 * Calculate the log2() of the maximum absolute value in an array.
333
 * @param tab input array
334
 * @param n   number of values in the array
335
 * @return    log2(max(abs(tab[])))
336
 */
337
static int log2_tab(int16_t *tab, int n)
338
{
339
    int i, v;
340

    
341
    v = 0;
342
    for (i = 0; i < n; i++)
343
        v |= abs(tab[i]);
344

    
345
    return av_log2(v);
346
}
347

    
348

    
349
/**
350
 * Left-shift each value in an array by a specified amount.
351
 * @param tab    input array
352
 * @param n      number of values in the array
353
 * @param lshift left shift amount. a negative value means right shift.
354
 */
355
static void lshift_tab(int16_t *tab, int n, int lshift)
356
{
357
    int i;
358

    
359
    if (lshift > 0) {
360
        for(i = 0; i < n; i++)
361
            tab[i] <<= lshift;
362
    } else if (lshift < 0) {
363
        lshift = -lshift;
364
        for (i = 0; i < n; i++)
365
            tab[i] >>= lshift;
366
    }
367
}
368

    
369

    
370
/**
371
 * Normalize the input samples to use the maximum available precision.
372
 * This assumes signed 16-bit input samples. Exponents are reduced by 9 to
373
 * match the 24-bit internal precision for MDCT coefficients.
374
 *
375
 * @return exponent shift
376
 */
377
static int normalize_samples(AC3EncodeContext *s,
378
                             int16_t windowed_samples[AC3_WINDOW_SIZE])
379
{
380
    int v = 14 - log2_tab(windowed_samples, AC3_WINDOW_SIZE);
381
    v = FFMAX(0, v);
382
    lshift_tab(windowed_samples, AC3_WINDOW_SIZE, v);
383
    return v - 9;
384
}
385

    
386

    
387
/**
388
 * Apply the MDCT to input samples to generate frequency coefficients.
389
 * This applies the KBD window and normalizes the input to reduce precision
390
 * loss due to fixed-point calculations.
391
 */
392
static void apply_mdct(AC3EncodeContext *s,
393
                       int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE],
394
                       int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
395
                       int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
396
{
397
    int blk, ch;
398
    int16_t windowed_samples[AC3_WINDOW_SIZE];
399

    
400
    for (ch = 0; ch < s->channels; ch++) {
401
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
402
            const int16_t *input_samples = &planar_samples[ch][blk * AC3_BLOCK_SIZE];
403

    
404
            apply_window(windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE);
405

    
406
            exp_shift[blk][ch] = normalize_samples(s, windowed_samples);
407

    
408
            mdct512(mdct_coef[blk][ch], windowed_samples);
409
        }
410
    }
411
}
412

    
413

    
414
/**
415
 * Calculate the sum of absolute differences (SAD) between 2 sets of exponents.
416
 */
417
static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
418
{
419
    int sum, i;
420
    sum = 0;
421
    for (i = 0; i < n; i++)
422
        sum += abs(exp1[i] - exp2[i]);
423
    return sum;
424
}
425

    
426

    
427
/**
428
 * Exponent Difference Threshold.
429
 * New exponents are sent if their SAD exceed this number.
430
 */
431
#define EXP_DIFF_THRESHOLD 1000
432

    
433

    
434
/**
435
 * Calculate exponent strategies for all blocks in a single channel.
436
 */
437
static void compute_exp_strategy_ch(uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
438
                                    uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
439
                                    int ch, int is_lfe)
440
{
441
    int blk, blk1;
442
    int exp_diff;
443

    
444
    /* estimate if the exponent variation & decide if they should be
445
       reused in the next frame */
446
    exp_strategy[0][ch] = EXP_NEW;
447
    for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
448
        exp_diff = calc_exp_diff(exp[blk][ch], exp[blk-1][ch], AC3_MAX_COEFS);
449
        if (exp_diff > EXP_DIFF_THRESHOLD)
450
            exp_strategy[blk][ch] = EXP_NEW;
451
        else
452
            exp_strategy[blk][ch] = EXP_REUSE;
453
    }
454
    if (is_lfe)
455
        return;
456

    
457
    /* now select the encoding strategy type : if exponents are often
458
       recoded, we use a coarse encoding */
459
    blk = 0;
460
    while (blk < AC3_MAX_BLOCKS) {
461
        blk1 = blk + 1;
462
        while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1][ch] == EXP_REUSE)
463
            blk1++;
464
        switch (blk1 - blk) {
465
        case 1:  exp_strategy[blk][ch] = EXP_D45; break;
466
        case 2:
467
        case 3:  exp_strategy[blk][ch] = EXP_D25; break;
468
        default: exp_strategy[blk][ch] = EXP_D15; break;
469
        }
470
        blk = blk1;
471
    }
472
}
473

    
474

    
475
/**
476
 * Set each encoded exponent in a block to the minimum of itself and the
477
 * exponent in the same frequency bin of a following block.
478
 * exp[i] = min(exp[i], exp1[i]
479
 */
480
static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS], int n)
481
{
482
    int i;
483
    for (i = 0; i < n; i++) {
484
        if (exp1[i] < exp[i])
485
            exp[i] = exp1[i];
486
    }
487
}
488

    
489

    
490
/**
491
 * Update the exponents so that they are the ones the decoder will decode.
492
 * @return the number of bits used to encode the exponents.
493
 */
494
static int encode_exponents_blk_ch(uint8_t encoded_exp[AC3_MAX_COEFS],
495
                                   uint8_t exp[AC3_MAX_COEFS],
496
                                   int nb_exps, int exp_strategy)
497
{
498
    int group_size, nb_groups, i, j, k, exp_min;
499
    uint8_t exp1[AC3_MAX_COEFS];
500

    
501
    group_size = exp_strategy + (exp_strategy == EXP_D45);
502
    nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3;
503

    
504
    /* for each group, compute the minimum exponent */
505
    exp1[0] = exp[0]; /* DC exponent is handled separately */
506
    k = 1;
507
    for (i = 1; i <= nb_groups; i++) {
508
        exp_min = exp[k];
509
        assert(exp_min >= 0 && exp_min <= 24);
510
        for (j = 1; j < group_size; j++) {
511
            if (exp[k+j] < exp_min)
512
                exp_min = exp[k+j];
513
        }
514
        exp1[i] = exp_min;
515
        k += group_size;
516
    }
517

    
518
    /* constraint for DC exponent */
519
    if (exp1[0] > 15)
520
        exp1[0] = 15;
521

    
522
    /* decrease the delta between each groups to within 2 so that they can be
523
       differentially encoded */
524
    for (i = 1; i <= nb_groups; i++)
525
        exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
526
    for (i = nb_groups-1; i >= 0; i--)
527
        exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2);
528

    
529
    /* now we have the exponent values the decoder will see */
530
    encoded_exp[0] = exp1[0];
531
    k = 1;
532
    for (i = 1; i <= nb_groups; i++) {
533
        for (j = 0; j < group_size; j++)
534
            encoded_exp[k+j] = exp1[i];
535
        k += group_size;
536
    }
537

    
538
    return 4 + (nb_groups / 3) * 7;
539
}
540

    
541

    
542
/**
543
 * Calculate the number of bits needed to encode a set of mantissas.
544
 */
545
static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs)
546
{
547
    int bits, mant, i;
548

    
549
    bits = 0;
550
    for (i = 0; i < nb_coefs; i++) {
551
        mant = m[i];
552
        switch (mant) {
553
        case 0:
554
            /* nothing */
555
            break;
556
        case 1:
557
            /* 3 mantissa in 5 bits */
558
            if (s->mant1_cnt == 0)
559
                bits += 5;
560
            if (++s->mant1_cnt == 3)
561
                s->mant1_cnt = 0;
562
            break;
563
        case 2:
564
            /* 3 mantissa in 7 bits */
565
            if (s->mant2_cnt == 0)
566
                bits += 7;
567
            if (++s->mant2_cnt == 3)
568
                s->mant2_cnt = 0;
569
            break;
570
        case 3:
571
            bits += 3;
572
            break;
573
        case 4:
574
            /* 2 mantissa in 7 bits */
575
            if (s->mant4_cnt == 0)
576
                bits += 7;
577
            if (++s->mant4_cnt == 2)
578
                s->mant4_cnt = 0;
579
            break;
580
        case 14:
581
            bits += 14;
582
            break;
583
        case 15:
584
            bits += 16;
585
            break;
586
        default:
587
            bits += mant - 1;
588
            break;
589
        }
590
    }
591
    return bits;
592
}
593

    
594

    
595
/**
596
 * Calculate masking curve based on the final exponents.
597
 * Also calculate the power spectral densities to use in future calculations.
598
 */
599
static void bit_alloc_masking(AC3EncodeContext *s,
600
                              uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
601
                              uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
602
                              int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
603
                              int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS])
604
{
605
    int blk, ch;
606
    int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS];
607

    
608
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
609
        for (ch = 0; ch < s->channels; ch++) {
610
            if(exp_strategy[blk][ch] == EXP_REUSE) {
611
                memcpy(psd[blk][ch],  psd[blk-1][ch],  AC3_MAX_COEFS*sizeof(psd[0][0][0]));
612
                memcpy(mask[blk][ch], mask[blk-1][ch], AC3_CRITICAL_BANDS*sizeof(mask[0][0][0]));
613
            } else {
614
                ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0,
615
                                          s->nb_coefs[ch],
616
                                          psd[blk][ch], band_psd[blk][ch]);
617
                ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, band_psd[blk][ch],
618
                                           0, s->nb_coefs[ch],
619
                                           ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
620
                                           ch == s->lfe_channel,
621
                                           DBA_NONE, 0, NULL, NULL, NULL,
622
                                           mask[blk][ch]);
623
            }
624
        }
625
    }
626
}
627

    
628

    
629
/**
630
 * Run the bit allocation with a given SNR offset.
631
 * This calculates the bit allocation pointers that will be used to determine
632
 * the quantization of each mantissa.
633
 * @return the number of remaining bits (positive or negative) if the given
634
 *         SNR offset is used to quantize the mantissas.
635
 */
636
static int bit_alloc(AC3EncodeContext *s,
637
                     int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS],
638
                     int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
639
                     uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
640
                     int frame_bits, int coarse_snr_offset, int fine_snr_offset)
641
{
642
    int blk, ch;
643
    int snr_offset;
644

    
645
    snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2;
646

    
647
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
648
        s->mant1_cnt = 0;
649
        s->mant2_cnt = 0;
650
        s->mant4_cnt = 0;
651
        for (ch = 0; ch < s->channels; ch++) {
652
            ff_ac3_bit_alloc_calc_bap(mask[blk][ch], psd[blk][ch], 0,
653
                                      s->nb_coefs[ch], snr_offset,
654
                                      s->bit_alloc.floor, ff_ac3_bap_tab,
655
                                      bap[blk][ch]);
656
            frame_bits += compute_mantissa_size(s, bap[blk][ch], s->nb_coefs[ch]);
657
        }
658
    }
659
    return 8 * s->frame_size - frame_bits;
660
}
661

    
662

    
663
#define SNR_INC1 4
664

    
665
/**
666
 * Perform bit allocation search.
667
 * Finds the SNR offset value that maximizes quality and fits in the specified
668
 * frame size.  Output is the SNR offset and a set of bit allocation pointers
669
 * used to quantize the mantissas.
670
 */
671
static int compute_bit_allocation(AC3EncodeContext *s,
672
                                  uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
673
                                  uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
674
                                  uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
675
                                  int frame_bits)
676
{
677
    int blk, ch;
678
    int coarse_snr_offset, fine_snr_offset;
679
    uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
680
    int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
681
    int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS];
682
    static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
683

    
684
    /* init default parameters */
685
    s->slow_decay_code = 2;
686
    s->fast_decay_code = 1;
687
    s->slow_gain_code  = 1;
688
    s->db_per_bit_code = 2;
689
    s->floor_code      = 4;
690
    for (ch = 0; ch < s->channels; ch++)
691
        s->fast_gain_code[ch] = 4;
692

    
693
    /* compute real values */
694
    s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
695
    s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
696
    s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];
697
    s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
698
    s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
699

    
700
    /* header size */
701
    frame_bits += 65;
702
    // if (s->channel_mode == 2)
703
    //    frame_bits += 2;
704
    frame_bits += frame_bits_inc[s->channel_mode];
705

    
706
    /* audio blocks */
707
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
708
        frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
709
        if (s->channel_mode == AC3_CHMODE_STEREO) {
710
            frame_bits++; /* rematstr */
711
            if (!blk)
712
                frame_bits += 4;
713
        }
714
        frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
715
        if (s->lfe_on)
716
            frame_bits++; /* lfeexpstr */
717
        for (ch = 0; ch < s->fbw_channels; ch++) {
718
            if (exp_strategy[blk][ch] != EXP_REUSE)
719
                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
720
        }
721
        frame_bits++; /* baie */
722
        frame_bits++; /* snr */
723
        frame_bits += 2; /* delta / skip */
724
    }
725
    frame_bits++; /* cplinu for block 0 */
726
    /* bit alloc info */
727
    /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
728
    /* csnroffset[6] */
729
    /* (fsnoffset[4] + fgaincod[4]) * c */
730
    frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);
731

    
732
    /* auxdatae, crcrsv */
733
    frame_bits += 2;
734

    
735
    /* CRC */
736
    frame_bits += 16;
737

    
738
    /* calculate psd and masking curve before doing bit allocation */
739
    bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
740

    
741
    /* now the big work begins : do the bit allocation. Modify the snr
742
       offset until we can pack everything in the requested frame size */
743

    
744
    coarse_snr_offset = s->coarse_snr_offset;
745
    while (coarse_snr_offset >= 0 &&
746
           bit_alloc(s, mask, psd, bap, frame_bits, coarse_snr_offset, 0) < 0)
747
        coarse_snr_offset -= SNR_INC1;
748
    if (coarse_snr_offset < 0) {
749
        av_log(NULL, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
750
        return -1;
751
    }
752
    while (coarse_snr_offset + SNR_INC1 <= 63 &&
753
           bit_alloc(s, mask, psd, bap1, frame_bits,
754
                     coarse_snr_offset + SNR_INC1, 0) >= 0) {
755
        coarse_snr_offset += SNR_INC1;
756
        memcpy(bap, bap1, sizeof(bap1));
757
    }
758
    while (coarse_snr_offset + 1 <= 63 &&
759
           bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) {
760
        coarse_snr_offset++;
761
        memcpy(bap, bap1, sizeof(bap1));
762
    }
763

    
764
    fine_snr_offset = 0;
765
    while (fine_snr_offset + SNR_INC1 <= 15 &&
766
           bit_alloc(s, mask, psd, bap1, frame_bits,
767
                     coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) {
768
        fine_snr_offset += SNR_INC1;
769
        memcpy(bap, bap1, sizeof(bap1));
770
    }
771
    while (fine_snr_offset + 1 <= 15 &&
772
           bit_alloc(s, mask, psd, bap1, frame_bits,
773
                     coarse_snr_offset, fine_snr_offset + 1) >= 0) {
774
        fine_snr_offset++;
775
        memcpy(bap, bap1, sizeof(bap1));
776
    }
777

    
778
    s->coarse_snr_offset = coarse_snr_offset;
779
    for (ch = 0; ch < s->channels; ch++)
780
        s->fine_snr_offset[ch] = fine_snr_offset;
781

    
782
    return 0;
783
}
784

    
785

    
786
/**
787
 * Write the AC-3 frame header to the output bitstream.
788
 */
789
static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
790
{
791
    init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
792

    
793
    put_bits(&s->pb, 16, 0x0b77);   /* frame header */
794
    put_bits(&s->pb, 16, 0);        /* crc1: will be filled later */
795
    put_bits(&s->pb, 2,  s->bit_alloc.sr_code);
796
    put_bits(&s->pb, 6,  s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
797
    put_bits(&s->pb, 5,  s->bitstream_id);
798
    put_bits(&s->pb, 3,  s->bitstream_mode);
799
    put_bits(&s->pb, 3,  s->channel_mode);
800
    if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
801
        put_bits(&s->pb, 2, 1);     /* XXX -4.5 dB */
802
    if (s->channel_mode & 0x04)
803
        put_bits(&s->pb, 2, 1);     /* XXX -6 dB */
804
    if (s->channel_mode == AC3_CHMODE_STEREO)
805
        put_bits(&s->pb, 2, 0);     /* surround not indicated */
806
    put_bits(&s->pb, 1, s->lfe_on); /* LFE */
807
    put_bits(&s->pb, 5, 31);        /* dialog norm: -31 db */
808
    put_bits(&s->pb, 1, 0);         /* no compression control word */
809
    put_bits(&s->pb, 1, 0);         /* no lang code */
810
    put_bits(&s->pb, 1, 0);         /* no audio production info */
811
    put_bits(&s->pb, 1, 0);         /* no copyright */
812
    put_bits(&s->pb, 1, 1);         /* original bitstream */
813
    put_bits(&s->pb, 1, 0);         /* no time code 1 */
814
    put_bits(&s->pb, 1, 0);         /* no time code 2 */
815
    put_bits(&s->pb, 1, 0);         /* no additional bit stream info */
816
}
817

    
818

    
819
/**
820
 * Symmetric quantization on 'levels' levels.
821
 */
822
static inline int sym_quant(int c, int e, int levels)
823
{
824
    int v;
825

    
826
    if (c >= 0) {
827
        v = (levels * (c << e)) >> 24;
828
        v = (v + 1) >> 1;
829
        v = (levels >> 1) + v;
830
    } else {
831
        v = (levels * ((-c) << e)) >> 24;
832
        v = (v + 1) >> 1;
833
        v = (levels >> 1) - v;
834
    }
835
    assert (v >= 0 && v < levels);
836
    return v;
837
}
838

    
839

    
840
/**
841
 * Asymmetric quantization on 2^qbits levels.
842
 */
843
static inline int asym_quant(int c, int e, int qbits)
844
{
845
    int lshift, m, v;
846

    
847
    lshift = e + qbits - 24;
848
    if (lshift >= 0)
849
        v = c << lshift;
850
    else
851
        v = c >> (-lshift);
852
    /* rounding */
853
    v = (v + 1) >> 1;
854
    m = (1 << (qbits-1));
855
    if (v >= m)
856
        v = m - 1;
857
    assert(v >= -m);
858
    return v & ((1 << qbits)-1);
859
}
860

    
861

    
862
/**
863
 * Write one audio block to the output bitstream.
864
 */
865
static void output_audio_block(AC3EncodeContext *s,
866
                               uint8_t exp_strategy[AC3_MAX_CHANNELS],
867
                               uint8_t encoded_exp[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
868
                               uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
869
                               int32_t mdct_coef[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
870
                               int8_t exp_shift[AC3_MAX_CHANNELS],
871
                               int block_num)
872
{
873
    int ch, nb_groups, group_size, i, baie, rbnd;
874
    uint8_t *p;
875
    uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS];
876
    int exp0, exp1;
877
    int mant1_cnt, mant2_cnt, mant4_cnt;
878
    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
879
    int delta0, delta1, delta2;
880

    
881
    for (ch = 0; ch < s->fbw_channels; ch++)
882
        put_bits(&s->pb, 1, 0); /* no block switching */
883
    for (ch = 0; ch < s->fbw_channels; ch++)
884
        put_bits(&s->pb, 1, 1); /* no dither */
885
    put_bits(&s->pb, 1, 0);     /* no dynamic range */
886
    if (!block_num) {
887
        put_bits(&s->pb, 1, 1); /* coupling strategy present */
888
        put_bits(&s->pb, 1, 0); /* no coupling strategy */
889
    } else {
890
        put_bits(&s->pb, 1, 0); /* no new coupling strategy */
891
    }
892

    
893
    if (s->channel_mode == AC3_CHMODE_STEREO) {
894
        if (!block_num) {
895
            /* first block must define rematrixing (rematstr) */
896
            put_bits(&s->pb, 1, 1);
897

    
898
            /* dummy rematrixing rematflg(1:4)=0 */
899
            for (rbnd = 0; rbnd < 4; rbnd++)
900
                put_bits(&s->pb, 1, 0);
901
        } else {
902
            /* no matrixing (but should be used in the future) */
903
            put_bits(&s->pb, 1, 0);
904
        }
905
    }
906

    
907
    /* exponent strategy */
908
    for (ch = 0; ch < s->fbw_channels; ch++)
909
        put_bits(&s->pb, 2, exp_strategy[ch]);
910

    
911
    if (s->lfe_on)
912
        put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
913

    
914
    /* bandwidth */
915
    for (ch = 0; ch < s->fbw_channels; ch++) {
916
        if (exp_strategy[ch] != EXP_REUSE)
917
            put_bits(&s->pb, 6, s->bandwidth_code[ch]);
918
    }
919

    
920
    /* exponents */
921
    for (ch = 0; ch < s->channels; ch++) {
922
        if (exp_strategy[ch] == EXP_REUSE)
923
            continue;
924
        group_size = exp_strategy[ch] + (exp_strategy[ch] == EXP_D45);
925
        nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
926
        p = encoded_exp[ch];
927

    
928
        /* first exponent */
929
        exp1 = *p++;
930
        put_bits(&s->pb, 4, exp1);
931

    
932
        /* next ones are delta encoded */
933
        for (i = 0; i < nb_groups; i++) {
934
            /* merge three delta in one code */
935
            exp0   = exp1;
936
            exp1   = p[0];
937
            p     += group_size;
938
            delta0 = exp1 - exp0 + 2;
939

    
940
            exp0   = exp1;
941
            exp1   = p[0];
942
            p     += group_size;
943
            delta1 = exp1 - exp0 + 2;
944

    
945
            exp0   = exp1;
946
            exp1   = p[0];
947
            p     += group_size;
948
            delta2 = exp1 - exp0 + 2;
949

    
950
            put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
951
        }
952

    
953
        if (ch != s->lfe_channel)
954
            put_bits(&s->pb, 2, 0); /* no gain range info */
955
    }
956

    
957
    /* bit allocation info */
958
    baie = (block_num == 0);
959
    put_bits(&s->pb, 1, baie);
960
    if (baie) {
961
        put_bits(&s->pb, 2, s->slow_decay_code);
962
        put_bits(&s->pb, 2, s->fast_decay_code);
963
        put_bits(&s->pb, 2, s->slow_gain_code);
964
        put_bits(&s->pb, 2, s->db_per_bit_code);
965
        put_bits(&s->pb, 3, s->floor_code);
966
    }
967

    
968
    /* snr offset */
969
    put_bits(&s->pb, 1, baie);
970
    if (baie) {
971
        put_bits(&s->pb, 6, s->coarse_snr_offset);
972
        for (ch = 0; ch < s->channels; ch++) {
973
            put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
974
            put_bits(&s->pb, 3, s->fast_gain_code[ch]);
975
        }
976
    }
977

    
978
    put_bits(&s->pb, 1, 0); /* no delta bit allocation */
979
    put_bits(&s->pb, 1, 0); /* no data to skip */
980

    
981
    /* mantissa encoding : we use two passes to handle the grouping. A
982
       one pass method may be faster, but it would necessitate to
983
       modify the output stream. */
984

    
985
    /* first pass: quantize */
986
    mant1_cnt = mant2_cnt = mant4_cnt = 0;
987
    qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
988

    
989
    for (ch = 0; ch < s->channels; ch++) {
990
        int b, c, e, v;
991

    
992
        for (i = 0; i < s->nb_coefs[ch]; i++) {
993
            c = mdct_coef[ch][i];
994
            e = encoded_exp[ch][i] - exp_shift[ch];
995
            b = bap[ch][i];
996
            switch (b) {
997
            case 0:
998
                v = 0;
999
                break;
1000
            case 1:
1001
                v = sym_quant(c, e, 3);
1002
                switch (mant1_cnt) {
1003
                case 0:
1004
                    qmant1_ptr = &qmant[ch][i];
1005
                    v = 9 * v;
1006
                    mant1_cnt = 1;
1007
                    break;
1008
                case 1:
1009
                    *qmant1_ptr += 3 * v;
1010
                    mant1_cnt = 2;
1011
                    v = 128;
1012
                    break;
1013
                default:
1014
                    *qmant1_ptr += v;
1015
                    mant1_cnt = 0;
1016
                    v = 128;
1017
                    break;
1018
                }
1019
                break;
1020
            case 2:
1021
                v = sym_quant(c, e, 5);
1022
                switch (mant2_cnt) {
1023
                case 0:
1024
                    qmant2_ptr = &qmant[ch][i];
1025
                    v = 25 * v;
1026
                    mant2_cnt = 1;
1027
                    break;
1028
                case 1:
1029
                    *qmant2_ptr += 5 * v;
1030
                    mant2_cnt = 2;
1031
                    v = 128;
1032
                    break;
1033
                default:
1034
                    *qmant2_ptr += v;
1035
                    mant2_cnt = 0;
1036
                    v = 128;
1037
                    break;
1038
                }
1039
                break;
1040
            case 3:
1041
                v = sym_quant(c, e, 7);
1042
                break;
1043
            case 4:
1044
                v = sym_quant(c, e, 11);
1045
                switch (mant4_cnt) {
1046
                case 0:
1047
                    qmant4_ptr = &qmant[ch][i];
1048
                    v = 11 * v;
1049
                    mant4_cnt = 1;
1050
                    break;
1051
                default:
1052
                    *qmant4_ptr += v;
1053
                    mant4_cnt = 0;
1054
                    v = 128;
1055
                    break;
1056
                }
1057
                break;
1058
            case 5:
1059
                v = sym_quant(c, e, 15);
1060
                break;
1061
            case 14:
1062
                v = asym_quant(c, e, 14);
1063
                break;
1064
            case 15:
1065
                v = asym_quant(c, e, 16);
1066
                break;
1067
            default:
1068
                v = asym_quant(c, e, b - 1);
1069
                break;
1070
            }
1071
            qmant[ch][i] = v;
1072
        }
1073
    }
1074

    
1075
    /* second pass : output the values */
1076
    for (ch = 0; ch < s->channels; ch++) {
1077
        int b, q;
1078

    
1079
        for (i = 0; i < s->nb_coefs[ch]; i++) {
1080
            q = qmant[ch][i];
1081
            b = bap[ch][i];
1082
            switch (b) {
1083
            case 0:                                         break;
1084
            case 1: if (q != 128) put_bits(&s->pb,   5, q); break;
1085
            case 2: if (q != 128) put_bits(&s->pb,   7, q); break;
1086
            case 3:               put_bits(&s->pb,   3, q); break;
1087
            case 4: if (q != 128) put_bits(&s->pb,   7, q); break;
1088
            case 14:              put_bits(&s->pb,  14, q); break;
1089
            case 15:              put_bits(&s->pb,  16, q); break;
1090
            default:              put_bits(&s->pb, b-1, q); break;
1091
            }
1092
        }
1093
    }
1094
}
1095

    
1096

    
1097
/** CRC-16 Polynomial */
1098
#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1099

    
1100

    
1101
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1102
{
1103
    unsigned int c;
1104

    
1105
    c = 0;
1106
    while (a) {
1107
        if (a & 1)
1108
            c ^= b;
1109
        a = a >> 1;
1110
        b = b << 1;
1111
        if (b & (1 << 16))
1112
            b ^= poly;
1113
    }
1114
    return c;
1115
}
1116

    
1117

    
1118
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
1119
{
1120
    unsigned int r;
1121
    r = 1;
1122
    while (n) {
1123
        if (n & 1)
1124
            r = mul_poly(r, a, poly);
1125
        a = mul_poly(a, a, poly);
1126
        n >>= 1;
1127
    }
1128
    return r;
1129
}
1130

    
1131

    
1132
/**
1133
 * Fill the end of the frame with 0's and compute the two CRCs.
1134
 */
1135
static void output_frame_end(AC3EncodeContext *s)
1136
{
1137
    int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1138
    uint8_t *frame;
1139

    
1140
    frame_size = s->frame_size; /* frame size in words */
1141
    /* align to 8 bits */
1142
    flush_put_bits(&s->pb);
1143
    /* add zero bytes to reach the frame size */
1144
    frame = s->pb.buf;
1145
    pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
1146
    assert(pad_bytes >= 0);
1147
    if (pad_bytes > 0)
1148
        memset(put_bits_ptr(&s->pb), 0, pad_bytes);
1149

    
1150
    /* Now we must compute both crcs : this is not so easy for crc1
1151
       because it is at the beginning of the data... */
1152
    frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1;
1153

    
1154
    crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1155
                             frame + 4, frame_size_58 - 4));
1156

    
1157
    /* XXX: could precompute crc_inv */
1158
    crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1159
    crc1    = mul_poly(crc_inv, crc1, CRC16_POLY);
1160
    AV_WB16(frame + 2, crc1);
1161

    
1162
    crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1163
                             frame + frame_size_58,
1164
                             frame_size - frame_size_58 - 2));
1165
    AV_WB16(frame + frame_size - 2, crc2);
1166
}
1167

    
1168

    
1169
/**
1170
 * Encode a single AC-3 frame.
1171
 */
1172
static int ac3_encode_frame(AVCodecContext *avctx,
1173
                            unsigned char *frame, int buf_size, void *data)
1174
{
1175
    AC3EncodeContext *s = avctx->priv_data;
1176
    const int16_t *samples = data;
1177
    int v;
1178
    int blk, blk1, blk2, ch, i;
1179
    int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE];
1180
    int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1181
    uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1182
    uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
1183
    uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1184
    uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
1185
    int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
1186
    int frame_bits;
1187

    
1188
    deinterleave_input_samples(s, samples, planar_samples);
1189

    
1190
    apply_mdct(s, planar_samples, exp_shift, mdct_coef);
1191

    
1192
    /* extract exponents */
1193
    for (ch = 0; ch < s->channels; ch++) {
1194
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1195
            /* compute "exponents". We take into account the normalization there */
1196
            for (i = 0; i < AC3_MAX_COEFS; i++) {
1197
                int e;
1198
                v = abs(mdct_coef[blk][ch][i]);
1199
                if (v == 0)
1200
                    e = 24;
1201
                else {
1202
                    e = 23 - av_log2(v) + exp_shift[blk][ch];
1203
                    if (e >= 24) {
1204
                        e = 24;
1205
                        mdct_coef[blk][ch][i] = 0;
1206
                    }
1207
                }
1208
                exp[blk][ch][i] = e;
1209
            }
1210
        }
1211
    }
1212

    
1213
    /* compute exponent strategies */
1214
    for (ch = 0; ch < s->channels; ch++) {
1215
        compute_exp_strategy_ch(exp_strategy, exp, ch, ch == s->lfe_channel);
1216
    }
1217

    
1218
    /* encode exponents */
1219
    frame_bits = 0;
1220
    for (ch = 0; ch < s->channels; ch++) {
1221
        /* compute the exponents as the decoder will see them. The
1222
           EXP_REUSE case must be handled carefully : we select the
1223
           min of the exponents */
1224
        blk = 0;
1225
        while (blk < AC3_MAX_BLOCKS) {
1226
            blk1 = blk + 1;
1227
            while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1][ch] == EXP_REUSE) {
1228
                exponent_min(exp[blk][ch], exp[blk1][ch], s->nb_coefs[ch]);
1229
                blk1++;
1230
            }
1231
            frame_bits += encode_exponents_blk_ch(encoded_exp[blk][ch],
1232
                                                  exp[blk][ch], s->nb_coefs[ch],
1233
                                                  exp_strategy[blk][ch]);
1234
            /* copy encoded exponents for reuse case */
1235
            for (blk2 = blk+1; blk2 < blk1; blk2++) {
1236
                memcpy(encoded_exp[blk2][ch], encoded_exp[blk][ch],
1237
                       s->nb_coefs[ch] * sizeof(uint8_t));
1238
            }
1239
            blk = blk1;
1240
        }
1241
    }
1242

    
1243
    /* adjust for fractional frame sizes */
1244
    while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
1245
        s->bits_written    -= s->bit_rate;
1246
        s->samples_written -= s->sample_rate;
1247
    }
1248
    s->frame_size = s->frame_size_min + 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
1249
    s->bits_written    += s->frame_size * 8;
1250
    s->samples_written += AC3_FRAME_SIZE;
1251

    
1252
    compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
1253
    /* everything is known... let's output the frame */
1254
    output_frame_header(s, frame);
1255

    
1256
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1257
        output_audio_block(s, exp_strategy[blk], encoded_exp[blk],
1258
                           bap[blk], mdct_coef[blk], exp_shift[blk], blk);
1259
    }
1260
    output_frame_end(s);
1261

    
1262
    return s->frame_size;
1263
}
1264

    
1265

    
1266
/**
1267
 * Finalize encoding and free any memory allocated by the encoder.
1268
 */
1269
static av_cold int ac3_encode_close(AVCodecContext *avctx)
1270
{
1271
    av_freep(&avctx->coded_frame);
1272
    return 0;
1273
}
1274

    
1275

    
1276
/**
1277
 * Set channel information during initialization.
1278
 */
1279
static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
1280
                                    int64_t *channel_layout)
1281
{
1282
    int ch_layout;
1283

    
1284
    if (channels < 1 || channels > AC3_MAX_CHANNELS)
1285
        return AVERROR(EINVAL);
1286
    if ((uint64_t)*channel_layout > 0x7FF)
1287
        return AVERROR(EINVAL);
1288
    ch_layout = *channel_layout;
1289
    if (!ch_layout)
1290
        ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
1291
    if (av_get_channel_layout_nb_channels(ch_layout) != channels)
1292
        return AVERROR(EINVAL);
1293

    
1294
    s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
1295
    s->channels     = channels;
1296
    s->fbw_channels = channels - s->lfe_on;
1297
    s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
1298
    if (s->lfe_on)
1299
        ch_layout -= AV_CH_LOW_FREQUENCY;
1300

    
1301
    switch (ch_layout) {
1302
    case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;
1303
    case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;
1304
    case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;
1305
    case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;
1306
    case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;
1307
    case AV_CH_LAYOUT_QUAD:
1308
    case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;
1309
    case AV_CH_LAYOUT_5POINT0:
1310
    case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;
1311
    default:
1312
        return AVERROR(EINVAL);
1313
    }
1314

    
1315
    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
1316
    *channel_layout = ch_layout;
1317
    if (s->lfe_on)
1318
        *channel_layout |= AV_CH_LOW_FREQUENCY;
1319

    
1320
    return 0;
1321
}
1322

    
1323

    
1324
static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
1325
{
1326
    int i, ret;
1327

    
1328
    /* validate channel layout */
1329
    if (!avctx->channel_layout) {
1330
        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
1331
                                      "encoder will guess the layout, but it "
1332
                                      "might be incorrect.\n");
1333
    }
1334
    ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
1335
    if (ret) {
1336
        av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
1337
        return ret;
1338
    }
1339

    
1340
    /* validate sample rate */
1341
    for (i = 0; i < 9; i++) {
1342
        if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
1343
            break;
1344
    }
1345
    if (i == 9) {
1346
        av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
1347
        return AVERROR(EINVAL);
1348
    }
1349
    s->sample_rate        = avctx->sample_rate;
1350
    s->bit_alloc.sr_shift = i % 3;
1351
    s->bit_alloc.sr_code  = i / 3;
1352

    
1353
    /* validate bit rate */
1354
    for (i = 0; i < 19; i++) {
1355
        if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
1356
            break;
1357
    }
1358
    if (i == 19) {
1359
        av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
1360
        return AVERROR(EINVAL);
1361
    }
1362
    s->bit_rate        = avctx->bit_rate;
1363
    s->frame_size_code = i << 1;
1364

    
1365
    return 0;
1366
}
1367

    
1368

    
1369
/**
1370
 * Set bandwidth for all channels.
1371
 * The user can optionally supply a cutoff frequency. Otherwise an appropriate
1372
 * default value will be used.
1373
 */
1374
static av_cold void set_bandwidth(AC3EncodeContext *s, int cutoff)
1375
{
1376
    int ch, bw_code;
1377

    
1378
    if (cutoff) {
1379
        /* calculate bandwidth based on user-specified cutoff frequency */
1380
        int fbw_coeffs;
1381
        cutoff         = av_clip(cutoff, 1, s->sample_rate >> 1);
1382
        fbw_coeffs     = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
1383
        bw_code        = av_clip((fbw_coeffs - 73) / 3, 0, 60);
1384
    } else {
1385
        /* use default bandwidth setting */
1386
        /* XXX: should compute the bandwidth according to the frame
1387
           size, so that we avoid annoying high frequency artifacts */
1388
        bw_code = 50;
1389
    }
1390

    
1391
    /* set number of coefficients for each channel */
1392
    for (ch = 0; ch < s->fbw_channels; ch++) {
1393
        s->bandwidth_code[ch] = bw_code;
1394
        s->nb_coefs[ch]       = bw_code * 3 + 73;
1395
    }
1396
    if (s->lfe_on)
1397
        s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
1398
}
1399

    
1400

    
1401
/**
1402
 * Initialize the encoder.
1403
 */
1404
static av_cold int ac3_encode_init(AVCodecContext *avctx)
1405
{
1406
    AC3EncodeContext *s = avctx->priv_data;
1407
    int ret;
1408

    
1409
    avctx->frame_size = AC3_FRAME_SIZE;
1410

    
1411
    ac3_common_init();
1412

    
1413
    ret = validate_options(avctx, s);
1414
    if (ret)
1415
        return ret;
1416

    
1417
    s->bitstream_id   = 8 + s->bit_alloc.sr_shift;
1418
    s->bitstream_mode = 0; /* complete main audio service */
1419

    
1420
    s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
1421
    s->bits_written    = 0;
1422
    s->samples_written = 0;
1423
    s->frame_size      = s->frame_size_min;
1424

    
1425
    set_bandwidth(s, avctx->cutoff);
1426

    
1427
    /* initial snr offset */
1428
    s->coarse_snr_offset = 40;
1429

    
1430
    mdct_init(9);
1431

    
1432
    avctx->coded_frame= avcodec_alloc_frame();
1433
    avctx->coded_frame->key_frame= 1;
1434

    
1435
    return 0;
1436
}
1437

    
1438

    
1439
#ifdef TEST
1440
/*************************************************************************/
1441
/* TEST */
1442

    
1443
#include "libavutil/lfg.h"
1444

    
1445
#define FN (MDCT_SAMPLES/4)
1446

    
1447

    
1448
static void fft_test(AVLFG *lfg)
1449
{
1450
    IComplex in[FN], in1[FN];
1451
    int k, n, i;
1452
    float sum_re, sum_im, a;
1453

    
1454
    for (i = 0; i < FN; i++) {
1455
        in[i].re = av_lfg_get(lfg) % 65535 - 32767;
1456
        in[i].im = av_lfg_get(lfg) % 65535 - 32767;
1457
        in1[i]   = in[i];
1458
    }
1459
    fft(in, 7);
1460

    
1461
    /* do it by hand */
1462
    for (k = 0; k < FN; k++) {
1463
        sum_re = 0;
1464
        sum_im = 0;
1465
        for (n = 0; n < FN; n++) {
1466
            a = -2 * M_PI * (n * k) / FN;
1467
            sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1468
            sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1469
        }
1470
        av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
1471
               k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1472
    }
1473
}
1474

    
1475

    
1476
static void mdct_test(AVLFG *lfg)
1477
{
1478
    int16_t input[MDCT_SAMPLES];
1479
    int32_t output[AC3_MAX_COEFS];
1480
    float input1[MDCT_SAMPLES];
1481
    float output1[AC3_MAX_COEFS];
1482
    float s, a, err, e, emax;
1483
    int i, k, n;
1484

    
1485
    for (i = 0; i < MDCT_SAMPLES; i++) {
1486
        input[i]  = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
1487
        input1[i] = input[i];
1488
    }
1489

    
1490
    mdct512(output, input);
1491

    
1492
    /* do it by hand */
1493
    for (k = 0; k < AC3_MAX_COEFS; k++) {
1494
        s = 0;
1495
        for (n = 0; n < MDCT_SAMPLES; n++) {
1496
            a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
1497
            s += input1[n] * cos(a);
1498
        }
1499
        output1[k] = -2 * s / MDCT_SAMPLES;
1500
    }
1501

    
1502
    err  = 0;
1503
    emax = 0;
1504
    for (i = 0; i < AC3_MAX_COEFS; i++) {
1505
        av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
1506
        e = output[i] - output1[i];
1507
        if (e > emax)
1508
            emax = e;
1509
        err += e * e;
1510
    }
1511
    av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
1512
}
1513

    
1514

    
1515
int main(void)
1516
{
1517
    AVLFG lfg;
1518

    
1519
    av_log_set_level(AV_LOG_DEBUG);
1520
    mdct_init(9);
1521

    
1522
    fft_test(&lfg);
1523
    mdct_test(&lfg);
1524

    
1525
    return 0;
1526
}
1527
#endif /* TEST */
1528

    
1529

    
1530
AVCodec ac3_encoder = {
1531
    "ac3",
1532
    AVMEDIA_TYPE_AUDIO,
1533
    CODEC_ID_AC3,
1534
    sizeof(AC3EncodeContext),
1535
    ac3_encode_init,
1536
    ac3_encode_frame,
1537
    ac3_encode_close,
1538
    NULL,
1539
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
1540
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
1541
    .channel_layouts = (const int64_t[]){
1542
        AV_CH_LAYOUT_MONO,
1543
        AV_CH_LAYOUT_STEREO,
1544
        AV_CH_LAYOUT_2_1,
1545
        AV_CH_LAYOUT_SURROUND,
1546
        AV_CH_LAYOUT_2_2,
1547
        AV_CH_LAYOUT_QUAD,
1548
        AV_CH_LAYOUT_4POINT0,
1549
        AV_CH_LAYOUT_5POINT0,
1550
        AV_CH_LAYOUT_5POINT0_BACK,
1551
       (AV_CH_LAYOUT_MONO     | AV_CH_LOW_FREQUENCY),
1552
       (AV_CH_LAYOUT_STEREO   | AV_CH_LOW_FREQUENCY),
1553
       (AV_CH_LAYOUT_2_1      | AV_CH_LOW_FREQUENCY),
1554
       (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
1555
       (AV_CH_LAYOUT_2_2      | AV_CH_LOW_FREQUENCY),
1556
       (AV_CH_LAYOUT_QUAD     | AV_CH_LOW_FREQUENCY),
1557
       (AV_CH_LAYOUT_4POINT0  | AV_CH_LOW_FREQUENCY),
1558
        AV_CH_LAYOUT_5POINT1,
1559
        AV_CH_LAYOUT_5POINT1_BACK,
1560
        0 },
1561
};