Coverage Report

Created: 2026-10-17 06:34

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
8.18M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
1.33M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
2.66M
#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
24.1M
{
48
24.1M
    if (!down_matrix)
49
22.8M
        return input[internal_channel[channel]][sample];
50
51
1.33M
    if (channel == 0)
52
653k
    {
53
653k
        return DM_MUL * (input[internal_channel[1]][sample] +
54
653k
            input[internal_channel[0]][sample] * RSQRT2 +
55
653k
            input[internal_channel[3]][sample] * RSQRT2);
56
677k
    } else {
57
677k
        return DM_MUL * (input[internal_channel[2]][sample] +
58
677k
            input[internal_channel[0]][sample] * RSQRT2 +
59
677k
            input[internal_channel[4]][sample] * RSQRT2);
60
677k
    }
61
1.33M
}
62
63
#ifndef HAS_LRINTF
64
17.5M
#define CLIP(sample, max, min) \
65
17.5M
if (sample >= 0.0f)            \
66
15.5M
{                              \
67
15.5M
    sample += 0.5f;            \
68
15.5M
    if (sample >= max)         \
69
15.5M
        sample = max;          \
70
15.5M
} else {                       \
71
1.98M
    sample += -0.5f;           \
72
1.98M
    if (sample <= min)         \
73
1.98M
        sample = min;          \
74
1.98M
}
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
4.06k
#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
791
{
93
791
    uint8_t ch, ch1;
94
791
    uint16_t i;
95
96
791
    switch (CONV(channels,hDecoder->downMatrix))
97
791
    {
98
165
    case CONV(1,0):
99
166
    case CONV(1,1):
100
200k
        for(i = 0; i < frame_len; i++)
101
200k
        {
102
200k
            real_t inp = input[hDecoder->internal_channel[0]][i];
103
104
200k
            CLIP(inp, 32767.0f, -32768.0f);
105
106
200k
            (*sample_buffer)[i] = (int16_t)lrintf(inp);
107
200k
        }
108
166
        break;
109
114
    case CONV(2,0):
110
114
        if (hDecoder->upMatrix)
111
0
        {
112
0
            ch  = hDecoder->internal_channel[0];
113
0
            for(i = 0; i < frame_len; i++)
114
0
            {
115
0
                real_t inp0 = input[ch][i];
116
117
0
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
0
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
0
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
0
            }
122
114
        } else {
123
114
            ch  = hDecoder->internal_channel[0];
124
114
            ch1 = hDecoder->internal_channel[1];
125
166k
            for(i = 0; i < frame_len; i++)
126
166k
            {
127
166k
                real_t inp0 = input[ch ][i];
128
166k
                real_t inp1 = input[ch1][i];
129
130
166k
                CLIP(inp0, 32767.0f, -32768.0f);
131
166k
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
166k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
166k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
166k
            }
136
114
        }
137
114
        break;
138
511
    default:
139
5.22k
        for (ch = 0; ch < channels; ch++)
140
4.71k
        {
141
6.90M
            for(i = 0; i < frame_len; i++)
142
6.89M
            {
143
6.89M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
6.89M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
6.89M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
6.89M
            }
149
4.71k
        }
150
511
        break;
151
791
    }
152
791
}
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
670
{
158
670
    uint8_t ch, ch1;
159
670
    uint16_t i;
160
161
670
    switch (CONV(channels,hDecoder->downMatrix))
162
670
    {
163
154
    case CONV(1,0):
164
155
    case CONV(1,1):
165
184k
        for(i = 0; i < frame_len; i++)
166
183k
        {
167
183k
            real_t inp = input[hDecoder->internal_channel[0]][i];
168
169
183k
            inp *= 256.0f;
170
183k
            CLIP(inp, 8388607.0f, -8388608.0f);
171
172
183k
            (*sample_buffer)[i] = (int32_t)lrintf(inp);
173
183k
        }
174
155
        break;
175
89
    case CONV(2,0):
176
89
        if (hDecoder->upMatrix)
177
0
        {
178
0
            ch = hDecoder->internal_channel[0];
179
0
            for(i = 0; i < frame_len; i++)
180
0
            {
181
0
                real_t inp0 = input[ch][i];
182
183
0
                inp0 *= 256.0f;
184
0
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
0
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
0
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
0
            }
189
89
        } else {
190
89
            ch  = hDecoder->internal_channel[0];
191
89
            ch1 = hDecoder->internal_channel[1];
192
138k
            for(i = 0; i < frame_len; i++)
193
138k
            {
194
138k
                real_t inp0 = input[ch ][i];
195
138k
                real_t inp1 = input[ch1][i];
196
197
138k
                inp0 *= 256.0f;
198
138k
                inp1 *= 256.0f;
199
138k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
138k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
138k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
138k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
138k
            }
205
89
        }
206
89
        break;
207
426
    default:
208
3.70k
        for (ch = 0; ch < channels; ch++)
209
3.27k
        {
210
4.58M
            for(i = 0; i < frame_len; i++)
211
4.58M
            {
212
4.58M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
4.58M
                inp *= 256.0f;
215
4.58M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
4.58M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
4.58M
            }
219
3.27k
        }
220
426
        break;
221
670
    }
222
670
}
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
673
{
228
673
    uint8_t ch, ch1;
229
673
    uint16_t i;
230
231
673
    switch (CONV(channels,hDecoder->downMatrix))
232
673
    {
233
146
    case CONV(1,0):
234
147
    case CONV(1,1):
235
157k
        for(i = 0; i < frame_len; i++)
236
157k
        {
237
157k
            real_t inp = input[hDecoder->internal_channel[0]][i];
238
239
157k
            inp *= 65536.0f;
240
157k
            CLIP(inp, 2147483647.0f, -2147483648.0f);
241
242
157k
            (*sample_buffer)[i] = (int32_t)lrintf(inp);
243
157k
        }
244
147
        break;
245
96
    case CONV(2,0):
246
96
        if (hDecoder->upMatrix)
247
0
        {
248
0
            ch = hDecoder->internal_channel[0];
249
0
            for(i = 0; i < frame_len; i++)
250
0
            {
251
0
                real_t inp0 = input[ch][i];
252
253
0
                inp0 *= 65536.0f;
254
0
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
0
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
0
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
0
            }
259
96
        } else {
260
96
            ch  = hDecoder->internal_channel[0];
261
96
            ch1 = hDecoder->internal_channel[1];
262
140k
            for(i = 0; i < frame_len; i++)
263
140k
            {
264
140k
                real_t inp0 = input[ch ][i];
265
140k
                real_t inp1 = input[ch1][i];
266
267
140k
                inp0 *= 65536.0f;
268
140k
                inp1 *= 65536.0f;
269
140k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
140k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
140k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
140k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
140k
            }
275
96
        }
276
96
        break;
277
430
    default:
278
3.81k
        for (ch = 0; ch < channels; ch++)
279
3.38k
        {
280
4.64M
            for(i = 0; i < frame_len; i++)
281
4.64M
            {
282
4.64M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
4.64M
                inp *= 65536.0f;
285
4.64M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
4.64M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
4.64M
            }
289
3.38k
        }
290
430
        break;
291
673
    }
292
673
}
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
337
{
298
337
    uint8_t ch, ch1;
299
337
    uint16_t i;
300
301
337
    switch (CONV(channels,hDecoder->downMatrix))
302
337
    {
303
21
    case CONV(1,0):
304
22
    case CONV(1,1):
305
40.5k
        for(i = 0; i < frame_len; i++)
306
40.5k
        {
307
40.5k
            real_t inp = input[hDecoder->internal_channel[0]][i];
308
40.5k
            (*sample_buffer)[i] = inp*FLOAT_SCALE;
309
40.5k
        }
310
22
        break;
311
15
    case CONV(2,0):
312
15
        if (hDecoder->upMatrix)
313
0
        {
314
0
            ch = hDecoder->internal_channel[0];
315
0
            for(i = 0; i < frame_len; i++)
316
0
            {
317
0
                real_t inp0 = input[ch][i];
318
0
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
0
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
0
            }
321
15
        } else {
322
15
            ch  = hDecoder->internal_channel[0];
323
15
            ch1 = hDecoder->internal_channel[1];
324
19.7k
            for(i = 0; i < frame_len; i++)
325
19.7k
            {
326
19.7k
                real_t inp0 = input[ch ][i];
327
19.7k
                real_t inp1 = input[ch1][i];
328
19.7k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
19.7k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
19.7k
            }
331
15
        }
332
15
        break;
333
300
    default:
334
3.08k
        for (ch = 0; ch < channels; ch++)
335
2.78k
        {
336
4.65M
            for(i = 0; i < frame_len; i++)
337
4.65M
            {
338
4.65M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
4.65M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
4.65M
            }
341
2.78k
        }
342
300
        break;
343
337
    }
344
337
}
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
244
{
350
244
    uint8_t ch, ch1;
351
244
    uint16_t i;
352
353
244
    switch (CONV(channels,hDecoder->downMatrix))
354
244
    {
355
20
    case CONV(1,0):
356
21
    case CONV(1,1):
357
31.3k
        for(i = 0; i < frame_len; i++)
358
31.2k
        {
359
31.2k
            real_t inp = input[hDecoder->internal_channel[0]][i];
360
31.2k
            (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
361
31.2k
        }
362
21
        break;
363
16
    case CONV(2,0):
364
16
        if (hDecoder->upMatrix)
365
0
        {
366
0
            ch = hDecoder->internal_channel[0];
367
0
            for(i = 0; i < frame_len; i++)
368
0
            {
369
0
                real_t inp0 = input[ch][i];
370
0
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
0
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
0
            }
373
16
        } else {
374
16
            ch  = hDecoder->internal_channel[0];
375
16
            ch1 = hDecoder->internal_channel[1];
376
21.5k
            for(i = 0; i < frame_len; i++)
377
21.5k
            {
378
21.5k
                real_t inp0 = input[ch ][i];
379
21.5k
                real_t inp1 = input[ch1][i];
380
21.5k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
21.5k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
21.5k
            }
383
16
        }
384
16
        break;
385
207
    default:
386
2.28k
        for (ch = 0; ch < channels; ch++)
387
2.08k
        {
388
3.38M
            for(i = 0; i < frame_len; i++)
389
3.37M
            {
390
3.37M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
3.37M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
3.37M
            }
393
2.08k
        }
394
207
        break;
395
244
    }
396
244
}
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
2.71k
{
402
2.71k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
2.71k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
2.71k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
2.71k
    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
2.71k
    switch (format)
413
2.71k
    {
414
791
    case FAAD_FMT_16BIT:
415
791
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
791
        break;
417
670
    case FAAD_FMT_24BIT:
418
670
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
670
        break;
420
673
    case FAAD_FMT_32BIT:
421
673
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
673
        break;
423
337
    case FAAD_FMT_FLOAT:
424
337
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
337
        break;
426
244
    case FAAD_FMT_DOUBLE:
427
244
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
244
        break;
429
2.71k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
2.71k
    return sample_buffer;
437
2.71k
}
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
{
449
    real_t C;
450
    if (up_matrix == 1)
451
        return input[internal_channel[0]][sample];
452
453
    if (!down_matrix)
454
        return input[internal_channel[channel]][sample];
455
456
    C = MUL_F(input[internal_channel[0]][sample], BOTH);
457
    if (channel == 0)
458
    {
459
        real_t L_S = MUL_F(input[internal_channel[3]][sample], BOTH);
460
        real_t core = MUL_F(input[internal_channel[1]][sample], DM_MUL);
461
        return core + C + L_S;
462
    } else {
463
        real_t R_S = MUL_F(input[internal_channel[4]][sample], BOTH);
464
        real_t core = MUL_F(input[internal_channel[2]][sample], DM_MUL);
465
        return core + C + R_S;
466
    }
467
}
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
{
473
    uint8_t ch;
474
    uint16_t i;
475
    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
476
    int32_t *int_sample_buffer = (int32_t*)sample_buffer;
477
    int32_t exp, half, sat_shift_mask;
478
479
    /* Copy output to a standard PCM buffer */
480
    for (ch = 0; ch < channels; ch++)
481
    {
482
        switch (format)
483
        {
484
        case FAAD_FMT_16BIT:
485
            for(i = 0; i < frame_len; i++)
486
            {
487
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
488
                    hDecoder->internal_channel);
489
                if (tmp >= 0)
490
                {
491
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-1)))
492
                        tmp += (1 << (REAL_BITS-1));
493
                    if (tmp >= REAL_CONST(32767))
494
                    {
495
                        tmp = REAL_CONST(32767);
496
                    }
497
                } else {
498
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-1)))
499
                        tmp += -(1 << (REAL_BITS-1));
500
                    if (tmp <= REAL_CONST(-32768))
501
                    {
502
                        tmp = REAL_CONST(-32768);
503
                    }
504
                }
505
                tmp >>= REAL_BITS;
506
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
507
            }
508
            break;
509
        case FAAD_FMT_24BIT:
510
            for(i = 0; i < frame_len; i++)
511
            {
512
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
513
                    hDecoder->internal_channel);
514
                if (tmp >= 0)
515
                {
516
                    if (tmp <= 0x7FFFFFFF - (1 << (REAL_BITS-9)))
517
                        tmp += (1 << (REAL_BITS-9));
518
                    tmp >>= (REAL_BITS-8);
519
                    if (tmp >= 8388607)
520
                    {
521
                        tmp = 8388607;
522
                    }
523
                } else {
524
                    if (tmp >= (int32_t)0x80000000 + (1 << (REAL_BITS-9)))
525
                        tmp += -(1 << (REAL_BITS-9));
526
                    tmp >>= (REAL_BITS-8);
527
                    if (tmp <= -8388608)
528
                    {
529
                        tmp = -8388608;
530
                    }
531
                }
532
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
533
            }
534
            break;
535
        case FAAD_FMT_32BIT:
536
            exp = 16 - REAL_BITS;
537
            half = 1 << (exp - 1);
538
            sat_shift_mask = SAT_SHIFT_MASK(exp);
539
            for(i = 0; i < frame_len; i++)
540
            {
541
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
542
                    hDecoder->internal_channel);
543
                if (tmp >= 0)
544
                {
545
                    if (tmp <= 0x7FFFFFFF - half)
546
                        tmp += half;
547
                } else {
548
                    if (tmp >= (int32_t)0x80000000 + half)
549
                        tmp += -half;
550
                }
551
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
552
                int_sample_buffer[(i*channels)+ch] = tmp;
553
            }
554
            break;
555
        case FAAD_FMT_FIXED:
556
            for(i = 0; i < frame_len; i++)
557
            {
558
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
559
                    hDecoder->internal_channel);
560
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
561
            }
562
            break;
563
        }
564
    }
565
566
    return sample_buffer;
567
}
568
569
#endif