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 <lc3.h> 20 21 #include "common.h" 22 #include "bits.h" 23 24 #include "attdet.h" 25 #include "bwdet.h" 26 #include "ltpf.h" 27 #include "mdct.h" 28 #include "energy.h" 29 #include "sns.h" 30 #include "tns.h" 31 #include "spec.h" 32 #include "plc.h" 33 34 35 /** 36 * Frame side data 37 */ 38 39 struct side_data { 40 enum lc3_bandwidth bw; 41 bool pitch_present; 42 lc3_ltpf_data_t ltpf; 43 lc3_sns_data_t sns; 44 lc3_tns_data_t tns; 45 lc3_spec_side_t spec; 46 }; 47 48 49 /* ---------------------------------------------------------------------------- 50 * General 51 * -------------------------------------------------------------------------- */ 52 53 /** 54 * Resolve frame duration in us 55 * us Frame duration in us 56 * return Frame duration identifier, or LC3_NUM_DT 57 */ 58 static enum lc3_dt resolve_dt(int us) 59 { 60 return us == 7500 ? LC3_DT_7M5 : 61 us == 10000 ? LC3_DT_10M : LC3_NUM_DT; 62 } 63 64 /** 65 * Resolve samplerate in Hz 66 * hz Samplerate in Hz 67 * return Sample rate identifier, or LC3_NUM_SRATE 68 */ 69 static enum lc3_srate resolve_sr(int hz) 70 { 71 return hz == 8000 ? LC3_SRATE_8K : hz == 16000 ? LC3_SRATE_16K : 72 hz == 24000 ? LC3_SRATE_24K : hz == 32000 ? LC3_SRATE_32K : 73 hz == 48000 ? LC3_SRATE_48K : LC3_NUM_SRATE; 74 } 75 76 /** 77 * Return the number of PCM samples in a frame 78 */ 79 int lc3_frame_samples(int dt_us, int sr_hz) 80 { 81 enum lc3_dt dt = resolve_dt(dt_us); 82 enum lc3_srate sr = resolve_sr(sr_hz); 83 84 if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE) 85 return -1; 86 87 return LC3_NS(dt, sr); 88 } 89 90 /** 91 * Return the size of frames, from bitrate 92 */ 93 int lc3_frame_bytes(int dt_us, int bitrate) 94 { 95 if (resolve_dt(dt_us) >= LC3_NUM_DT) 96 return -1; 97 98 if (bitrate < LC3_MIN_BITRATE) 99 return LC3_MIN_FRAME_BYTES; 100 101 if (bitrate > LC3_MAX_BITRATE) 102 return LC3_MAX_FRAME_BYTES; 103 104 int nbytes = ((unsigned)bitrate * dt_us) / (1000*1000*8); 105 106 return LC3_CLIP(nbytes, LC3_MIN_FRAME_BYTES, LC3_MAX_FRAME_BYTES); 107 } 108 109 /** 110 * Resolve the bitrate, from the size of frames 111 */ 112 int lc3_resolve_bitrate(int dt_us, int nbytes) 113 { 114 if (resolve_dt(dt_us) >= LC3_NUM_DT) 115 return -1; 116 117 if (nbytes < LC3_MIN_FRAME_BYTES) 118 return LC3_MIN_BITRATE; 119 120 if (nbytes > LC3_MAX_FRAME_BYTES) 121 return LC3_MAX_BITRATE; 122 123 int bitrate = ((unsigned)nbytes * (1000*1000*8) + dt_us/2) / dt_us; 124 125 return LC3_CLIP(bitrate, LC3_MIN_BITRATE, LC3_MAX_BITRATE); 126 } 127 128 /** 129 * Return algorithmic delay, as a number of samples 130 */ 131 int lc3_delay_samples(int dt_us, int sr_hz) 132 { 133 enum lc3_dt dt = resolve_dt(dt_us); 134 enum lc3_srate sr = resolve_sr(sr_hz); 135 136 if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE) 137 return -1; 138 139 return (dt == LC3_DT_7M5 ? 8 : 5) * (LC3_SRATE_KHZ(sr) / 2); 140 } 141 142 143 /* ---------------------------------------------------------------------------- 144 * Encoder 145 * -------------------------------------------------------------------------- */ 146 147 /** 148 * Input PCM Samples from signed 16 bits 149 * encoder Encoder state 150 * pcm, stride Input PCM samples, and count between two consecutives 151 */ 152 static void load_s16( 153 struct lc3_encoder *encoder, const void *_pcm, int stride) 154 { 155 const int16_t *pcm = _pcm; 156 157 enum lc3_dt dt = encoder->dt; 158 enum lc3_srate sr = encoder->sr_pcm; 159 160 int16_t *xt = encoder->xt; 161 float *xs = encoder->xs; 162 int ns = LC3_NS(dt, sr); 163 164 for (int i = 0; i < ns; i++, pcm += stride) 165 xt[i] = *pcm, xs[i] = *pcm; 166 } 167 168 /** 169 * Input PCM Samples from signed 24 bits 170 * encoder Encoder state 171 * pcm, stride Input PCM samples, and count between two consecutives 172 */ 173 static void load_s24( 174 struct lc3_encoder *encoder, const void *_pcm, int stride) 175 { 176 const int32_t *pcm = _pcm; 177 178 enum lc3_dt dt = encoder->dt; 179 enum lc3_srate sr = encoder->sr_pcm; 180 181 int16_t *xt = encoder->xt; 182 float *xs = encoder->xs; 183 int ns = LC3_NS(dt, sr); 184 185 for (int i = 0; i < ns; i++, pcm += stride) { 186 xt[i] = *pcm >> 8; 187 xs[i] = ldexpf(*pcm, -8); 188 } 189 } 190 191 /** 192 * Input PCM Samples from signed 24 bits packed 193 * encoder Encoder state 194 * pcm, stride Input PCM samples, and count between two consecutives 195 */ 196 static void load_s24_3le( 197 struct lc3_encoder *encoder, const void *_pcm, int stride) 198 { 199 const uint8_t *pcm = _pcm; 200 201 enum lc3_dt dt = encoder->dt; 202 enum lc3_srate sr = encoder->sr_pcm; 203 204 int16_t *xt = encoder->xt; 205 float *xs = encoder->xs; 206 int ns = LC3_NS(dt, sr); 207 208 for (int i = 0; i < ns; i++, pcm += 3*stride) { 209 int32_t in = ((uint32_t)pcm[0] << 8) | 210 ((uint32_t)pcm[1] << 16) | 211 ((uint32_t)pcm[2] << 24) ; 212 213 xt[i] = in >> 16; 214 xs[i] = ldexpf(in, -16); 215 } 216 } 217 218 /** 219 * Input PCM Samples from float 32 bits 220 * encoder Encoder state 221 * pcm, stride Input PCM samples, and count between two consecutives 222 */ 223 static void load_float( 224 struct lc3_encoder *encoder, const void *_pcm, int stride) 225 { 226 const float *pcm = _pcm; 227 228 enum lc3_dt dt = encoder->dt; 229 enum lc3_srate sr = encoder->sr_pcm; 230 231 int16_t *xt = encoder->xt; 232 float *xs = encoder->xs; 233 int ns = LC3_NS(dt, sr); 234 235 for (int i = 0; i < ns; i++, pcm += stride) { 236 xs[i] = ldexpf(*pcm, 15); 237 xt[i] = LC3_SAT16((int32_t)xs[i]); 238 } 239 } 240 241 /** 242 * Frame Analysis 243 * encoder Encoder state 244 * nbytes Size in bytes of the frame 245 * side, xq Return frame data 246 */ 247 static void analyze(struct lc3_encoder *encoder, 248 int nbytes, struct side_data *side, uint16_t *xq) 249 { 250 enum lc3_dt dt = encoder->dt; 251 enum lc3_srate sr = encoder->sr; 252 enum lc3_srate sr_pcm = encoder->sr_pcm; 253 int ns = LC3_NS(dt, sr_pcm); 254 int nt = LC3_NT(sr_pcm); 255 256 int16_t *xt = encoder->xt; 257 float *xs = encoder->xs; 258 float *xd = encoder->xd; 259 float *xf = xs; 260 261 /* --- Temporal --- */ 262 263 bool att = lc3_attdet_run(dt, sr_pcm, nbytes, &encoder->attdet, xt); 264 265 side->pitch_present = 266 lc3_ltpf_analyse(dt, sr_pcm, &encoder->ltpf, xt, &side->ltpf); 267 268 memmove(xt - nt, xt + (ns-nt), nt * sizeof(*xt)); 269 270 /* --- Spectral --- */ 271 272 float e[LC3_NUM_BANDS]; 273 274 lc3_mdct_forward(dt, sr_pcm, sr, xs, xd, xf); 275 276 bool nn_flag = lc3_energy_compute(dt, sr, xf, e); 277 if (nn_flag) 278 lc3_ltpf_disable(&side->ltpf); 279 280 side->bw = lc3_bwdet_run(dt, sr, e); 281 282 lc3_sns_analyze(dt, sr, e, att, &side->sns, xf, xf); 283 284 lc3_tns_analyze(dt, side->bw, nn_flag, nbytes, &side->tns, xf); 285 286 lc3_spec_analyze(dt, sr, 287 nbytes, side->pitch_present, &side->tns, 288 &encoder->spec, xf, xq, &side->spec); 289 } 290 291 /** 292 * Encode bitstream 293 * encoder Encoder state 294 * side, xq The frame data 295 * nbytes Target size of the frame (20 to 400) 296 * buffer Output bitstream buffer of `nbytes` size 297 */ 298 static void encode(struct lc3_encoder *encoder, 299 const struct side_data *side, uint16_t *xq, int nbytes, void *buffer) 300 { 301 enum lc3_dt dt = encoder->dt; 302 enum lc3_srate sr = encoder->sr; 303 enum lc3_bandwidth bw = side->bw; 304 float *xf = encoder->xs; 305 306 lc3_bits_t bits; 307 308 lc3_setup_bits(&bits, LC3_BITS_MODE_WRITE, buffer, nbytes); 309 310 lc3_bwdet_put_bw(&bits, sr, bw); 311 312 lc3_spec_put_side(&bits, dt, sr, &side->spec); 313 314 lc3_tns_put_data(&bits, &side->tns); 315 316 lc3_put_bit(&bits, side->pitch_present); 317 318 lc3_sns_put_data(&bits, &side->sns); 319 320 if (side->pitch_present) 321 lc3_ltpf_put_data(&bits, &side->ltpf); 322 323 lc3_spec_encode(&bits, 324 dt, sr, bw, nbytes, xq, &side->spec, xf); 325 326 lc3_flush_bits(&bits); 327 } 328 329 /** 330 * Return size needed for an encoder 331 */ 332 unsigned lc3_encoder_size(int dt_us, int sr_hz) 333 { 334 if (resolve_dt(dt_us) >= LC3_NUM_DT || 335 resolve_sr(sr_hz) >= LC3_NUM_SRATE) 336 return 0; 337 338 return sizeof(struct lc3_encoder) + 339 (LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float); 340 } 341 342 /** 343 * Setup encoder 344 */ 345 struct lc3_encoder *lc3_setup_encoder( 346 int dt_us, int sr_hz, int sr_pcm_hz, void *mem) 347 { 348 if (sr_pcm_hz <= 0) 349 sr_pcm_hz = sr_hz; 350 351 enum lc3_dt dt = resolve_dt(dt_us); 352 enum lc3_srate sr = resolve_sr(sr_hz); 353 enum lc3_srate sr_pcm = resolve_sr(sr_pcm_hz); 354 355 if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem) 356 return NULL; 357 358 struct lc3_encoder *encoder = mem; 359 int ns = LC3_NS(dt, sr_pcm); 360 int nt = LC3_NT(sr_pcm); 361 362 *encoder = (struct lc3_encoder){ 363 .dt = dt, .sr = sr, 364 .sr_pcm = sr_pcm, 365 366 .xt = (int16_t *)encoder->s + nt, 367 .xs = encoder->s + (nt+ns)/2, 368 .xd = encoder->s + (nt+ns)/2 + ns, 369 }; 370 371 memset(encoder->s, 0, 372 LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float)); 373 374 return encoder; 375 } 376 377 /** 378 * Encode a frame 379 */ 380 int lc3_encode(struct lc3_encoder *encoder, enum lc3_pcm_format fmt, 381 const void *pcm, int stride, int nbytes, void *out) 382 { 383 static void (* const load[])(struct lc3_encoder *, const void *, int) = { 384 [LC3_PCM_FORMAT_S16 ] = load_s16, 385 [LC3_PCM_FORMAT_S24 ] = load_s24, 386 [LC3_PCM_FORMAT_S24_3LE] = load_s24_3le, 387 [LC3_PCM_FORMAT_FLOAT ] = load_float, 388 }; 389 390 /* --- Check parameters --- */ 391 392 if (!encoder || nbytes < LC3_MIN_FRAME_BYTES 393 || nbytes > LC3_MAX_FRAME_BYTES) 394 return -1; 395 396 /* --- Processing --- */ 397 398 struct side_data side; 399 uint16_t xq[LC3_NE(encoder->dt, encoder->sr)]; 400 401 load[fmt](encoder, pcm, stride); 402 403 analyze(encoder, nbytes, &side, xq); 404 405 encode(encoder, &side, xq, nbytes, out); 406 407 return 0; 408 } 409 410 411 /* ---------------------------------------------------------------------------- 412 * Decoder 413 * -------------------------------------------------------------------------- */ 414 415 /** 416 * Output PCM Samples to signed 16 bits 417 * decoder Decoder state 418 * pcm, stride Output PCM samples, and count between two consecutives 419 */ 420 static void store_s16( 421 struct lc3_decoder *decoder, void *_pcm, int stride) 422 { 423 int16_t *pcm = _pcm; 424 425 enum lc3_dt dt = decoder->dt; 426 enum lc3_srate sr = decoder->sr_pcm; 427 428 float *xs = decoder->xs; 429 int ns = LC3_NS(dt, sr); 430 431 for ( ; ns > 0; ns--, xs++, pcm += stride) { 432 int32_t s = *xs >= 0 ? (int)(*xs + 0.5f) : (int)(*xs - 0.5f); 433 *pcm = LC3_SAT16(s); 434 } 435 } 436 437 /** 438 * Output PCM Samples to signed 24 bits 439 * decoder Decoder state 440 * pcm, stride Output PCM samples, and count between two consecutives 441 */ 442 static void store_s24( 443 struct lc3_decoder *decoder, void *_pcm, int stride) 444 { 445 int32_t *pcm = _pcm; 446 447 enum lc3_dt dt = decoder->dt; 448 enum lc3_srate sr = decoder->sr_pcm; 449 450 float *xs = decoder->xs; 451 int ns = LC3_NS(dt, sr); 452 453 for ( ; ns > 0; ns--, xs++, pcm += stride) { 454 int32_t s = *xs >= 0 ? (int32_t)(ldexpf(*xs, 8) + 0.5f) 455 : (int32_t)(ldexpf(*xs, 8) - 0.5f); 456 *pcm = LC3_SAT24(s); 457 } 458 } 459 460 /** 461 * Output PCM Samples to signed 24 bits packed 462 * decoder Decoder state 463 * pcm, stride Output PCM samples, and count between two consecutives 464 */ 465 static void store_s24_3le( 466 struct lc3_decoder *decoder, void *_pcm, int stride) 467 { 468 uint8_t *pcm = _pcm; 469 470 enum lc3_dt dt = decoder->dt; 471 enum lc3_srate sr = decoder->sr_pcm; 472 473 float *xs = decoder->xs; 474 int ns = LC3_NS(dt, sr); 475 476 for ( ; ns > 0; ns--, xs++, pcm += 3*stride) { 477 int32_t s = *xs >= 0 ? (int32_t)(ldexpf(*xs, 8) + 0.5f) 478 : (int32_t)(ldexpf(*xs, 8) - 0.5f); 479 480 s = LC3_SAT24(s); 481 pcm[0] = (s >> 0) & 0xff; 482 pcm[1] = (s >> 8) & 0xff; 483 pcm[2] = (s >> 16) & 0xff; 484 } 485 } 486 487 /** 488 * Output PCM Samples to float 32 bits 489 * decoder Decoder state 490 * pcm, stride Output PCM samples, and count between two consecutives 491 */ 492 static void store_float( 493 struct lc3_decoder *decoder, void *_pcm, int stride) 494 { 495 float *pcm = _pcm; 496 497 enum lc3_dt dt = decoder->dt; 498 enum lc3_srate sr = decoder->sr_pcm; 499 500 float *xs = decoder->xs; 501 int ns = LC3_NS(dt, sr); 502 503 for ( ; ns > 0; ns--, xs++, pcm += stride) { 504 float s = ldexpf(*xs, -15); 505 *pcm = fminf(fmaxf(s, -1.f), 1.f); 506 } 507 } 508 509 /** 510 * Decode bitstream 511 * decoder Decoder state 512 * data, nbytes Input bitstream buffer 513 * side Return the side data 514 * return 0: Ok < 0: Bitsream error detected 515 */ 516 static int decode(struct lc3_decoder *decoder, 517 const void *data, int nbytes, struct side_data *side) 518 { 519 enum lc3_dt dt = decoder->dt; 520 enum lc3_srate sr = decoder->sr; 521 522 float *xf = decoder->xs; 523 int ns = LC3_NS(dt, sr); 524 int ne = LC3_NE(dt, sr); 525 526 lc3_bits_t bits; 527 int ret = 0; 528 529 lc3_setup_bits(&bits, LC3_BITS_MODE_READ, (void *)data, nbytes); 530 531 if ((ret = lc3_bwdet_get_bw(&bits, sr, &side->bw)) < 0) 532 return ret; 533 534 if ((ret = lc3_spec_get_side(&bits, dt, sr, &side->spec)) < 0) 535 return ret; 536 537 lc3_tns_get_data(&bits, dt, side->bw, nbytes, &side->tns); 538 539 side->pitch_present = lc3_get_bit(&bits); 540 541 if ((ret = lc3_sns_get_data(&bits, &side->sns)) < 0) 542 return ret; 543 544 if (side->pitch_present) 545 lc3_ltpf_get_data(&bits, &side->ltpf); 546 547 if ((ret = lc3_spec_decode(&bits, dt, sr, 548 side->bw, nbytes, &side->spec, xf)) < 0) 549 return ret; 550 551 memset(xf + ne, 0, (ns - ne) * sizeof(float)); 552 553 return lc3_check_bits(&bits); 554 } 555 556 /** 557 * Frame synthesis 558 * decoder Decoder state 559 * side Frame data, NULL performs PLC 560 * nbytes Size in bytes of the frame 561 */ 562 static void synthesize(struct lc3_decoder *decoder, 563 const struct side_data *side, int nbytes) 564 { 565 enum lc3_dt dt = decoder->dt; 566 enum lc3_srate sr = decoder->sr; 567 enum lc3_srate sr_pcm = decoder->sr_pcm; 568 569 float *xf = decoder->xs; 570 int ns = LC3_NS(dt, sr_pcm); 571 int ne = LC3_NE(dt, sr); 572 573 float *xg = decoder->xg; 574 float *xd = decoder->xd; 575 float *xs = xf; 576 577 if (side) { 578 enum lc3_bandwidth bw = side->bw; 579 580 lc3_plc_suspend(&decoder->plc); 581 582 lc3_tns_synthesize(dt, bw, &side->tns, xf); 583 584 lc3_sns_synthesize(dt, sr, &side->sns, xf, xg); 585 586 lc3_mdct_inverse(dt, sr_pcm, sr, xg, xd, xs); 587 588 } else { 589 lc3_plc_synthesize(dt, sr, &decoder->plc, xg, xf); 590 591 memset(xf + ne, 0, (ns - ne) * sizeof(float)); 592 593 lc3_mdct_inverse(dt, sr_pcm, sr, xf, xd, xs); 594 } 595 596 lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf, 597 side && side->pitch_present ? &side->ltpf : NULL, decoder->xh, xs); 598 } 599 600 /** 601 * Update decoder state on decoding completion 602 * decoder Decoder state 603 */ 604 static void complete(struct lc3_decoder *decoder) 605 { 606 enum lc3_dt dt = decoder->dt; 607 enum lc3_srate sr_pcm = decoder->sr_pcm; 608 int nh = LC3_NH(dt, sr_pcm); 609 int ns = LC3_NS(dt, sr_pcm); 610 611 decoder->xs = decoder->xs - decoder->xh < nh - ns ? 612 decoder->xs + ns : decoder->xh; 613 } 614 615 /** 616 * Return size needed for a decoder 617 */ 618 unsigned lc3_decoder_size(int dt_us, int sr_hz) 619 { 620 if (resolve_dt(dt_us) >= LC3_NUM_DT || 621 resolve_sr(sr_hz) >= LC3_NUM_SRATE) 622 return 0; 623 624 return sizeof(struct lc3_decoder) + 625 (LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float); 626 } 627 628 /** 629 * Setup decoder 630 */ 631 struct lc3_decoder *lc3_setup_decoder( 632 int dt_us, int sr_hz, int sr_pcm_hz, void *mem) 633 { 634 if (sr_pcm_hz <= 0) 635 sr_pcm_hz = sr_hz; 636 637 enum lc3_dt dt = resolve_dt(dt_us); 638 enum lc3_srate sr = resolve_sr(sr_hz); 639 enum lc3_srate sr_pcm = resolve_sr(sr_pcm_hz); 640 641 if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem) 642 return NULL; 643 644 struct lc3_decoder *decoder = mem; 645 int nh = LC3_NH(dt, sr_pcm); 646 int ns = LC3_NS(dt, sr_pcm); 647 int nd = LC3_ND(dt, sr_pcm); 648 649 *decoder = (struct lc3_decoder){ 650 .dt = dt, .sr = sr, 651 .sr_pcm = sr_pcm, 652 653 .xh = decoder->s, 654 .xs = decoder->s + nh-ns, 655 .xd = decoder->s + nh, 656 .xg = decoder->s + nh+nd, 657 }; 658 659 lc3_plc_reset(&decoder->plc); 660 661 memset(decoder->s, 0, 662 LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float)); 663 664 return decoder; 665 } 666 667 /** 668 * Decode a frame 669 */ 670 int lc3_decode(struct lc3_decoder *decoder, const void *in, int nbytes, 671 enum lc3_pcm_format fmt, void *pcm, int stride) 672 { 673 static void (* const store[])(struct lc3_decoder *, void *, int) = { 674 [LC3_PCM_FORMAT_S16 ] = store_s16, 675 [LC3_PCM_FORMAT_S24 ] = store_s24, 676 [LC3_PCM_FORMAT_S24_3LE] = store_s24_3le, 677 [LC3_PCM_FORMAT_FLOAT ] = store_float, 678 }; 679 680 /* --- Check parameters --- */ 681 682 if (!decoder) 683 return -1; 684 685 if (in && (nbytes < LC3_MIN_FRAME_BYTES || 686 nbytes > LC3_MAX_FRAME_BYTES )) 687 return -1; 688 689 /* --- Processing --- */ 690 691 struct side_data side; 692 693 int ret = !in || (decode(decoder, in, nbytes, &side) < 0); 694 695 synthesize(decoder, ret ? NULL : &side, nbytes); 696 697 store[fmt](decoder, pcm, stride); 698 699 complete(decoder); 700 701 return ret; 702 } 703