Coverage Report

Created: 2025-07-23 06:30

/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.9M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.47M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
4.94M
#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.2M
{
48
40.2M
    if (!down_matrix)
49
37.7M
        return input[internal_channel[channel]][sample];
50
51
2.47M
    if (channel == 0)
52
1.22M
    {
53
1.22M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.22M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.22M
            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.47M
}
62
63
#ifndef HAS_LRINTF
64
34.8M
#define CLIP(sample, max, min) \
65
34.8M
if (sample >= 0.0f)            \
66
30.1M
{                              \
67
30.1M
    sample += 0.5f;            \
68
30.1M
    if (sample >= max)         \
69
30.1M
        sample = max;          \
70
30.1M
} else {                       \
71
4.67M
    sample += -0.5f;           \
72
4.67M
    if (sample <= min)         \
73
4.67M
        sample = min;          \
74
4.67M
}
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.14k
#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.85k
{
93
1.85k
    uint8_t ch, ch1;
94
1.85k
    uint16_t i;
95
96
1.85k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.85k
    {
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
970
    case CONV(2,0):
110
970
        if (hDecoder->upMatrix)
111
67
        {
112
67
            ch  = hDecoder->internal_channel[0];
113
108k
            for(i = 0; i < frame_len; i++)
114
108k
            {
115
108k
                real_t inp0 = input[ch][i];
116
117
108k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
108k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
108k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
108k
            }
122
903
        } else {
123
903
            ch  = hDecoder->internal_channel[0];
124
903
            ch1 = hDecoder->internal_channel[1];
125
1.44M
            for(i = 0; i < frame_len; i++)
126
1.44M
            {
127
1.44M
                real_t inp0 = input[ch ][i];
128
1.44M
                real_t inp1 = input[ch1][i];
129
130
1.44M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.44M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.44M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.44M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.44M
            }
136
903
        }
137
970
        break;
138
886
    default:
139
10.2k
        for (ch = 0; ch < channels; ch++)
140
9.41k
        {
141
13.0M
            for(i = 0; i < frame_len; i++)
142
13.0M
            {
143
13.0M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
13.0M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
13.0M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
13.0M
            }
149
9.41k
        }
150
886
        break;
151
1.85k
    }
152
1.85k
}
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.23k
{
158
1.23k
    uint8_t ch, ch1;
159
1.23k
    uint16_t i;
160
161
1.23k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.23k
    {
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
581
    case CONV(2,0):
176
581
        if (hDecoder->upMatrix)
177
50
        {
178
50
            ch = hDecoder->internal_channel[0];
179
81.3k
            for(i = 0; i < frame_len; i++)
180
81.2k
            {
181
81.2k
                real_t inp0 = input[ch][i];
182
183
81.2k
                inp0 *= 256.0f;
184
81.2k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
81.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
81.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
81.2k
            }
189
531
        } else {
190
531
            ch  = hDecoder->internal_channel[0];
191
531
            ch1 = hDecoder->internal_channel[1];
192
789k
            for(i = 0; i < frame_len; i++)
193
788k
            {
194
788k
                real_t inp0 = input[ch ][i];
195
788k
                real_t inp1 = input[ch1][i];
196
197
788k
                inp0 *= 256.0f;
198
788k
                inp1 *= 256.0f;
199
788k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
788k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
788k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
788k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
788k
            }
205
531
        }
206
581
        break;
207
652
    default:
208
6.38k
        for (ch = 0; ch < channels; ch++)
209
5.73k
        {
210
8.14M
            for(i = 0; i < frame_len; i++)
211
8.14M
            {
212
8.14M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.14M
                inp *= 256.0f;
215
8.14M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.14M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.14M
            }
219
5.73k
        }
220
652
        break;
221
1.23k
    }
222
1.23k
}
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.21k
{
228
1.21k
    uint8_t ch, ch1;
229
1.21k
    uint16_t i;
230
231
1.21k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.21k
    {
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
646
    case CONV(2,0):
246
646
        if (hDecoder->upMatrix)
247
45
        {
248
45
            ch = hDecoder->internal_channel[0];
249
69.2k
            for(i = 0; i < frame_len; i++)
250
69.2k
            {
251
69.2k
                real_t inp0 = input[ch][i];
252
253
69.2k
                inp0 *= 65536.0f;
254
69.2k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
69.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
69.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
69.2k
            }
259
601
        } else {
260
601
            ch  = hDecoder->internal_channel[0];
261
601
            ch1 = hDecoder->internal_channel[1];
262
913k
            for(i = 0; i < frame_len; i++)
263
912k
            {
264
912k
                real_t inp0 = input[ch ][i];
265
912k
                real_t inp1 = input[ch1][i];
266
267
912k
                inp0 *= 65536.0f;
268
912k
                inp1 *= 65536.0f;
269
912k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
912k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
912k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
912k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
912k
            }
275
601
        }
276
646
        break;
277
566
    default:
278
5.52k
        for (ch = 0; ch < channels; ch++)
279
4.95k
        {
280
7.07M
            for(i = 0; i < frame_len; i++)
281
7.07M
            {
282
7.07M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
7.07M
                inp *= 65536.0f;
285
7.07M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
7.07M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
7.07M
            }
289
4.95k
        }
290
566
        break;
291
1.21k
    }
292
1.21k
}
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
738
{
298
738
    uint8_t ch, ch1;
299
738
    uint16_t i;
300
301
738
    switch (CONV(channels,hDecoder->downMatrix))
302
738
    {
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
194
    case CONV(2,0):
312
194
        if (hDecoder->upMatrix)
313
14
        {
314
14
            ch = hDecoder->internal_channel[0];
315
24.2k
            for(i = 0; i < frame_len; i++)
316
24.1k
            {
317
24.1k
                real_t inp0 = input[ch][i];
318
24.1k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
24.1k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
24.1k
            }
321
180
        } else {
322
180
            ch  = hDecoder->internal_channel[0];
323
180
            ch1 = hDecoder->internal_channel[1];
324
274k
            for(i = 0; i < frame_len; i++)
325
274k
            {
326
274k
                real_t inp0 = input[ch ][i];
327
274k
                real_t inp1 = input[ch1][i];
328
274k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
274k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
274k
            }
331
180
        }
332
194
        break;
333
544
    default:
334
5.02k
        for (ch = 0; ch < channels; ch++)
335
4.47k
        {
336
6.28M
            for(i = 0; i < frame_len; i++)
337
6.28M
            {
338
6.28M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
6.28M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
6.28M
            }
341
4.47k
        }
342
544
        break;
343
738
    }
344
738
}
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
550
{
350
550
    uint8_t ch, ch1;
351
550
    uint16_t i;
352
353
550
    switch (CONV(channels,hDecoder->downMatrix))
354
550
    {
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
168
    case CONV(2,0):
364
168
        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
151
        } else {
374
151
            ch  = hDecoder->internal_channel[0];
375
151
            ch1 = hDecoder->internal_channel[1];
376
187k
            for(i = 0; i < frame_len; i++)
377
187k
            {
378
187k
                real_t inp0 = input[ch ][i];
379
187k
                real_t inp1 = input[ch1][i];
380
187k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
187k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
187k
            }
383
151
        }
384
168
        break;
385
382
    default:
386
4.24k
        for (ch = 0; ch < channels; ch++)
387
3.86k
        {
388
5.65M
            for(i = 0; i < frame_len; i++)
389
5.64M
            {
390
5.64M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
5.64M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
5.64M
            }
393
3.86k
        }
394
382
        break;
395
550
    }
396
550
}
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.1k
{
402
11.1k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
11.1k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
11.1k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
11.1k
    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.1k
    switch (format)
413
11.1k
    {
414
3.71k
    case FAAD_FMT_16BIT:
415
3.71k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.71k
        break;
417
2.46k
    case FAAD_FMT_24BIT:
418
2.46k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.46k
        break;
420
2.42k
    case FAAD_FMT_32BIT:
421
2.42k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.42k
        break;
423
1.47k
    case FAAD_FMT_FLOAT:
424
1.47k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.47k
        break;
426
1.10k
    case FAAD_FMT_DOUBLE:
427
1.10k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.10k
        break;
429
11.1k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
11.1k
    return sample_buffer;
437
11.1k
}
output_to_PCM
Line
Count
Source
401
5.58k
{
402
5.58k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.58k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.58k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.58k
    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.58k
    switch (format)
413
5.58k
    {
414
1.85k
    case FAAD_FMT_16BIT:
415
1.85k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.85k
        break;
417
1.23k
    case FAAD_FMT_24BIT:
418
1.23k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.23k
        break;
420
1.21k
    case FAAD_FMT_32BIT:
421
1.21k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.21k
        break;
423
738
    case FAAD_FMT_FLOAT:
424
738
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
738
        break;
426
550
    case FAAD_FMT_DOUBLE:
427
550
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
550
        break;
429
5.58k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.58k
    return sample_buffer;
437
5.58k
}
output_to_PCM
Line
Count
Source
401
5.58k
{
402
5.58k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.58k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.58k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.58k
    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.58k
    switch (format)
413
5.58k
    {
414
1.85k
    case FAAD_FMT_16BIT:
415
1.85k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.85k
        break;
417
1.23k
    case FAAD_FMT_24BIT:
418
1.23k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.23k
        break;
420
1.21k
    case FAAD_FMT_32BIT:
421
1.21k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.21k
        break;
423
738
    case FAAD_FMT_FLOAT:
424
738
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
738
        break;
426
550
    case FAAD_FMT_DOUBLE:
427
550
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
550
        break;
429
5.58k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.58k
    return sample_buffer;
437
5.58k
}
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
30.3M
{
449
30.3M
    real_t C;
450
30.3M
    if (up_matrix == 1)
451
316k
        return input[internal_channel[0]][sample];
452
453
30.0M
    if (!down_matrix)
454
29.3M
        return input[internal_channel[channel]][sample];
455
456
688k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
688k
    if (channel == 0)
458
334k
    {
459
334k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
334k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
334k
        return core + C + L_S;
462
353k
    } else {
463
353k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
353k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
353k
        return core + C + R_S;
466
353k
    }
467
688k
}
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
7.03k
{
473
7.03k
    uint8_t ch;
474
7.03k
    uint16_t i;
475
7.03k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
7.03k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
7.03k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
48.9k
    for (ch = 0; ch < channels; ch++)
481
41.8k
    {
482
41.8k
        switch (format)
483
41.8k
        {
484
12.3k
        case FAAD_FMT_16BIT:
485
18.0M
            for(i = 0; i < frame_len; i++)
486
18.0M
            {
487
18.0M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
18.0M
                    hDecoder->internal_channel);
489
18.0M
                if (tmp >= 0)
490
16.9M
                {
491
16.9M
                    tmp += (1 << (REAL_BITS-1));
492
16.9M
                    if (tmp >= REAL_CONST(32767))
493
27.6k
                    {
494
27.6k
                        tmp = REAL_CONST(32767);
495
27.6k
                    }
496
16.9M
                } else {
497
1.09M
                    tmp += -(1 << (REAL_BITS-1));
498
1.09M
                    if (tmp <= REAL_CONST(-32768))
499
29.0k
                    {
500
29.0k
                        tmp = REAL_CONST(-32768);
501
29.0k
                    }
502
1.09M
                }
503
18.0M
                tmp >>= REAL_BITS;
504
18.0M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
18.0M
            }
506
12.3k
            break;
507
12.5k
        case FAAD_FMT_24BIT:
508
18.4M
            for(i = 0; i < frame_len; i++)
509
18.4M
            {
510
18.4M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
18.4M
                    hDecoder->internal_channel);
512
18.4M
                if (tmp >= 0)
513
16.6M
                {
514
16.6M
                    tmp += (1 << (REAL_BITS-9));
515
16.6M
                    tmp >>= (REAL_BITS-8);
516
16.6M
                    if (tmp >= 8388607)
517
24.6k
                    {
518
24.6k
                        tmp = 8388607;
519
24.6k
                    }
520
16.6M
                } else {
521
1.80M
                    tmp += -(1 << (REAL_BITS-9));
522
1.80M
                    tmp >>= (REAL_BITS-8);
523
1.80M
                    if (tmp <= -8388608)
524
23.9k
                    {
525
23.9k
                        tmp = -8388608;
526
23.9k
                    }
527
1.80M
                }
528
18.4M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
18.4M
            }
530
12.5k
            break;
531
9.77k
        case FAAD_FMT_32BIT:
532
9.77k
            exp = 16 - REAL_BITS;
533
9.77k
            half = 1 << (exp - 1);
534
9.77k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
13.7M
            for(i = 0; i < frame_len; i++)
536
13.6M
            {
537
13.6M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
13.6M
                    hDecoder->internal_channel);
539
13.6M
                if (tmp >= 0)
540
12.5M
                {
541
12.5M
                    tmp += half;
542
12.5M
                } else {
543
1.17M
                    tmp += -half;
544
1.17M
                }
545
13.6M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
13.6M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
13.6M
            }
548
9.77k
            break;
549
7.25k
        case FAAD_FMT_FIXED:
550
10.4M
            for(i = 0; i < frame_len; i++)
551
10.4M
            {
552
10.4M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
10.4M
                    hDecoder->internal_channel);
554
10.4M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
10.4M
            }
556
7.25k
            break;
557
41.8k
        }
558
41.8k
    }
559
560
7.03k
    return sample_buffer;
561
7.03k
}
output_to_PCM
Line
Count
Source
472
3.51k
{
473
3.51k
    uint8_t ch;
474
3.51k
    uint16_t i;
475
3.51k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.51k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.51k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
24.4k
    for (ch = 0; ch < channels; ch++)
481
20.9k
    {
482
20.9k
        switch (format)
483
20.9k
        {
484
6.16k
        case FAAD_FMT_16BIT:
485
9.04M
            for(i = 0; i < frame_len; i++)
486
9.04M
            {
487
9.04M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
9.04M
                    hDecoder->internal_channel);
489
9.04M
                if (tmp >= 0)
490
8.49M
                {
491
8.49M
                    tmp += (1 << (REAL_BITS-1));
492
8.49M
                    if (tmp >= REAL_CONST(32767))
493
13.8k
                    {
494
13.8k
                        tmp = REAL_CONST(32767);
495
13.8k
                    }
496
8.49M
                } else {
497
547k
                    tmp += -(1 << (REAL_BITS-1));
498
547k
                    if (tmp <= REAL_CONST(-32768))
499
14.5k
                    {
500
14.5k
                        tmp = REAL_CONST(-32768);
501
14.5k
                    }
502
547k
                }
503
9.04M
                tmp >>= REAL_BITS;
504
9.04M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
9.04M
            }
506
6.16k
            break;
507
6.25k
        case FAAD_FMT_24BIT:
508
9.21M
            for(i = 0; i < frame_len; i++)
509
9.21M
            {
510
9.21M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
9.21M
                    hDecoder->internal_channel);
512
9.21M
                if (tmp >= 0)
513
8.30M
                {
514
8.30M
                    tmp += (1 << (REAL_BITS-9));
515
8.30M
                    tmp >>= (REAL_BITS-8);
516
8.30M
                    if (tmp >= 8388607)
517
12.3k
                    {
518
12.3k
                        tmp = 8388607;
519
12.3k
                    }
520
8.30M
                } else {
521
902k
                    tmp += -(1 << (REAL_BITS-9));
522
902k
                    tmp >>= (REAL_BITS-8);
523
902k
                    if (tmp <= -8388608)
524
11.9k
                    {
525
11.9k
                        tmp = -8388608;
526
11.9k
                    }
527
902k
                }
528
9.21M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
9.21M
            }
530
6.25k
            break;
531
4.88k
        case FAAD_FMT_32BIT:
532
4.88k
            exp = 16 - REAL_BITS;
533
4.88k
            half = 1 << (exp - 1);
534
4.88k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
6.85M
            for(i = 0; i < frame_len; i++)
536
6.84M
            {
537
6.84M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
6.84M
                    hDecoder->internal_channel);
539
6.84M
                if (tmp >= 0)
540
6.25M
                {
541
6.25M
                    tmp += half;
542
6.25M
                } else {
543
586k
                    tmp += -half;
544
586k
                }
545
6.84M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
6.84M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
6.84M
            }
548
4.88k
            break;
549
3.62k
        case FAAD_FMT_FIXED:
550
5.24M
            for(i = 0; i < frame_len; i++)
551
5.23M
            {
552
5.23M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
5.23M
                    hDecoder->internal_channel);
554
5.23M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
5.23M
            }
556
3.62k
            break;
557
20.9k
        }
558
20.9k
    }
559
560
3.51k
    return sample_buffer;
561
3.51k
}
output_to_PCM
Line
Count
Source
472
3.51k
{
473
3.51k
    uint8_t ch;
474
3.51k
    uint16_t i;
475
3.51k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.51k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.51k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
24.4k
    for (ch = 0; ch < channels; ch++)
481
20.9k
    {
482
20.9k
        switch (format)
483
20.9k
        {
484
6.16k
        case FAAD_FMT_16BIT:
485
9.04M
            for(i = 0; i < frame_len; i++)
486
9.04M
            {
487
9.04M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
9.04M
                    hDecoder->internal_channel);
489
9.04M
                if (tmp >= 0)
490
8.49M
                {
491
8.49M
                    tmp += (1 << (REAL_BITS-1));
492
8.49M
                    if (tmp >= REAL_CONST(32767))
493
13.8k
                    {
494
13.8k
                        tmp = REAL_CONST(32767);
495
13.8k
                    }
496
8.49M
                } else {
497
547k
                    tmp += -(1 << (REAL_BITS-1));
498
547k
                    if (tmp <= REAL_CONST(-32768))
499
14.5k
                    {
500
14.5k
                        tmp = REAL_CONST(-32768);
501
14.5k
                    }
502
547k
                }
503
9.04M
                tmp >>= REAL_BITS;
504
9.04M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
9.04M
            }
506
6.16k
            break;
507
6.25k
        case FAAD_FMT_24BIT:
508
9.21M
            for(i = 0; i < frame_len; i++)
509
9.21M
            {
510
9.21M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
9.21M
                    hDecoder->internal_channel);
512
9.21M
                if (tmp >= 0)
513
8.30M
                {
514
8.30M
                    tmp += (1 << (REAL_BITS-9));
515
8.30M
                    tmp >>= (REAL_BITS-8);
516
8.30M
                    if (tmp >= 8388607)
517
12.3k
                    {
518
12.3k
                        tmp = 8388607;
519
12.3k
                    }
520
8.30M
                } else {
521
902k
                    tmp += -(1 << (REAL_BITS-9));
522
902k
                    tmp >>= (REAL_BITS-8);
523
902k
                    if (tmp <= -8388608)
524
11.9k
                    {
525
11.9k
                        tmp = -8388608;
526
11.9k
                    }
527
902k
                }
528
9.21M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
9.21M
            }
530
6.25k
            break;
531
4.88k
        case FAAD_FMT_32BIT:
532
4.88k
            exp = 16 - REAL_BITS;
533
4.88k
            half = 1 << (exp - 1);
534
4.88k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
6.85M
            for(i = 0; i < frame_len; i++)
536
6.84M
            {
537
6.84M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
6.84M
                    hDecoder->internal_channel);
539
6.84M
                if (tmp >= 0)
540
6.25M
                {
541
6.25M
                    tmp += half;
542
6.25M
                } else {
543
586k
                    tmp += -half;
544
586k
                }
545
6.84M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
6.84M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
6.84M
            }
548
4.88k
            break;
549
3.62k
        case FAAD_FMT_FIXED:
550
5.24M
            for(i = 0; i < frame_len; i++)
551
5.23M
            {
552
5.23M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
5.23M
                    hDecoder->internal_channel);
554
5.23M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
5.23M
            }
556
3.62k
            break;
557
20.9k
        }
558
20.9k
    }
559
560
3.51k
    return sample_buffer;
561
3.51k
}
562
563
#endif