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