Commit 14c9f24b authored by Arthur Tritthart's avatar Arthur Tritthart
Browse files

fixed some issues in arithmetic decoder, might be NON-BE for EVS

parent 6e74641a
Loading
Loading
Loading
Loading
Loading
+105 −8
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
/*---------------------------------------------------------------
 * Ari decode 14 bits routines
  -------------------------------------------------------------*/
// Please remove all DEBUG lines, when FIX_2402 is done!!
//#define DEBUG_ari_decode_14bits

#ifdef FIX_2402_SIMPLIFY_ARI_CODEC
@@ -26,7 +27,15 @@ static Word32 L_msui_32_16( Word32 cum, Word32 range, Word16 cum_freq )

Word32 ari_decode_overflow_fx( Tastat *s )
{
#ifdef DEBUG_ari_decode_14bits
    if (s->high <= s->low)
        printf("ari_decode_overflow detected: high=0x%08X  low=0x%08X\n", s->high, s->low);
#endif	
#ifdef FIX_2402_SIMPLIFY_ARI_CODEC
    return ( L_sub( s->high, s->low ) <= 0 );
#else
    return ( L_sub( L_sub( s->high, 1 ), s->low ) <= 0 );
#endif
}

/**
@@ -51,7 +60,7 @@ void ari_start_decoding_14bits_fx(
    move32();

#ifdef DEBUG_ari_decode_14bits
    printf( "ari_start_decoding_14bits_fx: low=0x%08X  high=0x%08X  value=0x%08X\n", s->low, s->high, s->value );
    //printf( "ari_start_decoding_14bits_fx: low=0x%08X  high=0x%08X  value=0x%08X\n", s->low, s->high, s->value );
#endif
}

@@ -83,10 +92,10 @@ Word16 ari_start_decoding_14bits_prm_fx(
    s->value = val;
    move32();

    return add( bp, i );
#ifdef DEBUG_ari_decode_14bits
    printf( "ari_start_decoding_14bits_prm_fx: low=0x%08X  high=0x%08X  value=0x%08X  i=%d\n", s->low, s->high, s->value, i );
    //printf( "ari_start_decoding_14bits_prm_fx: low=0x%08X  high=0x%08X  value=0x%08X  i=%d\n", s->low, s->high, s->value, i );
#endif
    return add( bp, i );
}

static Word16 ari_lookup_s17_fx(
@@ -386,7 +395,8 @@ static Word16 ari_decode_14bits_ext_fx(
    low = L_add( low, mul_sbc_14bits( range, cum_freq[add( symbol, 1 )] ) );

#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_ext_fx: low=0x%08X  high=0x%08X  value=0x%08X  symbol=0x%04X  range=0x%08X  cum=0x%08X\n", low, high, value, symbol, range, cum );
//    printf( "ari_decode_14bits_ext_fx(look=%s): low=0x%08X  high=0x%08X  value=0x%08X  symbol=0x%04X  range=0x%08X  cum=0x%08X\n", ( lookup_fn == ari_lookup_bit_fx ) ? "bit" : ( lookup_fn == ari_lookup_s17_fx ) ? "s17" : "s27",
//        low, high, value, symbol, range, cum );
#endif

    FOR( i = 0; i < 0x7FFF; i++ )
@@ -409,8 +419,8 @@ static Word16 ari_decode_14bits_ext_fx(
        value = L_msu( value, 1, tab_ari_qnew[L_msb_high][L_msb_low] );
        value = L_mac0( L_shl( value, 1 ), 1, get_next_indice_1_fx( st ) );
#ifdef DEBUG_ari_decode_14bits
        printf( "=>low=0x%08X  high=0x%08X  value=0x%08X  dh=%d  dl=%d\n",
                low, high, value, L_msb_high, L_msb_low );
       // printf( "=>low=0x%08X  high=0x%08X  value=0x%08X  dh=%d  dl=%d\n",
       //         low, high, value, L_msb_high, L_msb_low );
#endif
    }

@@ -451,7 +461,16 @@ Word16 ari_decode_14bits_bit_ext_fx( Decoder_State *st, Tastat *s )
    move16();
    move16();
    move16();
#ifdef DEBUG_ari_decode_14bits
    Word16 bp = st->next_bit_pos;
    UWord16 *ptr = st->bit_stream;
    printf("ari_decode_14bits_bit_ext(beg): high=0x%08X low=0x%08X value=0x%08X ptr[bp=%d] = %d | %d | %d | %d\n", s->high, s->low, s->value, bp, ptr[bp], ptr[bp+1], ptr[bp+2], ptr[bp+3]);
    Word16 symbol = ari_decode_14bits_ext_fx( st, s, cum_freq, ari_lookup_bit_fx );
    printf("ari_decode_14bits_bit_ext(end): high=0x%08X low=0x%08X value=0x%08X symbol=0x%04X  bp=%d\n", s->high, s->low, s->value, symbol, st->next_bit_pos );
    return symbol;
#else
    return ari_decode_14bits_ext_fx( st, s, cum_freq, ari_lookup_bit_fx );
#endif
}

static Word16 ari_lookup_pow_fx( Tastat *s, Word16 base )
@@ -544,6 +563,9 @@ static Word16 ari_lookup_pow_fx( Tastat *s, Word16 base )
    move32();
    s->low = L_add( s->low, mul_sbc_14bits( range, lowlim ) );
    move32();
#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_pow_fx+mul_sbc_14bits: high: 0x%08X  low: 0x%08X  range: 0x%08X (lookup)\n", s->high, s->low, range );
#endif

    return symbol;
}
@@ -557,6 +579,10 @@ static Word16 ari_lookup_sign_fx( Tastat *s, Word16 base )

    range = L_sub( s->high, s->low );
    cum = L_sub( s->value, s->low );

#ifdef DEBUG_ari_decode_14bits
    printf( "sign-A: high: 0x%08X  low: 0x%08X  value: 0x%08X  range: 0x%08X  cum: 0x%08X\n", s->high, s->low, s->value, range, cum );
#endif
    range = L_shr( range, 1 );

    IF( GT_32( range, cum ) )
@@ -565,6 +591,10 @@ static Word16 ari_lookup_sign_fx( Tastat *s, Word16 base )
        move16();
        s->high = L_add( s->low, range );
        move32();
#ifdef DEBUG_ari_decode_14bits
        printf( "sign-B1: high: 0x%08X  low: 0x%08X  value: 0x%08X  range: 0x%08X  cum: 0x%08X\n", s->high, s->low, s->value, range, cum );

#endif
    }
    ELSE
    {
@@ -572,6 +602,9 @@ static Word16 ari_lookup_sign_fx( Tastat *s, Word16 base )
        move16();
        s->low = L_add( s->low, range );
        move32();
#ifdef DEBUG_ari_decode_14bits
        printf( "sign-B2: high: 0x%08X  low: 0x%08X  value: 0x%08X  range: 0x%08X  cum: 0x%08X\n", s->high, s->low, s->value, range, cum );
#endif
    }

    return symbol;
@@ -595,6 +628,16 @@ static Word16 ari_decode_14bits_notbl_fx(
    high = L_add( s->high, 0 );
    value = L_add( s->value, 0 );

#ifdef DEBUG_ari_decode_14bits
    //if (lookup_fn == ari_lookup_pow_fx)
    //printf( "ari_decode_14bits_pow_fx+mul_sbc_14bits: high: 0x%08X  low: 0x%08X\n", high, low );
#endif
#ifdef DEBUG_ari_decode_14bits
    //if ( lookup_fn == ari_lookup_pow_fx )
    //    printf( "ari_decode_14bits_notbl_fx (pow): symbol=0x%04X  high=0x%08X low=0x%08X value=0x%08X\n",
    //            symbol, high, low, value );
#endif

    FOR( ; bp < bits; ++bp )
    {
        Word32 L_msb_diff, L_msb_low, L_msb_high;
@@ -623,6 +666,9 @@ static Word16 ari_decode_14bits_notbl_fx(
    IF( ( lookup_fn != ari_lookup_sign_fx ) && !( NE_16( bp, bits ) || !( ( EQ_32( s->low, low ) ) && ( EQ_32( s->high, L_sub( high, 1 ) ) ) && ( EQ_32( s->value, value ) ) ) ) )
    {
        /* This should not happen except of bit errors. */
#ifdef DEBUG_ari_decode_14bits
        printf( "ari_decode_14bits_notbl_fx: bit error detected high: 0x%08X  low: 0x%08X  value: 0x%08X  bp: %d bits: %d\n", high, low, value, bp, bits );
#endif
        s->high = 0;
        move32();
        s->low = 0;
@@ -641,6 +687,10 @@ static Word16 ari_decode_14bits_notbl_fx(

    *res = symbol;
    move16();
#ifdef DEBUG_ari_decode_14bits
    if ( lookup_fn == ari_lookup_sign_fx )
        printf( "sign-X: high: 0x%08X  low: 0x%08X  value: 0x%08X  symbol: 0x%04X\n", high, low, value, *res );
#endif

    return bp;
}
@@ -657,7 +707,28 @@ static Word16 ari_decode_14bits_notbl_fx(
 *-------------------------------------------------------------------------*/
Word16 ari_decode_14bits_pow_fx( Word16 *ptr, Word16 bp, Word16 bits, Word16 *res, Tastat *s, Word16 base )
{
    return ari_decode_14bits_notbl_fx( ptr, bp, bits, res, s, base, ari_lookup_pow_fx );
	/* There are two versions to call: pow_ivas or pow_fx (default) */
#if 0
#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_pow_ivas(inp): high=0x%08X low=0x%08X value=0x%08X ptr[bp=%d] = %d | %d | %d | %d\n",
        s->high, s->low, s->value, bp, ptr[bp], ptr[bp+1], ptr[bp+2], ptr[bp+3] );
#endif        
    Word16 result = ari_decode_14bits_pow_ivas( ptr, bp, bits, res, s, base );
#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_pow_ivas(end): high=0x%08X low=0x%08X value=0x%08X symbol=0x%04X  bp=%d\n", s->high, s->low, s->value, *res, result );
#endif    
    return result;
#else
#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_pow_fx(inp): high=0x%08X low=0x%08X value=0x%08X ptr[bp=%d] = %d | %d | %d | %d\n",
        s->high, s->low, s->value, bp, ptr[bp], ptr[bp+1], ptr[bp+2], ptr[bp+3] );
#endif        
    Word16 result = ari_decode_14bits_notbl_fx( ptr, bp, bits, res, s, base, ari_lookup_pow_fx );
#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_pow_fx(end): high=0x%08X low=0x%08X value=0x%08X symbol=0x%04X  bp=%d\n", s->high, s->low, s->value, *res, result );
#endif
    return result;
#endif
}

/*------------------------------------------------------------------------
@@ -667,7 +738,15 @@ Word16 ari_decode_14bits_pow_fx( Word16 *ptr, Word16 bp, Word16 bits, Word16 *re
 *-------------------------------------------------------------------------*/
Word16 ari_decode_14bits_sign_fx( Word16 *ptr, Word16 bp, Word16 bits, Word16 *res, Tastat *s )
{
    return ari_decode_14bits_notbl_fx( ptr, bp, bits, res, s, 0, ari_lookup_sign_fx );
#ifdef DEBUG_ari_decode_14bits
    //printf( "ari_decode_14bits_sign_fx(inp): high=0x%08X low=0x%08X value=0x%08X ptr[bp=%d] = %d | %d | %d | %d\n",
    //        s->high, s->low, s->value, bp, ptr[bp], ptr[bp + 1], ptr[bp + 2], ptr[bp + 3] );
#endif
    Word16 result = ari_decode_14bits_notbl_fx( ptr, bp, bits, res, s, 0, ari_lookup_sign_fx );
#ifdef DEBUG_ari_decode_14bits
    //printf( "ari_decode_14bits_sign_fx(end): high=0x%08X low=0x%08X value=0x%08X symbol=0x%04X  bp=%d\n", s->high, s->low, s->value, *res, result );
#endif
    return result;
}

/*---------------------------------------------------------------
@@ -740,6 +819,11 @@ Word16 ari_decode_14bits_pow_ivas(
    cum = (UWord32) W_add( W_shl( ( W_sub( value, low ) ), stat_bitsnew ), ( 1 << stat_bitsnew ) - 1 );
    move32();


#ifdef DEBUG_ari_decode_14bits
    //printf( "ari_decode_14bits_pow_ivas: low=0x%08X  high=0x%08X  value=0x%08X  symbol=0x%04X  range=0x%08X  cum=0x%08X\n", low, high, value, symbol, range, cum );
#endif

    /* search for the interval where "cum" fits */
    IF( GT_64( W_mult0_32_32( L_shr( base, 1 ), range ), cum ) ) /* below pow-1 */
    {
@@ -763,6 +847,9 @@ Word16 ari_decode_14bits_pow_ivas(
                BREAK;
            }
        }
#ifdef DEBUG_ari_decode_14bits
        //printf( "ari_decode_14bits_pow_ivas (below-1): k=%d  symbol=0x%04X  pows[0]=0x%04X  pows[%d]=0x%04X\n", k, symbol, pows[0], k-1, pows[k-1] );
#endif
        assert( k < 12 ); /* maximum 2^10-1*/
        /* narrow the range down */
        FOR( k--; k > 0; k-- )
@@ -785,6 +872,9 @@ Word16 ari_decode_14bits_pow_ivas(
    }
    ELSE /* trivial case, above pow-1, that is, first symbol */
    {
#ifdef DEBUG_ari_decode_14bits
        //printf( "ari_decode_14bits_pow_ivas (above pow-1): symbol=0  base=0x%04X\n", base );
#endif
        symbol = 0;
        lowlim = extract_l( L_shr( base, 1 ) );
        highlim = 16384;
@@ -797,6 +887,10 @@ Word16 ari_decode_14bits_pow_ivas(

    low = L_add( low, mul_sbc_14bits( range, lowlim ) );

#ifdef DEBUG_ari_decode_14bits
    printf( "ari_decode_14bits_pow_ivas+mul_sbc_14bits: high: 0x%08X  low: 0x%08X  range: 0x%08X\n", high, low, range );
#endif

    /*ptr init for ptr*/
    FOR( ; bp < bits; )
    {
@@ -854,6 +948,9 @@ Word16 ari_decode_14bits_pow_ivas(

    *res = symbol;
    move16();
#ifdef DEBUG_ari_decode_14bits
    //printf( "ari_decode_14bits_pow_ivas(end): high=0x%08X low=0x%08X value=0x%08X symbol=0x%04X  bp=%d\n", s->high, s->low, s->value, *res, bp );
#endif
    return bp;
}