Coverage Report

Created: 2026-07-16 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/rvlc.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: rvlc.c,v 1.21 2007/11/01 12:33:34 menno Exp $
29
**/
30
31
/* RVLC scalefactor decoding
32
 *
33
 * RVLC works like this:
34
 *  1. Only symmetric huffman codewords are used
35
 *  2. Total length of the scalefactor data is stored in the bitsream
36
 *  3. Scalefactors are DPCM coded
37
 *  4. Next to the starting value for DPCM the ending value is also stored
38
 *
39
 * With all this it is possible to read the scalefactor data from 2 sides.
40
 * If there is a bit error in the scalefactor data it is possible to start
41
 * decoding from the other end of the data, to find all but 1 scalefactor.
42
 */
43
44
#include "common.h"
45
#include "structs.h"
46
47
#include <stdlib.h>
48
49
#include "syntax.h"
50
#include "bits.h"
51
#include "rvlc.h"
52
53
54
#ifdef ERROR_RESILIENCE
55
56
//#define PRINT_RVLC
57
58
/* static function declarations */
59
static uint8_t rvlc_decode_sf_forward(ic_stream *ics,
60
                                      bitfile *ld_sf,
61
                                      bitfile *ld_esc,
62
                                      uint8_t *is_used);
63
#if 0
64
static uint8_t rvlc_decode_sf_reverse(ic_stream *ics,
65
                                      bitfile *ld_sf,
66
                                      bitfile *ld_esc,
67
                                      uint8_t is_used);
68
#endif
69
static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc /*,
70
                              int8_t direction*/);
71
static int8_t rvlc_huffman_esc(bitfile *ld_esc /*, int8_t direction*/);
72
73
74
uint8_t rvlc_scale_factor_data(ic_stream *ics, bitfile *ld)
75
9.58k
{
76
9.58k
    uint8_t bits = 9;
77
78
9.58k
    ics->sf_concealment = faad_get1bit(ld
79
9.58k
        DEBUGVAR(1,149,"rvlc_scale_factor_data(): sf_concealment"));
80
9.58k
    ics->rev_global_gain = (uint8_t)faad_getbits(ld, 8
81
9.58k
        DEBUGVAR(1,150,"rvlc_scale_factor_data(): rev_global_gain"));
82
83
9.58k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
84
435
        bits = 11;
85
86
    /* the number of bits used for the huffman codewords */
87
9.58k
    ics->length_of_rvlc_sf = (uint16_t)faad_getbits(ld, bits
88
9.58k
        DEBUGVAR(1,151,"rvlc_scale_factor_data(): length_of_rvlc_sf"));
89
90
9.58k
    if (ics->noise_used)
91
385
    {
92
385
        ics->dpcm_noise_nrg = (uint16_t)faad_getbits(ld, 9
93
385
            DEBUGVAR(1,152,"rvlc_scale_factor_data(): dpcm_noise_nrg"));
94
95
        /* the 9 bits of dpcm_noise_nrg are counted in length_of_rvlc_sf, so a
96
           conformant value is at least 9; reject a smaller one before the
97
           unsigned subtraction wraps length_of_rvlc_sf to a huge value */
98
385
        if (ics->length_of_rvlc_sf < 9)
99
12
            return 8;
100
101
373
        ics->length_of_rvlc_sf -= 9;
102
373
    }
103
104
9.56k
    ics->sf_escapes_present = faad_get1bit(ld
105
9.56k
        DEBUGVAR(1,153,"rvlc_scale_factor_data(): sf_escapes_present"));
106
107
9.56k
    if (ics->sf_escapes_present)
108
1.18k
    {
109
1.18k
        ics->length_of_rvlc_escapes = (uint8_t)faad_getbits(ld, 8
110
1.18k
            DEBUGVAR(1,154,"rvlc_scale_factor_data(): length_of_rvlc_escapes"));
111
1.18k
    }
112
113
9.56k
    if (ics->noise_used)
114
373
    {
115
373
        ics->dpcm_noise_last_position = (uint16_t)faad_getbits(ld, 9
116
373
            DEBUGVAR(1,155,"rvlc_scale_factor_data(): dpcm_noise_last_position"));
117
373
    }
118
119
9.56k
    return 0;
120
9.58k
}
121
122
uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld)
123
9.56k
{
124
9.56k
    uint8_t result;
125
9.56k
    uint8_t intensity_used = 0;
126
9.56k
    uint8_t *rvlc_sf_buffer = NULL;
127
9.56k
    uint8_t *rvlc_esc_buffer = NULL;
128
9.56k
    bitfile ld_rvlc_sf = {0}, ld_rvlc_esc = {0};
129
//    bitfile ld_rvlc_sf_rev, ld_rvlc_esc_rev;
130
131
9.56k
    if (ics->length_of_rvlc_sf > 0)
132
2.67k
    {
133
        /* We read length_of_rvlc_sf bits here to put it in a
134
           seperate bitfile.
135
        */
136
2.67k
        rvlc_sf_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_sf
137
2.67k
            DEBUGVAR(1,156,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_sf"));
138
139
2.67k
        faad_initbits(&ld_rvlc_sf, (void*)rvlc_sf_buffer, bit2byte(ics->length_of_rvlc_sf));
140
//        faad_initbits_rev(&ld_rvlc_sf_rev, (void*)rvlc_sf_buffer,
141
//            ics->length_of_rvlc_sf);
142
2.67k
    }
143
9.56k
    if ((ics->length_of_rvlc_sf == 0) || (ld_rvlc_sf.error != 0)) {
144
6.88k
        memset(&ld_rvlc_sf, 0, sizeof(ld_rvlc_sf));
145
6.88k
        ld_rvlc_sf.error = 1;
146
6.88k
    }
147
148
9.56k
    if (ics->sf_escapes_present)
149
1.18k
    {
150
        /* We read length_of_rvlc_escapes bits here to put it in a
151
           seperate bitfile.
152
        */
153
1.18k
        rvlc_esc_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_escapes
154
1.18k
            DEBUGVAR(1,157,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_escapes"));
155
156
1.18k
        faad_initbits(&ld_rvlc_esc, (void*)rvlc_esc_buffer, bit2byte(ics->length_of_rvlc_escapes));
157
//        faad_initbits_rev(&ld_rvlc_esc_rev, (void*)rvlc_esc_buffer,
158
//            ics->length_of_rvlc_escapes);
159
1.18k
    }
160
9.56k
    if (!ics->sf_escapes_present || (ld_rvlc_esc.error != 0)) {
161
8.75k
        memset(&ld_rvlc_esc, 0, sizeof(ld_rvlc_esc));
162
8.75k
        ld_rvlc_esc.error = 1;
163
8.75k
    }
164
165
    /* decode the rvlc scale factors and escapes */
166
9.56k
    result = rvlc_decode_sf_forward(ics, &ld_rvlc_sf,
167
9.56k
        &ld_rvlc_esc, &intensity_used);
168
//    result = rvlc_decode_sf_reverse(ics, &ld_rvlc_sf_rev,
169
//        &ld_rvlc_esc_rev, intensity_used);
170
171
172
9.56k
    if (rvlc_esc_buffer) faad_free(rvlc_esc_buffer);
173
9.56k
    if (rvlc_sf_buffer) faad_free(rvlc_sf_buffer);
174
175
9.56k
    if (ics->length_of_rvlc_sf > 0)
176
2.67k
        faad_endbits(&ld_rvlc_sf);
177
9.56k
    if (ics->sf_escapes_present)
178
1.18k
        faad_endbits(&ld_rvlc_esc);
179
180
9.56k
    return result;
181
9.56k
}
182
183
static uint8_t rvlc_decode_sf_forward(ic_stream *ics, bitfile *ld_sf, bitfile *ld_esc,
184
                                      uint8_t *intensity_used)
185
9.56k
{
186
9.56k
    int8_t g, sfb;
187
9.56k
    int8_t t = 0;
188
9.56k
    int8_t error = ld_sf->error | ld_esc->error;
189
9.56k
    int8_t noise_pcm_flag = 1;
190
191
9.56k
    int16_t scale_factor = ics->global_gain;
192
9.56k
    int16_t is_position = 0;
193
9.56k
    int16_t noise_energy = ics->global_gain - 90 - 256;
194
9.56k
    int16_t scale_factor_max = 255;
195
9.56k
#ifdef FIXED_POINT
196
    /* TODO: consider rolling out to regular build. */
197
9.56k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
198
    /* The value is inexact, adjusted to current fuzzer findings. */
199
9.56k
    scale_factor_max = 165;
200
9.56k
#endif  // FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
201
9.56k
#endif  // FIXED_POINT
202
203
#ifdef PRINT_RVLC
204
    printf("\nglobal_gain: %d\n", ics->global_gain);
205
#endif
206
207
21.6k
    for (g = 0; g < ics->num_window_groups; g++)
208
12.0k
    {
209
41.1k
        for (sfb = 0; sfb < ics->max_sfb; sfb++)
210
29.1k
        {
211
29.1k
            if (error)
212
18.7k
            {
213
18.7k
                ics->scale_factors[g][sfb] = 0;
214
18.7k
            } else {
215
10.4k
                switch (ics->sfb_cb[g][sfb])
216
10.4k
                {
217
923
                case ZERO_HCB: /* zero book */
218
923
                    ics->scale_factors[g][sfb] = 0;
219
923
                    break;
220
434
                case INTENSITY_HCB: /* intensity books */
221
673
                case INTENSITY_HCB2:
222
223
673
                    *intensity_used = 1;
224
225
                    /* decode intensity position */
226
673
                    t = rvlc_huffman_sf(ld_sf, ld_esc /*, +1*/);
227
228
673
                    is_position += t;
229
673
                    ics->scale_factors[g][sfb] = is_position;
230
231
673
                    break;
232
5.93k
                case NOISE_HCB: /* noise books */
233
234
                    /* decode noise energy */
235
5.93k
                    if (noise_pcm_flag)
236
340
                    {
237
340
                        int16_t n = ics->dpcm_noise_nrg;
238
340
                        noise_pcm_flag = 0;
239
340
                        noise_energy += n;
240
5.59k
                    } else {
241
5.59k
                        t = rvlc_huffman_sf(ld_sf, ld_esc /*, +1*/);
242
5.59k
                        noise_energy += t;
243
5.59k
                    }
244
245
5.93k
                    ics->scale_factors[g][sfb] = noise_energy;
246
247
5.93k
                    break;
248
2.91k
                default: /* spectral books */
249
250
                    /* decode scale factor */
251
2.91k
                    t = rvlc_huffman_sf(ld_sf, ld_esc /*, +1*/);
252
253
2.91k
                    scale_factor += t;
254
2.91k
                    if (scale_factor < 0 || scale_factor > 255)
255
29
                        return 4;
256
257
2.88k
                    ics->scale_factors[g][sfb] = min(scale_factor, scale_factor_max);
258
259
2.88k
                    break;
260
10.4k
                }
261
#ifdef PRINT_RVLC
262
                printf("%3d:%4d%4d\n", sfb, ics->sfb_cb[g][sfb],
263
                    ics->scale_factors[g][sfb]);
264
#endif
265
10.4k
                if (t == 99)
266
305
                {
267
305
                    error = 1;
268
305
                }
269
10.4k
            }
270
29.1k
        }
271
12.0k
    }
272
#ifdef PRINT_RVLC
273
    printf("\n\n");
274
#endif
275
276
9.53k
    return 0;
277
9.56k
}
278
279
#if 0 // not used right now, doesn't work correctly yet
280
static uint8_t rvlc_decode_sf_reverse(ic_stream *ics, bitfile *ld_sf, bitfile *ld_esc,
281
                                      uint8_t intensity_used)
282
{
283
    int8_t g, sfb;
284
    int8_t t = 0;
285
    int8_t error = 0;
286
    int8_t noise_pcm_flag = 1, is_pcm_flag = 1, sf_pcm_flag = 1;
287
288
    int16_t scale_factor = ics->rev_global_gain;
289
    int16_t is_position = 0;
290
    int16_t noise_energy = ics->rev_global_gain;
291
292
#ifdef PRINT_RVLC
293
    printf("\nrev_global_gain: %d\n", ics->rev_global_gain);
294
#endif
295
296
    if (intensity_used)
297
    {
298
        is_position = rvlc_huffman_sf(ld_sf, ld_esc, -1);
299
#ifdef PRINT_RVLC
300
        printf("is_position: %d\n", is_position);
301
#endif
302
    }
303
304
    for (g = ics->num_window_groups-1; g >= 0; g--)
305
    {
306
        for (sfb = ics->max_sfb-1; sfb >= 0; sfb--)
307
        {
308
            if (error)
309
            {
310
                ics->scale_factors[g][sfb] = 0;
311
            } else {
312
                switch (ics->sfb_cb[g][sfb])
313
                {
314
                case ZERO_HCB: /* zero book */
315
                    ics->scale_factors[g][sfb] = 0;
316
                    break;
317
                case INTENSITY_HCB: /* intensity books */
318
                case INTENSITY_HCB2:
319
320
                    if (is_pcm_flag)
321
                    {
322
                        is_pcm_flag = 0;
323
                        ics->scale_factors[g][sfb] = is_position;
324
                    } else {
325
                        t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
326
                        is_position -= t;
327
328
                        ics->scale_factors[g][sfb] = (uint8_t)is_position;
329
                    }
330
                    break;
331
                case NOISE_HCB: /* noise books */
332
333
                    /* decode noise energy */
334
                    if (noise_pcm_flag)
335
                    {
336
                        noise_pcm_flag = 0;
337
                        noise_energy = ics->dpcm_noise_last_position;
338
                    } else {
339
                        t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
340
                        noise_energy -= t;
341
                    }
342
343
                    ics->scale_factors[g][sfb] = (uint8_t)noise_energy;
344
                    break;
345
                default: /* spectral books */
346
347
                    if (sf_pcm_flag || (sfb == 0))
348
                    {
349
                        sf_pcm_flag = 0;
350
                        if (sfb == 0)
351
                            scale_factor = ics->global_gain;
352
                    } else {
353
                        /* decode scale factor */
354
                        t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
355
                        scale_factor -= t;
356
                    }
357
358
                    if (scale_factor < 0)
359
                        return 4;
360
361
                    ics->scale_factors[g][sfb] = (uint8_t)scale_factor;
362
                    break;
363
                }
364
#ifdef PRINT_RVLC
365
                printf("%3d:%4d%4d\n", sfb, ics->sfb_cb[g][sfb],
366
                    ics->scale_factors[g][sfb]);
367
#endif
368
                if (t == 99)
369
                {
370
                    error = 1;
371
                }
372
            }
373
        }
374
    }
375
376
#ifdef PRINT_RVLC
377
    printf("\n\n");
378
#endif
379
380
    return 0;
381
}
382
#endif
383
384
/* index == 99 means not allowed codeword */
385
static rvlc_huff_table book_rvlc[] = {
386
    /*index  length  codeword */
387
    {  0, 1,   0 }, /*         0 */
388
    { -1, 3,   5 }, /*       101 */
389
    {  1, 3,   7 }, /*       111 */
390
    { -2, 4,   9 }, /*      1001 */
391
    { -3, 5,  17 }, /*     10001 */
392
    {  2, 5,  27 }, /*     11011 */
393
    { -4, 6,  33 }, /*    100001 */
394
    { 99, 6,  50 }, /*    110010 */
395
    {  3, 6,  51 }, /*    110011 */
396
    { 99, 6,  52 }, /*    110100 */
397
    { -7, 7,  65 }, /*   1000001 */
398
    { 99, 7,  96 }, /*   1100000 */
399
    { 99, 7,  98 }, /*   1100010 */
400
    {  7, 7,  99 }, /*   1100011 */
401
    {  4, 7, 107 }, /*   1101011 */
402
    { -5, 8, 129 }, /*  10000001 */
403
    { 99, 8, 194 }, /*  11000010 */
404
    {  5, 8, 195 }, /*  11000011 */
405
    { 99, 8, 212 }, /*  11010100 */
406
    { 99, 9, 256 }, /* 100000000 */
407
    { -6, 9, 257 }, /* 100000001 */
408
    { 99, 9, 426 }, /* 110101010 */
409
    {  6, 9, 427 }, /* 110101011 */
410
    { 99, 10,  0 } /* Shouldn't come this far */
411
};
412
413
static rvlc_huff_table book_escape[] = {
414
    /*index  length  codeword */
415
    { 1, 2, 0 },
416
    { 0, 2, 2 },
417
    { 3, 3, 2 },
418
    { 2, 3, 6 },
419
    { 4, 4, 14 },
420
    { 7, 5, 13 },
421
    { 6, 5, 15 },
422
    { 5, 5, 31 },
423
    { 11, 6, 24 },
424
    { 10, 6, 25 },
425
    { 9, 6, 29 },
426
    { 8, 6, 61 },
427
    { 13, 7, 56  },
428
    { 12, 7, 120 },
429
    { 15, 8, 114 },
430
    { 14, 8, 242 },
431
    { 17, 9, 230 },
432
    { 16, 9, 486 },
433
    { 19, 10, 463  },
434
    { 18, 10, 974  },
435
    { 22, 11, 925  },
436
    { 20, 11, 1950 },
437
    { 21, 11, 1951 },
438
    { 23, 12, 1848 },
439
    { 25, 13, 3698 },
440
    { 24, 14, 7399 },
441
    { 26, 15, 14797 },
442
    { 49, 19, 236736 },
443
    { 50, 19, 236737 },
444
    { 51, 19, 236738 },
445
    { 52, 19, 236739 },
446
    { 53, 19, 236740 },
447
    { 27, 20, 473482 },
448
    { 28, 20, 473483 },
449
    { 29, 20, 473484 },
450
    { 30, 20, 473485 },
451
    { 31, 20, 473486 },
452
    { 32, 20, 473487 },
453
    { 33, 20, 473488 },
454
    { 34, 20, 473489 },
455
    { 35, 20, 473490 },
456
    { 36, 20, 473491 },
457
    { 37, 20, 473492 },
458
    { 38, 20, 473493 },
459
    { 39, 20, 473494 },
460
    { 40, 20, 473495 },
461
    { 41, 20, 473496 },
462
    { 42, 20, 473497 },
463
    { 43, 20, 473498 },
464
    { 44, 20, 473499 },
465
    { 45, 20, 473500 },
466
    { 46, 20, 473501 },
467
    { 47, 20, 473502 },
468
    { 48, 20, 473503 },
469
    { 99, 21,  0 } /* Shouldn't come this far */
470
};
471
472
static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc /*,
473
                              int8_t direction*/)
474
9.18k
{
475
9.18k
    uint16_t i, j;
476
9.18k
    int16_t index;
477
9.18k
    uint32_t cw;
478
9.18k
    rvlc_huff_table *h = book_rvlc;
479
9.18k
    int8_t direction = +1;
480
481
9.18k
    i = h->len;
482
9.18k
    if (direction > 0)
483
9.18k
        cw = faad_getbits(ld_sf, i DEBUGVAR(1,0,""));
484
0
    else
485
0
        cw = 0 /* faad_getbits_rev(ld_sf, i DEBUGVAR(1,0,"")) */;
486
487
29.7k
    while ((cw != h->cw)
488
20.5k
        && (i < 10))
489
20.5k
    {
490
20.5k
        h++;
491
20.5k
        j = h->len-i;
492
20.5k
        i += j;
493
20.5k
        cw <<= j;
494
20.5k
        if (direction > 0)
495
20.5k
            cw |= faad_getbits(ld_sf, j DEBUGVAR(1,0,""));
496
0
        else
497
0
            cw |= 0 /* faad_getbits_rev(ld_sf, j DEBUGVAR(1,0,"")) */;
498
20.5k
    }
499
500
9.18k
    index = h->index;
501
502
9.18k
    if (index == +ESC_VAL)
503
458
    {
504
458
        int8_t esc = rvlc_huffman_esc(ld_esc /*, direction*/);
505
458
        if (esc == 99)
506
0
            return 99;
507
458
        index += esc;
508
#ifdef PRINT_RVLC
509
        printf("esc: %d - ", esc);
510
#endif
511
458
    }
512
9.18k
    if (index == -ESC_VAL)
513
338
    {
514
338
        int8_t esc = rvlc_huffman_esc(ld_esc /*, direction*/);
515
338
        if (esc == 99)
516
0
            return 99;
517
338
        index -= esc;
518
#ifdef PRINT_RVLC
519
        printf("esc: %d - ", esc);
520
#endif
521
338
    }
522
523
9.18k
    return (int8_t)index;
524
9.18k
}
525
526
static int8_t rvlc_huffman_esc(bitfile *ld /*,
527
                               int8_t direction*/)
528
796
{
529
796
    uint16_t i, j;
530
796
    uint32_t cw;
531
796
    rvlc_huff_table *h = book_escape;
532
796
    int8_t direction = +1;
533
534
796
    i = h->len;
535
796
    if (direction > 0)
536
796
        cw = faad_getbits(ld, i DEBUGVAR(1,0,""));
537
0
    else
538
0
        cw = 0 /* faad_getbits_rev(ld, i DEBUGVAR(1,0,"")) */;
539
540
5.51k
    while ((cw != h->cw)
541
4.71k
        && (i < 21))
542
4.71k
    {
543
4.71k
        h++;
544
4.71k
        j = h->len-i;
545
4.71k
        i += j;
546
4.71k
        cw <<= j;
547
4.71k
        if (direction > 0)
548
4.71k
            cw |= faad_getbits(ld, j DEBUGVAR(1,0,""));
549
0
        else
550
0
            cw |= 0 /* faad_getbits_rev(ld, j DEBUGVAR(1,0,"")) */;
551
4.71k
    }
552
553
796
    return (int8_t)h->index;
554
796
}
555
556
#endif
557