]> git.sesse.net Git - vlc/blob - modules/demux/wav.c
macosx: Update progress dialog on the main thread, make check thread safe
[vlc] / modules / demux / wav.c
1 /*****************************************************************************
2  * wav.c : wav file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2008 VLC authors and 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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
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 #include <vlc_aout.h>
36 #include <vlc_codecs.h>
37
38 #include "windows_audio_commons.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 vlc_module_begin ()
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 )
52 vlc_module_end ()
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int Demux  ( demux_t * );
58 static int Control( demux_t *, int i_query, va_list args );
59
60 struct demux_sys_t
61 {
62     es_format_t     fmt;
63     es_out_id_t     *p_es;
64
65     int64_t         i_data_pos;
66     int64_t         i_data_size;
67
68     unsigned int    i_frame_size;
69     int             i_frame_samples;
70
71     date_t          pts;
72
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];
76 };
77
78 static int ChunkFind( demux_t *, const char *, unsigned int * );
79
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 * );
84
85 /*****************************************************************************
86  * Open: check file and initializes structures
87  *****************************************************************************/
88 static int Open( vlc_object_t * p_this )
89 {
90     demux_t     *p_demux = (demux_t*)p_this;
91     demux_sys_t *p_sys;
92
93     const uint8_t *p_peek;
94     bool           b_is_rf64;
95     unsigned int   i_size;
96     uint64_t       i_data_size;
97     unsigned int   i_extended;
98     const char    *psz_name;
99
100     WAVEFORMATEXTENSIBLE *p_wf_ext = NULL;
101     WAVEFORMATEX         *p_wf = NULL;
102
103     /* Is it a wav file ? */
104     if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
105         return VLC_EGENERIC;
106
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 ) )
110     {
111         return VLC_EGENERIC;
112     }
113
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) )
118         return VLC_ENOMEM;
119
120     p_sys->p_es           = NULL;
121     p_sys->i_data_size    = 0;
122     p_sys->i_chans_to_reorder = 0;
123     p_sys->i_channel_mask = 0;
124
125     /* skip riff header */
126     if( stream_Read( p_demux->s, NULL, 12 ) != 12 )
127         goto error;
128
129     if( b_is_rf64 )
130     {
131         /* search datasize64 chunk */
132         if( ChunkFind( p_demux, "ds64", &i_size ) )
133         {
134             msg_Err( p_demux, "cannot find 'ds64' chunk" );
135             goto error;
136         }
137         if( i_size < 24 )
138         {
139             msg_Err( p_demux, "invalid 'ds64' chunk" );
140             goto error;
141         }
142         if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
143             goto error;
144         if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 )
145             goto error;
146         i_data_size = GetQWLE( &p_peek[8] );
147         if( i_data_size >> 62 )
148             p_sys->i_data_size = (int64_t)1 << 62;
149         else
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 ) )
153             goto error;
154     }
155
156     /* search fmt chunk */
157     if( ChunkFind( p_demux, "fmt ", &i_size ) )
158     {
159         msg_Err( p_demux, "cannot find 'fmt ' chunk" );
160         goto error;
161     }
162     i_size += 2;
163     if( i_size < sizeof( WAVEFORMATEX ) )
164     {
165         msg_Err( p_demux, "invalid 'fmt ' chunk" );
166         goto error;
167     }
168     if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
169         goto error;
170
171
172     /* load waveformatex */
173     p_wf_ext = malloc( i_size );
174     if( unlikely( !p_wf_ext ) )
175          goto error;
176
177     p_wf         = &p_wf_ext->Format;
178     p_wf->cbSize = 0;
179     i_size      -= 2;
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 ) )
182     {
183         msg_Err( p_demux, "cannot load 'fmt ' chunk" );
184         goto error;
185     }
186
187     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
188     wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
189                       &psz_name );
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) );
197     i_extended = 0;
198
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 ) ) )
206     {
207         unsigned i_channel_mask;
208         GUID guid_subformat;
209
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 );
214
215         sf_tag_to_fourcc( &guid_subformat, &p_sys->fmt.i_codec, &psz_name );
216
217         i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
218         p_sys->fmt.i_extra -= i_extended;
219
220         i_channel_mask = GetDWLE( &p_wf_ext->dwChannelMask );
221         if( i_channel_mask )
222         {
223             int i_match = 0;
224             p_sys->i_channel_mask = getChannelMask( &i_channel_mask, p_sys->fmt.audio.i_channels, &i_match );
225             if( i_channel_mask )
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 )
228             {
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 );
231
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 };
235                 /* FIXME: Unused yet
236                 static const uint32_t pi_center[] = { AOUT_CHAN_REARCENTER,
237                                                       0,
238                                                       AOUT_CHAN_CENTER }; */
239
240                 /* Try to complete with pair */
241                 for( unsigned i = 0; i < sizeof(pi_pair)/sizeof(*pi_pair); i++ )
242                 {
243                     if( i_missing >= 2 && !(p_sys->i_channel_mask & pi_pair[i] ) )
244                     {
245                         i_missing -= 2;
246                         p_sys->i_channel_mask |= pi_pair[i];
247                     }
248                 }
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++ )
251                 {
252                     if( !( p_sys->i_channel_mask & pi_channels_aout[i] ) )
253                     {
254                         p_sys->i_channel_mask |= pi_channels_aout[i];
255                         i_missing--;
256
257                         if( i_missing <= 0 )
258                             break;
259                     }
260                 }
261
262                 i_match = p_sys->fmt.audio.i_channels - i_missing;
263             }
264             if( i_match < p_sys->fmt.audio.i_channels )
265             {
266                 msg_Err( p_demux, "Invalid/unsupported channel mask" );
267                 p_sys->i_channel_mask = 0;
268             }
269         }
270     }
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 )
273     {
274         for( int i = 0; i < p_sys->fmt.audio.i_channels; i++ )
275             p_sys->i_channel_mask |= pi_channels_aout[i];
276     }
277
278     if( p_sys->i_channel_mask )
279     {
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 );
286
287         msg_Dbg( p_demux, "channel mask: %x, reordering: %u",
288                  p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
289     }
290
291     p_sys->fmt.audio.i_physical_channels =
292     p_sys->fmt.audio.i_original_channels = p_sys->i_channel_mask;
293
294     if( p_sys->fmt.i_extra > 0 )
295     {
296         p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
297         if( unlikely(!p_sys->fmt.p_extra) )
298         {
299             p_sys->fmt.i_extra = 0;
300             goto error;
301         }
302         memcpy( p_sys->fmt.p_extra, (uint8_t *)p_wf + sizeof( WAVEFORMATEX ) + i_extended,
303                 p_sys->fmt.i_extra );
304     }
305
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, "
308              "extra size: %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 );
313
314     free( p_wf );
315     p_wf = NULL;
316
317     switch( p_sys->fmt.i_codec )
318     {
319     case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
320     case VLC_FOURCC( 'a', 'f', 'l', 't' ):
321     case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
322     case VLC_CODEC_ALAW:
323     case VLC_CODEC_MULAW:
324         if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
325                            &p_sys->fmt ) )
326             goto error;
327         p_sys->fmt.i_codec =
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" );
332             goto error;
333         }
334         break;
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,
340                                 &p_sys->fmt ) )
341             goto error;
342         break;
343     case VLC_CODEC_ADPCM_IMA_WAV:
344         if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
345                                  &p_sys->fmt ) )
346             goto error;
347         break;
348     case VLC_CODEC_MPGA:
349     case VLC_CODEC_A52:
350         /* FIXME set end of area FIXME */
351         goto error;
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:
357     case VLC_CODEC_WMA2:
358         if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
359                              &p_sys->fmt ) )
360             goto error;
361         break;
362     default:
363         msg_Err( p_demux, "unsupported codec (%4.4s)",
364                  (char*)&p_sys->fmt.i_codec );
365         goto error;
366     }
367
368     if( p_sys->i_frame_size <= 0 || p_sys->i_frame_samples <= 0 )
369     {
370         msg_Dbg( p_demux, "invalid frame size: %i %i", p_sys->i_frame_size,
371                                                        p_sys->i_frame_samples );
372         goto error;
373     }
374     if( p_sys->fmt.audio.i_rate <= 0 )
375     {
376         msg_Dbg( p_demux, "invalid sample rate: %i", p_sys->fmt.audio.i_rate );
377         goto error;
378     }
379
380     msg_Dbg( p_demux, "found %s audio format", psz_name );
381
382     if( ChunkFind( p_demux, "data", &i_size ) )
383     {
384         msg_Err( p_demux, "cannot find 'data' chunk" );
385         goto error;
386     }
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 )
390         goto error;
391     p_sys->i_data_pos = stream_Tell( p_demux->s );
392
393     if( p_sys->fmt.i_bitrate <= 0 )
394     {
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;
397     }
398
399     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
400
401     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
402     date_Set( &p_sys->pts, 1 );
403
404     return VLC_SUCCESS;
405
406 error:
407     msg_Err( p_demux, "An error occurred during wav demuxing" );
408     free( p_wf );
409     free( p_sys );
410     return VLC_EGENERIC;
411 }
412
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 )
419 {
420     demux_sys_t *p_sys = p_demux->p_sys;
421     block_t     *p_block;
422     const int64_t i_pos = stream_Tell( p_demux->s );
423     unsigned int i_read_size = p_sys->i_frame_size;
424
425     if( p_sys->i_data_size > 0 )
426     {
427         int64_t i_end = p_sys->i_data_pos + p_sys->i_data_size;
428         if ( i_pos >= i_end )
429             return 0;  /* EOF */
430
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;
434     }
435
436     if( ( p_block = stream_Block( p_demux->s, i_read_size ) ) == NULL )
437     {
438         msg_Warn( p_demux, "cannot read data" );
439         return 0;
440     }
441
442     p_block->i_dts =
443     p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
444
445     /* set PCR */
446     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
447
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 );
453
454     es_out_Send( p_demux->out, p_sys->p_es, p_block );
455
456     date_Increment( &p_sys->pts, p_sys->i_frame_samples );
457
458     return 1;
459 }
460
461 /*****************************************************************************
462  * Close: frees unused data
463  *****************************************************************************/
464 static void Close ( vlc_object_t * p_this )
465 {
466     demux_t     *p_demux = (demux_t*)p_this;
467     demux_sys_t *p_sys   = p_demux->p_sys;
468
469     free( p_sys );
470 }
471
472 /*****************************************************************************
473  * Control:
474  *****************************************************************************/
475 static int Control( demux_t *p_demux, int i_query, va_list args )
476 {
477     demux_sys_t *p_sys  = p_demux->p_sys;
478     int64_t i_end = -1;
479
480     if( p_sys->i_data_size > 0 )
481         i_end = p_sys->i_data_pos + p_sys->i_data_size;
482
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,
486                                    i_query, args );
487 }
488
489 /*****************************************************************************
490  * Local functions
491  *****************************************************************************/
492 static int ChunkFind( demux_t *p_demux, const char *fcc, unsigned int *pi_size )
493 {
494     const uint8_t *p_peek;
495
496     for( ;; )
497     {
498         uint32_t i_size;
499
500         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
501         {
502             msg_Err( p_demux, "cannot peek" );
503             return VLC_EGENERIC;
504         }
505
506         i_size = GetDWLE( p_peek + 4 );
507
508         msg_Dbg( p_demux, "chunk: fcc=`%4.4s` size=%"PRIu32, p_peek, i_size );
509
510         if( !memcmp( p_peek, fcc, 4 ) )
511         {
512             if( pi_size )
513             {
514                 *pi_size = i_size;
515             }
516             return VLC_SUCCESS;
517         }
518
519         /* Skip chunk */
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 ) )
523             return VLC_EGENERIC;
524     }
525 }
526
527 static int FrameInfo_PCM( unsigned int *pi_size, int *pi_samples,
528                           const es_format_t *p_fmt )
529 {
530     int i_bytes;
531
532     /* read samples for 50ms of */
533     *pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
534
535     i_bytes = *pi_samples * p_fmt->audio.i_channels *
536         ( (p_fmt->audio.i_bitspersample + 7) / 8 );
537
538     if( p_fmt->audio.i_blockalign > 0 )
539     {
540         const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
541         if( i_modulo > 0 )
542             i_bytes += p_fmt->audio.i_blockalign - i_modulo;
543     }
544
545     *pi_size = i_bytes;
546     return VLC_SUCCESS;
547 }
548
549 static int FrameInfo_MS_ADPCM( unsigned int *pi_size, int *pi_samples,
550                                const es_format_t *p_fmt )
551 {
552     if( p_fmt->audio.i_channels <= 0 )
553         return VLC_EGENERIC;
554
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;
558
559     return VLC_SUCCESS;
560 }
561
562 static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, int *pi_samples,
563                                 const es_format_t *p_fmt )
564 {
565     if( p_fmt->audio.i_channels <= 0 )
566         return VLC_EGENERIC;
567
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;
571
572     return VLC_SUCCESS;
573 }
574
575 static int FrameInfo_MSGSM( unsigned int *pi_size, int *pi_samples,
576                             const es_format_t *p_fmt )
577 {
578     if( p_fmt->i_bitrate <= 0 )
579         return VLC_EGENERIC;
580
581     *pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
582                     / p_fmt->i_bitrate;
583     *pi_size = p_fmt->audio.i_blockalign;
584
585     return VLC_SUCCESS;
586 }
587