Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / enc_lag3.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: enc_lag3.cpp
35
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include "enc_lag3.h"
43 #include "typedef.h"
44 #include "basic_op.h"
45 #include "cnst.h"
46
47 /*----------------------------------------------------------------------------
48 ; MACROS
49 ; Define module specific macros here
50 ----------------------------------------------------------------------------*/
51
52 /*----------------------------------------------------------------------------
53 ; DEFINES
54 ; Include all pre-processor statements here. Include conditional
55 ; compile variables also.
56 ----------------------------------------------------------------------------*/
57
58
59 /*----------------------------------------------------------------------------
60 ; LOCAL FUNCTION DEFINITIONS
61 ; Function Prototype declaration
62 ----------------------------------------------------------------------------*/
63
64
65 /*----------------------------------------------------------------------------
66 ; LOCAL VARIABLE DEFINITIONS
67 ; Variable declaration - defined here and used outside this module
68 ----------------------------------------------------------------------------*/
69
70 /*
71 ------------------------------------------------------------------------------
72  FUNCTION NAME: enc_lag3
73 ------------------------------------------------------------------------------
74  INPUT AND OUTPUT DEFINITIONS
75
76  Inputs:
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
85
86  Outputs:
87   pOverflow = 1 if there is an overflow else it is zero.
88
89  Returns:
90   None
91
92  Global Variables Used:
93   None
94
95  Local Variables Needed:
96   None
97
98 ------------------------------------------------------------------------------
99  FUNCTION DESCRIPTION
100
101  This function implements the encoding of fractional pitch lag with
102  1/3 resolution.
103
104  *   FUNCTION:  Enc_lag3
105  *
106  *   PURPOSE:  Encoding of fractional pitch lag with 1/3 resolution.
107  *
108  *   DESCRIPTION:
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
114  *
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]
121  *
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
129  *
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]
133
134 ------------------------------------------------------------------------------
135  REQUIREMENTS
136
137  None
138
139 ------------------------------------------------------------------------------
140  REFERENCES
141
142  enc_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
143
144 ------------------------------------------------------------------------------
145  PSEUDO-CODE
146
147    Word16 index, i, tmp_ind, uplag;
148    Word16 tmp_lag;
149
150    if (delta_flag == 0)
151    {  // if 1st or 3rd subframe
152
153       // encode pitch delay (with fraction)
154
155       if (sub (T0, 85) <= 0)
156       {
157          // index = T0*3 - 58 + T0_frac
158          i = add (add (T0, T0), T0);
159          index = add (sub (i, 58), T0_frac);
160       }
161       else
162       {
163          index = add (T0, 112);
164       }
165    }
166    else
167    {   // if second or fourth subframe
168       if (flag4 == 0) {
169
170          // 'normal' encoding: either with 5 or 6 bit resolution
171
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);
176       }
177       else {
178
179          // encoding with 4 bit resolution
180
181          tmp_lag = T0_prev;
182
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);
187
188          uplag = add (add (add (T0, T0), T0), T0_frac);
189
190          i = sub (tmp_lag, 2);
191          tmp_ind = add (add (i, i), i);
192
193          if (sub (tmp_ind, uplag) >= 0) {
194             index = add (sub (T0, tmp_lag), 5);
195          }
196          else {
197
198             i = add (tmp_lag, 1);
199             i = add (add (i, i), i);
200
201             if (sub (i, uplag) > 0) {
202
203                 index = add ( sub (uplag, tmp_ind), 3);
204             }
205             else {
206
207                index = add (sub (T0, tmp_lag), 11);
208             }
209          }
210
211       } // end if (encoding with 4 bit resolution)
212    }   // end if (second of fourth subframe)
213
214    return index;
215 }
216
217 ------------------------------------------------------------------------------
218  CAUTION [optional]
219  [State any special notes, constraints or cautions for users of this function]
220
221 ------------------------------------------------------------------------------
222 */
223
224
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        */
233     Flag   *pOverflow
234 )
235 {
236     Word16 index, i, tmp_ind, uplag;
237     Word16 tmp_lag;
238     Word16 temp1;
239     Word16 temp2;
240
241
242
243     if (delta_flag == 0)
244     {  /* if 1st or 3rd subframe */
245
246         /* encode pitch delay (with fraction) */
247         temp1 = T0 - 85;
248         if (temp1 <= 0)
249         {
250             /* index = T0*3 - 58 + T0_frac   */
251             index = (T0 << 1) + T0 -  58 + T0_frac;
252         }
253         else
254         {
255             index = T0 + 112;
256         }
257     }
258     else
259     {   /* if second or fourth subframe */
260         if (flag4 == 0)
261         {
262
263             /* 'normal' encoding: either with 5 or 6 bit resolution */
264
265             /* index = 3*(T0 - T0_min) + 2 + T0_frac */
266             i = T0 - T0_min;
267             index = i + (i << 1) + 2 + T0_frac;
268         }
269         else
270         {
271
272             /* encoding with 4 bit resolution */
273
274             tmp_lag = T0_prev;
275             temp1 = tmp_lag - T0_min;
276             temp2 = temp1 - 5;
277             if (temp2 > 0)
278             {
279                 tmp_lag = T0_min + 5;
280             }
281             temp1 = T0_max - tmp_lag;
282             temp2 = temp1 - 4;
283             if (temp2 > 0)
284             {
285                 tmp_lag = T0_max - 4;
286             }
287             uplag  = T0 + (T0 << 1);
288             uplag += T0_frac;
289             i = tmp_lag - 2;
290
291             tmp_ind  = i + (i << 1);
292             temp1    = tmp_ind - uplag;
293
294             if (temp1 >= 0)
295             {
296                 index = T0 - tmp_lag + 5;
297             }
298             else
299             {
300                 i = tmp_lag + 1;
301
302                 i += i << 1;
303
304                 if (i > uplag)
305                 {
306                     index = uplag - tmp_ind + 3;
307                 }
308                 else
309                 {
310                     index = T0 - tmp_lag + 11;
311                 }
312             }
313
314         } /* end if (encoding with 4 bit resolution) */
315     }   /* end if (second of fourth subframe) */
316
317     return index;
318 }
319
320
321