summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/aacdec/mdct_fxp.cpp
blob: df371e8485633527da5cb01f8b2d3652b7f93124 (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* ------------------------------------------------------------------
 * 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.
 * -------------------------------------------------------------------
 */
/*

 Pathname: mdct_fxp.c
 Funtions: fft_rx2

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

 Inputs:
    data_quant  = Input vector, with quantized Q15 spectral lines:
                  type Int32

    Q_FFTarray  = Scratch memory used for in-place IFFT calculation,
                  min size required 1024, type Int32

    n           = Length of input vector "data_quant". Currently 256 or 2048.
                  type const Int

 Local Stores/Buffers/Pointers Needed:
    None

 Global Stores/Buffers/Pointers Needed:
    None

 Outputs:
    shift = shift factor to reflect scaling introduced by FFT and mdct_fxp,

 Pointers and Buffers Modified:
    calculation are done in-place and returned in "data_quant"

 Local Stores Modified:
     None

 Global Stores Modified:
     None

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

    The MDCT is a linear orthogonal lapped transform, based on the idea of
    time domain aliasing cancellation (TDAC).
    MDCT is critically sampled, which means that though it is 50% overlapped,
    a sequence data after MDCT has the same number of coefficients as samples
    before the transform (after overlap-and-add). This means, that a single
    block of MDCT data does not correspond to the original block on which the
    MDCT was performed. When subsequent blocks of data are added (still using
    50% overlap), the errors introduced by the transform cancels out.
    Thanks to the overlapping feature, the MDCT is very useful for
    quantization. It effectively removes the otherwise easily detectable
    blocking artifact between transform blocks.
    N = length of input vector X
    X = vector of length N/2, will hold fixed point DCT
    k = 0:1:N-1

                        N-1
            X(m) =  2   SUM   x(k)*cos(pi/(2*N)*(2*k+1+N/2)*(2*m+1))
                        k=0


    The window that completes the TDAC is applied before calling this function.
    The MDCT can be calculated using an FFT, for this, the MDCT needs to be
    rewritten as an odd-time odd-frequency discrete Fourier transform. Thus,
    the MDCT can be calculated using only one n/4 point FFT and some pre and
    post-rotation of the sample points.

    Computation of the MDCT implies computing

        x  = ( y   - y        ) + j( y       +  y       )
         n      2n    N/2-1-2n        N-1-2n     N/2+2n

    using the Fast discrete cosine transform as described in [2]

    where x(n) is an input with N points

    x(n) ----------------------------
                                     |
                                     |
                    Pre-rotation by exp(j(2pi/N)(n+1/8))
                                     |
                                     |
                              N/4- point FFT
                                     |
                                     |
                    Post-rotation by exp(j(2pi/N)(k+1/8))
                                     |
                                     |
                                      ------------- DCT

    By considering the N/2 overlap, a relation between successive input blocks
    is found:

        x   (2n) = x (N/2 + 2n)
         m+1        m
------------------------------------------------------------------------------
 REQUIREMENTS

    This function should provide a fixed point MDCT with an average
    quantization error less than 1 %.

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

    [1] Analysis/Synthesis Filter Bank design based on time domain
        aliasing cancellation
        Jhon Princen, et. al.
        IEEE Transactions on ASSP, vol ASSP-34, No. 5 October 1986
        Pg 1153 - 1161

    [2] Regular FFT-related transform kernels for DCT/DST based
        polyphase filterbanks
        Rolf Gluth
        Proc. ICASSP 1991, pg. 2205 - 2208

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

  Cx, Cy are complex number


    exp = log2(n)-1

    FOR ( k=0; k< n/4; k +=2)

        Cx =   (data_quant[3n/4 + k] + data_quant[3n/4 - 1 - k]) +
             j (data_quant[ n/4 + k] - data_quant[ n/4 - 1 - k])

        Q_FFTarray = Cx * exp(-j(2pi/n)(k+1/8))

    ENDFOR

    FOR ( k=n/4; k< n/2; k +=2)

        Cx =   (data_quant[3n/4 - 1 - k] + data_quant[ - n/4 + k]) +
             j (data_quant[5n/4 - 1 - k] - data_quant[   n/4 + k])

        Q_FFTarray = Cx * exp(-j(2pi/n)(k+1/8))

    ENDFOR

    CALL FFT( Q_FFTarray, n/4)

    MODIFYING( Q_FFTarray )

    RETURNING( shift )

    FOR ( k=0; k< n/2; k +=2)

        Cx = Q_FFTarray[ k] + j Q_FFTarray[ k+1]

        Cy = 2 * Cx * exp(-j(2pi/n)(k+1/8))

        data_quant[           k ] = - Real(Cy)
        data_quant[ n/2 - 1 - k ] =   Imag(Cy)
        data_quant[ n/2     + k ] = - Imag(Cy)
        data_quant[ n       - k ] =   Real(Cy)

    ENDFOR

    MODIFIED    data_quant[]

    RETURN      (-shift-1)

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

 STACK USAGE: [stack count for this module] + [variable to represent
          stack usage for each subroutine called]

     where: [stack usage variable] = stack usage for [subroutine
         name] (see [filename].ext)

 DATA MEMORY USED: x words

 PROGRAM MEMORY USED: x words

 CLOCK CYCLES: [cycle count equation for this module] + [variable
           used to represent cycle count for each subroutine
           called]

     where: [cycle count variable] = cycle count for [subroutine
        name] (see [filename].ext)

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


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

#include "pv_audio_type_defs.h"
#include "mdct_fxp.h"
#include "fft_rx4.h"
#include "mix_radix_fft.h"
#include "fwd_long_complex_rot.h"
#include "fwd_short_complex_rot.h"


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

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


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

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


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

/*----------------------------------------------------------------------------
; EXTERNAL VARIABLES REFERENCES
; Declare variables used in this module but defined elsewhere
----------------------------------------------------------------------------*/

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


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


Int mdct_fxp(
    Int32   data_quant[],
    Int32   Q_FFTarray[],
    Int     n)
{

    Int32   temp_re;
    Int32   temp_im;

    Int32   temp_re_32;
    Int32   temp_im_32;

    Int16     cos_n;
    Int16     sin_n;
    Int32     exp_jw;
    Int     shift;


    const Int32 *p_rotate;


    Int32   *p_data_1;
    Int32   *p_data_2;
    Int32   *p_data_3;
    Int32   *p_data_4;

    Int32 *p_Q_FFTarray;

    Int32   max1;

    Int k;
    Int n_2   = n >> 1;
    Int n_4   = n >> 2;
    Int n_8   = n >> 3;
    Int n_3_4 = 3 * n_4;

    switch (n)
    {
        case SHORT_WINDOW_TYPE:
            p_rotate = (Int32 *)exp_rotation_N_256;
            break;

        case LONG_WINDOW_TYPE:
            p_rotate = (Int32 *)exp_rotation_N_2048;
            break;

        default:
            /*
             *  There is no defined behavior for a non supported frame
             *  size. By returning a fixed scaling factor, the input will
             *  scaled down and this will be heard as a low level noise
             */
            return(ERROR_IN_FRAME_SIZE);

    }

    /*--- Reordering and Pre-rotation by exp(-j(2pi/N)(r+1/8))   */
    p_data_1 = &data_quant[n_3_4];
    p_data_2 = &data_quant[n_3_4 - 1];
    p_data_3 = &data_quant[n_4];
    p_data_4 = &data_quant[n_4 - 1];

    p_Q_FFTarray = Q_FFTarray;

    max1 = 0;

    for (k = n_8; k > 0; k--)
    {
        /*
         *  scale down to ensure numbers are Q15
         *  temp_re and temp_im are 32-bit but
         *  only the lower 16 bits are used
         */

        temp_re = (*(p_data_1++) + *(p_data_2--)) >> 1;
        temp_im = (*(p_data_3++) - *(p_data_4--)) >> 1;


        /*
         * cos_n + j*sin_n == exp(j(2pi/N)(k+1/8))
         */

        exp_jw = *p_rotate++;

        cos_n = (Int16)(exp_jw >> 16);
        sin_n = (Int16)(exp_jw & 0xFFFF);

        temp_re_32 = temp_re * cos_n + temp_im * sin_n;
        temp_im_32 = temp_im * cos_n - temp_re * sin_n;
        *(p_Q_FFTarray++) = temp_re_32;
        *(p_Q_FFTarray++) = temp_im_32;
        max1         |= (temp_re_32 >> 31) ^ temp_re_32;
        max1         |= (temp_im_32 >> 31) ^ temp_im_32;


        p_data_1++;
        p_data_2--;
        p_data_4--;
        p_data_3++;
    }


    p_data_1 = &data_quant[n - 1];
    p_data_2 = &data_quant[n_2 - 1];
    p_data_3 = &data_quant[n_2];
    p_data_4 =  data_quant;

    for (k = n_8; k > 0; k--)
    {
        /*
         *  scale down to ensure numbers are Q15
         */
        temp_re = (*(p_data_2--) - *(p_data_4++)) >> 1;
        temp_im = (*(p_data_1--) + *(p_data_3++)) >> 1;

        p_data_2--;
        p_data_1--;
        p_data_4++;
        p_data_3++;

        /*
         * cos_n + j*sin_n == exp(j(2pi/N)(k+1/8))
         */

        exp_jw = *p_rotate++;

        cos_n = (Int16)(exp_jw >> 16);
        sin_n = (Int16)(exp_jw & 0xFFFF);

        temp_re_32 = temp_re * cos_n + temp_im * sin_n;
        temp_im_32 = temp_im * cos_n - temp_re * sin_n;

        *(p_Q_FFTarray++) = temp_re_32;
        *(p_Q_FFTarray++) = temp_im_32;
        max1         |= (temp_re_32 >> 31) ^ temp_re_32;
        max1         |= (temp_im_32 >> 31) ^ temp_im_32;


    } /* for(k) */



    p_Q_FFTarray = Q_FFTarray;

    if (max1)
    {

        if (n != SHORT_WINDOW_TYPE)
        {

            shift = mix_radix_fft(
                        Q_FFTarray,
                        &max1);

            shift += fwd_long_complex_rot(
                         Q_FFTarray,
                         data_quant,
                         max1);

        }
        else        /*  n_4 is 64 */
        {

            shift = fft_rx4_short(
                        Q_FFTarray,
                        &max1);

            shift += fwd_short_complex_rot(
                         Q_FFTarray,
                         data_quant,
                         max1);
        }

    }
    else
    {
        shift = -31;
    }

    /*
     *  returns shift introduced by FFT and mdct_fxp, 12 accounts for
     *  regular downshift (14) and MDCT scale factor (-2)
     *  number are returned as 16 bits
     */
    return (12 - shift);

} /* mdct_fxp */