La Lossless Audio

From MultimediaWiki
Jump to navigation Jump to search

La is a lossless audio coding algorithm.

Container format

All values are little-endian.

File header:

3  bytes - "LA0"
1  byte  - model version ('2'-'4')
4  bytes - raw audio size
32 bytes - WAV header (starting from "WAVE....fmt ")
4  bytes - number of coded samples (for all channels)
1  byte  - flags (bit 0 - seek table is present, bit 1 - more filters in model 4)
4  bytes - CRC

If seek table is present it is stored as 32-bit words with the frame end (first frame starts immediately after the seek table). Frames contain audio for 16 blocks of 73728 samples for models 1-2 and 19 blocks of 61440 samples for model 4.

Compression details

Audio is coded as residues with either arithmetic coder (models 2-3) or range coder (model 4) plus many layers of filters performing decorrelation often on both channels at once.

General residue coding

Residues are coded using adaptive model and weight in the following fashion:

 weight = 0x4000;
 if (decode_freq(1) == 0) {
  channel data is all zeroes
 }
 for (i = 0; i < N; i++) {
   freqs[i] = decode_model(PREDEFINED_FREQ_MODEL[i]);
 }
 for (i = N; i < 192; i++) {
   freqs[i] = decode_freq(i + 1);
 }
 for all residues {
   mant = decode_model(freqs);
   freqs[mant]--;
   exp = weight >> 6;
   if (exp == 191) exp = 1 << decode_freq(5);
   val = mant * exp;
   if (exp > 1) val += decode_freq(exp);
   weight = update_weight(weight, val, mode);
   residue = (val & 1) ? ((val + 1) >> 1) : -(val >> 1);
 }

Stereo data is coded interleaved using common model.

Model 2 has first 10 predefined frequencies for decoding model frequencies, models 3 and 4 have 32 predefined frequencies. Model 2 updates weight as weight += (val + 1) / 2 - ((weight + 0x10) >> 5); Models 3-4 have following update modes:

 0: weight +=  ((val + 4) / 2 - ((weight + 0x10) >> 5)) >> 1;
 1: weight +=   (val + 4) / 2 - ((weight + 0x10) >> 5);
 2: weight += (((val + 4) / 2 - ((weight + 0x10) >> 5)) >> 1) * 3;
 3: weight +=  ((val + 4) / 2 - ((weight + 0x10) >> 5)) * 2;

Channel decorrelation

Decorrelation works by maintaining the rolling average error and performing decorrelation only when it's non-negative:

 avg = 0;
 for each sample {
   if avg >= 0 {
     new_l = r - l/2;
     new_r = l + new_l;
   }
   avg = (avg * 0xFF) >> 8;
   if (new_l == new_r)
     avg++;
   else
     avg--;
 }

Model 2 uses floating-point averages and model 4 uses decorrelation formula new_l = l; new_r = l - r; but essentially it is performed in the same way.

Filtering

All large filters are LMS filters that adapt coefficients with a value calculated from the difference between the predicted and actual sample value. The number of filters, their orders and other parameters (e.g. the way the filter adaptation value is calculated) depend on the model. Internally the filters are divided into four categories: channel decorrelator, delta filter (predicts value from one or two previous samples), quick filter (an adaptive filter with order up to 16), and big filter. The difference between big and quick filter is also the way in which filter coefficients are adapted.

Filters that predict channel data are combined with a stereo predictor which may introduce an additional filtering on reconstructed samples.

Model 2 filters

All filters are listed in the order they are applied. "Single-channel" means the filter works only using information for its own channel instead of using both channel samples.

  • channel decorrelator
  • delta filter (pred = last * 31 >> 5;) single-channel
  • quick filter with order 7
  • big filter with order 128 single-channel
  • quick filter with order 4
  • big filter with order 320 single-channel
  • big filter with order 256
  • big filter with order 64

Model 3 filters

  • channel decorrelator
  • delta filter (pred = last;) single-channel
  • quick filter order 10
  • big filter order 64
  • quick filter order 4
  • big filter order 320 single-channel
  • big filter order 256 single-channel
  • big filter order 64
  • quick filter order 4

Model 4 filters

  • channel decorrelator (only for stereo unlike in other models)
  • delta filter (pred = last * weight >> 8; with a weight selected adaptively depending on average of -src[-2] + src[-1] * 2 - src[0]) single-channel
  • quick filter order 16
  • big filter order 512 single-channel
  • big filter order 16 (only when extra compression bit is set in header flags)
  • big filter order 288 (only when extra compression bit is set in header flags) single-channel
  • big filter order 96
  • big filter order 16