float *innov;
spx_sig_t *innov;
int nb_decode(void *state, SpeexBits *bits, float *out){   DecState *st;   int i, sub;   int pitch;   float pitch_gain[3];   float ol_gain=0;   int ol_pitch=0;   float ol_pitch_coef=0;   int best_pitch=40;   float best_pitch_gain=0;   int wideband;   int m;   char *stack;   spx_coef_t *awk1, *awk2, *awk3;   float pitch_average=0;#ifdef EPIC_48K   int pitch_half[2];   int ol_pitch_id=0;#endif   st=(DecState*)state;   stack=st->stack;   if (st->encode_submode)   {#ifdef EPIC_48K   if (!st->lbr_48k) {#endif   /* Check if we're in DTX mode*/   if (!bits && st->dtx_enabled)   {      st->submodeID=0;   } else    {      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */      if (!bits)      {         nb_decode_lost(st, out, stack);         return 0;      }      /* Search for next narrowband block (handle requests, skip wideband blocks) */      do {         if (speex_bits_remaining(bits)<5)            return -1;         wideband = speex_bits_unpack_unsigned(bits, 1);         if (wideband) /* Skip wideband block (for compatibility) */         {            int submode;            int advance;            advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);            speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);            if (advance < 0)            {               speex_warning ("Invalid wideband mode encountered. Corrupted stream?");               return -2;            }             advance -= (SB_SUBMODE_BITS+1);            speex_bits_advance(bits, advance);                        if (speex_bits_remaining(bits)<5)               return -1;            wideband = speex_bits_unpack_unsigned(bits, 1);            if (wideband)            {               advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);               speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);               if (advance < 0)               {                  speex_warning ("Invalid wideband mode encountered: corrupted stream?");                  return -2;               }                advance -= (SB_SUBMODE_BITS+1);               speex_bits_advance(bits, advance);               wideband = speex_bits_unpack_unsigned(bits, 1);               if (wideband)               {                  speex_warning ("More than two wideband layers found: corrupted stream?");                  return -2;               }            }         }         if (speex_bits_remaining(bits)<4)            return -1;         /* FIXME: Check for overflow */         m = speex_bits_unpack_unsigned(bits, 4);         if (m==15) /* We found a terminator */         {            return -1;         } else if (m==14) /* Speex in-band request */         {            int ret = speex_inband_handler(bits, st->speex_callbacks, state);            if (ret)               return ret;         } else if (m==13) /* User in-band request */         {            int ret = st->user_callback.func(bits, state, st->user_callback.data);            if (ret)               return ret;         } else if (m>8) /* Invalid mode */         {            speex_warning("Invalid mode encountered: corrupted stream?");            return -2;         }            } while (m>8);      /* Get the sub-mode that was used */      st->submodeID = m;   }#ifdef EPIC_48K   }#endif   }   /* Shift all buffers by one frame */   speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   /* If null mode (no transmission), just set a couple things to zero*/   if (st->submodes[st->submodeID] == NULL)   {      spx_coef_t *lpc;      lpc = PUSH(stack,11, spx_coef_t);      bw_lpc(.93, st->interp_qlpc, lpc, 10);      /*for (i=0;i<st->frameSize;i++)        st->exc[i]=0;*/      {         float innov_gain=0;         float pgain=st->last_pitch_gain;         if (pgain>.6)            pgain=.6;         for (i=0;i<st->frameSize;i++)            innov_gain += st->innov[i]*st->innov[i];         innov_gain=sqrt(innov_gain/st->frameSize);         for (i=0;i<st->frameSize;i++)            st->exc[i]=0;         speex_rand_vec(innov_gain, st->exc, st->frameSize);      }      st->first=1;      /* Final signal synthesis from excitation */      iir_mem2(st->exc, lpc, st->frame, st->frameSize, st->lpcSize, st->mem_sp);      for (i=0;i<st->frameSize;i++)         out[i]=st->frame[i];      st->count_lost=0;      return 0;   }   /* Unquantize LSPs */   SUBMODE(lsp_unquant)(st->qlsp, st->lpcSize, bits);   /*Damp memory if a frame was lost and the LSP changed too much*/   if (st->count_lost)   {      float lsp_dist=0, fact;      for (i=0;i<st->lpcSize;i++)         lsp_dist += fabs(st->old_qlsp[i] - st->qlsp[i]);      fact = .6*exp(-.2*lsp_dist);      for (i=0;i<2*st->lpcSize;i++)         st->mem_sp[i] *= fact;   }   /* Handle first frame and lost-packet case */   if (st->first || st->count_lost)   {      for (i=0;i<st->lpcSize;i++)         st->old_qlsp[i] = st->qlsp[i];   }#ifdef EPIC_48K   if (st->lbr_48k) {      pitch_half[0] = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);      pitch_half[1] = pitch_half[0]+speex_bits_unpack_unsigned(bits, 2)-1;      ol_pitch_id = speex_bits_unpack_unsigned(bits, 3);      ol_pitch_coef=0.13514*ol_pitch_id;      {         int qe;         qe = speex_bits_unpack_unsigned(bits, 4);         ol_gain = exp((qe+2)/2.1);      }   } else {#endif   /* Get open-loop pitch estimation for low bit-rate pitch coding */   if (SUBMODE(lbr_pitch)!=-1)   {      ol_pitch = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);   }       if (SUBMODE(forced_pitch_gain))   {      int quant;      quant = speex_bits_unpack_unsigned(bits, 4);      ol_pitch_coef=0.066667*quant;   }      /* Get global excitation gain */   {      int qe;      qe = speex_bits_unpack_unsigned(bits, 5);      ol_gain = exp(qe/3.5);   }#ifdef EPIC_48K   }#endif   awk1=PUSH(stack, st->lpcSize+1, spx_coef_t);   awk2=PUSH(stack, st->lpcSize+1, spx_coef_t);   awk3=PUSH(stack, st->lpcSize+1, spx_coef_t);   if (st->submodeID==1)   {      int extra;      extra = speex_bits_unpack_unsigned(bits, 4);      if (extra==15)         st->dtx_enabled=1;      else         st->dtx_enabled=0;   }   if (st->submodeID>1)      st->dtx_enabled=0;   /*Loop on subframes */   for (sub=0;sub<st->nbSubframes;sub++)   {      int offset;      float *sp, *exc, tmp;#ifdef EPIC_48K      if (st->lbr_48k)      {         if (sub*2 < st->nbSubframes)            ol_pitch = pitch_half[0];         else            ol_pitch = pitch_half[1];      }#endif      /* Offset relative to start of frame */      offset = st->subframeSize*sub;      /* Original signal */      sp=st->frame+offset;      /* Excitation */      exc=st->exc+offset;      /* Excitation after post-filter*/      /* LSP interpolation (quantized and unquantized) */      tmp = (1.0 + sub)/st->nbSubframes;      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = (1-tmp)*st->old_qlsp[i] + tmp*st->qlsp[i];      /* Make sure the LSP's are stable */      lsp_enforce_margin(st->interp_qlsp, st->lpcSize, .002);      /* Compute interpolated LPCs (unquantized) */      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = cos(st->interp_qlsp[i]);      lsp_to_lpc(st->interp_qlsp, st->interp_qlpc, st->lpcSize, stack);      /* Compute enhanced synthesis filter */      if (st->lpc_enh_enabled)      {         float r=.9;                  float k1,k2,k3;         k1=SUBMODE(lpc_enh_k1);         k2=SUBMODE(lpc_enh_k2);         k3=(1-(1-r*k1)/(1-r*k2))/r;         if (!st->lpc_enh_enabled)         {            k1=k2;            k3=0;         }         bw_lpc(k1, st->interp_qlpc, awk1, st->lpcSize);         bw_lpc(k2, st->interp_qlpc, awk2, st->lpcSize);         bw_lpc(k3, st->interp_qlpc, awk3, st->lpcSize);               }      /* Compute analysis filter at w=pi */      tmp=1;      st->pi_gain[sub]=0;      for (i=0;i<=st->lpcSize;i++)      {         st->pi_gain[sub] += tmp*st->interp_qlpc[i];         tmp = -tmp;      }      st->pi_gain[sub] /= LPC_SCALING;      /* Reset excitation */      for (i=0;i<st->subframeSize;i++)         exc[i]=0;      /*Adaptive codebook contribution*/      if (SUBMODE(ltp_unquant))      {         int pit_min, pit_max;         /* Handle pitch constraints if any */         if (SUBMODE(lbr_pitch) != -1)         {            int margin;            margin = SUBMODE(lbr_pitch);            if (margin)            {/* GT - need optimization?               if (ol_pitch < st->min_pitch+margin-1)                  ol_pitch=st->min_pitch+margin-1;               if (ol_pitch > st->max_pitch-margin)                  ol_pitch=st->max_pitch-margin;               pit_min = ol_pitch-margin+1;               pit_max = ol_pitch+margin;*/               pit_min = ol_pitch-margin+1;               if (pit_min < st->min_pitch)		  pit_min = st->min_pitch;               pit_max = ol_pitch+margin;               if (pit_max > st->max_pitch)		  pit_max = st->max_pitch;            } else {               pit_min = pit_max = ol_pitch;            }         } else {            pit_min = st->min_pitch;            pit_max = st->max_pitch;         }#ifdef EPIC_48K         if (st->lbr_48k)         {             SUBMODE(ltp_unquant)(exc, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),                                   st->subframeSize, &pitch, &pitch_gain[0], bits, stack,                                   st->count_lost, offset, st->last_pitch_gain, ol_pitch_id);         } else {#endif             SUBMODE(ltp_unquant)(exc, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),                                   st->subframeSize, &pitch, &pitch_gain[0], bits, stack,                                   st->count_lost, offset, st->last_pitch_gain, 0);#ifdef EPIC_48K         }#endif                  /* If we had lost frames, check energy of last received frame */         if (st->count_lost && ol_gain < st->last_ol_gain)         {            float fact = ol_gain/(st->last_ol_gain+1);            for (i=0;i<st->subframeSize;i++)               exc[i]*=fact;         }         tmp = fabs(pitch_gain[0]+pitch_gain[1]+pitch_gain[2]);         tmp = fabs(pitch_gain[1]);         if (pitch_gain[0]>0)            tmp += pitch_gain[0];         else            tmp -= .5*pitch_gain[0];         if (pitch_gain[2]>0)            tmp += pitch_gain[2];         else            tmp -= .5*pitch_gain[0];         pitch_average += tmp;         if (tmp>best_pitch_gain)         {            best_pitch = pitch;	    best_pitch_gain = tmp;	    /*            best_pitch_gain = tmp*.9;	                if (best_pitch_gain>.85)                        best_pitch_gain=.85;*/         }      } else {         speex_error("No pitch prediction, what's wrong");      }            /* Unquantize the innovation */      {         int q_energy;         float ener;         float *innov;                  innov = st->innov+sub*st->subframeSize;         for (i=0;i<st->subframeSize;i++)            innov[i]=0;         /* Decode sub-frame gain correction */         if (SUBMODE(have_subframe_gain)==3)         {            q_energy = speex_bits_unpack_unsigned(bits, 3);            ener = ol_gain*exp(exc_gain_quant_scal3[q_energy]);         } else if (SUBMODE(have_subframe_gain)==1)         {            q_energy = speex_bits_unpack_unsigned(bits, 1);            ener = ol_gain*exp(exc_gain_quant_scal1[q_energy]);         } else {            ener = ol_gain;         }                           if (SUBMODE(innovation_unquant))         {            /*Fixed codebook contribution*/            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, stack);         } else {            speex_error("No fixed codebook");         }         /* De-normalize innovation and update excitation */         for (i=0;i<st->subframeSize;i++)            innov[i]*=ener;         /*Vocoder mode*/         if (st->submodeID==1)          {            float g=ol_pitch_coef;                        for (i=0;i<st->subframeSize;i++)               exc[i]=0;            while (st->voc_offset<st->subframeSize)            {               if (st->voc_offset>=0)                  exc[st->voc_offset]=SIG_SCALING*sqrt(1.0*ol_pitch);               st->voc_offset+=ol_pitch;            }            st->voc_offset -= st->subframeSize;            g=.5+2*(g-.6);            if (g<0)               g=0;            if (g>1)               g=1;            for (i=0;i<st->subframeSize;i++)            {               float exci=exc[i];               exc[i]=.8*g*exc[i]*ol_gain + .6*g*st->voc_m1*ol_gain + .5*g*innov[i] - .5*g*st->voc_m2 + (1-g)*innov[i];               st->voc_m1 = exci;               st->voc_m2=innov[i];               st->voc_mean = .95*st->voc_mean + .05*exc[i];               exc[i]-=st->voc_mean;            }         } else {            for (i=0;i<st->subframeSize;i++)               exc[i]+=innov[i];         }         /* Decode second codebook (only for some modes) */         if (SUBMODE(double_codebook))         {            char *tmp_stack=stack;            float *innov2 = PUSH(tmp_stack, st->subframeSize, float);            for (i=0;i<st->subframeSize;i++)               innov2[i]=0;            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, tmp_stack);            for (i=0;i<st->subframeSize;i++)               innov2[i]*=ener*(1/2.2);            for (i=0;i<st->subframeSize;i++)               exc[i] += innov2[i];         }      }      for (i=0;i<st->subframeSize;i++)         sp[i]=exc[i];      /* Signal synthesis */      if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0)         comb_filter(exc, sp, st->interp_qlpc, st->lpcSize, st->subframeSize,                              pitch, pitch_gain, SUBMODE(comb_gain), st->comb_mem);      if (st->lpc_enh_enabled)      {         /* Use enhanced LPC filter */         filter_mem2(sp, awk2, awk1, sp, st->subframeSize, st->lpcSize,                      st->mem_sp+st->lpcSize);         filter_mem2(sp, awk3, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,                      st->mem_sp);      } else {         /* Use regular filter */         for (i=0;i<st->lpcSize;i++)            st->mem_sp[st->lpcSize+i] = 0;         iir_mem2(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,                      st->mem_sp);      }   }      /*Copy output signal*/   for (i=0;i<st->frameSize;i++)     out[i]=st->frame[i]/SIG_SCALING;   /* Store the LSPs for interpolation in the next frame */   for (i=0;i<st->lpcSize;i++)      st->old_qlsp[i] = st->qlsp[i];   /* The next frame will not be the first (Duh!) */   st->first = 0;   st->count_lost=0;   st->last_pitch = best_pitch;   st->last_pitch_gain = .25*pitch_average;   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;   if (st->pitch_gain_buf_idx > 2) /* rollover */      st->pitch_gain_buf_idx = 0;   st->last_ol_gain = ol_gain;   return 0;}