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