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 ------------------------------------------------------------------------------
38 dtx_dec_activity_update
41 ------------------------------------------------------------------------------
44 These modules decode the comfort noise when in DTX.
46 ------------------------------------------------------------------------------
50 /*----------------------------------------------------------------------------
52 ----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------
70 ; Define module specific macros here
71 ----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------
76 ; Include all pre-processor statements here. Include conditional
77 ; compile variables also.
78 ----------------------------------------------------------------------------*/
79 #define PN_INITIAL_SEED 0x70816958L /* Pseudo noise generator seed value */
81 /*----------------------------------------------------------------------------
82 ; LOCAL FUNCTION DEFINITIONS
83 ; Function Prototype declaration
84 ----------------------------------------------------------------------------*/
87 /*----------------------------------------------------------------------------
88 ; LOCAL VARIABLE DEFINITIONS
89 ; Variable declaration - defined here and used outside this module
90 ----------------------------------------------------------------------------*/
92 /***************************************************
93 * Scaling factors for the lsp variability operation *
94 ***************************************************/
95 static const Word16 lsf_hist_mean_scale[M] =
109 /*************************************************
110 * level adjustment for different modes Q11 *
111 *************************************************/
112 static const Word16 dtx_log_en_adjust[9] =
127 ------------------------------------------------------------------------------
128 FUNCTION NAME: dtx_dec_reset
129 ------------------------------------------------------------------------------
130 INPUT AND OUTPUT DEFINITIONS
133 st = pointer to a structure of type dtx_decState
136 Structure pointed to by st is initialized to a set of initial values.
139 return_value = 0 if memory was successfully initialized,
140 otherwise returns -1 (int)
142 Global Variables Used:
145 Local Variables Needed:
148 ------------------------------------------------------------------------------
151 Reset of state memory for dtx_dec.
153 ------------------------------------------------------------------------------
158 ------------------------------------------------------------------------------
161 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
163 ------------------------------------------------------------------------------
166 int dtx_dec_reset (dtx_decState *st)
170 if (st == (dtx_decState *) NULL){
171 fprintf(stderr, "dtx_dec_reset: invalid parameter\n");
175 st->since_last_sid = 0;
176 st->true_sid_period_inv = (1 << 13);
179 st->old_log_en = 3500;
180 // low level noise for better performance in DTX handover cases
182 st->L_pn_seed_rx = PN_INITIAL_SEED;
184 // Initialize state->lsp [] and state->lsp_old []
185 Copy(lsp_init_data, &st->lsp[0], M);
186 Copy(lsp_init_data, &st->lsp_old[0], M);
188 st->lsf_hist_ptr = 0;
190 st->log_en_hist_ptr = 0;
192 // initialize decoder lsf history
193 Copy(mean_lsf, &st->lsf_hist[0], M);
195 for (i = 1; i < DTX_HIST_SIZE; i++)
197 Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
199 Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE);
201 // initialize decoder log frame energy
202 for (i = 0; i < DTX_HIST_SIZE; i++)
204 st->log_en_hist[i] = st->log_en;
207 st->log_en_adjust = 0;
209 st->dtxHangoverCount = DTX_HANG_CONST;
210 st->decAnaElapsedCount = 32767;
214 st->dtxHangoverAdded = 0;
216 st->dtxGlobalState = DTX;
217 st->data_updated = 0;
221 ------------------------------------------------------------------------------
223 [State any special notes, constraints or cautions for users of this function]
225 ------------------------------------------------------------------------------
228 Word16 dtx_dec_reset(dtx_decState *st)
232 if (st == (dtx_decState *) NULL)
234 /* fprint(stderr, "dtx_dec_reset: invalid parameter\n"); */
238 st->since_last_sid = 0;
239 st->true_sid_period_inv = (1 << 13);
242 st->old_log_en = 3500;
243 /* low level noise for better performance in DTX handover cases*/
245 st->L_pn_seed_rx = PN_INITIAL_SEED;
247 /* Initialize state->lsp [] */
259 /* Initialize state->lsp_old [] */
260 st->lsp_old[0] = 30000;
261 st->lsp_old[1] = 26000;
262 st->lsp_old[2] = 21000;
263 st->lsp_old[3] = 15000;
264 st->lsp_old[4] = 8000;
266 st->lsp_old[6] = -8000;
267 st->lsp_old[7] = -15000;
268 st->lsp_old[8] = -21000;
269 st->lsp_old[9] = -26000;
271 st->lsf_hist_ptr = 0;
273 st->log_en_hist_ptr = 0;
275 /* initialize decoder lsf history */
276 st->lsf_hist[0] = 1384;
277 st->lsf_hist[1] = 2077;
278 st->lsf_hist[2] = 3420;
279 st->lsf_hist[3] = 5108;
280 st->lsf_hist[4] = 6742;
281 st->lsf_hist[5] = 8122;
282 st->lsf_hist[6] = 9863;
283 st->lsf_hist[7] = 11092;
284 st->lsf_hist[8] = 12714;
285 st->lsf_hist[9] = 13701;
287 for (i = 1; i < DTX_HIST_SIZE; i++)
289 oscl_memmove((void *)&st->lsf_hist[M*i], &st->lsf_hist[0], M*sizeof(st->lsf_hist[0]));
291 oscl_memset(st->lsf_hist_mean, 0, sizeof(Word16)*M*DTX_HIST_SIZE);
293 /* initialize decoder log frame energy */
294 for (i = 0; i < DTX_HIST_SIZE; i++)
296 st->log_en_hist[i] = st->log_en;
299 st->log_en_adjust = 0;
301 st->dtxHangoverCount = DTX_HANG_CONST;
302 st->decAnaElapsedCount = 32767;
306 st->dtxHangoverAdded = 0;
308 st->dtxGlobalState = DTX;
309 st->data_updated = 0;
314 /****************************************************************************/
317 ------------------------------------------------------------------------------
318 FUNCTION NAME: dtx_dec
319 ------------------------------------------------------------------------------
320 INPUT AND OUTPUT DEFINITIONS
323 st = pointer to a structure of type dtx_decState
324 mem_syn = AMR decoder state
325 lsfState = decoder lsf states
326 predState = prediction states
327 averState = CB gain average states
328 new_state = new DTX state
330 parm = Vector of synthesis parameters
333 st points to an updated structure of type dtx_decState
334 mem_syn = AMR decoder state
335 lsfState = decoder lsf states
336 predState = prediction states
337 averState = CB gain average states
338 synth = synthesised speech
339 A_t = decoded LP filter in 4 subframes
342 return_value = 0 (int)
344 Global Variables Used:
347 Local Variables Needed:
350 ------------------------------------------------------------------------------
353 Decode comfort noise when in DTX.
355 ------------------------------------------------------------------------------
360 ------------------------------------------------------------------------------
363 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
365 ------------------------------------------------------------------------------
369 dtx_decState *st, // i/o : State struct
370 Word16 mem_syn[], // i/o : AMR decoder state
371 D_plsfState* lsfState, // i/o : decoder lsf states
372 gc_predState* predState, // i/o : prediction states
373 Cb_gain_averageState* averState, // i/o : CB gain average states
374 enum DTXStateType new_state, // i : new DTX state
375 enum Mode mode, // i : AMR mode
376 Word16 parm[], // i : Vector of synthesis parameters
377 Word16 synth[], // o : synthesised speech
378 Word16 A_t[] // o : decoded LP filter in 4 subframes
389 Word16 acoeff[M + 1];
394 Word16 log_pg_e, log_pg_m;
399 Word16 lsf_variab_index;
400 Word16 lsf_variab_factor;
402 Word16 lsf_int_variab[M];
403 Word16 lsp_int_variab[M];
404 Word16 acoeff_variab[M + 1];
409 Word16 tmp_int_length;
412 // This function is called if synthesis state is not SPEECH
413 // the globally passed inputs to this function are
416 // st->dtxHangoverAdded
417 // new_state (SPEECH, DTX, DTX_MUTE)
419 if ((st->dtxHangoverAdded != 0) &&
420 (st->sid_frame != 0))
422 // sid_first after dtx hangover period
423 // or sid_upd after dtxhangover
425 // set log_en_adjust to correct value
426 st->log_en_adjust = dtx_log_en_adjust[mode];
428 ptr = add(st->lsf_hist_ptr, M);
429 if (sub(ptr, 80) == 0)
433 Copy( &st->lsf_hist[st->lsf_hist_ptr],&st->lsf_hist[ptr],M);
435 ptr = add(st->log_en_hist_ptr,1);
436 if (sub(ptr, DTX_HIST_SIZE) == 0)
440 st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; // Q11
442 // compute mean log energy and lsp
443 // from decoded signal (SID_FIRST)
445 for (i = 0; i < M; i++)
450 // average energy and lsp
451 for (i = 0; i < DTX_HIST_SIZE; i++)
453 st->log_en = add(st->log_en,
454 shr(st->log_en_hist[i],3));
455 for (j = 0; j < M; j++)
457 L_lsf[j] = L_add(L_lsf[j],
458 L_deposit_l(st->lsf_hist[i * M + j]));
462 for (j = 0; j < M; j++)
464 lsf[j] = extract_l(L_shr(L_lsf[j],3)); // divide by 8
467 Lsf_lsp(lsf, st->lsp, M);
469 // make log_en speech coder mode independent
470 // added again later before synthesis
471 st->log_en = sub(st->log_en, st->log_en_adjust);
473 // compute lsf variability vector
474 Copy(st->lsf_hist, st->lsf_hist_mean, 80);
476 for (i = 0; i < M; i++)
480 for (j = 0; j < 8; j++)
482 L_lsf_mean = L_add(L_lsf_mean,
483 L_deposit_l(st->lsf_hist_mean[i+j*M]));
486 lsf_mean = extract_l(L_shr(L_lsf_mean, 3));
487 // subtract mean and limit to within reasonable limits
488 // moreover the upper lsf's are attenuated
489 for (j = 0; j < 8; j++)
492 st->lsf_hist_mean[i+j*M] =
493 sub(st->lsf_hist_mean[i+j*M], lsf_mean);
495 // attenuate deviation from mean, especially for upper lsf's
496 st->lsf_hist_mean[i+j*M] =
497 mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i]);
499 // limit the deviation
500 if (st->lsf_hist_mean[i+j*M] < 0)
508 st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);
511 if (sub(st->lsf_hist_mean[i+j*M], 655) > 0)
513 st->lsf_hist_mean[i+j*M] =
514 add(655, shr(sub(st->lsf_hist_mean[i+j*M], 655), 2));
518 if (sub(st->lsf_hist_mean[i+j*M], 1310) > 0)
520 st->lsf_hist_mean[i+j*M] = 1310;
524 st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
531 if (st->sid_frame != 0 )
533 // Set old SID parameters, always shift
534 // even if there is no new valid_data
535 Copy(st->lsp, st->lsp_old, M);
536 st->old_log_en = st->log_en;
538 if (st->valid_data != 0 ) // new data available (no CRC)
540 // Compute interpolation factor, since the division only works
541 // for values of since_last_sid < 32 we have to limit the
542 // interpolation to 32 frames
543 tmp_int_length = st->since_last_sid;
544 st->since_last_sid = 0;
546 if (sub(tmp_int_length, 32) > 0)
550 if (sub(tmp_int_length, 2) >= 0)
552 st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
556 st->true_sid_period_inv = 1 << 14; // 0.5 it Q15
559 Init_D_plsf_3(lsfState, parm[0]); // temporay initialization
560 D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp);
561 Set_zero(lsfState->past_r_q, M); // reset for next speech frame
563 log_en_index = parm[4];
564 // Q11 and divide by 4
565 st->log_en = shl(log_en_index, (11 - 2));
567 // Subtract 2.5 in Q11
568 st->log_en = sub(st->log_en, (2560 * 2));
570 // Index 0 is reserved for silence
571 if (log_en_index == 0)
576 // no interpolation at startup after coder reset
577 // or when SID_UPD has been received right after SPEECH
578 if ((st->data_updated == 0) ||
579 (sub(st->dtxGlobalState, SPEECH) == 0)
582 Copy(st->lsp, st->lsp_old, M);
583 st->old_log_en = st->log_en;
585 } // endif valid_data
587 // initialize gain predictor memory of other modes
588 ma_pred_init = sub(shr(st->log_en,1), 9000);
589 if (ma_pred_init > 0)
593 if (sub(ma_pred_init, -14436) < 0)
595 ma_pred_init = -14436;
598 predState->past_qua_en[0] = ma_pred_init;
599 predState->past_qua_en[1] = ma_pred_init;
600 predState->past_qua_en[2] = ma_pred_init;
601 predState->past_qua_en[3] = ma_pred_init;
603 // past_qua_en for other modes than MR122
604 ma_pred_init = mult(5443, ma_pred_init);
605 // scale down by factor 20*log10(2) in Q15
606 predState->past_qua_en_MR122[0] = ma_pred_init;
607 predState->past_qua_en_MR122[1] = ma_pred_init;
608 predState->past_qua_en_MR122[2] = ma_pred_init;
609 predState->past_qua_en_MR122[3] = ma_pred_init;
613 // recompute level adjustment factor Q11
614 // st->log_en_adjust = 0.9*st->log_en_adjust +
615 // 0.1*dtx_log_en_adjust[mode]);
616 st->log_en_adjust = add(mult(st->log_en_adjust, 29491),
617 shr(mult(shl(dtx_log_en_adjust[mode],5),3277),5));
619 // Interpolate SID info
620 int_fac = shl(add(1,st->since_last_sid), 10); // Q10
621 int_fac = mult(int_fac, st->true_sid_period_inv); // Q10 * Q15 -> Q10
623 // Maximize to 1.0 in Q10
624 if (sub(int_fac, 1024) > 0)
628 int_fac = shl(int_fac, 4); // Q10 -> Q14
630 L_log_en_int = L_mult(int_fac, st->log_en); // Q14 * Q11->Q26
631 for(i = 0; i < M; i++)
633 lsp_int[i] = mult(int_fac, st->lsp[i]);// Q14 * Q15 -> Q14
636 int_fac = sub(16384, int_fac); // 1-k in Q14
638 // (Q14 * Q11 -> Q26) + Q26 -> Q26
639 L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en);
640 for(i = 0; i < M; i++)
642 // Q14 + (Q14 * Q15 -> Q14) -> Q14
643 lsp_int[i] = add(lsp_int[i], mult(int_fac, st->lsp_old[i]));
644 lsp_int[i] = shl(lsp_int[i], 1); // Q14 -> Q15
647 // compute the amount of lsf variability
648 lsf_variab_factor = sub(st->log_pg_mean,2457); // -0.6 in Q12
649 // *0.3 Q12*Q15 -> Q12
650 lsf_variab_factor = sub(4096, mult(lsf_variab_factor, 9830));
652 // limit to values between 0..1 in Q12
653 if (sub(lsf_variab_factor, 4096) > 0)
655 lsf_variab_factor = 4096;
657 if (lsf_variab_factor < 0)
659 lsf_variab_factor = 0;
661 lsf_variab_factor = shl(lsf_variab_factor, 3); // -> Q15
663 // get index of vector to do variability with
664 lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);
667 Lsp_lsf(lsp_int, lsf_int, M);
669 // apply lsf variability
670 Copy(lsf_int, lsf_int_variab, M);
671 for(i = 0; i < M; i++)
673 lsf_int_variab[i] = add(lsf_int_variab[i],
674 mult(lsf_variab_factor,
675 st->lsf_hist_mean[i+lsf_variab_index*M]));
678 // make sure that LSP's are ordered
679 Reorder_lsf(lsf_int, LSF_GAP, M);
680 Reorder_lsf(lsf_int_variab, LSF_GAP, M);
682 // copy lsf to speech decoders lsf state
683 Copy(lsf_int, lsfState->past_lsf_q, M);
686 Lsf_lsp(lsf_int, lsp_int, M);
687 Lsf_lsp(lsf_int_variab, lsp_int_variab, M);
689 // Compute acoeffs Q12 acoeff is used for level
690 // normalization and postfilter, acoeff_variab is
691 // used for synthesis filter
692 // by doing this we make sure that the level
693 // in high frequenncies does not jump up and down
695 Lsp_Az(lsp_int, acoeff);
696 Lsp_Az(lsp_int_variab, acoeff_variab);
698 // For use in postfilter
699 Copy(acoeff, &A_t[0], M + 1);
700 Copy(acoeff, &A_t[M + 1], M + 1);
701 Copy(acoeff, &A_t[2 * (M + 1)], M + 1);
702 Copy(acoeff, &A_t[3 * (M + 1)], M + 1);
704 // Compute reflection coefficients Q15
705 A_Refl(&acoeff[1], refl);
707 // Compute prediction error in Q15
708 pred_err = MAX_16; // 0.99997 in Q15
709 for (i = 0; i < M; i++)
711 pred_err = mult(pred_err, sub(MAX_16, mult(refl[i], refl[i])));
714 // compute logarithm of prediction gain
715 Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m);
717 // convert exponent and mantissa to Word16 Q12
718 log_pg = shl(sub(log_pg_e,15), 12); // Q12
719 log_pg = shr(sub(0,add(log_pg, shr(log_pg_m, 15-12))), 1);
720 st->log_pg_mean = add(mult(29491,st->log_pg_mean),
723 // Compute interpolated log energy
724 L_log_en_int = L_shr(L_log_en_int, 10); // Q26 -> Q16
727 L_log_en_int = L_add(L_log_en_int, 4 * 65536L);
729 // subtract prediction gain
730 L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4));
732 // adjust level to speech coder mode
733 L_log_en_int = L_add(L_log_en_int,
734 L_shl(L_deposit_l(st->log_en_adjust), 5));
736 log_en_int_e = extract_h(L_log_en_int);
737 log_en_int_m = extract_l(L_shr(L_sub(L_log_en_int,
738 L_deposit_h(log_en_int_e)), 1));
739 level = extract_l(Pow2(log_en_int_e, log_en_int_m)); // Q4
741 for (i = 0; i < 4; i++)
743 // Compute innovation vector
744 build_CN_code(&st->L_pn_seed_rx, ex);
745 for (j = 0; j < L_SUBFR; j++)
747 ex[j] = mult(level, ex[j]);
750 Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
755 // reset codebook averaging variables
756 averState->hangVar = 20;
757 averState->hangCount = 0;
759 if (sub(new_state, DTX_MUTE) == 0)
761 // mute comfort noise as it has been quite a long time since
762 * last SID update was performed
764 tmp_int_length = st->since_last_sid;
765 if (sub(tmp_int_length, 32) > 0)
770 // safety guard against division by zero
771 if(tmp_int_length <= 0) {
775 st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
777 st->since_last_sid = 0;
778 Copy(st->lsp, st->lsp_old, M);
779 st->old_log_en = st->log_en;
780 // subtract 1/8 in Q11 i.e -6/8 dB
781 st->log_en = sub(st->log_en, 256);
784 // reset interpolation length timer
785 // if data has been updated.
786 if ((st->sid_frame != 0) &&
787 ((st->valid_data != 0) ||
788 ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
790 st->since_last_sid = 0;
791 st->data_updated = 1;
798 ------------------------------------------------------------------------------
800 [State any special notes, constraints or cautions for users of this function]
802 ------------------------------------------------------------------------------
806 dtx_decState *st, /* i/o : State struct */
807 Word16 mem_syn[], /* i/o : AMR decoder state */
808 D_plsfState* lsfState, /* i/o : decoder lsf states */
809 gc_predState* predState, /* i/o : prediction states */
810 Cb_gain_averageState* averState, /* i/o : CB gain average states */
811 enum DTXStateType new_state, /* i : new DTX state */
812 enum Mode mode, /* i : AMR mode */
813 Word16 parm[], /* i : Vector of synthesis parameters */
814 CommonAmrTbls* common_amr_tbls, /* i : Ptr to struct of table ptrs */
815 Word16 synth[], /* o : synthesised speech */
816 Word16 A_t[], /* o : decoded LP filter in 4 subframes*/
829 Word16 acoeff[M + 1];
840 Word16 lsf_variab_index;
841 Word16 lsf_variab_factor;
843 Word16 lsf_int_variab[M];
844 Word16 lsp_int_variab[M];
845 Word16 acoeff_variab[M + 1];
850 Word16 tmp_int_length;
855 /* This function is called if synthesis state is not SPEECH
856 * the globally passed inputs to this function are
859 * st->dtxHangoverAdded
860 * new_state (SPEECH, DTX, DTX_MUTE)
863 if ((st->dtxHangoverAdded != 0) &&
864 (st->sid_frame != 0))
866 /* sid_first after dtx hangover period */
867 /* or sid_upd after dtxhangover */
869 /* set log_en_adjust to correct value */
870 st->log_en_adjust = dtx_log_en_adjust[mode];
872 ptr = st->lsf_hist_ptr + M;
878 oscl_memmove((void *)&st->lsf_hist[ptr], &st->lsf_hist[st->lsf_hist_ptr], M*sizeof(*st->lsf_hist));
880 ptr = st->log_en_hist_ptr + 1;
882 if (ptr == DTX_HIST_SIZE)
887 st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */
889 /* compute mean log energy and lsp *
890 * from decoded signal (SID_FIRST) */
892 for (i = M - 1; i >= 0; i--)
897 /* average energy and lsp */
898 for (i = DTX_HIST_SIZE - 1; i >= 0; i--)
900 if (st->log_en_hist[i] < 0)
902 temp = ~((~st->log_en_hist[i]) >> 3);
906 temp = st->log_en_hist[i] >> 3;
908 st->log_en = add_16(st->log_en, temp, pOverflow);
909 for (j = M - 1; j >= 0; j--)
911 L_lsf[j] = L_add(L_lsf[j],
912 (Word32)(st->lsf_hist[i * M + j]), pOverflow);
916 for (j = M - 1; j >= 0; j--)
920 lsf[j] = (Word16)(~((~L_lsf[j]) >> 3));
924 lsf[j] = (Word16)(L_lsf[j] >> 3);
928 Lsf_lsp(lsf, st->lsp, M, pOverflow);
930 /* make log_en speech coder mode independent */
931 /* added again later before synthesis */
932 st->log_en = sub(st->log_en, st->log_en_adjust, pOverflow);
934 /* compute lsf variability vector */
935 oscl_memmove((void *)st->lsf_hist_mean, st->lsf_hist, 80*sizeof(*st->lsf_hist));
937 for (i = M - 1; i >= 0; i--)
940 /* compute mean lsf */
941 for (j = 8 - 1; j >= 0; j--)
943 L_lsf_mean = L_add(L_lsf_mean,
944 (Word32)(st->lsf_hist_mean[i+j*M]), pOverflow);
949 lsf_mean = (Word16)(~((~L_lsf_mean) >> 3));
953 lsf_mean = (Word16)(L_lsf_mean >> 3);
955 /* subtract mean and limit to within reasonable limits *
956 * moreover the upper lsf's are attenuated */
957 for (j = 8 - 1; j >= 0; j--)
960 st->lsf_hist_mean[i+j*M] =
961 sub(st->lsf_hist_mean[i+j*M], lsf_mean, pOverflow);
963 /* attenuate deviation from mean, especially for upper lsf's */
964 st->lsf_hist_mean[i+j*M] =
965 mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i], pOverflow);
967 /* limit the deviation */
968 if (st->lsf_hist_mean[i+j*M] < 0)
976 st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);
978 /* apply soft limit */
979 if (st->lsf_hist_mean[i+j*M] > 655)
981 st->lsf_hist_mean[i+j*M] = 655 + ((st->lsf_hist_mean[i+j*M]
985 /* apply hard limit */
986 if (st->lsf_hist_mean[i+j*M] > 1310)
988 st->lsf_hist_mean[i+j*M] = 1310;
993 st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
1000 if (st->sid_frame != 0)
1002 /* Set old SID parameters, always shift */
1003 /* even if there is no new valid_data */
1004 oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp));
1005 st->old_log_en = st->log_en;
1007 if (st->valid_data != 0) /* new data available (no CRC) */
1009 /* Compute interpolation factor, since the division only works *
1010 * for values of since_last_sid < 32 we have to limit the *
1011 * interpolation to 32 frames */
1012 tmp_int_length = st->since_last_sid;
1013 st->since_last_sid = 0;
1015 if (tmp_int_length >= 32)
1017 tmp_int_length = 32;
1020 L_temp = ((Word32) tmp_int_length) << 10;
1021 if (L_temp != (Word32)((Word16) L_temp))
1024 L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
1026 temp = (Word16) L_temp;
1028 if (tmp_int_length >= 2)
1030 st->true_sid_period_inv = div_s(1 << 10, temp);
1034 st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */
1037 Init_D_plsf_3(lsfState, parm[0], common_amr_tbls->past_rq_init_ptr);
1038 D_plsf_3(lsfState, MRDTX, 0, &parm[1], common_amr_tbls, st->lsp, pOverflow);
1039 /* reset for next speech frame */
1040 oscl_memset((void *)lsfState->past_r_q, 0, M*sizeof(*lsfState->past_r_q));
1042 log_en_index = parm[4];
1043 /* Q11 and divide by 4 */
1044 if ((log_en_index > 63) || (log_en_index < -64))
1046 st->log_en = (log_en_index > 0) ? MAX_16 : MIN_16;
1050 st->log_en = (log_en_index) << (11 - 2);
1053 /* Subtract 2.5 in Q11 */
1054 st->log_en -= (2560 * 2);
1056 /* Index 0 is reserved for silence */
1057 if (log_en_index == 0)
1059 st->log_en = MIN_16;
1062 /* no interpolation at startup after coder reset */
1063 /* or when SID_UPD has been received right after SPEECH */
1065 if ((st->data_updated == 0) ||
1066 (st->dtxGlobalState == SPEECH))
1068 oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp));
1069 st->old_log_en = st->log_en;
1071 } /* endif valid_data */
1073 /* initialize gain predictor memory of other modes */
1076 temp = ~((~st->log_en) >> 1);
1080 temp = st->log_en >> 1;
1082 ma_pred_init = temp - 9000;
1084 if (ma_pred_init > 0)
1088 else if (ma_pred_init < -14436)
1090 ma_pred_init = -14436;
1093 predState->past_qua_en[0] = ma_pred_init;
1094 predState->past_qua_en[1] = ma_pred_init;
1095 predState->past_qua_en[2] = ma_pred_init;
1096 predState->past_qua_en[3] = ma_pred_init;
1098 /* past_qua_en for other modes than MR122 */
1099 ma_pred_init = ((Word32) ma_pred_init * 5443) >> 15;
1101 /* scale down by factor 20*log10(2) in Q15 */
1102 predState->past_qua_en_MR122[0] = ma_pred_init;
1103 predState->past_qua_en_MR122[1] = ma_pred_init;
1104 predState->past_qua_en_MR122[2] = ma_pred_init;
1105 predState->past_qua_en_MR122[3] = ma_pred_init;
1106 } /* endif sid_frame */
1109 /* recompute level adjustment factor Q11 *
1110 * st->log_en_adjust = 0.9*st->log_en_adjust + *
1111 * 0.1*dtx_log_en_adjust[mode]); */
1112 if (dtx_log_en_adjust[mode] > 1023)
1116 else if (dtx_log_en_adjust[mode] < -1024)
1122 temp = (((Word32) dtx_log_en_adjust[mode] << 5) * 3277) >> 15;
1129 temp = ~((~temp) >> 5);
1135 st->log_en_adjust = add_16(((Word32)st->log_en_adjust * 29491) >> 15, temp, pOverflow);
1138 /* Interpolate SID info */
1139 int_fac = shl((st->since_last_sid + 1), 10, pOverflow); /* Q10 */
1140 int_fac = mult(int_fac, st->true_sid_period_inv, pOverflow); /* Q10 * Q15 -> Q10 */
1142 /* Maximize to 1.0 in Q10 */
1147 else if (int_fac < -2048)
1153 int_fac <<= 4; /* Q10 -> Q14 */
1156 L_log_en_int = L_mult(int_fac, st->log_en, pOverflow); /* Q14 * Q11->Q26 */
1157 for (i = M - 1; i >= 0; i--)
1159 lsp_int[i] = mult(int_fac, st->lsp[i], pOverflow);/* Q14 * Q15 -> Q14 */
1162 int_fac = 16384 - int_fac; /* 1-k in Q14 */
1164 /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */
1165 L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en, pOverflow);
1166 for (i = M - 1; i >= 0; i--)
1168 /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
1169 lsp_int[i] = add_16(lsp_int[i], mult(int_fac, st->lsp_old[i], pOverflow), pOverflow);
1171 L_temp = ((Word32) lsp_int[i]) << 1; /* Q14 -> Q15 */
1172 if (L_temp != (Word32)((Word16) L_temp))
1175 L_temp = (Word32)((lsp_int[i] > 0) ? MAX_16 : MIN_16);
1177 lsp_int[i] = (Word16) L_temp;
1180 /* compute the amount of lsf variability */
1181 lsf_variab_factor = st->log_pg_mean - 2457; /* -0.6 in Q12 */
1182 /* *0.3 Q12*Q15 -> Q12 */
1183 lsf_variab_factor = 4096 - mult(lsf_variab_factor, 9830, pOverflow);
1185 /* limit to values between 0..1 in Q12 */
1186 if (lsf_variab_factor > 4095)
1188 lsf_variab_factor = MAX_16;
1190 else if (lsf_variab_factor < 0)
1192 lsf_variab_factor = 0;
1196 lsf_variab_factor <<= 3; /* -> Q15 */
1199 /* get index of vector to do variability with */
1200 lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);
1202 /* convert to lsf */
1203 Lsp_lsf(lsp_int, lsf_int, M, pOverflow);
1205 /* apply lsf variability */
1206 oscl_memmove((void *)lsf_int_variab, lsf_int, M*sizeof(*lsf_int));
1207 for (i = M - 1; i >= 0; i--)
1209 lsf_int_variab[i] = add_16(lsf_int_variab[i],
1210 mult(lsf_variab_factor,
1211 st->lsf_hist_mean[i+lsf_variab_index*M], pOverflow)
1215 /* make sure that LSP's are ordered */
1216 Reorder_lsf(lsf_int, LSF_GAP, M, pOverflow);
1217 Reorder_lsf(lsf_int_variab, LSF_GAP, M, pOverflow);
1219 /* copy lsf to speech decoders lsf state */
1220 oscl_memmove((void *)lsfState->past_lsf_q, lsf_int, M*sizeof(*lsf_int));
1222 /* convert to lsp */
1223 Lsf_lsp(lsf_int, lsp_int, M, pOverflow);
1224 Lsf_lsp(lsf_int_variab, lsp_int_variab, M, pOverflow);
1226 /* Compute acoeffs Q12 acoeff is used for level *
1227 * normalization and postfilter, acoeff_variab is *
1228 * used for synthesis filter *
1229 * by doing this we make sure that the level *
1230 * in high frequenncies does not jump up and down */
1232 Lsp_Az(lsp_int, acoeff, pOverflow);
1233 Lsp_Az(lsp_int_variab, acoeff_variab, pOverflow);
1235 /* For use in postfilter */
1236 oscl_memmove((void *)&A_t[0], acoeff, (M + 1)*sizeof(*acoeff));
1237 oscl_memmove((void *)&A_t[M + 1], acoeff, (M + 1)*sizeof(*acoeff));
1238 oscl_memmove((void *)&A_t[2 *(M + 1)], acoeff, (M + 1)*sizeof(*acoeff));
1239 oscl_memmove((void *)&A_t[3 *(M + 1)], acoeff, (M + 1)*sizeof(*acoeff));
1241 /* Compute reflection coefficients Q15 */
1242 A_Refl(&acoeff[1], refl, pOverflow);
1244 /* Compute prediction error in Q15 */
1245 pred_err = MAX_16; /* 0.99997 in Q15 */
1246 for (i = 0; i < M; i++)
1248 L_temp = (((Word32) refl[i]) * refl[i]) >> 15;
1249 if (L_temp <= 0x00007fffL)
1251 temp = MAX_16 - (Word16) L_temp;
1258 pred_err = mult(pred_err, temp, pOverflow);
1261 /* compute logarithm of prediction gain */
1262 Log2((Word32)(pred_err), &log_pg_e, &log_pg_m, pOverflow);
1264 /* convert exponent and mantissa to Word16 Q12 */
1265 log_pg = shl((log_pg_e - 15), 12, pOverflow); /* Q12 */
1266 log_pg = shr(sub(0, add_16(log_pg, shr(log_pg_m, 15 - 12, pOverflow),
1267 pOverflow), pOverflow), 1, pOverflow);
1268 st->log_pg_mean = add_16(mult(29491, st->log_pg_mean, pOverflow),
1269 mult(3277, log_pg, pOverflow), pOverflow);
1271 /* Compute interpolated log energy */
1272 L_log_en_int = L_shr(L_log_en_int, 10, pOverflow); /* Q26 -> Q16 */
1275 L_log_en_int = L_add(L_log_en_int, 4 * 65536L, pOverflow);
1277 /* subtract prediction gain */
1278 L_log_en_int = L_sub(L_log_en_int, L_shl((Word32)(log_pg), 4, pOverflow), pOverflow);
1280 /* adjust level to speech coder mode */
1281 L_log_en_int = L_add(L_log_en_int,
1282 L_shl((Word32)(st->log_en_adjust), 5, pOverflow), pOverflow);
1284 log_en_int_e = (Word16)(L_log_en_int >> 16);
1286 log_en_int_m = (Word16)(L_shr(L_sub(L_log_en_int,
1287 ((Word32)log_en_int_e << 16), pOverflow), 1, pOverflow));
1288 level = (Word16)(Pow2(log_en_int_e, log_en_int_m, pOverflow)); /* Q4 */
1290 for (i = 0; i < 4; i++)
1292 /* Compute innovation vector */
1293 build_CN_code(&st->L_pn_seed_rx, ex, pOverflow);
1294 for (j = L_SUBFR - 1; j >= 0; j--)
1296 ex[j] = mult(level, ex[j], pOverflow);
1299 Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
1304 /* reset codebook averaging variables */
1305 averState->hangVar = 20;
1306 averState->hangCount = 0;
1308 if (new_state == DTX_MUTE)
1310 /* mute comfort noise as it has been quite a long time since
1311 * last SID update was performed */
1313 tmp_int_length = st->since_last_sid;
1315 if (tmp_int_length > 32)
1317 tmp_int_length = 32;
1319 else if (tmp_int_length <= 0)
1321 /* safety guard against division by zero */
1325 L_temp = ((Word32) tmp_int_length) << 10;
1326 if (L_temp != (Word32)((Word16) L_temp))
1329 L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
1331 temp = (Word16) L_temp;
1333 st->true_sid_period_inv = div_s(1 << 10, temp);
1335 st->since_last_sid = 0;
1336 oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp));
1337 st->old_log_en = st->log_en;
1338 /* subtract 1/8 in Q11 i.e -6/8 dB */
1339 st->log_en = st->log_en - 256;
1342 /* reset interpolation length timer
1343 * if data has been updated. */
1344 if ((st->sid_frame != 0) &&
1345 ((st->valid_data != 0) ||
1346 ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
1348 st->since_last_sid = 0;
1349 st->data_updated = 1;
1356 /****************************************************************************/
1359 ------------------------------------------------------------------------------
1360 FUNCTION NAME: dtx_dec_activity_update
1361 ------------------------------------------------------------------------------
1362 INPUT AND OUTPUT DEFINITIONS
1365 st = pointer to a structure of type dtx_decState
1370 st points to an updated structure of type dtx_decState
1375 Global Variables Used:
1378 Local Variables Needed:
1381 ------------------------------------------------------------------------------
1382 FUNCTION DESCRIPTION
1384 This function updates the DTX parameters.
1386 ------------------------------------------------------------------------------
1391 ------------------------------------------------------------------------------
1394 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
1396 ------------------------------------------------------------------------------
1399 void dtx_dec_activity_update(dtx_decState *st,
1406 Word16 log_en_e, log_en_m, log_en;
1408 // update lsp history
1409 st->lsf_hist_ptr = add(st->lsf_hist_ptr,M);
1410 if (sub(st->lsf_hist_ptr, 80) == 0)
1412 st->lsf_hist_ptr = 0;
1414 Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);
1416 // compute log energy based on frame energy
1417 L_frame_en = 0; // Q0
1418 for (i=0; i < L_FRAME; i++)
1420 L_frame_en = L_mac(L_frame_en, frame[i], frame[i]);
1422 Log2(L_frame_en, &log_en_e, &log_en_m);
1424 // convert exponent and mantissa to Word16 Q10
1425 log_en = shl(log_en_e, 10); // Q10
1426 log_en = add(log_en, shr(log_en_m, 15-10));
1428 // divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193
1429 log_en = sub(log_en, 7497+1024);
1431 // insert into log energy buffer, no division by two as *
1432 * log_en in decoder is Q11
1433 st->log_en_hist_ptr = add(st->log_en_hist_ptr, 1);
1434 if (sub(st->log_en_hist_ptr, DTX_HIST_SIZE) == 0)
1436 st->log_en_hist_ptr = 0;
1438 st->log_en_hist[st->log_en_hist_ptr] = log_en; // Q11
1441 ------------------------------------------------------------------------------
1443 [State any special notes, constraints or cautions for users of this function]
1445 ------------------------------------------------------------------------------
1448 void dtx_dec_activity_update(dtx_decState *st,
1458 Word16 log_en_m = 0;
1461 /* update lsp history */
1462 st->lsf_hist_ptr += M;
1464 if (st->lsf_hist_ptr == 80)
1466 st->lsf_hist_ptr = 0;
1468 oscl_memmove((void *)&st->lsf_hist[st->lsf_hist_ptr], lsf, M*sizeof(*lsf));
1470 /* compute log energy based on frame energy */
1471 L_frame_en = 0; /* Q0 */
1472 for (i = L_FRAME - 1; i >= 0; i--)
1474 L_temp = ((Word32) frame[i]) * frame[i];
1475 if (L_temp != (Word32) 0x40000000L)
1477 L_temp = L_temp << 1;
1483 L_frame_en = L_add(L_frame_en, L_temp, pOverflow);
1485 Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow);
1487 /* convert exponent and mantissa to Word16 Q10 */
1488 L_temp = ((Word32) log_en_e) << 10;
1490 if (L_temp != (Word32)((Word16) L_temp))
1493 L_temp = (Word32)((log_en_e > 0) ? MAX_16 : MIN_16);
1495 log_en_e = (Word16) L_temp;
1499 log_en_m = ~((~log_en_m) >> 5);
1505 log_en = log_en_e + log_en_m;
1507 /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
1508 log_en -= 7497 + 1024;
1510 /* insert into log energy buffer, no division by two as *
1511 * log_en in decoder is Q11 */
1512 st->log_en_hist_ptr += 1;
1514 if (st->log_en_hist_ptr == DTX_HIST_SIZE)
1516 st->log_en_hist_ptr = 0;
1518 st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */
1523 /****************************************************************************/
1526 ------------------------------------------------------------------------------
1527 FUNCTION NAME: rx_dtx_handler
1528 ------------------------------------------------------------------------------
1529 INPUT AND OUTPUT DEFINITIONS
1532 st = pointer to a structure of type dtx_decState
1533 frame_type = RX frame type
1536 newState = variable of type DTXStateType
1539 st points to an updated structure of type dtx_decState
1541 Global Variables Used:
1544 Local Variables Needed:
1547 ------------------------------------------------------------------------------
1548 FUNCTION DESCRIPTION
1550 This function determines the new state of the decoder based on the frame_type
1551 and sets up the decoder parameters according to newState.
1553 Table of new SPD synthesis states
1555 | previous SPD_synthesis_state
1557 frame_type | SPEECH | DTX | DTX_MUTE
1558 ---------------------------------------------------------------
1559 RX_SPEECH_GOOD , | | |
1560 RX_SPEECH_PR_DEGRADED | SPEECH | SPEECH | SPEECH
1561 ----------------------------------------------------------------
1562 RX_SPEECH_PR_BAD, | | |
1563 RX_SPEECH_BAD, | SPEECH | DTX | DTX_MUTE
1564 ----------------------------------------------------------------
1565 RX_SID_FIRST, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
1566 ----------------------------------------------------------------
1567 RX_SID_UPDATE, | DTX | DTX | DTX
1568 ----------------------------------------------------------------
1569 RX_SID_BAD, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
1570 ----------------------------------------------------------------
1571 RX_NO_DATA | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
1573 ----------------------------------------------------------------
1574 RX_ONSET | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
1576 ----------------------------------------------------------------
1578 ------------------------------------------------------------------------------
1583 ------------------------------------------------------------------------------
1586 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
1588 ------------------------------------------------------------------------------
1591 enum DTXStateType rx_dtx_handler(
1592 dtx_decState *st, // i/o : State struct
1593 enum RXFrameType frame_type // i : Frame type
1596 enum DTXStateType newState;
1597 enum DTXStateType encState;
1599 // DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH)
1600 if ((sub(frame_type, RX_SID_FIRST) == 0) ||
1601 (sub(frame_type, RX_SID_UPDATE) == 0) ||
1602 (sub(frame_type, RX_SID_BAD) == 0) ||
1603 (((sub(st->dtxGlobalState, DTX) == 0) ||
1604 (sub(st->dtxGlobalState, DTX_MUTE) == 0)) &&
1605 ((sub(frame_type, RX_NO_DATA) == 0) ||
1606 (sub(frame_type, RX_SPEECH_BAD) == 0) ||
1607 (sub(frame_type, RX_ONSET) == 0))))
1611 // stay in mute for these input types
1612 if ((sub(st->dtxGlobalState, DTX_MUTE) == 0) &&
1613 ((sub(frame_type, RX_SID_BAD) == 0) ||
1614 (sub(frame_type, RX_SID_FIRST) == 0) ||
1615 (sub(frame_type, RX_ONSET) == 0) ||
1616 (sub(frame_type, RX_NO_DATA) == 0)))
1618 newState = DTX_MUTE;
1621 // evaluate if noise parameters are too old
1622 // since_last_sid is reset when CN parameters have been updated
1623 st->since_last_sid = add(st->since_last_sid, 1);
1625 // no update of sid parameters in DTX for a long while
1626 // Due to the delayed update of st->since_last_sid counter
1627 // SID_UPDATE frames need to be handled separately to avoid
1628 // entering DTX_MUTE for late SID_UPDATE frames
1629 if((sub(frame_type, RX_SID_UPDATE) != 0) &&
1630 (sub(st->since_last_sid, DTX_MAX_EMPTY_THRESH) > 0))
1632 newState = DTX_MUTE;
1638 st->since_last_sid = 0;
1641 // reset the decAnaElapsed Counter when receiving CNI data the first
1642 // time, to robustify counter missmatch after handover
1643 // this might delay the bwd CNI analysis in the new decoder slightly.
1645 if ((st->data_updated == 0) &&
1646 (sub(frame_type, RX_SID_UPDATE) == 0))
1648 st->decAnaElapsedCount = 0;
1651 // update the SPE-SPD DTX hangover synchronization
1652 // to know when SPE has added dtx hangover
1653 st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1);
1654 st->dtxHangoverAdded = 0;
1656 if ((sub(frame_type, RX_SID_FIRST) == 0) ||
1657 (sub(frame_type, RX_SID_UPDATE) == 0) ||
1658 (sub(frame_type, RX_SID_BAD) == 0) ||
1659 (sub(frame_type, RX_ONSET) == 0) ||
1660 (sub(frame_type, RX_NO_DATA) == 0))
1664 // In frame errors simulations RX_NO_DATA may occasionally mean that
1665 // a speech packet was probably sent by the encoder,
1666 // the assumed _encoder_ state should be SPEECH in such cases.
1667 if((sub(frame_type, RX_NO_DATA) == 0) &&
1668 (sub(newState, SPEECH) == 0))
1673 // Note on RX_ONSET operation differing from RX_NO_DATA operation:
1674 // If a RX_ONSET is received in the decoder (by "accident")
1675 // it is still most likely that the encoder state
1676 // for the "ONSET frame" was DTX.
1684 if (sub(encState, SPEECH) == 0)
1686 st->dtxHangoverCount = DTX_HANG_CONST;
1690 if (sub(st->decAnaElapsedCount, DTX_ELAPSED_FRAMES_THRESH) > 0)
1692 st->dtxHangoverAdded = 1;
1693 st->decAnaElapsedCount = 0;
1694 st->dtxHangoverCount = 0;
1696 else if (st->dtxHangoverCount == 0)
1698 st->decAnaElapsedCount = 0;
1702 st->dtxHangoverCount = sub(st->dtxHangoverCount, 1);
1706 if (sub(newState, SPEECH) != 0)
1709 // CN data is not in a first SID, first SIDs are marked as SID_BAD
1710 // but will do backwards analysis if a hangover period has been added
1711 // according to the state machine above
1716 if (sub(frame_type, RX_SID_FIRST) == 0)
1720 else if (sub(frame_type, RX_SID_UPDATE) == 0)
1725 else if (sub(frame_type, RX_SID_BAD) == 0)
1728 st->dtxHangoverAdded = 0; // use old data
1733 // newState is used by both SPEECH AND DTX synthesis routines
1736 ------------------------------------------------------------------------------
1738 [State any special notes, constraints or cautions for users of this function]
1740 ------------------------------------------------------------------------------
1743 enum DTXStateType rx_dtx_handler(
1744 dtx_decState *st, /* i/o : State struct */
1745 enum RXFrameType frame_type,/* i : Frame type */
1748 enum DTXStateType newState;
1749 enum DTXStateType encState;
1752 /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */
1754 if ((frame_type == RX_SID_FIRST) ||
1755 (frame_type == RX_SID_UPDATE) ||
1756 (frame_type == RX_SID_BAD) ||
1757 (((st->dtxGlobalState == DTX) || (st->dtxGlobalState == DTX_MUTE)) &&
1758 ((frame_type == RX_NO_DATA) || (frame_type == RX_SPEECH_BAD) ||
1759 (frame_type == RX_ONSET))))
1763 /* stay in mute for these input types */
1765 if ((st->dtxGlobalState == DTX_MUTE) &&
1766 ((frame_type == RX_SID_BAD) ||
1767 (frame_type == RX_SID_FIRST) ||
1768 (frame_type == RX_ONSET) ||
1769 (frame_type == RX_NO_DATA)))
1771 newState = DTX_MUTE;
1774 /* evaluate if noise parameters are too old */
1775 /* since_last_sid is reset when CN parameters have been updated */
1776 st->since_last_sid += 1;
1778 /* no update of sid parameters in DTX for a long while */
1779 /* Due to the delayed update of st->since_last_sid counter */
1780 /* SID_UPDATE frames need to be handled separately to avoid */
1781 /* entering DTX_MUTE for late SID_UPDATE frames */
1782 if ((frame_type != RX_SID_UPDATE) &&
1783 (st->since_last_sid > DTX_MAX_EMPTY_THRESH))
1785 newState = DTX_MUTE;
1791 st->since_last_sid = 0;
1795 reset the decAnaElapsed Counter when receiving CNI data the first
1796 time, to robustify counter missmatch after handover
1797 this might delay the bwd CNI analysis in the new decoder slightly.
1800 if ((st->data_updated == 0) &&
1801 (frame_type == RX_SID_UPDATE))
1803 st->decAnaElapsedCount = 0;
1806 /* update the SPE-SPD DTX hangover synchronization */
1807 /* to know when SPE has added dtx hangover */
1808 st->decAnaElapsedCount = add_16(st->decAnaElapsedCount, 1, pOverflow);
1809 st->dtxHangoverAdded = 0;
1811 if ((frame_type == RX_SID_FIRST) ||
1812 (frame_type == RX_SID_UPDATE) ||
1813 (frame_type == RX_SID_BAD) ||
1814 (frame_type == RX_ONSET) ||
1815 (frame_type == RX_NO_DATA))
1820 In frame errors simulations RX_NO_DATA may occasionally mean that
1821 a speech packet was probably sent by the encoder,
1822 the assumed _encoder_ state should be SPEECH in such cases.
1824 if ((frame_type == RX_NO_DATA) &&
1825 (newState == SPEECH))
1831 Note on RX_ONSET operation differing from RX_NO_DATA operation:
1832 If a RX_ONSET is received in the decoder (by "accident")
1833 it is still most likely that the encoder state
1834 for the "ONSET frame" was DTX.
1843 if (encState == SPEECH)
1845 st->dtxHangoverCount = DTX_HANG_CONST;
1850 if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH)
1852 st->dtxHangoverAdded = 1;
1853 st->decAnaElapsedCount = 0;
1854 st->dtxHangoverCount = 0;
1856 else if (st->dtxHangoverCount == 0)
1858 st->decAnaElapsedCount = 0;
1862 st->dtxHangoverCount -= 1;
1866 if (newState != SPEECH)
1869 * CN data is not in a first SID, first SIDs are marked as SID_BAD
1870 * but will do backwards analysis if a hangover period has been added
1871 * according to the state machine above
1877 if (frame_type == RX_SID_FIRST)
1881 else if (frame_type == RX_SID_UPDATE)
1886 else if (frame_type == RX_SID_BAD)
1889 st->dtxHangoverAdded = 0; /* use old data */
1893 /* newState is used by both SPEECH AND DTX synthesis routines */