Covox ADPCM

From MultimediaWiki
Revision as of 16:50, 4 April 2007 by Trixter (talk | contribs)
Jump to navigation Jump to search

This is a family of ADPCM formats used for compressing 8-bit unsigned PCM. They were invented by Covox for use with the Covox Speech Thing, Sound Master, and other Covox sound output devices. There are 2-,3- and 4-bit ADPCM formats.

Header

File header consists of 16 bytes:

 FF 55 FF AA FF 55 FF AA xx yy pp pp pp pp 00 00
 xx - encoding type
 yy - playback rate (rate != sample rate, rather the timer tick rate divisor used for playback)
 pp - unsigned longint meant to indicate unpacked size (mostly unused)
 00 - unknown/unused

Type may be one of the following:

  • 0x01 - 1-bit ADPCM
  • 0x02 - 2-bit ADPCM
  • 0x03 - 3-bit ADPCM
  • 0x04 - 4-bit ADPCM
  • 0x08 - raw 8-bit unsigned PCM
  • 0x81 - 1-bit ADPCM with silence encoding
  • 0x82 - 2-bit ADPCM with silence encoding
  • 0x83 - 3-bit ADPCM with silence encoding
  • 0x84 - 4-bit ADPCM with silence encoding
  • 0x88 - 8-bit PCM with silence encoding

The first 8 bytes are a unique code guaranteed not to show up inside the rest of file. This means that multiple PCM files can be concatenated together and the locations found by looking for the headers.

Markers

There are two types of markers known:

  • FF 55 55 55 - silence block, next 16-bit little-endian value specifies number of silence samples - 27
  • FF AA AA AA - end of sound

For 3-bit ADPCM they are cut to 3 bytes (FF 55 55 and FF AA AA respectively).

After silence block ADPCM decoding state should be reset.

ADPCM decoding

This is generalized decoding procedure for all know ADPCM variants. n will denote the number of bits used in ADPCM (n=2 for 2-bit ADPCM, etc).

Decoding procedure:

  1. look at the next 3 or 4 bytes, if they make a marker, do action for this marker
  2. read n bytes
  3. decode 8 deltas from them by taking high bits from n values and shifting them
  4. apply these deltas to get decoded values

Update procedure:

high_bit = 1 << (n-1);
low_bits = (1 << (n-1)) - 1;
mul = step_table[step] * mult_table[delta & low_bits];
if(delta & high_bit)
 val -= mul;
else 
 val += mul;
val = clip(val, 0, 0xFFFF);
step = clip(step + step_update_table[delta & low_bits], 0, step_max);

ADPCM Tables

Value 4-bit ADPCM 3-bit ADPCM 2-bit ADPCM
Max. step value 72 96 80
Step update table -8, -6, -4, -2, 2, 4, 8, 16 -4, -2, 4, 16 -6, 6
Multiplication table 0.5, 1.0, 2.0, 3.0, 4.5, 5.0, 9.0, 12.0 1.0, 1.5, 2.0, 2.5 1.0, 3.0

Step table (it has much more values than needed):

0x0080, 0x008C, 0x0098, 0x00A6, 0x00B5, 0x00C5, 0x00D7, 0x00EB,
0x0100, 0x0117, 0x0130, 0x014C, 0x016A, 0x018B, 0x01AF, 0x01D6,
0x0200, 0x022E, 0x0261, 0x0298, 0x02D4, 0x0316, 0x035D, 0x03AB,
0x0400, 0x045D, 0x04C2, 0x0530, 0x05A8, 0x062B, 0x06BA, 0x0756,
0x0800, 0x08B9, 0x0983, 0x0A60, 0x0B50, 0x0C56, 0x0D74, 0x0EAC,
0x1000, 0x1173, 0x1307, 0x14C0, 0x16A1, 0x18AD, 0x1AE9, 0x1D58,
0x2000, 0x22E5, 0x260E, 0x2980, 0x2D41, 0x315A, 0x35D1, 0x3AB0,
0x4000, 0x45CB, 0x4C1C, 0x52FF, 0x5A82, 0x62B4, 0x6BA2, 0x7560,
0x8000, 0x8B96, 0x9838, 0xA5FF, 0xB505, 0xC567, 0xD745, 0xEAC1,

0x00C0, 0x00D1, 0x00E4, 0x00F9, 0x0110, 0x0128, 0x0143, 0x0160,
0x0180, 0x01A3, 0x01C9, 0x01F2, 0x021F, 0x0250, 0x0286, 0x02C0,
0x0300, 0x0346, 0x0391, 0x03E4, 0x043E, 0x04A0, 0x050C, 0x0581,
0x0600, 0x068B, 0x0723, 0x07C8, 0x087C, 0x0941, 0x0A17, 0x0B01,
0x0C00, 0x0D16, 0x0E45, 0x0F90, 0x10F8, 0x1282, 0x142E, 0x1602,
0x1800, 0x1A2C, 0x1C8A, 0x1F20, 0x21F1, 0x2503, 0x285D, 0x2C04,

0x0240, 0x0274, 0x02AD, 0x02EB, 0x032F, 0x0378, 0x03C9, 0x0420,
0x0480, 0x04E8, 0x055A, 0x05D6, 0x065D, 0x06F1, 0x0791, 0x0841,
0x0900, 0x09D1, 0x0AB4, 0x0BAC, 0x0CBA, 0x0DE1, 0x0F23, 0x1082,
0x1200, 0x13A1, 0x1568, 0x1758, 0x1975, 0x1BC3, 0x1E46, 0x2103,
0x2400, 0x2742, 0x2AD0, 0x2EB0, 0x32E9, 0x3785, 0x3C8B, 0x4206,
0x4800, 0x4E84, 0x559F, 0x5D5F, 0x65D3, 0x6F0A, 0x7917, 0x840C,

0x02C0, 0x0300, 0x0345, 0x0391, 0x03E4, 0x043E, 0x04A0, 0x050B,
0x0580, 0x05FF, 0x068A, 0x0722, 0x07C7, 0x087B, 0x0940, 0x0A16,
0x0B00, 0x0BFF, 0x0D15, 0x0E44, 0x0F8E, 0x10F7, 0x1280, 0x142D,
0x1600, 0x17FE, 0x1A2A, 0x1C88, 0x1F1D, 0x21EE, 0x2500, 0x2859,
0x2C00, 0x2FFB, 0x3453, 0x3910, 0x3E3A, 0x43DB, 0x4A00, 0x50B2,
0x5800, 0x5FF7, 0x68A6, 0x721F, 0x7C73, 0x87B7, 0x93FF, 0xA165,

0x03C0, 0x0417, 0x0476, 0x04DD, 0x054E, 0x05C9, 0x064F, 0x06E1,
0x0780, 0x082E, 0x08EB, 0x09BA, 0x0A9B, 0x0B91, 0x0C9D, 0x0DC1,
0x0F00, 0x105C, 0x11D7, 0x1374, 0x1537, 0x1722, 0x193A, 0x1B83,
0x1E00, 0x20B7, 0x23AD, 0x26E8, 0x2A6D, 0x2E44, 0x3274, 0x3705,
0x3C00, 0x416E, 0x475A, 0x4DCF, 0x54DA, 0x5C88, 0x64E8, 0x6E0A,
0x7800, 0x82DC, 0x8EB4, 0x9B9F, 0xA9B5, 0xB911, 0xC9D1, 0xDC15