Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / synthesis_amr_wb.cpp
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 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
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
44      int16 HfIsf[],
45      int16 nb_bits,
46      int16 newDTXState,
47      Decoder_State * st,                   (i/o) : State structure
48      int16 bfi,                            (i)   : bad frame indicator
49      int16 *ScratchMem
50
51
52 ------------------------------------------------------------------------------
53  FUNCTION DESCRIPTION
54
55     Synthesis of signal at 16kHz with HF extension
56
57 ------------------------------------------------------------------------------
58  REQUIREMENTS
59
60
61 ------------------------------------------------------------------------------
62  REFERENCES
63
64 ------------------------------------------------------------------------------
65  PSEUDO-CODE
66
67 ------------------------------------------------------------------------------
68 */
69
70
71 /*----------------------------------------------------------------------------
72 ; INCLUDES
73 ----------------------------------------------------------------------------*/
74
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"
85
86 /*----------------------------------------------------------------------------
87 ; MACROS
88 ; Define module specific macros here
89 ----------------------------------------------------------------------------*/
90
91
92 /*----------------------------------------------------------------------------
93 ; DEFINES
94 ; Include all pre-processor statements here. Include conditional
95 ; compile variables also.
96 ----------------------------------------------------------------------------*/
97
98 /*----------------------------------------------------------------------------
99 ; LOCAL FUNCTION DEFINITIONS
100 ; Function Prototype declaration
101 ----------------------------------------------------------------------------*/
102
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] =
109 {
110     3624, 4673, 5597, 6479, 7425, 8378, 9324, 10264,
111     11210, 12206, 13391, 14844, 16770, 19655, 24289, 32728
112 };
113
114 /*----------------------------------------------------------------------------
115 ; EXTERNAL FUNCTION REFERENCES
116 ; Declare functions defined elsewhere and referenced in this module
117 ----------------------------------------------------------------------------*/
118
119 /*----------------------------------------------------------------------------
120 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
121 ; Declare variables used in this module but defined elsewhere
122 ----------------------------------------------------------------------------*/
123
124 /*----------------------------------------------------------------------------
125 ; FUNCTION CODE
126 ----------------------------------------------------------------------------*/
127
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                  */
134     int16 HfIsf[],
135     int16 nb_bits,
136     int16 newDTXState,
137     Decoder_State * st,      /* (i/o) : State structure            */
138     int16 bfi,               /* (i)   : bad frame indicator        */
139     int16 *ScratchMem
140 )
141 {
142     int16 i, fac, exp;
143     int16 tmp;
144     int16 ener, exp_ener;
145     int32 L_tmp;
146     int32 L_tmp2;
147
148     int16 HF_corr_gain;
149     int16 HF_gain_ind;
150     int16 gain1, gain2;
151
152     int16 *pt_synth;
153     int16 *pt_HF;
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];
160     int16 *pt_tmp;
161
162     /*------------------------------------------------------------*
163      * speech synthesis                                           *
164      * ~~~~~~~~~~~~~~~~                                           *
165      * - Find synthesis speech corresponding to exc2[].           *
166      * - Perform fixed deemphasis and hp 50hz filtering.          *
167      * - Oversampling from 12.8kHz to 16kHz.                      *
168      *------------------------------------------------------------*/
169
170     pv_memcpy((void *)synth_hi,
171               (void *)st->mem_syn_hi,
172               M*sizeof(*synth_hi));
173
174     pv_memcpy((void *)synth_lo,
175               (void *)st->mem_syn_lo,
176               M*sizeof(*synth_lo));
177
178     Syn_filt_32(Aq, M, exc, Q_new, synth_hi + M, synth_lo + M, L_SUBFR);
179
180     pv_memcpy((void *)st->mem_syn_hi,
181               (void *)(synth_hi + L_SUBFR),
182               M*sizeof(*st->mem_syn_hi));
183
184     pv_memcpy((void *)st->mem_syn_lo,
185               (void *)(synth_lo + L_SUBFR),
186               M*sizeof(*st->mem_syn_lo));
187
188     deemphasis_32(synth_hi + M,
189                   synth_lo + M,
190                   synth,
191                   PREEMPH_FAC,
192                   L_SUBFR,
193                   &(st->mem_deemph));
194
195     highpass_50Hz_at_12k8(synth,
196                           L_SUBFR,
197                           st->mem_sig_out);
198
199     oversamp_12k8_to_16k(synth,
200                          L_SUBFR,
201                          synth16k,
202                          st->mem_oversamp,
203                          ScratchMem);
204
205     /*
206      * HF noise synthesis
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)
212      */
213
214     /* generate white noise vector */
215     pt_tmp = HF;
216     for (i = L_SUBFR16k >> 2; i != 0 ; i--)
217     {
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;
222     }
223     /* energy of excitation */
224
225     pt_tmp = exc;
226
227     for (i = L_SUBFR >> 2; i != 0; i--)
228     {
229         *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
230         pt_tmp++;
231         *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
232         pt_tmp++;
233         *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
234         pt_tmp++;
235         *(pt_tmp) = add_int16(*(pt_tmp), 0x0004) >> 3;
236         pt_tmp++;
237     }
238
239
240     Q_new -= 3;
241
242     ener = extract_h(Dot_product12(exc, exc, L_SUBFR, &exp_ener));
243     exp_ener -= Q_new << 1;
244
245     /* set energy of white noise to energy of excitation */
246
247     tmp = extract_h(Dot_product12(HF, HF, L_SUBFR16k, &exp));
248
249     if (tmp > ener)
250     {
251         tmp >>=  1;                 /* Be sure tmp < ener */
252         exp += 1;
253     }
254     L_tmp = L_deposit_h(div_16by16(tmp, ener)); /* result is normalized */
255     exp -= exp_ener;
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 */
258
259     tmp = (int16)(L_tmp >> 16);    /* tmp = 2 x sqrt(ener_exc/ener_hf) */
260
261
262
263     pt_tmp = HF;
264     for (i = L_SUBFR16k >> 2; i != 0 ; i--)
265     {
266         *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
267         pt_tmp++;
268         *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
269         pt_tmp++;
270         *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
271         pt_tmp++;
272         *(pt_tmp) = (int16)(fxp_mul_16by16(*(pt_tmp), tmp) >> 15);
273         pt_tmp++;
274     }
275
276     /* find tilt of synthesis speech (tilt: 1=voiced, -1=unvoiced) */
277
278     highpass_400Hz_at_12k8(synth, L_SUBFR, st->mem_hp400);
279
280     L_tmp = 1L;
281     L_tmp2 = 1L;
282
283
284     L_tmp = mac_16by16_to_int32(L_tmp, synth[0], synth[0]);
285
286     for (i = 1; i < L_SUBFR; i++)
287     {
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]);
290     }
291
292
293     exp = normalize_amr_wb(L_tmp);
294
295     ener = (int16)((L_tmp << exp) >> 16);   /* ener = r[0] */
296     tmp  = (int16)((L_tmp2 << exp) >> 16);    /* tmp = r[1] */
297
298     if (tmp > 0)
299     {
300         fac = div_16by16(tmp, ener);
301     }
302     else
303     {
304         fac = 0;
305     }
306
307     /* modify energy of white noise according to synthesis tilt */
308     gain1 = 32767 - fac;
309     gain2 = mult_int16(gain1, 20480);
310     gain2 = shl_int16(gain2, 1);
311
312     if (st->vad_hist > 0)
313     {
314         tmp  = gain2 - 1;
315     }
316     else
317     {
318         tmp  = gain1 - 1;
319     }
320
321
322     if (tmp != 0)
323     {
324         tmp++;
325     }
326
327     if (tmp < 3277)
328     {
329         tmp = 3277;                        /* 0.1 in Q15 */
330
331     }
332
333
334     if ((nb_bits >= NBBITS_24k) && (bfi == 0))
335     {
336         /* HF correction gain */
337         HF_gain_ind = prms;
338         HF_corr_gain = HP_gain[HF_gain_ind];
339
340         pt_tmp = HF;
341         for (i = L_SUBFR16k >> 2; i != 0 ; i--)
342         {
343             *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
344             pt_tmp++;
345             *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
346             pt_tmp++;
347             *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
348             pt_tmp++;
349             *(pt_tmp) = mult_int16(*(pt_tmp), HF_corr_gain) << 1;
350             pt_tmp++;
351         }
352
353         /* HF gain */
354     }
355     else
356     {
357         pt_tmp = HF;
358         for (i = L_SUBFR16k >> 2; i != 0 ; i--)
359         {
360             *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
361             pt_tmp++;
362             *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
363             pt_tmp++;
364             *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
365             pt_tmp++;
366             *(pt_tmp) = mult_int16(*(pt_tmp), tmp);
367             pt_tmp++;
368         }
369     }
370
371
372     if ((nb_bits <= NBBITS_7k) && (newDTXState == SPEECH))
373     {
374         isf_extrapolation(HfIsf);
375         Isp_Az(HfIsf, HfA, M16k, 0);
376
377         weight_amrwb_lpc(HfA, Ap, 29491, M16k);     /* fac=0.9 */
378
379         wb_syn_filt(Ap,
380                     M16k,
381                     HF,
382                     HF,
383                     L_SUBFR16k,
384                     st->mem_syn_hf,
385                     1,
386                     ScratchMem);
387     }
388     else
389     {
390         /* synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz */
391         weight_amrwb_lpc(Aq, Ap, 19661, M);         /* fac=0.6 */
392
393         wb_syn_filt(Ap,
394                     M,
395                     HF,
396                     HF,
397                     L_SUBFR16k,
398                     st->mem_syn_hf + (M16k - M),
399                     1,
400                     ScratchMem);
401     }
402
403     /* noise Band Pass filtering (1ms of delay) */
404     band_pass_6k_7k(HF,
405                     L_SUBFR16k,
406                     st->mem_hf,
407                     ScratchMem);
408
409
410     if (nb_bits >= NBBITS_24k)
411     {
412         /* Low Pass filtering (7 kHz) */
413         low_pass_filt_7k(HF,
414                          L_SUBFR16k,
415                          st->mem_hf3,
416                          ScratchMem);
417     }
418     /* add filtered HF noise to speech synthesis */
419
420     pt_synth = synth16k;
421     pt_HF = HF;
422
423     for (i = L_SUBFR16k >> 1; i != 0; i--)
424     {
425         *(pt_synth) = add_int16(*(pt_synth), *(pt_HF++)); /* check 16 bit saturation */
426         pt_synth++;
427         *(pt_synth) = add_int16(*(pt_synth), *(pt_HF++));
428         pt_synth++;
429     }
430
431 }
432