]> git.sesse.net Git - vlc/blob - modules/demux/wav.c
* modules/demux/util/id3.c, modules/demux/util/id3tag.c: don't use input_Peek() to...
[vlc] / modules / demux / wav.c
1 /*****************************************************************************
2  * wav.c : wav file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: wav.c,v 1.7 2003/09/12 16:26:40 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
38 static int  Open    ( vlc_object_t * );
39 static void Close  ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("WAV demuxer") );
43     set_capability( "demux", 142 );
44     set_callbacks( Open, Close );
45 vlc_module_end();
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50
51 static int  Demux       ( input_thread_t * );
52
53 struct demux_sys_t
54 {
55     WAVEFORMATEX    *p_wf;
56     es_descriptor_t *p_es;
57
58     int64_t         i_data_pos;
59     unsigned int    i_data_size;
60
61     mtime_t         i_time;
62     unsigned int    i_frame_size;
63     mtime_t         i_frame_length;
64 };
65
66
67 /*****************************************************************************
68  * Declaration of local function
69  *****************************************************************************/
70 #define __EVEN( x ) ( ( (x)%2 != 0 ) ? ((x)+1) : (x) )
71
72 static int ChunkFind( input_thread_t *, char *, unsigned int * );
73
74 static void FrameInfo_IMA_ADPCM( input_thread_t *, unsigned int *, mtime_t * );
75 static void FrameInfo_MS_ADPCM ( input_thread_t *, unsigned int *, mtime_t * );
76 static void FrameInfo_PCM      ( input_thread_t *, unsigned int *, mtime_t * );
77
78 /*****************************************************************************
79  * Open: check file and initializes structures
80  *****************************************************************************/
81 static int Open( vlc_object_t * p_this )
82 {
83     input_thread_t *p_input = (input_thread_t *)p_this;
84     demux_sys_t    *p_sys;
85
86     uint8_t        *p_peek;
87     unsigned int   i_size;
88     vlc_fourcc_t   i_fourcc;
89     char *psz_name;
90
91     /* Is it a wav file ? */
92     if( input_Peek( p_input, &p_peek, 12 ) < 12 )
93     {
94         msg_Warn( p_input, "WAV module discarded (cannot peek)" );
95         return VLC_EGENERIC;
96     }
97     if( strncmp( p_peek, "RIFF", 4 ) || strncmp( &p_peek[8], "WAVE", 4 ) )
98     {
99         msg_Warn( p_input, "WAV module discarded (not a valid file)" );
100         return VLC_EGENERIC;
101     }
102
103     p_input->pf_demux     = Demux;
104     p_input->pf_demux_control = demux_vaControlDefault;
105     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
106     p_sys->p_wf           = NULL;
107     p_sys->p_es           = NULL;
108     p_sys->i_time         = 0;
109
110     /* skip riff header */
111     stream_Read( p_input->s, NULL, 12 );  /* cannot fail as peek succeed */
112
113     /* search fmt chunk */
114     if( ChunkFind( p_input, "fmt ", &i_size ) )
115     {
116         msg_Err( p_input, "cannot find 'fmt ' chunk" );
117         goto error;
118     }
119     if( i_size < sizeof( WAVEFORMATEX ) - 2 )   /* XXX -2 isn't a typo */
120     {
121         msg_Err( p_input, "invalid 'fmt ' chunk" );
122         goto error;
123     }
124     stream_Read( p_input->s, NULL, 8 );   /* cannot fail */
125
126     /* load waveformatex */
127     p_sys->p_wf = malloc( __EVEN( i_size ) + 2 ); /* +2, for raw audio -> no cbSize */
128     p_sys->p_wf->cbSize = 0;
129     if( stream_Read( p_input->s,
130                      p_sys->p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
131     {
132         msg_Err( p_input, "cannot load 'fmt ' chunk" );
133         goto error;
134     }
135
136     /* le->me */
137     p_sys->p_wf->wFormatTag      = GetWLE ( &p_sys->p_wf->wFormatTag );
138     p_sys->p_wf->nChannels       = GetWLE ( &p_sys->p_wf->nChannels );
139     p_sys->p_wf->nSamplesPerSec  = GetDWLE( &p_sys->p_wf->nSamplesPerSec );
140     p_sys->p_wf->nAvgBytesPerSec = GetDWLE( &p_sys->p_wf->nAvgBytesPerSec );
141     p_sys->p_wf->nBlockAlign     = GetWLE ( &p_sys->p_wf->nBlockAlign );
142     p_sys->p_wf->wBitsPerSample  = GetWLE ( &p_sys->p_wf->wBitsPerSample );
143     p_sys->p_wf->cbSize          = GetWLE ( &p_sys->p_wf->cbSize );
144
145     msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
146             p_sys->p_wf->wFormatTag,
147             p_sys->p_wf->nChannels,
148             p_sys->p_wf->nSamplesPerSec,
149             p_sys->p_wf->nAvgBytesPerSec / 1024,
150             p_sys->p_wf->nBlockAlign,
151             p_sys->p_wf->wBitsPerSample,
152             p_sys->p_wf->cbSize );
153
154     if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
155     {
156         msg_Err( p_input, "cannot find 'data' chunk" );
157         goto error;
158     }
159
160     p_sys->i_data_pos = stream_Tell( p_input->s );
161
162     stream_Read( p_input->s, NULL, 8 );   /* cannot fail */
163
164     wf_tag_to_fourcc( p_sys->p_wf->wFormatTag, &i_fourcc, &psz_name );
165     if( i_fourcc == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
166     {
167         msg_Err( p_input,"unrecognize audio format(0x%x)",
168                  p_sys->p_wf->wFormatTag );
169         goto error;
170     }
171
172     switch( i_fourcc )
173     {
174         case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
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, &p_sys->i_frame_length );
181             break;
182         case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
183             FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size, &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_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
189             break;
190         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
191         case VLC_FOURCC( 'a', '5', '2', ' ' ):
192             /* FIXME set end of area FIXME */
193             goto relay;
194         default:
195             msg_Err( p_input, "unsupported codec (%4.4s)", (char*)&i_fourcc );
196             goto error;
197     }
198     msg_Dbg( p_input, "found %s audio format", psz_name );
199
200
201     /*  create one program */
202     vlc_mutex_lock( &p_input->stream.stream_lock );
203     if( input_InitStream( p_input, 0 ) == -1)
204     {
205         vlc_mutex_unlock( &p_input->stream.stream_lock );
206         msg_Err( p_input, "cannot init stream" );
207         goto error;
208     }
209     if( input_AddProgram( p_input, 0, 0) == NULL )
210     {
211         vlc_mutex_unlock( &p_input->stream.stream_lock );
212         msg_Err( p_input, "cannot add program" );
213         goto error;
214     }
215     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
216
217     if( p_sys->i_data_size > 0 )
218     {
219         p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
220                                      (mtime_t)1000000 / 50 / p_sys->i_frame_length;
221     }
222     else
223     {
224         p_input->stream.i_mux_rate = 0;
225     }
226
227     p_sys->p_es = input_AddES( p_input,
228                                  p_input->stream.p_selected_program,
229                                  1, AUDIO_ES, NULL, 0 );
230     p_sys->p_es->i_stream_id = 1;
231     p_sys->p_es->i_fourcc = i_fourcc;
232     p_sys->p_es->p_waveformatex = malloc( sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
233     memcpy( p_sys->p_es->p_waveformatex,
234             p_sys->p_wf,
235             sizeof( WAVEFORMATEX ) + p_sys->p_wf->cbSize );
236
237     input_SelectES( p_input, p_sys->p_es );
238
239     p_input->stream.p_selected_program->b_is_ok = 1;
240     vlc_mutex_unlock( &p_input->stream.stream_lock );
241
242     return VLC_SUCCESS;
243
244 error:
245 relay:
246     if( p_sys->p_wf )
247     {
248         free( p_sys->p_wf );
249     }
250     free( p_sys );
251
252     return VLC_EGENERIC;
253 }
254
255 /*****************************************************************************
256  * Demux: read packet and send them to decoders
257  *****************************************************************************
258  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
259  *****************************************************************************/
260 static int Demux( input_thread_t *p_input )
261 {
262     demux_sys_t  *p_sys = p_input->p_demux_data;
263     int64_t      i_pos;
264     pes_packet_t *p_pes;
265
266     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
267     {
268         i_pos = stream_Tell( p_input->s );
269         if( p_sys->p_wf->nBlockAlign != 0 )
270         {
271             i_pos += p_sys->p_wf->nBlockAlign - i_pos % p_sys->p_wf->nBlockAlign;
272             if( stream_Seek( p_input->s, i_pos ) )
273             {
274                 msg_Err( p_input, "stream_Sekk failed (cannot resync)" );
275             }
276         }
277     }
278
279     input_ClockManageRef( p_input,
280                           p_input->stream.p_selected_program,
281                           p_sys->i_time * 9 / 100 );
282
283     i_pos = stream_Tell( p_input->s );
284
285     if( p_sys->i_data_size > 0 &&
286         i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
287     {
288         /* EOF */
289         return 0;
290     }
291
292     if( ( p_pes = stream_PesPacket( p_input->s, p_sys->i_frame_size ) )==NULL )
293     {
294         msg_Warn( p_input, "cannot read data" );
295         return 0;
296     }
297     p_pes->i_dts =
298     p_pes->i_pts = input_ClockGetTS( p_input,
299                                      p_input->stream.p_selected_program,
300                                      p_sys->i_time * 9 / 100 );
301
302     if( !p_sys->p_es->p_decoder_fifo )
303     {
304         msg_Err( p_input, "no audio decoder" );
305         input_DeletePES( p_input->p_method_data, p_pes );
306         return( -1 );
307     }
308
309     input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
310     p_sys->i_time += p_sys->i_frame_length;
311     return( 1 );
312 }
313
314 /*****************************************************************************
315  * Close: frees unused data
316  *****************************************************************************/
317 static void Close ( vlc_object_t * p_this )
318 {
319     input_thread_t *p_input = (input_thread_t *)p_this;
320     demux_sys_t    *p_sys = p_input->p_demux_data;
321
322     free( p_sys->p_wf );
323     free( p_sys );
324 }
325
326
327 /*****************************************************************************
328  * Local functions
329  *****************************************************************************/
330 static int ChunkFind( input_thread_t *p_input,
331                       char *fcc, unsigned int *pi_size )
332 {
333     uint8_t     *p_peek;
334
335     for( ;; )
336     {
337         int i_size;
338
339         if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
340         {
341             msg_Err( p_input, "cannot peek()" );
342             return VLC_EGENERIC;
343         }
344
345         i_size = GetDWLE( p_peek + 4 );
346
347         msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
348
349         if( !strncmp( p_peek, fcc, 4 ) )
350         {
351             if( pi_size )
352             {
353                 *pi_size = i_size;
354             }
355             return VLC_SUCCESS;
356         }
357
358         i_size = __EVEN( i_size ) + 8;
359         if( stream_Read( p_input->s, NULL, i_size ) != i_size )
360         {
361             return VLC_EGENERIC;
362         }
363     }
364 }
365
366 static void FrameInfo_PCM( input_thread_t *p_input,
367                            unsigned int   *pi_size,
368                            mtime_t        *pi_length )
369 {
370     WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
371     int i_samples;
372
373     int i_bytes;
374     int i_modulo;
375
376     /* read samples for 50ms of */
377     i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
378
379
380     *pi_length = (mtime_t)1000000 *
381                  (mtime_t)i_samples /
382                  (mtime_t)p_wf->nSamplesPerSec;
383
384     i_bytes = i_samples * p_wf->nChannels * ( (p_wf->wBitsPerSample + 7) / 8 );
385
386     if( p_wf->nBlockAlign > 0 )
387     {
388         if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
389         {
390             i_bytes += p_wf->nBlockAlign - i_modulo;
391         }
392     }
393     *pi_size = i_bytes;
394 }
395
396 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
397                               unsigned int   *pi_size,
398                               mtime_t        *pi_length )
399 {
400     WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
401     int i_samples;
402
403     i_samples = 2 + 2 * ( p_wf->nBlockAlign -
404                                 7 * p_wf->nChannels ) / p_wf->nChannels;
405
406     *pi_length = (mtime_t)1000000 *
407                  (mtime_t)i_samples /
408                  (mtime_t)p_wf->nSamplesPerSec;
409
410     *pi_size = p_wf->nBlockAlign;
411 }
412
413 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
414                                unsigned int   *pi_size,
415                                mtime_t        *pi_length )
416 {
417     WAVEFORMATEX *p_wf = p_input->p_demux_data->p_wf;
418     int i_samples;
419
420     i_samples = 2 * ( p_wf->nBlockAlign -
421                         4 * p_wf->nChannels ) / p_wf->nChannels;
422
423     *pi_length = (mtime_t)1000000 *
424                  (mtime_t)i_samples /
425                  (mtime_t)p_wf->nSamplesPerSec;
426
427     *pi_size = p_wf->nBlockAlign;
428 }
429