1 /* 2 * Copyright (C) 2003 - 2016 Sony Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "ldac.h" 18 19 20 21 /*************************************************************************************************** 22 Subfunction: Calculate Bits for Audio Block 23 ***************************************************************************************************/ 24 static int encode_audio_block_a_ldac( 25 AB *p_ab, 26 int hqu) 27 { 28 AC *p_ac; 29 int ich, iqu; 30 int nchs = p_ab->blk_nchs; 31 int tmp, nbits = 0; 32 int idsp, idwl1, idwl2; 33 int grad_mode = p_ab->grad_mode; 34 int grad_qu_l = p_ab->grad_qu_l; 35 int grad_qu_h = p_ab->grad_qu_h; 36 int grad_os_l = p_ab->grad_os_l; 37 int grad_os_h = p_ab->grad_os_h; 38 int *p_grad = p_ab->a_grad; 39 int *p_idsf, *p_addwl, *p_idwl1, *p_idwl2; 40 const unsigned char *p_t; 41 42 /* Calculate Gradient Curve */ 43 tmp = grad_qu_h - grad_qu_l; 44 45 for (iqu = 0; iqu < grad_qu_h; iqu++) { 46 p_grad[iqu] = -grad_os_l; 47 } 48 for (iqu = grad_qu_h; iqu < hqu; iqu++) { 49 p_grad[iqu] = -grad_os_h; 50 } 51 52 if (tmp > 0) { 53 p_t = gaa_resamp_grad_ldac[tmp-1]; 54 55 tmp = grad_os_h - grad_os_l; 56 if (tmp > 0) { 57 tmp = tmp-1; 58 for (iqu = grad_qu_l; iqu < grad_qu_h; iqu++) { 59 p_grad[iqu] -= ((*p_t++ * tmp) >> 8) + 1; 60 } 61 } 62 else if (tmp < 0) { 63 tmp = -tmp-1; 64 for (iqu = grad_qu_l; iqu < grad_qu_h; iqu++) { 65 p_grad[iqu] += ((*p_t++ * tmp) >> 8) + 1; 66 } 67 } 68 } 69 70 /* Calculate Bits */ 71 for (ich = 0; ich < nchs; ich++) { 72 p_ac = p_ab->ap_ac[ich]; 73 p_idsf = p_ac->a_idsf; 74 p_addwl = p_ac->a_addwl; 75 p_idwl1 = p_ac->a_idwl1; 76 p_idwl2 = p_ac->a_idwl2; 77 78 if (grad_mode == LDAC_MODE_0) { 79 for (iqu = 0; iqu < hqu; iqu++) { 80 idwl1 = p_idsf[iqu] + p_grad[iqu]; 81 if (idwl1 < LDAC_MINIDWL1) { 82 idwl1 = LDAC_MINIDWL1; 83 } 84 idwl2 = 0; 85 if (idwl1 > LDAC_MAXIDWL1) { 86 idwl2 = idwl1 - LDAC_MAXIDWL1; 87 if (idwl2 > LDAC_MAXIDWL2) { 88 idwl2 = LDAC_MAXIDWL2; 89 } 90 idwl1 = LDAC_MAXIDWL1; 91 } 92 p_idwl1[iqu] = idwl1; 93 p_idwl2[iqu] = idwl2; 94 idsp = ga_idsp_ldac[iqu]; 95 nbits += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 96 } 97 } 98 else if (grad_mode == LDAC_MODE_1) { 99 for (iqu = 0; iqu < hqu; iqu++) { 100 idwl1 = p_idsf[iqu] + p_grad[iqu] + p_addwl[iqu]; 101 if (idwl1 > 0) { 102 idwl1 = idwl1 >> 1; 103 } 104 if (idwl1 < LDAC_MINIDWL1) { 105 idwl1 = LDAC_MINIDWL1; 106 } 107 idwl2 = 0; 108 if (idwl1 > LDAC_MAXIDWL1) { 109 idwl2 = idwl1 - LDAC_MAXIDWL1; 110 if (idwl2 > LDAC_MAXIDWL2) { 111 idwl2 = LDAC_MAXIDWL2; 112 } 113 idwl1 = LDAC_MAXIDWL1; 114 } 115 p_idwl1[iqu] = idwl1; 116 p_idwl2[iqu] = idwl2; 117 idsp = ga_idsp_ldac[iqu]; 118 nbits += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 119 } 120 } 121 else if (grad_mode == LDAC_MODE_2) { 122 for (iqu = 0; iqu < hqu; iqu++) { 123 idwl1 = p_idsf[iqu] + p_grad[iqu] + p_addwl[iqu]; 124 if (idwl1 > 0) { 125 idwl1 = (idwl1*3) >> 3; 126 } 127 if (idwl1 < LDAC_MINIDWL1) { 128 idwl1 = LDAC_MINIDWL1; 129 } 130 idwl2 = 0; 131 if (idwl1 > LDAC_MAXIDWL1) { 132 idwl2 = idwl1 - LDAC_MAXIDWL1; 133 if (idwl2 > LDAC_MAXIDWL2) { 134 idwl2 = LDAC_MAXIDWL2; 135 } 136 idwl1 = LDAC_MAXIDWL1; 137 } 138 p_idwl1[iqu] = idwl1; 139 p_idwl2[iqu] = idwl2; 140 idsp = ga_idsp_ldac[iqu]; 141 nbits += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 142 } 143 } 144 else if (grad_mode == LDAC_MODE_3) { 145 for (iqu = 0; iqu < hqu; iqu++) { 146 idwl1 = p_idsf[iqu] + p_grad[iqu] + p_addwl[iqu]; 147 if (idwl1 > 0) { 148 idwl1 = idwl1 >> 2; 149 } 150 if (idwl1 < LDAC_MINIDWL1) { 151 idwl1 = LDAC_MINIDWL1; 152 } 153 idwl2 = 0; 154 if (idwl1 > LDAC_MAXIDWL1) { 155 idwl2 = idwl1 - LDAC_MAXIDWL1; 156 if (idwl2 > LDAC_MAXIDWL2) { 157 idwl2 = LDAC_MAXIDWL2; 158 } 159 idwl1 = LDAC_MAXIDWL1; 160 } 161 p_idwl1[iqu] = idwl1; 162 p_idwl2[iqu] = idwl2; 163 idsp = ga_idsp_ldac[iqu]; 164 nbits += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 165 } 166 } 167 } 168 169 return nbits; 170 } 171 172 /*************************************************************************************************** 173 Subfunction: Calculate Bits for Audio Block 174 ***************************************************************************************************/ 175 static int encode_audio_block_b_ldac( 176 AB *p_ab, 177 int nadjqus) 178 { 179 AC *p_ac; 180 int ich, iqu; 181 int nchs = p_ab->blk_nchs; 182 int nqus = min_ldac(LDAC_MAXNADJQUS, p_ab->nqus); 183 int nbits = 0; 184 int idsp, idwl1, idwl2; 185 int *p_idwl1, *p_idwl2, *p_tmp; 186 187 /* Calculate Bits */ 188 for (ich = 0; ich < nchs; ich++) { 189 p_ac = p_ab->ap_ac[ich]; 190 p_idwl1 = p_ac->a_idwl1; 191 p_idwl2 = p_ac->a_idwl2; 192 p_tmp = p_ac->a_tmp; 193 194 for (iqu = 0; iqu < nqus; iqu++) { 195 idwl1 = p_tmp[iqu]; 196 if (iqu < nadjqus) { 197 idwl1++; 198 } 199 idwl2 = 0; 200 if (idwl1 > LDAC_MAXIDWL1) { 201 idwl2 = idwl1 - LDAC_MAXIDWL1; 202 if (idwl2 > LDAC_MAXIDWL2) { 203 idwl2 = LDAC_MAXIDWL2; 204 } 205 idwl1 = LDAC_MAXIDWL1; 206 } 207 p_idwl1[iqu] = idwl1; 208 p_idwl2[iqu] = idwl2; 209 idsp = ga_idsp_ldac[iqu]; 210 nbits += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 211 } 212 } 213 214 return nbits; 215 } 216 217 /*************************************************************************************************** 218 Subfunction: Decrease Lower Offset of Gradient Curve 219 ***************************************************************************************************/ 220 static int decrease_offset_low_ldac( 221 AB *p_ab, 222 int limit, 223 int *p_nbits_spec) 224 { 225 int ncalls = 0; 226 int nqus = p_ab->nqus; 227 int grad_os_l = p_ab->grad_os_l; 228 int nbits_avail = p_ab->nbits_avail; 229 int step = limit - grad_os_l; 230 int a_checked[LDAC_MAXGRADOS+1]; 231 232 if (*p_nbits_spec > nbits_avail) { 233 memset(a_checked, 0, (LDAC_MAXGRADOS+1)*sizeof(int)); 234 235 while (grad_os_l < limit) { 236 if (step > 1) { 237 step = (step+1)/2; 238 } 239 240 if (*p_nbits_spec < nbits_avail) { 241 grad_os_l -= step; 242 if (grad_os_l < 0) { 243 grad_os_l += step; 244 break; 245 } 246 else if (a_checked[grad_os_l]) { 247 grad_os_l += step; 248 break; 249 } 250 } 251 else if (*p_nbits_spec > nbits_avail) { 252 grad_os_l += step; 253 if (grad_os_l > LDAC_MAXGRADOS) { 254 grad_os_l -= step; 255 break; 256 } 257 else if (a_checked[grad_os_l]) { 258 grad_os_l -= step; 259 break; 260 } 261 } 262 else { 263 break; 264 } 265 266 p_ab->grad_os_l = grad_os_l; 267 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 268 a_checked[grad_os_l] = *p_nbits_spec; 269 ncalls++; 270 } 271 272 while ((*p_nbits_spec > nbits_avail) && (grad_os_l < limit)) { 273 p_ab->grad_os_l = ++grad_os_l; 274 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 275 ncalls++; 276 } 277 } 278 279 return ncalls; 280 } 281 282 /*************************************************************************************************** 283 Subfunction: Decrease Higher Offset of Gradient Curve 284 ***************************************************************************************************/ 285 static int decrease_offset_high_ldac( 286 AB *p_ab, 287 int *p_nbits_spec) 288 { 289 int ncalls = 0; 290 int nqus = p_ab->nqus; 291 int grad_os_h = p_ab->grad_os_h; 292 int nbits_avail = p_ab->nbits_avail; 293 int step = LDAC_MAXGRADOS - grad_os_h; 294 int a_checked[LDAC_MAXGRADOS+1]; 295 296 if (*p_nbits_spec > nbits_avail) { 297 memset(a_checked, 0, (LDAC_MAXGRADOS+1)*sizeof(int)); 298 299 while (grad_os_h < LDAC_MAXGRADOS) { 300 if (step > 1) { 301 step = (step+1)/2; 302 } 303 304 if (*p_nbits_spec < nbits_avail) { 305 grad_os_h -= step; 306 if (grad_os_h < 0) { 307 grad_os_h += step; 308 break; 309 } 310 else if (a_checked[grad_os_h]) { 311 grad_os_h += step; 312 break; 313 } 314 } 315 else if (*p_nbits_spec > nbits_avail) { 316 grad_os_h += step; 317 if (grad_os_h > LDAC_MAXGRADOS) { 318 grad_os_h -= step; 319 break; 320 } 321 else if (a_checked[grad_os_h]) { 322 grad_os_h -= step; 323 break; 324 } 325 } 326 else { 327 break; 328 } 329 330 p_ab->grad_os_h = grad_os_h; 331 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 332 a_checked[grad_os_h] = *p_nbits_spec; 333 ncalls++; 334 } 335 336 while ((*p_nbits_spec > nbits_avail) && (grad_os_h < LDAC_MAXGRADOS)) { 337 p_ab->grad_os_h = ++grad_os_h; 338 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 339 ncalls++; 340 } 341 } 342 343 return ncalls; 344 } 345 346 /*************************************************************************************************** 347 Subfunction: Increase Lower Offset of Gradient Curve 348 ***************************************************************************************************/ 349 static int increase_offset_low_ldac( 350 AB *p_ab, 351 int *p_nbits_spec) 352 { 353 int ncalls = 0; 354 int nqus = p_ab->nqus; 355 int grad_os_l = p_ab->grad_os_l; 356 int nbits_avail = p_ab->nbits_avail; 357 int step = grad_os_l; 358 int a_checked[LDAC_MAXGRADOS+1]; 359 360 memset(a_checked, 0, (LDAC_MAXGRADOS+1)*sizeof(int)); 361 362 while (grad_os_l > 0) { 363 if (step > 1) { 364 step = (step+1)/2; 365 } 366 367 if (*p_nbits_spec < nbits_avail) { 368 grad_os_l -= step; 369 if (grad_os_l < 0) { 370 grad_os_l += step; 371 break; 372 } 373 else if (a_checked[grad_os_l]) { 374 grad_os_l += step; 375 break; 376 } 377 } 378 else if (*p_nbits_spec > nbits_avail) { 379 grad_os_l += step; 380 if (grad_os_l > LDAC_MAXGRADOS) { 381 grad_os_l -= step; 382 break; 383 } 384 else if (a_checked[grad_os_l]) { 385 grad_os_l -= step; 386 break; 387 } 388 } 389 else { 390 break; 391 } 392 393 p_ab->grad_os_l = grad_os_l; 394 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 395 a_checked[grad_os_l] = *p_nbits_spec; 396 ncalls++; 397 } 398 399 while ((*p_nbits_spec > nbits_avail) && (grad_os_l < LDAC_MAXGRADOS)) { 400 p_ab->grad_os_l = ++grad_os_l; 401 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 402 ncalls++; 403 } 404 405 return ncalls; 406 } 407 408 409 /*************************************************************************************************** 410 Subfunction: Increase Lower QU of Gradient Curve 411 ***************************************************************************************************/ 412 static int increase_qu_low_ldac( 413 AB *p_ab, 414 int *p_nbits_spec) 415 { 416 int ncalls = 0; 417 int nqus = p_ab->nqus; 418 int grad_qu_l = p_ab->grad_qu_l; 419 int grad_qu_h = p_ab->grad_qu_h; 420 int nbits_avail = p_ab->nbits_avail; 421 int step = grad_qu_h - grad_qu_l; 422 int a_checked[LDAC_DEFGRADQUH+1]; 423 424 memset(a_checked, 0, (LDAC_DEFGRADQUH+1)*sizeof(int)); 425 426 while ((grad_qu_l > 0) && (grad_qu_l < LDAC_DEFGRADQUH)) { 427 if (step > 1) { 428 step = (step+1)/2; 429 } 430 431 if (*p_nbits_spec < nbits_avail) { 432 grad_qu_l += step; 433 if (grad_qu_l > LDAC_DEFGRADQUH) { 434 grad_qu_l -= step; 435 break; 436 } 437 else if (a_checked[grad_qu_l]) { 438 grad_qu_l -= step; 439 break; 440 } 441 } 442 else if (*p_nbits_spec > nbits_avail) { 443 grad_qu_l -= step; 444 if (grad_qu_l < 0) { 445 grad_qu_l += step; 446 break; 447 } 448 else if (a_checked[grad_qu_l]) { 449 grad_qu_l += step; 450 break; 451 } 452 } 453 else { 454 break; 455 } 456 457 p_ab->grad_qu_l = grad_qu_l; 458 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 459 a_checked[grad_qu_l] = *p_nbits_spec; 460 ncalls++; 461 } 462 463 while ((*p_nbits_spec > nbits_avail) && (grad_qu_l <= LDAC_DEFGRADQUH)) { 464 p_ab->grad_qu_l = --grad_qu_l; 465 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 466 ncalls++; 467 } 468 469 return ncalls; 470 } 471 472 /*************************************************************************************************** 473 Subfunction: Increase Lower QU of Gradient Curve 474 ***************************************************************************************************/ 475 static int increase_qu_low_0_ldac( 476 AB *p_ab, 477 int *p_nbits_spec) 478 { 479 int ncalls = 0; 480 int nqus = p_ab->nqus; 481 int grad_qu_l = p_ab->grad_qu_l; 482 int grad_qu_h = p_ab->grad_qu_h; 483 int nbits_avail = p_ab->nbits_avail; 484 int step = grad_qu_h - grad_qu_l; 485 int a_checked[LDAC_MAXGRADQU+1]; 486 487 memset(a_checked, 0, (LDAC_MAXGRADQU+1)*sizeof(int)); 488 489 while ((grad_qu_l > 0) && (grad_qu_l < grad_qu_h)) { 490 if (step > 1) { 491 step = step/2; 492 } 493 494 if (*p_nbits_spec < nbits_avail) { 495 grad_qu_l += step; 496 if (grad_qu_l >= grad_qu_h) { 497 grad_qu_l -= step; 498 break; 499 } 500 else if (a_checked[grad_qu_l]) { 501 grad_qu_l -= step; 502 break; 503 } 504 } 505 else if (*p_nbits_spec > nbits_avail) { 506 grad_qu_l -= step; 507 if (grad_qu_l < 0) { 508 grad_qu_l += step; 509 break; 510 } 511 else if (a_checked[grad_qu_l]) { 512 grad_qu_l += step; 513 break; 514 } 515 } 516 else { 517 break; 518 } 519 520 p_ab->grad_qu_l = grad_qu_l; 521 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 522 a_checked[grad_qu_l] = *p_nbits_spec; 523 ncalls++; 524 } 525 526 while ((*p_nbits_spec > nbits_avail) && (grad_qu_l > 0)) { 527 p_ab->grad_qu_l = --grad_qu_l; 528 *p_nbits_spec = encode_audio_block_a_ldac(p_ab, nqus); 529 ncalls++; 530 } 531 532 return ncalls; 533 } 534 535 /*************************************************************************************************** 536 Subfunction: Adjust Remaining Bits 537 ***************************************************************************************************/ 538 static int adjust_remain_bits_ldac( 539 AB *p_ab, 540 int *p_nbits_spec, 541 int *p_nadjqus) 542 { 543 int ich, iqu; 544 int ncalls = 0; 545 int nbits_fix, nbits_spec; 546 int nbits_avail = p_ab->nbits_avail; 547 int idsp, idwl1, idwl2, tmp; 548 int step = LDAC_MAXNADJQUS>>1; 549 int nadjqus = LDAC_MAXNADJQUS>>1; 550 int nchs = p_ab->blk_nchs; 551 int nqus = min_ldac(LDAC_MAXNADJQUS, p_ab->nqus); 552 int grad_mode = p_ab->grad_mode; 553 int *p_grad = p_ab->a_grad; 554 int *p_idsf, *p_addwl, *p_idwl1, *p_idwl2, *p_tmp; 555 AC *p_ac; 556 557 nbits_fix = 0; 558 for (ich = 0; ich < nchs; ich++){ 559 p_ac = p_ab->ap_ac[ich]; 560 p_idsf = p_ac->a_idsf; 561 p_addwl = p_ac->a_addwl; 562 p_idwl1 = p_ac->a_idwl1; 563 p_idwl2 = p_ac->a_idwl2; 564 p_tmp = p_ac->a_tmp; 565 566 if (grad_mode == LDAC_MODE_0) { 567 for (iqu = 0; iqu < nqus; iqu++) { 568 idwl1 = p_idwl1[iqu]; 569 idwl2 = p_idwl2[iqu]; 570 idsp = ga_idsp_ldac[iqu]; 571 nbits_fix += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 572 tmp = p_idsf[iqu] + p_grad[iqu]; 573 if (tmp < LDAC_MINIDWL1) { 574 tmp = LDAC_MINIDWL1; 575 } 576 p_tmp[iqu] = tmp; 577 } 578 } 579 else if (grad_mode == LDAC_MODE_1) { 580 for (iqu = 0; iqu < nqus; iqu++) { 581 idwl1 = p_idwl1[iqu]; 582 idwl2 = p_idwl2[iqu]; 583 idsp = ga_idsp_ldac[iqu]; 584 nbits_fix += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 585 tmp = p_idsf[iqu] + p_grad[iqu] + p_addwl[iqu]; 586 if (tmp > 0) { 587 tmp = tmp >> 1; 588 } 589 if (tmp < LDAC_MINIDWL1) { 590 tmp = LDAC_MINIDWL1; 591 } 592 p_tmp[iqu] = tmp; 593 } 594 } 595 else if (grad_mode == LDAC_MODE_2) { 596 for (iqu = 0; iqu < nqus; iqu++) { 597 idwl1 = p_idwl1[iqu]; 598 idwl2 = p_idwl2[iqu]; 599 idsp = ga_idsp_ldac[iqu]; 600 nbits_fix += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 601 tmp = p_idsf[iqu] + p_grad[iqu] + p_addwl[iqu]; 602 if (tmp > 0) { 603 tmp = (tmp*3) >> 3; 604 } 605 if (tmp < LDAC_MINIDWL1) { 606 tmp = LDAC_MINIDWL1; 607 } 608 p_tmp[iqu] = tmp; 609 } 610 } 611 else if (grad_mode == LDAC_MODE_3) { 612 for (iqu = 0; iqu < nqus; iqu++) { 613 idwl1 = p_idwl1[iqu]; 614 idwl2 = p_idwl2[iqu]; 615 idsp = ga_idsp_ldac[iqu]; 616 nbits_fix += gaa_ndim_wls_ldac[idsp][idwl1] + ga_wl_ldac[idwl2] * ga_nsps_ldac[iqu]; 617 tmp = p_idsf[iqu] + p_grad[iqu] + p_addwl[iqu]; 618 if (tmp > 0) { 619 tmp = tmp >> 2; 620 } 621 if (tmp < LDAC_MINIDWL1) { 622 tmp = LDAC_MINIDWL1; 623 } 624 p_tmp[iqu] = tmp; 625 } 626 } 627 } 628 629 nbits_fix = *p_nbits_spec - nbits_fix; 630 nbits_spec = nbits_fix + encode_audio_block_b_ldac(p_ab, nadjqus); 631 ncalls++; 632 633 while (step > 1) { 634 step >>= 1; 635 636 if (nbits_spec < nbits_avail) { 637 nadjqus += step; 638 if (nadjqus > p_ab->nqus) { 639 nadjqus = p_ab->nqus; 640 } 641 } 642 else if (nbits_spec > nbits_avail) { 643 nadjqus -= step; 644 } 645 else { 646 if (nadjqus > p_ab->nqus) { 647 nadjqus = p_ab->nqus; 648 } 649 break; 650 } 651 nbits_spec = nbits_fix + encode_audio_block_b_ldac(p_ab, nadjqus); 652 ncalls++; 653 } 654 655 if (nbits_spec > nbits_avail) { 656 nadjqus--; 657 nbits_spec = nbits_fix + encode_audio_block_b_ldac(p_ab, nadjqus); 658 ncalls++; 659 } 660 *p_nadjqus = nadjqus; 661 *p_nbits_spec = nbits_spec; 662 663 return ncalls; 664 } 665 666 /*************************************************************************************************** 667 Allocate Bits 668 ***************************************************************************************************/ 669 #define LDAC_UPPER_NOISE_LEVEL 20 670 #define LDAC_LOWER_NOISE_LEVEL 5 671 672 DECLFUNC int alloc_bits_ldac( 673 AB *p_ab) 674 { 675 int nbits_avail, nbits_side = 0, nbits_spec = 0; 676 int nbits_ab = p_ab->nbits_ab; 677 678 nbits_side = encode_side_info_ldac(p_ab); 679 p_ab->nbits_avail = nbits_avail = nbits_ab - nbits_side; 680 681 nbits_spec = encode_audio_block_a_ldac(p_ab, p_ab->nqus); 682 683 if (nbits_spec > nbits_avail) { 684 if (p_ab->grad_mode == LDAC_MODE_0) { 685 decrease_offset_low_ldac(p_ab, LDAC_UPPER_NOISE_LEVEL, &nbits_spec); 686 687 decrease_offset_high_ldac(p_ab, &nbits_spec); 688 689 decrease_offset_low_ldac(p_ab, LDAC_MAXGRADOS, &nbits_spec); 690 } 691 else { 692 decrease_offset_low_ldac(p_ab, LDAC_MAXGRADOS, &nbits_spec); 693 } 694 695 while ((nbits_spec > nbits_avail) && (p_ab->nbands > LDAC_BAND_OFFSET)) { 696 p_ab->nbands--; 697 p_ab->nqus = ga_nqus_ldac[p_ab->nbands]; 698 699 nbits_side = encode_side_info_ldac(p_ab); 700 p_ab->nbits_avail = nbits_avail = nbits_ab - nbits_side; 701 702 nbits_spec = encode_audio_block_a_ldac(p_ab, p_ab->nqus); 703 } 704 } 705 706 if (nbits_spec < nbits_avail) { 707 if (p_ab->grad_mode == LDAC_MODE_0) { 708 increase_offset_low_ldac(p_ab, &nbits_spec); 709 710 increase_qu_low_0_ldac(p_ab, &nbits_spec); 711 } 712 else { 713 increase_offset_low_ldac(p_ab, &nbits_spec); 714 715 increase_qu_low_ldac(p_ab, &nbits_spec); 716 } 717 } 718 719 p_ab->nadjqus = 0; 720 adjust_remain_bits_ldac(p_ab, &nbits_spec, &p_ab->nadjqus); 721 722 if (nbits_spec > nbits_avail) { 723 *p_ab->p_error_code = LDAC_ERR_BIT_ALLOCATION; 724 return LDAC_FALSE; 725 } 726 p_ab->nbits_spec = nbits_spec; 727 p_ab->nbits_used = nbits_spec + nbits_side; 728 729 730 return LDAC_TRUE; 731 } 732 733 734