1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.3 2003/08/17 23:02:52 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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 <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_description( _("WAV demuxer") );
44 set_capability( "demux", 142 );
45 set_callbacks( Open, Close );
48 /*****************************************************************************
50 *****************************************************************************/
52 static int Demux ( input_thread_t * );
59 es_descriptor_t *p_es;
62 unsigned int i_data_size;
65 unsigned int i_frame_size;
66 mtime_t i_frame_length;
70 /*****************************************************************************
71 * Declaration of local function
72 *****************************************************************************/
73 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
75 static int ChunkFind( input_thread_t *, char *, unsigned int * );
77 static void FrameInfo_IMA_ADPCM( input_thread_t *, unsigned int *, mtime_t * );
78 static void FrameInfo_MS_ADPCM ( input_thread_t *, unsigned int *, mtime_t * );
79 static void FrameInfo_PCM ( input_thread_t *, unsigned int *, mtime_t * );
81 /*****************************************************************************
82 * Open: check file and initializes structures
83 *****************************************************************************/
84 static int Open( vlc_object_t * p_this )
86 input_thread_t *p_input = (input_thread_t *)p_this;
91 vlc_fourcc_t i_fourcc;
93 /* Is it a wav file ? */
94 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
96 msg_Warn( p_input, "WAV module discarded (cannot peek)" );
99 if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
101 msg_Warn( p_input, "WAV module discarded (not a valid file)" );
105 p_input->pf_demux = Demux;
106 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
111 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
113 msg_Err( p_input, "cannot create stream" );
117 /* skip riff header */
118 stream_Read( p_sys->s, NULL, 12 ); /* cannot fail as peek succeed */
120 /* search fmt chunk */
121 if( ChunkFind( p_input, "fmt ", &i_size ) )
123 msg_Err( p_input, "cannot find 'fmt ' chunk" );
126 if( i_size < sizeof( WAVEFORMATEX ) - 2 ) /* XXX -2 isn't a typo */
128 msg_Err( p_input, "invalid 'fmt ' chunk" );
131 stream_Read( p_sys->s, NULL, 8 ); /* cannot fail */
133 /* load waveformatex */
134 p_sys->p_wf = malloc( __EVEN( i_size ) + 2 ); /* +2, for raw audio -> no cbSize */
135 p_sys->p_wf->cbSize = 0;
136 if( stream_Read( p_sys->s, p_sys->p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
138 msg_Err( p_input, "cannot load 'fmt ' chunk" );
143 p_sys->p_wf->wFormatTag = GetWLE ( &p_sys->p_wf->wFormatTag );
144 p_sys->p_wf->nChannels = GetWLE ( &p_sys->p_wf->nChannels );
145 p_sys->p_wf->nSamplesPerSec = GetDWLE( &p_sys->p_wf->nSamplesPerSec );
146 p_sys->p_wf->nAvgBytesPerSec = GetDWLE( &p_sys->p_wf->nAvgBytesPerSec );
147 p_sys->p_wf->nBlockAlign = GetWLE ( &p_sys->p_wf->nBlockAlign );
148 p_sys->p_wf->wBitsPerSample = GetWLE ( &p_sys->p_wf->wBitsPerSample );
149 p_sys->p_wf->cbSize = GetWLE ( &p_sys->p_wf->cbSize );
151 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
152 p_sys->p_wf->wFormatTag,
153 p_sys->p_wf->nChannels,
154 p_sys->p_wf->nSamplesPerSec,
155 p_sys->p_wf->nAvgBytesPerSec / 1024,
156 p_sys->p_wf->nBlockAlign,
157 p_sys->p_wf->wBitsPerSample,
158 p_sys->p_wf->cbSize );
160 if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
162 msg_Err( p_input, "cannot find 'data' chunk" );
166 stream_Control( p_sys->s, STREAM_GET_POSITION, &p_sys->i_data_pos );
168 stream_Read( p_sys->s, NULL, 8 ); /* cannot fail */
170 /* XXX p_sys->psz_demux shouldn't be NULL ! */
171 switch( p_sys->p_wf->wFormatTag )
173 case( WAVE_FORMAT_PCM ):
174 msg_Dbg( p_input,"found raw pcm audio format" );
175 i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
176 FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
178 case( WAVE_FORMAT_MULAW ):
179 msg_Dbg( p_input,"found mulaw pcm audio format" );
180 i_fourcc = VLC_FOURCC( 'u', 'l', 'a', 'w' );
181 FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
183 case( WAVE_FORMAT_ALAW ):
184 msg_Dbg( p_input,"found alaw pcm audio format" );
185 i_fourcc = VLC_FOURCC( 'a', 'l', 'a', 'w' );
186 FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
188 case( WAVE_FORMAT_ADPCM ):
189 msg_Dbg( p_input, "found ms adpcm audio format" );
190 i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x02 );
191 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
193 case( WAVE_FORMAT_IMA_ADPCM ):
194 msg_Dbg( p_input, "found ima adpcm audio format" );
195 i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x11 );
196 FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
199 case( WAVE_FORMAT_MPEG ):
200 case( WAVE_FORMAT_MPEGLAYER3 ):
201 msg_Dbg( p_input, "found mpeg audio format (relaying to another demux)" );
202 /* FIXME set end of area FIXME */
204 case( WAVE_FORMAT_A52 ):
205 msg_Dbg( p_input,"found a52 audio format (relaying to another demux)" );
206 /* FIXME set end of area FIXME */
210 msg_Err( p_input,"unrecognize audio format(0x%x)",
211 p_sys->p_wf->wFormatTag );
216 /* create one program */
217 vlc_mutex_lock( &p_input->stream.stream_lock );
218 if( input_InitStream( p_input, 0 ) == -1)
220 vlc_mutex_unlock( &p_input->stream.stream_lock );
221 msg_Err( p_input, "cannot init stream" );
224 if( input_AddProgram( p_input, 0, 0) == NULL )
226 vlc_mutex_unlock( &p_input->stream.stream_lock );
227 msg_Err( p_input, "cannot add program" );
230 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
232 if( p_sys->i_data_size > 0 )
234 p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size * (mtime_t)1000000 / 50 / p_sys->i_frame_length;
238 p_input->stream.i_mux_rate = 0;
241 p_sys->p_es = input_AddES( p_input,
242 p_input->stream.p_selected_program,
243 1, AUDIO_ES, NULL, 0 );
244 p_sys->p_es->i_stream_id = 1;
245 p_sys->p_es->i_fourcc = i_fourcc;
246 p_sys->p_es->p_waveformatex = malloc( sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
247 memcpy( p_sys->p_es->p_waveformatex,
249 sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
251 input_SelectES( p_input, p_sys->p_es );
253 p_input->stream.p_selected_program->b_is_ok = 1;
254 vlc_mutex_unlock( &p_input->stream.stream_lock );
266 stream_Release( p_sys->s );
273 /*****************************************************************************
274 * Demux: read packet and send them to decoders
275 *****************************************************************************
276 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
277 *****************************************************************************/
278 static int Demux( input_thread_t *p_input )
280 demux_sys_t *p_sys = p_input->p_demux_data;
284 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
286 stream_Control( p_sys->s, STREAM_GET_POSITION, &i_pos );
287 if( p_sys->p_wf->nBlockAlign != 0 )
289 i_pos += p_sys->p_wf->nBlockAlign - i_pos % p_sys->p_wf->nBlockAlign;
290 if( stream_Control( p_sys->s, STREAM_SET_POSITION, i_pos ) )
292 msg_Err( p_input, "STREAM_SET_POSITION failed (cannot resync)" );
297 input_ClockManageRef( p_input,
298 p_input->stream.p_selected_program,
299 p_sys->i_time * 9 / 100 );
301 stream_Control( p_sys->s, STREAM_GET_POSITION, &i_pos );
303 if( p_sys->i_data_size > 0 &&
304 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
310 if( ( p_pes = stream_PesPacket( p_sys->s, p_sys->i_frame_size ) ) == NULL )
312 msg_Warn( p_input, "cannot read data" );
316 p_pes->i_pts = input_ClockGetTS( p_input,
317 p_input->stream.p_selected_program,
318 p_sys->i_time * 9 / 100 );
320 if( !p_sys->p_es->p_decoder_fifo )
322 msg_Err( p_input, "no audio decoder" );
323 input_DeletePES( p_input->p_method_data, p_pes );
327 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
328 p_sys->i_time += p_sys->i_frame_length;
332 /*****************************************************************************
333 * Close: frees unused data
334 *****************************************************************************/
335 static void Close ( vlc_object_t * p_this )
337 input_thread_t *p_input = (input_thread_t *)p_this;
338 demux_sys_t *p_sys = p_input->p_demux_data;
340 stream_Release( p_sys->s );
346 /*****************************************************************************
348 *****************************************************************************/
349 static int ChunkFind( input_thread_t *p_input,
350 char *fcc, unsigned int *pi_size )
352 demux_sys_t *p_sys = p_input->p_demux_data;
359 if( stream_Peek( p_sys->s, &p_peek, 8 ) < 8 )
361 msg_Err( p_input, "cannot peek()" );
365 i_size = GetDWLE( p_peek + 4 );
367 msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
369 if( !strncmp( p_peek, fcc, 4 ) )
378 i_size = __EVEN( i_size ) + 8;
379 if( stream_Read( p_sys->s, NULL, i_size ) != i_size )
386 static void FrameInfo_PCM( input_thread_t *p_input,
387 unsigned int *pi_size,
390 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
396 /* read samples for 50ms of */
397 i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
400 *pi_length = (mtime_t)1000000 *
402 (mtime_t)p_wf->nSamplesPerSec;
404 i_bytes = i_samples * p_wf->nChannels * ( (p_wf->wBitsPerSample + 7) / 8 );
406 if( p_wf->nBlockAlign > 0 )
408 if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
410 i_bytes += p_wf->nBlockAlign - i_modulo;
416 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
417 unsigned int *pi_size,
420 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
423 i_samples = 2 + 2 * ( p_wf->nBlockAlign -
424 7 * p_wf->nChannels ) / p_wf->nChannels;
426 *pi_length = (mtime_t)1000000 *
428 (mtime_t)p_wf->nSamplesPerSec;
430 *pi_size = p_wf->nBlockAlign;
433 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
434 unsigned int *pi_size,
437 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
440 i_samples = 2 * ( p_wf->nBlockAlign -
441 4 * p_wf->nChannels ) / p_wf->nChannels;
443 *pi_length = (mtime_t)1000000 *
445 (mtime_t)p_wf->nSamplesPerSec;
447 *pi_size = p_wf->nBlockAlign;