1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: wav.c,v 1.15 2004/03/03 11:40:19 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 *****************************************************************************/
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 = (WAVEFORMATEX *)p_wf_ext = malloc( __EVEN( i_size ) + 2 );
124 if( stream_Read( p_demux->s,
125 p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
127 msg_Err( p_demux, "cannot load 'fmt ' chunk" );
131 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
132 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
134 p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
135 p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
136 p_sys->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );
137 p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
138 p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
139 p_sys->fmt.i_extra = GetWLE( &p_wf->cbSize );
142 /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
143 if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
144 i_size >= sizeof( WAVEFORMATEXTENSIBLE ) )
146 wf_tag_to_fourcc( GetWLE( &p_wf_ext->SubFormat ),
147 &p_sys->fmt.i_codec, &psz_name );
148 i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
149 p_sys->fmt.i_extra -= i_extended;
152 if( p_sys->fmt.i_extra > 0 )
154 p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
155 memcpy( p_sys->fmt.p_extra, ((uint8_t *)p_wf) + i_extended,
156 p_sys->fmt.i_extra );
159 msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
160 "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, bits/samples: %d, "
162 GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
163 p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
164 p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
165 p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
169 switch( p_sys->fmt.i_codec )
171 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
172 case VLC_FOURCC( 'a', 'f', 'l', 't' ):
173 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
174 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
175 FrameInfo_PCM( p_demux, &p_sys->i_frame_size, &p_sys->i_frame_length );
177 case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
178 FrameInfo_MS_ADPCM( p_demux, &p_sys->i_frame_size,
179 &p_sys->i_frame_length );
181 case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
182 FrameInfo_IMA_ADPCM( p_demux, &p_sys->i_frame_size,
183 &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_demux, &p_sys->i_frame_size,
189 &p_sys->i_frame_length );
191 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
192 case VLC_FOURCC( 'a', '5', '2', ' ' ):
193 /* FIXME set end of area FIXME */
196 msg_Err( p_demux, "unsupported codec (%4.4s)",
197 (char*)&p_sys->fmt.i_codec );
201 msg_Dbg( p_demux, "found %s audio format", psz_name );
203 if( ChunkFind( p_demux, "data", &p_sys->i_data_size ) )
205 msg_Err( p_demux, "cannot find 'data' chunk" );
208 stream_Read( p_demux->s, NULL, 8 ); /* Cannot fail */
209 p_sys->i_data_pos = stream_Tell( p_demux->s );
211 if( p_sys->fmt.i_bitrate <= 0 )
213 p_sys->fmt.i_bitrate = (mtime_t)p_sys->i_frame_size *
214 I64C(8000000) / p_sys->i_frame_length;
217 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
226 /*****************************************************************************
227 * Demux: read packet and send them to decoders
228 *****************************************************************************
229 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
230 *****************************************************************************/
231 static int Demux( demux_t *p_demux )
233 demux_sys_t *p_sys = p_demux->p_sys;
237 i_pos = stream_Tell( p_demux->s );
239 if( p_sys->i_data_size > 0 &&
240 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
246 if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
248 msg_Warn( p_demux, "cannot read data" );
251 p_block->i_dts = p_block->i_pts = p_sys->i_time;
254 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
256 es_out_Send( p_demux->out, p_sys->p_es, p_block );
258 p_sys->i_time += p_sys->i_frame_length;
263 /*****************************************************************************
264 * Close: frees unused data
265 *****************************************************************************/
266 static void Close ( vlc_object_t * p_this )
268 demux_t *p_demux = (demux_t*)p_this;
269 demux_sys_t *p_sys = p_demux->p_sys;
274 /*****************************************************************************
276 *****************************************************************************/
277 static int Control( demux_t *p_demux, int i_query, va_list args )
279 demux_sys_t *p_sys = p_demux->p_sys;
281 if( p_sys->i_data_size > 0 )
283 i_end = p_sys->i_data_pos + p_sys->i_data_size;
285 return demux2_vaControlHelper( p_demux->s,
286 p_sys->i_data_pos, i_end,
287 p_sys->fmt.i_bitrate, p_sys->fmt.audio.i_blockalign,
291 /*****************************************************************************
293 *****************************************************************************/
294 static int ChunkFind( demux_t *p_demux,
295 char *fcc, unsigned int *pi_size )
303 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
305 msg_Err( p_demux, "cannot peek()" );
309 i_size = GetDWLE( p_peek + 4 );
311 msg_Dbg( p_demux, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
313 if( !strncmp( p_peek, fcc, 4 ) )
322 i_size = __EVEN( i_size ) + 8;
323 if( stream_Read( p_demux->s, NULL, i_size ) != i_size )
330 static void FrameInfo_PCM( demux_t *p_demux,
331 unsigned int *pi_size,
334 demux_sys_t *p_sys = p_demux->p_sys;
341 /* read samples for 50ms of */
342 i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
345 *pi_length = (mtime_t)1000000 *
347 (mtime_t)p_sys->fmt.audio.i_rate;
349 i_bytes = i_samples * p_sys->fmt.audio.i_channels *
350 ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
352 if( p_sys->fmt.audio.i_blockalign > 0 )
354 if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
356 i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
363 static void FrameInfo_MS_ADPCM( demux_t *p_demux,
364 unsigned int *pi_size,
367 demux_sys_t *p_sys = p_demux->p_sys;
371 i_samples = 2 + 2 * ( p_sys->fmt.audio.i_blockalign -
372 7 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
374 *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
375 (mtime_t)p_sys->fmt.audio.i_rate;
377 *pi_size = p_sys->fmt.audio.i_blockalign;
380 static void FrameInfo_IMA_ADPCM( demux_t *p_demux,
381 unsigned int *pi_size,
384 demux_sys_t *p_sys = p_demux->p_sys;
388 i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
389 4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
391 *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
392 (mtime_t)p_sys->fmt.audio.i_rate;
394 *pi_size = p_sys->fmt.audio.i_blockalign;