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