Coverage Report

Created: 2025-08-03 06:05

/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
13.4M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.54M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
5.09M
#define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
43
44
45
static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
46
                                uint8_t down_matrix, uint8_t *internal_channel)
47
41.4M
{
48
41.4M
    if (!down_matrix)
49
38.9M
        return input[internal_channel[channel]][sample];
50
51
2.54M
    if (channel == 0)
52
1.26M
    {
53
1.26M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.26M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.26M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.28M
    } else {
57
1.28M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.28M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.28M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.28M
    }
61
2.54M
}
62
63
#ifndef HAS_LRINTF
64
35.5M
#define CLIP(sample, max, min) \
65
35.5M
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.86M
    sample += -0.5f;           \
72
4.86M
    if (sample <= min)         \
73
4.86M
        sample = min;          \
74
4.86M
}
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.34k
#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.87k
{
93
1.87k
    uint8_t ch, ch1;
94
1.87k
    uint16_t i;
95
96
1.87k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.87k
    {
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
943
    case CONV(2,0):
110
943
        if (hDecoder->upMatrix)
111
56
        {
112
56
            ch  = hDecoder->internal_channel[0];
113
91.7k
            for(i = 0; i < frame_len; i++)
114
91.6k
            {
115
91.6k
                real_t inp0 = input[ch][i];
116
117
91.6k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
91.6k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
91.6k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
91.6k
            }
122
887
        } else {
123
887
            ch  = hDecoder->internal_channel[0];
124
887
            ch1 = hDecoder->internal_channel[1];
125
1.38M
            for(i = 0; i < frame_len; i++)
126
1.38M
            {
127
1.38M
                real_t inp0 = input[ch ][i];
128
1.38M
                real_t inp1 = input[ch1][i];
129
130
1.38M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.38M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.38M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.38M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.38M
            }
136
887
        }
137
943
        break;
138
934
    default:
139
10.5k
        for (ch = 0; ch < channels; ch++)
140
9.57k
        {
141
13.4M
            for(i = 0; i < frame_len; i++)
142
13.4M
            {
143
13.4M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
13.4M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
13.4M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
13.4M
            }
149
9.57k
        }
150
934
        break;
151
1.87k
    }
152
1.87k
}
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.30k
{
158
1.30k
    uint8_t ch, ch1;
159
1.30k
    uint16_t i;
160
161
1.30k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.30k
    {
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
623
    case CONV(2,0):
176
623
        if (hDecoder->upMatrix)
177
53
        {
178
53
            ch = hDecoder->internal_channel[0];
179
84.2k
            for(i = 0; i < frame_len; i++)
180
84.2k
            {
181
84.2k
                real_t inp0 = input[ch][i];
182
183
84.2k
                inp0 *= 256.0f;
184
84.2k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
84.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
84.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
84.2k
            }
189
570
        } else {
190
570
            ch  = hDecoder->internal_channel[0];
191
570
            ch1 = hDecoder->internal_channel[1];
192
849k
            for(i = 0; i < frame_len; i++)
193
849k
            {
194
849k
                real_t inp0 = input[ch ][i];
195
849k
                real_t inp1 = input[ch1][i];
196
197
849k
                inp0 *= 256.0f;
198
849k
                inp1 *= 256.0f;
199
849k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
849k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
849k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
849k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
849k
            }
205
570
        }
206
623
        break;
207
679
    default:
208
6.77k
        for (ch = 0; ch < channels; ch++)
209
6.09k
        {
210
8.67M
            for(i = 0; i < frame_len; i++)
211
8.66M
            {
212
8.66M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.66M
                inp *= 256.0f;
215
8.66M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.66M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.66M
            }
219
6.09k
        }
220
679
        break;
221
1.30k
    }
222
1.30k
}
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.25k
{
228
1.25k
    uint8_t ch, ch1;
229
1.25k
    uint16_t i;
230
231
1.25k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.25k
    {
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
680
    case CONV(2,0):
246
680
        if (hDecoder->upMatrix)
247
48
        {
248
48
            ch = hDecoder->internal_channel[0];
249
75.5k
            for(i = 0; i < frame_len; i++)
250
75.5k
            {
251
75.5k
                real_t inp0 = input[ch][i];
252
253
75.5k
                inp0 *= 65536.0f;
254
75.5k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
75.5k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
75.5k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
75.5k
            }
259
632
        } else {
260
632
            ch  = hDecoder->internal_channel[0];
261
632
            ch1 = hDecoder->internal_channel[1];
262
936k
            for(i = 0; i < frame_len; i++)
263
935k
            {
264
935k
                real_t inp0 = input[ch ][i];
265
935k
                real_t inp1 = input[ch1][i];
266
267
935k
                inp0 *= 65536.0f;
268
935k
                inp1 *= 65536.0f;
269
935k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
935k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
935k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
935k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
935k
            }
275
632
        }
276
680
        break;
277
572
    default:
278
5.42k
        for (ch = 0; ch < channels; ch++)
279
4.85k
        {
280
6.88M
            for(i = 0; i < frame_len; i++)
281
6.88M
            {
282
6.88M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
6.88M
                inp *= 65536.0f;
285
6.88M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
6.88M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
6.88M
            }
289
4.85k
        }
290
572
        break;
291
1.25k
    }
292
1.25k
}
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
752
{
298
752
    uint8_t ch, ch1;
299
752
    uint16_t i;
300
301
752
    switch (CONV(channels,hDecoder->downMatrix))
302
752
    {
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
177
    case CONV(2,0):
312
177
        if (hDecoder->upMatrix)
313
17
        {
314
17
            ch = hDecoder->internal_channel[0];
315
31.6k
            for(i = 0; i < frame_len; i++)
316
31.6k
            {
317
31.6k
                real_t inp0 = input[ch][i];
318
31.6k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
31.6k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
31.6k
            }
321
160
        } else {
322
160
            ch  = hDecoder->internal_channel[0];
323
160
            ch1 = hDecoder->internal_channel[1];
324
243k
            for(i = 0; i < frame_len; i++)
325
243k
            {
326
243k
                real_t inp0 = input[ch ][i];
327
243k
                real_t inp1 = input[ch1][i];
328
243k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
243k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
243k
            }
331
160
        }
332
177
        break;
333
575
    default:
334
5.36k
        for (ch = 0; ch < channels; ch++)
335
4.79k
        {
336
6.60M
            for(i = 0; i < frame_len; i++)
337
6.60M
            {
338
6.60M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
6.60M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
6.60M
            }
341
4.79k
        }
342
575
        break;
343
752
    }
344
752
}
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
568
{
350
568
    uint8_t ch, ch1;
351
568
    uint16_t i;
352
353
568
    switch (CONV(channels,hDecoder->downMatrix))
354
568
    {
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
173
    case CONV(2,0):
364
173
        if (hDecoder->upMatrix)
365
13
        {
366
13
            ch = hDecoder->internal_channel[0];
367
18.1k
            for(i = 0; i < frame_len; i++)
368
18.1k
            {
369
18.1k
                real_t inp0 = input[ch][i];
370
18.1k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
18.1k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
18.1k
            }
373
160
        } else {
374
160
            ch  = hDecoder->internal_channel[0];
375
160
            ch1 = hDecoder->internal_channel[1];
376
195k
            for(i = 0; i < frame_len; i++)
377
195k
            {
378
195k
                real_t inp0 = input[ch ][i];
379
195k
                real_t inp1 = input[ch1][i];
380
195k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
195k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
195k
            }
383
160
        }
384
173
        break;
385
395
    default:
386
4.45k
        for (ch = 0; ch < channels; ch++)
387
4.05k
        {
388
5.92M
            for(i = 0; i < frame_len; i++)
389
5.91M
            {
390
5.91M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
5.91M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
5.91M
            }
393
4.05k
        }
394
395
        break;
395
568
    }
396
568
}
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.5k
{
402
11.5k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
11.5k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
11.5k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
11.5k
    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.5k
    switch (format)
413
11.5k
    {
414
3.75k
    case FAAD_FMT_16BIT:
415
3.75k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.75k
        break;
417
2.60k
    case FAAD_FMT_24BIT:
418
2.60k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.60k
        break;
420
2.50k
    case FAAD_FMT_32BIT:
421
2.50k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.50k
        break;
423
1.50k
    case FAAD_FMT_FLOAT:
424
1.50k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.50k
        break;
426
1.13k
    case FAAD_FMT_DOUBLE:
427
1.13k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.13k
        break;
429
11.5k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
11.5k
    return sample_buffer;
437
11.5k
}
output_to_PCM
Line
Count
Source
401
5.75k
{
402
5.75k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.75k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.75k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.75k
    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.75k
    switch (format)
413
5.75k
    {
414
1.87k
    case FAAD_FMT_16BIT:
415
1.87k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.87k
        break;
417
1.30k
    case FAAD_FMT_24BIT:
418
1.30k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.30k
        break;
420
1.25k
    case FAAD_FMT_32BIT:
421
1.25k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.25k
        break;
423
752
    case FAAD_FMT_FLOAT:
424
752
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
752
        break;
426
568
    case FAAD_FMT_DOUBLE:
427
568
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
568
        break;
429
5.75k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.75k
    return sample_buffer;
437
5.75k
}
output_to_PCM
Line
Count
Source
401
5.75k
{
402
5.75k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.75k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.75k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.75k
    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.75k
    switch (format)
413
5.75k
    {
414
1.87k
    case FAAD_FMT_16BIT:
415
1.87k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.87k
        break;
417
1.30k
    case FAAD_FMT_24BIT:
418
1.30k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.30k
        break;
420
1.25k
    case FAAD_FMT_32BIT:
421
1.25k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.25k
        break;
423
752
    case FAAD_FMT_FLOAT:
424
752
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
752
        break;
426
568
    case FAAD_FMT_DOUBLE:
427
568
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
568
        break;
429
5.75k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.75k
    return sample_buffer;
437
5.75k
}
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.1M
{
449
30.1M
    real_t C;
450
30.1M
    if (up_matrix == 1)
451
322k
        return input[internal_channel[0]][sample];
452
453
29.8M
    if (!down_matrix)
454
29.0M
        return input[internal_channel[channel]][sample];
455
456
747k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
747k
    if (channel == 0)
458
361k
    {
459
361k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
361k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
361k
        return core + C + L_S;
462
385k
    } else {
463
385k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
385k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
385k
        return core + C + R_S;
466
385k
    }
467
747k
}
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.98k
{
473
6.98k
    uint8_t ch;
474
6.98k
    uint16_t i;
475
6.98k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
6.98k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
6.98k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
49.3k
    for (ch = 0; ch < channels; ch++)
481
42.3k
    {
482
42.3k
        switch (format)
483
42.3k
        {
484
12.0k
        case FAAD_FMT_16BIT:
485
17.0M
            for(i = 0; i < frame_len; i++)
486
16.9M
            {
487
16.9M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
16.9M
                    hDecoder->internal_channel);
489
16.9M
                if (tmp >= 0)
490
15.9M
                {
491
15.9M
                    tmp += (1 << (REAL_BITS-1));
492
15.9M
                    if (tmp >= REAL_CONST(32767))
493
26.3k
                    {
494
26.3k
                        tmp = REAL_CONST(32767);
495
26.3k
                    }
496
15.9M
                } else {
497
1.04M
                    tmp += -(1 << (REAL_BITS-1));
498
1.04M
                    if (tmp <= REAL_CONST(-32768))
499
30.7k
                    {
500
30.7k
                        tmp = REAL_CONST(-32768);
501
30.7k
                    }
502
1.04M
                }
503
16.9M
                tmp >>= REAL_BITS;
504
16.9M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
16.9M
            }
506
12.0k
            break;
507
12.1k
        case FAAD_FMT_24BIT:
508
17.8M
            for(i = 0; i < frame_len; i++)
509
17.8M
            {
510
17.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
17.8M
                    hDecoder->internal_channel);
512
17.8M
                if (tmp >= 0)
513
15.9M
                {
514
15.9M
                    tmp += (1 << (REAL_BITS-9));
515
15.9M
                    tmp >>= (REAL_BITS-8);
516
15.9M
                    if (tmp >= 8388607)
517
22.1k
                    {
518
22.1k
                        tmp = 8388607;
519
22.1k
                    }
520
15.9M
                } else {
521
1.84M
                    tmp += -(1 << (REAL_BITS-9));
522
1.84M
                    tmp >>= (REAL_BITS-8);
523
1.84M
                    if (tmp <= -8388608)
524
22.6k
                    {
525
22.6k
                        tmp = -8388608;
526
22.6k
                    }
527
1.84M
                }
528
17.8M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
17.8M
            }
530
12.1k
            break;
531
10.6k
        case FAAD_FMT_32BIT:
532
10.6k
            exp = 16 - REAL_BITS;
533
10.6k
            half = 1 << (exp - 1);
534
10.6k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
15.1M
            for(i = 0; i < frame_len; i++)
536
15.1M
            {
537
15.1M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
15.1M
                    hDecoder->internal_channel);
539
15.1M
                if (tmp >= 0)
540
13.9M
                {
541
13.9M
                    tmp += half;
542
13.9M
                } else {
543
1.14M
                    tmp += -half;
544
1.14M
                }
545
15.1M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
15.1M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
15.1M
            }
548
10.6k
            break;
549
7.38k
        case FAAD_FMT_FIXED:
550
10.3M
            for(i = 0; i < frame_len; i++)
551
10.3M
            {
552
10.3M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
10.3M
                    hDecoder->internal_channel);
554
10.3M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
10.3M
            }
556
7.38k
            break;
557
42.3k
        }
558
42.3k
    }
559
560
6.98k
    return sample_buffer;
561
6.98k
}
output_to_PCM
Line
Count
Source
472
3.49k
{
473
3.49k
    uint8_t ch;
474
3.49k
    uint16_t i;
475
3.49k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.49k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.49k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
24.6k
    for (ch = 0; ch < channels; ch++)
481
21.1k
    {
482
21.1k
        switch (format)
483
21.1k
        {
484
6.03k
        case FAAD_FMT_16BIT:
485
8.50M
            for(i = 0; i < frame_len; i++)
486
8.49M
            {
487
8.49M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.49M
                    hDecoder->internal_channel);
489
8.49M
                if (tmp >= 0)
490
7.97M
                {
491
7.97M
                    tmp += (1 << (REAL_BITS-1));
492
7.97M
                    if (tmp >= REAL_CONST(32767))
493
13.1k
                    {
494
13.1k
                        tmp = REAL_CONST(32767);
495
13.1k
                    }
496
7.97M
                } else {
497
523k
                    tmp += -(1 << (REAL_BITS-1));
498
523k
                    if (tmp <= REAL_CONST(-32768))
499
15.3k
                    {
500
15.3k
                        tmp = REAL_CONST(-32768);
501
15.3k
                    }
502
523k
                }
503
8.49M
                tmp >>= REAL_BITS;
504
8.49M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.49M
            }
506
6.03k
            break;
507
6.09k
        case FAAD_FMT_24BIT:
508
8.90M
            for(i = 0; i < frame_len; i++)
509
8.90M
            {
510
8.90M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
8.90M
                    hDecoder->internal_channel);
512
8.90M
                if (tmp >= 0)
513
7.97M
                {
514
7.97M
                    tmp += (1 << (REAL_BITS-9));
515
7.97M
                    tmp >>= (REAL_BITS-8);
516
7.97M
                    if (tmp >= 8388607)
517
11.0k
                    {
518
11.0k
                        tmp = 8388607;
519
11.0k
                    }
520
7.97M
                } else {
521
923k
                    tmp += -(1 << (REAL_BITS-9));
522
923k
                    tmp >>= (REAL_BITS-8);
523
923k
                    if (tmp <= -8388608)
524
11.3k
                    {
525
11.3k
                        tmp = -8388608;
526
11.3k
                    }
527
923k
                }
528
8.90M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
8.90M
            }
530
6.09k
            break;
531
5.34k
        case FAAD_FMT_32BIT:
532
5.34k
            exp = 16 - REAL_BITS;
533
5.34k
            half = 1 << (exp - 1);
534
5.34k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
7.57M
            for(i = 0; i < frame_len; i++)
536
7.56M
            {
537
7.56M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
7.56M
                    hDecoder->internal_channel);
539
7.56M
                if (tmp >= 0)
540
6.99M
                {
541
6.99M
                    tmp += half;
542
6.99M
                } else {
543
571k
                    tmp += -half;
544
571k
                }
545
7.56M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
7.56M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
7.56M
            }
548
5.34k
            break;
549
3.69k
        case FAAD_FMT_FIXED:
550
5.18M
            for(i = 0; i < frame_len; i++)
551
5.17M
            {
552
5.17M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
5.17M
                    hDecoder->internal_channel);
554
5.17M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
5.17M
            }
556
3.69k
            break;
557
21.1k
        }
558
21.1k
    }
559
560
3.49k
    return sample_buffer;
561
3.49k
}
output_to_PCM
Line
Count
Source
472
3.49k
{
473
3.49k
    uint8_t ch;
474
3.49k
    uint16_t i;
475
3.49k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.49k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.49k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
24.6k
    for (ch = 0; ch < channels; ch++)
481
21.1k
    {
482
21.1k
        switch (format)
483
21.1k
        {
484
6.03k
        case FAAD_FMT_16BIT:
485
8.50M
            for(i = 0; i < frame_len; i++)
486
8.49M
            {
487
8.49M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.49M
                    hDecoder->internal_channel);
489
8.49M
                if (tmp >= 0)
490
7.97M
                {
491
7.97M
                    tmp += (1 << (REAL_BITS-1));
492
7.97M
                    if (tmp >= REAL_CONST(32767))
493
13.1k
                    {
494
13.1k
                        tmp = REAL_CONST(32767);
495
13.1k
                    }
496
7.97M
                } else {
497
523k
                    tmp += -(1 << (REAL_BITS-1));
498
523k
                    if (tmp <= REAL_CONST(-32768))
499
15.3k
                    {
500
15.3k
                        tmp = REAL_CONST(-32768);
501
15.3k
                    }
502
523k
                }
503
8.49M
                tmp >>= REAL_BITS;
504
8.49M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.49M
            }
506
6.03k
            break;
507
6.09k
        case FAAD_FMT_24BIT:
508
8.90M
            for(i = 0; i < frame_len; i++)
509
8.90M
            {
510
8.90M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
8.90M
                    hDecoder->internal_channel);
512
8.90M
                if (tmp >= 0)
513
7.97M
                {
514
7.97M
                    tmp += (1 << (REAL_BITS-9));
515
7.97M
                    tmp >>= (REAL_BITS-8);
516
7.97M
                    if (tmp >= 8388607)
517
11.0k
                    {
518
11.0k
                        tmp = 8388607;
519
11.0k
                    }
520
7.97M
                } else {
521
923k
                    tmp += -(1 << (REAL_BITS-9));
522
923k
                    tmp >>= (REAL_BITS-8);
523
923k
                    if (tmp <= -8388608)
524
11.3k
                    {
525
11.3k
                        tmp = -8388608;
526
11.3k
                    }
527
923k
                }
528
8.90M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
8.90M
            }
530
6.09k
            break;
531
5.34k
        case FAAD_FMT_32BIT:
532
5.34k
            exp = 16 - REAL_BITS;
533
5.34k
            half = 1 << (exp - 1);
534
5.34k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
7.57M
            for(i = 0; i < frame_len; i++)
536
7.56M
            {
537
7.56M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
7.56M
                    hDecoder->internal_channel);
539
7.56M
                if (tmp >= 0)
540
6.99M
                {
541
6.99M
                    tmp += half;
542
6.99M
                } else {
543
571k
                    tmp += -half;
544
571k
                }
545
7.56M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
7.56M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
7.56M
            }
548
5.34k
            break;
549
3.69k
        case FAAD_FMT_FIXED:
550
5.18M
            for(i = 0; i < frame_len; i++)
551
5.17M
            {
552
5.17M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
5.17M
                    hDecoder->internal_channel);
554
5.17M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
5.17M
            }
556
3.69k
            break;
557
21.1k
        }
558
21.1k
    }
559
560
3.49k
    return sample_buffer;
561
3.49k
}
562
563
#endif