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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: enc_lag3.cpp
36 ------------------------------------------------------------------------------
39 /*----------------------------------------------------------------------------
41 ----------------------------------------------------------------------------*/
47 /*----------------------------------------------------------------------------
49 ; Define module specific macros here
50 ----------------------------------------------------------------------------*/
52 /*----------------------------------------------------------------------------
54 ; Include all pre-processor statements here. Include conditional
55 ; compile variables also.
56 ----------------------------------------------------------------------------*/
59 /*----------------------------------------------------------------------------
60 ; LOCAL FUNCTION DEFINITIONS
61 ; Function Prototype declaration
62 ----------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; Variable declaration - defined here and used outside this module
68 ----------------------------------------------------------------------------*/
71 ------------------------------------------------------------------------------
72 FUNCTION NAME: enc_lag3
73 ------------------------------------------------------------------------------
74 INPUT AND OUTPUT DEFINITIONS
77 T0 = Pitch delay of type Word16
78 T0_frac = Fractional pitch delay of type Word16
79 T0_prev = Integer pitch delay of last subframe of type Word16
80 T0_min = minimum of search range of type Word16
81 T0_max = maximum of search range of type Word16
82 delta_flag = Flag for 1st (or 3rd) subframe of type Word16
83 flag4 = Flag for encoding with 4 bits of type Word16
84 pOverflow = pointer indicating overflow of type Flag
87 pOverflow = 1 if there is an overflow else it is zero.
92 Global Variables Used:
95 Local Variables Needed:
98 ------------------------------------------------------------------------------
101 This function implements the encoding of fractional pitch lag with
106 * PURPOSE: Encoding of fractional pitch lag with 1/3 resolution.
109 * First and third subframes:
110 * --------------------------
111 * The pitch range is divided as follows:
112 * 19 1/3 to 84 2/3 resolution 1/3
113 * 85 to 143 resolution 1
115 * The period is encoded with 8 bits.
116 * For the range with fractions:
117 * index = (T-19)*3 + frac - 1;
118 * where T=[19..85] and frac=[-1,0,1]
119 * and for the integer only range
120 * index = (T - 85) + 197; where T=[86..143]
122 * Second and fourth subframes:
123 * ----------------------------
124 * For the 2nd and 4th subframes a resolution of 1/3 is always used,
125 * and the search range is relative to the lag in previous subframe.
126 * If t0 is the lag in the previous subframe then
127 * t_min=t0-5 and t_max=t0+4 and the range is given by
128 * t_min - 2/3 to t_max + 2/3
130 * The period in the 2nd (and 4th) subframe is encoded with 5 bits:
131 * index = (T-(t_min-1))*3 + frac - 1;
132 * where T=[t_min-1..t_max+1]
134 ------------------------------------------------------------------------------
139 ------------------------------------------------------------------------------
142 enc_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
144 ------------------------------------------------------------------------------
147 Word16 index, i, tmp_ind, uplag;
151 { // if 1st or 3rd subframe
153 // encode pitch delay (with fraction)
155 if (sub (T0, 85) <= 0)
157 // index = T0*3 - 58 + T0_frac
158 i = add (add (T0, T0), T0);
159 index = add (sub (i, 58), T0_frac);
163 index = add (T0, 112);
167 { // if second or fourth subframe
170 // 'normal' encoding: either with 5 or 6 bit resolution
172 // index = 3*(T0 - T0_min) + 2 + T0_frac
173 i = sub (T0, T0_min);
174 i = add (add (i, i), i);
175 index = add (add (i, 2), T0_frac);
179 // encoding with 4 bit resolution
183 if ( sub( sub(tmp_lag, T0_min), 5) > 0)
184 tmp_lag = add (T0_min, 5);
185 if ( sub( sub(T0_max, tmp_lag), 4) > 0)
186 tmp_lag = sub (T0_max, 4);
188 uplag = add (add (add (T0, T0), T0), T0_frac);
190 i = sub (tmp_lag, 2);
191 tmp_ind = add (add (i, i), i);
193 if (sub (tmp_ind, uplag) >= 0) {
194 index = add (sub (T0, tmp_lag), 5);
198 i = add (tmp_lag, 1);
199 i = add (add (i, i), i);
201 if (sub (i, uplag) > 0) {
203 index = add ( sub (uplag, tmp_ind), 3);
207 index = add (sub (T0, tmp_lag), 11);
211 } // end if (encoding with 4 bit resolution)
212 } // end if (second of fourth subframe)
217 ------------------------------------------------------------------------------
219 [State any special notes, constraints or cautions for users of this function]
221 ------------------------------------------------------------------------------
225 Word16 Enc_lag3( /* o : Return index of encoding */
226 Word16 T0, /* i : Pitch delay */
227 Word16 T0_frac, /* i : Fractional pitch delay */
228 Word16 T0_prev, /* i : Integer pitch delay of last subframe */
229 Word16 T0_min, /* i : minimum of search range */
230 Word16 T0_max, /* i : maximum of search range */
231 Word16 delta_flag, /* i : Flag for 1st (or 3rd) subframe */
232 Word16 flag4, /* i : Flag for encoding with 4 bits */
236 Word16 index, i, tmp_ind, uplag;
244 { /* if 1st or 3rd subframe */
246 /* encode pitch delay (with fraction) */
250 /* index = T0*3 - 58 + T0_frac */
251 index = (T0 << 1) + T0 - 58 + T0_frac;
259 { /* if second or fourth subframe */
263 /* 'normal' encoding: either with 5 or 6 bit resolution */
265 /* index = 3*(T0 - T0_min) + 2 + T0_frac */
267 index = i + (i << 1) + 2 + T0_frac;
272 /* encoding with 4 bit resolution */
275 temp1 = tmp_lag - T0_min;
279 tmp_lag = T0_min + 5;
281 temp1 = T0_max - tmp_lag;
285 tmp_lag = T0_max - 4;
287 uplag = T0 + (T0 << 1);
291 tmp_ind = i + (i << 1);
292 temp1 = tmp_ind - uplag;
296 index = T0 - tmp_lag + 5;
306 index = uplag - tmp_ind + 3;
310 index = T0 - tmp_lag + 11;
314 } /* end if (encoding with 4 bit resolution) */
315 } /* end if (second of fourth subframe) */