About MPEG-2


Below is a table of contents:

  1. What is MPEG-2?
  2. How do you compile the MPEG-2 encoder/decoder source code?
  3. How do you optimize the MPEG-2 parameter file?
  4. How do you use the MPEG-2 Encoder?

What is MPEG-2 ?

The MPEG-2 standard is similar to the MPEG-1 standard, but it also has additional features for a wider range of applications. MPEG-2 is primary targeted for applications with higher bit rates. One such application is the transmission of all-digital broadcast quality TV.

The most significant enhancement MPEG-2 has over MPEG-1 are functions that allow for coding of interlaced video. TV is based on fields (60 fields/s) which is interlaced video. It takes 2 fields to construct a frame. This field coding feature allows more efficient compression for broadcast quality TV or HDTV.

There are also other subtle enhancements (e.g. 10-bit DCT precision, non-linear quantization, VLC tables, improved mismatch control) in MPEG-2. All of these features give a noticeable improvement on coding efficiency and quality at the expense of increased computation time. Another key feature of MPEG-2 are scaleable extensions. This allows video to be coded as two or more bit streams representing different resolutions. There can be a base layer, so everyone with a regular TV can view. The additional layers on top of the base layer can be added onto the base layer for higher quality video images like HDTV. The MPEG-2 encoder we have has most of the enhancements with the exception of scaleable extensions.

How do you compile the MPEG-2 encoder/decoder?

There currently is no widely available MPEG-2 encoder that supports all the features of MPEG-2. But there is an MPEG-2 encoder/decoder that offers much of the improvements in the MPEG-2 standard, but without the scalability. The source code for the MPEG-2 encoder/decoder and player is available by anonymous FTP from here. After you get these files, change to your home directory. Uncompress the source files by typing:

If you need to compile the source code, make sure you have gcc (v2.6), otherwise it will not compile properly. You also need to change the following in the Makefiles.

  1. You will need to uncomment these lines in the Makefile for the MPEG-2 encoder and decoder. This will be in the subdirectory mpeg2.

    # uncomment the following two lines if you want to include X11 support
    # (mpeg2decode)
    USE_DISP = -DDISPLAY
    LIBS = -lX11

    # uncomment the following two lines if you want to use shared memory
    # (faster display if server and client run on the same machine)
    # (mpeg2decode)
    USE_SHMEM = -DSH_MEM
    LIBS = -lXext -lX11

    # if your X11 include files / libraries are in a non standard location:
    # set INCLUDEDIR to -I followed by the appropriate include file path and
    # set LIBRARYDIR to -L followed by the appropriate library path
    # (mpeg2decode)
    INCLUDEDIR = -I/usr/include/X11R5
    LIBRARYDIR = -L/usr/lib/X11R5

    Be sure to have these paths. They are needed in order to compile correctly.

  2. You need to also uncomment these lines in the Makefile for the MPEG-2 player. This will be in the subdirectory mpeg2play.

    # uncomment the following two lines if you want to use shared memory
    # (faster display if server and client run on the same machine)
    USE_SHMEM = -DSH_MEM
    LIBS = -lXext -lX11

    # uncomment the following line to activate calculation of decoding speed
    # (frames per second) and frame rate control (-fn option)
    USE_TIME = -DUSE_TIME

    # if your X11 include files / libraries are in a non standard location:
    # set INCLUDEDIR to -I followed by the appropriate include file path and
    # set LIBRARYDIR to -L followed by the appropriate library path and
    INCLUDEDIR = -I/usr/include/X11R5
    LIBRARYDIR = -L/usr/lib/X11R5

    Be sure to have these paths. They are needed in order to compile correctly.

After compiling these files, you will have these files in directories:

Use mpeg2encode* to encode your files, and mpeg2play* to play your files. mpeg2decode* will also play the output files as well. Copy these files into your ~/bin directory. Afterwards, then change to your home directory and resource your .cshrc file.

How do you optimize the MPEG-2 parameter file?

First you will need to get the sample parameter file. Click here to download it. Then edit the MPEG-1.par file.

  1. You will need to change the file paths, the number of frames, and the type of the input video sequence. This is similar to:

    test/image.%d /* name of source files */

    test
    is the path, image is the video sequence. %d is a C/C++ syntax indicating the sequence occurs like {1 2 3 4 5 6 .... } If the sequence is like {001 002 003 004... }, you would use %3d. The actual filenames of the input video sequence might look like {image.001.ppm, image.002.ppm, etc.}.

    2 /* input picture file format: 0=*.Y,*.U,*.V, 1=*.yuv, 2=*.ppm */
    10 /* number of frames */
    0 /* number of first frame */

    Since we are using *.ppm format, we must select 2. We are also setting the number of frames to be 10, with the first frame sequence starting at 0.

  2. Next we need to set the frame size of the video sequence:

    352 /* horizontal_size */
    240 /* vertical_size */
    5 /* frame_rate_code 1=23.976, 2=24, 3=25, 4=29.97, 5=30 frames/sec. */

    There is another section with two more frame size parameters to set.

    352 /* display_horizontal_size */
    240 /* display_vertical_size */

    Since we need 352x240 at 30fps, we select these settings.

    The rest of the parameters don't need to be optimized. Most of the parameters seem to already be optimized.

How do you use the MPEG-2 Encoder?

An MPEG-2 encoder can also encode MPEG-1 video streams if the correct parameters are specified. It is somewhat slower than Berkeley's MPEG-1 encoder. When compressing a video sequence, there is a noticeable improvement in disk savings. In order to encode MPEG-1 and MPEG-2 files, you will need the following files:

First you will need the appropriate video sequence in the right file format. The MPEG-2 encoder will only accept *.YUV, separate (*.Y *.U *.V), and *.PPM file formats. If you are using DataExplorer's MPEGmac.net file, the macro generates *.rgb files. You will need to convert the *.rgb files over to a usable file format. With Berkeley's MPEG-1 encoder, you can specify the input files be converted to *.PPM. But with the MPEG-2 encoder, you need to pre-process the files before you can encode the files. One way to do this is to create a script to automate the process. The macro generates *.rgb files similar like (i.e. image.001.rgb, image.002.rgb, etc.). To convert *.rgb files to *.ppm files, you can do it file by file. At the command line type:

rawtoppm 352 240 image.001.rgb > image.001.ppm

The 352 240 is used to set the width and height (standard MPEG-1 size) of the video sequence. The number in image.001.rgb is the frame number. This will convert the rgb file to required ppm format of the MPEG-2 encoder. A test was performed on a flower garden video sequence (60 frames, 352x240 pixels). This is a common video sequence used to compare video compression techniques.

To encode the sequence you would type:

mpeg_encode flower.param

To view the sequence you would type:

mpeg_play flowgard.mpg

Or you can also try, the MPEG-2 player, which looks better because it chooses a better color palette.

mpeg2play flowgard.mpg

To encode the sequence you would type:

mpeg2encode flower2.par flowgard.m2v

To view the sequence you would type:

mpeg2play flowgard.m2v

The quality between the two video sequences seem to be identical. It is very hard to notice any difference. The parameters (flower2.par) specified for the MPEG-2 encoder allowed us to create an MPEG-1 standard file.

There was a net savings of 81K (or about 22.3%) in disk space. The reason for the significant file savings is due to the way the MPEG-2 encodes each frame and the predicted frames. MPEG-2 is able to optimize the video sequence better than MPEG-1 through more efficient and better prediction modes.


Please send any questions or comments to:

Bruce Land : bruce@tc.cornell.edu
Frank Jary : fjary@tc.cornell.edu