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