Changeset 628 for pjproject/trunk/pjmedia/src/pjmedia-codec/speex/ltp.c
- Timestamp:
- Jul 26, 2006 5:04:54 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia-codec/speex/ltp.c
r516 r628 56 56 57 57 #ifndef OVERRIDE_INNER_PROD 58 s tatic spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)58 spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len) 59 59 { 60 60 spx_word32_t sum=0; … … 76 76 #ifndef OVERRIDE_PITCH_XCORR 77 77 #if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */ 78 staticvoid pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)78 void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) 79 79 { 80 80 int i,j; … … 139 139 } 140 140 #else 141 staticvoid pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)141 void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) 142 142 { 143 143 int i; … … 153 153 154 154 #ifndef OVERRIDE_COMPUTE_PITCH_ERROR 155 static inline spx_word32_t compute_pitch_error(spx_word 32_t *C, spx_word16_t *g, spx_word16_t pitch_control)155 static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control) 156 156 { 157 157 spx_word32_t sum = 0; 158 sum = ADD32(sum,MULT16_ 32_Q15(MULT16_16_16(g[0],pitch_control),C[0]));159 sum = ADD32(sum,MULT16_ 32_Q15(MULT16_16_16(g[1],pitch_control),C[1]));160 sum = ADD32(sum,MULT16_ 32_Q15(MULT16_16_16(g[2],pitch_control),C[2]));161 sum = SUB32(sum,MULT16_ 32_Q15(MULT16_16_16(g[0],g[1]),C[3]));162 sum = SUB32(sum,MULT16_ 32_Q15(MULT16_16_16(g[2],g[1]),C[4]));163 sum = SUB32(sum,MULT16_ 32_Q15(MULT16_16_16(g[2],g[0]),C[5]));164 sum = SUB32(sum,MULT16_ 32_Q15(MULT16_16_16(g[0],g[0]),C[6]));165 sum = SUB32(sum,MULT16_ 32_Q15(MULT16_16_16(g[1],g[1]),C[7]));166 sum = SUB32(sum,MULT16_ 32_Q15(MULT16_16_16(g[2],g[2]),C[8]));158 sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0])); 159 sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1])); 160 sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2])); 161 sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3])); 162 sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4])); 163 sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5])); 164 sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6])); 165 sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7])); 166 sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8])); 167 167 return sum; 168 168 } 169 169 #endif 170 170 171 void open_loop_nbest_pitch(spx_sig_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack) 171 #ifndef OVERRIDE_OPEN_LOOP_NBEST_PITCH 172 void open_loop_nbest_pitch(spx_word16_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack) 172 173 { 173 174 int i,j,k; 174 175 VARDECL(spx_word32_t *best_score); 176 VARDECL(spx_word32_t *best_ener); 175 177 spx_word32_t e0; 176 178 VARDECL(spx_word32_t *corr); 177 179 VARDECL(spx_word32_t *energy); 178 VARDECL(spx_word32_t *score);179 VARDECL(spx_word16_t *swn2);180 spx_word16_t *swn;181 180 182 181 ALLOC(best_score, N, spx_word32_t); 182 ALLOC(best_ener, N, spx_word32_t); 183 183 ALLOC(corr, end-start+1, spx_word32_t); 184 184 ALLOC(energy, end-start+2, spx_word32_t); 185 ALLOC(score, end-start+1, spx_word32_t);186 187 #ifdef FIXED_POINT188 ALLOC(swn2, end+len, spx_word16_t);189 normalize16(sw-end, swn2, 16384, end+len);190 swn = swn2 + end;191 #else192 swn = sw;193 #endif194 185 195 186 for (i=0;i<N;i++) 196 187 { 197 188 best_score[i]=-1; 189 best_ener[i]=0; 198 190 pitch[i]=start; 199 191 } 200 192 201 202 energy[0]=inner_prod(swn-start, swn-start, len); 203 e0=inner_prod(swn, swn, len); 204 for (i=start;i<=end;i++) 193 energy[0]=inner_prod(sw-start, sw-start, len); 194 e0=inner_prod(sw, sw, len); 195 for (i=start;i<end;i++) 205 196 { 206 197 /* Update energy for next pitch*/ 207 energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw n[-i-1],swn[-i-1]),6)), SHR32(MULT16_16(swn[-i+len-1],swn[-i+len-1]),6));198 energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6)); 208 199 if (energy[i-start+1] < 0) 209 200 energy[i-start+1] = 0; 210 201 } 211 202 212 pitch_xcorr(swn, swn-end, corr, len, end-start+1, stack); 213 203 pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack); 204 205 /* FIXME: Fixed-point and floating-point code should be merged */ 214 206 #ifdef FIXED_POINT 215 207 { … … 218 210 ALLOC(corr16, end-start+1, spx_word16_t); 219 211 ALLOC(ener16, end-start+1, spx_word16_t); 220 normalize16(corr, corr16, 16384, end-start+1); 221 normalize16(energy, ener16, 16384, end-start+1); 212 /* Normalize to 180 so we can square it and it still fits in 16 bits */ 213 normalize16(corr, corr16, 180, end-start+1); 214 normalize16(energy, ener16, 180, end-start+1); 222 215 223 216 for (i=start;i<=end;i++) 224 217 { 225 spx_word16_t g; 226 spx_word32_t tmp; 227 tmp = corr16[i-start]; 228 if (tmp>0) 218 spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]); 219 /* Instead of dividing the tmp by the energy, we multiply on the other side */ 220 if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start]))) 229 221 { 230 if (SHR16(corr16[i-start],4)>ener16[i-start]) 231 tmp = SHL32(EXTEND32(ener16[i-start]),14); 232 else if (-SHR16(corr16[i-start],4)>ener16[i-start]) 233 tmp = -SHL32(EXTEND32(ener16[i-start]),14); 234 else 235 tmp = SHL32(tmp,10); 236 g = DIV32_16(tmp, 8+ener16[i-start]); 237 score[i-start] = MULT16_16(corr16[i-start],g); 238 } else 239 { 240 score[i-start] = 1; 222 /* We can safely put it last and then check */ 223 best_score[N-1]=tmp; 224 best_ener[N-1]=ener16[i-start]+1; 225 pitch[N-1]=i; 226 /* Check if it comes in front of others */ 227 for (j=0;j<N-1;j++) 228 { 229 if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start]))) 230 { 231 for (k=N-1;k>j;k--) 232 { 233 best_score[k]=best_score[k-1]; 234 best_ener[k]=best_ener[k-1]; 235 pitch[k]=pitch[k-1]; 236 } 237 best_score[j]=tmp; 238 best_ener[j]=ener16[i-start]+1; 239 pitch[j]=i; 240 break; 241 } 242 } 241 243 } 242 244 } … … 245 247 for (i=start;i<=end;i++) 246 248 { 247 float g = corr[i-start]/(1+energy[i-start]); 248 if (g>16) 249 g = 16; 250 else if (g<-16) 251 g = -16; 252 score[i-start] = g*corr[i-start]; 253 } 254 #endif 255 256 /* Extract best scores */ 257 for (i=start;i<=end;i++) 258 { 259 if (score[i-start]>best_score[N-1]) 249 float tmp = corr[i-start]*corr[i-start]; 250 if (tmp*best_ener[N-1]>best_score[N-1]*(1+energy[i-start])) 260 251 { 261 252 for (j=0;j<N;j++) 262 253 { 263 if ( score[i-start] > best_score[j])254 if (tmp*best_ener[j]>best_score[j]*(1+energy[i-start])) 264 255 { 265 256 for (k=N-1;k>j;k--) 266 257 { 267 258 best_score[k]=best_score[k-1]; 259 best_ener[k]=best_ener[k-1]; 268 260 pitch[k]=pitch[k-1]; 269 261 } 270 best_score[j]=score[i-start]; 262 best_score[j]=tmp; 263 best_ener[j]=energy[i-start]+1; 271 264 pitch[j]=i; 272 265 break; … … 275 268 } 276 269 } 270 #endif 277 271 278 272 /* Compute open-loop gain */ … … 291 285 } 292 286 } 293 287 #endif 288 289 #ifndef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ 290 static int pitch_gain_search_3tap_vq( 291 const signed char *gain_cdbk, 292 int gain_cdbk_size, 293 spx_word16_t *C16, 294 spx_word16_t max_gain 295 ) 296 { 297 const signed char *ptr=gain_cdbk; 298 int best_cdbk=0; 299 spx_word32_t best_sum=-VERY_LARGE32; 300 spx_word32_t sum=0; 301 spx_word16_t g[3]; 302 spx_word16_t pitch_control=64; 303 spx_word16_t gain_sum; 304 int i; 305 306 for (i=0;i<gain_cdbk_size;i++) { 307 308 ptr = gain_cdbk+4*i; 309 g[0]=ADD16((spx_word16_t)ptr[0],32); 310 g[1]=ADD16((spx_word16_t)ptr[1],32); 311 g[2]=ADD16((spx_word16_t)ptr[2],32); 312 gain_sum = (spx_word16_t)ptr[3]; 313 314 sum = compute_pitch_error(C16, g, pitch_control); 315 316 if (sum>best_sum && gain_sum<=max_gain) { 317 best_sum=sum; 318 best_cdbk=i; 319 } 320 } 321 322 return best_cdbk; 323 } 324 #endif 294 325 295 326 /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */ 296 static spx_word 64_t pitch_gain_search_3tap(297 const spx_ sig_t target[], /* Target vector */327 static spx_word32_t pitch_gain_search_3tap( 328 const spx_word16_t target[], /* Target vector */ 298 329 const spx_coef_t ak[], /* LPCs for this subframe */ 299 330 const spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */ 300 331 const spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */ 301 332 spx_sig_t exc[], /* Excitation */ 302 const void *par, 333 const signed char *gain_cdbk, 334 int gain_cdbk_size, 303 335 int pitch, /* Pitch value */ 304 336 int p, /* Number of LPC coeffs */ … … 306 338 SpeexBits *bits, 307 339 char *stack, 308 const spx_ sig_t *exc2,340 const spx_word16_t *exc2, 309 341 const spx_word16_t *r, 310 spx_ sig_t *new_target,342 spx_word16_t *new_target, 311 343 int *cdbk_index, 312 int cdbk_offset,313 int plc_tuning 344 int plc_tuning, 345 spx_word32_t cumul_gain 314 346 ) 315 347 { 316 348 int i,j; 317 VARDECL(spx_sig_t *tmp1); 318 VARDECL(spx_sig_t *tmp2); 319 spx_sig_t *x[3]; 320 spx_sig_t *e[3]; 349 VARDECL(spx_word16_t *tmp1); 350 VARDECL(spx_word16_t *e); 351 spx_word16_t *x[3]; 321 352 spx_word32_t corr[3]; 322 353 spx_word32_t A[3][3]; 323 int gain_cdbk_size;324 const signed char *gain_cdbk;325 354 spx_word16_t gain[3]; 326 spx_word64_t err; 327 328 const ltp_params *params; 329 params = (const ltp_params*) par; 330 gain_cdbk_size = 1<<params->gain_bits; 331 gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset; 332 ALLOC(tmp1, 3*nsf, spx_sig_t); 333 ALLOC(tmp2, 3*nsf, spx_sig_t); 334 355 spx_word32_t err; 356 spx_word16_t max_gain=128; 357 int best_cdbk=0; 358 359 ALLOC(tmp1, 3*nsf, spx_word16_t); 360 ALLOC(e, nsf, spx_word16_t); 361 362 if (cumul_gain > 262144) 363 max_gain = 31; 364 335 365 x[0]=tmp1; 336 366 x[1]=tmp1+nsf; 337 367 x[2]=tmp1+2*nsf; 338 368 339 e[0]=tmp2; 340 e[1]=tmp2+nsf; 341 e[2]=tmp2+2*nsf; 342 for (i=2;i>=0;i--) 343 { 344 int pp=pitch+1-i; 369 { 370 VARDECL(spx_mem_t *mm); 371 int pp=pitch-1; 372 ALLOC(mm, p, spx_mem_t); 345 373 for (j=0;j<nsf;j++) 346 374 { 347 375 if (j-pp<0) 348 e[ i][j]=exc2[j-pp];376 e[j]=exc2[j-pp]; 349 377 else if (j-pp-pitch<0) 350 e[ i][j]=exc2[j-pp-pitch];378 e[j]=exc2[j-pp-pitch]; 351 379 else 352 e[ i][j]=0;380 e[j]=0; 353 381 } 354 355 if (i==2) 356 syn_percep_zero(e[i], ak, awk1, awk2, x[i], nsf, p, stack); 357 else { 358 for (j=0;j<nsf-1;j++) 359 x[i][j+1]=x[i+1][j]; 360 x[i][0]=0; 361 for (j=0;j<nsf;j++) 362 { 363 x[i][j]=ADD32(x[i][j],SHL32(MULT16_32_Q15(r[j], e[i][0]),1)); 364 } 365 } 366 } 367 368 #ifdef FIXED_POINT 369 { 370 /* If using fixed-point, we need to normalize the signals first */ 371 spx_word16_t *y[3]; 372 VARDECL(spx_word16_t *ytmp); 373 VARDECL(spx_word16_t *t); 374 375 spx_sig_t max_val=1; 376 int sig_shift; 377 378 ALLOC(ytmp, 3*nsf, spx_word16_t); 379 #if 0 380 ALLOC(y[0], nsf, spx_word16_t); 381 ALLOC(y[1], nsf, spx_word16_t); 382 ALLOC(y[2], nsf, spx_word16_t); 383 #else 384 y[0] = ytmp; 385 y[1] = ytmp+nsf; 386 y[2] = ytmp+2*nsf; 387 #endif 388 ALLOC(t, nsf, spx_word16_t); 389 for (j=0;j<3;j++) 390 { 391 for (i=0;i<nsf;i++) 392 { 393 spx_sig_t tmp = x[j][i]; 394 if (tmp<0) 395 tmp = -tmp; 396 if (tmp > max_val) 397 max_val = tmp; 398 } 399 } 400 for (i=0;i<nsf;i++) 401 { 402 spx_sig_t tmp = target[i]; 403 if (tmp<0) 404 tmp = -tmp; 405 if (tmp > max_val) 406 max_val = tmp; 407 } 408 409 sig_shift=0; 410 while (max_val>16384) 411 { 412 sig_shift++; 413 max_val >>= 1; 414 } 415 416 for (j=0;j<3;j++) 417 { 418 for (i=0;i<nsf;i++) 419 { 420 y[j][i] = EXTRACT16(SHR32(x[j][i],sig_shift)); 421 } 422 } 423 for (i=0;i<nsf;i++) 424 { 425 t[i] = EXTRACT16(SHR32(target[i],sig_shift)); 426 } 427 428 for (i=0;i<3;i++) 429 corr[i]=inner_prod(y[i],t,nsf); 430 431 for (i=0;i<3;i++) 432 for (j=0;j<=i;j++) 433 A[i][j]=A[j][i]=inner_prod(y[i],y[j],nsf); 434 } 435 #else 436 { 437 for (i=0;i<3;i++) 438 corr[i]=inner_prod(x[i],target,nsf); 439 440 for (i=0;i<3;i++) 441 for (j=0;j<=i;j++) 442 A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf); 443 } 444 #endif 382 for (j=0;j<p;j++) 383 mm[j] = 0; 384 iir_mem16(e, ak, e, nsf, p, mm, stack); 385 for (j=0;j<p;j++) 386 mm[j] = 0; 387 filter_mem16(e, awk1, awk2, e, nsf, p, mm, stack); 388 for (j=0;j<nsf;j++) 389 x[2][j] = e[j]; 390 } 391 for (i=1;i>=0;i--) 392 { 393 spx_word16_t e0=exc2[-pitch-1+i]; 394 x[i][0]=MULT16_16_Q14(r[0], e0); 395 for (j=0;j<nsf-1;j++) 396 x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0)); 397 } 398 399 for (i=0;i<3;i++) 400 corr[i]=inner_prod(x[i],target,nsf); 401 for (i=0;i<3;i++) 402 for (j=0;j<=i;j++) 403 A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf); 445 404 446 405 { 447 406 spx_word32_t C[9]; 448 const signed char *ptr=gain_cdbk; 449 int best_cdbk=0; 450 spx_word32_t best_sum=0; 407 #ifdef FIXED_POINT 408 spx_word16_t C16[9]; 409 #else 410 spx_word16_t *C16=C; 411 #endif 451 412 C[0]=corr[2]; 452 413 C[1]=corr[1]; … … 462 423 if (plc_tuning<2) 463 424 plc_tuning=2; 464 #ifdef FIXED_POINT 465 C[0] = MAC16_32_Q15(C[0],MULT16_16_16(plc_tuning,-327),C[0]); 466 C[1] = MAC16_32_Q15(C[1],MULT16_16_16(plc_tuning,-327),C[1]); 467 C[2] = MAC16_32_Q15(C[2],MULT16_16_16(plc_tuning,-327),C[2]); 425 if (plc_tuning>30) 426 plc_tuning=30; 427 #ifdef FIXED_POINT 468 428 C[0] = SHL32(C[0],1); 469 429 C[1] = SHL32(C[1],1); … … 472 432 C[4] = SHL32(C[4],1); 473 433 C[5] = SHL32(C[5],1); 474 #else 475 C[0]*=1-.01*plc_tuning; 476 C[1]*=1-.01*plc_tuning; 477 C[2]*=1-.01*plc_tuning; 478 C[6]*=.5*(1+.01*plc_tuning); 479 C[7]*=.5*(1+.01*plc_tuning); 480 C[8]*=.5*(1+.01*plc_tuning); 481 #endif 482 for (i=0;i<gain_cdbk_size;i++) 483 { 484 spx_word32_t sum=0; 485 spx_word16_t g[3]; 486 spx_word16_t pitch_control=64; 487 spx_word16_t gain_sum; 488 489 ptr = gain_cdbk+3*i; 490 g[0]=ADD16((spx_word16_t)ptr[0],32); 491 g[1]=ADD16((spx_word16_t)ptr[1],32); 492 g[2]=ADD16((spx_word16_t)ptr[2],32); 493 494 /* We favor "safe" pitch values to handle packet loss better */ 495 gain_sum = ADD16(ADD16(g[1],MAX16(g[0], 0)),MAX16(g[2], 0)); 496 if (gain_sum > 64) 497 { 498 gain_sum = SUB16(gain_sum, 64); 499 if (gain_sum > 127) 500 gain_sum = 127; 501 #ifdef FIXED_POINT 502 pitch_control = SUB16(64,EXTRACT16(PSHR32(MULT16_16(64,MULT16_16_16(plc_tuning, gain_sum)),10))); 503 #else 504 pitch_control = 64*(1.-.001*plc_tuning*gain_sum); 505 #endif 506 if (pitch_control < 0) 507 pitch_control = 0; 508 } 509 510 sum = compute_pitch_error(C, g, pitch_control); 511 512 if (sum>best_sum || i==0) 513 { 514 best_sum=sum; 515 best_cdbk=i; 516 } 517 } 518 #ifdef FIXED_POINT 519 gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3]); 520 gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3+1]); 521 gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*3+2]); 434 C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]); 435 C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]); 436 C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]); 437 normalize16(C, C16, 32767, 9); 438 #else 439 C[6]*=.5*(1+.02*plc_tuning); 440 C[7]*=.5*(1+.02*plc_tuning); 441 C[8]*=.5*(1+.02*plc_tuning); 442 #endif 443 444 best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain); 445 446 #ifdef FIXED_POINT 447 gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]); 448 gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]); 449 gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]); 522 450 /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/ 523 451 #else 524 gain[0] = 0.015625*gain_cdbk[best_cdbk* 3] + .5;525 gain[1] = 0.015625*gain_cdbk[best_cdbk* 3+1]+ .5;526 gain[2] = 0.015625*gain_cdbk[best_cdbk* 3+2]+ .5;452 gain[0] = 0.015625*gain_cdbk[best_cdbk*4] + .5; 453 gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5; 454 gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5; 527 455 #endif 528 456 *cdbk_index=best_cdbk; 529 457 } 530 458 531 #ifdef FIXED_POINT532 459 for (i=0;i<nsf;i++) 533 exc[i]=SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),e[2][i]), MULT16_32_Q15(SHL16(gain[1],7),e[1][i])), 534 MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2); 535 536 err=0; 460 exc[i]=0; 461 for (i=0;i<3;i++) 462 { 463 int j; 464 int tmp1, tmp3; 465 int pp=pitch+1-i; 466 tmp1=nsf; 467 if (tmp1>pp) 468 tmp1=pp; 469 for (j=0;j<tmp1;j++) 470 exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]); 471 tmp3=nsf; 472 if (tmp3>pp+pitch) 473 tmp3=pp+pitch; 474 for (j=tmp1;j<tmp3;j++) 475 exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]); 476 } 537 477 for (i=0;i<nsf;i++) 538 478 { 539 spx_word16_t perr2; 540 spx_sig_t tmp = SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),x[2][i]),MULT16_32_Q15(SHL16(gain[1],7),x[1][i])), 541 MULT16_32_Q15(SHL16(gain[2],7),x[0][i])),2); 542 spx_sig_t perr=SUB32(target[i],tmp); 543 new_target[i] = SUB32(target[i], tmp); 544 perr2 = EXTRACT16(PSHR32(perr,15)); 545 err = ADD64(err,MULT16_16(perr2,perr2)); 546 547 } 548 #else 549 for (i=0;i<nsf;i++) 550 exc[i]=gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i]; 551 552 err=0; 553 for (i=0;i<nsf;i++) 554 { 555 spx_sig_t tmp = gain[2]*x[0][i]+gain[1]*x[1][i]+gain[0]*x[2][i]; 556 new_target[i] = target[i] - tmp; 557 err+=new_target[i]*new_target[i]; 558 } 559 #endif 479 spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])), 480 MULT16_16(gain[2],x[0][i])); 481 new_target[i] = SUB16(target[i], EXTRACT16(PSHR32(tmp,6))); 482 } 483 err = inner_prod(new_target, new_target, nsf); 560 484 561 485 return err; 562 486 } 563 564 487 565 488 /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */ 566 489 int pitch_search_3tap( 567 spx_ sig_t target[], /* Target vector */568 spx_ sig_t *sw,490 spx_word16_t target[], /* Target vector */ 491 spx_word16_t *sw, 569 492 spx_coef_t ak[], /* LPCs for this subframe */ 570 493 spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */ … … 579 502 SpeexBits *bits, 580 503 char *stack, 581 spx_ sig_t *exc2,504 spx_word16_t *exc2, 582 505 spx_word16_t *r, 583 506 int complexity, 584 507 int cdbk_offset, 585 int plc_tuning 508 int plc_tuning, 509 spx_word32_t *cumul_gain 586 510 ) 587 511 { … … 589 513 int cdbk_index, pitch=0, best_gain_index=0; 590 514 VARDECL(spx_sig_t *best_exc); 591 VARDECL(spx_ sig_t *new_target);592 VARDECL(spx_ sig_t *best_target);515 VARDECL(spx_word16_t *new_target); 516 VARDECL(spx_word16_t *best_target); 593 517 int best_pitch=0; 594 spx_word 64_t err, best_err=-1;518 spx_word32_t err, best_err=-1; 595 519 int N; 596 520 const ltp_params *params; 521 const signed char *gain_cdbk; 522 int gain_cdbk_size; 523 597 524 VARDECL(int *nbest); 598 525 526 params = (const ltp_params*) par; 527 gain_cdbk_size = 1<<params->gain_bits; 528 gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset; 529 599 530 N=complexity; 600 531 if (N>10) … … 614 545 return start; 615 546 } 616 617 ALLOC(best_exc, nsf, spx_sig_t);618 ALLOC(new_target, nsf, spx_sig_t);619 ALLOC(best_target, nsf, spx_sig_t);620 547 621 548 if (N>end-start+1) … … 625 552 else 626 553 nbest[0] = start; 554 555 ALLOC(best_exc, nsf, spx_sig_t); 556 ALLOC(new_target, nsf, spx_word16_t); 557 ALLOC(best_target, nsf, spx_word16_t); 558 627 559 for (i=0;i<N;i++) 628 560 { … … 630 562 for (j=0;j<nsf;j++) 631 563 exc[j]=0; 632 err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, par, pitch, p, nsf,633 bits, stack, exc2, r, new_target, &cdbk_index, cdbk_offset, plc_tuning);564 err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf, 565 bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain); 634 566 if (err<best_err || best_err<0) 635 567 { … … 643 575 } 644 576 } 645 646 577 /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/ 647 578 speex_bits_pack(bits, best_pitch-start, params->pitch_bits); 648 579 speex_bits_pack(bits, best_gain_index, params->gain_bits); 580 #ifdef FIXED_POINT 581 *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain)); 582 #else 583 *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3]; 584 #endif 585 /*printf ("%f\n", cumul_gain);*/ 649 586 /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/ 650 587 for (i=0;i<nsf;i++) … … 657 594 658 595 void pitch_unquant_3tap( 659 spx_sig_t exc[], /* Excitation */ 596 spx_word16_t exc[], /* Input excitation */ 597 spx_word32_t exc_out[], /* Output excitation */ 660 598 int start, /* Smallest pitch value allowed */ 661 599 int end, /* Largest pitch value allowed */ 662 spx_word16_t pitch_coef, 600 spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */ 663 601 const void *par, 664 602 int nsf, /* Number of samples in subframe */ … … 683 621 params = (const ltp_params*) par; 684 622 gain_cdbk_size = 1<<params->gain_bits; 685 gain_cdbk = params->gain_cdbk + 3*gain_cdbk_size*cdbk_offset;623 gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset; 686 624 687 625 pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits); … … 690 628 /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/ 691 629 #ifdef FIXED_POINT 692 gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index* 3]);693 gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index* 3+1]);694 gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index* 3+2]);695 #else 696 gain[0] = 0.015625*gain_cdbk[gain_index* 3]+.5;697 gain[1] = 0.015625*gain_cdbk[gain_index* 3+1]+.5;698 gain[2] = 0.015625*gain_cdbk[gain_index* 3+2]+.5;630 gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]); 631 gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]); 632 gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]); 633 #else 634 gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5; 635 gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5; 636 gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5; 699 637 #endif 700 638 … … 729 667 gain_val[1]=gain[1]; 730 668 gain_val[2]=gain[2]; 731 732 { 733 spx_sig_t *e[3]; 734 VARDECL(spx_sig_t *tmp2); 735 ALLOC(tmp2, 3*nsf, spx_sig_t); 736 e[0]=tmp2; 737 e[1]=tmp2+nsf; 738 e[2]=tmp2+2*nsf; 739 740 for (i=0;i<3;i++) 741 { 742 int j; 743 int pp=pitch+1-i; 744 #if 0 745 for (j=0;j<nsf;j++) 746 { 747 if (j-pp<0) 748 e[i][j]=exc[j-pp]; 749 else if (j-pp-pitch<0) 750 e[i][j]=exc[j-pp-pitch]; 751 else 752 e[i][j]=0; 753 } 754 #else 755 { 756 int tmp1, tmp3; 757 tmp1=nsf; 758 if (tmp1>pp) 759 tmp1=pp; 760 for (j=0;j<tmp1;j++) 761 e[i][j]=exc[j-pp]; 762 tmp3=nsf; 763 if (tmp3>pp+pitch) 764 tmp3=pp+pitch; 765 for (j=tmp1;j<tmp3;j++) 766 e[i][j]=exc[j-pp-pitch]; 767 for (j=tmp3;j<nsf;j++) 768 e[i][j]=0; 769 } 770 #endif 771 } 772 773 #ifdef FIXED_POINT 774 { 775 for (i=0;i<nsf;i++) 776 exc[i]=SHL32(ADD32(ADD32(MULT16_32_Q15(SHL16(gain[0],7),e[2][i]), MULT16_32_Q15(SHL16(gain[1],7),e[1][i])), 777 MULT16_32_Q15(SHL16(gain[2],7),e[0][i])), 2); 778 } 779 #else 780 for (i=0;i<nsf;i++) 781 exc[i]=VERY_SMALL+gain[0]*e[2][i]+gain[1]*e[1][i]+gain[2]*e[0][i]; 782 #endif 783 } 669 gain[0] = SHL16(gain[0],7); 670 gain[1] = SHL16(gain[1],7); 671 gain[2] = SHL16(gain[2],7); 672 for (i=0;i<nsf;i++) 673 exc_out[i]=0; 674 for (i=0;i<3;i++) 675 { 676 int j; 677 int tmp1, tmp3; 678 int pp=pitch+1-i; 679 tmp1=nsf; 680 if (tmp1>pp) 681 tmp1=pp; 682 for (j=0;j<tmp1;j++) 683 exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]); 684 tmp3=nsf; 685 if (tmp3>pp+pitch) 686 tmp3=pp+pitch; 687 for (j=tmp1;j<tmp3;j++) 688 exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]); 689 } 690 /*for (i=0;i<nsf;i++) 691 exc[i]=PSHR32(exc32[i],13);*/ 784 692 } 785 693 … … 787 695 /** Forced pitch delay and gain */ 788 696 int forced_pitch_quant( 789 spx_ sig_t target[], /* Target vector */790 spx_ sig_t *sw,697 spx_word16_t target[], /* Target vector */ 698 spx_word16_t *sw, 791 699 spx_coef_t ak[], /* LPCs for this subframe */ 792 700 spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */ … … 801 709 SpeexBits *bits, 802 710 char *stack, 803 spx_ sig_t *exc2,711 spx_word16_t *exc2, 804 712 spx_word16_t *r, 805 713 int complexity, 806 714 int cdbk_offset, 807 int plc_tuning 715 int plc_tuning, 716 spx_word32_t *cumul_gain 808 717 ) 809 718 { 810 719 int i; 811 float coef = GAIN_SCALING_1*pitch_coef; 812 if (coef>.99) 813 coef=.99; 720 VARDECL(spx_sig_t *res); 721 ALLOC(res, nsf, spx_sig_t); 722 #ifdef FIXED_POINT 723 if (pitch_coef>63) 724 pitch_coef=63; 725 #else 726 if (pitch_coef>.99) 727 pitch_coef=.99; 728 #endif 729 for (i=0;i<nsf&&i<start;i++) 730 { 731 exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]); 732 } 733 for (;i<nsf;i++) 734 { 735 exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]); 736 } 737 syn_percep_zero(exc, ak, awk1, awk2, res, nsf, p, stack); 814 738 for (i=0;i<nsf;i++) 815 { 816 exc[i]=exc[i-start]*coef; 817 } 739 target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),PSHR32(res[i],SIG_SHIFT-1)),32700)); 818 740 return start; 819 741 } … … 821 743 /** Unquantize forced pitch delay and gain */ 822 744 void forced_pitch_unquant( 823 spx_sig_t exc[], /* Excitation */ 745 spx_word16_t exc[], /* Input excitation */ 746 spx_word32_t exc_out[], /* Output excitation */ 824 747 int start, /* Smallest pitch value allowed */ 825 748 int end, /* Largest pitch value allowed */ 826 spx_word16_t pitch_coef, 749 spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */ 827 750 const void *par, 828 751 int nsf, /* Number of samples in subframe */ … … 838 761 { 839 762 int i; 840 float coef = GAIN_SCALING_1*pitch_coef; 841 if (coef>.99) 842 coef=.99; 763 #ifdef FIXED_POINT 764 if (pitch_coef>63) 765 pitch_coef=63; 766 #else 767 if (pitch_coef>.99) 768 pitch_coef=.99; 769 #endif 843 770 for (i=0;i<nsf;i++) 844 771 { 845 exc[i]=exc[i-start]*coef; 772 exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7)); 773 exc[i] = PSHR(exc_out[i],13); 846 774 } 847 775 *pitch_val = start;
Note: See TracChangeset
for help on using the changeset viewer.