WavPack

From MultimediaWiki
Revision as of 22:01, 24 September 2006 by Kostya (talk | contribs)
Jump to navigation Jump to search

WavPack is an open source lossless audio coding algorithm.

WavPack v.4

File Format

General details of WavPack format can be found in file 'format.txt' in wavpack sources archive. WavPack file consists of blocks each beginning with 'wvpk'. Every block contains all information about sound data - sampling rate, channels, bits per sample, etc. and so-called metadata. Metadata may contain different coefficients using for restoring samples, correction bitstream and actual compressed samples.

Samples coding

Samples are stored in metadata block with ID=0x0A and are packed with modified Golomb codes. Decoding process is specified below where get_unary() is the function which returns length of '1'-bits string (i.e. 111110b = 5, 10b = 1). Codeset is adaptively divided into four sets and every code has unary prefix (possibly escaped) defining interval of this code and mantis part like in Golomb code.

 if(last_zero){
   n = 0;
   last_zero = 0;
 }else{
   n = get_unary();
   if(n == 16){
     n2 = get_unary();
     if(n2 < 2) n += n2;
     else n += (1 << (n2-1)) | getbits(n2-1);
   }
   last_one = n & 1;
   if(last_one)
     n = (n>>1) + 1;
   else
     n = n >> 1;
   last_zero = !last_one;
 }
 if(n == 0){
   base = 0;
   add = median[0] - 1;
   decrease median[0];
 } else if(n == 1){
   base = median[0];
   add = median[1] - 1;
   increase median[0];
   decrease median[1];
 } else {
   base = median[0] + median[1] + median[2] * (n - 2);
   add = median[2] - 1;
   increase median[0];
   increase median[1];
   if(n == 2) derease median[2];
   else increase median[2];
 }
 k = log2(add);
 ex = (1 << k) - add - 1;
 t2 = getbits(k - 1);
 if(t2 >= ex)
   t2 = t2 * 2 - ex + getbit();
 sign = getbit();
 if(sign==0) result = base + t2;
 else result = ~(base + t2);