]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3enc.c
Move the list of supported channel layouts to a separate table.
[ffmpeg] / libavcodec / ac3enc.c
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     const int16_t *window;                  ///< MDCT window function
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 static uint8_t exponent_group_tab[3][256];
157
158
159 /**
160  * List of supported channel layouts.
161  */
162 static const int64_t ac3_channel_layouts[] = {
163      AV_CH_LAYOUT_MONO,
164      AV_CH_LAYOUT_STEREO,
165      AV_CH_LAYOUT_2_1,
166      AV_CH_LAYOUT_SURROUND,
167      AV_CH_LAYOUT_2_2,
168      AV_CH_LAYOUT_QUAD,
169      AV_CH_LAYOUT_4POINT0,
170      AV_CH_LAYOUT_5POINT0,
171      AV_CH_LAYOUT_5POINT0_BACK,
172     (AV_CH_LAYOUT_MONO     | AV_CH_LOW_FREQUENCY),
173     (AV_CH_LAYOUT_STEREO   | AV_CH_LOW_FREQUENCY),
174     (AV_CH_LAYOUT_2_1      | AV_CH_LOW_FREQUENCY),
175     (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY),
176     (AV_CH_LAYOUT_2_2      | AV_CH_LOW_FREQUENCY),
177     (AV_CH_LAYOUT_QUAD     | AV_CH_LOW_FREQUENCY),
178     (AV_CH_LAYOUT_4POINT0  | AV_CH_LOW_FREQUENCY),
179      AV_CH_LAYOUT_5POINT1,
180      AV_CH_LAYOUT_5POINT1_BACK,
181      0
182 };
183
184
185 /**
186  * Adjust the frame size to make the average bit rate match the target bit rate.
187  * This is only needed for 11025, 22050, and 44100 sample rates.
188  */
189 static void adjust_frame_size(AC3EncodeContext *s)
190 {
191     while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
192         s->bits_written    -= s->bit_rate;
193         s->samples_written -= s->sample_rate;
194     }
195     s->frame_size = s->frame_size_min +
196                     2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
197     s->bits_written    += s->frame_size * 8;
198     s->samples_written += AC3_FRAME_SIZE;
199 }
200
201
202 /**
203  * Deinterleave input samples.
204  * Channels are reordered from FFmpeg's default order to AC-3 order.
205  */
206 static void deinterleave_input_samples(AC3EncodeContext *s,
207                                        const int16_t *samples)
208 {
209     int ch, i;
210
211     /* deinterleave and remap input samples */
212     for (ch = 0; ch < s->channels; ch++) {
213         const int16_t *sptr;
214         int sinc;
215
216         /* copy last 256 samples of previous frame to the start of the current frame */
217         memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
218                AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
219
220         /* deinterleave */
221         sinc = s->channels;
222         sptr = samples + s->channel_map[ch];
223         for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
224             s->planar_samples[ch][i] = *sptr;
225             sptr += sinc;
226         }
227     }
228 }
229
230
231 /**
232  * Finalize MDCT and free allocated memory.
233  */
234 static av_cold void mdct_end(AC3MDCTContext *mdct)
235 {
236     mdct->nbits = 0;
237     av_freep(&mdct->costab);
238     av_freep(&mdct->sintab);
239     av_freep(&mdct->xcos1);
240     av_freep(&mdct->xsin1);
241     av_freep(&mdct->rot_tmp);
242     av_freep(&mdct->cplx_tmp);
243 }
244
245
246 /**
247  * Initialize FFT tables.
248  * @param ln log2(FFT size)
249  */
250 static av_cold int fft_init(AVCodecContext *avctx, AC3MDCTContext *mdct, int ln)
251 {
252     int i, n, n2;
253     float alpha;
254
255     n  = 1 << ln;
256     n2 = n >> 1;
257
258     FF_ALLOC_OR_GOTO(avctx, mdct->costab, n2 * sizeof(*mdct->costab), fft_alloc_fail);
259     FF_ALLOC_OR_GOTO(avctx, mdct->sintab, n2 * sizeof(*mdct->sintab), fft_alloc_fail);
260
261     for (i = 0; i < n2; i++) {
262         alpha     = 2.0 * M_PI * i / n;
263         mdct->costab[i] = FIX15(cos(alpha));
264         mdct->sintab[i] = FIX15(sin(alpha));
265     }
266
267     return 0;
268 fft_alloc_fail:
269     mdct_end(mdct);
270     return AVERROR(ENOMEM);
271 }
272
273
274 /**
275  * Initialize MDCT tables.
276  * @param nbits log2(MDCT size)
277  */
278 static av_cold int mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
279                              int nbits)
280 {
281     int i, n, n4, ret;
282
283     n  = 1 << nbits;
284     n4 = n >> 2;
285
286     mdct->nbits = nbits;
287
288     ret = fft_init(avctx, mdct, nbits - 2);
289     if (ret)
290         return ret;
291
292     mdct->window = ff_ac3_window;
293
294     FF_ALLOC_OR_GOTO(avctx, mdct->xcos1,    n4 * sizeof(*mdct->xcos1),    mdct_alloc_fail);
295     FF_ALLOC_OR_GOTO(avctx, mdct->xsin1,    n4 * sizeof(*mdct->xsin1),    mdct_alloc_fail);
296     FF_ALLOC_OR_GOTO(avctx, mdct->rot_tmp,  n  * sizeof(*mdct->rot_tmp),  mdct_alloc_fail);
297     FF_ALLOC_OR_GOTO(avctx, mdct->cplx_tmp, n4 * sizeof(*mdct->cplx_tmp), mdct_alloc_fail);
298
299     for (i = 0; i < n4; i++) {
300         float alpha = 2.0 * M_PI * (i + 1.0 / 8.0) / n;
301         mdct->xcos1[i] = FIX15(-cos(alpha));
302         mdct->xsin1[i] = FIX15(-sin(alpha));
303     }
304
305     return 0;
306 mdct_alloc_fail:
307     mdct_end(mdct);
308     return AVERROR(ENOMEM);
309 }
310
311
312 /** Butterfly op */
313 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1)  \
314 {                                                       \
315   int ax, ay, bx, by;                                   \
316   bx  = pre1;                                           \
317   by  = pim1;                                           \
318   ax  = qre1;                                           \
319   ay  = qim1;                                           \
320   pre = (bx + ax) >> 1;                                 \
321   pim = (by + ay) >> 1;                                 \
322   qre = (bx - ax) >> 1;                                 \
323   qim = (by - ay) >> 1;                                 \
324 }
325
326
327 /** Complex multiply */
328 #define CMUL(pre, pim, are, aim, bre, bim)              \
329 {                                                       \
330    pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;     \
331    pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;     \
332 }
333
334
335 /**
336  * Calculate a 2^n point complex FFT on 2^ln points.
337  * @param z  complex input/output samples
338  * @param ln log2(FFT size)
339  */
340 static void fft(AC3MDCTContext *mdct, IComplex *z, int ln)
341 {
342     int j, l, np, np2;
343     int nblocks, nloops;
344     register IComplex *p,*q;
345     int tmp_re, tmp_im;
346
347     np = 1 << ln;
348
349     /* reverse */
350     for (j = 0; j < np; j++) {
351         int k = av_reverse[j] >> (8 - ln);
352         if (k < j)
353             FFSWAP(IComplex, z[k], z[j]);
354     }
355
356     /* pass 0 */
357
358     p = &z[0];
359     j = np >> 1;
360     do {
361         BF(p[0].re, p[0].im, p[1].re, p[1].im,
362            p[0].re, p[0].im, p[1].re, p[1].im);
363         p += 2;
364     } while (--j);
365
366     /* pass 1 */
367
368     p = &z[0];
369     j = np >> 2;
370     do {
371         BF(p[0].re, p[0].im, p[2].re,  p[2].im,
372            p[0].re, p[0].im, p[2].re,  p[2].im);
373         BF(p[1].re, p[1].im, p[3].re,  p[3].im,
374            p[1].re, p[1].im, p[3].im, -p[3].re);
375         p+=4;
376     } while (--j);
377
378     /* pass 2 .. ln-1 */
379
380     nblocks = np >> 3;
381     nloops  =  1 << 2;
382     np2     = np >> 1;
383     do {
384         p = z;
385         q = z + nloops;
386         for (j = 0; j < nblocks; j++) {
387             BF(p->re, p->im, q->re, q->im,
388                p->re, p->im, q->re, q->im);
389             p++;
390             q++;
391             for(l = nblocks; l < np2; l += nblocks) {
392                 CMUL(tmp_re, tmp_im, mdct->costab[l], -mdct->sintab[l], q->re, q->im);
393                 BF(p->re, p->im, q->re,  q->im,
394                    p->re, p->im, tmp_re, tmp_im);
395                 p++;
396                 q++;
397             }
398             p += nloops;
399             q += nloops;
400         }
401         nblocks = nblocks >> 1;
402         nloops  = nloops  << 1;
403     } while (nblocks);
404 }
405
406
407 /**
408  * Calculate a 512-point MDCT
409  * @param out 256 output frequency coefficients
410  * @param in  512 windowed input audio samples
411  */
412 static void mdct512(AC3MDCTContext *mdct, int32_t *out, int16_t *in)
413 {
414     int i, re, im, n, n2, n4;
415     int16_t *rot = mdct->rot_tmp;
416     IComplex *x  = mdct->cplx_tmp;
417
418     n  = 1 << mdct->nbits;
419     n2 = n >> 1;
420     n4 = n >> 2;
421
422     /* shift to simplify computations */
423     for (i = 0; i <n4; i++)
424         rot[i] = -in[i + 3*n4];
425     memcpy(&rot[n4], &in[0], 3*n4*sizeof(*in));
426
427     /* pre rotation */
428     for (i = 0; i < n4; i++) {
429         re =  ((int)rot[   2*i] - (int)rot[ n-1-2*i]) >> 1;
430         im = -((int)rot[n2+2*i] - (int)rot[n2-1-2*i]) >> 1;
431         CMUL(x[i].re, x[i].im, re, im, -mdct->xcos1[i], mdct->xsin1[i]);
432     }
433
434     fft(mdct, x, mdct->nbits - 2);
435
436     /* post rotation */
437     for (i = 0; i < n4; i++) {
438         re = x[i].re;
439         im = x[i].im;
440         CMUL(out[n2-1-2*i], out[2*i], re, im, mdct->xsin1[i], mdct->xcos1[i]);
441     }
442 }
443
444
445 /**
446  * Apply KBD window to input samples prior to MDCT.
447  */
448 static void apply_window(int16_t *output, const int16_t *input,
449                          const int16_t *window, int n)
450 {
451     int i;
452     int n2 = n >> 1;
453
454     for (i = 0; i < n2; i++) {
455         output[i]     = MUL16(input[i],     window[i]) >> 15;
456         output[n-i-1] = MUL16(input[n-i-1], window[i]) >> 15;
457     }
458 }
459
460
461 /**
462  * Calculate the log2() of the maximum absolute value in an array.
463  * @param tab input array
464  * @param n   number of values in the array
465  * @return    log2(max(abs(tab[])))
466  */
467 static int log2_tab(int16_t *tab, int n)
468 {
469     int i, v;
470
471     v = 0;
472     for (i = 0; i < n; i++)
473         v |= abs(tab[i]);
474
475     return av_log2(v);
476 }
477
478
479 /**
480  * Left-shift each value in an array by a specified amount.
481  * @param tab    input array
482  * @param n      number of values in the array
483  * @param lshift left shift amount. a negative value means right shift.
484  */
485 static void lshift_tab(int16_t *tab, int n, int lshift)
486 {
487     int i;
488
489     if (lshift > 0) {
490         for (i = 0; i < n; i++)
491             tab[i] <<= lshift;
492     } else if (lshift < 0) {
493         lshift = -lshift;
494         for (i = 0; i < n; i++)
495             tab[i] >>= lshift;
496     }
497 }
498
499
500 /**
501  * Normalize the input samples to use the maximum available precision.
502  * This assumes signed 16-bit input samples. Exponents are reduced by 9 to
503  * match the 24-bit internal precision for MDCT coefficients.
504  *
505  * @return exponent shift
506  */
507 static int normalize_samples(AC3EncodeContext *s)
508 {
509     int v = 14 - log2_tab(s->windowed_samples, AC3_WINDOW_SIZE);
510     v = FFMAX(0, v);
511     lshift_tab(s->windowed_samples, AC3_WINDOW_SIZE, v);
512     return v - 9;
513 }
514
515
516 /**
517  * Apply the MDCT to input samples to generate frequency coefficients.
518  * This applies the KBD window and normalizes the input to reduce precision
519  * loss due to fixed-point calculations.
520  */
521 static void apply_mdct(AC3EncodeContext *s)
522 {
523     int blk, ch;
524
525     for (ch = 0; ch < s->channels; ch++) {
526         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
527             AC3Block *block = &s->blocks[blk];
528             const int16_t *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
529
530             apply_window(s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
531
532             block->exp_shift[ch] = normalize_samples(s);
533
534             mdct512(&s->mdct, block->mdct_coef[ch], s->windowed_samples);
535         }
536     }
537 }
538
539
540 /**
541  * Initialize exponent tables.
542  */
543 static av_cold void exponent_init(AC3EncodeContext *s)
544 {
545     int i;
546     for (i = 73; i < 256; i++) {
547         exponent_group_tab[0][i] = (i - 1) /  3;
548         exponent_group_tab[1][i] = (i + 2) /  6;
549         exponent_group_tab[2][i] = (i + 8) / 12;
550     }
551     /* LFE */
552     exponent_group_tab[0][7] = 2;
553 }
554
555
556 /**
557  * Extract exponents from the MDCT coefficients.
558  * This takes into account the normalization that was done to the input samples
559  * by adjusting the exponents by the exponent shift values.
560  */
561 static void extract_exponents(AC3EncodeContext *s)
562 {
563     int blk, ch, i;
564
565     for (ch = 0; ch < s->channels; ch++) {
566         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
567             AC3Block *block = &s->blocks[blk];
568             for (i = 0; i < AC3_MAX_COEFS; i++) {
569                 int e;
570                 int v = abs(block->mdct_coef[ch][i]);
571                 if (v == 0)
572                     e = 24;
573                 else {
574                     e = 23 - av_log2(v) + block->exp_shift[ch];
575                     if (e >= 24) {
576                         e = 24;
577                         block->mdct_coef[ch][i] = 0;
578                     }
579                 }
580                 block->exp[ch][i] = e;
581             }
582         }
583     }
584 }
585
586
587 /**
588  * Exponent Difference Threshold.
589  * New exponents are sent if their SAD exceed this number.
590  */
591 #define EXP_DIFF_THRESHOLD 1000
592
593
594 /**
595  * Calculate exponent strategies for all blocks in a single channel.
596  */
597 static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
598                                     uint8_t **exp)
599 {
600     int blk, blk1;
601     int exp_diff;
602
603     /* estimate if the exponent variation & decide if they should be
604        reused in the next frame */
605     exp_strategy[0] = EXP_NEW;
606     for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
607         exp_diff = s->dsp.sad[0](NULL, exp[blk], exp[blk-1], 16, 16);
608         if (exp_diff > EXP_DIFF_THRESHOLD)
609             exp_strategy[blk] = EXP_NEW;
610         else
611             exp_strategy[blk] = EXP_REUSE;
612     }
613     emms_c();
614
615     /* now select the encoding strategy type : if exponents are often
616        recoded, we use a coarse encoding */
617     blk = 0;
618     while (blk < AC3_MAX_BLOCKS) {
619         blk1 = blk + 1;
620         while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
621             blk1++;
622         switch (blk1 - blk) {
623         case 1:  exp_strategy[blk] = EXP_D45; break;
624         case 2:
625         case 3:  exp_strategy[blk] = EXP_D25; break;
626         default: exp_strategy[blk] = EXP_D15; break;
627         }
628         blk = blk1;
629     }
630 }
631
632
633 /**
634  * Calculate exponent strategies for all channels.
635  * Array arrangement is reversed to simplify the per-channel calculation.
636  */
637 static void compute_exp_strategy(AC3EncodeContext *s)
638 {
639     uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
640     uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
641     int ch, blk;
642
643     for (ch = 0; ch < s->fbw_channels; ch++) {
644         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
645             exp1[ch][blk]     = s->blocks[blk].exp[ch];
646             exp_str1[ch][blk] = s->blocks[blk].exp_strategy[ch];
647         }
648
649         compute_exp_strategy_ch(s, exp_str1[ch], exp1[ch]);
650
651         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
652             s->blocks[blk].exp_strategy[ch] = exp_str1[ch][blk];
653     }
654     if (s->lfe_on) {
655         ch = s->lfe_channel;
656         s->blocks[0].exp_strategy[ch] = EXP_D15;
657         for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
658             s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
659     }
660 }
661
662
663 /**
664  * Set each encoded exponent in a block to the minimum of itself and the
665  * exponent in the same frequency bin of a following block.
666  * exp[i] = min(exp[i], exp1[i]
667  */
668 static void exponent_min(uint8_t *exp, uint8_t *exp1, int n)
669 {
670     int i;
671     for (i = 0; i < n; i++) {
672         if (exp1[i] < exp[i])
673             exp[i] = exp1[i];
674     }
675 }
676
677
678 /**
679  * Update the exponents so that they are the ones the decoder will decode.
680  */
681 static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
682 {
683     int nb_groups, i, k;
684
685     nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
686
687     /* for each group, compute the minimum exponent */
688     switch(exp_strategy) {
689     case EXP_D25:
690         for (i = 1, k = 1; i <= nb_groups; i++) {
691             uint8_t exp_min = exp[k];
692             if (exp[k+1] < exp_min)
693                 exp_min = exp[k+1];
694             exp[i] = exp_min;
695             k += 2;
696         }
697         break;
698     case EXP_D45:
699         for (i = 1, k = 1; i <= nb_groups; i++) {
700             uint8_t exp_min = exp[k];
701             if (exp[k+1] < exp_min)
702                 exp_min = exp[k+1];
703             if (exp[k+2] < exp_min)
704                 exp_min = exp[k+2];
705             if (exp[k+3] < exp_min)
706                 exp_min = exp[k+3];
707             exp[i] = exp_min;
708             k += 4;
709         }
710         break;
711     }
712
713     /* constraint for DC exponent */
714     if (exp[0] > 15)
715         exp[0] = 15;
716
717     /* decrease the delta between each groups to within 2 so that they can be
718        differentially encoded */
719     for (i = 1; i <= nb_groups; i++)
720         exp[i] = FFMIN(exp[i], exp[i-1] + 2);
721     i--;
722     while (--i >= 0)
723         exp[i] = FFMIN(exp[i], exp[i+1] + 2);
724
725     /* now we have the exponent values the decoder will see */
726     switch (exp_strategy) {
727     case EXP_D25:
728         for (i = nb_groups, k = nb_groups * 2; i > 0; i--) {
729             uint8_t exp1 = exp[i];
730             exp[k--] = exp1;
731             exp[k--] = exp1;
732         }
733         break;
734     case EXP_D45:
735         for (i = nb_groups, k = nb_groups * 4; i > 0; i--) {
736             exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
737             k -= 4;
738         }
739         break;
740     }
741 }
742
743
744 /**
745  * Encode exponents from original extracted form to what the decoder will see.
746  * This copies and groups exponents based on exponent strategy and reduces
747  * deltas between adjacent exponent groups so that they can be differentially
748  * encoded.
749  */
750 static void encode_exponents(AC3EncodeContext *s)
751 {
752     int blk, blk1, blk2, ch;
753     AC3Block *block, *block1, *block2;
754
755     for (ch = 0; ch < s->channels; ch++) {
756         blk = 0;
757         block = &s->blocks[0];
758         while (blk < AC3_MAX_BLOCKS) {
759             blk1 = blk + 1;
760             block1 = block + 1;
761             /* for the EXP_REUSE case we select the min of the exponents */
762             while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) {
763                 exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]);
764                 blk1++;
765                 block1++;
766             }
767             encode_exponents_blk_ch(block->exp[ch], s->nb_coefs[ch],
768                                     block->exp_strategy[ch]);
769             /* copy encoded exponents for reuse case */
770             block2 = block + 1;
771             for (blk2 = blk+1; blk2 < blk1; blk2++, block2++) {
772                 memcpy(block2->exp[ch], block->exp[ch],
773                        s->nb_coefs[ch] * sizeof(uint8_t));
774             }
775             blk = blk1;
776             block = block1;
777         }
778     }
779 }
780
781
782 /**
783  * Group exponents.
784  * 3 delta-encoded exponents are in each 7-bit group. The number of groups
785  * varies depending on exponent strategy and bandwidth.
786  */
787 static void group_exponents(AC3EncodeContext *s)
788 {
789     int blk, ch, i;
790     int group_size, nb_groups, bit_count;
791     uint8_t *p;
792     int delta0, delta1, delta2;
793     int exp0, exp1;
794
795     bit_count = 0;
796     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
797         AC3Block *block = &s->blocks[blk];
798         for (ch = 0; ch < s->channels; ch++) {
799             if (block->exp_strategy[ch] == EXP_REUSE) {
800                 continue;
801             }
802             group_size = block->exp_strategy[ch] + (block->exp_strategy[ch] == EXP_D45);
803             nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
804             bit_count += 4 + (nb_groups * 7);
805             p = block->exp[ch];
806
807             /* DC exponent */
808             exp1 = *p++;
809             block->grouped_exp[ch][0] = exp1;
810
811             /* remaining exponents are delta encoded */
812             for (i = 1; i <= nb_groups; i++) {
813                 /* merge three delta in one code */
814                 exp0   = exp1;
815                 exp1   = p[0];
816                 p     += group_size;
817                 delta0 = exp1 - exp0 + 2;
818
819                 exp0   = exp1;
820                 exp1   = p[0];
821                 p     += group_size;
822                 delta1 = exp1 - exp0 + 2;
823
824                 exp0   = exp1;
825                 exp1   = p[0];
826                 p     += group_size;
827                 delta2 = exp1 - exp0 + 2;
828
829                 block->grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
830             }
831         }
832     }
833
834     s->exponent_bits = bit_count;
835 }
836
837
838 /**
839  * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
840  * Extract exponents from MDCT coefficients, calculate exponent strategies,
841  * and encode final exponents.
842  */
843 static void process_exponents(AC3EncodeContext *s)
844 {
845     extract_exponents(s);
846
847     compute_exp_strategy(s);
848
849     encode_exponents(s);
850
851     group_exponents(s);
852 }
853
854
855 /**
856  * Count frame bits that are based solely on fixed parameters.
857  * This only has to be run once when the encoder is initialized.
858  */
859 static void count_frame_bits_fixed(AC3EncodeContext *s)
860 {
861     static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
862     int blk;
863     int frame_bits;
864
865     /* assumptions:
866      *   no dynamic range codes
867      *   no channel coupling
868      *   no rematrixing
869      *   bit allocation parameters do not change between blocks
870      *   SNR offsets do not change between blocks
871      *   no delta bit allocation
872      *   no skipped data
873      *   no auxilliary data
874      */
875
876     /* header size */
877     frame_bits = 65;
878     frame_bits += frame_bits_inc[s->channel_mode];
879
880     /* audio blocks */
881     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
882         frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
883         if (s->channel_mode == AC3_CHMODE_STEREO) {
884             frame_bits++; /* rematstr */
885             if (!blk)
886                 frame_bits += 4;
887         }
888         frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
889         if (s->lfe_on)
890             frame_bits++; /* lfeexpstr */
891         frame_bits++; /* baie */
892         frame_bits++; /* snr */
893         frame_bits += 2; /* delta / skip */
894     }
895     frame_bits++; /* cplinu for block 0 */
896     /* bit alloc info */
897     /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
898     /* csnroffset[6] */
899     /* (fsnoffset[4] + fgaincod[4]) * c */
900     frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);
901
902     /* auxdatae, crcrsv */
903     frame_bits += 2;
904
905     /* CRC */
906     frame_bits += 16;
907
908     s->frame_bits_fixed = frame_bits;
909 }
910
911
912 /**
913  * Initialize bit allocation.
914  * Set default parameter codes and calculate parameter values.
915  */
916 static void bit_alloc_init(AC3EncodeContext *s)
917 {
918     int ch;
919
920     /* init default parameters */
921     s->slow_decay_code = 2;
922     s->fast_decay_code = 1;
923     s->slow_gain_code  = 1;
924     s->db_per_bit_code = 3;
925     s->floor_code      = 4;
926     for (ch = 0; ch < s->channels; ch++)
927         s->fast_gain_code[ch] = 4;
928
929     /* initial snr offset */
930     s->coarse_snr_offset = 40;
931
932     /* compute real values */
933     /* currently none of these values change during encoding, so we can just
934        set them once at initialization */
935     s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
936     s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
937     s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];
938     s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
939     s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
940
941     count_frame_bits_fixed(s);
942 }
943
944
945 /**
946  * Count the bits used to encode the frame, minus exponents and mantissas.
947  * Bits based on fixed parameters have already been counted, so now we just
948  * have to add the bits based on parameters that change during encoding.
949  */
950 static void count_frame_bits(AC3EncodeContext *s)
951 {
952     int blk, ch;
953     int frame_bits = 0;
954
955     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
956         uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
957         for (ch = 0; ch < s->fbw_channels; ch++) {
958             if (exp_strategy[ch] != EXP_REUSE)
959                 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
960         }
961     }
962     s->frame_bits = s->frame_bits_fixed + frame_bits;
963 }
964
965
966 /**
967  * Calculate the number of bits needed to encode a set of mantissas.
968  */
969 static int compute_mantissa_size(int mant_cnt[5], uint8_t *bap, int nb_coefs)
970 {
971     int bits, b, i;
972
973     bits = 0;
974     for (i = 0; i < nb_coefs; i++) {
975         b = bap[i];
976         if (b <= 4) {
977             // bap=1 to bap=4 will be counted in compute_mantissa_size_final
978             mant_cnt[b]++;
979         } else if (b <= 13) {
980             // bap=5 to bap=13 use (bap-1) bits
981             bits += b - 1;
982         } else {
983             // bap=14 uses 14 bits and bap=15 uses 16 bits
984             bits += (b == 14) ? 14 : 16;
985         }
986     }
987     return bits;
988 }
989
990
991 /**
992  * Finalize the mantissa bit count by adding in the grouped mantissas.
993  */
994 static int compute_mantissa_size_final(int mant_cnt[5])
995 {
996     // bap=1 : 3 mantissas in 5 bits
997     int bits = (mant_cnt[1] / 3) * 5;
998     // bap=2 : 3 mantissas in 7 bits
999     // bap=4 : 2 mantissas in 7 bits
1000     bits += ((mant_cnt[2] / 3) + (mant_cnt[4] >> 1)) * 7;
1001     // bap=3 : each mantissa is 3 bits
1002     bits += mant_cnt[3] * 3;
1003     return bits;
1004 }
1005
1006
1007 /**
1008  * Calculate masking curve based on the final exponents.
1009  * Also calculate the power spectral densities to use in future calculations.
1010  */
1011 static void bit_alloc_masking(AC3EncodeContext *s)
1012 {
1013     int blk, ch;
1014
1015     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1016         AC3Block *block = &s->blocks[blk];
1017         for (ch = 0; ch < s->channels; ch++) {
1018             /* We only need psd and mask for calculating bap.
1019                Since we currently do not calculate bap when exponent
1020                strategy is EXP_REUSE we do not need to calculate psd or mask. */
1021             if (block->exp_strategy[ch] != EXP_REUSE) {
1022                 ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
1023                                           s->nb_coefs[ch],
1024                                           block->psd[ch], block->band_psd[ch]);
1025                 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, block->band_psd[ch],
1026                                            0, s->nb_coefs[ch],
1027                                            ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
1028                                            ch == s->lfe_channel,
1029                                            DBA_NONE, 0, NULL, NULL, NULL,
1030                                            block->mask[ch]);
1031             }
1032         }
1033     }
1034 }
1035
1036
1037 /**
1038  * Ensure that bap for each block and channel point to the current bap_buffer.
1039  * They may have been switched during the bit allocation search.
1040  */
1041 static void reset_block_bap(AC3EncodeContext *s)
1042 {
1043     int blk, ch;
1044     if (s->blocks[0].bap[0] == s->bap_buffer)
1045         return;
1046     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1047         for (ch = 0; ch < s->channels; ch++) {
1048             s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
1049         }
1050     }
1051 }
1052
1053
1054 /**
1055  * Run the bit allocation with a given SNR offset.
1056  * This calculates the bit allocation pointers that will be used to determine
1057  * the quantization of each mantissa.
1058  * @return the number of bits needed for mantissas if the given SNR offset is
1059  *         is used.
1060  */
1061 static int bit_alloc(AC3EncodeContext *s, int snr_offset)
1062 {
1063     int blk, ch;
1064     int mantissa_bits;
1065     int mant_cnt[5];
1066
1067     snr_offset = (snr_offset - 240) << 2;
1068
1069     reset_block_bap(s);
1070     mantissa_bits = 0;
1071     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1072         AC3Block *block = &s->blocks[blk];
1073         // initialize grouped mantissa counts. these are set so that they are
1074         // padded to the next whole group size when bits are counted in
1075         // compute_mantissa_size_final
1076         mant_cnt[0] = mant_cnt[3] = 0;
1077         mant_cnt[1] = mant_cnt[2] = 2;
1078         mant_cnt[4] = 1;
1079         for (ch = 0; ch < s->channels; ch++) {
1080             /* Currently the only bit allocation parameters which vary across
1081                blocks within a frame are the exponent values.  We can take
1082                advantage of that by reusing the bit allocation pointers
1083                whenever we reuse exponents. */
1084             if (block->exp_strategy[ch] == EXP_REUSE) {
1085                 memcpy(block->bap[ch], s->blocks[blk-1].bap[ch], AC3_MAX_COEFS);
1086             } else {
1087                 ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
1088                                           s->nb_coefs[ch], snr_offset,
1089                                           s->bit_alloc.floor, ff_ac3_bap_tab,
1090                                           block->bap[ch]);
1091             }
1092             mantissa_bits += compute_mantissa_size(mant_cnt, block->bap[ch], s->nb_coefs[ch]);
1093         }
1094         mantissa_bits += compute_mantissa_size_final(mant_cnt);
1095     }
1096     return mantissa_bits;
1097 }
1098
1099
1100 /**
1101  * Constant bitrate bit allocation search.
1102  * Find the largest SNR offset that will allow data to fit in the frame.
1103  */
1104 static int cbr_bit_allocation(AC3EncodeContext *s)
1105 {
1106     int ch;
1107     int bits_left;
1108     int snr_offset, snr_incr;
1109
1110     bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);
1111
1112     snr_offset = s->coarse_snr_offset << 4;
1113
1114     while (snr_offset >= 0 &&
1115            bit_alloc(s, snr_offset) > bits_left) {
1116         snr_offset -= 64;
1117     }
1118     if (snr_offset < 0)
1119         return AVERROR(EINVAL);
1120
1121     FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1122     for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1123         while (snr_offset + 64 <= 1023 &&
1124                bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
1125             snr_offset += snr_incr;
1126             FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1127         }
1128     }
1129     FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
1130     reset_block_bap(s);
1131
1132     s->coarse_snr_offset = snr_offset >> 4;
1133     for (ch = 0; ch < s->channels; ch++)
1134         s->fine_snr_offset[ch] = snr_offset & 0xF;
1135
1136     return 0;
1137 }
1138
1139
1140 /**
1141  * Downgrade exponent strategies to reduce the bits used by the exponents.
1142  * This is a fallback for when bit allocation fails with the normal exponent
1143  * strategies.  Each time this function is run it only downgrades the
1144  * strategy in 1 channel of 1 block.
1145  * @return non-zero if downgrade was unsuccessful
1146  */
1147 static int downgrade_exponents(AC3EncodeContext *s)
1148 {
1149     int ch, blk;
1150
1151     for (ch = 0; ch < s->fbw_channels; ch++) {
1152         for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1153             if (s->blocks[blk].exp_strategy[ch] == EXP_D15) {
1154                 s->blocks[blk].exp_strategy[ch] = EXP_D25;
1155                 return 0;
1156             }
1157         }
1158     }
1159     for (ch = 0; ch < s->fbw_channels; ch++) {
1160         for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
1161             if (s->blocks[blk].exp_strategy[ch] == EXP_D25) {
1162                 s->blocks[blk].exp_strategy[ch] = EXP_D45;
1163                 return 0;
1164             }
1165         }
1166     }
1167     for (ch = 0; ch < s->fbw_channels; ch++) {
1168         /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
1169            the block number > 0 */
1170         for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
1171             if (s->blocks[blk].exp_strategy[ch] > EXP_REUSE) {
1172                 s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
1173                 return 0;
1174             }
1175         }
1176     }
1177     return -1;
1178 }
1179
1180
1181 /**
1182  * Reduce the bandwidth to reduce the number of bits used for a given SNR offset.
1183  * This is a second fallback for when bit allocation still fails after exponents
1184  * have been downgraded.
1185  * @return non-zero if bandwidth reduction was unsuccessful
1186  */
1187 static int reduce_bandwidth(AC3EncodeContext *s, int min_bw_code)
1188 {
1189     int ch;
1190
1191     if (s->bandwidth_code[0] > min_bw_code) {
1192         for (ch = 0; ch < s->fbw_channels; ch++) {
1193             s->bandwidth_code[ch]--;
1194             s->nb_coefs[ch] = s->bandwidth_code[ch] * 3 + 73;
1195         }
1196         return 0;
1197     }
1198     return -1;
1199 }
1200
1201
1202 /**
1203  * Perform bit allocation search.
1204  * Finds the SNR offset value that maximizes quality and fits in the specified
1205  * frame size.  Output is the SNR offset and a set of bit allocation pointers
1206  * used to quantize the mantissas.
1207  */
1208 static int compute_bit_allocation(AC3EncodeContext *s)
1209 {
1210     int ret;
1211
1212     count_frame_bits(s);
1213
1214     bit_alloc_masking(s);
1215
1216     ret = cbr_bit_allocation(s);
1217     while (ret) {
1218         /* fallback 1: downgrade exponents */
1219         if (!downgrade_exponents(s)) {
1220             extract_exponents(s);
1221             encode_exponents(s);
1222             group_exponents(s);
1223             ret = compute_bit_allocation(s);
1224             continue;
1225         }
1226
1227         /* fallback 2: reduce bandwidth */
1228         /* only do this if the user has not specified a specific cutoff
1229            frequency */
1230         if (!s->cutoff && !reduce_bandwidth(s, 0)) {
1231             process_exponents(s);
1232             ret = compute_bit_allocation(s);
1233             continue;
1234         }
1235
1236         /* fallbacks were not enough... */
1237         break;
1238     }
1239
1240     return ret;
1241 }
1242
1243
1244 /**
1245  * Symmetric quantization on 'levels' levels.
1246  */
1247 static inline int sym_quant(int c, int e, int levels)
1248 {
1249     int v;
1250
1251     if (c >= 0) {
1252         v = (levels * (c << e)) >> 24;
1253         v = (v + 1) >> 1;
1254         v = (levels >> 1) + v;
1255     } else {
1256         v = (levels * ((-c) << e)) >> 24;
1257         v = (v + 1) >> 1;
1258         v = (levels >> 1) - v;
1259     }
1260     assert(v >= 0 && v < levels);
1261     return v;
1262 }
1263
1264
1265 /**
1266  * Asymmetric quantization on 2^qbits levels.
1267  */
1268 static inline int asym_quant(int c, int e, int qbits)
1269 {
1270     int lshift, m, v;
1271
1272     lshift = e + qbits - 24;
1273     if (lshift >= 0)
1274         v = c << lshift;
1275     else
1276         v = c >> (-lshift);
1277     /* rounding */
1278     v = (v + 1) >> 1;
1279     m = (1 << (qbits-1));
1280     if (v >= m)
1281         v = m - 1;
1282     assert(v >= -m);
1283     return v & ((1 << qbits)-1);
1284 }
1285
1286
1287 /**
1288  * Quantize a set of mantissas for a single channel in a single block.
1289  */
1290 static void quantize_mantissas_blk_ch(AC3EncodeContext *s, int32_t *mdct_coef,
1291                                       int8_t exp_shift, uint8_t *exp,
1292                                       uint8_t *bap, uint16_t *qmant, int n)
1293 {
1294     int i;
1295
1296     for (i = 0; i < n; i++) {
1297         int v;
1298         int c = mdct_coef[i];
1299         int e = exp[i] - exp_shift;
1300         int b = bap[i];
1301         switch (b) {
1302         case 0:
1303             v = 0;
1304             break;
1305         case 1:
1306             v = sym_quant(c, e, 3);
1307             switch (s->mant1_cnt) {
1308             case 0:
1309                 s->qmant1_ptr = &qmant[i];
1310                 v = 9 * v;
1311                 s->mant1_cnt = 1;
1312                 break;
1313             case 1:
1314                 *s->qmant1_ptr += 3 * v;
1315                 s->mant1_cnt = 2;
1316                 v = 128;
1317                 break;
1318             default:
1319                 *s->qmant1_ptr += v;
1320                 s->mant1_cnt = 0;
1321                 v = 128;
1322                 break;
1323             }
1324             break;
1325         case 2:
1326             v = sym_quant(c, e, 5);
1327             switch (s->mant2_cnt) {
1328             case 0:
1329                 s->qmant2_ptr = &qmant[i];
1330                 v = 25 * v;
1331                 s->mant2_cnt = 1;
1332                 break;
1333             case 1:
1334                 *s->qmant2_ptr += 5 * v;
1335                 s->mant2_cnt = 2;
1336                 v = 128;
1337                 break;
1338             default:
1339                 *s->qmant2_ptr += v;
1340                 s->mant2_cnt = 0;
1341                 v = 128;
1342                 break;
1343             }
1344             break;
1345         case 3:
1346             v = sym_quant(c, e, 7);
1347             break;
1348         case 4:
1349             v = sym_quant(c, e, 11);
1350             switch (s->mant4_cnt) {
1351             case 0:
1352                 s->qmant4_ptr = &qmant[i];
1353                 v = 11 * v;
1354                 s->mant4_cnt = 1;
1355                 break;
1356             default:
1357                 *s->qmant4_ptr += v;
1358                 s->mant4_cnt = 0;
1359                 v = 128;
1360                 break;
1361             }
1362             break;
1363         case 5:
1364             v = sym_quant(c, e, 15);
1365             break;
1366         case 14:
1367             v = asym_quant(c, e, 14);
1368             break;
1369         case 15:
1370             v = asym_quant(c, e, 16);
1371             break;
1372         default:
1373             v = asym_quant(c, e, b - 1);
1374             break;
1375         }
1376         qmant[i] = v;
1377     }
1378 }
1379
1380
1381 /**
1382  * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1383  */
1384 static void quantize_mantissas(AC3EncodeContext *s)
1385 {
1386     int blk, ch;
1387
1388
1389     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1390         AC3Block *block = &s->blocks[blk];
1391         s->mant1_cnt  = s->mant2_cnt  = s->mant4_cnt  = 0;
1392         s->qmant1_ptr = s->qmant2_ptr = s->qmant4_ptr = NULL;
1393
1394         for (ch = 0; ch < s->channels; ch++) {
1395             quantize_mantissas_blk_ch(s, block->mdct_coef[ch], block->exp_shift[ch],
1396                                       block->exp[ch], block->bap[ch],
1397                                       block->qmant[ch], s->nb_coefs[ch]);
1398         }
1399     }
1400 }
1401
1402
1403 /**
1404  * Write the AC-3 frame header to the output bitstream.
1405  */
1406 static void output_frame_header(AC3EncodeContext *s)
1407 {
1408     put_bits(&s->pb, 16, 0x0b77);   /* frame header */
1409     put_bits(&s->pb, 16, 0);        /* crc1: will be filled later */
1410     put_bits(&s->pb, 2,  s->bit_alloc.sr_code);
1411     put_bits(&s->pb, 6,  s->frame_size_code + (s->frame_size - s->frame_size_min) / 2);
1412     put_bits(&s->pb, 5,  s->bitstream_id);
1413     put_bits(&s->pb, 3,  s->bitstream_mode);
1414     put_bits(&s->pb, 3,  s->channel_mode);
1415     if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
1416         put_bits(&s->pb, 2, 1);     /* XXX -4.5 dB */
1417     if (s->channel_mode & 0x04)
1418         put_bits(&s->pb, 2, 1);     /* XXX -6 dB */
1419     if (s->channel_mode == AC3_CHMODE_STEREO)
1420         put_bits(&s->pb, 2, 0);     /* surround not indicated */
1421     put_bits(&s->pb, 1, s->lfe_on); /* LFE */
1422     put_bits(&s->pb, 5, 31);        /* dialog norm: -31 db */
1423     put_bits(&s->pb, 1, 0);         /* no compression control word */
1424     put_bits(&s->pb, 1, 0);         /* no lang code */
1425     put_bits(&s->pb, 1, 0);         /* no audio production info */
1426     put_bits(&s->pb, 1, 0);         /* no copyright */
1427     put_bits(&s->pb, 1, 1);         /* original bitstream */
1428     put_bits(&s->pb, 1, 0);         /* no time code 1 */
1429     put_bits(&s->pb, 1, 0);         /* no time code 2 */
1430     put_bits(&s->pb, 1, 0);         /* no additional bit stream info */
1431 }
1432
1433
1434 /**
1435  * Write one audio block to the output bitstream.
1436  */
1437 static void output_audio_block(AC3EncodeContext *s, int block_num)
1438 {
1439     int ch, i, baie, rbnd;
1440     AC3Block *block = &s->blocks[block_num];
1441
1442     /* block switching */
1443     for (ch = 0; ch < s->fbw_channels; ch++)
1444         put_bits(&s->pb, 1, 0);
1445
1446     /* dither flags */
1447     for (ch = 0; ch < s->fbw_channels; ch++)
1448         put_bits(&s->pb, 1, 1);
1449
1450     /* dynamic range codes */
1451     put_bits(&s->pb, 1, 0);
1452
1453     /* channel coupling */
1454     if (!block_num) {
1455         put_bits(&s->pb, 1, 1); /* coupling strategy present */
1456         put_bits(&s->pb, 1, 0); /* no coupling strategy */
1457     } else {
1458         put_bits(&s->pb, 1, 0); /* no new coupling strategy */
1459     }
1460
1461     /* stereo rematrixing */
1462     if (s->channel_mode == AC3_CHMODE_STEREO) {
1463         if (!block_num) {
1464             /* first block must define rematrixing (rematstr) */
1465             put_bits(&s->pb, 1, 1);
1466
1467             /* dummy rematrixing rematflg(1:4)=0 */
1468             for (rbnd = 0; rbnd < 4; rbnd++)
1469                 put_bits(&s->pb, 1, 0);
1470         } else {
1471             /* no matrixing (but should be used in the future) */
1472             put_bits(&s->pb, 1, 0);
1473         }
1474     }
1475
1476     /* exponent strategy */
1477     for (ch = 0; ch < s->fbw_channels; ch++)
1478         put_bits(&s->pb, 2, block->exp_strategy[ch]);
1479     if (s->lfe_on)
1480         put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]);
1481
1482     /* bandwidth */
1483     for (ch = 0; ch < s->fbw_channels; ch++) {
1484         if (block->exp_strategy[ch] != EXP_REUSE)
1485             put_bits(&s->pb, 6, s->bandwidth_code[ch]);
1486     }
1487
1488     /* exponents */
1489     for (ch = 0; ch < s->channels; ch++) {
1490         int nb_groups;
1491
1492         if (block->exp_strategy[ch] == EXP_REUSE)
1493             continue;
1494
1495         /* DC exponent */
1496         put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
1497
1498         /* exponent groups */
1499         nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
1500         for (i = 1; i <= nb_groups; i++)
1501             put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
1502
1503         /* gain range info */
1504         if (ch != s->lfe_channel)
1505             put_bits(&s->pb, 2, 0);
1506     }
1507
1508     /* bit allocation info */
1509     baie = (block_num == 0);
1510     put_bits(&s->pb, 1, baie);
1511     if (baie) {
1512         put_bits(&s->pb, 2, s->slow_decay_code);
1513         put_bits(&s->pb, 2, s->fast_decay_code);
1514         put_bits(&s->pb, 2, s->slow_gain_code);
1515         put_bits(&s->pb, 2, s->db_per_bit_code);
1516         put_bits(&s->pb, 3, s->floor_code);
1517     }
1518
1519     /* snr offset */
1520     put_bits(&s->pb, 1, baie);
1521     if (baie) {
1522         put_bits(&s->pb, 6, s->coarse_snr_offset);
1523         for (ch = 0; ch < s->channels; ch++) {
1524             put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
1525             put_bits(&s->pb, 3, s->fast_gain_code[ch]);
1526         }
1527     }
1528
1529     put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1530     put_bits(&s->pb, 1, 0); /* no data to skip */
1531
1532     /* mantissas */
1533     for (ch = 0; ch < s->channels; ch++) {
1534         int b, q;
1535         for (i = 0; i < s->nb_coefs[ch]; i++) {
1536             q = block->qmant[ch][i];
1537             b = block->bap[ch][i];
1538             switch (b) {
1539             case 0:                                         break;
1540             case 1: if (q != 128) put_bits(&s->pb,   5, q); break;
1541             case 2: if (q != 128) put_bits(&s->pb,   7, q); break;
1542             case 3:               put_bits(&s->pb,   3, q); break;
1543             case 4: if (q != 128) put_bits(&s->pb,   7, q); break;
1544             case 14:              put_bits(&s->pb,  14, q); break;
1545             case 15:              put_bits(&s->pb,  16, q); break;
1546             default:              put_bits(&s->pb, b-1, q); break;
1547             }
1548         }
1549     }
1550 }
1551
1552
1553 /** CRC-16 Polynomial */
1554 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1555
1556
1557 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1558 {
1559     unsigned int c;
1560
1561     c = 0;
1562     while (a) {
1563         if (a & 1)
1564             c ^= b;
1565         a = a >> 1;
1566         b = b << 1;
1567         if (b & (1 << 16))
1568             b ^= poly;
1569     }
1570     return c;
1571 }
1572
1573
1574 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
1575 {
1576     unsigned int r;
1577     r = 1;
1578     while (n) {
1579         if (n & 1)
1580             r = mul_poly(r, a, poly);
1581         a = mul_poly(a, a, poly);
1582         n >>= 1;
1583     }
1584     return r;
1585 }
1586
1587
1588 /**
1589  * Fill the end of the frame with 0's and compute the two CRCs.
1590  */
1591 static void output_frame_end(AC3EncodeContext *s)
1592 {
1593     const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_16_ANSI);
1594     int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1595     uint8_t *frame;
1596
1597     frame_size_58 = ((s->frame_size >> 2) + (s->frame_size >> 4)) << 1;
1598
1599     /* pad the remainder of the frame with zeros */
1600     flush_put_bits(&s->pb);
1601     frame = s->pb.buf;
1602     pad_bytes = s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2;
1603     assert(pad_bytes >= 0);
1604     if (pad_bytes > 0)
1605         memset(put_bits_ptr(&s->pb), 0, pad_bytes);
1606
1607     /* compute crc1 */
1608     /* this is not so easy because it is at the beginning of the data... */
1609     crc1    = av_bswap16(av_crc(crc_ctx, 0, frame + 4, frame_size_58 - 4));
1610     crc_inv = s->crc_inv[s->frame_size > s->frame_size_min];
1611     crc1    = mul_poly(crc_inv, crc1, CRC16_POLY);
1612     AV_WB16(frame + 2, crc1);
1613
1614     /* compute crc2 */
1615     crc2_partial = av_crc(crc_ctx, 0, frame + frame_size_58,
1616                           s->frame_size - frame_size_58 - 3);
1617     crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
1618     /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
1619     if (crc2 == 0x770B) {
1620         frame[s->frame_size - 3] ^= 0x1;
1621         crc2 = av_crc(crc_ctx, crc2_partial, frame + s->frame_size - 3, 1);
1622     }
1623     crc2 = av_bswap16(crc2);
1624     AV_WB16(frame + s->frame_size - 2, crc2);
1625 }
1626
1627
1628 /**
1629  * Write the frame to the output bitstream.
1630  */
1631 static void output_frame(AC3EncodeContext *s, unsigned char *frame)
1632 {
1633     int blk;
1634
1635     init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
1636
1637     output_frame_header(s);
1638
1639     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
1640         output_audio_block(s, blk);
1641
1642     output_frame_end(s);
1643 }
1644
1645
1646 /**
1647  * Encode a single AC-3 frame.
1648  */
1649 static int ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
1650                             int buf_size, void *data)
1651 {
1652     AC3EncodeContext *s = avctx->priv_data;
1653     const int16_t *samples = data;
1654     int ret;
1655
1656     if (s->bit_alloc.sr_code == 1)
1657         adjust_frame_size(s);
1658
1659     deinterleave_input_samples(s, samples);
1660
1661     apply_mdct(s);
1662
1663     process_exponents(s);
1664
1665     ret = compute_bit_allocation(s);
1666     if (ret) {
1667         av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
1668         return ret;
1669     }
1670
1671     quantize_mantissas(s);
1672
1673     output_frame(s, frame);
1674
1675     return s->frame_size;
1676 }
1677
1678
1679 /**
1680  * Finalize encoding and free any memory allocated by the encoder.
1681  */
1682 static av_cold int ac3_encode_close(AVCodecContext *avctx)
1683 {
1684     int blk, ch;
1685     AC3EncodeContext *s = avctx->priv_data;
1686
1687     for (ch = 0; ch < s->channels; ch++)
1688         av_freep(&s->planar_samples[ch]);
1689     av_freep(&s->planar_samples);
1690     av_freep(&s->bap_buffer);
1691     av_freep(&s->bap1_buffer);
1692     av_freep(&s->mdct_coef_buffer);
1693     av_freep(&s->exp_buffer);
1694     av_freep(&s->grouped_exp_buffer);
1695     av_freep(&s->psd_buffer);
1696     av_freep(&s->band_psd_buffer);
1697     av_freep(&s->mask_buffer);
1698     av_freep(&s->qmant_buffer);
1699     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1700         AC3Block *block = &s->blocks[blk];
1701         av_freep(&block->bap);
1702         av_freep(&block->mdct_coef);
1703         av_freep(&block->exp);
1704         av_freep(&block->grouped_exp);
1705         av_freep(&block->psd);
1706         av_freep(&block->band_psd);
1707         av_freep(&block->mask);
1708         av_freep(&block->qmant);
1709     }
1710
1711     mdct_end(&s->mdct);
1712
1713     av_freep(&avctx->coded_frame);
1714     return 0;
1715 }
1716
1717
1718 /**
1719  * Set channel information during initialization.
1720  */
1721 static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
1722                                     int64_t *channel_layout)
1723 {
1724     int ch_layout;
1725
1726     if (channels < 1 || channels > AC3_MAX_CHANNELS)
1727         return AVERROR(EINVAL);
1728     if ((uint64_t)*channel_layout > 0x7FF)
1729         return AVERROR(EINVAL);
1730     ch_layout = *channel_layout;
1731     if (!ch_layout)
1732         ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);
1733     if (av_get_channel_layout_nb_channels(ch_layout) != channels)
1734         return AVERROR(EINVAL);
1735
1736     s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);
1737     s->channels     = channels;
1738     s->fbw_channels = channels - s->lfe_on;
1739     s->lfe_channel  = s->lfe_on ? s->fbw_channels : -1;
1740     if (s->lfe_on)
1741         ch_layout -= AV_CH_LOW_FREQUENCY;
1742
1743     switch (ch_layout) {
1744     case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;
1745     case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;
1746     case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;
1747     case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;
1748     case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;
1749     case AV_CH_LAYOUT_QUAD:
1750     case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;
1751     case AV_CH_LAYOUT_5POINT0:
1752     case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;
1753     default:
1754         return AVERROR(EINVAL);
1755     }
1756
1757     s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];
1758     *channel_layout = ch_layout;
1759     if (s->lfe_on)
1760         *channel_layout |= AV_CH_LOW_FREQUENCY;
1761
1762     return 0;
1763 }
1764
1765
1766 static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
1767 {
1768     int i, ret;
1769
1770     /* validate channel layout */
1771     if (!avctx->channel_layout) {
1772         av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
1773                                       "encoder will guess the layout, but it "
1774                                       "might be incorrect.\n");
1775     }
1776     ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);
1777     if (ret) {
1778         av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");
1779         return ret;
1780     }
1781
1782     /* validate sample rate */
1783     for (i = 0; i < 9; i++) {
1784         if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)
1785             break;
1786     }
1787     if (i == 9) {
1788         av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
1789         return AVERROR(EINVAL);
1790     }
1791     s->sample_rate        = avctx->sample_rate;
1792     s->bit_alloc.sr_shift = i % 3;
1793     s->bit_alloc.sr_code  = i / 3;
1794
1795     /* validate bit rate */
1796     for (i = 0; i < 19; i++) {
1797         if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)
1798             break;
1799     }
1800     if (i == 19) {
1801         av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");
1802         return AVERROR(EINVAL);
1803     }
1804     s->bit_rate        = avctx->bit_rate;
1805     s->frame_size_code = i << 1;
1806
1807     /* validate cutoff */
1808     if (avctx->cutoff < 0) {
1809         av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\n");
1810         return AVERROR(EINVAL);
1811     }
1812     s->cutoff = avctx->cutoff;
1813     if (s->cutoff > (s->sample_rate >> 1))
1814         s->cutoff = s->sample_rate >> 1;
1815
1816     return 0;
1817 }
1818
1819
1820 /**
1821  * Set bandwidth for all channels.
1822  * The user can optionally supply a cutoff frequency. Otherwise an appropriate
1823  * default value will be used.
1824  */
1825 static av_cold void set_bandwidth(AC3EncodeContext *s)
1826 {
1827     int ch, bw_code;
1828
1829     if (s->cutoff) {
1830         /* calculate bandwidth based on user-specified cutoff frequency */
1831         int fbw_coeffs;
1832         fbw_coeffs     = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
1833         bw_code        = av_clip((fbw_coeffs - 73) / 3, 0, 60);
1834     } else {
1835         /* use default bandwidth setting */
1836         /* XXX: should compute the bandwidth according to the frame
1837            size, so that we avoid annoying high frequency artifacts */
1838         bw_code = 50;
1839     }
1840
1841     /* set number of coefficients for each channel */
1842     for (ch = 0; ch < s->fbw_channels; ch++) {
1843         s->bandwidth_code[ch] = bw_code;
1844         s->nb_coefs[ch]       = bw_code * 3 + 73;
1845     }
1846     if (s->lfe_on)
1847         s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */
1848 }
1849
1850
1851 static av_cold int allocate_buffers(AVCodecContext *avctx)
1852 {
1853     int blk, ch;
1854     AC3EncodeContext *s = avctx->priv_data;
1855
1856     FF_ALLOC_OR_GOTO(avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
1857                      alloc_fail);
1858     for (ch = 0; ch < s->channels; ch++) {
1859         FF_ALLOCZ_OR_GOTO(avctx, s->planar_samples[ch],
1860                           (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
1861                           alloc_fail);
1862     }
1863     FF_ALLOC_OR_GOTO(avctx, s->bap_buffer,  AC3_MAX_BLOCKS * s->channels *
1864                      AC3_MAX_COEFS * sizeof(*s->bap_buffer),  alloc_fail);
1865     FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
1866                      AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
1867     FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
1868                      AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
1869     FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
1870                      AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
1871     FF_ALLOC_OR_GOTO(avctx, s->grouped_exp_buffer, AC3_MAX_BLOCKS * s->channels *
1872                      128 * sizeof(*s->grouped_exp_buffer), alloc_fail);
1873     FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
1874                      AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
1875     FF_ALLOC_OR_GOTO(avctx, s->band_psd_buffer, AC3_MAX_BLOCKS * s->channels *
1876                      64 * sizeof(*s->band_psd_buffer), alloc_fail);
1877     FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
1878                      64 * sizeof(*s->mask_buffer), alloc_fail);
1879     FF_ALLOC_OR_GOTO(avctx, s->qmant_buffer, AC3_MAX_BLOCKS * s->channels *
1880                      AC3_MAX_COEFS * sizeof(*s->qmant_buffer), alloc_fail);
1881     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
1882         AC3Block *block = &s->blocks[blk];
1883         FF_ALLOC_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
1884                          alloc_fail);
1885         FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
1886                           alloc_fail);
1887         FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
1888                           alloc_fail);
1889         FF_ALLOCZ_OR_GOTO(avctx, block->grouped_exp, s->channels * sizeof(*block->grouped_exp),
1890                           alloc_fail);
1891         FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
1892                           alloc_fail);
1893         FF_ALLOCZ_OR_GOTO(avctx, block->band_psd, s->channels * sizeof(*block->band_psd),
1894                           alloc_fail);
1895         FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
1896                           alloc_fail);
1897         FF_ALLOCZ_OR_GOTO(avctx, block->qmant, s->channels * sizeof(*block->qmant),
1898                           alloc_fail);
1899
1900         for (ch = 0; ch < s->channels; ch++) {
1901             block->bap[ch]         = &s->bap_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1902             block->mdct_coef[ch]   = &s->mdct_coef_buffer  [AC3_MAX_COEFS * (blk * s->channels + ch)];
1903             block->exp[ch]         = &s->exp_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1904             block->grouped_exp[ch] = &s->grouped_exp_buffer[128           * (blk * s->channels + ch)];
1905             block->psd[ch]         = &s->psd_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
1906             block->band_psd[ch]    = &s->band_psd_buffer   [64            * (blk * s->channels + ch)];
1907             block->mask[ch]        = &s->mask_buffer       [64            * (blk * s->channels + ch)];
1908             block->qmant[ch]       = &s->qmant_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
1909         }
1910     }
1911
1912     return 0;
1913 alloc_fail:
1914     return AVERROR(ENOMEM);
1915 }
1916
1917
1918 /**
1919  * Initialize the encoder.
1920  */
1921 static av_cold int ac3_encode_init(AVCodecContext *avctx)
1922 {
1923     AC3EncodeContext *s = avctx->priv_data;
1924     int ret, frame_size_58;
1925
1926     avctx->frame_size = AC3_FRAME_SIZE;
1927
1928     ac3_common_init();
1929
1930     ret = validate_options(avctx, s);
1931     if (ret)
1932         return ret;
1933
1934     s->bitstream_id   = 8 + s->bit_alloc.sr_shift;
1935     s->bitstream_mode = 0; /* complete main audio service */
1936
1937     s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];
1938     s->bits_written    = 0;
1939     s->samples_written = 0;
1940     s->frame_size      = s->frame_size_min;
1941
1942     /* calculate crc_inv for both possible frame sizes */
1943     frame_size_58 = (( s->frame_size    >> 2) + ( s->frame_size    >> 4)) << 1;
1944     s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1945     if (s->bit_alloc.sr_code == 1) {
1946         frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;
1947         s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);
1948     }
1949
1950     set_bandwidth(s);
1951
1952     exponent_init(s);
1953
1954     bit_alloc_init(s);
1955
1956     ret = mdct_init(avctx, &s->mdct, 9);
1957     if (ret)
1958         goto init_fail;
1959
1960     ret = allocate_buffers(avctx);
1961     if (ret)
1962         goto init_fail;
1963
1964     avctx->coded_frame= avcodec_alloc_frame();
1965
1966     dsputil_init(&s->dsp, avctx);
1967
1968     return 0;
1969 init_fail:
1970     ac3_encode_close(avctx);
1971     return ret;
1972 }
1973
1974
1975 #ifdef TEST
1976 /*************************************************************************/
1977 /* TEST */
1978
1979 #include "libavutil/lfg.h"
1980
1981 #define MDCT_NBITS 9
1982 #define MDCT_SAMPLES (1 << MDCT_NBITS)
1983 #define FN (MDCT_SAMPLES/4)
1984
1985
1986 static void fft_test(AC3MDCTContext *mdct, AVLFG *lfg)
1987 {
1988     IComplex in[FN], in1[FN];
1989     int k, n, i;
1990     float sum_re, sum_im, a;
1991
1992     for (i = 0; i < FN; i++) {
1993         in[i].re = av_lfg_get(lfg) % 65535 - 32767;
1994         in[i].im = av_lfg_get(lfg) % 65535 - 32767;
1995         in1[i]   = in[i];
1996     }
1997     fft(mdct, in, 7);
1998
1999     /* do it by hand */
2000     for (k = 0; k < FN; k++) {
2001         sum_re = 0;
2002         sum_im = 0;
2003         for (n = 0; n < FN; n++) {
2004             a = -2 * M_PI * (n * k) / FN;
2005             sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
2006             sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
2007         }
2008         av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n",
2009                k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
2010     }
2011 }
2012
2013
2014 static void mdct_test(AC3MDCTContext *mdct, AVLFG *lfg)
2015 {
2016     int16_t input[MDCT_SAMPLES];
2017     int32_t output[AC3_MAX_COEFS];
2018     float input1[MDCT_SAMPLES];
2019     float output1[AC3_MAX_COEFS];
2020     float s, a, err, e, emax;
2021     int i, k, n;
2022
2023     for (i = 0; i < MDCT_SAMPLES; i++) {
2024         input[i]  = (av_lfg_get(lfg) % 65535 - 32767) * 9 / 10;
2025         input1[i] = input[i];
2026     }
2027
2028     mdct512(mdct, output, input);
2029
2030     /* do it by hand */
2031     for (k = 0; k < AC3_MAX_COEFS; k++) {
2032         s = 0;
2033         for (n = 0; n < MDCT_SAMPLES; n++) {
2034             a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES));
2035             s += input1[n] * cos(a);
2036         }
2037         output1[k] = -2 * s / MDCT_SAMPLES;
2038     }
2039
2040     err  = 0;
2041     emax = 0;
2042     for (i = 0; i < AC3_MAX_COEFS; i++) {
2043         av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]);
2044         e = output[i] - output1[i];
2045         if (e > emax)
2046             emax = e;
2047         err += e * e;
2048     }
2049     av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax);
2050 }
2051
2052
2053 int main(void)
2054 {
2055     AVLFG lfg;
2056     AC3MDCTContext mdct;
2057
2058     mdct.avctx = NULL;
2059     av_log_set_level(AV_LOG_DEBUG);
2060     mdct_init(&mdct, 9);
2061
2062     fft_test(&mdct, &lfg);
2063     mdct_test(&mdct, &lfg);
2064
2065     return 0;
2066 }
2067 #endif /* TEST */
2068
2069
2070 AVCodec ac3_encoder = {
2071     "ac3",
2072     AVMEDIA_TYPE_AUDIO,
2073     CODEC_ID_AC3,
2074     sizeof(AC3EncodeContext),
2075     ac3_encode_init,
2076     ac3_encode_frame,
2077     ac3_encode_close,
2078     NULL,
2079     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
2080     .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
2081     .channel_layouts = ac3_channel_layouts,
2082 };