dyn_bits.c revision e2e838afcf03e603a41a0455846eaf9614537c16
1/* 2 ** Copyright 2003-2010, VisualOn, Inc. 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 File: dyn_bits.c 18 19 Content: Noiseless coder module functions 20 21*******************************************************************************/ 22 23#include "aac_rom.h" 24#include "dyn_bits.h" 25#include "bit_cnt.h" 26#include "psy_const.h" 27 28 29/***************************************************************************** 30* 31* function name: buildBitLookUp 32* description: count bits using all possible tables 33* 34*****************************************************************************/ 35static void 36buildBitLookUp(const Word16 *quantSpectrum, 37 const Word16 maxSfb, 38 const Word16 *sfbOffset, 39 const UWord16 *sfbMax, 40 Word16 bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], 41 SECTION_INFO * sectionInfo) 42{ 43 Word32 i; 44 45 for (i=0; i<maxSfb; i++) { 46 Word16 sfbWidth, maxVal; 47 48 sectionInfo[i].sfbCnt = 1; 49 sectionInfo[i].sfbStart = i; 50 sectionInfo[i].sectionBits = INVALID_BITCOUNT; 51 sectionInfo[i].codeBook = -1; 52 sfbWidth = sfbOffset[i + 1] - sfbOffset[i]; 53 maxVal = sfbMax[i]; 54 bitCount(quantSpectrum + sfbOffset[i], sfbWidth, maxVal, bitLookUp[i]); 55 } 56} 57 58 59/***************************************************************************** 60* 61* function name: findBestBook 62* description: essential helper functions 63* 64*****************************************************************************/ 65static Word16 66findBestBook(const Word16 *bc, Word16 *book) 67{ 68 Word32 minBits, j; 69 minBits = INVALID_BITCOUNT; 70 71 for (j=0; j<=CODE_BOOK_ESC_NDX; j++) { 72 73 if (bc[j] < minBits) { 74 minBits = bc[j]; 75 *book = j; 76 } 77 } 78 return extract_l(minBits); 79} 80 81static Word16 82findMinMergeBits(const Word16 *bc1, const Word16 *bc2) 83{ 84 Word32 minBits, j, sum; 85 minBits = INVALID_BITCOUNT; 86 87 for (j=0; j<=CODE_BOOK_ESC_NDX; j++) { 88 sum = bc1[j] + bc2[j]; 89 if (sum < minBits) { 90 minBits = sum; 91 } 92 } 93 return extract_l(minBits); 94} 95 96static void 97mergeBitLookUp(Word16 *bc1, const Word16 *bc2) 98{ 99 Word32 j; 100 101 for (j=0; j<=CODE_BOOK_ESC_NDX; j++) { 102 bc1[j] = min(bc1[j] + bc2[j], INVALID_BITCOUNT); 103 } 104} 105 106static Word16 107findMaxMerge(const Word16 mergeGainLookUp[MAX_SFB_LONG], 108 const SECTION_INFO *sectionInfo, 109 const Word16 maxSfb, Word16 *maxNdx) 110{ 111 Word32 i, maxMergeGain; 112 maxMergeGain = 0; 113 114 for (i=0; i+sectionInfo[i].sfbCnt < maxSfb; i += sectionInfo[i].sfbCnt) { 115 116 if (mergeGainLookUp[i] > maxMergeGain) { 117 maxMergeGain = mergeGainLookUp[i]; 118 *maxNdx = i; 119 } 120 } 121 return extract_l(maxMergeGain); 122} 123 124 125 126static Word16 127CalcMergeGain(const SECTION_INFO *sectionInfo, 128 Word16 bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], 129 const Word16 *sideInfoTab, 130 const Word16 ndx1, 131 const Word16 ndx2) 132{ 133 Word32 SplitBits; 134 Word32 MergeBits; 135 Word32 MergeGain; 136 137 /* 138 Bit amount for splitted sections 139 */ 140 SplitBits = sectionInfo[ndx1].sectionBits + sectionInfo[ndx2].sectionBits; 141 142 MergeBits = sideInfoTab[sectionInfo[ndx1].sfbCnt + sectionInfo[ndx2].sfbCnt] + 143 findMinMergeBits(bitLookUp[ndx1], bitLookUp[ndx2]); 144 MergeGain = (SplitBits - MergeBits); 145 146 return extract_l(MergeGain); 147} 148 149/* 150 sectioning Stage 0:find minimum codbooks 151*/ 152 153static void 154gmStage0(SECTION_INFO * sectionInfo, 155 Word16 bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], 156 const Word16 maxSfb) 157{ 158 Word32 i; 159 160 for (i=0; i<maxSfb; i++) { 161 /* Side-Info bits will be calculated in Stage 1! */ 162 163 if (sectionInfo[i].sectionBits == INVALID_BITCOUNT) { 164 sectionInfo[i].sectionBits = findBestBook(bitLookUp[i], &(sectionInfo[i].codeBook)); 165 } 166 } 167} 168 169/* 170 sectioning Stage 1:merge all connected regions with the same code book and 171 calculate side info 172*/ 173 174static void 175gmStage1(SECTION_INFO * sectionInfo, 176 Word16 bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], 177 const Word16 maxSfb, 178 const Word16 *sideInfoTab) 179{ 180 SECTION_INFO * sectionInfo_s; 181 SECTION_INFO * sectionInfo_e; 182 Word32 mergeStart, mergeEnd; 183 mergeStart = 0; 184 185 do { 186 187 sectionInfo_s = sectionInfo + mergeStart; 188 for (mergeEnd=mergeStart+1; mergeEnd<maxSfb; mergeEnd++) { 189 sectionInfo_e = sectionInfo + mergeEnd; 190 if (sectionInfo_s->codeBook != sectionInfo_e->codeBook) 191 break; 192 sectionInfo_s->sfbCnt += 1; 193 sectionInfo_s->sectionBits += sectionInfo_e->sectionBits; 194 195 mergeBitLookUp(bitLookUp[mergeStart], bitLookUp[mergeEnd]); 196 } 197 198 sectionInfo_s->sectionBits += sideInfoTab[sectionInfo_s->sfbCnt]; 199 sectionInfo[mergeEnd - 1].sfbStart = sectionInfo_s->sfbStart; /* speed up prev search */ 200 201 mergeStart = mergeEnd; 202 203 204 } while (mergeStart - maxSfb < 0); 205} 206 207/* 208 sectioning Stage 2:greedy merge algorithm, merge connected sections with 209 maximum bit gain until no more gain is possible 210*/ 211static void 212gmStage2(SECTION_INFO *sectionInfo, 213 Word16 mergeGainLookUp[MAX_SFB_LONG], 214 Word16 bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], 215 const Word16 maxSfb, 216 const Word16 *sideInfoTab) 217{ 218 Word16 i; 219 220 for (i=0; i+sectionInfo[i].sfbCnt<maxSfb; i+=sectionInfo[i].sfbCnt) { 221 mergeGainLookUp[i] = CalcMergeGain(sectionInfo, 222 bitLookUp, 223 sideInfoTab, 224 i, 225 (i + sectionInfo[i].sfbCnt)); 226 } 227 228 while (TRUE) { 229 Word16 maxMergeGain, maxNdx, maxNdxNext, maxNdxLast; 230 231 maxMergeGain = findMaxMerge(mergeGainLookUp, sectionInfo, maxSfb, &maxNdx); 232 233 234 if (maxMergeGain <= 0) 235 break; 236 237 238 maxNdxNext = maxNdx + sectionInfo[maxNdx].sfbCnt; 239 240 sectionInfo[maxNdx].sfbCnt = sectionInfo[maxNdx].sfbCnt + sectionInfo[maxNdxNext].sfbCnt; 241 sectionInfo[maxNdx].sectionBits = sectionInfo[maxNdx].sectionBits + 242 (sectionInfo[maxNdxNext].sectionBits - maxMergeGain); 243 244 245 mergeBitLookUp(bitLookUp[maxNdx], bitLookUp[maxNdxNext]); 246 247 248 if (maxNdx != 0) { 249 maxNdxLast = sectionInfo[maxNdx - 1].sfbStart; 250 mergeGainLookUp[maxNdxLast] = CalcMergeGain(sectionInfo, 251 bitLookUp, 252 sideInfoTab, 253 maxNdxLast, 254 maxNdx); 255 } 256 maxNdxNext = maxNdx + sectionInfo[maxNdx].sfbCnt; 257 258 sectionInfo[maxNdxNext - 1].sfbStart = sectionInfo[maxNdx].sfbStart; 259 260 261 if (maxNdxNext - maxSfb < 0) { 262 mergeGainLookUp[maxNdx] = CalcMergeGain(sectionInfo, 263 bitLookUp, 264 sideInfoTab, 265 maxNdx, 266 maxNdxNext); 267 } 268 } 269} 270 271/* 272 count bits used by the noiseless coder 273*/ 274static void 275noiselessCounter(SECTION_DATA *sectionData, 276 Word16 mergeGainLookUp[MAX_SFB_LONG], 277 Word16 bitLookUp[MAX_SFB_LONG][CODE_BOOK_ESC_NDX + 1], 278 const Word16 *quantSpectrum, 279 const UWord16 *maxValueInSfb, 280 const Word16 *sfbOffset, 281 const Word32 blockType) 282{ 283 Word32 grpNdx, i; 284 Word16 *sideInfoTab = NULL; 285 SECTION_INFO *sectionInfo; 286 287 /* 288 use appropriate side info table 289 */ 290 switch (blockType) 291 { 292 case LONG_WINDOW: 293 case START_WINDOW: 294 case STOP_WINDOW: 295 sideInfoTab = sideInfoTabLong; 296 break; 297 case SHORT_WINDOW: 298 sideInfoTab = sideInfoTabShort; 299 break; 300 } 301 302 303 sectionData->noOfSections = 0; 304 sectionData->huffmanBits = 0; 305 sectionData->sideInfoBits = 0; 306 307 308 if (sectionData->maxSfbPerGroup == 0) 309 return; 310 311 /* 312 loop trough groups 313 */ 314 for (grpNdx=0; grpNdx<sectionData->sfbCnt; grpNdx+=sectionData->sfbPerGroup) { 315 316 sectionInfo = sectionData->sectionInfo + sectionData->noOfSections; 317 318 buildBitLookUp(quantSpectrum, 319 sectionData->maxSfbPerGroup, 320 sfbOffset + grpNdx, 321 maxValueInSfb + grpNdx, 322 bitLookUp, 323 sectionInfo); 324 325 /* 326 0.Stage 327 */ 328 gmStage0(sectionInfo, bitLookUp, sectionData->maxSfbPerGroup); 329 330 /* 331 1.Stage 332 */ 333 gmStage1(sectionInfo, bitLookUp, sectionData->maxSfbPerGroup, sideInfoTab); 334 335 336 /* 337 2.Stage 338 */ 339 gmStage2(sectionInfo, 340 mergeGainLookUp, 341 bitLookUp, 342 sectionData->maxSfbPerGroup, 343 sideInfoTab); 344 345 346 /* 347 compress output, calculate total huff and side bits 348 */ 349 for (i=0; i<sectionData->maxSfbPerGroup; i+=sectionInfo[i].sfbCnt) { 350 findBestBook(bitLookUp[i], &(sectionInfo[i].codeBook)); 351 sectionInfo[i].sfbStart = sectionInfo[i].sfbStart + grpNdx; 352 353 sectionData->huffmanBits = (sectionData->huffmanBits + 354 (sectionInfo[i].sectionBits - sideInfoTab[sectionInfo[i].sfbCnt])); 355 sectionData->sideInfoBits = (sectionData->sideInfoBits + sideInfoTab[sectionInfo[i].sfbCnt]); 356 sectionData->sectionInfo[sectionData->noOfSections] = sectionInfo[i]; 357 sectionData->noOfSections = sectionData->noOfSections + 1; 358 } 359 } 360} 361 362 363/******************************************************************************* 364* 365* functionname: scfCount 366* returns : --- 367* description : count bits used by scalefactors. 368* 369********************************************************************************/ 370static void scfCount(const Word16 *scalefacGain, 371 const UWord16 *maxValueInSfb, 372 SECTION_DATA * sectionData) 373 374{ 375 SECTION_INFO *psectionInfo; 376 SECTION_INFO *psectionInfom; 377 378 /* counter */ 379 Word32 i = 0; /* section counter */ 380 Word32 j = 0; /* sfb counter */ 381 Word32 k = 0; /* current section auxiliary counter */ 382 Word32 m = 0; /* other section auxiliary counter */ 383 Word32 n = 0; /* other sfb auxiliary counter */ 384 385 /* further variables */ 386 Word32 lastValScf = 0; 387 Word32 deltaScf = 0; 388 Flag found = 0; 389 Word32 scfSkipCounter = 0; 390 391 392 sectionData->scalefacBits = 0; 393 394 395 if (scalefacGain == NULL) { 396 return; 397 } 398 399 lastValScf = 0; 400 sectionData->firstScf = 0; 401 402 psectionInfo = sectionData->sectionInfo; 403 for (i=0; i<sectionData->noOfSections; i++) { 404 405 if (psectionInfo->codeBook != CODE_BOOK_ZERO_NO) { 406 sectionData->firstScf = psectionInfo->sfbStart; 407 lastValScf = scalefacGain[sectionData->firstScf]; 408 break; 409 } 410 psectionInfo += 1; 411 } 412 413 psectionInfo = sectionData->sectionInfo; 414 for (i=0; i<sectionData->noOfSections; i++, psectionInfo += 1) { 415 416 if (psectionInfo->codeBook != CODE_BOOK_ZERO_NO 417 && psectionInfo->codeBook != CODE_BOOK_PNS_NO) { 418 for (j = psectionInfo->sfbStart; 419 j < (psectionInfo->sfbStart + psectionInfo->sfbCnt); j++) { 420 /* check if we can repeat the last value to save bits */ 421 422 if (maxValueInSfb[j] == 0) { 423 found = 0; 424 425 if (scfSkipCounter == 0) { 426 /* end of section */ 427 428 if (j - ((psectionInfo->sfbStart + psectionInfo->sfbCnt) - 1) == 0) { 429 found = 0; 430 } 431 else { 432 for (k = j + 1; k < psectionInfo->sfbStart + psectionInfo->sfbCnt; k++) { 433 434 if (maxValueInSfb[k] != 0) { 435 int tmp = L_abs(scalefacGain[k] - lastValScf); 436 found = 1; 437 438 if ( tmp < CODE_BOOK_SCF_LAV) { 439 /* save bits */ 440 deltaScf = 0; 441 } 442 else { 443 /* do not save bits */ 444 deltaScf = lastValScf - scalefacGain[j]; 445 lastValScf = scalefacGain[j]; 446 scfSkipCounter = 0; 447 } 448 break; 449 } 450 /* count scalefactor skip */ 451 scfSkipCounter = scfSkipCounter + 1; 452 } 453 } 454 455 psectionInfom = psectionInfo + 1; 456 /* search for the next maxValueInSfb[] != 0 in all other sections */ 457 for (m = i + 1; (m < sectionData->noOfSections) && (found == 0); m++) { 458 459 if ((psectionInfom->codeBook != CODE_BOOK_ZERO_NO) && 460 (psectionInfom->codeBook != CODE_BOOK_PNS_NO)) { 461 for (n = psectionInfom->sfbStart; 462 n < (psectionInfom->sfbStart + psectionInfom->sfbCnt); n++) { 463 464 if (maxValueInSfb[n] != 0) { 465 found = 1; 466 467 if ( (abs_s(scalefacGain[n] - lastValScf) < CODE_BOOK_SCF_LAV)) { 468 deltaScf = 0; 469 } 470 else { 471 deltaScf = (lastValScf - scalefacGain[j]); 472 lastValScf = scalefacGain[j]; 473 scfSkipCounter = 0; 474 } 475 break; 476 } 477 /* count scalefactor skip */ 478 scfSkipCounter = scfSkipCounter + 1; 479 } 480 } 481 482 psectionInfom += 1; 483 } 484 485 if (found == 0) { 486 deltaScf = 0; 487 scfSkipCounter = 0; 488 } 489 } 490 else { 491 deltaScf = 0; 492 scfSkipCounter = scfSkipCounter - 1; 493 } 494 } 495 else { 496 deltaScf = lastValScf - scalefacGain[j]; 497 lastValScf = scalefacGain[j]; 498 } 499 sectionData->scalefacBits += bitCountScalefactorDelta(deltaScf); 500 } 501 } 502 } 503} 504 505 506typedef Word16 (*lookUpTable)[CODE_BOOK_ESC_NDX + 1]; 507 508 509Word16 510dynBitCount(const Word16 *quantSpectrum, 511 const UWord16 *maxValueInSfb, 512 const Word16 *scalefac, 513 const Word16 blockType, 514 const Word16 sfbCnt, 515 const Word16 maxSfbPerGroup, 516 const Word16 sfbPerGroup, 517 const Word16 *sfbOffset, 518 SECTION_DATA *sectionData) 519{ 520 sectionData->blockType = blockType; 521 sectionData->sfbCnt = sfbCnt; 522 sectionData->sfbPerGroup = sfbPerGroup; 523 if(sfbPerGroup) 524 sectionData->noOfGroups = sfbCnt/sfbPerGroup; 525 else 526 sectionData->noOfGroups = 0x7fff; 527 sectionData->maxSfbPerGroup = maxSfbPerGroup; 528 529 noiselessCounter(sectionData, 530 sectionData->mergeGainLookUp, 531 (lookUpTable)sectionData->bitLookUp, 532 quantSpectrum, 533 maxValueInSfb, 534 sfbOffset, 535 blockType); 536 537 scfCount(scalefac, 538 maxValueInSfb, 539 sectionData); 540 541 542 return (sectionData->huffmanBits + sectionData->sideInfoBits + 543 sectionData->scalefacBits); 544} 545 546