📄 vlc.c
字号:
sym->value1 = (sym->len == 1) ? -1 : sym->inf;
#if TRACE
tracebits2(sym->tracestring, sym->len, sym->value1);
#endif
return 1;
}
int GetVLCSymbol_IntraMode (byte buffer[],int totbitoffset,int *info, int bytecount)
{
int byteoffset = (totbitoffset >> 3); // byte from start of buffer
int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
byte *cur_byte = &(buffer[byteoffset]);
int ctr_bit = (*cur_byte & (0x01 << bitoffset)); // control bit for current bit posision
//First bit
if (ctr_bit)
{
*info = 0;
return 1;
}
if (byteoffset >= bytecount)
{
return -1;
}
else
{
int inf = (*(cur_byte) << 8) + *(cur_byte + 1);
inf <<= (sizeof(byte) * 8) - bitoffset;
inf = inf & 0xFFFF;
inf >>= (sizeof(byte) * 8) * 2 - 3;
*info = inf;
return 4; // return absolute offset in bit from start of frame
}
}
/*!
************************************************************************
* \brief
* test if bit buffer contains only stop bit
*
* \param buffer
* buffer containing VLC-coded data bits
* \param totbitoffset
* bit offset from start of partition
* \param bytecount
* buffer length
* \return
* true if more bits available
************************************************************************
*/
int more_rbsp_data (byte buffer[],int totbitoffset,int bytecount)
{
int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
long byteoffset = (totbitoffset >> 3); // byte from start of buffer
byte *cur_byte = &(buffer[byteoffset]);
int ctr_bit = 0; // control bit for current bit posision
int cnt = 0;
//assert (byteoffset<bytecount);
// there is more until we're in the last byte
if (byteoffset < (bytecount - 1)) return TRUE;
// read one bit
ctr_bit = ((*cur_byte)>> (bitoffset--)) & 0x01;
// a stop bit has to be one
if (ctr_bit==0) return TRUE;
while (bitoffset>=0 && !cnt)
{
cnt |= ((*cur_byte)>> (bitoffset--)) & 0x01; // set up control bit
}
return (cnt);
}
/*!
************************************************************************
* \brief
* Check if there are symbols for the next MB
************************************************************************
*/
int uvlc_startcode_follows(Slice *currSlice, int dummy)
{
byte dp_Nr = assignSE2partition[currSlice->dp_mode][SE_MBTYPE];
DataPartition *dP = &(currSlice->partArr[dp_Nr]);
Bitstream *currStream = dP->bitstream;
byte *buf = currStream->streamBuffer;
return (!(more_rbsp_data(buf, currStream->frame_bitoffset,currStream->bitstream_length)));
}
/*!
************************************************************************
* \brief
* read one exp-golomb VLC symbol
*
* \param buffer
* containing VLC-coded data bits
* \param totbitoffset
* bit offset from start of partition
* \param info
* returns the value of the symbol
* \param bytecount
* buffer length
* \return
* bits read
************************************************************************
*/
int GetVLCSymbol (byte buffer[],int totbitoffset,int *info, int bytecount)
{
long byteoffset = (totbitoffset >> 3); // byte from start of buffer
int bitoffset = (7 - (totbitoffset & 0x07)); // bit from start of byte
int bitcounter = 1;
int len = 0;
byte *cur_byte = &(buffer[byteoffset]);
int ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01; // control bit for current bit posision
while (ctr_bit == 0)
{ // find leading 1 bit
len++;
bitcounter++;
bitoffset--;
bitoffset &= 0x07;
cur_byte += (bitoffset == 7);
byteoffset+= (bitoffset == 7);
ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01;
}
if (byteoffset + ((len + 7) >> 3) > bytecount)
return -1;
else
{
// make infoword
int inf = 0; // shortest possible code is 1, then info is always 0
while (len--)
{
bitoffset --;
bitoffset &= 0x07;
cur_byte += (bitoffset == 7);
bitcounter++;
inf <<= 1;
inf |= ((*cur_byte) >> (bitoffset)) & 0x01;
}
*info = inf;
return bitcounter; // return absolute offset in bit from start of frame
}
}
/*!
************************************************************************
* \brief
* Reads bits from the bitstream buffer (Threshold based)
*
* \param inf
* bytes to extract numbits from with bitoffset already applied
* \param numbits
* number of bits to read
*
************************************************************************
*/
//static inline int ShowBitsThres (int inf, int bitcount, int numbits)
static inline int ShowBitsThres (int inf, int numbits)
{
return ((inf) >> ((sizeof(byte) * 24) - (numbits)));
/*
if ((numbits + 7) > bitcount)
{
return -1;
}
else
{
//Worst case scenario is that we will need to traverse 3 bytes
inf >>= (sizeof(byte)*8)*3 - numbits;
}
return inf; //Will be a small unsigned integer so will not need any conversion when returning as int
*/
}
/*!
************************************************************************
* \brief
* code from bitstream (2d tables)
************************************************************************
*/
int code_from_bitstream_2d(SyntaxElement *sym,
Bitstream *currStream,
const byte *lentab,
const byte *codtab,
int tabwidth,
int tabheight,
int *code)
{
int i, j;
const byte *len = &lentab[0], *cod = &codtab[0];
int *frame_bitoffset = &currStream->frame_bitoffset;
byte *buf = &currStream->streamBuffer[*frame_bitoffset >> 3];
//Apply bitoffset to three bytes (maximum that may be traversed by ShowBitsThres)
unsigned int inf = ((*buf) << 16) + (*(buf + 1) << 8) + *(buf + 2); //Even at the end of a stream we will still be pulling out of allocated memory as alloc is done by MAX_CODED_FRAME_SIZE
inf <<= (*frame_bitoffset & 0x07); //Offset is constant so apply before extracting different numbers of bits
inf &= 0xFFFFFF; //Arithmetic shift so wipe any sign which may be extended inside ShowBitsThres
// this VLC decoding method is not optimized for speed
for (j = 0; j < tabheight; j++)
{
for (i = 0; i < tabwidth; i++)
{
if ((*len == 0) || (ShowBitsThres(inf, (int) *len) != *cod))
{
len++;
cod++;
}
else
{
sym->len = *len;
*frame_bitoffset += *len; // move bitstream pointer
*code = *cod;
sym->value1 = i;
sym->value2 = j;
return 0; // found code and return
}
}
}
return -1; // failed to find code
}
/*!
************************************************************************
* \brief
* read FLC codeword from UVLC-partition
************************************************************************
*/
int readSyntaxElement_FLC(SyntaxElement *sym, Bitstream *currStream)
{
int BitstreamLengthInBits = (currStream->bitstream_length << 3) + 7;
if ((GetBits(currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), BitstreamLengthInBits, sym->len)) < 0)
return -1;
sym->value1 = sym->inf;
currStream->frame_bitoffset += sym->len; // move bitstream pointer
#if TRACE
tracebits2(sym->tracestring, sym->len, sym->inf);
#endif
return 1;
}
/*!
************************************************************************
* \brief
* read NumCoeff/TrailingOnes codeword from UVLC-partition
************************************************************************
*/
int readSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *sym,
Bitstream *currStream,
char *type)
{
int frame_bitoffset = currStream->frame_bitoffset;
int BitstreamLengthInBytes = currStream->bitstream_length;
int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
byte *buf = currStream->streamBuffer;
static const byte lentab[3][4][17] =
{
{ // 0702
{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
{ 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
{ 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
{ 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
},
{
{ 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
{ 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
{ 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
{ 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
},
{
{ 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
{ 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
{ 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
{ 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
},
};
static const byte codtab[3][4][17] =
{
{
{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
{ 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
{ 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
{ 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
},
{
{ 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
{ 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
{ 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
{ 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
},
{
{15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
{ 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
{ 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
{ 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
},
};
int retval = 0, code;
int vlcnum = sym->value1;
// vlcnum is the index of Table used to code coeff_token
// vlcnum==3 means (8<=nC) which uses 6bit FLC
if (vlcnum == 3)
{
// read 6 bit FLC
//code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBytes, 6);
code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 6);
currStream->frame_bitoffset += 6;
sym->value2 = (code & 3);
sym->value1 = (code >> 2);
if (!sym->value1 && sym->value2 == 3)
{
// #c = 0, #t1 = 3 => #c = 0
sym->value2 = 0;
}
else
sym->value1++;
sym->len = 6;
}
else
{
//retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0][0], &codtab[vlcnum][0][0], 17, 4, &code);
retval = code_from_bitstream_2d(sym, currStream, lentab[vlcnum][0], codtab[vlcnum][0], 17, 4, &code);
if (retval)
{
printf("ERROR: failed to find NumCoeff/TrailingOnes\n");
exit(-1);
}
}
#if TRACE
snprintf(sym->tracestring, TRACESTRING_SIZE, "%s # c & tr.1s vlc=%d #c=%d #t1=%d",
type, vlcnum, sym->value1, sym->value2);
tracebits2(sym->tracestring, sym->len, code);
#endif
return retval;
}
/*!
************************************************************************
* \brief
* read NumCoeff/TrailingOnes codeword from UVLC-partition ChromaDC
************************************************************************
*/
int readSyntaxElement_NumCoeffTrailingOnesChromaDC(SyntaxElement *sym, Bitstream *currStream)
{
static const byte lentab[3][4][17] =
{
//YUV420
{{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 6, 7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7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
{ 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
{ 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
{ 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
};
int code;
int yuv = active_sps->chroma_format_idc - 1;
int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][0][0], &codtab[yuv][0][0], 17, 4, &code);
if (retval)
{
printf("ERROR: failed to find NumCoeff/TrailingOnes ChromaDC\n");
exit(-1);
}
#if TRACE
snprintf(sym->tracestring, TRACESTRING_SIZE, "ChrDC # c & tr.1s #c=%d #t1=%d",
sym->value1, sym->value2);
tracebits2(sym->tracestring, sym->len, code);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -