1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.13 2003/02/27 13:19:43 gbazin 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_MPEG ):
455 case( WAVE_FORMAT_MPEGLAYER3 ):
456 msg_Dbg( p_input, "found mpeg audio format" );
457 p_demux->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
458 p_demux->GetFrame = NULL;
459 p_demux->psz_demux = strdup( "mpegaudio" );
461 case( WAVE_FORMAT_A52 ):
462 msg_Dbg( p_input,"found a52 audio format" );
463 p_demux->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
464 p_demux->GetFrame = NULL;
465 p_demux->psz_demux = strdup( "a52" );
467 case( WAVE_FORMAT_ADPCM ):
468 msg_Dbg( p_input, "found ms adpcm audio format" );
469 p_demux->i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x02 );
470 p_demux->GetFrame = MS_ADPCM_GetFrame;
471 p_demux->psz_demux = strdup( "" );
473 case( WAVE_FORMAT_IMA_ADPCM ):
474 msg_Dbg( p_input, "found ima adpcm audio format" );
475 p_demux->i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x11 );
476 p_demux->GetFrame = IMA_ADPCM_GetFrame;
477 p_demux->psz_demux = strdup( "" );
480 msg_Warn( p_input,"unrecognize audio format(0x%x)",
481 p_demux->p_wf->wFormatTag );
483 VLC_FOURCC( 'm', 's',
484 (p_demux->p_wf->wFormatTag >> 8)&0xff,
485 (p_demux->p_wf->wFormatTag )&0xff);
486 p_demux->GetFrame = NULL;
487 p_demux->psz_demux = strdup( "" );
491 if( p_demux->GetFrame )
493 msg_Dbg( p_input, "using internal demux" );
495 p_input->pf_demux = WAVDemux;
496 p_input->p_demux_data = p_demux;
498 /* create one program */
499 vlc_mutex_lock( &p_input->stream.stream_lock );
500 if( input_InitStream( p_input, 0 ) == -1)
502 vlc_mutex_unlock( &p_input->stream.stream_lock );
503 msg_Err( p_input, "cannot init stream" );
507 if( input_AddProgram( p_input, 0, 0) == NULL )
509 vlc_mutex_unlock( &p_input->stream.stream_lock );
510 msg_Err( p_input, "cannot add program" );
514 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
515 p_input->stream.i_mux_rate = 0 ; /* FIXME */
517 p_demux->p_es = input_AddES( p_input,
518 p_input->stream.p_selected_program, 1,
520 p_demux->p_es->i_stream_id = 1;
521 p_demux->p_es->i_fourcc = p_demux->i_fourcc;
522 p_demux->p_es->i_cat = AUDIO_ES;
523 p_demux->p_es->p_waveformatex = malloc( p_demux->i_wf );
524 memcpy( p_demux->p_es->p_waveformatex, p_demux->p_wf, p_demux->i_wf );
526 input_SelectES( p_input, p_demux->p_es );
528 p_input->stream.p_selected_program->b_is_ok = 1;
529 vlc_mutex_unlock( &p_input->stream.stream_lock );
534 /* call an external demux */
535 msg_Warn( p_input, "unsupported formattag, using external demux" );
537 psz_sav = p_input->psz_demux;
538 p_input->psz_demux = p_demux->psz_demux;
540 p_demux->p_demux = module_Need( p_input, "demux", NULL );
542 p_input->psz_demux = psz_sav;
544 if( !p_demux->p_demux )
547 "cannot get external demux for formattag 0x%x",
548 p_demux->p_wf->wFormatTag );
549 FREE( p_demux->psz_demux );
550 FREE( p_demux->p_wf );
554 /* save value and switch back */
555 p_demux->pf_demux = p_input->pf_demux;
556 p_demux->p_demux_data = p_input->p_demux_data;
558 p_input->pf_demux = WAVCallDemux;
559 p_input->p_demux_data = p_demux;
566 /*****************************************************************************
567 * WAVCallDemux: call true demux
568 *****************************************************************************
569 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
570 *****************************************************************************/
571 static int WAVCallDemux( input_thread_t *p_input )
573 demux_sys_t *p_demux = p_input->p_demux_data;
578 psz_sav = p_input->psz_demux;
581 p_input->pf_demux = p_demux->pf_demux;
582 p_input->p_demux_data = p_demux->p_demux_data;
583 p_input->psz_demux = p_demux->psz_demux;
586 i_status = p_input->pf_demux( p_input );
588 /* save (new?) state */
589 p_demux->pf_demux = p_input->pf_demux;
590 p_demux->p_demux_data = p_input->p_demux_data;
593 p_input->psz_demux = psz_sav;
594 p_input->pf_demux = WAVCallDemux;
595 p_input->p_demux_data = p_demux;
600 /*****************************************************************************
601 * WAVDemux: read packet and send them to decoders
602 *****************************************************************************
603 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
604 *****************************************************************************/
605 static int WAVDemux( input_thread_t *p_input )
607 demux_sys_t *p_demux = p_input->p_demux_data;
611 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
615 i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
620 if( p_demux->p_wf->nBlockAlign != 0 )
622 i_offset += p_demux->p_wf->nBlockAlign -
623 i_offset % p_demux->p_wf->nBlockAlign;
625 SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
628 input_ClockManageRef( p_input,
629 p_input->stream.p_selected_program,
632 if( TellAbsolute( p_input )
633 >= (off_t)(p_demux->i_data_pos + p_demux->i_data_size) )
638 if( !p_demux->GetFrame( p_input, p_demux->p_wf, &p_pes, &i_length ) )
640 msg_Warn( p_input, "failed to get one frame" );
645 p_pes->i_pts = input_ClockGetTS( p_input,
646 p_input->stream.p_selected_program,
649 if( !p_demux->p_es->p_decoder_fifo )
651 msg_Err( p_input, "no audio decoder" );
652 input_DeletePES( p_input->p_method_data, p_pes );
657 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
660 p_demux->i_pcr += i_length * 9 / 100;
664 /*****************************************************************************
665 * WAVEnd: frees unused data
666 *****************************************************************************/
667 static void __WAVEnd ( vlc_object_t * p_this )
669 input_thread_t * p_input = (input_thread_t *)p_this;
670 demux_sys_t *p_demux = p_input->p_demux_data;
672 FREE( p_demux->p_wf );
673 FREE( p_demux->psz_demux );
675 if( p_demux->p_demux )
680 psz_sav = p_input->psz_demux;
683 p_input->pf_demux = p_demux->pf_demux;
684 p_input->p_demux_data = p_demux->p_demux_data;
685 p_input->psz_demux = p_demux->psz_demux;
688 module_Unneed( p_input, p_demux->p_demux );
691 p_input->psz_demux = psz_sav;
692 p_input->p_demux_data = p_demux;
695 FREE( p_input->p_demux_data );