#include <stdio.h>
#include <math.h>
#include <iostream.h>
void dct_QUANT4x4(int diff[4][4],int qp);
void main(char **argv, char argc)
{
int i,j;
int test[4][4];
int qp=26;
test[0][0]=21;
test[0][1]=21;
test[0][2]=21;
test[0][3]=21;
test[1][0]=21;
test[1][1]=21;
test[1][2]=21;
test[1][3]=21;
test[2][0]=21;
test[2][1]=21;
test[2][2]=21;
test[2][3]=21;
test[3][0]=21;
test[3][1]=21;
test[3][2]=21;
test[3][3]=21;
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
printf(" %d,",test[j][i]);
}
printf("\n");
}
dct_QUANT4x4(test, qp);
printf("\n");
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
printf(" %d,",test[j][i]);
}
printf("\n");
}
}
/*
void dct_QUANT8x8(int diff[8][8])
{
int i,j,k;
int a[8],b[8],c[8];
const c4 = 0.707;
const c2 = 0.923;
const c6 = 0.383;
const c1 = 0.981;
const c3 = 0.832;
const c5 = 0.556;
const c7 = 0.195;
const f1 = (1<<qbits)/3;
int qbits = 15 + floor(qp/6);
int M[8][8],Z[8][8];
int qp_y = qp%6;
//
// horizontal
//
for(i = 0 ; i < 8; i ++)
{
for(j=0;j<4;j++)
{ k=7-j;
a[j]=diff[i][j]+diff[i][k];
a[k]=diff[i][k]-diff[i][j];
}
b[0]=a[0]+a[3];
b[1]=a[1]+a[2];
b[2]=a[2]-a[1];
b[3]=a[3]-a[0];
b[4]=a[4];
b[5]=c4*(a[6]-a[5]);
b[6]=c4*(a[5]+a[6]);
b[7]=a[7];
c[0]=c4*(b[0]+b[1]);
c[1]=c4*(b[0]-b[1]);
c[2]=c6*b[2]+c2*b[3];
c[3]=c6*b[3]-c2*b[2];
c[4]=b[4]+b[5];
c[5]=b[4]-b[5];
c[6]=b[7]-b[6];
c[7]=b[7]+b[6];
diff[i][0]=c[0]>>1;
diff[i][4]=c[1]>>1;
diff[i][2]=c[2]>>1;
diff[i][6]=c[3]>>1;
diff[i][1]=(c7*c[4]+c1*c[7])>>1;
diff[i][5]=(c3*c[5]+c5*c[6])>>1;
diff[i][3]=(c3*c[6]-c5*c[5])>>1;
diff[i][7]=(c7*c[7]-c1*c[4])>>1;
}
/Vertical
for(j = 0 ; j < 8; j ++)
{
for(i=0;i<4;i++)
{ k=7-i;
a[i]=diff[j][i+diff[j][k];
a[k]=diff[j][k]-diff[j][i];
}
b[0]=a[0]+a[3];
b[1]=a[1]+a[2];
b[2]=a[2]-a[1];
b[3]=a[3]-a[0];
b[4]=a[4];
b[5]=c4*(a[6]-a[5]);
b[6]=c4*(a[5]+a[6]);
b[7]=a[7];
c[0]=c4*(b[0]+b[1]);
c[1]=c4*(b[0]-b[1]);
c[2]=c6*b[2]+c2*b[3];
c[3]=c6*b[3]-c2*b[2];
c[4]=b[4]+b[5];
c[5]=b[4]-b[5];
c[6]=b[7]-b[6];
c[7]=b[7]+b[6];.
diff[0][j]=c[0]>>1;
diff[4][j]=c[1]>>1;
diff[2][j]=c[2]>>1;
diff[6][j]=c[3]>>1;
diff[1][j]=(c7*c[4]+c1*c[7])>>1;
diff[5][j]=(c3*c[5]+c5*c[6])>>1;
diff[3][j]=(c3*c[6]-c5*c[5])>>1;
diff[7][j]=(c7*c[7]-c1*c[4])>>1;
}
//QUANT
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
M[i][j] = (abs(diff[j][i])*MF[qp_y][i][j]+f1)>>q_bits;
z[j][i] = diff[i][j]>=0 ? abs(M[i][j]) : (-abs(M[i][j]);
}
}
}
*/
void dct_QUANT4x4(int diff[4][4],int qp)
{
int i,j,k;
int a[4];
int qbits = 15+floor(qp/6); //qp 0-51
const f1 = (1<<qbits)/3;
int M[4][4];//Z[4][4];
int qp_y = qp%6;
int qp_c = qp/6;
int MF[6][4][4] =
{
{
{13107, 8066, 13107, 8066},
{8066, 5243, 8066, 5243},
{13107, 8066, 13107, 8066},
{8066, 5243, 8066, 5243},
},
{
{11916, 7490, 11916, 7490},
{7490, 4660, 7490, 4660},
{11916, 7490, 11916, 7490},
{7490, 4660, 7490, 4660},
},
{
{10082, 6554, 10082, 6554},
{6554, 4194, 6554, 4194},
{10082, 6554, 10082, 6554},
{6554, 4194, 6554, 4194},
},
{
{9362, 5825, 9362, 5825},
{5825, 3647, 5825, 3647},
{9362, 5825, 9362, 5825},
{5825, 3647, 5825, 3647},
},
{
{8192, 5243, 8192, 5243},
{5243, 3355, 5243, 3355},
{8192, 5243, 8192, 5243},
{5243, 3355, 5243, 3355},
},
{
{7282, 4559, 7282, 4559},
{4559, 2893, 4559, 2893},
{7282, 4559, 7282, 4559},
{4559, 2893, 4559, 2893},
},
};
//
// horizontal
//
for(i = 0 ; i < 4; i ++)
{
for(j=0;j<2;j++)
{ k=3-j;
a[j]=diff[i][j]+diff[i][k];
a[k]=diff[i][k]-diff[i][j];
}
diff[i][0]=a[0]+a[1];
diff[i][2]=a[0]-a[1];
diff[i][1]=2*a[3]+a[2];
diff[i][3]=a[3]-2*a[2];
}
//Vertical
for(j = 0 ; j < 4; j ++)
{
for(i=0;i<2;i++)
{
k=3-i;
a[i]=diff[i][j]+diff[k][j];
a[k]=diff[k][j]-diff[i][j];
}
diff[0][j]=a[0]+a[1];
diff[2][j]=a[0]-a[1];
diff[1][j]=2*a[3]+a[2];
diff[3][j]=a[3]-2*a[2];
}
//QUANT
// cout<<"test"<<endl;
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
M[i][j] = (abs(diff[i][j])*MF[qp_y][i][j]+f1)>>qbits;
diff[i][j] = diff[i][j]>=0 ? abs(M[i][j]) : (-abs(M[i][j]));
}
}
//cout<<"test"<<endl;
}
/*void Idct_IQUANT4x4(int diff[4][4],int qp)
{
int i,j,k;
int a[4];
int qbits = 15+floor(qp/6); //qp 0-51
const f1 = (1<<qbits)/3;
int M[4][4];//Z[4][4];
int qp_y = qp%6;
int MF[6][4][4] =
{
{
{13107, 8066, 13107, 8066},
{8066, 5243, 8066, 5243},
{13107, 8066, 13107, 8066},
{8066, 5243, 8066, 5243},
},
{
{11916, 7490, 11916, 7490},
{7490, 4660, 7490, 4660},
{11916, 7490, 11916, 7490},
{7490, 4660, 7490, 4660},
},
{
{10082, 6554, 10082, 6554},
{6554, 4194, 6554, 4194},
{10082, 6554, 10082, 6554},
{6554, 4194, 6554, 4194},
},
{
{9362, 5825, 9362, 5825},
{5825, 3647, 5825, 3647},
{9362, 5825, 9362, 5825},
{5825, 3647, 5825, 3647},
},
{
{8192, 5243, 8192, 5243},
{5243, 3355, 5243, 3355},
{8192, 5243, 8192, 5243},
{5243, 3355, 5243, 3355},
},
{
{7282, 4559, 7282, 4559},
{4559, 2893, 4559, 2893},
{7282, 4559, 7282, 4559},
{4559, 2893, 4559, 2893},
},
};
//
// horizontal
//
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
M[i][j] = (abs(diff[i][j])*MF[qp_y][i][j]+f1)<<qbits;
diff[i][j] = diff[i][j]>=0 ? abs(M[i][j]) : (-abs(M[i][j]));
}
}
for(i = 0 ; i < 4; i ++)
{
for(j=0;j<2;j++)
{ k=3-j;
a[j]=diff[i][j]+diff[i][k];
a[k]=diff[i][k]-diff[i][j];
}
diff[i][0]=a[0]+a[1];
diff[i][2]=a[0]-a[1];
diff[i][1]=a[3]/2+a[2];
diff[i][3]=a[3]-a[2]/2;
}
//Vertical
for(j = 0 ; j < 4; j ++)
{
for(i=0;i<2;i++)
{
k=3-i;
a[i]=diff[i][j]+diff[k][j];
a[k]=diff[k][j]-diff[i][j];
}
diff[0][j]=a[0]+a[1];
diff[2][j]=a[0]-a[1];
diff[1][j]=a[3]/2+a[2];
diff[3][j]=a[3]-a[2]/2;
}
//QUANT
// cout<<"test"<<endl;
//cout<<"test"<<endl;
}
*/