1 /*****************************************************************************
2 * wav.c: wav muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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 *****************************************************************************/
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("WAV muxer") );
43 set_capability( "sout mux", 5 );
44 set_category( CAT_SOUT );
45 set_subcategory( SUBCAT_SOUT_MUX );
46 set_callbacks( Open, Close );
47 add_shortcut( "wav" );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Control ( sout_mux_t *, int, va_list );
54 static int AddStream( sout_mux_t *, sout_input_t * );
55 static int DelStream( sout_mux_t *, sout_input_t * );
56 static int Mux ( sout_mux_t * );
58 #define MAX_CHANNELS 6
68 /* Wave header for the output data */
69 uint32_t waveheader[5];
70 WAVEFORMATEXTENSIBLE waveformat;
71 uint32_t waveheader2[2];
73 uint32_t i_channel_mask;
74 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
75 int pi_chan_table[AOUT_CHAN_MAX];
79 static const uint32_t pi_channels_src[] =
80 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
81 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
82 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
83 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
84 static const uint32_t pi_channels_in[] =
85 { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
86 WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT,
87 WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT,
88 WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY, 0 };
89 static const uint32_t pi_channels_out[] =
90 { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
91 WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY,
92 WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT,
93 WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT, 0 };
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 sout_mux_t *p_mux = (sout_mux_t*)p_this;
101 sout_mux_sys_t *p_sys;
103 p_mux->pf_control = Control;
104 p_mux->pf_addstream = AddStream;
105 p_mux->pf_delstream = DelStream;
108 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
109 p_sys->b_used = VLC_FALSE;
110 p_sys->b_header = VLC_TRUE;
113 p_sys->b_chan_reorder = 0;
118 /*****************************************************************************
120 *****************************************************************************/
121 static void Close( vlc_object_t * p_this )
123 sout_mux_t *p_mux = (sout_mux_t*)p_this;
124 sout_mux_sys_t *p_sys = p_mux->p_sys;
128 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
135 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
136 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
137 *pb_bool = VLC_FALSE;
140 case MUX_GET_ADD_STREAM_WAIT:
141 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
146 ppsz = (char**)va_arg( args, char ** );
147 *ppsz = strdup( "audio/wav" );
154 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
156 GUID subformat_guid = {0, 0, 0x10,{0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71}};
157 sout_mux_sys_t *p_sys = p_mux->p_sys;
158 WAVEFORMATEX *p_waveformat = &p_sys->waveformat.Format;
159 int i_bytes_per_sample, i_format;
162 if( p_input->p_fmt->i_cat != AUDIO_ES )
164 msg_Dbg( p_mux, "not an audio stream" );
170 msg_Dbg( p_mux, "can't add more than 1 stream" );
174 msg_Dbg( p_mux, "adding input %i channels, %iHz",
175 p_input->p_fmt->audio.i_channels,
176 p_input->p_fmt->audio.i_rate );
178 p_sys->i_channel_mask = 0;
179 if( p_input->p_fmt->audio.i_physical_channels )
183 for( i = 0; i < sizeof(pi_channels_in)/sizeof(uint32_t); i++ )
185 if( p_input->p_fmt->audio.i_physical_channels & pi_channels_src[i])
186 p_sys->i_channel_mask |= pi_channels_in[i];
189 p_sys->b_chan_reorder =
190 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
191 p_sys->i_channel_mask,
192 p_input->p_fmt->audio.i_channels,
193 p_sys->pi_chan_table );
195 msg_Dbg( p_mux, "channel mask: %x, reordering: %i",
196 p_sys->i_channel_mask, (int)p_sys->b_chan_reorder );
199 i_format = p_input->p_fmt->i_codec == VLC_FOURCC('f', 'l', '3', '2') ?
200 WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
201 b_ext = p_sys->b_ext = p_input->p_fmt->audio.i_channels > 2;
203 /* Build a WAV header for the output data */
204 p_sys->waveheader[0] = VLC_FOURCC('R', 'I', 'F', 'F'); /* MainChunkID */
205 SetDWLE( &p_sys->waveheader[1], 0 ); /* Length */
206 p_sys->waveheader[2] = VLC_FOURCC('W', 'A', 'V', 'E'); /* ChunkTypeID */
207 p_sys->waveheader[3] = VLC_FOURCC('f', 'm', 't', ' '); /* SubChunkID */
208 SetDWLE( &p_sys->waveheader[4], b_ext ? 40 : 16 ); /* SubChunkLength */
210 p_sys->waveheader2[0] = VLC_FOURCC('d', 'a', 't', 'a'); /* DataChunkID */
211 SetDWLE( &p_sys->waveheader2[1], 0 ); /* DataLength */
213 /* Build a WAVEVFORMAT header for the output data */
214 memset( &p_sys->waveformat, 0, sizeof(WAVEFORMATEXTENSIBLE) );
215 SetWLE( &p_waveformat->wFormatTag,
216 b_ext ? WAVE_FORMAT_EXTENSIBLE : i_format );
217 SetWLE( &p_waveformat->nChannels,
218 p_input->p_fmt->audio.i_channels );
219 SetDWLE( &p_waveformat->nSamplesPerSec, p_input->p_fmt->audio.i_rate );
220 i_bytes_per_sample = p_input->p_fmt->audio.i_channels *
221 p_input->p_fmt->audio.i_bitspersample / 8;
222 SetDWLE( &p_waveformat->nAvgBytesPerSec,
223 i_bytes_per_sample * p_input->p_fmt->audio.i_rate );
224 SetWLE( &p_waveformat->nBlockAlign, i_bytes_per_sample );
225 SetWLE( &p_waveformat->wBitsPerSample,
226 p_input->p_fmt->audio.i_bitspersample );
227 SetWLE( &p_waveformat->cbSize, 22 );
228 SetWLE( &p_sys->waveformat.Samples.wValidBitsPerSample,
229 p_input->p_fmt->audio.i_bitspersample );
230 SetDWLE( &p_sys->waveformat.dwChannelMask,
231 p_sys->i_channel_mask );
232 p_sys->waveformat.SubFormat = subformat_guid;
233 p_sys->waveformat.SubFormat.Data1 = i_format;
236 p_sys->b_used = VLC_TRUE;
241 static block_t *GetHeader( sout_mux_t *p_mux )
243 sout_mux_sys_t *p_sys = p_mux->p_sys;
245 block_New( p_mux, sizeof( WAVEFORMATEXTENSIBLE ) + 7 * 4 );
247 SetDWLE( &p_sys->waveheader[1],
248 20 + (p_sys->b_ext ? 40 : 16) + p_sys->i_data ); /* Length */
249 SetDWLE( &p_sys->waveheader2[1], p_sys->i_data ); /* DataLength */
251 memcpy( p_block->p_buffer, &p_sys->waveheader, 5 * 4 );
252 memcpy( p_block->p_buffer + 5 * 4, &p_sys->waveformat,
253 sizeof( WAVEFORMATEXTENSIBLE ) );
254 memcpy( p_block->p_buffer + 5 * 4 +
255 (p_sys->b_ext ? sizeof( WAVEFORMATEXTENSIBLE ) : 16),
256 &p_sys->waveheader2, 2 * 4 );
257 if( !p_sys->b_ext ) p_block->i_buffer -= 24;
261 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
263 msg_Dbg( p_mux, "removing input" );
265 msg_Dbg( p_mux, "writing header data" );
266 if( !sout_AccessOutSeek( p_mux->p_access, 0 ) )
268 sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
274 static int Mux( sout_mux_t *p_mux )
276 sout_mux_sys_t *p_sys = p_mux->p_sys;
277 sout_input_t *p_input;
279 if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
281 if( p_sys->b_header )
283 msg_Dbg( p_mux, "writing header data" );
284 sout_AccessOutWrite( p_mux->p_access, GetHeader( p_mux ) );
286 p_sys->b_header = VLC_FALSE;
288 p_input = p_mux->pp_inputs[0];
289 while( p_input->p_fifo->i_depth > 0 )
291 block_t *p_block = block_FifoGet( p_input->p_fifo );
292 p_sys->i_data += p_block->i_buffer;
294 /* Do the channel reordering */
295 if( p_sys->b_chan_reorder )
296 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
297 p_input->p_fmt->audio.i_channels,
298 p_sys->pi_chan_table,
299 p_input->p_fmt->audio.i_bitspersample );
301 sout_AccessOutWrite( p_mux->p_access, p_block );