/***************************************************************************
 *
 *  FILE NAME:  CODER.C
 *
 *  Main program of the EFR coder at 12.2 kbit/s.
 *
 *    Usage : coder speech_file  bitstream_file
 *
 *    Format for speech_file:
 *      Speech is read from a binary file of 16 bits data.
 *
 *    Format for bitstream_file:
 *      244  words (2-byte) containing 244 bits.
 *          Bit 0 = 0x0000 and Bit 1 = 0x0001
 *      One word (2-byte) for voice activity decision (VAD) flag bit
 *          0x0000 -> inactive (no detected speech activity);
 *          0x0001 -> active
 *      One word (2-byte) for speech (SP) flag bit
 *          0x0000 -> inactive (no transmission of speech frames);
 *          0x0001 -> active
 *
 ***************************************************************************/

#include <string.h>

#include "typedef.h"
#include "basic_op.h"
#include "sig_proc.h"
#include "codec.h"
#include "cnst.h"
#include "e_homing.h"

#include "dtx.h"

Word16 dtx_mode;
extern Word16 txdtx_ctrl;

/* L_FRAME, M, PRM_SIZE, AZ_SIZE, SERIAL_SIZE: defined in "cnst.h" */

void Encode( Word16 *in, Word32 size, Word16 *out )
{
    extern Word16 *new_speech;  /* Pointer to new speech data            */

    Word16 prm[PRM_SIZE];       /* Analysis parameters.                  */
    Word16 serial[SERIAL_SIZE]; /* Output bitstream buffer               */
    Word16 syn[L_FRAME];        /* Buffer for synthesis speech           */

    Word16 frame;

    Word16 vad, sp;

    Word16 reset_flag;
    Word16 i;

		Word32 part;

    /*----------------------------------------------------------------------*
     * Open speech file and result file (output serial bit stream)          *
     *----------------------------------------------------------------------*/

    dtx_mode = 0;               /* DTX disabled by default */

    /*-----------------------------------------------------------------------*
     * Initialisation of the coder.                                          *
     *-----------------------------------------------------------------------*/

    reset_enc (); /* Bring the encoder, VAD and DTX to the initial state */

    /* Loop for each "L_FRAME" speech data. */

    frame = 0;
    while (size > 0)
    {
				memset( new_speech, 0, L_FRAME*2 );

				part = size;
				if (part > L_FRAME)
					part = L_FRAME;
				memcpy( new_speech, in, part*2 );
				in += part;
				size -= part;

        /* Check whether this frame is an encoder homing frame */
        reset_flag = encoder_homing_frame_test (new_speech);

				for (i = 0; i < L_FRAME; i++)   /* Delete the 3 LSBs (13-bit input) */
				{
						new_speech[i] = new_speech[i] & 0xfff8;
				}

        Pre_Process (new_speech, L_FRAME);           /* filter + downscaling */

        Coder_12k2 (prm, syn);  /* Find speech parameters   */

        if ((txdtx_ctrl & TX_SP_FLAG) == 0)
        {
            /* Write comfort noise parameters into the parameter frame.
            Use old parameters in case SID frame is not to be updated */
            CN_encoding (prm, txdtx_ctrl);
        }
        Prm2bits_12k2 (prm, &serial[0]); /* Parameters to serial bits */

        if ((txdtx_ctrl & TX_SP_FLAG) == 0)
        {
            /* Insert SID codeword into the serial parameter frame */
            sid_codeword_encoding (&serial[0]);
        }

        /* Write the bit stream to file */
				memcpy( out, serial, SERIAL_SIZE*2 );
				out += SERIAL_SIZE;

        /* Write the VAD- and SP-flags to file after the speech
        parameter bit stream */
        vad = 0;
        sp = 0;

        if ((txdtx_ctrl & TX_VAD_FLAG) != 0)
        {
            vad = 1;
        }
        if ((txdtx_ctrl & TX_SP_FLAG) != 0)
        {
            sp = 1;
        }

        if (reset_flag != 0)
        {
            reset_enc (); /*Bring the encoder, VAD and DTX to the home state */
        }
    }
}
