Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | |
| 2 | /****************************************************************** |
| 3 | |
| 4 | iLBC Speech Coder ANSI-C Source Code |
| 5 | |
| 6 | iCBSearch.c |
| 7 | |
| 8 | Copyright (C) The Internet Society (2004). |
| 9 | All Rights Reserved. |
| 10 | |
| 11 | ******************************************************************/ |
| 12 | |
| 13 | #include <math.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include "iLBC_define.h" |
| 17 | #include "gainquant.h" |
| 18 | #include "createCB.h" |
| 19 | #include "filter.h" |
| 20 | #include "constants.h" |
| 21 | |
| 22 | /*----------------------------------------------------------------* |
| 23 | * Search routine for codebook encoding and gain quantization. |
| 24 | *---------------------------------------------------------------*/ |
| 25 | |
| 26 | void iCBSearch( |
| 27 | iLBC_Enc_Inst_t *iLBCenc_inst, |
| 28 | /* (i) the encoder state structure */ |
| 29 | int *index, /* (o) Codebook indices */ |
| 30 | int *gain_index,/* (o) Gain quantization indices */ |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | float *intarget,/* (i) Target vector for encoding */ |
| 37 | float *mem, /* (i) Buffer for codebook construction */ |
| 38 | int lMem, /* (i) Length of buffer */ |
| 39 | int lTarget, /* (i) Length of vector */ |
| 40 | int nStages, /* (i) Number of codebook stages */ |
| 41 | float *weightDenum, /* (i) weighting filter coefficients */ |
| 42 | float *weightState, /* (i) weighting filter state */ |
| 43 | int block /* (i) the sub-block number */ |
| 44 | ){ |
| 45 | int i, j, icount, stage, best_index, range, counter; |
| 46 | float max_measure, gain, measure, crossDot, ftmp; |
| 47 | float gains[CB_NSTAGES]; |
| 48 | float target[SUBL]; |
| 49 | int base_index, sInd, eInd, base_size; |
| 50 | int sIndAug=0, eIndAug=0; |
| 51 | float buf[CB_MEML+SUBL+2*LPC_FILTERORDER]; |
| 52 | float invenergy[CB_EXPAND*128], energy[CB_EXPAND*128]; |
| 53 | float *pp, *ppi=0, *ppo=0, *ppe=0; |
| 54 | float cbvectors[CB_MEML]; |
| 55 | float tene, cene, cvec[SUBL]; |
| 56 | float aug_vec[SUBL]; |
| 57 | |
| 58 | memset(cvec,0,SUBL*sizeof(float)); |
| 59 | |
| 60 | /* Determine size of codebook sections */ |
| 61 | |
| 62 | base_size=lMem-lTarget+1; |
| 63 | |
| 64 | if (lTarget==SUBL) { |
| 65 | base_size=lMem-lTarget+1+lTarget/2; |
| 66 | } |
| 67 | |
| 68 | /* setup buffer for weighting */ |
| 69 | |
| 70 | memcpy(buf,weightState,sizeof(float)*LPC_FILTERORDER); |
| 71 | memcpy(buf+LPC_FILTERORDER,mem,lMem*sizeof(float)); |
| 72 | memcpy(buf+LPC_FILTERORDER+lMem,intarget,lTarget*sizeof(float)); |
| 73 | |
| 74 | /* weighting */ |
| 75 | |
| 76 | AllPoleFilter(buf+LPC_FILTERORDER, weightDenum, |
| 77 | lMem+lTarget, LPC_FILTERORDER); |
| 78 | |
| 79 | /* Construct the codebook and target needed */ |
| 80 | |
| 81 | memcpy(target, buf+LPC_FILTERORDER+lMem, lTarget*sizeof(float)); |
| 82 | |
| 83 | tene=0.0; |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | for (i=0; i<lTarget; i++) { |
| 90 | tene+=target[i]*target[i]; |
| 91 | } |
| 92 | |
| 93 | /* Prepare search over one more codebook section. This section |
| 94 | is created by filtering the original buffer with a filter. */ |
| 95 | |
| 96 | filteredCBvecs(cbvectors, buf+LPC_FILTERORDER, lMem); |
| 97 | |
| 98 | /* The Main Loop over stages */ |
| 99 | |
| 100 | for (stage=0; stage<nStages; stage++) { |
| 101 | |
| 102 | range = search_rangeTbl[block][stage]; |
| 103 | |
| 104 | /* initialize search measure */ |
| 105 | |
| 106 | max_measure = (float)-10000000.0; |
| 107 | gain = (float)0.0; |
| 108 | best_index = 0; |
| 109 | |
| 110 | /* Compute cross dot product between the target |
| 111 | and the CB memory */ |
| 112 | |
| 113 | crossDot=0.0; |
| 114 | pp=buf+LPC_FILTERORDER+lMem-lTarget; |
| 115 | for (j=0; j<lTarget; j++) { |
| 116 | crossDot += target[j]*(*pp++); |
| 117 | } |
| 118 | |
| 119 | if (stage==0) { |
| 120 | |
| 121 | /* Calculate energy in the first block of |
| 122 | 'lTarget' samples. */ |
| 123 | ppe = energy; |
| 124 | ppi = buf+LPC_FILTERORDER+lMem-lTarget-1; |
| 125 | ppo = buf+LPC_FILTERORDER+lMem-1; |
| 126 | |
| 127 | *ppe=0.0; |
| 128 | pp=buf+LPC_FILTERORDER+lMem-lTarget; |
| 129 | for (j=0; j<lTarget; j++) { |
| 130 | *ppe+=(*pp)*(*pp); |
| 131 | ++pp; |
| 132 | } |
| 133 | |
| 134 | if (*ppe>0.0) { |
| 135 | invenergy[0] = (float) 1.0 / (*ppe + EPS); |
| 136 | } else { |
| 137 | invenergy[0] = (float) 0.0; |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | } |
| 144 | ppe++; |
| 145 | |
| 146 | measure=(float)-10000000.0; |
| 147 | |
| 148 | if (crossDot > 0.0) { |
| 149 | measure = crossDot*crossDot*invenergy[0]; |
| 150 | } |
| 151 | } |
| 152 | else { |
| 153 | measure = crossDot*crossDot*invenergy[0]; |
| 154 | } |
| 155 | |
| 156 | /* check if measure is better */ |
| 157 | ftmp = crossDot*invenergy[0]; |
| 158 | |
| 159 | if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) { |
| 160 | best_index = 0; |
| 161 | max_measure = measure; |
| 162 | gain = ftmp; |
| 163 | } |
| 164 | |
| 165 | /* loop over the main first codebook section, |
| 166 | full search */ |
| 167 | |
| 168 | for (icount=1; icount<range; icount++) { |
| 169 | |
| 170 | /* calculate measure */ |
| 171 | |
| 172 | crossDot=0.0; |
| 173 | pp = buf+LPC_FILTERORDER+lMem-lTarget-icount; |
| 174 | |
| 175 | for (j=0; j<lTarget; j++) { |
| 176 | crossDot += target[j]*(*pp++); |
| 177 | } |
| 178 | |
| 179 | if (stage==0) { |
| 180 | *ppe++ = energy[icount-1] + (*ppi)*(*ppi) - |
| 181 | (*ppo)*(*ppo); |
| 182 | ppo--; |
| 183 | ppi--; |
| 184 | |
| 185 | if (energy[icount]>0.0) { |
| 186 | invenergy[icount] = |
| 187 | (float)1.0/(energy[icount]+EPS); |
| 188 | } else { |
| 189 | invenergy[icount] = (float) 0.0; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | measure=(float)-10000000.0; |
| 197 | |
| 198 | if (crossDot > 0.0) { |
| 199 | measure = crossDot*crossDot*invenergy[icount]; |
| 200 | } |
| 201 | } |
| 202 | else { |
| 203 | measure = crossDot*crossDot*invenergy[icount]; |
| 204 | } |
| 205 | |
| 206 | /* check if measure is better */ |
| 207 | ftmp = crossDot*invenergy[icount]; |
| 208 | |
| 209 | if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) { |
| 210 | best_index = icount; |
| 211 | max_measure = measure; |
| 212 | gain = ftmp; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* Loop over augmented part in the first codebook |
| 217 | * section, full search. |
| 218 | * The vectors are interpolated. |
| 219 | */ |
| 220 | |
| 221 | if (lTarget==SUBL) { |
| 222 | |
| 223 | /* Search for best possible cb vector and |
| 224 | compute the CB-vectors' energy. */ |
| 225 | searchAugmentedCB(20, 39, stage, base_size-lTarget/2, |
| 226 | target, buf+LPC_FILTERORDER+lMem, |
| 227 | &max_measure, &best_index, &gain, energy, |
| 228 | invenergy); |
| 229 | } |
| 230 | |
| 231 | /* set search range for following codebook sections */ |
| 232 | |
| 233 | base_index=best_index; |
| 234 | |
| 235 | /* unrestricted search */ |
| 236 | |
| 237 | # if CB_RESRANGE == -1 |
| 238 | //if (CB_RESRANGE == -1) { |
| 239 | sInd=0; |
| 240 | eInd=range-1; |
| 241 | sIndAug=20; |
| 242 | eIndAug=39; |
| 243 | //} |
| 244 | |
| 245 | # else |
| 246 | |
| 247 | /* restricted search around best index from first |
| 248 | codebook section */ |
| 249 | |
| 250 | //else { |
| 251 | /* Initialize search indices */ |
| 252 | sIndAug=0; |
| 253 | eIndAug=0; |
| 254 | sInd=base_index-CB_RESRANGE/2; |
| 255 | eInd=sInd+CB_RESRANGE; |
| 256 | |
| 257 | if (lTarget==SUBL) { |
| 258 | |
| 259 | if (sInd<0) { |
| 260 | |
| 261 | sIndAug = 40 + sInd; |
| 262 | eIndAug = 39; |
| 263 | sInd=0; |
| 264 | |
| 265 | } else if ( base_index < (base_size-20) ) { |
| 266 | |
| 267 | if (eInd > range) { |
| 268 | sInd -= (eInd-range); |
| 269 | eInd = range; |
| 270 | } |
| 271 | } else { /* base_index >= (base_size-20) */ |
| 272 | |
| 273 | if (sInd < (base_size-20)) { |
| 274 | sIndAug = 20; |
| 275 | sInd = 0; |
| 276 | eInd = 0; |
| 277 | eIndAug = 19 + CB_RESRANGE; |
| 278 | |
| 279 | if(eIndAug > 39) { |
| 280 | eInd = eIndAug-39; |
| 281 | eIndAug = 39; |
| 282 | } |
| 283 | } else { |
| 284 | sIndAug = 20 + sInd - (base_size-20); |
| 285 | eIndAug = 39; |
| 286 | sInd = 0; |
| 287 | eInd = CB_RESRANGE - (eIndAug-sIndAug+1); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | } else { /* lTarget = 22 or 23 */ |
| 292 | |
| 293 | if (sInd < 0) { |
| 294 | eInd -= sInd; |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | sInd = 0; |
| 301 | } |
| 302 | |
| 303 | if(eInd > range) { |
| 304 | sInd -= (eInd - range); |
| 305 | eInd = range; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | //} |
| 310 | # endif /* CB_RESRANGE == -1 */ |
| 311 | |
| 312 | |
| 313 | /* search of higher codebook section */ |
| 314 | |
| 315 | /* index search range */ |
| 316 | counter = sInd; |
| 317 | sInd += base_size; |
| 318 | eInd += base_size; |
| 319 | |
| 320 | |
| 321 | if (stage==0) { |
| 322 | ppe = energy+base_size; |
| 323 | *ppe=0.0; |
| 324 | |
| 325 | pp=cbvectors+lMem-lTarget; |
| 326 | for (j=0; j<lTarget; j++) { |
| 327 | *ppe+=(*pp)*(*pp); |
| 328 | ++pp; |
| 329 | } |
| 330 | |
| 331 | ppi = cbvectors + lMem - 1 - lTarget; |
| 332 | ppo = cbvectors + lMem - 1; |
| 333 | |
| 334 | for (j=0; j<(range-1); j++) { |
| 335 | *(ppe+1) = *ppe + (*ppi)*(*ppi) - (*ppo)*(*ppo); |
| 336 | ppo--; |
| 337 | ppi--; |
| 338 | ppe++; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* loop over search range */ |
| 343 | |
| 344 | for (icount=sInd; icount<eInd; icount++) { |
| 345 | |
| 346 | /* calculate measure */ |
| 347 | |
| 348 | crossDot=0.0; |
| 349 | pp=cbvectors + lMem - (counter++) - lTarget; |
| 350 | |
| 351 | for (j=0;j<lTarget;j++) { |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | crossDot += target[j]*(*pp++); |
| 358 | } |
| 359 | |
| 360 | if (energy[icount]>0.0) { |
| 361 | invenergy[icount] =(float)1.0/(energy[icount]+EPS); |
| 362 | } else { |
| 363 | invenergy[icount] =(float)0.0; |
| 364 | } |
| 365 | |
| 366 | if (stage==0) { |
| 367 | |
| 368 | measure=(float)-10000000.0; |
| 369 | |
| 370 | if (crossDot > 0.0) { |
| 371 | measure = crossDot*crossDot* |
| 372 | invenergy[icount]; |
| 373 | } |
| 374 | } |
| 375 | else { |
| 376 | measure = crossDot*crossDot*invenergy[icount]; |
| 377 | } |
| 378 | |
| 379 | /* check if measure is better */ |
| 380 | ftmp = crossDot*invenergy[icount]; |
| 381 | |
| 382 | if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) { |
| 383 | best_index = icount; |
| 384 | max_measure = measure; |
| 385 | gain = ftmp; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* Search the augmented CB inside the limited range. */ |
| 390 | |
| 391 | if ((lTarget==SUBL)&&(sIndAug!=0)) { |
| 392 | searchAugmentedCB(sIndAug, eIndAug, stage, |
| 393 | 2*base_size-20, target, cbvectors+lMem, |
| 394 | &max_measure, &best_index, &gain, energy, |
| 395 | invenergy); |
| 396 | } |
| 397 | |
| 398 | /* record best index */ |
| 399 | |
| 400 | index[stage] = best_index; |
| 401 | |
| 402 | /* gain quantization */ |
| 403 | |
| 404 | if (stage==0){ |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | if (gain<0.0){ |
| 412 | gain = 0.0; |
| 413 | } |
| 414 | |
| 415 | if (gain>CB_MAXGAIN) { |
| 416 | gain = (float)CB_MAXGAIN; |
| 417 | } |
| 418 | gain = gainquant(gain, 1.0, 32, &gain_index[stage]); |
| 419 | } |
| 420 | else { |
| 421 | if (stage==1) { |
| 422 | gain = gainquant(gain, (float)fabs(gains[stage-1]), |
| 423 | 16, &gain_index[stage]); |
| 424 | } else { |
| 425 | gain = gainquant(gain, (float)fabs(gains[stage-1]), |
| 426 | 8, &gain_index[stage]); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* Extract the best (according to measure) |
| 431 | codebook vector */ |
| 432 | |
| 433 | if (lTarget==(STATE_LEN-iLBCenc_inst->state_short_len)) { |
| 434 | |
| 435 | if (index[stage]<base_size) { |
| 436 | pp=buf+LPC_FILTERORDER+lMem-lTarget-index[stage]; |
| 437 | } else { |
| 438 | pp=cbvectors+lMem-lTarget- |
| 439 | index[stage]+base_size; |
| 440 | } |
| 441 | } else { |
| 442 | |
| 443 | if (index[stage]<base_size) { |
| 444 | if (index[stage]<(base_size-20)) { |
| 445 | pp=buf+LPC_FILTERORDER+lMem- |
| 446 | lTarget-index[stage]; |
| 447 | } else { |
| 448 | createAugmentedVec(index[stage]-base_size+40, |
| 449 | buf+LPC_FILTERORDER+lMem,aug_vec); |
| 450 | pp=aug_vec; |
| 451 | } |
| 452 | } else { |
| 453 | int filterno, position; |
| 454 | |
| 455 | filterno=index[stage]/base_size; |
| 456 | position=index[stage]-filterno*base_size; |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | if (position<(base_size-20)) { |
| 465 | pp=cbvectors+filterno*lMem-lTarget- |
| 466 | index[stage]+filterno*base_size; |
| 467 | } else { |
| 468 | createAugmentedVec( |
| 469 | index[stage]-(filterno+1)*base_size+40, |
| 470 | cbvectors+filterno*lMem,aug_vec); |
| 471 | pp=aug_vec; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* Subtract the best codebook vector, according |
| 477 | to measure, from the target vector */ |
| 478 | |
| 479 | for (j=0;j<lTarget;j++) { |
| 480 | cvec[j] += gain*(*pp); |
| 481 | target[j] -= gain*(*pp++); |
| 482 | } |
| 483 | |
| 484 | /* record quantized gain */ |
| 485 | |
| 486 | gains[stage]=gain; |
| 487 | |
| 488 | }/* end of Main Loop. for (stage=0;... */ |
| 489 | |
| 490 | /* Gain adjustment for energy matching */ |
| 491 | cene=0.0; |
| 492 | for (i=0; i<lTarget; i++) { |
| 493 | cene+=cvec[i]*cvec[i]; |
| 494 | } |
| 495 | j=gain_index[0]; |
| 496 | |
| 497 | for (i=gain_index[0]; i<32; i++) { |
| 498 | ftmp=cene*gain_sq5Tbl[i]*gain_sq5Tbl[i]; |
| 499 | |
| 500 | if ((ftmp<(tene*gains[0]*gains[0])) && |
| 501 | (gain_sq5Tbl[j]<(2.0*gains[0]))) { |
| 502 | j=i; |
| 503 | } |
| 504 | } |
| 505 | gain_index[0]=j; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |