1 /* ----------------------------------------------------------------------------- 2 Software License for The Fraunhofer FDK AAC Codec Library for Android 3 4 Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Frderung der angewandten 5 Forschung e.V. All rights reserved. 6 7 1. INTRODUCTION 8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software 9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding 10 scheme for digital audio. This FDK AAC Codec software is intended to be used on 11 a wide variety of Android devices. 12 13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient 14 general perceptual audio codecs. AAC-ELD is considered the best-performing 15 full-bandwidth communications codec by independent studies and is widely 16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG 17 specifications. 18 19 Patent licenses for necessary patent claims for the FDK AAC Codec (including 20 those of Fraunhofer) may be obtained through Via Licensing 21 (www.vialicensing.com) or through the respective patent owners individually for 22 the purpose of encoding or decoding bit streams in products that are compliant 23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of 24 Android devices already license these patent claims through Via Licensing or 25 directly from the patent owners, and therefore FDK AAC Codec software may 26 already be covered under those patent licenses when it is used for those 27 licensed purposes only. 28 29 Commercially-licensed AAC software libraries, including floating-point versions 30 with enhanced sound quality, are also available from Fraunhofer. Users are 31 encouraged to check the Fraunhofer website for additional applications 32 information and documentation. 33 34 2. COPYRIGHT LICENSE 35 36 Redistribution and use in source and binary forms, with or without modification, 37 are permitted without payment of copyright license fees provided that you 38 satisfy the following conditions: 39 40 You must retain the complete text of this software license in redistributions of 41 the FDK AAC Codec or your modifications thereto in source code form. 42 43 You must retain the complete text of this software license in the documentation 44 and/or other materials provided with redistributions of the FDK AAC Codec or 45 your modifications thereto in binary form. You must make available free of 46 charge copies of the complete source code of the FDK AAC Codec and your 47 modifications thereto to recipients of copies in binary form. 48 49 The name of Fraunhofer may not be used to endorse or promote products derived 50 from this library without prior written permission. 51 52 You may not charge copyright license fees for anyone to use, copy or distribute 53 the FDK AAC Codec software or your modifications thereto. 54 55 Your modified versions of the FDK AAC Codec must carry prominent notices stating 56 that you changed the software and the date of any change. For modified versions 57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" 58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK 59 AAC Codec Library for Android." 60 61 3. NO PATENT LICENSE 62 63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without 64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. 65 Fraunhofer provides no warranty of patent non-infringement with respect to this 66 software. 67 68 You may use this FDK AAC Codec software or modifications thereto only for 69 purposes that are authorized by appropriate patent licenses. 70 71 4. DISCLAIMER 72 73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright 74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, 75 including but not limited to the implied warranties of merchantability and 76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, 78 or consequential damages, including but not limited to procurement of substitute 79 goods or services; loss of use, data, or profits, or business interruption, 80 however caused and on any theory of liability, whether in contract, strict 81 liability, or tort (including negligence), arising in any way out of the use of 82 this software, even if advised of the possibility of such damage. 83 84 5. CONTACT INFORMATION 85 86 Fraunhofer Institute for Integrated Circuits IIS 87 Attention: Audio and Multimedia Departments - FDK AAC LL 88 Am Wolfsmantel 33 89 91058 Erlangen, Germany 90 91 www.iis.fraunhofer.de/amm 92 amm-info (at) iis.fraunhofer.de 93 ----------------------------------------------------------------------------- */ 94 95 /******************* Library for basic calculation routines ******************** 96 97 Author(s): Omer Osman 98 99 Description: SAC/SAOC Dec Noiseless Coding 100 101 *******************************************************************************/ 102 103 #include "nlc_dec.h" 104 #include "FDK_tools_rom.h" 105 106 /* MAX_PARAMETER_BANDS defines array length in huffdec */ 107 108 #ifndef min 109 #define min(a, b) (((a) < (b)) ? (a) : (b)) 110 #endif 111 112 ERROR_t sym_restoreIPD(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) { 113 int sum_val = data[0] + data[1]; 114 int diff_val = data[0] - data[1]; 115 116 if (sum_val > lav) { 117 data[0] = -sum_val + (2 * lav + 1); 118 data[1] = -diff_val; 119 } else { 120 data[0] = sum_val; 121 data[1] = diff_val; 122 } 123 124 if (data[0] - data[1] != 0) { 125 ULONG sym_bit; 126 sym_bit = FDKreadBits(strm, 1); 127 if (sym_bit) { 128 int tmp; 129 tmp = data[0]; 130 data[0] = data[1]; 131 data[1] = tmp; 132 } 133 } 134 135 return HUFFDEC_OK; 136 } 137 138 static int ilog2(unsigned int i) { 139 int l = 0; 140 141 if (i) i--; 142 while (i > 0) { 143 i >>= 1; 144 l++; 145 } 146 147 return l; 148 } 149 150 static ERROR_t pcm_decode(HANDLE_FDK_BITSTREAM strm, SCHAR* out_data_1, 151 SCHAR* out_data_2, int offset, int num_val, 152 int num_levels) { 153 int i = 0, j = 0, idx = 0; 154 int max_grp_len = 0, next_val = 0; 155 ULONG tmp; 156 157 int pcm_chunk_size[7] = {0}; 158 159 switch (num_levels) { 160 case 3: 161 max_grp_len = 5; 162 break; 163 case 7: 164 max_grp_len = 6; 165 break; 166 case 11: 167 max_grp_len = 2; 168 break; 169 case 13: 170 max_grp_len = 4; 171 break; 172 case 19: 173 max_grp_len = 4; 174 break; 175 case 25: 176 max_grp_len = 3; 177 break; 178 case 51: 179 max_grp_len = 4; 180 break; 181 case 4: 182 case 8: 183 case 15: 184 case 16: 185 case 26: 186 case 31: 187 max_grp_len = 1; 188 break; 189 default: 190 return HUFFDEC_NOTOK; 191 } 192 193 tmp = 1; 194 for (i = 1; i <= max_grp_len; i++) { 195 tmp *= num_levels; 196 pcm_chunk_size[i] = ilog2(tmp); 197 } 198 199 for (i = 0; i < num_val; i += max_grp_len) { 200 int grp_len, grp_val, data; 201 grp_len = min(max_grp_len, num_val - i); 202 data = FDKreadBits(strm, pcm_chunk_size[grp_len]); 203 204 grp_val = data; 205 206 for (j = 0; j < grp_len; j++) { 207 idx = i + (grp_len - j - 1); 208 next_val = grp_val % num_levels; 209 210 if (out_data_2 == NULL) { 211 out_data_1[idx] = next_val - offset; 212 } else if (out_data_1 == NULL) { 213 out_data_2[idx] = next_val - offset; 214 } else { 215 if (idx % 2) { 216 out_data_2[idx / 2] = next_val - offset; 217 } else { 218 out_data_1[idx / 2] = next_val - offset; 219 } 220 } 221 222 grp_val = (grp_val - next_val) / num_levels; 223 } 224 } 225 226 return HUFFDEC_OK; 227 } 228 229 static ERROR_t huff_read(HANDLE_FDK_BITSTREAM strm, 230 const SHORT (*nodeTab)[MAX_ENTRIES][2], 231 int* out_data) { 232 int node = 0; 233 int len = 0; 234 235 do { 236 ULONG next_bit; 237 next_bit = FDKreadBits(strm, 1); 238 len++; 239 node = (*nodeTab)[node][next_bit]; 240 } while (node > 0); 241 242 *out_data = node; 243 244 return HUFFDEC_OK; 245 } 246 247 static ERROR_t huff_read_2D(HANDLE_FDK_BITSTREAM strm, 248 const SHORT (*nodeTab)[MAX_ENTRIES][2], 249 SCHAR out_data[2], int* escape) { 250 ERROR_t err = HUFFDEC_OK; 251 252 int huff_2D_8bit = 0; 253 int node = 0; 254 255 if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) { 256 goto bail; 257 } 258 *escape = (node == 0); 259 260 if (*escape) { 261 out_data[0] = 0; 262 out_data[1] = 1; 263 } else { 264 huff_2D_8bit = -(node + 1); 265 out_data[0] = huff_2D_8bit >> 4; 266 out_data[1] = huff_2D_8bit & 0xf; 267 } 268 269 bail: 270 return err; 271 } 272 273 static ERROR_t sym_restore(HANDLE_FDK_BITSTREAM strm, int lav, SCHAR data[2]) { 274 ULONG sym_bit = 0; 275 276 int sum_val = data[0] + data[1]; 277 int diff_val = data[0] - data[1]; 278 279 if (sum_val > lav) { 280 data[0] = -sum_val + (2 * lav + 1); 281 data[1] = -diff_val; 282 } else { 283 data[0] = sum_val; 284 data[1] = diff_val; 285 } 286 287 if (data[0] + data[1] != 0) { 288 sym_bit = FDKreadBits(strm, 1); 289 if (sym_bit) { 290 data[0] = -data[0]; 291 data[1] = -data[1]; 292 } 293 } 294 295 if (data[0] - data[1] != 0) { 296 sym_bit = FDKreadBits(strm, 1); 297 if (sym_bit) { 298 int tmp; 299 tmp = data[0]; 300 data[0] = data[1]; 301 data[1] = tmp; 302 } 303 } 304 305 return HUFFDEC_OK; 306 } 307 308 static ERROR_t huff_dec_1D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type, 309 const INT dim1, SCHAR* out_data, const INT num_val, 310 const INT p0_flag) 311 312 { 313 ERROR_t err = HUFFDEC_OK; 314 int i = 0, node = 0, offset = 0; 315 int od = 0, od_sign = 0; 316 ULONG data = 0; 317 int bitsAvail = 0; 318 319 const SHORT(*partTab)[MAX_ENTRIES][2] = NULL; 320 const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL; 321 322 switch (data_type) { 323 case t_CLD: 324 partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0]; 325 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h1D[dim1]->nodeTab[0][0]; 326 break; 327 case t_ICC: 328 partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0]; 329 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h1D[dim1]->nodeTab[0][0]; 330 break; 331 case t_OLD: 332 partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.old[0][0]; 333 nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h1D[dim1]->nodeTab[0][0]; 334 break; 335 case t_IPD: 336 partTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0]; 337 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h1D[dim1].nodeTab[0][0]; 338 break; 339 default: 340 FDK_ASSERT(0); 341 err = HUFFDEC_NOTOK; 342 goto bail; 343 } 344 345 if (p0_flag) { 346 if ((err = huff_read(strm, partTab, &node)) != HUFFDEC_OK) { 347 goto bail; 348 } 349 350 out_data[0] = -(node + 1); 351 offset = 1; 352 } 353 354 for (i = offset; i < num_val; i++) { 355 bitsAvail = FDKgetValidBits(strm); 356 if (bitsAvail < 1) { 357 err = HUFFDEC_NOTOK; 358 goto bail; 359 } 360 361 if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) { 362 goto bail; 363 } 364 od = -(node + 1); 365 366 if (data_type != t_IPD) { 367 if (od != 0) { 368 bitsAvail = FDKgetValidBits(strm); 369 if (bitsAvail < 1) { 370 err = HUFFDEC_NOTOK; 371 goto bail; 372 } 373 374 data = FDKreadBits(strm, 1); 375 od_sign = data; 376 377 if (od_sign) od = -od; 378 } 379 } 380 381 out_data[i] = od; 382 } 383 384 bail: 385 return err; 386 } 387 388 static ERROR_t huff_dec_2D(HANDLE_FDK_BITSTREAM strm, const DATA_TYPE data_type, 389 const INT dim1, const INT dim2, SCHAR out_data[][2], 390 const INT num_val, const INT stride, 391 SCHAR* p0_data[2]) { 392 ERROR_t err = HUFFDEC_OK; 393 int i = 0, lav = 0, escape = 0, escCntr = 0; 394 int node = 0; 395 unsigned long data = 0; 396 397 SCHAR esc_data[2][28] = {{0}}; 398 int escIdx[28] = {0}; 399 const SHORT(*nodeTab)[MAX_ENTRIES][2] = NULL; 400 401 /* LAV */ 402 if ((err = 403 huff_read(strm, (HANDLE_HUFF_NODE)&FDK_huffLavIdxNodes.nodeTab[0][0], 404 &node)) != HUFFDEC_OK) { 405 goto bail; 406 } 407 data = -(node + 1); 408 409 switch (data_type) { 410 case t_CLD: 411 lav = 2 * data + 3; /* 3, 5, 7, 9 */ 412 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.cld[0][0]; 413 break; 414 case t_ICC: 415 lav = 2 * data + 1; /* 1, 3, 5, 7 */ 416 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.icc[0][0]; 417 break; 418 case t_OLD: 419 lav = 3 * data + 3; 420 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.old[0][0]; 421 break; 422 case t_IPD: 423 if (data == 0) 424 data = 3; 425 else 426 data--; 427 lav = 2 * data + 1; /* 1, 3, 5, 7 */ 428 nodeTab = (HANDLE_HUFF_NODE)&FDK_huffPart0Nodes.ipd[0][0]; 429 break; 430 default: 431 FDK_ASSERT(0); 432 err = HUFFDEC_NOTOK; 433 goto bail; 434 } 435 436 /* Partition 0 */ 437 if (p0_data[0] != NULL) { 438 if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) { 439 goto bail; 440 } 441 *p0_data[0] = -(node + 1); 442 } 443 if (p0_data[1] != NULL) { 444 if ((err = huff_read(strm, nodeTab, &node)) != HUFFDEC_OK) { 445 goto bail; 446 } 447 *p0_data[1] = -(node + 1); 448 } 449 450 switch (data_type) { 451 case t_CLD: 452 switch (lav) { 453 case 3: 454 nodeTab = 455 (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav3[0][0]; 456 break; 457 case 5: 458 nodeTab = 459 (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav5[0][0]; 460 break; 461 case 7: 462 nodeTab = 463 (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav7[0][0]; 464 break; 465 case 9: 466 nodeTab = 467 (HANDLE_HUFF_NODE)&FDK_huffCLDNodes.h2D[dim1][dim2]->lav9[0][0]; 468 break; 469 } 470 break; 471 case t_ICC: 472 switch (lav) { 473 case 1: 474 nodeTab = 475 (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav1[0][0]; 476 break; 477 case 3: 478 nodeTab = 479 (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav3[0][0]; 480 break; 481 case 5: 482 nodeTab = 483 (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav5[0][0]; 484 break; 485 case 7: 486 nodeTab = 487 (HANDLE_HUFF_NODE)&FDK_huffICCNodes.h2D[dim1][dim2]->lav7[0][0]; 488 break; 489 } 490 break; 491 case t_OLD: 492 switch (lav) { 493 case 3: 494 nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav3[0][0]; 495 break; 496 case 6: 497 nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav6[0][0]; 498 break; 499 case 9: 500 nodeTab = (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav9[0][0]; 501 break; 502 case 12: 503 nodeTab = 504 (HANDLE_HUFF_NODE)&huffOLDNodes.h2D[dim1][dim2]->lav12[0][0]; 505 break; 506 } 507 break; 508 case t_IPD: 509 switch (lav) { 510 case 1: 511 nodeTab = 512 (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav1[0][0]; 513 break; 514 case 3: 515 nodeTab = 516 (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav3[0][0]; 517 break; 518 case 5: 519 nodeTab = 520 (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav5[0][0]; 521 break; 522 case 7: 523 nodeTab = 524 (HANDLE_HUFF_NODE)&FDK_huffIPDNodes.h2D[dim1][dim2].lav7[0][0]; 525 break; 526 } 527 break; 528 default: 529 break; 530 } 531 532 for (i = 0; i < num_val; i += stride) { 533 if ((err = huff_read_2D(strm, nodeTab, out_data[i], &escape)) != 534 HUFFDEC_OK) { 535 goto bail; 536 } 537 538 if (escape) { 539 escIdx[escCntr++] = i; 540 } else { 541 if (data_type == t_IPD) { 542 if ((err = sym_restoreIPD(strm, lav, out_data[i])) != HUFFDEC_OK) { 543 goto bail; 544 } 545 } else { 546 if ((err = sym_restore(strm, lav, out_data[i])) != HUFFDEC_OK) { 547 goto bail; 548 } 549 } 550 } 551 } /* i */ 552 553 if (escCntr > 0) { 554 if ((err = pcm_decode(strm, esc_data[0], esc_data[1], 0, 2 * escCntr, 555 (2 * lav + 1))) != HUFFDEC_OK) { 556 goto bail; 557 } 558 559 for (i = 0; i < escCntr; i++) { 560 out_data[escIdx[i]][0] = esc_data[0][i] - lav; 561 out_data[escIdx[i]][1] = esc_data[1][i] - lav; 562 } 563 } 564 bail: 565 return err; 566 } 567 568 static ERROR_t huff_decode(HANDLE_FDK_BITSTREAM strm, SCHAR* out_data_1, 569 SCHAR* out_data_2, DATA_TYPE data_type, 570 DIFF_TYPE diff_type_1, DIFF_TYPE diff_type_2, 571 int num_val, CODING_SCHEME* cdg_scheme, int ldMode) { 572 ERROR_t err = HUFFDEC_OK; 573 DIFF_TYPE diff_type; 574 575 int i = 0; 576 ULONG data = 0; 577 578 SCHAR pair_vec[28][2]; 579 580 SCHAR* p0_data_1[2] = {NULL, NULL}; 581 SCHAR* p0_data_2[2] = {NULL, NULL}; 582 583 int p0_flag[2]; 584 585 int num_val_1_int = num_val; 586 int num_val_2_int = num_val; 587 588 SCHAR* out_data_1_int = out_data_1; 589 SCHAR* out_data_2_int = out_data_2; 590 591 int df_rest_flag_1 = 0; 592 int df_rest_flag_2 = 0; 593 594 int hufYY1; 595 int hufYY2; 596 int hufYY; 597 598 /* Coding scheme */ 599 data = FDKreadBits(strm, 1); 600 *cdg_scheme = (CODING_SCHEME)(data << PAIR_SHIFT); 601 602 if (*cdg_scheme >> PAIR_SHIFT == HUFF_2D) { 603 if ((out_data_1 != NULL) && (out_data_2 != NULL) && (ldMode == 0)) { 604 data = FDKreadBits(strm, 1); 605 *cdg_scheme = (CODING_SCHEME)(*cdg_scheme | data); 606 } else { 607 *cdg_scheme = (CODING_SCHEME)(*cdg_scheme | FREQ_PAIR); 608 } 609 } 610 611 { 612 hufYY1 = diff_type_1; 613 hufYY2 = diff_type_2; 614 } 615 616 switch (*cdg_scheme >> PAIR_SHIFT) { 617 case HUFF_1D: 618 p0_flag[0] = (diff_type_1 == DIFF_FREQ); 619 p0_flag[1] = (diff_type_2 == DIFF_FREQ); 620 if (out_data_1 != NULL) { 621 if ((err = huff_dec_1D(strm, data_type, hufYY1, out_data_1, 622 num_val_1_int, p0_flag[0])) != HUFFDEC_OK) { 623 goto bail; 624 } 625 } 626 if (out_data_2 != NULL) { 627 if ((err = huff_dec_1D(strm, data_type, hufYY2, out_data_2, 628 num_val_2_int, p0_flag[1])) != HUFFDEC_OK) { 629 goto bail; 630 } 631 } 632 633 break; /* HUFF_1D */ 634 635 case HUFF_2D: 636 637 switch (*cdg_scheme & PAIR_MASK) { 638 case FREQ_PAIR: 639 640 if (out_data_1 != NULL) { 641 if (diff_type_1 == DIFF_FREQ) { 642 p0_data_1[0] = &out_data_1[0]; 643 p0_data_1[1] = NULL; 644 645 num_val_1_int -= 1; 646 out_data_1_int += 1; 647 } 648 df_rest_flag_1 = num_val_1_int % 2; 649 if (df_rest_flag_1) num_val_1_int -= 1; 650 } 651 if (out_data_2 != NULL) { 652 if (diff_type_2 == DIFF_FREQ) { 653 p0_data_2[0] = NULL; 654 p0_data_2[1] = &out_data_2[0]; 655 656 num_val_2_int -= 1; 657 out_data_2_int += 1; 658 } 659 df_rest_flag_2 = num_val_2_int % 2; 660 if (df_rest_flag_2) num_val_2_int -= 1; 661 } 662 663 if (out_data_1 != NULL) { 664 if ((err = huff_dec_2D(strm, data_type, hufYY1, FREQ_PAIR, pair_vec, 665 num_val_1_int, 2, p0_data_1)) != 666 HUFFDEC_OK) { 667 goto bail; 668 } 669 if (df_rest_flag_1) { 670 if ((err = huff_dec_1D(strm, data_type, hufYY1, 671 out_data_1_int + num_val_1_int, 1, 0)) != 672 HUFFDEC_OK) { 673 goto bail; 674 } 675 } 676 } 677 if (out_data_2 != NULL) { 678 if ((err = huff_dec_2D(strm, data_type, hufYY2, FREQ_PAIR, 679 pair_vec + 1, num_val_2_int, 2, 680 p0_data_2)) != HUFFDEC_OK) { 681 goto bail; 682 } 683 if (df_rest_flag_2) { 684 if ((err = huff_dec_1D(strm, data_type, hufYY2, 685 out_data_2_int + num_val_2_int, 1, 0)) != 686 HUFFDEC_OK) { 687 goto bail; 688 } 689 } 690 } 691 692 if (out_data_1 != NULL) { 693 for (i = 0; i < num_val_1_int - 1; i += 2) { 694 out_data_1_int[i] = pair_vec[i][0]; 695 out_data_1_int[i + 1] = pair_vec[i][1]; 696 } 697 } 698 if (out_data_2 != NULL) { 699 for (i = 0; i < num_val_2_int - 1; i += 2) { 700 out_data_2_int[i] = pair_vec[i + 1][0]; 701 out_data_2_int[i + 1] = pair_vec[i + 1][1]; 702 } 703 } 704 break; /* FREQ_PAIR */ 705 706 case TIME_PAIR: 707 if (((diff_type_1 == DIFF_FREQ) || (diff_type_2 == DIFF_FREQ))) { 708 p0_data_1[0] = &out_data_1[0]; 709 p0_data_1[1] = &out_data_2[0]; 710 711 out_data_1_int += 1; 712 out_data_2_int += 1; 713 714 num_val_1_int -= 1; 715 } 716 717 if ((diff_type_1 == DIFF_TIME) || (diff_type_2 == DIFF_TIME)) { 718 diff_type = DIFF_TIME; 719 } else { 720 diff_type = DIFF_FREQ; 721 } 722 { hufYY = diff_type; } 723 724 if ((err = huff_dec_2D(strm, data_type, hufYY, TIME_PAIR, pair_vec, 725 num_val_1_int, 1, p0_data_1)) != HUFFDEC_OK) { 726 goto bail; 727 } 728 729 for (i = 0; i < num_val_1_int; i++) { 730 out_data_1_int[i] = pair_vec[i][0]; 731 out_data_2_int[i] = pair_vec[i][1]; 732 } 733 734 break; /* TIME_PAIR */ 735 736 default: 737 break; 738 } 739 740 break; /* HUFF_2D */ 741 742 default: 743 break; 744 } 745 bail: 746 return err; 747 } 748 749 static void diff_freq_decode(const SCHAR* const diff_data, 750 SCHAR* const out_data, const int num_val) { 751 int i = 0; 752 out_data[0] = diff_data[0]; 753 754 for (i = 1; i < num_val; i++) { 755 out_data[i] = out_data[i - 1] + diff_data[i]; 756 } 757 } 758 759 static void diff_time_decode_backwards(const SCHAR* const prev_data, 760 const SCHAR* const diff_data, 761 SCHAR* const out_data, 762 const int mixed_diff_type, 763 const int num_val) { 764 int i = 0; /* default start value*/ 765 766 if (mixed_diff_type) { 767 out_data[0] = diff_data[0]; 768 i = 1; /* new start value */ 769 } 770 for (; i < num_val; i++) { 771 out_data[i] = prev_data[i] + diff_data[i]; 772 } 773 } 774 775 static void diff_time_decode_forwards(const SCHAR* const prev_data, 776 const SCHAR* const diff_data, 777 SCHAR* const out_data, 778 const int mixed_diff_type, 779 const int num_val) { 780 int i = 0; /* default start value*/ 781 782 if (mixed_diff_type) { 783 out_data[0] = diff_data[0]; 784 i = 1; /* new start value */ 785 } 786 for (; i < num_val; i++) { 787 out_data[i] = prev_data[i] - diff_data[i]; 788 } 789 } 790 791 static ERROR_t attach_lsb(HANDLE_FDK_BITSTREAM strm, SCHAR* in_data_msb, 792 int offset, int num_lsb, int num_val, 793 SCHAR* out_data) { 794 int i = 0, lsb = 0; 795 ULONG data = 0; 796 797 for (i = 0; i < num_val; i++) { 798 int msb; 799 msb = in_data_msb[i]; 800 801 if (num_lsb > 0) { 802 data = FDKreadBits(strm, num_lsb); 803 lsb = data; 804 805 out_data[i] = ((msb << num_lsb) | lsb) - offset; 806 } else 807 out_data[i] = msb - offset; 808 } 809 810 return HUFFDEC_OK; /* dummy */ 811 } 812 813 ERROR_t EcDataPairDec(DECODER_TYPE DECODER, HANDLE_FDK_BITSTREAM strm, 814 SCHAR* aaOutData1, SCHAR* aaOutData2, SCHAR* aHistory, 815 DATA_TYPE data_type, int startBand, int dataBands, 816 int pair_flag, int coarse_flag, 817 int allowDiffTimeBack_flag) 818 819 { 820 ERROR_t err = HUFFDEC_OK; 821 822 // int allowDiffTimeBack_flag = !independency_flag || (setIdx > 0); 823 int attachLsb_flag = 0; 824 int pcmCoding_flag = 0; 825 826 int mixed_time_pair = 0, numValPcm = 0; 827 int quant_levels = 0, quant_offset = 0; 828 ULONG data = 0; 829 830 SCHAR aaDataPair[2][28] = {{0}}; 831 SCHAR aaDataDiff[2][28] = {{0}}; 832 833 SCHAR aHistoryMsb[28] = {0}; 834 835 SCHAR* pDataVec[2] = {NULL, NULL}; 836 837 DIFF_TYPE diff_type[2] = {DIFF_FREQ, DIFF_FREQ}; 838 CODING_SCHEME cdg_scheme = HUFF_1D; 839 DIRECTION direction = BACKWARDS; 840 841 switch (data_type) { 842 case t_CLD: 843 if (coarse_flag) { 844 attachLsb_flag = 0; 845 quant_levels = 15; 846 quant_offset = 7; 847 } else { 848 attachLsb_flag = 0; 849 quant_levels = 31; 850 quant_offset = 15; 851 } 852 853 break; 854 855 case t_ICC: 856 if (coarse_flag) { 857 attachLsb_flag = 0; 858 quant_levels = 4; 859 quant_offset = 0; 860 } else { 861 attachLsb_flag = 0; 862 quant_levels = 8; 863 quant_offset = 0; 864 } 865 866 break; 867 868 case t_OLD: 869 if (coarse_flag) { 870 attachLsb_flag = 0; 871 quant_levels = 8; 872 quant_offset = 0; 873 } else { 874 attachLsb_flag = 0; 875 quant_levels = 16; 876 quant_offset = 0; 877 } 878 break; 879 880 case t_NRG: 881 if (coarse_flag) { 882 attachLsb_flag = 0; 883 quant_levels = 32; 884 quant_offset = 0; 885 } else { 886 attachLsb_flag = 0; 887 quant_levels = 64; 888 quant_offset = 0; 889 } 890 break; 891 892 case t_IPD: 893 if (!coarse_flag) { 894 attachLsb_flag = 1; 895 quant_levels = 16; 896 quant_offset = 0; 897 } else { 898 attachLsb_flag = 0; 899 quant_levels = 8; 900 quant_offset = 0; 901 } 902 break; 903 904 default: 905 return HUFFDEC_NOTOK; 906 } 907 908 data = FDKreadBits(strm, 1); 909 pcmCoding_flag = data; 910 911 if (pcmCoding_flag) { 912 if (pair_flag) { 913 pDataVec[0] = aaDataPair[0]; 914 pDataVec[1] = aaDataPair[1]; 915 numValPcm = 2 * dataBands; 916 } else { 917 pDataVec[0] = aaDataPair[0]; 918 pDataVec[1] = NULL; 919 numValPcm = dataBands; 920 } 921 922 err = pcm_decode(strm, pDataVec[0], pDataVec[1], quant_offset, numValPcm, 923 quant_levels); 924 if (err != HUFFDEC_OK) return HUFFDEC_NOTOK; 925 926 } else { /* Differential/Huffman/LSB Coding */ 927 928 if (pair_flag) { 929 pDataVec[0] = aaDataDiff[0]; 930 pDataVec[1] = aaDataDiff[1]; 931 } else { 932 pDataVec[0] = aaDataDiff[0]; 933 pDataVec[1] = NULL; 934 } 935 936 diff_type[0] = DIFF_FREQ; 937 diff_type[1] = DIFF_FREQ; 938 939 direction = BACKWARDS; 940 { 941 if (pair_flag || allowDiffTimeBack_flag) { 942 data = FDKreadBits(strm, 1); 943 diff_type[0] = (DIFF_TYPE)data; 944 } 945 946 if (pair_flag && 947 ((diff_type[0] == DIFF_FREQ) || allowDiffTimeBack_flag)) { 948 data = FDKreadBits(strm, 1); 949 diff_type[1] = (DIFF_TYPE)data; 950 } 951 } 952 /* Huffman decoding */ 953 err = huff_decode(strm, pDataVec[0], pDataVec[1], data_type, diff_type[0], 954 diff_type[1], dataBands, &cdg_scheme, 955 (DECODER == SAOC_DECODER)); 956 if (err != HUFFDEC_OK) { 957 return HUFFDEC_NOTOK; 958 } 959 960 { 961 /* Differential decoding */ 962 if ((diff_type[0] == DIFF_TIME) || (diff_type[1] == DIFF_TIME)) { 963 if (DECODER == SAOC_DECODER) { 964 direction = BACKWARDS; 965 } else { 966 if (pair_flag) { 967 if ((diff_type[0] == DIFF_TIME) && !allowDiffTimeBack_flag) { 968 direction = FORWARDS; 969 } else if (diff_type[1] == DIFF_TIME) { 970 direction = BACKWARDS; 971 } else { 972 data = FDKreadBits(strm, 1); 973 direction = (DIRECTION)data; 974 } 975 } else { 976 direction = BACKWARDS; 977 } 978 } 979 } 980 981 mixed_time_pair = (diff_type[0] != diff_type[1]) && 982 ((cdg_scheme & PAIR_MASK) == TIME_PAIR); 983 984 if (direction == BACKWARDS) { 985 if (diff_type[0] == DIFF_FREQ) { 986 diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands); 987 } else { 988 int i; 989 for (i = 0; i < dataBands; i++) { 990 aHistoryMsb[i] = aHistory[i + startBand] + quant_offset; 991 if (attachLsb_flag) { 992 aHistoryMsb[i] >>= 1; 993 } 994 } 995 diff_time_decode_backwards(aHistoryMsb, aaDataDiff[0], aaDataPair[0], 996 mixed_time_pair, dataBands); 997 } 998 if (diff_type[1] == DIFF_FREQ) { 999 diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands); 1000 } else { 1001 diff_time_decode_backwards(aaDataPair[0], aaDataDiff[1], 1002 aaDataPair[1], mixed_time_pair, dataBands); 1003 } 1004 } else { 1005 /* diff_type[1] MUST BE DIFF_FREQ */ 1006 diff_freq_decode(aaDataDiff[1], aaDataPair[1], dataBands); 1007 1008 if (diff_type[0] == DIFF_FREQ) { 1009 diff_freq_decode(aaDataDiff[0], aaDataPair[0], dataBands); 1010 } else { 1011 diff_time_decode_forwards(aaDataPair[1], aaDataDiff[0], aaDataPair[0], 1012 mixed_time_pair, dataBands); 1013 } 1014 } 1015 } 1016 1017 /* LSB decoding */ 1018 err = attach_lsb(strm, aaDataPair[0], quant_offset, attachLsb_flag ? 1 : 0, 1019 dataBands, aaDataPair[0]); 1020 if (err != HUFFDEC_OK) goto bail; 1021 1022 if (pair_flag) { 1023 err = attach_lsb(strm, aaDataPair[1], quant_offset, 1024 attachLsb_flag ? 1 : 0, dataBands, aaDataPair[1]); 1025 if (err != HUFFDEC_OK) goto bail; 1026 } 1027 } /* End: Differential/Huffman/LSB Coding */ 1028 1029 /* Copy data to output arrays */ 1030 FDKmemcpy(aaOutData1 + startBand, aaDataPair[0], sizeof(SCHAR) * dataBands); 1031 if (pair_flag) { 1032 FDKmemcpy(aaOutData2 + startBand, aaDataPair[1], sizeof(SCHAR) * dataBands); 1033 } 1034 1035 bail: 1036 return err; 1037 } 1038 1039 ERROR_t huff_dec_reshape(HANDLE_FDK_BITSTREAM strm, int* out_data, 1040 int num_val) { 1041 ERROR_t err = HUFFDEC_OK; 1042 int val_rcvd = 0, dummy = 0, i = 0, val = 0, len = 0; 1043 SCHAR rl_data[2] = {0}; 1044 1045 while (val_rcvd < num_val) { 1046 err = huff_read_2D(strm, 1047 (HANDLE_HUFF_NODE)&FDK_huffReshapeNodes.nodeTab[0][0], 1048 rl_data, &dummy); 1049 if (err != HUFFDEC_OK) goto bail; 1050 val = rl_data[0]; 1051 len = rl_data[1] + 1; 1052 if (val_rcvd + len > num_val) { 1053 err = HUFFDEC_NOTOK; 1054 goto bail; 1055 } 1056 for (i = val_rcvd; i < val_rcvd + len; i++) { 1057 out_data[i] = val; 1058 } 1059 val_rcvd += len; 1060 } 1061 bail: 1062 return err; 1063 } 1064