📄 vlc.c
字号:
#include "ippdefs.h"
#include "math.h"
#include "vlc.h"
#include "ippvc.h"
extern const Ipp8u mZigZagScan[64];
extern const Ipp8u mAlternateHorScan[64];
extern const Ipp8u mAlternateVerScan[64];
extern void PutBits(Ipp32u val, int n);
//added by bxd Apr.22
//void xhEncode_IntraDCVLC_MPEG4_16s1u( Ipp16s qDCCoef,
// Ipp8u** ppBitStream,
// int* pBitOffset,
// IppVideoComponent videoComp
// )
void xhEncode_IntraDCVLC_MPEG4_16s1u( Ipp16s qDCCoef,
IppVideoComponent videoComp
)
{
int val,n,i,temp;//i and temp is add by bxd .because pow() have some problem;
Ipp32u binStrLen = 0;
// Ipp8u* mPtr = *ppBitStream;
// int mBitOff = *pBitOffset;
temp=1;//add by bxd
if (qDCCoef < -255)
qDCCoef = -255;
if(qDCCoef > 255)
qDCCoef = 255;
val = abs(qDCCoef);
if(val == 1)
{
binStrLen = 1;
}
else if(val >= 2 && val <= 3)
{
binStrLen = 2;
}
else if(val >= 4 && val <= 7)
{
binStrLen = 3;
}
else if(val >= 8 && val <= 15)
{
binStrLen = 4;
}
else if(val >= 16 && val <= 31)
{
binStrLen = 5;
}
else if(val >= 32 && val <= 63)
{
binStrLen = 6;
}
else if(val >= 64 && val <= 127)
{
binStrLen = 7;
}
else if(val >= 128 && val <= 255)
{
binStrLen = 8;
}
if (videoComp == 0 || videoComp == 2) {//luma DC
PutBits(mVLC_DCSIZE_TB13[binStrLen].code, mVLC_DCSIZE_TB13[binStrLen].len);
/* val = mVLC_DCSIZE_TB13[binStrLen].code;
n = mVLC_DCSIZE_TB13[binStrLen].len;
val <<= 32 - n;
if (mBitOff == 0) {
mPtr[0] = (Ipp8u)(val >> 24);
if (n > 8) {
mPtr[1] = (Ipp8u)(val >> 16);
if (n > 16) {
mPtr[2] = (Ipp8u)(val >> 8);
if (n > 24) {
mPtr[3] = (Ipp8u)(val);
}
}
}
} else {
mPtr[0] = (Ipp8u)((mPtr[0] & (0xFF << (8 - mBitOff))) | (Ipp8u)(val >> (24 + mBitOff)));
if (n > 8 - mBitOff) {
val <<= 8 - mBitOff;
mPtr[1] = (Ipp8u)(val >> 24);
if (n > 16 - mBitOff) {
mPtr[2] = (Ipp8u)(val >> 16);
if (n > 24 - mBitOff) {
mPtr[3] = (Ipp8u)(val >> 8);
if (n > 32 - mBitOff) {
mPtr[4] = (Ipp8u)val;
}
}
}
}
}
mPtr += (mBitOff + n) >> 3;
*pBitOffset = (mBitOff + n) & 7;
*/
}
else {//chroma DC
PutBits(mVLC_DCSIZE_TB14[binStrLen].code, mVLC_DCSIZE_TB14[binStrLen].len);
/* val = mVLC_DCSIZE_TB14[binStrLen].code;
n = mVLC_DCSIZE_TB14[binStrLen].len;
val <<= 32 - n;
if (mBitOff == 0) {
mPtr[0] = (Ipp8u)(val >> 24);
if (n > 8) {
mPtr[1] = (Ipp8u)(val >> 16);
if (n > 16) {
mPtr[2] = (Ipp8u)(val >> 8);
if (n > 24) {
mPtr[3] = (Ipp8u)(val);
}
}
}
} else {
mPtr[0] = (Ipp8u)((mPtr[0] & (0xFF << (8 - mBitOff))) | (Ipp8u)(val >> (24 + mBitOff)));
if (n > 8 - mBitOff) {
val <<= 8 - mBitOff;
mPtr[1] = (Ipp8u)(val >> 24);
if (n > 16 - mBitOff) {
mPtr[2] = (Ipp8u)(val >> 16);
if (n > 24 - mBitOff) {
mPtr[3] = (Ipp8u)(val >> 8);
if (n > 32 - mBitOff) {
mPtr[4] = (Ipp8u)val;
}
}
}
}
}
mPtr += (mBitOff + n) >> 3;
*pBitOffset = (mBitOff + n) & 7;
*/
}
if(qDCCoef >= 0){
val = qDCCoef;
}
else{
//for(i=0;i<binStrLen;i++)
//temp*=2;
//val = (Ipp16s)(temp-1 + qDCCoef);
val = (Ipp16s)((1<<binStrLen)-1) + qDCCoef;
}
PutBits(val, binStrLen);
/* n = binStrLen;
val <<= 32 - n;
if (mBitOff == 0) {
mPtr[0] = (Ipp8u)(val >> 24);
if (n > 8) {
mPtr[1] = (Ipp8u)(val >> 16);
if (n > 16) {
mPtr[2] = (Ipp8u)(val >> 8);
if (n > 24) {
mPtr[3] = (Ipp8u)(val);
}
}
}
} else {
mPtr[0] = (Ipp8u)((mPtr[0] & (0xFF << (8 - mBitOff))) | (Ipp8u)(val >> (24 + mBitOff)));
if (n > 8 - mBitOff) {
val <<= 8 - mBitOff;
mPtr[1] = (Ipp8u)(val >> 24);
if (n > 16 - mBitOff) {
mPtr[2] = (Ipp8u)(val >> 16);
if (n > 24 - mBitOff) {
mPtr[3] = (Ipp8u)(val >> 8);
if (n > 32 - mBitOff) {
mPtr[4] = (Ipp8u)val;
}
}
}
}
}
mPtr += (mBitOff + n) >> 3;
*pBitOffset = (mBitOff + n) & 7;
*/
}
//added by bxd Apr.22
//void xhEncode_IntraACVLC_MPEG4_16s1u( Ipp16s* pQDctBlkCoef,
// Ipp8u** ppBitStream,
// int* pBitOffset,
// int predDir,
// int noDCcode
// )
void xhEncode_IntraACVLC_MPEG4_16s1u( Ipp16s* pQDctBlkCoef,
int predDir,
int noDCcode
)
{
Ipp32u i, code, len;
int run, prev_run, abs_level;
Ipp32s level, prev_level;
const Ipp8u* zigzag;
int offset;
int j,level1;
int run1 = 0;
i = 1;
run = 0;
if (predDir == IPP_VIDEO_NONE)
zigzag = mZigZagScan;
else if(predDir == IPP_VIDEO_HORIZONTAL)
zigzag = mAlternateVerScan;
else if(predDir == IPP_VIDEO_VERTICAL)
zigzag = mAlternateHorScan;
while (i<64 && !(level = pQDctBlkCoef[zigzag[i++]]))
run++;
prev_level = level;
prev_run = run;
run = 0;
while (i < 64)
{
if ((level = pQDctBlkCoef[zigzag[i++]]) != 0){
abs_level = abs(prev_level);
// abs_level = abs_level < 64 ? abs_level : 0;
//find code & len from TB16
if ((prev_run <= mVLC_TB16.runMax[0]) && (abs_level <= mVLC_TB16.levMax[0][prev_run])){
offset = mVLC_TB16.runOff[0][prev_run] + abs_level -1;
code = mVLC_TB16.vlc[offset].code;
len = mVLC_TB16.vlc[offset].len;
code |= (prev_level < 0);
PutBits(code, len);
}
else
{
PutBits(3, 7);
//escape 1
level1 = abs_level - mVLC_TB16.levMax[0][prev_run];
if (level1 < 1)
goto esc2;
if((prev_run <= mVLC_TB16.runMax[0]) && (level1 <= mVLC_TB16.levMax[0][prev_run])){
offset = mVLC_TB16.runOff[0][prev_run] + level1 - 1;
code = mVLC_TB16.vlc[offset].code;
len = mVLC_TB16.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc2:
PutBits(1, 1);
if (abs_level > 64)
goto esc3;
for(j = 0; j < 7; j++)
{
if(abs_level > mVLC_RMAX_TB21[0][j].code)
continue;
else{
run1 = prev_run - mVLC_RMAX_TB21[0][j].len - 1;
break;
}
}
if(run1 < 0)
goto esc3;
if((run1 <= mVLC_TB16.runMax[0]) && (abs_level <= mVLC_TB16.levMax[0][run1])){
offset = mVLC_TB16.runOff[0][run1] + abs_level -1;
code = mVLC_TB16.vlc[offset].code;
len = mVLC_TB16.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc3:
PutBits(2, 2);
code = (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;//15:ESCAPE3
len = 20;
PutBits(code, len);
}
}
}
// PutBits(code, len);
prev_level = level;
prev_run = run;
run = 0;
}
else
run++;
}
//encode the last level
abs_level = abs(prev_level);
// abs_level = abs_level < 64 ? abs_level : 0;
if ((prev_run <= mVLC_TB16.runMax[1]) && (abs_level <= mVLC_TB16.levMax[1][prev_run])){
offset = mVLC_TB16.runOff[1][prev_run] + abs_level -1;
code = mVLC_TB16.vlc[offset].code;
len = mVLC_TB16.vlc[offset].len;
code |= (prev_level < 0);
PutBits(code, len);
}
else
{
PutBits(3, 7);
//escape 1
//int level1, run1 = 0;
level1 = abs_level - mVLC_TB16.levMax[1][prev_run];
if (level1 < 1)
goto esc4;
if((prev_run <= mVLC_TB16.runMax[1]) && (level1 <= mVLC_TB16.levMax[1][prev_run])){
offset = mVLC_TB16.runOff[1][prev_run] + level1 - 1;
code = mVLC_TB16.vlc[offset].code;
len = mVLC_TB16.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc4:
PutBits(1, 1);
if (abs_level > 64)
goto esc5;
for(j = 0; j < 7; j++)
{
if(abs_level > mVLC_RMAX_TB21[1][j].code)
continue;
else{
run1 = prev_run - mVLC_RMAX_TB21[1][j].len - 1;
break;
}
}
if(run1 < 0)
goto esc5;
if((run1 <= mVLC_TB16.runMax[1]) && (abs_level <= mVLC_TB16.levMax[1][run1])){
offset = mVLC_TB16.runOff[1][run1] + abs_level -1;
code = mVLC_TB16.vlc[offset].code;
len = mVLC_TB16.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc5:
PutBits(3, 2);
code = (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;//15:ESCAPE3
len = 20;
PutBits(code, len);
}
}
}
// PutBits(code, len);
}
//added by bxd Apr.22
//void xhEncode_InterVLC_MPEG4_16s1u( Ipp16s* pQDctBlkCoef,
// Ipp8u** ppBitStream,
// int* pBitOffset
// )
void xhEncode_InterVLC_MPEG4_16s1u( Ipp16s* pQDctBlkCoef)
{
Ipp32u i, abs_level, run, prev_run, code, len;
Ipp32s level, prev_level;
int offset;
int j,level1;
int run1 = 0;
i = 0;
run = 0;
while (i<64 && !(level = pQDctBlkCoef[mZigZagScan[i++]]))
run++;
prev_level = level;
prev_run = run;
run = 0;
while (i < 64)
{
if ((level = pQDctBlkCoef[mZigZagScan[i++]]) != 0){
abs_level = abs(prev_level);
// abs_level = abs_level < 64 ? abs_level : 0;
//find code & len from TB17
if ((prev_run <= mVLC_TB17.runMax[0]) && (abs_level <= mVLC_TB17.levMax[0][prev_run])) {
offset = mVLC_TB17.runOff[0][prev_run] + abs_level -1;
code = mVLC_TB17.vlc[offset].code;
len = mVLC_TB17.vlc[offset].len;
code |= (prev_level < 0);
PutBits(code, len);
}
else
{
PutBits(3, 7);
//escape 1
// int level1, run1 = 0;
level1 = abs_level - mVLC_TB17.levMax[0][prev_run];
if (level1 < 1)
goto esc2;
if((prev_run <= mVLC_TB17.runMax[0]) && (level1 <= mVLC_TB17.levMax[0][prev_run])){
offset = mVLC_TB17.runOff[0][prev_run] + level1 - 1;
code = mVLC_TB17.vlc[offset].code;
len = mVLC_TB17.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc2:
PutBits(1, 1);
if (abs_level > 64)
goto esc3;
for(j = 0; j < 7; j++)
{
if(abs_level > mVLC_RMAX_TB22[0][j].code)
continue;
else{
run1 = prev_run - mVLC_RMAX_TB22[0][j].len - 1;
break;
}
}
if(run1 < 0)
goto esc3;
if((run1 <= mVLC_TB17.runMax[0]) && (abs_level <= mVLC_TB17.levMax[0][run1])){
offset = mVLC_TB17.runOff[0][run1] + abs_level -1;
code = mVLC_TB17.vlc[offset].code;
len = mVLC_TB17.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc3:
PutBits(2, 2);
code = (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;//15:ESCAPE3
len = 20;
PutBits(code, len);
}
}
}
// PutBits(code, len);
prev_level = level;
prev_run = run;
run = 0;
}
else
run++;
}
//encode the last level
abs_level = abs(prev_level);
//070712change
//abs_level = abs_level < 64 ? abs_level : 0;
if ((prev_run <= mVLC_TB17.runMax[1]) && (abs_level <= mVLC_TB17.levMax[1][prev_run])) {
offset = mVLC_TB17.runOff[1][prev_run] + abs_level -1;
code = mVLC_TB17.vlc[offset].code;
len = mVLC_TB17.vlc[offset].len;
code |= (prev_level < 0);
PutBits(code, len);
}
else
{
PutBits(3, 7);
//escape 1
// int level1, run1 = 0;
level1 = abs_level - mVLC_TB17.levMax[1][prev_run];
if (level1 < 1)
goto esc4;
if((prev_run <= mVLC_TB17.runMax[1]) && (level1 <= mVLC_TB17.levMax[1][prev_run])){
offset = mVLC_TB17.runOff[1][prev_run] + level1 - 1;
code = mVLC_TB17.vlc[offset].code;
len = mVLC_TB17.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc4:
PutBits(1, 1);
if (abs_level > 64)
goto esc5;
for(j = 0; j < 7; j++)
{
if(abs_level > mVLC_RMAX_TB22[1][j].code)
continue;
else{
run1 = prev_run - mVLC_RMAX_TB22[1][j].len - 1;
break;
}
}
if(run1 < 0)
goto esc5;
if((run1 <= mVLC_TB17.runMax[1]) && (abs_level <= mVLC_TB17.levMax[1][run1])){
offset = mVLC_TB17.runOff[1][run1] + abs_level -1;
code = mVLC_TB17.vlc[offset].code;
len = mVLC_TB17.vlc[offset].len;
code |= (prev_level < 0);
PutBits(0, 1);
PutBits(code, len);
}
else{
esc5:
PutBits(3, 2);
code = (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1;//15:ESCAPE3
len = 20;
PutBits(code, len);
}
}
}
// PutBits(code, len);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -