www.pudn.com > SMV_Code.rar > lib_bit.c


/*=========================================================================*/ 
/* Each of the companies; Ericsson, Lucent, Mindspeed, Motorola, Nokia,    */ 
/* Nortel Networks, and Qualcomm (hereinafter referred to individually as  */ 
/* “Source” or collectively as “Sources”) do hereby state:                 */ 
/*                                                                         */ 
/* To the extent to which the Source(s) may legally and freely do so,      */ 
/* the Source(s), upon submission of a Contribution, grant(s) a free,      */ 
/* irrevocable, non-exclusive, license to the Third Generation Partnership */ 
/* Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA,     */ 
/* TTA, and TTC, under the Source’s copyright or copyright license rights  */ 
/* in the Contribution, to, in whole or in part, copy, make derivative     */ 
/* works, perform, display and distribute the Contribution and derivative  */ 
/* works thereof consistent with 3GPP2’s and each Organizational Partner’s */ 
/* policies and procedures, with the right to (i) sublicense the foregoing */ 
/* rights consistent with 3GPP2’s and each Organizational Partner’s        */ 
/* policies and procedures and (ii) copyright and sell, if applicable) in  */ 
/* 3GPP2's name or each Organizational Partner’s name any 3GPP2 or         */ 
/* transposed Publication even though this Publication may contain the     */ 
/* Contribution or a derivative work thereof.  The Contribution shall      */ 
/* disclose any known limitations on the Source’s rights to license as     */ 
/* herein provided.                                                        */ 
/*                                                                         */ 
/* When a Contribution is submitted by the Source(s) to assist the         */ 
/* formulating groups of 3GPP2 or any of its Organizational Partners,      */ 
/* it is proposed to the Committee as a basis for discussion and is not    */ 
/* to be construed as a binding proposal on the Source(s).  The Source(s)  */ 
/* specifically reserve(s) the right to amend or modify the material       */ 
/* contained in the Contribution. Nothing contained in the Contribution    */ 
/* shall, except as herein expressly provided, be construed as conferring  */ 
/* by implication, estoppel or otherwise, any license or right under       */ 
/* (i) any existing or later issuing patent, whether or not the use of     */ 
/* information in the document necessarily employs an invention of any     */ 
/* existing or later issued patent, (ii) any copyright, (iii) any          */ 
/* trademark, or (iv) any other intellectual property right.               */ 
/*                                                                         */ 
/* With respect to the Software necessary for the practice of any or all   */ 
/* Normative portions of the Selectable Mode Vocoder (SMV) as it exists on */ 
/* the date of submittal of this form, should the SMV be approved as a     */ 
/* Specification or Report by 3GPP2, or as a transposed Standard by any of */ 
/* the 3GPP2’s Organizational Partners, the Source(s) state(s) that a      */ 
/* worldwide license to reproduce, use and distribute the Software, the    */ 
/* license rights to which are held by the Source(s), will be made         */ 
/* available to applicants under terms and conditions that are reasonable  */ 
/* and non-discriminatory, which may include monetary compensation,        */ 
/* and only to the extent necessary for the practice of any or all of the  */ 
/* Normative portions of the SMV or the field of use of practice of the    */ 
/* SMV Specification, Report, or Standard.  The statement contained above  */ 
/* is irrevocable and shall be binding upon the Source(s).  In the event   */ 
/* the rights of the Source(s) in and to copyright or copyright license    */ 
/* rights subject to such commitment are assigned or transferred,          */ 
/* the Source(s) shall notify the assignee or transferee of the existence  */ 
/* of such commitments.                                                    */ 
/*=========================================================================*/ 
/*                                                                   */ 
/*-------------------------------------------------------------------*/ 
/*===================================================================*/ 
/* PROTOTYPE FILE: lib_bit.h                                         */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE :  Bit-stream library                                     */ 
/*===================================================================*/ 
 
/*----------------------------------------------------------------------------*/ 
/*-------------------------------- INCLUDE -----------------------------------*/ 
/*----------------------------------------------------------------------------*/ 
 
#include "typedef.h" 
#include "main.h" 
#include "const.h" 
 
#include "ext_var.h" 
 
#include "gputil.h" 
#include "mcutil.h" 
#include "lib_bit.h" 
 
 
#ifdef DATA 
  INT16 illegal_half_id = 0x7fc8; 
  INT16 illegal_full_id[2]= { 0x20 , 0x2ea} ; 
#endif 
 
/*----------------------------------------------------------------------------*/ 
/*-------------------------------- FUNCTIONS ---------------------------------*/ 
/*----------------------------------------------------------------------------*/ 
 
#ifdef ENC_CMP 
 
/*===================================================================*/ 
/* FUNCTION      :  BIT_bitpack ().                                  */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE       :  This function converts the encoder indices into  */ 
/*                   the bit-stream.                                 */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*       _ (INT16 ) in:          input data.                         */ 
/*       _ (INT16 ) NoOfBits:    number of bits.                     */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*       _ (UNS_INT16 *) TrWords : trasmitted word.                  */ 
/*       _ (INT16     *) ptr :     bit and word pointers.            */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*                            _ None.                                */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                            _ None.                                */ 
/*===================================================================*/ 
 
void    BIT_bitpack (INT16 in, UNS_INT16 *TrWords, INT16 NoOfBits, 
		       INT16 *ptr) 
	{ 
	 /*-------------------------------------------------------------------*/ 
	  
	 INT16   temp; 
	 UNS_INT16 *WordPtr; 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 WordPtr = TrWords + ptr[1]; 
 
	 *ptr -= NoOfBits; 
	 if (*ptr >= 0) 
	   *WordPtr = *WordPtr | (in << *ptr); 
	 else 
	   { 
#ifdef DATA 
	     temp = (UNS_INT16)in >> (-*ptr); 
#else 
	     temp = in >> (-*ptr); 
#endif 
	     *WordPtr = *WordPtr | temp; 
	     WordPtr++; 
	     *ptr = 16 + *ptr; 
	     *WordPtr = (INT16) ((INT64) ((INT64) in << *ptr) & 0xffff); 
	   } 
	  
	 ptr[1] = (INT16) (WordPtr - TrWords); 
	  
	 /*-------------------------------------------------------------------*/ 
	  
	 return; 
	  
	 /*-------------------------------------------------------------------*/ 
	} 
 
#endif 
 
/*----------------------------------------------------------------------------*/ 
 
#ifdef DEC_CMP 
 
/*===================================================================*/ 
/* FUNCTION      :  BIT_bitunpack ().                                */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE       :  This function converts the decoder bit-stream    */ 
/*                  into indices.                                    */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*       _ (UNS_INT16 *) RecWords : received word.                   */ 
/*       _ (INT16      ) NoOfBits : number of bits.                  */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*       _ (INT16 *) out:   output data.                             */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*       _ (INT16 *) ptr:  bit and word pointers.                    */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*       _ None.                                                     */ 
/*===================================================================*/ 
 
void    BIT_bitunpack (INT16 *out, UNS_INT16 *RecWords, INT16 NoOfBits, 
		       INT16 *ptr) 
	{ 
	 /*-------------------------------------------------------------------*/ 
	  
	 INT64   temp; 
	 UNS_INT16 *WordPtr; 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 WordPtr = RecWords + ptr[1]; 
 
	 *ptr -= NoOfBits; 
	 if (*ptr >= 0) 
	   temp = (INT64) (*WordPtr) << NoOfBits; 
	 else 
	   { 
	     temp = (INT64) (*WordPtr) << (NoOfBits + *ptr); 
	     WordPtr++; 
	     temp = (temp << (-*ptr)) | ((INT64) *WordPtr << (-*ptr)); 
	     *ptr = 16 + *ptr; 
	   } 
	  
	 *WordPtr = (INT16) (temp & 0xffff); 
	 *out = (INT16) ((INT64) (temp & 0xffff0000) >> 16); 
 
	 ptr[1] = (INT16) (WordPtr - RecWords); 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 return; 
	  
	 /*-------------------------------------------------------------------*/ 
	} 
#endif 
 
/*----------------------------------------------------------------------------*/ 
 
#ifdef ENC_CMP 
 
/*===================================================================*/ 
/* FUNCTION      :  BIT_cdbk_index_to_bits ().                       */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE       :  This function converts the fixed codebook index  */ 
/*                   into the bit-stream representation.             */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*         _ (PARAMETER  *)  indices    : fixed codebook indices.    */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*         _ (INT16     [])  packedwords: bit-stream.                */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*         _ (INT16     [])  packw_ptr:   pointer to the bit-stream. */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                            _ None.                                */ 
/*===================================================================*/ 
	 
void BIT_cdbk_index_to_bits (PARAMETER *chan, INT16 packedwords [], 
						INT16 packw_ptr []) 
	{ 
	 /*-------------------------------------------------------------------*/ 
 
	 INT16 i, i_sf; 
	 INT16 flag_all_bits_zero; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                 Initalizing bit packing parameters                */ 
	 /*-------------------------------------------------------------------*/ 
	  
	 packw_ptr[0] = 16; 
 
#ifdef DATA 
	 /*-------------------------------------------------------------------*/ 
	 /*           For full rate data start 1 bit after SVS1=0 bit         */ 
 	 /*-------------------------------------------------------------------*/ 
	  
	 if ((data_mode > 1) && (num_data > 0)) 
	   packw_ptr[0] = 15; 
#endif 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                     Points to the second word                     */ 
	 /*-------------------------------------------------------------------*/ 
	  
	 packw_ptr[1] = 1;     
   
	 ini_svector(packedwords, 0, PACKWDSNUM-1, 0); 
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Slected bitrate ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
	  
	 switch (chan->fix_rate) 
	   { 
	    case RATE8_5K: packedwords[0] = 4; 
	      break; 
	       
	    case RATE4_0K: packedwords[0] = 3; 
#ifdef DATA 
	      /*--------------------------------------------------------------*/ 
	      /*               If data_mode>1 set rate to full                */ 
	      /*--------------------------------------------------------------*/ 
 
	      if ((data_mode>1) && (num_data>0 )) 
		packedwords[0] = 4; 
#endif 
	      break; 
	       
	    case RATE2_0K: packedwords[0] = 2; 
	      break; 
 
	    case RATE0_8K: packedwords[0] = 1;	  
	      break; 
 
	    default: nrerror("Invalid rate !!"); 
	      break; 
	   } 
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Classification index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
	  
	 if ((chan->fix_rate == RATE8_5K) || (chan->fix_rate==RATE4_0K)) 
	   BIT_bitpack(chan->idx_SVS_deci, (UNS_INT16 *) packedwords, 
		       1, packw_ptr);      
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ LSF quantizer index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
	 switch (chan->fix_rate) 
	 	{ 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE8_5K: 
		   /*---------------------------------------------------------*/ 
		   /*                        25 bits                          */ 
		   /*---------------------------------------------------------*/ 
		    
		   BIT_bitpack(chan->idx_lsf[0], (UNS_INT16 *) packedwords, 
			       7, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[1], (UNS_INT16 *) packedwords, 
			       7, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[2], (UNS_INT16 *) packedwords, 
			       6, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[3], (UNS_INT16 *) packedwords, 
			       4, packw_ptr); 
 
		   BIT_bitpack(chan->idx_lsf[4], (UNS_INT16 *) packedwords, 
			       1, packw_ptr); 
		    
		   /*---------------------------------------------------------*/ 
		   /*                LSF interpolation  2 bits                */ 
		   /*---------------------------------------------------------*/ 
		    
		   if (chan->idx_SVS_deci == 0) 
		     BIT_bitpack(chan->idx_lpc_int,  
				 (UNS_INT16 *) packedwords, 2, packw_ptr); 
		   break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE4_0K: 
		   /*---------------------------------------------------------*/ 
		   /*                        21 bits                          */ 
		   /*---------------------------------------------------------*/ 
		    
		   BIT_bitpack(chan->idx_lsf[0], (UNS_INT16 *) packedwords, 
			       7, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[1], (UNS_INT16 *) packedwords, 
			       7, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[2], (UNS_INT16 *) packedwords, 
			       6, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[3], (UNS_INT16 *) packedwords, 
			       1, packw_ptr); 
 
		   break; 
	    
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE2_0K: 
		   /*---------------------------------------------------------*/ 
		   /*                        20 bits                          */ 
		   /*---------------------------------------------------------*/ 
		    
		   BIT_bitpack(chan->idx_lsf[0], (UNS_INT16 *) packedwords, 
			       7, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[1], (UNS_INT16 *) packedwords, 
			 					7, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[2], (UNS_INT16 *) packedwords, 
			       6, packw_ptr); 
		    
		   break; 
	    
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE0_8K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE0_8K: 
		 
		   /*---------------------------------------------------------*/ 
		   /*                        11 bits                          */ 
		   /*---------------------------------------------------------*/ 
		    
		   BIT_bitpack(chan->idx_lsf[0], (UNS_INT16 *) packedwords, 
			       4, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[1], (UNS_INT16 *) packedwords, 
			       4, packw_ptr); 
		    
		   BIT_bitpack(chan->idx_lsf[2], (UNS_INT16 *) packedwords, 
			       3, packw_ptr); 
		   break; 
		    
		 default: nrerror ("Invalid rate !!"); 
		    
		   break; 
		}	  
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Pitch index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
	 if ((chan->fix_rate == RATE8_5K) || (chan->fix_rate == RATE4_0K)) 
	   { 
	     if (chan->idx_SVS_deci == 1) 
	       { 
		 if (chan->fix_rate == RATE8_5K) 
		   BIT_bitpack(chan->idx_pitch[0],  
			       (UNS_INT16 *) packedwords, 8, packw_ptr); 
		 else if (chan->fix_rate==RATE4_0K) 
		   BIT_bitpack(chan->idx_pitch[0], 
			       (UNS_INT16 *) packedwords, 7, packw_ptr); 
		  
	       } 
	     else 
	       { 
		 if (chan->fix_rate == RATE8_5K) 
		   { 
		     BIT_bitpack(chan->idx_pitch[0], 
				 (UNS_INT16 *) packedwords, 8, packw_ptr); 
		      
		     BIT_bitpack(chan->idx_pitch[1], 
				 (UNS_INT16 *) packedwords, 5, packw_ptr); 
		      
		     BIT_bitpack(chan->idx_pitch[2], 
				 (UNS_INT16 *) packedwords, 8, packw_ptr); 
		      
		     BIT_bitpack(chan->idx_pitch[3], 
				 (UNS_INT16 *) packedwords, 5, packw_ptr); 
		   } 
		 else if (chan->fix_rate==RATE4_0K) 
		   { 
		     for (i = 0; i < N_SF2; i++) 
		       BIT_bitpack(chan->idx_pitch[i], 
				 (UNS_INT16 *) packedwords, 7, packw_ptr); 
		   } 
	       } 
	   } 
	  
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Gains index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
	  
	 switch (chan->fix_rate) 
	        { 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE8_5K: 
		   if (chan->idx_SVS_deci==1) 
		     { 
		       BIT_bitpack(chan->idx_Gp_VQ, 
		       	   (UNS_INT16 *) packedwords, 6, packw_ptr); 
		        
		       BIT_bitpack(chan->idx_Gc_VQ, 
			   (UNS_INT16 *) packedwords, 10, packw_ptr); 
		     } 
		   else 
		     { 
		       for (i = 0; i < N_SF4; i++) 
			 BIT_bitpack(chan->idx_gainVQ[i], 
				     (UNS_INT16 *) packedwords, 7, packw_ptr); 
		     } 
		   break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE4_0K: 
		   if (chan->idx_SVS_deci==1) 
		     { 
		       BIT_bitpack(chan->idx_Gp_VQ, 
			     (UNS_INT16 *) packedwords, 4, packw_ptr); 
		        
		       BIT_bitpack(chan->idx_Gc_VQ, 
				   (UNS_INT16 *) packedwords, 8, packw_ptr); 
		     } 
		   else 
		     { 
		       for (i = 0; i < N_SF2; i++) 
			 BIT_bitpack(chan->idx_gainVQ[i], 
				     (UNS_INT16 *) packedwords, 7, packw_ptr); 
		     } 
		    
		   break; 
		    
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
                 case RATE2_0K: 
 
                   BIT_bitpack(chan->idx_gainVQ[0], (UNS_INT16 *) packedwords,  
			       5, packw_ptr); 
                   BIT_bitpack(chan->idx_gainVQ[1], (UNS_INT16 *) packedwords, 
			       6, packw_ptr); 
                   BIT_bitpack(chan->idx_gainVQ[2], (UNS_INT16 *) packedwords, 
			       6, packw_ptr); 
                   BIT_bitpack(chan->idx_gainVQ[3], (UNS_INT16 *) packedwords, 
			       2, packw_ptr); 
			break; 
	    
		 case RATE0_8K: 
		   BIT_bitpack(chan->idx_gainVQ[0], (UNS_INT16 *) packedwords, 
			       5, packw_ptr); 
		   break; 
 
		 default: nrerror("Invalid rate !!"); 
		   break; 
		}	  
	  
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Codeboks index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
	  
	 switch (chan->fix_rate)  
	 	{ 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE8_5K : 
    
		 if (chan->idx_SVS_deci == 1) 
		   { 
		     /*-------------------------------------------------------*/ 
		     /*                Stationary voiced speech               */ 
		     /*-------------------------------------------------------*/ 
			  
		     for (i_sf = 0; i_sf < N_SF4; i_sf++) 
		       { 
			 /*---------------------------------------------------*/ 
		         /* 8 pulses CB : 6p x 3b + 2p x 4b + 4b signs =      */ 
			 /*                  30 bits                          */ 
			 /*---------------------------------------------------*/ 
			  
			 for (i = 0; i < 4; i++) 
			   BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
				       (UNS_INT16 *) packedwords, 1, packw_ptr); 
 
			 BIT_bitpack((chan->idx_cpcb[i_sf][0]),  
				     (UNS_INT16 *) packedwords, 4, packw_ptr); 
			  
			 for (i = 1; i < 4; i++) 
			   BIT_bitpack((chan->idx_cpcb[i_sf][i]),  
				       (UNS_INT16 *) packedwords, 3, packw_ptr); 
 
			 BIT_bitpack((chan->idx_cpcb[i_sf][4]), 
				     (UNS_INT16 *) packedwords, 4, packw_ptr); 
			 for (i = 5; i < 8; i++) 
			   BIT_bitpack((chan->idx_cpcb[i_sf][i]), 
				       (UNS_INT16 *) packedwords, 3, packw_ptr); 
		       } 
		     /*-------------------------------------------------------*/ 
		   } 
		 else 
		   { 
		     /*-------------------------------------------------------*/ 
		     /*              Non-Stationary voiced speech             */ 
		     /*-------------------------------------------------------*/ 
 
		     for (i_sf = 0; i_sf < N_SF4; i_sf++) 
		       { 
			 BIT_bitpack((chan->idx_subcpcb[i_sf][0]), 
				     (UNS_INT16 *) packedwords, 1, packw_ptr); 
			  
			 if (chan->idx_subcpcb[i_sf][0]==1)  
			   { 
			     /*-----------------------------------------------*/ 
			     /* 5 pulses CB : 3p x 4b + 2p x 3b + 3b signs    */ 
			     /*                = 21 bits                      */ 
			     /*-----------------------------------------------*/ 
			     for (i = 0; i < 2; i++)	          
			       BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
					   (UNS_INT16 *) packedwords, 1,  
					   packw_ptr); 
			     BIT_bitpack((chan->idx_cpcbsign[i_sf][4]), 
					 (UNS_INT16 *) packedwords, 1, 
					 packw_ptr); 
			      
			     BIT_bitpack((chan->idx_cpcb[i_sf][0]), 
					 (UNS_INT16 *) packedwords, 4,  
					 packw_ptr); 
	 		 
			     BIT_bitpack((chan->idx_cpcb[i_sf][1]),  
					 (UNS_INT16 *) packedwords, 3, 
					 packw_ptr); 
		 		 
			     BIT_bitpack((chan->idx_cpcb[i_sf][2]), 
					 (UNS_INT16 *) packedwords, 4,  
					 packw_ptr);	 
 		 
			     BIT_bitpack((chan->idx_cpcb[i_sf][3]), 
					 (UNS_INT16 *) packedwords, 3,  
					 packw_ptr); 
			      
			     BIT_bitpack((chan->idx_cpcb[i_sf][4]), 
					 (UNS_INT16 *) packedwords, 4, 
					 packw_ptr); 
			      
			   } 
			 else 
			   { 
			     BIT_bitpack((chan->idx_subcpcb[i_sf][1]), 
					 (UNS_INT16 *) packedwords, 1,  
					 packw_ptr); 
			      
			     /*-----------------------------------------------*/ 
			     /*         5 pulses CB : 5p x 3b + 5b signs      */ 
			     /*                = 20 bits                      */ 
			     /*-----------------------------------------------*/ 
 
			     for (i = 0; i < 5; i++) 
			       BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
					   (UNS_INT16 *) packedwords, 1, 
					   packw_ptr); 
			     for (i = 0; i < 5; i++) 
			       BIT_bitpack((chan->idx_cpcb[i_sf][i]), 
					   (UNS_INT16 *) packedwords, 3, 
					   packw_ptr); 
			      
			   } 
		       }  
		   }	  	    
		 break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE4_0K : 
		  
		 if (chan->idx_SVS_deci==1) 
		   { 
		     /*-------------------------------------------------------*/ 
		     /*                Stationary voiced speech               */ 
		     /*-------------------------------------------------------*/ 
		      
		     for (i_sf = 0; i_sf < N_SF3; i_sf++) 
		       { 
			 BIT_bitpack((chan->idx_subcpcb[i_sf][0]), 
				     (UNS_INT16 *) packedwords, 1, packw_ptr); 
			  
			 if (chan->idx_subcpcb[i_sf][0] == 1) 
			   { 
			     /*-----------------------------------------------*/ 
			     /* 2 pulses CB : 2pulses x 5bits/pulse + 2 signs */ 
			     /*                = 12 bits                      */ 
			     /*-----------------------------------------------*/ 
					  
			     for (i = 0; i < 2; i++) 
			       BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
					   (UNS_INT16 *) packedwords, 1, 
					   packw_ptr); 
			     for (i = 0; i < 2; i++) 
			       BIT_bitpack((chan->idx_cpcb[i_sf][i]), 
					   (UNS_INT16 *) packedwords, 5, 
					   packw_ptr); 
			      
			   } 
			 else 
			   { 
			     BIT_bitpack((chan->idx_subcpcb[i_sf][1]), 
					 (UNS_INT16 *) packedwords, 1, packw_ptr); 
			      
                             if (chan->idx_subcpcb[i_sf][1] == 1) 
			       {			 
				 /*------------------------------------------*/ 
				 /* 3 pulses CB: 3 pulses x                  */ 
				 /*                 (4, 2 or 2)bits/pulse    */ 
				 /*                    + 3 signs = 11 bits   */ 
				 /*------------------------------------------*/ 
				  
				 for (i = 0; i < 3; i++) 
				   {		          
				     BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 1, 
						 packw_ptr); 
				   } 
                                             
				 BIT_bitpack((chan->idx_cpcb[i_sf][0]), 
					     (UNS_INT16 *) packedwords, 4,  
					     packw_ptr); 
				  
				 for (i = 1; i < 3; i++) 
				   { 
				     BIT_bitpack((chan->idx_cpcb[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 2, 
						 packw_ptr); 
				   }	 
			       } 
			     else 
			       { 
				 /*------------------------------------------*/  
				 /*        5 pulses CB :   11 bits           */  
				 /*------------------------------------------*/ 
					  
				 for (i = 0; i < 5; i++) 
				   {	 
				     BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 1, 
						 packw_ptr); 
				   } 
				  
				 BIT_bitpack((chan->idx_cpcb[i_sf][0]), 
					     (UNS_INT16 *) packedwords, 2, 
					     packw_ptr); 
				  
				 for (i = 1; i < 5; i++) 
				   { 
				     BIT_bitpack((chan->idx_cpcb[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 1, 
						 packw_ptr); 
				   }									 
			       }  
			   } 
		       } 
		   } 
		 else 
		   { 
		      
		     /*-------------------------------------------------------*/ 
		     /*              Non-Stationary voiced speech             */ 
		     /*-------------------------------------------------------*/ 
 
		     for (i_sf = 0; i_sf < N_SF2; i_sf++) 
		       { 
			 /*---------------------------------------------------*/ 
			 /* Remap indices of upper part of Gaussian codebook  */ 
			 /*   to the free index space of the 2-pulse codebook */ 
			 /*---------------------------------------------------*/ 
			  
			 if (chan->idx_subcpcb[i_sf][0] == 0 && 
			     chan->idx_subcpcb[i_sf][1] == 0 && 
			     chan->idx_cpcb[i_sf][0] >= NUM11BITS) 
			   { 
			     /*-----------------------------------------------*/ 
			     /*                 2 pulse codebook              */ 
			     /*-----------------------------------------------*/ 
 
			     chan->idx_subcpcb[i_sf][0] = 1; 
			      
			     chan->idx_cpcb[i_sf][0] += 
			       NUMPOS2PULSE - NUM11BITS + 
			       chan->idx_cpcbsign[i_sf][1] * 
			       DELTA_REMAP; 
			   } 
			  
			 /*---------------------------------------------------*/ 
 
			 BIT_bitpack((chan->idx_subcpcb[i_sf][0]), 
				     (UNS_INT16 *) packedwords, 1, packw_ptr); 
			  
			 if (chan->idx_subcpcb[i_sf][0]==1) 
			   { 
			     /*-----------------------------------------------*/ 
			     /*     2 pulses CB :  2pulses x 6.5bits/pulse    */ 
			     /*              + 1 sign = 12 bits               */ 
			     /*-----------------------------------------------*/ 
			      
			     BIT_bitpack((chan->idx_cpcbsign[i_sf][0]), 
					 (UNS_INT16 *) packedwords, 1, 
					 packw_ptr); 
			     BIT_bitpack((chan->idx_cpcb[i_sf][0]), 
					 (UNS_INT16 *) packedwords, 13, 
					 packw_ptr); 
			   } 
			 else 
			   { 
			     BIT_bitpack((chan->idx_subcpcb[i_sf][1]), 
					 (UNS_INT16 *) packedwords, 1,  
					 packw_ptr); 
			      
                             if (chan->idx_subcpcb[i_sf][1] == 1) 
			       {					 
				 /*-----------------------------------------*/ 
				 /* 3 pulses CB: 3 pulses x                 */ 
				 /*                    (4, 3, 3)bits/pulse  */ 
				 /*                    + 3 signs = 13 bits  */ 
				 /*-----------------------------------------*/ 
				  
				 for (i = 0; i < 3; i++) 
				   { 
				     BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 1, 
						 packw_ptr); 
				   } 
				  
				 BIT_bitpack((chan->idx_cpcb[i_sf][0]), 
					     (UNS_INT16 *) packedwords, 4, 
					     packw_ptr); 
				  
				 for (i = 1; i < 3; i++) 
				   { 
				     BIT_bitpack((chan->idx_cpcb[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 3, 
						 packw_ptr); 
				   } 
			       } 
			     else 
			       { 
				 /*-----------------------------------------*/ 
				 /*     Gaussian codebook, 13 bits          */ 
				 /*-----------------------------------------*/ 
				  
				 for (i = 0; i < 2; i++) 
				   {	 
				     BIT_bitpack((chan->idx_cpcbsign[i_sf][i]), 
						 (UNS_INT16 *) packedwords, 1, 
						 packw_ptr); 
				   } 
				  
				 BIT_bitpack((chan->idx_cpcb[i_sf][0]), 
					     (UNS_INT16 *) packedwords, 11,  
					     packw_ptr); 
			       } 
			   } 
		       } 
		   }   
		  
		 break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE2_0K :     
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE0_8K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE0_8K :     
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*======================== ERROR ============================*/ 
		 /*-----------------------------------------------------------*/ 
			 
		 default: nrerror ("Invalid rate !!"); 
			break; 
		}	  
		 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Badrate Flag ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
	 switch (chan->fix_rate)  
	 	{ 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE8_5K :     
		   BIT_bitpack(chan->badrate_flag, (UNS_INT16 *) packedwords, 1, 
			       packw_ptr);      
		   break; 
		    
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE4_0K :     
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE2_0K :     
		        BIT_bitpack(chan->badrate_flag,  
				    (UNS_INT16 *) packedwords, 1, packw_ptr); 
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE0_8K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE0_8K :     
			break; 
 
 
		 /*-----------------------------------------------------------*/ 
		 /*======================== ERROR ============================*/ 
		 /*-----------------------------------------------------------*/ 
			 
		 default: nrerror ("Invalid rate !!"); 
			break; 
		} 
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤ Remove all zero illegal configurations from bitstream ¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
	 flag_all_bits_zero = 1; 
	 switch (chan->fix_rate) 
	   { 
	    case RATE8_5K: 
	       for (i = 1; i <= 10; i++) 
		if (packedwords[i] != 0x0000) 
		  flag_all_bits_zero = 0; 
	       if ((packedwords[11] & 0x07FF) != 0x0000) 
		  flag_all_bits_zero = 0; 
 
	       if (flag_all_bits_zero == 1) 
		 packedwords[2] = 0x0400; 
	      break; 
	       
	    case RATE4_0K: 
	       for (i = 1; i <= 5; i++) 
		if (packedwords[i] != 0x0000) 
		  flag_all_bits_zero = 0; 
 
	       if (flag_all_bits_zero == 1) 
		 packedwords[2] = 0xC000; 
	      break; 
	       
	    case RATE2_0K: 
	       for (i = 1; i <= 2; i++) 
		if (packedwords[i] != 0x0000) 
		  flag_all_bits_zero = 0; 
	       if ((packedwords[11] & 0x00FF) != 0x0000) 
		  flag_all_bits_zero = 0; 
 
	       if (flag_all_bits_zero == 1) 
		 { 
		   packedwords[1] = 0x0001; 
		   packedwords[2] = 0x8000; 
		 } 
	      break; 
 
	    case RATE0_8K:	  
		if (packedwords[1] == 0x0000) 
		  packedwords[1] = 0x0040; 
		if ((UNS_INT16)packedwords[1] == 0xFFFF) 
		 packedwords[1] = 0xFFDF; 
	      break; 
 
	    default: nrerror("Invalid rate !!"); 
	      break; 
	   } 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 return; 
	  
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
 
#ifdef DATA 
/*===================================================================*/ 
/* FUNCTION      :  BIT_data_to_bits ().                             */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE       :  This function converts the data/dtmf/tty         */ 
/*                   into the bit-stream representation.             */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*         _ (PARAMETER  *)  indices    : data_buf                   */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*         _ (INT16     [])  packedwords: bit-stream.                */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*         _ (INT16     [])  packw_ptr:   pointer to the bit-stream. */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                            _ None.                                */ 
/*===================================================================*/ 
	 
void BIT_data_to_bits (INT16 *data_buf, INT16 packedwords [],  
		       INT16 packw_ptr []) 
	{ 
	 /*-------------------------------------------------------------------*/ 
 
	  INT16 i; 
 
	  /*------------------------------------------------------------------*/ 
	  /*                Initalizing bit packing parameters                */ 
	  /*------------------------------------------------------------------*/ 
	  
	  packw_ptr[0] = 15; 
	  
	  /*------------------------------------------------------------------*/ 
	  /*                    Points to the second word                     */ 
	  /*------------------------------------------------------------------*/ 
 
	  /*------------------------------------------------------------------*/ 
	  /*          If data_mode==3 overwrite half rate with aux data       */ 
	  /*------------------------------------------------------------------*/ 
 
	  if ((data_mode==3)&&(num_data>0 ))  
	    { 
	      packw_ptr[0] = 15; 
	      ini_svector(packedwords, 1, 5-1, 0); 
	      packw_ptr[1] = 1; 
              for (i = DATA_SIZE; i < DATA_SIZE+num_data-4; i++) 
		{ 
		  BIT_bitpack(data_buf[i], (UNS_INT16 *) packedwords, 16,  
			      packw_ptr); 
		} 
	    } 
	   
	  /*------------------------------------------------------------------*/ 
	  /*             If full rate start header+data at bit 81             */ 
	  /*------------------------------------------------------------------*/ 
 
	  if ((data_mode>1)&&(num_data>0 ))  
	    { 
	      packw_ptr[1] = 6;     
	    }  
	  else  
	    { 
	      packw_ptr[1] = 1;     
	      ini_svector(packedwords, 1, PACKWDSNUM-2, 0); /* ? */ 
	    } 
 
	  BIT_bitpack(data_buf[0], (UNS_INT16 *) packedwords, 3, packw_ptr); 
          for (i = 1; i< DATA_SIZE-1; i++) 
	    BIT_bitpack(data_buf[i], (UNS_INT16 *) packedwords, 16, packw_ptr); 
	   
	  /*------------------------------------------------------------------*/ 
	  /*                   If full rate write 64 bits                     */ 
	  /*------------------------------------------------------------------*/ 
 
	  if ((data_mode>1)&&(num_data>0 ))  
	    { 
	      BIT_bitpack (data_buf[DATA_SIZE-1], (UNS_INT16 *) packedwords,  
			   16, packw_ptr); 
	      BIT_bitpack(illegal_full_id[0], (UNS_INT16 *) packedwords, 7, 
			  packw_ptr); 
	      BIT_bitpack (illegal_full_id[1], (UNS_INT16 *) packedwords, 15, 
			   packw_ptr); 
	    }  
	  else 
	    { 
	      /*--------------------------------------------------------------*/ 
	      /*                Only 61 bits for half rate data               */ 
	      /*--------------------------------------------------------------*/ 
 
	      BIT_bitpack(data_buf[DATA_SIZE-1], (UNS_INT16 *) packedwords, 13, 
			  packw_ptr); 
	      BIT_bitpack(illegal_half_id, (UNS_INT16 *) packedwords, 15,  
			  packw_ptr); 
	    } 
 
	  /*-----------------------------------------------------------------*/ 
	   
	  return; 
	   
	  /*-----------------------------------------------------------------*/ 
        } 
#endif 
 
#endif 
 
/*----------------------------------------------------------------------------*/ 
 
#ifdef DEC_CMP 
 
/*===================================================================*/ 
/* FUNCTION      :  BIT_bits_to_cdbk_index ().                       */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE       :  This function converts the the bit-stream        */ 
/*                  representation into the codebook index           */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*         _ (INT16     [])  packedwords: bit-stream.                */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*         _ (PARAMETER  *)  indices    : fixed codebook indices.    */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*         _ (INT16     [])  packw_ptr:   pointer to the bit-stream. */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*         _ (INT16       )  bfi: Bad Frame Indicator.               */ 
/*===================================================================*/ 
 
 
INT16 BIT_bits_to_cdbk_index (INT16 packedwords [], INT16 packw_ptr [], 
			     PARAMETER *chan) 
	{ 
	 /*-------------------------------------------------------------------*/ 
	  
	 INT16 i, i_sf;     
	 INT16 bfi; 
 
	 /*-------------------------------------------------------------------*/ 
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤ Detect illegal configurations for bitstream ¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
	  
	 bfi = 1; 
	 switch (chan->fix_rate) 
	   { 
	    case RATE8_5K: 
	       for (i = 1; i <= 10; i++) 
		if (packedwords[i] != 0x0000) 
		  bfi = 0; 
	       if ((packedwords[11] & 0xFFE0) != 0x0000) 
		  bfi = 0; 
	      break; 
	       
	    case RATE4_0K: 
	       for (i = 1; i <= 5; i++) 
		if (packedwords[i] != 0x0000) 
		  bfi = 0; 
	      break; 
	       
	    case RATE2_0K: 
	       for (i = 1; i <= 2; i++) 
		if (packedwords[i] != 0x0000) 
		  bfi = 0; 
	       if ((packedwords[3] & 0xFF00) != 0x0000) 
		  bfi = 0; 
	      break; 
 
	    case RATE0_8K:	  
		if ((packedwords[1] != 0x0000) &&  
		    ((UNS_INT16)packedwords[1] != 0xFFFF)) 
		  bfi = 0; 
	      break; 
 
	    default: nrerror("Invalid rate !!"); 
	      break; 
	   } 
 
		 /*===========================================================*/ 
		 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Classification index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
		 /*===========================================================*/ 
		  
		 if ((chan->fix_rate == RATE8_5K) || 
		     (chan->fix_rate == RATE4_0K)) 
		   BIT_bitunpack (&chan->idx_SVS_deci, 
				  (UNS_INT16 *) packedwords, 1, packw_ptr); 
		 else 
		   chan->idx_SVS_deci=0; 
        
		 /*===========================================================*/ 
		 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ LSF quantizer index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
		 /*===========================================================*/ 
 
		 switch (chan->fix_rate) 
		   { 
		     /*-------------------------------------------------------*/ 
		     /*==================== case RATE8_5K ====================*/ 
		     /*-------------------------------------------------------*/ 
 
		     case RATE8_5K: 
 
		      /*------------------------------------------------------*/ 
		      /*                       25 bits                        */ 
		      /*------------------------------------------------------*/ 
 
		      BIT_bitunpack (&chan->idx_lsf[0], 
				     (UNS_INT16 *) packedwords, 7, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[1], 
				     (UNS_INT16 *) packedwords, 7, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[2], 
				     (UNS_INT16 *) packedwords, 6, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[3], 
				     (UNS_INT16 *) packedwords, 4, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[4], 
				     (UNS_INT16 *) packedwords, 1, packw_ptr); 
		       
		      /*------------------------------------------------------*/ 
		      /*               LSF interpolation  2 bits              */ 
		      /*------------------------------------------------------*/ 
 
		      if (chan->idx_SVS_deci == 0) 
			BIT_bitunpack (&chan->idx_lpc_int, 
				       (UNS_INT16 *) packedwords, 2, packw_ptr); 
		      break; 
		       
		    /*--------------------------------------------------------*/ 
		    /*==================== case RATE4_0K =====================*/ 
		    /*--------------------------------------------------------*/ 
		       
		    case RATE4_0K: 
			  
		      /*-----------------------------------------------------*/ 
		      /*                      21 bits                        */ 
		      /*-----------------------------------------------------*/ 
		       
		      BIT_bitunpack (&chan->idx_lsf[0], 
				     (UNS_INT16 *) packedwords, 7, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[1], 
				     (UNS_INT16 *) packedwords, 7, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[2], 
				     (UNS_INT16 *) packedwords, 6, packw_ptr); 
		       
		      BIT_bitunpack (&chan->idx_lsf[3], 
				     (UNS_INT16 *) packedwords, 1, packw_ptr); 
		       
		      break; 
		       
		   /*---------------------------------------------------------*/ 
		   /*==================== case RATE2_0K ======================*/ 
		   /*---------------------------------------------------------*/ 
		       
		   case RATE2_0K: 
		      
		     /*-------------------------------------------------------*/ 
		     /*                        20 bits                        */ 
		     /*-------------------------------------------------------*/ 
		      
		     BIT_bitunpack (&chan->idx_lsf[0], 
				    (UNS_INT16 *) packedwords, 7, packw_ptr); 
		      
		     BIT_bitunpack (&chan->idx_lsf[1], 
				    (UNS_INT16 *) packedwords, 7, packw_ptr);   
		      
		     BIT_bitunpack (&chan->idx_lsf[2], 
				    (UNS_INT16 *) packedwords, 6, packw_ptr);   
		     break; 
	    
		   /*-------------------------------------------------------*/ 
		   /*================== case RATE0_8K ======================*/ 
		   /*-------------------------------------------------------*/ 
		    
		   case RATE0_8K: 
		 
		     /*-----------------------------------------------------*/ 
		     /*                     11 bits                         */ 
		     /*-----------------------------------------------------*/ 
 
		     BIT_bitunpack (&chan->idx_lsf[0], 
				    (UNS_INT16 *) packedwords, 4, packw_ptr); 
		      
		     BIT_bitunpack (&chan->idx_lsf[1], 
				    (UNS_INT16 *) packedwords, 4, packw_ptr); 
		      
		     BIT_bitunpack (&chan->idx_lsf[2], 
				    (UNS_INT16 *) packedwords, 3, packw_ptr); 
		     break; 
		      
		    default: nrerror ("Invalid rate !!"); 
		 
		      break; 
		   }	  
		  
	 	 /*===========================================================*/ 
	 	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Pitch index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 	 /*===========================================================*/ 
 
		 if ((chan->fix_rate == RATE8_5K) ||  
		     (chan->fix_rate == RATE4_0K)) 
		   {  
		     if (chan->idx_SVS_deci == 1) 
		       { 
			 if (chan->fix_rate == RATE8_5K) 
			   { 
			     BIT_bitunpack (&chan->idx_pitch[0], 
					    (UNS_INT16 *) packedwords, 8, 
					    packw_ptr); 
			   } 
			 else if (chan->fix_rate == RATE4_0K) 
			   { 
			     BIT_bitunpack (&chan->idx_pitch[0], 
					    (UNS_INT16 *) packedwords, 7, 
					    packw_ptr); 
			   } 
		       } 
		     else 
		       { 
			 if (chan->fix_rate == RATE8_5K) 
			   { 
			     BIT_bitunpack (&chan->idx_pitch[0], 
					    (UNS_INT16 *) packedwords, 8,  
					    packw_ptr); 
			      
			     BIT_bitunpack (&chan->idx_pitch[1], 
					    (UNS_INT16 *) packedwords, 5, 
					    packw_ptr); 
			      
			     BIT_bitunpack (&chan->idx_pitch[2], 
					    (UNS_INT16 *) packedwords, 8, 
					    packw_ptr); 
			      
			     BIT_bitunpack (&chan->idx_pitch[3], 
					    (UNS_INT16 *) packedwords, 5, 
					    packw_ptr); 
			   } 
			 else if (chan->fix_rate == RATE4_0K) 
			   { 
			      
			      
			     for (i = 0; i < N_SF2; i++) 
			       { 
				 BIT_bitunpack (&chan->idx_pitch[i], 
						(UNS_INT16 *) packedwords, 7, 
						packw_ptr); 
			       } 
			   } 
		       } 
		   } 
		  
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Gains index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
	 switch (chan->fix_rate) 
	 	{ 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE8_5K: 
 
		   if (chan->idx_SVS_deci == 1) 
		     { 
		       BIT_bitunpack (&chan->idx_Gp_VQ, 
				      (UNS_INT16 *) packedwords, 6, packw_ptr); 
		        
		       BIT_bitunpack (&chan->idx_Gc_VQ, 
				      (UNS_INT16 *) packedwords, 10, packw_ptr); 
		     } 
		   else 
		     { 
		       for (i = 0; i < N_SF4; i++) 
			 BIT_bitunpack (&chan->idx_gainVQ[i], 
					(UNS_INT16 *) packedwords, 7, packw_ptr);  
		     } 
		   break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE4_0K: 
 
		   if (chan->idx_SVS_deci == 1) 
		     { 
		       BIT_bitunpack (&chan->idx_Gp_VQ, 
				      (UNS_INT16 *) packedwords, 4, packw_ptr); 
		        
		       BIT_bitunpack (&chan->idx_Gc_VQ, 
				      (UNS_INT16 *) packedwords, 8, packw_ptr); 
		     } 
		   else 
		     { 
		       for (i = 0; i < N_SF2; i++) 
			 BIT_bitunpack (&chan->idx_gainVQ[i], 
					(UNS_INT16 *) packedwords, 7, packw_ptr);  
		     } 
		   break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE2_0K: 
 
                   BIT_bitunpack (&chan->idx_gainVQ[0],  
				  (UNS_INT16 *) packedwords, 5, packw_ptr); 
                   BIT_bitunpack (&chan->idx_gainVQ[1],  
				  (UNS_INT16 *) packedwords, 6, packw_ptr); 
                   BIT_bitunpack (&chan->idx_gainVQ[2],  
				  (UNS_INT16 *) packedwords, 6, packw_ptr); 
                   BIT_bitunpack (&chan->idx_gainVQ[3],  
				  (UNS_INT16 *) packedwords, 2, packw_ptr); 
 
		   break; 
	    
		 case RATE0_8K:	 
		   BIT_bitunpack (&chan->idx_gainVQ[0], 
				  (UNS_INT16 *) packedwords, 5, packw_ptr); 
		   break; 
		    
		 default: nrerror("Invalid rate !!"); 
		   break; 
		} 
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Codeboks index ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
	 switch (chan->fix_rate) 
	 	{ 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE8_5K: 
 
		 if (chan->idx_SVS_deci == 1) 
		   { 
		     /*-------------------------------------------------------*/ 
		     /*                 Stationary voiced speech              */ 
		     /*-------------------------------------------------------*/ 
		      
		     for (i_sf = 0; i_sf < N_SF4; i_sf++) 
		       { 
			 /*---------------------------------------------------*/ 
			 /* 8 pulses CB : 6p x 3b + 2p x 4b + 4b signs =      */ 
			 /*                                       30 bits     */ 
			 /*---------------------------------------------------*/ 
			 	  
			 for (i = 0; i < 4; i++) 
			   BIT_bitunpack (&chan->idx_cpcbsign[i_sf][i],  
					  (UNS_INT16 *) packedwords, 1, 
					  packw_ptr); 
			  
			 BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
					(UNS_INT16 *) packedwords, 4,  
					packw_ptr); 
 
			 for (i = 1; i < 4; i++) 
			   BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
					  (UNS_INT16 *) packedwords, 3, 
					  packw_ptr); 
			  
			 BIT_bitunpack (&chan->idx_cpcb[i_sf][4], 
					(UNS_INT16 *) packedwords, 4,  
					packw_ptr); 
			  
			 for (i = 5; i < 8; i++) 
			   BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
					  (UNS_INT16 *) packedwords, 3, 
					  packw_ptr); 
		       } 
		   }				 
		 else 
		   { 
		      
		     /*-------------------------------------------------------*/ 
		     /*              Non-Stationary voiced speech             */ 
		     /*-------------------------------------------------------*/ 
		      
		     for (i_sf = 0; i_sf < N_SF4; i_sf++) 
		       { 
			 BIT_bitunpack (&chan->idx_subcpcb[i_sf][0], 
					(UNS_INT16 *) packedwords, 1,  
					packw_ptr); 
			  
			 if (chan->idx_subcpcb[i_sf][0] == 1) 
			   { 
			     /*-----------------------------------------------*/ 
			     /* 5 pulses CB : 3p x 4b + 2p x 3b + 3b signs    */ 
			     /*                = 21 bits                      */ 
			     /*-----------------------------------------------*/ 
 
			     for (i=0; i< 2; i++) 
			       BIT_bitunpack (&chan->idx_cpcbsign[i_sf][i], 
					      (UNS_INT16 *) packedwords, 1, 
					      packw_ptr); 
 
			     BIT_bitunpack (&chan->idx_cpcbsign[i_sf][4], 
					    (UNS_INT16 *) packedwords, 1, 
					    packw_ptr); 
			      
			     BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
					    (UNS_INT16 *) packedwords, 4, 
					    packw_ptr); 
 
			     BIT_bitunpack (&chan->idx_cpcb[i_sf][1], 
					    (UNS_INT16 *) packedwords, 3, 
					    packw_ptr); 
 
			     BIT_bitunpack (&chan->idx_cpcb[i_sf][2], 
					    (UNS_INT16 *) packedwords, 4, 
					    packw_ptr); 
 
			     BIT_bitunpack (&chan->idx_cpcb[i_sf][3], 
					    (UNS_INT16 *) packedwords, 3, 
					    packw_ptr); 
 
			     BIT_bitunpack (&chan->idx_cpcb[i_sf][4], 
					    (UNS_INT16 *) packedwords, 4, 
					    packw_ptr); 
			   } 
			 else 
			   { 
			     BIT_bitunpack (&chan->idx_subcpcb[i_sf][1], 
					    (UNS_INT16 *) packedwords, 1, 
					    packw_ptr); 
			      
			     /*-----------------------------------------------*/ 
			     /*         5 pulses CB : 5p x 3b + 5b signs      */ 
			     /*                = 20 bits                      */ 
			     /*-----------------------------------------------*/ 
 
			     for (i = 0; i < 5; i++) 
			       BIT_bitunpack (&chan->idx_cpcbsign[i_sf][i], 
					      (UNS_INT16 *) packedwords, 1, 
					      packw_ptr); 
			      
			     for (i = 0; i < 5; i++) 
			       BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
					      (UNS_INT16 *) packedwords, 3, 
					      packw_ptr); 
			      
			   } 
		       }  
		   }	  
		  
		 break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 case RATE4_0K:    
 
		   if (chan->idx_SVS_deci == 1) 
		     { 
		       /*-----------------------------------------------------*/ 
		       /*                Stationary voiced speech             */ 
		       /*-----------------------------------------------------*/ 
		        
		       for (i_sf = 0; i_sf < N_SF3; i_sf++) 
			 { 
			   BIT_bitunpack (&chan->idx_subcpcb[i_sf][0], 
					  (UNS_INT16 *) packedwords, 1, 
					  packw_ptr); 
			    
			   if (chan->idx_subcpcb[i_sf][0] == 1) 
			     { 
			       /*---------------------------------------------*/ 
			       /* 2 pulses CB: 2pulsesx5bits/pulse + 2 signs  */ 
			       /*                = 12 bits                    */ 
			       /*---------------------------------------------*/ 
			        
			       for (i = 0; i < 2; i++) 
				 BIT_bitunpack (&chan->idx_cpcbsign[i_sf][i], 
						(UNS_INT16 *) packedwords, 1, 
						packw_ptr); 
			        
			       for (i = 0; i < 2; i++) 
				 BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
						(UNS_INT16 *) packedwords, 5, 
						packw_ptr); 
			     } 
			   else 
			     { 
			       BIT_bitunpack (&chan->idx_subcpcb[i_sf][1], 
					      (UNS_INT16 *) packedwords, 1, 
					      packw_ptr); 
			        
			       if (chan->idx_subcpcb[i_sf][1] == 1) 
				 { 
				   /*----------------------------------------*/ 
				   /* 3 pulses CB: 3 pulses x                */ 
				   /*                  (4, 2 or 2)bits/pulse */ 
				   /*            + 3 signs = 11 bits         */ 
				   /*----------------------------------------*/ 
				    
				   for (i = 0; i < 3; i++) 
				    { 
				     BIT_bitunpack(&chan->idx_cpcbsign[i_sf][i], 
						   (UNS_INT16 *) packedwords, 1, 
						   packw_ptr); 
				    } 
				    
				   BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
						  (UNS_INT16 *) packedwords, 4, 
						  packw_ptr); 
				    
				   for (i = 1; i < 3; i++) 
				     {	 
				       BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
						      (UNS_INT16 *) packedwords, 
						      2, packw_ptr); 
				     }	 
				    
				 } 
			       else 
				 { 
				   /*----------------------------------------*/ 
				   /*   5 pulses CB:   11 bits               */ 
				   /*----------------------------------------*/ 
				    
				   for (i = 0; i < 5; i++) 
				    {	 
				     BIT_bitunpack(&chan->idx_cpcbsign[i_sf][i], 
						   (UNS_INT16 *) packedwords, 1, 
						   packw_ptr); 
				    } 
				    
				   BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
						  (UNS_INT16 *) packedwords, 2, 
						  packw_ptr); 
				    
				   for (i = 1; i < 5; i++) 
				     { 
				       BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
						      (UNS_INT16 *) packedwords, 
						      1, packw_ptr); 
				     } 
				 } 
			     } 
			    
			 } 
		     } 
		   else 
		     { 
		       /*-----------------------------------------------------*/ 
		       /*              Non-Stationary voiced speech           */ 
		       /*-----------------------------------------------------*/ 
		        
		       for (i_sf = 0; i_sf < N_SF2; i_sf++) 
			 { 
			   BIT_bitunpack (&chan->idx_subcpcb[i_sf][0], 
					  (UNS_INT16 *) packedwords, 1,  
					  packw_ptr); 
			    
			   if (chan->idx_subcpcb[i_sf][0] == 1) 
			     { 
			       /*---------------------------------------------*/ 
			       /*     2 pulses CB :  2pulses x 6.5bits/pulse  */ 
			       /*              + 1 sign = 12 bits             */ 
			       /*---------------------------------------------*/ 
 
			       BIT_bitunpack (&chan->idx_cpcbsign[i_sf][0], 
					      (UNS_INT16 *) packedwords, 1,  
					      packw_ptr); 
			        
			       BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
					      (UNS_INT16 *) packedwords, 13, 
					      packw_ptr); 
			        
			       /*---------------------------------------------*/ 
			       /* Inverse remap indices of upper part of      */ 
			       /* Gaussian codebook to the free index space of*/ 
			       /* the 2-pulse codebook                        */ 
			       /*---------------------------------------------*/ 
			        
			       /*---------------------------------------------*/ 
			       /* Test the configuration:                     */ 
			       /*     2 pulse codebook && free index space    */ 
			       /*---------------------------------------------*/ 
 
			       if (chan->idx_cpcb[i_sf][0] - NUMPOS2PULSE>= 0) 
				 { 
				   /*-----------------------------------------*/ 
				   /* Not really needed. Only advancement     */ 
				   /* of pointers is needed.                  */ 
				   /*-----------------------------------------*/ 
 
				   i = chan->idx_cpcb[i_sf][0] - NUMPOS2PULSE; 
				    
				   if(i >= DELTA_REMAP) 
				     chan->idx_cpcbsign[i_sf][1] = 1; 
				   else 
				     chan->idx_cpcbsign[i_sf][1] = 0; 
				    
				   chan->idx_cpcb[i_sf][0] = NUM11BITS + i - 
				     chan->idx_cpcbsign[i_sf][1]*DELTA_REMAP; 
				    
				   chan->idx_subcpcb[i_sf][0] = 0; 
				   chan->idx_subcpcb[i_sf][1] = 0; 
				    
				 } 
			     } 
			   else 
			     { 
			       BIT_bitunpack (&chan->idx_subcpcb[i_sf][1], 
					      (UNS_INT16 *) packedwords, 1,  
					      packw_ptr); 
			        
			       if (chan->idx_subcpcb[i_sf][1] == 1) 
				 { 
				   /*--------------------------------------*/ 
				   /* 3 pulses CB: 3 pulses x              */ 
				   /*     (4, 3, 3)bits/pulse + 3 signs    */ 
				   /*          = 13 bits                   */ 
				   /*--------------------------------------*/ 
				    
				   for (i = 0; i < 3; i++) 
				    { 
				     BIT_bitunpack(&chan->idx_cpcbsign[i_sf][i], 
						   (UNS_INT16 *) packedwords, 1, 
						   packw_ptr); 
				    } 
				    
				   BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
						  (UNS_INT16 *) packedwords, 4, 
						  packw_ptr); 
				    
				   for (i = 1; i < 3; i++) 
				     { 
				       BIT_bitunpack (&chan->idx_cpcb[i_sf][i], 
						      (UNS_INT16 *) packedwords, 
						      3, packw_ptr); 
				     }			        
				 } 
			       else   
				 { 
				   /*--------------------------------------*/ 
				   /*      Gaussian codebook, 13 bits      */ 
				   /*--------------------------------------*/ 
				    
				   for (i = 0; i < 2; i++) 
				    { 
				     BIT_bitunpack(&chan->idx_cpcbsign[i_sf][i], 
						   (UNS_INT16 *) packedwords, 1, 
						   packw_ptr); 
				    } 
				    
				   BIT_bitunpack (&chan->idx_cpcb[i_sf][0], 
						  (UNS_INT16 *) packedwords, 11, 
						  packw_ptr);	 
				 }						  
			     } 
			 } 
		     } 
		    
		   break; 
		    
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE2_0K :     
		   break; 
		    
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE0_8K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE0_8K :     
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*======================== ERROR ============================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 default: nrerror ("Invalid rate !!"); 
			break; 
		}	  
 
	 /*===================================================================*/ 
	 /*¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ Badrate flag ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤*/ 
	 /*===================================================================*/ 
 
         chan->badrate_flag=0; /* Setting badrate_flag to 0 as default */ 
	 switch (chan->fix_rate) 
	 	{ 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE8_5K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE8_5K :     
		        BIT_bitunpack (&chan->badrate_flag, 
				(UNS_INT16 *) packedwords, 1, packw_ptr); 
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE4_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE4_0K :     
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE2_0K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE2_0K :     
		        BIT_bitunpack (&chan->badrate_flag, 
				(UNS_INT16 *) packedwords, 1, packw_ptr); 
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*==================== case RATE0_8K ========================*/ 
		 /*-----------------------------------------------------------*/ 
		  
		 case RATE0_8K :     
			break; 
 
		 /*-----------------------------------------------------------*/ 
		 /*======================== ERROR ============================*/ 
		 /*-----------------------------------------------------------*/ 
 
		 default: nrerror ("Invalid rate !!"); 
			break; 
		}	  
 
	 /*-------------------------------------------------------------------*/ 
	  
	 return bfi; 
	  
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
 
#ifdef DATA 
/*===================================================================*/ 
/* FUNCTION      :  BIT_bits_to_data ().                             */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE       :  This function converts  bit-stream               */ 
/*                   into the data buffer                            */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*         _ (INT16     [])  packedwords: bit-stream.                */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*         _ (INT16     [])  data_buf:                               */ 
/*         _ (INT16     *)  check_data:                              */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*         _ (INT16     [])  packw_ptr:   pointer to the bit-stream. */ 
/*         _ (INT16 *   )    rate:                                   */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                            _ None.                                */ 
/*===================================================================*/ 
	 
void BIT_bits_to_data (INT16 packedwords [], INT16 packw_ptr [],  
		       INT16* data_buf,INT16  *rate, INT16 *check_data) 
	{ 
	  /*-----------------------------------------------------------------*/ 
 
	  INT16 i; 
	  INT16  svs; 
	  INT16 poss_illegal_id[2] ; 
	  INT16  save_packedwords[PACKWDSNUM]; 
	  
	  /*-----------------------------------------------------------------*/ 
          /*                 Save packedwords to get aux data                */ 
	  /*-----------------------------------------------------------------*/ 
 
	  cpy_svector(packedwords,save_packedwords,0,PACKWDSNUM-1); 
	  BIT_bitunpack (&svs, (UNS_INT16 *) packedwords, 1, packw_ptr);	 
 
	  /*-----------------------------------------------------------------*/ 
	  /*                         Check svs bit for 0                     */ 
	  /*-----------------------------------------------------------------*/ 
 
	  if (svs !=0 ) 
            return; 
          if (*rate==RATE8_5K)  
	    { 
	      packw_ptr[1]=6; 
	      packw_ptr[0]=16; 
 
	      /*-------------------------------------------------------------*/ 
	      /*                        Pull off bit 81                      */ 
	      /*-------------------------------------------------------------*/ 
 
	      BIT_bitunpack (&svs, (UNS_INT16 *) packedwords, 1, packw_ptr);	 
	    } 
 
	  /*-----------------------------------------------------------------*/ 
	  /*                           Get data_header                       */ 
	  /*-----------------------------------------------------------------*/ 
 
	  BIT_bitunpack (&data_buf[0], (UNS_INT16 *) packedwords, 3, packw_ptr); 
 
	  for (i = 1; i < DATA_SIZE-1; i++) 
	    BIT_bitunpack (&data_buf[i], (UNS_INT16 *) packedwords, 16, 
			   packw_ptr);	 
	  /*-----------------------------------------------------------------*/ 
	  /* Get last 16 bits for full rate or 13 bits for half rate and set */ 
          /*          check_data to 1 if illegal codeword is present         */ 
	  /*-----------------------------------------------------------------*/ 
 
          if (*rate==RATE8_5K)  
	    { 
	      BIT_bitunpack (&data_buf[i], (UNS_INT16 *) packedwords, 16,  
			     packw_ptr);	 
	      BIT_bitunpack(&poss_illegal_id[0], (UNS_INT16 *) packedwords,  
			    7, packw_ptr); 
	      BIT_bitunpack(&poss_illegal_id[1], (UNS_INT16 *) packedwords, 
			    15, packw_ptr); 
	      if ((poss_illegal_id[0]==illegal_full_id[0]) && 
		  (poss_illegal_id[1]==illegal_full_id[1]))   
		{ 
		  *check_data=1; 
		  /*---------------------------------------------------------*/ 
		  /*      Extract 5 more words if header==AUX_DATA_HEADER    */ 
		  /*---------------------------------------------------------*/ 
		   
		  if  (data_buf[0]==AUX_DATA_HEADER)  
		    { 
		      /*-----------------------------------------------------*/ 
		      /*                Go to top of frame                   */ 
		      /*-----------------------------------------------------*/ 
 
		      cpy_svector(save_packedwords,packedwords,0,PACKWDSNUM-1); 
		      packw_ptr[0] = 16; 
		      packw_ptr[1] = 1; 
		      BIT_bitunpack (&svs, (UNS_INT16 *) packedwords, 1,  
				     packw_ptr);	 
		      for (i = DATA_SIZE; i < DATA_SIZE+5; i++) 
			BIT_bitunpack (&data_buf[i], (UNS_INT16 *) packedwords, 
				       16, packw_ptr);	 
		    } 
                } 
	    }  
	  else  
	    { 
	      BIT_bitunpack (&data_buf[i], (UNS_INT16 *) packedwords, 13,  
			     packw_ptr);	 
	      BIT_bitunpack(&poss_illegal_id[0], (UNS_INT16 *) packedwords, 
			    15, packw_ptr); 
	      if (poss_illegal_id[0]==illegal_half_id) 
                (*check_data) = 1; 
	    } 
 
	  /*-----------------------------------------------------------------*/ 
	  /*      If speech+data set the rate to half and check_data to 2    */ 
	  /*-----------------------------------------------------------------*/ 
	   
	  if (*check_data==1)  
	    { 
	      if (*rate==RATE8_5K)  
		{ 
		  if (data_buf[0]!=AUX_DATA_HEADER)  
		    { 
		      *rate=RATE4_0K; 
                      *check_data=2; 
		    } 
		}  
            } 
 
	  /*-----------------------------------------------------------------*/ 
	  
	  return; 
	   
	  /*-----------------------------------------------------------------*/ 
        } 
#endif 
 
#endif 
 
/*============================================================================*/ 
/*----------------------------------- END ------------------------------------*/ 
/*============================================================================*/