www.pudn.com > T-REC-G.722.1-200505-I!!SOFT-ZST-E.zip > decode.c


/***************************************************************** 
****************************************************************** 
** 
**   ITU-T Wideband Coder Candidate (G.WB) Source Code 
** 
**	   File name : decode.c  
** 
**   © 2000 PictureTel Coporation 
**          Andover, MA, USA   
** 
**	    All rights reserved. 
** 
****************************************************************** 
*****************************************************************/ 
 
/*************************************************************************** 
 Include files                                                            
***************************************************************************/ 
 
#include 
#include 
#include "defs.h"

static int one = 0x0081;
static int zero = 0x007f;
static int frame_start = 0x6b21;
int read_ITU_format(short int [], int *, int, FILE *);
 
/************************************************************************************ 
 Extern function declarations                                              
*************************************************************************************/
extern void mlt_based_coder_init();
extern void decoder(int, int, short int [], float [], int);  
extern void rmlt_coefs_to_samples(float *, float *, int); 
 
/*************************************************************************** 
 Procedure/Function:     G722.1 main decoder function  
 
 Syntax: 
 
 Description:  Main processing loop for the G.722.1 decoder 
 
 Design Notes: 
								 
***************************************************************************/ 
			   
void main(argc, argv)
     int argc;
     char *argv[];
{
  FILE *fpout;
  FILE *fp_bitstream;

  int i;
  int nsamp1; 
  int number_of_regions; 
  short int output[MAX_DCT_SIZE];
  float decoder_mlt_coefs[MAX_DCT_SIZE]; 
  float float_out_samples[MAX_DCT_SIZE]; 
  int sample_rate;
  int bit_rate;
  int number_of_bits_per_frame;
  int number_of_16bit_words_per_frame;
  short int out_words[MAX_BITS_PER_FRAME/16];
  int framesize;
  int bandwidth=7;
  int syntax;
  int frame_error_flag=0;
 
  /* parse the command line input */
  if (argc < 5) {
    printf("Usage: decode 0(packed)/1 input-audio-file output-bitstream-file bit-rate\n");
    exit(1);
  }

  syntax = atoi(*++argv);

  if ((syntax != 0) && (syntax != 1))
    {
      printf("syntax must be 0 for packed or 1 for ITU format\n");
      exit(1);
    }
  
    if((fp_bitstream = fopen(*++argv,"rb")) == NULL) {
      printf("codec: error opening %s.\n",*argv);
      exit(1);
    }
    if((fpout = fopen(*++argv,"wb")) == NULL) {
      printf("codec: error opening %s.\n",*argv);
      exit(1);
	}
   
  printf ("FLOATING POINT DECODE...\n"); 
 
  bit_rate = atoi(*++argv);

  if ((bit_rate < 8000) || (bit_rate > 48000) ||
      ((bit_rate/800)*800 != bit_rate)) {
    printf("codec: Error. bit-rate must be multiple of 800 between 8000 and 48000\n");
    exit(1);
  }
   
 /* initializes bandwidth and sampling rate parameters */ 
   
   number_of_regions = 14; 
 
   sample_rate = 16000; 
 
   framesize = sample_rate/50;
   
   number_of_bits_per_frame = bit_rate/50;

  printf("decoder\n");
  printf("bandwidth = %d khz\n",bandwidth);
  printf("syntax = %d ",syntax);
  if (syntax == 0) printf(" packed bitstream\n");
  else if (syntax == 1) printf(" ITU selection test bitstream\n");
  printf("sample_rate = %d    bit_rate = %d\n",sample_rate,bit_rate);
  printf("framesize = %d samples\n",framesize);
  printf("number_of_regions = %d\n",number_of_regions);
  printf("number_of_bits_per_frame = %d bits\n",number_of_bits_per_frame);

  number_of_16bit_words_per_frame = number_of_bits_per_frame/16;

  mlt_based_coder_init();

/* Read first frame of samples from disk. */
 
    if (syntax == 0)
      nsamp1 = fread(out_words, 2, number_of_16bit_words_per_frame, fp_bitstream);
	 
	else
      nsamp1 = read_ITU_format(out_words,
			       &frame_error_flag,
			       number_of_16bit_words_per_frame,
			       fp_bitstream);
    		 
	if (nsamp1 == number_of_16bit_words_per_frame) nsamp1 = framesize;
 
  while(nsamp1 == framesize) {
	 
    decoder(number_of_regions, 
						number_of_bits_per_frame, 
						out_words, 
						decoder_mlt_coefs, 
						frame_error_flag); 
 
	rmlt_coefs_to_samples(decoder_mlt_coefs, float_out_samples, framesize); 
 
	{ 
	  float ftemp0; 
				 for (i=0; i= 0.0)  
							{ 
							  if (ftemp0 < 32767.0) 
								output[i] = (int) (ftemp0 + 0.5); 
							  else 
								output[i] = 32767; 
							}	     
							 
							else  
							{ 
							  if (ftemp0 > -32768.0) 
								output[i] = (int) (ftemp0 - 0.5); 
							  else 
								output[i] = -32768; 
							} 
 
				  } 
	} 
 
	/* For ITU testing and off the 2 lsbs. */
 
    for (i=0; i= 0) {
	bit = in_array[j++];
	if (bit == zero) 
	  bit = 0;
	else if (bit == one) 
	  bit = 1;
	else {
	  *p_frame_error_flag = 1;

	}
	packed_word <<= 1;
	packed_word += bit;
	bit_count--;
      }
      out_words[i] = packed_word;
    }
  }
  return((nsamp-1)/16);
}