Coverage Report

Created: 2025-08-26 06:13

/proc/self/cwd/libfaad/hcr.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: hcr.c,v 1.26 2009/01/26 23:51:15 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include <stdlib.h>
35
36
#include "specrec.h"
37
#include "huffman.h"
38
39
/* ISO/IEC 14496-3/Amd.1
40
 * 8.5.3.3: Huffman Codeword Reordering for AAC spectral data (HCR)
41
 *
42
 * HCR devides the spectral data in known fixed size segments, and
43
 * sorts it by the importance of the data. The importance is firstly
44
 * the (lower) position in the spectrum, and secondly the largest
45
 * value in the used codebook.
46
 * The most important data is written at the start of each segment
47
 * (at known positions), the remaining data is interleaved inbetween,
48
 * with the writing direction alternating.
49
 * Data length is not increased.
50
*/
51
52
#ifdef ERROR_RESILIENCE
53
54
/* 8.5.3.3.1 Pre-sorting */
55
56
120
#define NUM_CB      6
57
2.17k
#define NUM_CB_ER   22
58
#define MAX_CB      32
59
303k
#define VCB11_FIRST 16
60
63.5k
#define VCB11_LAST  31
61
62
static const uint8_t PreSortCB_STD[NUM_CB] =
63
    { 11, 9, 7, 5, 3, 1};
64
65
static const uint8_t PreSortCB_ER[NUM_CB_ER] =
66
    { 11, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 9, 7, 5, 3, 1};
67
68
/* 8.5.3.3.2 Derivation of segment width */
69
70
static const uint8_t maxCwLen[MAX_CB] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
71
    0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
72
73
23.7k
#define segmentWidth(cb)    min(maxCwLen[cb], ics->length_of_longest_codeword)
74
75
/* bit-twiddling helpers */
76
static const uint8_t  S[] = {1, 2, 4, 8, 16};
77
static const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
78
79
typedef struct
80
{
81
    uint8_t     cb;
82
    uint8_t     decoded;
83
    uint16_t  sp_offset;
84
    bits_t      bits;
85
} codeword_t;
86
87
static uint32_t reverse_word(uint32_t v)
88
119k
{
89
119k
    v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]);
90
119k
    v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]);
91
119k
    v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]);
92
119k
    v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
93
119k
    v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);
94
119k
    return v;
95
119k
}
96
97
/* bits_t version */
98
static void rewrev_bits(bits_t *bits)
99
145k
{
100
145k
    if (bits->len == 0) return;
101
104k
    if (bits->len <= 32) {
102
88.9k
        bits->bufb = 0;
103
88.9k
        bits->bufa = reverse_word(bits->bufa) >> (32 - bits->len);
104
88.9k
    } else {
105
        /* last 32<>32 bit swap via rename */
106
15.2k
        uint32_t lo = reverse_word(bits->bufb);
107
15.2k
        uint32_t hi = reverse_word(bits->bufa);
108
109
15.2k
        if (bits->len == 64) {
110
36
            bits->bufb = hi;
111
36
            bits->bufa = lo;
112
15.2k
        } else {
113
            /* shift off low bits (this is really only one 64 bit shift) */
114
15.2k
            bits->bufb = hi >> (64 - bits->len);
115
15.2k
            bits->bufa = (lo >> (64 - bits->len)) | (hi << (bits->len - 32));
116
15.2k
        }
117
15.2k
    }
118
104k
}
119
120
121
/* merge bits of a to b */
122
/* precondition: a->len + b->len <= 64 */
123
static void concat_bits(bits_t *b, bits_t *a)
124
7.04k
{
125
7.04k
    uint32_t bl, bh, al, ah;
126
127
    /* empty addend */
128
7.04k
    if (a->len == 0) return;
129
130
    /* addend becomes result */
131
7.04k
    if (b->len == 0)
132
0
    {
133
0
        *b = *a;
134
0
        return;
135
0
    }
136
137
7.04k
    al = a->bufa;
138
7.04k
    ah = a->bufb;
139
140
7.04k
    if (b->len > 32)
141
505
    {
142
        /* (b->len - 32) is 1..31 */
143
        /* maskoff superfluous high b bits */
144
505
        bl = b->bufa;
145
505
        bh = b->bufb & ((1u << (b->len-32)) - 1);
146
        /* left shift a b->len bits */
147
505
        ah = al << (b->len - 32);
148
505
        al = 0;
149
6.54k
    } else if (b->len == 32) {
150
63
        bl = b->bufa;
151
63
        bh = 0;
152
63
        ah = al;
153
63
        al = 0;
154
6.47k
    } else {
155
        /* b->len is 1..31, (32 - b->len) is 1..31 */
156
6.47k
        bl = b->bufa & ((1u << (b->len)) - 1);
157
6.47k
        bh = 0;
158
6.47k
        ah = (ah << (b->len)) | (al >> (32 - b->len));
159
6.47k
        al = al << b->len;
160
6.47k
    }
161
162
    /* merge */
163
7.04k
    b->bufa = bl | al;
164
7.04k
    b->bufb = bh | ah;
165
166
7.04k
    b->len += a->len;
167
7.04k
}
168
169
static uint8_t is_good_cb(uint8_t this_CB, uint8_t this_sec_CB)
170
586k
{
171
    /* only want spectral data CB's */
172
586k
    if ((this_sec_CB > ZERO_HCB && this_sec_CB <= ESC_HCB) || (this_sec_CB >= VCB11_FIRST && this_sec_CB <= VCB11_LAST))
173
498k
    {
174
498k
        if (this_CB < ESC_HCB)
175
118k
        {
176
            /* normal codebook pairs */
177
118k
            return ((this_sec_CB == this_CB) || (this_sec_CB == this_CB + 1));
178
118k
        } else
179
379k
        {
180
            /* escape codebook */
181
379k
            return (this_sec_CB == this_CB);
182
379k
        }
183
498k
    }
184
88.3k
    return 0;
185
586k
}
186
187
static void read_segment(bits_t *segment, uint8_t segwidth, bitfile *ld)
188
58.0k
{
189
58.0k
    segment->len = segwidth;
190
191
58.0k
     if (segwidth > 32)
192
8.81k
     {
193
8.81k
        segment->bufb = faad_getbits(ld, segwidth - 32);
194
8.81k
        segment->bufa = faad_getbits(ld, 32);
195
196
49.2k
    } else {
197
49.2k
        segment->bufb = 0;
198
49.2k
        segment->bufa = faad_getbits(ld, segwidth);
199
49.2k
    }
200
58.0k
}
201
202
static void fill_in_codeword(codeword_t *codeword, uint16_t index, uint16_t sp, uint8_t cb)
203
42.2k
{
204
42.2k
    codeword[index].sp_offset = sp;
205
42.2k
    codeword[index].cb = cb;
206
42.2k
    codeword[index].decoded = 0;
207
42.2k
    codeword[index].bits.len = 0;
208
42.2k
}
209
210
uint8_t reordered_spectral_data(NeAACDecStruct *hDecoder, ic_stream *ics,
211
                                bitfile *ld, int16_t *spectral_data)
212
6.19k
{
213
6.19k
    uint16_t PCWs_done;
214
6.19k
    uint16_t numberOfSegments, numberOfSets, numberOfCodewords;
215
216
6.19k
    codeword_t codeword[512];
217
6.19k
    bits_t segment[512];
218
219
6.19k
    uint16_t sp_offset[8];
220
6.19k
    uint16_t g, i, sortloop, set, bitsread;
221
6.19k
    /*uint16_t bitsleft, codewordsleft*/;
222
6.19k
    uint8_t w_idx, sfb, this_CB, last_CB, this_sec_CB;
223
224
6.19k
    const uint16_t nshort = hDecoder->frameLength/8;
225
6.19k
    const uint16_t sp_data_len = ics->length_of_reordered_spectral_data;
226
227
6.19k
    const uint8_t *PreSortCb;
228
229
    /* no data (e.g. silence) */
230
6.19k
    if (sp_data_len == 0)
231
3.86k
        return 0;
232
233
    /* since there is spectral data, at least one codeword has nonzero length */
234
2.32k
    if (ics->length_of_longest_codeword == 0)
235
24
        return 10;
236
237
2.30k
    if (sp_data_len < ics->length_of_longest_codeword)
238
6
        return 10;
239
240
2.29k
    sp_offset[0] = 0;
241
2.63k
    for (g = 1; g < ics->num_window_groups; g++)
242
340
    {
243
340
        sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
244
340
    }
245
246
2.29k
    PCWs_done = 0;
247
2.29k
    numberOfSegments = 0;
248
2.29k
    numberOfCodewords = 0;
249
2.29k
    bitsread = 0;
250
251
    /* VCB11 code books in use */
252
2.29k
    if (hDecoder->aacSectionDataResilienceFlag)
253
2.17k
    {
254
2.17k
        PreSortCb = PreSortCB_ER;
255
2.17k
        last_CB = NUM_CB_ER;
256
2.17k
    } else
257
120
    {
258
120
        PreSortCb = PreSortCB_STD;
259
120
        last_CB = NUM_CB;
260
120
    }
261
262
    /* step 1: decode PCW's (set 0), and stuff data in easier-to-use format */
263
50.1k
    for (sortloop = 0; sortloop < last_CB; sortloop++)
264
47.8k
    {
265
        /* select codebook to process this pass */
266
47.8k
        this_CB = PreSortCb[sortloop];
267
268
        /* loop over sfbs */
269
434k
        for (sfb = 0; sfb < ics->max_sfb; sfb++)
270
386k
        {
271
            /* loop over all in this sfb, 4 lines per loop */
272
942k
            for (w_idx = 0; 4*w_idx < (min(ics->swb_offset[sfb+1], ics->swb_offset_max) - ics->swb_offset[sfb]); w_idx++)
273
556k
            {
274
1.14M
                for(g = 0; g < ics->num_window_groups; g++)
275
586k
                {
276
2.20M
                    for (i = 0; i < ics->num_sec[g]; i++)
277
1.62M
                    {
278
                        /* check whether sfb used here is the one we want to process */
279
1.62M
                        if ((ics->sect_start[g][i] <= sfb) && (ics->sect_end[g][i] > sfb))
280
586k
                        {
281
                            /* check whether codebook used here is the one we want to process */
282
586k
                            this_sec_CB = ics->sect_cb[g][i];
283
284
586k
                            if (is_good_cb(this_CB, this_sec_CB))
285
23.7k
                            {
286
                                /* precalculate some stuff */
287
23.7k
                                uint16_t sect_sfb_size = ics->sect_sfb_offset[g][sfb+1] - ics->sect_sfb_offset[g][sfb];
288
23.7k
                                uint8_t inc = (this_sec_CB < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
289
23.7k
                                uint16_t group_cws_count = (4*ics->window_group_length[g])/inc;
290
23.7k
                                uint8_t segwidth = segmentWidth(this_sec_CB);
291
23.7k
                                uint16_t cws;
292
293
                                /* read codewords until end of sfb or end of window group (shouldn't only 1 trigger?) */
294
123k
                                for (cws = 0; (cws < group_cws_count) && ((cws + w_idx*group_cws_count) < sect_sfb_size); cws++)
295
99.9k
                                {
296
99.9k
                                    uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc * (cws + w_idx*group_cws_count);
297
298
                                    /* read and decode PCW */
299
99.9k
                                    if (!PCWs_done)
300
58.1k
                                    {
301
                                        /* read in normal segments */
302
58.1k
                                        if (bitsread + segwidth <= sp_data_len)
303
57.6k
                                        {
304
57.6k
                                            read_segment(&segment[numberOfSegments], segwidth, ld);
305
57.6k
                                            bitsread += segwidth;
306
307
57.6k
                                            huffman_spectral_data_2(this_sec_CB, &segment[numberOfSegments], &spectral_data[sp]);
308
309
                                            /* keep leftover bits */
310
57.6k
                                            rewrev_bits(&segment[numberOfSegments]);
311
312
57.6k
                                            numberOfSegments++;
313
57.6k
                                        } else {  // sp_data_len - bitsread < segwidth
314
                                            /* remaining stuff after last segment, we unfortunately couldn't read
315
                                               this in earlier because it might not fit in 64 bits. since we already
316
                                               decoded (and removed) the PCW it is now should fit */
317
467
                                            if (bitsread < sp_data_len)
318
374
                                            {
319
374
                                                const uint8_t additional_bits = (uint8_t)(sp_data_len - bitsread);
320
321
374
                                                read_segment(&segment[numberOfSegments], additional_bits, ld);
322
374
                                                segment[numberOfSegments].len += segment[numberOfSegments-1].len;
323
374
                                                if (segment[numberOfSegments].len > 64)
324
40
                                                    return 10;
325
334
                                                rewrev_bits(&segment[numberOfSegments]);
326
327
334
                                                if (segment[numberOfSegments-1].len > 32)
328
73
                                                {
329
73
                                                    segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb +
330
73
                                                        showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len - 32);
331
73
                                                    segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa +
332
73
                                                        showbits_hcr(&segment[numberOfSegments-1], 32);
333
261
                                                } else {
334
261
                                                    segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa +
335
261
                                                        showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len);
336
261
                                                    segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb;
337
261
                                                }
338
334
                                                segment[numberOfSegments-1].len += additional_bits;
339
334
                                            }
340
427
                                            bitsread = sp_data_len;
341
427
                                            PCWs_done = 1;
342
343
427
                                            fill_in_codeword(codeword, 0, sp, this_sec_CB);
344
427
                                        }
345
58.1k
                                    } else {
346
41.8k
                                        fill_in_codeword(codeword, numberOfCodewords - numberOfSegments, sp, this_sec_CB);
347
41.8k
                                    }
348
99.8k
                                    numberOfCodewords++;
349
99.8k
                                }
350
23.7k
                            }
351
586k
                        }
352
1.62M
                    }
353
586k
                 }
354
556k
             }
355
386k
         }
356
47.8k
    }
357
358
2.25k
    if (numberOfSegments == 0)
359
34
        return 10;
360
361
2.22k
    numberOfSets = numberOfCodewords / numberOfSegments;
362
363
    /* step 2: decode nonPCWs */
364
9.12k
    for (set = 1; set <= numberOfSets; set++)
365
6.90k
    {
366
6.90k
        uint16_t trial;
367
368
94.8k
        for (trial = 0; trial < numberOfSegments; trial++)
369
87.9k
        {
370
87.9k
            uint16_t codewordBase;
371
372
4.27M
            for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
373
4.24M
            {
374
4.24M
                const uint16_t segment_idx = (trial + codewordBase) % numberOfSegments;
375
4.24M
                const uint16_t codeword_idx = codewordBase + set*numberOfSegments - numberOfSegments;
376
377
                /* data up */
378
4.24M
                if (codeword_idx >= numberOfCodewords - numberOfSegments) break;
379
380
4.18M
                if (!codeword[codeword_idx].decoded && segment[segment_idx].len > 0)
381
38.2k
                {
382
38.2k
                    uint8_t tmplen = segment[segment_idx].len + codeword[codeword_idx].bits.len;
383
384
38.2k
                    if (tmplen > 64)
385
359
                    {
386
                      // Drop bits that do not fit concatenation result.
387
359
                      flushbits_hcr(&codeword[codeword_idx].bits, tmplen - 64);
388
359
                    }
389
390
38.2k
                    if (codeword[codeword_idx].bits.len != 0)
391
7.04k
                        concat_bits(&segment[segment_idx], &codeword[codeword_idx].bits);
392
393
38.2k
                    tmplen = segment[segment_idx].len;
394
395
38.2k
                    if (huffman_spectral_data_2(codeword[codeword_idx].cb, &segment[segment_idx],
396
38.2k
                                               &spectral_data[codeword[codeword_idx].sp_offset]) >= 0)
397
30.1k
                    {
398
30.1k
                        codeword[codeword_idx].decoded = 1;
399
30.1k
                    } else
400
8.03k
                    {
401
8.03k
                        codeword[codeword_idx].bits = segment[segment_idx];
402
8.03k
                        codeword[codeword_idx].bits.len = tmplen;
403
8.03k
                    }
404
405
38.2k
                }
406
4.18M
            }
407
87.9k
        }
408
94.8k
        for (i = 0; i < numberOfSegments; i++)
409
87.9k
            rewrev_bits(&segment[i]);
410
6.90k
    }
411
412
#if 0 // Seems to give false errors
413
    bitsleft = 0;
414
415
    for (i = 0; i < numberOfSegments && !bitsleft; i++)
416
        bitsleft += segment[i].len;
417
418
    if (bitsleft) return 10;
419
420
    codewordsleft = 0;
421
422
    for (i = 0; (i < numberOfCodewords - numberOfSegments) && (!codewordsleft); i++)
423
        if (!codeword[i].decoded)
424
                codewordsleft++;
425
426
    if (codewordsleft) return 10;
427
#endif
428
429
430
2.22k
    return 0;
431
432
2.25k
}
433
#endif