Changeset 628 for pjproject/trunk/pjmedia/src/pjmedia-codec/speex/nb_celp.c
- Timestamp:
- Jul 26, 2006 5:04:54 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia-codec/speex/nb_celp.c
r278 r628 47 47 #include "vbr.h" 48 48 #include "misc.h" 49 #include "math_approx.h" 49 50 #include <speex/speex_callbacks.h> 50 51 … … 106 107 107 108 #define sqr(x) ((x)*(x)) 109 110 extern const spx_word16_t lpc_window[]; 108 111 109 112 void *nb_encoder_init(const SpeexMode *m) … … 126 129 127 130 st->frameSize = mode->frameSize; 128 st->windowSize = st->frameSize*3/2;129 131 st->nbSubframes=mode->frameSize/mode->subframeSize; 130 132 st->subframeSize=mode->subframeSize; 133 st->windowSize = st->frameSize+st->subframeSize; 131 134 st->lpcSize = mode->lpcSize; 132 135 st->gamma1=mode->gamma1; … … 150 153 st->curve = speex_alloc(128*sizeof(float)); 151 154 st->old_curve = speex_alloc(128*sizeof(float)); 152 #endif 155 st->psy_window = speex_alloc(256*sizeof(float)); 156 #endif 157 158 st->cumul_gain = 1024; 153 159 154 160 /* Allocating input buffer */ 155 st->inBuf = speex_alloc((st->windowSize+EXTRA_BUFFER)*sizeof(spx_sig_t)); 156 st->frame = st->inBuf+EXTRA_BUFFER; 161 st->winBuf = speex_alloc((st->windowSize-st->frameSize)*sizeof(spx_word16_t)); 157 162 /* Allocating excitation buffer */ 158 st->excBuf = speex_alloc((mode->frameSize+mode->pitchEnd+1)*sizeof(spx_sig_t)); 159 st->exc = st->excBuf + mode->pitchEnd + 1; 160 st->swBuf = speex_alloc((mode->frameSize+mode->pitchEnd+1)*sizeof(spx_sig_t)); 161 st->sw = st->swBuf + mode->pitchEnd + 1; 162 163 st->innov = speex_alloc((st->frameSize)*sizeof(spx_sig_t)); 164 165 /* Asymmetric "pseudo-Hamming" window */ 166 { 167 int part1, part2; 168 part1=st->frameSize - (st->subframeSize>>1); 169 part2=(st->frameSize>>1) + (st->subframeSize>>1); 170 st->window = speex_alloc((st->windowSize)*sizeof(spx_word16_t)); 171 for (i=0;i<part1;i++) 172 st->window[i]=(spx_word16_t)(SIG_SCALING*(.54-.46*cos(M_PI*i/part1))); 173 for (i=0;i<part2;i++) 174 st->window[part1+i]=(spx_word16_t)(SIG_SCALING*(.54+.46*cos(M_PI*i/part2))); 175 } 163 st->excBuf = speex_alloc((mode->frameSize+mode->pitchEnd+2)*sizeof(spx_word16_t)); 164 st->exc = st->excBuf + mode->pitchEnd + 2; 165 st->swBuf = speex_alloc((mode->frameSize+mode->pitchEnd+2)*sizeof(spx_word16_t)); 166 st->sw = st->swBuf + mode->pitchEnd + 2; 167 168 st->window= lpc_window; 169 176 170 /* Create the window for autocorrelation (lag-windowing) */ 177 171 st->lagWindow = speex_alloc((st->lpcSize+1)*sizeof(spx_word16_t)); … … 179 173 st->lagWindow[i]=16384*exp(-.5*sqr(2*M_PI*st->lag_factor*i)); 180 174 181 st->autocorr = speex_alloc((st->lpcSize+1)*sizeof(spx_word16_t));182 183 st->lpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));184 st->interp_lpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));185 st->interp_qlpc = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));186 st->bw_lpc1 = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));187 st->bw_lpc2 = speex_alloc((st->lpcSize)*sizeof(spx_coef_t));188 189 st->lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));190 st->qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));191 175 st->old_lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t)); 192 176 st->old_qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t)); 193 st->interp_lsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));194 st->interp_qlsp = speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));195 196 177 st->first = 1; 197 178 for (i=0;i<st->lpcSize;i++) 198 179 { 199 st-> lsp[i]=LSP_SCALING*(M_PI*((float)(i+1)))/(st->lpcSize+1);180 st->old_lsp[i]=LSP_SCALING*(M_PI*((float)(i+1)))/(st->lpcSize+1); 200 181 } 201 182 … … 204 185 st->mem_sw_whole = speex_alloc((st->lpcSize)*sizeof(spx_mem_t)); 205 186 st->mem_exc = speex_alloc((st->lpcSize)*sizeof(spx_mem_t)); 187 st->mem_exc2 = speex_alloc((st->lpcSize)*sizeof(spx_mem_t)); 206 188 207 189 st->pi_gain = speex_alloc((st->nbSubframes)*sizeof(spx_word32_t)); 208 190 st->innov_save = NULL; 191 209 192 st->pitch = speex_alloc((st->nbSubframes)*sizeof(int)); 210 193 … … 213 196 st->vbr_quality = 8; 214 197 st->vbr_enabled = 0; 198 st->vbr_max = 0; 215 199 st->vad_enabled = 0; 216 200 st->dtx_enabled = 0; … … 237 221 #endif 238 222 239 speex_free (st-> inBuf);223 speex_free (st->winBuf); 240 224 speex_free (st->excBuf); 241 speex_free (st->innov);242 speex_free (st->interp_qlpc);243 speex_free (st->qlsp);244 225 speex_free (st->old_qlsp); 245 speex_free (st->interp_qlsp);246 226 speex_free (st->swBuf); 247 227 248 speex_free (st->window);249 228 speex_free (st->lagWindow); 250 speex_free (st->autocorr); 251 speex_free (st->lpc); 252 speex_free (st->lsp); 253 254 speex_free (st->interp_lpc); 255 speex_free (st->bw_lpc1); 256 speex_free (st->bw_lpc2); 229 257 230 speex_free (st->old_lsp); 258 speex_free (st->interp_lsp);259 231 speex_free (st->mem_sp); 260 232 speex_free (st->mem_sw); 261 233 speex_free (st->mem_sw_whole); 262 234 speex_free (st->mem_exc); 235 speex_free (st->mem_exc2); 263 236 speex_free (st->pi_gain); 264 237 speex_free (st->pitch); … … 271 244 speex_free (st->curve); 272 245 speex_free (st->old_curve); 246 speex_free (st->psy_window); 273 247 #endif 274 248 … … 284 258 spx_word16_t ol_pitch_coef; 285 259 spx_word32_t ol_gain; 286 VARDECL(spx_sig_t *res); 287 VARDECL(spx_sig_t *target); 260 VARDECL(spx_word16_t *ringing); 261 VARDECL(spx_word16_t *target); 262 VARDECL(spx_sig_t *innov); 263 VARDECL(spx_word32_t *exc32); 288 264 VARDECL(spx_mem_t *mem); 265 VARDECL(spx_coef_t *bw_lpc1); 266 VARDECL(spx_coef_t *bw_lpc2); 267 VARDECL(spx_coef_t *lpc); 268 VARDECL(spx_lsp_t *lsp); 269 VARDECL(spx_lsp_t *qlsp); 270 VARDECL(spx_lsp_t *interp_lsp); 271 VARDECL(spx_lsp_t *interp_qlsp); 272 VARDECL(spx_coef_t *interp_lpc); 273 VARDECL(spx_coef_t *interp_qlpc); 289 274 char *stack; 290 275 VARDECL(spx_word16_t *syn_resp); 291 VARDECL(spx_ sig_t *real_exc);276 VARDECL(spx_word16_t *real_exc); 292 277 #ifdef EPIC_48K 293 278 int pitch_half[2]; … … 299 284 stack=st->stack; 300 285 301 /* Copy new data in input buffer */ 302 speex_move(st->inBuf, st->inBuf+st->frameSize, (EXTRA_BUFFER+st->windowSize-st->frameSize)*sizeof(spx_sig_t)); 303 for (i=0;i<st->frameSize;i++) 304 st->inBuf[st->windowSize-st->frameSize+i+EXTRA_BUFFER] = SHL32(EXTEND32(in[i]), SIG_SHIFT); 286 ALLOC(lpc, st->lpcSize, spx_coef_t); 287 ALLOC(bw_lpc1, st->lpcSize, spx_coef_t); 288 ALLOC(bw_lpc2, st->lpcSize, spx_coef_t); 289 ALLOC(lsp, st->lpcSize, spx_lsp_t); 290 ALLOC(qlsp, st->lpcSize, spx_lsp_t); 291 ALLOC(interp_lsp, st->lpcSize, spx_lsp_t); 292 ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t); 293 ALLOC(interp_lpc, st->lpcSize, spx_coef_t); 294 ALLOC(interp_qlpc, st->lpcSize, spx_coef_t); 305 295 306 296 /* Move signals 1 frame towards the past */ 307 speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch+ 1)*sizeof(spx_sig_t));308 speex_move(st->swBuf, st->swBuf+st->frameSize, (st->max_pitch+ 1)*sizeof(spx_sig_t));297 speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch+2)*sizeof(spx_word16_t)); 298 speex_move(st->swBuf, st->swBuf+st->frameSize, (st->max_pitch+2)*sizeof(spx_word16_t)); 309 299 310 300 { 311 301 VARDECL(spx_word16_t *w_sig); 302 VARDECL(spx_word16_t *autocorr); 312 303 ALLOC(w_sig, st->windowSize, spx_word16_t); 304 ALLOC(autocorr, st->lpcSize+1, spx_word16_t); 313 305 /* Window for analysis */ 314 for (i=0;i<st->windowSize;i++) 315 w_sig[i] = EXTRACT16(SHR32(MULT16_16(EXTRACT16(SHR32(st->frame[i],SIG_SHIFT)),st->window[i]),SIG_SHIFT)); 316 306 for (i=0;i<st->windowSize-st->frameSize;i++) 307 w_sig[i] = EXTRACT16(SHR32(MULT16_16(st->winBuf[i],st->window[i]),SIG_SHIFT)); 308 for (;i<st->windowSize;i++) 309 w_sig[i] = EXTRACT16(SHR32(MULT16_16(in[i-st->windowSize+st->frameSize],st->window[i]),SIG_SHIFT)); 317 310 /* Compute auto-correlation */ 318 _spx_autocorr(w_sig, st->autocorr, st->lpcSize+1, st->windowSize); 319 } 320 st->autocorr[0] = ADD16(st->autocorr[0],MULT16_16_Q15(st->autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */ 321 322 /* Lag windowing: equivalent to filtering in the power-spectrum domain */ 323 for (i=0;i<st->lpcSize+1;i++) 324 st->autocorr[i] = MULT16_16_Q14(st->autocorr[i],st->lagWindow[i]); 325 326 /* Levinson-Durbin */ 327 _spx_lpc(st->lpc, st->autocorr, st->lpcSize); 328 329 /* LPC to LSPs (x-domain) transform */ 330 roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 15, LSP_DELTA1, stack); 331 /* Check if we found all the roots */ 332 if (roots!=st->lpcSize) 333 { 334 /* Search again if we can afford it */ 335 if (st->complexity>1) 336 roots = lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 11, LSP_DELTA2, stack); 337 if (roots!=st->lpcSize) 311 _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize); 312 autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */ 313 314 /* Lag windowing: equivalent to filtering in the power-spectrum domain */ 315 for (i=0;i<st->lpcSize+1;i++) 316 autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]); 317 318 /* Levinson-Durbin */ 319 _spx_lpc(lpc, autocorr, st->lpcSize); 320 /* LPC to LSPs (x-domain) transform */ 321 roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack); 322 /* Check if we found all the roots */ 323 if (roots!=st->lpcSize) 338 324 { 339 325 /*If we can't find all LSP's, do some damage control and use previous filter*/ 340 326 for (i=0;i<st->lpcSize;i++) 341 327 { 342 st->lsp[i]=st->old_lsp[i]; 343 } 344 } 345 } 328 lsp[i]=st->old_lsp[i]; 329 } 330 } 331 } 332 346 333 347 334 … … 351 338 if (st->first) 352 339 for (i=0;i<st->lpcSize;i++) 353 st->interp_lsp[i] = st->lsp[i];340 interp_lsp[i] = lsp[i]; 354 341 else 355 lsp_interpolate(st->old_lsp, st->lsp, st->interp_lsp, st->lpcSize, st->nbSubframes, st->nbSubframes<<1);356 357 lsp_enforce_margin( st->interp_lsp, st->lpcSize, LSP_MARGIN);342 lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, st->nbSubframes, st->nbSubframes<<1); 343 344 lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN); 358 345 359 346 /* Compute interpolated LPCs (unquantized) for whole frame*/ 360 lsp_to_lpc( st->interp_lsp, st->interp_lpc, st->lpcSize,stack);347 lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack); 361 348 362 349 363 350 /*Open-loop pitch*/ 364 if ( !st->submodes[st->submodeID] || st->vbr_enabled || st->vad_enabled || SUBMODE(forced_pitch_gain) ||351 if (st->complexity>2 || !st->submodes[st->submodeID] || st->vbr_enabled || st->vad_enabled || SUBMODE(forced_pitch_gain) || 365 352 SUBMODE(lbr_pitch) != -1) 366 353 { … … 368 355 spx_word16_t nol_pitch_coef[6]; 369 356 370 bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);371 bw_lpc(st->gamma2, st->interp_lpc, st->bw_lpc2, st->lpcSize);357 bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize); 358 bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize); 372 359 373 filter_mem2(st->frame, st->bw_lpc1, st->bw_lpc2, st->sw, st->frameSize, st->lpcSize, st->mem_sw_whole); 360 for (i=0;i<st->windowSize-st->frameSize;i++) 361 st->sw[i] = st->winBuf[i]; 362 for (;i<st->frameSize;i++) 363 st->sw[i] = in[i-st->windowSize+st->frameSize]; 364 filter_mem16(st->sw, bw_lpc1, bw_lpc2, st->sw, st->frameSize, st->lpcSize, st->mem_sw_whole, stack); 374 365 375 366 open_loop_nbest_pitch(st->sw, st->min_pitch, st->max_pitch, st->frameSize, … … 413 404 ol_pitch_coef=0; 414 405 } 406 415 407 /*Compute "real" excitation*/ 416 fir_mem2(st->frame, st->interp_lpc, st->exc, st->frameSize, st->lpcSize, st->mem_exc); 408 for (i=0;i<st->windowSize-st->frameSize;i++) 409 st->exc[i] = st->winBuf[i]; 410 for (;i<st->frameSize;i++) 411 st->exc[i] = in[i-st->windowSize+st->frameSize]; 412 fir_mem16(st->exc, interp_lpc, st->exc, st->frameSize, st->lpcSize, st->mem_exc, stack); 417 413 418 414 /* Compute open-loop excitation gain */ … … 422 418 float ol1=0,ol2=0; 423 419 float ol_gain2; 424 ol1 = compute_rms (st->exc, st->frameSize>>1);425 ol2 = compute_rms (st->exc+(st->frameSize>>1), st->frameSize>>1);420 ol1 = compute_rms16(st->exc, st->frameSize>>1); 421 ol2 = compute_rms16(st->exc+(st->frameSize>>1), st->frameSize>>1); 426 422 ol1 *= ol1*(st->frameSize>>1); 427 423 ol2 *= ol2*(st->frameSize>>1); … … 434 430 ol_gain=SHR(sqrt(1+ol_gain2/st->frameSize),SIG_SHIFT); 435 431 436 } else { 437 #endif 438 ol_gain = SHL32(EXTEND32(compute_rms(st->exc, st->frameSize)),SIG_SHIFT); 439 #ifdef EPIC_48K 440 } 441 #endif 432 } else 433 #endif 434 { 435 spx_word16_t g = compute_rms16(st->exc, st->frameSize); 436 if (ol_pitch>0) 437 ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14), 438 spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16))))); 439 else 440 ol_gain = SHL32(EXTEND32(g),SIG_SHIFT); 441 } 442 442 } 443 443 444 444 #ifdef VORBIS_PSYCHO 445 compute_curve(st->psy, st->frame-16, st->curve); 445 for(i=0;i<256-st->frameSize;i++) 446 st->psy_window[i] = st->psy_window[i+st->frameSize]; 447 for(i=0;i<st->frameSize;i++) 448 st->psy_window[256-st->frameSize+i] = in[i]; 449 compute_curve(st->psy, st->psy_window, st->curve); 446 450 /*print_vec(st->curve, 128, "curve");*/ 447 451 if (st->first) … … 455 459 float lsp_dist=0; 456 460 for (i=0;i<st->lpcSize;i++) 457 lsp_dist += (st->old_lsp[i] - st->lsp[i])*(st->old_lsp[i] - st->lsp[i]);461 lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]); 458 462 lsp_dist /= LSP_SCALING*LSP_SCALING; 459 463 … … 519 523 520 524 speex_encoder_ctl(state, SPEEX_SET_MODE, &mode); 521 525 if (st->vbr_max>0) 526 { 527 spx_int32_t rate; 528 speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate); 529 if (rate > st->vbr_max) 530 { 531 rate = st->vbr_max; 532 speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate); 533 } 534 } 535 522 536 if (st->abr_enabled) 523 537 { … … 581 595 st->bounded_pitch = 1; 582 596 583 /* Final signal synthesis from excitation */ 584 iir_mem2(st->exc, st->interp_qlpc, st->frame, st->frameSize, st->lpcSize, st->mem_sp); 585 586 #ifdef RESYNTH 587 for (i=0;i<st->frameSize;i++) 588 in[i]=st->frame[i]; 589 #endif 597 speex_move(st->winBuf, in+2*st->frameSize-st->windowSize, (st->windowSize-st->frameSize)*sizeof(spx_word16_t)); 598 599 /* Clear memory (no need to really compute it) */ 600 for (i=0;i<st->lpcSize;i++) 601 st->mem_sp[i] = 0; 590 602 return 0; 591 603 … … 596 608 { 597 609 for (i=0;i<st->lpcSize;i++) 598 st->old_lsp[i] = st->lsp[i];610 st->old_lsp[i] = lsp[i]; 599 611 } 600 612 … … 602 614 /*Quantize LSPs*/ 603 615 #if 1 /*0 for unquantized*/ 604 SUBMODE(lsp_quant)( st->lsp, st->qlsp, st->lpcSize, bits);616 SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits); 605 617 #else 606 618 for (i=0;i<st->lpcSize;i++) 607 st->qlsp[i]=st->lsp[i];619 qlsp[i]=lsp[i]; 608 620 #endif 609 621 … … 686 698 { 687 699 for (i=0;i<st->lpcSize;i++) 688 st->old_qlsp[i] = st->qlsp[i]; 689 } 690 691 /* Filter response */ 692 ALLOC(res, st->subframeSize, spx_sig_t); 700 st->old_qlsp[i] = qlsp[i]; 701 } 702 693 703 /* Target signal */ 694 ALLOC(target, st->subframeSize, spx_sig_t); 704 ALLOC(target, st->subframeSize, spx_word16_t); 705 ALLOC(innov, st->subframeSize, spx_sig_t); 706 ALLOC(exc32, st->subframeSize, spx_word32_t); 707 ALLOC(ringing, st->subframeSize, spx_word16_t); 695 708 ALLOC(syn_resp, st->subframeSize, spx_word16_t); 696 ALLOC(real_exc, st->subframeSize, spx_ sig_t);709 ALLOC(real_exc, st->subframeSize, spx_word16_t); 697 710 ALLOC(mem, st->lpcSize, spx_mem_t); 698 711 … … 701 714 { 702 715 int offset; 703 spx_sig_t *sp, *sw, *exc; 716 spx_word16_t *sw; 717 spx_word16_t *exc; 718 spx_sig_t *innov_save = NULL; 704 719 int pitch; 705 720 int response_bound = st->subframeSize; … … 716 731 /* Offset relative to start of frame */ 717 732 offset = st->subframeSize*sub; 718 /* Original signal */719 sp=st->frame+offset;720 733 /* Excitation */ 721 734 exc=st->exc+offset; 722 735 /* Weighted signal */ 723 736 sw=st->sw+offset; 724 737 /* Pointer for saving innovation */ 738 if (st->innov_save) 739 innov_save = st->innov_save+offset; 740 725 741 /* LSP interpolation (quantized and unquantized) */ 726 lsp_interpolate(st->old_lsp, st->lsp, st->interp_lsp, st->lpcSize, sub, st->nbSubframes);727 lsp_interpolate(st->old_qlsp, st->qlsp, st->interp_qlsp, st->lpcSize, sub, st->nbSubframes);742 lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes); 743 lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes); 728 744 729 745 /* Make sure the filters are stable */ 730 lsp_enforce_margin( st->interp_lsp, st->lpcSize, LSP_MARGIN);731 lsp_enforce_margin( st->interp_qlsp, st->lpcSize, LSP_MARGIN);746 lsp_enforce_margin(interp_lsp, st->lpcSize, LSP_MARGIN); 747 lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN); 732 748 733 749 /* Compute interpolated LPCs (quantized and unquantized) */ 734 lsp_to_lpc( st->interp_lsp, st->interp_lpc, st->lpcSize,stack);735 736 lsp_to_lpc( st->interp_qlsp, st->interp_qlpc, st->lpcSize, stack);750 lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack); 751 752 lsp_to_lpc(interp_qlsp, interp_qlpc, st->lpcSize, stack); 737 753 738 754 /* Compute analysis filter gain at w=pi (for use in SB-CELP) */ … … 742 758 { 743 759 /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/ 744 pi_g = ADD32(pi_g, SUB32( st->interp_qlpc[i+1],st->interp_qlpc[i]));760 pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i]))); 745 761 } 746 762 st->pi_gain[sub] = pi_g; … … 753 769 for (i=0;i<128;i++) 754 770 curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i]; 755 curve_to_lpc(st->psy, curr_curve, st->bw_lpc1, st->bw_lpc2, 10);771 curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10); 756 772 } 757 773 #else 758 774 /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */ 759 bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);775 bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize); 760 776 if (st->gamma2>=0) 761 bw_lpc(st->gamma2, st->interp_lpc, st->bw_lpc2, st->lpcSize);777 bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize); 762 778 else 763 779 { 764 st->bw_lpc2[0]=1;780 bw_lpc2[0]=1; 765 781 for (i=1;i<=st->lpcSize;i++) 766 st->bw_lpc2[i]=0;782 bw_lpc2[i]=0; 767 783 } 768 784 /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/ 769 785 #endif 770 786 771 for (i=0;i<st->subframeSize;i++) 772 real_exc[i] = exc[i]; 787 { 788 /*FIXME: This will break if we change the window size */ 789 if (st->windowSize-st->frameSize != st->subframeSize) 790 speex_error("windowSize-frameSize != subframeSize"); 791 if (sub==0) 792 { 793 for (i=0;i<st->subframeSize;i++) 794 real_exc[i] = sw[i] = st->winBuf[i]; 795 } else { 796 for (i=0;i<st->subframeSize;i++) 797 real_exc[i] = sw[i] = in[i+((sub-1)*st->subframeSize)]; 798 } 799 } 800 fir_mem16(real_exc, interp_qlpc, real_exc, st->subframeSize, st->lpcSize, st->mem_exc2, stack); 773 801 774 802 if (st->complexity==0) 775 803 response_bound >>= 1; 776 compute_impulse_response( st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, syn_resp, response_bound, st->lpcSize, stack);804 compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, st->lpcSize, stack); 777 805 for (i=response_bound;i<st->subframeSize;i++) 778 806 syn_resp[i]=VERY_SMALL; 779 807 780 /* Reset excitation */781 for (i=0;i<st->subframeSize;i++)782 exc[i]=VERY_SMALL;783 784 808 /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */ 785 809 for (i=0;i<st->lpcSize;i++) 786 mem[i]=st->mem_sp[i]; 810 mem[i]=SHL32(st->mem_sp[i],1); 811 for (i=0;i<st->subframeSize;i++) 812 ringing[i] = VERY_SMALL; 787 813 #ifdef SHORTCUTS2 788 iir_mem 2(exc, st->interp_qlpc, exc, response_bound, st->lpcSize, mem);814 iir_mem16(ringing, interp_qlpc, ringing, response_bound, st->lpcSize, mem, stack); 789 815 for (i=0;i<st->lpcSize;i++) 790 mem[i]= st->mem_sw[i];791 filter_mem 2(exc, st->bw_lpc1, st->bw_lpc2, res, response_bound, st->lpcSize, mem);816 mem[i]=SHL32(st->mem_sw[i],1); 817 filter_mem16(ringing, st->bw_lpc1, st->bw_lpc2, ringing, response_bound, st->lpcSize, mem, stack); 792 818 for (i=response_bound;i<st->subframeSize;i++) 793 r es[i]=0;819 ringing[i]=0; 794 820 #else 795 iir_mem 2(exc, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, mem);821 iir_mem16(ringing, interp_qlpc, ringing, st->subframeSize, st->lpcSize, mem, stack); 796 822 for (i=0;i<st->lpcSize;i++) 797 mem[i]= st->mem_sw[i];798 filter_mem 2(exc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, mem);823 mem[i]=SHL32(st->mem_sw[i],1); 824 filter_mem16(ringing, bw_lpc1, bw_lpc2, ringing, st->subframeSize, st->lpcSize, mem, stack); 799 825 #endif 800 826 … … 802 828 for (i=0;i<st->lpcSize;i++) 803 829 mem[i]=st->mem_sw[i]; 804 filter_mem 2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, mem);830 filter_mem16(sw, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack); 805 831 806 832 if (st->complexity==0) … … 810 836 /* Compute target signal */ 811 837 for (i=0;i<st->subframeSize;i++) 812 target[i]=sw[i]-res[i]; 813 838 target[i]=SUB16(sw[i],PSHR32(ringing[i],1)); 839 840 /* Reset excitation */ 814 841 for (i=0;i<st->subframeSize;i++) 815 842 exc[i]=0; … … 848 875 if (st->lbr_48k) 849 876 { 850 pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,851 exc , SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,877 pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2, 878 exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef, 852 879 st->lpcSize, st->subframeSize, bits, stack, 853 exc, syn_resp, st->complexity, ol_pitch_id, st->plc_tuning );880 exc, syn_resp, st->complexity, ol_pitch_id, st->plc_tuning, &st->cumul_gain); 854 881 } else { 855 882 #endif 856 883 857 884 /* Perform pitch search */ 858 pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,859 exc , SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,885 pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2, 886 exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef, 860 887 st->lpcSize, st->subframeSize, bits, stack, 861 exc, syn_resp, st->complexity, 0, st->plc_tuning );888 exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain); 862 889 #ifdef EPIC_48K 863 890 } … … 871 898 /* Quantization of innovation */ 872 899 { 873 spx_sig_t *innov;874 900 spx_word32_t ener=0; 875 901 spx_word16_t fine_gain; 876 902 877 innov = st->innov+sub*st->subframeSize;878 903 for (i=0;i<st->subframeSize;i++) 879 904 innov[i]=0; 880 905 881 906 for (i=0;i<st->subframeSize;i++) 882 real_exc[i] = SUB 32(real_exc[i], exc[i]);883 884 ener = SHL32(EXTEND32(compute_rms (real_exc, st->subframeSize)),SIG_SHIFT);907 real_exc[i] = SUB16(real_exc[i], PSHR32(exc32[i],SIG_SHIFT-1)); 908 909 ener = SHL32(EXTEND32(compute_rms16(real_exc, st->subframeSize)),SIG_SHIFT); 885 910 886 911 /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */ 887 912 #ifdef FIXED_POINT 888 913 { 889 spx_word32_t f = DIV32(ener,PSHR32(ol_gain,SIG_SHIFT));914 spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT)); 890 915 if (f<=32767) 891 916 fine_gain = f; … … 894 919 } 895 920 #else 896 fine_gain = DIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));921 fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT)); 897 922 #endif 898 923 /* Calculate gain correction for the sub-frame (if any) */ … … 923 948 { 924 949 /* Codebook search */ 925 SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,950 SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2, 926 951 SUBMODE(innovation_params), st->lpcSize, st->subframeSize, 927 952 innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook)); … … 931 956 932 957 for (i=0;i<st->subframeSize;i++) 933 exc[i] = ADD32(exc[i],innov[i]);958 exc[i] = EXTRACT16(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT)); 934 959 } else { 935 960 speex_error("No fixed codebook"); 936 961 } 937 962 963 if (innov_save) 964 { 965 for (i=0;i<st->subframeSize;i++) 966 innov_save[i] = innov[i]; 967 } 938 968 /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */ 939 969 if (SUBMODE(double_codebook)) { … … 944 974 innov2[i]=0; 945 975 for (i=0;i<st->subframeSize;i++) 946 target[i] *=2.2;947 SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,976 target[i]=MULT16_16_P13(QCONST16(2.2,13), target[i]); 977 SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2, 948 978 SUBMODE(innovation_params), st->lpcSize, st->subframeSize, 949 979 innov2, syn_resp, bits, stack, st->complexity, 0); 950 signal_mul(innov2, innov2, (spx_word32_t) (ener*(1.f/2.2f)), st->subframeSize);980 signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545,15),ener), st->subframeSize); 951 981 for (i=0;i<st->subframeSize;i++) 952 exc[i] = ADD32(exc[i],innov2[i]); 982 exc[i] = ADD32(exc[i],PSHR32(innov2[i],SIG_SHIFT)); 983 if (innov_save) 984 { 985 for (i=0;i<st->subframeSize;i++) 986 innov_save[i] = ADD32(innov_save[i],innov2[i]); 987 } 953 988 stack = tmp_stack; 954 989 } … … 956 991 } 957 992 993 for (i=0;i<st->subframeSize;i++) 994 sw[i] = exc[i]; 958 995 /* Final signal synthesis from excitation */ 959 iir_mem 2(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp);996 iir_mem16(sw, interp_qlpc, sw, st->subframeSize, st->lpcSize, st->mem_sp, stack); 960 997 961 998 /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */ 962 999 if (st->complexity!=0) 963 filter_mem 2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);1000 filter_mem16(sw, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack); 964 1001 965 1002 } … … 969 1006 { 970 1007 for (i=0;i<st->lpcSize;i++) 971 st->old_lsp[i] = st->lsp[i];1008 st->old_lsp[i] = lsp[i]; 972 1009 for (i=0;i<st->lpcSize;i++) 973 st->old_qlsp[i] = st->qlsp[i];1010 st->old_qlsp[i] = qlsp[i]; 974 1011 } 975 1012 … … 992 1029 /* The next frame will not be the first (Duh!) */ 993 1030 st->first = 0; 994 995 #ifdef RESYNTH 996 /* Replace input by synthesized speech */ 997 for (i=0;i<st->frameSize;i++) 998 { 999 spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT); 1000 if (sig>32767) 1001 sig = 32767; 1002 if (sig<-32767) 1003 sig = -32767; 1004 in[i]=sig; 1005 } 1006 #endif 1031 speex_move(st->winBuf, in+2*st->frameSize-st->windowSize, (st->windowSize-st->frameSize)*sizeof(spx_word16_t)); 1007 1032 1008 1033 if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0) … … 1013 1038 return 1; 1014 1039 } 1015 1016 1040 1017 1041 void *nb_decoder_init(const SpeexMode *m) … … 1051 1075 st->submodeID=mode->defaultSubmode; 1052 1076 1053 st->lpc_enh_enabled=0; 1054 1055 1056 st->inBuf = speex_alloc((st->frameSize)*sizeof(spx_sig_t)); 1057 st->frame = st->inBuf; 1058 st->excBuf = speex_alloc((st->frameSize + st->max_pitch + 1)*sizeof(spx_sig_t)); 1059 st->exc = st->excBuf + st->max_pitch + 1; 1060 for (i=0;i<st->frameSize;i++) 1061 st->inBuf[i]=0; 1077 st->lpc_enh_enabled=1; 1078 1079 st->excBuf = speex_alloc((st->frameSize + 2*st->max_pitch + st->subframeSize + 12)*sizeof(spx_word16_t)); 1080 st->exc = st->excBuf + 2*st->max_pitch + st->subframeSize + 6; 1062 1081 for (i=0;i<st->frameSize + st->max_pitch + 1;i++) 1063 1082 st->excBuf[i]=0; 1064 st->innov = speex_alloc((st->frameSize)*sizeof(spx_sig_t));1065 1083 1066 1084 st->interp_qlpc = speex_alloc(st->lpcSize*sizeof(spx_coef_t)); 1067 st->qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t));1068 1085 st->old_qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t)); 1069 st->interp_qlsp = speex_alloc(st->lpcSize*sizeof(spx_lsp_t)); 1070 st->mem_sp = speex_alloc((5*st->lpcSize)*sizeof(spx_mem_t)); 1071 st->comb_mem = speex_alloc(sizeof(CombFilterMem)); 1072 comb_filter_mem_init (st->comb_mem); 1073 1086 st->mem_sp = speex_alloc(st->lpcSize*sizeof(spx_mem_t)); 1074 1087 st->pi_gain = speex_alloc((st->nbSubframes)*sizeof(spx_word32_t)); 1075 1088 st->last_pitch = 40; … … 1105 1118 #endif 1106 1119 1107 speex_free (st->inBuf);1108 1120 speex_free (st->excBuf); 1109 speex_free (st->innov);1110 1121 speex_free (st->interp_qlpc); 1111 speex_free (st->qlsp);1112 1122 speex_free (st->old_qlsp); 1113 speex_free (st->interp_qlsp);1114 1123 speex_free (st->mem_sp); 1115 speex_free (st->comb_mem);1116 1124 speex_free (st->pi_gain); 1117 1125 … … 1132 1140 int i, sub; 1133 1141 int pitch_val; 1134 VARDECL(spx_coef_t *awk1);1135 VARDECL(spx_coef_t *awk2);1136 VARDECL(spx_coef_t *awk3);1137 1142 spx_word16_t pitch_gain; 1138 1143 spx_word16_t fact; … … 1163 1168 1164 1169 /* Shift all buffers by one frame */ 1165 /*speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(spx_sig_t));*/ 1166 speex_move(st->excBuf, st->excBuf+st->frameSize, (st->max_pitch + 1)*sizeof(spx_sig_t)); 1167 1168 ALLOC(awk1, (st->lpcSize+1), spx_coef_t); 1169 ALLOC(awk2, (st->lpcSize+1), spx_coef_t); 1170 ALLOC(awk3, (st->lpcSize+1), spx_coef_t); 1171 1170 speex_move(st->excBuf, st->excBuf+st->frameSize, (2*st->max_pitch + st->subframeSize + 12)*sizeof(spx_word16_t)); 1172 1171 for (sub=0;sub<st->nbSubframes;sub++) 1173 1172 { 1174 1173 int offset; 1175 spx_sig_t *sp, *exc; 1174 spx_word16_t *sp; 1175 spx_word16_t *exc; 1176 1176 /* Offset relative to start of frame */ 1177 1177 offset = st->subframeSize*sub; 1178 1178 /* Original signal */ 1179 sp= st->frame+offset;1179 sp=out+offset; 1180 1180 /* Excitation */ 1181 1181 exc=st->exc+offset; 1182 1182 /* Excitation after post-filter*/ 1183 1184 /* Calculate perceptually enhanced LPC filter */1185 if (st->lpc_enh_enabled)1186 {1187 spx_word16_t k1,k2,k3;1188 if (st->submodes[st->submodeID] != NULL)1189 {1190 k1=SUBMODE(lpc_enh_k1);1191 k2=SUBMODE(lpc_enh_k2);1192 k3=SUBMODE(lpc_enh_k3);1193 } else {1194 k1=k2=.7*GAMMA_SCALING;1195 k3=.0;1196 }1197 bw_lpc(k1, st->interp_qlpc, awk1, st->lpcSize);1198 bw_lpc(k2, st->interp_qlpc, awk2, st->lpcSize);1199 bw_lpc(k3, st->interp_qlpc, awk3, st->lpcSize);1200 }1201 1183 1202 1184 /* Make up a plausible excitation */ … … 1204 1186 /*if (pitch_gain>.95) 1205 1187 pitch_gain=.95;*/ 1206 innov_gain = compute_rms(st->innov, st->frameSize); 1188 1189 /* FIXME: This was rms of innovation (not exc) */ 1190 innov_gain = compute_rms16(st->exc, st->frameSize); 1207 1191 pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT); 1208 1192 if (pitch_val > st->max_pitch) … … 1212 1196 for (i=0;i<st->subframeSize;i++) 1213 1197 { 1214 exc[i]= MULT16_32_Q15(pitch_gain, (exc[i-pitch_val]+VERY_SMALL)) +1215 MULT16_32_Q15(fact, MULT16_32_Q15(SHL(Q15ONE,15)-SHL(MULT16_16(pitch_gain,pitch_gain),1),speex_rand(innov_gain, &st->seed)));1216 }1217 1198 /* FIXME: Second term need to be 16-bit */ 1199 exc[i]= MULT16_16_Q15(pitch_gain, (exc[i-pitch_val]+VERY_SMALL)) + 1200 MULT16_16_Q15(fact, MULT16_16_Q15(SHL(Q15ONE,15)-SHL(MULT16_16(pitch_gain,pitch_gain),1),speex_rand(innov_gain, &st->seed))); 1201 } 1218 1202 for (i=0;i<st->subframeSize;i++) 1219 sp[i]=exc[i]; 1220 1221 /* Signal synthesis */ 1222 if (st->lpc_enh_enabled) 1223 { 1224 filter_mem2(sp, awk2, awk1, sp, st->subframeSize, st->lpcSize, 1225 st->mem_sp+st->lpcSize); 1226 filter_mem2(sp, awk3, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 1227 st->mem_sp); 1228 } else { 1229 for (i=0;i<st->lpcSize;i++) 1230 st->mem_sp[st->lpcSize+i] = 0; 1231 iir_mem2(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 1232 st->mem_sp); 1233 } 1234 } 1235 1236 for (i=0;i<st->frameSize;i++) 1237 { 1238 spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT); 1239 if (sig>32767) 1240 sig = 32767; 1241 if (sig<-32767) 1242 sig = -32767; 1243 out[i]=sig; 1203 sp[i]=exc[i-st->subframeSize]; 1204 iir_mem16(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 1205 st->mem_sp, stack); 1206 1207 bw_lpc(QCONST16(.98,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize); 1244 1208 } 1245 1209 … … 1250 1214 st->pitch_gain_buf_idx = 0; 1251 1215 } 1216 1252 1217 1253 1218 int nb_decode(void *state, SpeexBits *bits, void *vout) … … 1265 1230 int m; 1266 1231 char *stack; 1267 VARDECL(spx_coef_t *awk1); 1268 VARDECL(spx_coef_t *awk2); 1269 VARDECL(spx_coef_t *awk3); 1232 VARDECL(spx_sig_t *innov); 1233 VARDECL(spx_word32_t *exc32); 1234 VARDECL(spx_coef_t *ak); 1235 VARDECL(spx_lsp_t *qlsp); 1270 1236 spx_word16_t pitch_average=0; 1271 1237 #ifdef EPIC_48K … … 1274 1240 #endif 1275 1241 spx_word16_t *out = vout; 1242 VARDECL(spx_lsp_t *interp_qlsp); 1276 1243 1277 1244 st=(DecState*)state; … … 1374 1341 1375 1342 /* Shift all buffers by one frame */ 1376 speex_move(st->excBuf, st->excBuf+st->frameSize, ( st->max_pitch + 1)*sizeof(spx_sig_t));1343 speex_move(st->excBuf, st->excBuf+st->frameSize, (2*st->max_pitch + st->subframeSize + 12)*sizeof(spx_word16_t)); 1377 1344 1378 1345 /* If null mode (no transmission), just set a couple things to zero*/ … … 1387 1354 if (pgain>.6) 1388 1355 pgain=.6; 1389 innov_gain = compute_rms(st->innov, st->frameSize); 1356 /* FIXME: This was innov, not exc */ 1357 innov_gain = compute_rms16(st->exc, st->frameSize); 1390 1358 for (i=0;i<st->frameSize;i++) 1391 st->exc[i]=VERY_SMALL; 1392 speex_rand_vec(innov_gain, st->exc, st->frameSize); 1359 st->exc[i]=speex_rand(innov_gain, &st->seed); 1393 1360 } 1394 1361 … … 1396 1363 st->first=1; 1397 1364 1365 for (i=0;i<st->frameSize;i++) 1366 out[i] = st->exc[i]; 1398 1367 /* Final signal synthesis from excitation */ 1399 iir_mem2(st->exc, lpc, st->frame, st->frameSize, st->lpcSize, st->mem_sp); 1400 1401 for (i=0;i<st->frameSize;i++) 1402 { 1403 spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT); 1404 if (sig>32767) 1405 sig = 32767; 1406 if (sig<-32767) 1407 sig = -32767; 1408 out[i]=sig; 1409 } 1368 iir_mem16(out, lpc, out, st->frameSize, st->lpcSize, st->mem_sp, stack); 1410 1369 1411 1370 st->count_lost=0; … … 1413 1372 } 1414 1373 1374 ALLOC(qlsp, st->lpcSize, spx_lsp_t); 1375 1415 1376 /* Unquantize LSPs */ 1416 SUBMODE(lsp_unquant)( st->qlsp, st->lpcSize, bits);1377 SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits); 1417 1378 1418 1379 /*Damp memory if a frame was lost and the LSP changed too much*/ … … 1422 1383 spx_word32_t lsp_dist=0; 1423 1384 for (i=0;i<st->lpcSize;i++) 1424 lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - st->qlsp[i])));1385 lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i]))); 1425 1386 #ifdef FIXED_POINT 1426 1387 fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2)); … … 1428 1389 fact = .6*exp(-.2*lsp_dist); 1429 1390 #endif 1430 for (i=0;i< 2*st->lpcSize;i++)1391 for (i=0;i<st->lpcSize;i++) 1431 1392 st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]); 1432 1393 } … … 1437 1398 { 1438 1399 for (i=0;i<st->lpcSize;i++) 1439 st->old_qlsp[i] = st->qlsp[i];1400 st->old_qlsp[i] = qlsp[i]; 1440 1401 } 1441 1402 … … 1484 1445 #endif 1485 1446 1486 ALLOC(a wk1, st->lpcSize+1, spx_coef_t);1487 ALLOC( awk2, st->lpcSize+1, spx_coef_t);1488 ALLOC( awk3, st->lpcSize+1, spx_coef_t);1447 ALLOC(ak, st->lpcSize, spx_coef_t); 1448 ALLOC(innov, st->subframeSize, spx_sig_t); 1449 ALLOC(exc32, st->subframeSize, spx_word32_t); 1489 1450 1490 1451 if (st->submodeID==1) … … 1505 1466 { 1506 1467 int offset; 1507 spx_sig_t *sp, *exc; 1468 spx_word16_t *exc; 1469 spx_word16_t *sp; 1470 spx_sig_t *innov_save = NULL; 1508 1471 spx_word16_t tmp; 1509 1472 … … 1520 1483 /* Offset relative to start of frame */ 1521 1484 offset = st->subframeSize*sub; 1522 /* Original signal */1523 sp=st->frame+offset;1524 1485 /* Excitation */ 1525 1486 exc=st->exc+offset; 1526 /* Excitation after post-filter*/ 1527 1528 /* LSP interpolation (quantized and unquantized) */ 1529 lsp_interpolate(st->old_qlsp, st->qlsp, st->interp_qlsp, st->lpcSize, sub, st->nbSubframes); 1530 1531 /* Make sure the LSP's are stable */ 1532 lsp_enforce_margin(st->interp_qlsp, st->lpcSize, LSP_MARGIN); 1533 1534 1535 /* Compute interpolated LPCs (unquantized) */ 1536 lsp_to_lpc(st->interp_qlsp, st->interp_qlpc, st->lpcSize, stack); 1537 1538 /* Compute enhanced synthesis filter */ 1539 if (st->lpc_enh_enabled) 1540 { 1541 bw_lpc(SUBMODE(lpc_enh_k1), st->interp_qlpc, awk1, st->lpcSize); 1542 bw_lpc(SUBMODE(lpc_enh_k2), st->interp_qlpc, awk2, st->lpcSize); 1543 bw_lpc(SUBMODE(lpc_enh_k3), st->interp_qlpc, awk3, st->lpcSize); 1544 } 1545 1546 /* Compute analysis filter at w=pi */ 1547 { 1548 spx_word32_t pi_g=LPC_SCALING; 1549 for (i=0;i<st->lpcSize;i+=2) 1550 { 1551 /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/ 1552 pi_g = ADD32(pi_g, SUB32(st->interp_qlpc[i+1],st->interp_qlpc[i])); 1553 } 1554 st->pi_gain[sub] = pi_g; 1555 } 1487 /* Original signal */ 1488 sp=out+offset; 1489 if (st->innov_save) 1490 innov_save = st->innov_save+offset; 1491 1556 1492 1557 1493 /* Reset excitation */ … … 1596 1532 if (st->lbr_48k) 1597 1533 { 1598 SUBMODE(ltp_unquant)(exc, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),1534 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params), 1599 1535 st->subframeSize, &pitch, &pitch_gain[0], bits, stack, 1600 1536 st->count_lost, offset, st->last_pitch_gain, ol_pitch_id); … … 1602 1538 #endif 1603 1539 1604 SUBMODE(ltp_unquant)(exc, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),1540 SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params), 1605 1541 st->subframeSize, &pitch, &pitch_gain[0], bits, stack, 1606 1542 st->count_lost, offset, st->last_pitch_gain, 0); … … 1610 1546 #endif 1611 1547 1612 1613 /* If we had lost frames, check energy of last received frame */1614 if (st->count_lost && ol_gain < st->last_ol_gain)1615 {1616 /*float fact = (float)ol_gain/(st->last_ol_gain+1);1617 for (i=0;i<st->subframeSize;i++)1618 exc[i]*=fact;*/1619 spx_word16_t fact = DIV32_16(SHL32(EXTEND32(ol_gain),15),st->last_ol_gain+1);1620 for (i=0;i<st->subframeSize;i++)1621 exc[i] = MULT16_32_Q15(fact, exc[i]);1622 }1623 1624 1548 tmp = gain_3tap_to_1tap(pitch_gain); 1625 1549 1626 1550 pitch_average += tmp; 1627 if (tmp>best_pitch_gain) 1551 if ((tmp>best_pitch_gain&&ABS(2*best_pitch-pitch)>=3&&ABS(3*best_pitch-pitch)>=4&&ABS(4*best_pitch-pitch)>=5) 1552 || (tmp>MULT16_16_Q15(QCONST16(.6,15),best_pitch_gain)&&(ABS(best_pitch-2*pitch)<3||ABS(best_pitch-3*pitch)<4||ABS(best_pitch-4*pitch)<5)) 1553 || (MULT16_16_Q15(QCONST16(.67,15),tmp)>best_pitch_gain&&(ABS(2*best_pitch-pitch)<3||ABS(3*best_pitch-pitch)<4||ABS(4*best_pitch-pitch)<5)) ) 1628 1554 { 1629 1555 best_pitch = pitch; 1630 best_pitch_gain = tmp; 1556 if (tmp > best_pitch_gain) 1557 best_pitch_gain = tmp; 1631 1558 } 1632 1559 } else { … … 1638 1565 int q_energy; 1639 1566 spx_word32_t ener; 1640 spx_sig_t *innov;1641 1567 1642 innov = st->innov+sub*st->subframeSize;1643 1568 for (i=0;i<st->subframeSize;i++) 1644 1569 innov[i]=0; … … 1682 1607 { 1683 1608 if (st->voc_offset>=0) 1684 exc[st->voc_offset]= SIG_SCALING*sqrt(1.0*ol_pitch);1609 exc[st->voc_offset]=sqrt(1.0*ol_pitch); 1685 1610 st->voc_offset+=ol_pitch; 1686 1611 } … … 1694 1619 for (i=0;i<st->subframeSize;i++) 1695 1620 { 1696 float exci=exc[i]; 1697 exc[i]=.8*g*exc[i]*ol_gain/SIG_SCALING + .6*g*st->voc_m1*ol_gain/SIG_SCALING + .5*g*innov[i] - .5*g*st->voc_m2 + (1-g)*innov[i]; 1621 spx_word16_t exci=exc[i]; 1622 /* FIXME: cleanup the innov[i]/SIG_SCALING */ 1623 exc[i]=.8*g*exc[i]*PSHR32(ol_gain,SIG_SHIFT) + .6*g*st->voc_m1*PSHR32(ol_gain,SIG_SHIFT) + (1-.5*g)*PSHR32(innov[i],SIG_SHIFT) - .5*g*PSHR32(st->voc_m2,SIG_SHIFT); 1698 1624 st->voc_m1 = exci; 1699 1625 st->voc_m2=innov[i]; … … 1703 1629 } else { 1704 1630 for (i=0;i<st->subframeSize;i++) 1705 exc[i]= ADD32(exc[i],innov[i]);1631 exc[i]=PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT); 1706 1632 /*print_vec(exc, 40, "innov");*/ 1633 } 1634 if (innov_save) 1635 { 1636 for (i=0;i<st->subframeSize;i++) 1637 innov_save[i] = innov[i]; 1707 1638 } 1708 1639 /* Decode second codebook (only for some modes) */ … … 1715 1646 innov2[i]=0; 1716 1647 SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize, bits, stack); 1717 signal_mul(innov2, innov2, (spx_word32_t) (ener*(1/2.2)), st->subframeSize);1648 signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545,15),ener), st->subframeSize); 1718 1649 for (i=0;i<st->subframeSize;i++) 1719 exc[i] = ADD32(exc[i],innov2[i]); 1650 exc[i] = ADD16(exc[i],PSHR32(innov2[i],SIG_SHIFT)); 1651 if (innov_save) 1652 { 1653 for (i=0;i<st->subframeSize;i++) 1654 innov_save[i] = ADD32(innov_save[i],innov2[i]); 1655 } 1720 1656 stack = tmp_stack; 1721 1657 } 1722 1723 } 1724 1725 /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */ 1726 if (st->count_lost) 1727 { 1728 spx_word16_t exc_ener; 1729 spx_word32_t gain32; 1730 spx_word16_t gain; 1731 exc_ener = compute_rms (exc, st->subframeSize); 1732 gain32 = DIV32(ol_gain, ADD16(exc_ener,1)); 1658 } 1659 } 1660 1661 ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t); 1662 1663 if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost) 1664 { 1665 multicomb(st->exc-st->subframeSize, out, st->interp_qlpc, st->lpcSize, 2*st->subframeSize, best_pitch, 40, SUBMODE(comb_gain), stack); 1666 multicomb(st->exc+st->subframeSize, out+2*st->subframeSize, st->interp_qlpc, st->lpcSize, 2*st->subframeSize, best_pitch, 40, SUBMODE(comb_gain), stack); 1667 } else { 1668 for (i=0;i<st->frameSize;i++) 1669 out[i]=st->exc[i-st->subframeSize]; 1670 } 1671 1672 /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */ 1673 if (st->count_lost) 1674 { 1675 spx_word16_t exc_ener; 1676 spx_word32_t gain32; 1677 spx_word16_t gain; 1678 exc_ener = compute_rms16 (st->exc, st->frameSize); 1679 gain32 = PDIV32(ol_gain, ADD16(exc_ener,1)); 1733 1680 #ifdef FIXED_POINT 1734 1735 1736 1681 if (gain32 > 32768) 1682 gain32 = 32768; 1683 gain = EXTRACT16(gain32); 1737 1684 #else 1738 if (gain32 > 2) 1739 gain32=2; 1740 gain = gain32; 1741 #endif 1742 for (i=0;i<st->subframeSize;i++) 1743 exc[i] = MULT16_32_Q14(gain, exc[i]); 1744 } 1745 1746 for (i=0;i<st->subframeSize;i++) 1747 sp[i]=exc[i]; 1748 1749 /* Signal synthesis */ 1750 if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0) 1751 comb_filter(exc, sp, st->interp_qlpc, st->lpcSize, st->subframeSize, 1752 pitch, pitch_gain, SUBMODE(comb_gain), st->comb_mem); 1753 1754 if (st->lpc_enh_enabled) 1755 { 1756 /* Use enhanced LPC filter */ 1757 filter_mem2(sp, awk2, awk1, sp, st->subframeSize, st->lpcSize, 1758 st->mem_sp+st->lpcSize); 1759 filter_mem2(sp, awk3, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 1760 st->mem_sp); 1761 } else { 1762 /* Use regular filter */ 1763 for (i=0;i<st->lpcSize;i++) 1764 st->mem_sp[st->lpcSize+i] = 0; 1765 iir_mem2(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 1766 st->mem_sp); 1767 } 1768 } 1769 1770 /*Copy output signal*/ 1771 for (i=0;i<st->frameSize;i++) 1772 { 1773 spx_word32_t sig = PSHR32(st->frame[i],SIG_SHIFT); 1774 if (sig>32767) 1775 sig = 32767; 1776 if (sig<-32767) 1777 sig = -32767; 1778 out[i]=sig; 1685 if (gain32 > 2) 1686 gain32=2; 1687 gain = gain32; 1688 #endif 1689 for (i=0;i<st->frameSize;i++) 1690 { 1691 st->exc[i] = MULT16_16_Q14(gain, st->exc[i]); 1692 out[i]=st->exc[i-st->subframeSize]; 1693 } 1694 } 1695 1696 /*Loop on subframes */ 1697 for (sub=0;sub<st->nbSubframes;sub++) 1698 { 1699 int offset; 1700 spx_word16_t *sp; 1701 spx_word16_t *exc; 1702 /* Offset relative to start of frame */ 1703 offset = st->subframeSize*sub; 1704 /* Original signal */ 1705 sp=out+offset; 1706 /* Excitation */ 1707 exc=st->exc+offset; 1708 1709 /* LSP interpolation (quantized and unquantized) */ 1710 lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes); 1711 1712 /* Make sure the LSP's are stable */ 1713 lsp_enforce_margin(interp_qlsp, st->lpcSize, LSP_MARGIN); 1714 1715 /* Compute interpolated LPCs (unquantized) */ 1716 lsp_to_lpc(interp_qlsp, ak, st->lpcSize, stack); 1717 1718 /* Compute analysis filter at w=pi */ 1719 { 1720 spx_word32_t pi_g=LPC_SCALING; 1721 for (i=0;i<st->lpcSize;i+=2) 1722 { 1723 /*pi_g += -st->interp_qlpc[i] + st->interp_qlpc[i+1];*/ 1724 pi_g = ADD32(pi_g, SUB32(EXTEND32(st->interp_qlpc[i+1]),EXTEND32(st->interp_qlpc[i]))); 1725 } 1726 st->pi_gain[sub] = pi_g; 1727 } 1728 1729 iir_mem16(sp, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, 1730 st->mem_sp, stack); 1731 1732 for (i=0;i<st->lpcSize;i++) 1733 st->interp_qlpc[i] = ak[i]; 1734 1779 1735 } 1780 1736 … … 1784 1740 /* Store the LSPs for interpolation in the next frame */ 1785 1741 for (i=0;i<st->lpcSize;i++) 1786 st->old_qlsp[i] = st->qlsp[i];1742 st->old_qlsp[i] = qlsp[i]; 1787 1743 1788 1744 /* The next frame will not be the first (Duh!) */ … … 1840 1796 break; 1841 1797 case SPEEX_SET_ABR: 1842 st->abr_enabled = (*(int*)ptr); 1843 st->vbr_enabled = 1; 1844 { 1845 int i=10, rate, target; 1798 st->abr_enabled = (*(spx_int32_t*)ptr); 1799 st->vbr_enabled = st->abr_enabled!=0; 1800 if (st->vbr_enabled) 1801 { 1802 int i=10; 1803 spx_int32_t rate, target; 1846 1804 float vbr_qual; 1847 target = (*( int*)ptr);1805 target = (*(spx_int32_t*)ptr); 1848 1806 while (i>=0) 1849 1807 { … … 1865 1823 break; 1866 1824 case SPEEX_GET_ABR: 1867 (*( int*)ptr) = st->abr_enabled;1825 (*(spx_int32_t*)ptr) = st->abr_enabled; 1868 1826 break; 1869 1827 case SPEEX_SET_VBR_QUALITY: … … 1889 1847 break; 1890 1848 case SPEEX_GET_COMPLEXITY: 1891 (*( int*)ptr) = st->complexity;1849 (*(spx_int32_t*)ptr) = st->complexity; 1892 1850 break; 1893 1851 case SPEEX_SET_BITRATE: 1894 1852 { 1895 int i=10, rate, target; 1896 target = (*(int*)ptr); 1853 int i=10; 1854 spx_int32_t rate, target; 1855 target = (*(spx_int32_t*)ptr); 1897 1856 while (i>=0) 1898 1857 { … … 1907 1866 case SPEEX_GET_BITRATE: 1908 1867 if (st->submodes[st->submodeID]) 1909 (*( int*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize;1868 (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; 1910 1869 else 1911 (*( int*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize;1870 (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; 1912 1871 break; 1913 1872 case SPEEX_SET_SAMPLING_RATE: 1914 st->sampling_rate = (*( int*)ptr);1873 st->sampling_rate = (*(spx_int32_t*)ptr); 1915 1874 break; 1916 1875 case SPEEX_GET_SAMPLING_RATE: 1917 (*( int*)ptr)=st->sampling_rate;1876 (*(spx_int32_t*)ptr)=st->sampling_rate; 1918 1877 break; 1919 1878 case SPEEX_RESET_STATE: … … 1923 1882 st->first = 1; 1924 1883 for (i=0;i<st->lpcSize;i++) 1925 st-> lsp[i]=(M_PI*((float)(i+1)))/(st->lpcSize+1);1884 st->old_lsp[i]=(M_PI*((float)(i+1)))/(st->lpcSize+1); 1926 1885 for (i=0;i<st->lpcSize;i++) 1927 1886 st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0; 1928 1887 for (i=0;i<st->frameSize+st->max_pitch+1;i++) 1929 1888 st->excBuf[i]=st->swBuf[i]=0; 1930 for (i=0;i<st->windowSize ;i++)1931 st-> inBuf[i]=0;1889 for (i=0;i<st->windowSize-st->frameSize;i++) 1890 st->winBuf[i]=0; 1932 1891 } 1933 1892 break; … … 1949 1908 (*(int*)ptr)=(st->plc_tuning); 1950 1909 break; 1910 case SPEEX_SET_VBR_MAX_BITRATE: 1911 st->vbr_max = (*(spx_int32_t*)ptr); 1912 break; 1913 case SPEEX_GET_VBR_MAX_BITRATE: 1914 (*(spx_int32_t*)ptr) = st->vbr_max; 1915 break; 1916 1917 1918 /* This is all internal stuff past this point */ 1951 1919 case SPEEX_GET_PI_GAIN: 1952 1920 { … … 1960 1928 { 1961 1929 int i; 1962 spx_ sig_t *e = (spx_sig_t*)ptr;1930 spx_word16_t *e = (spx_word16_t*)ptr; 1963 1931 for (i=0;i<st->frameSize;i++) 1964 1932 e[i]=st->exc[i]; 1965 1933 } 1966 1934 break; 1967 case SPEEX_GET_INNOV:1968 {1969 int i;1970 spx_sig_t *e = (spx_sig_t*)ptr;1971 for (i=0;i<st->frameSize;i++)1972 e[i]=st->innov[i];1973 }1974 break;1975 1935 case SPEEX_GET_RELATIVE_QUALITY: 1976 1936 (*(float*)ptr)=st->relative_quality; 1937 break; 1938 case SPEEX_SET_INNOVATION_SAVE: 1939 st->innov_save = ptr; 1977 1940 break; 1978 1941 default: … … 2008 1971 case SPEEX_GET_BITRATE: 2009 1972 if (st->submodes[st->submodeID]) 2010 (*( int*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize;1973 (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/st->frameSize; 2011 1974 else 2012 (*( int*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize;1975 (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/st->frameSize; 2013 1976 break; 2014 1977 case SPEEX_SET_SAMPLING_RATE: 2015 st->sampling_rate = (*( int*)ptr);1978 st->sampling_rate = (*(spx_int32_t*)ptr); 2016 1979 break; 2017 1980 case SPEEX_GET_SAMPLING_RATE: 2018 (*( int*)ptr)=st->sampling_rate;1981 (*(spx_int32_t*)ptr)=st->sampling_rate; 2019 1982 break; 2020 1983 case SPEEX_SET_HANDLER: … … 2037 2000 { 2038 2001 int i; 2039 for (i=0;i< 2*st->lpcSize;i++)2002 for (i=0;i<st->lpcSize;i++) 2040 2003 st->mem_sp[i]=0; 2041 2004 for (i=0;i<st->frameSize + st->max_pitch + 1;i++) 2042 2005 st->excBuf[i]=0; 2043 for (i=0;i<st->frameSize;i++)2044 st->inBuf[i] = 0;2045 2006 } 2046 2007 break; … … 2050 2011 case SPEEX_GET_SUBMODE_ENCODING: 2051 2012 (*(int*)ptr) = st->encode_submode; 2013 break; 2014 case SPEEX_GET_LOOKAHEAD: 2015 (*(int*)ptr)=st->subframeSize; 2052 2016 break; 2053 2017 case SPEEX_GET_PI_GAIN: … … 2062 2026 { 2063 2027 int i; 2064 spx_ sig_t *e = (spx_sig_t*)ptr;2028 spx_word16_t *e = (spx_word16_t*)ptr; 2065 2029 for (i=0;i<st->frameSize;i++) 2066 2030 e[i]=st->exc[i]; 2067 2031 } 2068 2032 break; 2069 case SPEEX_GET_INNOV:2070 {2071 int i;2072 spx_sig_t *e = (spx_sig_t*)ptr;2073 for (i=0;i<st->frameSize;i++)2074 e[i]=st->innov[i];2075 }2076 break;2077 2033 case SPEEX_GET_DTX_STATUS: 2078 2034 *((int*)ptr) = st->dtx_enabled; 2035 break; 2036 case SPEEX_SET_INNOVATION_SAVE: 2037 st->innov_save = ptr; 2079 2038 break; 2080 2039 default:
Note: See TracChangeset
for help on using the changeset viewer.