ADX (file format)
CRI ADX is a lossy proprietary audio storage and compression format developed by CRI Middleware specifically for use in video games; it is derived from ADPCM. Its most notable feature is a looping function that has proved useful for background sounds in various games that have adopted the format, including many games for the Sega Dreamcast as well as some PlayStation 2, GameCube and Wii games. One of the first games to use ADX was Burning Rangers, on the Sega Saturn. Notably, the Sonic the Hedgehog series from the Dreamcast generation up to at least Shadow the Hedgehog have used this format for sound and voice recordings. Jet Set Radio Future for original Xbox also used this format. 

On top of the main ADPCM encoding, the ADX toolkit also includes a sibling format, AHX, which uses a variant of MPEG-2 audio intended specifically for voice recordings and a packaging archive, AFS, for bundling multiple CRI ADX and AHX tracks into a single container file.

Version 2 of the format (ADX2) uses the HCA and HCA-MX extension, which are usually bundled into a container file with the extensions ACB and AWB.
The AWB extension is not to be confused with the Audio format with the same extension and mostly contains the binary data for the HCA files.

General overview 
CRI ADX is a compressed audio format, but unlike MP3 and similar formats, it doesn't apply a psychoacoustic model to the sound to reduce its complexity. The ADPCM model instead stores samples by recording the error relative to a prediction function which means more of the original signal survives the encoding process; as such ADPCM compression instead trades accuracy of the representation for size by using relatively small sample sizes, usually 4bits. The human auditory system's tolerance for the noise this causes makes  the loss of accuracy barely noticeable.

Like other encoding formats, CRI ADX supports multiple sampling frequencies such as 22050 Hz, 44100 Hz, 48000 Hz, etc. however, the output sample depth is locked at 16bits, generally due to the lack of precision already mentioned. It supports multiple channels but there seems to be an implicit limitation of stereo (2 channel) audio although the file format itself can represent up to 255 channels. The only particularly distinctive feature that sets CRI ADX apart from alternatives like IMA ADPCM (other than having a different prediction function) is the integrated looping functionality, this enables an audio player to optionally skip backwards after reaching a single specified point in the track to create a coherent loop; hypothetically, this functionality could be used to skip forwards as well but that would be redundant since the audio could simply be clipped with an editing program instead.

For playback there are a few plugins for WinAmp and a convert to wave tool (see the references section). The open source program / library FFmpeg also has CRI ADX support implemented, however, its decoder is hard coded so can only properly decode 44100 Hz ADXs.

Technical description 

The CRI ADX specification is not freely available, however the most important elements of the structure have been reverse engineered and documented in various places on the web. The information here may be incomplete but should be sufficient to build a working codec or transcoder.

As a side note, the AFS archive files that CRI ADXs are sometimes packed in are a simple variant of a tarball which uses numerical indices to identify the contents rather than names. Source code for an extractor can be found in the ADX archive at.

File header 
The ADX disk format is defined in big-endian. The identified sections of the main header are outlined below:

Fields labelled "Unknown" contain either unknown data or are apparently just reserved (i.e. filled with null bytes). Fields labelled with 'v3' or 'v4' but not both are considered "Unknown" in the version they are not marked with. This header may be as short as 20 bytes (0x14), as determined by the copyright offset, which implicitly removes support for a loop since those fields are not present.

The "Encoding Type" field should contain one of:
 0x02 for CRI ADX with pre-set prediction coefficients
 0x03 for Standard CRI ADX
 0x04 for CRI ADX with an exponential scale
 0x10 or 0x11 for AHX
The "Version" field should contain one of:
 0x03 for CRI ADX 'version 3'
 0x04 for CRI ADX 'version 4'
 0x05 for a variant of CRI ADX 4 without looping support
When decoding AHX audio, the version field does not appear to have any meaning and can be safely ignored.

Files with encoding type '2' use 4 possible sets of prediction coefficients as listed below:

Sample format 
CRI ADX encoded audio data is broken into a series of 'blocks', each containing data for only one channel. The blocks are then laid out in 'frames' which consist of one block from every channel in ascending order. For example, in a stereo (2 channel) stream this would consist of Frame 1: left channel block, right channel block; Frame 2: left, right; etc. Blocks are usually always 18 bytes in size containing 4bit samples though other sizes are technically possible, an example of such a block looks like this:

The first 3 bits of the 'Predictor/Scale' integer contain the predictor index. The scale is contained in the remaining 13 bits.

The predictor index is a 3bit integer that specifies which prediction coefficient set should be used to decode that block. This is only used in files with encoding type '2'.

The scale is a 13bit unsigned integer (big-endian like the header) which is essentially the amplification of all the samples in that block. Each sample in the block must be decoded in bit-stream order, that is, most significant bit first. For example, when the sample size is 4bits:

The samples themselves are not in reverse so there is no need to fiddle with them once they are extracted. Each sample is signed so for this example, the value can range between -8 and +7 (which will be multiplied by the scale during decoding). As an aside, although any bit-depth between 1 and 255 is made possible by the header. It is unlikely that one bit samples would ever occur as they can only represent the values {0, 1}, {-1, 0} or {-1, 1}, all of which are not particularly useful for encoding songs — if they were to occur then it is unclear which of the three possibilities is the correct interpretation.

CRI ADX decoding 
This section walks through decoding CRI ADX 'version 3' or 'version 4' when "Encoding Type" is "Standard CRI ADX" (0x03). An encoder can also be built by simply flipping the code to run in reverse. All code samples in this section are written using C99.

Before a 'standard' CRI ADX can be either encoded or decoded, the set of prediction coefficients must be calculated. This is generally best done in the initialisation stage:
 #define M_PI acos(-1.0)
 double a, b, c;
 a = sqrt(2.0) - cos(2.0 * M_PI * ((double)adx_header->highpass_frequency / adx_header->sample_rate));
 b = sqrt(2.0) - 1.0;
 c = (a - sqrt((a + b) * (a - b))) / b; //(a+b)*(a-b) = a*a-b*b, however the simpler formula loses accuracy in floating point
 
 // double coefficient[2];
 coefficient[0] = c * 2.0;
 coefficient[1] = -(c * c);
This code calculates prediction coefficients for predicting the current sample from the 2 previous samples. These coefficients also form a first order Finite Impulse Response high-pass filter as well.

Once we know the decoding coefficients we can start decoding the stream:
 static int32_t*      past_samples; // Previously decoded samples from each channel, zeroed at start (size = 2*channel_count)
 static uint_fast32_t sample_index = 0; // sample_index is the index of sample set that needs to be decoded next
 static ADX_header*   adx_header;
 
 // buffer is where the decoded samples will be put
 // samples_needed states how many sample 'sets' (one sample from every channel) need to be decoded to fill the buffer
 // looping_enabled is a boolean flag to control use of the built-in loop
 // Returns the number of sample 'sets' in the buffer that could not be filled (EOS)
 unsigned decode_adx_standard( int16_t* buffer, unsigned samples_needed, bool looping_enabled )
 {
  unsigned const samples_per_block = (adx_header->block_size - 2) * 8 / adx_header->sample_bitdepth;
  int16_t scale[ adx_header->channel_count ];
 
  if (looping_enabled && !adx_header->loop_enabled)
     looping_enabled = false;
 
  // Loop until the requested number of samples are decoded, or the end of file is reached
  while (samples_needed > 0 && sample_index < adx_header->total_samples)
  {
     // Calculate the number of samples that are left to be decoded in the current block
     unsigned sample_offset = sample_index % samples_per_block;
     unsigned samples_can_get = samples_per_block - sample_offset;

     // Clamp the samples we can get during this run if they won't fit in the buffer
     if (samples_can_get > samples_needed)
        samples_can_get = samples_needed;
 
     // Clamp the number of samples to be acquired if the stream isn't long enough or the loop trigger is nearby
     if (looping_enabled && sample_index + samples_can_get > adx_header->loop_end_index)
        samples_can_get = adx_header->loop_end_index - sample_index;
     else if (sample_index + samples_can_get > adx_header->total_samples)
        samples_can_get = adx_header->total_samples - sample_index;
 
     // Calculate the bit address of the start of the frame that sample_index resides in and record that location
     unsigned long started_at = (adx_header->copyright_offset + 4 + \
                     sample_index / samples_per_block * adx_header->block_size * adx_header->channel_count) * 8;
 
     // Read the scale values from the start of each block in this frame
     for (unsigned i = 0 ; i < adx_header->channel_count ; ++i)
     {
        bitstream_seek( started_at + adx_header->block_size * i * 8 );
        scale[i] = ntohs( bitstream_read( 16 ) );
     }
 
     // Pre-calculate the stop value for sample_offset
     unsigned sample_endoffset = sample_offset + samples_can_get;
 
     // Save the bitstream address of the first sample immediately after the scale in the first block of the frame
     started_at += 16;
     while ( sample_offset < sample_endoffset )
     {
        for (unsigned i = 0 ; i < adx_header->channel_count ; ++i)
        {
           // Predict the next sample
           double sample_prediction = coefficient[0] * past_samples[i*2 + 0] + coefficient[1] * past_samples[i*2 + 1];
 
           // Seek to the sample offset, read and sign extend it to a 32bit integer
           // Implementing sign extension is left as an exercise for the reader
           // The sign extension will also need to include a endian adjustment if there are more than 8 bits
           bitstream_seek( started_at + adx_header->sample_bitdepth * sample_offset + \
                           adx_header->block_size * 8 * i );
           int_fast32_t sample_error = bitstream_read( adx_header->sample_bitdepth );
           sample_error = sign_extend( sample_error, adx_header->sample_bitdepth );
 
           // Scale the error correction value
           sample_error *= scale[i];
 
           // Calculate the sample by combining the prediction with the error correction
           int_fast32_t sample = sample_error + (int_fast32_t)sample_prediction;
 
           // Update the past samples with the newer sample
           past_samples[i*2 + 1] = past_samples[i*2 + 0];
           past_samples[i*2 + 0] = sample;
 
           // Clamp the decoded sample to the valid range for a 16bit integer
           if (sample > 32767)
              sample = 32767;
           else if (sample < -32768)
              sample = -32768;
 
           // Save the sample to the buffer then advance one place
           *buffer++ = sample;
        }
        ++sample_offset;  // We've decoded one sample from every block, advance block offset by 1
        ++sample_index;   // This also means we're one sample further into the stream
        --samples_needed; // And so there is one less set of samples that need to be decoded
    }
 
    // Check if we hit the loop end marker, if we did we need to jump to the loop start
    if (looping_enabled && sample_index == adx_header->loop_end_index)
       sample_index = adx_header->loop_start_index;
  }
 
  return samples_needed;
 }
Most of the above code should be straightforward enough for anyone versed in C. The 'ADX_header' pointer refers to the data extracted from the header as outlined earlier, it is assumed to have already been converted to the host Endian. This implementation is not intended to be optimal and the external concerns have been ignored such as the specific method for sign extension and the method of acquiring a bitstream from a file or network source. Once it completes, there will be samples_needed sets (if stereo, there will be pairs for example) of samples in the output buffer. The decoded samples will be in host-endian standard interleaved PCM format, i.e. left 16bit, right 16bit, left, right, etc. Finally, if looping is not enabled, or not supported, then the function will return the number of sample spaces that were not used in the buffer. The caller can test if this value is not zero to detect the end of the stream and drop or write silence into the unused spaces if necessary.

Encryption 

CRI ADX supports a simple encryption scheme which XORs values from a linear congruential pseudorandom number generator with the block scale values. This method is computationally inexpensive to decrypt (in keeping with CRI ADX's real-time decoding) yet renders the encrypted files unusable. The encryption is active when the "Flags" value in the header is 0x08. As XOR is symmetric the same method is used to decrypt as to encrypt. The encryption key is a set of three 16-bit values: the multiplier, increment, and start values for the linear congruential generator (the modulus is 0x8000 to keep the values in the 15-bit range of valid block scales). Typically all ADX files from a single game will use the same key.

The encryption method is vulnerable to known-plaintext attacks. If an unencrypted version of the same audio is known the random number stream can be easily retrieved and from it the key parameters can be determined, rendering every CRI ADX encrypted with that same key decryptable. The encryption method attempts to make this more difficult by not encrypting silent blocks (with all sample nybbles equal to 0), as their scale is known to be 0.

Even if the encrypted CRI ADX is the only sample available, it is possible to determine a key by assuming that the scale values of the decrypted CRI ADX must fall within a "low range". This method does not necessarily find the key used to encrypt the file, however. While it can always determine keys that produce an apparently correct output, errors may exist undetected.  This is due to the increasingly random distribution of the lower bits of the scale values, which becomes impossible to separate from the randomness added by the encryption.

AHX decoding 
As noted earlier, AHX is just an implementation of MPEG2 audio and the decoding method is basically the same as the standard, it is possible just to demux the stream from the CRI ADX container and feed it through a standard MPEG Audio decoder like mpg123. The CRI ADX header's "sample rate" and "total samples" are usually correct if a decoder needs them (so should be set by encoder/muxer implementations) but most of the other fields such as the "block size" and "sample bitdepth" will usually be zero — as noted above, the looping functionality is also unavailable.

References

External links
 ADX product page at CRI Middleware website
 vgmstream WinAMP codec with source (supports ADX)
 Dreamcast utilities including ADX converters with source ( ( 2009-10-24)
 CRI ADX Description from multimedia.cx Wiki
 ADX technical description on vgmstream Wiki

Audio codecs
Lossy compression algorithms
Computer file formats
Articles with example C code