Coverage Report

Created: 2025-07-11 06:40

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