/*
***********************************************************************
*
* Copyright (C) 1999 Ericsson Radio Systems AB
* 
* The Association of Radio Industries and Businesses (ARIB) has the
* licence to reproduce, distribute and publish the Bit Exact C Code
* programs in this CD-ROM together with the other parts of the RCR
* STD-27 (Personal Digital Cellular Telecommunication System)
* specification.
* 
* Ericsson has declared Option 1 (royalty-free licence) to ARIB for
* its Essential Industrial Property Rights related to the ACELP
* specification and the Bit Exact C Code programs. The royalty-free
* licence is limited to the development and manufacturing of
* products complying with the RCR STD-27H specification (or
* subsequent versions) for sale and use in Japan. Copying of the
* program(s) for any other purpose requires the prior written
* permission from Ericsson.
* 
* The user of the Bit Exact C Code hereby acknowledges the
* following:
* 
* 1.  Ericsson does not provide the user with any warranties,
*     whether express or implied, including but not limited to
*     warranties of merchantability and fitness for a particular
*     purpose relating to the Bit Exact C Code.
* 
* 2.  Ericsson shall have no responsibility whatsoever to indemnify
*     the user against actions or claims for infringement of any
*     intellectual property rights, e.g. patents, copyrights,
*     registered designs or any other rights, by reason of the
*     user's use of the Bit Exact C Code.
* 
***********************************************************************
*/

/*************************************************************************
 *   FUNCTION:   Dec_lag3
 *
 *   PURPOSE:  Decoding of fractional pitch lag with 1/3 resolution.
 *             Extract the integer and fraction parts of the pitch lag from
 *             the received adaptive codebook index.
 *
 *    See "Enc_lag3.c" for more details about the encoding procedure.
 *
 *    The fractional lag in 1st and 3rd subframes is encoded with 8 bits
 *    while that in 2nd and 4th subframes is relatively encoded with 5 bits.
 *
 *************************************************************************/

#include "typedef.h"
#include "basic_op.h"

void Dec_lag3(
    Word16 index,    /* input : received pitch index           */
    Word16 pit_min,  /* input : minimum pitch lag              */
    Word16 pit_max,  /* input : maximum pitch lag              */
    Word16 i_subfr,  /* input : subframe flag                  */
    Word16 * T0,     /* output: integer part of pitch lag      */
    Word16 * T0_frac /* output: fractional part of pitch lag   */
)
{
    Word16 i;
    Word16 T0_min, T0_max;
    const Word16 modtab[]={1, 2, 0, 1, 2, 0, 1, 2};

    if (i_subfr == 0) {    /* if 1st or 3rd subframe */
        if (sub(index, 197) < 0) {

            *T0 = add(mult(add(index, 2), 10923), 19);


            i = add(add(*T0, *T0), *T0);
            *T0_frac = add(sub(index, i), 58);
        } else {
            *T0 = sub(index, 112);
            *T0_frac = 0;
        }

    } else {    /* 2nd or 4th subframe */

        T0_min = sub(*T0, 5);
        if (sub(T0_min, pit_min) < 0) {
            T0_min = pit_min;
        }
        T0_max = add(T0_min, 9);
        if (sub(T0_max, pit_max) > 0) {
            T0_max = pit_max;
            T0_min = sub(T0_max, 9);
        }

        index = index & (Word16) 15; /* 4 bits delta lag; assure only 4 bits used */
        if (sub(index, 3) <= 0) {
           *T0 = add(T0_min, index);
           *T0_frac = 0;
        }
        else if (sub(index, 12) < 0) {
           /* *T0_frac = index % 3; */
           *T0_frac = modtab[sub(index, 4)];
           /* *T0 = (index - *T0_frac) / 3 + T0_min + 2; */
           i = sub(index, *T0_frac);
           i = mult(i, 10923);
           i = add(i, T0_min);
           *T0 = add(i, 2);

           if (sub(*T0_frac, 2) == 0) {
              *T0_frac = -1;
              *T0 = add(*T0, 1);
           }
        }
        else {
           /* *T0 = T0_min + 6 + index - 12; */
           *T0 = add(T0_min, sub(index, 6));
           *T0_frac = 0;
        }
    }

    return;
}
