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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
|
/**********************************************************************
This software module was originally developed by
and edited by Texas Instruments in the course of
development of the MPEG-2 NBC/MPEG-4 Audio standard
ISO/IEC 13818-7, 14496-1,2 and 3. This software module is an
implementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio tools
as specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC gives
users of the MPEG-2 NBC/MPEG-4 Audio standards free license to this
software module or modifications thereof for use in hardware or
software products claiming conformance to the MPEG-2 NBC/ MPEG-4 Audio
standards. Those intending to use this software module in hardware or
software products are advised that this use may infringe existing
patents. The original developer of this software module and his/her
company, the subsequent editors and their companies, and ISO/IEC have
no liability for use of this software module or modifications thereof
in an implementation. Copyright is not released for non MPEG-2
NBC/MPEG-4 Audio conforming products. The original developer retains
full right to use the code for his/her own purpose, assign or donate
the code to a third party and to inhibit third party from using the
code for non MPEG-2 NBC/MPEG-4 Audio conforming products. This
copyright notice must be included in all copies or derivative works.
Copyright (c) 1997.
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "coder.h"
#include "channels.h"
#include "huff2.h"
#include "bitstream.h"
#include "util.h"
static int CountBitstream(faacEncStruct* hEncoder,
CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int numChannels);
static int WriteADTSHeader(faacEncStruct* hEncoder,
BitStream *bitStream,
int writeFlag);
static int WriteCPE(CoderInfo *coderInfoL,
CoderInfo *coderInfoR,
ChannelInfo *channelInfo,
BitStream* bitStream,
int objectType,
int writeFlag);
static int WriteSCE(CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int objectType,
int writeFlag);
static int WriteLFE(CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int objectType,
int writeFlag);
static int WriteICSInfo(CoderInfo *coderInfo,
BitStream *bitStream,
int objectType,
int common_window,
int writeFlag);
static int WriteICS(CoderInfo *coderInfo,
BitStream *bitStream,
int commonWindow,
int objectType,
int writeFlag);
static int WritePulseData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag);
static int WriteTNSData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag);
static int WriteGainControlData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag);
static int WriteSpectralData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag);
static int WriteAACFillBits(BitStream* bitStream,
int numBits,
int writeFlag);
static int FindGroupingBits(CoderInfo *coderInfo);
static long BufferNumBit(BitStream *bitStream);
static int ByteAlign(BitStream* bitStream,
int writeFlag, int bitsSoFar);
static int WriteFAACStr(BitStream *bitStream, char *version, int write)
{
int i;
char str[200];
int len, padbits, count;
int bitcnt;
sprintf(str, "libfaac %s", version);
len = strlen(str) + 1;
padbits = (8 - ((bitStream->numBit + 7) % 8)) % 8;
count = len + 3;
bitcnt = LEN_SE_ID + 4 + ((count < 15) ? 0 : 8) + count * 8;
if (!write)
return bitcnt;
PutBit(bitStream, ID_FIL, LEN_SE_ID);
if (count < 15)
{
PutBit(bitStream, count, 4);
}
else
{
PutBit(bitStream, 15, 4);
PutBit(bitStream, count - 14, 8);
}
PutBit(bitStream, 0, padbits);
PutBit(bitStream, 0, 8);
PutBit(bitStream, 0, 8); // just in case
for (i = 0; i < len; i++)
PutBit(bitStream, str[i], 8);
PutBit(bitStream, 0, 8 - padbits);
return bitcnt;
}
int WriteBitstream(faacEncStruct* hEncoder,
CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int numChannel)
{
int channel;
int bits = 0;
int bitsLeftAfterFill, numFillBits;
if (CountBitstream(hEncoder, coderInfo, channelInfo, bitStream, numChannel) < 0)
return -1;
if(hEncoder->config.outputFormat == 1){
bits += WriteADTSHeader(hEncoder, bitStream, 1);
}else{
bits = 0; // compilier will remove it, byt anyone will see that current size of bitstream is 0
}
/* sur: faad2 complains about scalefactor error if we are writing FAAC String */
if (hEncoder->frameNum == 4)
WriteFAACStr(bitStream, hEncoder->config.name, 1);
for (channel = 0; channel < numChannel; channel++) {
if (channelInfo[channel].present) {
/* Write out a single_channel_element */
if (!channelInfo[channel].cpe) {
if (channelInfo[channel].lfe) {
/* Write out lfe */
bits += WriteLFE(&coderInfo[channel],
&channelInfo[channel],
bitStream,
hEncoder->config.aacObjectType,
1);
} else {
/* Write out sce */
bits += WriteSCE(&coderInfo[channel],
&channelInfo[channel],
bitStream,
hEncoder->config.aacObjectType,
1);
}
} else {
if (channelInfo[channel].ch_is_left) {
/* Write out cpe */
bits += WriteCPE(&coderInfo[channel],
&coderInfo[channelInfo[channel].paired_ch],
&channelInfo[channel],
bitStream,
hEncoder->config.aacObjectType,
1);
}
}
}
}
/* Compute how many fill bits are needed to avoid overflowing bit reservoir */
/* Save room for ID_END terminator */
if (bits < (8 - LEN_SE_ID) ) {
numFillBits = 8 - LEN_SE_ID - bits;
} else {
numFillBits = 0;
}
/* Write AAC fill_elements, smallest fill element is 7 bits. */
/* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
numFillBits += 6;
bitsLeftAfterFill = WriteAACFillBits(bitStream, numFillBits, 1);
bits += (numFillBits - bitsLeftAfterFill);
/* Write ID_END terminator */
bits += LEN_SE_ID;
PutBit(bitStream, ID_END, LEN_SE_ID);
/* Now byte align the bitstream */
/*
* This byte_alignment() is correct for both MPEG2 and MPEG4, although
* in MPEG4 the byte_alignment() is officially done before the new frame
* instead of at the end. But this is basically the same.
*/
bits += ByteAlign(bitStream, 1, bits);
return bits;
}
static int CountBitstream(faacEncStruct* hEncoder,
CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int numChannel)
{
int channel;
int bits = 0;
int bitsLeftAfterFill, numFillBits;
if(hEncoder->config.outputFormat == 1){
bits += WriteADTSHeader(hEncoder, bitStream, 0);
}else{
bits = 0; // compilier will remove it, byt anyone will see that current size of bitstream is 0
}
/* sur: faad2 complains about scalefactor error if we are writing FAAC String */
if (hEncoder->frameNum == 4)
bits += WriteFAACStr(bitStream, hEncoder->config.name, 0);
for (channel = 0; channel < numChannel; channel++) {
if (channelInfo[channel].present) {
/* Write out a single_channel_element */
if (!channelInfo[channel].cpe) {
if (channelInfo[channel].lfe) {
/* Write out lfe */
bits += WriteLFE(&coderInfo[channel],
&channelInfo[channel],
bitStream,
hEncoder->config.aacObjectType,
0);
} else {
/* Write out sce */
bits += WriteSCE(&coderInfo[channel],
&channelInfo[channel],
bitStream,
hEncoder->config.aacObjectType,
0);
}
} else {
if (channelInfo[channel].ch_is_left) {
/* Write out cpe */
bits += WriteCPE(&coderInfo[channel],
&coderInfo[channelInfo[channel].paired_ch],
&channelInfo[channel],
bitStream,
hEncoder->config.aacObjectType,
0);
}
}
}
}
/* Compute how many fill bits are needed to avoid overflowing bit reservoir */
/* Save room for ID_END terminator */
if (bits < (8 - LEN_SE_ID) ) {
numFillBits = 8 - LEN_SE_ID - bits;
} else {
numFillBits = 0;
}
/* Write AAC fill_elements, smallest fill element is 7 bits. */
/* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
numFillBits += 6;
bitsLeftAfterFill = WriteAACFillBits(bitStream, numFillBits, 0);
bits += (numFillBits - bitsLeftAfterFill);
/* Write ID_END terminator */
bits += LEN_SE_ID;
/* Now byte align the bitstream */
bits += ByteAlign(bitStream, 0, bits);
hEncoder->usedBytes = bit2byte(bits);
if (hEncoder->usedBytes > bitStream->size)
{
fprintf(stderr, "frame buffer overrun\n");
return -1;
}
if (hEncoder->usedBytes >= ADTS_FRAMESIZE)
{
fprintf(stderr, "frame size limit exceeded\n");
return -1;
}
return bits;
}
static int WriteADTSHeader(faacEncStruct* hEncoder,
BitStream *bitStream,
int writeFlag)
{
int bits = 56;
if (writeFlag) {
/* Fixed ADTS header */
PutBit(bitStream, 0xFFFF, 12); /* 12 bit Syncword */
PutBit(bitStream, hEncoder->config.mpegVersion, 1); /* ID == 0 for MPEG4 AAC, 1 for MPEG2 AAC */
PutBit(bitStream, 0, 2); /* layer == 0 */
PutBit(bitStream, 1, 1); /* protection absent */
PutBit(bitStream, hEncoder->config.aacObjectType - 1, 2); /* profile */
PutBit(bitStream, hEncoder->sampleRateIdx, 4); /* sampling rate */
PutBit(bitStream, 0, 1); /* private bit */
PutBit(bitStream, hEncoder->numChannels, 3); /* ch. config (must be > 0) */
/* simply using numChannels only works for
6 channels or less, else a channel
configuration should be written */
PutBit(bitStream, 0, 1); /* original/copy */
PutBit(bitStream, 0, 1); /* home */
#if 0 // Removed in corrigendum 14496-3:2002
if (hEncoder->config.mpegVersion == 0)
PutBit(bitStream, 0, 2); /* emphasis */
#endif
/* Variable ADTS header */
PutBit(bitStream, 0, 1); /* copyr. id. bit */
PutBit(bitStream, 0, 1); /* copyr. id. start */
PutBit(bitStream, hEncoder->usedBytes, 13);
PutBit(bitStream, 0x7FF, 11); /* buffer fullness (0x7FF for VBR) */
PutBit(bitStream, 0, 2); /* raw data blocks (0+1=1) */
}
/*
* MPEG2 says byte_aligment() here, but ADTS always is multiple of 8 bits
* MPEG4 has no byte_alignment() here
*/
/*
if (hEncoder->config.mpegVersion == 1)
bits += ByteAlign(bitStream, writeFlag);
*/
#if 0 // Removed in corrigendum 14496-3:2002
if (hEncoder->config.mpegVersion == 0)
bits += 2; /* emphasis */
#endif
return bits;
}
static int WriteCPE(CoderInfo *coderInfoL,
CoderInfo *coderInfoR,
ChannelInfo *channelInfo,
BitStream* bitStream,
int objectType,
int writeFlag)
{
int bits = 0;
if (writeFlag) {
/* write ID_CPE, single_element_channel() identifier */
PutBit(bitStream, ID_CPE, LEN_SE_ID);
/* write the element_identifier_tag */
PutBit(bitStream, channelInfo->tag, LEN_TAG);
/* common_window? */
PutBit(bitStream, channelInfo->common_window, LEN_COM_WIN);
}
bits += LEN_SE_ID;
bits += LEN_TAG;
bits += LEN_COM_WIN;
/* if common_window, write ics_info */
if (channelInfo->common_window) {
int numWindows, maxSfb;
bits += WriteICSInfo(coderInfoL, bitStream, objectType, channelInfo->common_window, writeFlag);
numWindows = coderInfoL->groups.n;
maxSfb = coderInfoL->sfbn;
if (writeFlag) {
PutBit(bitStream, channelInfo->msInfo.is_present, LEN_MASK_PRES);
if (channelInfo->msInfo.is_present == 1) {
int g;
int b;
for (g=0;g<numWindows;g++) {
for (b=0;b<maxSfb;b++) {
PutBit(bitStream, channelInfo->msInfo.ms_used[g*maxSfb+b], LEN_MASK);
}
}
}
}
bits += LEN_MASK_PRES;
if (channelInfo->msInfo.is_present == 1)
bits += (numWindows*maxSfb*LEN_MASK);
}
/* Write individual_channel_stream elements */
bits += WriteICS(coderInfoL, bitStream, channelInfo->common_window, objectType, writeFlag);
bits += WriteICS(coderInfoR, bitStream, channelInfo->common_window, objectType, writeFlag);
return bits;
}
static int WriteSCE(CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int objectType,
int writeFlag)
{
int bits = 0;
if (writeFlag) {
/* write Single Element Channel (SCE) identifier */
PutBit(bitStream, ID_SCE, LEN_SE_ID);
/* write the element identifier tag */
PutBit(bitStream, channelInfo->tag, LEN_TAG);
}
bits += LEN_SE_ID;
bits += LEN_TAG;
/* Write an Individual Channel Stream element */
bits += WriteICS(coderInfo, bitStream, 0, objectType, writeFlag);
return bits;
}
static int WriteLFE(CoderInfo *coderInfo,
ChannelInfo *channelInfo,
BitStream *bitStream,
int objectType,
int writeFlag)
{
int bits = 0;
if (writeFlag) {
/* write ID_LFE, lfe_element_channel() identifier */
PutBit(bitStream, ID_LFE, LEN_SE_ID);
/* write the element_identifier_tag */
PutBit(bitStream, channelInfo->tag, LEN_TAG);
}
bits += LEN_SE_ID;
bits += LEN_TAG;
/* Write an individual_channel_stream element */
bits += WriteICS(coderInfo, bitStream, 0, objectType, writeFlag);
return bits;
}
static int WriteICSInfo(CoderInfo *coderInfo,
BitStream *bitStream,
int objectType,
int common_window,
int writeFlag)
{
int grouping_bits;
int bits = 0;
if (writeFlag) {
/* write out ics_info() information */
PutBit(bitStream, 0, LEN_ICS_RESERV); /* reserved Bit*/
/* Write out window sequence */
PutBit(bitStream, coderInfo->block_type, LEN_WIN_SEQ); /* block type */
/* Write out window shape */
PutBit(bitStream, coderInfo->window_shape, LEN_WIN_SH); /* window shape */
}
bits += LEN_ICS_RESERV;
bits += LEN_WIN_SEQ;
bits += LEN_WIN_SH;
/* For short windows, write out max_sfb and scale_factor_grouping */
if (coderInfo->block_type == ONLY_SHORT_WINDOW){
if (writeFlag) {
PutBit(bitStream, coderInfo->sfbn, LEN_MAX_SFBS);
grouping_bits = FindGroupingBits(coderInfo);
PutBit(bitStream, grouping_bits, MAX_SHORT_WINDOWS - 1); /* the grouping bits */
}
bits += LEN_MAX_SFBS;
bits += MAX_SHORT_WINDOWS - 1;
} else { /* Otherwise, write out max_sfb and predictor data */
if (writeFlag) {
PutBit(bitStream, coderInfo->sfbn, LEN_MAX_SFBL);
}
bits += LEN_MAX_SFBL;
bits++;
if (writeFlag)
PutBit(bitStream, 0, LEN_PRED_PRES); /* predictor_data_present */
}
return bits;
}
static int WriteICS(CoderInfo *coderInfo,
BitStream *bitStream,
int commonWindow,
int objectType,
int writeFlag)
{
/* this function writes out an individual_channel_stream to the bitstream and */
/* returns the number of bits written to the bitstream */
int bits = 0;
/* Write the 8-bit global_gain */
if (writeFlag)
PutBit(bitStream, coderInfo->global_gain, LEN_GLOB_GAIN);
bits += LEN_GLOB_GAIN;
/* Write ics information */
if (!commonWindow) {
bits += WriteICSInfo(coderInfo, bitStream, objectType, commonWindow, writeFlag);
}
bits += writebooks(coderInfo, bitStream, writeFlag);
bits += writesf(coderInfo, bitStream, writeFlag);
bits += WritePulseData(coderInfo, bitStream, writeFlag);
bits += WriteTNSData(coderInfo, bitStream, writeFlag);
bits += WriteGainControlData(coderInfo, bitStream, writeFlag);
bits += WriteSpectralData(coderInfo, bitStream, writeFlag);
/* Return number of bits */
return bits;
}
static int WritePulseData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag)
{
int bits = 0;
if (writeFlag) {
PutBit(bitStream, 0, LEN_PULSE_PRES); /* no pulse_data_present */
}
bits += LEN_PULSE_PRES;
return bits;
}
static int WriteTNSData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag)
{
int bits = 0;
int numWindows;
int len_tns_nfilt;
int len_tns_length;
int len_tns_order;
int filtNumber;
int resInBits;
int bitsToTransmit;
unsigned long unsignedIndex;
int w;
TnsInfo* tnsInfoPtr = &coderInfo->tnsInfo;
if (writeFlag) {
PutBit(bitStream,tnsInfoPtr->tnsDataPresent,LEN_TNS_PRES);
}
bits += LEN_TNS_PRES;
/* If TNS is not present, bail */
if (!tnsInfoPtr->tnsDataPresent) {
return bits;
}
/* Set window-dependent TNS parameters */
if (coderInfo->block_type == ONLY_SHORT_WINDOW) {
numWindows = MAX_SHORT_WINDOWS;
len_tns_nfilt = LEN_TNS_NFILTS;
len_tns_length = LEN_TNS_LENGTHS;
len_tns_order = LEN_TNS_ORDERS;
}
else {
numWindows = 1;
len_tns_nfilt = LEN_TNS_NFILTL;
len_tns_length = LEN_TNS_LENGTHL;
len_tns_order = LEN_TNS_ORDERL;
}
/* Write TNS data */
bits += (numWindows * len_tns_nfilt);
for (w=0;w<numWindows;w++) {
TnsWindowData* windowDataPtr = &tnsInfoPtr->windowData[w];
int numFilters = windowDataPtr->numFilters;
if (writeFlag) {
PutBit(bitStream,numFilters,len_tns_nfilt); /* n_filt[] = 0 */
}
if (numFilters) {
bits += LEN_TNS_COEFF_RES;
resInBits = windowDataPtr->coefResolution;
if (writeFlag) {
PutBit(bitStream,resInBits-DEF_TNS_RES_OFFSET,LEN_TNS_COEFF_RES);
}
bits += numFilters * (len_tns_length+len_tns_order);
for (filtNumber=0;filtNumber<numFilters;filtNumber++) {
TnsFilterData* tnsFilterPtr=&windowDataPtr->tnsFilter[filtNumber];
int order = tnsFilterPtr->order;
if (writeFlag) {
PutBit(bitStream,tnsFilterPtr->length,len_tns_length);
PutBit(bitStream,order,len_tns_order);
}
if (order) {
bits += (LEN_TNS_DIRECTION + LEN_TNS_COMPRESS);
if (writeFlag) {
PutBit(bitStream,tnsFilterPtr->direction,LEN_TNS_DIRECTION);
PutBit(bitStream,tnsFilterPtr->coefCompress,LEN_TNS_COMPRESS);
}
bitsToTransmit = resInBits - tnsFilterPtr->coefCompress;
bits += order * bitsToTransmit;
if (writeFlag) {
int i;
for (i=1;i<=order;i++) {
unsignedIndex = (unsigned long) (tnsFilterPtr->index[i])&(~(~0<<bitsToTransmit));
PutBit(bitStream,unsignedIndex,bitsToTransmit);
}
}
}
}
}
}
return bits;
}
static int WriteGainControlData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag)
{
int bits = 0;
if (writeFlag) {
PutBit(bitStream, 0, LEN_GAIN_PRES);
}
bits += LEN_GAIN_PRES;
return bits;
}
static int WriteSpectralData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag)
{
int i, bits = 0;
if (writeFlag) {
for(i = 0; i < coderInfo->datacnt; i++) {
int data = coderInfo->s[i].data;
int len = coderInfo->s[i].len;
if (len > 0) {
PutBit(bitStream, data, len);
bits += len;
}
}
} else {
for(i = 0; i < coderInfo->datacnt; i++) {
bits += coderInfo->s[i].len;
}
}
return bits;
}
static int WriteAACFillBits(BitStream* bitStream,
int numBits,
int writeFlag)
{
int numberOfBitsLeft = numBits;
/* Need at least (LEN_SE_ID + LEN_F_CNT) bits for a fill_element */
int minNumberOfBits = LEN_SE_ID + LEN_F_CNT;
while (numberOfBitsLeft >= minNumberOfBits)
{
int numberOfBytes;
int maxCount;
if (writeFlag) {
PutBit(bitStream, ID_FIL, LEN_SE_ID); /* Write fill_element ID */
}
numberOfBitsLeft -= minNumberOfBits; /* Subtract for ID,count */
numberOfBytes = (int)(numberOfBitsLeft/LEN_BYTE);
maxCount = (1<<LEN_F_CNT) - 1; /* Max count without escaping */
/* if we have less than maxCount bytes, write them now */
if (numberOfBytes < maxCount) {
int i;
if (writeFlag) {
PutBit(bitStream, numberOfBytes, LEN_F_CNT);
for (i = 0; i < numberOfBytes; i++) {
PutBit(bitStream, 0, LEN_BYTE);
}
}
/* otherwise, we need to write an escape count */
}
else {
int maxEscapeCount, maxNumberOfBytes, escCount;
int i;
if (writeFlag) {
PutBit(bitStream, maxCount, LEN_F_CNT);
}
maxEscapeCount = (1<<LEN_BYTE) - 1; /* Max escape count */
maxNumberOfBytes = maxCount + maxEscapeCount;
numberOfBytes = (numberOfBytes > maxNumberOfBytes ) ? (maxNumberOfBytes) : (numberOfBytes);
escCount = numberOfBytes - maxCount;
if (writeFlag) {
PutBit(bitStream, escCount, LEN_BYTE);
for (i = 0; i < numberOfBytes-1; i++) {
PutBit(bitStream, 0, LEN_BYTE);
}
}
}
numberOfBitsLeft -= LEN_BYTE*numberOfBytes;
}
return numberOfBitsLeft;
}
static int FindGroupingBits(CoderInfo *coderInfo)
{
/* This function inputs the grouping information and outputs the seven bit
'grouping_bits' field that the AAC decoder expects. */
int grouping_bits = 0;
int tmp[8];
int i, j;
int index = 0;
for(i = 0; i < coderInfo->groups.n; i++){
for (j = 0; j < coderInfo->groups.len[i]; j++){
tmp[index++] = i;
}
}
for(i = 1; i < 8; i++){
grouping_bits = grouping_bits << 1;
if(tmp[i] == tmp[i-1]) {
grouping_bits++;
}
}
return grouping_bits;
}
/* size in bytes! */
BitStream *OpenBitStream(int size, unsigned char *buffer)
{
BitStream *bitStream;
bitStream = AllocMemory(sizeof(BitStream));
bitStream->size = size;
bitStream->numBit = 0;
bitStream->currentBit = 0;
bitStream->data = buffer;
SetMemory(bitStream->data, 0, size);
return bitStream;
}
int CloseBitStream(BitStream *bitStream)
{
int bytes = bit2byte(bitStream->numBit);
FreeMemory(bitStream);
return bytes;
}
static long BufferNumBit(BitStream *bitStream)
{
return bitStream->numBit;
}
int PutBit(BitStream *bitStream,
unsigned long data,
int numBit)
{
/* write bits in packets according to buffer byte boundaries */
if (numBit == 0)
return 0;
/* Hoist bitstream state for faster access */
unsigned int currentBit = (unsigned int)bitStream->currentBit;
unsigned int bitOffset = currentBit & 7;
unsigned char *ptr = bitStream->data + (currentBit >> 3);
/* Update bitstream state immediately */
bitStream->currentBit += numBit;
bitStream->numBit = bitStream->currentBit;
/* Mask input data to ensure no extra bits are set */
data &= (1UL << numBit) - 1;
/* Fast path: bit write fits within the current byte */
if (bitOffset + numBit <= 8) {
if (bitOffset == 0) *ptr = 0;
*ptr |= (unsigned char)(data << (8 - bitOffset - numBit));
} else {
/* General case: multi-byte write */
/* Handle first partial byte */
int firstBits = 8 - bitOffset;
if (bitOffset == 0) *ptr = 0;
*ptr++ |= (unsigned char)(data >> (numBit - firstBits));
numBit -= firstBits;
/* Handle full bytes */
while (numBit >= 8) {
*ptr++ = (unsigned char)((data >> (numBit - 8)) & 0xFF);
numBit -= 8;
}
/* Handle remaining bits in last byte */
if (numBit > 0) {
*ptr = (unsigned char)((data & ((1UL << numBit) - 1)) << (8 - numBit));
}
}
return 0;
}
static int ByteAlign(BitStream *bitStream, int writeFlag, int bitsSoFar)
{
int len, i,j;
if (writeFlag)
{
len = BufferNumBit(bitStream);
} else {
len = bitsSoFar;
}
j = (8 - (len%8))%8;
if ((len % 8) == 0) j = 0;
if (writeFlag) {
for( i=0; i<j; i++ ) {
PutBit(bitStream, 0, 1);
}
}
return j;
}
|