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