Coverage Report

Created: 2025-08-29 06:11

/proc/self/cwd/libfaad/output.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: output.c,v 1.47 2009/01/26 23:51:15 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include "output.h"
35
36
#ifndef FIXED_POINT
37
38
39
12.3M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.31M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
4.62M
#define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
43
44
45
static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
46
                                uint8_t down_matrix, uint8_t *internal_channel)
47
41.6M
{
48
41.6M
    if (!down_matrix)
49
39.3M
        return input[internal_channel[channel]][sample];
50
51
2.31M
    if (channel == 0)
52
1.14M
    {
53
1.14M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.14M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.14M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.17M
    } else {
57
1.17M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.17M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.17M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.17M
    }
61
2.31M
}
62
63
#ifndef HAS_LRINTF
64
36.6M
#define CLIP(sample, max, min) \
65
36.6M
if (sample >= 0.0f)            \
66
31.4M
{                              \
67
31.4M
    sample += 0.5f;            \
68
31.4M
    if (sample >= max)         \
69
31.4M
        sample = max;          \
70
31.4M
} else {                       \
71
5.23M
    sample += -0.5f;           \
72
5.23M
    if (sample <= min)         \
73
5.23M
        sample = min;          \
74
5.23M
}
75
#else
76
#define CLIP(sample, max, min) \
77
if (sample >= 0.0f)            \
78
{                              \
79
    if (sample >= max)         \
80
        sample = max;          \
81
} else {                       \
82
    if (sample <= min)         \
83
        sample = min;          \
84
}
85
#endif
86
87
7.89k
#define CONV(a,b) ((a<<1)|(b&0x1))
88
89
static void to_PCM_16bit(NeAACDecStruct *hDecoder, real_t **input,
90
                         uint8_t channels, uint16_t frame_len,
91
                         int16_t **sample_buffer)
92
1.81k
{
93
1.81k
    uint8_t ch, ch1;
94
1.81k
    uint16_t i;
95
96
1.81k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.81k
    {
98
0
    case CONV(1,0):
99
0
    case CONV(1,1):
100
0
        for(i = 0; i < frame_len; i++)
101
0
        {
102
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
103
104
0
            CLIP(inp, 32767.0f, -32768.0f);
105
106
0
            (*sample_buffer)[i] = (int16_t)lrintf(inp);
107
0
        }
108
0
        break;
109
906
    case CONV(2,0):
110
906
        if (hDecoder->upMatrix)
111
60
        {
112
60
            ch  = hDecoder->internal_channel[0];
113
96.0k
            for(i = 0; i < frame_len; i++)
114
96.0k
            {
115
96.0k
                real_t inp0 = input[ch][i];
116
117
96.0k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
96.0k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
96.0k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
96.0k
            }
122
846
        } else {
123
846
            ch  = hDecoder->internal_channel[0];
124
846
            ch1 = hDecoder->internal_channel[1];
125
1.31M
            for(i = 0; i < frame_len; i++)
126
1.31M
            {
127
1.31M
                real_t inp0 = input[ch ][i];
128
1.31M
                real_t inp1 = input[ch1][i];
129
130
1.31M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.31M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.31M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.31M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.31M
            }
136
846
        }
137
906
        break;
138
910
    default:
139
10.8k
        for (ch = 0; ch < channels; ch++)
140
9.94k
        {
141
14.2M
            for(i = 0; i < frame_len; i++)
142
14.2M
            {
143
14.2M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
14.2M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
14.2M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
14.2M
            }
149
9.94k
        }
150
910
        break;
151
1.81k
    }
152
1.81k
}
153
154
static void to_PCM_24bit(NeAACDecStruct *hDecoder, real_t **input,
155
                         uint8_t channels, uint16_t frame_len,
156
                         int32_t **sample_buffer)
157
1.22k
{
158
1.22k
    uint8_t ch, ch1;
159
1.22k
    uint16_t i;
160
161
1.22k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.22k
    {
163
0
    case CONV(1,0):
164
0
    case CONV(1,1):
165
0
        for(i = 0; i < frame_len; i++)
166
0
        {
167
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
168
169
0
            inp *= 256.0f;
170
0
            CLIP(inp, 8388607.0f, -8388608.0f);
171
172
0
            (*sample_buffer)[i] = (int32_t)lrintf(inp);
173
0
        }
174
0
        break;
175
536
    case CONV(2,0):
176
536
        if (hDecoder->upMatrix)
177
35
        {
178
35
            ch = hDecoder->internal_channel[0];
179
52.8k
            for(i = 0; i < frame_len; i++)
180
52.8k
            {
181
52.8k
                real_t inp0 = input[ch][i];
182
183
52.8k
                inp0 *= 256.0f;
184
52.8k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
52.8k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
52.8k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
52.8k
            }
189
501
        } else {
190
501
            ch  = hDecoder->internal_channel[0];
191
501
            ch1 = hDecoder->internal_channel[1];
192
755k
            for(i = 0; i < frame_len; i++)
193
755k
            {
194
755k
                real_t inp0 = input[ch ][i];
195
755k
                real_t inp1 = input[ch1][i];
196
197
755k
                inp0 *= 256.0f;
198
755k
                inp1 *= 256.0f;
199
755k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
755k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
755k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
755k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
755k
            }
205
501
        }
206
536
        break;
207
690
    default:
208
7.01k
        for (ch = 0; ch < channels; ch++)
209
6.32k
        {
210
9.04M
            for(i = 0; i < frame_len; i++)
211
9.04M
            {
212
9.04M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
9.04M
                inp *= 256.0f;
215
9.04M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
9.04M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
9.04M
            }
219
6.32k
        }
220
690
        break;
221
1.22k
    }
222
1.22k
}
223
224
static void to_PCM_32bit(NeAACDecStruct *hDecoder, real_t **input,
225
                         uint8_t channels, uint16_t frame_len,
226
                         int32_t **sample_buffer)
227
1.24k
{
228
1.24k
    uint8_t ch, ch1;
229
1.24k
    uint16_t i;
230
231
1.24k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.24k
    {
233
0
    case CONV(1,0):
234
0
    case CONV(1,1):
235
0
        for(i = 0; i < frame_len; i++)
236
0
        {
237
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
238
239
0
            inp *= 65536.0f;
240
0
            CLIP(inp, 2147483647.0f, -2147483648.0f);
241
242
0
            (*sample_buffer)[i] = (int32_t)lrintf(inp);
243
0
        }
244
0
        break;
245
704
    case CONV(2,0):
246
704
        if (hDecoder->upMatrix)
247
42
        {
248
42
            ch = hDecoder->internal_channel[0];
249
66.4k
            for(i = 0; i < frame_len; i++)
250
66.4k
            {
251
66.4k
                real_t inp0 = input[ch][i];
252
253
66.4k
                inp0 *= 65536.0f;
254
66.4k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
66.4k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
66.4k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
66.4k
            }
259
662
        } else {
260
662
            ch  = hDecoder->internal_channel[0];
261
662
            ch1 = hDecoder->internal_channel[1];
262
1.05M
            for(i = 0; i < frame_len; i++)
263
1.05M
            {
264
1.05M
                real_t inp0 = input[ch ][i];
265
1.05M
                real_t inp1 = input[ch1][i];
266
267
1.05M
                inp0 *= 65536.0f;
268
1.05M
                inp1 *= 65536.0f;
269
1.05M
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
1.05M
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
1.05M
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
1.05M
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
1.05M
            }
275
662
        }
276
704
        break;
277
543
    default:
278
5.38k
        for (ch = 0; ch < channels; ch++)
279
4.84k
        {
280
6.92M
            for(i = 0; i < frame_len; i++)
281
6.92M
            {
282
6.92M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
6.92M
                inp *= 65536.0f;
285
6.92M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
6.92M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
6.92M
            }
289
4.84k
        }
290
543
        break;
291
1.24k
    }
292
1.24k
}
293
294
static void to_PCM_float(NeAACDecStruct *hDecoder, real_t **input,
295
                         uint8_t channels, uint16_t frame_len,
296
                         float32_t **sample_buffer)
297
667
{
298
667
    uint8_t ch, ch1;
299
667
    uint16_t i;
300
301
667
    switch (CONV(channels,hDecoder->downMatrix))
302
667
    {
303
0
    case CONV(1,0):
304
0
    case CONV(1,1):
305
0
        for(i = 0; i < frame_len; i++)
306
0
        {
307
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
308
0
            (*sample_buffer)[i] = inp*FLOAT_SCALE;
309
0
        }
310
0
        break;
311
174
    case CONV(2,0):
312
174
        if (hDecoder->upMatrix)
313
17
        {
314
17
            ch = hDecoder->internal_channel[0];
315
28.2k
            for(i = 0; i < frame_len; i++)
316
28.2k
            {
317
28.2k
                real_t inp0 = input[ch][i];
318
28.2k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
28.2k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
28.2k
            }
321
157
        } else {
322
157
            ch  = hDecoder->internal_channel[0];
323
157
            ch1 = hDecoder->internal_channel[1];
324
256k
            for(i = 0; i < frame_len; i++)
325
256k
            {
326
256k
                real_t inp0 = input[ch ][i];
327
256k
                real_t inp1 = input[ch1][i];
328
256k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
256k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
256k
            }
331
157
        }
332
174
        break;
333
493
    default:
334
4.57k
        for (ch = 0; ch < channels; ch++)
335
4.07k
        {
336
5.70M
            for(i = 0; i < frame_len; i++)
337
5.70M
            {
338
5.70M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
5.70M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
5.70M
            }
341
4.07k
        }
342
493
        break;
343
667
    }
344
667
}
345
346
static void to_PCM_double(NeAACDecStruct *hDecoder, real_t **input,
347
                          uint8_t channels, uint16_t frame_len,
348
                          double **sample_buffer)
349
485
{
350
485
    uint8_t ch, ch1;
351
485
    uint16_t i;
352
353
485
    switch (CONV(channels,hDecoder->downMatrix))
354
485
    {
355
0
    case CONV(1,0):
356
0
    case CONV(1,1):
357
0
        for(i = 0; i < frame_len; i++)
358
0
        {
359
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
360
0
            (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
361
0
        }
362
0
        break;
363
133
    case CONV(2,0):
364
133
        if (hDecoder->upMatrix)
365
21
        {
366
21
            ch = hDecoder->internal_channel[0];
367
30.4k
            for(i = 0; i < frame_len; i++)
368
30.4k
            {
369
30.4k
                real_t inp0 = input[ch][i];
370
30.4k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
30.4k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
30.4k
            }
373
112
        } else {
374
112
            ch  = hDecoder->internal_channel[0];
375
112
            ch1 = hDecoder->internal_channel[1];
376
135k
            for(i = 0; i < frame_len; i++)
377
135k
            {
378
135k
                real_t inp0 = input[ch ][i];
379
135k
                real_t inp1 = input[ch1][i];
380
135k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
135k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
135k
            }
383
112
        }
384
133
        break;
385
352
    default:
386
4.28k
        for (ch = 0; ch < channels; ch++)
387
3.93k
        {
388
5.77M
            for(i = 0; i < frame_len; i++)
389
5.77M
            {
390
5.77M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
5.77M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
5.77M
            }
393
3.93k
        }
394
352
        break;
395
485
    }
396
485
}
397
398
void *output_to_PCM(NeAACDecStruct *hDecoder,
399
                    real_t **input, void *sample_buffer, uint8_t channels,
400
                    uint16_t frame_len, uint8_t format)
401
10.8k
{
402
10.8k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
10.8k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
10.8k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
10.8k
    double    *double_sample_buffer = (double*)sample_buffer;
406
407
#ifdef PROFILE
408
    int64_t count = faad_get_ts();
409
#endif
410
411
    /* Copy output to a standard PCM buffer */
412
10.8k
    switch (format)
413
10.8k
    {
414
3.63k
    case FAAD_FMT_16BIT:
415
3.63k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.63k
        break;
417
2.45k
    case FAAD_FMT_24BIT:
418
2.45k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.45k
        break;
420
2.49k
    case FAAD_FMT_32BIT:
421
2.49k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.49k
        break;
423
1.33k
    case FAAD_FMT_FLOAT:
424
1.33k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.33k
        break;
426
970
    case FAAD_FMT_DOUBLE:
427
970
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
970
        break;
429
10.8k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
10.8k
    return sample_buffer;
437
10.8k
}
output_to_PCM
Line
Count
Source
401
5.44k
{
402
5.44k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.44k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.44k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.44k
    double    *double_sample_buffer = (double*)sample_buffer;
406
407
#ifdef PROFILE
408
    int64_t count = faad_get_ts();
409
#endif
410
411
    /* Copy output to a standard PCM buffer */
412
5.44k
    switch (format)
413
5.44k
    {
414
1.81k
    case FAAD_FMT_16BIT:
415
1.81k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.81k
        break;
417
1.22k
    case FAAD_FMT_24BIT:
418
1.22k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.22k
        break;
420
1.24k
    case FAAD_FMT_32BIT:
421
1.24k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.24k
        break;
423
667
    case FAAD_FMT_FLOAT:
424
667
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
667
        break;
426
485
    case FAAD_FMT_DOUBLE:
427
485
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
485
        break;
429
5.44k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.44k
    return sample_buffer;
437
5.44k
}
output_to_PCM
Line
Count
Source
401
5.44k
{
402
5.44k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.44k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.44k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.44k
    double    *double_sample_buffer = (double*)sample_buffer;
406
407
#ifdef PROFILE
408
    int64_t count = faad_get_ts();
409
#endif
410
411
    /* Copy output to a standard PCM buffer */
412
5.44k
    switch (format)
413
5.44k
    {
414
1.81k
    case FAAD_FMT_16BIT:
415
1.81k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.81k
        break;
417
1.22k
    case FAAD_FMT_24BIT:
418
1.22k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.22k
        break;
420
1.24k
    case FAAD_FMT_32BIT:
421
1.24k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.24k
        break;
423
667
    case FAAD_FMT_FLOAT:
424
667
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
667
        break;
426
485
    case FAAD_FMT_DOUBLE:
427
485
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
485
        break;
429
5.44k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.44k
    return sample_buffer;
437
5.44k
}
438
439
#else
440
441
#define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
442
#define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
443
#define BOTH FRAC_CONST(0.2265409196609864215998) // 1/(sqrt(2) + 2 + 1)
444
445
static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
446
                                uint8_t down_matrix, uint8_t up_matrix,
447
                                uint8_t *internal_channel)
448
22.8M
{
449
22.8M
    real_t C;
450
22.8M
    if (up_matrix == 1)
451
226k
        return input[internal_channel[0]][sample];
452
453
22.6M
    if (!down_matrix)
454
22.3M
        return input[internal_channel[channel]][sample];
455
456
334k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
334k
    if (channel == 0)
458
157k
    {
459
157k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
157k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
157k
        return core + C + L_S;
462
176k
    } else {
463
176k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
176k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
176k
        return core + C + R_S;
466
176k
    }
467
334k
}
468
469
void* output_to_PCM(NeAACDecStruct *hDecoder,
470
                    real_t **input, void *sample_buffer, uint8_t channels,
471
                    uint16_t frame_len, uint8_t format)
472
4.83k
{
473
4.83k
    uint8_t ch;
474
4.83k
    uint16_t i;
475
4.83k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
4.83k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
4.83k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
36.5k
    for (ch = 0; ch < channels; ch++)
481
31.6k
    {
482
31.6k
        switch (format)
483
31.6k
        {
484
9.21k
        case FAAD_FMT_16BIT:
485
13.3M
            for(i = 0; i < frame_len; i++)
486
13.3M
            {
487
13.3M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
13.3M
                    hDecoder->internal_channel);
489
13.3M
                if (tmp >= 0)
490
12.4M
                {
491
12.4M
                    tmp += (1 << (REAL_BITS-1));
492
12.4M
                    if (tmp >= REAL_CONST(32767))
493
20.4k
                    {
494
20.4k
                        tmp = REAL_CONST(32767);
495
20.4k
                    }
496
12.4M
                } else {
497
839k
                    tmp += -(1 << (REAL_BITS-1));
498
839k
                    if (tmp <= REAL_CONST(-32768))
499
20.5k
                    {
500
20.5k
                        tmp = REAL_CONST(-32768);
501
20.5k
                    }
502
839k
                }
503
13.3M
                tmp >>= REAL_BITS;
504
13.3M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
13.3M
            }
506
9.21k
            break;
507
10.5k
        case FAAD_FMT_24BIT:
508
15.1M
            for(i = 0; i < frame_len; i++)
509
15.1M
            {
510
15.1M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
15.1M
                    hDecoder->internal_channel);
512
15.1M
                if (tmp >= 0)
513
13.6M
                {
514
13.6M
                    tmp += (1 << (REAL_BITS-9));
515
13.6M
                    tmp >>= (REAL_BITS-8);
516
13.6M
                    if (tmp >= 8388607)
517
20.5k
                    {
518
20.5k
                        tmp = 8388607;
519
20.5k
                    }
520
13.6M
                } else {
521
1.45M
                    tmp += -(1 << (REAL_BITS-9));
522
1.45M
                    tmp >>= (REAL_BITS-8);
523
1.45M
                    if (tmp <= -8388608)
524
19.4k
                    {
525
19.4k
                        tmp = -8388608;
526
19.4k
                    }
527
1.45M
                }
528
15.1M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
15.1M
            }
530
10.5k
            break;
531
6.95k
        case FAAD_FMT_32BIT:
532
6.95k
            exp = 16 - REAL_BITS;
533
6.95k
            half = 1 << (exp - 1);
534
6.95k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
10.0M
            for(i = 0; i < frame_len; i++)
536
10.0M
            {
537
10.0M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
10.0M
                    hDecoder->internal_channel);
539
10.0M
                if (tmp >= 0)
540
9.09M
                {
541
9.09M
                    tmp += half;
542
9.09M
                } else {
543
947k
                    tmp += -half;
544
947k
                }
545
10.0M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
10.0M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
10.0M
            }
548
6.95k
            break;
549
5.00k
        case FAAD_FMT_FIXED:
550
7.27M
            for(i = 0; i < frame_len; i++)
551
7.26M
            {
552
7.26M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
7.26M
                    hDecoder->internal_channel);
554
7.26M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
7.26M
            }
556
5.00k
            break;
557
31.6k
        }
558
31.6k
    }
559
560
4.83k
    return sample_buffer;
561
4.83k
}
output_to_PCM
Line
Count
Source
472
2.41k
{
473
2.41k
    uint8_t ch;
474
2.41k
    uint16_t i;
475
2.41k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.41k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.41k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
18.2k
    for (ch = 0; ch < channels; ch++)
481
15.8k
    {
482
15.8k
        switch (format)
483
15.8k
        {
484
4.60k
        case FAAD_FMT_16BIT:
485
6.65M
            for(i = 0; i < frame_len; i++)
486
6.65M
            {
487
6.65M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
6.65M
                    hDecoder->internal_channel);
489
6.65M
                if (tmp >= 0)
490
6.23M
                {
491
6.23M
                    tmp += (1 << (REAL_BITS-1));
492
6.23M
                    if (tmp >= REAL_CONST(32767))
493
10.2k
                    {
494
10.2k
                        tmp = REAL_CONST(32767);
495
10.2k
                    }
496
6.23M
                } else {
497
419k
                    tmp += -(1 << (REAL_BITS-1));
498
419k
                    if (tmp <= REAL_CONST(-32768))
499
10.2k
                    {
500
10.2k
                        tmp = REAL_CONST(-32768);
501
10.2k
                    }
502
419k
                }
503
6.65M
                tmp >>= REAL_BITS;
504
6.65M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
6.65M
            }
506
4.60k
            break;
507
5.25k
        case FAAD_FMT_24BIT:
508
7.57M
            for(i = 0; i < frame_len; i++)
509
7.56M
            {
510
7.56M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
7.56M
                    hDecoder->internal_channel);
512
7.56M
                if (tmp >= 0)
513
6.83M
                {
514
6.83M
                    tmp += (1 << (REAL_BITS-9));
515
6.83M
                    tmp >>= (REAL_BITS-8);
516
6.83M
                    if (tmp >= 8388607)
517
10.2k
                    {
518
10.2k
                        tmp = 8388607;
519
10.2k
                    }
520
6.83M
                } else {
521
729k
                    tmp += -(1 << (REAL_BITS-9));
522
729k
                    tmp >>= (REAL_BITS-8);
523
729k
                    if (tmp <= -8388608)
524
9.73k
                    {
525
9.73k
                        tmp = -8388608;
526
9.73k
                    }
527
729k
                }
528
7.56M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
7.56M
            }
530
5.25k
            break;
531
3.47k
        case FAAD_FMT_32BIT:
532
3.47k
            exp = 16 - REAL_BITS;
533
3.47k
            half = 1 << (exp - 1);
534
3.47k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
5.02M
            for(i = 0; i < frame_len; i++)
536
5.02M
            {
537
5.02M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
5.02M
                    hDecoder->internal_channel);
539
5.02M
                if (tmp >= 0)
540
4.54M
                {
541
4.54M
                    tmp += half;
542
4.54M
                } else {
543
473k
                    tmp += -half;
544
473k
                }
545
5.02M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
5.02M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
5.02M
            }
548
3.47k
            break;
549
2.50k
        case FAAD_FMT_FIXED:
550
3.63M
            for(i = 0; i < frame_len; i++)
551
3.63M
            {
552
3.63M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
3.63M
                    hDecoder->internal_channel);
554
3.63M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
3.63M
            }
556
2.50k
            break;
557
15.8k
        }
558
15.8k
    }
559
560
2.41k
    return sample_buffer;
561
2.41k
}
output_to_PCM
Line
Count
Source
472
2.41k
{
473
2.41k
    uint8_t ch;
474
2.41k
    uint16_t i;
475
2.41k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.41k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.41k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
18.2k
    for (ch = 0; ch < channels; ch++)
481
15.8k
    {
482
15.8k
        switch (format)
483
15.8k
        {
484
4.60k
        case FAAD_FMT_16BIT:
485
6.65M
            for(i = 0; i < frame_len; i++)
486
6.65M
            {
487
6.65M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
6.65M
                    hDecoder->internal_channel);
489
6.65M
                if (tmp >= 0)
490
6.23M
                {
491
6.23M
                    tmp += (1 << (REAL_BITS-1));
492
6.23M
                    if (tmp >= REAL_CONST(32767))
493
10.2k
                    {
494
10.2k
                        tmp = REAL_CONST(32767);
495
10.2k
                    }
496
6.23M
                } else {
497
419k
                    tmp += -(1 << (REAL_BITS-1));
498
419k
                    if (tmp <= REAL_CONST(-32768))
499
10.2k
                    {
500
10.2k
                        tmp = REAL_CONST(-32768);
501
10.2k
                    }
502
419k
                }
503
6.65M
                tmp >>= REAL_BITS;
504
6.65M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
6.65M
            }
506
4.60k
            break;
507
5.25k
        case FAAD_FMT_24BIT:
508
7.57M
            for(i = 0; i < frame_len; i++)
509
7.56M
            {
510
7.56M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
7.56M
                    hDecoder->internal_channel);
512
7.56M
                if (tmp >= 0)
513
6.83M
                {
514
6.83M
                    tmp += (1 << (REAL_BITS-9));
515
6.83M
                    tmp >>= (REAL_BITS-8);
516
6.83M
                    if (tmp >= 8388607)
517
10.2k
                    {
518
10.2k
                        tmp = 8388607;
519
10.2k
                    }
520
6.83M
                } else {
521
729k
                    tmp += -(1 << (REAL_BITS-9));
522
729k
                    tmp >>= (REAL_BITS-8);
523
729k
                    if (tmp <= -8388608)
524
9.73k
                    {
525
9.73k
                        tmp = -8388608;
526
9.73k
                    }
527
729k
                }
528
7.56M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
7.56M
            }
530
5.25k
            break;
531
3.47k
        case FAAD_FMT_32BIT:
532
3.47k
            exp = 16 - REAL_BITS;
533
3.47k
            half = 1 << (exp - 1);
534
3.47k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
5.02M
            for(i = 0; i < frame_len; i++)
536
5.02M
            {
537
5.02M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
5.02M
                    hDecoder->internal_channel);
539
5.02M
                if (tmp >= 0)
540
4.54M
                {
541
4.54M
                    tmp += half;
542
4.54M
                } else {
543
473k
                    tmp += -half;
544
473k
                }
545
5.02M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
5.02M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
5.02M
            }
548
3.47k
            break;
549
2.50k
        case FAAD_FMT_FIXED:
550
3.63M
            for(i = 0; i < frame_len; i++)
551
3.63M
            {
552
3.63M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
3.63M
                    hDecoder->internal_channel);
554
3.63M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
3.63M
            }
556
2.50k
            break;
557
15.8k
        }
558
15.8k
    }
559
560
2.41k
    return sample_buffer;
561
2.41k
}
562
563
#endif