| 1 |
/* Copyright (C) 2005 Analog Devices */ |
| 2 |
/** |
| 3 |
@file ltp_bfin.h |
| 4 |
@author Jean-Marc Valin |
| 5 |
@brief Long-Term Prediction functions (Blackfin version) |
| 6 |
*/ |
| 7 |
/* |
| 8 |
Redistribution and use in source and binary forms, with or without |
| 9 |
modification, are permitted provided that the following conditions |
| 10 |
are met: |
| 11 |
|
| 12 |
- Redistributions of source code must retain the above copyright |
| 13 |
notice, this list of conditions and the following disclaimer. |
| 14 |
|
| 15 |
- Redistributions in binary form must reproduce the above copyright |
| 16 |
notice, this list of conditions and the following disclaimer in the |
| 17 |
documentation and/or other materials provided with the distribution. |
| 18 |
|
| 19 |
- Neither the name of the Xiph.org Foundation nor the names of its |
| 20 |
contributors may be used to endorse or promote products derived from |
| 21 |
this software without specific prior written permission. |
| 22 |
|
| 23 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 26 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 27 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 28 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 29 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "bfin.h" |
| 37 |
|
| 38 |
#define OVERRIDE_INNER_PROD |
| 39 |
spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len) |
| 40 |
{ |
| 41 |
spx_word32_t sum=0; |
| 42 |
__asm__ __volatile__ ( |
| 43 |
"P0 = %3;\n\t" |
| 44 |
"P1 = %1;\n\t" |
| 45 |
"P2 = %2;\n\t" |
| 46 |
"I0 = P1;\n\t" |
| 47 |
"I1 = P2;\n\t" |
| 48 |
"L0 = 0;\n\t" |
| 49 |
"L1 = 0;\n\t" |
| 50 |
"A0 = 0;\n\t" |
| 51 |
"R0.L = W[I0++] || R1.L = W[I1++];\n\t" |
| 52 |
"LOOP inner%= LC0 = P0;\n\t" |
| 53 |
"LOOP_BEGIN inner%=;\n\t" |
| 54 |
"A0 += R0.L*R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t" |
| 55 |
"LOOP_END inner%=;\n\t" |
| 56 |
"A0 += R0.L*R1.L (IS);\n\t" |
| 57 |
"A0 = A0 >>> 6;\n\t" |
| 58 |
"R0 = A0;\n\t" |
| 59 |
"%0 = R0;\n\t" |
| 60 |
: "=m" (sum) |
| 61 |
: "m" (x), "m" (y), "d" (len-1) |
| 62 |
: "P0", "P1", "P2", "R0", "R1", "A0", "I0", "I1", "L0", "L1", "R3", "ASTAT" BFIN_HWLOOP0_REGS |
| 63 |
); |
| 64 |
return sum; |
| 65 |
} |
| 66 |
|
| 67 |
#define OVERRIDE_PITCH_XCORR |
| 68 |
void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack) |
| 69 |
{ |
| 70 |
corr += nb_pitch - 1; |
| 71 |
__asm__ __volatile__ ( |
| 72 |
"P2 = %0;\n\t" |
| 73 |
"I0 = P2;\n\t" /* x in I0 */ |
| 74 |
"B0 = P2;\n\t" /* x in B0 */ |
| 75 |
"R0 = %3;\n\t" /* len in R0 */ |
| 76 |
"P3 = %3;\n\t" |
| 77 |
"P3 += -2;\n\t" /* len in R0 */ |
| 78 |
"P4 = %4;\n\t" /* nb_pitch in R0 */ |
| 79 |
"R1 = R0 << 1;\n\t" /* number of bytes in x */ |
| 80 |
"L0 = R1;\n\t" |
| 81 |
"P0 = %1;\n\t" |
| 82 |
|
| 83 |
"P1 = %2;\n\t" |
| 84 |
"B1 = P1;\n\t" |
| 85 |
"L1 = 0;\n\t" /*Disable looping on I1*/ |
| 86 |
|
| 87 |
"r0 = [I0++];\n\t" |
| 88 |
"LOOP pitch%= LC0 = P4 >> 1;\n\t" |
| 89 |
"LOOP_BEGIN pitch%=;\n\t" |
| 90 |
"I1 = P0;\n\t" |
| 91 |
"A1 = A0 = 0;\n\t" |
| 92 |
"R1 = [I1++];\n\t" |
| 93 |
"LOOP inner_prod%= LC1 = P3 >> 1;\n\t" |
| 94 |
"LOOP_BEGIN inner_prod%=;\n\t" |
| 95 |
"A1 += R0.L*R1.H, A0 += R0.L*R1.L (IS) || R1.L = W[I1++];\n\t" |
| 96 |
"A1 += R0.H*R1.L, A0 += R0.H*R1.H (IS) || R1.H = W[I1++] || R0 = [I0++];\n\t" |
| 97 |
"LOOP_END inner_prod%=;\n\t" |
| 98 |
"A1 += R0.L*R1.H, A0 += R0.L*R1.L (IS) || R1.L = W[I1++];\n\t" |
| 99 |
"A1 += R0.H*R1.L, A0 += R0.H*R1.H (IS) || R0 = [I0++];\n\t" |
| 100 |
"A0 = A0 >>> 6;\n\t" |
| 101 |
"A1 = A1 >>> 6;\n\t" |
| 102 |
"R2 = A0, R3 = A1;\n\t" |
| 103 |
"[P1--] = r2;\n\t" |
| 104 |
"[P1--] = r3;\n\t" |
| 105 |
"P0 += 4;\n\t" |
| 106 |
"LOOP_END pitch%=;\n\t" |
| 107 |
"L0 = 0;\n\t" |
| 108 |
: : "m" (_x), "m" (_y), "m" (corr), "m" (len), "m" (nb_pitch) |
| 109 |
: "A0", "A1", "P0", "P1", "P2", "P3", "P4", "R0", "R1", "R2", "R3", "I0", "I1", "L0", "L1", "B0", "B1", "memory", |
| 110 |
"ASTAT" BFIN_HWLOOP0_REGS BFIN_HWLOOP1_REGS |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
#define OVERRIDE_COMPUTE_PITCH_ERROR |
| 115 |
static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control) |
| 116 |
{ |
| 117 |
spx_word32_t sum; |
| 118 |
__asm__ __volatile__ |
| 119 |
( |
| 120 |
"A0 = 0;\n\t" |
| 121 |
|
| 122 |
"R0 = W[%1++];\n\t" |
| 123 |
"R1.L = %2.L*%5.L (IS);\n\t" |
| 124 |
"A0 += R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 125 |
|
| 126 |
"R1.L = %3.L*%5.L (IS);\n\t" |
| 127 |
"A0 += R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 128 |
|
| 129 |
"R1.L = %4.L*%5.L (IS);\n\t" |
| 130 |
"A0 += R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 131 |
|
| 132 |
"R1.L = %2.L*%3.L (IS);\n\t" |
| 133 |
"A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 134 |
|
| 135 |
"R1.L = %4.L*%3.L (IS);\n\t" |
| 136 |
"A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 137 |
|
| 138 |
"R1.L = %4.L*%2.L (IS);\n\t" |
| 139 |
"A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 140 |
|
| 141 |
"R1.L = %2.L*%2.L (IS);\n\t" |
| 142 |
"A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 143 |
|
| 144 |
"R1.L = %3.L*%3.L (IS);\n\t" |
| 145 |
"A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t" |
| 146 |
|
| 147 |
"R1.L = %4.L*%4.L (IS);\n\t" |
| 148 |
"A0 -= R1.L*R0.L (IS);\n\t" |
| 149 |
|
| 150 |
"%0 = A0;\n\t" |
| 151 |
: "=&D" (sum), "=a" (C) |
| 152 |
: "d" (g[0]), "d" (g[1]), "d" (g[2]), "d" (pitch_control), "1" (C) |
| 153 |
: "R0", "R1", "R2", "A0", "ASTAT" |
| 154 |
); |
| 155 |
return sum; |
| 156 |
} |
| 157 |
|
| 158 |
#define OVERRIDE_OPEN_LOOP_NBEST_PITCH |
| 159 |
#ifdef OVERRIDE_OPEN_LOOP_NBEST_PITCH |
| 160 |
void open_loop_nbest_pitch(spx_word16_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack) |
| 161 |
{ |
| 162 |
int i,j,k; |
| 163 |
VARDECL(spx_word32_t *best_score); |
| 164 |
VARDECL(spx_word32_t *best_ener); |
| 165 |
spx_word32_t e0; |
| 166 |
VARDECL(spx_word32_t *corr); |
| 167 |
VARDECL(spx_word32_t *energy); |
| 168 |
|
| 169 |
ALLOC(best_score, N, spx_word32_t); |
| 170 |
ALLOC(best_ener, N, spx_word32_t); |
| 171 |
ALLOC(corr, end-start+1, spx_word32_t); |
| 172 |
ALLOC(energy, end-start+2, spx_word32_t); |
| 173 |
|
| 174 |
for (i=0;i<N;i++) |
| 175 |
{ |
| 176 |
best_score[i]=-1; |
| 177 |
best_ener[i]=0; |
| 178 |
pitch[i]=start; |
| 179 |
} |
| 180 |
|
| 181 |
energy[0]=inner_prod(sw-start, sw-start, len); |
| 182 |
e0=inner_prod(sw, sw, len); |
| 183 |
|
| 184 |
/* energy update -------------------------------------*/ |
| 185 |
|
| 186 |
__asm__ __volatile__ |
| 187 |
( |
| 188 |
" P0 = %0;\n\t" |
| 189 |
" I1 = %1;\n\t" |
| 190 |
" L1 = 0;\n\t" |
| 191 |
" I2 = %2;\n\t" |
| 192 |
" L2 = 0;\n\t" |
| 193 |
" R2 = [P0++];\n\t" |
| 194 |
" R3 = 0;\n\t" |
| 195 |
" LSETUP (eu1, eu2) LC1 = %3;\n\t" |
| 196 |
"eu1: R1.L = W [I1--] || R0.L = W [I2--] ;\n\t" |
| 197 |
" R1 = R1.L * R1.L (IS);\n\t" |
| 198 |
" R0 = R0.L * R0.L (IS);\n\t" |
| 199 |
" R1 >>>= 6;\n\t" |
| 200 |
" R1 = R1 + R2;\n\t" |
| 201 |
" R0 >>>= 6;\n\t" |
| 202 |
" R1 = R1 - R0;\n\t" |
| 203 |
" R2 = MAX(R1,R3);\n\t" |
| 204 |
"eu2: [P0++] = R2;\n\t" |
| 205 |
: : "d" (energy), "d" (&sw[-start-1]), "d" (&sw[-start+len-1]), |
| 206 |
"a" (end-start) |
| 207 |
: "P0", "I1", "I2", "R0", "R1", "R2", "R3", "ASTAT" BFIN_HWLOOP1_REGS |
| 208 |
); |
| 209 |
|
| 210 |
pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack); |
| 211 |
|
| 212 |
/* FIXME: Fixed-point and floating-point code should be merged */ |
| 213 |
{ |
| 214 |
VARDECL(spx_word16_t *corr16); |
| 215 |
VARDECL(spx_word16_t *ener16); |
| 216 |
ALLOC(corr16, end-start+1, spx_word16_t); |
| 217 |
ALLOC(ener16, end-start+1, spx_word16_t); |
| 218 |
/* Normalize to 180 so we can square it and it still fits in 16 bits */ |
| 219 |
normalize16(corr, corr16, 180, end-start+1); |
| 220 |
normalize16(energy, ener16, 180, end-start+1); |
| 221 |
|
| 222 |
if (N == 1) { |
| 223 |
/* optimised asm to handle N==1 case */ |
| 224 |
__asm__ __volatile__ |
| 225 |
( |
| 226 |
" I0 = %1;\n\t" /* I0: corr16[] */ |
| 227 |
" L0 = 0;\n\t" |
| 228 |
" I1 = %2;\n\t" /* I1: energy */ |
| 229 |
" L1 = 0;\n\t" |
| 230 |
" R2 = -1;\n\t" /* R2: best score */ |
| 231 |
" R3 = 0;\n\t" /* R3: best energy */ |
| 232 |
" P0 = %4;\n\t" /* P0: best pitch */ |
| 233 |
" P1 = %4;\n\t" /* P1: counter */ |
| 234 |
" LSETUP (sl1, sl2) LC1 = %3;\n\t" |
| 235 |
"sl1: R0.L = W [I0++] || R1.L = W [I1++];\n\t" |
| 236 |
" R0 = R0.L * R0.L (IS);\n\t" |
| 237 |
" R1 += 1;\n\t" |
| 238 |
" R4 = R0.L * R3.L;\n\t" |
| 239 |
" R5 = R2.L * R1.L;\n\t" |
| 240 |
" cc = R5 < R4;\n\t" |
| 241 |
" if cc R2 = R0;\n\t" |
| 242 |
" if cc R3 = R1;\n\t" |
| 243 |
" if cc P0 = P1;\n\t" |
| 244 |
"sl2: P1 += 1;\n\t" |
| 245 |
" %0 = P0;\n\t" |
| 246 |
: "=&d" (pitch[0]) |
| 247 |
: "a" (corr16), "a" (ener16), "a" (end+1-start), "d" (start) |
| 248 |
: "P0", "P1", "I0", "I1", "R0", "R1", "R2", "R3", "R4", "R5", |
| 249 |
"ASTAT", "CC" BFIN_HWLOOP1_REGS |
| 250 |
); |
| 251 |
|
| 252 |
} |
| 253 |
else { |
| 254 |
for (i=start;i<=end;i++) |
| 255 |
{ |
| 256 |
spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]); |
| 257 |
/* Instead of dividing the tmp by the energy, we multiply on the other side */ |
| 258 |
if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start]))) |
| 259 |
{ |
| 260 |
/* We can safely put it last and then check */ |
| 261 |
best_score[N-1]=tmp; |
| 262 |
best_ener[N-1]=ener16[i-start]+1; |
| 263 |
pitch[N-1]=i; |
| 264 |
/* Check if it comes in front of others */ |
| 265 |
for (j=0;j<N-1;j++) |
| 266 |
{ |
| 267 |
if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start]))) |
| 268 |
{ |
| 269 |
for (k=N-1;k>j;k--) |
| 270 |
{ |
| 271 |
best_score[k]=best_score[k-1]; |
| 272 |
best_ener[k]=best_ener[k-1]; |
| 273 |
pitch[k]=pitch[k-1]; |
| 274 |
} |
| 275 |
best_score[j]=tmp; |
| 276 |
best_ener[j]=ener16[i-start]+1; |
| 277 |
pitch[j]=i; |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/* Compute open-loop gain */ |
| 287 |
if (gain) |
| 288 |
{ |
| 289 |
for (j=0;j<N;j++) |
| 290 |
{ |
| 291 |
spx_word16_t g; |
| 292 |
i=pitch[j]; |
| 293 |
g = DIV32(corr[i-start], 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(energy[i-start])),6)); |
| 294 |
/* FIXME: g = max(g,corr/energy) */ |
| 295 |
if (g<0) |
| 296 |
g = 0; |
| 297 |
gain[j]=g; |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
#endif |
| 302 |
|
| 303 |
#define OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ |
| 304 |
#ifdef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ |
| 305 |
static int pitch_gain_search_3tap_vq( |
| 306 |
const signed char *gain_cdbk, |
| 307 |
int gain_cdbk_size, |
| 308 |
spx_word16_t *C16, |
| 309 |
spx_word16_t max_gain |
| 310 |
) |
| 311 |
{ |
| 312 |
const signed char *ptr=gain_cdbk; |
| 313 |
int best_cdbk=0; |
| 314 |
spx_word32_t best_sum=-VERY_LARGE32; |
| 315 |
spx_word32_t sum=0; |
| 316 |
spx_word16_t g[3]; |
| 317 |
spx_word16_t pitch_control=64; |
| 318 |
spx_word16_t gain_sum; |
| 319 |
int i; |
| 320 |
|
| 321 |
/* fast asm version of VQ codebook search */ |
| 322 |
|
| 323 |
__asm__ __volatile__ |
| 324 |
( |
| 325 |
|
| 326 |
" P0 = %2;\n\t" /* P0: ptr to gain_cdbk */ |
| 327 |
" L1 = 0;\n\t" /* no circ addr for L1 */ |
| 328 |
" %0 = 0;\n\t" /* %0: best_sum */ |
| 329 |
" %1 = 0;\n\t" /* %1: best_cbdk */ |
| 330 |
" P1 = 0;\n\t" /* P1: loop counter */ |
| 331 |
|
| 332 |
" LSETUP (pgs1, pgs2) LC1 = %4;\n\t" |
| 333 |
"pgs1: R2 = B [P0++] (X);\n\t" /* R2: g[0] */ |
| 334 |
" R3 = B [P0++] (X);\n\t" /* R3: g[1] */ |
| 335 |
" R4 = B [P0++] (X);\n\t" /* R4: g[2] */ |
| 336 |
" R2 += 32;\n\t" |
| 337 |
" R3 += 32;\n\t" |
| 338 |
" R4 += 32;\n\t" |
| 339 |
" R4.H = 64;\n\t" /* R4.H: pitch_control */ |
| 340 |
|
| 341 |
" R0 = B [P0++] (X);\n\t" |
| 342 |
" B0 = R0;\n\t" /* BO: gain_sum */ |
| 343 |
|
| 344 |
/* compute_pitch_error() -------------------------------*/ |
| 345 |
|
| 346 |
" I1 = %3;\n\t" /* I1: ptr to C */ |
| 347 |
" A0 = 0;\n\t" |
| 348 |
|
| 349 |
" R0.L = W[I1++];\n\t" |
| 350 |
" R1.L = R2.L*R4.H (IS);\n\t" |
| 351 |
" A0 += R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 352 |
|
| 353 |
" R1.L = R3.L*R4.H (IS);\n\t" |
| 354 |
" A0 += R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 355 |
|
| 356 |
" R1.L = R4.L*R4.H (IS);\n\t" |
| 357 |
" A0 += R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 358 |
|
| 359 |
" R1.L = R2.L*R3.L (IS);\n\t" |
| 360 |
" A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 361 |
|
| 362 |
" R1.L = R4.L*R3.L (IS);\n\t" |
| 363 |
" A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 364 |
|
| 365 |
" R1.L = R4.L*R2.L (IS);\n\t" |
| 366 |
" A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 367 |
|
| 368 |
" R1.L = R2.L*R2.L (IS);\n\t" |
| 369 |
" A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 370 |
|
| 371 |
" R1.L = R3.L*R3.L (IS);\n\t" |
| 372 |
" A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t" |
| 373 |
|
| 374 |
" R1.L = R4.L*R4.L (IS);\n\t" |
| 375 |
" R0 = (A0 -= R1.L*R0.L) (IS);\n\t" |
| 376 |
|
| 377 |
/* |
| 378 |
Re-arrange the if-then to code efficiently on the Blackfin: |
| 379 |
|
| 380 |
if (sum>best_sum && gain_sum<=max_gain) ------ (1) |
| 381 |
|
| 382 |
if (sum>best_sum && !(gain_sum>max_gain)) ------ (2) |
| 383 |
|
| 384 |
if (max_gain<=gain_sum) { ------ (3) |
| 385 |
sum = -VERY_LARGE32; |
| 386 |
} |
| 387 |
if (best_sum<=sum) |
| 388 |
|
| 389 |
The blackin cc instructions are all of the form: |
| 390 |
|
| 391 |
cc = x < y (or cc = x <= y) |
| 392 |
*/ |
| 393 |
" R1 = B0\n\t" |
| 394 |
" R2 = %5\n\t" |
| 395 |
" R3 = %6\n\t" |
| 396 |
" cc = R2 <= R1;\n\t" |
| 397 |
" if cc R0 = R3;\n\t" |
| 398 |
" cc = %0 <= R0;\n\t" |
| 399 |
" if cc %0 = R0;\n\t" |
| 400 |
" if cc %1 = P1;\n\t" |
| 401 |
|
| 402 |
"pgs2: P1 += 1;\n\t" |
| 403 |
|
| 404 |
: "=&d" (best_sum), "=&d" (best_cdbk) |
| 405 |
: "a" (gain_cdbk), "a" (C16), "a" (gain_cdbk_size), "a" (max_gain), |
| 406 |
"b" (-VERY_LARGE32) |
| 407 |
: "R0", "R1", "R2", "R3", "R4", "P0", |
| 408 |
"P1", "I1", "L1", "A0", "B0", "CC", "ASTAT" BFIN_HWLOOP1_REGS |
| 409 |
); |
| 410 |
|
| 411 |
return best_cdbk; |
| 412 |
} |
| 413 |
#endif |
| 414 |
|