SGA: Difference between revisions

From MultimediaWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
* Samples: [http://samples.mplayerhq.hu/game-formats/segacd/sga/ http://samples.mplayerhq.hu/game-formats/segacd/sga/]
* Samples: [http://samples.mplayerhq.hu/game-formats/segacd/sga/ http://samples.mplayerhq.hu/game-formats/segacd/sga/]


SGA is a chunk-based multimedia file format used primarily in games by Digital Pictures for the [[Sega CD]] console system. Early versions of the format store uncompressed video frames, while later revisions add features such as [[LZ]] compression, tile maps, and overlapping chunks. The extension for SGA files is usually ".SGA", but some files from ''Supreme Warrior'' have the extensions ".CLP" and ".PT1", and some files from ''Slam City with Scottie Pippen'' have extensions that include numbers (e.g. ".SG0", ".S37", etc.).
SGA is a chunk-based multimedia file format used primarily in games by [[Digital Pictures]] for the [[Sega CD]] console system. Early versions of the format store uncompressed video frames, while later revisions add features such as [[LZ]] compression, tile maps, and overlapping chunks. The extension for SGA files is usually ".SGA", but some files from ''Supreme Warrior'' have the extensions ".CLP" and ".PT1", and some files from ''Slam City with Scottie Pippen'' have extensions that include numbers (e.g. ".SG0", ".S37", etc.). Variations of the format are found in versions of Digital Pictures games for [[DOS]] PCs and the [[3DO]]. In those versions all video and audio is stored in large, monolithic files rather than individual files.


== File Format ==
== File Format ==
Line 11: Line 11:
(1) In the majority of files, each chunk is stored using 2048 byte sectors due to the nature of CD storage. The first sector in the file contains 2048 bytes of data, each subsequent 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?)
(1) In the majority of files, each chunk is stored using 2048 byte sectors due to the nature of CD storage. The first sector in the file contains 2048 bytes of data, each subsequent 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?)


(2) Some files, in particular the audio-only files in Night Trap (all variations), do not use length indicators at the start of sectors. The files contain strictly chunk headers and data.
(2) Some files, in particular the audio-only files in ''Night Trap'' (all variations), do not use length indicators at the start of sectors. The files contain strictly chunk headers and data.


(3) Later versions of the SGA format, as seen in Slam City with Scottie Pippen, Prize Fighter, and Supreme Warriors, introduced a scheme in which chunks can overlap/interrupt other chunks. In this type of file, each sector begins with a two-byte sector header, with the top four bits set to a non-zero value, which we might call the chunk index. If the following twelve bits are set to zero, then we are at the start of a new chunk using the current index. If the following twelve bits are non-zero, then we are looking at a length value, similar to previous formats. A decoder written to handle this type of file would have to keep track of multiple chunks simultaneously.
(3) Later versions of the SGA format, as seen in ''Slam City with Scottie Pippen'', ''Prize Fighter'', and ''Supreme Warriors'', introduced a scheme in which chunks can overlap/interrupt other chunks. In this type of file, each sector begins with a two-byte sector header, with the top four bits set to a non-zero value, which we might call the chunk index. If the following twelve bits are set to zero, then we are at the start of a new chunk using the current index. If the following twelve bits are non-zero, then we are looking at a length value, similar to previous formats. A decoder written to handle this type of file would have to keep track of multiple chunks simultaneously.


An SGA file does not contain a global header, but has headers for each chunk. Each chunk can contain video or audio.  
Most SGA files do not contain a global header, but have headers for each chunk. Headers are never split across sector boundaries. Chunks are word aligned, can contain video or audio. The basic header is 4 bytes long, and is shared by all chunk types.
Chunks are word aligned. Chunk header is 12-bytes


  byte 0    chunk type  
  Byte  Description
  byte 1    stream index (Sewer Shark in particular uses this for its branching path-based gameplay)
----  -----------
  bytes 2-3 payload length  
0    Chunk type  
bytes 4-7  time indicator
  1    Stream index (Sewer Shark in particular uses this for its branching path-based gameplay)
  non 32x
  2-3   Payload length
  byte 8    format flags (usually 05h [normal] or 85h [uses tilemap])
  byte 9    palette count
  32x
  byte 8    palette start (usually 1)
  byte 9    palette update size (0 if use existing palette)
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.
This is followed by more metadata:
Byte  Description
----  -----------
4-7  Time in [[SMPTE]] format


When reading an audio chunk, bytes 8 and 9 represent the frequency counter value of the audio (For the PCM chip on the Sega CD). Bytes 11 and 12 are unknown (Usually 0x01 and 0x00 in Sewer Shark), however, byte 11 is presumed to represent the number of bytes per sample. Because all(?) of the known audio used in SGA files is in 8-bit monaural format, the value of this byte is generally set to 1.
Video chunks for the Sega CD contain the following metadata:
 
Byte  Description
----  -----------
8     Data flags
9     Palette count
 
Metadata in video chunks for the Sega CD 32X are as follows:
 
Byte  Description
----  -----------
8    Palette start offset (usually 1)
9    Palette update size (a value of 0 means use existing palette)
 
Then more metadata:
 
Byte  Description
----  -----------
10    tiles per column
11    tiles per row
 
Certain types of video chunks contain additional metadata, which is detailed below in the sections related to the various chunk types.
 
Audio chunks have the following metadata:
 
Byte  Description
----  -----------
8-9  Sample rate
10    (Probably) Number of channels / bytes per sample (usually 1)
11    Unknown (usually 0)


== Chunk Types ==
== Chunk Types ==
Some known chunk types are:
Some known chunk types are:


* 0x81: encoded video (used in most 32X games by Digital Pictures)
* 0x81: encoded video (used in most Sega CD 32X games by Digital Pictures)
* 0xA1: audio, sign/magnitude 8-bit PCM
* 0xA1: audio, sign/magnitude 8-bit PCM
* 0xB4: compressed? video (used in Prize Fighter "ALLOPTS.SGA", among others)
* 0xC1: uncompressed video (used in Night Trap SCD, Sewer Shark, Corpse Killer SCD, and others)
* 0xC1: uncompressed video (used in Night Trap SCD, Sewer Shark, Corpse Killer SCD, and others)
* 0xC2: compressed? video (used in Corpse Killer SCD, Slam City with Scottie Pippen, and others?)
* 0xC2: compressed? video (used in Corpse Killer SCD, Slam City with Scottie Pippen, and others?)
* 0xC4: compressed? video (used in Slam City with Scottie Pippen)
* 0xC6: compressed video (used in Night Trap SCD "DPLOGO.SGA", Sewer Shark, Make My Video C&C, and others)
* 0xC6: compressed video (used in Night Trap SCD "DPLOGO.SGA", Sewer Shark, Make My Video C&C, and others)
* 0xC7: compressed video (used in Prize Fighter and others)
* 0xC7: compressed video (used in Prize Fighter and others)
* 0xC8: compressed video (used in Sewer Shark, Make My VIdeo C&C, and others)
* 0xC8: compressed video (used in Sewer Shark, Make My Video C&C, and others)
* 0xCD: compressed? video (used in Double Switch SCD)
* 0xCD: compressed? video (used in Double Switch SCD)
* 0xCB: compressed video (used in Prize Fighter, Double Switch "DPLOGO.SGA", Corpse Killer 32X)
* 0xCB: compressed video (used in Prize Fighter, Double Switch "DPLOGO.SGA", Corpse Killer 32X)

Revision as of 07:52, 5 July 2014

SGA is a chunk-based multimedia file format used primarily in games by Digital Pictures for the Sega CD console system. Early versions of the format store uncompressed video frames, while later revisions add features such as LZ compression, tile maps, and overlapping chunks. The extension for SGA files is usually ".SGA", but some files from Supreme Warrior have the extensions ".CLP" and ".PT1", and some files from Slam City with Scottie Pippen have extensions that include numbers (e.g. ".SG0", ".S37", etc.). Variations of the format are found in versions of Digital Pictures games for DOS PCs and the 3DO. In those versions all video and audio is stored in large, monolithic files rather than individual files.

File Format

All multi-byte numbers in a SGA file are big-endian, since the Sega Genesis and Sega CD both use big-endian Motorola 68000 CPUs.

There are three known methods of storing data in SGA files:

(1) In the majority of files, each chunk is stored using 2048 byte sectors due to the nature of CD storage. The first sector in the file contains 2048 bytes of data, each subsequent 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?)

(2) Some files, in particular the audio-only files in Night Trap (all variations), do not use length indicators at the start of sectors. The files contain strictly chunk headers and data.

(3) Later versions of the SGA format, as seen in Slam City with Scottie Pippen, Prize Fighter, and Supreme Warriors, introduced a scheme in which chunks can overlap/interrupt other chunks. In this type of file, each sector begins with a two-byte sector header, with the top four bits set to a non-zero value, which we might call the chunk index. If the following twelve bits are set to zero, then we are at the start of a new chunk using the current index. If the following twelve bits are non-zero, then we are looking at a length value, similar to previous formats. A decoder written to handle this type of file would have to keep track of multiple chunks simultaneously.

Most SGA files do not contain a global header, but have headers for each chunk. Headers are never split across sector boundaries. Chunks are word aligned, can contain video or audio. The basic header is 4 bytes long, and is shared by all chunk types.

Byte  Description
----  -----------
0     Chunk type 
1     Stream index (Sewer Shark in particular uses this for its branching path-based gameplay)
2-3   Payload length

This is followed by more metadata:

Byte  Description
----  -----------
4-7   Time in SMPTE format

Video chunks for the Sega CD contain the following metadata:

Byte  Description
----  -----------
8     Data flags
9     Palette count

Metadata in video chunks for the Sega CD 32X are as follows:

Byte  Description
----  -----------
8     Palette start offset (usually 1)
9     Palette update size (a value of 0 means use existing palette)

Then more metadata:

Byte  Description
----  -----------
10    tiles per column
11    tiles per row

Certain types of video chunks contain additional metadata, which is detailed below in the sections related to the various chunk types.

Audio chunks have the following metadata:

Byte  Description
----  -----------
8-9   Sample rate
10    (Probably) Number of channels / bytes per sample (usually 1)
11    Unknown (usually 0)

Chunk Types

Some known chunk types are:

  • 0x81: encoded video (used in most Sega CD 32X games by Digital Pictures)
  • 0xA1: audio, sign/magnitude 8-bit PCM
  • 0xC1: uncompressed video (used in Night Trap SCD, Sewer Shark, Corpse Killer SCD, and others)
  • 0xC2: compressed? video (used in Corpse Killer SCD, Slam City with Scottie Pippen, and others?)
  • 0xC4: compressed? video (used in Slam City with Scottie Pippen)
  • 0xC6: compressed video (used in Night Trap SCD "DPLOGO.SGA", Sewer Shark, Make My Video C&C, and others)
  • 0xC7: compressed video (used in Prize Fighter and others)
  • 0xC8: compressed video (used in Sewer Shark, Make My Video C&C, and others)
  • 0xCD: compressed? video (used in Double Switch SCD)
  • 0xCB: compressed video (used in Prize Fighter, Double Switch "DPLOGO.SGA", Corpse Killer 32X)
  • 0xCD: compressed video (used in Double Switch)
  • 0xD1: compressed? overlay video (used in Sewer Shark when killing a Ratigator(TM, etc)
  • 0xD4: compressed? overlay video (used in Corpse Killer SCD/32X for the zombies)
  • 0xE7: (un)compressed video (used in the Make My Video series)
  • 0xE8: compressed? video (used in Ground Zero Texas)
  • 0xF0: container for other chunks (used in Slam City with Scottie Pippen)
  • 0xF1: container for other chunks (used in Slam City with Scottie Pippen)

Encoded Video 0x81

This type of video is probably most appropriately called "encoded" rather than "compressed". Video frames of this type are represented with a series of codes that indicate the size, color, and pixel layout of the graphics to be drawn, rather than, for example, LZSS encoded byte streams as in previous versions of SGA files.

The encoding scheme has been completely reverse-engineered, and a functional decoder has been written. A detailed description of the codes will be added at a later time.

Audio 0xA1

Audio sample rate can be determined in the following way (for NTSC systems only?):

SamplesPerSecond = ((Byte 8 << 8) + Byte 9) * SEGA_CD_PCM_INCREMENT

SEGA_CD_PCM_INCREMENT is ~15.8945723, and is calculated as SEGA_CD_PCM_FREQUENCY_MAX / 2048

SEGA_CD_PCM_FREQUENCY_MAX is ~32552.084, and is calculated as SEGA_CD_CPU_FREQUENCY / 384

SEGA_CD_CPU_FREQUENCY is 12500000, and is calculated as SEGA_CD_CRYSTAL_FREQUENCY / 4

SEGA_CD_CRYSTAL_FREQUENCY is 50000000

The sample rate can also be used to determine the frame rate of video by using the formula SamplesPerSecond / NumSamples, where NumSamples is the length of the audio data in the audio chunk.

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.

Compressed Video 0xC6, 0xC7, 0xC8

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

The tag is read in bits, starting with the most significant (left most) bit. For each bit set to 0, there is an uncompressed word literal. For each bit set to 1, the following word is a displacement/length reference in the following format:

LLLD DDDD DDDD DDDD

L = Amount of words to copy (amount of bytes to copy * 2)
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 amount, is based on bytes, not words. Also, the displacement does not have to be word aligned.

After the entire tag is read, the next flag block is read, and the process continues. The sequence ends when an reference word's top three bits are all zeros.

In C7 format chunks, the top three bits represent the amount of words to copy minus one. Any decoder that implements C7 decoding must take this into account.

Most chunks in C6, and all chunks C8 and E7 formats require that adjacent pixels in even-numbered lines be swapped for frames to be correctly displayed. The exact reason for this is unclear, although since Sega CD video often contains a lot of checkerboard dithering, swapping pixels would eliminate the checkerboard patterns and lead to higher amounts of identical/redundant data, thus leading to more efficient compression. There is currently no known way to determine which C6 chunks require pixel swapping.

Compressed Video 0xCB, 0xCD, 0xE7

Like other compressed chunk formats, chunks in this format consist of several 34 byte LZSS blocks. Each block contains a one word (2 bytes) tag of compression flags followed by 16 words of data.

For each bit set to 1, the following word is a displacement/length reference in the following format:

LLLL DDDD DDDD DDDD

L = Amount of words to copy minus one (amount of bytes to copy * 2)
D = Displacement

This may be calculated as:

for count = 0 to (top 4 bits of LZ word + 1) * 2
   data[current + count] = data[current + count - last 12 bits of LZ word]
next

Chunks of type E7 are used in the Make My Video series, and contain three subframes, which are stacked on top of each other to complete the whole frame. C7 chunks have an additional six bytes of metadata containing three 16-bit values. The topmost bit of each value indicates whether the data for that frame is raw (1) or compressed (0). The remaining 15 bits represent the length of the data for the subframe, with an apparent maximum value of 0x1500. The data for each subframe immediately follows the metadata in order, which is followed by 180 bytes of palette data in the usual format.

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.

Games Using SGA

  • Night Trap
  • Sewer Shark
  • Power Factory Featuring C&C Music Factory
  • Make My Video series
  • Ground Zero: Texas
  • Prize Fighter
  • Double Switch
  • Slam City with Scottie Pippen
  • Corpse Killer
  • Supreme Warrior

Decoders/Converters

  • A decoder has been written that can currently decode SGA files from Night Trap (SCD and 32X), Sewer Shark (including stream selection), Corpse Killer (SCD and 32X), Prize Fighter, Double Switch, as well as various other games, and convert them to AVI format files. It has yet to be released to the public.