1 /****************************************************************************** 2 * 3 * Copyright 2021 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #include "ltpf.h" 20 #include "tables.h" 21 22 23 /* ---------------------------------------------------------------------------- 24 * Resampling 25 * -------------------------------------------------------------------------- */ 26 27 /** 28 * Resample to 12.8 KHz (cf. 3.3.9.3-4) Template 29 * sr Samplerate source of the frame 30 * hp50 State of the High-Pass 50 Hz filter 31 * x [-d..-1] Previous, [0..ns-1] Current samples 32 * y, n [0..n-1] Output `n` processed samples 33 * 34 * The number of previous samples `d` accessed on `x` is : 35 * d: { 10, 20, 30, 40, 60 } - 1 for samplerates from 8KHz to 48KHz 36 */ 37 static inline void resample_12k8_template(const enum lc3_srate sr, 38 struct lc3_ltpf_hp50_state *hp50, const float *x, float *y, int n) 39 { 40 /* --- Parameters --- 41 * p: Resampling factor, from 4 to 24 42 * w: Half width of polyphase filter 43 * 44 * bn, an: High-Pass Biquad coefficients, 45 * with `bn` support of rescaling resampling factor. 46 * Note that it's an High-Pass filter, so we have `b0 = b2`, 47 * in the following steps we use `b0` as `b2`. */ 48 49 const int p = 192 / LC3_SRATE_KHZ(sr); 50 const int w = 5 * LC3_SRATE_KHZ(sr) / 8; 51 52 const int b_scale = p >> (sr == LC3_SRATE_8K); 53 const float a1 = -1.965293373, b1 = -1.965589417 * b_scale; 54 const float a2 = 0.965885461, b2 = 0.982794708 * b_scale; 55 56 /* --- Resampling --- 57 * The value `15*8 * n` is divisible by all resampling factors `p`, 58 * integer and fractionnal position can be determined at compilation 59 * time while unrolling the loops by 8 samples. 60 * The biquad filter implementation chosen in the `Direct Form 2`. */ 61 62 const float *h = lc3_ltpf_h12k8 + 119; 63 x -= w; 64 65 for (int i = 0; i < n; i += 8, x += 120/p) 66 for (int j = 0; j < 15*8; j += 15) { 67 float un, yn; 68 int e, f, k; 69 70 e = j / p, f = j % p; 71 for (un = 0, k = 1-w; k <= w; k++) 72 un += x[e+k] * h[k*p - f]; 73 74 yn = b2 * un + hp50->s1; 75 hp50->s1 = b1 * un - a1 * yn + hp50->s2; 76 hp50->s2 = b2 * un - a2 * yn; 77 *(y++) = yn; 78 } 79 } 80 81 /** 82 * LTPF Resample to 12.8 KHz implementations for each samplerates 83 */ 84 85 static void resample_8k_12k8( 86 struct lc3_ltpf_hp50_state *hp50, const float *x, float *y, int n) 87 { 88 resample_12k8_template(LC3_SRATE_8K, hp50, x, y, n); 89 } 90 91 static void resample_16k_12k8( 92 struct lc3_ltpf_hp50_state *hp50, const float *x, float *y, int n) 93 { 94 resample_12k8_template(LC3_SRATE_16K, hp50, x, y, n); 95 } 96 97 static void resample_24k_12k8( 98 struct lc3_ltpf_hp50_state *hp50, const float *x, float *y, int n) 99 { 100 resample_12k8_template(LC3_SRATE_24K, hp50, x, y, n); 101 } 102 103 static void resample_32k_12k8( 104 struct lc3_ltpf_hp50_state *hp50, const float *x, float *y, int n) 105 { 106 resample_12k8_template(LC3_SRATE_32K, hp50, x, y, n); 107 } 108 109 static void resample_48k_12k8( 110 struct lc3_ltpf_hp50_state *hp50, const float *x, float *y, int n) 111 { 112 resample_12k8_template(LC3_SRATE_48K, hp50, x, y, n); 113 } 114 115 static void (* const resample_12k8[]) 116 (struct lc3_ltpf_hp50_state *, const float *, float *, int ) = 117 { 118 [LC3_SRATE_8K ] = resample_8k_12k8, 119 [LC3_SRATE_16K] = resample_16k_12k8, 120 [LC3_SRATE_24K] = resample_24k_12k8, 121 [LC3_SRATE_32K] = resample_32k_12k8, 122 [LC3_SRATE_48K] = resample_48k_12k8, 123 }; 124 125 /** 126 * Resample to 6.4 KHz (cf. 3.3.9.3-4) 127 * x [-3..-1] Previous, [0..n-1] Current samples 128 * y, n [0..n-1] Output `n` processed samples 129 */ 130 static void resample_6k4(const float *x, float *y, int n) 131 { 132 static const float h[] = { 0.2819382921, 0.2353512128, 0.1236796411 }; 133 float xn2 = x[-3], xn1 = x[-2], x0 = x[-1], x1, x2; 134 135 for (const float *ye = y + n; y < ye; xn2 = x0, xn1 = x1, x0 = x2) { 136 x1 = *(x++); x2 = *(x++); 137 138 *(y++) = x0 * h[0] + (xn1 + x1) * h[1] + (xn2 + x2) * h[2]; 139 } 140 } 141 142 143 /* ---------------------------------------------------------------------------- 144 * Analysis 145 * -------------------------------------------------------------------------- */ 146 147 /** 148 * Return dot product of 2 vectors 149 * a, b, n The 2 vectors of size `n` 150 * return sum( a[i] * b[i] ), i = [0..n-1] 151 */ 152 static inline float dot(const float *a, const float *b, int n) 153 { 154 float v = 0; 155 156 while (n--) 157 v += *(a++) * *(b++); 158 159 return v; 160 } 161 162 /** 163 * Return vector of correlations 164 * a, b, n The 2 vector of size `n` to correlate 165 * y, nc Output the correlation vector of size `nc` 166 * 167 * The size `n` of input vectors must be multiple of 16 168 */ 169 static void correlate( 170 const float *a, const float *b, int n, float *y, int nc) 171 { 172 for (const float *ye = y + nc; y < ye; ) 173 *(y++) = dot(a, b--, n); 174 } 175 176 /** 177 * Search the maximum value and returns its argument 178 * x, n The input vector of size `n` 179 * x_max Return the maximum value 180 * return Return the argument of the maximum 181 */ 182 static int argmax(const float *x, int n, float *x_max) 183 { 184 int arg = 0; 185 186 *x_max = x[arg = 0]; 187 for (int i = 1; i < n; i++) 188 if (*x_max < x[i]) 189 *x_max = x[arg = i]; 190 191 return arg; 192 } 193 194 /** 195 * Search the maximum weithed value and returns its argument 196 * x, n The input vector of size `n` 197 * w_incr Increment of the weight 198 * x_max, xw_max Return the maximum not weighted value 199 * return Return the argument of the weigthed maximum 200 */ 201 static int argmax_weighted( 202 const float *x, int n, float w_incr, float *x_max) 203 { 204 int arg; 205 206 float xw_max = (*x_max = x[arg = 0]); 207 float w = 1 + w_incr; 208 209 for (int i = 1; i < n; i++, w += w_incr) 210 if (xw_max < x[i] * w) 211 xw_max = (*x_max = x[arg = i]) * w; 212 213 return arg; 214 } 215 216 /** 217 * Interpolate from pitch detected value (3.3.9.8) 218 * x, n [-2..-1] Previous, [0..n] Current input 219 * d The phase of interpolation (0 to 3) 220 * return The interpolated vector 221 * 222 * The size `n` of vectors must be multiple of 4 223 */ 224 static void interpolate(const float *x, int n, int d, float *y) 225 { 226 static const float h4[][8] = { 227 { 2.09880463e-01, 5.83527575e-01, 2.09880463e-01 }, 228 { 1.06999186e-01, 5.50075002e-01, 3.35690625e-01, 6.69885837e-03 }, 229 { 3.96711478e-02, 4.59220930e-01, 4.59220930e-01, 3.96711478e-02 }, 230 { 6.69885837e-03, 3.35690625e-01, 5.50075002e-01, 1.06999186e-01 }, 231 }; 232 233 const float *h = h4[d]; 234 float x3 = x[-2], x2 = x[-1], x1, x0; 235 236 x1 = (*x++); 237 for (const float *ye = y + n; y < ye; ) { 238 *(y++) = (x0 = *(x++)) * h[0] + x1 * h[1] + x2 * h[2] + x3 * h[3]; 239 *(y++) = (x3 = *(x++)) * h[0] + x0 * h[1] + x1 * h[2] + x2 * h[3]; 240 *(y++) = (x2 = *(x++)) * h[0] + x3 * h[1] + x0 * h[2] + x1 * h[3]; 241 *(y++) = (x1 = *(x++)) * h[0] + x2 * h[1] + x3 * h[2] + x0 * h[3]; 242 } 243 } 244 245 /** 246 * Interpolate autocorrelation (3.3.9.7) 247 * x [-4..-1] Previous, [0..4] Current input 248 * d The phase of interpolation (-3 to 3) 249 * return The interpolated value 250 */ 251 static float interpolate_4(const float *x, int d) 252 { 253 static const float h4[][8] = { 254 { 1.53572770e-02, -4.72963246e-02, 8.35788573e-02, 8.98638285e-01, 255 8.35788573e-02, -4.72963246e-02, 1.53572770e-02, }, 256 { 2.74547165e-03, 4.59833449e-03, -7.54404636e-02, 8.17488686e-01, 257 3.30182571e-01, -1.05835916e-01, 2.86823405e-02, -2.87456116e-03 }, 258 { -3.00125103e-03, 2.95038503e-02, -1.30305021e-01, 6.03297008e-01, 259 6.03297008e-01, -1.30305021e-01, 2.95038503e-02, -3.00125103e-03 }, 260 { -2.87456116e-03, 2.86823405e-02, -1.05835916e-01, 3.30182571e-01, 261 8.17488686e-01, -7.54404636e-02, 4.59833449e-03, 2.74547165e-03 }, 262 }; 263 264 const float *h = h4[(4+d) % 4]; 265 266 float y = d < 0 ? x[-4] * *(h++) : 267 d > 0 ? x[ 4] * *(h+7) : 0; 268 269 y += x[-3] * h[0] + x[-2] * h[1] + x[-1] * h[2] + x[0] * h[3] + 270 x[ 1] * h[4] + x[ 2] * h[5] + x[ 3] * h[6]; 271 272 return y; 273 } 274 275 /** 276 * Pitch detection algorithm (3.3.9.5-6) 277 * ltpf Context of analysis 278 * x, n [-114..-17] Previous, [0..n-1] Current 6.4KHz samples 279 * tc Return the pitch-lag estimation 280 * return True when pitch present 281 */ 282 static bool detect_pitch( 283 struct lc3_ltpf_analysis *ltpf, const float *x, int n, int *tc) 284 { 285 float rm1, rm2; 286 float r[98]; 287 288 const int r0 = 17, nr = 98; 289 int k0 = LC3_MAX( 0, ltpf->tc-4); 290 int nk = LC3_MIN(nr-1, ltpf->tc+4) - k0 + 1; 291 292 correlate(x, x - r0, n, r, nr); 293 294 int t1 = argmax_weighted(r, nr, -.5/(nr-1), &rm1); 295 int t2 = k0 + argmax(r + k0, nk, &rm2); 296 297 const float *x1 = x - (r0 + t1); 298 const float *x2 = x - (r0 + t2); 299 300 float nc1 = rm1 <= 0 ? 0 : 301 rm1 / sqrtf(dot(x, x, n) * dot(x1, x1, n)); 302 303 float nc2 = rm2 <= 0 ? 0 : 304 rm2 / sqrtf(dot(x, x, n) * dot(x2, x2, n)); 305 306 int t1sel = nc2 <= 0.85 * nc1; 307 ltpf->tc = (t1sel ? t1 : t2); 308 309 *tc = r0 + ltpf->tc; 310 return (t1sel ? nc1 : nc2) > 0.6; 311 } 312 313 /** 314 * Pitch-lag parameter (3.3.9.7) 315 * x, n [-232..-28] Previous, [0..n-1] Current 12.8KHz samples 316 * tc Pitch-lag estimation 317 * pitch The pitch value, in fixed .4 318 * return The bitstream pitch index value 319 */ 320 static int refine_pitch(const float *x, int n, int tc, int *pitch) 321 { 322 float r[17], rm; 323 int e, f; 324 325 int r0 = LC3_MAX( 32, 2*tc - 4); 326 int nr = LC3_MIN(228, 2*tc + 4) - r0 + 1; 327 328 correlate(x, x - (r0 - 4), n, r, nr + 8); 329 330 e = r0 + argmax(r + 4, nr, &rm); 331 const float *re = r + (e - (r0 - 4)); 332 333 float dm = interpolate_4(re, f = 0); 334 for (int i = 1; i <= 3; i++) { 335 float d; 336 337 if (e >= 127 && ((i & 1) | (e >= 157))) 338 continue; 339 340 if ((d = interpolate_4(re, i)) > dm) 341 dm = d, f = i; 342 343 if (e > 32 && (d = interpolate_4(re, -i)) > dm) 344 dm = d, f = -i; 345 } 346 347 e -= (f < 0); 348 f += 4*(f < 0); 349 350 *pitch = 4*e + f; 351 return e < 127 ? 4*e + f - 128 : 352 e < 157 ? 2*e + (f >> 1) + 126 : e + 283; 353 } 354 355 /** 356 * LTPF Analysis 357 */ 358 bool lc3_ltpf_analyse(enum lc3_dt dt, enum lc3_srate sr, 359 struct lc3_ltpf_analysis *ltpf, const float *x, struct lc3_ltpf_data *data) 360 { 361 /* --- Resampling to 12.8 KHz --- */ 362 363 int z_12k8 = sizeof(ltpf->x_12k8) / sizeof(float); 364 int n_12k8 = dt == LC3_DT_7M5 ? 96 : 128; 365 366 memmove(ltpf->x_12k8, ltpf->x_12k8 + n_12k8, 367 (z_12k8 - n_12k8) * sizeof(float)); 368 369 float *x_12k8 = ltpf->x_12k8 + (z_12k8 - n_12k8); 370 resample_12k8[sr](<pf->hp50, x, x_12k8, n_12k8); 371 372 x_12k8 -= (dt == LC3_DT_7M5 ? 44 : 24); 373 374 /* --- Resampling to 6.4 KHz --- */ 375 376 int z_6k4 = sizeof(ltpf->x_6k4) / sizeof(float); 377 int n_6k4 = n_12k8 >> 1; 378 379 memmove(ltpf->x_6k4, ltpf->x_6k4 + n_6k4, 380 (z_6k4 - n_6k4) * sizeof(float)); 381 382 float *x_6k4 = ltpf->x_6k4 + (z_6k4 - n_6k4); 383 resample_6k4(x_12k8, x_6k4, n_6k4); 384 385 /* --- Pitch detection --- */ 386 387 int tc, pitch = 0; 388 float nc = 0; 389 390 bool pitch_present = detect_pitch(ltpf, x_6k4, n_6k4, &tc); 391 392 if (pitch_present) { 393 float u[n_12k8], v[n_12k8]; 394 395 data->pitch_index = refine_pitch(x_12k8, n_12k8, tc, &pitch); 396 397 interpolate(x_12k8, n_12k8, 0, u); 398 interpolate(x_12k8 - (pitch >> 2), n_12k8, pitch & 3, v); 399 400 nc = dot(u, v, n_12k8) / sqrtf(dot(u, u, n_12k8) * dot(v, v, n_12k8)); 401 } 402 403 /* --- Activation --- */ 404 405 if (ltpf->active) { 406 int pitch_diff = 407 LC3_MAX(pitch, ltpf->pitch) - LC3_MIN(pitch, ltpf->pitch); 408 float nc_diff = nc - ltpf->nc[0]; 409 410 data->active = pitch_present && 411 ((nc > 0.9) || (nc > 0.84 && pitch_diff < 8 && nc_diff > -0.1)); 412 413 } else { 414 data->active = pitch_present && 415 ( (dt == LC3_DT_10M || ltpf->nc[1] > 0.94) && 416 (ltpf->nc[0] > 0.94 && nc > 0.94) ); 417 } 418 419 ltpf->active = data->active; 420 ltpf->pitch = pitch; 421 ltpf->nc[1] = ltpf->nc[0]; 422 ltpf->nc[0] = nc; 423 424 return pitch_present; 425 } 426 427 428 /* ---------------------------------------------------------------------------- 429 * Synthesis 430 * -------------------------------------------------------------------------- */ 431 432 /** 433 * Synthesis filter template 434 * ym [-w/2..0] Previous, [0..w-1] Current pitch samples 435 * xm w-1 previous input samples 436 * x, n Current samples as input, filtered as output 437 * c, w Coefficients by pair (num, den), and count of pairs 438 * fade Fading mode of filter -1: Out 1: In 0: None 439 */ 440 static inline void synthesize_template(const float *ym, const float *xm, 441 float *x, int n, const float (*c)[2], const int w, int fade) 442 { 443 float g = (float)(fade <= 0); 444 float g_incr = (float)((fade > 0) - (fade < 0)) / n; 445 float u[w]; 446 int i; 447 448 ym -= (w >> 1); 449 450 /* --- Load previous samples --- */ 451 452 for (i = 1-w; i < 0; i++) { 453 float xi = *(xm++), yi = *(ym++); 454 455 u[i + w-1] = 0; 456 for (int k = w-1; k+i >= 0; k--) 457 u[i+k] += xi * c[k][0] - yi * c[k][1]; 458 } 459 460 u[w-1] = 0; 461 462 /* --- Process --- */ 463 464 for (; i < n; i += w) { 465 466 for (int j = 0; j < w; j++, g += g_incr) { 467 float xi = *x, yi = *(ym++); 468 469 for (int k = w-1; k >= 0; k--) 470 u[(j+k)%w] += xi * c[k][0] - yi * c[k][1]; 471 472 *(x++) = xi - g * u[j]; 473 u[j] = 0; 474 } 475 } 476 } 477 478 /** 479 * Synthesis filter for each samplerates (width of filter) 480 */ 481 482 static void synthesize_4(const float *ym, const float *xm, 483 float *x, int n, const float (*c)[2], int fade) 484 { 485 synthesize_template(ym, xm, x, n, c, 4, fade); 486 } 487 488 static void synthesize_6(const float *ym, const float *xm, 489 float *x, int n, const float (*c)[2], int fade) 490 { 491 synthesize_template(ym, xm, x, n, c, 6, fade); 492 } 493 494 static void synthesize_8(const float *ym, const float *xm, 495 float *x, int n, const float (*c)[2], int fade) 496 { 497 synthesize_template(ym, xm, x, n, c, 8, fade); 498 } 499 500 static void synthesize_12(const float *ym, const float *xm, 501 float *x, int n, const float (*c)[2], int fade) 502 { 503 synthesize_template(ym, xm, x, n, c, 12, fade); 504 } 505 506 static void (* const synthesize[])( 507 const float *, const float *, float *, int, const float (*)[2], int) = 508 { 509 [LC3_SRATE_8K ] = synthesize_4, 510 [LC3_SRATE_16K] = synthesize_4, 511 [LC3_SRATE_24K] = synthesize_6, 512 [LC3_SRATE_32K] = synthesize_8, 513 [LC3_SRATE_48K] = synthesize_12, 514 }; 515 516 /** 517 * LTPF Synthesis 518 */ 519 void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, 520 int nbytes, struct lc3_ltpf_synthesis *ltpf, 521 const struct lc3_ltpf_data *data, float *x) 522 { 523 int dt_us = LC3_DT_US(dt); 524 525 /* --- Filter parameters --- */ 526 527 int p_idx = data ? data->pitch_index : 0; 528 int pitch = 529 p_idx >= 440 ? (((p_idx ) - 283) << 2) : 530 p_idx >= 380 ? (((p_idx >> 1) - 63) << 2) + (((p_idx & 1)) << 1) : 531 (((p_idx >> 2) + 32) << 2) + (((p_idx & 3)) << 0) ; 532 533 pitch = (pitch * LC3_SRATE_KHZ(sr) * 10 + 64) / 128; 534 535 int nbits = (nbytes*8 * 10000 + (dt_us/2)) / dt_us; 536 int g_idx = LC3_MAX(nbits / 80, 3 + (int)sr) - (3 + sr); 537 bool active = data && data->active && g_idx < 4; 538 539 int w = LC3_MAX(4, LC3_SRATE_KHZ(sr) / 4); 540 float c[w][2]; 541 542 for (int i = 0; i < w; i++) { 543 float g = active ? 0.4f - 0.05f * g_idx : 0; 544 545 c[i][0] = active ? 0.85f * g * lc3_ltpf_cnum[sr][g_idx][i] : 0; 546 c[i][1] = active ? g * lc3_ltpf_cden[sr][pitch & 3][i] : 0; 547 } 548 549 /* --- Transition handling --- */ 550 551 int ns = LC3_NS(dt, sr); 552 int nt = ns / (4 - (dt == LC3_DT_7M5)); 553 float xm[12]; 554 555 if (active) 556 memcpy(xm, x + nt-(w-1), (w-1) * sizeof(float)); 557 558 if (!ltpf->active && active) 559 synthesize[sr](x - pitch/4, ltpf->x, x, nt, c, 1); 560 else if (ltpf->active && !active) 561 synthesize[sr](x - ltpf->pitch/4, ltpf->x, x, nt, ltpf->c, -1); 562 else if (ltpf->active && active && ltpf->pitch == pitch) 563 synthesize[sr](x - pitch/4, ltpf->x, x, nt, c, 0); 564 else if (ltpf->active && active) { 565 synthesize[sr](x - ltpf->pitch/4, ltpf->x, x, nt, ltpf->c, -1); 566 synthesize[sr](x - pitch/4, x - (w-1), x, nt, c, 1); 567 } 568 569 /* --- Remainder --- */ 570 571 memcpy(ltpf->x, x + ns-(w-1), (w-1) * sizeof(float)); 572 573 if (active) 574 synthesize[sr](x - pitch/4 + nt, xm, x + nt, ns-nt, c, 0); 575 576 /* --- Update state --- */ 577 578 ltpf->active = active; 579 ltpf->pitch = pitch; 580 memcpy(ltpf->c, c, w * sizeof(ltpf->c[0])); 581 } 582 583 584 /* ---------------------------------------------------------------------------- 585 * Bitstream data 586 * -------------------------------------------------------------------------- */ 587 588 /** 589 * LTPF disable 590 */ 591 void lc3_ltpf_disable(struct lc3_ltpf_data *data) 592 { 593 data->active = false; 594 } 595 596 /** 597 * Return number of bits coding the bitstream data 598 */ 599 int lc3_ltpf_get_nbits(bool pitch) 600 { 601 return 1 + 10 * pitch; 602 } 603 604 /** 605 * Put bitstream data 606 */ 607 void lc3_ltpf_put_data(lc3_bits_t *bits, 608 const struct lc3_ltpf_data *data) 609 { 610 lc3_put_bit(bits, data->active); 611 lc3_put_bits(bits, data->pitch_index, 9); 612 } 613 614 /** 615 * Get bitstream data 616 */ 617 void lc3_ltpf_get_data(lc3_bits_t *bits, struct lc3_ltpf_data *data) 618 { 619 data->active = lc3_get_bit(bits); 620 data->pitch_index = lc3_get_bits(bits, 9); 621 } 622