]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_exponent.c
* Makefile :
[vlc] / src / ac3_decoder / ac3_exponent.c
1 #include <unistd.h>                                              /* getpid() */
2
3 #include <stdio.h>                                           /* "intf_msg.h" */
4 #include <stdlib.h>                                      /* malloc(), free() */
5 #include <sys/soundcard.h>                               /* "audio_output.h" */
6 #include <sys/types.h>
7 #include <sys/uio.h>                                            /* "input.h" */
8
9 #include "common.h"
10 #include "config.h"
11 #include "mtime.h"
12 #include "vlc_thread.h"
13 #include "debug.h"                                      /* "input_netlist.h" */
14
15 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
16
17 #include "input.h"                                           /* pes_packet_t */
18 #include "input_netlist.h"                         /* input_NetlistFreePES() */
19 #include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
20
21 #include "audio_output.h"
22
23 #include "ac3_decoder.h"
24 #include "ac3_exponent.h"
25
26 static const s16 exps_1[128] = { -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, -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, 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, 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, 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, 3, 3, 3 };
27
28 static const s16 exps_2[128] = { -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, -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, -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, -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, -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, -2, -2, -2 };
29
30 static const s16 exps_3[128] = { -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, -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, -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, -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, -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, -2, -1, 0 };
31
32 /*
33 static const s16 exps_1[128] = { 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, 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, 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, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5 };
34 static const s16 exps_2[128] = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0 };
35 static const s16 exps_3[128] = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2 };
36 */
37
38 static __inline__ void exp_unpack_ch( u16 type, u16 expstr, u16 ngrps, u16 initial_exp, u16 exps[], u16 * dest )
39 {
40         u16 i,j;
41         s16 exp_acc;
42         /*
43         s16 exp_1,exp_2,exp_3;
44         */
45
46         if ( expstr == EXP_REUSE )
47         {
48                 return;
49         }
50
51         /* Handle the initial absolute exponent */
52         exp_acc = initial_exp;
53         j = 0;
54
55         /* In the case of a fbw channel then the initial absolute values is 
56          * also an exponent */
57         if ( type != UNPACK_CPL )
58         {
59                 dest[j++] = exp_acc;
60         }
61
62         /* Loop through the groups and fill the dest array appropriately */
63         switch ( expstr )
64         {
65                 case EXP_D45:
66                         for ( i = 0; i < ngrps; i++ )
67                         {
68 #ifdef AC3_SIGSEGV
69                                 if ( exps[i] > 127 )
70                                 {
71                                         fprintf( stderr, "ac3dec debug: invalid exponent\n" );
72                                 }
73 #endif
74                                 exp_acc += (exps_1[exps[i]] /*- 2*/);
75                                 dest[j++] = exp_acc;
76                                 dest[j++] = exp_acc;
77                                 dest[j++] = exp_acc;
78                                 dest[j++] = exp_acc;
79                                 exp_acc += (exps_2[exps[i]] /*- 2*/);
80                                 dest[j++] = exp_acc;
81                                 dest[j++] = exp_acc;
82                                 dest[j++] = exp_acc;
83                                 dest[j++] = exp_acc;
84                                 exp_acc += (exps_3[exps[i]] /*- 2*/);
85                                 dest[j++] = exp_acc;
86                                 dest[j++] = exp_acc;
87                                 dest[j++] = exp_acc;
88                                 dest[j++] = exp_acc;
89                         }
90                         break;
91
92                 case EXP_D25:
93                         for ( i = 0; i < ngrps; i++ )
94                         {
95 #ifdef AC3_SIGSEGV
96                                 if ( exps[i] > 127 )
97                                 {
98                                         fprintf( stderr, "ac3dec debug: invalid exponent\n" );
99                                 }
100 #endif
101                                 exp_acc += (exps_1[exps[i]] /*- 2*/);
102                                 dest[j++] = exp_acc;
103                                 dest[j++] = exp_acc;
104                                 exp_acc += (exps_2[exps[i]] /*- 2*/);
105                                 dest[j++] = exp_acc;
106                                 dest[j++] = exp_acc;
107                                 exp_acc += (exps_3[exps[i]] /*- 2*/);
108                                 dest[j++] = exp_acc;
109                                 dest[j++] = exp_acc;
110                         }
111                         break;
112
113                 case EXP_D15:
114                         for ( i = 0; i < ngrps; i++ )
115                         {
116 #ifdef AC3_SIGSEGV
117                                 if ( exps[i] > 127 )
118                                 {
119                                         fprintf( stderr, "ac3dec debug: invalid exponent\n" );
120                                 }
121 #endif
122                                 exp_acc += (exps_1[exps[i]] /*- 2*/);
123                                 dest[j++] = exp_acc;
124                                 exp_acc += (exps_2[exps[i]] /*- 2*/);
125                                 dest[j++] = exp_acc;
126                                 exp_acc += (exps_3[exps[i]] /*- 2*/);
127                                 dest[j++] = exp_acc;
128                         }
129                         break;
130
131                 default:
132                         fprintf( stderr, "ac3dec debug: expstr == %i <maxx@via.ecp.fr>\n", expstr );
133                         break;
134         }
135 #if 0
136         for ( i = 0; i < ngrps; i++ )
137         {
138                 /*
139                 if ( exps[i] > 124 )
140                 {
141                         //FIXME set an error flag and mute the frame
142                         fprintf( stderr, "!! Invalid exponent !!\n" );
143 //                      exit( 1 );
144                 }
145                 */
146
147                 /*
148                 exp_1 = exps[i] / 25;
149                 exp_2 = (exps[i] - (exp_1 * 25)) / 5;
150                 exp_3 = exps[i] - (exp_1 * 25) - (exp_2 * 5) ;
151                 */
152
153                 switch ( expstr )
154                 {
155                         case EXP_D45:
156                                 exp_acc += (exps_1[exps[i]] - 2);
157                                 dest[j++] = exp_acc;
158                                 dest[j++] = exp_acc;
159                                 dest[j++] = exp_acc;
160                                 dest[j++] = exp_acc;
161                                 exp_acc += (exps_2[exps[i]] - 2);
162                                 dest[j++] = exp_acc;
163                                 dest[j++] = exp_acc;
164                                 dest[j++] = exp_acc;
165                                 dest[j++] = exp_acc;
166                                 exp_acc += (exps_3[exps[i]] - 2);
167                                 dest[j++] = exp_acc;
168                                 dest[j++] = exp_acc;
169                                 dest[j++] = exp_acc;
170                                 dest[j++] = exp_acc;
171                         break;
172
173                         case EXP_D25:
174                                 exp_acc += (exps_1[exps[i]] - 2);
175                                 dest[j++] = exp_acc;
176                                 dest[j++] = exp_acc;
177                                 exp_acc += (exps_2[exps[i]] - 2);
178                                 dest[j++] = exp_acc;
179                                 dest[j++] = exp_acc;
180                                 exp_acc += (exps_3[exps[i]] - 2);
181                                 dest[j++] = exp_acc;
182                                 dest[j++] = exp_acc;
183                         break;
184
185                         case EXP_D15:
186                                 exp_acc += (exps_1[exps[i]] - 2);
187                                 dest[j++] = exp_acc;
188                                 exp_acc += (exps_2[exps[i]] - 2);
189                                 dest[j++] = exp_acc;
190                                 exp_acc += (exps_3[exps[i]] - 2);
191                                 dest[j++] = exp_acc;
192                         break;
193                 }
194         }
195 #endif
196 }
197
198 void exponent_unpack( ac3dec_thread_t * p_ac3dec )
199 {
200         u16 i;
201
202         for ( i = 0; i < p_ac3dec->bsi.nfchans; i++ )
203         {
204                 exp_unpack_ch( UNPACK_FBW, p_ac3dec->audblk.chexpstr[i], p_ac3dec->audblk.nchgrps[i], p_ac3dec->audblk.exps[i][0], &p_ac3dec->audblk.exps[i][1], p_ac3dec->audblk.fbw_exp[i] );
205         }
206
207         if ( p_ac3dec->audblk.cplinu )
208         {
209                 exp_unpack_ch( UNPACK_CPL, p_ac3dec->audblk.cplexpstr, p_ac3dec->audblk.ncplgrps, p_ac3dec->audblk.cplabsexp << 1, p_ac3dec->audblk.cplexps, &p_ac3dec->audblk.cpl_exp[p_ac3dec->audblk.cplstrtmant] );
210         }
211
212         if ( p_ac3dec->bsi.lfeon )
213         {
214                 exp_unpack_ch( UNPACK_LFE, p_ac3dec->audblk.lfeexpstr, 2, p_ac3dec->audblk.lfeexps[0], &p_ac3dec->audblk.lfeexps[1], p_ac3dec->audblk.lfe_exp );
215         }
216 }