1 /* 2 * SpanDSP - a series of DSP components for telephony 3 * 4 * g722_decode.c - The ITU G.722 codec, decode part. 5 * 6 * Written by Steve Underwood <steveu (at) coppice.org> 7 * 8 * Copyright (C) 2005 Steve Underwood 9 * 10 * Despite my general liking of the GPL, I place my own contributions 11 * to this code in the public domain for the benefit of all mankind - 12 * even the slimy ones who might try to proprietize my work and use it 13 * to my detriment. 14 * 15 * Based in part on a single channel G.722 codec which is: 16 * 17 * Copyright (c) CMU 1993 18 * Computer Science, Speech Group 19 * Chengxiang Lu and Alex Hauptmann 20 * 21 * $Id: g722_decode.c,v 1.15 2006/07/07 16:37:49 steveu Exp $ 22 * 23 * Modifications for WebRtc, 2011/04/28, by tlegrand: 24 * -Removed usage of inttypes.h and tgmath.h 25 * -Changed to use WebRtc types 26 * -Changed __inline__ to __inline 27 * -Added saturation check on output 28 */ 29 30 /*! \file */ 31 32 33 #ifdef HAVE_CONFIG_H 34 #include <config.h> 35 #endif 36 37 #include <memory.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 41 #include "g722_enc_dec.h" 42 #include "webrtc/typedefs.h" 43 44 #if !defined(FALSE) 45 #define FALSE 0 46 #endif 47 #if !defined(TRUE) 48 #define TRUE (!FALSE) 49 #endif 50 51 static __inline int16_t saturate(int32_t amp) 52 { 53 int16_t amp16; 54 55 /* Hopefully this is optimised for the common case - not clipping */ 56 amp16 = (int16_t) amp; 57 if (amp == amp16) 58 return amp16; 59 if (amp > WEBRTC_INT16_MAX) 60 return WEBRTC_INT16_MAX; 61 return WEBRTC_INT16_MIN; 62 } 63 /*- End of function --------------------------------------------------------*/ 64 65 static void block4(G722DecoderState *s, int band, int d); 66 67 static void block4(G722DecoderState *s, int band, int d) 68 { 69 int wd1; 70 int wd2; 71 int wd3; 72 int i; 73 74 /* Block 4, RECONS */ 75 s->band[band].d[0] = d; 76 s->band[band].r[0] = saturate(s->band[band].s + d); 77 78 /* Block 4, PARREC */ 79 s->band[band].p[0] = saturate(s->band[band].sz + d); 80 81 /* Block 4, UPPOL2 */ 82 for (i = 0; i < 3; i++) 83 s->band[band].sg[i] = s->band[band].p[i] >> 15; 84 wd1 = saturate(s->band[band].a[1] << 2); 85 86 wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1; 87 if (wd2 > 32767) 88 wd2 = 32767; 89 wd3 = (s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128; 90 wd3 += (wd2 >> 7); 91 wd3 += (s->band[band].a[2]*32512) >> 15; 92 if (wd3 > 12288) 93 wd3 = 12288; 94 else if (wd3 < -12288) 95 wd3 = -12288; 96 s->band[band].ap[2] = wd3; 97 98 /* Block 4, UPPOL1 */ 99 s->band[band].sg[0] = s->band[band].p[0] >> 15; 100 s->band[band].sg[1] = s->band[band].p[1] >> 15; 101 wd1 = (s->band[band].sg[0] == s->band[band].sg[1]) ? 192 : -192; 102 wd2 = (s->band[band].a[1]*32640) >> 15; 103 104 s->band[band].ap[1] = saturate(wd1 + wd2); 105 wd3 = saturate(15360 - s->band[band].ap[2]); 106 if (s->band[band].ap[1] > wd3) 107 s->band[band].ap[1] = wd3; 108 else if (s->band[band].ap[1] < -wd3) 109 s->band[band].ap[1] = -wd3; 110 111 /* Block 4, UPZERO */ 112 wd1 = (d == 0) ? 0 : 128; 113 s->band[band].sg[0] = d >> 15; 114 for (i = 1; i < 7; i++) 115 { 116 s->band[band].sg[i] = s->band[band].d[i] >> 15; 117 wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1; 118 wd3 = (s->band[band].b[i]*32640) >> 15; 119 s->band[band].bp[i] = saturate(wd2 + wd3); 120 } 121 122 /* Block 4, DELAYA */ 123 for (i = 6; i > 0; i--) 124 { 125 s->band[band].d[i] = s->band[band].d[i - 1]; 126 s->band[band].b[i] = s->band[band].bp[i]; 127 } 128 129 for (i = 2; i > 0; i--) 130 { 131 s->band[band].r[i] = s->band[band].r[i - 1]; 132 s->band[band].p[i] = s->band[band].p[i - 1]; 133 s->band[band].a[i] = s->band[band].ap[i]; 134 } 135 136 /* Block 4, FILTEP */ 137 wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]); 138 wd1 = (s->band[band].a[1]*wd1) >> 15; 139 wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]); 140 wd2 = (s->band[band].a[2]*wd2) >> 15; 141 s->band[band].sp = saturate(wd1 + wd2); 142 143 /* Block 4, FILTEZ */ 144 s->band[band].sz = 0; 145 for (i = 6; i > 0; i--) 146 { 147 wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]); 148 s->band[band].sz += (s->band[band].b[i]*wd1) >> 15; 149 } 150 s->band[band].sz = saturate(s->band[band].sz); 151 152 /* Block 4, PREDIC */ 153 s->band[band].s = saturate(s->band[band].sp + s->band[band].sz); 154 } 155 /*- End of function --------------------------------------------------------*/ 156 157 G722DecoderState* WebRtc_g722_decode_init(G722DecoderState* s, 158 int rate, 159 int options) { 160 s = s ? s : malloc(sizeof(*s)); 161 memset(s, 0, sizeof(*s)); 162 if (rate == 48000) 163 s->bits_per_sample = 6; 164 else if (rate == 56000) 165 s->bits_per_sample = 7; 166 else 167 s->bits_per_sample = 8; 168 if ((options & G722_SAMPLE_RATE_8000)) 169 s->eight_k = TRUE; 170 if ((options & G722_PACKED) && s->bits_per_sample != 8) 171 s->packed = TRUE; 172 else 173 s->packed = FALSE; 174 s->band[0].det = 32; 175 s->band[1].det = 8; 176 return s; 177 } 178 /*- End of function --------------------------------------------------------*/ 179 180 int WebRtc_g722_decode_release(G722DecoderState *s) 181 { 182 free(s); 183 return 0; 184 } 185 /*- End of function --------------------------------------------------------*/ 186 187 size_t WebRtc_g722_decode(G722DecoderState *s, int16_t amp[], 188 const uint8_t g722_data[], size_t len) 189 { 190 static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 }; 191 static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 192 7, 6, 5, 4, 3, 2, 1, 0 }; 193 static const int ilb[32] = 194 { 195 2048, 2093, 2139, 2186, 2233, 2282, 2332, 196 2383, 2435, 2489, 2543, 2599, 2656, 2714, 197 2774, 2834, 2896, 2960, 3025, 3091, 3158, 198 3228, 3298, 3371, 3444, 3520, 3597, 3676, 199 3756, 3838, 3922, 4008 200 }; 201 static const int wh[3] = {0, -214, 798}; 202 static const int rh2[4] = {2, 1, 2, 1}; 203 static const int qm2[4] = {-7408, -1616, 7408, 1616}; 204 static const int qm4[16] = 205 { 206 0, -20456, -12896, -8968, 207 -6288, -4240, -2584, -1200, 208 20456, 12896, 8968, 6288, 209 4240, 2584, 1200, 0 210 }; 211 static const int qm5[32] = 212 { 213 -280, -280, -23352, -17560, 214 -14120, -11664, -9752, -8184, 215 -6864, -5712, -4696, -3784, 216 -2960, -2208, -1520, -880, 217 23352, 17560, 14120, 11664, 218 9752, 8184, 6864, 5712, 219 4696, 3784, 2960, 2208, 220 1520, 880, 280, -280 221 }; 222 static const int qm6[64] = 223 { 224 -136, -136, -136, -136, 225 -24808, -21904, -19008, -16704, 226 -14984, -13512, -12280, -11192, 227 -10232, -9360, -8576, -7856, 228 -7192, -6576, -6000, -5456, 229 -4944, -4464, -4008, -3576, 230 -3168, -2776, -2400, -2032, 231 -1688, -1360, -1040, -728, 232 24808, 21904, 19008, 16704, 233 14984, 13512, 12280, 11192, 234 10232, 9360, 8576, 7856, 235 7192, 6576, 6000, 5456, 236 4944, 4464, 4008, 3576, 237 3168, 2776, 2400, 2032, 238 1688, 1360, 1040, 728, 239 432, 136, -432, -136 240 }; 241 static const int qmf_coeffs[12] = 242 { 243 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11, 244 }; 245 246 int dlowt; 247 int rlow; 248 int ihigh; 249 int dhigh; 250 int rhigh; 251 int xout1; 252 int xout2; 253 int wd1; 254 int wd2; 255 int wd3; 256 int code; 257 size_t outlen; 258 int i; 259 size_t j; 260 261 outlen = 0; 262 rhigh = 0; 263 for (j = 0; j < len; ) 264 { 265 if (s->packed) 266 { 267 /* Unpack the code bits */ 268 if (s->in_bits < s->bits_per_sample) 269 { 270 s->in_buffer |= (g722_data[j++] << s->in_bits); 271 s->in_bits += 8; 272 } 273 code = s->in_buffer & ((1 << s->bits_per_sample) - 1); 274 s->in_buffer >>= s->bits_per_sample; 275 s->in_bits -= s->bits_per_sample; 276 } 277 else 278 { 279 code = g722_data[j++]; 280 } 281 282 switch (s->bits_per_sample) 283 { 284 default: 285 case 8: 286 wd1 = code & 0x3F; 287 ihigh = (code >> 6) & 0x03; 288 wd2 = qm6[wd1]; 289 wd1 >>= 2; 290 break; 291 case 7: 292 wd1 = code & 0x1F; 293 ihigh = (code >> 5) & 0x03; 294 wd2 = qm5[wd1]; 295 wd1 >>= 1; 296 break; 297 case 6: 298 wd1 = code & 0x0F; 299 ihigh = (code >> 4) & 0x03; 300 wd2 = qm4[wd1]; 301 break; 302 } 303 /* Block 5L, LOW BAND INVQBL */ 304 wd2 = (s->band[0].det*wd2) >> 15; 305 /* Block 5L, RECONS */ 306 rlow = s->band[0].s + wd2; 307 /* Block 6L, LIMIT */ 308 if (rlow > 16383) 309 rlow = 16383; 310 else if (rlow < -16384) 311 rlow = -16384; 312 313 /* Block 2L, INVQAL */ 314 wd2 = qm4[wd1]; 315 dlowt = (s->band[0].det*wd2) >> 15; 316 317 /* Block 3L, LOGSCL */ 318 wd2 = rl42[wd1]; 319 wd1 = (s->band[0].nb*127) >> 7; 320 wd1 += wl[wd2]; 321 if (wd1 < 0) 322 wd1 = 0; 323 else if (wd1 > 18432) 324 wd1 = 18432; 325 s->band[0].nb = wd1; 326 327 /* Block 3L, SCALEL */ 328 wd1 = (s->band[0].nb >> 6) & 31; 329 wd2 = 8 - (s->band[0].nb >> 11); 330 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); 331 s->band[0].det = wd3 << 2; 332 333 block4(s, 0, dlowt); 334 335 if (!s->eight_k) 336 { 337 /* Block 2H, INVQAH */ 338 wd2 = qm2[ihigh]; 339 dhigh = (s->band[1].det*wd2) >> 15; 340 /* Block 5H, RECONS */ 341 rhigh = dhigh + s->band[1].s; 342 /* Block 6H, LIMIT */ 343 if (rhigh > 16383) 344 rhigh = 16383; 345 else if (rhigh < -16384) 346 rhigh = -16384; 347 348 /* Block 2H, INVQAH */ 349 wd2 = rh2[ihigh]; 350 wd1 = (s->band[1].nb*127) >> 7; 351 wd1 += wh[wd2]; 352 if (wd1 < 0) 353 wd1 = 0; 354 else if (wd1 > 22528) 355 wd1 = 22528; 356 s->band[1].nb = wd1; 357 358 /* Block 3H, SCALEH */ 359 wd1 = (s->band[1].nb >> 6) & 31; 360 wd2 = 10 - (s->band[1].nb >> 11); 361 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); 362 s->band[1].det = wd3 << 2; 363 364 block4(s, 1, dhigh); 365 } 366 367 if (s->itu_test_mode) 368 { 369 amp[outlen++] = (int16_t) (rlow << 1); 370 amp[outlen++] = (int16_t) (rhigh << 1); 371 } 372 else 373 { 374 if (s->eight_k) 375 { 376 amp[outlen++] = (int16_t) (rlow << 1); 377 } 378 else 379 { 380 /* Apply the receive QMF */ 381 for (i = 0; i < 22; i++) 382 s->x[i] = s->x[i + 2]; 383 s->x[22] = rlow + rhigh; 384 s->x[23] = rlow - rhigh; 385 386 xout1 = 0; 387 xout2 = 0; 388 for (i = 0; i < 12; i++) 389 { 390 xout2 += s->x[2*i]*qmf_coeffs[i]; 391 xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i]; 392 } 393 /* We shift by 12 to allow for the QMF filters (DC gain = 4096), less 1 394 to allow for the 15 bit input to the G.722 algorithm. */ 395 /* WebRtc, tlegrand: added saturation */ 396 amp[outlen++] = saturate(xout1 >> 11); 397 amp[outlen++] = saturate(xout2 >> 11); 398 } 399 } 400 } 401 return outlen; 402 } 403 /*- End of function --------------------------------------------------------*/ 404 /*- End of file ------------------------------------------------------------*/ 405