1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.7 2003/09/12 16:26:40 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>
34 /*****************************************************************************
36 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("WAV demuxer") );
43 set_capability( "demux", 142 );
44 set_callbacks( Open, Close );
47 /*****************************************************************************
49 *****************************************************************************/
51 static int Demux ( input_thread_t * );
56 es_descriptor_t *p_es;
59 unsigned int i_data_size;
62 unsigned int i_frame_size;
63 mtime_t i_frame_length;
67 /*****************************************************************************
68 * Declaration of local function
69 *****************************************************************************/
70 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
72 static int ChunkFind( input_thread_t *, char *, unsigned int * );
74 static void FrameInfo_IMA_ADPCM( input_thread_t *, unsigned int *, mtime_t * );
75 static void FrameInfo_MS_ADPCM ( input_thread_t *, unsigned int *, mtime_t * );
76 static void FrameInfo_PCM ( input_thread_t *, unsigned int *, mtime_t * );
78 /*****************************************************************************
79 * Open: check file and initializes structures
80 *****************************************************************************/
81 static int Open( vlc_object_t * p_this )
83 input_thread_t *p_input = (input_thread_t *)p_this;
88 vlc_fourcc_t i_fourcc;
91 /* Is it a wav file ? */
92 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
94 msg_Warn( p_input, "WAV module discarded (cannot peek)" );
97 if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
99 msg_Warn( p_input, "WAV module discarded (not a valid file)" );
103 p_input->pf_demux = Demux;
104 p_input->pf_demux_control = demux_vaControlDefault;
105 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
110 /* skip riff header */
111 stream_Read( p_input->s, NULL, 12 ); /* cannot fail as peek succeed */
113 /* search fmt chunk */
114 if( ChunkFind( p_input, "fmt ", &i_size ) )
116 msg_Err( p_input, "cannot find 'fmt ' chunk" );
119 if( i_size < sizeof( WAVEFORMATEX ) - 2 ) /* XXX -2 isn't a typo */
121 msg_Err( p_input, "invalid 'fmt ' chunk" );
124 stream_Read( p_input->s, NULL, 8 ); /* cannot fail */
126 /* load waveformatex */
127 p_sys->p_wf = malloc( __EVEN( i_size ) + 2 ); /* +2, for raw audio -> no cbSize */
128 p_sys->p_wf->cbSize = 0;
129 if( stream_Read( p_input->s,
130 p_sys->p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
132 msg_Err( p_input, "cannot load 'fmt ' chunk" );
137 p_sys->p_wf->wFormatTag = GetWLE ( &p_sys->p_wf->wFormatTag );
138 p_sys->p_wf->nChannels = GetWLE ( &p_sys->p_wf->nChannels );
139 p_sys->p_wf->nSamplesPerSec = GetDWLE( &p_sys->p_wf->nSamplesPerSec );
140 p_sys->p_wf->nAvgBytesPerSec = GetDWLE( &p_sys->p_wf->nAvgBytesPerSec );
141 p_sys->p_wf->nBlockAlign = GetWLE ( &p_sys->p_wf->nBlockAlign );
142 p_sys->p_wf->wBitsPerSample = GetWLE ( &p_sys->p_wf->wBitsPerSample );
143 p_sys->p_wf->cbSize = GetWLE ( &p_sys->p_wf->cbSize );
145 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
146 p_sys->p_wf->wFormatTag,
147 p_sys->p_wf->nChannels,
148 p_sys->p_wf->nSamplesPerSec,
149 p_sys->p_wf->nAvgBytesPerSec / 1024,
150 p_sys->p_wf->nBlockAlign,
151 p_sys->p_wf->wBitsPerSample,
152 p_sys->p_wf->cbSize );
154 if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
156 msg_Err( p_input, "cannot find 'data' chunk" );
160 p_sys->i_data_pos = stream_Tell( p_input->s );
162 stream_Read( p_input->s, NULL, 8 ); /* cannot fail */
164 wf_tag_to_fourcc( p_sys->p_wf->wFormatTag, &i_fourcc, &psz_name );
165 if( i_fourcc == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
167 msg_Err( p_input,"unrecognize audio format(0x%x)",
168 p_sys->p_wf->wFormatTag );
174 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
175 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
176 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
177 FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
179 case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
180 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
182 case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
183 FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
185 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
186 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
187 /* FIXME not sure at all FIXME */
188 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
190 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
191 case VLC_FOURCC( 'a', '5', '2', ' ' ):
192 /* FIXME set end of area FIXME */
195 msg_Err( p_input, "unsupported codec (%4.4s)", (char*)&i_fourcc );
198 msg_Dbg( p_input, "found %s audio format", psz_name );
201 /* create one program */
202 vlc_mutex_lock( &p_input->stream.stream_lock );
203 if( input_InitStream( p_input, 0 ) == -1)
205 vlc_mutex_unlock( &p_input->stream.stream_lock );
206 msg_Err( p_input, "cannot init stream" );
209 if( input_AddProgram( p_input, 0, 0) == NULL )
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
212 msg_Err( p_input, "cannot add program" );
215 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
217 if( p_sys->i_data_size > 0 )
219 p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
220 (mtime_t)1000000 / 50 / p_sys->i_frame_length;
224 p_input->stream.i_mux_rate = 0;
227 p_sys->p_es = input_AddES( p_input,
228 p_input->stream.p_selected_program,
229 1, AUDIO_ES, NULL, 0 );
230 p_sys->p_es->i_stream_id = 1;
231 p_sys->p_es->i_fourcc = i_fourcc;
232 p_sys->p_es->p_waveformatex = malloc( sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
233 memcpy( p_sys->p_es->p_waveformatex,
235 sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
237 input_SelectES( p_input, p_sys->p_es );
239 p_input->stream.p_selected_program->b_is_ok = 1;
240 vlc_mutex_unlock( &p_input->stream.stream_lock );
255 /*****************************************************************************
256 * Demux: read packet and send them to decoders
257 *****************************************************************************
258 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
259 *****************************************************************************/
260 static int Demux( input_thread_t *p_input )
262 demux_sys_t *p_sys = p_input->p_demux_data;
266 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
268 i_pos = stream_Tell( p_input->s );
269 if( p_sys->p_wf->nBlockAlign != 0 )
271 i_pos += p_sys->p_wf->nBlockAlign - i_pos % p_sys->p_wf->nBlockAlign;
272 if( stream_Seek( p_input->s, i_pos ) )
274 msg_Err( p_input, "stream_Sekk failed (cannot resync)" );
279 input_ClockManageRef( p_input,
280 p_input->stream.p_selected_program,
281 p_sys->i_time * 9 / 100 );
283 i_pos = stream_Tell( p_input->s );
285 if( p_sys->i_data_size > 0 &&
286 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
292 if( ( p_pes = stream_PesPacket( p_input->s, p_sys->i_frame_size ) )==NULL )
294 msg_Warn( p_input, "cannot read data" );
298 p_pes->i_pts = input_ClockGetTS( p_input,
299 p_input->stream.p_selected_program,
300 p_sys->i_time * 9 / 100 );
302 if( !p_sys->p_es->p_decoder_fifo )
304 msg_Err( p_input, "no audio decoder" );
305 input_DeletePES( p_input->p_method_data, p_pes );
309 input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
310 p_sys->i_time += p_sys->i_frame_length;
314 /*****************************************************************************
315 * Close: frees unused data
316 *****************************************************************************/
317 static void Close ( vlc_object_t * p_this )
319 input_thread_t *p_input = (input_thread_t *)p_this;
320 demux_sys_t *p_sys = p_input->p_demux_data;
327 /*****************************************************************************
329 *****************************************************************************/
330 static int ChunkFind( input_thread_t *p_input,
331 char *fcc, unsigned int *pi_size )
339 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
341 msg_Err( p_input, "cannot peek()" );
345 i_size = GetDWLE( p_peek + 4 );
347 msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
349 if( !strncmp( p_peek, fcc, 4 ) )
358 i_size = __EVEN( i_size ) + 8;
359 if( stream_Read( p_input->s, NULL, i_size ) != i_size )
366 static void FrameInfo_PCM( input_thread_t *p_input,
367 unsigned int *pi_size,
370 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
376 /* read samples for 50ms of */
377 i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
380 *pi_length = (mtime_t)1000000 *
382 (mtime_t)p_wf->nSamplesPerSec;
384 i_bytes = i_samples * p_wf->nChannels * ( (p_wf->wBitsPerSample + 7) / 8 );
386 if( p_wf->nBlockAlign > 0 )
388 if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
390 i_bytes += p_wf->nBlockAlign - i_modulo;
396 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
397 unsigned int *pi_size,
400 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
403 i_samples = 2 + 2 * ( p_wf->nBlockAlign -
404 7 * p_wf->nChannels ) / p_wf->nChannels;
406 *pi_length = (mtime_t)1000000 *
408 (mtime_t)p_wf->nSamplesPerSec;
410 *pi_size = p_wf->nBlockAlign;
413 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
414 unsigned int *pi_size,
417 WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
420 i_samples = 2 * ( p_wf->nBlockAlign -
421 4 * p_wf->nChannels ) / p_wf->nChannels;
423 *pi_length = (mtime_t)1000000 *
425 (mtime_t)p_wf->nSamplesPerSec;
427 *pi_size = p_wf->nBlockAlign;