demux: mp4: use static mapping table per layout
[vlc.git] / modules / demux / aiff.c
blob2aeb99c0a9c9c7dcfa353bbc05ae891223a590c3
1 /*****************************************************************************
2 * aiff.c: Audio Interchange File Format demuxer
3 *****************************************************************************
4 * Copyright (C) 2004-2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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>
35 #include <limits.h>
37 /* TODO:
38 * - ...
41 /*****************************************************************************
42 * Module descriptor
43 *****************************************************************************/
44 static int Open ( 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, NULL )
52 add_shortcut( "aiff" )
53 vlc_module_end ()
55 /*****************************************************************************
56 * Local prototypes
57 *****************************************************************************/
59 typedef struct
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 vlc_tick_t i_time;
76 } demux_sys_t;
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;
107 const uint8_t *p_peek;
109 if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
110 return VLC_EGENERIC;
111 if( memcmp( p_peek, "FORM", 4 ) || memcmp( &p_peek[8], "AIFF", 4 ) )
112 return VLC_EGENERIC;
114 /* skip aiff header */
115 if( vlc_stream_Read( p_demux->s, NULL, 12 ) < 12 )
116 return VLC_EGENERIC;
118 /* Fill p_demux field */
119 demux_sys_t *p_sys = vlc_obj_calloc( p_this, 1, sizeof (*p_sys) );
120 es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
121 p_sys->i_time = 0;
122 p_sys->i_ssnd_pos = -1;
124 for( ;; )
126 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
127 return VLC_EGENERIC;
129 uint32_t i_data_size = GetDWBE( &p_peek[4] );
130 uint64_t i_chunk_size = UINT64_C( 8 ) + i_data_size + ( i_data_size & 1 );
132 msg_Dbg( p_demux, "chunk fcc=%4.4s size=%" PRIu64 " data_size=%" PRIu32,
133 p_peek, i_chunk_size, i_data_size );
135 if( !memcmp( p_peek, "COMM", 4 ) )
137 if( vlc_stream_Peek( p_demux->s, &p_peek, 18+8 ) < 18+8 )
138 return VLC_EGENERIC;
140 p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
141 p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
142 p_sys->fmt.audio.i_rate = GetF80BE( &p_peek[16] );
144 msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
145 GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ),
146 GetF80BE( &p_peek[16] ) );
148 else if( !memcmp( p_peek, "SSND", 4 ) )
150 if( vlc_stream_Peek( p_demux->s, &p_peek, 8+8 ) < 8+8 )
151 return VLC_EGENERIC;
153 p_sys->i_ssnd_pos = vlc_stream_Tell( p_demux->s );
154 p_sys->i_ssnd_size = i_data_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.audio.i_channels != 0 )
163 /* We have found the 2 needed chunks */
164 break;
167 /* consume chunk data */
168 for( ssize_t i_req; i_chunk_size; i_chunk_size -= i_req )
170 #if SSIZE_MAX < UINT64_MAX
171 i_req = __MIN( SSIZE_MAX, i_chunk_size );
172 #else
173 i_req = i_chunk_size;
174 #endif
175 if( vlc_stream_Read( p_demux->s, NULL, i_req ) != i_req )
177 msg_Warn( p_demux, "incomplete file" );
178 return VLC_EGENERIC;
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 || p_sys->fmt.audio.i_rate == 0 )
191 msg_Err( p_demux, "invalid audio parameters" );
192 return VLC_EGENERIC;
195 if( p_sys->i_ssnd_size <= 0 )
197 /* unknown */
198 p_sys->i_ssnd_end = 0;
201 /* seek into SSND chunk */
202 if( vlc_stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
204 msg_Err( p_demux, "cannot seek to data chunk" );
205 return VLC_EGENERIC;
208 /* */
209 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
210 if( unlikely(p_sys->es == NULL) )
211 return VLC_ENOMEM;
213 p_demux->pf_demux = Demux;
214 p_demux->pf_control = Control;
215 p_demux->p_sys = p_sys;
217 return VLC_SUCCESS;
220 /*****************************************************************************
221 * Demux:
222 *****************************************************************************/
223 static int Demux( demux_t *p_demux )
225 demux_sys_t *p_sys = p_demux->p_sys;
226 int64_t i_tell = vlc_stream_Tell( p_demux->s );
228 block_t *p_block;
229 int i_read;
231 if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
233 /* EOF */
234 return VLC_DEMUXER_EOF;
237 /* Set PCR */
238 es_out_SetPCR( p_demux->out, VLC_TICK_0 + p_sys->i_time);
240 /* we will read 100ms at once */
241 i_read = p_sys->i_ssnd_fsize * ( p_sys->fmt.audio.i_rate / 10 );
242 if( p_sys->i_ssnd_end > 0 && p_sys->i_ssnd_end - i_tell < i_read )
244 i_read = p_sys->i_ssnd_end - i_tell;
246 if( ( p_block = vlc_stream_Block( p_demux->s, i_read ) ) == NULL )
248 return VLC_DEMUXER_EOF;
251 p_block->i_dts =
252 p_block->i_pts = VLC_TICK_0 + p_sys->i_time;
254 p_sys->i_time += vlc_tick_from_samples(p_block->i_buffer,
255 p_sys->i_ssnd_fsize) /
256 p_sys->fmt.audio.i_rate;
258 /* */
259 es_out_Send( p_demux->out, p_sys->es, p_block );
260 return VLC_DEMUXER_SUCCESS;
263 /*****************************************************************************
264 * Control:
265 *****************************************************************************/
266 static int Control( demux_t *p_demux, int i_query, va_list args )
268 demux_sys_t *p_sys = p_demux->p_sys;
269 double f, *pf;
271 switch( i_query )
273 case DEMUX_CAN_SEEK:
274 return vlc_stream_vaControl( p_demux->s, i_query, args );
276 case DEMUX_GET_POSITION:
278 int64_t i_start = p_sys->i_ssnd_start;
279 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
280 int64_t i_tell = vlc_stream_Tell( p_demux->s );
282 pf = va_arg( args, double * );
284 if( i_start < i_end )
286 *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
287 return VLC_SUCCESS;
289 return VLC_EGENERIC;
292 case DEMUX_SET_POSITION:
294 int64_t i_start = p_sys->i_ssnd_start;
295 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
297 f = va_arg( args, double );
299 if( i_start < i_end )
301 int i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
302 int64_t i_new = i_start + i_frame * p_sys->i_ssnd_fsize;
304 if( vlc_stream_Seek( p_demux->s, i_new ) )
306 return VLC_EGENERIC;
308 p_sys->i_time = vlc_tick_from_samples( i_frame, p_sys->fmt.audio.i_rate );
309 return VLC_SUCCESS;
311 return VLC_EGENERIC;
314 case DEMUX_GET_TIME:
315 *va_arg( args, vlc_tick_t * ) = p_sys->i_time;
316 return VLC_SUCCESS;
318 case DEMUX_GET_LENGTH:
320 int64_t i_end = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
322 if( p_sys->i_ssnd_start < i_end )
324 *va_arg( args, vlc_tick_t * ) =
325 vlc_tick_from_samples( i_end - p_sys->i_ssnd_start, p_sys->i_ssnd_fsize) / p_sys->fmt.audio.i_rate;
326 return VLC_SUCCESS;
328 return VLC_EGENERIC;
330 case DEMUX_SET_TIME:
331 case DEMUX_GET_FPS:
332 return VLC_EGENERIC;
334 case DEMUX_CAN_PAUSE:
335 case DEMUX_SET_PAUSE_STATE:
336 case DEMUX_CAN_CONTROL_PACE:
337 case DEMUX_GET_PTS_DELAY:
338 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
340 default:
341 return VLC_EGENERIC;