/*
***********************************************************************
*
* 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:   decod_ACELP()
 *
 *  PURPOSE:   Algebraic codebook decoder
 *
 *************************************************************************/


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

void Decod_ACELP(
    Word16 sign,   /* (i)  : signs of 3 pulses.                       */
    Word16 index,  /* (i)  : Positions of the 3 pulses.               */
    Word16 cod[]   /* (o)  : algebraic (fixed) codebook excitation    */
)
{
    Word16 i, j;
    Word16 pos[3];

    /* Decode the positions */

    i = index & 7;

    pos[0] = add(i, shl(i, 2));    /* pos0 =i*5 */

    index = shr(index, 3);
    j = index & 1;
    index = shr(index, 1);
    i = index & 7;

    i = add(i, shl(i, 2));        /* pos1 =i*5+1+j*2 */
    i = add(i, 1);
    j = shl(j, 1);     
    pos[1] = add(i, j);

    index = shr(index, 3);
    j = index & 1;
    index = shr(index, 1);
    i = index & 7;

    i = add(i, shl(i, 2));        /* pos2 =i*5+2+j*2 */
    i = add(i, 2); 
    j = shl(j, 1); 
    pos[2] = add(i, j);

    /* decode the signs  and build the codeword */

    for (i = 0; i < L_SUBFR; i++) {
        cod[i] = 0;
    }

    for (j = 0; j < 3; j++) {

        i = sign & 1;
        sign = shr(sign, 1);

        if (i != 0) {
            cod[pos[j]] = 8191;
        } else {
            cod[pos[j]] = -8192;
        }
    }

    return;
}
