]> git.sesse.net Git - vlc/blob - modules/demux/wav.c
* a52.c aac.c au.c dts.c flac.c wav.c: Converted all audio only demuxers
[vlc] / modules / demux / wav.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <codecs.h>
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int  Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
39
40 vlc_module_begin();
41     set_description( _("WAV demuxer") );
42     set_capability( "demux2", 142 );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int Demux  ( demux_t * );
50 static int Control( demux_t *, int i_query, va_list args );
51
52 struct demux_sys_t
53 {
54     es_format_t     fmt;
55     es_out_id_t     *p_es;
56
57     int64_t         i_data_pos;
58     unsigned int    i_data_size;
59
60     mtime_t         i_time;
61     unsigned int    i_frame_size;
62     mtime_t         i_frame_length;
63 };
64
65 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
66
67 static int ChunkFind( demux_t *, char *, unsigned int * );
68
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 * );
72
73 /*****************************************************************************
74  * Open: check file and initializes structures
75  *****************************************************************************/
76 static int Open( vlc_object_t * p_this )
77 {
78     demux_t     *p_demux = (demux_t*)p_this;
79     demux_sys_t *p_sys;
80
81     uint8_t     *p_peek;
82     unsigned int i_size, i_extended;
83     char        *psz_name;
84
85     WAVEFORMATEXTENSIBLE *p_wf_ext;
86     WAVEFORMATEX         *p_wf;
87
88     /* Is it a wav file ? */
89     if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
90     {
91         msg_Warn( p_demux, "WAV module discarded (cannot peek)" );
92         return VLC_EGENERIC;
93     }
94     if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
95     {
96         return VLC_EGENERIC;
97     }
98
99     p_demux->pf_demux   = Demux;
100     p_demux->pf_control = Control;
101     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
102     p_sys->p_es         = NULL;
103     p_sys->i_time       = 0;
104
105     /* skip riff header */
106     stream_Read( p_demux->s, NULL, 12 );  /* cannot fail as peek succeed */
107
108     /* search fmt chunk */
109     if( ChunkFind( p_demux, "fmt ", &i_size ) )
110     {
111         msg_Err( p_demux, "cannot find 'fmt ' chunk" );
112         goto error;
113     }
114     if( i_size < sizeof( WAVEFORMATEX ) - 2 )   /* XXX -2 isn't a typo */
115     {
116         msg_Err( p_demux, "invalid 'fmt ' chunk" );
117         goto error;
118     }
119     stream_Read( p_demux->s, NULL, 8 );   /* Cannot fail */
120
121     /* load waveformatex */
122     p_wf = (WAVEFORMATEX *)p_wf_ext = malloc( __EVEN( i_size ) + 2 );
123     p_wf->cbSize = 0;
124     if( stream_Read( p_demux->s,
125                      p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
126     {
127         msg_Err( p_demux, "cannot load 'fmt ' chunk" );
128         goto error;
129     }
130
131     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
132     wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
133                       &psz_name );
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 );
140     i_extended = 0;
141
142     /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
143     if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
144         i_size >= sizeof( WAVEFORMATEXTENSIBLE ) )
145     {
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;
150     }
151
152     if( p_sys->fmt.i_extra > 0 )
153     {
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 );
157     }
158
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, "
161              "extra size: %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 );
166
167     free( p_wf );
168
169     switch( p_sys->fmt.i_codec )
170     {
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 );
176         break;
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 );
180         break;
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 );
184         break;
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 );
190         break;
191     case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
192     case VLC_FOURCC( 'a', '5', '2', ' ' ):
193         /* FIXME set end of area FIXME */
194         goto relay;
195     default:
196         msg_Err( p_demux, "unsupported codec (%4.4s)",
197                  (char*)&p_sys->fmt.i_codec );
198         goto error;
199     }
200
201     msg_Dbg( p_demux, "found %s audio format", psz_name );
202
203     if( ChunkFind( p_demux, "data", &p_sys->i_data_size ) )
204     {
205         msg_Err( p_demux, "cannot find 'data' chunk" );
206         goto error;
207     }
208     stream_Read( p_demux->s, NULL, 8 );   /* Cannot fail */
209     p_sys->i_data_pos = stream_Tell( p_demux->s );
210
211     if( p_sys->fmt.i_bitrate <= 0 )
212     {
213         p_sys->fmt.i_bitrate = (mtime_t)p_sys->i_frame_size *
214             I64C(8000000) / p_sys->i_frame_length;
215     }
216
217     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
218     return VLC_SUCCESS;
219
220 error:
221 relay:
222     free( p_sys );
223     return VLC_EGENERIC;
224 }
225
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 )
232 {
233     demux_sys_t *p_sys = p_demux->p_sys;
234     int64_t     i_pos;
235     block_t     *p_block;
236
237     i_pos = stream_Tell( p_demux->s );
238
239     if( p_sys->i_data_size > 0 &&
240         i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
241     {
242         /* EOF */
243         return 0;
244     }
245
246     if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
247     {
248         msg_Warn( p_demux, "cannot read data" );
249         return 0;
250     }
251     p_block->i_dts = p_block->i_pts = p_sys->i_time;
252
253     /* set PCR */
254     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
255
256     es_out_Send( p_demux->out, p_sys->p_es, p_block );
257
258     p_sys->i_time += p_sys->i_frame_length;
259
260     return 1;
261 }
262
263 /*****************************************************************************
264  * Close: frees unused data
265  *****************************************************************************/
266 static void Close ( vlc_object_t * p_this )
267 {
268     demux_t     *p_demux = (demux_t*)p_this;
269     demux_sys_t *p_sys  = p_demux->p_sys;
270
271     free( p_sys );
272 }
273
274 /*****************************************************************************
275  * Control:
276  *****************************************************************************/
277 static int Control( demux_t *p_demux, int i_query, va_list args )
278 {
279     demux_sys_t *p_sys  = p_demux->p_sys;
280     int64_t i_end = -1;
281     if( p_sys->i_data_size > 0 )
282     {
283         i_end = p_sys->i_data_pos + p_sys->i_data_size;
284     }
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,
288                                    i_query, args );
289 }
290
291 /*****************************************************************************
292  * Local functions
293  *****************************************************************************/
294 static int ChunkFind( demux_t *p_demux,
295                       char *fcc, unsigned int *pi_size )
296 {
297     uint8_t *p_peek;
298
299     for( ;; )
300     {
301         int i_size;
302
303         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
304         {
305             msg_Err( p_demux, "cannot peek()" );
306             return VLC_EGENERIC;
307         }
308
309         i_size = GetDWLE( p_peek + 4 );
310
311         msg_Dbg( p_demux, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
312
313         if( !strncmp( p_peek, fcc, 4 ) )
314         {
315             if( pi_size )
316             {
317                 *pi_size = i_size;
318             }
319             return VLC_SUCCESS;
320         }
321
322         i_size = __EVEN( i_size ) + 8;
323         if( stream_Read( p_demux->s, NULL, i_size ) != i_size )
324         {
325             return VLC_EGENERIC;
326         }
327     }
328 }
329
330 static void FrameInfo_PCM( demux_t      *p_demux,
331                            unsigned int *pi_size,
332                            mtime_t      *pi_length )
333 {
334     demux_sys_t *p_sys = p_demux->p_sys;
335
336     int i_samples;
337
338     int i_bytes;
339     int i_modulo;
340
341     /* read samples for 50ms of */
342     i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
343
344
345     *pi_length = (mtime_t)1000000 *
346                  (mtime_t)i_samples /
347                  (mtime_t)p_sys->fmt.audio.i_rate;
348
349     i_bytes = i_samples * p_sys->fmt.audio.i_channels *
350         ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
351
352     if( p_sys->fmt.audio.i_blockalign > 0 )
353     {
354         if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
355         {
356             i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
357         }
358     }
359
360     *pi_size = i_bytes;
361 }
362
363 static void FrameInfo_MS_ADPCM( demux_t      *p_demux,
364                                 unsigned int *pi_size,
365                                 mtime_t      *pi_length )
366 {
367     demux_sys_t *p_sys = p_demux->p_sys;
368
369     int i_samples;
370
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;
373
374     *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
375                  (mtime_t)p_sys->fmt.audio.i_rate;
376
377     *pi_size = p_sys->fmt.audio.i_blockalign;
378 }
379
380 static void FrameInfo_IMA_ADPCM( demux_t      *p_demux,
381                                  unsigned int *pi_size,
382                                  mtime_t      *pi_length )
383 {
384     demux_sys_t *p_sys = p_demux->p_sys;
385
386     int i_samples;
387
388     i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
389         4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
390
391     *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
392                  (mtime_t)p_sys->fmt.audio.i_rate;
393
394     *pi_size = p_sys->fmt.audio.i_blockalign;
395 }
396
397