/*********************************************************************/ /* dct.c Function to perform a 8 point 2D DCT */ /* DCT is performed using direct matrix multiplication */ /*********************************************************************/ /* externals */ #include "dct_main.h" extern unsigned char image_in[IMAGE_SIZE]; extern unsigned char image_out[IMAGE_SIZE]; extern short block[BLOCK_SIZE]; extern const short coe[12]; void dct(void) { short ADD[20]; /* Table of the addition coefficients */ int M[12]; /* Table of the results of the multiplication */ int postadd1,postadd2; int i,j; for(j=0;j<8;j++) { /* first set of addtions */ ADD[0]= (block[j]+block[56+j]); /* x(0)+x(7) */ ADD[1]= (block[24+j]+block[32+j]); /* x(3)+x(4) */ ADD[2]= (block[8+j]+block[48+j]); /* x(1)+x(6) */ ADD[3]= (block[16+j]+block[40+j]); /* x(2)+x(5) */ ADD[4]= (block[j]-block[56+j]); /* x(0)+x(7) */ ADD[5]= (block[48+j]-block[8+j]); /* x(6)-x(1) */ ADD[6]= (block[24+j]-block[32+j]); /* x(3)-x(4) */ ADD[7]= (block[16+j]-block[40+j]); /* x(2)-x(5) */ /* second set of addtions, this is done so previous additions do not need to be repeated */ ADD[8]= (ADD[0]+ADD[1]); ADD[9]= (ADD[0]-ADD[1]); ADD[10]=(ADD[2]+ADD[3]); ADD[11]=(ADD[2]-ADD[3]); ADD[12]=(ADD[4]+ADD[6]); ADD[13]=(ADD[5]+ADD[7]); ADD[14]=(ADD[9]+ADD[11]); ADD[15]=(ADD[4]+ADD[5]); ADD[16]=(ADD[12]+ADD[13]); ADD[17]=(ADD[6]+ADD[7]); ADD[18]=(ADD[8]+ADD[10]); ADD[19]=(ADD[8]-ADD[10]); /* Multiplications carried out, note: here 14. Includes one over root eight term */ M[0]= (int)(coe[0]*ADD[9]); M[1]= (int)(coe[1]*ADD[14]); M[2]= (int)(coe[2]*ADD[11]); M[3]= (int)(coe[3]*ADD[4]); M[4]= (int)(coe[4]*ADD[15]); M[5]= (int)(coe[5]*ADD[5]); M[6]= (int)(coe[6]*ADD[12]); M[7]= (int)(coe[7]*ADD[16]); M[8]=(int)(coe[8]*ADD[13]); M[9]=(int)(coe[9]*ADD[6]); M[10]=(int)(coe[10]*ADD[17]); M[11]=(int)(coe[11]*ADD[7]); /* post multiplication, additions + subtractions */ block[j]=ADD[18]; /* y(0) */ block[32+j]=ADD[19]; /* y(4) */ block[16+j]=(short)((M[0]+M[1])>>12); /* y(2) */ block[48+j]=(short)((M[1]-M[2])>>12); /* y(6) */ postadd1= M[6]+M[7]; postadd2= M[7]+M[8]; block[56+j]= (short)((M[3]+M[4]+postadd1)>>12); /* y(7) */ block[40+j]= (short)((M[4]+M[5]+postadd2)>>12); /* y(5) */ block[8+j]= (short)((M[9]+M[10]-postadd1)>>12); /* y(1) */ block[24+j]= (short)((postadd2-M[10]-M[11])>>12); /* y(3) */ } for(i=0;i<64;i+=8) { /* first set of addtions */ ADD[0]=(block[i]+block[i+7]); /* x(0)+x(7) */ ADD[1]=(block[i+3]+block[i+4]); /* x(3)+x(4) */ ADD[2]=(block[i+1]+block[i+6]); /* x(1)+x(6) */ ADD[3]=(block[i+2]+block[i+5]); /* x(2)+x(5) */ ADD[4]=(block[i]-block[i+7]); /* x(0)-x(7) */ ADD[5]=(block[i+6]-block[i+1]); /* x(6)-x(1) */ ADD[6]=(block[i+3]-block[i+4]); /* x(3)-x(4) */ ADD[7]=(block[i+2]-block[i+5]); /* x(2)-x(5) */ /* second set of addtions, this is done so previous additions do not need to be repeated */ ADD[8]=(ADD[0]+ADD[1]); ADD[9]=(ADD[0]-ADD[1]); ADD[10]=(ADD[2]+ADD[3]); ADD[11]=(ADD[2]-ADD[3]); ADD[12]=(ADD[4]+ADD[6]); ADD[13]=(ADD[5]+ADD[7]); ADD[14]=(ADD[9]+ADD[11]); ADD[15]=(ADD[4]+ADD[5]); ADD[16]=(ADD[12]+ADD[13]); ADD[17]=(ADD[6]+ADD[7]); ADD[18]=(ADD[8]+ADD[10]); ADD[19]=(ADD[8]-ADD[10]); /* Multiplications carried out, note: here 14. Includes one over root eight term */ M[0]= (int)(coe[0]*ADD[9]); M[1]= (int)(coe[1]*ADD[14]); M[2]= (int)(coe[2]*ADD[11]); M[3]= (int)(coe[3]*ADD[4]); M[4]= (int)(coe[4]*ADD[15]); M[5]= (int)(coe[5]*ADD[5]); M[6]= (int)(coe[6]*ADD[12]); M[7]= (int)(coe[7]*ADD[16]); M[8]=(int)(coe[8]*ADD[13]); M[9]=(int)(coe[9]*ADD[6]); M[10]=(int)(coe[10]*ADD[17]); M[11]=(int)(coe[11]*ADD[7]); /* post multiplication, additions + subtractions */ block[i]=(short)(ADD[18]>>3); /* y(0) */ block[i+4]=(short)(ADD[19]>>3); /* y(4) */ block[i+2]=(short)((M[0]+M[1])>>15); /* y(2) */ block[i+6]=(short)((M[1]-M[2])>>15); /* y(6) */ postadd1= M[6]+M[7]; postadd2= M[7]+M[8]; block[i+7]= (short)((M[3]+M[4]+postadd1)>>15); /* y(7) */ block[i+5]= (short)((M[4]+M[5]+postadd2)>>15); /* y(5) */ block[i+1]= (short)((M[9]+M[10]-postadd1)>>15); /* y(1) */ block[i+3]= (short)((postadd2-M[10]-M[11])>>15); /* y(3) */ } }