LZ5
From M.U.G.E.N Wiki
What is an LZ5 compressed image?
It is a stream of LZ5 packets.
What is an LZ5 packet?
There is a control packet, aka "control byte". There are also four kinds of data packets.
- Short RLE
- Long RLE
- Short LZ
- Long LZ
What is the ordering of packets?
Control packet followed by 8 data packets. This is repeated until the end of the stream. The last control packet may be followed by fewer than 8 data packets if the stream ends there.
How is it decoded?
Decompressed data is written into a caller-allocated buffer after each data packet is processed. Note that processing of later packets may depend on the decompressed data output from earlier packets.
What is the control packet?
The control packet is a collection of 8 bitflags. The flag in bit #n determines whether the n'th data packet after this control packet is an RLE packet or an LZ packet. The control packet is always followed by as many data packets are available, up to a maximum of 8. If there are fewer than 8 data packets available (because of end of stream) then the value of the unused bitflags is unspecified.
What is a short RLE packet?
It outputs 1--7 pixels of a single color.
- Byte 1
bits 0-4: pixel color
bits 5-7: number of times to output
What is a long RLE packet?
It outputs 8--263 pixels of a single color.
- Byte 1
bits 0-4: pixel color
bits 5-7: 0
- Byte 2
- bits 0-7: number of times to output this pixel, minus 8
What is a naive memcpy?
It is a memcpy that does the naivest possible thing in the case of overlapping src and dst buffers. It is used by LZ packets to copy a previous run of decompressed data. A reference implementation is given below.
void naivememcpy(byte *dst, const byte *src, int num) { byte mybyte; while (num--) { mybyte=*src++; *dst++=mybyte; } }
Note: this allows us to achieve a kind of run length encoding for pixel sequences, not just individual pixels (as long as the sequence to be duplicated immediately precedes the dst pointer).
What is an LZ copy?
Basically, it is:
naivememcpy(dst, dst - ofs, len)
where ofs is positive.
What is a short LZ packet?
It does an LZ copy of length 1--64, from offset 1--256. It is usually two bytes long, but the 4th, 8th, ..., 4k'th short LZ packets in the stream are only one byte long. See the discussion of recycled bits.
- Byte 1
bits 0-5: LZ copy length, minus 1
bits 6-7: recycled bits
- Byte 2 [if necessary]
- bits 0-7: LZ offset, minus 1
What is a long LZ packet?
It does an LZ copy of length 3--258, from offset 1--1024.
- Byte 1
bits 0-5: 0
bits 6-7: top 2 bits of offset-1
- Byte 2
- bits 0-7: bottom 8 bits of offset-1
- Byte 3
- bits 0-7: LZ copy length, minus 3
What are recycled bits?
Each short LZ packet contributes 2 recycled bits. Recycled bits are accumulated until there are 8 of them, which occurs at every fourth short LZ packet. These 8 bits are then used as Byte 2 for that packet. The ordering of the bits is:
- bits 6-7: recycled bits of short LZ packet 4k + 1
- bits 4-5: recycled bits of short LZ packet 4k + 2
- bits 2-3: recycled bits of short LZ packet 4k + 3
- bits 0-1: recycled bits of short LZ packet 4k + 4
The value of the recycled bits is undefined if there are not enough short LZ packets to use them on. That is, if there are n short LZ packets in the stream, then the value of the recycled bits for the last (n % 4) short LZ packets is undefined.
![]() |
Language: |
English |