Warn if xcb-keysyms is missing
[vlc/asuraparaju-public.git] / modules / demux / aiff.c
bloba236582b10f77af91bd450a81b68baae88f6d24d
1 /*****************************************************************************
2 * aiff.c: Audio Interchange File Format demuxer
3 *****************************************************************************
4 * Copyright (C) 2004-2007 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 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /* TODO:
37 * - ...
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 vlc_module_begin ()
47 set_category( CAT_INPUT )
48 set_subcategory( SUBCAT_INPUT_DEMUX )
49 set_description( N_("AIFF demuxer" ) )
50 set_capability( "demux", 10 )
51 set_callbacks( Open, Close )
52 add_shortcut( "aiff" )
53 vlc_module_end ()
55 /*****************************************************************************
56 * Local prototypes
57 *****************************************************************************/
59 struct demux_sys_t
61 es_format_t fmt;
62 es_out_id_t *es;
64 int64_t i_ssnd_pos;
65 int64_t i_ssnd_size;
66 int i_ssnd_offset;
67 int i_ssnd_blocksize;
69 /* real data start */
70 int64_t i_ssnd_start;
71 int64_t i_ssnd_end;
73 int i_ssnd_fsize;
75 int64_t i_time;
78 static int Demux ( demux_t *p_demux );
79 static int Control( demux_t *p_demux, int i_query, va_list args );
81 /* GetF80BE: read a 80 bits float in big endian */
82 static unsigned int GetF80BE( const uint8_t p[10] )
84 unsigned int i_mantissa = GetDWBE( &p[2] );
85 int i_exp = 30 - p[1];
86 unsigned int i_last = 0;
88 while( i_exp-- > 0 )
90 i_last = i_mantissa;
91 i_mantissa >>= 1;
93 if( i_last&0x01 )
95 i_mantissa++;
97 return i_mantissa;
100 /*****************************************************************************
101 * Open
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
105 demux_t *p_demux = (demux_t*)p_this;
106 demux_sys_t *p_sys;
108 const uint8_t *p_peek;
110 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
111 return VLC_EGENERIC;
112 if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) )
113 return VLC_EGENERIC;
115 /* skip aiff header */
116 stream_Read( p_demux->s, NULL, 12 );
118 /* Fill p_demux field */
119 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
120 es_format_Init( &p_sys->fmt, UNKNOWN_ES, 0 );
121 p_sys->i_time = 0;
122 p_sys->i_ssnd_pos = -1;
124 for( ;; )
126 uint32_t i_size;
128 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
129 goto error;
131 i_size = GetDWBE( &p_peek[4] );
133 msg_Dbg( p_demux, "chunk fcc=%4.4s size=%d", p_peek, i_size );
135 if( !memcmp( p_peek, "COMM", 4 ) )
137 if( stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 )
138 goto error;
140 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
141 p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
142 p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
143 p_sys->fmt.audio.i_rate = GetF80BE( &p_peek[16] );
145 msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
146 GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), GetF80BE( &p_peek[16] ) );
148 else if( !memcmp( p_peek, "SSND", 4 ) )
150 if( stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 )
151 goto error;
153 p_sys->i_ssnd_pos = stream_Tell( p_demux->s );
154 p_sys->i_ssnd_size = i_size;
155 p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
156 p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );
158 msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
159 p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
161 if( p_sys->i_ssnd_pos >= 12 && p_sys->fmt.i_cat == AUDIO_ES )
163 /* We have found the 2 needed chunks */
164 break;
167 /* Skip this chunk */
168 i_size += 8;
169 if( (i_size % 2) != 0 )
170 i_size++;
171 if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size )
173 msg_Warn( p_demux, "incomplete file" );
174 goto error;
178 p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
179 p_sys->i_ssnd_end = p_sys->i_ssnd_start + p_sys->i_ssnd_size;
181 p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
182 ((p_sys->fmt.audio.i_bitspersample + 7) / 8);
184 if( p_sys->i_ssnd_fsize <= 0 )
186 msg_Err( p_demux, "invalid audio parameters" );
187 goto error;
190 if( p_sys->i_ssnd_size <= 0 )
192 /* unknown */
193 p_sys->i_ssnd_end = 0;
196 /* seek into SSND chunk */
197 if( stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
199 msg_Err( p_demux, "cannot seek to data chunk" );
200 goto error;
203 /* */
204 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
206 return VLC_SUCCESS;
208 error:
209 free( p_sys );
210 return VLC_EGENERIC;
213 /*****************************************************************************
214 * Close
215 *****************************************************************************/
216 static void Close( vlc_object_t *p_this )
218 demux_t *p_demux = (demux_t*)p_this;
219 demux_sys_t *p_sys = p_demux->p_sys;
221 free( p_sys );
225 /*****************************************************************************
226 * Demux:
227 *****************************************************************************/
228 static int Demux( demux_t *p_demux )
230 demux_sys_t *p_sys = p_demux->p_sys;
231 int64_t i_tell = stream_Tell( p_demux->s );
233 block_t *p_block;
234 int i_read;
236 if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
238 /* EOF */
239 return 0;
242 /* Set PCR */
243 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time);
245 /* we will read 100ms at once */
246 i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
247 if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
249 i_read = p_sys->i_ssnd_end - i_tell;
251 if( ( p_block = stream_Block( p_demux->s, i_read ) ) == NULL )
253 return 0;
256 p_block->i_dts =
257 p_block->i_pts = VLC_TS_0 + p_sys->i_time;
259 p_sys->i_time += (int64_t)1000000 *
260 p_block->i_buffer /
261 p_sys->i_ssnd_fsize /
262 p_sys->fmt.audio.i_rate;
264 /* */
265 es_out_Send( p_demux->out, p_sys->es, p_block );
266 return 1;
269 /*****************************************************************************
270 * Control:
271 *****************************************************************************/
272 static int Control( demux_t *p_demux, int i_query, va_list args )
274 demux_sys_t *p_sys = p_demux->p_sys;
275 double f, *pf;
276 int64_t *pi64;
278 switch( i_query )
280 case DEMUX_GET_POSITION:
282 int64_t i_start = p_sys->i_ssnd_start;
283 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
284 int64_t i_tell = stream_Tell( p_demux->s );
286 pf = (double*) va_arg( args, double* );
288 if( i_start < i_end )
290 *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
291 return VLC_SUCCESS;
293 return VLC_EGENERIC;
296 case DEMUX_SET_POSITION:
298 int64_t i_start = p_sys->i_ssnd_start;
299 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
301 f = (double) va_arg( args, double );
303 if( i_start < i_end )
305 int i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
306 int64_t i_new = i_start + i_frame * p_sys->i_ssnd_fsize;
308 if( stream_Seek( p_demux->s, i_new ) )
310 return VLC_EGENERIC;
312 p_sys->i_time = (int64_t)1000000 * i_frame / p_sys->fmt.audio.i_rate;
313 return VLC_SUCCESS;
315 return VLC_EGENERIC;
318 case DEMUX_GET_TIME:
319 pi64 = (int64_t*)va_arg( args, int64_t * );
320 *pi64 = p_sys->i_time;
321 return VLC_SUCCESS;
323 case DEMUX_GET_LENGTH:
325 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
327 pi64 = (int64_t*)va_arg( args, int64_t * );
328 if( p_sys->i_ssnd_start < i_end )
330 *pi64 = (int64_t)1000000 * ( i_end - p_sys->i_ssnd_start ) / p_sys->i_ssnd_fsize / p_sys->fmt.audio.i_rate;
331 return VLC_SUCCESS;
333 return VLC_EGENERIC;
335 case DEMUX_SET_TIME:
336 case DEMUX_GET_FPS:
337 default:
338 return VLC_EGENERIC;