/*************************************************************************
 *   FUNCTION:   Dec_lag6
 *
 *   PURPOSE:  Decoding of fractional pitch lag with 1/6 resolution.
 *             Extract the integer and fraction parts of the pitch lag from
 *             the received adaptive codebook index.
 *
 *    See "Enc_lag6.c" for more details about the encoding procedure.
 *
 *    The fractional lag in 1st and 3rd subframes is encoded with 9 bits
 *    while that in 2nd and 4th subframes is relatively encoded with 6 bits.
 *    Note that in relative encoding only 61 values are used. If the
 *    decoder receives 61, 62, or 63 as the relative pitch index, it means
 *    that a transmission error occurred. In this case, the pitch lag from
 *    previous subframe is used.
 *
 *************************************************************************/

#include "typedef.h"
#include "cnst.h"


/* Old integer lag */

Word16 old_T0;

Word16 
Dec_lag6 (             /* output: return integer pitch lag       */
    Word16 index,      /* input : received pitch index           */
    Word16 i_subfr,    /* input : subframe flag                  */
    Word16 *T0_frac    /* output: fractional part of pitch lag   */
)
{
    Word16 pit_flag;
    Word16 T0, i;
    static Word16 T0_min, T0_max;

    pit_flag = i_subfr; /* flag for 1st or 3rd subframe */
    if (i_subfr == L_FRAME_BY2)
    {
        pit_flag = 0;
    }
    if (pit_flag == 0)          /* if 1st or 3rd subframe */
    {
        if (index < 463)
        {
#if 0
            /* T0 = (index+5)/6 + 17 */
            T0 = add (mult (add (index, 5), 5462), 17);
            i = add (add (T0, T0), T0);
            /* *T0_frac = index - T0*6 + 105 */
            *T0_frac = add (sub (index, add (i, i)), 105);
#else
            /* T0 = (index+5)/6 + 17 */
            T0 = (Word16)((((Word32)(index+5) * (Word32)5462) >> 15) + 17);
            i = (T0 * 6);
            /* *T0_frac = index - T0*6 + 105 */
            *T0_frac = index - i + 105;
#endif
        }
        else
        {
            T0 = index - 368;
            *T0_frac = 0;
        }

        /* find T0_min and T0_max for 2nd (or 4th) subframe */

        T0_min = T0 - 5;
        if (T0_min < PIT_MIN)
        {
            T0_min = PIT_MIN;
        }
        T0_max = T0_min + 9;
        if (T0_max > PIT_MAX)
        {
            T0_max = PIT_MAX;
            T0_min = T0_max - 9;
        }
    }
    else
        /* second or fourth subframe */
    {
        /* decode pitch */
        if (index < 61)
        {
#if 0
            /* i = (index+5)/6 - 1 */
            i = sub (mult (add (index, 5), 5462), 1);
            T0 = add (i, T0_min);
            i = add (add (i, i), i);
            *T0_frac = sub (sub (index, 3), add (i, i));
#else
            /* i = (index+5)/6 - 1 */
            i = (Word16)((((Word32)(index+5) * 5462) >> 15) - 1);
            T0 = (i + T0_min);
            *T0_frac = (index-3) - (i*6);
#endif
        }
        else
        /* index >= 61 */
        {
            T0 = old_T0;
            *T0_frac = 0;
        }
    }

    old_T0 = T0;

    return T0;
}
