Creating an MPEG from Other Sources

Getting Started

To allow access to all executables you will need to create an MPEG movie you must add the following to your path:

On HP Lab Machines: ~tgd/cs450/bin

In order to do this, add the statement:

On HP Lab Machines: set path=($path ~tgd/cs450/bin)

to your .cshrc file. Then, you must source your .cshrc file (in order for the changes to take effect in your current login session) by executing the command:

source .cshrc

There are a number of files in the directory you have just added to your path that will help you MPEG encode your animation. They are described below. All of the commands mentioned below should work exactly as specified below after this step is performed. This step must only be performed the first time once.

Preparing Frames

In order to MPEG encode your animation, you need to store your frames in separate files. Each file needs to be in either YUV or PPM format.

PPM File Format

To give you an idea of what a PPM file is, the following code will write one out:

    int **red, **green, **blue;

    void	WritePPM(char *fileName, int width, int height, int maxVal)
    {
	register int x, y;
	unsigned char   r, g, b;

	fprintf(stdout, "P6\n");
	fprintf(stdout, "%d %d\n", width, height);
	fprintf(stdout, "%d\n", maxVal);

	for ( y = 0; y < height; y++ )
	    for ( x = 0; x < width; x++ )
	    {
		r = red[x][y];  g = green[x][y];    b = blue[x][y];

		fwrite(&r, 1, 1, stdout);
		fwrite(&g, 1, 1, stdout);
		fwrite(&b, 1, 1, stdout);
	    }
    }
maxVal is the maximum color value. It must be between 0 and 255 inclusive. Generally speaking, it should be 255 always.

YUV File Format

You should be aware that the YUV format used in the MPEG encoder is DIFFERENT than the Abekas YUV format. The reason for this is that in MPEG, the U and V components are subsampled 4:1.

To give you an idea of what format the YUV file must be in, the following code will read in a YUV file:


    unsigned char **y_data, **cr_data, **cb_data;

    void	ReadYUV(char *fileName, int width, int height)
    {
	FILE *fpointer;
	register int y;

	/* should allocate memory for y_data, cr_data, cb_data here */

	fpointer = fopen(fileName, "r");

	for (y = 0; y < height; y++)			/* Y */
	    fread(y_data[y], 1, width, fpointer);

	for (y = 0; y < height / 2; y++)			/* U */
	    fread(cb_data[y], 1, width / 2, fpointer);

	for (y = 0; y < height / 2; y++)			/* V */
	    fread(cr_data[y], 1, width / 2, fpointer);

	fclose(fpointer);
    }

There are two reasons why you'd want to use YUV files rather than PPM files: The encoder can accept other YUV formats including ABEKAS and PHILLIPS. These need to be specified in the parameter file for mpeg_encode.

Other file formats.

I have written an RGB to PPM converter called rgb-to-ppm. It is available in the ~tgd/cs450/bin directory.

Preparing the Parameter File

The Multimedia Group at Berkeley has written this MPEG-1 encoder. It is controlled by a parameter file. Here is a sample parameter file. There is also a postscript User's Guide (Warning: this displays in reverse order.) I recommend that you read through the parameter file and the Users's Guide simultaneously, so that you understand what each parameter value is doing. Note that in this sample parameter file, I am using a program to convert from RGB format to PPM format.

Running the Encoder

Once the parameter file is prepared, you create the MPEG movie by typing.

mpeg_encode parameter-file-name

It will read your frames and write an output file according to the instructions in the parameter file.

Other Sources of Information

A good tutorial on MPEG is available. There is also a Frequently-Asked Questions listing.

Viewing an MPEG Movie

You can view an MPEG animation by typing:

mpeg_play file.mpg.

For example, you can view the ~tgd/cs450/sample.mpg animation on the HP system by typing:

mpeg_play ~tgd/cs450/sample.mpg

The argument -loop will play the movie in a continuous loop, e.g., mpeg_play -loop image.mpg.

Obtaining Your Own Copy of MPEG_ENCODE

Mpeg_encode is available via anonymous FTP from Berkeley. Look at the Berkeley Multimedia Group Page for pointers to various pre-compiled binaries, source code, documentation, etc.


Please send any questions or comments to:

Tom Dietterich : tgd@cs.orst.edu