| 1 |
/* Copyright (C) 2002-2006 Jean-Marc Valin |
| 2 |
File: filters.c |
| 3 |
Various analysis/synthesis filters |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without |
| 6 |
modification, are permitted provided that the following conditions |
| 7 |
are met: |
| 8 |
|
| 9 |
- Redistributions of source code must retain the above copyright |
| 10 |
notice, this list of conditions and the following disclaimer. |
| 11 |
|
| 12 |
- Redistributions in binary form must reproduce the above copyright |
| 13 |
notice, this list of conditions and the following disclaimer in the |
| 14 |
documentation and/or other materials provided with the distribution. |
| 15 |
|
| 16 |
- Neither the name of the Xiph.org Foundation nor the names of its |
| 17 |
contributors may be used to endorse or promote products derived from |
| 18 |
this software without specific prior written permission. |
| 19 |
|
| 20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include "config.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "filters.h" |
| 38 |
#include "stack_alloc.h" |
| 39 |
#include "arch.h" |
| 40 |
#include "math_approx.h" |
| 41 |
#include "ltp.h" |
| 42 |
#include <math.h> |
| 43 |
|
| 44 |
#ifdef _USE_SSE |
| 45 |
#include "filters_sse.h" |
| 46 |
#elif defined (ARM4_ASM) || defined(ARM5E_ASM) |
| 47 |
#include "filters_arm4.h" |
| 48 |
#elif defined (BFIN_ASM) |
| 49 |
#include "filters_bfin.h" |
| 50 |
#endif |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void bw_lpc(spx_word16_t gamma, const spx_coef_t *lpc_in, spx_coef_t *lpc_out, int order) |
| 55 |
{ |
| 56 |
int i; |
| 57 |
spx_word16_t tmp=gamma; |
| 58 |
for (i=0;i<order;i++) |
| 59 |
{ |
| 60 |
lpc_out[i] = MULT16_16_P15(tmp,lpc_in[i]); |
| 61 |
tmp = MULT16_16_P15(tmp, gamma); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void sanitize_values32(spx_word32_t *vec, spx_word32_t min_val, spx_word32_t max_val, int len) |
| 66 |
{ |
| 67 |
int i; |
| 68 |
for (i=0;i<len;i++) |
| 69 |
{ |
| 70 |
/* It's important we do the test that way so we can catch NaNs, which are neither greater nor smaller */ |
| 71 |
if (!(vec[i]>=min_val && vec[i] <= max_val)) |
| 72 |
{ |
| 73 |
if (vec[i] < min_val) |
| 74 |
vec[i] = min_val; |
| 75 |
else if (vec[i] > max_val) |
| 76 |
vec[i] = max_val; |
| 77 |
else /* Has to be NaN */ |
| 78 |
vec[i] = 0; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void highpass(const spx_word16_t *x, spx_word16_t *y, int len, int filtID, spx_mem_t *mem) |
| 84 |
{ |
| 85 |
int i; |
| 86 |
#ifdef FIXED_POINT |
| 87 |
const spx_word16_t Pcoef[5][3] = {{16384, -31313, 14991}, {16384, -31569, 15249}, {16384, -31677, 15328}, {16384, -32313, 15947}, {16384, -22446, 6537}}; |
| 88 |
const spx_word16_t Zcoef[5][3] = {{15672, -31344, 15672}, {15802, -31601, 15802}, {15847, -31694, 15847}, {16162, -32322, 16162}, {14418, -28836, 14418}}; |
| 89 |
#else |
| 90 |
const spx_word16_t Pcoef[5][3] = {{1.00000f, -1.91120f, 0.91498f}, {1.00000f, -1.92683f, 0.93071f}, {1.00000f, -1.93338f, 0.93553f}, {1.00000f, -1.97226f, 0.97332f}, {1.00000f, -1.37000f, 0.39900f}}; |
| 91 |
const spx_word16_t Zcoef[5][3] = {{0.95654f, -1.91309f, 0.95654f}, {0.96446f, -1.92879f, 0.96446f}, {0.96723f, -1.93445f, 0.96723f}, {0.98645f, -1.97277f, 0.98645f}, {0.88000f, -1.76000f, 0.88000f}}; |
| 92 |
#endif |
| 93 |
const spx_word16_t *den, *num; |
| 94 |
if (filtID>4) |
| 95 |
filtID=4; |
| 96 |
|
| 97 |
den = Pcoef[filtID]; num = Zcoef[filtID]; |
| 98 |
/*return;*/ |
| 99 |
for (i=0;i<len;i++) |
| 100 |
{ |
| 101 |
spx_word16_t yi; |
| 102 |
spx_word32_t vout = ADD32(MULT16_16(num[0], x[i]),mem[0]); |
| 103 |
yi = EXTRACT16(SATURATE(PSHR32(vout,14),32767)); |
| 104 |
mem[0] = ADD32(MAC16_16(mem[1], num[1],x[i]), SHL32(MULT16_32_Q15(-den[1],vout),1)); |
| 105 |
mem[1] = ADD32(MULT16_16(num[2],x[i]), SHL32(MULT16_32_Q15(-den[2],vout),1)); |
| 106 |
y[i] = yi; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#ifdef FIXED_POINT |
| 111 |
|
| 112 |
/* FIXME: These functions are ugly and probably introduce too much error */ |
| 113 |
void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len) |
| 114 |
{ |
| 115 |
int i; |
| 116 |
for (i=0;i<len;i++) |
| 117 |
{ |
| 118 |
y[i] = SHL32(MULT16_32_Q14(EXTRACT16(SHR32(x[i],7)),scale),7); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#ifndef DISABLE_ENCODER |
| 123 |
void signal_div(const spx_word16_t *x, spx_word16_t *y, spx_word32_t scale, int len) |
| 124 |
{ |
| 125 |
int i; |
| 126 |
if (scale > SHL32(EXTEND32(SIG_SCALING), 8)) |
| 127 |
{ |
| 128 |
spx_word16_t scale_1; |
| 129 |
scale = PSHR32(scale, SIG_SHIFT); |
| 130 |
scale_1 = EXTRACT16(PDIV32_16(SHL32(EXTEND32(SIG_SCALING),7),scale)); |
| 131 |
for (i=0;i<len;i++) |
| 132 |
{ |
| 133 |
y[i] = MULT16_16_P15(scale_1, x[i]); |
| 134 |
} |
| 135 |
} else if (scale > SHR32(EXTEND32(SIG_SCALING), 2)) { |
| 136 |
spx_word16_t scale_1; |
| 137 |
scale = PSHR32(scale, SIG_SHIFT-5); |
| 138 |
scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale); |
| 139 |
for (i=0;i<len;i++) |
| 140 |
{ |
| 141 |
y[i] = PSHR32(MULT16_16(scale_1, SHL16(x[i],2)),8); |
| 142 |
} |
| 143 |
} else { |
| 144 |
spx_word16_t scale_1; |
| 145 |
scale = PSHR32(scale, SIG_SHIFT-7); |
| 146 |
if (scale < 5) |
| 147 |
scale = 5; |
| 148 |
scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale); |
| 149 |
for (i=0;i<len;i++) |
| 150 |
{ |
| 151 |
y[i] = PSHR32(MULT16_16(scale_1, SHL16(x[i],2)),6); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
#endif /* DISABLE_ENCODER */ |
| 156 |
|
| 157 |
#else /* FIXED_POINT */ |
| 158 |
|
| 159 |
void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len) |
| 160 |
{ |
| 161 |
int i; |
| 162 |
for (i=0;i<len;i++) |
| 163 |
y[i] = scale*x[i]; |
| 164 |
} |
| 165 |
|
| 166 |
#ifndef DISABLE_ENCODER |
| 167 |
void signal_div(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len) |
| 168 |
{ |
| 169 |
int i; |
| 170 |
float scale_1 = 1/scale; |
| 171 |
for (i=0;i<len;i++) |
| 172 |
y[i] = scale_1*x[i]; |
| 173 |
} |
| 174 |
#endif /* DISABLE_ENCODER */ |
| 175 |
|
| 176 |
#endif /* !FIXED_POINT */ |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#ifdef FIXED_POINT |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
#if !defined (DISABLE_WIDEBAND) && !defined (DISABLE_ENCODER) |
| 185 |
spx_word16_t compute_rms(const spx_sig_t *x, int len) |
| 186 |
{ |
| 187 |
int i; |
| 188 |
spx_word32_t sum=0; |
| 189 |
spx_sig_t max_val=1; |
| 190 |
int sig_shift; |
| 191 |
|
| 192 |
for (i=0;i<len;i++) |
| 193 |
{ |
| 194 |
spx_sig_t tmp = x[i]; |
| 195 |
if (tmp<0) |
| 196 |
tmp = -tmp; |
| 197 |
if (tmp > max_val) |
| 198 |
max_val = tmp; |
| 199 |
} |
| 200 |
|
| 201 |
sig_shift=0; |
| 202 |
while (max_val>16383) |
| 203 |
{ |
| 204 |
sig_shift++; |
| 205 |
max_val >>= 1; |
| 206 |
} |
| 207 |
|
| 208 |
for (i=0;i<len;i+=4) |
| 209 |
{ |
| 210 |
spx_word32_t sum2=0; |
| 211 |
spx_word16_t tmp; |
| 212 |
tmp = EXTRACT16(SHR32(x[i],sig_shift)); |
| 213 |
sum2 = MAC16_16(sum2,tmp,tmp); |
| 214 |
tmp = EXTRACT16(SHR32(x[i+1],sig_shift)); |
| 215 |
sum2 = MAC16_16(sum2,tmp,tmp); |
| 216 |
tmp = EXTRACT16(SHR32(x[i+2],sig_shift)); |
| 217 |
sum2 = MAC16_16(sum2,tmp,tmp); |
| 218 |
tmp = EXTRACT16(SHR32(x[i+3],sig_shift)); |
| 219 |
sum2 = MAC16_16(sum2,tmp,tmp); |
| 220 |
sum = ADD32(sum,SHR32(sum2,6)); |
| 221 |
} |
| 222 |
|
| 223 |
return EXTRACT16(PSHR32(SHL32(EXTEND32(spx_sqrt(DIV32(sum,len))),(sig_shift+3)),SIG_SHIFT)); |
| 224 |
} |
| 225 |
#endif /* !defined (DISABLE_WIDEBAND) && !defined (DISABLE_ENCODER) */ |
| 226 |
|
| 227 |
spx_word16_t compute_rms16(const spx_word16_t *x, int len) |
| 228 |
{ |
| 229 |
int i; |
| 230 |
spx_word16_t max_val=10; |
| 231 |
|
| 232 |
for (i=0;i<len;i++) |
| 233 |
{ |
| 234 |
spx_sig_t tmp = x[i]; |
| 235 |
if (tmp<0) |
| 236 |
tmp = -tmp; |
| 237 |
if (tmp > max_val) |
| 238 |
max_val = tmp; |
| 239 |
} |
| 240 |
if (max_val>16383) |
| 241 |
{ |
| 242 |
spx_word32_t sum=0; |
| 243 |
for (i=0;i<len;i+=4) |
| 244 |
{ |
| 245 |
spx_word32_t sum2=0; |
| 246 |
sum2 = MAC16_16(sum2,SHR16(x[i],1),SHR16(x[i],1)); |
| 247 |
sum2 = MAC16_16(sum2,SHR16(x[i+1],1),SHR16(x[i+1],1)); |
| 248 |
sum2 = MAC16_16(sum2,SHR16(x[i+2],1),SHR16(x[i+2],1)); |
| 249 |
sum2 = MAC16_16(sum2,SHR16(x[i+3],1),SHR16(x[i+3],1)); |
| 250 |
sum = ADD32(sum,SHR32(sum2,6)); |
| 251 |
} |
| 252 |
return SHL16(spx_sqrt(DIV32(sum,len)),4); |
| 253 |
} else { |
| 254 |
spx_word32_t sum=0; |
| 255 |
int sig_shift=0; |
| 256 |
if (max_val < 8192) |
| 257 |
sig_shift=1; |
| 258 |
if (max_val < 4096) |
| 259 |
sig_shift=2; |
| 260 |
if (max_val < 2048) |
| 261 |
sig_shift=3; |
| 262 |
for (i=0;i<len;i+=4) |
| 263 |
{ |
| 264 |
spx_word32_t sum2=0; |
| 265 |
sum2 = MAC16_16(sum2,SHL16(x[i],sig_shift),SHL16(x[i],sig_shift)); |
| 266 |
sum2 = MAC16_16(sum2,SHL16(x[i+1],sig_shift),SHL16(x[i+1],sig_shift)); |
| 267 |
sum2 = MAC16_16(sum2,SHL16(x[i+2],sig_shift),SHL16(x[i+2],sig_shift)); |
| 268 |
sum2 = MAC16_16(sum2,SHL16(x[i+3],sig_shift),SHL16(x[i+3],sig_shift)); |
| 269 |
sum = ADD32(sum,SHR32(sum2,6)); |
| 270 |
} |
| 271 |
return SHL16(spx_sqrt(DIV32(sum,len)),3-sig_shift); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#ifndef OVERRIDE_NORMALIZE16 |
| 276 |
int normalize16(const spx_sig_t *x, spx_word16_t *y, spx_sig_t max_scale, int len) |
| 277 |
{ |
| 278 |
int i; |
| 279 |
spx_sig_t max_val=1; |
| 280 |
int sig_shift; |
| 281 |
|
| 282 |
for (i=0;i<len;i++) |
| 283 |
{ |
| 284 |
spx_sig_t tmp = x[i]; |
| 285 |
if (tmp<0) |
| 286 |
tmp = NEG32(tmp); |
| 287 |
if (tmp >= max_val) |
| 288 |
max_val = tmp; |
| 289 |
} |
| 290 |
|
| 291 |
sig_shift=0; |
| 292 |
while (max_val>max_scale) |
| 293 |
{ |
| 294 |
sig_shift++; |
| 295 |
max_val >>= 1; |
| 296 |
} |
| 297 |
|
| 298 |
for (i=0;i<len;i++) |
| 299 |
y[i] = EXTRACT16(SHR32(x[i], sig_shift)); |
| 300 |
|
| 301 |
return sig_shift; |
| 302 |
} |
| 303 |
#endif |
| 304 |
|
| 305 |
#else |
| 306 |
|
| 307 |
spx_word16_t compute_rms(const spx_sig_t *x, int len) |
| 308 |
{ |
| 309 |
int i; |
| 310 |
float sum=0; |
| 311 |
for (i=0;i<len;i++) |
| 312 |
{ |
| 313 |
sum += x[i]*x[i]; |
| 314 |
} |
| 315 |
return sqrt(.1+sum/len); |
| 316 |
} |
| 317 |
spx_word16_t compute_rms16(const spx_word16_t *x, int len) |
| 318 |
{ |
| 319 |
return compute_rms(x, len); |
| 320 |
} |
| 321 |
#endif |
| 322 |
|
| 323 |
#ifdef MERGE_FILTERS |
| 324 |
const spx_word16_t zeros[10] = {0,0,0,0,0,0,0,0,0,0}; |
| 325 |
#endif /* MERGE_FILTERS */ |
| 326 |
|
| 327 |
#if !defined(OVERRIDE_FILTER_MEM16) && !defined(DISABLE_ENCODER) |
| 328 |
void filter_mem16(const spx_word16_t *x, const spx_coef_t *num, const spx_coef_t *den, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack) |
| 329 |
{ |
| 330 |
int i,j; |
| 331 |
spx_word16_t xi,yi,nyi; |
| 332 |
for (i=0;i<N;i++) |
| 333 |
{ |
| 334 |
xi= x[i]; |
| 335 |
yi = EXTRACT16(SATURATE(ADD32(EXTEND32(x[i]),PSHR32(mem[0],LPC_SHIFT)),32767)); |
| 336 |
nyi = NEG16(yi); |
| 337 |
for (j=0;j<ord-1;j++) |
| 338 |
{ |
| 339 |
mem[j] = MAC16_16(MAC16_16(mem[j+1], num[j],xi), den[j],nyi); |
| 340 |
} |
| 341 |
mem[ord-1] = ADD32(MULT16_16(num[ord-1],xi), MULT16_16(den[ord-1],nyi)); |
| 342 |
y[i] = yi; |
| 343 |
} |
| 344 |
} |
| 345 |
#endif /* !defined(OVERRIDE_FILTER_MEM16) && !defined(DISABLE_ENCODER) */ |
| 346 |
|
| 347 |
#ifndef OVERRIDE_IIR_MEM16 |
| 348 |
void iir_mem16(const spx_word16_t *x, const spx_coef_t *den, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack) |
| 349 |
{ |
| 350 |
int i,j; |
| 351 |
spx_word16_t yi,nyi; |
| 352 |
|
| 353 |
for (i=0;i<N;i++) |
| 354 |
{ |
| 355 |
yi = EXTRACT16(SATURATE(ADD32(EXTEND32(x[i]),PSHR32(mem[0],LPC_SHIFT)),32767)); |
| 356 |
nyi = NEG16(yi); |
| 357 |
for (j=0;j<ord-1;j++) |
| 358 |
{ |
| 359 |
mem[j] = MAC16_16(mem[j+1],den[j],nyi); |
| 360 |
} |
| 361 |
mem[ord-1] = MULT16_16(den[ord-1],nyi); |
| 362 |
y[i] = yi; |
| 363 |
} |
| 364 |
} |
| 365 |
#endif |
| 366 |
|
| 367 |
#if !defined(OVERRIDE_FIR_MEM16) && !defined(DISABLE_ENCODER) |
| 368 |
void fir_mem16(const spx_word16_t *x, const spx_coef_t *num, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack) |
| 369 |
{ |
| 370 |
int i,j; |
| 371 |
spx_word16_t xi,yi; |
| 372 |
|
| 373 |
for (i=0;i<N;i++) |
| 374 |
{ |
| 375 |
xi=x[i]; |
| 376 |
yi = EXTRACT16(SATURATE(ADD32(EXTEND32(x[i]),PSHR32(mem[0],LPC_SHIFT)),32767)); |
| 377 |
for (j=0;j<ord-1;j++) |
| 378 |
{ |
| 379 |
mem[j] = MAC16_16(mem[j+1], num[j],xi); |
| 380 |
} |
| 381 |
mem[ord-1] = MULT16_16(num[ord-1],xi); |
| 382 |
y[i] = yi; |
| 383 |
} |
| 384 |
} |
| 385 |
#endif /* !defined(OVERRIDE_FIR_MEM16) && !defined(DISABLE_ENCODER) */ |
| 386 |
|
| 387 |
#ifndef DISABLE_ENCODER |
| 388 |
void syn_percep_zero16(const spx_word16_t *xx, const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack) |
| 389 |
{ |
| 390 |
int i; |
| 391 |
VARDECL(spx_mem_t *mem); |
| 392 |
ALLOC(mem, ord, spx_mem_t); |
| 393 |
for (i=0;i<ord;i++) |
| 394 |
mem[i]=0; |
| 395 |
iir_mem16(xx, ak, y, N, ord, mem, stack); |
| 396 |
for (i=0;i<ord;i++) |
| 397 |
mem[i]=0; |
| 398 |
filter_mem16(y, awk1, awk2, y, N, ord, mem, stack); |
| 399 |
} |
| 400 |
void residue_percep_zero16(const spx_word16_t *xx, const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack) |
| 401 |
{ |
| 402 |
int i; |
| 403 |
VARDECL(spx_mem_t *mem); |
| 404 |
ALLOC(mem, ord, spx_mem_t); |
| 405 |
for (i=0;i<ord;i++) |
| 406 |
mem[i]=0; |
| 407 |
filter_mem16(xx, ak, awk1, y, N, ord, mem, stack); |
| 408 |
for (i=0;i<ord;i++) |
| 409 |
mem[i]=0; |
| 410 |
fir_mem16(y, awk2, y, N, ord, mem, stack); |
| 411 |
} |
| 412 |
#endif /* DISABLE_ENCODER */ |
| 413 |
|
| 414 |
#if !defined(OVERRIDE_COMPUTE_IMPULSE_RESPONSE) && !defined(DISABLE_ENCODER) |
| 415 |
void compute_impulse_response(const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack) |
| 416 |
{ |
| 417 |
int i,j; |
| 418 |
spx_word16_t y1, ny1i, ny2i; |
| 419 |
VARDECL(spx_mem_t *mem1); |
| 420 |
VARDECL(spx_mem_t *mem2); |
| 421 |
ALLOC(mem1, ord, spx_mem_t); |
| 422 |
ALLOC(mem2, ord, spx_mem_t); |
| 423 |
|
| 424 |
y[0] = LPC_SCALING; |
| 425 |
for (i=0;i<ord;i++) |
| 426 |
y[i+1] = awk1[i]; |
| 427 |
i++; |
| 428 |
for (;i<N;i++) |
| 429 |
y[i] = VERY_SMALL; |
| 430 |
for (i=0;i<ord;i++) |
| 431 |
mem1[i] = mem2[i] = 0; |
| 432 |
for (i=0;i<N;i++) |
| 433 |
{ |
| 434 |
y1 = ADD16(y[i], EXTRACT16(PSHR32(mem1[0],LPC_SHIFT))); |
| 435 |
ny1i = NEG16(y1); |
| 436 |
y[i] = PSHR32(ADD32(SHL32(EXTEND32(y1),LPC_SHIFT+1),mem2[0]),LPC_SHIFT); |
| 437 |
ny2i = NEG16(y[i]); |
| 438 |
for (j=0;j<ord-1;j++) |
| 439 |
{ |
| 440 |
mem1[j] = MAC16_16(mem1[j+1], awk2[j],ny1i); |
| 441 |
mem2[j] = MAC16_16(mem2[j+1], ak[j],ny2i); |
| 442 |
} |
| 443 |
mem1[ord-1] = MULT16_16(awk2[ord-1],ny1i); |
| 444 |
mem2[ord-1] = MULT16_16(ak[ord-1],ny2i); |
| 445 |
} |
| 446 |
} |
| 447 |
#endif /* !defined(OVERRIDE_COMPUTE_IMPULSE_RESPONSE) && !defined(DISABLE_ENCODER) */ |
| 448 |
|
| 449 |
#ifndef DISABLE_WIDEBAND |
| 450 |
/* Decomposes a signal into low-band and high-band using a QMF */ |
| 451 |
void qmf_decomp(const spx_word16_t *xx, const spx_word16_t *aa, spx_word16_t *y1, spx_word16_t *y2, int N, int M, spx_word16_t *mem, char *stack) |
| 452 |
{ |
| 453 |
int i,j,k,M2; |
| 454 |
VARDECL(spx_word16_t *a); |
| 455 |
VARDECL(spx_word16_t *x); |
| 456 |
spx_word16_t *x2; |
| 457 |
|
| 458 |
ALLOC(a, M, spx_word16_t); |
| 459 |
ALLOC(x, N+M-1, spx_word16_t); |
| 460 |
x2=x+M-1; |
| 461 |
M2=M>>1; |
| 462 |
for (i=0;i<M;i++) |
| 463 |
a[M-i-1]= aa[i]; |
| 464 |
for (i=0;i<M-1;i++) |
| 465 |
x[i]=mem[M-i-2]; |
| 466 |
for (i=0;i<N;i++) |
| 467 |
x[i+M-1]=SHR16(xx[i],1); |
| 468 |
for (i=0;i<M-1;i++) |
| 469 |
mem[i]=SHR16(xx[N-i-1],1); |
| 470 |
for (i=0,k=0;i<N;i+=2,k++) |
| 471 |
{ |
| 472 |
spx_word32_t y1k=0, y2k=0; |
| 473 |
for (j=0;j<M2;j++) |
| 474 |
{ |
| 475 |
y1k=ADD32(y1k,MULT16_16(a[j],ADD16(x[i+j],x2[i-j]))); |
| 476 |
y2k=SUB32(y2k,MULT16_16(a[j],SUB16(x[i+j],x2[i-j]))); |
| 477 |
j++; |
| 478 |
y1k=ADD32(y1k,MULT16_16(a[j],ADD16(x[i+j],x2[i-j]))); |
| 479 |
y2k=ADD32(y2k,MULT16_16(a[j],SUB16(x[i+j],x2[i-j]))); |
| 480 |
} |
| 481 |
y1[k] = EXTRACT16(SATURATE(PSHR32(y1k,15),32767)); |
| 482 |
y2[k] = EXTRACT16(SATURATE(PSHR32(y2k,15),32767)); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
/* Re-synthesised a signal from the QMF low-band and high-band signals */ |
| 487 |
void qmf_synth(const spx_word16_t *x1, const spx_word16_t *x2, const spx_word16_t *a, spx_word16_t *y, int N, int M, spx_word16_t *mem1, spx_word16_t *mem2, char *stack) |
| 488 |
/* assumptions: |
| 489 |
all odd x[i] are zero -- well, actually they are left out of the array now |
| 490 |
N and M are multiples of 4 */ |
| 491 |
{ |
| 492 |
int i, j; |
| 493 |
int M2, N2; |
| 494 |
VARDECL(spx_word16_t *xx1); |
| 495 |
VARDECL(spx_word16_t *xx2); |
| 496 |
|
| 497 |
M2 = M>>1; |
| 498 |
N2 = N>>1; |
| 499 |
ALLOC(xx1, M2+N2, spx_word16_t); |
| 500 |
ALLOC(xx2, M2+N2, spx_word16_t); |
| 501 |
|
| 502 |
for (i = 0; i < N2; i++) |
| 503 |
xx1[i] = x1[N2-1-i]; |
| 504 |
for (i = 0; i < M2; i++) |
| 505 |
xx1[N2+i] = mem1[2*i+1]; |
| 506 |
for (i = 0; i < N2; i++) |
| 507 |
xx2[i] = x2[N2-1-i]; |
| 508 |
for (i = 0; i < M2; i++) |
| 509 |
xx2[N2+i] = mem2[2*i+1]; |
| 510 |
|
| 511 |
for (i = 0; i < N2; i += 2) { |
| 512 |
spx_sig_t y0, y1, y2, y3; |
| 513 |
spx_word16_t x10, x20; |
| 514 |
|
| 515 |
y0 = y1 = y2 = y3 = 0; |
| 516 |
x10 = xx1[N2-2-i]; |
| 517 |
x20 = xx2[N2-2-i]; |
| 518 |
|
| 519 |
for (j = 0; j < M2; j += 2) { |
| 520 |
spx_word16_t x11, x21; |
| 521 |
spx_word16_t a0, a1; |
| 522 |
|
| 523 |
a0 = a[2*j]; |
| 524 |
a1 = a[2*j+1]; |
| 525 |
x11 = xx1[N2-1+j-i]; |
| 526 |
x21 = xx2[N2-1+j-i]; |
| 527 |
|
| 528 |
#ifdef FIXED_POINT |
| 529 |
/* We multiply twice by the same coef to avoid overflows */ |
| 530 |
y0 = MAC16_16(MAC16_16(y0, a0, x11), NEG16(a0), x21); |
| 531 |
y1 = MAC16_16(MAC16_16(y1, a1, x11), a1, x21); |
| 532 |
y2 = MAC16_16(MAC16_16(y2, a0, x10), NEG16(a0), x20); |
| 533 |
y3 = MAC16_16(MAC16_16(y3, a1, x10), a1, x20); |
| 534 |
#else |
| 535 |
y0 = ADD32(y0,MULT16_16(a0, x11-x21)); |
| 536 |
y1 = ADD32(y1,MULT16_16(a1, x11+x21)); |
| 537 |
y2 = ADD32(y2,MULT16_16(a0, x10-x20)); |
| 538 |
y3 = ADD32(y3,MULT16_16(a1, x10+x20)); |
| 539 |
#endif |
| 540 |
a0 = a[2*j+2]; |
| 541 |
a1 = a[2*j+3]; |
| 542 |
x10 = xx1[N2+j-i]; |
| 543 |
x20 = xx2[N2+j-i]; |
| 544 |
|
| 545 |
#ifdef FIXED_POINT |
| 546 |
/* We multiply twice by the same coef to avoid overflows */ |
| 547 |
y0 = MAC16_16(MAC16_16(y0, a0, x10), NEG16(a0), x20); |
| 548 |
y1 = MAC16_16(MAC16_16(y1, a1, x10), a1, x20); |
| 549 |
y2 = MAC16_16(MAC16_16(y2, a0, x11), NEG16(a0), x21); |
| 550 |
y3 = MAC16_16(MAC16_16(y3, a1, x11), a1, x21); |
| 551 |
#else |
| 552 |
y0 = ADD32(y0,MULT16_16(a0, x10-x20)); |
| 553 |
y1 = ADD32(y1,MULT16_16(a1, x10+x20)); |
| 554 |
y2 = ADD32(y2,MULT16_16(a0, x11-x21)); |
| 555 |
y3 = ADD32(y3,MULT16_16(a1, x11+x21)); |
| 556 |
#endif |
| 557 |
} |
| 558 |
#ifdef FIXED_POINT |
| 559 |
y[2*i] = EXTRACT16(SATURATE32(PSHR32(y0,15),32767)); |
| 560 |
y[2*i+1] = EXTRACT16(SATURATE32(PSHR32(y1,15),32767)); |
| 561 |
y[2*i+2] = EXTRACT16(SATURATE32(PSHR32(y2,15),32767)); |
| 562 |
y[2*i+3] = EXTRACT16(SATURATE32(PSHR32(y3,15),32767)); |
| 563 |
#else |
| 564 |
/* Normalize up explicitly if we're in float */ |
| 565 |
y[2*i] = 2.f*y0; |
| 566 |
y[2*i+1] = 2.f*y1; |
| 567 |
y[2*i+2] = 2.f*y2; |
| 568 |
y[2*i+3] = 2.f*y3; |
| 569 |
#endif |
| 570 |
} |
| 571 |
|
| 572 |
for (i = 0; i < M2; i++) |
| 573 |
mem1[2*i+1] = xx1[i]; |
| 574 |
for (i = 0; i < M2; i++) |
| 575 |
mem2[2*i+1] = xx2[i]; |
| 576 |
} |
| 577 |
#endif /* DISABLE_WIDEBAND */ |
| 578 |
|
| 579 |
|
| 580 |
#ifndef DISABLE_DECODER |
| 581 |
|
| 582 |
#ifdef FIXED_POINT |
| 583 |
#if 0 |
| 584 |
const spx_word16_t shift_filt[3][7] = {{-33, 1043, -4551, 19959, 19959, -4551, 1043}, |
| 585 |
{-98, 1133, -4425, 29179, 8895, -2328, 444}, |
| 586 |
{444, -2328, 8895, 29179, -4425, 1133, -98}}; |
| 587 |
#else |
| 588 |
const spx_word16_t shift_filt[3][7] = {{-390, 1540, -4993, 20123, 20123, -4993, 1540}, |
| 589 |
{-1064, 2817, -6694, 31589, 6837, -990, -209}, |
| 590 |
{-209, -990, 6837, 31589, -6694, 2817, -1064}}; |
| 591 |
#endif |
| 592 |
#else |
| 593 |
#if 0 |
| 594 |
const float shift_filt[3][7] = {{-9.9369e-04, 3.1831e-02, -1.3889e-01, 6.0910e-01, 6.0910e-01, -1.3889e-01, 3.1831e-02}, |
| 595 |
{-0.0029937, 0.0345613, -0.1350474, 0.8904793, 0.2714479, -0.0710304, 0.0135403}, |
| 596 |
{0.0135403, -0.0710304, 0.2714479, 0.8904793, -0.1350474, 0.0345613, -0.0029937}}; |
| 597 |
#else |
| 598 |
const float shift_filt[3][7] = {{-0.011915f, 0.046995f, -0.152373f, 0.614108f, 0.614108f, -0.152373f, 0.046995f}, |
| 599 |
{-0.0324855f, 0.0859768f, -0.2042986f, 0.9640297f, 0.2086420f, -0.0302054f, -0.0063646f}, |
| 600 |
{-0.0063646f, -0.0302054f, 0.2086420f, 0.9640297f, -0.2042986f, 0.0859768f, -0.0324855f}}; |
| 601 |
#endif |
| 602 |
#endif |
| 603 |
|
| 604 |
static int interp_pitch( |
| 605 |
spx_word16_t *exc, /*decoded excitation*/ |
| 606 |
spx_word16_t *interp, /*decoded excitation*/ |
| 607 |
int pitch, /*pitch period*/ |
| 608 |
int len |
| 609 |
) |
| 610 |
{ |
| 611 |
int i,j,k; |
| 612 |
spx_word32_t corr[4][7]; |
| 613 |
spx_word32_t maxcorr; |
| 614 |
int maxi, maxj; |
| 615 |
for (i=0;i<7;i++) |
| 616 |
{ |
| 617 |
corr[0][i] = inner_prod(exc, exc-pitch-3+i, len); |
| 618 |
} |
| 619 |
for (i=0;i<3;i++) |
| 620 |
{ |
| 621 |
for (j=0;j<7;j++) |
| 622 |
{ |
| 623 |
int i1, i2; |
| 624 |
spx_word32_t tmp=0; |
| 625 |
i1 = 3-j; |
| 626 |
if (i1<0) |
| 627 |
i1 = 0; |
| 628 |
i2 = 10-j; |
| 629 |
if (i2>7) |
| 630 |
i2 = 7; |
| 631 |
for (k=i1;k<i2;k++) |
| 632 |
tmp += MULT16_32_Q15(shift_filt[i][k],corr[0][j+k-3]); |
| 633 |
corr[i+1][j] = tmp; |
| 634 |
} |
| 635 |
} |
| 636 |
maxi=maxj=0; |
| 637 |
maxcorr = corr[0][0]; |
| 638 |
for (i=0;i<4;i++) |
| 639 |
{ |
| 640 |
for (j=0;j<7;j++) |
| 641 |
{ |
| 642 |
if (corr[i][j] > maxcorr) |
| 643 |
{ |
| 644 |
maxcorr = corr[i][j]; |
| 645 |
maxi=i; |
| 646 |
maxj=j; |
| 647 |
} |
| 648 |
} |
| 649 |
} |
| 650 |
for (i=0;i<len;i++) |
| 651 |
{ |
| 652 |
spx_word32_t tmp = 0; |
| 653 |
if (maxi>0) |
| 654 |
{ |
| 655 |
for (k=0;k<7;k++) |
| 656 |
{ |
| 657 |
tmp += MULT16_16(exc[i-(pitch-maxj+3)+k-3],shift_filt[maxi-1][k]); |
| 658 |
} |
| 659 |
} else { |
| 660 |
tmp = SHL32(exc[i-(pitch-maxj+3)],15); |
| 661 |
} |
| 662 |
interp[i] = PSHR32(tmp,15); |
| 663 |
} |
| 664 |
return pitch-maxj+3; |
| 665 |
} |
| 666 |
|
| 667 |
void multicomb( |
| 668 |
spx_word16_t *exc, /*decoded excitation*/ |
| 669 |
spx_word16_t *new_exc, /*enhanced excitation*/ |
| 670 |
spx_coef_t *ak, /*LPC filter coefs*/ |
| 671 |
int p, /*LPC order*/ |
| 672 |
int nsf, /*sub-frame size*/ |
| 673 |
int pitch, /*pitch period*/ |
| 674 |
int max_pitch, |
| 675 |
spx_word16_t comb_gain, /*gain of comb filter*/ |
| 676 |
char *stack |
| 677 |
) |
| 678 |
{ |
| 679 |
int i; |
| 680 |
VARDECL(spx_word16_t *iexc); |
| 681 |
spx_word16_t old_ener, new_ener; |
| 682 |
int corr_pitch; |
| 683 |
|
| 684 |
spx_word16_t iexc0_mag, iexc1_mag, exc_mag; |
| 685 |
spx_word32_t corr0, corr1; |
| 686 |
spx_word16_t gain0, gain1; |
| 687 |
spx_word16_t pgain1, pgain2; |
| 688 |
spx_word16_t c1, c2; |
| 689 |
spx_word16_t g1, g2; |
| 690 |
spx_word16_t ngain; |
| 691 |
spx_word16_t gg1, gg2; |
| 692 |
#ifdef FIXED_POINT |
| 693 |
int scaledown=0; |
| 694 |
#endif |
| 695 |
#if 0 /* Set to 1 to enable full pitch search */ |
| 696 |
int nol_pitch[6]; |
| 697 |
spx_word16_t nol_pitch_coef[6]; |
| 698 |
spx_word16_t ol_pitch_coef; |
| 699 |
open_loop_nbest_pitch(exc, 20, 120, nsf, |
| 700 |
nol_pitch, nol_pitch_coef, 6, stack); |
| 701 |
corr_pitch=nol_pitch[0]; |
| 702 |
ol_pitch_coef = nol_pitch_coef[0]; |
| 703 |
/*Try to remove pitch multiples*/ |
| 704 |
for (i=1;i<6;i++) |
| 705 |
{ |
| 706 |
#ifdef FIXED_POINT |
| 707 |
if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],19661)) && |
| 708 |
#else |
| 709 |
if ((nol_pitch_coef[i]>.6*nol_pitch_coef[0]) && |
| 710 |
#endif |
| 711 |
(ABS(2*nol_pitch[i]-corr_pitch)<=2 || ABS(3*nol_pitch[i]-corr_pitch)<=3 || |
| 712 |
ABS(4*nol_pitch[i]-corr_pitch)<=4 || ABS(5*nol_pitch[i]-corr_pitch)<=5)) |
| 713 |
{ |
| 714 |
corr_pitch = nol_pitch[i]; |
| 715 |
} |
| 716 |
} |
| 717 |
#else |
| 718 |
corr_pitch = pitch; |
| 719 |
#endif |
| 720 |
|
| 721 |
ALLOC(iexc, 2*nsf, spx_word16_t); |
| 722 |
|
| 723 |
interp_pitch(exc, iexc, corr_pitch, 80); |
| 724 |
if (corr_pitch>max_pitch) |
| 725 |
interp_pitch(exc, iexc+nsf, 2*corr_pitch, 80); |
| 726 |
else |
| 727 |
interp_pitch(exc, iexc+nsf, -corr_pitch, 80); |
| 728 |
|
| 729 |
#ifdef FIXED_POINT |
| 730 |
for (i=0;i<nsf;i++) |
| 731 |
{ |
| 732 |
if (ABS16(exc[i])>16383) |
| 733 |
{ |
| 734 |
scaledown = 1; |
| 735 |
break; |
| 736 |
} |
| 737 |
} |
| 738 |
if (scaledown) |
| 739 |
{ |
| 740 |
for (i=0;i<nsf;i++) |
| 741 |
exc[i] = SHR16(exc[i],1); |
| 742 |
for (i=0;i<2*nsf;i++) |
| 743 |
iexc[i] = SHR16(iexc[i],1); |
| 744 |
} |
| 745 |
#endif |
| 746 |
/*interp_pitch(exc, iexc+2*nsf, 2*corr_pitch, 80);*/ |
| 747 |
|
| 748 |
/*printf ("%d %d %f\n", pitch, corr_pitch, max_corr*ener_1);*/ |
| 749 |
iexc0_mag = spx_sqrt(1000+inner_prod(iexc,iexc,nsf)); |
| 750 |
iexc1_mag = spx_sqrt(1000+inner_prod(iexc+nsf,iexc+nsf,nsf)); |
| 751 |
exc_mag = spx_sqrt(1+inner_prod(exc,exc,nsf)); |
| 752 |
corr0 = inner_prod(iexc,exc,nsf); |
| 753 |
if (corr0<0) |
| 754 |
corr0=0; |
| 755 |
corr1 = inner_prod(iexc+nsf,exc,nsf); |
| 756 |
if (corr1<0) |
| 757 |
corr1=0; |
| 758 |
#ifdef FIXED_POINT |
| 759 |
/* Doesn't cost much to limit the ratio and it makes the rest easier */ |
| 760 |
if (SHL32(EXTEND32(iexc0_mag),6) < EXTEND32(exc_mag)) |
| 761 |
iexc0_mag = ADD16(1,PSHR16(exc_mag,6)); |
| 762 |
if (SHL32(EXTEND32(iexc1_mag),6) < EXTEND32(exc_mag)) |
| 763 |
iexc1_mag = ADD16(1,PSHR16(exc_mag,6)); |
| 764 |
#endif |
| 765 |
if (corr0 > MULT16_16(iexc0_mag,exc_mag)) |
| 766 |
pgain1 = QCONST16(1., 14); |
| 767 |
else |
| 768 |
pgain1 = PDIV32_16(SHL32(PDIV32(corr0, exc_mag),14),iexc0_mag); |
| 769 |
if (corr1 > MULT16_16(iexc1_mag,exc_mag)) |
| 770 |
pgain2 = QCONST16(1., 14); |
| 771 |
else |
| 772 |
pgain2 = PDIV32_16(SHL32(PDIV32(corr1, exc_mag),14),iexc1_mag); |
| 773 |
gg1 = PDIV32_16(SHL32(EXTEND32(exc_mag),8), iexc0_mag); |
| 774 |
gg2 = PDIV32_16(SHL32(EXTEND32(exc_mag),8), iexc1_mag); |
| 775 |
if (comb_gain>0) |
| 776 |
{ |
| 777 |
#ifdef FIXED_POINT |
| 778 |
c1 = (MULT16_16_Q15(QCONST16(.4,15),comb_gain)+QCONST16(.07,15)); |
| 779 |
c2 = QCONST16(.5,15)+MULT16_16_Q14(QCONST16(1.72,14),(c1-QCONST16(.07,15))); |
| 780 |
#else |
| 781 |
c1 = .4*comb_gain+.07; |
| 782 |
c2 = .5+1.72*(c1-.07); |
| 783 |
#endif |
| 784 |
} else |
| 785 |
{ |
| 786 |
c1=c2=0; |
| 787 |
} |
| 788 |
#ifdef FIXED_POINT |
| 789 |
g1 = 32767 - MULT16_16_Q13(MULT16_16_Q15(c2, pgain1),pgain1); |
| 790 |
g2 = 32767 - MULT16_16_Q13(MULT16_16_Q15(c2, pgain2),pgain2); |
| 791 |
#else |
| 792 |
g1 = 1-c2*pgain1*pgain1; |
| 793 |
g2 = 1-c2*pgain2*pgain2; |
| 794 |
#endif |
| 795 |
if (g1<c1) |
| 796 |
g1 = c1; |
| 797 |
if (g2<c1) |
| 798 |
g2 = c1; |
| 799 |
g1 = (spx_word16_t)PDIV32_16(SHL32(EXTEND32(c1),14),(spx_word16_t)g1); |
| 800 |
g2 = (spx_word16_t)PDIV32_16(SHL32(EXTEND32(c1),14),(spx_word16_t)g2); |
| 801 |
if (corr_pitch>max_pitch) |
| 802 |
{ |
| 803 |
gain0 = MULT16_16_Q15(QCONST16(.7,15),MULT16_16_Q14(g1,gg1)); |
| 804 |
gain1 = MULT16_16_Q15(QCONST16(.3,15),MULT16_16_Q14(g2,gg2)); |
| 805 |
} else { |
| 806 |
gain0 = MULT16_16_Q15(QCONST16(.6,15),MULT16_16_Q14(g1,gg1)); |
| 807 |
gain1 = MULT16_16_Q15(QCONST16(.6,15),MULT16_16_Q14(g2,gg2)); |
| 808 |
} |
| 809 |
for (i=0;i<nsf;i++) |
| 810 |
new_exc[i] = ADD16(exc[i], EXTRACT16(PSHR32(ADD32(MULT16_16(gain0,iexc[i]), MULT16_16(gain1,iexc[i+nsf])),8))); |
| 811 |
/* FIXME: compute_rms16 is currently not quite accurate enough (but close) */ |
| 812 |
new_ener = compute_rms16(new_exc, nsf); |
| 813 |
old_ener = compute_rms16(exc, nsf); |
| 814 |
|
| 815 |
if (old_ener < 1) |
| 816 |
old_ener = 1; |
| 817 |
if (new_ener < 1) |
| 818 |
new_ener = 1; |
| 819 |
if (old_ener > new_ener) |
| 820 |
old_ener = new_ener; |
| 821 |
ngain = PDIV32_16(SHL32(EXTEND32(old_ener),14),new_ener); |
| 822 |
|
| 823 |
for (i=0;i<nsf;i++) |
| 824 |
new_exc[i] = MULT16_16_Q14(ngain, new_exc[i]); |
| 825 |
#ifdef FIXED_POINT |
| 826 |
if (scaledown) |
| 827 |
{ |
| 828 |
for (i=0;i<nsf;i++) |
| 829 |
exc[i] = SHL16(exc[i],1); |
| 830 |
for (i=0;i<nsf;i++) |
| 831 |
new_exc[i] = SHL16(SATURATE16(new_exc[i],16383),1); |
| 832 |
} |
| 833 |
#endif |
| 834 |
} |
| 835 |
|
| 836 |
#endif /* DISABLE_DECODER */ |