Diffstat (limited to 'core/multimedia/opieplayer/libmpeg3/audio/mantissa.c') (more/less context) (show whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmpeg3/audio/mantissa.c | 387 |
1 files changed, 387 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/libmpeg3/audio/mantissa.c b/core/multimedia/opieplayer/libmpeg3/audio/mantissa.c new file mode 100644 index 0000000..05fe251 --- a/dev/null +++ b/core/multimedia/opieplayer/libmpeg3/audio/mantissa.c | |||
@@ -0,0 +1,387 @@ | |||
1 | /* | ||
2 | * | ||
3 | *mantissa.c 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 | |||
28 | |||
29 | /* Lookup tables of 0.16 two's complement quantization values */ | ||
30 | static short mpeg3_q_1[3] = | ||
31 | { | ||
32 | (-2 << 15) / 3, | ||
33 | 0, | ||
34 | (2 << 15) / 3 | ||
35 | }; | ||
36 | |||
37 | static short mpeg3_q_2[5] = | ||
38 | { | ||
39 | (-4 << 15) / 5, | ||
40 | ((-2 << 15) / 5) << 1, | ||
41 | 0, | ||
42 | (2 << 15) / 5, | ||
43 | ((4 << 15) / 5) << 1 | ||
44 | }; | ||
45 | |||
46 | static short mpeg3_q_3[7] = | ||
47 | { | ||
48 | (-6 << 15) / 7, | ||
49 | (-4 << 15) / 7, | ||
50 | (-2 << 15) / 7, | ||
51 | 0, | ||
52 | (2 << 15) / 7, | ||
53 | (4 << 15) / 7, | ||
54 | (6 << 15) / 7 | ||
55 | }; | ||
56 | |||
57 | static short mpeg3_q_4[11] = | ||
58 | { | ||
59 | (-10 << 15) / 11, | ||
60 | (-8 << 15) / 11, | ||
61 | (-6 << 15) / 11, | ||
62 | (-4 << 15) / 11, | ||
63 | (-2 << 15) / 11, | ||
64 | 0, | ||
65 | ( 2 << 15) / 11, | ||
66 | ( 4 << 15) / 11, | ||
67 | ( 6 << 15) / 11, | ||
68 | ( 8 << 15) / 11, | ||
69 | (10 << 15) / 11 | ||
70 | }; | ||
71 | |||
72 | static short mpeg3_q_5[15] = | ||
73 | { | ||
74 | (-14 << 15) / 15, | ||
75 | (-12 << 15) / 15, | ||
76 | (-10 << 15) / 15, | ||
77 | (-8 << 15) / 15, | ||
78 | (-6 << 15) / 15, | ||
79 | (-4 << 15) / 15, | ||
80 | (-2 << 15) / 15, | ||
81 | 0, | ||
82 | ( 2 << 15) / 15, | ||
83 | ( 4 << 15) / 15, | ||
84 | ( 6 << 15) / 15, | ||
85 | ( 8 << 15) / 15, | ||
86 | (10 << 15) / 15, | ||
87 | (12 << 15) / 15, | ||
88 | (14 << 15) / 15 | ||
89 | }; | ||
90 | |||
91 | static short mpeg3_qnttztab[16] = {0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16}; | ||
92 | |||
93 | |||
94 | /* */ | ||
95 | /* Scale factors for tofloat */ | ||
96 | /* */ | ||
97 | |||
98 | static const unsigned MPEG3_INT32 mpeg3_scale_factors[25] = | ||
99 | { | ||
100 | 0x38000000, /*2 ^ -(0 + 15) */ | ||
101 | 0x37800000, /*2 ^ -(1 + 15) */ | ||
102 | 0x37000000, /*2 ^ -(2 + 15) */ | ||
103 | 0x36800000, /*2 ^ -(3 + 15) */ | ||
104 | 0x36000000, /*2 ^ -(4 + 15) */ | ||
105 | 0x35800000, /*2 ^ -(5 + 15) */ | ||
106 | 0x35000000, /*2 ^ -(6 + 15) */ | ||
107 | 0x34800000, /*2 ^ -(7 + 15) */ | ||
108 | 0x34000000, /*2 ^ -(8 + 15) */ | ||
109 | 0x33800000, /*2 ^ -(9 + 15) */ | ||
110 | 0x33000000, /*2 ^ -(10 + 15) */ | ||
111 | 0x32800000, /*2 ^ -(11 + 15) */ | ||
112 | 0x32000000, /*2 ^ -(12 + 15) */ | ||
113 | 0x31800000, /*2 ^ -(13 + 15) */ | ||
114 | 0x31000000, /*2 ^ -(14 + 15) */ | ||
115 | 0x30800000, /*2 ^ -(15 + 15) */ | ||
116 | 0x30000000, /*2 ^ -(16 + 15) */ | ||
117 | 0x2f800000, /*2 ^ -(17 + 15) */ | ||
118 | 0x2f000000, /*2 ^ -(18 + 15) */ | ||
119 | 0x2e800000, /*2 ^ -(19 + 15) */ | ||
120 | 0x2e000000, /*2 ^ -(20 + 15) */ | ||
121 | 0x2d800000, /*2 ^ -(21 + 15) */ | ||
122 | 0x2d000000, /*2 ^ -(22 + 15) */ | ||
123 | 0x2c800000, /*2 ^ -(23 + 15) */ | ||
124 | 0x2c000000 /*2 ^ -(24 + 15) */ | ||
125 | }; | ||
126 | |||
127 | static MPEG3_FLOAT32 *mpeg3_scale_factor = (MPEG3_FLOAT32*)mpeg3_scale_factors; | ||
128 | |||
129 | static inline mpeg3_real_t mpeg3audio_ac3_tofloat(unsigned short exponent, int mantissa) | ||
130 | { | ||
131 | mpeg3_real_t x; | ||
132 | x = mantissa * mpeg3_scale_factor[exponent]; | ||
133 | return x; | ||
134 | } | ||
135 | |||
136 | static inline void mpeg3audio_ac3_mantissa_reset(mpeg3_ac3_mantissa_t *mantissa) | ||
137 | { | ||
138 | mantissa->m_1[2] = mantissa->m_1[1] = mantissa->m_1[0] = 0; | ||
139 | mantissa->m_2[2] = mantissa->m_2[1] = mantissa->m_2[0] = 0; | ||
140 | mantissa->m_4[1] = mantissa->m_4[0] = 0; | ||
141 | /* Force new groups to be loaded */ | ||
142 | mantissa->m_1_pointer = mantissa->m_2_pointer = mantissa->m_4_pointer = 3; | ||
143 | } | ||
144 | |||
145 | /* | ||
146 | * Generate eight bits of pseudo-entropy using a 16 bit linear | ||
147 | * feedback shift register (LFSR). The primitive polynomial used | ||
148 | * is 1 + x^4 + x^14 + x^16. | ||
149 | * | ||
150 | * The distribution is uniform, over the range [-0.707,0.707] | ||
151 | * | ||
152 | */ | ||
153 | inline unsigned int mpeg3audio_ac3_dither_gen(mpeg3audio_t *audio) | ||
154 | { | ||
155 | int i; | ||
156 | unsigned int state; | ||
157 | |||
158 | /* explicitly bring the state into a local var as gcc > 3.0? */ | ||
159 | /* doesn't know how to optimize out the stores */ | ||
160 | state = audio->ac3_lfsr_state; | ||
161 | |||
162 | /* Generate eight pseudo random bits */ | ||
163 | for(i = 0; i < 8; i++) | ||
164 | { | ||
165 | state <<= 1; | ||
166 | |||
167 | if(state & 0x10000) | ||
168 | state ^= 0xa011; | ||
169 | } | ||
170 | |||
171 | audio->ac3_lfsr_state = state; | ||
172 | return (((((int)state << 8) >> 8) * (int)(0.707106f * 256.0f)) >> 16); | ||
173 | } | ||
174 | |||
175 | |||
176 | /* Fetch an unpacked, left justified, and properly biased/dithered mantissa value */ | ||
177 | static inline unsigned short mpeg3audio_ac3_mantissa_get(mpeg3audio_t *audio, | ||
178 | unsigned short bap, | ||
179 | unsigned short dithflag) | ||
180 | { | ||
181 | unsigned short mantissa; | ||
182 | unsigned int group_code; | ||
183 | mpeg3_ac3_mantissa_t *mantissa_struct = &(audio->ac3_mantissa); | ||
184 | |||
185 | /* If the bap is 0-5 then we have special cases to take care of */ | ||
186 | switch(bap) | ||
187 | { | ||
188 | case 0: | ||
189 | if(dithflag) | ||
190 | mantissa = mpeg3audio_ac3_dither_gen(audio); | ||
191 | else | ||
192 | mantissa = 0; | ||
193 | break; | ||
194 | |||
195 | case 1: | ||
196 | if(mantissa_struct->m_1_pointer > 2) | ||
197 | { | ||
198 | group_code = mpeg3bits_getbits(audio->astream, 5); | ||
199 | |||
200 | if(group_code > 26) | ||
201 | { | ||
202 | /* FIXME do proper block error handling */ | ||
203 | fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 1 %d\n", group_code); | ||
204 | return 0; | ||
205 | } | ||
206 | |||
207 | mantissa_struct->m_1[0] = group_code / 9; | ||
208 | mantissa_struct->m_1[1] = (group_code % 9) / 3; | ||
209 | mantissa_struct->m_1[2] = (group_code % 9) % 3; | ||
210 | mantissa_struct->m_1_pointer = 0; | ||
211 | } | ||
212 | mantissa = mantissa_struct->m_1[mantissa_struct->m_1_pointer++]; | ||
213 | mantissa = mpeg3_q_1[mantissa]; | ||
214 | break; | ||
215 | |||
216 | case 2: | ||
217 | if(mantissa_struct->m_2_pointer > 2) | ||
218 | { | ||
219 | group_code = mpeg3bits_getbits(audio->astream, 7); | ||
220 | |||
221 | if(group_code > 124) | ||
222 | { | ||
223 | fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 2 %d\n", group_code); | ||
224 | return 0; | ||
225 | } | ||
226 | |||
227 | mantissa_struct->m_2[0] = group_code / 25; | ||
228 | mantissa_struct->m_2[1] = (group_code % 25) / 5; | ||
229 | mantissa_struct->m_2[2] = (group_code % 25) % 5; | ||
230 | mantissa_struct->m_2_pointer = 0; | ||
231 | } | ||
232 | mantissa = mantissa_struct->m_2[mantissa_struct->m_2_pointer++]; | ||
233 | mantissa = mpeg3_q_2[mantissa]; | ||
234 | break; | ||
235 | |||
236 | case 3: | ||
237 | mantissa = mpeg3bits_getbits(audio->astream, 3); | ||
238 | |||
239 | if(mantissa > 6) | ||
240 | { | ||
241 | fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 3 %d\n", mantissa); | ||
242 | return 0; | ||
243 | } | ||
244 | |||
245 | mantissa = mpeg3_q_3[mantissa]; | ||
246 | break; | ||
247 | |||
248 | case 4: | ||
249 | if(mantissa_struct->m_4_pointer > 1) | ||
250 | { | ||
251 | group_code = mpeg3bits_getbits(audio->astream, 7); | ||
252 | |||
253 | if(group_code > 120) | ||
254 | { | ||
255 | fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 4 %d\n", group_code); | ||
256 | return 0; | ||
257 | } | ||
258 | |||
259 | mantissa_struct->m_4[0] = group_code / 11; | ||
260 | mantissa_struct->m_4[1] = group_code % 11; | ||
261 | mantissa_struct->m_4_pointer = 0; | ||
262 | } | ||
263 | mantissa = mantissa_struct->m_4[mantissa_struct->m_4_pointer++]; | ||
264 | mantissa = mpeg3_q_4[mantissa]; | ||
265 | break; | ||
266 | |||
267 | case 5: | ||
268 | mantissa = mpeg3bits_getbits(audio->astream, 4); | ||
269 | |||
270 | if(mantissa > 14) | ||
271 | { | ||
272 | /* FIXME do proper block error handling */ | ||
273 | fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 5 %d\n", mantissa); | ||
274 | return 0; | ||
275 | } | ||
276 | |||
277 | mantissa = mpeg3_q_5[mantissa]; | ||
278 | break; | ||
279 | |||
280 | default: | ||
281 | mantissa = mpeg3bits_getbits(audio->astream, mpeg3_qnttztab[bap]); | ||
282 | mantissa <<= 16 - mpeg3_qnttztab[bap]; | ||
283 | } | ||
284 | return mantissa; | ||
285 | } | ||
286 | |||
287 | void mpeg3audio_ac3_uncouple_channel(mpeg3audio_t *audio, | ||
288 | mpeg3_real_t samples[], | ||
289 | mpeg3_ac3bsi_t *bsi, | ||
290 | mpeg3_ac3audblk_t *audblk, | ||
291 | unsigned int ch) | ||
292 | { | ||
293 | unsigned int bnd = 0; | ||
294 | unsigned int sub_bnd = 0; | ||
295 | unsigned int i, j; | ||
296 | MPEG3_FLOAT32 cpl_coord = 1.0; | ||
297 | unsigned int cpl_exp_tmp; | ||
298 | unsigned int cpl_mant_tmp; | ||
299 | short mantissa; | ||
300 | |||
301 | for(i = audblk->cplstrtmant; i < audblk->cplendmant; ) | ||
302 | { | ||
303 | if(!audblk->cplbndstrc[sub_bnd++]) | ||
304 | { | ||
305 | cpl_exp_tmp = audblk->cplcoexp[ch][bnd] + 3 * audblk->mstrcplco[ch]; | ||
306 | if(audblk->cplcoexp[ch][bnd] == 15) | ||
307 | cpl_mant_tmp = (audblk->cplcomant[ch][bnd]) << 11; | ||
308 | else | ||
309 | cpl_mant_tmp = ((0x10) | audblk->cplcomant[ch][bnd]) << 10; | ||
310 | |||
311 | cpl_coord = mpeg3audio_ac3_tofloat(cpl_exp_tmp, cpl_mant_tmp) * 8.0f; | ||
312 | |||
313 | /*Invert the phase for the right channel if necessary */ | ||
314 | if(bsi->acmod == 0x2 && audblk->phsflginu && ch == 1 && audblk->phsflg[bnd]) | ||
315 | cpl_coord *= -1; | ||
316 | |||
317 | bnd++; | ||
318 | } | ||
319 | |||
320 | for(j = 0; j < 12; j++) | ||
321 | { | ||
322 | /* Get new dither values for each channel if necessary, so */ | ||
323 | /* the channels are uncorrelated */ | ||
324 | if(audblk->dithflag[ch] && audblk->cpl_bap[i] == 0) | ||
325 | mantissa = mpeg3audio_ac3_dither_gen(audio); | ||
326 | else | ||
327 | mantissa = audblk->cplmant[i]; | ||
328 | |||
329 | samples[i] = cpl_coord * mpeg3audio_ac3_tofloat(audblk->cpl_exp[i], mantissa);; | ||
330 | |||
331 | i++; | ||
332 | } | ||
333 | } | ||
334 | return; | ||
335 | } | ||
336 | |||
337 | int mpeg3audio_ac3_coeff_unpack(mpeg3audio_t *audio, | ||
338 | mpeg3_ac3bsi_t *bsi, | ||
339 | mpeg3_ac3audblk_t *audblk, | ||
340 | mpeg3ac3_stream_samples_t samples) | ||
341 | { | ||
342 | int i, j; | ||
343 | int done_cpl = 0; | ||
344 | short mantissa; | ||
345 | |||
346 | mpeg3audio_ac3_mantissa_reset(&(audio->ac3_mantissa)); | ||
347 | |||
348 | for(i = 0; i < bsi->nfchans && !mpeg3bits_error(audio->astream); i++) | ||
349 | { | ||
350 | for(j = 0; j < audblk->endmant[i] && !mpeg3bits_error(audio->astream); j++) | ||
351 | { | ||
352 | mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->fbw_bap[i][j], audblk->dithflag[i]); | ||
353 | samples[i][j] = mpeg3audio_ac3_tofloat(audblk->fbw_exp[i][j], mantissa); | ||
354 | } | ||
355 | |||
356 | if(audblk->cplinu && audblk->chincpl[i] && !(done_cpl) && !mpeg3bits_error(audio->astream)) | ||
357 | { | ||
358 | /* ncplmant is equal to 12 * ncplsubnd */ | ||
359 | /* Don't dither coupling channel until channel separation so that | ||
360 | * interchannel noise is uncorrelated */ | ||
361 | for(j = audblk->cplstrtmant; | ||
362 | j < audblk->cplendmant && !mpeg3bits_error(audio->astream); | ||
363 | j++) | ||
364 | { | ||
365 | audblk->cplmant[j] = mpeg3audio_ac3_mantissa_get(audio, audblk->cpl_bap[j], 0); | ||
366 | } | ||
367 | done_cpl = 1; | ||
368 | } | ||
369 | } | ||
370 | |||
371 | /* Uncouple the channel */ | ||
372 | if(audblk->cplinu) | ||
373 | { | ||
374 | if(audblk->chincpl[i]) | ||
375 | mpeg3audio_ac3_uncouple_channel(audio, samples[i], bsi, audblk, i); | ||
376 | } | ||
377 | |||
378 | if(bsi->lfeon && !mpeg3bits_error(audio->astream)) | ||
379 | { | ||
380 | /* There are always 7 mantissas for lfe, no dither for lfe */ | ||
381 | for(j = 0; j < 7 && !mpeg3bits_error(audio->astream); j++) | ||
382 | mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->lfe_bap[j], 0); | ||
383 | samples[5][j] = mpeg3audio_ac3_tofloat(audblk->lfe_exp[j], mantissa); | ||
384 | } | ||
385 | |||
386 | return mpeg3bits_error(audio->astream); | ||
387 | } | ||