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 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
45 set_description( N_("WAV demuxer") )
46 set_category( CAT_INPUT )
47 set_subcategory( SUBCAT_INPUT_DEMUX )
48 set_capability( "demux", 142 )
49 set_callbacks( Open, Close )
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Demux ( demux_t * );
56 static int Control( demux_t *, int i_query, va_list args );
66 unsigned int i_frame_size;
71 uint32_t i_channel_mask;
72 uint8_t i_chans_to_reorder; /* do we need channel reordering */
73 uint8_t pi_chan_table[AOUT_CHAN_MAX];
76 static int ChunkFind( demux_t *, const char *, unsigned int * );
78 static int FrameInfo_IMA_ADPCM( unsigned int *, int *, const es_format_t * );
79 static int FrameInfo_MS_ADPCM ( unsigned int *, int *, const es_format_t * );
80 static int FrameInfo_PCM ( unsigned int *, int *, const es_format_t * );
81 static int FrameInfo_MSGSM ( unsigned int *, int *, const es_format_t * );
83 static const uint32_t pi_channels_src[] =
84 { WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
85 WAVE_SPEAKER_FRONT_CENTER, WAVE_SPEAKER_LOW_FREQUENCY,
86 WAVE_SPEAKER_BACK_LEFT, WAVE_SPEAKER_BACK_RIGHT, WAVE_SPEAKER_BACK_CENTER,
87 WAVE_SPEAKER_SIDE_LEFT, WAVE_SPEAKER_SIDE_RIGHT, 0 };
88 static const uint32_t pi_channels_in[] =
89 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
90 AOUT_CHAN_CENTER, AOUT_CHAN_LFE,
91 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
92 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
94 /*****************************************************************************
95 * Open: check file and initializes structures
96 *****************************************************************************/
97 static int Open( vlc_object_t * p_this )
99 demux_t *p_demux = (demux_t*)p_this;
102 const uint8_t *p_peek;
105 uint64_t i_data_size;
106 unsigned int i_extended;
107 const char *psz_name;
109 WAVEFORMATEXTENSIBLE *p_wf_ext = NULL;
110 WAVEFORMATEX *p_wf = NULL;
112 /* Is it a wav file ? */
113 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
116 b_is_rf64 = ( memcmp( p_peek, "RF64", 4 ) == 0 );
117 if( ( !b_is_rf64 && memcmp( p_peek, "RIFF", 4 ) ) ||
118 memcmp( &p_peek[8], "WAVE", 4 ) )
123 p_demux->pf_demux = Demux;
124 p_demux->pf_control = Control;
125 p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
126 if( unlikely(!p_sys) )
130 p_sys->i_data_size = 0;
131 p_sys->i_chans_to_reorder = 0;
132 p_sys->i_channel_mask = 0;
134 /* skip riff header */
135 if( stream_Read( p_demux->s, NULL, 12 ) != 12 )
140 /* search datasize64 chunk */
141 if( ChunkFind( p_demux, "ds64", &i_size ) )
143 msg_Err( p_demux, "cannot find 'ds64' chunk" );
148 msg_Err( p_demux, "invalid 'ds64' chunk" );
151 if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
153 if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 )
155 i_data_size = GetQWLE( &p_peek[8] );
156 if( i_data_size >> 62 )
157 p_sys->i_data_size = (int64_t)1 << 62;
159 p_sys->i_data_size = i_data_size;
160 if( stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
161 ( (i_size & 1) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
165 /* search fmt chunk */
166 if( ChunkFind( p_demux, "fmt ", &i_size ) )
168 msg_Err( p_demux, "cannot find 'fmt ' chunk" );
172 if( i_size < sizeof( WAVEFORMATEX ) )
174 msg_Err( p_demux, "invalid 'fmt ' chunk" );
177 if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
181 /* load waveformatex */
182 p_wf_ext = malloc( i_size );
183 if( unlikely( !p_wf_ext ) )
186 p_wf = &p_wf_ext->Format;
189 if( stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size ||
190 ( ( i_size & 1 ) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
192 msg_Err( p_demux, "cannot load 'fmt ' chunk" );
196 es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
197 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
199 p_sys->fmt.audio.i_channels = GetWLE ( &p_wf->nChannels );
200 p_sys->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
201 p_sys->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );
202 p_sys->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
203 p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
204 if( i_size >= sizeof(WAVEFORMATEX) )
205 p_sys->fmt.i_extra = __MIN( GetWLE( &p_wf->cbSize ), i_size - sizeof(WAVEFORMATEX) );
208 /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
209 /* see the following link for more information:
210 * http://www.microsoft.com/whdc/device/audio/multichaud.mspx#EFAA */
211 if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
212 i_size >= sizeof( WAVEFORMATEXTENSIBLE ) &&
213 ( p_sys->fmt.i_extra + sizeof( WAVEFORMATEX )
214 >= sizeof( WAVEFORMATEXTENSIBLE ) ) )
216 unsigned i_channel_mask;
219 guid_subformat = p_wf_ext->SubFormat;
220 guid_subformat.Data1 = GetDWLE( &p_wf_ext->SubFormat.Data1 );
221 guid_subformat.Data2 = GetWLE( &p_wf_ext->SubFormat.Data2 );
222 guid_subformat.Data3 = GetWLE( &p_wf_ext->SubFormat.Data3 );
224 sf_tag_to_fourcc( &guid_subformat, &p_sys->fmt.i_codec, &psz_name );
226 i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
227 p_sys->fmt.i_extra -= i_extended;
229 i_channel_mask = GetDWLE( &p_wf_ext->dwChannelMask );
233 for( unsigned i = 0; i < sizeof(pi_channels_src)/sizeof(*pi_channels_src); i++ )
235 if( i_channel_mask & pi_channels_src[i] )
237 if( !( p_sys->i_channel_mask & pi_channels_in[i] ) )
240 i_channel_mask &= ~pi_channels_src[i];
241 p_sys->i_channel_mask |= pi_channels_in[i];
243 if( i_match >= p_sys->fmt.audio.i_channels )
248 msg_Warn( p_demux, "Some channels are unrecognized or uselessly specified (0x%x)", i_channel_mask );
249 if( i_match < p_sys->fmt.audio.i_channels )
251 int i_missing = p_sys->fmt.audio.i_channels - i_match;
252 msg_Warn( p_demux, "Trying to fill up unspecified position for %d channels", p_sys->fmt.audio.i_channels - i_match );
254 static const uint32_t pi_pair[] = { AOUT_CHAN_REARLEFT|AOUT_CHAN_REARRIGHT,
255 AOUT_CHAN_MIDDLELEFT|AOUT_CHAN_MIDDLERIGHT,
256 AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT };
258 static const uint32_t pi_center[] = { AOUT_CHAN_REARCENTER,
260 AOUT_CHAN_CENTER }; */
262 /* Try to complete with pair */
263 for( unsigned i = 0; i < sizeof(pi_pair)/sizeof(*pi_pair); i++ )
265 if( i_missing >= 2 && !(p_sys->i_channel_mask & pi_pair[i] ) )
268 p_sys->i_channel_mask |= pi_pair[i];
271 /* Well fill up with what we can */
272 for( unsigned i = 0; i < sizeof(pi_channels_in)/sizeof(*pi_channels_in) && i_missing > 0; i++ )
274 if( !( p_sys->i_channel_mask & pi_channels_in[i] ) )
276 p_sys->i_channel_mask |= pi_channels_in[i];
284 i_match = p_sys->fmt.audio.i_channels - i_missing;
286 if( i_match < p_sys->fmt.audio.i_channels )
288 msg_Err( p_demux, "Invalid/unsupported channel mask" );
289 p_sys->i_channel_mask = 0;
293 else if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_PCM &&
294 p_sys->fmt.audio.i_channels > 2 && p_sys->fmt.audio.i_channels <= 9 )
296 for( int i = 0; i < p_sys->fmt.audio.i_channels; i++ )
297 p_sys->i_channel_mask |= pi_channels_in[i];
300 if( p_sys->i_channel_mask )
302 if( p_sys->fmt.i_codec == VLC_FOURCC('a','r','a','w') ||
303 p_sys->fmt.i_codec == VLC_FOURCC('a','f','l','t') )
304 p_sys->i_chans_to_reorder =
305 aout_CheckChannelReorder( pi_channels_in, NULL,
306 p_sys->i_channel_mask,
307 p_sys->pi_chan_table );
309 msg_Dbg( p_demux, "channel mask: %x, reordering: %u",
310 p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
313 p_sys->fmt.audio.i_physical_channels =
314 p_sys->fmt.audio.i_original_channels = p_sys->i_channel_mask;
316 if( p_sys->fmt.i_extra > 0 )
318 p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
319 if( unlikely(!p_sys->fmt.p_extra) )
321 p_sys->fmt.i_extra = 0;
324 memcpy( p_sys->fmt.p_extra, (uint8_t *)p_wf + sizeof( WAVEFORMATEX ) + i_extended,
325 p_sys->fmt.i_extra );
328 msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
329 "freq: %u Hz, bitrate: %uKo/s, blockalign: %d, bits/samples: %d, "
331 GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
332 p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
333 p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
334 p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
339 switch( p_sys->fmt.i_codec )
341 case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
342 case VLC_FOURCC( 'a', 'f', 'l', 't' ):
343 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
345 case VLC_CODEC_MULAW:
346 if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
350 vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec,
351 p_sys->fmt.audio.i_bitspersample );
353 case VLC_CODEC_ADPCM_MS:
354 /* FIXME not sure at all FIXME */
355 case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
356 case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
357 if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
361 case VLC_CODEC_ADPCM_IMA_WAV:
362 if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
368 /* FIXME set end of area FIXME */
370 case VLC_CODEC_GSM_MS:
371 case VLC_CODEC_ADPCM_G726:
372 case VLC_CODEC_TRUESPEECH:
373 case VLC_CODEC_ATRAC3:
374 case VLC_CODEC_G723_1:
376 if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
381 msg_Err( p_demux, "unsupported codec (%4.4s)",
382 (char*)&p_sys->fmt.i_codec );
386 if( p_sys->i_frame_size <= 0 || p_sys->i_frame_samples <= 0 )
388 msg_Dbg( p_demux, "invalid frame size: %i %i", p_sys->i_frame_size,
389 p_sys->i_frame_samples );
392 if( p_sys->fmt.audio.i_rate <= 0 )
394 msg_Dbg( p_demux, "invalid sample rate: %i", p_sys->fmt.audio.i_rate );
398 msg_Dbg( p_demux, "found %s audio format", psz_name );
400 if( ChunkFind( p_demux, "data", &i_size ) )
402 msg_Err( p_demux, "cannot find 'data' chunk" );
405 if( !b_is_rf64 || i_size < UINT32_MAX )
406 p_sys->i_data_size = i_size;
407 if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
409 p_sys->i_data_pos = stream_Tell( p_demux->s );
411 if( p_sys->fmt.i_bitrate <= 0 )
413 p_sys->fmt.i_bitrate = (int64_t)p_sys->i_frame_size *
414 p_sys->fmt.audio.i_rate * 8 / p_sys->i_frame_samples;
417 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
419 date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
420 date_Set( &p_sys->pts, 1 );
425 msg_Err( p_demux, "An error occurred during wav demuxing" );
431 /*****************************************************************************
432 * Demux: read packet and send them to decoders
433 *****************************************************************************
434 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
435 *****************************************************************************/
436 static int Demux( demux_t *p_demux )
438 demux_sys_t *p_sys = p_demux->p_sys;
440 const int64_t i_pos = stream_Tell( p_demux->s );
442 if( p_sys->i_data_size > 0 &&
443 i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
449 if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
451 msg_Warn( p_demux, "cannot read data" );
456 p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
459 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
461 /* Do the channel reordering */
462 if( p_sys->i_chans_to_reorder )
463 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
464 p_sys->fmt.audio.i_channels,
465 p_sys->pi_chan_table, p_sys->fmt.i_codec );
467 es_out_Send( p_demux->out, p_sys->p_es, p_block );
469 date_Increment( &p_sys->pts, p_sys->i_frame_samples );
474 /*****************************************************************************
475 * Close: frees unused data
476 *****************************************************************************/
477 static void Close ( vlc_object_t * p_this )
479 demux_t *p_demux = (demux_t*)p_this;
480 demux_sys_t *p_sys = p_demux->p_sys;
485 /*****************************************************************************
487 *****************************************************************************/
488 static int Control( demux_t *p_demux, int i_query, va_list args )
490 demux_sys_t *p_sys = p_demux->p_sys;
493 if( p_sys->i_data_size > 0 )
494 i_end = p_sys->i_data_pos + p_sys->i_data_size;
496 return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, i_end,
497 p_sys->fmt.i_bitrate,
498 p_sys->fmt.audio.i_blockalign,
502 /*****************************************************************************
504 *****************************************************************************/
505 static int ChunkFind( demux_t *p_demux, const char *fcc, unsigned int *pi_size )
507 const uint8_t *p_peek;
513 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
515 msg_Err( p_demux, "cannot peek" );
519 i_size = GetDWLE( p_peek + 4 );
521 msg_Dbg( p_demux, "chunk: fcc=`%4.4s` size=%"PRIu32, p_peek, i_size );
523 if( !memcmp( p_peek, fcc, 4 ) )
533 if( stream_Read( p_demux->s, NULL, 8 ) != 8 ||
534 stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
535 ( (i_size & 1) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
540 static int FrameInfo_PCM( unsigned int *pi_size, int *pi_samples,
541 const es_format_t *p_fmt )
545 /* read samples for 50ms of */
546 *pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
548 i_bytes = *pi_samples * p_fmt->audio.i_channels *
549 ( (p_fmt->audio.i_bitspersample + 7) / 8 );
551 if( p_fmt->audio.i_blockalign > 0 )
553 const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
555 i_bytes += p_fmt->audio.i_blockalign - i_modulo;
562 static int FrameInfo_MS_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 + 2 * ( p_fmt->audio.i_blockalign -
569 7 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
570 *pi_size = p_fmt->audio.i_blockalign;
575 static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, int *pi_samples,
576 const es_format_t *p_fmt )
578 if( p_fmt->audio.i_channels <= 0 )
581 *pi_samples = 2 * ( p_fmt->audio.i_blockalign -
582 4 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
583 *pi_size = p_fmt->audio.i_blockalign;
588 static int FrameInfo_MSGSM( unsigned int *pi_size, int *pi_samples,
589 const es_format_t *p_fmt )
591 if( p_fmt->i_bitrate <= 0 )
594 *pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
596 *pi_size = p_fmt->audio.i_blockalign;