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