1 /****************************************************************************** 2 * 3 * Copyright 2022 Google LLC 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /** 20 * LC3 - Common constants and types 21 */ 22 23 #ifndef __LC3_COMMON_H 24 #define __LC3_COMMON_H 25 26 #include <lc3.h> 27 #include "fastmath.h" 28 29 #include <stdalign.h> 30 #include <limits.h> 31 #include <string.h> 32 33 #ifdef __ARM_ARCH 34 #include <arm_acle.h> 35 #endif 36 37 38 /** 39 * Hot Function attribute 40 * Selectively disable sanitizer 41 */ 42 43 #ifdef __clang__ 44 45 #define LC3_HOT \ 46 __attribute__((no_sanitize("bounds"))) \ 47 __attribute__((no_sanitize("integer"))) 48 49 #else /* __clang__ */ 50 51 #define LC3_HOT 52 53 #endif /* __clang__ */ 54 55 56 /** 57 * Macros 58 * MIN/MAX Minimum and maximum between 2 values 59 * CLIP Clip a value between low and high limits 60 * SATXX Signed saturation on 'xx' bits 61 * ABS Return absolute value 62 */ 63 64 #define LC3_MIN(a, b) ( (a) < (b) ? (a) : (b) ) 65 #define LC3_MAX(a, b) ( (a) > (b) ? (a) : (b) ) 66 67 #define LC3_CLIP(v, min, max) LC3_MIN(LC3_MAX(v, min), max) 68 #define LC3_SAT16(v) LC3_CLIP(v, -(1 << 15), (1 << 15) - 1) 69 #define LC3_SAT24(v) LC3_CLIP(v, -(1 << 23), (1 << 23) - 1) 70 71 #define LC3_ABS(v) ( (v) < 0 ? -(v) : (v) ) 72 73 74 #ifdef __ARM_FEATURE_SAT 75 76 #undef LC3_SAT16 77 #define LC3_SAT16(v) __ssat(v, 16) 78 79 #undef LC3_SAT24 80 #define LC3_SAT24(v) __ssat(v, 24) 81 82 #endif /* __ARM_FEATURE_SAT */ 83 84 85 /** 86 * Convert `dt` in us and `sr` in KHz 87 */ 88 89 #define LC3_DT_US(dt) \ 90 ( (3 + (dt)) * 2500 ) 91 92 #define LC3_SRATE_KHZ(sr) \ 93 ( (1 + (sr) + ((sr) == LC3_SRATE_48K)) * 8 ) 94 95 96 /** 97 * Return number of samples, delayed samples and 98 * encoded spectrum coefficients within a frame 99 * - For encoding, keep 1.25 ms for temporal window 100 * - For decoding, keep 18 ms of history, aligned on frames, and a frame 101 */ 102 103 #define LC3_NS(dt, sr) \ 104 ( 20 * (3 + (dt)) * (1 + (sr) + ((sr) == LC3_SRATE_48K)) ) 105 106 #define LC3_ND(dt, sr) \ 107 ( (dt) == LC3_DT_7M5 ? 23 * LC3_NS(dt, sr) / 30 \ 108 : 5 * LC3_NS(dt, sr) / 8 ) 109 110 #define LC3_NE(dt, sr) \ 111 ( 20 * (3 + (dt)) * (1 + (sr)) ) 112 113 #define LC3_MAX_NE \ 114 LC3_NE(LC3_DT_10M, LC3_SRATE_48K) 115 116 #define LC3_NT(sr_hz) \ 117 ( (5 * LC3_SRATE_KHZ(sr)) / 4 ) 118 119 #define LC3_NH(dt, sr) \ 120 ( ((3 - dt) + 1) * LC3_NS(dt, sr) ) 121 122 123 /** 124 * Bandwidth, mapped to Nyquist frequency of samplerates 125 */ 126 127 enum lc3_bandwidth { 128 LC3_BANDWIDTH_NB = LC3_SRATE_8K, 129 LC3_BANDWIDTH_WB = LC3_SRATE_16K, 130 LC3_BANDWIDTH_SSWB = LC3_SRATE_24K, 131 LC3_BANDWIDTH_SWB = LC3_SRATE_32K, 132 LC3_BANDWIDTH_FB = LC3_SRATE_48K, 133 134 LC3_NUM_BANDWIDTH, 135 }; 136 137 138 /** 139 * Complex floating point number 140 */ 141 142 struct lc3_complex 143 { 144 float re, im; 145 }; 146 147 148 #endif /* __LC3_COMMON_H */ 149