Coverage Report

Created: 2026-07-24 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/output.c
Line
Count
Source
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: output.c,v 1.47 2009/01/26 23:51:15 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include "output.h"
35
36
#ifndef FIXED_POINT
37
38
39
12.6M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.30M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
4.61M
#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.2M
{
48
39.2M
    if (!down_matrix)
49
36.9M
        return input[internal_channel[channel]][sample];
50
51
2.30M
    if (channel == 0)
52
1.14M
    {
53
1.14M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.14M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.14M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.16M
    } else {
57
1.16M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.16M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.16M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.16M
    }
61
2.30M
}
62
63
#ifndef HAS_LRINTF
64
33.5M
#define CLIP(sample, max, min) \
65
33.5M
if (sample >= 0.0f)            \
66
28.9M
{                              \
67
28.9M
    sample += 0.5f;            \
68
28.9M
    if (sample >= max)         \
69
28.9M
        sample = max;          \
70
28.9M
} else {                       \
71
4.64M
    sample += -0.5f;           \
72
4.64M
    if (sample <= min)         \
73
4.64M
        sample = min;          \
74
4.64M
}
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.51k
#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.63k
{
93
1.63k
    uint8_t ch, ch1;
94
1.63k
    uint16_t i;
95
96
1.63k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.63k
    {
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
862
    case CONV(2,0):
110
862
        if (hDecoder->upMatrix)
111
51
        {
112
51
            ch  = hDecoder->internal_channel[0];
113
81.0k
            for(i = 0; i < frame_len; i++)
114
81.0k
            {
115
81.0k
                real_t inp0 = input[ch][i];
116
117
81.0k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
81.0k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
81.0k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
81.0k
            }
122
811
        } else {
123
811
            ch  = hDecoder->internal_channel[0];
124
811
            ch1 = hDecoder->internal_channel[1];
125
1.22M
            for(i = 0; i < frame_len; i++)
126
1.22M
            {
127
1.22M
                real_t inp0 = input[ch ][i];
128
1.22M
                real_t inp1 = input[ch1][i];
129
130
1.22M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.22M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.22M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.22M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.22M
            }
136
811
        }
137
862
        break;
138
775
    default:
139
8.34k
        for (ch = 0; ch < channels; ch++)
140
7.56k
        {
141
11.1M
            for(i = 0; i < frame_len; i++)
142
11.1M
            {
143
11.1M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
11.1M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
11.1M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
11.1M
            }
149
7.56k
        }
150
775
        break;
151
1.63k
    }
152
1.63k
}
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.24k
{
158
1.24k
    uint8_t ch, ch1;
159
1.24k
    uint16_t i;
160
161
1.24k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.24k
    {
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
607
    case CONV(2,0):
176
607
        if (hDecoder->upMatrix)
177
37
        {
178
37
            ch = hDecoder->internal_channel[0];
179
56.1k
            for(i = 0; i < frame_len; i++)
180
56.0k
            {
181
56.0k
                real_t inp0 = input[ch][i];
182
183
56.0k
                inp0 *= 256.0f;
184
56.0k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
56.0k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
56.0k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
56.0k
            }
189
570
        } else {
190
570
            ch  = hDecoder->internal_channel[0];
191
570
            ch1 = hDecoder->internal_channel[1];
192
857k
            for(i = 0; i < frame_len; i++)
193
856k
            {
194
856k
                real_t inp0 = input[ch ][i];
195
856k
                real_t inp1 = input[ch1][i];
196
197
856k
                inp0 *= 256.0f;
198
856k
                inp1 *= 256.0f;
199
856k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
856k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
856k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
856k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
856k
            }
205
570
        }
206
607
        break;
207
640
    default:
208
7.07k
        for (ch = 0; ch < channels; ch++)
209
6.43k
        {
210
9.18M
            for(i = 0; i < frame_len; i++)
211
9.17M
            {
212
9.17M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
9.17M
                inp *= 256.0f;
215
9.17M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
9.17M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
9.17M
            }
219
6.43k
        }
220
640
        break;
221
1.24k
    }
222
1.24k
}
223
224
static void to_PCM_32bit(NeAACDecStruct *hDecoder, real_t **input,
225
                         uint8_t channels, uint16_t frame_len,
226
                         int32_t **sample_buffer)
227
1.18k
{
228
1.18k
    uint8_t ch, ch1;
229
1.18k
    uint16_t i;
230
231
1.18k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.18k
    {
233
0
    case CONV(1,0):
234
0
    case CONV(1,1):
235
0
        for(i = 0; i < frame_len; i++)
236
0
        {
237
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
238
239
0
            inp *= 65536.0f;
240
0
            CLIP(inp, 2147483647.0f, -2147483648.0f);
241
242
0
            (*sample_buffer)[i] = (int32_t)lrintf(inp);
243
0
        }
244
0
        break;
245
583
    case CONV(2,0):
246
583
        if (hDecoder->upMatrix)
247
44
        {
248
44
            ch = hDecoder->internal_channel[0];
249
67.1k
            for(i = 0; i < frame_len; i++)
250
67.0k
            {
251
67.0k
                real_t inp0 = input[ch][i];
252
253
67.0k
                inp0 *= 65536.0f;
254
67.0k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
67.0k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
67.0k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
67.0k
            }
259
539
        } else {
260
539
            ch  = hDecoder->internal_channel[0];
261
539
            ch1 = hDecoder->internal_channel[1];
262
850k
            for(i = 0; i < frame_len; i++)
263
850k
            {
264
850k
                real_t inp0 = input[ch ][i];
265
850k
                real_t inp1 = input[ch1][i];
266
267
850k
                inp0 *= 65536.0f;
268
850k
                inp1 *= 65536.0f;
269
850k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
850k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
850k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
850k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
850k
            }
275
539
        }
276
583
        break;
277
598
    default:
278
5.86k
        for (ch = 0; ch < channels; ch++)
279
5.26k
        {
280
7.21M
            for(i = 0; i < frame_len; i++)
281
7.20M
            {
282
7.20M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
7.20M
                inp *= 65536.0f;
285
7.20M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
7.20M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
7.20M
            }
289
5.26k
        }
290
598
        break;
291
1.18k
    }
292
1.18k
}
293
294
static void to_PCM_float(NeAACDecStruct *hDecoder, real_t **input,
295
                         uint8_t channels, uint16_t frame_len,
296
                         float32_t **sample_buffer)
297
578
{
298
578
    uint8_t ch, ch1;
299
578
    uint16_t i;
300
301
578
    switch (CONV(channels,hDecoder->downMatrix))
302
578
    {
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
182
    case CONV(2,0):
312
182
        if (hDecoder->upMatrix)
313
20
        {
314
20
            ch = hDecoder->internal_channel[0];
315
28.3k
            for(i = 0; i < frame_len; i++)
316
28.2k
            {
317
28.2k
                real_t inp0 = input[ch][i];
318
28.2k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
28.2k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
28.2k
            }
321
162
        } else {
322
162
            ch  = hDecoder->internal_channel[0];
323
162
            ch1 = hDecoder->internal_channel[1];
324
244k
            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
162
        }
332
182
        break;
333
396
    default:
334
3.58k
        for (ch = 0; ch < channels; ch++)
335
3.18k
        {
336
4.86M
            for(i = 0; i < frame_len; i++)
337
4.86M
            {
338
4.86M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
4.86M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
4.86M
            }
341
3.18k
        }
342
396
        break;
343
578
    }
344
578
}
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
517
{
350
517
    uint8_t ch, ch1;
351
517
    uint16_t i;
352
353
517
    switch (CONV(channels,hDecoder->downMatrix))
354
517
    {
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
125
    case CONV(2,0):
364
125
        if (hDecoder->upMatrix)
365
18
        {
366
18
            ch = hDecoder->internal_channel[0];
367
31.5k
            for(i = 0; i < frame_len; i++)
368
31.4k
            {
369
31.4k
                real_t inp0 = input[ch][i];
370
31.4k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
31.4k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
31.4k
            }
373
107
        } else {
374
107
            ch  = hDecoder->internal_channel[0];
375
107
            ch1 = hDecoder->internal_channel[1];
376
136k
            for(i = 0; i < frame_len; i++)
377
136k
            {
378
136k
                real_t inp0 = input[ch ][i];
379
136k
                real_t inp1 = input[ch1][i];
380
136k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
136k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
136k
            }
383
107
        }
384
125
        break;
385
392
    default:
386
4.61k
        for (ch = 0; ch < channels; ch++)
387
4.22k
        {
388
6.87M
            for(i = 0; i < frame_len; i++)
389
6.87M
            {
390
6.87M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
6.87M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
6.87M
            }
393
4.22k
        }
394
392
        break;
395
517
    }
396
517
}
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.3k
{
402
10.3k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
10.3k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
10.3k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
10.3k
    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.3k
    switch (format)
413
10.3k
    {
414
3.27k
    case FAAD_FMT_16BIT:
415
3.27k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.27k
        break;
417
2.49k
    case FAAD_FMT_24BIT:
418
2.49k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.49k
        break;
420
2.36k
    case FAAD_FMT_32BIT:
421
2.36k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.36k
        break;
423
1.15k
    case FAAD_FMT_FLOAT:
424
1.15k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.15k
        break;
426
1.03k
    case FAAD_FMT_DOUBLE:
427
1.03k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.03k
        break;
429
10.3k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
10.3k
    return sample_buffer;
437
10.3k
}
output_to_PCM
Line
Count
Source
401
5.16k
{
402
5.16k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.16k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.16k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.16k
    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.16k
    switch (format)
413
5.16k
    {
414
1.63k
    case FAAD_FMT_16BIT:
415
1.63k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.63k
        break;
417
1.24k
    case FAAD_FMT_24BIT:
418
1.24k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.24k
        break;
420
1.18k
    case FAAD_FMT_32BIT:
421
1.18k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.18k
        break;
423
578
    case FAAD_FMT_FLOAT:
424
578
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
578
        break;
426
517
    case FAAD_FMT_DOUBLE:
427
517
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
517
        break;
429
5.16k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.16k
    return sample_buffer;
437
5.16k
}
output_to_PCM
Line
Count
Source
401
5.16k
{
402
5.16k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.16k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.16k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.16k
    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.16k
    switch (format)
413
5.16k
    {
414
1.63k
    case FAAD_FMT_16BIT:
415
1.63k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.63k
        break;
417
1.24k
    case FAAD_FMT_24BIT:
418
1.24k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.24k
        break;
420
1.18k
    case FAAD_FMT_32BIT:
421
1.18k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.18k
        break;
423
578
    case FAAD_FMT_FLOAT:
424
578
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
578
        break;
426
517
    case FAAD_FMT_DOUBLE:
427
517
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
517
        break;
429
5.16k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.16k
    return sample_buffer;
437
5.16k
}
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
33.8M
{
449
33.8M
    real_t C;
450
33.8M
    if (up_matrix == 1)
451
324k
        return input[internal_channel[0]][sample];
452
453
33.5M
    if (!down_matrix)
454
32.6M
        return input[internal_channel[channel]][sample];
455
456
893k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
893k
    if (channel == 0)
458
434k
    {
459
434k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
434k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
434k
        return core + C + L_S;
462
458k
    } else {
463
458k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
458k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
458k
        return core + C + R_S;
466
458k
    }
467
893k
}
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.65k
{
473
7.65k
    uint8_t ch;
474
7.65k
    uint16_t i;
475
7.65k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
7.65k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
7.65k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
55.1k
    for (ch = 0; ch < channels; ch++)
481
47.5k
    {
482
47.5k
        switch (format)
483
47.5k
        {
484
14.7k
        case FAAD_FMT_16BIT:
485
22.8M
            for(i = 0; i < frame_len; i++)
486
22.8M
            {
487
22.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
22.8M
                    hDecoder->internal_channel);
489
22.8M
                if (tmp >= 0)
490
20.8M
                {
491
20.8M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
20.8M
                        tmp += (1 << (REAL_BITS-1));
493
20.8M
                    if (tmp >= REAL_CONST(32767))
494
77.6k
                    {
495
77.6k
                        tmp = REAL_CONST(32767);
496
77.6k
                    }
497
20.8M
                } else {
498
1.94M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
1.94M
                        tmp += -(1 << (REAL_BITS-1));
500
1.94M
                    if (tmp <= REAL_CONST(-32768))
501
79.0k
                    {
502
79.0k
                        tmp = REAL_CONST(-32768);
503
79.0k
                    }
504
1.94M
                }
505
22.8M
                tmp >>= REAL_BITS;
506
22.8M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
22.8M
            }
508
14.7k
            break;
509
15.2k
        case FAAD_FMT_24BIT:
510
21.6M
            for(i = 0; i < frame_len; i++)
511
21.6M
            {
512
21.6M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
21.6M
                    hDecoder->internal_channel);
514
21.6M
                if (tmp >= 0)
515
19.8M
                {
516
19.8M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
19.8M
                        tmp += (1 << (REAL_BITS-9));
518
19.8M
                    tmp >>= (REAL_BITS-8);
519
19.8M
                    if (tmp >= 8388607)
520
54.2k
                    {
521
54.2k
                        tmp = 8388607;
522
54.2k
                    }
523
19.8M
                } else {
524
1.78M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
1.78M
                        tmp += -(1 << (REAL_BITS-9));
526
1.78M
                    tmp >>= (REAL_BITS-8);
527
1.78M
                    if (tmp <= -8388608)
528
51.1k
                    {
529
51.1k
                        tmp = -8388608;
530
51.1k
                    }
531
1.78M
                }
532
21.6M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
21.6M
            }
534
15.2k
            break;
535
10.1k
        case FAAD_FMT_32BIT:
536
10.1k
            exp = 16 - REAL_BITS;
537
10.1k
            half = 1 << (exp - 1);
538
10.1k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
13.4M
            for(i = 0; i < frame_len; i++)
540
13.4M
            {
541
13.4M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
13.4M
                    hDecoder->internal_channel);
543
13.4M
                if (tmp >= 0)
544
12.4M
                {
545
12.4M
                    if (tmp <= 0x7FFFFFFF - half)
546
12.4M
                        tmp += half;
547
12.4M
                } else {
548
950k
                    if (tmp >= (int32_t)0x80000000 + half)
549
950k
                        tmp += -half;
550
950k
                }
551
13.4M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
13.4M
                int_sample_buffer[(i*channels)+ch] = tmp;
553
13.4M
            }
554
10.1k
            break;
555
7.33k
        case FAAD_FMT_FIXED:
556
9.77M
            for(i = 0; i < frame_len; i++)
557
9.76M
            {
558
9.76M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
9.76M
                    hDecoder->internal_channel);
560
9.76M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
9.76M
            }
562
7.33k
            break;
563
47.5k
        }
564
47.5k
    }
565
566
7.65k
    return sample_buffer;
567
7.65k
}
output_to_PCM
Line
Count
Source
472
3.82k
{
473
3.82k
    uint8_t ch;
474
3.82k
    uint16_t i;
475
3.82k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.82k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.82k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
27.5k
    for (ch = 0; ch < channels; ch++)
481
23.7k
    {
482
23.7k
        switch (format)
483
23.7k
        {
484
7.39k
        case FAAD_FMT_16BIT:
485
11.4M
            for(i = 0; i < frame_len; i++)
486
11.4M
            {
487
11.4M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
11.4M
                    hDecoder->internal_channel);
489
11.4M
                if (tmp >= 0)
490
10.4M
                {
491
10.4M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
10.4M
                        tmp += (1 << (REAL_BITS-1));
493
10.4M
                    if (tmp >= REAL_CONST(32767))
494
38.8k
                    {
495
38.8k
                        tmp = REAL_CONST(32767);
496
38.8k
                    }
497
10.4M
                } else {
498
972k
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
972k
                        tmp += -(1 << (REAL_BITS-1));
500
972k
                    if (tmp <= REAL_CONST(-32768))
501
39.5k
                    {
502
39.5k
                        tmp = REAL_CONST(-32768);
503
39.5k
                    }
504
972k
                }
505
11.4M
                tmp >>= REAL_BITS;
506
11.4M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
11.4M
            }
508
7.39k
            break;
509
7.64k
        case FAAD_FMT_24BIT:
510
10.8M
            for(i = 0; i < frame_len; i++)
511
10.8M
            {
512
10.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
10.8M
                    hDecoder->internal_channel);
514
10.8M
                if (tmp >= 0)
515
9.93M
                {
516
9.93M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
9.93M
                        tmp += (1 << (REAL_BITS-9));
518
9.93M
                    tmp >>= (REAL_BITS-8);
519
9.93M
                    if (tmp >= 8388607)
520
27.1k
                    {
521
27.1k
                        tmp = 8388607;
522
27.1k
                    }
523
9.93M
                } else {
524
891k
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
891k
                        tmp += -(1 << (REAL_BITS-9));
526
891k
                    tmp >>= (REAL_BITS-8);
527
891k
                    if (tmp <= -8388608)
528
25.5k
                    {
529
25.5k
                        tmp = -8388608;
530
25.5k
                    }
531
891k
                }
532
10.8M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
10.8M
            }
534
7.64k
            break;
535
5.06k
        case FAAD_FMT_32BIT:
536
5.06k
            exp = 16 - REAL_BITS;
537
5.06k
            half = 1 << (exp - 1);
538
5.06k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
6.71M
            for(i = 0; i < frame_len; i++)
540
6.71M
            {
541
6.71M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
6.71M
                    hDecoder->internal_channel);
543
6.71M
                if (tmp >= 0)
544
6.23M
                {
545
6.23M
                    if (tmp <= 0x7FFFFFFF - half)
546
6.23M
                        tmp += half;
547
6.23M
                } else {
548
475k
                    if (tmp >= (int32_t)0x80000000 + half)
549
475k
                        tmp += -half;
550
475k
                }
551
6.71M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
6.71M
                int_sample_buffer[(i*channels)+ch] = tmp;
553
6.71M
            }
554
5.06k
            break;
555
3.66k
        case FAAD_FMT_FIXED:
556
4.88M
            for(i = 0; i < frame_len; i++)
557
4.88M
            {
558
4.88M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
4.88M
                    hDecoder->internal_channel);
560
4.88M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
4.88M
            }
562
3.66k
            break;
563
23.7k
        }
564
23.7k
    }
565
566
3.82k
    return sample_buffer;
567
3.82k
}
output_to_PCM
Line
Count
Source
472
3.82k
{
473
3.82k
    uint8_t ch;
474
3.82k
    uint16_t i;
475
3.82k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.82k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.82k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
27.5k
    for (ch = 0; ch < channels; ch++)
481
23.7k
    {
482
23.7k
        switch (format)
483
23.7k
        {
484
7.39k
        case FAAD_FMT_16BIT:
485
11.4M
            for(i = 0; i < frame_len; i++)
486
11.4M
            {
487
11.4M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
11.4M
                    hDecoder->internal_channel);
489
11.4M
                if (tmp >= 0)
490
10.4M
                {
491
10.4M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
10.4M
                        tmp += (1 << (REAL_BITS-1));
493
10.4M
                    if (tmp >= REAL_CONST(32767))
494
38.8k
                    {
495
38.8k
                        tmp = REAL_CONST(32767);
496
38.8k
                    }
497
10.4M
                } else {
498
972k
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
972k
                        tmp += -(1 << (REAL_BITS-1));
500
972k
                    if (tmp <= REAL_CONST(-32768))
501
39.5k
                    {
502
39.5k
                        tmp = REAL_CONST(-32768);
503
39.5k
                    }
504
972k
                }
505
11.4M
                tmp >>= REAL_BITS;
506
11.4M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
11.4M
            }
508
7.39k
            break;
509
7.64k
        case FAAD_FMT_24BIT:
510
10.8M
            for(i = 0; i < frame_len; i++)
511
10.8M
            {
512
10.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
10.8M
                    hDecoder->internal_channel);
514
10.8M
                if (tmp >= 0)
515
9.93M
                {
516
9.93M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
9.93M
                        tmp += (1 << (REAL_BITS-9));
518
9.93M
                    tmp >>= (REAL_BITS-8);
519
9.93M
                    if (tmp >= 8388607)
520
27.1k
                    {
521
27.1k
                        tmp = 8388607;
522
27.1k
                    }
523
9.93M
                } else {
524
891k
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
891k
                        tmp += -(1 << (REAL_BITS-9));
526
891k
                    tmp >>= (REAL_BITS-8);
527
891k
                    if (tmp <= -8388608)
528
25.5k
                    {
529
25.5k
                        tmp = -8388608;
530
25.5k
                    }
531
891k
                }
532
10.8M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
10.8M
            }
534
7.64k
            break;
535
5.06k
        case FAAD_FMT_32BIT:
536
5.06k
            exp = 16 - REAL_BITS;
537
5.06k
            half = 1 << (exp - 1);
538
5.06k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
6.71M
            for(i = 0; i < frame_len; i++)
540
6.71M
            {
541
6.71M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
6.71M
                    hDecoder->internal_channel);
543
6.71M
                if (tmp >= 0)
544
6.23M
                {
545
6.23M
                    if (tmp <= 0x7FFFFFFF - half)
546
6.23M
                        tmp += half;
547
6.23M
                } else {
548
475k
                    if (tmp >= (int32_t)0x80000000 + half)
549
475k
                        tmp += -half;
550
475k
                }
551
6.71M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
6.71M
                int_sample_buffer[(i*channels)+ch] = tmp;
553
6.71M
            }
554
5.06k
            break;
555
3.66k
        case FAAD_FMT_FIXED:
556
4.88M
            for(i = 0; i < frame_len; i++)
557
4.88M
            {
558
4.88M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
4.88M
                    hDecoder->internal_channel);
560
4.88M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
4.88M
            }
562
3.66k
            break;
563
23.7k
        }
564
23.7k
    }
565
566
3.82k
    return sample_buffer;
567
3.82k
}
568
569
#endif