1 /*****************************************************************************
2 * aiff.c: Audio Interchange File Format demuxer
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN (Centrale Réseaux) and its contributors
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
43 set_category( CAT_INPUT );
44 set_subcategory( SUBCAT_INPUT_DEMUX );
45 set_description( _("AIFF demuxer" ) );
46 set_capability( "demux2", 10 );
47 set_callbacks( Open, Close );
48 add_shortcut( "aiff" );
51 /*****************************************************************************
53 *****************************************************************************/
74 static int Demux ( demux_t *p_demux );
75 static int Control( demux_t *p_demux, int i_query, va_list args );
77 /* GetF80BE: read a 80 bits float in big endian */
78 static unsigned int GetF80BE( uint8_t p[10] )
80 unsigned int i_mantissa = GetDWBE( &p[2] );
81 int i_exp = 30 - p[1];
82 unsigned int i_last = 0;
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 demux_t *p_demux = (demux_t*)p_this;
106 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
107 if( strncmp( &p_peek[0], "FORM", 4 ) || strncmp( &p_peek[8], "AIFF", 4 ) )
112 /* skip aiff header */
113 stream_Read( p_demux->s, NULL, 12 );
115 /* Fill p_demux field */
116 p_demux->pf_demux = Demux;
117 p_demux->pf_control = Control;
118 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
120 es_format_Init( &p_sys->fmt, UNKNOWN_ES, 0 );
122 p_sys->i_ssnd_pos = -1;
128 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
130 msg_Dbg( p_demux, "cannot peek()" );
133 i_size = GetDWBE( &p_peek[4] );
135 msg_Dbg( p_demux, "chunk fcc=%4.4s size=%d", p_peek, i_size );
137 if( !strncmp( &p_peek[0], "COMM", 4 ) )
139 if( stream_Peek( p_demux->s, &p_peek, 18 + 8 ) < 18 + 8 )
141 msg_Dbg( p_demux, "cannot peek()" );
144 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
145 p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
146 p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
147 p_sys->fmt.audio.i_rate = GetF80BE( &p_peek[16] );
149 msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
150 GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), GetF80BE( &p_peek[16] ) );
152 else if( !strncmp( &p_peek[0], "SSND", 4 ) )
154 if( stream_Peek( p_demux->s, &p_peek, 8 + 8 ) < 8 + 8 )
156 msg_Dbg( p_demux, "cannot peek()" );
160 p_sys->i_ssnd_pos = stream_Tell( p_demux->s );
161 p_sys->i_ssnd_size = i_size;
162 p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
163 p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );
165 msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
166 p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
168 if( p_sys->i_ssnd_pos >= 12 && p_sys->fmt.i_cat == AUDIO_ES )
170 /* We have found the 2 needed chunks */
174 /* Skip this chunk */
176 if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size )
178 msg_Warn( p_demux, "incomplete file" );
183 p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
184 p_sys->i_ssnd_end = p_sys->i_ssnd_start + p_sys->i_ssnd_size;
186 p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
187 ( p_sys->fmt.audio.i_bitspersample + 7 ) / 8;
189 if( p_sys->i_ssnd_fsize <= 0 )
191 msg_Err( p_demux, "invalid audio parameters" );
195 if( p_sys->i_ssnd_size <= 0 )
198 p_sys->i_ssnd_end = 0;
201 /* seek into SSND chunk */
202 if( stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
204 msg_Err( p_demux, "cannot seek to data chunk" );
209 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
218 /*****************************************************************************
220 *****************************************************************************/
221 static void Close( vlc_object_t *p_this )
223 demux_t *p_demux = (demux_t*)p_this;
224 demux_sys_t *p_sys = p_demux->p_sys;
230 /*****************************************************************************
232 *****************************************************************************/
233 static int Demux( demux_t *p_demux )
235 demux_sys_t *p_sys = p_demux->p_sys;
236 int64_t i_tell = stream_Tell( p_demux->s );
241 if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
248 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time);
250 /* we will read 100ms at once */
251 i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
252 if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
254 i_read = p_sys->i_ssnd_end - i_tell;
256 if( ( p_block = stream_Block( p_demux->s, i_read ) ) == NULL )
262 p_block->i_pts = p_sys->i_time;
264 p_sys->i_time += (int64_t)1000000 *
266 p_sys->i_ssnd_fsize /
267 p_sys->fmt.audio.i_rate;
270 es_out_Send( p_demux->out, p_sys->es, p_block );
274 /*****************************************************************************
276 *****************************************************************************/
277 static int Control( demux_t *p_demux, int i_query, va_list args )
279 demux_sys_t *p_sys = p_demux->p_sys;
285 case DEMUX_GET_POSITION:
287 int64_t i_start = p_sys->i_ssnd_start;
288 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
289 int64_t i_tell = stream_Tell( p_demux->s );
291 pf = (double*) va_arg( args, double* );
293 if( i_start < i_end )
295 *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
301 case DEMUX_SET_POSITION:
303 int64_t i_start = p_sys->i_ssnd_start;
304 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
306 f = (double) va_arg( args, double );
308 if( i_start < i_end )
310 int i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
311 int64_t i_new = i_start + i_frame * p_sys->i_ssnd_fsize;
313 if( stream_Seek( p_demux->s, i_new ) )
317 p_sys->i_time = 1 + (int64_t)1000000 * i_frame / p_sys->fmt.audio.i_rate;
324 pi64 = (int64_t*)va_arg( args, int64_t * );
325 *pi64 = p_sys->i_time;
328 case DEMUX_GET_LENGTH:
330 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
332 pi64 = (int64_t*)va_arg( args, int64_t * );
333 if( p_sys->i_ssnd_start < i_end )
335 *pi64 = (int64_t)1000000 * ( i_end - p_sys->i_ssnd_start ) / p_sys->i_ssnd_fsize / p_sys->fmt.audio.i_rate;