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_lag6.cpp
36 ------------------------------------------------------------------------------
40 ------------------------------------------------------------------------------
43 /*----------------------------------------------------------------------------
45 ----------------------------------------------------------------------------*/
50 /*----------------------------------------------------------------------------
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
57 ; Include all pre-processor statements here. Include conditional
58 ; compile variables also.
59 ----------------------------------------------------------------------------*/
61 /*----------------------------------------------------------------------------
62 ; LOCAL FUNCTION DEFINITIONS
63 ; Function Prototype declaration
64 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
67 ; LOCAL VARIABLE DEFINITIONS
68 ; Variable declaration - defined here and used outside this module
69 ----------------------------------------------------------------------------*/
73 ------------------------------------------------------------------------------
74 FUNCTION NAME: Enc_lag6
75 ------------------------------------------------------------------------------
76 INPUT AND OUTPUT DEFINITIONS
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
85 pOverflow -- Pointer to Flag -- overflow indicator
88 Word16 -- Return index of encoding
90 Global Variables Used:
93 Local Variables Needed:
96 ------------------------------------------------------------------------------
99 PURPOSE: Encoding of fractional pitch lag with 1/6 resolution.
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
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]
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
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]
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.
131 ------------------------------------------------------------------------------
136 ------------------------------------------------------------------------------
139 enc_lag6.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
141 ------------------------------------------------------------------------------
145 ------------------------------------------------------------------------------
147 [State any special notes, constraints or cautions for users of this function]
149 ------------------------------------------------------------------------------
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 */
164 if (delta_flag == 0) /* if 1st or 3rd subframe */
166 /* encode pitch delay (with fraction) */
169 /* index = T0*6 - 105 + T0_frac */
170 i = (T0 << 3) - (T0 << 1) - 105;
181 /* if second or fourth subframe */
183 /* index = 6*(T0-T0_min) + 3 + T0_frac */
184 temp = (T0 - T0_min);
186 i = (temp << 3) - (temp << 1);