/*
***********************************************************************
*
* 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.
* 
***********************************************************************
*/

/***************************************************************************
 *
 *  FILE NAME:  decoder.c
 *
 *         Usage : decoder bitstream_file synth_file
 *
 *         Format for bitstream_file:
 *           One word (2-byte) for bad frame indication (BFI) flag bit
 *               0x0000 -> good frame;  0x0001 -> bad frame
 *           Two dummy words (2-byte) not used (positions used for
 *               VOX in a Base Station speech decoder)
 *           134 words (2-byte) containing 134 bits.
 *               Bit 0 = 0x0000 and Bit 1 = 0x0001
 *
 *         Format for synth_file:
 *           Synthesis is written to a binary file of 16 bits data.
 *
 ***************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "typedef.h"
#include "basic_op.h"
#include "sig_proc.h"
#include "codec.h"
#include "cnst.h"
#if defined(__BORLANDC__)
#include "n_stack.h"
#endif


/*-----------------------------------------------------------------*
 *            Main decoder routine                                 *
 *-----------------------------------------------------------------*/

int main(int argc, char *argv[])
{
    Word16 synth_buf[L_FRAME + M], *synth;    /* Synthesis                  */
    Word16 parm[PRM_SIZE];                    /* Synthesis parameters       */
    Word16 serial[SERIAL_SIZE];               /* Serial stream              */
    Word16 Az_dec[AZ_SIZE];                   /* Decoded Az for post-filter */
                                              /* in 4 subframes, length= 44 */
    Word16 i, frame;
    FILE *f_syn, *f_serial;
    Word16 bfi=0;

    proc_head("Speech Decoder");

/*-----------------------------------------------------------------*
 *           Read passed arguments and open in/out files           *
 *-----------------------------------------------------------------*/

    if (!(argc == 3)) {
        fprintf(stderr, 
                "Usage : decoder  bitstream_file  synth_file \n");
        fprintf(stderr, "\n");
        exit(1);
    }
    /* Open file for synthesis and packed serial stream */

    if ((f_serial = fopen(argv[1], "rb")) == NULL) {
        fprintf(stderr, "Input file '%s' does not exist !!\n", argv[1]);
        exit(0);
    } else
        fprintf(stderr, " Input bitstream file :   %s\n", argv[1]);

    if ((f_syn = fopen(argv[2], "wb")) == NULL) {
        fprintf(stderr, "Cannot open file '%s' !!\n", argv[2]);
        exit(0);
    } else
        fprintf(stderr, " Synthesis speech file:   %s\n", argv[2]);

    fprintf(stderr, "\n");

/*-----------------------------------------------------------------*
 *           Initialization of decoder                             *
 *-----------------------------------------------------------------*/


    for (i = 0; i < M; i++)
        synth_buf[i] = 0;
    synth = synth_buf + M;

    Init_Decoder_6k7();
    Init_Post_Filter();
    Init_Post_Process();

/*-----------------------------------------------------------------*
 *            Loop for each "L_FRAME" speech data                  *
 *-----------------------------------------------------------------*/

    frame = 0;
    while (fread(serial, sizeof(Word16), SERIAL_SIZE, f_serial) == SERIAL_SIZE) {
       fprintf(stderr, "frame = %-6d", frame++);
       
       reorderFRtoEFRchannelFormat(serial);

       Bits2prm_6k7(serial, parm);  /* serial to parameters   */
       
       bfi = parm[0];
       /* 2 dummy bits in parm[1] not used */
       Decoder_6k7(&parm[2], bfi, synth, Az_dec);  /* Synthesis */

       Post_Filter(synth, Az_dec);
       
       Post_Process(synth, L_FRAME);  /* filter + 15->16 bits */
       
       fwrite(synth, sizeof(Word16), L_FRAME, f_syn);
       
       fprintf(stderr, "\r");
    }
    fprintf(stderr, "Number of decoded frames : %-6d", frame++);

    fprintf(stderr, "\n");

    fclose(f_syn);
    fclose(f_serial);
    return 0;
}
