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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Pathname: ./audio/gsm-amr/c/src/enc_lag6.c
35 Functions:
36
37     Date: 02/05/2002
38
39------------------------------------------------------------------------------
40 REVISION HISTORY
41
42 Description: Updated template used to PV coding template.
43 Changed to accept the pOverflow flag for EPOC compatibility.
44
45 Description:
46 (1) Removed optimization -- mult(i, 6, pOverflow) is NOT the same as adding
47     i to itself 6 times.  The reason is because the mult function does a
48     right shift by 15, which will obliterate smaller numbers.
49
50 Description:  Replaced "int" and/or "char" with OSCL defined types.
51
52 Description:
53
54------------------------------------------------------------------------------
55 MODULE DESCRIPTION
56
57
58------------------------------------------------------------------------------
59*/
60
61/*----------------------------------------------------------------------------
62; INCLUDES
63----------------------------------------------------------------------------*/
64#include "enc_lag6.h"
65#include "typedef.h"
66#include "basic_op.h"
67
68/*----------------------------------------------------------------------------
69; MACROS
70; Define module specific macros here
71----------------------------------------------------------------------------*/
72
73/*----------------------------------------------------------------------------
74; DEFINES
75; Include all pre-processor statements here. Include conditional
76; compile variables also.
77----------------------------------------------------------------------------*/
78
79/*----------------------------------------------------------------------------
80; LOCAL FUNCTION DEFINITIONS
81; Function Prototype declaration
82----------------------------------------------------------------------------*/
83
84/*----------------------------------------------------------------------------
85; LOCAL VARIABLE DEFINITIONS
86; Variable declaration - defined here and used outside this module
87----------------------------------------------------------------------------*/
88
89
90/*
91------------------------------------------------------------------------------
92 FUNCTION NAME: Enc_lag6
93------------------------------------------------------------------------------
94 INPUT AND OUTPUT DEFINITIONS
95
96 Inputs:
97    T0 -- Word16 -- Pitch delay
98    T0_frac -- Word16 -- Fractional pitch delay
99    T0_min -- Word16 -- minimum of search range
100    delta_flag -- Word16 -- Flag for 1st (or 3rd) subframe
101
102 Outputs:
103    pOverflow -- Pointer to Flag -- overflow indicator
104
105 Returns:
106    Word16 -- Return index of encoding
107
108 Global Variables Used:
109    None
110
111 Local Variables Needed:
112    None
113
114------------------------------------------------------------------------------
115 FUNCTION DESCRIPTION
116
117 PURPOSE:  Encoding of fractional pitch lag with 1/6 resolution.
118
119 DESCRIPTION:
120                  First and third subframes:
121                  --------------------------
122 The pitch range is divided as follows:
123         17 3/6  to   94 3/6   resolution 1/6
124         95      to   143      resolution 1
125
126 The period is encoded with 9 bits.
127 For the range with fractions:
128   index = (T-17)*6 + frac - 3;
129                       where T=[17..94] and frac=[-2,-1,0,1,2,3]
130 and for the integer only range
131   index = (T - 95) + 463;        where T=[95..143]
132
133                  Second and fourth subframes:
134                  ----------------------------
135 For the 2nd and 4th subframes a resolution of 1/6 is always used,
136 and the search range is relative to the lag in previous subframe.
137 If t0 is the lag in the previous subframe then
138 t_min=t0-5   and  t_max=t0+4   and  the range is given by
139     (t_min-1) 3/6   to  (t_max) 3/6
140
141 The period in the 2nd (and 4th) subframe is encoded with 6 bits:
142   index = (T-(t_min-1))*6 + frac - 3;
143               where T=[t_min-1..t_max] and frac=[-2,-1,0,1,2,3]
144
145 Note that only 61 values are used. If the decoder receives 61, 62,
146 or 63 as the relative pitch index, it means that a transmission
147 error occurred and the pitch from previous subframe should be used.
148
149------------------------------------------------------------------------------
150 REQUIREMENTS
151
152 None
153
154------------------------------------------------------------------------------
155 REFERENCES
156
157 enc_lag6.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
158
159------------------------------------------------------------------------------
160 PSEUDO-CODE
161
162
163------------------------------------------------------------------------------
164 RESOURCES USED [optional]
165
166 When the code is written for a specific target processor the
167 the resources used should be documented below.
168
169 HEAP MEMORY USED: x bytes
170
171 STACK MEMORY USED: x bytes
172
173 CLOCK CYCLES: (cycle count equation for this function) + (variable
174                used to represent cycle count for each subroutine
175                called)
176     where: (cycle count variable) = cycle count for [subroutine
177                                     name]
178
179------------------------------------------------------------------------------
180 CAUTION [optional]
181 [State any special notes, constraints or cautions for users of this function]
182
183------------------------------------------------------------------------------
184*/
185
186Word16 Enc_lag6(         /* o : Return index of encoding             */
187    Word16 T0,           /* i : Pitch delay                          */
188    Word16 T0_frac,      /* i : Fractional pitch delay               */
189    Word16 T0_min,       /* i : minimum of search range              */
190    Word16 delta_flag,   /* i : Flag for 1st (or 3rd) subframe       */
191    Flag   *pOverflow    /* o : overflow indicator                   */
192)
193{
194    Word16 index;
195    Word16 i;
196    Word16 temp;
197
198    if (delta_flag == 0)          /* if 1st or 3rd subframe */
199    {
200        /* encode pitch delay (with fraction) */
201        if (T0 <= 94)
202        {
203            /* index = T0*6 - 105 + T0_frac */
204            i = 6 * T0 - 105;
205
206            index = add(i, T0_frac, pOverflow);
207        }
208        else
209        {
210            index = add(T0, 368, pOverflow);
211        }
212
213    }
214    else
215        /* if second or fourth subframe */
216    {
217        /* index = 6*(T0-T0_min) + 3 + T0_frac  */
218        temp = sub(T0, T0_min, pOverflow);
219
220        i = add(temp, temp, pOverflow);
221        i = add(temp, i, pOverflow);
222        i = add(i, i, pOverflow);
223
224        i = add(i, 3, pOverflow);
225
226        index = add(i, T0_frac, pOverflow);
227    }
228
229    return index;
230}
231