/*
***********************************************************************
*
* 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:  Bits2prm_6k7
 *
 *  PURPOSE: Retrieves the vector of encoder parameters from the received
 *           serial bits in a frame.
 *
 *  DESCRIPTION: The decoded parameters are:
 *
 *     BFI      bad frame indicator      1 bit
 *
 *     DUMMY    dummy bits (not used)    2 bit
 *
 *     LPC:
 *              1st codebook             8 bit
 *              2nd codebook             9 bit
 *              3rd codebook             9 bit
 *
 *     1st and 3rd subframes:
 *           pitch period                8 bit
 *           codebook index              14 bit
 *           pitch&codebook gain         7 bit
 *
 *     2nd and 4th subframes:
 *           pitch period                4 bit
 *           codebook index              14 bit
 *           pitch&codebook gain         7 bit
 *
 *************************************************************************/


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

/* Local function */

Word16 Bin2int_lsb(         /* Reconstructed parameter                      */
    Word16 no_of_bits,  /* (i) : number of bits associated with value   */
    Word16 * bitstream  /* (o) : address where bits are written         */
);

#define BIT_0     0
#define BIT_1     1
#define PRM_NO    21

void Bits2prm_6k7(
    Word16 bits[],      /* (i) : serial bits (134)                    */
    Word16 prm[]        /* (o) : analysis parameters  (21 parameters) */
)
{
    Word16 i;

    static Word16 bitno[PRM_NO] =
    {                       /* bit rate mode: 6.7 kbit/s */
       1,                  /* BFI             */
       2,                  /* dummy bits      */
       8, 9, 9,            /* LSP VQ          */
       8, 11, 3, 7,        /* first subframe  */
       4, 11, 3, 7,        /* second subframe */
       8, 11, 3, 7,        /* third subframe  */
       4, 11, 3, 7};       /* fourth subframe */

    for (i = 0; i < PRM_NO; i++) {
       prm[i] = Bin2int_lsb(bitno[i], bits);
       bits += bitno[i];
    }

    return;
}

/*
**************************************************************************
*
*  Function    : Bin2int_lsb
*  Purpose     : Read "no_of_bits" bits from the array bitstream[] 
*                and convert to integer, LSBss are first in stream.
*
**************************************************************************
*/
Word16 Bin2int_lsb (        /* Reconstructed parameter                  */
    Word16 no_of_bits,  /* input : number of bits associated with value */
    Word16 *bitstream   /* output: address where bits are written       */
)
{
    Word16 value, i, bit;
    Word16 *pt_bitstream;

    /* point past end  of parameter position */ 
    pt_bitstream = &bitstream[no_of_bits];

    value = 0;
    for (i = 0; i < no_of_bits; i++)
    {
        value = shl (value, 1);
        bit = *--pt_bitstream;
        if (sub (bit, BIT_1) == 0)
            value = add (value, 1);
    }
    return (value);
}
