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