61
63
#define SUBMODE(x) st->submodes[st->submodeID]->x
65
/* Default size for the encoder and decoder stack (can be changed at compile time).
66
This does not apply when using variable-size arrays or alloca. */
68
#define NB_ENC_STACK (8000*sizeof(spx_sig_t))
72
#define NB_DEC_STACK (4000*sizeof(spx_sig_t))
65
77
const spx_word32_t ol_gain_table[32]={18900, 25150, 33468, 44536, 59265, 78865, 104946, 139653, 185838, 247297, 329081, 437913, 582736, 775454, 1031906, 1373169, 1827293, 2431601, 3235761, 4305867, 5729870, 7624808, 10146425, 13501971, 17967238, 23909222, 31816294, 42338330, 56340132, 74972501, 99766822, 132760927};
127
141
st->lbr_48k=mode->lbr48k;
145
st->psy = vorbis_psy_init(8000, 128);
146
st->curve = speex_alloc(64*sizeof(float));
147
st->old_curve = speex_alloc(64*sizeof(float));
130
150
/* Allocating input buffer */
131
st->inBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
132
st->frame = st->inBuf + st->bufSize - st->windowSize;
151
st->inBuf = speex_alloc((st->windowSize)*sizeof(spx_sig_t));
152
st->frame = st->inBuf;
133
153
/* Allocating excitation buffer */
134
st->excBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
135
st->exc = st->excBuf + st->bufSize - st->windowSize;
136
st->swBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
137
st->sw = st->swBuf + st->bufSize - st->windowSize;
139
st->exc2Buf = PUSH(st->stack, st->bufSize, spx_sig_t);
140
st->exc2 = st->exc2Buf + st->bufSize - st->windowSize;
142
st->innov = PUSH(st->stack, st->frameSize, spx_sig_t);
154
st->excBuf = speex_alloc((mode->frameSize+mode->pitchEnd+1)*sizeof(spx_sig_t));
155
st->exc = st->excBuf + mode->pitchEnd + 1;
156
st->swBuf = speex_alloc((mode->frameSize+mode->pitchEnd+1)*sizeof(spx_sig_t));
157
st->sw = st->swBuf + mode->pitchEnd + 1;
159
st->innov = speex_alloc((st->frameSize)*sizeof(spx_sig_t));
144
161
/* Asymmetric "pseudo-Hamming" window */
146
163
int part1, part2;
147
164
part1=st->frameSize - (st->subframeSize>>1);
148
165
part2=(st->frameSize>>1) + (st->subframeSize>>1);
149
st->window = PUSH(st->stack, st->windowSize, spx_word16_t);
166
st->window = speex_alloc((st->windowSize)*sizeof(spx_word16_t));
150
167
for (i=0;i<part1;i++)
151
168
st->window[i]=(spx_word16_t)(SIG_SCALING*(.54-.46*cos(M_PI*i/part1)));
152
169
for (i=0;i<part2;i++)
153
170
st->window[part1+i]=(spx_word16_t)(SIG_SCALING*(.54+.46*cos(M_PI*i/part2)));
155
172
/* Create the window for autocorrelation (lag-windowing) */
156
st->lagWindow = PUSH(st->stack, st->lpcSize+1, spx_word16_t);
173
st->lagWindow = speex_alloc((st->lpcSize+1)*sizeof(spx_word16_t));
157
174
for (i=0;i<st->lpcSize+1;i++)
158
175
st->lagWindow[i]=16384*exp(-.5*sqr(2*M_PI*st->lag_factor*i));
160
st->autocorr = PUSH(st->stack, st->lpcSize+1, spx_word16_t);
162
st->buf2 = PUSH(st->stack, st->windowSize, spx_sig_t);
164
st->lpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
165
st->interp_lpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
166
st->interp_qlpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
167
st->bw_lpc1 = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
168
st->bw_lpc2 = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
170
st->lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
171
st->qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
172
st->old_lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
173
st->old_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
174
st->interp_lsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
175
st->interp_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
177
st->autocorr = speex_alloc((st->lpcSize+1)*sizeof(spx_word16_t));
179
st->lpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
180
st->interp_lpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
181
st->interp_qlpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
182
st->bw_lpc1 = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
183
st->bw_lpc2 = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));
185
st->lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
186
st->qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
187
st->old_lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
188
st->old_qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
189
st->interp_lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
190
st->interp_qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
178
193
for (i=0;i<st->lpcSize;i++)
180
195
st->lsp[i]=LSP_SCALING*(M_PI*((float)(i+1)))/(st->lpcSize+1);
183
st->mem_sp = PUSH(st->stack, st->lpcSize, spx_mem_t);
184
st->mem_sw = PUSH(st->stack, st->lpcSize, spx_mem_t);
185
st->mem_sw_whole = PUSH(st->stack, st->lpcSize, spx_mem_t);
186
st->mem_exc = PUSH(st->stack, st->lpcSize, spx_mem_t);
188
st->pi_gain = PUSH(st->stack, st->nbSubframes, spx_word32_t);
190
st->pitch = PUSH(st->stack, st->nbSubframes, int);
192
st->vbr = PUSHS(st->stack, VBRState);
198
st->mem_sp = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
199
st->mem_sw = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
200
st->mem_sw_whole = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
201
st->mem_exc = speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
st->pi_gain = speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
205
st->pitch = speex_alloc((st->nbSubframes)*sizeof(int));
207
st->vbr = speex_alloc(sizeof(VBRState));
193
208
vbr_init(st->vbr);
194
209
st->vbr_quality = 8;
195
210
st->vbr_enabled = 0;
213
229
EncState *st=(EncState *)state;
214
230
/* Free all allocated memory */
231
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
232
speex_free_scratch(st->stack);
235
speex_free (st->inBuf);
236
speex_free (st->excBuf);
237
speex_free (st->innov);
238
speex_free (st->interp_qlpc);
239
speex_free (st->qlsp);
240
speex_free (st->old_qlsp);
241
speex_free (st->interp_qlsp);
242
speex_free (st->swBuf);
244
speex_free (st->window);
245
speex_free (st->lagWindow);
246
speex_free (st->autocorr);
247
speex_free (st->lpc);
248
speex_free (st->lsp);
250
speex_free (st->interp_lpc);
251
speex_free (st->bw_lpc1);
252
speex_free (st->bw_lpc2);
253
speex_free (st->old_lsp);
254
speex_free (st->interp_lsp);
255
speex_free (st->mem_sp);
256
speex_free (st->mem_sw);
257
speex_free (st->mem_sw_whole);
258
speex_free (st->mem_exc);
259
speex_free (st->pi_gain);
260
speex_free (st->pitch);
216
262
vbr_destroy(st->vbr);
263
speex_free (st->vbr);
266
vorbis_psy_destroy(st->psy);
267
speex_free (st->curve);
268
speex_free (st->old_curve);
218
271
/*Free state memory... should be last*/
243
297
/* Copy new data in input buffer */
244
speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
298
speex_move(st->inBuf, st->inBuf+st->frameSize, (st->windowSize-st->frameSize)*sizeof(spx_sig_t));
245
299
for (i=0;i<st->frameSize;i++)
246
st->inBuf[st->bufSize-st->frameSize+i] = SHL((int)in[i], SIG_SHIFT);
300
st->inBuf[st->windowSize-st->frameSize+i] = SHL32(EXTEND32(in[i]), SIG_SHIFT);
248
302
/* Move signals 1 frame towards the past */
249
speex_move(st->exc2Buf, st->exc2Buf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
250
speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
251
speex_move(st->swBuf, st->swBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
303
speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch+1)*sizeof(spx_sig_t));
304
speex_move(st->swBuf, st->swBuf+st->frameSize, (st->max_pitch+1)*sizeof(spx_sig_t));
256
w_sig = PUSH(stack, st->windowSize, spx_word16_t);
307
VARDECL(spx_word16_t *w_sig);
308
ALLOC(w_sig, st->windowSize, spx_word16_t);
257
309
/* Window for analysis */
258
310
for (i=0;i<st->windowSize;i++)
259
w_sig[i] = SHR(MULT16_16(SHR((spx_word32_t)(st->frame[i]),SIG_SHIFT),st->window[i]),SIG_SHIFT);
311
w_sig[i] = EXTRACT16(SHR32(MULT16_16(EXTRACT16(SHR32(st->frame[i],SIG_SHIFT)),st->window[i]),SIG_SHIFT));
261
313
/* Compute auto-correlation */
262
314
_spx_autocorr(w_sig, st->autocorr, st->lpcSize+1, st->windowSize);
264
st->autocorr[0] = (spx_word16_t) (st->autocorr[0]*st->lpc_floor); /* Noise floor in auto-correlation domain */
316
st->autocorr[0] = ADD16(st->autocorr[0],MULT16_16_Q15(st->autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
266
318
/* Lag windowing: equivalent to filtering in the power-spectrum domain */
267
319
for (i=0;i<st->lpcSize+1;i++)
268
320
st->autocorr[i] = MULT16_16_Q14(st->autocorr[i],st->lagWindow[i]);
270
322
/* Levinson-Durbin */
271
_spx_lpc(st->lpc+1, st->autocorr, st->lpcSize);
272
st->lpc[0]=(spx_coef_t)LPC_SCALING;
323
_spx_lpc(st->lpc, st->autocorr, st->lpcSize);
274
325
/* LPC to LSPs (x-domain) transform */
275
326
roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 15, LSP_DELTA1, stack);
627
686
/* Filter response */
628
res = PUSH(stack, st->subframeSize, spx_sig_t);
687
ALLOC(res, st->subframeSize, spx_sig_t);
629
688
/* Target signal */
630
target = PUSH(stack, st->subframeSize, spx_sig_t);
631
syn_resp = PUSH(stack, st->subframeSize, spx_sig_t);
632
mem = PUSH(stack, st->lpcSize, spx_mem_t);
633
orig = PUSH(stack, st->frameSize, spx_sig_t);
634
for (i=0;i<st->frameSize;i++)
635
orig[i]=st->frame[i];
689
ALLOC(target, st->subframeSize, spx_sig_t);
690
ALLOC(syn_resp, st->subframeSize, spx_word16_t);
691
ALLOC(real_exc, st->subframeSize, spx_sig_t);
692
ALLOC(mem, st->lpcSize, spx_mem_t);
637
694
/* Loop on sub-frames */
638
695
for (sub=0;sub<st->nbSubframes;sub++)
641
spx_sig_t *sp, *sw, *exc, *exc2;
698
spx_sig_t *sp, *sw, *exc;
700
int response_bound = st->subframeSize;
679
733
/* Compute analysis filter gain at w=pi (for use in SB-CELP) */
681
spx_word32_t pi_g=st->interp_qlpc[0];
682
for (i=1;i<=st->lpcSize;i+=2)
735
spx_word32_t pi_g=LPC_SCALING;
736
for (i=0;i<st->lpcSize;i+=2)
684
pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];
738
/*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/
739
pi_g = ADD32(pi_g, SUB32(st->interp_qlpc[i+1],st->interp_qlpc[i]));
686
741
st->pi_gain[sub] = pi_g;
746
float curr_curve[64];
747
float fact = ((float)sub+1.0f)/st->nbSubframes;
749
curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
750
curve_to_lpc(st->psy, curr_curve, st->bw_lpc1, st->bw_lpc2, 10);
690
753
/* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
691
754
bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);
692
755
if (st->gamma2>=0)
697
760
for (i=1;i<=st->lpcSize;i++)
698
761
st->bw_lpc2[i]=0;
701
/* Compute impulse response of A(z/g1) / ( A(z)*A(z/g2) )*/
702
765
for (i=0;i<st->subframeSize;i++)
705
syn_percep_zero(exc, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
766
real_exc[i] = exc[i];
768
if (st->complexity==0)
769
response_bound >>= 1;
770
compute_impulse_response(st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, syn_resp, response_bound, st->lpcSize, stack);
771
for (i=response_bound;i<st->subframeSize;i++)
772
syn_resp[i]=VERY_SMALL;
707
774
/* Reset excitation */
708
775
for (i=0;i<st->subframeSize;i++)
709
776
exc[i]=VERY_SMALL;
710
for (i=0;i<st->subframeSize;i++)
713
778
/* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
714
779
for (i=0;i<st->lpcSize;i++)
715
780
mem[i]=st->mem_sp[i];
782
iir_mem2(exc, st->interp_qlpc, exc, response_bound, st->lpcSize, mem);
783
for (i=0;i<st->lpcSize;i++)
784
mem[i]=st->mem_sw[i];
785
filter_mem2(exc, st->bw_lpc1, st->bw_lpc2, res, response_bound, st->lpcSize, mem);
786
for (i=response_bound;i<st->subframeSize;i++)
716
789
iir_mem2(exc, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, mem);
718
790
for (i=0;i<st->lpcSize;i++)
719
791
mem[i]=st->mem_sw[i];
720
792
filter_mem2(exc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, mem);
722
795
/* Compute weighted signal */
723
796
for (i=0;i<st->lpcSize;i++)
724
797
mem[i]=st->mem_sw[i];
725
798
filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, mem);
800
if (st->complexity==0)
801
for (i=0;i<st->lpcSize;i++)
802
st->mem_sw[i]=mem[i];
727
804
/* Compute target signal */
728
805
for (i=0;i<st->subframeSize;i++)
729
806
target[i]=sw[i]-res[i];
731
808
for (i=0;i<st->subframeSize;i++)
734
811
/* If we have a long-term predictor (otherwise, something's wrong) */
735
812
if (SUBMODE(ltp_quant))
767
844
pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
768
845
exc, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
769
846
st->lpcSize, st->subframeSize, bits, stack,
770
exc2, syn_resp, st->complexity, ol_pitch_id);
847
exc, syn_resp, st->complexity, ol_pitch_id, st->plc_tuning);
775
852
pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
776
853
exc, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
777
854
st->lpcSize, st->subframeSize, bits, stack,
778
exc2, syn_resp, st->complexity, 0);
855
exc, syn_resp, st->complexity, 0, st->plc_tuning);
801
872
for (i=0;i<st->subframeSize;i++)
804
residue_percep_zero(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, st->buf2, st->subframeSize, st->lpcSize, stack);
806
ener = SHL(compute_rms(st->buf2, st->subframeSize),SIG_SHIFT);
808
/*for (i=0;i<st->subframeSize;i++)
809
printf ("%f\n", st->buf2[i]/ener);
875
for (i=0;i<st->subframeSize;i++)
876
real_exc[i] = SUB32(real_exc[i], exc[i]);
878
ener = SHL32(EXTEND32(compute_rms(real_exc, st->subframeSize)),SIG_SHIFT);
812
880
/*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
813
881
#ifdef FIXED_POINT
815
spx_word32_t f = DIV32(ener,PSHR(ol_gain,SIG_SHIFT));
883
spx_word32_t f = DIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
819
887
fine_gain = 32767;
822
fine_gain = DIV32_16(ener,PSHR(ol_gain,SIG_SHIFT));
890
fine_gain = DIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
824
892
/* Calculate gain correction for the sub-frame (if any) */
825
893
if (SUBMODE(have_subframe_gain))
850
918
/* Codebook search */
851
919
SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
852
920
SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
853
innov, syn_resp, bits, stack, st->complexity);
921
innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
855
923
/* De-normalize innovation and update excitation */
856
924
signal_mul(innov, innov, ener, st->subframeSize);
858
926
for (i=0;i<st->subframeSize;i++)
927
exc[i] = ADD32(exc[i],innov[i]);
861
929
speex_error("No fixed codebook");
864
932
/* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
865
933
if (SUBMODE(double_codebook)) {
866
934
char *tmp_stack=stack;
867
spx_sig_t *innov2 = PUSH(tmp_stack, st->subframeSize, spx_sig_t);
935
VARDECL(spx_sig_t *innov2);
936
ALLOC(innov2, st->subframeSize, spx_sig_t);
868
937
for (i=0;i<st->subframeSize;i++)
870
939
for (i=0;i<st->subframeSize;i++)
872
941
SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
873
942
SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
874
innov2, syn_resp, bits, tmp_stack, st->complexity);
875
signal_mul(innov2, innov2, ener*(1/2.2), st->subframeSize);
943
innov2, syn_resp, bits, stack, st->complexity, 0);
944
signal_mul(innov2, innov2, (spx_word32_t) (ener*(1.f/2.2f)), st->subframeSize);
876
945
for (i=0;i<st->subframeSize;i++)
946
exc[i] = ADD32(exc[i],innov2[i]);
880
signal_mul(target, target, ener, st->subframeSize);
883
/*Keep the previous memory*/
884
for (i=0;i<st->lpcSize;i++)
885
mem[i]=st->mem_sp[i];
886
952
/* Final signal synthesis from excitation */
887
953
iir_mem2(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp);
889
955
/* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
890
filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);
891
for (i=0;i<st->subframeSize;i++)
956
if (st->complexity!=0)
957
filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);
895
961
/* Store the LSPs for interpolation in the next frame */
912
978
/* The next frame will not be the first (Duh!) */
918
for (i=0;i<st->frameSize;i++)
920
ener+=st->frame[i]*st->frame[i];
921
err += (st->frame[i]-orig[i])*(st->frame[i]-orig[i]);
923
snr = 10*log10((ener+1)/(err+1));
924
/*printf ("%f %f %f\n", snr, ener, err);*/
927
982
/* Replace input by synthesized speech */
928
983
for (i=0;i<st->frameSize;i++)
930
spx_word32_t sig = PSHR(st->frame[i],SIG_SHIFT);
985
spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT);
938
994
if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
939
995
st->bounded_pitch = 1;
978
1039
st->lpc_enh_enabled=0;
981
st->inBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
982
st->frame = st->inBuf + st->bufSize - st->windowSize;
983
st->excBuf = PUSH(st->stack, st->bufSize, spx_sig_t);
984
st->exc = st->excBuf + st->bufSize - st->windowSize;
985
for (i=0;i<st->bufSize;i++)
1042
st->inBuf = speex_alloc((st->frameSize)*sizeof(spx_sig_t));
1043
st->frame = st->inBuf;
1044
st->excBuf = speex_alloc((st->frameSize + st->max_pitch + 1)*sizeof(spx_sig_t));
1045
st->exc = st->excBuf + st->max_pitch + 1;
1046
for (i=0;i<st->frameSize;i++)
987
for (i=0;i<st->bufSize;i++)
1048
for (i=0;i<st->frameSize + st->max_pitch + 1;i++)
988
1049
st->excBuf[i]=0;
989
st->innov = PUSH(st->stack, st->frameSize, spx_sig_t);
1050
st->innov = speex_alloc((st->frameSize)*sizeof(spx_sig_t));
991
st->interp_qlpc = PUSH(st->stack, st->lpcSize+1, spx_coef_t);
992
st->qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
993
st->old_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
994
st->interp_qlsp = PUSH(st->stack, st->lpcSize, spx_lsp_t);
995
st->mem_sp = PUSH(st->stack, 5*st->lpcSize, spx_mem_t);
996
st->comb_mem = PUSHS(st->stack, CombFilterMem);
1052
st->interp_qlpc = speex_alloc(st->lpcSize*sizeof(spx_coef_t));
1053
st->qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
1054
st->old_qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
1055
st->interp_qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
1056
st->mem_sp = speex_alloc((5*st->lpcSize)*sizeof(spx_mem_t));
1057
st->comb_mem = speex_alloc(sizeof(CombFilterMem));
997
1058
comb_filter_mem_init (st->comb_mem);
999
st->pi_gain = PUSH(st->stack, st->nbSubframes, spx_word32_t);
1060
st->pi_gain = speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
1000
1061
st->last_pitch = 40;
1001
1062
st->count_lost=0;
1002
1063
st->pitch_gain_buf[0] = st->pitch_gain_buf[1] = st->pitch_gain_buf[2] = 0;
1003
1064
st->pitch_gain_buf_idx = 0;
1005
1067
st->sampling_rate=8000;
1006
1068
st->last_ol_gain = 0;
1025
1087
st=(DecState*)state;
1089
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
1090
speex_free_scratch(st->stack);
1093
speex_free (st->inBuf);
1094
speex_free (st->excBuf);
1095
speex_free (st->innov);
1096
speex_free (st->interp_qlpc);
1097
speex_free (st->qlsp);
1098
speex_free (st->old_qlsp);
1099
speex_free (st->interp_qlsp);
1100
speex_free (st->mem_sp);
1101
speex_free (st->comb_mem);
1102
speex_free (st->pi_gain);
1027
1104
speex_free(state);
1030
1107
#define median3(a, b, c) ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a))))
1110
const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283};
1112
const spx_word16_t attenuation[10] = {1., 0.961, 0.852, 0.698, 0.527, 0.368, 0.237, 0.141, 0.077, 0.039};
1032
1116
static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack)
1035
spx_coef_t *awk1, *awk2, *awk3;
1036
float pitch_gain, fact;
1120
VARDECL(spx_coef_t *awk1);
1121
VARDECL(spx_coef_t *awk2);
1122
VARDECL(spx_coef_t *awk3);
1123
spx_word16_t pitch_gain;
1037
1125
spx_word16_t gain_med;
1126
spx_word16_t innov_gain;
1128
if (st->count_lost<10)
1129
fact = attenuation[st->count_lost];
1039
fact = exp(-.04*st->count_lost*st->count_lost);
1040
1133
gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]);
1041
1134
if (gain_med < st->last_pitch_gain)
1042
1135
st->last_pitch_gain = gain_med;
1138
pitch_gain = st->last_pitch_gain;
1141
pitch_gain = SHL(pitch_gain, 9);
1044
1143
pitch_gain = GAIN_SCALING_1*st->last_pitch_gain;
1148
pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL;
1050
1150
/* Shift all buffers by one frame */
1051
speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
1052
speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
1151
/*speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));*/
1152
speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch + 1)*sizeof(spx_sig_t));
1054
awk1=PUSH(stack, (st->lpcSize+1), spx_coef_t);
1055
awk2=PUSH(stack, (st->lpcSize+1), spx_coef_t);
1056
awk3=PUSH(stack, (st->lpcSize+1), spx_coef_t);
1154
ALLOC(awk1, (st->lpcSize+1), spx_coef_t);
1155
ALLOC(awk2, (st->lpcSize+1), spx_coef_t);
1156
ALLOC(awk3, (st->lpcSize+1), spx_coef_t);
1058
1158
for (sub=0;sub<st->nbSubframes;sub++)
1089
1189
/* FIXME: THIS CAN BE IMPROVED */
1090
1190
/*if (pitch_gain>.95)
1091
1191
pitch_gain=.95;*/
1094
for (i=0;i<st->frameSize;i++)
1095
innov_gain += 1.0*st->innov[i]*st->innov[i];
1096
innov_gain=sqrt(innov_gain/st->frameSize);
1192
innov_gain = compute_rms(st->innov, st->frameSize);
1193
pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT);
1194
if (pitch_val > st->max_pitch)
1195
pitch_val = st->max_pitch;
1196
if (pitch_val < st->min_pitch)
1197
pitch_val = st->min_pitch;
1097
1198
for (i=0;i<st->subframeSize;i++)
1100
exc[i] = pitch_gain * exc[i - st->last_pitch] + fact*sqrt(1-pitch_gain)*st->innov[i+offset];
1101
/*Just so it give the same lost packets as with if 0*/
1104
/*exc[i]=pitch_gain*exc[i-st->last_pitch] + fact*st->innov[i+offset];*/
1105
exc[i]=pitch_gain*exc[i-st->last_pitch] +
1106
fact*sqrt(1-pitch_gain)*speex_rand(innov_gain);
1200
exc[i]= MULT16_32_Q15(pitch_gain, (exc[i-pitch_val]+VERY_SMALL)) +
1201
MULT16_32_Q15(fact, MULT16_32_Q15(SHL(Q15ONE,15)-SHL(MULT16_16(pitch_gain,pitch_gain),1),speex_rand(innov_gain, &st->seed)));
1110
1204
for (i=0;i<st->subframeSize;i++)
1265
1361
/* Shift all buffers by one frame */
1266
speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
1267
speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));
1362
speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch + 1)*sizeof(spx_sig_t));
1269
1364
/* If null mode (no transmission), just set a couple things to zero*/
1270
1365
if (st->submodes[st->submodeID] == NULL)
1273
lpc = PUSH(stack,11, spx_coef_t);
1274
bw_lpc(GAMMA_SCALING*.93, st->interp_qlpc, lpc, 10);
1275
/*for (i=0;i<st->frameSize;i++)
1367
VARDECL(spx_coef_t *lpc);
1368
ALLOC(lpc, st->lpcSize, spx_coef_t);
1369
bw_lpc(GAMMA_SCALING*.93, st->interp_qlpc, lpc, st->lpcSize);
1278
1371
float innov_gain=0;
1279
1372
float pgain=GAIN_SCALING_1*st->last_pitch_gain;
1282
for (i=0;i<st->frameSize;i++)
1283
innov_gain += st->innov[i]*st->innov[i];
1284
innov_gain=sqrt(innov_gain/st->frameSize);
1285
for (i=0;i<st->frameSize;i++)
1375
innov_gain = compute_rms(st->innov, st->frameSize);
1376
for (i=0;i<st->frameSize;i++)
1377
st->exc[i]=VERY_SMALL;
1287
1378
speex_rand_vec(innov_gain, st->exc, st->frameSize);
1591
1690
for (i=0;i<st->subframeSize;i++)
1691
exc[i]=ADD32(exc[i],innov[i]);
1593
1692
/*print_vec(exc, 40, "innov");*/
1595
1694
/* Decode second codebook (only for some modes) */
1596
1695
if (SUBMODE(double_codebook))
1598
1697
char *tmp_stack=stack;
1599
spx_sig_t *innov2 = PUSH(tmp_stack, st->subframeSize, spx_sig_t);
1698
VARDECL(spx_sig_t *innov2);
1699
ALLOC(innov2, st->subframeSize, spx_sig_t);
1600
1700
for (i=0;i<st->subframeSize;i++)
1602
SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, tmp_stack);
1603
signal_mul(innov2, innov2, ener*(1/2.2), st->subframeSize);
1702
SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, stack);
1703
signal_mul(innov2, innov2, (spx_word32_t) (ener*(1/2.2)), st->subframeSize);
1604
1704
for (i=0;i<st->subframeSize;i++)
1605
exc[i] += innov2[i];
1705
exc[i] = ADD32(exc[i],innov2[i]);
1711
/* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1714
spx_word16_t exc_ener;
1715
spx_word32_t gain32;
1717
exc_ener = compute_rms (exc, st->subframeSize);
1718
gain32 = DIV32(ol_gain, ADD16(exc_ener,1));
1722
gain = EXTRACT16(gain32);
1728
for (i=0;i<st->subframeSize;i++)
1729
exc[i] = MULT16_32_Q14(gain, exc[i]);
1610
1732
for (i=0;i<st->subframeSize;i++)