1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.8 2002/12/18 14:17:10 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>
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 u16 GetWLE( u8 *p_buff )
65 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
68 static u32 GetDWLE( u8 *p_buff )
70 return( p_buff[0] + ( p_buff[1] <<8 ) +
71 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
74 static u32 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;
87 // - ( p_input->p_last_data - p_input->p_current_data );
89 vlc_mutex_unlock( &p_input->stream.stream_lock );
94 static int SeekAbsolute( input_thread_t *p_input,
99 if( i_pos >= p_input->stream.p_selected_area->i_size )
104 i_filepos = TellAbsolute( p_input );
105 if( i_pos != i_filepos )
107 p_input->pf_seek( p_input, i_pos );
108 input_AccessReinit( p_input );
113 static int SkipBytes( input_thread_t *p_input, int i_skip )
115 return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
118 /* return 1 if success, 0 if fail */
119 static int ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
121 data_packet_t *p_data;
133 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
138 memcpy( p_buff, p_data->p_payload_start, i_read );
139 input_DeletePacket( p_input->p_method_data, p_data );
150 static int ReadPES( input_thread_t *p_input,
151 pes_packet_t **pp_pes,
158 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
160 msg_Err( p_input, "cannot allocate new PES" );
166 data_packet_t *p_data;
169 if( (i_read = input_SplitBuffer( p_input,
171 __MIN( i_size, 1024 ) ) ) <= 0 )
173 input_DeletePES( p_input->p_method_data, p_pes );
176 if( !p_pes->p_first )
178 p_pes->p_first = p_data;
179 p_pes->i_nb_data = 1;
180 p_pes->i_pes_size = i_read;
184 p_pes->p_last->p_next = p_data;
186 p_pes->i_pes_size += i_read;
188 p_pes->p_last = p_data;
195 static int FindTag( input_thread_t *p_input, u32 i_tag )
204 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
206 msg_Err( p_input, "cannot peek()" );
210 i_id = GetDWLE( p_peek );
211 i_size = GetDWLE( p_peek + 4 );
213 msg_Dbg( p_input, "FindTag: tag:%4.4s size:%d", (char*)&i_id, i_size );
216 /* Yes, we have found the good tag */
219 if( !SkipBytes( p_input, __EVEN( i_size ) + 8 ) )
226 static int LoadTag_fmt( input_thread_t *p_input,
227 demux_sys_t *p_demux )
234 if( input_Peek( p_input, &p_peek , 8 ) < 8 )
239 p_demux->i_wf = i_size = GetDWLE( p_peek + 4 );
240 SkipBytes( p_input, 8 );
243 SkipBytes( p_input, i_size );
246 p_wf = p_demux->p_wf = malloc( __MAX( i_size, sizeof( WAVEFORMATEX) ) );
247 ReadData( p_input, (uint8_t*)p_wf, __EVEN( i_size ) );
249 p_wf->wFormatTag = GetWLE( (uint8_t*)&p_demux->p_wf->wFormatTag );
250 p_wf->nChannels = GetWLE( (uint8_t*)&p_demux->p_wf->nChannels );
251 p_wf->nSamplesPerSec = GetWLE( (uint8_t*)&p_demux->p_wf->nSamplesPerSec );
252 p_wf->nAvgBytesPerSec = GetWLE( (uint8_t*)&p_demux->p_wf->nAvgBytesPerSec );
253 p_wf->nBlockAlign = GetWLE( (uint8_t*)&p_demux->p_wf->nBlockAlign );
254 p_wf->wBitsPerSample = GetWLE( (uint8_t*)&p_demux->p_wf->wBitsPerSample );
255 if( i_size >= sizeof( WAVEFORMATEX) )
257 p_wf->cbSize = GetWLE( (uint8_t*)&p_demux->p_wf->cbSize );
264 msg_Dbg( p_input, "loaded \"fmt \" chunk" );
268 static int PCM_GetFrame( input_thread_t *p_input,
270 pes_packet_t **pp_pes,
278 /* read samples for 50ms of */
279 i_samples = __MAX( p_wf->nSamplesPerSec / 20, 1 );
282 *pi_length = (mtime_t)1000000 *
284 (mtime_t)p_wf->nSamplesPerSec;
286 i_bytes = i_samples * p_wf->nChannels * ( (p_wf->wBitsPerSample + 7) / 8 );
288 if( p_wf->nBlockAlign > 0 )
290 if( ( i_modulo = i_bytes % p_wf->nBlockAlign ) != 0 )
292 i_bytes += p_wf->nBlockAlign - i_modulo;
296 return( ReadPES( p_input, pp_pes, i_bytes ) );
299 static int MS_ADPCM_GetFrame( input_thread_t *p_input,
301 pes_packet_t **pp_pes,
306 i_samples = 2 + 2 * ( p_wf->nBlockAlign -
307 7 * p_wf->nChannels ) / p_wf->nChannels;
309 *pi_length = (mtime_t)1000000 *
311 (mtime_t)p_wf->nSamplesPerSec;
313 return( ReadPES( p_input, pp_pes, p_wf->nBlockAlign ) );
316 static int IMA_ADPCM_GetFrame( input_thread_t *p_input,
318 pes_packet_t **pp_pes,
323 i_samples = 2 * ( p_wf->nBlockAlign -
324 4 * p_wf->nChannels ) / p_wf->nChannels;
326 *pi_length = (mtime_t)1000000 *
328 (mtime_t)p_wf->nSamplesPerSec;
330 return( ReadPES( p_input, pp_pes, p_wf->nBlockAlign ) );
333 /*****************************************************************************
334 * WAVInit: check file and initializes structures
335 *****************************************************************************/
336 static int WAVInit( vlc_object_t * p_this )
338 input_thread_t *p_input = (input_thread_t *)p_this;
342 demux_sys_t *p_demux;
346 /* Initialize access plug-in structures. */
347 if( p_input->i_mtu == 0 )
350 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
353 /* a little test to see if it's a wav file */
354 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
356 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
360 if( ( GetDWLE( p_peek ) != CreateDWLE( 'R', 'I', 'F', 'F' ) )||
361 ( GetDWLE( p_peek + 8 ) != CreateDWLE( 'W', 'A', 'V', 'E' ) ) )
363 msg_Warn( p_input, "WAV plugin discarded (not a valid file)" );
366 i_size = GetDWLE( p_peek + 4 );
367 SkipBytes( p_input, 12 );
369 if( !FindTag( p_input, CreateDWLE( 'f', 'm', 't' ,' ' ) ) )
371 msg_Err( p_input, "cannot find \"fmt \" tag" );
375 /* create our structure that will contains all data */
376 if( !( p_input->p_demux_data =
377 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
379 msg_Err( p_input, "out of memory" );
382 memset( p_demux, 0, sizeof( demux_sys_t ) );
384 /* Load WAVEFORMATEX header */
385 if( !LoadTag_fmt( p_input, p_demux ) )
387 msg_Err( p_input, "cannot load \"fmt \" tag" );
391 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
392 p_demux->p_wf->wFormatTag,
393 p_demux->p_wf->nChannels,
394 p_demux->p_wf->nSamplesPerSec,
395 p_demux->p_wf->nAvgBytesPerSec / 1024,
396 p_demux->p_wf->nBlockAlign,
397 p_demux->p_wf->wBitsPerSample,
398 p_demux->p_wf->cbSize );
400 if( !FindTag( p_input, CreateDWLE( 'd', 'a', 't', 'a' ) ) )
402 msg_Err( p_input, "cannot find \"data\" tag" );
403 FREE( p_demux->p_wf );
407 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
409 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
410 FREE( p_demux->p_wf );
415 p_demux->i_data_pos = TellAbsolute( p_input ) + 8;
416 p_demux->i_data_size = GetDWLE( p_peek + 4 );
417 SkipBytes( p_input, 8 );
419 /* XXX p_demux->psz_demux shouldn't be NULL ! */
420 switch( p_demux->p_wf->wFormatTag )
422 case( WAVE_FORMAT_PCM ):
423 msg_Dbg( p_input,"found raw pcm audio format" );
424 p_demux->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
425 p_demux->GetFrame = PCM_GetFrame;
426 p_demux->psz_demux = strdup( "" );
428 case( WAVE_FORMAT_MPEG ):
429 case( WAVE_FORMAT_MPEGLAYER3 ):
430 msg_Dbg( p_input, "found mpeg audio format" );
431 p_demux->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
432 p_demux->GetFrame = NULL;
433 p_demux->psz_demux = strdup( "mpegaudio" );
435 case( WAVE_FORMAT_A52 ):
436 msg_Dbg( p_input,"found a52 audio format" );
437 p_demux->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
438 p_demux->GetFrame = NULL;
439 p_demux->psz_demux = strdup( "a52" );
441 case( WAVE_FORMAT_ADPCM ):
442 msg_Dbg( p_input, "found ms adpcm audio format" );
443 p_demux->i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x02 );
444 p_demux->GetFrame = MS_ADPCM_GetFrame;
445 p_demux->psz_demux = strdup( "" );
447 case( WAVE_FORMAT_IMA_ADPCM ):
448 msg_Dbg( p_input, "found ima adpcm audio format" );
449 p_demux->i_fourcc = VLC_FOURCC( 'm', 's', 0x00, 0x11 );
450 p_demux->GetFrame = IMA_ADPCM_GetFrame;
451 p_demux->psz_demux = strdup( "" );
454 msg_Warn( p_input,"unrecognize audio format(0x%x)",
455 p_demux->p_wf->wFormatTag );
457 VLC_FOURCC( 'm', 's',
458 (p_demux->p_wf->wFormatTag >> 8)&0xff,
459 (p_demux->p_wf->wFormatTag )&0xff);
460 p_demux->GetFrame = NULL;
461 p_demux->psz_demux = strdup( "" );
465 if( p_demux->GetFrame )
467 msg_Dbg( p_input, "using internal demux" );
469 p_input->pf_demux = WAVDemux;
470 p_input->p_demux_data = p_demux;
472 /* create one program */
473 vlc_mutex_lock( &p_input->stream.stream_lock );
474 if( input_InitStream( p_input, 0 ) == -1)
476 vlc_mutex_unlock( &p_input->stream.stream_lock );
477 msg_Err( p_input, "cannot init stream" );
481 if( input_AddProgram( p_input, 0, 0) == NULL )
483 vlc_mutex_unlock( &p_input->stream.stream_lock );
484 msg_Err( p_input, "cannot add program" );
488 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
489 p_input->stream.i_mux_rate = 0 ; /* FIXME */
491 p_demux->p_es = input_AddES( p_input,
492 p_input->stream.p_selected_program, 1,
494 p_demux->p_es->i_stream_id = 1;
495 p_demux->p_es->i_fourcc = p_demux->i_fourcc;
496 p_demux->p_es->i_cat = AUDIO_ES;
497 memcpy( p_demux->p_es->p_demux_data,
501 input_SelectES( p_input, p_demux->p_es );
503 p_input->stream.p_selected_program->b_is_ok = 1;
504 vlc_mutex_unlock( &p_input->stream.stream_lock );
509 /* call an external demux */
510 msg_Warn( p_input, "unsupported formattag, using external demux" );
512 psz_sav = p_input->psz_demux;
513 p_input->psz_demux = p_demux->psz_demux;
515 p_demux->p_demux = module_Need( p_input, "demux", NULL );
517 p_input->psz_demux = psz_sav;
519 if( !p_demux->p_demux )
522 "cannot get external demux for formattag 0x%x",
523 p_demux->p_wf->wFormatTag );
524 FREE( p_demux->psz_demux );
525 FREE( p_demux->p_wf );
529 /* save value and switch back */
530 p_demux->pf_demux = p_input->pf_demux;
531 p_demux->p_demux_data = p_input->p_demux_data;
533 p_input->pf_demux = WAVCallDemux;
534 p_input->p_demux_data = p_demux;
541 /*****************************************************************************
542 * WAVCallDemux: call true demux
543 *****************************************************************************
544 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
545 *****************************************************************************/
546 static int WAVCallDemux( input_thread_t *p_input )
548 demux_sys_t *p_demux = p_input->p_demux_data;
553 psz_sav = p_input->psz_demux;
556 p_input->pf_demux = p_demux->pf_demux;
557 p_input->p_demux_data = p_demux->p_demux_data;
558 p_input->psz_demux = p_demux->psz_demux;
561 i_status = p_input->pf_demux( p_input );
563 /* save (new?) state */
564 p_demux->pf_demux = p_input->pf_demux;
565 p_demux->p_demux_data = p_input->p_demux_data;
568 p_input->psz_demux = psz_sav;
569 p_input->pf_demux = WAVCallDemux;
570 p_input->p_demux_data = p_demux;
575 /*****************************************************************************
576 * WAVDemux: read packet and send them to decoders
577 *****************************************************************************
578 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
579 *****************************************************************************/
580 static int WAVDemux( input_thread_t *p_input )
582 demux_sys_t *p_demux = p_input->p_demux_data;
586 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
590 i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
595 if( p_demux->p_wf->nBlockAlign != 0 )
597 i_offset += p_demux->p_wf->nBlockAlign -
598 i_offset % p_demux->p_wf->nBlockAlign;
600 SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
603 input_ClockManageRef( p_input,
604 p_input->stream.p_selected_program,
607 if( TellAbsolute( p_input )
608 >= (off_t)(p_demux->i_data_pos + p_demux->i_data_size) )
613 if( !p_demux->GetFrame( p_input, p_demux->p_wf, &p_pes, &i_length ) )
615 msg_Warn( p_input, "failed to get one frame" );
620 p_pes->i_pts = input_ClockGetTS( p_input,
621 p_input->stream.p_selected_program,
624 if( !p_demux->p_es->p_decoder_fifo )
626 msg_Err( p_input, "no audio decoder" );
627 input_DeletePES( p_input->p_method_data, p_pes );
632 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
635 p_demux->i_pcr += i_length * 9 / 100;
639 /*****************************************************************************
640 * WAVEnd: frees unused data
641 *****************************************************************************/
642 static void __WAVEnd ( vlc_object_t * p_this )
644 input_thread_t * p_input = (input_thread_t *)p_this;
645 demux_sys_t *p_demux = p_input->p_demux_data;
647 FREE( p_demux->p_wf );
648 FREE( p_demux->psz_demux );
650 if( p_demux->p_demux )
652 module_Unneed( p_input, p_demux->p_demux );