1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: wav.c,v 1.14 2004/02/23 23:01:05 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 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
41 set_description( _("WAV demuxer") );
42 set_capability( "demux", 142 );
43 set_callbacks( Open, Close );
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Demux( input_thread_t * );
57 unsigned int i_data_size;
60 unsigned int i_frame_size;
61 mtime_t i_frame_length;
64 /*****************************************************************************
65 * Declaration of local function
66 *****************************************************************************/
67 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
69 static int ChunkFind( input_thread_t *, char *, unsigned int * );
71 static void FrameInfo_IMA_ADPCM( input_thread_t *, unsigned int *, mtime_t * );
72 static void FrameInfo_MS_ADPCM ( input_thread_t *, unsigned int *, mtime_t * );
73 static void FrameInfo_PCM ( input_thread_t *, unsigned int *, mtime_t * );
75 /*****************************************************************************
76 * Open: check file and initializes structures
77 *****************************************************************************/
78 static int Open( vlc_object_t * p_this )
80 input_thread_t *p_input = (input_thread_t *)p_this;
84 unsigned int i_size, i_extended;
87 WAVEFORMATEXTENSIBLE *p_wf_ext;
90 /* Is it a wav file ? */
91 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
93 msg_Warn( p_input, "WAV module discarded (cannot peek)" );
96 if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
101 p_input->pf_demux = Demux;
102 p_input->pf_demux_control = demux_vaControlDefault;
103 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
107 /* skip riff header */
108 stream_Read( p_input->s, NULL, 12 ); /* cannot fail as peek succeed */
110 /* search fmt chunk */
111 if( ChunkFind( p_input, "fmt ", &i_size ) )
113 msg_Err( p_input, "cannot find 'fmt ' chunk" );
116 if( i_size < sizeof( WAVEFORMATEX ) - 2 ) /* XXX -2 isn't a typo */
118 msg_Err( p_input, "invalid 'fmt ' chunk" );
121 stream_Read( p_input->s, NULL, 8 ); /* Cannot fail */
123 /* load waveformatex */
124 p_wf = (WAVEFORMATEX *)p_wf_ext = malloc( __EVEN( i_size ) + 2 );
126 if( stream_Read( p_input->s,
127 p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
129 msg_Err( p_input, "cannot load 'fmt ' chunk" );
133 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
134 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
136 p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
137 p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
138 p_sys->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );
139 p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
140 p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
141 p_sys->fmt.i_extra = GetWLE( &p_wf->cbSize );
144 /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
145 if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
146 i_size >= sizeof( WAVEFORMATEXTENSIBLE ) )
148 wf_tag_to_fourcc( GetWLE( &p_wf_ext->SubFormat ),
149 &p_sys->fmt.i_codec, &psz_name );
150 i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
151 p_sys->fmt.i_extra -= i_extended;
154 if( p_sys->fmt.i_extra > 0 )
156 p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
157 memcpy( p_sys->fmt.p_extra, ((uint8_t *)p_wf) + i_extended,
158 p_sys->fmt.i_extra );
161 msg_Dbg( p_input, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
162 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, bits/samples: %d, "
164 GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
165 p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
166 p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
167 p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
171 switch( p_sys->fmt.i_codec )
173 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
174 case VLC_FOURCC( 'a', 'f', 'l', 't' ):
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,
181 &p_sys->i_frame_length );
183 case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
184 FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size,
185 &p_sys->i_frame_length );
187 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
188 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
189 /* FIXME not sure at all FIXME */
190 FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size,
191 &p_sys->i_frame_length );
193 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
194 case VLC_FOURCC( 'a', '5', '2', ' ' ):
195 /* FIXME set end of area FIXME */
198 msg_Err( p_input, "unsupported codec (%4.4s)",
199 (char*)&p_sys->fmt.i_codec );
203 msg_Dbg( p_input, "found %s audio format", psz_name );
205 if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
207 msg_Err( p_input, "cannot find 'data' chunk" );
210 stream_Read( p_input->s, NULL, 8 ); /* Cannot fail */
211 p_sys->i_data_pos = stream_Tell( p_input->s );
213 /* Create one program */
214 vlc_mutex_lock( &p_input->stream.stream_lock );
215 if( input_InitStream( p_input, 0 ) == -1)
217 vlc_mutex_unlock( &p_input->stream.stream_lock );
218 msg_Err( p_input, "cannot init stream" );
222 p_input->stream.i_mux_rate = 0;
223 if( p_sys->fmt.i_bitrate )
225 p_input->stream.i_mux_rate = p_sys->fmt.i_bitrate / 50 / 8;
227 else if( p_sys->i_data_size > 0 )
229 p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
230 (mtime_t)1000000 / 50 / p_sys->i_frame_length;
232 vlc_mutex_unlock( &p_input->stream.stream_lock );
234 p_sys->p_es = es_out_Add( p_input->p_es_out, &p_sys->fmt );
243 /*****************************************************************************
244 * Demux: read packet and send them to decoders
245 *****************************************************************************
246 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
247 *****************************************************************************/
248 static int Demux( input_thread_t *p_input )
250 demux_sys_t *p_sys = p_input->p_demux_data;
254 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
256 i_pos = stream_Tell( p_input->s );
259 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
260 p_sys->i_time * 9 / 100 );
262 i_pos = stream_Tell( p_input->s );
264 /* Check alignment */
265 if( p_sys->fmt.audio.i_blockalign != 0 &&
266 i_pos % p_sys->fmt.audio.i_blockalign )
268 int i_skip = p_sys->fmt.audio.i_blockalign -
269 i_pos % p_sys->fmt.audio.i_blockalign;
271 if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
273 msg_Err( p_input, "failed to re-align stream" );
277 if( p_sys->i_data_size > 0 &&
278 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
284 if( ( p_block = stream_Block( p_input->s, p_sys->i_frame_size ) ) == NULL )
286 msg_Warn( p_input, "cannot read data" );
290 p_block->i_pts = input_ClockGetTS( p_input,
291 p_input->stream.p_selected_program,
292 p_sys->i_time * 9 / 100 );
294 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
296 p_sys->i_time += p_sys->i_frame_length;
301 /*****************************************************************************
302 * Close: frees unused data
303 *****************************************************************************/
304 static void Close ( vlc_object_t * p_this )
306 input_thread_t *p_input = (input_thread_t *)p_this;
307 demux_sys_t *p_sys = p_input->p_demux_data;
312 /*****************************************************************************
314 *****************************************************************************/
315 static int ChunkFind( input_thread_t *p_input,
316 char *fcc, unsigned int *pi_size )
324 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
326 msg_Err( p_input, "cannot peek()" );
330 i_size = GetDWLE( p_peek + 4 );
332 msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
334 if( !strncmp( p_peek, fcc, 4 ) )
343 i_size = __EVEN( i_size ) + 8;
344 if( stream_Read( p_input->s, NULL, i_size ) != i_size )
351 static void FrameInfo_PCM( input_thread_t *p_input,
352 unsigned int *pi_size,
355 demux_sys_t *p_sys = p_input->p_demux_data;
362 /* read samples for 50ms of */
363 i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
366 *pi_length = (mtime_t)1000000 *
368 (mtime_t)p_sys->fmt.audio.i_rate;
370 i_bytes = i_samples * p_sys->fmt.audio.i_channels *
371 ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
373 if( p_sys->fmt.audio.i_blockalign > 0 )
375 if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
377 i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
384 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
385 unsigned int *pi_size,
388 demux_sys_t *p_sys = p_input->p_demux_data;
392 i_samples = 2 + 2 * ( p_sys->fmt.audio.i_blockalign -
393 7 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
395 *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
396 (mtime_t)p_sys->fmt.audio.i_rate;
398 *pi_size = p_sys->fmt.audio.i_blockalign;
401 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
402 unsigned int *pi_size,
405 demux_sys_t *p_sys = p_input->p_demux_data;
409 i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
410 4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
412 *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
413 (mtime_t)p_sys->fmt.audio.i_rate;
415 *pi_size = p_sys->fmt.audio.i_blockalign;