summaryrefslogtreecommitdiffstats
path: root/libfaad2/sbr_e_nf.c
blob: ba8a6f81bb5484d04cce0f0d84b82c18c620d919 (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
/*
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
** Any non-GPL usage of this software or parts of this software is strictly
** forbidden.
**
** Commercial non-GPL licensing of this software is possible.
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
**
** $Id: sbr_e_nf.c,v 1.1 2003/07/29 08:20:13 menno Exp $
**/

#include "common.h"
#include "structs.h"

#ifdef SBR_DEC

#include <stdlib.h>

#include "sbr_syntax.h"
#include "sbr_e_nf.h"

void extract_envelope_data(sbr_info *sbr, uint8_t ch)
{
    uint8_t l, k;

#if 0
    if (sbr->frame == 19)
    {
        sbr->frame = 19;
    }
#endif

    for (l = 0; l < sbr->L_E[ch]; l++)
    {
        if (sbr->bs_df_env[ch][l] == 0)
        {
            for (k = 1; k < sbr->n[sbr->f[ch][l]]; k++)
            {
                sbr->E[ch][k][l] = sbr->E[ch][k - 1][l] + sbr->E[ch][k][l];
            }

        } else { /* bs_df_env == 1 */

            uint8_t g = (l == 0) ? sbr->f_prev[ch] : sbr->f[ch][l-1];
            int16_t E_prev;

            if (sbr->f[ch][l] == g)
            {
                for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
                {
                    if (l == 0)
                        E_prev = sbr->E_prev[ch][k];
                    else
                        E_prev = sbr->E[ch][k][l - 1];

                    sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
                }

            } else if ((g == 1) && (sbr->f[ch][l] == 0)) {
                uint8_t i;

                for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
                {
                    for (i = 0; i < sbr->N_high; i++)
                    {
                        if (sbr->f_table_res[HI_RES][i] == sbr->f_table_res[LO_RES][k])
                        {
                            if (l == 0)
                                E_prev = sbr->E_prev[ch][i];
                            else
                                E_prev = sbr->E[ch][i][l - 1];

                            sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
                        }
                    }
                }

            } else if ((g == 0) && (sbr->f[ch][l] == 1)) {
                uint8_t i;

                for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
                {
                    for (i = 0; i < sbr->N_low; i++)
                    {
                        if ((sbr->f_table_res[LO_RES][i] <= sbr->f_table_res[HI_RES][k]) &&
                            (sbr->f_table_res[HI_RES][k] < sbr->f_table_res[LO_RES][i + 1]))
                        {
                            if (l == 0)
                                E_prev = sbr->E_prev[ch][i];
                            else
                                E_prev = sbr->E[ch][i][l - 1];

                            sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
                        }
                    }
                }
            }
        }
    }

#if 0
    if (sbr->frame == 23)
    {
        int l, k;

        for (l = 0; l < sbr->L_E[ch]; l++)
        {
            for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
            {
                //printf("l:%d k:%d E:%d\n",l,k, sbr->E[ch][k][l]);
                printf("%d\n", sbr->E[ch][k][l]);
            }
        }
        printf("\n");
    }
#endif
}

void extract_noise_floor_data(sbr_info *sbr, uint8_t ch)
{
    uint8_t l, k;

    for (l = 0; l < sbr->L_Q[ch]; l++)
    {
        if (sbr->bs_df_noise[ch][l] == 0)
        {
            for (k = 1; k < sbr->N_Q; k++)
            {
                sbr->Q[ch][k][l] = sbr->Q[ch][k][l] + sbr->Q[ch][k-1][l];
            }
        } else {
            if (l == 0)
            {
                for (k = 0; k < sbr->N_Q; k++)
                {
                    sbr->Q[ch][k][l] = sbr->Q_prev[ch][k] + sbr->Q[ch][k][0];
                }
            } else {
                for (k = 0; k < sbr->N_Q; k++)
                {
                    sbr->Q[ch][k][l] = sbr->Q[ch][k][l - 1] + sbr->Q[ch][k][l];
                }
            }
        }
    }

#if 0
    if (sbr->frame == 23)
    {
        int l, k;

        for (l = 0; l < sbr->L_Q[ch]; l++)
        {
            for (k = 0; k < sbr->N_Q; k++)
            {
                //printf("l:%d k:%d E:%d\n",l,k, sbr->E[ch][k][l]);
                printf("%d\n", sbr->Q[ch][k][l]);
            }
        }
        printf("\n");
    }
#endif
}

/* FIXME: pow() not needed */
void envelope_noise_dequantisation(sbr_info *sbr, uint8_t ch)
{
    if (sbr->bs_coupling == 0)
    {
        uint8_t l, k;
#ifdef FIXED_POINT
        uint8_t amp = (sbr->amp_res[ch]) ? 0 : 1;
#else
        real_t amp = (sbr->amp_res[ch]) ? 1.0 : 0.5;
#endif

        for (l = 0; l < sbr->L_E[ch]; l++)
        {
            for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
            {
#ifndef FIXED_POINT
                /* +6 for the *64 */
                sbr->E_orig[ch][k][l] = pow(2, sbr->E[ch][k][l]*amp + 6);
#else
                int8_t exp;

                /* +6 for the *64 and -10 for the /32 in the synthesis QMF
                 * since this is a energy value: (x/32)^2 = (x^2)/1024
                 */
                exp = (sbr->E[ch][k][l] >> amp) + 6 - 10;

//                printf("%d\n", exp);

                if (exp < 0)
                    sbr->E_orig[ch][k][l] = 0; //REAL_CONST(1) >> -exp;
                else
                    sbr->E_orig[ch][k][l] = 1 << exp; //REAL_CONST(1) << exp;

                if (amp && (sbr->E[ch][k][l] & 1))
                    sbr->E_orig[ch][k][l] = MUL(sbr->E_orig[ch][k][l], REAL_CONST(1.414213562));
#endif
            }
        }

        for (l = 0; l < sbr->L_Q[ch]; l++)
        {
            for (k = 0; k < sbr->N_Q; k++)
            {
                if (sbr->Q[ch][k][l] < 0 || sbr->Q[ch][k][l] > 30)
                    sbr->Q_orig[ch][k][l] = 0;
                else {
#ifndef FIXED_POINT
                    sbr->Q_orig[ch][k][l] = pow(2, NOISE_FLOOR_OFFSET - sbr->Q[ch][k][l]);
#else
                    int8_t exp = NOISE_FLOOR_OFFSET - sbr->Q[ch][k][l];
                    if (exp < 0)
                        sbr->Q_orig[ch][k][l] = REAL_CONST(1) >> -exp;
                    else
                        sbr->Q_orig[ch][k][l] = REAL_CONST(1) << exp;
#endif
                }
            }
        }
    }
}

void unmap_envelope_noise(sbr_info *sbr)
{
    uint8_t l, k;
#ifdef FIXED_POINT
    uint8_t amp0 = (sbr->amp_res[0]) ? 0 : 1;
    uint8_t amp1 = (sbr->amp_res[1]) ? 0 : 1;
#else
    real_t amp0 = (sbr->amp_res[0]) ? 1.0 : 0.5;
    real_t amp1 = (sbr->amp_res[1]) ? 1.0 : 0.5;
#endif

    for (l = 0; l < sbr->L_E[0]; l++)
    {
        for (k = 0; k < sbr->n[sbr->f[0][l]]; k++)
        {
            real_t l_temp, r_temp;

#ifdef FIXED_POINT
            int8_t exp;

            /* +6: * 64 ; +1: * 2 ; -10: /1024 QMF */
            exp = (sbr->E[0][k][l] >> amp0) - 3;

//            printf("%d\n", exp);

            if (exp < 0)
                l_temp = REAL_CONST(1) >> -exp;
            else
                l_temp = REAL_CONST(1) << exp;

            if (amp0 && (sbr->E[0][k][l] & 1))
                l_temp = MUL(l_temp, REAL_CONST(1.414213562373095));

            /* UN_MAP removed: (x / 4096) same as (x >> 12) */
            exp = (sbr->E[1][k][l] >> amp1) - 12;

//            printf("%d\n", exp);

            if (exp < 0)
                r_temp = REAL_CONST(1) >> -exp;
            else
                r_temp = REAL_CONST(1) << exp;

            if (amp1 && (sbr->E[1][k][l] & 1))
                r_temp = MUL(r_temp, REAL_CONST(1.414213562373095));
#else
            /* +6: * 64 ; +1: * 2 */
            l_temp = pow(2, sbr->E[0][k][l]*amp0 + 7);
            /* UN_MAP removed: (x / 4096) same as (x >> 12) */
            r_temp = pow(2, sbr->E[1][k][l]*amp1 - 12);
#endif


#ifdef FIXED_POINT
            {
                real_t tmp = REAL_CONST(1.0) + r_temp;
                sbr->E_orig[1][k][l] = SBR_DIV(l_temp, tmp);
            }
#else
            sbr->E_orig[1][k][l] = l_temp / (1.0 + r_temp);
#endif
            sbr->E_orig[0][k][l] = MUL(r_temp, sbr->E_orig[1][k][l]);

#ifdef FIXED_POINT
            sbr->E_orig[0][k][l] >>= REAL_BITS;
            sbr->E_orig[1][k][l] >>= REAL_BITS;
#endif

            //printf("%f\t%f\n", sbr->E_orig[0][k][l] /(float)(1<<REAL_BITS), sbr->E_orig[1][k][l] /(float)(1<<REAL_BITS));
            //printf("%f\t%f\n", sbr->E_orig[0][k][l]/1024., sbr->E_orig[1][k][l]/1024.);
        }
    }
    for (l = 0; l < sbr->L_Q[0]; l++)
    {
        for (k = 0; k < sbr->N_Q; k++)
        {
            if ((sbr->Q[0][k][l] < 0 || sbr->Q[0][k][l] > 30) ||
                (sbr->Q[1][k][l] < 0 || sbr->Q[1][k][l] > 30))
            {
                sbr->Q_orig[0][k][l] = 0;
                sbr->Q_orig[1][k][l] = 0;
            } else {
                real_t l_temp, r_temp;

#ifndef FIXED_POINT
                l_temp = pow(2.0, NOISE_FLOOR_OFFSET - sbr->Q[0][k][l] + 1);
                r_temp = pow(2.0, sbr->Q[1][k][l] - 12);
#else
                int8_t exp;

                exp = NOISE_FLOOR_OFFSET - sbr->Q[0][k][l] + 1;
                if (exp < 0)
                    l_temp = REAL_CONST(1) >> -exp;
                else
                    l_temp = REAL_CONST(1) << exp;

                exp = sbr->Q[1][k][l] - 12;
                if (exp < 0)
                    r_temp = REAL_CONST(1) >> -exp;
                else
                    r_temp = REAL_CONST(1) << exp;
#endif

#ifdef FIXED_POINT
                sbr->Q_orig[1][k][l] = SBR_DIV(l_temp, (REAL_CONST(1.0) + r_temp));
#else
                sbr->Q_orig[1][k][l] = l_temp / (1.0 + r_temp);
#endif
                sbr->Q_orig[0][k][l] = MUL(r_temp, sbr->Q_orig[1][k][l]);
            }
        }
    }
}

#endif