Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3enc.c @ e62ef8f2

History | View | Annotate | Download (59.5 KB)

1
/*
2
 * The simplest AC-3 encoder
3
 * Copyright (c) 2000 Fabrice Bellard
4
 * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
5
 * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23

    
24
/**
25
 * @file
26
 * The simplest AC-3 encoder.
27
 */
28

    
29
//#define DEBUG
30

    
31
#include "libavcore/audioconvert.h"
32
#include "libavutil/crc.h"
33
#include "avcodec.h"
34
#include "put_bits.h"
35
#include "dsputil.h"
36
#include "ac3.h"
37
#include "audioconvert.h"
38

    
39

    
40
/** Maximum number of exponent groups. +1 for separate DC exponent. */
41
#define AC3_MAX_EXP_GROUPS 85
42

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

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

    
49

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

    
58
typedef struct AC3MDCTContext {
59
    AVCodecContext *avctx;                  ///< parent context for av_log()
60
    int nbits;                              ///< log2(transform size)
61
    int16_t *costab;                        ///< FFT cos table
62
    int16_t *sintab;                        ///< FFT sin table
63
    int16_t *xcos1;                         ///< MDCT cos table
64
    int16_t *xsin1;                         ///< MDCT sin table
65
    int16_t *rot_tmp;                       ///< temp buffer for pre-rotated samples
66
    IComplex *cplx_tmp;                     ///< temp buffer for complex pre-rotated samples
67
} AC3MDCTContext;
68

    
69
/**
70
 * Data for a single audio block.
71
 */
72
typedef struct AC3Block {
73
    uint8_t  **bap;                             ///< bit allocation pointers (bap)
74
    int32_t  **mdct_coef;                       ///< MDCT coefficients
75
    uint8_t  **exp;                             ///< original exponents
76
    uint8_t  **grouped_exp;                     ///< grouped exponents
77
    int16_t  **psd;                             ///< psd per frequency bin
78
    int16_t  **band_psd;                        ///< psd per critical band
79
    int16_t  **mask;                            ///< masking curve
80
    uint16_t **qmant;                           ///< quantized mantissas
81
    uint8_t  exp_strategy[AC3_MAX_CHANNELS];    ///< exponent strategies
82
    int8_t   exp_shift[AC3_MAX_CHANNELS];       ///< exponent shift values
83
} AC3Block;
84

    
85
/**
86
 * AC-3 encoder private context.
87
 */
88
typedef struct AC3EncodeContext {
89
    PutBitContext pb;                       ///< bitstream writer context
90
    DSPContext dsp;
91
    AC3MDCTContext mdct;                    ///< MDCT context
92

    
93
    AC3Block blocks[AC3_MAX_BLOCKS];        ///< per-block info
94

    
95
    int bitstream_id;                       ///< bitstream id                           (bsid)
96
    int bitstream_mode;                     ///< bitstream mode                         (bsmod)
97

    
98
    int bit_rate;                           ///< target bit rate, in bits-per-second
99
    int sample_rate;                        ///< sampling frequency, in Hz
100

    
101
    int frame_size_min;                     ///< minimum frame size in case rounding is necessary
102
    int frame_size;                         ///< current frame size in bytes
103
    int frame_size_code;                    ///< frame size code                        (frmsizecod)
104
    int bits_written;                       ///< bit count    (used to avg. bitrate)
105
    int samples_written;                    ///< sample count (used to avg. bitrate)
106

    
107
    int fbw_channels;                       ///< number of full-bandwidth channels      (nfchans)
108
    int channels;                           ///< total number of channels               (nchans)
109
    int lfe_on;                             ///< indicates if there is an LFE channel   (lfeon)
110
    int lfe_channel;                        ///< channel index of the LFE channel
111
    int channel_mode;                       ///< channel mode                           (acmod)
112
    const uint8_t *channel_map;             ///< channel map used to reorder channels
113

    
114
    int cutoff;                             ///< user-specified cutoff frequency, in Hz
115
    int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
116
    int nb_coefs[AC3_MAX_CHANNELS];
117

    
118
    /* bitrate allocation control */
119
    int slow_gain_code;                     ///< slow gain code                         (sgaincod)
120
    int slow_decay_code;                    ///< slow decay code                        (sdcycod)
121
    int fast_decay_code;                    ///< fast decay code                        (fdcycod)
122
    int db_per_bit_code;                    ///< dB/bit code                            (dbpbcod)
123
    int floor_code;                         ///< floor code                             (floorcod)
124
    AC3BitAllocParameters bit_alloc;        ///< bit allocation parameters
125
    int coarse_snr_offset;                  ///< coarse SNR offsets                     (csnroffst)
126
    int fast_gain_code[AC3_MAX_CHANNELS];   ///< fast gain codes (signal-to-mask ratio) (fgaincod)
127
    int fine_snr_offset[AC3_MAX_CHANNELS];  ///< fine SNR offsets                       (fsnroffst)
128
    int frame_bits_fixed;                   ///< number of non-coefficient bits for fixed parameters
129
    int frame_bits;                         ///< all frame bits except exponents and mantissas
130
    int exponent_bits;                      ///< number of bits used for exponents
131

    
132
    /* mantissa encoding */
133
    int mant1_cnt, mant2_cnt, mant4_cnt;    ///< mantissa counts for bap=1,2,4
134
    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4
135

    
136
    int16_t **planar_samples;
137
    uint8_t *bap_buffer;
138
    uint8_t *bap1_buffer;
139
    int32_t *mdct_coef_buffer;
140
    uint8_t *exp_buffer;
141
    uint8_t *grouped_exp_buffer;
142
    int16_t *psd_buffer;
143
    int16_t *band_psd_buffer;
144
    int16_t *mask_buffer;
145
    uint16_t *qmant_buffer;
146

    
147
    DECLARE_ALIGNED(16, int16_t, windowed_samples)[AC3_WINDOW_SIZE];
148
} AC3EncodeContext;
149

    
150

    
151
/**
152
 * LUT for number of exponent groups.
153
 * exponent_group_tab[exponent strategy-1][number of coefficients]
154
 */
155
uint8_t exponent_group_tab[3][256];
156

    
157

    
158
/**
159
 * Adjust the frame size to make the average bit rate match the target bit rate.
160
 * This is only needed for 11025, 22050, and 44100 sample rates.
161
 */
162
static void adjust_frame_size(AC3EncodeContext *s)
163
{
164
    while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
165
        s->bits_written    -= s->bit_rate;
166
        s->samples_written -= s->sample_rate;
167
    }
168
    s->frame_size = s->frame_size_min +
169
                    2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
170
    s->bits_written    += s->frame_size * 8;
171
    s->samples_written += AC3_FRAME_SIZE;
172
}
173

    
174

    
175
/**
176
 * Deinterleave input samples.
177
 * Channels are reordered from FFmpeg's default order to AC-3 order.
178
 */
179
static void deinterleave_input_samples(AC3EncodeContext *s,
180
                                       const int16_t *samples)
181
{
182
    int ch, i;
183

    
184
    /* deinterleave and remap input samples */
185
    for (ch = 0; ch < s->channels; ch++) {
186
        const int16_t *sptr;
187
        int sinc;
188

    
189
        /* copy last 256 samples of previous frame to the start of the current frame */
190
        memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
191
               AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
192

    
193
        /* deinterleave */
194
        sinc = s->channels;
195
        sptr = samples + s->channel_map[ch];
196
        for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
197
            s->planar_samples[ch][i] = *sptr;
198
            sptr += sinc;
199
        }
200
    }
201
}
202

    
203

    
204
/**
205
 * Finalize MDCT and free allocated memory.
206
 */
207
static av_cold void mdct_end(AC3MDCTContext *mdct)
208
{
209
    mdct->nbits = 0;
210
    av_freep(&mdct->costab);
211
    av_freep(&mdct->sintab);
212
    av_freep(&mdct->xcos1);
213
    av_freep(&mdct->xsin1);
214
    av_freep(&mdct->rot_tmp);
215
    av_freep(&mdct->cplx_tmp);
216
}
217

    
218

    
219

    
220
/**
221
 * Initialize FFT tables.
222
 * @param ln log2(FFT size)
223
 */
224
static av_cold int fft_init(AC3MDCTContext *mdct, int ln)
225
{
226
    int i, n, n2;
227
    float alpha;
228

    
229
    n  = 1 << ln;
230
    n2 = n >> 1;
231

    
232
    FF_ALLOC_OR_GOTO(mdct->avctx, mdct->costab, n2 * sizeof(*mdct->costab),
233
                     fft_alloc_fail);
234
    FF_ALLOC_OR_GOTO(mdct->avctx, mdct->sintab, n2 * sizeof(*mdct->sintab),
235
                     fft_alloc_fail);
236

    
237
    for (i = 0; i < n2; i++) {
238
        alpha     = 2.0 * M_PI * i / n;
239
        mdct->costab[i] = FIX15(cos(alpha));
240
        mdct->sintab[i] = FIX15(sin(alpha));
241
    }
242

    
243
    return 0;
244
fft_alloc_fail:
245
    mdct_end(mdct);
246
    return AVERROR(ENOMEM);
247
}
248

    
249

    
250
/**
251
 * Initialize MDCT tables.
252
 * @param nbits log2(MDCT size)
253
 */
254
static av_cold int mdct_init(AC3MDCTContext *mdct, int nbits)
255
{
256
    int i, n, n4, ret;
257

    
258
    n  = 1 << nbits;
259
    n4 = n >> 2;
260

    
261
    mdct->nbits = nbits;
262

    
263
    ret = fft_init(mdct, nbits - 2);
264
    if (ret)
265
        return ret;
266

    
267
    FF_ALLOC_OR_GOTO(mdct->avctx, mdct->xcos1,    n4 * sizeof(*mdct->xcos1),
268
                     mdct_alloc_fail);
269
    FF_ALLOC_OR_GOTO(mdct->avctx, mdct->xsin1 ,   n4 * sizeof(*mdct->xsin1),
270
                     mdct_alloc_fail);
271
    FF_ALLOC_OR_GOTO(mdct->avctx, mdct->rot_tmp,  n  * sizeof(*mdct->rot_tmp),
272
                     mdct_alloc_fail);
273
    FF_ALLOC_OR_GOTO(mdct->avctx, mdct->cplx_tmp, n4 * sizeof(*mdct->cplx_tmp),
274
                     mdct_alloc_fail);
275

    
276
    for (i = 0; i < n4; i++) {
277
        float alpha = 2.0 * M_PI * (i + 1.0 / 8.0) / n;
278
        mdct->xcos1[i] = FIX15(-cos(alpha));
279
        mdct->xsin1[i] = FIX15(-sin(alpha));
280
    }
281

    
282
    return 0;
283
mdct_alloc_fail:
284
    mdct_end(mdct);
285
    return AVERROR(ENOMEM);
286
}
287

    
288

    
289
/** Butterfly op */
290
#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1)  \
291
{                                                       \
292
  int ax, ay, bx, by;                                   \
293
  bx  = pre1;                                           \
294
  by  = pim1;                                           \
295
  ax  = qre1;                                           \
296
  ay  = qim1;                                           \
297
  pre = (bx + ax) >> 1;                                 \
298
  pim = (by + ay) >> 1;                                 \
299
  qre = (bx - ax) >> 1;                                 \
300
  qim = (by - ay) >> 1;                                 \
301
}
302

    
303

    
304
/** Complex multiply */
305
#define CMUL(pre, pim, are, aim, bre, bim)              \
306
{                                                       \
307
   pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;     \
308
   pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;     \
309
}
310

    
311

    
312
/**
313
 * Calculate a 2^n point complex FFT on 2^ln points.
314
 * @param z  complex input/output samples
315
 * @param ln log2(FFT size)
316
 */
317
static void fft(AC3MDCTContext *mdct, IComplex *z, int ln)
318
{
319
    int j, l, np, np2;
320
    int nblocks, nloops;
321
    register IComplex *p,*q;
322
    int tmp_re, tmp_im;
323

    
324
    np = 1 << ln;
325

    
326
    /* reverse */
327
    for (j = 0; j < np; j++) {
328
        int k = av_reverse[j] >> (8 - ln);
329
        if (k < j)
330
            FFSWAP(IComplex, z[k], z[j]);
331
    }
332

    
333
    /* pass 0 */
334

    
335
    p = &z[0];
336
    j = np >> 1;
337
    do {
338
        BF(p[0].re, p[0].im, p[1].re, p[1].im,
339
           p[0].re, p[0].im, p[1].re, p[1].im);
340
        p += 2;
341
    } while (--j);
342

    
343
    /* pass 1 */
344

    
345
    p = &z[0];
346
    j = np >> 2;
347
    do {
348
        BF(p[0].re, p[0].im, p[2].re,  p[2].im,
349
           p[0].re, p[0].im, p[2].re,  p[2].im);
350
        BF(p[1].re, p[1].im, p[3].re,  p[3].im,
351
           p[1].re, p[1].im, p[3].im, -p[3].re);
352
        p+=4;
353
    } while (--j);
354

    
355
    /* pass 2 .. ln-1 */
356

    
357
    nblocks = np >> 3;
358
    nloops  =  1 << 2;
359
    np2     = np >> 1;
360
    do {
361
        p = z;
362
        q = z + nloops;
363
        for (j = 0; j < nblocks; j++) {
364
            BF(p->re, p->im, q->re, q->im,
365
               p->re, p->im, q->re, q->im);
366
            p++;
367
            q++;
368
            for(l = nblocks; l < np2; l += nblocks) {
369
                CMUL(tmp_re, tmp_im, mdct->costab[l], -mdct->sintab[l], q->re, q->im);
370
                BF(p->re, p->im, q->re,  q->im,
371
                   p->re, p->im, tmp_re, tmp_im);
372
                p++;
373
                q++;
374
            }
375
            p += nloops;
376
            q += nloops;
377
        }
378
        nblocks = nblocks >> 1;
379
        nloops  = nloops  << 1;
380
    } while (nblocks);
381
}
382

    
383

    
384
/**
385
 * Calculate a 512-point MDCT
386
 * @param out 256 output frequency coefficients
387
 * @param in  512 windowed input audio samples
388
 */
389
static void mdct512(AC3MDCTContext *mdct, int32_t *out, int16_t *in)
390
{
391
    int i, re, im, n, n2, n4;
392
    int16_t *rot = mdct->rot_tmp;
393
    IComplex *x  = mdct->cplx_tmp;
394

    
395
    n  = 1 << mdct->nbits;
396
    n2 = n >> 1;
397
    n4 = n >> 2;
398

    
399
    /* shift to simplify computations */
400
    for (i = 0; i <n4; i++)
401
        rot[i] = -in[i + 3*n4];
402
    memcpy(&rot[n4], &in[0], 3*n4*sizeof(*in));
403

    
404
    /* pre rotation */
405
    for (i = 0; i < n4; i++) {
406
        re =  ((int)rot[   2*i] - (int)rot[ n-1-2*i]) >> 1;
407
        im = -((int)rot[n2+2*i] - (int)rot[n2-1-2*i]) >> 1;
408
        CMUL(x[i].re, x[i].im, re, im, -mdct->xcos1[i], mdct->xsin1[i]);
409
    }
410

    
411
    fft(mdct, x, mdct->nbits - 2);
412

    
413
    /* post rotation */
414
    for (i = 0; i < n4; i++) {
415
        re = x[i].re;
416
        im = x[i].im;
417
        CMUL(out[n2-1-2*i], out[2*i], re, im, mdct->xsin1[i], mdct->xcos1[i]);
418
    }
419
}
420

    
421

    
422
/**
423
 * Apply KBD window to input samples prior to MDCT.
424
 */
425
static void apply_window(int16_t *output, const int16_t *input,
426
                         const int16_t *window, int n)
427
{
428
    int i;
429
    int n2 = n >> 1;
430

    
431
    for (i = 0; i < n2; i++) {
432
        output[i]     = MUL16(input[i],     window[i]) >> 15;
433
        output[n-i-1] = MUL16(input[n-i-1], window[i]) >> 15;
434
    }
435
}
436

    
437

    
438
/**
439
 * Calculate the log2() of the maximum absolute value in an array.
440
 * @param tab input array
441
 * @param n   number of values in the array
442
 * @return    log2(max(abs(tab[])))
443
 */
444
static int log2_tab(int16_t *tab, int n)
445
{
446
    int i, v;
447

    
448
    v = 0;
449
    for (i = 0; i < n; i++)
450
        v |= abs(tab[i]);
451

    
452
    return av_log2(v);
453
}
454

    
455

    
456
/**
457
 * Left-shift each value in an array by a specified amount.
458
 * @param tab    input array
459
 * @param n      number of values in the array
460
 * @param lshift left shift amount. a negative value means right shift.
461
 */
462
static void lshift_tab(int16_t *tab, int n, int lshift)
463
{
464
    int i;
465

    
466
    if (lshift > 0) {
467
        for (i = 0; i < n; i++)
468
            tab[i] <<= lshift;
469
    } else if (lshift < 0) {
470
        lshift = -lshift;
471
        for (i = 0; i < n; i++)
472
            tab[i] >>= lshift;
473
    }
474
}
475

    
476

    
477
/**
478
 * Normalize the input samples to use the maximum available precision.
479
 * This assumes signed 16-bit input samples. Exponents are reduced by 9 to
480
 * match the 24-bit internal precision for MDCT coefficients.
481
 *
482
 * @return exponent shift
483
 */
484
static int normalize_samples(AC3EncodeContext *s)
485
{
486
    int v = 14 - log2_tab(s->windowed_samples, AC3_WINDOW_SIZE);
487
    v = FFMAX(0, v);
488
    lshift_tab(s->windowed_samples, AC3_WINDOW_SIZE, v);
489
    return v - 9;
490
}
491

    
492

    
493
/**
494
 * Apply the MDCT to input samples to generate frequency coefficients.
495
 * This applies the KBD window and normalizes the input to reduce precision
496
 * loss due to fixed-point calculations.
497
 */
498
static void apply_mdct(AC3EncodeContext *s)
499
{
500
    int blk, ch;
501

    
502
    for (ch = 0; ch < s->channels; ch++) {
503
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
504
            AC3Block *block = &s->blocks[blk];
505
            const int16_t *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
506

    
507
            apply_window(s->windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE);
508

    
509
            block->exp_shift[ch] = normalize_samples(s);
510

    
511
            mdct512(&s->mdct, block->mdct_coef[ch], s->windowed_samples);
512
        }
513
    }
514
}
515

    
516

    
517
/**
518
 * Initialize exponent tables.
519
 */
520
static av_cold void exponent_init(AC3EncodeContext *s)
521
{
522
    int i;
523
    for (i = 73; i < 256; i++) {
524
        exponent_group_tab[0][i] = (i - 1) /  3;
525
        exponent_group_tab[1][i] = (i + 2) /  6;
526
        exponent_group_tab[2][i] = (i + 8) / 12;
527
    }
528
    /* LFE */
529
    exponent_group_tab[0][7] = 2;
530
}
531

    
532

    
533
/**
534
 * Extract exponents from the MDCT coefficients.
535
 * This takes into account the normalization that was done to the input samples
536
 * by adjusting the exponents by the exponent shift values.
537
 */
538
static void extract_exponents(AC3EncodeContext *s)
539
{
540
    int blk, ch, i;
541

    
542
    for (ch = 0; ch < s->channels; ch++) {
543
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
544
            AC3Block *block = &s->blocks[blk];
545
            for (i = 0; i < AC3_MAX_COEFS; i++) {
546
                int e;
547
                int v = abs(block->mdct_coef[ch][i]);
548
                if (v == 0)
549
                    e = 24;
550
                else {
551
                    e = 23 - av_log2(v) + block->exp_shift[ch];
552
                    if (e >= 24) {
553
                        e = 24;
554
                        block->mdct_coef[ch][i] = 0;
555
                    }
556
                }
557
                block->exp[ch][i] = e;
558
            }
559
        }
560
    }
561
}
562

    
563

    
564
/**
565
 * Exponent Difference Threshold.
566
 * New exponents are sent if their SAD exceed this number.
567
 */
568
#define EXP_DIFF_THRESHOLD 1000
569

    
570

    
571
/**
572
 * Calculate exponent strategies for all blocks in a single channel.
573
 */
574
static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy, uint8_t **exp)
575
{
576
    int blk, blk1;
577
    int exp_diff;
578

    
579
    /* estimate if the exponent variation & decide if they should be
580
       reused in the next frame */
581
    exp_strategy[0] = EXP_NEW;
582
    for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
583
        exp_diff = s->dsp.sad[0](NULL, exp[blk], exp[blk-1], 16, 16);
584
        if (exp_diff > EXP_DIFF_THRESHOLD)
585
            exp_strategy[blk] = EXP_NEW;
586
        else
587
            exp_strategy[blk] = EXP_REUSE;
588
    }
589

    
590
    /* now select the encoding strategy type : if exponents are often
591
       recoded, we use a coarse encoding */
592
    blk = 0;
593
    while (blk < AC3_MAX_BLOCKS) {
594
        blk1 = blk + 1;
595
        while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
596
            blk1++;
597
        switch (blk1 - blk) {
598
        case 1:  exp_strategy[blk] = EXP_D45; break;
599
        case 2:
600
        case 3:  exp_strategy[blk] = EXP_D25; break;
601
        default: exp_strategy[blk] = EXP_D15; break;
602
        }
603
        blk = blk1;
604
    }
605
}
606

    
607

    
608
/**
609
 * Calculate exponent strategies for all channels.
610
 * Array arrangement is reversed to simplify the per-channel calculation.
611
 */
612
static void compute_exp_strategy(AC3EncodeContext *s)
613
{
614
    uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
615
    uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
616
    int ch, blk;
617

    
618
    for (ch = 0; ch < s->fbw_channels; ch++) {
619
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
620
            exp1[ch][blk]     = s->blocks[blk].exp[ch];
621
            exp_str1[ch][blk] = s->blocks[blk].exp_strategy[ch];
622
        }
623

    
624
        compute_exp_strategy_ch(s, exp_str1[ch], exp1[ch]);
625

    
626
        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
627
            s->blocks[blk].exp_strategy[ch] = exp_str1[ch][blk];
628
    }
629
    if (s->lfe_on) {
630
        ch = s->lfe_channel;
631
        s->blocks[0].exp_strategy[ch] = EXP_D15;
632
        for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
633
            s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
634
    }
635
}
636

    
637

    
638
/**
639
 * Set each encoded exponent in a block to the minimum of itself and the
640
 * exponent in the same frequency bin of a following block.
641
 * exp[i] = min(exp[i], exp1[i]
642
 */
643
static void exponent_min(uint8_t *exp, uint8_t *exp1, int n)
644
{
645
    int i;
646
    for (i = 0; i < n; i++) {
647
        if (exp1[i] < exp[i])
648
            exp[i] = exp1[i];
649
    }
650
}
651

    
652

    
653
/**
654
 * Update the exponents so that they are the ones the decoder will decode.
655
 */
656
static void encode_exponents_blk_ch(uint8_t *exp,
657
                                    int nb_exps, int exp_strategy)
658
{
659
    int nb_groups, i, k;
660

    
661
    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
662

    
663
    /* for each group, compute the minimum exponent */
664
    switch(exp_strategy) {
665
    case EXP_D25:
666
        for (i = 1, k = 1; i <= nb_groups; i++) {
667
            uint8_t exp_min = exp[k];
668
            if (exp[k+1] < exp_min)
669
                exp_min = exp[k+1];
670
            exp[i] = exp_min;
671
            k += 2;
672
        }
673
        break;
674
    case EXP_D45:
675
        for (i = 1, k = 1; i <= nb_groups; i++) {
676
            uint8_t exp_min = exp[k];
677
            if (exp[k+1] < exp_min)
678
                exp_min = exp[k+1];
679
            if (exp[k+2] < exp_min)
680
                exp_min = exp[k+2];
681
            if (exp[k+3] < exp_min)
682
                exp_min = exp[k+3];
683
            exp[i] = exp_min;
684
            k += 4;
685
        }
686
        break;
687
    }
688

    
689
    /* constraint for DC exponent */
690
    if (exp[0] > 15)
691
        exp[0] = 15;
692

    
693
    /* decrease the delta between each groups to within 2 so that they can be
694
       differentially encoded */
695
    for (i = 1; i <= nb_groups; i++)
696
        exp[i] = FFMIN(exp[i], exp[i-1] + 2);
697
    i--;
698
    while (--i >= 0)
699
        exp[i] = FFMIN(exp[i], exp[i+1] + 2);
700

    
701
    /* now we have the exponent values the decoder will see */
702
    switch (exp_strategy) {
703
    case EXP_D25:
704
        for (i = nb_groups, k = nb_groups * 2; i > 0; i--) {
705
            uint8_t exp1 = exp[i];
706
            exp[k--] = exp1;
707
            exp[k--] = exp1;
708
        }
709
        break;
710
    case EXP_D45:
711
        for (i = nb_groups, k = nb_groups * 4; i > 0; i--) {
712
            exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
713
            k -= 4;
714
        }
715
        break;
716
    }
717
}
718

    
719

    
720
/**
721
 * Encode exponents from original extracted form to what the decoder will see.
722
 * This copies and groups exponents based on exponent strategy and reduces
723
 * deltas between adjacent exponent groups so that they can be differentially
724
 * encoded.
725
 */
726
static void encode_exponents(AC3EncodeContext *s)
727
{
728
    int blk, blk1, blk2, ch;
729
    AC3Block *block, *block1, *block2;
730

    
731
    for (ch = 0; ch < s->channels; ch++) {
732
        blk = 0;
733
        block = &s->blocks[0];
734
        while (blk < AC3_MAX_BLOCKS) {
735
            blk1 = blk + 1;
736
            block1 = block + 1;
737
            /* for the EXP_REUSE case we select the min of the exponents */
738
            while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) {
739
                exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]);
740
                blk1++;
741
                block1++;
742
            }
743
            encode_exponents_blk_ch(block->exp[ch], s->nb_coefs[ch],
744
                                    block->exp_strategy[ch]);
745
            /* copy encoded exponents for reuse case */
746
            block2 = block + 1;
747
            for (blk2 = blk+1; blk2 < blk1; blk2++, block2++) {
748
                memcpy(block2->exp[ch], block->exp[ch],
749
                       s->nb_coefs[ch] * sizeof(uint8_t));
750
            }
751
            blk = blk1;
752
            block = block1;
753
        }
754
    }
755
}
756

    
757

    
758
/**
759
 * Group exponents.
760
 * 3 delta-encoded exponents are in each 7-bit group. The number of groups
761
 * varies depending on exponent strategy and bandwidth.
762
 */
763
static void group_exponents(AC3EncodeContext *s)
764
{
765
    int blk, ch, i;
766
    int group_size, nb_groups, bit_count;
767
    uint8_t *p;
768
    int delta0, delta1, delta2;
769
    int exp0, exp1;
770

    
771
    bit_count = 0;
772
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
773
        AC3Block *block = &s->blocks[blk];
774
        for (ch = 0; ch < s->channels; ch++) {
775
            if (block->exp_strategy[ch] == EXP_REUSE) {
776
                continue;
777
            }
778
            group_size = block->exp_strategy[ch] + (block->exp_strategy[ch] == EXP_D45);
779
            nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
780
            bit_count += 4 + (nb_groups * 7);
781
            p = block->exp[ch];
782

    
783
            /* DC exponent */
784
            exp1 = *p++;
785
            block->grouped_exp[ch][0] = exp1;
786

    
787
            /* remaining exponents are delta encoded */
788
            for (i = 1; i <= nb_groups; i++) {
789
                /* merge three delta in one code */
790
                exp0   = exp1;
791
                exp1   = p[0];
792
                p     += group_size;
793
                delta0 = exp1 - exp0 + 2;
794

    
795
                exp0   = exp1;
796
                exp1   = p[0];
797
                p     += group_size;
798
                delta1 = exp1 - exp0 + 2;
799

    
800
                exp0   = exp1;
801
                exp1   = p[0];
802
                p     += group_size;
803
                delta2 = exp1 - exp0 + 2;
804

    
805
                block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
806
            }
807
        }
808
    }
809

    
810
    s->exponent_bits = bit_count;
811
}
812

    
813

    
814
/**
815
 * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
816
 * Extract exponents from MDCT coefficients, calculate exponent strategies,
817
 * and encode final exponents.
818
 */
819
static void process_exponents(AC3EncodeContext *s)
820
{
821
    extract_exponents(s);
822

    
823
    compute_exp_strategy(s);
824

    
825
    encode_exponents(s);
826

    
827
    group_exponents(s);
828
}
829

    
830

    
831
/**
832
 * Count frame bits that are based solely on fixed parameters.
833
 * This only has to be run once when the encoder is initialized.
834
 */
835
static void count_frame_bits_fixed(AC3EncodeContext *s)
836
{
837
    static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
838
    int blk;
839
    int frame_bits;
840

    
841
    /* assumptions:
842
     *   no dynamic range codes
843
     *   no channel coupling
844
     *   no rematrixing
845
     *   bit allocation parameters do not change between blocks
846
     *   SNR offsets do not change between blocks
847
     *   no delta bit allocation
848
     *   no skipped data
849
     *   no auxilliary data
850
     */
851

    
852
    /* header size */
853
    frame_bits = 65;
854
    frame_bits += frame_bits_inc[s->channel_mode];
855

    
856
    /* audio blocks */
857
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
858
        frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
859
        if (s->channel_mode == AC3_CHMODE_STEREO) {
860
            frame_bits++; /* rematstr */
861
            if (!blk)
862
                frame_bits += 4;
863
        }
864
        frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
865
        if (s->lfe_on)
866
            frame_bits++; /* lfeexpstr */
867
        frame_bits++; /* baie */
868
        frame_bits++; /* snr */
869
        frame_bits += 2; /* delta / skip */
870
    }
871
    frame_bits++; /* cplinu for block 0 */
872
    /* bit alloc info */
873
    /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
874
    /* csnroffset[6] */
875
    /* (fsnoffset[4] + fgaincod[4]) * c */
876
    frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);
877

    
878
    /* auxdatae, crcrsv */
879
    frame_bits += 2;
880

    
881
    /* CRC */
882
    frame_bits += 16;
883

    
884
    s->frame_bits_fixed = frame_bits;
885
}
886

    
887

    
888
/**
889
 * Initialize bit allocation.
890
 * Set default parameter codes and calculate parameter values.
891
 */
892
static void bit_alloc_init(AC3EncodeContext *s)
893
{
894
    int ch;
895

    
896
    /* init default parameters */
897
    s->slow_decay_code = 2;
898
    s->fast_decay_code = 1;
899
    s->slow_gain_code  = 1;
900
    s->db_per_bit_code = 2;
901
    s->floor_code      = 4;
902
    for (ch = 0; ch < s->channels; ch++)
903
        s->fast_gain_code[ch] = 4;
904

    
905
    /* initial snr offset */
906
    s->coarse_snr_offset = 40;
907

    
908
    /* compute real values */
909
    /* currently none of these values change during encoding, so we can just
910
       set them once at initialization */
911
    s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
912
    s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
913
    s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];
914
    s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
915
    s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
916

    
917
    count_frame_bits_fixed(s);
918
}
919

    
920

    
921
/**
922
 * Count the bits used to encode the frame, minus exponents and mantissas.
923
 * Bits based on fixed parameters have already been counted, so now we just
924
 * have to add the bits based on parameters that change during encoding.
925
 */
926
static void count_frame_bits(AC3EncodeContext *s)
927
{
928
    int blk, ch;
929
    int frame_bits = 0;
930

    
931
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
932
        uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
933
        for (ch = 0; ch < s->fbw_channels; ch++) {
934
            if (exp_strategy[ch] != EXP_REUSE)
935
                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
936
        }
937
    }
938
    s->frame_bits = s->frame_bits_fixed + frame_bits;
939
}
940

    
941

    
942
/**
943
 * Calculate the number of bits needed to encode a set of mantissas.
944
 */
945
static int compute_mantissa_size(int mant_cnt[5], uint8_t *bap, int nb_coefs)
946
{
947
    int bits, b, i;
948

    
949
    bits = 0;
950
    for (i = 0; i < nb_coefs; i++) {
951
        b = bap[i];
952
        if (b <= 4) {
953
            // bap=1 to bap=4 will be counted in compute_mantissa_size_final
954
            mant_cnt[b]++;
955
        } else if (b <= 13) {
956
            // bap=5 to bap=13 use (bap-1) bits
957
            bits += b - 1;
958
        } else {
959
            // bap=14 uses 14 bits and bap=15 uses 16 bits
960
            bits += (b == 14) ? 14 : 16;
961
        }
962
    }
963
    return bits;
964
}
965

    
966

    
967
/**
968
 * Finalize the mantissa bit count by adding in the grouped mantissas.
969
 */
970
static int compute_mantissa_size_final(int mant_cnt[5])
971
{
972
    // bap=1 : 3 mantissas in 5 bits
973
    int bits = (mant_cnt[1] / 3) * 5;
974
    // bap=2 : 3 mantissas in 7 bits
975
    // bap=4 : 2 mantissas in 7 bits
976
    bits += ((mant_cnt[2] / 3) + (mant_cnt[4] >> 1)) * 7;
977
    // bap=3 : each mantissa is 3 bits
978
    bits += mant_cnt[3] * 3;
979
    return bits;
980
}
981

    
982

    
983
/**
984
 * Calculate masking curve based on the final exponents.
985
 * Also calculate the power spectral densities to use in future calculations.
986
 */
987
static void bit_alloc_masking(AC3EncodeContext *s)
988
{
989
    int blk, ch;
990

    
991
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
992
        AC3Block *block = &s->blocks[blk];
993
        for (ch = 0; ch < s->channels; ch++) {
994
            /* We only need psd and mask for calculating bap.
995
               Since we currently do not calculate bap when exponent
996
               strategy is EXP_REUSE we do not need to calculate psd or mask. */
997
            if (block->exp_strategy[ch] != EXP_REUSE) {
998
                ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
999
                                          s->nb_coefs[ch],
1000
                                          block->psd[ch], block->band_psd[ch]);
1001
                ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
1002
                                           0, s->nb_coefs[ch],
1003
                                           ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
1004
                                           ch == s->lfe_channel,
1005
                                           DBA_NONE, 0, NULL, NULL, NULL,
1006
                                           block->mask[ch]);
1007
            }
1008
        }
1009
    }
1010
}
1011

    
1012

    
1013
/**
1014
 * Ensure that bap for each block and channel point to the current bap_buffer.
1015
 * They may have been switched during the bit allocation search.
1016
 */
1017
static void reset_block_bap(AC3EncodeContext *s)
1018
{
1019
    int blk, ch;
1020
    if (s->blocks[0].bap[0] == s->bap_buffer)
1021
        return;
1022
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1023
        for (ch = 0; ch < s->channels; ch++) {
1024
            s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
1025
        }
1026
    }
1027
}
1028

    
1029

    
1030
/**
1031
 * Run the bit allocation with a given SNR offset.
1032
 * This calculates the bit allocation pointers that will be used to determine
1033
 * the quantization of each mantissa.
1034
 * @return the number of bits needed for mantissas if the given SNR offset is
1035
 *         is used.
1036
 */
1037
static int bit_alloc(AC3EncodeContext *s,
1038
                     int snr_offset)
1039
{
1040
    int blk, ch;
1041
    int mantissa_bits;
1042
    int mant_cnt[5];
1043

    
1044
    snr_offset = (snr_offset - 240) << 2;
1045

    
1046
    reset_block_bap(s);
1047
    mantissa_bits = 0;
1048
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1049
        AC3Block *block = &s->blocks[blk];
1050
        // initialize grouped mantissa counts. these are set so that they are
1051
        // padded to the next whole group size when bits are counted in
1052
        // compute_mantissa_size_final
1053
        mant_cnt[0] = mant_cnt[3] = 0;
1054
        mant_cnt[1] = mant_cnt[2] = 2;
1055
        mant_cnt[4] = 1;
1056
        for (ch = 0; ch < s->channels; ch++) {
1057
            /* Currently the only bit allocation parameters which vary across
1058
               blocks within a frame are the exponent values.  We can take
1059
               advantage of that by reusing the bit allocation pointers
1060
               whenever we reuse exponents. */
1061
            if (block->exp_strategy[ch] == EXP_REUSE) {
1062
                memcpy(block->bap[ch], s->blocks[blk-1].bap[ch], AC3_MAX_COEFS);
1063
            } else {
1064
                ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
1065
                                          s->nb_coefs[ch], snr_offset,
1066
                                          s->bit_alloc.floor, ff_ac3_bap_tab,
1067
                                          block->bap[ch]);
1068
            }
1069
            mantissa_bits += compute_mantissa_size(mant_cnt, block->bap[ch], s->nb_coefs[ch]);
1070
        }
1071
        mantissa_bits += compute_mantissa_size_final(mant_cnt);
1072
    }
1073
    return mantissa_bits;
1074
}
1075

    
1076

    
1077
/**
1078
 * Constant bitrate bit allocation search.
1079
 * Find the largest SNR offset that will allow data to fit in the frame.
1080
 */
1081
static int cbr_bit_allocation(AC3EncodeContext *s)
1082
{
1083
    int ch;
1084
    int bits_left;
1085
    int snr_offset, snr_incr;
1086

    
1087
    bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
1088

    
1089
    snr_offset = s->coarse_snr_offset << 4;
1090

    
1091
    while (snr_offset >= 0 &&
1092
           bit_alloc(s, snr_offset) > bits_left) {
1093
        snr_offset -= 64;
1094
    }
1095
    if (snr_offset < 0)
1096
        return AVERROR(EINVAL);
1097

    
1098
    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1099
    for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1100
        while (snr_offset + 64 <= 1023 &&
1101
               bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
1102
            snr_offset += snr_incr;
1103
            FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1104
        }
1105
    }
1106
    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1107
    reset_block_bap(s);
1108

    
1109
    s->coarse_snr_offset = snr_offset >> 4;
1110
    for (ch = 0; ch < s->channels; ch++)
1111
        s->fine_snr_offset[ch] = snr_offset & 0xF;
1112

    
1113
    return 0;
1114
}
1115

    
1116

    
1117
/**
1118
 * Perform bit allocation search.
1119
 * Finds the SNR offset value that maximizes quality and fits in the specified
1120
 * frame size.  Output is the SNR offset and a set of bit allocation pointers
1121
 * used to quantize the mantissas.
1122
 */
1123
static int compute_bit_allocation(AC3EncodeContext *s)
1124
{
1125
    count_frame_bits(s);
1126

    
1127
    bit_alloc_masking(s);
1128

    
1129
    return cbr_bit_allocation(s);
1130
}
1131

    
1132

    
1133
/**
1134
 * Symmetric quantization on 'levels' levels.
1135
 */
1136
static inline int sym_quant(int c, int e, int levels)
1137
{
1138
    int v;
1139

    
1140
    if (c >= 0) {
1141
        v = (levels * (c << e)) >> 24;
1142
        v = (v + 1) >> 1;
1143
        v = (levels >> 1) + v;
1144
    } else {
1145
        v = (levels * ((-c) << e)) >> 24;
1146
        v = (v + 1) >> 1;
1147
        v = (levels >> 1) - v;
1148
    }
1149
    assert(v >= 0 && v < levels);
1150
    return v;
1151
}
1152

    
1153

    
1154
/**
1155
 * Asymmetric quantization on 2^qbits levels.
1156
 */
1157
static inline int asym_quant(int c, int e, int qbits)
1158
{
1159
    int lshift, m, v;
1160

    
1161
    lshift = e + qbits - 24;
1162
    if (lshift >= 0)
1163
        v = c << lshift;
1164
    else
1165
        v = c >> (-lshift);
1166
    /* rounding */
1167
    v = (v + 1) >> 1;
1168
    m = (1 << (qbits-1));
1169
    if (v >= m)
1170
        v = m - 1;
1171
    assert(v >= -m);
1172
    return v & ((1 << qbits)-1);
1173
}
1174

    
1175

    
1176
/**
1177
 * Quantize a set of mantissas for a single channel in a single block.
1178
 */
1179
static void quantize_mantissas_blk_ch(AC3EncodeContext *s,
1180
                                      int32_t *mdct_coef, int8_t exp_shift,
1181
                                      uint8_t *exp, uint8_t *bap,
1182
                                      uint16_t *qmant, int n)
1183
{
1184
    int i;
1185

    
1186
    for (i = 0; i < n; i++) {
1187
        int v;
1188
        int c = mdct_coef[i];
1189
        int e = exp[i] - exp_shift;
1190
        int b = bap[i];
1191
        switch (b) {
1192
        case 0:
1193
            v = 0;
1194
            break;
1195
        case 1:
1196
            v = sym_quant(c, e, 3);
1197
            switch (s->mant1_cnt) {
1198
            case 0:
1199
                s->qmant1_ptr = &qmant[i];
1200
                v = 9 * v;
1201
                s->mant1_cnt = 1;
1202
                break;
1203
            case 1:
1204
                *s->qmant1_ptr += 3 * v;
1205
                s->mant1_cnt = 2;
1206
                v = 128;
1207
                break;
1208
            default:
1209
                *s->qmant1_ptr += v;
1210
                s->mant1_cnt = 0;
1211
                v = 128;
1212
                break;
1213
            }
1214
            break;
1215
        case 2:
1216
            v = sym_quant(c, e, 5);
1217
            switch (s->mant2_cnt) {
1218
            case 0:
1219
                s->qmant2_ptr = &qmant[i];
1220
                v = 25 * v;
1221
                s->mant2_cnt = 1;
1222
                break;
1223
            case 1:
1224
                *s->qmant2_ptr += 5 * v;
1225
                s->mant2_cnt = 2;
1226
                v = 128;
1227
                break;
1228
            default:
1229
                *s->qmant2_ptr += v;
1230
                s->mant2_cnt = 0;
1231
                v = 128;
1232
                break;
1233
            }
1234
            break;
1235
        case 3:
1236
            v = sym_quant(c, e, 7);
1237
            break;
1238
        case 4:
1239
            v = sym_quant(c, e, 11);
1240
            switch (s->mant4_cnt) {
1241
            case 0:
1242
                s->qmant4_ptr = &qmant[i];
1243
                v = 11 * v;
1244
                s->mant4_cnt = 1;
1245
                break;
1246
            default:
1247
                *s->qmant4_ptr += v;
1248
                s->mant4_cnt = 0;
1249
                v = 128;
1250
                break;
1251
            }
1252
            break;
1253
        case 5:
1254
            v = sym_quant(c, e, 15);
1255
            break;
1256
        case 14:
1257
            v = asym_quant(c, e, 14);
1258
            break;
1259
        case 15:
1260
            v = asym_quant(c, e, 16);
1261
            break;
1262
        default:
1263
            v = asym_quant(c, e, b - 1);
1264
            break;
1265
        }
1266
        qmant[i] = v;
1267
    }
1268
}
1269

    
1270

    
1271
/**
1272
 * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1273
 */
1274
static void quantize_mantissas(AC3EncodeContext *s)
1275
{
1276
    int blk, ch;
1277

    
1278

    
1279
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1280
        AC3Block *block = &s->blocks[blk];
1281
        s->mant1_cnt  = s->mant2_cnt  = s->mant4_cnt  = 0;
1282
        s->qmant1_ptr = s->qmant2_ptr = s->qmant4_ptr = NULL;
1283

    
1284
        for (ch = 0; ch < s->channels; ch++) {
1285
            quantize_mantissas_blk_ch(s, block->mdct_coef[ch], block->exp_shift[ch],
1286
                                      block->exp[ch], block->bap[ch],
1287
                                      block->qmant[ch], s->nb_coefs[ch]);
1288
        }
1289
    }
1290
}
1291

    
1292

    
1293
/**
1294
 * Write the AC-3 frame header to the output bitstream.
1295
 */
1296
static void output_frame_header(AC3EncodeContext *s)
1297
{
1298
    put_bits(&s->pb, 16, 0x0b77);   /* frame header */
1299
    put_bits(&s->pb, 16, 0);        /* crc1: will be filled later */
1300
    put_bits(&s->pb, 2,  s->bit_alloc.sr_code);
1301
    put_bits(&s->pb, 6,  s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
1302
    put_bits(&s->pb, 5,  s->bitstream_id);
1303
    put_bits(&s->pb, 3,  s->bitstream_mode);
1304
    put_bits(&s->pb, 3,  s->channel_mode);
1305
    if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
1306
        put_bits(&s->pb, 2, 1);     /* XXX -4.5 dB */
1307
    if (s->channel_mode & 0x04)
1308
        put_bits(&s->pb, 2, 1);     /* XXX -6 dB */
1309
    if (s->channel_mode == AC3_CHMODE_STEREO)
1310
        put_bits(&s->pb, 2, 0);     /* surround not indicated */
1311
    put_bits(&s->pb, 1, s->lfe_on); /* LFE */
1312
    put_bits(&s->pb, 5, 31);        /* dialog norm: -31 db */
1313
    put_bits(&s->pb, 1, 0);         /* no compression control word */
1314
    put_bits(&s->pb, 1, 0);         /* no lang code */
1315
    put_bits(&s->pb, 1, 0);         /* no audio production info */
1316
    put_bits(&s->pb, 1, 0);         /* no copyright */
1317
    put_bits(&s->pb, 1, 1);         /* original bitstream */
1318
    put_bits(&s->pb, 1, 0);         /* no time code 1 */
1319
    put_bits(&s->pb, 1, 0);         /* no time code 2 */
1320
    put_bits(&s->pb, 1, 0);         /* no additional bit stream info */
1321
}
1322

    
1323

    
1324
/**
1325
 * Write one audio block to the output bitstream.
1326
 */
1327
static void output_audio_block(AC3EncodeContext *s,
1328
                               int block_num)
1329
{
1330
    int ch, i, baie, rbnd;
1331
    AC3Block *block = &s->blocks[block_num];
1332

    
1333
    /* block switching */
1334
    for (ch = 0; ch < s->fbw_channels; ch++)
1335
        put_bits(&s->pb, 1, 0);
1336

    
1337
    /* dither flags */
1338
    for (ch = 0; ch < s->fbw_channels; ch++)
1339
        put_bits(&s->pb, 1, 1);
1340

    
1341
    /* dynamic range codes */
1342
    put_bits(&s->pb, 1, 0);
1343

    
1344
    /* channel coupling */
1345
    if (!block_num) {
1346
        put_bits(&s->pb, 1, 1); /* coupling strategy present */
1347
        put_bits(&s->pb, 1, 0); /* no coupling strategy */
1348
    } else {
1349
        put_bits(&s->pb, 1, 0); /* no new coupling strategy */
1350
    }
1351

    
1352
    /* stereo rematrixing */
1353
    if (s->channel_mode == AC3_CHMODE_STEREO) {
1354
        if (!block_num) {
1355
            /* first block must define rematrixing (rematstr) */
1356
            put_bits(&s->pb, 1, 1);
1357

    
1358
            /* dummy rematrixing rematflg(1:4)=0 */
1359
            for (rbnd = 0; rbnd < 4; rbnd++)
1360
                put_bits(&s->pb, 1, 0);
1361
        } else {
1362
            /* no matrixing (but should be used in the future) */
1363
            put_bits(&s->pb, 1, 0);
1364
        }
1365
    }
1366

    
1367
    /* exponent strategy */
1368
    for (ch = 0; ch < s->fbw_channels; ch++)
1369
        put_bits(&s->pb, 2, block->exp_strategy[ch]);
1370
    if (s->lfe_on)
1371
        put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]);
1372

    
1373
    /* bandwidth */
1374
    for (ch = 0; ch < s->fbw_channels; ch++) {
1375
        if (block->exp_strategy[ch] != EXP_REUSE)
1376
            put_bits(&s->pb, 6, s->bandwidth_code[ch]);
1377
    }
1378

    
1379
    /* exponents */
1380
    for (ch = 0; ch < s->channels; ch++) {
1381
        int nb_groups;
1382

    
1383
        if (block->exp_strategy[ch] == EXP_REUSE)
1384
            continue;
1385

    
1386
        /* DC exponent */
1387
        put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
1388

    
1389
        /* exponent groups */
1390
        nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
1391
        for (i = 1; i <= nb_groups; i++)
1392
            put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
1393

    
1394
        /* gain range info */
1395
        if (ch != s->lfe_channel)
1396
            put_bits(&s->pb, 2, 0);
1397
    }
1398

    
1399
    /* bit allocation info */
1400
    baie = (block_num == 0);
1401
    put_bits(&s->pb, 1, baie);
1402
    if (baie) {
1403
        put_bits(&s->pb, 2, s->slow_decay_code);
1404
        put_bits(&s->pb, 2, s->fast_decay_code);
1405
        put_bits(&s->pb, 2, s->slow_gain_code);
1406
        put_bits(&s->pb, 2, s->db_per_bit_code);
1407
        put_bits(&s->pb, 3, s->floor_code);
1408
    }
1409

    
1410
    /* snr offset */
1411
    put_bits(&s->pb, 1, baie);
1412
    if (baie) {
1413
        put_bits(&s->pb, 6, s->coarse_snr_offset);
1414
        for (ch = 0; ch < s->channels; ch++) {
1415
            put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
1416
            put_bits(&s->pb, 3, s->fast_gain_code[ch]);
1417
        }
1418
    }
1419

    
1420
    put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1421
    put_bits(&s->pb, 1, 0); /* no data to skip */
1422

    
1423
    /* mantissas */
1424
    for (ch = 0; ch < s->channels; ch++) {
1425
        int b, q;
1426
        for (i = 0; i < s->nb_coefs[ch]; i++) {
1427
            q = block->qmant[ch][i];
1428
            b = block->bap[ch][i];
1429
            switch (b) {
1430
            case 0:                                         break;
1431
            case 1: if (q != 128) put_bits(&s->pb,   5, q); break;
1432
            case 2: if (q != 128) put_bits(&s->pb,   7, q); break;
1433
            case 3:               put_bits(&s->pb,   3, q); break;
1434
            case 4: if (q != 128) put_bits(&s->pb,   7, q); break;
1435
            case 14:              put_bits(&s->pb,  14, q); break;
1436
            case 15:              put_bits(&s->pb,  16, q); break;
1437
            default:              put_bits(&s->pb, b-1, q); break;
1438
            }
1439
        }
1440
    }
1441
}
1442

    
1443

    
1444
/** CRC-16 Polynomial */
1445
#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1446

    
1447

    
1448
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1449
{
1450
    unsigned int c;
1451

    
1452
    c = 0;
1453
    while (a) {
1454
        if (a & 1)
1455
            c ^= b;
1456
        a = a >> 1;
1457
        b = b << 1;
1458
        if (b & (1 << 16))
1459
            b ^= poly;
1460
    }
1461
    return c;
1462
}
1463

    
1464

    
1465
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
1466
{
1467
    unsigned int r;
1468
    r = 1;
1469
    while (n) {
1470
        if (n & 1)
1471
            r = mul_poly(r, a, poly);
1472
        a = mul_poly(a, a, poly);
1473
        n >>= 1;
1474
    }
1475
    return r;
1476
}
1477

    
1478

    
1479
/**
1480
 * Fill the end of the frame with 0's and compute the two CRCs.
1481
 */
1482
static void output_frame_end(AC3EncodeContext *s)
1483
{
1484
    int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1485
    uint8_t *frame;
1486

    
1487
    frame_size    = s->frame_size;
1488
    frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1;
1489

    
1490
    /* pad the remainder of the frame with zeros */
1491
    flush_put_bits(&s->pb);
1492
    frame = s->pb.buf;
1493
    pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
1494
    assert(pad_bytes >= 0);
1495
    if (pad_bytes > 0)
1496
        memset(put_bits_ptr(&s->pb), 0, pad_bytes);
1497

    
1498
    /* compute crc1 */
1499
    /* this is not so easy because it is at the beginning of the data... */
1500
    crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1501
                             frame + 4, frame_size_58 - 4));
1502
    /* XXX: could precompute crc_inv */
1503
    crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1504
    crc1    = mul_poly(crc_inv, crc1, CRC16_POLY);
1505
    AV_WB16(frame + 2, crc1);
1506

    
1507
    /* compute crc2 */
1508
    crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1509
                             frame + frame_size_58,
1510
                             frame_size - frame_size_58 - 2));
1511
    AV_WB16(frame + frame_size - 2, crc2);
1512
}
1513

    
1514

    
1515
/**
1516
 * Write the frame to the output bitstream.
1517
 */
1518
static void output_frame(AC3EncodeContext *s,
1519
                         unsigned char *frame)
1520
{
1521
    int blk;
1522

    
1523
    init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
1524

    
1525
    output_frame_header(s);
1526

    
1527
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1528
        output_audio_block(s, blk);
1529

    
1530
    output_frame_end(s);
1531
}
1532

    
1533

    
1534
/**
1535
 * Encode a single AC-3 frame.
1536
 */
1537
static int ac3_encode_frame(AVCodecContext *avctx,
1538
                            unsigned char *frame, int buf_size, void *data)
1539
{
1540
    AC3EncodeContext *s = avctx->priv_data;
1541
    const int16_t *samples = data;
1542
    int ret;
1543

    
1544
    if (s->bit_alloc.sr_code == 1)
1545
        adjust_frame_size(s);
1546

    
1547
    deinterleave_input_samples(s, samples);
1548

    
1549
    apply_mdct(s);
1550

    
1551
    process_exponents(s);
1552

    
1553
    ret = compute_bit_allocation(s);
1554
    if (ret) {
1555
        av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
1556
        return ret;
1557
    }
1558

    
1559
    quantize_mantissas(s);
1560

    
1561
    output_frame(s, frame);
1562

    
1563
    return s->frame_size;
1564
}
1565

    
1566

    
1567
/**
1568
 * Finalize encoding and free any memory allocated by the encoder.
1569
 */
1570
static av_cold int ac3_encode_close(AVCodecContext *avctx)
1571
{
1572
    int blk, ch;
1573
    AC3EncodeContext *s = avctx->priv_data;
1574

    
1575
    for (ch = 0; ch < s->channels; ch++)
1576
        av_freep(&s->planar_samples[ch]);
1577
    av_freep(&s->planar_samples);
1578
    av_freep(&s->bap_buffer);
1579
    av_freep(&s->bap1_buffer);
1580
    av_freep(&s->mdct_coef_buffer);
1581
    av_freep(&s->exp_buffer);
1582
    av_freep(&s->grouped_exp_buffer);
1583
    av_freep(&s->psd_buffer);
1584
    av_freep(&s->band_psd_buffer);
1585
    av_freep(&s->mask_buffer);
1586
    av_freep(&s->qmant_buffer);
1587
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1588
        AC3Block *block = &s->blocks[blk];
1589
        av_freep(&block->bap);
1590
        av_freep(&block->mdct_coef);
1591
        av_freep(&block->exp);
1592
        av_freep(&block->grouped_exp);
1593
        av_freep(&block->psd);
1594
        av_freep(&block->band_psd);
1595
        av_freep(&block->mask);
1596
        av_freep(&block->qmant);
1597
    }
1598

    
1599
    mdct_end(&s->mdct);
1600

    
1601
    av_freep(&avctx->coded_frame);
1602
    return 0;
1603
}
1604

    
1605

    
1606
/**
1607
 * Set channel information during initialization.
1608
 */
1609
static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
1610
                                    int64_t *channel_layout)
1611
{
1612
    int ch_layout;
1613

    
1614
    if (channels < 1 || channels > AC3_MAX_CHANNELS)
1615
        return AVERROR(EINVAL);
1616
    if ((uint64_t)*channel_layout > 0x7FF)
1617
        return AVERROR(EINVAL);
1618
    ch_layout = *channel_layout;
1619
    if (!ch_layout)
1620
        ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
1621
    if (av_get_channel_layout_nb_channels(ch_layout) != channels)
1622
        return AVERROR(EINVAL);
1623

    
1624
    s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
1625
    s->channels     = channels;
1626
    s->fbw_channels = channels - s->lfe_on;
1627
    s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
1628
    if (s->lfe_on)
1629
        ch_layout -= AV_CH_LOW_FREQUENCY;
1630

    
1631
    switch (ch_layout) {
1632
    case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;
1633
    case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;
1634
    case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;
1635
    case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;
1636
    case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;
1637
    case AV_CH_LAYOUT_QUAD:
1638
    case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;
1639
    case AV_CH_LAYOUT_5POINT0:
1640
    case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;
1641
    default:
1642
        return AVERROR(EINVAL);
1643
    }
1644

    
1645
    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
1646
    *channel_layout = ch_layout;
1647
    if (s->lfe_on)
1648
        *channel_layout |= AV_CH_LOW_FREQUENCY;
1649

    
1650
    return 0;
1651
}
1652

    
1653

    
1654
static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
1655
{
1656
    int i, ret;
1657

    
1658
    /* validate channel layout */
1659
    if (!avctx->channel_layout) {
1660
        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
1661
                                      "encoder will guess the layout, but it "
1662
                                      "might be incorrect.\n");
1663
    }
1664
    ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
1665
    if (ret) {
1666
        av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
1667
        return ret;
1668
    }
1669

    
1670
    /* validate sample rate */
1671
    for (i = 0; i < 9; i++) {
1672
        if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
1673
            break;
1674
    }
1675
    if (i == 9) {
1676
        av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
1677
        return AVERROR(EINVAL);
1678
    }
1679
    s->sample_rate        = avctx->sample_rate;
1680
    s->bit_alloc.sr_shift = i % 3;
1681
    s->bit_alloc.sr_code  = i / 3;
1682

    
1683
    /* validate bit rate */
1684
    for (i = 0; i < 19; i++) {
1685
        if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
1686
            break;
1687
    }
1688
    if (i == 19) {
1689
        av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
1690
        return AVERROR(EINVAL);
1691
    }
1692
    s->bit_rate        = avctx->bit_rate;
1693
    s->frame_size_code = i << 1;
1694

    
1695
    /* validate cutoff */
1696
    if (avctx->cutoff < 0) {
1697
        av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
1698
        return AVERROR(EINVAL);
1699
    }
1700
    s->cutoff = avctx->cutoff;
1701
    if (s->cutoff > (s->sample_rate >> 1))
1702
        s->cutoff = s->sample_rate >> 1;
1703

    
1704
    return 0;
1705
}
1706

    
1707

    
1708
/**
1709
 * Set bandwidth for all channels.
1710
 * The user can optionally supply a cutoff frequency. Otherwise an appropriate
1711
 * default value will be used.
1712
 */
1713
static av_cold void set_bandwidth(AC3EncodeContext *s)
1714
{
1715
    int ch, bw_code;
1716

    
1717
    if (s->cutoff) {
1718
        /* calculate bandwidth based on user-specified cutoff frequency */
1719
        int fbw_coeffs;
1720
        fbw_coeffs     = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
1721
        bw_code        = av_clip((fbw_coeffs - 73) / 3, 0, 60);
1722
    } else {
1723
        /* use default bandwidth setting */
1724
        /* XXX: should compute the bandwidth according to the frame
1725
           size, so that we avoid annoying high frequency artifacts */
1726
        bw_code = 50;
1727
    }
1728

    
1729
    /* set number of coefficients for each channel */
1730
    for (ch = 0; ch < s->fbw_channels; ch++) {
1731
        s->bandwidth_code[ch] = bw_code;
1732
        s->nb_coefs[ch]       = bw_code * 3 + 73;
1733
    }
1734
    if (s->lfe_on)
1735
        s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
1736
}
1737

    
1738

    
1739
static av_cold int allocate_buffers(AVCodecContext *avctx)
1740
{
1741
    int blk, ch;
1742
    AC3EncodeContext *s = avctx->priv_data;
1743

    
1744
    FF_ALLOC_OR_GOTO(avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
1745
                     alloc_fail);
1746
    for (ch = 0; ch < s->channels; ch++) {
1747
        FF_ALLOCZ_OR_GOTO(avctx, s->planar_samples[ch],
1748
                          (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
1749
                          alloc_fail);
1750
    }
1751
    FF_ALLOC_OR_GOTO(avctx, s->bap_buffer,  AC3_MAX_BLOCKS * s->channels *
1752
                     AC3_MAX_COEFS * sizeof(*s->bap_buffer),  alloc_fail);
1753
    FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
1754
                     AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
1755
    FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
1756
                     AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
1757
    FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
1758
                     AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
1759
    FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * s->channels *
1760
                     128 * sizeof(*s->grouped_exp_buffer), alloc_fail);
1761
    FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
1762
                     AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
1763
    FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * s->channels *
1764
                     64 * sizeof(*s->band_psd_buffer), alloc_fail);
1765
    FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
1766
                     64 * sizeof(*s->mask_buffer), alloc_fail);
1767
    FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * s->channels *
1768
                     AC3_MAX_COEFS * sizeof(*s->qmant_buffer), alloc_fail);
1769
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1770
        AC3Block *block = &s->blocks[blk];
1771
        FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
1772
                         alloc_fail);
1773
        FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
1774
                          alloc_fail);
1775
        FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
1776
                          alloc_fail);
1777
        FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, s->channels * sizeof(*block->grouped_exp),
1778
                          alloc_fail);
1779
        FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
1780
                          alloc_fail);
1781
        FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, s->channels * sizeof(*block->band_psd),
1782
                          alloc_fail);
1783
        FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
1784
                          alloc_fail);
1785
        FF_ALLOCZ_OR_GOTO(avctx, block->qmant, s->channels * sizeof(*block->qmant),
1786
                          alloc_fail);
1787

    
1788
        for (ch = 0; ch < s->channels; ch++) {
1789
            block->bap[ch]         = &s->bap_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1790
            block->mdct_coef[ch]   = &s->mdct_coef_buffer  [AC3_MAX_COEFS * (blk * s->channels + ch)];
1791
            block->exp[ch]         = &s->exp_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1792
            block->grouped_exp[ch] = &s->grouped_exp_buffer[128           * (blk * s->channels + ch)];
1793
            block->psd[ch]         = &s->psd_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1794
            block->band_psd[ch]    = &s->band_psd_buffer   [64            * (blk * s->channels + ch)];
1795
            block->mask[ch]        = &s->mask_buffer       [64            * (blk * s->channels + ch)];
1796
            block->qmant[ch]       = &s->qmant_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
1797
        }
1798
    }
1799

    
1800
    return 0;
1801
alloc_fail:
1802
    return AVERROR(ENOMEM);
1803
}
1804

    
1805

    
1806
/**
1807
 * Initialize the encoder.
1808
 */
1809
static av_cold int ac3_encode_init(AVCodecContext *avctx)
1810
{
1811
    AC3EncodeContext *s = avctx->priv_data;
1812
    int ret;
1813

    
1814
    avctx->frame_size = AC3_FRAME_SIZE;
1815

    
1816
    ac3_common_init();
1817

    
1818
    ret = validate_options(avctx, s);
1819
    if (ret)
1820
        return ret;
1821

    
1822
    s->bitstream_id   = 8 + s->bit_alloc.sr_shift;
1823
    s->bitstream_mode = 0; /* complete main audio service */
1824

    
1825
    s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
1826
    s->bits_written    = 0;
1827
    s->samples_written = 0;
1828
    s->frame_size      = s->frame_size_min;
1829

    
1830
    set_bandwidth(s);
1831

    
1832
    exponent_init(s);
1833

    
1834
    bit_alloc_init(s);
1835

    
1836
    s->mdct.avctx = avctx;
1837
    ret = mdct_init(&s->mdct, 9);
1838
    if (ret)
1839
        goto init_fail;
1840

    
1841
    ret = allocate_buffers(avctx);
1842
    if (ret)
1843
        goto init_fail;
1844

    
1845
    avctx->coded_frame= avcodec_alloc_frame();
1846

    
1847
    dsputil_init(&s->dsp, avctx);
1848

    
1849
    return 0;
1850
init_fail:
1851
    ac3_encode_close(avctx);
1852
    return ret;
1853
}
1854

    
1855

    
1856
#ifdef TEST
1857
/*************************************************************************/
1858
/* TEST */
1859

    
1860
#include "libavutil/lfg.h"
1861

    
1862
#define MDCT_NBITS 9
1863
#define MDCT_SAMPLES (1 << MDCT_NBITS)
1864
#define FN (MDCT_SAMPLES/4)
1865

    
1866

    
1867
static void fft_test(AC3MDCTContext *mdct, AVLFG *lfg)
1868
{
1869
    IComplex in[FN], in1[FN];
1870
    int k, n, i;
1871
    float sum_re, sum_im, a;
1872

    
1873
    for (i = 0; i < FN; i++) {
1874
        in[i].re = av_lfg_get(lfg) % 65535 - 32767;
1875
        in[i].im = av_lfg_get(lfg) % 65535 - 32767;
1876
        in1[i]   = in[i];
1877
    }
1878
    fft(mdct, in, 7);
1879

    
1880
    /* do it by hand */
1881
    for (k = 0; k < FN; k++) {
1882
        sum_re = 0;
1883
        sum_im = 0;
1884
        for (n = 0; n < FN; n++) {
1885
            a = -2 * M_PI * (n * k) / FN;
1886
            sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1887
            sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1888
        }
1889
        av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
1890
               k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1891
    }
1892
}
1893

    
1894

    
1895
static void mdct_test(AC3MDCTContext *mdct, AVLFG *lfg)
1896
{
1897
    int16_t input[MDCT_SAMPLES];
1898
    int32_t output[AC3_MAX_COEFS];
1899
    float input1[MDCT_SAMPLES];
1900
    float output1[AC3_MAX_COEFS];
1901
    float s, a, err, e, emax;
1902
    int i, k, n;
1903

    
1904
    for (i = 0; i < MDCT_SAMPLES; i++) {
1905
        input[i]  = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
1906
        input1[i] = input[i];
1907
    }
1908

    
1909
    mdct512(mdct, output, input);
1910

    
1911
    /* do it by hand */
1912
    for (k = 0; k < AC3_MAX_COEFS; k++) {
1913
        s = 0;
1914
        for (n = 0; n < MDCT_SAMPLES; n++) {
1915
            a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
1916
            s += input1[n] * cos(a);
1917
        }
1918
        output1[k] = -2 * s / MDCT_SAMPLES;
1919
    }
1920

    
1921
    err  = 0;
1922
    emax = 0;
1923
    for (i = 0; i < AC3_MAX_COEFS; i++) {
1924
        av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
1925
        e = output[i] - output1[i];
1926
        if (e > emax)
1927
            emax = e;
1928
        err += e * e;
1929
    }
1930
    av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
1931
}
1932

    
1933

    
1934
int main(void)
1935
{
1936
    AVLFG lfg;
1937
    AC3MDCTContext mdct;
1938

    
1939
    mdct.avctx = NULL;
1940
    av_log_set_level(AV_LOG_DEBUG);
1941
    mdct_init(&mdct, 9);
1942

    
1943
    fft_test(&mdct, &lfg);
1944
    mdct_test(&mdct, &lfg);
1945

    
1946
    return 0;
1947
}
1948
#endif /* TEST */
1949

    
1950

    
1951
AVCodec ac3_encoder = {
1952
    "ac3",
1953
    AVMEDIA_TYPE_AUDIO,
1954
    CODEC_ID_AC3,
1955
    sizeof(AC3EncodeContext),
1956
    ac3_encode_init,
1957
    ac3_encode_frame,
1958
    ac3_encode_close,
1959
    NULL,
1960
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
1961
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
1962
    .channel_layouts = (const int64_t[]){
1963
        AV_CH_LAYOUT_MONO,
1964
        AV_CH_LAYOUT_STEREO,
1965
        AV_CH_LAYOUT_2_1,
1966
        AV_CH_LAYOUT_SURROUND,
1967
        AV_CH_LAYOUT_2_2,
1968
        AV_CH_LAYOUT_QUAD,
1969
        AV_CH_LAYOUT_4POINT0,
1970
        AV_CH_LAYOUT_5POINT0,
1971
        AV_CH_LAYOUT_5POINT0_BACK,
1972
       (AV_CH_LAYOUT_MONO     | AV_CH_LOW_FREQUENCY),
1973
       (AV_CH_LAYOUT_STEREO   | AV_CH_LOW_FREQUENCY),
1974
       (AV_CH_LAYOUT_2_1      | AV_CH_LOW_FREQUENCY),
1975
       (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
1976
       (AV_CH_LAYOUT_2_2      | AV_CH_LOW_FREQUENCY),
1977
       (AV_CH_LAYOUT_QUAD     | AV_CH_LOW_FREQUENCY),
1978
       (AV_CH_LAYOUT_4POINT0  | AV_CH_LOW_FREQUENCY),
1979
        AV_CH_LAYOUT_5POINT1,
1980
        AV_CH_LAYOUT_5POINT1_BACK,
1981
        0 },
1982
};