📄 vlc.c
字号:
int frame_bitoffset = currStream->frame_bitoffset;
int BitstreamLengthInBytes = currStream->bitstream_length;
byte *buf = currStream->streamBuffer;
int levabs, sign;
int len = 0;
int code = 1, sb;
int numPrefix = 0;
int shift = vlc - 1;
int escape = (15 << shift) + 1;
int addbit, offset;
// read pre zeros
while (!ShowBits(buf, frame_bitoffset + numPrefix, BitstreamLengthInBytes, 1))
numPrefix++;
len = numPrefix + 1;
if (numPrefix < 15)
{
levabs = (numPrefix << shift) + 1;
// read (vlc-1) bits -> suffix
if (shift)
{
sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBytes, shift);
code = (code << (shift) )| sb;
levabs += sb;
len += (shift);
}
// read 1 bit -> sign
sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBytes, 1);
code = (code << 1)| sign;
len ++;
}
else // escape
{
addbit = numPrefix - 15;
sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBytes, (11 + addbit));
code = (code << (11 + addbit) )| sb;
len += (11 + addbit);
offset = (2048 << addbit) + escape - 2048;
levabs = sb + offset;
// read 1 bit -> sign
sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBytes, 1);
code = (code << 1)| sign;
len++;
}
sym->inf = (sign)? -levabs : levabs;
sym->len = len;
currStream->frame_bitoffset = frame_bitoffset+len;
#if TRACE
tracebits2(sym->tracestring, sym->len, code);
#endif
return 0;
}
/*!
************************************************************************
* \brief
* read Total Zeros codeword from UVLC-partition
************************************************************************
*/
int readSyntaxElement_TotalZeros(SyntaxElement *sym, Bitstream *currStream)
{
static const int lentab[TOTRUN_NUM][16] =
{
{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
{ 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
{ 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
{ 5,3,4,4,3,3,3,4,3,4,5,5,5},
{ 4,4,4,3,3,3,3,3,4,5,4,5},
{ 6,5,3,3,3,3,3,3,4,3,6},
{ 6,5,3,3,3,2,3,4,3,6},
{ 6,4,5,3,2,2,3,3,6},
{ 6,6,4,2,2,3,2,5},
{ 5,5,3,2,2,2,4},
{ 4,4,3,3,1,3},
{ 4,4,2,1,3},
{ 3,3,1,2},
{ 2,2,1},
{ 1,1},
};
static const int codtab[TOTRUN_NUM][16] =
{
{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
{7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
{5,7,6,5,4,3,4,3,2,3,2,1,1,0},
{3,7,5,4,6,5,4,3,3,2,2,1,0},
{5,4,3,7,6,5,4,3,2,1,1,0},
{1,1,7,6,5,4,3,2,1,1,0},
{1,1,5,4,3,3,2,1,1,0},
{1,1,1,3,3,2,2,1,0},
{1,0,1,3,2,1,1,1,},
{1,0,1,3,2,1,1,},
{0,1,1,2,1,3},
{0,1,1,1,1},
{0,1,1,1},
{0,1,1},
{0,1},
};
int code;
int vlcnum = sym->value1;
int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
if (retval)
{
printf("ERROR: failed to find Total Zeros\n");
exit(-1);
}
#if TRACE
tracebits2(sym->tracestring, sym->len, code);
#endif
return retval;
}
/*!
************************************************************************
* \brief
* read Total Zeros Chroma DC codeword from UVLC-partition
************************************************************************
*/
int readSyntaxElement_TotalZerosChromaDC(SyntaxElement *sym, Bitstream *currStream)
{
static const int lentab[3][TOTRUN_NUM][16] =
{
//YUV420
{{ 1,2,3,3},
{ 1,2,2},
{ 1,1}},
//YUV422
{{ 1,3,3,4,4,4,5,5},
{ 3,2,3,3,3,3,3},
{ 3,3,2,2,3,3},
{ 3,2,2,2,3},
{ 2,2,2,2},
{ 2,2,1},
{ 1,1}},
//YUV444
{{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
{ 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
{ 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
{ 5,3,4,4,3,3,3,4,3,4,5,5,5},
{ 4,4,4,3,3,3,3,3,4,5,4,5},
{ 6,5,3,3,3,3,3,3,4,3,6},
{ 6,5,3,3,3,2,3,4,3,6},
{ 6,4,5,3,2,2,3,3,6},
{ 6,6,4,2,2,3,2,5},
{ 5,5,3,2,2,2,4},
{ 4,4,3,3,1,3},
{ 4,4,2,1,3},
{ 3,3,1,2},
{ 2,2,1},
{ 1,1}}
};
static const int codtab[3][TOTRUN_NUM][16] =
{
//YUV420
{{ 1,1,1,0},
{ 1,1,0},
{ 1,0}},
//YUV422
{{ 1,2,3,2,3,1,1,0},
{ 0,1,1,4,5,6,7},
{ 0,1,1,2,6,7},
{ 6,0,1,2,7},
{ 0,1,2,3},
{ 0,1,1},
{ 0,1}},
//YUV444
{{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
{7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
{5,7,6,5,4,3,4,3,2,3,2,1,1,0},
{3,7,5,4,6,5,4,3,3,2,2,1,0},
{5,4,3,7,6,5,4,3,2,1,1,0},
{1,1,7,6,5,4,3,2,1,1,0},
{1,1,5,4,3,3,2,1,1,0},
{1,1,1,3,3,2,2,1,0},
{1,0,1,3,2,1,1,1,},
{1,0,1,3,2,1,1,},
{0,1,1,2,1,3},
{0,1,1,1,1},
{0,1,1,1},
{0,1,1},
{0,1}}
};
int code;
int yuv = active_sps->chroma_format_idc - 1;
int vlcnum = sym->value1;
int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][vlcnum][0], &codtab[yuv][vlcnum][0], 16, 1, &code);
if (retval)
{
printf("ERROR: failed to find Total Zeros\n");
exit(-1);
}
#if TRACE
tracebits2(sym->tracestring, sym->len, code);
#endif
return retval;
}
/*!
************************************************************************
* \brief
* read Run codeword from UVLC-partition
************************************************************************
*/
int readSyntaxElement_Run(SyntaxElement *sym, Bitstream *currStream)
{
static const int lentab[TOTRUN_NUM][16] =
{
{1,1},
{1,2,2},
{2,2,2,2},
{2,2,2,3,3},
{2,2,3,3,3,3},
{2,3,3,3,3,3,3},
{3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
};
static const int codtab[TOTRUN_NUM][16] =
{
{1,0},
{1,1,0},
{3,2,1,0},
{3,2,1,1,0},
{3,2,3,2,1,0},
{3,0,1,3,2,5,4},
{7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
};
int code;
int vlcnum = sym->value1;
int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
if (retval)
{
printf("ERROR: failed to find Run\n");
exit(-1);
}
#if TRACE
tracebits2(sym->tracestring, sym->len, code);
#endif
return retval;
}
/*!
************************************************************************
* \brief
* Reads bits from the bitstream buffer
*
* \param buffer
* containing VLC-coded data bits
* \param totbitoffset
* bit offset from start of partition
* \param info
* returns value of the read bits
* \param bytecount
* total bytes in bitstream
* \param numbits
* number of bits to read
*
************************************************************************
*/
int GetBits (byte buffer[],int totbitoffset,int *info, int bytecount,
int numbits)
{
register int inf;
int bitoffset = (totbitoffset & 0x07); // bit from start of byte
long byteoffset = (totbitoffset >> 3); // byte from start of buffer
int bitcounter = numbits;
static byte *curbyte;
if ((byteoffset) + ((numbits + bitoffset)>> 3) > bytecount)
return -1;
curbyte = &(buffer[byteoffset]);
bitoffset = 7 - bitoffset;
inf=0;
while (numbits--)
{
inf <<=1;
inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
//curbyte += (bitoffset >> 3) & 0x01;
curbyte -= (bitoffset >> 3);
bitoffset &= 0x07;
//curbyte += (bitoffset == 7);
}
*info = inf;
return bitcounter; // return absolute offset in bit from start of frame
}
/*!
************************************************************************
* \brief
* Reads bits from the bitstream buffer
*
* \param buffer
* buffer containing VLC-coded data bits
* \param totbitoffset
* bit offset from start of partition
* \param bytecount
* total bytes in bitstream
* \param numbits
* number of bits to read
*
************************************************************************
*/
int ShowBits (byte buffer[],int totbitoffset,int bytecount, int numbits)
{
register int inf;
int bitoffset = (totbitoffset & 0x07); // bit from start of byte
long byteoffset = (totbitoffset >> 3); // byte from start of buffer
static byte *curbyte;
if ((byteoffset) + ((numbits + bitoffset)>> 3) > bytecount)
return -1;
curbyte = &(buffer[byteoffset]);
bitoffset = 7 - bitoffset;
inf=0;
while (numbits--)
{
inf <<=1;
inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
//curbyte += (bitoffset >> 3) & 0x01;
curbyte -= (bitoffset >> 3);
bitoffset &= 0x07;
//curbyte += (bitoffset == 7);
}
return inf; // return absolute offset in bit from start of frame
}
/*!
************************************************************************
* \brief
* Reads bits from the bitstream buffer (Threshold based)
*
* \param curbyte
* current byte position in buffer
* \param bitoffset
* bit offset at current position
* \param bytecount
* total bytes in bitstream
* \param numbits
* number of bits to read
* \param code
* threshold parameter
*
************************************************************************
*/
int ShowBitsThres (byte *curbyte, int bitoffset, int bytecount, int numbits, int code)
{
register int inf;
if (((numbits + 7)>> 3) > bytecount)
return -1;
else
{
inf=0;
#if 0
while (numbits--)
{
inf <<=1;
inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
if ( inf > code)
{
return -1;
}
//curbyte += (bitoffset >> 3) & 0x01;
curbyte -= (bitoffset >> 3);
bitoffset &= 0x07;
//curbyte += (bitoffset == 7);
}
#else
while (numbits--)
{
inf <<=1;
inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
if ( (inf << numbits) > code)
{
return -1;
}
//curbyte += (bitoffset >> 3) & 0x01; // the mask does not seem to be needed since value can be 0 or -1 only.
curbyte -= (bitoffset >> 3);
bitoffset &= 0x07;
//curbyte += (bitoffset == 7);
}
#endif
return inf; // return absolute offset in bit from start of frame
}
}
/*!
************************************************************************
* \brief
* peek at the next 2 UVLC codeword from UVLC-partition to determine
* if a skipped MB is field/frame
************************************************************************
*/
int peekSyntaxElement_UVLC(SyntaxElement *sym, struct img_par *img, struct datapartition *dP)
{
Bitstream *currStream = dP->bitstream;
int frame_bitoffset = currStream->frame_bitoffset;
int BitstreamLengthInBytes = currStream->bitstream_length;
byte *buf = currStream->streamBuffer;
sym->len = GetVLCSymbol (buf, frame_bitoffset, &(sym->inf), BitstreamLengthInBytes);
if (sym->len == -1)
return -1;
frame_bitoffset += sym->len;
sym->mapping(sym->len, sym->inf, &(sym->value1), &(sym->value2));
#if TRACE
tracebits(sym->tracestring, sym->len, sym->inf, sym->value1);
#endif
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -