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