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