Rename dec2() function
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / ac3enc.c
blobe85d86c9018eefaf29be4f6f4f7de26eb90a33e5
1 /*
2 * The simplest AC3 encoder
3 * Copyright (c) 2000 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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 02110-1301 USA
22 /**
23 * @file ac3enc.c
24 * The simplest AC3 encoder.
26 //#define DEBUG
27 //#define DEBUG_BITALLOC
28 #include "libavutil/crc.h"
29 #include "avcodec.h"
30 #include "bitstream.h"
31 #include "ac3.h"
33 typedef struct AC3EncodeContext {
34 PutBitContext pb;
35 int nb_channels;
36 int nb_all_channels;
37 int lfe_channel;
38 int bit_rate;
39 unsigned int sample_rate;
40 unsigned int bitstream_id;
41 unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */
42 unsigned int frame_size; /* current frame size in words */
43 unsigned int bits_written;
44 unsigned int samples_written;
45 int sr_shift;
46 unsigned int frame_size_code;
47 unsigned int sr_code; /* frequency */
48 unsigned int channel_mode;
49 int lfe;
50 unsigned int bitstream_mode;
51 short last_samples[AC3_MAX_CHANNELS][256];
52 unsigned int chbwcod[AC3_MAX_CHANNELS];
53 int nb_coefs[AC3_MAX_CHANNELS];
55 /* bitrate allocation control */
56 int slow_gain_code, slow_decay_code, fast_decay_code, db_per_bit_code, floor_code;
57 AC3BitAllocParameters bit_alloc;
58 int coarse_snr_offset;
59 int fast_gain_code[AC3_MAX_CHANNELS];
60 int fine_snr_offset[AC3_MAX_CHANNELS];
61 /* mantissa encoding */
62 int mant1_cnt, mant2_cnt, mant4_cnt;
63 } AC3EncodeContext;
65 static int16_t costab[64];
66 static int16_t sintab[64];
67 static int16_t xcos1[128];
68 static int16_t xsin1[128];
70 #define MDCT_NBITS 9
71 #define N (1 << MDCT_NBITS)
73 /* new exponents are sent if their Norm 1 exceed this number */
74 #define EXP_DIFF_THRESHOLD 1000
76 static inline int16_t fix15(float a)
78 int v;
79 v = (int)(a * (float)(1 << 15));
80 if (v < -32767)
81 v = -32767;
82 else if (v > 32767)
83 v = 32767;
84 return v;
87 typedef struct IComplex {
88 short re,im;
89 } IComplex;
91 static void fft_init(int ln)
93 int i, n;
94 float alpha;
96 n = 1 << ln;
98 for(i=0;i<(n/2);i++) {
99 alpha = 2 * M_PI * (float)i / (float)n;
100 costab[i] = fix15(cos(alpha));
101 sintab[i] = fix15(sin(alpha));
105 /* butter fly op */
106 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
108 int ax, ay, bx, by;\
109 bx=pre1;\
110 by=pim1;\
111 ax=qre1;\
112 ay=qim1;\
113 pre = (bx + ax) >> 1;\
114 pim = (by + ay) >> 1;\
115 qre = (bx - ax) >> 1;\
116 qim = (by - ay) >> 1;\
119 #define MUL16(a,b) ((a) * (b))
121 #define CMUL(pre, pim, are, aim, bre, bim) \
123 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
124 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
128 /* do a 2^n point complex fft on 2^ln points. */
129 static void fft(IComplex *z, int ln)
131 int j, l, np, np2;
132 int nblocks, nloops;
133 register IComplex *p,*q;
134 int tmp_re, tmp_im;
136 np = 1 << ln;
138 /* reverse */
139 for(j=0;j<np;j++) {
140 int k = ff_reverse[j] >> (8 - ln);
141 if (k < j)
142 FFSWAP(IComplex, z[k], z[j]);
145 /* pass 0 */
147 p=&z[0];
148 j=(np >> 1);
149 do {
150 BF(p[0].re, p[0].im, p[1].re, p[1].im,
151 p[0].re, p[0].im, p[1].re, p[1].im);
152 p+=2;
153 } while (--j != 0);
155 /* pass 1 */
157 p=&z[0];
158 j=np >> 2;
159 do {
160 BF(p[0].re, p[0].im, p[2].re, p[2].im,
161 p[0].re, p[0].im, p[2].re, p[2].im);
162 BF(p[1].re, p[1].im, p[3].re, p[3].im,
163 p[1].re, p[1].im, p[3].im, -p[3].re);
164 p+=4;
165 } while (--j != 0);
167 /* pass 2 .. ln-1 */
169 nblocks = np >> 3;
170 nloops = 1 << 2;
171 np2 = np >> 1;
172 do {
173 p = z;
174 q = z + nloops;
175 for (j = 0; j < nblocks; ++j) {
177 BF(p->re, p->im, q->re, q->im,
178 p->re, p->im, q->re, q->im);
180 p++;
181 q++;
182 for(l = nblocks; l < np2; l += nblocks) {
183 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
184 BF(p->re, p->im, q->re, q->im,
185 p->re, p->im, tmp_re, tmp_im);
186 p++;
187 q++;
189 p += nloops;
190 q += nloops;
192 nblocks = nblocks >> 1;
193 nloops = nloops << 1;
194 } while (nblocks != 0);
197 /* do a 512 point mdct */
198 static void mdct512(int32_t *out, int16_t *in)
200 int i, re, im, re1, im1;
201 int16_t rot[N];
202 IComplex x[N/4];
204 /* shift to simplify computations */
205 for(i=0;i<N/4;i++)
206 rot[i] = -in[i + 3*N/4];
207 for(i=N/4;i<N;i++)
208 rot[i] = in[i - N/4];
210 /* pre rotation */
211 for(i=0;i<N/4;i++) {
212 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1;
213 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1;
214 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
217 fft(x, MDCT_NBITS - 2);
219 /* post rotation */
220 for(i=0;i<N/4;i++) {
221 re = x[i].re;
222 im = x[i].im;
223 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]);
224 out[2*i] = im1;
225 out[N/2-1-2*i] = re1;
229 /* XXX: use another norm ? */
230 static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
232 int sum, i;
233 sum = 0;
234 for(i=0;i<n;i++) {
235 sum += abs(exp1[i] - exp2[i]);
237 return sum;
240 static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
241 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
242 int ch, int is_lfe)
244 int i, j;
245 int exp_diff;
247 /* estimate if the exponent variation & decide if they should be
248 reused in the next frame */
249 exp_strategy[0][ch] = EXP_NEW;
250 for(i=1;i<NB_BLOCKS;i++) {
251 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
252 #ifdef DEBUG
253 av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff);
254 #endif
255 if (exp_diff > EXP_DIFF_THRESHOLD)
256 exp_strategy[i][ch] = EXP_NEW;
257 else
258 exp_strategy[i][ch] = EXP_REUSE;
260 if (is_lfe)
261 return;
263 /* now select the encoding strategy type : if exponents are often
264 recoded, we use a coarse encoding */
265 i = 0;
266 while (i < NB_BLOCKS) {
267 j = i + 1;
268 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE)
269 j++;
270 switch(j - i) {
271 case 1:
272 exp_strategy[i][ch] = EXP_D45;
273 break;
274 case 2:
275 case 3:
276 exp_strategy[i][ch] = EXP_D25;
277 break;
278 default:
279 exp_strategy[i][ch] = EXP_D15;
280 break;
282 i = j;
286 /* set exp[i] to min(exp[i], exp1[i]) */
287 static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n)
289 int i;
291 for(i=0;i<n;i++) {
292 if (exp1[i] < exp[i])
293 exp[i] = exp1[i];
297 /* update the exponents so that they are the ones the decoder will
298 decode. Return the number of bits used to code the exponents */
299 static int encode_exp(uint8_t encoded_exp[N/2],
300 uint8_t exp[N/2],
301 int nb_exps,
302 int exp_strategy)
304 int group_size, nb_groups, i, j, k, exp_min;
305 uint8_t exp1[N/2];
307 switch(exp_strategy) {
308 case EXP_D15:
309 group_size = 1;
310 break;
311 case EXP_D25:
312 group_size = 2;
313 break;
314 default:
315 case EXP_D45:
316 group_size = 4;
317 break;
319 nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3;
321 /* for each group, compute the minimum exponent */
322 exp1[0] = exp[0]; /* DC exponent is handled separately */
323 k = 1;
324 for(i=1;i<=nb_groups;i++) {
325 exp_min = exp[k];
326 assert(exp_min >= 0 && exp_min <= 24);
327 for(j=1;j<group_size;j++) {
328 if (exp[k+j] < exp_min)
329 exp_min = exp[k+j];
331 exp1[i] = exp_min;
332 k += group_size;
335 /* constraint for DC exponent */
336 if (exp1[0] > 15)
337 exp1[0] = 15;
339 /* Decrease the delta between each groups to within 2
340 * so that they can be differentially encoded */
341 for (i=1;i<=nb_groups;i++)
342 exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
343 for (i=nb_groups-1;i>=0;i--)
344 exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2);
346 /* now we have the exponent values the decoder will see */
347 encoded_exp[0] = exp1[0];
348 k = 1;
349 for(i=1;i<=nb_groups;i++) {
350 for(j=0;j<group_size;j++) {
351 encoded_exp[k+j] = exp1[i];
353 k += group_size;
356 #if defined(DEBUG)
357 av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy);
358 for(i=0;i<=nb_groups * group_size;i++) {
359 av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]);
361 av_log(NULL, AV_LOG_DEBUG, "\n");
362 #endif
364 return 4 + (nb_groups / 3) * 7;
367 /* return the size in bits taken by the mantissa */
368 static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs)
370 int bits, mant, i;
372 bits = 0;
373 for(i=0;i<nb_coefs;i++) {
374 mant = m[i];
375 switch(mant) {
376 case 0:
377 /* nothing */
378 break;
379 case 1:
380 /* 3 mantissa in 5 bits */
381 if (s->mant1_cnt == 0)
382 bits += 5;
383 if (++s->mant1_cnt == 3)
384 s->mant1_cnt = 0;
385 break;
386 case 2:
387 /* 3 mantissa in 7 bits */
388 if (s->mant2_cnt == 0)
389 bits += 7;
390 if (++s->mant2_cnt == 3)
391 s->mant2_cnt = 0;
392 break;
393 case 3:
394 bits += 3;
395 break;
396 case 4:
397 /* 2 mantissa in 7 bits */
398 if (s->mant4_cnt == 0)
399 bits += 7;
400 if (++s->mant4_cnt == 2)
401 s->mant4_cnt = 0;
402 break;
403 case 14:
404 bits += 14;
405 break;
406 case 15:
407 bits += 16;
408 break;
409 default:
410 bits += mant - 1;
411 break;
414 return bits;
418 static void bit_alloc_masking(AC3EncodeContext *s,
419 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
420 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
421 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
422 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50])
424 int blk, ch;
425 int16_t band_psd[NB_BLOCKS][AC3_MAX_CHANNELS][50];
427 for(blk=0; blk<NB_BLOCKS; blk++) {
428 for(ch=0;ch<s->nb_all_channels;ch++) {
429 if(exp_strategy[blk][ch] == EXP_REUSE) {
430 memcpy(psd[blk][ch], psd[blk-1][ch], (N/2)*sizeof(int16_t));
431 memcpy(mask[blk][ch], mask[blk-1][ch], 50*sizeof(int16_t));
432 } else {
433 ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0,
434 s->nb_coefs[ch],
435 psd[blk][ch], band_psd[blk][ch]);
436 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, band_psd[blk][ch],
437 0, s->nb_coefs[ch],
438 ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
439 ch == s->lfe_channel,
440 DBA_NONE, 0, NULL, NULL, NULL,
441 mask[blk][ch]);
447 static int bit_alloc(AC3EncodeContext *s,
448 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50],
449 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
450 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
451 int frame_bits, int coarse_snr_offset, int fine_snr_offset)
453 int i, ch;
454 int snr_offset;
456 snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2;
458 /* compute size */
459 for(i=0;i<NB_BLOCKS;i++) {
460 s->mant1_cnt = 0;
461 s->mant2_cnt = 0;
462 s->mant4_cnt = 0;
463 for(ch=0;ch<s->nb_all_channels;ch++) {
464 ff_ac3_bit_alloc_calc_bap(mask[i][ch], psd[i][ch], 0,
465 s->nb_coefs[ch], snr_offset,
466 s->bit_alloc.floor, bap[i][ch]);
467 frame_bits += compute_mantissa_size(s, bap[i][ch],
468 s->nb_coefs[ch]);
471 #if 0
472 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
473 coarse_snr_offset, fine_snr_offset, frame_bits,
474 16 * s->frame_size - ((frame_bits + 7) & ~7));
475 #endif
476 return 16 * s->frame_size - frame_bits;
479 #define SNR_INC1 4
481 static int compute_bit_allocation(AC3EncodeContext *s,
482 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
483 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
484 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
485 int frame_bits)
487 int i, ch;
488 int coarse_snr_offset, fine_snr_offset;
489 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
490 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
491 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50];
492 static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
494 /* init default parameters */
495 s->slow_decay_code = 2;
496 s->fast_decay_code = 1;
497 s->slow_gain_code = 1;
498 s->db_per_bit_code = 2;
499 s->floor_code = 4;
500 for(ch=0;ch<s->nb_all_channels;ch++)
501 s->fast_gain_code[ch] = 4;
503 /* compute real values */
504 s->bit_alloc.sr_code = s->sr_code;
505 s->bit_alloc.sr_shift = s->sr_shift;
506 s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->sr_shift;
507 s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->sr_shift;
508 s->bit_alloc.slow_gain = ff_ac3_slow_gain_tab[s->slow_gain_code];
509 s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
510 s->bit_alloc.floor = ff_ac3_floor_tab[s->floor_code];
512 /* header size */
513 frame_bits += 65;
514 // if (s->channel_mode == 2)
515 // frame_bits += 2;
516 frame_bits += frame_bits_inc[s->channel_mode];
518 /* audio blocks */
519 for(i=0;i<NB_BLOCKS;i++) {
520 frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
521 if (s->channel_mode == AC3_CHMODE_STEREO) {
522 frame_bits++; /* rematstr */
523 if(i==0) frame_bits += 4;
525 frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */
526 if (s->lfe)
527 frame_bits++; /* lfeexpstr */
528 for(ch=0;ch<s->nb_channels;ch++) {
529 if (exp_strategy[i][ch] != EXP_REUSE)
530 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
532 frame_bits++; /* baie */
533 frame_bits++; /* snr */
534 frame_bits += 2; /* delta / skip */
536 frame_bits++; /* cplinu for block 0 */
537 /* bit alloc info */
538 /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
539 /* csnroffset[6] */
540 /* (fsnoffset[4] + fgaincod[4]) * c */
541 frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);
543 /* auxdatae, crcrsv */
544 frame_bits += 2;
546 /* CRC */
547 frame_bits += 16;
549 /* calculate psd and masking curve before doing bit allocation */
550 bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
552 /* now the big work begins : do the bit allocation. Modify the snr
553 offset until we can pack everything in the requested frame size */
555 coarse_snr_offset = s->coarse_snr_offset;
556 while (coarse_snr_offset >= 0 &&
557 bit_alloc(s, mask, psd, bap, frame_bits, coarse_snr_offset, 0) < 0)
558 coarse_snr_offset -= SNR_INC1;
559 if (coarse_snr_offset < 0) {
560 av_log(NULL, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
561 return -1;
563 while ((coarse_snr_offset + SNR_INC1) <= 63 &&
564 bit_alloc(s, mask, psd, bap1, frame_bits,
565 coarse_snr_offset + SNR_INC1, 0) >= 0) {
566 coarse_snr_offset += SNR_INC1;
567 memcpy(bap, bap1, sizeof(bap1));
569 while ((coarse_snr_offset + 1) <= 63 &&
570 bit_alloc(s, mask, psd, bap1, frame_bits, coarse_snr_offset + 1, 0) >= 0) {
571 coarse_snr_offset++;
572 memcpy(bap, bap1, sizeof(bap1));
575 fine_snr_offset = 0;
576 while ((fine_snr_offset + SNR_INC1) <= 15 &&
577 bit_alloc(s, mask, psd, bap1, frame_bits,
578 coarse_snr_offset, fine_snr_offset + SNR_INC1) >= 0) {
579 fine_snr_offset += SNR_INC1;
580 memcpy(bap, bap1, sizeof(bap1));
582 while ((fine_snr_offset + 1) <= 15 &&
583 bit_alloc(s, mask, psd, bap1, frame_bits,
584 coarse_snr_offset, fine_snr_offset + 1) >= 0) {
585 fine_snr_offset++;
586 memcpy(bap, bap1, sizeof(bap1));
589 s->coarse_snr_offset = coarse_snr_offset;
590 for(ch=0;ch<s->nb_all_channels;ch++)
591 s->fine_snr_offset[ch] = fine_snr_offset;
592 #if defined(DEBUG_BITALLOC)
594 int j;
596 for(i=0;i<6;i++) {
597 for(ch=0;ch<s->nb_all_channels;ch++) {
598 printf("Block #%d Ch%d:\n", i, ch);
599 printf("bap=");
600 for(j=0;j<s->nb_coefs[ch];j++) {
601 printf("%d ",bap[i][ch][j]);
603 printf("\n");
607 #endif
608 return 0;
611 static av_cold int AC3_encode_init(AVCodecContext *avctx)
613 int freq = avctx->sample_rate;
614 int bitrate = avctx->bit_rate;
615 int channels = avctx->channels;
616 AC3EncodeContext *s = avctx->priv_data;
617 int i, j, ch;
618 float alpha;
619 int bw_code;
620 static const uint8_t channel_mode_defs[6] = {
621 0x01, /* C */
622 0x02, /* L R */
623 0x03, /* L C R */
624 0x06, /* L R SL SR */
625 0x07, /* L C R SL SR */
626 0x07, /* L C R SL SR (+LFE) */
629 avctx->frame_size = AC3_FRAME_SIZE;
631 ac3_common_init();
633 /* number of channels */
634 if (channels < 1 || channels > 6)
635 return -1;
636 s->channel_mode = channel_mode_defs[channels - 1];
637 s->lfe = (channels == 6) ? 1 : 0;
638 s->nb_all_channels = channels;
639 s->nb_channels = channels > 5 ? 5 : channels;
640 s->lfe_channel = s->lfe ? 5 : -1;
642 /* frequency */
643 for(i=0;i<3;i++) {
644 for(j=0;j<3;j++)
645 if ((ff_ac3_sample_rate_tab[j] >> i) == freq)
646 goto found;
648 return -1;
649 found:
650 s->sample_rate = freq;
651 s->sr_shift = i;
652 s->sr_code = j;
653 s->bitstream_id = 8 + s->sr_shift;
654 s->bitstream_mode = 0; /* complete main audio service */
656 /* bitrate & frame size */
657 for(i=0;i<19;i++) {
658 if ((ff_ac3_bitrate_tab[i] >> s->sr_shift)*1000 == bitrate)
659 break;
661 if (i == 19)
662 return -1;
663 s->bit_rate = bitrate;
664 s->frame_size_code = i << 1;
665 s->frame_size_min = ff_ac3_frame_size_tab[s->frame_size_code][s->sr_code];
666 s->bits_written = 0;
667 s->samples_written = 0;
668 s->frame_size = s->frame_size_min;
670 /* bit allocation init */
671 if(avctx->cutoff) {
672 /* calculate bandwidth based on user-specified cutoff frequency */
673 int cutoff = av_clip(avctx->cutoff, 1, s->sample_rate >> 1);
674 int fbw_coeffs = cutoff * 512 / s->sample_rate;
675 bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
676 } else {
677 /* use default bandwidth setting */
678 /* XXX: should compute the bandwidth according to the frame
679 size, so that we avoid annoying high frequency artifacts */
680 bw_code = 50;
682 for(ch=0;ch<s->nb_channels;ch++) {
683 /* bandwidth for each channel */
684 s->chbwcod[ch] = bw_code;
685 s->nb_coefs[ch] = bw_code * 3 + 73;
687 if (s->lfe) {
688 s->nb_coefs[s->lfe_channel] = 7; /* fixed */
690 /* initial snr offset */
691 s->coarse_snr_offset = 40;
693 /* mdct init */
694 fft_init(MDCT_NBITS - 2);
695 for(i=0;i<N/4;i++) {
696 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N;
697 xcos1[i] = fix15(-cos(alpha));
698 xsin1[i] = fix15(-sin(alpha));
701 avctx->coded_frame= avcodec_alloc_frame();
702 avctx->coded_frame->key_frame= 1;
704 return 0;
707 /* output the AC3 frame header */
708 static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
710 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE);
712 put_bits(&s->pb, 16, 0x0b77); /* frame header */
713 put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
714 put_bits(&s->pb, 2, s->sr_code);
715 put_bits(&s->pb, 6, s->frame_size_code + (s->frame_size - s->frame_size_min));
716 put_bits(&s->pb, 5, s->bitstream_id);
717 put_bits(&s->pb, 3, s->bitstream_mode);
718 put_bits(&s->pb, 3, s->channel_mode);
719 if ((s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO)
720 put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
721 if (s->channel_mode & 0x04)
722 put_bits(&s->pb, 2, 1); /* XXX -6 dB */
723 if (s->channel_mode == AC3_CHMODE_STEREO)
724 put_bits(&s->pb, 2, 0); /* surround not indicated */
725 put_bits(&s->pb, 1, s->lfe); /* LFE */
726 put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
727 put_bits(&s->pb, 1, 0); /* no compression control word */
728 put_bits(&s->pb, 1, 0); /* no lang code */
729 put_bits(&s->pb, 1, 0); /* no audio production info */
730 put_bits(&s->pb, 1, 0); /* no copyright */
731 put_bits(&s->pb, 1, 1); /* original bitstream */
732 put_bits(&s->pb, 1, 0); /* no time code 1 */
733 put_bits(&s->pb, 1, 0); /* no time code 2 */
734 put_bits(&s->pb, 1, 0); /* no additional bit stream info */
737 /* symetric quantization on 'levels' levels */
738 static inline int sym_quant(int c, int e, int levels)
740 int v;
742 if (c >= 0) {
743 v = (levels * (c << e)) >> 24;
744 v = (v + 1) >> 1;
745 v = (levels >> 1) + v;
746 } else {
747 v = (levels * ((-c) << e)) >> 24;
748 v = (v + 1) >> 1;
749 v = (levels >> 1) - v;
751 assert (v >= 0 && v < levels);
752 return v;
755 /* asymetric quantization on 2^qbits levels */
756 static inline int asym_quant(int c, int e, int qbits)
758 int lshift, m, v;
760 lshift = e + qbits - 24;
761 if (lshift >= 0)
762 v = c << lshift;
763 else
764 v = c >> (-lshift);
765 /* rounding */
766 v = (v + 1) >> 1;
767 m = (1 << (qbits-1));
768 if (v >= m)
769 v = m - 1;
770 assert(v >= -m);
771 return v & ((1 << qbits)-1);
774 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
775 frame */
776 static void output_audio_block(AC3EncodeContext *s,
777 uint8_t exp_strategy[AC3_MAX_CHANNELS],
778 uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2],
779 uint8_t bap[AC3_MAX_CHANNELS][N/2],
780 int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2],
781 int8_t global_exp[AC3_MAX_CHANNELS],
782 int block_num)
784 int ch, nb_groups, group_size, i, baie, rbnd;
785 uint8_t *p;
786 uint16_t qmant[AC3_MAX_CHANNELS][N/2];
787 int exp0, exp1;
788 int mant1_cnt, mant2_cnt, mant4_cnt;
789 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
790 int delta0, delta1, delta2;
792 for(ch=0;ch<s->nb_channels;ch++)
793 put_bits(&s->pb, 1, 0); /* 512 point MDCT */
794 for(ch=0;ch<s->nb_channels;ch++)
795 put_bits(&s->pb, 1, 1); /* no dither */
796 put_bits(&s->pb, 1, 0); /* no dynamic range */
797 if (block_num == 0) {
798 /* for block 0, even if no coupling, we must say it. This is a
799 waste of bit :-) */
800 put_bits(&s->pb, 1, 1); /* coupling strategy present */
801 put_bits(&s->pb, 1, 0); /* no coupling strategy */
802 } else {
803 put_bits(&s->pb, 1, 0); /* no new coupling strategy */
806 if (s->channel_mode == AC3_CHMODE_STEREO)
808 if(block_num==0)
810 /* first block must define rematrixing (rematstr) */
811 put_bits(&s->pb, 1, 1);
813 /* dummy rematrixing rematflg(1:4)=0 */
814 for (rbnd=0;rbnd<4;rbnd++)
815 put_bits(&s->pb, 1, 0);
817 else
819 /* no matrixing (but should be used in the future) */
820 put_bits(&s->pb, 1, 0);
824 #if defined(DEBUG)
826 static int count = 0;
827 av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++);
829 #endif
830 /* exponent strategy */
831 for(ch=0;ch<s->nb_channels;ch++) {
832 put_bits(&s->pb, 2, exp_strategy[ch]);
835 if (s->lfe) {
836 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
839 for(ch=0;ch<s->nb_channels;ch++) {
840 if (exp_strategy[ch] != EXP_REUSE)
841 put_bits(&s->pb, 6, s->chbwcod[ch]);
844 /* exponents */
845 for (ch = 0; ch < s->nb_all_channels; ch++) {
846 switch(exp_strategy[ch]) {
847 case EXP_REUSE:
848 continue;
849 case EXP_D15:
850 group_size = 1;
851 break;
852 case EXP_D25:
853 group_size = 2;
854 break;
855 default:
856 case EXP_D45:
857 group_size = 4;
858 break;
860 nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
861 p = encoded_exp[ch];
863 /* first exponent */
864 exp1 = *p++;
865 put_bits(&s->pb, 4, exp1);
867 /* next ones are delta encoded */
868 for(i=0;i<nb_groups;i++) {
869 /* merge three delta in one code */
870 exp0 = exp1;
871 exp1 = p[0];
872 p += group_size;
873 delta0 = exp1 - exp0 + 2;
875 exp0 = exp1;
876 exp1 = p[0];
877 p += group_size;
878 delta1 = exp1 - exp0 + 2;
880 exp0 = exp1;
881 exp1 = p[0];
882 p += group_size;
883 delta2 = exp1 - exp0 + 2;
885 put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
888 if (ch != s->lfe_channel)
889 put_bits(&s->pb, 2, 0); /* no gain range info */
892 /* bit allocation info */
893 baie = (block_num == 0);
894 put_bits(&s->pb, 1, baie);
895 if (baie) {
896 put_bits(&s->pb, 2, s->slow_decay_code);
897 put_bits(&s->pb, 2, s->fast_decay_code);
898 put_bits(&s->pb, 2, s->slow_gain_code);
899 put_bits(&s->pb, 2, s->db_per_bit_code);
900 put_bits(&s->pb, 3, s->floor_code);
903 /* snr offset */
904 put_bits(&s->pb, 1, baie); /* always present with bai */
905 if (baie) {
906 put_bits(&s->pb, 6, s->coarse_snr_offset);
907 for(ch=0;ch<s->nb_all_channels;ch++) {
908 put_bits(&s->pb, 4, s->fine_snr_offset[ch]);
909 put_bits(&s->pb, 3, s->fast_gain_code[ch]);
913 put_bits(&s->pb, 1, 0); /* no delta bit allocation */
914 put_bits(&s->pb, 1, 0); /* no data to skip */
916 /* mantissa encoding : we use two passes to handle the grouping. A
917 one pass method may be faster, but it would necessitate to
918 modify the output stream. */
920 /* first pass: quantize */
921 mant1_cnt = mant2_cnt = mant4_cnt = 0;
922 qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
924 for (ch = 0; ch < s->nb_all_channels; ch++) {
925 int b, c, e, v;
927 for(i=0;i<s->nb_coefs[ch];i++) {
928 c = mdct_coefs[ch][i];
929 e = encoded_exp[ch][i] - global_exp[ch];
930 b = bap[ch][i];
931 switch(b) {
932 case 0:
933 v = 0;
934 break;
935 case 1:
936 v = sym_quant(c, e, 3);
937 switch(mant1_cnt) {
938 case 0:
939 qmant1_ptr = &qmant[ch][i];
940 v = 9 * v;
941 mant1_cnt = 1;
942 break;
943 case 1:
944 *qmant1_ptr += 3 * v;
945 mant1_cnt = 2;
946 v = 128;
947 break;
948 default:
949 *qmant1_ptr += v;
950 mant1_cnt = 0;
951 v = 128;
952 break;
954 break;
955 case 2:
956 v = sym_quant(c, e, 5);
957 switch(mant2_cnt) {
958 case 0:
959 qmant2_ptr = &qmant[ch][i];
960 v = 25 * v;
961 mant2_cnt = 1;
962 break;
963 case 1:
964 *qmant2_ptr += 5 * v;
965 mant2_cnt = 2;
966 v = 128;
967 break;
968 default:
969 *qmant2_ptr += v;
970 mant2_cnt = 0;
971 v = 128;
972 break;
974 break;
975 case 3:
976 v = sym_quant(c, e, 7);
977 break;
978 case 4:
979 v = sym_quant(c, e, 11);
980 switch(mant4_cnt) {
981 case 0:
982 qmant4_ptr = &qmant[ch][i];
983 v = 11 * v;
984 mant4_cnt = 1;
985 break;
986 default:
987 *qmant4_ptr += v;
988 mant4_cnt = 0;
989 v = 128;
990 break;
992 break;
993 case 5:
994 v = sym_quant(c, e, 15);
995 break;
996 case 14:
997 v = asym_quant(c, e, 14);
998 break;
999 case 15:
1000 v = asym_quant(c, e, 16);
1001 break;
1002 default:
1003 v = asym_quant(c, e, b - 1);
1004 break;
1006 qmant[ch][i] = v;
1010 /* second pass : output the values */
1011 for (ch = 0; ch < s->nb_all_channels; ch++) {
1012 int b, q;
1014 for(i=0;i<s->nb_coefs[ch];i++) {
1015 q = qmant[ch][i];
1016 b = bap[ch][i];
1017 switch(b) {
1018 case 0:
1019 break;
1020 case 1:
1021 if (q != 128)
1022 put_bits(&s->pb, 5, q);
1023 break;
1024 case 2:
1025 if (q != 128)
1026 put_bits(&s->pb, 7, q);
1027 break;
1028 case 3:
1029 put_bits(&s->pb, 3, q);
1030 break;
1031 case 4:
1032 if (q != 128)
1033 put_bits(&s->pb, 7, q);
1034 break;
1035 case 14:
1036 put_bits(&s->pb, 14, q);
1037 break;
1038 case 15:
1039 put_bits(&s->pb, 16, q);
1040 break;
1041 default:
1042 put_bits(&s->pb, b - 1, q);
1043 break;
1049 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1051 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
1053 unsigned int c;
1055 c = 0;
1056 while (a) {
1057 if (a & 1)
1058 c ^= b;
1059 a = a >> 1;
1060 b = b << 1;
1061 if (b & (1 << 16))
1062 b ^= poly;
1064 return c;
1067 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
1069 unsigned int r;
1070 r = 1;
1071 while (n) {
1072 if (n & 1)
1073 r = mul_poly(r, a, poly);
1074 a = mul_poly(a, a, poly);
1075 n >>= 1;
1077 return r;
1081 /* compute log2(max(abs(tab[]))) */
1082 static int log2_tab(int16_t *tab, int n)
1084 int i, v;
1086 v = 0;
1087 for(i=0;i<n;i++) {
1088 v |= abs(tab[i]);
1090 return av_log2(v);
1093 static void lshift_tab(int16_t *tab, int n, int lshift)
1095 int i;
1097 if (lshift > 0) {
1098 for(i=0;i<n;i++) {
1099 tab[i] <<= lshift;
1101 } else if (lshift < 0) {
1102 lshift = -lshift;
1103 for(i=0;i<n;i++) {
1104 tab[i] >>= lshift;
1109 /* fill the end of the frame and compute the two crcs */
1110 static int output_frame_end(AC3EncodeContext *s)
1112 int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
1113 uint8_t *frame;
1115 frame_size = s->frame_size; /* frame size in words */
1116 /* align to 8 bits */
1117 flush_put_bits(&s->pb);
1118 /* add zero bytes to reach the frame size */
1119 frame = s->pb.buf;
1120 n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2;
1121 assert(n >= 0);
1122 if(n>0)
1123 memset(pbBufPtr(&s->pb), 0, n);
1125 /* Now we must compute both crcs : this is not so easy for crc1
1126 because it is at the beginning of the data... */
1127 frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
1128 crc1 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1129 frame + 4, 2 * frame_size_58 - 4));
1130 /* XXX: could precompute crc_inv */
1131 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
1132 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
1133 AV_WB16(frame+2,crc1);
1135 crc2 = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
1136 frame + 2 * frame_size_58,
1137 (frame_size - frame_size_58) * 2 - 2));
1138 AV_WB16(frame+2*frame_size-2,crc2);
1140 // printf("n=%d frame_size=%d\n", n, frame_size);
1141 return frame_size * 2;
1144 static int AC3_encode_frame(AVCodecContext *avctx,
1145 unsigned char *frame, int buf_size, void *data)
1147 AC3EncodeContext *s = avctx->priv_data;
1148 int16_t *samples = data;
1149 int i, j, k, v, ch;
1150 int16_t input_samples[N];
1151 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
1152 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
1153 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
1154 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
1155 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
1156 int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
1157 int frame_bits;
1159 frame_bits = 0;
1160 for(ch=0;ch<s->nb_all_channels;ch++) {
1161 /* fixed mdct to the six sub blocks & exponent computation */
1162 for(i=0;i<NB_BLOCKS;i++) {
1163 int16_t *sptr;
1164 int sinc;
1166 /* compute input samples */
1167 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t));
1168 sinc = s->nb_all_channels;
1169 sptr = samples + (sinc * (N/2) * i) + ch;
1170 for(j=0;j<N/2;j++) {
1171 v = *sptr;
1172 input_samples[j + N/2] = v;
1173 s->last_samples[ch][j] = v;
1174 sptr += sinc;
1177 /* apply the MDCT window */
1178 for(j=0;j<N/2;j++) {
1179 input_samples[j] = MUL16(input_samples[j],
1180 ff_ac3_window[j]) >> 15;
1181 input_samples[N-j-1] = MUL16(input_samples[N-j-1],
1182 ff_ac3_window[j]) >> 15;
1185 /* Normalize the samples to use the maximum available
1186 precision */
1187 v = 14 - log2_tab(input_samples, N);
1188 if (v < 0)
1189 v = 0;
1190 exp_samples[i][ch] = v - 9;
1191 lshift_tab(input_samples, N, v);
1193 /* do the MDCT */
1194 mdct512(mdct_coef[i][ch], input_samples);
1196 /* compute "exponents". We take into account the
1197 normalization there */
1198 for(j=0;j<N/2;j++) {
1199 int e;
1200 v = abs(mdct_coef[i][ch][j]);
1201 if (v == 0)
1202 e = 24;
1203 else {
1204 e = 23 - av_log2(v) + exp_samples[i][ch];
1205 if (e >= 24) {
1206 e = 24;
1207 mdct_coef[i][ch][j] = 0;
1210 exp[i][ch][j] = e;
1214 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
1216 /* compute the exponents as the decoder will see them. The
1217 EXP_REUSE case must be handled carefully : we select the
1218 min of the exponents */
1219 i = 0;
1220 while (i < NB_BLOCKS) {
1221 j = i + 1;
1222 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
1223 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
1224 j++;
1226 frame_bits += encode_exp(encoded_exp[i][ch],
1227 exp[i][ch], s->nb_coefs[ch],
1228 exp_strategy[i][ch]);
1229 /* copy encoded exponents for reuse case */
1230 for(k=i+1;k<j;k++) {
1231 memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
1232 s->nb_coefs[ch] * sizeof(uint8_t));
1234 i = j;
1238 /* adjust for fractional frame sizes */
1239 while(s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
1240 s->bits_written -= s->bit_rate;
1241 s->samples_written -= s->sample_rate;
1243 s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate);
1244 s->bits_written += s->frame_size * 16;
1245 s->samples_written += AC3_FRAME_SIZE;
1247 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
1248 /* everything is known... let's output the frame */
1249 output_frame_header(s, frame);
1251 for(i=0;i<NB_BLOCKS;i++) {
1252 output_audio_block(s, exp_strategy[i], encoded_exp[i],
1253 bap[i], mdct_coef[i], exp_samples[i], i);
1255 return output_frame_end(s);
1258 static av_cold int AC3_encode_close(AVCodecContext *avctx)
1260 av_freep(&avctx->coded_frame);
1261 return 0;
1264 #if 0
1265 /*************************************************************************/
1266 /* TEST */
1268 #undef random
1269 #define FN (N/4)
1271 void fft_test(void)
1273 IComplex in[FN], in1[FN];
1274 int k, n, i;
1275 float sum_re, sum_im, a;
1277 /* FFT test */
1279 for(i=0;i<FN;i++) {
1280 in[i].re = random() % 65535 - 32767;
1281 in[i].im = random() % 65535 - 32767;
1282 in1[i] = in[i];
1284 fft(in, 7);
1286 /* do it by hand */
1287 for(k=0;k<FN;k++) {
1288 sum_re = 0;
1289 sum_im = 0;
1290 for(n=0;n<FN;n++) {
1291 a = -2 * M_PI * (n * k) / FN;
1292 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1293 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1295 printf("%3d: %6d,%6d %6.0f,%6.0f\n",
1296 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1300 void mdct_test(void)
1302 int16_t input[N];
1303 int32_t output[N/2];
1304 float input1[N];
1305 float output1[N/2];
1306 float s, a, err, e, emax;
1307 int i, k, n;
1309 for(i=0;i<N;i++) {
1310 input[i] = (random() % 65535 - 32767) * 9 / 10;
1311 input1[i] = input[i];
1314 mdct512(output, input);
1316 /* do it by hand */
1317 for(k=0;k<N/2;k++) {
1318 s = 0;
1319 for(n=0;n<N;n++) {
1320 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
1321 s += input1[n] * cos(a);
1323 output1[k] = -2 * s / N;
1326 err = 0;
1327 emax = 0;
1328 for(i=0;i<N/2;i++) {
1329 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
1330 e = output[i] - output1[i];
1331 if (e > emax)
1332 emax = e;
1333 err += e * e;
1335 printf("err2=%f emax=%f\n", err / (N/2), emax);
1338 void test_ac3(void)
1340 AC3EncodeContext ctx;
1341 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
1342 short samples[AC3_FRAME_SIZE];
1343 int ret, i;
1345 AC3_encode_init(&ctx, 44100, 64000, 1);
1347 fft_test();
1348 mdct_test();
1350 for(i=0;i<AC3_FRAME_SIZE;i++)
1351 samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
1352 ret = AC3_encode_frame(&ctx, frame, samples);
1353 printf("ret=%d\n", ret);
1355 #endif
1357 AVCodec ac3_encoder = {
1358 "ac3",
1359 CODEC_TYPE_AUDIO,
1360 CODEC_ID_AC3,
1361 sizeof(AC3EncodeContext),
1362 AC3_encode_init,
1363 AC3_encode_frame,
1364 AC3_encode_close,
1365 NULL,
1366 .long_name = "ATSC A/52 / AC-3",