QDesign Music Codec

From MultimediaWiki
Revision as of 03:37, 7 May 2016 by Kostya (talk | contribs) (QDM1 description)
Jump to navigation Jump to search

QDesign Music Codec is a perceptual audio codec commonly used in MOV files between 1998 and 2005, after which it was more or less supplanted by AAC for the same role. Variants of QDM2 with 32, 16 or 8 subbands exist, 16 and 8 decode with noticeable artifacts (metallic sound).

QDMC was the first version of the codec which was apparently short lived and quickly superceded by the second version, QDM2. While QDM2 is supported in open source software (via FFmpeg), QDMC is not supported at all at this point.

QDesign Music Codec (v1)

This codec codes audio as a set of tones and noise parameters that are later reconstructed with FFT.

For sampling rates < 16kHz subframe should be 64 samples and full frame should be 2048 samples. For 16-32kHz it's 128 and 4096 samples correspondingly. For >=32kHz it's 256 and 8192 samples.

Bits are read from LSB using 16-bit little-endian words.

Subblock format

Noise data
 for (band = 0; band < noise_bands[mode]; band++) {
   v = get_huff(noise_val_tree);
   if (v & 1)
     v = (v + 1) >> 1;
   else
     v = -v / 2;
   noise[band][0] = v - 1;
   lastval = v;
   for (idx = 0; idx < 16;) {
     len = get_huff_long(noise_seg_len_tree);
     v = get_huff(noise_val_tree);
     if (v & 1)
       v = (v + 1) >> 1;
     else
       v = -v / 2;
     newval = lastval + v;
     for (j = 0; j < len; j++)
       noise[band][idx + 1 + j] = lastval + v * j / len;
     lastval = newval;
     idx += len + 1;
   }
 }
Wave data
 for (group = 0; group < 5; group++) {
   group_size = 1 << (frame_bits - group - 1);
   group_bits = 4 - group;
   freq = 1;
   off  = 0;
   pos2 = 0;
   do {
     freq += get_huff_long(freq_diff_tree);
     while (freq >= group_size - 1) {
       freq -= group_size - 2;
       off  += 1 << group_bits;
       pos2 += group_size;
     }
     if (pos2 >= frame_size)
       break;
     if (channels > 1)
       stereo_mode = get_bits(2);
     else
       stereo_mode = 0;
     amp   = get_huff(amp_tree);
     phase = get_bits(3);
     if (stereo_mode & 2) {
       amp2   =  amp   - get_huff(amp_diff_tree);
       phase2 = (phase - get_huff(phase_diff_tree)) & 7;
     } else {
       amp2   = 0;
       phase2 = 0;
     }
     add tone <off, freq, stereo_mode & 1, amp, phase>
     if (stereo_mode & 2)
       add tone <off, freq, ~stereo_mode & 1, amp2, phase2>
   } while (freq < group_size);
 }
Huffman code reading

Trees are static but the codes can have an additional data afterwards:

 int get_huff(tree) {
   v = read_code(tree);
   if (v)
     return v - 1;
   else {
     v = get_bits(3) + 1;
     return get_bits(v);
   }
 }
 int get_huff_long(tree) {
   v = read_code(tree);
   if (v)
     v--;
   else
     v = get_bits(get_bits(3) + 1);
   return code_prefix[v] + get_bits(v >> 2);
 }
 unsigned code_prefix[] = {
   0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
   0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
   0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
   0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
   0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
   0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
   0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
   0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
   0x3FFFC
 };

Tables

Noise bands selector (depending on bitrate):

 4, 3, 2, 1, 0, 0, 0, 0

Number of noise bands:

 19, 14, 11, 9, 4

Noise subbands:

 0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64, 80, 96, 120, 144, 176, 208, 240, 256
 0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104, 128, 160, 208, 256
 0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208, 256
 0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256
 0, 4, 16, 32, 64, 256

Levels table:

 1.1875, 1.6835938, 2.375, 3.3671875, 4.75, 6.734375, 9.5, 13.46875,
 19.0, 26.9375, 38.0, 53.875, 76.0, 107.75, 152.0, 215.5,
 304.0, 431.0, 608.0, 862.0, 1216.0, 1724.0, 2432.0, 3448.0,
 4864.0, 6896.0, 9728.0, 13792.0, 19456.0, 27584.0, 38912.0, 55168.0,
 77824.0, 110336.0, 155648.0, 220672.0, 311296.0, 441344.0, 622592.0, 882688.0,
 1245184.0, 1765376.0, 2490368.0, 3530752.0, 4980736.0, 7061504.0

Frequencies for the trees

Table 0 — noise value

 3233, 1195, 1897, 877, 1240, 368, 364, 222, 103, 125, 18, 68, 10, 25, 7, 13,
 0, 18, 0, 20, 0, 31, 0, 28, 0, 31, 0, 19, 0, 23, 0, 10, 0, 9, 0, 1,

Table 1 — noise segment length

 7647, 1011, 380, 215, 180, 65, 33, 12, 4, 0, 0, 0, 16, 0, 0, 0, 84

Table 2 — amplitude

 2436, 1411, 692, 389, 316, 310, 368, 457, 651, 1359, 2563, 4732, 8946, 17150, 29621, 44245,
 50156, 45928, 33262, 20474, 9855, 3813, 1378, 514, 154, 82, 3

Table 3 — frequency difference

 57884, 27424, 14988, 11027, 17889, 14609, 11790, 9479,
 15948, 11581, 7815, 6917, 10486, 6603, 4897, 3983,
 5120, 3479, 2949, 2626, 3443, 2984, 3725, 3593,
 3307, 3283, 2954, 2384, 1777, 2042, 1641, 798,
 769, 863, 776, 239, 162, 104, 63, 43,
 49, 30, 6, 1, 0, 4, 1

Table 4 — amplitude difference tree

 8392, 14998, 5103, 1797, 648, 237, 42, 7

Table 5 — phase difference tree

 8860, 9620, 2138, 897, 618, 834, 1920, 6337

Converting frequencies into codes is left as an exercise to the reader.