]> git.sesse.net Git - vlc/blob - modules/demux/au.c
3b2bd272798b584169c2ceaefb6bb50b37e4c10d
[vlc] / modules / demux / au.c
1 /*****************************************************************************
2  * au.c : au file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-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  *  - all adpcm things (I _NEED_ samples)
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( _("AU demuxer") );
50     set_capability( "demux", 10 );
51     set_callbacks( Open, Close );
52     add_shortcut( "au" );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 enum AuType_e
59 {
60     AU_UNKNOWN      =  0,
61     AU_MULAW_8      =  1,  /* 8-bit ISDN u-law */
62     AU_LINEAR_8     =  2,  /* 8-bit linear PCM */
63     AU_LINEAR_16    =  3,  /* 16-bit linear PCM */
64     AU_LINEAR_24    =  4,  /* 24-bit linear PCM */
65     AU_LINEAR_32    =  5,  /* 32-bit linear PCM */
66     AU_FLOAT        =  6,  /* 32-bit IEEE floating point */
67     AU_DOUBLE       =  7,  /* 64-bit IEEE floating point */
68     AU_ADPCM_G721   =  23, /* 4-bit CCITT g.721 ADPCM */
69     AU_ADPCM_G722   =  24, /* CCITT g.722 ADPCM */
70     AU_ADPCM_G723_3 =  25, /* CCITT g.723 3-bit ADPCM */
71     AU_ADPCM_G723_5 =  26, /* CCITT g.723 5-bit ADPCM */
72     AU_ALAW_8       =  27  /* 8-bit ISDN A-law */
73 };
74
75 enum AuCat_e
76 {
77     AU_CAT_UNKNOWN  = 0,
78     AU_CAT_PCM      = 1,
79     AU_CAT_ADPCM    = 2
80 };
81
82 struct demux_sys_t
83 {
84     es_format_t     fmt;
85     es_out_id_t     *es;
86
87     mtime_t         i_time;
88
89     int             i_frame_size;
90     mtime_t         i_frame_length;
91
92     int             i_header_size;
93 };
94
95 static int Demux( demux_t * );
96 static int Control ( demux_t *, int i_query, va_list args );
97
98 /*****************************************************************************
99  * Open: check file and initializes structures
100  *****************************************************************************/
101 static int Open( vlc_object_t *p_this )
102 {
103     demux_t     *p_demux = (demux_t*)p_this;
104     demux_sys_t *p_sys;
105
106     uint8_t      hdr[20];
107     const uint8_t *p_peek;
108     int          i_cat;
109     int          i_samples, i_modulo;
110
111     CHECK_PEEK( p_peek, 4 );
112
113     if( memcmp( p_peek, ".snd", 4 ) )
114     {
115         return VLC_EGENERIC;
116     }
117
118     /* skip signature */
119     stream_Read( p_demux->s, NULL, 4 );   /* cannot fail */
120
121     /* read header */
122     if( stream_Read( p_demux->s, hdr, 20 ) < 20 )
123     {
124         msg_Err( p_demux, "cannot read" );
125         return VLC_EGENERIC;
126     }
127
128     if( GetDWBE( &hdr[0]  ) < 24 )
129     {
130         msg_Err( p_demux, "invalid file" );
131         return VLC_EGENERIC;
132     }
133
134     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
135     p_sys->i_time = 1;
136     p_sys->i_header_size = GetDWBE( &hdr[0] );
137
138     /* skip extra header data */
139     if( p_sys->i_header_size > 24 )
140     {
141         stream_Read( p_demux->s, NULL, p_sys->i_header_size - 24 );
142     }
143
144     /* init fmt */
145     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
146     p_sys->fmt.audio.i_rate     = GetDWBE( &hdr[12] );
147     p_sys->fmt.audio.i_channels = GetDWBE( &hdr[16] );
148
149 #if 0
150     p_sys->au.i_header_size   = GetDWBE( &p_sys->au.i_header_size );
151     p_sys->au.i_data_size     = GetDWBE( &p_sys->au.i_data_size );
152     p_sys->au.i_encoding      = GetDWBE( &p_sys->au.i_encoding );
153     p_sys->au.i_sample_rate   = GetDWBE( &p_sys->au.i_sample_rate );
154     p_sys->au.i_channels      = GetDWBE( &p_sys->au.i_channels );
155 #endif
156     switch( GetDWBE( &hdr[8] ) )
157     {
158         case AU_ALAW_8:        /* 8-bit ISDN A-law */
159             p_sys->fmt.i_codec               = VLC_FOURCC( 'a','l','a','w' );
160             p_sys->fmt.audio.i_bitspersample = 8;
161             p_sys->fmt.audio.i_blockalign    = 1 * p_sys->fmt.audio.i_channels;
162             i_cat                    = AU_CAT_PCM;
163             break;
164
165         case AU_MULAW_8:       /* 8-bit ISDN u-law */
166             p_sys->fmt.i_codec               = VLC_FOURCC( 'u','l','a','w' );
167             p_sys->fmt.audio.i_bitspersample = 8;
168             p_sys->fmt.audio.i_blockalign    = 1 * p_sys->fmt.audio.i_channels;
169             i_cat                    = AU_CAT_PCM;
170             break;
171
172         case AU_LINEAR_8:      /* 8-bit linear PCM */
173             p_sys->fmt.i_codec               = VLC_FOURCC( 't','w','o','s' );
174             p_sys->fmt.audio.i_bitspersample = 8;
175             p_sys->fmt.audio.i_blockalign    = 1 * p_sys->fmt.audio.i_channels;
176             i_cat                    = AU_CAT_PCM;
177             break;
178
179         case AU_LINEAR_16:     /* 16-bit linear PCM */
180             p_sys->fmt.i_codec               = VLC_FOURCC( 't','w','o','s' );
181             p_sys->fmt.audio.i_bitspersample = 16;
182             p_sys->fmt.audio.i_blockalign    = 2 * p_sys->fmt.audio.i_channels;
183             i_cat                    = AU_CAT_PCM;
184             break;
185
186         case AU_LINEAR_24:     /* 24-bit linear PCM */
187             p_sys->fmt.i_codec               = VLC_FOURCC( 't','w','o','s' );
188             p_sys->fmt.audio.i_bitspersample = 24;
189             p_sys->fmt.audio.i_blockalign    = 3 * p_sys->fmt.audio.i_channels;
190             i_cat                    = AU_CAT_PCM;
191             break;
192
193         case AU_LINEAR_32:     /* 32-bit linear PCM */
194             p_sys->fmt.i_codec               = VLC_FOURCC( 't','w','o','s' );
195             p_sys->fmt.audio.i_bitspersample = 32;
196             p_sys->fmt.audio.i_blockalign    = 4 * p_sys->fmt.audio.i_channels;
197             i_cat                    = AU_CAT_PCM;
198             break;
199
200         case AU_FLOAT:         /* 32-bit IEEE floating point */
201             p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_FLOAT );
202             p_sys->fmt.audio.i_bitspersample = 32;
203             p_sys->fmt.audio.i_blockalign    = 4 * p_sys->fmt.audio.i_channels;
204             i_cat                    = AU_CAT_PCM;
205             break;
206
207         case AU_DOUBLE:        /* 64-bit IEEE floating point */
208             p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_DOUBLE );
209             p_sys->fmt.audio.i_bitspersample = 64;
210             p_sys->fmt.audio.i_blockalign    = 8 * p_sys->fmt.audio.i_channels;
211             i_cat                    = AU_CAT_PCM;
212             break;
213
214         case AU_ADPCM_G721:    /* 4-bit CCITT g.721 ADPCM */
215             p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G721 );
216             p_sys->fmt.audio.i_bitspersample = 0;
217             p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
218             i_cat                    = AU_CAT_ADPCM;
219             break;
220
221         case AU_ADPCM_G722:    /* CCITT g.722 ADPCM */
222             p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G722 );
223             p_sys->fmt.audio.i_bitspersample = 0;
224             p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
225             i_cat                    = AU_CAT_ADPCM;
226             break;
227
228         case AU_ADPCM_G723_3:  /* CCITT g.723 3-bit ADPCM */
229             p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G723_3 );
230             p_sys->fmt.audio.i_bitspersample = 0;
231             p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
232             i_cat                    = AU_CAT_ADPCM;
233             break;
234
235         case AU_ADPCM_G723_5:  /* CCITT g.723 5-bit ADPCM */
236             p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G723_5 );
237             p_sys->fmt.audio.i_bitspersample = 0;
238             p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
239             i_cat                    = AU_CAT_ADPCM;
240             break;
241
242         default:
243             msg_Warn( p_demux, "unknow encoding=0x%x", GetDWBE( &hdr[8] ) );
244             p_sys->fmt.audio.i_bitspersample = 0;
245             p_sys->fmt.audio.i_blockalign    = 0;
246             i_cat                    = AU_CAT_UNKNOWN;
247             break;
248     }
249
250     p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
251                            p_sys->fmt.audio.i_channels *
252                            p_sys->fmt.audio.i_bitspersample;
253
254     if( i_cat == AU_CAT_UNKNOWN || i_cat == AU_CAT_ADPCM )
255     {
256         p_sys->i_frame_size = 0;
257         p_sys->i_frame_length = 0;
258
259         msg_Err( p_demux, "unsupported codec/type (Please report it)" );
260         free( p_sys );
261         return VLC_EGENERIC;
262     }
263
264     /* add the es */
265     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
266
267     /* calculate 50ms frame size/time */
268     i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
269     p_sys->i_frame_size = i_samples * p_sys->fmt.audio.i_channels *
270                           ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
271     if( p_sys->fmt.audio.i_blockalign > 0 )
272     {
273         if( ( i_modulo = p_sys->i_frame_size % p_sys->fmt.audio.i_blockalign ) != 0 )
274         {
275             p_sys->i_frame_size += p_sys->fmt.audio.i_blockalign - i_modulo;
276         }
277     }
278     p_sys->i_frame_length = (mtime_t)1000000 *
279                             (mtime_t)i_samples /
280                             (mtime_t)p_sys->fmt.audio.i_rate;
281
282     return VLC_SUCCESS;
283 }
284
285 /*****************************************************************************
286  * Demux: read packet and send them to decoders
287  *****************************************************************************
288  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
289  *****************************************************************************/
290 static int Demux( demux_t *p_demux )
291 {
292     demux_sys_t *p_sys = p_demux->p_sys;
293     block_t     *p_block;
294
295     /* set PCR */
296     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
297
298     if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
299     {
300         msg_Warn( p_demux, "cannot read data" );
301         return 0;
302     }
303
304     p_block->i_dts =
305     p_block->i_pts = p_sys->i_time;
306
307     es_out_Send( p_demux->out, p_sys->es, p_block );
308
309     p_sys->i_time += p_sys->i_frame_length;
310
311     return 1;
312 }
313
314 /*****************************************************************************
315  * Close: frees unused data
316  *****************************************************************************/
317 static void Close( vlc_object_t * p_this )
318 {
319     demux_t     *p_demux = (demux_t*)p_this;
320     demux_sys_t *p_sys = p_demux->p_sys;
321
322     free( p_sys );
323 }
324
325 /*****************************************************************************
326  * Control:
327  *****************************************************************************/
328 static int Control( demux_t *p_demux, int i_query, va_list args )
329 {
330     demux_sys_t *p_sys = p_demux->p_sys;
331
332     return demux_vaControlHelper( p_demux->s, p_sys->i_header_size, -1,
333                                    p_sys->fmt.i_bitrate, p_sys->fmt.audio.i_blockalign,
334                                    i_query, args );
335 }
336