Coverage Report

Created: 2026-01-10 06:28

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
7.47M
#define FLOAT_SCALE (1.0f/(1<<15))
40
41
1.24M
#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
42
2.49M
#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
21.9M
{
48
21.9M
    if (!down_matrix)
49
20.6M
        return input[internal_channel[channel]][sample];
50
51
1.24M
    if (channel == 0)
52
611k
    {
53
611k
        return DM_MUL * (input[internal_channel[1]][sample] +
54
611k
            input[internal_channel[0]][sample] * RSQRT2 +
55
611k
            input[internal_channel[3]][sample] * RSQRT2);
56
635k
    } else {
57
635k
        return DM_MUL * (input[internal_channel[2]][sample] +
58
635k
            input[internal_channel[0]][sample] * RSQRT2 +
59
635k
            input[internal_channel[4]][sample] * RSQRT2);
60
635k
    }
61
1.24M
}
62
63
#ifndef HAS_LRINTF
64
17.7M
#define CLIP(sample, max, min) \
65
17.7M
if (sample >= 0.0f)            \
66
15.6M
{                              \
67
15.6M
    sample += 0.5f;            \
68
15.6M
    if (sample >= max)         \
69
15.6M
        sample = max;          \
70
15.6M
} else {                       \
71
2.14M
    sample += -0.5f;           \
72
2.14M
    if (sample <= min)         \
73
2.14M
        sample = min;          \
74
2.14M
}
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.19k
#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
909
{
93
909
    uint8_t ch, ch1;
94
909
    uint16_t i;
95
96
909
    switch (CONV(channels,hDecoder->downMatrix))
97
909
    {
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
361
    case CONV(2,0):
110
361
        if (hDecoder->upMatrix)
111
54
        {
112
54
            ch  = hDecoder->internal_channel[0];
113
81.7k
            for(i = 0; i < frame_len; i++)
114
81.7k
            {
115
81.7k
                real_t inp0 = input[ch][i];
116
117
81.7k
                CLIP(inp0, 32767.0f, -32768.0f);
118
119
81.7k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
120
81.7k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
121
81.7k
            }
122
307
        } else {
123
307
            ch  = hDecoder->internal_channel[0];
124
307
            ch1 = hDecoder->internal_channel[1];
125
453k
            for(i = 0; i < frame_len; i++)
126
453k
            {
127
453k
                real_t inp0 = input[ch ][i];
128
453k
                real_t inp1 = input[ch1][i];
129
130
453k
                CLIP(inp0, 32767.0f, -32768.0f);
131
453k
                CLIP(inp1, 32767.0f, -32768.0f);
132
133
453k
                (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
134
453k
                (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
135
453k
            }
136
307
        }
137
361
        break;
138
548
    default:
139
5.58k
        for (ch = 0; ch < channels; ch++)
140
5.03k
        {
141
6.87M
            for(i = 0; i < frame_len; i++)
142
6.86M
            {
143
6.86M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
144
145
6.86M
                CLIP(inp, 32767.0f, -32768.0f);
146
147
6.86M
                (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
148
6.86M
            }
149
5.03k
        }
150
548
        break;
151
909
    }
152
909
}
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
773
{
158
773
    uint8_t ch, ch1;
159
773
    uint16_t i;
160
161
773
    switch (CONV(channels,hDecoder->downMatrix))
162
773
    {
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
360
    case CONV(2,0):
176
360
        if (hDecoder->upMatrix)
177
38
        {
178
38
            ch = hDecoder->internal_channel[0];
179
59.1k
            for(i = 0; i < frame_len; i++)
180
59.1k
            {
181
59.1k
                real_t inp0 = input[ch][i];
182
183
59.1k
                inp0 *= 256.0f;
184
59.1k
                CLIP(inp0, 8388607.0f, -8388608.0f);
185
186
59.1k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
187
59.1k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
188
59.1k
            }
189
322
        } else {
190
322
            ch  = hDecoder->internal_channel[0];
191
322
            ch1 = hDecoder->internal_channel[1];
192
460k
            for(i = 0; i < frame_len; i++)
193
459k
            {
194
459k
                real_t inp0 = input[ch ][i];
195
459k
                real_t inp1 = input[ch1][i];
196
197
459k
                inp0 *= 256.0f;
198
459k
                inp1 *= 256.0f;
199
459k
                CLIP(inp0, 8388607.0f, -8388608.0f);
200
459k
                CLIP(inp1, 8388607.0f, -8388608.0f);
201
202
459k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
203
459k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
204
459k
            }
205
322
        }
206
360
        break;
207
413
    default:
208
3.46k
        for (ch = 0; ch < channels; ch++)
209
3.05k
        {
210
4.61M
            for(i = 0; i < frame_len; i++)
211
4.61M
            {
212
4.61M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
213
214
4.61M
                inp *= 256.0f;
215
4.61M
                CLIP(inp, 8388607.0f, -8388608.0f);
216
217
4.61M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
218
4.61M
            }
219
3.05k
        }
220
413
        break;
221
773
    }
222
773
}
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
627
{
228
627
    uint8_t ch, ch1;
229
627
    uint16_t i;
230
231
627
    switch (CONV(channels,hDecoder->downMatrix))
232
627
    {
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
318
    case CONV(2,0):
246
318
        if (hDecoder->upMatrix)
247
41
        {
248
41
            ch = hDecoder->internal_channel[0];
249
61.3k
            for(i = 0; i < frame_len; i++)
250
61.3k
            {
251
61.3k
                real_t inp0 = input[ch][i];
252
253
61.3k
                inp0 *= 65536.0f;
254
61.3k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
255
256
61.3k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
257
61.3k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
258
61.3k
            }
259
277
        } else {
260
277
            ch  = hDecoder->internal_channel[0];
261
277
            ch1 = hDecoder->internal_channel[1];
262
412k
            for(i = 0; i < frame_len; i++)
263
412k
            {
264
412k
                real_t inp0 = input[ch ][i];
265
412k
                real_t inp1 = input[ch1][i];
266
267
412k
                inp0 *= 65536.0f;
268
412k
                inp1 *= 65536.0f;
269
412k
                CLIP(inp0, 2147483647.0f, -2147483648.0f);
270
412k
                CLIP(inp1, 2147483647.0f, -2147483648.0f);
271
272
412k
                (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
273
412k
                (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
274
412k
            }
275
277
        }
276
318
        break;
277
309
    default:
278
2.79k
        for (ch = 0; ch < channels; ch++)
279
2.48k
        {
280
3.43M
            for(i = 0; i < frame_len; i++)
281
3.43M
            {
282
3.43M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
283
284
3.43M
                inp *= 65536.0f;
285
3.43M
                CLIP(inp, 2147483647.0f, -2147483648.0f);
286
287
3.43M
                (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
288
3.43M
            }
289
2.48k
        }
290
309
        break;
291
627
    }
292
627
}
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
334
{
298
334
    uint8_t ch, ch1;
299
334
    uint16_t i;
300
301
334
    switch (CONV(channels,hDecoder->downMatrix))
302
334
    {
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
84
    case CONV(2,0):
312
84
        if (hDecoder->upMatrix)
313
16
        {
314
16
            ch = hDecoder->internal_channel[0];
315
25.4k
            for(i = 0; i < frame_len; i++)
316
25.4k
            {
317
25.4k
                real_t inp0 = input[ch][i];
318
25.4k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
319
25.4k
                (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
320
25.4k
            }
321
68
        } else {
322
68
            ch  = hDecoder->internal_channel[0];
323
68
            ch1 = hDecoder->internal_channel[1];
324
85.7k
            for(i = 0; i < frame_len; i++)
325
85.6k
            {
326
85.6k
                real_t inp0 = input[ch ][i];
327
85.6k
                real_t inp1 = input[ch1][i];
328
85.6k
                (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
329
85.6k
                (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
330
85.6k
            }
331
68
        }
332
84
        break;
333
250
    default:
334
2.29k
        for (ch = 0; ch < channels; ch++)
335
2.04k
        {
336
3.12M
            for(i = 0; i < frame_len; i++)
337
3.12M
            {
338
3.12M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
339
3.12M
                (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
340
3.12M
            }
341
2.04k
        }
342
250
        break;
343
334
    }
344
334
}
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
{
350
350
    uint8_t ch, ch1;
351
350
    uint16_t i;
352
353
350
    switch (CONV(channels,hDecoder->downMatrix))
354
350
    {
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
81
    case CONV(2,0):
364
81
        if (hDecoder->upMatrix)
365
26
        {
366
26
            ch = hDecoder->internal_channel[0];
367
42.7k
            for(i = 0; i < frame_len; i++)
368
42.6k
            {
369
42.6k
                real_t inp0 = input[ch][i];
370
42.6k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
371
42.6k
                (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
372
42.6k
            }
373
55
        } else {
374
55
            ch  = hDecoder->internal_channel[0];
375
55
            ch1 = hDecoder->internal_channel[1];
376
72.8k
            for(i = 0; i < frame_len; i++)
377
72.8k
            {
378
72.8k
                real_t inp0 = input[ch ][i];
379
72.8k
                real_t inp1 = input[ch1][i];
380
72.8k
                (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
381
72.8k
                (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
382
72.8k
            }
383
55
        }
384
81
        break;
385
269
    default:
386
2.70k
        for (ch = 0; ch < channels; ch++)
387
2.43k
        {
388
3.90M
            for(i = 0; i < frame_len; i++)
389
3.90M
            {
390
3.90M
                real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
391
3.90M
                (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
392
3.90M
            }
393
2.43k
        }
394
269
        break;
395
350
    }
396
350
}
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.99k
{
402
2.99k
    int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
403
2.99k
    int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
404
2.99k
    float32_t *float_sample_buffer = (float32_t*)sample_buffer;
405
2.99k
    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.99k
    switch (format)
413
2.99k
    {
414
909
    case FAAD_FMT_16BIT:
415
909
        to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
416
909
        break;
417
773
    case FAAD_FMT_24BIT:
418
773
        to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
419
773
        break;
420
627
    case FAAD_FMT_32BIT:
421
627
        to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
422
627
        break;
423
334
    case FAAD_FMT_FLOAT:
424
334
        to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
425
334
        break;
426
350
    case FAAD_FMT_DOUBLE:
427
350
        to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
428
350
        break;
429
2.99k
    }
430
431
#ifdef PROFILE
432
    count = faad_get_ts() - count;
433
    hDecoder->output_cycles += count;
434
#endif
435
436
2.99k
    return sample_buffer;
437
2.99k
}
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
                    tmp += (1 << (REAL_BITS-1));
492
                    if (tmp >= REAL_CONST(32767))
493
                    {
494
                        tmp = REAL_CONST(32767);
495
                    }
496
                } else {
497
                    tmp += -(1 << (REAL_BITS-1));
498
                    if (tmp <= REAL_CONST(-32768))
499
                    {
500
                        tmp = REAL_CONST(-32768);
501
                    }
502
                }
503
                tmp >>= REAL_BITS;
504
                short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
505
            }
506
            break;
507
        case FAAD_FMT_24BIT:
508
            for(i = 0; i < frame_len; i++)
509
            {
510
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
511
                    hDecoder->internal_channel);
512
                if (tmp >= 0)
513
                {
514
                    tmp += (1 << (REAL_BITS-9));
515
                    tmp >>= (REAL_BITS-8);
516
                    if (tmp >= 8388607)
517
                    {
518
                        tmp = 8388607;
519
                    }
520
                } else {
521
                    tmp += -(1 << (REAL_BITS-9));
522
                    tmp >>= (REAL_BITS-8);
523
                    if (tmp <= -8388608)
524
                    {
525
                        tmp = -8388608;
526
                    }
527
                }
528
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
529
            }
530
            break;
531
        case FAAD_FMT_32BIT:
532
            exp = 16 - REAL_BITS;
533
            half = 1 << (exp - 1);
534
            sat_shift_mask = SAT_SHIFT_MASK(exp);
535
            for(i = 0; i < frame_len; i++)
536
            {
537
                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
538
                    hDecoder->internal_channel);
539
                if (tmp >= 0)
540
                {
541
                    tmp += half;
542
                } else {
543
                    tmp += -half;
544
                }
545
                tmp = SAT_SHIFT(tmp, exp, sat_shift_mask);
546
                int_sample_buffer[(i*channels)+ch] = tmp;
547
            }
548
            break;
549
        case FAAD_FMT_FIXED:
550
            for(i = 0; i < frame_len; i++)
551
            {
552
                real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
553
                    hDecoder->internal_channel);
554
                int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
555
            }
556
            break;
557
        }
558
    }
559
560
    return sample_buffer;
561
}
562
563
#endif