summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrwb/src/oversamp_12k8_to_16k.cpp
blob: 806851ea42e851fbe3e221fb9c7340ba8bce264a (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
/* ------------------------------------------------------------------
 * 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.173
    ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
    Available from http://www.3gpp.org

(C) 2007, 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.
****************************************************************************************/
/*
------------------------------------------------------------------------------



 Filename: oversamp_12k8_to_16k.cpp

     Date: 05/08/2004

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


 Description:

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

     int16 signal[],             input signal / output is divided by 16
     int16 lg,                   lenght of signal
     int16 mem[]                 in/out: memory (size=30)
     int16 x[]                   scratch mem ( size= 60)

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

 Oversamp_16k : oversampling from 12.8kHz to 16kHz.


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


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

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

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


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/

#include "pv_amr_wb_type_defs.h"
#include "pvamrwbdecoder_basic_op.h"
#include "pvamrwbdecoder_acelp.h"
#include "pvamrwbdecoder_cnst.h"

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


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

#define FAC4   4
#define FAC5   5
#define INV_FAC5   6554                    /* 1/5 in Q15 */
#define DOWN_FAC  26215                    /* 4/5 in Q15 */
#define UP_FAC    20480                    /* 5/4 in Q14 */
#define NB_COEF_DOWN  15
#define NB_COEF_UP    12
#define N_LOOP_COEF_UP    4

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

#ifdef __cplusplus
extern "C"
{
#endif


    /* Local functions */

    void AmrWbUp_samp(
        int16 * sig_d,                       /* input:  signal to oversampling  */
        int16 * sig_u,                       /* output: oversampled signal      */
        int16 L_frame                        /* input:  length of output        */
    );


    int16 AmrWbInterpol(                      /* return result of interpolation */
        int16 * x,                           /* input vector                   */
        const int16 * fir,                   /* filter coefficient             */
        int16 nb_coef                        /* number of coefficients         */
    );


#ifdef __cplusplus
}
#endif

/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/


/* 1/5 resolution interpolation filter  (in Q14)  */
/* -1.5dB @ 6kHz,    -6dB @ 6.4kHz, -10dB @ 6.6kHz,
    -20dB @ 6.9kHz, -25dB @ 7kHz,   -55dB @ 8kHz  */


const int16 fir_up[4][24] =
{

    {
        -1,        12,       -33,       68,       -119,       191,
        -291,       430,      -634,       963,     -1616,      3792,
        15317,     -2496,      1288,      -809,       542,      -369,
        247,      -160,        96,       -52,        23,        -6,
    },
    {
        -4,        24,       -62,       124,      -213,       338,
        -510,       752,     -1111,      1708,     -2974,      8219,
        12368,     -3432,      1881,     -1204,       812,      -552,
        368,      -235,       139,       -73,        30,        -7,
    },
    {
        -7,        30,       -73,       139,      -235,       368,
        -552,       812,     -1204,      1881,     -3432,     12368,
        8219,     -2974,      1708,     -1111,       752,      -510,
        338,      -213,       124,       -62,        24,        -4,
    },
    {
        -6,        23,       -52,        96,      -160,       247,
        -369,       542,      -809,      1288,     -2496,     15317,
        3792,     -1616,       963,      -634,       430,      -291,
        191,      -119,        68,       -33,        12,        -1,
    }
};

/*----------------------------------------------------------------------------
; EXTERNAL FUNCTION REFERENCES
; Declare functions defined elsewhere and referenced in this module
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
; Declare variables used in this module but defined elsewhere
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/


/* output: memory (2*NB_COEF_UP) set to zeros  */
void oversamp_12k8_to_16k_init(int16 mem[])
{
    pv_memset((void *)mem, 0, (2*NB_COEF_UP)*sizeof(*mem));

}

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/

void oversamp_12k8_to_16k(
    int16 sig12k8[],                     /* input:  signal to oversampling  */
    int16 lg,                            /* input:  length of input         */
    int16 sig16k[],                      /* output: oversampled signal      */
    int16 mem[],                         /* in/out: memory (2*NB_COEF_UP)   */
    int16 signal[]
)
{
    int16 lg_up;

    pv_memcpy((void *)signal,
              (void *)mem,
              (2*NB_COEF_UP)*sizeof(*mem));

    pv_memcpy((void *)(signal + (2*NB_COEF_UP)),
              (void *)sig12k8,
              lg*sizeof(*sig12k8));

    lg_up = lg + (lg >> 2); /* 5/4 of lg */

    AmrWbUp_samp(signal + NB_COEF_UP, sig16k, lg_up);

    pv_memcpy((void *)mem,
              (void *)(signal + lg),
              (2*NB_COEF_UP)*sizeof(*signal));

    return;
}



/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/


void AmrWbUp_samp(
    int16 * sig_d,                       /* input:  signal to oversampling  */
    int16 * sig_u,                       /* output: oversampled signal      */
    int16 L_frame                        /* input:  length of output        */
)
{

    int32 i;
    int16 frac, j;
    int16 * pt_sig_u = sig_u;

    frac = 1;
    for (j = 0; j < L_frame; j++)
    {
        i = ((int32)j * INV_FAC5) >> 13;       /* integer part = pos * 1/5 */

        frac--;
        if (frac)
        {
            *(pt_sig_u++) = AmrWbInterpol(&sig_d[i],
                                          fir_up[(FAC5-1) - frac],
                                          N_LOOP_COEF_UP);
        }
        else
        {
            *(pt_sig_u++) = sig_d[i+12 - NB_COEF_UP ];
            frac = FAC5;
        }
    }

}

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/


/* Fractional interpolation of signal at position (frac/resol) */


int16 AmrWbInterpol(                      /* return result of interpolation */
    int16 * x,                           /* input vector                   */
    const int16 *fir,                    /* filter coefficient             */
    int16 nb_coef                        /* number of coefficients         */
)
{
    int32 L_sum;
    const int16 *pt_fir = fir;

    int16 tmp1, tmp2, tmp3, tmp4;
    int16 *pt_x = x - nb_coef - (nb_coef << 1) + 1;


    tmp1 = *(pt_x++);
    tmp2 = *(pt_x++);
    tmp3 = *(pt_x++);
    tmp4 = *(pt_x++);
    L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), 0x00002000L);
    L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
    tmp1 = *(pt_x++);
    tmp2 = *(pt_x++);
    tmp3 = *(pt_x++);
    tmp4 = *(pt_x++);
    L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
    tmp1 = *(pt_x++);
    tmp2 = *(pt_x++);
    tmp3 = *(pt_x++);
    tmp4 = *(pt_x++);
    L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
    tmp1 = *(pt_x++);
    tmp2 = *(pt_x++);
    tmp3 = *(pt_x++);
    tmp4 = *(pt_x++);
    L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
    tmp1 = *(pt_x++);
    tmp2 = *(pt_x++);
    tmp3 = *(pt_x++);
    tmp4 = *(pt_x++);
    L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);
    tmp1 = *(pt_x++);
    tmp2 = *(pt_x++);
    tmp3 = *(pt_x++);
    tmp4 = *(pt_x++);
    L_sum = fxp_mac_16by16(tmp1, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp2, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp3, *(pt_fir++), L_sum);
    L_sum = fxp_mac_16by16(tmp4, *(pt_fir++), L_sum);


    L_sum = shl_int32(L_sum, 2);               /* saturation can occur here */

    return ((int16)(L_sum >> 16));
}