]> git.sesse.net Git - vlc/blob - modules/demux/wav.c
* ALL: moved i_bitrate out of audio_format_t into es_format_t
[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.10 2003/11/16 22:54:12 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
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     es_format_t     fmt;
56     es_out_id_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     WAVEFORMATEX   *p_wf;
88     unsigned int   i_size;
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_es           = NULL;
107     p_sys->i_time         = 0;
108
109     /* skip riff header */
110     stream_Read( p_input->s, NULL, 12 );  /* cannot fail as peek succeed */
111
112     /* search fmt chunk */
113     if( ChunkFind( p_input, "fmt ", &i_size ) )
114     {
115         msg_Err( p_input, "cannot find 'fmt ' chunk" );
116         goto error;
117     }
118     if( i_size < sizeof( WAVEFORMATEX ) - 2 )   /* XXX -2 isn't a typo */
119     {
120         msg_Err( p_input, "invalid 'fmt ' chunk" );
121         goto error;
122     }
123     stream_Read( p_input->s, NULL, 8 );   /* cannot fail */
124
125     /* load waveformatex */
126     p_wf = malloc( __EVEN( i_size ) + 2 ); /* +2, for raw audio -> no cbSize */
127     p_wf->cbSize = 0;
128     if( stream_Read( p_input->s,
129                      p_wf, __EVEN( i_size ) ) < (int)__EVEN( i_size ) )
130     {
131         msg_Err( p_input, "cannot load 'fmt ' chunk" );
132         goto error;
133     }
134
135     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
136     wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec, &psz_name );
137     p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
138     p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
139     p_sys->fmt.audio.i_blockalign = GetWLE ( &p_wf->nBlockAlign );
140     p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
141     p_sys->fmt.audio.i_bitspersample = GetWLE ( &p_wf->wBitsPerSample );;
142
143     p_sys->fmt.i_extra = GetWLE ( &p_wf->cbSize );
144     if( p_sys->fmt.i_extra > 0 )
145     {
146         p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
147         memcpy( p_sys->fmt.p_extra, &p_wf[1], p_sys->fmt.i_extra );
148     }
149
150     msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
151             GetWLE( &p_wf->wFormatTag ),
152             p_sys->fmt.audio.i_channels,
153             p_sys->fmt.audio.i_rate,
154             p_sys->fmt.i_bitrate / 8 / 1024,
155             p_sys->fmt.audio.i_blockalign,
156             p_sys->fmt.audio.i_bitspersample,
157             p_sys->fmt.i_extra );
158     free( p_wf );
159
160     switch( p_sys->fmt.i_codec )
161     {
162         case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
163         case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
164         case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
165             FrameInfo_PCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
166             break;
167         case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
168             FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
169             break;
170         case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
171             FrameInfo_IMA_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
172             break;
173         case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
174         case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
175             /* FIXME not sure at all FIXME */
176             FrameInfo_MS_ADPCM( p_input, &p_sys->i_frame_size, &p_sys->i_frame_length );
177             break;
178         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
179         case VLC_FOURCC( 'a', '5', '2', ' ' ):
180             /* FIXME set end of area FIXME */
181             goto relay;
182         default:
183             msg_Err( p_input, "unsupported codec (%4.4s)", (char*)&p_sys->fmt.i_codec );
184             goto error;
185     }
186
187     msg_Dbg( p_input, "found %s audio format", psz_name );
188
189     if( ChunkFind( p_input, "data", &p_sys->i_data_size ) )
190     {
191         msg_Err( p_input, "cannot find 'data' chunk" );
192         goto error;
193     }
194
195     p_sys->i_data_pos = stream_Tell( p_input->s );
196
197     stream_Read( p_input->s, NULL, 8 );   /* cannot fail */
198
199     /*  create one program */
200     vlc_mutex_lock( &p_input->stream.stream_lock );
201     if( input_InitStream( p_input, 0 ) == -1)
202     {
203         vlc_mutex_unlock( &p_input->stream.stream_lock );
204         msg_Err( p_input, "cannot init stream" );
205         goto error;
206     }
207     p_input->stream.i_mux_rate = 0;
208     if( p_sys->i_data_size > 0 )
209     {
210         p_input->stream.i_mux_rate = (mtime_t)p_sys->i_frame_size *
211                                      (mtime_t)1000000 / 50 / p_sys->i_frame_length;
212     }
213     vlc_mutex_unlock( &p_input->stream.stream_lock );
214
215     p_sys->p_es = es_out_Add( p_input->p_es_out, &p_sys->fmt );
216     return VLC_SUCCESS;
217
218 error:
219 relay:
220     free( p_sys );
221     return VLC_EGENERIC;
222 }
223
224 /*****************************************************************************
225  * Demux: read packet and send them to decoders
226  *****************************************************************************
227  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
228  *****************************************************************************/
229 static int Demux( input_thread_t *p_input )
230 {
231     demux_sys_t  *p_sys = p_input->p_demux_data;
232     int64_t      i_pos;
233     pes_packet_t *p_pes;
234
235     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
236     {
237         i_pos = stream_Tell( p_input->s );
238         if( p_sys->fmt.audio.i_blockalign != 0 )
239         {
240             i_pos += p_sys->fmt.audio.i_blockalign - i_pos % p_sys->fmt.audio.i_blockalign;
241             if( stream_Seek( p_input->s, i_pos ) )
242             {
243                 msg_Err( p_input, "stream_Sekk failed (cannot resync)" );
244             }
245         }
246     }
247
248     input_ClockManageRef( p_input,
249                           p_input->stream.p_selected_program,
250                           p_sys->i_time * 9 / 100 );
251
252     i_pos = stream_Tell( p_input->s );
253
254     if( p_sys->i_data_size > 0 &&
255         i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
256     {
257         /* EOF */
258         return 0;
259     }
260
261     if( ( p_pes = stream_PesPacket( p_input->s, p_sys->i_frame_size ) )==NULL )
262     {
263         msg_Warn( p_input, "cannot read data" );
264         return 0;
265     }
266     p_pes->i_dts =
267     p_pes->i_pts = input_ClockGetTS( p_input,
268                                      p_input->stream.p_selected_program,
269                                      p_sys->i_time * 9 / 100 );
270
271     es_out_Send( p_input->p_es_out, p_sys->p_es, p_pes );
272
273     p_sys->i_time += p_sys->i_frame_length;
274
275     return( 1 );
276 }
277
278 /*****************************************************************************
279  * Close: frees unused data
280  *****************************************************************************/
281 static void Close ( vlc_object_t * p_this )
282 {
283     input_thread_t *p_input = (input_thread_t *)p_this;
284     demux_sys_t    *p_sys = p_input->p_demux_data;
285
286     free( p_sys );
287 }
288
289
290 /*****************************************************************************
291  * Local functions
292  *****************************************************************************/
293 static int ChunkFind( input_thread_t *p_input,
294                       char *fcc, unsigned int *pi_size )
295 {
296     uint8_t     *p_peek;
297
298     for( ;; )
299     {
300         int i_size;
301
302         if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
303         {
304             msg_Err( p_input, "cannot peek()" );
305             return VLC_EGENERIC;
306         }
307
308         i_size = GetDWLE( p_peek + 4 );
309
310         msg_Dbg( p_input, "Chunk: fcc=`%4.4s` size=%d", p_peek, i_size );
311
312         if( !strncmp( p_peek, fcc, 4 ) )
313         {
314             if( pi_size )
315             {
316                 *pi_size = i_size;
317             }
318             return VLC_SUCCESS;
319         }
320
321         i_size = __EVEN( i_size ) + 8;
322         if( stream_Read( p_input->s, NULL, i_size ) != i_size )
323         {
324             return VLC_EGENERIC;
325         }
326     }
327 }
328
329 static void FrameInfo_PCM( input_thread_t *p_input,
330                            unsigned int   *pi_size,
331                            mtime_t        *pi_length )
332 {
333     demux_sys_t    *p_sys = p_input->p_demux_data;
334
335     int i_samples;
336
337     int i_bytes;
338     int i_modulo;
339
340     /* read samples for 50ms of */
341     i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
342
343
344     *pi_length = (mtime_t)1000000 *
345                  (mtime_t)i_samples /
346                  (mtime_t)p_sys->fmt.audio.i_rate;
347
348     i_bytes = i_samples * p_sys->fmt.audio.i_channels * ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
349
350     if( p_sys->fmt.audio.i_blockalign > 0 )
351     {
352         if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
353         {
354             i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
355         }
356     }
357     *pi_size = i_bytes;
358 }
359
360 static void FrameInfo_MS_ADPCM( input_thread_t *p_input,
361                               unsigned int   *pi_size,
362                               mtime_t        *pi_length )
363 {
364     demux_sys_t    *p_sys = p_input->p_demux_data;
365
366     int i_samples;
367
368     i_samples = 2 + 2 * ( p_sys->fmt.audio.i_blockalign -
369                                 7 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
370
371     *pi_length = (mtime_t)1000000 *
372                  (mtime_t)i_samples /
373                  (mtime_t)p_sys->fmt.audio.i_rate;
374
375     *pi_size = p_sys->fmt.audio.i_blockalign;
376 }
377
378 static void FrameInfo_IMA_ADPCM( input_thread_t *p_input,
379                                unsigned int   *pi_size,
380                                mtime_t        *pi_length )
381 {
382     demux_sys_t    *p_sys = p_input->p_demux_data;
383
384     int i_samples;
385
386     i_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
387                         4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
388
389     *pi_length = (mtime_t)1000000 *
390                  (mtime_t)i_samples /
391                  (mtime_t)p_sys->fmt.audio.i_rate;
392
393     *pi_size = p_sys->fmt.audio.i_blockalign;
394 }
395