1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
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( "demux2", 142 );
43 set_callbacks( Open, Close );
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Demux ( demux_t * );
50 static int Control( demux_t *, int i_query, va_list args );
58 unsigned int i_data_size;
61 unsigned int i_frame_size;
62 mtime_t i_frame_length;
65 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
67 static int ChunkFind( demux_t *, char *, unsigned int * );
69 static void FrameInfo_IMA_ADPCM( demux_t *, unsigned int *, mtime_t * );
70 static void FrameInfo_MS_ADPCM ( demux_t *, unsigned int *, mtime_t * );
71 static void FrameInfo_PCM ( demux_t *, unsigned int *, mtime_t * );
73 /*****************************************************************************
74 * Open: check file and initializes structures
75 *****************************************************************************/
76 static int Open( vlc_object_t * p_this )
78 demux_t *p_demux = (demux_t*)p_this;
82 unsigned int i_size, i_extended;
85 WAVEFORMATEXTENSIBLE *p_wf_ext;
88 /* Is it a wav file ? */
89 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
91 msg_Warn( p_demux, "WAV module discarded (cannot peek)" );
94 if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
99 p_demux->pf_demux = Demux;
100 p_demux->pf_control = Control;
101 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
105 /* skip riff header */
106 stream_Read( p_demux->s, NULL, 12 ); /* cannot fail as peek succeed */
108 /* search fmt chunk */
109 if( ChunkFind( p_demux, "fmt ", &i_size ) )
111 msg_Err( p_demux, "cannot find 'fmt ' chunk" );
114 if( i_size < sizeof( WAVEFORMATEX ) - 2 ) /* XXX -2 isn't a typo */
116 msg_Err( p_demux, "invalid 'fmt ' chunk" );
119 stream_Read( p_demux->s, NULL, 8 ); /* Cannot fail */
121 /* load waveformatex */
122 p_wf_ext = malloc( __EVEN( i_size ) + 2 );
123 p_wf = (WAVEFORMATEX *)p_wf_ext;
125 if( stream_Read( p_demux->s,
126 p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
128 msg_Err( p_demux, "cannot load 'fmt ' chunk" );
132 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
133 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
135 p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
136 p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
137 p_sys->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );
138 p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
139 p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
140 p_sys->fmt.i_extra = GetWLE( &p_wf->cbSize );
143 /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
144 if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
145 i_size >= sizeof( WAVEFORMATEXTENSIBLE ) )
147 wf_tag_to_fourcc( GetWLE( &p_wf_ext->SubFormat ),
148 &p_sys->fmt.i_codec, &psz_name );
149 i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
150 p_sys->fmt.i_extra -= i_extended;
153 if( p_sys->fmt.i_extra > 0 )
155 p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
156 memcpy( p_sys->fmt.p_extra, ((uint8_t *)p_wf) + i_extended,
157 p_sys->fmt.i_extra );
160 msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
161 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, bits/samples: %d, "
163 GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
164 p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
165 p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
166 p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
170 switch( p_sys->fmt.i_codec )
172 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
173 case VLC_FOURCC( 'a', 'f', 'l', 't' ):
174 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
175 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
176 FrameInfo_PCM( p_demux, &p_sys->i_frame_size, &p_sys->i_frame_length );
178 case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
179 FrameInfo_MS_ADPCM( p_demux, &p_sys->i_frame_size,
180 &p_sys->i_frame_length );
182 case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
183 FrameInfo_IMA_ADPCM( p_demux, &p_sys->i_frame_size,
184 &p_sys->i_frame_length );
186 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
187 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
188 /* FIXME not sure at all FIXME */
189 FrameInfo_MS_ADPCM( p_demux, &p_sys->i_frame_size,
190 &p_sys->i_frame_length );
192 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
193 case VLC_FOURCC( 'a', '5', '2', ' ' ):
194 /* FIXME set end of area FIXME */
197 msg_Err( p_demux, "unsupported codec (%4.4s)",
198 (char*)&p_sys->fmt.i_codec );
202 msg_Dbg( p_demux, "found %s audio format", psz_name );
204 if( ChunkFind( p_demux, "data", &p_sys->i_data_size ) )
206 msg_Err( p_demux, "cannot find 'data' chunk" );
209 stream_Read( p_demux->s, NULL, 8 ); /* Cannot fail */
210 p_sys->i_data_pos = stream_Tell( p_demux->s );
212 if( p_sys->fmt.i_bitrate <= 0 )
214 p_sys->fmt.i_bitrate = (mtime_t)p_sys->i_frame_size *
215 I64C(8000000) / p_sys->i_frame_length;
218 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
227 /*****************************************************************************
228 * Demux: read packet and send them to decoders
229 *****************************************************************************
230 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
231 *****************************************************************************/
232 static int Demux( demux_t *p_demux )
234 demux_sys_t *p_sys = p_demux->p_sys;
238 i_pos = stream_Tell( p_demux->s );
240 if( p_sys->i_data_size > 0 &&
241 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
247 if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
249 msg_Warn( p_demux, "cannot read data" );
252 p_block->i_dts = p_block->i_pts = p_sys->i_time;
255 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
257 es_out_Send( p_demux->out, p_sys->p_es, p_block );
259 p_sys->i_time += p_sys->i_frame_length;
264 /*****************************************************************************
265 * Close: frees unused data
266 *****************************************************************************/
267 static void Close ( vlc_object_t * p_this )
269 demux_t *p_demux = (demux_t*)p_this;
270 demux_sys_t *p_sys = p_demux->p_sys;
275 /*****************************************************************************
277 *****************************************************************************/
278 static int Control( demux_t *p_demux, int i_query, va_list args )
280 demux_sys_t *p_sys = p_demux->p_sys;
282 if( p_sys->i_data_size > 0 )
284 i_end = p_sys->i_data_pos + p_sys->i_data_size;
286 return demux2_vaControlHelper( p_demux->s,
287 p_sys->i_data_pos, i_end,
288 p_sys->fmt.i_bitrate, p_sys->fmt.audio.i_blockalign,
292 /*****************************************************************************
294 *****************************************************************************/
295 static int ChunkFind( demux_t *p_demux,
296 char *fcc, unsigned int *pi_size )
304 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
306 msg_Err( p_demux, "cannot peek()" );
310 i_size = GetDWLE( p_peek + 4 );
312 msg_Dbg( p_demux, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
314 if( !strncmp( p_peek, fcc, 4 ) )
323 i_size = __EVEN( i_size ) + 8;
324 if( stream_Read( p_demux->s, NULL, i_size ) != i_size )
331 static void FrameInfo_PCM( demux_t *p_demux,
332 unsigned int *pi_size,
335 demux_sys_t *p_sys = p_demux->p_sys;
342 /* read samples for 50ms of */
343 i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
346 *pi_length = (mtime_t)1000000 *
348 (mtime_t)p_sys->fmt.audio.i_rate;
350 i_bytes = i_samples * p_sys->fmt.audio.i_channels *
351 ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
353 if( p_sys->fmt.audio.i_blockalign > 0 )
355 if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
357 i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
364 static void FrameInfo_MS_ADPCM( demux_t *p_demux,
365 unsigned int *pi_size,
368 demux_sys_t *p_sys = p_demux->p_sys;
372 i_samples = 2 + 2 * ( p_sys->fmt.audio.i_blockalign -
373 7 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
375 *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
376 (mtime_t)p_sys->fmt.audio.i_rate;
378 *pi_size = p_sys->fmt.audio.i_blockalign;
381 static void FrameInfo_IMA_ADPCM( demux_t *p_demux,
382 unsigned int *pi_size,
385 demux_sys_t *p_sys = p_demux->p_sys;
389 i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
390 4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
392 *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
393 (mtime_t)p_sys->fmt.audio.i_rate;
395 *pi_size = p_sys->fmt.audio.i_blockalign;