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: pvamrwbdecoder.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
39 int16 mode, input : used mode
40 int16 prms[], input : parameter vector
41 int16 synth16k[], output: synthesis speech
42 int16 * frame_length, output: lenght of the frame
43 void *spd_state, i/o : State structure
44 int16 frame_type, input : received frame type
47 ------------------------------------------------------------------------------
50 Performs the main decoder routine AMR WB ACELP coding algorithm with 20 ms
51 speech frames for wideband speech signals.
54 ------------------------------------------------------------------------------
58 ------------------------------------------------------------------------------
61 ------------------------------------------------------------------------------
64 ------------------------------------------------------------------------------
68 /*----------------------------------------------------------------------------
70 ----------------------------------------------------------------------------*/
72 #include "pv_amr_wb_type_defs.h"
73 #include "pvamrwbdecoder_mem_funcs.h"
74 #include "pvamrwbdecoder_basic_op.h"
75 #include "pvamrwbdecoder_cnst.h"
76 #include "pvamrwbdecoder_acelp.h"
77 #include "e_pv_amrwbdec.h"
78 #include "get_amr_wb_bits.h"
79 #include "pvamrwb_math_op.h"
80 #include "pvamrwbdecoder_api.h"
81 #include "pvamrwbdecoder.h"
82 #include "synthesis_amr_wb.h"
85 /*----------------------------------------------------------------------------
87 ; Define module specific macros here
88 ----------------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------------
93 ; Include all pre-processor statements here. Include conditional
94 ; compile variables also.
95 ----------------------------------------------------------------------------*/
97 /*----------------------------------------------------------------------------
98 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
99 ; Variable declaration - defined here and used outside this module
100 ----------------------------------------------------------------------------*/
102 /* LPC interpolation coef {0.45, 0.8, 0.96, 1.0}; in Q15 */
103 static const int16 interpol_frac[NB_SUBFR] = {14746, 26214, 31457, 32767};
106 /* isp tables for initialization */
108 static const int16 isp_init[M] =
110 32138, 30274, 27246, 23170, 18205, 12540, 6393, 0,
111 -6393, -12540, -18205, -23170, -27246, -30274, -32138, 1475
114 static const int16 isf_init[M] =
116 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192,
117 9216, 10240, 11264, 12288, 13312, 14336, 15360, 3840
120 /*----------------------------------------------------------------------------
121 ; EXTERNAL FUNCTION REFERENCES
122 ; Declare functions defined elsewhere and referenced in this module
123 ----------------------------------------------------------------------------*/
125 /*----------------------------------------------------------------------------
126 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
127 ; Declare variables used in this module but defined elsewhere
128 ----------------------------------------------------------------------------*/
130 /*----------------------------------------------------------------------------
132 ----------------------------------------------------------------------------*/
134 /*----------------------------------------------------------------------------
135 FUNCTION DESCRIPTION pvDecoder_AmrWb_Init
137 Initialization of variables for the decoder section.
139 ----------------------------------------------------------------------------*/
144 void pvDecoder_AmrWb_Init(void **spd_state, void *pt_st, int16 **ScratchMem)
147 Decoder_State *st = &(((PV_AmrWbDec *)pt_st)->state);
149 *ScratchMem = ((PV_AmrWbDec *)pt_st)->ScratchMem;
153 dtx_dec_amr_wb_reset(&(st->dtx_decSt), isf_init);
155 pvDecoder_AmrWb_Reset((void *) st, 1);
157 *spd_state = (void *) st;
162 /*----------------------------------------------------------------------------
164 ----------------------------------------------------------------------------*/
166 void pvDecoder_AmrWb_Reset(void *st, int16 reset_all)
170 Decoder_State *dec_state;
172 dec_state = (Decoder_State *) st;
174 pv_memset((void *)dec_state->old_exc,
176 (PIT_MAX + L_INTERPOL)*sizeof(*dec_state->old_exc));
178 pv_memset((void *)dec_state->past_isfq,
180 M*sizeof(*dec_state->past_isfq));
183 dec_state->old_T0_frac = 0; /* old pitch value = 64.0 */
184 dec_state->old_T0 = 64;
185 dec_state->first_frame = 1;
186 dec_state->L_gc_thres = 0;
187 dec_state->tilt_code = 0;
189 pv_memset((void *)dec_state->disp_mem,
191 8*sizeof(*dec_state->disp_mem));
194 /* scaling memories for excitation */
195 dec_state->Q_old = Q_MAX;
196 dec_state->Qsubfr[3] = Q_MAX;
197 dec_state->Qsubfr[2] = Q_MAX;
198 dec_state->Qsubfr[1] = Q_MAX;
199 dec_state->Qsubfr[0] = Q_MAX;
203 /* routines initialization */
205 dec_gain2_amr_wb_init(dec_state->dec_gain);
206 oversamp_12k8_to_16k_init(dec_state->mem_oversamp);
207 band_pass_6k_7k_init(dec_state->mem_hf);
208 low_pass_filt_7k_init(dec_state->mem_hf3);
209 highpass_50Hz_at_12k8_init(dec_state->mem_sig_out);
210 highpass_400Hz_at_12k8_init(dec_state->mem_hp400);
211 Init_Lagconc(dec_state->lag_hist);
213 /* isp initialization */
215 pv_memcpy((void *)dec_state->ispold, (void *)isp_init, M*sizeof(*isp_init));
217 pv_memcpy((void *)dec_state->isfold, (void *)isf_init, M*sizeof(*isf_init));
218 for (i = 0; i < L_MEANBUF; i++)
220 pv_memcpy((void *)&dec_state->isf_buf[i * M],
222 M*sizeof(*isf_init));
224 /* variable initialization */
226 dec_state->mem_deemph = 0;
228 dec_state->seed = 21845; /* init random with 21845 */
229 dec_state->seed2 = 21845;
230 dec_state->seed3 = 21845;
232 dec_state->state = 0;
233 dec_state->prev_bfi = 0;
235 /* Static vectors to zero */
237 pv_memset((void *)dec_state->mem_syn_hf,
239 M16k*sizeof(*dec_state->mem_syn_hf));
241 pv_memset((void *)dec_state->mem_syn_hi,
243 M*sizeof(*dec_state->mem_syn_hi));
245 pv_memset((void *)dec_state->mem_syn_lo,
247 M*sizeof(*dec_state->mem_syn_lo));
250 dtx_dec_amr_wb_reset(&(dec_state->dtx_decSt), isf_init);
251 dec_state->vad_hist = 0;
257 /*----------------------------------------------------------------------------
259 ----------------------------------------------------------------------------*/
261 int32 pvDecoder_AmrWbMemRequirements()
263 return(sizeof(PV_AmrWbDec));
267 /*----------------------------------------------------------------------------
269 ----------------------------------------------------------------------------*/
271 /* Main decoder routine. */
273 int32 pvDecoder_AmrWb(
274 int16 mode, /* input : used mode */
275 int16 prms[], /* input : parameter vector */
276 int16 synth16k[], /* output: synthesis speech */
277 int16 * frame_length, /* output: lenght of the frame */
278 void *spd_state, /* i/o : State structure */
279 int16 frame_type, /* input : received frame type */
287 int16 *ScratchMem2 = &ScratchMem[ L_SUBFR + L_SUBFR16k + ((L_SUBFR + M + M16k +1)<<1)];
290 /* Excitation vector */
293 int16 *old_exc = ScratchMem2;
295 int16 *Aq = &old_exc[(L_FRAME + 1) + PIT_MAX + L_INTERPOL];/* A(z) quantized for the 4 subframes */
297 int16 *ispnew = &Aq[NB_SUBFR * (M + 1)];/* immittance spectral pairs at 4nd sfr */
298 int16 *isf = &ispnew[M]; /* ISF (frequency domain) at 4nd sfr */
299 int16 *isf_tmp = &isf[M];
300 int16 *code = &isf_tmp[M]; /* algebraic codevector */
301 int16 *excp = &code[L_SUBFR];
302 int16 *exc2 = &excp[L_SUBFR]; /* excitation vector */
303 int16 *HfIsf = &exc2[L_FRAME];
308 /* LPC coefficients */
310 int16 *p_Aq; /* ptr to A(z) for the 4 subframes */
314 int16 fac, stab_fac, voice_fac, Q_new = 0;
315 int32 L_tmp, L_gain_code;
319 int16 i, j, i_subfr, index, ind[8], tmp;
321 int16 T0, T0_frac, pit_flag, T0_max, select, T0_min = 0;
322 int16 gain_pit, gain_code;
323 int16 newDTXState, bfi, unusable_frame, nb_bits;
329 st = (Decoder_State *) spd_state;
331 /* mode verification */
333 nb_bits = AMR_WB_COMPRESSED[mode];
335 *frame_length = AMR_WB_PCM_FRAME;
337 /* find the new DTX state SPEECH OR DTX */
338 newDTXState = rx_amr_wb_dtx_handler(&(st->dtx_decSt), frame_type);
341 if (newDTXState != SPEECH)
343 dtx_dec_amr_wb(&(st->dtx_decSt), exc2, newDTXState, isf, &prms);
345 /* SPEECH action state machine */
347 if ((frame_type == RX_SPEECH_BAD) ||
348 (frame_type == RX_SPEECH_PROBABLY_DEGRADED))
350 /* bfi for all index, bits are not usable */
354 else if ((frame_type == RX_NO_DATA) ||
355 (frame_type == RX_SPEECH_LOST))
357 /* bfi only for lsf, gains and pitch period */
381 /* If this frame is the first speech frame after CNI period,
382 * set the BFH state machine to an appropriate state depending
383 * on whether there was DTX muting before start of speech or not
384 * If there was DTX muting, the first speech frame is muted.
385 * If there was no DTX muting, the first speech frame is not
386 * muted. The BFH state machine starts from state 5, however, to
387 * keep the audible noise resulting from a SID frame which is
388 * erroneously interpreted as a good speech frame as small as
389 * possible (the decoder output in this case is quickly muted)
392 if (st->dtx_decSt.dtxGlobalState == DTX)
397 else if (st->dtx_decSt.dtxGlobalState == DTX_MUTE)
403 if (newDTXState == SPEECH)
405 vad_flag = Serial_parm_1bit(&prms);
411 st->vad_hist = add_int16(st->vad_hist, 1);
423 if (newDTXState != SPEECH) /* CNG mode */
425 /* increase slightly energy of noise below 200 Hz */
427 /* Convert ISFs to the cosine domain */
428 Isf_isp(isf, ispnew, M);
430 Isp_Az(ispnew, Aq, M, 1);
432 pv_memcpy((void *)isf_tmp, (void *)st->isfold, M*sizeof(*isf_tmp));
435 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
439 for (i = 0; i < M; i++)
441 L_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, interpol_frac[j]));
442 L_tmp = mac_16by16_to_int32(L_tmp, isf[i], interpol_frac[j]);
443 HfIsf[i] = amr_wb_round(L_tmp);
449 &synth16k[i_subfr *5/4],
459 /* reset speech coder memories */
460 pvDecoder_AmrWb_Reset(st, 0);
462 pv_memcpy((void *)st->isfold, (void *)isf, M*sizeof(*isf));
465 st->dtx_decSt.dtxGlobalState = newDTXState;
473 /* copy coder memory state into working space (internal memory for DSP) */
475 pv_memcpy((void *)old_exc, (void *)st->old_exc, (PIT_MAX + L_INTERPOL)*sizeof(*old_exc));
477 exc = old_exc + PIT_MAX + L_INTERPOL;
479 /* Decode the ISFs */
481 if (nb_bits > NBBITS_7k) /* all rates but 6.6 Kbps */
483 ind[0] = Serial_parm(8, &prms); /* index of 1st ISP subvector */
484 ind[1] = Serial_parm(8, &prms); /* index of 2nd ISP subvector */
485 ind[2] = Serial_parm(6, &prms); /* index of 3rd ISP subvector */
486 ind[3] = Serial_parm(7, &prms); /* index of 4th ISP subvector */
487 ind[4] = Serial_parm(7, &prms); /* index of 5th ISP subvector */
488 ind[5] = Serial_parm(5, &prms); /* index of 6th ISP subvector */
489 ind[6] = Serial_parm(5, &prms); /* index of 7th ISP subvector */
491 Dpisf_2s_46b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
495 ind[0] = Serial_parm(8, &prms);
496 ind[1] = Serial_parm(8, &prms);
497 ind[2] = Serial_parm(14, &prms);
498 ind[3] = ind[2] & 0x007F;
500 ind[4] = Serial_parm(6, &prms);
502 Dpisf_2s_36b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
505 /* Convert ISFs to the cosine domain */
507 Isf_isp(isf, ispnew, M);
509 if (st->first_frame != 0)
512 pv_memcpy((void *)st->ispold, (void *)ispnew, M*sizeof(*ispnew));
515 /* Find the interpolated ISPs and convert to a[] for all subframes */
516 interpolate_isp(st->ispold, ispnew, interpol_frac, Aq);
518 /* update ispold[] for the next frame */
519 pv_memcpy((void *)st->ispold, (void *)ispnew, M*sizeof(*ispnew));
521 /* Check stability on isf : distance between old isf and current isf */
524 for (i = 0; i < M - 1; i++)
526 tmp = sub_int16(isf[i], st->isfold[i]);
527 L_tmp = mac_16by16_to_int32(L_tmp, tmp, tmp);
529 tmp = extract_h(shl_int32(L_tmp, 8));
530 tmp = mult_int16(tmp, 26214); /* tmp = L_tmp*0.8/256 */
532 tmp = 20480 - tmp; /* 1.25 - tmp */
533 stab_fac = shl_int16(tmp, 1); /* Q14 -> Q15 with saturation */
539 pv_memcpy((void *)isf_tmp, (void *)st->isfold, M*sizeof(*isf_tmp));
541 pv_memcpy((void *)st->isfold, (void *)isf, M*sizeof(*isf));
544 * Loop for every subframe in the analysis frame
546 * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR
548 * - decode the pitch delay and filter mode
549 * - decode algebraic code
550 * - decode pitch and codebook gains
551 * - find voicing factor and tilt of code for next subframe.
552 * - find the excitation and compute synthesis speech
555 p_Aq = Aq; /* pointer to interpolated LPC parameters */
559 * Sub process next 3 subframes
563 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
568 if ((i_subfr == 2*L_SUBFR) && (nb_bits > NBBITS_7k))
570 pit_flag = 0; /* set to 0 for 3rd subframe, <=> is not 6.6 kbps */
572 /*-------------------------------------------------*
573 * - Decode pitch lag *
574 * Lag indeces received also in case of BFI, *
575 * so that the parameter pointer stays in sync. *
576 *-------------------------------------------------*/
581 if (nb_bits <= NBBITS_9k)
583 index = Serial_parm(8, &prms);
585 if (index < (PIT_FR1_8b - PIT_MIN) * 2)
587 T0 = PIT_MIN + (index >> 1);
588 T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_MIN), 1));
589 T0_frac = shl_int16(T0_frac, 1);
593 T0 = add_int16(index, PIT_FR1_8b - ((PIT_FR1_8b - PIT_MIN) * 2));
599 index = Serial_parm(9, &prms);
601 if (index < (PIT_FR2 - PIT_MIN) * 4)
603 T0 = PIT_MIN + (index >> 2);
604 T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_MIN), 2));
606 else if (index < (((PIT_FR2 - PIT_MIN) << 2) + ((PIT_FR1_9b - PIT_FR2) << 1)))
608 index -= (PIT_FR2 - PIT_MIN) << 2;
609 T0 = PIT_FR2 + (index >> 1);
610 T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_FR2), 1));
611 T0_frac = shl_int16(T0_frac, 1);
615 T0 = add_int16(index, (PIT_FR1_9b - ((PIT_FR2 - PIT_MIN) * 4) - ((PIT_FR1_9b - PIT_FR2) * 2)));
620 /* find T0_min and T0_max for subframe 2 and 4 */
624 if (T0_min < PIT_MIN)
628 T0_max = T0_min + 15;
630 if (T0_max > PIT_MAX)
633 T0_min = PIT_MAX - 15;
637 { /* if subframe 2 or 4 */
639 if (nb_bits <= NBBITS_9k)
641 index = Serial_parm(5, &prms);
643 T0 = T0_min + (index >> 1);
644 T0_frac = sub_int16(index, shl_int16(T0 - T0_min, 1));
645 T0_frac = shl_int16(T0_frac, 1);
649 index = Serial_parm(6, &prms);
651 T0 = T0_min + (index >> 2);
652 T0_frac = sub_int16(index, shl_int16(T0 - T0_min, 2));
656 /* check BFI after pitch lag decoding */
658 if (bfi != 0) /* if frame erasure */
660 lagconceal(&(st->dec_gain[17]), st->lag_hist, &T0, &(st->old_T0), &(st->seed3), unusable_frame);
664 * Find the pitch gain, the interpolation filter
665 * and the adaptive codebook vector.
668 Pred_lt4(&exc[i_subfr], T0, T0_frac, L_SUBFR + 1);
678 if (nb_bits <= NBBITS_9k)
684 select = Serial_parm_1bit(&prms);
691 /* find pitch excitation with lp filter */
692 for (i = 0; i < L_SUBFR; i++)
694 L_tmp = ((int32) exc[i-1+i_subfr] + exc[i+1+i_subfr]);
696 L_tmp += ((int32) exc[i+i_subfr] * 20972);
698 code[i] = amr_wb_round(L_tmp << 1);
700 pv_memcpy((void *)&exc[i_subfr], (void *)code, L_SUBFR*sizeof(*code));
704 * Decode innovative codebook.
705 * Add the fixed-gain pitch contribution to code[].
708 if (unusable_frame != 0)
710 /* the innovative code doesn't need to be scaled (see Q_gain2) */
711 for (i = 0; i < L_SUBFR; i++)
713 code[i] = noise_gen_amrwb(&(st->seed)) >> 3;
716 else if (nb_bits <= NBBITS_7k)
718 ind[0] = Serial_parm(12, &prms);
719 dec_acelp_2p_in_64(ind[0], code);
721 else if (nb_bits <= NBBITS_9k)
723 for (i = 0; i < 4; i++)
725 ind[i] = Serial_parm(5, &prms);
727 dec_acelp_4p_in_64(ind, 20, code);
729 else if (nb_bits <= NBBITS_12k)
731 for (i = 0; i < 4; i++)
733 ind[i] = Serial_parm(9, &prms);
735 dec_acelp_4p_in_64(ind, 36, code);
737 else if (nb_bits <= NBBITS_14k)
739 ind[0] = Serial_parm(13, &prms);
740 ind[1] = Serial_parm(13, &prms);
741 ind[2] = Serial_parm(9, &prms);
742 ind[3] = Serial_parm(9, &prms);
743 dec_acelp_4p_in_64(ind, 44, code);
745 else if (nb_bits <= NBBITS_16k)
747 for (i = 0; i < 4; i++)
749 ind[i] = Serial_parm(13, &prms);
751 dec_acelp_4p_in_64(ind, 52, code);
753 else if (nb_bits <= NBBITS_18k)
755 for (i = 0; i < 4; i++)
757 ind[i] = Serial_parm(2, &prms);
759 for (i = 4; i < 8; i++)
761 ind[i] = Serial_parm(14, &prms);
763 dec_acelp_4p_in_64(ind, 64, code);
765 else if (nb_bits <= NBBITS_20k)
767 ind[0] = Serial_parm(10, &prms);
768 ind[1] = Serial_parm(10, &prms);
769 ind[2] = Serial_parm(2, &prms);
770 ind[3] = Serial_parm(2, &prms);
771 ind[4] = Serial_parm(10, &prms);
772 ind[5] = Serial_parm(10, &prms);
773 ind[6] = Serial_parm(14, &prms);
774 ind[7] = Serial_parm(14, &prms);
775 dec_acelp_4p_in_64(ind, 72, code);
779 for (i = 0; i < 8; i++)
781 ind[i] = Serial_parm(11, &prms);
784 dec_acelp_4p_in_64(ind, 88, code);
787 preemph_amrwb_dec(code, st->tilt_code, L_SUBFR);
795 Pit_shrp(code, tmp, PIT_SHARP, L_SUBFR);
798 * Decode codebooks gains.
801 if (nb_bits <= NBBITS_9k)
803 index = Serial_parm(6, &prms); /* codebook gain index */
805 dec_gain2_amr_wb(index,
820 index = Serial_parm(7, &prms); /* codebook gain index */
822 dec_gain2_amr_wb(index,
836 /* find best scaling to perform on excitation (Q_new) */
839 for (i = 1; i < 4; i++)
841 if (st->Qsubfr[i] < tmp)
847 /* limit scaling (Q_new) to Q_MAX: see pv_amr_wb_cnst.h and syn_filt_32() */
854 L_tmp = L_gain_code; /* L_gain_code in Q16 */
857 while ((L_tmp < 0x08000000L) && (Q_new < tmp))
863 gain_code = amr_wb_round(L_tmp); /* scaled gain_code with Qnew */
865 scale_signal(exc + i_subfr - (PIT_MAX + L_INTERPOL),
866 PIT_MAX + L_INTERPOL + L_SUBFR,
867 (int16)(Q_new - st->Q_old));
873 * Update parameters for the next subframe.
874 * - tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
880 /* LTP-Lag history update */
881 for (i = 4; i > 0; i--)
883 st->lag_hist[i] = st->lag_hist[i - 1];
885 st->lag_hist[0] = T0;
888 st->old_T0_frac = 0; /* Remove fraction in case of BFI */
890 /* find voice factor in Q15 (1=voiced, -1=unvoiced) */
895 for (i = L_SUBFR - 1; i >= 0; i--)
897 exc2[i] = (exc[i_subfr + i] + (0x0004 * (exc[i_subfr + i] != MAX_16))) >> 3;
901 /* post processing of excitation elements */
903 if (nb_bits <= NBBITS_9k)
905 pit_sharp = shl_int16(gain_pit, 1);
907 if (pit_sharp > 16384)
909 for (i = 0; i < L_SUBFR; i++)
911 tmp = mult_int16(exc2[i], pit_sharp);
912 L_tmp = mul_16by16_to_int32(tmp, gain_pit);
914 excp[i] = amr_wb_round(L_tmp);
923 voice_fac = voice_factor(exc2, -3, gain_pit, code, gain_code, L_SUBFR);
925 /* tilt of code for next subframe: 0.5=voiced, 0=unvoiced */
927 st->tilt_code = (voice_fac >> 2) + 8192;
930 * - Find the total excitation.
931 * - Find synthesis speech corresponding to exc[].
932 * - Find maximum value of excitation for next scaling
935 pv_memcpy((void *)exc2, (void *)&exc[i_subfr], L_SUBFR*sizeof(*exc2));
938 for (i = 0; i < L_SUBFR; i++)
940 L_tmp = mul_16by16_to_int32(code[i], gain_code);
941 L_tmp = shl_int32(L_tmp, 5);
942 L_tmp = mac_16by16_to_int32(L_tmp, exc[i + i_subfr], gain_pit);
943 L_tmp = shl_int32(L_tmp, 1);
944 tmp = amr_wb_round(L_tmp);
945 exc[i + i_subfr] = tmp;
946 tmp = tmp - (tmp < 0);
947 max |= tmp ^(tmp >> 15); /* |= tmp ^sign(tmp) */
951 /* tmp = scaling possible according to max value of excitation */
952 tmp = add_int16(norm_s(max), Q_new) - 1;
954 st->Qsubfr[3] = st->Qsubfr[2];
955 st->Qsubfr[2] = st->Qsubfr[1];
956 st->Qsubfr[1] = st->Qsubfr[0];
960 * phase dispersion to enhance noise in low bit rate
964 if (nb_bits <= NBBITS_7k)
966 j = 0; /* high dispersion for rate <= 7.5 kbit/s */
968 else if (nb_bits <= NBBITS_9k)
970 j = 1; /* low dispersion for rate <= 9.6 kbit/s */
974 j = 2; /* no dispersion for rate > 9.6 kbit/s */
977 /* L_gain_code in Q16 */
979 phase_dispersion((int16)(L_gain_code >> 16),
988 * - Enhance excitation on noise. (modify gain of code)
989 * If signal is noisy and LPC filter is stable, move gain
990 * of code 1.5 dB toward gain of code threshold.
991 * This decrease by 3 dB noise energy variation.
994 tmp = 16384 - (voice_fac >> 1); /* 1=unvoiced, 0=voiced */
995 fac = mult_int16(stab_fac, tmp);
999 if (L_tmp < st->L_gc_thres)
1001 L_tmp += fxp_mul32_by_16b(L_gain_code, 6226) << 1;
1003 if (L_tmp > st->L_gc_thres)
1005 L_tmp = st->L_gc_thres;
1010 L_tmp = fxp_mul32_by_16b(L_gain_code, 27536) << 1;
1012 if (L_tmp < st->L_gc_thres)
1014 L_tmp = st->L_gc_thres;
1017 st->L_gc_thres = L_tmp;
1019 L_gain_code = fxp_mul32_by_16b(L_gain_code, (32767 - fac)) << 1;
1022 L_gain_code = add_int32(L_gain_code, fxp_mul32_by_16b(L_tmp, fac) << 1);
1026 * - Enhance excitation on voice. (HP filtering of code)
1027 * On voiced signal, filtering of code by a smooth fir HP
1028 * filter to decrease energy of code in low frequency.
1031 tmp = (voice_fac >> 3) + 4096;/* 0.25=voiced, 0=unvoiced */
1033 /* build excitation */
1035 gain_code = amr_wb_round(shl_int32(L_gain_code, Q_new));
1037 L_tmp = (int32)(code[0] << 16);
1038 L_tmp = msu_16by16_from_int32(L_tmp, code[1], tmp);
1039 L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1040 L_tmp = shl_int32(L_tmp, 5);
1041 L_tmp = mac_16by16_to_int32(L_tmp, exc2[0], gain_pit);
1042 L_tmp = shl_int32(L_tmp, 1); /* saturation can occur here */
1043 exc2[0] = amr_wb_round(L_tmp);
1046 for (i = 1; i < L_SUBFR - 1; i++)
1048 L_tmp = (int32)(code[i] << 16);
1049 L_tmp = msu_16by16_from_int32(L_tmp, (code[i + 1] + code[i - 1]), tmp);
1050 L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1051 L_tmp = shl_int32(L_tmp, 5);
1052 L_tmp = mac_16by16_to_int32(L_tmp, exc2[i], gain_pit);
1053 L_tmp = shl_int32(L_tmp, 1); /* saturation can occur here */
1054 exc2[i] = amr_wb_round(L_tmp);
1057 L_tmp = (int32)(code[L_SUBFR - 1] << 16);
1058 L_tmp = msu_16by16_from_int32(L_tmp, code[L_SUBFR - 2], tmp);
1059 L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1060 L_tmp = shl_int32(L_tmp, 5);
1061 L_tmp = mac_16by16_to_int32(L_tmp, exc2[L_SUBFR - 1], gain_pit);
1062 L_tmp = shl_int32(L_tmp, 1); /* saturation can occur here */
1063 exc2[L_SUBFR - 1] = amr_wb_round(L_tmp);
1067 if (nb_bits <= NBBITS_9k)
1069 if (pit_sharp > 16384)
1071 for (i = 0; i < L_SUBFR; i++)
1073 excp[i] = add_int16(excp[i], exc2[i]);
1075 agc2_amr_wb(exc2, excp, L_SUBFR);
1076 pv_memcpy((void *)exc2, (void *)excp, L_SUBFR*sizeof(*exc2));
1080 if (nb_bits <= NBBITS_7k)
1083 for (i = 0; i < M; i++)
1085 L_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, interpol_frac[j]));
1086 L_tmp = mac_16by16_to_int32(L_tmp, isf[i], interpol_frac[j]);
1087 HfIsf[i] = amr_wb_round(L_tmp);
1092 pv_memset((void *)st->mem_syn_hf,
1094 (M16k - M)*sizeof(*st->mem_syn_hf));
1097 if (nb_bits >= NBBITS_24k)
1099 corr_gain = Serial_parm(4, &prms);
1106 synthesis_amr_wb(p_Aq,
1109 &synth16k[i_subfr + (i_subfr>>2)],
1118 p_Aq += (M + 1); /* interpolated LPC parameters for next subframe */
1122 * Update signal for next frame.
1123 * -> save past of exc[]
1124 * -> save pitch parameters
1127 pv_memcpy((void *)st->old_exc,
1128 (void *)&old_exc[L_FRAME],
1129 (PIT_MAX + L_INTERPOL)*sizeof(*old_exc));
1131 scale_signal(exc, L_FRAME, (int16)(-Q_new));
1133 dtx_dec_amr_wb_activity_update(&(st->dtx_decSt), isf, exc);
1135 st->dtx_decSt.dtxGlobalState = newDTXState;