summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/mp3dec/src/pvmp3_imdct_synth.cpp
blob: 11961d1e1d7fa56dd346c512d40f635a734bc6ce (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
/* ------------------------------------------------------------------
 * 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.
 * -------------------------------------------------------------------
 */
/*
------------------------------------------------------------------------------

   PacketVideo Corp.
   MP3 Decoder Library

   Filename: pvmp3_imdct_synth.cpp

     Date: 09/21/2007

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


 Description:

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

 Input
    int32 in[],         Pointer to spec values of current channel
    int32 overlap[],    Pointer to overlap values of current channel
    uint32 blk_type,    Block type
    int16 mx_band,      In case of mixed blocks, # of bands with long
                        blocks (2 or 4) else 0
    int32 *Scratch_mem
  Returns

    int32 in[],

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

    The frequency lines are preprocessed by the "alias reduction" scheme
    and fed into the IMDCT matrix, each 18 into one transform block.
    The first half of the output values are added to the stored overlap
    values from the last block. These values are new output values and
    are input values for the polyphase filterbank. The second half of the
    output values is stored for overlap with the next data granule.
    The number of windowed samples is 12 for short blocks, and 36 for long
    blocks

Windowing

    Depending on window_switching_flag[gr][ch], block_type[gr][ch] and
    mixed_block_flag[gr][ch] different shapes of windows are used.
        normal window
        start window
        stop window
        short windows
            Each of the three short blocks is windowed separately.
            The windowed short blocks must be overlapped and concatenated.

Overlapping and adding with previous block

    The first half (18 values) of the current block (36 values) has to be
    overlapped with the second half of the previous block. The second half
    of the current block has to be stored for overlapping with the next block

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


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

 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
     ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension

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

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


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

#include "pvmp3_imdct_synth.h"
#include "pv_mp3dec_fxd_op.h"
#include "pvmp3_dec_defs.h"
#include "pvmp3_mdct_18.h"
#include "pvmp3_mdct_6.h"
#include "mp3_mem_funcs.h"



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


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
#define LONG        0
#define START       1
#define SHORT       2
#define STOP        3

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

/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/
/*
 *   sin(pi/36*(k+0.5)),k=0..35
 */

const int32 normal_win[36] =
{
    Qfmt_31(0.08723877473068f),   Qfmt_31(0.26105238444010f),   Qfmt_31(0.43287922787620f),
    Qfmt_31(0.60141159900854f),   Qfmt_31(0.76536686473018f),   Qfmt_31(0.92349722647006f),
    Qfmt_31(0.53729960834682f),   Qfmt_31(0.60876142900872f),   Qfmt_31(0.67559020761566f),
    Qfmt_31(-0.73727733681012f),   Qfmt_31(-0.79335334029124f),   Qfmt_31(0.84339144581289f),
    Qfmt_31(0.88701083317822f),   Qfmt_31(0.92387953251129f),   Qfmt_31(-0.95371695074823f),
    Qfmt_31(-0.97629600711993f),   Qfmt_31(-0.99144486137381f),   Qfmt_31(-0.99904822158186f),
    Qfmt_31(0.99904822158186f),   Qfmt_31(0.99144486137381f),   Qfmt_31(0.97629600711993f),
    Qfmt_31(0.95371695074823f),   Qfmt_31(0.92387953251129f),   Qfmt_31(0.88701083317822f),
    Qfmt_31(0.84339144581289f),   Qfmt_31(0.79335334029124f),   Qfmt_31(0.73727733681012f),
    Qfmt_31(0.67559020761566f),   Qfmt_31(0.60876142900872f),   Qfmt_31(0.53729960834682f),
    Qfmt_31(0.46174861323503f),   Qfmt_31(0.38268343236509f),   Qfmt_31(0.30070579950427f),
    Qfmt_31(0.21643961393810f),   Qfmt_31(0.13052619222005f),   Qfmt_31(0.04361938736534f)
};


const int32 start_win[36] =
{
    /*   k=0..17  sin(pi/36*(k+0.5)),  */
    Qfmt_31(0.08723877473068f),   Qfmt_31(0.26105238444010f),   Qfmt_31(0.43287922787620f),
    Qfmt_31(0.60141159900854f),   Qfmt_31(0.76536686473018f),   Qfmt_31(0.92349722647006f),
    Qfmt_31(0.53729960834682f),   Qfmt_31(0.60876142900872f),   Qfmt_31(0.67559020761566f),
    Qfmt_31(-0.73727733681012f),   Qfmt_31(-0.79335334029124f),   Qfmt_31(0.84339144581289f),
    Qfmt_31(0.88701083317822f),   Qfmt_31(0.92387953251129f),   Qfmt_31(-0.95371695074823f),
    Qfmt_31(-0.97629600711993f),   Qfmt_31(-0.99144486137381f),   Qfmt_31(-0.99904822158186f),

    Qfmt_31(0.99999990000000f),   Qfmt_31(0.99999990000000f),   Qfmt_31(0.99999990000000f),
    Qfmt_31(0.99999990000000f),   Qfmt_31(0.99999990000000f),   Qfmt_31(0.99999990000000f),
    /*    k=24..29; sin(pi/12*(k-18+0.5)) */
    Qfmt_31(0.99144486137381f),   Qfmt_31(0.92387953251129f),   Qfmt_31(0.79335334029124f),
    Qfmt_31(0.60876142900872f),   Qfmt_31(0.38268343236509f),   Qfmt_31(0.13052619222005f),

    Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),
    Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f)
};


const int32 stop_win[36] =
{
    Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),
    Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),   Qfmt_31(0.00000000000000f),
    /*    k=6..11; sin(pi/12*(k-6+0.5)) */
    Qfmt_31(0.13052619222005f),   Qfmt_31(0.38268343236509f),   Qfmt_31(0.60876142900872f),
    Qfmt_31(-0.79335334029124f),   Qfmt_31(-0.92387953251129f),   Qfmt_31(0.99144486137381f),

    Qfmt_31(0.99999990000000f),   Qfmt_31(0.99999990000000f),   Qfmt_31(-0.99999990000000f),
    Qfmt_31(-0.99999990000000f),   Qfmt_31(-0.99999990000000f),   Qfmt_31(-0.99999990000000f),
    /*   k=18..35  sin(pi/36*(k+0.5)),  */
    Qfmt_31(0.99904822158186f),   Qfmt_31(0.99144486137381f),   Qfmt_31(0.97629600711993f),
    Qfmt_31(0.95371695074823f),   Qfmt_31(0.92387953251129f),   Qfmt_31(0.88701083317822f),
    Qfmt_31(0.84339144581289f),   Qfmt_31(0.79335334029124f),   Qfmt_31(0.73727733681012f),
    Qfmt_31(0.67559020761566f),   Qfmt_31(0.60876142900872f),   Qfmt_31(0.53729960834682f),
    Qfmt_31(0.46174861323503f),   Qfmt_31(0.38268343236509f),   Qfmt_31(0.30070579950427f),
    Qfmt_31(0.21643961393810f),   Qfmt_31(0.13052619222005f),   Qfmt_31(0.04361938736534f)
};


const int32 short_win[12] =
{
    /*    k=0..11; sin(pi/12*(k+0.5)) */
    Qfmt_31(0.13052619222005f),   Qfmt_31(0.38268343236509f),   Qfmt_31(0.60876142900872f),
    Qfmt_31(0.79335334029124f),   Qfmt_31(0.92387953251129f),   Qfmt_31(0.99144486137381f),
    Qfmt_31(0.99144486137381f),   Qfmt_31(0.92387953251129f),   Qfmt_31(0.79335334029124f),
    Qfmt_31(0.60876142900872f),   Qfmt_31(0.38268343236509f),   Qfmt_31(0.13052619222005f),
};
/*----------------------------------------------------------------------------
; 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
----------------------------------------------------------------------------*/

void pvmp3_imdct_synth(int32  in[SUBBANDS_NUMBER*FILTERBANK_BANDS],
                       int32  overlap[SUBBANDS_NUMBER*FILTERBANK_BANDS],
                       uint32 blk_type,
                       int16  mx_band,
                       int32  used_freq_lines,
                       int32  *Scratch_mem)
{

    int32 band;
    int32 bands2process = used_freq_lines + 2;

    if (bands2process > SUBBANDS_NUMBER)
    {
        bands2process = SUBBANDS_NUMBER;  /* default */
    }


    /*
     *  in case of mx_poly_band> 0, do
     *  long transforms
     */


    for (band = 0; band < bands2process; band++)
    {
        uint32 current_blk_type = (band < mx_band) ? LONG : blk_type;

        int32 * out     = in      + (band * FILTERBANK_BANDS);
        int32 * history = overlap + (band * FILTERBANK_BANDS);

        switch (current_blk_type)
        {
            case LONG:

                pvmp3_mdct_18(out, history, normal_win);

                break;

            case START:

                pvmp3_mdct_18(out, history, start_win);

                break;

            case STOP:

                pvmp3_mdct_18(out, history, stop_win);

                break;

            case SHORT:
            {
                int32 *tmp_prev_ovr = &Scratch_mem[FILTERBANK_BANDS];
                int32 i;

                for (i = 0; i < 6; i++)
                {
                    Scratch_mem[i    ] = out[(i*3)];
                    Scratch_mem[6  +i] = out[(i*3) + 1];
                    Scratch_mem[12 +i] = out[(i*3) + 2];
                }

                pvmp3_mdct_6(&Scratch_mem[ 0], &tmp_prev_ovr[ 0]);
                pvmp3_mdct_6(&Scratch_mem[ 6], &tmp_prev_ovr[ 6]);
                pvmp3_mdct_6(&Scratch_mem[12], &tmp_prev_ovr[12]);

                for (i = 0; i < 6; i++)
                {
                    int32 temp  =  history[i];
                    /* next iteration overlap */
                    history[i]  =  fxp_mul32_Q32(tmp_prev_ovr[ 6+i] << 1, short_win[6+i]);
                    history[i] +=  fxp_mul32_Q32(Scratch_mem[12+i] << 1, short_win[  i]);
                    out[i]  =  temp;
                }

                for (i = 0; i < 6; i++)
                {
                    out[i+6]   =  fxp_mul32_Q32(Scratch_mem[i] << 1, short_win[i]);
                    out[i+6]  +=  history[i+6];
                    /* next iteration overlap */
                    history[i+6]  =  fxp_mul32_Q32(tmp_prev_ovr[12+i] << 1, short_win[6+i]);

                }
                for (i = 0; i < 6; i++)
                {
                    out[i+12]  =  fxp_mul32_Q32(tmp_prev_ovr[  i] << 1, short_win[6+i]);
                    out[i+12] +=  fxp_mul32_Q32(Scratch_mem[6+i] << 1, short_win[  i]);
                    out[i+12] +=  history[i+12];
                    history[12+i]  =  0;
                }
            }

            break;
        }

        /*
         *     Compensation for frequency inversion of polyphase filterbank
         *     every odd time sample of every odd odd subband is mulitplied by -1  before
         *     processing by the polyphase filter
         */

        if (band & 1)
        {
            for (int32 slot = 1; slot < FILTERBANK_BANDS; slot += 6)
            {
                int32 temp1 = out[slot  ];
                int32 temp2 = out[slot+2];
                int32 temp3 = out[slot+4];
                out[slot  ] = -temp1;
                out[slot+2] = -temp2;
                out[slot+4] = -temp3;
            }
        }
    }


    for (band = bands2process; band < SUBBANDS_NUMBER; band++)
    {
        int32 * out     = in      + (band * FILTERBANK_BANDS);
        int32 * history = overlap + (band * FILTERBANK_BANDS);
        int32 slot;

        if (band & 1)
        {
            for (slot = 0; slot < FILTERBANK_BANDS; slot += 6)
            {
                int32 temp1 =  history[slot  ];
                int32 temp2 =  history[slot+1];
                int32 temp3 =  history[slot+2];
                out[slot  ] =  temp1;
                out[slot+1] = -temp2;
                out[slot+2] =  temp3;

                temp1 =  history[slot+3];
                temp2 =  history[slot+4];
                temp3 =  history[slot+5];
                out[slot+3] = -temp1;
                out[slot+4] =  temp2;
                out[slot+5] = -temp3;
            }
        }
        else
        {
            for (slot = 0; slot < FILTERBANK_BANDS; slot += 3)
            {
                int32 temp1 =  history[slot  ];
                int32 temp2 =  history[slot+1];
                int32 temp3 =  history[slot+2];
                out[slot  ] =  temp1;
                out[slot+1] =  temp2;
                out[slot+2] =  temp3;
            }
        }

        pv_memset(history, 0, FILTERBANK_BANDS*sizeof(*overlap));
    }
}