xref: /btstack/src/classic/btstack_sbc_plc.c (revision de854f9a28de198c2ddef1824d28b975373a1dd8)
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 #define __BTSTACK_FILE__ "btstack_sbc_plc.c"
39 
40 /*
41  * btstack_sbc_plc.c
42  *
43  */
44 
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "btstack_sbc_plc.h"
51 #include "btstack_debug.h"
52 
53 #define SAMPLE_FORMAT int16_t
54 
55 static uint8_t indices0[] = { 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x77, 0x6d,
56 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
57 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
58 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
59 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c};
60 
61 /* Raised COSine table for OLA */
62 static float rcos[SBC_OLAL] = {
63     0.99148655f,0.96623611f,0.92510857f,0.86950446f,
64     0.80131732f,0.72286918f,0.63683150f,0.54613418f,
65     0.45386582f,0.36316850f,0.27713082f,0.19868268f,
66     0.13049554f,0.07489143f,0.03376389f,0.00851345f};
67 
68 // taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi
69 // Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation
70 static float sqrt3(const float x){
71     union {
72         int i;
73         float x;
74     } u;
75     u.x = x;
76     u.i = (1<<29) + (u.i >> 1) - (1<<22);
77 
78     // Two Babylonian Steps (simplified from:)
79     // u.x = 0.5f * (u.x + x/u.x);
80     // u.x = 0.5f * (u.x + x/u.x);
81     u.x =       u.x + x/u.x;
82     u.x = 0.25f*u.x + x/u.x;
83 
84     return u.x;
85 }
86 
87 static float absolute(float x){
88      if (x < 0) x = -x;
89      return x;
90 }
91 
92 static float CrossCorrelation(SAMPLE_FORMAT *x, SAMPLE_FORMAT *y){
93     float num = 0;
94     float den = 0;
95     float x2 = 0;
96     float y2 = 0;
97     int   m;
98     for (m=0;m<SBC_M;m++){
99         num+=((float)x[m])*y[m];
100         x2+=((float)x[m])*x[m];
101         y2+=((float)y[m])*y[m];
102     }
103     den = (float)sqrt3(x2*y2);
104     return num/den;
105 }
106 
107 static int PatternMatch(SAMPLE_FORMAT *y){
108     float maxCn = -999999.0;  // large negative number
109     int   bestmatch = 0;
110     float Cn;
111     int   n;
112     for (n=0;n<SBC_N;n++){
113         Cn = CrossCorrelation(&y[SBC_LHIST-SBC_M], &y[n]);
114         if (Cn>maxCn){
115             bestmatch=n;
116             maxCn = Cn;
117         }
118     }
119     return bestmatch;
120 }
121 
122 static float AmplitudeMatch(SAMPLE_FORMAT *y, SAMPLE_FORMAT bestmatch) {
123     int   i;
124     float sumx = 0;
125     float sumy = 0.000001f;
126     float sf;
127 
128     for (i=0;i<SBC_FS;i++){
129         sumx += absolute(y[SBC_LHIST-SBC_FS+i]);
130         sumy += absolute(y[bestmatch+i]);
131     }
132     sf = sumx/sumy;
133     // This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts
134     if (sf<0.75f) sf=0.75f;
135     if (sf>1.2f) sf=1.2f;
136     return sf;
137 }
138 
139 static SAMPLE_FORMAT crop_sample(float val){
140     float croped_val = val;
141     if (croped_val > 32767.0)  croped_val= 32767.0;
142     if (croped_val < -32768.0) croped_val=-32768.0;
143     return (SAMPLE_FORMAT) croped_val;
144 }
145 
146 uint8_t * btstack_sbc_plc_zero_signal_frame(void){
147     return (uint8_t *)&indices0;
148 }
149 
150 void btstack_sbc_plc_init(btstack_sbc_plc_state_t *plc_state){
151     plc_state->nbf=0;
152     plc_state->bestlag=0;
153     memset(plc_state->hist,0,sizeof(plc_state->hist));
154 }
155 
156 void btstack_sbc_plc_bad_frame(btstack_sbc_plc_state_t *plc_state, SAMPLE_FORMAT *ZIRbuf, SAMPLE_FORMAT *out){
157     float val;
158     int   i = 0;
159     float sf = 1;
160     plc_state->nbf++;
161 
162     plc_state->bad_frames_nr++;
163     plc_state->frame_count++;
164     if (plc_state->max_consecutive_bad_frames_nr < plc_state->nbf){
165         plc_state->max_consecutive_bad_frames_nr = plc_state->nbf;
166     }
167 
168     if (plc_state->nbf==1){
169         // Perform pattern matching to find where to replicate
170         plc_state->bestlag = PatternMatch(plc_state->hist);
171         // the replication begins after the template match
172         plc_state->bestlag += SBC_M;
173 
174         // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet
175         sf = AmplitudeMatch(plc_state->hist, plc_state->bestlag);
176         for (i=0;i<SBC_OLAL;i++){
177             float left  = ZIRbuf[i];
178             float right = sf*plc_state->hist[plc_state->bestlag+i];
179             val = left*rcos[i] + right*rcos[SBC_OLAL-1-i];
180             plc_state->hist[SBC_LHIST+i] = crop_sample(val);
181         }
182 
183         for (;i<SBC_FS;i++){
184             val = sf*plc_state->hist[plc_state->bestlag+i];
185             plc_state->hist[SBC_LHIST+i] = crop_sample(val);
186         }
187 
188         for (;i<SBC_FS+SBC_OLAL;i++){
189             float left  = sf*plc_state->hist[plc_state->bestlag+i];
190             float right = plc_state->hist[plc_state->bestlag+i];
191             val = left*rcos[i-SBC_FS]+right*rcos[SBC_OLAL-1-i+SBC_FS];
192             plc_state->hist[SBC_LHIST+i] = crop_sample(val);
193         }
194 
195         for (;i<SBC_FS+SBC_RT+SBC_OLAL;i++){
196             plc_state->hist[SBC_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
197         }
198     } else {
199         for (;i<SBC_FS+SBC_RT+SBC_OLAL;i++){
200             plc_state->hist[SBC_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
201         }
202     }
203     for (i=0;i<SBC_FS;i++){
204         out[i] = plc_state->hist[SBC_LHIST+i];
205     }
206 
207    // shift the history buffer
208     for (i=0;i<SBC_LHIST+SBC_RT+SBC_OLAL;i++){
209         plc_state->hist[i] = plc_state->hist[i+SBC_FS];
210     }
211 }
212 
213 void btstack_sbc_plc_good_frame(btstack_sbc_plc_state_t *plc_state, SAMPLE_FORMAT *in, SAMPLE_FORMAT *out){
214     float val;
215     int i = 0;
216     plc_state->good_frames_nr++;
217     plc_state->frame_count++;
218 
219     if (plc_state->nbf>0){
220         for (i=0;i<SBC_RT;i++){
221             out[i] = plc_state->hist[SBC_LHIST+i];
222         }
223 
224         for (i = SBC_RT;i<SBC_RT+SBC_OLAL;i++){
225             float left  = plc_state->hist[SBC_LHIST+i];
226             float right = in[i];
227             val = left*rcos[i-SBC_RT] + right*rcos[SBC_OLAL+SBC_RT-1-i];
228             out[i] = (SAMPLE_FORMAT)val;
229         }
230     }
231 
232     for (;i<SBC_FS;i++){
233         out[i] = in[i];
234     }
235     // Copy the output to the history buffer
236     for (i=0;i<SBC_FS;i++){
237         plc_state->hist[SBC_LHIST+i] = out[i];
238     }
239     // shift the history buffer
240     for (i=0;i<SBC_LHIST;i++){
241         plc_state->hist[i] = plc_state->hist[i+SBC_FS];
242     }
243 
244     plc_state->nbf=0;
245 }
246 
247 void btstack_sbc_dump_statistics(btstack_sbc_plc_state_t * state){
248     log_info("Good frames: %d\n", state->good_frames_nr);
249     log_info("Bad frames: %d\n", state->bad_frames_nr);
250     log_info("Max Consecutive bad frames: %d\n", state->max_consecutive_bad_frames_nr);
251 }
252