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