Origin MGI

From MultimediaWiki
Revision as of 15:19, 10 April 2007 by SpdC4c (talk | contribs)
Jump to navigation Jump to search


This document comes from wotsit.org, and originated on GAP's (Game Audio Player) website (now defunct). By Valery V. Anisimovsky (no valid email address known) Dmitry Kirnocenskij (ejt@mail.ru) is credited with working out EA ADPCM decompression algorithm.

MGI File Header

The MGI file has the following header:

struct MGIHeader
 char	szID[4];
 DWORD dwUnknown1;
 DWORD dwNumSecIndices;
 DWORD dwUnknown2;
 DWORD dwNumIntIndices;

szID -- string ID, which is equal to "\x8F\xC2\x35\x3F".

dwUnknown1, dwUnknown2 -- seem to be always 0.

dwNumSecIndices -- the number of section indices used in section descriptors (see below).

dwNumIntIndices -- seems to be the number of indices used in interactive playback descriptors (see below).

After the header comes the table of interactive playback descriptors. Each descriptor has the following format:

struct MGIIntDesc
 LONG	lIndex;
 DWORD dwSection;

lIndex -- seems to be the index of interactive sequence. Note that some indices are negative values.

dwSection -- the pointer to the section (that is, the index of the section descriptor correspondent to the section, NOT the index of section given in the descriptor itself!)

The number of descriptors in this table (its size) is very uncertain. I use a kind of heuristic approach to get past this table, outlined below.

After the table of interactive playback descriptors comes the (DWORD) number of sections in the file (let it be denoted as dwNumSections). After this number comes the table of (dwNumSections) section descriptors. Each descriptor has the following format:

struct MGISecDesc
 DWORD dwStart;
 DWORD dwIndex;
 DWORD dwOutSize;

dwStart -- the starting position of the audio data for the section.

dwIndex -- the index of the section (NOT the index of the correspondent descriptor). Several different sections may have the same index. The meaning of this index seems to be quite uncertain, though it is not required for non-interactive playback of MGI files.

dwOutSize -- the output size of the audio stream stored in the section (in bytes). May be used for section length (in seconds) calculation. Includes the outsizes for both compressed and non-compressed parts of the section, that is, it's the whole outsize of the section.

Now, here's the approach I use to get to the start of section descriptors table. First, we should read MGIHeader. Then, we may read the interactive playback table -- descriptor by descriptor and for each descriptor we should check whether its (dwIndex) is less than header values (dwNumSecIndices) and (dwNumIntIndices). If the suspicious descriptor is found (for which the index value is out of range -- note that the descriptors with negative indices are correct!) we may check whether this descriptors is really the beginning of the section descriptors table. This check is simple to perform: assume that audio data for the first section starts right after the section descriptors table, then we can get the following equation: (dwDescPos) 4 (lIndex)*sizeof(MGISecDesc)=(dwSection), where (dwDescPos) is the position in MGI file at which starts the suspicious descriptor, (lIndex) and (dwSection) are the values from that descriptor. When this equation holds we most likely found the beginning of the section descriptors table.

After the section descriptors table comes the audio data for the sections.

MGI Section Audio Data

For each section we can get the starting position of the audio data from the section's descriptor. Note that the last section seems to be always empty (it starts at the end of the MGI file, has zero outsize and zero index). Each section consists of two parts: EA ADPCM compressed and non-compressed. First comes the compressed part and right after that comes non-compressed part. To get the size of non-compressed tail you can use the following formula: dwTailSize=(dwSectionSize*0x70-dwOutSize*0x1E)/0x52, where (dwSectionSize) is the size of the whole section (which may be calculated as the difference between the start positions of the next and the current sections), (dwOutSize) is the value from the section's descriptor. This formula can be easily derived knowing the fact the compressed data is composed of blocks (0x1E bytes each -- for stereo stream) which are decompressed into 0x1C*4 bytes each (for stereo stream). Note that this formula is valid for both stereo and mono MGI files.

The compressed part contains EA ADPCM compressed stream. It's devided into small blocks of 0x1E (stereo) or 0xF (mono) bytes. The non-compressed part contains raw (16-bit signed) PCM data. All MGI files I've seen are stereo 22050 Hz 16-bit. The non-compressed part may be played right after the compressed part.

All sections may be played consequently right in their turn. Some MGI files contain several quite independent tunes, though when played consequently, those tunes form relatively seamless composition.

EA ADPCM Decompression Algorithm

During the decompression four LONG variables must be maintained for stereo stream: lCurSampleLeft, lCurSampleRight, lPrevSampleLeft, lPrevSampleRight and two -- for mono stream: lCurSample, lPrevSample. At the beginning of each section you must initialize these variables to zeros. Note that LONG here is signed.

The stream is divided into small blocks of 0x1E (stereo) or 0xF (mono) bytes. You should process all blocks in their turn. Here's the code which decompresses one stereo stream block.

BYTE  InputBuffer[InputBufferSize]; // buffer containing data for one block
BYTE  bInput;
LONG  c1left,c2left,c1right,c2right,left,right;
BYTE  dleft,dright;

c1left=EATable[HINIBBLE(bInput)];   // predictor coeffs for left channel
c2left=EATable[HINIBBLE(bInput) 4];
c1right=EATable[LONIBBLE(bInput)];  // predictor coeffs for right channel
c2right=EATable[LONIBBLE(bInput) 4];

dleft=HINIBBLE(bInput) 8;   // shift value for left channel
dright=LONIBBLE(bInput) 8;  // shift value for right channel

for (i=2;i<0x1E;i  )
 left=HINIBBLE(InputBuffer[i]);  // HIGHER nibble for left channel
 left=(left lCurSampleLeft*c1left lPrevSampleLeft*c2left 0x80)>>8;

 right=LONIBBLE(InputBuffer[i]); // LOWER nibble for right channel
 right=(right lCurSampleRight*c1right lPrevSampleRight*c2right 0x80)>>8;

 // Now we've got lCurSampleLeft and lCurSampleRight which form one stereo
 // sample and all is set for the next step...
 Output((SHORT)lCurSampleLeft,(SHORT)lCurSampleRight); // send the sample to output

HINIBBLE and LONIBBLE are higher and lower 4-bit nibbles:

#define HINIBBLE(byte) ((byte) >> 4)
#define LONIBBLE(byte) ((byte)