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 ----------------------------------------------------------------------------*/
57 #include "mpy_32_16.h"
59 #include "l_extract.h"
72 /*----------------------------------------------------------------------------
74 ; Define module specific macros here
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
79 ; Include all pre-processor statements here. Include conditional
80 ; compile variables also.
81 ----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------
84 ; LOCAL FUNCTION DEFINITIONS
85 ; Function Prototype declaration
86 ----------------------------------------------------------------------------*/
88 /*----------------------------------------------------------------------------
89 ; LOCAL VARIABLE DEFINITIONS
90 ; Variable declaration - defined here and used outside this module
91 ----------------------------------------------------------------------------*/
94 * The channel table is defined below. In this table, the
95 * lower and higher frequency coefficients for each of the 16
96 * channels are specified. The table excludes the coefficients
97 * with numbers 0 (DC), 1, and 64 (Foldover frequency).
100 const Word16 ch_tbl[NUM_CHAN][2] =
122 /* channel energy scaling table - allows efficient division by number
123 * of DFT bins in the channel: 1/2, 1/3, 1/4, etc.
126 const Word16 ch_tbl_sh[NUM_CHAN] =
128 16384, 16384, 16384, 16384, 16384, 16384, 10923, 10923,
129 10923, 8192, 8192, 6554, 5461, 4681, 4681, 4096
133 * The voice metric table is defined below. It is a non-
134 * linear table with a deadband near zero. It maps the SNR
135 * index (quantized SNR value) to a number that is a measure
139 const Word16 vm_tbl[90] =
141 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
142 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7,
143 8, 8, 9, 9, 10, 10, 11, 12, 12, 13, 13, 14, 15,
144 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 23, 24,
145 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 33, 34,
146 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45,
147 46, 47, 48, 49, 50, 50, 50, 50, 50, 50, 50, 50,
151 /* hangover as a function of peak SNR (3 dB steps) */
152 const Word16 hangover_table[20] =
154 30, 30, 30, 30, 30, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 8, 8, 8
157 /* burst sensitivity as a function of peak SNR (3 dB steps) */
158 const Word16 burstcount_table[20] =
160 8, 8, 8, 8, 8, 8, 8, 8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4
163 /* voice metric sensitivity as a function of peak SNR (3 dB steps) */
164 const Word16 vm_threshold_table[20] =
166 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
167 34, 40, 51, 71, 100, 139, 191, 257, 337, 432
171 ------------------------------------------------------------------------------
172 FUNCTION NAME: fn10Log10
173 ------------------------------------------------------------------------------
174 INPUT AND OUTPUT DEFINITIONS
177 L_Input -- Word32 -- (scaled as 31-fbits,fbits)
178 fbits -- Word16 -- number of fractional bits on input
181 pOverflow -- pointer to type Flag -- overflow indicator
184 output -- Word16 -- (scaled as 7,8)
186 Global Variables Used:
189 Local Variables Needed:
192 ------------------------------------------------------------------------------
196 The purpose of this function is to take the 10*log base 10 of input and
197 divide by 128 and return; i.e. output = 10*log10(input)/128 (scaled as 7,8)
201 10*log10(x)/128 = 10*(log10(2) * (log2(x<<fbits)-log2(1<<fbits)) >> 7
202 = 3.0103 * (log2(x<<fbits) - fbits) >> 7
203 = ((3.0103/4.0 * (log2(x<<fbits) - fbits) << 2) >> 7
204 = (3.0103/4.0 * (log2(x<<fbits) - fbits) >> 5
206 ------------------------------------------------------------------------------
211 ------------------------------------------------------------------------------
214 vad2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
216 ------------------------------------------------------------------------------
220 ------------------------------------------------------------------------------
222 [State any special notes, constraints or cautions for users of this function]
224 ------------------------------------------------------------------------------
227 Word16 fn10Log10(Word32 L_Input, Word16 fbits, Flag *pOverflow)
230 Word16 integer; /* Integer part of Log2. (range: 0<=val<=30) */
231 Word16 fraction; /* Fractional part of Log2. (range: 0<=val<1) */
236 Log2(L_Input, &integer, &fraction, pOverflow);
238 integer = sub(integer, fbits, pOverflow);
240 /* 24660 = 10*log10(2)/4 scaled 0,15 */
241 Ltmp = Mpy_32_16(integer, fraction, 24660, pOverflow);
243 /* extra shift for 30,1 => 15,0 extract correction */
244 Ltmp = L_shr_r(Ltmp, 5 + 1, pOverflow);
252 ------------------------------------------------------------------------------
253 FUNCTION NAME: block_norm
254 ------------------------------------------------------------------------------
255 INPUT AND OUTPUT DEFINITIONS
258 in -- array of type Word16 -- pointer to data sequence to be normalised
259 length -- Word16 -- number of elements in data sequence
260 headroom -- Word16 -- number of headroom bits
263 out -- array of type Word16 -- normalised output data sequence
264 pOverflow -- pointer to type Flag -- overflow indicator
267 Word16 -- number of bits sequence was left shifted
269 Global Variables Used:
272 Local Variables Needed:
275 ------------------------------------------------------------------------------
278 The purpose of this function is block normalise the input data sequence
280 1) Search for maximum absolute valued data element
281 2) Normalise the max element with "headroom"
282 3) Transfer/shift the input sequence to the output buffer
283 4) Return the number of left shifts
285 ------------------------------------------------------------------------------
290 ------------------------------------------------------------------------------
293 vad2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
295 ------------------------------------------------------------------------------
299 ------------------------------------------------------------------------------
302 An input sequence of all zeros will return the maximum
303 number of left shifts allowed, NOT the value returned
304 by a norm_s(0) call, since it desired to associate an
305 all zeros sequence with low energy.
306 ------------------------------------------------------------------------------
324 for (i = 1; i < length; i++)
326 adata = abs_s(in[i]);
335 scnt = sub(norm_s(max), headroom, pOverflow);
336 for (i = 0; i < length; i++)
338 out[i] = shl(in[i], scnt, pOverflow);
343 scnt = sub(16, headroom, pOverflow);
344 for (i = 0; i < length; i++)
356 ------------------------------------------------------------------------------
358 ------------------------------------------------------------------------------
359 INPUT AND OUTPUT DEFINITIONS
362 farray_ptr -- array of type Word16, length 80 (input array)
363 vadState2 -- pointer to vadState2 state structure
366 vadState2 -- pointer to vadState2 state structure --
367 state variables are updated
368 pOverflow -- pointer to type Flag -- overflow indicator
372 VAD(m) - two successive calls to vad2() yield
373 the VAD decision for the 20 ms frame:
374 VAD_flag = VAD(m-1) || VAD(m)
376 Global Variables Used:
379 Local Variables Needed:
382 ------------------------------------------------------------------------------
385 This function provides the Voice Activity Detection function option 2
386 for the Adaptive Multi-rate (AMR) codec.
388 ------------------------------------------------------------------------------
393 ------------------------------------------------------------------------------
396 vad2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
398 ------------------------------------------------------------------------------
402 ------------------------------------------------------------------------------
404 [State any special notes, constraints or cautions for users of this function]
406 ------------------------------------------------------------------------------
409 Word16 vad2(Word16 * farray_ptr, vadState2 * st, Flag *pOverflow)
412 /* State tables that use 22,9 or 27,4 scaling for ch_enrg[] */
414 const Word16 noise_floor_chan[2] = {NOISE_FLOOR_CHAN_0, NOISE_FLOOR_CHAN_1};
415 const Word16 min_chan_enrg[2] = {MIN_CHAN_ENRG_0, MIN_CHAN_ENRG_1};
416 const Word16 ine_noise[2] = {INE_NOISE_0, INE_NOISE_1};
417 const Word16 fbits[2] = {FRACTIONAL_BITS_0, FRACTIONAL_BITS_1};
418 const Word16 state_change_shift_r[2] = {STATE_1_TO_0_SHIFT_R, STATE_0_TO_1_SHIFT_R};
420 /* Energy scale table given 30,1 input scaling (also account for -6 dB shift on input) */
421 const Word16 enrg_norm_shift[2] = {(FRACTIONAL_BITS_0 - 1 + 2), (FRACTIONAL_BITS_1 - 1 + 2)};
424 /* Automatic variables */
426 Word32 Lenrg; /* scaled as 30,1 */
427 Word32 Ltne; /* scaled as 22,9 */
428 Word32 Ltce; /* scaled as 22,9 or 27,4 */
430 Word16 tne_db; /* scaled as 7,8 */
431 Word16 tce_db; /* scaled as 7,8 */
433 Word16 input_buffer[FRM_LEN]; /* used for block normalising input data */
434 Word16 data_buffer[FFT_LEN]; /* used for in-place FFT */
436 Word16 ch_snr[NUM_CHAN]; /* scaled as 7,8 */
437 Word16 ch_snrq; /* scaled as 15,0 (in 0.375 dB steps) */
438 Word16 vm_sum; /* scaled as 15,0 */
439 Word16 ch_enrg_dev; /* scaled as 7,8 */
441 Word32 Lpeak; /* maximum channel energy */
442 Word16 p2a_flag; /* flag to indicate spectral peak-to-average ratio > 10 dB */
444 Word16 ch_enrg_db[NUM_CHAN]; /* scaled as 7,8 */
445 Word16 ch_noise_db; /* scaled as 7,8 */
447 Word16 alpha; /* scaled as 0,15 */
448 Word16 one_m_alpha; /* scaled as 0,15 */
449 Word16 update_flag; /* set to indicate a background noise estimate update */
454 Word16 j2; /* Scratch variables */
464 Word16 normb_shift; /* block norm shift count */
466 Word16 ivad; /* intermediate VAD decision (return value) */
467 Word16 tsnrq; /* total signal-to-noise ratio (quantized 3 dB steps) scaled as 15,0 */
468 Word16 xt; /* instantaneous frame SNR in dB, scaled as 7,8 */
473 /* Increment frame counter */
474 st->Lframe_cnt = L_add(st->Lframe_cnt, 1, pOverflow);
476 /* Block normalize the input */
477 normb_shift = block_norm(farray_ptr, input_buffer, FRM_LEN, FFT_HEADROOM, pOverflow);
479 /* Pre-emphasize the input data and store in the data buffer with the appropriate offset */
480 for (i = 0; i < DELAY; i++)
485 st->pre_emp_mem = shr_r(st->pre_emp_mem, sub(st->last_normb_shift, normb_shift, pOverflow), pOverflow);
486 st->last_normb_shift = normb_shift;
488 data_buffer[DELAY] = add(input_buffer[0], mult(PRE_EMP_FAC, st->pre_emp_mem, pOverflow), pOverflow);
490 for (i = DELAY + 1, j = 1; i < DELAY + FRM_LEN; i++, j++)
492 data_buffer[i] = add(input_buffer[j], mult(PRE_EMP_FAC, input_buffer[j-1], pOverflow), pOverflow);
494 st->pre_emp_mem = input_buffer[FRM_LEN-1];
496 for (i = DELAY + FRM_LEN; i < FFT_LEN; i++)
502 /* Perform FFT on the data buffer */
503 r_fft(data_buffer, pOverflow);
506 /* Use normb_shift factor to determine the scaling of the energy estimates */
508 if (st->shift_state == 0)
510 if (normb_shift <= (-FFT_HEADROOM + 2))
518 if (normb_shift >= (-FFT_HEADROOM + 5))
525 /* Scale channel energy estimate */
528 for (i = LO_CHAN; i <= HI_CHAN; i++)
533 state_change_shift_r[st->shift_state],
539 /* Estimate the energy in each channel */
540 if (st->Lframe_cnt == 1)
548 one_m_alpha = ONE_MINUS_CEE_SM_FAC;
551 for (i = LO_CHAN; i <= HI_CHAN; i++)
557 for (j = j1; j <= j2; j++)
567 data_buffer[2 * j + 1],
568 data_buffer[2 * j + 1],
572 /* Denorm energy & scale 30,1 according to the state */
573 tmp = shl(normb_shift, 1, pOverflow);
574 tmp = sub(tmp, enrg_norm_shift[st->shift_state], pOverflow);
575 Lenrg = L_shr_r(Lenrg, tmp, pOverflow);
577 /* integrate over time:
578 * e[i] = (1-alpha)*e[i] + alpha*enrg/num_bins_in_chan
580 tmp = mult(alpha, ch_tbl_sh[i], pOverflow);
581 L_Extract(Lenrg, &hi1, &lo1, pOverflow);
582 Ltmp = Mpy_32_16(hi1, lo1, tmp, pOverflow);
584 L_Extract(st->Lch_enrg[i], &hi1, &lo1, pOverflow);
586 Ltmp1 = Mpy_32_16(hi1, lo1, one_m_alpha, pOverflow);
587 st->Lch_enrg[i] = L_add(Ltmp, Ltmp1, pOverflow);
589 if (st->Lch_enrg[i] < min_chan_enrg[st->shift_state])
591 st->Lch_enrg[i] = min_chan_enrg[st->shift_state];
597 /* Compute the total channel energy estimate (Ltce) */
599 for (i = LO_CHAN; i <= HI_CHAN; i++)
609 /* Calculate spectral peak-to-average ratio, set flag if p2a > 10 dB */
612 /* Sine waves not valid for low frequencies */
613 for (i = LO_CHAN + 2; i <= HI_CHAN; i++)
615 if (L_sub(st->Lch_enrg [i], Lpeak, pOverflow) > 0)
617 Lpeak = st->Lch_enrg [i];
621 /* Set p2a_flag if peak (dB) > average channel energy (dB) + 10 dB */
622 /* Lpeak > Ltce/num_channels * 10^(10/10) */
623 /* Lpeak > (10/16)*Ltce */
625 L_Extract(Ltce, &hi1, &lo1, pOverflow);
626 Ltmp = Mpy_32_16(hi1, lo1, 20480, pOverflow);
627 if (L_sub(Lpeak, Ltmp, pOverflow) > 0)
637 /* Initialize channel noise estimate to either the channel energy or fixed level */
638 /* Scale the energy appropriately to yield state 0 (22,9) scaling for noise */
639 if (st->Lframe_cnt <= 4)
641 if (p2a_flag == TRUE)
643 for (i = LO_CHAN; i <= HI_CHAN; i++)
645 st->Lch_noise[i] = INE_NOISE_0;
650 for (i = LO_CHAN; i <= HI_CHAN; i++)
652 if (st->Lch_enrg[i] < ine_noise[st->shift_state])
654 st->Lch_noise[i] = INE_NOISE_0;
658 if (st->shift_state == 1)
663 state_change_shift_r[0],
668 st->Lch_noise[i] = st->Lch_enrg[i];
676 /* Compute the channel energy (in dB), the channel SNRs, and the sum of voice metrics */
678 for (i = LO_CHAN; i <= HI_CHAN; i++)
683 fbits[st->shift_state],
692 ch_snr[i] = sub(ch_enrg_db[i], ch_noise_db, pOverflow);
694 /* quantize channel SNR in 3/8 dB steps (scaled 7,8 => 15,0) */
695 /* ch_snr = pv_round((snr/(3/8))>>8) */
696 /* = pv_round(((0.6667*snr)<<2)>>8) */
697 /* = pv_round((0.6667*snr)>>6) */
699 tmp = mult(21845, ch_snr[i], pOverflow);
701 ch_snrq = shr_r(tmp, 6, pOverflow);
703 /* Accumulate the sum of voice metrics */
719 vm_sum = add(vm_sum, vm_tbl[j], pOverflow);
723 /* Initialize NOMINAL peak voice energy and average noise energy, calculate instantaneous SNR */
724 if (st->Lframe_cnt <= 4 || st->fupdate_flag == TRUE)
726 /* tce_db = (96 - 22 - 10*log10(64) (due to FFT)) scaled as 7,8 */
731 /* Compute the total noise estimate (Ltne) */
733 for (i = LO_CHAN; i <= HI_CHAN; i++)
735 Ltne = L_add(Ltne, st->Lch_noise[i], pOverflow);
738 /* Get total noise in dB */
745 /* Initialise instantaneous and long-term peak signal-to-noise ratios */
746 xt = sub(tce_db, tne_db, pOverflow);
751 /* Calculate instantaneous frame signal-to-noise ratio */
752 /* xt = 10*log10( sum(2.^(ch_snr*0.1*log2(10)))/length(ch_snr) ) */
754 for (i = LO_CHAN; i <= HI_CHAN; i++)
756 /* Ltmp2 = ch_snr[i] * 0.1 * log2(10); (ch_snr scaled as 7,8) */
757 Ltmp2 = L_mult(ch_snr[i], 10885, pOverflow);
758 Ltmp2 = L_shr(Ltmp2, 8, pOverflow);
760 L_Extract(Ltmp2, &hi1, &lo1, pOverflow);
761 hi1 = add(hi1, 3, pOverflow); /* 2^3 to compensate for negative SNR */
763 Ltmp2 = Pow2(hi1, lo1, pOverflow);
765 Ltmp1 = L_add(Ltmp1, Ltmp2, pOverflow);
771 pOverflow); /* average by 16, inverse compensation 2^3 */
773 /* Estimate long-term "peak" SNR */
776 Ltmp1 = L_mult(29491, st->tsnr, pOverflow);
777 Ltmp2 = L_mult(3277, xt, pOverflow);
778 Ltmp1 = L_add(Ltmp1, Ltmp2, pOverflow);
780 /* tsnr = 0.9*tsnr + 0.1*xt; */
781 st->tsnr = pv_round(Ltmp1, pOverflow);
783 /* else if (xt > 0.625*tsnr) */
786 tmp = mult(20480, st->tsnr, pOverflow);
787 tmp = sub(xt, tmp, pOverflow);
791 /* tsnr = 0.998*tsnr + 0.002*xt; */
792 Ltmp1 = L_mult(32702, st->tsnr, pOverflow);
793 Ltmp2 = L_mult(66, xt, pOverflow);
794 Ltmp1 = L_add(Ltmp1, Ltmp2, pOverflow);
796 st->tsnr = pv_round(Ltmp1, pOverflow);
801 /* Quantize the long-term SNR in 3 dB steps, limit to 0 <= tsnrq <= 19 */
802 tmp = mult(st->tsnr, 10923, pOverflow);
803 tsnrq = shr(tmp, 8, pOverflow);
805 /* tsnrq = min(19, max(0, tsnrq)); */
815 /* Calculate the negative SNR sensitivity bias */
818 /* negSNRvar = 0.99*negSNRvar + 0.01*xt*xt; */
819 /* xt scaled as 7,8 => xt*xt scaled as 14,17, shift to 7,8 and round */
820 Ltmp1 = L_mult(xt, xt, pOverflow);
821 Ltmp1 = L_shl(Ltmp1, 7, pOverflow);
822 tmp = pv_round(Ltmp1, pOverflow);
824 Ltmp1 = L_mult(32440, st->negSNRvar, pOverflow);
825 Ltmp2 = L_mult(328, tmp, pOverflow);
826 Ltmp1 = L_add(Ltmp1, Ltmp2, pOverflow);
828 st->negSNRvar = pv_round(Ltmp1, pOverflow);
830 /* if (negSNRvar > 4.0) negSNRvar = 4.0; */
831 if (st->negSNRvar > 1024)
833 st->negSNRvar = 1024;
836 /* negSNRbias = max(12.0*(negSNRvar - 0.65), 0.0); */
837 tmp = sub(st->negSNRvar, 166, pOverflow);
838 tmp = shl(tmp, 4, pOverflow);
839 tmp = mult_r(tmp, 24576, pOverflow);
847 st->negSNRbias = shr(tmp, 8, pOverflow);
852 /* Determine VAD as a function of the voice metric sum and quantized SNR */
854 tmp = add(vm_threshold_table[tsnrq], st->negSNRbias, pOverflow);
859 st->burstcount = add(st->burstcount, 1, pOverflow);
860 if (st->burstcount > burstcount_table[tsnrq])
862 st->hangover = hangover_table[tsnrq];
868 st->hangover = sub(st->hangover, 1, pOverflow);
869 if (st->hangover <= 0)
881 /* Calculate log spectral deviation */
883 if (st->Lframe_cnt == 1)
885 for (i = LO_CHAN; i <= HI_CHAN; i++)
887 st->ch_enrg_long_db[i] = ch_enrg_db[i];
892 for (i = LO_CHAN; i <= HI_CHAN; i++)
894 tmp = sub(st->ch_enrg_long_db[i], ch_enrg_db[i], pOverflow);
897 ch_enrg_dev = add(ch_enrg_dev, tmp, pOverflow);
902 * Calculate long term integration constant as
903 * a function of instantaneous SNR
904 * (i.e., high SNR (tsnr dB) -> slower integration (alpha = HIGH_ALPHA),
905 * low SNR (0 dB) -> faster integration (alpha = LOW_ALPHA)
908 /* alpha = HIGH_ALPHA - ALPHA_RANGE * (tsnr - xt)
909 * ----------------------------------------------
910 * tsnr, low <= alpha <= high
912 tmp = sub(st->tsnr, xt, pOverflow);
913 if (tmp <= 0 || st->tsnr <= 0)
916 one_m_alpha = 32768L - HIGH_ALPHA;
918 else if (tmp > st->tsnr)
921 one_m_alpha = 32768L - LOW_ALPHA;
925 tmp = div_s(tmp, st->tsnr);
926 tmp = mult(ALPHA_RANGE, tmp, pOverflow);
927 alpha = sub(HIGH_ALPHA, tmp, pOverflow);
928 one_m_alpha = sub(32767, alpha, pOverflow);
931 /* Calc long term log spectral energy */
932 for (i = LO_CHAN; i <= HI_CHAN; i++)
934 Ltmp1 = L_mult(one_m_alpha, ch_enrg_db[i], pOverflow);
935 Ltmp2 = L_mult(alpha, st->ch_enrg_long_db[i], pOverflow);
937 Ltmp1 = L_add(Ltmp1, Ltmp2, pOverflow);
938 st->ch_enrg_long_db[i] = pv_round(Ltmp1, pOverflow);
942 /* Set or clear the noise update flags */
944 st->fupdate_flag = FALSE;
945 if (vm_sum <= UPDATE_THLD)
947 if (st->burstcount == 0)
953 else if (L_sub(Ltce, noise_floor_chan[st->shift_state], pOverflow) > 0)
955 if (ch_enrg_dev < DEV_THLD)
957 if (p2a_flag == FALSE)
959 if (st->LTP_flag == FALSE)
961 st->update_cnt = add(st->update_cnt, 1, pOverflow);
962 if (st->update_cnt >= UPDATE_CNT_THLD)
965 st->fupdate_flag = TRUE;
971 if (st->update_cnt == st->last_update_cnt)
973 st->hyster_cnt = add(st->hyster_cnt, 1, pOverflow);
980 st->last_update_cnt = st->update_cnt;
982 if (st->hyster_cnt > HYSTER_CNT_THLD)
988 /* Conditionally update the channel noise estimates */
989 if (update_flag == TRUE)
991 /* Check shift state */
992 if (st->shift_state == 1)
994 /* get factor to shift ch_enrg[]
995 * from state 1 to 0 (noise always state 0)
997 tmp = state_change_shift_r[0];
1001 /* No shift if already state 0 */
1005 /* Update noise energy estimate */
1006 for (i = LO_CHAN; i <= HI_CHAN; i++)
1008 /* integrate over time: en[i] = (1-alpha)*en[i] + alpha*e[n] */
1009 /* (extract with shift compensation for state 1) */
1011 Ltmp1 = L_shr(st->Lch_enrg[i], tmp, pOverflow);
1012 L_Extract(Ltmp1, &hi1, &lo1, pOverflow);
1014 Ltmp = Mpy_32_16(hi1, lo1, CNE_SM_FAC, pOverflow);
1016 L_Extract(st->Lch_noise[i], &hi1, &lo1, pOverflow);
1018 Ltmp1 = Mpy_32_16(hi1, lo1, ONE_MINUS_CNE_SM_FAC, pOverflow);
1019 st->Lch_noise[i] = L_add(Ltmp, Ltmp1, pOverflow);
1021 /* Limit low level noise */
1022 if (st->Lch_noise[i] <= MIN_NOISE_ENRG_0)
1024 st->Lch_noise[i] = MIN_NOISE_ENRG_0;
1030 } /* end of vad2 () */
1034 ------------------------------------------------------------------------------
1035 FUNCTION NAME: vad2_init
1036 ------------------------------------------------------------------------------
1037 INPUT AND OUTPUT DEFINITIONS
1040 state -- double pointer to type vadState2 -- pointer to memory to
1044 state -- points to initalized area in memory.
1049 Global Variables Used:
1052 Local Variables Needed:
1055 ------------------------------------------------------------------------------
1056 FUNCTION DESCRIPTION
1058 Allocates state memory and initializes state memory
1060 ------------------------------------------------------------------------------
1065 ------------------------------------------------------------------------------
1068 vad2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1070 ------------------------------------------------------------------------------
1074 ------------------------------------------------------------------------------
1076 [State any special notes, constraints or cautions for users of this function]
1078 ------------------------------------------------------------------------------
1080 Word16 vad2_init(vadState2 **state)
1084 if (state == (vadState2 **) NULL)
1090 /* allocate memory */
1091 if ((s = (vadState2 *) oscl_malloc(sizeof(vadState2))) == NULL)
1105 ------------------------------------------------------------------------------
1106 FUNCTION NAME: vad2_reset
1107 ------------------------------------------------------------------------------
1108 INPUT AND OUTPUT DEFINITIONS
1111 state -- pointer to type vadState1 -- State struct
1114 state -- pointer to type vadState1 -- State struct
1119 Global Variables Used:
1122 Local Variables Needed:
1125 ------------------------------------------------------------------------------
1126 FUNCTION DESCRIPTION
1128 Purpose: Resets state memory to zero
1130 ------------------------------------------------------------------------------
1135 ------------------------------------------------------------------------------
1138 vad2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1140 ------------------------------------------------------------------------------
1144 ------------------------------------------------------------------------------
1146 [State any special notes, constraints or cautions for users of this function]
1148 ------------------------------------------------------------------------------
1151 Word16 vad2_reset(vadState2 * st)
1156 if (st == (vadState2 *) NULL)
1162 for (i = 0; i < sizeof(vadState2) / 2; i++)
1168 } /* end of vad2_reset () */
1172 ------------------------------------------------------------------------------
1173 FUNCTION NAME: vad2_exit
1174 ------------------------------------------------------------------------------
1175 INPUT AND OUTPUT DEFINITIONS
1178 state -- pointer to type vadState1 -- State struct
1186 Global Variables Used:
1189 Local Variables Needed:
1192 ------------------------------------------------------------------------------
1193 FUNCTION DESCRIPTION
1195 The memory used for state memory is freed
1197 ------------------------------------------------------------------------------
1202 ------------------------------------------------------------------------------
1205 vad2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
1207 ------------------------------------------------------------------------------
1211 ------------------------------------------------------------------------------
1213 [State any special notes, constraints or cautions for users of this function]
1215 ------------------------------------------------------------------------------
1218 void vad2_exit(vadState2 **state)
1220 if (state == NULL || *state == NULL)
1225 /* deallocate memory */