Coverage Report

Created: 2025-07-11 06:40

/proc/self/cwd/libfaad/sbr_huff.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: sbr_huff.c,v 1.21 2007/11/01 12:33:35 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#ifdef SBR_DEC
35
36
#include "sbr_syntax.h"
37
#include "bits.h"
38
#include "sbr_huff.h"
39
#include "sbr_e_nf.h"
40
41
/*
42
 * Huffman tables describe structure and symbols of Huffman trees. Huffman tree
43
 * is a kind of binary tree, i. e. each node is either a leaf or has 2 child
44
 * nodes. Items of Huffman table describe non-leaf nodes, that is why those are
45
 * represented by a pair of integers. Depending on a bit value left (0-th) or 
46
 * right (1-st) child chosen. If value is non-negative, then it is an index
47
 * of next inner node. Otherwise it encodes a leaf (Huffman symbol). To get the
48
 * symbol value 64 is added to leaf value. Since all integers are int8_t, the
49
 * range of symbol values is -64..63.
50
 *
51
 * Note: table with N entries describes tree with N+1 leaves.
52
 * Note: it is possible to extend range to -64..64, since index 0 is forbidden.
53
 * Note: it is possible to extend the range even further by using deltas instead
54
 *       of absolute indices, switching to uint8_t, and employing all the values
55
 *       bigger than maximal delta.
56
 */
57
typedef const int8_t (*sbr_huff_tab)[2];
58
59
static const int8_t t_huffman_env_1_5dB[120][2] = {
60
    {   1,   2 },    { -64, -65 },    {   3,   4 },    { -63, -66 },
61
    {   5,   6 },    { -62, -67 },    {   7,   8 },    { -61, -68 },
62
    {   9,  10 },    { -60, -69 },    {  11,  12 },    { -59, -70 },
63
    {  13,  14 },    { -58, -71 },    {  15,  16 },    { -57, -72 },
64
    {  17,  18 },    { -73, -56 },    {  19,  21 },    { -74,  20 },
65
    { -55, -75 },    {  22,  26 },    {  23,  24 },    { -54, -76 },
66
    { -77,  25 },    { -53, -78 },    {  27,  34 },    {  28,  29 },
67
    { -52, -79 },    {  30,  31 },    { -80, -51 },    {  32,  33 },
68
    { -83, -82 },    { -81, -50 },    {  35,  57 },    {  36,  40 },
69
    {  37,  38 },    { -88, -84 },    { -48,  39 },    { -90, -85 },
70
    {  41,  46 },    {  42,  43 },    { -49, -87 },    {  44,  45 },
71
    { -89, -86 },    {-124,-123 },    {  47,  50 },    {  48,  49 },
72
    {-122,-121 },    {-120,-119 },    {  51,  54 },    {  52,  53 },
73
    {-118,-117 },    {-116,-115 },    {  55,  56 },    {-114,-113 },
74
    {-112,-111 },    {  58,  89 },    {  59,  74 },    {  60,  67 },
75
    {  61,  64 },    {  62,  63 },    {-110,-109 },    {-108,-107 },
76
    {  65,  66 },    {-106,-105 },    {-104,-103 },    {  68,  71 },
77
    {  69,  70 },    {-102,-101 },    {-100, -99 },    {  72,  73 },
78
    { -98, -97 },    { -96, -95 },    {  75,  82 },    {  76,  79 },
79
    {  77,  78 },    { -94, -93 },    { -92, -91 },    {  80,  81 },
80
    { -47, -46 },    { -45, -44 },    {  83,  86 },    {  84,  85 },
81
    { -43, -42 },    { -41, -40 },    {  87,  88 },    { -39, -38 },
82
    { -37, -36 },    {  90, 105 },    {  91,  98 },    {  92,  95 },
83
    {  93,  94 },    { -35, -34 },    { -33, -32 },    {  96,  97 },
84
    { -31, -30 },    { -29, -28 },    {  99, 102 },    { 100, 101 },
85
    { -27, -26 },    { -25, -24 },    { 103, 104 },    { -23, -22 },
86
    { -21, -20 },    { 106, 113 },    { 107, 110 },    { 108, 109 },
87
    { -19, -18 },    { -17, -16 },    { 111, 112 },    { -15, -14 },
88
    { -13, -12 },    { 114, 117 },    { 115, 116 },    { -11, -10 },
89
    {  -9,  -8 },    { 118, 119 },    {  -7,  -6 },    {  -5,  -4 }
90
};
91
92
static const int8_t f_huffman_env_1_5dB[120][2] = {
93
    {   1,   2 },    { -64, -65 },    {   3,   4 },    { -63, -66 },
94
    {   5,   6 },    { -67, -62 },    {   7,   8 },    { -68, -61 },
95
    {   9,  10 },    { -69, -60 },    {  11,  13 },    { -70,  12 },
96
    { -59, -71 },    {  14,  16 },    { -58,  15 },    { -72, -57 },
97
    {  17,  19 },    { -73,  18 },    { -56, -74 },    {  20,  23 },
98
    {  21,  22 },    { -55, -75 },    { -54, -53 },    {  24,  27 },
99
    {  25,  26 },    { -76, -52 },    { -77, -51 },    {  28,  31 },
100
    {  29,  30 },    { -50, -78 },    { -79, -49 },    {  32,  36 },
101
    {  33,  34 },    { -48, -47 },    { -80,  35 },    { -81, -82 },
102
    {  37,  47 },    {  38,  41 },    {  39,  40 },    { -83, -46 },
103
    { -45, -84 },    {  42,  44 },    { -85,  43 },    { -44, -43 },
104
    {  45,  46 },    { -88, -87 },    { -86, -90 },    {  48,  66 },
105
    {  49,  56 },    {  50,  53 },    {  51,  52 },    { -92, -42 },
106
    { -41, -39 },    {  54,  55 },    {-105, -89 },    { -38, -37 },
107
    {  57,  60 },    {  58,  59 },    { -94, -91 },    { -40, -36 },
108
    {  61,  63 },    { -20,  62 },    {-115,-110 },    {  64,  65 },
109
    {-108,-107 },    {-101, -97 },    {  67,  89 },    {  68,  75 },
110
    {  69,  72 },    {  70,  71 },    { -95, -93 },    { -34, -27 },
111
    {  73,  74 },    { -22, -17 },    { -16,-124 },    {  76,  82 },
112
    {  77,  79 },    {-123,  78 },    {-122,-121 },    {  80,  81 },
113
    {-120,-119 },    {-118,-117 },    {  83,  86 },    {  84,  85 },
114
    {-116,-114 },    {-113,-112 },    {  87,  88 },    {-111,-109 },
115
    {-106,-104 },    {  90, 105 },    {  91,  98 },    {  92,  95 },
116
    {  93,  94 },    {-103,-102 },    {-100, -99 },    {  96,  97 },
117
    { -98, -96 },    { -35, -33 },    {  99, 102 },    { 100, 101 },
118
    { -32, -31 },    { -30, -29 },    { 103, 104 },    { -28, -26 },
119
    { -25, -24 },    { 106, 113 },    { 107, 110 },    { 108, 109 },
120
    { -23, -21 },    { -19, -18 },    { 111, 112 },    { -15, -14 },
121
    { -13, -12 },    { 114, 117 },    { 115, 116 },    { -11, -10 },
122
    {  -9,  -8 },    { 118, 119 },    {  -7,  -6 },    {  -5,  -4 }
123
};
124
125
static const int8_t t_huffman_env_bal_1_5dB[48][2] = {
126
    {-64, 1},     {-62, 2},   {-66, 3},   {-60, 4},     {-68, 5},
127
    {-58, 6},     {-70, 7},   {-56, 8},   {-72, 9},     {10, 11},
128
    {-74, -54},   {12, 13},   {-76, -52}, {14, 28},     {15, 21},
129
    {16, 18},     {-50, 17},  {-78, -48}, {19, 20},     {-112, -110},
130
    {-108, -106}, {22, 25},   {23, 24},   {-104, -102}, {-100, -98},
131
    {26, 27},     {-96, -94}, {-92, -90}, {29, 36},     {30, 33},
132
    {31, 32},     {-88, -86}, {-84, -82}, {34, 35},     {-80, -46},
133
    {-44, -42},   {37, 41},   {38, 39},   {-40, -38},   {-36, 40},
134
    {-34, -32},   {42, 45},   {43, 44},   {-30, -28},   {-26, -24},
135
    {46, 47},     {-22, -20}, {-18, -16}
136
};
137
138
static const int8_t f_huffman_env_bal_1_5dB[48][2] = {
139
    {-64, 1},   {-66, 2},     {-62, 3},    {-68, 4},     {-60, 5},
140
    {-58, 6},   {-70, 7},     {-72, 8},    {-56, 9},     {10, 11},
141
    {-74, -54}, {-76, 12},    {-52, 13},   {14, 17},     {-78, 15},
142
    {-50, 16},  {-48, -82},   {18, 32},    {19, 25},     {20, 22},
143
    {-80, 21},  {-112, -110}, {23, 24},    {-108, -106}, {-104, -102},
144
    {26, 29},   {27, 28},     {-100, -98}, {-96, -94},   {30, 31},
145
    {-92, -90}, {-88, -86},   {33, 40},    {34, 37},     {35, 36},
146
    {-84, -46}, {-44, -42},   {38, 39},    {-40, -38},   {-36, -34},
147
    {41, 44},   {42, 43},     {-32, -30},  {-28, -26},   {45, 46},
148
    {-24, -22}, {-20, 47},    {-18, -16}
149
};
150
151
static const int8_t t_huffman_env_3_0dB[62][2] = {
152
    { -64,   1 },    { -65,   2 },    { -63,   3 },    { -66,   4 },
153
    { -62,   5 },    { -67,   6 },    { -61,   7 },    { -68,   8 },
154
    { -60,   9 },    {  10,  11 },    { -69, -59 },    {  12,  14 },
155
    { -70,  13 },    { -71, -58 },    {  15,  18 },    {  16,  17 },
156
    { -72, -57 },    { -73, -74 },    {  19,  22 },    { -56,  20 },
157
    { -55,  21 },    { -54, -77 },    {  23,  31 },    {  24,  25 },
158
    { -75, -76 },    {  26,  27 },    { -78, -53 },    {  28,  29 },
159
    { -52, -95 },    { -94,  30 },    { -93, -92 },    {  32,  47 },
160
    {  33,  40 },    {  34,  37 },    {  35,  36 },    { -91, -90 },
161
    { -89, -88 },    {  38,  39 },    { -87, -86 },    { -85, -84 },
162
    {  41,  44 },    {  42,  43 },    { -83, -82 },    { -81, -80 },
163
    {  45,  46 },    { -79, -51 },    { -50, -49 },    {  48,  55 },
164
    {  49,  52 },    {  50,  51 },    { -48, -47 },    { -46, -45 },
165
    {  53,  54 },    { -44, -43 },    { -42, -41 },    {  56,  59 },
166
    {  57,  58 },    { -40, -39 },    { -38, -37 },    {  60,  61 },
167
    { -36, -35 },    { -34, -33 }
168
};
169
170
static const int8_t f_huffman_env_3_0dB[62][2] = {
171
    { -64,   1 },    { -65,   2 },    { -63,   3 },    { -66,   4 },
172
    { -62,   5 },    { -67,   6 },    {   7,   8 },    { -61, -68 },
173
    {   9,  10 },    { -60, -69 },    {  11,  12 },    { -59, -70 },
174
    {  13,  14 },    { -58, -71 },    {  15,  16 },    { -57, -72 },
175
    {  17,  19 },    { -56,  18 },    { -55, -73 },    {  20,  24 },
176
    {  21,  22 },    { -74, -54 },    { -53,  23 },    { -75, -76 },
177
    {  25,  30 },    {  26,  27 },    { -52, -51 },    {  28,  29 },
178
    { -77, -79 },    { -50, -49 },    {  31,  39 },    {  32,  35 },
179
    {  33,  34 },    { -78, -46 },    { -82, -88 },    {  36,  37 },
180
    { -83, -48 },    { -47,  38 },    { -86, -85 },    {  40,  47 },
181
    {  41,  44 },    {  42,  43 },    { -80, -44 },    { -43, -42 },
182
    {  45,  46 },    { -39, -87 },    { -84, -40 },    {  48,  55 },
183
    {  49,  52 },    {  50,  51 },    { -95, -94 },    { -93, -92 },
184
    {  53,  54 },    { -91, -90 },    { -89, -81 },    {  56,  59 },
185
    {  57,  58 },    { -45, -41 },    { -38, -37 },    {  60,  61 },
186
    { -36, -35 },    { -34, -33 }
187
};
188
189
static const int8_t t_huffman_env_bal_3_0dB[24][2] = {
190
    {-64, 1},   {-62, 2},   {-66, 3},   {-68, 4},   {-60, 5},  {-58, 6},
191
    {-70, 7},   {-72, 8},   {-56, 9},   {10, 16},   {11, 13},  {-74, 12},
192
    {-88, -86}, {14, 15},   {-84, -82}, {-80, -78}, {17, 20},  {18, 19},
193
    {-76, -54}, {-52, -50}, {21, 22},   {-48, -46}, {-44, 23}, {-42, -40}
194
};
195
196
static const int8_t f_huffman_env_bal_3_0dB[24][2] = {
197
    {-64, 1},   {-66, 2},   {-62, 3},   {-68, 4},   {-60, 5},   {-58, 6},
198
    {-70, 7},   {-72, 8},   {-56, 9},   {10, 13},   {-74, 11},  {-54, 12},
199
    {-52, -88}, {14, 17},   {15, 16},   {-86, -84}, {-82, -80}, {18, 21},
200
    {19, 20},   {-78, -76}, {-50, -48}, {22, 23},   {-46, -44}, {-42, -40}
201
};
202
203
static const int8_t t_huffman_noise_3_0dB[62][2] = {
204
    { -64,   1 },    { -63,   2 },    { -65,   3 },    { -66,   4 },
205
    { -62,   5 },    { -67,   6 },    {   7,   8 },    { -61, -68 },
206
    {   9,  30 },    {  10,  15 },    { -60,  11 },    { -69,  12 },
207
    {  13,  14 },    { -59, -53 },    { -95, -94 },    {  16,  23 },
208
    {  17,  20 },    {  18,  19 },    { -93, -92 },    { -91, -90 },
209
    {  21,  22 },    { -89, -88 },    { -87, -86 },    {  24,  27 },
210
    {  25,  26 },    { -85, -84 },    { -83, -82 },    {  28,  29 },
211
    { -81, -80 },    { -79, -78 },    {  31,  46 },    {  32,  39 },
212
    {  33,  36 },    {  34,  35 },    { -77, -76 },    { -75, -74 },
213
    {  37,  38 },    { -73, -72 },    { -71, -70 },    {  40,  43 },
214
    {  41,  42 },    { -58, -57 },    { -56, -55 },    {  44,  45 },
215
    { -54, -52 },    { -51, -50 },    {  47,  54 },    {  48,  51 },
216
    {  49,  50 },    { -49, -48 },    { -47, -46 },    {  52,  53 },
217
    { -45, -44 },    { -43, -42 },    {  55,  58 },    {  56,  57 },
218
    { -41, -40 },    { -39, -38 },    {  59,  60 },    { -37, -36 },
219
    { -35,  61 },    { -34, -33 }
220
};
221
222
static const int8_t t_huffman_noise_bal_3_0dB[24][2] = {
223
    {-64, 1},   {-66, 2},   {-62, 3},   {4, 9},     {-68, 5},   {-60, 6},
224
    {7, 8},     {-88, -86}, {-84, -82}, {10, 17},   {11, 14},   {12, 13},
225
    {-80, -78}, {-76, -74}, {15, 16},   {-72, -70}, {-58, -56}, {18, 21},
226
    {19, 20},   {-54, -52}, {-50, -48}, {22, 23},   {-46, -44}, {-42, -40}
227
};
228
229
230
static INLINE int16_t sbr_huff_dec(bitfile *ld, sbr_huff_tab t_huff)
231
1.92M
{
232
1.92M
    uint8_t bit;
233
1.92M
    int16_t index = 0;
234
235
6.29M
    while (index >= 0)
236
4.37M
    {
237
4.37M
        bit = (uint8_t)faad_get1bit(ld);
238
4.37M
        index = t_huff[index][bit];
239
4.37M
    }
240
241
1.92M
    return index + 64;
242
1.92M
}
243
244
/* table 10 */
245
void sbr_envelope(bitfile *ld, sbr_info *sbr, uint8_t ch)
246
98.1k
{
247
98.1k
    uint8_t env, band;
248
98.1k
    int8_t delta = 0;
249
98.1k
    sbr_huff_tab t_huff, f_huff;
250
251
98.1k
    if ((sbr->L_E[ch] == 1) && (sbr->bs_frame_class[ch] == FIXFIX))
252
23.3k
        sbr->amp_res[ch] = 0;
253
74.8k
    else
254
74.8k
        sbr->amp_res[ch] = sbr->bs_amp_res;
255
256
98.1k
    if ((sbr->bs_coupling) && (ch == 1))
257
12.6k
    {
258
12.6k
        delta = 1;
259
12.6k
        if (sbr->amp_res[ch])
260
5.46k
        {
261
5.46k
            t_huff = t_huffman_env_bal_3_0dB;
262
5.46k
            f_huff = f_huffman_env_bal_3_0dB;
263
7.13k
        } else {
264
7.13k
            t_huff = t_huffman_env_bal_1_5dB;
265
7.13k
            f_huff = f_huffman_env_bal_1_5dB;
266
7.13k
        }
267
85.5k
    } else {
268
85.5k
        delta = 0;
269
85.5k
        if (sbr->amp_res[ch])
270
35.3k
        {
271
35.3k
            t_huff = t_huffman_env_3_0dB;
272
35.3k
            f_huff = f_huffman_env_3_0dB;
273
50.2k
        } else {
274
50.2k
            t_huff = t_huffman_env_1_5dB;
275
50.2k
            f_huff = f_huffman_env_1_5dB;
276
50.2k
        }
277
85.5k
    }
278
279
321k
    for (env = 0; env < sbr->L_E[ch]; env++)
280
222k
    {
281
222k
        if (sbr->bs_df_env[ch][env] == 0)
282
149k
        {
283
149k
            if ((sbr->bs_coupling == 1) && (ch == 1))
284
24.4k
            {
285
24.4k
                if (sbr->amp_res[ch])
286
9.94k
                {
287
9.94k
                    sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 5
288
9.94k
                        DEBUGVAR(1,272,"sbr_envelope(): bs_data_env")) << delta);
289
14.5k
                } else {
290
14.5k
                    sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6
291
14.5k
                        DEBUGVAR(1,273,"sbr_envelope(): bs_data_env")) << delta);
292
14.5k
                }
293
124k
            } else {
294
124k
                if (sbr->amp_res[ch])
295
56.5k
                {
296
56.5k
                    sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6
297
56.5k
                        DEBUGVAR(1,274,"sbr_envelope(): bs_data_env")) << delta);
298
68.1k
                } else {
299
68.1k
                    sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 7
300
68.1k
                        DEBUGVAR(1,275,"sbr_envelope(): bs_data_env")) << delta);
301
68.1k
                }
302
124k
            }
303
304
1.26M
            for (band = 1; band < sbr->n[sbr->f[ch][env]]; band++)
305
1.11M
            {
306
1.11M
                sbr->E[ch][band][env] = (sbr_huff_dec(ld, f_huff));
307
1.11M
            }
308
309
149k
        } else {
310
702k
            for (band = 0; band < sbr->n[sbr->f[ch][env]]; band++)
311
628k
            {
312
628k
                sbr->E[ch][band][env] = (sbr_huff_dec(ld, t_huff));
313
628k
            }
314
73.8k
        }
315
222k
    }
316
317
98.1k
    extract_envelope_data(sbr, ch);
318
98.1k
}
319
320
/* table 11 */
321
void sbr_noise(bitfile *ld, sbr_info *sbr, uint8_t ch)
322
98.1k
{
323
98.1k
    uint8_t noise, band;
324
98.1k
    int8_t delta = 0;
325
98.1k
    sbr_huff_tab t_huff, f_huff;
326
327
98.1k
    if ((sbr->bs_coupling == 1) && (ch == 1))
328
12.6k
    {
329
12.6k
        delta = 1;
330
12.6k
        t_huff = t_huffman_noise_bal_3_0dB;
331
12.6k
        f_huff = f_huffman_env_bal_3_0dB;
332
85.5k
    } else {
333
85.5k
        delta = 0;
334
85.5k
        t_huff = t_huffman_noise_3_0dB;
335
85.5k
        f_huff = f_huffman_env_3_0dB;
336
85.5k
    }
337
338
245k
    for (noise = 0; noise < sbr->L_Q[ch]; noise++)
339
147k
    {
340
147k
        if(sbr->bs_df_noise[ch][noise] == 0)
341
98.7k
        {
342
98.7k
            if ((sbr->bs_coupling == 1) && (ch == 1))
343
13.3k
            {
344
13.3k
                sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
345
13.3k
                    DEBUGVAR(1,276,"sbr_noise(): bs_data_noise")) << delta);
346
85.3k
            } else {
347
85.3k
                sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
348
85.3k
                    DEBUGVAR(1,277,"sbr_noise(): bs_data_noise")) << delta);
349
85.3k
            }
350
183k
            for (band = 1; band < sbr->N_Q; band++)
351
84.5k
            {
352
84.5k
                sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, f_huff));
353
84.5k
            }
354
98.7k
        } else {
355
143k
            for (band = 0; band < sbr->N_Q; band++)
356
95.1k
            {
357
95.1k
                sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, t_huff));
358
95.1k
            }
359
48.8k
        }
360
147k
    }
361
362
98.1k
    extract_noise_floor_data(sbr, ch);
363
98.1k
}
364
365
#endif