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 (AMR) speech codec
23 Available from http://www.3gpp.org
25 (C) 2004, 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 ------------------------------------------------------------------------------
33 Funtions: Decoder_amr_init
37 ------------------------------------------------------------------------------
40 This file contains the function used to decode one speech frame using a given
41 codec mode. The functions used to initialize, reset, and exit are also
42 included in this file.
44 ------------------------------------------------------------------------------
47 /*----------------------------------------------------------------------------
49 ----------------------------------------------------------------------------*/
85 /*----------------------------------------------------------------------------
87 ; Define module specific macros here
88 ----------------------------------------------------------------------------*/
90 /*----------------------------------------------------------------------------
92 ; Include all pre-processor statements here. Include conditional
93 ; compile variables also.
94 ----------------------------------------------------------------------------*/
96 /*----------------------------------------------------------------------------
97 ; LOCAL FUNCTION DEFINITIONS
98 ; Function Prototype declaration
99 ----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------
102 ; LOCAL VARIABLE DEFINITIONS
103 ; Variable declaration - defined here and used outside this module
104 ----------------------------------------------------------------------------*/
108 ------------------------------------------------------------------------------
109 FUNCTION NAME: Decoder_amr_init
110 ------------------------------------------------------------------------------
111 INPUT AND OUTPUT DEFINITIONS
114 state = pointer to a pointer to structures of type Decoder_amrState
117 structure pointed to by the pointer which is pointed to by state is
118 initialized to each field's initial values
120 state pointer points to the address of the memory allocated by
121 Decoder_amr_init function
124 return_value = 0, if the initialization was successful; -1, otherwise (int)
126 Global Variables Used:
129 Local Variables Needed:
132 ------------------------------------------------------------------------------
135 This function allocates and initializes state memory used by the Decoder_amr
136 function. It stores the pointer to the filter status structure in state. This
137 pointer has to be passed to Decoder_amr in each call. The function returns
138 0, if initialization was successful and -1, otherwise.
140 ------------------------------------------------------------------------------
145 ------------------------------------------------------------------------------
148 dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
150 ------------------------------------------------------------------------------
153 int Decoder_amr_init (Decoder_amrState **state)
158 if (state == (Decoder_amrState **) NULL){
159 fprintf(stderr, "Decoder_amr_init: invalid parameter\n");
165 if ((s= (Decoder_amrState *) malloc(sizeof(Decoder_amrState))) == NULL){
166 fprintf(stderr, "Decoder_amr_init: can not malloc state structure\n");
171 s->inBackgroundNoise = 0;
172 s->voicedHangover = 0;
173 for (i = 0; i < 9; i++)
174 s->ltpGainHistory[i] = 0;
177 s->ec_gain_p_st = NULL;
178 s->ec_gain_c_st = NULL;
179 s->pred_state = NULL;
180 s->ph_disp_st = NULL;
181 s->dtxDecoderState = NULL;
183 if (D_plsf_init(&s->lsfState) ||
184 ec_gain_pitch_init(&s->ec_gain_p_st) ||
185 ec_gain_code_init(&s->ec_gain_c_st) ||
186 gc_pred_init(&s->pred_state) ||
187 Cb_gain_average_init(&s->Cb_gain_averState) ||
188 lsp_avg_init(&s->lsp_avg_st) ||
189 Bgn_scd_init(&s->background_state) ||
190 ph_disp_init(&s->ph_disp_st) ||
191 dtx_dec_init(&s->dtxDecoderState)) {
192 Decoder_amr_exit(&s);
196 Decoder_amr_reset(s, (enum Mode)0);
202 ------------------------------------------------------------------------------
204 [State any special notes, constraints or cautions for users of this function]
206 ------------------------------------------------------------------------------
209 Word16 Decoder_amr_init(Decoder_amrState *s)
213 if (s == (Decoder_amrState *) NULL)
215 /* fprint(stderr, "Decoder_amr_init: invalid parameter\n"); */
219 get_const_tbls(&s->common_amr_tbls);
222 s->inBackgroundNoise = 0;
223 s->voicedHangover = 0;
225 /* Initialize overflow Flag */
229 for (i = 0; i < LTP_GAIN_HISTORY_LEN; i++)
231 s->ltpGainHistory[i] = 0;
234 D_plsf_reset(&s->lsfState, s->common_amr_tbls.mean_lsf_5_ptr);
235 ec_gain_pitch_reset(&s->ec_gain_p_st);
236 ec_gain_code_reset(&s->ec_gain_c_st);
237 Cb_gain_average_reset(&s->Cb_gain_averState);
238 lsp_avg_reset(&s->lsp_avg_st, s->common_amr_tbls.mean_lsf_5_ptr);
239 Bgn_scd_reset(&s->background_state);
240 ph_disp_reset(&s->ph_disp_st);
241 dtx_dec_reset(&s->dtxDecoderState);
242 gc_pred_reset(&s->pred_state);
244 Decoder_amr_reset(s, MR475);
249 /****************************************************************************/
252 ------------------------------------------------------------------------------
253 FUNCTION NAME: Decoder_amr_reset
254 ------------------------------------------------------------------------------
255 INPUT AND OUTPUT DEFINITIONS
258 state = pointer to a structure of type Decoder_amrState
259 mode = codec mode (enum Mode)
262 structure pointed to by state is initialized to its reset value
265 return_value = 0, if reset was successful; -1, otherwise (int)
267 Global Variables Used:
270 Local Variables Needed:
273 ------------------------------------------------------------------------------
276 This function resets the state memory used by the Decoder_amr function. It
277 returns a 0, if reset was successful and -1, otherwise.
279 ------------------------------------------------------------------------------
284 ------------------------------------------------------------------------------
287 dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
289 ------------------------------------------------------------------------------
292 int Decoder_amr_reset (Decoder_amrState *state, enum Mode mode)
296 if (state == (Decoder_amrState *) NULL){
297 fprintf(stderr, "Decoder_amr_reset: invalid parameter\n");
301 // Initialize static pointer
302 state->exc = state->old_exc + PIT_MAX + L_INTERPOL;
304 // Static vectors to zero
305 Set_zero (state->old_exc, PIT_MAX + L_INTERPOL);
308 Set_zero (state->mem_syn, M);
310 // initialize pitch sharpening
311 state->sharp = SHARPMIN;
314 // Initialize state->lsp_old []
317 Copy(lsp_init_data, &state->lsp_old[0], M);
320 // Initialize memories of bad frame handling
325 state->T0_lagBuff = 40;
326 state->inBackgroundNoise = 0;
327 state->voicedHangover = 0;
330 state->excEnergyHist[i] = 0;
333 for (i = 0; i < 9; i++)
334 state->ltpGainHistory[i] = 0;
336 Cb_gain_average_reset(state->Cb_gain_averState);
338 lsp_avg_reset(state->lsp_avg_st);
339 D_plsf_reset(state->lsfState);
340 ec_gain_pitch_reset(state->ec_gain_p_st);
341 ec_gain_code_reset(state->ec_gain_c_st);
344 gc_pred_reset(state->pred_state);
346 Bgn_scd_reset(state->background_state);
347 state->nodataSeed = 21845;
348 ph_disp_reset(state->ph_disp_st);
350 dtx_dec_reset(state->dtxDecoderState);
355 ------------------------------------------------------------------------------
357 [State any special notes, constraints or cautions for users of this function]
359 ------------------------------------------------------------------------------
362 Word16 Decoder_amr_reset(Decoder_amrState *state, enum Mode mode)
366 if (state == (Decoder_amrState *) NULL)
368 /* fprint(stderr, "Decoder_amr_reset: invalid parameter\n"); */
372 /* Initialize static pointer */
373 state->exc = state->old_exc + PIT_MAX + L_INTERPOL;
375 /* Static vectors to zero */
376 oscl_memset(state->old_exc, 0, sizeof(Word16)*(PIT_MAX + L_INTERPOL));
380 oscl_memset(state->mem_syn, 0, sizeof(Word16)*M);
382 /* initialize pitch sharpening */
383 state->sharp = SHARPMIN;
386 /* Initialize overflow Flag */
390 /* Initialize state->lsp_old [] */
394 state->lsp_old[0] = 30000;
395 state->lsp_old[1] = 26000;
396 state->lsp_old[2] = 21000;
397 state->lsp_old[3] = 15000;
398 state->lsp_old[4] = 8000;
399 state->lsp_old[5] = 0;
400 state->lsp_old[6] = -8000;
401 state->lsp_old[7] = -15000;
402 state->lsp_old[8] = -21000;
403 state->lsp_old[9] = -26000;
406 /* Initialize memories of bad frame handling */
411 state->T0_lagBuff = 40;
412 state->inBackgroundNoise = 0;
413 state->voicedHangover = 0;
416 for (i = 0; i < EXC_ENERGY_HIST_LEN; i++)
418 state->excEnergyHist[i] = 0;
422 for (i = 0; i < LTP_GAIN_HISTORY_LEN; i++)
424 state->ltpGainHistory[i] = 0;
427 Cb_gain_average_reset(&(state->Cb_gain_averState));
430 lsp_avg_reset(&(state->lsp_avg_st), state->common_amr_tbls.mean_lsf_5_ptr);
432 D_plsf_reset(&(state->lsfState), state->common_amr_tbls.mean_lsf_5_ptr);
433 ec_gain_pitch_reset(&(state->ec_gain_p_st));
434 ec_gain_code_reset(&(state->ec_gain_c_st));
438 gc_pred_reset(&(state->pred_state));
441 Bgn_scd_reset(&(state->background_state));
442 state->nodataSeed = 21845;
443 ph_disp_reset(&(state->ph_disp_st));
446 dtx_dec_reset(&(state->dtxDecoderState));
452 /****************************************************************************/
455 ------------------------------------------------------------------------------
456 FUNCTION NAME: Decoder_amr
457 ------------------------------------------------------------------------------
458 INPUT AND OUTPUT DEFINITIONS
461 st = pointer to a structure of type Decoder_amrState
462 mode = codec mode (enum Mode)
463 parm = buffer of synthesis parameters (Word16)
464 frame_type = received frame type (enum RXFrameType)
465 synth = buffer containing synthetic speech (Word16)
466 A_t = buffer containing decoded LP filter in 4 subframes (Word16)
469 structure pointed to by st contains the newly calculated decoder
471 synth buffer contains the decoded speech samples
472 A_t buffer contains the decoded LP filter parameters
475 return_value = 0 (int)
477 Global Variables Used:
480 Local Variables Needed:
483 ------------------------------------------------------------------------------
486 This function performs the decoding of one speech frame for a given codec
489 ------------------------------------------------------------------------------
494 ------------------------------------------------------------------------------
497 dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
499 ------------------------------------------------------------------------------
503 Decoder_amrState *st, // i/o : State variables
504 enum Mode mode, // i : AMR mode
505 Word16 parm[], // i : vector of synthesis parameters
507 enum RXFrameType frame_type, // i : received frame type
508 Word16 synth[], // o : synthesis speech (L_FRAME)
509 Word16 A_t[] // o : decoded LP filter in 4 subframes
515 Word16 *Az; // Pointer on A_t
527 // Algebraic codevector
529 Word16 code[L_SUBFR];
533 Word16 excp[L_SUBFR];
534 Word16 exc_enhanced[L_SUBFR];
539 Word16 T0, T0_frac, index, index_mr475 = 0;
540 Word16 gain_pit, gain_code, gain_code_mix, pit_sharp, pit_flag, pitch_fac;
541 Word16 t0_min, t0_max;
542 Word16 delta_frc_low, delta_frc_range;
550 Word16 evenSubfr = 0;
552 Word16 bfi = 0; // bad frame indication flag
553 Word16 pdfi = 0; // potential degraded bad frame flag
555 enum DTXStateType newDTXState; // SPEECH , DTX, DTX_MUTE
557 // find the new DTX state SPEECH OR DTX
558 newDTXState = rx_dtx_handler(st->dtxDecoderState, frame_type);
561 if (sub(newDTXState, SPEECH) != 0 )
563 Decoder_amr_reset (st, MRDTX);
565 dtx_dec(st->dtxDecoderState,
569 st->Cb_gain_averState,
573 // update average lsp
575 Lsf_lsp(st->lsfState->past_lsf_q, st->lsp_old, M);
576 lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
580 // SPEECH action state machine
581 if ((sub(frame_type, RX_SPEECH_BAD) == 0) ||
582 (sub(frame_type, RX_NO_DATA) == 0) ||
583 (sub(frame_type, RX_ONSET) == 0))
586 if ((sub(frame_type, RX_NO_DATA) == 0) ||
587 (sub(frame_type, RX_ONSET) == 0))
589 build_CN_param(&st->nodataSeed,
595 else if (sub(frame_type, RX_SPEECH_DEGRADED) == 0)
602 st->state = add (st->state, 1);
604 else if (sub (st->state, 6) == 0)
614 if (sub (st->state, 6) > 0)
619 // If this frame is the first speech frame after CNI period,
620 // set the BFH state machine to an appropriate state depending
621 // on whether there was DTX muting before start of speech or not
622 // If there was DTX muting, the first speech frame is muted.
623 // If there was no DTX muting, the first speech frame is not
624 // muted. The BFH state machine starts from state 5, however, to
625 // keep the audible noise resulting from a SID frame which is
626 // erroneously interpreted as a good speech frame as small as
627 // possible (the decoder output in this case is quickly muted)
629 if (sub(st->dtxDecoderState->dtxGlobalState, DTX) == 0)
634 else if (sub(st->dtxDecoderState->dtxGlobalState, DTX_MUTE) == 0)
640 // save old LSFs for CB gain smoothing
641 Copy (st->lsfState->past_lsf_q, prev_lsf, M);
643 // decode LSF parameters and generate interpolated lpc coefficients
645 if (sub (mode, MR122) != 0)
647 D_plsf_3(st->lsfState, mode, bfi, parm, lsp_new);
649 // Advance synthesis parameters pointer
652 Int_lpc_1to3(st->lsp_old, lsp_new, A_t);
656 D_plsf_5 (st->lsfState, bfi, parm, lsp_mid, lsp_new);
658 // Advance synthesis parameters pointer
661 Int_lpc_1and3 (st->lsp_old, lsp_mid, lsp_new, A_t);
664 // update the LSPs for the next frame
665 for (i = 0; i < M; i++)
667 st->lsp_old[i] = lsp_new[i];
670 *------------------------------------------------------------------------*
671 * Loop for every subframe in the analysis frame *
672 *------------------------------------------------------------------------*
673 * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR *
675 * - decode the pitch delay *
676 * - decode algebraic code *
677 * - decode pitch and codebook gains *
678 * - find the excitation and compute synthesis speech *
679 *------------------------------------------------------------------------*
681 // pointer to interpolated LPC parameters
686 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
688 subfrNr = add(subfrNr, 1);
689 evenSubfr = sub(1, evenSubfr);
691 // flag for first and 3th subframe
694 if (sub (i_subfr, L_FRAME_BY2) == 0)
696 if (sub(mode, MR475) != 0 && sub(mode, MR515) != 0)
705 *-------------------------------------------------------*
706 * - decode pitch lag and find adaptive codebook vector. *
707 *-------------------------------------------------------*
709 if (sub(mode, MR122) != 0)
711 // flag4 indicates encoding with 4 bit resolution;
712 // this is needed for mode MR475, MR515, MR59 and MR67
715 if ((sub (mode, MR475) == 0) ||
716 (sub (mode, MR515) == 0) ||
717 (sub (mode, MR59) == 0) ||
718 (sub (mode, MR67) == 0) ) {
722 *-------------------------------------------------------*
723 * - get ranges for the t0_min and t0_max *
724 * - only needed in delta decoding *
725 *-------------------------------------------------------*
730 if ( sub(mode, MR795) == 0 )
733 delta_frc_range = 19;
736 t0_min = sub(st->old_T0, delta_frc_low);
737 if (sub(t0_min, PIT_MIN) < 0)
741 t0_max = add(t0_min, delta_frc_range);
742 if (sub(t0_max, PIT_MAX) > 0)
745 t0_min = sub(t0_max, delta_frc_range);
748 Dec_lag3 (index, t0_min, t0_max, pit_flag, st->old_T0,
749 &T0, &T0_frac, flag4);
755 if (sub (st->old_T0, PIT_MAX) < 0)
757 st->old_T0 = add(st->old_T0, 1); // degradation
762 if ( st->inBackgroundNoise != 0 &&
763 sub(st->voicedHangover, 4) > 0 &&
764 ((sub(mode, MR475) == 0 ) ||
765 (sub(mode, MR515) == 0 ) ||
766 (sub(mode, MR59) == 0) )
773 Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 1);
777 Dec_lag6 (index, PIT_MIN_MR122,
778 PIT_MAX, pit_flag, &T0, &T0_frac);
780 if ( bfi == 0 && (pit_flag == 0 || sub (index, 61) < 0))
790 Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 0);
793 *-------------------------------------------------------*
794 * - (MR122 only: Decode pitch gain.) *
795 * - Decode innovative codebook. *
796 * - set pitch sharpening factor *
797 *-------------------------------------------------------*
799 if (sub (mode, MR475) == 0 || sub (mode, MR515) == 0)
801 index = *parm++; // index of position
802 i = *parm++; // signs
804 decode_2i40_9bits (subfrNr, i, index, code);
806 pit_sharp = shl (st->sharp, 1);
808 else if (sub (mode, MR59) == 0)
810 index = *parm++; // index of position
811 i = *parm++; // signs
813 decode_2i40_11bits (i, index, code);
815 pit_sharp = shl (st->sharp, 1);
817 else if (sub (mode, MR67) == 0)
819 index = *parm++; // index of position
820 i = *parm++; // signs
822 decode_3i40_14bits (i, index, code);
824 pit_sharp = shl (st->sharp, 1);
826 else if (sub (mode, MR795) <= 0)
828 index = *parm++; // index of position
829 i = *parm++; // signs
831 decode_4i40_17bits (i, index, code);
833 pit_sharp = shl (st->sharp, 1);
835 else if (sub (mode, MR102) == 0)
837 dec_8i40_31bits (parm, code);
840 pit_sharp = shl (st->sharp, 1);
847 ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
851 gain_pit = d_gain_pitch (mode, index);
853 ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
856 dec_10i40_35bits (parm, code);
859 // pit_sharp = gain_pit;
860 // if (pit_sharp > 1.0) pit_sharp = 1.0;
862 pit_sharp = shl (gain_pit, 1);
865 *-------------------------------------------------------*
866 * - Add the pitch contribution to code[]. *
867 *-------------------------------------------------------*
868 for (i = T0; i < L_SUBFR; i++)
870 temp = mult (code[i - T0], pit_sharp);
871 code[i] = add (code[i], temp);
874 *------------------------------------------------------------*
875 * - Decode codebook gain (MR122) or both pitch *
876 * gain and codebook gain (all others) *
877 * - Update pitch sharpening "sharp" with quantized gain_pit *
878 *------------------------------------------------------------*
880 if (sub (mode, MR475) == 0)
882 // read and decode pitch and code gain
885 index_mr475 = *parm++; // index of gain(s)
890 Dec_gain(st->pred_state, mode, index_mr475, code,
891 evenSubfr, &gain_pit, &gain_code);
895 ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
896 ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
899 ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
901 ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
904 pit_sharp = gain_pit;
905 if (sub (pit_sharp, SHARPMAX) > 0)
907 pit_sharp = SHARPMAX;
911 else if ((sub (mode, MR74) <= 0) ||
912 (sub (mode, MR102) == 0))
914 // read and decode pitch and code gain
915 index = *parm++; // index of gain(s)
919 Dec_gain(st->pred_state, mode, index, code,
920 evenSubfr, &gain_pit, &gain_code);
924 ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
925 ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
928 ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
930 ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
933 pit_sharp = gain_pit;
934 if (sub (pit_sharp, SHARPMAX) > 0)
936 pit_sharp = SHARPMAX;
939 if (sub (mode, MR102) == 0)
941 if (sub (st->old_T0, add(L_SUBFR, 5)) > 0)
943 pit_sharp = shr(pit_sharp, 2);
949 // read and decode pitch gain
950 index = *parm++; // index of gain(s)
952 if (sub (mode, MR795) == 0)
957 ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
961 gain_pit = d_gain_pitch (mode, index);
963 ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
966 // read and decode code gain
970 d_gain_code (st->pred_state, mode, index, code, &gain_code);
974 ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
977 ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
980 pit_sharp = gain_pit;
981 if (sub (pit_sharp, SHARPMAX) > 0)
983 pit_sharp = SHARPMAX;
990 d_gain_code (st->pred_state, mode, index, code, &gain_code);
994 ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
997 ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
1000 pit_sharp = gain_pit;
1004 // store pitch sharpening for next subframe
1005 // (for modes which use the previous pitch gain for
1006 // pitch sharpening in the search phase)
1007 // do not update sharpening in even subframes for MR475
1008 if (sub(mode, MR475) != 0 || evenSubfr == 0)
1010 st->sharp = gain_pit;
1011 if (sub (st->sharp, SHARPMAX) > 0)
1013 st->sharp = SHARPMAX;
1017 pit_sharp = shl (pit_sharp, 1);
1018 if (sub (pit_sharp, 16384) > 0)
1020 for (i = 0; i < L_SUBFR; i++)
1022 temp = mult (st->exc[i], pit_sharp);
1023 L_temp = L_mult (temp, gain_pit);
1024 if (sub(mode, MR122)==0)
1026 L_temp = L_shr (L_temp, 1);
1028 excp[i] = pv_round (L_temp);
1032 *-------------------------------------------------------*
1033 * - Store list of LTP gains needed in the source *
1034 * characteristic detector (SCD) *
1035 *-------------------------------------------------------*
1038 for (i = 0; i < 8; i++)
1040 st->ltpGainHistory[i] = st->ltpGainHistory[i+1];
1042 st->ltpGainHistory[8] = gain_pit;
1045 *-------------------------------------------------------*
1046 * - Limit gain_pit if in background noise and BFI *
1047 * for MR475, MR515, MR59 *
1048 *-------------------------------------------------------*
1050 if ( (st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 &&
1051 ((sub(mode, MR475) == 0) ||
1052 (sub(mode, MR515) == 0) ||
1053 (sub(mode, MR59) == 0))
1056 if ( sub (gain_pit, 12288) > 0) // if (gain_pit > 0.75) in Q14
1057 gain_pit = add( shr( sub(gain_pit, 12288), 1 ), 12288 );
1058 // gain_pit = (gain_pit-0.75)/2.0 + 0.75;
1060 if ( sub (gain_pit, 14745) > 0) // if (gain_pit > 0.90) in Q14
1066 *-------------------------------------------------------*
1067 * Calculate CB mixed gain *
1068 *-------------------------------------------------------*
1069 Int_lsf(prev_lsf, st->lsfState->past_lsf_q, i_subfr, lsf_i);
1070 gain_code_mix = Cb_gain_average(
1071 st->Cb_gain_averState, mode, gain_code,
1072 lsf_i, st->lsp_avg_st->lsp_meanSave, bfi,
1073 st->prev_bf, pdfi, st->prev_pdf,
1074 st->inBackgroundNoise, st->voicedHangover);
1076 // make sure that MR74, MR795, MR122 have original code_gain
1077 if ((sub(mode, MR67) > 0) && (sub(mode, MR102) != 0) )
1078 // MR74, MR795, MR122
1080 gain_code_mix = gain_code;
1083 *-------------------------------------------------------*
1084 * - Find the total excitation. *
1085 * - Find synthesis speech corresponding to st->exc[]. *
1086 *-------------------------------------------------------*
1087 if (sub(mode, MR102) <= 0) // MR475, MR515, MR59, MR67, MR74, MR795, MR102
1089 pitch_fac = gain_pit;
1094 pitch_fac = shr (gain_pit, 1);
1098 // copy unscaled LTP excitation to exc_enhanced (used in phase
1099 * dispersion below) and compute total excitation for LTP feedback
1101 for (i = 0; i < L_SUBFR; i++)
1103 exc_enhanced[i] = st->exc[i];
1105 // st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i];
1106 L_temp = L_mult (st->exc[i], pitch_fac);
1109 L_temp = L_mac (L_temp, code[i], gain_code);
1112 L_temp = L_shl (L_temp, tmp_shift); // Q16
1113 st->exc[i] = pv_round (L_temp);
1116 *-------------------------------------------------------*
1117 * - Adaptive phase dispersion *
1118 *-------------------------------------------------------*
1119 ph_disp_release(st->ph_disp_st); // free phase dispersion adaption
1121 if ( ((sub(mode, MR475) == 0) ||
1122 (sub(mode, MR515) == 0) ||
1123 (sub(mode, MR59) == 0)) &&
1124 sub(st->voicedHangover, 3) > 0 &&
1125 st->inBackgroundNoise != 0 &&
1128 ph_disp_lock(st->ph_disp_st); // Always Use full Phase Disp.
1129 } // if error in bg noise
1131 // apply phase dispersion to innovation (if enabled) and
1132 compute total excitation for synthesis part
1133 ph_disp(st->ph_disp_st, mode,
1134 exc_enhanced, gain_code_mix, gain_pit, code,
1135 pitch_fac, tmp_shift);
1137 *-------------------------------------------------------*
1138 * - The Excitation control module are active during BFI.*
1139 * - Conceal drops in signal energy if in bg noise. *
1140 *-------------------------------------------------------*
1143 for (i = 0; i < L_SUBFR; i++)
1145 L_temp = L_mac (L_temp, exc_enhanced[i], exc_enhanced[i] );
1148 L_temp = L_shr (L_temp, 1); // excEnergy = sqrt(L_temp) in Q0
1149 L_temp = sqrt_l_exp(L_temp, &temp); // function result
1150 L_temp = L_shr(L_temp, add( shr(temp, 1), 15));
1151 L_temp = L_shr(L_temp, 2); // To cope with 16-bit and
1152 excEnergy = extract_l(L_temp); // scaling in ex_ctrl()
1154 if ( ((sub (mode, MR475) == 0) ||
1155 (sub (mode, MR515) == 0) ||
1156 (sub (mode, MR59) == 0)) &&
1157 sub(st->voicedHangover, 5) > 0 &&
1158 st->inBackgroundNoise != 0 &&
1159 sub(st->state, 4) < 0 &&
1160 ( (pdfi != 0 && st->prev_pdf != 0) ||
1165 if ( pdfi != 0 && bfi == 0 )
1170 Ex_ctrl(exc_enhanced,
1178 if ( st->inBackgroundNoise != 0 &&
1179 ( bfi != 0 || st->prev_bf != 0 ) &&
1180 sub(st->state, 4) < 0 )
1186 // Update energy history for all modes
1187 for (i = 0; i < 8; i++)
1189 st->excEnergyHist[i] = st->excEnergyHist[i+1];
1191 st->excEnergyHist[8] = excEnergy;
1193 *-------------------------------------------------------*
1194 * Excitation control module end. *
1195 *-------------------------------------------------------*
1197 if (sub (pit_sharp, 16384) > 0)
1199 for (i = 0; i < L_SUBFR; i++)
1201 excp[i] = add (excp[i], exc_enhanced[i]);
1203 agc2 (exc_enhanced, excp, L_SUBFR);
1205 Syn_filt (Az, excp, &synth[i_subfr], L_SUBFR,
1211 Syn_filt (Az, exc_enhanced, &synth[i_subfr], L_SUBFR,
1215 if (Overflow != 0) // Test for overflow
1217 for (i = 0; i < PIT_MAX + L_INTERPOL + L_SUBFR; i++)
1219 st->old_exc[i] = shr(st->old_exc[i], 2);
1221 for (i = 0; i < L_SUBFR; i++)
1223 exc_enhanced[i] = shr(exc_enhanced[i], 2);
1225 Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
1229 Copy(&synth[i_subfr+L_SUBFR-M], st->mem_syn, M);
1232 *--------------------------------------------------*
1233 * Update signal for next frame. *
1234 * -> shift to the left by L_SUBFR st->exc[] *
1235 *--------------------------------------------------*
1237 Copy (&st->old_exc[L_SUBFR], &st->old_exc[0], PIT_MAX + L_INTERPOL);
1239 // interpolated LPC parameters for next subframe
1242 // store T0 for next subframe
1246 *-------------------------------------------------------*
1247 * Call the Source Characteristic Detector which updates *
1248 * st->inBackgroundNoise and st->voicedHangover. *
1249 *-------------------------------------------------------*
1251 st->inBackgroundNoise = Bgn_scd(st->background_state,
1252 &(st->ltpGainHistory[0]),
1254 &(st->voicedHangover) );
1256 dtx_dec_activity_update(st->dtxDecoderState,
1257 st->lsfState->past_lsf_q,
1260 // store bfi for next subframe
1262 st->prev_pdf = pdfi;
1264 *--------------------------------------------------*
1265 * Calculate the LSF averages on the eight *
1267 *--------------------------------------------------*
1269 lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
1272 st->dtxDecoderState->dtxGlobalState = newDTXState;
1277 ------------------------------------------------------------------------------
1279 [State any special notes, constraints or cautions for users of this function]
1281 ------------------------------------------------------------------------------
1285 Decoder_amrState *st, /* i/o : State variables */
1286 enum Mode mode, /* i : AMR mode */
1287 Word16 parm[], /* i : vector of synthesis parameters
1289 enum RXFrameType frame_type, /* i : received frame type */
1290 Word16 synth[], /* o : synthesis speech (L_FRAME) */
1291 Word16 A_t[] /* o : decoded LP filter in 4 subframes
1295 /* LPC coefficients */
1297 Word16 *Az; /* Pointer on A_t */
1309 /* Algebraic codevector */
1311 Word16 code[L_SUBFR];
1315 Word16 excp[L_SUBFR];
1316 Word16 exc_enhanced[L_SUBFR];
1325 Word16 index_mr475 = 0;
1328 Word16 gain_code_mix;
1334 Word16 delta_frc_low;
1335 Word16 delta_frc_range;
1343 Word16 evenSubfr = 0;
1345 Word16 bfi = 0; /* bad frame indication flag */
1346 Word16 pdfi = 0; /* potential degraded bad frame flag */
1348 enum DTXStateType newDTXState; /* SPEECH , DTX, DTX_MUTE */
1349 Flag *pOverflow = &(st->overflow); /* Overflow flag */
1352 /* find the new DTX state SPEECH OR DTX */
1353 newDTXState = rx_dtx_handler(&(st->dtxDecoderState), frame_type, pOverflow);
1357 if (newDTXState != SPEECH)
1359 Decoder_amr_reset(st, MRDTX);
1361 dtx_dec(&(st->dtxDecoderState),
1365 &(st->Cb_gain_averState),
1368 parm, &(st->common_amr_tbls), synth, A_t, pOverflow);
1370 /* update average lsp */
1372 st->lsfState.past_lsf_q,
1379 st->lsfState.past_lsf_q,
1385 /* SPEECH action state machine */
1386 if ((frame_type == RX_SPEECH_BAD) || (frame_type == RX_NO_DATA) ||
1387 (frame_type == RX_ONSET))
1391 if ((frame_type == RX_NO_DATA) || (frame_type == RX_ONSET))
1393 build_CN_param(&st->nodataSeed,
1394 st->common_amr_tbls.prmno_ptr[mode],
1395 st->common_amr_tbls.bitno_ptr[mode],
1397 st->common_amr_tbls.window_200_40_ptr,
1401 else if (frame_type == RX_SPEECH_DEGRADED)
1410 else if (st->state == 6)
1426 /* If this frame is the first speech frame after CNI period, */
1427 /* set the BFH state machine to an appropriate state depending */
1428 /* on whether there was DTX muting before start of speech or not */
1429 /* If there was DTX muting, the first speech frame is muted. */
1430 /* If there was no DTX muting, the first speech frame is not */
1431 /* muted. The BFH state machine starts from state 5, however, to */
1432 /* keep the audible noise resulting from a SID frame which is */
1433 /* erroneously interpreted as a good speech frame as small as */
1434 /* possible (the decoder output in this case is quickly muted) */
1436 if (st->dtxDecoderState.dtxGlobalState == DTX)
1441 else if (st->dtxDecoderState.dtxGlobalState == DTX_MUTE)
1447 /* save old LSFs for CB gain smoothing */
1448 oscl_memmove((void *)prev_lsf, st->lsfState.past_lsf_q, M*sizeof(*st->lsfState.past_lsf_q));
1450 /* decode LSF parameters and generate interpolated lpc coefficients
1451 for the 4 subframes */
1460 &st->common_amr_tbls,
1464 /* Advance synthesis parameters pointer */
1479 &(st->common_amr_tbls),
1484 /* Advance synthesis parameters pointer */
1495 /* update the LSPs for the next frame */
1496 for (i = 0; i < M; i++)
1498 st->lsp_old[i] = lsp_new[i];
1501 /*------------------------------------------------------------------------*
1502 * Loop for every subframe in the analysis frame *
1503 *------------------------------------------------------------------------*
1504 * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR *
1506 * - decode the pitch delay *
1507 * - decode algebraic code *
1508 * - decode pitch and codebook gains *
1509 * - find the excitation and compute synthesis speech *
1510 *------------------------------------------------------------------------*/
1512 /* pointer to interpolated LPC parameters */
1517 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
1520 evenSubfr = 1 - evenSubfr;
1522 /* flag for first and 3th subframe */
1526 if (i_subfr == L_FRAME_BY2)
1528 if ((mode != MR475) && (mode != MR515))
1537 /*-------------------------------------------------------*
1538 * - decode pitch lag and find adaptive codebook vector. *
1539 *-------------------------------------------------------*/
1543 /* flag4 indicates encoding with 4 bit resolution; */
1544 /* this is needed for mode MR475, MR515, MR59 and MR67 */
1548 if ((mode == MR475) || (mode == MR515) || (mode == MR59) ||
1554 /*-------------------------------------------------------*
1555 * - get ranges for the t0_min and t0_max *
1556 * - only needed in delta decoding *
1557 *-------------------------------------------------------*/
1560 delta_frc_range = 9;
1565 delta_frc_range = 19;
1568 t0_min = st->old_T0 - delta_frc_low;
1570 if (t0_min < PIT_MIN)
1574 t0_max = t0_min + delta_frc_range;
1576 if (t0_max > PIT_MAX)
1579 t0_min = t0_max - delta_frc_range;
1582 Dec_lag3(index, t0_min, t0_max, pit_flag, st->old_T0,
1583 &T0, &T0_frac, flag4, pOverflow);
1585 st->T0_lagBuff = T0;
1589 if (st->old_T0 < PIT_MAX)
1590 { /* Graceful pitch */
1591 st->old_T0 += 1; /* degradation */
1596 if ((st->inBackgroundNoise != 0) && (st->voicedHangover > 4) &&
1597 ((mode == MR475) || (mode == MR515) || (mode == MR59)))
1599 T0 = st->T0_lagBuff;
1603 Pred_lt_3or6(st->exc, T0, T0_frac, L_SUBFR, 1, pOverflow);
1607 Dec_lag6(index, PIT_MIN_MR122,
1608 PIT_MAX, pit_flag, &T0, &T0_frac, pOverflow);
1611 if (!(bfi == 0 && (pit_flag == 0 || index < 61)))
1613 st->T0_lagBuff = T0;
1618 Pred_lt_3or6(st->exc, T0, T0_frac, L_SUBFR, 0, pOverflow);
1621 /*-------------------------------------------------------*
1622 * - (MR122 only: Decode pitch gain.) *
1623 * - Decode innovative codebook. *
1624 * - set pitch sharpening factor *
1625 *-------------------------------------------------------*/
1626 if ((mode == MR475) || (mode == MR515))
1627 { /* MR475, MR515 */
1628 index = *parm++; /* index of position */
1629 i = *parm++; /* signs */
1631 decode_2i40_9bits(subfrNr, i, index, st->common_amr_tbls.startPos_ptr, code, pOverflow);
1633 L_temp = (Word32)st->sharp << 1;
1634 if (L_temp != (Word32)((Word16) L_temp))
1636 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1640 pit_sharp = (Word16) L_temp;
1643 else if (mode == MR59)
1645 index = *parm++; /* index of position */
1646 i = *parm++; /* signs */
1648 decode_2i40_11bits(i, index, code);
1650 L_temp = (Word32)st->sharp << 1;
1651 if (L_temp != (Word32)((Word16) L_temp))
1653 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1657 pit_sharp = (Word16) L_temp;
1660 else if (mode == MR67)
1662 index = *parm++; /* index of position */
1663 i = *parm++; /* signs */
1665 decode_3i40_14bits(i, index, code);
1667 L_temp = (Word32)st->sharp << 1;
1668 if (L_temp != (Word32)((Word16) L_temp))
1670 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1674 pit_sharp = (Word16) L_temp;
1677 else if (mode <= MR795)
1679 index = *parm++; /* index of position */
1680 i = *parm++; /* signs */
1682 decode_4i40_17bits(i, index, st->common_amr_tbls.dgray_ptr, code);
1684 L_temp = (Word32)st->sharp << 1;
1685 if (L_temp != (Word32)((Word16) L_temp))
1687 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1691 pit_sharp = (Word16) L_temp;
1694 else if (mode == MR102)
1696 dec_8i40_31bits(parm, code, pOverflow);
1699 L_temp = (Word32)st->sharp << 1;
1700 if (L_temp != (Word32)((Word16) L_temp))
1702 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1706 pit_sharp = (Word16) L_temp;
1716 &(st->ec_gain_p_st),
1723 gain_pit = d_gain_pitch(mode, index, st->common_amr_tbls.qua_gain_pitch_ptr);
1725 ec_gain_pitch_update(
1726 &(st->ec_gain_p_st),
1733 dec_10i40_35bits(parm, code, st->common_amr_tbls.dgray_ptr);
1736 /* pit_sharp = gain_pit; */
1737 /* if (pit_sharp > 1.0) pit_sharp = 1.0; */
1739 L_temp = (Word32)gain_pit << 1;
1740 if (L_temp != (Word32)((Word16) L_temp))
1742 pit_sharp = (gain_pit > 0) ? MAX_16 : MIN_16;
1746 pit_sharp = (Word16) L_temp;
1749 /*-------------------------------------------------------*
1750 * - Add the pitch contribution to code[]. *
1751 *-------------------------------------------------------*/
1752 for (i = T0; i < L_SUBFR; i++)
1754 temp = mult(*(code + i - T0), pit_sharp, pOverflow);
1755 *(code + i) = add_16(*(code + i), temp, pOverflow);
1759 /*------------------------------------------------------------*
1760 * - Decode codebook gain (MR122) or both pitch *
1761 * gain and codebook gain (all others) *
1762 * - Update pitch sharpening "sharp" with quantized gain_pit *
1763 *------------------------------------------------------------*/
1766 /* read and decode pitch and code gain */
1770 index_mr475 = *parm++; /* index of gain(s) */
1783 &(st->common_amr_tbls),
1789 &(st->ec_gain_p_st),
1795 &(st->ec_gain_c_st),
1801 ec_gain_pitch_update(
1808 ec_gain_code_update(
1815 pit_sharp = gain_pit;
1817 if (pit_sharp > SHARPMAX)
1819 pit_sharp = SHARPMAX;
1823 else if ((mode <= MR74) || (mode == MR102))
1825 /* read and decode pitch and code gain */
1826 index = *parm++; /* index of gain(s) */
1838 &(st->common_amr_tbls),
1844 &(st->ec_gain_p_st),
1850 &(st->ec_gain_c_st),
1857 ec_gain_pitch_update(
1858 &(st->ec_gain_p_st),
1864 ec_gain_code_update(
1865 &(st->ec_gain_c_st),
1871 pit_sharp = gain_pit;
1873 if (pit_sharp > SHARPMAX)
1875 pit_sharp = SHARPMAX;
1880 if (st->old_T0 > (L_SUBFR + 5))
1884 pit_sharp = ~((~pit_sharp) >> 2);
1888 pit_sharp = pit_sharp >> 2;
1895 /* read and decode pitch gain */
1896 index = *parm++; /* index of gain(s) */
1900 /* decode pitch gain */
1904 &(st->ec_gain_p_st),
1911 gain_pit = d_gain_pitch(mode, index, st->common_amr_tbls.qua_gain_pitch_ptr);
1913 ec_gain_pitch_update(
1914 &(st->ec_gain_p_st),
1920 /* read and decode code gain */
1930 st->common_amr_tbls.qua_gain_code_ptr,
1937 &(st->ec_gain_c_st),
1944 ec_gain_code_update(
1945 &(st->ec_gain_c_st),
1951 pit_sharp = gain_pit;
1953 if (pit_sharp > SHARPMAX)
1955 pit_sharp = SHARPMAX;
1968 st->common_amr_tbls.qua_gain_code_ptr,
1975 &(st->ec_gain_c_st),
1982 ec_gain_code_update(
1983 &(st->ec_gain_c_st),
1989 pit_sharp = gain_pit;
1993 /* store pitch sharpening for next subframe */
1994 /* (for modes which use the previous pitch gain for */
1995 /* pitch sharpening in the search phase) */
1996 /* do not update sharpening in even subframes for MR475 */
1997 if ((mode != MR475) || (evenSubfr == 0))
1999 st->sharp = gain_pit;
2001 if (st->sharp > SHARPMAX)
2003 st->sharp = SHARPMAX;
2007 pit_sharp = shl(pit_sharp, 1, pOverflow);
2009 if (pit_sharp > 16384)
2011 for (i = 0; i < L_SUBFR; i++)
2013 temp = mult(st->exc[i], pit_sharp, pOverflow);
2014 L_temp = L_mult(temp, gain_pit, pOverflow);
2020 L_temp = ~((~L_temp) >> 1);
2024 L_temp = L_temp >> 1;
2027 *(excp + i) = pv_round(L_temp, pOverflow);
2031 /*-------------------------------------------------------*
2032 * - Store list of LTP gains needed in the source *
2033 * characteristic detector (SCD) *
2034 *-------------------------------------------------------*/
2038 for (i = 0; i < 8; i++)
2040 st->ltpGainHistory[i] = st->ltpGainHistory[i+1];
2042 st->ltpGainHistory[8] = gain_pit;
2045 /*-------------------------------------------------------*
2046 * - Limit gain_pit if in background noise and BFI *
2047 * for MR475, MR515, MR59 *
2048 *-------------------------------------------------------*/
2051 if ((st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 &&
2052 ((mode == MR475) || (mode == MR515) || (mode == MR59)))
2055 if (gain_pit > 12288) /* if (gain_pit > 0.75) in Q14*/
2057 gain_pit = ((gain_pit - 12288) >> 1) + 12288;
2058 /* gain_pit = (gain_pit-0.75)/2.0 + 0.75; */
2061 if (gain_pit > 14745) /* if (gain_pit > 0.90) in Q14*/
2067 /*-------------------------------------------------------*
2068 * Calculate CB mixed gain *
2069 *-------------------------------------------------------*/
2072 st->lsfState.past_lsf_q,
2079 &(st->Cb_gain_averState),
2083 st->lsp_avg_st.lsp_meanSave,
2088 st->inBackgroundNoise,
2092 /* make sure that MR74, MR795, MR122 have original code_gain*/
2093 if ((mode > MR67) && (mode != MR102))
2094 /* MR74, MR795, MR122 */
2096 gain_code_mix = gain_code;
2099 /*-------------------------------------------------------*
2100 * - Find the total excitation. *
2101 * - Find synthesis speech corresponding to st->exc[]. *
2102 *-------------------------------------------------------*/
2103 if (mode <= MR102) /* MR475, MR515, MR59, MR67, MR74, MR795, MR102*/
2105 pitch_fac = gain_pit;
2112 pitch_fac = ~((~gain_pit) >> 1);
2116 pitch_fac = gain_pit >> 1;
2121 /* copy unscaled LTP excitation to exc_enhanced (used in phase
2122 * dispersion below) and compute total excitation for LTP feedback
2124 for (i = 0; i < L_SUBFR; i++)
2126 exc_enhanced[i] = st->exc[i];
2128 /* st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i]; */
2129 L_temp = L_mult(st->exc[i], pitch_fac, pOverflow);
2130 /* 12.2: Q0 * Q13 */
2132 L_temp = L_mac(L_temp, code[i], gain_code, pOverflow);
2133 /* 12.2: Q12 * Q1 */
2135 L_temp = L_shl(L_temp, tmp_shift, pOverflow); /* Q16 */
2136 st->exc[i] = pv_round(L_temp, pOverflow);
2139 /*-------------------------------------------------------*
2140 * - Adaptive phase dispersion *
2141 *-------------------------------------------------------*/
2142 ph_disp_release(&(st->ph_disp_st)); /* free phase dispersion adaption */
2145 if (((mode == MR475) || (mode == MR515) || (mode == MR59)) &&
2146 (st->voicedHangover > 3) && (st->inBackgroundNoise != 0) &&
2149 ph_disp_lock(&(st->ph_disp_st)); /* Always Use full Phase Disp. */
2150 } /* if error in bg noise */
2152 /* apply phase dispersion to innovation (if enabled) and
2153 compute total excitation for synthesis part */
2163 &(st->common_amr_tbls),
2166 /*-------------------------------------------------------*
2167 * - The Excitation control module are active during BFI.*
2168 * - Conceal drops in signal energy if in bg noise. *
2169 *-------------------------------------------------------*/
2171 for (i = 0; i < L_SUBFR; i++)
2173 L_temp = L_mac(L_temp, *(exc_enhanced + i), *(exc_enhanced + i), pOverflow);
2176 /* excEnergy = sqrt(L_temp) in Q0 */
2179 L_temp = ~((~L_temp) >> 1);
2183 L_temp = L_temp >> 1;
2186 L_temp = sqrt_l_exp(L_temp, &temp, pOverflow);
2187 /* To cope with 16-bit and scaling in ex_ctrl() */
2188 L_temp = L_shr(L_temp, (Word16)((temp >> 1) + 15), pOverflow);
2191 excEnergy = (Word16)(~((~L_temp) >> 2));
2195 excEnergy = (Word16)(L_temp >> 2);
2198 if (((mode == MR475) || (mode == MR515) || (mode == MR59)) &&
2199 (st->voicedHangover > 5) && (st->inBackgroundNoise != 0) &&
2201 ((pdfi != 0 && st->prev_pdf != 0) || bfi != 0 || st->prev_bf != 0))
2205 if (pdfi != 0 && bfi == 0)
2210 Ex_ctrl(exc_enhanced,
2215 carefulFlag, pOverflow);
2218 if (!((st->inBackgroundNoise != 0) && (bfi != 0 || st->prev_bf != 0) &&
2221 /* Update energy history for all modes */
2222 for (i = 0; i < 8; i++)
2224 st->excEnergyHist[i] = st->excEnergyHist[i+1];
2226 st->excEnergyHist[8] = excEnergy;
2228 /*-------------------------------------------------------*
2229 * Excitation control module end. *
2230 *-------------------------------------------------------*/
2231 if (pit_sharp > 16384)
2233 for (i = 0; i < L_SUBFR; i++)
2235 *(excp + i) = add_16(*(excp + i), *(exc_enhanced + i), pOverflow);
2238 agc2(exc_enhanced, excp, L_SUBFR, pOverflow);
2240 Syn_filt(Az, excp, &synth[i_subfr], L_SUBFR,
2246 Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR,
2250 if (*pOverflow != 0) /* Test for overflow */
2252 for (i = PIT_MAX + L_INTERPOL + L_SUBFR - 1; i >= 0; i--)
2254 if (st->old_exc[i] < 0)
2256 st->old_exc[i] = ~((~st->old_exc[i]) >> 2);
2260 st->old_exc[i] = st->old_exc[i] >> 2;
2265 for (i = L_SUBFR - 1; i >= 0; i--)
2267 if (*(exc_enhanced + i) < 0)
2269 *(exc_enhanced + i) = ~((~(*(exc_enhanced + i))) >> 2);
2273 *(exc_enhanced + i) = *(exc_enhanced + i) >> 2;
2276 Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
2280 oscl_memmove((void *)st->mem_syn, &synth[i_subfr+L_SUBFR-M], M*sizeof(synth[0]));
2283 /*--------------------------------------------------*
2284 * Update signal for next frame. *
2285 * -> shift to the left by L_SUBFR st->exc[] *
2286 *--------------------------------------------------*/
2288 oscl_memmove((void *)&st->old_exc[0], &st->old_exc[L_SUBFR], (PIT_MAX + L_INTERPOL)*sizeof(st->old_exc[0]));
2290 /* interpolated LPC parameters for next subframe */
2293 /* store T0 for next subframe */
2297 /*-------------------------------------------------------*
2298 * Call the Source Characteristic Detector which updates *
2299 * st->inBackgroundNoise and st->voicedHangover. *
2300 *-------------------------------------------------------*/
2302 st->inBackgroundNoise =
2304 &(st->background_state),
2305 &(st->ltpGainHistory[0]),
2307 &(st->voicedHangover),
2310 dtx_dec_activity_update(
2311 &(st->dtxDecoderState),
2312 st->lsfState.past_lsf_q,
2316 /* store bfi for next subframe */
2318 st->prev_pdf = pdfi;
2320 /*--------------------------------------------------*
2321 * Calculate the LSF averages on the eight *
2323 *--------------------------------------------------*/
2326 st->lsfState.past_lsf_q,
2330 st->dtxDecoderState.dtxGlobalState = newDTXState;