2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
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"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: rvlc.c,v 1.21 2007/11/01 12:33:34 menno Exp $
31 /* RVLC scalefactor decoding
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
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.
54 #ifdef ERROR_RESILIENCE
58 /* static function declarations */
59 static uint8_t rvlc_decode_sf_forward(ic_stream *ics,
64 static uint8_t rvlc_decode_sf_reverse(ic_stream *ics,
69 static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc,
71 static int8_t rvlc_huffman_esc(bitfile *ld_esc, int8_t direction);
74 uint8_t rvlc_scale_factor_data(ic_stream *ics, bitfile *ld)
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"));
83 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
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"));
92 ics->dpcm_noise_nrg = (uint16_t)faad_getbits(ld, 9
93 DEBUGVAR(1,152,"rvlc_scale_factor_data(): dpcm_noise_nrg"));
95 ics->length_of_rvlc_sf -= 9;
98 ics->sf_escapes_present = faad_get1bit(ld
99 DEBUGVAR(1,153,"rvlc_scale_factor_data(): sf_escapes_present"));
101 if (ics->sf_escapes_present)
103 ics->length_of_rvlc_escapes = (uint8_t)faad_getbits(ld, 8
104 DEBUGVAR(1,154,"rvlc_scale_factor_data(): length_of_rvlc_escapes"));
109 ics->dpcm_noise_last_position = (uint16_t)faad_getbits(ld, 9
110 DEBUGVAR(1,155,"rvlc_scale_factor_data(): dpcm_noise_last_position"));
116 uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld)
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;
125 if (ics->length_of_rvlc_sf > 0)
127 /* We read length_of_rvlc_sf bits here to put it in a
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"));
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);
138 if (ics->sf_escapes_present)
140 /* We read length_of_rvlc_escapes bits here to put it in a
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"));
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);
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);
158 if (rvlc_esc_buffer) faad_free(rvlc_esc_buffer);
159 if (rvlc_sf_buffer) faad_free(rvlc_sf_buffer);
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);
169 static uint8_t rvlc_decode_sf_forward(ic_stream *ics, bitfile *ld_sf, bitfile *ld_esc,
170 uint8_t *intensity_used)
175 int8_t noise_pcm_flag = 1;
177 int16_t scale_factor = ics->global_gain;
178 int16_t is_position = 0;
179 int16_t noise_energy = ics->global_gain - 90 - 256;
182 printf("\nglobal_gain: %d\n", ics->global_gain);
185 for (g = 0; g < ics->num_window_groups; g++)
187 for (sfb = 0; sfb < ics->max_sfb; sfb++)
191 ics->scale_factors[g][sfb] = 0;
193 switch (ics->sfb_cb[g][sfb])
195 case ZERO_HCB: /* zero book */
196 ics->scale_factors[g][sfb] = 0;
198 case INTENSITY_HCB: /* intensity books */
203 /* decode intensity position */
204 t = rvlc_huffman_sf(ld_sf, ld_esc, +1);
207 ics->scale_factors[g][sfb] = is_position;
210 case NOISE_HCB: /* noise books */
212 /* decode noise energy */
215 int16_t n = ics->dpcm_noise_nrg;
219 t = rvlc_huffman_sf(ld_sf, ld_esc, +1);
223 ics->scale_factors[g][sfb] = noise_energy;
226 default: /* spectral books */
228 /* decode scale factor */
229 t = rvlc_huffman_sf(ld_sf, ld_esc, +1);
232 if (scale_factor < 0)
235 ics->scale_factors[g][sfb] = scale_factor;
240 printf("%3d:%4d%4d\n", sfb, ics->sfb_cb[g][sfb],
241 ics->scale_factors[g][sfb]);
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)
264 int8_t noise_pcm_flag = 1, is_pcm_flag = 1, sf_pcm_flag = 1;
266 int16_t scale_factor = ics->rev_global_gain;
267 int16_t is_position = 0;
268 int16_t noise_energy = ics->rev_global_gain;
271 printf("\nrev_global_gain: %d\n", ics->rev_global_gain);
276 is_position = rvlc_huffman_sf(ld_sf, ld_esc, -1);
278 printf("is_position: %d\n", is_position);
282 for (g = ics->num_window_groups-1; g >= 0; g--)
284 for (sfb = ics->max_sfb-1; sfb >= 0; sfb--)
288 ics->scale_factors[g][sfb] = 0;
290 switch (ics->sfb_cb[g][sfb])
292 case ZERO_HCB: /* zero book */
293 ics->scale_factors[g][sfb] = 0;
295 case INTENSITY_HCB: /* intensity books */
301 ics->scale_factors[g][sfb] = is_position;
303 t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
306 ics->scale_factors[g][sfb] = (uint8_t)is_position;
309 case NOISE_HCB: /* noise books */
311 /* decode noise energy */
315 noise_energy = ics->dpcm_noise_last_position;
317 t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
321 ics->scale_factors[g][sfb] = (uint8_t)noise_energy;
323 default: /* spectral books */
325 if (sf_pcm_flag || (sfb == 0))
329 scale_factor = ics->global_gain;
331 /* decode scale factor */
332 t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
336 if (scale_factor < 0)
339 ics->scale_factors[g][sfb] = (uint8_t)scale_factor;
343 printf("%3d:%4d%4d\n", sfb, ics->sfb_cb[g][sfb],
344 ics->scale_factors[g][sfb]);
362 /* index == 99 means not allowed codeword */
363 static rvlc_huff_table book_rvlc[] = {
364 /*index length codeword */
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 */
391 static rvlc_huff_table book_escape[] = {
392 /*index length codeword */
447 { 99, 21, 0 } /* Shouldn't come this far */
450 static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc,
456 rvlc_huff_table *h = book_rvlc;
460 cw = faad_getbits(ld_sf, i DEBUGVAR(1,0,""));
462 cw = faad_getbits_rev(ld_sf, i DEBUGVAR(1,0,""));
472 cw |= faad_getbits(ld_sf, j DEBUGVAR(1,0,""));
474 cw |= faad_getbits_rev(ld_sf, j DEBUGVAR(1,0,""));
479 if (index == +ESC_VAL)
481 int8_t esc = rvlc_huffman_esc(ld_esc, direction);
486 printf("esc: %d - ", esc);
489 if (index == -ESC_VAL)
491 int8_t esc = rvlc_huffman_esc(ld_esc, direction);
496 printf("esc: %d - ", esc);
503 static int8_t rvlc_huffman_esc(bitfile *ld,
508 rvlc_huff_table *h = book_escape;
512 cw = faad_getbits(ld, i DEBUGVAR(1,0,""));
514 cw = faad_getbits_rev(ld, i DEBUGVAR(1,0,""));
524 cw |= faad_getbits(ld, j DEBUGVAR(1,0,""));
526 cw |= faad_getbits_rev(ld, j DEBUGVAR(1,0,""));