/**************************************************************************
*
* ROUTINE
*				dcodtau
*
* FUNCTION
*				 Decodes all pitch tau values for a frame
*
*
* SYNOPSIS
*				subroutine dcodtau(taubits,taudelta,bitsum1,bitsum2,bitpointer,
*								   nn,stream,pddecode,pdtabi,taus)
*
*	formal
*
*						data	I/O
*		name			type	type	function
*		-------------------------------------------------------------------
*		taubits 		int 	i		number of bits for quantization
*		taudelta		int 	i		number of bits for even frame delta
*		bitsum1 		int 	i		number of bits for odd subframes
*		bitsum2 		int 	i		number of bits for even subframes
*		bitpointer		int 	i/o 	number of bits used
*		nn				int 	i		number of subframes/frame
*		stream			short	i		bit stream
*		pddecode		real	i		pitch delay permutation table
*		pdtabi			int 	i		pitch delay table index
*       taus            float   o       vector of pitch delays "taus"
*
*	external
*
*		name			type	type	function
*		-------------------------------------------------------------------
*
*		plevel1 		int 			number of full search pitch delays
*		plevel2 		int 			number of delta search pitch delays
*		pdelay[]		float			pitch delay coding table
*
***************************************************************************
*
* DESCRIPTION
*
*				This routine converts the pitch delay bits of the 
*		bitstream to actual delay values.  These delays are output
*		to the main routine in taus(i), one delay for each of four
*		subframes.
*
***************************************************************************
*
* CALLED BY
*
*		celp
*
* CALLS
*
*		unpack
*
***************************************************************************
*
* REFERENCES
*
*
**************************************************************************/

static void dcodtau(int taubits, int taudelta, int a_bitsum1,
	int a_bitsum2, int *a_bitpointer, int nn, short a_stream[],
	const float pddecode[], int a_pdtabi[], float a_taus[])
{
  int i, l_pointer, tptr, mxptr, mnptr;
#ifdef CELP_USE_CONTEXT
#define lptr	(ctx->DCODTAU_lptr)
#else
  static int lptr = 0;
#endif

  l_pointer = *a_bitpointer;
  for (i = 0; i < nn; i++)
  {
	if (((i + 1) % 2) != 0)
	{
	  unpack(a_stream, taubits, &tptr, &l_pointer);
	  a_taus[i] = pddecode[tptr];
	  l_pointer += a_bitsum1 - taubits;
	}
	else
	{
	  unpack(a_stream, taudelta, &tptr, &l_pointer);
	  l_pointer += a_bitsum2 - taudelta;
	  mnptr = lptr - (plevel2 / 2 - 1);
	  mxptr = lptr + (plevel2 / 2);
	  if (mnptr < 0)
		mnptr = 0;
	  if (mxptr > plevel1 - 1)
		mnptr = plevel1 - plevel2;
	  a_taus[i] = pdelay[tptr + mnptr];
	}
	lptr = a_pdtabi[tptr];
  }
  *a_bitpointer += taubits;
}
#ifdef CELP_USE_CONTEXT
#undef lptr
#endif
