/*************************************************************************
 *
 *   FUNCTIONS:  Lsp_lsf and Lsf_lsp
 *
 *   PURPOSE:
 *      Lsf_lsp:   Transformation lsf to lsp
 *
 *   DESCRIPTION:
 *         lsp[i] = cos(2*pi*lsf[i]) and lsf[i] = arccos(lsp[i])/(2*pi)
 *
 *   The transformation from lsp[i] to lsf[i] and lsf[i] to lsp[i] are
 *   approximated by a look-up table and interpolation.
 *
 *************************************************************************/

#include "typedef.h"

#include "lsp_lsf.tab"          /* Look-up table for transformations */

typedef unsigned char u8;

void Lsf_lsp (
    Word16 lsf[],       /* (i) : lsf[m] normalized (range: 0.0<=val<=0.5) */
    Word16 lsp[],       /* (o) : lsp[m] (range: -1<=val<1)                */
    Word16 m            /* (i) : LPC order                                */
)
{
    Word16 i, ind, offset, t1, t2;
    Word32 L_tmp;

		u8 *ptr = (u8 *)lsf;

    for (i = 0; i < m; i++)
    {
#if 0
        ind = lsf[i] >> 8;      /* ind    = b8-b15 of lsf[i] */
        offset = lsf[i] & 0x00ff; /* offset = b0-b7  of lsf[i] */

        /* lsp[i] = table[ind]+ ((table[ind+1]-table[ind])*offset) / 256 */

        L_tmp = (Word32)(table[ind + 1] - table[ind]) * (Word32)offset;
        lsp[i] = table[ind] + (Word16)(L_tmp >> 8);
#else
				ind = ptr[0];
				offset = ptr[1];

				t1 = table[ind];
				t2 = table[ind+1];

				ptr += 2;

        /* lsp[i] = table[ind]+ ((table[ind+1]-table[ind])*offset) / 256 */

        L_tmp = (Word32)(t2 - t1) * (Word32)offset;
        lsp[i] = t1 + (Word16)(L_tmp >> 8);
#endif
    }
}
