Commit 0b489f66 authored by Thomas Guillem's avatar Thomas Guillem

packetizer/mlp: reorder code

parent 3ea47109
......@@ -96,52 +96,215 @@ struct decoder_sys_t
static const uint8_t pu_start_code[3] = { 0xf8, 0x72, 0x6f };
/****************************************************************************
* Local prototypes
****************************************************************************/
static block_t *Packetize( decoder_t *, block_t **pp_block );
static void Flush( decoder_t * );
static int SyncInfo( const uint8_t *p_hdr, bool *pb_mlp, mlp_header_t *p_mlp );
static int SyncInfoDolby( const uint8_t *p_buf );
/**
* It parse MLP sync info.
*
* TODO handle CRC (at offset 26)
*/
static int TrueHdChannels( int i_map )
{
static const uint8_t pu_thd[13] =
{
2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
};
int i_count = 0;
/*****************************************************************************
* Open: probe the decoder/packetizer and return score
*****************************************************************************/
static int Open( vlc_object_t *p_this )
for( int i = 0; i < 13; i++ )
{
if( i_map & (1<<i) )
i_count += pu_thd[i];
}
return i_count;
}
static int MlpParse( mlp_header_t *p_mlp, const uint8_t p_hdr[MLP_HEADER_SYNC] )
{
decoder_t *p_dec = (decoder_t*)p_this;
decoder_sys_t *p_sys;
bs_t s;
if( p_dec->fmt_in.i_codec != VLC_CODEC_MLP &&
p_dec->fmt_in.i_codec != VLC_CODEC_TRUEHD )
assert( !memcmp( p_hdr, pu_start_code, 3 ) );
/* TODO Checksum ? */
/* */
bs_init( &s, &p_hdr[3], MLP_HEADER_SYNC - 3 );
/* Stream type */
p_mlp->i_type = bs_read( &s, 8 );
int i_rate_idx1;
if( p_mlp->i_type == 0xbb ) /* MLP */
{
static const unsigned pu_channels[32] = {
1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
bs_skip( &s, 4 + 4 );
i_rate_idx1 = bs_read( &s, 4 );
// Just skip the 4 following, since we don't use it
// const int i_rate_idx2 = bs_read( &s, 4 );
bs_skip( &s, 4 );
bs_skip( &s, 11 );
const int i_channel_idx = bs_read( &s, 5 );
p_mlp->i_channels = pu_channels[i_channel_idx];
}
else if( p_mlp->i_type == 0xba ) /* True HD */
{
i_rate_idx1 = bs_read( &s, 4 );
bs_skip( &s, 8 );
const int i_channel1 = bs_read( &s, 5 );
bs_skip( &s, 2 );
const int i_channel2 = bs_read( &s, 13 );
if( i_channel2 )
p_mlp->i_channels = TrueHdChannels( i_channel2 );
else
p_mlp->i_channels = TrueHdChannels( i_channel1 );
}
else
{
return VLC_EGENERIC;
}
if( i_rate_idx1 == 0x0f )
p_mlp->i_rate = 0;
else
p_mlp->i_rate = ( ( i_rate_idx1 & 0x8 ) ? 44100 : 48000 ) << (i_rate_idx1 & 0x7);
p_mlp->i_channels_conf = 0; /* TODO ? */
p_mlp->i_samples = 40 << ( i_rate_idx1 & 0x07 );
bs_skip( &s, 48 );
p_mlp->b_vbr = bs_read( &s, 1 );
p_mlp->i_bitrate = ( bs_read( &s, 15 ) * p_mlp->i_rate + 8) / 16;
p_mlp->i_substreams = bs_read( &s, 4 );
bs_skip( &s, 4 + 11 * 8 );
//fprintf( stderr, "i_samples = %d channels:%d rate:%d bitsrate=%d substreams=%d\n",
// p_mlp->i_samples, p_mlp->i_channels, p_mlp->i_rate, p_mlp->i_bitrate, p_mlp->i_substreams );
return VLC_SUCCESS;
}
static int SyncInfo( const uint8_t *p_hdr, bool *pb_mlp, mlp_header_t *p_mlp )
{
/* Check major sync presence */
const bool b_has_sync = !memcmp( &p_hdr[4], pu_start_code, 3 );
/* Wait for a major sync */
if( !b_has_sync && !*pb_mlp )
return 0;
/* Parse major sync if present */
if( b_has_sync )
{
*pb_mlp = !MlpParse( p_mlp, &p_hdr[4] );
if( !*pb_mlp )
return 0;
}
/* Check parity TODO even with major sync */
if( 1 )
{
int i_tmp = 0 ^ p_hdr[0] ^ p_hdr[1] ^ p_hdr[2] ^ p_hdr[3];
const uint8_t *p = &p_hdr[4 + ( b_has_sync ? 28 : 0 )];
for( unsigned i = 0; i < p_mlp->i_substreams; i++ )
{
i_tmp ^= *p++;
i_tmp ^= *p++;
if( p[-2] & 0x80 )
{
i_tmp ^= *p++;
i_tmp ^= *p++;
}
}
i_tmp = ( i_tmp >> 4 ) ^ i_tmp;
if( ( i_tmp & 0x0f ) != 0x0f )
return 0;
}
/* */
p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
if( !p_sys )
return VLC_ENOMEM;
const int i_word = ( ( p_hdr[0] << 8 ) | p_hdr[1] ) & 0xfff;
return i_word * 2;
}
/**
* It returns the size of an AC3 frame (or 0 if invalid)
*/
static int GetAc3Size( const uint8_t *p_buf )
{
static const int pi_rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
128, 160, 192, 224, 256, 320, 384, 448,
512, 576, 640 };
/* */
p_sys->i_state = STATE_NOSYNC;
date_Set( &p_sys->end_date, 0 );
const int i_frmsizecod = p_buf[4] & 63;
if( i_frmsizecod >= 38 )
return 0;
block_BytestreamInit( &p_sys->bytestream );
p_sys->b_mlp = false;
const int bitrate = pi_rate[i_frmsizecod >> 1];
/* Set output properties */
p_dec->fmt_out.i_cat = AUDIO_ES;
p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
p_dec->fmt_out.audio.i_rate = 0;
switch( p_buf[4] & 0xc0 )
{
case 0:
return 4 * bitrate;
case 0x40:
return 2 * (320 * bitrate / 147 + (i_frmsizecod & 1));
case 0x80:
return 6 * bitrate;
default:
return 0;
}
}
/* Set callback */
p_dec->pf_packetize = Packetize;
p_dec->pf_flush = Flush;
return VLC_SUCCESS;
/**
* It return the size of a EAC3 frame (or 0 if invalid)
*/
static int GetEac3Size( const uint8_t *p_buf )
{
int i_frame_size;
int i_bytes;
i_frame_size = ( ( p_buf[2] << 8 ) | p_buf[3] ) & 0x7ff;
if( i_frame_size < 2 )
return 0;
i_bytes = 2 * ( i_frame_size + 1 );
return i_bytes;
}
/**
* It returns the size of an AC3/EAC3 frame (or 0 if invalid)
*/
static int SyncInfoDolby( const uint8_t *p_buf )
{
int bsid;
/* Check synword */
if( p_buf[0] != 0x0b || p_buf[1] != 0x77 )
return 0;
/* Check bsid */
bsid = p_buf[5] >> 3;
if( bsid > 16 )
return 0;
if( bsid <= 10 )
return GetAc3Size( p_buf );
else
return GetEac3Size( p_buf );
}
/*****************************************************************************
* Flush:
*****************************************************************************/
static void Flush( decoder_t *p_dec )
{
decoder_sys_t *p_sys = p_dec->p_sys;
......@@ -152,9 +315,6 @@ static void Flush( decoder_t *p_dec )
date_Set( &p_sys->end_date, 0 );
}
/****************************************************************************
* Packetize:
****************************************************************************/
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
{
decoder_sys_t *p_sys = p_dec->p_sys;
......@@ -339,226 +499,44 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
return NULL;
}
/*****************************************************************************
* Close:
*****************************************************************************/
static void Close( vlc_object_t *p_this )
static int Open( vlc_object_t *p_this )
{
decoder_t *p_dec = (decoder_t*)p_this;
decoder_sys_t *p_sys = p_dec->p_sys;
block_BytestreamRelease( &p_sys->bytestream );
free( p_sys );
}
/**
* It parse MLP sync info.
*
* TODO handle CRC (at offset 26)
*/
static int TrueHdChannels( int i_map )
{
static const uint8_t pu_thd[13] =
{
2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1
};
int i_count = 0;
for( int i = 0; i < 13; i++ )
{
if( i_map & (1<<i) )
i_count += pu_thd[i];
}
return i_count;
}
static int MlpParse( mlp_header_t *p_mlp, const uint8_t p_hdr[MLP_HEADER_SYNC] )
{
bs_t s;
assert( !memcmp( p_hdr, pu_start_code, 3 ) );
/* TODO Checksum ? */
/* */
bs_init( &s, &p_hdr[3], MLP_HEADER_SYNC - 3 );
/* Stream type */
p_mlp->i_type = bs_read( &s, 8 );
int i_rate_idx1;
if( p_mlp->i_type == 0xbb ) /* MLP */
{
static const unsigned pu_channels[32] = {
1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4,
5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
bs_skip( &s, 4 + 4 );
i_rate_idx1 = bs_read( &s, 4 );
// Just skip the 4 following, since we don't use it
// const int i_rate_idx2 = bs_read( &s, 4 );
bs_skip( &s, 4 );
bs_skip( &s, 11 );
const int i_channel_idx = bs_read( &s, 5 );
p_mlp->i_channels = pu_channels[i_channel_idx];
}
else if( p_mlp->i_type == 0xba ) /* True HD */
{
i_rate_idx1 = bs_read( &s, 4 );
bs_skip( &s, 8 );
const int i_channel1 = bs_read( &s, 5 );
bs_skip( &s, 2 );
decoder_sys_t *p_sys;
const int i_channel2 = bs_read( &s, 13 );
if( i_channel2 )
p_mlp->i_channels = TrueHdChannels( i_channel2 );
else
p_mlp->i_channels = TrueHdChannels( i_channel1 );
}
else
{
if( p_dec->fmt_in.i_codec != VLC_CODEC_MLP &&
p_dec->fmt_in.i_codec != VLC_CODEC_TRUEHD )
return VLC_EGENERIC;
}
if( i_rate_idx1 == 0x0f )
p_mlp->i_rate = 0;
else
p_mlp->i_rate = ( ( i_rate_idx1 & 0x8 ) ? 44100 : 48000 ) << (i_rate_idx1 & 0x7);
p_mlp->i_channels_conf = 0; /* TODO ? */
p_mlp->i_samples = 40 << ( i_rate_idx1 & 0x07 );
bs_skip( &s, 48 );
p_mlp->b_vbr = bs_read( &s, 1 );
p_mlp->i_bitrate = ( bs_read( &s, 15 ) * p_mlp->i_rate + 8) / 16;
p_mlp->i_substreams = bs_read( &s, 4 );
bs_skip( &s, 4 + 11 * 8 );
//fprintf( stderr, "i_samples = %d channels:%d rate:%d bitsrate=%d substreams=%d\n",
// p_mlp->i_samples, p_mlp->i_channels, p_mlp->i_rate, p_mlp->i_bitrate, p_mlp->i_substreams );
return VLC_SUCCESS;
}
static int SyncInfo( const uint8_t *p_hdr, bool *pb_mlp, mlp_header_t *p_mlp )
{
/* Check major sync presence */
const bool b_has_sync = !memcmp( &p_hdr[4], pu_start_code, 3 );
/* Wait for a major sync */
if( !b_has_sync && !*pb_mlp )
return 0;
/* Parse major sync if present */
if( b_has_sync )
{
*pb_mlp = !MlpParse( p_mlp, &p_hdr[4] );
if( !*pb_mlp )
return 0;
}
/* Check parity TODO even with major sync */
if( 1 )
{
int i_tmp = 0 ^ p_hdr[0] ^ p_hdr[1] ^ p_hdr[2] ^ p_hdr[3];
const uint8_t *p = &p_hdr[4 + ( b_has_sync ? 28 : 0 )];
for( unsigned i = 0; i < p_mlp->i_substreams; i++ )
{
i_tmp ^= *p++;
i_tmp ^= *p++;
if( p[-2] & 0x80 )
{
i_tmp ^= *p++;
i_tmp ^= *p++;
}
}
i_tmp = ( i_tmp >> 4 ) ^ i_tmp;
if( ( i_tmp & 0x0f ) != 0x0f )
return 0;
}
/* */
const int i_word = ( ( p_hdr[0] << 8 ) | p_hdr[1] ) & 0xfff;
return i_word * 2;
}
p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
if( !p_sys )
return VLC_ENOMEM;
/**
* It returns the size of an AC3 frame (or 0 if invalid)
*/
static int GetAc3Size( const uint8_t *p_buf )
{
static const int pi_rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
128, 160, 192, 224, 256, 320, 384, 448,
512, 576, 640 };
/* */
const int i_frmsizecod = p_buf[4] & 63;
if( i_frmsizecod >= 38 )
return 0;
const int bitrate = pi_rate[i_frmsizecod >> 1];
switch( p_buf[4] & 0xc0 )
{
case 0:
return 4 * bitrate;
case 0x40:
return 2 * (320 * bitrate / 147 + (i_frmsizecod & 1));
case 0x80:
return 6 * bitrate;
default:
return 0;
}
}
p_sys->i_state = STATE_NOSYNC;
date_Set( &p_sys->end_date, 0 );
/**
* It return the size of a EAC3 frame (or 0 if invalid)
*/
static int GetEac3Size( const uint8_t *p_buf )
{
int i_frame_size;
int i_bytes;
block_BytestreamInit( &p_sys->bytestream );
p_sys->b_mlp = false;
i_frame_size = ( ( p_buf[2] << 8 ) | p_buf[3] ) & 0x7ff;
if( i_frame_size < 2 )
return 0;
i_bytes = 2 * ( i_frame_size + 1 );
/* Set output properties */
p_dec->fmt_out.i_cat = AUDIO_ES;
p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
p_dec->fmt_out.audio.i_rate = 0;
return i_bytes;
/* Set callback */
p_dec->pf_packetize = Packetize;
p_dec->pf_flush = Flush;
return VLC_SUCCESS;
}
/**
* It returns the size of an AC3/EAC3 frame (or 0 if invalid)
*/
static int SyncInfoDolby( const uint8_t *p_buf )
static void Close( vlc_object_t *p_this )
{
int bsid;
/* Check synword */
if( p_buf[0] != 0x0b || p_buf[1] != 0x77 )
return 0;
decoder_t *p_dec = (decoder_t*)p_this;
decoder_sys_t *p_sys = p_dec->p_sys;
/* Check bsid */
bsid = p_buf[5] >> 3;
if( bsid > 16 )
return 0;
block_BytestreamRelease( &p_sys->bytestream );
if( bsid <= 10 )
return GetAc3Size( p_buf );
else
return GetEac3Size( p_buf );
free( p_sys );
}
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