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