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