xref: /btstack/3rd-party/lc3-google/src/bwdet.c (revision 4930cef6e21e6da2d7571b9259c7f0fb8bed3d01)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
3*4930cef6SMatthias 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  */
259a19cd78SMatthias Ringwald enum lc3_bandwidth lc3_bwdet_run(
269a19cd78SMatthias Ringwald     enum lc3_dt dt, enum lc3_srate sr, const float *e)
279a19cd78SMatthias Ringwald {
289a19cd78SMatthias Ringwald     /* Bandwidth regions (Table 3.6)  */
299a19cd78SMatthias Ringwald 
309a19cd78SMatthias Ringwald     struct region { int is : 8; int ie : 8; };
319a19cd78SMatthias Ringwald 
329a19cd78SMatthias Ringwald     static const struct region bws_table[LC3_NUM_DT]
339a19cd78SMatthias Ringwald             [LC3_NUM_BANDWIDTH-1][LC3_NUM_BANDWIDTH-1] = {
349a19cd78SMatthias Ringwald 
359a19cd78SMatthias Ringwald         [LC3_DT_7M5] = {
369a19cd78SMatthias Ringwald             { { 51, 63+1 } },
379a19cd78SMatthias Ringwald             { { 45, 55+1 }, { 58, 63+1 } },
389a19cd78SMatthias Ringwald             { { 42, 51+1 }, { 53, 58+1 }, { 60, 63+1 } },
399a19cd78SMatthias Ringwald             { { 40, 48+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
409a19cd78SMatthias Ringwald         },
419a19cd78SMatthias Ringwald 
429a19cd78SMatthias Ringwald         [LC3_DT_10M] = {
439a19cd78SMatthias Ringwald             { { 53, 63+1 } },
449a19cd78SMatthias Ringwald             { { 47, 56+1 }, { 59, 63+1 } },
459a19cd78SMatthias Ringwald             { { 44, 52+1 }, { 54, 59+1 }, { 60, 63+1 } },
469a19cd78SMatthias Ringwald             { { 41, 49+1 }, { 51, 55+1 }, { 57, 60+1 }, { 61, 63+1 } },
479a19cd78SMatthias Ringwald         },
489a19cd78SMatthias Ringwald     };
499a19cd78SMatthias Ringwald 
509a19cd78SMatthias Ringwald     static const int l_table[LC3_NUM_DT][LC3_NUM_BANDWIDTH-1] = {
519a19cd78SMatthias Ringwald         [LC3_DT_7M5] = { 4, 4, 3, 2 },
529a19cd78SMatthias Ringwald         [LC3_DT_10M] = { 4, 4, 3, 1 },
539a19cd78SMatthias Ringwald     };
549a19cd78SMatthias Ringwald 
559a19cd78SMatthias Ringwald     /* --- Stage 1 ---
569a19cd78SMatthias Ringwald      * Determine bw0 candidate */
579a19cd78SMatthias Ringwald 
589a19cd78SMatthias Ringwald     enum lc3_bandwidth bw0 = LC3_BANDWIDTH_NB;
599a19cd78SMatthias Ringwald     enum lc3_bandwidth bwn = (enum lc3_bandwidth)sr;
609a19cd78SMatthias Ringwald 
619a19cd78SMatthias Ringwald     if (bwn <= bw0)
629a19cd78SMatthias Ringwald         return bwn;
639a19cd78SMatthias Ringwald 
649a19cd78SMatthias Ringwald     const struct region *bwr = bws_table[dt][bwn-1];
659a19cd78SMatthias Ringwald 
669a19cd78SMatthias Ringwald     for (enum lc3_bandwidth bw = bw0; bw < bwn; bw++) {
679a19cd78SMatthias Ringwald         int i = bwr[bw].is, ie = bwr[bw].ie;
689a19cd78SMatthias Ringwald         int n = ie - i;
699a19cd78SMatthias Ringwald 
709a19cd78SMatthias Ringwald         float se = e[i];
719a19cd78SMatthias Ringwald         for (i++; i < ie; i++)
729a19cd78SMatthias Ringwald             se += e[i];
739a19cd78SMatthias Ringwald 
749a19cd78SMatthias Ringwald         if (se >= (10 << (bw == LC3_BANDWIDTH_NB)) * n)
759a19cd78SMatthias Ringwald             bw0 = bw + 1;
769a19cd78SMatthias Ringwald     }
779a19cd78SMatthias Ringwald 
789a19cd78SMatthias Ringwald     /* --- Stage 2 ---
799a19cd78SMatthias Ringwald      * Detect drop above cut-off frequency.
809a19cd78SMatthias Ringwald      * The Tc condition (13) is precalculated, as
819a19cd78SMatthias Ringwald      * Tc[] = 10 ^ (n / 10) , n = { 15, 23, 20, 20 } */
829a19cd78SMatthias Ringwald 
839a19cd78SMatthias Ringwald     int hold = bw0 >= bwn;
849a19cd78SMatthias Ringwald 
859a19cd78SMatthias Ringwald     if (!hold) {
869a19cd78SMatthias Ringwald         int i0 = bwr[bw0].is, l = l_table[dt][bw0];
879a19cd78SMatthias Ringwald         float tc = (const float []){
889a19cd78SMatthias Ringwald              31.62277660, 199.52623150, 100, 100 }[bw0];
899a19cd78SMatthias Ringwald 
909a19cd78SMatthias Ringwald         for (int i = i0 - l + 1; !hold && i <= i0 + 1; i++) {
919a19cd78SMatthias Ringwald             hold = e[i-l] > tc * e[i];
929a19cd78SMatthias Ringwald         }
939a19cd78SMatthias Ringwald 
949a19cd78SMatthias Ringwald     }
959a19cd78SMatthias Ringwald 
969a19cd78SMatthias Ringwald     return hold ? bw0 : bwn;
979a19cd78SMatthias Ringwald }
989a19cd78SMatthias Ringwald 
999a19cd78SMatthias Ringwald /**
1009a19cd78SMatthias Ringwald  * Return number of bits coding the bandwidth value
1019a19cd78SMatthias Ringwald  */
1029a19cd78SMatthias Ringwald int lc3_bwdet_get_nbits(enum lc3_srate sr)
1039a19cd78SMatthias Ringwald {
1049a19cd78SMatthias Ringwald     return (sr > 0) + (sr > 1) + (sr > 3);
1059a19cd78SMatthias Ringwald }
1069a19cd78SMatthias Ringwald 
1079a19cd78SMatthias Ringwald /**
1089a19cd78SMatthias Ringwald  * Put bandwidth indication
1099a19cd78SMatthias Ringwald  */
1109a19cd78SMatthias Ringwald void lc3_bwdet_put_bw(lc3_bits_t *bits,
1119a19cd78SMatthias Ringwald     enum lc3_srate sr, enum lc3_bandwidth bw)
1129a19cd78SMatthias Ringwald {
1139a19cd78SMatthias Ringwald     int nbits_bw = lc3_bwdet_get_nbits(sr);
1149a19cd78SMatthias Ringwald     if (nbits_bw > 0)
1159a19cd78SMatthias Ringwald         lc3_put_bits(bits, bw, nbits_bw);
1169a19cd78SMatthias Ringwald }
1179a19cd78SMatthias Ringwald 
1189a19cd78SMatthias Ringwald /**
1199a19cd78SMatthias Ringwald  * Get bandwidth indication
1209a19cd78SMatthias Ringwald  */
1219a19cd78SMatthias Ringwald int lc3_bwdet_get_bw(lc3_bits_t *bits,
1229a19cd78SMatthias Ringwald     enum lc3_srate sr, enum lc3_bandwidth *bw)
1239a19cd78SMatthias Ringwald {
1249a19cd78SMatthias Ringwald     enum lc3_bandwidth max_bw = (enum lc3_bandwidth)sr;
1259a19cd78SMatthias Ringwald     int nbits_bw = lc3_bwdet_get_nbits(sr);
1269a19cd78SMatthias Ringwald 
1279a19cd78SMatthias Ringwald     *bw = nbits_bw > 0 ? lc3_get_bits(bits, nbits_bw) : LC3_BANDWIDTH_NB;
1289a19cd78SMatthias Ringwald     return *bw > max_bw ? (*bw = max_bw), -1 : 0;
1299a19cd78SMatthias Ringwald }
130