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