2 * The simplest AC3 encoder
3 * Copyright (c) 2000 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * The simplest AC3 encoder.
25 //#define DEBUG_BITALLOC
27 #include "bitstream.h"
30 typedef struct AC3EncodeContext
{
36 unsigned int sample_rate
;
38 unsigned int frame_size_min
; /* minimum frame size in case rounding is necessary */
39 unsigned int frame_size
; /* current frame size in words */
41 unsigned int frmsizecod
;
42 unsigned int fscod
; /* frequency */
46 short last_samples
[AC3_MAX_CHANNELS
][256];
47 unsigned int chbwcod
[AC3_MAX_CHANNELS
];
48 int nb_coefs
[AC3_MAX_CHANNELS
];
50 /* bitrate allocation control */
51 int sgaincod
, sdecaycod
, fdecaycod
, dbkneecod
, floorcod
;
52 AC3BitAllocParameters bit_alloc
;
54 int fgaincod
[AC3_MAX_CHANNELS
];
55 int fsnroffst
[AC3_MAX_CHANNELS
];
56 /* mantissa encoding */
57 int mant1_cnt
, mant2_cnt
, mant4_cnt
;
63 #define N (1 << MDCT_NBITS)
65 /* new exponents are sent if their Norm 1 exceed this number */
66 #define EXP_DIFF_THRESHOLD 1000
68 static void fft_init(int ln
);
69 static void ac3_crc_init(void);
71 static inline int16_t fix15(float a
)
74 v
= (int)(a
* (float)(1 << 15));
82 static inline int calc_lowcomp1(int a
, int b0
, int b1
)
84 if ((b0
+ 256) == b1
) {
93 static inline int calc_lowcomp(int a
, int b0
, int b1
, int bin
)
96 if ((b0
+ 256) == b1
) {
102 } else if (bin
< 20) {
103 if ((b0
+ 256) == b1
) {
105 } else if (b0
> b1
) {
116 /* AC3 bit allocation. The algorithm is the one described in the AC3
118 void ac3_parametric_bit_allocation(AC3BitAllocParameters
*s
, uint8_t *bap
,
119 int8_t *exp
, int start
, int end
,
120 int snroffset
, int fgain
, int is_lfe
,
121 int deltbae
,int deltnseg
,
122 uint8_t *deltoffst
, uint8_t *deltlen
, uint8_t *deltba
)
124 int bin
,i
,j
,k
,end1
,v
,v1
,bndstrt
,bndend
,lowcomp
,begin
;
125 int fastleak
,slowleak
,address
,tmp
;
126 int16_t psd
[256]; /* scaled exponents */
127 int16_t bndpsd
[50]; /* interpolated exponents */
128 int16_t excite
[50]; /* excitation */
129 int16_t mask
[50]; /* masking value */
131 /* exponent mapping to PSD */
132 for(bin
=start
;bin
<end
;bin
++) {
133 psd
[bin
]=(3072 - (exp
[bin
] << 7));
136 /* PSD integration */
143 if (end1
> end
) end1
=end
;
144 for(i
=j
;i
<end1
;i
++) {
151 if (adr
> 255) adr
=255;
155 if (adr
> 255) adr
=255;
162 } while (end
> bndtab
[k
]);
164 /* excitation function */
165 bndstrt
= masktab
[start
];
166 bndend
= masktab
[end
-1] + 1;
170 lowcomp
= calc_lowcomp1(lowcomp
, bndpsd
[0], bndpsd
[1]) ;
171 excite
[0] = bndpsd
[0] - fgain
- lowcomp
;
172 lowcomp
= calc_lowcomp1(lowcomp
, bndpsd
[1], bndpsd
[2]) ;
173 excite
[1] = bndpsd
[1] - fgain
- lowcomp
;
175 for (bin
= 2; bin
< 7; bin
++) {
176 if (!(is_lfe
&& bin
== 6))
177 lowcomp
= calc_lowcomp1(lowcomp
, bndpsd
[bin
], bndpsd
[bin
+1]) ;
178 fastleak
= bndpsd
[bin
] - fgain
;
179 slowleak
= bndpsd
[bin
] - s
->sgain
;
180 excite
[bin
] = fastleak
- lowcomp
;
181 if (!(is_lfe
&& bin
== 6)) {
182 if (bndpsd
[bin
] <= bndpsd
[bin
+1]) {
190 if (end1
> 22) end1
=22;
192 for (bin
= begin
; bin
< end1
; bin
++) {
193 if (!(is_lfe
&& bin
== 6))
194 lowcomp
= calc_lowcomp(lowcomp
, bndpsd
[bin
], bndpsd
[bin
+1], bin
) ;
196 fastleak
-= s
->fdecay
;
197 v
= bndpsd
[bin
] - fgain
;
198 if (fastleak
< v
) fastleak
= v
;
200 slowleak
-= s
->sdecay
;
201 v
= bndpsd
[bin
] - s
->sgain
;
202 if (slowleak
< v
) slowleak
= v
;
204 v
=fastleak
- lowcomp
;
205 if (slowleak
> v
) v
=slowleak
;
211 /* coupling channel */
214 fastleak
= (s
->cplfleak
<< 8) + 768;
215 slowleak
= (s
->cplsleak
<< 8) + 768;
218 for (bin
= begin
; bin
< bndend
; bin
++) {
219 fastleak
-= s
->fdecay
;
220 v
= bndpsd
[bin
] - fgain
;
221 if (fastleak
< v
) fastleak
= v
;
222 slowleak
-= s
->sdecay
;
223 v
= bndpsd
[bin
] - s
->sgain
;
224 if (slowleak
< v
) slowleak
= v
;
227 if (slowleak
> v
) v
= slowleak
;
231 /* compute masking curve */
233 for (bin
= bndstrt
; bin
< bndend
; bin
++) {
235 tmp
= s
->dbknee
- bndpsd
[bin
];
239 v
=hth
[bin
>> s
->halfratecod
][s
->fscod
];
244 /* delta bit allocation */
246 if (deltbae
== 0 || deltbae
== 1) {
247 int band
, seg
, delta
;
249 for (seg
= 0; seg
< deltnseg
; seg
++) {
250 band
+= deltoffst
[seg
] ;
251 if (deltba
[seg
] >= 4) {
252 delta
= (deltba
[seg
] - 3) << 7;
254 delta
= (deltba
[seg
] - 4) << 7;
256 for (k
= 0; k
< deltlen
[seg
]; k
++) {
257 mask
[band
] += delta
;
263 /* compute bit allocation */
275 end1
=bndtab
[j
] + bndsz
[j
];
276 if (end1
> end
) end1
=end
;
278 for (k
= i
; k
< end1
; k
++) {
279 address
= (psd
[i
] - v
) >> 5 ;
280 if (address
< 0) address
=0;
281 else if (address
> 63) address
=63;
282 bap
[i
] = baptab
[address
];
285 } while (end
> bndtab
[j
++]) ;
288 typedef struct IComplex
{
292 static void fft_init(int ln
)
299 for(i
=0;i
<(n
/2);i
++) {
300 alpha
= 2 * M_PI
* (float)i
/ (float)n
;
301 costab
[i
] = fix15(cos(alpha
));
302 sintab
[i
] = fix15(sin(alpha
));
308 m
|= ((i
>> j
) & 1) << (ln
-j
-1);
315 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
322 pre = (bx + ax) >> 1;\
323 pim = (by + ay) >> 1;\
324 qre = (bx - ax) >> 1;\
325 qim = (by - ay) >> 1;\
328 #define MUL16(a,b) ((a) * (b))
330 #define CMUL(pre, pim, are, aim, bre, bim) \
332 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
333 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
337 /* do a 2^n point complex fft on 2^ln points. */
338 static void fft(IComplex
*z
, int ln
)
342 register IComplex
*p
,*q
;
364 BF(p
[0].re
, p
[0].im
, p
[1].re
, p
[1].im
,
365 p
[0].re
, p
[0].im
, p
[1].re
, p
[1].im
);
374 BF(p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
,
375 p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
);
376 BF(p
[1].re
, p
[1].im
, p
[3].re
, p
[3].im
,
377 p
[1].re
, p
[1].im
, p
[3].im
, -p
[3].re
);
389 for (j
= 0; j
< nblocks
; ++j
) {
391 BF(p
->re
, p
->im
, q
->re
, q
->im
,
392 p
->re
, p
->im
, q
->re
, q
->im
);
396 for(l
= nblocks
; l
< np2
; l
+= nblocks
) {
397 CMUL(tmp_re
, tmp_im
, costab
[l
], -sintab
[l
], q
->re
, q
->im
);
398 BF(p
->re
, p
->im
, q
->re
, q
->im
,
399 p
->re
, p
->im
, tmp_re
, tmp_im
);
406 nblocks
= nblocks
>> 1;
407 nloops
= nloops
<< 1;
408 } while (nblocks
!= 0);
411 /* do a 512 point mdct */
412 static void mdct512(int32_t *out
, int16_t *in
)
414 int i
, re
, im
, re1
, im1
;
418 /* shift to simplify computations */
420 rot
[i
] = -in
[i
+ 3*N
/4];
422 rot
[i
] = in
[i
- N
/4];
426 re
= ((int)rot
[2*i
] - (int)rot
[N
-1-2*i
]) >> 1;
427 im
= -((int)rot
[N
/2+2*i
] - (int)rot
[N
/2-1-2*i
]) >> 1;
428 CMUL(x
[i
].re
, x
[i
].im
, re
, im
, -xcos1
[i
], xsin1
[i
]);
431 fft(x
, MDCT_NBITS
- 2);
437 CMUL(re1
, im1
, re
, im
, xsin1
[i
], xcos1
[i
]);
439 out
[N
/2-1-2*i
] = re1
;
443 /* XXX: use another norm ? */
444 static int calc_exp_diff(uint8_t *exp1
, uint8_t *exp2
, int n
)
449 sum
+= abs(exp1
[i
] - exp2
[i
]);
454 static void compute_exp_strategy(uint8_t exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
],
455 uint8_t exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
461 /* estimate if the exponent variation & decide if they should be
462 reused in the next frame */
463 exp_strategy
[0][ch
] = EXP_NEW
;
464 for(i
=1;i
<NB_BLOCKS
;i
++) {
465 exp_diff
= calc_exp_diff(exp
[i
][ch
], exp
[i
-1][ch
], N
/2);
467 av_log(NULL
, AV_LOG_DEBUG
, "exp_diff=%d\n", exp_diff
);
469 if (exp_diff
> EXP_DIFF_THRESHOLD
)
470 exp_strategy
[i
][ch
] = EXP_NEW
;
472 exp_strategy
[i
][ch
] = EXP_REUSE
;
477 /* now select the encoding strategy type : if exponents are often
478 recoded, we use a coarse encoding */
480 while (i
< NB_BLOCKS
) {
482 while (j
< NB_BLOCKS
&& exp_strategy
[j
][ch
] == EXP_REUSE
)
486 exp_strategy
[i
][ch
] = EXP_D45
;
490 exp_strategy
[i
][ch
] = EXP_D25
;
493 exp_strategy
[i
][ch
] = EXP_D15
;
500 /* set exp[i] to min(exp[i], exp1[i]) */
501 static void exponent_min(uint8_t exp
[N
/2], uint8_t exp1
[N
/2], int n
)
506 if (exp1
[i
] < exp
[i
])
511 /* update the exponents so that they are the ones the decoder will
512 decode. Return the number of bits used to code the exponents */
513 static int encode_exp(uint8_t encoded_exp
[N
/2],
518 int group_size
, nb_groups
, i
, j
, k
, exp_min
;
521 switch(exp_strategy
) {
533 nb_groups
= ((nb_exps
+ (group_size
* 3) - 4) / (3 * group_size
)) * 3;
535 /* for each group, compute the minimum exponent */
536 exp1
[0] = exp
[0]; /* DC exponent is handled separately */
538 for(i
=1;i
<=nb_groups
;i
++) {
540 assert(exp_min
>= 0 && exp_min
<= 24);
541 for(j
=1;j
<group_size
;j
++) {
542 if (exp
[k
+j
] < exp_min
)
549 /* constraint for DC exponent */
553 /* Decrease the delta between each groups to within 2
554 * so that they can be differentially encoded */
555 for (i
=1;i
<=nb_groups
;i
++)
556 exp1
[i
] = FFMIN(exp1
[i
], exp1
[i
-1] + 2);
557 for (i
=nb_groups
-1;i
>=0;i
--)
558 exp1
[i
] = FFMIN(exp1
[i
], exp1
[i
+1] + 2);
560 /* now we have the exponent values the decoder will see */
561 encoded_exp
[0] = exp1
[0];
563 for(i
=1;i
<=nb_groups
;i
++) {
564 for(j
=0;j
<group_size
;j
++) {
565 encoded_exp
[k
+j
] = exp1
[i
];
571 av_log(NULL
, AV_LOG_DEBUG
, "exponents: strategy=%d\n", exp_strategy
);
572 for(i
=0;i
<=nb_groups
* group_size
;i
++) {
573 av_log(NULL
, AV_LOG_DEBUG
, "%d ", encoded_exp
[i
]);
575 av_log(NULL
, AV_LOG_DEBUG
, "\n");
578 return 4 + (nb_groups
/ 3) * 7;
581 /* return the size in bits taken by the mantissa */
582 static int compute_mantissa_size(AC3EncodeContext
*s
, uint8_t *m
, int nb_coefs
)
587 for(i
=0;i
<nb_coefs
;i
++) {
594 /* 3 mantissa in 5 bits */
595 if (s
->mant1_cnt
== 0)
597 if (++s
->mant1_cnt
== 3)
601 /* 3 mantissa in 7 bits */
602 if (s
->mant2_cnt
== 0)
604 if (++s
->mant2_cnt
== 3)
611 /* 2 mantissa in 7 bits */
612 if (s
->mant4_cnt
== 0)
614 if (++s
->mant4_cnt
== 2)
632 static int bit_alloc(AC3EncodeContext
*s
,
633 uint8_t bap
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
634 uint8_t encoded_exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
635 uint8_t exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
],
636 int frame_bits
, int csnroffst
, int fsnroffst
)
641 for(i
=0;i
<NB_BLOCKS
;i
++) {
645 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
646 ac3_parametric_bit_allocation(&s
->bit_alloc
,
647 bap
[i
][ch
], (int8_t *)encoded_exp
[i
][ch
],
649 (((csnroffst
-15) << 4) +
651 fgaintab
[s
->fgaincod
[ch
]],
652 ch
== s
->lfe_channel
,
653 2, 0, NULL
, NULL
, NULL
);
654 frame_bits
+= compute_mantissa_size(s
, bap
[i
][ch
],
659 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
660 csnroffst
, fsnroffst
, frame_bits
,
661 16 * s
->frame_size
- ((frame_bits
+ 7) & ~7));
663 return 16 * s
->frame_size
- frame_bits
;
668 static int compute_bit_allocation(AC3EncodeContext
*s
,
669 uint8_t bap
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
670 uint8_t encoded_exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
671 uint8_t exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
],
675 int csnroffst
, fsnroffst
;
676 uint8_t bap1
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
677 static int frame_bits_inc
[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
679 /* init default parameters */
685 for(ch
=0;ch
<s
->nb_all_channels
;ch
++)
688 /* compute real values */
689 s
->bit_alloc
.fscod
= s
->fscod
;
690 s
->bit_alloc
.halfratecod
= s
->halfratecod
;
691 s
->bit_alloc
.sdecay
= sdecaytab
[s
->sdecaycod
] >> s
->halfratecod
;
692 s
->bit_alloc
.fdecay
= fdecaytab
[s
->fdecaycod
] >> s
->halfratecod
;
693 s
->bit_alloc
.sgain
= sgaintab
[s
->sgaincod
];
694 s
->bit_alloc
.dbknee
= dbkneetab
[s
->dbkneecod
];
695 s
->bit_alloc
.floor
= floortab
[s
->floorcod
];
699 // if (s->acmod == 2)
701 frame_bits
+= frame_bits_inc
[s
->acmod
];
704 for(i
=0;i
<NB_BLOCKS
;i
++) {
705 frame_bits
+= s
->nb_channels
* 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
707 frame_bits
++; /* rematstr */
708 if(i
==0) frame_bits
+= 4;
710 frame_bits
+= 2 * s
->nb_channels
; /* chexpstr[2] * c */
712 frame_bits
++; /* lfeexpstr */
713 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
714 if (exp_strategy
[i
][ch
] != EXP_REUSE
)
715 frame_bits
+= 6 + 2; /* chbwcod[6], gainrng[2] */
717 frame_bits
++; /* baie */
718 frame_bits
++; /* snr */
719 frame_bits
+= 2; /* delta / skip */
721 frame_bits
++; /* cplinu for block 0 */
723 /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
725 /* (fsnoffset[4] + fgaincod[4]) * c */
726 frame_bits
+= 2*4 + 3 + 6 + s
->nb_all_channels
* (4 + 3);
728 /* auxdatae, crcrsv */
734 /* now the big work begins : do the bit allocation. Modify the snr
735 offset until we can pack everything in the requested frame size */
737 csnroffst
= s
->csnroffst
;
738 while (csnroffst
>= 0 &&
739 bit_alloc(s
, bap
, encoded_exp
, exp_strategy
, frame_bits
, csnroffst
, 0) < 0)
740 csnroffst
-= SNR_INC1
;
742 av_log(NULL
, AV_LOG_ERROR
, "Yack, Error !!!\n");
745 while ((csnroffst
+ SNR_INC1
) <= 63 &&
746 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
,
747 csnroffst
+ SNR_INC1
, 0) >= 0) {
748 csnroffst
+= SNR_INC1
;
749 memcpy(bap
, bap1
, sizeof(bap1
));
751 while ((csnroffst
+ 1) <= 63 &&
752 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
, csnroffst
+ 1, 0) >= 0) {
754 memcpy(bap
, bap1
, sizeof(bap1
));
758 while ((fsnroffst
+ SNR_INC1
) <= 15 &&
759 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
,
760 csnroffst
, fsnroffst
+ SNR_INC1
) >= 0) {
761 fsnroffst
+= SNR_INC1
;
762 memcpy(bap
, bap1
, sizeof(bap1
));
764 while ((fsnroffst
+ 1) <= 15 &&
765 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
,
766 csnroffst
, fsnroffst
+ 1) >= 0) {
768 memcpy(bap
, bap1
, sizeof(bap1
));
771 s
->csnroffst
= csnroffst
;
772 for(ch
=0;ch
<s
->nb_all_channels
;ch
++)
773 s
->fsnroffst
[ch
] = fsnroffst
;
774 #if defined(DEBUG_BITALLOC)
779 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
780 printf("Block #%d Ch%d:\n", i
, ch
);
782 for(j
=0;j
<s
->nb_coefs
[ch
];j
++) {
783 printf("%d ",bap
[i
][ch
][j
]);
793 void ac3_common_init(void)
796 /* compute bndtab and masktab from bandsz */
802 for(j
=0;j
<v
;j
++) masktab
[k
++]=i
;
809 static int AC3_encode_init(AVCodecContext
*avctx
)
811 int freq
= avctx
->sample_rate
;
812 int bitrate
= avctx
->bit_rate
;
813 int channels
= avctx
->channels
;
814 AC3EncodeContext
*s
= avctx
->priv_data
;
817 static const uint8_t acmod_defs
[6] = {
821 0x06, /* L R SL SR */
822 0x07, /* L C R SL SR */
823 0x07, /* L C R SL SR (+LFE) */
826 avctx
->frame_size
= AC3_FRAME_SIZE
;
828 /* number of channels */
829 if (channels
< 1 || channels
> 6)
831 s
->acmod
= acmod_defs
[channels
- 1];
832 s
->lfe
= (channels
== 6) ? 1 : 0;
833 s
->nb_all_channels
= channels
;
834 s
->nb_channels
= channels
> 5 ? 5 : channels
;
835 s
->lfe_channel
= s
->lfe
? 5 : -1;
840 if ((ac3_freqs
[j
] >> i
) == freq
)
845 s
->sample_rate
= freq
;
848 s
->bsid
= 8 + s
->halfratecod
;
849 s
->bsmod
= 0; /* complete main audio service */
851 /* bitrate & frame size */
854 if ((ac3_bitratetab
[i
] >> s
->halfratecod
) == bitrate
)
859 s
->bit_rate
= bitrate
;
860 s
->frmsizecod
= i
<< 1;
861 s
->frame_size_min
= (bitrate
* 1000 * AC3_FRAME_SIZE
) / (freq
* 16);
862 /* for now we do not handle fractional sizes */
863 s
->frame_size
= s
->frame_size_min
;
865 /* bit allocation init */
866 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
867 /* bandwidth for each channel */
868 /* XXX: should compute the bandwidth according to the frame
869 size, so that we avoid anoying high freq artefacts */
870 s
->chbwcod
[ch
] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
871 s
->nb_coefs
[ch
] = ((s
->chbwcod
[ch
] + 12) * 3) + 37;
874 s
->nb_coefs
[s
->lfe_channel
] = 7; /* fixed */
876 /* initial snr offset */
882 fft_init(MDCT_NBITS
- 2);
884 alpha
= 2 * M_PI
* (i
+ 1.0 / 8.0) / (float)N
;
885 xcos1
[i
] = fix15(-cos(alpha
));
886 xsin1
[i
] = fix15(-sin(alpha
));
891 avctx
->coded_frame
= avcodec_alloc_frame();
892 avctx
->coded_frame
->key_frame
= 1;
897 /* output the AC3 frame header */
898 static void output_frame_header(AC3EncodeContext
*s
, unsigned char *frame
)
900 init_put_bits(&s
->pb
, frame
, AC3_MAX_CODED_FRAME_SIZE
);
902 put_bits(&s
->pb
, 16, 0x0b77); /* frame header */
903 put_bits(&s
->pb
, 16, 0); /* crc1: will be filled later */
904 put_bits(&s
->pb
, 2, s
->fscod
);
905 put_bits(&s
->pb
, 6, s
->frmsizecod
+ (s
->frame_size
- s
->frame_size_min
));
906 put_bits(&s
->pb
, 5, s
->bsid
);
907 put_bits(&s
->pb
, 3, s
->bsmod
);
908 put_bits(&s
->pb
, 3, s
->acmod
);
909 if ((s
->acmod
& 0x01) && s
->acmod
!= 0x01)
910 put_bits(&s
->pb
, 2, 1); /* XXX -4.5 dB */
912 put_bits(&s
->pb
, 2, 1); /* XXX -6 dB */
913 if (s
->acmod
== 0x02)
914 put_bits(&s
->pb
, 2, 0); /* surround not indicated */
915 put_bits(&s
->pb
, 1, s
->lfe
); /* LFE */
916 put_bits(&s
->pb
, 5, 31); /* dialog norm: -31 db */
917 put_bits(&s
->pb
, 1, 0); /* no compression control word */
918 put_bits(&s
->pb
, 1, 0); /* no lang code */
919 put_bits(&s
->pb
, 1, 0); /* no audio production info */
920 put_bits(&s
->pb
, 1, 0); /* no copyright */
921 put_bits(&s
->pb
, 1, 1); /* original bitstream */
922 put_bits(&s
->pb
, 1, 0); /* no time code 1 */
923 put_bits(&s
->pb
, 1, 0); /* no time code 2 */
924 put_bits(&s
->pb
, 1, 0); /* no addtional bit stream info */
927 /* symetric quantization on 'levels' levels */
928 static inline int sym_quant(int c
, int e
, int levels
)
933 v
= (levels
* (c
<< e
)) >> 24;
935 v
= (levels
>> 1) + v
;
937 v
= (levels
* ((-c
) << e
)) >> 24;
939 v
= (levels
>> 1) - v
;
941 assert (v
>= 0 && v
< levels
);
945 /* asymetric quantization on 2^qbits levels */
946 static inline int asym_quant(int c
, int e
, int qbits
)
950 lshift
= e
+ qbits
- 24;
957 m
= (1 << (qbits
-1));
961 return v
& ((1 << qbits
)-1);
964 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
966 static void output_audio_block(AC3EncodeContext
*s
,
967 uint8_t exp_strategy
[AC3_MAX_CHANNELS
],
968 uint8_t encoded_exp
[AC3_MAX_CHANNELS
][N
/2],
969 uint8_t bap
[AC3_MAX_CHANNELS
][N
/2],
970 int32_t mdct_coefs
[AC3_MAX_CHANNELS
][N
/2],
971 int8_t global_exp
[AC3_MAX_CHANNELS
],
974 int ch
, nb_groups
, group_size
, i
, baie
, rbnd
;
976 uint16_t qmant
[AC3_MAX_CHANNELS
][N
/2];
978 int mant1_cnt
, mant2_cnt
, mant4_cnt
;
979 uint16_t *qmant1_ptr
, *qmant2_ptr
, *qmant4_ptr
;
980 int delta0
, delta1
, delta2
;
982 for(ch
=0;ch
<s
->nb_channels
;ch
++)
983 put_bits(&s
->pb
, 1, 0); /* 512 point MDCT */
984 for(ch
=0;ch
<s
->nb_channels
;ch
++)
985 put_bits(&s
->pb
, 1, 1); /* no dither */
986 put_bits(&s
->pb
, 1, 0); /* no dynamic range */
987 if (block_num
== 0) {
988 /* for block 0, even if no coupling, we must say it. This is a
990 put_bits(&s
->pb
, 1, 1); /* coupling strategy present */
991 put_bits(&s
->pb
, 1, 0); /* no coupling strategy */
993 put_bits(&s
->pb
, 1, 0); /* no new coupling strategy */
1000 /* first block must define rematrixing (rematstr) */
1001 put_bits(&s
->pb
, 1, 1);
1003 /* dummy rematrixing rematflg(1:4)=0 */
1004 for (rbnd
=0;rbnd
<4;rbnd
++)
1005 put_bits(&s
->pb
, 1, 0);
1009 /* no matrixing (but should be used in the future) */
1010 put_bits(&s
->pb
, 1, 0);
1016 static int count
= 0;
1017 av_log(NULL
, AV_LOG_DEBUG
, "Block #%d (%d)\n", block_num
, count
++);
1020 /* exponent strategy */
1021 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1022 put_bits(&s
->pb
, 2, exp_strategy
[ch
]);
1026 put_bits(&s
->pb
, 1, exp_strategy
[s
->lfe_channel
]);
1029 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1030 if (exp_strategy
[ch
] != EXP_REUSE
)
1031 put_bits(&s
->pb
, 6, s
->chbwcod
[ch
]);
1035 for (ch
= 0; ch
< s
->nb_all_channels
; ch
++) {
1036 switch(exp_strategy
[ch
]) {
1050 nb_groups
= (s
->nb_coefs
[ch
] + (group_size
* 3) - 4) / (3 * group_size
);
1051 p
= encoded_exp
[ch
];
1053 /* first exponent */
1055 put_bits(&s
->pb
, 4, exp1
);
1057 /* next ones are delta encoded */
1058 for(i
=0;i
<nb_groups
;i
++) {
1059 /* merge three delta in one code */
1063 delta0
= exp1
- exp0
+ 2;
1068 delta1
= exp1
- exp0
+ 2;
1073 delta2
= exp1
- exp0
+ 2;
1075 put_bits(&s
->pb
, 7, ((delta0
* 5 + delta1
) * 5) + delta2
);
1078 if (ch
!= s
->lfe_channel
)
1079 put_bits(&s
->pb
, 2, 0); /* no gain range info */
1082 /* bit allocation info */
1083 baie
= (block_num
== 0);
1084 put_bits(&s
->pb
, 1, baie
);
1086 put_bits(&s
->pb
, 2, s
->sdecaycod
);
1087 put_bits(&s
->pb
, 2, s
->fdecaycod
);
1088 put_bits(&s
->pb
, 2, s
->sgaincod
);
1089 put_bits(&s
->pb
, 2, s
->dbkneecod
);
1090 put_bits(&s
->pb
, 3, s
->floorcod
);
1094 put_bits(&s
->pb
, 1, baie
); /* always present with bai */
1096 put_bits(&s
->pb
, 6, s
->csnroffst
);
1097 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
1098 put_bits(&s
->pb
, 4, s
->fsnroffst
[ch
]);
1099 put_bits(&s
->pb
, 3, s
->fgaincod
[ch
]);
1103 put_bits(&s
->pb
, 1, 0); /* no delta bit allocation */
1104 put_bits(&s
->pb
, 1, 0); /* no data to skip */
1106 /* mantissa encoding : we use two passes to handle the grouping. A
1107 one pass method may be faster, but it would necessitate to
1108 modify the output stream. */
1110 /* first pass: quantize */
1111 mant1_cnt
= mant2_cnt
= mant4_cnt
= 0;
1112 qmant1_ptr
= qmant2_ptr
= qmant4_ptr
= NULL
;
1114 for (ch
= 0; ch
< s
->nb_all_channels
; ch
++) {
1117 for(i
=0;i
<s
->nb_coefs
[ch
];i
++) {
1118 c
= mdct_coefs
[ch
][i
];
1119 e
= encoded_exp
[ch
][i
] - global_exp
[ch
];
1126 v
= sym_quant(c
, e
, 3);
1129 qmant1_ptr
= &qmant
[ch
][i
];
1134 *qmant1_ptr
+= 3 * v
;
1146 v
= sym_quant(c
, e
, 5);
1149 qmant2_ptr
= &qmant
[ch
][i
];
1154 *qmant2_ptr
+= 5 * v
;
1166 v
= sym_quant(c
, e
, 7);
1169 v
= sym_quant(c
, e
, 11);
1172 qmant4_ptr
= &qmant
[ch
][i
];
1184 v
= sym_quant(c
, e
, 15);
1187 v
= asym_quant(c
, e
, 14);
1190 v
= asym_quant(c
, e
, 16);
1193 v
= asym_quant(c
, e
, b
- 1);
1200 /* second pass : output the values */
1201 for (ch
= 0; ch
< s
->nb_all_channels
; ch
++) {
1204 for(i
=0;i
<s
->nb_coefs
[ch
];i
++) {
1212 put_bits(&s
->pb
, 5, q
);
1216 put_bits(&s
->pb
, 7, q
);
1219 put_bits(&s
->pb
, 3, q
);
1223 put_bits(&s
->pb
, 7, q
);
1226 put_bits(&s
->pb
, 14, q
);
1229 put_bits(&s
->pb
, 16, q
);
1232 put_bits(&s
->pb
, b
- 1, q
);
1239 /* compute the ac3 crc */
1241 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1243 static void ac3_crc_init(void)
1245 unsigned int c
, n
, k
;
1247 for(n
=0;n
<256;n
++) {
1249 for (k
= 0; k
< 8; k
++) {
1251 c
= ((c
<< 1) & 0xffff) ^ (CRC16_POLY
& 0xffff);
1259 static unsigned int ac3_crc(uint8_t *data
, int n
, unsigned int crc
)
1263 crc
= (crc_table
[data
[i
] ^ (crc
>> 8)] ^ (crc
<< 8)) & 0xffff;
1268 static unsigned int mul_poly(unsigned int a
, unsigned int b
, unsigned int poly
)
1284 static unsigned int pow_poly(unsigned int a
, unsigned int n
, unsigned int poly
)
1290 r
= mul_poly(r
, a
, poly
);
1291 a
= mul_poly(a
, a
, poly
);
1298 /* compute log2(max(abs(tab[]))) */
1299 static int log2_tab(int16_t *tab
, int n
)
1310 static void lshift_tab(int16_t *tab
, int n
, int lshift
)
1318 } else if (lshift
< 0) {
1326 /* fill the end of the frame and compute the two crcs */
1327 static int output_frame_end(AC3EncodeContext
*s
)
1329 int frame_size
, frame_size_58
, n
, crc1
, crc2
, crc_inv
;
1332 frame_size
= s
->frame_size
; /* frame size in words */
1333 /* align to 8 bits */
1334 flush_put_bits(&s
->pb
);
1335 /* add zero bytes to reach the frame size */
1337 n
= 2 * s
->frame_size
- (pbBufPtr(&s
->pb
) - frame
) - 2;
1340 memset(pbBufPtr(&s
->pb
), 0, n
);
1342 /* Now we must compute both crcs : this is not so easy for crc1
1343 because it is at the beginning of the data... */
1344 frame_size_58
= (frame_size
>> 1) + (frame_size
>> 3);
1345 crc1
= ac3_crc(frame
+ 4, (2 * frame_size_58
) - 4, 0);
1346 /* XXX: could precompute crc_inv */
1347 crc_inv
= pow_poly((CRC16_POLY
>> 1), (16 * frame_size_58
) - 16, CRC16_POLY
);
1348 crc1
= mul_poly(crc_inv
, crc1
, CRC16_POLY
);
1349 frame
[2] = crc1
>> 8;
1352 crc2
= ac3_crc(frame
+ 2 * frame_size_58
, (frame_size
- frame_size_58
) * 2 - 2, 0);
1353 frame
[2*frame_size
- 2] = crc2
>> 8;
1354 frame
[2*frame_size
- 1] = crc2
;
1356 // printf("n=%d frame_size=%d\n", n, frame_size);
1357 return frame_size
* 2;
1360 static int AC3_encode_frame(AVCodecContext
*avctx
,
1361 unsigned char *frame
, int buf_size
, void *data
)
1363 AC3EncodeContext
*s
= avctx
->priv_data
;
1364 int16_t *samples
= data
;
1366 int16_t input_samples
[N
];
1367 int32_t mdct_coef
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1368 uint8_t exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1369 uint8_t exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
];
1370 uint8_t encoded_exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1371 uint8_t bap
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1372 int8_t exp_samples
[NB_BLOCKS
][AC3_MAX_CHANNELS
];
1376 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
1377 /* fixed mdct to the six sub blocks & exponent computation */
1378 for(i
=0;i
<NB_BLOCKS
;i
++) {
1382 /* compute input samples */
1383 memcpy(input_samples
, s
->last_samples
[ch
], N
/2 * sizeof(int16_t));
1384 sinc
= s
->nb_all_channels
;
1385 sptr
= samples
+ (sinc
* (N
/2) * i
) + ch
;
1386 for(j
=0;j
<N
/2;j
++) {
1388 input_samples
[j
+ N
/2] = v
;
1389 s
->last_samples
[ch
][j
] = v
;
1393 /* apply the MDCT window */
1394 for(j
=0;j
<N
/2;j
++) {
1395 input_samples
[j
] = MUL16(input_samples
[j
],
1396 ac3_window
[j
]) >> 15;
1397 input_samples
[N
-j
-1] = MUL16(input_samples
[N
-j
-1],
1398 ac3_window
[j
]) >> 15;
1401 /* Normalize the samples to use the maximum available
1403 v
= 14 - log2_tab(input_samples
, N
);
1406 exp_samples
[i
][ch
] = v
- 8;
1407 lshift_tab(input_samples
, N
, v
);
1410 mdct512(mdct_coef
[i
][ch
], input_samples
);
1412 /* compute "exponents". We take into account the
1413 normalization there */
1414 for(j
=0;j
<N
/2;j
++) {
1416 v
= abs(mdct_coef
[i
][ch
][j
]);
1420 e
= 23 - av_log2(v
) + exp_samples
[i
][ch
];
1423 mdct_coef
[i
][ch
][j
] = 0;
1430 compute_exp_strategy(exp_strategy
, exp
, ch
, ch
== s
->lfe_channel
);
1432 /* compute the exponents as the decoder will see them. The
1433 EXP_REUSE case must be handled carefully : we select the
1434 min of the exponents */
1436 while (i
< NB_BLOCKS
) {
1438 while (j
< NB_BLOCKS
&& exp_strategy
[j
][ch
] == EXP_REUSE
) {
1439 exponent_min(exp
[i
][ch
], exp
[j
][ch
], s
->nb_coefs
[ch
]);
1442 frame_bits
+= encode_exp(encoded_exp
[i
][ch
],
1443 exp
[i
][ch
], s
->nb_coefs
[ch
],
1444 exp_strategy
[i
][ch
]);
1445 /* copy encoded exponents for reuse case */
1446 for(k
=i
+1;k
<j
;k
++) {
1447 memcpy(encoded_exp
[k
][ch
], encoded_exp
[i
][ch
],
1448 s
->nb_coefs
[ch
] * sizeof(uint8_t));
1454 compute_bit_allocation(s
, bap
, encoded_exp
, exp_strategy
, frame_bits
);
1455 /* everything is known... let's output the frame */
1456 output_frame_header(s
, frame
);
1458 for(i
=0;i
<NB_BLOCKS
;i
++) {
1459 output_audio_block(s
, exp_strategy
[i
], encoded_exp
[i
],
1460 bap
[i
], mdct_coef
[i
], exp_samples
[i
], i
);
1462 return output_frame_end(s
);
1465 static int AC3_encode_close(AVCodecContext
*avctx
)
1467 av_freep(&avctx
->coded_frame
);
1472 /*************************************************************************/
1479 IComplex in
[FN
], in1
[FN
];
1481 float sum_re
, sum_im
, a
;
1486 in
[i
].re
= random() % 65535 - 32767;
1487 in
[i
].im
= random() % 65535 - 32767;
1497 a
= -2 * M_PI
* (n
* k
) / FN
;
1498 sum_re
+= in1
[n
].re
* cos(a
) - in1
[n
].im
* sin(a
);
1499 sum_im
+= in1
[n
].re
* sin(a
) + in1
[n
].im
* cos(a
);
1501 printf("%3d: %6d,%6d %6.0f,%6.0f\n",
1502 k
, in
[k
].re
, in
[k
].im
, sum_re
/ FN
, sum_im
/ FN
);
1506 void mdct_test(void)
1509 int32_t output
[N
/2];
1512 float s
, a
, err
, e
, emax
;
1516 input
[i
] = (random() % 65535 - 32767) * 9 / 10;
1517 input1
[i
] = input
[i
];
1520 mdct512(output
, input
);
1523 for(k
=0;k
<N
/2;k
++) {
1526 a
= (2*M_PI
*(2*n
+1+N
/2)*(2*k
+1) / (4 * N
));
1527 s
+= input1
[n
] * cos(a
);
1529 output1
[k
] = -2 * s
/ N
;
1534 for(i
=0;i
<N
/2;i
++) {
1535 printf("%3d: %7d %7.0f\n", i
, output
[i
], output1
[i
]);
1536 e
= output
[i
] - output1
[i
];
1541 printf("err2=%f emax=%f\n", err
/ (N
/2), emax
);
1546 AC3EncodeContext ctx
;
1547 unsigned char frame
[AC3_MAX_CODED_FRAME_SIZE
];
1548 short samples
[AC3_FRAME_SIZE
];
1551 AC3_encode_init(&ctx
, 44100, 64000, 1);
1556 for(i
=0;i
<AC3_FRAME_SIZE
;i
++)
1557 samples
[i
] = (int)(sin(2*M_PI
*i
*1000.0/44100) * 10000);
1558 ret
= AC3_encode_frame(&ctx
, frame
, samples
);
1559 printf("ret=%d\n", ret
);
1563 AVCodec ac3_encoder
= {
1567 sizeof(AC3EncodeContext
),