1 #include <unistd.h> /* getpid() */
3 #include <stdio.h> /* "intf_msg.h" */
4 #include <stdlib.h> /* malloc(), free() */
5 #include <sys/soundcard.h> /* "audio_output.h" */
7 #include <sys/uio.h> /* "input.h" */
12 #include "vlc_thread.h"
13 #include "debug.h" /* "input_netlist.h" */
15 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
17 #include "input.h" /* pes_packet_t */
18 #include "input_netlist.h" /* input_NetlistFreePES() */
19 #include "decoder_fifo.h" /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
21 #include "audio_output.h"
23 #include "ac3_decoder.h"
24 #include "ac3_downmix.h"
28 typedef struct prefs_s
30 u16 use_dolby_surround;
31 u16 dual_mono_channel_select;
35 prefs_t global_prefs = {0,0};
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 };
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 */
45 void downmix( ac3dec_thread_t * p_ac3dec, s16 * out_buf )
51 float *centre = 0, *left = 0, *right = 0, *left_sur = 0, *right_sur = 0;
54 if(p_ac3dec->bsi.acmod > 7)
55 dprintf("(downmix) invalid acmod number\n");
58 //There are two main cases, with or without Dolby Surround
59 if(global_prefs.use_dolby_surround)
61 switch(p_ac3dec->bsi.acmod)
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];
71 for ( j = 0; j < 256; j++ )
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++;
78 *(out_buf++) = left_tmp * NORM;
79 *(out_buf++) = right_tmp * NORM;
81 p_ac3dec->samples.channel[1][j] = right_tmp;
82 p_ac3dec->samples.channel[0][j] = left_tmp;
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];
94 for (j = 0; j < 256; j++)
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++ ;
101 *(out_buf++) = left_tmp * NORM;
102 *(out_buf++) = right_tmp * NORM;
104 p_ac3dec->samples.channel[1][j] = right_tmp;
105 p_ac3dec->samples.channel[0][j] = left_tmp;
112 left = p_ac3dec->samples.channel[0];
113 centre = p_ac3dec->samples.channel[1];
114 right = p_ac3dec->samples.channel[2];
116 right_sur = p_ac3dec->samples.channel[3];
118 for (j = 0; j < 256; j++)
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++;
125 *(out_buf++) = left_tmp * NORM;
126 *(out_buf++) = right_tmp * NORM;
128 p_ac3dec->samples.channel[1][j] = right_tmp;
129 p_ac3dec->samples.channel[0][j] = left_tmp;
136 left = p_ac3dec->samples.channel[0];
137 right = p_ac3dec->samples.channel[1];
139 right_sur = p_ac3dec->samples.channel[2];
141 for (j = 0; j < 256; j++)
143 right_tmp = 0.2265f * *right_sur++;
144 left_tmp = - right_tmp;
145 right_tmp += 0.3204f * *right++;
146 left_tmp += 0.3204f * *left++;
148 *(out_buf++) = left_tmp * NORM;
149 *(out_buf++) = right_tmp * NORM;
151 p_ac3dec->samples.channel[1][j] = right_tmp;
152 p_ac3dec->samples.channel[0][j] = left_tmp;
159 left = p_ac3dec->samples.channel[0];
160 centre = p_ac3dec->samples.channel[1];
161 right = p_ac3dec->samples.channel[2];
163 for (j = 0; j < 256; j++)
165 right_tmp = 0.3204f * *right++ + 0.2265f * *centre;
166 left_tmp = 0.3204f * *left++ + 0.2265f * *centre++;
168 *(out_buf++) = left_tmp * NORM;
169 *(out_buf++) = right_tmp * NORM;
171 p_ac3dec->samples.channel[1][j] = right_tmp;
172 p_ac3dec->samples.channel[0][j] = left_tmp;
179 left = p_ac3dec->samples.channel[0];
180 right = p_ac3dec->samples.channel[1];
182 for ( j = 0; j < 256; j++ )
184 *(out_buf++) = *(left++) * NORM;
185 *(out_buf++) = *(right++) * NORM;
192 right = p_ac3dec->samples.channel[0];
194 for (j = 0; j < 256; j++)
196 right_tmp = 0.7071f * *right++;
198 *(out_buf++) = right_tmp * NORM;
199 *(out_buf++) = right_tmp * NORM;
201 p_ac3dec->samples.channel[1][j] = right_tmp;
202 p_ac3dec->samples.channel[0][j] = right_tmp;
209 //Dual mono, output selected by user
210 right = p_ac3dec->samples.channel[global_prefs.dual_mono_channel_select];
212 for (j = 0; j < 256; j++)
214 right_tmp = 0.7071f * *right++;
216 *(out_buf++) = right_tmp * NORM;
217 *(out_buf++) = right_tmp * NORM;
219 p_ac3dec->samples.channel[1][j] = right_tmp;
220 p_ac3dec->samples.channel[0][j] = right_tmp;
228 //Non-Dolby surround downmixes
229 switch(p_ac3dec->bsi.acmod)
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];
239 clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
240 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
242 for (j = 0; j < 256; j++)
244 right_tmp= 0.4142f * *right++ + clev * *centre + slev * *right_sur++;
245 left_tmp = 0.4142f * *left++ + clev * *centre++ + slev * *left_sur++;
247 *(out_buf++) = left_tmp * NORM;
248 *(out_buf++) = right_tmp * NORM;
250 p_ac3dec->samples.channel[1][j] = right_tmp;
251 p_ac3dec->samples.channel[0][j] = left_tmp;
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];
263 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
265 for (j = 0; j < 256; j++)
267 right_tmp= 0.4142f * *right++ + slev * *right_sur++;
268 left_tmp = 0.4142f * *left++ + slev * *left_sur++;
270 *(out_buf++) = left_tmp * NORM;
271 *(out_buf++) = right_tmp * NORM;
273 p_ac3dec->samples.channel[1][j] = right_tmp;
274 p_ac3dec->samples.channel[0][j] = left_tmp;
281 left = p_ac3dec->samples.channel[0];
282 centre = p_ac3dec->samples.channel[1];
283 right = p_ac3dec->samples.channel[2];
285 right_sur = p_ac3dec->samples.channel[3];
287 clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
288 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
290 for (j = 0; j < 256; j++)
292 right_tmp= 0.4142f * *right++ + clev * *centre + slev * *right_sur;
293 left_tmp = 0.4142f * *left++ + clev * *centre++ + slev * *right_sur++;
295 *(out_buf++) = left_tmp * NORM;
296 *(out_buf++) = right_tmp * NORM;
298 p_ac3dec->samples.channel[1][j] = right_tmp;
299 p_ac3dec->samples.channel[0][j] = left_tmp;
306 left = p_ac3dec->samples.channel[0];
307 right = p_ac3dec->samples.channel[1];
309 right_sur = p_ac3dec->samples.channel[2];
311 slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
313 for (j = 0; j < 256; j++)
315 right_tmp= 0.4142f * *right++ + slev * *right_sur;
316 left_tmp = 0.4142f * *left++ + slev * *right_sur++;
318 *(out_buf++) = left_tmp * NORM;
319 *(out_buf++) = right_tmp * NORM;
321 p_ac3dec->samples.channel[1][j] = right_tmp;
322 p_ac3dec->samples.channel[0][j] = left_tmp;
329 left = p_ac3dec->samples.channel[0];
330 centre = p_ac3dec->samples.channel[1];
331 right = p_ac3dec->samples.channel[2];
333 clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
335 for (j = 0; j < 256; j++)
337 right_tmp= 0.4142f * *right++ + clev * *centre;
338 left_tmp = 0.4142f * *left++ + clev * *centre++;
340 *(out_buf++) = left_tmp * NORM;
341 *(out_buf++) = right_tmp * NORM;
343 p_ac3dec->samples.channel[1][j] = right_tmp;
344 p_ac3dec->samples.channel[0][j] = left_tmp;
350 left = p_ac3dec->samples.channel[0];
351 right = p_ac3dec->samples.channel[1];
353 for ( j = 0; j < 256; j++ )
355 *(out_buf++) = *(left++) * NORM;
356 *(out_buf++) = *(right++) * NORM;
363 right = p_ac3dec->samples.channel[0];
365 for (j = 0; j < 256; j++)
367 right_tmp = 0.7071f * *right++;
369 *(out_buf++) = right_tmp * NORM;
370 *(out_buf++) = right_tmp * NORM;
372 p_ac3dec->samples.channel[1][j] = right_tmp;
373 p_ac3dec->samples.channel[0][j] = right_tmp;
380 //Dual mono, output selected by user
381 right = p_ac3dec->samples.channel[global_prefs.dual_mono_channel_select];
383 for (j = 0; j < 256; j++)
385 right_tmp = 0.7071f * *right++;
387 *(out_buf++) = right_tmp * NORM;
388 *(out_buf++) = right_tmp * NORM;
390 p_ac3dec->samples.channel[1][j] = right_tmp;
391 p_ac3dec->samples.channel[0][j] = right_tmp;