Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/gsm0610_long_term.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 15:50:58 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
|---|---|
| 1 /* | |
| 2 * SpanDSP - a series of DSP components for telephony | |
| 3 * | |
| 4 * gsm0610_long_term.c - GSM 06.10 full rate speech codec. | |
| 5 * | |
| 6 * Written by Steve Underwood <steveu@coppice.org> | |
| 7 * | |
| 8 * Copyright (C) 2006 Steve Underwood | |
| 9 * | |
| 10 * All rights reserved. | |
| 11 * | |
| 12 * This program is free software; you can redistribute it and/or modify | |
| 13 * it under the terms of the GNU Lesser General Public License version 2.1, | |
| 14 * as published by the Free Software Foundation. | |
| 15 * | |
| 16 * This program is distributed in the hope that it will be useful, | |
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 * GNU Lesser General Public License for more details. | |
| 20 * | |
| 21 * You should have received a copy of the GNU Lesser General Public | |
| 22 * License along with this program; if not, write to the Free Software | |
| 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 24 * | |
| 25 * This code is based on the widely used GSM 06.10 code available from | |
| 26 * http://kbs.cs.tu-berlin.de/~jutta/toast.html | |
| 27 * | |
| 28 * $Id: gsm0610_long_term.c,v 1.24 2009/04/20 16:36:36 steveu Exp $ | |
| 29 */ | |
| 30 | |
| 31 /*! \file */ | |
| 32 | |
| 33 #if defined(HAVE_CONFIG_H) | |
| 34 #include "config.h" | |
| 35 #endif | |
| 36 | |
| 37 #include <assert.h> | |
| 38 #include <inttypes.h> | |
| 39 #if defined(HAVE_TGMATH_H) | |
| 40 #include <tgmath.h> | |
| 41 #endif | |
| 42 #if defined(HAVE_MATH_H) | |
| 43 #include <math.h> | |
| 44 #endif | |
| 45 #include "floating_fudge.h" | |
| 46 #include <stdlib.h> | |
| 47 | |
| 48 #include "spandsp/telephony.h" | |
| 49 #include "spandsp/fast_convert.h" | |
| 50 #include "spandsp/bitstream.h" | |
| 51 #include "spandsp/saturated.h" | |
| 52 #include "spandsp/gsm0610.h" | |
| 53 | |
| 54 #include "gsm0610_local.h" | |
| 55 | |
| 56 /* Table 4.3a Decision level of the LTP gain quantizer */ | |
| 57 static const int16_t gsm_DLB[4] = | |
| 58 { | |
| 59 6554, 16384, 26214, 32767 | |
| 60 }; | |
| 61 | |
| 62 /* Table 4.3b Quantization levels of the LTP gain quantizer */ | |
| 63 static const int16_t gsm_QLB[4] = | |
| 64 { | |
| 65 3277, 11469, 21299, 32767 | |
| 66 }; | |
| 67 | |
| 68 /* 4.2.11 .. 4.2.12 LONG TERM PREDICTOR (LTP) SECTION */ | |
| 69 | |
| 70 static int32_t gsm0610_max_cross_corr(const int16_t *wt, const int16_t *dp, int16_t *index_out) | |
| 71 { | |
| 72 int32_t max; | |
| 73 int32_t index; | |
| 74 int32_t res; | |
| 75 int i; | |
| 76 | |
| 77 max = 0; | |
| 78 index = 40; /* index for the maximum cross-correlation */ | |
| 79 | |
| 80 for (i = 40; i <= 120; i++) | |
| 81 { | |
| 82 #if defined(__GNUC__) && defined(SPANDSP_USE_MMX) && defined(__x86_64__) | |
| 83 __asm__ __volatile__( | |
| 84 " emms;\n" | |
| 85 " .p2align 2;\n" | |
| 86 " movq (%%rdi),%%mm0;\n" | |
| 87 " movq (%%rsi),%%mm2;\n" | |
| 88 " pmaddwd %%mm2,%%mm0;\n" | |
| 89 " movq 8(%%rdi),%%mm1;\n" | |
| 90 " movq 8(%%rsi),%%mm2;\n" | |
| 91 " pmaddwd %%mm2,%%mm1;\n" | |
| 92 " paddd %%mm1,%%mm0;\n" | |
| 93 " movq 16(%%rdi),%%mm1;\n" | |
| 94 " movq 16(%%rsi),%%mm2;\n" | |
| 95 " pmaddwd %%mm2,%%mm1;\n" | |
| 96 " paddd %%mm1,%%mm0;\n" | |
| 97 " movq 24(%%rdi),%%mm1;\n" | |
| 98 " movq 24(%%rsi),%%mm2;\n" | |
| 99 " pmaddwd %%mm2,%%mm1;\n" | |
| 100 " paddd %%mm1,%%mm0;\n" | |
| 101 " movq 32(%%rdi),%%mm1;\n" | |
| 102 " movq 32(%%rsi),%%mm2;\n" | |
| 103 " pmaddwd %%mm2,%%mm1;\n" | |
| 104 " paddd %%mm1,%%mm0;\n" | |
| 105 " movq 40(%%rdi),%%mm1;\n" | |
| 106 " movq 40(%%rsi),%%mm2;\n" | |
| 107 " pmaddwd %%mm2,%%mm1;\n" | |
| 108 " paddd %%mm1,%%mm0;\n" | |
| 109 " movq 48(%%rdi),%%mm1;\n" | |
| 110 " movq 48(%%rsi),%%mm2;\n" | |
| 111 " pmaddwd %%mm2,%%mm1;\n" | |
| 112 " paddd %%mm1,%%mm0;\n" | |
| 113 " movq 56(%%rdi),%%mm1;\n" | |
| 114 " movq 56(%%rsi),%%mm2;\n" | |
| 115 " pmaddwd %%mm2,%%mm1;\n" | |
| 116 " paddd %%mm1,%%mm0;\n" | |
| 117 " movq 64(%%rdi),%%mm1;\n" | |
| 118 " movq 64(%%rsi),%%mm2;\n" | |
| 119 " pmaddwd %%mm2,%%mm1;\n" | |
| 120 " paddd %%mm1,%%mm0;\n" | |
| 121 " movq 72(%%rdi),%%mm1;\n" | |
| 122 " movq 72(%%rsi),%%mm2;\n" | |
| 123 " pmaddwd %%mm2,%%mm1;\n" | |
| 124 " paddd %%mm1,%%mm0;\n" | |
| 125 " movq %%mm0,%%mm1;\n" | |
| 126 " punpckhdq %%mm0,%%mm1;\n" /* mm1 has high int32 of mm0 dup'd */ | |
| 127 " paddd %%mm1,%%mm0;\n" | |
| 128 " movd %%mm0,%[res];\n" | |
| 129 " emms;\n" | |
| 130 : [res] "=r" (res) | |
| 131 : "D" (wt), "S" (&dp[-i]) | |
| 132 ); | |
| 133 #elif defined(__GNUC__) && defined(SPANDSP_USE_MMX) && defined(__i386__) | |
| 134 __asm__ __volatile__( | |
| 135 " emms;\n" | |
| 136 " .p2align 2;\n" | |
| 137 " movq (%%edi),%%mm0;\n" | |
| 138 " movq (%%esi),%%mm2;\n" | |
| 139 " pmaddwd %%mm2,%%mm0;\n" | |
| 140 " movq 8(%%edi),%%mm1;\n" | |
| 141 " movq 8(%%esi),%%mm2;\n" | |
| 142 " pmaddwd %%mm2,%%mm1;\n" | |
| 143 " paddd %%mm1,%%mm0;\n" | |
| 144 " movq 16(%%edi),%%mm1;\n" | |
| 145 " movq 16(%%esi),%%mm2;\n" | |
| 146 " pmaddwd %%mm2,%%mm1;\n" | |
| 147 " paddd %%mm1,%%mm0;\n" | |
| 148 " movq 24(%%edi),%%mm1;\n" | |
| 149 " movq 24(%%esi),%%mm2;\n" | |
| 150 " pmaddwd %%mm2,%%mm1;\n" | |
| 151 " paddd %%mm1,%%mm0;\n" | |
| 152 " movq 32(%%edi),%%mm1;\n" | |
| 153 " movq 32(%%esi),%%mm2;\n" | |
| 154 " pmaddwd %%mm2,%%mm1;\n" | |
| 155 " paddd %%mm1,%%mm0;\n" | |
| 156 " movq 40(%%edi),%%mm1;\n" | |
| 157 " movq 40(%%esi),%%mm2;\n" | |
| 158 " pmaddwd %%mm2,%%mm1;\n" | |
| 159 " paddd %%mm1,%%mm0;\n" | |
| 160 " movq 48(%%edi),%%mm1;\n" | |
| 161 " movq 48(%%esi),%%mm2;\n" | |
| 162 " pmaddwd %%mm2,%%mm1;\n" | |
| 163 " paddd %%mm1,%%mm0;\n" | |
| 164 " movq 56(%%edi),%%mm1;\n" | |
| 165 " movq 56(%%esi),%%mm2;\n" | |
| 166 " pmaddwd %%mm2,%%mm1;\n" | |
| 167 " paddd %%mm1,%%mm0;\n" | |
| 168 " movq 64(%%edi),%%mm1;\n" | |
| 169 " movq 64(%%esi),%%mm2;\n" | |
| 170 " pmaddwd %%mm2,%%mm1;\n" | |
| 171 " paddd %%mm1,%%mm0;\n" | |
| 172 " movq 72(%%edi),%%mm1;\n" | |
| 173 " movq 72(%%esi),%%mm2;\n" | |
| 174 " pmaddwd %%mm2,%%mm1;\n" | |
| 175 " paddd %%mm1,%%mm0;\n" | |
| 176 " movq %%mm0,%%mm1;\n" | |
| 177 " punpckhdq %%mm0,%%mm1;\n" /* mm1 has high int32 of mm0 dup'd */ | |
| 178 " paddd %%mm1,%%mm0;\n" | |
| 179 " movd %%mm0,%[res];\n" | |
| 180 " emms;\n" | |
| 181 : [res] "=r" (res) | |
| 182 : "D" (wt), "S" (&dp[-i]) | |
| 183 ); | |
| 184 #else | |
| 185 res = (wt[0]*dp[0 - i]) | |
| 186 + (wt[1]*dp[1 - i]) | |
| 187 + (wt[2]*dp[2 - i]) | |
| 188 + (wt[3]*dp[3 - i]) | |
| 189 + (wt[4]*dp[4 - i]) | |
| 190 + (wt[5]*dp[5 - i]) | |
| 191 + (wt[6]*dp[6 - i]) | |
| 192 + (wt[7]*dp[7 - i]) | |
| 193 + (wt[8]*dp[8 - i]) | |
| 194 + (wt[9]*dp[9 - i]) | |
| 195 + (wt[10]*dp[10 - i]) | |
| 196 + (wt[11]*dp[11 - i]) | |
| 197 + (wt[12]*dp[12 - i]) | |
| 198 + (wt[13]*dp[13 - i]) | |
| 199 + (wt[14]*dp[14 - i]) | |
| 200 + (wt[15]*dp[15 - i]) | |
| 201 + (wt[16]*dp[16 - i]) | |
| 202 + (wt[17]*dp[17 - i]) | |
| 203 + (wt[18]*dp[18 - i]) | |
| 204 + (wt[19]*dp[19 - i]) | |
| 205 + (wt[20]*dp[20 - i]) | |
| 206 + (wt[21]*dp[21 - i]) | |
| 207 + (wt[22]*dp[22 - i]) | |
| 208 + (wt[23]*dp[23 - i]) | |
| 209 + (wt[24]*dp[24 - i]) | |
| 210 + (wt[25]*dp[25 - i]) | |
| 211 + (wt[26]*dp[26 - i]) | |
| 212 + (wt[27]*dp[27 - i]) | |
| 213 + (wt[28]*dp[28 - i]) | |
| 214 + (wt[29]*dp[29 - i]) | |
| 215 + (wt[30]*dp[30 - i]) | |
| 216 + (wt[31]*dp[31 - i]) | |
| 217 + (wt[32]*dp[32 - i]) | |
| 218 + (wt[33]*dp[33 - i]) | |
| 219 + (wt[34]*dp[34 - i]) | |
| 220 + (wt[35]*dp[35 - i]) | |
| 221 + (wt[36]*dp[36 - i]) | |
| 222 + (wt[37]*dp[37 - i]) | |
| 223 + (wt[38]*dp[38 - i]) | |
| 224 + (wt[39]*dp[39 - i]); | |
| 225 #endif | |
| 226 if (res > max) | |
| 227 { | |
| 228 max = res; | |
| 229 index = i; | |
| 230 } | |
| 231 /*endif*/ | |
| 232 } | |
| 233 /*endfor*/ | |
| 234 *index_out = index; | |
| 235 return max; | |
| 236 } | |
| 237 /*- End of function --------------------------------------------------------*/ | |
| 238 | |
| 239 /* This procedure computes the LTP gain (bc) and the LTP lag (Nc) | |
| 240 for the long term analysis filter. This is done by calculating a | |
| 241 maximum of the cross-correlation function between the current | |
| 242 sub-segment short term residual signal d[0..39] (output of | |
| 243 the short term analysis filter; for simplification the index | |
| 244 of this array begins at 0 and ends at 39 for each sub-segment of the | |
| 245 RPE-LTP analysis) and the previous reconstructed short term | |
| 246 residual signal dp[ -120 .. -1 ]. A dynamic scaling must be | |
| 247 performed to avoid overflow. */ | |
| 248 | |
| 249 /* This procedure exists in three versions. First, the integer | |
| 250 version; then, the two floating point versions (as another | |
| 251 function), with or without scaling. */ | |
| 252 | |
| 253 static int16_t evaluate_ltp_parameters(int16_t d[40], | |
| 254 int16_t *dp, // [-120..-1] IN | |
| 255 int16_t *Nc_out) | |
| 256 { | |
| 257 int k; | |
| 258 int16_t bc; | |
| 259 int16_t wt[40]; | |
| 260 int32_t L_max; | |
| 261 int32_t L_power; | |
| 262 int16_t R; | |
| 263 int16_t S; | |
| 264 int16_t dmax; | |
| 265 int16_t scale; | |
| 266 int16_t temp; | |
| 267 int32_t L_temp; | |
| 268 | |
| 269 /* Search of the optimum scaling of d[0..39]. */ | |
| 270 dmax = 0; | |
| 271 for (k = 0; k < 40; k++) | |
| 272 { | |
| 273 temp = d[k]; | |
| 274 temp = saturated_abs16(temp); | |
| 275 if (temp > dmax) | |
| 276 dmax = temp; | |
| 277 /*endif*/ | |
| 278 } | |
| 279 /*endfor*/ | |
| 280 | |
| 281 if (dmax == 0) | |
| 282 { | |
| 283 temp = 0; | |
| 284 } | |
| 285 else | |
| 286 { | |
| 287 assert(dmax > 0); | |
| 288 temp = gsm0610_norm((int32_t) dmax << 16); | |
| 289 } | |
| 290 /*endif*/ | |
| 291 | |
| 292 if (temp > 6) | |
| 293 scale = 0; | |
| 294 else | |
| 295 scale = (int16_t) (6 - temp); | |
| 296 /*endif*/ | |
| 297 assert(scale >= 0); | |
| 298 | |
| 299 /* Initialization of a working array wt */ | |
| 300 for (k = 0; k < 40; k++) | |
| 301 wt[k] = d[k] >> scale; | |
| 302 /*endfor*/ | |
| 303 | |
| 304 /* Search for the maximum cross-correlation and coding of the LTP lag */ | |
| 305 L_max = gsm0610_max_cross_corr(wt, dp, Nc_out); | |
| 306 L_max <<= 1; | |
| 307 | |
| 308 /* Rescaling of L_max */ | |
| 309 assert(scale <= 100 && scale >= -100); | |
| 310 L_max = L_max >> (6 - scale); | |
| 311 | |
| 312 assert(*Nc_out <= 120 && *Nc_out >= 40); | |
| 313 | |
| 314 /* Compute the power of the reconstructed short term residual signal dp[..] */ | |
| 315 L_power = 0; | |
| 316 for (k = 0; k < 40; k++) | |
| 317 { | |
| 318 L_temp = dp[k - *Nc_out] >> 3; | |
| 319 L_power += L_temp*L_temp; | |
| 320 } | |
| 321 /*endfor*/ | |
| 322 L_power <<= 1; /* from L_MULT */ | |
| 323 | |
| 324 /* Normalization of L_max and L_power */ | |
| 325 if (L_max <= 0) | |
| 326 return 0; | |
| 327 /*endif*/ | |
| 328 if (L_max >= L_power) | |
| 329 return 3; | |
| 330 /*endif*/ | |
| 331 temp = gsm0610_norm(L_power); | |
| 332 | |
| 333 R = (int16_t) ((L_max << temp) >> 16); | |
| 334 S = (int16_t) ((L_power << temp) >> 16); | |
| 335 | |
| 336 /* Coding of the LTP gain */ | |
| 337 | |
| 338 /* Table 4.3a must be used to obtain the level DLB[i] for the | |
| 339 quantization of the LTP gain b to get the coded version bc. */ | |
| 340 for (bc = 0; bc <= 2; bc++) | |
| 341 { | |
| 342 if (R <= saturated_mul16(S, gsm_DLB[bc])) | |
| 343 break; | |
| 344 /*endif*/ | |
| 345 } | |
| 346 /*endfor*/ | |
| 347 return bc; | |
| 348 } | |
| 349 /*- End of function --------------------------------------------------------*/ | |
| 350 | |
| 351 /* 4.2.12 */ | |
| 352 static void long_term_analysis_filtering(int16_t bc, | |
| 353 int16_t Nc, | |
| 354 int16_t *dp, // previous d [-120..-1] IN | |
| 355 int16_t d[40], | |
| 356 int16_t dpp[40], | |
| 357 int16_t e[40]) | |
| 358 { | |
| 359 int k; | |
| 360 | |
| 361 /* In this part, we have to decode the bc parameter to compute | |
| 362 the samples of the estimate dpp[0..39]. The decoding of bc needs the | |
| 363 use of table 4.3b. The long term residual signal e[0..39] | |
| 364 is then calculated to be fed to the RPE encoding section. */ | |
| 365 for (k = 0; k < 40; k++) | |
| 366 { | |
| 367 dpp[k] = gsm_mult_r(gsm_QLB[bc], dp[k - Nc]); | |
| 368 e[k] = saturated_sub16(d[k], dpp[k]); | |
| 369 } | |
| 370 /*endfor*/ | |
| 371 } | |
| 372 /*- End of function --------------------------------------------------------*/ | |
| 373 | |
| 374 /* 4x for 160 samples */ | |
| 375 void gsm0610_long_term_predictor(gsm0610_state_t *s, | |
| 376 int16_t d[40], | |
| 377 int16_t *dp, // [-120..-1] d' IN | |
| 378 int16_t e[40], | |
| 379 int16_t dpp[40], | |
| 380 int16_t *Nc, | |
| 381 int16_t *bc) | |
| 382 { | |
| 383 #if 0 | |
| 384 assert(d); | |
| 385 assert(dp); | |
| 386 assert(e); | |
| 387 assert(dpp); | |
| 388 assert(Nc); | |
| 389 assert(bc); | |
| 390 #endif | |
| 391 | |
| 392 *bc = evaluate_ltp_parameters(d, dp, Nc); | |
| 393 long_term_analysis_filtering(*bc, *Nc, dp, d, dpp, e); | |
| 394 } | |
| 395 /*- End of function --------------------------------------------------------*/ | |
| 396 | |
| 397 /* 4.3.2 */ | |
| 398 void gsm0610_long_term_synthesis_filtering(gsm0610_state_t *s, | |
| 399 int16_t Ncr, | |
| 400 int16_t bcr, | |
| 401 int16_t erp[40], | |
| 402 int16_t *drp) // [-120..-1] IN, [0..40] OUT | |
| 403 { | |
| 404 int k; | |
| 405 int16_t brp; | |
| 406 int16_t drpp; | |
| 407 int16_t Nr; | |
| 408 | |
| 409 /* This procedure uses the bcr and Ncr parameters to realize the | |
| 410 long term synthesis filter. The decoding of bcr needs | |
| 411 table 4.3b. */ | |
| 412 | |
| 413 /* Check the limits of Nr. */ | |
| 414 Nr = (Ncr < 40 || Ncr > 120) ? s->nrp : Ncr; | |
| 415 s->nrp = Nr; | |
| 416 assert (Nr >= 40 && Nr <= 120); | |
| 417 | |
| 418 /* Decode the LTP gain, bcr */ | |
| 419 brp = gsm_QLB[bcr]; | |
| 420 | |
| 421 /* Compute the reconstructed short term residual signal, drp[0..39] */ | |
| 422 assert(brp != INT16_MIN); | |
| 423 for (k = 0; k < 40; k++) | |
| 424 { | |
| 425 drpp = gsm_mult_r(brp, drp[k - Nr]); | |
| 426 drp[k] = saturated_add16(erp[k], drpp); | |
| 427 } | |
| 428 /*endfor*/ | |
| 429 | |
| 430 /* Update the reconstructed short term residual signal, drp[-1..-120] */ | |
| 431 for (k = 0; k < 120; k++) | |
| 432 drp[k - 120] = drp[k - 80]; | |
| 433 /*endfor*/ | |
| 434 } | |
| 435 /*- End of function --------------------------------------------------------*/ | |
| 436 /*- End of file ------------------------------------------------------------*/ |
