Recently, I re-read Iain E. Richardson’s book: The H.264 Advanced Video Compression Standard, 2nd edition. I always focused on the theoretically part of the book and never pay attention to the detail of the calculation. But today I hit upon to check calculation of the book using Python for fun. The first part of the calculation I tried to check is about DCT which is printed on page 43-48. And I found that there maybe a mistake of figure 3.31 DCT coefficients on page 47.

I use two ways to check the result of DCT coefficients.

The first way is to use matrix A provided by the book to calculate the Y of X. Here is the formula:

and here is the matrix of A:

The code is very simple, here is the sample code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import numpy as np

#get a 4x4 block
X = np.array([[126, 159, 178, 181],
              [98, 151, 181, 181],
              [80, 137, 176, 156],
              [75, 114, 88, 68]])

A = np.array([[0.5, 0.5, 0.5, 0.5],
              [0.653, 0.271, -0.271, -0.653],
              [0.5, -0.5, -0.5, 0.5],
              [0.271, -0.653, 0.653, -0.271]])

#first way, use formula Y=A.X.AT to generate DCT coefficients
temp = np.dot(A, X)
temp = np.round(temp, 1)
Y = np.dot(temp, A.T)
Y = np.round(Y, 1)

 

The result of Y is:

The second way is to use the dct function of SciPy itself. Here is the sample code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt
import numpy as np
from scipy.fftpack import fft, dct
import scipy

def img2dct(a):
    return scipy.fftpack.dct( scipy.fftpack.dct( a, axis=0, norm='ortho' ), axis=1, norm='ortho' )

def dct2img(a):
    return scipy.fftpack.idct( scipy.fftpack.idct( a, axis=0 , norm='ortho'), axis=1 , norm='ortho')

#get a 4x4 block
X = np.array([[126, 159, 178, 181],
              [98, 151, 181, 181],
              [80, 137, 176, 156],
              [75, 114, 88, 68]])

# second way, use python dct function to generate DCT coefficients
Y = np.round(img2dct(X), 1)

X_revert = np.round(dct2img(dct_python), 0)

 

The result of Y is following which is the same as the first way:

But the result printed on page 47 is:

The second row and fourth row of the DCT coefficients is not the same as my result. The sign of the numbers is just the opposite! I also use inverse DCT to check my result, the inverse DCT result is the same as X. So I think the printed numbers on the book has some mistakes.