]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
* modules/audio_output/waveout.c: Fixed a rounding issue
[vlc] / modules / audio_output / waveout.c
1 /*****************************************************************************
2  * waveout.c : Windows waveOut plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 #include <math.h>                                                /* roundf() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include "aout_internal.h"
34
35 #include <windows.h>
36 #include <mmsystem.h>
37
38 #define FRAME_SIZE 4096              /* The size is in samples, not in bytes */
39 #define FRAMES_NUM 8
40
41 /*****************************************************************************
42  * Useful macros
43  *****************************************************************************/
44 #ifdef UNDER_CE
45 #   define DWORD_PTR DWORD
46 #endif
47
48 #ifndef WAVE_FORMAT_IEEE_FLOAT
49 #   define WAVE_FORMAT_IEEE_FLOAT 0x0003
50 #endif
51
52 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
53 #   define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
54 #endif
55
56 #ifndef WAVE_FORMAT_EXTENSIBLE
57 #define  WAVE_FORMAT_EXTENSIBLE   0xFFFE
58 #endif
59
60 #ifndef SPEAKER_FRONT_LEFT
61 #   define SPEAKER_FRONT_LEFT             0x1
62 #   define SPEAKER_FRONT_RIGHT            0x2
63 #   define SPEAKER_FRONT_CENTER           0x4
64 #   define SPEAKER_LOW_FREQUENCY          0x8
65 #   define SPEAKER_BACK_LEFT              0x10
66 #   define SPEAKER_BACK_RIGHT             0x20
67 #   define SPEAKER_FRONT_LEFT_OF_CENTER   0x40
68 #   define SPEAKER_FRONT_RIGHT_OF_CENTER  0x80
69 #   define SPEAKER_BACK_CENTER            0x100
70 #   define SPEAKER_SIDE_LEFT              0x200
71 #   define SPEAKER_SIDE_RIGHT             0x400
72 #   define SPEAKER_TOP_CENTER             0x800
73 #   define SPEAKER_TOP_FRONT_LEFT         0x1000
74 #   define SPEAKER_TOP_FRONT_CENTER       0x2000
75 #   define SPEAKER_TOP_FRONT_RIGHT        0x4000
76 #   define SPEAKER_TOP_BACK_LEFT          0x8000
77 #   define SPEAKER_TOP_BACK_CENTER        0x10000
78 #   define SPEAKER_TOP_BACK_RIGHT         0x20000
79 #   define SPEAKER_RESERVED               0x80000000
80 #endif
81
82 #ifndef _WAVEFORMATEXTENSIBLE_
83 typedef struct {
84     WAVEFORMATEX    Format;
85     union {
86         WORD wValidBitsPerSample;       /* bits of precision  */
87         WORD wSamplesPerBlock;          /* valid if wBitsPerSample==0 */
88         WORD wReserved;                 /* If neither applies, set to zero. */
89     } Samples;
90     DWORD           dwChannelMask;      /* which channels are */
91                                         /* present in stream  */
92     GUID            SubFormat;
93 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
94 #endif
95
96 static const GUID __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = {WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
97 static const GUID __KSDATAFORMAT_SUBTYPE_PCM = {WAVE_FORMAT_PCM, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 static const 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 int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
122                            aout_buffer_t * );
123
124 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
125 static void WaveOutThread( notification_thread_t * );
126
127 static int VolumeInfos( aout_instance_t *, audio_volume_t * );
128 static int VolumeGet( aout_instance_t *, audio_volume_t * );
129 static int VolumeSet( aout_instance_t *, audio_volume_t );
130
131 /*****************************************************************************
132  * Module descriptor
133  *****************************************************************************/
134 #define FLOAT_TEXT N_("Use float32 output")
135 #define FLOAT_LONGTEXT N_( \
136     "The option allows you to enable or disable the high-quality float32 " \
137     "audio output mode (which is not well supported by some soundcards)." )
138
139 vlc_module_begin();
140     set_shortname( "WaveOut" );
141     set_description( _("Win32 waveOut extension output") );
142     set_capability( "audio output", 50 );
143     set_category( CAT_AUDIO );
144     set_subcategory( SUBCAT_AUDIO_AOUT );
145     add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, VLC_TRUE );
146     set_callbacks( Open, Close );
147 vlc_module_end();
148
149 /*****************************************************************************
150  * aout_sys_t: waveOut audio output method descriptor
151  *****************************************************************************
152  * This structure is part of the audio output thread descriptor.
153  * It describes the waveOut specific properties of an audio device.
154  *****************************************************************************/
155 struct aout_sys_t
156 {
157     HWAVEOUT h_waveout;                        /* handle to waveout instance */
158
159     WAVEFORMATEXTENSIBLE waveformat;                         /* audio format */
160
161     WAVEHDR waveheader[FRAMES_NUM];
162
163     notification_thread_t *p_notif;                      /* WaveOutThread id */
164     HANDLE event;
165
166     int i_buffer_size;
167
168     byte_t *p_silence_buffer;               /* buffer we use to play silence */
169
170     vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
171     int pi_chan_table[AOUT_CHAN_MAX];
172 };
173
174 static const uint32_t pi_channels_src[] =
175     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
176       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
177       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
178       AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
179 static const uint32_t pi_channels_in[] =
180     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
181       SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
182       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
183       SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
184 static const uint32_t pi_channels_out[] =
185     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
186       SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
187       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
188       SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
189
190 /*****************************************************************************
191  * Open: open the audio device
192  *****************************************************************************
193  * This function opens and setups Win32 waveOut
194  *****************************************************************************/
195 static int Open( vlc_object_t *p_this )
196 {
197     aout_instance_t *p_aout = (aout_instance_t *)p_this;
198     vlc_value_t val;
199     int i;
200
201     /* Allocate structure */
202     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
203
204     if( p_aout->output.p_sys == NULL )
205     {
206         msg_Err( p_aout, "out of memory" );
207         return VLC_EGENERIC;
208     }
209
210     p_aout->output.pf_play = Play;
211     p_aout->b_die = VLC_FALSE;
212
213     if( var_Type( p_aout, "audio-device" ) == 0 )
214     {
215         Probe( p_aout );
216     }
217
218     if( var_Get( p_aout, "audio-device", &val ) < 0 )
219     {
220         /* Probe() has failed. */
221         free( p_aout->output.p_sys );
222         return VLC_EGENERIC;
223     }
224
225     var_Create( p_aout, "waveout-float32", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
226
227     /* Open the device */
228     if( val.i_int == AOUT_VAR_SPDIF )
229     {
230         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
231
232         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
233                          p_aout->output.output.i_physical_channels,
234                          aout_FormatNbChannels( &p_aout->output.output ),
235                          p_aout->output.output.i_rate, VLC_FALSE )
236             != VLC_SUCCESS )
237         {
238             msg_Err( p_aout, "cannot open waveout audio device" );
239             free( p_aout->output.p_sys );
240             return VLC_EGENERIC;
241         }
242
243         /* Calculate the frame size in bytes */
244         p_aout->output.i_nb_samples = A52_FRAME_NB;
245         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
246         p_aout->output.output.i_frame_length = A52_FRAME_NB;
247         p_aout->output.p_sys->i_buffer_size =
248             p_aout->output.output.i_bytes_per_frame;
249
250         aout_VolumeNoneInit( p_aout );
251     }
252     else
253     {
254         if( val.i_int == AOUT_VAR_5_1 )
255         {
256             p_aout->output.output.i_physical_channels
257                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
258                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
259                    | AOUT_CHAN_LFE;
260         }
261         else if( val.i_int == AOUT_VAR_2F2R )
262         {
263             p_aout->output.output.i_physical_channels
264                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
265                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
266         }
267         else if( val.i_int == AOUT_VAR_MONO )
268         {
269             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
270         }
271         else
272         {
273             p_aout->output.output.i_physical_channels
274                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
275         }
276
277         if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
278                             p_aout->output.output.i_physical_channels,
279                             aout_FormatNbChannels( &p_aout->output.output ),
280                             p_aout->output.output.i_rate, VLC_FALSE )
281             != VLC_SUCCESS )
282         {
283             msg_Err( p_aout, "cannot open waveout audio device" );
284             free( p_aout->output.p_sys );
285             return VLC_EGENERIC;
286         }
287
288         /* Calculate the frame size in bytes */
289         p_aout->output.i_nb_samples = FRAME_SIZE;
290         aout_FormatPrepare( &p_aout->output.output );
291         p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
292             p_aout->output.output.i_bytes_per_frame;
293
294         aout_VolumeSoftInit( p_aout );
295
296         p_aout->output.pf_volume_infos = VolumeInfos;
297         p_aout->output.pf_volume_get = VolumeGet;
298         p_aout->output.pf_volume_set = VolumeSet;
299     }
300
301
302     waveOutReset( p_aout->output.p_sys->h_waveout );
303
304     /* Allocate silence buffer */
305     p_aout->output.p_sys->p_silence_buffer =
306         malloc( p_aout->output.p_sys->i_buffer_size );
307     if( p_aout->output.p_sys->p_silence_buffer == NULL )
308     {
309         free( p_aout->output.p_sys );
310         msg_Err( p_aout, "out of memory" );
311         return 1;
312     }
313
314     /* Zero the buffer. WinCE doesn't have calloc(). */
315     memset( p_aout->output.p_sys->p_silence_buffer, 0,
316             p_aout->output.p_sys->i_buffer_size );
317
318     /* Now we need to setup our waveOut play notification structure */
319     p_aout->output.p_sys->p_notif =
320         vlc_object_create( p_aout, sizeof(notification_thread_t) );
321     p_aout->output.p_sys->p_notif->p_aout = p_aout;
322     p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );
323
324     /* Then launch the notification thread */
325     if( vlc_thread_create( p_aout->output.p_sys->p_notif,
326                            "waveOut Notification Thread", WaveOutThread,
327                            VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
328     {
329         msg_Err( p_aout, "cannot create WaveOutThread" );
330     }
331
332     /* We need to kick off the playback in order to have the callback properly
333      * working */
334     for( i = 0; i < FRAMES_NUM; i++ )
335     {
336         p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
337         p_aout->output.p_sys->waveheader[i].dwUser = 0;
338     }
339     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
340                  &p_aout->output.p_sys->waveheader[0], NULL );
341
342     return 0;
343 }
344
345 /*****************************************************************************
346  * Probe: probe the audio device for available formats and channels
347  *****************************************************************************/
348 static void Probe( aout_instance_t * p_aout )
349 {
350     vlc_value_t val, text;
351     int i_format;
352     unsigned int i_physical_channels;
353
354     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
355     text.psz_string = _("Audio Device");
356     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
357
358     /* Test for 5.1 support */
359     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
360                           AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
361                           AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
362     if( p_aout->output.output.i_physical_channels == i_physical_channels )
363     {
364         if( OpenWaveOutPCM( p_aout, &i_format,
365                             i_physical_channels, 6,
366                             p_aout->output.output.i_rate, VLC_TRUE )
367             == VLC_SUCCESS )
368         {
369             val.i_int = AOUT_VAR_5_1;
370             text.psz_string = N_("5.1");
371             var_Change( p_aout, "audio-device",
372                         VLC_VAR_ADDCHOICE, &val, &text );
373             msg_Dbg( p_aout, "device supports 5.1 channels" );
374         }
375     }
376
377     /* Test for 2 Front 2 Rear support */
378     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
379                           AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
380     if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
381         == i_physical_channels )
382     {
383         if( OpenWaveOutPCM( p_aout, &i_format,
384                             i_physical_channels, 4,
385                             p_aout->output.output.i_rate, VLC_TRUE )
386             == VLC_SUCCESS )
387         {
388             val.i_int = AOUT_VAR_2F2R;
389             text.psz_string = N_("2 Front 2 Rear");
390             var_Change( p_aout, "audio-device",
391                         VLC_VAR_ADDCHOICE, &val, &text );
392             msg_Dbg( p_aout, "device supports 4 channels" );
393         }
394     }
395
396     /* Test for stereo support */
397     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
398     if( OpenWaveOutPCM( p_aout, &i_format,
399                         i_physical_channels, 2,
400                         p_aout->output.output.i_rate, VLC_TRUE )
401         == VLC_SUCCESS )
402     {
403         val.i_int = AOUT_VAR_STEREO;
404         text.psz_string = N_("Stereo");
405         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
406         msg_Dbg( p_aout, "device supports 2 channels" );
407     }
408
409     /* Test for mono support */
410     i_physical_channels = AOUT_CHAN_CENTER;
411     if( OpenWaveOutPCM( p_aout, &i_format,
412                         i_physical_channels, 1,
413                         p_aout->output.output.i_rate, VLC_TRUE )
414         == VLC_SUCCESS )
415     {
416         val.i_int = AOUT_VAR_MONO;
417         text.psz_string = N_("Mono");
418         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
419         msg_Dbg( p_aout, "device supports 1 channel" );
420     }
421
422     /* Test for SPDIF support */
423     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
424     {
425         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
426                          p_aout->output.output.i_physical_channels,
427                          aout_FormatNbChannels( &p_aout->output.output ),
428                          p_aout->output.output.i_rate, VLC_TRUE )
429             == VLC_SUCCESS )
430         {
431             msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
432             val.i_int = AOUT_VAR_SPDIF;
433             text.psz_string = N_("A/52 over S/PDIF");
434             var_Change( p_aout, "audio-device",
435                         VLC_VAR_ADDCHOICE, &val, &text );
436             if( config_GetInt( p_aout, "spdif" ) )
437                 var_Set( p_aout, "audio-device", val );
438         }
439     }
440
441     var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
442     if( val.i_int <= 0 )
443     {
444         /* Probe() has failed. */
445         var_Destroy( p_aout, "audio-device" );
446         return;
447     }
448
449     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
450
451     val.b_bool = VLC_TRUE;
452     var_Set( p_aout, "intf-change", val );
453 }
454
455 /*****************************************************************************
456  * Play: play a sound buffer
457  *****************************************************************************
458  * This doesn't actually play the buffer. This just stores the buffer so it
459  * can be played by the callback thread.
460  *****************************************************************************/
461 static void Play( aout_instance_t *_p_aout )
462 {
463 }
464
465 /*****************************************************************************
466  * Close: close the audio device
467  *****************************************************************************/
468 static void Close( vlc_object_t *p_this )
469 {
470     aout_instance_t *p_aout = (aout_instance_t *)p_this;
471     aout_sys_t *p_sys = p_aout->output.p_sys;
472
473     /* Before calling waveOutClose we must reset the device */
474     p_aout->b_die = VLC_TRUE;
475
476     waveOutReset( p_sys->h_waveout );
477
478     /* wake up the audio thread */
479     SetEvent( p_sys->event );
480     vlc_thread_join( p_sys->p_notif );
481     vlc_object_destroy( p_sys->p_notif );
482     CloseHandle( p_sys->event );
483
484     /* Close the device */
485     if( waveOutClose( p_sys->h_waveout ) != MMSYSERR_NOERROR )
486     {
487         msg_Err( p_aout, "waveOutClose failed" );
488     }
489
490     free( p_sys->p_silence_buffer );
491     free( 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_src)/sizeof(uint32_t); i++ )
510     {
511         if( i_channels & pi_channels_src[i] )
512             waveformat.dwChannelMask |= pi_channels_in[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     p_aout->output.p_sys->b_chan_reorder =
586         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
587                                   waveformat.dwChannelMask, i_nb_channels,
588                                   p_aout->output.p_sys->pi_chan_table );
589
590     if( p_aout->output.p_sys->b_chan_reorder )
591     {
592         msg_Dbg( p_aout, "channel reordering needed" );
593     }
594
595     return VLC_SUCCESS;
596
597 #undef waveformat
598
599 }
600
601 /*****************************************************************************
602  * OpenWaveOutPCM: open a PCM waveout sound device
603  ****************************************************************************/
604 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
605                            int i_channels, int i_nb_channels, int i_rate,
606                            vlc_bool_t b_probe )
607 {
608     vlc_value_t val;
609
610     var_Get( p_aout, "waveout-float32", &val );
611
612     if( !val.b_bool || OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
613                                    i_channels, i_nb_channels, i_rate, b_probe )
614         != VLC_SUCCESS )
615     {
616         if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
617                           i_channels, i_nb_channels, i_rate, b_probe )
618              != VLC_SUCCESS )
619         {
620             return VLC_EGENERIC;
621         }
622         else
623         {
624             *i_format = VLC_FOURCC('s','1','6','l');
625             return VLC_SUCCESS;
626         }
627     }
628     else
629     {
630         *i_format = VLC_FOURCC('f','l','3','2');
631         return VLC_SUCCESS;
632     }
633 }
634
635 /*****************************************************************************
636  * PlayWaveOut: play a buffer through the WaveOut device
637  *****************************************************************************/
638 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
639                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
640 {
641     MMRESULT result;
642
643     /* Prepare the buffer */
644     if( p_buffer != NULL )
645         p_waveheader->lpData = p_buffer->p_buffer;
646     else
647         /* Use silence buffer instead */
648         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
649
650     p_waveheader->dwUser = p_buffer ? (DWORD_PTR)p_buffer : (DWORD_PTR)1;
651     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
652     p_waveheader->dwFlags = 0;
653
654     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
655     if( result != MMSYSERR_NOERROR )
656     {
657         msg_Err( p_aout, "waveOutPrepareHeader failed" );
658         return VLC_EGENERIC;
659     }
660
661     /* Send the buffer to the waveOut queue */
662     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
663     if( result != MMSYSERR_NOERROR )
664     {
665         msg_Err( p_aout, "waveOutWrite failed" );
666         return VLC_EGENERIC;
667     }
668
669     return VLC_SUCCESS;
670 }
671
672 /*****************************************************************************
673  * WaveOutCallback: what to do once WaveOut has played its sound samples
674  *****************************************************************************/
675 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
676                                       DWORD _p_aout,
677                                       DWORD dwParam1, DWORD dwParam2 )
678 {
679     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
680     int i, i_queued_frames = 0;
681
682     if( uMsg != WOM_DONE ) return;
683
684     if( p_aout->b_die ) return;
685
686     /* Find out the current latency */
687     for( i = 0; i < FRAMES_NUM; i++ )
688     {
689         /* Check if frame buf is available */
690         if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
691         {
692             i_queued_frames++;
693         }
694     }
695
696     /* Don't wake up the thread too much */
697     if( i_queued_frames < FRAMES_NUM / 2 )
698         SetEvent( p_aout->output.p_sys->event );
699 }
700
701 /*****************************************************************************
702  * WaveOutThread: this thread will capture play notification events.
703  *****************************************************************************
704  * We use this thread to feed new audio samples to the sound card because
705  * we are not authorized to use waveOutWrite() directly in the waveout
706  * callback.
707  *****************************************************************************/
708 static void WaveOutThread( notification_thread_t *p_notif )
709 {
710     aout_instance_t *p_aout = p_notif->p_aout;
711     aout_sys_t *p_sys = p_aout->output.p_sys;
712     aout_buffer_t *p_buffer = NULL;
713     WAVEHDR *p_waveheader = p_sys->waveheader;
714     int i, i_queued_frames;
715     vlc_bool_t b_sleek;
716
717     /* We don't want any resampling when using S/PDIF */
718     b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
719
720     while( 1 )
721     {
722         WaitForSingleObject( p_sys->event, INFINITE );
723
724         /* Cleanup and find out the current latency */
725         i_queued_frames = 0;
726         for( i = 0; i < FRAMES_NUM; i++ )
727         {
728             if( (p_waveheader[i].dwFlags & WHDR_DONE) &&
729                 p_waveheader[i].dwUser )
730             {
731                 /* Unprepare and free the buffers which has just been played */
732                 waveOutUnprepareHeader( p_sys->h_waveout, &p_waveheader[i],
733                                         sizeof(WAVEHDR) );
734
735                 if( p_waveheader[i].dwUser != 1 )
736                     aout_BufferFree( (aout_buffer_t *)p_waveheader[i].dwUser );
737
738                 p_waveheader[i].dwUser = 0;
739             }
740
741             /* Check if frame buf is available */
742             if( !(p_waveheader[i].dwFlags & WHDR_DONE) )
743             {
744                 i_queued_frames++;
745             }
746         }
747
748         if( p_aout->b_die ) return;
749
750         /* Try to fill in as many frame buffers as possible */
751         for( i = 0; i < FRAMES_NUM; i++ )
752         {
753             /* Check if frame buf is available */
754             if( p_waveheader[i].dwFlags & WHDR_DONE )
755             {
756                 /* Take into account the latency */
757                 p_buffer = aout_OutputNextBuffer( p_aout,
758                     mdate() + 1000000 * i_queued_frames /
759                     p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
760                     b_sleek );
761
762                 if( !p_buffer && i_queued_frames )
763                 {
764                     /* We aren't late so no need to play a blank sample */
765                     break;
766                 }
767
768                 /* Do the channel reordering */
769                 if( p_buffer && p_sys->b_chan_reorder )
770                 {
771                     aout_ChannelReorder( p_buffer->p_buffer,
772                         p_buffer->i_nb_bytes,
773                         p_sys->waveformat.Format.nChannels,
774                         p_sys->pi_chan_table,
775                         p_sys->waveformat.Format.wBitsPerSample );
776                 }
777
778                 PlayWaveOut( p_aout, p_sys->h_waveout,
779                              &p_waveheader[i], p_buffer );
780
781                 i_queued_frames++;
782             }
783         }
784     }
785 }
786
787 static int VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
788 {
789     *pi_soft = AOUT_VOLUME_MAX / 2;
790     return 0;
791 }
792
793 static int VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
794 {
795     DWORD i_waveout_vol;
796
797 #ifdef UNDER_CE
798     waveOutGetVolume( 0, &i_waveout_vol );
799 #else
800     waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_waveout_vol );
801 #endif
802
803     i_waveout_vol &= 0xFFFF;
804     /* Force float computation, otherwise VolumeGet does not return the value
805      * which was set with VolumeSet, because of rounding issues */
806     *pi_volume = p_aout->output.i_volume =
807         (audio_volume_t)roundf((float)i_waveout_vol * AOUT_VOLUME_MAX
808                                / 2.0 / 0xFFFF);
809     return 0;
810 }
811
812 static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
813 {
814     unsigned long i_waveout_vol = i_volume * 0xFFFF * 2 / AOUT_VOLUME_MAX;
815     i_waveout_vol |= (i_waveout_vol << 16);
816
817 #ifdef UNDER_CE
818     waveOutSetVolume( 0, i_waveout_vol );
819 #else
820     waveOutSetVolume( p_aout->output.p_sys->h_waveout, i_waveout_vol );
821 #endif
822
823     p_aout->output.i_volume = i_volume;
824     return 0;
825 }