author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /core/multimedia/opieplayer/libmpeg3/audio/bit_allocation.c | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'core/multimedia/opieplayer/libmpeg3/audio/bit_allocation.c') (more/less context) (show whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmpeg3/audio/bit_allocation.c | 586 |
1 files changed, 586 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/libmpeg3/audio/bit_allocation.c b/core/multimedia/opieplayer/libmpeg3/audio/bit_allocation.c new file mode 100644 index 0000000..29df7d7 --- a/dev/null +++ b/core/multimedia/opieplayer/libmpeg3/audio/bit_allocation.c | |||
@@ -0,0 +1,586 @@ | |||
1 | /* | ||
2 | * | ||
3 | *Copyright (C) Aaron Holtzman - May 1999 | ||
4 | * | ||
5 | * | ||
6 | * This file is part of libmpeg3 | ||
7 | * | ||
8 | * libmpeg3 is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2, or (at your option) | ||
11 | * any later version. | ||
12 | * | ||
13 | * libmpeg3 is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with GNU Make; see the file COPYING. If not, write to | ||
20 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include "mpeg3audio.h" | ||
25 | #include "../libmpeg3.h" | ||
26 | #include "../mpeg3protos.h" | ||
27 | #include <string.h> | ||
28 | #include <stdlib.h> | ||
29 | |||
30 | /* Bit allocation tables */ | ||
31 | |||
32 | static short mpeg3_slowdec[] = { 0x0f, 0x11, 0x13, 0x15 }; | ||
33 | static short mpeg3_fastdec[] = { 0x3f, 0x53, 0x67, 0x7b }; | ||
34 | static short mpeg3_slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 }; | ||
35 | static short mpeg3_dbpbtab[] = { 0x000, 0x700, 0x900, 0xb00 }; | ||
36 | |||
37 | static unsigned short mpeg3_floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 }; | ||
38 | static short mpeg3_fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400 }; | ||
39 | |||
40 | |||
41 | static short mpeg3_bndtab[] = | ||
42 | { | ||
43 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | ||
44 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, | ||
45 | 20, 21, 22, 23, 24, 25, 26, 27, 28, 31, | ||
46 | 34, 37, 40, 43, 46, 49, 55, 61, 67, 73, | ||
47 | 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 | ||
48 | }; | ||
49 | |||
50 | static short mpeg3_bndsz[] = | ||
51 | { | ||
52 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
53 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | ||
54 | 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, | ||
55 | 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, | ||
56 | 6, 12, 12, 12, 12, 24, 24, 24, 24, 24 | ||
57 | }; | ||
58 | |||
59 | static short mpeg3_masktab[] = | ||
60 | { | ||
61 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | ||
62 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29, | ||
63 | 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, | ||
64 | 34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37, | ||
65 | 37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40, | ||
66 | 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, | ||
67 | 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, | ||
68 | 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, | ||
69 | 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, | ||
70 | 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, | ||
71 | 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, | ||
72 | 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, | ||
73 | 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, | ||
74 | 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, | ||
75 | 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, | ||
76 | 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 0, 0, 0 | ||
77 | }; | ||
78 | |||
79 | |||
80 | static short mpeg3_latab[] = | ||
81 | { | ||
82 | 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039, | ||
83 | 0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032, | ||
84 | 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c, | ||
85 | 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026, | ||
86 | 0x0025, 0x0024, 0x0024, 0x0023, 0x0023, 0x0022, 0x0021, 0x0021, | ||
87 | 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c, | ||
88 | 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018, | ||
89 | 0x0018, 0x0017, 0x0017, 0x0016, 0x0016, 0x0015, 0x0015, 0x0015, | ||
90 | 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012, | ||
91 | 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f, | ||
92 | 0x000f, 0x000e, 0x000e, 0x000e, 0x000d, 0x000d, 0x000d, 0x000d, | ||
93 | 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b, | ||
94 | 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009, | ||
95 | 0x0009, 0x0009, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, | ||
96 | 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006, | ||
97 | 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005, | ||
98 | 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004, | ||
99 | 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, | ||
100 | 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, | ||
101 | 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0002, | ||
102 | 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, | ||
103 | 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, | ||
104 | 0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, | ||
105 | 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, | ||
106 | 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, | ||
107 | 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, | ||
108 | 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
109 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
110 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
111 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
112 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
113 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | ||
114 | 0x0000, 0x0000, 0x0000, 0x0000 | ||
115 | }; | ||
116 | |||
117 | static short mpeg3_hth[][50] = | ||
118 | { | ||
119 | { | ||
120 | 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0, | ||
121 | 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, | ||
122 | 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350, | ||
123 | 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, | ||
124 | 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0340, 0x0390, 0x03e0, 0x0420, | ||
125 | 0x0460, 0x0490, 0x04a0, 0x0460, 0x0440, 0x0440, 0x0520, 0x0800, | ||
126 | 0x0840, 0x0840 | ||
127 | }, | ||
128 | |||
129 | { | ||
130 | 0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0, | ||
131 | 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, | ||
132 | 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, | ||
133 | 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0, | ||
134 | 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0320, 0x0350, 0x0390, 0x03e0, | ||
135 | 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630, | ||
136 | 0x0840, 0x0840 | ||
137 | }, | ||
138 | |||
139 | { | ||
140 | 0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0, | ||
141 | 0x03c0, 0x03b0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, | ||
142 | 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390, | ||
143 | 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330, | ||
144 | 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310, | ||
145 | 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440, | ||
146 | 0x0450, 0x04e0 | ||
147 | } | ||
148 | }; | ||
149 | |||
150 | |||
151 | static short mpeg3_baptab[] = | ||
152 | { | ||
153 | 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, | ||
154 | 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, | ||
155 | 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, | ||
156 | 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 | ||
157 | }; | ||
158 | |||
159 | |||
160 | inline int logadd(int a, int b) | ||
161 | { | ||
162 | int c; | ||
163 | int address; | ||
164 | |||
165 | c = a - b; | ||
166 | address = mpeg3_min((abs(c) >> 1), 255); | ||
167 | |||
168 | if(c >= 0) | ||
169 | return(a + mpeg3_latab[address]); | ||
170 | else | ||
171 | return(b + mpeg3_latab[address]); | ||
172 | } | ||
173 | |||
174 | |||
175 | int mpeg3audio_ac3_calc_lowcomp(int a, int b0, int b1, int bin) | ||
176 | { | ||
177 | if(bin < 7) | ||
178 | { | ||
179 | if((b0 + 256) == b1) | ||
180 | a = 384; | ||
181 | else | ||
182 | if(b0 > b1) | ||
183 | a = mpeg3_max(0, a - 64); | ||
184 | } | ||
185 | else if(bin < 20) | ||
186 | { | ||
187 | if((b0 + 256) == b1) | ||
188 | a = 320; | ||
189 | else if(b0 > b1) | ||
190 | a = mpeg3_max(0, a - 64) ; | ||
191 | } | ||
192 | else | ||
193 | a = mpeg3_max(0, a - 128); | ||
194 | |||
195 | return(a); | ||
196 | } | ||
197 | |||
198 | void mpeg3audio_ac3_ba_compute_psd(int start, | ||
199 | int end, | ||
200 | unsigned short exps[], | ||
201 | short psd[], | ||
202 | short bndpsd[]) | ||
203 | { | ||
204 | int bin,i,j,k; | ||
205 | int lastbin = 0; | ||
206 | |||
207 | /* Map the exponents into dBs */ | ||
208 | for (bin = start; bin < end; bin++) | ||
209 | { | ||
210 | psd[bin] = (3072 - (exps[bin] << 7)); | ||
211 | } | ||
212 | |||
213 | /* Integrate the psd function over each bit allocation band */ | ||
214 | j = start; | ||
215 | k = mpeg3_masktab[start]; | ||
216 | |||
217 | do | ||
218 | { | ||
219 | lastbin = mpeg3_min(mpeg3_bndtab[k] + mpeg3_bndsz[k], end); | ||
220 | bndpsd[k] = psd[j]; | ||
221 | j++; | ||
222 | |||
223 | for(i = j; i < lastbin; i++) | ||
224 | { | ||
225 | bndpsd[k] = logadd(bndpsd[k], psd[j]); | ||
226 | j++; | ||
227 | } | ||
228 | |||
229 | k++; | ||
230 | }while(end > lastbin); | ||
231 | } | ||
232 | |||
233 | void mpeg3audio_ac3_ba_compute_excitation(mpeg3audio_t *audio, | ||
234 | int start, | ||
235 | int end, | ||
236 | int fgain, | ||
237 | int fastleak, | ||
238 | int slowleak, | ||
239 | int is_lfe, | ||
240 | short bndpsd[], | ||
241 | short excite[]) | ||
242 | { | ||
243 | int bin; | ||
244 | int bndstrt; | ||
245 | int bndend; | ||
246 | int lowcomp = 0; | ||
247 | int begin = 0; | ||
248 | |||
249 | /* Compute excitation function */ | ||
250 | bndstrt = mpeg3_masktab[start]; | ||
251 | bndend = mpeg3_masktab[end - 1] + 1; | ||
252 | |||
253 | if(bndstrt == 0) /* For fbw and lfe channels */ | ||
254 | { | ||
255 | lowcomp = mpeg3audio_ac3_calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0); | ||
256 | excite[0] = bndpsd[0] - fgain - lowcomp; | ||
257 | lowcomp = mpeg3audio_ac3_calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1); | ||
258 | excite[1] = bndpsd[1] - fgain - lowcomp; | ||
259 | begin = 7 ; | ||
260 | |||
261 | /* Note: Do not call mpeg3audio_ac3_calc_lowcomp() for the last band of the lfe channel, (bin = 6) */ | ||
262 | for (bin = 2; bin < 7; bin++) | ||
263 | { | ||
264 | if(!(is_lfe && (bin == 6))) | ||
265 | lowcomp = mpeg3audio_ac3_calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin); | ||
266 | fastleak = bndpsd[bin] - fgain; | ||
267 | slowleak = bndpsd[bin] - audio->ac3_bit_allocation.sgain; | ||
268 | excite[bin] = fastleak - lowcomp; | ||
269 | |||
270 | if(!(is_lfe && (bin == 6))) | ||
271 | { | ||
272 | if(bndpsd[bin] <= bndpsd[bin+1]) | ||
273 | { | ||
274 | begin = bin + 1 ; | ||
275 | break; | ||
276 | } | ||
277 | } | ||
278 | } | ||
279 | |||
280 | for (bin = begin; bin < mpeg3_min(bndend, 22); bin++) | ||
281 | { | ||
282 | if (!(is_lfe && (bin == 6))) | ||
283 | lowcomp = mpeg3audio_ac3_calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin); | ||
284 | fastleak -= audio->ac3_bit_allocation.fdecay; | ||
285 | fastleak = mpeg3_max(fastleak, bndpsd[bin] - fgain); | ||
286 | slowleak -= audio->ac3_bit_allocation.sdecay; | ||
287 | slowleak = mpeg3_max(slowleak, bndpsd[bin] - audio->ac3_bit_allocation.sgain); | ||
288 | excite[bin] = mpeg3_max(fastleak - lowcomp, slowleak); | ||
289 | } | ||
290 | begin = 22; | ||
291 | } | ||
292 | else /* For coupling channel */ | ||
293 | { | ||
294 | begin = bndstrt; | ||
295 | } | ||
296 | |||
297 | for (bin = begin; bin < bndend; bin++) | ||
298 | { | ||
299 | fastleak -= audio->ac3_bit_allocation.fdecay; | ||
300 | fastleak = mpeg3_max(fastleak, bndpsd[bin] - fgain); | ||
301 | slowleak -= audio->ac3_bit_allocation.sdecay; | ||
302 | slowleak = mpeg3_max(slowleak, bndpsd[bin] - audio->ac3_bit_allocation.sgain); | ||
303 | excite[bin] = mpeg3_max(fastleak, slowleak) ; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | void mpeg3audio_ac3_ba_compute_mask(mpeg3audio_t *audio, | ||
308 | int start, | ||
309 | int end, | ||
310 | int fscod, | ||
311 | int deltbae, | ||
312 | int deltnseg, | ||
313 | short deltoffst[], | ||
314 | short deltba[], | ||
315 | short deltlen[], | ||
316 | short excite[], | ||
317 | short mask[]) | ||
318 | { | ||
319 | int bin, k; | ||
320 | int bndstrt; | ||
321 | int bndend; | ||
322 | int delta; | ||
323 | |||
324 | bndstrt = mpeg3_masktab[start]; | ||
325 | bndend = mpeg3_masktab[end - 1] + 1; | ||
326 | |||
327 | /* Compute the masking curve */ | ||
328 | |||
329 | for (bin = bndstrt; bin < bndend; bin++) | ||
330 | { | ||
331 | if (audio->ac3_bit_allocation.bndpsd[bin] < audio->ac3_bit_allocation.dbknee) | ||
332 | { | ||
333 | excite[bin] += ((audio->ac3_bit_allocation.dbknee - audio->ac3_bit_allocation.bndpsd[bin]) >> 2); | ||
334 | } | ||
335 | mask[bin] = mpeg3_max(excite[bin], mpeg3_hth[fscod][bin]); | ||
336 | } | ||
337 | |||
338 | /* Perform delta bit modulation if necessary */ | ||
339 | if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW)) | ||
340 | { | ||
341 | int band = 0; | ||
342 | int seg = 0; | ||
343 | |||
344 | for (seg = 0; seg < deltnseg + 1; seg++) | ||
345 | { | ||
346 | band += deltoffst[seg]; | ||
347 | if (deltba[seg] >= 4) | ||
348 | { | ||
349 | delta = (deltba[seg] - 3) << 7; | ||
350 | } | ||
351 | else | ||
352 | { | ||
353 | delta = (deltba[seg] - 4) << 7; | ||
354 | } | ||
355 | |||
356 | for (k = 0; k < deltlen[seg]; k++) | ||
357 | { | ||
358 | mask[band] += delta; | ||
359 | band++; | ||
360 | } | ||
361 | } | ||
362 | } | ||
363 | } | ||
364 | |||
365 | void mpeg3audio_ac3_ba_compute_bap(mpeg3audio_t *audio, | ||
366 | int start, | ||
367 | int end, | ||
368 | int snroffset, | ||
369 | short psd[], | ||
370 | short mask[], | ||
371 | short bap[]) | ||
372 | { | ||
373 | int i, j, k; | ||
374 | short lastbin = 0; | ||
375 | short address = 0; | ||
376 | |||
377 | /* Compute the bit allocation pointer for each bin */ | ||
378 | i = start; | ||
379 | j = mpeg3_masktab[start]; | ||
380 | |||
381 | do | ||
382 | { | ||
383 | lastbin = mpeg3_min(mpeg3_bndtab[j] + mpeg3_bndsz[j], end); | ||
384 | mask[j] -= snroffset; | ||
385 | mask[j] -= audio->ac3_bit_allocation.floor; | ||
386 | |||
387 | if(mask[j] < 0) | ||
388 | mask[j] = 0; | ||
389 | |||
390 | mask[j] &= 0x1fe0; | ||
391 | mask[j] += audio->ac3_bit_allocation.floor; | ||
392 | for(k = i; k < lastbin; k++) | ||
393 | { | ||
394 | address = (psd[i] - mask[j]) >> 5; | ||
395 | address = mpeg3_min(63, mpeg3_max(0, address)); | ||
396 | bap[i] = mpeg3_baptab[address]; | ||
397 | i++; | ||
398 | } | ||
399 | j++; | ||
400 | }while (end > lastbin); | ||
401 | } | ||
402 | |||
403 | int mpeg3audio_ac3_bit_allocate(mpeg3audio_t *audio, | ||
404 | unsigned int fscod, | ||
405 | mpeg3_ac3bsi_t *bsi, | ||
406 | mpeg3_ac3audblk_t *audblk) | ||
407 | { | ||
408 | int result = 0; | ||
409 | int i; | ||
410 | int fgain; | ||
411 | int snroffset; | ||
412 | int start; | ||
413 | int end; | ||
414 | int fastleak; | ||
415 | int slowleak; | ||
416 | |||
417 | /*printf("mpeg3audio_ac3_bit_allocate %d %d %d %d %d\n", audblk->sdcycod, audblk->fdcycod, audblk->sgaincod, audblk->dbpbcod, audblk->floorcod); */ | ||
418 | /* Only perform bit_allocation if the exponents have changed or we | ||
419 | * have new sideband information */ | ||
420 | if(audblk->chexpstr[0] == 0 && audblk->chexpstr[1] == 0 && | ||
421 | audblk->chexpstr[2] == 0 && audblk->chexpstr[3] == 0 && | ||
422 | audblk->chexpstr[4] == 0 && audblk->cplexpstr == 0 && | ||
423 | audblk->lfeexpstr == 0 && audblk->baie == 0 && | ||
424 | audblk->snroffste == 0 && audblk->deltbaie == 0) | ||
425 | return 0; | ||
426 | |||
427 | /* Do some setup before we do the bit alloc */ | ||
428 | audio->ac3_bit_allocation.sdecay = mpeg3_slowdec[audblk->sdcycod]; | ||
429 | audio->ac3_bit_allocation.fdecay = mpeg3_fastdec[audblk->fdcycod]; | ||
430 | audio->ac3_bit_allocation.sgain = mpeg3_slowgain[audblk->sgaincod]; | ||
431 | audio->ac3_bit_allocation.dbknee = mpeg3_dbpbtab[audblk->dbpbcod]; | ||
432 | audio->ac3_bit_allocation.floor = mpeg3_floortab[audblk->floorcod]; | ||
433 | |||
434 | /* if all the SNR offset constants are zero then the whole block is zero */ | ||
435 | if(!audblk->csnroffst && !audblk->fsnroffst[0] && | ||
436 | !audblk->fsnroffst[1] && !audblk->fsnroffst[2] && | ||
437 | !audblk->fsnroffst[3] && !audblk->fsnroffst[4] && | ||
438 | !audblk->cplfsnroffst && !audblk->lfefsnroffst) | ||
439 | { | ||
440 | memset(audblk->fbw_bap, 0, sizeof(short) * 256 * 5); | ||
441 | memset(audblk->cpl_bap, 0, sizeof(short) * 256); | ||
442 | memset(audblk->lfe_bap, 0, sizeof(short) * 7); | ||
443 | return 0; | ||
444 | } | ||
445 | |||
446 | for(i = 0; i < bsi->nfchans; i++) | ||
447 | { | ||
448 | start = 0; | ||
449 | end = audblk->endmant[i]; | ||
450 | fgain = mpeg3_fastgain[audblk->fgaincod[i]]; | ||
451 | snroffset = (((audblk->csnroffst - 15) << 4) + audblk->fsnroffst[i]) << 2 ; | ||
452 | fastleak = 0; | ||
453 | slowleak = 0; | ||
454 | |||
455 | mpeg3audio_ac3_ba_compute_psd(start, | ||
456 | end, | ||
457 | audblk->fbw_exp[i], | ||
458 | audio->ac3_bit_allocation.psd, | ||
459 | audio->ac3_bit_allocation.bndpsd); | ||
460 | |||
461 | mpeg3audio_ac3_ba_compute_excitation(audio, | ||
462 | start, | ||
463 | end , | ||
464 | fgain, | ||
465 | fastleak, | ||
466 | slowleak, | ||
467 | 0, | ||
468 | audio->ac3_bit_allocation.bndpsd, | ||
469 | audio->ac3_bit_allocation.excite); | ||
470 | |||
471 | mpeg3audio_ac3_ba_compute_mask(audio, | ||
472 | start, | ||
473 | end, | ||
474 | fscod, | ||
475 | audblk->deltbae[i], | ||
476 | audblk->deltnseg[i], | ||
477 | audblk->deltoffst[i], | ||
478 | audblk->deltba[i], | ||
479 | audblk->deltlen[i], | ||
480 | audio->ac3_bit_allocation.excite, | ||
481 | audio->ac3_bit_allocation.mask); | ||
482 | |||
483 | mpeg3audio_ac3_ba_compute_bap(audio, | ||
484 | start, | ||
485 | end, | ||
486 | snroffset, | ||
487 | audio->ac3_bit_allocation.psd, | ||
488 | audio->ac3_bit_allocation.mask, | ||
489 | audblk->fbw_bap[i]); | ||
490 | } | ||
491 | |||
492 | if(audblk->cplinu) | ||
493 | { | ||
494 | start = audblk->cplstrtmant; | ||
495 | end = audblk->cplendmant; | ||
496 | fgain = mpeg3_fastgain[audblk->cplfgaincod]; | ||
497 | snroffset = (((audblk->csnroffst - 15) << 4) + audblk->cplfsnroffst) << 2 ; | ||
498 | fastleak = (audblk->cplfleak << 8) + 768; | ||
499 | slowleak = (audblk->cplsleak << 8) + 768; | ||
500 | |||
501 | mpeg3audio_ac3_ba_compute_psd(start, | ||
502 | end, | ||
503 | audblk->cpl_exp, | ||
504 | audio->ac3_bit_allocation.psd, | ||
505 | audio->ac3_bit_allocation.bndpsd); | ||
506 | |||
507 | mpeg3audio_ac3_ba_compute_excitation(audio, | ||
508 | start, | ||
509 | end , | ||
510 | fgain, | ||
511 | fastleak, | ||
512 | slowleak, | ||
513 | 0, | ||
514 | audio->ac3_bit_allocation.bndpsd, | ||
515 | audio->ac3_bit_allocation.excite); | ||
516 | |||
517 | mpeg3audio_ac3_ba_compute_mask(audio, | ||
518 | start, | ||
519 | end, | ||
520 | fscod, | ||
521 | audblk->cpldeltbae, | ||
522 | audblk->cpldeltnseg, | ||
523 | audblk->cpldeltoffst, | ||
524 | audblk->cpldeltba, | ||
525 | audblk->cpldeltlen, | ||
526 | audio->ac3_bit_allocation.excite, | ||
527 | audio->ac3_bit_allocation.mask); | ||
528 | |||
529 | mpeg3audio_ac3_ba_compute_bap(audio, | ||
530 | start, | ||
531 | end, | ||
532 | snroffset, | ||
533 | audio->ac3_bit_allocation.psd, | ||
534 | audio->ac3_bit_allocation.mask, | ||
535 | audblk->cpl_bap); | ||
536 | } | ||
537 | |||
538 | if(bsi->lfeon) | ||
539 | { | ||
540 | start = 0; | ||
541 | end = 7; | ||
542 | fgain = mpeg3_fastgain[audblk->lfefgaincod]; | ||
543 | snroffset = (((audblk->csnroffst - 15) << 4) + audblk->lfefsnroffst) << 2 ; | ||
544 | fastleak = 0; | ||
545 | slowleak = 0; | ||
546 | |||
547 | mpeg3audio_ac3_ba_compute_psd(start, | ||
548 | end, | ||
549 | audblk->lfe_exp, | ||
550 | audio->ac3_bit_allocation.psd, | ||
551 | audio->ac3_bit_allocation.bndpsd); | ||
552 | |||
553 | mpeg3audio_ac3_ba_compute_excitation(audio, | ||
554 | start, | ||
555 | end , | ||
556 | fgain, | ||
557 | fastleak, | ||
558 | slowleak, | ||
559 | 1, | ||
560 | audio->ac3_bit_allocation.bndpsd, | ||
561 | audio->ac3_bit_allocation.excite); | ||
562 | |||
563 | /* Perform no delta bit allocation for lfe */ | ||
564 | mpeg3audio_ac3_ba_compute_mask(audio, | ||
565 | start, | ||
566 | end, | ||
567 | fscod, | ||
568 | 2, | ||
569 | 0, | ||
570 | 0, | ||
571 | 0, | ||
572 | 0, | ||
573 | audio->ac3_bit_allocation.excite, | ||
574 | audio->ac3_bit_allocation.mask); | ||
575 | |||
576 | mpeg3audio_ac3_ba_compute_bap(audio, | ||
577 | start, | ||
578 | end, | ||
579 | snroffset, | ||
580 | audio->ac3_bit_allocation.psd, | ||
581 | audio->ac3_bit_allocation.mask, | ||
582 | audblk->lfe_bap); | ||
583 | } | ||
584 | |||
585 | return result; | ||
586 | } | ||