1 /****************************************************************************** 2 * 3 * Copyright 2022 Google LLC 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 "spec.h" 20 #include "bits.h" 21 #include "tables.h" 22 23 24 /* ---------------------------------------------------------------------------- 25 * Global Gain / Quantization 26 * -------------------------------------------------------------------------- */ 27 28 /** 29 * Resolve quantized gain index offset 30 * sr, nbytes Samplerate and size of the frame 31 * return Gain index offset 32 */ 33 static int resolve_gain_offset(enum lc3_srate sr, int nbytes) 34 { 35 int g_off = (nbytes * 8) / (10 * (1 + sr)); 36 return 105 + 5*(1 + sr) + LC3_MIN(g_off, 115); 37 } 38 39 /** 40 * Global Gain Estimation 41 * dt, sr Duration and samplerate of the frame 42 * x Spectral coefficients 43 * nbits_budget Number of bits available coding the spectrum 44 * nbits_off Offset on the available bits, temporarily smoothed 45 * g_off Gain index offset 46 * reset_off Return True when the nbits_off must be reset 47 * return The quantized gain value 48 */ 49 LC3_HOT static int estimate_gain( 50 enum lc3_dt dt, enum lc3_srate sr, const float *x, 51 int nbits_budget, float nbits_off, int g_off, bool *reset_off) 52 { 53 int ne = LC3_NE(dt, sr) >> 2; 54 int e[ne]; 55 56 /* --- Energy (dB) by 4 MDCT blocks --- */ 57 58 float x2_max = 0; 59 60 for (int i = 0; i < ne; i++, x += 4) { 61 float x0 = x[0] * x[0]; 62 float x1 = x[1] * x[1]; 63 float x2 = x[2] * x[2]; 64 float x3 = x[3] * x[3]; 65 66 x2_max = fmaxf(x2_max, x0); 67 x2_max = fmaxf(x2_max, x1); 68 x2_max = fmaxf(x2_max, x2); 69 x2_max = fmaxf(x2_max, x3); 70 71 e[i] = fast_db_q16(fmaxf(x0 + x1 + x2 + x3, 1e-10f)); 72 } 73 74 /* --- Determine gain index --- */ 75 76 int nbits = nbits_budget + nbits_off + 0.5f; 77 int g_int = 255 - g_off; 78 79 const int k_20_28 = 20.f/28 * 0x1p16f + 0.5f; 80 const int k_2u7 = 2.7f * 0x1p16f + 0.5f; 81 const int k_1u4 = 1.4f * 0x1p16f + 0.5f; 82 83 for (int i = 128, j, j0 = ne-1, j1 ; i > 0; i >>= 1) { 84 int gn = (g_int - i) * k_20_28; 85 int v = 0; 86 87 for (j = j0; j >= 0 && e[j] < gn; j--); 88 89 for (j1 = j; j >= 0; j--) { 90 int e_diff = e[j] - gn; 91 92 v += e_diff < 0 ? k_2u7 : 93 e_diff < 43 << 16 ? e_diff + ( 7 << 16) 94 : 2*e_diff - (36 << 16); 95 } 96 97 if (v > nbits * k_1u4) 98 j0 = j1; 99 else 100 g_int = g_int - i; 101 } 102 103 /* --- Limit gain index --- */ 104 105 int g_min = x2_max == 0 ? -g_off : 106 ceilf(28 * log10f(sqrtf(x2_max) / (32768 - 0.375f))); 107 108 *reset_off = g_int < g_min || x2_max == 0; 109 if (*reset_off) 110 g_int = g_min; 111 112 return g_int; 113 } 114 115 /** 116 * Global Gain Adjustment 117 * sr Samplerate of the frame 118 * g_idx The estimated quantized gain index 119 * nbits Computed number of bits coding the spectrum 120 * nbits_budget Number of bits available for coding the spectrum 121 * return Gain adjust value (-1 to 2) 122 */ 123 LC3_HOT static int adjust_gain( 124 enum lc3_srate sr, int g_idx, int nbits, int nbits_budget) 125 { 126 /* --- Compute delta threshold --- */ 127 128 const int *t = (const int [LC3_NUM_SRATE][3]){ 129 { 80, 500, 850 }, { 230, 1025, 1700 }, { 380, 1550, 2550 }, 130 { 530, 2075, 3400 }, { 680, 2600, 4250 } 131 }[sr]; 132 133 int delta, den = 48; 134 135 if (nbits < t[0]) { 136 delta = 3*(nbits + 48); 137 138 } else if (nbits < t[1]) { 139 int n0 = 3*(t[0] + 48), range = t[1] - t[0]; 140 delta = n0 * range + (nbits - t[0]) * (t[1] - n0); 141 den *= range; 142 143 } else { 144 delta = LC3_MIN(nbits, t[2]); 145 } 146 147 delta = (delta + den/2) / den; 148 149 /* --- Adjust gain --- */ 150 151 if (nbits < nbits_budget - (delta + 2)) 152 return -(g_idx > 0); 153 154 if (nbits > nbits_budget) 155 return (g_idx < 255) + (g_idx < 254 && nbits >= nbits_budget + delta); 156 157 return 0; 158 } 159 160 /** 161 * Unquantize gain 162 * g_int Quantization gain value 163 * return Unquantized gain value 164 */ 165 static float unquantize_gain(int g_int) 166 { 167 /* Unquantization gain table : 168 * G[i] = 10 ^ (i / 28) , i = [0..64] */ 169 170 static const float iq_table[] = { 171 1.00000000e+00, 1.08571112e+00, 1.17876863e+00, 1.27980221e+00, 172 1.38949549e+00, 1.50859071e+00, 1.63789371e+00, 1.77827941e+00, 173 1.93069773e+00, 2.09617999e+00, 2.27584593e+00, 2.47091123e+00, 174 2.68269580e+00, 2.91263265e+00, 3.16227766e+00, 3.43332002e+00, 175 3.72759372e+00, 4.04708995e+00, 4.39397056e+00, 4.77058270e+00, 176 5.17947468e+00, 5.62341325e+00, 6.10540230e+00, 6.62870316e+00, 177 7.19685673e+00, 7.81370738e+00, 8.48342898e+00, 9.21055318e+00, 178 1.00000000e+01, 1.08571112e+01, 1.17876863e+01, 1.27980221e+01, 179 1.38949549e+01, 1.50859071e+01, 1.63789371e+01, 1.77827941e+01, 180 1.93069773e+01, 2.09617999e+01, 2.27584593e+01, 2.47091123e+01, 181 2.68269580e+01, 2.91263265e+01, 3.16227766e+01, 3.43332002e+01, 182 3.72759372e+01, 4.04708995e+01, 4.39397056e+01, 4.77058270e+01, 183 5.17947468e+01, 5.62341325e+01, 6.10540230e+01, 6.62870316e+01, 184 7.19685673e+01, 7.81370738e+01, 8.48342898e+01, 9.21055318e+01, 185 1.00000000e+02, 1.08571112e+02, 1.17876863e+02, 1.27980221e+02, 186 1.38949549e+02, 1.50859071e+02, 1.63789371e+02, 1.77827941e+02, 187 1.93069773e+02 188 }; 189 190 float g = iq_table[LC3_ABS(g_int) & 0x3f]; 191 for(int n64 = LC3_ABS(g_int) >> 6; n64--; ) 192 g *= iq_table[64]; 193 194 return g_int >= 0 ? g : 1 / g; 195 } 196 197 /** 198 * Spectrum quantization 199 * dt, sr Duration and samplerate of the frame 200 * g_int Quantization gain value 201 * x Spectral coefficients, scaled as output 202 * xq, nq Output spectral quantized coefficients, and count 203 * 204 * The spectral coefficients `xq` are stored as : 205 * b0 0:positive or zero 1:negative 206 * b15..b1 Absolute value 207 */ 208 LC3_HOT static void quantize(enum lc3_dt dt, enum lc3_srate sr, 209 int g_int, float *x, uint16_t *xq, int *nq) 210 { 211 float g_inv = 1 / unquantize_gain(g_int); 212 int ne = LC3_NE(dt, sr); 213 214 *nq = ne; 215 216 for (int i = 0; i < ne; i += 2) { 217 uint16_t x0, x1; 218 219 x[i+0] *= g_inv; 220 x[i+1] *= g_inv; 221 222 x0 = fminf(fabsf(x[i+0]) + 6.f/16, INT16_MAX); 223 x1 = fminf(fabsf(x[i+1]) + 6.f/16, INT16_MAX); 224 225 xq[i+0] = (x0 << 1) + ((x0 > 0) & (x[i+0] < 0)); 226 xq[i+1] = (x1 << 1) + ((x1 > 0) & (x[i+1] < 0)); 227 228 *nq = x0 || x1 ? ne : *nq - 2; 229 } 230 } 231 232 /** 233 * Spectrum quantization inverse 234 * dt, sr Duration and samplerate of the frame 235 * g_int Quantization gain value 236 * x, nq Spectral quantized, and count of significants 237 * return Unquantized gain value 238 */ 239 LC3_HOT static float unquantize(enum lc3_dt dt, enum lc3_srate sr, 240 int g_int, float *x, int nq) 241 { 242 float g = unquantize_gain(g_int); 243 int i, ne = LC3_NE(dt, sr); 244 245 for (i = 0; i < nq; i++) 246 x[i] = x[i] * g; 247 248 for ( ; i < ne; i++) 249 x[i] = 0; 250 251 return g; 252 } 253 254 255 /* ---------------------------------------------------------------------------- 256 * Spectrum coding 257 * -------------------------------------------------------------------------- */ 258 259 /** 260 * Resolve High-bitrate mode according size of the frame 261 * sr, nbytes Samplerate and size of the frame 262 * return True when High-Rate mode enabled 263 */ 264 static int resolve_high_rate(enum lc3_srate sr, int nbytes) 265 { 266 return nbytes > 20 * (1 + (int)sr); 267 } 268 269 /** 270 * Bit consumption 271 * dt, sr, nbytes Duration, samplerate and size of the frame 272 * x Spectral quantized coefficients 273 * n Count of significant coefficients, updated on truncation 274 * nbits_budget Truncate to stay in budget, when not zero 275 * p_lsb_mode Return True when LSB's are not AC coded, or NULL 276 * return The number of bits coding the spectrum 277 * 278 * The spectral coefficients `x` storage is : 279 * b0 0:positive or zero 1:negative 280 * b15..b1 Absolute value 281 */ 282 LC3_HOT static int compute_nbits( 283 enum lc3_dt dt, enum lc3_srate sr, int nbytes, 284 const uint16_t *x, int *n, int nbits_budget, bool *p_lsb_mode) 285 { 286 int ne = LC3_NE(dt, sr); 287 288 /* --- Mode and rate --- */ 289 290 bool lsb_mode = nbytes >= 20 * (3 + (int)sr); 291 bool high_rate = resolve_high_rate(sr, nbytes); 292 293 /* --- Loop on quantized coefficients --- */ 294 295 int nbits = 0, nbits_lsb = 0; 296 uint8_t state = 0; 297 298 int nbits_end = 0; 299 int n_end = 0; 300 301 nbits_budget = nbits_budget ? nbits_budget * 2048 : INT_MAX; 302 303 for (int i = 0, h = 0; h < 2; h++) { 304 const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; 305 306 for ( ; i < LC3_MIN(*n, (ne + 2) >> (1 - h)) 307 && nbits <= nbits_budget; i += 2) { 308 309 const uint8_t *lut = lut_coeff[state]; 310 uint16_t a = x[i] >> 1, b = x[i+1] >> 1; 311 312 /* --- Sign values --- */ 313 314 int s = (a > 0) + (b > 0); 315 nbits += s * 2048; 316 317 /* --- LSB values Reduce to 2*2 bits MSB values --- 318 * Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic 319 * coded with an escape code followed by 1 bit for each values. 320 * The LSB mode does not arthmetic code the first LSB, 321 * add the sign of the LSB when one of pair was at value 1 */ 322 323 int k = 0; 324 int m = (a | b) >> 2; 325 326 if (m) { 327 328 if (lsb_mode) { 329 nbits += lc3_spectrum_bits[lut[k++]][16] - 2*2048; 330 nbits_lsb += 2 + (a == 1) + (b == 1); 331 } 332 333 for (m >>= lsb_mode; m; m >>= 1, k++) 334 nbits += lc3_spectrum_bits[lut[LC3_MIN(k, 3)]][16]; 335 336 nbits += k * 2*2048; 337 a >>= k; 338 b >>= k; 339 340 k = LC3_MIN(k, 3); 341 } 342 343 /* --- MSB values --- */ 344 345 nbits += lc3_spectrum_bits[lut[k]][a + 4*b]; 346 347 /* --- Update state --- */ 348 349 if (s && nbits <= nbits_budget) { 350 n_end = i + 2; 351 nbits_end = nbits; 352 } 353 354 state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); 355 } 356 } 357 358 /* --- Return --- */ 359 360 *n = n_end; 361 362 if (p_lsb_mode) 363 *p_lsb_mode = lsb_mode && 364 nbits_end + nbits_lsb * 2048 > nbits_budget; 365 366 if (nbits_budget >= INT_MAX) 367 nbits_end += nbits_lsb * 2048; 368 369 return (nbits_end + 2047) / 2048; 370 } 371 372 /** 373 * Put quantized spectrum 374 * bits Bitstream context 375 * dt, sr, nbytes Duration, samplerate and size of the frame 376 * x Spectral quantized 377 * nq, lsb_mode Count of significants, and LSB discard indication 378 * 379 * The spectral coefficients `x` storage is : 380 * b0 0:positive or zero 1:negative 381 * b15..b1 Absolute value 382 */ 383 LC3_HOT static void put_quantized(lc3_bits_t *bits, 384 enum lc3_dt dt, enum lc3_srate sr, int nbytes, 385 const uint16_t *x, int nq, bool lsb_mode) 386 { 387 int ne = LC3_NE(dt, sr); 388 bool high_rate = resolve_high_rate(sr, nbytes); 389 390 /* --- Loop on quantized coefficients --- */ 391 392 uint8_t state = 0; 393 394 for (int i = 0, h = 0; h < 2; h++) { 395 const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; 396 397 for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) { 398 399 const uint8_t *lut = lut_coeff[state]; 400 uint16_t a = x[i] >> 1, b = x[i+1] >> 1; 401 402 /* --- LSB values Reduce to 2*2 bits MSB values --- 403 * Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic 404 * coded with an escape code and 1 bits for each values. 405 * The LSB mode discard the first LSB (at this step) */ 406 407 int m = (a | b) >> 2; 408 int k = 0, shr = 0; 409 410 if (m) { 411 412 if (lsb_mode) 413 lc3_put_symbol(bits, 414 lc3_spectrum_models + lut[k++], 16); 415 416 for (m >>= lsb_mode; m; m >>= 1, k++) { 417 lc3_put_bit(bits, (a >> k) & 1); 418 lc3_put_bit(bits, (b >> k) & 1); 419 lc3_put_symbol(bits, 420 lc3_spectrum_models + lut[LC3_MIN(k, 3)], 16); 421 } 422 423 a >>= lsb_mode; 424 b >>= lsb_mode; 425 426 shr = k - lsb_mode; 427 k = LC3_MIN(k, 3); 428 } 429 430 /* --- Sign values --- */ 431 432 if (a) lc3_put_bit(bits, x[i+0] & 1); 433 if (b) lc3_put_bit(bits, x[i+1] & 1); 434 435 /* --- MSB values --- */ 436 437 a >>= shr; 438 b >>= shr; 439 440 lc3_put_symbol(bits, lc3_spectrum_models + lut[k], a + 4*b); 441 442 /* --- Update state --- */ 443 444 state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); 445 } 446 } 447 } 448 449 /** 450 * Get quantized spectrum 451 * bits Bitstream context 452 * dt, sr, nbytes Duration, samplerate and size of the frame 453 * nq, lsb_mode Count of significants, and LSB discard indication 454 * xq Return `nq` spectral quantized coefficients 455 * nf_seed Return the noise factor seed associated 456 * return 0: Ok -1: Invalid bitstream data 457 */ 458 LC3_HOT static int get_quantized(lc3_bits_t *bits, 459 enum lc3_dt dt, enum lc3_srate sr, int nbytes, 460 int nq, bool lsb_mode, float *xq, uint16_t *nf_seed) 461 { 462 int ne = LC3_NE(dt, sr); 463 bool high_rate = resolve_high_rate(sr, nbytes); 464 465 *nf_seed = 0; 466 467 /* --- Loop on quantized coefficients --- */ 468 469 uint8_t state = 0; 470 471 for (int i = 0, h = 0; h < 2; h++) { 472 const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; 473 474 for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) { 475 476 const uint8_t *lut = lut_coeff[state]; 477 478 /* --- LSB values --- 479 * Until the symbol read indicates the escape value 16, 480 * read an LSB bit for each values. 481 * The LSB mode discard the first LSB (at this step) */ 482 483 int u = 0, v = 0; 484 int k = 0, shl = 0; 485 486 unsigned s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]); 487 488 if (lsb_mode && s >= 16) { 489 s = lc3_get_symbol(bits, lc3_spectrum_models + lut[++k]); 490 shl++; 491 } 492 493 for ( ; s >= 16 && shl < 14; shl++) { 494 u |= lc3_get_bit(bits) << shl; 495 v |= lc3_get_bit(bits) << shl; 496 497 k += (k < 3); 498 s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]); 499 } 500 501 if (s >= 16) 502 return -1; 503 504 /* --- MSB & sign values --- */ 505 506 int a = s % 4; 507 int b = s / 4; 508 509 u |= a << shl; 510 v |= b << shl; 511 512 xq[i ] = u && lc3_get_bit(bits) ? -u : u; 513 xq[i+1] = v && lc3_get_bit(bits) ? -v : v; 514 515 *nf_seed = (*nf_seed + u * i + v * (i+1)) & 0xffff; 516 517 /* --- Update state --- */ 518 519 state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); 520 } 521 } 522 523 return 0; 524 } 525 526 /** 527 * Put residual bits of quantization 528 * bits Bitstream context 529 * nbits Maximum number of bits to output 530 * x, n Spectral quantized, and count of significants 531 * xf Scaled spectral coefficients 532 * 533 * The spectral coefficients `x` storage is : 534 * b0 0:positive or zero 1:negative 535 * b15..b1 Absolute value 536 */ 537 LC3_HOT static void put_residual( 538 lc3_bits_t *bits, int nbits, const uint16_t *x, int n, const float *xf) 539 { 540 for (int i = 0; i < n && nbits > 0; i++) { 541 542 if (x[i] == 0) 543 continue; 544 545 float xq = x[i] & 1 ? -(x[i] >> 1) : (x[i] >> 1); 546 547 lc3_put_bit(bits, xf[i] >= xq); 548 nbits--; 549 } 550 } 551 552 /** 553 * Get residual bits of quantization 554 * bits Bitstream context 555 * nbits Maximum number of bits to output 556 * x, nq Spectral quantized, and count of significants 557 */ 558 LC3_HOT static void get_residual( 559 lc3_bits_t *bits, int nbits, float *x, int nq) 560 { 561 for (int i = 0; i < nq && nbits > 0; i++) { 562 563 if (x[i] == 0) 564 continue; 565 566 if (lc3_get_bit(bits) == 0) 567 x[i] -= x[i] < 0 ? 5.f/16 : 3.f/16; 568 else 569 x[i] += x[i] > 0 ? 5.f/16 : 3.f/16; 570 571 nbits--; 572 } 573 } 574 575 /** 576 * Put LSB values of quantized spectrum values 577 * bits Bitstream context 578 * nbits Maximum number of bits to output 579 * x, n Spectral quantized, and count of significants 580 * 581 * The spectral coefficients `x` storage is : 582 * b0 0:positive or zero 1:negative 583 * b15..b1 Absolute value 584 */ 585 LC3_HOT static void put_lsb( 586 lc3_bits_t *bits, int nbits, const uint16_t *x, int n) 587 { 588 for (int i = 0; i < n && nbits > 0; i += 2) { 589 uint16_t a = x[i] >> 1, b = x[i+1] >> 1; 590 int a_neg = x[i] & 1, b_neg = x[i+1] & 1; 591 592 if ((a | b) >> 2 == 0) 593 continue; 594 595 if (nbits-- > 0) 596 lc3_put_bit(bits, a & 1); 597 598 if (a == 1 && nbits-- > 0) 599 lc3_put_bit(bits, a_neg); 600 601 if (nbits-- > 0) 602 lc3_put_bit(bits, b & 1); 603 604 if (b == 1 && nbits-- > 0) 605 lc3_put_bit(bits, b_neg); 606 } 607 } 608 609 /** 610 * Get LSB values of quantized spectrum values 611 * bits Bitstream context 612 * nbits Maximum number of bits to output 613 * x, nq Spectral quantized, and count of significants 614 * nf_seed Update the noise factor seed according 615 */ 616 LC3_HOT static void get_lsb(lc3_bits_t *bits, 617 int nbits, float *x, int nq, uint16_t *nf_seed) 618 { 619 for (int i = 0; i < nq && nbits > 0; i += 2) { 620 621 float a = fabsf(x[i]), b = fabsf(x[i+1]); 622 623 if (fmaxf(a, b) < 4) 624 continue; 625 626 if (nbits-- > 0 && lc3_get_bit(bits)) { 627 if (a) { 628 x[i] += x[i] < 0 ? -1 : 1; 629 *nf_seed = (*nf_seed + i) & 0xffff; 630 } else if (nbits-- > 0) { 631 x[i] = lc3_get_bit(bits) ? -1 : 1; 632 *nf_seed = (*nf_seed + i) & 0xffff; 633 } 634 } 635 636 if (nbits-- > 0 && lc3_get_bit(bits)) { 637 if (b) { 638 x[i+1] += x[i+1] < 0 ? -1 : 1; 639 *nf_seed = (*nf_seed + i+1) & 0xffff; 640 } else if (nbits-- > 0) { 641 x[i+1] = lc3_get_bit(bits) ? -1 : 1; 642 *nf_seed = (*nf_seed + i+1) & 0xffff; 643 } 644 } 645 } 646 } 647 648 649 /* ---------------------------------------------------------------------------- 650 * Noise coding 651 * -------------------------------------------------------------------------- */ 652 653 /** 654 * Estimate noise level 655 * dt, bw Duration and bandwidth of the frame 656 * xq, nq Quantized spectral coefficients 657 * x Quantization scaled spectrum coefficients 658 * return Noise factor (0 to 7) 659 * 660 * The spectral coefficients `x` storage is : 661 * b0 0:positive or zero 1:negative 662 * b15..b1 Absolute value 663 */ 664 LC3_HOT static int estimate_noise(enum lc3_dt dt, enum lc3_bandwidth bw, 665 const uint16_t *xq, int nq, const float *x) 666 { 667 int bw_stop = (dt == LC3_DT_7M5 ? 60 : 80) * (1 + bw); 668 int w = 2 + dt; 669 670 float sum = 0; 671 int i, n = 0, z = 0; 672 673 for (i = 6*(3 + dt) - w; i < LC3_MIN(nq, bw_stop); i++) { 674 z = xq[i] ? 0 : z + 1; 675 if (z > 2*w) 676 sum += fabsf(x[i - w]), n++; 677 } 678 679 for ( ; i < bw_stop + w; i++) 680 if (++z > 2*w) 681 sum += fabsf(x[i - w]), n++; 682 683 int nf = n ? 8 - (int)((16 * sum) / n + 0.5f) : 0; 684 685 return LC3_CLIP(nf, 0, 7); 686 } 687 688 /** 689 * Noise filling 690 * dt, bw Duration and bandwidth of the frame 691 * nf, nf_seed The noise factor and pseudo-random seed 692 * g Quantization gain 693 * x, nq Spectral quantized, and count of significants 694 */ 695 LC3_HOT static void fill_noise(enum lc3_dt dt, enum lc3_bandwidth bw, 696 int nf, uint16_t nf_seed, float g, float *x, int nq) 697 { 698 int bw_stop = (dt == LC3_DT_7M5 ? 60 : 80) * (1 + bw); 699 int w = 2 + dt; 700 701 float s = g * (float)(8 - nf) / 16; 702 int i, z = 0; 703 704 for (i = 6*(3 + dt) - w; i < LC3_MIN(nq, bw_stop); i++) { 705 z = x[i] ? 0 : z + 1; 706 if (z > 2*w) { 707 nf_seed = (13849 + nf_seed*31821) & 0xffff; 708 x[i - w] = nf_seed & 0x8000 ? -s : s; 709 } 710 } 711 712 for ( ; i < bw_stop + w; i++) 713 if (++z > 2*w) { 714 nf_seed = (13849 + nf_seed*31821) & 0xffff; 715 x[i - w] = nf_seed & 0x8000 ? -s : s; 716 } 717 } 718 719 /** 720 * Put noise factor 721 * bits Bitstream context 722 * nf Noise factor (0 to 7) 723 */ 724 static void put_noise_factor(lc3_bits_t *bits, int nf) 725 { 726 lc3_put_bits(bits, nf, 3); 727 } 728 729 /** 730 * Get noise factor 731 * bits Bitstream context 732 * return Noise factor (0 to 7) 733 */ 734 static int get_noise_factor(lc3_bits_t *bits) 735 { 736 return lc3_get_bits(bits, 3); 737 } 738 739 740 /* ---------------------------------------------------------------------------- 741 * Encoding 742 * -------------------------------------------------------------------------- */ 743 744 /** 745 * Bit consumption of the number of coded coefficients 746 * dt, sr Duration, samplerate of the frame 747 * return Bit consumpution of the number of coded coefficients 748 */ 749 static int get_nbits_nq(enum lc3_dt dt, enum lc3_srate sr) 750 { 751 int ne = LC3_NE(dt, sr); 752 return 4 + (ne > 32) + (ne > 64) + (ne > 128) + (ne > 256); 753 } 754 755 /** 756 * Bit consumption of the arithmetic coder 757 * dt, sr, nbytes Duration, samplerate and size of the frame 758 * return Bit consumption of bitstream data 759 */ 760 static int get_nbits_ac(enum lc3_dt dt, enum lc3_srate sr, int nbytes) 761 { 762 return get_nbits_nq(dt, sr) + 3 + LC3_MIN((nbytes-1) / 160, 2); 763 } 764 765 /** 766 * Spectrum analysis 767 */ 768 void lc3_spec_analyze(enum lc3_dt dt, enum lc3_srate sr, 769 int nbytes, bool pitch, const lc3_tns_data_t *tns, 770 struct lc3_spec_analysis *spec, float *x, 771 uint16_t *xq, struct lc3_spec_side *side) 772 { 773 bool reset_off; 774 775 /* --- Bit budget --- */ 776 777 const int nbits_gain = 8; 778 const int nbits_nf = 3; 779 780 int nbits_budget = 8*nbytes - get_nbits_ac(dt, sr, nbytes) - 781 lc3_bwdet_get_nbits(sr) - lc3_ltpf_get_nbits(pitch) - 782 lc3_sns_get_nbits() - lc3_tns_get_nbits(tns) - nbits_gain - nbits_nf; 783 784 /* --- Global gain --- */ 785 786 float nbits_off = spec->nbits_off + spec->nbits_spare; 787 nbits_off = fminf(fmaxf(nbits_off, -40), 40); 788 nbits_off = 0.8f * spec->nbits_off + 0.2f * nbits_off; 789 790 int g_off = resolve_gain_offset(sr, nbytes); 791 792 int g_int = estimate_gain(dt, sr, 793 x, nbits_budget, nbits_off, g_off, &reset_off); 794 795 /* --- Quantization --- */ 796 797 quantize(dt, sr, g_int, x, xq, &side->nq); 798 799 int nbits = compute_nbits(dt, sr, nbytes, xq, &side->nq, 0, NULL); 800 801 spec->nbits_off = reset_off ? 0 : nbits_off; 802 spec->nbits_spare = reset_off ? 0 : nbits_budget - nbits; 803 804 /* --- Adjust gain and requantize --- */ 805 806 int g_adj = adjust_gain(sr, g_int + g_off, nbits, nbits_budget); 807 808 if (g_adj) 809 quantize(dt, sr, g_adj, x, xq, &side->nq); 810 811 side->g_idx = g_int + g_adj + g_off; 812 nbits = compute_nbits(dt, sr, nbytes, 813 xq, &side->nq, nbits_budget, &side->lsb_mode); 814 } 815 816 /** 817 * Put spectral quantization side data 818 */ 819 void lc3_spec_put_side(lc3_bits_t *bits, 820 enum lc3_dt dt, enum lc3_srate sr, const struct lc3_spec_side *side) 821 { 822 int nbits_nq = get_nbits_nq(dt, sr); 823 824 lc3_put_bits(bits, LC3_MAX(side->nq >> 1, 1) - 1, nbits_nq); 825 lc3_put_bits(bits, side->lsb_mode, 1); 826 lc3_put_bits(bits, side->g_idx, 8); 827 } 828 829 /** 830 * Encode spectral coefficients 831 */ 832 void lc3_spec_encode(lc3_bits_t *bits, 833 enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, int nbytes, 834 const uint16_t *xq, const lc3_spec_side_t *side, const float *x) 835 { 836 bool lsb_mode = side->lsb_mode; 837 int nq = side->nq; 838 839 put_noise_factor(bits, estimate_noise(dt, bw, xq, nq, x)); 840 841 put_quantized(bits, dt, sr, nbytes, xq, nq, lsb_mode); 842 843 int nbits_left = lc3_get_bits_left(bits); 844 845 if (lsb_mode) 846 put_lsb(bits, nbits_left, xq, nq); 847 else 848 put_residual(bits, nbits_left, xq, nq, x); 849 } 850 851 852 /* ---------------------------------------------------------------------------- 853 * Decoding 854 * -------------------------------------------------------------------------- */ 855 856 /** 857 * Get spectral quantization side data 858 */ 859 int lc3_spec_get_side(lc3_bits_t *bits, 860 enum lc3_dt dt, enum lc3_srate sr, struct lc3_spec_side *side) 861 { 862 int nbits_nq = get_nbits_nq(dt, sr); 863 int ne = LC3_NE(dt, sr); 864 865 side->nq = (lc3_get_bits(bits, nbits_nq) + 1) << 1; 866 side->lsb_mode = lc3_get_bit(bits); 867 side->g_idx = lc3_get_bits(bits, 8); 868 869 return side->nq > ne ? (side->nq = ne), -1 : 0; 870 } 871 872 /** 873 * Decode spectral coefficients 874 */ 875 int lc3_spec_decode(lc3_bits_t *bits, 876 enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, 877 int nbytes, const lc3_spec_side_t *side, float *x) 878 { 879 bool lsb_mode = side->lsb_mode; 880 int nq = side->nq; 881 int ret = 0; 882 883 int nf = get_noise_factor(bits); 884 uint16_t nf_seed; 885 886 if ((ret = get_quantized(bits, dt, sr, nbytes, 887 nq, lsb_mode, x, &nf_seed)) < 0) 888 return ret; 889 890 int nbits_left = lc3_get_bits_left(bits); 891 892 if (lsb_mode) 893 get_lsb(bits, nbits_left, x, nq, &nf_seed); 894 else 895 get_residual(bits, nbits_left, x, nq); 896 897 int g_int = side->g_idx - resolve_gain_offset(sr, nbytes); 898 float g = unquantize(dt, sr, g_int, x, nq); 899 900 if (nq > 2 || x[0] || x[1] || side->g_idx > 0 || nf < 7) 901 fill_noise(dt, bw, nf, nf_seed, g, x, nq); 902 903 return 0; 904 } 905