├── zephyr └── module.yml ├── src ├── energy.h ├── attdet.h ├── plc.h ├── plc.c ├── mdct.h ├── energy.c ├── bwdet.h ├── tables.h ├── common.h ├── attdet.c ├── tns.h ├── sns.h ├── ltpf.h ├── spec.h ├── bwdet.c ├── bits.h ├── bits.c ├── tns.c ├── lc3.c ├── mdct.c ├── ltpf.c ├── spec.c └── sns.c ├── Android.bp ├── fuzzer └── liblc3_fuzzer.cpp └── include ├── lc3_private.h └── lc3.h /zephyr/module.yml: -------------------------------------------------------------------------------- 1 | build: 2 | cmake-ext: True 3 | kconfig-ext: True 4 | -------------------------------------------------------------------------------- /src/energy.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Energy estimation per band 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_ENERGY_H 27 | #define __LC3_ENERGY_H 28 | 29 | #include "common.h" 30 | 31 | 32 | /** 33 | * Energy estimation per band 34 | * dt, sr Duration and samplerate of the frame 35 | * x Input MDCT coefficient 36 | * e Energy estimation per bands 37 | * return True when high energy detected near Nyquist frequency 38 | */ 39 | bool lc3_energy_compute( 40 | enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e); 41 | 42 | 43 | #endif /* __LC3_ENERGY_H */ 44 | -------------------------------------------------------------------------------- /Android.bp: -------------------------------------------------------------------------------- 1 | package { 2 | // See: http://go/android-license-faq 3 | // A large-scale-change added 'default_applicable_licenses' to import 4 | // all of the 'license_kinds' from "system_bt_license" 5 | // to get the below license kinds: 6 | // SPDX-license-identifier-Apache-2.0 7 | default_applicable_licenses: ["system_bt_license"], 8 | } 9 | 10 | cc_library_static { 11 | name: "liblc3", 12 | host_supported: true, 13 | apex_available: [ 14 | 15 | "//apex_available:platform", 16 | "com.android.bluetooth" 17 | ], 18 | defaults: ["fluoride_defaults"], 19 | srcs: [ 20 | "src/*.c", 21 | ], 22 | cflags: [ 23 | "-O3", 24 | "-ffast-math", 25 | "-Werror", 26 | "-Wmissing-braces", 27 | "-Wno-unused-parameter", 28 | "-Wno-#warnings", 29 | "-Wuninitialized", 30 | "-Wno-self-assign", 31 | "-Wno-implicit-fallthrough", 32 | ], 33 | target: { 34 | android: { 35 | sanitize: { 36 | misc_undefined:[ 37 | "unsigned-integer-overflow", 38 | "signed-integer-overflow", 39 | "bounds", 40 | ], 41 | cfi: true, 42 | }, 43 | }, 44 | }, 45 | export_include_dirs: [ 46 | "include", 47 | ], 48 | } 49 | 50 | cc_fuzz { 51 | name: "liblc3_fuzzer", 52 | 53 | srcs: [ 54 | "fuzzer/liblc3_fuzzer.cpp", 55 | ], 56 | 57 | static_libs: [ 58 | "liblc3", 59 | ], 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/attdet.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Time domain attack detector 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_ATTDET_H 27 | #define __LC3_ATTDET_H 28 | 29 | #include "common.h" 30 | 31 | 32 | /** 33 | * Time domain attack detector 34 | * dt, sr Duration and samplerate of the frame 35 | * nbytes Size in bytes of the frame 36 | * attdet Context of the Attack Detector 37 | * x [-6..-1] Previous, [0..ns-1] Current samples 38 | * return 1: Attack detected 0: Otherwise 39 | */ 40 | bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr, 41 | int nbytes, lc3_attdet_analysis_t *attdet, const float *x); 42 | 43 | 44 | #endif /* __LC3_ATTDET_H */ 45 | -------------------------------------------------------------------------------- /src/plc.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Packet Loss Concealment 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_PLC_H 27 | #define __LC3_PLC_H 28 | 29 | #include "common.h" 30 | 31 | 32 | /** 33 | * Reset PLC state 34 | * plc PLC State to reset 35 | */ 36 | void lc3_plc_reset(lc3_plc_state_t *plc); 37 | 38 | /** 39 | * Suspend PLC synthesis (Error-free frame decoded) 40 | * plc PLC State 41 | */ 42 | void lc3_plc_suspend(lc3_plc_state_t *plc); 43 | 44 | /** 45 | * Synthesis of a PLC frame 46 | * dt, sr Duration and samplerate of the frame 47 | * plc PLC State 48 | * x Last good spectral coefficients 49 | * y Return emulated ones 50 | * 51 | * `x` and `y` can be the same buffer 52 | */ 53 | void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr, 54 | lc3_plc_state_t *plc, const float *x, float *y); 55 | 56 | 57 | #endif /* __LC3_PLC_H */ 58 | -------------------------------------------------------------------------------- /src/plc.c: -------------------------------------------------------------------------------- 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 "plc.h" 20 | 21 | 22 | /** 23 | * Reset Packet Loss Concealment state 24 | */ 25 | void lc3_plc_reset(struct lc3_plc_state *plc) 26 | { 27 | plc->seed = 24607; 28 | lc3_plc_suspend(plc); 29 | } 30 | 31 | /** 32 | * Suspend PLC execution (Good frame received) 33 | */ 34 | void lc3_plc_suspend(struct lc3_plc_state *plc) 35 | { 36 | plc->count = 1; 37 | plc->alpha = 1.0f; 38 | } 39 | 40 | /** 41 | * Synthesis of a PLC frame 42 | */ 43 | void lc3_plc_synthesize(enum lc3_dt dt, enum lc3_srate sr, 44 | struct lc3_plc_state *plc, const float *x, float *y) 45 | { 46 | uint16_t seed = plc->seed; 47 | float alpha = plc->alpha; 48 | int ne = LC3_NE(dt, sr); 49 | 50 | alpha *= (plc->count < 4 ? 1.0f : 51 | plc->count < 8 ? 0.9f : 0.85f); 52 | 53 | for (int i = 0; i < ne; i++) { 54 | seed = (16831 + seed * 12821) & 0xffff; 55 | y[i] = alpha * (seed & 0x8000 ? -x[i] : x[i]); 56 | } 57 | 58 | plc->seed = seed; 59 | plc->alpha = alpha; 60 | plc->count++; 61 | } 62 | -------------------------------------------------------------------------------- /src/mdct.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Compute LD-MDCT (Low Delay Modified Discret Cosinus Transform) 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_MDCT_H 27 | #define __LC3_MDCT_H 28 | 29 | #include "common.h" 30 | 31 | 32 | /** 33 | * Forward MDCT transformation 34 | * dt, sr Duration and samplerate (size of the transform) 35 | * sr_dst Samplerate destination, scale transforam accordingly 36 | * x [-nd..-1] Previous, [0..ns-1] Current samples 37 | * y Output `ns` frequency coefficients 38 | * 39 | * The number of previous samples `nd` accessed on `x` is : 40 | * nd: `ns` * 23/30 for 7.5ms frame duration 41 | * nd: `ns` * 5/ 8 for 10ms frame duration 42 | */ 43 | void lc3_mdct_forward(enum lc3_dt dt, enum lc3_srate sr, 44 | enum lc3_srate sr_dst, const float *x, float *y); 45 | 46 | /** 47 | * Inverse MDCT transformation 48 | * dt, sr Duration and samplerate (size of the transform) 49 | * sr_src Samplerate source, scale transforam accordingly 50 | * x, d Frequency coefficients and delayed buffer 51 | * y, d Output `ns` samples and `nd` delayed ones 52 | * 53 | * `x` and `y` can be the same buffer 54 | */ 55 | void lc3_mdct_inverse(enum lc3_dt dt, enum lc3_srate sr, 56 | enum lc3_srate sr_src, const float *x, float *d, float *y); 57 | 58 | 59 | #endif /* __LC3_MDCT_H */ 60 | -------------------------------------------------------------------------------- /src/energy.c: -------------------------------------------------------------------------------- 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 "energy.h" 20 | #include "tables.h" 21 | 22 | 23 | /** 24 | * Energy estimation per band 25 | */ 26 | bool lc3_energy_compute( 27 | enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e) 28 | { 29 | static const int n1_table[LC3_NUM_DT][LC3_NUM_SRATE] = { 30 | [LC3_DT_7M5] = { 56, 34, 27, 24, 22 }, 31 | [LC3_DT_10M] = { 49, 28, 23, 20, 18 }, 32 | }; 33 | 34 | /* First bands are 1 coefficient width */ 35 | 36 | int n1 = n1_table[dt][sr]; 37 | float e_sum[2] = { 0, 0 }; 38 | int iband; 39 | 40 | for (iband = 0; iband < n1; iband++) { 41 | *e = x[iband] * x[iband]; 42 | e_sum[0] += *(e++); 43 | } 44 | 45 | /* Mean the square of coefficients within each band, 46 | * note that 7.5ms 8KHz frame has more bands than samples */ 47 | 48 | int nb = LC3_MIN(LC3_NUM_BANDS, LC3_NS(dt, sr)); 49 | int iband_h = nb - 2*(2 - dt); 50 | const int *lim = lc3_band_lim[dt][sr]; 51 | 52 | for (int i = lim[iband]; iband < nb; iband++) { 53 | int ie = lim[iband+1]; 54 | int n = ie - i; 55 | 56 | float sx2 = x[i] * x[i]; 57 | for (i++; i < ie; i++) 58 | sx2 += x[i] * x[i]; 59 | 60 | *e = sx2 / n; 61 | e_sum[iband >= iband_h] += *(e++); 62 | } 63 | 64 | for (; iband < LC3_NUM_BANDS; iband++) 65 | *(e++) = 0; 66 | 67 | /* Return the near nyquist flag */ 68 | 69 | return e_sum[1] > 30 * e_sum[0]; 70 | } 71 | -------------------------------------------------------------------------------- /src/bwdet.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Bandwidth detector 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_BWDET_H 27 | #define __LC3_BWDET_H 28 | 29 | #include "common.h" 30 | #include "bits.h" 31 | 32 | 33 | /** 34 | * Bandwidth detector (cf. 3.3.5) 35 | * dt, sr Duration and samplerate of the frame 36 | * e Energy estimation per bands 37 | * return Return detected bandwitdth 38 | */ 39 | enum lc3_bandwidth lc3_bwdet_run( 40 | enum lc3_dt dt, enum lc3_srate sr, const float *e); 41 | 42 | /** 43 | * Return number of bits coding the bandwidth value 44 | * sr Samplerate of the frame 45 | * return Number of bits coding the bandwidth value 46 | */ 47 | int lc3_bwdet_get_nbits(enum lc3_srate sr); 48 | 49 | /** 50 | * Put bandwidth indication 51 | * bits Bitstream context 52 | * sr Samplerate of the frame 53 | * bw Bandwidth detected 54 | */ 55 | void lc3_bwdet_put_bw(lc3_bits_t *bits, 56 | enum lc3_srate sr, enum lc3_bandwidth bw); 57 | 58 | /** 59 | * Get bandwidth indication 60 | * bits Bitstream context 61 | * sr Samplerate of the frame 62 | * bw Return bandwidth indication 63 | * return 0: Ok -1: Invalid bandwidth indication 64 | */ 65 | int lc3_bwdet_get_bw(lc3_bits_t *bits, 66 | enum lc3_srate sr, enum lc3_bandwidth *bw); 67 | 68 | 69 | #endif /* __LC3_BWDET_H */ 70 | -------------------------------------------------------------------------------- /src/tables.h: -------------------------------------------------------------------------------- 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 | #ifndef __LC3_TABLES_H 20 | #define __LC3_TABLES_H 21 | 22 | #include "common.h" 23 | #include "bits.h" 24 | 25 | 26 | /** 27 | * MDCT Twiddles and window coefficients 28 | */ 29 | 30 | struct lc3_fft_bf3_twiddles { int n3; const struct lc3_complex (*t)[2]; }; 31 | struct lc3_fft_bf2_twiddles { int n2; const struct lc3_complex *t; }; 32 | struct lc3_mdct_rot_def { int n4; const struct lc3_complex *w; }; 33 | 34 | extern const struct lc3_fft_bf3_twiddles *lc3_fft_twiddles_bf3[]; 35 | extern const struct lc3_fft_bf2_twiddles *lc3_fft_twiddles_bf2[][3]; 36 | extern const struct lc3_mdct_rot_def *lc3_mdct_rot[LC3_NUM_DT][LC3_NUM_SRATE]; 37 | 38 | extern const float *lc3_mdct_win[LC3_NUM_DT][LC3_NUM_SRATE]; 39 | 40 | 41 | /** 42 | * Limits of bands 43 | */ 44 | 45 | #define LC3_NUM_BANDS 64 46 | 47 | extern const int lc3_band_lim[LC3_NUM_DT][LC3_NUM_SRATE][LC3_NUM_BANDS+1]; 48 | 49 | 50 | /** 51 | * SNS Quantization 52 | */ 53 | 54 | extern const float lc3_sns_lfcb[32][8]; 55 | extern const float lc3_sns_hfcb[32][8]; 56 | 57 | struct lc3_sns_vq_gains { 58 | int count; const float *v; 59 | }; 60 | 61 | extern const struct lc3_sns_vq_gains lc3_sns_vq_gains[4]; 62 | 63 | extern const int32_t lc3_sns_mpvq_offsets[][11]; 64 | 65 | 66 | /** 67 | * TNS Arithmetic Coding 68 | */ 69 | 70 | extern const struct lc3_ac_model lc3_tns_order_models[]; 71 | extern const uint16_t lc3_tns_order_bits[][8]; 72 | 73 | extern const struct lc3_ac_model lc3_tns_coeffs_models[]; 74 | extern const uint16_t lc3_tns_coeffs_bits[][17]; 75 | 76 | 77 | /** 78 | * Long Term Postfilter 79 | */ 80 | 81 | extern const float lc3_ltpf_h12k8[240]; 82 | 83 | extern const float *lc3_ltpf_cnum[LC3_NUM_SRATE][4]; 84 | extern const float *lc3_ltpf_cden[LC3_NUM_SRATE][4]; 85 | 86 | 87 | /** 88 | * Spectral Data Arithmetic Coding 89 | */ 90 | 91 | extern const uint8_t lc3_spectrum_lookup[2][2][256][4]; 92 | extern const struct lc3_ac_model lc3_spectrum_models[]; 93 | extern const uint16_t lc3_spectrum_bits[][17]; 94 | 95 | 96 | #endif /* __LC3_TABLES_H */ 97 | -------------------------------------------------------------------------------- /src/common.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Common constants and types 21 | */ 22 | 23 | #ifndef __LC3_COMMON_H 24 | #define __LC3_COMMON_H 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | /** 34 | * Macros 35 | * MIN/MAX Minimum and maximum between 2 values 36 | * CLIP Clip a value between low and high limits 37 | * ABS Return the absolute value 38 | */ 39 | 40 | #define LC3_MIN(a, b) ( (a) < (b) ? (a) : (b) ) 41 | #define LC3_MAX(a, b) ( (a) > (b) ? (a) : (b) ) 42 | #define LC3_CLIP(v, min, max) LC3_MIN(LC3_MAX(v, min), max) 43 | 44 | #define LC3_ABS(n) ( (n) < 0 ? -(n) : (n) ) 45 | 46 | 47 | /** 48 | * Convert `dt` in us and `sr` in KHz 49 | */ 50 | 51 | #define LC3_DT_US(dt) \ 52 | ( (3 + (dt)) * 2500 ) 53 | 54 | #define LC3_SRATE_KHZ(sr) \ 55 | ( (1 + (sr) + ((sr) == LC3_SRATE_48K)) * 8 ) 56 | 57 | 58 | /** 59 | * Return number of samples, delayed samples and 60 | * encoded spectrum coefficients within a frame 61 | * For decoding, add number of samples of 18 ms history 62 | */ 63 | 64 | #define LC3_NS(dt, sr) \ 65 | ( 20 * (3 + (dt)) * (1 + (sr) + ((sr) == LC3_SRATE_48K)) ) 66 | 67 | #define LC3_ND(dt, sr) \ 68 | ( (dt) == LC3_DT_7M5 ? 23 * LC3_NS(dt, sr) / 30 \ 69 | : 5 * LC3_NS(dt, sr) / 8 ) 70 | #define LC3_NE(dt, sr) \ 71 | ( 20 * (3 + (dt)) * (1 + (sr)) ) 72 | 73 | #define LC3_MAX_NE \ 74 | LC3_NE(LC3_DT_10M, LC3_SRATE_48K) 75 | 76 | #define LC3_NH(sr) \ 77 | (18 * LC3_SRATE_KHZ(sr)) 78 | 79 | 80 | /** 81 | * Bandwidth, mapped to Nyquist frequency of samplerates 82 | */ 83 | 84 | enum lc3_bandwidth { 85 | LC3_BANDWIDTH_NB = LC3_SRATE_8K, 86 | LC3_BANDWIDTH_WB = LC3_SRATE_16K, 87 | LC3_BANDWIDTH_SSWB = LC3_SRATE_24K, 88 | LC3_BANDWIDTH_SWB = LC3_SRATE_32K, 89 | LC3_BANDWIDTH_FB = LC3_SRATE_48K, 90 | 91 | LC3_NUM_BANDWIDTH, 92 | }; 93 | 94 | 95 | /** 96 | * Complex floating point number 97 | */ 98 | 99 | struct lc3_complex 100 | { 101 | float re, im; 102 | }; 103 | 104 | 105 | #endif /* __LC3_COMMON_H */ 106 | -------------------------------------------------------------------------------- /src/attdet.c: -------------------------------------------------------------------------------- 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 "attdet.h" 20 | 21 | 22 | /** 23 | * Time domain attack detector 24 | */ 25 | bool lc3_attdet_run(enum lc3_dt dt, enum lc3_srate sr, 26 | int nbytes, struct lc3_attdet_analysis *attdet, const float *x) 27 | { 28 | /* --- Check enabling --- */ 29 | 30 | const int nbytes_ranges[LC3_NUM_DT][LC3_NUM_SRATE - LC3_SRATE_32K][2] = { 31 | [LC3_DT_7M5] = { { 61, 149 }, { 75, 149 } }, 32 | [LC3_DT_10M] = { { 81, INT_MAX }, { 100, INT_MAX } }, 33 | }; 34 | 35 | if (sr < LC3_SRATE_32K || 36 | nbytes < nbytes_ranges[dt][sr - LC3_SRATE_32K][0] || 37 | nbytes > nbytes_ranges[dt][sr - LC3_SRATE_32K][1] ) 38 | return 0; 39 | 40 | /* --- Filtering & Energy calculation --- */ 41 | 42 | int nblk = 4 - (dt == LC3_DT_7M5); 43 | float e[4]; 44 | 45 | for (int i = 0; i < nblk; i++) { 46 | e[i] = 0; 47 | 48 | if (sr == LC3_SRATE_32K) { 49 | float xn2 = x[-4] + x[-3]; 50 | float xn1 = x[-2] + x[-1]; 51 | float xn, xf; 52 | 53 | for (int j = 0; j < 40; j++, x += 2, xn2 = xn1, xn1 = xn) { 54 | xn = x[0] + x[1]; 55 | xf = 0.375 * xn - 0.5 * xn1 + 0.125 * xn2; 56 | e[i] += xf * xf; 57 | } 58 | } 59 | 60 | else { 61 | float xn2 = x[-6] + x[-5] + x[-4]; 62 | float xn1 = x[-3] + x[-2] + x[-1]; 63 | float xn, xf; 64 | 65 | for (int j = 0; j < 40; j++, x += 3, xn2 = xn1, xn1 = xn) { 66 | xn = x[0] + x[1] + x[2]; 67 | xf = 0.375 * xn - 0.5 * xn1 + 0.125 * xn2; 68 | e[i] += xf * xf; 69 | } 70 | } 71 | } 72 | 73 | /* --- Attack detection --- 74 | * The attack block `p_att` is defined as the normative value + 1, 75 | * in such way, it will be initialized to 0 */ 76 | 77 | int p_att = 0; 78 | float a[4]; 79 | 80 | for (int i = 0; i < nblk; i++) { 81 | a[i] = fmaxf(0.25 * attdet->an1, attdet->en1); 82 | attdet->en1 = e[i], attdet->an1 = a[i]; 83 | 84 | if (e[i] > 8.5 * a[i]) 85 | p_att = i + 1; 86 | } 87 | 88 | int att = attdet->p_att >= 1 + (nblk >> 1) || p_att > 0; 89 | attdet->p_att = p_att; 90 | 91 | return att; 92 | } 93 | -------------------------------------------------------------------------------- /src/tns.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Temporal Noise Shaping 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_TNS_H 27 | #define __LC3_TNS_H 28 | 29 | #include "common.h" 30 | #include "bits.h" 31 | 32 | 33 | /** 34 | * Bitstream data 35 | */ 36 | 37 | typedef struct lc3_tns_data { 38 | int nfilters; 39 | bool lpc_weighting; 40 | int rc_order[2]; 41 | int rc[2][8]; 42 | } lc3_tns_data_t; 43 | 44 | 45 | /* ---------------------------------------------------------------------------- 46 | * Encoding 47 | * -------------------------------------------------------------------------- */ 48 | 49 | /** 50 | * TNS analysis 51 | * dt, bw Duration and bandwidth of the frame 52 | * nn_flag True when high energy detected near Nyquist frequency 53 | * nbytes Size in bytes of the frame 54 | * data Return bitstream data 55 | * x Spectral coefficients, filtered as output 56 | */ 57 | void lc3_tns_analyze(enum lc3_dt dt, enum lc3_bandwidth bw, 58 | bool nn_flag, int nbytes, lc3_tns_data_t *data, float *x); 59 | 60 | /** 61 | * Return number of bits coding the data 62 | * data Bitstream data 63 | * return Bit consumption 64 | */ 65 | int lc3_tns_get_nbits(const lc3_tns_data_t *data); 66 | 67 | /** 68 | * Put bitstream data 69 | * bits Bitstream context 70 | * data Bitstream data 71 | */ 72 | void lc3_tns_put_data(lc3_bits_t *bits, const lc3_tns_data_t *data); 73 | 74 | 75 | /* ---------------------------------------------------------------------------- 76 | * Decoding 77 | * -------------------------------------------------------------------------- */ 78 | 79 | /** 80 | * Get bitstream data 81 | * bits Bitstream context 82 | * dt, bw Duration and bandwidth of the frame 83 | * nbytes Size in bytes of the frame 84 | * data Bitstream data 85 | */ 86 | void lc3_tns_get_data(lc3_bits_t *bits, 87 | enum lc3_dt dt, enum lc3_bandwidth bw, int nbytes, lc3_tns_data_t *data); 88 | 89 | /** 90 | * TNS synthesis 91 | * dt, bw Duration and bandwidth of the frame 92 | * data Bitstream data 93 | * x Spectral coefficients, filtered as output 94 | */ 95 | void lc3_tns_synthesize(enum lc3_dt dt, enum lc3_bandwidth bw, 96 | const lc3_tns_data_t *data, float *x); 97 | 98 | 99 | #endif /* __LC3_TNS_H */ 100 | -------------------------------------------------------------------------------- /src/sns.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Spectral Noise Shaping 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_SNS_H 27 | #define __LC3_SNS_H 28 | 29 | #include "common.h" 30 | #include "bits.h" 31 | 32 | 33 | /** 34 | * Bitstream data 35 | */ 36 | 37 | typedef struct lc3_sns_data { 38 | int lfcb, hfcb; 39 | int shape, gain; 40 | int idx_a, idx_b; 41 | bool ls_a, ls_b; 42 | } lc3_sns_data_t; 43 | 44 | 45 | /* ---------------------------------------------------------------------------- 46 | * Encoding 47 | * -------------------------------------------------------------------------- */ 48 | 49 | /** 50 | * SNS analysis 51 | * dt, sr Duration and samplerate of the frame 52 | * eb Energy estimation per bands, and count of bands 53 | * att 1: Attack detected 0: Otherwise 54 | * data Return bitstream data 55 | * x Spectral coefficients 56 | * y Return shapped coefficients 57 | * 58 | * `x` and `y` can be the same buffer 59 | */ 60 | void lc3_sns_analyze(enum lc3_dt dt, enum lc3_srate sr, 61 | const float *eb, bool att, lc3_sns_data_t *data, 62 | const float *x, float *y); 63 | 64 | /** 65 | * Return number of bits coding the bitstream data 66 | * return Bit consumption 67 | */ 68 | int lc3_sns_get_nbits(void); 69 | 70 | /** 71 | * Put bitstream data 72 | * bits Bitstream context 73 | * data Bitstream data 74 | */ 75 | void lc3_sns_put_data(lc3_bits_t *bits, const lc3_sns_data_t *data); 76 | 77 | 78 | /* ---------------------------------------------------------------------------- 79 | * Decoding 80 | * -------------------------------------------------------------------------- */ 81 | 82 | /** 83 | * Get bitstream data 84 | * bits Bitstream context 85 | * data Return SNS data 86 | * return 0: Ok -1: Invalid SNS data 87 | */ 88 | int lc3_sns_get_data(lc3_bits_t *bits, lc3_sns_data_t *data); 89 | 90 | /** 91 | * SNS synthesis 92 | * dt, sr Duration and samplerate of the frame 93 | * data Bitstream data 94 | * x Spectral coefficients 95 | * y Return shapped coefficients 96 | * 97 | * `x` and `y` can be the same buffer 98 | */ 99 | void lc3_sns_synthesize(enum lc3_dt dt, enum lc3_srate sr, 100 | const lc3_sns_data_t *data, const float *x, float *y); 101 | 102 | 103 | #endif /* __LC3_SNS_H */ 104 | -------------------------------------------------------------------------------- /fuzzer/liblc3_fuzzer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The Android Open Source Project 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 | #include 17 | 18 | #include "include/lc3.h" 19 | 20 | void TestEncoder(FuzzedDataProvider& fdp) { 21 | enum lc3_pcm_format pcm_format = 22 | fdp.PickValueInArray({LC3_PCM_FORMAT_S16, LC3_PCM_FORMAT_S24}); 23 | int dt_us = fdp.PickValueInArray({10000, 7500}); 24 | int sr_hz = 25 | fdp.PickValueInArray({8000, 16000, 24000, 32000, /*44100,*/ 48000}); 26 | unsigned enc_size = lc3_encoder_size(dt_us, sr_hz); 27 | uint16_t output_byte_count = fdp.ConsumeIntegralInRange(20, 400); 28 | uint16_t num_frames = lc3_frame_samples(dt_us, sr_hz); 29 | 30 | if (fdp.remaining_bytes() < num_frames * 2) { 31 | return; 32 | } 33 | 34 | std::vector input_frames(num_frames); 35 | fdp.ConsumeData(input_frames.data(), 36 | input_frames.size() * 2 /* each frame is 2 bytes */); 37 | 38 | void* lc3_encoder_mem = nullptr; 39 | lc3_encoder_mem = malloc(enc_size); 40 | lc3_encoder_t lc3_encoder = 41 | lc3_setup_encoder(dt_us, sr_hz, 0, lc3_encoder_mem); 42 | 43 | std::vector output(output_byte_count); 44 | lc3_encode(lc3_encoder, pcm_format, (const int16_t*)input_frames.data(), 1, 45 | output.size(), output.data()); 46 | 47 | free(lc3_encoder_mem); 48 | lc3_encoder_mem = nullptr; 49 | return; 50 | } 51 | 52 | void TestDecoder(FuzzedDataProvider& fdp) { 53 | enum lc3_pcm_format pcm_format = 54 | fdp.PickValueInArray({LC3_PCM_FORMAT_S16, LC3_PCM_FORMAT_S24}); 55 | int dt_us = fdp.PickValueInArray({10000, 7500}); 56 | int sr_hz = 57 | fdp.PickValueInArray({8000, 16000, 24000, 32000, /*44100,*/ 48000}); 58 | unsigned dec_size = lc3_decoder_size(dt_us, sr_hz); 59 | uint16_t input_byte_count = fdp.ConsumeIntegralInRange(20, 400); 60 | uint16_t num_frames = lc3_frame_samples(dt_us, sr_hz); 61 | 62 | if (fdp.remaining_bytes() < input_byte_count) { 63 | return; 64 | } 65 | 66 | std::vector input(input_byte_count); 67 | fdp.ConsumeData(input.data(), input.size()); 68 | 69 | void* lc3_decoder_mem = nullptr; 70 | lc3_decoder_mem = malloc(dec_size); 71 | lc3_decoder_t lc3_decoder = 72 | lc3_setup_decoder(dt_us, sr_hz, 0, lc3_decoder_mem); 73 | 74 | std::vector output(num_frames); 75 | lc3_decode(lc3_decoder, input.data(), input.size(), pcm_format, 76 | (int16_t*)output.data(), 1); 77 | 78 | /* Empty input performs PLC (packet loss concealment) */ 79 | lc3_decode(lc3_decoder, nullptr, 0, pcm_format, (int16_t*)output.data(), 1); 80 | 81 | free(lc3_decoder_mem); 82 | lc3_decoder_mem = nullptr; 83 | return; 84 | } 85 | 86 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 87 | FuzzedDataProvider fdp(data, size); 88 | TestEncoder(fdp); 89 | TestDecoder(fdp); 90 | return 0; 91 | } -------------------------------------------------------------------------------- /src/ltpf.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Long Term Postfilter 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_LTPF_H 27 | #define __LC3_LTPF_H 28 | 29 | #include "common.h" 30 | #include "bits.h" 31 | 32 | 33 | /** 34 | * LTPF data 35 | */ 36 | 37 | typedef struct lc3_ltpf_data { 38 | bool active; 39 | int pitch_index; 40 | } lc3_ltpf_data_t; 41 | 42 | 43 | /* ---------------------------------------------------------------------------- 44 | * Encoding 45 | * -------------------------------------------------------------------------- */ 46 | 47 | /** 48 | * LTPF analysis 49 | * dt, sr Duration and samplerate of the frame 50 | * ltpf Context of analysis 51 | * allowed True when activation of LTPF is allowed 52 | * x [-d..-1] Previous, [0..ns-1] Current samples 53 | * data Return bitstream data 54 | * return True when pitch present, False otherwise 55 | * 56 | * The number of previous samples `d` accessed on `x` is : 57 | * d: { 10, 20, 30, 40, 60 } - 1 for samplerates from 8KHz to 48KHz 58 | */ 59 | bool lc3_ltpf_analyse(enum lc3_dt dt, enum lc3_srate sr, 60 | lc3_ltpf_analysis_t *ltpf, const float *x, lc3_ltpf_data_t *data); 61 | 62 | /** 63 | * LTPF disable 64 | * data LTPF data, disabled activation on return 65 | */ 66 | void lc3_ltpf_disable(lc3_ltpf_data_t *data); 67 | 68 | /** 69 | * Return number of bits coding the bitstream data 70 | * pitch True when pitch present, False otherwise 71 | * return Bit consumption, including the pitch present flag 72 | */ 73 | int lc3_ltpf_get_nbits(bool pitch); 74 | 75 | /** 76 | * Put bitstream data 77 | * bits Bitstream context 78 | * data LTPF data 79 | */ 80 | void lc3_ltpf_put_data(lc3_bits_t *bits, const lc3_ltpf_data_t *data); 81 | 82 | 83 | /* ---------------------------------------------------------------------------- 84 | * Decoding 85 | * -------------------------------------------------------------------------- */ 86 | /** 87 | * Get bitstream data 88 | * bits Bitstream context 89 | * data Return bitstream data 90 | */ 91 | void lc3_ltpf_get_data(lc3_bits_t *bits, lc3_ltpf_data_t *data); 92 | 93 | /** 94 | * LTPF synthesis 95 | * dt, sr Duration and samplerate of the frame 96 | * nbytes Size in bytes of the frame 97 | * ltpf Context of synthesis 98 | * data Bitstream data, NULL when pitch not present 99 | * x [-d..-1] Previous, [0..ns-1] Current, filtered as output 100 | * 101 | * The number of previous samples `d` accessed on `x` is about 18 ms 102 | */ 103 | void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, int nbytes, 104 | lc3_ltpf_synthesis_t *ltpf, const lc3_ltpf_data_t *data, float *x); 105 | 106 | 107 | #endif /* __LC3_LTPF_H */ 108 | -------------------------------------------------------------------------------- /include/lc3_private.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * Copyright 2015 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 | #ifndef __LC3_PRIVATE_H 20 | #define __LC3_PRIVATE_H 21 | 22 | #include 23 | #include 24 | 25 | 26 | /** 27 | * Return number of samples, delayed samples and 28 | * encoded spectrum coefficients within a frame 29 | * For decoding, add number of samples of 18 ms history 30 | */ 31 | 32 | #define __LC3_NS(dt_us, sr_hz) \ 33 | ((dt_us * sr_hz) / 1000 / 1000) 34 | 35 | #define __LC3_ND(dt_us, sr_hz) \ 36 | ( (dt_us) == 7500 ? 23 * __LC3_NS(dt_us, sr_hz) / 30 \ 37 | : 5 * __LC3_NS(dt_us, sr_hz) / 8 ) 38 | 39 | #define __LC3_NH(sr_hz) \ 40 | ( (18 * sr_hz) / 1000 ) 41 | 42 | 43 | /** 44 | * Frame duration 7.5ms or 10ms 45 | */ 46 | 47 | enum lc3_dt { 48 | LC3_DT_7M5, 49 | LC3_DT_10M, 50 | 51 | LC3_NUM_DT 52 | }; 53 | 54 | /** 55 | * Sampling frequency 56 | */ 57 | 58 | enum lc3_srate { 59 | LC3_SRATE_8K, 60 | LC3_SRATE_16K, 61 | LC3_SRATE_24K, 62 | LC3_SRATE_32K, 63 | LC3_SRATE_48K, 64 | 65 | LC3_NUM_SRATE, 66 | }; 67 | 68 | 69 | /** 70 | * Encoder state and memory 71 | */ 72 | 73 | typedef struct lc3_attdet_analysis { 74 | float en1, an1; 75 | int p_att; 76 | } lc3_attdet_analysis_t; 77 | 78 | struct lc3_ltpf_hp50_state { 79 | float s1, s2; 80 | }; 81 | 82 | typedef struct lc3_ltpf_analysis { 83 | bool active; 84 | int pitch; 85 | float nc[2]; 86 | 87 | struct lc3_ltpf_hp50_state hp50; 88 | float x_12k8[384]; 89 | float x_6k4[178]; 90 | int tc; 91 | } lc3_ltpf_analysis_t; 92 | 93 | typedef struct lc3_spec_analysis { 94 | float nbits_off; 95 | int nbits_spare; 96 | } lc3_spec_analysis_t; 97 | 98 | struct lc3_encoder { 99 | enum lc3_dt dt; 100 | enum lc3_srate sr, sr_pcm; 101 | 102 | lc3_attdet_analysis_t attdet; 103 | lc3_ltpf_analysis_t ltpf; 104 | lc3_spec_analysis_t spec; 105 | 106 | float *xs, *xf, s[0]; 107 | }; 108 | 109 | #define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \ 110 | ( 2*__LC3_NS(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) ) 111 | 112 | #define LC3_ENCODER_MEM_T(dt_us, sr_hz) \ 113 | struct { \ 114 | struct lc3_encoder __e; \ 115 | float __s[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)]; \ 116 | } 117 | 118 | 119 | /** 120 | * Decoder state and memory 121 | */ 122 | 123 | typedef struct lc3_ltpf_synthesis { 124 | bool active; 125 | int pitch; 126 | float c[12][2], x[12]; 127 | } lc3_ltpf_synthesis_t; 128 | 129 | typedef struct lc3_plc_state { 130 | uint16_t seed; 131 | int count; 132 | float alpha; 133 | } lc3_plc_state_t; 134 | 135 | struct lc3_decoder { 136 | enum lc3_dt dt; 137 | enum lc3_srate sr, sr_pcm; 138 | 139 | lc3_ltpf_synthesis_t ltpf; 140 | lc3_plc_state_t plc; 141 | 142 | float *xs, *xd, *xg, s[0]; 143 | }; 144 | 145 | #define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \ 146 | ( __LC3_NH(sr_hz) + __LC3_NS(dt_us, sr_hz) + \ 147 | __LC3_ND(dt_us, sr_hz) + __LC3_NS(dt_us, sr_hz) ) 148 | 149 | #define LC3_DECODER_MEM_T(dt_us, sr_hz) \ 150 | struct { \ 151 | struct lc3_decoder __d; \ 152 | float __s[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)]; \ 153 | } 154 | 155 | 156 | #endif /* __LC3_PRIVATE_H */ 157 | -------------------------------------------------------------------------------- /src/spec.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Spectral coefficients encoding/decoding 21 | * 22 | * Reference : Low Complexity Communication Codec (LC3) 23 | * Bluetooth Specification v1.0 24 | */ 25 | 26 | #ifndef __LC3_SPEC_H 27 | #define __LC3_SPEC_H 28 | 29 | #include "common.h" 30 | #include "tables.h" 31 | #include "bwdet.h" 32 | #include "ltpf.h" 33 | #include "tns.h" 34 | #include "sns.h" 35 | 36 | 37 | /** 38 | * Spectral quantization side data 39 | */ 40 | typedef struct lc3_spec_side { 41 | int g_idx, nq; 42 | bool lsb_mode; 43 | } lc3_spec_side_t; 44 | 45 | 46 | /* ---------------------------------------------------------------------------- 47 | * Encoding 48 | * -------------------------------------------------------------------------- */ 49 | 50 | /** 51 | * Spectrum analysis 52 | * dt, sr, nbytes Duration, samplerate and size of the frame 53 | * pitch, tns Pitch present indication and TNS bistream data 54 | * spec Context of analysis 55 | * x Spectral coefficients, scaled as output 56 | * xq, side Return quantization data 57 | */ 58 | void lc3_spec_analyze(enum lc3_dt dt, enum lc3_srate sr, 59 | int nbytes, bool pitch, const lc3_tns_data_t *tns, 60 | lc3_spec_analysis_t *spec, float *x, int16_t *xq, lc3_spec_side_t *side); 61 | 62 | /** 63 | * Put spectral quantization side data 64 | * bits Bitstream context 65 | * dt, sr Duration and samplerate of the frame 66 | * side Spectral quantization side data 67 | */ 68 | void lc3_spec_put_side(lc3_bits_t *bits, 69 | enum lc3_dt dt, enum lc3_srate sr, const lc3_spec_side_t *side); 70 | 71 | /** 72 | * Encode spectral coefficients 73 | * bits Bitstream context 74 | * dt, sr, bw Duration, samplerate, bandwidth 75 | * nbytes and size of the frame 76 | * xq, side Quantization data 77 | * x Scaled spectral coefficients 78 | */ 79 | void lc3_spec_encode(lc3_bits_t *bits, 80 | enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, int nbytes, 81 | const int16_t *xq, const lc3_spec_side_t *side, const float *x); 82 | 83 | 84 | /* ---------------------------------------------------------------------------- 85 | * Decoding 86 | * -------------------------------------------------------------------------- */ 87 | 88 | /** 89 | * Get spectral quantization side data 90 | * bits Bitstream context 91 | * dt, sr Duration and samplerate of the frame 92 | * side Return quantization side data 93 | * return 0: Ok -1: Invalid bandwidth indication 94 | */ 95 | int lc3_spec_get_side(lc3_bits_t *bits, 96 | enum lc3_dt dt, enum lc3_srate sr, lc3_spec_side_t *side); 97 | 98 | /** 99 | * Decode spectral coefficients 100 | * bits Bitstream context 101 | * dt, sr, bw Duration, samplerate, bandwidth 102 | * nbytes and size of the frame 103 | * side Quantization side data 104 | * x Spectral coefficients 105 | * return 0: Ok -1: Invalid bitstream data 106 | */ 107 | int lc3_spec_decode(lc3_bits_t *bits, enum lc3_dt dt, enum lc3_srate sr, 108 | enum lc3_bandwidth bw, int nbytes, const lc3_spec_side_t *side, float *x); 109 | 110 | 111 | #endif /* __LC3_SPEC_H */ 112 | -------------------------------------------------------------------------------- /src/bwdet.c: -------------------------------------------------------------------------------- 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 "bwdet.h" 20 | 21 | 22 | /** 23 | * Bandwidth detector 24 | */ 25 | enum lc3_bandwidth lc3_bwdet_run( 26 | enum lc3_dt dt, enum lc3_srate sr, const float *e) 27 | { 28 | /* Bandwidth regions (Table 3.6) */ 29 | 30 | struct region { int is : 8; int ie : 8; }; 31 | 32 | static const struct region bws_table[LC3_NUM_DT] 33 | [LC3_NUM_BANDWIDTH-1][LC3_NUM_BANDWIDTH-1] = { 34 | 35 | [LC3_DT_7M5] = { 36 | { { 51, 63+1 } }, 37 | { { 45, 55+1 }, { 58, 63+1 } }, 38 | { { 42, 51+1 }, { 53, 58+1 }, { 60, 63+1 } }, 39 | { { 40, 48+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } }, 40 | }, 41 | 42 | [LC3_DT_10M] = { 43 | { { 53, 63+1 } }, 44 | { { 47, 56+1 }, { 59, 63+1 } }, 45 | { { 44, 52+1 }, { 54, 59+1 }, { 60, 63+1 } }, 46 | { { 41, 49+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } }, 47 | }, 48 | }; 49 | 50 | static const int l_table[LC3_NUM_DT][LC3_NUM_BANDWIDTH-1] = { 51 | [LC3_DT_7M5] = { 4, 4, 3, 2 }, 52 | [LC3_DT_10M] = { 4, 4, 3, 1 }, 53 | }; 54 | 55 | /* --- Stage 1 --- 56 | * Determine bw0 candidate */ 57 | 58 | enum lc3_bandwidth bw0 = LC3_BANDWIDTH_NB; 59 | enum lc3_bandwidth bwn = (enum lc3_bandwidth)sr; 60 | 61 | if (bwn <= bw0) 62 | return bwn; 63 | 64 | const struct region *bwr = bws_table[dt][bwn-1]; 65 | 66 | for (enum lc3_bandwidth bw = bw0; bw < bwn; bw++) { 67 | int i = bwr[bw].is, ie = bwr[bw].ie; 68 | int n = ie - i; 69 | 70 | float se = e[i]; 71 | for (i++; i < ie; i++) 72 | se += e[i]; 73 | 74 | if (se >= (10 << (bw == LC3_BANDWIDTH_NB)) * n) 75 | bw0 = bw + 1; 76 | } 77 | 78 | /* --- Stage 2 --- 79 | * Detect drop above cut-off frequency. 80 | * The Tc condition (13) is precalculated, as 81 | * Tc[] = 10 ^ (n / 10) , n = { 15, 23, 20, 20 } */ 82 | 83 | int hold = bw0 >= bwn; 84 | 85 | if (!hold) { 86 | int i0 = bwr[bw0].is, l = l_table[dt][bw0]; 87 | float tc = (const float []){ 88 | 31.62277660, 199.52623150, 100, 100 }[bw0]; 89 | 90 | for (int i = i0 - l + 1; !hold && i <= i0 + 1; i++) { 91 | hold = e[i-l] > tc * e[i]; 92 | } 93 | 94 | } 95 | 96 | return hold ? bw0 : bwn; 97 | } 98 | 99 | /** 100 | * Return number of bits coding the bandwidth value 101 | */ 102 | int lc3_bwdet_get_nbits(enum lc3_srate sr) 103 | { 104 | return (sr > 0) + (sr > 1) + (sr > 3); 105 | } 106 | 107 | /** 108 | * Put bandwidth indication 109 | */ 110 | void lc3_bwdet_put_bw(lc3_bits_t *bits, 111 | enum lc3_srate sr, enum lc3_bandwidth bw) 112 | { 113 | int nbits_bw = lc3_bwdet_get_nbits(sr); 114 | if (nbits_bw > 0) 115 | lc3_put_bits(bits, bw, nbits_bw); 116 | } 117 | 118 | /** 119 | * Get bandwidth indication 120 | */ 121 | int lc3_bwdet_get_bw(lc3_bits_t *bits, 122 | enum lc3_srate sr, enum lc3_bandwidth *bw) 123 | { 124 | enum lc3_bandwidth max_bw = (enum lc3_bandwidth)sr; 125 | int nbits_bw = lc3_bwdet_get_nbits(sr); 126 | 127 | *bw = nbits_bw > 0 ? lc3_get_bits(bits, nbits_bw) : LC3_BANDWIDTH_NB; 128 | return *bw > max_bw ? (*bw = max_bw), -1 : 0; 129 | } 130 | -------------------------------------------------------------------------------- /src/bits.h: -------------------------------------------------------------------------------- 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 | /** 20 | * LC3 - Bitstream management 21 | * 22 | * The bitstream is written by the 2 ends of the buffer : 23 | * 24 | * - Arthmetic coder put bits while increasing memory addresses 25 | * in the buffer (forward) 26 | * 27 | * - Plain bits are puts starting the end of the buffer, with memeory 28 | * addresses decreasing (backward) 29 | * 30 | * .---------------------------------------------------. 31 | * | > > > > > > > > > > : : < < < < < < < < < | 32 | * '---------------------------------------------------' 33 | * |---------------------> - - - - - - - - - - - - - ->| 34 | * |< - - - <-------------------| 35 | * Arithmetic coding Plain bits 36 | * `lc3_put_symbol()` `lc3_put_bits()` 37 | * 38 | * - The forward writing is protected against buffer overflow, it cannot 39 | * write after the buffer, but can overwrite plain bits previously 40 | * written in the buffer. 41 | * 42 | * - The backward writing is protected against overwrite of the arithmetic 43 | * coder bitstream. In such way, the backward bitstream is always limited 44 | * by the aritmetic coder bitstream, and can be overwritten by him. 45 | * 46 | * .---------------------------------------------------. 47 | * | > > > > > > > > > > : : < < < < < < < < < | 48 | * '---------------------------------------------------' 49 | * |---------------------> - - - - - - - - - - - - - ->| 50 | * |< - - - - - - - - - - - - - - <-------------------| 51 | * Arithmetic coding Plain bits 52 | * `lc3_get_symbol()` `lc3_get_bits()` 53 | * 54 | * - Reading is limited to read of the complementary end of the buffer. 55 | * 56 | * - The procedure `lc3_check_bits()` returns indication that read has been 57 | * made crossing the other bit plane. 58 | * 59 | * 60 | * Reference : Low Complexity Communication Codec (LC3) 61 | * Bluetooth Specification v1.0 62 | * 63 | */ 64 | 65 | #ifndef __LC3_BITS_H 66 | #define __LC3_BITS_H 67 | 68 | #include "common.h" 69 | 70 | 71 | /** 72 | * Bitstream mode 73 | */ 74 | 75 | enum lc3_bits_mode { 76 | LC3_BITS_MODE_READ, 77 | LC3_BITS_MODE_WRITE, 78 | }; 79 | 80 | /** 81 | * Arithmetic coder symbol interval 82 | * The model split the interval in 17 symbols 83 | */ 84 | 85 | struct lc3_ac_symbol { 86 | uint16_t low : 16; 87 | uint16_t range : 16; 88 | }; 89 | 90 | struct lc3_ac_model { 91 | struct lc3_ac_symbol s[17]; 92 | }; 93 | 94 | /** 95 | * Bitstream context 96 | */ 97 | 98 | #define LC3_ACCU_BITS (int)(8 * sizeof(unsigned)) 99 | 100 | struct lc3_bits_accu { 101 | unsigned v; 102 | int n, nover; 103 | }; 104 | 105 | #define LC3_AC_BITS (int)(24) 106 | 107 | struct lc3_bits_ac { 108 | unsigned low, range; 109 | int cache, carry, carry_count; 110 | bool error; 111 | }; 112 | 113 | struct lc3_bits_buffer { 114 | const uint8_t *start, *end; 115 | uint8_t *p_fw, *p_bw; 116 | }; 117 | 118 | typedef struct lc3_bits { 119 | enum lc3_bits_mode mode; 120 | struct lc3_bits_ac ac; 121 | struct lc3_bits_accu accu; 122 | struct lc3_bits_buffer buffer; 123 | } lc3_bits_t; 124 | 125 | 126 | /** 127 | * Setup bitstream reading/writing 128 | * bits Bitstream context 129 | * mode Either READ or WRITE mode 130 | * buffer, len Output buffer and length (in bytes) 131 | */ 132 | void lc3_setup_bits(lc3_bits_t *bits, 133 | enum lc3_bits_mode mode, void *buffer, int len); 134 | 135 | /** 136 | * Return number of bits left in the bitstream 137 | * bits Bitstream context 138 | * return Number of bits left 139 | */ 140 | int lc3_get_bits_left(const lc3_bits_t *bits); 141 | 142 | /** 143 | * Check if error occured on bitstream reading/writing 144 | * bits Bitstream context 145 | * return 0: Ok -1: Bitstream overflow or AC reading error 146 | */ 147 | int lc3_check_bits(const lc3_bits_t *bits); 148 | 149 | /** 150 | * Put a bit 151 | * bits Bitstream context 152 | * v Bit value, 0 or 1 153 | */ 154 | static inline void lc3_put_bit(lc3_bits_t *bits, int v); 155 | 156 | /** 157 | * Put from 1 to 32 bits 158 | * bits Bitstream context 159 | * v, n Value, in range 0 to 2^n - 1, and bits count (1 to 32) 160 | */ 161 | static inline void lc3_put_bits(lc3_bits_t *bits, unsigned v, int n); 162 | 163 | /** 164 | * Put arithmetic coder symbol 165 | * bits Bitstream context 166 | * model, s Model distribution and symbol value 167 | */ 168 | static inline void lc3_put_symbol(lc3_bits_t *bits, 169 | const struct lc3_ac_model *model, unsigned s); 170 | 171 | /** 172 | * Flush and terminate bitstream writing 173 | * bits Bitstream context 174 | */ 175 | void lc3_flush_bits(lc3_bits_t *bits); 176 | 177 | /** 178 | * Get a bit 179 | * bits Bitstream context 180 | */ 181 | static inline int lc3_get_bit(lc3_bits_t *bits); 182 | 183 | /** 184 | * Get from 1 to 32 bits 185 | * bits Bitstream context 186 | * n Number of bits to read (1 to 32) 187 | * return The value read 188 | */ 189 | static inline unsigned lc3_get_bits(lc3_bits_t *bits, int n); 190 | 191 | /** 192 | * Get arithmetic coder symbol 193 | * bits Bitstream context 194 | * model Model distribution 195 | * return The value read 196 | */ 197 | static inline unsigned lc3_get_symbol(lc3_bits_t *bits, 198 | const struct lc3_ac_model *model); 199 | 200 | 201 | 202 | /* ---------------------------------------------------------------------------- 203 | * Inline implementations 204 | * -------------------------------------------------------------------------- */ 205 | 206 | void lc3_put_bits_generic(lc3_bits_t *bits, unsigned v, int n); 207 | unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n); 208 | 209 | void lc3_ac_read_renorm(lc3_bits_t *bits); 210 | void lc3_ac_write_renorm(lc3_bits_t *bits); 211 | 212 | 213 | /** 214 | * Put a bit 215 | */ 216 | static inline void lc3_put_bit(lc3_bits_t *bits, int v) 217 | { 218 | lc3_put_bits(bits, v, 1); 219 | } 220 | 221 | /** 222 | * Put from 1 to 32 bits 223 | */ 224 | static inline void lc3_put_bits(struct lc3_bits *bits, unsigned v, int n) 225 | { 226 | struct lc3_bits_accu *accu = &bits->accu; 227 | 228 | if (accu->n + n <= LC3_ACCU_BITS) { 229 | accu->v |= v << accu->n; 230 | accu->n += n; 231 | } else { 232 | lc3_put_bits_generic(bits, v, n); 233 | } 234 | } 235 | 236 | /** 237 | * Get a bit 238 | */ 239 | static inline int lc3_get_bit(lc3_bits_t *bits) 240 | { 241 | return lc3_get_bits(bits, 1); 242 | } 243 | 244 | /** 245 | * Get from 1 to 32 bits 246 | */ 247 | static inline unsigned lc3_get_bits(struct lc3_bits *bits, int n) 248 | { 249 | struct lc3_bits_accu *accu = &bits->accu; 250 | 251 | if (accu->n + n <= LC3_ACCU_BITS) { 252 | int v = (accu->v >> accu->n) & ((1u << n) - 1); 253 | return (accu->n += n), v; 254 | } 255 | else { 256 | return lc3_get_bits_generic(bits, n); 257 | } 258 | } 259 | 260 | /** 261 | * Put arithmetic coder symbol 262 | */ 263 | static inline void lc3_put_symbol( 264 | struct lc3_bits *bits, const struct lc3_ac_model *model, unsigned s) 265 | { 266 | const struct lc3_ac_symbol *symbols = model->s; 267 | struct lc3_bits_ac *ac = &bits->ac; 268 | unsigned range = ac->range >> 10; 269 | 270 | ac->low += range * symbols[s].low; 271 | ac->range = range * symbols[s].range; 272 | 273 | ac->carry |= ac->low >> 24; 274 | ac->low &= 0xffffff; 275 | 276 | if (ac->range < 0x10000) 277 | lc3_ac_write_renorm(bits); 278 | } 279 | 280 | /** 281 | * Get arithmetic coder symbol 282 | */ 283 | static inline unsigned lc3_get_symbol( 284 | lc3_bits_t *bits, const struct lc3_ac_model *model) 285 | { 286 | const struct lc3_ac_symbol *symbols = model->s; 287 | struct lc3_bits_ac *ac = &bits->ac; 288 | 289 | unsigned range = (ac->range >> 10) & 0xffff; 290 | 291 | ac->error |= (ac->low >= (range << 10)); 292 | if (ac->error) 293 | ac->low = 0; 294 | 295 | int s = 16; 296 | 297 | if (ac->low < range * symbols[s].low) { 298 | s >>= 1; 299 | s -= ac->low < range * symbols[s].low ? 4 : -4; 300 | s -= ac->low < range * symbols[s].low ? 2 : -2; 301 | s -= ac->low < range * symbols[s].low ? 1 : -1; 302 | s -= ac->low < range * symbols[s].low; 303 | } 304 | 305 | ac->low -= range * symbols[s].low; 306 | ac->range = range * symbols[s].range; 307 | 308 | if (ac->range < 0x10000) 309 | lc3_ac_read_renorm(bits); 310 | 311 | return s; 312 | } 313 | 314 | #endif /* __LC3_BITS_H */ 315 | -------------------------------------------------------------------------------- /src/bits.c: -------------------------------------------------------------------------------- 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 "bits.h" 20 | #include "common.h" 21 | 22 | 23 | /* ---------------------------------------------------------------------------- 24 | * Common 25 | * -------------------------------------------------------------------------- */ 26 | 27 | static inline int ac_get(struct lc3_bits_buffer *); 28 | static inline void accu_load(struct lc3_bits_accu *, struct lc3_bits_buffer *); 29 | 30 | /** 31 | * Arithmetic coder return range bits 32 | * ac Arithmetic coder 33 | * return 1 + log2(ac->range) 34 | */ 35 | static int ac_get_range_bits(const struct lc3_bits_ac *ac) 36 | { 37 | int nbits = 0; 38 | 39 | for (unsigned r = ac->range; r; r >>= 1, nbits++); 40 | 41 | return nbits; 42 | } 43 | 44 | /** 45 | * Arithmetic coder return pending bits 46 | * ac Arithmetic coder 47 | * return Pending bits 48 | */ 49 | static int ac_get_pending_bits(const struct lc3_bits_ac *ac) 50 | { 51 | return 26 - ac_get_range_bits(ac) + 52 | ((ac->cache >= 0) + ac->carry_count) * 8; 53 | } 54 | 55 | /** 56 | * Return number of bits left in the bitstream 57 | * bits Bitstream context 58 | * return >= 0: Number of bits left < 0: Overflow 59 | */ 60 | static int get_bits_left(const struct lc3_bits *bits) 61 | { 62 | const struct lc3_bits_buffer *buffer = &bits->buffer; 63 | const struct lc3_bits_accu *accu = &bits->accu; 64 | const struct lc3_bits_ac *ac = &bits->ac; 65 | 66 | uintptr_t end = (uintptr_t)buffer->p_bw + 67 | (bits->mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS/8 : 0); 68 | 69 | uintptr_t start = (uintptr_t)buffer->p_fw - 70 | (bits->mode == LC3_BITS_MODE_READ ? LC3_AC_BITS/8 : 0); 71 | 72 | int n = end > start ? (int)(end - start) : -(int)(start - end); 73 | 74 | return 8 * n - (accu->n + accu->nover + ac_get_pending_bits(ac)); 75 | } 76 | 77 | /** 78 | * Setup bitstream writing 79 | */ 80 | void lc3_setup_bits(struct lc3_bits *bits, 81 | enum lc3_bits_mode mode, void *buffer, int len) 82 | { 83 | *bits = (struct lc3_bits){ 84 | .mode = mode, 85 | .accu = { 86 | .n = mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS : 0, 87 | }, 88 | .ac = { 89 | .range = 0xffffff, 90 | .cache = -1 91 | }, 92 | .buffer = { 93 | .start = (uint8_t *)buffer, .end = (uint8_t *)buffer + len, 94 | .p_fw = (uint8_t *)buffer, .p_bw = (uint8_t *)buffer + len, 95 | } 96 | }; 97 | 98 | if (mode == LC3_BITS_MODE_READ) { 99 | struct lc3_bits_ac *ac = &bits->ac; 100 | struct lc3_bits_accu *accu = &bits->accu; 101 | struct lc3_bits_buffer *buffer = &bits->buffer; 102 | 103 | ac->low = ac_get(buffer) << 16; 104 | ac->low |= ac_get(buffer) << 8; 105 | ac->low |= ac_get(buffer); 106 | 107 | accu_load(accu, buffer); 108 | } 109 | } 110 | 111 | /** 112 | * Return number of bits left in the bitstream 113 | */ 114 | int lc3_get_bits_left(const struct lc3_bits *bits) 115 | { 116 | return LC3_MAX(get_bits_left(bits), 0); 117 | } 118 | 119 | /** 120 | * Return number of bits left in the bitstream 121 | */ 122 | int lc3_check_bits(const struct lc3_bits *bits) 123 | { 124 | const struct lc3_bits_ac *ac = &bits->ac; 125 | 126 | return -(get_bits_left(bits) < 0 || ac->error); 127 | } 128 | 129 | 130 | /* ---------------------------------------------------------------------------- 131 | * Writing 132 | * -------------------------------------------------------------------------- */ 133 | 134 | /** 135 | * Flush the bits accumulator 136 | * accu Bitstream accumulator 137 | * buffer Bitstream buffer 138 | */ 139 | static inline void accu_flush( 140 | struct lc3_bits_accu *accu, struct lc3_bits_buffer *buffer) 141 | { 142 | int nbytes = LC3_MIN(accu->n >> 3, 143 | LC3_MAX(buffer->p_bw - buffer->p_fw, 0)); 144 | 145 | accu->n -= 8 * nbytes; 146 | 147 | for ( ; nbytes; accu->v >>= 8, nbytes--) 148 | *(--buffer->p_bw) = accu->v & 0xff; 149 | 150 | if (accu->n >= 8) 151 | accu->n = 0; 152 | } 153 | 154 | /** 155 | * Arithmetic coder put byte 156 | * buffer Bitstream buffer 157 | * byte Byte to output 158 | */ 159 | static inline void ac_put(struct lc3_bits_buffer *buffer, int byte) 160 | { 161 | if (buffer->p_fw < buffer->end) 162 | *(buffer->p_fw++) = byte; 163 | } 164 | 165 | /** 166 | * Arithmetic coder range shift 167 | * ac Arithmetic coder 168 | * buffer Bitstream buffer 169 | */ 170 | static inline void ac_shift( 171 | struct lc3_bits_ac *ac, struct lc3_bits_buffer *buffer) 172 | { 173 | if (ac->low < 0xff0000 || ac->carry) 174 | { 175 | if (ac->cache >= 0) 176 | ac_put(buffer, ac->cache + ac->carry); 177 | 178 | for ( ; ac->carry_count > 0; ac->carry_count--) 179 | ac_put(buffer, ac->carry ? 0x00 : 0xff); 180 | 181 | ac->cache = ac->low >> 16; 182 | ac->carry = 0; 183 | } 184 | else 185 | ac->carry_count++; 186 | 187 | ac->low = (ac->low << 8) & 0xffffff; 188 | } 189 | 190 | /** 191 | * Arithmetic coder termination 192 | * ac Arithmetic coder 193 | * buffer Bitstream buffer 194 | * end_val/nbits End value and count of bits to terminate (1 to 8) 195 | */ 196 | static void ac_terminate(struct lc3_bits_ac *ac, 197 | struct lc3_bits_buffer *buffer) 198 | { 199 | int nbits = 25 - ac_get_range_bits(ac); 200 | unsigned mask = 0xffffff >> nbits; 201 | unsigned val = ac->low + mask; 202 | unsigned high = ac->low + ac->range; 203 | 204 | bool over_val = val >> 24; 205 | bool over_high = high >> 24; 206 | 207 | val = (val & 0xffffff) & ~mask; 208 | high = (high & 0xffffff); 209 | 210 | if (over_val == over_high) { 211 | 212 | if (val + mask >= high) { 213 | nbits++; 214 | mask >>= 1; 215 | val = ((ac->low + mask) & 0xffffff) & ~mask; 216 | } 217 | 218 | ac->carry |= val < ac->low; 219 | } 220 | 221 | ac->low = val; 222 | 223 | for (; nbits > 8; nbits -= 8) 224 | ac_shift(ac, buffer); 225 | ac_shift(ac, buffer); 226 | 227 | int end_val = ac->cache >> (8 - nbits); 228 | 229 | if (ac->carry_count) { 230 | ac_put(buffer, ac->cache); 231 | for ( ; ac->carry_count > 1; ac->carry_count--) 232 | ac_put(buffer, 0xff); 233 | 234 | end_val = nbits < 8 ? 0 : 0xff; 235 | } 236 | 237 | if (buffer->p_fw < buffer->end) { 238 | *buffer->p_fw &= 0xff >> nbits; 239 | *buffer->p_fw |= end_val << (8 - nbits); 240 | } 241 | } 242 | 243 | /** 244 | * Flush and terminate bitstream 245 | */ 246 | void lc3_flush_bits(struct lc3_bits *bits) 247 | { 248 | struct lc3_bits_ac *ac = &bits->ac; 249 | struct lc3_bits_accu *accu = &bits->accu; 250 | struct lc3_bits_buffer *buffer = &bits->buffer; 251 | 252 | int nleft = buffer->p_bw - buffer->p_fw; 253 | for (int n = 8 * nleft - accu->n; n > 0; n -= 32) 254 | lc3_put_bits(bits, 0, LC3_MIN(n, 32)); 255 | 256 | accu_flush(accu, buffer); 257 | 258 | ac_terminate(ac, buffer); 259 | } 260 | 261 | /** 262 | * Write from 1 to 32 bits, 263 | * exceeding the capacity of the accumulator 264 | */ 265 | void lc3_put_bits_generic(struct lc3_bits *bits, unsigned v, int n) 266 | { 267 | struct lc3_bits_accu *accu = &bits->accu; 268 | 269 | /* --- Fulfill accumulator and flush -- */ 270 | 271 | int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n); 272 | if (n1) { 273 | accu->v |= v << accu->n; 274 | accu->n = LC3_ACCU_BITS; 275 | } 276 | 277 | accu_flush(accu, &bits->buffer); 278 | 279 | /* --- Accumulate remaining bits -- */ 280 | 281 | accu->v = v >> n1; 282 | accu->n = n - n1; 283 | } 284 | 285 | /** 286 | * Arithmetic coder renormalization 287 | */ 288 | void lc3_ac_write_renorm(struct lc3_bits *bits) 289 | { 290 | struct lc3_bits_ac *ac = &bits->ac; 291 | 292 | for ( ; ac->range < 0x10000; ac->range <<= 8) 293 | ac_shift(ac, &bits->buffer); 294 | } 295 | 296 | 297 | /* ---------------------------------------------------------------------------- 298 | * Reading 299 | * -------------------------------------------------------------------------- */ 300 | 301 | /** 302 | * Arithmetic coder get byte 303 | * buffer Bitstream buffer 304 | * return Byte read, 0 on overflow 305 | */ 306 | static inline int ac_get(struct lc3_bits_buffer *buffer) 307 | { 308 | return buffer->p_fw < buffer->end ? *(buffer->p_fw++) : 0; 309 | } 310 | 311 | /** 312 | * Load the accumulator 313 | * accu Bitstream accumulator 314 | * buffer Bitstream buffer 315 | */ 316 | static inline void accu_load(struct lc3_bits_accu *accu, 317 | struct lc3_bits_buffer *buffer) 318 | { 319 | int nbytes = LC3_MIN(accu->n >> 3, buffer->p_bw - buffer->start); 320 | 321 | accu->n -= 8 * nbytes; 322 | 323 | for ( ; nbytes; nbytes--) { 324 | accu->v >>= 8; 325 | accu->v |= *(--buffer->p_bw) << (LC3_ACCU_BITS - 8); 326 | } 327 | 328 | if (accu->n >= 8) { 329 | accu->nover = LC3_MIN(accu->nover + accu->n, LC3_ACCU_BITS); 330 | accu->v >>= accu->n; 331 | accu->n = 0; 332 | } 333 | } 334 | 335 | /** 336 | * Read from 1 to 32 bits, 337 | * exceeding the capacity of the accumulator 338 | */ 339 | unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n) 340 | { 341 | struct lc3_bits_accu *accu = &bits->accu; 342 | struct lc3_bits_buffer *buffer = &bits->buffer; 343 | 344 | /* --- Fulfill accumulator and read -- */ 345 | 346 | accu_load(accu, buffer); 347 | 348 | int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n); 349 | unsigned v = (accu->v >> accu->n) & ((1u << n1) - 1); 350 | accu->n += n1; 351 | 352 | /* --- Second round --- */ 353 | 354 | int n2 = n - n1; 355 | 356 | if (n2) { 357 | accu_load(accu, buffer); 358 | 359 | v |= ((accu->v >> accu->n) & ((1u << n2) - 1)) << n1; 360 | accu->n += n2; 361 | } 362 | 363 | return v; 364 | } 365 | 366 | /** 367 | * Arithmetic coder renormalization 368 | */ 369 | void lc3_ac_read_renorm(struct lc3_bits *bits) 370 | { 371 | struct lc3_bits_ac *ac = &bits->ac; 372 | 373 | for ( ; ac->range < 0x10000; ac->range <<= 8) 374 | ac->low = ((ac->low << 8) | ac_get(&bits->buffer)) & 0xffffff; 375 | } 376 | -------------------------------------------------------------------------------- /include/lc3.h: -------------------------------------------------------------------------------- 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 | /** 20 | * Low Complexity Communication Codec (LC3) 21 | * 22 | * This implementation conforms to : 23 | * Low Complexity Communication Codec (LC3) 24 | * Bluetooth Specification v1.0 25 | * 26 | * 27 | * The LC3 is an efficient low latency audio codec. 28 | * 29 | * - Unlike most other codecs, the LC3 codec is focused on audio streaming 30 | * in constrained (on packet sizes and interval) tranport layer. 31 | * In this way, the LC3 does not handle : 32 | * VBR (Variable Bitrate), based on input signal complexity 33 | * ABR (Adaptative Bitrate). It does not rely on any bit reservoir, 34 | * a frame will be strictly encoded in the bytes budget given by 35 | * the user (or transport layer). 36 | * 37 | * However, the bitrate (bytes budget for encoding a frame) can be 38 | * freely changed at any time. But will not rely on signal complexity, 39 | * it can follow a temporary bandwidth increase or reduction. 40 | * 41 | * - Unlike classic codecs, the LC3 codecs does not run on fixed amount 42 | * of samples as input. It operate only on fixed frame duration, for 43 | * any supported samplerates (8 to 48 KHz). Two frame duration are 44 | * available 7.5ms and 10ms. 45 | * 46 | * 47 | * --- About 44.1 KHz samplerate --- 48 | * 49 | * The Bluetooth specification oddly add the 44.1 KHz samplerate. Although 50 | * there is NO SUPPORT in the core algorithm of the codec of 44.1 KHz. 51 | * We can summarize the 44.1 KHz support by "you can put any samplerate 52 | * around the base defined samplerates". But be concerned by : 53 | * 54 | * 1. The frame size will not be 7.5 ms or 10 ms, but is scaled 55 | * by 'supported samplerate' / 'input samplerate' 56 | * 57 | * 2. The bandwidth will be hard limited (to 20 KHz) if you select 48 KHz. 58 | * The encoded bandwidth will also be affected by the above inverse 59 | * factor of 20 KHz. 60 | * 61 | * Applied to 44.1 KHz, we get : 62 | * 63 | * 1. About 8.16 ms frame duration, instead of 7.5 ms 64 | * About 10.88 ms frame duration, instead of 10 ms 65 | * 66 | * 2. The bandwidth becomes limited to 18.375 KHz 67 | * 68 | * 69 | * --- How to encode / decode --- 70 | * 71 | * An encoder / decoder context need to be setup. This context keep states 72 | * on the current stream to proceed, and samples that overlapped across 73 | * frames. 74 | * 75 | * You have two ways to setup the encoder / decoder : 76 | * 77 | * - Using static memory allocation (this module does not rely on 78 | * any dynamic memory allocation). The types `lc3_xxcoder_mem_16k_t`, 79 | * and `lc3_xxcoder_mem_48k_t` have size of the memory needed for 80 | * encoding up to 16 KHz or 48 KHz. 81 | * 82 | * - Using dynamic memory allocation. The `lc3_xxcoder_size()` procedure 83 | * returns the needed memory size, for a given configuration. The memory 84 | * space must be aligned to a pointer size. As an example, you can setup 85 | * encoder like this : 86 | * 87 | * | enc = lc3_setup_encoder(frame_us, samplerate, 88 | * | malloc(lc3_encoder_size(frame_us, samplerate))); 89 | * | ... 90 | * | free(enc); 91 | * 92 | * Note : 93 | * - A NULL memory adress as input, will return a NULL encoder context. 94 | * - The returned encoder handle is set at the address of the allocated 95 | * memory space, you can directly free the handle. 96 | * 97 | * Next, call the `lc3_encode()` encoding procedure, for each frames. 98 | * To handle multichannel streams (Stereo or more), you can proceed with 99 | * inerleaved channels PCM stream like this : 100 | * 101 | * | for(int ich = 0; ich < nch: ich++) 102 | * | lc3_encode(encoder[ich], pcm + ich, nch, ...); 103 | * 104 | * with `nch` as the number of channels in the PCM stream 105 | */ 106 | 107 | #ifndef __LC3_H 108 | #define __LC3_H 109 | 110 | #ifdef __cplusplus 111 | extern "C" { 112 | #endif 113 | 114 | #include 115 | #include 116 | 117 | #include "lc3_private.h" 118 | 119 | 120 | /** 121 | * Limitations 122 | * - On the bitrate, in bps, of a stream 123 | * - On the size of the frames in bytes 124 | */ 125 | 126 | #define LC3_MIN_BITRATE 16000 127 | #define LC3_MAX_BITRATE 320000 128 | 129 | #define LC3_MIN_FRAME_BYTES 20 130 | #define LC3_MAX_FRAME_BYTES 400 131 | 132 | 133 | /** 134 | * Parameters check 135 | * LC3_CHECK_DT_US(us) True when frame duration in us is suitable 136 | * LC3_CHECK_SR_HZ(sr) True when samplerate in Hz is suitable 137 | */ 138 | 139 | #define LC3_CHECK_DT_US(us) \ 140 | ( ((us) == 7500) || ((us) == 10000) ) 141 | 142 | #define LC3_CHECK_SR_HZ(sr) \ 143 | ( ((sr) == 8000) || ((sr) == 16000) || ((sr) == 24000) || \ 144 | ((sr) == 32000) || ((sr) == 48000) ) 145 | 146 | 147 | /** 148 | * PCM Sample Format 149 | * S16 Signed 16 bits, in 16 bits words (int16_t) 150 | * S24 Signed 24 bits, using low three bytes of 32 bits words (int32_t). 151 | * The high byte sign extends the sample value (bits 31..24 set to b23). 152 | */ 153 | 154 | enum lc3_pcm_format { 155 | LC3_PCM_FORMAT_S16, 156 | LC3_PCM_FORMAT_S24, 157 | }; 158 | 159 | 160 | /** 161 | * Handle 162 | */ 163 | 164 | typedef struct lc3_encoder *lc3_encoder_t; 165 | typedef struct lc3_decoder *lc3_decoder_t; 166 | 167 | 168 | /** 169 | * Static memory of encoder context 170 | * 171 | * Propose types suitable for static memory allocation, supporting 172 | * any frame duration, and maximum samplerates 16k and 48k respectively 173 | * You can customize your type using the `LC3_ENCODER_MEM_T` or 174 | * `LC3_DECODER_MEM_T` macro. 175 | */ 176 | 177 | typedef LC3_ENCODER_MEM_T(10000, 16000) lc3_encoder_mem_16k_t; 178 | typedef LC3_ENCODER_MEM_T(10000, 48000) lc3_encoder_mem_48k_t; 179 | 180 | typedef LC3_DECODER_MEM_T(10000, 16000) lc3_decoder_mem_16k_t; 181 | typedef LC3_DECODER_MEM_T(10000, 48000) lc3_decoder_mem_48k_t; 182 | 183 | 184 | /** 185 | * Return the number of PCM samples in a frame 186 | * dt_us Frame duration in us, 7500 or 10000 187 | * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 188 | * return Number of PCM samples, -1 on bad parameters 189 | */ 190 | int lc3_frame_samples(int dt_us, int sr_hz); 191 | 192 | /** 193 | * Return the size of frames, from bitrate 194 | * dt_us Frame duration in us, 7500 or 10000 195 | * bitrate Target bitrate in bit per seconds 196 | * return The floor size in bytes of the frames, -1 on bad parameters 197 | */ 198 | int lc3_frame_bytes(int dt_us, int bitrate); 199 | 200 | /** 201 | * Resolve the bitrate, from the size of frames 202 | * dt_us Frame duration in us, 7500 or 10000 203 | * nbytes Size in bytes of the frames 204 | * return The according bitrate in bps, -1 on bad parameters 205 | */ 206 | int lc3_resolve_bitrate(int dt_us, int nbytes); 207 | 208 | /** 209 | * Return algorithmic delay, as a number of samples 210 | * dt_us Frame duration in us, 7500 or 10000 211 | * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 212 | * return Number of algorithmic delay samples, -1 on bad parameters 213 | */ 214 | int lc3_delay_samples(int dt_us, int sr_hz); 215 | 216 | /** 217 | * Return size needed for an encoder 218 | * dt_us Frame duration in us, 7500 or 10000 219 | * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 220 | * return Size of then encoder in bytes, 0 on bad parameters 221 | * 222 | * The `sr_hz` parameter is the samplerate of the PCM input stream, 223 | * and will match `sr_pcm_hz` of `lc3_setup_encoder()`. 224 | */ 225 | unsigned lc3_encoder_size(int dt_us, int sr_hz); 226 | 227 | /** 228 | * Setup encoder 229 | * dt_us Frame duration in us, 7500 or 10000 230 | * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 231 | * sr_pcm_hz Input samplerate, downsampling option of input, or 0 232 | * mem Encoder memory space, aligned to pointer type 233 | * return Encoder as an handle, NULL on bad parameters 234 | * 235 | * The `sr_pcm_hz` parameter is a downsampling option of PCM input, 236 | * the value `0` fallback to the samplerate of the encoded stream `sr_hz`. 237 | * When used, `sr_pcm_hz` is intended to be higher or equal to the encoder 238 | * samplerate `sr_hz`. The size of the context needed, given by 239 | * `lc3_encoder_size()` will be set accordingly to `sr_pcm_hz`. 240 | */ 241 | lc3_encoder_t lc3_setup_encoder( 242 | int dt_us, int sr_hz, int sr_pcm_hz, void *mem); 243 | 244 | /** 245 | * Encode a frame 246 | * encoder Handle of the encoder 247 | * fmt PCM input format 248 | * pcm, stride Input PCM samples, and count between two consecutives 249 | * nbytes Target size, in bytes, of the frame (20 to 400) 250 | * out Output buffer of `nbytes` size 251 | * return 0: On success -1: Wrong parameters 252 | */ 253 | int lc3_encode(lc3_encoder_t encoder, enum lc3_pcm_format fmt, 254 | const void *pcm, int stride, int nbytes, void *out); 255 | 256 | /** 257 | * Return size needed for an decoder 258 | * dt_us Frame duration in us, 7500 or 10000 259 | * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 260 | * return Size of then decoder in bytes, 0 on bad parameters 261 | * 262 | * The `sr_hz` parameter is the samplerate of the PCM output stream, 263 | * and will match `sr_pcm_hz` of `lc3_setup_decoder()`. 264 | */ 265 | unsigned lc3_decoder_size(int dt_us, int sr_hz); 266 | 267 | /** 268 | * Setup decoder 269 | * dt_us Frame duration in us, 7500 or 10000 270 | * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 271 | * sr_pcm_hz Output samplerate, upsampling option of output (or 0) 272 | * mem Decoder memory space, aligned to pointer type 273 | * return Decoder as an handle, NULL on bad parameters 274 | * 275 | * The `sr_pcm_hz` parameter is an upsampling option of PCM output, 276 | * the value `0` fallback to the samplerate of the decoded stream `sr_hz`. 277 | * When used, `sr_pcm_hz` is intended to be higher or equal to the decoder 278 | * samplerate `sr_hz`. The size of the context needed, given by 279 | * `lc3_decoder_size()` will be set accordingly to `sr_pcm_hz`. 280 | */ 281 | lc3_decoder_t lc3_setup_decoder( 282 | int dt_us, int sr_hz, int sr_pcm_hz, void *mem); 283 | 284 | /** 285 | * Decode a frame 286 | * decoder Handle of the decoder 287 | * in, nbytes Input bitstream, and size in bytes, NULL performs PLC 288 | * fmt PCM output format 289 | * pcm, stride Output PCM samples, and count between two consecutives 290 | * return 0: On success 1: PLC operated -1: Wrong parameters 291 | */ 292 | int lc3_decode(lc3_decoder_t decoder, const void *in, int nbytes, 293 | enum lc3_pcm_format fmt, void *pcm, int stride); 294 | 295 | 296 | #ifdef __cplusplus 297 | } 298 | #endif 299 | 300 | #endif /* __LC3_H */ 301 | -------------------------------------------------------------------------------- /src/tns.c: -------------------------------------------------------------------------------- 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 "tns.h" 20 | #include "tables.h" 21 | 22 | 23 | /* ---------------------------------------------------------------------------- 24 | * Filter Coefficients 25 | * -------------------------------------------------------------------------- */ 26 | 27 | /** 28 | * Resolve LPC Weighting indication according bitrate 29 | * dt, nbytes Duration and size of the frame 30 | * return True when LPC Weighting enabled 31 | */ 32 | static bool resolve_lpc_weighting(enum lc3_dt dt, int nbytes) 33 | { 34 | return nbytes < (dt == LC3_DT_7M5 ? 360/8 : 480/8); 35 | } 36 | 37 | /** 38 | * Return dot product of 2 vectors 39 | * a, b, n The 2 vectors of size `n` 40 | * return sum( a[i] * b[i] ), i = [0..n-1] 41 | */ 42 | static inline float dot(const float *a, const float *b, int n) 43 | { 44 | float v = 0; 45 | 46 | while (n--) 47 | v += *(a++) * *(b++); 48 | 49 | return v; 50 | } 51 | 52 | /** 53 | * LPC Coefficients 54 | * dt, bw Duration and bandwidth of the frame 55 | * x Spectral coefficients 56 | * gain, a Output the prediction gains and LPC coefficients 57 | */ 58 | static void compute_lpc_coeffs(enum lc3_dt dt, enum lc3_bandwidth bw, 59 | const float *x, float *gain, float (*a)[9]) 60 | { 61 | static const int sub_7m5_nb[] = { 9, 26, 43, 60 }; 62 | static const int sub_7m5_wb[] = { 9, 46, 83, 120 }; 63 | static const int sub_7m5_sswb[] = { 9, 66, 123, 180 }; 64 | static const int sub_7m5_swb[] = { 9, 46, 82, 120, 159, 200, 240 }; 65 | static const int sub_7m5_fb[] = { 9, 56, 103, 150, 200, 250, 300 }; 66 | 67 | static const int sub_10m_nb[] = { 12, 34, 57, 80 }; 68 | static const int sub_10m_wb[] = { 12, 61, 110, 160 }; 69 | static const int sub_10m_sswb[] = { 12, 88, 164, 240 }; 70 | static const int sub_10m_swb[] = { 12, 61, 110, 160, 213, 266, 320 }; 71 | static const int sub_10m_fb[] = { 12, 74, 137, 200, 266, 333, 400 }; 72 | 73 | /* --- Normalized autocorrelation --- */ 74 | 75 | static const float lag_window[] = { 76 | 1.00000000e+00, 9.98028026e-01, 9.92135406e-01, 9.82391584e-01, 77 | 9.68910791e-01, 9.51849807e-01, 9.31404933e-01, 9.07808230e-01, 78 | 8.81323137e-01 79 | }; 80 | 81 | const int *sub = (const int * const [LC3_NUM_DT][LC3_NUM_SRATE]){ 82 | { sub_7m5_nb, sub_7m5_wb, sub_7m5_sswb, sub_7m5_swb, sub_7m5_fb }, 83 | { sub_10m_nb, sub_10m_wb, sub_10m_sswb, sub_10m_swb, sub_10m_fb }, 84 | }[dt][bw]; 85 | 86 | int nfilters = 1 + (bw >= LC3_BANDWIDTH_SWB); 87 | 88 | const float *xs, *xe = x + *sub; 89 | float r[2][9]; 90 | 91 | for (int f = 0; f < nfilters; f++) { 92 | float c[9][3]; 93 | 94 | for (int s = 0; s < 3; s++) { 95 | xs = xe, xe = x + *(++sub); 96 | 97 | for (int k = 0; k < 9; k++) 98 | c[k][s] = dot(xs, xs + k, (xe - xs) - k); 99 | } 100 | 101 | float e0 = c[0][0], e1 = c[0][1], e2 = c[0][2]; 102 | 103 | r[f][0] = 3; 104 | for (int k = 1; k < 9; k++) 105 | r[f][k] = e0 == 0 || e1 == 0 || e2 == 0 ? 0 : 106 | (c[k][0]/e0 + c[k][1]/e1 + c[k][2]/e2) * lag_window[k]; 107 | } 108 | 109 | /* --- Levinson-Durbin recursion --- */ 110 | 111 | for (int f = 0; f < nfilters; f++) { 112 | float *a0 = a[f], a1[9]; 113 | float err = r[f][0], rc; 114 | 115 | gain[f] = err; 116 | 117 | a0[0] = 1; 118 | for (int k = 1; k < 9; ) { 119 | 120 | rc = -r[f][k]; 121 | for (int i = 1; i < k; i++) 122 | rc -= a0[i] * r[f][k-i]; 123 | 124 | rc /= err; 125 | err *= 1 - rc * rc; 126 | 127 | for (int i = 1; i < k; i++) 128 | a1[i] = a0[i] + rc * a0[k-i]; 129 | a1[k++] = rc; 130 | 131 | rc = -r[f][k]; 132 | for (int i = 1; i < k; i++) 133 | rc -= a1[i] * r[f][k-i]; 134 | 135 | rc /= err; 136 | err *= 1 - rc * rc; 137 | 138 | for (int i = 1; i < k; i++) 139 | a0[i] = a1[i] + rc * a1[k-i]; 140 | a0[k++] = rc; 141 | } 142 | 143 | gain[f] /= err; 144 | } 145 | } 146 | 147 | /** 148 | * LPC Weighting 149 | * gain, a Prediction gain and LPC coefficients, weighted as output 150 | */ 151 | static void lpc_weighting(float pred_gain, float *a) 152 | { 153 | float gamma = 1. - (1. - 0.85) * (2. - pred_gain) / (2. - 1.5), g = 1; 154 | for (int i = 1; i < 9; i++) 155 | a[i] *= (g *= gamma); 156 | } 157 | 158 | /** 159 | * LPC reflection 160 | * a LPC coefficients 161 | * rc Output refelection coefficients 162 | */ 163 | static void lpc_reflection(const float *a, float *rc) 164 | { 165 | float e, b[2][7], *b0, *b1; 166 | 167 | rc[7] = a[1+7]; 168 | e = 1 - rc[7] * rc[7]; 169 | 170 | b1 = b[1]; 171 | for (int i = 0; i < 7; i++) 172 | b1[i] = (a[1+i] - rc[7] * a[7-i]) / e; 173 | 174 | for (int k = 6; k > 0; k--) { 175 | b0 = b1, b1 = b[k & 1]; 176 | 177 | rc[k] = b0[k]; 178 | e = 1 - rc[k] * rc[k]; 179 | 180 | for (int i = 0; i < k; i++) 181 | b1[i] = (b0[i] - rc[k] * b0[k-1-i]) / e; 182 | } 183 | 184 | rc[0] = b1[0]; 185 | } 186 | 187 | /** 188 | * Quantization of RC coefficients 189 | * rc Refelection coefficients 190 | * rc_order Return order of coefficients 191 | * rc_i Return quantized coefficients 192 | */ 193 | static void quantize_rc(const float *rc, int *rc_order, int *rc_q) 194 | { 195 | /* Quantization table, sin(delta * (i + 0.5)), delta = Pi / 17 */ 196 | 197 | static float q_thr[] = { 198 | 9.22683595e-02, 2.73662990e-01, 4.45738356e-01, 6.02634636e-01, 199 | 7.39008917e-01, 8.50217136e-01, 9.32472229e-01, 9.82973100e-01 200 | }; 201 | 202 | *rc_order = 8; 203 | 204 | for (int i = 0; i < 8; i++) { 205 | float rc_m = fabsf(rc[i]); 206 | 207 | rc_q[i] = 4 * (rc_m >= q_thr[4]); 208 | for (int j = 0; j < 4 && rc_m >= q_thr[rc_q[i]]; j++, rc_q[i]++); 209 | 210 | if (rc[i] < 0) 211 | rc_q[i] = -rc_q[i]; 212 | 213 | *rc_order = rc_q[i] != 0 ? 8 : *rc_order - 1; 214 | } 215 | } 216 | 217 | /** 218 | * Unquantization of RC coefficients 219 | * rc_q Quantized coefficients 220 | * rc_order Order of coefficients 221 | * rc Return refelection coefficients 222 | */ 223 | static void unquantize_rc(const int *rc_q, int rc_order, float rc[8]) 224 | { 225 | /* Quantization table, sin(delta * i), delta = Pi / 17 */ 226 | 227 | static float q_inv[] = { 228 | 0.00000000e+00, 1.83749517e-01, 3.61241664e-01, 5.26432173e-01, 229 | 6.73695641e-01, 7.98017215e-01, 8.95163302e-01, 9.61825645e-01, 230 | 9.95734176e-01 231 | }; 232 | 233 | int i; 234 | 235 | for (i = 0; i < rc_order; i++) { 236 | float rc_m = q_inv[LC3_ABS(rc_q[i])]; 237 | rc[i] = rc_q[i] < 0 ? -rc_m : rc_m; 238 | } 239 | } 240 | 241 | 242 | /* ---------------------------------------------------------------------------- 243 | * Filtering 244 | * -------------------------------------------------------------------------- */ 245 | 246 | /** 247 | * Forward filtering 248 | * dt, bw Duration and bandwidth of the frame 249 | * rc_order, rc Order of coefficients, and coefficients 250 | * x Spectral coefficients, filtered as output 251 | */ 252 | static void forward_filtering( 253 | enum lc3_dt dt, enum lc3_bandwidth bw, 254 | const int rc_order[2], const float rc[2][8], float *x) 255 | { 256 | int nfilters = 1 + (bw >= LC3_BANDWIDTH_SWB); 257 | int nf = LC3_NE(dt, bw) >> (nfilters - 1); 258 | int i0, ie = 3*(3 + dt); 259 | 260 | float s[8] = { 0 }; 261 | 262 | for (int f = 0; f < nfilters; f++) { 263 | 264 | i0 = ie; 265 | ie = nf * (1 + f); 266 | 267 | if (!rc_order[f]) 268 | continue; 269 | 270 | for (int i = i0; i < ie; i++) { 271 | float xi = x[i]; 272 | float s0, s1 = xi; 273 | 274 | for (int k = 0; k < rc_order[f]; k++) { 275 | s0 = s[k]; 276 | s[k] = s1; 277 | 278 | s1 = rc[f][k] * xi + s0; 279 | xi += rc[f][k] * s0; 280 | } 281 | 282 | x[i] = xi; 283 | } 284 | } 285 | } 286 | 287 | /** 288 | * Inverse filtering 289 | * dt, bw Duration and bandwidth of the frame 290 | * rc_order, rc Order of coefficients, and unquantized coefficients 291 | * x Spectral coefficients, filtered as output 292 | */ 293 | static void inverse_filtering( 294 | enum lc3_dt dt, enum lc3_bandwidth bw, 295 | const int rc_order[2], const float rc[2][8], float *x) 296 | { 297 | int nfilters = 1 + (bw >= LC3_BANDWIDTH_SWB); 298 | int nf = LC3_NE(dt, bw) >> (nfilters - 1); 299 | int i0, ie = 3*(3 + dt); 300 | 301 | float s[8] = { 0 }; 302 | 303 | for (int f = 0; f < nfilters; f++) { 304 | 305 | i0 = ie; 306 | ie = nf * (1 + f); 307 | 308 | if (!rc_order[f]) 309 | continue; 310 | 311 | for (int i = i0; i < ie; i++) { 312 | float xi = x[i]; 313 | 314 | xi -= s[7] * rc[f][7]; 315 | for (int k = 6; k >= 0; k--) { 316 | xi -= s[k] * rc[f][k]; 317 | s[k+1] = s[k] + rc[f][k] * xi; 318 | } 319 | s[0] = xi; 320 | x[i] = xi; 321 | } 322 | 323 | for (int k = 7; k >= rc_order[f]; k--) 324 | s[k] = 0; 325 | } 326 | } 327 | 328 | 329 | /* ---------------------------------------------------------------------------- 330 | * Interface 331 | * -------------------------------------------------------------------------- */ 332 | 333 | /** 334 | * TNS analysis 335 | */ 336 | void lc3_tns_analyze(enum lc3_dt dt, enum lc3_bandwidth bw, 337 | bool nn_flag, int nbytes, struct lc3_tns_data *data, float *x) 338 | { 339 | /* Processing steps : 340 | * - Determine the LPC (Linear Predictive Coding) Coefficients 341 | * - Check is the filtering is disabled 342 | * - The coefficients are weighted on low bitrates and predicition gain 343 | * - Convert to reflection coefficients and quantize 344 | * - Finally filter the spectral coefficients */ 345 | 346 | float pred_gain[2], a[2][9]; 347 | float rc[2][8]; 348 | 349 | data->nfilters = 1 + (bw >= LC3_BANDWIDTH_SWB); 350 | data->lpc_weighting = resolve_lpc_weighting(dt, nbytes); 351 | 352 | compute_lpc_coeffs(dt, bw, x, pred_gain, a); 353 | 354 | for (int f = 0; f < data->nfilters; f++) { 355 | 356 | data->rc_order[f] = 0; 357 | if (nn_flag || pred_gain[f] <= 1.5) 358 | continue; 359 | 360 | if (data->lpc_weighting && pred_gain[f] < 2) 361 | lpc_weighting(pred_gain[f], a[f]); 362 | 363 | lpc_reflection(a[f], rc[f]); 364 | 365 | quantize_rc(rc[f], &data->rc_order[f], data->rc[f]); 366 | unquantize_rc(data->rc[f], data->rc_order[f], rc[f]); 367 | } 368 | 369 | forward_filtering(dt, bw, data->rc_order, rc, x); 370 | } 371 | 372 | /** 373 | * TNS synthesis 374 | */ 375 | void lc3_tns_synthesize(enum lc3_dt dt, enum lc3_bandwidth bw, 376 | const struct lc3_tns_data *data, float *x) 377 | { 378 | float rc[2][8] = { }; 379 | 380 | for (int f = 0; f < data->nfilters; f++) 381 | if (data->rc_order[f]) 382 | unquantize_rc(data->rc[f], data->rc_order[f], rc[f]); 383 | 384 | inverse_filtering(dt, bw, data->rc_order, rc, x); 385 | } 386 | 387 | /** 388 | * Bit consumption of bitstream data 389 | */ 390 | int lc3_tns_get_nbits(const struct lc3_tns_data *data) 391 | { 392 | int nbits = 0; 393 | 394 | for (int f = 0; f < data->nfilters; f++) { 395 | 396 | int nbits_2048 = 2048; 397 | int rc_order = data->rc_order[f]; 398 | 399 | nbits_2048 += rc_order > 0 ? lc3_tns_order_bits 400 | [data->lpc_weighting][rc_order-1] : 0; 401 | 402 | for (int i = 0; i < rc_order; i++) 403 | nbits_2048 += lc3_tns_coeffs_bits[i][8 + data->rc[f][i]]; 404 | 405 | nbits += (nbits_2048 + (1 << 11) - 1) >> 11; 406 | } 407 | 408 | return nbits; 409 | } 410 | 411 | /** 412 | * Put bitstream data 413 | */ 414 | void lc3_tns_put_data(lc3_bits_t *bits, const struct lc3_tns_data *data) 415 | { 416 | for (int f = 0; f < data->nfilters; f++) { 417 | int rc_order = data->rc_order[f]; 418 | 419 | lc3_put_bits(bits, rc_order > 0, 1); 420 | if (rc_order <= 0) 421 | continue; 422 | 423 | lc3_put_symbol(bits, 424 | lc3_tns_order_models + data->lpc_weighting, rc_order-1); 425 | 426 | for (int i = 0; i < rc_order; i++) 427 | lc3_put_symbol(bits, 428 | lc3_tns_coeffs_models + i, 8 + data->rc[f][i]); 429 | } 430 | } 431 | 432 | /** 433 | * Get bitstream data 434 | */ 435 | void lc3_tns_get_data(lc3_bits_t *bits, 436 | enum lc3_dt dt, enum lc3_bandwidth bw, int nbytes, lc3_tns_data_t *data) 437 | { 438 | data->nfilters = 1 + (bw >= LC3_BANDWIDTH_SWB); 439 | data->lpc_weighting = resolve_lpc_weighting(dt, nbytes); 440 | 441 | for (int f = 0; f < data->nfilters; f++) { 442 | 443 | data->rc_order[f] = lc3_get_bit(bits); 444 | if (!data->rc_order[f]) 445 | continue; 446 | 447 | data->rc_order[f] += lc3_get_symbol(bits, 448 | lc3_tns_order_models + data->lpc_weighting); 449 | 450 | for (int i = 0; i < data->rc_order[f]; i++) 451 | data->rc[f][i] = (int)lc3_get_symbol(bits, 452 | lc3_tns_coeffs_models + i) - 8; 453 | } 454 | } 455 | -------------------------------------------------------------------------------- /src/lc3.c: -------------------------------------------------------------------------------- 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 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 | float *xs = encoder->xs; 160 | int ns = LC3_NS(dt, sr); 161 | 162 | for (int i = 0; i < ns; i++) 163 | xs[i] = pcm[i*stride]; 164 | } 165 | 166 | /** 167 | * Input PCM Samples from signed 24 bits 168 | * encoder Encoder state 169 | * pcm, stride Input PCM samples, and count between two consecutives 170 | */ 171 | static void load_s24( 172 | struct lc3_encoder *encoder, const void *_pcm, int stride) 173 | { 174 | const int32_t *pcm = _pcm; 175 | 176 | enum lc3_dt dt = encoder->dt; 177 | enum lc3_srate sr = encoder->sr_pcm; 178 | float *xs = encoder->xs; 179 | int ns = LC3_NS(dt, sr); 180 | 181 | for (int i = 0; i < ns; i++) 182 | xs[i] = ldexpf(pcm[i*stride], -8); 183 | } 184 | 185 | /** 186 | * Frame Analysis 187 | * encoder Encoder state 188 | * nbytes Size in bytes of the frame 189 | * side, xq Return frame data 190 | */ 191 | static void analyze(struct lc3_encoder *encoder, 192 | int nbytes, struct side_data *side, int16_t *xq) 193 | { 194 | enum lc3_dt dt = encoder->dt; 195 | enum lc3_srate sr = encoder->sr; 196 | enum lc3_srate sr_pcm = encoder->sr_pcm; 197 | int ns = LC3_NS(dt, sr_pcm); 198 | int nd = LC3_ND(dt, sr_pcm); 199 | 200 | float *xs = encoder->xs; 201 | float *xf = encoder->xf; 202 | 203 | /* --- Temporal --- */ 204 | 205 | bool att = lc3_attdet_run(dt, sr_pcm, nbytes, &encoder->attdet, xs); 206 | 207 | side->pitch_present = 208 | lc3_ltpf_analyse(dt, sr_pcm, &encoder->ltpf, xs, &side->ltpf); 209 | 210 | /* --- Spectral --- */ 211 | 212 | float e[LC3_NUM_BANDS]; 213 | 214 | lc3_mdct_forward(dt, sr_pcm, sr, xs, xf); 215 | memmove(xs - nd, xs + ns-nd, nd * sizeof(float)); 216 | 217 | bool nn_flag = lc3_energy_compute(dt, sr, xf, e); 218 | if (nn_flag) 219 | lc3_ltpf_disable(&side->ltpf); 220 | 221 | side->bw = lc3_bwdet_run(dt, sr, e); 222 | 223 | lc3_sns_analyze(dt, sr, e, att, &side->sns, xf, xf); 224 | 225 | lc3_tns_analyze(dt, side->bw, nn_flag, nbytes, &side->tns, xf); 226 | 227 | lc3_spec_analyze(dt, sr, 228 | nbytes, side->pitch_present, &side->tns, 229 | &encoder->spec, xf, xq, &side->spec); 230 | } 231 | 232 | /** 233 | * Encode bitstream 234 | * encoder Encoder state 235 | * side, xq The frame data 236 | * nbytes Target size of the frame (20 to 400) 237 | * buffer Output bitstream buffer of `nbytes` size 238 | */ 239 | static void encode(struct lc3_encoder *encoder, 240 | const struct side_data *side, int16_t *xq, int nbytes, void *buffer) 241 | { 242 | enum lc3_dt dt = encoder->dt; 243 | enum lc3_srate sr = encoder->sr; 244 | enum lc3_bandwidth bw = side->bw; 245 | float *xf = encoder->xf; 246 | 247 | lc3_bits_t bits; 248 | 249 | lc3_setup_bits(&bits, LC3_BITS_MODE_WRITE, buffer, nbytes); 250 | 251 | lc3_bwdet_put_bw(&bits, sr, bw); 252 | 253 | lc3_spec_put_side(&bits, dt, sr, &side->spec); 254 | 255 | lc3_tns_put_data(&bits, &side->tns); 256 | 257 | lc3_put_bit(&bits, side->pitch_present); 258 | 259 | lc3_sns_put_data(&bits, &side->sns); 260 | 261 | if (side->pitch_present) 262 | lc3_ltpf_put_data(&bits, &side->ltpf); 263 | 264 | lc3_spec_encode(&bits, 265 | dt, sr, bw, nbytes, xq, &side->spec, xf); 266 | 267 | lc3_flush_bits(&bits); 268 | } 269 | 270 | /** 271 | * Return size needed for an encoder 272 | */ 273 | unsigned lc3_encoder_size(int dt_us, int sr_hz) 274 | { 275 | if (resolve_dt(dt_us) >= LC3_NUM_DT || 276 | resolve_sr(sr_hz) >= LC3_NUM_SRATE) 277 | return 0; 278 | 279 | return sizeof(struct lc3_encoder) + 280 | LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) * sizeof(float); 281 | } 282 | 283 | /** 284 | * Setup encoder 285 | */ 286 | struct lc3_encoder *lc3_setup_encoder( 287 | int dt_us, int sr_hz, int sr_pcm_hz, void *mem) 288 | { 289 | if (sr_pcm_hz <= 0) 290 | sr_pcm_hz = sr_hz; 291 | 292 | enum lc3_dt dt = resolve_dt(dt_us); 293 | enum lc3_srate sr = resolve_sr(sr_hz); 294 | enum lc3_srate sr_pcm = resolve_sr(sr_pcm_hz); 295 | 296 | if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem) 297 | return NULL; 298 | 299 | struct lc3_encoder *encoder = mem; 300 | int ns = LC3_NS(dt, sr_pcm); 301 | int nd = LC3_ND(dt, sr_pcm); 302 | 303 | *encoder = (struct lc3_encoder){ 304 | .dt = dt, .sr = sr, 305 | .sr_pcm = sr_pcm, 306 | .xs = encoder->s + nd, 307 | .xf = encoder->s + nd+ns, 308 | }; 309 | 310 | memset(encoder->s, 0, 311 | LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float)); 312 | 313 | return encoder; 314 | } 315 | 316 | /** 317 | * Encode a frame 318 | */ 319 | int lc3_encode(struct lc3_encoder *encoder, enum lc3_pcm_format fmt, 320 | const void *pcm, int stride, int nbytes, void *out) 321 | { 322 | static void (* const load[])(struct lc3_encoder *, const void *, int) = { 323 | [LC3_PCM_FORMAT_S16] = load_s16, 324 | [LC3_PCM_FORMAT_S24] = load_s24, 325 | }; 326 | 327 | /* --- Check parameters --- */ 328 | 329 | if (!encoder || nbytes < LC3_MIN_FRAME_BYTES 330 | || nbytes > LC3_MAX_FRAME_BYTES) 331 | return -1; 332 | 333 | /* --- Processing --- */ 334 | 335 | struct side_data side; 336 | int16_t xq[LC3_NE(encoder->dt, encoder->sr)]; 337 | 338 | load[fmt](encoder, pcm, stride); 339 | 340 | analyze(encoder, nbytes, &side, xq); 341 | 342 | encode(encoder, &side, xq, nbytes, out); 343 | 344 | return 0; 345 | } 346 | 347 | 348 | /* ---------------------------------------------------------------------------- 349 | * Decoder 350 | * -------------------------------------------------------------------------- */ 351 | 352 | /** 353 | * Output PCM Samples to signed 16 bits 354 | * decoder Decoder state 355 | * pcm, stride Output PCM samples, and count between two consecutives 356 | */ 357 | static void store_s16( 358 | struct lc3_decoder *decoder, void *_pcm, int stride) 359 | { 360 | int16_t *pcm = _pcm; 361 | 362 | enum lc3_dt dt = decoder->dt; 363 | enum lc3_srate sr = decoder->sr_pcm; 364 | float *xs = decoder->xs; 365 | int ns = LC3_NS(dt, sr); 366 | 367 | for ( ; ns > 0; ns--, xs++, pcm += stride) { 368 | int s = *xs >= 0 ? (int)(*xs + 0.5f) : (int)(*xs - 0.5f); 369 | *pcm = LC3_CLIP(s, INT16_MIN, INT16_MAX); 370 | } 371 | } 372 | 373 | /** 374 | * Output PCM Samples to signed 24 bits 375 | * decoder Decoder state 376 | * pcm, stride Output PCM samples, and count between two consecutives 377 | */ 378 | static void store_s24( 379 | struct lc3_decoder *decoder, void *_pcm, int stride) 380 | { 381 | int32_t *pcm = _pcm; 382 | const int32_t int24_max = (1 << 23) - 1; 383 | const int32_t int24_min = -(1 << 23); 384 | 385 | enum lc3_dt dt = decoder->dt; 386 | enum lc3_srate sr = decoder->sr_pcm; 387 | float *xs = decoder->xs; 388 | int ns = LC3_NS(dt, sr); 389 | 390 | for ( ; ns > 0; ns--, xs++, pcm += stride) { 391 | int32_t s = *xs >= 0 ? (int32_t)(ldexpf(*xs, 8) + 0.5f) 392 | : (int32_t)(ldexpf(*xs, 8) - 0.5f); 393 | *pcm = LC3_CLIP(s, int24_min, int24_max); 394 | } 395 | } 396 | 397 | /** 398 | * Decode bitstream 399 | * decoder Decoder state 400 | * data, nbytes Input bitstream buffer 401 | * side Return the side data 402 | * return 0: Ok < 0: Bitsream error detected 403 | */ 404 | static int decode(struct lc3_decoder *decoder, 405 | const void *data, int nbytes, struct side_data *side) 406 | { 407 | enum lc3_dt dt = decoder->dt; 408 | enum lc3_srate sr = decoder->sr; 409 | float *xf = decoder->xs; 410 | int ns = LC3_NS(dt, sr); 411 | int ne = LC3_NE(dt, sr); 412 | 413 | lc3_bits_t bits; 414 | int ret = 0; 415 | 416 | lc3_setup_bits(&bits, LC3_BITS_MODE_READ, (void *)data, nbytes); 417 | 418 | if ((ret = lc3_bwdet_get_bw(&bits, sr, &side->bw)) < 0) 419 | return ret; 420 | 421 | if ((ret = lc3_spec_get_side(&bits, dt, sr, &side->spec)) < 0) 422 | return ret; 423 | 424 | lc3_tns_get_data(&bits, dt, side->bw, nbytes, &side->tns); 425 | 426 | side->pitch_present = lc3_get_bit(&bits); 427 | 428 | if ((ret = lc3_sns_get_data(&bits, &side->sns)) < 0) 429 | return ret; 430 | 431 | if (side->pitch_present) 432 | lc3_ltpf_get_data(&bits, &side->ltpf); 433 | 434 | if ((ret = lc3_spec_decode(&bits, dt, sr, 435 | side->bw, nbytes, &side->spec, xf)) < 0) 436 | return ret; 437 | 438 | memset(xf + ne, 0, (ns - ne) * sizeof(float)); 439 | 440 | return lc3_check_bits(&bits); 441 | } 442 | 443 | /** 444 | * Frame synthesis 445 | * decoder Decoder state 446 | * side Frame data, NULL performs PLC 447 | * nbytes Size in bytes of the frame 448 | */ 449 | static void synthesize(struct lc3_decoder *decoder, 450 | const struct side_data *side, int nbytes) 451 | { 452 | enum lc3_dt dt = decoder->dt; 453 | enum lc3_srate sr = decoder->sr; 454 | enum lc3_srate sr_pcm = decoder->sr_pcm; 455 | int ns = LC3_NS(dt, sr_pcm); 456 | int nh = LC3_NH(sr_pcm); 457 | 458 | float *xf = decoder->xs; 459 | float *xg = decoder->xg; 460 | float *xd = decoder->xd; 461 | float *xs = xf; 462 | 463 | if (side) { 464 | enum lc3_bandwidth bw = side->bw; 465 | 466 | lc3_plc_suspend(&decoder->plc); 467 | 468 | lc3_tns_synthesize(dt, bw, &side->tns, xf); 469 | 470 | lc3_sns_synthesize(dt, sr, &side->sns, xf, xg); 471 | 472 | lc3_mdct_inverse(dt, sr_pcm, sr, xg, xd, xs); 473 | 474 | } else { 475 | lc3_plc_synthesize(dt, sr, &decoder->plc, xg, xf); 476 | 477 | lc3_mdct_inverse(dt, sr_pcm, sr, xf, xd, xs); 478 | } 479 | 480 | lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf, 481 | side && side->pitch_present ? &side->ltpf : NULL, xs); 482 | 483 | memmove(xs - nh, xs - nh+ns, nh * sizeof(*xs)); 484 | } 485 | 486 | /** 487 | * Return size needed for a decoder 488 | */ 489 | unsigned lc3_decoder_size(int dt_us, int sr_hz) 490 | { 491 | if (resolve_dt(dt_us) >= LC3_NUM_DT || 492 | resolve_sr(sr_hz) >= LC3_NUM_SRATE) 493 | return 0; 494 | 495 | return sizeof(struct lc3_decoder) + 496 | LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) * sizeof(float); 497 | } 498 | 499 | /** 500 | * Setup decoder 501 | */ 502 | struct lc3_decoder *lc3_setup_decoder( 503 | int dt_us, int sr_hz, int sr_pcm_hz, void *mem) 504 | { 505 | if (sr_pcm_hz <= 0) 506 | sr_pcm_hz = sr_hz; 507 | 508 | enum lc3_dt dt = resolve_dt(dt_us); 509 | enum lc3_srate sr = resolve_sr(sr_hz); 510 | enum lc3_srate sr_pcm = resolve_sr(sr_pcm_hz); 511 | 512 | if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem) 513 | return NULL; 514 | 515 | struct lc3_decoder *decoder = mem; 516 | int nh = LC3_NH(sr_pcm); 517 | int ns = LC3_NS(dt, sr_pcm); 518 | int nd = LC3_ND(dt, sr_pcm); 519 | 520 | *decoder = (struct lc3_decoder){ 521 | .dt = dt, .sr = sr, 522 | .sr_pcm = sr_pcm, 523 | .xs = decoder->s + nh, 524 | .xd = decoder->s + nh+ns, 525 | .xg = decoder->s + nh+ns+nd, 526 | }; 527 | 528 | lc3_plc_reset(&decoder->plc); 529 | 530 | memset(decoder->s, 0, 531 | LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float)); 532 | 533 | return decoder; 534 | } 535 | 536 | /** 537 | * Decode a frame 538 | */ 539 | int lc3_decode(struct lc3_decoder *decoder, const void *in, int nbytes, 540 | enum lc3_pcm_format fmt, void *pcm, int stride) 541 | { 542 | static void (* const store[])(struct lc3_decoder *, void *, int) = { 543 | [LC3_PCM_FORMAT_S16] = store_s16, 544 | [LC3_PCM_FORMAT_S24] = store_s24, 545 | }; 546 | 547 | /* --- Check parameters --- */ 548 | 549 | if (!decoder) 550 | return -1; 551 | 552 | if (in && (nbytes < LC3_MIN_FRAME_BYTES || 553 | nbytes > LC3_MAX_FRAME_BYTES )) 554 | return -1; 555 | 556 | /* --- Processing --- */ 557 | 558 | struct side_data side; 559 | 560 | int ret = !in || (decode(decoder, in, nbytes, &side) < 0); 561 | 562 | synthesize(decoder, ret ? NULL : &side, nbytes); 563 | 564 | store[fmt](decoder, pcm, stride); 565 | 566 | return ret; 567 | } 568 | -------------------------------------------------------------------------------- /src/mdct.c: -------------------------------------------------------------------------------- 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 "tables.h" 20 | 21 | 22 | /* ---------------------------------------------------------------------------- 23 | * FFT processing 24 | * -------------------------------------------------------------------------- */ 25 | 26 | /** 27 | * FFT 5 Points template 28 | * s -1: Forward 1: Inverse 29 | * x, y Input and output coefficients, of size 5xn 30 | * n Number of interleaved transform to perform 31 | */ 32 | static inline void xfft_5(const float s, 33 | const struct lc3_complex *x, struct lc3_complex *y, int n) 34 | { 35 | static const float cos1 = 0.3090169944; /* cos(-2Pi 1/5) */ 36 | static const float cos2 = -0.8090169944; /* cos(-2Pi 2/5) */ 37 | 38 | static const float sin1 = -0.9510565163; /* sin(-2Pi 1/5) */ 39 | static const float sin2 = -0.5877852523; /* sin(-2Pi 2/5) */ 40 | 41 | for (int i = 0; i < n; i++, x++, y+= 5) { 42 | 43 | struct lc3_complex s14 = 44 | { x[1*n].re + x[4*n].re, x[1*n].im + x[4*n].im }; 45 | struct lc3_complex d14 = 46 | { x[1*n].re - x[4*n].re, x[1*n].im - x[4*n].im }; 47 | 48 | struct lc3_complex s23 = 49 | { x[2*n].re + x[3*n].re, x[2*n].im + x[3*n].im }; 50 | struct lc3_complex d23 = 51 | { x[2*n].re - x[3*n].re, x[2*n].im - x[3*n].im }; 52 | 53 | y[0].re = x[0].re + s14.re + s23.re; 54 | y[0].im = x[0].im + s14.im + s23.im; 55 | 56 | y[1].re = x[0].re + s14.re * cos1 + s * d14.im * sin1 57 | + s23.re * cos2 + s * d23.im * sin2; 58 | 59 | y[1].im = x[0].im + s14.im * cos1 - s * d14.re * sin1 60 | + s23.im * cos2 - s * d23.re * sin2; 61 | 62 | y[2].re = x[0].re + s14.re * cos2 + s * d14.im * sin2 63 | + s23.re * cos1 - s * d23.im * sin1; 64 | 65 | y[2].im = x[0].im + s14.im * cos2 - s * d14.re * sin2 66 | + s23.im * cos1 + s * d23.re * sin1; 67 | 68 | y[3].re = x[0].re + s14.re * cos2 - s * d14.im * sin2 69 | + s23.re * cos1 + s * d23.im * sin1; 70 | 71 | y[3].im = x[0].im + s14.im * cos2 + s * d14.re * sin2 72 | + s23.im * cos1 - s * d23.re * sin1; 73 | 74 | y[4].re = x[0].re + s14.re * cos1 - s * d14.im * sin1 75 | + s23.re * cos2 - s * d23.im * sin2; 76 | 77 | y[4].im = x[0].im + s14.im * cos1 + s * d14.re * sin1 78 | + s23.im * cos2 + s * d23.re * sin2; 79 | } 80 | } 81 | 82 | /** 83 | * FFT Butterfly 3 Points template 84 | * s -1: Forward 1: Inverse 85 | * x, y Input and output coefficients 86 | * twiddles Twiddles factors, determine size of transform 87 | * n Number of interleaved transforms 88 | */ 89 | static inline void xfft_bf3( 90 | const float s, const struct lc3_fft_bf3_twiddles *twiddles, 91 | const struct lc3_complex *x, struct lc3_complex *y, int n) 92 | { 93 | int n3 = twiddles->n3; 94 | const struct lc3_complex (*w0)[2] = twiddles->t; 95 | const struct lc3_complex (*w1)[2] = w0 + n3, (*w2)[2] = w1 + n3; 96 | 97 | const struct lc3_complex *x0 = x, *x1 = x0 + n*n3, *x2 = x1 + n*n3; 98 | struct lc3_complex *y0 = y, *y1 = y0 + n3, *y2 = y1 + n3; 99 | 100 | for (int i = 0; i < n; i++, y0 += 3*n3, y1 += 3*n3, y2 += 3*n3) { 101 | 102 | for (int j = 0; j < n3; j++, x0++, x1++, x2++) { 103 | 104 | y0[j].re = x0->re + x1->re * w0[j][0].re + s * x1->im * w0[j][0].im 105 | + x2->re * w0[j][1].re + s * x2->im * w0[j][1].im; 106 | 107 | y0[j].im = x0->im + x1->im * w0[j][0].re - s * x1->re * w0[j][0].im 108 | + x2->im * w0[j][1].re - s * x2->re * w0[j][1].im; 109 | 110 | y1[j].re = x0->re + x1->re * w1[j][0].re + s * x1->im * w1[j][0].im 111 | + x2->re * w1[j][1].re + s * x2->im * w1[j][1].im; 112 | 113 | y1[j].im = x0->im + x1->im * w1[j][0].re - s * x1->re * w1[j][0].im 114 | + x2->im * w1[j][1].re - s * x2->re * w1[j][1].im; 115 | 116 | y2[j].re = x0->re + x1->re * w2[j][0].re + s * x1->im * w2[j][0].im 117 | + x2->re * w2[j][1].re + s * x2->im * w2[j][1].im; 118 | 119 | y2[j].im = x0->im + x1->im * w2[j][0].re - s * x1->re * w2[j][0].im 120 | + x2->im * w2[j][1].re - s * x2->re * w2[j][1].im; 121 | } 122 | } 123 | } 124 | 125 | /** 126 | * FFT Butterfly 2 Points template 127 | * s -1: Forward 1: Inverse 128 | * twiddles Twiddles factors, determine size of transform 129 | * x, y Input and output coefficients 130 | * n Number of interleaved transforms 131 | */ 132 | static inline void xfft_bf2( 133 | const float s, const struct lc3_fft_bf2_twiddles *twiddles, 134 | const struct lc3_complex *x, struct lc3_complex *y, int n) 135 | { 136 | int n2 = twiddles->n2; 137 | const struct lc3_complex *w = twiddles->t; 138 | 139 | const struct lc3_complex *x0 = x, *x1 = x0 + n*n2; 140 | struct lc3_complex *y0 = y, *y1 = y0 + n2; 141 | 142 | for (int i = 0; i < n; i++, y0 += 2*n2, y1 += 2*n2) { 143 | 144 | for (int j = 0; j < n2; j++, x0++, x1++) { 145 | 146 | y0[j].re = x0->re + x1->re * w[j].re + s * x1->im * w[j].im; 147 | y0[j].im = x0->im + x1->im * w[j].re - s * x1->re * w[j].im; 148 | 149 | y1[j].re = x0->re - x1->re * w[j].re - s * x1->im * w[j].im; 150 | y1[j].im = x0->im - x1->im * w[j].re + s * x1->re * w[j].im; 151 | } 152 | } 153 | } 154 | 155 | /** 156 | * Forward FFT 5 Points 157 | * x, y Input and output coefficients, of size 5xn 158 | * n Number of interleaved transform to perform 159 | */ 160 | static void ffft_5(const struct lc3_complex *x, struct lc3_complex *y, int n) 161 | { 162 | xfft_5(-1, x, y, n); 163 | } 164 | 165 | /** 166 | * Inverse FFT 5 Points 167 | * x, y Input and output coefficients, of size 5xn 168 | * n Number of interleaved transform to perform 169 | */ 170 | static void ifft_5(const struct lc3_complex *x, struct lc3_complex *y, int n) 171 | { 172 | xfft_5(1, x, y, n); 173 | } 174 | 175 | /** 176 | * Forward FFT Butterfly 3 Points 177 | * twiddles Twiddles factors, determine size of transform 178 | * x, y Input and output coefficients 179 | * n Number of interleaved transforms 180 | */ 181 | static void ffft_bf3(const struct lc3_fft_bf3_twiddles *twiddles, 182 | const struct lc3_complex *x, struct lc3_complex *y, int n) 183 | { 184 | xfft_bf3(-1, twiddles, x, y, n); 185 | } 186 | 187 | /** 188 | * Inverse FFT Butterfly 3 Points 189 | * twiddles Twiddles factors, determine size of transform 190 | * x, y Input and output coefficients 191 | * n Number of interleaved transforms 192 | */ 193 | static void ifft_bf3(const struct lc3_fft_bf3_twiddles *twiddles, 194 | const struct lc3_complex *x, struct lc3_complex *y, int n) 195 | { 196 | xfft_bf3(1, twiddles, x, y, n); 197 | } 198 | 199 | /** 200 | * Forward FFT Butterfly 2 Points 201 | * twiddles Twiddles factors, determine size of transform 202 | * x, y Input and output coefficients 203 | * n Number of interleaved transforms 204 | */ 205 | static void ffft_bf2(const struct lc3_fft_bf2_twiddles *twiddles, 206 | const struct lc3_complex *x, struct lc3_complex *y, int n) 207 | { 208 | xfft_bf2(-1, twiddles, x, y, n); 209 | } 210 | 211 | /** 212 | * InverseIFFT Butterfly 2 Points 213 | * twiddles Twiddles factors, determine size of transform 214 | * x, y Input and output coefficients 215 | * n Number of interleaved transforms 216 | */ 217 | static void ifft_bf2(const struct lc3_fft_bf2_twiddles *twiddles, 218 | const struct lc3_complex *x, struct lc3_complex *y, int n) 219 | { 220 | xfft_bf2(1, twiddles, x, y, n); 221 | } 222 | 223 | /** 224 | * Perform FFT 225 | * inverse True on inverse transform else forward 226 | * x, y0, y1 Input, and 2 scratch buffers of size `n` 227 | * n Number of points 30, 40, 60, 80, 90, 120, 160, 180, 240 228 | * return The buffer `y0` or `y1` that hold the result 229 | * 230 | * Input `x` can be the same as the `y0` second scratch buffer 231 | */ 232 | static struct lc3_complex *fft( 233 | bool inverse, const struct lc3_complex *x, int n, 234 | struct lc3_complex *y0, struct lc3_complex *y1) 235 | { 236 | struct lc3_complex *y[2] = { y1, y0 }; 237 | int i2, i3, is = 0; 238 | 239 | /* The number of points `n` can be decomposed as : 240 | * 241 | * n = 5^1 * 3^n3 * 2^n2 242 | * 243 | * for n = 40, 80, 160 n3 = 0, n2 = [3..5] 244 | * n = 30, 60, 120, 240 n3 = 1, n2 = [1..4] 245 | * n = 90, 180 n3 = 2, n2 = [1..2] 246 | * 247 | * Note that the expression `n & (n-1) == 0` is equivalent 248 | * to the check that `n` is a power of 2. */ 249 | 250 | (inverse ? ifft_5 : ffft_5)(x, y[is], n /= 5); 251 | 252 | for (i3 = 0; n & (n-1); i3++, is ^= 1) 253 | (inverse ? ifft_bf3 : ffft_bf3) 254 | (lc3_fft_twiddles_bf3[i3], y[is], y[is ^ 1], n /= 3); 255 | 256 | for (i2 = 0; n > 1; i2++, is ^= 1) 257 | (inverse ? ifft_bf2 : ffft_bf2) 258 | (lc3_fft_twiddles_bf2[i2][i3], y[is], y[is ^ 1], n >>= 1); 259 | 260 | return y[is]; 261 | } 262 | 263 | 264 | /* ---------------------------------------------------------------------------- 265 | * MDCT processing 266 | * -------------------------------------------------------------------------- */ 267 | 268 | /** 269 | * Windowing of samples before MDCT 270 | * dt, sr Duration and samplerate (size of the transform) 271 | * x [-nd..-1] Previous, [0..ns-1] Current samples 272 | * y Output `ns` windowed samples 273 | * 274 | * The number of previous samples `nd` accessed on `x` is : 275 | * nd: `ns` * 23/30 for 7.5ms frame duration 276 | * nd: `ns` * 5/ 8 for 10ms frame duration 277 | */ 278 | static void mdct_window( 279 | enum lc3_dt dt, enum lc3_srate sr, const float *x, float *y) 280 | { 281 | int ns = LC3_NS(dt, sr), nd = LC3_ND(dt, sr); 282 | 283 | const float *w0 = lc3_mdct_win[dt][sr], *w1 = w0 + ns; 284 | const float *w2 = w1, *w3 = w2 + nd; 285 | 286 | const float *x0 = x - nd, *x1 = x0 + ns; 287 | const float *x2 = x1, *x3 = x2 + nd; 288 | 289 | float *y0 = y + ns/2, *y1 = y0; 290 | 291 | while (x0 < x1) 292 | *(--y0) = *(x0++) * *(w0++) - *(--x1) * *(--w1); 293 | 294 | for (const float *xe = x2 + ns-nd; x2 < xe; ) 295 | *(y1++) = *(x2++) * *(w2++); 296 | 297 | while (x2 < x3) 298 | *(y1++) = *(x2++) * *(w2++) + *(--x3) * *(--w3); 299 | } 300 | 301 | /** 302 | * Pre-rotate MDCT coefficients of N/2 points, before FFT N/4 points FFT 303 | * def Size and twiddles factors 304 | * x, y Input and output coefficients 305 | * 306 | * `x` and y` can be the same buffer 307 | */ 308 | static void mdct_pre_fft(const struct lc3_mdct_rot_def *def, 309 | const float *x, struct lc3_complex *y) 310 | { 311 | int n4 = def->n4; 312 | 313 | const float *x0 = x, *x1 = x0 + 2*n4; 314 | const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; 315 | struct lc3_complex *y0 = y, *y1 = y0 + n4; 316 | 317 | while (x0 < x1) { 318 | struct lc3_complex u, uw = *(w0++); 319 | u.re = - *(--x1) * uw.re + *x0 * uw.im; 320 | u.im = *(x0++) * uw.re + *x1 * uw.im; 321 | 322 | struct lc3_complex v, vw = *(--w1); 323 | v.re = - *(--x1) * vw.im + *x0 * vw.re; 324 | v.im = - *(x0++) * vw.im - *x1 * vw.re; 325 | 326 | *(y0++) = u; 327 | *(--y1) = v; 328 | } 329 | } 330 | 331 | /** 332 | * Post-rotate FFT N/4 points coefficients, resulting MDCT N points 333 | * def Size and twiddles factors 334 | * x, y Input and output coefficients 335 | * scale Scale on output coefficients 336 | * 337 | * `x` and y` can be the same buffer 338 | */ 339 | static void mdct_post_fft(const struct lc3_mdct_rot_def *def, 340 | const struct lc3_complex *x, float *y, float scale) 341 | { 342 | int n4 = def->n4, n8 = n4 >> 1; 343 | 344 | const struct lc3_complex *w0 = def->w + n8, *w1 = w0 - 1; 345 | const struct lc3_complex *x0 = x + n8, *x1 = x0 - 1; 346 | 347 | float *y0 = y + n4, *y1 = y0; 348 | 349 | for ( ; y1 > y; x0++, x1--, w0++, w1--) { 350 | 351 | float u0 = (x0->im * w0->im + x0->re * w0->re) * scale; 352 | float u1 = (x1->re * w1->im - x1->im * w1->re) * scale; 353 | 354 | float v0 = (x0->re * w0->im - x0->im * w0->re) * scale; 355 | float v1 = (x1->im * w1->im + x1->re * w1->re) * scale; 356 | 357 | *(y0++) = u0; *(y0++) = u1; 358 | *(--y1) = v0; *(--y1) = v1; 359 | } 360 | } 361 | 362 | /** 363 | * Pre-rotate IMDCT coefficients of N points, before FFT N/4 points FFT 364 | * def Size and twiddles factors 365 | * x, y Input and output coefficients 366 | * 367 | * `x` and y` can be the same buffer 368 | */ 369 | static void imdct_pre_fft(const struct lc3_mdct_rot_def *def, 370 | const float *x, struct lc3_complex *y) 371 | { 372 | int n4 = def->n4; 373 | 374 | const float *x0 = x, *x1 = x0 + 2*n4; 375 | 376 | const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; 377 | struct lc3_complex *y0 = y, *y1 = y0 + n4; 378 | 379 | while (x0 < x1) { 380 | float u0 = *(x0++), u1 = *(--x1); 381 | float v0 = *(x0++), v1 = *(--x1); 382 | struct lc3_complex uw = *(w0++), vw = *(--w1); 383 | 384 | (y0 )->re = - u1 * uw.re + u0 * uw.im; 385 | (y0++)->im = - u0 * uw.re - u1 * uw.im; 386 | 387 | (--y1)->re = - v0 * vw.re + v1 * vw.im; 388 | ( y1)->im = - v1 * vw.re - v0 * vw.im; 389 | } 390 | } 391 | 392 | /** 393 | * Post-rotate FFT N/4 points coefficients, resulting IMDCT N points 394 | * def Size and twiddles factors 395 | * x, y Input and output coefficients 396 | * scale Scale on output coefficients 397 | * 398 | * `x` and y` can be the same buffer 399 | */ 400 | static void imdct_post_fft(const struct lc3_mdct_rot_def *def, 401 | const struct lc3_complex *x, float *y, float scale) 402 | { 403 | int n4 = def->n4; 404 | 405 | const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; 406 | const struct lc3_complex *x0 = x, *x1 = x0 + n4; 407 | 408 | float *y0 = y, *y1 = y0 + 2*n4; 409 | 410 | while (x0 < x1) { 411 | struct lc3_complex uz = *(x0++), vz = *(--x1); 412 | struct lc3_complex uw = *(w0++), vw = *(--w1); 413 | 414 | *(y0++) = (uz.im * uw.im - uz.re * uw.re) * scale; 415 | *(--y1) = (uz.im * uw.re + uz.re * uw.im) * scale; 416 | 417 | *(--y1) = (vz.im * vw.im - vz.re * vw.re) * scale; 418 | *(y0++) = (vz.im * vw.re + vz.re * vw.im) * scale; 419 | } 420 | } 421 | 422 | /** 423 | * Apply windowing of samples 424 | * dt, sr Duration and samplerate 425 | * x, d Middle half of IMDCT coefficients and delayed samples 426 | * y, d Output samples and delayed ones 427 | */ 428 | static void imdct_window(enum lc3_dt dt, enum lc3_srate sr, 429 | const float *x, float *d, float *y) 430 | { 431 | /* The full MDCT coefficients is given by symmetry : 432 | * T[ 0 .. n/4-1] = -half[n/4-1 .. 0 ] 433 | * T[ n/4 .. n/2-1] = half[0 .. n/4-1] 434 | * T[ n/2 .. 3n/4-1] = half[n/4 .. n/2-1] 435 | * T[3n/4 .. n-1] = half[n/2-1 .. n/4 ] */ 436 | 437 | int n4 = LC3_NS(dt, sr) >> 1, nd = LC3_ND(dt, sr); 438 | const float *w2 = lc3_mdct_win[dt][sr], *w0 = w2 + 3*n4, *w1 = w0; 439 | 440 | const float *x0 = d + nd-n4, *x1 = x0; 441 | float *y0 = y + nd-n4, *y1 = y0, *y2 = d + nd, *y3 = d; 442 | 443 | while (y0 > y) { 444 | *(--y0) = *(--x0) - *(x ) * *(w1++); 445 | *(y1++) = *(x1++) + *(x++) * *(--w0); 446 | } 447 | 448 | while (y1 < y + nd) { 449 | *(y1++) = *(x1++) + *(x++) * *(--w0); 450 | } 451 | 452 | while (y1 < y + 2*n4) { 453 | *(y1++) = *(x ) * *(--w0); 454 | *(--y2) = *(x++) * *(w2++); 455 | } 456 | 457 | while (y2 > y3) { 458 | *(y3++) = *(x ) * *(--w0); 459 | *(--y2) = *(x++) * *(w2++); 460 | } 461 | } 462 | 463 | /** 464 | * Forward MDCT transformation 465 | */ 466 | void lc3_mdct_forward(enum lc3_dt dt, enum lc3_srate sr, 467 | enum lc3_srate sr_dst, const float *x, float *y) 468 | { 469 | const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr]; 470 | int nf = LC3_NS(dt, sr_dst); 471 | int ns = LC3_NS(dt, sr); 472 | 473 | union { float *f; struct lc3_complex *z; } u = { .f = y }; 474 | struct lc3_complex z[ns/2]; 475 | 476 | mdct_window(dt, sr, x, u.f); 477 | 478 | mdct_pre_fft(rot, u.f, u.z); 479 | u.z = fft(false, u.z, ns/2, u.z, z); 480 | mdct_post_fft(rot, u.z, y, sqrtf( (2.f*nf) / (ns*ns) )); 481 | } 482 | 483 | /** 484 | * Inverse MDCT transformation 485 | */ 486 | void lc3_mdct_inverse(enum lc3_dt dt, enum lc3_srate sr, 487 | enum lc3_srate sr_src, const float *x, float *d, float *y) 488 | { 489 | const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr]; 490 | int nf = LC3_NS(dt, sr_src); 491 | int ns = LC3_NS(dt, sr); 492 | 493 | struct lc3_complex buffer[ns/2]; 494 | struct lc3_complex *z = (struct lc3_complex *)y; 495 | union { float *f; struct lc3_complex *z; } u = { .z = buffer }; 496 | 497 | imdct_pre_fft(rot, x, z); 498 | z = fft(true, z, ns/2, z, u.z); 499 | imdct_post_fft(rot, z, u.f, sqrtf(2.f / nf)); 500 | 501 | imdct_window(dt, sr, u.f, d, y); 502 | } 503 | -------------------------------------------------------------------------------- /src/ltpf.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /src/spec.c: -------------------------------------------------------------------------------- 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 "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 | 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 | float e[ne]; 55 | 56 | /* --- Energy (dB) by 4 NDCT blocks --- 57 | * For the next steps, add energy offset 28/20 dB, 58 | * and compute the maximum magnitude */ 59 | 60 | float x_max = 0; 61 | 62 | for (int i = 0; i < ne; i++, x += 4) { 63 | float x0 = fabsf(x[0]), x1 = fabsf(x[1]); 64 | float x2 = fabsf(x[2]), x3 = fabsf(x[3]); 65 | 66 | x_max = fmaxf(x_max, x0); 67 | x_max = fmaxf(x_max, x1); 68 | x_max = fmaxf(x_max, x2); 69 | x_max = fmaxf(x_max, x3); 70 | 71 | float s2 = x0*x0 + x1*x1 + x2*x2 + x3*x3; 72 | e[i] = 28.f/20 * 10 * (s2 > 0 ? log10f(s2) : -10); 73 | } 74 | 75 | /* --- Determine gain index --- */ 76 | 77 | int nbits = nbits_budget + nbits_off + 0.5f; 78 | int g_int = 255 - g_off; 79 | 80 | for (int i = 128; i > 0; i >>= 1) { 81 | const float *e_ptr = e + ne-1; 82 | float v = 0; 83 | 84 | g_int -= i; 85 | 86 | for ( ; e_ptr >= e && *e_ptr < g_int; e_ptr--); 87 | 88 | while (e_ptr >= e) { 89 | float e_diff = *(e_ptr--) - g_int; 90 | 91 | if (e_diff < 0) { 92 | v += 2.7f * 28.f/20; 93 | } else { 94 | v += e_diff + 7 * 28.f/20; 95 | if (e_diff > 43 * 28.f/20) 96 | v += e_diff - 43 * 28.f/20; 97 | } 98 | } 99 | 100 | if (v > nbits * 1.4 * 28./20) 101 | g_int += i; 102 | } 103 | 104 | /* --- Limit gain index --- */ 105 | 106 | int g_min = x_max == 0 ? -g_off : 107 | ceilf(28 * log10f(x_max / (32768 - 0.375f))); 108 | 109 | *reset_off = g_int < g_min || x_max == 0; 110 | if (*reset_off) 111 | g_int = g_min; 112 | 113 | return g_int; 114 | } 115 | 116 | /** 117 | * Global Gain Adjustment 118 | * sr Samplerate of the frame 119 | * g_idx The estimated quantized gain index 120 | * nbits Computed number of bits coding the spectrum 121 | * nbits_budget Number of bits available for coding the spectrum 122 | * return Gain adjust value (-1 to 2) 123 | */ 124 | static int adjust_gain(enum lc3_srate sr, 125 | int g_idx, int nbits, int nbits_budget) 126 | { 127 | /* --- Compute delta threshold --- */ 128 | 129 | const int *t = (const int [LC3_NUM_SRATE][3]){ 130 | { 80, 500, 850 }, { 230, 1025, 1700 }, { 380, 1550, 2550 }, 131 | { 530, 2075, 3400 }, { 680, 2600, 4250 } 132 | }[sr]; 133 | 134 | int delta, den = 48; 135 | 136 | if (nbits < t[0]) { 137 | delta = 3*(nbits + 48); 138 | 139 | } else if (nbits < t[1]) { 140 | int n0 = 3*(t[0] + 48), range = t[1] - t[0]; 141 | delta = n0 * range + (nbits - t[0]) * (t[1] - n0); 142 | den *= range; 143 | 144 | } else { 145 | delta = LC3_MIN(nbits, t[2]); 146 | } 147 | 148 | delta = (delta + den/2) / den; 149 | 150 | /* --- Adjust gain --- */ 151 | 152 | if (nbits < nbits_budget - (delta + 2)) 153 | return -(g_idx > 0); 154 | 155 | if (nbits > nbits_budget) 156 | return (g_idx < 255) + (g_idx < 254 && nbits >= nbits_budget + delta); 157 | 158 | return 0; 159 | } 160 | 161 | /** 162 | * Unquantize gain 163 | * g_int Quantization gain value 164 | * return Unquantized gain value 165 | */ 166 | static float unquantize_gain(int g_int) 167 | { 168 | /* Unquantization gain table : 169 | * G[i] = 10 ^ (i / 28) , i = [0..64] */ 170 | 171 | static const float iq_table[] = { 172 | 1.00000000e+00, 1.08571112e+00, 1.17876863e+00, 1.27980221e+00, 173 | 1.38949549e+00, 1.50859071e+00, 1.63789371e+00, 1.77827941e+00, 174 | 1.93069773e+00, 2.09617999e+00, 2.27584593e+00, 2.47091123e+00, 175 | 2.68269580e+00, 2.91263265e+00, 3.16227766e+00, 3.43332002e+00, 176 | 3.72759372e+00, 4.04708995e+00, 4.39397056e+00, 4.77058270e+00, 177 | 5.17947468e+00, 5.62341325e+00, 6.10540230e+00, 6.62870316e+00, 178 | 7.19685673e+00, 7.81370738e+00, 8.48342898e+00, 9.21055318e+00, 179 | 1.00000000e+01, 1.08571112e+01, 1.17876863e+01, 1.27980221e+01, 180 | 1.38949549e+01, 1.50859071e+01, 1.63789371e+01, 1.77827941e+01, 181 | 1.93069773e+01, 2.09617999e+01, 2.27584593e+01, 2.47091123e+01, 182 | 2.68269580e+01, 2.91263265e+01, 3.16227766e+01, 3.43332002e+01, 183 | 3.72759372e+01, 4.04708995e+01, 4.39397056e+01, 4.77058270e+01, 184 | 5.17947468e+01, 5.62341325e+01, 6.10540230e+01, 6.62870316e+01, 185 | 7.19685673e+01, 7.81370738e+01, 8.48342898e+01, 9.21055318e+01, 186 | 1.00000000e+02, 1.08571112e+02, 1.17876863e+02, 1.27980221e+02, 187 | 1.38949549e+02, 1.50859071e+02, 1.63789371e+02, 1.77827941e+02, 188 | 1.93069773e+02 189 | }; 190 | 191 | float g = iq_table[LC3_ABS(g_int) & 0x3f]; 192 | for(int n64 = LC3_ABS(g_int) >> 6; n64--; ) 193 | g *= iq_table[64]; 194 | 195 | return g_int >= 0 ? g : 1 / g; 196 | } 197 | 198 | /** 199 | * Spectrum quantization 200 | * dt, sr Duration and samplerate of the frame 201 | * g_int Quantization gain value 202 | * x Spectral coefficients, scaled as output 203 | * xq, nq Output spectral quantized coefficients, and count 204 | */ 205 | static void quantize(enum lc3_dt dt, enum lc3_srate sr, 206 | int g_int, float *x, int16_t *xq, int *nq) 207 | { 208 | float g_inv = 1 / unquantize_gain(g_int); 209 | int ne = LC3_NE(dt, sr); 210 | 211 | *nq = ne; 212 | 213 | for (int i = 0; i < ne; i += 2) { 214 | int16_t x0, x1; 215 | 216 | x[i+0] *= g_inv; 217 | x0 = fminf(floorf(fabsf(x[i+0]) + 6.f/16), INT16_MAX); 218 | xq[i+0] = x[i+0] < 0 ? -x0 : x0; 219 | 220 | x[i+1] *= g_inv; 221 | x1 = fminf(floorf(fabsf(x[i+1]) + 6.f/16), INT16_MAX); 222 | xq[i+1] = x[i+1] < 0 ? -x1 : x1; 223 | 224 | *nq = x0 || x1 ? ne : *nq - 2; 225 | } 226 | } 227 | 228 | /** 229 | * Spectrum quantization inverse 230 | * dt, sr Duration and samplerate of the frame 231 | * g_int Quantization gain value 232 | * x, nq Spectral quantized, and count of significants 233 | * return Unquantized gain value 234 | */ 235 | static float unquantize(enum lc3_dt dt, enum lc3_srate sr, 236 | int g_int, float *x, int nq) 237 | { 238 | float g = unquantize_gain(g_int); 239 | int i, ne = LC3_NE(dt, sr); 240 | 241 | for (i = 0; i < nq; i++) 242 | x[i] = x[i] * g; 243 | 244 | for ( ; i < ne; i++) 245 | x[i] = 0; 246 | 247 | return g; 248 | } 249 | 250 | 251 | /* ---------------------------------------------------------------------------- 252 | * Spectrum coding 253 | * -------------------------------------------------------------------------- */ 254 | 255 | /** 256 | * Resolve High-bitrate mode according size of the frame 257 | * sr, nbytes Samplerate and size of the frame 258 | * return True when High-Rate mode enabled 259 | */ 260 | static int resolve_high_rate(enum lc3_srate sr, int nbytes) 261 | { 262 | return nbytes > 20 * (1 + (int)sr); 263 | } 264 | 265 | /** 266 | * Bit consumption 267 | * dt, sr, nbytes Duration, samplerate and size of the frame 268 | * x Spectral quantized coefficients 269 | * n Count of significant coefficients, updated on truncation 270 | * nbits_budget Truncate to stay in budget, when not zero 271 | * p_lsb_mode Return True when LSB's are not AC coded, or NULL 272 | * return The number of bits coding the spectrum 273 | */ 274 | static int compute_nbits( 275 | enum lc3_dt dt, enum lc3_srate sr, int nbytes, 276 | const int16_t *x, int *n, int nbits_budget, bool *p_lsb_mode) 277 | { 278 | int ne = LC3_NE(dt, sr); 279 | 280 | /* --- Mode and rate --- */ 281 | 282 | bool lsb_mode = nbytes >= 20 * (3 + (int)sr); 283 | bool high_rate = resolve_high_rate(sr, nbytes); 284 | 285 | /* --- Loop on quantized coefficients --- */ 286 | 287 | int nbits = 0, nbits_lsb = 0; 288 | uint8_t state = 0; 289 | 290 | int nbits_end = 0; 291 | int n_end = 0; 292 | 293 | nbits_budget = nbits_budget ? nbits_budget * 2048 : INT_MAX; 294 | 295 | for (int i = 0, h = 0; h < 2; h++) { 296 | const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; 297 | 298 | for ( ; i < LC3_MIN(*n, (ne + 2) >> (1 - h)) 299 | && nbits <= nbits_budget; i += 2) { 300 | 301 | const uint8_t *lut = lut_coeff[state]; 302 | int a = LC3_ABS(x[i]), b = LC3_ABS(x[i+1]); 303 | 304 | /* --- Sign values --- */ 305 | 306 | int s = (a != 0) + (b != 0); 307 | nbits += s * 2048; 308 | 309 | /* --- LSB values Reduce to 2*2 bits MSB values --- 310 | * Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic 311 | * coded with an escape code followed by 1 bit for each values. 312 | * The LSB mode does not arthmetic code the first LSB, 313 | * add the sign of the LSB when one of pair was at value 1 */ 314 | 315 | int k = 0; 316 | int m = (a | b) >> 2; 317 | 318 | if (m) { 319 | if (lsb_mode) { 320 | nbits += lc3_spectrum_bits[lut[k++]][16] - 2*2048; 321 | nbits_lsb += 2 + (a == 1) + (b == 1); 322 | } 323 | 324 | for (m >>= lsb_mode; m; m >>= 1, k++) 325 | nbits += lc3_spectrum_bits[lut[LC3_MIN(k, 3)]][16]; 326 | 327 | nbits += k * 2*2048; 328 | a >>= k; 329 | b >>= k; 330 | 331 | k = LC3_MIN(k, 3); 332 | } 333 | 334 | /* --- MSB values --- */ 335 | 336 | nbits += lc3_spectrum_bits[lut[k]][a + 4*b]; 337 | 338 | /* --- Update state --- */ 339 | 340 | if (s && nbits <= nbits_budget) { 341 | n_end = i + 2; 342 | nbits_end = nbits; 343 | } 344 | 345 | state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); 346 | } 347 | } 348 | 349 | /* --- Return --- */ 350 | 351 | *n = n_end; 352 | 353 | if (p_lsb_mode) 354 | *p_lsb_mode = lsb_mode && 355 | nbits_end + nbits_lsb * 2048 > nbits_budget; 356 | 357 | if (nbits_budget >= INT_MAX) 358 | nbits_end += nbits_lsb * 2048; 359 | 360 | return (nbits_end + 2047) / 2048; 361 | } 362 | 363 | /** 364 | * Put quantized spectrum 365 | * bits Bitstream context 366 | * dt, sr, nbytes Duration, samplerate and size of the frame 367 | * x Spectral quantized 368 | * nq, lsb_mode Count of significants, and LSB discard indication 369 | */ 370 | static void put_quantized(lc3_bits_t *bits, 371 | enum lc3_dt dt, enum lc3_srate sr, int nbytes, 372 | const int16_t *x, int nq, bool lsb_mode) 373 | { 374 | int ne = LC3_NE(dt, sr); 375 | bool high_rate = resolve_high_rate(sr, nbytes); 376 | 377 | /* --- Loop on quantized coefficients --- */ 378 | 379 | uint8_t state = 0; 380 | 381 | for (int i = 0, h = 0; h < 2; h++) { 382 | const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; 383 | 384 | for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) { 385 | 386 | const uint8_t *lut = lut_coeff[state]; 387 | bool a_neg = x[i] < 0, b_neg = x[i+1] < 0; 388 | int a = LC3_ABS(x[i]), b = LC3_ABS(x[i+1]); 389 | 390 | /* --- LSB values Reduce to 2*2 bits MSB values --- 391 | * Reduce to 2x2 bits MSB values. The LSB's pair are arithmetic 392 | * coded with an escape code and 1 bits for each values. 393 | * The LSB mode discard the first LSB (at this step) */ 394 | 395 | int m = (a | b) >> 2; 396 | int k = 0, shr = 0; 397 | 398 | if (m) { 399 | 400 | if (lsb_mode) 401 | lc3_put_symbol(bits, 402 | lc3_spectrum_models + lut[k++], 16); 403 | 404 | for (m >>= lsb_mode; m; m >>= 1, k++) { 405 | lc3_put_bit(bits, (a >> k) & 1); 406 | lc3_put_bit(bits, (b >> k) & 1); 407 | lc3_put_symbol(bits, 408 | lc3_spectrum_models + lut[LC3_MIN(k, 3)], 16); 409 | } 410 | 411 | a >>= lsb_mode; 412 | b >>= lsb_mode; 413 | 414 | shr = k - lsb_mode; 415 | k = LC3_MIN(k, 3); 416 | } 417 | 418 | /* --- Sign values --- */ 419 | 420 | if (a) lc3_put_bit(bits, a_neg); 421 | if (b) lc3_put_bit(bits, b_neg); 422 | 423 | /* --- MSB values --- */ 424 | 425 | a >>= shr; 426 | b >>= shr; 427 | 428 | lc3_put_symbol(bits, lc3_spectrum_models + lut[k], a + 4*b); 429 | 430 | /* --- Update state --- */ 431 | 432 | state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); 433 | } 434 | } 435 | } 436 | 437 | /** 438 | * Get quantized spectrum 439 | * bits Bitstream context 440 | * dt, sr, nbytes Duration, samplerate and size of the frame 441 | * nq, lsb_mode Count of significants, and LSB discard indication 442 | * xq Return `nq` spectral quantized coefficients 443 | * nf_seed Return the noise factor seed associated 444 | * return 0: Ok -1: Invalid bitstream data 445 | */ 446 | static int get_quantized(lc3_bits_t *bits, 447 | enum lc3_dt dt, enum lc3_srate sr, int nbytes, 448 | int nq, bool lsb_mode, float *xq, uint16_t *nf_seed) 449 | { 450 | int ne = LC3_NE(dt, sr); 451 | bool high_rate = resolve_high_rate(sr, nbytes); 452 | 453 | *nf_seed = 0; 454 | 455 | /* --- Loop on quantized coefficients --- */ 456 | 457 | uint8_t state = 0; 458 | 459 | for (int i = 0, h = 0; h < 2; h++) { 460 | const uint8_t (*lut_coeff)[4] = lc3_spectrum_lookup[high_rate][h]; 461 | 462 | for ( ; i < LC3_MIN(nq, (ne + 2) >> (1 - h)); i += 2) { 463 | 464 | const uint8_t *lut = lut_coeff[state]; 465 | 466 | /* --- LSB values --- 467 | * Until the symbol read indicates the escape value 16, 468 | * read an LSB bit for each values. 469 | * The LSB mode discard the first LSB (at this step) */ 470 | 471 | int u = 0, v = 0; 472 | int k = 0, shl = 0; 473 | 474 | unsigned s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]); 475 | 476 | if (lsb_mode && s >= 16) { 477 | s = lc3_get_symbol(bits, lc3_spectrum_models + lut[++k]); 478 | shl++; 479 | } 480 | 481 | for ( ; s >= 16 && shl < 14; shl++) { 482 | u |= lc3_get_bit(bits) << shl; 483 | v |= lc3_get_bit(bits) << shl; 484 | 485 | k += (k < 3); 486 | s = lc3_get_symbol(bits, lc3_spectrum_models + lut[k]); 487 | } 488 | 489 | if (s >= 16) 490 | return -1; 491 | 492 | /* --- MSB & sign values --- */ 493 | 494 | int a = s % 4; 495 | int b = s / 4; 496 | 497 | u |= a << shl; 498 | v |= b << shl; 499 | 500 | xq[i ] = u && lc3_get_bit(bits) ? -u : u; 501 | xq[i+1] = v && lc3_get_bit(bits) ? -v : v; 502 | 503 | *nf_seed = (*nf_seed + u * i + v * (i+1)) & 0xffff; 504 | 505 | /* --- Update state --- */ 506 | 507 | state = (state << 4) + (k > 1 ? 12 + k : 1 + (a + b) * (k + 1)); 508 | } 509 | } 510 | 511 | return 0; 512 | } 513 | 514 | /** 515 | * Put residual bits of quantization 516 | * bits Bitstream context 517 | * nbits Maximum number of bits to output 518 | * xq, n Spectral quantized, and count of significants 519 | * xf Scaled spectral coefficients 520 | */ 521 | static void put_residual(lc3_bits_t *bits, int nbits, 522 | const int16_t *xq, int n, const float *xf) 523 | { 524 | for (int i = 0; i < n && nbits > 0; i++) { 525 | 526 | if (xq[i] == 0) 527 | continue; 528 | 529 | lc3_put_bit(bits, xf[i] >= xq[i]); 530 | nbits--; 531 | } 532 | } 533 | 534 | /** 535 | * Get residual bits of quantization 536 | * bits Bitstream context 537 | * nbits Maximum number of bits to output 538 | * x, nq Spectral quantized, and count of significants 539 | */ 540 | static void get_residual(lc3_bits_t *bits, int nbits, float *x, int nq) 541 | { 542 | for (int i = 0; i < nq && nbits > 0; i++) { 543 | 544 | if (x[i] == 0) 545 | continue; 546 | 547 | if (lc3_get_bit(bits) == 0) 548 | x[i] -= x[i] < 0 ? 5.f/16 : 3.f/16; 549 | else 550 | x[i] += x[i] > 0 ? 5.f/16 : 3.f/16; 551 | 552 | nbits--; 553 | } 554 | } 555 | 556 | /** 557 | * Put LSB values of quantized spectrum values 558 | * bits Bitstream context 559 | * nbits Maximum number of bits to output 560 | * x, n Spectral quantized, and count of significants 561 | */ 562 | static void put_lsb(lc3_bits_t *bits, int nbits, const int16_t *x, int n) 563 | { 564 | for (int i = 0; i < n && nbits > 0; i += 2) { 565 | 566 | bool a_neg = x[i] < 0, b_neg = x[i+1] < 0; 567 | int a = LC3_ABS(x[i]), b = LC3_ABS(x[i+1]); 568 | 569 | if ((a | b) >> 2 == 0) 570 | continue; 571 | 572 | if (nbits-- > 0) 573 | lc3_put_bit(bits, a & 1); 574 | 575 | if (a == 1 && nbits-- > 0) 576 | lc3_put_bit(bits, a_neg); 577 | 578 | if (nbits-- > 0) 579 | lc3_put_bit(bits, b & 1); 580 | 581 | if (b == 1 && nbits-- > 0) 582 | lc3_put_bit(bits, b_neg); 583 | } 584 | } 585 | 586 | /** 587 | * Get LSB values of quantized spectrum values 588 | * bits Bitstream context 589 | * nbits Maximum number of bits to output 590 | * x, nq Spectral quantized, and count of significants 591 | * nf_seed Update the noise factor seed according 592 | */ 593 | static void get_lsb(lc3_bits_t *bits, 594 | int nbits, float *x, int nq, uint16_t *nf_seed) 595 | { 596 | for (int i = 0; i < nq && nbits > 0; i += 2) { 597 | 598 | float a = fabsf(x[i]), b = fabsf(x[i+1]); 599 | 600 | if (fmaxf(a, b) < 4) 601 | continue; 602 | 603 | if (nbits-- > 0 && lc3_get_bit(bits)) { 604 | if (a) { 605 | x[i] += x[i] < 0 ? -1 : 1; 606 | *nf_seed = (*nf_seed + i) & 0xffff; 607 | } else if (nbits-- > 0) { 608 | x[i] = lc3_get_bit(bits) ? -1 : 1; 609 | *nf_seed = (*nf_seed + i) & 0xffff; 610 | } 611 | } 612 | 613 | if (nbits-- > 0 && lc3_get_bit(bits)) { 614 | if (b) { 615 | x[i+1] += x[i+1] < 0 ? -1 : 1; 616 | *nf_seed = (*nf_seed + i+1) & 0xffff; 617 | } else if (nbits-- > 0) { 618 | x[i+1] = lc3_get_bit(bits) ? -1 : 1; 619 | *nf_seed = (*nf_seed + i+1) & 0xffff; 620 | } 621 | } 622 | } 623 | } 624 | 625 | 626 | /* ---------------------------------------------------------------------------- 627 | * Noise coding 628 | * -------------------------------------------------------------------------- */ 629 | 630 | /** 631 | * Estimate noise level 632 | * dt, bw Duration and bandwidth of the frame 633 | * xq, nq Quantized spectral coefficients 634 | * x Quantization scaled spectrum coefficients 635 | * return Noise factor (0 to 7) 636 | */ 637 | static int estimate_noise(enum lc3_dt dt, enum lc3_bandwidth bw, 638 | const int16_t *xq, int nq, const float *x) 639 | { 640 | int bw_stop = (dt == LC3_DT_7M5 ? 60 : 80) * (1 + bw); 641 | int w = 2 + dt; 642 | 643 | float sum = 0; 644 | int i, n = 0, z = 0; 645 | 646 | for (i = 6*(3 + dt) - w; i < LC3_MIN(nq, bw_stop); i++) { 647 | z = xq[i] ? 0 : z + 1; 648 | if (z > 2*w) 649 | sum += fabs(x[i - w]), n++; 650 | } 651 | 652 | for ( ; i < bw_stop + w; i++) 653 | if (++z > 2*w) 654 | sum += fabs(x[i - w]), n++; 655 | 656 | int nf = n ? 8 - (int)((16 * sum) / n + 0.5f) : 0; 657 | 658 | return LC3_CLIP(nf, 0, 7); 659 | } 660 | 661 | /** 662 | * Noise filling 663 | * dt, bw Duration and bandwidth of the frame 664 | * nf, nf_seed The noise factor and pseudo-random seed 665 | * g Quantization gain 666 | * x, nq Spectral quantized, and count of significants 667 | */ 668 | static void fill_noise(enum lc3_dt dt, enum lc3_bandwidth bw, 669 | int nf, uint16_t nf_seed, float g, float *x, int nq) 670 | { 671 | int bw_stop = (dt == LC3_DT_7M5 ? 60 : 80) * (1 + bw); 672 | int w = 2 + dt; 673 | 674 | float s = g * (float)(8 - nf) / 16; 675 | int i, z = 0; 676 | 677 | for (i = 6*(3 + dt) - w; i < LC3_MIN(nq, bw_stop); i++) { 678 | z = x[i] ? 0 : z + 1; 679 | if (z > 2*w) { 680 | nf_seed = (13849 + nf_seed*31821) & 0xffff; 681 | x[i - w] = nf_seed & 0x8000 ? -s : s; 682 | } 683 | } 684 | 685 | for ( ; i < bw_stop + w; i++) 686 | if (++z > 2*w) { 687 | nf_seed = (13849 + nf_seed*31821) & 0xffff; 688 | x[i - w] = nf_seed & 0x8000 ? -s : s; 689 | } 690 | } 691 | 692 | /** 693 | * Put noise factor 694 | * bits Bitstream context 695 | * nf Noise factor (0 to 7) 696 | */ 697 | static void put_noise_factor(lc3_bits_t *bits, int nf) 698 | { 699 | lc3_put_bits(bits, nf, 3); 700 | } 701 | 702 | /** 703 | * Get noise factor 704 | * bits Bitstream context 705 | * return Noise factor (0 to 7) 706 | */ 707 | static int get_noise_factor(lc3_bits_t *bits) 708 | { 709 | return lc3_get_bits(bits, 3); 710 | } 711 | 712 | 713 | /* ---------------------------------------------------------------------------- 714 | * Encoding 715 | * -------------------------------------------------------------------------- */ 716 | 717 | /** 718 | * Bit consumption of the number of coded coefficients 719 | * dt, sr Duration, samplerate of the frame 720 | * return Bit consumpution of the number of coded coefficients 721 | */ 722 | static int get_nbits_nq(enum lc3_dt dt, enum lc3_srate sr) 723 | { 724 | int ne = LC3_NE(dt, sr); 725 | return 4 + (ne > 32) + (ne > 64) + (ne > 128) + (ne > 256); 726 | } 727 | 728 | /** 729 | * Bit consumption of the arithmetic coder 730 | * dt, sr, nbytes Duration, samplerate and size of the frame 731 | * return Bit consumption of bitstream data 732 | */ 733 | static int get_nbits_ac(enum lc3_dt dt, enum lc3_srate sr, int nbytes) 734 | { 735 | return get_nbits_nq(dt, sr) + 3 + LC3_MIN((nbytes-1) / 160, 2); 736 | } 737 | 738 | /** 739 | * Spectrum analysis 740 | */ 741 | void lc3_spec_analyze(enum lc3_dt dt, enum lc3_srate sr, 742 | int nbytes, bool pitch, const lc3_tns_data_t *tns, 743 | struct lc3_spec_analysis *spec, float *x, 744 | int16_t *xq, struct lc3_spec_side *side) 745 | { 746 | bool reset_off; 747 | 748 | /* --- Bit budget --- */ 749 | 750 | const int nbits_gain = 8; 751 | const int nbits_nf = 3; 752 | 753 | int nbits_budget = 8*nbytes - get_nbits_ac(dt, sr, nbytes) - 754 | lc3_bwdet_get_nbits(sr) - lc3_ltpf_get_nbits(pitch) - 755 | lc3_sns_get_nbits() - lc3_tns_get_nbits(tns) - nbits_gain - nbits_nf; 756 | 757 | /* --- Global gain --- */ 758 | 759 | float nbits_off = spec->nbits_off + spec->nbits_spare; 760 | nbits_off = fminf(fmaxf(nbits_off, -40), 40); 761 | nbits_off = 0.8 * spec->nbits_off + 0.2 * nbits_off; 762 | 763 | int g_off = resolve_gain_offset(sr, nbytes); 764 | 765 | int g_int = estimate_gain(dt, sr, 766 | x, nbits_budget, nbits_off, g_off, &reset_off); 767 | 768 | /* --- Quantization --- */ 769 | 770 | quantize(dt, sr, g_int, x, xq, &side->nq); 771 | 772 | int nbits = compute_nbits(dt, sr, nbytes, xq, &side->nq, 0, NULL); 773 | 774 | spec->nbits_off = reset_off ? 0 : nbits_off; 775 | spec->nbits_spare = reset_off ? 0 : nbits_budget - nbits; 776 | 777 | /* --- Adjust gain and requantize --- */ 778 | 779 | int g_adj = adjust_gain(sr, g_int + g_off, nbits, nbits_budget); 780 | 781 | if (g_adj) 782 | quantize(dt, sr, g_adj, x, xq, &side->nq); 783 | 784 | side->g_idx = g_int + g_adj + g_off; 785 | nbits = compute_nbits(dt, sr, nbytes, 786 | xq, &side->nq, nbits_budget, &side->lsb_mode); 787 | } 788 | 789 | /** 790 | * Put spectral quantization side data 791 | */ 792 | void lc3_spec_put_side(lc3_bits_t *bits, 793 | enum lc3_dt dt, enum lc3_srate sr, const struct lc3_spec_side *side) 794 | { 795 | int nbits_nq = get_nbits_nq(dt, sr); 796 | 797 | lc3_put_bits(bits, LC3_MAX(side->nq >> 1, 1) - 1, nbits_nq); 798 | lc3_put_bits(bits, side->lsb_mode, 1); 799 | lc3_put_bits(bits, side->g_idx, 8); 800 | } 801 | 802 | /** 803 | * Encode spectral coefficients 804 | */ 805 | void lc3_spec_encode(lc3_bits_t *bits, 806 | enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, int nbytes, 807 | const int16_t *xq, const lc3_spec_side_t *side, const float *x) 808 | { 809 | bool lsb_mode = side->lsb_mode; 810 | int nq = side->nq; 811 | 812 | put_noise_factor(bits, estimate_noise(dt, bw, xq, nq, x)); 813 | 814 | put_quantized(bits, dt, sr, nbytes, xq, nq, lsb_mode); 815 | 816 | int nbits_left = lc3_get_bits_left(bits); 817 | 818 | if (lsb_mode) 819 | put_lsb(bits, nbits_left, xq, nq); 820 | else 821 | put_residual(bits, nbits_left, xq, nq, x); 822 | } 823 | 824 | 825 | /* ---------------------------------------------------------------------------- 826 | * Decoding 827 | * -------------------------------------------------------------------------- */ 828 | 829 | /** 830 | * Get spectral quantization side data 831 | */ 832 | int lc3_spec_get_side(lc3_bits_t *bits, 833 | enum lc3_dt dt, enum lc3_srate sr, struct lc3_spec_side *side) 834 | { 835 | int nbits_nq = get_nbits_nq(dt, sr); 836 | int ne = LC3_NE(dt, sr); 837 | 838 | side->nq = (lc3_get_bits(bits, nbits_nq) + 1) << 1; 839 | side->lsb_mode = lc3_get_bit(bits); 840 | side->g_idx = lc3_get_bits(bits, 8); 841 | 842 | return side->nq > ne ? (side->nq = ne), -1 : 0; 843 | } 844 | 845 | /** 846 | * Decode spectral coefficients 847 | */ 848 | int lc3_spec_decode(lc3_bits_t *bits, 849 | enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, 850 | int nbytes, const lc3_spec_side_t *side, float *x) 851 | { 852 | bool lsb_mode = side->lsb_mode; 853 | int nq = side->nq; 854 | int ret = 0; 855 | 856 | int nf = get_noise_factor(bits); 857 | uint16_t nf_seed; 858 | 859 | if ((ret = get_quantized(bits, dt, sr, nbytes, 860 | nq, lsb_mode, x, &nf_seed)) < 0) 861 | return ret; 862 | 863 | int nbits_left = lc3_get_bits_left(bits); 864 | 865 | if (lsb_mode) 866 | get_lsb(bits, nbits_left, x, nq, &nf_seed); 867 | else 868 | get_residual(bits, nbits_left, x, nq); 869 | 870 | int g_int = side->g_idx - resolve_gain_offset(sr, nbytes); 871 | float g = unquantize(dt, sr, g_int, x, nq); 872 | 873 | if (nq > 2 || x[0] || x[1] || side->g_idx > 0 || nf < 7) 874 | fill_noise(dt, bw, nf, nf_seed, g, x, nq); 875 | 876 | return 0; 877 | } 878 | -------------------------------------------------------------------------------- /src/sns.c: -------------------------------------------------------------------------------- 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 "sns.h" 20 | #include "tables.h" 21 | 22 | 23 | /* ---------------------------------------------------------------------------- 24 | * DCT-16 25 | * -------------------------------------------------------------------------- */ 26 | 27 | /** 28 | * Matrix of DCT-16 coefficients 29 | * 30 | * M[n][k] = 2f cos( Pi k (2n + 1) / 2N ) 31 | * 32 | * k = [0..N-1], n = [0..N-1], N = 16 33 | * f = sqrt(1/4N) for k=0, sqrt(1/2N) otherwise 34 | */ 35 | static const float dct16_m[16][16] = { 36 | 37 | { 2.50000000e-01, 3.51850934e-01, 3.46759961e-01, 3.38329500e-01, 38 | 3.26640741e-01, 3.11806253e-01, 2.93968901e-01, 2.73300467e-01, 39 | 2.50000000e-01, 2.24291897e-01, 1.96423740e-01, 1.66663915e-01, 40 | 1.35299025e-01, 1.02631132e-01, 6.89748448e-02, 3.46542923e-02 }, 41 | 42 | { 2.50000000e-01, 3.38329500e-01, 2.93968901e-01, 2.24291897e-01, 43 | 1.35299025e-01, 3.46542923e-02, -6.89748448e-02, -1.66663915e-01, 44 | -2.50000000e-01, -3.11806253e-01, -3.46759961e-01, -3.51850934e-01, 45 | -3.26640741e-01, -2.73300467e-01, -1.96423740e-01, -1.02631132e-01 }, 46 | 47 | { 2.50000000e-01, 3.11806253e-01, 1.96423740e-01, 3.46542923e-02, 48 | -1.35299025e-01, -2.73300467e-01, -3.46759961e-01, -3.38329500e-01, 49 | -2.50000000e-01, -1.02631132e-01, 6.89748448e-02, 2.24291897e-01, 50 | 3.26640741e-01, 3.51850934e-01, 2.93968901e-01, 1.66663915e-01 }, 51 | 52 | { 2.50000000e-01, 2.73300467e-01, 6.89748448e-02, -1.66663915e-01, 53 | -3.26640741e-01, -3.38329500e-01, -1.96423740e-01, 3.46542923e-02, 54 | 2.50000000e-01, 3.51850934e-01, 2.93968901e-01, 1.02631132e-01, 55 | -1.35299025e-01, -3.11806253e-01, -3.46759961e-01, -2.24291897e-01 }, 56 | 57 | { 2.50000000e-01, 2.24291897e-01, -6.89748448e-02, -3.11806253e-01, 58 | -3.26640741e-01, -1.02631132e-01, 1.96423740e-01, 3.51850934e-01, 59 | 2.50000000e-01, -3.46542923e-02, -2.93968901e-01, -3.38329500e-01, 60 | -1.35299025e-01, 1.66663915e-01, 3.46759961e-01, 2.73300467e-01 }, 61 | 62 | { 2.50000000e-01, 1.66663915e-01, -1.96423740e-01, -3.51850934e-01, 63 | -1.35299025e-01, 2.24291897e-01, 3.46759961e-01, 1.02631132e-01, 64 | -2.50000000e-01, -3.38329500e-01, -6.89748448e-02, 2.73300467e-01, 65 | 3.26640741e-01, 3.46542923e-02, -2.93968901e-01, -3.11806253e-01 }, 66 | 67 | { 2.50000000e-01, 1.02631132e-01, -2.93968901e-01, -2.73300467e-01, 68 | 1.35299025e-01, 3.51850934e-01, 6.89748448e-02, -3.11806253e-01, 69 | -2.50000000e-01, 1.66663915e-01, 3.46759961e-01, 3.46542923e-02, 70 | -3.26640741e-01, -2.24291897e-01, 1.96423740e-01, 3.38329500e-01 }, 71 | 72 | { 2.50000000e-01, 3.46542923e-02, -3.46759961e-01, -1.02631132e-01, 73 | 3.26640741e-01, 1.66663915e-01, -2.93968901e-01, -2.24291897e-01, 74 | 2.50000000e-01, 2.73300467e-01, -1.96423740e-01, -3.11806253e-01, 75 | 1.35299025e-01, 3.38329500e-01, -6.89748448e-02, -3.51850934e-01 }, 76 | 77 | { 2.50000000e-01, -3.46542923e-02, -3.46759961e-01, 1.02631132e-01, 78 | 3.26640741e-01, -1.66663915e-01, -2.93968901e-01, 2.24291897e-01, 79 | 2.50000000e-01, -2.73300467e-01, -1.96423740e-01, 3.11806253e-01, 80 | 1.35299025e-01, -3.38329500e-01, -6.89748448e-02, 3.51850934e-01 }, 81 | 82 | { 2.50000000e-01, -1.02631132e-01, -2.93968901e-01, 2.73300467e-01, 83 | 1.35299025e-01, -3.51850934e-01, 6.89748448e-02, 3.11806253e-01, 84 | -2.50000000e-01, -1.66663915e-01, 3.46759961e-01, -3.46542923e-02, 85 | -3.26640741e-01, 2.24291897e-01, 1.96423740e-01, -3.38329500e-01 }, 86 | 87 | { 2.50000000e-01, -1.66663915e-01, -1.96423740e-01, 3.51850934e-01, 88 | -1.35299025e-01, -2.24291897e-01, 3.46759961e-01, -1.02631132e-01, 89 | -2.50000000e-01, 3.38329500e-01, -6.89748448e-02, -2.73300467e-01, 90 | 3.26640741e-01, -3.46542923e-02, -2.93968901e-01, 3.11806253e-01 }, 91 | 92 | { 2.50000000e-01, -2.24291897e-01, -6.89748448e-02, 3.11806253e-01, 93 | -3.26640741e-01, 1.02631132e-01, 1.96423740e-01, -3.51850934e-01, 94 | 2.50000000e-01, 3.46542923e-02, -2.93968901e-01, 3.38329500e-01, 95 | -1.35299025e-01, -1.66663915e-01, 3.46759961e-01, -2.73300467e-01 }, 96 | 97 | { 2.50000000e-01, -2.73300467e-01, 6.89748448e-02, 1.66663915e-01, 98 | -3.26640741e-01, 3.38329500e-01, -1.96423740e-01, -3.46542923e-02, 99 | 2.50000000e-01, -3.51850934e-01, 2.93968901e-01, -1.02631132e-01, 100 | -1.35299025e-01, 3.11806253e-01, -3.46759961e-01, 2.24291897e-01 }, 101 | 102 | { 2.50000000e-01, -3.11806253e-01, 1.96423740e-01, -3.46542923e-02, 103 | -1.35299025e-01, 2.73300467e-01, -3.46759961e-01, 3.38329500e-01, 104 | -2.50000000e-01, 1.02631132e-01, 6.89748448e-02, -2.24291897e-01, 105 | 3.26640741e-01, -3.51850934e-01, 2.93968901e-01, -1.66663915e-01 }, 106 | 107 | { 2.50000000e-01, -3.38329500e-01, 2.93968901e-01, -2.24291897e-01, 108 | 1.35299025e-01, -3.46542923e-02, -6.89748448e-02, 1.66663915e-01, 109 | -2.50000000e-01, 3.11806253e-01, -3.46759961e-01, 3.51850934e-01, 110 | -3.26640741e-01, 2.73300467e-01, -1.96423740e-01, 1.02631132e-01 }, 111 | 112 | { 2.50000000e-01, -3.51850934e-01, 3.46759961e-01, -3.38329500e-01, 113 | 3.26640741e-01, -3.11806253e-01, 2.93968901e-01, -2.73300467e-01, 114 | 2.50000000e-01, -2.24291897e-01, 1.96423740e-01, -1.66663915e-01, 115 | 1.35299025e-01, -1.02631132e-01, 6.89748448e-02, -3.46542923e-02 }, 116 | 117 | }; 118 | 119 | /** 120 | * Forward DCT-16 transformation 121 | * x, y Input and output 16 values 122 | */ 123 | static void dct16_forward(const float *x, float *y) 124 | { 125 | for (int i = 0, j; i < 16; i++) 126 | for (y[i] = 0, j = 0; j < 16; j++) 127 | y[i] += x[j] * dct16_m[j][i]; 128 | } 129 | 130 | /** 131 | * Inverse DCT-16 transformation 132 | * x, y Input and output 16 values 133 | */ 134 | static void dct16_inverse(const float *x, float *y) 135 | { 136 | for (int i = 0, j; i < 16; i++) 137 | for (y[i] = 0, j = 0; j < 16; j++) 138 | y[i] += x[j] * dct16_m[i][j]; 139 | } 140 | 141 | 142 | /* ---------------------------------------------------------------------------- 143 | * Scale factors 144 | * -------------------------------------------------------------------------- */ 145 | 146 | /** 147 | * Scale factors 148 | * dt, sr Duration and samplerate of the frame 149 | * eb Energy estimation per bands 150 | * att 1: Attack detected 0: Otherwise 151 | * scf Output 16 scale factors 152 | */ 153 | static void compute_scale_factors(enum lc3_dt dt, enum lc3_srate sr, 154 | const float *eb, bool att, float *scf) 155 | { 156 | /* Pre-emphasis gain table : 157 | * Ge[b] = 10 ^ (b * g_tilt) / 630 , b = [0..63] */ 158 | 159 | static const float ge_table[LC3_NUM_SRATE][LC3_NUM_BANDS] = { 160 | 161 | [LC3_SRATE_8K] = { /* g_tilt = 14 */ 162 | 1.00000000e+00, 1.05250029e+00, 1.10775685e+00, 1.16591440e+00, 163 | 1.22712524e+00, 1.29154967e+00, 1.35935639e+00, 1.43072299e+00, 164 | 1.50583635e+00, 1.58489319e+00, 1.66810054e+00, 1.75567629e+00, 165 | 1.84784980e+00, 1.94486244e+00, 2.04696827e+00, 2.15443469e+00, 166 | 2.26754313e+00, 2.38658979e+00, 2.51188643e+00, 2.64376119e+00, 167 | 2.78255940e+00, 2.92864456e+00, 3.08239924e+00, 3.24422608e+00, 168 | 3.41454887e+00, 3.59381366e+00, 3.78248991e+00, 3.98107171e+00, 169 | 4.19007911e+00, 4.41005945e+00, 4.64158883e+00, 4.88527357e+00, 170 | 5.14175183e+00, 5.41169527e+00, 5.69581081e+00, 5.99484250e+00, 171 | 6.30957344e+00, 6.64082785e+00, 6.98947321e+00, 7.35642254e+00, 172 | 7.74263683e+00, 8.14912747e+00, 8.57695899e+00, 9.02725178e+00, 173 | 9.50118507e+00, 1.00000000e+01, 1.05250029e+01, 1.10775685e+01, 174 | 1.16591440e+01, 1.22712524e+01, 1.29154967e+01, 1.35935639e+01, 175 | 1.43072299e+01, 1.50583635e+01, 1.58489319e+01, 1.66810054e+01, 176 | 1.75567629e+01, 1.84784980e+01, 1.94486244e+01, 2.04696827e+01, 177 | 2.15443469e+01, 2.26754313e+01, 2.38658979e+01, 2.51188643e+01 }, 178 | 179 | [LC3_SRATE_16K] = { /* g_tilt = 18 */ 180 | 1.00000000e+00, 1.06800043e+00, 1.14062492e+00, 1.21818791e+00, 181 | 1.30102522e+00, 1.38949549e+00, 1.48398179e+00, 1.58489319e+00, 182 | 1.69266662e+00, 1.80776868e+00, 1.93069773e+00, 2.06198601e+00, 183 | 2.20220195e+00, 2.35195264e+00, 2.51188643e+00, 2.68269580e+00, 184 | 2.86512027e+00, 3.05994969e+00, 3.26802759e+00, 3.49025488e+00, 185 | 3.72759372e+00, 3.98107171e+00, 4.25178630e+00, 4.54090961e+00, 186 | 4.84969343e+00, 5.17947468e+00, 5.53168120e+00, 5.90783791e+00, 187 | 6.30957344e+00, 6.73862717e+00, 7.19685673e+00, 7.68624610e+00, 188 | 8.20891416e+00, 8.76712387e+00, 9.36329209e+00, 1.00000000e+01, 189 | 1.06800043e+01, 1.14062492e+01, 1.21818791e+01, 1.30102522e+01, 190 | 1.38949549e+01, 1.48398179e+01, 1.58489319e+01, 1.69266662e+01, 191 | 1.80776868e+01, 1.93069773e+01, 2.06198601e+01, 2.20220195e+01, 192 | 2.35195264e+01, 2.51188643e+01, 2.68269580e+01, 2.86512027e+01, 193 | 3.05994969e+01, 3.26802759e+01, 3.49025488e+01, 3.72759372e+01, 194 | 3.98107171e+01, 4.25178630e+01, 4.54090961e+01, 4.84969343e+01, 195 | 5.17947468e+01, 5.53168120e+01, 5.90783791e+01, 6.30957344e+01 }, 196 | 197 | [LC3_SRATE_24K] = { /* g_tilt = 22 */ 198 | 1.00000000e+00, 1.08372885e+00, 1.17446822e+00, 1.27280509e+00, 199 | 1.37937560e+00, 1.49486913e+00, 1.62003281e+00, 1.75567629e+00, 200 | 1.90267705e+00, 2.06198601e+00, 2.23463373e+00, 2.42173704e+00, 201 | 2.62450630e+00, 2.84425319e+00, 3.08239924e+00, 3.34048498e+00, 202 | 3.62017995e+00, 3.92329345e+00, 4.25178630e+00, 4.60778348e+00, 203 | 4.99358789e+00, 5.41169527e+00, 5.86481029e+00, 6.35586411e+00, 204 | 6.88803330e+00, 7.46476041e+00, 8.08977621e+00, 8.76712387e+00, 205 | 9.50118507e+00, 1.02967084e+01, 1.11588399e+01, 1.20931568e+01, 206 | 1.31057029e+01, 1.42030283e+01, 1.53922315e+01, 1.66810054e+01, 207 | 1.80776868e+01, 1.95913107e+01, 2.12316686e+01, 2.30093718e+01, 208 | 2.49359200e+01, 2.70237760e+01, 2.92864456e+01, 3.17385661e+01, 209 | 3.43959997e+01, 3.72759372e+01, 4.03970086e+01, 4.37794036e+01, 210 | 4.74450028e+01, 5.14175183e+01, 5.57226480e+01, 6.03882412e+01, 211 | 6.54444792e+01, 7.09240702e+01, 7.68624610e+01, 8.32980665e+01, 212 | 9.02725178e+01, 9.78309319e+01, 1.06022203e+02, 1.14899320e+02, 213 | 1.24519708e+02, 1.34945600e+02, 1.46244440e+02, 1.58489319e+02 }, 214 | 215 | [LC3_SRATE_32K] = { /* g_tilt = 26 */ 216 | 1.00000000e+00, 1.09968890e+00, 1.20931568e+00, 1.32987103e+00, 217 | 1.46244440e+00, 1.60823388e+00, 1.76855694e+00, 1.94486244e+00, 218 | 2.13874364e+00, 2.35195264e+00, 2.58641621e+00, 2.84425319e+00, 219 | 3.12779366e+00, 3.43959997e+00, 3.78248991e+00, 4.15956216e+00, 220 | 4.57422434e+00, 5.03022373e+00, 5.53168120e+00, 6.08312841e+00, 221 | 6.68954879e+00, 7.35642254e+00, 8.08977621e+00, 8.89623710e+00, 222 | 9.78309319e+00, 1.07583590e+01, 1.18308480e+01, 1.30102522e+01, 223 | 1.43072299e+01, 1.57335019e+01, 1.73019574e+01, 1.90267705e+01, 224 | 2.09235283e+01, 2.30093718e+01, 2.53031508e+01, 2.78255940e+01, 225 | 3.05994969e+01, 3.36499270e+01, 3.70044512e+01, 4.06933843e+01, 226 | 4.47500630e+01, 4.92111475e+01, 5.41169527e+01, 5.95118121e+01, 227 | 6.54444792e+01, 7.19685673e+01, 7.91430346e+01, 8.70327166e+01, 228 | 9.57089124e+01, 1.05250029e+02, 1.15742288e+02, 1.27280509e+02, 229 | 1.39968963e+02, 1.53922315e+02, 1.69266662e+02, 1.86140669e+02, 230 | 2.04696827e+02, 2.25102829e+02, 2.47543082e+02, 2.72220379e+02, 231 | 2.99357729e+02, 3.29200372e+02, 3.62017995e+02, 3.98107171e+02 }, 232 | 233 | [LC3_SRATE_48K] = { /* g_tilt = 30 */ 234 | 1.00000000e+00, 1.11588399e+00, 1.24519708e+00, 1.38949549e+00, 235 | 1.55051578e+00, 1.73019574e+00, 1.93069773e+00, 2.15443469e+00, 236 | 2.40409918e+00, 2.68269580e+00, 2.99357729e+00, 3.34048498e+00, 237 | 3.72759372e+00, 4.15956216e+00, 4.64158883e+00, 5.17947468e+00, 238 | 5.77969288e+00, 6.44946677e+00, 7.19685673e+00, 8.03085722e+00, 239 | 8.96150502e+00, 1.00000000e+01, 1.11588399e+01, 1.24519708e+01, 240 | 1.38949549e+01, 1.55051578e+01, 1.73019574e+01, 1.93069773e+01, 241 | 2.15443469e+01, 2.40409918e+01, 2.68269580e+01, 2.99357729e+01, 242 | 3.34048498e+01, 3.72759372e+01, 4.15956216e+01, 4.64158883e+01, 243 | 5.17947468e+01, 5.77969288e+01, 6.44946677e+01, 7.19685673e+01, 244 | 8.03085722e+01, 8.96150502e+01, 1.00000000e+02, 1.11588399e+02, 245 | 1.24519708e+02, 1.38949549e+02, 1.55051578e+02, 1.73019574e+02, 246 | 1.93069773e+02, 2.15443469e+02, 2.40409918e+02, 2.68269580e+02, 247 | 2.99357729e+02, 3.34048498e+02, 3.72759372e+02, 4.15956216e+02, 248 | 4.64158883e+02, 5.17947468e+02, 5.77969288e+02, 6.44946677e+02, 249 | 7.19685673e+02, 8.03085722e+02, 8.96150502e+02, 1.00000000e+03 }, 250 | }; 251 | 252 | float e[LC3_NUM_BANDS]; 253 | 254 | /* --- Copy and padding --- */ 255 | 256 | int nb = LC3_MIN(lc3_band_lim[dt][sr][LC3_NUM_BANDS], LC3_NUM_BANDS); 257 | int n2 = LC3_NUM_BANDS - nb; 258 | 259 | for (int i2 = 0; i2 < n2; i2++) 260 | e[2*i2 + 0] = e[2*i2 + 1] = eb[i2]; 261 | 262 | memcpy(e + 2*n2, eb + n2, (nb - n2) * sizeof(float)); 263 | 264 | /* --- Smoothing, pre-emphasis and logarithm --- */ 265 | 266 | const float *ge = ge_table[sr]; 267 | 268 | float e0 = e[0], e1 = e[0], e2; 269 | float e_sum = 0; 270 | 271 | for (int i = 0; i < LC3_NUM_BANDS-1; ) { 272 | e[i] = (e0 * 0.25 + e1 * 0.5 + (e2 = e[i+1]) * 0.25) * ge[i]; 273 | e_sum += e[i++]; 274 | 275 | e[i] = (e1 * 0.25 + e2 * 0.5 + (e0 = e[i+1]) * 0.25) * ge[i]; 276 | e_sum += e[i++]; 277 | 278 | e[i] = (e2 * 0.25 + e0 * 0.5 + (e1 = e[i+1]) * 0.25) * ge[i]; 279 | e_sum += e[i++]; 280 | } 281 | 282 | e[LC3_NUM_BANDS-1] = (e0 * 0.25 + e1 * 0.75) * ge[LC3_NUM_BANDS-1]; 283 | e_sum += e[LC3_NUM_BANDS-1]; 284 | 285 | float noise_floor = fmaxf(e_sum * (1e-4 / 64), 0x1p-32); 286 | 287 | for (int i = 0; i < LC3_NUM_BANDS; i++) 288 | e[i] = log2f(fmaxf(e[i], noise_floor)) * 0.5; 289 | 290 | /* --- Grouping & scaling --- */ 291 | 292 | float scf_sum; 293 | 294 | scf[0] = (e[0] + e[4]) * 1./12 + 295 | (e[0] + e[3]) * 2./12 + 296 | (e[1] + e[2]) * 3./12 ; 297 | scf_sum = scf[0]; 298 | 299 | for (int i = 1; i < 15; i++) { 300 | scf[i] = (e[4*i-1] + e[4*i+4]) * 1./12 + 301 | (e[4*i ] + e[4*i+3]) * 2./12 + 302 | (e[4*i+1] + e[4*i+2]) * 3./12 ; 303 | scf_sum += scf[i]; 304 | } 305 | 306 | scf[15] = (e[59] + e[63]) * 1./12 + 307 | (e[60] + e[63]) * 2./12 + 308 | (e[61] + e[62]) * 3./12 ; 309 | scf_sum += scf[15]; 310 | 311 | for (int i = 0; i < 16; i++) 312 | scf[i] = 0.85 * (scf[i] - scf_sum * 1./16); 313 | 314 | /* --- Attack handling --- */ 315 | 316 | if (!att) 317 | return; 318 | 319 | float s0, s1 = scf[0], s2 = scf[1], s3 = scf[2], s4 = scf[3]; 320 | float sn = s1 + s2; 321 | 322 | scf[0] = (sn += s3) * 1./3; 323 | scf[1] = (sn += s4) * 1./4; 324 | scf_sum = scf[0] + scf[1]; 325 | 326 | for (int i = 2; i < 14; i++, sn -= s0) { 327 | s0 = s1, s1 = s2, s2 = s3, s3 = s4, s4 = scf[i+2]; 328 | scf[i] = (sn += s4) * 1./5; 329 | scf_sum += scf[i]; 330 | } 331 | 332 | scf[14] = (sn ) * 1./4; 333 | scf[15] = (sn -= s1) * 1./3; 334 | scf_sum += scf[14] + scf[15]; 335 | 336 | for (int i = 0; i < 16; i++) 337 | scf[i] = (dt == LC3_DT_7M5 ? 0.3 : 0.5) * 338 | (scf[i] - scf_sum * 1./16); 339 | } 340 | 341 | /** 342 | * Codebooks 343 | * scf Input 16 scale factors 344 | * lf/hfcb_idx Output the low and high frequency codebooks index 345 | */ 346 | static void resolve_codebooks(const float *scf, int *lfcb_idx, int *hfcb_idx) 347 | { 348 | float dlfcb_max = 0, dhfcb_max = 0; 349 | *lfcb_idx = *hfcb_idx = 0; 350 | 351 | for (int icb = 0; icb < 32; icb++) { 352 | const float *lfcb = lc3_sns_lfcb[icb]; 353 | const float *hfcb = lc3_sns_hfcb[icb]; 354 | float dlfcb = 0, dhfcb = 0; 355 | 356 | for (int i = 0; i < 8; i++) { 357 | dlfcb += (scf[ i] - lfcb[i]) * (scf[ i] - lfcb[i]); 358 | dhfcb += (scf[8+i] - hfcb[i]) * (scf[8+i] - hfcb[i]); 359 | } 360 | 361 | if (icb == 0 || dlfcb < dlfcb_max) 362 | *lfcb_idx = icb, dlfcb_max = dlfcb; 363 | 364 | if (icb == 0 || dhfcb < dhfcb_max) 365 | *hfcb_idx = icb, dhfcb_max = dhfcb; 366 | } 367 | } 368 | 369 | /** 370 | * Unit energy normalize pulse configuration 371 | * c Pulse configuration 372 | * cn Normalized pulse configuration 373 | */ 374 | static void normalize(const int *c, float *cn) 375 | { 376 | int c2_sum = 0; 377 | for (int i = 0; i < 16; i++) 378 | c2_sum += c[i] * c[i]; 379 | 380 | float c_norm = 1.f / sqrtf(c2_sum); 381 | 382 | for (int i = 0; i < 16; i++) 383 | cn[i] = c[i] * c_norm; 384 | } 385 | 386 | /** 387 | * Sub-procedure of `quantize()`, add unit pulse 388 | * x, y, n Transformed residual, and vector of pulses with length 389 | * start, end Current number of pulses, limit to reach 390 | * corr, energy Correlation (x,y) and y energy, updated at output 391 | */ 392 | static void add_pulse(const float *x, int *y, int n, 393 | int start, int end, float *corr, float *energy) 394 | { 395 | for (int k = start; k < end; k++) { 396 | float best_c2 = (*corr + x[0]) * (*corr + x[0]); 397 | float best_e = *energy + 2*y[0] + 1; 398 | int nbest = 0; 399 | 400 | for (int i = 1; i < n; i++) { 401 | float c2 = (*corr + x[i]) * (*corr + x[i]); 402 | float e = *energy + 2*y[i] + 1; 403 | 404 | if (c2 * best_e > e * best_c2) 405 | best_c2 = c2, best_e = e, nbest = i; 406 | } 407 | 408 | *corr += x[nbest]; 409 | *energy += 2*y[nbest] + 1; 410 | y[nbest]++; 411 | } 412 | } 413 | 414 | /** 415 | * Quantization of codebooks residual 416 | * scf Input 16 scale factors, output quantized version 417 | * lf/hfcb_idx Codebooks index 418 | * c, cn Output 4 pulse configurations candidates, normalized 419 | * shape/gain_idx Output selected shape/gain indexes 420 | */ 421 | static void quantize(const float *scf, int lfcb_idx, int hfcb_idx, 422 | int (*c)[16], float (*cn)[16], int *shape_idx, int *gain_idx) 423 | { 424 | /* --- Residual --- */ 425 | 426 | const float *lfcb = lc3_sns_lfcb[lfcb_idx]; 427 | const float *hfcb = lc3_sns_hfcb[hfcb_idx]; 428 | float r[16], x[16]; 429 | 430 | for (int i = 0; i < 8; i++) { 431 | r[ i] = scf[ i] - lfcb[i]; 432 | r[8+i] = scf[8+i] - hfcb[i]; 433 | } 434 | 435 | dct16_forward(r, x); 436 | 437 | /* --- Shape 3 candidate --- 438 | * Project to or below pyramid N = 16, K = 6, 439 | * then add unit pulses until you reach K = 6, over N = 16 */ 440 | 441 | float xm[16]; 442 | float xm_sum = 0; 443 | 444 | for (int i = 0; i < 16; i++) { 445 | xm[i] = fabsf(x[i]); 446 | xm_sum += xm[i]; 447 | } 448 | 449 | float proj_factor = (6 - 1) / fmaxf(xm_sum, 1e-31); 450 | float corr = 0, energy = 0; 451 | int npulses = 0; 452 | 453 | for (int i = 0; i < 16; i++) { 454 | c[3][i] = floorf(xm[i] * proj_factor); 455 | npulses += c[3][i]; 456 | corr += c[3][i] * xm[i]; 457 | energy += c[3][i] * c[3][i]; 458 | } 459 | 460 | add_pulse(xm, c[3], 16, npulses, 6, &corr, &energy); 461 | npulses = 6; 462 | 463 | /* --- Shape 2 candidate --- 464 | * Add unit pulses until you reach K = 8 on shape 3 */ 465 | 466 | memcpy(c[2], c[3], sizeof(c[2])); 467 | 468 | add_pulse(xm, c[2], 16, npulses, 8, &corr, &energy); 469 | npulses = 8; 470 | 471 | /* --- Shape 1 candidate --- 472 | * Remove any unit pulses from shape 2 that are not part of 0 to 9 473 | * Update energy and correlation terms accordingly 474 | * Add unit pulses until you reach K = 10, over N = 10 */ 475 | 476 | memcpy(c[1], c[2], sizeof(c[1])); 477 | 478 | for (int i = 10; i < 16; i++) { 479 | c[1][i] = 0; 480 | npulses -= c[2][i]; 481 | corr -= c[2][i] * xm[i]; 482 | energy -= c[2][i] * c[2][i]; 483 | } 484 | 485 | add_pulse(xm, c[1], 10, npulses, 10, &corr, &energy); 486 | npulses = 10; 487 | 488 | /* --- Shape 0 candidate --- 489 | * Add unit pulses until you reach K = 1, on shape 1 */ 490 | 491 | memcpy(c[0], c[1], sizeof(c[0])); 492 | 493 | add_pulse(xm + 10, c[0] + 10, 6, 0, 1, &corr, &energy); 494 | 495 | /* --- Add sign and unit energy normalize --- */ 496 | 497 | for (int j = 0; j < 16; j++) 498 | for (int i = 0; i < 4; i++) 499 | c[i][j] = x[j] < 0 ? -c[i][j] : c[i][j]; 500 | 501 | for (int i = 0; i < 4; i++) 502 | normalize(c[i], cn[i]); 503 | 504 | /* --- Determe shape & gain index --- 505 | * Search the Mean Square Error, within (shape, gain) combinations */ 506 | 507 | float mse_min = INFINITY; 508 | *shape_idx = *gain_idx = 0; 509 | 510 | for (int ic = 0; ic < 4; ic++) { 511 | const struct lc3_sns_vq_gains *cgains = lc3_sns_vq_gains + ic; 512 | float cmse_min = INFINITY; 513 | int cgain_idx = 0; 514 | 515 | for (int ig = 0; ig < cgains->count; ig++) { 516 | float g = cgains->v[ig]; 517 | 518 | float mse = 0; 519 | for (int i = 0; i < 16; i++) 520 | mse += (x[i] - g * cn[ic][i]) * (x[i] - g * cn[ic][i]); 521 | 522 | if (mse < cmse_min) { 523 | cgain_idx = ig, 524 | cmse_min = mse; 525 | } 526 | } 527 | 528 | if (cmse_min < mse_min) { 529 | *shape_idx = ic, *gain_idx = cgain_idx; 530 | mse_min = cmse_min; 531 | } 532 | } 533 | } 534 | 535 | /** 536 | * Unquantization of codebooks residual 537 | * lf/hfcb_idx Low and high frequency codebooks index 538 | * c Table of normalized pulse configuration 539 | * shape/gain Selected shape/gain indexes 540 | * scf Return unquantized scale factors 541 | */ 542 | static void unquantize(int lfcb_idx, int hfcb_idx, 543 | const float *c, int shape, int gain, float *scf) 544 | { 545 | const float *lfcb = lc3_sns_lfcb[lfcb_idx]; 546 | const float *hfcb = lc3_sns_hfcb[hfcb_idx]; 547 | float g = lc3_sns_vq_gains[shape].v[gain]; 548 | 549 | dct16_inverse(c, scf); 550 | 551 | for (int i = 0; i < 8; i++) 552 | scf[i] = lfcb[i] + g * scf[i]; 553 | 554 | for (int i = 8; i < 16; i++) 555 | scf[i] = hfcb[i-8] + g * scf[i]; 556 | } 557 | 558 | /** 559 | * Sub-procedure of `sns_enumerate()`, enumeration of a vector 560 | * c, n Table of pulse configuration, and length 561 | * idx, ls Return enumeration set 562 | */ 563 | static void enum_mvpq(const int *c, int n, int *idx, bool *ls) 564 | { 565 | int ci, i, j; 566 | 567 | /* --- Scan for 1st significant coeff --- */ 568 | 569 | for (i = 0, c += n; (ci = *(--c)) == 0 ; i++); 570 | 571 | *idx = 0; 572 | *ls = ci < 0; 573 | 574 | /* --- Scan remaining coefficients --- */ 575 | 576 | for (i++, j = LC3_ABS(ci); i < n; i++, j += LC3_ABS(ci)) { 577 | 578 | if ((ci = *(--c)) != 0) { 579 | *idx = (*idx << 1) | *ls; 580 | *ls = ci < 0; 581 | } 582 | 583 | *idx += lc3_sns_mpvq_offsets[i][j]; 584 | } 585 | } 586 | 587 | /** 588 | * Sub-procedure of `sns_deenumerate()`, deenumeration of a vector 589 | * idx, ls Enumeration set 590 | * npulses Number of pulses in the set 591 | * c, n Table of pulses configuration, and length 592 | */ 593 | static void deenum_mvpq(int idx, bool ls, int npulses, int *c, int n) 594 | { 595 | int i; 596 | 597 | /* --- Scan for coefficients --- */ 598 | 599 | for (i = n-1; i >= 0 && idx; i--) { 600 | 601 | int ci = 0; 602 | 603 | for (ci = 0; idx < lc3_sns_mpvq_offsets[i][npulses - ci]; ci++); 604 | idx -= lc3_sns_mpvq_offsets[i][npulses - ci]; 605 | 606 | *(c++) = ls ? -ci : ci; 607 | npulses -= ci; 608 | if (ci > 0) { 609 | ls = idx & 1; 610 | idx >>= 1; 611 | } 612 | } 613 | 614 | /* --- Set last significant --- */ 615 | 616 | int ci = npulses; 617 | 618 | if (i-- >= 0) 619 | *(c++) = ls ? -ci : ci; 620 | 621 | while (i-- >= 0) 622 | *(c++) = 0; 623 | } 624 | 625 | /** 626 | * SNS Enumeration of PVQ configuration 627 | * shape Selected shape index 628 | * c Selected pulse configuration 629 | * idx_a, ls_a Return enumeration set A 630 | * idx_b, ls_b Return enumeration set B (shape = 0) 631 | */ 632 | static void enumerate(int shape, const int *c, 633 | int *idx_a, bool *ls_a, int *idx_b, bool *ls_b) 634 | { 635 | enum_mvpq(c, shape < 2 ? 10 : 16, idx_a, ls_a); 636 | 637 | if (shape == 0) 638 | enum_mvpq(c + 10, 6, idx_b, ls_b); 639 | } 640 | 641 | /** 642 | * SNS Deenumeration of PVQ configuration 643 | * shape Selected shape index 644 | * idx_a, ls_a enumeration set A 645 | * idx_b, ls_b enumeration set B (shape = 0) 646 | * c Return pulse configuration 647 | */ 648 | static void deenumerate(int shape, 649 | int idx_a, bool ls_a, int idx_b, bool ls_b, int *c) 650 | { 651 | int npulses_a = (const int []){ 10, 10, 8, 6 }[shape]; 652 | 653 | deenum_mvpq(idx_a, ls_a, npulses_a, c, shape < 2 ? 10 : 16); 654 | 655 | if (shape == 0) 656 | deenum_mvpq(idx_b, ls_b, 1, c + 10, 6); 657 | else if (shape == 1) 658 | memset(c + 10, 0, 6 * sizeof(*c)); 659 | } 660 | 661 | 662 | /* ---------------------------------------------------------------------------- 663 | * Filtering 664 | * -------------------------------------------------------------------------- */ 665 | 666 | /** 667 | * Spectral shaping 668 | * dt, sr Duration and samplerate of the frame 669 | * scf_q Quantized scale factors 670 | * inv True on inverse shaping, False otherwise 671 | * x Spectral coefficients 672 | * y Return shapped coefficients 673 | * 674 | * `x` and `y` can be the same buffer 675 | */ 676 | static void spectral_shaping(enum lc3_dt dt, enum lc3_srate sr, 677 | const float *scf_q, bool inv, const float *x, float *y) 678 | { 679 | /* --- Interpolate scale factors --- */ 680 | 681 | float scf[LC3_NUM_BANDS]; 682 | float s0, s1 = inv ? -scf_q[0] : scf_q[0]; 683 | 684 | scf[0] = scf[1] = s1; 685 | for (int i = 0; i < 15; i++) { 686 | s0 = s1, s1 = inv ? -scf_q[i+1] : scf_q[i+1]; 687 | scf[4*i+2] = s0 + 0.125 * (s1 - s0); 688 | scf[4*i+3] = s0 + 0.375 * (s1 - s0); 689 | scf[4*i+4] = s0 + 0.625 * (s1 - s0); 690 | scf[4*i+5] = s0 + 0.875 * (s1 - s0); 691 | } 692 | scf[62] = s1 + 0.125 * (s1 - s0); 693 | scf[63] = s1 + 0.375 * (s1 - s0); 694 | 695 | int nb = LC3_MIN(lc3_band_lim[dt][sr][LC3_NUM_BANDS], LC3_NUM_BANDS); 696 | int n2 = LC3_NUM_BANDS - nb; 697 | 698 | for (int i2 = 0; i2 < n2; i2++) 699 | scf[i2] = 0.5 * (scf[2*i2] + scf[2*i2+1]); 700 | 701 | if (n2 > 0) 702 | memmove(scf + n2, scf + 2*n2, (nb - n2) * sizeof(float)); 703 | 704 | /* --- Spectral shaping --- */ 705 | 706 | const int *lim = lc3_band_lim[dt][sr]; 707 | 708 | for (int i = 0, ib = 0; ib < nb; ib++) { 709 | float g_sns = powf(2, -scf[ib]); 710 | 711 | for ( ; i < lim[ib+1]; i++) 712 | y[i] = x[i] * g_sns; 713 | } 714 | } 715 | 716 | 717 | /* ---------------------------------------------------------------------------- 718 | * Interface 719 | * -------------------------------------------------------------------------- */ 720 | 721 | /** 722 | * SNS analysis 723 | */ 724 | void lc3_sns_analyze(enum lc3_dt dt, enum lc3_srate sr, 725 | const float *eb, bool att, struct lc3_sns_data *data, 726 | const float *x, float *y) 727 | { 728 | /* Processing steps : 729 | * - Determine 16 scale factors from bands energy estimation 730 | * - Get codebooks indexes that match thoses scale factors 731 | * - Quantize the residual with the selected codebook 732 | * - The pulse configuration `c[]` is enumerated 733 | * - Finally shape the spectrum coefficients accordingly */ 734 | 735 | float scf[16], cn[4][16]; 736 | int c[4][16]; 737 | 738 | compute_scale_factors(dt, sr, eb, att, scf); 739 | 740 | resolve_codebooks(scf, &data->lfcb, &data->hfcb); 741 | 742 | quantize(scf, data->lfcb, data->hfcb, 743 | c, cn, &data->shape, &data->gain); 744 | 745 | unquantize(data->lfcb, data->hfcb, 746 | cn[data->shape], data->shape, data->gain, scf); 747 | 748 | enumerate(data->shape, c[data->shape], 749 | &data->idx_a, &data->ls_a, &data->idx_b, &data->ls_b); 750 | 751 | spectral_shaping(dt, sr, scf, false, x, y); 752 | } 753 | 754 | /** 755 | * SNS synthesis 756 | */ 757 | void lc3_sns_synthesize(enum lc3_dt dt, enum lc3_srate sr, 758 | const lc3_sns_data_t *data, const float *x, float *y) 759 | { 760 | float scf[16], cn[16]; 761 | int c[16]; 762 | 763 | deenumerate(data->shape, 764 | data->idx_a, data->ls_a, data->idx_b, data->ls_b, c); 765 | 766 | normalize(c, cn); 767 | 768 | unquantize(data->lfcb, data->hfcb, cn, data->shape, data->gain, scf); 769 | 770 | spectral_shaping(dt, sr, scf, true, x, y); 771 | } 772 | 773 | /** 774 | * Return number of bits coding the bitstream data 775 | */ 776 | int lc3_sns_get_nbits(void) 777 | { 778 | return 38; 779 | } 780 | 781 | /** 782 | * Put bitstream data 783 | */ 784 | void lc3_sns_put_data(lc3_bits_t *bits, const struct lc3_sns_data *data) 785 | { 786 | /* --- Codebooks --- */ 787 | 788 | lc3_put_bits(bits, data->lfcb, 5); 789 | lc3_put_bits(bits, data->hfcb, 5); 790 | 791 | /* --- Shape, gain and vectors --- * 792 | * Write MSB bit of shape index, next LSB bits of shape and gain, 793 | * and MVPQ vectors indexes are muxed */ 794 | 795 | int shape_msb = data->shape >> 1; 796 | lc3_put_bit(bits, shape_msb); 797 | 798 | if (shape_msb == 0) { 799 | const int size_a = 2390004; 800 | int submode = data->shape & 1; 801 | 802 | int mux_high = submode == 0 ? 803 | 2 * (data->idx_b + 1) + data->ls_b : data->gain & 1; 804 | int mux_code = mux_high * size_a + data->idx_a; 805 | 806 | lc3_put_bits(bits, data->gain >> submode, 1); 807 | lc3_put_bits(bits, data->ls_a, 1); 808 | lc3_put_bits(bits, mux_code, 25); 809 | 810 | } else { 811 | const int size_a = 15158272; 812 | int submode = data->shape & 1; 813 | 814 | int mux_code = submode == 0 ? 815 | data->idx_a : size_a + 2 * data->idx_a + (data->gain & 1); 816 | 817 | lc3_put_bits(bits, data->gain >> submode, 2); 818 | lc3_put_bits(bits, data->ls_a, 1); 819 | lc3_put_bits(bits, mux_code, 24); 820 | } 821 | } 822 | 823 | /** 824 | * Get bitstream data 825 | */ 826 | int lc3_sns_get_data(lc3_bits_t *bits, struct lc3_sns_data *data) 827 | { 828 | /* --- Codebooks --- */ 829 | 830 | *data = (struct lc3_sns_data){ 831 | .lfcb = lc3_get_bits(bits, 5), 832 | .hfcb = lc3_get_bits(bits, 5) 833 | }; 834 | 835 | /* --- Shape, gain and vectors --- */ 836 | 837 | int shape_msb = lc3_get_bit(bits); 838 | data->gain = lc3_get_bits(bits, 1 + shape_msb); 839 | data->ls_a = lc3_get_bit(bits); 840 | 841 | int mux_code = lc3_get_bits(bits, 25 - shape_msb); 842 | 843 | if (shape_msb == 0) { 844 | const int size_a = 2390004; 845 | 846 | if (mux_code >= size_a * 14) 847 | return -1; 848 | 849 | data->idx_a = mux_code % size_a; 850 | mux_code = mux_code / size_a; 851 | 852 | data->shape = (mux_code < 2); 853 | 854 | if (data->shape == 0) { 855 | data->idx_b = (mux_code - 2) / 2; 856 | data->ls_b = (mux_code - 2) % 2; 857 | } else { 858 | data->gain = (data->gain << 1) + (mux_code % 2); 859 | } 860 | 861 | } else { 862 | const int size_a = 15158272; 863 | 864 | if (mux_code >= size_a + 1549824) 865 | return -1; 866 | 867 | data->shape = 2 + (mux_code >= size_a); 868 | if (data->shape == 2) { 869 | data->idx_a = mux_code; 870 | } else { 871 | mux_code -= size_a; 872 | data->idx_a = mux_code / 2; 873 | data->gain = (data->gain << 1) + (mux_code % 2); 874 | } 875 | } 876 | 877 | return 0; 878 | } 879 | --------------------------------------------------------------------------------