15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SpanDSP - a series of DSP components for telephony 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * g722_decode.c - The ITU G.722 codec, decode part. 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Written by Steve Underwood <steveu@coppice.org> 71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * 81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * Copyright (C) 2005 Steve Underwood 91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * 10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) * Despite my general liking of the GPL, I place my own contributions 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * to this code in the public domain for the benefit of all mankind - 121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * even the slimy ones who might try to proprietize my work and use it 131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * to my detriment. 141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Based in part on a single channel G.722 codec which is: 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * Copyright (c) CMU 1993 18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * Computer Science, Speech Group 19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * Chengxiang Lu and Alex Hauptmann 20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * 21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * $Id: g722_decode.c,v 1.15 2006/07/07 16:37:49 steveu Exp $ 22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * 23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * Modifications for WebRtc, 2011/04/28, by tlegrand: 24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * -Removed usage of inttypes.h and tgmath.h 25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * -Changed to use WebRtc types 26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * -Changed __inline__ to __inline 27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * -Added saturation check on output 28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) */ 29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)/*! \file */ 31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef HAVE_CONFIG_H 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <config.h> 351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <memory.h> 38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <stdio.h> 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <stdlib.h> 401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "g722_enc_dec.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "webrtc/typedefs.h" 43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if !defined(FALSE) 451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define FALSE 0 46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif 47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if !defined(TRUE) 48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define TRUE (!FALSE) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static __inline int16_t saturate(int32_t amp) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int16_t amp16; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Hopefully this is optimised for the common case - not clipping */ 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) amp16 = (int16_t) amp; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (amp == amp16) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return amp16; 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (amp > WEBRTC_INT16_MAX) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return WEBRTC_INT16_MAX; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return WEBRTC_INT16_MIN; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*- End of function --------------------------------------------------------*/ 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void block4(g722_decode_state_t *s, int band, int d); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void block4(g722_decode_state_t *s, int band, int d) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){ 691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci int wd1; 701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci int wd2; 711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci int wd3; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int i; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Block 4, RECONS */ 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].d[0] = d; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].r[0] = saturate(s->band[band].s + d); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Block 4, PARREC */ 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].p[0] = saturate(s->band[band].sz + d); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Block 4, UPPOL2 */ 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (i = 0; i < 3; i++) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].sg[i] = s->band[band].p[i] >> 15; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd1 = saturate(s->band[band].a[1] << 2); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (wd2 > 32767) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd2 = 32767; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd3 = (s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd3 += (wd2 >> 7); 911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci wd3 += (s->band[band].a[2]*32512) >> 15; 921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (wd3 > 12288) 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci wd3 = 12288; 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (wd3 < -12288) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd3 = -12288; 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].ap[2] = wd3; 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Block 4, UPPOL1 */ 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].sg[0] = s->band[band].p[0] >> 15; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].sg[1] = s->band[band].p[1] >> 15; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd1 = (s->band[band].sg[0] == s->band[band].sg[1]) ? 192 : -192; 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd2 = (s->band[band].a[1]*32640) >> 15; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].ap[1] = saturate(wd1 + wd2); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) wd3 = saturate(15360 - s->band[band].ap[2]); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (s->band[band].ap[1] > wd3) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].ap[1] = wd3; 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (s->band[band].ap[1] < -wd3) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) s->band[band].ap[1] = -wd3; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /* Block 4, UPZERO */ 112a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 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 157g722_decode_state_t *WebRtc_g722_decode_init(g722_decode_state_t *s, 158 int rate, 159 int options) 160{ 161 if (s == NULL) 162 { 163 if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL) 164 return NULL; 165 } 166 memset(s, 0, sizeof(*s)); 167 if (rate == 48000) 168 s->bits_per_sample = 6; 169 else if (rate == 56000) 170 s->bits_per_sample = 7; 171 else 172 s->bits_per_sample = 8; 173 if ((options & G722_SAMPLE_RATE_8000)) 174 s->eight_k = TRUE; 175 if ((options & G722_PACKED) && s->bits_per_sample != 8) 176 s->packed = TRUE; 177 else 178 s->packed = FALSE; 179 s->band[0].det = 32; 180 s->band[1].det = 8; 181 return s; 182} 183/*- End of function --------------------------------------------------------*/ 184 185int WebRtc_g722_decode_release(g722_decode_state_t *s) 186{ 187 free(s); 188 return 0; 189} 190/*- End of function --------------------------------------------------------*/ 191 192int WebRtc_g722_decode(g722_decode_state_t *s, int16_t amp[], 193 const uint8_t g722_data[], int len) 194{ 195 static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 }; 196 static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 197 7, 6, 5, 4, 3, 2, 1, 0 }; 198 static const int ilb[32] = 199 { 200 2048, 2093, 2139, 2186, 2233, 2282, 2332, 201 2383, 2435, 2489, 2543, 2599, 2656, 2714, 202 2774, 2834, 2896, 2960, 3025, 3091, 3158, 203 3228, 3298, 3371, 3444, 3520, 3597, 3676, 204 3756, 3838, 3922, 4008 205 }; 206 static const int wh[3] = {0, -214, 798}; 207 static const int rh2[4] = {2, 1, 2, 1}; 208 static const int qm2[4] = {-7408, -1616, 7408, 1616}; 209 static const int qm4[16] = 210 { 211 0, -20456, -12896, -8968, 212 -6288, -4240, -2584, -1200, 213 20456, 12896, 8968, 6288, 214 4240, 2584, 1200, 0 215 }; 216 static const int qm5[32] = 217 { 218 -280, -280, -23352, -17560, 219 -14120, -11664, -9752, -8184, 220 -6864, -5712, -4696, -3784, 221 -2960, -2208, -1520, -880, 222 23352, 17560, 14120, 11664, 223 9752, 8184, 6864, 5712, 224 4696, 3784, 2960, 2208, 225 1520, 880, 280, -280 226 }; 227 static const int qm6[64] = 228 { 229 -136, -136, -136, -136, 230 -24808, -21904, -19008, -16704, 231 -14984, -13512, -12280, -11192, 232 -10232, -9360, -8576, -7856, 233 -7192, -6576, -6000, -5456, 234 -4944, -4464, -4008, -3576, 235 -3168, -2776, -2400, -2032, 236 -1688, -1360, -1040, -728, 237 24808, 21904, 19008, 16704, 238 14984, 13512, 12280, 11192, 239 10232, 9360, 8576, 7856, 240 7192, 6576, 6000, 5456, 241 4944, 4464, 4008, 3576, 242 3168, 2776, 2400, 2032, 243 1688, 1360, 1040, 728, 244 432, 136, -432, -136 245 }; 246 static const int qmf_coeffs[12] = 247 { 248 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11, 249 }; 250 251 int dlowt; 252 int rlow; 253 int ihigh; 254 int dhigh; 255 int rhigh; 256 int xout1; 257 int xout2; 258 int wd1; 259 int wd2; 260 int wd3; 261 int code; 262 int outlen; 263 int i; 264 int j; 265 266 outlen = 0; 267 rhigh = 0; 268 for (j = 0; j < len; ) 269 { 270 if (s->packed) 271 { 272 /* Unpack the code bits */ 273 if (s->in_bits < s->bits_per_sample) 274 { 275 s->in_buffer |= (g722_data[j++] << s->in_bits); 276 s->in_bits += 8; 277 } 278 code = s->in_buffer & ((1 << s->bits_per_sample) - 1); 279 s->in_buffer >>= s->bits_per_sample; 280 s->in_bits -= s->bits_per_sample; 281 } 282 else 283 { 284 code = g722_data[j++]; 285 } 286 287 switch (s->bits_per_sample) 288 { 289 default: 290 case 8: 291 wd1 = code & 0x3F; 292 ihigh = (code >> 6) & 0x03; 293 wd2 = qm6[wd1]; 294 wd1 >>= 2; 295 break; 296 case 7: 297 wd1 = code & 0x1F; 298 ihigh = (code >> 5) & 0x03; 299 wd2 = qm5[wd1]; 300 wd1 >>= 1; 301 break; 302 case 6: 303 wd1 = code & 0x0F; 304 ihigh = (code >> 4) & 0x03; 305 wd2 = qm4[wd1]; 306 break; 307 } 308 /* Block 5L, LOW BAND INVQBL */ 309 wd2 = (s->band[0].det*wd2) >> 15; 310 /* Block 5L, RECONS */ 311 rlow = s->band[0].s + wd2; 312 /* Block 6L, LIMIT */ 313 if (rlow > 16383) 314 rlow = 16383; 315 else if (rlow < -16384) 316 rlow = -16384; 317 318 /* Block 2L, INVQAL */ 319 wd2 = qm4[wd1]; 320 dlowt = (s->band[0].det*wd2) >> 15; 321 322 /* Block 3L, LOGSCL */ 323 wd2 = rl42[wd1]; 324 wd1 = (s->band[0].nb*127) >> 7; 325 wd1 += wl[wd2]; 326 if (wd1 < 0) 327 wd1 = 0; 328 else if (wd1 > 18432) 329 wd1 = 18432; 330 s->band[0].nb = wd1; 331 332 /* Block 3L, SCALEL */ 333 wd1 = (s->band[0].nb >> 6) & 31; 334 wd2 = 8 - (s->band[0].nb >> 11); 335 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); 336 s->band[0].det = wd3 << 2; 337 338 block4(s, 0, dlowt); 339 340 if (!s->eight_k) 341 { 342 /* Block 2H, INVQAH */ 343 wd2 = qm2[ihigh]; 344 dhigh = (s->band[1].det*wd2) >> 15; 345 /* Block 5H, RECONS */ 346 rhigh = dhigh + s->band[1].s; 347 /* Block 6H, LIMIT */ 348 if (rhigh > 16383) 349 rhigh = 16383; 350 else if (rhigh < -16384) 351 rhigh = -16384; 352 353 /* Block 2H, INVQAH */ 354 wd2 = rh2[ihigh]; 355 wd1 = (s->band[1].nb*127) >> 7; 356 wd1 += wh[wd2]; 357 if (wd1 < 0) 358 wd1 = 0; 359 else if (wd1 > 22528) 360 wd1 = 22528; 361 s->band[1].nb = wd1; 362 363 /* Block 3H, SCALEH */ 364 wd1 = (s->band[1].nb >> 6) & 31; 365 wd2 = 10 - (s->band[1].nb >> 11); 366 wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); 367 s->band[1].det = wd3 << 2; 368 369 block4(s, 1, dhigh); 370 } 371 372 if (s->itu_test_mode) 373 { 374 amp[outlen++] = (int16_t) (rlow << 1); 375 amp[outlen++] = (int16_t) (rhigh << 1); 376 } 377 else 378 { 379 if (s->eight_k) 380 { 381 amp[outlen++] = (int16_t) (rlow << 1); 382 } 383 else 384 { 385 /* Apply the receive QMF */ 386 for (i = 0; i < 22; i++) 387 s->x[i] = s->x[i + 2]; 388 s->x[22] = rlow + rhigh; 389 s->x[23] = rlow - rhigh; 390 391 xout1 = 0; 392 xout2 = 0; 393 for (i = 0; i < 12; i++) 394 { 395 xout2 += s->x[2*i]*qmf_coeffs[i]; 396 xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i]; 397 } 398 /* We shift by 12 to allow for the QMF filters (DC gain = 4096), less 1 399 to allow for the 15 bit input to the G.722 algorithm. */ 400 /* WebRtc, tlegrand: added saturation */ 401 amp[outlen++] = saturate(xout1 >> 11); 402 amp[outlen++] = saturate(xout2 >> 11); 403 } 404 } 405 } 406 return outlen; 407} 408/*- End of function --------------------------------------------------------*/ 409/*- End of file ------------------------------------------------------------*/ 410