JPEG Microcode

Introduction
The N64 JPEG library is an adapted version of the useful elements of the JPEG compression method customized for use in N64 game development.

Compression
The N64 JPEG library uses the Huffman entropy encoding method for data compression.

The Huffman method stores redundancy information, so it uses fewer bits to express data that appears frequently. As a result, the size of the compressed data is kept very small. Typically you can compress the original file down to 10% of its original size. Depending on the image, you may even be able to compress the file down to 5% of its original size. However, the speed of decompressing the compressed data slows things down. It typically takes more than 80 ms to decompress a 320x240 compressed image. Of course, the decompressing speed will vary depending on the image.

N64 JPEG Library
The N64 JPEG library consists of library functions, microcode, and tools.

Library Functions
There are two library functions that you can use for entropy encoding and decoding.

Microcode
There are two microcode object files that you can use for encoding and decoding.

Tools
There are three tools that you can use to compress an image to the proper form.

Library Functions
There are two N64 JPEG library functions, njpgHuffEncode and njpgHuffDecode.

njpgHuffEncode Function
Use the njpgHuffEncode function to encode and compress data using Huffman encoding.

Syntax

#include 
u32 njpgHuffEncode (u8 *indata, u8 *outdata, u32 size);

Arguments

Remarks
The Huffman method encodes the output data by using the njpgespMain microcode. The address of the input/output data buffer must be 4 bytes-alignment. Also, the input data size needs to be a multiple of 768 bytes (8x8 (unit process matrix) x 2 (bytes) x 6). The output data from the njpgespMain microcode is usually this size.

Note that the output data size of the njpgespMain microcode is one-and-a-half times the size of the original image. The value should be (the width of the original image) x (the height of the original image) x 2 (bytes) x 1.5.

Return Value
Data size after compression.


njpgHuffDecode Function
Use the njpgHuffDecode function to decompress data that was compressed by the Huffman encoding method.

Syntax

#include 
s32 njpgHuffDecode (u8 *indata, u8 *outdata);

Arguments

Remarks
This function decompresses the Huffman-encoded compression data. The address of the input/output buffer must be 4 byte-alignment.

If a function is called successfully, 0 is returned. If the call was unsuccessful, -1 is returned. The data which was output will be decoded again using the njpgdsMain microcode.

You should check the return value to see if the decoding process failed because the original data (the compressed data) may have been destroyed; for example, you could have saved the compression data on a disk or Controller Pak that was later destroyed. To be sure the file is valid, check to see if the top 4 bytes of the compression data is the recognition header �HUFF� (ASCII: 0x48554646), and check the data in the following 16 x 16 dot macro block.

Please see Section 27.5.4.1, "njpgCompress Tool," for details about the data format.

Example Code


if (* ( (u32*) indata)  !=  0x4e485546) {
   /* Error process */
   }

if (* ( (u32*) (indata + 4) )  != IMAGE_SIZE) {
   /* Error process */
   }

njpgHuffDecode (indata, outdata);
/* Decoding by njpgdspMain microcode */


Microcode
N64 JPEG microcode does the high-speed compression and decompression of N64 JPEG files from the N64 original format.

There are the two kinds of microcode:

As the application programmer, you can choose the microcode appropriate for your game application.

The Decompression Microcode (njpgdspMain)
The decompression microcode processes the image files that have been compressed.

The Decompression Microcode�s Input Data
The N64 JPEG format has two operation blocks for image compression, a sub-block (SB) and a macro block (MB).

The following diagram shows the RSP output data format during compression (or RSP input data format during decompression):

How to Activate the Decompression Microcode
The procedure for starting the expansion microcode, njpgdsMain, is described below.

First, include the header-file n64jpeg.h in the source code for your game application, and include njpgdspMain.o in the spec file for makerom. Then establish the OSTask structure as follows:


type                      M_NJPEGTASK
flags                     0x0
*ucode_boot               (u64 *) rspbootTextStart
ucode_boot_size           SP_BOOT_UCODE_SIZE
*ucode                    (u64 *) njpgdspMainTextStart
ucode_size                SP_UCODE_SIZE
*ucode_data               (u64 *) njpgdspMainDataStart
ucode_data_size           SP_UCODE_DATA_SIZE
*dram_stack               NULL
dram_stack_size           0
*output_buf               NULL
*output_buff_size         NULL
*data_ptr                 Pointer to the NJPEGDParam parameter structure
data_size                 NJPEGD_PARAM_SIZE
*yield_data_ptr           Pointer to the yield buffer
yield_data_size           NJPEGD_YIELD_SIZE

The address of the yield buffer must conform to 16-byte alignment, and it must be 272 bytes in size.

The NJPEGDParam structure is defined as follows:


typedef struct {
   u64 *buffer;     /* input/output data buffer address */
   u32 mbs;         /* number of all the macro blocks */
   u32 scale;       /* quantization scale (-2, -2, 0, 1, 2) */
   u32 dummy;
} NJPEGDParam;

In the header address of the input/output data buffer, enter the output address for the CPU decoding process that will be done by njpgHuffDecode function. Microcode overwrites the decompressed input data in RAM, so you don�t have to specify the output address for the microcode.

The microcode processes and outputs the image data with a macro block of 16x16 (length x width) dots (pixels). For the mbs argument (the number of all macro blocks), enter the number macro blocks in the original image data. For example, if the size of the original image data is 240x 320 (length x width) dots, the total number of macro blocks should be 300, which is 15 (length) x 20 (width).

For the scale argument (the quantization scale value), enter the specified quantization scale value during the compression. The larger this value is, the higher the compression rate becomes and the shorter the decompression time. But the image quality becomes worse. Choose a value appropriate for your image.

You can specify any value in the range from -2 to 2 as the quantization scale value. Any other value may cause unexpected results. If you specify 0, the reverse-quantization is not done.

After the CPU decoding process ends and the OSTask structure is prepared, you can start to use the microcode. Call the osSpTaskStart function. It works in the same way as graphics or audio microcode.

Example Code


OSTask njpgdtlist;
      .
      .
      .


/* OSTask structure njpgdtlist set /*
      .
      .
      .


/* Start RSP Processing */
osWritebackDCacheAll();
osSpTaskStart(&njpgdtlist);
      .
      .
      .


/* Wait for the end of RSP processing */
osRecvMesg(&rspMessageQ, NULL, OS_MESG_BLOCK);

The Decompression Microcode�s Output Data
Data output by the decompression microcode has a format such that each graphics microcode is usable for the YUV texture. For more information about the 16-bit YUV texel format, please see Section 13.8, "Texture Memory" (figure 13.8.6).

The decompression microcode�s output data overwrites the input data of each macro block, so you don�t have to prepare another microcode output buffer.

The following diagram shows the format of the data output by the decompression microcode. To render a graphic image, you simply paste the microcode�s output data (YUV texels) shown in the following diagram into the proper places on the screen in each macro block. There are many rendering methods you can use. For example, you can use this output data with the sprite microcode.

The Compression Microcode (njpgespMain)
The compression microcode compresses 16-bit RGBA formatted images into N64 JPEG format.

The Compression Microcode�s Input Data
This microcode can only encode (compress) 16-bit RGBA formatted image data format. No other format can be encoded. With regard to the size of the graphic data, the only restriction is that the length and width each conform to a dot multiple of 16. For example, 320 x 240 dots will work because both the length and width are evenly divisible by 16.

The microcode directly divides and encodes the image data into 16x16 dot macro blocks (MBs), so it�s impossible to cut and process a piece of a large image. For example, it�s impossible to cut out a 240x160 piece from a drawn 320x 240 dot image in a frame buffer to use directly as input data for the microcode without erasing it from the frame buffer. However, after first cutting out the piece for another buffer in the application, you can then use the cut-out data as the input data for the microcode.

How to Activate the Compression Microcode
First, include the n64jpeg.h header file in the source code for your game application, and include the njpgespMain.o file in the spec file for makerom. Then set the OSTask structure as follows:


type                      M_NJPEGTASK
flags                     0x0
*ucode_boot               (u64 *) rspbootTextStart
ucode_boot_size           SP_BOOT_UCODE_SIZE
*ucode                    (u64 *) njpgespMainTextStart
ucode_size                SP_UCODE_SIZE
*ucode_data               (u64 *) njpgespMainDataStart
ucode_data_size           SP_UCODE_DATA_SIZE
*dram_stack               NULL
dram_stack_size           0
*output_buf               NULL
*output_buff_size         NULL
*data_ptr                 Pointer to the NJPEGEParam parameter structure
data_size                 NJPEGE_PARAM_SIZE
*yield_data_ptr           Pointer to the yield buffer
yield_data_size           NJPEGE_YIELD_SIZE

The OSTask structure is used to transfer the data to the RCP. The address of the yield buffer must conform to 16-byte alignment, and it must be 288 bytes in size for encoding.

The NJPEGEParam structure is defined as follows:


typedef struct {
   u64 *input;      /* input data buffer address */
   u64 *output;     /* output data buffer address */
   u32 mbs_x;       /* number of macro blocks in the x direction - 1 */
   u32 mbs_y;       /* number of macro blocks in the y direction - 1 */
   u32 scale;       /* quantization scale (-2, -2, 0, 1, 2) */
   u32 dummy1;
   u32 dummy2;
   u32 dummy3;
} NJPEGEParam;

The quantization scale value is a coefficient to be multiplied by each value in a quantization table when quantizing. There are five possible scale values: -2, -1, 0, 1, and 2 which represent �, �, don�t use the quantization table, 1, and 2 respectively. When the quantization table isn�t used, the output value of the discrete cosine transformation is used only for zigzag scanning.

After completing each setup item outlined above, call the osSpTaskStart function to activate the compression microcode in the same way you would activate graphics or audio microcode.

Example Code


OSTask njpgetlist;
      .
      .
      .


/* OSTask structure njpgetlist set */
      .
      .
      .


/* Start RSP Processing */
osWritebackDCacheAll();
osSpTaskStart(&njpgetlist);
      .
      .
      .


/* Wait for the end of RSP processing */
osRecvMesg(&rspMessageQ, NULL, OS_MESG_BLOCK);

The Compression Microcode�s Output Data
In each macro block (MB), the microcode converts the data from RGB to YUV format, reduces (culls) the data, performs discrete cosine transformation, quantizes the data, and zigzag scans the data.

You can use culling (in the ratio of 4:1:1) and the quantization table to make necessary modifications to the data. You can change the quantization table by changing the quantization scale factor.

Each macro block must be 768 bytes in size to hold the output data buffer. For example, an image of 320 x 240 dots is divided into a total of 300 MBs. Therefore, a 230,400-byte region is required for the output buffer. Each application must prepare this buffer.

Tools

njpgCompress Tool
Use the njpgCompress tool to compress a 16-bit RGBA-format graphic image into N64 JPEG format.

Syntax

njpgCompress [-w width] [-h height] [-s scale] [-v] inFile outFile

Command Line Options
The following command line options are available for the njpgCompress tool:

njpgMakeSlide Tool
Use the njpgMakeSlide tool to compress one or more 16-bit RGBA-format graphic images into N64 JPEG format and place the result into a single file. This is useful for making a file of all background images or for using images continuously as slides or flip animation pictures.

Syntax

njpgMakeSlide [-w width] [-h height] [-s scale] [-v] inFile outFile

Command Line Options
The following command line options are available for the njpgMakeSlide tool:

rgb2u64 Tool
Each original image file must be in the 16-bit RGBA format before it can be compressed into the N64 JPEG format. You can use this rgb2u64 tool to convert an image from RGB format into 16-bit RGBA format.

This tool must be used on an SGI workstation.

Syntax

rgb2u64  [x_size y_size]> 

Explanation
This tool converts images in RGB format to 16-bit RGBA format.

Type the command from the console to use this tool.

Be sure to use the redirection symbol (>) to send the output to the output file.

General Reference Material

N64 JPEG Compression Process
The N64 JPEG compression method is based on the standard JPEG process. See the JPEG standard for a complete description of the exact process. Following is a brief explanation of the N64 JPEG compression process. The procedure for decompressing compressed data is basically the opposite of this.

  1. Convert to the YUV Format
    The N64 compression process first converts data from 16-bit RGBA format to YUV format. During this process, less essential visual data (data that is less important in human vision) is culled. The resulting YUV data format is easier to compress.

    First the data is converted from RGB to YCrCb format. The Y value holds the luminance (brightness) information and Cr and Cb values hold the chrominance (color) information:

    
    r = R/255, g = G/255, b = B/255
           (0 � R � 255, 0 � G � 255, 0 � B � 255)
           (0 � r � 1, 0 � g � 1, 0 � b � 1)
    
    Y  = 0.299r + 0.587g + 0.114b
    Cr = 0.713(r - Y)
    Cb = 0.564(b - Y)
           (0 � Y � 1, -0.5 � Cr � 0.5, -0.5 � Cb � 0.5)
    

    Then the YCrCb formatted data is converted to the YUV format:

    
    y = 219Y + 16
    u = 224Cb + 128
    v = 224Cr + 128
           (0 � y � 255, 0 � u � 255, 0 � v � 255)
    

  2. Cull Less Essential Data
    Human eyes are relatively insensitive to UV components compared to the Y component. Thus, culling the UV components can decrease the amount of data without a large noticeable change. This process extracts four 8x8 blocks of the Y component and one 8x8 block of each UV (U and V) component from the 16x16 dots (macro block).

  3. Perform DCT
    DCT (discrete cosine transformation) regulates the apparently random image to ease the compression process.

    When processing an 8x8 matrix, DCT calculates an average value (the DC coefficient) of the data elements in the matrix and places it in the [0][0] position of the transformed matrix. Then it calculates the variance (the AC coefficient) from that DC coefficient for each of the other 63 cells of the 8x8 matrix. The farther the AC element is from the position of the DC element, the higher the displayed frequency element is.

  4. Reduce the Data Range Through Quantization
    The range of data after DCT is wide, and the wider the range the less compression, so you need to reduce the range. To reduce the range, divide each value by a specific value. Human vision is less sensitive to high frequency components than it is to low frequency components, so you should divide high frequency components by a large value and divide low frequency components by a small value.

  5. Perform Zigzag Scanning
    The DCT and quantization processes concentrate on the DC components and low frequency components. The high frequency components don�t change much and the data concentrates around 0. That is, large changes in data concentrate around cell [0][0] of the 8x8 pixel matrix after DCT, and many zeros concentrate around cell [7][7]. The continuous appearance of these zeros is profitable for compression, so this step rearranges the data to take advantage of the large concentration of zeros. One way to do this is to apply zigzag scanning to the data from [0][0] to [8][8] like this: [0][0] � [1][0] � [0][1] � [0][2] � [1][1] � [2][1]... and so on.

    DC components have differential data of DC components in the previous block so that the range of data values becomes smaller.

    Note that the N64 JPEG process does the scanning after first inverting the matrix. This is different from normal JPEG zigzag scanning. It is done this way to ease microcode decompression of compressed data.

  6. Do the Huffman Entropy Encoding
    The previous five steps converted the random image into a compressible form. Now this final step completes the Huffman entropy encoding to compress the image data.

Output Data Formats

njpgMakeSlide
To move data output by this tool into the game application, first use DMA to load all images into RAM until numImages (the number of included images) is reached. After checking the necessary information following numImages, use DMA again to transfer frame[] into RAM. Then if you also need the image data, use DMA again to transfer data[] into RAM.

typedef struct{
   s32     size;
   s32     offset;
} MVFrame;

struct{
   u8 headerID[] = 'NJPG'
   s32 version;
   s16      width;
   s16      height;
   u8       rawMode;
   u8       compressType;
   s16      qScale;
   s32      numImages;
   MVFrame  frame[];
   u8       data[];
};

njpgCompress
The data output by the njpgCompress tool is suited to only a part of the data output by the njpgMakeSlide tool. Be careful that the special supplemented header is not changed. Note that the minimum header required for data decompressing has been supplemented. The following is the data format. Refer to it to check the data when decompressing:

struct{
   u8 headerID[] = "HUFF";
   s16      numMB;       /* number of macro blocks */
   u8       compData[];
};

Quantization Table
The quantization table for both luminance (brightness) and chrominance (color) is adapted to the luminosity quantization table of quantization table in the attachment K (reference) �Examples and guide line� of ITU-T advice, T.81 �The digital compression and coding of continuous-tone still images.� The quantization table has been organized to make RSP calculation faster when decompressing the compressed data. The following is the table:


static short QTable[64] = {
16,  11,  10,  16,  24,  40,  51,  61,
12,  12,  14,  19,  26,  58,  60,  55,
14,  13,  16,  24,  40,  57,  69,  56,
14,  17,  22,  29,  51,  87,  80,  62,
18,  22,  37,  56,  68, 109, 103,  77,
24,  35,  55,  64,  81, 104, 113,  92,
49,  64,  78,  87, 103, 121, 120, 101,
72,  92,  95,  98, 112, 100, 103, 99
};

As a practical matter, this table is used by multiples of �, �, 1, and 2 (these correspond to the scale values -2, -1, 1 and 2). When the value of the quantization table is small, the image quality goes up but the size after compression is larger. On the other hand, when the value of the quantization table is large, the size after the compression is small but the image quality is worse.

Huffman Encoding Table
The Huffman encoding table is the one in the attachment K (reference) �Examples and Guidelines� of the ITU-T advice, T.81 �Digital compression and encoding of continuous-tone still images.�

Following are the four tables. The first and second show the possible DC codes for luminance and chrominance, and the third and fourth show the possible AC codes for luminance and chrominance.

Table of DC Codes for Luminance (Brightness)

Classification Code Length Code Word
020x0000
110x0002
230x0003
330x0004
430x0005
530x0006
660x000e
750x001e
860x003e
970x007e
1080x00fe
1190x01fe

Table of DC Codes for Chrominance (Color)

Classification Code Length Code Word
020x0000
110x0001
220x0002
330x0006
440x000e
550x001e
660x003e
770x007e
880x00fe
990x01fe
10100x03fe
11110x07fe

Table of AC Codes for Luminance (Brightness)

Run Size Code Length Code Word
0/0 (EOB) 4 0x000a
0/120x0000
0/220x0003
0/330x0004
0/440x000b
0/550x001a
0/670x0078
0/780x00f8
0/8100x03f6e
0/9160xff82
0/A160xff83
1/140x000c
1/250x001b
1/370x0079
1/5110x07f6
1/6160xff84
1/7160xff85
1/8160xff86
1/9160xff87
1/A160xff88
2/150x001c
2/280x00f9
2/3100x03f7
2/4120x0ff4
2/5160xff89
2/6160xff8a
2/7160xff8b
2/8160xff8c
2/9160xff8d
2/A160xff8e
3/160x003a
3/290x01f7
3/3120x0ff5
3/4160xff8f
3/5160xff90
3/6160xff91
3/7160xff92
3/8160xff93
3/9160xff94
3/A160xff95
4/160x003b
4/2100x03f8
4/3160xff96
4/4160xff97
4/5160xff98
4/6160xff99
4/7160xff9a
4/8160xff9b
4/9160xff9c
4/A160xff9d
5/170x007a
5/2110x07f7
5/3160xff9e
5/4160xff9f
5/5160xffa0
5/6160xffa1
5/7160xffa2
5/8160xffa3
5/9160xffa4
5/A160xffa5
6/170x007b
6/2120x0ff6
6/3160xffa6
6/4160xffa7
6/5160xffa8
6/616 0xffa9
6/7160xffaa
6/8160xffab
6/9160xffac
6/A 160xffad
7/180x00fa
7/2 12 0x0ff7
7/3160xffae
7/4160xffaf
7/5160xffb0
7/6160xffb1
7/7160xffb2
7/8160xffb3
7/9160xffb4
7/A160xffb5
8/190x01f8
8/2150x7fc0
8/3160xffb6
8/4160xffb7
8/5160xffb8
8/6160xffb9
8/7160xffba
8/8160xffbb
8/9160xffbc
8/A160xffbd
9/190x01f9
9/290xffbe
9/390xffbf
9/490xffc0
9/590xffc1
9/690xffc2
9/790xffc3
9/890xffc4
9/990xffc5
9/A90xffc6
A/190x01fa
A/2160xffc7
A/3160xffc8
A/4160xffc9
A/5160xffca
A/6160xffcb
A/7160xffcc
A/8160xffcd
A/9160xffce
A/A160xffcf
B/1100x03f9
B/216 0xffd0
B/316 0xffd1
B/416 0xffd2
B/516 0xffd3
B/616 0xffd4
B/716 0xffd5
B/8160xffd6
B/9160xffd7
B/A160xffd8
C/1100x03fa
C/2160xffd9
C/3160xffda
C/4160xffdb
C/5160xffdc
C/6160xffdd
C/7160xffde
C/8160xffdf
C/9160xffe0
C/A160xffe1
D/1110x07f8
D/2110xffe2
D/3110xffe3
D/4160xffe4
D/5160xffe5
D/6160xffe6
D/7160xffe7
D/8160xffe8
D/9160xffe9
D/A160xffea
E/1160xffeb
E/2160xffec
E/3160xffed
E/4160xffee
E/5160xffef
E/6160xfff0
E/7160xfff1
E/8160xfff2
E/9160xfff3
E/A160xfff4
F/0(ZRL)110x07f9
F/1160xfff5
F/2160xfff6
F/3160xfff7
F/4160xfff8
F/5160xfff9
F/6160xfffa
F/7160xfffb
F/8160xfffc
F/9160xfffd
F/A160xfffe

Table of AC Codes for Chrominance (Color)

Run Size Code Length Code Word
0/0(EOB)20x0000
0/120x0001
0/230x0004
0/340x000a
0/450x0018
0/550x0019
0/660x0038
0/770x0078
0/890x01f4
0/9100x03f6
0/A120x0ff4
1/140x000b
1/260x0039
1/380x00f6
1/490x01f5
1/5110x07f6
1/6120x0ff5
1/7160xff88
1/8160xff89
1/9160xff8a
1/A160xff8b
2/150x001a
2/280x00f7
2/3100x03f7
2/4120x0ff6
2/5150x7fc2
2/6160xff8c
2/7160xff8d
2/8160xff8e
2/9160xff8f
2/A160xff90
3/150x001b
3/280x00f8
3/3100x03f8
3/4120x0ff7
3/5160xff91
3/6160xff92
3/7160xff93
3/8160xff94
3/9160xff95
3/A160xff96
4/160x003a
4/290x01f6
4/3160xff97
4/4160xff98
4/5160xff99
4/6160xff9a
4/7160xff9b
4/8160xff9c
4/9160xff9d
4/A160xff9e
5/1160x003b
5/2160x03f9
5/3160xff9f
5/4160xffa0
5/5160xffa1
5/6 160xffa2
5/7160xffa3
5/8160xffa4
5/9160xffa5
5/A 160xffa6
6/170x0079
6/2110x07f7
6/3160xffa7
6/4160xffa8
6/5160xffa9
6/6160xffaa
6/7160xffab
6/8160xffac
6/9160xffad
6/A160xffae
7/170x007a
7/2110x07f8
7/3160xffaf
7/4160xffb0
7/5 160xffb1
7/6160xffb2
7/7160xffb3
7/8160xffb4
7/9160xffb5
7/A 160xffb6
8/1 80x00f9
8/2160xffb7
8/3160xffb8
8/4160xffb9
8/5160xffba
8/6160xffbb
8/7160xffbc
8/8160xffbd
8/9160xffbe
8/A160xffbf
9/190x01f7
9/2160xffc0
9/3160xffc1
9/4160xffc2
9/5160xffc3
9/6160xffc4
9/7 160xffc5
9/8160xffc6
9/9160xffc76
9/A160xffc8
A/190x01f8
A/2160xffc9
A/3160xffca
A/4 160xffcb
A/5160xffcc
A/6160xffcd
A/7160xffce
A/8160xffcf
A/9 160xffd0
A/A160xffd1
B/190x01f9
B/2160xffd2
B/3160xffd3
B/4160xffd4
B/5160xffd5
B/6160xffd6
B/7160xffd7
B/8160xffd8
B/9160xffd9
B/A160xffda
C/190x01fa
C/2160xffdb
C/3160xffdc
C/4160xffdd
C/5160xffde
C/6160xffdf
C/7160xffe0
C/8160xffe1
C/9160xffe2
C/A160xffe3
D/1110x07f9
D/2160xffe4
D/3160xffe5
D/4160xffe6
D/5160xffe7
D/6160xffe8
D/7160xffe9
D/8160xffea
D/9160xffeb
D/A160xffec
E/1140x3fe0
E/2160xffed
E/3160xffee
E/4160xffef
E/5160xfff0
E/6160xfff1
E/7160xfff2
E/8 160xfff3
E/9160xfff4
E/A160xfff5
F/0 (ZRL)100x03fa
F/1150x7fc3
F/2160xfff6
F/3160xfff7
F/4160xfff8
F/5160xfff9
F/6160xfffa
F/7160xfffb
F/8160xfffc
F/9160xfffd
F/A160xfffe

The standard JPEG format distinguishes between the marker for defining the image information and the regular data when outputting the data after Huffman encoding. Therefore, if it encounters the same code (0xFF) as the data, it inserts 0x00 to indicate that this code is not the marker. However, N64 JPEG doesn�t provide this process since it doesn�t use the marker.

Programming Cautions

Image Quality
N64 JPEG raises the compression rate by slightly reducing the image quality. Therefore, if you repeatedly compress and decompress the compressed image, image quality can become noticeably worse.

Also, the N64 JPEG process includes errors, so you must be particularly careful when culling the UV data in the YUV space. For example, if you make an N64 drawing tool for use over and over in the N64 JPEG library for the purpose of image storage and retrieval, image encoding and decoding are provided whenever you repeat the storage and retrieval process. This means that if the drawing is done in the RGBA space (in the regular N64 image process space), UV data in the macro block is culled again and again based on the drawing. In short, an application that repeatedly compresses and decompresses a compressed image provides data culling over and over which can cause a noticeable deterioration in image quality.

If you make such an application, you should store the regular RGBA data without using compression or store the compressed data (for example, by the run length encoding as in the compression used in a BMP file), and use N64 JPEG as the final storage method.

Data Form after Decompression
Data decoded by the njpgdspMain microcode is output in YUV format, not RGBA format. Therefore, you need to be aware that the image won�t be displayed normally, even if you directly specify the output data buffer to the frame buffer.

Also, data output in YUV format needs to be expressed as a 16x16 dot texture. Both input and output data regions are divided into 768-byte macro blocks, and each macro block processed completes in its region. As a result, the size of the output data becomes 512 byte for the 768 byte (Y4, U1, and V1 are each 128 bytes) after input to the macro block region. The 256-byte empty region (suitable for the unnecessary UV component region) is produced.

When you get the texture from the data buffer, add the pointer to the buffer at each 768-byte boundary. Please see Section 27.3.1.3, "Decompression Microcode's Output Data."

Please see the sample program for the specific methods.


Copyright © 1999
Nintendo of America Inc. All Rights Reserved
Nintendo and N64 are registered trademarks of Nintendo
Last Updated January, 1999