xref: /btstack/src/classic/btstack_sbc_plc.c (revision 6ccd8248590f666db07dd7add13fecb4f5664fb5)
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.0f) sf=1.0f;
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 #ifdef OCTAVE_OUTPUT
157 typedef enum {
158     OCTAVE_FRAME_TYPE_UNKNOWN = 0,
159     OCTAVE_FRAME_TYPE_GOOD,
160     OCTAVE_FRAME_TYPE_BAD
161 } octave_frame_type_t;
162 
163 static const char * octave_frame_type_name[] = {
164     "unknown",
165     "good",
166     "bad"
167 };
168 
169 static octave_frame_type_t octave_frame_type;
170 static char octave_base_name[1000];
171 
172 const char * octave_frame_type2str(int index){
173     if (index <= 0 || index >= sizeof(octave_frame_type_t)) return octave_frame_type_name[0];
174     return octave_frame_type_name[index];
175 }
176 
177 void btstack_sbc_plc_octave_set_base_name(const char * base_name){
178     strcpy(octave_base_name, base_name);
179     printf("OCTAVE: base name set to %s\n", octave_base_name);
180 }
181 
182 static void octave_fprintf_array_int16(FILE * oct_file, char * name, int data_len, int16_t * data){
183     fprintf(oct_file, "%s = [", name);
184     int i;
185     for (i = 0; i < data_len - 1; i++){
186         fprintf(oct_file, "%d, ", data[i]);
187     }
188     fprintf(oct_file, "%d", data[i]);
189     fprintf(oct_file, "%s", "];\n");
190 }
191 
192 static FILE * open_octave_file(btstack_sbc_plc_state_t *plc_state, octave_frame_type_t frame_type){
193     char oct_file_name[1200];
194     octave_frame_type = frame_type;
195     sprintf(oct_file_name, "%s_octave_plc_%d_%s.m", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type));
196 
197     FILE * oct_file = fopen(oct_file_name, "wb");
198     if (oct_file == NULL){
199         printf("OCTAVE: could not open file %s\n", oct_file_name);
200         return NULL;
201     }
202     printf("OCTAVE: opened file %s\n", oct_file_name);
203     return oct_file;
204 }
205 
206 static void octave_fprintf_plot_history_frame(btstack_sbc_plc_state_t *plc_state, FILE * oct_file, int frame_nr){
207     char title[100];
208     char hist_name[10];
209     sprintf(hist_name, "hist%d", plc_state->nbf);
210 
211     octave_fprintf_array_int16(oct_file, hist_name, SBC_LHIST, plc_state->hist);
212 
213     fprintf(oct_file, "y = [min(%s):1000:max(%s)];\n", hist_name, hist_name);
214     fprintf(oct_file, "x = zeros(1, size(y,2));\n");
215     fprintf(oct_file, "b = [0: %d];\n", SBC_LHIST+SBC_FS+SBC_RT+SBC_OLAL);
216 
217     int pos = SBC_FS;
218     fprintf(oct_file, "shift_x = x + %d;\n", pos);
219 
220     pos = SBC_LHIST - 1;
221     fprintf(oct_file, "lhist_x = x + %d;\n", pos);
222     pos += SBC_OLAL;
223     fprintf(oct_file, "lhist_olal1_x = x + %d;\n", pos);
224     pos += SBC_FS - SBC_OLAL;
225     fprintf(oct_file, "lhist_fs_x = x + %d;\n", pos);
226     pos += SBC_OLAL;
227     fprintf(oct_file, "lhist_olal2_x = x + %d;\n", pos);
228     pos += SBC_RT;
229     fprintf(oct_file, "lhist_rt_x = x + %d;\n", pos);
230 
231     fprintf(oct_file, "pattern_window_x = x + %d;\n", SBC_LHIST - SBC_M);
232 
233     fprintf(oct_file, "hf = figure();\n");
234     sprintf(title, "PLC %s frame %d", octave_frame_type2str(octave_frame_type), frame_nr);
235 
236     fprintf(oct_file, "hold on;\n");
237     fprintf(oct_file, "h1 = plot(%s); \n", hist_name);
238 
239     fprintf(oct_file, "title(\"%s\");\n", title);
240 
241     fprintf(oct_file, "plot(lhist_x, y, 'k'); \n");
242     fprintf(oct_file, "text(max(lhist_x) - 10, max(y)+1000, 'lhist'); \n");
243 
244     fprintf(oct_file, "plot(lhist_olal1_x, y, 'k'); \n");
245     fprintf(oct_file, "text(max(lhist_olal1_x) - 10, max(y)+1000, 'OLAL'); \n");
246 
247     fprintf(oct_file, "plot(lhist_fs_x, y, 'k'); \n");
248     fprintf(oct_file, "text(max(lhist_fs_x) - 10, max(y)+1000, 'FS'); \n");
249 
250     fprintf(oct_file, "plot(lhist_olal2_x, y, 'k'); \n");
251     fprintf(oct_file, "text(max(lhist_olal2_x) - 10, max(y)+1000, 'OLAL'); \n");
252 
253     fprintf(oct_file, "plot(lhist_rt_x, y, 'k');\n");
254     fprintf(oct_file, "text(max(lhist_rt_x) - 10, max(y)+1000, 'RT'); \n");
255 
256     if (octave_frame_type == OCTAVE_FRAME_TYPE_GOOD) return;
257 
258     int x0 = plc_state->bestlag;
259     int x1 = plc_state->bestlag + SBC_M - 1;
260     fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1);
261     fprintf(oct_file, "text(%d - 10, -10, 'bestlag'); \n", x0);
262 
263     x0 = plc_state->bestlag + SBC_M ;
264     x1 = plc_state->bestlag + SBC_M + SBC_FS - 1;
265     fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'kd'); \n", x0, x1, hist_name, x0, x1);
266 
267     x0 = SBC_LHIST - SBC_M;
268     x1 = SBC_LHIST - 1;
269     fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1);
270     fprintf(oct_file, "plot(pattern_window_x, y, 'g'); \n");
271     fprintf(oct_file, "text(max(pattern_window_x) - 10, max(y)+1000, 'M'); \n");
272 }
273 
274 static void octave_fprintf_plot_output(btstack_sbc_plc_state_t *plc_state, FILE * oct_file){
275     if (!oct_file) return;
276     char out_name[10];
277     sprintf(out_name, "out%d", plc_state->nbf);
278     int x0  = SBC_LHIST;
279     int x1  = x0 + SBC_FS - 1;
280     octave_fprintf_array_int16(oct_file, out_name, SBC_FS, plc_state->hist+x0);
281     fprintf(oct_file, "h2 = plot(b(%d:%d), %s, 'cd'); \n", x0, x1, out_name);
282 
283     char rest_hist_name[10];
284     sprintf(rest_hist_name, "rest%d", plc_state->nbf);
285     x0  = SBC_LHIST + SBC_FS;
286     x1  = x0 + SBC_OLAL + SBC_RT - 1;
287     octave_fprintf_array_int16(oct_file, rest_hist_name, SBC_OLAL + SBC_RT, plc_state->hist+x0);
288     fprintf(oct_file, "h3 = plot(b(%d:%d), %s, 'kd'); \n", x0, x1, rest_hist_name);
289 
290     char new_hist_name[10];
291     sprintf(new_hist_name, "hist%d", plc_state->nbf);
292     octave_fprintf_array_int16(oct_file, new_hist_name, SBC_LHIST, plc_state->hist);
293     fprintf(oct_file, "h4 = plot(%s, 'r--'); \n", new_hist_name);
294 
295     fprintf(oct_file, "legend ([h1, h2, h3, h4], {\"hist\", \"out\", \"rest\", \"new hist\"}, \"location\", \"northeast\");\n ");
296 
297     char fig_name[1200];
298     sprintf(fig_name, "../%s_octave_plc_%d_%s", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type));
299     fprintf(oct_file, "print(hf, \"%s.jpg\", \"-djpg\");", fig_name);
300 }
301 #endif
302 
303 
304 void btstack_sbc_plc_bad_frame(btstack_sbc_plc_state_t *plc_state, SAMPLE_FORMAT *ZIRbuf, SAMPLE_FORMAT *out){
305     float val;
306     int   i = 0;
307     float sf = 1;
308     plc_state->nbf++;
309 
310     plc_state->bad_frames_nr++;
311     plc_state->frame_count++;
312     if (plc_state->max_consecutive_bad_frames_nr < plc_state->nbf){
313         plc_state->max_consecutive_bad_frames_nr = plc_state->nbf;
314     }
315 
316     if (plc_state->nbf==1){
317         // printf("first bad frame\n");
318         // Perform pattern matching to find where to replicate
319         plc_state->bestlag = PatternMatch(plc_state->hist);
320     }
321 
322 #ifdef OCTAVE_OUTPUT
323     FILE * oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_BAD);
324     if (oct_file){
325         octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count);
326     }
327 #endif
328 
329     if (plc_state->nbf==1){
330         // the replication begins after the template match
331         plc_state->bestlag += SBC_M;
332 
333         // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet
334         sf = AmplitudeMatch(plc_state->hist, plc_state->bestlag);
335         // printf("sf Apmlitude Match %f, new data %d, bestlag+M %d\n", sf, ZIRbuf[0], plc_state->hist[plc_state->bestlag]);
336         for (i=0;i<SBC_OLAL;i++){
337             float left  = ZIRbuf[i];
338             float right = sf*plc_state->hist[plc_state->bestlag+i];
339             val = (left*rcos[i]) + (right*rcos[SBC_OLAL-1-i]);
340             // val = sf*plc_state->hist[plc_state->bestlag+i];
341             plc_state->hist[SBC_LHIST+i] = crop_sample(val);
342         }
343 
344         for (;i<SBC_FS;i++){
345             val = sf*plc_state->hist[plc_state->bestlag+i];
346             plc_state->hist[SBC_LHIST+i] = crop_sample(val);
347         }
348 
349         for (;i<(SBC_FS+SBC_OLAL);i++){
350             float left  = sf*plc_state->hist[plc_state->bestlag+i];
351             float right = plc_state->hist[plc_state->bestlag+i];
352             val = (left*rcos[i-SBC_FS])+(right*rcos[SBC_OLAL-1-i+SBC_FS]);
353             plc_state->hist[SBC_LHIST+i] = crop_sample(val);
354         }
355 
356         for (;i<(SBC_FS+SBC_RT+SBC_OLAL);i++){
357             plc_state->hist[SBC_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
358         }
359     } else {
360         // printf("succesive bad frame nr %d\n", plc_state->nbf);
361         for (;i<(SBC_FS+SBC_RT+SBC_OLAL);i++){
362             plc_state->hist[SBC_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
363         }
364     }
365     for (i=0;i<SBC_FS;i++){
366         out[i] = plc_state->hist[SBC_LHIST+i];
367     }
368 
369    // shift the history buffer
370     for (i=0;i<(SBC_LHIST+SBC_RT+SBC_OLAL);i++){
371         plc_state->hist[i] = plc_state->hist[i+SBC_FS];
372     }
373 
374 #ifdef OCTAVE_OUTPUT
375     if (oct_file){
376         octave_fprintf_plot_output(plc_state, oct_file);
377         fclose(oct_file);
378     }
379 #endif
380 }
381 
382 void btstack_sbc_plc_good_frame(btstack_sbc_plc_state_t *plc_state, SAMPLE_FORMAT *in, SAMPLE_FORMAT *out){
383     float val;
384     int i = 0;
385     plc_state->good_frames_nr++;
386     plc_state->frame_count++;
387 
388 #ifdef OCTAVE_OUTPUT
389     FILE * oct_file = NULL;
390     if (plc_state->nbf>0){
391         oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_GOOD);
392         if (oct_file){
393             octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count);
394         }
395     }
396 #endif
397 
398     if (plc_state->nbf>0){
399         for (i=0;i<SBC_RT;i++){
400             out[i] = plc_state->hist[SBC_LHIST+i];
401         }
402 
403         for (i = SBC_RT;i<(SBC_RT+SBC_OLAL);i++){
404             float left  = plc_state->hist[SBC_LHIST+i];
405             float right = in[i];
406             val = (left*rcos[i-SBC_RT]) + (right*rcos[SBC_OLAL+SBC_RT-1-i]);
407             out[i] = crop_sample(val);
408         }
409     }
410 
411     for (;i<SBC_FS;i++){
412         out[i] = in[i];
413     }
414     // Copy the output to the history buffer
415     for (i=0;i<SBC_FS;i++){
416         plc_state->hist[SBC_LHIST+i] = out[i];
417     }
418 
419     // shift the history buffer
420     for (i=0;i<SBC_LHIST;i++){
421         plc_state->hist[i] = plc_state->hist[i+SBC_FS];
422     }
423 
424 #ifdef OCTAVE_OUTPUT
425     if (oct_file){
426         octave_fprintf_plot_output(plc_state, oct_file);
427         fclose(oct_file);
428     }
429 #endif
430     plc_state->nbf=0;
431 }
432 
433 void btstack_sbc_dump_statistics(btstack_sbc_plc_state_t * state){
434     log_info("Good frames: %d\n", state->good_frames_nr);
435     log_info("Bad frames: %d\n", state->bad_frames_nr);
436     log_info("Max Consecutive bad frames: %d\n", state->max_consecutive_bad_frames_nr);
437 }
438