xref: /btstack/src/classic/btstack_sbc_plc.c (revision 1a6822024624aab466a9aff5f67325f81ed6d5e2)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  * btstack_sbc_plc.c
40  *
41  */
42 
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include "btstack_sbc_plc.h"
49 
50 static uint8_t indices0[] = { 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x77, 0x6d,
51 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
52 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
53 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
54 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c};
55 
56 /* Raised COSine table for OLA */
57 static float rcos[SBC_OLAL] = {
58     0.99148655f,0.96623611f,0.92510857f,0.86950446f,
59     0.80131732f,0.72286918f,0.63683150f,0.54613418f,
60     0.45386582f,0.36316850f,0.27713082f,0.19868268f,
61     0.13049554f,0.07489143f,0.03376389f,0.00851345f};
62 
63 // taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi
64 // Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation
65 static float sqrt3(const float x){
66     union {
67         int i;
68         float x;
69     } u;
70     u.x = x;
71     u.i = (1<<29) + (u.i >> 1) - (1<<22);
72 
73     // Two Babylonian Steps (simplified from:)
74     // u.x = 0.5f * (u.x + x/u.x);
75     // u.x = 0.5f * (u.x + x/u.x);
76     u.x =       u.x + x/u.x;
77     u.x = 0.25f*u.x + x/u.x;
78 
79     return u.x;
80 }
81 
82 static float absolute(float x){
83      if (x < 0) x = -x;
84      return x;
85 }
86 
87 static float CrossCorrelation(int16_t *x, int16_t *y){
88     float num = 0;
89     float den = 0;
90     float x2 = 0;
91     float y2 = 0;
92     int   m;
93     for (m=0;m<SBC_M;m++){
94         num+=((float)x[m])*y[m];
95         x2+=((float)x[m])*x[m];
96         y2+=((float)y[m])*y[m];
97     }
98     den = (float)sqrt3(x2*y2);
99     return num/den;
100 }
101 
102 static int PatternMatch(int16_t *y){
103     float maxCn = -999999.0;  /* large negative number */
104     int   bestmatch = 0;
105     float Cn;
106     int   n;
107     for (n=0;n<SBC_N;n++){
108         Cn = CrossCorrelation(&y[SBC_LHIST-SBC_M] /* x */, &y[n]);
109         if (Cn>maxCn){
110             bestmatch=n;
111             maxCn = Cn;
112         }
113     }
114     return bestmatch;
115 }
116 
117 
118 static float AmplitudeMatch(int16_t *y, int16_t bestmatch) {
119     int   i;
120     float sumx = 0;
121     float sumy = 0.000001f;
122     float sf;
123 
124     for (i=0;i<SBC_FS;i++){
125         sumx += absolute(y[SBC_LHIST-SBC_FS+i]);
126         sumy += absolute(y[bestmatch+i]);
127     }
128     sf = sumx/sumy;
129     /* This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts */
130     if (sf<0.75f) sf=0.75f;
131     if (sf>1.2f) sf=1.2f;
132     return sf;
133 }
134 
135 static int16_t crop_to_int16(float val){
136     float croped_val = val;
137     if (croped_val > 32767.0)  croped_val= 32767.0;
138     if (croped_val < -32768.0) croped_val=-32768.0;
139     return (int16_t) croped_val;
140 }
141 
142 uint8_t * btstack_sbc_plc_zero_signal_frame(void){
143     return (uint8_t *)&indices0;
144 }
145 
146 void btstack_sbc_plc_init(btstack_sbc_plc_state_t *plc_state){
147     plc_state->nbf=0;
148     plc_state->bestlag=0;
149     memset(plc_state->hist,0,sizeof(plc_state->hist));
150 }
151 
152 void btstack_sbc_plc_bad_frame(btstack_sbc_plc_state_t *plc_state, int16_t *ZIRbuf, int16_t *out){
153     float val;
154     int   i = 0;
155     float sf = 1;
156 
157     plc_state->nbf++;
158 
159     if (plc_state->nbf==1){
160         /* Perform pattern matching to find where to replicate */
161         plc_state->bestlag = PatternMatch(plc_state->hist);
162         /* the replication begins after the template match */
163         plc_state->bestlag += SBC_M;
164 
165         /* Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet */
166         sf = AmplitudeMatch(plc_state->hist, plc_state->bestlag);
167         for (i=0;i<SBC_OLAL;i++){
168             float left  = ZIRbuf[i];
169             float right = sf*plc_state->hist[plc_state->bestlag+i];
170             val = left*rcos[i] + right*rcos[SBC_OLAL-1-i];
171             plc_state->hist[SBC_LHIST+i] = crop_to_int16(val);
172         }
173 
174         for (;i<SBC_FS;i++){
175             val = sf*plc_state->hist[plc_state->bestlag+i];
176             plc_state->hist[SBC_LHIST+i] = crop_to_int16(val);
177         }
178 
179         for (;i<SBC_FS+SBC_OLAL;i++){
180             float left  = sf*plc_state->hist[plc_state->bestlag+i];
181             float right = plc_state->hist[plc_state->bestlag+i];
182             val = left*rcos[i-SBC_FS]+right*rcos[SBC_FS+SBC_OLAL-1-i];
183             plc_state->hist[SBC_LHIST+i] = crop_to_int16(val);
184         }
185 
186         for (;i<SBC_FS+SBC_RT+SBC_OLAL;i++){
187             plc_state->hist[SBC_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
188         }
189 
190     } else {
191         for (i=0;i<SBC_FS+SBC_RT+SBC_OLAL;i++){
192             plc_state->hist[SBC_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
193         }
194     }
195     for (i=0;i<SBC_FS;i++){
196         out[i] = plc_state->hist[SBC_LHIST+i];
197     }
198 
199     /* shift the history buffer */
200     for (i=0;i<SBC_LHIST+SBC_RT+SBC_OLAL;i++){
201         plc_state->hist[i] = plc_state->hist[i+SBC_FS];
202     }
203 
204 }
205 
206 void btstack_sbc_plc_good_frame(btstack_sbc_plc_state_t *plc_state, int16_t *in, int16_t *out){
207     float val;
208     int i = 0;
209     if (plc_state->nbf>0){
210         for (i=0;i<SBC_RT;i++){
211             out[i] = plc_state->hist[SBC_LHIST+i];
212         }
213 
214         for (;i<SBC_RT+SBC_OLAL;i++){
215             float left  = plc_state->hist[SBC_LHIST+i];
216             float right = in[i];
217             val = left*rcos[i-SBC_RT] + right*rcos[SBC_OLAL-1-i+SBC_RT];
218             out[i] = (int16_t)val;
219         }
220     }
221     for (;i<SBC_FS;i++){
222         out[i] = in[i];
223     }
224 
225     /*Copy the output to the history buffer */
226     for (i=0;i<SBC_FS;i++){
227         plc_state->hist[SBC_LHIST+i] = out[i];
228     }
229     /* shift the history buffer */
230     for (i=0;i<SBC_LHIST;i++){
231         plc_state->hist[i] = plc_state->hist[i+SBC_FS];
232     }
233 
234     plc_state->nbf=0;
235 }
236