comparison ac3enc.c @ 782:dd7d5748d064 libavcodec

preparing integration of new AC3 decoder
author bellard
date Mon, 28 Oct 2002 00:39:05 +0000
parents 718a22dc121f
children 7fccaa0d699d
comparison
equal deleted inserted replaced
781:6f5e87957bcb 782:dd7d5748d064
18 */ 18 */
19 //#define DEBUG 19 //#define DEBUG
20 //#define DEBUG_BITALLOC 20 //#define DEBUG_BITALLOC
21 #include "avcodec.h" 21 #include "avcodec.h"
22 22
23 #include "ac3enc.h" 23 #include "ac3.h"
24
25 typedef struct AC3EncodeContext {
26 PutBitContext pb;
27 int nb_channels;
28 int nb_all_channels;
29 int lfe_channel;
30 int bit_rate;
31 int sample_rate;
32 int bsid;
33 int frame_size_min; /* minimum frame size in case rounding is necessary */
34 int frame_size; /* current frame size in words */
35 int halfratecod;
36 int frmsizecod;
37 int fscod; /* frequency */
38 int acmod;
39 int lfe;
40 int bsmod;
41 short last_samples[AC3_MAX_CHANNELS][256];
42 int chbwcod[AC3_MAX_CHANNELS];
43 int nb_coefs[AC3_MAX_CHANNELS];
44
45 /* bitrate allocation control */
46 int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod;
47 AC3BitAllocParameters bit_alloc;
48 int csnroffst;
49 int fgaincod[AC3_MAX_CHANNELS];
50 int fsnroffst[AC3_MAX_CHANNELS];
51 /* mantissa encoding */
52 int mant1_cnt, mant2_cnt, mant4_cnt;
53 } AC3EncodeContext;
54
24 #include "ac3tab.h" 55 #include "ac3tab.h"
25
26 56
27 #define MDCT_NBITS 9 57 #define MDCT_NBITS 9
28 #define N (1 << MDCT_NBITS) 58 #define N (1 << MDCT_NBITS)
29 #define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
30 59
31 /* new exponents are sent if their Norm 1 exceed this number */ 60 /* new exponents are sent if their Norm 1 exceed this number */
32 #define EXP_DIFF_THRESHOLD 1000 61 #define EXP_DIFF_THRESHOLD 1000
33
34 /* exponent encoding strategy */
35 #define EXP_REUSE 0
36 #define EXP_NEW 1
37
38 #define EXP_D15 1
39 #define EXP_D25 2
40 #define EXP_D45 3
41 62
42 static void fft_init(int ln); 63 static void fft_init(int ln);
43 static void ac3_crc_init(void); 64 static void ac3_crc_init(void);
44 65
45 static inline INT16 fix15(float a) 66 static inline INT16 fix15(float a)
86 } 107 }
87 return a; 108 return a;
88 } 109 }
89 110
90 /* AC3 bit allocation. The algorithm is the one described in the AC3 111 /* AC3 bit allocation. The algorithm is the one described in the AC3
91 spec with some optimizations because of our simplified encoding 112 spec. */
92 assumptions. */ 113 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, UINT8 *bap,
93 void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap, 114 INT8 *exp, int start, int end,
94 INT8 *exp, int start, int end, 115 int snroffset, int fgain, int is_lfe,
95 int snroffset, int fgain, int is_lfe) 116 int deltbae,int deltnseg,
117 UINT8 *deltoffst, UINT8 *deltlen, UINT8 *deltba)
96 { 118 {
97 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin; 119 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
98 int fastleak,slowleak,address,tmp; 120 int fastleak,slowleak,address,tmp;
99 INT16 psd[256]; /* scaled exponents */ 121 INT16 psd[256]; /* scaled exponents */
100 INT16 bndpsd[50]; /* interpolated exponents */ 122 INT16 bndpsd[50]; /* interpolated exponents */
136 158
137 /* excitation function */ 159 /* excitation function */
138 bndstrt = masktab[start]; 160 bndstrt = masktab[start];
139 bndend = masktab[end-1] + 1; 161 bndend = masktab[end-1] + 1;
140 162
141 lowcomp = 0; 163 if (bndstrt == 0) {
142 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ; 164 lowcomp = 0;
143 excite[0] = bndpsd[0] - fgain - lowcomp ; 165 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
144 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ; 166 excite[0] = bndpsd[0] - fgain - lowcomp ;
145 excite[1] = bndpsd[1] - fgain - lowcomp ; 167 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
146 begin = 7 ; 168 excite[1] = bndpsd[1] - fgain - lowcomp ;
147 for (bin = 2; bin < 7; bin++) { 169 begin = 7 ;
148 if (!(is_lfe && bin == 6)) 170 for (bin = 2; bin < 7; bin++) {
149 lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ; 171 if (!(is_lfe && bin == 6))
150 fastleak = bndpsd[bin] - fgain ; 172 lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
151 slowleak = bndpsd[bin] - s->sgain ; 173 fastleak = bndpsd[bin] - fgain ;
152 excite[bin] = fastleak - lowcomp ; 174 slowleak = bndpsd[bin] - s->sgain ;
153 if (!(is_lfe && bin == 6)) { 175 excite[bin] = fastleak - lowcomp ;
154 if (bndpsd[bin] <= bndpsd[bin+1]) { 176 if (!(is_lfe && bin == 6)) {
155 begin = bin + 1 ; 177 if (bndpsd[bin] <= bndpsd[bin+1]) {
156 break ; 178 begin = bin + 1 ;
157 } 179 break ;
158 } 180 }
159 } 181 }
160 182 }
161 end1=bndend; 183
162 if (end1 > 22) end1=22; 184 end1=bndend;
163 185 if (end1 > 22) end1=22;
164 for (bin = begin; bin < end1; bin++) { 186
165 if (!(is_lfe && bin == 6)) 187 for (bin = begin; bin < end1; bin++) {
166 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ; 188 if (!(is_lfe && bin == 6))
189 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
167 190
168 fastleak -= s->fdecay ; 191 fastleak -= s->fdecay ;
169 v = bndpsd[bin] - fgain; 192 v = bndpsd[bin] - fgain;
170 if (fastleak < v) fastleak = v; 193 if (fastleak < v) fastleak = v;
171 194
172 slowleak -= s->sdecay ; 195 slowleak -= s->sdecay ;
173 v = bndpsd[bin] - s->sgain; 196 v = bndpsd[bin] - s->sgain;
174 if (slowleak < v) slowleak = v; 197 if (slowleak < v) slowleak = v;
175 198
176 v=fastleak - lowcomp; 199 v=fastleak - lowcomp;
177 if (slowleak > v) v=slowleak; 200 if (slowleak > v) v=slowleak;
178 201
179 excite[bin] = v; 202 excite[bin] = v;
180 } 203 }
181 204 begin = 22;
182 for (bin = 22; bin < bndend; bin++) { 205 } else {
206 /* coupling channel */
207 begin = bndstrt;
208
209 fastleak = (s->cplfleak << 8) + 768;
210 slowleak = (s->cplsleak << 8) + 768;
211 }
212
213 for (bin = begin; bin < bndend; bin++) {
183 fastleak -= s->fdecay ; 214 fastleak -= s->fdecay ;
184 v = bndpsd[bin] - fgain; 215 v = bndpsd[bin] - fgain;
185 if (fastleak < v) fastleak = v; 216 if (fastleak < v) fastleak = v;
186 slowleak -= s->sdecay ; 217 slowleak -= s->sdecay ;
187 v = bndpsd[bin] - s->sgain; 218 v = bndpsd[bin] - s->sgain;
201 v1 += tmp >> 2; 232 v1 += tmp >> 2;
202 } 233 }
203 v=hth[bin >> s->halfratecod][s->fscod]; 234 v=hth[bin >> s->halfratecod][s->fscod];
204 if (v1 > v) v=v1; 235 if (v1 > v) v=v1;
205 mask[bin] = v; 236 mask[bin] = v;
237 }
238
239 /* delta bit allocation */
240
241 if (deltbae == 0 || deltbae == 1) {
242 int band, seg, delta;
243 band = 0 ;
244 for (seg = 0; seg < deltnseg; seg++) {
245 band += deltoffst[seg] ;
246 if (deltba[seg] >= 4) {
247 delta = (deltba[seg] - 3) << 7;
248 } else {
249 delta = (deltba[seg] - 4) << 7;
250 }
251 for (k = 0; k < deltlen[seg]; k++) {
252 mask[band] += delta ;
253 band++ ;
254 }
255 }
206 } 256 }
207 257
208 /* compute bit allocation */ 258 /* compute bit allocation */
209 259
210 i = start ; 260 i = start ;
598 for(i=0;i<NB_BLOCKS;i++) { 648 for(i=0;i<NB_BLOCKS;i++) {
599 s->mant1_cnt = 0; 649 s->mant1_cnt = 0;
600 s->mant2_cnt = 0; 650 s->mant2_cnt = 0;
601 s->mant4_cnt = 0; 651 s->mant4_cnt = 0;
602 for(ch=0;ch<s->nb_all_channels;ch++) { 652 for(ch=0;ch<s->nb_all_channels;ch++) {
603 parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch], 653 ac3_parametric_bit_allocation(&s->bit_alloc,
604 0, s->nb_coefs[ch], 654 bap[i][ch], (INT8 *)encoded_exp[i][ch],
605 (((csnroffst-15) << 4) + 655 0, s->nb_coefs[ch],
606 fsnroffst) << 2, 656 (((csnroffst-15) << 4) +
607 fgaintab[s->fgaincod[ch]], 657 fsnroffst) << 2,
608 ch == s->lfe_channel); 658 fgaintab[s->fgaincod[ch]],
659 ch == s->lfe_channel,
660 2, 0, NULL, NULL, NULL);
609 frame_bits += compute_mantissa_size(s, bap[i][ch], 661 frame_bits += compute_mantissa_size(s, bap[i][ch],
610 s->nb_coefs[ch]); 662 s->nb_coefs[ch]);
611 } 663 }
612 } 664 }
613 #if 0 665 #if 0
639 s->floorcod = 4; 691 s->floorcod = 4;
640 for(ch=0;ch<s->nb_all_channels;ch++) 692 for(ch=0;ch<s->nb_all_channels;ch++)
641 s->fgaincod[ch] = 4; 693 s->fgaincod[ch] = 4;
642 694
643 /* compute real values */ 695 /* compute real values */
644 s->sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod; 696 s->bit_alloc.fscod = s->fscod;
645 s->fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod; 697 s->bit_alloc.halfratecod = s->halfratecod;
646 s->sgain = sgaintab[s->sgaincod]; 698 s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
647 s->dbknee = dbkneetab[s->dbkneecod]; 699 s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
648 s->floor = floortab[s->floorcod]; 700 s->bit_alloc.sgain = sgaintab[s->sgaincod];
649 701 s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];
702 s->bit_alloc.floor = floortab[s->floorcod];
703
650 /* header size */ 704 /* header size */
651 frame_bits += 65; 705 frame_bits += 65;
652 // if (s->acmod == 2) 706 // if (s->acmod == 2)
653 // frame_bits += 2; 707 // frame_bits += 2;
654 frame_bits += frame_bits_inc[s->acmod]; 708 frame_bits += frame_bits_inc[s->acmod];
736 } 790 }
737 #endif 791 #endif
738 return 0; 792 return 0;
739 } 793 }
740 794
795 void ac3_common_init(void)
796 {
797 int i, j, k, l, v;
798 /* compute bndtab and masktab from bandsz */
799 k = 0;
800 l = 0;
801 for(i=0;i<50;i++) {
802 bndtab[i] = l;
803 v = bndsz[i];
804 for(j=0;j<v;j++) masktab[k++]=i;
805 l += v;
806 }
807 bndtab[50] = 0;
808 }
809
810
741 static int AC3_encode_init(AVCodecContext *avctx) 811 static int AC3_encode_init(AVCodecContext *avctx)
742 { 812 {
743 int freq = avctx->sample_rate; 813 int freq = avctx->sample_rate;
744 int bitrate = avctx->bit_rate; 814 int bitrate = avctx->bit_rate;
745 int channels = avctx->channels; 815 int channels = avctx->channels;
746 AC3EncodeContext *s = avctx->priv_data; 816 AC3EncodeContext *s = avctx->priv_data;
747 int i, j, k, l, ch, v; 817 int i, j, ch;
748 float alpha; 818 float alpha;
749 static unsigned short freqs[3] = { 48000, 44100, 32000 }; 819 static const UINT8 acmod_defs[6] = {
750 static int acmod_defs[6] = {
751 0x01, /* C */ 820 0x01, /* C */
752 0x02, /* L R */ 821 0x02, /* L R */
753 0x03, /* L C R */ 822 0x03, /* L C R */
754 0x06, /* L R SL SR */ 823 0x06, /* L R SL SR */
755 0x07, /* L C R SL SR */ 824 0x07, /* L C R SL SR */
769 s->lfe_channel = s->lfe ? 5 : -1; 838 s->lfe_channel = s->lfe ? 5 : -1;
770 839
771 /* frequency */ 840 /* frequency */
772 for(i=0;i<3;i++) { 841 for(i=0;i<3;i++) {
773 for(j=0;j<3;j++) 842 for(j=0;j<3;j++)
774 if ((freqs[j] >> i) == freq) 843 if ((ac3_freqs[j] >> i) == freq)
775 goto found; 844 goto found;
776 } 845 }
777 return -1; 846 return -1;
778 found: 847 found:
779 s->sample_rate = freq; 848 s->sample_rate = freq;
783 s->bsmod = 0; /* complete main audio service */ 852 s->bsmod = 0; /* complete main audio service */
784 853
785 /* bitrate & frame size */ 854 /* bitrate & frame size */
786 bitrate /= 1000; 855 bitrate /= 1000;
787 for(i=0;i<19;i++) { 856 for(i=0;i<19;i++) {
788 if ((bitratetab[i] >> s->halfratecod) == bitrate) 857 if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate)
789 break; 858 break;
790 } 859 }
791 if (i == 19) 860 if (i == 19)
792 return -1; 861 return -1;
793 s->bit_rate = bitrate; 862 s->bit_rate = bitrate;
808 s->nb_coefs[s->lfe_channel] = 7; /* fixed */ 877 s->nb_coefs[s->lfe_channel] = 7; /* fixed */
809 } 878 }
810 /* initial snr offset */ 879 /* initial snr offset */
811 s->csnroffst = 40; 880 s->csnroffst = 40;
812 881
813 /* compute bndtab and masktab from bandsz */ 882 ac3_common_init();
814 k = 0;
815 l = 0;
816 for(i=0;i<50;i++) {
817 bndtab[i] = l;
818 v = bndsz[i];
819 for(j=0;j<v;j++) masktab[k++]=i;
820 l += v;
821 }
822 bndtab[50] = 0;
823 883
824 /* mdct init */ 884 /* mdct init */
825 fft_init(MDCT_NBITS - 2); 885 fft_init(MDCT_NBITS - 2);
826 for(i=0;i<N/4;i++) { 886 for(i=0;i<N/4;i++) {
827 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N; 887 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N;
1280 1340
1281 // printf("n=%d frame_size=%d\n", n, frame_size); 1341 // printf("n=%d frame_size=%d\n", n, frame_size);
1282 return frame_size * 2; 1342 return frame_size * 2;
1283 } 1343 }
1284 1344
1285 int AC3_encode_frame(AVCodecContext *avctx, 1345 static int AC3_encode_frame(AVCodecContext *avctx,
1286 unsigned char *frame, int buf_size, void *data) 1346 unsigned char *frame, int buf_size, void *data)
1287 { 1347 {
1288 AC3EncodeContext *s = avctx->priv_data; 1348 AC3EncodeContext *s = avctx->priv_data;
1289 short *samples = data; 1349 short *samples = data;
1290 int i, j, k, v, ch; 1350 int i, j, k, v, ch;
1291 INT16 input_samples[N]; 1351 INT16 input_samples[N];