1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: wav.c,v 1.10 2003/11/16 22:54:12 gbazin 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 * );
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;
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 ) );
109 /* skip riff header */
110 stream_Read( p_input->s, NULL, 12 ); /* cannot fail as peek succeed */
112 /* search fmt chunk */
113 if( ChunkFind( p_input, "fmt ", &i_size ) )
115 msg_Err( p_input, "cannot find 'fmt ' chunk" );
118 if( i_size < sizeof( WAVEFORMATEX ) - 2 ) /* XXX -2 isn't a typo */
120 msg_Err( p_input, "invalid 'fmt ' chunk" );
123 stream_Read( p_input->s, NULL, 8 ); /* cannot fail */
125 /* load waveformatex */
126 p_wf = malloc( __EVEN( i_size ) + 2 ); /* +2, for raw audio -> no cbSize */
128 if( stream_Read( p_input->s,
129 p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
131 msg_Err( p_input, "cannot load 'fmt ' chunk" );
135 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
136 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec, &psz_name );
137 p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
138 p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
139 p_sys->fmt.audio.i_blockalign = GetWLE ( &p_wf->nBlockAlign );
140 p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
141 p_sys->fmt.audio.i_bitspersample = GetWLE ( &p_wf->wBitsPerSample );;
143 p_sys->fmt.i_extra = GetWLE ( &p_wf->cbSize );
144 if( p_sys->fmt.i_extra > 0 )
146 p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
147 memcpy( p_sys->fmt.p_extra, &p_wf[1], p_sys->fmt.i_extra );
150 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
151 GetWLE( &p_wf->wFormatTag ),
152 p_sys->fmt.audio.i_channels,
153 p_sys->fmt.audio.i_rate,
154 p_sys->fmt.i_bitrate / 8 / 1024,
155 p_sys->fmt.audio.i_blockalign,
156 p_sys->fmt.audio.i_bitspersample,
157 p_sys->fmt.i_extra );
160 switch( p_sys->fmt.i_codec )
162 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
163 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
164 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
165 FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
167 case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
168 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
170 case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
171 FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
173 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
174 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
175 /* FIXME not sure at all FIXME */
176 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
178 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
179 case VLC_FOURCC( 'a', '5', '2', ' ' ):
180 /* FIXME set end of area FIXME */
183 msg_Err( p_input, "unsupported codec (%4.4s)", (char*)&p_sys->fmt.i_codec );
187 msg_Dbg( p_input, "found %s audio format", psz_name );
189 if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
191 msg_Err( p_input, "cannot find 'data' chunk" );
195 p_sys->i_data_pos = stream_Tell( p_input->s );
197 stream_Read( p_input->s, NULL, 8 ); /* cannot fail */
199 /* create one program */
200 vlc_mutex_lock( &p_input->stream.stream_lock );
201 if( input_InitStream( p_input, 0 ) == -1)
203 vlc_mutex_unlock( &p_input->stream.stream_lock );
204 msg_Err( p_input, "cannot init stream" );
207 p_input->stream.i_mux_rate = 0;
208 if( p_sys->i_data_size > 0 )
210 p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
211 (mtime_t)1000000 / 50 / p_sys->i_frame_length;
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
215 p_sys->p_es = es_out_Add( p_input->p_es_out, &p_sys->fmt );
224 /*****************************************************************************
225 * Demux: read packet and send them to decoders
226 *****************************************************************************
227 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
228 *****************************************************************************/
229 static int Demux( input_thread_t *p_input )
231 demux_sys_t *p_sys = p_input->p_demux_data;
235 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
237 i_pos = stream_Tell( p_input->s );
238 if( p_sys->fmt.audio.i_blockalign != 0 )
240 i_pos += p_sys->fmt.audio.i_blockalign - i_pos % p_sys->fmt.audio.i_blockalign;
241 if( stream_Seek( p_input->s, i_pos ) )
243 msg_Err( p_input, "stream_Sekk failed (cannot resync)" );
248 input_ClockManageRef( p_input,
249 p_input->stream.p_selected_program,
250 p_sys->i_time * 9 / 100 );
252 i_pos = stream_Tell( p_input->s );
254 if( p_sys->i_data_size > 0 &&
255 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
261 if( ( p_pes = stream_PesPacket( p_input->s, p_sys->i_frame_size ) )==NULL )
263 msg_Warn( p_input, "cannot read data" );
267 p_pes->i_pts = input_ClockGetTS( p_input,
268 p_input->stream.p_selected_program,
269 p_sys->i_time * 9 / 100 );
271 es_out_Send( p_input->p_es_out, p_sys->p_es, p_pes );
273 p_sys->i_time += p_sys->i_frame_length;
278 /*****************************************************************************
279 * Close: frees unused data
280 *****************************************************************************/
281 static void Close ( vlc_object_t * p_this )
283 input_thread_t *p_input = (input_thread_t *)p_this;
284 demux_sys_t *p_sys = p_input->p_demux_data;
290 /*****************************************************************************
292 *****************************************************************************/
293 static int ChunkFind( input_thread_t *p_input,
294 char *fcc, unsigned int *pi_size )
302 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
304 msg_Err( p_input, "cannot peek()" );
308 i_size = GetDWLE( p_peek + 4 );
310 msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
312 if( !strncmp( p_peek, fcc, 4 ) )
321 i_size = __EVEN( i_size ) + 8;
322 if( stream_Read( p_input->s, NULL, i_size ) != i_size )
329 static void FrameInfo_PCM( input_thread_t *p_input,
330 unsigned int *pi_size,
333 demux_sys_t *p_sys = p_input->p_demux_data;
340 /* read samples for 50ms of */
341 i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
344 *pi_length = (mtime_t)1000000 *
346 (mtime_t)p_sys->fmt.audio.i_rate;
348 i_bytes = i_samples * p_sys->fmt.audio.i_channels * ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
350 if( p_sys->fmt.audio.i_blockalign > 0 )
352 if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
354 i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
360 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
361 unsigned int *pi_size,
364 demux_sys_t *p_sys = p_input->p_demux_data;
368 i_samples = 2 + 2 * ( p_sys->fmt.audio.i_blockalign -
369 7 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
371 *pi_length = (mtime_t)1000000 *
373 (mtime_t)p_sys->fmt.audio.i_rate;
375 *pi_size = p_sys->fmt.audio.i_blockalign;
378 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
379 unsigned int *pi_size,
382 demux_sys_t *p_sys = p_input->p_demux_data;
386 i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
387 4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
389 *pi_length = (mtime_t)1000000 *
391 (mtime_t)p_sys->fmt.audio.i_rate;
393 *pi_size = p_sys->fmt.audio.i_blockalign;