summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/enc/src/enc_lag3.cpp
blob: a1fc0080bada245977f1535d8715ecec2fef5692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/****************************************************************************************
Portions of this file are derived from the following 3GPP standard:

    3GPP TS 26.073
    ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
    Available from http://www.3gpp.org

(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
Permission to distribute, modify and use this file under the standard license
terms listed above has been obtained from the copyright holder.
****************************************************************************************/
/*
------------------------------------------------------------------------------



 Pathname: ./audio/gsm-amr/c/src/enc_lag3.c
 Functions:

     Date: 01/28/2002

------------------------------------------------------------------------------
 REVISION HISTORY

 Description:  Replaced "int" and/or "char" with OSCL defined types.

 Description:

------------------------------------------------------------------------------
*/

/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "enc_lag3.h"
#include "typedef.h"
#include "basic_op.h"
#include "cnst.h"

/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; LOCAL VARIABLE DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/

/*
------------------------------------------------------------------------------
 FUNCTION NAME: enc_lag3
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
  T0 = Pitch delay of type Word16
  T0_frac = Fractional pitch delay of type Word16
  T0_prev = Integer pitch delay of last subframe of type Word16
  T0_min  = minimum of search range of type Word16
  T0_max  = maximum of search range of type Word16
  delta_flag = Flag for 1st (or 3rd) subframe of type Word16
  flag4   = Flag for encoding with 4 bits of type Word16
  pOverflow = pointer indicating overflow of type Flag

 Outputs:
  pOverflow = 1 if there is an overflow else it is zero.

 Returns:
  None

 Global Variables Used:
  None

 Local Variables Needed:
  None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function implements the encoding of fractional pitch lag with
 1/3 resolution.

 *   FUNCTION:  Enc_lag3
 *
 *   PURPOSE:  Encoding of fractional pitch lag with 1/3 resolution.
 *
 *   DESCRIPTION:
 *                    First and third subframes:
 *                    --------------------------
 *   The pitch range is divided as follows:
 *           19 1/3  to   84 2/3   resolution 1/3
 *           85      to   143      resolution 1
 *
 *   The period is encoded with 8 bits.
 *   For the range with fractions:
 *     index = (T-19)*3 + frac - 1;
 *                         where T=[19..85] and frac=[-1,0,1]
 *   and for the integer only range
 *     index = (T - 85) + 197;        where T=[86..143]
 *
 *                    Second and fourth subframes:
 *                    ----------------------------
 *   For the 2nd and 4th subframes a resolution of 1/3 is always used,
 *   and the search range is relative to the lag in previous subframe.
 *   If t0 is the lag in the previous subframe then
 *   t_min=t0-5   and  t_max=t0+4   and  the range is given by
 *        t_min - 2/3   to  t_max + 2/3
 *
 *   The period in the 2nd (and 4th) subframe is encoded with 5 bits:
 *     index = (T-(t_min-1))*3 + frac - 1;
 *                 where T=[t_min-1..t_max+1]

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 enc_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

   Word16 index, i, tmp_ind, uplag;
   Word16 tmp_lag;

   if (delta_flag == 0)
   {  // if 1st or 3rd subframe

      // encode pitch delay (with fraction)

      if (sub (T0, 85) <= 0)
      {
         // index = T0*3 - 58 + T0_frac
         i = add (add (T0, T0), T0);
         index = add (sub (i, 58), T0_frac);
      }
      else
      {
         index = add (T0, 112);
      }
   }
   else
   {   // if second or fourth subframe
      if (flag4 == 0) {

         // 'normal' encoding: either with 5 or 6 bit resolution

         // index = 3*(T0 - T0_min) + 2 + T0_frac
         i = sub (T0, T0_min);
         i = add (add (i, i), i);
         index = add (add (i, 2), T0_frac);
      }
      else {

         // encoding with 4 bit resolution

         tmp_lag = T0_prev;

         if ( sub( sub(tmp_lag, T0_min), 5) > 0)
            tmp_lag = add (T0_min, 5);
         if ( sub( sub(T0_max, tmp_lag), 4) > 0)
            tmp_lag = sub (T0_max, 4);

         uplag = add (add (add (T0, T0), T0), T0_frac);

         i = sub (tmp_lag, 2);
         tmp_ind = add (add (i, i), i);

         if (sub (tmp_ind, uplag) >= 0) {
            index = add (sub (T0, tmp_lag), 5);
         }
         else {

            i = add (tmp_lag, 1);
            i = add (add (i, i), i);

            if (sub (i, uplag) > 0) {

                index = add ( sub (uplag, tmp_ind), 3);
            }
            else {

               index = add (sub (T0, tmp_lag), 11);
            }
         }

      } // end if (encoding with 4 bit resolution)
   }   // end if (second of fourth subframe)

   return index;
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/


Word16 Enc_lag3(         /* o  : Return index of encoding             */
    Word16 T0,           /* i  : Pitch delay                          */
    Word16 T0_frac,      /* i  : Fractional pitch delay               */
    Word16 T0_prev,      /* i  : Integer pitch delay of last subframe */
    Word16 T0_min,       /* i  : minimum of search range              */
    Word16 T0_max,       /* i  : maximum of search range              */
    Word16 delta_flag,   /* i  : Flag for 1st (or 3rd) subframe       */
    Word16 flag4,        /* i  : Flag for encoding with 4 bits        */
    Flag   *pOverflow
)
{
    Word16 index, i, tmp_ind, uplag;
    Word16 tmp_lag;
    Word16 temp1;
    Word16 temp2;



    if (delta_flag == 0)
    {  /* if 1st or 3rd subframe */

        /* encode pitch delay (with fraction) */
        temp1 = sub(T0, 85, pOverflow);
        if (temp1 <= 0)
        {
            /* index = T0*3 - 58 + T0_frac   */
            temp2 = add(T0, T0, pOverflow);
            i = add(temp2, T0, pOverflow);
            temp2 = sub(i, 58, pOverflow);
            index = add(temp2, T0_frac, pOverflow);
        }
        else
        {
            index = add(T0, 112, pOverflow);
        }
    }
    else
    {   /* if second or fourth subframe */
        if (flag4 == 0)
        {

            /* 'normal' encoding: either with 5 or 6 bit resolution */

            /* index = 3*(T0 - T0_min) + 2 + T0_frac */
            i = sub(T0, T0_min, pOverflow);
            temp2 = add(i, i, pOverflow);
            i = add(temp2, i, pOverflow);
            temp2 = add(i, 2, pOverflow);
            index = add(temp2, T0_frac, pOverflow);
        }
        else
        {

            /* encoding with 4 bit resolution */

            tmp_lag = T0_prev;
            temp1 = sub(tmp_lag, T0_min, pOverflow);
            temp2 = sub(temp1, 5, pOverflow);
            if (temp2 > 0)
                tmp_lag = add(T0_min, 5, pOverflow);
            temp1 = sub(T0_max, tmp_lag, pOverflow);
            temp2 = sub(temp1, 4, pOverflow);
            if (temp2 > 0)
                tmp_lag = sub(T0_max, 4, pOverflow);

            temp1 = add(T0, T0, pOverflow);
            temp2 = add(temp1, T0, pOverflow);
            uplag = add(temp2, T0_frac, pOverflow);

            i = sub(tmp_lag, 2, pOverflow);
            temp1 = add(i, i, pOverflow);
            tmp_ind = add(temp1, i, pOverflow);

            temp1 = sub(tmp_ind, uplag, pOverflow);
            if (temp1 >= 0)
            {
                temp1 = sub(T0, tmp_lag, pOverflow);
                index = add(temp1, 5, pOverflow);
            }
            else
            {

                i = add(tmp_lag, 1, pOverflow);
                temp1 = add(i, i, pOverflow);
                i = add(temp1, i, pOverflow);

                if (sub(i, uplag, pOverflow) > 0)
                {
                    temp1 = sub(uplag, tmp_ind, pOverflow);
                    index = add(temp1, 3, pOverflow);
                }
                else
                {
                    temp1 = sub(T0, tmp_lag, pOverflow);
                    index = add(temp1, 11, pOverflow);
                }
            }

        } /* end if (encoding with 4 bit resolution) */
    }   /* end if (second of fourth subframe) */

    return index;
}