]> git.sesse.net Git - vlc/blob - modules/demux/aiff.c
Fixed potential infinite loop.
[vlc] / modules / demux / aiff.c
1 /*****************************************************************************
2  * aiff.c: Audio Interchange File Format demuxer
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35
36 /* TODO:
37  *  - ...
38  */
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open    ( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
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();
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58
59 struct demux_sys_t
60 {
61     es_format_t  fmt;
62     es_out_id_t *es;
63
64     int64_t     i_ssnd_pos;
65     int64_t     i_ssnd_size;
66     int         i_ssnd_offset;
67     int         i_ssnd_blocksize;
68
69     /* real data start */
70     int64_t     i_ssnd_start;
71     int64_t     i_ssnd_end;
72
73     int         i_ssnd_fsize;
74
75     int64_t     i_time;
76 };
77
78 static int Demux  ( demux_t *p_demux );
79 static int Control( demux_t *p_demux, int i_query, va_list args );
80
81 /* GetF80BE: read a 80 bits float in big endian */
82 static unsigned int GetF80BE( const uint8_t p[10] )
83 {
84     unsigned int i_mantissa = GetDWBE( &p[2] );
85     int          i_exp = 30 - p[1];
86     unsigned int i_last = 0;
87
88     while( i_exp-- > 0 )
89     {
90         i_last = i_mantissa;
91         i_mantissa >>= 1;
92     }
93     if( i_last&0x01 )
94     {
95         i_mantissa++;
96     }
97     return i_mantissa;
98 }
99
100 /*****************************************************************************
101  * Open
102  *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
104 {
105     demux_t     *p_demux = (demux_t*)p_this;
106     demux_sys_t *p_sys;
107
108     const uint8_t *p_peek;
109
110     if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
111         return VLC_EGENERIC;
112     if( memcmp( p_peek, "FORM", 4 )
113      || memcmp( &p_peek[8], "AIFF", 4 ) )
114         return VLC_EGENERIC;
115
116     /* skip aiff header */
117     stream_Read( p_demux->s, NULL, 12 );
118
119     /* Fill p_demux field */
120     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
121     es_format_Init( &p_sys->fmt, UNKNOWN_ES, 0 );
122     p_sys->i_time = 1;
123     p_sys->i_ssnd_pos = -1;
124
125     for( ;; )
126     {
127         uint32_t i_size;
128
129         CHECK_PEEK_GOTO( p_peek, 8 );
130         i_size = GetDWBE( &p_peek[4] );
131
132         msg_Dbg( p_demux, "chunk fcc=%4.4s size=%d", p_peek, i_size );
133
134         if( !memcmp( p_peek, "COMM", 4 ) )
135         {
136             CHECK_PEEK_GOTO( p_peek, 18+8 );
137             es_format_Init( &p_sys->fmt, AUDIO_ES, VLC_FOURCC( 't', 'w', 'o', 's' ) );
138             p_sys->fmt.audio.i_channels = GetWBE( &p_peek[8] );
139             p_sys->fmt.audio.i_bitspersample = GetWBE( &p_peek[14] );
140             p_sys->fmt.audio.i_rate     = GetF80BE( &p_peek[16] );
141
142             msg_Dbg( p_demux, "COMM: channels=%d samples_frames=%d bits=%d rate=%d",
143                      GetWBE( &p_peek[8] ), GetDWBE( &p_peek[10] ), GetWBE( &p_peek[14] ), GetF80BE( &p_peek[16] ) );
144         }
145         else if( !memcmp( p_peek, "SSND", 4 ) )
146         {
147             CHECK_PEEK_GOTO( p_peek, 8+8 );
148             p_sys->i_ssnd_pos = stream_Tell( p_demux->s );
149             p_sys->i_ssnd_size = i_size;
150             p_sys->i_ssnd_offset = GetDWBE( &p_peek[8] );
151             p_sys->i_ssnd_blocksize = GetDWBE( &p_peek[12] );
152
153             msg_Dbg( p_demux, "SSND: (offset=%d blocksize=%d)",
154                      p_sys->i_ssnd_offset, p_sys->i_ssnd_blocksize );
155         }
156         if( p_sys->i_ssnd_pos >= 12 && p_sys->fmt.i_cat == AUDIO_ES )
157         {
158             /* We have found the 2 needed chunks */
159             break;
160         }
161
162         /* Skip this chunk */
163         i_size += 8;
164         if( (i_size % 2) != 0 )
165             i_size++;
166         if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size )
167         {
168             msg_Warn( p_demux, "incomplete file" );
169             goto error;
170         }
171     }
172
173     p_sys->i_ssnd_start = p_sys->i_ssnd_pos + 16 + p_sys->i_ssnd_offset;
174     p_sys->i_ssnd_end   = p_sys->i_ssnd_start + p_sys->i_ssnd_size;
175
176     p_sys->i_ssnd_fsize = p_sys->fmt.audio.i_channels *
177                           ((p_sys->fmt.audio.i_bitspersample + 7) / 8);
178
179     if( p_sys->i_ssnd_fsize <= 0 )
180     {
181         msg_Err( p_demux, "invalid audio parameters" );
182         goto error;
183     }
184
185     if( p_sys->i_ssnd_size <= 0 )
186     {
187         /* unknown */
188         p_sys->i_ssnd_end = 0;
189     }
190
191     /* seek into SSND chunk */
192     if( stream_Seek( p_demux->s, p_sys->i_ssnd_start ) )
193     {
194         msg_Err( p_demux, "cannot seek to data chunk" );
195         goto error;
196     }
197
198     /* */
199     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
200
201     return VLC_SUCCESS;
202
203 error:
204     free( p_sys );
205     return VLC_EGENERIC;
206 }
207
208 /*****************************************************************************
209  * Close
210  *****************************************************************************/
211 static void Close( vlc_object_t *p_this )
212 {
213     demux_t     *p_demux = (demux_t*)p_this;
214     demux_sys_t *p_sys = p_demux->p_sys;
215
216     free( p_sys );
217 }
218
219
220 /*****************************************************************************
221  * Demux:
222  *****************************************************************************/
223 static int Demux( demux_t *p_demux )
224 {
225     demux_sys_t *p_sys = p_demux->p_sys;
226     int64_t     i_tell = stream_Tell( p_demux->s );
227
228     block_t     *p_block;
229     int         i_read;
230
231     if( p_sys->i_ssnd_end > 0 && i_tell >= p_sys->i_ssnd_end )
232     {
233         /* EOF */
234         return 0;
235     }
236
237     /* Set PCR */
238     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time);
239
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 )
243     {
244         i_read = p_sys->i_ssnd_end - i_tell;
245     }
246     if( ( p_block = stream_Block( p_demux->s, i_read ) ) == NULL )
247     {
248         return 0;
249     }
250
251     p_block->i_dts =
252     p_block->i_pts = p_sys->i_time;
253
254     p_sys->i_time += (int64_t)1000000 *
255                      p_block->i_buffer /
256                      p_sys->i_ssnd_fsize /
257                      p_sys->fmt.audio.i_rate;
258
259     /* */
260     es_out_Send( p_demux->out, p_sys->es, p_block );
261     return 1;
262 }
263
264 /*****************************************************************************
265  * Control:
266  *****************************************************************************/
267 static int Control( demux_t *p_demux, int i_query, va_list args )
268 {
269     demux_sys_t *p_sys = p_demux->p_sys;
270     double f, *pf;
271     int64_t *pi64;
272
273     switch( i_query )
274     {
275         case DEMUX_GET_POSITION:
276         {
277             int64_t i_start = p_sys->i_ssnd_start;
278             int64_t i_end   = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
279             int64_t i_tell  = stream_Tell( p_demux->s );
280
281             pf = (double*) va_arg( args, double* );
282
283             if( i_start < i_end )
284             {
285                 *pf = (double)(i_tell - i_start)/(double)(i_end - i_start);
286                 return VLC_SUCCESS;
287             }
288             return VLC_EGENERIC;
289         }
290
291         case DEMUX_SET_POSITION:
292         {
293             int64_t i_start = p_sys->i_ssnd_start;
294             int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
295
296             f = (double) va_arg( args, double );
297
298             if( i_start < i_end )
299             {
300                 int     i_frame = (f * ( i_end - i_start )) / p_sys->i_ssnd_fsize;
301                 int64_t i_new   = i_start + i_frame * p_sys->i_ssnd_fsize;
302
303                 if( stream_Seek( p_demux->s, i_new ) )
304                 {
305                     return VLC_EGENERIC;
306                 }
307                 p_sys->i_time = 1 + (int64_t)1000000 * i_frame / p_sys->fmt.audio.i_rate;
308                 return VLC_SUCCESS;
309             }
310             return VLC_EGENERIC;
311         }
312
313         case DEMUX_GET_TIME:
314             pi64 = (int64_t*)va_arg( args, int64_t * );
315             *pi64 = p_sys->i_time;
316             return VLC_SUCCESS;
317
318         case DEMUX_GET_LENGTH:
319         {
320             int64_t i_end  = p_sys->i_ssnd_end > 0 ? p_sys->i_ssnd_end : stream_Size( p_demux->s );
321
322             pi64 = (int64_t*)va_arg( args, int64_t * );
323             if( p_sys->i_ssnd_start < i_end )
324             {
325                 *pi64 = (int64_t)1000000 * ( i_end - p_sys->i_ssnd_start ) / p_sys->i_ssnd_fsize / p_sys->fmt.audio.i_rate;
326                 return VLC_SUCCESS;
327             }
328             return VLC_EGENERIC;
329         }
330         case DEMUX_SET_TIME:
331         case DEMUX_GET_FPS:
332         default:
333             return VLC_EGENERIC;
334     }
335 }