3 * mantissa.c Copyright (C) Aaron Holtzman - May 1999
6 * This file is part of libmpeg3
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)
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] =
35 static short mpeg3_q_2
[5] =
38 ((-2 << 15) / 5) << 1,
44 static short mpeg3_q_3
[7] =
55 static short mpeg3_q_4
[11] =
70 static short mpeg3_q_5
[15] =
89 static short mpeg3_qnttztab
[16] = {0, 0, 0, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16};
93 /* Scale factors for tofloat */
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
)
133 x
= mantissa
* mpeg3_scale_factor
[CLIP(exponent
, 0, 25)];
134 //printf(__FUNCTION__ " %f\n", 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
)
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
++)
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
,
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 */
193 mantissa
= mpeg3audio_ac3_dither_gen(audio
);
199 if(mantissa_struct
->m_1_pointer
> 2)
201 group_code
= mpeg3bits_getbits(audio
->astream
, 5);
205 /* FIXME do proper block error handling */
206 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 1 %d\n", group_code);
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
];
220 if(mantissa_struct
->m_2_pointer
> 2)
222 group_code
= mpeg3bits_getbits(audio
->astream
, 7);
226 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 2 %d\n", group_code);
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
];
240 mantissa
= mpeg3bits_getbits(audio
->astream
, 3);
244 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 3 %d\n", group_code);
248 mantissa
= mpeg3_q_3
[mantissa
];
252 if(mantissa_struct
->m_4_pointer
> 1)
254 group_code
= mpeg3bits_getbits(audio
->astream
, 7);
258 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 4 %d\n", group_code);
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
];
271 mantissa
= mpeg3bits_getbits(audio
->astream
, 4);
275 /* FIXME do proper block error handling */
276 // fprintf(stderr, "mpeg3audio_ac3_mantissa_get: Invalid mantissa 5 %d\n", group_code);
280 mantissa
= mpeg3_q_5
[mantissa
];
284 mantissa
= mpeg3bits_getbits(audio
->astream
, mpeg3_qnttztab
[bap
]);
285 mantissa
<<= 16 - mpeg3_qnttztab
[bap
];
290 void mpeg3audio_ac3_uncouple_channel(mpeg3audio_t
*audio
,
293 mpeg3_ac3audblk_t
*audblk
,
296 unsigned int bnd
= 0;
297 unsigned int sub_bnd
= 0;
299 MPEG3_FLOAT32 cpl_coord
= 1.0;
300 unsigned int cpl_exp_tmp
;
301 unsigned int cpl_mant_tmp
;
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;
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
])
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
);
330 mantissa
= audblk
->cplmant
[i
];
332 samples
[i
] = cpl_coord
* mpeg3audio_ac3_tofloat(audblk
->cpl_exp
[i
], mantissa
);;
340 int mpeg3audio_ac3_coeff_unpack(mpeg3audio_t
*audio
,
342 mpeg3_ac3audblk_t
*audblk
,
343 mpeg3ac3_stream_samples_t samples
)
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
);
368 audblk
->cplmant
[j
] = mpeg3audio_ac3_mantissa_get(audio
, audblk
->cpl_bap
[j
], 0);
374 /* Uncouple the channel */
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]);
393 return mpeg3bits_error(audio
->astream
);