1 /* 2 ** Copyright 2003-2010, VisualOn, Inc. 3 ** 4 ** Licensed under the Apache License, Version 2.0 (the "License"); 5 ** you may not use this file except in compliance with the License. 6 ** You may obtain a copy of the License at 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 10 ** Unless required by applicable law or agreed to in writing, software 11 ** distributed under the License is distributed on an "AS IS" BASIS, 12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 ** See the License for the specific language governing permissions and 14 ** limitations under the License. 15 */ 16 17 /*********************************************************************** 18 * File: apisf_2s.c * 19 * * 20 * Description: Coding/Decodeing of ISF parameters with predication 21 * The ISF vector is quantized using two-stage VQ with split-by-2 * 22 * in 1st stage and split-by-5(or 3) in the second stage * 23 * * 24 ************************************************************************/ 25 26 #include "typedef.h" 27 #include "basic_op.h" 28 #include "cnst.h" 29 #include "acelp.h" 30 #include "qpisf_2s.tab" /* Codebooks of isfs */ 31 32 #define MU 10923 /* Prediction factor (1.0/3.0) in Q15 */ 33 #define N_SURV_MAX 4 /* 4 survivors max */ 34 #define ALPHA 29491 /* 0. 9 in Q15 */ 35 #define ONE_ALPHA (32768-ALPHA) /* (1.0 - ALPHA) in Q15 */ 36 37 /* private functions */ 38 static void VQ_stage1( 39 Word16 * x, /* input : ISF residual vector */ 40 Word16 * dico, /* input : quantization codebook */ 41 Word16 dim, /* input : dimention of vector */ 42 Word16 dico_size, /* input : size of quantization codebook */ 43 Word16 * index, /* output: indices of survivors */ 44 Word16 surv /* input : number of survivor */ 45 ); 46 47 /************************************************************************** 48 * Function: Qpisf_2s_46B() * 49 * * 50 * Description: Quantization of isf parameters with prediction. (46 bits) * 51 * * 52 * The isf vector is quantized using two-stage VQ with split-by-2 in * 53 * 1st stage and split-by-5 in the second stage. * 54 ***************************************************************************/ 55 56 void Qpisf_2s_46b( 57 Word16 * isf1, /* (i) Q15 : ISF in the frequency domain (0..0.5) */ 58 Word16 * isf_q, /* (o) Q15 : quantized ISF (0..0.5) */ 59 Word16 * past_isfq, /* (io)Q15 : past ISF quantizer */ 60 Word16 * indice, /* (o) : quantization indices */ 61 Word16 nb_surv /* (i) : number of survivor (1, 2, 3 or 4) */ 62 ) 63 { 64 Word16 tmp_ind[5]; 65 Word16 surv1[N_SURV_MAX]; /* indices of survivors from 1st stage */ 66 Word32 i, k, temp, min_err, distance; 67 Word16 isf[ORDER]; 68 Word16 isf_stage2[ORDER]; 69 70 for (i = 0; i < ORDER; i++) 71 { 72 isf[i] = vo_sub(isf1[i], mean_isf[i]); 73 isf[i] = vo_sub(isf[i], vo_mult(MU, past_isfq[i])); 74 } 75 76 VQ_stage1(&isf[0], dico1_isf, 9, SIZE_BK1, surv1, nb_surv); 77 78 distance = MAX_32; 79 80 for (k = 0; k < nb_surv; k++) 81 { 82 for (i = 0; i < 9; i++) 83 { 84 isf_stage2[i] = vo_sub(isf[i], dico1_isf[i + surv1[k] * 9]); 85 } 86 tmp_ind[0] = Sub_VQ(&isf_stage2[0], dico21_isf, 3, SIZE_BK21, &min_err); 87 temp = min_err; 88 tmp_ind[1] = Sub_VQ(&isf_stage2[3], dico22_isf, 3, SIZE_BK22, &min_err); 89 temp = vo_L_add(temp, min_err); 90 tmp_ind[2] = Sub_VQ(&isf_stage2[6], dico23_isf, 3, SIZE_BK23, &min_err); 91 temp = vo_L_add(temp, min_err); 92 93 if(temp < distance) 94 { 95 distance = temp; 96 indice[0] = surv1[k]; 97 for (i = 0; i < 3; i++) 98 { 99 indice[i + 2] = tmp_ind[i]; 100 } 101 } 102 } 103 104 105 VQ_stage1(&isf[9], dico2_isf, 7, SIZE_BK2, surv1, nb_surv); 106 107 distance = MAX_32; 108 109 for (k = 0; k < nb_surv; k++) 110 { 111 for (i = 0; i < 7; i++) 112 { 113 isf_stage2[i] = vo_sub(isf[9 + i], dico2_isf[i + surv1[k] * 7]); 114 } 115 116 tmp_ind[0] = Sub_VQ(&isf_stage2[0], dico24_isf, 3, SIZE_BK24, &min_err); 117 temp = min_err; 118 tmp_ind[1] = Sub_VQ(&isf_stage2[3], dico25_isf, 4, SIZE_BK25, &min_err); 119 temp = vo_L_add(temp, min_err); 120 121 if(temp < distance) 122 { 123 distance = temp; 124 indice[1] = surv1[k]; 125 for (i = 0; i < 2; i++) 126 { 127 indice[i + 5] = tmp_ind[i]; 128 } 129 } 130 } 131 132 Dpisf_2s_46b(indice, isf_q, past_isfq, isf_q, isf_q, 0, 0); 133 134 return; 135 } 136 137 /***************************************************************************** 138 * Function: Qpisf_2s_36B() * 139 * * 140 * Description: Quantization of isf parameters with prediction. (36 bits) * 141 * * 142 * The isf vector is quantized using two-stage VQ with split-by-2 in * 143 * 1st stage and split-by-3 in the second stage. * 144 ******************************************************************************/ 145 146 void Qpisf_2s_36b( 147 Word16 * isf1, /* (i) Q15 : ISF in the frequency domain (0..0.5) */ 148 Word16 * isf_q, /* (o) Q15 : quantized ISF (0..0.5) */ 149 Word16 * past_isfq, /* (io)Q15 : past ISF quantizer */ 150 Word16 * indice, /* (o) : quantization indices */ 151 Word16 nb_surv /* (i) : number of survivor (1, 2, 3 or 4) */ 152 ) 153 { 154 Word16 i, k, tmp_ind[5]; 155 Word16 surv1[N_SURV_MAX]; /* indices of survivors from 1st stage */ 156 Word32 temp, min_err, distance; 157 Word16 isf[ORDER]; 158 Word16 isf_stage2[ORDER]; 159 160 for (i = 0; i < ORDER; i++) 161 { 162 isf[i] = vo_sub(isf1[i], mean_isf[i]); 163 isf[i] = vo_sub(isf[i], vo_mult(MU, past_isfq[i])); 164 } 165 166 VQ_stage1(&isf[0], dico1_isf, 9, SIZE_BK1, surv1, nb_surv); 167 168 distance = MAX_32; 169 170 for (k = 0; k < nb_surv; k++) 171 { 172 for (i = 0; i < 9; i++) 173 { 174 isf_stage2[i] = vo_sub(isf[i], dico1_isf[i + surv1[k] * 9]); 175 } 176 177 tmp_ind[0] = Sub_VQ(&isf_stage2[0], dico21_isf_36b, 5, SIZE_BK21_36b, &min_err); 178 temp = min_err; 179 tmp_ind[1] = Sub_VQ(&isf_stage2[5], dico22_isf_36b, 4, SIZE_BK22_36b, &min_err); 180 temp = vo_L_add(temp, min_err); 181 182 if(temp < distance) 183 { 184 distance = temp; 185 indice[0] = surv1[k]; 186 for (i = 0; i < 2; i++) 187 { 188 indice[i + 2] = tmp_ind[i]; 189 } 190 } 191 } 192 193 VQ_stage1(&isf[9], dico2_isf, 7, SIZE_BK2, surv1, nb_surv); 194 distance = MAX_32; 195 196 for (k = 0; k < nb_surv; k++) 197 { 198 for (i = 0; i < 7; i++) 199 { 200 isf_stage2[i] = vo_sub(isf[9 + i], dico2_isf[i + surv1[k] * 7]); 201 } 202 203 tmp_ind[0] = Sub_VQ(&isf_stage2[0], dico23_isf_36b, 7, SIZE_BK23_36b, &min_err); 204 temp = min_err; 205 206 if(temp < distance) 207 { 208 distance = temp; 209 indice[1] = surv1[k]; 210 indice[4] = tmp_ind[0]; 211 } 212 } 213 214 Dpisf_2s_36b(indice, isf_q, past_isfq, isf_q, isf_q, 0, 0); 215 216 return; 217 } 218 219 /********************************************************************* 220 * Function: Dpisf_2s_46b() * 221 * * 222 * Description: Decoding of ISF parameters * 223 **********************************************************************/ 224 225 void Dpisf_2s_46b( 226 Word16 * indice, /* input: quantization indices */ 227 Word16 * isf_q, /* output: quantized ISF in frequency domain (0..0.5) */ 228 Word16 * past_isfq, /* i/0 : past ISF quantizer */ 229 Word16 * isfold, /* input : past quantized ISF */ 230 Word16 * isf_buf, /* input : isf buffer */ 231 Word16 bfi, /* input : Bad frame indicator */ 232 Word16 enc_dec 233 ) 234 { 235 Word16 ref_isf[M], tmp; 236 Word32 i, j, L_tmp; 237 238 if (bfi == 0) /* Good frame */ 239 { 240 for (i = 0; i < 9; i++) 241 { 242 isf_q[i] = dico1_isf[indice[0] * 9 + i]; 243 } 244 for (i = 0; i < 7; i++) 245 { 246 isf_q[i + 9] = dico2_isf[indice[1] * 7 + i]; 247 } 248 249 for (i = 0; i < 3; i++) 250 { 251 isf_q[i] = add1(isf_q[i], dico21_isf[indice[2] * 3 + i]); 252 isf_q[i + 3] = add1(isf_q[i + 3], dico22_isf[indice[3] * 3 + i]); 253 isf_q[i + 6] = add1(isf_q[i + 6], dico23_isf[indice[4] * 3 + i]); 254 isf_q[i + 9] = add1(isf_q[i + 9], dico24_isf[indice[5] * 3 + i]); 255 } 256 257 for (i = 0; i < 4; i++) 258 { 259 isf_q[i + 12] = add1(isf_q[i + 12], dico25_isf[indice[6] * 4 + i]); 260 } 261 262 for (i = 0; i < ORDER; i++) 263 { 264 tmp = isf_q[i]; 265 isf_q[i] = add1(tmp, mean_isf[i]); 266 isf_q[i] = add1(isf_q[i], vo_mult(MU, past_isfq[i])); 267 past_isfq[i] = tmp; 268 } 269 270 if (enc_dec) 271 { 272 for (i = 0; i < M; i++) 273 { 274 for (j = (L_MEANBUF - 1); j > 0; j--) 275 { 276 isf_buf[j * M + i] = isf_buf[(j - 1) * M + i]; 277 } 278 isf_buf[i] = isf_q[i]; 279 } 280 } 281 } else 282 { /* bad frame */ 283 for (i = 0; i < M; i++) 284 { 285 L_tmp = mean_isf[i] << 14; 286 for (j = 0; j < L_MEANBUF; j++) 287 { 288 L_tmp += (isf_buf[j * M + i] << 14); 289 } 290 ref_isf[i] = vo_round(L_tmp); 291 } 292 293 /* use the past ISFs slightly shifted towards their mean */ 294 for (i = 0; i < ORDER; i++) 295 { 296 isf_q[i] = add1(vo_mult(ALPHA, isfold[i]), vo_mult(ONE_ALPHA, ref_isf[i])); 297 } 298 299 /* estimate past quantized residual to be used in next frame */ 300 for (i = 0; i < ORDER; i++) 301 { 302 tmp = add1(ref_isf[i], vo_mult(past_isfq[i], MU)); /* predicted ISF */ 303 past_isfq[i] = vo_sub(isf_q[i], tmp); 304 past_isfq[i] = (past_isfq[i] >> 1); /* past_isfq[i] *= 0.5 */ 305 } 306 } 307 308 Reorder_isf(isf_q, ISF_GAP, ORDER); 309 return; 310 } 311 312 /********************************************************************* 313 * Function: Disf_2s_36b() * 314 * * 315 * Description: Decoding of ISF parameters * 316 *********************************************************************/ 317 318 void Dpisf_2s_36b( 319 Word16 * indice, /* input: quantization indices */ 320 Word16 * isf_q, /* output: quantized ISF in frequency domain (0..0.5) */ 321 Word16 * past_isfq, /* i/0 : past ISF quantizer */ 322 Word16 * isfold, /* input : past quantized ISF */ 323 Word16 * isf_buf, /* input : isf buffer */ 324 Word16 bfi, /* input : Bad frame indicator */ 325 Word16 enc_dec 326 ) 327 { 328 Word16 ref_isf[M], tmp; 329 Word32 i, j, L_tmp; 330 331 if (bfi == 0) /* Good frame */ 332 { 333 for (i = 0; i < 9; i++) 334 { 335 isf_q[i] = dico1_isf[indice[0] * 9 + i]; 336 } 337 for (i = 0; i < 7; i++) 338 { 339 isf_q[i + 9] = dico2_isf[indice[1] * 7 + i]; 340 } 341 342 for (i = 0; i < 5; i++) 343 { 344 isf_q[i] = add1(isf_q[i], dico21_isf_36b[indice[2] * 5 + i]); 345 } 346 for (i = 0; i < 4; i++) 347 { 348 isf_q[i + 5] = add1(isf_q[i + 5], dico22_isf_36b[indice[3] * 4 + i]); 349 } 350 for (i = 0; i < 7; i++) 351 { 352 isf_q[i + 9] = add1(isf_q[i + 9], dico23_isf_36b[indice[4] * 7 + i]); 353 } 354 355 for (i = 0; i < ORDER; i++) 356 { 357 tmp = isf_q[i]; 358 isf_q[i] = add1(tmp, mean_isf[i]); 359 isf_q[i] = add1(isf_q[i], vo_mult(MU, past_isfq[i])); 360 past_isfq[i] = tmp; 361 } 362 363 364 if (enc_dec) 365 { 366 for (i = 0; i < M; i++) 367 { 368 for (j = (L_MEANBUF - 1); j > 0; j--) 369 { 370 isf_buf[j * M + i] = isf_buf[(j - 1) * M + i]; 371 } 372 isf_buf[i] = isf_q[i]; 373 } 374 } 375 } else 376 { /* bad frame */ 377 for (i = 0; i < M; i++) 378 { 379 L_tmp = (mean_isf[i] << 14); 380 for (j = 0; j < L_MEANBUF; j++) 381 { 382 L_tmp += (isf_buf[j * M + i] << 14); 383 } 384 ref_isf[i] = vo_round(L_tmp); 385 } 386 387 /* use the past ISFs slightly shifted towards their mean */ 388 for (i = 0; i < ORDER; i++) 389 { 390 isf_q[i] = add1(vo_mult(ALPHA, isfold[i]), vo_mult(ONE_ALPHA, ref_isf[i])); 391 } 392 393 /* estimate past quantized residual to be used in next frame */ 394 for (i = 0; i < ORDER; i++) 395 { 396 tmp = add1(ref_isf[i], vo_mult(past_isfq[i], MU)); /* predicted ISF */ 397 past_isfq[i] = vo_sub(isf_q[i], tmp); 398 past_isfq[i] = past_isfq[i] >> 1; /* past_isfq[i] *= 0.5 */ 399 } 400 } 401 402 Reorder_isf(isf_q, ISF_GAP, ORDER); 403 404 return; 405 } 406 407 408 /*************************************************************************** 409 * Function: Reorder_isf() * 410 * * 411 * Description: To make sure that the isfs are properly order and to * 412 * keep a certain minimum distance between consecutive isfs. * 413 *--------------------------------------------------------------------------* 414 * Argument description in/out * 415 * * 416 * isf[] vector of isfs i/o * 417 * min_dist minimum required distance i * 418 * n LPC order i * 419 ****************************************************************************/ 420 421 void Reorder_isf( 422 Word16 * isf, /* (i/o) Q15: ISF in the frequency domain (0..0.5) */ 423 Word16 min_dist, /* (i) Q15 : minimum distance to keep */ 424 Word16 n /* (i) : number of ISF */ 425 ) 426 { 427 Word32 i; 428 Word16 isf_min; 429 430 isf_min = min_dist; 431 for (i = 0; i < n - 1; i++) 432 { 433 if(isf[i] < isf_min) 434 { 435 isf[i] = isf_min; 436 } 437 isf_min = (isf[i] + min_dist); 438 } 439 return; 440 } 441 442 443 Word16 Sub_VQ( /* output: return quantization index */ 444 Word16 * x, /* input : ISF residual vector */ 445 Word16 * dico, /* input : quantization codebook */ 446 Word16 dim, /* input : dimention of vector */ 447 Word16 dico_size, /* input : size of quantization codebook */ 448 Word32 * distance /* output: error of quantization */ 449 ) 450 { 451 Word16 temp, *p_dico; 452 Word32 i, j, index; 453 Word32 dist_min, dist; 454 455 dist_min = MAX_32; 456 p_dico = dico; 457 458 index = 0; 459 for (i = 0; i < dico_size; i++) 460 { 461 dist = 0; 462 463 for (j = 0; j < dim; j++) 464 { 465 temp = x[j] - (*p_dico++); 466 dist += (temp * temp)<<1; 467 } 468 469 if(dist < dist_min) 470 { 471 dist_min = dist; 472 index = i; 473 } 474 } 475 476 *distance = dist_min; 477 478 /* Reading the selected vector */ 479 p_dico = &dico[index * dim]; 480 for (j = 0; j < dim; j++) 481 { 482 x[j] = *p_dico++; 483 } 484 485 return index; 486 } 487 488 489 static void VQ_stage1( 490 Word16 * x, /* input : ISF residual vector */ 491 Word16 * dico, /* input : quantization codebook */ 492 Word16 dim, /* input : dimention of vector */ 493 Word16 dico_size, /* input : size of quantization codebook */ 494 Word16 * index, /* output: indices of survivors */ 495 Word16 surv /* input : number of survivor */ 496 ) 497 { 498 Word16 temp, *p_dico; 499 Word32 i, j, k, l; 500 Word32 dist_min[N_SURV_MAX], dist; 501 502 dist_min[0] = MAX_32; 503 dist_min[1] = MAX_32; 504 dist_min[2] = MAX_32; 505 dist_min[3] = MAX_32; 506 index[0] = 0; 507 index[1] = 1; 508 index[2] = 2; 509 index[3] = 3; 510 511 p_dico = dico; 512 513 for (i = 0; i < dico_size; i++) 514 { 515 dist = 0; 516 for (j = 0; j < dim; j++) 517 { 518 temp = x[j] - (*p_dico++); 519 dist += (temp * temp)<<1; 520 } 521 522 for (k = 0; k < surv; k++) 523 { 524 if(dist < dist_min[k]) 525 { 526 for (l = surv - 1; l > k; l--) 527 { 528 dist_min[l] = dist_min[l - 1]; 529 index[l] = index[l - 1]; 530 } 531 dist_min[k] = dist; 532 index[k] = i; 533 break; 534 } 535 } 536 } 537 return; 538 } 539 540 541 542 543