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