]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_downmix.c
rajoout d'includes pour que �a compile sous FreeBSD (thx Dae)
[vlc] / src / ac3_decoder / ac3_downmix.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_downmix.h"
25
26 #define NORM 16384
27
28 typedef struct prefs_s
29 {
30         u16 use_dolby_surround;
31         u16 dual_mono_channel_select;
32
33 } prefs_t;
34
35 prefs_t global_prefs = {0,0};
36
37 //Pre-scaled downmix coefficients
38 static float cmixlev_lut[4] = { 0.2928, 0.2468, 0.2071, 0.2468 };
39 static float smixlev_lut[4] = { 0.2928, 0.2071, 0.0   , 0.2071 };
40
41 /* Downmix into _two_ channels...other downmix modes aren't implemented
42  * to reduce complexity. Realistically, there aren't many machines around
43  * with > 2 channel output anyways */
44
45 void downmix( ac3dec_thread_t * p_ac3dec, s16 * out_buf )
46 {
47         int j;
48         float right_tmp;
49         float left_tmp;
50         float clev,slev;
51         float *centre = 0, *left = 0, *right = 0, *left_sur = 0, *right_sur = 0;
52
53         /*
54         if(p_ac3dec->bsi.acmod > 7)
55                 dprintf("(downmix) invalid acmod number\n"); 
56         */
57
58         //There are two main cases, with or without Dolby Surround
59         if(global_prefs.use_dolby_surround)
60         {
61                 switch(p_ac3dec->bsi.acmod)
62                 {
63                         // 3/2
64                         case 7:
65                                 left      = p_ac3dec->samples.channel[0];
66                                 centre    = p_ac3dec->samples.channel[1];
67                                 right     = p_ac3dec->samples.channel[2];
68                                 left_sur  = p_ac3dec->samples.channel[3];
69                                 right_sur = p_ac3dec->samples.channel[4];
70
71                                 for ( j = 0; j < 256; j++ )
72                                 {
73                                         right_tmp = 0.2265f * *left_sur++ + 0.2265f * *right_sur++;
74                                         left_tmp  = -1 * right_tmp;
75                                         right_tmp += 0.3204f * *right++ + 0.2265f * *centre;
76                                         left_tmp  += 0.3204f * *left++  + 0.2265f * *centre++;
77
78                                         *(out_buf++) = left_tmp * NORM;
79                                         *(out_buf++) = right_tmp * NORM;
80                                         /*
81                                         p_ac3dec->samples.channel[1][j] = right_tmp;
82                                         p_ac3dec->samples.channel[0][j] = left_tmp;
83                                         */
84                                 }
85                         break;
86
87                         // 2/2
88                         case 6:
89                                 left      = p_ac3dec->samples.channel[0];
90                                 right     = p_ac3dec->samples.channel[1];
91                                 left_sur  = p_ac3dec->samples.channel[2];
92                                 right_sur = p_ac3dec->samples.channel[3];
93
94                                 for (j = 0; j < 256; j++) 
95                                 {
96                                         right_tmp = 0.2265f * *left_sur++ + 0.2265f * *right_sur++;
97                                         left_tmp  = -1 * right_tmp;
98                                         right_tmp += 0.3204f * *right++;
99                                         left_tmp  += 0.3204f * *left++ ;
100
101                                         *(out_buf++) = left_tmp * NORM;
102                                         *(out_buf++) = right_tmp * NORM;
103                                         /*
104                                         p_ac3dec->samples.channel[1][j] = right_tmp;
105                                         p_ac3dec->samples.channel[0][j] = left_tmp;
106                                         */
107                                 }
108                         break;
109
110                         // 3/1
111                         case 5:
112                                 left      = p_ac3dec->samples.channel[0];
113                                 centre    = p_ac3dec->samples.channel[1];
114                                 right     = p_ac3dec->samples.channel[2];
115                                 //Mono surround
116                                 right_sur = p_ac3dec->samples.channel[3];
117
118                                 for (j = 0; j < 256; j++) 
119                                 {
120                                         right_tmp =  0.2265f * *right_sur++;
121                                         left_tmp  = - right_tmp;
122                                         right_tmp += 0.3204f * *right++ + 0.2265f * *centre;
123                                         left_tmp  += 0.3204f * *left++  + 0.2265f * *centre++;
124
125                                         *(out_buf++) = left_tmp * NORM;
126                                         *(out_buf++) = right_tmp * NORM;
127                                         /*
128                                         p_ac3dec->samples.channel[1][j] = right_tmp;
129                                         p_ac3dec->samples.channel[0][j] = left_tmp;
130                                         */
131                                 }
132                         break;
133
134                         // 2/1
135                         case 4:
136                                 left      = p_ac3dec->samples.channel[0];
137                                 right     = p_ac3dec->samples.channel[1];
138                                 //Mono surround
139                                 right_sur = p_ac3dec->samples.channel[2];
140
141                                 for (j = 0; j < 256; j++) 
142                                 {
143                                         right_tmp =  0.2265f * *right_sur++;
144                                         left_tmp  = - right_tmp;
145                                         right_tmp += 0.3204f * *right++; 
146                                         left_tmp  += 0.3204f * *left++;
147
148                                         *(out_buf++) = left_tmp * NORM;
149                                         *(out_buf++) = right_tmp * NORM;
150                                         /*
151                                         p_ac3dec->samples.channel[1][j] = right_tmp;
152                                         p_ac3dec->samples.channel[0][j] = left_tmp;
153                                         */
154                                 }
155                         break;
156
157                         // 3/0
158                         case 3:
159                                 left      = p_ac3dec->samples.channel[0];
160                                 centre    = p_ac3dec->samples.channel[1];
161                                 right     = p_ac3dec->samples.channel[2];
162
163                                 for (j = 0; j < 256; j++) 
164                                 {
165                                         right_tmp = 0.3204f * *right++ + 0.2265f * *centre;
166                                         left_tmp  = 0.3204f * *left++  + 0.2265f * *centre++;
167
168                                         *(out_buf++) = left_tmp * NORM;
169                                         *(out_buf++) = right_tmp * NORM;
170                                         /*
171                                         p_ac3dec->samples.channel[1][j] = right_tmp;
172                                         p_ac3dec->samples.channel[0][j] = left_tmp;
173                                         */
174                                 }
175                         break;
176
177                         // 2/0
178                         case 2:
179                                 left = p_ac3dec->samples.channel[0];
180                                 right = p_ac3dec->samples.channel[1];
181
182                                 for ( j = 0; j < 256; j++ )
183                                 {
184                                         *(out_buf++) = *(left++) * NORM;
185                                         *(out_buf++) = *(right++) * NORM;
186                                 }
187                         break;
188
189                         // 1/0
190                         case 1:
191                                 //Mono program!
192                                 right = p_ac3dec->samples.channel[0];
193
194                                 for (j = 0; j < 256; j++) 
195                                 {
196                                         right_tmp = 0.7071f * *right++;
197
198                                         *(out_buf++) = right_tmp * NORM;
199                                         *(out_buf++) = right_tmp * NORM;
200                                         /*
201                                         p_ac3dec->samples.channel[1][j] = right_tmp;
202                                         p_ac3dec->samples.channel[0][j] = right_tmp;
203                                         */
204                                 }
205                         break;
206
207                         // 1+1
208                         case 0:
209                                 //Dual mono, output selected by user
210                                 right = p_ac3dec->samples.channel[global_prefs.dual_mono_channel_select];
211
212                                 for (j = 0; j < 256; j++) 
213                                 {
214                                         right_tmp = 0.7071f * *right++;
215
216                                         *(out_buf++) = right_tmp * NORM;
217                                         *(out_buf++) = right_tmp * NORM;
218                                         /*
219                                         p_ac3dec->samples.channel[1][j] = right_tmp;
220                                         p_ac3dec->samples.channel[0][j] = right_tmp;
221                                         */
222                                 }
223                         break;
224                 }
225         }
226         else
227         {
228                 //Non-Dolby surround downmixes
229                 switch(p_ac3dec->bsi.acmod)
230                 {
231                         // 3/2
232                         case 7:
233                                 left      = p_ac3dec->samples.channel[0];
234                                 centre    = p_ac3dec->samples.channel[1];
235                                 right     = p_ac3dec->samples.channel[2];
236                                 left_sur  = p_ac3dec->samples.channel[3];
237                                 right_sur = p_ac3dec->samples.channel[4];
238
239                                 clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
240                                 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
241
242                                 for (j = 0; j < 256; j++) 
243                                 {
244                                         right_tmp= 0.4142f * *right++ + clev * *centre   + slev * *right_sur++;
245                                         left_tmp = 0.4142f * *left++  + clev * *centre++ + slev * *left_sur++;
246
247                                         *(out_buf++) = left_tmp * NORM;
248                                         *(out_buf++) = right_tmp * NORM;
249                                         /*
250                                         p_ac3dec->samples.channel[1][j] = right_tmp;
251                                         p_ac3dec->samples.channel[0][j] = left_tmp;
252                                         */
253                                 }
254                         break;
255
256                         // 2/2
257                         case 6:
258                                 left      = p_ac3dec->samples.channel[0];
259                                 right     = p_ac3dec->samples.channel[1];
260                                 left_sur  = p_ac3dec->samples.channel[2];
261                                 right_sur = p_ac3dec->samples.channel[3];
262
263                                 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
264
265                                 for (j = 0; j < 256; j++) 
266                                 {
267                                         right_tmp= 0.4142f * *right++ + slev * *right_sur++;
268                                         left_tmp = 0.4142f * *left++  + slev * *left_sur++;
269
270                                         *(out_buf++) = left_tmp * NORM;
271                                         *(out_buf++) = right_tmp * NORM;
272                                         /*
273                                         p_ac3dec->samples.channel[1][j] = right_tmp;
274                                         p_ac3dec->samples.channel[0][j] = left_tmp;
275                                         */
276                                 }
277                         break;
278
279                         // 3/1
280                         case 5:
281                                 left      = p_ac3dec->samples.channel[0];
282                                 centre    = p_ac3dec->samples.channel[1];
283                                 right     = p_ac3dec->samples.channel[2];
284                                 //Mono surround
285                                 right_sur = p_ac3dec->samples.channel[3];
286
287                                 clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
288                                 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
289
290                                 for (j = 0; j < 256; j++) 
291                                 {
292                                         right_tmp= 0.4142f * *right++ + clev * *centre   + slev * *right_sur;
293                                         left_tmp = 0.4142f * *left++  + clev * *centre++ + slev * *right_sur++;
294
295                                         *(out_buf++) = left_tmp * NORM;
296                                         *(out_buf++) = right_tmp * NORM;
297                                         /*
298                                         p_ac3dec->samples.channel[1][j] = right_tmp;
299                                         p_ac3dec->samples.channel[0][j] = left_tmp;
300                                         */
301                                 }
302                         break;
303
304                         // 2/1
305                         case 4:
306                                 left      = p_ac3dec->samples.channel[0];
307                                 right     = p_ac3dec->samples.channel[1];
308                                 //Mono surround
309                                 right_sur = p_ac3dec->samples.channel[2];
310
311                                 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
312
313                                 for (j = 0; j < 256; j++) 
314                                 {
315                                         right_tmp= 0.4142f * *right++ + slev * *right_sur;
316                                         left_tmp = 0.4142f * *left++  + slev * *right_sur++;
317
318                                         *(out_buf++) = left_tmp * NORM;
319                                         *(out_buf++) = right_tmp * NORM;
320                                         /*
321                                         p_ac3dec->samples.channel[1][j] = right_tmp;
322                                         p_ac3dec->samples.channel[0][j] = left_tmp;
323                                         */
324                                 }
325                         break;
326
327                         // 3/0
328                         case 3:
329                                 left      = p_ac3dec->samples.channel[0];
330                                 centre    = p_ac3dec->samples.channel[1];
331                                 right     = p_ac3dec->samples.channel[2];
332
333                                 clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
334
335                                 for (j = 0; j < 256; j++) 
336                                 {
337                                         right_tmp= 0.4142f * *right++ + clev * *centre;   
338                                         left_tmp = 0.4142f * *left++  + clev * *centre++; 
339
340                                         *(out_buf++) = left_tmp * NORM;
341                                         *(out_buf++) = right_tmp * NORM;
342                                         /*
343                                         p_ac3dec->samples.channel[1][j] = right_tmp;
344                                         p_ac3dec->samples.channel[0][j] = left_tmp;
345                                         */
346                                 }
347                         break;
348
349                         case 2:
350                                 left = p_ac3dec->samples.channel[0];
351                                 right = p_ac3dec->samples.channel[1];
352
353                                 for ( j = 0; j < 256; j++ )
354                                 {
355                                         *(out_buf++) = *(left++) * NORM;
356                                         *(out_buf++) = *(right++) * NORM;
357                                 }
358                         break;
359
360                         // 1/0
361                         case 1:
362                                 //Mono program!
363                                 right = p_ac3dec->samples.channel[0];
364
365                                 for (j = 0; j < 256; j++) 
366                                 {
367                                         right_tmp = 0.7071f * *right++;
368
369                                         *(out_buf++) = right_tmp * NORM;
370                                         *(out_buf++) = right_tmp * NORM;
371                                         /*
372                                         p_ac3dec->samples.channel[1][j] = right_tmp;
373                                         p_ac3dec->samples.channel[0][j] = right_tmp;
374                                         */
375                                 }
376                         break;
377
378                         // 1+1
379                         case 0:
380                                 //Dual mono, output selected by user
381                                 right = p_ac3dec->samples.channel[global_prefs.dual_mono_channel_select];
382
383                                 for (j = 0; j < 256; j++) 
384                                 {
385                                         right_tmp = 0.7071f * *right++;
386
387                                         *(out_buf++) = right_tmp * NORM;
388                                         *(out_buf++) = right_tmp * NORM;
389                                         /*
390                                         p_ac3dec->samples.channel[1][j] = right_tmp;
391                                         p_ac3dec->samples.channel[0][j] = right_tmp;
392                                         */
393                                 }
394                         break;
395                 }
396         }
397 }