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 temp1 = shr(in[4*i+0], 2, pOverflow);
149 temp0 = sub(temp1, temp0, pOverflow);
151 temp1 = mult(COEFF5_1, temp0, pOverflow);
152 temp1 = add(data0, temp1, pOverflow);
154 temp3 = mult(COEFF5_2, data1, pOverflow);
155 temp2 = shr(in[4*i+1], 2, pOverflow);
157 temp3 = sub(temp2, temp3, pOverflow);
159 temp2 = mult(COEFF5_2, temp3, pOverflow);
160 temp2 = add(data1, temp2, pOverflow);
162 out[4*i+0] = add(temp1, temp2, pOverflow);
163 out[4*i+1] = sub(temp1, temp2, pOverflow);
165 temp1 = mult(COEFF5_1, temp0, pOverflow);
166 temp2 = shr(in[4*i+2], 2, pOverflow);
167 data0 = sub(temp2, temp1, pOverflow);
169 temp1 = mult(COEFF5_1, data0, pOverflow);
170 temp1 = add(temp0, temp1, pOverflow);
172 data1 = mult(COEFF5_2, temp3, pOverflow);
173 temp2 = shr(in[4*i+3], 2, pOverflow);
174 data1 = sub(temp2, data1, pOverflow);
176 temp2 = mult(COEFF5_2, data1, pOverflow);
177 temp2 = add(temp3, temp2, pOverflow);
179 out[4*i+2] = add(temp1, temp2, pOverflow);
180 out[4*i+3] = sub(temp1, temp2, pOverflow);
189 ------------------------------------------------------------------------------
190 FUNCTION NAME: filter5
191 ------------------------------------------------------------------------------
192 INPUT AND OUTPUT DEFINITIONS
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
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
208 Global Variables Used:
211 Local Variables Needed:
214 ------------------------------------------------------------------------------
217 Purpose : Fifth-order half-band lowpass/highpass filter pair with
219 ------------------------------------------------------------------------------
224 ------------------------------------------------------------------------------
227 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
229 ------------------------------------------------------------------------------
233 ------------------------------------------------------------------------------
235 [State any special notes, constraints or cautions for users of this function]
237 ------------------------------------------------------------------------------
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 */
250 temp0 = mult(COEFF5_1, data[0], pOverflow);
251 temp0 = sub(*in0, temp0, pOverflow);
253 temp1 = mult(COEFF5_1, temp0, pOverflow);
254 temp1 = add(data[0], temp1, pOverflow);
257 temp0 = mult(COEFF5_2, data[1], pOverflow);
258 temp0 = sub(*in1, temp0, pOverflow);
260 temp2 = mult(COEFF5_2, temp0, pOverflow);
261 temp2 = add(data[1], temp2, pOverflow);
265 temp0 = add(temp1, temp2, pOverflow);
266 *in0 = shr(temp0, 1, pOverflow);
268 temp0 = sub(temp1, temp2, pOverflow);
269 *in1 = shr(temp0, 1, pOverflow);
276 ------------------------------------------------------------------------------
277 FUNCTION NAME: filter3
278 ------------------------------------------------------------------------------
279 INPUT AND OUTPUT DEFINITIONS
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
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
296 Global Variables Used:
299 Local Variables Needed:
302 ------------------------------------------------------------------------------
305 Purpose : Third-order half-band lowpass/highpass filter pair with
307 ------------------------------------------------------------------------------
312 ------------------------------------------------------------------------------
315 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
317 ------------------------------------------------------------------------------
321 ------------------------------------------------------------------------------
323 [State any special notes, constraints or cautions for users of this function]
325 ------------------------------------------------------------------------------
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 */
338 temp1 = mult(COEFF3, *data, pOverflow);
339 temp1 = sub(*in1, temp1, pOverflow);
341 temp2 = mult(COEFF3, temp1, pOverflow);
342 temp2 = add(*data, temp2, pOverflow);
346 temp1 = sub(*in0, temp2, pOverflow);
348 *in1 = shr(temp1, 1, pOverflow);
350 temp1 = add(*in0, temp2, pOverflow);
352 *in0 = shr(temp1, 1, pOverflow);
359 ------------------------------------------------------------------------------
360 FUNCTION NAME: level_calculation
361 ------------------------------------------------------------------------------
362 INPUT AND OUTPUT DEFINITIONS
365 data -- array of type Word16 -- signal buffer
366 sub_level -- pointer to type Word16 -- level calculated at the end of
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
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
383 Global Variables Used:
386 Local Variables Needed:
389 ------------------------------------------------------------------------------
392 Purpose : Calculate signal level in a sub-band. Level is calculated
393 by summing absolute values of the input data.
395 ------------------------------------------------------------------------------
400 ------------------------------------------------------------------------------
403 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
405 ------------------------------------------------------------------------------
409 ------------------------------------------------------------------------------
411 [State any special notes, constraints or cautions for users of this function]
413 ------------------------------------------------------------------------------
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 */
437 for (i = count1; i < count2; i++)
439 l_temp1 = L_mac(l_temp1, 1, abs_s(data[ind_m*i+ind_a]), pOverflow);
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));
445 for (i = 0; i < count1; i++)
447 l_temp2 = L_mac(l_temp2, 1, abs_s(data[ind_m*i+ind_a]), pOverflow);
449 level = extract_h(L_shl(l_temp2, scale, pOverflow));
458 ------------------------------------------------------------------------------
459 FUNCTION NAME: filter_bank
460 ------------------------------------------------------------------------------
461 INPUT AND OUTPUT DEFINITIONS
464 st -- pointer to type vadState1 -- State struct
465 in -- array of type Word16 -- input frame
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
475 Global Variables Used:
478 Local Variables Needed:
481 ------------------------------------------------------------------------------
484 Purpose : Divides input signal into 9-bands and calculas level of
485 the signal in each band
487 ------------------------------------------------------------------------------
492 ------------------------------------------------------------------------------
495 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
497 ------------------------------------------------------------------------------
501 ------------------------------------------------------------------------------
503 [State any special notes, constraints or cautions for users of this function]
505 ------------------------------------------------------------------------------
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 */
516 Word16 tmp_buf[FRAME_LEN];
518 /* calculate the filter bank */
520 first_filter_stage(in, tmp_buf, st->a_data5[0], pOverflow);
522 for (i = 0; i < FRAME_LEN / 4; i++)
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);
527 for (i = 0; i < FRAME_LEN / 8; i++)
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);
534 for (i = 0; i < FRAME_LEN / 16; i++)
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);
540 /* calculate levels in each frequency band */
543 level[8] = level_calculation(tmp_buf, &st->sub_level[8], FRAME_LEN / 4 - 8,
544 FRAME_LEN / 4, 4, 1, 15, pOverflow);
546 level[7] = level_calculation(tmp_buf, &st->sub_level[7], FRAME_LEN / 8 - 4,
547 FRAME_LEN / 8, 8, 7, 16, pOverflow);
549 level[6] = level_calculation(tmp_buf, &st->sub_level[6], FRAME_LEN / 8 - 4,
550 FRAME_LEN / 8, 8, 3, 16, pOverflow);
552 level[5] = level_calculation(tmp_buf, &st->sub_level[5], FRAME_LEN / 8 - 4,
553 FRAME_LEN / 8, 8, 2, 16, pOverflow);
555 level[4] = level_calculation(tmp_buf, &st->sub_level[4], FRAME_LEN / 8 - 4,
556 FRAME_LEN / 8, 8, 6, 16, pOverflow);
558 level[3] = level_calculation(tmp_buf, &st->sub_level[3], FRAME_LEN / 16 - 2,
559 FRAME_LEN / 16, 16, 4, 16, pOverflow);
561 level[2] = level_calculation(tmp_buf, &st->sub_level[2], FRAME_LEN / 16 - 2,
562 FRAME_LEN / 16, 16, 12, 16, pOverflow);
564 level[1] = level_calculation(tmp_buf, &st->sub_level[1], FRAME_LEN / 16 - 2,
565 FRAME_LEN / 16, 16, 8, 16, pOverflow);
567 level[0] = level_calculation(tmp_buf, &st->sub_level[0], FRAME_LEN / 16 - 2,
568 FRAME_LEN / 16, 16, 0, 16, pOverflow);
574 ------------------------------------------------------------------------------
575 FUNCTION NAME: update_cntrl
576 ------------------------------------------------------------------------------
577 INPUT AND OUTPUT DEFINITIONS
580 st -- pointer to type vadState1 -- State struct
581 level -- array of type Word16 -- sub-band levels of the input frame
584 st -- pointer to type vadState1 -- State struct
585 pOverflow -- pointer to type Flag -- overflow indicator
590 Global Variables Used:
593 Local Variables Needed:
596 ------------------------------------------------------------------------------
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
608 ------------------------------------------------------------------------------
613 ------------------------------------------------------------------------------
616 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
618 ------------------------------------------------------------------------------
622 ------------------------------------------------------------------------------
624 [State any special notes, constraints or cautions for users of this function]
626 ------------------------------------------------------------------------------
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 */
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)
648 if (st->stat_count < CAD_MIN_STAT_COUNT)
650 st->stat_count = CAD_MIN_STAT_COUNT;
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 */
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))
661 st->stat_count = STAT_COUNT;
665 /* if 8 last vad-decisions have been "0", reinitialize stat_count */
666 if ((st->vadreg & 0x7f80) == 0)
668 st->stat_count = STAT_COUNT;
673 for (i = 0; i < COMPLEN; i++)
675 if (level[i] > st->ave_level[i])
678 denom = st->ave_level[i];
682 num = st->ave_level[i];
685 /* Limit nimimum value of num and denom to STAT_THR_LEVEL */
686 if (num < STAT_THR_LEVEL)
688 num = STAT_THR_LEVEL;
690 if (denom < STAT_THR_LEVEL)
692 denom = STAT_THR_LEVEL;
697 denom = shl(denom, exp, pOverflow);
699 /* stat_rat = num/denom * 64 */
700 temp = shr(num, 1, pOverflow);
701 temp = div_s(temp, denom);
703 stat_rat = add(stat_rat, shr(temp, sub(8, exp, pOverflow), pOverflow), pOverflow);
706 /* compare stat_rat with a threshold and update stat_count */
707 if (stat_rat > STAT_THR)
709 st->stat_count = STAT_COUNT;
713 if ((st->vadreg & 0x4000) != 0)
715 if (st->stat_count != 0)
717 st->stat_count = sub(st->stat_count, 1, pOverflow);
724 /* Update average amplitude estimate for stationarity estimation */
726 if (st->stat_count == STAT_COUNT)
730 else if ((st->vadreg & 0x4000) == 0)
735 for (i = 0; i < COMPLEN; i++)
737 temp = sub(level[i], st->ave_level[i], pOverflow);
738 temp = mult_r(alpha, temp, pOverflow);
751 ------------------------------------------------------------------------------
752 FUNCTION NAME: hangover_addition
753 ------------------------------------------------------------------------------
754 INPUT AND OUTPUT DEFINITIONS
757 noise_level -- Word16 -- average level of the noise estimates
758 low_power -- Word16 -- flag power of the input frame
761 st -- pointer to type vadState1 -- State struct
762 pOverflow -- pointer to type Flag -- overflow indicato
765 VAD_flag indicating final VAD decision (Word16)
767 Global Variables Used:
770 Local Variables Needed:
773 ------------------------------------------------------------------------------
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
786 ------------------------------------------------------------------------------
791 ------------------------------------------------------------------------------
794 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
796 ------------------------------------------------------------------------------
800 ------------------------------------------------------------------------------
802 [State any special notes, constraints or cautions for users of this function]
804 ------------------------------------------------------------------------------
807 static Word16 hangover_addition(
808 vadState1 *st, /* i/o : State struct */
809 Word16 noise_level, /* i : average level of the noise */
811 Word16 low_power, /* i : flag power of the input frame */
812 Flag *pOverflow /* o : Flag set when overflow occurs */
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
825 if (noise_level > HANG_NOISE_THR)
827 burst_len = BURST_LEN_HIGH_NOISE;
828 hang_len = HANG_LEN_HIGH_NOISE;
832 burst_len = BURST_LEN_LOW_NOISE;
833 hang_len = HANG_LEN_LOW_NOISE;
836 /* if the input power (pow_sum) is lower than a threshold, clear
837 counters and set VAD_flag to "0" "fast exit" */
842 st->complex_hang_count = 0;
843 st->complex_hang_timer = 0;
847 if (st->complex_hang_timer > CVAD_HANG_LIMIT)
849 if (st->complex_hang_count < CVAD_HANG_LENGTH)
851 st->complex_hang_count = CVAD_HANG_LENGTH;
855 /* long time very complex signal override VAD output function */
856 if (st->complex_hang_count != 0)
858 st->burst_count = BURST_LEN_HIGH_NOISE;
859 st->complex_hang_count = sub(st->complex_hang_count, 1, pOverflow);
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))
872 /* update the counters (hang_count, burst_count) */
873 if ((st->vadreg & 0x4000) != 0)
875 st->burst_count = add(st->burst_count, 1, pOverflow);
877 if (st->burst_count >= burst_len)
879 st->hang_count = hang_len;
886 if (st->hang_count > 0)
888 st->hang_count = sub(st->hang_count, 1, pOverflow);
898 ------------------------------------------------------------------------------
899 FUNCTION NAME: noise_estimate_update
900 ------------------------------------------------------------------------------
901 INPUT AND OUTPUT DEFINITIONS
904 st -- pointer to type vadState1 -- State struct
905 level -- array of type Word16 -- sub-band levels of the input frame
908 st -- pointer to type vadState1 -- State struct
909 pOverflow -- pointer to type Flag -- overflow indicator
914 Global Variables Used:
917 Local Variables Needed:
920 ------------------------------------------------------------------------------
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
929 ------------------------------------------------------------------------------
934 ------------------------------------------------------------------------------
937 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
939 ------------------------------------------------------------------------------
943 ------------------------------------------------------------------------------
945 [State any special notes, constraints or cautions for users of this function]
947 ------------------------------------------------------------------------------
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 */
961 /* Control update of bckr_est[] */
962 update_cntrl(st, level, pOverflow);
964 /* Choose update speed */
967 if (((0x7800 & st->vadreg) == 0) &&
968 ((st->pitch & 0x7800) == 0)
969 && (st->complex_hang_count == 0))
971 alpha_up = ALPHA_UP1;
972 alpha_down = ALPHA_DOWN1;
976 if ((st->stat_count == 0)
977 && (st->complex_hang_count == 0))
979 alpha_up = ALPHA_UP2;
980 alpha_down = ALPHA_DOWN2;
990 /* Update noise estimate (bckr_est) */
991 for (i = 0; i < COMPLEN; i++)
995 temp = sub(st->old_level[i], st->bckr_est[i], pOverflow);
998 { /* update downwards*/
999 temp = mult_r(alpha_down, temp, pOverflow);
1000 temp = add(st->bckr_est[i], temp, pOverflow);
1002 st->bckr_est[i] = add(-2, temp, pOverflow);
1004 /* limit minimum value of the noise estimate to NOISE_MIN */
1005 if (st->bckr_est[i] < NOISE_MIN)
1007 st->bckr_est[i] = NOISE_MIN;
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);
1016 /* limit maximum value of the noise estimate to NOISE_MAX */
1017 if (st->bckr_est[i] > NOISE_MAX)
1019 st->bckr_est[i] = NOISE_MAX;
1024 /* Update signal levels of the previous frame (old_level) */
1025 for (i = 0; i < COMPLEN; i++)
1027 st->old_level[i] = level[i];
1033 ------------------------------------------------------------------------------
1034 FUNCTION NAME: complex_estimate_adapt
1035 ------------------------------------------------------------------------------
1036 INPUT AND OUTPUT DEFINITIONS
1039 st -- pointer to type vadState1 -- State struct
1040 low_power -- Word16 -- very low level flag of the input frame
1043 st -- pointer to type vadState1 -- State struct
1044 pOverflow -- pointer to type Flag -- overflow indicator
1049 Global Variables Used:
1052 Local Variables Needed:
1055 ------------------------------------------------------------------------------
1056 FUNCTION DESCRIPTION
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
1063 ------------------------------------------------------------------------------
1068 ------------------------------------------------------------------------------
1071 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1073 ------------------------------------------------------------------------------
1077 ------------------------------------------------------------------------------
1079 [State any special notes, constraints or cautions for users of this function]
1081 ------------------------------------------------------------------------------
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 */
1090 Word16 alpha; /* Q15 */
1091 Word32 L_tmp; /* Q31 */
1094 /* adapt speed on own state */
1095 if (st->best_corr_hp < st->corr_hp_fast) /* decrease */
1097 if (st->corr_hp_fast < CVAD_THRESH_ADAPT_HIGH)
1099 alpha = CVAD_ADAPT_FAST;
1103 alpha = CVAD_ADAPT_REALLY_FAST;
1108 if (st->corr_hp_fast < CVAD_THRESH_ADAPT_HIGH)
1110 alpha = CVAD_ADAPT_FAST;
1114 alpha = CVAD_ADAPT_SLOW;
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 */
1123 if (st->corr_hp_fast < CVAD_MIN_CORR)
1125 st->corr_hp_fast = CVAD_MIN_CORR;
1130 st->corr_hp_fast = CVAD_MIN_CORR;
1136 ------------------------------------------------------------------------------
1137 FUNCTION NAME: complex_vad
1138 ------------------------------------------------------------------------------
1139 INPUT AND OUTPUT DEFINITIONS
1142 st -- pointer to type vadState1 -- State struct
1143 low_power -- Word16 -- flag power of the input frame
1146 st -- pointer to type vadState1 -- State struct
1147 pOverflow -- pointer to type Flag -- overflow indicator
1151 the complex background decision
1153 Global Variables Used:
1156 Local Variables Needed:
1159 ------------------------------------------------------------------------------
1160 FUNCTION DESCRIPTION
1162 Purpose : complex background decision
1163 Return value : the complex background decision
1165 ------------------------------------------------------------------------------
1170 ------------------------------------------------------------------------------
1173 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1175 ------------------------------------------------------------------------------
1179 ------------------------------------------------------------------------------
1181 [State any special notes, constraints or cautions for users of this function]
1183 ------------------------------------------------------------------------------
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 */
1192 st->complex_high = shr(st->complex_high, 1, pOverflow);
1193 st->complex_low = shr(st->complex_low, 1, pOverflow);
1197 if (st->corr_hp_fast > CVAD_THRESH_ADAPT_HIGH)
1199 st->complex_high |= 0x4000;
1202 if (st->corr_hp_fast > CVAD_THRESH_ADAPT_LOW)
1204 st->complex_low |= 0x4000;
1208 if (st->corr_hp_fast > CVAD_THRESH_HANG)
1210 st->complex_hang_timer = add(st->complex_hang_timer, 1, pOverflow);
1214 st->complex_hang_timer = 0;
1217 return ((Word16)(st->complex_high & 0x7f80) == 0x7f80 ||
1218 (Word16)(st->complex_low & 0x7fff) == 0x7fff);
1223 ------------------------------------------------------------------------------
1224 FUNCTION NAME: vad_decision
1225 ------------------------------------------------------------------------------
1226 INPUT AND OUTPUT DEFINITIONS
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
1234 st -- pointer to type vadState1 -- State struct
1235 pOverflow -- pointer to type Flag -- overflow indicator
1240 Global Variables Used:
1243 Local Variables Needed:
1246 ------------------------------------------------------------------------------
1247 FUNCTION DESCRIPTION
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
1256 ------------------------------------------------------------------------------
1261 ------------------------------------------------------------------------------
1264 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1266 ------------------------------------------------------------------------------
1270 ------------------------------------------------------------------------------
1272 [State any special notes, constraints or cautions for users of this function]
1274 ------------------------------------------------------------------------------
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 */
1290 Word16 low_power_flag;
1294 Calculate squared sum of the input levels (level)
1295 divided by the background noise components (bckr_est).
1299 for (i = 0; i < COMPLEN; i++)
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);
1310 snr_sum = extract_h(L_shl(L_temp, 6, pOverflow));
1311 snr_sum = mult(snr_sum, INV_COMPLEN, pOverflow);
1313 /* Calculate average level of estimated background noise */
1315 for (i = 0; i < COMPLEN; i++)
1317 L_temp = L_add(L_temp, st->bckr_est[i], pOverflow);
1320 noise_level = extract_h(L_shl(L_temp, 13, pOverflow));
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);
1327 if (vad_thr < VAD_THR_LOW)
1329 vad_thr = VAD_THR_LOW;
1332 /* Shift VAD decision register */
1333 st->vadreg = shr(st->vadreg, 1, pOverflow);
1335 /* Make intermediate VAD decision */
1336 if (snr_sum > vad_thr)
1338 st->vadreg |= 0x4000;
1340 /* primary vad decsion made */
1342 /* check if the input power (pow_sum) is lower than a threshold" */
1343 if (L_sub(pow_sum, VAD_POW_LOW, pOverflow) < 0)
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);
1356 /* check multiple thresholds of the st->corr_hp_fast value */
1357 st->complex_warning = complex_vad(st, low_power_flag, pOverflow);
1359 /* Update speech subband vad background noise estimates */
1360 noise_estimate_update(st, level, pOverflow);
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);
1366 return (st->speech_vad_decision);
1371 ------------------------------------------------------------------------------
1372 FUNCTION NAME: vad1_init
1373 ------------------------------------------------------------------------------
1374 INPUT AND OUTPUT DEFINITIONS
1377 state -- double pointer to type vadState1 -- pointer to memory to
1381 state -- points to initalized area in memory.
1386 Global Variables Used:
1389 Local Variables Needed:
1392 ------------------------------------------------------------------------------
1393 FUNCTION DESCRIPTION
1395 Allocates state memory and initializes state memory
1397 ------------------------------------------------------------------------------
1402 ------------------------------------------------------------------------------
1405 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1407 ------------------------------------------------------------------------------
1411 ------------------------------------------------------------------------------
1413 [State any special notes, constraints or cautions for users of this function]
1415 ------------------------------------------------------------------------------
1418 Word16 vad1_init(vadState1 **state)
1422 if (state == (vadState1 **) NULL)
1428 /* allocate memory */
1429 if ((s = (vadState1 *) oscl_malloc(sizeof(vadState1))) == NULL)
1442 ------------------------------------------------------------------------------
1443 FUNCTION NAME: vad1_reset
1444 ------------------------------------------------------------------------------
1445 INPUT AND OUTPUT DEFINITIONS
1448 state -- pointer to type vadState1 -- State struct
1451 state -- pointer to type vadState1 -- State struct
1456 Global Variables Used:
1459 Local Variables Needed:
1462 ------------------------------------------------------------------------------
1463 FUNCTION DESCRIPTION
1465 Purpose: Resets state memory to zero
1467 ------------------------------------------------------------------------------
1472 ------------------------------------------------------------------------------
1475 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1477 ------------------------------------------------------------------------------
1481 ------------------------------------------------------------------------------
1483 [State any special notes, constraints or cautions for users of this function]
1485 ------------------------------------------------------------------------------
1488 Word16 vad1_reset(vadState1 *state)
1493 if (state == (vadState1 *) NULL)
1498 /* Initialize pitch detection variables */
1499 state->oldlag_count = 0;
1504 state->complex_high = 0;
1505 state->complex_low = 0;
1506 state->complex_hang_timer = 0;
1510 state->stat_count = 0;
1511 state->burst_count = 0;
1512 state->hang_count = 0;
1513 state->complex_hang_count = 0;
1515 /* initialize memory used by the filter bank */
1516 for (i = 0; i < 3; i++)
1518 for (j = 0; j < 2; j++)
1520 state->a_data5[i][j] = 0;
1524 for (i = 0; i < 5; i++)
1526 state->a_data3[i] = 0;
1529 /* initialize the rest of the memory */
1530 for (i = 0; i < COMPLEN; i++)
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;
1538 state->best_corr_hp = CVAD_LOWPOW_RESET;
1540 state->speech_vad_decision = 0;
1541 state->complex_warning = 0;
1542 state->sp_burst_count = 0;
1544 state->corr_hp_fast = CVAD_LOWPOW_RESET;
1551 ------------------------------------------------------------------------------
1552 FUNCTION NAME: vad1_exit
1553 ------------------------------------------------------------------------------
1554 INPUT AND OUTPUT DEFINITIONS
1557 state -- pointer to type vadState1 -- State struct
1565 Global Variables Used:
1568 Local Variables Needed:
1571 ------------------------------------------------------------------------------
1572 FUNCTION DESCRIPTION
1574 The memory used for state memory is freed
1576 ------------------------------------------------------------------------------
1581 ------------------------------------------------------------------------------
1584 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1586 ------------------------------------------------------------------------------
1590 ------------------------------------------------------------------------------
1592 [State any special notes, constraints or cautions for users of this function]
1594 ------------------------------------------------------------------------------
1597 void vad1_exit(vadState1 **state)
1599 if (state == NULL || *state == NULL)
1602 /* deallocate memory */
1611 ------------------------------------------------------------------------------
1612 FUNCTION NAME: vad_complex_detection_update
1613 ------------------------------------------------------------------------------
1614 INPUT AND OUTPUT DEFINITIONS
1617 best_corr_hp -- Word16 -- best Corr
1618 state -- pointer to type vadState1 -- State struct
1621 state -- pointer to type vadState1 -- State struct
1626 Global Variables Used:
1629 Local Variables Needed:
1632 ------------------------------------------------------------------------------
1633 FUNCTION DESCRIPTION
1635 Purpose : update vad->bestCorr_hp complex signal feature state
1636 ------------------------------------------------------------------------------
1641 ------------------------------------------------------------------------------
1644 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1646 ------------------------------------------------------------------------------
1650 ------------------------------------------------------------------------------
1652 [State any special notes, constraints or cautions for users of this function]
1654 ------------------------------------------------------------------------------
1657 void vad_complex_detection_update(
1658 vadState1 *st, /* i/o : State struct */
1659 Word16 best_corr_hp) /* i : best Corr */
1661 st->best_corr_hp = best_corr_hp;
1667 ------------------------------------------------------------------------------
1668 FUNCTION NAME: vad_tone_detection
1669 ------------------------------------------------------------------------------
1670 INPUT AND OUTPUT DEFINITIONS
1673 st -- pointer to type vadState1 -- State struct
1674 t0 -- Word32 -- autocorrelation maxima
1675 t1 -- Word32 -- energy
1678 st -- pointer to type vadState1 -- State struct
1679 pOverflow -- pointer to type Flag -- overflow indicator
1684 Global Variables Used:
1687 Local Variables Needed:
1690 ------------------------------------------------------------------------------
1691 FUNCTION DESCRIPTION
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 ------------------------------------------------------------------------------
1702 ------------------------------------------------------------------------------
1705 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1707 ------------------------------------------------------------------------------
1711 ------------------------------------------------------------------------------
1713 [State any special notes, constraints or cautions for users of this function]
1715 ------------------------------------------------------------------------------
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 */
1727 if (t0 > TONE_THR * t1)
1730 temp = pv_round(t1, pOverflow);
1732 if ((temp > 0) && (L_msu(t0, temp, TONE_THR, pOverflow) > 0))
1740 ------------------------------------------------------------------------------
1741 FUNCTION NAME: vad_tone_detection_update
1742 ------------------------------------------------------------------------------
1743 INPUT AND OUTPUT DEFINITIONS
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
1751 st -- pointer to type vadState1 -- State struct
1752 pOverflow -- pointer to type Flag -- overflow indicator
1757 Global Variables Used:
1760 Local Variables Needed:
1763 ------------------------------------------------------------------------------
1764 FUNCTION DESCRIPTION
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.
1770 ------------------------------------------------------------------------------
1775 ------------------------------------------------------------------------------
1778 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1780 ------------------------------------------------------------------------------
1784 ------------------------------------------------------------------------------
1786 [State any special notes, constraints or cautions for users of this function]
1788 ------------------------------------------------------------------------------
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 */
1799 /* Shift tone flags right by one bit */
1800 st->tone = shr(st->tone, 1, pOverflow);
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)
1806 st->tone = shr(st->tone, 1, pOverflow);
1813 ------------------------------------------------------------------------------
1814 FUNCTION NAME: vad_pitch_detection
1815 ------------------------------------------------------------------------------
1816 INPUT AND OUTPUT DEFINITIONS
1819 T_op -- array of type Word16 -- speech encoder open loop lags
1820 st -- pointer to type vadState1 -- State struct
1823 st -- pointer to type vadState1 -- State struct
1824 pOverflow -- pointer to type Flag -- overflow indicator
1829 Global Variables Used:
1832 Local Variables Needed:
1835 ------------------------------------------------------------------------------
1836 FUNCTION DESCRIPTION
1838 Purpose : Test whether signal contains pitch or other periodic
1840 Return value : Boolean voiced / unvoiced decision in state variable
1842 ------------------------------------------------------------------------------
1847 ------------------------------------------------------------------------------
1850 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1852 ------------------------------------------------------------------------------
1856 ------------------------------------------------------------------------------
1858 [State any special notes, constraints or cautions for users of this function]
1860 ------------------------------------------------------------------------------
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 */
1875 for (i = 0; i < 2; i++)
1877 temp = sub(st->oldlag, T_op[i], pOverflow);
1882 lagcount = add(lagcount, 1, pOverflow);
1885 /* Save the current LTP lag */
1886 st->oldlag = T_op[i];
1889 /* Make pitch decision.
1890 Save flag of the pitch detection to the variable pitch.
1892 st->pitch = shr(st->pitch, 1, pOverflow);
1900 if (temp >= NTHRESH)
1902 st->pitch |= 0x4000;
1905 /* Update oldlagcount */
1906 st->oldlag_count = lagcount;
1910 ------------------------------------------------------------------------------
1912 ------------------------------------------------------------------------------
1913 INPUT AND OUTPUT DEFINITIONS
1916 st -- pointer to type vadState1 -- State struct
1917 in_buf -- array of type Word16 -- samples of the input frame
1920 st -- pointer to type vadState1 -- State struct
1921 pOverflow -- pointer to type Flag -- overflow indicator
1924 VAD Decision, 1 = speech, 0 = noise
1926 Global Variables Used:
1929 Local Variables Needed:
1932 ------------------------------------------------------------------------------
1933 FUNCTION DESCRIPTION
1935 Purpose : Main program for Voice Activity Detection (VAD) for AMR
1936 Return value : VAD Decision, 1 = speech, 0 = noise
1938 ------------------------------------------------------------------------------
1943 ------------------------------------------------------------------------------
1946 vad1.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1948 ------------------------------------------------------------------------------
1952 ------------------------------------------------------------------------------
1954 [State any special notes, constraints or cautions for users of this function]
1956 ------------------------------------------------------------------------------
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 */
1965 Word16 level[COMPLEN];
1969 /* Calculate power of the input frame. */
1972 for (i = 0; i < FRAME_LEN; i++)
1974 pow_sum = L_mac(pow_sum, in_buf[i-LOOKAHEAD], in_buf[i-LOOKAHEAD], pOverflow);
1978 If input power is very low, clear pitch flag of the current frame
1980 if (L_sub(pow_sum, POW_PITCH_THR, pOverflow) < 0)
1982 st->pitch = st->pitch & 0x3fff;
1986 If input power is very low, clear complex flag of the "current" frame
1988 if (L_sub(pow_sum, POW_COMPLEX_THR, pOverflow) < 0)
1990 st->complex_low = st->complex_low & 0x3fff;
1994 Run the filter bank which calculates signal levels at each band
1996 filter_bank(st, in_buf, level, pOverflow);
1998 return (vad_decision(st, level, pow_sum, pOverflow));