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 (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  */
lc3_bwdet_get_nbits(enum lc3_srate sr)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  */
lc3_bwdet_put_bw(lc3_bits_t * bits,enum lc3_srate sr,enum lc3_bandwidth bw)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  */
lc3_bwdet_get_bw(lc3_bits_t * bits,enum lc3_srate sr,enum lc3_bandwidth * bw)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