@ -99,7 +99,8 @@ static VLC huffman_vlc[4][4];
static const int vlc_offsets [ 17 ] = {
0 , 640 , 1156 , 1732 , 2308 , 2852 , 3396 , 3924 ,
4452 , 5220 , 5860 , 6628 , 7268 , 7908 , 8424 , 8936 , VLC_TABLES_SIZE } ;
4452 , 5220 , 5860 , 6628 , 7268 , 7908 , 8424 , 8936 , VLC_TABLES_SIZE
} ;
static VLC_TYPE vlc_tables [ VLC_TABLES_SIZE ] [ 2 ] ;
@ -130,13 +131,10 @@ static av_cold int imc_decode_init(AVCodecContext * avctx)
r1 = sin ( ( i * 4.0 + 1.0 ) / 1024.0 * M_PI ) ;
r2 = cos ( ( i * 4.0 + 1.0 ) / 1024.0 * M_PI ) ;
if ( i & 0x1 )
{
if ( i & 0x1 ) {
q - > pre_coef1 [ i ] = ( r1 + r2 ) * sqrt ( 2.0 ) ;
q - > pre_coef2 [ i ] = - ( r1 - r2 ) * sqrt ( 2.0 ) ;
}
else
{
} else {
q - > pre_coef1 [ i ] = - ( r1 + r2 ) * sqrt ( 2.0 ) ;
q - > pre_coef2 [ i ] = ( r1 - r2 ) * sqrt ( 2.0 ) ;
}
@ -146,9 +144,8 @@ static av_cold int imc_decode_init(AVCodecContext * avctx)
/* Generate a square root table */
for ( i = 0 ; i < 30 ; i + + ) {
for ( i = 0 ; i < 30 ; i + + )
q - > sqrt_tab [ i ] = sqrt ( i ) ;
}
/* initialize the VLC tables */
for ( i = 0 ; i < 4 ; i + + ) {
@ -176,7 +173,8 @@ static av_cold int imc_decode_init(AVCodecContext * avctx)
return 0 ;
}
static void imc_calculate_coeffs ( IMCContext * q , float * flcoeffs1 , float * flcoeffs2 , int * bandWidthT ,
static void imc_calculate_coeffs ( IMCContext * q , float * flcoeffs1 ,
float * flcoeffs2 , int * bandWidthT ,
float * flcoeffs3 , float * flcoeffs5 )
{
float workT1 [ BANDS ] ;
@ -230,7 +228,8 @@ static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeff
}
static void imc_read_level_coeffs ( IMCContext * q , int stream_format_code , int * levlCoeffs )
static void imc_read_level_coeffs ( IMCContext * q , int stream_format_code ,
int * levlCoeffs )
{
int i ;
VLC * hufftab [ 4 ] ;
@ -250,14 +249,15 @@ static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* le
if ( start )
levlCoeffs [ 0 ] = get_bits ( & q - > gb , 7 ) ;
for ( i = start ; i < BANDS ; i + + ) {
levlCoeffs [ i ] = get_vlc2 ( & q - > gb , hufftab [ cb_sel [ i ] ] - > table , hufftab [ cb_sel [ i ] ] - > bits , 2 ) ;
levlCoeffs [ i ] = get_vlc2 ( & q - > gb , hufftab [ cb_sel [ i ] ] - > table ,
hufftab [ cb_sel [ i ] ] - > bits , 2 ) ;
if ( levlCoeffs [ i ] = = 17 )
levlCoeffs [ i ] + = get_bits ( & q - > gb , 4 ) ;
}
}
static void imc_decode_level_coefficients ( IMCContext * q , int * levlCoeffBuf , float * flcoeffs1 ,
float * flcoeffs2 )
static void imc_decode_level_coefficients ( IMCContext * q , int * levlCoeffBuf ,
float * flcoeffs1 , float * flcoeffs2 )
{
int i , level ;
float tmp , tmp2 ;
@ -290,12 +290,15 @@ static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, floa
}
static void imc_decode_level_coefficients2 ( IMCContext * q , int * levlCoeffBuf , float * old_floor , float * flcoeffs1 ,
float * flcoeffs2 ) {
static void imc_decode_level_coefficients2 ( IMCContext * q , int * levlCoeffBuf ,
float * old_floor , float * flcoeffs1 ,
float * flcoeffs2 )
{
int i ;
//FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
// and flcoeffs2 old scale factors
// might be incomplete due to a missing table that is in the binary code
/* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
* and flcoeffs2 old scale factors
* might be incomplete due to a missing table that is in the binary code
*/
for ( i = 0 ; i < BANDS ; i + + ) {
flcoeffs1 [ i ] = 0 ;
if ( levlCoeffBuf [ i ] < 16 ) {
@ -310,7 +313,9 @@ static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, flo
/**
* Perform bit allocation depending on bits available
*/
static int bit_allocation ( IMCContext * q , int stream_format_code , int freebits , int flag ) {
static int bit_allocation ( IMCContext * q , int stream_format_code , int freebits ,
int flag )
{
int i , j ;
const float limit = - 1.e20 ;
float highest = 0.0 ;
@ -330,9 +335,8 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
for ( i = 0 ; i < BANDS ; i + + )
highest = FFMAX ( highest , q - > flcoeffs1 [ i ] ) ;
for ( i = 0 ; i < BANDS - 1 ; i + + ) {
for ( i = 0 ; i < BANDS - 1 ; i + + )
q - > flcoeffs4 [ i ] = q - > flcoeffs3 [ i ] - log ( q - > flcoeffs5 [ i ] ) / log ( 2 ) ;
}
q - > flcoeffs4 [ BANDS - 1 ] = limit ;
highest = highest * 0.25 ;
@ -351,7 +355,7 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
if ( indx = = - 1 )
return AVERROR_INVALIDDATA ;
q - > flcoeffs4 [ i ] = q - > flcoeffs4 [ i ] + xTab [ ( indx * 2 + ( q - > flcoeffs1 [ i ] < highest ) ) * 2 + flag ] ;
q - > flcoeffs4 [ i ] + = xTab [ ( indx * 2 + ( q - > flcoeffs1 [ i ] < highest ) ) * 2 + flag ] ;
}
if ( stream_format_code & 0x2 ) {
@ -371,7 +375,8 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
for ( i = 0 ; i < BANDS / 2 ; i + + ) {
rres = summer - freebits ;
if ( ( rres > = - 8 ) & & ( rres < = 8 ) ) break ;
if ( ( rres > = - 8 ) & & ( rres < = 8 ) )
break ;
summer = 0 ;
iacc = 0 ;
@ -405,7 +410,8 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
if ( freebits > summer ) {
for ( i = 0 ; i < BANDS ; i + + ) {
workT [ i ] = ( q - > bitsBandT [ i ] = = 6 ) ? - 1.e20 : ( q - > bitsBandT [ i ] * - 2 + q - > flcoeffs4 [ i ] - 0.415 ) ;
workT [ i ] = ( q - > bitsBandT [ i ] = = 6 ) ? - 1.e20
: ( q - > bitsBandT [ i ] * - 2 + q - > flcoeffs4 [ i ] - 0.415 ) ;
}
highest = 0.0 ;
@ -426,7 +432,7 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
if ( highest > - 1.e20 ) {
workT [ found_indx ] - = 2.0 ;
if ( + + ( q - > bitsBandT [ found_indx ] ) = = 6 )
if ( + + q - > bitsBandT [ found_indx ] = = 6 )
workT [ found_indx ] = - 1.e20 ;
for ( j = band_tab [ found_indx ] ; j < band_tab [ found_indx + 1 ] & & ( freebits > summer ) ; j + + ) {
@ -438,7 +444,8 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
}
if ( freebits < summer ) {
for ( i = 0 ; i < BANDS ; i + + ) {
workT [ i ] = q - > bitsBandT [ i ] ? ( q - > bitsBandT [ i ] * - 2 + q - > flcoeffs4 [ i ] + 1.585 ) : 1.e20 ;
workT [ i ] = q - > bitsBandT [ i ] ? ( q - > bitsBandT [ i ] * - 2 + q - > flcoeffs4 [ i ] + 1.585 )
: 1.e20 ;
}
if ( stream_format_code & 0x2 ) {
workT [ 0 ] = 1.e20 ;
@ -455,10 +462,11 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
low_indx = i ;
}
}
//if(lowest >= 1.e10) break;
// if (lowest >= 1.e10)
// break;
workT [ low_indx ] = lowest + 2.0 ;
if ( ! ( - - q - > bitsBandT [ low_indx ] ) )
if ( ! - - q - > bitsBandT [ low_indx ] )
workT [ low_indx ] = 1.e20 ;
for ( j = band_tab [ low_indx ] ; j < band_tab [ low_indx + 1 ] & & ( freebits < summer ) ; j + + ) {
@ -472,7 +480,8 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
return 0 ;
}
static void imc_get_skip_coeff ( IMCContext * q ) {
static void imc_get_skip_coeff ( IMCContext * q )
{
int i , j ;
memset ( q - > skipFlagBits , 0 , sizeof ( q - > skipFlagBits ) ) ;
@ -485,11 +494,12 @@ static void imc_get_skip_coeff(IMCContext* q) {
q - > skipFlagBits [ i ] = band_tab [ i + 1 ] - band_tab [ i ] ;
for ( j = band_tab [ i ] ; j < band_tab [ i + 1 ] ; j + + ) {
if ( ( q - > skipFlags [ j ] = get_bits1 ( & q - > gb ) ) )
q - > skipFlags [ j ] = get_bits1 ( & q - > gb ) ;
if ( q - > skipFlags [ j ] )
q - > skipFlagCount [ i ] + + ;
}
} else {
for ( j = band_tab [ i ] ; j < ( band_tab [ i + 1 ] - 1 ) ; j + = 2 ) {
for ( j = band_tab [ i ] ; j < band_tab [ i + 1 ] - 1 ; j + = 2 ) {
if ( ! get_bits1 ( & q - > gb ) ) { // 0
q - > skipFlagBits [ i ] + + ;
q - > skipFlags [ j ] = 1 ;
@ -526,7 +536,8 @@ static void imc_get_skip_coeff(IMCContext* q) {
/**
* Increase highest ' band coefficient sizes as some bits won ' t be used
*/
static void imc_adjust_bit_allocation ( IMCContext * q , int summer ) {
static void imc_adjust_bit_allocation ( IMCContext * q , int summer )
{
float workT [ 32 ] ;
int corrected = 0 ;
int i , j ;
@ -534,7 +545,8 @@ static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
int found_indx = 0 ;
for ( i = 0 ; i < BANDS ; i + + ) {
workT [ i ] = ( q - > bitsBandT [ i ] = = 6 ) ? - 1.e20 : ( q - > bitsBandT [ i ] * - 2 + q - > flcoeffs4 [ i ] - 0.415 ) ;
workT [ i ] = ( q - > bitsBandT [ i ] = = 6 ) ? - 1.e20
: ( q - > bitsBandT [ i ] * - 2 + q - > flcoeffs4 [ i ] - 0.415 ) ;
}
while ( corrected < summer ) {
@ -565,7 +577,8 @@ static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
}
}
static void imc_imdct256 ( IMCContext * q ) {
static void imc_imdct256 ( IMCContext * q )
{
int i ;
float re , im ;
@ -585,13 +598,16 @@ static void imc_imdct256(IMCContext *q) {
for ( i = 0 ; i < COEFFS / 2 ; i + + ) {
re = ( q - > samples [ i ] . re * q - > post_cos [ i ] ) + ( - q - > samples [ i ] . im * q - > post_sin [ i ] ) ;
im = ( - q - > samples [ i ] . im * q - > post_cos [ i ] ) - ( q - > samples [ i ] . re * q - > post_sin [ i ] ) ;
q - > out_samples [ i * 2 ] = ( q - > mdct_sine_window [ COEFFS - 1 - i * 2 ] * q - > last_fft_im [ i ] ) + ( q - > mdct_sine_window [ i * 2 ] * re ) ;
q - > out_samples [ COEFFS - 1 - i * 2 ] = ( q - > mdct_sine_window [ i * 2 ] * q - > last_fft_im [ i ] ) - ( q - > mdct_sine_window [ COEFFS - 1 - i * 2 ] * re ) ;
q - > out_samples [ i * 2 ] = ( q - > mdct_sine_window [ COEFFS - 1 - i * 2 ] * q - > last_fft_im [ i ] )
+ ( q - > mdct_sine_window [ i * 2 ] * re ) ;
q - > out_samples [ COEFFS - 1 - i * 2 ] = ( q - > mdct_sine_window [ i * 2 ] * q - > last_fft_im [ i ] )
- ( q - > mdct_sine_window [ COEFFS - 1 - i * 2 ] * re ) ;
q - > last_fft_im [ i ] = im ;
}
}
static int inverse_quant_coeff ( IMCContext * q , int stream_format_code ) {
static int inverse_quant_coeff ( IMCContext * q , int stream_format_code )
{
int i , j ;
int middle_value , cw_len , max_size ;
const float * quantizer ;
@ -629,11 +645,13 @@ static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
}
static int imc_get_coeffs ( IMCContext * q ) {
static int imc_get_coeffs ( IMCContext * q )
{
int i , j , cw_len , cw ;
for ( i = 0 ; i < BANDS ; i + + ) {
if ( ! q - > sumLenArr [ i ] ) continue ;
if ( ! q - > sumLenArr [ i ] )
continue ;
if ( q - > bandFlagsBuf [ i ] | | q - > bandWidthT [ i ] ) {
for ( j = band_tab [ i ] ; j < band_tab [ i + 1 ] ; j + + ) {
cw_len = q - > CWlengthT [ j ] ;
@ -707,8 +725,10 @@ static int imc_decode_frame(AVCodecContext * avctx, void *data,
if ( q - > decoder_reset ) {
memset ( q - > out_samples , 0 , sizeof ( q - > out_samples ) ) ;
for ( i = 0 ; i < BANDS ; i + + ) q - > old_floor [ i ] = 1.0 ;
for ( i = 0 ; i < COEFFS ; i + + ) q - > CWdecoded [ i ] = 0 ;
for ( i = 0 ; i < BANDS ; i + + )
q - > old_floor [ i ] = 1.0 ;
for ( i = 0 ; i < COEFFS ; i + + )
q - > CWdecoded [ i ] = 0 ;
q - > decoder_reset = 0 ;
}
@ -716,9 +736,11 @@ static int imc_decode_frame(AVCodecContext * avctx, void *data,
imc_read_level_coeffs ( q , stream_format_code , q - > levlCoeffBuf ) ;
if ( stream_format_code & 0x4 )
imc_decode_level_coefficients ( q , q - > levlCoeffBuf , q - > flcoeffs1 , q - > flcoeffs2 ) ;
imc_decode_level_coefficients ( q , q - > levlCoeffBuf ,
q - > flcoeffs1 , q - > flcoeffs2 ) ;
else
imc_decode_level_coefficients2 ( q , q - > levlCoeffBuf , q - > old_floor , q - > flcoeffs1 , q - > flcoeffs2 ) ;
imc_decode_level_coefficients2 ( q , q - > levlCoeffBuf , q - > old_floor ,
q - > flcoeffs1 , q - > flcoeffs2 ) ;
memcpy ( q - > old_floor , q - > flcoeffs1 , 32 * sizeof ( float ) ) ;
@ -758,7 +780,8 @@ static int imc_decode_frame(AVCodecContext * avctx, void *data,
}
if ( ( ret = bit_allocation ( q , stream_format_code ,
512 - bitscount - get_bits_count ( & q - > gb ) , flag ) ) < 0 ) {
512 - bitscount - get_bits_count ( & q - > gb ) ,
flag ) ) < 0 ) {
av_log ( avctx , AV_LOG_ERROR , " Bit allocations failed \n " ) ;
q - > decoder_reset = 1 ;
return ret ;