Mercurial > libavcodec.hg
comparison ac3enc.c @ 4643:1e175640dad3 libavcodec
Remove common code from AC-3 encoder and utilize ac3.c.
author | jbr |
---|---|
date | Fri, 09 Mar 2007 13:57:42 +0000 |
parents | 5bc169ed9db6 |
children | 056127e5df89 |
comparison
equal
deleted
inserted
replaced
4642:7e140e4cd8cb | 4643:1e175640dad3 |
---|---|
60 int fsnroffst[AC3_MAX_CHANNELS]; | 60 int fsnroffst[AC3_MAX_CHANNELS]; |
61 /* mantissa encoding */ | 61 /* mantissa encoding */ |
62 int mant1_cnt, mant2_cnt, mant4_cnt; | 62 int mant1_cnt, mant2_cnt, mant4_cnt; |
63 } AC3EncodeContext; | 63 } AC3EncodeContext; |
64 | 64 |
65 #include "ac3tab.h" | 65 static int16_t costab[64]; |
66 static int16_t sintab[64]; | |
67 static int16_t fft_rev[512]; | |
68 static int16_t xcos1[128]; | |
69 static int16_t xsin1[128]; | |
66 | 70 |
67 #define MDCT_NBITS 9 | 71 #define MDCT_NBITS 9 |
68 #define N (1 << MDCT_NBITS) | 72 #define N (1 << MDCT_NBITS) |
69 | 73 |
70 /* new exponents are sent if their Norm 1 exceed this number */ | 74 /* new exponents are sent if their Norm 1 exceed this number */ |
79 if (v < -32767) | 83 if (v < -32767) |
80 v = -32767; | 84 v = -32767; |
81 else if (v > 32767) | 85 else if (v > 32767) |
82 v = 32767; | 86 v = 32767; |
83 return v; | 87 return v; |
84 } | |
85 | |
86 static inline int calc_lowcomp1(int a, int b0, int b1, int c) | |
87 { | |
88 if ((b0 + 256) == b1) { | |
89 a = c; | |
90 } else if (b0 > b1) { | |
91 a = FFMAX(a - 64, 0); | |
92 } | |
93 return a; | |
94 } | |
95 | |
96 static inline int calc_lowcomp(int a, int b0, int b1, int bin) | |
97 { | |
98 if (bin < 7) { | |
99 return calc_lowcomp1(a, b0, b1, 384); | |
100 } else if (bin < 20) { | |
101 return calc_lowcomp1(a, b0, b1, 320); | |
102 } else { | |
103 return FFMAX(a - 128, 0); | |
104 } | |
105 } | |
106 | |
107 /* AC3 bit allocation. The algorithm is the one described in the AC3 | |
108 spec. */ | |
109 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap, | |
110 int8_t *exp, int start, int end, | |
111 int snroffset, int fgain, int is_lfe, | |
112 int deltbae,int deltnseg, | |
113 uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba) | |
114 { | |
115 int bin,i,j,k,end1,v,bndstrt,bndend,lowcomp,begin; | |
116 int fastleak,slowleak,address,tmp; | |
117 int16_t psd[256]; /* scaled exponents */ | |
118 int16_t bndpsd[50]; /* interpolated exponents */ | |
119 int16_t excite[50]; /* excitation */ | |
120 int16_t mask[50]; /* masking value */ | |
121 | |
122 /* exponent mapping to PSD */ | |
123 for(bin=start;bin<end;bin++) { | |
124 psd[bin]=(3072 - (exp[bin] << 7)); | |
125 } | |
126 | |
127 /* PSD integration */ | |
128 j=start; | |
129 k=masktab[start]; | |
130 do { | |
131 v=psd[j]; | |
132 j++; | |
133 end1 = FFMIN(bndtab[k+1], end); | |
134 for(i=j;i<end1;i++) { | |
135 /* logadd */ | |
136 int adr = FFMIN(FFABS(v - psd[j]) >> 1, 255); | |
137 v = FFMAX(v, psd[j]) + latab[adr]; | |
138 j++; | |
139 } | |
140 bndpsd[k]=v; | |
141 k++; | |
142 } while (end > bndtab[k]); | |
143 | |
144 /* excitation function */ | |
145 bndstrt = masktab[start]; | |
146 bndend = masktab[end-1] + 1; | |
147 | |
148 if (bndstrt == 0) { | |
149 lowcomp = 0; | |
150 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1], 384); | |
151 excite[0] = bndpsd[0] - fgain - lowcomp; | |
152 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2], 384); | |
153 excite[1] = bndpsd[1] - fgain - lowcomp; | |
154 begin = 7; | |
155 for (bin = 2; bin < 7; bin++) { | |
156 if (!(is_lfe && bin == 6)) | |
157 lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1], 384); | |
158 fastleak = bndpsd[bin] - fgain; | |
159 slowleak = bndpsd[bin] - s->sgain; | |
160 excite[bin] = fastleak - lowcomp; | |
161 if (!(is_lfe && bin == 6)) { | |
162 if (bndpsd[bin] <= bndpsd[bin+1]) { | |
163 begin = bin + 1; | |
164 break; | |
165 } | |
166 } | |
167 } | |
168 | |
169 end1=bndend; | |
170 if (end1 > 22) end1=22; | |
171 | |
172 for (bin = begin; bin < end1; bin++) { | |
173 if (!(is_lfe && bin == 6)) | |
174 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin); | |
175 | |
176 fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain); | |
177 slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain); | |
178 excite[bin] = FFMAX(fastleak - lowcomp, slowleak); | |
179 } | |
180 begin = 22; | |
181 } else { | |
182 /* coupling channel */ | |
183 begin = bndstrt; | |
184 | |
185 fastleak = (s->cplfleak << 8) + 768; | |
186 slowleak = (s->cplsleak << 8) + 768; | |
187 } | |
188 | |
189 for (bin = begin; bin < bndend; bin++) { | |
190 fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain); | |
191 slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain); | |
192 excite[bin] = FFMAX(fastleak, slowleak); | |
193 } | |
194 | |
195 /* compute masking curve */ | |
196 | |
197 for (bin = bndstrt; bin < bndend; bin++) { | |
198 tmp = s->dbknee - bndpsd[bin]; | |
199 if (tmp > 0) { | |
200 excite[bin] += tmp >> 2; | |
201 } | |
202 mask[bin] = FFMAX(hth[bin >> s->halfratecod][s->fscod], excite[bin]); | |
203 } | |
204 | |
205 /* delta bit allocation */ | |
206 | |
207 if (deltbae == 0 || deltbae == 1) { | |
208 int band, seg, delta; | |
209 band = 0; | |
210 for (seg = 0; seg < deltnseg; seg++) { | |
211 band += deltoffst[seg]; | |
212 if (deltba[seg] >= 4) { | |
213 delta = (deltba[seg] - 3) << 7; | |
214 } else { | |
215 delta = (deltba[seg] - 4) << 7; | |
216 } | |
217 for (k = 0; k < deltlen[seg]; k++) { | |
218 mask[band] += delta; | |
219 band++; | |
220 } | |
221 } | |
222 } | |
223 | |
224 /* compute bit allocation */ | |
225 | |
226 i = start; | |
227 j = masktab[start]; | |
228 do { | |
229 v = (FFMAX(mask[j] - snroffset - s->floor, 0) & 0x1FE0) + s->floor; | |
230 end1 = FFMIN(bndtab[j] + bndsz[j], end); | |
231 for (k = i; k < end1; k++) { | |
232 address = av_clip((psd[i] - v) >> 5, 0, 63); | |
233 bap[i] = baptab[address]; | |
234 i++; | |
235 } | |
236 } while (end > bndtab[j++]); | |
237 } | 88 } |
238 | 89 |
239 typedef struct IComplex { | 90 typedef struct IComplex { |
240 short re,im; | 91 short re,im; |
241 } IComplex; | 92 } IComplex; |
597 ac3_parametric_bit_allocation(&s->bit_alloc, | 448 ac3_parametric_bit_allocation(&s->bit_alloc, |
598 bap[i][ch], (int8_t *)encoded_exp[i][ch], | 449 bap[i][ch], (int8_t *)encoded_exp[i][ch], |
599 0, s->nb_coefs[ch], | 450 0, s->nb_coefs[ch], |
600 (((csnroffst-15) << 4) + | 451 (((csnroffst-15) << 4) + |
601 fsnroffst) << 2, | 452 fsnroffst) << 2, |
602 fgaintab[s->fgaincod[ch]], | 453 ff_fgaintab[s->fgaincod[ch]], |
603 ch == s->lfe_channel, | 454 ch == s->lfe_channel, |
604 2, 0, NULL, NULL, NULL); | 455 2, 0, NULL, NULL, NULL); |
605 frame_bits += compute_mantissa_size(s, bap[i][ch], | 456 frame_bits += compute_mantissa_size(s, bap[i][ch], |
606 s->nb_coefs[ch]); | 457 s->nb_coefs[ch]); |
607 } | 458 } |
637 s->fgaincod[ch] = 4; | 488 s->fgaincod[ch] = 4; |
638 | 489 |
639 /* compute real values */ | 490 /* compute real values */ |
640 s->bit_alloc.fscod = s->fscod; | 491 s->bit_alloc.fscod = s->fscod; |
641 s->bit_alloc.halfratecod = s->halfratecod; | 492 s->bit_alloc.halfratecod = s->halfratecod; |
642 s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod; | 493 s->bit_alloc.sdecay = ff_sdecaytab[s->sdecaycod] >> s->halfratecod; |
643 s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod; | 494 s->bit_alloc.fdecay = ff_fdecaytab[s->fdecaycod] >> s->halfratecod; |
644 s->bit_alloc.sgain = sgaintab[s->sgaincod]; | 495 s->bit_alloc.sgain = ff_sgaintab[s->sgaincod]; |
645 s->bit_alloc.dbknee = dbkneetab[s->dbkneecod]; | 496 s->bit_alloc.dbknee = ff_dbkneetab[s->dbkneecod]; |
646 s->bit_alloc.floor = floortab[s->floorcod]; | 497 s->bit_alloc.floor = ff_floortab[s->floorcod]; |
647 | 498 |
648 /* header size */ | 499 /* header size */ |
649 frame_bits += 65; | 500 frame_bits += 65; |
650 // if (s->acmod == 2) | 501 // if (s->acmod == 2) |
651 // frame_bits += 2; | 502 // frame_bits += 2; |
738 } | 589 } |
739 } | 590 } |
740 #endif | 591 #endif |
741 return 0; | 592 return 0; |
742 } | 593 } |
743 | |
744 void ac3_common_init(void) | |
745 { | |
746 int i, j, k, l, v; | |
747 /* compute bndtab and masktab from bandsz */ | |
748 k = 0; | |
749 l = 0; | |
750 for(i=0;i<50;i++) { | |
751 bndtab[i] = l; | |
752 v = bndsz[i]; | |
753 for(j=0;j<v;j++) masktab[k++]=i; | |
754 l += v; | |
755 } | |
756 bndtab[50] = l; | |
757 } | |
758 | |
759 | 594 |
760 static int AC3_encode_init(AVCodecContext *avctx) | 595 static int AC3_encode_init(AVCodecContext *avctx) |
761 { | 596 { |
762 int freq = avctx->sample_rate; | 597 int freq = avctx->sample_rate; |
763 int bitrate = avctx->bit_rate; | 598 int bitrate = avctx->bit_rate; |
786 s->lfe_channel = s->lfe ? 5 : -1; | 621 s->lfe_channel = s->lfe ? 5 : -1; |
787 | 622 |
788 /* frequency */ | 623 /* frequency */ |
789 for(i=0;i<3;i++) { | 624 for(i=0;i<3;i++) { |
790 for(j=0;j<3;j++) | 625 for(j=0;j<3;j++) |
791 if ((ac3_freqs[j] >> i) == freq) | 626 if ((ff_ac3_freqs[j] >> i) == freq) |
792 goto found; | 627 goto found; |
793 } | 628 } |
794 return -1; | 629 return -1; |
795 found: | 630 found: |
796 s->sample_rate = freq; | 631 s->sample_rate = freq; |
800 s->bsmod = 0; /* complete main audio service */ | 635 s->bsmod = 0; /* complete main audio service */ |
801 | 636 |
802 /* bitrate & frame size */ | 637 /* bitrate & frame size */ |
803 bitrate /= 1000; | 638 bitrate /= 1000; |
804 for(i=0;i<19;i++) { | 639 for(i=0;i<19;i++) { |
805 if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate) | 640 if ((ff_ac3_bitratetab[i] >> s->halfratecod) == bitrate) |
806 break; | 641 break; |
807 } | 642 } |
808 if (i == 19) | 643 if (i == 19) |
809 return -1; | 644 return -1; |
810 s->bit_rate = bitrate; | 645 s->bit_rate = bitrate; |
1314 } | 1149 } |
1315 | 1150 |
1316 /* apply the MDCT window */ | 1151 /* apply the MDCT window */ |
1317 for(j=0;j<N/2;j++) { | 1152 for(j=0;j<N/2;j++) { |
1318 input_samples[j] = MUL16(input_samples[j], | 1153 input_samples[j] = MUL16(input_samples[j], |
1319 ac3_window[j]) >> 15; | 1154 ff_ac3_window[j]) >> 15; |
1320 input_samples[N-j-1] = MUL16(input_samples[N-j-1], | 1155 input_samples[N-j-1] = MUL16(input_samples[N-j-1], |
1321 ac3_window[j]) >> 15; | 1156 ff_ac3_window[j]) >> 15; |
1322 } | 1157 } |
1323 | 1158 |
1324 /* Normalize the samples to use the maximum available | 1159 /* Normalize the samples to use the maximum available |
1325 precision */ | 1160 precision */ |
1326 v = 14 - log2_tab(input_samples, N); | 1161 v = 14 - log2_tab(input_samples, N); |