1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
33 ------------------------------------------------------------------------------
37 ------------------------------------------------------------------------------
40 /*----------------------------------------------------------------------------
42 ----------------------------------------------------------------------------*/
50 /*----------------------------------------------------------------------------
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
57 ; Include all pre-processor statements here. Include conditional
58 ; compile variables also.
59 ----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
62 ; LOCAL FUNCTION DEFINITIONS
63 ; Function Prototype declaration
64 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
67 ; LOCAL VARIABLE DEFINITIONS
68 ; Variable declaration - defined here and used outside this module
69 ----------------------------------------------------------------------------*/
72 ------------------------------------------------------------------------------
73 FUNCTION NAME: first_filter_stage
74 ------------------------------------------------------------------------------
75 INPUT AND OUTPUT DEFINITIONS
78 data -- array of type Word16 -- filter memory
79 in -- array of type Word16 -- input signal
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
87 pOverflow -- pointer to type Flag -- overflow indicator
92 Global Variables Used:
95 Local Variables Needed:
98 ------------------------------------------------------------------------------
101 Purpose : Scale input down by one bit. Calculate 5th order
102 half-band lowpass/highpass filter pair with
104 ------------------------------------------------------------------------------
109 ------------------------------------------------------------------------------
112 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
114 ------------------------------------------------------------------------------
118 ------------------------------------------------------------------------------
120 [State any special notes, constraints or cautions for users of this function]
122 ------------------------------------------------------------------------------
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 */
145 for (i = 0; i < FRAME_LEN / 4; i++)
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);
152 // temp1 = mult(COEFF5_1, temp0, pOverflow);
153 temp1 = (Word16)(((Word32)COEFF5_1 * temp0) >> 15);
154 temp1 = add_16(data0, temp1, pOverflow);
156 // temp3 = mult(COEFF5_2, data1, pOverflow);
157 temp3 = (Word16)(((Word32)COEFF5_2 * data1) >> 15);
159 temp2 = in[4*i+1] >> 2;
161 temp3 = sub(temp2, temp3, pOverflow);
163 // temp2 = mult(COEFF5_2, temp3, pOverflow);
164 temp2 = (Word16)(((Word32)COEFF5_2 * temp3) >> 15);
166 temp2 = add_16(data1, temp2, pOverflow);
168 out[4*i+0] = add_16(temp1, temp2, pOverflow);
169 out[4*i+1] = sub(temp1, temp2, pOverflow);
171 // temp1 = mult(COEFF5_1, temp0, pOverflow);
172 temp1 = (Word16)(((Word32)COEFF5_1 * temp0) >> 15);
174 temp2 = in[4*i+2] >> 2;
175 data0 = sub(temp2, temp1, pOverflow);
177 // temp1 = mult(COEFF5_1, data0, pOverflow);
178 temp1 = (Word16)(((Word32)COEFF5_1 * data0) >> 15);
180 temp1 = add_16(temp0, temp1, pOverflow);
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);
187 // temp2 = mult(COEFF5_2, data1, pOverflow);
188 temp2 = (Word16)(((Word32)COEFF5_2 * data1) >> 15);
189 temp2 = add_16(temp3, temp2, pOverflow);
191 out[4*i+2] = add_16(temp1, temp2, pOverflow);
192 out[4*i+3] = sub(temp1, temp2, pOverflow);
201 ------------------------------------------------------------------------------
202 FUNCTION NAME: filter5
203 ------------------------------------------------------------------------------
204 INPUT AND OUTPUT DEFINITIONS
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
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
220 Global Variables Used:
223 Local Variables Needed:
226 ------------------------------------------------------------------------------
229 Purpose : Fifth-order half-band lowpass/highpass filter pair with
231 ------------------------------------------------------------------------------
236 ------------------------------------------------------------------------------
239 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
241 ------------------------------------------------------------------------------
245 ------------------------------------------------------------------------------
247 [State any special notes, constraints or cautions for users of this function]
249 ------------------------------------------------------------------------------
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 */
262 temp0 = mult(COEFF5_1, data[0], pOverflow);
263 temp0 = sub(*in0, temp0, pOverflow);
265 temp1 = mult(COEFF5_1, temp0, pOverflow);
266 temp1 = add_16(data[0], temp1, pOverflow);
269 temp0 = mult(COEFF5_2, data[1], pOverflow);
270 temp0 = sub(*in1, temp0, pOverflow);
272 temp2 = mult(COEFF5_2, temp0, pOverflow);
273 temp2 = add_16(data[1], temp2, pOverflow);
277 temp0 = add_16(temp1, temp2, pOverflow);
278 *in0 = shr(temp0, 1, pOverflow);
280 temp0 = sub(temp1, temp2, pOverflow);
281 *in1 = shr(temp0, 1, pOverflow);
288 ------------------------------------------------------------------------------
289 FUNCTION NAME: filter3
290 ------------------------------------------------------------------------------
291 INPUT AND OUTPUT DEFINITIONS
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
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
308 Global Variables Used:
311 Local Variables Needed:
314 ------------------------------------------------------------------------------
317 Purpose : Third-order half-band lowpass/highpass filter pair with
319 ------------------------------------------------------------------------------
324 ------------------------------------------------------------------------------
327 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
329 ------------------------------------------------------------------------------
333 ------------------------------------------------------------------------------
335 [State any special notes, constraints or cautions for users of this function]
337 ------------------------------------------------------------------------------
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 */
350 temp1 = mult(COEFF3, *data, pOverflow);
351 temp1 = sub(*in1, temp1, pOverflow);
353 temp2 = mult(COEFF3, temp1, pOverflow);
354 temp2 = add_16(*data, temp2, pOverflow);
358 temp1 = sub(*in0, temp2, pOverflow);
360 *in1 = shr(temp1, 1, pOverflow);
362 temp1 = add_16(*in0, temp2, pOverflow);
364 *in0 = shr(temp1, 1, pOverflow);
371 ------------------------------------------------------------------------------
372 FUNCTION NAME: level_calculation
373 ------------------------------------------------------------------------------
374 INPUT AND OUTPUT DEFINITIONS
377 data -- array of type Word16 -- signal buffer
378 sub_level -- pointer to type Word16 -- level calculated at the end of
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
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
395 Global Variables Used:
398 Local Variables Needed:
401 ------------------------------------------------------------------------------
404 Purpose : Calculate signal level in a sub-band. Level is calculated
405 by summing absolute values of the input data.
407 ------------------------------------------------------------------------------
412 ------------------------------------------------------------------------------
415 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
417 ------------------------------------------------------------------------------
421 ------------------------------------------------------------------------------
423 [State any special notes, constraints or cautions for users of this function]
425 ------------------------------------------------------------------------------
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 */
449 for (i = count1; i < count2; i++)
451 l_temp1 = L_mac(l_temp1, 1, abs_s(data[ind_m*i+ind_a]), pOverflow);
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);
457 for (i = 0; i < count1; i++)
459 l_temp2 = L_mac(l_temp2, 1, abs_s(data[ind_m*i+ind_a]), pOverflow);
461 level = (Word16)(L_shl(l_temp2, scale, pOverflow) >> 16);
470 ------------------------------------------------------------------------------
471 FUNCTION NAME: filter_bank
472 ------------------------------------------------------------------------------
473 INPUT AND OUTPUT DEFINITIONS
476 st -- pointer to type vadState1 -- State struct
477 in -- array of type Word16 -- input frame
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
487 Global Variables Used:
490 Local Variables Needed:
493 ------------------------------------------------------------------------------
496 Purpose : Divides input signal into 9-bands and calculas level of
497 the signal in each band
499 ------------------------------------------------------------------------------
504 ------------------------------------------------------------------------------
507 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
509 ------------------------------------------------------------------------------
513 ------------------------------------------------------------------------------
515 [State any special notes, constraints or cautions for users of this function]
517 ------------------------------------------------------------------------------
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 */
528 Word16 tmp_buf[FRAME_LEN];
530 /* calculate the filter bank */
532 first_filter_stage(in, tmp_buf, st->a_data5[0], pOverflow);
534 for (i = 0; i < FRAME_LEN / 4; i++)
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);
539 for (i = 0; i < FRAME_LEN / 8; i++)
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);
546 for (i = 0; i < FRAME_LEN / 16; i++)
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);
552 /* calculate levels in each frequency band */
555 level[8] = level_calculation(tmp_buf, &st->sub_level[8], FRAME_LEN / 4 - 8,
556 FRAME_LEN / 4, 4, 1, 15, pOverflow);
558 level[7] = level_calculation(tmp_buf, &st->sub_level[7], FRAME_LEN / 8 - 4,
559 FRAME_LEN / 8, 8, 7, 16, pOverflow);
561 level[6] = level_calculation(tmp_buf, &st->sub_level[6], FRAME_LEN / 8 - 4,
562 FRAME_LEN / 8, 8, 3, 16, pOverflow);
564 level[5] = level_calculation(tmp_buf, &st->sub_level[5], FRAME_LEN / 8 - 4,
565 FRAME_LEN / 8, 8, 2, 16, pOverflow);
567 level[4] = level_calculation(tmp_buf, &st->sub_level[4], FRAME_LEN / 8 - 4,
568 FRAME_LEN / 8, 8, 6, 16, pOverflow);
570 level[3] = level_calculation(tmp_buf, &st->sub_level[3], FRAME_LEN / 16 - 2,
571 FRAME_LEN / 16, 16, 4, 16, pOverflow);
573 level[2] = level_calculation(tmp_buf, &st->sub_level[2], FRAME_LEN / 16 - 2,
574 FRAME_LEN / 16, 16, 12, 16, pOverflow);
576 level[1] = level_calculation(tmp_buf, &st->sub_level[1], FRAME_LEN / 16 - 2,
577 FRAME_LEN / 16, 16, 8, 16, pOverflow);
579 level[0] = level_calculation(tmp_buf, &st->sub_level[0], FRAME_LEN / 16 - 2,
580 FRAME_LEN / 16, 16, 0, 16, pOverflow);
586 ------------------------------------------------------------------------------
587 FUNCTION NAME: update_cntrl
588 ------------------------------------------------------------------------------
589 INPUT AND OUTPUT DEFINITIONS
592 st -- pointer to type vadState1 -- State struct
593 level -- array of type Word16 -- sub-band levels of the input frame
596 st -- pointer to type vadState1 -- State struct
597 pOverflow -- pointer to type Flag -- overflow indicator
602 Global Variables Used:
605 Local Variables Needed:
608 ------------------------------------------------------------------------------
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
620 ------------------------------------------------------------------------------
625 ------------------------------------------------------------------------------
628 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
630 ------------------------------------------------------------------------------
634 ------------------------------------------------------------------------------
636 [State any special notes, constraints or cautions for users of this function]
638 ------------------------------------------------------------------------------
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 */
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)
660 if (st->stat_count < CAD_MIN_STAT_COUNT)
662 st->stat_count = CAD_MIN_STAT_COUNT;
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 */
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))
673 st->stat_count = STAT_COUNT;
677 /* if 8 last vad-decisions have been "0", reinitialize stat_count */
678 if ((st->vadreg & 0x7f80) == 0)
680 st->stat_count = STAT_COUNT;
685 for (i = 0; i < COMPLEN; i++)
687 if (level[i] > st->ave_level[i])
690 denom = st->ave_level[i];
694 num = st->ave_level[i];
697 /* Limit nimimum value of num and denom to STAT_THR_LEVEL */
698 if (num < STAT_THR_LEVEL)
700 num = STAT_THR_LEVEL;
702 if (denom < STAT_THR_LEVEL)
704 denom = STAT_THR_LEVEL;
709 denom = shl(denom, exp, pOverflow);
711 /* stat_rat = num/denom * 64 */
712 temp = shr(num, 1, pOverflow);
713 temp = div_s(temp, denom);
715 stat_rat = add_16(stat_rat, shr(temp, sub(8, exp, pOverflow), pOverflow), pOverflow);
718 /* compare stat_rat with a threshold and update stat_count */
719 if (stat_rat > STAT_THR)
721 st->stat_count = STAT_COUNT;
725 if ((st->vadreg & 0x4000) != 0)
727 if (st->stat_count != 0)
729 st->stat_count = sub(st->stat_count, 1, pOverflow);
736 /* Update average amplitude estimate for stationarity estimation */
738 if (st->stat_count == STAT_COUNT)
742 else if ((st->vadreg & 0x4000) == 0)
747 for (i = 0; i < COMPLEN; i++)
749 temp = sub(level[i], st->ave_level[i], pOverflow);
750 temp = mult_r(alpha, temp, pOverflow);
763 ------------------------------------------------------------------------------
764 FUNCTION NAME: hangover_addition
765 ------------------------------------------------------------------------------
766 INPUT AND OUTPUT DEFINITIONS
769 noise_level -- Word16 -- average level of the noise estimates
770 low_power -- Word16 -- flag power of the input frame
773 st -- pointer to type vadState1 -- State struct
774 pOverflow -- pointer to type Flag -- overflow indicato
777 VAD_flag indicating final VAD decision (Word16)
779 Global Variables Used:
782 Local Variables Needed:
785 ------------------------------------------------------------------------------
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
798 ------------------------------------------------------------------------------
803 ------------------------------------------------------------------------------
806 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
808 ------------------------------------------------------------------------------
812 ------------------------------------------------------------------------------
814 [State any special notes, constraints or cautions for users of this function]
816 ------------------------------------------------------------------------------
819 static Word16 hangover_addition(
820 vadState1 *st, /* i/o : State struct */
821 Word16 noise_level, /* i : average level of the noise */
823 Word16 low_power, /* i : flag power of the input frame */
824 Flag *pOverflow /* o : Flag set when overflow occurs */
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
837 if (noise_level > HANG_NOISE_THR)
839 burst_len = BURST_LEN_HIGH_NOISE;
840 hang_len = HANG_LEN_HIGH_NOISE;
844 burst_len = BURST_LEN_LOW_NOISE;
845 hang_len = HANG_LEN_LOW_NOISE;
848 /* if the input power (pow_sum) is lower than a threshold, clear
849 counters and set VAD_flag to "0" "fast exit" */
854 st->complex_hang_count = 0;
855 st->complex_hang_timer = 0;
859 if (st->complex_hang_timer > CVAD_HANG_LIMIT)
861 if (st->complex_hang_count < CVAD_HANG_LENGTH)
863 st->complex_hang_count = CVAD_HANG_LENGTH;
867 /* long time very complex signal override VAD output function */
868 if (st->complex_hang_count != 0)
870 st->burst_count = BURST_LEN_HIGH_NOISE;
871 st->complex_hang_count = sub(st->complex_hang_count, 1, pOverflow);
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))
884 /* update the counters (hang_count, burst_count) */
885 if ((st->vadreg & 0x4000) != 0)
887 st->burst_count = add_16(st->burst_count, 1, pOverflow);
889 if (st->burst_count >= burst_len)
891 st->hang_count = hang_len;
898 if (st->hang_count > 0)
900 st->hang_count = sub(st->hang_count, 1, pOverflow);
910 ------------------------------------------------------------------------------
911 FUNCTION NAME: noise_estimate_update
912 ------------------------------------------------------------------------------
913 INPUT AND OUTPUT DEFINITIONS
916 st -- pointer to type vadState1 -- State struct
917 level -- array of type Word16 -- sub-band levels of the input frame
920 st -- pointer to type vadState1 -- State struct
921 pOverflow -- pointer to type Flag -- overflow indicator
926 Global Variables Used:
929 Local Variables Needed:
932 ------------------------------------------------------------------------------
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
941 ------------------------------------------------------------------------------
946 ------------------------------------------------------------------------------
949 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
951 ------------------------------------------------------------------------------
955 ------------------------------------------------------------------------------
957 [State any special notes, constraints or cautions for users of this function]
959 ------------------------------------------------------------------------------
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 */
973 /* Control update of bckr_est[] */
974 update_cntrl(st, level, pOverflow);
976 /* Choose update speed */
979 if (((0x7800 & st->vadreg) == 0) &&
980 ((st->pitch & 0x7800) == 0)
981 && (st->complex_hang_count == 0))
983 alpha_up = ALPHA_UP1;
984 alpha_down = ALPHA_DOWN1;
988 if ((st->stat_count == 0)
989 && (st->complex_hang_count == 0))
991 alpha_up = ALPHA_UP2;
992 alpha_down = ALPHA_DOWN2;
1002 /* Update noise estimate (bckr_est) */
1003 for (i = 0; i < COMPLEN; i++)
1007 temp = sub(st->old_level[i], st->bckr_est[i], pOverflow);
1010 { /* update downwards*/
1011 temp = mult_r(alpha_down, temp, pOverflow);
1012 temp = add_16(st->bckr_est[i], temp, pOverflow);
1014 st->bckr_est[i] = add_16(-2, temp, pOverflow);
1016 /* limit minimum value of the noise estimate to NOISE_MIN */
1017 if (st->bckr_est[i] < NOISE_MIN)
1019 st->bckr_est[i] = NOISE_MIN;
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);
1028 /* limit maximum value of the noise estimate to NOISE_MAX */
1029 if (st->bckr_est[i] > NOISE_MAX)
1031 st->bckr_est[i] = NOISE_MAX;
1036 /* Update signal levels of the previous frame (old_level) */
1037 for (i = 0; i < COMPLEN; i++)
1039 st->old_level[i] = level[i];
1045 ------------------------------------------------------------------------------
1046 FUNCTION NAME: complex_estimate_adapt
1047 ------------------------------------------------------------------------------
1048 INPUT AND OUTPUT DEFINITIONS
1051 st -- pointer to type vadState1 -- State struct
1052 low_power -- Word16 -- very low level flag of the input frame
1055 st -- pointer to type vadState1 -- State struct
1056 pOverflow -- pointer to type Flag -- overflow indicator
1061 Global Variables Used:
1064 Local Variables Needed:
1067 ------------------------------------------------------------------------------
1068 FUNCTION DESCRIPTION
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
1075 ------------------------------------------------------------------------------
1080 ------------------------------------------------------------------------------
1083 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1085 ------------------------------------------------------------------------------
1089 ------------------------------------------------------------------------------
1091 [State any special notes, constraints or cautions for users of this function]
1093 ------------------------------------------------------------------------------
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 */
1102 Word16 alpha; /* Q15 */
1103 Word32 L_tmp; /* Q31 */
1106 /* adapt speed on own state */
1107 if (st->best_corr_hp < st->corr_hp_fast) /* decrease */
1109 if (st->corr_hp_fast < CVAD_THRESH_ADAPT_HIGH)
1111 alpha = CVAD_ADAPT_FAST;
1115 alpha = CVAD_ADAPT_REALLY_FAST;
1120 if (st->corr_hp_fast < CVAD_THRESH_ADAPT_HIGH)
1122 alpha = CVAD_ADAPT_FAST;
1126 alpha = CVAD_ADAPT_SLOW;
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 */
1135 if (st->corr_hp_fast < CVAD_MIN_CORR)
1137 st->corr_hp_fast = CVAD_MIN_CORR;
1142 st->corr_hp_fast = CVAD_MIN_CORR;
1148 ------------------------------------------------------------------------------
1149 FUNCTION NAME: complex_vad
1150 ------------------------------------------------------------------------------
1151 INPUT AND OUTPUT DEFINITIONS
1154 st -- pointer to type vadState1 -- State struct
1155 low_power -- Word16 -- flag power of the input frame
1158 st -- pointer to type vadState1 -- State struct
1159 pOverflow -- pointer to type Flag -- overflow indicator
1163 the complex background decision
1165 Global Variables Used:
1168 Local Variables Needed:
1171 ------------------------------------------------------------------------------
1172 FUNCTION DESCRIPTION
1174 Purpose : complex background decision
1175 Return value : the complex background decision
1177 ------------------------------------------------------------------------------
1182 ------------------------------------------------------------------------------
1185 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1187 ------------------------------------------------------------------------------
1191 ------------------------------------------------------------------------------
1193 [State any special notes, constraints or cautions for users of this function]
1195 ------------------------------------------------------------------------------
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 */
1204 st->complex_high = shr(st->complex_high, 1, pOverflow);
1205 st->complex_low = shr(st->complex_low, 1, pOverflow);
1209 if (st->corr_hp_fast > CVAD_THRESH_ADAPT_HIGH)
1211 st->complex_high |= 0x4000;
1214 if (st->corr_hp_fast > CVAD_THRESH_ADAPT_LOW)
1216 st->complex_low |= 0x4000;
1220 if (st->corr_hp_fast > CVAD_THRESH_HANG)
1222 st->complex_hang_timer = add_16(st->complex_hang_timer, 1, pOverflow);
1226 st->complex_hang_timer = 0;
1229 return ((Word16)(st->complex_high & 0x7f80) == 0x7f80 ||
1230 (Word16)(st->complex_low & 0x7fff) == 0x7fff);
1235 ------------------------------------------------------------------------------
1236 FUNCTION NAME: vad_decision
1237 ------------------------------------------------------------------------------
1238 INPUT AND OUTPUT DEFINITIONS
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
1246 st -- pointer to type vadState1 -- State struct
1247 pOverflow -- pointer to type Flag -- overflow indicator
1252 Global Variables Used:
1255 Local Variables Needed:
1258 ------------------------------------------------------------------------------
1259 FUNCTION DESCRIPTION
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
1268 ------------------------------------------------------------------------------
1273 ------------------------------------------------------------------------------
1276 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1278 ------------------------------------------------------------------------------
1282 ------------------------------------------------------------------------------
1284 [State any special notes, constraints or cautions for users of this function]
1286 ------------------------------------------------------------------------------
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 */
1302 Word16 low_power_flag;
1306 Calculate squared sum of the input levels (level)
1307 divided by the background noise components (bckr_est).
1311 for (i = 0; i < COMPLEN; i++)
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);
1322 snr_sum = (Word16)(L_shl(L_temp, 6, pOverflow) >> 16);
1323 snr_sum = mult(snr_sum, INV_COMPLEN, pOverflow);
1325 /* Calculate average level of estimated background noise */
1327 for (i = 0; i < COMPLEN; i++)
1329 L_temp = L_add(L_temp, st->bckr_est[i], pOverflow);
1332 noise_level = (Word16)(L_shl(L_temp, 13, pOverflow) >> 16);
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);
1339 if (vad_thr < VAD_THR_LOW)
1341 vad_thr = VAD_THR_LOW;
1344 /* Shift VAD decision register */
1345 st->vadreg = shr(st->vadreg, 1, pOverflow);
1347 /* Make intermediate VAD decision */
1348 if (snr_sum > vad_thr)
1350 st->vadreg |= 0x4000;
1352 /* primary vad decsion made */
1354 /* check if the input power (pow_sum) is lower than a threshold" */
1355 if (L_sub(pow_sum, VAD_POW_LOW, pOverflow) < 0)
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);
1368 /* check multiple thresholds of the st->corr_hp_fast value */
1369 st->complex_warning = complex_vad(st, low_power_flag, pOverflow);
1371 /* Update speech subband vad background noise estimates */
1372 noise_estimate_update(st, level, pOverflow);
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);
1378 return (st->speech_vad_decision);
1383 ------------------------------------------------------------------------------
1384 FUNCTION NAME: vad1_init
1385 ------------------------------------------------------------------------------
1386 INPUT AND OUTPUT DEFINITIONS
1389 state -- double pointer to type vadState1 -- pointer to memory to
1393 state -- points to initalized area in memory.
1398 Global Variables Used:
1401 Local Variables Needed:
1404 ------------------------------------------------------------------------------
1405 FUNCTION DESCRIPTION
1407 Allocates state memory and initializes state memory
1409 ------------------------------------------------------------------------------
1414 ------------------------------------------------------------------------------
1417 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1419 ------------------------------------------------------------------------------
1423 ------------------------------------------------------------------------------
1425 [State any special notes, constraints or cautions for users of this function]
1427 ------------------------------------------------------------------------------
1430 Word16 vad1_init(vadState1 **state)
1434 if (state == (vadState1 **) NULL)
1440 /* allocate memory */
1441 if ((s = (vadState1 *) oscl_malloc(sizeof(vadState1))) == NULL)
1454 ------------------------------------------------------------------------------
1455 FUNCTION NAME: vad1_reset
1456 ------------------------------------------------------------------------------
1457 INPUT AND OUTPUT DEFINITIONS
1460 state -- pointer to type vadState1 -- State struct
1463 state -- pointer to type vadState1 -- State struct
1468 Global Variables Used:
1471 Local Variables Needed:
1474 ------------------------------------------------------------------------------
1475 FUNCTION DESCRIPTION
1477 Purpose: Resets state memory to zero
1479 ------------------------------------------------------------------------------
1484 ------------------------------------------------------------------------------
1487 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1489 ------------------------------------------------------------------------------
1493 ------------------------------------------------------------------------------
1495 [State any special notes, constraints or cautions for users of this function]
1497 ------------------------------------------------------------------------------
1500 Word16 vad1_reset(vadState1 *state)
1505 if (state == (vadState1 *) NULL)
1510 /* Initialize pitch detection variables */
1511 state->oldlag_count = 0;
1516 state->complex_high = 0;
1517 state->complex_low = 0;
1518 state->complex_hang_timer = 0;
1522 state->stat_count = 0;
1523 state->burst_count = 0;
1524 state->hang_count = 0;
1525 state->complex_hang_count = 0;
1527 /* initialize memory used by the filter bank */
1528 for (i = 0; i < 3; i++)
1530 for (j = 0; j < 2; j++)
1532 state->a_data5[i][j] = 0;
1536 for (i = 0; i < 5; i++)
1538 state->a_data3[i] = 0;
1541 /* initialize the rest of the memory */
1542 for (i = 0; i < COMPLEN; i++)
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;
1550 state->best_corr_hp = CVAD_LOWPOW_RESET;
1552 state->speech_vad_decision = 0;
1553 state->complex_warning = 0;
1554 state->sp_burst_count = 0;
1556 state->corr_hp_fast = CVAD_LOWPOW_RESET;
1563 ------------------------------------------------------------------------------
1564 FUNCTION NAME: vad1_exit
1565 ------------------------------------------------------------------------------
1566 INPUT AND OUTPUT DEFINITIONS
1569 state -- pointer to type vadState1 -- State struct
1577 Global Variables Used:
1580 Local Variables Needed:
1583 ------------------------------------------------------------------------------
1584 FUNCTION DESCRIPTION
1586 The memory used for state memory is freed
1588 ------------------------------------------------------------------------------
1593 ------------------------------------------------------------------------------
1596 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1598 ------------------------------------------------------------------------------
1602 ------------------------------------------------------------------------------
1604 [State any special notes, constraints or cautions for users of this function]
1606 ------------------------------------------------------------------------------
1609 void vad1_exit(vadState1 **state)
1611 if (state == NULL || *state == NULL)
1614 /* deallocate memory */
1623 ------------------------------------------------------------------------------
1624 FUNCTION NAME: vad_complex_detection_update
1625 ------------------------------------------------------------------------------
1626 INPUT AND OUTPUT DEFINITIONS
1629 best_corr_hp -- Word16 -- best Corr
1630 state -- pointer to type vadState1 -- State struct
1633 state -- pointer to type vadState1 -- State struct
1638 Global Variables Used:
1641 Local Variables Needed:
1644 ------------------------------------------------------------------------------
1645 FUNCTION DESCRIPTION
1647 Purpose : update vad->bestCorr_hp complex signal feature state
1648 ------------------------------------------------------------------------------
1653 ------------------------------------------------------------------------------
1656 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1658 ------------------------------------------------------------------------------
1662 ------------------------------------------------------------------------------
1664 [State any special notes, constraints or cautions for users of this function]
1666 ------------------------------------------------------------------------------
1669 void vad_complex_detection_update(
1670 vadState1 *st, /* i/o : State struct */
1671 Word16 best_corr_hp) /* i : best Corr */
1673 st->best_corr_hp = best_corr_hp;
1679 ------------------------------------------------------------------------------
1680 FUNCTION NAME: vad_tone_detection
1681 ------------------------------------------------------------------------------
1682 INPUT AND OUTPUT DEFINITIONS
1685 st -- pointer to type vadState1 -- State struct
1686 t0 -- Word32 -- autocorrelation maxima
1687 t1 -- Word32 -- energy
1690 st -- pointer to type vadState1 -- State struct
1691 pOverflow -- pointer to type Flag -- overflow indicator
1696 Global Variables Used:
1699 Local Variables Needed:
1702 ------------------------------------------------------------------------------
1703 FUNCTION DESCRIPTION
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 ------------------------------------------------------------------------------
1714 ------------------------------------------------------------------------------
1717 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1719 ------------------------------------------------------------------------------
1723 ------------------------------------------------------------------------------
1725 [State any special notes, constraints or cautions for users of this function]
1727 ------------------------------------------------------------------------------
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 */
1739 if (t0 > TONE_THR * t1)
1742 temp = pv_round(t1, pOverflow);
1744 if ((temp > 0) && (L_msu(t0, temp, TONE_THR, pOverflow) > 0))
1752 ------------------------------------------------------------------------------
1753 FUNCTION NAME: vad_tone_detection_update
1754 ------------------------------------------------------------------------------
1755 INPUT AND OUTPUT DEFINITIONS
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
1763 st -- pointer to type vadState1 -- State struct
1764 pOverflow -- pointer to type Flag -- overflow indicator
1769 Global Variables Used:
1772 Local Variables Needed:
1775 ------------------------------------------------------------------------------
1776 FUNCTION DESCRIPTION
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.
1782 ------------------------------------------------------------------------------
1787 ------------------------------------------------------------------------------
1790 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1792 ------------------------------------------------------------------------------
1796 ------------------------------------------------------------------------------
1798 [State any special notes, constraints or cautions for users of this function]
1800 ------------------------------------------------------------------------------
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 */
1811 /* Shift tone flags right by one bit */
1812 st->tone = shr(st->tone, 1, pOverflow);
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)
1818 st->tone = shr(st->tone, 1, pOverflow);
1825 ------------------------------------------------------------------------------
1826 FUNCTION NAME: vad_pitch_detection
1827 ------------------------------------------------------------------------------
1828 INPUT AND OUTPUT DEFINITIONS
1831 T_op -- array of type Word16 -- speech encoder open loop lags
1832 st -- pointer to type vadState1 -- State struct
1835 st -- pointer to type vadState1 -- State struct
1836 pOverflow -- pointer to type Flag -- overflow indicator
1841 Global Variables Used:
1844 Local Variables Needed:
1847 ------------------------------------------------------------------------------
1848 FUNCTION DESCRIPTION
1850 Purpose : Test whether signal contains pitch or other periodic
1852 Return value : Boolean voiced / unvoiced decision in state variable
1854 ------------------------------------------------------------------------------
1859 ------------------------------------------------------------------------------
1862 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1864 ------------------------------------------------------------------------------
1868 ------------------------------------------------------------------------------
1870 [State any special notes, constraints or cautions for users of this function]
1872 ------------------------------------------------------------------------------
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 */
1887 for (i = 0; i < 2; i++)
1889 temp = sub(st->oldlag, T_op[i], pOverflow);
1897 /* Save the current LTP lag */
1898 st->oldlag = T_op[i];
1901 /* Make pitch decision.
1902 Save flag of the pitch detection to the variable pitch.
1904 st->pitch = shr(st->pitch, 1, pOverflow);
1912 if (temp >= NTHRESH)
1914 st->pitch |= 0x4000;
1917 /* Update oldlagcount */
1918 st->oldlag_count = lagcount;
1922 ------------------------------------------------------------------------------
1924 ------------------------------------------------------------------------------
1925 INPUT AND OUTPUT DEFINITIONS
1928 st -- pointer to type vadState1 -- State struct
1929 in_buf -- array of type Word16 -- samples of the input frame
1932 st -- pointer to type vadState1 -- State struct
1933 pOverflow -- pointer to type Flag -- overflow indicator
1936 VAD Decision, 1 = speech, 0 = noise
1938 Global Variables Used:
1941 Local Variables Needed:
1944 ------------------------------------------------------------------------------
1945 FUNCTION DESCRIPTION
1947 Purpose : Main program for Voice Activity Detection (VAD) for AMR
1948 Return value : VAD Decision, 1 = speech, 0 = noise
1950 ------------------------------------------------------------------------------
1955 ------------------------------------------------------------------------------
1958 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1960 ------------------------------------------------------------------------------
1964 ------------------------------------------------------------------------------
1966 [State any special notes, constraints or cautions for users of this function]
1968 ------------------------------------------------------------------------------
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 */
1977 Word16 level[COMPLEN];
1981 /* Calculate power of the input frame. */
1984 for (i = 0; i < FRAME_LEN; i++)
1986 pow_sum = L_mac(pow_sum, in_buf[i-LOOKAHEAD], in_buf[i-LOOKAHEAD], pOverflow);
1990 If input power is very low, clear pitch flag of the current frame
1992 if (L_sub(pow_sum, POW_PITCH_THR, pOverflow) < 0)
1994 st->pitch = st->pitch & 0x3fff;
1998 If input power is very low, clear complex flag of the "current" frame
2000 if (L_sub(pow_sum, POW_COMPLEX_THR, pOverflow) < 0)
2002 st->complex_low = st->complex_low & 0x3fff;
2006 Run the filter bank which calculates signal levels at each band
2008 filter_bank(st, in_buf, level, pOverflow);
2010 return (vad_decision(st, level, pow_sum, pOverflow));