1 /*****************************************************************************
2 * au.c : au file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: au.c,v 1.2 2003/05/05 22:23:34 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>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int AUInit ( vlc_object_t * );
38 static void AUEnd ( vlc_object_t * );
39 static int AUDemux ( input_thread_t * );
41 static int AUDemuxPCM ( input_thread_t * );
43 /*****************************************************************************
45 *****************************************************************************/
47 set_description( _("AU demuxer") );
48 set_capability( "demux", 142 );
49 set_callbacks( AUInit, AUEnd );
53 #define AUDIO_FILE_ENCODING_MULAW_8 1 /* 8-bit ISDN u-law */
54 #define AUDIO_FILE_ENCODING_LINEAR_8 2 /* 8-bit linear PCM */
55 #define AUDIO_FILE_ENCODING_LINEAR_16 3 /* 16-bit linear PCM */
56 #define AUDIO_FILE_ENCODING_LINEAR_24 4 /* 24-bit linear PCM */
57 #define AUDIO_FILE_ENCODING_LINEAR_32 5 /* 32-bit linear PCM */
58 #define AUDIO_FILE_ENCODING_FLOAT 6 /* 32-bit IEEE floating point */
59 #define AUDIO_FILE_ENCODING_DOUBLE 7 /* 64-bit IEEE floating point */
60 #define AUDIO_FILE_ENCODING_ADPCM_G721 23 /* 4-bit CCITT g.721 ADPCM */
61 #define AUDIO_FILE_ENCODING_ADPCM_G722 24 /* CCITT g.722 ADPCM */
62 #define AUDIO_FILE_ENCODING_ADPCM_G723_3 25 /* CCITT g.723 3-bit ADPCM */
63 #define AUDIO_FILE_ENCODING_ADPCM_G723_5 26 /* CCITT g.723 5-bit ADPCM */
64 #define AUDIO_FILE_ENCODING_ALAW_8 27 /* 8-bit ISDN A-law */
68 uint32_t i_header_size;
71 uint32_t i_sample_rate;
75 #define AU_DEMUX_PCM 0x01
76 #define AU_DEMUX_ADPCM 0x02
84 es_descriptor_t *p_es;
89 /*****************************************************************************
90 * Declaration of local function
91 *****************************************************************************/
93 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
95 static uint32_t GetDWBE( uint8_t *p_buff )
97 return( ( p_buff[0] << 24 ) + ( p_buff[1] << 16 ) +
98 ( p_buff[2] << 8 ) + p_buff[3] );
102 static off_t TellAbsolute( input_thread_t *p_input )
106 vlc_mutex_lock( &p_input->stream.stream_lock );
108 i_pos= p_input->stream.p_selected_area->i_tell;
110 vlc_mutex_unlock( &p_input->stream.stream_lock );
115 /* return 1 if success, 0 if fail */
116 static int ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
118 data_packet_t *p_data;
131 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
136 memcpy( p_buff, p_data->p_payload_start, i_read );
137 input_DeletePacket( p_input->p_method_data, p_data );
148 static int SeekAbsolute( input_thread_t *p_input,
153 i_skip = i_pos - TellAbsolute( p_input );
156 return( VLC_SUCCESS );
158 if( i_skip < 0 && !p_input->stream.b_seekable )
160 return( VLC_EGENERIC );
162 else if( !p_input->stream.b_seekable ||
163 ( i_skip > 0 && i_skip < 1024 && p_input->stream.i_method != INPUT_METHOD_FILE ) )
170 i_read = ReadData( p_input, dummy, __MIN( i_skip, 1024 ) );
173 return( VLC_EGENERIC );
177 return( VLC_SUCCESS );
181 input_AccessReinit( p_input );
182 p_input->pf_seek( p_input, i_pos );
183 return( VLC_SUCCESS );
187 static int SkipBytes( input_thread_t *p_input, int i_skip )
189 return( SeekAbsolute( p_input, TellAbsolute( p_input ) + i_skip ) );
192 static int ReadPES( input_thread_t *p_input,
193 pes_packet_t **pp_pes,
200 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
202 msg_Err( p_input, "cannot allocate new PES" );
203 return( VLC_EGENERIC );
208 data_packet_t *p_data;
211 if( (i_read = input_SplitBuffer( p_input,
213 __MIN( i_size, 1024 ) ) ) <= 0 )
215 input_DeletePES( p_input->p_method_data, p_pes );
216 return( VLC_EGENERIC );
218 if( !p_pes->p_first )
220 p_pes->p_first = p_data;
221 p_pes->i_nb_data = 1;
222 p_pes->i_pes_size = i_read;
226 p_pes->p_last->p_next = p_data;
228 p_pes->i_pes_size += i_read;
230 p_pes->p_last = p_data;
234 return( VLC_SUCCESS );
237 /*****************************************************************************
238 * AUInit: check file and initializes structures
239 *****************************************************************************/
240 static int AUInit( vlc_object_t * p_this )
242 input_thread_t *p_input = (input_thread_t *)p_this;
247 vlc_fourcc_t i_codec;
249 demux_sys_t *p_demux;
250 es_descriptor_t *p_es;
253 /* Initialize access plug-in structures. */
254 if( p_input->i_mtu == 0 )
257 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
260 /* a little test to see if it's a wav file */
261 if( input_Peek( p_input, &p_peek, 24 ) < 24 )
263 msg_Warn( p_input, "AU plugin discarded (cannot peek)" );
268 if( strcmp( p_peek, ".snd" ) )
270 msg_Warn( p_input, "AU plugin discarded (not a valid file)" );
271 return( VLC_EGENERIC );
273 au.i_header_size = GetDWBE( &p_peek[0x04] );
274 au.i_data_size = GetDWBE( &p_peek[0x08] );
275 au.i_encoding = GetDWBE( &p_peek[0x0c] );
276 au.i_sample_rate = GetDWBE( &p_peek[0x10] );
277 au.i_channels = GetDWBE( &p_peek[0x14] );
279 "au file: header_size=%d data_size=%d encoding=0x%x sample_rate=%d channels=%d",
285 if( au.i_header_size < 24 )
287 msg_Warn( p_input, "AU plugin discarded (not a valid file)" );
288 return( VLC_EGENERIC );
291 /* Create WAVEFORMATEX structure */
292 wf.nChannels = au.i_channels;
293 wf.nSamplesPerSec= au.i_sample_rate;
296 switch( au.i_encoding )
298 case AUDIO_FILE_ENCODING_MULAW_8: /* 8-bit ISDN u-law */
299 wf.wFormatTag = WAVE_FORMAT_MULAW; // FIXME ??
300 wf.wBitsPerSample = 8;
301 wf.nBlockAlign = 1 * wf.nChannels;
302 i_codec = VLC_FOURCC( 'u','l','a','w' );
304 case AUDIO_FILE_ENCODING_LINEAR_8: /* 8-bit linear PCM */
305 wf.wFormatTag = WAVE_FORMAT_PCM;
306 wf.wBitsPerSample = 8;
307 wf.nBlockAlign = 1 * wf.nChannels;
308 i_codec = VLC_FOURCC( 't','w','o','s' );
311 case AUDIO_FILE_ENCODING_LINEAR_16: /* 16-bit linear PCM */
312 wf.wFormatTag = WAVE_FORMAT_PCM;
313 wf.wBitsPerSample = 16;
314 wf.nBlockAlign = 2 * wf.nChannels;
315 i_codec = VLC_FOURCC( 't','w','o','s' );
318 case AUDIO_FILE_ENCODING_LINEAR_24: /* 24-bit linear PCM */
319 wf.wFormatTag = WAVE_FORMAT_PCM;
320 wf.wBitsPerSample = 24;
321 wf.nBlockAlign = 3 * wf.nChannels;
322 i_codec = VLC_FOURCC( 't','w','o','s' );
325 case AUDIO_FILE_ENCODING_LINEAR_32: /* 32-bit linear PCM */
326 wf.wFormatTag = WAVE_FORMAT_PCM;
327 wf.wBitsPerSample = 32;
328 wf.nBlockAlign = 4 * wf.nChannels;
329 i_codec = VLC_FOURCC( 't','w','o','s' );
332 case AUDIO_FILE_ENCODING_FLOAT: /* 32-bit IEEE floating point */
333 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
334 wf.wBitsPerSample = 32;
335 wf.nBlockAlign = 4 * wf.nChannels;
336 i_codec = VLC_FOURCC( 'a', 'u', 0, AUDIO_FILE_ENCODING_FLOAT );
339 case AUDIO_FILE_ENCODING_DOUBLE: /* 64-bit IEEE floating point */
340 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
341 wf.wBitsPerSample = 64;
342 wf.nBlockAlign = 8 * wf.nChannels;
343 i_codec = VLC_FOURCC( 'a', 'u', 0, AUDIO_FILE_ENCODING_DOUBLE );
346 case AUDIO_FILE_ENCODING_ADPCM_G721: /* 4-bit CCITT g.721 ADPCM */
347 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
348 wf.wBitsPerSample = 0;
349 wf.nBlockAlign = 0 * wf.nChannels;
350 i_codec = VLC_FOURCC( 'a', 'u', 0, AUDIO_FILE_ENCODING_ADPCM_G721 );
353 case AUDIO_FILE_ENCODING_ADPCM_G722: /* CCITT g.722 ADPCM */
354 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
355 wf.wBitsPerSample = 0;
356 wf.nBlockAlign = 0 * wf.nChannels;
357 i_codec = VLC_FOURCC( 'a', 'u', 0, AUDIO_FILE_ENCODING_ADPCM_G722 );
360 case AUDIO_FILE_ENCODING_ADPCM_G723_3: /* CCITT g.723 3-bit ADPCM */
361 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
362 wf.wBitsPerSample = 0;
363 wf.nBlockAlign = 0 * wf.nChannels;
364 i_codec = VLC_FOURCC( 'a', 'u', 0, AUDIO_FILE_ENCODING_ADPCM_G723_3 );
367 case AUDIO_FILE_ENCODING_ADPCM_G723_5: /* CCITT g.723 5-bit ADPCM */
368 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
369 wf.wBitsPerSample = 0;
370 wf.nBlockAlign = 0 * wf.nChannels;
371 i_codec = VLC_FOURCC( 'a', 'u', 0, AUDIO_FILE_ENCODING_ADPCM_G723_5 );
374 case AUDIO_FILE_ENCODING_ALAW_8: /* 8-bit ISDN A-law */
375 wf.wFormatTag = WAVE_FORMAT_ALAW; // FIXME ??
376 wf.wBitsPerSample = 8;
377 wf.nBlockAlign = 1 * wf.nChannels;
378 i_codec = VLC_FOURCC( 'a','l','a','w' );
382 msg_Warn( p_input, "unknow encoding=0x%x", au.i_encoding );
383 wf.wFormatTag = WAVE_FORMAT_UNKNOWN;
384 wf.wBitsPerSample = 0;
385 wf.nBlockAlign = 0 * wf.nChannels;
386 i_codec = VLC_FOURCC( 'a','u', 0, au.i_encoding );
389 wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nChannels * wf.wBitsPerSample / 8;
391 /* create one program */
392 vlc_mutex_lock( &p_input->stream.stream_lock );
393 if( input_InitStream( p_input, 0 ) == -1)
395 vlc_mutex_unlock( &p_input->stream.stream_lock );
396 msg_Err( p_input, "cannot init stream" );
397 return( VLC_EGENERIC );
399 if( input_AddProgram( p_input, 0, 0) == NULL )
401 vlc_mutex_unlock( &p_input->stream.stream_lock );
402 msg_Err( p_input, "cannot add program" );
403 return( VLC_EGENERIC );
406 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
407 p_input->stream.i_mux_rate = wf.nAvgBytesPerSec / 50;
409 p_es = input_AddES( p_input, p_input->stream.p_selected_program,
410 0x01, AUDIO_ES, NULL, 0 );
412 p_es->i_stream_id = 0x01;
413 p_es->i_fourcc = i_codec;
414 p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
415 memcpy( p_es->p_waveformatex,
417 sizeof( WAVEFORMATEX ) );
419 input_SelectES( p_input, p_es );
420 p_input->stream.p_selected_program->b_is_ok = 1;
421 vlc_mutex_unlock( &p_input->stream.stream_lock );
423 /* create our structure that will contains all data */
424 if( ( p_demux = malloc( sizeof( demux_sys_t ) ) ) == NULL )
426 msg_Err( p_input, "out of memory" );
427 return( VLC_EGENERIC );
429 p_demux->p_es = p_es;
431 memcpy( &p_demux->au, &au, sizeof( au_t ) );
432 memcpy( &p_demux->wf, &wf, sizeof( WAVEFORMATEX ) );
433 switch( au.i_encoding )
435 case AUDIO_FILE_ENCODING_MULAW_8:
436 case AUDIO_FILE_ENCODING_LINEAR_8:
437 case AUDIO_FILE_ENCODING_LINEAR_16:
438 case AUDIO_FILE_ENCODING_LINEAR_24:
439 case AUDIO_FILE_ENCODING_LINEAR_32:
440 case AUDIO_FILE_ENCODING_FLOAT:
441 case AUDIO_FILE_ENCODING_DOUBLE:
442 case AUDIO_FILE_ENCODING_ALAW_8:
443 p_demux->i_demux = AU_DEMUX_PCM;
446 case AUDIO_FILE_ENCODING_ADPCM_G721:
447 case AUDIO_FILE_ENCODING_ADPCM_G722:
448 case AUDIO_FILE_ENCODING_ADPCM_G723_3:
449 case AUDIO_FILE_ENCODING_ADPCM_G723_5:
450 p_demux->i_demux = AU_DEMUX_ADPCM;
454 p_demux->i_demux = 0;
458 p_input->p_demux_data = p_demux;
459 p_input->pf_demux = AUDemux;
462 SkipBytes( p_input, au.i_header_size );
464 return( VLC_SUCCESS );
467 /*****************************************************************************
468 * AUDemux: read packet and send them to decoders
469 *****************************************************************************
470 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
471 *****************************************************************************/
472 static int AUDemux( input_thread_t *p_input )
474 demux_sys_t *p_demux = p_input->p_demux_data;
476 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
480 i_offset = TellAbsolute( p_input ) - p_demux->au.i_header_size;
485 if( p_demux->wf.nBlockAlign != 0 )
487 i_offset += p_demux->wf.nBlockAlign -
488 i_offset % p_demux->wf.nBlockAlign;
490 SeekAbsolute( p_input, p_demux->au.i_header_size + i_offset );
493 input_ClockManageRef( p_input,
494 p_input->stream.p_selected_program,
495 p_demux->i_time * 9 / 100 );
497 switch( p_demux->i_demux )
500 return( AUDemuxPCM( p_input ) );
503 msg_Err( p_input, "Internal error (p_demux->i_demux invalid/unsported)" );
507 static int AUDemuxPCM( input_thread_t *p_input )
509 demux_sys_t *p_demux = p_input->p_demux_data;
517 /* read samples for 50ms of */
518 i_samples = __MAX( p_demux->wf.nSamplesPerSec / 20, 1 );
522 i_bytes = i_samples * p_demux->wf.nChannels * ( (p_demux->wf.wBitsPerSample + 7) / 8 );
524 if( p_demux->wf.nBlockAlign > 0 )
526 if( ( i_modulo = i_bytes % p_demux->wf.nBlockAlign ) != 0 )
528 i_bytes += p_demux->wf.nBlockAlign - i_modulo;
532 if( ReadPES( p_input, &p_pes, i_bytes ) )
534 msg_Warn( p_input, "failed to get one frame" );
539 p_pes->i_pts = input_ClockGetTS( p_input,
540 p_input->stream.p_selected_program,
541 p_demux->i_time * 9 / 100 );
543 if( !p_demux->p_es->p_decoder_fifo )
545 msg_Err( p_input, "no audio decoder" );
546 input_DeletePES( p_input->p_method_data, p_pes );
551 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
554 p_demux->i_time += (mtime_t)1000000 *
556 (mtime_t)p_demux->wf.nSamplesPerSec;
561 /*****************************************************************************
562 * AUEnd: frees unused data
563 *****************************************************************************/
564 static void AUEnd ( vlc_object_t * p_this )
566 input_thread_t * p_input = (input_thread_t *)p_this;
568 FREE( p_input->p_demux_data );