1 /*****************************************************************************
2 * au.c : au file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
38 * - all adpcm things (I _NEED_ samples)
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t
* );
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
, NULL
)
56 /*****************************************************************************
58 *****************************************************************************/
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 */
91 vlc_tick_t i_frame_length
;
93 uint32_t i_header_size
;
96 static int Demux( demux_t
* );
97 static int Control ( demux_t
*, int i_query
, va_list args
);
99 /*****************************************************************************
100 * Open: check file and initializes structures
101 *****************************************************************************/
102 static int Open( vlc_object_t
*p_this
)
104 demux_t
*p_demux
= (demux_t
*)p_this
;
107 const uint8_t *p_peek
;
110 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 4 ) < 4 )
113 if( memcmp( p_peek
, ".snd", 4 ) )
117 if( vlc_stream_Read( p_demux
->s
, NULL
, 4 ) < 4 )
121 if( vlc_stream_Read( p_demux
->s
, hdr
, 20 ) < 20 )
123 msg_Err( p_demux
, "cannot read" );
127 if( GetDWBE( &hdr
[0] ) < 24 )
129 msg_Err( p_demux
, "invalid file" );
133 demux_sys_t
*p_sys
= vlc_obj_malloc( p_this
, sizeof (*p_sys
) );
134 if( unlikely(p_sys
== NULL
) )
138 p_sys
->i_header_size
= GetDWBE( &hdr
[0] );
140 /* skip extra header data */
141 if( p_sys
->i_header_size
> 24 )
143 #if (SSIZE_MAX <= INT32_MAX)
144 if( p_sys
->i_header_size
> SSIZE_MAX
)
147 size_t skip
= p_sys
->i_header_size
- 24;
148 if( vlc_stream_Read( p_demux
->s
, NULL
, skip
) < (ssize_t
)skip
)
153 es_format_Init( &p_sys
->fmt
, AUDIO_ES
, 0 );
154 p_sys
->fmt
.audio
.i_rate
= GetDWBE( &hdr
[12] );
155 p_sys
->fmt
.audio
.i_channels
= GetDWBE( &hdr
[16] );
158 p_sys
->au
.i_header_size
= GetDWBE( &p_sys
->au
.i_header_size
);
159 p_sys
->au
.i_data_size
= GetDWBE( &p_sys
->au
.i_data_size
);
160 p_sys
->au
.i_encoding
= GetDWBE( &p_sys
->au
.i_encoding
);
161 p_sys
->au
.i_sample_rate
= GetDWBE( &p_sys
->au
.i_sample_rate
);
162 p_sys
->au
.i_channels
= GetDWBE( &p_sys
->au
.i_channels
);
164 switch( GetDWBE( &hdr
[8] ) )
166 case AU_ALAW_8
: /* 8-bit ISDN A-law */
167 p_sys
->fmt
.i_codec
= VLC_CODEC_ALAW
;
168 p_sys
->fmt
.audio
.i_bitspersample
= 8;
169 p_sys
->fmt
.audio
.i_blockalign
= 1 * p_sys
->fmt
.audio
.i_channels
;
173 case AU_MULAW_8
: /* 8-bit ISDN u-law */
174 p_sys
->fmt
.i_codec
= VLC_CODEC_MULAW
;
175 p_sys
->fmt
.audio
.i_bitspersample
= 8;
176 p_sys
->fmt
.audio
.i_blockalign
= 1 * p_sys
->fmt
.audio
.i_channels
;
180 case AU_LINEAR_8
: /* 8-bit linear PCM */
181 p_sys
->fmt
.i_codec
= VLC_CODEC_S8
;
182 p_sys
->fmt
.audio
.i_bitspersample
= 8;
183 p_sys
->fmt
.audio
.i_blockalign
= 1 * p_sys
->fmt
.audio
.i_channels
;
187 case AU_LINEAR_16
: /* 16-bit linear PCM */
188 p_sys
->fmt
.i_codec
= VLC_CODEC_S16B
;
189 p_sys
->fmt
.audio
.i_bitspersample
= 16;
190 p_sys
->fmt
.audio
.i_blockalign
= 2 * p_sys
->fmt
.audio
.i_channels
;
194 case AU_LINEAR_24
: /* 24-bit linear PCM */
195 p_sys
->fmt
.i_codec
= VLC_CODEC_S24B
;
196 p_sys
->fmt
.audio
.i_bitspersample
= 24;
197 p_sys
->fmt
.audio
.i_blockalign
= 3 * p_sys
->fmt
.audio
.i_channels
;
201 case AU_LINEAR_32
: /* 32-bit linear PCM */
202 p_sys
->fmt
.i_codec
= VLC_CODEC_S32B
;
203 p_sys
->fmt
.audio
.i_bitspersample
= 32;
204 p_sys
->fmt
.audio
.i_blockalign
= 4 * p_sys
->fmt
.audio
.i_channels
;
208 case AU_FLOAT
: /* 32-bit IEEE floating point */
209 p_sys
->fmt
.i_codec
= VLC_FOURCC( 'a', 'u', 0, AU_FLOAT
);
210 p_sys
->fmt
.audio
.i_bitspersample
= 32;
211 p_sys
->fmt
.audio
.i_blockalign
= 4 * p_sys
->fmt
.audio
.i_channels
;
215 case AU_DOUBLE
: /* 64-bit IEEE floating point */
216 p_sys
->fmt
.i_codec
= VLC_FOURCC( 'a', 'u', 0, AU_DOUBLE
);
217 p_sys
->fmt
.audio
.i_bitspersample
= 64;
218 p_sys
->fmt
.audio
.i_blockalign
= 8 * p_sys
->fmt
.audio
.i_channels
;
222 case AU_ADPCM_G721
: /* 4-bit CCITT g.721 ADPCM */
223 p_sys
->fmt
.i_codec
= VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G721
);
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
;
229 case AU_ADPCM_G722
: /* CCITT g.722 ADPCM */
230 p_sys
->fmt
.i_codec
= VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G722
);
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
;
236 case AU_ADPCM_G723_3
: /* CCITT g.723 3-bit ADPCM */
237 p_sys
->fmt
.i_codec
= VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G723_3
);
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
;
243 case AU_ADPCM_G723_5
: /* CCITT g.723 5-bit ADPCM */
244 p_sys
->fmt
.i_codec
= VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G723_5
);
245 p_sys
->fmt
.audio
.i_bitspersample
= 0;
246 p_sys
->fmt
.audio
.i_blockalign
= 0 * p_sys
->fmt
.audio
.i_channels
;
247 i_cat
= AU_CAT_ADPCM
;
251 msg_Warn( p_demux
, "unknown encoding=0x%x", GetDWBE( &hdr
[8] ) );
252 p_sys
->fmt
.audio
.i_bitspersample
= 0;
253 p_sys
->fmt
.audio
.i_blockalign
= 0;
254 i_cat
= AU_CAT_UNKNOWN
;
258 p_sys
->fmt
.i_bitrate
= p_sys
->fmt
.audio
.i_rate
*
259 p_sys
->fmt
.audio
.i_channels
*
260 p_sys
->fmt
.audio
.i_bitspersample
;
262 if( i_cat
== AU_CAT_UNKNOWN
|| i_cat
== AU_CAT_ADPCM
)
264 msg_Err( p_demux
, "unsupported codec/type (Please report it)" );
268 if( p_sys
->fmt
.audio
.i_rate
== 0 )
270 msg_Err( p_demux
, "invalid samplerate: 0" );
275 p_sys
->es
= es_out_Add( p_demux
->out
, &p_sys
->fmt
);
276 if( unlikely(p_sys
->es
== NULL
) )
279 /* calculate 50ms frame size/time */
280 unsigned i_samples
= __MAX( p_sys
->fmt
.audio
.i_rate
/ 20, 1 );
281 p_sys
->i_frame_size
= i_samples
* p_sys
->fmt
.audio
.i_channels
*
282 ( (p_sys
->fmt
.audio
.i_bitspersample
+ 7) / 8 );
283 if( p_sys
->fmt
.audio
.i_blockalign
> 0 )
285 unsigned mod
= p_sys
->i_frame_size
% p_sys
->fmt
.audio
.i_blockalign
;
288 p_sys
->i_frame_size
+= p_sys
->fmt
.audio
.i_blockalign
- mod
;
291 p_sys
->i_frame_length
= vlc_tick_from_samples( i_samples
,
292 p_sys
->fmt
.audio
.i_rate
);
294 p_demux
->p_sys
= p_sys
;
295 p_demux
->pf_demux
= Demux
;
296 p_demux
->pf_control
= Control
;
300 /*****************************************************************************
301 * Demux: read packet and send them to decoders
302 *****************************************************************************
303 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
304 *****************************************************************************/
305 static int Demux( demux_t
*p_demux
)
307 demux_sys_t
*p_sys
= p_demux
->p_sys
;
311 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+ p_sys
->i_time
);
313 p_block
= vlc_stream_Block( p_demux
->s
, p_sys
->i_frame_size
);
314 if( p_block
== NULL
)
316 msg_Warn( p_demux
, "cannot read data" );
317 return VLC_DEMUXER_EOF
;
321 p_block
->i_pts
= VLC_TICK_0
+ p_sys
->i_time
;
322 es_out_Send( p_demux
->out
, p_sys
->es
, p_block
);
324 p_sys
->i_time
+= p_sys
->i_frame_length
;
326 return VLC_DEMUXER_SUCCESS
;
329 /*****************************************************************************
331 *****************************************************************************/
332 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
334 demux_sys_t
*p_sys
= p_demux
->p_sys
;
336 return demux_vaControlHelper( p_demux
->s
, p_sys
->i_header_size
, -1,
337 p_sys
->fmt
.i_bitrate
, p_sys
->fmt
.audio
.i_blockalign
,