1 /******************************************************************************
2 *
3 * Copyright 2022 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "bwdet.h"
20
21
22 /**
23 * Bandwidth detector
24 */
lc3_bwdet_run(enum lc3_dt dt,enum lc3_srate sr,const float * e)25 enum lc3_bandwidth lc3_bwdet_run(
26 enum lc3_dt dt, enum lc3_srate sr, const float *e)
27 {
28 /* Bandwidth regions */
29
30 struct region { int is : 8; int ie : 8; };
31
32 #if LC3_PLUS
33
34 static const struct region bws_table_2m5[][LC3_BANDWIDTH_SWB+1] = {
35 { { 24, 34+1 } },
36 { { 24, 32+1 }, { 35, 39+1 } },
37 { { 24, 31+1 }, { 33, 38+1 }, { 39, 42+1 } },
38 { { 22, 29+1 }, { 31, 35+1 }, { 37, 40+1 }, { 41, 43+1 } },
39 };
40
41 static const struct region bws_table_5m[][LC3_BANDWIDTH_SWB+1] = {
42 { { 39, 49+1 } },
43 { { 35, 44+1 }, { 47, 51+1 } },
44 { { 34, 42+1 }, { 44, 49+1 }, { 50, 53+1 } },
45 { { 32, 40+1 }, { 42, 46+1 }, { 48, 51+1 }, { 52, 54+1 } },
46 };
47
48 #endif /* LC3_PLUS */
49
50 static const struct region bws_table_7m5[][LC3_BANDWIDTH_SWB+1] = {
51 { { 51, 63+1 } },
52 { { 45, 55+1 }, { 58, 63+1 } },
53 { { 42, 51+1 }, { 53, 58+1 }, { 60, 63+1 } },
54 { { 40, 48+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
55 };
56
57 static const struct region bws_table_10m[][LC3_BANDWIDTH_SWB+1] = {
58 { { 53, 63+1 } },
59 { { 47, 56+1 }, { 59, 63+1 } },
60 { { 44, 52+1 }, { 54, 59+1 }, { 60, 63+1 } },
61 { { 41, 49+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
62 };
63
64 static const struct region (*bws_table[])[LC3_BANDWIDTH_SWB+1] = {
65 [LC3_DT_2M5] = LC3_IF_PLUS(bws_table_2m5, NULL),
66 [LC3_DT_5M ] = LC3_IF_PLUS(bws_table_5m , NULL),
67 [LC3_DT_7M5] = bws_table_7m5,
68 [LC3_DT_10M] = bws_table_10m,
69 };
70
71 static const int l_table[LC3_NUM_DT][LC3_BANDWIDTH_SWB+1] = {
72 [LC3_DT_2M5] = { 4, 4, 3, 1 },
73 [LC3_DT_5M ] = { 4, 4, 3, 1 },
74 [LC3_DT_7M5] = { 4, 4, 3, 2 },
75 [LC3_DT_10M] = { 4, 4, 3, 1 },
76 };
77
78 /* --- Stage 1 ---
79 * Determine bw0 candidate */
80
81 enum lc3_bandwidth bw0 = LC3_BANDWIDTH_NB;
82 enum lc3_bandwidth bwn = (enum lc3_bandwidth)sr;
83
84 if (bwn <= bw0 || lc3_hr(sr))
85 return bwn;
86
87 const struct region *bwr = bws_table[dt][bwn-1];
88
89 for (enum lc3_bandwidth bw = bw0; bw < bwn; bw++) {
90 int i = bwr[bw].is, ie = bwr[bw].ie;
91 int n = ie - i;
92
93 float se = e[i];
94 for (i++; i < ie; i++)
95 se += e[i];
96
97 if (se >= (10 << (bw == LC3_BANDWIDTH_NB)) * n)
98 bw0 = bw + 1;
99 }
100
101 /* --- Stage 2 ---
102 * Detect drop above cut-off frequency.
103 * The Tc condition (13) is precalculated, as
104 * Tc[] = 10 ^ (n / 10) , n = { 15, 23, 20, 20 } */
105
106 int hold = bw0 >= bwn;
107
108 if (!hold) {
109 int i0 = bwr[bw0].is, l = l_table[dt][bw0];
110 float tc = (const float []){
111 31.62277660, 199.52623150, 100, 100 }[bw0];
112
113 for (int i = i0 - l + 1; !hold && i <= i0 + 1; i++) {
114 hold = e[i-l] > tc * e[i];
115 }
116
117 }
118
119 return hold ? bw0 : bwn;
120 }
121
122 /**
123 * Return number of bits coding the bandwidth value
124 */
lc3_bwdet_get_nbits(enum lc3_srate sr)125 int lc3_bwdet_get_nbits(enum lc3_srate sr)
126 {
127 return lc3_hr(sr) ? 0 : (sr > 0) + (sr > 1) + (sr > 3);
128 }
129
130 /**
131 * Put bandwidth indication
132 */
lc3_bwdet_put_bw(lc3_bits_t * bits,enum lc3_srate sr,enum lc3_bandwidth bw)133 void lc3_bwdet_put_bw(lc3_bits_t *bits,
134 enum lc3_srate sr, enum lc3_bandwidth bw)
135 {
136 int nbits_bw = lc3_bwdet_get_nbits(sr);
137 if (nbits_bw > 0)
138 lc3_put_bits(bits, bw, nbits_bw);
139 }
140
141 /**
142 * Get bandwidth indication
143 */
lc3_bwdet_get_bw(lc3_bits_t * bits,enum lc3_srate sr,enum lc3_bandwidth * bw)144 int lc3_bwdet_get_bw(lc3_bits_t *bits,
145 enum lc3_srate sr, enum lc3_bandwidth *bw)
146 {
147 enum lc3_bandwidth max_bw = (enum lc3_bandwidth)sr;
148 int nbits_bw = lc3_bwdet_get_nbits(sr);
149
150 *bw = nbits_bw <= 0 ? max_bw :
151 (enum lc3_bandwidth)lc3_get_bits(bits, nbits_bw);
152
153 return *bw > max_bw ? (*bw = max_bw), -1 : 0;
154 }
155