1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 /**************************************************************************************** 19 Portions of this file are derived from the following 3GPP standard: 20 21 3GPP TS 26.173 22 ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec 23 Available from http://www.3gpp.org 24 25 (C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) 26 Permission to distribute, modify and use this file under the standard license 27 terms listed above has been obtained from the copyright holder. 28 ****************************************************************************************/ 29 /* 30 ------------------------------------------------------------------------------ 31 32 33 34 Filename: synthesis_amr_wb.cpp 35 36 Date: 05/04/2007 37 38 ------------------------------------------------------------------------------ 39 REVISION HISTORY 40 41 42 Description: 43 44 ------------------------------------------------------------------------------ 45 INPUT AND OUTPUT DEFINITIONS 46 47 int16 Aq[], A(z) : quantized Az 48 int16 exc[], (i) : excitation at 12kHz 49 int16 Q_new, (i) : scaling performed on exc 50 int16 synth16k[], (o) : 16kHz synthesis signal 51 int16 prms, (i) : compressed amr wb 52 int16 HfIsf[], 53 int16 nb_bits, 54 int16 newDTXState, 55 Decoder_State * st, (i/o) : State structure 56 int16 bfi, (i) : bad frame indicator 57 int16 *ScratchMem 58 59 60 ------------------------------------------------------------------------------ 61 FUNCTION DESCRIPTION 62 63 Synthesis of signal at 16kHz with HF extension 64 65 ------------------------------------------------------------------------------ 66 REQUIREMENTS 67 68 69 ------------------------------------------------------------------------------ 70 REFERENCES 71 72 ------------------------------------------------------------------------------ 73 PSEUDO-CODE 74 75 ------------------------------------------------------------------------------ 76 */ 77 78 79 /*---------------------------------------------------------------------------- 80 ; INCLUDES 81 ----------------------------------------------------------------------------*/ 82 83 #include "pv_amr_wb_type_defs.h" 84 #include "pvamrwbdecoder_mem_funcs.h" 85 #include "pvamrwbdecoder_basic_op.h" 86 #include "pvamrwbdecoder_cnst.h" 87 #include "pvamrwbdecoder_acelp.h" 88 #include "e_pv_amrwbdec.h" 89 #include "get_amr_wb_bits.h" 90 #include "pvamrwb_math_op.h" 91 #include "pvamrwbdecoder_api.h" 92 #include "synthesis_amr_wb.h" 93 94 /*---------------------------------------------------------------------------- 95 ; MACROS 96 ; Define module specific macros here 97 ----------------------------------------------------------------------------*/ 98 99 100 /*---------------------------------------------------------------------------- 101 ; DEFINES 102 ; Include all pre-processor statements here. Include conditional 103 ; compile variables also. 104 ----------------------------------------------------------------------------*/ 105 106 /*---------------------------------------------------------------------------- 107 ; LOCAL FUNCTION DEFINITIONS 108 ; Function Prototype declaration 109 ----------------------------------------------------------------------------*/ 110 111 /*---------------------------------------------------------------------------- 112 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS 113 ; Variable declaration - defined here and used outside this module 114 ----------------------------------------------------------------------------*/ 115 /* High Band encoding */ 116 const int16 HP_gain[16] = 117 { 118 3624, 4673, 5597, 6479, 7425, 8378, 9324, 10264, 119 11210, 12206, 13391, 14844, 16770, 19655, 24289, 32728 120 }; 121 122 /*---------------------------------------------------------------------------- 123 ; EXTERNAL FUNCTION REFERENCES 124 ; Declare functions defined elsewhere and referenced in this module 125 ----------------------------------------------------------------------------*/ 126 127 /*---------------------------------------------------------------------------- 128 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES 129 ; Declare variables used in this module but defined elsewhere 130 ----------------------------------------------------------------------------*/ 131 132 /*---------------------------------------------------------------------------- 133 ; FUNCTION CODE 134 ----------------------------------------------------------------------------*/ 135 136 void synthesis_amr_wb( 137 int16 Aq[], /* A(z) : quantized Az */ 138 int16 exc[], /* (i) : excitation at 12kHz */ 139 int16 Q_new, /* (i) : scaling performed on exc */ 140 int16 synth16k[], /* (o) : 16kHz synthesis signal */ 141 int16 prms, /* (i) : parameter */ 142 int16 HfIsf[], 143 int16 nb_bits, 144 int16 newDTXState, 145 Decoder_State * st, /* (i/o) : State structure */ 146 int16 bfi, /* (i) : bad frame indicator */ 147 int16 *ScratchMem 148 ) 149 { 150 int16 i, fac, exp; 151 int16 tmp; 152 int16 ener, exp_ener; 153 int32 L_tmp; 154 int32 L_tmp2; 155 156 int16 HF_corr_gain; 157 int16 HF_gain_ind; 158 int16 gain1, gain2; 159 160 int16 *pt_synth; 161 int16 *pt_HF; 162 int16 *synth_hi = ScratchMem; 163 int16 *synth_lo = &ScratchMem[M + L_SUBFR]; 164 int16 *synth = &synth_lo[M + L_SUBFR]; 165 int16 *HF = &synth[L_SUBFR]; 166 int16 *Ap = &HF[L_SUBFR16k]; /* High Frequency vector */ 167 int16 *HfA = &Ap[M16k + 1]; 168 int16 *pt_tmp; 169 170 /*------------------------------------------------------------* 171 * speech synthesis * 172 * ~~~~~~~~~~~~~~~~ * 173 * - Find synthesis speech corresponding to exc2[]. * 174 * - Perform fixed deemphasis and hp 50hz filtering. * 175 * - Oversampling from 12.8kHz to 16kHz. * 176 *------------------------------------------------------------*/ 177 178 pv_memcpy((void *)synth_hi, 179 (void *)st->mem_syn_hi, 180 M*sizeof(*synth_hi)); 181 182 pv_memcpy((void *)synth_lo, 183 (void *)st->mem_syn_lo, 184 M*sizeof(*synth_lo)); 185 186 Syn_filt_32(Aq, M, exc, Q_new, synth_hi + M, synth_lo + M, L_SUBFR); 187 188 pv_memcpy((void *)st->mem_syn_hi, 189 (void *)(synth_hi + L_SUBFR), 190 M*sizeof(*st->mem_syn_hi)); 191 192 pv_memcpy((void *)st->mem_syn_lo, 193 (void *)(synth_lo + L_SUBFR), 194 M*sizeof(*st->mem_syn_lo)); 195 196 deemphasis_32(synth_hi + M, 197 synth_lo + M, 198 synth, 199 PREEMPH_FAC, 200 L_SUBFR, 201 &(st->mem_deemph)); 202 203 highpass_50Hz_at_12k8(synth, 204 L_SUBFR, 205 st->mem_sig_out); 206 207 oversamp_12k8_to_16k(synth, 208 L_SUBFR, 209 synth16k, 210 st->mem_oversamp, 211 ScratchMem); 212 213 /* 214 * HF noise synthesis 215 * - Generate HF noise between 5.5 and 7.5 kHz. 216 * - Set energy of noise according to synthesis tilt. 217 * tilt > 0.8 ==> - 14 dB (voiced) 218 * tilt 0.5 ==> - 6 dB (voiced or noise) 219 * tilt < 0.0 ==> 0 dB (noise) 220 */ 221 222 /* generate white noise vector */ 223 pt_tmp = HF; 224 for (i = L_SUBFR16k >> 2; i != 0 ; i--) 225 { 226 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3; 227 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3; 228 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3; 229 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3; 230 } 231 /* energy of excitation */ 232 233 pt_tmp = exc; 234 235 for (i = L_SUBFR >> 2; i != 0; i--) 236 { 237 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3; 238 pt_tmp++; 239 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3; 240 pt_tmp++; 241 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3; 242 pt_tmp++; 243 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3; 244 pt_tmp++; 245 } 246 247 248 Q_new -= 3; 249 250 ener = extract_h(Dot_product12(exc, exc, L_SUBFR, &exp_ener)); 251 exp_ener -= Q_new << 1; 252 253 /* set energy of white noise to energy of excitation */ 254 255 tmp = extract_h(Dot_product12(HF, HF, L_SUBFR16k, &exp)); 256 257 if (tmp > ener) 258 { 259 tmp >>= 1; /* Be sure tmp < ener */ 260 exp += 1; 261 } 262 L_tmp = L_deposit_h(div_16by16(tmp, ener)); /* result is normalized */ 263 exp -= exp_ener; 264 one_ov_sqrt_norm(&L_tmp, &exp); 265 L_tmp = shl_int32(L_tmp, exp + 1); /* L_tmp x 2, L_tmp in Q31 */ 266 267 tmp = (int16)(L_tmp >> 16); /* tmp = 2 x sqrt(ener_exc/ener_hf) */ 268 269 270 271 pt_tmp = HF; 272 for (i = L_SUBFR16k >> 2; i != 0 ; i--) 273 { 274 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15); 275 pt_tmp++; 276 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15); 277 pt_tmp++; 278 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15); 279 pt_tmp++; 280 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15); 281 pt_tmp++; 282 } 283 284 /* find tilt of synthesis speech (tilt: 1=voiced, -1=unvoiced) */ 285 286 highpass_400Hz_at_12k8(synth, L_SUBFR, st->mem_hp400); 287 288 L_tmp = 1L; 289 L_tmp2 = 1L; 290 291 292 L_tmp = mac_16by16_to_int32(L_tmp, synth[0], synth[0]); 293 294 for (i = 1; i < L_SUBFR; i++) 295 { 296 L_tmp = mac_16by16_to_int32(L_tmp, synth[i], synth[i ]); 297 L_tmp2 = mac_16by16_to_int32(L_tmp2, synth[i], synth[i - 1]); 298 } 299 300 301 exp = normalize_amr_wb(L_tmp); 302 303 ener = (int16)((L_tmp << exp) >> 16); /* ener = r[0] */ 304 tmp = (int16)((L_tmp2 << exp) >> 16); /* tmp = r[1] */ 305 306 if (tmp > 0) 307 { 308 fac = div_16by16(tmp, ener); 309 } 310 else 311 { 312 fac = 0; 313 } 314 315 /* modify energy of white noise according to synthesis tilt */ 316 gain1 = 32767 - fac; 317 gain2 = mult_int16(gain1, 20480); 318 gain2 = shl_int16(gain2, 1); 319 320 if (st->vad_hist > 0) 321 { 322 tmp = gain2 - 1; 323 } 324 else 325 { 326 tmp = gain1 - 1; 327 } 328 329 330 if (tmp != 0) 331 { 332 tmp++; 333 } 334 335 if (tmp < 3277) 336 { 337 tmp = 3277; /* 0.1 in Q15 */ 338 339 } 340 341 342 if ((nb_bits >= NBBITS_24k) && (bfi == 0)) 343 { 344 /* HF correction gain */ 345 HF_gain_ind = prms; 346 HF_corr_gain = HP_gain[HF_gain_ind]; 347 348 pt_tmp = HF; 349 for (i = L_SUBFR16k >> 2; i != 0 ; i--) 350 { 351 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1; 352 pt_tmp++; 353 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1; 354 pt_tmp++; 355 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1; 356 pt_tmp++; 357 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1; 358 pt_tmp++; 359 } 360 361 /* HF gain */ 362 } 363 else 364 { 365 pt_tmp = HF; 366 for (i = L_SUBFR16k >> 2; i != 0 ; i--) 367 { 368 *(pt_tmp) = mult_int16(*(pt_tmp), tmp); 369 pt_tmp++; 370 *(pt_tmp) = mult_int16(*(pt_tmp), tmp); 371 pt_tmp++; 372 *(pt_tmp) = mult_int16(*(pt_tmp), tmp); 373 pt_tmp++; 374 *(pt_tmp) = mult_int16(*(pt_tmp), tmp); 375 pt_tmp++; 376 } 377 } 378 379 380 if ((nb_bits <= NBBITS_7k) && (newDTXState == SPEECH)) 381 { 382 isf_extrapolation(HfIsf); 383 Isp_Az(HfIsf, HfA, M16k, 0); 384 385 weight_amrwb_lpc(HfA, Ap, 29491, M16k); /* fac=0.9 */ 386 387 wb_syn_filt(Ap, 388 M16k, 389 HF, 390 HF, 391 L_SUBFR16k, 392 st->mem_syn_hf, 393 1, 394 ScratchMem); 395 } 396 else 397 { 398 /* synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz */ 399 weight_amrwb_lpc(Aq, Ap, 19661, M); /* fac=0.6 */ 400 401 wb_syn_filt(Ap, 402 M, 403 HF, 404 HF, 405 L_SUBFR16k, 406 st->mem_syn_hf + (M16k - M), 407 1, 408 ScratchMem); 409 } 410 411 /* noise Band Pass filtering (1ms of delay) */ 412 band_pass_6k_7k(HF, 413 L_SUBFR16k, 414 st->mem_hf, 415 ScratchMem); 416 417 418 if (nb_bits >= NBBITS_24k) 419 { 420 /* Low Pass filtering (7 kHz) */ 421 low_pass_filt_7k(HF, 422 L_SUBFR16k, 423 st->mem_hf3, 424 ScratchMem); 425 } 426 /* add filtered HF noise to speech synthesis */ 427 428 pt_synth = synth16k; 429 pt_HF = HF; 430 431 for (i = L_SUBFR16k >> 1; i != 0; i--) 432 { 433 *(pt_synth) = add_int16(*(pt_synth), *(pt_HF++)); /* check 16 bit saturation */ 434 pt_synth++; 435 *(pt_synth) = add_int16(*(pt_synth), *(pt_HF++)); 436 pt_synth++; 437 } 438 439 } 440 441