1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.24 2003/05/04 22:42:15 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <string.h> /* strerror() */
28 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include "aout_internal.h"
37 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
40 /*****************************************************************************
42 *****************************************************************************/
44 # define DWORD_PTR DWORD
47 #ifndef WAVE_FORMAT_IEEE_FLOAT
48 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
51 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
52 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
55 #ifndef WAVE_FORMAT_EXTENSIBLE
56 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
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
81 #ifndef _WAVEFORMATEXTENSIBLE_
85 WORD wValidBitsPerSample; /* bits of precision */
86 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
87 WORD wReserved; /* If neither applies, set to zero. */
89 DWORD dwChannelMask; /* which channels are */
90 /* present in stream */
92 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
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 );
100 /*****************************************************************************
102 *****************************************************************************/
103 static int Open ( vlc_object_t * );
104 static void Close ( vlc_object_t * );
105 static void Play ( aout_instance_t * );
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 *,
115 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
117 static void InterleaveFloat32( float *, int *, int );
118 static void InterleaveS16( int16_t *, int *, int );
120 /*****************************************************************************
122 *****************************************************************************/
124 set_description( _("Win32 waveOut extension output") );
125 set_capability( "audio output", 50 );
126 set_callbacks( Open, Close );
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 *****************************************************************************/
137 HWAVEOUT h_waveout; /* handle to waveout instance */
139 WAVEFORMATEXTENSIBLE waveformat; /* audio format */
141 WAVEHDR waveheader[FRAMES_NUM];
145 byte_t *p_silence_buffer; /* buffer we use to play silence */
147 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
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 };
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 )
171 aout_instance_t *p_aout = (aout_instance_t *)p_this;
175 /* Allocate structure */
176 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
178 if( p_aout->output.p_sys == NULL )
180 msg_Err( p_aout, "out of memory" );
184 p_aout->output.pf_play = Play;
185 p_aout->output.p_sys->pi_chan_table = NULL;
186 p_aout->b_die = VLC_FALSE;
188 if( var_Type( p_aout, "audio-device" ) == 0 )
193 if( var_Get( p_aout, "audio-device", &val ) < 0 )
195 /* Probe() has failed. */
196 free( p_aout->output.p_sys );
200 /* Open the device */
201 if( val.i_int == AOUT_VAR_SPDIF )
203 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
205 if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
206 p_aout->output.output.i_physical_channels,
207 aout_FormatNbChannels( &p_aout->output.output ),
208 p_aout->output.output.i_rate, VLC_FALSE )
211 msg_Err( p_aout, "cannot open waveout audio device" );
212 free( p_aout->output.p_sys );
216 /* Calculate the frame size in bytes */
217 p_aout->output.i_nb_samples = A52_FRAME_NB;
218 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
219 p_aout->output.output.i_frame_length = A52_FRAME_NB;
220 p_aout->output.p_sys->i_buffer_size =
221 p_aout->output.output.i_bytes_per_frame;
223 aout_VolumeNoneInit( p_aout );
227 if( val.i_int == AOUT_VAR_5_1 )
229 p_aout->output.output.i_physical_channels
230 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
231 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
234 else if( val.i_int == AOUT_VAR_2F2R )
236 p_aout->output.output.i_physical_channels
237 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
238 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
240 else if( val.i_int == AOUT_VAR_MONO )
242 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
246 p_aout->output.output.i_physical_channels
247 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
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 )
256 msg_Err( p_aout, "cannot open waveout audio device" );
257 free( p_aout->output.p_sys );
261 /* Calculate the frame size in bytes */
262 p_aout->output.i_nb_samples = FRAME_SIZE;
263 aout_FormatPrepare( &p_aout->output.output );
264 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
265 p_aout->output.output.i_bytes_per_frame;
267 aout_VolumeSoftInit( p_aout );
271 waveOutReset( p_aout->output.p_sys->h_waveout );
273 /* Allocate silence buffer */
274 p_aout->output.p_sys->p_silence_buffer =
275 malloc( p_aout->output.p_sys->i_buffer_size );
276 if( p_aout->output.p_sys->p_silence_buffer == NULL )
278 free( p_aout->output.p_sys );
279 msg_Err( p_aout, "out of memory" );
283 /* Zero the buffer. WinCE doesn't have calloc(). */
284 memset( p_aout->output.p_sys->p_silence_buffer, 0,
285 p_aout->output.p_sys->i_buffer_size );
287 /* We need to kick off the playback in order to have the callback properly
289 for( i = 0; i < FRAMES_NUM; i++ )
291 p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
293 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
294 &p_aout->output.p_sys->waveheader[0], NULL );
299 /*****************************************************************************
300 * Probe: probe the audio device for available formats and channels
301 *****************************************************************************/
302 static void Probe( aout_instance_t * p_aout )
304 vlc_value_t val, text;
306 unsigned int i_physical_channels;
308 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
309 text.psz_string = _("Audio device");
310 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
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 )
318 if( OpenWaveOutPCM( p_aout, &i_format,
319 i_physical_channels, 6,
320 p_aout->output.output.i_rate, VLC_TRUE )
323 val.i_int = AOUT_VAR_5_1;
324 text.psz_string = N_("5.1");
325 var_Change( p_aout, "audio-device",
326 VLC_VAR_ADDCHOICE, &val, &text );
327 msg_Dbg( p_aout, "device supports 5.1 channels" );
331 /* Test for 2 Front 2 Rear support */
332 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
333 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
334 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
335 == i_physical_channels )
337 if( OpenWaveOutPCM( p_aout, &i_format,
338 i_physical_channels, 4,
339 p_aout->output.output.i_rate, VLC_TRUE )
342 val.i_int = AOUT_VAR_2F2R;
343 text.psz_string = N_("2 Front 2 Rear");
344 var_Change( p_aout, "audio-device",
345 VLC_VAR_ADDCHOICE, &val, &text );
346 msg_Dbg( p_aout, "device supports 4 channels" );
350 /* Test for stereo support */
351 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
352 if( OpenWaveOutPCM( p_aout, &i_format,
353 i_physical_channels, 2,
354 p_aout->output.output.i_rate, VLC_TRUE )
357 val.i_int = AOUT_VAR_STEREO;
358 text.psz_string = N_("Stereo");
359 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
360 msg_Dbg( p_aout, "device supports 2 channels" );
363 /* Test for mono support */
364 i_physical_channels = AOUT_CHAN_CENTER;
365 if( OpenWaveOutPCM( p_aout, &i_format,
366 i_physical_channels, 1,
367 p_aout->output.output.i_rate, VLC_TRUE )
370 val.i_int = AOUT_VAR_MONO;
371 text.psz_string = N_("Mono");
372 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
373 msg_Dbg( p_aout, "device supports 1 channel" );
376 /* Test for SPDIF support */
377 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
379 if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
380 p_aout->output.output.i_physical_channels,
381 aout_FormatNbChannels( &p_aout->output.output ),
382 p_aout->output.output.i_rate, VLC_TRUE )
385 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
386 val.i_int = AOUT_VAR_SPDIF;
387 text.psz_string = N_("A/52 over S/PDIF");
388 var_Change( p_aout, "audio-device",
389 VLC_VAR_ADDCHOICE, &val, &text );
390 if( config_GetInt( p_aout, "spdif" ) )
391 var_Set( p_aout, "audio-device", val );
395 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
397 val.b_bool = VLC_TRUE;
398 var_Set( p_aout, "intf-change", val );
401 /*****************************************************************************
402 * Play: play a sound buffer
403 *****************************************************************************
404 * This doesn't actually play the buffer. This just stores the buffer so it
405 * can be played by the callback thread.
406 *****************************************************************************/
407 static void Play( aout_instance_t *p_aout )
411 /*****************************************************************************
412 * Close: close the audio device
413 *****************************************************************************/
414 static void Close( vlc_object_t *p_this )
416 aout_instance_t *p_aout = (aout_instance_t *)p_this;
418 /* Before calling waveOutClose we must reset the device */
419 p_aout->b_die = VLC_TRUE;
421 /* Wait for the waveout buffers to be freed */
425 vlc_bool_t b_not_done = VLC_FALSE;
427 for( i = 0; i < FRAMES_NUM; i++ )
429 if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
430 b_not_done = VLC_TRUE;
439 waveOutReset( p_aout->output.p_sys->h_waveout );
441 /* Close the device */
442 if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
444 msg_Err( p_aout, "waveOutClose failed" );
447 free( p_aout->output.p_sys->p_silence_buffer );
448 if( p_aout->output.p_sys->pi_chan_table )
449 free( p_aout->output.p_sys->pi_chan_table );
450 free( p_aout->output.p_sys );
453 /*****************************************************************************
454 * OpenWaveOut: open the waveout sound device
455 ****************************************************************************/
456 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
457 int i_channels, int i_nb_channels, int i_rate,
463 /* Set sound format */
465 #define waveformat p_aout->output.p_sys->waveformat
467 waveformat.dwChannelMask = 0;
468 for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
470 if( i_channels & pi_channels_in[i] )
471 waveformat.dwChannelMask |= pi_channels_out[i];
476 case VLC_FOURCC('s','p','d','i'):
478 /* To prevent channel re-ordering */
479 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
480 waveformat.Format.wBitsPerSample = 16;
481 waveformat.Samples.wValidBitsPerSample =
482 waveformat.Format.wBitsPerSample;
483 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
484 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
487 case VLC_FOURCC('f','l','3','2'):
488 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
489 waveformat.Samples.wValidBitsPerSample =
490 waveformat.Format.wBitsPerSample;
491 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
492 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
495 case VLC_FOURCC('s','1','6','l'):
496 waveformat.Format.wBitsPerSample = 16;
497 waveformat.Samples.wValidBitsPerSample =
498 waveformat.Format.wBitsPerSample;
499 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
500 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_PCM;
504 waveformat.Format.nChannels = i_nb_channels;
505 waveformat.Format.nSamplesPerSec = i_rate;
506 waveformat.Format.nBlockAlign =
507 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
508 waveformat.Format.nAvgBytesPerSec =
509 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
511 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
512 if( i_nb_channels <= 2 )
514 waveformat.Format.cbSize = 0;
518 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
519 waveformat.Format.cbSize =
520 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
523 /* Open the device */
524 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
525 (WAVEFORMATEX *)&waveformat,
526 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
527 CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
528 if( result == WAVERR_BADFORMAT )
530 msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
533 if( result == MMSYSERR_ALLOCATED )
535 msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
538 if( result != MMSYSERR_NOERROR )
540 msg_Warn( p_aout, "waveOutOpen failed" );
544 CheckReordering( p_aout, i_nb_channels );
552 /*****************************************************************************
553 * OpenWaveOutPCM: open a PCM waveout sound device
554 ****************************************************************************/
555 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
556 int i_channels, int i_nb_channels, int i_rate,
559 if( OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
560 i_channels, i_nb_channels, i_rate, b_probe )
563 if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
564 i_channels, i_nb_channels, i_rate, b_probe )
571 *i_format = VLC_FOURCC('s','1','6','l');
577 *i_format = VLC_FOURCC('f','l','3','2');
582 /*****************************************************************************
583 * CheckReordering: Check if we need to do some channel re-ordering (the ac3
584 * channel order is different from the one chosen by
586 *****************************************************************************/
587 static void CheckReordering( aout_instance_t *p_aout, int i_nb_channels )
591 #define waveformat p_aout->output.p_sys->waveformat
592 #define pi_chan_table p_aout->output.p_sys->pi_chan_table
594 p_aout->output.p_sys->b_chan_reorder = VLC_FALSE;
596 pi_chan_table = malloc( i_nb_channels * sizeof(int) );
603 i < (int)(sizeof(pi_channels_out)/sizeof(uint32_t)); i++ )
605 if( waveformat.dwChannelMask & pi_channels_out[i] )
608 pi_channels_out[i] != pi_channels_ordered[k]; k++ )
610 if( waveformat.dwChannelMask & pi_channels_ordered[k] )
616 pi_chan_table[j] = l;
622 for( i = 0; i < i_nb_channels; i++ )
624 if( pi_chan_table[i] != i )
626 p_aout->output.p_sys->b_chan_reorder = VLC_TRUE;
630 if( p_aout->output.p_sys->b_chan_reorder )
632 msg_Dbg( p_aout, "channel reordering needed" );
639 /*****************************************************************************
640 * PlayWaveOut: play a buffer through the WaveOut device
641 *****************************************************************************/
642 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
643 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
647 /* Prepare the buffer */
648 if( p_buffer != NULL )
649 p_waveheader->lpData = p_buffer->p_buffer;
651 /* Use silence buffer instead */
652 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
654 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
655 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
656 p_waveheader->dwFlags = 0;
658 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
659 if( result != MMSYSERR_NOERROR )
661 msg_Err( p_aout, "waveOutPrepareHeader failed" );
665 /* Send the buffer to the waveOut queue */
666 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
667 if( result != MMSYSERR_NOERROR )
669 msg_Err( p_aout, "waveOutWrite failed" );
676 /*****************************************************************************
677 * WaveOutCallback: what to do once WaveOut has played its sound samples
678 *****************************************************************************/
679 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
681 DWORD dwParam1, DWORD dwParam2 )
683 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
684 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
685 aout_buffer_t *p_buffer = NULL;
687 int i, i_queued_frames = 0;
689 if( uMsg != WOM_DONE ) return;
691 /* Unprepare and free the buffer which has just been played */
692 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
693 if( p_waveheader->dwUser )
694 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
696 if( p_aout->b_die ) return;
698 /* We don't want any resampling when using S/PDIF */
699 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
701 /* Find out the current latency */
702 for( i = 0; i < FRAMES_NUM; i++ )
704 /* Check if frame buf is available */
705 if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
711 /* Try to fill in as many frame buffers as possible */
712 for( i = 0; i < FRAMES_NUM; i++ )
714 /* Check if frame buf is available */
715 if( p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE )
717 /* Take into account the latency */
718 p_buffer = aout_OutputNextBuffer( p_aout,
719 mdate() + 1000000 * i_queued_frames /
720 p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
723 if( !p_buffer && i_queued_frames )
725 /* We aren't late so no need to play a blank sample */
729 /* Do the channel reordering here */
730 if( p_buffer && p_aout->output.p_sys->b_chan_reorder )
732 if( p_aout->output.output.i_format ==
733 VLC_FOURCC('s','1','6','l') )
734 InterleaveS16( (int16_t *)p_buffer->p_buffer,
735 p_aout->output.p_sys->pi_chan_table,
736 aout_FormatNbChannels( &p_aout->output.output ) );
738 InterleaveFloat32( (float *)p_buffer->p_buffer,
739 p_aout->output.p_sys->pi_chan_table,
740 aout_FormatNbChannels( &p_aout->output.output ) );
743 PlayWaveOut( p_aout, h_waveout,
744 &p_aout->output.p_sys->waveheader[i] , p_buffer );
751 /*****************************************************************************
752 * InterleaveFloat32/S16: change the channel order to the Microsoft one.
753 *****************************************************************************/
754 static void InterleaveFloat32( float *p_buf, int *pi_chan_table,
760 for( i = 0; i < FRAME_SIZE; i++ )
762 for( j = 0; j < i_nb_channels; j++ )
764 p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
767 memcpy( &p_buf[i*i_nb_channels], p_tmp,
768 i_nb_channels * sizeof(float) );
772 static void InterleaveS16( int16_t *p_buf, int *pi_chan_table,
778 for( i = 0; i < FRAME_SIZE; i++ )
780 for( j = 0; j < i_nb_channels; j++ )
782 p_tmp[pi_chan_table[j]] = p_buf[i*i_nb_channels + j];
785 memcpy( &p_buf[i*i_nb_channels], p_tmp,
786 i_nb_channels * sizeof(int16_t) );