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
20 //#define DEBUG_BITALLOC
25 typedef struct AC3EncodeContext
{
33 int frame_size_min
; /* minimum frame size in case rounding is necessary */
34 int frame_size
; /* current frame size in words */
37 int fscod
; /* frequency */
41 short last_samples
[AC3_MAX_CHANNELS
][256];
42 int chbwcod
[AC3_MAX_CHANNELS
];
43 int nb_coefs
[AC3_MAX_CHANNELS
];
45 /* bitrate allocation control */
46 int sgaincod
, sdecaycod
, fdecaycod
, dbkneecod
, floorcod
;
47 AC3BitAllocParameters bit_alloc
;
49 int fgaincod
[AC3_MAX_CHANNELS
];
50 int fsnroffst
[AC3_MAX_CHANNELS
];
51 /* mantissa encoding */
52 int mant1_cnt
, mant2_cnt
, mant4_cnt
;
58 #define N (1 << MDCT_NBITS)
60 /* new exponents are sent if their Norm 1 exceed this number */
61 #define EXP_DIFF_THRESHOLD 1000
63 static void fft_init(int ln
);
64 static void ac3_crc_init(void);
66 static inline INT16
fix15(float a
)
69 v
= (int)(a
* (float)(1 << 15));
77 static inline int calc_lowcomp1(int a
, int b0
, int b1
)
79 if ((b0
+ 256) == b1
) {
88 static inline int calc_lowcomp(int a
, int b0
, int b1
, int bin
)
91 if ((b0
+ 256) == b1
) {
97 } else if (bin
< 20) {
98 if ((b0
+ 256) == b1
) {
100 } else if (b0
> b1
) {
111 /* AC3 bit allocation. The algorithm is the one described in the AC3
113 void ac3_parametric_bit_allocation(AC3BitAllocParameters
*s
, UINT8
*bap
,
114 INT8
*exp
, int start
, int end
,
115 int snroffset
, int fgain
, int is_lfe
,
116 int deltbae
,int deltnseg
,
117 UINT8
*deltoffst
, UINT8
*deltlen
, UINT8
*deltba
)
119 int bin
,i
,j
,k
,end1
,v
,v1
,bndstrt
,bndend
,lowcomp
,begin
;
120 int fastleak
,slowleak
,address
,tmp
;
121 INT16 psd
[256]; /* scaled exponents */
122 INT16 bndpsd
[50]; /* interpolated exponents */
123 INT16 excite
[50]; /* excitation */
124 INT16 mask
[50]; /* masking value */
126 /* exponent mapping to PSD */
127 for(bin
=start
;bin
<end
;bin
++) {
128 psd
[bin
]=(3072 - (exp
[bin
] << 7));
131 /* PSD integration */
138 if (end1
> end
) end1
=end
;
139 for(i
=j
;i
<end1
;i
++) {
146 if (adr
> 255) adr
=255;
150 if (adr
> 255) adr
=255;
157 } while (end
> bndtab
[k
]);
159 /* excitation function */
160 bndstrt
= masktab
[start
];
161 bndend
= masktab
[end
-1] + 1;
165 lowcomp
= calc_lowcomp1(lowcomp
, bndpsd
[0], bndpsd
[1]) ;
166 excite
[0] = bndpsd
[0] - fgain
- lowcomp
;
167 lowcomp
= calc_lowcomp1(lowcomp
, bndpsd
[1], bndpsd
[2]) ;
168 excite
[1] = bndpsd
[1] - fgain
- lowcomp
;
170 for (bin
= 2; bin
< 7; bin
++) {
171 if (!(is_lfe
&& bin
== 6))
172 lowcomp
= calc_lowcomp1(lowcomp
, bndpsd
[bin
], bndpsd
[bin
+1]) ;
173 fastleak
= bndpsd
[bin
] - fgain
;
174 slowleak
= bndpsd
[bin
] - s
->sgain
;
175 excite
[bin
] = fastleak
- lowcomp
;
176 if (!(is_lfe
&& bin
== 6)) {
177 if (bndpsd
[bin
] <= bndpsd
[bin
+1]) {
185 if (end1
> 22) end1
=22;
187 for (bin
= begin
; bin
< end1
; bin
++) {
188 if (!(is_lfe
&& bin
== 6))
189 lowcomp
= calc_lowcomp(lowcomp
, bndpsd
[bin
], bndpsd
[bin
+1], bin
) ;
191 fastleak
-= s
->fdecay
;
192 v
= bndpsd
[bin
] - fgain
;
193 if (fastleak
< v
) fastleak
= v
;
195 slowleak
-= s
->sdecay
;
196 v
= bndpsd
[bin
] - s
->sgain
;
197 if (slowleak
< v
) slowleak
= v
;
199 v
=fastleak
- lowcomp
;
200 if (slowleak
> v
) v
=slowleak
;
206 /* coupling channel */
209 fastleak
= (s
->cplfleak
<< 8) + 768;
210 slowleak
= (s
->cplsleak
<< 8) + 768;
213 for (bin
= begin
; bin
< bndend
; bin
++) {
214 fastleak
-= s
->fdecay
;
215 v
= bndpsd
[bin
] - fgain
;
216 if (fastleak
< v
) fastleak
= v
;
217 slowleak
-= s
->sdecay
;
218 v
= bndpsd
[bin
] - s
->sgain
;
219 if (slowleak
< v
) slowleak
= v
;
222 if (slowleak
> v
) v
= slowleak
;
226 /* compute masking curve */
228 for (bin
= bndstrt
; bin
< bndend
; bin
++) {
230 tmp
= s
->dbknee
- bndpsd
[bin
];
234 v
=hth
[bin
>> s
->halfratecod
][s
->fscod
];
239 /* delta bit allocation */
241 if (deltbae
== 0 || deltbae
== 1) {
242 int band
, seg
, delta
;
244 for (seg
= 0; seg
< deltnseg
; seg
++) {
245 band
+= deltoffst
[seg
] ;
246 if (deltba
[seg
] >= 4) {
247 delta
= (deltba
[seg
] - 3) << 7;
249 delta
= (deltba
[seg
] - 4) << 7;
251 for (k
= 0; k
< deltlen
[seg
]; k
++) {
252 mask
[band
] += delta
;
258 /* compute bit allocation */
270 end1
=bndtab
[j
] + bndsz
[j
];
271 if (end1
> end
) end1
=end
;
273 for (k
= i
; k
< end1
; k
++) {
274 address
= (psd
[i
] - v
) >> 5 ;
275 if (address
< 0) address
=0;
276 else if (address
> 63) address
=63;
277 bap
[i
] = baptab
[address
];
280 } while (end
> bndtab
[j
++]) ;
283 typedef struct IComplex
{
287 static void fft_init(int ln
)
294 for(i
=0;i
<(n
/2);i
++) {
295 alpha
= 2 * M_PI
* (float)i
/ (float)n
;
296 costab
[i
] = fix15(cos(alpha
));
297 sintab
[i
] = fix15(sin(alpha
));
303 m
|= ((i
>> j
) & 1) << (ln
-j
-1);
310 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
317 pre = (bx + ax) >> 1;\
318 pim = (by + ay) >> 1;\
319 qre = (bx - ax) >> 1;\
320 qim = (by - ay) >> 1;\
323 #define MUL16(a,b) ((a) * (b))
325 #define CMUL(pre, pim, are, aim, bre, bim) \
327 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
328 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
332 /* do a 2^n point complex fft on 2^ln points. */
333 static void fft(IComplex
*z
, int ln
)
337 register IComplex
*p
,*q
;
359 BF(p
[0].re
, p
[0].im
, p
[1].re
, p
[1].im
,
360 p
[0].re
, p
[0].im
, p
[1].re
, p
[1].im
);
369 BF(p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
,
370 p
[0].re
, p
[0].im
, p
[2].re
, p
[2].im
);
371 BF(p
[1].re
, p
[1].im
, p
[3].re
, p
[3].im
,
372 p
[1].re
, p
[1].im
, p
[3].im
, -p
[3].re
);
384 for (j
= 0; j
< nblocks
; ++j
) {
386 BF(p
->re
, p
->im
, q
->re
, q
->im
,
387 p
->re
, p
->im
, q
->re
, q
->im
);
391 for(l
= nblocks
; l
< np2
; l
+= nblocks
) {
392 CMUL(tmp_re
, tmp_im
, costab
[l
], -sintab
[l
], q
->re
, q
->im
);
393 BF(p
->re
, p
->im
, q
->re
, q
->im
,
394 p
->re
, p
->im
, tmp_re
, tmp_im
);
401 nblocks
= nblocks
>> 1;
402 nloops
= nloops
<< 1;
403 } while (nblocks
!= 0);
406 /* do a 512 point mdct */
407 static void mdct512(INT32
*out
, INT16
*in
)
409 int i
, re
, im
, re1
, im1
;
413 /* shift to simplify computations */
415 rot
[i
] = -in
[i
+ 3*N
/4];
417 rot
[i
] = in
[i
- N
/4];
421 re
= ((int)rot
[2*i
] - (int)rot
[N
-1-2*i
]) >> 1;
422 im
= -((int)rot
[N
/2+2*i
] - (int)rot
[N
/2-1-2*i
]) >> 1;
423 CMUL(x
[i
].re
, x
[i
].im
, re
, im
, -xcos1
[i
], xsin1
[i
]);
426 fft(x
, MDCT_NBITS
- 2);
432 CMUL(re1
, im1
, re
, im
, xsin1
[i
], xcos1
[i
]);
434 out
[N
/2-1-2*i
] = re1
;
438 /* XXX: use another norm ? */
439 static int calc_exp_diff(UINT8
*exp1
, UINT8
*exp2
, int n
)
444 sum
+= abs(exp1
[i
] - exp2
[i
]);
449 static void compute_exp_strategy(UINT8 exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
],
450 UINT8 exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
456 /* estimate if the exponent variation & decide if they should be
457 reused in the next frame */
458 exp_strategy
[0][ch
] = EXP_NEW
;
459 for(i
=1;i
<NB_BLOCKS
;i
++) {
460 exp_diff
= calc_exp_diff(exp
[i
][ch
], exp
[i
-1][ch
], N
/2);
462 printf("exp_diff=%d\n", exp_diff
);
464 if (exp_diff
> EXP_DIFF_THRESHOLD
)
465 exp_strategy
[i
][ch
] = EXP_NEW
;
467 exp_strategy
[i
][ch
] = EXP_REUSE
;
472 /* now select the encoding strategy type : if exponents are often
473 recoded, we use a coarse encoding */
475 while (i
< NB_BLOCKS
) {
477 while (j
< NB_BLOCKS
&& exp_strategy
[j
][ch
] == EXP_REUSE
)
481 exp_strategy
[i
][ch
] = EXP_D45
;
485 exp_strategy
[i
][ch
] = EXP_D25
;
488 exp_strategy
[i
][ch
] = EXP_D15
;
495 /* set exp[i] to min(exp[i], exp1[i]) */
496 static void exponent_min(UINT8 exp
[N
/2], UINT8 exp1
[N
/2], int n
)
501 if (exp1
[i
] < exp
[i
])
506 /* update the exponents so that they are the ones the decoder will
507 decode. Return the number of bits used to code the exponents */
508 static int encode_exp(UINT8 encoded_exp
[N
/2],
513 int group_size
, nb_groups
, i
, j
, k
, recurse
, exp_min
, delta
;
516 switch(exp_strategy
) {
528 nb_groups
= ((nb_exps
+ (group_size
* 3) - 4) / (3 * group_size
)) * 3;
530 /* for each group, compute the minimum exponent */
531 exp1
[0] = exp
[0]; /* DC exponent is handled separately */
533 for(i
=1;i
<=nb_groups
;i
++) {
535 assert(exp_min
>= 0 && exp_min
<= 24);
536 for(j
=1;j
<group_size
;j
++) {
537 if (exp
[k
+j
] < exp_min
)
544 /* constraint for DC exponent */
548 /* Iterate until the delta constraints between each groups are
549 satisfyed. I'm sure it is possible to find a better algorithm,
553 for(i
=1;i
<=nb_groups
;i
++) {
554 delta
= exp1
[i
] - exp1
[i
-1];
556 /* if delta too big, we encode a smaller exponent */
557 exp1
[i
] = exp1
[i
-1] + 2;
558 } else if (delta
< -2) {
559 /* if delta is too small, we must decrease the previous
560 exponent, which means we must recurse */
562 exp1
[i
-1] = exp1
[i
] + 2;
567 /* now we have the exponent values the decoder will see */
568 encoded_exp
[0] = exp1
[0];
570 for(i
=1;i
<=nb_groups
;i
++) {
571 for(j
=0;j
<group_size
;j
++) {
572 encoded_exp
[k
+j
] = exp1
[i
];
578 printf("exponents: strategy=%d\n", exp_strategy
);
579 for(i
=0;i
<=nb_groups
* group_size
;i
++) {
580 printf("%d ", encoded_exp
[i
]);
585 return 4 + (nb_groups
/ 3) * 7;
588 /* return the size in bits taken by the mantissa */
589 int compute_mantissa_size(AC3EncodeContext
*s
, UINT8
*m
, int nb_coefs
)
594 for(i
=0;i
<nb_coefs
;i
++) {
601 /* 3 mantissa in 5 bits */
602 if (s
->mant1_cnt
== 0)
604 if (++s
->mant1_cnt
== 3)
608 /* 3 mantissa in 7 bits */
609 if (s
->mant2_cnt
== 0)
611 if (++s
->mant2_cnt
== 3)
618 /* 2 mantissa in 7 bits */
619 if (s
->mant4_cnt
== 0)
621 if (++s
->mant4_cnt
== 2)
639 static int bit_alloc(AC3EncodeContext
*s
,
640 UINT8 bap
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
641 UINT8 encoded_exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
642 UINT8 exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
],
643 int frame_bits
, int csnroffst
, int fsnroffst
)
648 for(i
=0;i
<NB_BLOCKS
;i
++) {
652 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
653 ac3_parametric_bit_allocation(&s
->bit_alloc
,
654 bap
[i
][ch
], (INT8
*)encoded_exp
[i
][ch
],
656 (((csnroffst
-15) << 4) +
658 fgaintab
[s
->fgaincod
[ch
]],
659 ch
== s
->lfe_channel
,
660 2, 0, NULL
, NULL
, NULL
);
661 frame_bits
+= compute_mantissa_size(s
, bap
[i
][ch
],
666 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
667 csnroffst
, fsnroffst
, frame_bits
,
668 16 * s
->frame_size
- ((frame_bits
+ 7) & ~7));
670 return 16 * s
->frame_size
- frame_bits
;
675 static int compute_bit_allocation(AC3EncodeContext
*s
,
676 UINT8 bap
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
677 UINT8 encoded_exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2],
678 UINT8 exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
],
682 int csnroffst
, fsnroffst
;
683 UINT8 bap1
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
684 static int frame_bits_inc
[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
686 /* init default parameters */
692 for(ch
=0;ch
<s
->nb_all_channels
;ch
++)
695 /* compute real values */
696 s
->bit_alloc
.fscod
= s
->fscod
;
697 s
->bit_alloc
.halfratecod
= s
->halfratecod
;
698 s
->bit_alloc
.sdecay
= sdecaytab
[s
->sdecaycod
] >> s
->halfratecod
;
699 s
->bit_alloc
.fdecay
= fdecaytab
[s
->fdecaycod
] >> s
->halfratecod
;
700 s
->bit_alloc
.sgain
= sgaintab
[s
->sgaincod
];
701 s
->bit_alloc
.dbknee
= dbkneetab
[s
->dbkneecod
];
702 s
->bit_alloc
.floor
= floortab
[s
->floorcod
];
706 // if (s->acmod == 2)
708 frame_bits
+= frame_bits_inc
[s
->acmod
];
711 for(i
=0;i
<NB_BLOCKS
;i
++) {
712 frame_bits
+= s
->nb_channels
* 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
714 frame_bits
++; /* rematstr */
715 frame_bits
+= 2 * s
->nb_channels
; /* chexpstr[2] * c */
717 frame_bits
++; /* lfeexpstr */
718 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
719 if (exp_strategy
[i
][ch
] != EXP_REUSE
)
720 frame_bits
+= 6 + 2; /* chbwcod[6], gainrng[2] */
722 frame_bits
++; /* baie */
723 frame_bits
++; /* snr */
724 frame_bits
+= 2; /* delta / skip */
726 frame_bits
++; /* cplinu for block 0 */
728 /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
730 /* (fsnoffset[4] + fgaincod[4]) * c */
731 frame_bits
+= 2*4 + 3 + 6 + s
->nb_all_channels
* (4 + 3);
736 /* now the big work begins : do the bit allocation. Modify the snr
737 offset until we can pack everything in the requested frame size */
739 csnroffst
= s
->csnroffst
;
740 while (csnroffst
>= 0 &&
741 bit_alloc(s
, bap
, encoded_exp
, exp_strategy
, frame_bits
, csnroffst
, 0) < 0)
742 csnroffst
-= SNR_INC1
;
744 fprintf(stderr
, "Yack, Error !!!\n");
747 while ((csnroffst
+ SNR_INC1
) <= 63 &&
748 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
,
749 csnroffst
+ SNR_INC1
, 0) >= 0) {
750 csnroffst
+= SNR_INC1
;
751 memcpy(bap
, bap1
, sizeof(bap1
));
753 while ((csnroffst
+ 1) <= 63 &&
754 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
, csnroffst
+ 1, 0) >= 0) {
756 memcpy(bap
, bap1
, sizeof(bap1
));
760 while ((fsnroffst
+ SNR_INC1
) <= 15 &&
761 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
,
762 csnroffst
, fsnroffst
+ SNR_INC1
) >= 0) {
763 fsnroffst
+= SNR_INC1
;
764 memcpy(bap
, bap1
, sizeof(bap1
));
766 while ((fsnroffst
+ 1) <= 15 &&
767 bit_alloc(s
, bap1
, encoded_exp
, exp_strategy
, frame_bits
,
768 csnroffst
, fsnroffst
+ 1) >= 0) {
770 memcpy(bap
, bap1
, sizeof(bap1
));
773 s
->csnroffst
= csnroffst
;
774 for(ch
=0;ch
<s
->nb_all_channels
;ch
++)
775 s
->fsnroffst
[ch
] = fsnroffst
;
776 #if defined(DEBUG_BITALLOC)
781 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
782 printf("Block #%d Ch%d:\n", i
, ch
);
784 for(j
=0;j
<s
->nb_coefs
[ch
];j
++) {
785 printf("%d ",bap
[i
][ch
][j
]);
795 void ac3_common_init(void)
798 /* compute bndtab and masktab from bandsz */
804 for(j
=0;j
<v
;j
++) masktab
[k
++]=i
;
811 static int AC3_encode_init(AVCodecContext
*avctx
)
813 int freq
= avctx
->sample_rate
;
814 int bitrate
= avctx
->bit_rate
;
815 int channels
= avctx
->channels
;
816 AC3EncodeContext
*s
= avctx
->priv_data
;
819 static const UINT8 acmod_defs
[6] = {
823 0x06, /* L R SL SR */
824 0x07, /* L C R SL SR */
825 0x07, /* L C R SL SR (+LFE) */
828 avctx
->frame_size
= AC3_FRAME_SIZE
;
830 /* number of channels */
831 if (channels
< 1 || channels
> 6)
833 s
->acmod
= acmod_defs
[channels
- 1];
834 s
->lfe
= (channels
== 6) ? 1 : 0;
835 s
->nb_all_channels
= channels
;
836 s
->nb_channels
= channels
> 5 ? 5 : channels
;
837 s
->lfe_channel
= s
->lfe
? 5 : -1;
842 if ((ac3_freqs
[j
] >> i
) == freq
)
847 s
->sample_rate
= freq
;
850 s
->bsid
= 8 + s
->halfratecod
;
851 s
->bsmod
= 0; /* complete main audio service */
853 /* bitrate & frame size */
856 if ((ac3_bitratetab
[i
] >> s
->halfratecod
) == bitrate
)
861 s
->bit_rate
= bitrate
;
862 s
->frmsizecod
= i
<< 1;
863 s
->frame_size_min
= (bitrate
* 1000 * AC3_FRAME_SIZE
) / (freq
* 16);
864 /* for now we do not handle fractional sizes */
865 s
->frame_size
= s
->frame_size_min
;
867 /* bit allocation init */
868 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
869 /* bandwidth for each channel */
870 /* XXX: should compute the bandwidth according to the frame
871 size, so that we avoid anoying high freq artefacts */
872 s
->chbwcod
[ch
] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
873 s
->nb_coefs
[ch
] = ((s
->chbwcod
[ch
] + 12) * 3) + 37;
876 s
->nb_coefs
[s
->lfe_channel
] = 7; /* fixed */
878 /* initial snr offset */
884 fft_init(MDCT_NBITS
- 2);
886 alpha
= 2 * M_PI
* (i
+ 1.0 / 8.0) / (float)N
;
887 xcos1
[i
] = fix15(-cos(alpha
));
888 xsin1
[i
] = fix15(-sin(alpha
));
893 avctx
->coded_frame
= avcodec_alloc_frame();
894 avctx
->coded_frame
->key_frame
= 1;
899 /* output the AC3 frame header */
900 static void output_frame_header(AC3EncodeContext
*s
, unsigned char *frame
)
902 init_put_bits(&s
->pb
, frame
, AC3_MAX_CODED_FRAME_SIZE
, NULL
, NULL
);
904 put_bits(&s
->pb
, 16, 0x0b77); /* frame header */
905 put_bits(&s
->pb
, 16, 0); /* crc1: will be filled later */
906 put_bits(&s
->pb
, 2, s
->fscod
);
907 put_bits(&s
->pb
, 6, s
->frmsizecod
+ (s
->frame_size
- s
->frame_size_min
));
908 put_bits(&s
->pb
, 5, s
->bsid
);
909 put_bits(&s
->pb
, 3, s
->bsmod
);
910 put_bits(&s
->pb
, 3, s
->acmod
);
911 if ((s
->acmod
& 0x01) && s
->acmod
!= 0x01)
912 put_bits(&s
->pb
, 2, 1); /* XXX -4.5 dB */
914 put_bits(&s
->pb
, 2, 1); /* XXX -6 dB */
915 if (s
->acmod
== 0x02)
916 put_bits(&s
->pb
, 2, 0); /* surround not indicated */
917 put_bits(&s
->pb
, 1, s
->lfe
); /* LFE */
918 put_bits(&s
->pb
, 5, 31); /* dialog norm: -31 db */
919 put_bits(&s
->pb
, 1, 0); /* no compression control word */
920 put_bits(&s
->pb
, 1, 0); /* no lang code */
921 put_bits(&s
->pb
, 1, 0); /* no audio production info */
922 put_bits(&s
->pb
, 1, 0); /* no copyright */
923 put_bits(&s
->pb
, 1, 1); /* original bitstream */
924 put_bits(&s
->pb
, 1, 0); /* no time code 1 */
925 put_bits(&s
->pb
, 1, 0); /* no time code 2 */
926 put_bits(&s
->pb
, 1, 0); /* no addtional bit stream info */
929 /* symetric quantization on 'levels' levels */
930 static inline int sym_quant(int c
, int e
, int levels
)
935 v
= (levels
* (c
<< e
)) >> 24;
937 v
= (levels
>> 1) + v
;
939 v
= (levels
* ((-c
) << e
)) >> 24;
941 v
= (levels
>> 1) - v
;
943 assert (v
>= 0 && v
< levels
);
947 /* asymetric quantization on 2^qbits levels */
948 static inline int asym_quant(int c
, int e
, int qbits
)
952 lshift
= e
+ qbits
- 24;
959 m
= (1 << (qbits
-1));
963 return v
& ((1 << qbits
)-1);
966 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
968 static void output_audio_block(AC3EncodeContext
*s
,
969 UINT8 exp_strategy
[AC3_MAX_CHANNELS
],
970 UINT8 encoded_exp
[AC3_MAX_CHANNELS
][N
/2],
971 UINT8 bap
[AC3_MAX_CHANNELS
][N
/2],
972 INT32 mdct_coefs
[AC3_MAX_CHANNELS
][N
/2],
973 INT8 global_exp
[AC3_MAX_CHANNELS
],
976 int ch
, nb_groups
, group_size
, i
, baie
;
978 UINT16 qmant
[AC3_MAX_CHANNELS
][N
/2];
980 int mant1_cnt
, mant2_cnt
, mant4_cnt
;
981 UINT16
*qmant1_ptr
, *qmant2_ptr
, *qmant4_ptr
;
982 int delta0
, delta1
, delta2
;
984 for(ch
=0;ch
<s
->nb_channels
;ch
++)
985 put_bits(&s
->pb
, 1, 0); /* 512 point MDCT */
986 for(ch
=0;ch
<s
->nb_channels
;ch
++)
987 put_bits(&s
->pb
, 1, 1); /* no dither */
988 put_bits(&s
->pb
, 1, 0); /* no dynamic range */
989 if (block_num
== 0) {
990 /* for block 0, even if no coupling, we must say it. This is a
992 put_bits(&s
->pb
, 1, 1); /* coupling strategy present */
993 put_bits(&s
->pb
, 1, 0); /* no coupling strategy */
995 put_bits(&s
->pb
, 1, 0); /* no new coupling strategy */
999 put_bits(&s
->pb
, 1, 0); /* no matrixing (but should be used in the future) */
1004 static int count
= 0;
1005 printf("Block #%d (%d)\n", block_num
, count
++);
1008 /* exponent strategy */
1009 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1010 put_bits(&s
->pb
, 2, exp_strategy
[ch
]);
1014 put_bits(&s
->pb
, 1, exp_strategy
[s
->lfe_channel
]);
1017 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1018 if (exp_strategy
[ch
] != EXP_REUSE
)
1019 put_bits(&s
->pb
, 6, s
->chbwcod
[ch
]);
1023 for (ch
= 0; ch
< s
->nb_all_channels
; ch
++) {
1024 switch(exp_strategy
[ch
]) {
1038 nb_groups
= (s
->nb_coefs
[ch
] + (group_size
* 3) - 4) / (3 * group_size
);
1039 p
= encoded_exp
[ch
];
1041 /* first exponent */
1043 put_bits(&s
->pb
, 4, exp1
);
1045 /* next ones are delta encoded */
1046 for(i
=0;i
<nb_groups
;i
++) {
1047 /* merge three delta in one code */
1051 delta0
= exp1
- exp0
+ 2;
1056 delta1
= exp1
- exp0
+ 2;
1061 delta2
= exp1
- exp0
+ 2;
1063 put_bits(&s
->pb
, 7, ((delta0
* 5 + delta1
) * 5) + delta2
);
1066 if (ch
!= s
->lfe_channel
)
1067 put_bits(&s
->pb
, 2, 0); /* no gain range info */
1070 /* bit allocation info */
1071 baie
= (block_num
== 0);
1072 put_bits(&s
->pb
, 1, baie
);
1074 put_bits(&s
->pb
, 2, s
->sdecaycod
);
1075 put_bits(&s
->pb
, 2, s
->fdecaycod
);
1076 put_bits(&s
->pb
, 2, s
->sgaincod
);
1077 put_bits(&s
->pb
, 2, s
->dbkneecod
);
1078 put_bits(&s
->pb
, 3, s
->floorcod
);
1082 put_bits(&s
->pb
, 1, baie
); /* always present with bai */
1084 put_bits(&s
->pb
, 6, s
->csnroffst
);
1085 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
1086 put_bits(&s
->pb
, 4, s
->fsnroffst
[ch
]);
1087 put_bits(&s
->pb
, 3, s
->fgaincod
[ch
]);
1091 put_bits(&s
->pb
, 1, 0); /* no delta bit allocation */
1092 put_bits(&s
->pb
, 1, 0); /* no data to skip */
1094 /* mantissa encoding : we use two passes to handle the grouping. A
1095 one pass method may be faster, but it would necessitate to
1096 modify the output stream. */
1098 /* first pass: quantize */
1099 mant1_cnt
= mant2_cnt
= mant4_cnt
= 0;
1100 qmant1_ptr
= qmant2_ptr
= qmant4_ptr
= NULL
;
1102 for (ch
= 0; ch
< s
->nb_all_channels
; ch
++) {
1105 for(i
=0;i
<s
->nb_coefs
[ch
];i
++) {
1106 c
= mdct_coefs
[ch
][i
];
1107 e
= encoded_exp
[ch
][i
] - global_exp
[ch
];
1114 v
= sym_quant(c
, e
, 3);
1117 qmant1_ptr
= &qmant
[ch
][i
];
1122 *qmant1_ptr
+= 3 * v
;
1134 v
= sym_quant(c
, e
, 5);
1137 qmant2_ptr
= &qmant
[ch
][i
];
1142 *qmant2_ptr
+= 5 * v
;
1154 v
= sym_quant(c
, e
, 7);
1157 v
= sym_quant(c
, e
, 11);
1160 qmant4_ptr
= &qmant
[ch
][i
];
1172 v
= sym_quant(c
, e
, 15);
1175 v
= asym_quant(c
, e
, 14);
1178 v
= asym_quant(c
, e
, 16);
1181 v
= asym_quant(c
, e
, b
- 1);
1188 /* second pass : output the values */
1189 for (ch
= 0; ch
< s
->nb_all_channels
; ch
++) {
1192 for(i
=0;i
<s
->nb_coefs
[ch
];i
++) {
1200 put_bits(&s
->pb
, 5, q
);
1204 put_bits(&s
->pb
, 7, q
);
1207 put_bits(&s
->pb
, 3, q
);
1211 put_bits(&s
->pb
, 7, q
);
1214 put_bits(&s
->pb
, 14, q
);
1217 put_bits(&s
->pb
, 16, q
);
1220 put_bits(&s
->pb
, b
- 1, q
);
1227 /* compute the ac3 crc */
1229 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1231 static void ac3_crc_init(void)
1233 unsigned int c
, n
, k
;
1235 for(n
=0;n
<256;n
++) {
1237 for (k
= 0; k
< 8; k
++) {
1239 c
= ((c
<< 1) & 0xffff) ^ (CRC16_POLY
& 0xffff);
1247 static unsigned int ac3_crc(UINT8
*data
, int n
, unsigned int crc
)
1251 crc
= (crc_table
[data
[i
] ^ (crc
>> 8)] ^ (crc
<< 8)) & 0xffff;
1256 static unsigned int mul_poly(unsigned int a
, unsigned int b
, unsigned int poly
)
1272 static unsigned int pow_poly(unsigned int a
, unsigned int n
, unsigned int poly
)
1278 r
= mul_poly(r
, a
, poly
);
1279 a
= mul_poly(a
, a
, poly
);
1286 /* compute log2(max(abs(tab[]))) */
1287 static int log2_tab(INT16
*tab
, int n
)
1298 static void lshift_tab(INT16
*tab
, int n
, int lshift
)
1306 } else if (lshift
< 0) {
1314 /* fill the end of the frame and compute the two crcs */
1315 static int output_frame_end(AC3EncodeContext
*s
)
1317 int frame_size
, frame_size_58
, n
, crc1
, crc2
, crc_inv
;
1320 frame_size
= s
->frame_size
; /* frame size in words */
1321 /* align to 8 bits */
1322 flush_put_bits(&s
->pb
);
1323 /* add zero bytes to reach the frame size */
1325 n
= 2 * s
->frame_size
- (pbBufPtr(&s
->pb
) - frame
) - 2;
1327 memset(pbBufPtr(&s
->pb
), 0, n
);
1329 /* Now we must compute both crcs : this is not so easy for crc1
1330 because it is at the beginning of the data... */
1331 frame_size_58
= (frame_size
>> 1) + (frame_size
>> 3);
1332 crc1
= ac3_crc(frame
+ 4, (2 * frame_size_58
) - 4, 0);
1333 /* XXX: could precompute crc_inv */
1334 crc_inv
= pow_poly((CRC16_POLY
>> 1), (16 * frame_size_58
) - 16, CRC16_POLY
);
1335 crc1
= mul_poly(crc_inv
, crc1
, CRC16_POLY
);
1336 frame
[2] = crc1
>> 8;
1339 crc2
= ac3_crc(frame
+ 2 * frame_size_58
, (frame_size
- frame_size_58
) * 2 - 2, 0);
1340 frame
[2*frame_size
- 2] = crc2
>> 8;
1341 frame
[2*frame_size
- 1] = crc2
;
1343 // printf("n=%d frame_size=%d\n", n, frame_size);
1344 return frame_size
* 2;
1347 static int AC3_encode_frame(AVCodecContext
*avctx
,
1348 unsigned char *frame
, int buf_size
, void *data
)
1350 AC3EncodeContext
*s
= avctx
->priv_data
;
1351 short *samples
= data
;
1353 INT16 input_samples
[N
];
1354 INT32 mdct_coef
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1355 UINT8 exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1356 UINT8 exp_strategy
[NB_BLOCKS
][AC3_MAX_CHANNELS
];
1357 UINT8 encoded_exp
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1358 UINT8 bap
[NB_BLOCKS
][AC3_MAX_CHANNELS
][N
/2];
1359 INT8 exp_samples
[NB_BLOCKS
][AC3_MAX_CHANNELS
];
1363 for(ch
=0;ch
<s
->nb_all_channels
;ch
++) {
1364 /* fixed mdct to the six sub blocks & exponent computation */
1365 for(i
=0;i
<NB_BLOCKS
;i
++) {
1369 /* compute input samples */
1370 memcpy(input_samples
, s
->last_samples
[ch
], N
/2 * sizeof(INT16
));
1371 sinc
= s
->nb_all_channels
;
1372 sptr
= samples
+ (sinc
* (N
/2) * i
) + ch
;
1373 for(j
=0;j
<N
/2;j
++) {
1375 input_samples
[j
+ N
/2] = v
;
1376 s
->last_samples
[ch
][j
] = v
;
1380 /* apply the MDCT window */
1381 for(j
=0;j
<N
/2;j
++) {
1382 input_samples
[j
] = MUL16(input_samples
[j
],
1383 ac3_window
[j
]) >> 15;
1384 input_samples
[N
-j
-1] = MUL16(input_samples
[N
-j
-1],
1385 ac3_window
[j
]) >> 15;
1388 /* Normalize the samples to use the maximum available
1390 v
= 14 - log2_tab(input_samples
, N
);
1393 exp_samples
[i
][ch
] = v
- 8;
1394 lshift_tab(input_samples
, N
, v
);
1397 mdct512(mdct_coef
[i
][ch
], input_samples
);
1399 /* compute "exponents". We take into account the
1400 normalization there */
1401 for(j
=0;j
<N
/2;j
++) {
1403 v
= abs(mdct_coef
[i
][ch
][j
]);
1407 e
= 23 - av_log2(v
) + exp_samples
[i
][ch
];
1410 mdct_coef
[i
][ch
][j
] = 0;
1417 compute_exp_strategy(exp_strategy
, exp
, ch
, ch
== s
->lfe_channel
);
1419 /* compute the exponents as the decoder will see them. The
1420 EXP_REUSE case must be handled carefully : we select the
1421 min of the exponents */
1423 while (i
< NB_BLOCKS
) {
1425 while (j
< NB_BLOCKS
&& exp_strategy
[j
][ch
] == EXP_REUSE
) {
1426 exponent_min(exp
[i
][ch
], exp
[j
][ch
], s
->nb_coefs
[ch
]);
1429 frame_bits
+= encode_exp(encoded_exp
[i
][ch
],
1430 exp
[i
][ch
], s
->nb_coefs
[ch
],
1431 exp_strategy
[i
][ch
]);
1432 /* copy encoded exponents for reuse case */
1433 for(k
=i
+1;k
<j
;k
++) {
1434 memcpy(encoded_exp
[k
][ch
], encoded_exp
[i
][ch
],
1435 s
->nb_coefs
[ch
] * sizeof(UINT8
));
1441 compute_bit_allocation(s
, bap
, encoded_exp
, exp_strategy
, frame_bits
);
1442 /* everything is known... let's output the frame */
1443 output_frame_header(s
, frame
);
1445 for(i
=0;i
<NB_BLOCKS
;i
++) {
1446 output_audio_block(s
, exp_strategy
[i
], encoded_exp
[i
],
1447 bap
[i
], mdct_coef
[i
], exp_samples
[i
], i
);
1449 return output_frame_end(s
);
1452 static int AC3_encode_close(AVCodecContext
*avctx
)
1454 av_freep(&avctx
->coded_frame
);
1458 /*************************************************************************/
1465 IComplex in
[FN
], in1
[FN
];
1467 float sum_re
, sum_im
, a
;
1472 in
[i
].re
= random() % 65535 - 32767;
1473 in
[i
].im
= random() % 65535 - 32767;
1483 a
= -2 * M_PI
* (n
* k
) / FN
;
1484 sum_re
+= in1
[n
].re
* cos(a
) - in1
[n
].im
* sin(a
);
1485 sum_im
+= in1
[n
].re
* sin(a
) + in1
[n
].im
* cos(a
);
1487 printf("%3d: %6d,%6d %6.0f,%6.0f\n",
1488 k
, in
[k
].re
, in
[k
].im
, sum_re
/ FN
, sum_im
/ FN
);
1492 void mdct_test(void)
1498 float s
, a
, err
, e
, emax
;
1502 input
[i
] = (random() % 65535 - 32767) * 9 / 10;
1503 input1
[i
] = input
[i
];
1506 mdct512(output
, input
);
1509 for(k
=0;k
<N
/2;k
++) {
1512 a
= (2*M_PI
*(2*n
+1+N
/2)*(2*k
+1) / (4 * N
));
1513 s
+= input1
[n
] * cos(a
);
1515 output1
[k
] = -2 * s
/ N
;
1520 for(i
=0;i
<N
/2;i
++) {
1521 printf("%3d: %7d %7.0f\n", i
, output
[i
], output1
[i
]);
1522 e
= output
[i
] - output1
[i
];
1527 printf("err2=%f emax=%f\n", err
/ (N
/2), emax
);
1532 AC3EncodeContext ctx
;
1533 unsigned char frame
[AC3_MAX_CODED_FRAME_SIZE
];
1534 short samples
[AC3_FRAME_SIZE
];
1537 AC3_encode_init(&ctx
, 44100, 64000, 1);
1542 for(i
=0;i
<AC3_FRAME_SIZE
;i
++)
1543 samples
[i
] = (int)(sin(2*M_PI
*i
*1000.0/44100) * 10000);
1544 ret
= AC3_encode_frame(&ctx
, frame
, samples
);
1545 printf("ret=%d\n", ret
);
1549 AVCodec ac3_encoder
= {
1553 sizeof(AC3EncodeContext
),