SGA

From MultimediaWiki
Revision as of 22:21, 1 August 2010 by Sasuke (talk | contribs) (→‎Chunk Types: Added capitalization and Sewer Shark)
Jump to navigation Jump to search

SGA is a multimedia file format commonly used for games on the Sega CD console system. Early versions of the format store uncompressed video frames. Newer versions use a simple LZ compression scheme. The final version of the format, used on Sega 32X/CD games use a yet to be determined compression method.

File Format

All multi-byte numbers in a SGA file are big-endian.

Due to using CD storage each chunk is stored using 2048 byte sectors. The first sector on the disc contains 2048 bytes of data, each subsiquent sectors contain a 2 byte header specifying how much of the chunk is left followed by 2046 bytes of data. One item to be aware of is if the value of the header is zero then to skip the next 2046 bytes and check the next header. (I'm presuming this has something to do with padding the CD for faster loading?)

An SGA file does not contain a global header, but has headers for each chunk. Each chunk can contain video or audio. Chunk header is 12-bytes

byte 0     chunk type
byte 1     unknown (always 0)
bytes 2-3  payload length 
bytes 4-7  unknown (time indicator)
byte 8     column start (can be ignored since we are not drawing on a 320x224 screen) 
byte 9     row start (again can be ignored)
byte 10    column size
byte 11    row size
bytes 12.. chunk payload

Bytes 4-5 are presumed to be part of the chunk preamble since the payload length specifies the length of the chunk.

Chunk Types

Some observed chunk types are:

  • 0x81: compressed video (Night Trap 32x uses this)
  • 0xC1: uncompressed video (used in Night Trap)
  • 0xC6: compressed video
  • 0xC8: compressed video (used in Sewer Shark)
  • 0xCD: video, perhaps followed by audio
  • 0xCB: video, perhaps in files that are video only
  • 0xA1: audio, sign/magnitude 8-bit PCM

Uncompressed Video 0xC1

Genesis Video uses 4-bit(16 color) pixels and upto 4 palettes

Video is made up of tiles. (column * 4 * row * 8) (each byte has 2 pixels) Then palette data follows (if there is only 24 bytes then only one palette is used, otherwise there is 4 palettes) A palette map follows for videos that use 4 palettes.

Palette Data

Palettes are stored in an unusual format. As the genesis normally uses either RGB or BGR stored in nibbles (even though only the top 3 bits are used).

bitmap={1,2,4}

For bit=0 to 2
   for color=0 to 15
       red[color]+=Top Most Bit of Data *bitmap[bit]
   next
next

Repeat for green and blue.

Palette Map

Reading 2 bits for each tile, determines which of the 4 palettes to use.

For Row=0 to RowMax
   For Col=0 to ColMax
       PalMap[Row*ColMax+Col]=Top 2 Bits of data
   Next
Next

When Drawing the Tile you would select the palette based upon Map.

Compressed Video 0xC6

Chunks in this format consist of several 34 byte blocks. Each block contains one word (2 bytes) of compression flags followed by 16 words of data.

The flag word is read in bits, starting with the most significant bit. If the bit is 0, the next word being read in the data stream is uncompressed. If the bit is one, the next word is an LZ command in the following format:

CCCD DDDD DDDD DDDD

C = Amount of words to copy
D = Displacement

This may be calculated as:

for count = 0 to top 3 bits of LZ word * 2
   data[current + count] = data[current + count - last 13 bits of LZ word]
next

Note that the displacement, unlike the copy ammount, is based on bytes, not words. Also, the displacement does not have to be word aligned.

After all sixteen bits of flags are read, the next block is read, and the process continues. The sequence ends when an LZ word's top three bits are all zeros.

Games Using SGA