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