Coverage Report

Created: 2026-09-14 06:52

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
13.8M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.84M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
5.68M
#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.0M
{
48
41.0M
    if (!down_matrix)
49
38.1M
        return input[internal_channel[channel]][sample];
50
51
2.84M
    if (channel == 0)
52
1.40M
    {
53
1.40M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.40M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.40M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.43M
    } else {
57
1.43M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.43M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.43M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.43M
    }
61
2.84M
}
62
63
#ifndef HAS_LRINTF
64
35.0M
#define CLIP(sample, max, min) \
65
35.0M
if (sample >= 0.0f)            \
66
30.5M
{                              \
67
30.5M
    sample += 0.5f;            \
68
30.5M
    if (sample >= max)         \
69
30.5M
        sample = max;          \
70
30.5M
} else {                       \
71
4.48M
    sample += -0.5f;           \
72
4.48M
    if (sample <= min)         \
73
4.48M
        sample = min;          \
74
4.48M
}
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.53k
#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.89k
{
93
1.89k
    uint8_t ch, ch1;
94
1.89k
    uint16_t i;
95
96
1.89k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.89k
    {
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
1.06k
    case CONV(2,0):
110
1.06k
        if (hDecoder->upMatrix)
111
63
        {
112
63
            ch  = hDecoder->internal_channel[0];
113
97.0k
            for(i = 0; i < frame_len; i++)
114
96.9k
            {
115
96.9k
                real_t inp0 = input[ch][i];
116
117
96.9k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
96.9k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
96.9k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
96.9k
            }
122
999
        } else {
123
999
            ch  = hDecoder->internal_channel[0];
124
999
            ch1 = hDecoder->internal_channel[1];
125
1.52M
            for(i = 0; i < frame_len; i++)
126
1.52M
            {
127
1.52M
                real_t inp0 = input[ch ][i];
128
1.52M
                real_t inp1 = input[ch1][i];
129
130
1.52M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.52M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.52M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.52M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.52M
            }
136
999
        }
137
1.06k
        break;
138
829
    default:
139
8.44k
        for (ch = 0; ch < channels; ch++)
140
7.61k
        {
141
11.4M
            for(i = 0; i < frame_len; i++)
142
11.4M
            {
143
11.4M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
11.4M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
11.4M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
11.4M
            }
149
7.61k
        }
150
829
        break;
151
1.89k
    }
152
1.89k
}
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.38k
{
158
1.38k
    uint8_t ch, ch1;
159
1.38k
    uint16_t i;
160
161
1.38k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.38k
    {
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
622
    case CONV(2,0):
176
622
        if (hDecoder->upMatrix)
177
35
        {
178
35
            ch = hDecoder->internal_channel[0];
179
57.2k
            for(i = 0; i < frame_len; i++)
180
57.2k
            {
181
57.2k
                real_t inp0 = input[ch][i];
182
183
57.2k
                inp0 *= 256.0f;
184
57.2k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
57.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
57.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
57.2k
            }
189
587
        } else {
190
587
            ch  = hDecoder->internal_channel[0];
191
587
            ch1 = hDecoder->internal_channel[1];
192
855k
            for(i = 0; i < frame_len; i++)
193
854k
            {
194
854k
                real_t inp0 = input[ch ][i];
195
854k
                real_t inp1 = input[ch1][i];
196
197
854k
                inp0 *= 256.0f;
198
854k
                inp1 *= 256.0f;
199
854k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
854k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
854k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
854k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
854k
            }
205
587
        }
206
622
        break;
207
765
    default:
208
7.01k
        for (ch = 0; ch < channels; ch++)
209
6.24k
        {
210
9.00M
            for(i = 0; i < frame_len; i++)
211
8.99M
            {
212
8.99M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.99M
                inp *= 256.0f;
215
8.99M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.99M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.99M
            }
219
6.24k
        }
220
765
        break;
221
1.38k
    }
222
1.38k
}
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.37k
{
228
1.37k
    uint8_t ch, ch1;
229
1.37k
    uint16_t i;
230
231
1.37k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.37k
    {
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
686
    case CONV(2,0):
246
686
        if (hDecoder->upMatrix)
247
37
        {
248
37
            ch = hDecoder->internal_channel[0];
249
59.9k
            for(i = 0; i < frame_len; i++)
250
59.9k
            {
251
59.9k
                real_t inp0 = input[ch][i];
252
253
59.9k
                inp0 *= 65536.0f;
254
59.9k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
59.9k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
59.9k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
59.9k
            }
259
649
        } else {
260
649
            ch  = hDecoder->internal_channel[0];
261
649
            ch1 = hDecoder->internal_channel[1];
262
988k
            for(i = 0; i < frame_len; i++)
263
987k
            {
264
987k
                real_t inp0 = input[ch ][i];
265
987k
                real_t inp1 = input[ch1][i];
266
267
987k
                inp0 *= 65536.0f;
268
987k
                inp1 *= 65536.0f;
269
987k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
987k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
987k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
987k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
987k
            }
275
649
        }
276
686
        break;
277
692
    default:
278
6.39k
        for (ch = 0; ch < channels; ch++)
279
5.70k
        {
280
7.67M
            for(i = 0; i < frame_len; i++)
281
7.67M
            {
282
7.67M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
7.67M
                inp *= 65536.0f;
285
7.67M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
7.67M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
7.67M
            }
289
5.70k
        }
290
692
        break;
291
1.37k
    }
292
1.37k
}
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
640
{
298
640
    uint8_t ch, ch1;
299
640
    uint16_t i;
300
301
640
    switch (CONV(channels,hDecoder->downMatrix))
302
640
    {
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
174
    case CONV(2,0):
312
174
        if (hDecoder->upMatrix)
313
13
        {
314
13
            ch = hDecoder->internal_channel[0];
315
22.1k
            for(i = 0; i < frame_len; i++)
316
22.1k
            {
317
22.1k
                real_t inp0 = input[ch][i];
318
22.1k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
22.1k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
22.1k
            }
321
161
        } else {
322
161
            ch  = hDecoder->internal_channel[0];
323
161
            ch1 = hDecoder->internal_channel[1];
324
257k
            for(i = 0; i < frame_len; i++)
325
257k
            {
326
257k
                real_t inp0 = input[ch ][i];
327
257k
                real_t inp1 = input[ch1][i];
328
257k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
257k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
257k
            }
331
161
        }
332
174
        break;
333
466
    default:
334
4.61k
        for (ch = 0; ch < channels; ch++)
335
4.14k
        {
336
6.10M
            for(i = 0; i < frame_len; i++)
337
6.10M
            {
338
6.10M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
6.10M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
6.10M
            }
341
4.14k
        }
342
466
        break;
343
640
    }
344
640
}
345
346
static void to_PCM_double(NeAACDecStruct *hDecoder, real_t **input,
347
                          uint8_t channels, uint16_t frame_len,
348
                          double **sample_buffer)
349
550
{
350
550
    uint8_t ch, ch1;
351
550
    uint16_t i;
352
353
550
    switch (CONV(channels,hDecoder->downMatrix))
354
550
    {
355
0
    case CONV(1,0):
356
0
    case CONV(1,1):
357
0
        for(i = 0; i < frame_len; i++)
358
0
        {
359
0
            real_t inp = input[hDecoder->internal_channel[0]][i];
360
0
            (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
361
0
        }
362
0
        break;
363
141
    case CONV(2,0):
364
141
        if (hDecoder->upMatrix)
365
13
        {
366
13
            ch = hDecoder->internal_channel[0];
367
25.4k
            for(i = 0; i < frame_len; i++)
368
25.4k
            {
369
25.4k
                real_t inp0 = input[ch][i];
370
25.4k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
25.4k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
25.4k
            }
373
128
        } else {
374
128
            ch  = hDecoder->internal_channel[0];
375
128
            ch1 = hDecoder->internal_channel[1];
376
161k
            for(i = 0; i < frame_len; i++)
377
161k
            {
378
161k
                real_t inp0 = input[ch ][i];
379
161k
                real_t inp1 = input[ch1][i];
380
161k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
161k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
161k
            }
383
128
        }
384
141
        break;
385
409
    default:
386
4.69k
        for (ch = 0; ch < channels; ch++)
387
4.28k
        {
388
6.78M
            for(i = 0; i < frame_len; i++)
389
6.77M
            {
390
6.77M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
6.77M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
6.77M
            }
393
4.28k
        }
394
409
        break;
395
550
    }
396
550
}
397
398
void *output_to_PCM(NeAACDecStruct *hDecoder,
399
                    real_t **input, void *sample_buffer, uint8_t channels,
400
                    uint16_t frame_len, uint8_t format)
401
11.6k
{
402
11.6k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
11.6k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
11.6k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
11.6k
    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.6k
    switch (format)
413
11.6k
    {
414
3.78k
    case FAAD_FMT_16BIT:
415
3.78k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.78k
        break;
417
2.77k
    case FAAD_FMT_24BIT:
418
2.77k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.77k
        break;
420
2.75k
    case FAAD_FMT_32BIT:
421
2.75k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.75k
        break;
423
1.28k
    case FAAD_FMT_FLOAT:
424
1.28k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.28k
        break;
426
1.10k
    case FAAD_FMT_DOUBLE:
427
1.10k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.10k
        break;
429
11.6k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
11.6k
    return sample_buffer;
437
11.6k
}
output_to_PCM
Line
Count
Source
401
5.84k
{
402
5.84k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.84k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.84k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.84k
    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.84k
    switch (format)
413
5.84k
    {
414
1.89k
    case FAAD_FMT_16BIT:
415
1.89k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.89k
        break;
417
1.38k
    case FAAD_FMT_24BIT:
418
1.38k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.38k
        break;
420
1.37k
    case FAAD_FMT_32BIT:
421
1.37k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.37k
        break;
423
640
    case FAAD_FMT_FLOAT:
424
640
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
640
        break;
426
550
    case FAAD_FMT_DOUBLE:
427
550
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
550
        break;
429
5.84k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.84k
    return sample_buffer;
437
5.84k
}
output_to_PCM
Line
Count
Source
401
5.84k
{
402
5.84k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.84k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.84k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.84k
    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.84k
    switch (format)
413
5.84k
    {
414
1.89k
    case FAAD_FMT_16BIT:
415
1.89k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.89k
        break;
417
1.38k
    case FAAD_FMT_24BIT:
418
1.38k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.38k
        break;
420
1.37k
    case FAAD_FMT_32BIT:
421
1.37k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.37k
        break;
423
640
    case FAAD_FMT_FLOAT:
424
640
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
640
        break;
426
550
    case FAAD_FMT_DOUBLE:
427
550
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
550
        break;
429
5.84k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.84k
    return sample_buffer;
437
5.84k
}
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
37.3M
{
449
37.3M
    real_t C;
450
37.3M
    if (up_matrix == 1)
451
32.6k
        return input[internal_channel[0]][sample];
452
453
37.3M
    if (!down_matrix)
454
36.7M
        return input[internal_channel[channel]][sample];
455
456
555k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
555k
    if (channel == 0)
458
277k
    {
459
277k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
277k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
277k
        return core + C + L_S;
462
277k
    } else {
463
277k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
277k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
277k
        return core + C + R_S;
466
277k
    }
467
555k
}
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
8.08k
{
473
8.08k
    uint8_t ch;
474
8.08k
    uint16_t i;
475
8.08k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
8.08k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
8.08k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
59.1k
    for (ch = 0; ch < channels; ch++)
481
51.0k
    {
482
51.0k
        switch (format)
483
51.0k
        {
484
15.8k
        case FAAD_FMT_16BIT:
485
24.2M
            for(i = 0; i < frame_len; i++)
486
24.2M
            {
487
24.2M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
24.2M
                    hDecoder->internal_channel);
489
24.2M
                if (tmp >= 0)
490
21.7M
                {
491
21.7M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
21.7M
                        tmp += (1 << (REAL_BITS-1));
493
21.7M
                    if (tmp >= REAL_CONST(32767))
494
146k
                    {
495
146k
                        tmp = REAL_CONST(32767);
496
146k
                    }
497
21.7M
                } else {
498
2.46M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
2.46M
                        tmp += -(1 << (REAL_BITS-1));
500
2.46M
                    if (tmp <= REAL_CONST(-32768))
501
134k
                    {
502
134k
                        tmp = REAL_CONST(-32768);
503
134k
                    }
504
2.46M
                }
505
24.2M
                tmp >>= REAL_BITS;
506
24.2M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
24.2M
            }
508
15.8k
            break;
509
16.1k
        case FAAD_FMT_24BIT:
510
23.5M
            for(i = 0; i < frame_len; i++)
511
23.5M
            {
512
23.5M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
23.5M
                    hDecoder->internal_channel);
514
23.5M
                if (tmp >= 0)
515
21.0M
                {
516
21.0M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
21.0M
                        tmp += (1 << (REAL_BITS-9));
518
21.0M
                    tmp >>= (REAL_BITS-8);
519
21.0M
                    if (tmp >= 8388607)
520
107k
                    {
521
107k
                        tmp = 8388607;
522
107k
                    }
523
21.0M
                } else {
524
2.47M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
2.47M
                        tmp += -(1 << (REAL_BITS-9));
526
2.47M
                    tmp >>= (REAL_BITS-8);
527
2.47M
                    if (tmp <= -8388608)
528
101k
                    {
529
101k
                        tmp = -8388608;
530
101k
                    }
531
2.47M
                }
532
23.5M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
23.5M
            }
534
16.1k
            break;
535
9.51k
        case FAAD_FMT_32BIT:
536
9.51k
            exp = 16 - REAL_BITS;
537
9.51k
            half = 1 << (exp - 1);
538
9.51k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
13.3M
            for(i = 0; i < frame_len; i++)
540
13.3M
            {
541
13.3M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
13.3M
                    hDecoder->internal_channel);
543
13.3M
                if (tmp >= 0)
544
12.5M
                {
545
12.5M
                    if (tmp <= 0x7FFFFFFF - half)
546
12.5M
                        tmp += half;
547
12.5M
                } else {
548
825k
                    if (tmp >= (int32_t)0x80000000 + half)
549
825k
                        tmp += -half;
550
825k
                }
551
13.3M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
13.3M
                int_sample_buffer[(i*channels)+ch] = tmp;
553
13.3M
            }
554
9.51k
            break;
555
9.46k
        case FAAD_FMT_FIXED:
556
13.6M
            for(i = 0; i < frame_len; i++)
557
13.6M
            {
558
13.6M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
13.6M
                    hDecoder->internal_channel);
560
13.6M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
13.6M
            }
562
9.46k
            break;
563
51.0k
        }
564
51.0k
    }
565
566
8.08k
    return sample_buffer;
567
8.08k
}
output_to_PCM
Line
Count
Source
472
4.04k
{
473
4.04k
    uint8_t ch;
474
4.04k
    uint16_t i;
475
4.04k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
4.04k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
4.04k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
29.5k
    for (ch = 0; ch < channels; ch++)
481
25.5k
    {
482
25.5k
        switch (format)
483
25.5k
        {
484
7.94k
        case FAAD_FMT_16BIT:
485
12.1M
            for(i = 0; i < frame_len; i++)
486
12.1M
            {
487
12.1M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
12.1M
                    hDecoder->internal_channel);
489
12.1M
                if (tmp >= 0)
490
10.8M
                {
491
10.8M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
10.8M
                        tmp += (1 << (REAL_BITS-1));
493
10.8M
                    if (tmp >= REAL_CONST(32767))
494
73.2k
                    {
495
73.2k
                        tmp = REAL_CONST(32767);
496
73.2k
                    }
497
10.8M
                } else {
498
1.23M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
1.23M
                        tmp += -(1 << (REAL_BITS-1));
500
1.23M
                    if (tmp <= REAL_CONST(-32768))
501
67.1k
                    {
502
67.1k
                        tmp = REAL_CONST(-32768);
503
67.1k
                    }
504
1.23M
                }
505
12.1M
                tmp >>= REAL_BITS;
506
12.1M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
12.1M
            }
508
7.94k
            break;
509
8.09k
        case FAAD_FMT_24BIT:
510
11.7M
            for(i = 0; i < frame_len; i++)
511
11.7M
            {
512
11.7M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
11.7M
                    hDecoder->internal_channel);
514
11.7M
                if (tmp >= 0)
515
10.5M
                {
516
10.5M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
10.5M
                        tmp += (1 << (REAL_BITS-9));
518
10.5M
                    tmp >>= (REAL_BITS-8);
519
10.5M
                    if (tmp >= 8388607)
520
53.8k
                    {
521
53.8k
                        tmp = 8388607;
522
53.8k
                    }
523
10.5M
                } else {
524
1.23M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
1.23M
                        tmp += -(1 << (REAL_BITS-9));
526
1.23M
                    tmp >>= (REAL_BITS-8);
527
1.23M
                    if (tmp <= -8388608)
528
50.7k
                    {
529
50.7k
                        tmp = -8388608;
530
50.7k
                    }
531
1.23M
                }
532
11.7M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
11.7M
            }
534
8.09k
            break;
535
4.75k
        case FAAD_FMT_32BIT:
536
4.75k
            exp = 16 - REAL_BITS;
537
4.75k
            half = 1 << (exp - 1);
538
4.75k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
6.67M
            for(i = 0; i < frame_len; i++)
540
6.66M
            {
541
6.66M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
6.66M
                    hDecoder->internal_channel);
543
6.66M
                if (tmp >= 0)
544
6.25M
                {
545
6.25M
                    if (tmp <= 0x7FFFFFFF - half)
546
6.25M
                        tmp += half;
547
6.25M
                } else {
548
412k
                    if (tmp >= (int32_t)0x80000000 + half)
549
412k
                        tmp += -half;
550
412k
                }
551
6.66M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
6.66M
                int_sample_buffer[(i*channels)+ch] = tmp;
553
6.66M
            }
554
4.75k
            break;
555
4.73k
        case FAAD_FMT_FIXED:
556
6.81M
            for(i = 0; i < frame_len; i++)
557
6.81M
            {
558
6.81M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
6.81M
                    hDecoder->internal_channel);
560
6.81M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
6.81M
            }
562
4.73k
            break;
563
25.5k
        }
564
25.5k
    }
565
566
4.04k
    return sample_buffer;
567
4.04k
}
output_to_PCM
Line
Count
Source
472
4.04k
{
473
4.04k
    uint8_t ch;
474
4.04k
    uint16_t i;
475
4.04k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
4.04k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
4.04k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
29.5k
    for (ch = 0; ch < channels; ch++)
481
25.5k
    {
482
25.5k
        switch (format)
483
25.5k
        {
484
7.94k
        case FAAD_FMT_16BIT:
485
12.1M
            for(i = 0; i < frame_len; i++)
486
12.1M
            {
487
12.1M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
12.1M
                    hDecoder->internal_channel);
489
12.1M
                if (tmp >= 0)
490
10.8M
                {
491
10.8M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
10.8M
                        tmp += (1 << (REAL_BITS-1));
493
10.8M
                    if (tmp >= REAL_CONST(32767))
494
73.2k
                    {
495
73.2k
                        tmp = REAL_CONST(32767);
496
73.2k
                    }
497
10.8M
                } else {
498
1.23M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
1.23M
                        tmp += -(1 << (REAL_BITS-1));
500
1.23M
                    if (tmp <= REAL_CONST(-32768))
501
67.1k
                    {
502
67.1k
                        tmp = REAL_CONST(-32768);
503
67.1k
                    }
504
1.23M
                }
505
12.1M
                tmp >>= REAL_BITS;
506
12.1M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
12.1M
            }
508
7.94k
            break;
509
8.09k
        case FAAD_FMT_24BIT:
510
11.7M
            for(i = 0; i < frame_len; i++)
511
11.7M
            {
512
11.7M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
11.7M
                    hDecoder->internal_channel);
514
11.7M
                if (tmp >= 0)
515
10.5M
                {
516
10.5M
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
10.5M
                        tmp += (1 << (REAL_BITS-9));
518
10.5M
                    tmp >>= (REAL_BITS-8);
519
10.5M
                    if (tmp >= 8388607)
520
53.8k
                    {
521
53.8k
                        tmp = 8388607;
522
53.8k
                    }
523
10.5M
                } else {
524
1.23M
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
1.23M
                        tmp += -(1 << (REAL_BITS-9));
526
1.23M
                    tmp >>= (REAL_BITS-8);
527
1.23M
                    if (tmp <= -8388608)
528
50.7k
                    {
529
50.7k
                        tmp = -8388608;
530
50.7k
                    }
531
1.23M
                }
532
11.7M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
11.7M
            }
534
8.09k
            break;
535
4.75k
        case FAAD_FMT_32BIT:
536
4.75k
            exp = 16 - REAL_BITS;
537
4.75k
            half = 1 << (exp - 1);
538
4.75k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
6.67M
            for(i = 0; i < frame_len; i++)
540
6.66M
            {
541
6.66M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
6.66M
                    hDecoder->internal_channel);
543
6.66M
                if (tmp >= 0)
544
6.25M
                {
545
6.25M
                    if (tmp <= 0x7FFFFFFF - half)
546
6.25M
                        tmp += half;
547
6.25M
                } else {
548
412k
                    if (tmp >= (int32_t)0x80000000 + half)
549
412k
                        tmp += -half;
550
412k
                }
551
6.66M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
6.66M
                int_sample_buffer[(i*channels)+ch] = tmp;
553
6.66M
            }
554
4.75k
            break;
555
4.73k
        case FAAD_FMT_FIXED:
556
6.81M
            for(i = 0; i < frame_len; i++)
557
6.81M
            {
558
6.81M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
6.81M
                    hDecoder->internal_channel);
560
6.81M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
6.81M
            }
562
4.73k
            break;
563
25.5k
        }
564
25.5k
    }
565
566
4.04k
    return sample_buffer;
567
4.04k
}
568
569
#endif