]> git.sesse.net Git - vlc/blob - modules/demux/wav.c
* all: use 1 as starting time (allow to have a valid date for the first frame).
[vlc] / modules / demux / wav.c
1 /*****************************************************************************
2  * wav.c : wav file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
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       = 1;
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_ext = malloc( __EVEN( i_size ) + 2 );
123     p_wf = (WAVEFORMATEX *)p_wf_ext;
124     p_wf->cbSize = 0;
125     if( stream_Read( p_demux->s,
126                      p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
127     {
128         msg_Err( p_demux, "cannot load 'fmt ' chunk" );
129         goto error;
130     }
131
132     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
133     wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
134                       &psz_name );
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 );
141     i_extended = 0;
142
143     /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
144     if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
145         i_size >= sizeof( WAVEFORMATEXTENSIBLE ) )
146     {
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;
151     }
152
153     if( p_sys->fmt.i_extra > 0 )
154     {
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 );
158     }
159
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, "
162              "extra size: %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 );
167
168     free( p_wf );
169
170     switch( p_sys->fmt.i_codec )
171     {
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 );
177         break;
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 );
181         break;
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 );
185         break;
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 );
191         break;
192     case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
193     case VLC_FOURCC( 'a', '5', '2', ' ' ):
194         /* FIXME set end of area FIXME */
195         goto relay;
196     default:
197         msg_Err( p_demux, "unsupported codec (%4.4s)",
198                  (char*)&p_sys->fmt.i_codec );
199         goto error;
200     }
201
202     msg_Dbg( p_demux, "found %s audio format", psz_name );
203
204     if( ChunkFind( p_demux, "data", &p_sys->i_data_size ) )
205     {
206         msg_Err( p_demux, "cannot find 'data' chunk" );
207         goto error;
208     }
209     stream_Read( p_demux->s, NULL, 8 );   /* Cannot fail */
210     p_sys->i_data_pos = stream_Tell( p_demux->s );
211
212     if( p_sys->fmt.i_bitrate <= 0 )
213     {
214         p_sys->fmt.i_bitrate = (mtime_t)p_sys->i_frame_size *
215             I64C(8000000) / p_sys->i_frame_length;
216     }
217
218     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
219     return VLC_SUCCESS;
220
221 error:
222 relay:
223     free( p_sys );
224     return VLC_EGENERIC;
225 }
226
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 )
233 {
234     demux_sys_t *p_sys = p_demux->p_sys;
235     int64_t     i_pos;
236     block_t     *p_block;
237
238     i_pos = stream_Tell( p_demux->s );
239
240     if( p_sys->i_data_size > 0 &&
241         i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
242     {
243         /* EOF */
244         return 0;
245     }
246
247     if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
248     {
249         msg_Warn( p_demux, "cannot read data" );
250         return 0;
251     }
252     p_block->i_dts = p_block->i_pts = p_sys->i_time;
253
254     /* set PCR */
255     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
256
257     es_out_Send( p_demux->out, p_sys->p_es, p_block );
258
259     p_sys->i_time += p_sys->i_frame_length;
260
261     return 1;
262 }
263
264 /*****************************************************************************
265  * Close: frees unused data
266  *****************************************************************************/
267 static void Close ( vlc_object_t * p_this )
268 {
269     demux_t     *p_demux = (demux_t*)p_this;
270     demux_sys_t *p_sys  = p_demux->p_sys;
271
272     free( p_sys );
273 }
274
275 /*****************************************************************************
276  * Control:
277  *****************************************************************************/
278 static int Control( demux_t *p_demux, int i_query, va_list args )
279 {
280     demux_sys_t *p_sys  = p_demux->p_sys;
281     int64_t i_end = -1;
282     if( p_sys->i_data_size > 0 )
283     {
284         i_end = p_sys->i_data_pos + p_sys->i_data_size;
285     }
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,
289                                    i_query, args );
290 }
291
292 /*****************************************************************************
293  * Local functions
294  *****************************************************************************/
295 static int ChunkFind( demux_t *p_demux,
296                       char *fcc, unsigned int *pi_size )
297 {
298     uint8_t *p_peek;
299
300     for( ;; )
301     {
302         int i_size;
303
304         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
305         {
306             msg_Err( p_demux, "cannot peek()" );
307             return VLC_EGENERIC;
308         }
309
310         i_size = GetDWLE( p_peek + 4 );
311
312         msg_Dbg( p_demux, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
313
314         if( !strncmp( p_peek, fcc, 4 ) )
315         {
316             if( pi_size )
317             {
318                 *pi_size = i_size;
319             }
320             return VLC_SUCCESS;
321         }
322
323         i_size = __EVEN( i_size ) + 8;
324         if( stream_Read( p_demux->s, NULL, i_size ) != i_size )
325         {
326             return VLC_EGENERIC;
327         }
328     }
329 }
330
331 static void FrameInfo_PCM( demux_t      *p_demux,
332                            unsigned int *pi_size,
333                            mtime_t      *pi_length )
334 {
335     demux_sys_t *p_sys = p_demux->p_sys;
336
337     int i_samples;
338
339     int i_bytes;
340     int i_modulo;
341
342     /* read samples for 50ms of */
343     i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
344
345
346     *pi_length = (mtime_t)1000000 *
347                  (mtime_t)i_samples /
348                  (mtime_t)p_sys->fmt.audio.i_rate;
349
350     i_bytes = i_samples * p_sys->fmt.audio.i_channels *
351         ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
352
353     if( p_sys->fmt.audio.i_blockalign > 0 )
354     {
355         if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
356         {
357             i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
358         }
359     }
360
361     *pi_size = i_bytes;
362 }
363
364 static void FrameInfo_MS_ADPCM( demux_t      *p_demux,
365                                 unsigned int *pi_size,
366                                 mtime_t      *pi_length )
367 {
368     demux_sys_t *p_sys = p_demux->p_sys;
369
370     int i_samples;
371
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;
374
375     *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
376                  (mtime_t)p_sys->fmt.audio.i_rate;
377
378     *pi_size = p_sys->fmt.audio.i_blockalign;
379 }
380
381 static void FrameInfo_IMA_ADPCM( demux_t      *p_demux,
382                                  unsigned int *pi_size,
383                                  mtime_t      *pi_length )
384 {
385     demux_sys_t *p_sys = p_demux->p_sys;
386
387     int i_samples;
388
389     i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
390         4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
391
392     *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
393                  (mtime_t)p_sys->fmt.audio.i_rate;
394
395     *pi_size = p_sys->fmt.audio.i_blockalign;
396 }
397
398