Coverage Report

Created: 2026-02-26 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/output.c
Line
Count
Source
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
14.2M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.81M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
5.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
40.7M
{
48
40.7M
    if (!down_matrix)
49
37.9M
        return input[internal_channel[channel]][sample];
50
51
2.81M
    if (channel == 0)
52
1.39M
    {
53
1.39M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.39M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.39M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.42M
    } else {
57
1.42M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.42M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.42M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.42M
    }
61
2.81M
}
62
63
#ifndef HAS_LRINTF
64
33.9M
#define CLIP(sample, max, min) \
65
33.9M
if (sample >= 0.0f)            \
66
29.1M
{                              \
67
29.1M
    sample += 0.5f;            \
68
29.1M
    if (sample >= max)         \
69
29.1M
        sample = max;          \
70
29.1M
} else {                       \
71
4.79M
    sample += -0.5f;           \
72
4.79M
    if (sample <= min)         \
73
4.79M
        sample = min;          \
74
4.79M
}
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
8.27k
#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.84k
{
93
1.84k
    uint8_t ch, ch1;
94
1.84k
    uint16_t i;
95
96
1.84k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.84k
    {
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
934
    case CONV(2,0):
110
934
        if (hDecoder->upMatrix)
111
63
        {
112
63
            ch  = hDecoder->internal_channel[0];
113
100k
            for(i = 0; i < frame_len; i++)
114
100k
            {
115
100k
                real_t inp0 = input[ch][i];
116
117
100k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
100k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
100k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
100k
            }
122
871
        } else {
123
871
            ch  = hDecoder->internal_channel[0];
124
871
            ch1 = hDecoder->internal_channel[1];
125
1.37M
            for(i = 0; i < frame_len; i++)
126
1.37M
            {
127
1.37M
                real_t inp0 = input[ch ][i];
128
1.37M
                real_t inp1 = input[ch1][i];
129
130
1.37M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.37M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.37M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.37M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.37M
            }
136
871
        }
137
934
        break;
138
910
    default:
139
9.23k
        for (ch = 0; ch < channels; ch++)
140
8.32k
        {
141
11.6M
            for(i = 0; i < frame_len; i++)
142
11.6M
            {
143
11.6M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
11.6M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
11.6M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
11.6M
            }
149
8.32k
        }
150
910
        break;
151
1.84k
    }
152
1.84k
}
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.32k
{
158
1.32k
    uint8_t ch, ch1;
159
1.32k
    uint16_t i;
160
161
1.32k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.32k
    {
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
644
    case CONV(2,0):
176
644
        if (hDecoder->upMatrix)
177
35
        {
178
35
            ch = hDecoder->internal_channel[0];
179
54.1k
            for(i = 0; i < frame_len; i++)
180
54.1k
            {
181
54.1k
                real_t inp0 = input[ch][i];
182
183
54.1k
                inp0 *= 256.0f;
184
54.1k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
54.1k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
54.1k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
54.1k
            }
189
609
        } else {
190
609
            ch  = hDecoder->internal_channel[0];
191
609
            ch1 = hDecoder->internal_channel[1];
192
846k
            for(i = 0; i < frame_len; i++)
193
846k
            {
194
846k
                real_t inp0 = input[ch ][i];
195
846k
                real_t inp1 = input[ch1][i];
196
197
846k
                inp0 *= 256.0f;
198
846k
                inp1 *= 256.0f;
199
846k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
846k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
846k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
846k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
846k
            }
205
609
        }
206
644
        break;
207
676
    default:
208
6.78k
        for (ch = 0; ch < channels; ch++)
209
6.10k
        {
210
8.82M
            for(i = 0; i < frame_len; i++)
211
8.82M
            {
212
8.82M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.82M
                inp *= 256.0f;
215
8.82M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.82M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.82M
            }
219
6.10k
        }
220
676
        break;
221
1.32k
    }
222
1.32k
}
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.18k
{
228
1.18k
    uint8_t ch, ch1;
229
1.18k
    uint16_t i;
230
231
1.18k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.18k
    {
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
604
    case CONV(2,0):
246
604
        if (hDecoder->upMatrix)
247
48
        {
248
48
            ch = hDecoder->internal_channel[0];
249
78.6k
            for(i = 0; i < frame_len; i++)
250
78.5k
            {
251
78.5k
                real_t inp0 = input[ch][i];
252
253
78.5k
                inp0 *= 65536.0f;
254
78.5k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
78.5k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
78.5k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
78.5k
            }
259
556
        } else {
260
556
            ch  = hDecoder->internal_channel[0];
261
556
            ch1 = hDecoder->internal_channel[1];
262
877k
            for(i = 0; i < frame_len; i++)
263
876k
            {
264
876k
                real_t inp0 = input[ch ][i];
265
876k
                real_t inp1 = input[ch1][i];
266
267
876k
                inp0 *= 65536.0f;
268
876k
                inp1 *= 65536.0f;
269
876k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
876k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
876k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
876k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
876k
            }
275
556
        }
276
604
        break;
277
581
    default:
278
5.53k
        for (ch = 0; ch < channels; ch++)
279
4.95k
        {
280
7.10M
            for(i = 0; i < frame_len; i++)
281
7.10M
            {
282
7.10M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
7.10M
                inp *= 65536.0f;
285
7.10M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
7.10M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
7.10M
            }
289
4.95k
        }
290
581
        break;
291
1.18k
    }
292
1.18k
}
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
747
{
298
747
    uint8_t ch, ch1;
299
747
    uint16_t i;
300
301
747
    switch (CONV(channels,hDecoder->downMatrix))
302
747
    {
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
226
    case CONV(2,0):
312
226
        if (hDecoder->upMatrix)
313
13
        {
314
13
            ch = hDecoder->internal_channel[0];
315
20.3k
            for(i = 0; i < frame_len; i++)
316
20.3k
            {
317
20.3k
                real_t inp0 = input[ch][i];
318
20.3k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
20.3k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
20.3k
            }
321
213
        } else {
322
213
            ch  = hDecoder->internal_channel[0];
323
213
            ch1 = hDecoder->internal_channel[1];
324
314k
            for(i = 0; i < frame_len; i++)
325
314k
            {
326
314k
                real_t inp0 = input[ch ][i];
327
314k
                real_t inp1 = input[ch1][i];
328
314k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
314k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
314k
            }
331
213
        }
332
226
        break;
333
521
    default:
334
5.00k
        for (ch = 0; ch < channels; ch++)
335
4.48k
        {
336
6.32M
            for(i = 0; i < frame_len; i++)
337
6.32M
            {
338
6.32M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
6.32M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
6.32M
            }
341
4.48k
        }
342
521
        break;
343
747
    }
344
747
}
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
610
{
350
610
    uint8_t ch, ch1;
351
610
    uint16_t i;
352
353
610
    switch (CONV(channels,hDecoder->downMatrix))
354
610
    {
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
162
    case CONV(2,0):
364
162
        if (hDecoder->upMatrix)
365
23
        {
366
23
            ch = hDecoder->internal_channel[0];
367
39.8k
            for(i = 0; i < frame_len; i++)
368
39.8k
            {
369
39.8k
                real_t inp0 = input[ch][i];
370
39.8k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
39.8k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
39.8k
            }
373
139
        } else {
374
139
            ch  = hDecoder->internal_channel[0];
375
139
            ch1 = hDecoder->internal_channel[1];
376
178k
            for(i = 0; i < frame_len; i++)
377
178k
            {
378
178k
                real_t inp0 = input[ch ][i];
379
178k
                real_t inp1 = input[ch1][i];
380
178k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
178k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
178k
            }
383
139
        }
384
162
        break;
385
448
    default:
386
4.85k
        for (ch = 0; ch < channels; ch++)
387
4.40k
        {
388
6.84M
            for(i = 0; i < frame_len; i++)
389
6.84M
            {
390
6.84M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
6.84M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
6.84M
            }
393
4.40k
        }
394
448
        break;
395
610
    }
396
610
}
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
11.4k
{
402
11.4k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
11.4k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
11.4k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
11.4k
    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
11.4k
    switch (format)
413
11.4k
    {
414
3.68k
    case FAAD_FMT_16BIT:
415
3.68k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.68k
        break;
417
2.64k
    case FAAD_FMT_24BIT:
418
2.64k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.64k
        break;
420
2.37k
    case FAAD_FMT_32BIT:
421
2.37k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.37k
        break;
423
1.49k
    case FAAD_FMT_FLOAT:
424
1.49k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.49k
        break;
426
1.22k
    case FAAD_FMT_DOUBLE:
427
1.22k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.22k
        break;
429
11.4k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
11.4k
    return sample_buffer;
437
11.4k
}
output_to_PCM
Line
Count
Source
401
5.70k
{
402
5.70k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.70k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.70k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.70k
    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.70k
    switch (format)
413
5.70k
    {
414
1.84k
    case FAAD_FMT_16BIT:
415
1.84k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.84k
        break;
417
1.32k
    case FAAD_FMT_24BIT:
418
1.32k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.32k
        break;
420
1.18k
    case FAAD_FMT_32BIT:
421
1.18k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.18k
        break;
423
747
    case FAAD_FMT_FLOAT:
424
747
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
747
        break;
426
610
    case FAAD_FMT_DOUBLE:
427
610
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
610
        break;
429
5.70k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.70k
    return sample_buffer;
437
5.70k
}
output_to_PCM
Line
Count
Source
401
5.70k
{
402
5.70k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.70k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.70k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.70k
    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.70k
    switch (format)
413
5.70k
    {
414
1.84k
    case FAAD_FMT_16BIT:
415
1.84k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.84k
        break;
417
1.32k
    case FAAD_FMT_24BIT:
418
1.32k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.32k
        break;
420
1.18k
    case FAAD_FMT_32BIT:
421
1.18k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.18k
        break;
423
747
    case FAAD_FMT_FLOAT:
424
747
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
747
        break;
426
610
    case FAAD_FMT_DOUBLE:
427
610
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
610
        break;
429
5.70k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.70k
    return sample_buffer;
437
5.70k
}
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
25.2M
{
449
25.2M
    real_t C;
450
25.2M
    if (up_matrix == 1)
451
323k
        return input[internal_channel[0]][sample];
452
453
24.8M
    if (!down_matrix)
454
24.2M
        return input[internal_channel[channel]][sample];
455
456
593k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
593k
    if (channel == 0)
458
286k
    {
459
286k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
286k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
286k
        return core + C + L_S;
462
306k
    } else {
463
306k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
306k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
306k
        return core + C + R_S;
466
306k
    }
467
593k
}
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
5.50k
{
473
5.50k
    uint8_t ch;
474
5.50k
    uint16_t i;
475
5.50k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
5.50k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
5.50k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
41.2k
    for (ch = 0; ch < channels; ch++)
481
35.7k
    {
482
35.7k
        switch (format)
483
35.7k
        {
484
11.4k
        case FAAD_FMT_16BIT:
485
16.9M
            for(i = 0; i < frame_len; i++)
486
16.8M
            {
487
16.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
16.8M
                    hDecoder->internal_channel);
489
16.8M
                if (tmp >= 0)
490
15.3M
                {
491
15.3M
                    tmp += (1 << (REAL_BITS-1));
492
15.3M
                    if (tmp >= REAL_CONST(32767))
493
34.6k
                    {
494
34.6k
                        tmp = REAL_CONST(32767);
495
34.6k
                    }
496
15.3M
                } else {
497
1.56M
                    tmp += -(1 << (REAL_BITS-1));
498
1.56M
                    if (tmp <= REAL_CONST(-32768))
499
36.1k
                    {
500
36.1k
                        tmp = REAL_CONST(-32768);
501
36.1k
                    }
502
1.56M
                }
503
16.8M
                tmp >>= REAL_BITS;
504
16.8M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
16.8M
            }
506
11.4k
            break;
507
9.80k
        case FAAD_FMT_24BIT:
508
13.9M
            for(i = 0; i < frame_len; i++)
509
13.9M
            {
510
13.9M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
13.9M
                    hDecoder->internal_channel);
512
13.9M
                if (tmp >= 0)
513
12.8M
                {
514
12.8M
                    tmp += (1 << (REAL_BITS-9));
515
12.8M
                    tmp >>= (REAL_BITS-8);
516
12.8M
                    if (tmp >= 8388607)
517
31.0k
                    {
518
31.0k
                        tmp = 8388607;
519
31.0k
                    }
520
12.8M
                } else {
521
1.11M
                    tmp += -(1 << (REAL_BITS-9));
522
1.11M
                    tmp >>= (REAL_BITS-8);
523
1.11M
                    if (tmp <= -8388608)
524
29.6k
                    {
525
29.6k
                        tmp = -8388608;
526
29.6k
                    }
527
1.11M
                }
528
13.9M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
13.9M
            }
530
9.80k
            break;
531
7.58k
        case FAAD_FMT_32BIT:
532
7.58k
            exp = 16 - REAL_BITS;
533
7.58k
            half = 1 << (exp - 1);
534
7.58k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
10.2M
            for(i = 0; i < frame_len; i++)
536
10.2M
            {
537
10.2M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
10.2M
                    hDecoder->internal_channel);
539
10.2M
                if (tmp >= 0)
540
9.43M
                {
541
9.43M
                    tmp += half;
542
9.43M
                } else {
543
794k
                    tmp += -half;
544
794k
                }
545
10.2M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
10.2M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
10.2M
            }
548
7.58k
            break;
549
6.92k
        case FAAD_FMT_FIXED:
550
9.36M
            for(i = 0; i < frame_len; i++)
551
9.35M
            {
552
9.35M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
9.35M
                    hDecoder->internal_channel);
554
9.35M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
9.35M
            }
556
6.92k
            break;
557
35.7k
        }
558
35.7k
    }
559
560
5.50k
    return sample_buffer;
561
5.50k
}
output_to_PCM
Line
Count
Source
472
2.75k
{
473
2.75k
    uint8_t ch;
474
2.75k
    uint16_t i;
475
2.75k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.75k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.75k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
20.6k
    for (ch = 0; ch < channels; ch++)
481
17.8k
    {
482
17.8k
        switch (format)
483
17.8k
        {
484
5.71k
        case FAAD_FMT_16BIT:
485
8.45M
            for(i = 0; i < frame_len; i++)
486
8.44M
            {
487
8.44M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.44M
                    hDecoder->internal_channel);
489
8.44M
                if (tmp >= 0)
490
7.66M
                {
491
7.66M
                    tmp += (1 << (REAL_BITS-1));
492
7.66M
                    if (tmp >= REAL_CONST(32767))
493
17.3k
                    {
494
17.3k
                        tmp = REAL_CONST(32767);
495
17.3k
                    }
496
7.66M
                } else {
497
780k
                    tmp += -(1 << (REAL_BITS-1));
498
780k
                    if (tmp <= REAL_CONST(-32768))
499
18.0k
                    {
500
18.0k
                        tmp = REAL_CONST(-32768);
501
18.0k
                    }
502
780k
                }
503
8.44M
                tmp >>= REAL_BITS;
504
8.44M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.44M
            }
506
5.71k
            break;
507
4.90k
        case FAAD_FMT_24BIT:
508
6.97M
            for(i = 0; i < frame_len; i++)
509
6.96M
            {
510
6.96M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
6.96M
                    hDecoder->internal_channel);
512
6.96M
                if (tmp >= 0)
513
6.41M
                {
514
6.41M
                    tmp += (1 << (REAL_BITS-9));
515
6.41M
                    tmp >>= (REAL_BITS-8);
516
6.41M
                    if (tmp >= 8388607)
517
15.5k
                    {
518
15.5k
                        tmp = 8388607;
519
15.5k
                    }
520
6.41M
                } else {
521
556k
                    tmp += -(1 << (REAL_BITS-9));
522
556k
                    tmp >>= (REAL_BITS-8);
523
556k
                    if (tmp <= -8388608)
524
14.8k
                    {
525
14.8k
                        tmp = -8388608;
526
14.8k
                    }
527
556k
                }
528
6.96M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
6.96M
            }
530
4.90k
            break;
531
3.79k
        case FAAD_FMT_32BIT:
532
3.79k
            exp = 16 - REAL_BITS;
533
3.79k
            half = 1 << (exp - 1);
534
3.79k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
5.11M
            for(i = 0; i < frame_len; i++)
536
5.11M
            {
537
5.11M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
5.11M
                    hDecoder->internal_channel);
539
5.11M
                if (tmp >= 0)
540
4.71M
                {
541
4.71M
                    tmp += half;
542
4.71M
                } else {
543
397k
                    tmp += -half;
544
397k
                }
545
5.11M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
5.11M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
5.11M
            }
548
3.79k
            break;
549
3.46k
        case FAAD_FMT_FIXED:
550
4.68M
            for(i = 0; i < frame_len; i++)
551
4.67M
            {
552
4.67M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.67M
                    hDecoder->internal_channel);
554
4.67M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.67M
            }
556
3.46k
            break;
557
17.8k
        }
558
17.8k
    }
559
560
2.75k
    return sample_buffer;
561
2.75k
}
output_to_PCM
Line
Count
Source
472
2.75k
{
473
2.75k
    uint8_t ch;
474
2.75k
    uint16_t i;
475
2.75k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.75k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.75k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
20.6k
    for (ch = 0; ch < channels; ch++)
481
17.8k
    {
482
17.8k
        switch (format)
483
17.8k
        {
484
5.71k
        case FAAD_FMT_16BIT:
485
8.45M
            for(i = 0; i < frame_len; i++)
486
8.44M
            {
487
8.44M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.44M
                    hDecoder->internal_channel);
489
8.44M
                if (tmp >= 0)
490
7.66M
                {
491
7.66M
                    tmp += (1 << (REAL_BITS-1));
492
7.66M
                    if (tmp >= REAL_CONST(32767))
493
17.3k
                    {
494
17.3k
                        tmp = REAL_CONST(32767);
495
17.3k
                    }
496
7.66M
                } else {
497
780k
                    tmp += -(1 << (REAL_BITS-1));
498
780k
                    if (tmp <= REAL_CONST(-32768))
499
18.0k
                    {
500
18.0k
                        tmp = REAL_CONST(-32768);
501
18.0k
                    }
502
780k
                }
503
8.44M
                tmp >>= REAL_BITS;
504
8.44M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.44M
            }
506
5.71k
            break;
507
4.90k
        case FAAD_FMT_24BIT:
508
6.97M
            for(i = 0; i < frame_len; i++)
509
6.96M
            {
510
6.96M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
6.96M
                    hDecoder->internal_channel);
512
6.96M
                if (tmp >= 0)
513
6.41M
                {
514
6.41M
                    tmp += (1 << (REAL_BITS-9));
515
6.41M
                    tmp >>= (REAL_BITS-8);
516
6.41M
                    if (tmp >= 8388607)
517
15.5k
                    {
518
15.5k
                        tmp = 8388607;
519
15.5k
                    }
520
6.41M
                } else {
521
556k
                    tmp += -(1 << (REAL_BITS-9));
522
556k
                    tmp >>= (REAL_BITS-8);
523
556k
                    if (tmp <= -8388608)
524
14.8k
                    {
525
14.8k
                        tmp = -8388608;
526
14.8k
                    }
527
556k
                }
528
6.96M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
6.96M
            }
530
4.90k
            break;
531
3.79k
        case FAAD_FMT_32BIT:
532
3.79k
            exp = 16 - REAL_BITS;
533
3.79k
            half = 1 << (exp - 1);
534
3.79k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
5.11M
            for(i = 0; i < frame_len; i++)
536
5.11M
            {
537
5.11M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
5.11M
                    hDecoder->internal_channel);
539
5.11M
                if (tmp >= 0)
540
4.71M
                {
541
4.71M
                    tmp += half;
542
4.71M
                } else {
543
397k
                    tmp += -half;
544
397k
                }
545
5.11M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
5.11M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
5.11M
            }
548
3.79k
            break;
549
3.46k
        case FAAD_FMT_FIXED:
550
4.68M
            for(i = 0; i < frame_len; i++)
551
4.67M
            {
552
4.67M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.67M
                    hDecoder->internal_channel);
554
4.67M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.67M
            }
556
3.46k
            break;
557
17.8k
        }
558
17.8k
    }
559
560
2.75k
    return sample_buffer;
561
2.75k
}
562
563
#endif