Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/faac/libfaad/stereo.c
Line
Count
Source
1
/*
2
 * FAAD - Freeware Advanced Audio Decoder
3
 * Copyright (C) 2026 Nils Schimmelmann
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 */
15
16
#include "faad_internal.h"
17
18
/* 0.5^(is_position/4) for is_position in [-IS_POS_RANGE, IS_POS_RANGE]. */
19
20.7k
#define IS_POS_RANGE 128
20
static float is_scale_lut[2 * IS_POS_RANGE + 1];
21
22
void init_is_tables(void)
23
2
{
24
516
    for (int p = -IS_POS_RANGE; p <= IS_POS_RANGE; p++) {
25
514
        is_scale_lut[p + IS_POS_RANGE] = powf(0.5f, 0.25f * (float)p);
26
514
    }
27
2
}
28
29
void apply_ms_stereo(CPEInfo *cpe, float * restrict spec_l, float * restrict spec_r)
30
2.56k
{
31
2.56k
    ICSInfo *ics = &cpe->ics[0];
32
33
2.56k
    int window_offset = 0;
34
6.52k
    for (int g = 0; g < ics->num_window_groups && g < 8; g++) {
35
24.6k
        for (int sfb = 0; sfb < ics->max_sfb && sfb < MAX_SFB; sfb++) {
36
20.6k
            bool ms_flag = false;
37
20.6k
            if (cpe->ms_mask_present == MS_MASK_EXPLICIT) {
38
2.96k
                ms_flag = (cpe->ms_used[g][sfb] != 0);
39
17.7k
            } else if (cpe->ms_mask_present != MS_MASK_NONE) {
40
4.77k
                ms_flag = true;
41
4.77k
            }
42
43
20.6k
            if (!ms_flag) continue;
44
20.6k
            bool pns_l = ics->sfb_cb[g][sfb] == 13, pns_r = cpe->ics[1].sfb_cb[g][sfb] == 13;
45
6.05k
            if (pns_l != pns_r) continue;
46
47
#ifdef FAAD_STATS
48
            /* Counted per channel slot, the granularity of totalBands. */
49
            g_faadStats.msBands += 2;
50
#endif
51
52
5.57k
#ifndef FAAD_DISABLE_DAB
53
5.57k
            int short_len = (ics->frame_len_long == 960) ? 120 : 128;
54
5.57k
            int long_len = (ics->frame_len_long == 960) ? 960 : 1024;
55
#else
56
            int short_len = 128;
57
            int long_len = 1024;
58
#endif
59
5.57k
            int start_k = ics->sfb_offsets[sfb];
60
5.57k
            int end_k = ics->sfb_offsets[sfb + 1];
61
5.57k
            if (start_k >= long_len) continue;
62
5.57k
            if (end_k > long_len) end_k = long_len;
63
5.57k
            int len = end_k - start_k;
64
65
14.6k
            for (int w = 0; w < ics->window_group_length[g]; w++) {
66
9.06k
                int win_offset_k = (window_offset + w) * short_len + start_k;
67
9.06k
                if (win_offset_k < 0 || win_offset_k + len > long_len) continue;
68
9.06k
                float * restrict l_ptr = spec_l + win_offset_k;
69
9.06k
                float * restrict r_ptr = spec_r + win_offset_k;
70
71
9.06k
                if (pns_l) {
72
                    /* §4.6.13.3: ms_used on a PNS band means correlated noise:
73
                     * the right channel reuses the left vector at its own level. */
74
1.57k
                    float gain = powf(2.0f, 0.25f * (float)(cpe->ics[1].scalefactors[g][sfb] - ics->scalefactors[g][sfb]));
75
11.5k
                    for (int k = 0; k < len; k++) r_ptr[k] = l_ptr[k] * gain;
76
7.48k
                } else {
77
77.5k
                    for (int k = 0; k < len; k++) {
78
70.0k
                        float m = l_ptr[k];
79
70.0k
                        float s = r_ptr[k];
80
70.0k
                        l_ptr[k] = m + s;
81
70.0k
                        r_ptr[k] = m - s;
82
70.0k
                    }
83
7.48k
                }
84
9.06k
            }
85
5.57k
        }
86
3.95k
        window_offset += ics->window_group_length[g];
87
3.95k
    }
88
2.56k
}
89
90
void apply_is_stereo(CPEInfo *cpe, float * restrict spec_l, float * restrict spec_r)
91
2.56k
{
92
2.56k
    ICSInfo *ics_r = &cpe->ics[1];
93
94
2.56k
    int window_offset = 0;
95
12.1k
    for (int g = 0; g < ics_r->num_window_groups && g < 8; g++) {
96
30.2k
        for (int sfb = 0; sfb < ics_r->max_sfb && sfb < MAX_SFB; sfb++) {
97
20.7k
            int cb = ics_r->sfb_cb[g][sfb];
98
20.7k
            if (cb == 14 || cb == 15) {
99
6.12k
                {
100
6.12k
                    int pos = ics_r->scalefactors[g][sfb];
101
6.12k
                    if (pos < -IS_POS_RANGE) pos = -IS_POS_RANGE;
102
6.12k
                    if (pos > IS_POS_RANGE) pos = IS_POS_RANGE;
103
6.12k
                    float scale = is_scale_lut[pos + IS_POS_RANGE];
104
6.12k
                    if (cb == 14) scale = -scale; /* INTENSITY_HCB2: out of phase */
105
                    /* §4.6.8.2.3: the all-one mask also flips intensity polarity. */
106
6.12k
                    if (cpe->ms_mask_present == MS_MASK_ALL ||
107
5.28k
                        (cpe->ms_mask_present == MS_MASK_EXPLICIT && cpe->ms_used[g][sfb]))
108
1.33k
                        scale = -scale;
109
110
6.12k
#ifndef FAAD_DISABLE_DAB
111
6.12k
                    int short_len = (ics_r->frame_len_long == 960) ? 120 : 128;
112
6.12k
                    int long_len = (ics_r->frame_len_long == 960) ? 960 : 1024;
113
#else
114
                    int short_len = 128;
115
                    int long_len = 1024;
116
#endif
117
6.12k
                    int start_k = ics_r->sfb_offsets[sfb];
118
6.12k
                    int end_k = ics_r->sfb_offsets[sfb + 1];
119
6.12k
                    if (start_k >= long_len) continue;
120
6.12k
                    if (end_k > long_len) end_k = long_len;
121
6.12k
                    int len = end_k - start_k;
122
123
13.9k
                    for (int w = 0; w < ics_r->window_group_length[g]; w++) {
124
7.79k
                        int win_idx = window_offset + w;
125
7.79k
                        const float * restrict l_ptr = spec_l + win_idx * short_len + start_k;
126
7.79k
                        float * restrict r_ptr = spec_r + win_idx * short_len + start_k;
127
128
82.2k
                        for (int k = 0; k < len; k++) {
129
74.4k
                            r_ptr[k] = l_ptr[k] * scale;
130
74.4k
                        }
131
7.79k
                    }
132
6.12k
                }
133
6.12k
            }
134
20.7k
        }
135
9.53k
        window_offset += ics_r->window_group_length[g];
136
9.53k
    }
137
2.56k
}