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