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