ffmpeg: targetos must now be in lower case.
[vlc.git] / modules / demux / aiff.c
blobda98254d1cb2913b33495a04d3520fd274d3c1c5
1 /*****************************************************************************
2 * aiff.c: Audio Interchange File Format demuxer
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
32 /* TODO:
33 * - ...
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 vlc_module_begin();
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" );
49 vlc_module_end();
51 /*****************************************************************************
52 * Local prototypes
53 *****************************************************************************/
55 struct demux_sys_t
57 es_format_t fmt;
58 es_out_id_t *es;
60 int64_t i_ssnd_pos;
61 int64_t i_ssnd_size;
62 int i_ssnd_offset;
63 int i_ssnd_blocksize;
65 /* real data start */
66 int64_t i_ssnd_start;
67 int64_t i_ssnd_end;
69 int i_ssnd_fsize;
71 int64_t i_time;
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;
84 while( i_exp-- )
86 i_last = i_mantissa;
87 i_mantissa >>= 1;
89 if( i_last&0x01 )
91 i_mantissa++;
93 return i_mantissa;
96 /*****************************************************************************
97 * Open
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 demux_t *p_demux = (demux_t*)p_this;
102 demux_sys_t *p_sys;
104 uint8_t *p_peek;
106 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
107 if( strncmp( (char *)&p_peek[0], "FORM", 4 ) || strncmp( (char *)&p_peek[8], "AIFF", 4 ) )
109 return VLC_EGENERIC;
112 /* skip aiff header */
113 stream_Read( p_demux->s, NULL, 12 );
115 /* Fill p_demux field */
116 STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
117 es_format_Init( &p_sys->fmt, UNKNOWN_ES, 0 );
118 p_sys->i_time = 1;
119 p_sys->i_ssnd_pos = -1;
121 for( ;; )
123 uint32_t i_size;
125 CHECK_PEEK_GOTO( p_peek, 8 );
126 i_size = GetDWBE( &p_peek[4] );
128 msg_Dbg( p_demux, "chunk fcc=%4.4s size=%d", p_peek, i_size );
130 if( !strncmp( (char *)&p_peek[0], "COMM", 4 ) )
132 CHECK_PEEK_GOTO( p_peek, 18+8 );
133 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
134 p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
135 p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
136 p_sys->fmt.audio.i_rate = GetF80BE( &p_peek[16] );
138 msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
139 GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), GetF80BE( &p_peek[16] ) );
141 else if( !strncmp( (char *)&p_peek[0], "SSND", 4 ) )
143 CHECK_PEEK_GOTO( p_peek, 8+8 );
144 p_sys->i_ssnd_pos = stream_Tell( p_demux->s );
145 p_sys->i_ssnd_size = i_size;
146 p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
147 p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );
149 msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
150 p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
152 if( p_sys->i_ssnd_pos >= 12 && p_sys->fmt.i_cat == AUDIO_ES )
154 /* We have found the 2 needed chunks */
155 break;
158 /* Skip this chunk */
159 i_size += 8;
160 if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size )
162 msg_Warn( p_demux, "incomplete file" );
163 goto error;
167 p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
168 p_sys->i_ssnd_end = p_sys->i_ssnd_start + p_sys->i_ssnd_size;
170 p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
171 ( p_sys->fmt.audio.i_bitspersample + 7 ) / 8;
173 if( p_sys->i_ssnd_fsize <= 0 )
175 msg_Err( p_demux, "invalid audio parameters" );
176 goto error;
179 if( p_sys->i_ssnd_size <= 0 )
181 /* unknown */
182 p_sys->i_ssnd_end = 0;
185 /* seek into SSND chunk */
186 if( stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
188 msg_Err( p_demux, "cannot seek to data chunk" );
189 goto error;
192 /* */
193 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
195 return VLC_SUCCESS;
197 error:
198 free( p_sys );
199 return VLC_EGENERIC;
202 /*****************************************************************************
203 * Close
204 *****************************************************************************/
205 static void Close( vlc_object_t *p_this )
207 demux_t *p_demux = (demux_t*)p_this;
208 demux_sys_t *p_sys = p_demux->p_sys;
210 free( p_sys );
214 /*****************************************************************************
215 * Demux:
216 *****************************************************************************/
217 static int Demux( demux_t *p_demux )
219 demux_sys_t *p_sys = p_demux->p_sys;
220 int64_t i_tell = stream_Tell( p_demux->s );
222 block_t *p_block;
223 int i_read;
225 if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
227 /* EOF */
228 return 0;
231 /* Set PCR */
232 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time);
234 /* we will read 100ms at once */
235 i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
236 if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
238 i_read = p_sys->i_ssnd_end - i_tell;
240 if( ( p_block = stream_Block( p_demux->s, i_read ) ) == NULL )
242 return 0;
245 p_block->i_dts =
246 p_block->i_pts = p_sys->i_time;
248 p_sys->i_time += (int64_t)1000000 *
249 p_block->i_buffer /
250 p_sys->i_ssnd_fsize /
251 p_sys->fmt.audio.i_rate;
253 /* */
254 es_out_Send( p_demux->out, p_sys->es, p_block );
255 return 1;
258 /*****************************************************************************
259 * Control:
260 *****************************************************************************/
261 static int Control( demux_t *p_demux, int i_query, va_list args )
263 demux_sys_t *p_sys = p_demux->p_sys;
264 double f, *pf;
265 int64_t *pi64;
267 switch( i_query )
269 case DEMUX_GET_POSITION:
271 int64_t i_start = p_sys->i_ssnd_start;
272 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
273 int64_t i_tell = stream_Tell( p_demux->s );
275 pf = (double*) va_arg( args, double* );
277 if( i_start < i_end )
279 *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
280 return VLC_SUCCESS;
282 return VLC_EGENERIC;
285 case DEMUX_SET_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 );
290 f = (double) va_arg( args, double );
292 if( i_start < i_end )
294 int i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
295 int64_t i_new = i_start + i_frame * p_sys->i_ssnd_fsize;
297 if( stream_Seek( p_demux->s, i_new ) )
299 return VLC_EGENERIC;
301 p_sys->i_time = 1 + (int64_t)1000000 * i_frame / p_sys->fmt.audio.i_rate;
302 return VLC_SUCCESS;
304 return VLC_EGENERIC;
307 case DEMUX_GET_TIME:
308 pi64 = (int64_t*)va_arg( args, int64_t * );
309 *pi64 = p_sys->i_time;
310 return VLC_SUCCESS;
312 case DEMUX_GET_LENGTH:
314 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
316 pi64 = (int64_t*)va_arg( args, int64_t * );
317 if( p_sys->i_ssnd_start < i_end )
319 *pi64 = (int64_t)1000000 * ( i_end - p_sys->i_ssnd_start ) / p_sys->i_ssnd_fsize / p_sys->fmt.audio.i_rate;
320 return VLC_SUCCESS;
322 return VLC_EGENERIC;
324 case DEMUX_SET_TIME:
325 case DEMUX_GET_FPS:
326 default:
327 return VLC_EGENERIC;