Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / dtx_dec.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: dtx_dec.cpp
35  Functions:
36            dtx_dec_reset
37            dtx_dec
38            dtx_dec_activity_update
39            rx_dtx_handler
40
41 ------------------------------------------------------------------------------
42  MODULE DESCRIPTION
43
44  These modules decode the comfort noise when in DTX.
45
46 ------------------------------------------------------------------------------
47 */
48
49
50 /*----------------------------------------------------------------------------
51 ; INCLUDES
52 ----------------------------------------------------------------------------*/
53 #include "dtx_dec.h"
54 #include "typedef.h"
55 #include "basic_op.h"
56 #include "set_zero.h"
57 #include "mode.h"
58 #include "log2.h"
59 #include "lsp_az.h"
60 #include "pow2.h"
61 #include "a_refl.h"
62 #include "b_cn_cod.h"
63 #include "syn_filt.h"
64 #include "lsp_lsf.h"
65 #include "reorder.h"
66 #include "oscl_mem.h"
67
68 /*----------------------------------------------------------------------------
69 ; MACROS
70 ; Define module specific macros here
71 ----------------------------------------------------------------------------*/
72
73
74 /*----------------------------------------------------------------------------
75 ; DEFINES
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  */
80
81 /*----------------------------------------------------------------------------
82 ; LOCAL FUNCTION DEFINITIONS
83 ; Function Prototype declaration
84 ----------------------------------------------------------------------------*/
85
86
87 /*----------------------------------------------------------------------------
88 ; LOCAL VARIABLE DEFINITIONS
89 ; Variable declaration - defined here and used outside this module
90 ----------------------------------------------------------------------------*/
91
92 /***************************************************
93  * Scaling factors for the lsp variability operation *
94  ***************************************************/
95 static const Word16 lsf_hist_mean_scale[M] =
96 {
97     20000,
98     20000,
99     20000,
100     20000,
101     20000,
102     18000,
103     16384,
104     8192,
105     0,
106     0
107 };
108
109 /*************************************************
110  * level adjustment for different modes Q11      *
111  *************************************************/
112 static const Word16 dtx_log_en_adjust[9] =
113 {
114     -1023, /* MR475 */
115     -878, /* MR515 */
116     -732, /* MR59  */
117     -586, /* MR67  */
118     -440, /* MR74  */
119     -294, /* MR795 */
120     -148, /* MR102 */
121     0, /* MR122 */
122     0, /* MRDTX */
123 };
124
125
126 /*
127 ------------------------------------------------------------------------------
128  FUNCTION NAME: dtx_dec_reset
129 ------------------------------------------------------------------------------
130  INPUT AND OUTPUT DEFINITIONS
131
132  Inputs:
133     st = pointer to a structure of type dtx_decState
134
135  Outputs:
136     Structure pointed to by st is initialized to a set of initial values.
137
138  Returns:
139     return_value = 0 if memory was successfully initialized,
140         otherwise returns -1 (int)
141
142  Global Variables Used:
143     None.
144
145  Local Variables Needed:
146     None.
147
148 ------------------------------------------------------------------------------
149  FUNCTION DESCRIPTION
150
151  Reset of state memory for dtx_dec.
152
153 ------------------------------------------------------------------------------
154  REQUIREMENTS
155
156  None.
157
158 ------------------------------------------------------------------------------
159  REFERENCES
160
161  dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
162
163 ------------------------------------------------------------------------------
164  PSEUDO-CODE
165
166 int dtx_dec_reset (dtx_decState *st)
167 {
168    int i;
169
170    if (st == (dtx_decState *) NULL){
171       fprintf(stderr, "dtx_dec_reset: invalid parameter\n");
172       return -1;
173    }
174
175    st->since_last_sid = 0;
176    st->true_sid_period_inv = (1 << 13);
177
178    st->log_en = 3500;
179    st->old_log_en = 3500;
180    // low level noise for better performance in  DTX handover cases
181
182    st->L_pn_seed_rx = PN_INITIAL_SEED;
183
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);
187
188    st->lsf_hist_ptr = 0;
189    st->log_pg_mean = 0;
190    st->log_en_hist_ptr = 0;
191
192    // initialize decoder lsf history
193    Copy(mean_lsf, &st->lsf_hist[0], M);
194
195    for (i = 1; i < DTX_HIST_SIZE; i++)
196    {
197       Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
198    }
199    Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE);
200
201    // initialize decoder log frame energy
202    for (i = 0; i < DTX_HIST_SIZE; i++)
203    {
204       st->log_en_hist[i] = st->log_en;
205    }
206
207    st->log_en_adjust = 0;
208
209    st->dtxHangoverCount = DTX_HANG_CONST;
210    st->decAnaElapsedCount = 32767;
211
212    st->sid_frame = 0;
213    st->valid_data = 0;
214    st->dtxHangoverAdded = 0;
215
216    st->dtxGlobalState = DTX;
217    st->data_updated = 0;
218    return 0;
219 }
220
221 ------------------------------------------------------------------------------
222  CAUTION [optional]
223  [State any special notes, constraints or cautions for users of this function]
224
225 ------------------------------------------------------------------------------
226 */
227
228 Word16 dtx_dec_reset(dtx_decState *st)
229 {
230     Word16 i;
231
232     if (st == (dtx_decState *) NULL)
233     {
234         /* fprint(stderr, "dtx_dec_reset: invalid parameter\n");  */
235         return(-1);
236     }
237
238     st->since_last_sid = 0;
239     st->true_sid_period_inv = (1 << 13);
240
241     st->log_en = 3500;
242     st->old_log_en = 3500;
243     /* low level noise for better performance in  DTX handover cases*/
244
245     st->L_pn_seed_rx = PN_INITIAL_SEED;
246
247     /* Initialize state->lsp [] */
248     st->lsp[0] = 30000;
249     st->lsp[1] = 26000;
250     st->lsp[2] = 21000;
251     st->lsp[3] = 15000;
252     st->lsp[4] = 8000;
253     st->lsp[5] = 0;
254     st->lsp[6] = -8000;
255     st->lsp[7] = -15000;
256     st->lsp[8] = -21000;
257     st->lsp[9] = -26000;
258
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;
265     st->lsp_old[5] = 0;
266     st->lsp_old[6] = -8000;
267     st->lsp_old[7] = -15000;
268     st->lsp_old[8] = -21000;
269     st->lsp_old[9] = -26000;
270
271     st->lsf_hist_ptr = 0;
272     st->log_pg_mean = 0;
273     st->log_en_hist_ptr = 0;
274
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;
286
287     for (i = 1; i < DTX_HIST_SIZE; i++)
288     {
289         oscl_memmove((void *)&st->lsf_hist[M*i], &st->lsf_hist[0], M*sizeof(st->lsf_hist[0]));
290     }
291     oscl_memset(st->lsf_hist_mean, 0, sizeof(Word16)*M*DTX_HIST_SIZE);
292
293     /* initialize decoder log frame energy */
294     for (i = 0; i < DTX_HIST_SIZE; i++)
295     {
296         st->log_en_hist[i] = st->log_en;
297     }
298
299     st->log_en_adjust = 0;
300
301     st->dtxHangoverCount = DTX_HANG_CONST;
302     st->decAnaElapsedCount = 32767;
303
304     st->sid_frame = 0;
305     st->valid_data = 0;
306     st->dtxHangoverAdded = 0;
307
308     st->dtxGlobalState = DTX;
309     st->data_updated = 0;
310
311     return(0);
312 }
313
314 /****************************************************************************/
315
316 /*
317 ------------------------------------------------------------------------------
318  FUNCTION NAME: dtx_dec
319 ------------------------------------------------------------------------------
320  INPUT AND OUTPUT DEFINITIONS
321
322  Inputs:
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
329     mode = AMR mode
330     parm = Vector of synthesis parameters
331
332  Outputs:
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
340
341  Returns:
342     return_value = 0 (int)
343
344  Global Variables Used:
345     None.
346
347  Local Variables Needed:
348     None.
349
350 ------------------------------------------------------------------------------
351  FUNCTION DESCRIPTION
352
353  Decode comfort noise when in DTX.
354
355 ------------------------------------------------------------------------------
356  REQUIREMENTS
357
358  None
359
360 ------------------------------------------------------------------------------
361  REFERENCES
362
363  dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
364
365 ------------------------------------------------------------------------------
366  PSEUDO-CODE
367
368 int dtx_dec(
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
379    )
380 {
381    Word16 log_en_index;
382    Word16 i, j;
383    Word16 int_fac;
384    Word32 L_log_en_int;
385    Word16 lsp_int[M];
386    Word16 log_en_int_e;
387    Word16 log_en_int_m;
388    Word16 level;
389    Word16 acoeff[M + 1];
390    Word16 refl[M];
391    Word16 pred_err;
392    Word16 ex[L_SUBFR];
393    Word16 ma_pred_init;
394    Word16 log_pg_e, log_pg_m;
395    Word16 log_pg;
396    Flag negative;
397    Word16 lsf_mean;
398    Word32 L_lsf_mean;
399    Word16 lsf_variab_index;
400    Word16 lsf_variab_factor;
401    Word16 lsf_int[M];
402    Word16 lsf_int_variab[M];
403    Word16 lsp_int_variab[M];
404    Word16 acoeff_variab[M + 1];
405
406    Word16 lsf[M];
407    Word32 L_lsf[M];
408    Word16 ptr;
409    Word16 tmp_int_length;
410
411
412     // This function is called if synthesis state is not SPEECH
413     // the globally passed  inputs to this function are
414     // st->sid_frame
415     // st->valid_data
416     // st->dtxHangoverAdded
417     // new_state  (SPEECH, DTX, DTX_MUTE)
418
419    if ((st->dtxHangoverAdded != 0) &&
420        (st->sid_frame != 0))
421    {
422       // sid_first after dtx hangover period
423       // or sid_upd after dtxhangover
424
425       // set log_en_adjust to correct value
426       st->log_en_adjust = dtx_log_en_adjust[mode];
427
428       ptr = add(st->lsf_hist_ptr, M);
429       if (sub(ptr, 80) == 0)
430       {
431          ptr = 0;
432       }
433       Copy( &st->lsf_hist[st->lsf_hist_ptr],&st->lsf_hist[ptr],M);
434
435       ptr = add(st->log_en_hist_ptr,1);
436       if (sub(ptr, DTX_HIST_SIZE) == 0)
437       {
438          ptr = 0;
439       }
440       st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; // Q11
441
442       // compute mean log energy and lsp
443       // from decoded signal (SID_FIRST)
444       st->log_en = 0;
445       for (i = 0; i < M; i++)
446       {
447          L_lsf[i] = 0;
448       }
449
450       // average energy and lsp
451       for (i = 0; i < DTX_HIST_SIZE; i++)
452       {
453          st->log_en = add(st->log_en,
454                           shr(st->log_en_hist[i],3));
455          for (j = 0; j < M; j++)
456          {
457             L_lsf[j] = L_add(L_lsf[j],
458                              L_deposit_l(st->lsf_hist[i * M + j]));
459          }
460       }
461
462       for (j = 0; j < M; j++)
463       {
464          lsf[j] = extract_l(L_shr(L_lsf[j],3)); // divide by 8
465       }
466
467       Lsf_lsp(lsf, st->lsp, M);
468
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);
472
473       // compute lsf variability vector
474       Copy(st->lsf_hist, st->lsf_hist_mean, 80);
475
476       for (i = 0; i < M; i++)
477       {
478          L_lsf_mean = 0;
479          // compute mean lsf
480          for (j = 0; j < 8; j++)
481          {
482             L_lsf_mean = L_add(L_lsf_mean,
483                                L_deposit_l(st->lsf_hist_mean[i+j*M]));
484          }
485
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++)
490          {
491             // subtract mean
492             st->lsf_hist_mean[i+j*M] =
493                sub(st->lsf_hist_mean[i+j*M], lsf_mean);
494
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]);
498
499             // limit the deviation
500             if (st->lsf_hist_mean[i+j*M] < 0)
501             {
502                negative = 1;
503             }
504             else
505             {
506                negative = 0;
507             }
508             st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);
509
510             // apply soft limit
511             if (sub(st->lsf_hist_mean[i+j*M], 655) > 0)
512             {
513                st->lsf_hist_mean[i+j*M] =
514                   add(655, shr(sub(st->lsf_hist_mean[i+j*M], 655), 2));
515             }
516
517             // apply hard limit
518             if (sub(st->lsf_hist_mean[i+j*M], 1310) > 0)
519             {
520                st->lsf_hist_mean[i+j*M] = 1310;
521             }
522             if (negative != 0)
523             {
524                st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
525             }
526
527          }
528       }
529    }
530
531    if (st->sid_frame != 0 )
532    {
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;
537
538       if (st->valid_data != 0 )  // new data available (no CRC)
539       {
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;
545
546          if (sub(tmp_int_length, 32) > 0)
547          {
548             tmp_int_length = 32;
549          }
550          if (sub(tmp_int_length, 2) >= 0)
551          {
552             st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
553          }
554          else
555          {
556             st->true_sid_period_inv = 1 << 14; // 0.5 it Q15
557          }
558
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
562
563          log_en_index = parm[4];
564          // Q11 and divide by 4
565          st->log_en = shl(log_en_index, (11 - 2));
566
567          // Subtract 2.5 in Q11
568          st->log_en = sub(st->log_en, (2560 * 2));
569
570          // Index 0 is reserved for silence
571          if (log_en_index == 0)
572          {
573             st->log_en = MIN_16;
574          }
575
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)
580              )
581          {
582             Copy(st->lsp, st->lsp_old, M);
583             st->old_log_en = st->log_en;
584          }
585       } // endif valid_data
586
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)
590       {
591          ma_pred_init = 0;
592       }
593       if (sub(ma_pred_init, -14436) < 0)
594       {
595          ma_pred_init = -14436;
596       }
597
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;
602
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;
610    } // endif sid_frame
611
612    // CN generation
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));
618
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
622
623    // Maximize to 1.0 in Q10
624    if (sub(int_fac, 1024) > 0)
625    {
626       int_fac = 1024;
627    }
628    int_fac = shl(int_fac, 4); // Q10 -> Q14
629
630    L_log_en_int = L_mult(int_fac, st->log_en); // Q14 * Q11->Q26
631    for(i = 0; i < M; i++)
632    {
633       lsp_int[i] = mult(int_fac, st->lsp[i]);// Q14 * Q15 -> Q14
634    }
635
636    int_fac = sub(16384, int_fac); // 1-k in Q14
637
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++)
641    {
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
645    }
646
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));
651
652    // limit to values between 0..1 in Q12
653    if (sub(lsf_variab_factor, 4096) > 0)
654    {
655       lsf_variab_factor = 4096;
656    }
657    if (lsf_variab_factor < 0)
658    {
659       lsf_variab_factor = 0;
660    }
661    lsf_variab_factor = shl(lsf_variab_factor, 3); // -> Q15
662
663    // get index of vector to do variability with
664    lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);
665
666    // convert to lsf
667    Lsp_lsf(lsp_int, lsf_int, M);
668
669    // apply lsf variability
670    Copy(lsf_int, lsf_int_variab, M);
671    for(i = 0; i < M; i++)
672    {
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]));
676    }
677
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);
681
682    // copy lsf to speech decoders lsf state
683    Copy(lsf_int, lsfState->past_lsf_q, M);
684
685    // convert to lsp
686    Lsf_lsp(lsf_int, lsp_int, M);
687    Lsf_lsp(lsf_int_variab, lsp_int_variab, M);
688
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
694
695    Lsp_Az(lsp_int, acoeff);
696    Lsp_Az(lsp_int_variab, acoeff_variab);
697
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);
703
704    // Compute reflection coefficients Q15
705    A_Refl(&acoeff[1], refl);
706
707    // Compute prediction error in Q15
708    pred_err = MAX_16; // 0.99997 in Q15
709    for (i = 0; i < M; i++)
710    {
711       pred_err = mult(pred_err, sub(MAX_16, mult(refl[i], refl[i])));
712    }
713
714    // compute logarithm of prediction gain
715    Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m);
716
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),
721                          mult(3277, log_pg));
722
723    // Compute interpolated log energy
724    L_log_en_int = L_shr(L_log_en_int, 10); // Q26 -> Q16
725
726    // Add 4 in Q16
727    L_log_en_int = L_add(L_log_en_int, 4 * 65536L);
728
729    // subtract prediction gain
730    L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4));
731
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));
735
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
740
741    for (i = 0; i < 4; i++)
742    {
743       // Compute innovation vector
744       build_CN_code(&st->L_pn_seed_rx, ex);
745       for (j = 0; j < L_SUBFR; j++)
746       {
747          ex[j] = mult(level, ex[j]);
748       }
749       // Synthesize
750       Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
751                mem_syn, 1);
752
753    } // next i
754
755    // reset codebook averaging variables
756    averState->hangVar = 20;
757    averState->hangCount = 0;
758
759    if (sub(new_state, DTX_MUTE) == 0)
760    {
761       // mute comfort noise as it has been quite a long time since
762        * last SID update  was performed
763
764       tmp_int_length = st->since_last_sid;
765       if (sub(tmp_int_length, 32) > 0)
766       {
767          tmp_int_length = 32;
768       }
769
770       // safety guard against division by zero
771       if(tmp_int_length <= 0) {
772          tmp_int_length = 8;
773       }
774
775       st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
776
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);
782    }
783
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)))
789    {
790       st->since_last_sid =  0;
791       st->data_updated = 1;
792    }
793
794    return 0;
795 }
796
797
798 ------------------------------------------------------------------------------
799  CAUTION [optional]
800  [State any special notes, constraints or cautions for users of this function]
801
802 ------------------------------------------------------------------------------
803 */
804
805 void dtx_dec(
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*/
817     Flag   *pOverflow
818 )
819 {
820     Word16 log_en_index;
821     Word16 i;
822     Word16 j;
823     Word16 int_fac;
824     Word32 L_log_en_int;
825     Word16 lsp_int[M];
826     Word16 log_en_int_e;
827     Word16 log_en_int_m;
828     Word16 level;
829     Word16 acoeff[M + 1];
830     Word16 refl[M];
831     Word16 pred_err;
832     Word16 ex[L_SUBFR];
833     Word16 ma_pred_init;
834     Word16 log_pg_e;
835     Word16 log_pg_m;
836     Word16 log_pg;
837     Flag negative;
838     Word16 lsf_mean;
839     Word32 L_lsf_mean;
840     Word16 lsf_variab_index;
841     Word16 lsf_variab_factor;
842     Word16 lsf_int[M];
843     Word16 lsf_int_variab[M];
844     Word16 lsp_int_variab[M];
845     Word16 acoeff_variab[M + 1];
846
847     Word16 lsf[M];
848     Word32 L_lsf[M];
849     Word16 ptr;
850     Word16 tmp_int_length;
851
852     Word32 L_temp;
853     Word16 temp;
854
855     /*  This function is called if synthesis state is not SPEECH
856      *  the globally passed  inputs to this function are
857      * st->sid_frame
858      * st->valid_data
859      * st->dtxHangoverAdded
860      * new_state  (SPEECH, DTX, DTX_MUTE)
861      */
862
863     if ((st->dtxHangoverAdded != 0) &&
864             (st->sid_frame != 0))
865     {
866         /* sid_first after dtx hangover period */
867         /* or sid_upd after dtxhangover        */
868
869         /* set log_en_adjust to correct value */
870         st->log_en_adjust = dtx_log_en_adjust[mode];
871
872         ptr = st->lsf_hist_ptr + M;
873
874         if (ptr == 80)
875         {
876             ptr = 0;
877         }
878         oscl_memmove((void *)&st->lsf_hist[ptr], &st->lsf_hist[st->lsf_hist_ptr], M*sizeof(*st->lsf_hist));
879
880         ptr = st->log_en_hist_ptr + 1;
881
882         if (ptr == DTX_HIST_SIZE)
883         {
884             ptr = 0;
885         }
886
887         st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */
888
889         /* compute mean log energy and lsp *
890          * from decoded signal (SID_FIRST) */
891         st->log_en = 0;
892         for (i = M - 1; i >= 0; i--)
893         {
894             L_lsf[i] = 0;
895         }
896
897         /* average energy and lsp */
898         for (i = DTX_HIST_SIZE - 1; i >= 0; i--)
899         {
900             if (st->log_en_hist[i] < 0)
901             {
902                 temp = ~((~st->log_en_hist[i]) >> 3);
903             }
904             else
905             {
906                 temp = st->log_en_hist[i] >> 3;
907             }
908             st->log_en = add_16(st->log_en, temp, pOverflow);
909             for (j = M - 1; j >= 0; j--)
910             {
911                 L_lsf[j] = L_add(L_lsf[j],
912                                  (Word32)(st->lsf_hist[i * M + j]), pOverflow);
913             }
914         }
915
916         for (j = M - 1; j >= 0; j--)
917         {
918             if (L_lsf[j] < 0)
919             {
920                 lsf[j] = (Word16)(~((~L_lsf[j]) >> 3));
921             }
922             else
923             {
924                 lsf[j] = (Word16)(L_lsf[j] >> 3);
925             }
926         }
927
928         Lsf_lsp(lsf, st->lsp, M, pOverflow);
929
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);
933
934         /* compute lsf variability vector */
935         oscl_memmove((void *)st->lsf_hist_mean, st->lsf_hist, 80*sizeof(*st->lsf_hist));
936
937         for (i = M - 1; i >= 0; i--)
938         {
939             L_lsf_mean = 0;
940             /* compute mean lsf */
941             for (j = 8 - 1; j >= 0; j--)
942             {
943                 L_lsf_mean = L_add(L_lsf_mean,
944                                    (Word32)(st->lsf_hist_mean[i+j*M]), pOverflow);
945             }
946
947             if (L_lsf_mean < 0)
948             {
949                 lsf_mean = (Word16)(~((~L_lsf_mean) >> 3));
950             }
951             else
952             {
953                 lsf_mean = (Word16)(L_lsf_mean >> 3);
954             }
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--)
958             {
959                 /* subtract mean */
960                 st->lsf_hist_mean[i+j*M] =
961                     sub(st->lsf_hist_mean[i+j*M], lsf_mean, pOverflow);
962
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);
966
967                 /* limit the deviation */
968                 if (st->lsf_hist_mean[i+j*M] < 0)
969                 {
970                     negative = 1;
971                 }
972                 else
973                 {
974                     negative = 0;
975                 }
976                 st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);
977
978                 /* apply soft limit */
979                 if (st->lsf_hist_mean[i+j*M] > 655)
980                 {
981                     st->lsf_hist_mean[i+j*M] = 655 + ((st->lsf_hist_mean[i+j*M]
982                                                        - 655) >> 2);
983                 }
984
985                 /* apply hard limit */
986                 if (st->lsf_hist_mean[i+j*M] > 1310)
987                 {
988                     st->lsf_hist_mean[i+j*M] = 1310;
989                 }
990
991                 if (negative != 0)
992                 {
993                     st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
994                 }
995             }
996         }
997     }
998
999
1000     if (st->sid_frame != 0)
1001     {
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;
1006
1007         if (st->valid_data != 0)   /* new data available (no CRC) */
1008         {
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;
1014
1015             if (tmp_int_length >= 32)
1016             {
1017                 tmp_int_length = 32;
1018             }
1019
1020             L_temp = ((Word32) tmp_int_length) << 10;
1021             if (L_temp != (Word32)((Word16) L_temp))
1022             {
1023                 *pOverflow = 1;
1024                 L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
1025             }
1026             temp = (Word16) L_temp;
1027
1028             if (tmp_int_length >= 2)
1029             {
1030                 st->true_sid_period_inv = div_s(1 << 10, temp);
1031             }
1032             else
1033             {
1034                 st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */
1035             }
1036
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));
1041
1042             log_en_index = parm[4];
1043             /* Q11 and divide by 4 */
1044             if ((log_en_index > 63) || (log_en_index < -64))
1045             {
1046                 st->log_en = (log_en_index > 0) ? MAX_16 : MIN_16;
1047             }
1048             else
1049             {
1050                 st->log_en = (log_en_index) << (11 - 2);
1051             }
1052
1053             /* Subtract 2.5 in Q11 */
1054             st->log_en -= (2560 * 2);
1055
1056             /* Index 0 is reserved for silence */
1057             if (log_en_index == 0)
1058             {
1059                 st->log_en = MIN_16;
1060             }
1061
1062             /* no interpolation at startup after coder reset        */
1063             /* or when SID_UPD has been received right after SPEECH */
1064
1065             if ((st->data_updated == 0) ||
1066                     (st->dtxGlobalState == SPEECH))
1067             {
1068                 oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp));
1069                 st->old_log_en = st->log_en;
1070             }
1071         } /* endif valid_data */
1072
1073         /* initialize gain predictor memory of other modes */
1074         if (st->log_en < 0)
1075         {
1076             temp = ~((~st->log_en) >> 1);
1077         }
1078         else
1079         {
1080             temp = st->log_en >> 1;
1081         }
1082         ma_pred_init = temp - 9000;
1083
1084         if (ma_pred_init > 0)
1085         {
1086             ma_pred_init = 0;
1087         }
1088         else if (ma_pred_init < -14436)
1089         {
1090             ma_pred_init = -14436;
1091         }
1092
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;
1097
1098         /* past_qua_en for other modes than MR122 */
1099         ma_pred_init = ((Word32) ma_pred_init * 5443) >> 15;
1100
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 */
1107
1108     /* CN generation */
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)
1113     {
1114         temp = MAX_16;
1115     }
1116     else if (dtx_log_en_adjust[mode] < -1024)
1117     {
1118         temp = MIN_16;
1119     }
1120     else
1121     {
1122         temp = (((Word32) dtx_log_en_adjust[mode] << 5) * 3277) >> 15;
1123
1124
1125     }
1126
1127     if (temp < 0)
1128     {
1129         temp = ~((~temp) >> 5);
1130     }
1131     else
1132     {
1133         temp >>= 5;
1134     }
1135     st->log_en_adjust = add_16(((Word32)st->log_en_adjust * 29491) >> 15, temp, pOverflow);
1136
1137
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 */
1141
1142     /* Maximize to 1.0 in Q10 */
1143     if (int_fac > 1024)
1144     {
1145         int_fac = 16384;
1146     }
1147     else if (int_fac < -2048)
1148     {
1149         int_fac = MIN_16;
1150     }
1151     else
1152     {
1153         int_fac <<= 4;      /* Q10 -> Q14 */
1154     }
1155
1156     L_log_en_int = L_mult(int_fac, st->log_en, pOverflow); /* Q14 * Q11->Q26 */
1157     for (i = M - 1; i >= 0; i--)
1158     {
1159         lsp_int[i] = mult(int_fac, st->lsp[i], pOverflow);/* Q14 * Q15 -> Q14 */
1160     }
1161
1162     int_fac = 16384 - int_fac; /* 1-k in Q14 */
1163
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--)
1167     {
1168         /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
1169         lsp_int[i] = add_16(lsp_int[i], mult(int_fac, st->lsp_old[i], pOverflow), pOverflow);
1170
1171         L_temp = ((Word32) lsp_int[i]) << 1;    /* Q14 -> Q15 */
1172         if (L_temp != (Word32)((Word16) L_temp))
1173         {
1174             *pOverflow = 1;
1175             L_temp = (Word32)((lsp_int[i] > 0) ? MAX_16 : MIN_16);
1176         }
1177         lsp_int[i] = (Word16) L_temp;
1178     }
1179
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);
1184
1185     /* limit to values between 0..1 in Q12 */
1186     if (lsf_variab_factor > 4095)
1187     {
1188         lsf_variab_factor = MAX_16;
1189     }
1190     else if (lsf_variab_factor < 0)
1191     {
1192         lsf_variab_factor = 0;
1193     }
1194     else
1195     {
1196         lsf_variab_factor <<= 3; /* -> Q15 */
1197     }
1198
1199     /* get index of vector to do variability with */
1200     lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);
1201
1202     /* convert to lsf */
1203     Lsp_lsf(lsp_int, lsf_int, M, pOverflow);
1204
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--)
1208     {
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)
1212                                    , pOverflow);
1213     }
1214
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);
1218
1219     /* copy lsf to speech decoders lsf state */
1220     oscl_memmove((void *)lsfState->past_lsf_q, lsf_int, M*sizeof(*lsf_int));
1221
1222     /* convert to lsp */
1223     Lsf_lsp(lsf_int, lsp_int, M, pOverflow);
1224     Lsf_lsp(lsf_int_variab, lsp_int_variab, M, pOverflow);
1225
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  */
1231
1232     Lsp_Az(lsp_int, acoeff, pOverflow);
1233     Lsp_Az(lsp_int_variab, acoeff_variab, pOverflow);
1234
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));
1240
1241     /* Compute reflection coefficients Q15 */
1242     A_Refl(&acoeff[1], refl, pOverflow);
1243
1244     /* Compute prediction error in Q15 */
1245     pred_err = MAX_16; /* 0.99997 in Q15 */
1246     for (i = 0; i < M; i++)
1247     {
1248         L_temp = (((Word32) refl[i]) * refl[i]) >> 15;
1249         if (L_temp <= 0x00007fffL)
1250         {
1251             temp = MAX_16 - (Word16) L_temp;
1252         }
1253         else
1254         {
1255             *pOverflow = 1;
1256             temp = 0;
1257         }
1258         pred_err = mult(pred_err, temp, pOverflow);
1259     }
1260
1261     /* compute logarithm of prediction gain */
1262     Log2((Word32)(pred_err), &log_pg_e, &log_pg_m, pOverflow);
1263
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);
1270
1271     /* Compute interpolated log energy */
1272     L_log_en_int = L_shr(L_log_en_int, 10, pOverflow); /* Q26 -> Q16 */
1273
1274     /* Add 4 in Q16 */
1275     L_log_en_int = L_add(L_log_en_int, 4 * 65536L, pOverflow);
1276
1277     /* subtract prediction gain */
1278     L_log_en_int = L_sub(L_log_en_int, L_shl((Word32)(log_pg), 4, pOverflow), pOverflow);
1279
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);
1283
1284     log_en_int_e = (Word16)(L_log_en_int >> 16);
1285
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 */
1289
1290     for (i = 0; i < 4; i++)
1291     {
1292         /* Compute innovation vector */
1293         build_CN_code(&st->L_pn_seed_rx, ex, pOverflow);
1294         for (j = L_SUBFR - 1; j >= 0; j--)
1295         {
1296             ex[j] = mult(level, ex[j], pOverflow);
1297         }
1298         /* Synthesize */
1299         Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
1300                  mem_syn, 1);
1301
1302     } /* next i */
1303
1304     /* reset codebook averaging variables */
1305     averState->hangVar = 20;
1306     averState->hangCount = 0;
1307
1308     if (new_state == DTX_MUTE)
1309     {
1310         /* mute comfort noise as it has been quite a long time since
1311          * last SID update  was performed                            */
1312
1313         tmp_int_length = st->since_last_sid;
1314
1315         if (tmp_int_length > 32)
1316         {
1317             tmp_int_length = 32;
1318         }
1319         else if (tmp_int_length <= 0)
1320         {
1321             /* safety guard against division by zero */
1322             tmp_int_length = 8;
1323         }
1324
1325         L_temp = ((Word32) tmp_int_length) << 10;
1326         if (L_temp != (Word32)((Word16) L_temp))
1327         {
1328             *pOverflow = 1;
1329             L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
1330         }
1331         temp = (Word16) L_temp;
1332
1333         st->true_sid_period_inv = div_s(1 << 10, temp);
1334
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;
1340     }
1341
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)))
1347     {
1348         st->since_last_sid =  0;
1349         st->data_updated = 1;
1350     }
1351
1352     return;
1353 }
1354
1355
1356 /****************************************************************************/
1357
1358 /*
1359 ------------------------------------------------------------------------------
1360  FUNCTION NAME: dtx_dec_activity_update
1361 ------------------------------------------------------------------------------
1362  INPUT AND OUTPUT DEFINITIONS
1363
1364  Inputs:
1365     st = pointer to a structure of type dtx_decState
1366     lsf =
1367         frame =
1368
1369  Outputs:
1370     st points to an updated structure of type dtx_decState
1371
1372  Returns:
1373     None.
1374
1375  Global Variables Used:
1376     None.
1377
1378  Local Variables Needed:
1379     None.
1380
1381 ------------------------------------------------------------------------------
1382  FUNCTION DESCRIPTION
1383
1384  This function updates the DTX parameters.
1385
1386 ------------------------------------------------------------------------------
1387  REQUIREMENTS
1388
1389  None
1390
1391 ------------------------------------------------------------------------------
1392  REFERENCES
1393
1394  dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
1395
1396 ------------------------------------------------------------------------------
1397  PSEUDO-CODE
1398
1399 void dtx_dec_activity_update(dtx_decState *st,
1400                              Word16 lsf[],
1401                              Word16 frame[])
1402 {
1403    Word16 i;
1404
1405    Word32 L_frame_en;
1406    Word16 log_en_e, log_en_m, log_en;
1407
1408    // update lsp history
1409    st->lsf_hist_ptr = add(st->lsf_hist_ptr,M);
1410    if (sub(st->lsf_hist_ptr, 80) == 0)
1411    {
1412       st->lsf_hist_ptr = 0;
1413    }
1414    Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);
1415
1416    // compute log energy based on frame energy
1417    L_frame_en = 0;     // Q0
1418    for (i=0; i < L_FRAME; i++)
1419    {
1420       L_frame_en = L_mac(L_frame_en, frame[i], frame[i]);
1421    }
1422    Log2(L_frame_en, &log_en_e, &log_en_m);
1423
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));
1427
1428    // divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193
1429    log_en = sub(log_en, 7497+1024);
1430
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)
1435    {
1436       st->log_en_hist_ptr = 0;
1437    }
1438    st->log_en_hist[st->log_en_hist_ptr] = log_en; // Q11
1439 }
1440
1441 ------------------------------------------------------------------------------
1442  CAUTION [optional]
1443  [State any special notes, constraints or cautions for users of this function]
1444
1445 ------------------------------------------------------------------------------
1446 */
1447
1448 void dtx_dec_activity_update(dtx_decState *st,
1449                              Word16 lsf[],
1450                              Word16 frame[],
1451                              Flag   *pOverflow)
1452 {
1453     Word16 i;
1454
1455     Word32 L_frame_en;
1456     Word32 L_temp;
1457     Word16 log_en_e;
1458     Word16 log_en_m = 0;
1459     Word16 log_en = 0;
1460
1461     /* update lsp history */
1462     st->lsf_hist_ptr += M;
1463
1464     if (st->lsf_hist_ptr == 80)
1465     {
1466         st->lsf_hist_ptr = 0;
1467     }
1468     oscl_memmove((void *)&st->lsf_hist[st->lsf_hist_ptr], lsf, M*sizeof(*lsf));
1469
1470     /* compute log energy based on frame energy */
1471     L_frame_en = 0;     /* Q0 */
1472     for (i = L_FRAME - 1; i >= 0; i--)
1473     {
1474         L_temp = ((Word32) frame[i]) * frame[i];
1475         if (L_temp != (Word32) 0x40000000L)
1476         {
1477             L_temp = L_temp << 1;
1478         }
1479         else
1480         {
1481             L_temp = MAX_32;
1482         }
1483         L_frame_en = L_add(L_frame_en, L_temp, pOverflow);
1484     }
1485     Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow);
1486
1487     /* convert exponent and mantissa to Word16 Q10 */
1488     L_temp = ((Word32) log_en_e) << 10;
1489
1490     if (L_temp != (Word32)((Word16) L_temp))
1491     {
1492         *pOverflow = 1;
1493         L_temp = (Word32)((log_en_e > 0) ? MAX_16 : MIN_16);
1494     }
1495     log_en_e = (Word16) L_temp;
1496
1497     if (log_en_m < 0)
1498     {
1499         log_en_m = ~((~log_en_m) >> 5);
1500     }
1501     else
1502     {
1503         log_en_m >>= 5;
1504     }
1505     log_en = log_en_e + log_en_m;
1506
1507     /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
1508     log_en -= 7497 + 1024;
1509
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;
1513
1514     if (st->log_en_hist_ptr == DTX_HIST_SIZE)
1515     {
1516         st->log_en_hist_ptr = 0;
1517     }
1518     st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */
1519
1520     return;
1521 }
1522
1523 /****************************************************************************/
1524
1525 /*
1526 ------------------------------------------------------------------------------
1527  FUNCTION NAME: rx_dtx_handler
1528 ------------------------------------------------------------------------------
1529  INPUT AND OUTPUT DEFINITIONS
1530
1531  Inputs:
1532     st = pointer to a structure of type dtx_decState
1533     frame_type = RX frame type
1534
1535  Returns:
1536     newState = variable of type DTXStateType
1537
1538  Outputs:
1539     st points to an updated structure of type dtx_decState
1540
1541  Global Variables Used:
1542     None.
1543
1544  Local Variables Needed:
1545     None.
1546
1547 ------------------------------------------------------------------------------
1548  FUNCTION DESCRIPTION
1549
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.
1552
1553  Table of new SPD synthesis states
1554
1555                            |     previous SPD_synthesis_state
1556      Incoming              |
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
1572                            |(class2 garb.)|               |
1573      ----------------------------------------------------------------
1574      RX_ONSET              | SPEECH       | DTX/(DTX_MUTE)| DTX_MUTE
1575                            |(class2 garb.)|               |
1576      ----------------------------------------------------------------
1577
1578 ------------------------------------------------------------------------------
1579  REQUIREMENTS
1580
1581  None
1582
1583 ------------------------------------------------------------------------------
1584  REFERENCES
1585
1586  dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001
1587
1588 ------------------------------------------------------------------------------
1589  PSEUDO-CODE
1590
1591 enum DTXStateType rx_dtx_handler(
1592                       dtx_decState *st,           // i/o : State struct
1593                       enum RXFrameType frame_type // i   : Frame type
1594                       )
1595 {
1596    enum DTXStateType newState;
1597    enum DTXStateType encState;
1598
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))))
1608    {
1609       newState = DTX;
1610
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)))
1617       {
1618          newState = DTX_MUTE;
1619       }
1620
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);
1624
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))
1631       {
1632          newState = DTX_MUTE;
1633       }
1634    }
1635    else
1636    {
1637       newState = SPEECH;
1638       st->since_last_sid = 0;
1639    }
1640
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.
1644
1645    if ((st->data_updated == 0) &&
1646        (sub(frame_type, RX_SID_UPDATE) == 0))
1647    {
1648       st->decAnaElapsedCount = 0;
1649    }
1650
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;
1655
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))
1661    {
1662       encState = DTX;
1663
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))
1669       {
1670          encState = SPEECH;
1671       }
1672
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.
1677
1678    }
1679    else
1680    {
1681       encState = SPEECH;
1682    }
1683
1684    if (sub(encState, SPEECH) == 0)
1685    {
1686       st->dtxHangoverCount = DTX_HANG_CONST;
1687    }
1688    else
1689    {
1690       if (sub(st->decAnaElapsedCount, DTX_ELAPSED_FRAMES_THRESH) > 0)
1691       {
1692          st->dtxHangoverAdded = 1;
1693          st->decAnaElapsedCount = 0;
1694          st->dtxHangoverCount = 0;
1695       }
1696       else if (st->dtxHangoverCount == 0)
1697       {
1698          st->decAnaElapsedCount = 0;
1699       }
1700       else
1701       {
1702          st->dtxHangoverCount = sub(st->dtxHangoverCount, 1);
1703       }
1704    }
1705
1706    if (sub(newState, SPEECH) != 0)
1707    {
1708       // DTX or DTX_MUTE
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
1712
1713       st->sid_frame = 0;
1714       st->valid_data = 0;
1715
1716       if (sub(frame_type, RX_SID_FIRST) == 0)
1717       {
1718          st->sid_frame = 1;
1719       }
1720       else if (sub(frame_type, RX_SID_UPDATE) == 0)
1721       {
1722          st->sid_frame = 1;
1723          st->valid_data = 1;
1724       }
1725       else if (sub(frame_type, RX_SID_BAD) == 0)
1726       {
1727          st->sid_frame = 1;
1728          st->dtxHangoverAdded = 0; // use old data
1729       }
1730    }
1731
1732    return newState;
1733    // newState is used by both SPEECH AND DTX synthesis routines
1734 }
1735
1736 ------------------------------------------------------------------------------
1737  CAUTION [optional]
1738  [State any special notes, constraints or cautions for users of this function]
1739
1740 ------------------------------------------------------------------------------
1741 */
1742
1743 enum DTXStateType rx_dtx_handler(
1744     dtx_decState *st,           /* i/o : State struct     */
1745     enum RXFrameType frame_type,/* i   : Frame type       */
1746     Flag *pOverflow)
1747 {
1748     enum DTXStateType newState;
1749     enum DTXStateType encState;
1750
1751
1752     /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */
1753
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))))
1760     {
1761         newState = DTX;
1762
1763         /* stay in mute for these input types */
1764
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)))
1770         {
1771             newState = DTX_MUTE;
1772         }
1773
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;
1777
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))
1784         {
1785             newState = DTX_MUTE;
1786         }
1787     }
1788     else
1789     {
1790         newState = SPEECH;
1791         st->since_last_sid = 0;
1792     }
1793
1794     /*
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.
1798     */
1799
1800     if ((st->data_updated == 0) &&
1801             (frame_type == RX_SID_UPDATE))
1802     {
1803         st->decAnaElapsedCount = 0;
1804     }
1805
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;
1810
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))
1816     {
1817         encState = DTX;
1818
1819         /*
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.
1823         */
1824         if ((frame_type == RX_NO_DATA) &&
1825                 (newState == SPEECH))
1826         {
1827             encState = SPEECH;
1828         }
1829
1830         /*
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.
1835         */
1836     }
1837     else
1838     {
1839         encState = SPEECH;
1840     }
1841
1842
1843     if (encState == SPEECH)
1844     {
1845         st->dtxHangoverCount = DTX_HANG_CONST;
1846     }
1847     else
1848     {
1849
1850         if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH)
1851         {
1852             st->dtxHangoverAdded = 1;
1853             st->decAnaElapsedCount = 0;
1854             st->dtxHangoverCount = 0;
1855         }
1856         else if (st->dtxHangoverCount == 0)
1857         {
1858             st->decAnaElapsedCount = 0;
1859         }
1860         else
1861         {
1862             st->dtxHangoverCount -= 1;
1863         }
1864     }
1865
1866     if (newState != SPEECH)
1867     {
1868         /* DTX or DTX_MUTE
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
1872         */
1873
1874         st->sid_frame = 0;
1875         st->valid_data = 0;
1876
1877         if (frame_type == RX_SID_FIRST)
1878         {
1879             st->sid_frame = 1;
1880         }
1881         else if (frame_type == RX_SID_UPDATE)
1882         {
1883             st->sid_frame = 1;
1884             st->valid_data = 1;
1885         }
1886         else if (frame_type == RX_SID_BAD)
1887         {
1888             st->sid_frame = 1;
1889             st->dtxHangoverAdded = 0; /* use old data */
1890         }
1891     }
1892
1893     /* newState is used by both SPEECH AND DTX synthesis routines */
1894     return(newState);
1895 }