Coverage Report

Created: 2026-07-10 07:01

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
14.2M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.58M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
5.17M
#define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
43
44
45
static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
46
                                uint8_t down_matrix, uint8_t *internal_channel)
47
40.2M
{
48
40.2M
    if (!down_matrix)
49
37.6M
        return input[internal_channel[channel]][sample];
50
51
2.58M
    if (channel == 0)
52
1.28M
    {
53
1.28M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.28M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.28M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.30M
    } else {
57
1.30M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.30M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.30M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.30M
    }
61
2.58M
}
62
63
#ifndef HAS_LRINTF
64
33.9M
#define CLIP(sample, max, min) \
65
33.9M
if (sample >= 0.0f)            \
66
29.3M
{                              \
67
29.3M
    sample += 0.5f;            \
68
29.3M
    if (sample >= max)         \
69
29.3M
        sample = max;          \
70
29.3M
} else {                       \
71
4.59M
    sample += -0.5f;           \
72
4.59M
    if (sample <= min)         \
73
4.59M
        sample = min;          \
74
4.59M
}
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.58k
#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.92k
{
93
1.92k
    uint8_t ch, ch1;
94
1.92k
    uint16_t i;
95
96
1.92k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.92k
    {
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.03k
    case CONV(2,0):
110
1.03k
        if (hDecoder->upMatrix)
111
54
        {
112
54
            ch  = hDecoder->internal_channel[0];
113
83.7k
            for(i = 0; i < frame_len; i++)
114
83.6k
            {
115
83.6k
                real_t inp0 = input[ch][i];
116
117
83.6k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
83.6k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
83.6k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
83.6k
            }
122
980
        } else {
123
980
            ch  = hDecoder->internal_channel[0];
124
980
            ch1 = hDecoder->internal_channel[1];
125
1.49M
            for(i = 0; i < frame_len; i++)
126
1.49M
            {
127
1.49M
                real_t inp0 = input[ch ][i];
128
1.49M
                real_t inp1 = input[ch1][i];
129
130
1.49M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.49M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.49M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.49M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.49M
            }
136
980
        }
137
1.03k
        break;
138
888
    default:
139
8.77k
        for (ch = 0; ch < channels; ch++)
140
7.88k
        {
141
11.2M
            for(i = 0; i < frame_len; i++)
142
11.2M
            {
143
11.2M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
11.2M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
11.2M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
11.2M
            }
149
7.88k
        }
150
888
        break;
151
1.92k
    }
152
1.92k
}
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
693
    case CONV(2,0):
176
693
        if (hDecoder->upMatrix)
177
40
        {
178
40
            ch = hDecoder->internal_channel[0];
179
60.2k
            for(i = 0; i < frame_len; i++)
180
60.1k
            {
181
60.1k
                real_t inp0 = input[ch][i];
182
183
60.1k
                inp0 *= 256.0f;
184
60.1k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
60.1k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
60.1k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
60.1k
            }
189
653
        } else {
190
653
            ch  = hDecoder->internal_channel[0];
191
653
            ch1 = hDecoder->internal_channel[1];
192
945k
            for(i = 0; i < frame_len; i++)
193
944k
            {
194
944k
                real_t inp0 = input[ch ][i];
195
944k
                real_t inp1 = input[ch1][i];
196
197
944k
                inp0 *= 256.0f;
198
944k
                inp1 *= 256.0f;
199
944k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
944k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
944k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
944k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
944k
            }
205
653
        }
206
693
        break;
207
691
    default:
208
6.84k
        for (ch = 0; ch < channels; ch++)
209
6.15k
        {
210
8.88M
            for(i = 0; i < frame_len; i++)
211
8.87M
            {
212
8.87M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
8.87M
                inp *= 256.0f;
215
8.87M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
8.87M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
8.87M
            }
219
6.15k
        }
220
691
        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.26k
{
228
1.26k
    uint8_t ch, ch1;
229
1.26k
    uint16_t i;
230
231
1.26k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.26k
    {
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
674
    case CONV(2,0):
246
674
        if (hDecoder->upMatrix)
247
41
        {
248
41
            ch = hDecoder->internal_channel[0];
249
65.3k
            for(i = 0; i < frame_len; i++)
250
65.2k
            {
251
65.2k
                real_t inp0 = input[ch][i];
252
253
65.2k
                inp0 *= 65536.0f;
254
65.2k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
65.2k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
65.2k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
65.2k
            }
259
633
        } else {
260
633
            ch  = hDecoder->internal_channel[0];
261
633
            ch1 = hDecoder->internal_channel[1];
262
967k
            for(i = 0; i < frame_len; i++)
263
966k
            {
264
966k
                real_t inp0 = input[ch ][i];
265
966k
                real_t inp1 = input[ch1][i];
266
267
966k
                inp0 *= 65536.0f;
268
966k
                inp1 *= 65536.0f;
269
966k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
966k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
966k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
966k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
966k
            }
275
633
        }
276
674
        break;
277
586
    default:
278
5.64k
        for (ch = 0; ch < channels; ch++)
279
5.06k
        {
280
6.86M
            for(i = 0; i < frame_len; i++)
281
6.86M
            {
282
6.86M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
6.86M
                inp *= 65536.0f;
285
6.86M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
6.86M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
6.86M
            }
289
5.06k
        }
290
586
        break;
291
1.26k
    }
292
1.26k
}
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
669
{
298
669
    uint8_t ch, ch1;
299
669
    uint16_t i;
300
301
669
    switch (CONV(channels,hDecoder->downMatrix))
302
669
    {
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
201
    case CONV(2,0):
312
201
        if (hDecoder->upMatrix)
313
20
        {
314
20
            ch = hDecoder->internal_channel[0];
315
27.5k
            for(i = 0; i < frame_len; i++)
316
27.5k
            {
317
27.5k
                real_t inp0 = input[ch][i];
318
27.5k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
27.5k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
27.5k
            }
321
181
        } else {
322
181
            ch  = hDecoder->internal_channel[0];
323
181
            ch1 = hDecoder->internal_channel[1];
324
269k
            for(i = 0; i < frame_len; i++)
325
268k
            {
326
268k
                real_t inp0 = input[ch ][i];
327
268k
                real_t inp1 = input[ch1][i];
328
268k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
268k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
268k
            }
331
181
        }
332
201
        break;
333
468
    default:
334
4.62k
        for (ch = 0; ch < channels; ch++)
335
4.15k
        {
336
5.98M
            for(i = 0; i < frame_len; i++)
337
5.98M
            {
338
5.98M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
5.98M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
5.98M
            }
341
4.15k
        }
342
468
        break;
343
669
    }
344
669
}
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
606
{
350
606
    uint8_t ch, ch1;
351
606
    uint16_t i;
352
353
606
    switch (CONV(channels,hDecoder->downMatrix))
354
606
    {
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
142
    case CONV(2,0):
364
142
        if (hDecoder->upMatrix)
365
23
        {
366
23
            ch = hDecoder->internal_channel[0];
367
42.5k
            for(i = 0; i < frame_len; i++)
368
42.4k
            {
369
42.4k
                real_t inp0 = input[ch][i];
370
42.4k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
42.4k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
42.4k
            }
373
119
        } else {
374
119
            ch  = hDecoder->internal_channel[0];
375
119
            ch1 = hDecoder->internal_channel[1];
376
157k
            for(i = 0; i < frame_len; i++)
377
157k
            {
378
157k
                real_t inp0 = input[ch ][i];
379
157k
                real_t inp1 = input[ch1][i];
380
157k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
157k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
157k
            }
383
119
        }
384
142
        break;
385
464
    default:
386
5.15k
        for (ch = 0; ch < channels; ch++)
387
4.68k
        {
388
7.31M
            for(i = 0; i < frame_len; i++)
389
7.31M
            {
390
7.31M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
7.31M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
7.31M
            }
393
4.68k
        }
394
464
        break;
395
606
    }
396
606
}
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.84k
    case FAAD_FMT_16BIT:
415
3.84k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.84k
        break;
417
2.76k
    case FAAD_FMT_24BIT:
418
2.76k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.76k
        break;
420
2.52k
    case FAAD_FMT_32BIT:
421
2.52k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.52k
        break;
423
1.33k
    case FAAD_FMT_FLOAT:
424
1.33k
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
1.33k
        break;
426
1.21k
    case FAAD_FMT_DOUBLE:
427
1.21k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.21k
        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.92k
    case FAAD_FMT_16BIT:
415
1.92k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.92k
        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.26k
    case FAAD_FMT_32BIT:
421
1.26k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.26k
        break;
423
669
    case FAAD_FMT_FLOAT:
424
669
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
669
        break;
426
606
    case FAAD_FMT_DOUBLE:
427
606
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
606
        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.92k
    case FAAD_FMT_16BIT:
415
1.92k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.92k
        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.26k
    case FAAD_FMT_32BIT:
421
1.26k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.26k
        break;
423
669
    case FAAD_FMT_FLOAT:
424
669
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
669
        break;
426
606
    case FAAD_FMT_DOUBLE:
427
606
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
606
        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
24.2M
{
449
24.2M
    real_t C;
450
24.2M
    if (up_matrix == 1)
451
244k
        return input[internal_channel[0]][sample];
452
453
24.0M
    if (!down_matrix)
454
23.4M
        return input[internal_channel[channel]][sample];
455
456
526k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
526k
    if (channel == 0)
458
258k
    {
459
258k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
258k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
258k
        return core + C + L_S;
462
268k
    } else {
463
268k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
268k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
268k
        return core + C + R_S;
466
268k
    }
467
526k
}
468
469
void* output_to_PCM(NeAACDecStruct *hDecoder,
470
                    real_t **input, void *sample_buffer, uint8_t channels,
471
                    uint16_t frame_len, uint8_t format)
472
5.06k
{
473
5.06k
    uint8_t ch;
474
5.06k
    uint16_t i;
475
5.06k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
5.06k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
5.06k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
39.8k
    for (ch = 0; ch < channels; ch++)
481
34.7k
    {
482
34.7k
        switch (format)
483
34.7k
        {
484
10.5k
        case FAAD_FMT_16BIT:
485
15.5M
            for(i = 0; i < frame_len; i++)
486
15.5M
            {
487
15.5M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
15.5M
                    hDecoder->internal_channel);
489
15.5M
                if (tmp >= 0)
490
14.2M
                {
491
14.2M
                    tmp += (1 << (REAL_BITS-1));
492
14.2M
                    if (tmp >= REAL_CONST(32767))
493
30.1k
                    {
494
30.1k
                        tmp = REAL_CONST(32767);
495
30.1k
                    }
496
14.2M
                } else {
497
1.30M
                    tmp += -(1 << (REAL_BITS-1));
498
1.30M
                    if (tmp <= REAL_CONST(-32768))
499
30.7k
                    {
500
30.7k
                        tmp = REAL_CONST(-32768);
501
30.7k
                    }
502
1.30M
                }
503
15.5M
                tmp >>= REAL_BITS;
504
15.5M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
15.5M
            }
506
10.5k
            break;
507
10.3k
        case FAAD_FMT_24BIT:
508
14.3M
            for(i = 0; i < frame_len; i++)
509
14.3M
            {
510
14.3M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
14.3M
                    hDecoder->internal_channel);
512
14.3M
                if (tmp >= 0)
513
13.3M
                {
514
13.3M
                    tmp += (1 << (REAL_BITS-9));
515
13.3M
                    tmp >>= (REAL_BITS-8);
516
13.3M
                    if (tmp >= 8388607)
517
20.8k
                    {
518
20.8k
                        tmp = 8388607;
519
20.8k
                    }
520
13.3M
                } else {
521
1.03M
                    tmp += -(1 << (REAL_BITS-9));
522
1.03M
                    tmp >>= (REAL_BITS-8);
523
1.03M
                    if (tmp <= -8388608)
524
19.8k
                    {
525
19.8k
                        tmp = -8388608;
526
19.8k
                    }
527
1.03M
                }
528
14.3M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
14.3M
            }
530
10.3k
            break;
531
7.25k
        case FAAD_FMT_32BIT:
532
7.25k
            exp = 16 - REAL_BITS;
533
7.25k
            half = 1 << (exp - 1);
534
7.25k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
10.2M
            for(i = 0; i < frame_len; i++)
536
10.2M
            {
537
10.2M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
10.2M
                    hDecoder->internal_channel);
539
10.2M
                if (tmp >= 0)
540
9.44M
                {
541
9.44M
                    tmp += half;
542
9.44M
                } else {
543
825k
                    tmp += -half;
544
825k
                }
545
10.2M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
10.2M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
10.2M
            }
548
7.25k
            break;
549
6.54k
        case FAAD_FMT_FIXED:
550
8.32M
            for(i = 0; i < frame_len; i++)
551
8.32M
            {
552
8.32M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
8.32M
                    hDecoder->internal_channel);
554
8.32M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
8.32M
            }
556
6.54k
            break;
557
34.7k
        }
558
34.7k
    }
559
560
5.06k
    return sample_buffer;
561
5.06k
}
output_to_PCM
Line
Count
Source
472
2.53k
{
473
2.53k
    uint8_t ch;
474
2.53k
    uint16_t i;
475
2.53k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.53k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.53k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
19.9k
    for (ch = 0; ch < channels; ch++)
481
17.3k
    {
482
17.3k
        switch (format)
483
17.3k
        {
484
5.29k
        case FAAD_FMT_16BIT:
485
7.76M
            for(i = 0; i < frame_len; i++)
486
7.76M
            {
487
7.76M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
7.76M
                    hDecoder->internal_channel);
489
7.76M
                if (tmp >= 0)
490
7.10M
                {
491
7.10M
                    tmp += (1 << (REAL_BITS-1));
492
7.10M
                    if (tmp >= REAL_CONST(32767))
493
15.0k
                    {
494
15.0k
                        tmp = REAL_CONST(32767);
495
15.0k
                    }
496
7.10M
                } else {
497
652k
                    tmp += -(1 << (REAL_BITS-1));
498
652k
                    if (tmp <= REAL_CONST(-32768))
499
15.3k
                    {
500
15.3k
                        tmp = REAL_CONST(-32768);
501
15.3k
                    }
502
652k
                }
503
7.76M
                tmp >>= REAL_BITS;
504
7.76M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
7.76M
            }
506
5.29k
            break;
507
5.17k
        case FAAD_FMT_24BIT:
508
7.19M
            for(i = 0; i < frame_len; i++)
509
7.19M
            {
510
7.19M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
7.19M
                    hDecoder->internal_channel);
512
7.19M
                if (tmp >= 0)
513
6.67M
                {
514
6.67M
                    tmp += (1 << (REAL_BITS-9));
515
6.67M
                    tmp >>= (REAL_BITS-8);
516
6.67M
                    if (tmp >= 8388607)
517
10.4k
                    {
518
10.4k
                        tmp = 8388607;
519
10.4k
                    }
520
6.67M
                } else {
521
517k
                    tmp += -(1 << (REAL_BITS-9));
522
517k
                    tmp >>= (REAL_BITS-8);
523
517k
                    if (tmp <= -8388608)
524
9.90k
                    {
525
9.90k
                        tmp = -8388608;
526
9.90k
                    }
527
517k
                }
528
7.19M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
7.19M
            }
530
5.17k
            break;
531
3.62k
        case FAAD_FMT_32BIT:
532
3.62k
            exp = 16 - REAL_BITS;
533
3.62k
            half = 1 << (exp - 1);
534
3.62k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
5.13M
            for(i = 0; i < frame_len; i++)
536
5.13M
            {
537
5.13M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
5.13M
                    hDecoder->internal_channel);
539
5.13M
                if (tmp >= 0)
540
4.72M
                {
541
4.72M
                    tmp += half;
542
4.72M
                } else {
543
412k
                    tmp += -half;
544
412k
                }
545
5.13M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
5.13M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
5.13M
            }
548
3.62k
            break;
549
3.27k
        case FAAD_FMT_FIXED:
550
4.16M
            for(i = 0; i < frame_len; i++)
551
4.16M
            {
552
4.16M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.16M
                    hDecoder->internal_channel);
554
4.16M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.16M
            }
556
3.27k
            break;
557
17.3k
        }
558
17.3k
    }
559
560
2.53k
    return sample_buffer;
561
2.53k
}
output_to_PCM
Line
Count
Source
472
2.53k
{
473
2.53k
    uint8_t ch;
474
2.53k
    uint16_t i;
475
2.53k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
2.53k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
2.53k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
19.9k
    for (ch = 0; ch < channels; ch++)
481
17.3k
    {
482
17.3k
        switch (format)
483
17.3k
        {
484
5.29k
        case FAAD_FMT_16BIT:
485
7.76M
            for(i = 0; i < frame_len; i++)
486
7.76M
            {
487
7.76M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
7.76M
                    hDecoder->internal_channel);
489
7.76M
                if (tmp >= 0)
490
7.10M
                {
491
7.10M
                    tmp += (1 << (REAL_BITS-1));
492
7.10M
                    if (tmp >= REAL_CONST(32767))
493
15.0k
                    {
494
15.0k
                        tmp = REAL_CONST(32767);
495
15.0k
                    }
496
7.10M
                } else {
497
652k
                    tmp += -(1 << (REAL_BITS-1));
498
652k
                    if (tmp <= REAL_CONST(-32768))
499
15.3k
                    {
500
15.3k
                        tmp = REAL_CONST(-32768);
501
15.3k
                    }
502
652k
                }
503
7.76M
                tmp >>= REAL_BITS;
504
7.76M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
7.76M
            }
506
5.29k
            break;
507
5.17k
        case FAAD_FMT_24BIT:
508
7.19M
            for(i = 0; i < frame_len; i++)
509
7.19M
            {
510
7.19M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
7.19M
                    hDecoder->internal_channel);
512
7.19M
                if (tmp >= 0)
513
6.67M
                {
514
6.67M
                    tmp += (1 << (REAL_BITS-9));
515
6.67M
                    tmp >>= (REAL_BITS-8);
516
6.67M
                    if (tmp >= 8388607)
517
10.4k
                    {
518
10.4k
                        tmp = 8388607;
519
10.4k
                    }
520
6.67M
                } else {
521
517k
                    tmp += -(1 << (REAL_BITS-9));
522
517k
                    tmp >>= (REAL_BITS-8);
523
517k
                    if (tmp <= -8388608)
524
9.90k
                    {
525
9.90k
                        tmp = -8388608;
526
9.90k
                    }
527
517k
                }
528
7.19M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
7.19M
            }
530
5.17k
            break;
531
3.62k
        case FAAD_FMT_32BIT:
532
3.62k
            exp = 16 - REAL_BITS;
533
3.62k
            half = 1 << (exp - 1);
534
3.62k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
5.13M
            for(i = 0; i < frame_len; i++)
536
5.13M
            {
537
5.13M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
5.13M
                    hDecoder->internal_channel);
539
5.13M
                if (tmp >= 0)
540
4.72M
                {
541
4.72M
                    tmp += half;
542
4.72M
                } else {
543
412k
                    tmp += -half;
544
412k
                }
545
5.13M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
5.13M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
5.13M
            }
548
3.62k
            break;
549
3.27k
        case FAAD_FMT_FIXED:
550
4.16M
            for(i = 0; i < frame_len; i++)
551
4.16M
            {
552
4.16M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.16M
                    hDecoder->internal_channel);
554
4.16M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.16M
            }
556
3.27k
            break;
557
17.3k
        }
558
17.3k
    }
559
560
2.53k
    return sample_buffer;
561
2.53k
}
562
563
#endif