Coverage Report

Created: 2026-05-30 06:09

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.0M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
2.39M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
4.79M
#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.9M
{
48
39.9M
    if (!down_matrix)
49
37.5M
        return input[internal_channel[channel]][sample];
50
51
2.39M
    if (channel == 0)
52
1.18M
    {
53
1.18M
        return DM_MUL * (input[internal_channel[1]][sample] +
54
1.18M
            input[internal_channel[0]][sample] * RSQRT2 +
55
1.18M
            input[internal_channel[3]][sample] * RSQRT2);
56
1.21M
    } else {
57
1.21M
        return DM_MUL * (input[internal_channel[2]][sample] +
58
1.21M
            input[internal_channel[0]][sample] * RSQRT2 +
59
1.21M
            input[internal_channel[4]][sample] * RSQRT2);
60
1.21M
    }
61
2.39M
}
62
63
#ifndef HAS_LRINTF
64
33.7M
#define CLIP(sample, max, min) \
65
33.7M
if (sample >= 0.0f)            \
66
28.8M
{                              \
67
28.8M
    sample += 0.5f;            \
68
28.8M
    if (sample >= max)         \
69
28.8M
        sample = max;          \
70
28.8M
} else {                       \
71
4.81M
    sample += -0.5f;           \
72
4.81M
    if (sample <= min)         \
73
4.81M
        sample = min;          \
74
4.81M
}
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.33k
#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.82k
{
93
1.82k
    uint8_t ch, ch1;
94
1.82k
    uint16_t i;
95
96
1.82k
    switch (CONV(channels,hDecoder->downMatrix))
97
1.82k
    {
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
935
    case CONV(2,0):
110
935
        if (hDecoder->upMatrix)
111
55
        {
112
55
            ch  = hDecoder->internal_channel[0];
113
88.5k
            for(i = 0; i < frame_len; i++)
114
88.5k
            {
115
88.5k
                real_t inp0 = input[ch][i];
116
117
88.5k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
88.5k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
88.5k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
88.5k
            }
122
880
        } else {
123
880
            ch  = hDecoder->internal_channel[0];
124
880
            ch1 = hDecoder->internal_channel[1];
125
1.34M
            for(i = 0; i < frame_len; i++)
126
1.34M
            {
127
1.34M
                real_t inp0 = input[ch ][i];
128
1.34M
                real_t inp1 = input[ch1][i];
129
130
1.34M
                CLIP(inp0, 32767.0f, -32768.0f);
131
1.34M
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
1.34M
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
1.34M
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
1.34M
            }
136
880
        }
137
935
        break;
138
889
    default:
139
9.49k
        for (ch = 0; ch < channels; ch++)
140
8.60k
        {
141
12.5M
            for(i = 0; i < frame_len; i++)
142
12.5M
            {
143
12.5M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
12.5M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
12.5M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
12.5M
            }
149
8.60k
        }
150
889
        break;
151
1.82k
    }
152
1.82k
}
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.32k
{
158
1.32k
    uint8_t ch, ch1;
159
1.32k
    uint16_t i;
160
161
1.32k
    switch (CONV(channels,hDecoder->downMatrix))
162
1.32k
    {
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
704
    case CONV(2,0):
176
704
        if (hDecoder->upMatrix)
177
48
        {
178
48
            ch = hDecoder->internal_channel[0];
179
70.5k
            for(i = 0; i < frame_len; i++)
180
70.4k
            {
181
70.4k
                real_t inp0 = input[ch][i];
182
183
70.4k
                inp0 *= 256.0f;
184
70.4k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
70.4k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
70.4k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
70.4k
            }
189
656
        } else {
190
656
            ch  = hDecoder->internal_channel[0];
191
656
            ch1 = hDecoder->internal_channel[1];
192
963k
            for(i = 0; i < frame_len; i++)
193
963k
            {
194
963k
                real_t inp0 = input[ch ][i];
195
963k
                real_t inp1 = input[ch1][i];
196
197
963k
                inp0 *= 256.0f;
198
963k
                inp1 *= 256.0f;
199
963k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
963k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
963k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
963k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
963k
            }
205
656
        }
206
704
        break;
207
625
    default:
208
6.22k
        for (ch = 0; ch < channels; ch++)
209
5.59k
        {
210
7.79M
            for(i = 0; i < frame_len; i++)
211
7.79M
            {
212
7.79M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
7.79M
                inp *= 256.0f;
215
7.79M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
7.79M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
7.79M
            }
219
5.59k
        }
220
625
        break;
221
1.32k
    }
222
1.32k
}
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.27k
{
228
1.27k
    uint8_t ch, ch1;
229
1.27k
    uint16_t i;
230
231
1.27k
    switch (CONV(channels,hDecoder->downMatrix))
232
1.27k
    {
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
688
    case CONV(2,0):
246
688
        if (hDecoder->upMatrix)
247
44
        {
248
44
            ch = hDecoder->internal_channel[0];
249
67.3k
            for(i = 0; i < frame_len; i++)
250
67.3k
            {
251
67.3k
                real_t inp0 = input[ch][i];
252
253
67.3k
                inp0 *= 65536.0f;
254
67.3k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
67.3k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
67.3k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
67.3k
            }
259
644
        } else {
260
644
            ch  = hDecoder->internal_channel[0];
261
644
            ch1 = hDecoder->internal_channel[1];
262
1.00M
            for(i = 0; i < frame_len; i++)
263
1.00M
            {
264
1.00M
                real_t inp0 = input[ch ][i];
265
1.00M
                real_t inp1 = input[ch1][i];
266
267
1.00M
                inp0 *= 65536.0f;
268
1.00M
                inp1 *= 65536.0f;
269
1.00M
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
1.00M
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
1.00M
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
1.00M
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
1.00M
            }
275
644
        }
276
688
        break;
277
587
    default:
278
5.32k
        for (ch = 0; ch < channels; ch++)
279
4.73k
        {
280
6.49M
            for(i = 0; i < frame_len; i++)
281
6.48M
            {
282
6.48M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
6.48M
                inp *= 65536.0f;
285
6.48M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
6.48M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
6.48M
            }
289
4.73k
        }
290
587
        break;
291
1.27k
    }
292
1.27k
}
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
667
{
298
667
    uint8_t ch, ch1;
299
667
    uint16_t i;
300
301
667
    switch (CONV(channels,hDecoder->downMatrix))
302
667
    {
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
188
    case CONV(2,0):
312
188
        if (hDecoder->upMatrix)
313
18
        {
314
18
            ch = hDecoder->internal_channel[0];
315
24.4k
            for(i = 0; i < frame_len; i++)
316
24.4k
            {
317
24.4k
                real_t inp0 = input[ch][i];
318
24.4k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
24.4k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
24.4k
            }
321
170
        } else {
322
170
            ch  = hDecoder->internal_channel[0];
323
170
            ch1 = hDecoder->internal_channel[1];
324
242k
            for(i = 0; i < frame_len; i++)
325
242k
            {
326
242k
                real_t inp0 = input[ch ][i];
327
242k
                real_t inp1 = input[ch1][i];
328
242k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
242k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
242k
            }
331
170
        }
332
188
        break;
333
479
    default:
334
4.74k
        for (ch = 0; ch < channels; ch++)
335
4.26k
        {
336
6.35M
            for(i = 0; i < frame_len; i++)
337
6.35M
            {
338
6.35M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
6.35M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
6.35M
            }
341
4.26k
        }
342
479
        break;
343
667
    }
344
667
}
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
579
{
350
579
    uint8_t ch, ch1;
351
579
    uint16_t i;
352
353
579
    switch (CONV(channels,hDecoder->downMatrix))
354
579
    {
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
148
    case CONV(2,0):
364
148
        if (hDecoder->upMatrix)
365
22
        {
366
22
            ch = hDecoder->internal_channel[0];
367
40.7k
            for(i = 0; i < frame_len; i++)
368
40.7k
            {
369
40.7k
                real_t inp0 = input[ch][i];
370
40.7k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
40.7k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
40.7k
            }
373
126
        } else {
374
126
            ch  = hDecoder->internal_channel[0];
375
126
            ch1 = hDecoder->internal_channel[1];
376
158k
            for(i = 0; i < frame_len; i++)
377
158k
            {
378
158k
                real_t inp0 = input[ch ][i];
379
158k
                real_t inp1 = input[ch1][i];
380
158k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
158k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
158k
            }
383
126
        }
384
148
        break;
385
431
    default:
386
4.88k
        for (ch = 0; ch < channels; ch++)
387
4.45k
        {
388
6.74M
            for(i = 0; i < frame_len; i++)
389
6.73M
            {
390
6.73M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
6.73M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
6.73M
            }
393
4.45k
        }
394
431
        break;
395
579
    }
396
579
}
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.3k
{
402
11.3k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
11.3k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
11.3k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
11.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
11.3k
    switch (format)
413
11.3k
    {
414
3.64k
    case FAAD_FMT_16BIT:
415
3.64k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
3.64k
        break;
417
2.65k
    case FAAD_FMT_24BIT:
418
2.65k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
2.65k
        break;
420
2.55k
    case FAAD_FMT_32BIT:
421
2.55k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
2.55k
        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.15k
    case FAAD_FMT_DOUBLE:
427
1.15k
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
1.15k
        break;
429
11.3k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
11.3k
    return sample_buffer;
437
11.3k
}
output_to_PCM
Line
Count
Source
401
5.67k
{
402
5.67k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.67k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.67k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.67k
    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.67k
    switch (format)
413
5.67k
    {
414
1.82k
    case FAAD_FMT_16BIT:
415
1.82k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.82k
        break;
417
1.32k
    case FAAD_FMT_24BIT:
418
1.32k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.32k
        break;
420
1.27k
    case FAAD_FMT_32BIT:
421
1.27k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.27k
        break;
423
667
    case FAAD_FMT_FLOAT:
424
667
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
667
        break;
426
579
    case FAAD_FMT_DOUBLE:
427
579
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
579
        break;
429
5.67k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.67k
    return sample_buffer;
437
5.67k
}
output_to_PCM
Line
Count
Source
401
5.67k
{
402
5.67k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
5.67k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
5.67k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
5.67k
    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.67k
    switch (format)
413
5.67k
    {
414
1.82k
    case FAAD_FMT_16BIT:
415
1.82k
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
1.82k
        break;
417
1.32k
    case FAAD_FMT_24BIT:
418
1.32k
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
1.32k
        break;
420
1.27k
    case FAAD_FMT_32BIT:
421
1.27k
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
1.27k
        break;
423
667
    case FAAD_FMT_FLOAT:
424
667
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
667
        break;
426
579
    case FAAD_FMT_DOUBLE:
427
579
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
579
        break;
429
5.67k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
5.67k
    return sample_buffer;
437
5.67k
}
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
28.4M
{
449
28.4M
    real_t C;
450
28.4M
    if (up_matrix == 1)
451
289k
        return input[internal_channel[0]][sample];
452
453
28.1M
    if (!down_matrix)
454
27.4M
        return input[internal_channel[channel]][sample];
455
456
705k
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
705k
    if (channel == 0)
458
343k
    {
459
343k
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
343k
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
343k
        return core + C + L_S;
462
362k
    } else {
463
362k
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
362k
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
362k
        return core + C + R_S;
466
362k
    }
467
705k
}
468
469
void* output_to_PCM(NeAACDecStruct *hDecoder,
470
                    real_t **input, void *sample_buffer, uint8_t channels,
471
                    uint16_t frame_len, uint8_t format)
472
6.39k
{
473
6.39k
    uint8_t ch;
474
6.39k
    uint16_t i;
475
6.39k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
6.39k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
6.39k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
46.6k
    for (ch = 0; ch < channels; ch++)
481
40.2k
    {
482
40.2k
        switch (format)
483
40.2k
        {
484
11.9k
        case FAAD_FMT_16BIT:
485
17.1M
            for(i = 0; i < frame_len; i++)
486
17.1M
            {
487
17.1M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
17.1M
                    hDecoder->internal_channel);
489
17.1M
                if (tmp >= 0)
490
15.5M
                {
491
15.5M
                    tmp += (1 << (REAL_BITS-1));
492
15.5M
                    if (tmp >= REAL_CONST(32767))
493
41.3k
                    {
494
41.3k
                        tmp = REAL_CONST(32767);
495
41.3k
                    }
496
15.5M
                } else {
497
1.57M
                    tmp += -(1 << (REAL_BITS-1));
498
1.57M
                    if (tmp <= REAL_CONST(-32768))
499
44.0k
                    {
500
44.0k
                        tmp = REAL_CONST(-32768);
501
44.0k
                    }
502
1.57M
                }
503
17.1M
                tmp >>= REAL_BITS;
504
17.1M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
17.1M
            }
506
11.9k
            break;
507
12.2k
        case FAAD_FMT_24BIT:
508
17.2M
            for(i = 0; i < frame_len; i++)
509
17.2M
            {
510
17.2M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
17.2M
                    hDecoder->internal_channel);
512
17.2M
                if (tmp >= 0)
513
16.0M
                {
514
16.0M
                    tmp += (1 << (REAL_BITS-9));
515
16.0M
                    tmp >>= (REAL_BITS-8);
516
16.0M
                    if (tmp >= 8388607)
517
24.8k
                    {
518
24.8k
                        tmp = 8388607;
519
24.8k
                    }
520
16.0M
                } else {
521
1.18M
                    tmp += -(1 << (REAL_BITS-9));
522
1.18M
                    tmp >>= (REAL_BITS-8);
523
1.18M
                    if (tmp <= -8388608)
524
24.3k
                    {
525
24.3k
                        tmp = -8388608;
526
24.3k
                    }
527
1.18M
                }
528
17.2M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
17.2M
            }
530
12.2k
            break;
531
9.02k
        case FAAD_FMT_32BIT:
532
9.02k
            exp = 16 - REAL_BITS;
533
9.02k
            half = 1 << (exp - 1);
534
9.02k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
12.8M
            for(i = 0; i < frame_len; i++)
536
12.8M
            {
537
12.8M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
12.8M
                    hDecoder->internal_channel);
539
12.8M
                if (tmp >= 0)
540
11.8M
                {
541
11.8M
                    tmp += half;
542
11.8M
                } else {
543
978k
                    tmp += -half;
544
978k
                }
545
12.8M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
12.8M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
12.8M
            }
548
9.02k
            break;
549
7.03k
        case FAAD_FMT_FIXED:
550
9.69M
            for(i = 0; i < frame_len; i++)
551
9.69M
            {
552
9.69M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
9.69M
                    hDecoder->internal_channel);
554
9.69M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
9.69M
            }
556
7.03k
            break;
557
40.2k
        }
558
40.2k
    }
559
560
6.39k
    return sample_buffer;
561
6.39k
}
output_to_PCM
Line
Count
Source
472
3.19k
{
473
3.19k
    uint8_t ch;
474
3.19k
    uint16_t i;
475
3.19k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.19k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.19k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
23.3k
    for (ch = 0; ch < channels; ch++)
481
20.1k
    {
482
20.1k
        switch (format)
483
20.1k
        {
484
5.95k
        case FAAD_FMT_16BIT:
485
8.59M
            for(i = 0; i < frame_len; i++)
486
8.58M
            {
487
8.58M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.58M
                    hDecoder->internal_channel);
489
8.58M
                if (tmp >= 0)
490
7.79M
                {
491
7.79M
                    tmp += (1 << (REAL_BITS-1));
492
7.79M
                    if (tmp >= REAL_CONST(32767))
493
20.6k
                    {
494
20.6k
                        tmp = REAL_CONST(32767);
495
20.6k
                    }
496
7.79M
                } else {
497
787k
                    tmp += -(1 << (REAL_BITS-1));
498
787k
                    if (tmp <= REAL_CONST(-32768))
499
22.0k
                    {
500
22.0k
                        tmp = REAL_CONST(-32768);
501
22.0k
                    }
502
787k
                }
503
8.58M
                tmp >>= REAL_BITS;
504
8.58M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.58M
            }
506
5.95k
            break;
507
6.14k
        case FAAD_FMT_24BIT:
508
8.63M
            for(i = 0; i < frame_len; i++)
509
8.62M
            {
510
8.62M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
8.62M
                    hDecoder->internal_channel);
512
8.62M
                if (tmp >= 0)
513
8.03M
                {
514
8.03M
                    tmp += (1 << (REAL_BITS-9));
515
8.03M
                    tmp >>= (REAL_BITS-8);
516
8.03M
                    if (tmp >= 8388607)
517
12.4k
                    {
518
12.4k
                        tmp = 8388607;
519
12.4k
                    }
520
8.03M
                } else {
521
594k
                    tmp += -(1 << (REAL_BITS-9));
522
594k
                    tmp >>= (REAL_BITS-8);
523
594k
                    if (tmp <= -8388608)
524
12.1k
                    {
525
12.1k
                        tmp = -8388608;
526
12.1k
                    }
527
594k
                }
528
8.62M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
8.62M
            }
530
6.14k
            break;
531
4.51k
        case FAAD_FMT_32BIT:
532
4.51k
            exp = 16 - REAL_BITS;
533
4.51k
            half = 1 << (exp - 1);
534
4.51k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
6.43M
            for(i = 0; i < frame_len; i++)
536
6.42M
            {
537
6.42M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
6.42M
                    hDecoder->internal_channel);
539
6.42M
                if (tmp >= 0)
540
5.93M
                {
541
5.93M
                    tmp += half;
542
5.93M
                } else {
543
489k
                    tmp += -half;
544
489k
                }
545
6.42M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
6.42M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
6.42M
            }
548
4.51k
            break;
549
3.51k
        case FAAD_FMT_FIXED:
550
4.84M
            for(i = 0; i < frame_len; i++)
551
4.84M
            {
552
4.84M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.84M
                    hDecoder->internal_channel);
554
4.84M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.84M
            }
556
3.51k
            break;
557
20.1k
        }
558
20.1k
    }
559
560
3.19k
    return sample_buffer;
561
3.19k
}
output_to_PCM
Line
Count
Source
472
3.19k
{
473
3.19k
    uint8_t ch;
474
3.19k
    uint16_t i;
475
3.19k
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
3.19k
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
3.19k
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
23.3k
    for (ch = 0; ch < channels; ch++)
481
20.1k
    {
482
20.1k
        switch (format)
483
20.1k
        {
484
5.95k
        case FAAD_FMT_16BIT:
485
8.59M
            for(i = 0; i < frame_len; i++)
486
8.58M
            {
487
8.58M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
8.58M
                    hDecoder->internal_channel);
489
8.58M
                if (tmp >= 0)
490
7.79M
                {
491
7.79M
                    tmp += (1 << (REAL_BITS-1));
492
7.79M
                    if (tmp >= REAL_CONST(32767))
493
20.6k
                    {
494
20.6k
                        tmp = REAL_CONST(32767);
495
20.6k
                    }
496
7.79M
                } else {
497
787k
                    tmp += -(1 << (REAL_BITS-1));
498
787k
                    if (tmp <= REAL_CONST(-32768))
499
22.0k
                    {
500
22.0k
                        tmp = REAL_CONST(-32768);
501
22.0k
                    }
502
787k
                }
503
8.58M
                tmp >>= REAL_BITS;
504
8.58M
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
8.58M
            }
506
5.95k
            break;
507
6.14k
        case FAAD_FMT_24BIT:
508
8.63M
            for(i = 0; i < frame_len; i++)
509
8.62M
            {
510
8.62M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
8.62M
                    hDecoder->internal_channel);
512
8.62M
                if (tmp >= 0)
513
8.03M
                {
514
8.03M
                    tmp += (1 << (REAL_BITS-9));
515
8.03M
                    tmp >>= (REAL_BITS-8);
516
8.03M
                    if (tmp >= 8388607)
517
12.4k
                    {
518
12.4k
                        tmp = 8388607;
519
12.4k
                    }
520
8.03M
                } else {
521
594k
                    tmp += -(1 << (REAL_BITS-9));
522
594k
                    tmp >>= (REAL_BITS-8);
523
594k
                    if (tmp <= -8388608)
524
12.1k
                    {
525
12.1k
                        tmp = -8388608;
526
12.1k
                    }
527
594k
                }
528
8.62M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
8.62M
            }
530
6.14k
            break;
531
4.51k
        case FAAD_FMT_32BIT:
532
4.51k
            exp = 16 - REAL_BITS;
533
4.51k
            half = 1 << (exp - 1);
534
4.51k
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
6.43M
            for(i = 0; i < frame_len; i++)
536
6.42M
            {
537
6.42M
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
6.42M
                    hDecoder->internal_channel);
539
6.42M
                if (tmp >= 0)
540
5.93M
                {
541
5.93M
                    tmp += half;
542
5.93M
                } else {
543
489k
                    tmp += -half;
544
489k
                }
545
6.42M
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
6.42M
                int_sample_buffer[(i*channels)+ch] = tmp;
547
6.42M
            }
548
4.51k
            break;
549
3.51k
        case FAAD_FMT_FIXED:
550
4.84M
            for(i = 0; i < frame_len; i++)
551
4.84M
            {
552
4.84M
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
4.84M
                    hDecoder->internal_channel);
554
4.84M
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
4.84M
            }
556
3.51k
            break;
557
20.1k
        }
558
20.1k
    }
559
560
3.19k
    return sample_buffer;
561
3.19k
}
562
563
#endif