summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/dec/src/dtx_dec.cpp
blob: 9d04373068a5608bad773c63a95743a47105e230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/****************************************************************************************
Portions of this file are derived from the following 3GPP standard:

    3GPP TS 26.073
    ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
    Available from http://www.3gpp.org

(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
Permission to distribute, modify and use this file under the standard license
terms listed above has been obtained from the copyright holder.
****************************************************************************************/
/*
------------------------------------------------------------------------------



 Pathname: ./audio/gsm-amr/c/src/dtx_dec.c
 Functions:
           dtx_dec_reset
           dtx_dec
           dtx_dec_activity_update
           rx_dtx_handler

------------------------------------------------------------------------------
 MODULE DESCRIPTION

 These modules decode the comfort noise when in DTX.

------------------------------------------------------------------------------
*/


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include <string.h>

#include "dtx_dec.h"
#include "typedef.h"
#include "basic_op.h"
#include "copy.h"
#include "set_zero.h"
#include "mode.h"
#include "log2.h"
#include "lsp_az.h"
#include "pow2.h"
#include "a_refl.h"
#include "b_cn_cod.h"
#include "syn_filt.h"
#include "lsp_lsf.h"
#include "reorder.h"
#include "lsp_tab.h"

/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
#define PN_INITIAL_SEED 0x70816958L   /* Pseudo noise generator seed value  */

/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; LOCAL VARIABLE DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/

/***************************************************
 * Scaling factors for the lsp variability operation *
 ***************************************************/
static const Word16 lsf_hist_mean_scale[M] =
{
    20000,
    20000,
    20000,
    20000,
    20000,
    18000,
    16384,
    8192,
    0,
    0
};

/*************************************************
 * level adjustment for different modes Q11      *
 *************************************************/
static const Word16 dtx_log_en_adjust[9] =
{
    -1023, /* MR475 */
    -878, /* MR515 */
    -732, /* MR59  */
    -586, /* MR67  */
    -440, /* MR74  */
    -294, /* MR795 */
    -148, /* MR102 */
    0, /* MR122 */
    0, /* MRDTX */
};


/*
------------------------------------------------------------------------------
 FUNCTION NAME: dtx_dec_reset
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    st = pointer to a structure of type dtx_decState

 Outputs:
    Structure pointed to by st is initialized to a set of initial values.

 Returns:
    return_value = 0 if memory was successfully initialized,
        otherwise returns -1 (int)

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 Reset of state memory for dtx_dec.

------------------------------------------------------------------------------
 REQUIREMENTS

 None.

------------------------------------------------------------------------------
 REFERENCES

 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

int dtx_dec_reset (dtx_decState *st)
{
   int i;

   if (st == (dtx_decState *) NULL){
      fprintf(stderr, "dtx_dec_reset: invalid parameter\n");
      return -1;
   }

   st->since_last_sid = 0;
   st->true_sid_period_inv = (1 << 13);

   st->log_en = 3500;
   st->old_log_en = 3500;
   // low level noise for better performance in  DTX handover cases

   st->L_pn_seed_rx = PN_INITIAL_SEED;

   // Initialize state->lsp [] and state->lsp_old []
   Copy(lsp_init_data, &st->lsp[0], M);
   Copy(lsp_init_data, &st->lsp_old[0], M);

   st->lsf_hist_ptr = 0;
   st->log_pg_mean = 0;
   st->log_en_hist_ptr = 0;

   // initialize decoder lsf history
   Copy(mean_lsf, &st->lsf_hist[0], M);

   for (i = 1; i < DTX_HIST_SIZE; i++)
   {
      Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
   }
   Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE);

   // initialize decoder log frame energy
   for (i = 0; i < DTX_HIST_SIZE; i++)
   {
      st->log_en_hist[i] = st->log_en;
   }

   st->log_en_adjust = 0;

   st->dtxHangoverCount = DTX_HANG_CONST;
   st->decAnaElapsedCount = 32767;

   st->sid_frame = 0;
   st->valid_data = 0;
   st->dtxHangoverAdded = 0;

   st->dtxGlobalState = DTX;
   st->data_updated = 0;
   return 0;
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

Word16 dtx_dec_reset(dtx_decState *st)
{
    Word16 i;

    if (st == (dtx_decState *) NULL)
    {
        /* fprint(stderr, "dtx_dec_reset: invalid parameter\n");  */
        return(-1);
    }

    st->since_last_sid = 0;
    st->true_sid_period_inv = (1 << 13);

    st->log_en = 3500;
    st->old_log_en = 3500;
    /* low level noise for better performance in  DTX handover cases*/

    st->L_pn_seed_rx = PN_INITIAL_SEED;

    /* Initialize state->lsp [] */
    st->lsp[0] = 30000;
    st->lsp[1] = 26000;
    st->lsp[2] = 21000;
    st->lsp[3] = 15000;
    st->lsp[4] = 8000;
    st->lsp[5] = 0;
    st->lsp[6] = -8000;
    st->lsp[7] = -15000;
    st->lsp[8] = -21000;
    st->lsp[9] = -26000;

    /* Initialize state->lsp_old [] */
    st->lsp_old[0] = 30000;
    st->lsp_old[1] = 26000;
    st->lsp_old[2] = 21000;
    st->lsp_old[3] = 15000;
    st->lsp_old[4] = 8000;
    st->lsp_old[5] = 0;
    st->lsp_old[6] = -8000;
    st->lsp_old[7] = -15000;
    st->lsp_old[8] = -21000;
    st->lsp_old[9] = -26000;

    st->lsf_hist_ptr = 0;
    st->log_pg_mean = 0;
    st->log_en_hist_ptr = 0;

    /* initialize decoder lsf history */
    st->lsf_hist[0] =  1384;
    st->lsf_hist[1] =  2077;
    st->lsf_hist[2] =  3420;
    st->lsf_hist[3] =  5108;
    st->lsf_hist[4] =  6742;
    st->lsf_hist[5] =  8122;
    st->lsf_hist[6] =  9863;
    st->lsf_hist[7] = 11092;
    st->lsf_hist[8] = 12714;
    st->lsf_hist[9] = 13701;

    for (i = 1; i < DTX_HIST_SIZE; i++)
    {
        Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
    }
    memset(st->lsf_hist_mean, 0, sizeof(Word16)*M*DTX_HIST_SIZE);

    /* initialize decoder log frame energy */
    for (i = 0; i < DTX_HIST_SIZE; i++)
    {
        st->log_en_hist[i] = st->log_en;
    }

    st->log_en_adjust = 0;

    st->dtxHangoverCount = DTX_HANG_CONST;
    st->decAnaElapsedCount = 32767;

    st->sid_frame = 0;
    st->valid_data = 0;
    st->dtxHangoverAdded = 0;

    st->dtxGlobalState = DTX;
    st->data_updated = 0;

    return(0);
}

/****************************************************************************/

/*
------------------------------------------------------------------------------
 FUNCTION NAME: dtx_dec
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    st = pointer to a structure of type dtx_decState
    mem_syn = AMR decoder state
    lsfState = decoder lsf states
    predState = prediction states
    averState = CB gain average states
    new_state = new DTX state
    mode = AMR mode
    parm = Vector of synthesis parameters

 Outputs:
    st points to an updated structure of type dtx_decState
    mem_syn = AMR decoder state
    lsfState = decoder lsf states
    predState = prediction states
    averState = CB gain average states
    synth = synthesised speech
    A_t = decoded LP filter in 4 subframes

 Returns:
    return_value = 0 (int)

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 Decode comfort noise when in DTX.

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

int dtx_dec(
   dtx_decState *st,                // i/o : State struct
   Word16 mem_syn[],                // i/o : AMR decoder state
   D_plsfState* lsfState,           // i/o : decoder lsf states
   gc_predState* predState,         // i/o : prediction states
   Cb_gain_averageState* averState, // i/o : CB gain average states
   enum DTXStateType new_state,     // i   : new DTX state
   enum Mode mode,                  // i   : AMR mode
   Word16 parm[],                   // i   : Vector of synthesis parameters
   Word16 synth[],                  // o   : synthesised speech
   Word16 A_t[]                     // o   : decoded LP filter in 4 subframes
   )
{
   Word16 log_en_index;
   Word16 i, j;
   Word16 int_fac;
   Word32 L_log_en_int;
   Word16 lsp_int[M];
   Word16 log_en_int_e;
   Word16 log_en_int_m;
   Word16 level;
   Word16 acoeff[M + 1];
   Word16 refl[M];
   Word16 pred_err;
   Word16 ex[L_SUBFR];
   Word16 ma_pred_init;
   Word16 log_pg_e, log_pg_m;
   Word16 log_pg;
   Flag negative;
   Word16 lsf_mean;
   Word32 L_lsf_mean;
   Word16 lsf_variab_index;
   Word16 lsf_variab_factor;
   Word16 lsf_int[M];
   Word16 lsf_int_variab[M];
   Word16 lsp_int_variab[M];
   Word16 acoeff_variab[M + 1];

   Word16 lsf[M];
   Word32 L_lsf[M];
   Word16 ptr;
   Word16 tmp_int_length;


    // This function is called if synthesis state is not SPEECH
    // the globally passed  inputs to this function are
    // st->sid_frame
    // st->valid_data
    // st->dtxHangoverAdded
    // new_state  (SPEECH, DTX, DTX_MUTE)

   if ((st->dtxHangoverAdded != 0) &&
       (st->sid_frame != 0))
   {
      // sid_first after dtx hangover period
      // or sid_upd after dtxhangover

      // set log_en_adjust to correct value
      st->log_en_adjust = dtx_log_en_adjust[mode];

      ptr = add(st->lsf_hist_ptr, M);
      if (sub(ptr, 80) == 0)
      {
         ptr = 0;
      }
      Copy( &st->lsf_hist[st->lsf_hist_ptr],&st->lsf_hist[ptr],M);

      ptr = add(st->log_en_hist_ptr,1);
      if (sub(ptr, DTX_HIST_SIZE) == 0)
      {
         ptr = 0;
      }
      st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; // Q11

      // compute mean log energy and lsp
      // from decoded signal (SID_FIRST)
      st->log_en = 0;
      for (i = 0; i < M; i++)
      {
         L_lsf[i] = 0;
      }

      // average energy and lsp
      for (i = 0; i < DTX_HIST_SIZE; i++)
      {
         st->log_en = add(st->log_en,
                          shr(st->log_en_hist[i],3));
         for (j = 0; j < M; j++)
         {
            L_lsf[j] = L_add(L_lsf[j],
                             L_deposit_l(st->lsf_hist[i * M + j]));
         }
      }

      for (j = 0; j < M; j++)
      {
         lsf[j] = extract_l(L_shr(L_lsf[j],3)); // divide by 8
      }

      Lsf_lsp(lsf, st->lsp, M);

      // make log_en speech coder mode independent
      // added again later before synthesis
      st->log_en = sub(st->log_en, st->log_en_adjust);

      // compute lsf variability vector
      Copy(st->lsf_hist, st->lsf_hist_mean, 80);

      for (i = 0; i < M; i++)
      {
         L_lsf_mean = 0;
         // compute mean lsf
         for (j = 0; j < 8; j++)
         {
            L_lsf_mean = L_add(L_lsf_mean,
                               L_deposit_l(st->lsf_hist_mean[i+j*M]));
         }

         lsf_mean = extract_l(L_shr(L_lsf_mean, 3));
          // subtract mean and limit to within reasonable limits
          // moreover the upper lsf's are attenuated
         for (j = 0; j < 8; j++)
         {
            // subtract mean
            st->lsf_hist_mean[i+j*M] =
               sub(st->lsf_hist_mean[i+j*M], lsf_mean);

            // attenuate deviation from mean, especially for upper lsf's
            st->lsf_hist_mean[i+j*M] =
               mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i]);

            // limit the deviation
            if (st->lsf_hist_mean[i+j*M] < 0)
            {
               negative = 1;
            }
            else
            {
               negative = 0;
            }
            st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);

            // apply soft limit
            if (sub(st->lsf_hist_mean[i+j*M], 655) > 0)
            {
               st->lsf_hist_mean[i+j*M] =
                  add(655, shr(sub(st->lsf_hist_mean[i+j*M], 655), 2));
            }

            // apply hard limit
            if (sub(st->lsf_hist_mean[i+j*M], 1310) > 0)
            {
               st->lsf_hist_mean[i+j*M] = 1310;
            }
            if (negative != 0)
            {
               st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
            }

         }
      }
   }

   if (st->sid_frame != 0 )
   {
      // Set old SID parameters, always shift
      // even if there is no new valid_data
      Copy(st->lsp, st->lsp_old, M);
      st->old_log_en = st->log_en;

      if (st->valid_data != 0 )  // new data available (no CRC)
      {
         // Compute interpolation factor, since the division only works
         // for values of since_last_sid < 32 we have to limit the
         // interpolation to 32 frames
         tmp_int_length = st->since_last_sid;
         st->since_last_sid = 0;

         if (sub(tmp_int_length, 32) > 0)
         {
            tmp_int_length = 32;
         }
         if (sub(tmp_int_length, 2) >= 0)
         {
            st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));
         }
         else
         {
            st->true_sid_period_inv = 1 << 14; // 0.5 it Q15
         }

         Init_D_plsf_3(lsfState, parm[0]);  // temporay initialization
         D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp);
         Set_zero(lsfState->past_r_q, M);   // reset for next speech frame

         log_en_index = parm[4];
         // Q11 and divide by 4
         st->log_en = shl(log_en_index, (11 - 2));

         // Subtract 2.5 in Q11
         st->log_en = sub(st->log_en, (2560 * 2));

         // Index 0 is reserved for silence
         if (log_en_index == 0)
         {
            st->log_en = MIN_16;
         }

         // no interpolation at startup after coder reset
         // or when SID_UPD has been received right after SPEECH
         if ((st->data_updated == 0) ||
             (sub(st->dtxGlobalState, SPEECH) == 0)
             )
         {
            Copy(st->lsp, st->lsp_old, M);
            st->old_log_en = st->log_en;
         }
      } // endif valid_data

      // initialize gain predictor memory of other modes
      ma_pred_init = sub(shr(st->log_en,1), 9000);
      if (ma_pred_init > 0)
      {
         ma_pred_init = 0;
      }
      if (sub(ma_pred_init, -14436) < 0)
      {
         ma_pred_init = -14436;
      }

      predState->past_qua_en[0] = ma_pred_init;
      predState->past_qua_en[1] = ma_pred_init;
      predState->past_qua_en[2] = ma_pred_init;
      predState->past_qua_en[3] = ma_pred_init;

      // past_qua_en for other modes than MR122
      ma_pred_init = mult(5443, ma_pred_init);
      // scale down by factor 20*log10(2) in Q15
      predState->past_qua_en_MR122[0] = ma_pred_init;
      predState->past_qua_en_MR122[1] = ma_pred_init;
      predState->past_qua_en_MR122[2] = ma_pred_init;
      predState->past_qua_en_MR122[3] = ma_pred_init;
   } // endif sid_frame

   // CN generation
   // recompute level adjustment factor Q11
   // st->log_en_adjust = 0.9*st->log_en_adjust +
   //                     0.1*dtx_log_en_adjust[mode]);
   st->log_en_adjust = add(mult(st->log_en_adjust, 29491),
                           shr(mult(shl(dtx_log_en_adjust[mode],5),3277),5));

   // Interpolate SID info
   int_fac = shl(add(1,st->since_last_sid), 10); // Q10
   int_fac = mult(int_fac, st->true_sid_period_inv); // Q10 * Q15 -> Q10

   // Maximize to 1.0 in Q10
   if (sub(int_fac, 1024) > 0)
   {
      int_fac = 1024;
   }
   int_fac = shl(int_fac, 4); // Q10 -> Q14

   L_log_en_int = L_mult(int_fac, st->log_en); // Q14 * Q11->Q26
   for(i = 0; i < M; i++)
   {
      lsp_int[i] = mult(int_fac, st->lsp[i]);// Q14 * Q15 -> Q14
   }

   int_fac = sub(16384, int_fac); // 1-k in Q14

   // (Q14 * Q11 -> Q26) + Q26 -> Q26
   L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en);
   for(i = 0; i < M; i++)
   {
      // Q14 + (Q14 * Q15 -> Q14) -> Q14
      lsp_int[i] = add(lsp_int[i], mult(int_fac, st->lsp_old[i]));
      lsp_int[i] = shl(lsp_int[i], 1); // Q14 -> Q15
   }

   // compute the amount of lsf variability
   lsf_variab_factor = sub(st->log_pg_mean,2457); // -0.6 in Q12
   // *0.3 Q12*Q15 -> Q12
   lsf_variab_factor = sub(4096, mult(lsf_variab_factor, 9830));

   // limit to values between 0..1 in Q12
   if (sub(lsf_variab_factor, 4096) > 0)
   {
      lsf_variab_factor = 4096;
   }
   if (lsf_variab_factor < 0)
   {
      lsf_variab_factor = 0;
   }
   lsf_variab_factor = shl(lsf_variab_factor, 3); // -> Q15

   // get index of vector to do variability with
   lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);

   // convert to lsf
   Lsp_lsf(lsp_int, lsf_int, M);

   // apply lsf variability
   Copy(lsf_int, lsf_int_variab, M);
   for(i = 0; i < M; i++)
   {
      lsf_int_variab[i] = add(lsf_int_variab[i],
                              mult(lsf_variab_factor,
                                   st->lsf_hist_mean[i+lsf_variab_index*M]));
   }

   // make sure that LSP's are ordered
   Reorder_lsf(lsf_int, LSF_GAP, M);
   Reorder_lsf(lsf_int_variab, LSF_GAP, M);

   // copy lsf to speech decoders lsf state
   Copy(lsf_int, lsfState->past_lsf_q, M);

   // convert to lsp
   Lsf_lsp(lsf_int, lsp_int, M);
   Lsf_lsp(lsf_int_variab, lsp_int_variab, M);

   // Compute acoeffs Q12 acoeff is used for level
   // normalization and postfilter, acoeff_variab is
   // used for synthesis filter
   // by doing this we make sure that the level
   // in high frequenncies does not jump up and down

   Lsp_Az(lsp_int, acoeff);
   Lsp_Az(lsp_int_variab, acoeff_variab);

   // For use in postfilter
   Copy(acoeff, &A_t[0],           M + 1);
   Copy(acoeff, &A_t[M + 1],       M + 1);
   Copy(acoeff, &A_t[2 * (M + 1)], M + 1);
   Copy(acoeff, &A_t[3 * (M + 1)], M + 1);

   // Compute reflection coefficients Q15
   A_Refl(&acoeff[1], refl);

   // Compute prediction error in Q15
   pred_err = MAX_16; // 0.99997 in Q15
   for (i = 0; i < M; i++)
   {
      pred_err = mult(pred_err, sub(MAX_16, mult(refl[i], refl[i])));
   }

   // compute logarithm of prediction gain
   Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m);

   // convert exponent and mantissa to Word16 Q12
   log_pg = shl(sub(log_pg_e,15), 12);  // Q12
   log_pg = shr(sub(0,add(log_pg, shr(log_pg_m, 15-12))), 1);
   st->log_pg_mean = add(mult(29491,st->log_pg_mean),
                         mult(3277, log_pg));

   // Compute interpolated log energy
   L_log_en_int = L_shr(L_log_en_int, 10); // Q26 -> Q16

   // Add 4 in Q16
   L_log_en_int = L_add(L_log_en_int, 4 * 65536L);

   // subtract prediction gain
   L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4));

   // adjust level to speech coder mode
   L_log_en_int = L_add(L_log_en_int,
                        L_shl(L_deposit_l(st->log_en_adjust), 5));

   log_en_int_e = extract_h(L_log_en_int);
   log_en_int_m = extract_l(L_shr(L_sub(L_log_en_int,
                                        L_deposit_h(log_en_int_e)), 1));
   level = extract_l(Pow2(log_en_int_e, log_en_int_m)); // Q4

   for (i = 0; i < 4; i++)
   {
      // Compute innovation vector
      build_CN_code(&st->L_pn_seed_rx, ex);
      for (j = 0; j < L_SUBFR; j++)
      {
         ex[j] = mult(level, ex[j]);
      }
      // Synthesize
      Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
               mem_syn, 1);

   } // next i

   // reset codebook averaging variables
   averState->hangVar = 20;
   averState->hangCount = 0;

   if (sub(new_state, DTX_MUTE) == 0)
   {
      // mute comfort noise as it has been quite a long time since
       * last SID update  was performed

      tmp_int_length = st->since_last_sid;
      if (sub(tmp_int_length, 32) > 0)
      {
         tmp_int_length = 32;
      }

      // safety guard against division by zero
      if(tmp_int_length <= 0) {
         tmp_int_length = 8;
      }

      st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10));

      st->since_last_sid = 0;
      Copy(st->lsp, st->lsp_old, M);
      st->old_log_en = st->log_en;
      // subtract 1/8 in Q11 i.e -6/8 dB
      st->log_en = sub(st->log_en, 256);
   }

   // reset interpolation length timer
   // if data has been updated.
   if ((st->sid_frame != 0) &&
       ((st->valid_data != 0) ||
        ((st->valid_data == 0) &&  (st->dtxHangoverAdded) != 0)))
   {
      st->since_last_sid =  0;
      st->data_updated = 1;
   }

   return 0;
}


------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

void dtx_dec(
    dtx_decState *st,                /* i/o : State struct                    */
    Word16 mem_syn[],                /* i/o : AMR decoder state               */
    D_plsfState* lsfState,           /* i/o : decoder lsf states              */
    gc_predState* predState,         /* i/o : prediction states               */
    Cb_gain_averageState* averState, /* i/o : CB gain average states          */
    enum DTXStateType new_state,     /* i   : new DTX state                   */
    enum Mode mode,                  /* i   : AMR mode                        */
    Word16 parm[],                   /* i   : Vector of synthesis parameters  */
    Word16 synth[],                  /* o   : synthesised speech              */
    Word16 A_t[],                    /* o   : decoded LP filter in 4 subframes*/
    Flag   *pOverflow
)
{
    Word16 log_en_index;
    Word16 i;
    Word16 j;
    Word16 int_fac;
    Word32 L_log_en_int;
    Word16 lsp_int[M];
    Word16 log_en_int_e;
    Word16 log_en_int_m;
    Word16 level;
    Word16 acoeff[M + 1];
    Word16 refl[M];
    Word16 pred_err;
    Word16 ex[L_SUBFR];
    Word16 ma_pred_init;
    Word16 log_pg_e;
    Word16 log_pg_m;
    Word16 log_pg;
    Flag negative;
    Word16 lsf_mean;
    Word32 L_lsf_mean;
    Word16 lsf_variab_index;
    Word16 lsf_variab_factor;
    Word16 lsf_int[M];
    Word16 lsf_int_variab[M];
    Word16 lsp_int_variab[M];
    Word16 acoeff_variab[M + 1];

    Word16 lsf[M];
    Word32 L_lsf[M];
    Word16 ptr;
    Word16 tmp_int_length;

    Word32 L_temp;
    Word16 temp;

    /*  This function is called if synthesis state is not SPEECH
     *  the globally passed  inputs to this function are
     * st->sid_frame
     * st->valid_data
     * st->dtxHangoverAdded
     * new_state  (SPEECH, DTX, DTX_MUTE)
     */

    if ((st->dtxHangoverAdded != 0) &&
            (st->sid_frame != 0))
    {
        /* sid_first after dtx hangover period */
        /* or sid_upd after dtxhangover        */

        /* set log_en_adjust to correct value */
        st->log_en_adjust = dtx_log_en_adjust[mode];

        ptr = st->lsf_hist_ptr + M;

        if (ptr == 80)
        {
            ptr = 0;
        }
        Copy(&st->lsf_hist[st->lsf_hist_ptr], &st->lsf_hist[ptr], M);

        ptr = st->log_en_hist_ptr + 1;

        if (ptr == DTX_HIST_SIZE)
        {
            ptr = 0;
        }

        st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */

        /* compute mean log energy and lsp *
         * from decoded signal (SID_FIRST) */
        st->log_en = 0;
        for (i = M - 1; i >= 0; i--)
        {
            L_lsf[i] = 0;
        }

        /* average energy and lsp */
        for (i = DTX_HIST_SIZE - 1; i >= 0; i--)
        {
            if (st->log_en_hist[i] < 0)
            {
                temp = ~((~st->log_en_hist[i]) >> 3);
            }
            else
            {
                temp = st->log_en_hist[i] >> 3;
            }
            st->log_en = add(st->log_en, temp, pOverflow);
            for (j = M - 1; j >= 0; j--)
            {
                L_lsf[j] = L_add(L_lsf[j],
                                 L_deposit_l(st->lsf_hist[i * M + j]), pOverflow);
            }
        }

        for (j = M - 1; j >= 0; j--)
        {
            if (L_lsf[j] < 0)
            {
                lsf[j] = (Word16)(~((~L_lsf[j]) >> 3));
            }
            else
            {
                lsf[j] = (Word16)(L_lsf[j] >> 3);
            }
        }

        Lsf_lsp(lsf, st->lsp, M, pOverflow);

        /* make log_en speech coder mode independent */
        /* added again later before synthesis        */
        st->log_en = sub(st->log_en, st->log_en_adjust, pOverflow);

        /* compute lsf variability vector */
        Copy(st->lsf_hist, st->lsf_hist_mean, 80);

        for (i = M - 1; i >= 0; i--)
        {
            L_lsf_mean = 0;
            /* compute mean lsf */
            for (j = 8 - 1; j >= 0; j--)
            {
                L_lsf_mean = L_add(L_lsf_mean,
                                   L_deposit_l(st->lsf_hist_mean[i+j*M]), pOverflow);
            }

            if (L_lsf_mean < 0)
            {
                lsf_mean = (Word16)(~((~L_lsf_mean) >> 3));
            }
            else
            {
                lsf_mean = (Word16)(L_lsf_mean >> 3);
            }
            /* subtract mean and limit to within reasonable limits  *
            * moreover the upper lsf's are attenuated              */
            for (j = 8 - 1; j >= 0; j--)
            {
                /* subtract mean */
                st->lsf_hist_mean[i+j*M] =
                    sub(st->lsf_hist_mean[i+j*M], lsf_mean, pOverflow);

                /* attenuate deviation from mean, especially for upper lsf's */
                st->lsf_hist_mean[i+j*M] =
                    mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i], pOverflow);

                /* limit the deviation */
                if (st->lsf_hist_mean[i+j*M] < 0)
                {
                    negative = 1;
                }
                else
                {
                    negative = 0;
                }
                st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]);

                /* apply soft limit */
                if (st->lsf_hist_mean[i+j*M] > 655)
                {
                    st->lsf_hist_mean[i+j*M] = 655 + ((st->lsf_hist_mean[i+j*M]
                                                       - 655) >> 2);
                }

                /* apply hard limit */
                if (st->lsf_hist_mean[i+j*M] > 1310)
                {
                    st->lsf_hist_mean[i+j*M] = 1310;
                }

                if (negative != 0)
                {
                    st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];
                }
            }
        }
    }


    if (st->sid_frame != 0)
    {
        /* Set old SID parameters, always shift */
        /* even if there is no new valid_data   */
        Copy(st->lsp, st->lsp_old, M);
        st->old_log_en = st->log_en;

        if (st->valid_data != 0)   /* new data available (no CRC) */
        {
            /* Compute interpolation factor, since the division only works *
             * for values of since_last_sid < 32 we have to limit the      *
             * interpolation to 32 frames                                  */
            tmp_int_length = st->since_last_sid;
            st->since_last_sid = 0;

            if (tmp_int_length >= 32)
            {
                tmp_int_length = 32;
            }

            L_temp = ((Word32) tmp_int_length) << 10;
            if (L_temp != (Word32)((Word16) L_temp))
            {
                *pOverflow = 1;
                L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
            }
            temp = (Word16) L_temp;

            if (tmp_int_length >= 2)
            {
                st->true_sid_period_inv = div_s(1 << 10, temp);
            }
            else
            {
                st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */
            }

            Init_D_plsf_3(lsfState, parm[0]);
            D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp, pOverflow);
            Set_zero(lsfState->past_r_q, M);   /* reset for next speech frame */

            log_en_index = parm[4];
            /* Q11 and divide by 4 */
            if ((log_en_index > 63) || (log_en_index < -64))
            {
                st->log_en = (log_en_index > 0) ? MAX_16 : MIN_16;
            }
            else
            {
                st->log_en = (log_en_index) << (11 - 2);
            }

            /* Subtract 2.5 in Q11 */
            st->log_en = sub(st->log_en, (2560 * 2), pOverflow);

            /* Index 0 is reserved for silence */
            if (log_en_index == 0)
            {
                st->log_en = MIN_16;
            }

            /* no interpolation at startup after coder reset        */
            /* or when SID_UPD has been received right after SPEECH */

            if ((st->data_updated == 0) ||
                    (st->dtxGlobalState == SPEECH))
            {
                Copy(st->lsp, st->lsp_old, M);
                st->old_log_en = st->log_en;
            }
        } /* endif valid_data */

        /* initialize gain predictor memory of other modes */
        if (st->log_en < 0)
        {
            temp = ~((~st->log_en) >> 1);
        }
        else
        {
            temp = st->log_en >> 1;
        }
        ma_pred_init = sub(temp, 9000, pOverflow);

        if (ma_pred_init > 0)
        {
            ma_pred_init = 0;
        }
        else if (ma_pred_init < -14436)
        {
            ma_pred_init = -14436;
        }

        predState->past_qua_en[0] = ma_pred_init;
        predState->past_qua_en[1] = ma_pred_init;
        predState->past_qua_en[2] = ma_pred_init;
        predState->past_qua_en[3] = ma_pred_init;

        /* past_qua_en for other modes than MR122 */
        ma_pred_init = mult(5443, ma_pred_init, pOverflow);
        /* scale down by factor 20*log10(2) in Q15 */
        predState->past_qua_en_MR122[0] = ma_pred_init;
        predState->past_qua_en_MR122[1] = ma_pred_init;
        predState->past_qua_en_MR122[2] = ma_pred_init;
        predState->past_qua_en_MR122[3] = ma_pred_init;
    } /* endif sid_frame */

    /* CN generation */
    /* recompute level adjustment factor Q11             *
     * st->log_en_adjust = 0.9*st->log_en_adjust +       *
     *                     0.1*dtx_log_en_adjust[mode]); */
    if (dtx_log_en_adjust[mode] > 1023)
    {
        temp = MAX_16;
    }
    else if (dtx_log_en_adjust[mode] < -1024)
    {
        temp = MIN_16;
    }
    else
    {
        temp = mult((Word16)((Word32)dtx_log_en_adjust[mode] << 5), 3277, pOverflow);
    }

    if (temp < 0)
    {
        temp = ~((~temp) >> 5);
    }
    else
    {
        temp >>= 5;
    }
    st->log_en_adjust = add(mult(st->log_en_adjust, 29491, pOverflow), temp, pOverflow);

    /* Interpolate SID info */
    int_fac = shl(add(1, st->since_last_sid, pOverflow), 10, pOverflow); /* Q10 */
    int_fac = mult(int_fac, st->true_sid_period_inv, pOverflow); /* Q10 * Q15 -> Q10 */

    /* Maximize to 1.0 in Q10 */
    if (int_fac > 1024)
    {
        int_fac = 16384;
    }
    else if (int_fac < -2048)
    {
        int_fac = MIN_16;
    }
    else
    {
        int_fac <<= 4;      /* Q10 -> Q14 */
    }

    L_log_en_int = L_mult(int_fac, st->log_en, pOverflow); /* Q14 * Q11->Q26 */
    for (i = M - 1; i >= 0; i--)
    {
        lsp_int[i] = mult(int_fac, st->lsp[i], pOverflow);/* Q14 * Q15 -> Q14 */
    }

    int_fac = sub(16384, int_fac, pOverflow); /* 1-k in Q14 */

    /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */
    L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en, pOverflow);
    for (i = M - 1; i >= 0; i--)
    {
        /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
        lsp_int[i] = add(lsp_int[i], mult(int_fac, st->lsp_old[i], pOverflow), pOverflow);

        L_temp = ((Word32) lsp_int[i]) << 1;    /* Q14 -> Q15 */
        if (L_temp != (Word32)((Word16) L_temp))
        {
            *pOverflow = 1;
            L_temp = (Word32)((lsp_int[i] > 0) ? MAX_16 : MIN_16);
        }
        lsp_int[i] = (Word16) L_temp;
    }

    /* compute the amount of lsf variability */
    lsf_variab_factor = sub(st->log_pg_mean, 2457, pOverflow); /* -0.6 in Q12 */
    /* *0.3 Q12*Q15 -> Q12 */
    lsf_variab_factor = sub(4096, mult(lsf_variab_factor, 9830, pOverflow), pOverflow);

    /* limit to values between 0..1 in Q12 */
    if (lsf_variab_factor > 4095)
    {
        lsf_variab_factor = MAX_16;
    }
    else if (lsf_variab_factor < 0)
    {
        lsf_variab_factor = 0;
    }
    else
    {
        lsf_variab_factor <<= 3; /* -> Q15 */
    }

    /* get index of vector to do variability with */
    lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3);

    /* convert to lsf */
    Lsp_lsf(lsp_int, lsf_int, M, pOverflow);

    /* apply lsf variability */
    Copy(lsf_int, lsf_int_variab, M);
    for (i = M - 1; i >= 0; i--)
    {
        lsf_int_variab[i] = add(lsf_int_variab[i],
                                mult(lsf_variab_factor,
                                     st->lsf_hist_mean[i+lsf_variab_index*M], pOverflow)
                                , pOverflow);
    }

    /* make sure that LSP's are ordered */
    Reorder_lsf(lsf_int, LSF_GAP, M, pOverflow);
    Reorder_lsf(lsf_int_variab, LSF_GAP, M, pOverflow);

    /* copy lsf to speech decoders lsf state */
    Copy(lsf_int, lsfState->past_lsf_q, M);

    /* convert to lsp */
    Lsf_lsp(lsf_int, lsp_int, M, pOverflow);
    Lsf_lsp(lsf_int_variab, lsp_int_variab, M, pOverflow);

    /* Compute acoeffs Q12 acoeff is used for level    *
     * normalization and postfilter, acoeff_variab is  *
     * used for synthesis filter                       *
     * by doing this we make sure that the level       *
     * in high frequenncies does not jump up and down  */

    Lsp_Az(lsp_int, acoeff, pOverflow);
    Lsp_Az(lsp_int_variab, acoeff_variab, pOverflow);

    /* For use in postfilter */
    Copy(acoeff, &A_t[0],           M + 1);
    Copy(acoeff, &A_t[M + 1],       M + 1);
    Copy(acoeff, &A_t[2 *(M + 1)], M + 1);
    Copy(acoeff, &A_t[3 *(M + 1)], M + 1);

    /* Compute reflection coefficients Q15 */
    A_Refl(&acoeff[1], refl, pOverflow);

    /* Compute prediction error in Q15 */
    pred_err = MAX_16; /* 0.99997 in Q15 */
    for (i = 0; i < M; i++)
    {
        L_temp = (((Word32) refl[i]) * refl[i]) >> 15;
        if (L_temp <= 0x00007fffL)
        {
            temp = MAX_16 - (Word16) L_temp;
        }
        else
        {
            *pOverflow = 1;
            temp = 0;
        }
        pred_err = mult(pred_err, temp, pOverflow);
    }

    /* compute logarithm of prediction gain */
    Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m, pOverflow);

    /* convert exponent and mantissa to Word16 Q12 */
    log_pg = shl(sub(log_pg_e, 15, pOverflow), 12, pOverflow); /* Q12 */
    log_pg = shr(sub(0, add(log_pg, shr(log_pg_m, 15 - 12, pOverflow),
                            pOverflow), pOverflow), 1, pOverflow);
    st->log_pg_mean = add(mult(29491, st->log_pg_mean, pOverflow),
                          mult(3277, log_pg, pOverflow), pOverflow);

    /* Compute interpolated log energy */
    L_log_en_int = L_shr(L_log_en_int, 10, pOverflow); /* Q26 -> Q16 */

    /* Add 4 in Q16 */
    L_log_en_int = L_add(L_log_en_int, 4 * 65536L, pOverflow);

    /* subtract prediction gain */
    L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4, pOverflow), pOverflow);

    /* adjust level to speech coder mode */
    L_log_en_int = L_add(L_log_en_int,
                         L_shl(L_deposit_l(st->log_en_adjust), 5, pOverflow), pOverflow);

    log_en_int_e = (Word16)(L_log_en_int >> 16);

    log_en_int_m = (Word16)(L_shr(L_sub(L_log_en_int,
                                        L_deposit_h(log_en_int_e), pOverflow), 1, pOverflow));
    level = (Word16)(Pow2(log_en_int_e, log_en_int_m, pOverflow));  /* Q4 */

    for (i = 0; i < 4; i++)
    {
        /* Compute innovation vector */
        build_CN_code(&st->L_pn_seed_rx, ex, pOverflow);
        for (j = L_SUBFR - 1; j >= 0; j--)
        {
            ex[j] = mult(level, ex[j], pOverflow);
        }
        /* Synthesize */
        Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
                 mem_syn, 1);

    } /* next i */

    /* reset codebook averaging variables */
    averState->hangVar = 20;
    averState->hangCount = 0;

    if (new_state == DTX_MUTE)
    {
        /* mute comfort noise as it has been quite a long time since
         * last SID update  was performed                            */

        tmp_int_length = st->since_last_sid;

        if (tmp_int_length > 32)
        {
            tmp_int_length = 32;
        }
        else if (tmp_int_length <= 0)
        {
            /* safety guard against division by zero */
            tmp_int_length = 8;
        }

        L_temp = ((Word32) tmp_int_length) << 10;
        if (L_temp != (Word32)((Word16) L_temp))
        {
            *pOverflow = 1;
            L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16);
        }
        temp = (Word16) L_temp;

        st->true_sid_period_inv = div_s(1 << 10, temp);

        st->since_last_sid = 0;
        Copy(st->lsp, st->lsp_old, M);
        st->old_log_en = st->log_en;
        /* subtract 1/8 in Q11 i.e -6/8 dB */
        st->log_en = sub(st->log_en, 256, pOverflow);
    }

    /* reset interpolation length timer
     * if data has been updated.        */
    if ((st->sid_frame != 0) &&
            ((st->valid_data != 0) ||
             ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
    {
        st->since_last_sid =  0;
        st->data_updated = 1;
    }

    return;
}


/****************************************************************************/

/*
------------------------------------------------------------------------------
 FUNCTION NAME: dtx_dec_activity_update
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    st = pointer to a structure of type dtx_decState
    lsf =
        frame =

 Outputs:
    st points to an updated structure of type dtx_decState

 Returns:
    None.

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function updates the DTX parameters.

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

void dtx_dec_activity_update(dtx_decState *st,
                             Word16 lsf[],
                             Word16 frame[])
{
   Word16 i;

   Word32 L_frame_en;
   Word16 log_en_e, log_en_m, log_en;

   // update lsp history
   st->lsf_hist_ptr = add(st->lsf_hist_ptr,M);
   if (sub(st->lsf_hist_ptr, 80) == 0)
   {
      st->lsf_hist_ptr = 0;
   }
   Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);

   // compute log energy based on frame energy
   L_frame_en = 0;     // Q0
   for (i=0; i < L_FRAME; i++)
   {
      L_frame_en = L_mac(L_frame_en, frame[i], frame[i]);
   }
   Log2(L_frame_en, &log_en_e, &log_en_m);

   // convert exponent and mantissa to Word16 Q10
   log_en = shl(log_en_e, 10);  // Q10
   log_en = add(log_en, shr(log_en_m, 15-10));

   // divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193
   log_en = sub(log_en, 7497+1024);

   // insert into log energy buffer, no division by two as  *
    * log_en in decoder is Q11
   st->log_en_hist_ptr = add(st->log_en_hist_ptr, 1);
   if (sub(st->log_en_hist_ptr, DTX_HIST_SIZE) == 0)
   {
      st->log_en_hist_ptr = 0;
   }
   st->log_en_hist[st->log_en_hist_ptr] = log_en; // Q11
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

void dtx_dec_activity_update(dtx_decState *st,
                             Word16 lsf[],
                             Word16 frame[],
                             Flag   *pOverflow)
{
    Word16 i;

    Word32 L_frame_en;
    Word32 L_temp;
    Word16 log_en_e;
    Word16 log_en_m;
    Word16 log_en;

    /* update lsp history */
    st->lsf_hist_ptr += M;

    if (st->lsf_hist_ptr == 80)
    {
        st->lsf_hist_ptr = 0;
    }
    Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);

    /* compute log energy based on frame energy */
    L_frame_en = 0;     /* Q0 */
    for (i = L_FRAME - 1; i >= 0; i--)
    {
        L_temp = ((Word32) frame[i]) * frame[i];
        if (L_temp != (Word32) 0x40000000L)
        {
            L_temp = L_temp << 1;
        }
        else
        {
            L_temp = MAX_32;
        }
        L_frame_en = L_add(L_frame_en, L_temp, pOverflow);
    }
    Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow);

    /* convert exponent and mantissa to Word16 Q10 */
    L_temp = ((Word32) log_en_e) << 10;

    if (L_temp != (Word32)((Word16) L_temp))
    {
        *pOverflow = 1;
        L_temp = (Word32)((log_en_e > 0) ? MAX_16 : MIN_16);
    }
    log_en_e = (Word16) L_temp;

    if (log_en_m < 0)
    {
        log_en_m = ~((~log_en_m) >> 5);
    }
    else
    {
        log_en_m >>= 5;
    }
    log_en = add(log_en_e, log_en_m, pOverflow);

    /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
    log_en = sub(log_en, 7497 + 1024, pOverflow);

    /* insert into log energy buffer, no division by two as  *
    * log_en in decoder is Q11                              */
    st->log_en_hist_ptr += 1;

    if (st->log_en_hist_ptr == DTX_HIST_SIZE)
    {
        st->log_en_hist_ptr = 0;
    }
    st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */

    return;
}

/****************************************************************************/

/*
------------------------------------------------------------------------------
 FUNCTION NAME: rx_dtx_handler
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    st = pointer to a structure of type dtx_decState
    frame_type = RX frame type

 Returns:
    newState = variable of type DTXStateType

 Outputs:
    st points to an updated structure of type dtx_decState

 Global Variables Used:
    None.

 Local Variables Needed:
    None.

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function determines the new state of the decoder based on the frame_type
 and sets up the decoder parameters according to newState.

 Table of new SPD synthesis states

                           |     previous SPD_synthesis_state
     Incoming              |
     frame_type            | SPEECH       | DTX           | DTX_MUTE
     ---------------------------------------------------------------
     RX_SPEECH_GOOD ,      |              |               |
     RX_SPEECH_PR_DEGRADED | SPEECH       | SPEECH        | SPEECH
     ----------------------------------------------------------------
     RX_SPEECH_PR_BAD,     |              |               |
     RX_SPEECH_BAD,        | SPEECH       | DTX           | DTX_MUTE
     ----------------------------------------------------------------
     RX_SID_FIRST,         | DTX          | DTX/(DTX_MUTE)| DTX_MUTE
     ----------------------------------------------------------------
     RX_SID_UPDATE,        | DTX          | DTX           | DTX
     ----------------------------------------------------------------
     RX_SID_BAD,           | DTX          | DTX/(DTX_MUTE)| DTX_MUTE
     ----------------------------------------------------------------
     RX_NO_DATA            | SPEECH       | DTX/(DTX_MUTE)| DTX_MUTE
                           |(class2 garb.)|               |
     ----------------------------------------------------------------
     RX_ONSET              | SPEECH       | DTX/(DTX_MUTE)| DTX_MUTE
                           |(class2 garb.)|               |
     ----------------------------------------------------------------

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

enum DTXStateType rx_dtx_handler(
                      dtx_decState *st,           // i/o : State struct
                      enum RXFrameType frame_type // i   : Frame type
                      )
{
   enum DTXStateType newState;
   enum DTXStateType encState;

   // DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH)
   if ((sub(frame_type, RX_SID_FIRST) == 0)   ||
       (sub(frame_type, RX_SID_UPDATE) == 0)  ||
       (sub(frame_type, RX_SID_BAD) == 0)     ||
       (((sub(st->dtxGlobalState, DTX) == 0) ||
         (sub(st->dtxGlobalState, DTX_MUTE) == 0)) &&
        ((sub(frame_type, RX_NO_DATA) == 0) ||
         (sub(frame_type, RX_SPEECH_BAD) == 0) ||
         (sub(frame_type, RX_ONSET) == 0))))
   {
      newState = DTX;

      // stay in mute for these input types
      if ((sub(st->dtxGlobalState, DTX_MUTE) == 0) &&
          ((sub(frame_type, RX_SID_BAD) == 0) ||
           (sub(frame_type, RX_SID_FIRST) ==  0) ||
           (sub(frame_type, RX_ONSET) ==  0) ||
           (sub(frame_type, RX_NO_DATA) == 0)))
      {
         newState = DTX_MUTE;
      }

      // evaluate if noise parameters are too old
      // since_last_sid is reset when CN parameters have been updated
      st->since_last_sid = add(st->since_last_sid, 1);

      // no update of sid parameters in DTX for a long while
      // Due to the delayed update of  st->since_last_sid counter
      // SID_UPDATE frames need to be handled separately to avoid
      // entering DTX_MUTE for late SID_UPDATE frames
      if((sub(frame_type, RX_SID_UPDATE) != 0) &&
         (sub(st->since_last_sid, DTX_MAX_EMPTY_THRESH) > 0))
      {
         newState = DTX_MUTE;
      }
   }
   else
   {
      newState = SPEECH;
      st->since_last_sid = 0;
   }

    // reset the decAnaElapsed Counter when receiving CNI data the first
    // time, to robustify counter missmatch after handover
    // this might delay the bwd CNI analysis in the new decoder slightly.

   if ((st->data_updated == 0) &&
       (sub(frame_type, RX_SID_UPDATE) == 0))
   {
      st->decAnaElapsedCount = 0;
   }

   // update the SPE-SPD DTX hangover synchronization
   // to know when SPE has added dtx hangover
   st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1);
   st->dtxHangoverAdded = 0;

   if ((sub(frame_type, RX_SID_FIRST) == 0)  ||
       (sub(frame_type, RX_SID_UPDATE) == 0) ||
       (sub(frame_type, RX_SID_BAD) == 0)    ||
       (sub(frame_type, RX_ONSET) == 0)      ||
       (sub(frame_type, RX_NO_DATA) == 0))
   {
      encState = DTX;

      // In frame errors simulations RX_NO_DATA may occasionally mean that
      // a speech packet was probably sent by the encoder,
      // the assumed _encoder_ state should be SPEECH in such cases.
      if((sub(frame_type, RX_NO_DATA) == 0) &&
         (sub(newState, SPEECH) == 0))
      {
         encState = SPEECH;
      }

      // Note on RX_ONSET operation differing from RX_NO_DATA operation:
      // If a  RX_ONSET is received in the decoder (by "accident")
      // it is still most likely that the encoder  state
      // for the "ONSET frame" was DTX.

   }
   else
   {
      encState = SPEECH;
   }

   if (sub(encState, SPEECH) == 0)
   {
      st->dtxHangoverCount = DTX_HANG_CONST;
   }
   else
   {
      if (sub(st->decAnaElapsedCount, DTX_ELAPSED_FRAMES_THRESH) > 0)
      {
         st->dtxHangoverAdded = 1;
         st->decAnaElapsedCount = 0;
         st->dtxHangoverCount = 0;
      }
      else if (st->dtxHangoverCount == 0)
      {
         st->decAnaElapsedCount = 0;
      }
      else
      {
         st->dtxHangoverCount = sub(st->dtxHangoverCount, 1);
      }
   }

   if (sub(newState, SPEECH) != 0)
   {
      // DTX or DTX_MUTE
      // CN data is not in a first SID, first SIDs are marked as SID_BAD
      //  but will do backwards analysis if a hangover period has been added
      // according to the state machine above

      st->sid_frame = 0;
      st->valid_data = 0;

      if (sub(frame_type, RX_SID_FIRST) == 0)
      {
         st->sid_frame = 1;
      }
      else if (sub(frame_type, RX_SID_UPDATE) == 0)
      {
         st->sid_frame = 1;
         st->valid_data = 1;
      }
      else if (sub(frame_type, RX_SID_BAD) == 0)
      {
         st->sid_frame = 1;
         st->dtxHangoverAdded = 0; // use old data
      }
   }

   return newState;
   // newState is used by both SPEECH AND DTX synthesis routines
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

enum DTXStateType rx_dtx_handler(
    dtx_decState *st,           /* i/o : State struct     */
    enum RXFrameType frame_type,/* i   : Frame type       */
    Flag *pOverflow)
{
    enum DTXStateType newState;
    enum DTXStateType encState;


    /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */

    if ((frame_type == RX_SID_FIRST)   ||
            (frame_type == RX_SID_UPDATE)  ||
            (frame_type == RX_SID_BAD)     ||
            (((st->dtxGlobalState == DTX) || (st->dtxGlobalState == DTX_MUTE)) &&
             ((frame_type == RX_NO_DATA) || (frame_type == RX_SPEECH_BAD) ||
              (frame_type == RX_ONSET))))
    {
        newState = DTX;

        /* stay in mute for these input types */

        if ((st->dtxGlobalState == DTX_MUTE) &&
                ((frame_type == RX_SID_BAD) ||
                 (frame_type == RX_SID_FIRST) ||
                 (frame_type == RX_ONSET) ||
                 (frame_type == RX_NO_DATA)))
        {
            newState = DTX_MUTE;
        }

        /* evaluate if noise parameters are too old                     */
        /* since_last_sid is reset when CN parameters have been updated */
        st->since_last_sid = add(st->since_last_sid, 1, pOverflow);

        /* no update of sid parameters in DTX for a long while      */
        /* Due to the delayed update of  st->since_last_sid counter */
        /* SID_UPDATE frames need to be handled separately to avoid */
        /* entering DTX_MUTE for late SID_UPDATE frames             */
        if ((frame_type != RX_SID_UPDATE) &&
                (st->since_last_sid > DTX_MAX_EMPTY_THRESH))
        {
            newState = DTX_MUTE;
        }
    }
    else
    {
        newState = SPEECH;
        st->since_last_sid = 0;
    }

    /*
    reset the decAnaElapsed Counter when receiving CNI data the first
    time, to robustify counter missmatch after handover
    this might delay the bwd CNI analysis in the new decoder slightly.
    */

    if ((st->data_updated == 0) &&
            (frame_type == RX_SID_UPDATE))
    {
        st->decAnaElapsedCount = 0;
    }

    /* update the SPE-SPD DTX hangover synchronization */
    /* to know when SPE has added dtx hangover         */
    st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1, pOverflow);
    st->dtxHangoverAdded = 0;

    if ((frame_type == RX_SID_FIRST)  ||
            (frame_type == RX_SID_UPDATE) ||
            (frame_type == RX_SID_BAD)    ||
            (frame_type == RX_ONSET) ||
            (frame_type == RX_NO_DATA))
    {
        encState = DTX;

        /*
         In frame errors simulations RX_NO_DATA may occasionally mean that
         a speech packet was probably sent by the encoder,
         the assumed _encoder_ state should be SPEECH in such cases.
        */
        if ((frame_type == RX_NO_DATA) &&
                (newState == SPEECH))
        {
            encState = SPEECH;
        }

        /*
         Note on RX_ONSET operation differing from RX_NO_DATA operation:
         If a  RX_ONSET is received in the decoder (by "accident")
         it is still most likely that the encoder  state
         for the "ONSET frame" was DTX.
        */
    }
    else
    {
        encState = SPEECH;
    }


    if (encState == SPEECH)
    {
        st->dtxHangoverCount = DTX_HANG_CONST;
    }
    else
    {

        if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH)
        {
            st->dtxHangoverAdded = 1;
            st->decAnaElapsedCount = 0;
            st->dtxHangoverCount = 0;
        }
        else if (st->dtxHangoverCount == 0)
        {
            st->decAnaElapsedCount = 0;
        }
        else
        {
            st->dtxHangoverCount -= 1;
        }
    }

    if (newState != SPEECH)
    {
        /* DTX or DTX_MUTE
         * CN data is not in a first SID, first SIDs are marked as SID_BAD
         *  but will do backwards analysis if a hangover period has been added
         *  according to the state machine above
        */

        st->sid_frame = 0;
        st->valid_data = 0;

        if (frame_type == RX_SID_FIRST)
        {
            st->sid_frame = 1;
        }
        else if (frame_type == RX_SID_UPDATE)
        {
            st->sid_frame = 1;
            st->valid_data = 1;
        }
        else if (frame_type == RX_SID_BAD)
        {
            st->sid_frame = 1;
            st->dtxHangoverAdded = 0; /* use old data */
        }
    }

    /* newState is used by both SPEECH AND DTX synthesis routines */
    return(newState);
}