summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/enc/src/dtx_enc.cpp
blob: 2ccb777622df1e4bd34eb09c4ba5d920508deee3 (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
/* ------------------------------------------------------------------
 * 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_enc.c
 Funtions: dtx_enc_init
           dtx_enc_reset
           dtx_enc_exit
           dtx_enc
           dtx_buffer
           tx_dtx_handler

     Date: 06/08/2000

------------------------------------------------------------------------------
 REVISION HISTORY

 Description: Updated template used to PV coding template. First attempt at
          optimizing C code.

 Description: Updated file per comments gathered from Phase 2/3 review.
          Synched up with new template (Inputs/Outputs section). Deleted
          lines leftover from original code prior to the code section of
          dtx_enc_exit function. Deleted confusing comment in the log_en
          calculation in dtx_enc function. Restructured IF statement in
          the calculation of the sum of squares of speech signals in
          dtx_buffer.

 Description: Added setting of Overflow flag in inlined code.

 Description: Synchronized file with UTMS version 3.2.0. Updated coding
              template. Removed unnecessary include files.

 Description: Made the following changes per comments from Phase 2/3 review:
              1. Modified FOR loops to count down.
              2. Fixed typecasting issue with TI C compiler.
              3. Fixed comment in dtx_enc pseudo-code.
              4. Added dtx_enc code comment pertaining to possible assembly
                 implementation.

 Description: Added calls to add() in tx_dtx_handler. Updated copyright year.

 Description: Pass in pointer to overflow flag to all functions requiring this
              flag. This is to make the library EPOC compatible.

 Description:  For dtx_enc_reset() only
              1. Replaced copy() with memcpy.
              2. Eliminated include file copy.h
              3. Eliminated printf statement
              For dtx_buffer()
              1. Replaced copy() with memcpy.
              2. Eliminated math operations that unnecessary checked for
                 saturation, in some cases this by shifting before adding and
                 in other cases by evaluating the operands
              3. Unrolled loop to speed up execution

 Description:  For dtx_buffer()
              1. Modified scaling and added check for saturation. Previous
                 scaling was correct but altered precision, this cause bit
                 exactness test failure.

 Description:  For dtx_buffer()
              1. Modified scaling and saturation checks. Previous
                 scaling was correct but altered precision, this cause bit
                 exactness test failure for dtx vad2.

 Description:  Replaced OSCL mem type functions and eliminated include
               files that now are chosen by OSCL definitions

 Description:  Replaced "int" and/or "char" with OSCL defined types.

 Description:

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

 This file contains the various functions that perform the computation of the
 Silence Indicator (SID) parameters when in Discontinuous Transmission (DTX)
 mode.

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


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

#include "dtx_enc.h"
#include "q_plsf.h"
#include "typedef.h"
#include "mode.h"
#include "basic_op.h"
#include "log2.h"
#include "lsp_lsf.h"
#include "reorder.h"

/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/
extern Word32 L_add(Word32 L_var1, Word32 L_var2, Flag *pOverflow);

/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/

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

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


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

 Inputs:
    st = pointer to an array of pointers to structures of type
         dtx_encState

 Outputs:
    pointer pointed to by st is set to the address of the allocated
      memory

 Returns:
    return_value = 0, if initialization was successful; -1, otherwise (int)

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 This function allocates the state memory used by the dtx_enc function.

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

 None

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

 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

int dtx_enc_init (dtx_encState **st)
{
  dtx_encState* s;

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

  *st = NULL;

  // allocate memory
  if ((s= (dtx_encState *) malloc(sizeof(dtx_encState))) == NULL){
    fprintf(stderr, "dtx_enc_init: can not malloc state structure\n");
    return -1;
  }

  dtx_enc_reset(s);
  *st = s;

  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_enc_init(dtx_encState **st)
{
    dtx_encState* s;

    if (st == (dtx_encState **) NULL)
    {
        return(-1);
    }

    *st = NULL;

    /* allocate memory */
    if ((s = (dtx_encState *) malloc(sizeof(dtx_encState))) == NULL)
    {
        return(-1);
    }

    dtx_enc_reset(s);
    *st = s;

    return(0);
}

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

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

 Inputs:
    st = pointer to structures of type dtx_encState

 Outputs:
    structure pointed to by st is initialized to its reset value

 Returns:
    return_value = 1, if reset was successful; -1, otherwise (int)

 Global Variables Used:
    None

 Local Variables Needed:
    lsp_init_data = table containing LSP initialization values;
            table elements are constants of type Word16;
            table length is M

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

 This function initializes the fields of the state memory used by dtx_enc
 to their reset values.

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

 None

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

 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

int dtx_enc_reset (dtx_encState *st)
{
  Word16 i;

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

  st->hist_ptr = 0;
  st->log_en_index = 0;
  st->init_lsf_vq_index = 0;
  st->lsp_index[0] = 0;
  st->lsp_index[1] = 0;
  st->lsp_index[2] = 0;

  // Init lsp_hist[]
  for(i = 0; i < DTX_HIST_SIZE; i++)
  {
    Copy(lsp_init_data, &st->lsp_hist[i * M], M);
  }

  // Reset energy history
  Set_zero(st->log_en_hist, M);

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

  return 1;
}

------------------------------------------------------------------------------
 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_enc_reset(dtx_encState *st)
{
    Word16 i;

    if (st == (dtx_encState *) NULL)
    {
        return(-1);
    }

    st->hist_ptr = 0;
    st->log_en_index = 0;
    st->init_lsf_vq_index = 0;
    st->lsp_index[0] = 0;
    st->lsp_index[1] = 0;
    st->lsp_index[2] = 0;

    /* Init lsp_hist[] */
    for (i = 0; i < DTX_HIST_SIZE; i++)
    {
        memcpy(&st->lsp_hist[i * M], lsp_init_data, M*sizeof(Word16));
    }

    /* Reset energy history */
    memset(st->log_en_hist, 0, sizeof(Word16)*M);
    st->dtxHangoverCount = DTX_HANG_CONST;
    st->decAnaElapsedCount = 32767;

    return(1);
}

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

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

 Inputs:
    st = pointer to an array of pointers to structures of type
         dtx_encState

 Outputs:
    st points to the NULL address

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 This function deallocates the state memory used by dtx_enc function.

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

 None

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

 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

void dtx_enc_exit (dtx_encState **st)
{
   if (st == NULL || *st == NULL)
      return;

   // deallocate memory
   free(*st);
   *st = NULL;

   return;
}

------------------------------------------------------------------------------
 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_enc_exit(dtx_encState **st)
{
    if (st == NULL || *st == NULL)
    {
        return;
    }

    /* deallocate memory */
    free(*st);
    *st = NULL;

    return;
}

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

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

 Inputs:
    st = pointer to structures of type dtx_encState
    computeSidFlag = compute SID flag of type Word16
    qSt = pointer to structures of type Q_plsfState
    predState = pointer to structures of type gc_predState
    anap = pointer to an array of pointers to analysis parameters of
           type Word16

 Outputs:
    structure pointed to by st contains the newly calculated SID
      parameters
    structure pointed to by predState contains the new logarithmic frame
      energy
    pointer pointed to by anap points to the location of the new
      logarithmic frame energy and new LSPs

 Returns:
    return_value = 0 (int)

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 This function calculates the SID parameters when in the DTX mode.

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

 None

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

 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

int dtx_enc(dtx_encState *st,        // i/o : State struct
            Word16 computeSidFlag,   // i   : compute SID
            Q_plsfState *qSt,        // i/o : Qunatizer state struct
            gc_predState* predState, // i/o : State struct
        Word16 **anap            // o   : analysis parameters
        )
{
   Word16 i,j;
   Word16 log_en;
   Word16 lsf[M];
   Word16 lsp[M];
   Word16 lsp_q[M];
   Word32 L_lsp[M];

   // VOX mode computation of SID parameters
   if ((computeSidFlag != 0)  ||
        (st->log_en_index == 0))
   {
      // compute new SID frame if safe i.e don't
      // compute immediately after a talk spurt
      log_en = 0;
      for (i = 0; i < M; i++)
      {
         L_lsp[i] = 0;
      }

      // average energy and lsp
      for (i = 0; i < DTX_HIST_SIZE; i++)
      {
         log_en = add(log_en,
                      shr(st->log_en_hist[i],2));

         for (j = 0; j < M; j++)
         {
            L_lsp[j] = L_add(L_lsp[j],
                             L_deposit_l(st->lsp_hist[i * M + j]));
         }
      }

      log_en = shr(log_en, 1);
      for (j = 0; j < M; j++)
      {
         lsp[j] = extract_l(L_shr(L_lsp[j], 3));   // divide by 8
      }

      //  quantize logarithmic energy to 6 bits
      st->log_en_index = add(log_en, 2560);          // +2.5 in Q10
      st->log_en_index = add(st->log_en_index, 128); // add 0.5/4 in Q10
      st->log_en_index = shr(st->log_en_index, 8);

      if (sub(st->log_en_index, 63) > 0)
      {
         st->log_en_index = 63;
      }
      if (st->log_en_index < 0)
      {
         st->log_en_index = 0;
      }

      // update gain predictor memory
      log_en = shl(st->log_en_index, -2+10); // Q11 and divide by 4
      log_en = sub(log_en, 2560);            // add 2.5 in Q11

      log_en = sub(log_en, 9000);
      if (log_en > 0)
      {
         log_en = 0;
      }
      if (sub(log_en, -14436) < 0)
      {
         log_en = -14436;
      }

      // past_qua_en for other modes than MR122
      predState->past_qua_en[0] = log_en;
      predState->past_qua_en[1] = log_en;
      predState->past_qua_en[2] = log_en;
      predState->past_qua_en[3] = log_en;

      // scale down by factor 20*log10(2) in Q15
      log_en = mult(5443, log_en);

      // past_qua_en for mode MR122
      predState->past_qua_en_MR122[0] = log_en;
      predState->past_qua_en_MR122[1] = log_en;
      predState->past_qua_en_MR122[2] = log_en;
      predState->past_qua_en_MR122[3] = log_en;

      // make sure that LSP's are ordered
      Lsp_lsf(lsp, lsf, M);
      Reorder_lsf(lsf, LSF_GAP, M);
      Lsf_lsp(lsf, lsp, M);

      // Quantize lsp and put on parameter list
      Q_plsf_3(qSt, MRDTX, lsp, lsp_q, st->lsp_index,
               &st->init_lsf_vq_index);
   }

   *(*anap)++ = st->init_lsf_vq_index; // 3 bits

   *(*anap)++ = st->lsp_index[0];      // 8 bits
   *(*anap)++ = st->lsp_index[1];      // 9 bits
   *(*anap)++ = st->lsp_index[2];      // 9 bits


   *(*anap)++ = st->log_en_index;      // 6 bits
                                       // = 35 bits

   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_enc(dtx_encState *st,        /* i/o : State struct                  */
             Word16 computeSidFlag,   /* i   : compute SID                   */
             Q_plsfState *qSt,        /* i/o : Qunatizer state struct        */
             gc_predState* predState, /* i/o : State struct                  */
             Word16 **anap,           /* o   : analysis parameters           */
             Flag   *pOverflow        /* i/o : overflow indicator            */
            )
{
    Word16 i, j;
    Word16 temp;
    Word16 log_en;
    Word16 lsf[M];
    Word16 lsp[M];
    Word16 lsp_q[M];
    Word32 L_lsp[M];

    /* VOX mode computation of SID parameters */

    if ((computeSidFlag != 0)  ||
            (st->log_en_index == 0))
    {
        /* compute new SID frame if safe i.e don't
         * compute immediately after a talk spurt  */
        log_en = 0;
        for (i = M - 1; i >= 0; i--)
        {
            L_lsp[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])) >> 2);
            }
            else
            {
                temp = st->log_en_hist[i] >> 2;
            }
            log_en = add(log_en, temp, pOverflow);

            for (j = M - 1; j >= 0; j--)
            {
                L_lsp[j] = L_add(L_lsp[j],
                                 (Word32)(st->lsp_hist[i * M + j]),
                                 pOverflow);
            }
        }

        if (log_en < 0)
        {
            log_en = ~((~log_en) >> 1);
        }
        else
        {
            log_en = log_en >> 1;
        }

        for (j = M - 1; j >= 0; j--)
        {
            /* divide by 8 */
            if (L_lsp[j] < 0)
            {
                lsp[j] = (Word16)(~((~L_lsp[j]) >> 3));
            }
            else
            {
                lsp[j] = (Word16)(L_lsp[j] >> 3);
            }
        }

        /*  quantize logarithmic energy to 6 bits */
        /* +2.5 in Q10 */
        st->log_en_index = add(log_en, 2560, pOverflow);
        /* add 0.5/4 in Q10 */
        st->log_en_index = add(st->log_en_index, 128, pOverflow);
        if (st->log_en_index < 0)
        {
            st->log_en_index = ~((~st->log_en_index) >> 8);
        }
        else
        {
            st->log_en_index = st->log_en_index >> 8;
        }

        /*---------------------------------------------*/
        /* Limit to max and min allowable 6-bit values */
        /* Note: For assembly implementation, use the  */
        /*       following:                            */
        /*       if(st->long_en_index >> 6 != 0)       */
        /*       {                                     */
        /*           if(st->long_en_index < 0)         */
        /*           {                                 */
        /*               st->long_en_index = 0         */
        /*           }                                 */
        /*           else                              */
        /*           {                                 */
        /*               st->long_en_index = 63        */
        /*           }                                 */
        /*       }                                     */
        /*---------------------------------------------*/
        if (st->log_en_index > 63)
        {
            st->log_en_index = 63;
        }
        else if (st->log_en_index < 0)
        {
            st->log_en_index = 0;
        }

        /* update gain predictor memory */
        /* Q11 and divide by 4 */
        log_en = (Word16)(((Word32) st->log_en_index) << (-2 + 10));

        log_en = sub(log_en, 11560, pOverflow);

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

        /* past_qua_en for other modes than MR122 */
        predState->past_qua_en[0] = log_en;
        predState->past_qua_en[1] = log_en;
        predState->past_qua_en[2] = log_en;
        predState->past_qua_en[3] = log_en;

        /* scale down by factor 20*log10(2) in Q15 */
        log_en = (Word16)(((Word32)(5443 * log_en)) >> 15);

        /* past_qua_en for mode MR122 */
        predState->past_qua_en_MR122[0] = log_en;
        predState->past_qua_en_MR122[1] = log_en;
        predState->past_qua_en_MR122[2] = log_en;
        predState->past_qua_en_MR122[3] = log_en;

        /* make sure that LSP's are ordered */
        Lsp_lsf(lsp, lsf, M, pOverflow);
        Reorder_lsf(lsf, LSF_GAP, M, pOverflow);
        Lsf_lsp(lsf, lsp, M, pOverflow);

        /* Quantize lsp and put on parameter list */
        Q_plsf_3(qSt, MRDTX, lsp, lsp_q, st->lsp_index,
                 &st->init_lsf_vq_index, pOverflow);
    }

    *(*anap)++ = st->init_lsf_vq_index; /* 3 bits */
    *(*anap)++ = st->lsp_index[0];      /* 8 bits */
    *(*anap)++ = st->lsp_index[1];      /* 9 bits */
    *(*anap)++ = st->lsp_index[2];      /* 9 bits */
    *(*anap)++ = st->log_en_index;      /* 6 bits    */
    /* = 35 bits */

}

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


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

 Inputs:
    st = pointer to structures of type dtx_encState
    lsp_new = LSP vector whose elements are of type Word16; vector
          length is M
    speech = vector of speech samples of type Word16; vector length is
         BFR_SIZE_GSM

 Outputs:
    structure pointed to by st contains the new LSPs and logarithmic
      frame energy

 Returns:
    return_value = 0 (int)

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 This function handles the DTX buffer.

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

 None

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

 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

int dtx_buffer(dtx_encState *st,   // i/o : State struct
               Word16 lsp_new[],   // i   : LSP vector
               Word16 speech[]     // i   : speech samples
)
{
   Word16 i;
   Word32 L_frame_en;
   Word16 log_en_e;
   Word16 log_en_m;
   Word16 log_en;

   // update pointer to circular buffer
   st->hist_ptr = add(st->hist_ptr, 1);
   if (sub(st->hist_ptr, DTX_HIST_SIZE) == 0)
   {
      st->hist_ptr = 0;
   }

   // copy lsp vector into buffer
   Copy(lsp_new, &st->lsp_hist[st->hist_ptr * M], 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, speech[i], speech[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, 8521);

   // insert into log energy buffer with division by 2
   log_en = shr(log_en, 1);
   st->log_en_hist[st->hist_ptr] = log_en; // Q10

   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_buffer(dtx_encState *st,   /* i/o : State struct                    */
                Word16 lsp_new[],   /* i   : LSP vector                      */
                Word16 speech[],    /* i   : speech samples                  */
                Flag   *pOverflow   /* i/o : overflow indicator              */
               )
{

    Word16 i;
    Word32 L_frame_en;
    Word32 L_temp;
    Word16 log_en_e;
    Word16 log_en_m;
    Word16 log_en;
    Word16 *p_speech = &speech[0];

    /* update pointer to circular buffer      */
    st->hist_ptr += 1;

    if (st->hist_ptr == DTX_HIST_SIZE)
    {
        st->hist_ptr = 0;
    }

    /* copy lsp vector into buffer */
    memcpy(&st->lsp_hist[st->hist_ptr * M], lsp_new, M*sizeof(Word16));

    /* compute log energy based on frame energy */
    L_frame_en = 0;     /* Q0 */

    for (i = L_FRAME; i != 0; i--)
    {
        L_frame_en += (((Word32) * p_speech) * *(p_speech)) << 1;
        p_speech++;
        if (L_frame_en < 0)
        {
            L_frame_en = MAX_32;
            break;
        }
    }

    Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow);

    /* convert exponent and mantissa to Word16 Q10 */
    /* Q10 */
    L_temp = ((Word32) log_en_e) << 10;
    if (L_temp != (Word32)((Word16) L_temp))
    {
        *pOverflow = 1;
        log_en = (log_en_e > 0) ? MAX_16 : MIN_16;
    }
    else
    {
        log_en = (Word16) L_temp;
    }

    log_en += log_en_m >> (15 - 10);

    /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
    log_en -= 8521;

    /* insert into log energy buffer with division by 2 */

    st->log_en_hist[st->hist_ptr] = log_en >> 1; /* Q10 */

}

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

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

 Inputs:
    st = pointer to structures of type dtx_encState
    vad_flag = VAD decision flag of type Word16
    usedMode = pointer to the currently used mode of type enum Mode

 Outputs:
    structure pointed to by st contains the newly calculated speech
      hangover

 Returns:
    compute_new_sid_possible = flag to indicate a change in the
                   used mode; store type is Word16

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 This function adds extra speech hangover to analyze speech on the decoding
 side.

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

 None

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

 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

Word16 tx_dtx_handler(dtx_encState *st,      // i/o : State struct
                      Word16 vad_flag,       // i   : vad decision
                      enum Mode *usedMode    // i/o : mode changed or not
                      )
{
   Word16 compute_new_sid_possible;

   // this state machine is in synch with the GSMEFR txDtx machine
   st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1);

   compute_new_sid_possible = 0;

   if (vad_flag != 0)
   {
      st->dtxHangoverCount = DTX_HANG_CONST;
   }
   else
   {  // non-speech
      if (st->dtxHangoverCount == 0)
      {  // out of decoder analysis hangover
         st->decAnaElapsedCount = 0;
         *usedMode = MRDTX;
         compute_new_sid_possible = 1;
      }
      else
      { // in possible analysis hangover
         st->dtxHangoverCount = sub(st->dtxHangoverCount, 1);

         // decAnaElapsedCount + dtxHangoverCount < DTX_ELAPSED_FRAMES_THRESH
         if (sub(add(st->decAnaElapsedCount, st->dtxHangoverCount),
                 DTX_ELAPSED_FRAMES_THRESH) < 0)
         {
            *usedMode = MRDTX;
            // if short time since decoder update, do not add extra HO
         }
         // else
         //   override VAD and stay in
         //   speech mode *usedMode
         //   and add extra hangover
      }
   }

   return compute_new_sid_possible;
}

------------------------------------------------------------------------------
 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 tx_dtx_handler(dtx_encState *st,      /* i/o : State struct           */
                      Word16 vad_flag,       /* i   : vad decision           */
                      enum Mode *usedMode,   /* i/o : mode changed or not    */
                      Flag   *pOverflow      /* i/o : overflow indicator     */
                     )
{
    Word16 compute_new_sid_possible;
    Word16 count;

    /* this state machine is in synch with the GSMEFR txDtx machine */
    st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1, pOverflow);

    compute_new_sid_possible = 0;

    if (vad_flag != 0)
    {
        st->dtxHangoverCount = DTX_HANG_CONST;
    }
    else
    {  /* non-speech */
        if (st->dtxHangoverCount == 0)
        {  /* out of decoder analysis hangover  */
            st->decAnaElapsedCount = 0;
            *usedMode = MRDTX;
            compute_new_sid_possible = 1;
        }
        else
        { /* in possible analysis hangover */
            st->dtxHangoverCount -= 1;

            /* decAnaElapsedCount + dtxHangoverCount < */
            /* DTX_ELAPSED_FRAMES_THRESH               */
            count = add(st->decAnaElapsedCount, st->dtxHangoverCount,
                        pOverflow);
            if (count < DTX_ELAPSED_FRAMES_THRESH)
            {
                *usedMode = MRDTX;
                /* if short time since decoder update, */
                /* do not add extra HO                 */
            }
        }
    }

    return(compute_new_sid_possible);
}