ffmpeg / libavcodec / ac3enc.c @ a0d763fc
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 
int frame_bits; ///< all frame bits except exponents and mantissas 
97 
int exponent_bits; ///< number of bits used for exponents 
98  
99 
/* mantissa encoding */

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

102  
103 
int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; ///< last 256 samples from previous frame

104 
} AC3EncodeContext; 
105  
106  
107 
/** MDCT and FFT tables */

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

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

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

117 
*/

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

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

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

126 
s>samples_written += AC3_FRAME_SIZE; 
127 
} 
128  
129  
130 
/**

131 
* Deinterleave input samples.

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

133 
*/

134 
static void deinterleave_input_samples(AC3EncodeContext *s, 
135 
const int16_t *samples,

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

139  
140 
/* deinterleave and remap input samples */

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

143 
int sinc;

144  
145 
/* copy last 256 samples of previous frame to the start of the current frame */

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

147 
AC3_BLOCK_SIZE * sizeof(planar_samples[0][0])); 
148  
149 
/* deinterleave */

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

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

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

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

165 
* Initialize FFT tables.

166 
* @param ln log2(FFT size)

167 
*/

168 
static av_cold void fft_init(int ln) 
169 
{ 
170 
int i, n, n2;

171 
float alpha;

172  
173 
n = 1 << ln;

174 
n2 = n >> 1;

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

185 
* Initialize MDCT tables.

186 
* @param nbits log2(MDCT size)

187 
*/

188 
static av_cold void mdct_init(int nbits) 
189 
{ 
190 
int i, n, n4;

191  
192 
n = 1 << nbits;

193 
n4 = n >> 2;

194  
195 
fft_init(nbits  2);

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

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

207 
{ \ 
208 
int ax, ay, bx, by; \

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

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

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

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

217 
} 
218  
219  
220 
/** Complex multiply */

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

222 
{ \ 
223 
pre = (MUL16(are, bre)  MUL16(aim, bim)) >> 15; \

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

225 
} 
226  
227  
228 
/**

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

230 
* @param z complex input/output samples

231 
* @param ln log2(FFT size)

232 
*/

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

236 
int nblocks, nloops;

237 
register IComplex *p,*q;

238 
int tmp_re, tmp_im;

239  
240 
np = 1 << ln;

241  
242 
/* reverse */

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

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

250  
251 
p = &z[0];

252 
j = np >> 1;

253 
do {

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

257 
} while (j);

258  
259 
/* pass 1 */

260  
261 
p = &z[0];

262 
j = np >> 2;

263 
do {

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

269 
} while (j);

270  
271 
/* pass 2 .. ln1 */

272  
273 
nblocks = np >> 3;

274 
nloops = 1 << 2; 
275 
np2 = np >> 1;

276 
do {

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

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

295 
nloops = nloops << 1;

296 
} while (nblocks);

297 
} 
298  
299  
300 
/**

301 
* Calculate a 512point MDCT

302 
* @param out 256 output frequency coefficients

303 
* @param in 512 windowed input audio samples

304 
*/

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

308 
int16_t rot[MDCT_SAMPLES]; 
309 
IComplex x[MDCT_SAMPLES/4];

310  
311 
/* shift to simplify computations */

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

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

316  
317 
/* pre rotation */

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

325  
326 
/* post rotation */

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

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

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

339 
*/

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

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

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

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

355 
* @param tab input array

356 
* @param n number of values in the array

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

358 
*/

359 
static int log2_tab(int16_t *tab, int n) 
360 
{ 
361 
int i, v;

362  
363 
v = 0;

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

368 
} 
369  
370  
371 
/**

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

373 
* @param tab input array

374 
* @param n number of values in the array

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

376 
*/

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

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

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

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

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

396 
*

397 
* @return exponent shift

398 
*/

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

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

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

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

412 
* loss due to fixedpoint calculations.

413 
*/

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

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

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

437 
* Extract exponents from the MDCT coefficients.

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

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

440 
*/

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

447  
448 
/* extract exponents */

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

452 
for (i = 0; i < AC3_MAX_COEFS; i++) { 
453 
int e;

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

455 
if (v == 0) 
456 
e = 24;

457 
else {

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

459 
if (e >= 24) { 
460 
e = 24;

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

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

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

473 
*/

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

477 
sum = 0;

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

481 
} 
482  
483  
484 
/**

485 
* Exponent Difference Threshold.

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

487 
*/

488 
#define EXP_DIFF_THRESHOLD 1000 
489  
490  
491 
/**

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

493 
*/

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

497 
int exp_diff;

498  
499 
/* estimate if the exponent variation & decide if they should be

500 
reused in the next frame */

501 
exp_strategy[0] = EXP_NEW;

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

504 
if (exp_diff > EXP_DIFF_THRESHOLD)

505 
exp_strategy[blk] = EXP_NEW; 
506 
else

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

511 
recoded, we use a coarse encoding */

512 
blk = 0;

513 
while (blk < AC3_MAX_BLOCKS) {

514 
blk1 = blk + 1;

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

516 
blk1++; 
517 
switch (blk1  blk) {

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

529 
* Calculate exponent strategies for all channels.

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

531 
*/

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

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

552 
ch = s>lfe_channel; 
553 
exp_strategy[0][ch] = EXP_D15;

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

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

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

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

564 
*/

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

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

570 
exp[i] = exp1[i]; 
571 
} 
572 
} 
573  
574  
575 
/**

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

577 
*/

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

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

589  
590 
/* for each group, compute the minimum exponent */

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

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

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

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

609 
differentially encoded */

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

616 
encoded_exp[0] = exp1[0]; 
617 
k = 1;

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

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

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

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

630 
* encoded.

631 
*/

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

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

642 
blk = 0;

643 
while (blk < AC3_MAX_BLOCKS) {

644 
blk1 = blk + 1;

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

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

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

657 
} 
658 
blk = blk1; 
659 
} 
660 
} 
661 
} 
662  
663  
664 
/**

665 
* Group exponents.

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

667 
* varies depending on exponent strategy and bandwidth.

668 
*/

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

676 
int group_size, bit_count;

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

679 
int exp0, exp1;

680  
681 
bit_count = 0;

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

685 
num_exp_groups[blk][ch] = 0;

686 
continue;

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

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

695  
696 
/* remaining exponents are delta encoded */

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

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

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

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

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

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

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

713  
714 
grouped_exp[blk][ch][i] = ((delta0 * 5 + delta1) * 5) + delta2; 
715 
} 
716 
} 
717 
} 
718  
719 
s>exponent_bits = bit_count; 
720 
} 
721  
722  
723 
/**

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

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

726 
* and encode final exponents.

727 
*/

728 
static void 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 
group_exponents(s, encoded_exp, exp_strategy, num_exp_groups, grouped_exp); 
744 
} 
745  
746  
747 
/**

748 
* Initialize bit allocation.

749 
* Set default parameter codes and calculate parameter values.

750 
*/

751 
static void bit_alloc_init(AC3EncodeContext *s) 
752 
{ 
753 
int ch;

754  
755 
/* init default parameters */

756 
s>slow_decay_code = 2;

757 
s>fast_decay_code = 1;

758 
s>slow_gain_code = 1;

759 
s>db_per_bit_code = 2;

760 
s>floor_code = 4;

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

763  
764 
/* initial snr offset */

765 
s>coarse_snr_offset = 40;

766  
767 
/* compute real values */

768 
/* currently none of these values change during encoding, so we can just

769 
set them once at initialization */

770 
s>bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s>slow_decay_code] >> s>bit_alloc.sr_shift; 
771 
s>bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s>fast_decay_code] >> s>bit_alloc.sr_shift; 
772 
s>bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s>slow_gain_code]; 
773 
s>bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s>db_per_bit_code]; 
774 
s>bit_alloc.floor = ff_ac3_floor_tab[s>floor_code]; 
775 
} 
776  
777  
778 
/**

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

780 
*/

781 
static void count_frame_bits(AC3EncodeContext *s, 
782 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]) 
783 
{ 
784 
static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; 
785 
int blk, ch;

786 
int frame_bits;

787  
788 
/* header size */

789 
frame_bits = 65;

790 
frame_bits += frame_bits_inc[s>channel_mode]; 
791  
792 
/* audio blocks */

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

796 
frame_bits++; /* rematstr */

797 
if (!blk)

798 
frame_bits += 4;

799 
} 
800 
frame_bits += 2 * s>fbw_channels; /* chexpstr[2] * c */ 
801 
if (s>lfe_on)

802 
frame_bits++; /* lfeexpstr */

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

805 
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ 
806 
} 
807 
frame_bits++; /* baie */

808 
frame_bits++; /* snr */

809 
frame_bits += 2; /* delta / skip */ 
810 
} 
811 
frame_bits++; /* cplinu for block 0 */

812 
/* bit alloc info */

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

814 
/* csnroffset[6] */

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

816 
frame_bits += 2*4 + 3 + 6 + s>channels * (4 + 3); 
817  
818 
/* auxdatae, crcrsv */

819 
frame_bits += 2;

820  
821 
/* CRC */

822 
frame_bits += 16;

823  
824 
s>frame_bits = frame_bits; 
825 
} 
826  
827  
828 
/**

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

830 
*/

831 
static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) 
832 
{ 
833 
int bits, mant, i;

834  
835 
bits = 0;

836 
for (i = 0; i < nb_coefs; i++) { 
837 
mant = m[i]; 
838 
switch (mant) {

839 
case 0: 
840 
/* nothing */

841 
break;

842 
case 1: 
843 
/* 3 mantissa in 5 bits */

844 
if (s>mant1_cnt == 0) 
845 
bits += 5;

846 
if (++s>mant1_cnt == 3) 
847 
s>mant1_cnt = 0;

848 
break;

849 
case 2: 
850 
/* 3 mantissa in 7 bits */

851 
if (s>mant2_cnt == 0) 
852 
bits += 7;

853 
if (++s>mant2_cnt == 3) 
854 
s>mant2_cnt = 0;

855 
break;

856 
case 3: 
857 
bits += 3;

858 
break;

859 
case 4: 
860 
/* 2 mantissa in 7 bits */

861 
if (s>mant4_cnt == 0) 
862 
bits += 7;

863 
if (++s>mant4_cnt == 2) 
864 
s>mant4_cnt = 0;

865 
break;

866 
case 14: 
867 
bits += 14;

868 
break;

869 
case 15: 
870 
bits += 16;

871 
break;

872 
default:

873 
bits += mant  1;

874 
break;

875 
} 
876 
} 
877 
return bits;

878 
} 
879  
880  
881 
/**

882 
* Calculate masking curve based on the final exponents.

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

884 
*/

885 
static void bit_alloc_masking(AC3EncodeContext *s, 
886 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
887 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
888 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
889 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]) 
890 
{ 
891 
int blk, ch;

892 
int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 
893  
894 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
895 
for (ch = 0; ch < s>channels; ch++) { 
896 
if(exp_strategy[blk][ch] == EXP_REUSE) {

897 
memcpy(psd[blk][ch], psd[blk1][ch], AC3_MAX_COEFS*sizeof(psd[0][0][0])); 
898 
memcpy(mask[blk][ch], mask[blk1][ch], AC3_CRITICAL_BANDS*sizeof(mask[0][0][0])); 
899 
} else {

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

901 
s>nb_coefs[ch], 
902 
psd[blk][ch], band_psd[blk][ch]); 
903 
ff_ac3_bit_alloc_calc_mask(&s>bit_alloc, band_psd[blk][ch], 
904 
0, s>nb_coefs[ch],

905 
ff_ac3_fast_gain_tab[s>fast_gain_code[ch]], 
906 
ch == s>lfe_channel, 
907 
DBA_NONE, 0, NULL, NULL, NULL, 
908 
mask[blk][ch]); 
909 
} 
910 
} 
911 
} 
912 
} 
913  
914  
915 
/**

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

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

918 
* the quantization of each mantissa.

919 
* @return the number of bits needed for mantissas if the given SNR offset is

920 
* is used.

921 
*/

922 
static int bit_alloc(AC3EncodeContext *s, 
923 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS], 
924 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
925 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
926 
int snr_offset)

927 
{ 
928 
int blk, ch;

929 
int mantissa_bits;

930  
931 
snr_offset = (snr_offset  240) << 2; 
932  
933 
mantissa_bits = 0;

934 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
935 
s>mant1_cnt = 0;

936 
s>mant2_cnt = 0;

937 
s>mant4_cnt = 0;

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

940 
s>nb_coefs[ch], snr_offset, 
941 
s>bit_alloc.floor, ff_ac3_bap_tab, 
942 
bap[blk][ch]); 
943 
mantissa_bits += compute_mantissa_size(s, bap[blk][ch], s>nb_coefs[ch]); 
944 
} 
945 
} 
946 
return mantissa_bits;

947 
} 
948  
949  
950 
/**

951 
* Perform bit allocation search.

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

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

954 
* used to quantize the mantissas.

955 
*/

956 
static int compute_bit_allocation(AC3EncodeContext *s, 
957 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
958 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
959 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]) 
960 
{ 
961 
int ch;

962 
int bits_left;

963 
int snr_offset;

964 
uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
965 
int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
966 
int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_CRITICAL_BANDS]; 
967  
968 
/* count frame bits other than exponents and mantissas */

969 
count_frame_bits(s, exp_strategy); 
970  
971 
/* calculate psd and masking curve before doing bit allocation */

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

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

976  
977 
bits_left = 8 * s>frame_size  (s>frame_bits + s>exponent_bits);

978 
snr_offset = s>coarse_snr_offset << 4;

979 
while (snr_offset >= 0 && 
980 
bit_alloc(s, mask, psd, bap, snr_offset) > bits_left) 
981 
snr_offset = 64;

982 
if (snr_offset < 0) { 
983 
return AVERROR(EINVAL);

984 
} 
985 
while (snr_offset + 64 <= 1023 && 
986 
bit_alloc(s, mask, psd, bap1, 
987 
snr_offset + 64) <= bits_left) {

988 
snr_offset += 64;

989 
memcpy(bap, bap1, sizeof(bap1));

990 
} 
991 
while (snr_offset + 16 <= 1023 && 
992 
bit_alloc(s, mask, psd, bap1, snr_offset + 16) <= bits_left) {

993 
snr_offset += 16;

994 
memcpy(bap, bap1, sizeof(bap1));

995 
} 
996 
while (snr_offset + 4 <= 1023 && 
997 
bit_alloc(s, mask, psd, bap1, 
998 
snr_offset + 4) <= bits_left) {

999 
snr_offset += 4;

1000 
memcpy(bap, bap1, sizeof(bap1));

1001 
} 
1002 
while (snr_offset + 1 <= 1023 && 
1003 
bit_alloc(s, mask, psd, bap1, 
1004 
snr_offset + 1) <= bits_left) {

1005 
snr_offset++; 
1006 
memcpy(bap, bap1, sizeof(bap1));

1007 
} 
1008  
1009 
s>coarse_snr_offset = snr_offset >> 4;

1010 
for (ch = 0; ch < s>channels; ch++) 
1011 
s>fine_snr_offset[ch] = snr_offset & 0xF;

1012  
1013 
return 0; 
1014 
} 
1015  
1016  
1017 
/**

1018 
* Symmetric quantization on 'levels' levels.

1019 
*/

1020 
static inline int sym_quant(int c, int e, int levels) 
1021 
{ 
1022 
int v;

1023  
1024 
if (c >= 0) { 
1025 
v = (levels * (c << e)) >> 24;

1026 
v = (v + 1) >> 1; 
1027 
v = (levels >> 1) + v;

1028 
} else {

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

1030 
v = (v + 1) >> 1; 
1031 
v = (levels >> 1)  v;

1032 
} 
1033 
assert (v >= 0 && v < levels);

1034 
return v;

1035 
} 
1036  
1037  
1038 
/**

1039 
* Asymmetric quantization on 2^qbits levels.

1040 
*/

1041 
static inline int asym_quant(int c, int e, int qbits) 
1042 
{ 
1043 
int lshift, m, v;

1044  
1045 
lshift = e + qbits  24;

1046 
if (lshift >= 0) 
1047 
v = c << lshift; 
1048 
else

1049 
v = c >> (lshift); 
1050 
/* rounding */

1051 
v = (v + 1) >> 1; 
1052 
m = (1 << (qbits1)); 
1053 
if (v >= m)

1054 
v = m  1;

1055 
assert(v >= m); 
1056 
return v & ((1 << qbits)1); 
1057 
} 
1058  
1059  
1060 
/**

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

1062 
*/

1063 
static void quantize_mantissas_blk_ch(AC3EncodeContext *s, 
1064 
int32_t *mdct_coef, int8_t exp_shift, 
1065 
uint8_t *encoded_exp, uint8_t *bap, 
1066 
uint16_t *qmant, int n)

1067 
{ 
1068 
int i;

1069  
1070 
for (i = 0; i < n; i++) { 
1071 
int v;

1072 
int c = mdct_coef[i];

1073 
int e = encoded_exp[i]  exp_shift;

1074 
int b = bap[i];

1075 
switch (b) {

1076 
case 0: 
1077 
v = 0;

1078 
break;

1079 
case 1: 
1080 
v = sym_quant(c, e, 3);

1081 
switch (s>mant1_cnt) {

1082 
case 0: 
1083 
s>qmant1_ptr = &qmant[i]; 
1084 
v = 9 * v;

1085 
s>mant1_cnt = 1;

1086 
break;

1087 
case 1: 
1088 
*s>qmant1_ptr += 3 * v;

1089 
s>mant1_cnt = 2;

1090 
v = 128;

1091 
break;

1092 
default:

1093 
*s>qmant1_ptr += v; 
1094 
s>mant1_cnt = 0;

1095 
v = 128;

1096 
break;

1097 
} 
1098 
break;

1099 
case 2: 
1100 
v = sym_quant(c, e, 5);

1101 
switch (s>mant2_cnt) {

1102 
case 0: 
1103 
s>qmant2_ptr = &qmant[i]; 
1104 
v = 25 * v;

1105 
s>mant2_cnt = 1;

1106 
break;

1107 
case 1: 
1108 
*s>qmant2_ptr += 5 * v;

1109 
s>mant2_cnt = 2;

1110 
v = 128;

1111 
break;

1112 
default:

1113 
*s>qmant2_ptr += v; 
1114 
s>mant2_cnt = 0;

1115 
v = 128;

1116 
break;

1117 
} 
1118 
break;

1119 
case 3: 
1120 
v = sym_quant(c, e, 7);

1121 
break;

1122 
case 4: 
1123 
v = sym_quant(c, e, 11);

1124 
switch (s>mant4_cnt) {

1125 
case 0: 
1126 
s>qmant4_ptr = &qmant[i]; 
1127 
v = 11 * v;

1128 
s>mant4_cnt = 1;

1129 
break;

1130 
default:

1131 
*s>qmant4_ptr += v; 
1132 
s>mant4_cnt = 0;

1133 
v = 128;

1134 
break;

1135 
} 
1136 
break;

1137 
case 5: 
1138 
v = sym_quant(c, e, 15);

1139 
break;

1140 
case 14: 
1141 
v = asym_quant(c, e, 14);

1142 
break;

1143 
case 15: 
1144 
v = asym_quant(c, e, 16);

1145 
break;

1146 
default:

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

1148 
break;

1149 
} 
1150 
qmant[i] = v; 
1151 
} 
1152 
} 
1153  
1154  
1155 
/**

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

1157 
*/

1158 
static void quantize_mantissas(AC3EncodeContext *s, 
1159 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1160 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
1161 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1162 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1163 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
1164 
{ 
1165 
int blk, ch;

1166  
1167  
1168 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
1169 
s>mant1_cnt = s>mant2_cnt = s>mant4_cnt = 0;

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

1171  
1172 
for (ch = 0; ch < s>channels; ch++) { 
1173 
quantize_mantissas_blk_ch(s, mdct_coef[blk][ch], exp_shift[blk][ch], 
1174 
encoded_exp[blk][ch], bap[blk][ch], 
1175 
qmant[blk][ch], s>nb_coefs[ch]); 
1176 
} 
1177 
} 
1178 
} 
1179  
1180  
1181 
/**

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

1183 
*/

1184 
static void output_frame_header(AC3EncodeContext *s) 
1185 
{ 
1186 
put_bits(&s>pb, 16, 0x0b77); /* frame header */ 
1187 
put_bits(&s>pb, 16, 0); /* crc1: will be filled later */ 
1188 
put_bits(&s>pb, 2, s>bit_alloc.sr_code);

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

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

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

1193 
if ((s>channel_mode & 0x01) && s>channel_mode != AC3_CHMODE_MONO) 
1194 
put_bits(&s>pb, 2, 1); /* XXX 4.5 dB */ 
1195 
if (s>channel_mode & 0x04) 
1196 
put_bits(&s>pb, 2, 1); /* XXX 6 dB */ 
1197 
if (s>channel_mode == AC3_CHMODE_STEREO)

1198 
put_bits(&s>pb, 2, 0); /* surround not indicated */ 
1199 
put_bits(&s>pb, 1, s>lfe_on); /* LFE */ 
1200 
put_bits(&s>pb, 5, 31); /* dialog norm: 31 db */ 
1201 
put_bits(&s>pb, 1, 0); /* no compression control word */ 
1202 
put_bits(&s>pb, 1, 0); /* no lang code */ 
1203 
put_bits(&s>pb, 1, 0); /* no audio production info */ 
1204 
put_bits(&s>pb, 1, 0); /* no copyright */ 
1205 
put_bits(&s>pb, 1, 1); /* original bitstream */ 
1206 
put_bits(&s>pb, 1, 0); /* no time code 1 */ 
1207 
put_bits(&s>pb, 1, 0); /* no time code 2 */ 
1208 
put_bits(&s>pb, 1, 0); /* no additional bit stream info */ 
1209 
} 
1210  
1211  
1212 
/**

1213 
* Write one audio block to the output bitstream.

1214 
*/

1215 
static void output_audio_block(AC3EncodeContext *s, 
1216 
uint8_t exp_strategy[AC3_MAX_CHANNELS], 
1217 
uint8_t num_exp_groups[AC3_MAX_CHANNELS], 
1218 
uint8_t grouped_exp[AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS], 
1219 
uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1220 
uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1221 
int block_num)

1222 
{ 
1223 
int ch, i, baie, rbnd;

1224  
1225 
for (ch = 0; ch < s>fbw_channels; ch++) 
1226 
put_bits(&s>pb, 1, 0); /* no block switching */ 
1227 
for (ch = 0; ch < s>fbw_channels; ch++) 
1228 
put_bits(&s>pb, 1, 1); /* no dither */ 
1229 
put_bits(&s>pb, 1, 0); /* no dynamic range */ 
1230 
if (!block_num) {

1231 
put_bits(&s>pb, 1, 1); /* coupling strategy present */ 
1232 
put_bits(&s>pb, 1, 0); /* no coupling strategy */ 
1233 
} else {

1234 
put_bits(&s>pb, 1, 0); /* no new coupling strategy */ 
1235 
} 
1236  
1237 
if (s>channel_mode == AC3_CHMODE_STEREO) {

1238 
if (!block_num) {

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

1240 
put_bits(&s>pb, 1, 1); 
1241  
1242 
/* dummy rematrixing rematflg(1:4)=0 */

1243 
for (rbnd = 0; rbnd < 4; rbnd++) 
1244 
put_bits(&s>pb, 1, 0); 
1245 
} else {

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

1247 
put_bits(&s>pb, 1, 0); 
1248 
} 
1249 
} 
1250  
1251 
/* exponent strategy */

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

1254  
1255 
if (s>lfe_on)

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

1257  
1258 
/* bandwidth */

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

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

1262 
} 
1263  
1264 
/* exponents */

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

1267 
continue;

1268  
1269 
/* first exponent */

1270 
put_bits(&s>pb, 4, grouped_exp[ch][0]); 
1271  
1272 
/* next ones are deltaencoded and grouped */

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

1275  
1276 
if (ch != s>lfe_channel)

1277 
put_bits(&s>pb, 2, 0); /* no gain range info */ 
1278 
} 
1279  
1280 
/* bit allocation info */

1281 
baie = (block_num == 0);

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

1283 
if (baie) {

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

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

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

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

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

1289 
} 
1290  
1291 
/* snr offset */

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

1293 
if (baie) {

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

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

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

1298 
} 
1299 
} 
1300  
1301 
put_bits(&s>pb, 1, 0); /* no delta bit allocation */ 
1302 
put_bits(&s>pb, 1, 0); /* no data to skip */ 
1303  
1304 
/* mantissa encoding */

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

1307  
1308 
for (i = 0; i < s>nb_coefs[ch]; i++) { 
1309 
q = qmant[ch][i]; 
1310 
b = bap[ch][i]; 
1311 
switch (b) {

1312 
case 0: break; 
1313 
case 1: if (q != 128) put_bits(&s>pb, 5, q); break; 
1314 
case 2: if (q != 128) put_bits(&s>pb, 7, q); break; 
1315 
case 3: put_bits(&s>pb, 3, q); break; 
1316 
case 4: if (q != 128) put_bits(&s>pb, 7, q); break; 
1317 
case 14: put_bits(&s>pb, 14, q); break; 
1318 
case 15: put_bits(&s>pb, 16, q); break; 
1319 
default: put_bits(&s>pb, b1, q); break; 
1320 
} 
1321 
} 
1322 
} 
1323 
} 
1324  
1325  
1326 
/** CRC16 Polynomial */

1327 
#define CRC16_POLY ((1 << 0)  (1 << 2)  (1 << 15)  (1 << 16)) 
1328  
1329  
1330 
static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly) 
1331 
{ 
1332 
unsigned int c; 
1333  
1334 
c = 0;

1335 
while (a) {

1336 
if (a & 1) 
1337 
c ^= b; 
1338 
a = a >> 1;

1339 
b = b << 1;

1340 
if (b & (1 << 16)) 
1341 
b ^= poly; 
1342 
} 
1343 
return c;

1344 
} 
1345  
1346  
1347 
static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) 
1348 
{ 
1349 
unsigned int r; 
1350 
r = 1;

1351 
while (n) {

1352 
if (n & 1) 
1353 
r = mul_poly(r, a, poly); 
1354 
a = mul_poly(a, a, poly); 
1355 
n >>= 1;

1356 
} 
1357 
return r;

1358 
} 
1359  
1360  
1361 
/**

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

1363 
*/

1364 
static void output_frame_end(AC3EncodeContext *s) 
1365 
{ 
1366 
int frame_size, frame_size_58, pad_bytes, crc1, crc2, crc_inv;

1367 
uint8_t *frame; 
1368  
1369 
frame_size = s>frame_size; /* frame size in words */

1370 
/* align to 8 bits */

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

1373 
frame = s>pb.buf; 
1374 
pad_bytes = s>frame_size  (put_bits_ptr(&s>pb)  frame)  2;

1375 
assert(pad_bytes >= 0);

1376 
if (pad_bytes > 0) 
1377 
memset(put_bits_ptr(&s>pb), 0, pad_bytes);

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

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

1381 
frame_size_58 = ((frame_size >> 2) + (frame_size >> 4)) << 1; 
1382  
1383 
crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,

1384 
frame + 4, frame_size_58  4)); 
1385  
1386 
/* XXX: could precompute crc_inv */

1387 
crc_inv = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58)  16, CRC16_POLY); 
1388 
crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); 
1389 
AV_WB16(frame + 2, crc1);

1390  
1391 
crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,

1392 
frame + frame_size_58, 
1393 
frame_size  frame_size_58  2));

1394 
AV_WB16(frame + frame_size  2, crc2);

1395 
} 
1396  
1397  
1398 
/**

1399 
* Write the frame to the output bitstream.

1400 
*/

1401 
static void output_frame(AC3EncodeContext *s, 
1402 
unsigned char *frame, 
1403 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
1404 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], 
1405 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS], 
1406 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], 
1407 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]) 
1408 
{ 
1409 
int blk;

1410  
1411 
init_put_bits(&s>pb, frame, AC3_MAX_CODED_FRAME_SIZE); 
1412  
1413 
output_frame_header(s); 
1414  
1415 
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { 
1416 
output_audio_block(s, exp_strategy[blk], num_exp_groups[blk], 
1417 
grouped_exp[blk], bap[blk], qmant[blk], blk); 
1418 
} 
1419  
1420 
output_frame_end(s); 
1421 
} 
1422  
1423  
1424 
/**

1425 
* Encode a single AC3 frame.

1426 
*/

1427 
static int ac3_encode_frame(AVCodecContext *avctx, 
1428 
unsigned char *frame, int buf_size, void *data) 
1429 
{ 
1430 
AC3EncodeContext *s = avctx>priv_data; 
1431 
const int16_t *samples = data;

1432 
int16_t planar_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE+AC3_FRAME_SIZE]; 
1433 
int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1434 
uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1435 
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 
1436 
uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1437 
uint8_t num_exp_groups[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 
1438 
uint8_t grouped_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_EXP_GROUPS]; 
1439 
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1440 
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; 
1441 
uint16_t qmant[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; 
1442 
int ret;

1443  
1444 
if (s>bit_alloc.sr_code == 1) 
1445 
adjust_frame_size(s); 
1446  
1447 
deinterleave_input_samples(s, samples, planar_samples); 
1448  
1449 
apply_mdct(s, planar_samples, exp_shift, mdct_coef); 
1450  
1451 
process_exponents(s, mdct_coef, exp_shift, exp, exp_strategy, encoded_exp, 
1452 
num_exp_groups, grouped_exp); 
1453  
1454 
ret = compute_bit_allocation(s, bap, encoded_exp, exp_strategy); 
1455 
if (ret) {

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

1457 
return ret;

1458 
} 
1459  
1460 
quantize_mantissas(s, mdct_coef, exp_shift, encoded_exp, bap, qmant); 
1461  
1462 
output_frame(s, frame, exp_strategy, num_exp_groups, grouped_exp, bap, qmant); 
1463  
1464 
return s>frame_size;

1465 
} 
1466  
1467  
1468 
/**

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

1470 
*/

1471 
static av_cold int ac3_encode_close(AVCodecContext *avctx) 
1472 
{ 
1473 
av_freep(&avctx>coded_frame); 
1474 
return 0; 
1475 
} 
1476  
1477  
1478 
/**

1479 
* Set channel information during initialization.

1480 
*/

1481 
static av_cold int set_channel_info(AC3EncodeContext *s, int channels, 
1482 
int64_t *channel_layout) 
1483 
{ 
1484 
int ch_layout;

1485  
1486 
if (channels < 1  channels > AC3_MAX_CHANNELS) 
1487 
return AVERROR(EINVAL);

1488 
if ((uint64_t)*channel_layout > 0x7FF) 
1489 
return AVERROR(EINVAL);

1490 
ch_layout = *channel_layout; 
1491 
if (!ch_layout)

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

1493 
if (av_get_channel_layout_nb_channels(ch_layout) != channels)

1494 
return AVERROR(EINVAL);

1495  
1496 
s>lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY); 
1497 
s>channels = channels; 
1498 
s>fbw_channels = channels  s>lfe_on; 
1499 
s>lfe_channel = s>lfe_on ? s>fbw_channels : 1;

1500 
if (s>lfe_on)

1501 
ch_layout = AV_CH_LOW_FREQUENCY; 
1502  
1503 
switch (ch_layout) {

1504 
case AV_CH_LAYOUT_MONO: s>channel_mode = AC3_CHMODE_MONO; break; 
1505 
case AV_CH_LAYOUT_STEREO: s>channel_mode = AC3_CHMODE_STEREO; break; 
1506 
case AV_CH_LAYOUT_SURROUND: s>channel_mode = AC3_CHMODE_3F; break; 
1507 
case AV_CH_LAYOUT_2_1: s>channel_mode = AC3_CHMODE_2F1R; break; 
1508 
case AV_CH_LAYOUT_4POINT0: s>channel_mode = AC3_CHMODE_3F1R; break; 
1509 
case AV_CH_LAYOUT_QUAD:

1510 
case AV_CH_LAYOUT_2_2: s>channel_mode = AC3_CHMODE_2F2R; break; 
1511 
case AV_CH_LAYOUT_5POINT0:

1512 
case AV_CH_LAYOUT_5POINT0_BACK: s>channel_mode = AC3_CHMODE_3F2R; break; 
1513 
default:

1514 
return AVERROR(EINVAL);

1515 
} 
1516  
1517 
s>channel_map = ff_ac3_enc_channel_map[s>channel_mode][s>lfe_on]; 
1518 
*channel_layout = ch_layout; 
1519 
if (s>lfe_on)

1520 
*channel_layout = AV_CH_LOW_FREQUENCY; 
1521  
1522 
return 0; 
1523 
} 
1524  
1525  
1526 
static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s) 
1527 
{ 
1528 
int i, ret;

1529  
1530 
/* validate channel layout */

1531 
if (!avctx>channel_layout) {

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

1533 
"encoder will guess the layout, but it "

1534 
"might be incorrect.\n");

1535 
} 
1536 
ret = set_channel_info(s, avctx>channels, &avctx>channel_layout); 
1537 
if (ret) {

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

1539 
return ret;

1540 
} 
1541  
1542 
/* validate sample rate */

1543 
for (i = 0; i < 9; i++) { 
1544 
if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx>sample_rate) 
1545 
break;

1546 
} 
1547 
if (i == 9) { 
1548 
av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");

1549 
return AVERROR(EINVAL);

1550 
} 
1551 
s>sample_rate = avctx>sample_rate; 
1552 
s>bit_alloc.sr_shift = i % 3;

1553 
s>bit_alloc.sr_code = i / 3;

1554  
1555 
/* validate bit rate */

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

1559 
} 
1560 
if (i == 19) { 
1561 
av_log(avctx, AV_LOG_ERROR, "invalid bit rate\n");

1562 
return AVERROR(EINVAL);

1563 
} 
1564 
s>bit_rate = avctx>bit_rate; 
1565 
s>frame_size_code = i << 1;

1566  
1567 
return 0; 
1568 
} 
1569  
1570  
1571 
/**

1572 
* Set bandwidth for all channels.

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

1574 
* default value will be used.

1575 
*/

1576 
static av_cold void set_bandwidth(AC3EncodeContext *s, int cutoff) 
1577 
{ 
1578 
int ch, bw_code;

1579  
1580 
if (cutoff) {

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

1582 
int fbw_coeffs;

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

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

1587 
/* use default bandwidth setting */

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

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

1590 
bw_code = 50;

1591 
} 
1592  
1593 
/* set number of coefficients for each channel */

1594 
for (ch = 0; ch < s>fbw_channels; ch++) { 
1595 
s>bandwidth_code[ch] = bw_code; 
1596 
s>nb_coefs[ch] = bw_code * 3 + 73; 
1597 
} 
1598 
if (s>lfe_on)

1599 
s>nb_coefs[s>lfe_channel] = 7; /* LFE channel always has 7 coefs */ 
1600 
} 
1601  
1602  
1603 
/**

1604 
* Initialize the encoder.

1605 
*/

1606 
static av_cold int ac3_encode_init(AVCodecContext *avctx) 
1607 
{ 
1608 
AC3EncodeContext *s = avctx>priv_data; 
1609 
int ret;

1610  
1611 
avctx>frame_size = AC3_FRAME_SIZE; 
1612  
1613 
ac3_common_init(); 
1614  
1615 
ret = validate_options(avctx, s); 
1616 
if (ret)

1617 
return ret;

1618  
1619 
s>bitstream_id = 8 + s>bit_alloc.sr_shift;

1620 
s>bitstream_mode = 0; /* complete main audio service */ 
1621  
1622 
s>frame_size_min = 2 * ff_ac3_frame_size_tab[s>frame_size_code][s>bit_alloc.sr_code];

1623 
s>bits_written = 0;

1624 
s>samples_written = 0;

1625 
s>frame_size = s>frame_size_min; 
1626  
1627 
set_bandwidth(s, avctx>cutoff); 
1628  
1629 
bit_alloc_init(s); 
1630  
1631 
mdct_init(9);

1632  
1633 
avctx>coded_frame= avcodec_alloc_frame(); 
1634 
avctx>coded_frame>key_frame= 1;

1635  
1636 
return 0; 
1637 
} 
1638  
1639  
1640 
#ifdef TEST

1641 
/*************************************************************************/

1642 
/* TEST */

1643  
1644 
#include "libavutil/lfg.h" 
1645  
1646 
#define FN (MDCT_SAMPLES/4) 
1647  
1648  
1649 
static void fft_test(AVLFG *lfg) 
1650 
{ 
1651 
IComplex in[FN], in1[FN]; 
1652 
int k, n, i;

1653 
float sum_re, sum_im, a;

1654  
1655 
for (i = 0; i < FN; i++) { 
1656 
in[i].re = av_lfg_get(lfg) % 65535  32767; 
1657 
in[i].im = av_lfg_get(lfg) % 65535  32767; 
1658 
in1[i] = in[i]; 
1659 
} 
1660 
fft(in, 7);

1661  
1662 
/* do it by hand */

1663 
for (k = 0; k < FN; k++) { 
1664 
sum_re = 0;

1665 
sum_im = 0;

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

1668 
sum_re += in1[n].re * cos(a)  in1[n].im * sin(a); 
1669 
sum_im += in1[n].re * sin(a) + in1[n].im * cos(a); 
1670 
} 
1671 
av_log(NULL, AV_LOG_DEBUG, "%3d: %6d,%6d %6.0f,%6.0f\n", 
1672 
k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); 
1673 
} 
1674 
} 
1675  
1676  
1677 
static void mdct_test(AVLFG *lfg) 
1678 
{ 
1679 
int16_t input[MDCT_SAMPLES]; 
1680 
int32_t output[AC3_MAX_COEFS]; 
1681 
float input1[MDCT_SAMPLES];

1682 
float output1[AC3_MAX_COEFS];

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

1684 
int i, k, n;

1685  
1686 
for (i = 0; i < MDCT_SAMPLES; i++) { 
1687 
input[i] = (av_lfg_get(lfg) % 65535  32767) * 9 / 10; 
1688 
input1[i] = input[i]; 
1689 
} 
1690  
1691 
mdct512(output, input); 
1692  
1693 
/* do it by hand */

1694 
for (k = 0; k < AC3_MAX_COEFS; k++) { 
1695 
s = 0;

1696 
for (n = 0; n < MDCT_SAMPLES; n++) { 
1697 
a = (2*M_PI*(2*n+1+MDCT_SAMPLES/2)*(2*k+1) / (4 * MDCT_SAMPLES)); 
1698 
s += input1[n] * cos(a); 
1699 
} 
1700 
output1[k] = 2 * s / MDCT_SAMPLES;

1701 
} 
1702  
1703 
err = 0;

1704 
emax = 0;

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

1709 
emax = e; 
1710 
err += e * e; 
1711 
} 
1712 
av_log(NULL, AV_LOG_DEBUG, "err2=%f emax=%f\n", err / AC3_MAX_COEFS, emax); 
1713 
} 
1714  
1715  
1716 
int main(void) 
1717 
{ 
1718 
AVLFG lfg; 
1719  
1720 
av_log_set_level(AV_LOG_DEBUG); 
1721 
mdct_init(9);

1722  
1723 
fft_test(&lfg); 
1724 
mdct_test(&lfg); 
1725  
1726 
return 0; 
1727 
} 
1728 
#endif /* TEST */ 
1729  
1730  
1731 
AVCodec ac3_encoder = { 
1732 
"ac3",

1733 
AVMEDIA_TYPE_AUDIO, 
1734 
CODEC_ID_AC3, 
1735 
sizeof(AC3EncodeContext),

1736 
ac3_encode_init, 
1737 
ac3_encode_frame, 
1738 
ac3_encode_close, 
1739 
NULL,

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

1742 
.channel_layouts = (const int64_t[]){

1743 
AV_CH_LAYOUT_MONO, 
1744 
AV_CH_LAYOUT_STEREO, 
1745 
AV_CH_LAYOUT_2_1, 
1746 
AV_CH_LAYOUT_SURROUND, 
1747 
AV_CH_LAYOUT_2_2, 
1748 
AV_CH_LAYOUT_QUAD, 
1749 
AV_CH_LAYOUT_4POINT0, 
1750 
AV_CH_LAYOUT_5POINT0, 
1751 
AV_CH_LAYOUT_5POINT0_BACK, 
1752 
(AV_CH_LAYOUT_MONO  AV_CH_LOW_FREQUENCY), 
1753 
(AV_CH_LAYOUT_STEREO  AV_CH_LOW_FREQUENCY), 
1754 
(AV_CH_LAYOUT_2_1  AV_CH_LOW_FREQUENCY), 
1755 
(AV_CH_LAYOUT_SURROUND  AV_CH_LOW_FREQUENCY), 
1756 
(AV_CH_LAYOUT_2_2  AV_CH_LOW_FREQUENCY), 
1757 
(AV_CH_LAYOUT_QUAD  AV_CH_LOW_FREQUENCY), 
1758 
(AV_CH_LAYOUT_4POINT0  AV_CH_LOW_FREQUENCY), 
1759 
AV_CH_LAYOUT_5POINT1, 
1760 
AV_CH_LAYOUT_5POINT1_BACK, 
1761 
0 },

1762 
}; 