Coverage Report

Created: 2025-07-18 06:36

/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.8M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.45M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
4.90M
#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.6M
{
48
40.6M
    if (!down_matrix)
49
38.2M
        return input[internal_channel[channel]][sample];
50
51
2.45M
    if (channel == 0)
52
1.21M
    {
53
1.21M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.21M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.21M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.24M
    } else {
57
1.24M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.24M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.24M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.24M
    }
61
2.45M
}
62
63
#ifndef HAS_LRINTF
64
35.3M
#define CLIP(sample, max, min) \
65
35.3M
if (sample >= 0.0f)            \
66
30.6M
{                              \
67
30.6M
    sample += 0.5f;            \
68
30.6M
    if (sample >= max)         \
69
30.6M
        sample = max;          \
70
30.6M
} else {                       \
71
4.63M
    sample += -0.5f;           \
72
4.63M
    if (sample <= min)         \
73
4.63M
        sample = min;          \
74
4.63M
}
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.03k
#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.88k
{
93
1.88k
    uint8_t ch, ch1;
94
1.88k
    uint16_t i;
95
96
1.88k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.88k
    {
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
960
    case CONV(2,0):
110
960
        if (hDecoder->upMatrix)
111
64
        {
112
64
            ch  = hDecoder->internal_channel[0];
113
101k
            for(i = 0; i < frame_len; i++)
114
101k
            {
115
101k
                real_t inp0 = input[ch][i];
116
117
101k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
101k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
101k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
101k
            }
122
896
        } else {
123
896
            ch  = hDecoder->internal_channel[0];
124
896
            ch1 = hDecoder->internal_channel[1];
125
1.45M
            for(i = 0; i < frame_len; i++)
126
1.45M
            {
127
1.45M
                real_t inp0 = input[ch ][i];
128
1.45M
                real_t inp1 = input[ch1][i];
129
130
1.45M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.45M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.45M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.45M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.45M
            }
136
896
        }
137
960
        break;
138
923
    default:
139
10.5k
        for (ch = 0; ch < channels; ch++)
140
9.59k
        {
141
13.5M
            for(i = 0; i < frame_len; i++)
142
13.5M
            {
143
13.5M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
13.5M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
13.5M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
13.5M
            }
149
9.59k
        }
150
923
        break;
151
1.88k
    }
152
1.88k
}
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.20k
{
158
1.20k
    uint8_t ch, ch1;
159
1.20k
    uint16_t i;
160
161
1.20k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.20k
    {
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
554
    case CONV(2,0):
176
554
        if (hDecoder->upMatrix)
177
53
        {
178
53
            ch = hDecoder->internal_channel[0];
179
85.3k
            for(i = 0; i < frame_len; i++)
180
85.2k
            {
181
85.2k
                real_t inp0 = input[ch][i];
182
183
85.2k
                inp0 *= 256.0f;
184
85.2k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
85.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
85.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
85.2k
            }
189
501
        } else {
190
501
            ch  = hDecoder->internal_channel[0];
191
501
            ch1 = hDecoder->internal_channel[1];
192
736k
            for(i = 0; i < frame_len; i++)
193
736k
            {
194
736k
                real_t inp0 = input[ch ][i];
195
736k
                real_t inp1 = input[ch1][i];
196
197
736k
                inp0 *= 256.0f;
198
736k
                inp1 *= 256.0f;
199
736k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
736k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
736k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
736k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
736k
            }
205
501
        }
206
554
        break;
207
652
    default:
208
6.34k
        for (ch = 0; ch < channels; ch++)
209
5.69k
        {
210
8.11M
            for(i = 0; i < frame_len; i++)
211
8.10M
            {
212
8.10M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.10M
                inp *= 256.0f;
215
8.10M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.10M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.10M
            }
219
5.69k
        }
220
652
        break;
221
1.20k
    }
222
1.20k
}
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
632
    case CONV(2,0):
246
632
        if (hDecoder->upMatrix)
247
46
        {
248
46
            ch = hDecoder->internal_channel[0];
249
70.2k
            for(i = 0; i < frame_len; i++)
250
70.2k
            {
251
70.2k
                real_t inp0 = input[ch][i];
252
253
70.2k
                inp0 *= 65536.0f;
254
70.2k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
70.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
70.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
70.2k
            }
259
586
        } else {
260
586
            ch  = hDecoder->internal_channel[0];
261
586
            ch1 = hDecoder->internal_channel[1];
262
905k
            for(i = 0; i < frame_len; i++)
263
905k
            {
264
905k
                real_t inp0 = input[ch ][i];
265
905k
                real_t inp1 = input[ch1][i];
266
267
905k
                inp0 *= 65536.0f;
268
905k
                inp1 *= 65536.0f;
269
905k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
905k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
905k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
905k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
905k
            }
275
586
        }
276
632
        break;
277
553
    default:
278
5.51k
        for (ch = 0; ch < channels; ch++)
279
4.95k
        {
280
7.19M
            for(i = 0; i < frame_len; i++)
281
7.19M
            {
282
7.19M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
7.19M
                inp *= 65536.0f;
285
7.19M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
7.19M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
7.19M
            }
289
4.95k
        }
290
553
        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
731
{
298
731
    uint8_t ch, ch1;
299
731
    uint16_t i;
300
301
731
    switch (CONV(channels,hDecoder->downMatrix))
302
731
    {
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
185
    case CONV(2,0):
312
185
        if (hDecoder->upMatrix)
313
11
        {
314
11
            ch = hDecoder->internal_channel[0];
315
18.3k
            for(i = 0; i < frame_len; i++)
316
18.3k
            {
317
18.3k
                real_t inp0 = input[ch][i];
318
18.3k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
18.3k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
18.3k
            }
321
174
        } else {
322
174
            ch  = hDecoder->internal_channel[0];
323
174
            ch1 = hDecoder->internal_channel[1];
324
273k
            for(i = 0; i < frame_len; i++)
325
272k
            {
326
272k
                real_t inp0 = input[ch ][i];
327
272k
                real_t inp1 = input[ch1][i];
328
272k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
272k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
272k
            }
331
174
        }
332
185
        break;
333
546
    default:
334
5.01k
        for (ch = 0; ch < channels; ch++)
335
4.46k
        {
336
6.22M
            for(i = 0; i < frame_len; i++)
337
6.21M
            {
338
6.21M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
6.21M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
6.21M
            }
341
4.46k
        }
342
546
        break;
343
731
    }
344
731
}
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
529
{
350
529
    uint8_t ch, ch1;
351
529
    uint16_t i;
352
353
529
    switch (CONV(channels,hDecoder->downMatrix))
354
529
    {
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
169
    case CONV(2,0):
364
169
        if (hDecoder->upMatrix)
365
17
        {
366
17
            ch = hDecoder->internal_channel[0];
367
24.2k
            for(i = 0; i < frame_len; i++)
368
24.1k
            {
369
24.1k
                real_t inp0 = input[ch][i];
370
24.1k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
24.1k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
24.1k
            }
373
152
        } else {
374
152
            ch  = hDecoder->internal_channel[0];
375
152
            ch1 = hDecoder->internal_channel[1];
376
194k
            for(i = 0; i < frame_len; i++)
377
194k
            {
378
194k
                real_t inp0 = input[ch ][i];
379
194k
                real_t inp1 = input[ch1][i];
380
194k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
194k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
194k
            }
383
152
        }
384
169
        break;
385
360
    default:
386
4.13k
        for (ch = 0; ch < channels; ch++)
387
3.77k
        {
388
5.61M
            for(i = 0; i < frame_len; i++)
389
5.61M
            {
390
5.61M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
5.61M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
5.61M
            }
393
3.77k
        }
394
360
        break;
395
529
    }
396
529
}
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.0k
{
402
11.0k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
11.0k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
11.0k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
11.0k
    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.0k
    switch (format)
413
11.0k
    {
414
3.76k
    case FAAD_FMT_16BIT:
415
3.76k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.76k
        break;
417
2.41k
    case FAAD_FMT_24BIT:
418
2.41k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.41k
        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.46k
    case FAAD_FMT_FLOAT:
424
1.46k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.46k
        break;
426
1.05k
    case FAAD_FMT_DOUBLE:
427
1.05k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.05k
        break;
429
11.0k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
11.0k
    return sample_buffer;
437
11.0k
}
output_to_PCM
Line
Count
Source
401
5.53k
{
402
5.53k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.53k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.53k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.53k
    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.53k
    switch (format)
413
5.53k
    {
414
1.88k
    case FAAD_FMT_16BIT:
415
1.88k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.88k
        break;
417
1.20k
    case FAAD_FMT_24BIT:
418
1.20k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.20k
        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
731
    case FAAD_FMT_FLOAT:
424
731
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
731
        break;
426
529
    case FAAD_FMT_DOUBLE:
427
529
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
529
        break;
429
5.53k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.53k
    return sample_buffer;
437
5.53k
}
output_to_PCM
Line
Count
Source
401
5.53k
{
402
5.53k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.53k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.53k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.53k
    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.53k
    switch (format)
413
5.53k
    {
414
1.88k
    case FAAD_FMT_16BIT:
415
1.88k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.88k
        break;
417
1.20k
    case FAAD_FMT_24BIT:
418
1.20k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.20k
        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
731
    case FAAD_FMT_FLOAT:
424
731
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
731
        break;
426
529
    case FAAD_FMT_DOUBLE:
427
529
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
529
        break;
429
5.53k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.53k
    return sample_buffer;
437
5.53k
}
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
29.1M
{
449
29.1M
    real_t C;
450
29.1M
    if (up_matrix == 1)
451
322k
        return input[internal_channel[0]][sample];
452
453
28.8M
    if (!down_matrix)
454
28.2M
        return input[internal_channel[channel]][sample];
455
456
609k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
609k
    if (channel == 0)
458
292k
    {
459
292k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
292k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
292k
        return core + C + L_S;
462
316k
    } else {
463
316k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
316k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
316k
        return core + C + R_S;
466
316k
    }
467
609k
}
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
6.77k
{
473
6.77k
    uint8_t ch;
474
6.77k
    uint16_t i;
475
6.77k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
6.77k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
6.77k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
46.8k
    for (ch = 0; ch < channels; ch++)
481
40.1k
    {
482
40.1k
        switch (format)
483
40.1k
        {
484
12.4k
        case FAAD_FMT_16BIT:
485
17.8M
            for(i = 0; i < frame_len; i++)
486
17.8M
            {
487
17.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
17.8M
                    hDecoder->internal_channel);
489
17.8M
                if (tmp >= 0)
490
16.7M
                {
491
16.7M
                    tmp += (1 << (REAL_BITS-1));
492
16.7M
                    if (tmp >= REAL_CONST(32767))
493
30.2k
                    {
494
30.2k
                        tmp = REAL_CONST(32767);
495
30.2k
                    }
496
16.7M
                } else {
497
1.15M
                    tmp += -(1 << (REAL_BITS-1));
498
1.15M
                    if (tmp <= REAL_CONST(-32768))
499
31.0k
                    {
500
31.0k
                        tmp = REAL_CONST(-32768);
501
31.0k
                    }
502
1.15M
                }
503
17.8M
                tmp >>= REAL_BITS;
504
17.8M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
17.8M
            }
506
12.4k
            break;
507
12.4k
        case FAAD_FMT_24BIT:
508
18.5M
            for(i = 0; i < frame_len; i++)
509
18.5M
            {
510
18.5M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
18.5M
                    hDecoder->internal_channel);
512
18.5M
                if (tmp >= 0)
513
16.8M
                {
514
16.8M
                    tmp += (1 << (REAL_BITS-9));
515
16.8M
                    tmp >>= (REAL_BITS-8);
516
16.8M
                    if (tmp >= 8388607)
517
29.1k
                    {
518
29.1k
                        tmp = 8388607;
519
29.1k
                    }
520
16.8M
                } else {
521
1.71M
                    tmp += -(1 << (REAL_BITS-9));
522
1.71M
                    tmp >>= (REAL_BITS-8);
523
1.71M
                    if (tmp <= -8388608)
524
29.5k
                    {
525
29.5k
                        tmp = -8388608;
526
29.5k
                    }
527
1.71M
                }
528
18.5M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
18.5M
            }
530
12.4k
            break;
531
8.98k
        case FAAD_FMT_32BIT:
532
8.98k
            exp = 16 - REAL_BITS;
533
8.98k
            half = 1 << (exp - 1);
534
8.98k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
12.6M
            for(i = 0; i < frame_len; i++)
536
12.6M
            {
537
12.6M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
12.6M
                    hDecoder->internal_channel);
539
12.6M
                if (tmp >= 0)
540
11.4M
                {
541
11.4M
                    tmp += half;
542
11.4M
                } else {
543
1.13M
                    tmp += -half;
544
1.13M
                }
545
12.6M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
12.6M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
12.6M
            }
548
8.98k
            break;
549
6.29k
        case FAAD_FMT_FIXED:
550
9.33M
            for(i = 0; i < frame_len; i++)
551
9.33M
            {
552
9.33M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
9.33M
                    hDecoder->internal_channel);
554
9.33M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
9.33M
            }
556
6.29k
            break;
557
40.1k
        }
558
40.1k
    }
559
560
6.77k
    return sample_buffer;
561
6.77k
}
output_to_PCM
Line
Count
Source
472
3.38k
{
473
3.38k
    uint8_t ch;
474
3.38k
    uint16_t i;
475
3.38k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.38k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.38k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
23.4k
    for (ch = 0; ch < channels; ch++)
481
20.0k
    {
482
20.0k
        switch (format)
483
20.0k
        {
484
6.21k
        case FAAD_FMT_16BIT:
485
8.94M
            for(i = 0; i < frame_len; i++)
486
8.94M
            {
487
8.94M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.94M
                    hDecoder->internal_channel);
489
8.94M
                if (tmp >= 0)
490
8.36M
                {
491
8.36M
                    tmp += (1 << (REAL_BITS-1));
492
8.36M
                    if (tmp >= REAL_CONST(32767))
493
15.1k
                    {
494
15.1k
                        tmp = REAL_CONST(32767);
495
15.1k
                    }
496
8.36M
                } else {
497
579k
                    tmp += -(1 << (REAL_BITS-1));
498
579k
                    if (tmp <= REAL_CONST(-32768))
499
15.5k
                    {
500
15.5k
                        tmp = REAL_CONST(-32768);
501
15.5k
                    }
502
579k
                }
503
8.94M
                tmp >>= REAL_BITS;
504
8.94M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.94M
            }
506
6.21k
            break;
507
6.21k
        case FAAD_FMT_24BIT:
508
9.27M
            for(i = 0; i < frame_len; i++)
509
9.26M
            {
510
9.26M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
9.26M
                    hDecoder->internal_channel);
512
9.26M
                if (tmp >= 0)
513
8.40M
                {
514
8.40M
                    tmp += (1 << (REAL_BITS-9));
515
8.40M
                    tmp >>= (REAL_BITS-8);
516
8.40M
                    if (tmp >= 8388607)
517
14.5k
                    {
518
14.5k
                        tmp = 8388607;
519
14.5k
                    }
520
8.40M
                } else {
521
857k
                    tmp += -(1 << (REAL_BITS-9));
522
857k
                    tmp >>= (REAL_BITS-8);
523
857k
                    if (tmp <= -8388608)
524
14.7k
                    {
525
14.7k
                        tmp = -8388608;
526
14.7k
                    }
527
857k
                }
528
9.26M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
9.26M
            }
530
6.21k
            break;
531
4.49k
        case FAAD_FMT_32BIT:
532
4.49k
            exp = 16 - REAL_BITS;
533
4.49k
            half = 1 << (exp - 1);
534
4.49k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
6.30M
            for(i = 0; i < frame_len; i++)
536
6.30M
            {
537
6.30M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
6.30M
                    hDecoder->internal_channel);
539
6.30M
                if (tmp >= 0)
540
5.73M
                {
541
5.73M
                    tmp += half;
542
5.73M
                } else {
543
565k
                    tmp += -half;
544
565k
                }
545
6.30M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
6.30M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
6.30M
            }
548
4.49k
            break;
549
3.14k
        case FAAD_FMT_FIXED:
550
4.66M
            for(i = 0; i < frame_len; i++)
551
4.66M
            {
552
4.66M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.66M
                    hDecoder->internal_channel);
554
4.66M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.66M
            }
556
3.14k
            break;
557
20.0k
        }
558
20.0k
    }
559
560
3.38k
    return sample_buffer;
561
3.38k
}
output_to_PCM
Line
Count
Source
472
3.38k
{
473
3.38k
    uint8_t ch;
474
3.38k
    uint16_t i;
475
3.38k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.38k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.38k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
23.4k
    for (ch = 0; ch < channels; ch++)
481
20.0k
    {
482
20.0k
        switch (format)
483
20.0k
        {
484
6.21k
        case FAAD_FMT_16BIT:
485
8.94M
            for(i = 0; i < frame_len; i++)
486
8.94M
            {
487
8.94M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.94M
                    hDecoder->internal_channel);
489
8.94M
                if (tmp >= 0)
490
8.36M
                {
491
8.36M
                    tmp += (1 << (REAL_BITS-1));
492
8.36M
                    if (tmp >= REAL_CONST(32767))
493
15.1k
                    {
494
15.1k
                        tmp = REAL_CONST(32767);
495
15.1k
                    }
496
8.36M
                } else {
497
579k
                    tmp += -(1 << (REAL_BITS-1));
498
579k
                    if (tmp <= REAL_CONST(-32768))
499
15.5k
                    {
500
15.5k
                        tmp = REAL_CONST(-32768);
501
15.5k
                    }
502
579k
                }
503
8.94M
                tmp >>= REAL_BITS;
504
8.94M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.94M
            }
506
6.21k
            break;
507
6.21k
        case FAAD_FMT_24BIT:
508
9.27M
            for(i = 0; i < frame_len; i++)
509
9.26M
            {
510
9.26M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
9.26M
                    hDecoder->internal_channel);
512
9.26M
                if (tmp >= 0)
513
8.40M
                {
514
8.40M
                    tmp += (1 << (REAL_BITS-9));
515
8.40M
                    tmp >>= (REAL_BITS-8);
516
8.40M
                    if (tmp >= 8388607)
517
14.5k
                    {
518
14.5k
                        tmp = 8388607;
519
14.5k
                    }
520
8.40M
                } else {
521
857k
                    tmp += -(1 << (REAL_BITS-9));
522
857k
                    tmp >>= (REAL_BITS-8);
523
857k
                    if (tmp <= -8388608)
524
14.7k
                    {
525
14.7k
                        tmp = -8388608;
526
14.7k
                    }
527
857k
                }
528
9.26M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
9.26M
            }
530
6.21k
            break;
531
4.49k
        case FAAD_FMT_32BIT:
532
4.49k
            exp = 16 - REAL_BITS;
533
4.49k
            half = 1 << (exp - 1);
534
4.49k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
6.30M
            for(i = 0; i < frame_len; i++)
536
6.30M
            {
537
6.30M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
6.30M
                    hDecoder->internal_channel);
539
6.30M
                if (tmp >= 0)
540
5.73M
                {
541
5.73M
                    tmp += half;
542
5.73M
                } else {
543
565k
                    tmp += -half;
544
565k
                }
545
6.30M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
6.30M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
6.30M
            }
548
4.49k
            break;
549
3.14k
        case FAAD_FMT_FIXED:
550
4.66M
            for(i = 0; i < frame_len; i++)
551
4.66M
            {
552
4.66M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.66M
                    hDecoder->internal_channel);
554
4.66M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.66M
            }
556
3.14k
            break;
557
20.0k
        }
558
20.0k
    }
559
560
3.38k
    return sample_buffer;
561
3.38k
}
562
563
#endif