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