]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
* modules/audio_output/directx.c, modules/audio_output/waveout.c:
[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.22 2003/04/07 16:02:08 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 4
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 /* local functions */
108 static void Probe        ( aout_instance_t * );
109 static int OpenWaveOut   ( aout_instance_t *, int, int, int, int, vlc_bool_t );
110 static int OpenWaveOutPCM( aout_instance_t *, int*, int, int, int, vlc_bool_t );
111 static void CheckReordering( aout_instance_t *, int );
112 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
113                            aout_buffer_t * );
114
115 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
116
117 static void InterleaveFloat32( float *, int *, int );
118 static void InterleaveS16( int16_t *, int *, int );
119
120 /*****************************************************************************
121  * Module descriptor
122  *****************************************************************************/
123 vlc_module_begin();
124     set_description( _("Win32 waveOut extension output") );
125     set_capability( "audio output", 50 );
126     set_callbacks( Open, Close );
127 vlc_module_end();
128
129 /*****************************************************************************
130  * aout_sys_t: waveOut audio output method descriptor
131  *****************************************************************************
132  * This structure is part of the audio output thread descriptor.
133  * It describes the waveOut specific properties of an audio device.
134  *****************************************************************************/
135 struct aout_sys_t
136 {
137     HWAVEOUT h_waveout;                        /* handle to waveout instance */
138
139     WAVEFORMATEXTENSIBLE waveformat;                         /* audio format */
140
141     WAVEHDR waveheader[FRAMES_NUM];
142
143     int i_buffer_size;
144
145     byte_t *p_silence_buffer;               /* buffer we use to play silence */
146
147     vlc_bool_t b_chan_reorder;              /* do we need channel reordering */
148     int *pi_chan_table;
149 };
150
151 static const uint32_t pi_channels_in[] =
152     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
153       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
154       AOUT_CHAN_CENTER, AOUT_CHAN_LFE };
155 static const uint32_t pi_channels_out[] =
156     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
157       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
158       SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY };
159 static const uint32_t pi_channels_ordered[] =
160     { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER,
161       SPEAKER_LOW_FREQUENCY,
162       SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT };
163
164 /*****************************************************************************
165  * Open: open the audio device
166  *****************************************************************************
167  * This function opens and setups Win32 waveOut
168  *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
170 {
171     aout_instance_t *p_aout = (aout_instance_t *)p_this;
172     vlc_value_t val;
173     int i;
174
175     /* Allocate structure */
176     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
177
178     if( p_aout->output.p_sys == NULL )
179     {
180         msg_Err( p_aout, "out of memory" );
181         return VLC_EGENERIC;
182     }
183
184     p_aout->output.pf_play = Play;
185     p_aout->output.p_sys->pi_chan_table = NULL;
186     p_aout->b_die = VLC_FALSE;
187
188     if( var_Type( p_aout, "audio-device" ) == 0 )
189     {
190         Probe( p_aout );
191     }
192
193     if( var_Get( p_aout, "audio-device", &val ) < 0 )
194     {
195         /* Probe() has failed. */
196         free( p_aout->output.p_sys );
197         return VLC_EGENERIC;
198     }
199
200     /* Open the device */
201     if( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
202     {
203         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
204         free( val.psz_string );
205
206         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
207                          p_aout->output.output.i_physical_channels,
208                          aout_FormatNbChannels( &p_aout->output.output ),
209                          p_aout->output.output.i_rate, VLC_FALSE )
210             != VLC_SUCCESS )
211         {
212             msg_Err( p_aout, "cannot open waveout audio device" );
213             free( p_aout->output.p_sys );
214             return VLC_EGENERIC;
215         }
216
217         /* Calculate the frame size in bytes */
218         p_aout->output.i_nb_samples = A52_FRAME_NB;
219         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
220         p_aout->output.output.i_frame_length = A52_FRAME_NB;
221         p_aout->output.p_sys->i_buffer_size =
222             p_aout->output.output.i_bytes_per_frame;
223
224         aout_VolumeNoneInit( p_aout );
225     }
226     else
227     {
228         if( !strcmp( val.psz_string, N_("5.1") ) )
229         {
230             p_aout->output.output.i_physical_channels
231                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
232                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
233                    | AOUT_CHAN_LFE;
234         }
235         else if( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
236         {
237             p_aout->output.output.i_physical_channels
238                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
239                    | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
240         }
241         else if( !strcmp( val.psz_string, "Mono" ) )
242         {
243             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
244         }
245         else
246         {
247             p_aout->output.output.i_physical_channels
248                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
249         }
250         free( val.psz_string );
251
252         if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
253                             p_aout->output.output.i_physical_channels,
254                             aout_FormatNbChannels( &p_aout->output.output ),
255                             p_aout->output.output.i_rate, VLC_FALSE )
256             != VLC_SUCCESS )
257         {
258             msg_Err( p_aout, "cannot open waveout audio device" );
259             free( p_aout->output.p_sys );
260             return VLC_EGENERIC;
261         }
262
263         /* Calculate the frame size in bytes */
264         p_aout->output.i_nb_samples = FRAME_SIZE;
265         aout_FormatPrepare( &p_aout->output.output );
266         p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
267             p_aout->output.output.i_bytes_per_frame;
268
269         aout_VolumeSoftInit( p_aout );
270     }
271
272
273     waveOutReset( p_aout->output.p_sys->h_waveout );
274
275     /* Allocate silence buffer */
276     p_aout->output.p_sys->p_silence_buffer =
277         malloc( p_aout->output.p_sys->i_buffer_size );
278     if( p_aout->output.p_sys->p_silence_buffer == NULL )
279     {
280         free( p_aout->output.p_sys );
281         msg_Err( p_aout, "out of memory" );
282         return 1;
283     }
284
285     /* Zero the buffer. WinCE doesn't have calloc(). */
286     memset( p_aout->output.p_sys->p_silence_buffer, 0,
287             p_aout->output.p_sys->i_buffer_size );
288
289     /* We need to kick off the playback in order to have the callback properly
290      * working */
291     for( i = 0; i < FRAMES_NUM; i++ )
292     {
293         p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
294     }
295     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
296                  &p_aout->output.p_sys->waveheader[0], NULL );
297
298     return 0;
299 }
300
301 /*****************************************************************************
302  * Probe: probe the audio device for available formats and channels
303  *****************************************************************************/
304 static void Probe( aout_instance_t * p_aout )
305 {
306     vlc_value_t val;
307     int i_format;
308     unsigned int i_physical_channels;
309
310     var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
311
312     /* Test for 5.1 support */
313     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
314                           AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
315                           AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
316     if( p_aout->output.output.i_physical_channels == i_physical_channels )
317     {
318         if( OpenWaveOutPCM( p_aout, &i_format,
319                             i_physical_channels, 6,
320                             p_aout->output.output.i_rate, VLC_TRUE )
321             == VLC_SUCCESS )
322         {
323             val.psz_string = N_("5.1");
324             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
325             msg_Dbg( p_aout, "device supports 5.1 channels" );
326         }
327     }
328
329     /* Test for 2 Front 2 Rear support */
330     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
331                           AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
332     if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
333         == i_physical_channels )
334     {
335         if( OpenWaveOutPCM( p_aout, &i_format,
336                             i_physical_channels, 4,
337                             p_aout->output.output.i_rate, VLC_TRUE )
338             == VLC_SUCCESS )
339         {
340             val.psz_string = N_("2 Front 2 Rear");
341             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
342             msg_Dbg( p_aout, "device supports 4 channels" );
343         }
344     }
345
346     /* Test for stereo support */
347     i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
348     if( OpenWaveOutPCM( p_aout, &i_format,
349                         i_physical_channels, 2,
350                         p_aout->output.output.i_rate, VLC_TRUE )
351         == VLC_SUCCESS )
352     {
353         val.psz_string = N_("Stereo");
354         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
355         var_Set( p_aout, "audio-device", val );
356         msg_Dbg( p_aout, "device supports 2 channels" );
357     }
358
359     /* Test for mono support */
360     i_physical_channels = AOUT_CHAN_CENTER;
361     if( OpenWaveOutPCM( p_aout, &i_format,
362                         i_physical_channels, 1,
363                         p_aout->output.output.i_rate, VLC_TRUE )
364         == VLC_SUCCESS )
365     {
366         val.psz_string = N_("Mono");
367         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );        
368         msg_Dbg( p_aout, "device supports 1 channel" );
369     }
370
371     /* Test for SPDIF support */
372     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
373     {
374         if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
375                          p_aout->output.output.i_physical_channels,
376                          aout_FormatNbChannels( &p_aout->output.output ),
377                          p_aout->output.output.i_rate, VLC_TRUE )
378             == VLC_SUCCESS )
379         {
380             msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
381             val.psz_string = N_("A/52 over S/PDIF");
382             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
383             if( config_GetInt( p_aout, "spdif" ) )
384                 var_Set( p_aout, "audio-device", val );
385         }
386     }
387
388     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
389
390     val.b_bool = VLC_TRUE;
391     var_Set( p_aout, "intf-change", val );
392 }
393
394 /*****************************************************************************
395  * Play: play a sound buffer
396  *****************************************************************************
397  * This doesn't actually play the buffer. This just stores the buffer so it
398  * can be played by the callback thread.
399  *****************************************************************************/
400 static void Play( aout_instance_t *p_aout )
401 {
402 }
403
404 /*****************************************************************************
405  * Close: close the audio device
406  *****************************************************************************/
407 static void Close( vlc_object_t *p_this )
408 {
409     aout_instance_t *p_aout = (aout_instance_t *)p_this;
410
411     /* Before calling waveOutClose we must reset the device */
412     p_aout->b_die = VLC_TRUE;
413
414     /* Wait for the waveout buffers to be freed */
415     while( VLC_TRUE )
416     {
417         int i;
418         vlc_bool_t b_not_done = VLC_FALSE;
419
420         for( i = 0; i < FRAMES_NUM; i++ )
421         {
422            if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
423                b_not_done = VLC_TRUE;
424         }
425
426         if( !b_not_done )
427             break;
428
429         msleep( 1000 );
430     }
431
432     waveOutReset( p_aout->output.p_sys->h_waveout );
433
434     /* Close the device */
435     if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
436     {
437         msg_Err( p_aout, "waveOutClose failed" );
438     }
439
440     free( p_aout->output.p_sys->p_silence_buffer );
441     if( p_aout->output.p_sys->pi_chan_table )
442         free( p_aout->output.p_sys->pi_chan_table );
443     free( p_aout->output.p_sys );
444 }
445
446 /*****************************************************************************
447  * OpenWaveOut: open the waveout sound device
448  ****************************************************************************/
449 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
450                         int i_channels, int i_nb_channels, int i_rate,
451                         vlc_bool_t b_probe )
452 {
453     MMRESULT result;
454     unsigned int i;
455
456     /* Set sound format */
457
458 #define waveformat p_aout->output.p_sys->waveformat
459
460     waveformat.dwChannelMask = 0;
461     for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
462     {
463         if( i_channels & pi_channels_in[i] )
464             waveformat.dwChannelMask |= pi_channels_out[i];
465     }
466
467     switch( i_format )
468     {
469     case VLC_FOURCC('s','p','d','i'):
470         i_nb_channels = 2;
471         /* To prevent channel re-ordering */
472         waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
473         waveformat.Format.wBitsPerSample = 16;
474         waveformat.Samples.wValidBitsPerSample =
475             waveformat.Format.wBitsPerSample;
476         waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
477         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
478         break;
479
480     case VLC_FOURCC('f','l','3','2'):
481         waveformat.Format.wBitsPerSample = sizeof(float) * 8;
482         waveformat.Samples.wValidBitsPerSample =
483             waveformat.Format.wBitsPerSample;
484         waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
485         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
486         break;
487
488     case VLC_FOURCC('s','1','6','l'):
489         waveformat.Format.wBitsPerSample = 16;
490         waveformat.Samples.wValidBitsPerSample =
491             waveformat.Format.wBitsPerSample;
492         waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
493         waveformat.SubFormat = _KSDATAFORMAT_SUBTYPE_PCM;
494         break;
495     }
496
497     waveformat.Format.nChannels = i_nb_channels;
498     waveformat.Format.nSamplesPerSec = i_rate;
499     waveformat.Format.nBlockAlign =
500         waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
501     waveformat.Format.nAvgBytesPerSec =
502         waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
503
504     /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
505     if( i_nb_channels <= 2 )
506     {
507         waveformat.Format.cbSize = 0;
508     }
509     else
510     {
511         waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
512         waveformat.Format.cbSize =
513             sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
514     }
515
516     /* Open the device */
517     result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
518                           (WAVEFORMATEX *)&waveformat,
519                           (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
520                           CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
521     if( result == WAVERR_BADFORMAT )
522     {
523         msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
524         return VLC_EGENERIC;
525     }
526     if( result == MMSYSERR_ALLOCATED )
527     {
528         msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
529         return VLC_EGENERIC;
530     }
531     if( result != MMSYSERR_NOERROR )
532     {
533         msg_Warn( p_aout, "waveOutOpen failed" );
534         return VLC_EGENERIC;
535     }
536
537     CheckReordering( p_aout, i_nb_channels );
538
539     return VLC_SUCCESS;
540
541 #undef waveformat
542
543 }
544
545 /*****************************************************************************
546  * OpenWaveOutPCM: open a PCM waveout sound device
547  ****************************************************************************/
548 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
549                            int i_channels, int i_nb_channels, int i_rate,
550                            vlc_bool_t b_probe )
551 {
552     if( OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
553                      i_channels, i_nb_channels, i_rate, b_probe )
554         != VLC_SUCCESS )
555     {
556         if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
557                           i_channels, i_nb_channels, i_rate, b_probe )
558              != VLC_SUCCESS )
559         {
560             return VLC_EGENERIC;
561         }
562         else
563         {
564             *i_format = VLC_FOURCC('s','1','6','l');
565             return VLC_SUCCESS;
566         }
567     }
568     else
569     {
570         *i_format = VLC_FOURCC('f','l','3','2');
571         return VLC_SUCCESS;
572     }
573 }
574
575 /*****************************************************************************
576  * CheckReordering: Check if we need to do some channel re-ordering (the ac3
577  *                  channel order is different from the one chosen by
578  *                  Microsoft).
579  *****************************************************************************/
580 static void CheckReordering( aout_instance_t *p_aout, int i_nb_channels )
581 {
582     int i, j, k, l;
583
584 #define waveformat p_aout->output.p_sys->waveformat
585 #define pi_chan_table p_aout->output.p_sys->pi_chan_table
586
587     p_aout->output.p_sys->b_chan_reorder = VLC_FALSE;
588
589     pi_chan_table = malloc( i_nb_channels * sizeof(int) );
590     if( !pi_chan_table )
591     {
592         return;
593     }
594
595     for( i = 0, j = 0;
596          i < (int)(sizeof(pi_channels_out)/sizeof(uint32_t)); i++ )
597     {
598         if( waveformat.dwChannelMask & pi_channels_out[i] )
599         {
600             for( k = 0, l = 0;
601                  pi_channels_out[i] != pi_channels_ordered[k]; k++ )
602             {
603                 if( waveformat.dwChannelMask & pi_channels_ordered[k] )
604                 {
605                     l++;
606                 }
607             }
608
609             pi_chan_table[j] = l;
610
611             j++;
612         }
613     }
614
615     for( i = 0; i < i_nb_channels; i++ )
616     {
617         if( pi_chan_table[i] != i )
618         {
619             p_aout->output.p_sys->b_chan_reorder = VLC_TRUE;
620         }
621     }
622
623     if( p_aout->output.p_sys->b_chan_reorder )
624     {
625         msg_Dbg( p_aout, "channel reordering needed" );
626     }
627
628 #undef pi_chan_table
629 #undef waveformat
630 }
631
632 /*****************************************************************************
633  * PlayWaveOut: play a buffer through the WaveOut device
634  *****************************************************************************/
635 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
636                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
637 {
638     MMRESULT result;
639
640     /* Prepare the buffer */
641     if( p_buffer != NULL )
642         p_waveheader->lpData = p_buffer->p_buffer;
643     else
644         /* Use silence buffer instead */
645         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
646
647     p_waveheader->dwUser = (DWORD_PTR)p_buffer;
648     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
649     p_waveheader->dwFlags = 0;
650
651     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
652     if( result != MMSYSERR_NOERROR )
653     {
654         msg_Err( p_aout, "waveOutPrepareHeader failed" );
655         return VLC_EGENERIC;
656     }
657
658     /* Send the buffer to the waveOut queue */
659     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
660     if( result != MMSYSERR_NOERROR )
661     {
662         msg_Err( p_aout, "waveOutWrite failed" );
663         return VLC_EGENERIC;
664     }
665
666     return VLC_SUCCESS;
667 }
668
669 /*****************************************************************************
670  * WaveOutCallback: what to do once WaveOut has played its sound samples
671  *****************************************************************************/
672 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
673                                       DWORD _p_aout,
674                                       DWORD dwParam1, DWORD dwParam2 )
675 {
676     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
677     WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
678     aout_buffer_t *p_buffer = NULL;
679     vlc_bool_t b_sleek;
680     int i, i_queued_frames = 0;
681
682     if( uMsg != WOM_DONE ) return;
683
684     /* Unprepare and free the buffer which has just been played */
685     waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
686     if( p_waveheader->dwUser )
687         aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
688
689     if( p_aout->b_die ) return;
690
691     /* We don't want any resampling when using S/PDIF */
692     b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
693
694     /* Find out the current latency */
695     for( i = 0; i < FRAMES_NUM; i++ )
696     {
697         /* Check if frame buf is available */
698         if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
699         {
700             i_queued_frames++;
701         }
702     }
703
704     /* Try to fill in as many frame buffers as possible */
705     for( i = 0; i < FRAMES_NUM; i++ )
706     {
707         /* Check if frame buf is available */
708         if( p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE )
709         {
710             /* Take into account the latency */
711             p_buffer = aout_OutputNextBuffer( p_aout,
712                 mdate() + 1000000 * i_queued_frames /
713                     p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
714                 b_sleek );
715
716             if( !p_buffer && i_queued_frames )
717             {
718                 /* We aren't late so no need to play a blank sample */
719                 return;
720             }
721
722             /* Do the channel reordering here */
723             if( p_buffer && p_aout->output.p_sys->b_chan_reorder )
724             {
725                 if( p_aout->output.output.i_format ==
726                         VLC_FOURCC('s','1','6','l') )
727                     InterleaveS16( (int16_t *)p_buffer->p_buffer,
728                         p_aout->output.p_sys->pi_chan_table,
729                         aout_FormatNbChannels( &p_aout->output.output ) );
730                 else
731                     InterleaveFloat32( (float *)p_buffer->p_buffer,
732                         p_aout->output.p_sys->pi_chan_table,
733                         aout_FormatNbChannels( &p_aout->output.output ) );
734             }
735
736             PlayWaveOut( p_aout, h_waveout,
737                          &p_aout->output.p_sys->waveheader[i] , p_buffer );
738
739             i_queued_frames++;
740         }
741     }
742 }
743
744 /*****************************************************************************
745  * InterleaveFloat32/S16: change the channel order to the Microsoft one.
746  *****************************************************************************/
747 static void InterleaveFloat32( float *p_buf, int *pi_chan_table,
748                                int i_nb_channels )
749 {
750     int i, j;
751     float p_tmp[10];
752
753     for( i = 0; i < FRAME_SIZE; i++ )
754     {
755         for( j = 0; j < i_nb_channels; j++ )
756         {
757             p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
758         }
759
760         memcpy( &p_buf[i*i_nb_channels], p_tmp,
761                 i_nb_channels * sizeof(float) );
762     }
763 }
764
765 static void InterleaveS16( int16_t *p_buf, int *pi_chan_table,
766                            int i_nb_channels )
767 {
768     int i, j;
769     int16_t p_tmp[10];
770
771     for( i = 0; i < FRAME_SIZE; i++ )
772     {
773         for( j = 0; j < i_nb_channels; j++ )
774         {
775             p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
776         }
777
778         memcpy( &p_buf[i*i_nb_channels], p_tmp,
779                 i_nb_channels * sizeof(int16_t) );
780     }
781 }