Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3enc.c @ a897423b

History | View | Annotate | Download (62.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 range -32767..32767. */
47
#define FIX15(a) av_clip(SCALE_FLOAT(a, 15), -32767, 32767)
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
    uint16_t crc_inv[2];
105
    int bits_written;                       ///< bit count    (used to avg. bitrate)
106
    int samples_written;                    ///< sample count (used to avg. bitrate)
107

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

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

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

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

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

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

    
151

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

    
158

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

    
175

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

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

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

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

    
204

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

    
219

    
220

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

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

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

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

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

    
250

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

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

    
262
    mdct->nbits = nbits;
263

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

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

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

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

    
289

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

    
304

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

    
312

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

    
325
    np = 1 << ln;
326

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

    
334
    /* pass 0 */
335

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

    
344
    /* pass 1 */
345

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

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

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

    
384

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

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

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

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

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

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

    
422

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

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

    
438

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

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

    
453
    return av_log2(v);
454
}
455

    
456

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

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

    
477

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

    
493

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

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

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

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

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

    
517

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

    
533

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

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

    
564

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

    
571

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

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

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

    
609

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

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

    
626
        compute_exp_strategy_ch(s, exp_str1[ch], exp1[ch]);
627

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

    
639

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

    
654

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

    
663
    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
664

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

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

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

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

    
721

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

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

    
759

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

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

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

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

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

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

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

    
812
    s->exponent_bits = bit_count;
813
}
814

    
815

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

    
825
    compute_exp_strategy(s);
826

    
827
    encode_exponents(s);
828

    
829
    group_exponents(s);
830
}
831

    
832

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

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

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

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

    
880
    /* auxdatae, crcrsv */
881
    frame_bits += 2;
882

    
883
    /* CRC */
884
    frame_bits += 16;
885

    
886
    s->frame_bits_fixed = frame_bits;
887
}
888

    
889

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

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

    
907
    /* initial snr offset */
908
    s->coarse_snr_offset = 40;
909

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

    
919
    count_frame_bits_fixed(s);
920
}
921

    
922

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

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

    
943

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

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

    
968

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

    
984

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

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

    
1014

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

    
1031

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

    
1046
    snr_offset = (snr_offset - 240) << 2;
1047

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

    
1078

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

    
1089
    bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
1090

    
1091
    snr_offset = s->coarse_snr_offset << 4;
1092

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

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

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

    
1115
    return 0;
1116
}
1117

    
1118

    
1119
/**
1120
 * Downgrade exponent strategies to reduce the bits used by the exponents.
1121
 * This is a fallback for when bit allocation fails with the normal exponent
1122
 * strategies.  Each time this function is run it only downgrades the
1123
 * strategy in 1 channel of 1 block.
1124
 * @return non-zero if downgrade was unsuccessful
1125
 */
1126
static int downgrade_exponents(AC3EncodeContext *s)
1127
{
1128
    int ch, blk;
1129

    
1130
    for (ch = 0; ch < s->fbw_channels; ch++) {
1131
        for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1132
            if (s->blocks[blk].exp_strategy[ch] == EXP_D15) {
1133
                s->blocks[blk].exp_strategy[ch] = EXP_D25;
1134
                return 0;
1135
            }
1136
        }
1137
    }
1138
    for (ch = 0; ch < s->fbw_channels; ch++) {
1139
        for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1140
            if (s->blocks[blk].exp_strategy[ch] == EXP_D25) {
1141
                s->blocks[blk].exp_strategy[ch] = EXP_D45;
1142
                return 0;
1143
            }
1144
        }
1145
    }
1146
    for (ch = 0; ch < s->fbw_channels; ch++) {
1147
        /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
1148
           the block number > 0 */
1149
        for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
1150
            if (s->blocks[blk].exp_strategy[ch] > EXP_REUSE) {
1151
                s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
1152
                return 0;
1153
            }
1154
        }
1155
    }
1156
    return -1;
1157
}
1158

    
1159

    
1160
/**
1161
 * Reduce the bandwidth to reduce the number of bits used for a given SNR offset.
1162
 * This is a second fallback for when bit allocation still fails after exponents
1163
 * have been downgraded.
1164
 * @return non-zero if bandwidth reduction was unsuccessful
1165
 */
1166
static int reduce_bandwidth(AC3EncodeContext *s, int min_bw_code)
1167
{
1168
    int ch;
1169

    
1170
    if (s->bandwidth_code[0] > min_bw_code) {
1171
        for (ch = 0; ch < s->fbw_channels; ch++) {
1172
            s->bandwidth_code[ch]--;
1173
            s->nb_coefs[ch] = s->bandwidth_code[ch] * 3 + 73;
1174
        }
1175
        return 0;
1176
    }
1177
    return -1;
1178
}
1179

    
1180

    
1181
/**
1182
 * Perform bit allocation search.
1183
 * Finds the SNR offset value that maximizes quality and fits in the specified
1184
 * frame size.  Output is the SNR offset and a set of bit allocation pointers
1185
 * used to quantize the mantissas.
1186
 */
1187
static int compute_bit_allocation(AC3EncodeContext *s)
1188
{
1189
    int ret;
1190

    
1191
    count_frame_bits(s);
1192

    
1193
    bit_alloc_masking(s);
1194

    
1195
    ret = cbr_bit_allocation(s);
1196
    while (ret) {
1197
        /* fallback 1: downgrade exponents */
1198
        if (!downgrade_exponents(s)) {
1199
            extract_exponents(s);
1200
            encode_exponents(s);
1201
            group_exponents(s);
1202
            ret = compute_bit_allocation(s);
1203
            continue;
1204
        }
1205

    
1206
        /* fallback 2: reduce bandwidth */
1207
        /* only do this if the user has not specified a specific cutoff
1208
           frequency */
1209
        if (!s->cutoff && !reduce_bandwidth(s, 0)) {
1210
            process_exponents(s);
1211
            ret = compute_bit_allocation(s);
1212
            continue;
1213
        }
1214

    
1215
        /* fallbacks were not enough... */
1216
        break;
1217
    }
1218

    
1219
    return ret;
1220
}
1221

    
1222

    
1223
/**
1224
 * Symmetric quantization on 'levels' levels.
1225
 */
1226
static inline int sym_quant(int c, int e, int levels)
1227
{
1228
    int v;
1229

    
1230
    if (c >= 0) {
1231
        v = (levels * (c << e)) >> 24;
1232
        v = (v + 1) >> 1;
1233
        v = (levels >> 1) + v;
1234
    } else {
1235
        v = (levels * ((-c) << e)) >> 24;
1236
        v = (v + 1) >> 1;
1237
        v = (levels >> 1) - v;
1238
    }
1239
    assert(v >= 0 && v < levels);
1240
    return v;
1241
}
1242

    
1243

    
1244
/**
1245
 * Asymmetric quantization on 2^qbits levels.
1246
 */
1247
static inline int asym_quant(int c, int e, int qbits)
1248
{
1249
    int lshift, m, v;
1250

    
1251
    lshift = e + qbits - 24;
1252
    if (lshift >= 0)
1253
        v = c << lshift;
1254
    else
1255
        v = c >> (-lshift);
1256
    /* rounding */
1257
    v = (v + 1) >> 1;
1258
    m = (1 << (qbits-1));
1259
    if (v >= m)
1260
        v = m - 1;
1261
    assert(v >= -m);
1262
    return v & ((1 << qbits)-1);
1263
}
1264

    
1265

    
1266
/**
1267
 * Quantize a set of mantissas for a single channel in a single block.
1268
 */
1269
static void quantize_mantissas_blk_ch(AC3EncodeContext *s,
1270
                                      int32_t *mdct_coef, int8_t exp_shift,
1271
                                      uint8_t *exp, uint8_t *bap,
1272
                                      uint16_t *qmant, int n)
1273
{
1274
    int i;
1275

    
1276
    for (i = 0; i < n; i++) {
1277
        int v;
1278
        int c = mdct_coef[i];
1279
        int e = exp[i] - exp_shift;
1280
        int b = bap[i];
1281
        switch (b) {
1282
        case 0:
1283
            v = 0;
1284
            break;
1285
        case 1:
1286
            v = sym_quant(c, e, 3);
1287
            switch (s->mant1_cnt) {
1288
            case 0:
1289
                s->qmant1_ptr = &qmant[i];
1290
                v = 9 * v;
1291
                s->mant1_cnt = 1;
1292
                break;
1293
            case 1:
1294
                *s->qmant1_ptr += 3 * v;
1295
                s->mant1_cnt = 2;
1296
                v = 128;
1297
                break;
1298
            default:
1299
                *s->qmant1_ptr += v;
1300
                s->mant1_cnt = 0;
1301
                v = 128;
1302
                break;
1303
            }
1304
            break;
1305
        case 2:
1306
            v = sym_quant(c, e, 5);
1307
            switch (s->mant2_cnt) {
1308
            case 0:
1309
                s->qmant2_ptr = &qmant[i];
1310
                v = 25 * v;
1311
                s->mant2_cnt = 1;
1312
                break;
1313
            case 1:
1314
                *s->qmant2_ptr += 5 * v;
1315
                s->mant2_cnt = 2;
1316
                v = 128;
1317
                break;
1318
            default:
1319
                *s->qmant2_ptr += v;
1320
                s->mant2_cnt = 0;
1321
                v = 128;
1322
                break;
1323
            }
1324
            break;
1325
        case 3:
1326
            v = sym_quant(c, e, 7);
1327
            break;
1328
        case 4:
1329
            v = sym_quant(c, e, 11);
1330
            switch (s->mant4_cnt) {
1331
            case 0:
1332
                s->qmant4_ptr = &qmant[i];
1333
                v = 11 * v;
1334
                s->mant4_cnt = 1;
1335
                break;
1336
            default:
1337
                *s->qmant4_ptr += v;
1338
                s->mant4_cnt = 0;
1339
                v = 128;
1340
                break;
1341
            }
1342
            break;
1343
        case 5:
1344
            v = sym_quant(c, e, 15);
1345
            break;
1346
        case 14:
1347
            v = asym_quant(c, e, 14);
1348
            break;
1349
        case 15:
1350
            v = asym_quant(c, e, 16);
1351
            break;
1352
        default:
1353
            v = asym_quant(c, e, b - 1);
1354
            break;
1355
        }
1356
        qmant[i] = v;
1357
    }
1358
}
1359

    
1360

    
1361
/**
1362
 * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1363
 */
1364
static void quantize_mantissas(AC3EncodeContext *s)
1365
{
1366
    int blk, ch;
1367

    
1368

    
1369
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1370
        AC3Block *block = &s->blocks[blk];
1371
        s->mant1_cnt  = s->mant2_cnt  = s->mant4_cnt  = 0;
1372
        s->qmant1_ptr = s->qmant2_ptr = s->qmant4_ptr = NULL;
1373

    
1374
        for (ch = 0; ch < s->channels; ch++) {
1375
            quantize_mantissas_blk_ch(s, block->mdct_coef[ch], block->exp_shift[ch],
1376
                                      block->exp[ch], block->bap[ch],
1377
                                      block->qmant[ch], s->nb_coefs[ch]);
1378
        }
1379
    }
1380
}
1381

    
1382

    
1383
/**
1384
 * Write the AC-3 frame header to the output bitstream.
1385
 */
1386
static void output_frame_header(AC3EncodeContext *s)
1387
{
1388
    put_bits(&s->pb, 16, 0x0b77);   /* frame header */
1389
    put_bits(&s->pb, 16, 0);        /* crc1: will be filled later */
1390
    put_bits(&s->pb, 2,  s->bit_alloc.sr_code);
1391
    put_bits(&s->pb, 6,  s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
1392
    put_bits(&s->pb, 5,  s->bitstream_id);
1393
    put_bits(&s->pb, 3,  s->bitstream_mode);
1394
    put_bits(&s->pb, 3,  s->channel_mode);
1395
    if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
1396
        put_bits(&s->pb, 2, 1);     /* XXX -4.5 dB */
1397
    if (s->channel_mode & 0x04)
1398
        put_bits(&s->pb, 2, 1);     /* XXX -6 dB */
1399
    if (s->channel_mode == AC3_CHMODE_STEREO)
1400
        put_bits(&s->pb, 2, 0);     /* surround not indicated */
1401
    put_bits(&s->pb, 1, s->lfe_on); /* LFE */
1402
    put_bits(&s->pb, 5, 31);        /* dialog norm: -31 db */
1403
    put_bits(&s->pb, 1, 0);         /* no compression control word */
1404
    put_bits(&s->pb, 1, 0);         /* no lang code */
1405
    put_bits(&s->pb, 1, 0);         /* no audio production info */
1406
    put_bits(&s->pb, 1, 0);         /* no copyright */
1407
    put_bits(&s->pb, 1, 1);         /* original bitstream */
1408
    put_bits(&s->pb, 1, 0);         /* no time code 1 */
1409
    put_bits(&s->pb, 1, 0);         /* no time code 2 */
1410
    put_bits(&s->pb, 1, 0);         /* no additional bit stream info */
1411
}
1412

    
1413

    
1414
/**
1415
 * Write one audio block to the output bitstream.
1416
 */
1417
static void output_audio_block(AC3EncodeContext *s,
1418
                               int block_num)
1419
{
1420
    int ch, i, baie, rbnd;
1421
    AC3Block *block = &s->blocks[block_num];
1422

    
1423
    /* block switching */
1424
    for (ch = 0; ch < s->fbw_channels; ch++)
1425
        put_bits(&s->pb, 1, 0);
1426

    
1427
    /* dither flags */
1428
    for (ch = 0; ch < s->fbw_channels; ch++)
1429
        put_bits(&s->pb, 1, 1);
1430

    
1431
    /* dynamic range codes */
1432
    put_bits(&s->pb, 1, 0);
1433

    
1434
    /* channel coupling */
1435
    if (!block_num) {
1436
        put_bits(&s->pb, 1, 1); /* coupling strategy present */
1437
        put_bits(&s->pb, 1, 0); /* no coupling strategy */
1438
    } else {
1439
        put_bits(&s->pb, 1, 0); /* no new coupling strategy */
1440
    }
1441

    
1442
    /* stereo rematrixing */
1443
    if (s->channel_mode == AC3_CHMODE_STEREO) {
1444
        if (!block_num) {
1445
            /* first block must define rematrixing (rematstr) */
1446
            put_bits(&s->pb, 1, 1);
1447

    
1448
            /* dummy rematrixing rematflg(1:4)=0 */
1449
            for (rbnd = 0; rbnd < 4; rbnd++)
1450
                put_bits(&s->pb, 1, 0);
1451
        } else {
1452
            /* no matrixing (but should be used in the future) */
1453
            put_bits(&s->pb, 1, 0);
1454
        }
1455
    }
1456

    
1457
    /* exponent strategy */
1458
    for (ch = 0; ch < s->fbw_channels; ch++)
1459
        put_bits(&s->pb, 2, block->exp_strategy[ch]);
1460
    if (s->lfe_on)
1461
        put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]);
1462

    
1463
    /* bandwidth */
1464
    for (ch = 0; ch < s->fbw_channels; ch++) {
1465
        if (block->exp_strategy[ch] != EXP_REUSE)
1466
            put_bits(&s->pb, 6, s->bandwidth_code[ch]);
1467
    }
1468

    
1469
    /* exponents */
1470
    for (ch = 0; ch < s->channels; ch++) {
1471
        int nb_groups;
1472

    
1473
        if (block->exp_strategy[ch] == EXP_REUSE)
1474
            continue;
1475

    
1476
        /* DC exponent */
1477
        put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
1478

    
1479
        /* exponent groups */
1480
        nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
1481
        for (i = 1; i <= nb_groups; i++)
1482
            put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
1483

    
1484
        /* gain range info */
1485
        if (ch != s->lfe_channel)
1486
            put_bits(&s->pb, 2, 0);
1487
    }
1488

    
1489
    /* bit allocation info */
1490
    baie = (block_num == 0);
1491
    put_bits(&s->pb, 1, baie);
1492
    if (baie) {
1493
        put_bits(&s->pb, 2, s->slow_decay_code);
1494
        put_bits(&s->pb, 2, s->fast_decay_code);
1495
        put_bits(&s->pb, 2, s->slow_gain_code);
1496
        put_bits(&s->pb, 2, s->db_per_bit_code);
1497
        put_bits(&s->pb, 3, s->floor_code);
1498
    }
1499

    
1500
    /* snr offset */
1501
    put_bits(&s->pb, 1, baie);
1502
    if (baie) {
1503
        put_bits(&s->pb, 6, s->coarse_snr_offset);
1504
        for (ch = 0; ch < s->channels; ch++) {
1505
            put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
1506
            put_bits(&s->pb, 3, s->fast_gain_code[ch]);
1507
        }
1508
    }
1509

    
1510
    put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1511
    put_bits(&s->pb, 1, 0); /* no data to skip */
1512

    
1513
    /* mantissas */
1514
    for (ch = 0; ch < s->channels; ch++) {
1515
        int b, q;
1516
        for (i = 0; i < s->nb_coefs[ch]; i++) {
1517
            q = block->qmant[ch][i];
1518
            b = block->bap[ch][i];
1519
            switch (b) {
1520
            case 0:                                         break;
1521
            case 1: if (q != 128) put_bits(&s->pb,   5, q); break;
1522
            case 2: if (q != 128) put_bits(&s->pb,   7, q); break;
1523
            case 3:               put_bits(&s->pb,   3, q); break;
1524
            case 4: if (q != 128) put_bits(&s->pb,   7, q); break;
1525
            case 14:              put_bits(&s->pb,  14, q); break;
1526
            case 15:              put_bits(&s->pb,  16, q); break;
1527
            default:              put_bits(&s->pb, b-1, q); break;
1528
            }
1529
        }
1530
    }
1531
}
1532

    
1533

    
1534
/** CRC-16 Polynomial */
1535
#define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1536

    
1537

    
1538
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1539
{
1540
    unsigned int c;
1541

    
1542
    c = 0;
1543
    while (a) {
1544
        if (a & 1)
1545
            c ^= b;
1546
        a = a >> 1;
1547
        b = b << 1;
1548
        if (b & (1 << 16))
1549
            b ^= poly;
1550
    }
1551
    return c;
1552
}
1553

    
1554

    
1555
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
1556
{
1557
    unsigned int r;
1558
    r = 1;
1559
    while (n) {
1560
        if (n & 1)
1561
            r = mul_poly(r, a, poly);
1562
        a = mul_poly(a, a, poly);
1563
        n >>= 1;
1564
    }
1565
    return r;
1566
}
1567

    
1568

    
1569
/**
1570
 * Fill the end of the frame with 0's and compute the two CRCs.
1571
 */
1572
static void output_frame_end(AC3EncodeContext *s)
1573
{
1574
    const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
1575
    int frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1576
    uint8_t *frame;
1577

    
1578
    frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
1579

    
1580
    /* pad the remainder of the frame with zeros */
1581
    flush_put_bits(&s->pb);
1582
    frame = s->pb.buf;
1583
    pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
1584
    assert(pad_bytes >= 0);
1585
    if (pad_bytes > 0)
1586
        memset(put_bits_ptr(&s->pb), 0, pad_bytes);
1587

    
1588
    /* compute crc1 */
1589
    /* this is not so easy because it is at the beginning of the data... */
1590
    crc1 = av_bswap16(av_crc(crc_ctx, 0,
1591
                             frame + 4, frame_size_58 - 4));
1592
    crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
1593
    crc1    = mul_poly(crc_inv, crc1, CRC16_POLY);
1594
    AV_WB16(frame + 2, crc1);
1595

    
1596
    /* compute crc2 */
1597
    crc2 = av_bswap16(av_crc(crc_ctx, 0,
1598
                             frame + frame_size_58,
1599
                             s->frame_size - frame_size_58 - 2));
1600
    AV_WB16(frame + s->frame_size - 2, crc2);
1601
}
1602

    
1603

    
1604
/**
1605
 * Write the frame to the output bitstream.
1606
 */
1607
static void output_frame(AC3EncodeContext *s,
1608
                         unsigned char *frame)
1609
{
1610
    int blk;
1611

    
1612
    init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
1613

    
1614
    output_frame_header(s);
1615

    
1616
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1617
        output_audio_block(s, blk);
1618

    
1619
    output_frame_end(s);
1620
}
1621

    
1622

    
1623
/**
1624
 * Encode a single AC-3 frame.
1625
 */
1626
static int ac3_encode_frame(AVCodecContext *avctx,
1627
                            unsigned char *frame, int buf_size, void *data)
1628
{
1629
    AC3EncodeContext *s = avctx->priv_data;
1630
    const int16_t *samples = data;
1631
    int ret;
1632

    
1633
    if (s->bit_alloc.sr_code == 1)
1634
        adjust_frame_size(s);
1635

    
1636
    deinterleave_input_samples(s, samples);
1637

    
1638
    apply_mdct(s);
1639

    
1640
    process_exponents(s);
1641

    
1642
    ret = compute_bit_allocation(s);
1643
    if (ret) {
1644
        av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
1645
        return ret;
1646
    }
1647

    
1648
    quantize_mantissas(s);
1649

    
1650
    output_frame(s, frame);
1651

    
1652
    return s->frame_size;
1653
}
1654

    
1655

    
1656
/**
1657
 * Finalize encoding and free any memory allocated by the encoder.
1658
 */
1659
static av_cold int ac3_encode_close(AVCodecContext *avctx)
1660
{
1661
    int blk, ch;
1662
    AC3EncodeContext *s = avctx->priv_data;
1663

    
1664
    for (ch = 0; ch < s->channels; ch++)
1665
        av_freep(&s->planar_samples[ch]);
1666
    av_freep(&s->planar_samples);
1667
    av_freep(&s->bap_buffer);
1668
    av_freep(&s->bap1_buffer);
1669
    av_freep(&s->mdct_coef_buffer);
1670
    av_freep(&s->exp_buffer);
1671
    av_freep(&s->grouped_exp_buffer);
1672
    av_freep(&s->psd_buffer);
1673
    av_freep(&s->band_psd_buffer);
1674
    av_freep(&s->mask_buffer);
1675
    av_freep(&s->qmant_buffer);
1676
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1677
        AC3Block *block = &s->blocks[blk];
1678
        av_freep(&block->bap);
1679
        av_freep(&block->mdct_coef);
1680
        av_freep(&block->exp);
1681
        av_freep(&block->grouped_exp);
1682
        av_freep(&block->psd);
1683
        av_freep(&block->band_psd);
1684
        av_freep(&block->mask);
1685
        av_freep(&block->qmant);
1686
    }
1687

    
1688
    mdct_end(&s->mdct);
1689

    
1690
    av_freep(&avctx->coded_frame);
1691
    return 0;
1692
}
1693

    
1694

    
1695
/**
1696
 * Set channel information during initialization.
1697
 */
1698
static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
1699
                                    int64_t *channel_layout)
1700
{
1701
    int ch_layout;
1702

    
1703
    if (channels < 1 || channels > AC3_MAX_CHANNELS)
1704
        return AVERROR(EINVAL);
1705
    if ((uint64_t)*channel_layout > 0x7FF)
1706
        return AVERROR(EINVAL);
1707
    ch_layout = *channel_layout;
1708
    if (!ch_layout)
1709
        ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
1710
    if (av_get_channel_layout_nb_channels(ch_layout) != channels)
1711
        return AVERROR(EINVAL);
1712

    
1713
    s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
1714
    s->channels     = channels;
1715
    s->fbw_channels = channels - s->lfe_on;
1716
    s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
1717
    if (s->lfe_on)
1718
        ch_layout -= AV_CH_LOW_FREQUENCY;
1719

    
1720
    switch (ch_layout) {
1721
    case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;
1722
    case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;
1723
    case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;
1724
    case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;
1725
    case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;
1726
    case AV_CH_LAYOUT_QUAD:
1727
    case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;
1728
    case AV_CH_LAYOUT_5POINT0:
1729
    case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;
1730
    default:
1731
        return AVERROR(EINVAL);
1732
    }
1733

    
1734
    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
1735
    *channel_layout = ch_layout;
1736
    if (s->lfe_on)
1737
        *channel_layout |= AV_CH_LOW_FREQUENCY;
1738

    
1739
    return 0;
1740
}
1741

    
1742

    
1743
static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
1744
{
1745
    int i, ret;
1746

    
1747
    /* validate channel layout */
1748
    if (!avctx->channel_layout) {
1749
        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
1750
                                      "encoder will guess the layout, but it "
1751
                                      "might be incorrect.\n");
1752
    }
1753
    ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
1754
    if (ret) {
1755
        av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
1756
        return ret;
1757
    }
1758

    
1759
    /* validate sample rate */
1760
    for (i = 0; i < 9; i++) {
1761
        if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
1762
            break;
1763
    }
1764
    if (i == 9) {
1765
        av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
1766
        return AVERROR(EINVAL);
1767
    }
1768
    s->sample_rate        = avctx->sample_rate;
1769
    s->bit_alloc.sr_shift = i % 3;
1770
    s->bit_alloc.sr_code  = i / 3;
1771

    
1772
    /* validate bit rate */
1773
    for (i = 0; i < 19; i++) {
1774
        if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
1775
            break;
1776
    }
1777
    if (i == 19) {
1778
        av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
1779
        return AVERROR(EINVAL);
1780
    }
1781
    s->bit_rate        = avctx->bit_rate;
1782
    s->frame_size_code = i << 1;
1783

    
1784
    /* validate cutoff */
1785
    if (avctx->cutoff < 0) {
1786
        av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
1787
        return AVERROR(EINVAL);
1788
    }
1789
    s->cutoff = avctx->cutoff;
1790
    if (s->cutoff > (s->sample_rate >> 1))
1791
        s->cutoff = s->sample_rate >> 1;
1792

    
1793
    return 0;
1794
}
1795

    
1796

    
1797
/**
1798
 * Set bandwidth for all channels.
1799
 * The user can optionally supply a cutoff frequency. Otherwise an appropriate
1800
 * default value will be used.
1801
 */
1802
static av_cold void set_bandwidth(AC3EncodeContext *s)
1803
{
1804
    int ch, bw_code;
1805

    
1806
    if (s->cutoff) {
1807
        /* calculate bandwidth based on user-specified cutoff frequency */
1808
        int fbw_coeffs;
1809
        fbw_coeffs     = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
1810
        bw_code        = av_clip((fbw_coeffs - 73) / 3, 0, 60);
1811
    } else {
1812
        /* use default bandwidth setting */
1813
        /* XXX: should compute the bandwidth according to the frame
1814
           size, so that we avoid annoying high frequency artifacts */
1815
        bw_code = 50;
1816
    }
1817

    
1818
    /* set number of coefficients for each channel */
1819
    for (ch = 0; ch < s->fbw_channels; ch++) {
1820
        s->bandwidth_code[ch] = bw_code;
1821
        s->nb_coefs[ch]       = bw_code * 3 + 73;
1822
    }
1823
    if (s->lfe_on)
1824
        s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
1825
}
1826

    
1827

    
1828
static av_cold int allocate_buffers(AVCodecContext *avctx)
1829
{
1830
    int blk, ch;
1831
    AC3EncodeContext *s = avctx->priv_data;
1832

    
1833
    FF_ALLOC_OR_GOTO(avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
1834
                     alloc_fail);
1835
    for (ch = 0; ch < s->channels; ch++) {
1836
        FF_ALLOCZ_OR_GOTO(avctx, s->planar_samples[ch],
1837
                          (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
1838
                          alloc_fail);
1839
    }
1840
    FF_ALLOC_OR_GOTO(avctx, s->bap_buffer,  AC3_MAX_BLOCKS * s->channels *
1841
                     AC3_MAX_COEFS * sizeof(*s->bap_buffer),  alloc_fail);
1842
    FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
1843
                     AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
1844
    FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
1845
                     AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
1846
    FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
1847
                     AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
1848
    FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * s->channels *
1849
                     128 * sizeof(*s->grouped_exp_buffer), alloc_fail);
1850
    FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
1851
                     AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
1852
    FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * s->channels *
1853
                     64 * sizeof(*s->band_psd_buffer), alloc_fail);
1854
    FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
1855
                     64 * sizeof(*s->mask_buffer), alloc_fail);
1856
    FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * s->channels *
1857
                     AC3_MAX_COEFS * sizeof(*s->qmant_buffer), alloc_fail);
1858
    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1859
        AC3Block *block = &s->blocks[blk];
1860
        FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
1861
                         alloc_fail);
1862
        FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
1863
                          alloc_fail);
1864
        FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
1865
                          alloc_fail);
1866
        FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, s->channels * sizeof(*block->grouped_exp),
1867
                          alloc_fail);
1868
        FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
1869
                          alloc_fail);
1870
        FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, s->channels * sizeof(*block->band_psd),
1871
                          alloc_fail);
1872
        FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
1873
                          alloc_fail);
1874
        FF_ALLOCZ_OR_GOTO(avctx, block->qmant, s->channels * sizeof(*block->qmant),
1875
                          alloc_fail);
1876

    
1877
        for (ch = 0; ch < s->channels; ch++) {
1878
            block->bap[ch]         = &s->bap_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1879
            block->mdct_coef[ch]   = &s->mdct_coef_buffer  [AC3_MAX_COEFS * (blk * s->channels + ch)];
1880
            block->exp[ch]         = &s->exp_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1881
            block->grouped_exp[ch] = &s->grouped_exp_buffer[128           * (blk * s->channels + ch)];
1882
            block->psd[ch]         = &s->psd_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1883
            block->band_psd[ch]    = &s->band_psd_buffer   [64            * (blk * s->channels + ch)];
1884
            block->mask[ch]        = &s->mask_buffer       [64            * (blk * s->channels + ch)];
1885
            block->qmant[ch]       = &s->qmant_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
1886
        }
1887
    }
1888

    
1889
    return 0;
1890
alloc_fail:
1891
    return AVERROR(ENOMEM);
1892
}
1893

    
1894

    
1895
/**
1896
 * Initialize the encoder.
1897
 */
1898
static av_cold int ac3_encode_init(AVCodecContext *avctx)
1899
{
1900
    AC3EncodeContext *s = avctx->priv_data;
1901
    int ret, frame_size_58;
1902

    
1903
    avctx->frame_size = AC3_FRAME_SIZE;
1904

    
1905
    ac3_common_init();
1906

    
1907
    ret = validate_options(avctx, s);
1908
    if (ret)
1909
        return ret;
1910

    
1911
    s->bitstream_id   = 8 + s->bit_alloc.sr_shift;
1912
    s->bitstream_mode = 0; /* complete main audio service */
1913

    
1914
    s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
1915
    s->bits_written    = 0;
1916
    s->samples_written = 0;
1917
    s->frame_size      = s->frame_size_min;
1918

    
1919
    /* calculate crc_inv for both possible frame sizes */
1920
    frame_size_58 = (( s->frame_size    >> 2) + ( s->frame_size    >> 4)) << 1;
1921
    s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1922
    if (s->bit_alloc.sr_code == 1) {
1923
        frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
1924
        s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1925
    }
1926

    
1927
    set_bandwidth(s);
1928

    
1929
    exponent_init(s);
1930

    
1931
    bit_alloc_init(s);
1932

    
1933
    s->mdct.avctx = avctx;
1934
    ret = mdct_init(&s->mdct, 9);
1935
    if (ret)
1936
        goto init_fail;
1937

    
1938
    ret = allocate_buffers(avctx);
1939
    if (ret)
1940
        goto init_fail;
1941

    
1942
    avctx->coded_frame= avcodec_alloc_frame();
1943

    
1944
    dsputil_init(&s->dsp, avctx);
1945

    
1946
    return 0;
1947
init_fail:
1948
    ac3_encode_close(avctx);
1949
    return ret;
1950
}
1951

    
1952

    
1953
#ifdef TEST
1954
/*************************************************************************/
1955
/* TEST */
1956

    
1957
#include "libavutil/lfg.h"
1958

    
1959
#define MDCT_NBITS 9
1960
#define MDCT_SAMPLES (1 << MDCT_NBITS)
1961
#define FN (MDCT_SAMPLES/4)
1962

    
1963

    
1964
static void fft_test(AC3MDCTContext *mdct, AVLFG *lfg)
1965
{
1966
    IComplex in[FN], in1[FN];
1967
    int k, n, i;
1968
    float sum_re, sum_im, a;
1969

    
1970
    for (i = 0; i < FN; i++) {
1971
        in[i].re = av_lfg_get(lfg) % 65535 - 32767;
1972
        in[i].im = av_lfg_get(lfg) % 65535 - 32767;
1973
        in1[i]   = in[i];
1974
    }
1975
    fft(mdct, in, 7);
1976

    
1977
    /* do it by hand */
1978
    for (k = 0; k < FN; k++) {
1979
        sum_re = 0;
1980
        sum_im = 0;
1981
        for (n = 0; n < FN; n++) {
1982
            a = -2 * M_PI * (n * k) / FN;
1983
            sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1984
            sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1985
        }
1986
        av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
1987
               k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1988
    }
1989
}
1990

    
1991

    
1992
static void mdct_test(AC3MDCTContext *mdct, AVLFG *lfg)
1993
{
1994
    int16_t input[MDCT_SAMPLES];
1995
    int32_t output[AC3_MAX_COEFS];
1996
    float input1[MDCT_SAMPLES];
1997
    float output1[AC3_MAX_COEFS];
1998
    float s, a, err, e, emax;
1999
    int i, k, n;
2000

    
2001
    for (i = 0; i < MDCT_SAMPLES; i++) {
2002
        input[i]  = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
2003
        input1[i] = input[i];
2004
    }
2005

    
2006
    mdct512(mdct, output, input);
2007

    
2008
    /* do it by hand */
2009
    for (k = 0; k < AC3_MAX_COEFS; k++) {
2010
        s = 0;
2011
        for (n = 0; n < MDCT_SAMPLES; n++) {
2012
            a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
2013
            s += input1[n] * cos(a);
2014
        }
2015
        output1[k] = -2 * s / MDCT_SAMPLES;
2016
    }
2017

    
2018
    err  = 0;
2019
    emax = 0;
2020
    for (i = 0; i < AC3_MAX_COEFS; i++) {
2021
        av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
2022
        e = output[i] - output1[i];
2023
        if (e > emax)
2024
            emax = e;
2025
        err += e * e;
2026
    }
2027
    av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
2028
}
2029

    
2030

    
2031
int main(void)
2032
{
2033
    AVLFG lfg;
2034
    AC3MDCTContext mdct;
2035

    
2036
    mdct.avctx = NULL;
2037
    av_log_set_level(AV_LOG_DEBUG);
2038
    mdct_init(&mdct, 9);
2039

    
2040
    fft_test(&mdct, &lfg);
2041
    mdct_test(&mdct, &lfg);
2042

    
2043
    return 0;
2044
}
2045
#endif /* TEST */
2046

    
2047

    
2048
AVCodec ac3_encoder = {
2049
    "ac3",
2050
    AVMEDIA_TYPE_AUDIO,
2051
    CODEC_ID_AC3,
2052
    sizeof(AC3EncodeContext),
2053
    ac3_encode_init,
2054
    ac3_encode_frame,
2055
    ac3_encode_close,
2056
    NULL,
2057
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
2058
    .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
2059
    .channel_layouts = (const int64_t[]){
2060
        AV_CH_LAYOUT_MONO,
2061
        AV_CH_LAYOUT_STEREO,
2062
        AV_CH_LAYOUT_2_1,
2063
        AV_CH_LAYOUT_SURROUND,
2064
        AV_CH_LAYOUT_2_2,
2065
        AV_CH_LAYOUT_QUAD,
2066
        AV_CH_LAYOUT_4POINT0,
2067
        AV_CH_LAYOUT_5POINT0,
2068
        AV_CH_LAYOUT_5POINT0_BACK,
2069
       (AV_CH_LAYOUT_MONO     | AV_CH_LOW_FREQUENCY),
2070
       (AV_CH_LAYOUT_STEREO   | AV_CH_LOW_FREQUENCY),
2071
       (AV_CH_LAYOUT_2_1      | AV_CH_LOW_FREQUENCY),
2072
       (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
2073
       (AV_CH_LAYOUT_2_2      | AV_CH_LOW_FREQUENCY),
2074
       (AV_CH_LAYOUT_QUAD     | AV_CH_LOW_FREQUENCY),
2075
       (AV_CH_LAYOUT_4POINT0  | AV_CH_LOW_FREQUENCY),
2076
        AV_CH_LAYOUT_5POINT1,
2077
        AV_CH_LAYOUT_5POINT1_BACK,
2078
        0 },
2079
};