We've just updated MediaWiki and its underlying software. If anything doesn't look or work quite right, please mention it to us. --RanAS

LZ5

From SnesLab
Jump to: navigation, search

LZ5 or LC_LZ5, as named by Lunar Compress, is a lossless data compression format, used by Super Mario Kart to compress graphics and other data. The LZ5 compression format is a slight enhancement of the LZ2 compression format, due to new commands.

Compression

The compressed data consists of "chunks", each with a header:

bits
76543210
CCCLLLLL
CCC:   Command bits
LLLLL: Length

If the header byte is $FF, the end of the compressed data has been reached, and decompression will be aborted.

The commands mostly remain their LC_LZ2 commands. However, there are new commands too. Here is a list of commands bits which the LC_LZ5 decompression function can use during the decompression:

000    "Direct Copy"
       Followed by (L+1) bytes of data
001    "Byte Fill"
       Followed by one byte to be repeated (L+1) times
010    "Word Fill"
       Followed by two bytes. Output first byte, then second, then first,
       then second, etc. until (L+1) bytes has been outputted
011    "Increasing Fill"
       Followed by one byte to be repeated (L+1) times, but the byte is
       increased by 1 after each write
100    "Repeat"
       Followed by a word (little-endian) containing address relative 
       to the output buffer's first byte to copy (L+1) bytes from.
       Basically it's this math: (Output buffer offset + word value)
101    "XOR Repeat"
       Same concept as above, except all the copied bytes are getting
       XOR'd by the value $FF.
110    "Negative Repeat", followed by a byte subtracted by the CURRENT index
       to the output buffer, to copy (L+1) bytes from.
       Basically it's this math: (Output buffer offset's index - byte value)
111    "Long length"
       This command has got a two-byte header:
       111CCCLL LLLLLLLL
       CCC:        Real command
       LLLLLLLLLL: Length
Long Length-exclusive command
111    "Negative XOR repeat"
       Same concept as "Negative Repeat", except all the copied bytes
       are getting XOR'd by the value $FF.

Seeing that the long length command uses ten bits for its length, the maximum length is 1024 bytes/1kB, while the normal length is 32 bytes.

Decompression

During the decompression, the decompressed chunks are outputted into a buffer which is either RAM or SRAM.

Usage

You can use the LZ5 compression to mainly compress graphics and data. Technically you can compress anything with this format.