1 /*****************************************************************************
2  * wav.c : wav file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2008 VLC authors and VideoLAN
5  * $Id: fd1a3b4325b884c4baecc46eb5b6866e702e77f7 $
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 <assert.h>
33 
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_aout.h>
38 #include <vlc_codecs.h>
39 
40 #include "windows_audio_commons.h"
41 
42 #define WAV_CHAN_MAX 32
43 static_assert( INPUT_CHAN_MAX >= WAV_CHAN_MAX, "channel count mismatch" );
44 
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
50 
51 vlc_module_begin ()
52     set_description( N_("WAV demuxer") )
53     set_category( CAT_INPUT )
54     set_subcategory( SUBCAT_INPUT_DEMUX )
55     set_capability( "demux", 142 )
56     set_callbacks( Open, Close )
57 vlc_module_end ()
58 
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int Demux  ( demux_t * );
63 static int Control( demux_t *, int i_query, va_list args );
64 
65 struct demux_sys_t
66 {
67     es_format_t     fmt;
68     es_out_id_t     *p_es;
69 
70     int64_t         i_data_pos;
71     int64_t         i_data_size;
72 
73     unsigned int    i_frame_size;
74     int             i_frame_samples;
75 
76     date_t          pts;
77 
78     uint32_t i_channel_mask;
79     uint8_t i_chans_to_reorder;            /* do we need channel reordering */
80     uint8_t pi_chan_table[AOUT_CHAN_MAX];
81 };
82 
83 static int ChunkFind( demux_t *, const char *, unsigned int * );
84 
85 static int FrameInfo_IMA_ADPCM( unsigned int *, int *, const es_format_t * );
86 static int FrameInfo_MS_ADPCM ( unsigned int *, int *, const es_format_t * );
87 static int FrameInfo_Creative_ADPCM( unsigned int *, int *, const es_format_t * );
88 static int FrameInfo_PCM      ( unsigned int *, int *, const es_format_t * );
89 static int FrameInfo_MSGSM    ( unsigned int *, int *, const es_format_t * );
90 
91 /*****************************************************************************
92  * Open: check file and initializes structures
93  *****************************************************************************/
Open(vlc_object_t * p_this)94 static int Open( vlc_object_t * p_this )
95 {
96     demux_t     *p_demux = (demux_t*)p_this;
97     demux_sys_t *p_sys;
98 
99     const uint8_t *p_peek;
100     bool           b_is_rf64;
101     unsigned int   i_size;
102     uint64_t       i_data_size;
103     unsigned int   i_extended;
104     const char    *psz_name;
105 
106     WAVEFORMATEXTENSIBLE *p_wf_ext = NULL;
107     WAVEFORMATEX         *p_wf = NULL;
108 
109     /* Is it a wav file ? */
110     if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
111         return VLC_EGENERIC;
112 
113     b_is_rf64 = ( memcmp( p_peek, "RF64", 4 ) == 0 );
114     if( ( !b_is_rf64 && memcmp( p_peek, "RIFF", 4 ) ) ||
115           memcmp( &p_peek[8], "WAVE", 4 ) )
116     {
117         return VLC_EGENERIC;
118     }
119 
120     p_demux->pf_demux     = Demux;
121     p_demux->pf_control   = Control;
122     p_demux->p_sys        = p_sys = malloc( sizeof( *p_sys ) );
123     if( unlikely(!p_sys) )
124         return VLC_ENOMEM;
125 
126     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
127     p_sys->p_es           = NULL;
128     p_sys->i_data_size    = 0;
129     p_sys->i_chans_to_reorder = 0;
130     p_sys->i_channel_mask = 0;
131 
132     /* skip riff header */
133     if( vlc_stream_Read( p_demux->s, NULL, 12 ) != 12 )
134         goto error;
135 
136     if( b_is_rf64 )
137     {
138         /* search datasize64 chunk */
139         if( ChunkFind( p_demux, "ds64", &i_size ) )
140         {
141             msg_Err( p_demux, "cannot find 'ds64' chunk" );
142             goto error;
143         }
144         if( i_size < 24 )
145         {
146             msg_Err( p_demux, "invalid 'ds64' chunk" );
147             goto error;
148         }
149         if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
150             goto error;
151         if( vlc_stream_Peek( p_demux->s, &p_peek, 24 ) < 24 )
152             goto error;
153         i_data_size = GetQWLE( &p_peek[8] );
154         if( i_data_size >> 62 )
155             p_sys->i_data_size = (int64_t)1 << 62;
156         else
157             p_sys->i_data_size = i_data_size;
158         if( vlc_stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
159             ( (i_size & 1) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
160             goto error;
161     }
162 
163     /* search fmt chunk */
164     if( ChunkFind( p_demux, "fmt ", &i_size ) )
165     {
166         msg_Err( p_demux, "cannot find 'fmt ' chunk" );
167         goto error;
168     }
169     i_size += 2;
170     if( i_size < sizeof( WAVEFORMATEX ) )
171     {
172         msg_Err( p_demux, "invalid 'fmt ' chunk" );
173         goto error;
174     }
175     if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
176         goto error;
177 
178 
179     /* load waveformatex */
180     p_wf_ext = malloc( i_size );
181     if( unlikely( !p_wf_ext ) )
182          goto error;
183 
184     p_wf         = &p_wf_ext->Format;
185     p_wf->cbSize = 0;
186     i_size      -= 2;
187     if( vlc_stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size ||
188         ( ( i_size & 1 ) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
189     {
190         msg_Err( p_demux, "cannot load 'fmt ' chunk" );
191         goto error;
192     }
193 
194     wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_sys->fmt.i_codec,
195                       &psz_name );
196     p_sys->fmt.audio.i_channels      = GetWLE ( &p_wf->nChannels );
197     p_sys->fmt.audio.i_rate          = GetDWLE( &p_wf->nSamplesPerSec );
198     p_sys->fmt.audio.i_blockalign    = GetWLE( &p_wf->nBlockAlign );
199     p_sys->fmt.i_bitrate             = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
200     p_sys->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
201     if( i_size >= sizeof(WAVEFORMATEX) )
202         p_sys->fmt.i_extra = __MIN( GetWLE( &p_wf->cbSize ), i_size - sizeof(WAVEFORMATEX) );
203     i_extended = 0;
204 
205     /* Handle new WAVE_FORMAT_EXTENSIBLE wav files */
206     /* see the following link for more information:
207      * http://www.microsoft.com/whdc/device/audio/multichaud.mspx#EFAA */
208     if( GetWLE( &p_wf->wFormatTag ) == WAVE_FORMAT_EXTENSIBLE &&
209         i_size >= sizeof( WAVEFORMATEXTENSIBLE ) &&
210         ( p_sys->fmt.i_extra + sizeof( WAVEFORMATEX )
211             >= sizeof( WAVEFORMATEXTENSIBLE ) ) )
212     {
213         unsigned i_channel_mask;
214         GUID guid_subformat;
215 
216         guid_subformat = p_wf_ext->SubFormat;
217         guid_subformat.Data1 = GetDWLE( &p_wf_ext->SubFormat.Data1 );
218         guid_subformat.Data2 = GetWLE( &p_wf_ext->SubFormat.Data2 );
219         guid_subformat.Data3 = GetWLE( &p_wf_ext->SubFormat.Data3 );
220 
221         sf_tag_to_fourcc( &guid_subformat, &p_sys->fmt.i_codec, &psz_name );
222 
223         msg_Dbg( p_demux, "extensible format guid " GUID_FMT, GUID_PRINT(guid_subformat) );
224 
225         i_extended = sizeof( WAVEFORMATEXTENSIBLE ) - sizeof( WAVEFORMATEX );
226         p_sys->fmt.i_extra -= i_extended;
227 
228         i_channel_mask = GetDWLE( &p_wf_ext->dwChannelMask );
229         if( i_channel_mask )
230         {
231             int i_match = 0;
232             p_sys->i_channel_mask = getChannelMask( &i_channel_mask, p_sys->fmt.audio.i_channels, &i_match );
233             if( i_channel_mask )
234                 msg_Warn( p_demux, "Some channels are unrecognized or uselessly specified (0x%x)", i_channel_mask );
235             if( i_match < p_sys->fmt.audio.i_channels )
236             {
237                 int i_missing = p_sys->fmt.audio.i_channels - i_match;
238                 msg_Warn( p_demux, "Trying to fill up unspecified position for %d channels", p_sys->fmt.audio.i_channels - i_match );
239 
240                 static const uint32_t pi_pair[] = { AOUT_CHAN_REARLEFT|AOUT_CHAN_REARRIGHT,
241                                                     AOUT_CHAN_MIDDLELEFT|AOUT_CHAN_MIDDLERIGHT,
242                                                     AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT };
243                 /* FIXME: Unused yet
244                 static const uint32_t pi_center[] = { AOUT_CHAN_REARCENTER,
245                                                       0,
246                                                       AOUT_CHAN_CENTER }; */
247 
248                 /* Try to complete with pair */
249                 for( unsigned i = 0; i < sizeof(pi_pair)/sizeof(*pi_pair); i++ )
250                 {
251                     if( i_missing >= 2 && !(p_sys->i_channel_mask & pi_pair[i] ) )
252                     {
253                         i_missing -= 2;
254                         p_sys->i_channel_mask |= pi_pair[i];
255                     }
256                 }
257                 /* Well fill up with what we can */
258                 for( unsigned i = 0; i < sizeof(pi_channels_aout)/sizeof(*pi_channels_aout) && i_missing > 0; i++ )
259                 {
260                     if( !( p_sys->i_channel_mask & pi_channels_aout[i] ) )
261                     {
262                         p_sys->i_channel_mask |= pi_channels_aout[i];
263                         i_missing--;
264 
265                         if( i_missing <= 0 )
266                             break;
267                     }
268                 }
269 
270                 i_match = p_sys->fmt.audio.i_channels - i_missing;
271             }
272             if( i_match < p_sys->fmt.audio.i_channels )
273             {
274                 msg_Err( p_demux, "Invalid/unsupported channel mask" );
275                 p_sys->i_channel_mask = 0;
276             }
277         }
278     }
279     if( p_sys->i_channel_mask == 0 && p_sys->fmt.audio.i_channels > 2
280      && p_sys->fmt.audio.i_channels <= AOUT_CHAN_MAX )
281     {
282         /* A dwChannelMask of 0 tells the audio device to render the first
283          * channel to the first port on the device, the second channel to the
284          * second port on the device, and so on. pi_default_channels is
285          * different than pi_channels_aout. Indeed FLC/FRC must be treated a
286          * SL/SR in that case. See "Default Channel Ordering" and "Details
287          * about dwChannelMask" from msdn */
288 
289         static const uint32_t pi_default_channels[] = {
290             AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_CENTER,
291             AOUT_CHAN_LFE, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
292             AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, AOUT_CHAN_REARCENTER };
293 
294         for( unsigned i = 0; i < p_sys->fmt.audio.i_channels &&
295              i < (sizeof(pi_default_channels) / sizeof(*pi_default_channels));
296              i++ )
297             p_sys->i_channel_mask |= pi_default_channels[i];
298     }
299 
300     if( p_sys->i_channel_mask )
301     {
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_aout, NULL,
306                                           p_sys->i_channel_mask,
307                                           p_sys->pi_chan_table );
308 
309         msg_Dbg( p_demux, "channel mask: %x, reordering: %u",
310                  p_sys->i_channel_mask, p_sys->i_chans_to_reorder );
311     }
312 
313     p_sys->fmt.audio.i_physical_channels = p_sys->i_channel_mask;
314 
315     if( p_sys->fmt.i_extra > 0 )
316     {
317         p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
318         if( unlikely(!p_sys->fmt.p_extra) )
319         {
320             p_sys->fmt.i_extra = 0;
321             goto error;
322         }
323         memcpy( p_sys->fmt.p_extra, (uint8_t *)p_wf + sizeof( WAVEFORMATEX ) + i_extended,
324                 p_sys->fmt.i_extra );
325     }
326 
327     msg_Dbg( p_demux, "format: 0x%4.4x, fourcc: %4.4s, channels: %d, "
328              "freq: %u Hz, bitrate: %uKo/s, blockalign: %d, bits/samples: %d, "
329              "extra size: %d",
330              GetWLE( &p_wf->wFormatTag ), (char *)&p_sys->fmt.i_codec,
331              p_sys->fmt.audio.i_channels, p_sys->fmt.audio.i_rate,
332              p_sys->fmt.i_bitrate / 8 / 1024, p_sys->fmt.audio.i_blockalign,
333              p_sys->fmt.audio.i_bitspersample, p_sys->fmt.i_extra );
334 
335     free( p_wf );
336     p_wf = NULL;
337 
338     switch( p_sys->fmt.i_codec )
339     {
340     case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
341     case VLC_FOURCC( 'a', 'f', 'l', 't' ):
342     case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
343     case VLC_CODEC_ALAW:
344     case VLC_CODEC_MULAW:
345         if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
346                            &p_sys->fmt ) )
347             goto error;
348         p_sys->fmt.i_codec =
349             vlc_fourcc_GetCodecAudio( p_sys->fmt.i_codec,
350                                       p_sys->fmt.audio.i_bitspersample );
351         if( p_sys->fmt.i_codec == 0 ) {
352             msg_Err( p_demux, "Unrecognized codec" );
353             goto error;
354         }
355         break;
356     case VLC_CODEC_ADPCM_MS:
357     /* FIXME not sure at all FIXME */
358     case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
359     case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
360         if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
361                                 &p_sys->fmt ) )
362             goto error;
363         break;
364     case VLC_CODEC_ADPCM_IMA_WAV:
365         if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
366                                  &p_sys->fmt ) )
367             goto error;
368         break;
369     case VLC_CODEC_ADPCM_CREATIVE:
370         if( FrameInfo_Creative_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
371                                       &p_sys->fmt ) )
372             goto error;
373         break;
374     case VLC_CODEC_MPGA:
375     case VLC_CODEC_A52:
376         /* FIXME set end of area FIXME */
377         goto error;
378     case VLC_CODEC_GSM_MS:
379     case VLC_CODEC_ADPCM_G726:
380     case VLC_CODEC_TRUESPEECH:
381     case VLC_CODEC_ATRAC3P:
382     case VLC_CODEC_ATRAC3:
383     case VLC_CODEC_G723_1:
384     case VLC_CODEC_WMA2:
385         if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
386                              &p_sys->fmt ) )
387             goto error;
388         break;
389     default:
390         msg_Err( p_demux, "unsupported codec (%4.4s)",
391                  (char*)&p_sys->fmt.i_codec );
392         goto error;
393     }
394 
395     if( p_sys->i_frame_size <= 0 || p_sys->i_frame_samples <= 0 )
396     {
397         msg_Dbg( p_demux, "invalid frame size: %i %i", p_sys->i_frame_size,
398                                                        p_sys->i_frame_samples );
399         goto error;
400     }
401     if( p_sys->fmt.audio.i_rate == 0 )
402     {
403         msg_Dbg( p_demux, "invalid sample rate: %i", p_sys->fmt.audio.i_rate );
404         goto error;
405     }
406 
407     msg_Dbg( p_demux, "found %s audio format", psz_name );
408 
409     if( ChunkFind( p_demux, "data", &i_size ) )
410     {
411         msg_Err( p_demux, "cannot find 'data' chunk" );
412         goto error;
413     }
414 
415     if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 )
416         goto error;
417     p_sys->i_data_pos = vlc_stream_Tell( p_demux->s );
418 
419     if( !b_is_rf64 || i_size < UINT32_MAX )
420     {
421         int64_t i_stream_size = stream_Size( p_demux->s );
422         if( i_stream_size > 0 && i_stream_size >= i_size + p_sys->i_data_pos )
423             p_sys->i_data_size = i_size;
424     }
425 
426     if( p_sys->fmt.i_bitrate <= 0 )
427     {
428         p_sys->fmt.i_bitrate = (int64_t)p_sys->i_frame_size *
429             p_sys->fmt.audio.i_rate * 8 / p_sys->i_frame_samples;
430     }
431 
432     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
433 
434     date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
435     date_Set( &p_sys->pts, 1 );
436 
437     return VLC_SUCCESS;
438 
439 error:
440     msg_Err( p_demux, "An error occurred during wav demuxing" );
441     free( p_wf );
442     es_format_Clean( &p_sys->fmt );
443     free( p_sys );
444     return VLC_EGENERIC;
445 }
446 
447 /*****************************************************************************
448  * Demux: read packet and send them to decoders
449  *****************************************************************************
450  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
451  *****************************************************************************/
Demux(demux_t * p_demux)452 static int Demux( demux_t *p_demux )
453 {
454     demux_sys_t *p_sys = p_demux->p_sys;
455     block_t     *p_block;
456     const int64_t i_pos = vlc_stream_Tell( p_demux->s );
457     unsigned int i_read_size = p_sys->i_frame_size;
458 
459     if( p_sys->i_data_size > 0 )
460     {
461         int64_t i_end = p_sys->i_data_pos + p_sys->i_data_size;
462         if ( i_pos >= i_end )
463             return 0;  /* EOF */
464 
465         /* Don't read past data chunk boundary */
466         if ( i_end < i_pos + i_read_size )
467             i_read_size = i_end - i_pos;
468     }
469 
470     if( ( p_block = vlc_stream_Block( p_demux->s, i_read_size ) ) == NULL )
471     {
472         msg_Warn( p_demux, "cannot read data" );
473         return 0;
474     }
475 
476     p_block->i_dts =
477     p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
478 
479     /* set PCR */
480     es_out_SetPCR( p_demux->out, p_block->i_pts );
481 
482     /* Do the channel reordering */
483     if( p_sys->i_chans_to_reorder )
484         aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
485                              p_sys->fmt.audio.i_channels,
486                              p_sys->pi_chan_table, p_sys->fmt.i_codec );
487 
488     es_out_Send( p_demux->out, p_sys->p_es, p_block );
489 
490     date_Increment( &p_sys->pts, p_sys->i_frame_samples );
491 
492     return 1;
493 }
494 
495 /*****************************************************************************
496  * Close: frees unused data
497  *****************************************************************************/
Close(vlc_object_t * p_this)498 static void Close ( vlc_object_t * p_this )
499 {
500     demux_t     *p_demux = (demux_t*)p_this;
501     demux_sys_t *p_sys   = p_demux->p_sys;
502 
503     es_format_Clean( &p_sys->fmt );
504     free( p_sys );
505 }
506 
507 /*****************************************************************************
508  * Control:
509  *****************************************************************************/
Control(demux_t * p_demux,int i_query,va_list args)510 static int Control( demux_t *p_demux, int i_query, va_list args )
511 {
512     demux_sys_t *p_sys  = p_demux->p_sys;
513     int64_t i_end = -1;
514 
515     if( p_sys->i_data_size > 0 )
516         i_end = p_sys->i_data_pos + p_sys->i_data_size;
517 
518     return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, i_end,
519                                    p_sys->fmt.i_bitrate,
520                                    p_sys->fmt.audio.i_blockalign,
521                                    i_query, args );
522 }
523 
524 /*****************************************************************************
525  * Local functions
526  *****************************************************************************/
ChunkFind(demux_t * p_demux,const char * fcc,unsigned int * pi_size)527 static int ChunkFind( demux_t *p_demux, const char *fcc, unsigned int *pi_size )
528 {
529     const uint8_t *p_peek;
530 
531     for( ;; )
532     {
533         uint32_t i_size;
534 
535         if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
536         {
537             msg_Err( p_demux, "cannot peek" );
538             return VLC_EGENERIC;
539         }
540 
541         i_size = GetDWLE( p_peek + 4 );
542 
543         msg_Dbg( p_demux, "chunk: fcc=`%4.4s` size=%"PRIu32, p_peek, i_size );
544 
545         if( !memcmp( p_peek, fcc, 4 ) )
546         {
547             if( pi_size )
548             {
549                 *pi_size = i_size;
550             }
551             return VLC_SUCCESS;
552         }
553 
554         /* Skip chunk */
555         if( vlc_stream_Read( p_demux->s, NULL, 8 ) != 8 ||
556             vlc_stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
557             ( (i_size & 1) && vlc_stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
558             return VLC_EGENERIC;
559     }
560 }
561 
FrameInfo_PCM(unsigned int * pi_size,int * pi_samples,const es_format_t * p_fmt)562 static int FrameInfo_PCM( unsigned int *pi_size, int *pi_samples,
563                           const es_format_t *p_fmt )
564 {
565     int i_bytes;
566 
567     if( p_fmt->audio.i_rate > 352800
568      || p_fmt->audio.i_bitspersample > 64
569      || p_fmt->audio.i_channels > WAV_CHAN_MAX )
570         return VLC_EGENERIC;
571 
572     /* read samples for 50ms of */
573     *pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
574 
575     i_bytes = *pi_samples * p_fmt->audio.i_channels *
576         ( (p_fmt->audio.i_bitspersample + 7) / 8 );
577 
578     if( p_fmt->audio.i_blockalign > 0 )
579     {
580         const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
581         if( i_modulo > 0 )
582             i_bytes += p_fmt->audio.i_blockalign - i_modulo;
583     }
584 
585     *pi_size = i_bytes;
586     return VLC_SUCCESS;
587 }
588 
FrameInfo_MS_ADPCM(unsigned int * pi_size,int * pi_samples,const es_format_t * p_fmt)589 static int FrameInfo_MS_ADPCM( unsigned int *pi_size, int *pi_samples,
590                                const es_format_t *p_fmt )
591 {
592     if( p_fmt->audio.i_channels == 0 )
593         return VLC_EGENERIC;
594 
595     *pi_samples = 2 + 2 * ( p_fmt->audio.i_blockalign -
596         7 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
597     *pi_size = p_fmt->audio.i_blockalign;
598 
599     return VLC_SUCCESS;
600 }
601 
FrameInfo_IMA_ADPCM(unsigned int * pi_size,int * pi_samples,const es_format_t * p_fmt)602 static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, int *pi_samples,
603                                 const es_format_t *p_fmt )
604 {
605     if( p_fmt->audio.i_channels == 0 )
606         return VLC_EGENERIC;
607 
608     *pi_samples = 2 * ( p_fmt->audio.i_blockalign -
609         4 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
610     *pi_size = p_fmt->audio.i_blockalign;
611 
612     return VLC_SUCCESS;
613 }
614 
FrameInfo_Creative_ADPCM(unsigned int * pi_size,int * pi_samples,const es_format_t * p_fmt)615 static int FrameInfo_Creative_ADPCM( unsigned int *pi_size, int *pi_samples,
616                                      const es_format_t *p_fmt )
617 {
618     if( p_fmt->audio.i_channels == 0 )
619         return VLC_EGENERIC;
620 
621     /* 4 bits / sample */
622     *pi_samples = p_fmt->audio.i_blockalign * 2 / p_fmt->audio.i_channels;
623     *pi_size = p_fmt->audio.i_blockalign;
624 
625     return VLC_SUCCESS;
626 }
627 
FrameInfo_MSGSM(unsigned int * pi_size,int * pi_samples,const es_format_t * p_fmt)628 static int FrameInfo_MSGSM( unsigned int *pi_size, int *pi_samples,
629                             const es_format_t *p_fmt )
630 {
631     if( p_fmt->i_bitrate <= 0 )
632         return VLC_EGENERIC;
633 
634     *pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
635                     / p_fmt->i_bitrate;
636     *pi_size = p_fmt->audio.i_blockalign;
637 
638     return VLC_SUCCESS;
639 }
640 
641