]> git.sesse.net Git - vlc/blob - modules/demux/wav/wav.c
69dbae880a059ca3a6e541406b7c15aa038ac54f
[vlc] / modules / demux / wav / wav.c
1 /*****************************************************************************
2  * wav.c : wav file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: wav.c,v 1.2 2002/10/26 19:14:46 fenrir Exp $
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdlib.h>                                      /* malloc(), free() */
27 #include <string.h>                                              /* strdup() */
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33
34 #include "wav.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int    WAVInit       ( vlc_object_t * );
40 static void __WAVEnd        ( vlc_object_t * );
41 static int    WAVDemux      ( input_thread_t * );
42 static int    WAVCallDemux  ( input_thread_t * );
43
44 #define WAVEnd(a) __WAVEnd(VLC_OBJECT(a))
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50     set_description( "WAV demuxer" );
51     set_capability( "demux", 142 );
52     set_callbacks( WAVInit, __WAVEnd );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * Declaration of local function 
57  *****************************************************************************/
58
59 #define FREE( p ) if( p ) free( p ); (p) = NULL
60
61 #define __EVEN( x ) ( (x)%2 != 0 ) ? ((x)+1) : (x)
62
63 /* Some functions to manipulate memory */
64 static u16 GetWLE( u8 *p_buff )
65 {
66     return( (p_buff[0]) + ( p_buff[1] <<8 ) );
67 }
68
69 static u32 GetDWLE( u8 *p_buff )
70 {
71     return( p_buff[0] + ( p_buff[1] <<8 ) +
72             ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
73 }
74
75 static u32 CreateDWLE( int a, int b, int c, int d )
76 {
77     return( a + ( b << 8 ) + ( c << 16 ) + ( d << 24 ) );
78 }
79
80
81 static off_t TellAbsolute( input_thread_t *p_input )
82 {
83     off_t i_pos;
84     
85     vlc_mutex_lock( &p_input->stream.stream_lock );
86     
87     i_pos= p_input->stream.p_selected_area->i_tell;
88 //          - ( p_input->p_last_data - p_input->p_current_data  );
89
90     vlc_mutex_unlock( &p_input->stream.stream_lock );
91
92     return( i_pos );
93 }
94  
95 static int SeekAbsolute( input_thread_t *p_input,
96                          off_t i_pos)
97 {
98     off_t i_filepos;
99
100     if( i_pos >= p_input->stream.p_selected_area->i_size )
101     {
102         return( 0 );
103     }
104             
105     i_filepos = TellAbsolute( p_input );
106     if( i_pos != i_filepos )
107     {
108         p_input->pf_seek( p_input, i_pos );
109         input_AccessReinit( p_input );
110     }
111     return( 1 );
112 }
113
114 static int SkipBytes( input_thread_t *p_input, int i_skip )
115 {
116     return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
117 }
118
119 /* return 1 if success, 0 if fail */
120 static int ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
121 {
122     data_packet_t *p_data;
123
124     int i_read;
125
126                 
127     if( !i_size )
128     {
129         return( 1 );
130     }
131
132     do
133     {
134         i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
135         if( i_read <= 0 )
136         {
137             return( 0 );
138         }
139         memcpy( p_buff, p_data->p_payload_start, i_read );
140         input_DeletePacket( p_input->p_method_data, p_data );
141         
142         p_buff += i_read;
143         i_size -= i_read;
144                 
145     } while( i_size );
146     
147     return( 1 );
148 }
149
150
151 static int ReadPES( input_thread_t *p_input, 
152                     pes_packet_t **pp_pes, 
153                     int i_size )
154 {
155     pes_packet_t *p_pes;
156
157     *pp_pes = NULL;
158         
159     if( !(p_pes = input_NewPES( p_input->p_method_data )) )
160     {
161         msg_Err( p_input, "cannot allocate new PES" );
162         return( 0 );
163     }
164
165     while( i_size > 0 )
166     {
167         data_packet_t   *p_data;
168         int i_read;
169
170         if( (i_read = input_SplitBuffer( p_input, 
171                                          &p_data, 
172                                          __MIN( i_size, 1024 ) ) ) <= 0 )
173         {
174             input_DeletePES( p_input->p_method_data, p_pes );
175             return( 0 );
176         }
177         if( !p_pes->p_first )
178         {
179             p_pes->p_first = p_data;
180             p_pes->i_nb_data = 1;
181             p_pes->i_pes_size = i_read;
182         }
183         else
184         {
185             p_pes->p_last->p_next  = p_data;
186             p_pes->i_nb_data++;
187             p_pes->i_pes_size += i_read;
188         }
189         p_pes->p_last  = p_data;
190         i_size -= i_read;
191     }
192     *pp_pes = p_pes;
193     return( 1 );
194 }
195
196 static int FindTag( input_thread_t *p_input, u32 i_tag )
197 {
198     u32   i_id;
199     u32   i_size;
200     u8    *p_peek;
201
202     for( ;; )
203     {
204
205         if( input_Peek( p_input, &p_peek, 8 ) < 8 )
206         {
207             msg_Err( p_input, "cannot peek()" );
208             return( 0 );
209         }
210
211         i_id   = GetDWLE( p_peek );
212         i_size = GetDWLE( p_peek + 4 );
213
214         msg_Dbg( p_input, "FindTag: tag:%4.4s size:%d", &i_id, i_size );
215         if( i_id == i_tag )
216         {
217             /* Yes, we have found the good tag */
218             return( 1 );
219         }
220         if( !SkipBytes( p_input, __EVEN( i_size ) + 8 ) )
221         {
222             return( 0 );
223         }
224     }
225 }
226
227 static int LoadTag_fmt( input_thread_t *p_input, 
228                         demux_sys_t *p_demux )
229 {
230     u8  *p_peek;
231     u32 i_size;
232     
233     if( input_Peek( p_input, &p_peek , 8 ) < 8 )
234     {
235         return( 0 );
236     }
237
238     p_demux->i_wf = i_size = GetDWLE( p_peek + 4 );
239     SkipBytes( p_input, 8 );
240     if( i_size < 16 )
241     {
242         SkipBytes( p_input, i_size );
243         return( 0 );
244     }
245     p_demux->p_wf = malloc( i_size );
246     ReadData( p_input,  p_demux->p_wf, __EVEN( i_size ) );
247     p_demux->format.i_format         = GetWLE( p_demux->p_wf );
248     p_demux->format.i_channels       = GetWLE( p_demux->p_wf + 2 );
249     p_demux->format.i_samplepersec   = GetDWLE( p_demux->p_wf + 4 );
250     p_demux->format.i_avgbytespersec = GetDWLE( p_demux->p_wf + 8);
251     p_demux->format.i_blockalign    = GetWLE( p_demux->p_wf + 12 );
252     p_demux->format.i_bitspersample = GetWLE( p_demux->p_wf + 14);
253     if( i_size > 18 )
254     {
255         p_demux->format.i_size = GetWLE( p_demux->p_wf + 16 );
256         p_demux->format.p_data = malloc( p_demux->format.i_size );
257         memcpy( p_demux->format.p_data, 
258                 p_demux->p_wf + 18, 
259                 p_demux->format.i_size );
260     }
261     else
262     {
263         p_demux->format.i_size = 0;
264         p_demux->format.p_data = NULL;
265     }
266
267     msg_Dbg( p_input, "loaded \"fmt \" chunk" );
268     return( 1 );
269 }
270
271 static int PCM_GetFrame( input_thread_t *p_input,
272                          waveformatex_t *p_wf,
273                          pes_packet_t **pp_pes,
274                          mtime_t *pi_length )
275 {
276     int i_samples;
277
278     int i_bytes;
279     int i_modulo;
280
281     /* read samples for 50ms of */
282     i_samples = __MAX( p_wf->i_samplepersec / 20, 1 );
283         
284     
285     *pi_length = (mtime_t)1000000 * 
286                  (mtime_t)i_samples / 
287                  (mtime_t)p_wf->i_samplepersec;
288
289     i_bytes = i_samples * p_wf->i_channels * ( p_wf->i_bitspersample + 7 ) / 8;
290     
291     if( p_wf->i_blockalign > 0 )
292     {
293         if( ( i_modulo = i_bytes % p_wf->i_blockalign ) != 0 )
294         {
295             i_bytes += p_wf->i_blockalign - i_modulo;
296         }
297     }
298
299     return( ReadPES( p_input, pp_pes, i_bytes ) );
300 }
301
302 /*****************************************************************************
303  * WAVInit: check file and initializes structures
304  *****************************************************************************/
305 static int WAVInit( vlc_object_t * p_this )
306 {   
307     input_thread_t *p_input = (input_thread_t *)p_this;
308     u8  *p_peek;
309     u32 i_size;
310     
311     demux_sys_t *p_demux;
312     
313
314
315     /* Initialize access plug-in structures. */
316     if( p_input->i_mtu == 0 )
317     {
318         /* Improve speed. */
319         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
320     }
321
322     /* a little test to see if it's a wav file */
323     if( input_Peek( p_input, &p_peek, 12 ) < 12 )
324     {
325         msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
326         return( -1 );
327     }
328
329     if( ( GetDWLE( p_peek ) != CreateDWLE( 'R', 'I', 'F', 'F' ) )||
330         ( GetDWLE( p_peek + 8 ) != CreateDWLE( 'W', 'A', 'V', 'E' ) ) )
331     {
332         msg_Warn( p_input, "WAV plugin discarded (not a valid file)" );
333         return( -1 );
334     }
335     i_size = GetDWLE( p_peek + 4 );
336     SkipBytes( p_input, 12 );
337
338     if( !FindTag( p_input, CreateDWLE( 'f', 'm', 't' ,' ' ) ) )
339     {
340         msg_Err( p_input, "cannot find \"fmt \" tag" );
341         return( -1 );
342     }
343
344     /* create our structure that will contains all data */
345     if( !( p_input->p_demux_data = 
346                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
347     {
348         msg_Err( p_input, "out of memory" );
349         return( -1 );
350     }
351     memset( p_demux, 0, sizeof( demux_sys_t ) );
352        
353     /* Load waveformatex_t header */
354     if( !LoadTag_fmt( p_input, p_demux ) )
355     {
356         msg_Err( p_input, "cannot load \"fmt \" tag" );
357         FREE( p_demux );
358         return( -1 );
359     }
360     msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
361             p_demux->format.i_format,
362             p_demux->format.i_channels,
363             p_demux->format.i_samplepersec,
364             p_demux->format.i_avgbytespersec/1024,
365             p_demux->format.i_blockalign,
366             p_demux->format.i_bitspersample,
367             p_demux->format.i_size );
368
369     if( !FindTag( p_input, CreateDWLE( 'd', 'a', 't', 'a' ) ) )
370     {
371         msg_Err( p_input, "cannot find \"data\" tag" );
372         FREE( p_demux->p_wf );
373         FREE( p_demux->format.p_data );
374         FREE( p_demux );
375         return( -1 );
376     }
377     if( input_Peek( p_input, &p_peek, 8 ) < 8 )
378     {
379         msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
380         FREE( p_demux->p_wf );
381         FREE( p_demux->format.p_data );
382         FREE( p_demux );
383         return( -1 );
384     }
385
386     p_demux->i_data_pos = TellAbsolute( p_input ) + 8;
387     p_demux->i_data_size = GetDWLE( p_peek + 4 );
388     SkipBytes( p_input, 8 );
389
390     /* XXX p_demux->psz_demux shouldn't be NULL ! */
391     switch( p_demux->format.i_format )
392     {
393         case( 0x01 ):
394             msg_Dbg( p_input,"found raw pcm audio format" );
395             p_demux->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
396             p_demux->GetFrame = PCM_GetFrame;
397             p_demux->psz_demux = strdup( "" );
398             break;
399         case( 0x50 ):
400         case( 0x55 ):
401             msg_Dbg( p_input, "found mpeg audio format" );
402             p_demux->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
403             p_demux->GetFrame = NULL;
404             p_demux->psz_demux = strdup( "mpegaudio" );
405             break;
406         case( 0x2000 ):
407             msg_Dbg( p_input,"found a52 audio format" );
408             p_demux->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
409             p_demux->GetFrame = NULL;
410             p_demux->psz_demux = strdup( "a52" );
411             break;
412         default:
413             msg_Warn( p_input,"unrecognize audio format(0x%x)", 
414                       p_demux->format.i_format );
415             p_demux->i_fourcc = 
416                 VLC_FOURCC( 'm', 's', 
417                             (p_demux->format.i_format >> 8)&0xff,
418                             (p_demux->format.i_format )&0xff);
419             p_demux->GetFrame = NULL;
420             p_demux->psz_demux = strdup( "" );
421             break;
422     }
423     
424     if( p_demux->GetFrame )
425     {
426         msg_Dbg( p_input, "using internal demux" );
427
428         p_input->pf_demux = WAVDemux;
429         p_input->p_demux_data = p_demux;
430         
431         /*  create one program */
432         vlc_mutex_lock( &p_input->stream.stream_lock );
433         if( input_InitStream( p_input, 0 ) == -1)
434         {
435             vlc_mutex_unlock( &p_input->stream.stream_lock );
436             msg_Err( p_input, "cannot init stream" );
437             // FIXME 
438             return( -1 );
439         }
440         if( input_AddProgram( p_input, 0, 0) == NULL )
441         {
442             vlc_mutex_unlock( &p_input->stream.stream_lock );
443             msg_Err( p_input, "cannot add program" );
444             // FIXME 
445             return( -1 );
446         }
447         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
448         p_input->stream.i_mux_rate = 0 ; /* FIXME */
449
450         p_demux->p_es = input_AddES( p_input,
451                                      p_input->stream.p_selected_program, 1,
452                                      p_demux->i_wf );
453         p_demux->p_es->i_stream_id = 1;
454         p_demux->p_es->i_fourcc = p_demux->i_fourcc;
455         p_demux->p_es->i_cat = AUDIO_ES;
456         if( p_demux->i_wf > 0 && p_demux->p_wf )
457         {
458             memcpy( p_demux->p_es->p_demux_data,
459                     p_demux->p_wf,
460                     p_demux->i_wf );
461         }
462         
463         input_SelectES( p_input, p_demux->p_es );
464         
465         p_input->stream.p_selected_program->b_is_ok = 1;
466         vlc_mutex_unlock( &p_input->stream.stream_lock );
467     }
468     else
469     {
470         char *psz_sav;
471         /* call an external demux */
472         msg_Warn( p_input, "unsupported formattag, using external demux" );
473         
474         psz_sav = p_input->psz_demux;
475         p_input->psz_demux = p_demux->psz_demux;
476
477         p_demux->p_demux = module_Need( p_input, "demux", NULL );
478         
479         p_input->psz_demux = psz_sav;
480         
481         if( !p_demux->p_demux )
482         {
483             msg_Err( p_input, 
484                      "cannot get external demux for formattag 0x%x",
485                      p_demux->format.i_format );
486             FREE( p_demux->psz_demux );
487             FREE( p_demux->p_wf );
488             FREE( p_demux->format.p_data );
489             FREE( p_demux );
490             return( -1 );
491         }
492         /* save value and switch back */
493         p_demux->pf_demux = p_input->pf_demux;
494         p_demux->p_demux_data = p_input->p_demux_data;
495
496         p_input->pf_demux = WAVCallDemux;
497         p_input->p_demux_data = p_demux;
498
499     }
500
501
502     return( 0 );    
503 }
504
505 /*****************************************************************************
506  * WAVCallDemux: call true demux
507  *****************************************************************************
508  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
509  *****************************************************************************/
510 static int WAVCallDemux( input_thread_t *p_input )
511 {
512     demux_sys_t  *p_demux = p_input->p_demux_data;
513     int i_status;
514     char *psz_sav;
515     
516     /* save context */
517     psz_sav = p_input->psz_demux;
518
519     /* switch context */
520     p_input->pf_demux = p_demux->pf_demux;
521     p_input->p_demux_data = p_demux->p_demux_data;
522     p_input->psz_demux = p_demux->psz_demux;
523
524     /* call demux */
525     i_status = p_input->pf_demux( p_input );
526
527     /* save (new?) state */
528     p_demux->pf_demux = p_input->pf_demux;
529     p_demux->p_demux_data = p_input->p_demux_data;
530     
531     /* switch back */
532     p_input->psz_demux = psz_sav;
533     p_input->pf_demux = WAVCallDemux;
534     p_input->p_demux_data = p_demux;
535
536     return( i_status );
537 }
538
539 /*****************************************************************************
540  * WAVDemux: read packet and send them to decoders
541  *****************************************************************************
542  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
543  *****************************************************************************/
544 static int WAVDemux( input_thread_t *p_input )
545 {
546     demux_sys_t  *p_demux = p_input->p_demux_data;
547     pes_packet_t *p_pes;
548     mtime_t      i_length;
549
550     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
551     {
552         off_t   i_offset;
553
554         i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
555         if( i_offset < 0 )
556         {
557             SeekAbsolute( p_input, p_demux->i_data_pos );
558         }
559         else
560         if( p_demux->format.i_blockalign != 0 )
561         {
562             
563             i_offset = i_offset - i_offset % p_demux->format.i_blockalign;
564             SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
565         }
566     }
567     
568     input_ClockManageRef( p_input,
569                           p_input->stream.p_selected_program,
570                           p_demux->i_pcr );
571
572     if( TellAbsolute( p_input ) >= p_demux->i_data_pos + p_demux->i_data_size )
573     {
574         return( 0 ); // EOF
575     }
576
577     if( !p_demux->GetFrame( p_input, &p_demux->format, &p_pes, &i_length ) )
578     {
579         msg_Warn( p_input, "failed to get one frame" );
580         return( 0 );
581     }
582
583     p_pes->i_dts = 
584         p_pes->i_pts = input_ClockGetTS( p_input, 
585                                          p_input->stream.p_selected_program,
586                                          p_demux->i_pcr );
587    
588     if( !p_demux->p_es->p_decoder_fifo )
589     {
590         msg_Err( p_input, "no audio decoder" );
591         input_DeletePES( p_input->p_method_data, p_pes );
592         return( -1 );
593     }
594     else
595     {
596         input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
597     }
598     
599     p_demux->i_pcr += i_length * 9 / 100;
600     return( 1 );
601 }
602
603 /*****************************************************************************
604  * WAVEnd: frees unused data
605  *****************************************************************************/
606 static void __WAVEnd ( vlc_object_t * p_this )
607 {   
608     input_thread_t *  p_input = (input_thread_t *)p_this;
609     demux_sys_t *p_demux = p_input->p_demux_data;
610     
611     FREE( p_demux->p_wf );
612     FREE( p_demux->format.p_data );
613     FREE( p_demux->psz_demux );
614     
615     if( p_demux->p_demux )
616     {
617         module_Unneed( p_input, p_demux->p_demux );
618     }
619
620 }
621