Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / vad1.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30  Filename: vad1.cpp
31  Functions:
32
33 ------------------------------------------------------------------------------
34  MODULE DESCRIPTION
35
36
37 ------------------------------------------------------------------------------
38 */
39
40 /*----------------------------------------------------------------------------
41 ; INCLUDES
42 ----------------------------------------------------------------------------*/
43 #include "vad.h"
44 #include "typedef.h"
45 #include "shr.h"
46 #include "basic_op.h"
47 #include "cnst_vad.h"
48 #include "oscl_mem.h"
49
50 /*----------------------------------------------------------------------------
51 ; MACROS
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
54
55 /*----------------------------------------------------------------------------
56 ; DEFINES
57 ; Include all pre-processor statements here. Include conditional
58 ; compile variables also.
59 ----------------------------------------------------------------------------*/
60
61 /*----------------------------------------------------------------------------
62 ; LOCAL FUNCTION DEFINITIONS
63 ; Function Prototype declaration
64 ----------------------------------------------------------------------------*/
65
66 /*----------------------------------------------------------------------------
67 ; LOCAL VARIABLE DEFINITIONS
68 ; Variable declaration - defined here and used outside this module
69 ----------------------------------------------------------------------------*/
70
71 /*
72 ------------------------------------------------------------------------------
73  FUNCTION NAME: first_filter_stage
74 ------------------------------------------------------------------------------
75  INPUT AND OUTPUT DEFINITIONS
76
77  Inputs:
78     data -- array of type Word16 -- filter memory
79     in   -- array of type Word16 -- input signal
80
81  Outputs:
82     data -- array of type Word16 -- filter memory
83     out  -- array of type Word16 -- output values, every other
84                                     output is low-pass part and
85                                     other is high-pass part every
86
87     pOverflow -- pointer to type Flag -- overflow indicator
88
89  Returns:
90     None
91
92  Global Variables Used:
93     None
94
95  Local Variables Needed:
96     None
97
98 ------------------------------------------------------------------------------
99  FUNCTION DESCRIPTION
100
101  Purpose      : Scale input down by one bit. Calculate 5th order
102                 half-band lowpass/highpass filter pair with
103                 decimation.
104 ------------------------------------------------------------------------------
105  REQUIREMENTS
106
107  None
108
109 ------------------------------------------------------------------------------
110  REFERENCES
111
112  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
113
114 ------------------------------------------------------------------------------
115  PSEUDO-CODE
116
117
118 ------------------------------------------------------------------------------
119  CAUTION [optional]
120  [State any special notes, constraints or cautions for users of this function]
121
122 ------------------------------------------------------------------------------
123 */
124
125 static void first_filter_stage(
126     Word16 in[],      /* i   : input signal                  */
127     Word16 out[],     /* o   : output values, every other    */
128     /*       output is low-pass part and   */
129     /*       other is high-pass part every */
130     Word16 data[],    /* i/o : filter memory                 */
131     Flag  *pOverflow  /* o : Flag set when overflow occurs   */
132 )
133 {
134     Word16 temp0;
135     Word16 temp1;
136     Word16 temp2;
137     Word16 temp3;
138     Word16 i;
139     Word16 data0;
140     Word16 data1;
141
142     data0 = data[0];
143     data1 = data[1];
144
145     for (i = 0; i < FRAME_LEN / 4; i++)
146     {
147 //        temp0 = mult(COEFF5_1, data0, pOverflow);
148         temp0 = (Word16)(((Word32)COEFF5_1 * data0) >> 15);
149         temp1 = in[4*i+0] >> 2;
150         temp0 = sub(temp1, temp0, pOverflow);
151
152 //        temp1 = mult(COEFF5_1, temp0, pOverflow);
153         temp1 = (Word16)(((Word32)COEFF5_1 * temp0) >> 15);
154         temp1 = add_16(data0, temp1, pOverflow);
155
156 //        temp3 = mult(COEFF5_2, data1, pOverflow);
157         temp3 = (Word16)(((Word32)COEFF5_2 * data1) >> 15);
158
159         temp2 = in[4*i+1] >> 2;
160
161         temp3 = sub(temp2, temp3, pOverflow);
162
163 //        temp2 = mult(COEFF5_2, temp3, pOverflow);
164         temp2 = (Word16)(((Word32)COEFF5_2 * temp3) >> 15);
165
166         temp2 = add_16(data1, temp2, pOverflow);
167
168         out[4*i+0] = add_16(temp1, temp2, pOverflow);
169         out[4*i+1] = sub(temp1, temp2, pOverflow);
170
171 //        temp1 = mult(COEFF5_1, temp0, pOverflow);
172         temp1 = (Word16)(((Word32)COEFF5_1 * temp0) >> 15);
173
174         temp2 = in[4*i+2] >> 2;
175         data0 = sub(temp2, temp1, pOverflow);
176
177 //        temp1 = mult(COEFF5_1, data0, pOverflow);
178         temp1 = (Word16)(((Word32)COEFF5_1 * data0) >> 15);
179
180         temp1 = add_16(temp0, temp1, pOverflow);
181
182 //        data1 = mult(COEFF5_2, temp3, pOverflow);
183         data1 = (Word16)(((Word32)COEFF5_2 * temp3) >> 15);
184         temp2 = in[4*i+3] >> 2;
185         data1 = sub(temp2, data1, pOverflow);
186
187 //        temp2 = mult(COEFF5_2, data1, pOverflow);
188         temp2 = (Word16)(((Word32)COEFF5_2 * data1) >> 15);
189         temp2 = add_16(temp3, temp2, pOverflow);
190
191         out[4*i+2] = add_16(temp1, temp2, pOverflow);
192         out[4*i+3] = sub(temp1, temp2, pOverflow);
193     }
194
195     data[0] = data0;
196     data[1] = data1;
197 }
198
199
200 /*
201 ------------------------------------------------------------------------------
202  FUNCTION NAME: filter5
203 ------------------------------------------------------------------------------
204  INPUT AND OUTPUT DEFINITIONS
205
206  Inputs:
207     in0 -- array of type Word16 -- input values; output low-pass part
208     in1 -- array of type Word16 -- input values; output high-pass part
209     data -- array of type Word16 -- updated filter memory
210
211  Outputs:
212     in0 -- array of type Word16 -- input values; output low-pass part
213     in1 -- array of type Word16 -- input values; output high-pass part
214     data -- array of type Word16 -- updated filter memory
215     pOverflow -- pointer to type Flag -- overflow indicator
216
217  Returns:
218     None
219
220  Global Variables Used:
221     None
222
223  Local Variables Needed:
224     None
225
226 ------------------------------------------------------------------------------
227  FUNCTION DESCRIPTION
228
229  Purpose      : Fifth-order half-band lowpass/highpass filter pair with
230                 decimation.
231 ------------------------------------------------------------------------------
232  REQUIREMENTS
233
234  None
235
236 ------------------------------------------------------------------------------
237  REFERENCES
238
239  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
240
241 ------------------------------------------------------------------------------
242  PSEUDO-CODE
243
244
245 ------------------------------------------------------------------------------
246  CAUTION [optional]
247  [State any special notes, constraints or cautions for users of this function]
248
249 ------------------------------------------------------------------------------
250 */
251
252 static void filter5(Word16 *in0,    /* i/o : input values; output low-pass part  */
253                     Word16 *in1,    /* i/o : input values; output high-pass part */
254                     Word16 data[],  /* i/o : updated filter memory               */
255                     Flag  *pOverflow  /* o : Flag set when overflow occurs       */
256                    )
257 {
258     Word16 temp0;
259     Word16 temp1;
260     Word16 temp2;
261
262     temp0 = mult(COEFF5_1, data[0], pOverflow);
263     temp0 = sub(*in0, temp0, pOverflow);
264
265     temp1 = mult(COEFF5_1, temp0, pOverflow);
266     temp1 = add_16(data[0], temp1, pOverflow);
267     data[0] = temp0;
268
269     temp0 = mult(COEFF5_2, data[1], pOverflow);
270     temp0 = sub(*in1, temp0, pOverflow);
271
272     temp2 = mult(COEFF5_2, temp0, pOverflow);
273     temp2 = add_16(data[1], temp2, pOverflow);
274
275     data[1] = temp0;
276
277     temp0 = add_16(temp1, temp2, pOverflow);
278     *in0 = shr(temp0, 1, pOverflow);
279
280     temp0 = sub(temp1, temp2, pOverflow);
281     *in1 = shr(temp0, 1, pOverflow);
282 }
283
284
285
286
287 /*
288 ------------------------------------------------------------------------------
289  FUNCTION NAME: filter3
290 ------------------------------------------------------------------------------
291  INPUT AND OUTPUT DEFINITIONS
292
293
294  Inputs:
295     in0 -- array of type Word16 -- input values; output low-pass part
296     in1 -- array of type Word16 -- input values; output high-pass part
297     data -- array of type Word16 -- updated filter memory
298
299  Outputs:
300     in0 -- array of type Word16 -- input values; output low-pass part
301     in1 -- array of type Word16 -- input values; output high-pass part
302     data -- array of type Word16 -- updated filter memory
303     pOverflow -- pointer to type Flag -- overflow indicator
304
305  Returns:
306     None
307
308  Global Variables Used:
309     None
310
311  Local Variables Needed:
312     None
313
314 ------------------------------------------------------------------------------
315  FUNCTION DESCRIPTION
316
317  Purpose      : Third-order half-band lowpass/highpass filter pair with
318                 decimation.
319 ------------------------------------------------------------------------------
320  REQUIREMENTS
321
322  None
323
324 ------------------------------------------------------------------------------
325  REFERENCES
326
327  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
328
329 ------------------------------------------------------------------------------
330  PSEUDO-CODE
331
332
333 ------------------------------------------------------------------------------
334  CAUTION [optional]
335  [State any special notes, constraints or cautions for users of this function]
336
337 ------------------------------------------------------------------------------
338 */
339
340 static void filter3(
341     Word16 *in0,      /* i/o : input values; output low-pass part  */
342     Word16 *in1,      /* i/o : input values; output high-pass part */
343     Word16 *data,     /* i/o : updated filter memory               */
344     Flag  *pOverflow  /* o : Flag set when overflow occurs         */
345 )
346 {
347     Word16 temp1;
348     Word16 temp2;
349
350     temp1 = mult(COEFF3, *data, pOverflow);
351     temp1 = sub(*in1, temp1, pOverflow);
352
353     temp2 = mult(COEFF3, temp1, pOverflow);
354     temp2 = add_16(*data, temp2, pOverflow);
355
356     *data = temp1;
357
358     temp1 = sub(*in0, temp2, pOverflow);
359
360     *in1 = shr(temp1, 1, pOverflow);
361
362     temp1 = add_16(*in0, temp2, pOverflow);
363
364     *in0 = shr(temp1, 1, pOverflow);
365 }
366
367
368
369
370 /*
371 ------------------------------------------------------------------------------
372  FUNCTION NAME: level_calculation
373 ------------------------------------------------------------------------------
374  INPUT AND OUTPUT DEFINITIONS
375
376  Inputs:
377     data -- array of type Word16 -- signal buffer
378     sub_level -- pointer to type Word16 -- level calculated at the end of
379                                            the previous frame
380
381     count1 -- Word16 -- number of samples to be counted
382     count2 -- Word16 -- number of samples to be counted
383     ind_m  -- Word16 -- step size for the index of the data buffer
384     ind_a  -- Word16 -- starting index of the data buffer
385     scale  -- Word16 -- scaling for the level calculation
386
387  Outputs:
388     sub_level -- pointer to tyep Word16 -- level of signal calculated from the
389                                            last (count2 - count1) samples.
390     pOverflow -- pointer to type Flag -- overflow indicator
391
392  Returns:
393     signal level
394
395  Global Variables Used:
396
397
398  Local Variables Needed:
399     None
400
401 ------------------------------------------------------------------------------
402  FUNCTION DESCRIPTION
403
404  Purpose      : Calculate signal level in a sub-band. Level is calculated
405                 by summing absolute values of the input data.
406
407 ------------------------------------------------------------------------------
408  REQUIREMENTS
409
410  None
411
412 ------------------------------------------------------------------------------
413  REFERENCES
414
415  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
416
417 ------------------------------------------------------------------------------
418  PSEUDO-CODE
419
420
421 ------------------------------------------------------------------------------
422  CAUTION [optional]
423  [State any special notes, constraints or cautions for users of this function]
424
425 ------------------------------------------------------------------------------
426 */
427
428 static Word16 level_calculation(
429     Word16 data[],     /* i   : signal buffer                                */
430     Word16 *sub_level, /* i   : level calculate at the end of                */
431     /*       the previous frame                           */
432     /* o   : level of signal calculated from the last     */
433     /*       (count2 - count1) samples                    */
434     Word16 count1,     /* i   : number of samples to be counted              */
435     Word16 count2,     /* i   : number of samples to be counted              */
436     Word16 ind_m,      /* i   : step size for the index of the data buffer   */
437     Word16 ind_a,      /* i   : starting index of the data buffer            */
438     Word16 scale,      /* i   : scaling for the level calculation            */
439     Flag  *pOverflow   /* o : Flag set when overflow occurs                  */
440 )
441 {
442     Word32 l_temp1;
443     Word32 l_temp2;
444     Word16 level;
445     Word16 i;
446
447     l_temp1 = 0L;
448
449     for (i = count1; i < count2; i++)
450     {
451         l_temp1 = L_mac(l_temp1, 1, abs_s(data[ind_m*i+ind_a]), pOverflow);
452     }
453
454     l_temp2 = L_add(l_temp1, L_shl(*sub_level, sub(16, scale, pOverflow), pOverflow), pOverflow);
455     *sub_level = (Word16)(L_shl(l_temp1, scale, pOverflow) >> 16);
456
457     for (i = 0; i < count1; i++)
458     {
459         l_temp2 = L_mac(l_temp2, 1, abs_s(data[ind_m*i+ind_a]), pOverflow);
460     }
461     level = (Word16)(L_shl(l_temp2, scale, pOverflow) >> 16);
462
463     return level;
464 }
465
466
467
468
469 /*
470 ------------------------------------------------------------------------------
471  FUNCTION NAME: filter_bank
472 ------------------------------------------------------------------------------
473  INPUT AND OUTPUT DEFINITIONS
474
475  Inputs:
476     st -- pointer to type vadState1 --  State struct
477     in -- array of type Word16 -- input frame
478
479  Outputs:
480     level -- array of type Word16 -- signal levels at each band
481     st -- pointer to type vadState1 --  State struct
482     pOverflow -- pointer to type Flag -- overflow indicator
483
484  Returns:
485     None
486
487  Global Variables Used:
488     None
489
490  Local Variables Needed:
491     None
492
493 ------------------------------------------------------------------------------
494  FUNCTION DESCRIPTION
495
496  Purpose      : Divides input signal into 9-bands and calculas level of
497                 the signal in each band
498
499 ------------------------------------------------------------------------------
500  REQUIREMENTS
501
502  None
503
504 ------------------------------------------------------------------------------
505  REFERENCES
506
507  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
508
509 ------------------------------------------------------------------------------
510  PSEUDO-CODE
511
512
513 ------------------------------------------------------------------------------
514  CAUTION [optional]
515  [State any special notes, constraints or cautions for users of this function]
516
517 ------------------------------------------------------------------------------
518 */
519
520 static void filter_bank(
521     vadState1 *st,    /* i/o : State struct                    */
522     Word16 in[],      /* i   : input frame                     */
523     Word16 level[],   /* 0   : signal levels at each band      */
524     Flag  *pOverflow  /* o   : Flag set when overflow occurs   */
525 )
526 {
527     Word16 i;
528     Word16 tmp_buf[FRAME_LEN];
529
530     /* calculate the filter bank */
531
532     first_filter_stage(in, tmp_buf, st->a_data5[0], pOverflow);
533
534     for (i = 0; i < FRAME_LEN / 4; i++)
535     {
536         filter5(&tmp_buf[4*i], &tmp_buf[4*i+2], st->a_data5[1], pOverflow);
537         filter5(&tmp_buf[4*i+1], &tmp_buf[4*i+3], st->a_data5[2], pOverflow);
538     }
539     for (i = 0; i < FRAME_LEN / 8; i++)
540     {
541         filter3(&tmp_buf[8*i+0], &tmp_buf[8*i+4], &st->a_data3[0], pOverflow);
542         filter3(&tmp_buf[8*i+2], &tmp_buf[8*i+6], &st->a_data3[1], pOverflow);
543         filter3(&tmp_buf[8*i+3], &tmp_buf[8*i+7], &st->a_data3[4], pOverflow);
544     }
545
546     for (i = 0; i < FRAME_LEN / 16; i++)
547     {
548         filter3(&tmp_buf[16*i+0], &tmp_buf[16*i+8], &st->a_data3[2], pOverflow);
549         filter3(&tmp_buf[16*i+4], &tmp_buf[16*i+12], &st->a_data3[3], pOverflow);
550     }
551
552     /* calculate levels in each frequency band */
553
554     /* 3000 - 4000 Hz*/
555     level[8] = level_calculation(tmp_buf, &st->sub_level[8], FRAME_LEN / 4 - 8,
556                                  FRAME_LEN / 4, 4, 1, 15, pOverflow);
557     /* 2500 - 3000 Hz*/
558     level[7] = level_calculation(tmp_buf, &st->sub_level[7], FRAME_LEN / 8 - 4,
559                                  FRAME_LEN / 8, 8, 7, 16, pOverflow);
560     /* 2000 - 2500 Hz*/
561     level[6] = level_calculation(tmp_buf, &st->sub_level[6], FRAME_LEN / 8 - 4,
562                                  FRAME_LEN / 8, 8, 3, 16, pOverflow);
563     /* 1500 - 2000 Hz*/
564     level[5] = level_calculation(tmp_buf, &st->sub_level[5], FRAME_LEN / 8 - 4,
565                                  FRAME_LEN / 8, 8, 2, 16, pOverflow);
566     /* 1000 - 1500 Hz*/
567     level[4] = level_calculation(tmp_buf, &st->sub_level[4], FRAME_LEN / 8 - 4,
568                                  FRAME_LEN / 8, 8, 6, 16, pOverflow);
569     /* 750 - 1000 Hz*/
570     level[3] = level_calculation(tmp_buf, &st->sub_level[3], FRAME_LEN / 16 - 2,
571                                  FRAME_LEN / 16, 16, 4, 16, pOverflow);
572     /* 500 - 750 Hz*/
573     level[2] = level_calculation(tmp_buf, &st->sub_level[2], FRAME_LEN / 16 - 2,
574                                  FRAME_LEN / 16, 16, 12, 16, pOverflow);
575     /* 250 - 500 Hz*/
576     level[1] = level_calculation(tmp_buf, &st->sub_level[1], FRAME_LEN / 16 - 2,
577                                  FRAME_LEN / 16, 16, 8, 16, pOverflow);
578     /* 0 - 250 Hz*/
579     level[0] = level_calculation(tmp_buf, &st->sub_level[0], FRAME_LEN / 16 - 2,
580                                  FRAME_LEN / 16, 16, 0, 16, pOverflow);
581 }
582
583
584
585 /*
586 ------------------------------------------------------------------------------
587  FUNCTION NAME: update_cntrl
588 ------------------------------------------------------------------------------
589  INPUT AND OUTPUT DEFINITIONS
590
591  Inputs:
592     st -- pointer to type vadState1 --  State struct
593     level -- array of type Word16 -- sub-band levels of the input frame
594
595  Outputs:
596     st -- pointer to type vadState1 --  State struct
597     pOverflow -- pointer to type Flag -- overflow indicator
598
599  Returns:
600     None
601
602  Global Variables Used:
603     None
604
605  Local Variables Needed:
606     None
607
608 ------------------------------------------------------------------------------
609  FUNCTION DESCRIPTION
610
611  Purpose    : Control update of the background noise estimate.
612  Inputs     : pitch:      flags for pitch detection
613               stat_count: stationary counter
614               tone:       flags indicating presence of a tone
615               complex:      flags for complex  detection
616               vadreg:     intermediate VAD flags
617  Output     : stat_count: stationary counter
618
619
620 ------------------------------------------------------------------------------
621  REQUIREMENTS
622
623  None
624
625 ------------------------------------------------------------------------------
626  REFERENCES
627
628  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
629
630 ------------------------------------------------------------------------------
631  PSEUDO-CODE
632
633
634 ------------------------------------------------------------------------------
635  CAUTION [optional]
636  [State any special notes, constraints or cautions for users of this function]
637
638 ------------------------------------------------------------------------------
639 */
640
641 static void update_cntrl(
642     vadState1 *st,   /* i/o : State struct                       */
643     Word16 level[],  /* i   : sub-band levels of the input frame */
644     Flag  *pOverflow /* o   : Flag set when overflow occurs      */
645 )
646 {
647     Word16 i;
648     Word16 temp;
649     Word16 stat_rat;
650     Word16 exp;
651     Word16 num;
652     Word16 denom;
653     Word16 alpha;
654
655     /* handle highband complex signal input  separately       */
656     /* if ther has been highband correlation for some time    */
657     /* make sure that the VAD update speed is low for a while */
658     if (st->complex_warning != 0)
659     {
660         if (st->stat_count < CAD_MIN_STAT_COUNT)
661         {
662             st->stat_count = CAD_MIN_STAT_COUNT;
663         }
664     }
665     /* NB stat_count is allowed to be decreased by one below again  */
666     /* deadlock in speech is not possible unless the signal is very */
667     /* complex and need a high rate                                 */
668
669     /* if fullband pitch or tone have been detected for a while, initialize stat_count */
670     if (((Word16)(st->pitch & 0x6000) == 0x6000) ||
671             ((Word16)(st->tone & 0x7c00) == 0x7c00))
672     {
673         st->stat_count = STAT_COUNT;
674     }
675     else
676     {
677         /* if 8 last vad-decisions have been "0", reinitialize stat_count */
678         if ((st->vadreg & 0x7f80) == 0)
679         {
680             st->stat_count = STAT_COUNT;
681         }
682         else
683         {
684             stat_rat = 0;
685             for (i = 0; i < COMPLEN; i++)
686             {
687                 if (level[i] > st->ave_level[i])
688                 {
689                     num = level[i];
690                     denom = st->ave_level[i];
691                 }
692                 else
693                 {
694                     num = st->ave_level[i];
695                     denom = level[i];
696                 }
697                 /* Limit nimimum value of num and denom to STAT_THR_LEVEL */
698                 if (num < STAT_THR_LEVEL)
699                 {
700                     num = STAT_THR_LEVEL;
701                 }
702                 if (denom < STAT_THR_LEVEL)
703                 {
704                     denom = STAT_THR_LEVEL;
705                 }
706
707                 exp = norm_s(denom);
708
709                 denom = shl(denom, exp, pOverflow);
710
711                 /* stat_rat = num/denom * 64 */
712                 temp = shr(num, 1, pOverflow);
713                 temp = div_s(temp, denom);
714
715                 stat_rat = add_16(stat_rat, shr(temp, sub(8, exp, pOverflow), pOverflow), pOverflow);
716             }
717
718             /* compare stat_rat with a threshold and update stat_count */
719             if (stat_rat > STAT_THR)
720             {
721                 st->stat_count = STAT_COUNT;
722             }
723             else
724             {
725                 if ((st->vadreg & 0x4000) != 0)
726                 {
727                     if (st->stat_count != 0)
728                     {
729                         st->stat_count = sub(st->stat_count, 1, pOverflow);
730                     }
731                 }
732             }
733         }
734     }
735
736     /* Update average amplitude estimate for stationarity estimation */
737     alpha = ALPHA4;
738     if (st->stat_count == STAT_COUNT)
739     {
740         alpha = 32767;
741     }
742     else if ((st->vadreg & 0x4000) == 0)
743     {
744         alpha = ALPHA5;
745     }
746
747     for (i = 0; i < COMPLEN; i++)
748     {
749         temp = sub(level[i], st->ave_level[i], pOverflow);
750         temp = mult_r(alpha, temp, pOverflow);
751
752         st->ave_level[i] =
753             add_16(
754                 st->ave_level[i],
755                 temp,
756                 pOverflow);
757     }
758 }
759
760
761
762 /*
763 ------------------------------------------------------------------------------
764  FUNCTION NAME: hangover_addition
765 ------------------------------------------------------------------------------
766  INPUT AND OUTPUT DEFINITIONS
767
768  Inputs:
769     noise_level -- Word16 -- average level of the noise estimates
770     low_power   -- Word16 -- flag power of the input frame
771
772  Outputs:
773     st -- pointer to type vadState1 --  State struct
774     pOverflow -- pointer to type Flag -- overflow indicato
775
776  Returns:
777     VAD_flag indicating final VAD decision (Word16)
778
779  Global Variables Used:
780     None
781
782  Local Variables Needed:
783     None
784
785 ------------------------------------------------------------------------------
786  FUNCTION DESCRIPTION
787
788  Function     : hangover_addition
789  Purpose      : Add hangover for complex signal or after speech bursts
790  Inputs       : burst_count:  counter for the length of speech bursts
791                 hang_count:   hangover counter
792                 vadreg:       intermediate VAD decision
793  Outputs      : burst_count:  counter for the length of speech bursts
794                 hang_count:   hangover counter
795  Return value : VAD_flag indicating final VAD decision
796
797
798 ------------------------------------------------------------------------------
799  REQUIREMENTS
800
801  None
802
803 ------------------------------------------------------------------------------
804  REFERENCES
805
806  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
807
808 ------------------------------------------------------------------------------
809  PSEUDO-CODE
810
811
812 ------------------------------------------------------------------------------
813  CAUTION [optional]
814  [State any special notes, constraints or cautions for users of this function]
815
816 ------------------------------------------------------------------------------
817 */
818
819 static Word16 hangover_addition(
820     vadState1 *st,      /* i/o : State struct                     */
821     Word16 noise_level, /* i   : average level of the noise       */
822     /*       estimates                        */
823     Word16 low_power,   /* i   : flag power of the input frame    */
824     Flag  *pOverflow    /* o   : Flag set when overflow occurs    */
825 )
826 {
827     Word16 hang_len;
828     Word16 burst_len;
829
830     /*
831        Calculate burst_len and hang_len
832        burst_len: number of consecutive intermediate vad flags with "1"-decision
833                   required for hangover addition
834        hang_len:  length of the hangover
835        */
836
837     if (noise_level > HANG_NOISE_THR)
838     {
839         burst_len = BURST_LEN_HIGH_NOISE;
840         hang_len = HANG_LEN_HIGH_NOISE;
841     }
842     else
843     {
844         burst_len = BURST_LEN_LOW_NOISE;
845         hang_len = HANG_LEN_LOW_NOISE;
846     }
847
848     /* if the input power (pow_sum) is lower than a threshold, clear
849        counters and set VAD_flag to "0"  "fast exit"                 */
850     if (low_power != 0)
851     {
852         st->burst_count = 0;
853         st->hang_count = 0;
854         st->complex_hang_count = 0;
855         st->complex_hang_timer = 0;
856         return 0;
857     }
858
859     if (st->complex_hang_timer > CVAD_HANG_LIMIT)
860     {
861         if (st->complex_hang_count < CVAD_HANG_LENGTH)
862         {
863             st->complex_hang_count = CVAD_HANG_LENGTH;
864         }
865     }
866
867     /* long time very complex signal override VAD output function */
868     if (st->complex_hang_count != 0)
869     {
870         st->burst_count = BURST_LEN_HIGH_NOISE;
871         st->complex_hang_count = sub(st->complex_hang_count, 1, pOverflow);
872         return 1;
873     }
874     else
875     {
876         /* let hp_corr work in from a noise_period indicated by the VAD */
877         if (((st->vadreg & 0x3ff0) == 0) &&
878                 (st->corr_hp_fast > CVAD_THRESH_IN_NOISE))
879         {
880             return 1;
881         }
882     }
883
884     /* update the counters (hang_count, burst_count) */
885     if ((st->vadreg & 0x4000) != 0)
886     {
887         st->burst_count = add_16(st->burst_count, 1, pOverflow);
888
889         if (st->burst_count >= burst_len)
890         {
891             st->hang_count = hang_len;
892         }
893         return 1;
894     }
895     else
896     {
897         st->burst_count = 0;
898         if (st->hang_count > 0)
899         {
900             st->hang_count = sub(st->hang_count, 1, pOverflow);
901             return 1;
902         }
903     }
904     return 0;
905 }
906
907
908
909 /*
910 ------------------------------------------------------------------------------
911  FUNCTION NAME: noise_estimate_update
912 ------------------------------------------------------------------------------
913  INPUT AND OUTPUT DEFINITIONS
914
915  Inputs:
916     st -- pointer to type vadState1 --  State struct
917     level -- array of type Word16 -- sub-band levels of the input frame
918
919  Outputs:
920     st -- pointer to type vadState1 --  State struct
921     pOverflow -- pointer to type Flag -- overflow indicator
922
923  Returns:
924     None
925
926  Global Variables Used:
927     None
928
929  Local Variables Needed:
930     None
931
932 ------------------------------------------------------------------------------
933  FUNCTION DESCRIPTION
934
935  Purpose    : Update of background noise estimate
936  Inputs     : bckr_est:   background noise estimate
937               pitch:      flags for pitch detection
938               stat_count: stationary counter
939  Outputs    : bckr_est:   background noise estimate
940
941 ------------------------------------------------------------------------------
942  REQUIREMENTS
943
944  None
945
946 ------------------------------------------------------------------------------
947  REFERENCES
948
949  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
950
951 ------------------------------------------------------------------------------
952  PSEUDO-CODE
953
954
955 ------------------------------------------------------------------------------
956  CAUTION [optional]
957  [State any special notes, constraints or cautions for users of this function]
958
959 ------------------------------------------------------------------------------
960 */
961
962 static void noise_estimate_update(
963     vadState1 *st,    /* i/o : State struct                       */
964     Word16 level[],   /* i   : sub-band levels of the input frame */
965     Flag  *pOverflow  /* o : Flag set when overflow occurs        */
966 )
967 {
968     Word16 i;
969     Word16 alpha_up;
970     Word16 alpha_down;
971     Word16 bckr_add;
972
973     /* Control update of bckr_est[] */
974     update_cntrl(st, level, pOverflow);
975
976     /* Choose update speed */
977     bckr_add = 2;
978
979     if (((0x7800 & st->vadreg) == 0) &&
980             ((st->pitch & 0x7800) == 0)
981             && (st->complex_hang_count == 0))
982     {
983         alpha_up = ALPHA_UP1;
984         alpha_down = ALPHA_DOWN1;
985     }
986     else
987     {
988         if ((st->stat_count == 0)
989                 && (st->complex_hang_count == 0))
990         {
991             alpha_up = ALPHA_UP2;
992             alpha_down = ALPHA_DOWN2;
993         }
994         else
995         {
996             alpha_up = 0;
997             alpha_down = ALPHA3;
998             bckr_add = 0;
999         }
1000     }
1001
1002     /* Update noise estimate (bckr_est) */
1003     for (i = 0; i < COMPLEN; i++)
1004     {
1005         Word16 temp;
1006
1007         temp = sub(st->old_level[i], st->bckr_est[i], pOverflow);
1008
1009         if (temp < 0)
1010         { /* update downwards*/
1011             temp = mult_r(alpha_down, temp, pOverflow);
1012             temp = add_16(st->bckr_est[i], temp, pOverflow);
1013
1014             st->bckr_est[i] = add_16(-2, temp, pOverflow);
1015
1016             /* limit minimum value of the noise estimate to NOISE_MIN */
1017             if (st->bckr_est[i] < NOISE_MIN)
1018             {
1019                 st->bckr_est[i] = NOISE_MIN;
1020             }
1021         }
1022         else
1023         { /* update upwards */
1024             temp = mult_r(alpha_up, temp, pOverflow);
1025             temp = add_16(st->bckr_est[i], temp, pOverflow);
1026             st->bckr_est[i] = add_16(bckr_add, temp, pOverflow);
1027
1028             /* limit maximum value of the noise estimate to NOISE_MAX */
1029             if (st->bckr_est[i] > NOISE_MAX)
1030             {
1031                 st->bckr_est[i] = NOISE_MAX;
1032             }
1033         }
1034     }
1035
1036     /* Update signal levels of the previous frame (old_level) */
1037     for (i = 0; i < COMPLEN; i++)
1038     {
1039         st->old_level[i] = level[i];
1040     }
1041 }
1042
1043
1044 /*
1045 ------------------------------------------------------------------------------
1046  FUNCTION NAME: complex_estimate_adapt
1047 ------------------------------------------------------------------------------
1048  INPUT AND OUTPUT DEFINITIONS
1049
1050  Inputs:
1051     st -- pointer to type vadState1 --  State struct
1052     low_power -- Word16 -- very low level flag of the input frame
1053
1054  Outputs:
1055     st -- pointer to type vadState1 --  State struct
1056     pOverflow -- pointer to type Flag -- overflow indicator
1057
1058  Returns:
1059     None
1060
1061  Global Variables Used:
1062     None
1063
1064  Local Variables Needed:
1065     None
1066
1067 ------------------------------------------------------------------------------
1068  FUNCTION DESCRIPTION
1069
1070  Function   : complex_estimate_adapt
1071  Purpose    : Update/adapt of complex signal estimate
1072  Inputs     : low_power:   low signal power flag
1073  Outputs    : st->corr_hp_fast:   long term complex signal estimate
1074
1075 ------------------------------------------------------------------------------
1076  REQUIREMENTS
1077
1078  None
1079
1080 ------------------------------------------------------------------------------
1081  REFERENCES
1082
1083  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1084
1085 ------------------------------------------------------------------------------
1086  PSEUDO-CODE
1087
1088
1089 ------------------------------------------------------------------------------
1090  CAUTION [optional]
1091  [State any special notes, constraints or cautions for users of this function]
1092
1093 ------------------------------------------------------------------------------
1094 */
1095
1096 static void complex_estimate_adapt(
1097     vadState1 *st,      /* i/o : VAD state struct                       */
1098     Word16 low_power,   /* i   : very low level flag of the input frame */
1099     Flag  *pOverflow    /* o : Flag set when overflow occurs            */
1100 )
1101 {
1102     Word16 alpha;            /* Q15 */
1103     Word32 L_tmp;            /* Q31 */
1104
1105
1106     /* adapt speed on own state */
1107     if (st->best_corr_hp < st->corr_hp_fast) /* decrease */
1108     {
1109         if (st->corr_hp_fast < CVAD_THRESH_ADAPT_HIGH)
1110         {  /* low state  */
1111             alpha = CVAD_ADAPT_FAST;
1112         }
1113         else
1114         {  /* high state */
1115             alpha = CVAD_ADAPT_REALLY_FAST;
1116         }
1117     }
1118     else  /* increase */
1119     {
1120         if (st->corr_hp_fast < CVAD_THRESH_ADAPT_HIGH)
1121         {
1122             alpha = CVAD_ADAPT_FAST;
1123         }
1124         else
1125         {
1126             alpha = CVAD_ADAPT_SLOW;
1127         }
1128     }
1129
1130     L_tmp = ((Word32)st->corr_hp_fast << 16);
1131     L_tmp = L_msu(L_tmp, alpha, st->corr_hp_fast, pOverflow);
1132     L_tmp = L_mac(L_tmp, alpha, st->best_corr_hp, pOverflow);
1133     st->corr_hp_fast = pv_round(L_tmp, pOverflow);           /* Q15 */
1134
1135     if (st->corr_hp_fast < CVAD_MIN_CORR)
1136     {
1137         st->corr_hp_fast = CVAD_MIN_CORR;
1138     }
1139
1140     if (low_power != 0)
1141     {
1142         st->corr_hp_fast = CVAD_MIN_CORR;
1143     }
1144 }
1145
1146
1147 /*
1148 ------------------------------------------------------------------------------
1149  FUNCTION NAME: complex_vad
1150 ------------------------------------------------------------------------------
1151  INPUT AND OUTPUT DEFINITIONS
1152
1153  Inputs:
1154     st -- pointer to type vadState1 --  State struct
1155     low_power -- Word16 -- flag power of the input frame
1156
1157  Outputs:
1158     st -- pointer to type vadState1 --  State struct
1159     pOverflow -- pointer to type Flag -- overflow indicator
1160
1161
1162  Returns:
1163     the complex background decision
1164
1165  Global Variables Used:
1166     None
1167
1168  Local Variables Needed:
1169     None
1170
1171 ------------------------------------------------------------------------------
1172  FUNCTION DESCRIPTION
1173
1174  Purpose      : complex background decision
1175  Return value : the complex background decision
1176
1177 ------------------------------------------------------------------------------
1178  REQUIREMENTS
1179
1180  None
1181
1182 ------------------------------------------------------------------------------
1183  REFERENCES
1184
1185  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1186
1187 ------------------------------------------------------------------------------
1188  PSEUDO-CODE
1189
1190
1191 ------------------------------------------------------------------------------
1192  CAUTION [optional]
1193  [State any special notes, constraints or cautions for users of this function]
1194
1195 ------------------------------------------------------------------------------
1196 */
1197
1198 static Word16 complex_vad(
1199     vadState1 *st,    /* i/o : VAD state struct              */
1200     Word16 low_power, /* i   : flag power of the input frame */
1201     Flag  *pOverflow  /* o : Flag set when overflow occurs   */
1202 )
1203 {
1204     st->complex_high = shr(st->complex_high, 1, pOverflow);
1205     st->complex_low = shr(st->complex_low, 1, pOverflow);
1206
1207     if (low_power == 0)
1208     {
1209         if (st->corr_hp_fast > CVAD_THRESH_ADAPT_HIGH)
1210         {
1211             st->complex_high |= 0x4000;
1212         }
1213
1214         if (st->corr_hp_fast > CVAD_THRESH_ADAPT_LOW)
1215         {
1216             st->complex_low |= 0x4000;
1217         }
1218     }
1219
1220     if (st->corr_hp_fast > CVAD_THRESH_HANG)
1221     {
1222         st->complex_hang_timer = add_16(st->complex_hang_timer, 1, pOverflow);
1223     }
1224     else
1225     {
1226         st->complex_hang_timer =  0;
1227     }
1228
1229     return ((Word16)(st->complex_high & 0x7f80) == 0x7f80 ||
1230             (Word16)(st->complex_low & 0x7fff) == 0x7fff);
1231 }
1232
1233
1234 /*
1235 ------------------------------------------------------------------------------
1236  FUNCTION NAME: vad_decision
1237 ------------------------------------------------------------------------------
1238  INPUT AND OUTPUT DEFINITIONS
1239
1240  Inputs:
1241     st -- pointer to type vadState1 --  State struct
1242     level -- array of type Word16 -- sub-band levels of the input frame
1243     pow_sum -- Word32 -- power of the input frame
1244
1245  Outputs:
1246     st -- pointer to type vadState1 --  State struct
1247     pOverflow -- pointer to type Flag -- overflow indicator
1248
1249  Returns:
1250     VAD_flag (Word16)
1251
1252  Global Variables Used:
1253     None
1254
1255  Local Variables Needed:
1256     None
1257
1258 ------------------------------------------------------------------------------
1259  FUNCTION DESCRIPTION
1260
1261  Purpose      : Calculates VAD_flag
1262  Inputs       : bckr_est:    background noise estimate
1263                 vadreg:      intermediate VAD flags
1264  Outputs      : noise_level: average level of the noise estimates
1265                 vadreg:      intermediate VAD flags
1266  Return value : VAD_flag
1267
1268 ------------------------------------------------------------------------------
1269  REQUIREMENTS
1270
1271  None
1272
1273 ------------------------------------------------------------------------------
1274  REFERENCES
1275
1276  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1277
1278 ------------------------------------------------------------------------------
1279  PSEUDO-CODE
1280
1281
1282 ------------------------------------------------------------------------------
1283  CAUTION [optional]
1284  [State any special notes, constraints or cautions for users of this function]
1285
1286 ------------------------------------------------------------------------------
1287 */
1288
1289 static Word16 vad_decision(
1290     vadState1 *st,         /* i/o : State struct                       */
1291     Word16 level[COMPLEN], /* i   : sub-band levels of the input frame */
1292     Word32 pow_sum,        /* i   : power of the input frame           */
1293     Flag  *pOverflow       /* o : Flag set when overflow occurs        */
1294 )
1295 {
1296     Word16 i;
1297     Word16 snr_sum;
1298     Word32 L_temp;
1299     Word16 vad_thr;
1300     Word16 temp;
1301     Word16 noise_level;
1302     Word16 low_power_flag;
1303     Word16 temp1;
1304
1305     /*
1306        Calculate squared sum of the input levels (level)
1307        divided by the background noise components (bckr_est).
1308        */
1309     L_temp = 0;
1310
1311     for (i = 0; i < COMPLEN; i++)
1312     {
1313         Word16 exp;
1314
1315         exp = norm_s(st->bckr_est[i]);
1316         temp = shl(st->bckr_est[i], exp, pOverflow);
1317         temp = div_s(shr(level[i], 1, pOverflow), temp);
1318         temp = shl(temp, sub(exp, UNIRSHFT - 1, pOverflow), pOverflow);
1319         L_temp = L_mac(L_temp, temp, temp, pOverflow);
1320     }
1321
1322     snr_sum = (Word16)(L_shl(L_temp, 6, pOverflow) >> 16);
1323     snr_sum = mult(snr_sum, INV_COMPLEN, pOverflow);
1324
1325     /* Calculate average level of estimated background noise */
1326     L_temp = 0;
1327     for (i = 0; i < COMPLEN; i++)
1328     {
1329         L_temp = L_add(L_temp, st->bckr_est[i], pOverflow);
1330     }
1331
1332     noise_level = (Word16)(L_shl(L_temp, 13, pOverflow) >> 16);
1333
1334     /* Calculate VAD threshold */
1335     temp1 = sub(noise_level, VAD_P1, pOverflow);
1336     temp1 = mult(VAD_SLOPE, temp1, pOverflow);
1337     vad_thr = add_16(temp1, VAD_THR_HIGH, pOverflow);
1338
1339     if (vad_thr < VAD_THR_LOW)
1340     {
1341         vad_thr = VAD_THR_LOW;
1342     }
1343
1344     /* Shift VAD decision register */
1345     st->vadreg = shr(st->vadreg, 1, pOverflow);
1346
1347     /* Make intermediate VAD decision */
1348     if (snr_sum > vad_thr)
1349     {
1350         st->vadreg |= 0x4000;
1351     }
1352     /* primary vad decsion made */
1353
1354     /* check if the input power (pow_sum) is lower than a threshold" */
1355     if (L_sub(pow_sum, VAD_POW_LOW, pOverflow) < 0)
1356     {
1357         low_power_flag = 1;
1358     }
1359     else
1360     {
1361         low_power_flag = 0;
1362     }
1363
1364     /* update complex signal estimate st->corr_hp_fast and hangover reset timer using */
1365     /* low_power_flag and corr_hp_fast  and various adaptation speeds                 */
1366     complex_estimate_adapt(st, low_power_flag, pOverflow);
1367
1368     /* check multiple thresholds of the st->corr_hp_fast value */
1369     st->complex_warning = complex_vad(st, low_power_flag, pOverflow);
1370
1371     /* Update speech subband vad background noise estimates */
1372     noise_estimate_update(st, level, pOverflow);
1373
1374     /*  Add speech and complex hangover and return speech VAD_flag */
1375     /*  long term complex hangover may be added */
1376     st->speech_vad_decision = hangover_addition(st, noise_level, low_power_flag, pOverflow);
1377
1378     return (st->speech_vad_decision);
1379 }
1380
1381
1382 /*
1383 ------------------------------------------------------------------------------
1384  FUNCTION NAME: vad1_init
1385 ------------------------------------------------------------------------------
1386  INPUT AND OUTPUT DEFINITIONS
1387
1388  Inputs:
1389     state -- double pointer to type vadState1 -- pointer to memory to
1390                                                  be initialized.
1391
1392  Outputs:
1393     state -- points to initalized area in memory.
1394
1395  Returns:
1396     None
1397
1398  Global Variables Used:
1399     None
1400
1401  Local Variables Needed:
1402     None
1403
1404 ------------------------------------------------------------------------------
1405  FUNCTION DESCRIPTION
1406
1407  Allocates state memory and initializes state memory
1408
1409 ------------------------------------------------------------------------------
1410  REQUIREMENTS
1411
1412  None
1413
1414 ------------------------------------------------------------------------------
1415  REFERENCES
1416
1417  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1418
1419 ------------------------------------------------------------------------------
1420  PSEUDO-CODE
1421
1422
1423 ------------------------------------------------------------------------------
1424  CAUTION [optional]
1425  [State any special notes, constraints or cautions for users of this function]
1426
1427 ------------------------------------------------------------------------------
1428 */
1429
1430 Word16 vad1_init(vadState1 **state)
1431 {
1432     vadState1* s;
1433
1434     if (state == (vadState1 **) NULL)
1435     {
1436         return -1;
1437     }
1438     *state = NULL;
1439
1440     /* allocate memory */
1441     if ((s = (vadState1 *) oscl_malloc(sizeof(vadState1))) == NULL)
1442     {
1443         return -1;
1444     }
1445
1446     vad1_reset(s);
1447
1448     *state = s;
1449
1450     return 0;
1451 }
1452
1453 /*
1454 ------------------------------------------------------------------------------
1455  FUNCTION NAME: vad1_reset
1456 ------------------------------------------------------------------------------
1457  INPUT AND OUTPUT DEFINITIONS
1458
1459  Inputs:
1460     state -- pointer to type vadState1 --  State struct
1461
1462  Outputs:
1463     state -- pointer to type vadState1 --  State struct
1464
1465  Returns:
1466     None
1467
1468  Global Variables Used:
1469     None
1470
1471  Local Variables Needed:
1472     None
1473
1474 ------------------------------------------------------------------------------
1475  FUNCTION DESCRIPTION
1476
1477  Purpose:    Resets state memory to zero
1478
1479 ------------------------------------------------------------------------------
1480  REQUIREMENTS
1481
1482  None
1483
1484 ------------------------------------------------------------------------------
1485  REFERENCES
1486
1487  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1488
1489 ------------------------------------------------------------------------------
1490  PSEUDO-CODE
1491
1492
1493 ------------------------------------------------------------------------------
1494  CAUTION [optional]
1495  [State any special notes, constraints or cautions for users of this function]
1496
1497 ------------------------------------------------------------------------------
1498 */
1499
1500 Word16 vad1_reset(vadState1 *state)
1501 {
1502     Word16 i;
1503     Word16 j;
1504
1505     if (state == (vadState1 *) NULL)
1506     {
1507         return -1;
1508     }
1509
1510     /* Initialize pitch detection variables */
1511     state->oldlag_count = 0;
1512     state->oldlag = 0;
1513     state->pitch = 0;
1514     state->tone = 0;
1515
1516     state->complex_high = 0;
1517     state->complex_low = 0;
1518     state->complex_hang_timer = 0;
1519
1520     state->vadreg = 0;
1521
1522     state->stat_count = 0;
1523     state->burst_count = 0;
1524     state->hang_count = 0;
1525     state->complex_hang_count = 0;
1526
1527     /* initialize memory used by the filter bank */
1528     for (i = 0; i < 3; i++)
1529     {
1530         for (j = 0; j < 2; j++)
1531         {
1532             state->a_data5[i][j] = 0;
1533         }
1534     }
1535
1536     for (i = 0; i < 5; i++)
1537     {
1538         state->a_data3[i] = 0;
1539     }
1540
1541     /* initialize the rest of the memory */
1542     for (i = 0; i < COMPLEN; i++)
1543     {
1544         state->bckr_est[i] = NOISE_INIT;
1545         state->old_level[i] = NOISE_INIT;
1546         state->ave_level[i] = NOISE_INIT;
1547         state->sub_level[i] = 0;
1548     }
1549
1550     state->best_corr_hp = CVAD_LOWPOW_RESET;
1551
1552     state->speech_vad_decision = 0;
1553     state->complex_warning = 0;
1554     state->sp_burst_count = 0;
1555
1556     state->corr_hp_fast = CVAD_LOWPOW_RESET;
1557
1558     return 0;
1559 }
1560
1561
1562 /*
1563 ------------------------------------------------------------------------------
1564  FUNCTION NAME: vad1_exit
1565 ------------------------------------------------------------------------------
1566  INPUT AND OUTPUT DEFINITIONS
1567
1568  Inputs:
1569     state -- pointer to type vadState1 --  State struct
1570
1571  Outputs:
1572     None
1573
1574  Returns:
1575     None
1576
1577  Global Variables Used:
1578     None
1579
1580  Local Variables Needed:
1581     None
1582
1583 ------------------------------------------------------------------------------
1584  FUNCTION DESCRIPTION
1585
1586     The memory used for state memory is freed
1587
1588 ------------------------------------------------------------------------------
1589  REQUIREMENTS
1590
1591  None
1592
1593 ------------------------------------------------------------------------------
1594  REFERENCES
1595
1596  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1597
1598 ------------------------------------------------------------------------------
1599  PSEUDO-CODE
1600
1601
1602 ------------------------------------------------------------------------------
1603  CAUTION [optional]
1604  [State any special notes, constraints or cautions for users of this function]
1605
1606 ------------------------------------------------------------------------------
1607 */
1608
1609 void vad1_exit(vadState1 **state)
1610 {
1611     if (state == NULL || *state == NULL)
1612         return;
1613
1614     /* deallocate memory */
1615     oscl_free(*state);
1616     *state = NULL;
1617
1618     return;
1619 }
1620
1621
1622 /*
1623 ------------------------------------------------------------------------------
1624  FUNCTION NAME: vad_complex_detection_update
1625 ------------------------------------------------------------------------------
1626  INPUT AND OUTPUT DEFINITIONS
1627
1628  Inputs:
1629     best_corr_hp -- Word16 -- best Corr
1630     state -- pointer to type vadState1 --  State struct
1631
1632  Outputs:
1633     state -- pointer to type vadState1 --  State struct
1634
1635  Returns:
1636     None
1637
1638  Global Variables Used:
1639     None
1640
1641  Local Variables Needed:
1642     None
1643
1644 ------------------------------------------------------------------------------
1645  FUNCTION DESCRIPTION
1646
1647  Purpose      : update vad->bestCorr_hp  complex signal feature state
1648 ------------------------------------------------------------------------------
1649  REQUIREMENTS
1650
1651  None
1652
1653 ------------------------------------------------------------------------------
1654  REFERENCES
1655
1656  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1657
1658 ------------------------------------------------------------------------------
1659  PSEUDO-CODE
1660
1661
1662 ------------------------------------------------------------------------------
1663  CAUTION [optional]
1664  [State any special notes, constraints or cautions for users of this function]
1665
1666 ------------------------------------------------------------------------------
1667 */
1668
1669 void vad_complex_detection_update(
1670     vadState1 *st,       /* i/o : State struct */
1671     Word16 best_corr_hp) /* i   : best Corr    */
1672 {
1673     st->best_corr_hp = best_corr_hp;
1674 }
1675
1676
1677
1678 /*
1679 ------------------------------------------------------------------------------
1680  FUNCTION NAME: vad_tone_detection
1681 ------------------------------------------------------------------------------
1682  INPUT AND OUTPUT DEFINITIONS
1683
1684  Inputs:
1685     st -- pointer to type vadState1 --  State struct
1686     t0 -- Word32 -- autocorrelation maxima
1687     t1 -- Word32 -- energy
1688
1689  Outputs:
1690     st -- pointer to type vadState1 --  State struct
1691     pOverflow -- pointer to type Flag -- overflow indicator
1692
1693  Returns:
1694     None
1695
1696  Global Variables Used:
1697     None
1698
1699  Local Variables Needed:
1700     None
1701
1702 ------------------------------------------------------------------------------
1703  FUNCTION DESCRIPTION
1704
1705  Purpose      : Set tone flag if pitch gain is high. This is used to detect
1706                 signaling tones and other signals with high pitch gain.
1707  Inputs       : tone: flags indicating presence of a tone
1708  Outputs      : tone: flags indicating presence of a tone
1709 ------------------------------------------------------------------------------
1710  REQUIREMENTS
1711
1712  None
1713
1714 ------------------------------------------------------------------------------
1715  REFERENCES
1716
1717  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1718
1719 ------------------------------------------------------------------------------
1720  PSEUDO-CODE
1721
1722
1723 ------------------------------------------------------------------------------
1724  CAUTION [optional]
1725  [State any special notes, constraints or cautions for users of this function]
1726
1727 ------------------------------------------------------------------------------
1728 */
1729
1730 void vad_tone_detection(
1731     vadState1 *st,    /* i/o : State struct                       */
1732     Word32 t0,        /* i   : autocorrelation maxima             */
1733     Word32 t1,        /* i   : energy                             */
1734     Flag  *pOverflow  /* o : Flag set when overflow occurs        */
1735 )
1736 {
1737     Word16 temp;
1738     /*
1739        if (t0 > TONE_THR * t1)
1740        set tone flag
1741        */
1742     temp = pv_round(t1, pOverflow);
1743
1744     if ((temp > 0) && (L_msu(t0, temp, TONE_THR, pOverflow) > 0))
1745     {
1746         st->tone |= 0x4000;
1747     }
1748 }
1749
1750
1751 /*
1752 ------------------------------------------------------------------------------
1753  FUNCTION NAME: vad_tone_detection_update
1754 ------------------------------------------------------------------------------
1755  INPUT AND OUTPUT DEFINITIONS
1756
1757  Inputs:
1758     one_lag_per_frame -- Word16 -- 1 if one open-loop lag is calculated per
1759                                    each frame, otherwise 0
1760     st -- pointer to type vadState1 --  State struct
1761
1762  Outputs:
1763     st -- pointer to type vadState1 --  State struct
1764     pOverflow -- pointer to type Flag -- overflow indicator
1765
1766  Returns:
1767     None
1768
1769  Global Variables Used:
1770     None
1771
1772  Local Variables Needed:
1773     None
1774
1775 ------------------------------------------------------------------------------
1776  FUNCTION DESCRIPTION
1777
1778  Purpose      : Update the tone flag register. Tone flags are shifted right
1779                 by one bit. This function should be called from the speech
1780                 encoder before call to Vad_tone_detection() function.
1781
1782 ------------------------------------------------------------------------------
1783  REQUIREMENTS
1784
1785  None
1786
1787 ------------------------------------------------------------------------------
1788  REFERENCES
1789
1790  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1791
1792 ------------------------------------------------------------------------------
1793  PSEUDO-CODE
1794
1795
1796 ------------------------------------------------------------------------------
1797  CAUTION [optional]
1798  [State any special notes, constraints or cautions for users of this function]
1799
1800 ------------------------------------------------------------------------------
1801 */
1802
1803 void vad_tone_detection_update(
1804     vadState1 *st,              /* i/o : State struct           */
1805     Word16 one_lag_per_frame,   /* i   : 1 if one open-loop lag */
1806     /*       is calculated per each */
1807     /*       frame, otherwise 0     */
1808     Flag *pOverflow             /* o   : Flags overflow         */
1809 )
1810 {
1811     /* Shift tone flags right by one bit */
1812     st->tone = shr(st->tone, 1, pOverflow);
1813
1814     /* If open-loop lag is calculated only once in each frame, do extra update
1815        and assume that the other tone flag of the frame is one. */
1816     if (one_lag_per_frame != 0)
1817     {
1818         st->tone = shr(st->tone, 1, pOverflow);
1819         st->tone |= 0x2000;
1820     }
1821 }
1822
1823
1824 /*
1825 ------------------------------------------------------------------------------
1826  FUNCTION NAME: vad_pitch_detection
1827 ------------------------------------------------------------------------------
1828  INPUT AND OUTPUT DEFINITIONS
1829
1830  Inputs:
1831     T_op -- array of type Word16 -- speech encoder open loop lags
1832     st -- pointer to type vadState1 --  State struct
1833
1834  Outputs:
1835     st -- pointer to type vadState1 --  State struct
1836     pOverflow -- pointer to type Flag -- overflow indicator
1837
1838  Returns:
1839     None
1840
1841  Global Variables Used:
1842     None
1843
1844  Local Variables Needed:
1845     None
1846
1847 ------------------------------------------------------------------------------
1848  FUNCTION DESCRIPTION
1849
1850  Purpose      : Test whether signal contains pitch or other periodic
1851                 component.
1852  Return value : Boolean voiced / unvoiced decision in state variable
1853
1854 ------------------------------------------------------------------------------
1855  REQUIREMENTS
1856
1857  None
1858
1859 ------------------------------------------------------------------------------
1860  REFERENCES
1861
1862  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1863
1864 ------------------------------------------------------------------------------
1865  PSEUDO-CODE
1866
1867
1868 ------------------------------------------------------------------------------
1869  CAUTION [optional]
1870  [State any special notes, constraints or cautions for users of this function]
1871
1872 ------------------------------------------------------------------------------
1873 */
1874
1875 void vad_pitch_detection(
1876     vadState1 *st,    /* i/o : State struct                  */
1877     Word16 T_op[],    /* i   : speech encoder open loop lags */
1878     Flag  *pOverflow  /* o : Flag set when overflow occurs   */
1879 )
1880 {
1881     Word16 lagcount;
1882     Word16 i;
1883     Word16 temp;
1884
1885     lagcount = 0;
1886
1887     for (i = 0; i < 2; i++)
1888     {
1889         temp = sub(st->oldlag, T_op[i], pOverflow);
1890         temp = abs_s(temp);
1891
1892         if (temp < LTHRESH)
1893         {
1894             lagcount += 1;
1895         }
1896
1897         /* Save the current LTP lag */
1898         st->oldlag = T_op[i];
1899     }
1900
1901     /* Make pitch decision.
1902        Save flag of the pitch detection to the variable pitch.
1903        */
1904     st->pitch = shr(st->pitch, 1, pOverflow);
1905
1906     temp =
1907         add_16(
1908             st->oldlag_count,
1909             lagcount,
1910             pOverflow);
1911
1912     if (temp >= NTHRESH)
1913     {
1914         st->pitch |= 0x4000;
1915     }
1916
1917     /* Update oldlagcount */
1918     st->oldlag_count = lagcount;
1919 }
1920
1921 /*
1922 ------------------------------------------------------------------------------
1923  FUNCTION NAME: vad1
1924 ------------------------------------------------------------------------------
1925  INPUT AND OUTPUT DEFINITIONS
1926
1927  Inputs:
1928     st -- pointer to type vadState1 --  State struct
1929     in_buf -- array of type Word16 -- samples of the input frame
1930
1931  Outputs:
1932     st -- pointer to type vadState1 --  State struct
1933     pOverflow -- pointer to type Flag -- overflow indicator
1934
1935  Returns:
1936     VAD Decision, 1 = speech, 0 = noise
1937
1938  Global Variables Used:
1939     None
1940
1941  Local Variables Needed:
1942     None
1943
1944 ------------------------------------------------------------------------------
1945  FUNCTION DESCRIPTION
1946
1947  Purpose      : Main program for Voice Activity Detection (VAD) for AMR
1948  Return value : VAD Decision, 1 = speech, 0 = noise
1949
1950 ------------------------------------------------------------------------------
1951  REQUIREMENTS
1952
1953  None
1954
1955 ------------------------------------------------------------------------------
1956  REFERENCES
1957
1958  vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1959
1960 ------------------------------------------------------------------------------
1961  PSEUDO-CODE
1962
1963
1964 ------------------------------------------------------------------------------
1965  CAUTION [optional]
1966  [State any special notes, constraints or cautions for users of this function]
1967
1968 ------------------------------------------------------------------------------
1969 */
1970
1971 Word16 vad1(
1972     vadState1 *st,    /* i/o : State struct                       */
1973     Word16 in_buf[],  /* i   : samples of the input frame         */
1974     Flag  *pOverflow  /* o   : Flag set when overflow occurs      */
1975 )
1976 {
1977     Word16 level[COMPLEN];
1978     Word32 pow_sum;
1979     Word16 i;
1980
1981     /* Calculate power of the input frame. */
1982     pow_sum = 0L;
1983
1984     for (i = 0; i < FRAME_LEN; i++)
1985     {
1986         pow_sum = L_mac(pow_sum, in_buf[i-LOOKAHEAD], in_buf[i-LOOKAHEAD], pOverflow);
1987     }
1988
1989     /*
1990       If input power is very low, clear pitch flag of the current frame
1991       */
1992     if (L_sub(pow_sum, POW_PITCH_THR, pOverflow) < 0)
1993     {
1994         st->pitch = st->pitch & 0x3fff;
1995     }
1996
1997     /*
1998       If input power is very low, clear complex flag of the "current" frame
1999       */
2000     if (L_sub(pow_sum, POW_COMPLEX_THR, pOverflow) < 0)
2001     {
2002         st->complex_low = st->complex_low & 0x3fff;
2003     }
2004
2005     /*
2006       Run the filter bank which calculates signal levels at each band
2007       */
2008     filter_bank(st, in_buf, level, pOverflow);
2009
2010     return (vad_decision(st, level, pow_sum, pOverflow));
2011 }
2012
2013