/**********************************************************************
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 WriteByte(BitStream *bitStream,
unsigned long data,
int numBit);
static int ByteAlign(BitStream* bitStream,
int writeFlag, int bitsSoFar);
#ifdef DRM
static int PutBitHcr(BitStream *bitStream,
unsigned long curpos,
unsigned long data,
int numBit);
static int rewind_word(int W, int len);
static int WriteReorderedSpectralData(CoderInfo *coderInfo,
BitStream *bitStream,
int writeFlag);
static void calc_CRC(BitStream *bitStream, int len);
#endif
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 */
#ifndef DRM
if (hEncoder->frameNum == 4)
WriteFAACStr(bitStream, hEncoder->config.name, 1);
#endif
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 += LE