1 /*****************************************************************************
2 * wav.c : wav file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2008 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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 #include <vlc_codecs.h>
38 #include "windows_audio_commons.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
47 set_description( N_("WAV demuxer") )
48 set_category( CAT_INPUT )
49 set_subcategory( SUBCAT_INPUT_DEMUX )
50 set_capability( "demux", 142 )
51 set_callbacks( Open, Close )
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Demux ( demux_t * );
58 static int Control( demux_t *, int i_query, va_list args );
68 unsigned int i_frame_size;
73 uint32_t i_channel_mask;
74 uint8_t i_chans_to_reorder; /* do we need channel reordering */
75 uint8_t pi_chan_table[AOUT_CHAN_MAX];
78 static int ChunkFind( demux_t *, const char *, unsigned int * );
80 static int FrameInfo_IMA_ADPCM( unsigned int *, int *, const es_format_t * );
81 static int FrameInfo_MS_ADPCM ( unsigned int *, int *, const es_format_t * );
82 static int FrameInfo_PCM ( unsigned int *, int *, const es_format_t * );
83 static int FrameInfo_MSGSM ( unsigned int *, int *, const es_format_t * );
85 /*****************************************************************************
86 * Open: check file and initializes structures
87 *****************************************************************************/
88 static int Open( vlc_object_t * p_this )
90 demux_t *p_demux = (demux_t*)p_this;
93 const uint8_t *p_peek;
97 unsigned int i_extended;
100 WAVEFORMATEXTENSIBLE *p_wf_ext = NULL;
101 WAVEFORMATEX *p_wf = NULL;
103 /* Is it a wav file ? */
104 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
107 b_is_rf64 = ( memcmp( p_peek, "RF64", 4 ) == 0 );
108 if( ( !b_is_rf64 && memcmp( p_peek, "RIFF", 4 ) ) ||
109 memcmp( &p_peek[8], "WAVE", 4 ) )
114 p_demux->pf_demux = Demux;
115 p_demux->pf_control = Control;
116 p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
117 if( unlikely(!p_sys) )
121 p_sys->i_data_size = 0;
122 p_sys->i_chans_to_reorder = 0;
123 p_sys->i_channel_mask = 0;
125 /* skip riff header */
126 if( stream_Read( p_demux->s, NULL, 12 ) != 12 )
131 /* search datasize64 chunk */
132 if( ChunkFind( p_demux, "ds64", &i_size ) )
134 msg_Err( p_demux, "cannot find 'ds64' chunk" );
139 msg_Err( p_demux, "invalid 'ds64' chunk" );
142 if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
144 if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 )
146 i_data_size = GetQWLE( &p_peek[8] );
147 if( i_data_size >> 62 )
148 p_sys->i_data_size = (int64_t)1 << 62;
150 p_sys->i_data_size = i_data_size;
151 if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
152 ( (i_size & 1) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
156 /* search fmt chunk */
157 if( ChunkFind( p_demux, "fmt ", &i_size ) )
159 msg_Err( p_demux, "cannot find 'fmt ' chunk" );
163 if( i_size < sizeof( WAVEFORMATEX ) )
165 msg_Err( p_demux, "invalid 'fmt ' chunk" );
168 if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
172 /* load waveformatex */
173 p_wf_ext = malloc( i_size );
174 if( unlikely( !p_wf_ext ) )
177 p_wf = &p_wf_ext->Format;
180 if( stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size ||
181 ( ( i_size & 1 ) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
183 msg_Err( p_demux, "cannot load 'fmt ' chunk" );
187 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
188 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
190 p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
191 p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
192 p_sys->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );
193 p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
194 p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
195 if( i_size >= sizeof(WAVEFORMATEX) )
196 p_sys->fmt.i_extra = __MIN( GetWLE( &p_wf->cbSize ), i_size - sizeof(WAVEFORMATEX) );
199 /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
200 /* see the following link for more information:
201 * http://www.microsoft.com/whdc/device/audio/multichaud.mspx#EFAA */
202 if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
203 i_size >= sizeof( WAVEFORMATEXTENSIBLE ) &&
204 ( p_sys->fmt.i_extra + sizeof( WAVEFORMATEX )
205 >= sizeof( WAVEFORMATEXTENSIBLE ) ) )
207 unsigned i_channel_mask;
210 guid_subformat = p_wf_ext->SubFormat;
211 guid_subformat.Data1 = GetDWLE( &p_wf_ext->SubFormat.Data1 );
212 guid_subformat.Data2 = GetWLE( &p_wf_ext->SubFormat.Data2 );
213 guid_subformat.Data3 = GetWLE( &p_wf_ext->SubFormat.Data3 );
215 sf_tag_to_fourcc( &guid_subformat, &p_sys->fmt.i_codec, &psz_name );
217 i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
218 p_sys->fmt.i_extra -= i_extended;
220 i_channel_mask = GetDWLE( &p_wf_ext->dwChannelMask );
224 p_sys->i_channel_mask = getChannelMask( &i_channel_mask, p_sys->fmt.audio.i_channels, &i_match );
226 msg_Warn( p_demux, "Some channels are unrecognized or uselessly specified (0x%x)", i_channel_mask );
227 if( i_match < p_sys->fmt.audio.i_channels )
229 int i_missing = p_sys->fmt.audio.i_channels - i_match;
230 msg_Warn( p_demux, "Trying to fill up unspecified position for %d channels", p_sys->fmt.audio.i_channels - i_match );
232 static const uint32_t pi_pair[] = { AOUT_CHAN_REARLEFT|AOUT_CHAN_REARRIGHT,
233 AOUT_CHAN_MIDDLELEFT|AOUT_CHAN_MIDDLERIGHT,
234 AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT };
236 static const uint32_t pi_center[] = { AOUT_CHAN_REARCENTER,
238 AOUT_CHAN_CENTER }; */
240 /* Try to complete with pair */
241 for( unsigned i = 0; i < sizeof(pi_pair)/sizeof(*pi_pair); i++ )
243 if( i_missing >= 2 && !(p_sys->i_channel_mask & pi_pair[i] ) )
246 p_sys->i_channel_mask |= pi_pair[i];
249 /* Well fill up with what we can */
250 for( unsigned i = 0; i < sizeof(pi_channels_aout)/sizeof(*pi_channels_aout) && i_missing > 0; i++ )
252 if( !( p_sys->i_channel_mask & pi_channels_aout[i] ) )
254 p_sys->i_channel_mask |= pi_channels_aout[i];
262 i_match = p_sys->fmt.audio.i_channels - i_missing;
264 if( i_match < p_sys->fmt.audio.i_channels )
266 msg_Err( p_demux, "Invalid/unsupported channel mask" );
267 p_sys->i_channel_mask = 0;
271 else if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_PCM &&
272 p_sys->fmt.audio.i_channels > 2 && p_sys->fmt.audio.i_channels <= 9 )
274 for( int i = 0; i < p_sys->fmt.audio.i_channels; i++ )
275 p_sys->i_channel_mask |= pi_channels_aout[i];
278 if( p_sys->i_channel_mask )
280 if( p_sys->fmt.i_codec == VLC_FOURCC('a','r','a','w') ||
281 p_sys->fmt.i_codec == VLC_FOURCC('a','f','l','t') )
282 p_sys->i_chans_to_reorder =
283 aout_CheckChannelReorder( pi_channels_aout, NULL,
284 p_sys->i_channel_mask,
285 p_sys->pi_chan_table );
287 msg_Dbg( p_demux, "channel mask: %x, reordering: %u",
288 p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
291 p_sys->fmt.audio.i_physical_channels =
292 p_sys->fmt.audio.i_original_channels = p_sys->i_channel_mask;
294 if( p_sys->fmt.i_extra > 0 )
296 p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
297 if( unlikely(!p_sys->fmt.p_extra) )
299 p_sys->fmt.i_extra = 0;
302 memcpy( p_sys->fmt.p_extra, (uint8_t *)p_wf + sizeof( WAVEFORMATEX ) + i_extended,
303 p_sys->fmt.i_extra );
306 msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
307 "freq: %u Hz, bitrate: %uKo/s, blockalign: %d, bits/samples: %d, "
309 GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
310 p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
311 p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
312 p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
317 switch( p_sys->fmt.i_codec )
319 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
320 case VLC_FOURCC( 'a', 'f', 'l', 't' ):
321 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
323 case VLC_CODEC_MULAW:
324 if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
328 vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec,
329 p_sys->fmt.audio.i_bitspersample );
330 if( p_sys->fmt.i_codec == 0 ) {
331 msg_Err( p_demux, "Unrecognized codec" );
335 case VLC_CODEC_ADPCM_MS:
336 /* FIXME not sure at all FIXME */
337 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
338 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
339 if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
343 case VLC_CODEC_ADPCM_IMA_WAV:
344 if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
350 /* FIXME set end of area FIXME */
352 case VLC_CODEC_GSM_MS:
353 case VLC_CODEC_ADPCM_G726:
354 case VLC_CODEC_TRUESPEECH:
355 case VLC_CODEC_ATRAC3:
356 case VLC_CODEC_G723_1:
358 if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
363 msg_Err( p_demux, "unsupported codec (%4.4s)",
364 (char*)&p_sys->fmt.i_codec );
368 if( p_sys->i_frame_size <= 0 || p_sys->i_frame_samples <= 0 )
370 msg_Dbg( p_demux, "invalid frame size: %i %i", p_sys->i_frame_size,
371 p_sys->i_frame_samples );
374 if( p_sys->fmt.audio.i_rate <= 0 )
376 msg_Dbg( p_demux, "invalid sample rate: %i", p_sys->fmt.audio.i_rate );
380 msg_Dbg( p_demux, "found %s audio format", psz_name );
382 if( ChunkFind( p_demux, "data", &i_size ) )
384 msg_Err( p_demux, "cannot find 'data' chunk" );
387 if( !b_is_rf64 || i_size < UINT32_MAX )
388 p_sys->i_data_size = i_size;
389 if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
391 p_sys->i_data_pos = stream_Tell( p_demux->s );
393 if( p_sys->fmt.i_bitrate <= 0 )
395 p_sys->fmt.i_bitrate = (int64_t)p_sys->i_frame_size *
396 p_sys->fmt.audio.i_rate * 8 / p_sys->i_frame_samples;
399 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
401 date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
402 date_Set( &p_sys->pts, 1 );
407 msg_Err( p_demux, "An error occurred during wav demuxing" );
413 /*****************************************************************************
414 * Demux: read packet and send them to decoders
415 *****************************************************************************
416 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
417 *****************************************************************************/
418 static int Demux( demux_t *p_demux )
420 demux_sys_t *p_sys = p_demux->p_sys;
422 const int64_t i_pos = stream_Tell( p_demux->s );
423 unsigned int i_read_size = p_sys->i_frame_size;
425 if( p_sys->i_data_size > 0 )
427 int64_t i_end = p_sys->i_data_pos + p_sys->i_data_size;
428 if ( i_pos >= i_end )
431 /* Don't read past data chunk boundary */
432 if ( i_end < i_pos + i_read_size )
433 i_read_size = i_end - i_pos;
436 if( ( p_block = stream_Block( p_demux->s, i_read_size ) ) == NULL )
438 msg_Warn( p_demux, "cannot read data" );
443 p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
446 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
448 /* Do the channel reordering */
449 if( p_sys->i_chans_to_reorder )
450 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
451 p_sys->fmt.audio.i_channels,
452 p_sys->pi_chan_table, p_sys->fmt.i_codec );
454 es_out_Send( p_demux->out, p_sys->p_es, p_block );
456 date_Increment( &p_sys->pts, p_sys->i_frame_samples );
461 /*****************************************************************************
462 * Close: frees unused data
463 *****************************************************************************/
464 static void Close ( vlc_object_t * p_this )
466 demux_t *p_demux = (demux_t*)p_this;
467 demux_sys_t *p_sys = p_demux->p_sys;
472 /*****************************************************************************
474 *****************************************************************************/
475 static int Control( demux_t *p_demux, int i_query, va_list args )
477 demux_sys_t *p_sys = p_demux->p_sys;
480 if( p_sys->i_data_size > 0 )
481 i_end = p_sys->i_data_pos + p_sys->i_data_size;
483 return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, i_end,
484 p_sys->fmt.i_bitrate,
485 p_sys->fmt.audio.i_blockalign,
489 /*****************************************************************************
491 *****************************************************************************/
492 static int ChunkFind( demux_t *p_demux, const char *fcc, unsigned int *pi_size )
494 const uint8_t *p_peek;
500 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
502 msg_Err( p_demux, "cannot peek" );
506 i_size = GetDWLE( p_peek + 4 );
508 msg_Dbg( p_demux, "chunk: fcc=`%4.4s` size=%"PRIu32, p_peek, i_size );
510 if( !memcmp( p_peek, fcc, 4 ) )
520 if( stream_Read( p_demux->s, NULL, 8 ) != 8 ||
521 stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
522 ( (i_size & 1) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
527 static int FrameInfo_PCM( unsigned int *pi_size, int *pi_samples,
528 const es_format_t *p_fmt )
532 /* read samples for 50ms of */
533 *pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
535 i_bytes = *pi_samples * p_fmt->audio.i_channels *
536 ( (p_fmt->audio.i_bitspersample + 7) / 8 );
538 if( p_fmt->audio.i_blockalign > 0 )
540 const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
542 i_bytes += p_fmt->audio.i_blockalign - i_modulo;
549 static int FrameInfo_MS_ADPCM( unsigned int *pi_size, int *pi_samples,
550 const es_format_t *p_fmt )
552 if( p_fmt->audio.i_channels <= 0 )
555 *pi_samples = 2 + 2 * ( p_fmt->audio.i_blockalign -
556 7 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
557 *pi_size = p_fmt->audio.i_blockalign;
562 static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, int *pi_samples,
563 const es_format_t *p_fmt )
565 if( p_fmt->audio.i_channels <= 0 )
568 *pi_samples = 2 * ( p_fmt->audio.i_blockalign -
569 4 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
570 *pi_size = p_fmt->audio.i_blockalign;
575 static int FrameInfo_MSGSM( unsigned int *pi_size, int *pi_samples,
576 const es_format_t *p_fmt )
578 if( p_fmt->i_bitrate <= 0 )
581 *pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
583 *pi_size = p_fmt->audio.i_blockalign;