xref: /btstack/src/classic/btstack_cvsd_plc.c (revision 40e1e61c3d08a44ca507a809db8cca4e716135d7)
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 
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 
51 #include "btstack_cvsd_plc.h"
52 #include "btstack_debug.h"
53 
54 static float rcos[CVSD_OLAL] = {
55     0.99148655,0.96623611,0.92510857,0.86950446,
56     0.80131732,0.72286918,0.63683150,0.54613418,
57     0.45386582,0.36316850,0.27713082,0.19868268,
58     0.13049554,0.07489143,0.03376389,0.00851345};
59 
60 // taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi
61 // Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation
62 static float sqrt3(const float x){
63     union {
64         int i;
65         float x;
66     } u;
67     u.x = x;
68     u.i = (1<<29) + (u.i >> 1) - (1<<22);
69 
70     // Two Babylonian Steps (simplified from:)
71     // u.x = 0.5f * (u.x + x/u.x);
72     // u.x = 0.5f * (u.x + x/u.x);
73     u.x =       u.x + x/u.x;
74     u.x = 0.25f*u.x + x/u.x;
75 
76     return u.x;
77 }
78 
79 static float absolute(float x){
80      if (x < 0) x = -x;
81      return x;
82 }
83 
84 static float CrossCorrelation(int8_t *x, int8_t *y){
85     float num = 0;
86     float den = 0;
87     float x2 = 0;
88     float y2 = 0;
89     int   m;
90     for (m=0;m<CVSD_M;m++){
91         num+=((float)x[m])*y[m];
92         x2+=((float)x[m])*x[m];
93         y2+=((float)y[m])*y[m];
94     }
95     den = (float)sqrt3(x2*y2);
96     return num/den;
97 }
98 
99 static int PatternMatch(int8_t *y){
100     float maxCn = -999999.0;  // large negative number
101     int   bestmatch = 0;
102     float Cn;
103     int   n;
104     for (n=0;n<CVSD_N;n++){
105         Cn = CrossCorrelation(&y[CVSD_LHIST-CVSD_M], &y[n]);
106         if (Cn>maxCn){
107             bestmatch=n;
108             maxCn = Cn;
109         }
110     }
111     return bestmatch;
112 }
113 
114 static float AmplitudeMatch(int8_t *y, int8_t bestmatch) {
115     int   i;
116     float sumx = 0;
117     float sumy = 0.000001f;
118     float sf;
119 
120     for (i=0;i<CVSD_FS;i++){
121         sumx += absolute(y[CVSD_LHIST-CVSD_FS+i]);
122         sumy += absolute(y[bestmatch+i]);
123     }
124     sf = sumx/sumy;
125     // This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts
126     if (sf<0.75f) sf=0.75f;
127     if (sf>1.2f) sf=1.2f;
128     return sf;
129 }
130 
131 static int8_t crop_to_int8(float val){
132     float croped_val = val;
133     if (croped_val > 127.0)  croped_val= 127.0;
134     if (croped_val < -128.0) croped_val=-128.0;
135     return (int8_t) croped_val;
136 }
137 
138 
139 void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state){
140     memset(plc_state, 0, sizeof(btstack_cvsd_plc_state_t));
141 }
142 
143 void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *out){
144     float val;
145     int   i = 0;
146     float sf = 1;
147     plc_state->nbf++;
148 
149     if (plc_state->nbf==1){
150         // Perform pattern matching to find where to replicate
151         plc_state->bestlag = PatternMatch(plc_state->hist);
152         // the replication begins after the template match
153         plc_state->bestlag += CVSD_M;
154 
155         // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet
156         sf = AmplitudeMatch(plc_state->hist, plc_state->bestlag);
157         for (i=0;i<CVSD_OLAL;i++){
158             val = sf*plc_state->hist[plc_state->bestlag+i];
159             plc_state->hist[CVSD_LHIST+i] = crop_to_int8(val);
160         }
161 
162         for (;i<CVSD_FS;i++){
163             val = sf*plc_state->hist[plc_state->bestlag+i];
164             plc_state->hist[CVSD_LHIST+i] = crop_to_int8(val);
165         }
166 
167         for (;i<CVSD_FS+CVSD_OLAL;i++){
168             float left  = sf*plc_state->hist[plc_state->bestlag+i];
169             float right = plc_state->hist[plc_state->bestlag+i];
170             val = left*rcos[i-CVSD_FS] + right*rcos[CVSD_OLAL-1-i+CVSD_FS];
171             plc_state->hist[CVSD_LHIST+i] = crop_to_int8(val);
172         }
173 
174         for (;i<CVSD_FS+CVSD_RT+CVSD_OLAL;i++){
175             plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
176         }
177     } else {
178         for (;i<CVSD_FS+CVSD_RT+CVSD_OLAL;i++){
179             plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
180         }
181     }
182     for (i=0;i<CVSD_FS;i++){
183         out[i] = plc_state->hist[CVSD_LHIST+i];
184     }
185 
186    // shift the history buffer
187    for (i=0;i<CVSD_LHIST+CVSD_RT+CVSD_OLAL;i++){
188         plc_state->hist[i] = plc_state->hist[i+CVSD_FS];
189     }
190 }
191 
192 void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *in, int8_t *out){
193     float val;
194     int i = 0;
195     if (plc_state->nbf>0){
196         for (i=0;i<CVSD_RT;i++){
197             out[i] = plc_state->hist[CVSD_LHIST+i];
198         }
199 
200         for (i=CVSD_RT;i<CVSD_RT+CVSD_OLAL;i++){
201             float left  = plc_state->hist[CVSD_LHIST+i];
202             float right = in[i];
203             val = left * rcos[i-CVSD_RT] + right *rcos[CVSD_OLAL+CVSD_RT-1-i];
204             out[i] = (int8_t)val;
205         }
206     }
207 
208     for (;i<CVSD_FS;i++){
209         out[i] = in[i];
210     }
211     // Copy the output to the history buffer
212     for (i=0;i<CVSD_FS;i++){
213         plc_state->hist[CVSD_LHIST+i] = out[i];
214     }
215     // shift the history buffer
216     for (i=0;i<CVSD_LHIST;i++){
217         plc_state->hist[i] = plc_state->hist[i+CVSD_FS];
218     }
219     plc_state->nbf=0;
220 }
221 
222 static int count_equal_bytes(int8_t * packet, uint16_t size){
223     int count = 0;
224     int temp_count = 1;
225     int i;
226     for (i = 0; i < size-1; i++){
227         if (packet[i] == packet[i+1]){
228             temp_count++;
229             continue;
230         }
231         if (count < temp_count){
232             count = temp_count;
233         }
234         temp_count = 1;
235     }
236     if (temp_count > count + 1){
237         count = temp_count;
238     }
239     return count;
240 }
241 
242 static int bad_frame(int8_t * frame, uint16_t size){
243     return count_equal_bytes(frame, size) > 20;
244 }
245 
246 void btstack_cvsd_plc_process_data(btstack_cvsd_plc_state_t * state, int8_t * in, uint16_t size, int8_t * out){
247     if (size != 24){
248         log_error("btstack_cvsd_plc_process_data: audio frame size is incorrect. Expected %d, got %d", CVSD_FS, size);
249     }
250     state->frame_count++;
251     if (bad_frame(in,size)){
252         memcpy(out, in, size);
253         if (state->good_frames_nr > CVSD_LHIST/CVSD_FS){
254             btstack_cvsd_plc_bad_frame(state, out);
255             state->bad_frames_nr++;
256         } else {
257             memset(out, 0, CVSD_FS);
258         }
259     } else {
260         btstack_cvsd_plc_good_frame(state, in, out);
261         state->good_frames_nr++;
262         if (state->good_frames_nr == 1){
263             printf("First good frame at index %d\n", state->frame_count-1);
264         }
265     }
266 }
267 
268 void btstack_cvsd_plc_mark_bad_frame(btstack_cvsd_plc_state_t * state, int8_t * in, uint16_t size, int8_t * out){
269     if (size != 24){
270         log_error("btstack_cvsd_plc_mark_bad_frame: audio frame size is incorrect. Expected %d, got %d", CVSD_FS, size);
271     }
272     state->frame_count++;
273 
274     if (bad_frame(in,size)){
275         memcpy(out, in, size);
276         if (state->good_frames_nr > CVSD_LHIST/CVSD_FS){
277             memset(out, 50, size);
278             state->bad_frames_nr++;
279         }
280     } else {
281         memcpy(out, in, size);
282         state->good_frames_nr++;
283         if (state->good_frames_nr == 1){
284             printf("First good frame at index %d\n", state->frame_count-1);
285         }
286     }
287 }
288 
289 void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state){
290     printf("Good frames: %d\n", state->good_frames_nr);
291     printf("Bad frames: %d\n", state->bad_frames_nr);
292 }
293