]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_exponent.c
f71d7ea97e48ac309df7692e9f30cf1dc1a60832
[vlc] / src / ac3_decoder / ac3_exponent.c
1 /*****************************************************************************
2  * ac3_exponent.c: ac3 exponent calculations
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Michel Kaempf <maxx@via.ecp.fr>
7  *          Michel Lespinasse <walken@zoy.org>
8  *          Aaron Holtzman <aholtzma@engr.uvic.ca>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24 #include "defs.h"
25
26 #include "config.h"
27 #include "common.h"
28 #include "threads.h"
29 #include "mtime.h"
30
31 #include "stream_control.h"
32 #include "input_ext-dec.h"
33
34 #include "audio_output.h"
35
36 #include "ac3_decoder.h"
37 #include "ac3_decoder_thread.h"
38
39 #include "intf_msg.h"
40
41 #include "ac3_bit_stream.h"
42 #include "ac3_internal.h"
43
44 static const s16 exps_1[128] =
45 {
46     -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
47     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
48      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50      2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51      0, 0, 0
52 };
53
54 static const s16 exps_2[128] =
55 {
56     -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
57     -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
58     -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
59     -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
60     -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
61      0, 0, 0
62 };
63
64 static const s16 exps_3[128] =
65 {
66     -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
67     -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
68     -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
69     -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
70     -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,
71      0, 0, 0
72 };
73
74 #define UNPACK_FBW 1 
75 #define UNPACK_CPL 2 
76 #define UNPACK_LFE 4
77
78 static __inline__ int exp_unpack_ch (ac3dec_t * p_ac3dec, u16 type,
79                                      u16 expstr, u16 ngrps, u16 initial_exp,
80                                      u16 exps[], u16 * dest)
81 {
82     u16 i,j;
83     s16 exp_acc;
84
85     if  (expstr == EXP_REUSE)
86     {
87         return 0;
88     }
89
90     /* Handle the initial absolute exponent */
91     exp_acc = initial_exp;
92     j = 0;
93
94     /* In the case of a fbw channel then the initial absolute values is
95      * also an exponent */
96     if (type != UNPACK_CPL)
97     {
98         dest[j++] = exp_acc;
99     }
100
101     /* Loop through the groups and fill the dest array appropriately */
102     switch (expstr)
103     {
104     case EXP_D15:        /* 1 */
105         for (i = 0; i < ngrps; i++)
106         {
107             if (exps[i] > 124)
108             {
109                 intf_ErrMsg ( "ac3dec error: invalid exponent" );
110                 return 1;
111             }
112             exp_acc += (exps_1[exps[i]] /*- 2*/);
113             dest[j++] = exp_acc;
114             exp_acc += (exps_2[exps[i]] /*- 2*/);
115             dest[j++] = exp_acc;
116             exp_acc += (exps_3[exps[i]] /*- 2*/);
117             dest[j++] = exp_acc;
118         }
119         break;
120
121     case EXP_D25:        /* 2 */
122         for (i = 0; i < ngrps; i++)
123         {
124             if (exps[i] > 124)
125             {
126                 intf_ErrMsg ( "ac3dec error: invalid exponent" );
127                 return 1;
128             }
129             exp_acc += (exps_1[exps[i]] /*- 2*/);
130             dest[j++] = exp_acc;
131             dest[j++] = exp_acc;
132             exp_acc += (exps_2[exps[i]] /*- 2*/);
133             dest[j++] = exp_acc;
134             dest[j++] = exp_acc;
135             exp_acc += (exps_3[exps[i]] /*- 2*/);
136             dest[j++] = exp_acc;
137             dest[j++] = exp_acc;
138         }
139         break;
140
141     case EXP_D45:        /* 3 */
142         for (i = 0; i < ngrps; i++)
143         {
144             if (exps[i] > 124)
145             {
146                 intf_ErrMsg ( "ac3dec error: invalid exponent" );
147                 return 1;
148             }
149             exp_acc += (exps_1[exps[i]] /*- 2*/);
150             dest[j++] = exp_acc;
151             dest[j++] = exp_acc;
152             dest[j++] = exp_acc;
153             dest[j++] = exp_acc;
154             exp_acc += (exps_2[exps[i]] /*- 2*/);
155             dest[j++] = exp_acc;
156             dest[j++] = exp_acc;
157             dest[j++] = exp_acc;
158             dest[j++] = exp_acc;
159             exp_acc += (exps_3[exps[i]] /*- 2*/);
160             dest[j++] = exp_acc;
161             dest[j++] = exp_acc;
162             dest[j++] = exp_acc;
163             dest[j++] = exp_acc;
164         }
165         break;
166     }
167
168     return 0;
169 }
170
171 int exponent_unpack (ac3dec_t * p_ac3dec)
172 {
173     u16 i;
174
175     for (i = 0; i < p_ac3dec->bsi.nfchans; i++)
176     {
177         if (exp_unpack_ch (p_ac3dec, UNPACK_FBW, p_ac3dec->audblk.chexpstr[i],
178                            p_ac3dec->audblk.nchgrps[i],
179                            p_ac3dec->audblk.exps[i][0],
180                            &p_ac3dec->audblk.exps[i][1],
181                            p_ac3dec->audblk.fbw_exp[i]))
182         {
183             return 1;
184         }
185     }
186
187     if (p_ac3dec->audblk.cplinu)
188     {
189         if (exp_unpack_ch (p_ac3dec, UNPACK_CPL, p_ac3dec->audblk.cplexpstr,
190                            p_ac3dec->audblk.ncplgrps,
191                            p_ac3dec->audblk.cplabsexp << 1,
192                            p_ac3dec->audblk.cplexps,
193                            &p_ac3dec->audblk.cpl_exp[p_ac3dec->audblk.cplstrtmant]))
194         {
195             return 1;
196         }
197     }
198
199     if (p_ac3dec->bsi.lfeon)
200     {
201         if (exp_unpack_ch (p_ac3dec, UNPACK_LFE, p_ac3dec->audblk.lfeexpstr,
202                            2, p_ac3dec->audblk.lfeexps[0],
203                            &p_ac3dec->audblk.lfeexps[1],
204                            p_ac3dec->audblk.lfe_exp))
205         {
206             return 1;
207         }
208     }
209
210     return 0;
211 }
212