Commit b6352fa8 authored by Laurent Aimar's avatar Laurent Aimar

Review a bit wav demuxer against broken files.

parent aaddb130
......@@ -75,10 +75,10 @@ struct demux_sys_t
static int ChunkFind( demux_t *, const char *, unsigned int * );
static void FrameInfo_IMA_ADPCM( demux_t *, unsigned int *, int * );
static void FrameInfo_MS_ADPCM ( demux_t *, unsigned int *, int * );
static void FrameInfo_PCM ( demux_t *, unsigned int *, int * );
static void FrameInfo_MSGSM ( demux_t *, unsigned int *, int * );
static int FrameInfo_IMA_ADPCM( unsigned int *, int *, const es_format_t * );
static int FrameInfo_MS_ADPCM ( unsigned int *, int *, const es_format_t * );
static int FrameInfo_PCM ( unsigned int *, int *, const es_format_t * );
static int FrameInfo_MSGSM ( unsigned int *, int *, const es_format_t * );
static const uint32_t pi_channels_src[] =
{ WAVE_SPEAKER_FRONT_LEFT, WAVE_SPEAKER_FRONT_RIGHT,
......@@ -100,7 +100,7 @@ static int Open( vlc_object_t * p_this )
demux_sys_t *p_sys;
const uint8_t *p_peek;
uint32_t i_size;
unsigned int i_size;
unsigned int i_extended;
const char *psz_name;
......@@ -108,7 +108,8 @@ static int Open( vlc_object_t * p_this )
WAVEFORMATEX *p_wf = NULL;
/* Is it a wav file ? */
if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
return VLC_EGENERIC;
if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
{
......@@ -117,7 +118,7 @@ static int Open( vlc_object_t * p_this )
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
p_demux->p_sys = p_sys = malloc( sizeof( *p_sys ) );
if( p_sys == NULL )
return VLC_ENOMEM;
......@@ -126,7 +127,8 @@ static int Open( vlc_object_t * p_this )
p_sys->i_channel_mask = 0;
/* skip riff header */
stream_Read( p_demux->s, NULL, 12 ); /* cannot fail as peek succeed */
if( stream_Read( p_demux->s, NULL, 12 ) != 12 )
goto error;
/* search fmt chunk */
if( ChunkFind( p_demux, "fmt ", &i_size ) )
......@@ -140,7 +142,9 @@ static int Open( vlc_object_t * p_this )
msg_Err( p_demux, "invalid 'fmt ' chunk" );
goto error;
}
stream_Read( p_demux->s, NULL, 8 ); /* Cannot fail */
if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
goto error;
/* load waveformatex */
p_wf_ext = malloc( i_size );
......@@ -150,8 +154,8 @@ static int Open( vlc_object_t * p_this )
p_wf = (WAVEFORMATEX *)p_wf_ext;
p_wf->cbSize = 0;
i_size -= 2;
if( stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size
|| ( ( i_size & 1 ) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
if( stream_Read( p_demux->s, p_wf, i_size ) != (int)i_size ||
( ( i_size & 1 ) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
{
msg_Err( p_demux, "cannot load 'fmt ' chunk" );
goto error;
......@@ -220,6 +224,11 @@ static int Open( vlc_object_t * p_this )
if( p_sys->fmt.i_extra > 0 )
{
p_sys->fmt.p_extra = malloc( p_sys->fmt.i_extra );
if( !p_sys->fmt.p_extra )
{
p_sys->fmt.i_extra = 0;
goto error;
}
memcpy( p_sys->fmt.p_extra, ((uint8_t *)p_wf) + i_extended,
p_sys->fmt.i_extra );
}
......@@ -243,30 +252,35 @@ static int Open( vlc_object_t * p_this )
case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
case VLC_FOURCC( 'm', 'l', 'a', 'w' ):
case VLC_FOURCC( 'p', 'c', 'm', ' ' ):
FrameInfo_PCM( p_demux, &p_sys->i_frame_size,
&p_sys->i_frame_samples );
if( FrameInfo_PCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
&p_sys->fmt ) )
goto error;
break;
case VLC_FOURCC( 'm', 's', 0x00, 0x02 ):
FrameInfo_MS_ADPCM( p_demux, &p_sys->i_frame_size,
&p_sys->i_frame_samples );
if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
&p_sys->fmt ) )
goto error;
break;
case VLC_FOURCC( 'm', 's', 0x00, 0x11 ):
FrameInfo_IMA_ADPCM( p_demux, &p_sys->i_frame_size,
&p_sys->i_frame_samples );
if( FrameInfo_IMA_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
&p_sys->fmt ) )
goto error;
break;
case VLC_FOURCC( 'm', 's', 0x00, 0x61 ):
case VLC_FOURCC( 'm', 's', 0x00, 0x62 ):
/* FIXME not sure at all FIXME */
FrameInfo_MS_ADPCM( p_demux, &p_sys->i_frame_size,
&p_sys->i_frame_samples );
if( FrameInfo_MS_ADPCM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
&p_sys->fmt ) )
goto error;
break;
case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
case VLC_FOURCC( 'a', '5', '2', ' ' ):
/* FIXME set end of area FIXME */
goto error;
case VLC_FOURCC( 'a', 'g', 's', 'm' ):
FrameInfo_MSGSM( p_demux, &p_sys->i_frame_size,
&p_sys->i_frame_samples );
if( FrameInfo_MSGSM( &p_sys->i_frame_size, &p_sys->i_frame_samples,
&p_sys->fmt ) )
goto error;
break;
default:
msg_Err( p_demux, "unsupported codec (%4.4s)",
......@@ -274,6 +288,17 @@ static int Open( vlc_object_t * p_this )
goto error;
}
if( p_sys->i_frame_size <= 0 || p_sys->i_frame_samples <= 0 )
{
msg_Dbg( p_demux, "invalid frame size" );
goto error;
}
if( p_sys->fmt.audio.i_rate <= 0 )
{
msg_Dbg( p_demux, "invalid sample rate" );
goto error;
}
msg_Dbg( p_demux, "found %s audio format", psz_name );
if( ChunkFind( p_demux, "data", &p_sys->i_data_size ) )
......@@ -281,12 +306,13 @@ static int Open( vlc_object_t * p_this )
msg_Err( p_demux, "cannot find 'data' chunk" );
goto error;
}
stream_Read( p_demux->s, NULL, 8 ); /* Cannot fail */
if( stream_Read( p_demux->s, NULL, 8 ) != 8 )
goto error;
p_sys->i_data_pos = stream_Tell( p_demux->s );
if( p_sys->fmt.i_bitrate <= 0 )
{
p_sys->fmt.i_bitrate = (mtime_t)p_sys->i_frame_size *
p_sys->fmt.i_bitrate = (int64_t)p_sys->i_frame_size *
p_sys->fmt.audio.i_rate * 8 / p_sys->i_frame_samples;
}
......@@ -311,10 +337,8 @@ error:
static int Demux( demux_t *p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
int64_t i_pos;
block_t *p_block;
i_pos = stream_Tell( p_demux->s );
const int64_t i_pos = stream_Tell( p_demux->s );
if( p_sys->i_data_size > 0 &&
i_pos >= p_sys->i_data_pos + p_sys->i_data_size )
......@@ -329,8 +353,8 @@ static int Demux( demux_t *p_demux )
return 0;
}
p_block->i_dts = p_block->i_pts =
date_Increment( &p_sys->pts, p_sys->i_frame_samples );
p_block->i_dts =
p_block->i_pts = date_Increment( &p_sys->pts, p_sys->i_frame_samples );
/* set PCR */
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
......@@ -367,9 +391,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
int64_t i_end = -1;
if( p_sys->i_data_size > 0 )
{
i_end = p_sys->i_data_pos + p_sys->i_data_size;
}
return demux_vaControlHelper( p_demux->s, p_sys->i_data_pos, i_end,
p_sys->fmt.i_bitrate,
......@@ -408,64 +430,71 @@ static int ChunkFind( demux_t *p_demux, const char *fcc, unsigned int *pi_size )
}
/* Skip chunk */
if( stream_Read( p_demux->s, NULL, 8 ) != 8
|| stream_Read( p_demux->s, NULL, i_size ) != i_size
|| ((i_size & 1) && stream_Read( p_demux->s, NULL, 1 ) != 1 ))
if( stream_Read( p_demux->s, NULL, 8 ) != 8 ||
stream_Read( p_demux->s, NULL, i_size ) != (int)i_size ||
( (i_size & 1) && stream_Read( p_demux->s, NULL, 1 ) != 1 ) )
return VLC_EGENERIC;
}
}
static void FrameInfo_PCM( demux_t *p_demux, unsigned int *pi_size,
int *pi_samples )
static int FrameInfo_PCM( unsigned int *pi_size, int *pi_samples,
const es_format_t *p_fmt )
{
demux_sys_t *p_sys = p_demux->p_sys;
int i_bytes, i_modulo;
int i_bytes;
/* read samples for 50ms of */
*pi_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
*pi_samples = __MAX( p_fmt->audio.i_rate / 20, 1 );
i_bytes = *pi_samples * p_sys->fmt.audio.i_channels *
( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
i_bytes = *pi_samples * p_fmt->audio.i_channels *
( (p_fmt->audio.i_bitspersample + 7) / 8 );
if( p_sys->fmt.audio.i_blockalign > 0 )
if( p_fmt->audio.i_blockalign > 0 )
{
if( ( i_modulo = i_bytes % p_sys->fmt.audio.i_blockalign ) != 0 )
{
i_bytes += p_sys->fmt.audio.i_blockalign - i_modulo;
}
const int i_modulo = i_bytes % p_fmt->audio.i_blockalign;
if( i_modulo > 0 )
i_bytes += p_fmt->audio.i_blockalign - i_modulo;
}
*pi_size = i_bytes;
return VLC_EGENERIC;
}
static void FrameInfo_MS_ADPCM( demux_t *p_demux, unsigned int *pi_size,
int *pi_samples )
static int FrameInfo_MS_ADPCM( unsigned int *pi_size, int *pi_samples,
const es_format_t *p_fmt )
{
demux_sys_t *p_sys = p_demux->p_sys;
if( p_fmt->audio.i_channels <= 0 )
return VLC_EGENERIC;
*pi_samples = 2 + 2 * ( p_sys->fmt.audio.i_blockalign -
7 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
*pi_samples = 2 + 2 * ( p_fmt->audio.i_blockalign -
7 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
*pi_size = p_fmt->audio.i_blockalign;
*pi_size = p_sys->fmt.audio.i_blockalign;
return VLC_SUCCESS;
}
static void FrameInfo_IMA_ADPCM( demux_t *p_demux, unsigned int *pi_size,
int *pi_samples )
static int FrameInfo_IMA_ADPCM( unsigned int *pi_size, int *pi_samples,
const es_format_t *p_fmt )
{
demux_sys_t *p_sys = p_demux->p_sys;
if( p_fmt->audio.i_channels <= 0 )
return VLC_EGENERIC;
*pi_samples = 2 * ( p_sys->fmt.audio.i_blockalign -
4 * p_sys->fmt.audio.i_channels ) / p_sys->fmt.audio.i_channels;
*pi_samples = 2 * ( p_fmt->audio.i_blockalign -
4 * p_fmt->audio.i_channels ) / p_fmt->audio.i_channels;
*pi_size = p_fmt->audio.i_blockalign;
*pi_size = p_sys->fmt.audio.i_blockalign;
return VLC_SUCCESS;
}
static void FrameInfo_MSGSM( demux_t *p_demux, unsigned int *pi_size,
int *pi_samples )
static int FrameInfo_MSGSM( unsigned int *pi_size, int *pi_samples,
const es_format_t *p_fmt )
{
demux_sys_t *p_sys = p_demux->p_sys;
if( p_fmt->i_bitrate <= 0 )
return VLC_EGENERIC;
*pi_samples = ( p_fmt->audio.i_blockalign * p_fmt->audio.i_rate * 8)
/ p_fmt->i_bitrate;
*pi_size = p_fmt->audio.i_blockalign;
*pi_samples = ( p_sys->fmt.audio.i_blockalign * p_sys->fmt.audio.i_rate * 8)
/ p_sys->fmt.i_bitrate;
*pi_size = p_sys->fmt.audio.i_blockalign;
return VLC_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment