home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- cDCT.cpp
-
- 03/02/08 Xiaohong
- *************************************************************************/
- #include "cDCT.h"
- #include "macros.h"
- #include <math.h>
- /**********************************************************
- cDCT é╠âüâôâoè╓Éöé╠ÉΘî╛
- **********************************************************/
- cDCT::cDCT()
- {
- int i, j;
- double s;
- double sTmp;
-
- for(i=0; i<8; i++)
- {
- if(i == 0)
- {
- s = sqrt(0.125);
- }
- else
- {
- s = 0.5;
- }
-
- for (j=0; j<8; j++)
- {
- m_fCoefficients[i][j] = s * cos((PI/8.0)*i*(j+0.5));
- /* save integer value shifted 14 bits to left */
- sTmp = m_fCoefficients[i][j] * 16384;
- }
- }
-
- m_pIclip = m_nIclip+512;
- for (i= -512; i<512; i++)
- m_pIclip[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
-
- }
- cDCT::~cDCT()
- {
- }
- void cDCT::fDCT(short *pBlock)
- {
- int i, j, k;
- double s;
- double tmp[64];
-
-
- for (i=0; i<8; i++)
- {
- for (j=0; j<8; j++)
- {
- s = 0.0;
- for (k=0; k<8; k++)
- s += m_fCoefficients[j][k] * pBlock[8*i+k];
- tmp[8*i+j] = s;
- }
- }
-
- for (j=0; j<8; j++)
- {
- for (i=0; i<8; i++)
- {
-
- s = 0.0;
- for (k=0; k<8; k++)
- s += m_fCoefficients[i][k] * tmp[8*k+j];
- pBlock[8*i+j] = (short)(s+0.499999);
- }
- }
- }
- void cDCT::iDCT(short *pBlock)
- {
- int i;
-
- for (i=0; i<8; i++)
- IdctRow(pBlock+8*i);
-
- for (i=0; i<8; i++)
- IdctCol(pBlock+i);
- }
- inline void cDCT::IdctRow(short* pBlock)
- {
- int x0, x1, x2, x3, x4, x5, x6, x7, x8;
-
- /* shortcut */
- if (!((x1 = pBlock[4]<<11) | (x2 = pBlock[6]) | (x3 = pBlock[2]) |
- (x4 = pBlock[1]) | (x5 = pBlock[7]) | (x6 = pBlock[5]) | (x7 = pBlock[3])))
- {
- pBlock[0]=pBlock[1]=pBlock[2]=pBlock[3]=pBlock[4]=pBlock[5]=pBlock[6]=pBlock[7]=pBlock[0]<<3;
- return;
- }
-
- x0 = (pBlock[0]<<11) + 128; /* for proper rounding in the fourth stage */
-
- /* first stage */
- x8 = W7*(x4+x5);
- x4 = x8 + (W1-W7)*x4;
- x5 = x8 - (W1+W7)*x5;
- x8 = W3*(x6+x7);
- x6 = x8 - (W3-W5)*x6;
- x7 = x8 - (W3+W5)*x7;
-
- /* second stage */
- x8 = x0 + x1;
- x0 -= x1;
- x1 = W6*(x3+x2);
- x2 = x1 - (W2+W6)*x2;
- x3 = x1 + (W2-W6)*x3;
- x1 = x4 + x6;
- x4 -= x6;
- x6 = x5 + x7;
- x5 -= x7;
-
- /* third stage */
- x7 = x8 + x3;
- x8 -= x3;
- x3 = x0 + x2;
- x0 -= x2;
- x2 = (181*(x4+x5)+128)>>8;
- x4 = (181*(x4-x5)+128)>>8;
-
- /* fourth stage */
- pBlock[0] = (x7+x1)>>8;
- pBlock[1] = (x3+x2)>>8;
- pBlock[2] = (x0+x4)>>8;
- pBlock[3] = (x8+x6)>>8;
- pBlock[4] = (x8-x6)>>8;
- pBlock[5] = (x0-x4)>>8;
- pBlock[6] = (x3-x2)>>8;
- pBlock[7] = (x7-x1)>>8;
- }
- inline void cDCT::IdctCol(short* pBlock)
- {
- int x0, x1, x2, x3, x4, x5, x6, x7, x8;
-
- /* shortcut */
- if (!((x1 = (pBlock[8*4]<<8)) | (x2 = pBlock[8*6]) | (x3 = pBlock[8*2]) |
- (x4 = pBlock[8*1]) | (x5 = pBlock[8*7]) | (x6 = pBlock[8*5]) | (x7 = pBlock[8*3])))
- {
- pBlock[8*0]=pBlock[8*1]=pBlock[8*2]=pBlock[8*3]=pBlock[8*4]=pBlock[8*5]=pBlock[8*6]=pBlock[8*7]=m_pIclip[(pBlock[8*0]+32)>>6];
- return;
- }
-
- x0 = (pBlock[8*0]<<8) + 8192;
-
- /* first stage */
- x8 = W7*(x4+x5) + 4;
- x4 = (x8+(W1-W7)*x4)>>3;
- x5 = (x8-(W1+W7)*x5)>>3;
- x8 = W3*(x6+x7) + 4;
- x6 = (x8-(W3-W5)*x6)>>3;
- x7 = (x8-(W3+W5)*x7)>>3;
-
- /* second stage */
- x8 = x0 + x1;
- x0 -= x1;
- x1 = W6*(x3+x2) + 4;
- x2 = (x1-(W2+W6)*x2)>>3;
- x3 = (x1+(W2-W6)*x3)>>3;
- x1 = x4 + x6;
- x4 -= x6;
- x6 = x5 + x7;
- x5 -= x7;
-
- /* third stage */
- x7 = x8 + x3;
- x8 -= x3;
- x3 = x0 + x2;
- x0 -= x2;
- x2 = (181*(x4+x5)+128)>>8;
- x4 = (181*(x4-x5)+128)>>8;
-
- /* fourth stage */
- pBlock[8*0] = m_pIclip[(x7+x1)>>14];
- pBlock[8*1] = m_pIclip[(x3+x2)>>14];
- pBlock[8*2] = m_pIclip[(x0+x4)>>14];
- pBlock[8*3] = m_pIclip[(x8+x6)>>14];
- pBlock[8*4] = m_pIclip[(x8-x6)>>14];
- pBlock[8*5] = m_pIclip[(x0-x4)>>14];
- pBlock[8*6] = m_pIclip[(x3-x2)>>14];
- pBlock[8*7] = m_pIclip[(x7-x1)>>14];
- }
-
-