1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: wav.c,v 1.1 2002/10/14 21:59:44 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() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int WAVInit ( vlc_object_t * );
40 static void __WAVEnd ( vlc_object_t * );
41 static int WAVDemux ( input_thread_t * );
42 static int WAVCallDemux ( input_thread_t * );
44 #define WAVEnd(a) __WAVEnd(VLC_OBJECT(a))
46 /*****************************************************************************
48 *****************************************************************************/
50 set_description( "WAV demuxer" );
51 set_capability( "demux", 142 );
52 set_callbacks( WAVInit, __WAVEnd );
55 /*****************************************************************************
56 * Declaration of local function
57 *****************************************************************************/
59 #define FREE( p ) if( p ) free( p ); (p) = NULL
61 #define __EVEN( x ) ( (x)%2 != 0 ) ? ((x)+1) : (x)
63 /* Some functions to manipulate memory */
64 static u16 GetWLE( u8 *p_buff )
66 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
69 static u32 GetDWLE( u8 *p_buff )
71 return( p_buff[0] + ( p_buff[1] <<8 ) +
72 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
75 static u32 CreateDWLE( int a, int b, int c, int d )
77 return( a + ( b << 8 ) + ( c << 16 ) + ( d << 24 ) );
81 static off_t TellAbsolute( input_thread_t *p_input )
85 vlc_mutex_lock( &p_input->stream.stream_lock );
87 i_pos= p_input->stream.p_selected_area->i_tell -
88 ( p_input->p_last_data - p_input->p_current_data );
90 vlc_mutex_unlock( &p_input->stream.stream_lock );
95 static int SeekAbsolute( input_thread_t *p_input,
100 if( i_pos >= p_input->stream.p_selected_area->i_size )
105 i_filepos = TellAbsolute( p_input );
106 if( i_pos != i_filepos )
108 p_input->pf_seek( p_input, i_pos );
109 input_AccessReinit( p_input );
114 static int SkipBytes( input_thread_t *p_input, int i_skip )
116 return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
119 /* return 1 if success, 0 if fail */
120 static int ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
122 data_packet_t *p_data;
134 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
139 memcpy( p_buff, p_data->p_payload_start, i_read );
140 input_DeletePacket( p_input->p_method_data, p_data );
151 static int ReadPES( input_thread_t *p_input,
152 pes_packet_t **pp_pes,
159 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
161 msg_Err( p_input, "cannot allocate new PES" );
167 data_packet_t *p_data;
170 if( (i_read = input_SplitBuffer( p_input,
172 __MIN( i_size, 1024 ) ) ) <= 0 )
174 input_DeletePES( p_input->p_method_data, p_pes );
177 if( !p_pes->p_first )
179 p_pes->p_first = p_data;
180 p_pes->i_nb_data = 1;
181 p_pes->i_pes_size = i_read;
185 p_pes->p_last->p_next = p_data;
187 p_pes->i_pes_size += i_read;
189 p_pes->p_last = p_data;
196 static int FindTag( input_thread_t *p_input, u32 i_tag )
205 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
207 msg_Err( p_input, "cannot peek()" );
211 i_id = GetDWLE( p_peek );
212 i_size = GetDWLE( p_peek + 4 );
214 msg_Dbg( p_input, "FindTag: tag:%4.4s size:%d", &i_id, i_size );
217 /* Yes, we have found the good tag */
220 if( !SkipBytes( p_input, __EVEN( i_size ) + 8 ) )
227 static int LoadTag_fmt( input_thread_t *p_input,
228 demux_sys_t *p_demux )
233 if( input_Peek( p_input, &p_peek , 8 ) < 8 )
238 p_demux->i_wf = i_size = GetDWLE( p_peek + 4 );
239 SkipBytes( p_input, 8 );
242 SkipBytes( p_input, i_size );
245 p_demux->p_wf = malloc( i_size );
246 ReadData( p_input, p_demux->p_wf, __EVEN( i_size ) );
247 p_demux->format.i_format = GetWLE( p_demux->p_wf );
248 p_demux->format.i_channels = GetWLE( p_demux->p_wf + 2 );
249 p_demux->format.i_samplepersec = GetDWLE( p_demux->p_wf + 4 );
250 p_demux->format.i_avgbytespersec = GetDWLE( p_demux->p_wf + 8);
251 p_demux->format.i_blockalign = GetWLE( p_demux->p_wf + 12 );
252 p_demux->format.i_bitspersample = GetWLE( p_demux->p_wf + 14);
255 p_demux->format.i_size = GetWLE( p_demux->p_wf + 16 );
256 p_demux->format.p_data = malloc( p_demux->format.i_size );
257 memcpy( p_demux->format.p_data,
259 p_demux->format.i_size );
263 p_demux->format.i_size = 0;
264 p_demux->format.p_data = NULL;
267 msg_Dbg( p_input, "loaded \"fmt \" chunk" );
271 static int PCM_GetFrame( input_thread_t *p_input,
272 waveformatex_t *p_wf,
273 pes_packet_t **pp_pes,
281 /* read samples for 50ms of */
282 i_samples = __MAX( p_wf->i_samplepersec / 20, 1 );
285 *pi_length = (mtime_t)1000000 *
287 (mtime_t)p_wf->i_samplepersec;
289 i_bytes = i_samples * p_wf->i_channels * ( p_wf->i_bitspersample + 7 ) / 8;
291 if( p_wf->i_blockalign > 0 )
293 if( ( i_modulo = i_bytes % p_wf->i_blockalign ) != 0 )
295 i_bytes += p_wf->i_blockalign - i_modulo;
299 return( ReadPES( p_input, pp_pes, i_bytes ) );
302 /*****************************************************************************
303 * WAVInit: check file and initializes structures
304 *****************************************************************************/
305 static int WAVInit( vlc_object_t * p_this )
307 input_thread_t *p_input = (input_thread_t *)p_this;
311 demux_sys_t *p_demux;
315 /* Initialize access plug-in structures. */
316 if( p_input->i_mtu == 0 )
319 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
322 /* a little test to see if it's a wav file */
323 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
325 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
329 if( ( GetDWLE( p_peek ) != CreateDWLE( 'R', 'I', 'F', 'F' ) )||
330 ( GetDWLE( p_peek + 8 ) != CreateDWLE( 'W', 'A', 'V', 'E' ) ) )
332 msg_Warn( p_input, "WAV plugin discarded (not a valid file)" );
335 i_size = GetDWLE( p_peek + 4 );
336 SkipBytes( p_input, 12 );
338 if( !FindTag( p_input, CreateDWLE( 'f', 'm', 't' ,' ' ) ) )
340 msg_Err( p_input, "cannot find \"fmt \" tag" );
344 /* create our structure that will contains all data */
345 if( !( p_input->p_demux_data =
346 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
348 msg_Err( p_input, "out of memory" );
351 memset( p_demux, 0, sizeof( demux_sys_t ) );
353 /* Load waveformatex_t header */
354 if( !LoadTag_fmt( p_input, p_demux ) )
356 msg_Err( p_input, "cannot load \"fmt \" tag" );
360 msg_Dbg( p_input, "format:0x%4.4x channels:%d %dHz %dKo/s blockalign:%d bits/samples:%d extra size:%d",
361 p_demux->format.i_format,
362 p_demux->format.i_channels,
363 p_demux->format.i_samplepersec,
364 p_demux->format.i_avgbytespersec/1024,
365 p_demux->format.i_blockalign,
366 p_demux->format.i_bitspersample,
367 p_demux->format.i_size );
369 if( !FindTag( p_input, CreateDWLE( 'd', 'a', 't', 'a' ) ) )
371 msg_Err( p_input, "cannot find \"data\" tag" );
372 FREE( p_demux->p_wf );
373 FREE( p_demux->format.p_data );
377 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
379 msg_Warn( p_input, "WAV plugin discarded (cannot peek)" );
380 FREE( p_demux->p_wf );
381 FREE( p_demux->format.p_data );
386 p_demux->i_data_pos = TellAbsolute( p_input ) + 8;
387 p_demux->i_data_size = GetDWLE( p_peek + 4 );
388 SkipBytes( p_input, 8 );
390 /* XXX p_demux->psz_demux shouldn't be NULL ! */
391 switch( p_demux->format.i_format )
394 msg_Dbg( p_input,"found raw pcm audio format" );
395 p_demux->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
396 p_demux->GetFrame = PCM_GetFrame;
397 p_demux->psz_demux = strdup( "" );
401 msg_Dbg( p_input, "found mpeg audio format" );
402 p_demux->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
403 p_demux->GetFrame = NULL;
404 p_demux->psz_demux = strdup( "mpegaudio" );
407 msg_Dbg( p_input,"found a52 audio format" );
408 p_demux->i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
409 p_demux->GetFrame = NULL;
410 p_demux->psz_demux = strdup( "a52" );
413 msg_Warn( p_input,"unrecognize audio format(0x%x)",
414 p_demux->format.i_format );
416 VLC_FOURCC( 'm', 's',
417 (p_demux->format.i_format >> 8)&0xff,
418 (p_demux->format.i_format )&0xff);
419 p_demux->GetFrame = NULL;
420 p_demux->psz_demux = strdup( "" );
424 if( p_demux->GetFrame )
426 msg_Dbg( p_input, "using internal demux" );
428 p_input->pf_demux = WAVDemux;
429 p_input->p_demux_data = p_demux;
431 /* create one program */
432 vlc_mutex_lock( &p_input->stream.stream_lock );
433 if( input_InitStream( p_input, 0 ) == -1)
435 vlc_mutex_unlock( &p_input->stream.stream_lock );
436 msg_Err( p_input, "cannot init stream" );
440 if( input_AddProgram( p_input, 0, 0) == NULL )
442 vlc_mutex_unlock( &p_input->stream.stream_lock );
443 msg_Err( p_input, "cannot add program" );
447 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
448 p_input->stream.i_mux_rate = 0 ; /* FIXME */
450 p_demux->p_es = input_AddES( p_input,
451 p_input->stream.p_selected_program, 1,
453 p_demux->p_es->i_stream_id = 1;
454 p_demux->p_es->i_fourcc = p_demux->i_fourcc;
455 p_demux->p_es->i_cat = AUDIO_ES;
456 if( p_demux->i_wf > 0 && p_demux->p_wf )
458 memcpy( p_demux->p_es->p_demux_data,
463 input_SelectES( p_input, p_demux->p_es );
465 p_input->stream.p_selected_program->b_is_ok = 1;
466 vlc_mutex_unlock( &p_input->stream.stream_lock );
471 /* call an external demux */
472 msg_Warn( p_input, "unsupported formattag, using external demux" );
474 psz_sav = p_input->psz_demux;
475 p_input->psz_demux = p_demux->psz_demux;
477 p_demux->p_demux = module_Need( p_input, "demux", NULL );
479 p_input->psz_demux = psz_sav;
481 if( !p_demux->p_demux )
484 "cannot get external demux for formattag 0x%x",
485 p_demux->format.i_format );
486 FREE( p_demux->psz_demux );
487 FREE( p_demux->p_wf );
488 FREE( p_demux->format.p_data );
492 /* save value and switch back */
493 p_demux->pf_demux = p_input->pf_demux;
494 p_demux->p_demux_data = p_input->p_demux_data;
496 p_input->pf_demux = WAVCallDemux;
497 p_input->p_demux_data = p_demux;
505 /*****************************************************************************
506 * WAVCallDemux: call true demux
507 *****************************************************************************
508 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
509 *****************************************************************************/
510 static int WAVCallDemux( input_thread_t *p_input )
512 demux_sys_t *p_demux = p_input->p_demux_data;
517 psz_sav = p_input->psz_demux;
520 p_input->pf_demux = p_demux->pf_demux;
521 p_input->p_demux_data = p_demux->p_demux_data;
522 p_input->psz_demux = p_demux->psz_demux;
525 i_status = p_input->pf_demux( p_input );
527 /* save (new?) state */
528 p_demux->pf_demux = p_input->pf_demux;
529 p_demux->p_demux_data = p_input->p_demux_data;
532 p_input->psz_demux = psz_sav;
533 p_input->pf_demux = WAVCallDemux;
534 p_input->p_demux_data = p_demux;
539 /*****************************************************************************
540 * WAVDemux: read packet and send them to decoders
541 *****************************************************************************
542 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
543 *****************************************************************************/
544 static int WAVDemux( input_thread_t *p_input )
546 demux_sys_t *p_demux = p_input->p_demux_data;
550 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
554 i_offset = TellAbsolute( p_input ) - p_demux->i_data_pos;
557 SeekAbsolute( p_input, p_demux->i_data_pos );
560 if( p_demux->format.i_blockalign != 0 )
563 i_offset = i_offset - i_offset % p_demux->format.i_blockalign;
564 SeekAbsolute( p_input, p_demux->i_data_pos + i_offset );
568 input_ClockManageRef( p_input,
569 p_input->stream.p_selected_program,
572 if( TellAbsolute( p_input ) >= p_demux->i_data_pos + p_demux->i_data_size )
577 if( !p_demux->GetFrame( p_input, &p_demux->format, &p_pes, &i_length ) )
579 msg_Warn( p_input, "failed to get one frame" );
584 p_pes->i_pts = input_ClockGetTS( p_input,
585 p_input->stream.p_selected_program,
588 if( !p_demux->p_es->p_decoder_fifo )
590 msg_Err( p_input, "no audio decoder" );
591 input_DeletePES( p_input->p_method_data, p_pes );
596 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
599 p_demux->i_pcr += i_length * 9 / 100;
603 /*****************************************************************************
604 * WAVEnd: frees unused data
605 *****************************************************************************/
606 static void __WAVEnd ( vlc_object_t * p_this )
608 input_thread_t * p_input = (input_thread_t *)p_this;
609 demux_sys_t *p_demux = p_input->p_demux_data;
611 FREE( p_demux->p_wf );
612 FREE( p_demux->format.p_data );
613 FREE( p_demux->psz_demux );
615 if( p_demux->p_demux )
617 module_Unneed( p_input, p_demux->p_demux );