Commit c5fcb0ae authored by JÜRGEN Gerstacker's avatar JÜRGEN Gerstacker
Browse files

replaced EVS arith-encoding functions by IVAS functions. EVS pipeline is bitexact

parent 06d6085f
Loading
Loading
Loading
Loading
Loading
+25 −2
Original line number Diff line number Diff line
@@ -17,6 +17,16 @@
#define MAKE_NUMBER_QX( number, QX )     ( ( number ) << ( QX ) )                                      /* evaulated at compile time */
#define MAKE_VARIABLE_QX( variable, QX ) W_shl( W_deposit32_l( L_deposit_l( ( variable ) ) ), ( QX ) ) /* evaluated at run time */


#if 1  //VERSX
#include "issue2402_replacement.h"
#else
#define HIGH(p) ((p)+0)
#define BITS_TO_FOLLOW value
#endif



/*-------------------------------------------------------------------*
 * ACcontextMapping_encode2_no_mem_s17_LC_fx()
 *
@@ -94,6 +104,9 @@ void ACcontextMapping_encode2_no_mem_s17_LC_fx(
    flag_overflow = 0;
    move16();

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("in ACcontextMapping_encode2_no_mem_s17_LC_fx()\n");
#endif
    IF( hm_cfg ) /* mapped domain */
    {
        numPeakIndicesOrig = hm_cfg->numPeakIndices; /* Q0 */
@@ -161,6 +174,9 @@ void ACcontextMapping_encode2_no_mem_s17_LC_fx(
        b1_i = get_next_coeff( ii, &p2, &idx2, hm_cfg ); /* Q0 */

        idx = s_min( idx1, idx2 ); /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("start loop: k,lastnz=%d,%d, a1_i,b1_i,idx= %d,%d,%d\n", k,lastnz, a1_i, b1_i, idx );
#endif

        /* Get context */
        ctx = &c[s_or( p1, p2 )];  /* Q0 */
@@ -190,6 +206,9 @@ void ACcontextMapping_encode2_no_mem_s17_LC_fx(

        /*Copy states*/
        ari_copy_states_fx( &as, &as_overflow );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("middle loop: k,lastnz=%d,%d, a1_i,b1_i,idx= %d,%d,%d\n", k,lastnz, a1_i, b1_i, idx );
#endif
        bp_overflow = bp; /* Q0 */
        move16();
        nbbits_signs_overflow = nbbits_signs; /* Q0 */
@@ -220,6 +239,10 @@ void ACcontextMapping_encode2_no_mem_s17_LC_fx(
            move16();
            bp = ari_encode_14bits_ext_fx( ptr, bp, &as, VAL_ESC, ari_pk_s17_LC_ext[pki] ); /* Q0 */

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
            printf("l,h,v= 0x%04x,0x%04x,0x%04x, bp=%03d, a1,b1=0x%04x,0x%04x\n",
              as.low, HIGH(as.high), as.BITS_TO_FOLLOW, bp, a1,b1);
#endif
            *lsbs_bits2-- = s_and( a1, 1 );
            move16();
            *lsbs_bits2-- = s_and( b1, 1 );
@@ -241,7 +264,7 @@ void ACcontextMapping_encode2_no_mem_s17_LC_fx(
        bp = ari_encode_14bits_ext_fx( ptr, bp, &as, add( a1, shl( b1, A_THRES_SHIFT ) ), ari_pk_s17_LC_ext[pki] ); /* Q0 */

        /* Check bit budget */
        IF( GT_16( add( add( add( bp, extract_l( as.value ) ), nbbits_signs ), nbbits_lsbs ), nbbits_m2 ) )
        IF( GT_16( add( add( add( bp, extract_l( as.BITS_TO_FOLLOW ) ), nbbits_signs ), nbbits_lsbs ), nbbits_m2 ) )
        {
            ari_copy_states_fx( &as_overflow, &as );
            bp = bp_overflow; /* Q0 */
@@ -271,7 +294,7 @@ void ACcontextMapping_encode2_no_mem_s17_LC_fx(
                    move16();
                    bp = ari_encode_14bits_ext_fx( ptr, bp, &as, 0, ari_pk_s17_LC_ext[pki] ); /* Q0 */

                    IF( GT_16( add( add( add( bp, extract_l( as.value ) ), nbbits_signs ), nbbits_lsbs ), nbbits_m2 ) )
                    IF( GT_16( add( add( add( bp, extract_l( as.BITS_TO_FOLLOW ) ), nbbits_signs ), nbbits_lsbs ), nbbits_m2 ) )
                    {
                        ari_copy_states_fx( &as_overflow, &as );
                        bp = bp_overflow; /* Q0 */
+89 −13
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
#include "prot_fx_enc.h" /* Function prototypes                    */



/*---------------------------------------------------------------
 * ari_copy_states_fx()
 *
@@ -20,6 +21,7 @@
 * \return none
 *-------------------------------------------------------------*/

#if 0  //VERSX
void ari_copy_states_fx(
    TastatEnc *source,
    TastatEnc *dest )
@@ -33,6 +35,7 @@ void ari_copy_states_fx(

    return;
}
#endif

/*---------------------------------------------------------------
  Ari encoder 14 bits routines
@@ -47,6 +50,7 @@ void ari_copy_states_fx(
 * \return none
 *-------------------------------------------------------------*/

#if 0  //VERSX
void ari_start_encoding_14bits_fx(
    TastatEnc *s )
{
@@ -58,7 +62,7 @@ void ari_start_encoding_14bits_fx(

    return;
}

#endif

/*---------------------------------------------------------------
 * Ari encoder 14 bits routines
@@ -74,11 +78,17 @@ Word16 ari_put_bit_plus_follow(
{
    assert( bit == 0 || bit == 1 );
    ptr[bp++] = bit; /* send initially a zero or one    Q0*/
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("ari_enc_fx.c:1: ptr[%d-1]=%d\n", bp, ptr[bp-1]);
#endif
    move16();
    bit = s_xor( bit, 1 ); /* invert bit to send           */
    FOR( ; bits_to_follow > 0; bits_to_follow-- )
    {
        ptr[bp++] = bit; /* send inverted bit    Q0*/
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("ari_enc_fx.c:2: ptr[%d-1]=%d\n", bp, ptr[bp-1]);
#endif
        move16();
    }

@@ -96,6 +106,7 @@ Word16 ari_put_bit_plus_follow(
 *
 * \return bit consumption
 * ------------------------------------------------------------ - */
#if 0  //VERSX
Word16 ari_done_encoding_14bits_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -111,7 +122,7 @@ Word16 ari_done_encoding_14bits_fx(
    }
    return ari_put_bit_plus_follow( ptr, bp, add( s->value, 1 ), bit );
}

#endif

/*---------------------------------------------------------------
 *ari_encode_14bits_ext_fx()
@@ -127,7 +138,7 @@ Word16 ari_done_encoding_14bits_fx(
 *
 * \return bit consumption
 * ------------------------------------------------------------ - */

#if 0  //VERSX
Word16 ari_encode_14bits_ext_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -203,10 +214,12 @@ Word16 ari_encode_14bits_ext_fx(
    move32();
    s->value = bits_to_follow; /* Q0 */
    move16();

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("ari_encode_14bits_ext_fx(),  bp=%d, symbol=%d\n", bp, symbol );
#endif
    return bp;
}

#endif

/*------------------------------------------------------------------------
 * Function: ari_encode_overflow_fx()
@@ -214,11 +227,13 @@ Word16 ari_encode_14bits_ext_fx(
 *
 *-------------------------------------------------------------------------*/

#if 0  //VERSX
Word16 ari_encode_overflow_fx(
    TastatEnc *s )
{
    return L_sub( L_sub( s->high, 1 ), s->low ) <= 0;
}
#endif

/*------------------------------------------------------------------------
 * Function: ari_encode_14bits_high_low()
@@ -226,6 +241,7 @@ Word16 ari_encode_overflow_fx(
 *
 *-------------------------------------------------------------------------*/

#if 0  //VERSX
static Word16 ari_encode_14bits_high_low(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -240,6 +256,10 @@ static Word16 ari_encode_14bits_high_low(
    bits_to_follow = s->value;
    move16();

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("high_low1: s->low,high,btfo=0x%04x,0x%04x,0x%04x high,low=0x%04x,0x%04x  bp=%d\n",
        s->low, s->high-1, s->value, high,low,bp );
#endif
    /* while there are more than 16 bits left */
    tmp = sub( 16, bits ); /* Q0 */
    WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
@@ -276,6 +296,10 @@ static Word16 ari_encode_14bits_high_low(
        }

        low = L_add( low, low );    /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("high_low2: high=0x%04x, s->btfo=%d\n",
            high, s->value);
#endif
        high = L_add( high, high ); /* Scale up code range.     Q0*/
    }

@@ -288,6 +312,7 @@ static Word16 ari_encode_14bits_high_low(

    return bp;
}
#endif


/*------------------------------------------------------------------------
@@ -296,6 +321,7 @@ static Word16 ari_encode_14bits_high_low(
 * Encode an cumulative frequency interval.
 *-------------------------------------------------------------------------*/

#if 0  //VERSX
Word16 ari_encode_14bits_range_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -311,9 +337,17 @@ Word16 ari_encode_14bits_range_fx(

    high = L_add( s->low, mul_sbc_14bits( range, cum_freq_high ) ); /* Q0 */
    low = L_add( s->low, mul_sbc_14bits( range, cum_freq_low ) );   /* Q0 */

    return ari_encode_14bits_high_low( ptr, bp, bits, s, high, low );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("range, s->low,high=0x%04x,0x%04x   range=0x%04x  low,high=0x%04x,0x%04x\n",
        s->low, s->high, range, low, high );
#endif
    Word16 ret=ari_encode_14bits_high_low( ptr, bp, bits, s, high, low );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("range end ret=%d,  s->low,high,value=x%04x,0x%04x,x%04x\n", ret, s->low,s->high,s->value);
#endif
    return ret;
}
#endif


/*------------------------------------------------------------------------
@@ -322,6 +356,7 @@ Word16 ari_encode_14bits_range_fx(
 * Encode a sign with equal probabilities.
 *-------------------------------------------------------------------------*/

#if 0  //VERSX
Word16 ari_encode_14bits_sign_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -336,7 +371,9 @@ Word16 ari_encode_14bits_sign_fx(
    high = L_add( s->high, 0 );
    low = L_add( s->low, 0 );
    range = L_sub( high, low ); /* Q0 */

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("sign low,high=0x%04x,0x%04x range=0x%04x\n", low, high,range);
#endif
    L_tmp = L_shr( range, 1 );
    if ( sign != 0 )
    {
@@ -347,8 +384,10 @@ Word16 ari_encode_14bits_sign_fx(
        low = L_add( low, L_tmp ); /* Q0 */
    }

    return ari_encode_14bits_high_low( ptr, bp, bits, s, high, low );
    Word16 ret = ari_encode_14bits_high_low( ptr, bp, bits, s, high, low );
    return ret;
}
#endif


/*------------------------------------------------------------------------
@@ -357,6 +396,7 @@ Word16 ari_encode_14bits_sign_fx(
 * Finish up encoding in CBR mode.
 *-------------------------------------------------------------------------*/

#if 0  //VERSX
Word16 ari_done_cbr_encoding_14bits_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -364,11 +404,22 @@ Word16 ari_done_cbr_encoding_14bits_fx(
    TastatEnc *s )
{
    Word16 high, tmp, k;

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
Word16 bp0=bp;
#endif
    tmp = sub( bits, 16 ); /* Q0 */
    WHILE( GT_16( sub( tmp, bp ), s->value ) )
    {
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("%s:%d: before  ari_encode_14bits_sign_fx() bp=%d, bits=%d, s=0x%04x,0x%04x,0x%04x\n",
           __FILE__,__LINE__,
           bp, bits, s->low,s->high,s->value);
#endif
        bp = ari_encode_14bits_sign_fx( ptr, bp, bits, s, 0 ); /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("after ari_encode_14bits_sign_fx() bp=%d, bits=%d, s=0x%04x,0x%04x,0x%04x\n",
           bp, bits, s->low,s->high,s->value);
#endif
    }

    high = extract_l( L_sub( s->high, 1 ) ); /* Q0 */
@@ -377,15 +428,23 @@ Word16 ari_done_cbr_encoding_14bits_fx(
    high = lshl( high, 1 );

    tmp = s_min( 15, sub( bits, bp ) );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("ari_enc_fx.c:3: bp0=%d bp=%d, bits=%d,s=0x%04x,0x%04x,0x%04x, high=%d,tmp=%d,\n",
       bp0, bp, s->low,s->high,s->BIT_TO_FOLLOW, high, tmp);
#endif
    FOR( k = 0; k < tmp; ++k )
    {
        ptr[bp++] = lshr( high, 15 ); /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("ari_enc_fx.c:4: ptr[%d-1]=%d\n", bp, ptr[bp-1]);
#endif
        move16();
        high = lshl( high, 1 );
    }

    return bp;
}
#endif


void ari_start_encoding_14bits_ivas_fx(
@@ -507,7 +566,9 @@ Word16 ari_encode_14bits_ext_ivas_fx(
    move32();
    s->bits_to_follow = bits_to_follow; /* Q0 */
    move32();

#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("ari_encode_14bits_ext_fx(),  bp=%d, symbol=%d\n", bp, symbol );
#endif
    return bp;
}

@@ -517,6 +578,7 @@ Word16 ari_encode_14bits_ext_ivas_fx(
 *
 *-------------------------------------------------------------------------*/

#ifndef  FUNCTION_ari_encode_14bits_high_low_ivas_fx
static Word16 ari_encode_14bits_high_low_ivas_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
@@ -528,8 +590,13 @@ static Word16 ari_encode_14bits_high_low_ivas_fx(
{
    Word16 bits_to_follow, tmp;

    //high--;
    bits_to_follow = extract_l( s->bits_to_follow ); /* Q0 */
    move16();
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("high_low1: s->low,high,btfo=0x%04x,0x%04x,0x%04x high,low=0x%04x,0x%04x  bp=%d\n",
        s->low, s->high, s->bits_to_follow, high,low,bp );
#endif

    /* while there are more than 16 bits left */
    tmp = sub( 16, bits ); /* Q0 */
@@ -567,11 +634,16 @@ static Word16 ari_encode_14bits_high_low_ivas_fx(
        }

        low = L_add( low, low );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
	printf("high_low2: high=0x%04x, s->btfo=%d\n",
            high, s->bits_to_follow);
#endif
        high = L_add( high, high ); /* Scale up code range.     */
    }

    s->low = low; /* Q0 */
    move32();
    //high++;
    s->high = L_sub( high, 1 ); /* Q0 */
    move32();
    s->bits_to_follow = bits_to_follow; /* Q0 */
@@ -579,7 +651,7 @@ static Word16 ari_encode_14bits_high_low_ivas_fx(

    return bp;
}

#endif

/*------------------------------------------------------------------------
 * Function: ari_encode_14bits_sign_ivas_fx()
@@ -601,6 +673,9 @@ Word16 ari_encode_14bits_sign_ivas_fx(
    high = L_add( s->high, 1 );
    low = L_add( s->low, 0 );
    range = L_sub( high, low ); /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("sign low,high=0x%04x,0x%04x range=0x%04x\n", low, high,range);
#endif

    L_tmp = L_shr( range, 1 );
    if ( sign != 0 )
@@ -612,5 +687,6 @@ Word16 ari_encode_14bits_sign_ivas_fx(
        low = L_add( low, L_tmp ); /* Q0 */
    }

    return ari_encode_14bits_high_low_ivas_fx( ptr, bp, extract_l( bits ), s, high, low );
    Word16 ret = ari_encode_14bits_high_low_ivas_fx( ptr, bp, extract_l( bits ), s, high, low );
    return ret;
}
+28 −2
Original line number Diff line number Diff line
@@ -11,6 +11,14 @@
#include "basop_util.h"
#include "rom_com.h"

#if 1  //VERSX
#include "issue2402_replacement.h"
#else
#define HIGH(p) (p)
#define BITS_TO_FOLLOW value
#endif



/*-------------------------------------------------------------------*
 * Local constants
@@ -551,6 +559,7 @@ static Word16 tcx_arith_encode(
        IF( q_abs_spectrum[k] == 0 )
        {
            assert( exps[k] >= 2 );
            //printf("%s:%d: before  ari_encode_14bits_range_fx()\n", __FILE__,__LINE__);
            bp = ari_encode_14bits_range_fx( prm, bp, target_bits, &as, shr( exps[k], 1 ), 16384 );
        }
        ELSE /* q_abs_spectrum[k] != 0 */
@@ -564,7 +573,9 @@ static Word16 tcx_arith_encode(
                powfp_odd2( exps[k], q_abs_spectrum[k], &tmpi1, &tmpi2 );
            }

            //printf("%s:%d: before  ari_encode_14bits_range_fx()\n", __FILE__,__LINE__);
            bp = ari_encode_14bits_range_fx( prm, bp, target_bits, &as, shr( tmpi2, 1 ), shr( tmpi1, 1 ) );
            //printf("%s:%d: before  ari_encode_14bits_sign_fx()\n", __FILE__,__LINE__);
            bp = ari_encode_14bits_sign_fx( prm, bp, target_bits, &as, signs[k] );
        }

@@ -587,7 +598,9 @@ static Word16 tcx_arith_encode(
                /* Retry encoding */
                powfp_odd2( exps[k], q_abs_spectrum[k], &tmpi1, &tmpi2 );

                //printf("%s:%d: before  ari_encode_14bits_range_fx()\n", __FILE__,__LINE__);
                bp = ari_encode_14bits_range_fx( prm, bp, target_bits, &as, shr( tmpi2, 1 ), shr( tmpi1, 1 ) );
                //printf("%s:%d: before  ari_encode_14bits_sign_fx()\n", __FILE__,__LINE__);
                bp = ari_encode_14bits_sign_fx( prm, bp, target_bits, &as, signs[k] );

                IF( !ari_encode_overflow_fx( &as ) ) /* Success */
@@ -625,6 +638,7 @@ static Word16 tcx_arith_encode(
    {
        assert( exps[k] >= 1 );

        //printf("%s:%d: before  ari_encode_14bits_range_fx()\n", __FILE__,__LINE__);
        bp = ari_encode_14bits_range_fx( prm, bp, target_bits, &as, shr( exps[k], 1 ), 16384 );
        /* Check bit budget status */
        IF( ari_encode_overflow_fx( &as ) ) /* no bits left */
@@ -646,10 +660,20 @@ static Word16 tcx_arith_encode(
    IF( EQ_16( kEncoded, sub( L_frame, 1 ) ) ) /* RESQ bits possibly available */
    {
        /* Limit target bits to actually needed bits */
        target_bits = add( add( bp, 16 ), extract_l( as.value ) );
        target_bits = add( add( bp, 16 ), extract_l( as.BITS_TO_FOLLOW ) );
    }

    return ari_done_cbr_encoding_14bits_fx( prm, bp, target_bits, &as );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("%s:%d: before ari_done_cbr_encoding_14bits_fx()i bp=%d target_bits=%d as=0x%04x,0x%04x,0x%04x\n",
       __FILE__,__LINE__,
      bp, target_bits, as.low,HIGH(as.high),as.BITS_TO_FOLLOW );
#endif
    Word16 ret = ari_done_cbr_encoding_14bits_fx( prm, bp, target_bits, &as );
#if 0
    printf("after ari_done_cbr_encoding_14bits_fx() ret=%d, as=0x%04x,0x%04x,0x%04x\n",
       ret,  as.low,HIGH(as.high),as.BITS_TO_FOLLOW);
#endif
    return ret;
}


@@ -774,6 +798,7 @@ void tcx_arith_encode_envelope_fx(
    }

    /* Final encoding */
    //printf("%s:%d: before tcx_arith_encode()\n", __FILE__,__LINE__);
    *arith_bits = tcx_arith_encode( q_spectrum, signs, kMax, L_spec, exponents, target_bits, prm );
    move16();

@@ -915,6 +940,7 @@ void tcx_arith_encode_envelope_ivas_fx(
    }

    /* Final encoding */
    //printf("%s:%d: before tcx_arith_encode()\n", __FILE__,__LINE__);
    *arith_bits = tcx_arith_encode( q_spectrum, signs, kMax, L_spec, exponents, target_bits, prm );
    move16();

+5 −0
Original line number Diff line number Diff line
@@ -13,6 +13,11 @@
#include "prot_fx_enc.h" /* Function prototypes                    */


#if 1  //VERSX
#include "issue2402_replacement.h"
#endif


/**********************************************************************/ /**
initialization of an instance of this module
**************************************************************************/
+259 −0
Original line number Diff line number Diff line

//#define DEBUG_ARITH_IVAS_REPLACEMENT
#define ari_start_encoding_14bits_fx(p)          ari_start_encoding_14bits_ivas_fx(p)
#define ari_done_encoding_14bits_fx(p1, p2, p3)  ari_done_encoding_14bits_ivas_fx(p1, p2, p3)
#define TastatEnc Tastat
#define ari_copy_states_fx(s,d)                  ari_copy_states_ivas_fx(s,d)
#define ari_encode_14bits_ext_fx( ptr, bp, as, p1, p2 )  ari_encode_14bits_ext_ivas_fx( ptr, bp, as, p1, p2 )
#define ari_done_cbr_encoding_14bits_fx  ari_done_cbr_encoding_14bits_ivas_fx
#define ari_encode_overflow_fx ari_encode_overflow_ivas_fx
#define  ari_encode_14bits_sign_fx( ptr, bp, bits, s, sign )  ari_encode_14bits_sign_ivas_fx( ptr, bp, bits, s, sign )
#define ari_encode_14bits_range_fx ari_encode_14bits_range_ivas_fx
#define  ari_encode_14bits_high_low ari_encode_14bits_high_low_ivas_fx
//#define TastatEnc Tastat
#define acState_fx   acState_ivas_fx
#define HIGH(p) ((p)+1)
#define BITS_TO_FOLLOW bits_to_follow

/*---------------------------------------------------------------
 * ari_copy_states_ivas_fx()
 *
 * \param[i] source
 * \param[o] dest
 *
 * \return none
 *-------------------------------------------------------------*/
static void ari_copy_states_ivas_fx(
    Tastat *source,
    Tastat *dest )
{
    dest->low = source->low;
    move32();
    dest->high = source->high;
    move32();
    dest->value = source->value;
    move16();
    dest->bits_to_follow = source->bits_to_follow;
    move16();

    return;
}

static Word16 ari_done_cbr_encoding_14bits_ivas_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
    Word16 bits, /* Q0 */
    Tastat *s )
{
    Word16 high, tmp, k;
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
Word16 bp0=bp;
#endif
    tmp = sub( bits, 16 ); /* Q0 */
    WHILE( GT_16( sub( tmp, bp ), s->bits_to_follow ) )
    {
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("%s:%d: before  ari_encode_14bits_sign_fx() bp=%d, bits=%d, s=0x%04x,0x%04x,0x%04x\n",
           __FILE__,__LINE__,
           bp, bits, s->low,s->high,s->bits_to_follow);
#endif
        bp = ari_encode_14bits_sign_ivas_fx( ptr, bp, bits, s, 0 ); /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("after ari_encode_14bits_sign_fx() bp=%d, bits=%d, s=0x%04x,0x%04x,0x%04x\n",
           bp, bits, s->low,s->high,s->value);
#endif
    }

    high = extract_l( L_sub( s->high, 0 ) ); /* Q0 */

    bp = ari_put_bit_plus_follow( ptr, bp, s->bits_to_follow, lshr( high, 15 ) ); /* Q0 */
    high = lshl( high, 1 );

    tmp = s_min( 15, sub( bits, bp ) );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("ari_enc_fx.c:3: bp0=%d bp=%d, bits=%d,s=0x%04x,0x%04x,0x%04x, high=%d,tmp=%d,\n",
       bp0, bp, s->low,s->high,s->BITS_TO_FOLLOW, high, tmp);
#endif
    FOR( k = 0; k < tmp; ++k )
    {
        ptr[bp++] = lshr( high, 15 ); /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("ari_enc_fx.c:4: ptr[%d-1]=%d\n", bp, ptr[bp-1]);
#endif
        move16();
        high = lshl( high, 1 );
    }

    return bp;
}

static Word16 ari_encode_overflow_ivas_fx(
    Tastat *s )
{
    return L_sub( L_sub( s->high, 0 ), s->low ) <= 0;
}

#if 0
static Word16 ari_encode_14bits_high_low_ivas(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
    Word16 bits, /* Q0 */
    Tastat *s,
    Word32 high, /* Q0 */
    Word32 low   /* Q0 */
)
{
    Word16 bits_to_follow, tmp;

    bits_to_follow = s->bits_to_follow;
    move16();

    /* while there are more than 16 bits left */
    tmp = sub( 16, bits ); /* Q0 */
    WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
    {
        IF( LE_32( high, ari_q2new ) )
        {
            bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
            bits_to_follow = 0;
            move16();
        }
        ELSE IF( GE_32( low, ari_q2new ) )
        {
            bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
            bits_to_follow = 0;
            move16();
            low = L_sub( low, ari_q2new );   /* Q0 */
            high = L_sub( high, ari_q2new ); /* Subtract offset to top.  Q0*/
        }
        ELSE
        {
            test();
            IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
            {
                /* Output an opposite bit   */
                /* later if in middle half. */
                bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
                low = L_sub( low, ari_q1new );             /* Subtract offset to middle    Q0*/
                high = L_sub( high, ari_q1new );           /* Q0 */
            }
            ELSE
            {
                BREAK; /* Otherwise exit loop.     */
            }
        }

        low = L_add( low, low );    /* Q0 */
        high = L_add( high, high ); /* Scale up code range.     Q0*/
    }

    s->low = low; /* Q0 */
    move32();
    s->high = high; /* Q0 */
    move32();
    s->bits_to_follow = bits_to_follow; /* Q0 */
    move16();

    return bp;
}
#endif

#define FUNCTION_ari_encode_14bits_high_low_ivas_fx
static Word16 ari_encode_14bits_high_low_ivas_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
    Word16 bits, /* Q0 */
    Tastat *s,
    Word32 high, /* Q0 */
    Word32 low   /* Q0 */
)
{
    Word16 bits_to_follow, tmp;

    bits_to_follow = extract_l( s->bits_to_follow ); /* Q0 */
    move16();
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("high_low1: s->low,high,btfo=0x%04x,0x%04x,0x%04x high,low=0x%04x,0x%04x  bp=%d\n",
        s->low, s->high, s->bits_to_follow, high,low,bp );
#endif

    /* while there are more than 16 bits left */
    tmp = sub( 16, bits ); /* Q0 */
    WHILE( add( add( bp, bits_to_follow ), tmp ) < 0 )
    {
        IF( LE_32( high, ari_q2new ) )
        {
            bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 0 ); /* Q0 */
            bits_to_follow = 0;
            move16();
        }
        ELSE IF( GE_32( low, ari_q2new ) )
        {
            bp = ari_put_bit_plus_follow( ptr, bp, bits_to_follow, 1 ); /* Q0 */
            bits_to_follow = 0;
            move16();
            low = L_sub( low, ari_q2new );   /* Q0 */
            high = L_sub( high, ari_q2new ); /* Subtract offset to top.  Q0*/
        }
        ELSE
        {
            test();
            IF( GE_32( low, ari_q1new ) && LE_32( high, ari_q3new ) )
            {
                /* Output an opposite bit   */
                /* later if in middle half. */
                bits_to_follow = add( bits_to_follow, 1 ); /* Q0 */
                low = L_sub( low, ari_q1new );             /* Subtract offset to middle    Q0*/
                high = L_sub( high, ari_q1new );           /* Q0 */
            }
            ELSE
            {
                BREAK; /* Otherwise exit loop.     */
            }
        }

        low = L_add( low, low );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
        printf("high_low2: high=0x%04x, s->btfo=%d\n",
            high, s->bits_to_follow);
#endif
        high = L_add( high, high ); /* Scale up code range.     */
    }

    s->low = low; /* Q0 */
    move32();
    s->high = L_sub( high, 1 ); /* Q0 */
    move32();
    s->bits_to_follow = bits_to_follow; /* Q0 */
    move16();

    return bp;
}

static Word16 ari_encode_14bits_range_ivas_fx(
    Word16 *ptr, /* Q0 */
    Word16 bp,   /* Q0 */
    Word16 bits, /* Q0 */
    Tastat *s,
    Word16 cum_freq_low, /* Q0 */
    Word16 cum_freq_high /* Q0 */
)
{
    Word32 low, high, range;

    range = L_sub( s->high, s->low ) + 1; /* Q0 */

    high = L_add( s->low, mul_sbc_14bits( range, cum_freq_high ) ); /* Q0 */
    low = L_add( s->low, mul_sbc_14bits( range, cum_freq_low ) );   /* Q0 */
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("range, s->low,high=0x%04x,0x%04x   range=0x%04x  low,high=0x%04x,0x%04x\n",
        s->low, s->high+1, range, low, high );
#endif

    Word16 ret = ari_encode_14bits_high_low_ivas_fx( ptr, bp, bits, s, high, low );
#ifdef DEBUG_ARITH_IVAS_REPLACEMENT
    printf("range end ret=%d,  s->low,high,value=x%04x,0x%04x,x%04x\n", ret, s->low,s->high+1,s->bits_to_follow);
#endif
    return ret;
}
Loading