ffmpeg / libavcodec / ac3enc.c @ 8999944e
History  View  Annotate  Download (54.9 KB)
1 
/*


2 
* The simplest AC3 encoder

3 
* Copyright (c) 2000 Fabrice Bellard

4 
*

5 
* This file is part of FFmpeg.

6 
*

7 
* FFmpeg is free software; you can redistribute it and/or

8 
* modify it under the terms of the GNU Lesser General Public

9 
* License as published by the Free Software Foundation; either

10 
* version 2.1 of the License, or (at your option) any later version.

11 
*

12 
* FFmpeg is distributed in the hope that it will be useful,

13 
* but WITHOUT ANY WARRANTY; without even the implied warranty of

14 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

15 
* Lesser General Public License for more details.

16 
*

17 
* You should have received a copy of the GNU Lesser General Public

18 
* License along with FFmpeg; if not, write to the Free Software

19 
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 021101301 USA

20 
*/

21  
22 
/**

23 
* @file

24 
* The simplest AC3 encoder.

25 
*/

26  
27 
//#define DEBUG

28  
29 
#include "libavcore/audioconvert.h" 
30 
#include "libavutil/crc.h" 
31 
#include "avcodec.h" 
32 
#include "put_bits.h" 
33 
#include "ac3.h" 
34 
#include "audioconvert.h" 
35  
36  
37 
#define MDCT_NBITS 9 
38 
#define MDCT_SAMPLES (1 << MDCT_NBITS) 
39  
40 
/** 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 fixedpoint MDCT calculation.

53 
*/

54 
typedef struct IComplex { 
55 
int16_t re,im; 
56 
} IComplex; 
57  
58 
/**

59 
* AC3 encoder private context.

60 
*/

61 
typedef struct AC3EncodeContext { 
62 
PutBitContext pb; ///< bitstream writer context

63  
64 
int bitstream_id; ///< bitstream id (bsid) 
65 
int bitstream_mode; ///< bitstream mode (bsmod) 
66  
67 
int bit_rate; ///< target bit rate, in bitspersecond 
68 
int sample_rate; ///< sampling frequency, in Hz 
69  
70 
int frame_size_min; ///< minimum frame size in case rounding is necessary 
71 
int frame_size; ///< current frame size in bytes 
72 
int frame_size_code; ///< frame size code (frmsizecod) 
73 
int bits_written; ///< bit count (used to avg. bitrate) 
74 
int samples_written; ///< sample count (used to avg. bitrate) 
75  
76 
int fbw_channels; ///< number of fullbandwidth channels (nfchans) 
77 
int channels; ///< total number of channels (nchans) 
78 
int lfe_on; ///< indicates if there is an LFE channel (lfeon) 
79 
int lfe_channel; ///< channel index of the LFE channel 
80 
int channel_mode; ///< channel mode (acmod) 
81 
const uint8_t *channel_map; ///< channel map used to reorder channels 
82  
83 
int bandwidth_code[AC3_MAX_CHANNELS]; ///< bandwidth code (0 to 60) (chbwcod) 
84 
int nb_coefs[AC3_MAX_CHANNELS];

85  
86 
/* bitrate allocation control */

87 
int slow_gain_code; ///< slow gain code (sgaincod) 
88 
int slow_decay_code; ///< slow decay code (sdcycod) 
89 
int fast_decay_code; ///< fast decay code (fdcycod) 
90 
int db_per_bit_code; ///< dB/bit code (dbpbcod) 
91 
int floor_code; ///< floor code (floorcod) 
92 
AC3BitAllocParameters bit_alloc; ///< bit allocation parameters

93 
int coarse_snr_offset; ///< coarse SNR offsets (csnroffst) 
94 
int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signaltomask ratio) (fgaincod) 
95 
int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst) 
96  
97 
/* mantissa encoding */

98 
int mant1_cnt, mant2_cnt, mant4_cnt; ///< mantissa counts for bap=1,2,4 
99 
uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; ///< mantissa pointers for bap=1,2,4

100  
101 
int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; ///< last 256 samples from previous frame

102 
} AC3EncodeContext; 
103  
104  
105 
/** MDCT and FFT tables */

106 
static int16_t costab[64]; 
107 
static int16_t sintab[64]; 
108 
static int16_t xcos1[128]; 
109 
static int16_t xsin1[128]; 
110  
111  
112 
/**

113 
* Adjust the frame size to make the average bit rate match the target bit rate.

114 
* This is only needed for 11025, 22050, and 44100 sample rates.

115 
*/

116 
static void adjust_frame_size(AC3EncodeContext *s) 
117 
{ 
118 
while (s>bits_written >= s>bit_rate && s>samples_written >= s>sample_rate) {

119 
s>bits_written = s>bit_rate; 
120 
s>samples_written = s>sample_rate; 
121 
} 
122 
s>frame_size = s>frame_size_min + 2 * (s>bits_written * s>sample_rate < s>samples_written * s>bit_rate);

123 
s>bits_written += s>frame_size * 8;

124 
s>samples_written += AC3_FRAME_SIZE; 
125 
} 
126  
127  
128 
/**

129 
* Deinterleave input samples.

130 
* Channels are reordered from FFmpeg's default order to AC3 order.

131 
*/

132 
static void deinterleave_input_samples(AC3EncodeContext *s, 
133 
const int16_t *samples,

134 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE]) 
135 
{ 
136 
int ch, i;

137  
138 
/* deinterleave and remap input samples */

139 
for (ch = 0; ch < s>channels; ch++) { 
140 
const int16_t *sptr;

141 
int sinc;

142  
143 
/* copy last 256 samples of previous frame to the start of the current frame */

144 
memcpy(&planar_samples[ch][0], s>last_samples[ch],

145 
AC3_BLOCK_SIZE * sizeof(planar_samples[0][0])); 
146  
147 
/* deinterleave */

148 
sinc = s>channels; 
149 
sptr = samples + s>channel_map[ch]; 
150 
for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {

151 
planar_samples[ch][i] = *sptr; 
152 
sptr += sinc; 
153 
} 
154  
155 
/* save last 256 samples for next frame */

156 
memcpy(s>last_samples[ch], &planar_samples[ch][6* AC3_BLOCK_SIZE],

157 
AC3_BLOCK_SIZE * sizeof(planar_samples[0][0])); 
158 
} 
159 
} 
160  
161  
162 
/**

163 
* Initialize FFT tables.

164 
* @param ln log2(FFT size)

165 
*/

166 
static av_cold void fft_init(int ln) 
167 
{ 
168 
int i, n, n2;

169 
float alpha;

170  
171 
n = 1 << ln;

172 
n2 = n >> 1;

173  
174 
for (i = 0; i < n2; i++) { 
175 
alpha = 2.0 * M_PI * i / n; 
176 
costab[i] = FIX15(cos(alpha)); 
177 
sintab[i] = FIX15(sin(alpha)); 
178 
} 
179 
} 
180  
181  
182 
/**

183 
* Initialize MDCT tables.

184 
* @param nbits log2(MDCT size)

185 
*/

186 
static av_cold void mdct_init(int nbits) 
187 
{ 
188 
int i, n, n4;

189  
190 
n = 1 << nbits;

191 
n4 = n >> 2;

192  
193 
fft_init(nbits  2);

194  
195 
for (i = 0; i < n4; i++) { 
196 
float alpha = 2.0 * M_PI * (i + 1.0 / 8.0) / n; 
197 
xcos1[i] = FIX15(cos(alpha)); 
198 
xsin1[i] = FIX15(sin(alpha)); 
199 
} 
200 
} 
201  
202  
203 
/** Butterfly op */

204 
#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \

205 
{ \ 
206 
int ax, ay, bx, by; \

207 
bx = pre1; \ 
208 
by = pim1; \ 
209 
ax = qre1; \ 
210 
ay = qim1; \ 
211 
pre = (bx + ax) >> 1; \

212 
pim = (by + ay) >> 1; \

213 
qre = (bx  ax) >> 1; \

214 
qim = (by  ay) >> 1; \

215 
} 
216  
217  
218 
/** Complex multiply */

219 
#define CMUL(pre, pim, are, aim, bre, bim) \

220 
{ \ 
221 
pre = (MUL16(are, bre)  MUL16(aim, bim)) >> 15; \

222 
pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15; \

223 
} 
224  
225  
226 
/**

227 
* Calculate a 2^n point complex FFT on 2^ln points.

228 
* @param z complex input/output samples

229 
* @param ln log2(FFT size)

230 
*/

231 
static void fft(IComplex *z, int ln) 
232 
{ 
233 
int j, l, np, np2;

234 
int nblocks, nloops;

235 
register IComplex *p,*q;

236 
int tmp_re, tmp_im;

237  
238 
np = 1 << ln;

239  
240 
/* reverse */

241 
for (j = 0; j < np; j++) { 
242 
int k = av_reverse[j] >> (8  ln); 
243 
if (k < j)

244 
FFSWAP(IComplex, z[k], z[j]); 
245 
} 
246  
247 
/* pass 0 */

248  
249 
p = &z[0];

250 
j = np >> 1;

251 
do {

252 
BF(p[0].re, p[0].im, p[1].re, p[1].im, 
253 
p[0].re, p[0].im, p[1].re, p[1].im); 
254 
p += 2;

255 
} while (j);

256  
257 
/* pass 1 */

258  
259 
p = &z[0];

260 
j = np >> 2;

261 
do {

262 
BF(p[0].re, p[0].im, p[2].re, p[2].im, 
263 
p[0].re, p[0].im, p[2].re, p[2].im); 
264 
BF(p[1].re, p[1].im, p[3].re, p[3].im, 
265 
p[1].re, p[1].im, p[3].im, p[3].re); 
266 
p+=4;

267 
} while (j);

268  
269 
/* pass 2 .. ln1 */

270  
271 
nblocks = np >> 3;

272 
nloops = 1 << 2; 
273 
np2 = np >> 1;

274 
do {

275 
p = z; 
276 
q = z + nloops; 
277 
for (j = 0; j < nblocks; j++) { 
278 
BF(p>re, p>im, q>re, q>im, 
279 
p>re, p>im, q>re, q>im); 
280 
p++; 
281 
q++; 
282 
for(l = nblocks; l < np2; l += nblocks) {

283 
CMUL(tmp_re, tmp_im, costab[l], sintab[l], q>re, q>im); 
284 
BF(p>re, p>im, q>re, q>im, 
285 
p>re, p>im, tmp_re, tmp_im); 
286 
p++; 
287 
q++; 
288 
} 
289 
p += nloops; 
290 
q += nloops; 
291 
} 
292 
nblocks = nblocks >> 1;

293 
nloops = nloops << 1;

294 
} while (nblocks);

295 
} 
296  
297  
298 
/**

299 
* Calculate a 512point MDCT

300 
* @param out 256 output frequency coefficients

301 
* @param in 512 windowed input audio samples

302 
*/

303 
static void mdct512(int32_t *out, int16_t *in) 
304 
{ 
305 
int i, re, im, re1, im1;

306 
int16_t rot[MDCT_SAMPLES]; 
307 
IComplex x[MDCT_SAMPLES/4];

308  
309 
/* shift to simplify computations */

310 
for (i = 0; i < MDCT_SAMPLES/4; i++) 
311 
rot[i] = in[i + 3*MDCT_SAMPLES/4]; 
312 
for (;i < MDCT_SAMPLES; i++)

313 
rot[i] = in[i  MDCT_SAMPLES/4];

314  
315 
/* pre rotation */

316 
for (i = 0; i < MDCT_SAMPLES/4; i++) { 
317 
re = ((int)rot[ 2*i]  (int)rot[MDCT_SAMPLES 12*i]) >> 1; 
318 
im = ((int)rot[MDCT_SAMPLES/2+2*i]  (int)rot[MDCT_SAMPLES/212*i]) >> 1; 
319 
CMUL(x[i].re, x[i].im, re, im, xcos1[i], xsin1[i]); 
320 
} 
321  
322 
fft(x, MDCT_NBITS  2);

323  
324 
/* post rotation */

325 
for (i = 0; i < MDCT_SAMPLES/4; i++) { 
326 
re = x[i].re; 
327 
im = x[i].im; 
328 
CMUL(re1, im1, re, im, xsin1[i], xcos1[i]); 
329 
out[ 2*i] = im1;

330 
out[MDCT_SAMPLES/212*i] = re1; 
331 
} 
332 
} 
333  
334  
335 
/**

336 
* Apply KBD window to input samples prior to MDCT.

337 
*/

338 
static void apply_window(int16_t *output, const int16_t *input, 
339 
const int16_t *window, int n) 
340 
{ 
341 
int i;

342 
int n2 = n >> 1; 
343  
344 
for (i = 0; i < n2; i++) { 
345 
output[i] = MUL16(input[i], window[i]) >> 15;

346 
output[ni1] = MUL16(input[ni1], window[i]) >> 15; 
347 
} 
348 
} 
349  
350  
351 
/**

352 
* Calculate the log2() of the maximum absolute value in an array.

353 
* @param tab input array

354 
* @param n number of values in the array

355 
* @return log2(max(abs(tab[])))

356 
*/

357 
static int log2_tab(int16_t *tab, int n) 
358 
{ 
359 
int i, v;

360  
361 
v = 0;

362 
for (i = 0; i < n; i++) 
363 
v = abs(tab[i]); 
364  
365 
return av_log2(v);

366 
} 
367  
368  
369 
/**

370 
* Leftshift each value in an array by a specified amount.

371 
* @param tab input array

372 
* @param n number of values in the array

373 
* @param lshift left shift amount. a negative value means right shift.

374 
*/

375 
static void lshift_tab(int16_t *tab, int n, int lshift) 
376 
{ 
377 
int i;

378  
379 
if (lshift > 0) { 
380 
for(i = 0; i < n; i++) 
381 
tab[i] <<= lshift; 
382 
} else if (lshift < 0) { 
383 
lshift = lshift; 
384 
for (i = 0; i < n; i++) 
385 
tab[i] >>= lshift; 
386 
} 
387 
} 
388  
389  
390 
/**

391 
* Normalize the input samples to use the maximum available precision.

392 
* This assumes signed 16bit input samples. Exponents are reduced by 9 to

393 
* match the 24bit internal precision for MDCT coefficients.

394 
*

395 
* @return exponent shift

396 
*/

397 
static int normalize_samples(AC3EncodeContext *s, 
398 
int16_t windowed_samples[AC3_WINDOW_SIZE]) 
399 
{ 
400 
int v = 14  log2_tab(windowed_samples, AC3_WINDOW_SIZE); 
401 
v = FFMAX(0, v);

402 
lshift_tab(windowed_samples, AC3_WINDOW_SIZE, v); 
403 
return v  9; 
404 
} 
405  
406  
407 
/**

408 
* Apply the MDCT to input samples to generate frequency coefficients.

409 
* This applies the KBD window and normalizes the input to reduce precision

410 
* loss due to fixedpoint calculations.

411 
*/

412 
static void apply_mdct(AC3EncodeContext *s, 
413 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE], 
414 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
415 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
416 
{ 
417 
int blk, ch;

418 
int16_t windowed_samples[AC3_WINDOW_SIZE]; 
419  
420 
for (ch = 0; ch < s>channels; ch++) { 
421 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
422 
const int16_t *input_samples = &planar_samples[ch][blk * AC3_BLOCK_SIZE];

423  
424 
apply_window(windowed_samples, input_samples, ff_ac3_window, AC3_WINDOW_SIZE); 
425  
426 
exp_shift[blk][ch] = normalize_samples(s, windowed_samples); 
427  
428 
mdct512(mdct_coef[blk][ch], windowed_samples); 
429 
} 
430 
} 
431 
} 
432  
433  
434 
/**

435 
* Extract exponents from the MDCT coefficients.

436 
* This takes into account the normalization that was done to the input samples

437 
* by adjusting the exponents by the exponent shift values.

438 
*/

439 
static void extract_exponents(AC3EncodeContext *s, 
440 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
441 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
442 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
443 
{ 
444 
int blk, ch, i;

445  
446 
/* extract exponents */

447 
for (ch = 0; ch < s>channels; ch++) { 
448 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
449 
/* compute "exponents". We take into account the normalization there */

450 
for (i = 0; i < AC3_MAX_COEFS; i++) { 
451 
int e;

452 
int v = abs(mdct_coef[blk][ch][i]);

453 
if (v == 0) 
454 
e = 24;

455 
else {

456 
e = 23  av_log2(v) + exp_shift[blk][ch];

457 
if (e >= 24) { 
458 
e = 24;

459 
mdct_coef[blk][ch][i] = 0;

460 
} 
461 
} 
462 
exp[blk][ch][i] = e; 
463 
} 
464 
} 
465 
} 
466 
} 
467  
468  
469 
/**

470 
* Calculate the sum of absolute differences (SAD) between 2 sets of exponents.

471 
*/

472 
static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) 
473 
{ 
474 
int sum, i;

475 
sum = 0;

476 
for (i = 0; i < n; i++) 
477 
sum += abs(exp1[i]  exp2[i]); 
478 
return sum;

479 
} 
480  
481  
482 
/**

483 
* Exponent Difference Threshold.

484 
* New exponents are sent if their SAD exceed this number.

485 
*/

486 
#define EXP_DIFF_THRESHOLD 1000 
487  
488  
489 
/**

490 
* Calculate exponent strategies for all blocks in a single channel.

491 
*/

492 
static void compute_exp_strategy_ch(uint8_t *exp_strategy, uint8_t **exp) 
493 
{ 
494 
int blk, blk1;

495 
int exp_diff;

496  
497 
/* estimate if the exponent variation & decide if they should be

498 
reused in the next frame */

499 
exp_strategy[0] = EXP_NEW;

500 
for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) { 
501 
exp_diff = calc_exp_diff(exp[blk], exp[blk1], AC3_MAX_COEFS);

502 
if (exp_diff > EXP_DIFF_THRESHOLD)

503 
exp_strategy[blk] = EXP_NEW; 
504 
else

505 
exp_strategy[blk] = EXP_REUSE; 
506 
} 
507  
508 
/* now select the encoding strategy type : if exponents are often

509 
recoded, we use a coarse encoding */

510 
blk = 0;

511 
while (blk < AC3_MAX_BLOCKS) {

512 
blk1 = blk + 1;

513 
while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)

514 
blk1++; 
515 
switch (blk1  blk) {

516 
case 1: exp_strategy[blk] = EXP_D45; break; 
517 
case 2: 
518 
case 3: exp_strategy[blk] = EXP_D25; break; 
519 
default: exp_strategy[blk] = EXP_D15; break; 
520 
} 
521 
blk = blk1; 
522 
} 
523 
} 
524  
525  
526 
/**

527 
* Calculate exponent strategies for all channels.

528 
* Array arrangement is reversed to simplify the perchannel calculation.

529 
*/

530 
static void compute_exp_strategy(AC3EncodeContext *s, 
531 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
532 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
533 
{ 
534 
uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; 
535 
uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; 
536 
int ch, blk;

537  
538 
for (ch = 0; ch < s>fbw_channels; ch++) { 
539 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
540 
exp1[ch][blk] = exp[blk][ch]; 
541 
exp_str1[ch][blk] = exp_strategy[blk][ch]; 
542 
} 
543  
544 
compute_exp_strategy_ch(exp_str1[ch], exp1[ch]); 
545  
546 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) 
547 
exp_strategy[blk][ch] = exp_str1[ch][blk]; 
548 
} 
549 
if (s>lfe_on) {

550 
ch = s>lfe_channel; 
551 
exp_strategy[0][ch] = EXP_D15;

552 
for (blk = 1; blk < 5; blk++) 
553 
exp_strategy[blk][ch] = EXP_REUSE; 
554 
} 
555 
} 
556  
557  
558 
/**

559 
* Set each encoded exponent in a block to the minimum of itself and the

560 
* exponent in the same frequency bin of a following block.

561 
* exp[i] = min(exp[i], exp1[i]

562 
*/

563 
static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS], int n) 
564 
{ 
565 
int i;

566 
for (i = 0; i < n; i++) { 
567 
if (exp1[i] < exp[i])

568 
exp[i] = exp1[i]; 
569 
} 
570 
} 
571  
572  
573 
/**

574 
* Update the exponents so that they are the ones the decoder will decode.

575 
*/

576 
static void encode_exponents_blk_ch(uint8_t encoded_exp[AC3_MAX_COEFS], 
577 
uint8_t exp[AC3_MAX_COEFS], 
578 
int nb_exps, int exp_strategy, 
579 
uint8_t *num_exp_groups) 
580 
{ 
581 
int group_size, nb_groups, i, j, k, exp_min;

582 
uint8_t exp1[AC3_MAX_COEFS]; 
583  
584 
group_size = exp_strategy + (exp_strategy == EXP_D45); 
585 
*num_exp_groups = (nb_exps + (group_size * 3)  4) / (3 * group_size); 
586 
nb_groups = *num_exp_groups * 3;

587  
588 
/* for each group, compute the minimum exponent */

589 
exp1[0] = exp[0]; /* DC exponent is handled separately */ 
590 
k = 1;

591 
for (i = 1; i <= nb_groups; i++) { 
592 
exp_min = exp[k]; 
593 
assert(exp_min >= 0 && exp_min <= 24); 
594 
for (j = 1; j < group_size; j++) { 
595 
if (exp[k+j] < exp_min)

596 
exp_min = exp[k+j]; 
597 
} 
598 
exp1[i] = exp_min; 
599 
k += group_size; 
600 
} 
601  
602 
/* constraint for DC exponent */

603 
if (exp1[0] > 15) 
604 
exp1[0] = 15; 
605  
606 
/* decrease the delta between each groups to within 2 so that they can be

607 
differentially encoded */

608 
for (i = 1; i <= nb_groups; i++) 
609 
exp1[i] = FFMIN(exp1[i], exp1[i1] + 2); 
610 
for (i = nb_groups1; i >= 0; i) 
611 
exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2); 
612  
613 
/* now we have the exponent values the decoder will see */

614 
encoded_exp[0] = exp1[0]; 
615 
k = 1;

616 
for (i = 1; i <= nb_groups; i++) { 
617 
for (j = 0; j < group_size; j++) 
618 
encoded_exp[k+j] = exp1[i]; 
619 
k += group_size; 
620 
} 
621 
} 
622  
623  
624 
/**

625 
* Encode exponents from original extracted form to what the decoder will see.

626 
* This copies and groups exponents based on exponent strategy and reduces

627 
* deltas between adjacent exponent groups so that they can be differentially

628 
* encoded.

629 
*/

630 
static void encode_exponents(AC3EncodeContext *s, 
631 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
632 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
633 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
634 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
635 
{ 
636 
int blk, blk1, blk2, ch;

637  
638 
for (ch = 0; ch < s>channels; ch++) { 
639 
/* for the EXP_REUSE case we select the min of the exponents */

640 
blk = 0;

641 
while (blk < AC3_MAX_BLOCKS) {

642 
blk1 = blk + 1;

643 
while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1][ch] == EXP_REUSE) {

644 
exponent_min(exp[blk][ch], exp[blk1][ch], s>nb_coefs[ch]); 
645 
blk1++; 
646 
} 
647 
encode_exponents_blk_ch(encoded_exp[blk][ch], 
648 
exp[blk][ch], s>nb_coefs[ch], 
649 
exp_strategy[blk][ch], 
650 
&num_exp_groups[blk][ch]); 
651 
/* copy encoded exponents for reuse case */

652 
for (blk2 = blk+1; blk2 < blk1; blk2++) { 
653 
memcpy(encoded_exp[blk2][ch], encoded_exp[blk][ch], 
654 
s>nb_coefs[ch] * sizeof(uint8_t));

655 
} 
656 
blk = blk1; 
657 
} 
658 
} 
659 
} 
660  
661  
662 
/**

663 
* Group exponents.

664 
* 3 deltaencoded exponents are in each 7bit group. The number of groups

665 
* varies depending on exponent strategy and bandwidth.

666 
* @return bits needed to encode the exponents

667 
*/

668 
static int group_exponents(AC3EncodeContext *s, 
669 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
670 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
671 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
672 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]) 
673 
{ 
674 
int blk, ch, i;

675 
int group_size, bit_count;

676 
uint8_t *p; 
677 
int delta0, delta1, delta2;

678 
int exp0, exp1;

679  
680 
bit_count = 0;

681 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
682 
for (ch = 0; ch < s>channels; ch++) { 
683 
if (exp_strategy[blk][ch] == EXP_REUSE) {

684 
num_exp_groups[blk][ch] = 0;

685 
continue;

686 
} 
687 
group_size = exp_strategy[blk][ch] + (exp_strategy[blk][ch] == EXP_D45); 
688 
bit_count += 4 + (num_exp_groups[blk][ch] * 7); 
689 
p = encoded_exp[blk][ch]; 
690  
691 
/* DC exponent */

692 
exp1 = *p++; 
693 
grouped_exp[blk][ch][0] = exp1;

694  
695 
/* remaining exponents are delta encoded */

696 
for (i = 1; i <= num_exp_groups[blk][ch]; i++) { 
697 
/* merge three delta in one code */

698 
exp0 = exp1; 
699 
exp1 = p[0];

700 
p += group_size; 
701 
delta0 = exp1  exp0 + 2;

702  
703 
exp0 = exp1; 
704 
exp1 = p[0];

705 
p += group_size; 
706 
delta1 = exp1  exp0 + 2;

707  
708 
exp0 = exp1; 
709 
exp1 = p[0];

710 
p += group_size; 
711 
delta2 = exp1  exp0 + 2;

712  
713 
grouped_exp[blk][ch][i] = ((delta0 * 5 + delta1) * 5) + delta2; 
714 
} 
715 
} 
716 
} 
717  
718 
return bit_count;

719 
} 
720  
721  
722 
/**

723 
* Calculate final exponents from the supplied MDCT coefficients and exponent shift.

724 
* Extract exponents from MDCT coefficients, calculate exponent strategies,

725 
* and encode final exponents.

726 
* @return bits needed to encode the exponents

727 
*/

728 
static int process_exponents(AC3EncodeContext *s, 
729 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
730 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
731 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
732 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
733 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
734 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
735 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]) 
736 
{ 
737 
extract_exponents(s, mdct_coef, exp_shift, exp); 
738  
739 
compute_exp_strategy(s, exp_strategy, exp); 
740  
741 
encode_exponents(s, exp, exp_strategy, num_exp_groups, encoded_exp); 
742  
743 
return group_exponents(s, encoded_exp, exp_strategy, num_exp_groups, grouped_exp);

744 
} 
745  
746  
747 
/**

748 
* Count the bits used to encode the frame, minus exponents and mantissas.

749 
* @return bit count

750 
*/

751 
static int count_frame_bits(AC3EncodeContext *s, 
752 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]) 
753 
{ 
754 
static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; 
755 
int blk, ch;

756 
int frame_bits;

757  
758 
/* header size */

759 
frame_bits = 65;

760 
frame_bits += frame_bits_inc[s>channel_mode]; 
761  
762 
/* audio blocks */

763 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
764 
frame_bits += s>fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */ 
765 
if (s>channel_mode == AC3_CHMODE_STEREO) {

766 
frame_bits++; /* rematstr */

767 
if (!blk)

768 
frame_bits += 4;

769 
} 
770 
frame_bits += 2 * s>fbw_channels; /* chexpstr[2] * c */ 
771 
if (s>lfe_on)

772 
frame_bits++; /* lfeexpstr */

773 
for (ch = 0; ch < s>fbw_channels; ch++) { 
774 
if (exp_strategy[blk][ch] != EXP_REUSE)

775 
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ 
776 
} 
777 
frame_bits++; /* baie */

778 
frame_bits++; /* snr */

779 
frame_bits += 2; /* delta / skip */ 
780 
} 
781 
frame_bits++; /* cplinu for block 0 */

782 
/* bit alloc info */

783 
/* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */

784 
/* csnroffset[6] */

785 
/* (fsnoffset[4] + fgaincod[4]) * c */

786 
frame_bits += 2*4 + 3 + 6 + s>channels * (4 + 3); 
787  
788 
/* auxdatae, crcrsv */

789 
frame_bits += 2;

790  
791 
/* CRC */

792 
frame_bits += 16;

793  
794 
return frame_bits;

795 
} 
796  
797  
798 
/**

799 
* Calculate the number of bits needed to encode a set of mantissas.

800 
*/

801 
static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) 
802 
{ 
803 
int bits, mant, i;

804  
805 
bits = 0;

806 
for (i = 0; i < nb_coefs; i++) { 
807 
mant = m[i]; 
808 
switch (mant) {

809 
case 0: 
810 
/* nothing */

811 
break;

812 
case 1: 
813 
/* 3 mantissa in 5 bits */

814 
if (s>mant1_cnt == 0) 
815 
bits += 5;

816 
if (++s>mant1_cnt == 3) 
817 
s>mant1_cnt = 0;

818 
break;

819 
case 2: 
820 
/* 3 mantissa in 7 bits */

821 
if (s>mant2_cnt == 0) 
822 
bits += 7;

823 
if (++s>mant2_cnt == 3) 
824 
s>mant2_cnt = 0;

825 
break;

826 
case 3: 
827 
bits += 3;

828 
break;

829 
case 4: 
830 
/* 2 mantissa in 7 bits */

831 
if (s>mant4_cnt == 0) 
832 
bits += 7;

833 
if (++s>mant4_cnt == 2) 
834 
s>mant4_cnt = 0;

835 
break;

836 
case 14: 
837 
bits += 14;

838 
break;

839 
case 15: 
840 
bits += 16;

841 
break;

842 
default:

843 
bits += mant  1;

844 
break;

845 
} 
846 
} 
847 
return bits;

848 
} 
849  
850  
851 
/**

852 
* Calculate masking curve based on the final exponents.

853 
* Also calculate the power spectral densities to use in future calculations.

854 
*/

855 
static void bit_alloc_masking(AC3EncodeContext *s, 
856 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
857 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
858 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
859 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]) 
860 
{ 
861 
int blk, ch;

862 
int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 
863  
864 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
865 
for (ch = 0; ch < s>channels; ch++) { 
866 
if(exp_strategy[blk][ch] == EXP_REUSE) {

867 
memcpy(psd[blk][ch], psd[blk1][ch], AC3_MAX_COEFS*sizeof(psd[0][0][0])); 
868 
memcpy(mask[blk][ch], mask[blk1][ch], AC3_CRITICAL_BANDS*sizeof(mask[0][0][0])); 
869 
} else {

870 
ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0,

871 
s>nb_coefs[ch], 
872 
psd[blk][ch], band_psd[blk][ch]); 
873 
ff_ac3_bit_alloc_calc_mask(&s>bit_alloc, band_psd[blk][ch], 
874 
0, s>nb_coefs[ch],

875 
ff_ac3_fast_gain_tab[s>fast_gain_code[ch]], 
876 
ch == s>lfe_channel, 
877 
DBA_NONE, 0, NULL, NULL, NULL, 
878 
mask[blk][ch]); 
879 
} 
880 
} 
881 
} 
882 
} 
883  
884  
885 
/**

886 
* Run the bit allocation with a given SNR offset.

887 
* This calculates the bit allocation pointers that will be used to determine

888 
* the quantization of each mantissa.

889 
* @return the number of remaining bits (positive or negative) if the given

890 
* SNR offset is used to quantize the mantissas.

891 
*/

892 
static int bit_alloc(AC3EncodeContext *s, 
893 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS], 
894 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
895 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
896 
int frame_bits, int coarse_snr_offset, int fine_snr_offset) 
897 
{ 
898 
int blk, ch;

899 
int snr_offset;

900  
901 
snr_offset = (((coarse_snr_offset  15) << 4) + fine_snr_offset) << 2; 
902  
903 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
904 
s>mant1_cnt = 0;

905 
s>mant2_cnt = 0;

906 
s>mant4_cnt = 0;

907 
for (ch = 0; ch < s>channels; ch++) { 
908 
ff_ac3_bit_alloc_calc_bap(mask[blk][ch], psd[blk][ch], 0,

909 
s>nb_coefs[ch], snr_offset, 
910 
s>bit_alloc.floor, ff_ac3_bap_tab, 
911 
bap[blk][ch]); 
912 
frame_bits += compute_mantissa_size(s, bap[blk][ch], s>nb_coefs[ch]); 
913 
} 
914 
} 
915 
return 8 * s>frame_size  frame_bits; 
916 
} 
917  
918  
919 
#define SNR_INC1 4 
920  
921 
/**

922 
* Perform bit allocation search.

923 
* Finds the SNR offset value that maximizes quality and fits in the specified

924 
* frame size. Output is the SNR offset and a set of bit allocation pointers

925 
* used to quantize the mantissas.

926 
*/

927 
static int compute_bit_allocation(AC3EncodeContext *s, 
928 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
929 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
930 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
931 
int frame_bits)

932 
{ 
933 
int ch;

934 
int coarse_snr_offset, fine_snr_offset;

935 
uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
936 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
937 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 
938  
939 
/* init default parameters */

940 
s>slow_decay_code = 2;

941 
s>fast_decay_code = 1;

942 
s>slow_gain_code = 1;

943 
s>db_per_bit_code = 2;

944 
s>floor_code = 4;

945 
for (ch = 0; ch < s>channels; ch++) 
946 
s>fast_gain_code[ch] = 4;

947  
948 
/* compute real values */

949 
s>bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s>slow_decay_code] >> s>bit_alloc.sr_shift; 
950 
s>bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s>fast_decay_code] >> s>bit_alloc.sr_shift; 
951 
s>bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s>slow_gain_code]; 
952 
s>bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s>db_per_bit_code]; 
953 
s>bit_alloc.floor = ff_ac3_floor_tab[s>floor_code]; 
954  
955 
/* count frame bits other than exponents and mantissas */

956 
frame_bits += count_frame_bits(s, exp_strategy); 
957  
958 
/* calculate psd and masking curve before doing bit allocation */

959 
bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask); 
960  
961 
/* now the big work begins : do the bit allocation. Modify the snr

962 
offset until we can pack everything in the requested frame size */

963  
964 
coarse_snr_offset = s>coarse_snr_offset; 
965 
while (coarse_snr_offset >= 0 && 
966 
bit_alloc(s, mask, psd, bap, frame_bits, coarse_snr_offset, 0) < 0) 
967 
coarse_snr_offset = SNR_INC1; 
968 
if (coarse_snr_offset < 0) { 
969 
return AVERROR(EINVAL);

970 
} 
971 
while (coarse_snr_offset + SNR_INC1 <= 63 && 
972 
bit_alloc(s, mask, psd, bap1, frame_bits, 
973 
coarse_snr_offset + SNR_INC1, 0) >= 0) { 
974 
coarse_snr_offset += SNR_INC1; 
975 
memcpy(bap, bap1, sizeof(bap1));

976 
} 
977 
while (coarse_snr_offset + 1 <= 63 && 
978 
bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) { 
979 
coarse_snr_offset++; 
980 
memcpy(bap, bap1, sizeof(bap1));

981 
} 
982  
983 
fine_snr_offset = 0;

984 
while (fine_snr_offset + SNR_INC1 <= 15 && 
985 
bit_alloc(s, mask, psd, bap1, frame_bits, 
986 
coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) {

987 
fine_snr_offset += SNR_INC1; 
988 
memcpy(bap, bap1, sizeof(bap1));

989 
} 
990 
while (fine_snr_offset + 1 <= 15 && 
991 
bit_alloc(s, mask, psd, bap1, frame_bits, 
992 
coarse_snr_offset, fine_snr_offset + 1) >= 0) { 
993 
fine_snr_offset++; 
994 
memcpy(bap, bap1, sizeof(bap1));

995 
} 
996  
997 
s>coarse_snr_offset = coarse_snr_offset; 
998 
for (ch = 0; ch < s>channels; ch++) 
999 
s>fine_snr_offset[ch] = fine_snr_offset; 
1000  
1001 
return 0; 
1002 
} 
1003  
1004  
1005 
/**

1006 
* Symmetric quantization on 'levels' levels.

1007 
*/

1008 
static inline int sym_quant(int c, int e, int levels) 
1009 
{ 
1010 
int v;

1011  
1012 
if (c >= 0) { 
1013 
v = (levels * (c << e)) >> 24;

1014 
v = (v + 1) >> 1; 
1015 
v = (levels >> 1) + v;

1016 
} else {

1017 
v = (levels * ((c) << e)) >> 24;

1018 
v = (v + 1) >> 1; 
1019 
v = (levels >> 1)  v;

1020 
} 
1021 
assert (v >= 0 && v < levels);

1022 
return v;

1023 
} 
1024  
1025  
1026 
/**

1027 
* Asymmetric quantization on 2^qbits levels.

1028 
*/

1029 
static inline int asym_quant(int c, int e, int qbits) 
1030 
{ 
1031 
int lshift, m, v;

1032  
1033 
lshift = e + qbits  24;

1034 
if (lshift >= 0) 
1035 
v = c << lshift; 
1036 
else

1037 
v = c >> (lshift); 
1038 
/* rounding */

1039 
v = (v + 1) >> 1; 
1040 
m = (1 << (qbits1)); 
1041 
if (v >= m)

1042 
v = m  1;

1043 
assert(v >= m); 
1044 
return v & ((1 << qbits)1); 
1045 
} 
1046  
1047  
1048 
/**

1049 
* Quantize a set of mantissas for a single channel in a single block.

1050 
*/

1051 
static void quantize_mantissas_blk_ch(AC3EncodeContext *s, 
1052 
int32_t *mdct_coef, int8_t exp_shift, 
1053 
uint8_t *encoded_exp, uint8_t *bap, 
1054 
uint16_t *qmant, int n)

1055 
{ 
1056 
int i;

1057  
1058 
for (i = 0; i < n; i++) { 
1059 
int v;

1060 
int c = mdct_coef[i];

1061 
int e = encoded_exp[i]  exp_shift;

1062 
int b = bap[i];

1063 
switch (b) {

1064 
case 0: 
1065 
v = 0;

1066 
break;

1067 
case 1: 
1068 
v = sym_quant(c, e, 3);

1069 
switch (s>mant1_cnt) {

1070 
case 0: 
1071 
s>qmant1_ptr = &qmant[i]; 
1072 
v = 9 * v;

1073 
s>mant1_cnt = 1;

1074 
break;

1075 
case 1: 
1076 
*s>qmant1_ptr += 3 * v;

1077 
s>mant1_cnt = 2;

1078 
v = 128;

1079 
break;

1080 
default:

1081 
*s>qmant1_ptr += v; 
1082 
s>mant1_cnt = 0;

1083 
v = 128;

1084 
break;

1085 
} 
1086 
break;

1087 
case 2: 
1088 
v = sym_quant(c, e, 5);

1089 
switch (s>mant2_cnt) {

1090 
case 0: 
1091 
s>qmant2_ptr = &qmant[i]; 
1092 
v = 25 * v;

1093 
s>mant2_cnt = 1;

1094 
break;

1095 
case 1: 
1096 
*s>qmant2_ptr += 5 * v;

1097 
s>mant2_cnt = 2;

1098 
v = 128;

1099 
break;

1100 
default:

1101 
*s>qmant2_ptr += v; 
1102 
s>mant2_cnt = 0;

1103 
v = 128;

1104 
break;

1105 
} 
1106 
break;

1107 
case 3: 
1108 
v = sym_quant(c, e, 7);

1109 
break;

1110 
case 4: 
1111 
v = sym_quant(c, e, 11);

1112 
switch (s>mant4_cnt) {

1113 
case 0: 
1114 
s>qmant4_ptr = &qmant[i]; 
1115 
v = 11 * v;

1116 
s>mant4_cnt = 1;

1117 
break;

1118 
default:

1119 
*s>qmant4_ptr += v; 
1120 
s>mant4_cnt = 0;

1121 
v = 128;

1122 
break;

1123 
} 
1124 
break;

1125 
case 5: 
1126 
v = sym_quant(c, e, 15);

1127 
break;

1128 
case 14: 
1129 
v = asym_quant(c, e, 14);

1130 
break;

1131 
case 15: 
1132 
v = asym_quant(c, e, 16);

1133 
break;

1134 
default:

1135 
v = asym_quant(c, e, b  1);

1136 
break;

1137 
} 
1138 
qmant[i] = v; 
1139 
} 
1140 
} 
1141  
1142  
1143 
/**

1144 
* Quantize mantissas using coefficients, exponents, and bit allocation pointers.

1145 
*/

1146 
static void quantize_mantissas(AC3EncodeContext *s, 
1147 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1148 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
1149 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1150 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1151 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
1152 
{ 
1153 
int blk, ch;

1154  
1155  
1156 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
1157 
s>mant1_cnt = s>mant2_cnt = s>mant4_cnt = 0;

1158 
s>qmant1_ptr = s>qmant2_ptr = s>qmant4_ptr = NULL;

1159  
1160 
for (ch = 0; ch < s>channels; ch++) { 
1161 
quantize_mantissas_blk_ch(s, mdct_coef[blk][ch], exp_shift[blk][ch], 
1162 
encoded_exp[blk][ch], bap[blk][ch], 
1163 
qmant[blk][ch], s>nb_coefs[ch]); 
1164 
} 
1165 
} 
1166 
} 
1167  
1168  
1169 
/**

1170 
* Write the AC3 frame header to the output bitstream.

1171 
*/

1172 
static void output_frame_header(AC3EncodeContext *s) 
1173 
{ 
1174 
put_bits(&s>pb, 16, 0x0b77); /* frame header */ 
1175 
put_bits(&s>pb, 16, 0); /* crc1: will be filled later */ 
1176 
put_bits(&s>pb, 2, s>bit_alloc.sr_code);

1177 
put_bits(&s>pb, 6, s>frame_size_code + (s>frame_size  s>frame_size_min) / 2); 
1178 
put_bits(&s>pb, 5, s>bitstream_id);

1179 
put_bits(&s>pb, 3, s>bitstream_mode);

1180 
put_bits(&s>pb, 3, s>channel_mode);

1181 
if ((s>channel_mode & 0x01) && s>channel_mode != AC3_CHMODE_MONO) 
1182 
put_bits(&s>pb, 2, 1); /* XXX 4.5 dB */ 
1183 
if (s>channel_mode & 0x04) 
1184 
put_bits(&s>pb, 2, 1); /* XXX 6 dB */ 
1185 
if (s>channel_mode == AC3_CHMODE_STEREO)

1186 
put_bits(&s>pb, 2, 0); /* surround not indicated */ 
1187 
put_bits(&s>pb, 1, s>lfe_on); /* LFE */ 
1188 
put_bits(&s>pb, 5, 31); /* dialog norm: 31 db */ 
1189 
put_bits(&s>pb, 1, 0); /* no compression control word */ 
1190 
put_bits(&s>pb, 1, 0); /* no lang code */ 
1191 
put_bits(&s>pb, 1, 0); /* no audio production info */ 
1192 
put_bits(&s>pb, 1, 0); /* no copyright */ 
1193 
put_bits(&s>pb, 1, 1); /* original bitstream */ 
1194 
put_bits(&s>pb, 1, 0); /* no time code 1 */ 
1195 
put_bits(&s>pb, 1, 0); /* no time code 2 */ 
1196 
put_bits(&s>pb, 1, 0); /* no additional bit stream info */ 
1197 
} 
1198  
1199  
1200 
/**

1201 
* Write one audio block to the output bitstream.

1202 
*/

1203 
static void output_audio_block(AC3EncodeContext *s, 
1204 
uint8_t exp_strategy[AC3_MAX_CHANNELS], 
1205 
uint8_t num_exp_groups[AC3_MAX_CHANNELS], 
1206 
uint8_t grouped_exp[AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS], 
1207 
uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1208 
uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1209 
int block_num)

1210 
{ 
1211 
int ch, i, baie, rbnd;

1212  
1213 
for (ch = 0; ch < s>fbw_channels; ch++) 
1214 
put_bits(&s>pb, 1, 0); /* no block switching */ 
1215 
for (ch = 0; ch < s>fbw_channels; ch++) 
1216 
put_bits(&s>pb, 1, 1); /* no dither */ 
1217 
put_bits(&s>pb, 1, 0); /* no dynamic range */ 
1218 
if (!block_num) {

1219 
put_bits(&s>pb, 1, 1); /* coupling strategy present */ 
1220 
put_bits(&s>pb, 1, 0); /* no coupling strategy */ 
1221 
} else {

1222 
put_bits(&s>pb, 1, 0); /* no new coupling strategy */ 
1223 
} 
1224  
1225 
if (s>channel_mode == AC3_CHMODE_STEREO) {

1226 
if (!block_num) {

1227 
/* first block must define rematrixing (rematstr) */

1228 
put_bits(&s>pb, 1, 1); 
1229  
1230 
/* dummy rematrixing rematflg(1:4)=0 */

1231 
for (rbnd = 0; rbnd < 4; rbnd++) 
1232 
put_bits(&s>pb, 1, 0); 
1233 
} else {

1234 
/* no matrixing (but should be used in the future) */

1235 
put_bits(&s>pb, 1, 0); 
1236 
} 
1237 
} 
1238  
1239 
/* exponent strategy */

1240 
for (ch = 0; ch < s>fbw_channels; ch++) 
1241 
put_bits(&s>pb, 2, exp_strategy[ch]);

1242  
1243 
if (s>lfe_on)

1244 
put_bits(&s>pb, 1, exp_strategy[s>lfe_channel]);

1245  
1246 
/* bandwidth */

1247 
for (ch = 0; ch < s>fbw_channels; ch++) { 
1248 
if (exp_strategy[ch] != EXP_REUSE)

1249 
put_bits(&s>pb, 6, s>bandwidth_code[ch]);

1250 
} 
1251  
1252 
/* exponents */

1253 
for (ch = 0; ch < s>channels; ch++) { 
1254 
if (exp_strategy[ch] == EXP_REUSE)

1255 
continue;

1256  
1257 
/* first exponent */

1258 
put_bits(&s>pb, 4, grouped_exp[ch][0]); 
1259  
1260 
/* next ones are deltaencoded and grouped */

1261 
for (i = 1; i <= num_exp_groups[ch]; i++) 
1262 
put_bits(&s>pb, 7, grouped_exp[ch][i]);

1263  
1264 
if (ch != s>lfe_channel)

1265 
put_bits(&s>pb, 2, 0); /* no gain range info */ 
1266 
} 
1267  
1268 
/* bit allocation info */

1269 
baie = (block_num == 0);

1270 
put_bits(&s>pb, 1, baie);

1271 
if (baie) {

1272 
put_bits(&s>pb, 2, s>slow_decay_code);

1273 
put_bits(&s>pb, 2, s>fast_decay_code);

1274 
put_bits(&s>pb, 2, s>slow_gain_code);

1275 
put_bits(&s>pb, 2, s>db_per_bit_code);

1276 
put_bits(&s>pb, 3, s>floor_code);

1277 
} 
1278  
1279 
/* snr offset */

1280 
put_bits(&s>pb, 1, baie);

1281 
if (baie) {

1282 
put_bits(&s>pb, 6, s>coarse_snr_offset);

1283 
for (ch = 0; ch < s>channels; ch++) { 
1284 
put_bits(&s>pb, 4, s>fine_snr_offset[ch]);

1285 
put_bits(&s>pb, 3, s>fast_gain_code[ch]);

1286 
} 
1287 
} 
1288  
1289 
put_bits(&s>pb, 1, 0); /* no delta bit allocation */ 
1290 
put_bits(&s>pb, 1, 0); /* no data to skip */ 
1291  
1292 
/* mantissa encoding */

1293 
for (ch = 0; ch < s>channels; ch++) { 
1294 
int b, q;

1295  
1296 
for (i = 0; i < s>nb_coefs[ch]; i++) { 
1297 
q = qmant[ch][i]; 
1298 
b = bap[ch][i]; 
1299 
switch (b) {

1300 
case 0: break; 
1301 
case 1: if (q != 128) put_bits(&s>pb, 5, q); break; 
1302 
case 2: if (q != 128) put_bits(&s>pb, 7, q); break; 
1303 
case 3: put_bits(&s>pb, 3, q); break; 
1304 
case 4: if (q != 128) put_bits(&s>pb, 7, q); break; 
1305 
case 14: put_bits(&s>pb, 14, q); break; 
1306 
case 15: put_bits(&s>pb, 16, q); break; 
1307 
default: put_bits(&s>pb, b1, q); break; 
1308 
} 
1309 
} 
1310 
} 
1311 
} 
1312  
1313  
1314 
/** CRC16 Polynomial */

1315 
#define CRC16_POLY ((1 << 0)  (1 << 2)  (1 << 15)  (1 << 16)) 
1316  
1317  
1318 
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly) 
1319 
{ 
1320 
unsigned int c; 
1321  
1322 
c = 0;

1323 
while (a) {

1324 
if (a & 1) 
1325 
c ^= b; 
1326 
a = a >> 1;

1327 
b = b << 1;

1328 
if (b & (1 << 16)) 
1329 
b ^= poly; 
1330 
} 
1331 
return c;

1332 
} 
1333  
1334  
1335 
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) 
1336 
{ 
1337 
unsigned int r; 
1338 
r = 1;

1339 
while (n) {

1340 
if (n & 1) 
1341 
r = mul_poly(r, a, poly); 
1342 
a = mul_poly(a, a, poly); 
1343 
n >>= 1;

1344 
} 
1345 
return r;

1346 
} 
1347  
1348  
1349 
/**

1350 
* Fill the end of the frame with 0's and compute the two CRCs.

1351 
*/

1352 
static void output_frame_end(AC3EncodeContext *s) 
1353 
{ 
1354 
int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv;

1355 
uint8_t *frame; 
1356  
1357 
frame_size = s>frame_size; /* frame size in words */

1358 
/* align to 8 bits */

1359 
flush_put_bits(&s>pb); 
1360 
/* add zero bytes to reach the frame size */

1361 
frame = s>pb.buf; 
1362 
pad_bytes = s>frame_size  (put_bits_ptr(&s>pb)  frame)  2;

1363 
assert(pad_bytes >= 0);

1364 
if (pad_bytes > 0) 
1365 
memset(put_bits_ptr(&s>pb), 0, pad_bytes);

1366  
1367 
/* Now we must compute both crcs : this is not so easy for crc1

1368 
because it is at the beginning of the data... */

1369 
frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1; 
1370  
1371 
crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,

1372 
frame + 4, frame_size_58  4)); 
1373  
1374 
/* XXX: could precompute crc_inv */

1375 
crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58)  16, CRC16_POLY); 
1376 
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); 
1377 
AV_WB16(frame + 2, crc1);

1378  
1379 
crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,

1380 
frame + frame_size_58, 
1381 
frame_size  frame_size_58  2));

1382 
AV_WB16(frame + frame_size  2, crc2);

1383 
} 
1384  
1385  
1386 
/**

1387 
* Write the frame to the output bitstream.

1388 
*/

1389 
static void output_frame(AC3EncodeContext *s, 
1390 
unsigned char *frame, 
1391 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
1392 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
1393 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS], 
1394 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1395 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
1396 
{ 
1397 
int blk;

1398  
1399 
init_put_bits(&s>pb, frame, AC3_MAX_CODED_FRAME_SIZE); 
1400  
1401 
output_frame_header(s); 
1402  
1403 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
1404 
output_audio_block(s, exp_strategy[blk], num_exp_groups[blk], 
1405 
grouped_exp[blk], bap[blk], qmant[blk], blk); 
1406 
} 
1407  
1408 
output_frame_end(s); 
1409 
} 
1410  
1411  
1412 
/**

1413 
* Encode a single AC3 frame.

1414 
*/

1415 
static int ac3_encode_frame(AVCodecContext *avctx, 
1416 
unsigned char *frame, int buf_size, void *data) 
1417 
{ 
1418 
AC3EncodeContext *s = avctx>priv_data; 
1419 
const int16_t *samples = data;

1420 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE]; 
1421 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1422 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1423 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 
1424 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1425 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 
1426 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]; 
1427 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1428 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 
1429 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1430 
int frame_bits;

1431 
int ret;

1432  
1433 
if (s>bit_alloc.sr_code == 1) 
1434 
adjust_frame_size(s); 
1435  
1436 
deinterleave_input_samples(s, samples, planar_samples); 
1437  
1438 
apply_mdct(s, planar_samples, exp_shift, mdct_coef); 
1439  
1440 
frame_bits = process_exponents(s, mdct_coef, exp_shift, exp, exp_strategy, 
1441 
encoded_exp, num_exp_groups, grouped_exp); 
1442  
1443 
ret = compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); 
1444 
if (ret) {

1445 
av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");

1446 
return ret;

1447 
} 
1448  
1449 
quantize_mantissas(s, mdct_coef, exp_shift, encoded_exp, bap, qmant); 
1450  
1451 
output_frame(s, frame, exp_strategy, num_exp_groups, grouped_exp, bap, qmant); 
1452  
1453 
return s>frame_size;

1454 
} 
1455  
1456  
1457 
/**

1458 
* Finalize encoding and free any memory allocated by the encoder.

1459 
*/

1460 
static av_cold int ac3_encode_close(AVCodecContext *avctx) 
1461 
{ 
1462 
av_freep(&avctx>coded_frame); 
1463 
return 0; 
1464 
} 
1465  
1466  
1467 
/**

1468 
* Set channel information during initialization.

1469 
*/

1470 
static av_cold int set_channel_info(AC3EncodeContext *s, int channels, 
1471 
int64_t *channel_layout) 
1472 
{ 
1473 
int ch_layout;

1474  
1475 
if (channels < 1  channels > AC3_MAX_CHANNELS) 
1476 
return AVERROR(EINVAL);

1477 
if ((uint64_t)*channel_layout > 0x7FF) 
1478 
return AVERROR(EINVAL);

1479 
ch_layout = *channel_layout; 
1480 
if (!ch_layout)

1481 
ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);

1482 
if (av_get_channel_layout_nb_channels(ch_layout) != channels)

1483 
return AVERROR(EINVAL);

1484  
1485 
s>lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY); 
1486 
s>channels = channels; 
1487 
s>fbw_channels = channels  s>lfe_on; 
1488 
s>lfe_channel = s>lfe_on ? s>fbw_channels : 1;

1489 
if (s>lfe_on)

1490 
ch_layout = AV_CH_LOW_FREQUENCY; 
1491  
1492 
switch (ch_layout) {

1493 
case AV_CH_LAYOUT_MONO: s>channel_mode = AC3_CHMODE_MONO; break; 
1494 
case AV_CH_LAYOUT_STEREO: s>channel_mode = AC3_CHMODE_STEREO; break; 
1495 
case AV_CH_LAYOUT_SURROUND: s>channel_mode = AC3_CHMODE_3F; break; 
1496 
case AV_CH_LAYOUT_2_1: s>channel_mode = AC3_CHMODE_2F1R; break; 
1497 
case AV_CH_LAYOUT_4POINT0: s>channel_mode = AC3_CHMODE_3F1R; break; 
1498 
case AV_CH_LAYOUT_QUAD:

1499 
case AV_CH_LAYOUT_2_2: s>channel_mode = AC3_CHMODE_2F2R; break; 
1500 
case AV_CH_LAYOUT_5POINT0:

1501 
case AV_CH_LAYOUT_5POINT0_BACK: s>channel_mode = AC3_CHMODE_3F2R; break; 
1502 
default:

1503 
return AVERROR(EINVAL);

1504 
} 
1505  
1506 
s>channel_map = ff_ac3_enc_channel_map[s>channel_mode][s>lfe_on]; 
1507 
*channel_layout = ch_layout; 
1508 
if (s>lfe_on)

1509 
*channel_layout = AV_CH_LOW_FREQUENCY; 
1510  
1511 
return 0; 
1512 
} 
1513  
1514  
1515 
static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s) 
1516 
{ 
1517 
int i, ret;

1518  
1519 
/* validate channel layout */

1520 
if (!avctx>channel_layout) {

1521 
av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "

1522 
"encoder will guess the layout, but it "

1523 
"might be incorrect.\n");

1524 
} 
1525 
ret = set_channel_info(s, avctx>channels, &avctx>channel_layout); 
1526 
if (ret) {

1527 
av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n");

1528 
return ret;

1529 
} 
1530  
1531 
/* validate sample rate */

1532 
for (i = 0; i < 9; i++) { 
1533 
if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx>sample_rate) 
1534 
break;

1535 
} 
1536 
if (i == 9) { 
1537 
av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");

1538 
return AVERROR(EINVAL);

1539 
} 
1540 
s>sample_rate = avctx>sample_rate; 
1541 
s>bit_alloc.sr_shift = i % 3;

1542 
s>bit_alloc.sr_code = i / 3;

1543  
1544 
/* validate bit rate */

1545 
for (i = 0; i < 19; i++) { 
1546 
if ((ff_ac3_bitrate_tab[i] >> s>bit_alloc.sr_shift)*1000 == avctx>bit_rate) 
1547 
break;

1548 
} 
1549 
if (i == 19) { 
1550 
av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");

1551 
return AVERROR(EINVAL);

1552 
} 
1553 
s>bit_rate = avctx>bit_rate; 
1554 
s>frame_size_code = i << 1;

1555  
1556 
return 0; 
1557 
} 
1558  
1559  
1560 
/**

1561 
* Set bandwidth for all channels.

1562 
* The user can optionally supply a cutoff frequency. Otherwise an appropriate

1563 
* default value will be used.

1564 
*/

1565 
static av_cold void set_bandwidth(AC3EncodeContext *s, int cutoff) 
1566 
{ 
1567 
int ch, bw_code;

1568  
1569 
if (cutoff) {

1570 
/* calculate bandwidth based on userspecified cutoff frequency */

1571 
int fbw_coeffs;

1572 
cutoff = av_clip(cutoff, 1, s>sample_rate >> 1); 
1573 
fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s>sample_rate;

1574 
bw_code = av_clip((fbw_coeffs  73) / 3, 0, 60); 
1575 
} else {

1576 
/* use default bandwidth setting */

1577 
/* XXX: should compute the bandwidth according to the frame

1578 
size, so that we avoid annoying high frequency artifacts */

1579 
bw_code = 50;

1580 
} 
1581  
1582 
/* set number of coefficients for each channel */

1583 
for (ch = 0; ch < s>fbw_channels; ch++) { 
1584 
s>bandwidth_code[ch] = bw_code; 
1585 
s>nb_coefs[ch] = bw_code * 3 + 73; 
1586 
} 
1587 
if (s>lfe_on)

1588 
s>nb_coefs[s>lfe_channel] = 7; /* LFE channel always has 7 coefs */ 
1589 
} 
1590  
1591  
1592 
/**

1593 
* Initialize the encoder.

1594 
*/

1595 
static av_cold int ac3_encode_init(AVCodecContext *avctx) 
1596 
{ 
1597 
AC3EncodeContext *s = avctx>priv_data; 
1598 
int ret;

1599  
1600 
avctx>frame_size = AC3_FRAME_SIZE; 
1601  
1602 
ac3_common_init(); 
1603  
1604 
ret = validate_options(avctx, s); 
1605 
if (ret)

1606 
return ret;

1607  
1608 
s>bitstream_id = 8 + s>bit_alloc.sr_shift;

1609 
s>bitstream_mode = 0; /* complete main audio service */ 
1610  
1611 
s>frame_size_min = 2 * ff_ac3_frame_size_tab[s>frame_size_code][s>bit_alloc.sr_code];

1612 
s>bits_written = 0;

1613 
s>samples_written = 0;

1614 
s>frame_size = s>frame_size_min; 
1615  
1616 
set_bandwidth(s, avctx>cutoff); 
1617  
1618 
/* initial snr offset */

1619 
s>coarse_snr_offset = 40;

1620  
1621 
mdct_init(9);

1622  
1623 
avctx>coded_frame= avcodec_alloc_frame(); 
1624 
avctx>coded_frame>key_frame= 1;

1625  
1626 
return 0; 
1627 
} 
1628  
1629  
1630 
#ifdef TEST

1631 
/*************************************************************************/

1632 
/* TEST */

1633  
1634 
#include "libavutil/lfg.h" 
1635  
1636 
#define FN (MDCT_SAMPLES/4) 
1637  
1638  
1639 
static void fft_test(AVLFG *lfg) 
1640 
{ 
1641 
IComplex in[FN], in1[FN]; 
1642 
int k, n, i;

1643 
float sum_re, sum_im, a;

1644  
1645 
for (i = 0; i < FN; i++) { 
1646 
in[i].re = av_lfg_get(lfg) % 65535  32767; 
1647 
in[i].im = av_lfg_get(lfg) % 65535  32767; 
1648 
in1[i] = in[i]; 
1649 
} 
1650 
fft(in, 7);

1651  
1652 
/* do it by hand */

1653 
for (k = 0; k < FN; k++) { 
1654 
sum_re = 0;

1655 
sum_im = 0;

1656 
for (n = 0; n < FN; n++) { 
1657 
a = 2 * M_PI * (n * k) / FN;

1658 
sum_re += in1[n].re * cos(a)  in1[n].im * sin(a); 
1659 
sum_im += in1[n].re * sin(a) + in1[n].im * cos(a); 
1660 
} 
1661 
av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n", 
1662 
k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); 
1663 
} 
1664 
} 
1665  
1666  
1667 
static void mdct_test(AVLFG *lfg) 
1668 
{ 
1669 
int16_t input[MDCT_SAMPLES]; 
1670 
int32_t output[AC3_MAX_COEFS]; 
1671 
float input1[MDCT_SAMPLES];

1672 
float output1[AC3_MAX_COEFS];

1673 
float s, a, err, e, emax;

1674 
int i, k, n;

1675  
1676 
for (i = 0; i < MDCT_SAMPLES; i++) { 
1677 
input[i] = (av_lfg_get(lfg) % 65535  32767) * 9 / 10; 
1678 
input1[i] = input[i]; 
1679 
} 
1680  
1681 
mdct512(output, input); 
1682  
1683 
/* do it by hand */

1684 
for (k = 0; k < AC3_MAX_COEFS; k++) { 
1685 
s = 0;

1686 
for (n = 0; n < MDCT_SAMPLES; n++) { 
1687 
a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES)); 
1688 
s += input1[n] * cos(a); 
1689 
} 
1690 
output1[k] = 2 * s / MDCT_SAMPLES;

1691 
} 
1692  
1693 
err = 0;

1694 
emax = 0;

1695 
for (i = 0; i < AC3_MAX_COEFS; i++) { 
1696 
av_log(NULL, AV_LOG_DEBUG, "%3d: %7d %7.0f\n", i, output[i], output1[i]); 
1697 
e = output[i]  output1[i]; 
1698 
if (e > emax)

1699 
emax = e; 
1700 
err += e * e; 
1701 
} 
1702 
av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax); 
1703 
} 
1704  
1705  
1706 
int main(void) 
1707 
{ 
1708 
AVLFG lfg; 
1709  
1710 
av_log_set_level(AV_LOG_DEBUG); 
1711 
mdct_init(9);

1712  
1713 
fft_test(&lfg); 
1714 
mdct_test(&lfg); 
1715  
1716 
return 0; 
1717 
} 
1718 
#endif /* TEST */ 
1719  
1720  
1721 
AVCodec ac3_encoder = { 
1722 
"ac3",

1723 
AVMEDIA_TYPE_AUDIO, 
1724 
CODEC_ID_AC3, 
1725 
sizeof(AC3EncodeContext),

1726 
ac3_encode_init, 
1727 
ac3_encode_frame, 
1728 
ac3_encode_close, 
1729 
NULL,

1730 
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE}, 
1731 
.long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC3)"),

1732 
.channel_layouts = (const int64_t[]){

1733 
AV_CH_LAYOUT_MONO, 
1734 
AV_CH_LAYOUT_STEREO, 
1735 
AV_CH_LAYOUT_2_1, 
1736 
AV_CH_LAYOUT_SURROUND, 
1737 
AV_CH_LAYOUT_2_2, 
1738 
AV_CH_LAYOUT_QUAD, 
1739 
AV_CH_LAYOUT_4POINT0, 
1740 
AV_CH_LAYOUT_5POINT0, 
1741 
AV_CH_LAYOUT_5POINT0_BACK, 
1742 
(AV_CH_LAYOUT_MONO  AV_CH_LOW_FREQUENCY), 
1743 
(AV_CH_LAYOUT_STEREO  AV_CH_LOW_FREQUENCY), 
1744 
(AV_CH_LAYOUT_2_1  AV_CH_LOW_FREQUENCY), 
1745 
(AV_CH_LAYOUT_SURROUND  AV_CH_LOW_FREQUENCY), 
1746 
(AV_CH_LAYOUT_2_2  AV_CH_LOW_FREQUENCY), 
1747 
(AV_CH_LAYOUT_QUAD  AV_CH_LOW_FREQUENCY), 
1748 
(AV_CH_LAYOUT_4POINT0  AV_CH_LOW_FREQUENCY), 
1749 
AV_CH_LAYOUT_5POINT1, 
1750 
AV_CH_LAYOUT_5POINT1_BACK, 
1751 
0 },

1752 
}; 