JPEG and H.264 Compression

Send the Iab report to . In your lab report, you should include the steps, necessary output (or screen shots), code (if any), and answer the questions.

Part I: DCT transform

Step 1: In Matlab, type help dctmtx, understand how to calculate dct coefficients for a matrix.

Step 2: Run the following examples to calculate DCT coefficients. Fill in the table 1 and answer questions. The lena_gray.tif can be downloaded from the link www.etcs.ipfw.edu/~luo/cpet581/Lectures/lena_gra y.tif

Ex1.

> A1 = ones(16,16);

> D1 =dctmtx(size(A1,1));

> dct1=D1*A1*D1';

> dct1

>imshow(dct1);

Ex2.

> A2 = rand(16,16);

> D2 =dctmtx(size(A2,1));

> dct2=D2*A2*D2';

> dct2

>imshow(dct2);

Ex3.

A3 = im2double(imread('lena_gray.tif'));

D3 = dctmtx(size(A3,1));

dct3 = D3*A3*D3';

> imshow(dct3)

Step 3. Fill the Table 1.

Table 1 DCT coefficients

DCT / DC coefficient / AC coefficients
Dct1
Dct2 / N/A
Dct3 / N/A

Step 4: Answer the questions:

What is the difference between the DC coefficients and AC coefficients of DCT1 and DCT2? Explain it.

Part Two: Get familiar with cif/qcif video

Step 1: Download the package video.zip from

http://www.etcs.ipfw.edu/~luo/cpet581/Lectures/video.zip

You can also go to http://trace.eas.asu.edu/yuv/index.html to download the other YUV testing video sequence.

Step 2: Unzip the video.zip. Unzip the YuvFilePlayer.zip.

Step 3: Open YUVFilePlayer.exe to play the cif/qcif video sequences. Display the luminance channel and chrominance channels of a frame.

Step 4: Answer the questions:

What is the resolution for forman_qcif.yuv, forman_cif.yuv, mobile_qcif.yuv?

Part Three: H.264 video codec

Step 1: Download the software

Download H.264/AVC video codec from

http://www.e tcs.ipfw.edu/~luo/cpet581/Lectures/h264.zip

You can also go to the official site to download the latest H.264/AVC reference software,

http://iphome.hhi.de/suehring/tml/download/old_jm/

There are 2 files in H264.zip, jm12.0.zip and a pdf file.

·  PDF file is the JM reference software manual. For information about the encoder and decoder of H.264, refer to this manual.

·  The jm12.0.zip is the H.264/AVC reference software. Unzip it. e.g., if you unzip the package to folder c:\cpet581, you will have folder c:\cpet581\jm12.0

Step 2: Compile the encoder and decoder.

1.  You must have Visual C++ compiler on your machine.

2.  Open the lencod.dsw under c:\cpet581\jm12.0\JM. This is the workspace file for the encoder. Compile this project. You can find the lencod.exe under c:\cpet581\jm12.0\JM\bin. Lencod.exe is the executable file for encoder.

3.  Open the ldecod.dsw under c:\cpet581\jm12.0\JM. This is the workspace file for the decoder. Compile this project. You can find the ldecod.exe under c:\cpet581\jm12.0\JM\bin. Ldecod.exe is the executable file for encoder.

4.  Copy forman_qcif.yuv, mobile_qcif.yuv to this folder c:\cpet581\jm12.0\JM\bin.

Step 3: Encode yuv video file using H.264 codec. Fill the table 3.

1.  Open a windows command window, go to the folder where the lencod.exe and ldecod.exe are located, using

cd c:\cpet581\jm12.0\JM\bin

Run the following commands to do encoding. Read the message displayed in the window and fill the table 3.

2.  To encode forman_qcif to forman1.264,

lencod -p InputFile="foreman_qcif.yuv" -p OutputFile="foreman1.264" -p FramesToBeEncoded=10

3.  To encode forman_qcif.yuv to forman2.264,

lencod.exe -p InputFile="foreman_qcif.yuv" -p OutputFile="foreman2.264" -p FramesToBeEncoded=7 -p FrameSkip=2 -p NumberBFrames=2

4.  To encode mobile_qcif.yuv to mobile1.264,

lencod -p InputFile="mobile_qcif.yuv" -p OutputFile="mobile1.264" -p FramesToBeEncoded=7

5.  To encode mobile_qcif.yuv to mobile2.264,

lencod.exe -p InputFile="mobile_qcif.yuv" -p OutputFile="mobile2.264" -p FramesToBeEncoded=5 -p FrameSkip=2 -p NumberBFrames=2

6.  Fill the Table 3

Table 3 H.264 Encoding

Output
264 file / GOP Display sequence / Number of
Frames in one GOP / Avg bit rates / Bits for I frame / Bits for the last B frame / Average PSNR Y
values
Forman1.264
Forman2.264
Mobile1.264
Mobile2.264

7. Answer the questions:

1)  Compare mobile1.264 and mobile2.264, which compression has higher PSNR values for Y image, explain why?

2)  Compare mobile1.264 and mobile2.264, which compression has higher average bit rates over all frames in this GOP, explain why?

3)  Compare foreman1.264 and mobile1.264, which compression has higher average bit rates over all frames in this GOP, explain why?

4)  The two sequence forman_qcif.yuv and mobile_qcif.yuv, which sequence can achieve higher compression ratio under the same encoding parameters? Explain why?

5)  Why I frames has a larger bits than B and P frames? Why generally B frames has least bits?

Step 4: Decoding

1.  Open a Windows command window, go to the directory where the ldecod.exe is located using

Cd c:\cpet581\jm12.0\JM\bin

Type the following commands to do decoding. Read the message displayed in the window and fill the table 4.

2.  To decode the forman1.264 to forman1.yuv

ldecod.exe -i foreman1.264 -o foreman1.yuv -r foreman_qcif.yuv

3.  To decode the forman2.264 to forman2. yuv

ldecod.exe -i foreman2.264 -o foreman2.yuv -r foreman_qcif.yuv

4.  To decode the mobile1.264 to mobile1.yuv

ldecod.exe -i mobile1.264 -o mobile1.yuv -r mobile_qcif.yuv

5.  To decode the mobile2.264 to mobile2.yuv

ldecod.exe -i mobile1.264 -o mobile1.yuv -r mobile_qcif.yuv

6.  Fill the Table 4.

Table 4 H.264 Decoding

Output YUV video file / Average SNR Y values
Foreman1.yuv
Foreman2.yuv
Mobile1.yuv
Mobile2.yuv

Part IV: JPEG Image Compression

This part is not required. It is working on the old version of MATLAB, but not working on the new version.

Step 1: Download the JPEG.zip files from the link,

http://www.etcs.ipfw.edu/~luo/cpet581/Lectures/JPEG.zip

It includes all the files needed in this lab.

Step 2: Unzip jpeg.zip to your local computer, e.g., c:\cpet581\jpeg. Open Matlab, set c:\cpet581\jpeg as the current working directory of Matlab. There should be a folder c:\cpet581\jpeg\dipum_1.1.4.

Step 3: Add DIPUM toolbox to Matlab.

In Matlab, click the file -> set path -> Add Folder, select the folder c:\cpet581\jpeg\dipum_1.1.4,

Click save -> close.

The path c:\cpet581\jpeg\dipum_1.1.4. is now in the search path.

Step 4: test jpeg compression and calculate psnr. Finish the table 2 and answer the questions.

1.  create an array to save the original pixel values

org=imread(‘lena_gray.gif’);

2.  compression the original image to jpeg1, using quality factor 1

jpg1 = im2jpeg(org, 1);

3.  decompression the jpeg1 to

new = jpeg2im(jpg1);

4.  display the reconstructed image

imshow(new);

5.  save the reconstructed image and view it in Explore

imwrite(new, ‘lena_new1.jpg’);

6.  calculate the psnr

psnr(org, new);

Step 5: repeat the Step 4, using different quality factors, such as 2, 5, 10.

Table 2 JPEG compression quality

Quality factor / PSNR
1
2
5
10

Step 6: Answer the questions:

1.  Read the program im2jpeg.m. What is the quantization table used in this compression?

2.  How does the quality factor change the quality of the jpeg?