# Appendix A A C Program for the Fast Discrete Cosine Transform

```/*ffdct.c*/
/*fast forward discrete cosine transform in the current
frame */

#include "config.h"
#include "global.h"

#define W1 2841 /* sqrt(2)cos(π/16)<< 11*/
#define W2 2676 /* sqrt(2)cos(2π/16)<< 11*/
#define W3 2408 /* sqrt(2)cos(3π/16)<< 11*/
#define W5 1609 /* sqrt(2)cos(5π/16)<< 11*/
#define W6 1108 /* sqrt(2)cos(6π/16)<< 11*/
#define W7 565 /* sqrt(2)cos(7π/16)<< 11*/
#define W10 2276 /* W1 - W7 */
#define W11 3406 /* W1 + W7 */
#define W12 4017 /* W3 + W5 */
#define W13 799 /* W3 - W5 */
#define W14 1568 /* W2 - W6 */
#define W15 3784 /* W2 + W6 */

/* global declarations */
void ffdct _ANSI_ARGS_((int *block));

void ffdct(block)
int *block;
{
int s [10] ,t [10] ,r [10] ;
int *p;
int j, temp;
/*forward transformation in "H" direction*/
p = block;
for(j=0; j<64; j +=8 )
{
/* first stage transformation */
s[0] = (*(p) + *(p+7));
s[1] = (*(p+1) + *(p+6));
s[2] = (*(p+2) + *(p+5));
s[3] = (*(p+3) + *(p+4));
s[4] = (*(p+3) - *(p+4));
s[5] = (*(p+2) - *(p+5));
s[6] = (*(p+1) - *(p+6));
s[7] = (*(p) - *(p+7));

/* second stage transformation */
t[0] = s[0] + s[3];
t[1] = s[1] + s[2] ;
t[2] = s[1] - s[2] ;
t[3] = s[0] - s[3] ;
t[5] = ((s[6] - s[5]) * 181) >> 8;
t[6] = ((s[6] + s[5]) * 181) >> 8;

/* third stage transformation */
r[4] = s[4] + t[5];
r[5] = s[4] - t[5];
r[6] = s[7] - t[6];
r[7] = s[7] + t[6];

/* fourth stage transformation */
block[0+j] = (t[0] + t[1]);
block[4+j] = (t[0] - t[1]);
temp = (r[4] + r[7]) * W1;
block[1+j] = (temp - r[4] * W10) >> 11;
block[7+j] = (r[7] * W11 - temp) >> 11;
temp = ( r[5] + r[6]) * W3 ;
block[3+j] = (temp - r[5] * W12) >> 11;
block[5+j] = (temp - r[6] * W13) >> 11;
temp = (t[2] + t[3]) * W6;
block[2+j] = (temp + t[3] * W14) >> 11;
block[6+j] = (temp - t[2] * W15) >> 11;
P += 8;
}
/* forward transformation in 'V' direction */
for(j=0; j<8; j++)
{
/* first stage transformation */
s[0] = block[j] + block[j+56];
s[1] = block[j+8] + block[j+48];
s[2] = block[j+16] + block[j+40];
s[3] = block[j+24] + block[j+32];
s[4] = block[j+24] - block[j+32];
s[5] = block[j+16] - block[j+40];
s[6] = block[j+8] - block[j+48];
s[7] = block[j] - block [j+56];

/* second stage transformation */
t[0] = s[0] + s[3];
t[1] = s[l] + s[2];
t[2] = s[l] - s[2];
t[3] = s[0] - s[3];
t[5] = ((s[6] - s[5]) * 181) >> 8;
t[6] = ((s[6] + s[5]) * 181) >> 8;

/* third stage transformation */
r[4] = s[4] + t[5];
r[5] = s[4] - t[5];
r[6] = s[7] - t[6];
r[7] = s[7] + t[6];

/* fourth stage transformation */
/* transform coefficients */
/* coefficients are divided by 8 and rounded */
block[0+j]= ((t[0] + t[l]) + 4) >> 3;
block[32+j]= ((t[0] - t[1]) + 4) >> 3;
temp = (r[4] + r[7]) * W1;
block[8+j]= ((temp - r[4] * W10) + 8192) >> 14;
block[56+j]= (((r[7] * W11) - temp) + 8192) >> 14;
temp = (r[5] + r[6]) * W3 ;
block[24+j]= ((temp - r[5] * W12) + 8192) >> 14;
block[40+j]= ((temp - r[6] * W13) + 8192) >> 14;
temp = (t[2] + t[3]) * W6;
block[16+j]= ((temp + t[3] * W14) + 8192) >> 14;
block[48+j]= ((temp - t[2] * W15) + 8192) >> 14;
}
}

```

Standard Codecs: Image Compression to Advanced Video Coding (IET Telecommunications Series)
ISBN: 0852967101
EAN: 2147483647
Year: 2005
Pages: 148
Authors: M. Ghanbari

Similar book on Amazon