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