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>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
27 #include <string.h> /* strdup() */
30 #include <vlc/input.h>
34 /*****************************************************************************
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 * );
42 #define WAVEnd(a) __WAVEnd(VLC_OBJECT(a))
44 /*****************************************************************************
46 *****************************************************************************/
48 set_description( "WAV demuxer" );
49 set_capability( "demux", 142 );
50 set_callbacks( WAVInit, __WAVEnd );
53 /*****************************************************************************
54 * Declaration of local function
55 *****************************************************************************/
57 #define FREE( p ) if( p ) free( p ); (p) = NULL
59 #define __EVEN( x ) ( (x)%2 != 0 ) ? ((x)+1) : (x)
61 /* Some functions to manipulate memory */
62 static u16 GetWLE( u8 *p_buff )
64 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
67 static u32 GetDWLE( u8 *p_buff )
69 return( p_buff[0] + ( p_buff[1] <<8 ) +
70 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
73 static u32 CreateDWLE( int a, int b, int c, int d )
75 return( a + ( b << 8 ) + ( c << 16 ) + ( d << 24 ) );
79 static off_t TellAbsolute( input_thread_t *p_input )
83 vlc_mutex_lock( &p_input->stream.stream_lock );
85 i_pos= p_input->stream.p_selected_area->i_tell;
86 // - ( p_input->p_last_data - p_input->p_current_data );
88 vlc_mutex_unlock( &p_input->stream.stream_lock );
93 static int SeekAbsolute( input_thread_t *p_input,
98 if( i_pos >= p_input->stream.p_selected_area->i_size )
103 i_filepos = TellAbsolute( p_input );
104 if( i_pos != i_filepos )
106 p_input->pf_seek( p_input, i_pos );
107 input_AccessReinit( p_input );
112 static int SkipBytes( input_thread_t *p_input, int i_skip )
114 return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
117 /* return 1 if success, 0 if fail */
118 static int ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
120 data_packet_t *p_data;
132 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
137 memcpy( p_buff, p_data->p_payload_start, i_read );
138 input_DeletePacket( p_input->p_method_data, p_data );
149 static int ReadPES( input_thread_t *p_input,
150 pes_packet_t **pp_pes,
157 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
159 msg_Err( p_input, "cannot allocate new PES" );
165 data_packet_t *p_data;
168 if( (i_read = input_SplitBuffer( p_input,
170 __MIN( i_size, 1024 ) ) ) <= 0 )
172 input_DeletePES( p_input->p_method_data, p_pes );
175 if( !p_pes->p_first )
177 p_pes->p_first = p_data;
178 p_pes->i_nb_data = 1;
179 p_pes->i_pes_size = i_read;
183 p_pes->p_last->p_next = p_data;
185 p_pes->i_pes_size += i_read;
187 p_pes->p_last = p_data;
194 static int FindTag( input_thread_t *p_input, u32 i_tag )
203 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
205 msg_Err( p_input, "cannot peek()" );
209 i_id = GetDWLE( p_peek );
210 i_size = GetDWLE( p_peek + 4 );
212 msg_Dbg( p_input, "FindTag: tag:%4.4s size:%d", &i_id, i_size );
215 /* Yes, we have found the good tag */
218 if( !SkipBytes( p_input, __EVEN( i_size ) + 8 ) )
225 static int LoadTag_fmt( input_thread_t *p_input,
226 demux_sys_t *p_demux )
231 if( input_Peek( p_input, &p_peek , 8 ) < 8 )
236 p_demux->i_wf = i_size = GetDWLE( p_peek + 4 );
237 SkipBytes( p_input, 8 );
240 SkipBytes( p_input, i_size );
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);
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,
257 p_demux->format.i_size );
261 p_demux->format.i_size = 0;
262 p_demux->format.p_data = NULL;
265 msg_Dbg( p_input, "loaded \"fmt \" chunk" );
269 static int PCM_GetFrame( input_thread_t *p_input,
270 waveformatex_t *p_wf,
271 pes_packet_t **pp_pes,
279 /* read samples for 50ms of */
280 i_samples = __MAX( p_wf->i_samplepersec / 20, 1 );
283 *pi_length = (mtime_t)1000000 *
285 (mtime_t)p_wf->i_samplepersec;
287 i_bytes = i_samples * p_wf->i_channels * ( p_wf->i_bitspersample + 7 ) / 8;
289 if( p_wf->i_blockalign > 0 )
291 if( ( i_modulo = i_bytes % p_wf->i_blockalign ) != 0 )
293 i_bytes += p_wf->i_blockalign - i_modulo;
297 return( ReadPES( p_input, pp_pes, i_bytes ) );
300 /*****************************************************************************
301 * WAVInit: check file and initializes structures
302 *****************************************************************************/
303 static int WAVInit( vlc_object_t * p_this )
305 input_thread_t *p_input = (input_thread_t *)p_this;
309 demux_sys_t *p_demux;
313 /* Initialize access plug-in structures. */
314 if( p_input->i_mtu == 0 )
317 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
320 /* a little test to see if it's a wav file */
321 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
323 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
327 if( ( GetDWLE( p_peek ) != CreateDWLE( 'R', 'I', 'F', 'F' ) )||
328 ( GetDWLE( p_peek + 8 ) != CreateDWLE( 'W', 'A', 'V', 'E' ) ) )
330 msg_Warn( p_input, "WAV plugin discarded (not a valid file)" );
333 i_size = GetDWLE( p_peek + 4 );
334 SkipBytes( p_input, 12 );
336 if( !FindTag( p_input, CreateDWLE( 'f', 'm', 't' ,' ' ) ) )
338 msg_Err( p_input, "cannot find \"fmt \" tag" );
342 /* create our structure that will contains all data */
343 if( !( p_input->p_demux_data =
344 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
346 msg_Err( p_input, "out of memory" );
349 memset( p_demux, 0, sizeof( demux_sys_t ) );
351 /* Load waveformatex_t header */
352 if( !LoadTag_fmt( p_input, p_demux ) )
354 msg_Err( p_input, "cannot load \"fmt \" tag" );
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 );
367 if( !FindTag( p_input, CreateDWLE( 'd', 'a', 't', 'a' ) ) )
369 msg_Err( p_input, "cannot find \"data\" tag" );
370 FREE( p_demux->p_wf );
371 FREE( p_demux->format.p_data );
375 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
377 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
378 FREE( p_demux->p_wf );
379 FREE( p_demux->format.p_data );
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 );
388 /* XXX p_demux->psz_demux shouldn't be NULL ! */
389 switch( p_demux->format.i_format )
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( "" );
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" );
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" );
411 msg_Warn( p_input,"unrecognize audio format(0x%x)",
412 p_demux->format.i_format );
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( "" );
422 if( p_demux->GetFrame )
424 msg_Dbg( p_input, "using internal demux" );
426 p_input->pf_demux = WAVDemux;
427 p_input->p_demux_data = p_demux;
429 /* create one program */
430 vlc_mutex_lock( &p_input->stream.stream_lock );
431 if( input_InitStream( p_input, 0 ) == -1)
433 vlc_mutex_unlock( &p_input->stream.stream_lock );
434 msg_Err( p_input, "cannot init stream" );
438 if( input_AddProgram( p_input, 0, 0) == NULL )
440 vlc_mutex_unlock( &p_input->stream.stream_lock );
441 msg_Err( p_input, "cannot add program" );
445 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
446 p_input->stream.i_mux_rate = 0 ; /* FIXME */
448 p_demux->p_es = input_AddES( p_input,
449 p_input->stream.p_selected_program, 1,
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 )
456 memcpy( p_demux->p_es->p_demux_data,
461 input_SelectES( p_input, p_demux->p_es );
463 p_input->stream.p_selected_program->b_is_ok = 1;
464 vlc_mutex_unlock( &p_input->stream.stream_lock );
469 /* call an external demux */
470 msg_Warn( p_input, "unsupported formattag, using external demux" );
472 psz_sav = p_input->psz_demux;
473 p_input->psz_demux = p_demux->psz_demux;
475 p_demux->p_demux = module_Need( p_input, "demux", NULL );
477 p_input->psz_demux = psz_sav;
479 if( !p_demux->p_demux )
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 );
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;
494 p_input->pf_demux = WAVCallDemux;
495 p_input->p_demux_data = p_demux;
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 )
510 demux_sys_t *p_demux = p_input->p_demux_data;
515 psz_sav = p_input->psz_demux;
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;
523 i_status = p_input->pf_demux( p_input );
525 /* save (new?) state */
526 p_demux->pf_demux = p_input->pf_demux;
527 p_demux->p_demux_data = p_input->p_demux_data;
530 p_input->psz_demux = psz_sav;
531 p_input->pf_demux = WAVCallDemux;
532 p_input->p_demux_data = p_demux;
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 )
544 demux_sys_t *p_demux = p_input->p_demux_data;
548 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
552 i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
555 SeekAbsolute( p_input, p_demux->i_data_pos );
558 if( p_demux->format.i_blockalign != 0 )
561 i_offset = i_offset - i_offset % p_demux->format.i_blockalign;
562 SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
566 input_ClockManageRef( p_input,
567 p_input->stream.p_selected_program,
570 if( TellAbsolute( p_input ) >= p_demux->i_data_pos + p_demux->i_data_size )
575 if( !p_demux->GetFrame( p_input, &p_demux->format, &p_pes, &i_length ) )
577 msg_Warn( p_input, "failed to get one frame" );
582 p_pes->i_pts = input_ClockGetTS( p_input,
583 p_input->stream.p_selected_program,
586 if( !p_demux->p_es->p_decoder_fifo )
588 msg_Err( p_input, "no audio decoder" );
589 input_DeletePES( p_input->p_method_data, p_pes );
594 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
597 p_demux->i_pcr += i_length * 9 / 100;
601 /*****************************************************************************
602 * WAVEnd: frees unused data
603 *****************************************************************************/
604 static void __WAVEnd ( vlc_object_t * p_this )
606 input_thread_t * p_input = (input_thread_t *)p_this;
607 demux_sys_t *p_demux = p_input->p_demux_data;
609 FREE( p_demux->p_wf );
610 FREE( p_demux->format.p_data );
611 FREE( p_demux->psz_demux );
613 if( p_demux->p_demux )
615 module_Unneed( p_input, p_demux->p_demux );