📄 mqc.c
字号:
static unsigned short mqc_qe[47]=
{0x5601,0x3401,0x1801,0x0ac1,0x0521,0x0221,0x5601,0x5401,
0x4801,0x3801,0x3001,0x2401,0x1c01,0x1601,0x5601,0x5401,
0x5101,0x4801,0x3801,0x3401,0x3001,0x2801,0x2401,0x2201,
0x1c01,0x1801,0x1601,0x1401,0x1201,0x1101,0x0ac1,0x09c1,
0x08a1,0x0521,0x0441,0x02a1,0x0221,0x0141,0x0111,0x0085,
0x0049,0x0025,0x0015,0x0009,0x0005,0x0001,0x5601};
static unsigned short mqc_nmps[47]=
{1, 2, 3, 4, 5, 38,7, 8, 9, 10,11,12,13,29,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,45,46};
static unsigned short mqc_nlps[47]=
{1, 6, 9, 12,29,33,6, 14,14,14,17,18,20,21,14,14,15,16,17,18,
19,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
38,39,40,41,42,43,46};
static unsigned short mqc_flag[47]=
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned short mqc_a,mqc_ct,mqc_mps[20],mqc_index[20];
unsigned long mqc_c;
unsigned char mqc_b;
unsigned char *mqc_bp,*mqc_start,*mqc_end;
double distortion;
double out_distortion;
//unsigned long sum_hist[16];
int mqc_numbytes()
{
return mqc_bp-mqc_start;
}
void mqc_byteout(void)
{
if(*mqc_bp==0xff)
{//bit stuffing
mqc_bp++;
//out_distortion++;
*mqc_bp=mqc_c>>20; //字节输出
out_distortion=distortion; //
mqc_c&=0xfffff;
mqc_ct=7;
}
else
{
if (mqc_c>=0x8000000)
{//'c'===1
(*mqc_bp)++;//bofore byte;
if(*mqc_bp==0xff)
{
mqc_c&=0x7ffffff;//
mqc_bp++;
//out_distortion++;
*mqc_bp=mqc_c>>20; //字节输出
out_distortion=distortion; //
mqc_c&=0xfffff;
mqc_ct=7;
}
else
{
mqc_bp++;
// out_distortion++;
*mqc_bp=mqc_c>>19; //字节输出
out_distortion=distortion;
mqc_c&=0x7ffff;
mqc_ct=8;
}
}
else
{
mqc_bp++;
//out_distortion++;
*mqc_bp=mqc_c>>19; //字节输出
out_distortion=distortion;
mqc_c&=0x7ffff;
mqc_ct=8;
}
}
}
void Init_AC_QM_Encoder(unsigned char *bp)
{
short i;
mqc_a=0x8000;
mqc_c=0;
mqc_b=0;
mqc_bp=bp-1;
//out_distortion-=1;
mqc_start=bp;
mqc_ct=12;
if(*mqc_bp==0xff)
mqc_ct=13;
mqc_index[0]=4;
mqc_index[17]=3;
mqc_index[18]=mqc_index[19]=46;
for(i=1;i<17;i++) mqc_index[i]=0;
for(i=0;i<20;i++) mqc_mps[i]=0;
}
void AMQ_pack(
short bit_num,
short bit_value,
short con)//con=context
{
short i,f_bit;
short a,b;
// printf("\n%d %d ;",bit_value,con);
// printf("IN:A=0X%X,C=0X%X,Qe=0X%X,mps=%d;",mqc_a,mqc_c,mqc_qe[mqc_index[con]],mqc_mps[con]);
for(i=0;i<bit_num;i++)
{
f_bit=bit_value>>i;
if((f_bit&1)==mqc_mps[con])//codemqc_mps
{
mqc_a-=mqc_qe[mqc_index[con]];
if((mqc_a&0x8000)==0)//mqc_mps<0.75
{
if(mqc_a<mqc_qe[mqc_index[con]])
mqc_a=mqc_qe[mqc_index[con]];//abnormal mqc_mps<Qe
else
mqc_c+=mqc_qe[mqc_index[con]];//normal mqc_mps>Qe
mqc_index[con]=mqc_nmps[mqc_index[con]];
////////////////////////////////////////////
//renorme();
do{
mqc_a<<=1;
mqc_c<<=1;
mqc_ct--;
if(mqc_ct==0)
{
mqc_byteout();
}
}while((mqc_a&0x8000)==0);
///////////////////////////////////////////
}
else
mqc_c+=mqc_qe[mqc_index[con]];
}
else//codelps
{
mqc_a-=mqc_qe[mqc_index[con]];//Qe=LPS,mqc_mps
if(mqc_a<mqc_qe[mqc_index[con]])//mqc_mps<Qe
mqc_c+=mqc_qe[mqc_index[con]];
else
mqc_a=mqc_qe[mqc_index[con]];
if(mqc_flag[mqc_index[con]]==1)
mqc_mps[con]=1-mqc_mps[con];
mqc_index[con]=mqc_nlps[mqc_index[con]];
////////////////////////////////////////////
//renorme();
//a=0;
do
{ mqc_a<<=1;
mqc_c<<=1;
mqc_ct--;
if(mqc_ct==0)
{
mqc_byteout();
}
// a++;
}while((mqc_a&0x8000)==0);
//printf("%d,",a);
}
////////////////////////////////////////////
}
// printf("OUT:A=0X%X,C=0X%X,Qe=0X%X,mps=%d;",mqc_a,mqc_c,mqc_qe[mqc_index[con]],mqc_mps[con]);
//for(i=0;i<=18;i++)
// printf("index[%d]=%d ",i,mqc_index[i]);
}
void mqc_setbits()
{
unsigned int tempc=mqc_c+mqc_a;
mqc_c|=0xffff;
if (mqc_c>=tempc) {
mqc_c-=0x8000;
}
}
void mqc_flush(void)
{
mqc_setbits();
mqc_c<<=mqc_ct;
mqc_byteout();
mqc_c<<=mqc_ct;
mqc_byteout();
if (*mqc_bp!=0xff)
mqc_bp++;
}
void mqc_bytein(void)
{
if(mqc_bp!=mqc_end)
{
unsigned long b1;
if(mqc_bp+1!=mqc_end)
b1=*(mqc_bp+1);
else
{
b1=0xff;
//return 1;
}
if(*mqc_bp==0xff)
{
if(b1>0x8f) {
mqc_c+=0xff00;
mqc_ct=8;
}
else{
mqc_bp++;
mqc_c+=b1<<9;
mqc_ct=7;
}
}
else{
mqc_bp++;
mqc_c+=b1<<8;
mqc_ct=8;
}
}
else
{
mqc_c+=0xff00;
mqc_ct=8;
//return 1;
}
}
void Init_AC_QM_Decoder(unsigned char *bp,
unsigned short len)
{
short i;
mqc_start=bp;
mqc_end=bp+len;
mqc_bp=bp;
mqc_c=(*mqc_bp)<<16;
mqc_bytein();
mqc_c<<=7;
mqc_ct-=7;
mqc_a=0x8000;
mqc_index[0]=4;
for(i=1;i<17;i++) mqc_index[i]=0;
mqc_index[17]=3;
mqc_index[18]=46;
mqc_index[19]=46;
for(i=0;i<20;i++) mqc_mps[i]=0;
}
short AMQ_unpack(
short bit_num,
short *bit_value,
short con)
{
unsigned short i,d;
unsigned long chigh;
unsigned char hist;
*bit_value=0;
for(i=0;i<bit_num;i++)
{
chigh=mqc_c>>16;
mqc_a-=mqc_qe[mqc_index[con]];
if(chigh<mqc_qe[mqc_index[con]])
{//decode LPS
if(mqc_a<mqc_qe[mqc_index[con]])
{//abnormal
mqc_a=mqc_qe[mqc_index[con]];
d=mqc_mps[con];
mqc_index[con]=mqc_nmps[mqc_index[con]];
}
else
{//normal
mqc_a=mqc_qe[mqc_index[con]];
d=1-mqc_mps[con];
if(mqc_flag[mqc_index[con]]==1)
mqc_mps[con]=1-mqc_mps[con];
mqc_index[con]=mqc_nlps[mqc_index[con]];
}
hist=0;
do
{
//hist++;
if(mqc_ct==0)
{
mqc_bytein();
if(mqc_bp>=mqc_end)
return -1;
}
mqc_a<<=1;
mqc_c<<=1;
mqc_ct--;
}while((mqc_a&0x8000)==0);
// printf("%d,",hist);
// sum_hist[hist]++;
}
else
{//decode mqc_mps
chigh-=mqc_qe[mqc_index[con]];
mqc_c&=0xffff;
mqc_c+=chigh<<16;
if((mqc_a&0x8000)==0)
{//A<0.75
if(mqc_a<mqc_qe[mqc_index[con]])
{
d=1-mqc_mps[con];
if(mqc_flag[mqc_index[con]]==1)
mqc_mps[con]=1-mqc_mps[con];
mqc_index[con]=mqc_nlps[mqc_index[con]];
}
else
{
d=mqc_mps[con];
mqc_index[con]=mqc_nmps[mqc_index[con]];
}
hist=0;
do{ // hist++;
if(mqc_ct==0)
{
mqc_bytein();
if(mqc_bp>=mqc_end)
return -1;
}
mqc_a<<=1;
mqc_c<<=1;
mqc_ct--;
}while((mqc_a&0x8000)==0);
//sum_hist[hist]++;
//printf("%d,",hist);
}
else//A>0.75
d=mqc_mps[con];
}
*bit_value+=(short)(d<<i);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -