Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3enc.c @ 386268df

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

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

    
1157

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

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

    
1178

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

    
1189
    count_frame_bits(s);
1190

    
1191
    bit_alloc_masking(s);
1192

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

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

    
1213
        /* fallbacks were not enough... */
1214
        break;
1215
    }
1216

    
1217
    return ret;
1218
}
1219

    
1220

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

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

    
1241

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

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

    
1263

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

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

    
1358

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

    
1366

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

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

    
1380

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

    
1411

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

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

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

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

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

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

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

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

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

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

    
1471
        if (block->exp_strategy[ch] == EXP_REUSE)
1472
            continue;
1473

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

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

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

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

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

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

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

    
1531

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

    
1535

    
1536
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1537
{
1538
    unsigned int c;
1539

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

    
1552

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

    
1566

    
1567
/**
1568
 * Fill the end of the frame with 0's and compute the two CRCs.
1569
 */
1570
static void output_frame_end(AC3EncodeContext *s)
1571
{
1572
    int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv;
1573
    uint8_t *frame;
1574

    
1575
    frame_size    = s->frame_size;
1576
    frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1;
1577

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

    
1586
    /* compute crc1 */
1587
    /* this is not so easy because it is at the beginning of the data... */
1588
    crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1589
                             frame + 4, frame_size_58 - 4));
1590
    /* XXX: could precompute crc_inv */
1591
    crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1592
    crc1    = mul_poly(crc_inv, crc1, CRC16_POLY);
1593
    AV_WB16(frame + 2, crc1);
1594

    
1595
    /* compute crc2 */
1596
    crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1597
                             frame + frame_size_58,
1598
                             frame_size - frame_size_58 - 2));
1599
    AV_WB16(frame + frame_size - 2, crc2);
1600
}
1601

    
1602

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

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

    
1613
    output_frame_header(s);
1614

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

    
1618
    output_frame_end(s);
1619
}
1620

    
1621

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

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

    
1635
    deinterleave_input_samples(s, samples);
1636

    
1637
    apply_mdct(s);
1638

    
1639
    process_exponents(s);
1640

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

    
1647
    quantize_mantissas(s);
1648

    
1649
    output_frame(s, frame);
1650

    
1651
    return s->frame_size;
1652
}
1653

    
1654

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

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

    
1687
    mdct_end(&s->mdct);
1688

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

    
1693

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

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

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

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

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

    
1738
    return 0;
1739
}
1740

    
1741

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

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

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

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

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

    
1792
    return 0;
1793
}
1794

    
1795

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

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

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

    
1826

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

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

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

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

    
1893

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

    
1902
    avctx->frame_size = AC3_FRAME_SIZE;
1903

    
1904
    ac3_common_init();
1905

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

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

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

    
1918
    set_bandwidth(s);
1919

    
1920
    exponent_init(s);
1921

    
1922
    bit_alloc_init(s);
1923

    
1924
    s->mdct.avctx = avctx;
1925
    ret = mdct_init(&s->mdct, 9);
1926
    if (ret)
1927
        goto init_fail;
1928

    
1929
    ret = allocate_buffers(avctx);
1930
    if (ret)
1931
        goto init_fail;
1932

    
1933
    avctx->coded_frame= avcodec_alloc_frame();
1934

    
1935
    dsputil_init(&s->dsp, avctx);
1936

    
1937
    return 0;
1938
init_fail:
1939
    ac3_encode_close(avctx);
1940
    return ret;
1941
}
1942

    
1943

    
1944
#ifdef TEST
1945
/*************************************************************************/
1946
/* TEST */
1947

    
1948
#include "libavutil/lfg.h"
1949

    
1950
#define MDCT_NBITS 9
1951
#define MDCT_SAMPLES (1 << MDCT_NBITS)
1952
#define FN (MDCT_SAMPLES/4)
1953

    
1954

    
1955
static void fft_test(AC3MDCTContext *mdct, AVLFG *lfg)
1956
{
1957
    IComplex in[FN], in1[FN];
1958
    int k, n, i;
1959
    float sum_re, sum_im, a;
1960

    
1961
    for (i = 0; i < FN; i++) {
1962
        in[i].re = av_lfg_get(lfg) % 65535 - 32767;
1963
        in[i].im = av_lfg_get(lfg) % 65535 - 32767;
1964
        in1[i]   = in[i];
1965
    }
1966
    fft(mdct, in, 7);
1967

    
1968
    /* do it by hand */
1969
    for (k = 0; k < FN; k++) {
1970
        sum_re = 0;
1971
        sum_im = 0;
1972
        for (n = 0; n < FN; n++) {
1973
            a = -2 * M_PI * (n * k) / FN;
1974
            sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1975
            sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1976
        }
1977
        av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
1978
               k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1979
    }
1980
}
1981

    
1982

    
1983
static void mdct_test(AC3MDCTContext *mdct, AVLFG *lfg)
1984
{
1985
    int16_t input[MDCT_SAMPLES];
1986
    int32_t output[AC3_MAX_COEFS];
1987
    float input1[MDCT_SAMPLES];
1988
    float output1[AC3_MAX_COEFS];
1989
    float s, a, err, e, emax;
1990
    int i, k, n;
1991

    
1992
    for (i = 0; i < MDCT_SAMPLES; i++) {
1993
        input[i]  = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
1994
        input1[i] = input[i];
1995
    }
1996

    
1997
    mdct512(mdct, output, input);
1998

    
1999
    /* do it by hand */
2000
    for (k = 0; k < AC3_MAX_COEFS; k++) {
2001
        s = 0;
2002
        for (n = 0; n < MDCT_SAMPLES; n++) {
2003
            a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
2004
            s += input1[n] * cos(a);
2005
        }
2006
        output1[k] = -2 * s / MDCT_SAMPLES;
2007
    }
2008

    
2009
    err  = 0;
2010
    emax = 0;
2011
    for (i = 0; i < AC3_MAX_COEFS; i++) {
2012
        av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
2013
        e = output[i] - output1[i];
2014
        if (e > emax)
2015
            emax = e;
2016
        err += e * e;
2017
    }
2018
    av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
2019
}
2020

    
2021

    
2022
int main(void)
2023
{
2024
    AVLFG lfg;
2025
    AC3MDCTContext mdct;
2026

    
2027
    mdct.avctx = NULL;
2028
    av_log_set_level(AV_LOG_DEBUG);
2029
    mdct_init(&mdct, 9);
2030

    
2031
    fft_test(&mdct, &lfg);
2032
    mdct_test(&mdct, &lfg);
2033

    
2034
    return 0;
2035
}
2036
#endif /* TEST */
2037

    
2038

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