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