]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
* modules/audio_output/directx.c: 5.1 audio output now works on sblive/audigy as...
[vlc] / modules / audio_output / waveout.c
1 /*****************************************************************************
2  * waveout.c : Windows waveOut plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: waveout.c,v 1.26 2003/07/11 23:14:03 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *      
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <string.h>                                            /* strerror() */
28 #include <stdlib.h>                            /* calloc(), malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/aout.h>
32 #include "aout_internal.h"
33
34 #include <windows.h>
35 #include <mmsystem.h>
36
37 #define FRAME_SIZE 1024              /* The size is in samples, not in bytes */
38 #define FRAMES_NUM 8
39
40 /*****************************************************************************
41  * Useful macros
42  *****************************************************************************/
43 #ifdef UNDER_CE
44 #   define DWORD_PTR DWORD
45 #endif
46
47 #ifndef WAVE_FORMAT_IEEE_FLOAT
48 #   define WAVE_FORMAT_IEEE_FLOAT 0x0003
49 #endif
50
51 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
52 #   define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
53 #endif
54
55 #ifndef WAVE_FORMAT_EXTENSIBLE
56 #define  WAVE_FORMAT_EXTENSIBLE   0xFFFE
57 #endif
58
59 #ifndef SPEAKER_FRONT_LEFT
60 #   define SPEAKER_FRONT_LEFT             0x1
61 #   define SPEAKER_FRONT_RIGHT            0x2
62 #   define SPEAKER_FRONT_CENTER           0x4
63 #   define SPEAKER_LOW_FREQUENCY          0x8
64 #   define SPEAKER_BACK_LEFT              0x10
65 #   define SPEAKER_BACK_RIGHT             0x20
66 #   define SPEAKER_FRONT_LEFT_OF_CENTER   0x40
67 #   define SPEAKER_FRONT_RIGHT_OF_CENTER  0x80
68 #   define SPEAKER_BACK_CENTER            0x100
69 #   define SPEAKER_SIDE_LEFT              0x200
70 #   define SPEAKER_SIDE_RIGHT             0x400
71 #   define SPEAKER_TOP_CENTER             0x800
72 #   define SPEAKER_TOP_FRONT_LEFT         0x1000
73 #   define SPEAKER_TOP_FRONT_CENTER       0x2000
74 #   define SPEAKER_TOP_FRONT_RIGHT        0x4000
75 #   define SPEAKER_TOP_BACK_LEFT          0x8000
76 #   define SPEAKER_TOP_BACK_CENTER        0x10000
77 #   define SPEAKER_TOP_BACK_RIGHT         0x20000
78 #   define SPEAKER_RESERVED               0x80000000
79 #endif
80
81 #ifndef _WAVEFORMATEXTENSIBLE_
82 typedef struct {
83     WAVEFORMATEX    Format;
84     union {
85         WORD wValidBitsPerSample;       /* bits of precision  */
86         WORD wSamplesPerBlock;          /* valid if wBitsPerSample==0 */
87         WORD wReserved;                 /* If neither applies, set to zero. */
88     } Samples;
89     DWORD           dwChannelMask;      /* which channels are */
90                                         /* present in stream  */
91     GUID            SubFormat;
92 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
93 #endif
94
95 #include <initguid.h>
96 DEFINE_GUID( __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
97 DEFINE_GUID( __KSDATAFORMAT_SUBTYPE_PCM, WAVE_FORMAT_PCM, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
98 DEFINE_GUID( __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF, WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 );
99
100 /*****************************************************************************
101  * Local prototypes
102  *****************************************************************************/
103 static int  Open         ( vlc_object_t * );
104 static void Close        ( vlc_object_t * );
105 static void Play         ( aout_instance_t * );
106
107 /*****************************************************************************
108  * notification_thread_t: waveOut event thread
109  *****************************************************************************/
110 typedef struct notification_thread_t
111 {
112     VLC_COMMON_MEMBERS
113     aout_instance_t *p_aout;
114
115 } notification_thread_t;
116
117 /* local functions */
118 static void Probe        ( aout_instance_t * );
119 static int OpenWaveOut   ( aout_instance_t *, int, int, int, int, vlc_bool_t );
120 static int OpenWaveOutPCM( aout_instance_t *, int*, int, int, int, vlc_bool_t );
121 static void CheckReordering( aout_instance_t *, int );
122 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
123                            aout_buffer_t * );
124
125 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
126 static void WaveOutThread( notification_thread_t * );
127
128 static void InterleaveFloat32( float *, int *, int );
129 static void InterleaveS16( int16_t *, int *, int );
130
131 /*****************************************************************************
132  * Module descriptor
133  *****************************************************************************/
134 vlc_module_begin();
135     set_description( _("Win32 waveOut extension output") );
136     set_capability( "audio output", 50 );
137     set_callbacks( Open, Close );
138 vlc_module_end();
139
140 /*****************************************************************************
141  * aout_sys_t: waveOut audio output method descriptor
142  *****************************************************************************
143  * This structure is part of the audio output thread descriptor.
144  * It describes the waveOut specific properties of an audio device.
145  *****************************************************************************/
146 struct aout_sys_t
147 {
148     HWAVEOUT h_waveout;                        /* handle to waveout instance */
149
150     WAVEFORMATEXTENSIBLE waveformat;                         /* audio format */
151
152     WAVEHDR waveheader[FRAMES_NUM];
153
154     notification_thread_t *p_notif;                      /* WaveOutThread id */
155     HANDLE event;
156
157     int i_buffer_size;
158
159     byte_t *p_silence_buffer;               /* buffer we use to play silence */
160
161     vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
162     int *pi_chan_table;
163 };
164
165 static const uint32_t pi_channels_in[] =
166     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
167       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
168       AOUT_CHAN_CENTER, AOUT_CHAN_LFE };
169 static const uint32_t pi_channels_out[] =
170     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
171       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
172       SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY };
173 static const uint32_t pi_channels_ordered[] =
174     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER,
175       SPEAKER_LOW_FREQUENCY,
176       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT };
177
178 /*****************************************************************************
179  * Open: open the audio device
180  *****************************************************************************
181  * This function opens and setups Win32 waveOut
182  *****************************************************************************/
183 static int Open( vlc_object_t *p_this )
184 {
185     aout_instance_t *p_aout = (aout_instance_t *)p_this;
186     vlc_value_t val;
187     int i;
188
189     /* Allocate structure */
190     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
191
192     if( p_aout->output.p_sys == NULL )
193     {
194         msg_Err( p_aout, "out of memory" );
195         return VLC_EGENERIC;
196     }
197
198     p_aout->output.pf_play = Play;
199     p_aout->output.p_sys->pi_chan_table = NULL;
200     p_aout->b_die = VLC_FALSE;
201
202     if( var_Type( p_aout, "audio-device" ) == 0 )
203     {
204         Probe( p_aout );
205     }
206
207     if( var_Get( p_aout, "audio-device", &val ) < 0 )
208     {
209         /* Probe() has failed. */
210         free( p_aout->output.p_sys );
211         return VLC_EGENERIC;
212     }
213
214     /* Open the device */
215     if( val.i_int == AOUT_VAR_SPDIF )
216     {
217         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
218
219         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
220                          p_aout->output.output.i_physical_channels,
221                          aout_FormatNbChannels( &p_aout->output.output ),
222                          p_aout->output.output.i_rate, VLC_FALSE )
223             != VLC_SUCCESS )
224         {
225             msg_Err( p_aout, "cannot open waveout audio device" );
226             free( p_aout->output.p_sys );
227             return VLC_EGENERIC;
228         }
229
230         /* Calculate the frame size in bytes */
231         p_aout->output.i_nb_samples = A52_FRAME_NB;
232         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
233         p_aout->output.output.i_frame_length = A52_FRAME_NB;
234         p_aout->output.p_sys->i_buffer_size =
235             p_aout->output.output.i_bytes_per_frame;
236
237         aout_VolumeNoneInit( p_aout );
238     }
239     else
240     {
241         if( val.i_int == AOUT_VAR_5_1 )
242         {
243             p_aout->output.output.i_physical_channels
244                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
245                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
246                    | AOUT_CHAN_LFE;
247         }
248         else if( val.i_int == AOUT_VAR_2F2R )
249         {
250             p_aout->output.output.i_physical_channels
251                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
252                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
253         }
254         else if( val.i_int == AOUT_VAR_MONO )
255         {
256             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
257         }
258         else
259         {
260             p_aout->output.output.i_physical_channels
261                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
262         }
263
264         if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
265                             p_aout->output.output.i_physical_channels,
266                             aout_FormatNbChannels( &p_aout->output.output ),
267                             p_aout->output.output.i_rate, VLC_FALSE )
268             != VLC_SUCCESS )
269         {
270             msg_Err( p_aout, "cannot open waveout audio device" );
271             free( p_aout->output.p_sys );
272             return VLC_EGENERIC;
273         }
274
275         /* Calculate the frame size in bytes */
276         p_aout->output.i_nb_samples = FRAME_SIZE;
277         aout_FormatPrepare( &p_aout->output.output );
278         p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
279             p_aout->output.output.i_bytes_per_frame;
280
281         aout_VolumeSoftInit( p_aout );
282     }
283
284
285     waveOutReset( p_aout->output.p_sys->h_waveout );
286
287     /* Allocate silence buffer */
288     p_aout->output.p_sys->p_silence_buffer =
289         malloc( p_aout->output.p_sys->i_buffer_size );
290     if( p_aout->output.p_sys->p_silence_buffer == NULL )
291     {
292         free( p_aout->output.p_sys );
293         msg_Err( p_aout, "out of memory" );
294         return 1;
295     }
296
297     /* Zero the buffer. WinCE doesn't have calloc(). */
298     memset( p_aout->output.p_sys->p_silence_buffer, 0,
299             p_aout->output.p_sys->i_buffer_size );
300
301     /* Now we need to setup our waveOut play notification structure */
302     p_aout->output.p_sys->p_notif =
303         vlc_object_create( p_aout, sizeof(notification_thread_t) );
304     p_aout->output.p_sys->p_notif->p_aout = p_aout;
305     p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );
306
307     /* Then launch the notification thread */
308     if( vlc_thread_create( p_aout->output.p_sys->p_notif,
309                            "waveOut Notification Thread", WaveOutThread,
310                            VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
311     {
312         msg_Err( p_aout, "cannot create WaveOutThread" );
313     }
314
315     /* We need to kick off the playback in order to have the callback properly
316      * working */
317     for( i = 0; i < FRAMES_NUM; i++ )
318     {
319         p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
320     }
321     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
322                  &p_aout->output.p_sys->waveheader[0], NULL );
323
324     return 0;
325 }
326
327 /*****************************************************************************
328  * Probe: probe the audio device for available formats and channels
329  *****************************************************************************/
330 static void Probe( aout_instance_t * p_aout )
331 {
332     vlc_value_t val, text;
333     int i_format;
334     unsigned int i_physical_channels;
335
336     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
337     text.psz_string = _("Audio device");
338     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
339
340     /* Test for 5.1 support */
341     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
342                           AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
343                           AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
344     if( p_aout->output.output.i_physical_channels == i_physical_channels )
345     {
346         if( OpenWaveOutPCM( p_aout, &i_format,
347                             i_physical_channels, 6,
348                             p_aout->output.output.i_rate, VLC_TRUE )
349             == VLC_SUCCESS )
350         {
351             val.i_int = AOUT_VAR_5_1;
352             text.psz_string = N_("5.1");
353             var_Change( p_aout, "audio-device",
354                         VLC_VAR_ADDCHOICE, &val, &text );
355             msg_Dbg( p_aout, "device supports 5.1 channels" );
356         }
357     }
358
359     /* Test for 2 Front 2 Rear support */
360     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
361                           AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
362     if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
363         == i_physical_channels )
364     {
365         if( OpenWaveOutPCM( p_aout, &i_format,
366                             i_physical_channels, 4,
367                             p_aout->output.output.i_rate, VLC_TRUE )
368             == VLC_SUCCESS )
369         {
370             val.i_int = AOUT_VAR_2F2R;
371             text.psz_string = N_("2 Front 2 Rear");
372             var_Change( p_aout, "audio-device",
373                         VLC_VAR_ADDCHOICE, &val, &text );
374             msg_Dbg( p_aout, "device supports 4 channels" );
375         }
376     }
377
378     /* Test for stereo support */
379     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
380     if( OpenWaveOutPCM( p_aout, &i_format,
381                         i_physical_channels, 2,
382                         p_aout->output.output.i_rate, VLC_TRUE )
383         == VLC_SUCCESS )
384     {
385         val.i_int = AOUT_VAR_STEREO;
386         text.psz_string = N_("Stereo");
387         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
388         msg_Dbg( p_aout, "device supports 2 channels" );
389     }
390
391     /* Test for mono support */
392     i_physical_channels = AOUT_CHAN_CENTER;
393     if( OpenWaveOutPCM( p_aout, &i_format,
394                         i_physical_channels, 1,
395                         p_aout->output.output.i_rate, VLC_TRUE )
396         == VLC_SUCCESS )
397     {
398         val.i_int = AOUT_VAR_MONO;
399         text.psz_string = N_("Mono");
400         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
401         msg_Dbg( p_aout, "device supports 1 channel" );
402     }
403
404     /* Test for SPDIF support */
405     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
406     {
407         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
408                          p_aout->output.output.i_physical_channels,
409                          aout_FormatNbChannels( &p_aout->output.output ),
410                          p_aout->output.output.i_rate, VLC_TRUE )
411             == VLC_SUCCESS )
412         {
413             msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
414             val.i_int = AOUT_VAR_SPDIF;
415             text.psz_string = N_("A/52 over S/PDIF");
416             var_Change( p_aout, "audio-device",
417                         VLC_VAR_ADDCHOICE, &val, &text );
418             if( config_GetInt( p_aout, "spdif" ) )
419                 var_Set( p_aout, "audio-device", val );
420         }
421     }
422
423     var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
424     if( val.i_int <= 0 )
425     {
426         /* Probe() has failed. */
427         var_Destroy( p_aout, "audio-device" );
428         return;
429     }
430
431     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
432
433     val.b_bool = VLC_TRUE;
434     var_Set( p_aout, "intf-change", val );
435 }
436
437 /*****************************************************************************
438  * Play: play a sound buffer
439  *****************************************************************************
440  * This doesn't actually play the buffer. This just stores the buffer so it
441  * can be played by the callback thread.
442  *****************************************************************************/
443 static void Play( aout_instance_t *_p_aout )
444 {
445 }
446
447 /*****************************************************************************
448  * Close: close the audio device
449  *****************************************************************************/
450 static void Close( vlc_object_t *p_this )
451 {
452     aout_instance_t *p_aout = (aout_instance_t *)p_this;
453
454     /* Before calling waveOutClose we must reset the device */
455     p_aout->b_die = VLC_TRUE;
456
457     /* wake up the audio thread */
458     SetEvent( p_aout->output.p_sys->event );
459     vlc_thread_join( p_aout->output.p_sys->p_notif );
460     CloseHandle( p_aout->output.p_sys->event );
461
462     /* Wait for the waveout buffers to be freed */
463     while( VLC_TRUE )
464     {
465         int i;
466         vlc_bool_t b_not_done = VLC_FALSE;
467
468         for( i = 0; i < FRAMES_NUM; i++ )
469         {
470            if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
471                b_not_done = VLC_TRUE;
472         }
473
474         if( !b_not_done )
475             break;
476
477         msleep( 1000 );
478     }
479
480     waveOutReset( p_aout->output.p_sys->h_waveout );
481
482     /* Close the device */
483     if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
484     {
485         msg_Err( p_aout, "waveOutClose failed" );
486     }
487
488     free( p_aout->output.p_sys->p_silence_buffer );
489     if( p_aout->output.p_sys->pi_chan_table )
490         free( p_aout->output.p_sys->pi_chan_table );
491     free( p_aout->output.p_sys );
492 }
493
494 /*****************************************************************************
495  * OpenWaveOut: open the waveout sound device
496  ****************************************************************************/
497 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
498                         int i_channels, int i_nb_channels, int i_rate,
499                         vlc_bool_t b_probe )
500 {
501     MMRESULT result;
502     unsigned int i;
503
504     /* Set sound format */
505
506 #define waveformat p_aout->output.p_sys->waveformat
507
508     waveformat.dwChannelMask = 0;
509     for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
510     {
511         if( i_channels & pi_channels_in[i] )
512             waveformat.dwChannelMask |= pi_channels_out[i];
513     }
514
515     switch( i_format )
516     {
517     case VLC_FOURCC('s','p','d','i'):
518         i_nb_channels = 2;
519         /* To prevent channel re-ordering */
520         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
521         waveformat.Format.wBitsPerSample = 16;
522         waveformat.Samples.wValidBitsPerSample =
523             waveformat.Format.wBitsPerSample;
524         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
525         waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
526         break;
527
528     case VLC_FOURCC('f','l','3','2'):
529         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
530         waveformat.Samples.wValidBitsPerSample =
531             waveformat.Format.wBitsPerSample;
532         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
533         waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
534         break;
535
536     case VLC_FOURCC('s','1','6','l'):
537         waveformat.Format.wBitsPerSample = 16;
538         waveformat.Samples.wValidBitsPerSample =
539             waveformat.Format.wBitsPerSample;
540         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
541         waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_PCM;
542         break;
543     }
544
545     waveformat.Format.nChannels = i_nb_channels;
546     waveformat.Format.nSamplesPerSec = i_rate;
547     waveformat.Format.nBlockAlign =
548         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
549     waveformat.Format.nAvgBytesPerSec =
550         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
551
552     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
553     if( i_nb_channels <= 2 )
554     {
555         waveformat.Format.cbSize = 0;
556     }
557     else
558     {
559         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
560         waveformat.Format.cbSize =
561             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
562     }
563
564     /* Open the device */
565     result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
566                           (WAVEFORMATEX *)&waveformat,
567                           (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
568                           CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
569     if( result == WAVERR_BADFORMAT )
570     {
571         msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
572         return VLC_EGENERIC;
573     }
574     if( result == MMSYSERR_ALLOCATED )
575     {
576         msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
577         return VLC_EGENERIC;
578     }
579     if( result != MMSYSERR_NOERROR )
580     {
581         msg_Warn( p_aout, "waveOutOpen failed" );
582         return VLC_EGENERIC;
583     }
584
585     CheckReordering( p_aout, i_nb_channels );
586
587     return VLC_SUCCESS;
588
589 #undef waveformat
590
591 }
592
593 /*****************************************************************************
594  * OpenWaveOutPCM: open a PCM waveout sound device
595  ****************************************************************************/
596 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
597                            int i_channels, int i_nb_channels, int i_rate,
598                            vlc_bool_t b_probe )
599 {
600     if( OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
601                      i_channels, i_nb_channels, i_rate, b_probe )
602         != VLC_SUCCESS )
603     {
604         if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
605                           i_channels, i_nb_channels, i_rate, b_probe )
606              != VLC_SUCCESS )
607         {
608             return VLC_EGENERIC;
609         }
610         else
611         {
612             *i_format = VLC_FOURCC('s','1','6','l');
613             return VLC_SUCCESS;
614         }
615     }
616     else
617     {
618         *i_format = VLC_FOURCC('f','l','3','2');
619         return VLC_SUCCESS;
620     }
621 }
622
623 /*****************************************************************************
624  * CheckReordering: Check if we need to do some channel re-ordering (the ac3
625  *                  channel order is different from the one chosen by
626  *                  Microsoft).
627  *****************************************************************************/
628 static void CheckReordering( aout_instance_t *p_aout, int i_nb_channels )
629 {
630     int i, j, k, l;
631
632 #define waveformat p_aout->output.p_sys->waveformat
633 #define pi_chan_table p_aout->output.p_sys->pi_chan_table
634
635     p_aout->output.p_sys->b_chan_reorder = VLC_FALSE;
636
637     pi_chan_table = malloc( i_nb_channels * sizeof(int) );
638     if( !pi_chan_table )
639     {
640         return;
641     }
642
643     for( i = 0, j = 0;
644          i < (int)(sizeof(pi_channels_out)/sizeof(uint32_t)); i++ )
645     {
646         if( waveformat.dwChannelMask & pi_channels_out[i] )
647         {
648             for( k = 0, l = 0;
649                  pi_channels_out[i] != pi_channels_ordered[k]; k++ )
650             {
651                 if( waveformat.dwChannelMask & pi_channels_ordered[k] )
652                 {
653                     l++;
654                 }
655             }
656
657             pi_chan_table[j] = l;
658
659             j++;
660         }
661     }
662
663     for( i = 0; i < i_nb_channels; i++ )
664     {
665         if( pi_chan_table[i] != i )
666         {
667             p_aout->output.p_sys->b_chan_reorder = VLC_TRUE;
668         }
669     }
670
671     if( p_aout->output.p_sys->b_chan_reorder )
672     {
673         msg_Dbg( p_aout, "channel reordering needed" );
674     }
675
676 #undef pi_chan_table
677 #undef waveformat
678 }
679
680 /*****************************************************************************
681  * PlayWaveOut: play a buffer through the WaveOut device
682  *****************************************************************************/
683 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
684                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
685 {
686     MMRESULT result;
687
688     /* Prepare the buffer */
689     if( p_buffer != NULL )
690         p_waveheader->lpData = p_buffer->p_buffer;
691     else
692         /* Use silence buffer instead */
693         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
694
695     p_waveheader->dwUser = (DWORD_PTR)p_buffer;
696     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
697     p_waveheader->dwFlags = 0;
698
699     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
700     if( result != MMSYSERR_NOERROR )
701     {
702         msg_Err( p_aout, "waveOutPrepareHeader failed" );
703         return VLC_EGENERIC;
704     }
705
706     /* Send the buffer to the waveOut queue */
707     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
708     if( result != MMSYSERR_NOERROR )
709     {
710         msg_Err( p_aout, "waveOutWrite failed" );
711         return VLC_EGENERIC;
712     }
713
714     return VLC_SUCCESS;
715 }
716
717 /*****************************************************************************
718  * WaveOutCallback: what to do once WaveOut has played its sound samples
719  *****************************************************************************/
720 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
721                                       DWORD _p_aout,
722                                       DWORD dwParam1, DWORD dwParam2 )
723 {
724     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
725     WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
726     int i, i_queued_frames = 0;
727
728     if( uMsg != WOM_DONE ) return;
729
730     /* Unprepare and free the buffer which has just been played */
731     waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
732     if( p_waveheader->dwUser )
733         aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
734
735     if( p_aout->b_die ) return;
736
737     /* Find out the current latency */
738     for( i = 0; i < FRAMES_NUM; i++ )
739     {
740         /* Check if frame buf is available */
741         if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
742         {
743             i_queued_frames++;
744         }
745     }
746
747     /* Don't wake up the thread too much */
748     if( i_queued_frames < FRAMES_NUM / 2 )
749         SetEvent( p_aout->output.p_sys->event );
750 }
751
752 /*****************************************************************************
753  * InterleaveFloat32/S16: change the channel order to the Microsoft one.
754  *****************************************************************************/
755 static void InterleaveFloat32( float *p_buf, int *pi_chan_table,
756                                int i_nb_channels )
757 {
758     int i, j;
759     float p_tmp[10];
760
761     for( i = 0; i < FRAME_SIZE; i++ )
762     {
763         for( j = 0; j < i_nb_channels; j++ )
764         {
765             p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
766         }
767
768         memcpy( &p_buf[i*i_nb_channels], p_tmp,
769                 i_nb_channels * sizeof(float) );
770     }
771 }
772
773 static void InterleaveS16( int16_t *p_buf, int *pi_chan_table,
774                            int i_nb_channels )
775 {
776     int i, j;
777     int16_t p_tmp[10];
778
779     for( i = 0; i < FRAME_SIZE; i++ )
780     {
781         for( j = 0; j < i_nb_channels; j++ )
782         {
783             p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
784         }
785
786         memcpy( &p_buf[i*i_nb_channels], p_tmp,
787                 i_nb_channels * sizeof(int16_t) );
788     }
789 }
790
791 /*****************************************************************************
792  * WaveOutThread: this thread will capture play notification events. 
793  *****************************************************************************
794  * We use this thread to feed new audio samples to the sound card because
795  * we are not authorized to use waveOutWrite() directly in the waveout
796  * callback.
797  *****************************************************************************/
798 static void WaveOutThread( notification_thread_t *p_notif )
799 {
800     aout_instance_t *p_aout = p_notif->p_aout;
801     aout_buffer_t *p_buffer = NULL;
802     vlc_bool_t b_sleek;
803     int i, i_queued_frames;
804
805     /* We don't want any resampling when using S/PDIF */
806     b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
807
808     while( 1 )
809     {
810         WaitForSingleObject( p_aout->output.p_sys->event, INFINITE );
811         if( p_aout->b_die ) return;
812
813         /* Find out the current latency */
814         i_queued_frames = 0;
815         for( i = 0; i < FRAMES_NUM; i++ )
816         {
817             /* Check if frame buf is available */
818             if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
819             {
820                 i_queued_frames++;
821             }
822         }
823
824         /* Try to fill in as many frame buffers as possible */
825         for( i = 0; i < FRAMES_NUM; i++ )
826         {
827             /* Check if frame buf is available */
828             if( p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE )
829             {
830                 /* Take into account the latency */
831                 p_buffer = aout_OutputNextBuffer( p_aout,
832                     mdate() + 1000000 * i_queued_frames /
833                     p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
834                     b_sleek );
835
836                 if( !p_buffer && i_queued_frames )
837                 {
838                     /* We aren't late so no need to play a blank sample */
839                     break;
840                 }
841
842                 /* Do the channel reordering here */
843                 if( p_buffer && p_aout->output.p_sys->b_chan_reorder )
844                 {
845                     if( p_aout->output.output.i_format ==
846                             VLC_FOURCC('s','1','6','l') )
847                         InterleaveS16( (int16_t *)p_buffer->p_buffer,
848                             p_aout->output.p_sys->pi_chan_table,
849                             aout_FormatNbChannels( &p_aout->output.output ) );
850                     else
851                         InterleaveFloat32( (float *)p_buffer->p_buffer,
852                             p_aout->output.p_sys->pi_chan_table,
853                             aout_FormatNbChannels( &p_aout->output.output ) );
854                 }
855
856                 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
857                              &p_aout->output.p_sys->waveheader[i] , p_buffer );
858
859                 i_queued_frames++;
860             }
861         }
862     }
863 }