📄 sacode.cpp
字号:
#include "stdafx.h"
#include "sactable.h"
#include "color.h"
#include "Cbitstream.h"
#include "sac.h"
SAC::SAC(CBitStream *OutStream)
{
low=0;
high=top;
opposite_bits=0;
SAClength=0;
zerorun=0;
OutputStream=OutStream;
}
int SAC::AR_Encode(int index, const int *cumul_freq)
{ int bitcount=0;
if (index<0) return -1; /* Escape Code */
CodeWord=0;
CodeLength=0;
SAClength = high - low + 1;
high = low - 1 + (SAClength * cumul_freq[index]) / cumul_freq[0];
low += (SAClength * cumul_freq[index+1]) / cumul_freq[0];
for ( ; ; )
{if (high < q2)
bitcount+=bit_opp_bits(0);
else if (low >= q2)
{bitcount+=bit_opp_bits(1);
low -= q2;
high -= q2;
}
else if (low >= q1 && high < q3)
{opposite_bits += 1;
low -= q1;
high -= q1;
}
else break;
low *= 2;
high = 2*high+1;
}
if (CodeLength)
OutputStream->PutVarible(CodeWord>>1,CodeLength);
return bitcount;
}
int SAC::CodeTCoef(int mod_index, int position, int intra)
{ int length;
switch (position)
{case 1:
if (intra)
length = AR_Encode(mod_index, cumf_TCOEF1_intra);
else
length = AR_Encode(mod_index, cumf_TCOEF1);
break;
case 2:
if (intra)
length = AR_Encode(mod_index, cumf_TCOEF2_intra);
else
length = AR_Encode(mod_index, cumf_TCOEF2);
break;
case 3:
if (intra)
length = AR_Encode(mod_index, cumf_TCOEF3_intra);
else
length = AR_Encode(mod_index, cumf_TCOEF3);
break;
default:
if (intra)
length = AR_Encode(mod_index, cumf_TCOEFr_intra);
else
length = AR_Encode(mod_index, cumf_TCOEFr);
break;
}
return length;
}
int SAC::bit_opp_bits(BOOL bit) // Output a bit and the following opposite bits
{ int bitcount=0;
if (zerorun>13)
{CodeWord|=1;
CodeWord<<=1;
bitcount++;
CodeLength++;
if (CodeLength>=31)
{OutputStream->PutVarible(CodeWord>>1,CodeLength);
CodeWord=0;
CodeLength=0;
}
zerorun=0;
}
if (bit)
zerorun=0;
else
zerorun++;
CodeWord|=bit;
CodeWord<<=1;
bitcount++;
CodeLength++;
if (CodeLength>=31)
{OutputStream->PutVarible(CodeWord>>1,CodeLength);
CodeWord=0;
CodeLength=0;
}
while(opposite_bits > 0)
{if (zerorun>13)
{CodeWord|=1;
CodeWord<<=1;
bitcount++;
CodeLength++;
if (CodeLength>=31)
{OutputStream->PutVarible(CodeWord>>1,CodeLength);
CodeWord=0;
CodeLength=0;
}
zerorun=0;
}
if (!bit)
zerorun=0;
else
zerorun++;
CodeWord|=!bit;
CodeWord<<=1;
bitcount++;
CodeLength++;
if (CodeLength>=31)
{OutputStream->PutVarible(CodeWord>>1,CodeLength);
CodeWord=0;
CodeLength=0;
}
opposite_bits--;
}
return bitcount;
}
int SAC::encode_flush()
{ int bitcount = 0;
CodeWord=0;
CodeLength=0;
opposite_bits++;
if (low < q1)
bitcount+=bit_opp_bits(0);
else
bitcount+=bit_opp_bits(1);
low = 0;
high = top;
zerorun=0;
if (CodeLength)
OutputStream->PutVarible(CodeWord>>1,CodeLength);
return bitcount;
}
int SAC::IndexFN(int value, const int *table, int max)
{ int n=0;
while(1)
{if (table[n++]==value)
return n-1;
if (n>max)
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -