19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald *
34930cef6SMatthias Ringwald * Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald *
59a19cd78SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald * you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald * You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald *
99a19cd78SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald *
119a19cd78SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald * See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald * limitations under the License.
169a19cd78SMatthias Ringwald *
179a19cd78SMatthias Ringwald ******************************************************************************/
189a19cd78SMatthias Ringwald
199a19cd78SMatthias Ringwald #include "bwdet.h"
209a19cd78SMatthias Ringwald
219a19cd78SMatthias Ringwald
229a19cd78SMatthias Ringwald /**
239a19cd78SMatthias Ringwald * Bandwidth detector
249a19cd78SMatthias Ringwald */
lc3_bwdet_run(enum lc3_dt dt,enum lc3_srate sr,const float * e)259a19cd78SMatthias Ringwald enum lc3_bandwidth lc3_bwdet_run(
269a19cd78SMatthias Ringwald enum lc3_dt dt, enum lc3_srate sr, const float *e)
279a19cd78SMatthias Ringwald {
28*6897da5cSDirk Helbig /* Bandwidth regions */
299a19cd78SMatthias Ringwald
309a19cd78SMatthias Ringwald struct region { int is : 8; int ie : 8; };
319a19cd78SMatthias Ringwald
32*6897da5cSDirk Helbig #if LC3_PLUS
339a19cd78SMatthias Ringwald
34*6897da5cSDirk Helbig static const struct region bws_table_2m5[][LC3_BANDWIDTH_SWB+1] = {
35*6897da5cSDirk Helbig { { 24, 34+1 } },
36*6897da5cSDirk Helbig { { 24, 32+1 }, { 35, 39+1 } },
37*6897da5cSDirk Helbig { { 24, 31+1 }, { 33, 38+1 }, { 39, 42+1 } },
38*6897da5cSDirk Helbig { { 22, 29+1 }, { 31, 35+1 }, { 37, 40+1 }, { 41, 43+1 } },
39*6897da5cSDirk Helbig };
40*6897da5cSDirk Helbig
41*6897da5cSDirk Helbig static const struct region bws_table_5m[][LC3_BANDWIDTH_SWB+1] = {
42*6897da5cSDirk Helbig { { 39, 49+1 } },
43*6897da5cSDirk Helbig { { 35, 44+1 }, { 47, 51+1 } },
44*6897da5cSDirk Helbig { { 34, 42+1 }, { 44, 49+1 }, { 50, 53+1 } },
45*6897da5cSDirk Helbig { { 32, 40+1 }, { 42, 46+1 }, { 48, 51+1 }, { 52, 54+1 } },
46*6897da5cSDirk Helbig };
47*6897da5cSDirk Helbig
48*6897da5cSDirk Helbig #endif /* LC3_PLUS */
49*6897da5cSDirk Helbig
50*6897da5cSDirk Helbig static const struct region bws_table_7m5[][LC3_BANDWIDTH_SWB+1] = {
519a19cd78SMatthias Ringwald { { 51, 63+1 } },
529a19cd78SMatthias Ringwald { { 45, 55+1 }, { 58, 63+1 } },
539a19cd78SMatthias Ringwald { { 42, 51+1 }, { 53, 58+1 }, { 60, 63+1 } },
549a19cd78SMatthias Ringwald { { 40, 48+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
55*6897da5cSDirk Helbig };
569a19cd78SMatthias Ringwald
57*6897da5cSDirk Helbig static const struct region bws_table_10m[][LC3_BANDWIDTH_SWB+1] = {
589a19cd78SMatthias Ringwald { { 53, 63+1 } },
599a19cd78SMatthias Ringwald { { 47, 56+1 }, { 59, 63+1 } },
609a19cd78SMatthias Ringwald { { 44, 52+1 }, { 54, 59+1 }, { 60, 63+1 } },
619a19cd78SMatthias Ringwald { { 41, 49+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
629a19cd78SMatthias Ringwald };
639a19cd78SMatthias Ringwald
64*6897da5cSDirk Helbig static const struct region (*bws_table[])[LC3_BANDWIDTH_SWB+1] = {
65*6897da5cSDirk Helbig [LC3_DT_2M5] = LC3_IF_PLUS(bws_table_2m5, NULL),
66*6897da5cSDirk Helbig [LC3_DT_5M ] = LC3_IF_PLUS(bws_table_5m , NULL),
67*6897da5cSDirk Helbig [LC3_DT_7M5] = bws_table_7m5,
68*6897da5cSDirk Helbig [LC3_DT_10M] = bws_table_10m,
69*6897da5cSDirk Helbig };
70*6897da5cSDirk Helbig
71*6897da5cSDirk Helbig static const int l_table[LC3_NUM_DT][LC3_BANDWIDTH_SWB+1] = {
72*6897da5cSDirk Helbig [LC3_DT_2M5] = { 4, 4, 3, 1 },
73*6897da5cSDirk Helbig [LC3_DT_5M ] = { 4, 4, 3, 1 },
749a19cd78SMatthias Ringwald [LC3_DT_7M5] = { 4, 4, 3, 2 },
759a19cd78SMatthias Ringwald [LC3_DT_10M] = { 4, 4, 3, 1 },
769a19cd78SMatthias Ringwald };
779a19cd78SMatthias Ringwald
789a19cd78SMatthias Ringwald /* --- Stage 1 ---
799a19cd78SMatthias Ringwald * Determine bw0 candidate */
809a19cd78SMatthias Ringwald
819a19cd78SMatthias Ringwald enum lc3_bandwidth bw0 = LC3_BANDWIDTH_NB;
829a19cd78SMatthias Ringwald enum lc3_bandwidth bwn = (enum lc3_bandwidth)sr;
839a19cd78SMatthias Ringwald
84*6897da5cSDirk Helbig if (bwn <= bw0 || lc3_hr(sr))
859a19cd78SMatthias Ringwald return bwn;
869a19cd78SMatthias Ringwald
879a19cd78SMatthias Ringwald const struct region *bwr = bws_table[dt][bwn-1];
889a19cd78SMatthias Ringwald
899a19cd78SMatthias Ringwald for (enum lc3_bandwidth bw = bw0; bw < bwn; bw++) {
909a19cd78SMatthias Ringwald int i = bwr[bw].is, ie = bwr[bw].ie;
919a19cd78SMatthias Ringwald int n = ie - i;
929a19cd78SMatthias Ringwald
939a19cd78SMatthias Ringwald float se = e[i];
949a19cd78SMatthias Ringwald for (i++; i < ie; i++)
959a19cd78SMatthias Ringwald se += e[i];
969a19cd78SMatthias Ringwald
979a19cd78SMatthias Ringwald if (se >= (10 << (bw == LC3_BANDWIDTH_NB)) * n)
989a19cd78SMatthias Ringwald bw0 = bw + 1;
999a19cd78SMatthias Ringwald }
1009a19cd78SMatthias Ringwald
1019a19cd78SMatthias Ringwald /* --- Stage 2 ---
1029a19cd78SMatthias Ringwald * Detect drop above cut-off frequency.
1039a19cd78SMatthias Ringwald * The Tc condition (13) is precalculated, as
1049a19cd78SMatthias Ringwald * Tc[] = 10 ^ (n / 10) , n = { 15, 23, 20, 20 } */
1059a19cd78SMatthias Ringwald
1069a19cd78SMatthias Ringwald int hold = bw0 >= bwn;
1079a19cd78SMatthias Ringwald
1089a19cd78SMatthias Ringwald if (!hold) {
1099a19cd78SMatthias Ringwald int i0 = bwr[bw0].is, l = l_table[dt][bw0];
1109a19cd78SMatthias Ringwald float tc = (const float []){
1119a19cd78SMatthias Ringwald 31.62277660, 199.52623150, 100, 100 }[bw0];
1129a19cd78SMatthias Ringwald
1139a19cd78SMatthias Ringwald for (int i = i0 - l + 1; !hold && i <= i0 + 1; i++) {
1149a19cd78SMatthias Ringwald hold = e[i-l] > tc * e[i];
1159a19cd78SMatthias Ringwald }
1169a19cd78SMatthias Ringwald
1179a19cd78SMatthias Ringwald }
1189a19cd78SMatthias Ringwald
1199a19cd78SMatthias Ringwald return hold ? bw0 : bwn;
1209a19cd78SMatthias Ringwald }
1219a19cd78SMatthias Ringwald
1229a19cd78SMatthias Ringwald /**
1239a19cd78SMatthias Ringwald * Return number of bits coding the bandwidth value
1249a19cd78SMatthias Ringwald */
lc3_bwdet_get_nbits(enum lc3_srate sr)1259a19cd78SMatthias Ringwald int lc3_bwdet_get_nbits(enum lc3_srate sr)
1269a19cd78SMatthias Ringwald {
127*6897da5cSDirk Helbig return lc3_hr(sr) ? 0 : (sr > 0) + (sr > 1) + (sr > 3);
1289a19cd78SMatthias Ringwald }
1299a19cd78SMatthias Ringwald
1309a19cd78SMatthias Ringwald /**
1319a19cd78SMatthias Ringwald * Put bandwidth indication
1329a19cd78SMatthias Ringwald */
lc3_bwdet_put_bw(lc3_bits_t * bits,enum lc3_srate sr,enum lc3_bandwidth bw)1339a19cd78SMatthias Ringwald void lc3_bwdet_put_bw(lc3_bits_t *bits,
1349a19cd78SMatthias Ringwald enum lc3_srate sr, enum lc3_bandwidth bw)
1359a19cd78SMatthias Ringwald {
1369a19cd78SMatthias Ringwald int nbits_bw = lc3_bwdet_get_nbits(sr);
1379a19cd78SMatthias Ringwald if (nbits_bw > 0)
1389a19cd78SMatthias Ringwald lc3_put_bits(bits, bw, nbits_bw);
1399a19cd78SMatthias Ringwald }
1409a19cd78SMatthias Ringwald
1419a19cd78SMatthias Ringwald /**
1429a19cd78SMatthias Ringwald * Get bandwidth indication
1439a19cd78SMatthias Ringwald */
lc3_bwdet_get_bw(lc3_bits_t * bits,enum lc3_srate sr,enum lc3_bandwidth * bw)1449a19cd78SMatthias Ringwald int lc3_bwdet_get_bw(lc3_bits_t *bits,
1459a19cd78SMatthias Ringwald enum lc3_srate sr, enum lc3_bandwidth *bw)
1469a19cd78SMatthias Ringwald {
1479a19cd78SMatthias Ringwald enum lc3_bandwidth max_bw = (enum lc3_bandwidth)sr;
1489a19cd78SMatthias Ringwald int nbits_bw = lc3_bwdet_get_nbits(sr);
1499a19cd78SMatthias Ringwald
150*6897da5cSDirk Helbig *bw = nbits_bw <= 0 ? max_bw :
151*6897da5cSDirk Helbig (enum lc3_bandwidth)lc3_get_bits(bits, nbits_bw);
152*6897da5cSDirk Helbig
1539a19cd78SMatthias Ringwald return *bw > max_bw ? (*bw = max_bw), -1 : 0;
1549a19cd78SMatthias Ringwald }
155