Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/faac/libfaad/huffman.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
#include "sfb_tables.h"
18
#include <math.h>
19
20
13.2M
#define POW_4_3_LUT_SIZE 128
21
static float pow_4_3_lut[POW_4_3_LUT_SIZE];
22
static float sf_scale_lut[256];
23
24
void init_dequant_tables(void)
25
1
{
26
129
    for (int i = 0; i < POW_4_3_LUT_SIZE; i++) {
27
128
        pow_4_3_lut[i] = powf((float)i, 4.0f / 3.0f);
28
128
    }
29
257
    for (int i = 0; i < 256; i++) {
30
256
        sf_scale_lut[i] = powf(2.0f, 0.25f * (i - 100));
31
256
    }
32
1
}
33
34
void setup_sfb_offsets(ICSInfo *ics, uint32_t sample_rate)
35
890k
{
36
890k
    memset(ics->sfb_offsets, 0, sizeof(ics->sfb_offsets));
37
890k
    int sr_idx = get_sr_index(sample_rate);
38
890k
    ics->sample_rate_index = (int8_t)sr_idx;
39
890k
    if (sr_idx < 0) {
40
0
        ics->num_sfbs = 0;      /* faad_decoder_open() rejects such rates */
41
0
        return;
42
0
    }
43
890k
#ifndef FAAD_DISABLE_DAB
44
890k
    bool is_960 = ICS_IS_960(ics);
45
890k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
46
64.6k
        ics->num_sfbs = is_960 ? num_sfbs_120[sr_idx] : num_sfbs_128[sr_idx];
47
64.6k
        const uint16_t *offsets = is_960 ? sfb_offsets_120[sr_idx] : sfb_offsets_128[sr_idx];
48
1.03M
        for (int i = 0; i <= ics->num_sfbs && i < 68; i++) {
49
972k
            ics->sfb_offsets[i] = offsets[i];
50
972k
        }
51
825k
    } else {
52
825k
        ics->num_sfbs = is_960 ? num_sfbs_960[sr_idx] : num_sfbs_1024[sr_idx];
53
825k
        const uint16_t *offsets = is_960 ? sfb_offsets_960[sr_idx] : sfb_offsets_1024[sr_idx];
54
41.8M
        for (int i = 0; i <= ics->num_sfbs && i < 68; i++) {
55
40.9M
            ics->sfb_offsets[i] = offsets[i];
56
40.9M
        }
57
825k
    }
58
#else
59
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
60
        ics->num_sfbs = num_sfbs_128[sr_idx];
61
        const uint16_t *offsets = sfb_offsets_128[sr_idx];
62
        for (int i = 0; i <= ics->num_sfbs && i < 68; i++) {
63
            ics->sfb_offsets[i] = offsets[i];
64
        }
65
    } else {
66
        ics->num_sfbs = num_sfbs_1024[sr_idx];
67
        const uint16_t *offsets = sfb_offsets_1024[sr_idx];
68
        for (int i = 0; i <= ics->num_sfbs && i < 68; i++) {
69
            ics->sfb_offsets[i] = offsets[i];
70
        }
71
    }
72
#endif
73
890k
}
74
75
typedef struct { uint16_t sym : 9; uint16_t len : 5; } HuffDesc;
76
_Static_assert(sizeof(HuffDesc) == 2, "Huffman descriptor size");
77
/* Symbol order and code length per book; the lookup tables are rebuilt from
78
 * them as canonical codes, so full codewords never sit in decoder ROM. */
79
static const HuffDesc huff_desc[] = {
80
    /* Book 1 */
81
    {40, 1}, {67, 5}, {13, 5}, {39, 5}, {49, 5}, {41, 5}, {37, 5}, {43, 5},
82
    {31, 5}, {58, 7}, {22, 7}, {38, 7}, {46, 7}, {34, 7}, {42, 7}, {76, 7},
83
    {36, 7}, {4, 7}, {28, 7}, {64, 7}, {48, 7}, {16, 7}, {44, 7}, {70, 7},
84
    {32, 7}, {52, 7}, {50, 7}, {10, 7}, {68, 7}, {12, 7}, {66, 7}, {14, 7},
85
    {30, 7}, {73, 9}, {19, 9}, {61, 9}, {51, 9}, {47, 9}, {35, 9}, {33, 9},
86
    {55, 9}, {65, 9}, {45, 9}, {25, 9}, {15, 9}, {7, 9}, {29, 9}, {59, 9},
87
    {57, 9}, {21, 9}, {1, 9}, {27, 9}, {53, 9}, {69, 9}, {77, 9}, {23, 9},
88
    {79, 9}, {5, 10}, {9, 10}, {75, 10}, {63, 10}, {11, 10}, {3, 10}, {17, 10},
89
    {71, 10}, {60, 11}, {20, 11}, {24, 11}, {56, 11}, {80, 11}, {8, 11}, {72, 11},
90
    {6, 11}, {0, 11}, {74, 11}, {62, 11}, {26, 11}, {18, 11}, {2, 11}, {54, 11},
91
    {78, 11},
92
    /* Book 2 */
93
    {40, 3}, {67, 4}, {13, 5}, {41, 5}, {37, 5}, {39, 5}, {31, 5}, {43, 5},
94
    {49, 5}, {34, 6}, {22, 6}, {46, 6}, {42, 6}, {48, 6}, {38, 6}, {12, 6},
95
    {58, 6}, {64, 6}, {4, 6}, {36, 6}, {70, 6}, {68, 6}, {32, 6}, {16, 6},
96
    {50, 6}, {28, 6}, {14, 6}, {30, 6}, {10, 6}, {76, 6}, {52, 6}, {44, 6},
97
    {66, 6}, {47, 7}, {65, 7}, {19, 7}, {33, 7}, {61, 7}, {75, 7}, {71, 7},
98
    {25, 7}, {29, 7}, {79, 7}, {15, 7}, {1, 7}, {11, 7}, {55, 7}, {73, 7},
99
    {59, 8}, {21, 8}, {7, 8}, {17, 8}, {5, 8}, {3, 8}, {27, 8}, {69, 8},
100
    {63, 8}, {45, 8}, {53, 8}, {23, 8}, {9, 8}, {51, 8}, {57, 8}, {35, 8},
101
    {77, 8}, {60, 8}, {20, 8}, {56, 9}, {0, 9}, {24, 9}, {26, 9}, {80, 9},
102
    {6, 9}, {62, 9}, {18, 9}, {8, 9}, {72, 9}, {54, 9}, {2, 9}, {74, 9},
103
    {78, 9},
104
    /* Book 3 */
105
    {0, 1}, {27, 4}, {1, 4}, {9, 4}, {3, 4}, {36, 5}, {4, 5}, {12, 6},
106
    {10, 6}, {30, 6}, {13, 6}, {28, 6}, {39, 6}, {40, 7}, {31, 7}, {37, 7},
107
    {54, 8}, {2, 8}, {5, 8}, {63, 8}, {48, 8}, {7, 9}, {16, 9}, {45, 9},
108
    {14, 9}, {66, 9}, {6, 9}, {21, 9}, {15, 9}, {18, 9}, {11, 9}, {57, 9},
109
    {49, 9}, {22, 9}, {42, 9}, {43, 9}, {46, 10}, {33, 10}, {34, 10}, {19, 10},
110
    {67, 10}, {41, 10}, {64, 10}, {32, 10}, {8, 10}, {17, 10}, {75, 10}, {51, 10},
111
    {29, 10}, {55, 10}, {25, 10}, {72, 11}, {52, 11}, {38, 11}, {58, 11}, {44, 11},
112
    {76, 11}, {24, 11}, {23, 11}, {35, 12}, {73, 12}, {69, 12}, {78, 12}, {26, 12},
113
    {79, 12}, {70, 12}, {50, 12}, {53, 12}, {20, 13}, {60, 13}, {47, 13}, {61, 14},
114
    {68, 14}, {65, 14}, {80, 15}, {77, 15}, {71, 15}, {59, 15}, {56, 15}, {74, 16},
115
    {62, 16},
116
    /* Book 4 */
117
    {40, 4}, {13, 4}, {37, 4}, {39, 4}, {31, 4}, {27, 4}, {36, 4}, {0, 4},
118
    {4, 4}, {30, 4}, {28, 5}, {12, 5}, {1, 5}, {10, 5}, {3, 5}, {9, 5},
119
    {67, 7}, {43, 7}, {49, 7}, {41, 7}, {66, 7}, {64, 7}, {48, 7}, {58, 7},
120
    {16, 7}, {14, 8}, {42, 8}, {22, 8}, {32, 8}, {46, 8}, {38, 8}, {34, 8},
121
    {63, 8}, {57, 8}, {45, 8}, {55, 8}, {11, 8}, {21, 8}, {5, 8}, {15, 8},
122
    {19, 8}, {29, 8}, {7, 8}, {33, 8}, {54, 8}, {2, 8}, {18, 9}, {6, 9},
123
    {52, 9}, {76, 9}, {70, 9}, {44, 9}, {50, 9}, {68, 9}, {51, 10}, {75, 10},
124
    {69, 10}, {25, 10}, {17, 10}, {73, 10}, {23, 10}, {61, 10}, {35, 10}, {79, 10},
125
    {47, 10}, {59, 10}, {65, 10}, {53, 10}, {71, 11}, {77, 11}, {24, 11}, {72, 11},
126
    {8, 11}, {60, 11}, {20, 11}, {56, 11}, {80, 11}, {26, 11}, {78, 11}, {74, 12},
127
    {62, 12},
128
    /* Book 5 */
129
    {40, 1}, {31, 4}, {49, 4}, {41, 4}, {39, 4}, {48, 5}, {32, 5}, {30, 5},
130
    {50, 5}, {22, 7}, {42, 7}, {58, 7}, {38, 7}, {21, 8}, {59, 8}, {29, 8},
131
    {51, 8}, {23, 8}, {57, 8}, {33, 8}, {47, 8}, {13, 8}, {67, 8}, {37, 8},
132
    {43, 8}, {12, 9}, {52, 9}, {68, 9}, {28, 9}, {14, 9}, {66, 9}, {46, 9},
133
    {34, 9}, {24, 9}, {60, 9}, {20, 9}, {56, 9}, {11, 10}, {65, 10}, {25, 10},
134
    {55, 10}, {69, 10}, {61, 10}, {15, 10}, {19, 10}, {36, 10}, {4, 10}, {77, 10},
135
    {76, 10}, {3, 11}, {44, 11}, {75, 11}, {27, 11}, {53, 11}, {35, 11}, {5, 11},
136
    {45, 11}, {64, 11}, {10, 11}, {16, 11}, {26, 11}, {2, 11}, {78, 11}, {54, 11},
137
    {62, 11}, {70, 11}, {6, 11}, {18, 12}, {74, 12}, {63, 12}, {1, 12}, {7, 12},
138
    {71, 12}, {17, 12}, {79, 12}, {73, 12}, {9, 12}, {72, 13}, {8, 13}, {80, 13},
139
    {0, 13},
140
    /* Book 6 */
141
    {40, 4}, {49, 4}, {39, 4}, {41, 4}, {31, 4}, {50, 4}, {32, 4}, {48, 4},
142
    {30, 4}, {57, 6}, {59, 6}, {23, 6}, {21, 6}, {22, 6}, {33, 6}, {58, 6},
143
    {47, 6}, {51, 6}, {38, 6}, {29, 6}, {42, 6}, {56, 6}, {24, 6}, {20, 6},
144
    {60, 6}, {14, 7}, {68, 7}, {66, 7}, {34, 7}, {12, 7}, {52, 7}, {46, 7},
145
    {28, 7}, {67, 7}, {13, 7}, {37, 7}, {43, 7}, {69, 7}, {11, 8}, {25, 8},
146
    {61, 8}, {65, 8}, {55, 8}, {19, 8}, {15, 8}, {70, 8}, {64, 9}, {10, 9},
147
    {16, 9}, {45, 9}, {27, 9}, {77, 9}, {5, 9}, {3, 9}, {53, 9}, {75, 9},
148
    {35, 9}, {36, 9}, {6, 9}, {2, 9}, {62, 9}, {18, 9}, {4, 9}, {78, 9},
149
    {74, 9}, {26, 9}, {76, 9}, {54, 9}, {44, 9}, {9, 10}, {17, 10}, {63, 10},
150
    {73, 10}, {71, 10}, {79, 10}, {7, 10}, {1, 10}, {80, 11}, {8, 11}, {0, 11},
151
    {72, 11},
152
    /* Book 7 */
153
    {0, 1}, {8, 3}, {1, 3}, {9, 4}, {17, 6}, {10, 6}, {16, 6}, {2, 6},
154
    {25, 7}, {11, 7}, {18, 7}, {24, 7}, {3, 7}, {19, 8}, {26, 8}, {12, 8},
155
    {33, 8}, {13, 8}, {41, 8}, {27, 8}, {20, 8}, {4, 8}, {32, 8}, {34, 9},
156
    {21, 9}, {42, 9}, {5, 9}, {49, 9}, {40, 9}, {14, 9}, {35, 9}, {29, 9},
157
    {28, 9}, {43, 9}, {22, 9}, {50, 9}, {15, 9}, {30, 10}, {6, 10}, {48, 10},
158
    {36, 10}, {57, 10}, {37, 10}, {58, 10}, {44, 10}, {51, 10}, {23, 10}, {59, 10},
159
    {52, 10}, {45, 10}, {38, 10}, {31, 10}, {56, 11}, {7, 11}, {53, 11}, {46, 11},
160
    {60, 11}, {39, 11}, {47, 11}, {61, 11}, {62, 12}, {54, 12}, {55, 12}, {63, 12},
161
    /* Book 8 */
162
    {9, 3}, {17, 4}, {8, 4}, {10, 4}, {1, 4}, {18, 4}, {0, 5}, {16, 5},
163
    {2, 5}, {25, 5}, {11, 5}, {26, 5}, {19, 5}, {27, 6}, {33, 6}, {12, 6},
164
    {34, 6}, {20, 6}, {24, 6}, {3, 6}, {35, 6}, {28, 6}, {42, 6}, {41, 7},
165
    {21, 7}, {13, 7}, {43, 7}, {29, 7}, {36, 7}, {44, 7}, {4, 7}, {37, 7},
166
    {32, 7}, {22, 7}, {50, 7}, {49, 7}, {14, 7}, {30, 8}, {51, 8}, {45, 8},
167
    {40, 8}, {52, 8}, {5, 8}, {38, 8}, {57, 8}, {58, 8}, {23, 8}, {53, 8},
168
    {59, 8}, {15, 8}, {46, 8}, {31, 8}, {54, 9}, {60, 9}, {48, 9}, {39, 9},
169
    {6, 9}, {61, 9}, {62, 9}, {55, 9}, {47, 10}, {56, 10}, {7, 10}, {63, 10},
170
    /* Book 9 */
171
    {0, 1}, {13, 3}, {1, 3}, {14, 4}, {27, 6}, {15, 6}, {26, 6}, {2, 6},
172
    {40, 7}, {28, 7}, {16, 7}, {39, 8}, {3, 8}, {29, 8}, {41, 8}, {17, 8},
173
    {53, 8}, {30, 8}, {18, 8}, {54, 9}, {42, 9}, {4, 9}, {52, 9}, {66, 9},
174
    {31, 9}, {19, 9}, {43, 9}, {67, 9}, {79, 9}, {55, 9}, {5, 10}, {32, 10},
175
    {65, 10}, {20, 10}, {44, 10}, {21, 10}, {105, 10}, {56, 10}, {68, 10}, {80, 10},
176
    {92, 10}, {6, 10}, {106, 10}, {34, 10}, {45, 10}, {33, 10}, {57, 10}, {118, 10},
177
    {22, 10}, {93, 10}, {78, 11}, {69, 11}, {81, 11}, {107, 11}, {7, 11}, {119, 11},
178
    {47, 11}, {58, 11}, {46, 11}, {8, 11}, {131, 11}, {82, 11}, {35, 11}, {70, 11},
179
    {104, 11}, {91, 11}, {94, 11}, {132, 11}, {120, 11}, {108, 11}, {23, 11}, {95, 11},
180
    {83, 11}, {71, 11}, {60, 11}, {59, 11}, {48, 11}, {144, 11}, {73, 11}, {117, 11},
181
    {109, 11}, {133, 12}, {36, 12}, {9, 12}, {145, 12}, {121, 12}, {84, 12}, {157, 12},
182
    {61, 12}, {110, 12}, {24, 12}, {122, 12}, {134, 12}, {72, 12}, {96, 12}, {37, 12},
183
    {25, 12}, {158, 12}, {146, 12}, {49, 12}, {74, 12}, {85, 12}, {111, 12}, {147, 12},
184
    {10, 12}, {97, 12}, {159, 12}, {130, 12}, {135, 12}, {62, 12}, {86, 12}, {38, 12},
185
    {123, 12}, {124, 12}, {63, 12}, {143, 12}, {87, 12}, {50, 12}, {75, 12}, {112, 13},
186
    {99, 13}, {161, 13}, {51, 13}, {148, 13}, {98, 13}, {160, 13}, {149, 13}, {136, 13},
187
    {64, 13}, {100, 13}, {76, 13}, {11, 13}, {162, 13}, {88, 13}, {156, 13}, {137, 13},
188
    {77, 13}, {101, 13}, {125, 13}, {12, 13}, {150, 13}, {113, 13}, {126, 13}, {138, 13},
189
    {102, 13}, {163, 13}, {89, 13}, {115, 13}, {151, 13}, {103, 13}, {90, 13}, {114, 14},
190
    {139, 14}, {116, 14}, {127, 14}, {128, 14}, {129, 14}, {141, 14}, {165, 14}, {140, 14},
191
    {152, 14}, {164, 14}, {153, 14}, {166, 14}, {167, 14}, {142, 15}, {154, 15}, {155, 15},
192
    {168, 15},
193
    /* Book 10 */
194
    {14, 4}, {15, 4}, {27, 4}, {28, 5}, {13, 5}, {1, 5}, {16, 5}, {41, 5},
195
    {40, 5}, {29, 5}, {42, 5}, {26, 6}, {2, 6}, {30, 6}, {54, 6}, {17, 6},
196
    {53, 6}, {0, 6}, {55, 6}, {43, 6}, {39, 6}, {3, 6}, {56, 6}, {31, 6},
197
    {67, 6}, {18, 7}, {66, 7}, {68, 7}, {44, 7}, {69, 7}, {57, 7}, {80, 7},
198
    {32, 7}, {81, 7}, {52, 7}, {79, 7}, {4, 7}, {19, 7}, {45, 7}, {70, 7},
199
    {82, 7}, {58, 7}, {83, 8}, {93, 8}, {46, 8}, {33, 8}, {71, 8}, {106, 8},
200
    {94, 8}, {65, 8}, {92, 8}, {5, 8}, {105, 8}, {20, 8}, {107, 8}, {95, 8},
201
    {59, 8}, {34, 8}, {84, 8}, {96, 8}, {21, 8}, {47, 8}, {108, 8}, {60, 8},
202
    {72, 8}, {109, 8}, {73, 8}, {97, 9}, {85, 9}, {119, 9}, {78, 9}, {86, 9},
203
    {120, 9}, {48, 9}, {118, 9}, {35, 9}, {6, 9}, {110, 9}, {121, 9}, {61, 9},
204
    {132, 9}, {22, 9}, {98, 9}, {111, 9}, {122, 9}, {99, 9}, {133, 9}, {74, 9},
205
    {134, 9}, {36, 9}, {131, 9}, {49, 9}, {123, 9}, {87, 9}, {104, 9}, {62, 9},
206
    {91, 9}, {145, 9}, {100, 10}, {146, 10}, {136, 10}, {23, 10}, {144, 10}, {124, 10},
207
    {7, 10}, {112, 10}, {135, 10}, {50, 10}, {75, 10}, {113, 10}, {148, 10}, {8, 10},
208
    {147, 10}, {37, 10}, {101, 10}, {88, 10}, {137, 10}, {63, 10}, {24, 10}, {158, 10},
209
    {125, 10}, {159, 10}, {149, 10}, {76, 10}, {160, 10}, {150, 10}, {161, 10}, {51, 10},
210
    {89, 10}, {117, 10}, {138, 10}, {130, 10}, {157, 10}, {9, 10}, {64, 10}, {126, 10},
211
    {162, 10}, {38, 10}, {114, 10}, {127, 11}, {25, 11}, {151, 11}, {163, 11}, {102, 11},
212
    {77, 11}, {90, 11}, {139, 11}, {115, 11}, {164, 11}, {10, 11}, {103, 11}, {143, 11},
213
    {140, 11}, {152, 11}, {153, 11}, {11, 11}, {154, 11}, {128, 11}, {141, 11}, {156, 11},
214
    {116, 11}, {165, 12}, {142, 12}, {129, 12}, {155, 12}, {167, 12}, {12, 12}, {166, 12},
215
    {168, 12},
216
    /* Book 11 */
217
    {0, 4}, {18, 4}, {288, 5}, {17, 5}, {1, 5}, {35, 5}, {19, 5}, {36, 5},
218
    {20, 6}, {52, 6}, {53, 6}, {34, 6}, {37, 6}, {2, 6}, {54, 6}, {69, 7},
219
    {21, 7}, {70, 7}, {38, 7}, {71, 7}, {55, 7}, {51, 7}, {3, 7}, {86, 7},
220
    {87, 7}, {39, 7}, {72, 7}, {22, 7}, {88, 7}, {56, 7}, {89, 7}, {73, 8},
221
    {104, 8}, {40, 8}, {103, 8}, {105, 8}, {57, 8}, {23, 8}, {84, 8}, {67, 8},
222
    {277, 8}, {275, 8}, {276, 8}, {106, 8}, {278, 8}, {68, 8}, {74, 8}, {4, 8},
223
    {50, 8}, {90, 8}, {101, 8}, {279, 8}, {274, 8}, {280, 8}, {41, 8}, {121, 8},
224
    {58, 8}, {107, 8}, {91, 8}, {118, 8}, {282, 8}, {122, 8}, {120, 8}, {281, 8},
225
    {135, 8}, {33, 8}, {24, 8}, {75, 8}, {283, 8}, {123, 8}, {284, 8}, {152, 8},
226
    {273, 8}, {108, 8}, {169, 8}, {42, 8}, {92, 8}, {186, 8}, {285, 8}, {139, 8},
227
    {138, 8}, {59, 8}, {85, 8}, {286, 8}, {203, 8}, {124, 8}, {76, 8}, {109, 8},
228
    {125, 8}, {5, 8}, {140, 9}, {287, 9}, {220, 9}, {25, 9}, {137, 9}, {254, 9},
229
    {93, 9}, {237, 9}, {60, 9}, {141, 9}, {126, 9}, {43, 9}, {142, 9}, {155, 9},
230
    {156, 9}, {271, 9}, {77, 9}, {110, 9}, {102, 9}, {157, 9}, {94, 9}, {143, 9},
231
    {127, 9}, {26, 9}, {173, 9}, {6, 9}, {172, 9}, {154, 9}, {158, 9}, {78, 9},
232
    {44, 9}, {159, 9}, {61, 9}, {111, 9}, {174, 9}, {144, 9}, {175, 9}, {160, 9},
233
    {190, 9}, {27, 9}, {119, 9}, {176, 9}, {128, 9}, {62, 9}, {95, 9}, {171, 9},
234
    {79, 9}, {189, 9}, {223, 9}, {112, 9}, {224, 9}, {45, 9}, {272, 9}, {96, 9},
235
    {192, 9}, {191, 10}, {161, 10}, {129, 10}, {145, 10}, {16, 10}, {81, 10}, {7, 10},
236
    {64, 10}, {193, 10}, {222, 10}, {225, 10}, {207, 10}, {47, 10}, {226, 10}, {146, 10},
237
    {113, 10}, {178, 10}, {177, 10}, {240, 10}, {208, 10}, {28, 10}, {80, 10}, {188, 10},
238
    {63, 10}, {30, 10}, {206, 10}, {130, 10}, {65, 10}, {97, 10}, {98, 10}, {242, 10},
239
    {82, 10}, {194, 10}, {241, 10}, {209, 10}, {227, 10}, {210, 10}, {136, 10}, {195, 10},
240
    {46, 10}, {162, 10}, {243, 10}, {115, 10}, {180, 10}, {257, 10}, {147, 10}, {163, 10},
241
    {244, 10}, {179, 10}, {99, 10}, {196, 10}, {239, 10}, {48, 10}, {114, 10}, {29, 10},
242
    {229, 10}, {8, 10}, {228, 10}, {131, 10}, {211, 10}, {132, 10}, {258, 10}, {205, 10},
243
    {116, 10}, {49, 10}, {260, 10}, {259, 10}, {31, 10}, {164, 10}, {83, 10}, {245, 10},
244
    {149, 10}, {230, 10}, {148, 10}, {100, 10}, {66, 10}, {181, 10}, {197, 10}, {212, 10},
245
    {261, 10}, {262, 10}, {150, 10}, {256, 10}, {133, 10}, {153, 10}, {9, 10}, {166, 10},
246
    {165, 10}, {213, 10}, {246, 10}, {183, 10}, {247, 10}, {214, 10}, {117, 10}, {134, 10},
247
    {167, 11}, {263, 11}, {198, 11}, {201, 11}, {32, 11}, {182, 11}, {184, 11}, {232, 11},
248
    {231, 11}, {200, 11}, {199, 11}, {151, 11}, {249, 11}, {233, 11}, {217, 11}, {264, 11},
249
    {248, 11}, {170, 11}, {215, 11}, {168, 11}, {10, 11}, {216, 11}, {187, 11}, {218, 11},
250
    {185, 11}, {234, 11}, {13, 11}, {250, 11}, {265, 11}, {266, 11}, {202, 11}, {251, 11},
251
    {221, 11}, {11, 11}, {235, 11}, {267, 11}, {268, 11}, {219, 11}, {238, 11}, {252, 11},
252
    {236, 11}, {204, 11}, {253, 11}, {14, 12}, {12, 12}, {269, 12}, {255, 12}, {15, 12},
253
    {270, 12},
254
    /* Book 12 */
255
    {60, 1}, {59, 3}, {61, 4}, {58, 4}, {62, 4}, {57, 5}, {63, 5}, {56, 6},
256
    {64, 6}, {55, 6}, {65, 6}, {66, 7}, {54, 7}, {67, 7}, {53, 8}, {68, 8},
257
    {52, 8}, {69, 8}, {51, 8}, {70, 9}, {50, 9}, {49, 9}, {71, 9}, {72, 10},
258
    {48, 10}, {73, 10}, {47, 10}, {74, 10}, {46, 10}, {76, 11}, {75, 11}, {77, 11},
259
    {78, 11}, {45, 11}, {43, 11}, {44, 12}, {79, 12}, {42, 12}, {41, 12}, {80, 12},
260
    {40, 12}, {81, 13}, {39, 13}, {82, 13}, {38, 13}, {83, 13}, {37, 14}, {35, 14},
261
    {85, 14}, {33, 14}, {36, 14}, {34, 14}, {84, 14}, {32, 14}, {87, 15}, {89, 15},
262
    {30, 15}, {31, 15}, {86, 16}, {29, 16}, {26, 16}, {27, 16}, {28, 16}, {24, 16},
263
    {88, 16}, {25, 17}, {22, 17}, {23, 17}, {90, 18}, {21, 18}, {19, 18}, {3, 18},
264
    {1, 18}, {2, 18}, {0, 18}, {98, 19}, {99, 19}, {100, 19}, {101, 19}, {102, 19},
265
    {117, 19}, {97, 19}, {91, 19}, {92, 19}, {93, 19}, {94, 19}, {95, 19}, {96, 19},
266
    {104, 19}, {111, 19}, {112, 19}, {113, 19}, {114, 19}, {115, 19}, {116, 19}, {110, 19},
267
    {105, 19}, {106, 19}, {107, 19}, {108, 19}, {109, 19}, {118, 19}, {6, 19}, {8, 19},
268
    {9, 19}, {10, 19}, {5, 19}, {103, 19}, {120, 19}, {119, 19}, {4, 19}, {7, 19},
269
    {15, 19}, {16, 19}, {18, 19}, {20, 19}, {17, 19}, {11, 19}, {12, 19}, {14, 19},
270
    {13, 19},
271
};
272
static const uint16_t huffbook_sizes[13] = { 0, 81, 81, 81, 81, 81, 81, 64, 64, 169, 169, 289, 121 };
273
/* pair books: index = x * base + y */
274
static const uint8_t book_base[12] = { 0, 0, 0, 0, 0, 9, 9, 8, 8, 13, 13, 17 };
275
276
/* Decoding is a lookup on the next 8 bits, which resolves the short codes
277
 * that carry most of the symbols directly; a longer code's 8-bit prefix
278
 * leads to a second table covering the rest of its subtree, so every code
279
 * takes at most two lookups. A lookup yields the decoded tuple, not a
280
 * symbol index: four 2-bit magnitudes for a quad book, two 6-bit values
281
 * for a pair book (the escape magnitude 16 included), the index itself
282
 * for the scalefactor book. Rows 0..10 are the spectral books 1..11,
283
 * row 11 the scalefactor book. */
284
7.04M
#define HUFF_LUT_BITS 8
285
13.9M
#define HUFF_MAX_LEN  19
286
44.7k
#define HUFF_SUBTREES 200  /* 8-bit prefixes shared by longer codes, all books */
287
#define HUFF_SUB_ENTRIES 3206 /* their second-level entries */
288
typedef uint16_t HuffEntry; /* len (bits beyond the prefix at level two) | tuple << 4; level one: 0 | subtree << 4 */
289
static HuffEntry huff_lut[12][1 << HUFF_LUT_BITS];
290
static HuffEntry huff_sub[HUFF_SUB_ENTRIES];
291
static struct { uint16_t start; uint8_t depth; } huff_subtree[HUFF_SUBTREES];
292
293
static uint32_t huff_tuple(int book, int sym)
294
6.08k
{
295
6.08k
    if (book == 12) return (uint32_t)sym;
296
3.76k
    if (book <= 4) { /* v w x y, base 3 */
297
1.37k
        return (uint32_t)((sym / 27) | ((sym / 9 % 3) << 2) | ((sym / 3 % 3) << 4) | ((sym % 3) << 6));
298
1.37k
    }
299
2.38k
    int base = book_base[book];
300
2.38k
    return (uint32_t)((sym / base) | ((sym % base) << 6));
301
3.76k
}
302
303
void init_huffman_luts(void)
304
1
{
305
1
    int n_sub = 0, n_entries = 0;
306
1
    const HuffDesc *tab = huff_desc;
307
13
    for (int book = 1; book <= 12; book++) {
308
12
        int n = huffbook_sizes[book];
309
12
        uint32_t code = 0;
310
12
        int prev_len = 0;
311
12
        HuffEntry *lut = huff_lut[book - 1];
312
12
        memset(lut, 0, sizeof(huff_lut[0]));
313
314
        /* short codes fill their share of the first level; each longer
315
         * code's prefix gets a subtree as deep as its longest code */
316
1.37k
        for (int i = 0; i < n; i++) {
317
1.36k
            int len = (int)tab[i].len;
318
1.36k
            code = (code + (i != 0)) << (len - prev_len);
319
1.36k
            prev_len = len;
320
1.36k
            if (len == 0) continue;
321
1.36k
            if (len <= HUFF_LUT_BITS) {
322
508
                uint32_t start = code << (HUFF_LUT_BITS - len);
323
3.38k
                for (uint32_t k = 0; k < (1U << (HUFF_LUT_BITS - len)); k++)
324
2.87k
                    lut[start + k] = (HuffEntry)(len | (huff_tuple(book, tab[i].sym) << 4));
325
854
            } else {
326
854
                uint32_t prefix = code >> (len - HUFF_LUT_BITS);
327
854
                if (lut[prefix] == 0) {
328
198
                    lut[prefix] = (HuffEntry)(n_sub << 4);
329
198
                    huff_subtree[n_sub].depth = 0;
330
198
                    n_sub++;
331
198
                }
332
854
                int t = lut[prefix] >> 4;
333
854
                if (len - HUFF_LUT_BITS > huff_subtree[t].depth) huff_subtree[t].depth = (uint8_t)(len - HUFF_LUT_BITS);
334
854
            }
335
1.36k
        }
336
12
        code = 0;
337
12
        prev_len = 0;
338
1.37k
        for (int i = 0; i < n; i++) {
339
1.36k
            int len = (int)tab[i].len;
340
1.36k
            code = (code + (i != 0)) << (len - prev_len);
341
1.36k
            prev_len = len;
342
1.36k
            if (len <= HUFF_LUT_BITS) continue;
343
854
            uint32_t prefix = code >> (len - HUFF_LUT_BITS);
344
854
            int t = lut[prefix] >> 4;
345
854
            if (!(huff_subtree[t].depth & 0x80)) {
346
                /* first code of this subtree: allocate it */
347
197
                huff_subtree[t].start = (uint16_t)n_entries;
348
197
                n_entries += 1 << huff_subtree[t].depth;
349
197
                memset(huff_sub + huff_subtree[t].start, 0, sizeof(HuffEntry) << huff_subtree[t].depth);
350
197
                huff_subtree[t].depth |= 0x80;
351
197
            }
352
854
            int depth = huff_subtree[t].depth & 0x7F;
353
854
            int rest = len - HUFF_LUT_BITS;
354
854
            uint32_t tail = code & ((1U << rest) - 1);
355
854
            uint32_t start = huff_subtree[t].start + (tail << (depth - rest));
356
4.06k
            for (uint32_t k = 0; k < (1U << (depth - rest)); k++)
357
3.20k
                huff_sub[start + k] = (HuffEntry)(rest | (huff_tuple(book, tab[i].sym) << 4));
358
854
        }
359
12
        tab += n;
360
12
    }
361
199
    for (int t = 0; t < n_sub; t++) huff_subtree[t].depth &= 0x7F;
362
1
}
363
364
/* One codeword of book (1..12): the decoded tuple. */
365
static inline uint32_t huff_decode(BitReader *bs, int book)
366
6.94M
{
367
    /* Reuse the same lookahead when a long code needs the second table. */
368
6.94M
    uint32_t lookahead = bits_show_fast(bs, HUFF_MAX_LEN);
369
6.94M
    HuffEntry e = huff_lut[book - 1][lookahead >> (HUFF_MAX_LEN - HUFF_LUT_BITS)];
370
6.94M
    if (e & 15) {
371
6.89M
        bits_skip(bs, e & 15);
372
6.89M
        return e >> 4;
373
6.89M
    }
374
44.7k
    int t = e >> 4;
375
44.7k
    if (t < 0 || t >= HUFF_SUBTREES) return 0;
376
44.7k
    int depth = huff_subtree[t].depth;
377
44.7k
    uint32_t rest = (lookahead >> (HUFF_MAX_LEN - HUFF_LUT_BITS - depth)) & ((1U << depth) - 1);
378
44.7k
    e = huff_sub[huff_subtree[t].start + rest];
379
44.7k
    uint32_t skip = e & 15;
380
44.7k
    if (skip == 0 && depth > 0) return 0;
381
44.7k
    bits_skip(bs, HUFF_LUT_BITS + skip);
382
44.7k
    return e >> 4;
383
44.7k
}
384
385
1.61M
#define DECODE_HUFF_SF(bs) ((int)huff_decode((bs), 12))
386
387
#if MAX_CHANNELS >= 3
388
int decode_coupling_gain(BitReader *bs)
389
93.8k
{
390
93.8k
    return DECODE_HUFF_SF(bs) - 60;
391
93.8k
}
392
#endif
393
394
static inline void decode_quad(BitReader *bs, int book, int *v, int *w, int *x, int *y)
395
1.28M
{
396
    /* Quad books (1..4) carry values in {-1,0,1} and have no escape path. */
397
1.28M
    uint32_t t = huff_decode(bs, book);
398
1.28M
    int v_val = (int)(t & 3), w_val = (int)((t >> 2) & 3), x_val = (int)((t >> 4) & 3), y_val = (int)((t >> 6) & 3);
399
1.28M
    if (book <= 2) {
400
        /* signed 4-tuple: values in {-1, 0, 1} */
401
699k
        *v = v_val - 1; *w = w_val - 1; *x = x_val - 1; *y = y_val - 1;
402
699k
    } else {
403
        /* unsigned 4-tuple: a sign bit follows for each non-zero value */
404
582k
        uint32_t count = (v_val != 0) + (w_val != 0) + (x_val != 0) + (y_val != 0);
405
582k
        uint32_t signs = count ? bits_get_fast(bs, count) : 0;
406
582k
        uint32_t remaining = count;
407
582k
        int sign;
408
        /* Zero stays zero under either sign, avoiding per-value branches. */
409
582k
        remaining -= v_val != 0;
410
582k
        sign = (int)((signs >> remaining) & 1U);
411
582k
        v_val = (v_val ^ -sign) + sign;
412
582k
        remaining -= w_val != 0;
413
582k
        sign = (int)((signs >> remaining) & 1U);
414
582k
        w_val = (w_val ^ -sign) + sign;
415
582k
        remaining -= x_val != 0;
416
582k
        sign = (int)((signs >> remaining) & 1U);
417
582k
        x_val = (x_val ^ -sign) + sign;
418
582k
        remaining -= y_val != 0;
419
582k
        sign = (int)((signs >> remaining) & 1U);
420
582k
        y_val = (y_val ^ -sign) + sign;
421
582k
        *v = v_val; *w = w_val; *x = x_val; *y = y_val;
422
582k
    }
423
1.28M
}
424
425
/* The escape prefix has at most 8 ones, so a magnitude never exceeds 8191; a
426
 * longer run is corrupt and is clamped instead of shifting out of range. */
427
static inline int decode_escape(BitReader *bs)
428
8.27k
{
429
8.27k
    int prefix = 0;
430
15.4k
    while (prefix < 8 && bits_get_1(bs)) prefix++;
431
8.27k
    if (prefix == 8) bits_get_1(bs);
432
8.27k
    return (1 << (prefix + 4)) + (int)bits_get_fast(bs, (uint32_t)prefix + 4);
433
8.27k
}
434
435
static inline void decode_pair(BitReader *bs, int book, int *x, int *y)
436
4.04M
{
437
4.04M
    uint32_t t = huff_decode(bs, book);
438
4.04M
    *x = (int)(t & 63);
439
4.04M
    *y = (int)(t >> 6);
440
441
4.04M
    if (book == 5 || book == 6) {
442
        /* signed 2-tuples in [-4, 4], no sign bits */
443
1.42M
        *x -= 4;
444
1.42M
        *y -= 4;
445
2.62M
    } else if (book == 11) {
446
        /* the sign bits of both values come right after the codeword, the
447
         * escape sequences (for a magnitude of 16) after those */
448
417k
        int abs_x = *x, abs_y = *y;
449
417k
        bool neg_x = abs_x && bits_get_1(bs);
450
417k
        bool neg_y = abs_y && bits_get_1(bs);
451
417k
        if (abs_x == 16) {
452
3.90k
            abs_x = decode_escape(bs);
453
#ifdef FAAD_STATS
454
            g_faadStats.escbookMagnitudeEscapes++;
455
#endif
456
3.90k
        }
457
417k
        if (abs_y == 16) {
458
4.36k
            abs_y = decode_escape(bs);
459
#ifdef FAAD_STATS
460
            g_faadStats.escbookMagnitudeEscapes++;
461
#endif
462
4.36k
        }
463
417k
        *x = neg_x ? -abs_x : abs_x;
464
417k
        *y = neg_y ? -abs_y : abs_y;
465
2.21M
    } else {
466
        /* unsigned 2-tuple: a sign bit follows for each non-zero value */
467
2.21M
        if (*x) if (bits_get_1(bs)) *x = -*x;
468
2.21M
        if (*y) if (bits_get_1(bs)) *y = -*y;
469
2.21M
    }
470
4.04M
}
471
472
faad_status decode_scale_factor_data(BitReader *bs, ICSInfo *ics, uint32_t sample_rate)
473
426k
{
474
426k
    setup_sfb_offsets(ics, sample_rate);
475
476
426k
    int sf = ics->global_gain;
477
426k
    int is_pos = 0;
478
426k
    int pns_energy = ics->global_gain - 90; /* §4.6.13.3: noise_nrg starts from global_gain, not the running sf */
479
426k
    bool is_first_pns = true;
480
481
426k
    int max_sfb = ics->max_sfb < ics->num_sfbs ? ics->max_sfb : ics->num_sfbs;
482
426k
    if (max_sfb > MAX_SFB) max_sfb = MAX_SFB;
483
977k
    for (int g = 0; g < ics->num_window_groups && g < 8; g++) {
484
3.64M
        for (int sfb = 0; sfb < max_sfb; sfb++) {
485
3.09M
            int cb = ics->sfb_cb[g][sfb];
486
#ifdef FAAD_STATS
487
            g_faadStats.totalBands++;
488
#endif
489
3.09M
            if (cb == 0) {
490
1.56M
                continue;
491
1.56M
            } else if (cb == 13) { /* PNS */
492
#ifdef FAAD_STATS
493
                g_faadStats.pnsBands++;
494
#endif
495
66.7k
                if (is_first_pns) {
496
7.94k
                    pns_energy += (int)bits_get(bs, 9) - 256;
497
7.94k
                    is_first_pns = false;
498
58.7k
                } else {
499
58.7k
                    int dpns = DECODE_HUFF_SF(bs);
500
58.7k
                    pns_energy += dpns - 60;
501
58.7k
                }
502
66.7k
                ics->scalefactors[g][sfb] = (int16_t)pns_energy;
503
1.45M
            } else if (cb == 14 || cb == 15) { /* Intensity stereo */
504
#ifdef FAAD_STATS
505
                g_faadStats.isBands++;
506
#endif
507
144k
                int dis = DECODE_HUFF_SF(bs);
508
144k
                is_pos += dis - 60; /* signed: negative positions boost the right channel */
509
144k
                ics->scalefactors[g][sfb] = (int16_t)is_pos;
510
1.31M
            } else {
511
1.31M
                int dsf = DECODE_HUFF_SF(bs);
512
1.31M
                sf += dsf - 60;
513
1.31M
                if (sf < 0) sf = 0;
514
1.31M
                if (sf > 255) sf = 255;
515
1.31M
                ics->scalefactors[g][sfb] = (int16_t)sf;
516
1.31M
            }
517
3.09M
        }
518
550k
    }
519
426k
    return FAAD_OK;
520
426k
}
521
522
static inline float pow_4_3_fast(int x)
523
13.2M
{
524
13.2M
    int abs_x = abs(x);
525
13.2M
    if (abs_x < POW_4_3_LUT_SIZE) {
526
13.2M
        return copysignf(pow_4_3_lut[abs_x], (float)x);
527
13.2M
    }
528
746
    return copysignf(powf((float)abs_x, 4.0f / 3.0f), (float)x);
529
13.2M
}
530
531
/* §4.6.4.3: a pulse adds to the quantized value, away from zero, before
532
 * inverse quantization; the line is re-quantized from its dequantized
533
 * value, which is exact for the integer magnitudes the books carry. */
534
static void apply_pulses(const ICSInfo *ics, float *spec, int max_sfb)
535
19.3k
{
536
65.6k
    for (int i = 0; i < ics->pulse_count; i++) {
537
46.2k
        int k = ics->pulse_pos[i];
538
46.2k
        int sfb = 0;
539
261k
        while (sfb < max_sfb && ics->sfb_offsets[sfb + 1] <= k) sfb++;
540
46.2k
        if (sfb >= max_sfb) continue;
541
10.0k
        int cb = ics->sfb_cb[0][sfb];
542
10.0k
        if (cb == 0 || cb >= 13) continue;
543
6.37k
        float scale = sf_scale_lut[ics->scalefactors[0][sfb]]; /* clamped to 0..255 when read */
544
6.37k
        float q = floorf(powf(fabsf(spec[k]) / scale, 0.75f) + 0.5f);
545
6.37k
        if (spec[k] < 0.0f) q = -q;
546
6.37k
        q += (q > 0.0f) ? ics->pulse_amp[i] : -(float)ics->pulse_amp[i];
547
6.37k
        spec[k] = copysignf(powf(fabsf(q), 4.0f / 3.0f), q) * scale;
548
6.37k
    }
549
19.3k
}
550
551
faad_status decode_spectral_data(BitReader *bs, ICSInfo *ics, float *spec)
552
410k
{
553
410k
    int window_offset = 0;
554
410k
    const uint16_t * restrict sfb_offsets = ics->sfb_offsets;
555
410k
    int max_sfb = ics->max_sfb < ics->num_sfbs ? ics->max_sfb : ics->num_sfbs;
556
410k
    if (max_sfb > MAX_SFB) max_sfb = MAX_SFB;
557
558
410k
#ifndef FAAD_DISABLE_DAB
559
410k
    int short_len = ICS_IS_960(ics) ? 120 : 128;
560
410k
    int long_len = ICS_IS_960(ics) ? 960 : 1024;
561
#else
562
    int short_len = 128;
563
    int long_len = 1024;
564
#endif
565
566
933k
    for (int g = 0; g < ics->num_window_groups && g < 8; g++) {
567
523k
        int win_group_len = ics->window_group_length[g];
568
3.42M
        for (int sfb = 0; sfb < max_sfb; sfb++) {
569
2.90M
            int cb = ics->sfb_cb[g][sfb];
570
2.90M
            if (cb == 0 || cb >= 13) continue;
571
1.17M
            {
572
1.17M
                int sf = ics->scalefactors[g][sfb];
573
1.17M
                float scale = (sf >= 0 && sf < 256) ? sf_scale_lut[sf] : powf(2.0f, 0.25f * (sf - 100));
574
575
1.17M
                int start_k = sfb_offsets[sfb];
576
1.17M
                int end_k = sfb_offsets[sfb + 1];
577
1.17M
                if (start_k >= long_len) continue;
578
1.17M
                if (end_k > long_len) end_k = long_len;
579
580
2.43M
                for (int w = 0; w < win_group_len; w++) {
581
1.25M
                    float * restrict ptr = spec + (window_offset + w) * short_len + start_k;
582
1.25M
                    int k = start_k;
583
1.25M
                    if (cb <= 4) {
584
1.78M
                        while (k < end_k) {
585
1.28M
                            int v, w_val, x, y;
586
1.28M
                            decode_quad(bs, cb, &v, &w_val, &x, &y);
587
1.28M
                            ptr[0] = pow_4_3_fast(v) * scale;
588
1.28M
                            ptr[1] = pow_4_3_fast(w_val) * scale;
589
1.28M
                            ptr[2] = pow_4_3_fast(x) * scale;
590
1.28M
                            ptr[3] = pow_4_3_fast(y) * scale;
591
1.28M
                            ptr += 4;
592
1.28M
                            k += 4;
593
1.28M
                        }
594
759k
                    } else {
595
4.80M
                        while (k < end_k) {
596
4.04M
                            int x, y;
597
4.04M
                            decode_pair(bs, cb, &x, &y);
598
4.04M
                            ptr[0] = pow_4_3_fast(x) * scale;
599
4.04M
                            ptr[1] = pow_4_3_fast(y) * scale;
600
4.04M
                            ptr += 2;
601
4.04M
                            k += 2;
602
4.04M
                        }
603
759k
                    }
604
1.25M
                }
605
1.17M
            }
606
1.17M
        }
607
523k
        window_offset += win_group_len;
608
523k
    }
609
610
410k
    if (ics->pulse_count) apply_pulses(ics, spec, max_sfb);
611
410k
    return FAAD_OK;
612
410k
}