Coverage Report

Created: 2025-07-11 06:40

/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
11.8M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.17M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
4.35M
#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
39.5M
{
48
39.5M
    if (!down_matrix)
49
37.3M
        return input[internal_channel[channel]][sample];
50
51
2.17M
    if (channel == 0)
52
1.07M
    {
53
1.07M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.07M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.07M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.10M
    } else {
57
1.10M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.10M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.10M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.10M
    }
61
2.17M
}
62
63
#ifndef HAS_LRINTF
64
34.4M
#define CLIP(sample, max, min) \
65
34.4M
if (sample >= 0.0f)            \
66
29.8M
{                              \
67
29.8M
    sample += 0.5f;            \
68
29.8M
    if (sample >= max)         \
69
29.8M
        sample = max;          \
70
29.8M
} else {                       \
71
4.61M
    sample += -0.5f;           \
72
4.61M
    if (sample <= min)         \
73
4.61M
        sample = min;          \
74
4.61M
}
75
#else
76
#define CLIP(sample, max, min) \
77
if (sample >= 0.0f)            \
78
{                              \
79
    if (sample >= max)         \
80
        sample = max;          \
81
} else {                       \
82
    if (sample <= min)         \
83
        sample = min;          \
84
}
85
#endif
86
87
7.39k
#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.77k
{
93
1.77k
    uint8_t ch, ch1;
94
1.77k
    uint16_t i;
95
96
1.77k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.77k
    {
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
920
    case CONV(2,0):
110
920
        if (hDecoder->upMatrix)
111
52
        {
112
52
            ch  = hDecoder->internal_channel[0];
113
75.5k
            for(i = 0; i < frame_len; i++)
114
75.5k
            {
115
75.5k
                real_t inp0 = input[ch][i];
116
117
75.5k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
75.5k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
75.5k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
75.5k
            }
122
868
        } else {
123
868
            ch  = hDecoder->internal_channel[0];
124
868
            ch1 = hDecoder->internal_channel[1];
125
1.39M
            for(i = 0; i < frame_len; i++)
126
1.39M
            {
127
1.39M
                real_t inp0 = input[ch ][i];
128
1.39M
                real_t inp1 = input[ch1][i];
129
130
1.39M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.39M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.39M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.39M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.39M
            }
136
868
        }
137
920
        break;
138
855
    default:
139
9.72k
        for (ch = 0; ch < channels; ch++)
140
8.87k
        {
141
12.7M
            for(i = 0; i < frame_len; i++)
142
12.7M
            {
143
12.7M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
12.7M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
12.7M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
12.7M
            }
149
8.87k
        }
150
855
        break;
151
1.77k
    }
152
1.77k
}
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.10k
{
158
1.10k
    uint8_t ch, ch1;
159
1.10k
    uint16_t i;
160
161
1.10k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.10k
    {
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
486
    case CONV(2,0):
176
486
        if (hDecoder->upMatrix)
177
44
        {
178
44
            ch = hDecoder->internal_channel[0];
179
69.1k
            for(i = 0; i < frame_len; i++)
180
69.1k
            {
181
69.1k
                real_t inp0 = input[ch][i];
182
183
69.1k
                inp0 *= 256.0f;
184
69.1k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
69.1k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
69.1k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
69.1k
            }
189
442
        } else {
190
442
            ch  = hDecoder->internal_channel[0];
191
442
            ch1 = hDecoder->internal_channel[1];
192
660k
            for(i = 0; i < frame_len; i++)
193
660k
            {
194
660k
                real_t inp0 = input[ch ][i];
195
660k
                real_t inp1 = input[ch1][i];
196
197
660k
                inp0 *= 256.0f;
198
660k
                inp1 *= 256.0f;
199
660k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
660k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
660k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
660k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
660k
            }
205
442
        }
206
486
        break;
207
618
    default:
208
6.62k
        for (ch = 0; ch < channels; ch++)
209
6.00k
        {
210
8.83M
            for(i = 0; i < frame_len; i++)
211
8.83M
            {
212
8.83M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.83M
                inp *= 256.0f;
215
8.83M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.83M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.83M
            }
219
6.00k
        }
220
618
        break;
221
1.10k
    }
222
1.10k
}
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.08k
{
228
1.08k
    uint8_t ch, ch1;
229
1.08k
    uint16_t i;
230
231
1.08k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.08k
    {
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
562
    case CONV(2,0):
246
562
        if (hDecoder->upMatrix)
247
46
        {
248
46
            ch = hDecoder->internal_channel[0];
249
70.1k
            for(i = 0; i < frame_len; i++)
250
70.1k
            {
251
70.1k
                real_t inp0 = input[ch][i];
252
253
70.1k
                inp0 *= 65536.0f;
254
70.1k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
70.1k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
70.1k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
70.1k
            }
259
516
        } else {
260
516
            ch  = hDecoder->internal_channel[0];
261
516
            ch1 = hDecoder->internal_channel[1];
262
798k
            for(i = 0; i < frame_len; i++)
263
797k
            {
264
797k
                real_t inp0 = input[ch ][i];
265
797k
                real_t inp1 = input[ch1][i];
266
267
797k
                inp0 *= 65536.0f;
268
797k
                inp1 *= 65536.0f;
269
797k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
797k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
797k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
797k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
797k
            }
275
516
        }
276
562
        break;
277
524
    default:
278
5.42k
        for (ch = 0; ch < channels; ch++)
279
4.90k
        {
280
6.91M
            for(i = 0; i < frame_len; i++)
281
6.91M
            {
282
6.91M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
6.91M
                inp *= 65536.0f;
285
6.91M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
6.91M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
6.91M
            }
289
4.90k
        }
290
524
        break;
291
1.08k
    }
292
1.08k
}
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
665
{
298
665
    uint8_t ch, ch1;
299
665
    uint16_t i;
300
301
665
    switch (CONV(channels,hDecoder->downMatrix))
302
665
    {
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
158
    case CONV(2,0):
312
158
        if (hDecoder->upMatrix)
313
17
        {
314
17
            ch = hDecoder->internal_channel[0];
315
27.2k
            for(i = 0; i < frame_len; i++)
316
27.2k
            {
317
27.2k
                real_t inp0 = input[ch][i];
318
27.2k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
27.2k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
27.2k
            }
321
141
        } else {
322
141
            ch  = hDecoder->internal_channel[0];
323
141
            ch1 = hDecoder->internal_channel[1];
324
203k
            for(i = 0; i < frame_len; i++)
325
203k
            {
326
203k
                real_t inp0 = input[ch ][i];
327
203k
                real_t inp1 = input[ch1][i];
328
203k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
203k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
203k
            }
331
141
        }
332
158
        break;
333
507
    default:
334
4.80k
        for (ch = 0; ch < channels; ch++)
335
4.29k
        {
336
5.90M
            for(i = 0; i < frame_len; i++)
337
5.90M
            {
338
5.90M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
5.90M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
5.90M
            }
341
4.29k
        }
342
507
        break;
343
665
    }
344
665
}
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
495
{
350
495
    uint8_t ch, ch1;
351
495
    uint16_t i;
352
353
495
    switch (CONV(channels,hDecoder->downMatrix))
354
495
    {
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
141
    case CONV(2,0):
364
141
        if (hDecoder->upMatrix)
365
15
        {
366
15
            ch = hDecoder->internal_channel[0];
367
26.2k
            for(i = 0; i < frame_len; i++)
368
26.2k
            {
369
26.2k
                real_t inp0 = input[ch][i];
370
26.2k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
26.2k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
26.2k
            }
373
126
        } else {
374
126
            ch  = hDecoder->internal_channel[0];
375
126
            ch1 = hDecoder->internal_channel[1];
376
159k
            for(i = 0; i < frame_len; i++)
377
159k
            {
378
159k
                real_t inp0 = input[ch ][i];
379
159k
                real_t inp1 = input[ch1][i];
380
159k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
159k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
159k
            }
383
126
        }
384
141
        break;
385
354
    default:
386
3.90k
        for (ch = 0; ch < channels; ch++)
387
3.54k
        {
388
5.15M
            for(i = 0; i < frame_len; i++)
389
5.15M
            {
390
5.15M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
5.15M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
5.15M
            }
393
3.54k
        }
394
354
        break;
395
495
    }
396
495
}
397
398
void *output_to_PCM(NeAACDecStruct *hDecoder,
399
                    real_t **input, void *sample_buffer, uint8_t channels,
400
                    uint16_t frame_len, uint8_t format)
401
10.2k
{
402
10.2k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
10.2k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
10.2k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
10.2k
    double    *double_sample_buffer = (double*)sample_buffer;
406
407
#ifdef PROFILE
408
    int64_t count = faad_get_ts();
409
#endif
410
411
    /* Copy output to a standard PCM buffer */
412
10.2k
    switch (format)
413
10.2k
    {
414
3.55k
    case FAAD_FMT_16BIT:
415
3.55k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.55k
        break;
417
2.20k
    case FAAD_FMT_24BIT:
418
2.20k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.20k
        break;
420
2.17k
    case FAAD_FMT_32BIT:
421
2.17k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.17k
        break;
423
1.33k
    case FAAD_FMT_FLOAT:
424
1.33k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.33k
        break;
426
990
    case FAAD_FMT_DOUBLE:
427
990
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
990
        break;
429
10.2k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
10.2k
    return sample_buffer;
437
10.2k
}
output_to_PCM
Line
Count
Source
401
5.12k
{
402
5.12k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.12k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.12k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.12k
    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.12k
    switch (format)
413
5.12k
    {
414
1.77k
    case FAAD_FMT_16BIT:
415
1.77k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.77k
        break;
417
1.10k
    case FAAD_FMT_24BIT:
418
1.10k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.10k
        break;
420
1.08k
    case FAAD_FMT_32BIT:
421
1.08k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.08k
        break;
423
665
    case FAAD_FMT_FLOAT:
424
665
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
665
        break;
426
495
    case FAAD_FMT_DOUBLE:
427
495
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
495
        break;
429
5.12k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.12k
    return sample_buffer;
437
5.12k
}
output_to_PCM
Line
Count
Source
401
5.12k
{
402
5.12k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.12k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.12k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.12k
    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.12k
    switch (format)
413
5.12k
    {
414
1.77k
    case FAAD_FMT_16BIT:
415
1.77k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.77k
        break;
417
1.10k
    case FAAD_FMT_24BIT:
418
1.10k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.10k
        break;
420
1.08k
    case FAAD_FMT_32BIT:
421
1.08k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.08k
        break;
423
665
    case FAAD_FMT_FLOAT:
424
665
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
665
        break;
426
495
    case FAAD_FMT_DOUBLE:
427
495
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
495
        break;
429
5.12k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.12k
    return sample_buffer;
437
5.12k
}
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
24.5M
{
449
24.5M
    real_t C;
450
24.5M
    if (up_matrix == 1)
451
204k
        return input[internal_channel[0]][sample];
452
453
24.3M
    if (!down_matrix)
454
24.0M
        return input[internal_channel[channel]][sample];
455
456
322k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
322k
    if (channel == 0)
458
153k
    {
459
153k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
153k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
153k
        return core + C + L_S;
462
168k
    } else {
463
168k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
168k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
168k
        return core + C + R_S;
466
168k
    }
467
322k
}
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.59k
{
473
5.59k
    uint8_t ch;
474
5.59k
    uint16_t i;
475
5.59k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
5.59k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
5.59k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
39.0k
    for (ch = 0; ch < channels; ch++)
481
33.4k
    {
482
33.4k
        switch (format)
483
33.4k
        {
484
11.3k
        case FAAD_FMT_16BIT:
485
15.7M
            for(i = 0; i < frame_len; i++)
486
15.7M
            {
487
15.7M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
15.7M
                    hDecoder->internal_channel);
489
15.7M
                if (tmp >= 0)
490
14.5M
                {
491
14.5M
                    tmp += (1 << (REAL_BITS-1));
492
14.5M
                    if (tmp >= REAL_CONST(32767))
493
24.5k
                    {
494
24.5k
                        tmp = REAL_CONST(32767);
495
24.5k
                    }
496
14.5M
                } else {
497
1.19M
                    tmp += -(1 << (REAL_BITS-1));
498
1.19M
                    if (tmp <= REAL_CONST(-32768))
499
25.1k
                    {
500
25.1k
                        tmp = REAL_CONST(-32768);
501
25.1k
                    }
502
1.19M
                }
503
15.7M
                tmp >>= REAL_BITS;
504
15.7M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
15.7M
            }
506
11.3k
            break;
507
10.1k
        case FAAD_FMT_24BIT:
508
16.1M
            for(i = 0; i < frame_len; i++)
509
16.1M
            {
510
16.1M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
16.1M
                    hDecoder->internal_channel);
512
16.1M
                if (tmp >= 0)
513
14.9M
                {
514
14.9M
                    tmp += (1 << (REAL_BITS-9));
515
14.9M
                    tmp >>= (REAL_BITS-8);
516
14.9M
                    if (tmp >= 8388607)
517
27.1k
                    {
518
27.1k
                        tmp = 8388607;
519
27.1k
                    }
520
14.9M
                } else {
521
1.15M
                    tmp += -(1 << (REAL_BITS-9));
522
1.15M
                    tmp >>= (REAL_BITS-8);
523
1.15M
                    if (tmp <= -8388608)
524
26.3k
                    {
525
26.3k
                        tmp = -8388608;
526
26.3k
                    }
527
1.15M
                }
528
16.1M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
16.1M
            }
530
10.1k
            break;
531
6.35k
        case FAAD_FMT_32BIT:
532
6.35k
            exp = 16 - REAL_BITS;
533
6.35k
            half = 1 << (exp - 1);
534
6.35k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
8.87M
            for(i = 0; i < frame_len; i++)
536
8.87M
            {
537
8.87M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
8.87M
                    hDecoder->internal_channel);
539
8.87M
                if (tmp >= 0)
540
8.16M
                {
541
8.16M
                    tmp += half;
542
8.16M
                } else {
543
703k
                    tmp += -half;
544
703k
                }
545
8.87M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
8.87M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
8.87M
            }
548
6.35k
            break;
549
5.61k
        case FAAD_FMT_FIXED:
550
8.37M
            for(i = 0; i < frame_len; i++)
551
8.37M
            {
552
8.37M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
8.37M
                    hDecoder->internal_channel);
554
8.37M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
8.37M
            }
556
5.61k
            break;
557
33.4k
        }
558
33.4k
    }
559
560
5.59k
    return sample_buffer;
561
5.59k
}
output_to_PCM
Line
Count
Source
472
2.79k
{
473
2.79k
    uint8_t ch;
474
2.79k
    uint16_t i;
475
2.79k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.79k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.79k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
19.5k
    for (ch = 0; ch < channels; ch++)
481
16.7k
    {
482
16.7k
        switch (format)
483
16.7k
        {
484
5.66k
        case FAAD_FMT_16BIT:
485
7.89M
            for(i = 0; i < frame_len; i++)
486
7.89M
            {
487
7.89M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
7.89M
                    hDecoder->internal_channel);
489
7.89M
                if (tmp >= 0)
490
7.29M
                {
491
7.29M
                    tmp += (1 << (REAL_BITS-1));
492
7.29M
                    if (tmp >= REAL_CONST(32767))
493
12.2k
                    {
494
12.2k
                        tmp = REAL_CONST(32767);
495
12.2k
                    }
496
7.29M
                } else {
497
595k
                    tmp += -(1 << (REAL_BITS-1));
498
595k
                    if (tmp <= REAL_CONST(-32768))
499
12.5k
                    {
500
12.5k
                        tmp = REAL_CONST(-32768);
501
12.5k
                    }
502
595k
                }
503
7.89M
                tmp >>= REAL_BITS;
504
7.89M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
7.89M
            }
506
5.66k
            break;
507
5.09k
        case FAAD_FMT_24BIT:
508
8.06M
            for(i = 0; i < frame_len; i++)
509
8.05M
            {
510
8.05M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
8.05M
                    hDecoder->internal_channel);
512
8.05M
                if (tmp >= 0)
513
7.47M
                {
514
7.47M
                    tmp += (1 << (REAL_BITS-9));
515
7.47M
                    tmp >>= (REAL_BITS-8);
516
7.47M
                    if (tmp >= 8388607)
517
13.5k
                    {
518
13.5k
                        tmp = 8388607;
519
13.5k
                    }
520
7.47M
                } else {
521
578k
                    tmp += -(1 << (REAL_BITS-9));
522
578k
                    tmp >>= (REAL_BITS-8);
523
578k
                    if (tmp <= -8388608)
524
13.1k
                    {
525
13.1k
                        tmp = -8388608;
526
13.1k
                    }
527
578k
                }
528
8.05M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
8.05M
            }
530
5.09k
            break;
531
3.17k
        case FAAD_FMT_32BIT:
532
3.17k
            exp = 16 - REAL_BITS;
533
3.17k
            half = 1 << (exp - 1);
534
3.17k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
4.43M
            for(i = 0; i < frame_len; i++)
536
4.43M
            {
537
4.43M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
4.43M
                    hDecoder->internal_channel);
539
4.43M
                if (tmp >= 0)
540
4.08M
                {
541
4.08M
                    tmp += half;
542
4.08M
                } else {
543
351k
                    tmp += -half;
544
351k
                }
545
4.43M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
4.43M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
4.43M
            }
548
3.17k
            break;
549
2.80k
        case FAAD_FMT_FIXED:
550
4.18M
            for(i = 0; i < frame_len; i++)
551
4.18M
            {
552
4.18M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.18M
                    hDecoder->internal_channel);
554
4.18M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.18M
            }
556
2.80k
            break;
557
16.7k
        }
558
16.7k
    }
559
560
2.79k
    return sample_buffer;
561
2.79k
}
output_to_PCM
Line
Count
Source
472
2.79k
{
473
2.79k
    uint8_t ch;
474
2.79k
    uint16_t i;
475
2.79k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.79k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.79k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
19.5k
    for (ch = 0; ch < channels; ch++)
481
16.7k
    {
482
16.7k
        switch (format)
483
16.7k
        {
484
5.66k
        case FAAD_FMT_16BIT:
485
7.89M
            for(i = 0; i < frame_len; i++)
486
7.89M
            {
487
7.89M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
7.89M
                    hDecoder->internal_channel);
489
7.89M
                if (tmp >= 0)
490
7.29M
                {
491
7.29M
                    tmp += (1 << (REAL_BITS-1));
492
7.29M
                    if (tmp >= REAL_CONST(32767))
493
12.2k
                    {
494
12.2k
                        tmp = REAL_CONST(32767);
495
12.2k
                    }
496
7.29M
                } else {
497
595k
                    tmp += -(1 << (REAL_BITS-1));
498
595k
                    if (tmp <= REAL_CONST(-32768))
499
12.5k
                    {
500
12.5k
                        tmp = REAL_CONST(-32768);
501
12.5k
                    }
502
595k
                }
503
7.89M
                tmp >>= REAL_BITS;
504
7.89M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
7.89M
            }
506
5.66k
            break;
507
5.09k
        case FAAD_FMT_24BIT:
508
8.06M
            for(i = 0; i < frame_len; i++)
509
8.05M
            {
510
8.05M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
8.05M
                    hDecoder->internal_channel);
512
8.05M
                if (tmp >= 0)
513
7.47M
                {
514
7.47M
                    tmp += (1 << (REAL_BITS-9));
515
7.47M
                    tmp >>= (REAL_BITS-8);
516
7.47M
                    if (tmp >= 8388607)
517
13.5k
                    {
518
13.5k
                        tmp = 8388607;
519
13.5k
                    }
520
7.47M
                } else {
521
578k
                    tmp += -(1 << (REAL_BITS-9));
522
578k
                    tmp >>= (REAL_BITS-8);
523
578k
                    if (tmp <= -8388608)
524
13.1k
                    {
525
13.1k
                        tmp = -8388608;
526
13.1k
                    }
527
578k
                }
528
8.05M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
8.05M
            }
530
5.09k
            break;
531
3.17k
        case FAAD_FMT_32BIT:
532
3.17k
            exp = 16 - REAL_BITS;
533
3.17k
            half = 1 << (exp - 1);
534
3.17k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
4.43M
            for(i = 0; i < frame_len; i++)
536
4.43M
            {
537
4.43M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
4.43M
                    hDecoder->internal_channel);
539
4.43M
                if (tmp >= 0)
540
4.08M
                {
541
4.08M
                    tmp += half;
542
4.08M
                } else {
543
351k
                    tmp += -half;
544
351k
                }
545
4.43M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
4.43M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
4.43M
            }
548
3.17k
            break;
549
2.80k
        case FAAD_FMT_FIXED:
550
4.18M
            for(i = 0; i < frame_len; i++)
551
4.18M
            {
552
4.18M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.18M
                    hDecoder->internal_channel);
554
4.18M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.18M
            }
556
2.80k
            break;
557
16.7k
        }
558
16.7k
    }
559
560
2.79k
    return sample_buffer;
561
2.79k
}
562
563
#endif