Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / enc_lag6.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_lag6.cpp
35
36 ------------------------------------------------------------------------------
37  MODULE DESCRIPTION
38
39
40 ------------------------------------------------------------------------------
41 */
42
43 /*----------------------------------------------------------------------------
44 ; INCLUDES
45 ----------------------------------------------------------------------------*/
46 #include "enc_lag6.h"
47 #include "typedef.h"
48 #include "basic_op.h"
49
50 /*----------------------------------------------------------------------------
51 ; MACROS
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
54
55 /*----------------------------------------------------------------------------
56 ; DEFINES
57 ; Include all pre-processor statements here. Include conditional
58 ; compile variables also.
59 ----------------------------------------------------------------------------*/
60
61 /*----------------------------------------------------------------------------
62 ; LOCAL FUNCTION DEFINITIONS
63 ; Function Prototype declaration
64 ----------------------------------------------------------------------------*/
65
66 /*----------------------------------------------------------------------------
67 ; LOCAL VARIABLE DEFINITIONS
68 ; Variable declaration - defined here and used outside this module
69 ----------------------------------------------------------------------------*/
70
71
72 /*
73 ------------------------------------------------------------------------------
74  FUNCTION NAME: Enc_lag6
75 ------------------------------------------------------------------------------
76  INPUT AND OUTPUT DEFINITIONS
77
78  Inputs:
79     T0 -- Word16 -- Pitch delay
80     T0_frac -- Word16 -- Fractional pitch delay
81     T0_min -- Word16 -- minimum of search range
82     delta_flag -- Word16 -- Flag for 1st (or 3rd) subframe
83
84  Outputs:
85     pOverflow -- Pointer to Flag -- overflow indicator
86
87  Returns:
88     Word16 -- Return index of encoding
89
90  Global Variables Used:
91     None
92
93  Local Variables Needed:
94     None
95
96 ------------------------------------------------------------------------------
97  FUNCTION DESCRIPTION
98
99  PURPOSE:  Encoding of fractional pitch lag with 1/6 resolution.
100
101  DESCRIPTION:
102                   First and third subframes:
103                   --------------------------
104  The pitch range is divided as follows:
105          17 3/6  to   94 3/6   resolution 1/6
106          95      to   143      resolution 1
107
108  The period is encoded with 9 bits.
109  For the range with fractions:
110    index = (T-17)*6 + frac - 3;
111                        where T=[17..94] and frac=[-2,-1,0,1,2,3]
112  and for the integer only range
113    index = (T - 95) + 463;        where T=[95..143]
114
115                   Second and fourth subframes:
116                   ----------------------------
117  For the 2nd and 4th subframes a resolution of 1/6 is always used,
118  and the search range is relative to the lag in previous subframe.
119  If t0 is the lag in the previous subframe then
120  t_min=t0-5   and  t_max=t0+4   and  the range is given by
121      (t_min-1) 3/6   to  (t_max) 3/6
122
123  The period in the 2nd (and 4th) subframe is encoded with 6 bits:
124    index = (T-(t_min-1))*6 + frac - 3;
125                where T=[t_min-1..t_max] and frac=[-2,-1,0,1,2,3]
126
127  Note that only 61 values are used. If the decoder receives 61, 62,
128  or 63 as the relative pitch index, it means that a transmission
129  error occurred and the pitch from previous subframe should be used.
130
131 ------------------------------------------------------------------------------
132  REQUIREMENTS
133
134  None
135
136 ------------------------------------------------------------------------------
137  REFERENCES
138
139  enc_lag6.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
140
141 ------------------------------------------------------------------------------
142  PSEUDO-CODE
143
144
145 ------------------------------------------------------------------------------
146  CAUTION [optional]
147  [State any special notes, constraints or cautions for users of this function]
148
149 ------------------------------------------------------------------------------
150 */
151
152 Word16 Enc_lag6(         /* o : Return index of encoding             */
153     Word16 T0,           /* i : Pitch delay                          */
154     Word16 T0_frac,      /* i : Fractional pitch delay               */
155     Word16 T0_min,       /* i : minimum of search range              */
156     Word16 delta_flag,   /* i : Flag for 1st (or 3rd) subframe       */
157     Flag   *pOverflow    /* o : overflow indicator                   */
158 )
159 {
160     Word16 index;
161     Word16 i;
162     Word16 temp;
163
164     if (delta_flag == 0)          /* if 1st or 3rd subframe */
165     {
166         /* encode pitch delay (with fraction) */
167         if (T0 <= 94)
168         {
169             /* index = T0*6 - 105 + T0_frac */
170             i = (T0 << 3) - (T0 << 1) - 105;
171
172             index = i + T0_frac;
173         }
174         else
175         {
176             index = T0 + 368;
177         }
178
179     }
180     else
181         /* if second or fourth subframe */
182     {
183         /* index = 6*(T0-T0_min) + 3 + T0_frac  */
184         temp = (T0 - T0_min);
185
186         i = (temp << 3) - (temp << 1);
187         i += 3;
188
189         index = i + T0_frac;
190     }
191
192     return index;
193 }