r726: Implementing ability to add textural info to the labels
[cinelerra_cv/mob.git] / libmpeg3 / audio / uncouple.c
blob90d2ca5bcec5871fabfd71efe01fd529ce4906f6
1 /*
3 * uncouple.c Copyright (C) Aaron Holtzman - May 1999
5 * This file is part of libmpeg3
6 *
7 * libmpeg3 is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * libmpeg3 is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Make; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "../bitstream.h"
24 #include "mpeg3audio.h"
26 static unsigned char mpeg3_first_bit_lut[256] =
28 0, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5,
29 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
30 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
31 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
32 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
33 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
34 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
35 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
36 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
46 /* Converts an unsigned exponent in the range of 0-24 and a 16 bit mantissa
47 * to an IEEE single precision floating point value */
48 static inline void mpeg3audio_ac3_convert_to_float(unsigned short exp,
49 unsigned short mantissa,
50 u_int32_t *dest)
52 int num;
53 short exponent;
54 int i;
56 /* If the mantissa is zero we can simply return zero */
57 if(mantissa == 0)
59 *dest = 0;
60 return;
63 /* Exponent is offset by 127 in IEEE format minus the shift to
64 * align the mantissa to 1.f (subtracted in the final result) */
65 exponent = 127 - exp;
67 /* Take care of the one asymmetric negative number */
68 if(mantissa == 0x8000)
69 mantissa++;
71 /* Extract the sign bit, invert the mantissa if it's negative, and
72 shift out the sign bit */
73 if(mantissa & 0x8000)
75 mantissa *= -1;
76 num = 0x80000000 + (exponent << 23);
78 else
80 mantissa *= 1;
81 num = exponent << 23;
84 /* Find the index of the most significant one bit */
85 i = mpeg3_first_bit_lut[mantissa >> 8];
87 if(i == 0)
88 i = mpeg3_first_bit_lut[mantissa & 0xff] + 8;
90 *dest = num - (i << 23) + (mantissa << (7 + i));
91 return;
95 int mpeg3audio_ac3_uncouple(mpeg3audio_t *audio,
96 mpeg3_ac3bsi_t *bsi,
97 mpeg3_ac3audblk_t *audblk,
98 mpeg3_stream_coeffs_t *coeffs)
100 int i, j;
102 for(i = 0; i < bsi->nfchans; i++)
104 for(j = 0; j < audblk->endmant[i]; j++)
105 mpeg3audio_ac3_convert_to_float(audblk->fbw_exp[i][j],
106 audblk->chmant[i][j],
107 (u_int32_t*)&coeffs->fbw[i][j]);
110 if(audblk->cplinu)
112 for(i = 0; i < bsi->nfchans; i++)
114 if(audblk->chincpl[i])
116 mpeg3audio_ac3_uncouple_channel(audio,
117 coeffs,
118 audblk,
125 if(bsi->lfeon)
127 /* There are always 7 mantissas for lfe */
128 for(j = 0; j < 7 ; j++)
129 mpeg3audio_ac3_convert_to_float(audblk->lfe_exp[j],
130 audblk->lfemant[j],
131 (u_int32_t*)&coeffs->lfe[j]);
134 return 0;