Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / audio / mantissa.c
blob110a91babae8561822b3b869eded79d43d3bddf4
1 /*
3 * mantissa.c Copyright (C) Aaron Holtzman - May 1999
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.
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.
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.
24 #include "mpeg3audio.h"
27 /* Lookup tables of 0.16 two's complement quantization values */
28 static short mpeg3_q_1[3] =
30 (-2 << 15) / 3,
31 0,
32 (2 << 15) / 3
35 static short mpeg3_q_2[5] =
37 (-4 << 15) / 5,
38 ((-2 << 15) / 5) << 1,
40 (2 << 15) / 5,
41 ((4 << 15) / 5) << 1
44 static short mpeg3_q_3[7] =
46 (-6 << 15) / 7,
47 (-4 << 15) / 7,
48 (-2 << 15) / 7,
49 0,
50 (2 << 15) / 7,
51 (4 << 15) / 7,
52 (6 << 15) / 7
55 static short mpeg3_q_4[11] =
57 (-10 << 15) / 11,
58 (-8 << 15) / 11,
59 (-6 << 15) / 11,
60 (-4 << 15) / 11,
61 (-2 << 15) / 11,
63 ( 2 << 15) / 11,
64 ( 4 << 15) / 11,
65 ( 6 << 15) / 11,
66 ( 8 << 15) / 11,
67 (10 << 15) / 11
70 static short mpeg3_q_5[15] =
72 (-14 << 15) / 15,
73 (-12 << 15) / 15,
74 (-10 << 15) / 15,
75 (-8 << 15) / 15,
76 (-6 << 15) / 15,
77 (-4 << 15) / 15,
78 (-2 << 15) / 15,
79 0,
80 ( 2 << 15) / 15,
81 ( 4 << 15) / 15,
82 ( 6 << 15) / 15,
83 ( 8 << 15) / 15,
84 (10 << 15) / 15,
85 (12 << 15) / 15,
86 (14 << 15) / 15
89 static short mpeg3_qnttztab[16] = {0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16};
92 /* */
93 /* Scale factors for tofloat */
94 /* */
96 static const u_int32_t mpeg3_scale_factors[25] =
98 0x38000000, /*2 ^ -(0 + 15) */
99 0x37800000, /*2 ^ -(1 + 15) */
100 0x37000000, /*2 ^ -(2 + 15) */
101 0x36800000, /*2 ^ -(3 + 15) */
102 0x36000000, /*2 ^ -(4 + 15) */
103 0x35800000, /*2 ^ -(5 + 15) */
104 0x35000000, /*2 ^ -(6 + 15) */
105 0x34800000, /*2 ^ -(7 + 15) */
106 0x34000000, /*2 ^ -(8 + 15) */
107 0x33800000, /*2 ^ -(9 + 15) */
108 0x33000000, /*2 ^ -(10 + 15) */
109 0x32800000, /*2 ^ -(11 + 15) */
110 0x32000000, /*2 ^ -(12 + 15) */
111 0x31800000, /*2 ^ -(13 + 15) */
112 0x31000000, /*2 ^ -(14 + 15) */
113 0x30800000, /*2 ^ -(15 + 15) */
114 0x30000000, /*2 ^ -(16 + 15) */
115 0x2f800000, /*2 ^ -(17 + 15) */
116 0x2f000000, /*2 ^ -(18 + 15) */
117 0x2e800000, /*2 ^ -(19 + 15) */
118 0x2e000000, /*2 ^ -(20 + 15) */
119 0x2d800000, /*2 ^ -(21 + 15) */
120 0x2d000000, /*2 ^ -(22 + 15) */
121 0x2c800000, /*2 ^ -(23 + 15) */
122 0x2c000000 /*2 ^ -(24 + 15) */
125 static MPEG3_FLOAT32 *mpeg3_scale_factor = (MPEG3_FLOAT32*)mpeg3_scale_factors;
127 #define CLIP(x, y, z) ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
129 static float mpeg3audio_ac3_tofloat(unsigned short exponent, int mantissa)
131 float x;
133 x = mantissa * mpeg3_scale_factor[CLIP(exponent, 0, 25)];
134 //printf(__FUNCTION__ " %f\n", x);
136 return x;
139 static void mpeg3audio_ac3_mantissa_reset(mpeg3_ac3_mantissa_t *mantissa)
141 mantissa->m_1[2] = mantissa->m_1[1] = mantissa->m_1[0] = 0;
142 mantissa->m_2[2] = mantissa->m_2[1] = mantissa->m_2[0] = 0;
143 mantissa->m_4[1] = mantissa->m_4[0] = 0;
144 /* Force new groups to be loaded */
145 mantissa->m_1_pointer = mantissa->m_2_pointer = mantissa->m_4_pointer = 3;
149 * Generate eight bits of pseudo-entropy using a 16 bit linear
150 * feedback shift register (LFSR). The primitive polynomial used
151 * is 1 + x^4 + x^14 + x^16.
153 * The distribution is uniform, over the range [-0.707,0.707]
156 static unsigned int mpeg3audio_ac3_dither_gen(mpeg3audio_t *audio)
158 int i;
159 unsigned int state;
161 /* explicitly bring the state into a local var as gcc > 3.0? */
162 /* doesn't know how to optimize out the stores */
163 state = audio->ac3_lfsr_state;
165 /* Generate eight pseudo random bits */
166 for(i = 0; i < 8; i++)
168 state <<= 1;
170 if(state & 0x10000)
171 state ^= 0xa011;
174 audio->ac3_lfsr_state = state;
175 return (((((int)state << 8) >> 8) * (int)(0.707106f * 256.0f)) >> 16);
179 /* Fetch an unpacked, left justified, and properly biased/dithered mantissa value */
180 static unsigned short mpeg3audio_ac3_mantissa_get(mpeg3audio_t *audio,
181 unsigned short bap,
182 unsigned short dithflag)
184 unsigned short mantissa;
185 unsigned int group_code;
186 mpeg3_ac3_mantissa_t *mantissa_struct = &(audio->ac3_mantissa);
188 /* If the bap is 0-5 then we have special cases to take care of */
189 switch(bap)
191 case 0:
192 if(dithflag)
193 mantissa = mpeg3audio_ac3_dither_gen(audio);
194 else
195 mantissa = 0;
196 break;
198 case 1:
199 if(mantissa_struct->m_1_pointer > 2)
201 group_code = mpeg3bits_getbits(audio->astream, 5);
203 if(group_code > 26)
205 /* FIXME do proper block error handling */
206 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 1 %d\n", group_code);
207 return 0;
210 mantissa_struct->m_1[0] = group_code / 9;
211 mantissa_struct->m_1[1] = (group_code % 9) / 3;
212 mantissa_struct->m_1[2] = (group_code % 9) % 3;
213 mantissa_struct->m_1_pointer = 0;
215 mantissa = mantissa_struct->m_1[mantissa_struct->m_1_pointer++];
216 mantissa = mpeg3_q_1[mantissa];
217 break;
219 case 2:
220 if(mantissa_struct->m_2_pointer > 2)
222 group_code = mpeg3bits_getbits(audio->astream, 7);
224 if(group_code > 124)
226 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 2 %d\n", group_code);
227 return 0;
230 mantissa_struct->m_2[0] = group_code / 25;
231 mantissa_struct->m_2[1] = (group_code % 25) / 5;
232 mantissa_struct->m_2[2] = (group_code % 25) % 5;
233 mantissa_struct->m_2_pointer = 0;
235 mantissa = mantissa_struct->m_2[mantissa_struct->m_2_pointer++];
236 mantissa = mpeg3_q_2[mantissa];
237 break;
239 case 3:
240 mantissa = mpeg3bits_getbits(audio->astream, 3);
242 if(mantissa > 6)
244 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 3 %d\n", group_code);
245 return 0;
248 mantissa = mpeg3_q_3[mantissa];
249 break;
251 case 4:
252 if(mantissa_struct->m_4_pointer > 1)
254 group_code = mpeg3bits_getbits(audio->astream, 7);
256 if(group_code > 120)
258 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 4 %d\n", group_code);
259 return 0;
262 mantissa_struct->m_4[0] = group_code / 11;
263 mantissa_struct->m_4[1] = group_code % 11;
264 mantissa_struct->m_4_pointer = 0;
266 mantissa = mantissa_struct->m_4[mantissa_struct->m_4_pointer++];
267 mantissa = mpeg3_q_4[mantissa];
268 break;
270 case 5:
271 mantissa = mpeg3bits_getbits(audio->astream, 4);
273 if(mantissa > 14)
275 /* FIXME do proper block error handling */
276 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 5 %d\n", group_code);
277 return 0;
280 mantissa = mpeg3_q_5[mantissa];
281 break;
283 default:
284 mantissa = mpeg3bits_getbits(audio->astream, mpeg3_qnttztab[bap]);
285 mantissa <<= 16 - mpeg3_qnttztab[bap];
287 return mantissa;
290 void mpeg3audio_ac3_uncouple_channel(mpeg3audio_t *audio,
291 float samples[],
292 mpeg3_ac3bsi_t *bsi,
293 mpeg3_ac3audblk_t *audblk,
294 unsigned int ch)
296 unsigned int bnd = 0;
297 unsigned int sub_bnd = 0;
298 unsigned int i, j;
299 MPEG3_FLOAT32 cpl_coord = 1.0;
300 unsigned int cpl_exp_tmp;
301 unsigned int cpl_mant_tmp;
302 short mantissa;
304 for(i = audblk->cplstrtmant; i < audblk->cplendmant; )
306 if(!audblk->cplbndstrc[sub_bnd++])
308 cpl_exp_tmp = audblk->cplcoexp[ch][bnd] + 3 * audblk->mstrcplco[ch];
309 if(audblk->cplcoexp[ch][bnd] == 15)
310 cpl_mant_tmp = (audblk->cplcomant[ch][bnd]) << 11;
311 else
312 cpl_mant_tmp = ((0x10) | audblk->cplcomant[ch][bnd]) << 10;
314 cpl_coord = mpeg3audio_ac3_tofloat(cpl_exp_tmp, cpl_mant_tmp) * 8.0f;
316 /*Invert the phase for the right channel if necessary */
317 if(bsi->acmod == 0x2 && audblk->phsflginu && ch == 1 && audblk->phsflg[bnd])
318 cpl_coord *= -1;
320 bnd++;
323 for(j = 0; j < 12; j++)
325 /* Get new dither values for each channel if necessary, so */
326 /* the channels are uncorrelated */
327 if(audblk->dithflag[ch] && audblk->cpl_bap[i] == 0)
328 mantissa = mpeg3audio_ac3_dither_gen(audio);
329 else
330 mantissa = audblk->cplmant[i];
332 samples[i] = cpl_coord * mpeg3audio_ac3_tofloat(audblk->cpl_exp[i], mantissa);;
334 i++;
337 return;
340 int mpeg3audio_ac3_coeff_unpack(mpeg3audio_t *audio,
341 mpeg3_ac3bsi_t *bsi,
342 mpeg3_ac3audblk_t *audblk,
343 mpeg3ac3_stream_samples_t samples)
345 int i, j;
346 int done_cpl = 0;
347 short mantissa;
349 mpeg3audio_ac3_mantissa_reset(&(audio->ac3_mantissa));
351 for(i = 0; i < bsi->nfchans && !mpeg3bits_error(audio->astream); i++)
353 for(j = 0; j < audblk->endmant[i] && !mpeg3bits_error(audio->astream); j++)
355 mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->fbw_bap[i][j], audblk->dithflag[i]);
356 samples[i][j] = mpeg3audio_ac3_tofloat(audblk->fbw_exp[i][j], mantissa);
359 if(audblk->cplinu && audblk->chincpl[i] && !(done_cpl) && !mpeg3bits_error(audio->astream))
361 /* ncplmant is equal to 12 * ncplsubnd */
362 /* Don't dither coupling channel until channel separation so that
363 * interchannel noise is uncorrelated */
364 for(j = audblk->cplstrtmant;
365 j < audblk->cplendmant && !mpeg3bits_error(audio->astream);
366 j++)
368 audblk->cplmant[j] = mpeg3audio_ac3_mantissa_get(audio, audblk->cpl_bap[j], 0);
370 done_cpl = 1;
374 /* Uncouple the channel */
375 if(audblk->cplinu)
377 if(audblk->chincpl[i])
378 mpeg3audio_ac3_uncouple_channel(audio, samples[i], bsi, audblk, i);
381 if(bsi->lfeon && !mpeg3bits_error(audio->astream))
383 /* There are always 7 mantissas for lfe, no dither for lfe */
384 for(j = 0; j < 7 && !mpeg3bits_error(audio->astream); j++)
386 mantissa = mpeg3audio_ac3_mantissa_get(audio, audblk->lfe_bap[j], 0);
387 samples[5][j] = mpeg3audio_ac3_tofloat(audblk->lfe_exp[j], mantissa);
388 //printf("%f ", samples[5][j]);
390 //printf("\n");
393 return mpeg3bits_error(audio->astream);