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/dec_lag3.c
35 Functions: Dec_lag3
36
37     Date: 01/31/2002
38
39------------------------------------------------------------------------------
40 REVISION HISTORY
41
42 Description:
43 (1) Updated to accept new parameter, Flag *pOverflow.
44 (2) Placed file in the proper PV Software template.
45
46 Description:
47 (1) Removed "count.h" and "basic_op.h" and replaced with individual include
48     files (add.h, sub.h, etc.)
49
50 Description:
51 (1) Removed optimization -- mult(i, 3, pOverflow) is NOT the same as adding
52     i to itself 3 times.  The reason is because the mult function does a
53     right shift by 15, which will obliterate smaller numbers.
54
55 Description:  Replaced "int" and/or "char" with OSCL defined types.
56
57 Description:
58
59 ------------------------------------------------------------------------------
60 INPUT AND OUTPUT DEFINITIONS
61
62 Inputs:
63    index   -- Word16 -- received pitch index
64    t0_min  -- Word16 -- minimum of search range
65    t0_max  -- Word16 -- maximum of search range
66    i_subfr -- Word16 -- subframe flag
67    T0_prev -- Word16 -- integer pitch delay of last subframe
68                         used in 2nd and 4th subframes
69    flag4   -- Word16 -- flag for encoding with 4 bits
70
71 Outputs:
72
73    T0 -- Pointer to type Word16 -- integer part of pitch lag
74    T0_frac -- Pointer to type Word16 -- fractional part of pitch lag
75    pOverflow -- Pointer to type Flag -- Flag set when overflow occurs
76
77 Returns:
78    None.
79
80 Global Variables Used:
81    None
82
83 Local Variables Needed:
84    None
85
86
87              )
88------------------------------------------------------------------------------
89 FUNCTION DESCRIPTION
90
91 PURPOSE:  Decoding of fractional pitch lag with 1/3 resolution.
92           Extract the integer and fraction parts of the pitch lag from
93           the received adaptive codebook index.
94
95  See "Enc_lag3.c" for more details about the encoding procedure.
96
97  The fractional lag in 1st and 3rd subframes is encoded with 8 bits
98  while that in 2nd and 4th subframes is relatively encoded with 4, 5
99  and 6 bits depending on the mode.
100
101------------------------------------------------------------------------------
102 REQUIREMENTS
103
104
105
106------------------------------------------------------------------------------
107 REFERENCES
108
109 dec_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
110
111------------------------------------------------------------------------------
112 PSEUDO-CODE
113
114
115
116------------------------------------------------------------------------------
117 RESOURCES USED
118   When the code is written for a specific target processor the
119     the resources used should be documented below.
120
121 STACK USAGE: [stack count for this module] + [variable to represent
122          stack usage for each subroutine called]
123
124     where: [stack usage variable] = stack usage for [subroutine
125         name] (see [filename].ext)
126
127 DATA MEMORY USED: x words
128
129 PROGRAM MEMORY USED: x words
130
131 CLOCK CYCLES: [cycle count equation for this module] + [variable
132           used to represent cycle count for each subroutine
133           called]
134
135     where: [cycle count variable] = cycle count for [subroutine
136        name] (see [filename].ext)
137
138------------------------------------------------------------------------------
139*/
140
141
142/*----------------------------------------------------------------------------
143; INCLUDES
144----------------------------------------------------------------------------*/
145#include "dec_lag3.h"
146#include "typedef.h"
147#include "basic_op.h"
148
149/*----------------------------------------------------------------------------
150; MACROS
151; Define module specific macros here
152----------------------------------------------------------------------------*/
153
154
155/*----------------------------------------------------------------------------
156; DEFINES
157; Include all pre-processor statements here. Include conditional
158; compile variables also.
159----------------------------------------------------------------------------*/
160
161/*----------------------------------------------------------------------------
162; LOCAL FUNCTION DEFINITIONS
163; Function Prototype declaration
164----------------------------------------------------------------------------*/
165
166
167/*----------------------------------------------------------------------------
168; LOCAL STORE/BUFFER/POINTER DEFINITIONS
169; Variable declaration - defined here and used outside this module
170----------------------------------------------------------------------------*/
171
172/*----------------------------------------------------------------------------
173; EXTERNAL FUNCTION REFERENCES
174; Declare functions defined elsewhere and referenced in this module
175----------------------------------------------------------------------------*/
176
177/*----------------------------------------------------------------------------
178; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
179; Declare variables used in this module but defined elsewhere
180----------------------------------------------------------------------------*/
181
182/*----------------------------------------------------------------------------
183; FUNCTION CODE
184----------------------------------------------------------------------------*/
185
186void Dec_lag3(Word16 index,     /* i : received pitch index                 */
187              Word16 t0_min,    /* i : minimum of search range              */
188              Word16 t0_max,    /* i : maximum of search range              */
189              Word16 i_subfr,   /* i : subframe flag                        */
190              Word16 T0_prev,   /* i : integer pitch delay of last subframe
191                                       used in 2nd and 4th subframes        */
192              Word16 * T0,      /* o : integer part of pitch lag            */
193              Word16 * T0_frac, /* o : fractional part of pitch lag         */
194              Word16 flag4,     /* i : flag for encoding with 4 bits        */
195              Flag  *pOverflow  /* o : Flag set when overflow occurs        */
196             )
197{
198    Word16 i;
199    Word16 tmp_lag;
200
201    if (i_subfr == 0)    /* if 1st or 3rd subframe */
202    {
203
204        if (index < 197)
205        {
206
207            tmp_lag = index + 2;
208
209            tmp_lag =
210                mult(
211                    tmp_lag,
212                    10923,
213                    pOverflow);
214
215            i =
216                add(
217                    tmp_lag,
218                    19,
219                    pOverflow);
220
221            *T0 = i;
222
223            /* i = 3 * (*T0) */
224
225            i = add(i, i, pOverflow);
226            i = add(i, *T0, pOverflow);
227
228            tmp_lag =
229                sub(
230                    index,
231                    i,
232                    pOverflow);
233
234            *T0_frac =
235                add(
236                    tmp_lag,
237                    58,
238                    pOverflow);
239        }
240        else
241        {
242            *T0 = index - 112;
243
244            *T0_frac = 0;
245        }
246
247    }
248    else
249    {  /* 2nd or 4th subframe */
250
251        if (flag4 == 0)
252        {
253
254            /* 'normal' decoding: either with 5 or 6 bit resolution */
255
256            i =
257                add(
258                    index,
259                    2,
260                    pOverflow);
261
262            i =
263                mult(
264                    i,
265                    10923,
266                    pOverflow);
267
268            i =
269                sub(
270                    i,
271                    1,
272                    pOverflow);
273
274            *T0 =
275                add(
276                    i,
277                    t0_min,
278                    pOverflow);
279
280            /* i = 3* (*T0) */
281            i = add(add(i, i, pOverflow), i, pOverflow);
282
283            tmp_lag =
284                sub(
285                    index,
286                    2,
287                    pOverflow);
288
289            *T0_frac =
290                sub(
291                    tmp_lag,
292                    i,
293                    pOverflow);
294        }
295        else
296        {
297
298            /* decoding with 4 bit resolution */
299
300            tmp_lag = T0_prev;
301
302            i =
303                sub(
304                    tmp_lag,
305                    t0_min,
306                    pOverflow);
307
308            if (i > 5)
309            {
310                tmp_lag =
311                    add(
312                        t0_min,
313                        5,
314                        pOverflow);
315            }
316
317            i =
318                sub(
319                    t0_max,
320                    tmp_lag,
321                    pOverflow);
322
323            if (i > 4)
324            {
325                tmp_lag =
326                    sub(
327                        t0_max,
328                        4,
329                        pOverflow);
330            }
331
332            if (index < 4)
333            {
334                i =
335                    sub(
336                        tmp_lag,
337                        5,
338                        pOverflow);
339
340                *T0 =
341                    add(
342                        i,
343                        index,
344                        pOverflow);
345
346                *T0_frac = 0;
347            }
348            else
349            {
350                /* 4 >= index < 12 */
351                if (index < 12)
352                {
353                    i = index - 5;
354
355                    i = mult(
356                            i,
357                            10923,
358                            pOverflow);
359
360                    i--;
361
362                    *T0 = add(
363                              i,
364                              tmp_lag,
365                              pOverflow);
366
367                    i = add(
368                            add(
369                                i,
370                                i,
371                                pOverflow),
372                            i,
373                            pOverflow);
374
375                    tmp_lag = index - 9;
376
377                    *T0_frac =
378                        sub(
379                            tmp_lag,
380                            i,
381                            pOverflow);
382                }
383                else
384                {
385                    i = index - 12;
386
387                    i =
388                        add(
389                            i,
390                            tmp_lag,
391                            pOverflow);
392
393                    *T0 =
394                        add(
395                            i,
396                            1,
397                            pOverflow);
398
399                    *T0_frac = 0;
400                }
401            }
402
403        } /* end if (decoding with 4 bit resolution) */
404    }
405
406    return;
407}
408