/*
***********************************************************************
*
* 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:  Prm2bits_6k7
 *
 *  PURPOSE:  converts the encoder parameter vector into a vector of serial
 *            bits.
 *
 *  DESCRIPTION: The encoder parameters are:
 *
 *     BFI      bad frame indicator      1 bit
 *
 *     FRTYPE   frame type               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 */

void Int2bin_lsb(
    Word16 value,       /* (i) : value to be converted to binary      */
    Word16 no_of_bits,  /* (i) : number of bits associated with value */
    Word16 * bitstream  /* (o) : address where bits are written       */
);

#define SYNC_WORD 0
#define BIT_0     0
#define BIT_1     1
#define MASK      0x0001
#define PRM_NO    21    /* 19 speech parameters, 1 BFI, 1 frame type */

void Prm2bits_6k7(
    Word16 prm[],  /* (i) : analysis parameters (19+2 parameters)    */
    Word16 bits[]  /* (o) : serial bits (1*bfi + 2*frame type + 134) */
)
{
    Word16 i;

    static Word16 bitno[PRM_NO] =
    {                           /* bit rate mode: 6.7 kbit/s */
        1,                      /* BFI (no TX      */
        2,                      /* frame type      */
        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++) {
        Int2bin_lsb(prm[i], bitno[i], bits);
        bits += bitno[i];
    }

    return;
}

/*************************************************************************
 *
 *  FUNCTION:  Int2bin_lsb
 *
 *  PURPOSE:  convert integer to binary and write the bits to the array
 *            bitstream[]. The least significant bits are written first.
 *           
 *************************************************************************/

void Int2bin_lsb (
    Word16 value,       /* input : value to be converted to binary      */
    Word16 no_of_bits,  /* input : number of bits associated with value */
    Word16 *bitstream   /* output: address where bits are written       */
)
{
    Word16 *pt_bitstream, i, bit;
    /*    fprintf(stderr, "\nvalue, nbits= %d,%d",value,no_of_bits); */

    pt_bitstream = &bitstream[0];

    for (i = 0; i < no_of_bits; i++)
    {
        bit = value & MASK;
        if (bit == 0)
        {
            *pt_bitstream++ = BIT_0;
        }
        else
        {
            *pt_bitstream++ = BIT_1;
        }
        value = shr (value, 1);
    }
}
