1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: synthesis_amr_wb.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
39 int16 Aq[], A(z) : quantized Az
40 int16 exc[], (i) : excitation at 12kHz
41 int16 Q_new, (i) : scaling performed on exc
42 int16 synth16k[], (o) : 16kHz synthesis signal
43 int16 prms, (i) : compressed amr wb
47 Decoder_State * st, (i/o) : State structure
48 int16 bfi, (i) : bad frame indicator
52 ------------------------------------------------------------------------------
55 Synthesis of signal at 16kHz with HF extension
57 ------------------------------------------------------------------------------
61 ------------------------------------------------------------------------------
64 ------------------------------------------------------------------------------
67 ------------------------------------------------------------------------------
71 /*----------------------------------------------------------------------------
73 ----------------------------------------------------------------------------*/
75 #include "pv_amr_wb_type_defs.h"
76 #include "pvamrwbdecoder_mem_funcs.h"
77 #include "pvamrwbdecoder_basic_op.h"
78 #include "pvamrwbdecoder_cnst.h"
79 #include "pvamrwbdecoder_acelp.h"
80 #include "e_pv_amrwbdec.h"
81 #include "get_amr_wb_bits.h"
82 #include "pvamrwb_math_op.h"
83 #include "pvamrwbdecoder_api.h"
84 #include "synthesis_amr_wb.h"
86 /*----------------------------------------------------------------------------
88 ; Define module specific macros here
89 ----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------
94 ; Include all pre-processor statements here. Include conditional
95 ; compile variables also.
96 ----------------------------------------------------------------------------*/
98 /*----------------------------------------------------------------------------
99 ; LOCAL FUNCTION DEFINITIONS
100 ; Function Prototype declaration
101 ----------------------------------------------------------------------------*/
103 /*----------------------------------------------------------------------------
104 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
105 ; Variable declaration - defined here and used outside this module
106 ----------------------------------------------------------------------------*/
107 /* High Band encoding */
108 const int16 HP_gain[16] =
110 3624, 4673, 5597, 6479, 7425, 8378, 9324, 10264,
111 11210, 12206, 13391, 14844, 16770, 19655, 24289, 32728
114 /*----------------------------------------------------------------------------
115 ; EXTERNAL FUNCTION REFERENCES
116 ; Declare functions defined elsewhere and referenced in this module
117 ----------------------------------------------------------------------------*/
119 /*----------------------------------------------------------------------------
120 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
121 ; Declare variables used in this module but defined elsewhere
122 ----------------------------------------------------------------------------*/
124 /*----------------------------------------------------------------------------
126 ----------------------------------------------------------------------------*/
128 void synthesis_amr_wb(
129 int16 Aq[], /* A(z) : quantized Az */
130 int16 exc[], /* (i) : excitation at 12kHz */
131 int16 Q_new, /* (i) : scaling performed on exc */
132 int16 synth16k[], /* (o) : 16kHz synthesis signal */
133 int16 prms, /* (i) : parameter */
137 Decoder_State * st, /* (i/o) : State structure */
138 int16 bfi, /* (i) : bad frame indicator */
144 int16 ener, exp_ener;
154 int16 *synth_hi = ScratchMem;
155 int16 *synth_lo = &ScratchMem[M + L_SUBFR];
156 int16 *synth = &synth_lo[M + L_SUBFR];
157 int16 *HF = &synth[L_SUBFR];
158 int16 *Ap = &HF[L_SUBFR16k]; /* High Frequency vector */
159 int16 *HfA = &Ap[M16k + 1];
162 /*------------------------------------------------------------*
165 * - Find synthesis speech corresponding to exc2[]. *
166 * - Perform fixed deemphasis and hp 50hz filtering. *
167 * - Oversampling from 12.8kHz to 16kHz. *
168 *------------------------------------------------------------*/
170 pv_memcpy((void *)synth_hi,
171 (void *)st->mem_syn_hi,
172 M*sizeof(*synth_hi));
174 pv_memcpy((void *)synth_lo,
175 (void *)st->mem_syn_lo,
176 M*sizeof(*synth_lo));
178 Syn_filt_32(Aq, M, exc, Q_new, synth_hi + M, synth_lo + M, L_SUBFR);
180 pv_memcpy((void *)st->mem_syn_hi,
181 (void *)(synth_hi + L_SUBFR),
182 M*sizeof(*st->mem_syn_hi));
184 pv_memcpy((void *)st->mem_syn_lo,
185 (void *)(synth_lo + L_SUBFR),
186 M*sizeof(*st->mem_syn_lo));
188 deemphasis_32(synth_hi + M,
195 highpass_50Hz_at_12k8(synth,
199 oversamp_12k8_to_16k(synth,
207 * - Generate HF noise between 5.5 and 7.5 kHz.
208 * - Set energy of noise according to synthesis tilt.
209 * tilt > 0.8 ==> - 14 dB (voiced)
210 * tilt 0.5 ==> - 6 dB (voiced or noise)
211 * tilt < 0.0 ==> 0 dB (noise)
214 /* generate white noise vector */
216 for (i = L_SUBFR16k >> 2; i != 0 ; i--)
218 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3;
219 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3;
220 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3;
221 *(pt_tmp++) = noise_gen_amrwb(&(st->seed2)) >> 3;
223 /* energy of excitation */
227 for (i = L_SUBFR >> 2; i != 0; i--)
229 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
231 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
233 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
235 *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
242 ener = extract_h(Dot_product12(exc, exc, L_SUBFR, &exp_ener));
243 exp_ener -= Q_new << 1;
245 /* set energy of white noise to energy of excitation */
247 tmp = extract_h(Dot_product12(HF, HF, L_SUBFR16k, &exp));
251 tmp >>= 1; /* Be sure tmp < ener */
254 L_tmp = L_deposit_h(div_16by16(tmp, ener)); /* result is normalized */
256 one_ov_sqrt_norm(&L_tmp, &exp);
257 L_tmp = shl_int32(L_tmp, exp + 1); /* L_tmp x 2, L_tmp in Q31 */
259 tmp = (int16)(L_tmp >> 16); /* tmp = 2 x sqrt(ener_exc/ener_hf) */
264 for (i = L_SUBFR16k >> 2; i != 0 ; i--)
266 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
268 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
270 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
272 *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
276 /* find tilt of synthesis speech (tilt: 1=voiced, -1=unvoiced) */
278 highpass_400Hz_at_12k8(synth, L_SUBFR, st->mem_hp400);
284 L_tmp = mac_16by16_to_int32(L_tmp, synth[0], synth[0]);
286 for (i = 1; i < L_SUBFR; i++)
288 L_tmp = mac_16by16_to_int32(L_tmp, synth[i], synth[i ]);
289 L_tmp2 = mac_16by16_to_int32(L_tmp2, synth[i], synth[i - 1]);
293 exp = normalize_amr_wb(L_tmp);
295 ener = (int16)((L_tmp << exp) >> 16); /* ener = r[0] */
296 tmp = (int16)((L_tmp2 << exp) >> 16); /* tmp = r[1] */
300 fac = div_16by16(tmp, ener);
307 /* modify energy of white noise according to synthesis tilt */
309 gain2 = mult_int16(gain1, 20480);
310 gain2 = shl_int16(gain2, 1);
312 if (st->vad_hist > 0)
329 tmp = 3277; /* 0.1 in Q15 */
334 if ((nb_bits >= NBBITS_24k) && (bfi == 0))
336 /* HF correction gain */
338 HF_corr_gain = HP_gain[HF_gain_ind];
341 for (i = L_SUBFR16k >> 2; i != 0 ; i--)
343 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
345 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
347 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
349 *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
358 for (i = L_SUBFR16k >> 2; i != 0 ; i--)
360 *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
362 *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
364 *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
366 *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
372 if ((nb_bits <= NBBITS_7k) && (newDTXState == SPEECH))
374 isf_extrapolation(HfIsf);
375 Isp_Az(HfIsf, HfA, M16k, 0);
377 weight_amrwb_lpc(HfA, Ap, 29491, M16k); /* fac=0.9 */
390 /* synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz */
391 weight_amrwb_lpc(Aq, Ap, 19661, M); /* fac=0.6 */
398 st->mem_syn_hf + (M16k - M),
403 /* noise Band Pass filtering (1ms of delay) */
410 if (nb_bits >= NBBITS_24k)
412 /* Low Pass filtering (7 kHz) */
418 /* add filtered HF noise to speech synthesis */
423 for (i = L_SUBFR16k >> 1; i != 0; i--)
425 *(pt_synth) = add_int16(*(pt_synth), *(pt_HF++)); /* check 16 bit saturation */
427 *(pt_synth) = add_int16(*(pt_synth), *(pt_HF++));