# Quantization scaling for JPEG decoder based on Arai Agui and Nakajima

This topic is 1823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey people,

I'm currently working on a jpeg decoder based on the algorithm of Arai, Agui and Nakajima. While my solution is almost working I am missing a critical scaling operation that is key for this approach (which turns dct values into a dft values). I am supposed to scale the quantization matrix with based on the position of the values in the quantization matrix/ I just can't figure out how to calculate these scaling factors.

A good reference I am working with can be found here: http://vsr.informatik.tu-chemnitz.de/~jan/MPEG/HTML/IDCT.html

Hoping to find some people that have practical experience with this.

##### Share on other sites

Alright... found the solution!

In case someone else runs into this, here is how it works. You need to scale the quantization matrices with a value depending on the row and column position of each element in this matrix. The formula for this is:

scaling = 4 / Cu * cos((PI * u) / 16)

The trick is that Cu is 1/sqrt(2) in case u is 0 and 1 in any other case. The scaling must be done for both the row and column. Because I work with 8x8 blocks the pseudo code looks like:

// Pre-calculate 1/sqrt(2)
var oneOverSqrtTwo = 0.70710678118654752440084436210485;

// Our quantization is an 1D array with 64 (8x8) elements.
var index = 0;

// Go through all columns.
for (column in 0...8)
{
// Calculate scaling for the column and divide by 1/sqrt(2) if needed.
var columnScaling = 4 * Math.cos(Math.PI * column  * 0.0625);
if (column == 0) columnScaling /= oneOverSqrtTwo;

for (r in 0...8)
{
// Calculate scaling for the row and divide by 1/sqrt(2) if needed.
var rowScaling = 4 * Math.cos(Math.PI * r * 0.0625);
if (r == 0) rowScaling /= oneOverSqrtTwo;

// Scale quantization.
quantization[index] *= rowScaling * columnScaling;

index ++;
}
}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634061
• Total Posts
3015302
×