]> git.sesse.net Git - vlc/blob - modules/demux/wav.c
d2ac680b7ac1913ffdd969561551ab5481d8cbf4
[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.14 2004/02/23 23:01:05 gbazin 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( "demux", 142 );
43     set_callbacks( Open, Close );
44 vlc_module_end();
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int Demux( input_thread_t * );
50
51 struct demux_sys_t
52 {
53     es_format_t     fmt;
54     es_out_id_t     *p_es;
55
56     int64_t         i_data_pos;
57     unsigned int    i_data_size;
58
59     mtime_t         i_time;
60     unsigned int    i_frame_size;
61     mtime_t         i_frame_length;
62 };
63
64 /*****************************************************************************
65  * Declaration of local function
66  *****************************************************************************/
67 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
68
69 static int ChunkFind( input_thread_t *, char *, unsigned int * );
70
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 * );
74
75 /*****************************************************************************
76  * Open: check file and initializes structures
77  *****************************************************************************/
78 static int Open( vlc_object_t * p_this )
79 {
80     input_thread_t *p_input = (input_thread_t *)p_this;
81     demux_sys_t    *p_sys;
82
83     uint8_t        *p_peek;
84     unsigned int   i_size, i_extended;
85     char *psz_name;
86
87     WAVEFORMATEXTENSIBLE *p_wf_ext;
88     WAVEFORMATEX         *p_wf;
89
90     /* Is it a wav file ? */
91     if( input_Peek( p_input, &p_peek, 12 ) < 12 )
92     {
93         msg_Warn( p_input, "WAV module discarded (cannot peek)" );
94         return VLC_EGENERIC;
95     }
96     if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
97     {
98         return VLC_EGENERIC;
99     }
100
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 ) );
104     p_sys->p_es           = NULL;
105     p_sys->i_time         = 0;
106
107     /* skip riff header */
108     stream_Read( p_input->s, NULL, 12 );  /* cannot fail as peek succeed */
109
110     /* search fmt chunk */
111     if( ChunkFind( p_input, "fmt ", &i_size ) )
112     {
113         msg_Err( p_input, "cannot find 'fmt ' chunk" );
114         goto error;
115     }
116     if( i_size < sizeof( WAVEFORMATEX ) - 2 )   /* XXX -2 isn't a typo */
117     {
118         msg_Err( p_input, "invalid 'fmt ' chunk" );
119         goto error;
120     }
121     stream_Read( p_input->s, NULL, 8 );   /* Cannot fail */
122
123     /* load waveformatex */
124     p_wf = (WAVEFORMATEX *)p_wf_ext = malloc( __EVEN( i_size ) + 2 );
125     p_wf->cbSize = 0;
126     if( stream_Read( p_input->s,
127                      p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
128     {
129         msg_Err( p_input, "cannot load 'fmt ' chunk" );
130         goto error;
131     }
132
133     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
134     wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
135                       &psz_name );
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 );
142     i_extended = 0;
143
144     /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
145     if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
146         i_size >= sizeof( WAVEFORMATEXTENSIBLE ) )
147     {
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;
152     }
153
154     if( p_sys->fmt.i_extra > 0 )
155     {
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 );
159     }
160
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, "
163              "extra size: %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 );
168
169     free( p_wf );
170
171     switch( p_sys->fmt.i_codec )
172     {
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 );
178         break;
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 );
182         break;
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 );
186         break;
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 );
192         break;
193     case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
194     case VLC_FOURCC( 'a', '5', '2', ' ' ):
195         /* FIXME set end of area FIXME */
196         goto relay;
197     default:
198         msg_Err( p_input, "unsupported codec (%4.4s)",
199                  (char*)&p_sys->fmt.i_codec );
200         goto error;
201     }
202
203     msg_Dbg( p_input, "found %s audio format", psz_name );
204
205     if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
206     {
207         msg_Err( p_input, "cannot find 'data' chunk" );
208         goto error;
209     }
210     stream_Read( p_input->s, NULL, 8 );   /* Cannot fail */
211     p_sys->i_data_pos = stream_Tell( p_input->s );
212
213     /* Create one program */
214     vlc_mutex_lock( &p_input->stream.stream_lock );
215     if( input_InitStream( p_input, 0 ) == -1)
216     {
217         vlc_mutex_unlock( &p_input->stream.stream_lock );
218         msg_Err( p_input, "cannot init stream" );
219         goto error;
220     }
221
222     p_input->stream.i_mux_rate = 0;
223     if( p_sys->fmt.i_bitrate )
224     {
225         p_input->stream.i_mux_rate = p_sys->fmt.i_bitrate / 50 / 8;
226     }
227     else if( p_sys->i_data_size > 0 )
228     {
229         p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
230             (mtime_t)1000000 / 50 / p_sys->i_frame_length;
231     }
232     vlc_mutex_unlock( &p_input->stream.stream_lock );
233
234     p_sys->p_es = es_out_Add( p_input->p_es_out, &p_sys->fmt );
235     return VLC_SUCCESS;
236
237 error:
238 relay:
239     free( p_sys );
240     return VLC_EGENERIC;
241 }
242
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 )
249 {
250     demux_sys_t *p_sys = p_input->p_demux_data;
251     int64_t     i_pos;
252     block_t     *p_block;
253
254     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
255     {
256         i_pos = stream_Tell( p_input->s );
257     }
258
259     input_ClockManageRef( p_input, p_input->stream.p_selected_program,
260                           p_sys->i_time * 9 / 100 );
261
262     i_pos = stream_Tell( p_input->s );
263
264     /* Check alignment */
265     if( p_sys->fmt.audio.i_blockalign != 0 &&
266         i_pos % p_sys->fmt.audio.i_blockalign )
267     {
268         int i_skip = p_sys->fmt.audio.i_blockalign -
269             i_pos % p_sys->fmt.audio.i_blockalign;
270
271         if( stream_Read( p_input->s, NULL, i_skip ) != i_skip )
272         {
273             msg_Err( p_input, "failed to re-align stream" );
274         }
275     }
276
277     if( p_sys->i_data_size > 0 &&
278         i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
279     {
280         /* EOF */
281         return 0;
282     }
283
284     if( ( p_block = stream_Block( p_input->s, p_sys->i_frame_size ) ) == NULL )
285     {
286         msg_Warn( p_input, "cannot read data" );
287         return 0;
288     }
289     p_block->i_dts =
290     p_block->i_pts = input_ClockGetTS( p_input,
291                                        p_input->stream.p_selected_program,
292                                        p_sys->i_time * 9 / 100 );
293
294     es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
295
296     p_sys->i_time += p_sys->i_frame_length;
297
298     return 1;
299 }
300
301 /*****************************************************************************
302  * Close: frees unused data
303  *****************************************************************************/
304 static void Close ( vlc_object_t * p_this )
305 {
306     input_thread_t *p_input = (input_thread_t *)p_this;
307     demux_sys_t    *p_sys = p_input->p_demux_data;
308
309     free( p_sys );
310 }
311
312 /*****************************************************************************
313  * Local functions
314  *****************************************************************************/
315 static int ChunkFind( input_thread_t *p_input,
316                       char *fcc, unsigned int *pi_size )
317 {
318     uint8_t *p_peek;
319
320     for( ;; )
321     {
322         int i_size;
323
324         if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
325         {
326             msg_Err( p_input, "cannot peek()" );
327             return VLC_EGENERIC;
328         }
329
330         i_size = GetDWLE( p_peek + 4 );
331
332         msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
333
334         if( !strncmp( p_peek, fcc, 4 ) )
335         {
336             if( pi_size )
337             {
338                 *pi_size = i_size;
339             }
340             return VLC_SUCCESS;
341         }
342
343         i_size = __EVEN( i_size ) + 8;
344         if( stream_Read( p_input->s, NULL, i_size ) != i_size )
345         {
346             return VLC_EGENERIC;
347         }
348     }
349 }
350
351 static void FrameInfo_PCM( input_thread_t *p_input,
352                            unsigned int   *pi_size,
353                            mtime_t        *pi_length )
354 {
355     demux_sys_t    *p_sys = p_input->p_demux_data;
356
357     int i_samples;
358
359     int i_bytes;
360     int i_modulo;
361
362     /* read samples for 50ms of */
363     i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
364
365
366     *pi_length = (mtime_t)1000000 *
367                  (mtime_t)i_samples /
368                  (mtime_t)p_sys->fmt.audio.i_rate;
369
370     i_bytes = i_samples * p_sys->fmt.audio.i_channels *
371         ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
372
373     if( p_sys->fmt.audio.i_blockalign > 0 )
374     {
375         if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
376         {
377             i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
378         }
379     }
380
381     *pi_size = i_bytes;
382 }
383
384 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
385                                 unsigned int   *pi_size,
386                                 mtime_t        *pi_length )
387 {
388     demux_sys_t *p_sys = p_input->p_demux_data;
389
390     int i_samples;
391
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;
394
395     *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
396                  (mtime_t)p_sys->fmt.audio.i_rate;
397
398     *pi_size = p_sys->fmt.audio.i_blockalign;
399 }
400
401 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
402                                  unsigned int   *pi_size,
403                                  mtime_t        *pi_length )
404 {
405     demux_sys_t *p_sys = p_input->p_demux_data;
406
407     int i_samples;
408
409     i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
410         4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
411
412     *pi_length = (mtime_t)1000000 * (mtime_t)i_samples /
413                  (mtime_t)p_sys->fmt.audio.i_rate;
414
415     *pi_size = p_sys->fmt.audio.i_blockalign;
416 }