Coverage Report

Created: 2025-07-11 06:40

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