diff options
-rw-r--r-- | libspeex/nb_celp.c | 449 | ||||
-rw-r--r-- | libspeex/nb_celp.h | 13 |
2 files changed, 218 insertions, 244 deletions
diff --git a/libspeex/nb_celp.c b/libspeex/nb_celp.c index 2ff0bef..999cc6e 100644 --- a/libspeex/nb_celp.c +++ b/libspeex/nb_celp.c @@ -128,15 +128,8 @@ void *nb_encoder_init(const SpeexMode *m) st->mode=m; - st->frameSize = mode->frameSize; - st->nbSubframes=mode->frameSize/mode->subframeSize; - st->subframeSize=mode->subframeSize; - st->windowSize = NB_WINDOW_SIZE; - st->lpcSize = mode->lpcSize; st->gamma1=mode->gamma1; st->gamma2=mode->gamma2; - st->min_pitch=mode->pitchStart; - st->max_pitch=mode->pitchEnd; st->lpc_floor = mode->lpc_floor; st->submodes=mode->submodes; @@ -160,8 +153,8 @@ void *nb_encoder_init(const SpeexMode *m) st->lagWindow = lag_window; st->first = 1; - for (i=0;i<st->lpcSize;i++) - st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1); + for (i=0;i<NB_ORDER;i++) + st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1); st->innov_rms_save = NULL; @@ -221,7 +214,7 @@ int nb_encoder_ctl(void *state, int request, void *ptr) switch(request) { case SPEEX_GET_FRAME_SIZE: - (*(spx_int32_t*)ptr) = st->frameSize; + (*(spx_int32_t*)ptr) = NB_FRAME_SIZE; break; case SPEEX_SET_LOW_MODE: case SPEEX_SET_MODE: @@ -324,9 +317,9 @@ int nb_encoder_ctl(void *state, int request, void *ptr) break; case SPEEX_GET_BITRATE: if (st->submodes[st->submodeID]) - (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; + (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE; else - (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; + (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE; break; case SPEEX_SET_SAMPLING_RATE: st->sampling_rate = (*(spx_int32_t*)ptr); @@ -339,13 +332,13 @@ int nb_encoder_ctl(void *state, int request, void *ptr) int i; st->bounded_pitch = 1; st->first = 1; - for (i=0;i<st->lpcSize;i++) - st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1); - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) + st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1); + for (i=0;i<NB_ORDER;i++) st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0; - for (i=0;i<st->frameSize+st->max_pitch+1;i++) + for (i=0;i<NB_FRAME_SIZE+NB_PITCH_END+1;i++) st->excBuf[i]=st->swBuf[i]=0; - for (i=0;i<st->windowSize-st->frameSize;i++) + for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++) st->winBuf[i]=0; } break; @@ -356,7 +349,7 @@ int nb_encoder_ctl(void *state, int request, void *ptr) (*(spx_int32_t*)ptr) = st->encode_submode; break; case SPEEX_GET_LOOKAHEAD: - (*(spx_int32_t*)ptr)=(st->windowSize-st->frameSize); + (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE); break; case SPEEX_SET_PLC_TUNING: st->plc_tuning = (*(spx_int32_t*)ptr); @@ -386,15 +379,15 @@ int nb_encoder_ctl(void *state, int request, void *ptr) { int i; spx_word32_t *g = (spx_word32_t*)ptr; - for (i=0;i<st->nbSubframes;i++) + for (i=0;i<NB_NB_SUBFRAMES;i++) g[i]=st->pi_gain[i]; } break; case SPEEX_GET_EXC: { int i; - for (i=0;i<st->nbSubframes;i++) - ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize); + for (i=0;i<NB_NB_SUBFRAMES;i++) + ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE); } break; #ifndef DISABLE_VBR @@ -451,52 +444,52 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) st=(EncState *)state; stack=st->stack; - ALLOC(lpc, st->lpcSize, spx_coef_t); - ALLOC(bw_lpc1, st->lpcSize, spx_coef_t); - ALLOC(bw_lpc2, st->lpcSize, spx_coef_t); - ALLOC(lsp, st->lpcSize, spx_lsp_t); - ALLOC(qlsp, st->lpcSize, spx_lsp_t); - ALLOC(interp_lsp, st->lpcSize, spx_lsp_t); - ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t); - ALLOC(interp_lpc, st->lpcSize, spx_coef_t); - ALLOC(interp_qlpc, st->lpcSize, spx_coef_t); - - st->exc = st->excBuf + st->max_pitch + 2; - st->sw = st->swBuf + st->max_pitch + 2; + ALLOC(lpc, NB_ORDER, spx_coef_t); + ALLOC(bw_lpc1, NB_ORDER, spx_coef_t); + ALLOC(bw_lpc2, NB_ORDER, spx_coef_t); + ALLOC(lsp, NB_ORDER, spx_lsp_t); + ALLOC(qlsp, NB_ORDER, spx_lsp_t); + ALLOC(interp_lsp, NB_ORDER, spx_lsp_t); + ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t); + ALLOC(interp_lpc, NB_ORDER, spx_coef_t); + ALLOC(interp_qlpc, NB_ORDER, spx_coef_t); + + st->exc = st->excBuf + NB_PITCH_END + 2; + st->sw = st->swBuf + NB_PITCH_END + 2; /* Move signals 1 frame towards the past */ - SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, st->max_pitch+2); - SPEEX_MOVE(st->swBuf, st->swBuf+st->frameSize, st->max_pitch+2); + SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2); + SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2); if (st->highpass_enabled) - highpass(in, in, st->frameSize, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp); + highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp); { VARDECL(spx_word16_t *w_sig); VARDECL(spx_word16_t *autocorr); - ALLOC(w_sig, st->windowSize, spx_word16_t); - ALLOC(autocorr, st->lpcSize+1, spx_word16_t); + ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t); + ALLOC(autocorr, NB_ORDER+1, spx_word16_t); /* Window for analysis */ - for (i=0;i<st->windowSize-st->frameSize;i++) + for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++) w_sig[i] = EXTRACT16(SHR32(MULT16_16(st->winBuf[i],st->window[i]),SIG_SHIFT)); - for (;i<st->windowSize;i++) - w_sig[i] = EXTRACT16(SHR32(MULT16_16(in[i-st->windowSize+st->frameSize],st->window[i]),SIG_SHIFT)); + for (;i<NB_WINDOW_SIZE;i++) + w_sig[i] = EXTRACT16(SHR32(MULT16_16(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]),SIG_SHIFT)); /* Compute auto-correlation */ - _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize); + _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE); autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */ /* Lag windowing: equivalent to filtering in the power-spectrum domain */ - for (i=0;i<st->lpcSize+1;i++) + for (i=0;i<NB_ORDER+1;i++) autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]); /* Levinson-Durbin */ - _spx_lpc(lpc, autocorr, st->lpcSize); + _spx_lpc(lpc, autocorr, NB_ORDER); /* LPC to LSPs (x-domain) transform */ - roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack); + roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack); /* Check if we found all the roots */ - if (roots!=st->lpcSize) + if (roots!=NB_ORDER) { /*If we can't find all LSP's, do some damage control and use previous filter*/ - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) { lsp[i]=st->old_lsp[i]; } @@ -508,17 +501,17 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */ { - int diff = st->windowSize-st->frameSize; + int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE; if (st->first) - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) interp_lsp[i] = lsp[i]; else - lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, st->nbSubframes, st->nbSubframes<<1); + lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, NB_NB_SUBFRAMES, NB_NB_SUBFRAMES<<1); - lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN); + lsp_enforce_margin(interp_lsp, NB_ORDER, LSP_MARGIN); /* Compute interpolated LPCs (unquantized) for whole frame*/ - lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack); + lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack); /*Open-loop pitch*/ @@ -531,14 +524,14 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) int nol_pitch[6]; spx_word16_t nol_pitch_coef[6]; - bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize); - bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize); + bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER); + bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER); SPEEX_COPY(st->sw, st->winBuf, diff); - SPEEX_COPY(st->sw+diff, in, st->frameSize-diff); - filter_mem16(st->sw, bw_lpc1, bw_lpc2, st->sw, st->frameSize, st->lpcSize, st->mem_sw_whole, stack); + SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff); + filter_mem16(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, NB_ORDER, st->mem_sw_whole, stack); - open_loop_nbest_pitch(st->sw, st->min_pitch, st->max_pitch, st->frameSize, + open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE, nol_pitch, nol_pitch_coef, 6, stack); ol_pitch=nol_pitch[0]; ol_pitch_coef = nol_pitch_coef[0]; @@ -568,12 +561,12 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /*Compute "real" excitation*/ SPEEX_COPY(st->exc, st->winBuf, diff); - SPEEX_COPY(st->exc+diff, in, st->frameSize-diff); - fir_mem16(st->exc, interp_lpc, st->exc, st->frameSize, st->lpcSize, st->mem_exc, stack); + SPEEX_COPY(st->exc+diff, in, NB_FRAME_SIZE-diff); + fir_mem16(st->exc, interp_lpc, st->exc, NB_FRAME_SIZE, NB_ORDER, st->mem_exc, stack); /* Compute open-loop excitation gain */ { - spx_word16_t g = compute_rms16(st->exc, st->frameSize); + spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE); if (st->submodeID!=1 && ol_pitch>0) ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14), spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16))))); @@ -583,8 +576,8 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) } #ifdef VORBIS_PSYCHO - SPEEX_MOVE(st->psy_window, st->psy_window+st->frameSize, 256-st->frameSize); - SPEEX_COPY(&st->psy_window[256-st->frameSize], in, st->frameSize); + SPEEX_MOVE(st->psy_window, st->psy_window+NB_FRAME_SIZE, 256-NB_FRAME_SIZE); + SPEEX_COPY(&st->psy_window[256-NB_FRAME_SIZE], in, NB_FRAME_SIZE); compute_curve(st->psy, st->psy_window, st->curve); /*print_vec(st->curve, 128, "curve");*/ if (st->first) @@ -596,7 +589,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) if (st->vbr_enabled||st->vad_enabled) { float lsp_dist=0; - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]); lsp_dist /= LSP_SCALING*LSP_SCALING; @@ -619,7 +612,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) st->vbr_quality=0; } - st->relative_quality = vbr_analysis(&st->vbr, in, st->frameSize, ol_pitch, GAIN_SCALING_1*ol_pitch_coef); + st->relative_quality = vbr_analysis(&st->vbr, in, NB_FRAME_SIZE, ol_pitch, GAIN_SCALING_1*ol_pitch_coef); /*if (delta_qual<0)*/ /* delta_qual*=.1*(3+st->vbr_quality);*/ if (st->vbr_enabled) @@ -719,18 +712,18 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /* If null mode (no transmission), just set a couple things to zero*/ if (st->submodes[st->submodeID] == NULL) { - for (i=0;i<st->frameSize;i++) + for (i=0;i<NB_FRAME_SIZE;i++) st->exc[i]=st->sw[i]=VERY_SMALL; - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->mem_sw[i]=0; st->first=1; st->bounded_pitch = 1; - SPEEX_COPY(st->winBuf, in+2*st->frameSize-st->windowSize, st->windowSize-st->frameSize); + SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE); /* Clear memory (no need to really compute it) */ - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->mem_sp[i] = 0; return 0; @@ -739,23 +732,23 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /* LSP Quantization */ if (st->first) { - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->old_lsp[i] = lsp[i]; } /*Quantize LSPs*/ #if 1 /*0 for unquantized*/ - SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits); + SUBMODE(lsp_quant)(lsp, qlsp, NB_ORDER, bits); #else - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) qlsp[i]=lsp[i]; #endif /*If we use low bit-rate pitch mode, transmit open-loop pitch*/ if (SUBMODE(lbr_pitch)!=-1) { - speex_bits_pack(bits, ol_pitch-st->min_pitch, 7); + speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7); } if (SUBMODE(forced_pitch_gain)) @@ -802,52 +795,52 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /* Special case for first frame */ if (st->first) { - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->old_qlsp[i] = qlsp[i]; } /* Target signal */ - ALLOC(target, st->subframeSize, spx_word16_t); - ALLOC(innov, st->subframeSize, spx_sig_t); - ALLOC(exc32, st->subframeSize, spx_word32_t); - ALLOC(ringing, st->subframeSize, spx_word16_t); - ALLOC(syn_resp, st->subframeSize, spx_word16_t); - ALLOC(real_exc, st->subframeSize, spx_word16_t); - ALLOC(mem, st->lpcSize, spx_mem_t); + ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t); + ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t); + ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t); + ALLOC(ringing, NB_SUBFRAME_SIZE, spx_word16_t); + ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t); + ALLOC(real_exc, NB_SUBFRAME_SIZE, spx_word16_t); + ALLOC(mem, NB_ORDER, spx_mem_t); /* Loop on sub-frames */ - for (sub=0;sub<st->nbSubframes;sub++) + for (sub=0;sub<NB_NB_SUBFRAMES;sub++) { int offset; spx_word16_t *sw; spx_word16_t *exc; int pitch; - int response_bound = st->subframeSize; + int response_bound = NB_SUBFRAME_SIZE; /* Offset relative to start of frame */ - offset = st->subframeSize*sub; + offset = NB_SUBFRAME_SIZE*sub; /* Excitation */ exc=st->exc+offset; /* Weighted signal */ sw=st->sw+offset; /* LSP interpolation (quantized and unquantized) */ - lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes); - lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes); + lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES); + lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES); /* Make sure the filters are stable */ - lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN); - lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN); + lsp_enforce_margin(interp_lsp, NB_ORDER, LSP_MARGIN); + lsp_enforce_margin(interp_qlsp, NB_ORDER, LSP_MARGIN); /* Compute interpolated LPCs (quantized and unquantized) */ - lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack); + lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack); - lsp_to_lpc(interp_qlsp, interp_qlpc, st->lpcSize, stack); + lsp_to_lpc(interp_qlsp, interp_qlpc, NB_ORDER, stack); /* Compute analysis filter gain at w=pi (for use in SB-CELP) */ { spx_word32_t pi_g=LPC_SCALING; - for (i=0;i<st->lpcSize;i+=2) + for (i=0;i<NB_ORDER;i+=2) { /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/ pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i]))); @@ -858,70 +851,70 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) #ifdef VORBIS_PSYCHO { float curr_curve[128]; - float fact = ((float)sub+1.0f)/st->nbSubframes; + float fact = ((float)sub+1.0f)/NB_NB_SUBFRAMES; for (i=0;i<128;i++) curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i]; curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10); } #else /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */ - bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize); - bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize); + bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER); + bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER); /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/ #endif /*FIXME: This will break if we change the window size */ - speex_assert(st->windowSize-st->frameSize == st->subframeSize); + speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE); { spx_word16_t *buf; if (sub==0) buf = st->winBuf; else - buf = &in[((sub-1)*st->subframeSize)]; - for (i=0;i<st->subframeSize;i++) + buf = &in[((sub-1)*NB_SUBFRAME_SIZE)]; + for (i=0;i<NB_SUBFRAME_SIZE;i++) real_exc[i] = sw[i] = buf[i]; } - fir_mem16(real_exc, interp_qlpc, real_exc, st->subframeSize, st->lpcSize, st->mem_exc2, stack); + fir_mem16(real_exc, interp_qlpc, real_exc, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_exc2, stack); if (st->complexity==0) response_bound >>= 1; - compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, st->lpcSize, stack); - for (i=response_bound;i<st->subframeSize;i++) + compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack); + for (i=response_bound;i<NB_SUBFRAME_SIZE;i++) syn_resp[i]=VERY_SMALL; /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */ - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) mem[i]=SHL32(st->mem_sp[i],1); - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) ringing[i] = VERY_SMALL; #ifdef SHORTCUTS2 - iir_mem16(ringing, interp_qlpc, ringing, response_bound, st->lpcSize, mem, stack); - for (i=0;i<st->lpcSize;i++) + iir_mem16(ringing, interp_qlpc, ringing, response_bound, NB_ORDER, mem, stack); + for (i=0;i<NB_ORDER;i++) mem[i]=SHL32(st->mem_sw[i],1); - filter_mem16(ringing, st->bw_lpc1, st->bw_lpc2, ringing, response_bound, st->lpcSize, mem, stack); - SPEEX_MEMSET(&ringing[response_bound], 0, st->subframeSize-response_bound); + filter_mem16(ringing, st->bw_lpc1, st->bw_lpc2, ringing, response_bound, NB_ORDER, mem, stack); + SPEEX_MEMSET(&ringing[response_bound], 0, NB_SUBFRAME_SIZE-response_bound); #else - iir_mem16(ringing, interp_qlpc, ringing, st->subframeSize, st->lpcSize, mem, stack); - for (i=0;i<st->lpcSize;i++) + iir_mem16(ringing, interp_qlpc, ringing, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack); + for (i=0;i<NB_ORDER;i++) mem[i]=SHL32(st->mem_sw[i],1); - filter_mem16(ringing, bw_lpc1, bw_lpc2, ringing, st->subframeSize, st->lpcSize, mem, stack); + filter_mem16(ringing, bw_lpc1, bw_lpc2, ringing, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack); #endif /* Compute weighted signal */ - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) mem[i]=st->mem_sw[i]; - filter_mem16(sw, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack); + filter_mem16(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack); if (st->complexity==0) - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->mem_sw[i]=mem[i]; /* Compute target signal (saturation prevents overflows on clipped input speech) */ - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(ringing[i],1)),32767)); /* Reset excitation */ - SPEEX_MEMSET(exc, 0, st->subframeSize); + SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE); /* If we have a long-term predictor (otherwise, something's wrong) */ speex_assert (SUBMODE(ltp_quant)); @@ -935,18 +928,18 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) margin = SUBMODE(lbr_pitch); if (margin) { - 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; + if (ol_pitch < NB_PITCH_START+margin-1) + ol_pitch=NB_PITCH_START+margin-1; + if (ol_pitch > NB_PITCH_END-margin) + ol_pitch=NB_PITCH_END-margin; pit_min = ol_pitch-margin+1; pit_max = ol_pitch+margin; } else { pit_min=pit_max=ol_pitch; } } else { - pit_min = st->min_pitch; - pit_max = st->max_pitch; + pit_min = NB_PITCH_START; + pit_max = NB_PITCH_END; } /* Force pitch to use only the current frame if needed */ @@ -956,19 +949,19 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /* Perform pitch search */ pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2, exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef, - st->lpcSize, st->subframeSize, bits, stack, + NB_ORDER, NB_SUBFRAME_SIZE, bits, stack, exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain); st->pitch[sub]=pitch; } /* Quantization of innovation */ - SPEEX_MEMSET(innov, 0, st->subframeSize); + SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE); /* FIXME: Make sure this is save from overflows (so far so good) */ - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) real_exc[i] = EXTRACT16(SUB32(EXTEND32(real_exc[i]), PSHR32(exc32[i],SIG_SHIFT-1))); - ener = SHL32(EXTEND32(compute_rms16(real_exc, st->subframeSize)),SIG_SHIFT); + ener = SHL32(EXTEND32(compute_rms16(real_exc, NB_SUBFRAME_SIZE)),SIG_SHIFT); /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */ #ifdef FIXED_POINT @@ -1003,61 +996,61 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /*printf ("%f %f\n", ener, ol_gain);*/ /* Normalize innovation */ - signal_div(target, target, ener, st->subframeSize); + signal_div(target, target, ener, NB_SUBFRAME_SIZE); /* Quantize innovation */ speex_assert (SUBMODE(innovation_quant)); { /* Codebook search */ SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2, - SUBMODE(innovation_params), st->lpcSize, st->subframeSize, + SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE, innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook)); /* De-normalize innovation and update excitation */ - signal_mul(innov, innov, ener, st->subframeSize); + signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE); - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767)); /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */ if (SUBMODE(double_codebook)) { char *tmp_stack=stack; VARDECL(spx_sig_t *innov2); - ALLOC(innov2, st->subframeSize, spx_sig_t); - SPEEX_MEMSET(innov2, 0, st->subframeSize); - for (i=0;i<st->subframeSize;i++) + ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t); + SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE); + for (i=0;i<NB_SUBFRAME_SIZE;i++) target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]); SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2, - SUBMODE(innovation_params), st->lpcSize, st->subframeSize, + SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE, innov2, syn_resp, bits, stack, st->complexity, 0); - signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), st->subframeSize); - for (i=0;i<st->subframeSize;i++) + signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE); + for (i=0;i<NB_SUBFRAME_SIZE;i++) innov[i] = ADD32(innov[i],innov2[i]); stack = tmp_stack; } - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767)); #ifndef DISABLE_WIDEBAND if (st->innov_rms_save) - st->innov_rms_save[sub] = compute_rms(innov, st->subframeSize); + st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE); #endif } /* Final signal synthesis from excitation */ - iir_mem16(exc, interp_qlpc, sw, st->subframeSize, st->lpcSize, st->mem_sp, stack); + iir_mem16(exc, interp_qlpc, sw, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp, stack); /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */ if (st->complexity!=0) - filter_mem16(sw, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack); + filter_mem16(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sw, stack); } /* Store the LSPs for interpolation in the next frame */ if (st->submodeID>=1) { - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->old_lsp[i] = lsp[i]; - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->old_qlsp[i] = qlsp[i]; } @@ -1078,7 +1071,7 @@ int nb_encode(void *state, void *vin, SpeexBits *bits) /* The next frame will not be the first (Duh!) */ st->first = 0; - SPEEX_COPY(st->winBuf, in+2*st->frameSize-st->windowSize, st->windowSize-st->frameSize); + SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE); if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0) st->bounded_pitch = 1; @@ -1114,19 +1107,13 @@ void *nb_decoder_init(const SpeexMode *m) st->first=1; /* Codec parameters, should eventually have several "modes"*/ - st->frameSize = mode->frameSize; - st->nbSubframes=mode->frameSize/mode->subframeSize; - st->subframeSize=mode->subframeSize; - st->lpcSize = mode->lpcSize; - st->min_pitch=mode->pitchStart; - st->max_pitch=mode->pitchEnd; st->submodes=mode->submodes; st->submodeID=mode->defaultSubmode; st->lpc_enh_enabled=1; - SPEEX_MEMSET(st->excBuf, 0, st->frameSize + st->max_pitch); + SPEEX_MEMSET(st->excBuf, 0, NB_FRAME_SIZE + NB_PITCH_END); st->last_pitch = 40; st->count_lost=0; @@ -1187,13 +1174,13 @@ int nb_decoder_ctl(void *state, int request, void *ptr) *((spx_int32_t*)ptr) = st->lpc_enh_enabled; break; case SPEEX_GET_FRAME_SIZE: - (*(spx_int32_t*)ptr) = st->frameSize; + (*(spx_int32_t*)ptr) = NB_FRAME_SIZE; break; case SPEEX_GET_BITRATE: if (st->submodes[st->submodeID]) - (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; + (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE; else - (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; + (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE; break; case SPEEX_SET_SAMPLING_RATE: st->sampling_rate = (*(spx_int32_t*)ptr); @@ -1220,9 +1207,9 @@ int nb_decoder_ctl(void *state, int request, void *ptr) case SPEEX_RESET_STATE: { int i; - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->mem_sp[i]=0; - for (i=0;i<st->frameSize + st->max_pitch + 1;i++) + for (i=0;i<NB_FRAME_SIZE + NB_PITCH_END + 1;i++) st->excBuf[i]=0; } break; @@ -1233,7 +1220,7 @@ int nb_decoder_ctl(void *state, int request, void *ptr) (*(spx_int32_t*)ptr) = st->encode_submode; break; case SPEEX_GET_LOOKAHEAD: - (*(spx_int32_t*)ptr)=st->subframeSize; + (*(spx_int32_t*)ptr)=NB_SUBFRAME_SIZE; break; case SPEEX_SET_HIGHPASS: st->highpass_enabled = (*(spx_int32_t*)ptr); @@ -1261,15 +1248,15 @@ int nb_decoder_ctl(void *state, int request, void *ptr) { int i; spx_word32_t *g = (spx_word32_t*)ptr; - for (i=0;i<st->nbSubframes;i++) + for (i=0;i<NB_NB_SUBFRAMES;i++) g[i]=st->pi_gain[i]; } break; case SPEEX_GET_EXC: { int i; - for (i=0;i<st->nbSubframes;i++) - ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*st->subframeSize, st->subframeSize); + for (i=0;i<NB_NB_SUBFRAMES;i++) + ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE); } break; case SPEEX_GET_DTX_STATUS: @@ -1311,7 +1298,7 @@ static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack) spx_word16_t innov_gain; spx_word16_t noise_gain; - st->exc = st->excBuf + 2*st->max_pitch + st->subframeSize + 6; + st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6; if (st->count_lost<10) fact = attenuation[st->count_lost]; @@ -1334,27 +1321,27 @@ static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack) #endif pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL; /* FIXME: This was rms of innovation (not exc) */ - innov_gain = compute_rms16(st->exc, st->frameSize); + innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE); noise_gain = MULT16_16_Q15(innov_gain, MULT16_16_Q15(fact, SUB16(Q15ONE,MULT16_16_Q15(pitch_gain,pitch_gain)))); /* Shift all buffers by one frame */ - SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, 2*st->max_pitch + st->subframeSize + 12); + SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12); pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT); - if (pitch_val > st->max_pitch) - pitch_val = st->max_pitch; - if (pitch_val < st->min_pitch) - pitch_val = st->min_pitch; - for (i=0;i<st->frameSize;i++) + if (pitch_val > NB_PITCH_END) + pitch_val = NB_PITCH_END; + if (pitch_val < NB_PITCH_START) + pitch_val = NB_PITCH_START; + for (i=0;i<NB_FRAME_SIZE;i++) { st->exc[i]= MULT16_16_Q15(pitch_gain, (st->exc[i-pitch_val]+VERY_SMALL)) + speex_rand(noise_gain, &st->seed); } - bw_lpc(QCONST16(.98,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize); - iir_mem16(&st->exc[-st->subframeSize], st->interp_qlpc, out, st->frameSize, - st->lpcSize, st->mem_sp, stack); - highpass(out, out, st->frameSize, HIGHPASS_NARROWBAND|HIGHPASS_OUTPUT, st->mem_hp); + bw_lpc(QCONST16(.98,15), st->interp_qlpc, st->interp_qlpc, NB_ORDER); + iir_mem16(&st->exc[-NB_SUBFRAME_SIZE], st->interp_qlpc, out, NB_FRAME_SIZE, + NB_ORDER, st->mem_sp, stack); + highpass(out, out, NB_FRAME_SIZE, HIGHPASS_NARROWBAND|HIGHPASS_OUTPUT, st->mem_hp); st->first = 0; st->count_lost++; @@ -1392,7 +1379,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) st=(DecState*)state; stack=st->stack; - st->exc = st->excBuf + 2*st->max_pitch + st->subframeSize + 6; + st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6; /* Check if we're in DTX mode*/ if (!bits && st->dtx_enabled) @@ -1486,19 +1473,19 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) } /* Shift all buffers by one frame */ - SPEEX_MOVE(st->excBuf, st->excBuf+st->frameSize, 2*st->max_pitch + st->subframeSize + 12); + SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12); /* If null mode (no transmission), just set a couple things to zero*/ if (st->submodes[st->submodeID] == NULL) { VARDECL(spx_coef_t *lpc); - ALLOC(lpc, st->lpcSize, spx_coef_t); - bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, st->lpcSize); + ALLOC(lpc, NB_ORDER, spx_coef_t); + bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER); { spx_word16_t innov_gain=0; /* FIXME: This was innov, not exc */ - innov_gain = compute_rms16(st->exc, st->frameSize); - for (i=0;i<st->frameSize;i++) + innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE); + for (i=0;i<NB_FRAME_SIZE;i++) st->exc[i]=speex_rand(innov_gain, &st->seed); } @@ -1506,30 +1493,30 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) st->first=1; /* Final signal synthesis from excitation */ - iir_mem16(st->exc, lpc, out, st->frameSize, st->lpcSize, st->mem_sp, stack); + iir_mem16(st->exc, lpc, out, NB_FRAME_SIZE, NB_ORDER, st->mem_sp, stack); st->count_lost=0; return 0; } - ALLOC(qlsp, st->lpcSize, spx_lsp_t); + ALLOC(qlsp, NB_ORDER, spx_lsp_t); /* Unquantize LSPs */ - SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits); + SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits); /*Damp memory if a frame was lost and the LSP changed too much*/ if (st->count_lost) { spx_word16_t fact; spx_word32_t lsp_dist=0; - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i]))); #ifdef FIXED_POINT fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2)); #else fact = .6*exp(-.2*lsp_dist); #endif - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]); } @@ -1537,14 +1524,14 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) /* Handle first frame and lost-packet case */ if (st->first || st->count_lost) { - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->old_qlsp[i] = qlsp[i]; } /* 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); + ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7); } if (SUBMODE(forced_pitch_gain)) @@ -1566,9 +1553,9 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) #endif } - ALLOC(ak, st->lpcSize, spx_coef_t); - ALLOC(innov, st->subframeSize, spx_sig_t); - ALLOC(exc32, st->subframeSize, spx_word32_t); + ALLOC(ak, NB_ORDER, spx_coef_t); + ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t); + ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t); if (st->submodeID==1) { @@ -1584,7 +1571,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) st->dtx_enabled=0; /*Loop on subframes */ - for (sub=0;sub<st->nbSubframes;sub++) + for (sub=0;sub<NB_NB_SUBFRAMES;sub++) { int offset; spx_word16_t *exc; @@ -1593,7 +1580,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) spx_word16_t tmp; /* Offset relative to start of frame */ - offset = st->subframeSize*sub; + offset = NB_SUBFRAME_SIZE*sub; /* Excitation */ exc=st->exc+offset; /* Original signal */ @@ -1603,7 +1590,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) /* Reset excitation */ - SPEEX_MEMSET(exc, 0, st->subframeSize); + SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE); /*Adaptive codebook contribution*/ speex_assert (SUBMODE(ltp_unquant)); @@ -1617,37 +1604,37 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) 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; + if (ol_pitch < NB_PITCH_START+margin-1) + ol_pitch=NB_PITCH_START+margin-1; + if (ol_pitch > NB_PITCH_END-margin) + ol_pitch=NB_PITCH_END-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; + if (pit_min < NB_PITCH_START) + pit_min = NB_PITCH_START; pit_max = ol_pitch+margin; - if (pit_max > st->max_pitch) - pit_max = st->max_pitch; + if (pit_max > NB_PITCH_END) + pit_max = NB_PITCH_END; } else { pit_min = pit_max = ol_pitch; } } else { - pit_min = st->min_pitch; - pit_max = st->max_pitch; + pit_min = NB_PITCH_START; + pit_max = NB_PITCH_END; } SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params), - st->subframeSize, &pitch, &pitch_gain[0], bits, stack, + NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack, st->count_lost, offset, st->last_pitch_gain, 0); /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat). We can probably be even more aggressive and limit to 15000 or so. */ - sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), st->subframeSize); + sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE); tmp = gain_3tap_to_1tap(pitch_gain); @@ -1667,7 +1654,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) int q_energy; spx_word32_t ener; - SPEEX_MEMSET(innov, 0, st->subframeSize); + SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE); /* Decode sub-frame gain correction */ if (SUBMODE(have_subframe_gain)==3) @@ -1685,30 +1672,30 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) speex_assert (SUBMODE(innovation_unquant)); { /*Fixed codebook contribution*/ - SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, stack, &st->seed); + SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed); /* De-normalize innovation and update excitation */ - signal_mul(innov, innov, ener, st->subframeSize); + signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE); /* Decode second codebook (only for some modes) */ if (SUBMODE(double_codebook)) { char *tmp_stack=stack; VARDECL(spx_sig_t *innov2); - ALLOC(innov2, st->subframeSize, spx_sig_t); - SPEEX_MEMSET(innov2, 0, st->subframeSize); - SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, stack, &st->seed); - signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), st->subframeSize); - for (i=0;i<st->subframeSize;i++) + ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t); + SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE); + SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed); + signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE); + for (i=0;i<NB_SUBFRAME_SIZE;i++) innov[i] = ADD32(innov[i], innov2[i]); stack = tmp_stack; } - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767)); /*print_vec(exc, 40, "innov");*/ if (innov_save) { - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT)); } } @@ -1723,8 +1710,8 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) if (g>GAIN_SCALING) g=GAIN_SCALING; - SPEEX_MEMSET(exc, 0, st->subframeSize); - while (st->voc_offset<st->subframeSize) + SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE); + while (st->voc_offset<NB_SUBFRAME_SIZE) { /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain; Not quite sure why we need the factor of two in the sqrt */ @@ -1732,9 +1719,9 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) exc[st->voc_offset]=MULT16_16(spx_sqrt(MULT16_16_16(2,ol_pitch)),EXTRACT16(PSHR32(MULT16_16(g,PSHR32(ol_gain,SIG_SHIFT)),6))); st->voc_offset+=ol_pitch; } - st->voc_offset -= st->subframeSize; + st->voc_offset -= NB_SUBFRAME_SIZE; - for (i=0;i<st->subframeSize;i++) + for (i=0;i<NB_SUBFRAME_SIZE;i++) { spx_word16_t exci=exc[i]; exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)), @@ -1751,14 +1738,14 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) } } - ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t); + ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t); if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost) { - multicomb(st->exc-st->subframeSize, out, st->interp_qlpc, st->lpcSize, 2*st->subframeSize, best_pitch, 40, SUBMODE(comb_gain), stack); - multicomb(st->exc+st->subframeSize, out+2*st->subframeSize, st->interp_qlpc, st->lpcSize, 2*st->subframeSize, best_pitch, 40, SUBMODE(comb_gain), stack); + multicomb(st->exc-NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack); + multicomb(st->exc+NB_SUBFRAME_SIZE, out+2*NB_SUBFRAME_SIZE, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack); } else { - SPEEX_COPY(out, &st->exc[-st->subframeSize], st->frameSize); + SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE); } /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */ @@ -1767,7 +1754,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) spx_word16_t exc_ener; spx_word32_t gain32; spx_word16_t gain; - exc_ener = compute_rms16 (st->exc, st->frameSize); + exc_ener = compute_rms16 (st->exc, NB_FRAME_SIZE); gain32 = PDIV32(ol_gain, ADD16(exc_ener,1)); #ifdef FIXED_POINT if (gain32 > 32767) @@ -1778,39 +1765,39 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) gain32=2; gain = gain32; #endif - for (i=0;i<st->frameSize;i++) + for (i=0;i<NB_FRAME_SIZE;i++) { st->exc[i] = MULT16_16_Q14(gain, st->exc[i]); - out[i]=st->exc[i-st->subframeSize]; + out[i]=st->exc[i-NB_SUBFRAME_SIZE]; } } /*Loop on subframes */ - for (sub=0;sub<st->nbSubframes;sub++) + for (sub=0;sub<NB_NB_SUBFRAMES;sub++) { int offset; spx_word16_t *sp; spx_word16_t *exc; /* Offset relative to start of frame */ - offset = st->subframeSize*sub; + offset = NB_SUBFRAME_SIZE*sub; /* Original signal */ sp=out+offset; /* Excitation */ exc=st->exc+offset; /* LSP interpolation (quantized and unquantized) */ - lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes); + lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES); /* Make sure the LSP's are stable */ - lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN); + lsp_enforce_margin(interp_qlsp, NB_ORDER, LSP_MARGIN); /* Compute interpolated LPCs (unquantized) */ - lsp_to_lpc(interp_qlsp, ak, st->lpcSize, stack); + lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack); /* Compute analysis filter at w=pi */ { spx_word32_t pi_g=LPC_SCALING; - for (i=0;i<st->lpcSize;i+=2) + for (i=0;i<NB_ORDER;i+=2) { /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/ pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i]))); @@ -1818,17 +1805,17 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) st->pi_gain[sub] = pi_g; } - iir_mem16(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, + iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp, stack); - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->interp_qlpc[i] = ak[i]; } if (st->highpass_enabled) - highpass(out, out, st->frameSize, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp); - /*for (i=0;i<st->frameSize;i++) + highpass(out, out, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp); + /*for (i=0;i<NB_FRAME_SIZE;i++) printf ("%d\n", (int)st->frame[i]);*/ /* Tracking output level */ @@ -1840,7 +1827,7 @@ int nb_decode(void *state, SpeexBits *bits, void *vout) /*printf ("%f %f %f %d\n", og, st->min_level, st->max_level, update);*/ /* Store the LSPs for interpolation in the next frame */ - for (i=0;i<st->lpcSize;i++) + for (i=0;i<NB_ORDER;i++) st->old_qlsp[i] = qlsp[i]; /* The next frame will not be the first (Duh!) */ diff --git a/libspeex/nb_celp.h b/libspeex/nb_celp.h index 37cd283..39be0b1 100644 --- a/libspeex/nb_celp.h +++ b/libspeex/nb_celp.h @@ -60,13 +60,6 @@ typedef struct EncState { const SpeexMode *mode; /**< Mode corresponding to the state */ int first; /**< Is this the first frame? */ - int frameSize; /**< Size of frames */ - int subframeSize; /**< Size of sub-frames */ - int nbSubframes; /**< Number of sub-frames */ - int windowSize; /**< Analysis (LPC) window length */ - int lpcSize; /**< LPC order */ - int min_pitch; /**< Minimum pitch value allowed */ - int max_pitch; /**< Maximum pitch value allowed */ spx_word32_t cumul_gain; /**< Product of previously used pitch gains (Q10) */ int bounded_pitch; /**< Next frame should not rely on previous frames for pitch */ @@ -134,12 +127,6 @@ typedef struct DecState { const SpeexMode *mode; /**< Mode corresponding to the state */ int first; /**< Is this the first frame? */ int count_lost; /**< Was the last frame lost? */ - int frameSize; /**< Size of frames */ - int subframeSize; /**< Size of sub-frames */ - int nbSubframes; /**< Number of sub-frames */ - int lpcSize; /**< LPC order */ - int min_pitch; /**< Minimum pitch value allowed */ - int max_pitch; /**< Maximum pitch value allowed */ spx_int32_t sampling_rate; spx_word16_t last_ol_gain; /**< Open-loop gain for previous frame */ |