思路:用手算把cos(PI*(2*n+1)*k/16)计算出来,程序查表使用
实际使用,一副640×480的图片编码程序运行时间从0.34秒减少到0.19秒。
如果用此方法压缩摄像头yuv视频,帧率可能得到10帧。摄像头yuv格式视频帧率一般为15-20。
对比验证数据,只有几个数偏差大些,可能是四舍五入造成的
#include
#include
#include
#include
#define PI 3.1415926
int main(void){
//cs:cos(PI*(2*n+1)*k/16)
double cs[64]={1, 1, 1, 1, 1, 1, 1, 1,
0.98, 0.83, 0.56, 0.20, -0.20, -0.56, -0.83, -0.98,
0.92, 0.38, -0.38, -0.92, -0.92, -0.38, 0.38, 0.92 ,
0.83, -0.20, -0.98, -0.56, 0.56, 0.98, 0.20, -0.83,
0.71, -0.71, -0.71, 0.71, 0.71, -0.71, -0.71,0.71,
0.56, -0.98, 0.20, 0.83, -0.83, -0.20, 0.98, -0.56,
0.38, -0.92, 0.92, -0.38, -0.38, 0.92, -0.92, 0.38,
0.20, -0.56, 0.83, -0.98, 0.98, -0.83, 0.56, -0.20
};
//--------------1D DCT-----------------------------------------
int DCT(double i[8],double o[8]){ //ID DCT 参数类型不能用unsigned char ,因为中间系数已超char取值范围
double s=0.0;
for(int k=0;k<8;k++){
for(int n=0;n<8;n++){
s=s+i[n]*cs[k*8+n]; //查cs表
}
if(k==0){
s=s*(1.0/(2*sqrt(2)));
}else{
s=s*(1.0/2);
}
o[k]=s;
s=0.0;
}
return 0;
}
//--------------------------------------------------------------------
double i[64]={
-76,-73,-67,-62,-58,-67,-64,-55,
-65,-69,-73,-38,-19,-43,-59,-56,
-66,-69,-60,-15,16,-24,-62,-55,
-65,-70,-57,-6,26,-22,-58,-59,
-61,-67,-60,-24,-2,-40,-60,-58,
-49,-63,-68,-58,-51,-60,-70,-53,
-43,-57,-64,-69,-73,-67,-63,-45,
-41,-49,-59,-60,-63,-52,-50,-34
};
//-------------8行分别1D DCT---------------------
double w[64]={}; //中间8×8
for(int a=0;a<64;a=a+8){
double ls_o[8]={};
double ls_i[8]={};
memcpy(ls_i,&(i[a]),64);
DCT(ls_i,ls_o);
memcpy(&(w[a]),ls_o,64);
}
//----------对中间8×8 列1D DCT-------------------------
double zj[8][8]={}; //取中间w的8个8列
int t=0;
for(int a=0;a<8;a++){
for(int b=0;b<8;b++){
zj[t][b]=w[b*8+a];
}
t++;
}
double ll[64]={}; //现在的列是水平放置的,也就是列变成了行,要转为列
for(int a=0;a<8;a++){ //对8列1D DCT
double zz[8]={};
DCT(zj[a],zz);
memcpy(&(ll[8*a]),zz,64);
}
int k=0;
double out[64]={}; //2D DCT 系数
for(int a=0;a<8;a++){
for(int b=0;b<8;b++){
out[8*b+a]=ll[k];
k++;
}
}
//----------显示--------------------------------------------
for(int a=0;a<8;a++){
for(int b=0;b<8;b++){
printf("%f ,",out[a*8+b]);
}
puts("");
}
return 0;
}