1219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard/*
2219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** Copyright 2003-2010, VisualOn, Inc.
3219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard **
4219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** Licensed under the Apache License, Version 2.0 (the "License");
5219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** you may not use this file except in compliance with the License.
6219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** You may obtain a copy of the License at
7219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard **
8219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard **     http://www.apache.org/licenses/LICENSE-2.0
9219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard **
10219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** Unless required by applicable law or agreed to in writing, software
11219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** distributed under the License is distributed on an "AS IS" BASIS,
12219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** See the License for the specific language governing permissions and
14219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard ** limitations under the License.
15219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard */
16219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
17219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard/***********************************************************************
18219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*      File: p_med_ol.c                                                *
19219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*                                                                      *
20219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*      Description: Compute the open loop pitch lag                    *
21219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*	            output: open loop pitch lag                        *
22219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard************************************************************************/
23219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
24219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard#include "typedef.h"
25219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard#include "basic_op.h"
26219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard#include "acelp.h"
27219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard#include "oper_32b.h"
28219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard#include "math_op.h"
29219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard#include "p_med_ol.tab"
30219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
31219e2627f1e062c10645664b0d2470d4dfaf5083Mans RullgardWord16 Pitch_med_ol(
32219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		   Word16      wsp[],        /*   i: signal used to compute the open loop pitch*/
33219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard                                     /*      wsp[-pit_max] to wsp[-1] should be known */
34219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		   Coder_State *st,          /* i/o: codec global structure */
35219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		   Word16      L_frame       /*   i: length of frame to compute pitch */
36219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		)
37219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard{
38219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 Tm;
39219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 hi, lo;
40219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 *ww, *we, *hp_wsp;
41219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 exp_R0, exp_R1, exp_R2;
42219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word32 i, j, max, R0, R1, R2;
43219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 *p1, *p2;
44219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 L_min = 17;                   /* minimum pitch lag: PIT_MIN / OPL_DECIM */
45219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 L_max = 115;                  /* maximum pitch lag: PIT_MAX / OPL_DECIM */
46219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 L_0 = st->old_T0_med;         /* old open-loop pitch */
47219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 *gain = &(st->ol_gain);       /* normalize correlation of hp_wsp for the lag */
48219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 *hp_wsp_mem = st->hp_wsp_mem; /* memory of the hypass filter for hp_wsp[] (lg = 9)*/
49219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 *old_hp_wsp = st->old_hp_wsp; /* hypass wsp[] */
50219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 wght_flg = st->ol_wght_flg;   /* is weighting function used */
51219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
52219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	ww = &corrweight[198];
53219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	we = &corrweight[98 + L_max - L_0];
54219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
55219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	max = MIN_32;
56219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Tm = 0;
57219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	for (i = L_max; i > L_min; i--)
58219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
59219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		/* Compute the correlation */
60219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R0 = 0;
61219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		p1 = wsp;
62219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		p2 = &wsp[-i];
63219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		for (j = 0; j < L_frame; j+=4)
64219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		{
65219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			R0 += vo_L_mult((*p1++), (*p2++));
66219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			R0 += vo_L_mult((*p1++), (*p2++));
67219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			R0 += vo_L_mult((*p1++), (*p2++));
68219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			R0 += vo_L_mult((*p1++), (*p2++));
69219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		}
70219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		/* Weighting of the correlation function.   */
71219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		hi = R0>>16;
72219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		lo = (R0 & 0xffff)>>1;
73219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
74219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R0 = Mpy_32_16(hi, lo, *ww);
75219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		ww--;
76219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
77219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		if ((L_0 > 0) && (wght_flg > 0))
78219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		{
79219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			/* Weight the neighbourhood of the old lag. */
80219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			hi = R0>>16;
81219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			lo = (R0 & 0xffff)>>1;
82219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			R0 = Mpy_32_16(hi, lo, *we);
83219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			we--;
84219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		}
85219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		if(R0 >= max)
86219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		{
87219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			max = R0;
88219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard			Tm = i;
89219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		}
90219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
91219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
92219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	/* Hypass the wsp[] vector */
93219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	hp_wsp = old_hp_wsp + L_max;
94219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Hp_wsp(wsp, hp_wsp, L_frame, hp_wsp_mem);
95219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
96219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	/* Compute normalize correlation at delay Tm */
97219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R0 = 0;
98219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R1 = 0;
99219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R2 = 0;
100219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	p1 = hp_wsp;
101219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	p2 = hp_wsp - Tm;
102219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	for (j = 0; j < L_frame; j+=4)
103219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
104219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R2 += vo_mult32(*p1, *p1);
105219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R1 += vo_mult32(*p2, *p2);
106219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R0 += vo_mult32(*p1++, *p2++);
107219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R2 += vo_mult32(*p1, *p1);
108219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R1 += vo_mult32(*p2, *p2);
109219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R0 += vo_mult32(*p1++, *p2++);
110219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R2 += vo_mult32(*p1, *p1);
111219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R1 += vo_mult32(*p2, *p2);
112219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R0 += vo_mult32(*p1++, *p2++);
113219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R2 += vo_mult32(*p1, *p1);
114219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R1 += vo_mult32(*p2, *p2);
115219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		R0 += vo_mult32(*p1++, *p2++);
116219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
117219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R0 = R0 <<1;
118219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R1 = (R1 <<1) + 1L;
119219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R2 = (R2 <<1) + 1L;
120219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	/* gain = R0/ sqrt(R1*R2) */
121219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
122219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R0 = norm_l(R0);
123219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R0 = (R0 << exp_R0);
124219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
125219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R1 = norm_l(R1);
126219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R1 = (R1 << exp_R1);
127219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
128219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R2 = norm_l(R2);
129219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R2 = (R2 << exp_R2);
130219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
131219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
132219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R1 = vo_L_mult(vo_round(R1), vo_round(R2));
133219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
134219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	i = norm_l(R1);
135219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R1 = (R1 << i);
136219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
137219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R1 += exp_R2;
138219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R1 += i;
139219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R1 = 62 - exp_R1;
140219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
141219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Isqrt_n(&R1, &exp_R1);
142219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
143219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	R0 = vo_L_mult(voround(R0), voround(R1));
144219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R0 = 31 - exp_R0;
145219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	exp_R0 += exp_R1;
146219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
147219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	*gain = vo_round(L_shl(R0, exp_R0));
148219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
149219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	/* Shitf hp_wsp[] for next frame */
150219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
151219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	for (i = 0; i < L_max; i++)
152219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
153219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		old_hp_wsp[i] = old_hp_wsp[i + L_frame];
154219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
155219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
156219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	return (Tm);
157219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard}
158219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
159219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard/************************************************************************
160219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*  Function: median5                                                    *
161219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*                                                                       *
162219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*      Returns the median of the set {X[-2], X[-1],..., X[2]},          *
163219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*      whose elements are 16-bit integers.                              *
164219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*                                                                       *
165219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*  Input:                                                               *
166219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*      X[-2:2]   16-bit integers.                                       *
167219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*                                                                       *
168219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*  Return:                                                              *
169219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard*      The median of {X[-2], X[-1],..., X[2]}.                          *
170219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard************************************************************************/
171219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
172219e2627f1e062c10645664b0d2470d4dfaf5083Mans RullgardWord16 median5(Word16 x[])
173219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard{
174219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 x1, x2, x3, x4, x5;
175219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word16 tmp;
176219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
177219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	x1 = x[-2];
178219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	x2 = x[-1];
179219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	x3 = x[0];
180219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	x4 = x[1];
181219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	x5 = x[2];
182219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
183219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x2 < x1)
184219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
185219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		tmp = x1;
186219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x1 = x2;
187219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x2 = tmp;
188219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
189219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x3 < x1)
190219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
191219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		tmp = x1;
192219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x1 = x3;
193219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x3 = tmp;
194219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
195219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x4 < x1)
196219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
197219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		tmp = x1;
198219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x1 = x4;
199219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x4 = tmp;
200219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
201219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x5 < x1)
202219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
203219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x5 = x1;
204219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
205219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x3 < x2)
206219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
207219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		tmp = x2;
208219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x2 = x3;
209219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x3 = tmp;
210219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
211219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x4 < x2)
212219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
213219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		tmp = x2;
214219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x2 = x4;
215219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x4 = tmp;
216219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
217219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x5 < x2)
218219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
219219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x5 = x2;
220219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
221219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x4 < x3)
222219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
223219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x3 = x4;
224219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
225219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	if (x5 < x3)
226219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
227219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		x3 = x5;
228219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
229219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	return (x3);
230219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard}
231219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
232219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
233219e2627f1e062c10645664b0d2470d4dfaf5083Mans RullgardWord16 Med_olag(                           /* output : median of  5 previous open-loop lags       */
234219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		Word16 prev_ol_lag,                /* input  : previous open-loop lag                     */
235219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		Word16 old_ol_lag[5]
236219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	       )
237219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard{
238219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	Word32 i;
239219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
240219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	/* Use median of 5 previous open-loop lags as old lag */
241219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
242219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	for (i = 4; i > 0; i--)
243219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	{
244219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard		old_ol_lag[i] = old_ol_lag[i - 1];
245219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	}
246219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
247219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	old_ol_lag[0] = prev_ol_lag;
248219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
249219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	i = median5(&old_ol_lag[2]);
250219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
251219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard	return i;
252219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
253219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard}
254219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
255219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
256219e2627f1e062c10645664b0d2470d4dfaf5083Mans Rullgard
257