DSPRelated.com
Forums

DCT & IDCT algorithm problems

Started by Unknown April 10, 2005
Sweet... ok I finally get it... and it works!  Thanks James!
I just tested it on some other matrices with a much larger variance in
numbers and the IDCT was mostly correct on recreating the original
matrix.  From what I understand, it shouldn't be 100% right all the
time correct?  Below is the matrix I tested and its result from IDCT:

Original Matrix
140  144  147  140  140  155  179  175
144  152  140  147  140  148  167  179
152  155  136  167  163  162  152  172
168  145  156  160  152  155  136  160
162  148  156  148  140  136  147  162
147  167  140  155  155  140  136  162
136  156  123  167  162  144  140  147
148  155  136  155  152  147  147  136

After iDCT
140  144  147  141  140  155  179  175
144  152  140  147  140  148  167  179
152  155  136  167  163  162  152  172
168  145  156  160  152  154  136  160
162  148  156  148  140  136  147  162
147  167  140  155  155  140  136  162
136  156  123  166  162  144  140  148
148  155  136  155  152  147  147  136

Actually, all I know about this algorithm is the given equations. You could 
help precision by using the best constants possible: try 4 * atan(1.0) for 
PI and sqrt(2.0) / 2 for .7071 and see if that helps any. Also, don't round 
off at the end of your algorithm functions (the floor calls).

Rather than:
dct() / idct()
{
do stuff
for all values: floor(value + .5}
}
output:: for all values: value;

use:
dct() / idct()
{
do stuff
}
output: for all values:: floor(values + .5);

<groups@jrmanes.com> wrote in message 
news:1113182015.009802.128280@g14g2000cwa.googlegroups.com...
> Sweet... ok I finally get it... and it works! Thanks James! > I just tested it on some other matrices with a much larger variance in > numbers and the IDCT was mostly correct on recreating the original > matrix. From what I understand, it shouldn't be 100% right all the > time correct? Below is the matrix I tested and its result from IDCT: > > Original Matrix > 140 144 147 140 140 155 179 175 > 144 152 140 147 140 148 167 179 > 152 155 136 167 163 162 152 172 > 168 145 156 160 152 155 136 160 > 162 148 156 148 140 136 147 162 > 147 167 140 155 155 140 136 162 > 136 156 123 167 162 144 140 147 > 148 155 136 155 152 147 147 136 > > After iDCT > 140 144 147 141 140 155 179 175 > 144 152 140 147 140 148 167 179 > 152 155 136 167 163 162 152 172 > 168 145 156 160 152 154 136 160 > 162 148 156 148 140 136 147 162 > 147 167 140 155 155 140 136 162 > 136 156 123 166 162 144 140 148 > 148 155 136 155 152 147 147 136 >
<groups@jrmanes.com> wrote in message 
news:1113182015.009802.128280@g14g2000cwa.googlegroups.com...
> Sweet... ok I finally get it... and it works! Thanks James! > I just tested it on some other matrices with a much larger variance in > numbers and the IDCT was mostly correct on recreating the original > matrix. From what I understand, it shouldn't be 100% right all the > time correct?
There's an accuracy requirement for the IDCT. You care more about the IDCT because the encoder and decoder must track (otherwise errors build up the further you move from an I frame). This is given in a now discontinued IEEE standard, but is also discussed by the ijg. They have an open-source test for IDCT compliance. http://cvs.sourceforge.net/viewcvs.py/libmpeg2/MSSG/ieeetest.c?rev=1.3 MPEG-4 adds a few more requirements.
groups@jrmanes.com wrote in message news:<1113172739.325872.68860@l41g2000cwc.googlegroups.com>...
> Howdy, > > I'm trying to get the DCT and IDCT algorithm correct for MPEG-4 > encoding/decoding. Following the DCT equation that I've put here: > http://www.teamearth.com/wiki/Main/MPEG4 , I generated the following > DCT and IDCT algorithms: > > void dct(int data[8][8]) { > int u,v,x,y; > double temp[8][8] = {{0}}; > for (u=0; u<8; u++) { > for (v=0; v<8; v++) { > for (x=0; x<8; x++) { > for (y=0; y<8; y++) { > temp[u][v] += data[x][y] * cos((2*x+1)*u*PI/16.0) * > cos((2*y+1)*v*PI/16.0);
how come the +1 in 2*x+1 as this is not a DC cosine of frequency 0.
> } > } > } > } > > for (u=0; u<8; u++) { > for (v=0; v<8; v++) { > if (u==0) temp[u][v] *= .7071; > if (v==0) temp[u][v] *= .7071; > data[u][v] = floor(temp[u][v] / 16.0+0.5); > } > } > } > > void idct(int data[8][8]) { > int u,v,x,y; > double temp[8][8] = {{0}}; > for (x=0; x<8; x++) { > for (y=0; y<8; y++) { > for (u=0; u<8; u++) { > for (v=0; v<8; v++) { > temp[x][y] += data[u][v] * cos((2*x+1)*u*PI/16.0) * > cos((2*y+1)*v*PI/16.0); > if (u==0) { temp[x][y] *= .7071; } > if (v==0) { temp[x][y] *= .7071; } > } > } > } > } > > for (x=0; x<8; x++) { > for (y=0; y<8; y++) { > data[x][y] = floor(temp[x][y] / 16.0 + 0.5); > } > } > } > > When I give DCT an 8x8 matrix of all 10s, I get back a matrix with a 20 > for the DC and 0s for the AC. When I send that to the AC, I get all 0s > back. Does anyone see what my problem is? Or am I approaching this > incorrectly? > > Thanks, > J.R. > > Test runs: > > Original Matrix > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > 10 10 10 10 10 10 10 10 > > After DCT > 20 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > > After IDCT > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0 > 0 0 0 0 0 0 0 0
Hi,

Finally whats the code? What are the mistakes? I cant figure it out. Could
u explain? 

How to draw moving picures to pictures and then pictures to macroblocks?

I am presently looking for MPEG4 Encoder and Decoder. Please give some
information. Is there any opensources? Is it good to start from
opensource? Let us say, if i want to develope MPEG4 Enco n Deco, how much
time will it takes? PLease help me in this issue.

Looking for response.

GIMS




>groups@jrmanes.com wrote in message
news:<1113172739.325872.68860@l41g2000cwc.googlegroups.com>...
>> Howdy, >> >> I'm trying to get the DCT and IDCT algorithm correct for MPEG-4 >> encoding/decoding. Following the DCT equation that I've put here: >> http://www.teamearth.com/wiki/Main/MPEG4 , I generated the following >> DCT and IDCT algorithms: >> >> void dct(int data[8][8]) { >> int u,v,x,y; >> double temp[8][8] = {{0}}; >> for (u=0; u<8; u++) { >> for (v=0; v<8; v++) { >> for (x=0; x<8; x++) { >> for (y=0; y<8; y++) { >> temp[u][v] += data[x][y] * cos((2*x+1)*u*PI/16.0) * >> cos((2*y+1)*v*PI/16.0); > >how come the +1 in 2*x+1 as this is not a DC cosine of frequency 0. > >> } >> } >> } >> } >> >> for (u=0; u<8; u++) { >> for (v=0; v<8; v++) { >> if (u==0) temp[u][v] *= .7071; >> if (v==0) temp[u][v] *= .7071; >> data[u][v] = floor(temp[u][v] / 16.0+0.5); >> } >> } >> } >> >> void idct(int data[8][8]) { >> int u,v,x,y; >> double temp[8][8] = {{0}}; >> for (x=0; x<8; x++) { >> for (y=0; y<8; y++) { >> for (u=0; u<8; u++) { >> for (v=0; v<8; v++) { >> temp[x][y] += data[u][v] * cos((2*x+1)*u*PI/16.0) * >> cos((2*y+1)*v*PI/16.0); >> if (u==0) { temp[x][y] *= .7071; } >> if (v==0) { temp[x][y] *= .7071; } >> } >> } >> } >> } >> >> for (x=0; x<8; x++) { >> for (y=0; y<8; y++) { >> data[x][y] = floor(temp[x][y] / 16.0 + 0.5); >> } >> } >> } >> >> When I give DCT an 8x8 matrix of all 10s, I get back a matrix with a
20
>> for the DC and 0s for the AC. When I send that to the AC, I get all
0s
>> back. Does anyone see what my problem is? Or am I approaching this >> incorrectly? >> >> Thanks, >> J.R. >> >> Test runs: >> >> Original Matrix >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> 10 10 10 10 10 10 10 10 >> >> After DCT >> 20 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> >> After IDCT >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >> 0 0 0 0 0 0 0 0 >
This message was sent using the Comp.DSP web interface on www.DSPRelated.com