1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.5 2003/08/22 20:32:27 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;
94 /* Is it a wav file ? */
95 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
97 msg_Warn( p_input, "WAV module discarded (cannot peek)" );
100 if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
102 msg_Warn( p_input, "WAV module discarded (not a valid file)" );
106 p_input->pf_demux = Demux;
107 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
112 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
114 msg_Err( p_input, "cannot create stream" );
118 /* skip riff header */
119 stream_Read( p_sys->s, NULL, 12 ); /* cannot fail as peek succeed */
121 /* search fmt chunk */
122 if( ChunkFind( p_input, "fmt ", &i_size ) )
124 msg_Err( p_input, "cannot find 'fmt ' chunk" );
127 if( i_size < sizeof( WAVEFORMATEX ) - 2 ) /* XXX -2 isn't a typo */
129 msg_Err( p_input, "invalid 'fmt ' chunk" );
132 stream_Read( p_sys->s, NULL, 8 ); /* cannot fail */
134 /* load waveformatex */
135 p_sys->p_wf = malloc( __EVEN( i_size ) + 2 ); /* +2, for raw audio -> no cbSize */
136 p_sys->p_wf->cbSize = 0;
137 if( stream_Read( p_sys->s, p_sys->p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
139 msg_Err( p_input, "cannot load 'fmt ' chunk" );
144 p_sys->p_wf->wFormatTag = GetWLE ( &p_sys->p_wf->wFormatTag );
145 p_sys->p_wf->nChannels = GetWLE ( &p_sys->p_wf->nChannels );
146 p_sys->p_wf->nSamplesPerSec = GetDWLE( &p_sys->p_wf->nSamplesPerSec );
147 p_sys->p_wf->nAvgBytesPerSec = GetDWLE( &p_sys->p_wf->nAvgBytesPerSec );
148 p_sys->p_wf->nBlockAlign = GetWLE ( &p_sys->p_wf->nBlockAlign );
149 p_sys->p_wf->wBitsPerSample = GetWLE ( &p_sys->p_wf->wBitsPerSample );
150 p_sys->p_wf->cbSize = GetWLE ( &p_sys->p_wf->cbSize );
152 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
153 p_sys->p_wf->wFormatTag,
154 p_sys->p_wf->nChannels,
155 p_sys->p_wf->nSamplesPerSec,
156 p_sys->p_wf->nAvgBytesPerSec / 1024,
157 p_sys->p_wf->nBlockAlign,
158 p_sys->p_wf->wBitsPerSample,
159 p_sys->p_wf->cbSize );
161 if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
163 msg_Err( p_input, "cannot find 'data' chunk" );
167 p_sys->i_data_pos = stream_Tell( p_sys->s );
169 stream_Read( p_sys->s, NULL, 8 ); /* cannot fail */
171 wf_tag_to_fourcc( p_sys->p_wf->wFormatTag, &i_fourcc, &psz_name );
172 if( i_fourcc == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
174 msg_Err( p_input,"unrecognize audio format(0x%x)",
175 p_sys->p_wf->wFormatTag );
181 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
182 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
183 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
184 FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
186 case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
187 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
189 case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
190 FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
192 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
193 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
194 /* FIXME not sure at all FIXME */
195 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
197 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
198 case VLC_FOURCC( 'a', '5', '2', ' ' ):
199 /* FIXME set end of area FIXME */
202 msg_Err( p_input, "unsupported codec (%4.4s)", (char*)&i_fourcc );
205 msg_Dbg( p_input, "found %s audio format", psz_name );
208 /* create one program */
209 vlc_mutex_lock( &p_input->stream.stream_lock );
210 if( input_InitStream( p_input, 0 ) == -1)
212 vlc_mutex_unlock( &p_input->stream.stream_lock );
213 msg_Err( p_input, "cannot init stream" );
216 if( input_AddProgram( p_input, 0, 0) == NULL )
218 vlc_mutex_unlock( &p_input->stream.stream_lock );
219 msg_Err( p_input, "cannot add program" );
222 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
224 if( p_sys->i_data_size > 0 )
226 p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
227 (mtime_t)1000000 / 50 / p_sys->i_frame_length;
231 p_input->stream.i_mux_rate = 0;
234 p_sys->p_es = input_AddES( p_input,
235 p_input->stream.p_selected_program,
236 1, AUDIO_ES, NULL, 0 );
237 p_sys->p_es->i_stream_id = 1;
238 p_sys->p_es->i_fourcc = i_fourcc;
239 p_sys->p_es->p_waveformatex = malloc( sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
240 memcpy( p_sys->p_es->p_waveformatex,
242 sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
244 input_SelectES( p_input, p_sys->p_es );
246 p_input->stream.p_selected_program->b_is_ok = 1;
247 vlc_mutex_unlock( &p_input->stream.stream_lock );
259 stream_Release( p_sys->s );
266 /*****************************************************************************
267 * Demux: read packet and send them to decoders
268 *****************************************************************************
269 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
270 *****************************************************************************/
271 static int Demux( input_thread_t *p_input )
273 demux_sys_t *p_sys = p_input->p_demux_data;
277 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
279 i_pos = stream_Tell( p_sys->s );
280 if( p_sys->p_wf->nBlockAlign != 0 )
282 i_pos += p_sys->p_wf->nBlockAlign - i_pos % p_sys->p_wf->nBlockAlign;
283 if( stream_Seek( p_sys->s, i_pos ) )
285 msg_Err( p_input, "stream_Sekk failed (cannot resync)" );
290 input_ClockManageRef( p_input,
291 p_input->stream.p_selected_program,
292 p_sys->i_time * 9 / 100 );
294 i_pos = stream_Tell( p_sys->s );
296 if( p_sys->i_data_size > 0 &&
297 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
303 if( ( p_pes = stream_PesPacket( p_sys->s, p_sys->i_frame_size ) ) == NULL )
305 msg_Warn( p_input, "cannot read data" );
309 p_pes->i_pts = input_ClockGetTS( p_input,
310 p_input->stream.p_selected_program,
311 p_sys->i_time * 9 / 100 );
313 if( !p_sys->p_es->p_decoder_fifo )
315 msg_Err( p_input, "no audio decoder" );
316 input_DeletePES( p_input->p_method_data, p_pes );
320 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
321 p_sys->i_time += p_sys->i_frame_length;
325 /*****************************************************************************
326 * Close: frees unused data
327 *****************************************************************************/
328 static void Close ( vlc_object_t * p_this )
330 input_thread_t *p_input = (input_thread_t *)p_this;
331 demux_sys_t *p_sys = p_input->p_demux_data;
333 stream_Release( p_sys->s );
339 /*****************************************************************************
341 *****************************************************************************/
342 static int ChunkFind( input_thread_t *p_input,
343 char *fcc, unsigned int *pi_size )
345 demux_sys_t *p_sys = p_input->p_demux_data;
352 if( stream_Peek( p_sys->s, &p_peek, 8 ) < 8 )
354 msg_Err( p_input, "cannot peek()" );
358 i_size = GetDWLE( p_peek + 4 );
360 msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
362 if( !strncmp( p_peek, fcc, 4 ) )
371 i_size = __EVEN( i_size ) + 8;
372 if( stream_Read( p_sys->s, NULL, i_size ) != i_size )
379 static void FrameInfo_PCM( input_thread_t *p_input,
380 unsigned int *pi_size,
383 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
389 /* read samples for 50ms of */
390 i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
393 *pi_length = (mtime_t)1000000 *
395 (mtime_t)p_wf->nSamplesPerSec;
397 i_bytes = i_samples * p_wf->nChannels * ( (p_wf->wBitsPerSample + 7) / 8 );
399 if( p_wf->nBlockAlign > 0 )
401 if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
403 i_bytes += p_wf->nBlockAlign - i_modulo;
409 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
410 unsigned int *pi_size,
413 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
416 i_samples = 2 + 2 * ( p_wf->nBlockAlign -
417 7 * p_wf->nChannels ) / p_wf->nChannels;
419 *pi_length = (mtime_t)1000000 *
421 (mtime_t)p_wf->nSamplesPerSec;
423 *pi_size = p_wf->nBlockAlign;
426 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
427 unsigned int *pi_size,
430 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
433 i_samples = 2 * ( p_wf->nBlockAlign -
434 4 * p_wf->nChannels ) / p_wf->nChannels;
436 *pi_length = (mtime_t)1000000 *
438 (mtime_t)p_wf->nSamplesPerSec;
440 *pi_size = p_wf->nBlockAlign;