1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.14 2003/03/11 06:45:59 fenrir 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>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int WAVInit ( vlc_object_t * );
39 static void __WAVEnd ( vlc_object_t * );
40 static int WAVDemux ( input_thread_t * );
41 static int WAVCallDemux ( input_thread_t * );
43 #define WAVEnd(a) __WAVEnd(VLC_OBJECT(a))
45 /*****************************************************************************
47 *****************************************************************************/
49 set_description( _("WAV demuxer") );
50 set_capability( "demux", 142 );
51 set_callbacks( WAVInit, __WAVEnd );
54 /*****************************************************************************
55 * Declaration of local function
56 *****************************************************************************/
58 #define FREE( p ) if( p ) free( p ); (p) = NULL
60 #define __EVEN( x ) ( (x)%2 != 0 ) ? ((x)+1) : (x)
62 /* Some functions to manipulate memory */
63 static uint16_t GetWLE( uint8_t *p_buff )
65 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
68 static uint32_t GetDWLE( uint8_t *p_buff )
70 return( p_buff[0] + ( p_buff[1] <<8 ) +
71 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
74 static uint32_t CreateDWLE( int a, int b, int c, int d )
76 return( a + ( b << 8 ) + ( c << 16 ) + ( d << 24 ) );
80 static off_t TellAbsolute( input_thread_t *p_input )
84 vlc_mutex_lock( &p_input->stream.stream_lock );
86 i_pos= p_input->stream.p_selected_area->i_tell;
88 vlc_mutex_unlock( &p_input->stream.stream_lock );
93 /* return 1 if success, 0 if fail */
94 static int ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
96 data_packet_t *p_data;
109 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
114 memcpy( p_buff, p_data->p_payload_start, i_read );
115 input_DeletePacket( p_input->p_method_data, p_data );
126 static int SeekAbsolute( input_thread_t *p_input,
131 if( i_pos >= p_input->stream.p_selected_area->i_size )
137 i_skip = i_pos - TellAbsolute( p_input );
140 return( VLC_SUCCESS );
142 if( i_skip < 0 && !p_input->stream.b_seekable )
144 return( VLC_EGENERIC );
146 else if( !p_input->stream.b_seekable ||
147 ( i_skip > 0 && i_skip < 1024 && p_input->stream.i_method != INPUT_METHOD_FILE ) )
154 i_read = ReadData( p_input, dummy, __MIN( i_skip, 1024 ) );
157 return( VLC_EGENERIC );
161 return( VLC_SUCCESS );
165 input_AccessReinit( p_input );
166 p_input->pf_seek( p_input, i_pos );
167 return( VLC_SUCCESS );
171 static int SkipBytes( input_thread_t *p_input, int i_skip )
173 return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
176 static int ReadPES( input_thread_t *p_input,
177 pes_packet_t **pp_pes,
184 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
186 msg_Err( p_input, "cannot allocate new PES" );
192 data_packet_t *p_data;
195 if( (i_read = input_SplitBuffer( p_input,
197 __MIN( i_size, 1024 ) ) ) <= 0 )
199 input_DeletePES( p_input->p_method_data, p_pes );
202 if( !p_pes->p_first )
204 p_pes->p_first = p_data;
205 p_pes->i_nb_data = 1;
206 p_pes->i_pes_size = i_read;
210 p_pes->p_last->p_next = p_data;
212 p_pes->i_pes_size += i_read;
214 p_pes->p_last = p_data;
221 static int FindTag( input_thread_t *p_input, uint32_t i_tag )
230 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
232 msg_Err( p_input, "cannot peek()" );
236 i_id = GetDWLE( p_peek );
237 i_size = GetDWLE( p_peek + 4 );
239 msg_Dbg( p_input, "FindTag: tag:%4.4s size:%d", (char*)&i_id, i_size );
242 /* Yes, we have found the good tag */
245 if( !SkipBytes( p_input, __EVEN( i_size ) + 8 ) )
252 static int LoadTag_fmt( input_thread_t *p_input,
253 demux_sys_t *p_demux )
260 if( input_Peek( p_input, &p_peek , 8 ) < 8 )
265 p_demux->i_wf = i_size = GetDWLE( p_peek + 4 );
266 SkipBytes( p_input, 8 );
269 SkipBytes( p_input, i_size );
272 p_wf = p_demux->p_wf = malloc( __MAX( i_size, sizeof( WAVEFORMATEX) ) );
273 ReadData( p_input, (uint8_t*)p_wf, __EVEN( i_size ) );
275 p_wf->wFormatTag = GetWLE( (uint8_t*)&p_demux->p_wf->wFormatTag );
276 p_wf->nChannels = GetWLE( (uint8_t*)&p_demux->p_wf->nChannels );
277 p_wf->nSamplesPerSec = GetWLE( (uint8_t*)&p_demux->p_wf->nSamplesPerSec );
278 p_wf->nAvgBytesPerSec = GetWLE( (uint8_t*)&p_demux->p_wf->nAvgBytesPerSec );
279 p_wf->nBlockAlign = GetWLE( (uint8_t*)&p_demux->p_wf->nBlockAlign );
280 p_wf->wBitsPerSample = GetWLE( (uint8_t*)&p_demux->p_wf->wBitsPerSample );
281 if( i_size >= sizeof( WAVEFORMATEX) )
283 p_wf->cbSize = GetWLE( (uint8_t*)&p_demux->p_wf->cbSize );
290 msg_Dbg( p_input, "loaded \"fmt \" chunk" );
294 static int PCM_GetFrame( input_thread_t *p_input,
296 pes_packet_t **pp_pes,
304 /* read samples for 50ms of */
305 i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
308 *pi_length = (mtime_t)1000000 *
310 (mtime_t)p_wf->nSamplesPerSec;
312 i_bytes = i_samples * p_wf->nChannels * ( (p_wf->wBitsPerSample + 7) / 8 );
314 if( p_wf->nBlockAlign > 0 )
316 if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
318 i_bytes += p_wf->nBlockAlign - i_modulo;
322 return( ReadPES( p_input, pp_pes, i_bytes ) );
325 static int MS_ADPCM_GetFrame( input_thread_t *p_input,
327 pes_packet_t **pp_pes,
332 i_samples = 2 + 2 * ( p_wf->nBlockAlign -
333 7 * p_wf->nChannels ) / p_wf->nChannels;
335 *pi_length = (mtime_t)1000000 *
337 (mtime_t)p_wf->nSamplesPerSec;
339 return( ReadPES( p_input, pp_pes, p_wf->nBlockAlign ) );
342 static int IMA_ADPCM_GetFrame( input_thread_t *p_input,
344 pes_packet_t **pp_pes,
349 i_samples = 2 * ( p_wf->nBlockAlign -
350 4 * p_wf->nChannels ) / p_wf->nChannels;
352 *pi_length = (mtime_t)1000000 *
354 (mtime_t)p_wf->nSamplesPerSec;
356 return( ReadPES( p_input, pp_pes, p_wf->nBlockAlign ) );
359 /*****************************************************************************
360 * WAVInit: check file and initializes structures
361 *****************************************************************************/
362 static int WAVInit( vlc_object_t * p_this )
364 input_thread_t *p_input = (input_thread_t *)p_this;
368 demux_sys_t *p_demux;
372 /* Initialize access plug-in structures. */
373 if( p_input->i_mtu == 0 )
376 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
379 /* a little test to see if it's a wav file */
380 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
382 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
386 if( ( GetDWLE( p_peek ) != CreateDWLE( 'R', 'I', 'F', 'F' ) )||
387 ( GetDWLE( p_peek + 8 ) != CreateDWLE( 'W', 'A', 'V', 'E' ) ) )
389 msg_Warn( p_input, "WAV plugin discarded (not a valid file)" );
392 i_size = GetDWLE( p_peek + 4 );
393 SkipBytes( p_input, 12 );
395 if( !FindTag( p_input, CreateDWLE( 'f', 'm', 't' ,' ' ) ) )
397 msg_Err( p_input, "cannot find \"fmt \" tag" );
401 /* create our structure that will contains all data */
402 if( !( p_input->p_demux_data =
403 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
405 msg_Err( p_input, "out of memory" );
408 memset( p_demux, 0, sizeof( demux_sys_t ) );
410 /* Load WAVEFORMATEX header */
411 if( !LoadTag_fmt( p_input, p_demux ) )
413 msg_Err( p_input, "cannot load \"fmt \" tag" );
417 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
418 p_demux->p_wf->wFormatTag,
419 p_demux->p_wf->nChannels,
420 p_demux->p_wf->nSamplesPerSec,
421 p_demux->p_wf->nAvgBytesPerSec / 1024,
422 p_demux->p_wf->nBlockAlign,
423 p_demux->p_wf->wBitsPerSample,
424 p_demux->p_wf->cbSize );
426 if( !FindTag( p_input, CreateDWLE( 'd', 'a', 't', 'a' ) ) )
428 msg_Err( p_input, "cannot find \"data\" tag" );
429 FREE( p_demux->p_wf );
433 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
435 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
436 FREE( p_demux->p_wf );
441 p_demux->i_data_pos = TellAbsolute( p_input ) + 8;
442 p_demux->i_data_size = GetDWLE( p_peek + 4 );
443 SkipBytes( p_input, 8 );
445 /* XXX p_demux->psz_demux shouldn't be NULL ! */
446 switch( p_demux->p_wf->wFormatTag )
448 case( WAVE_FORMAT_PCM ):
449 msg_Dbg( p_input,"found raw pcm audio format" );
450 p_demux->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
451 p_demux->GetFrame = PCM_GetFrame;
452 p_demux->psz_demux = strdup( "" );
454 case( WAVE_FORMAT_MULAW ):
455 msg_Dbg( p_input,"found mulaw pcm audio format" );
456 p_demux->i_fourcc = VLC_FOURCC( 'u', 'l', 'a', 'w' );
457 p_demux->GetFrame = PCM_GetFrame;
458 p_demux->psz_demux = strdup( "" );
460 case( WAVE_FORMAT_ALAW ):
461 msg_Dbg( p_input,"found alaw pcm audio format" );
462 p_demux->i_fourcc = VLC_FOURCC( 'a', 'l', 'a', 'w' );
463 p_demux->GetFrame = PCM_GetFrame;
464 p_demux->psz_demux = strdup( "" );
466 case( WAVE_FORMAT_MPEG ):
467 case( WAVE_FORMAT_MPEGLAYER3 ):
468 msg_Dbg( p_input, "found mpeg audio format" );
469 p_demux->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
470 p_demux->GetFrame = NULL;
471 p_demux->psz_demux = strdup( "mpegaudio" );
473 case( WAVE_FORMAT_A52 ):
474 msg_Dbg( p_input,"found a52 audio format" );
475 p_demux->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
476 p_demux->GetFrame = NULL;
477 p_demux->psz_demux = strdup( "a52" );
479 case( WAVE_FORMAT_ADPCM ):
480 msg_Dbg( p_input, "found ms adpcm audio format" );
481 p_demux->i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x02 );
482 p_demux->GetFrame = MS_ADPCM_GetFrame;
483 p_demux->psz_demux = strdup( "" );
485 case( WAVE_FORMAT_IMA_ADPCM ):
486 msg_Dbg( p_input, "found ima adpcm audio format" );
487 p_demux->i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x11 );
488 p_demux->GetFrame = IMA_ADPCM_GetFrame;
489 p_demux->psz_demux = strdup( "" );
492 msg_Warn( p_input,"unrecognize audio format(0x%x)",
493 p_demux->p_wf->wFormatTag );
495 VLC_FOURCC( 'm', 's',
496 (p_demux->p_wf->wFormatTag >> 8)&0xff,
497 (p_demux->p_wf->wFormatTag )&0xff);
498 p_demux->GetFrame = NULL;
499 p_demux->psz_demux = strdup( "" );
503 if( p_demux->GetFrame )
505 msg_Dbg( p_input, "using internal demux" );
507 p_input->pf_demux = WAVDemux;
508 p_input->p_demux_data = p_demux;
510 /* create one program */
511 vlc_mutex_lock( &p_input->stream.stream_lock );
512 if( input_InitStream( p_input, 0 ) == -1)
514 vlc_mutex_unlock( &p_input->stream.stream_lock );
515 msg_Err( p_input, "cannot init stream" );
519 if( input_AddProgram( p_input, 0, 0) == NULL )
521 vlc_mutex_unlock( &p_input->stream.stream_lock );
522 msg_Err( p_input, "cannot add program" );
526 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
527 p_input->stream.i_mux_rate = 0 ; /* FIXME */
529 p_demux->p_es = input_AddES( p_input,
530 p_input->stream.p_selected_program, 1,
532 p_demux->p_es->i_stream_id = 1;
533 p_demux->p_es->i_fourcc = p_demux->i_fourcc;
534 p_demux->p_es->i_cat = AUDIO_ES;
535 p_demux->p_es->p_waveformatex = malloc( p_demux->i_wf );
536 memcpy( p_demux->p_es->p_waveformatex, p_demux->p_wf, p_demux->i_wf );
538 input_SelectES( p_input, p_demux->p_es );
540 p_input->stream.p_selected_program->b_is_ok = 1;
541 vlc_mutex_unlock( &p_input->stream.stream_lock );
546 /* call an external demux */
547 msg_Warn( p_input, "unsupported formattag, using external demux" );
549 psz_sav = p_input->psz_demux;
550 p_input->psz_demux = p_demux->psz_demux;
552 p_demux->p_demux = module_Need( p_input, "demux", NULL );
554 p_input->psz_demux = psz_sav;
556 if( !p_demux->p_demux )
559 "cannot get external demux for formattag 0x%x",
560 p_demux->p_wf->wFormatTag );
561 FREE( p_demux->psz_demux );
562 FREE( p_demux->p_wf );
566 /* save value and switch back */
567 p_demux->pf_demux = p_input->pf_demux;
568 p_demux->p_demux_data = p_input->p_demux_data;
570 p_input->pf_demux = WAVCallDemux;
571 p_input->p_demux_data = p_demux;
578 /*****************************************************************************
579 * WAVCallDemux: call true demux
580 *****************************************************************************
581 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
582 *****************************************************************************/
583 static int WAVCallDemux( input_thread_t *p_input )
585 demux_sys_t *p_demux = p_input->p_demux_data;
590 psz_sav = p_input->psz_demux;
593 p_input->pf_demux = p_demux->pf_demux;
594 p_input->p_demux_data = p_demux->p_demux_data;
595 p_input->psz_demux = p_demux->psz_demux;
598 i_status = p_input->pf_demux( p_input );
600 /* save (new?) state */
601 p_demux->pf_demux = p_input->pf_demux;
602 p_demux->p_demux_data = p_input->p_demux_data;
605 p_input->psz_demux = psz_sav;
606 p_input->pf_demux = WAVCallDemux;
607 p_input->p_demux_data = p_demux;
612 /*****************************************************************************
613 * WAVDemux: read packet and send them to decoders
614 *****************************************************************************
615 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
616 *****************************************************************************/
617 static int WAVDemux( input_thread_t *p_input )
619 demux_sys_t *p_demux = p_input->p_demux_data;
623 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
627 i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
632 if( p_demux->p_wf->nBlockAlign != 0 )
634 i_offset += p_demux->p_wf->nBlockAlign -
635 i_offset % p_demux->p_wf->nBlockAlign;
637 SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
640 input_ClockManageRef( p_input,
641 p_input->stream.p_selected_program,
644 if( TellAbsolute( p_input )
645 >= (off_t)(p_demux->i_data_pos + p_demux->i_data_size) )
650 if( !p_demux->GetFrame( p_input, p_demux->p_wf, &p_pes, &i_length ) )
652 msg_Warn( p_input, "failed to get one frame" );
657 p_pes->i_pts = input_ClockGetTS( p_input,
658 p_input->stream.p_selected_program,
661 if( !p_demux->p_es->p_decoder_fifo )
663 msg_Err( p_input, "no audio decoder" );
664 input_DeletePES( p_input->p_method_data, p_pes );
669 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
672 p_demux->i_pcr += i_length * 9 / 100;
676 /*****************************************************************************
677 * WAVEnd: frees unused data
678 *****************************************************************************/
679 static void __WAVEnd ( vlc_object_t * p_this )
681 input_thread_t * p_input = (input_thread_t *)p_this;
682 demux_sys_t *p_demux = p_input->p_demux_data;
684 FREE( p_demux->p_wf );
685 FREE( p_demux->psz_demux );
687 if( p_demux->p_demux )
692 psz_sav = p_input->psz_demux;
695 p_input->pf_demux = p_demux->pf_demux;
696 p_input->p_demux_data = p_demux->p_demux_data;
697 p_input->psz_demux = p_demux->psz_demux;
700 module_Unneed( p_input, p_demux->p_demux );
703 p_input->psz_demux = psz_sav;
704 p_input->p_demux_data = p_demux;
707 FREE( p_input->p_demux_data );