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 Pathname: ./audio/gsm-amr/c/src/enc_lag3.c
39 ------------------------------------------------------------------------------
42 Description: Replaced "int" and/or "char" with OSCL defined types.
46 ------------------------------------------------------------------------------
49 /*----------------------------------------------------------------------------
51 ----------------------------------------------------------------------------*/
57 /*----------------------------------------------------------------------------
59 ; Define module specific macros here
60 ----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
64 ; Include all pre-processor statements here. Include conditional
65 ; compile variables also.
66 ----------------------------------------------------------------------------*/
69 /*----------------------------------------------------------------------------
70 ; LOCAL FUNCTION DEFINITIONS
71 ; Function Prototype declaration
72 ----------------------------------------------------------------------------*/
75 /*----------------------------------------------------------------------------
76 ; LOCAL VARIABLE DEFINITIONS
77 ; Variable declaration - defined here and used outside this module
78 ----------------------------------------------------------------------------*/
81 ------------------------------------------------------------------------------
82 FUNCTION NAME: enc_lag3
83 ------------------------------------------------------------------------------
84 INPUT AND OUTPUT DEFINITIONS
87 T0 = Pitch delay of type Word16
88 T0_frac = Fractional pitch delay of type Word16
89 T0_prev = Integer pitch delay of last subframe of type Word16
90 T0_min = minimum of search range of type Word16
91 T0_max = maximum of search range of type Word16
92 delta_flag = Flag for 1st (or 3rd) subframe of type Word16
93 flag4 = Flag for encoding with 4 bits of type Word16
94 pOverflow = pointer indicating overflow of type Flag
97 pOverflow = 1 if there is an overflow else it is zero.
102 Global Variables Used:
105 Local Variables Needed:
108 ------------------------------------------------------------------------------
111 This function implements the encoding of fractional pitch lag with
116 * PURPOSE: Encoding of fractional pitch lag with 1/3 resolution.
119 * First and third subframes:
120 * --------------------------
121 * The pitch range is divided as follows:
122 * 19 1/3 to 84 2/3 resolution 1/3
123 * 85 to 143 resolution 1
125 * The period is encoded with 8 bits.
126 * For the range with fractions:
127 * index = (T-19)*3 + frac - 1;
128 * where T=[19..85] and frac=[-1,0,1]
129 * and for the integer only range
130 * index = (T - 85) + 197; where T=[86..143]
132 * Second and fourth subframes:
133 * ----------------------------
134 * For the 2nd and 4th subframes a resolution of 1/3 is always used,
135 * and the search range is relative to the lag in previous subframe.
136 * If t0 is the lag in the previous subframe then
137 * t_min=t0-5 and t_max=t0+4 and the range is given by
138 * t_min - 2/3 to t_max + 2/3
140 * The period in the 2nd (and 4th) subframe is encoded with 5 bits:
141 * index = (T-(t_min-1))*3 + frac - 1;
142 * where T=[t_min-1..t_max+1]
144 ------------------------------------------------------------------------------
149 ------------------------------------------------------------------------------
152 enc_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
154 ------------------------------------------------------------------------------
157 Word16 index, i, tmp_ind, uplag;
161 { // if 1st or 3rd subframe
163 // encode pitch delay (with fraction)
165 if (sub (T0, 85) <= 0)
167 // index = T0*3 - 58 + T0_frac
168 i = add (add (T0, T0), T0);
169 index = add (sub (i, 58), T0_frac);
173 index = add (T0, 112);
177 { // if second or fourth subframe
180 // 'normal' encoding: either with 5 or 6 bit resolution
182 // index = 3*(T0 - T0_min) + 2 + T0_frac
183 i = sub (T0, T0_min);
184 i = add (add (i, i), i);
185 index = add (add (i, 2), T0_frac);
189 // encoding with 4 bit resolution
193 if ( sub( sub(tmp_lag, T0_min), 5) > 0)
194 tmp_lag = add (T0_min, 5);
195 if ( sub( sub(T0_max, tmp_lag), 4) > 0)
196 tmp_lag = sub (T0_max, 4);
198 uplag = add (add (add (T0, T0), T0), T0_frac);
200 i = sub (tmp_lag, 2);
201 tmp_ind = add (add (i, i), i);
203 if (sub (tmp_ind, uplag) >= 0) {
204 index = add (sub (T0, tmp_lag), 5);
208 i = add (tmp_lag, 1);
209 i = add (add (i, i), i);
211 if (sub (i, uplag) > 0) {
213 index = add ( sub (uplag, tmp_ind), 3);
217 index = add (sub (T0, tmp_lag), 11);
221 } // end if (encoding with 4 bit resolution)
222 } // end if (second of fourth subframe)
227 ------------------------------------------------------------------------------
228 RESOURCES USED [optional]
230 When the code is written for a specific target processor the
231 the resources used should be documented below.
233 HEAP MEMORY USED: x bytes
235 STACK MEMORY USED: x bytes
237 CLOCK CYCLES: (cycle count equation for this function) + (variable
238 used to represent cycle count for each subroutine
240 where: (cycle count variable) = cycle count for [subroutine
243 ------------------------------------------------------------------------------
245 [State any special notes, constraints or cautions for users of this function]
247 ------------------------------------------------------------------------------
251 Word16 Enc_lag3( /* o : Return index of encoding */
252 Word16 T0, /* i : Pitch delay */
253 Word16 T0_frac, /* i : Fractional pitch delay */
254 Word16 T0_prev, /* i : Integer pitch delay of last subframe */
255 Word16 T0_min, /* i : minimum of search range */
256 Word16 T0_max, /* i : maximum of search range */
257 Word16 delta_flag, /* i : Flag for 1st (or 3rd) subframe */
258 Word16 flag4, /* i : Flag for encoding with 4 bits */
262 Word16 index, i, tmp_ind, uplag;
270 { /* if 1st or 3rd subframe */
272 /* encode pitch delay (with fraction) */
273 temp1 = sub(T0, 85, pOverflow);
276 /* index = T0*3 - 58 + T0_frac */
277 temp2 = add(T0, T0, pOverflow);
278 i = add(temp2, T0, pOverflow);
279 temp2 = sub(i, 58, pOverflow);
280 index = add(temp2, T0_frac, pOverflow);
284 index = add(T0, 112, pOverflow);
288 { /* if second or fourth subframe */
292 /* 'normal' encoding: either with 5 or 6 bit resolution */
294 /* index = 3*(T0 - T0_min) + 2 + T0_frac */
295 i = sub(T0, T0_min, pOverflow);
296 temp2 = add(i, i, pOverflow);
297 i = add(temp2, i, pOverflow);
298 temp2 = add(i, 2, pOverflow);
299 index = add(temp2, T0_frac, pOverflow);
304 /* encoding with 4 bit resolution */
307 temp1 = sub(tmp_lag, T0_min, pOverflow);
308 temp2 = sub(temp1, 5, pOverflow);
310 tmp_lag = add(T0_min, 5, pOverflow);
311 temp1 = sub(T0_max, tmp_lag, pOverflow);
312 temp2 = sub(temp1, 4, pOverflow);
314 tmp_lag = sub(T0_max, 4, pOverflow);
316 temp1 = add(T0, T0, pOverflow);
317 temp2 = add(temp1, T0, pOverflow);
318 uplag = add(temp2, T0_frac, pOverflow);
320 i = sub(tmp_lag, 2, pOverflow);
321 temp1 = add(i, i, pOverflow);
322 tmp_ind = add(temp1, i, pOverflow);
324 temp1 = sub(tmp_ind, uplag, pOverflow);
327 temp1 = sub(T0, tmp_lag, pOverflow);
328 index = add(temp1, 5, pOverflow);
333 i = add(tmp_lag, 1, pOverflow);
334 temp1 = add(i, i, pOverflow);
335 i = add(temp1, i, pOverflow);
337 if (sub(i, uplag, pOverflow) > 0)
339 temp1 = sub(uplag, tmp_ind, pOverflow);
340 index = add(temp1, 3, pOverflow);
344 temp1 = sub(T0, tmp_lag, pOverflow);
345 index = add(temp1, 11, pOverflow);
349 } /* end if (encoding with 4 bit resolution) */
350 } /* end if (second of fourth subframe) */