| 1 | = Decoding a sv7 frame = |
| 2 | |
| 3 | For more details on the entropy decoding of musepack, see mpc_decoder_read_bitstream_sv7() in [source:libmpc/trunk/libmpcdec/mpc_decoder.c mpc_decoder.c] |
| 4 | |
| 5 | === datas from stream header : === |
| 6 | |
| 7 | max_band : max number of bands used in the stream[[BR]] |
| 8 | ms : 1 if mid/side stereo is used, 0 else[[BR]] |
| 9 | |
| 10 | |
| 11 | === used datas : === |
| 12 | |
| 13 | L/R means that there is a data for the right and the left channel[[BR]] |
| 14 | |
| 15 | max_used_band : number of bands used in this frame[[BR]] |
| 16 | res[L/R]![32] : Quantization level of each band, range from -1 to 17[[BR]] |
| 17 | ms_flag![32] : 1 if mid/side stereo is used for this band, 0 else[[BR]] |
| 18 | SCFI[L/R]![32] : Scalefactor index, range from 0 to 3[[BR]] |
| 19 | SCF[L/R]![32]![3] : Scalefactor, range from -6 to 121. There is 3 scalefactors per band (so 1 scalefactor for 12 samples)[[BR]] |
| 20 | Q[L/R]![32]![36] : Quantized samples values of the bands, ranges from -2^15^-1 to 2^15^-1[[BR]] |
| 21 | |
| 22 | |
| 23 | == 1) Decoding the quantization levels == |
| 24 | |
| 25 | === 1.1) first band === |
| 26 | {{{ |
| 27 | res[L][0] = read 4 bits from stream as unsigned integer |
| 28 | res[R][0] = read 4 bits from stream as unsigned integer |
| 29 | |
| 30 | if (res[L][0] != 0 && res[R][0] != 0) { |
| 31 | max_used_band = 1; |
| 32 | if (ms == 1) |
| 33 | ms_flag[0] = read 1 bit from stream |
| 34 | } |
| 35 | }}} |
| 36 | |
| 37 | === 1.2) other bands === |
| 38 | {{{ |
| 39 | for each band n from 1 until max_band do : |
| 40 | |
| 41 | index = decode from stream as huffman code using table mpc_HuffHdr |
| 42 | if (index != 4) |
| 43 | res[L][n] = res[L][n - 1] + index |
| 44 | else |
| 45 | res[L][n] = read 4 bits from stream as insigned integer |
| 46 | |
| 47 | if (index != 4) |
| 48 | res[R][n] = res[R][n - 1] + index |
| 49 | else |
| 50 | res[R][n] = read 4 bits from stream as insigned integer |
| 51 | |
| 52 | if (res[L][n] != 0 && res[R][n] != 0) { |
| 53 | max_used_band = n + 1; |
| 54 | if (ms == 1) |
| 55 | ms_flag[n] = read 1 bit from stream |
| 56 | } |
| 57 | }}} |
| 58 | |
| 59 | == 2) Scalefactors index == |
| 60 | {{{ |
| 61 | for each band n from 0 to max_used_band - 1 do : |
| 62 | |
| 63 | if (res[L][n] != 0) |
| 64 | SCFI[L][n] = decode from stream as huffman code using table mpc_HuffSCFI |
| 65 | if (res[R][n] != 0) |
| 66 | SCFI[R][n] = decode from stream as huffman code using table mpc_HuffSCFI |
| 67 | }}} |
| 68 | |
| 69 | == 3) Scalefactors == |
| 70 | |
| 71 | === 3.1) Definition === |
| 72 | |
| 73 | define decode_scalefactor(ref) as : |
| 74 | {{{ |
| 75 | index = decode from stream as huffman code using table mpc_HuffDSCF |
| 76 | if (index != 8) |
| 77 | return (ref + index) |
| 78 | else |
| 79 | return (read 6 bits from stream as insigned integer) |
| 80 | }}} |
| 81 | |
| 82 | === 3.2) Decoding === |
| 83 | {{{ |
| 84 | for each band n from 0 to max_used_band - 1 do : |
| 85 | |
| 86 | if (res[L][n] != 0) { |
| 87 | SCF[L][n][0] = decode_scalefactor(SCF[L][n][2]) |
| 88 | if (SCFI[L][n] & 2) |
| 89 | SCF[L][n][1] = SCF[L][n][0] |
| 90 | else |
| 91 | SCF[L][n][1] = decode_scalefactor(SCF[L][n][0]) |
| 92 | |
| 93 | if (SCFI[L][n] & 1) |
| 94 | SCF[L][n][2] = SCF[L][n][1] |
| 95 | else |
| 96 | SCF[L][n][2] = decode_scalefactor(SCF[L][n][1]) |
| 97 | } |
| 98 | |
| 99 | do the same as before replacing L channel by R |
| 100 | }}} |
| 101 | |
| 102 | ,,note : SCF[L][n][2] when used as reference to decode SCF[L][n][0] is the last decoded scalefactor (in the last frame). So here there is a frame dependency,, |
| 103 | |
| 104 | == 4) Samples == |
| 105 | {{{ |
| 106 | for each band n from 0 to max_used_band - 1 do : |
| 107 | |
| 108 | switch (res[L][n]) { |
| 109 | case -1 |
| 110 | set each sample in Q[L][n] to a random number whose range is -510 to 510 // distribution needs to be precised |
| 111 | case 1 |
| 112 | index = read 1 bit from stream as insigned integer |
| 113 | huff_table = mpc_HuffQ[0][index] |
| 114 | for each 3 samples k in Q[L][n] do : |
| 115 | index = decode from stream as huffman code using table huff_table |
| 116 | Q[L][n][k] = idx30[index] |
| 117 | Q[L][n][k+1] = idx31[index] |
| 118 | Q[L][n][k+2] = idx32[index] |
| 119 | with : |
| 120 | idx30[] = { -1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1}; |
| 121 | idx31[] = { -1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1}; |
| 122 | idx32[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 123 | case 2 |
| 124 | index = read 1 bit from stream as insigned integer |
| 125 | huff_table = mpc_HuffQ[1][index] |
| 126 | for each 2 samples k in Q[L][n] do : |
| 127 | index = decode from stream as huffman code using table huff_table |
| 128 | Q[L][n][k] = idx50[index] |
| 129 | Q[L][n][k+1] = idx51[index] |
| 130 | with : |
| 131 | idx50[] = { -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2} |
| 132 | idx51[] = { -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2} |
| 133 | case 3, 4, 5, 6, 7 |
| 134 | index = read 1 bit from stream as insigned integer |
| 135 | huff_table = mpc_HuffQ[res[L][n] - 1][index] |
| 136 | for each sample k in Q[L][n] do : |
| 137 | Q[L][n][k] = decode from stream as huffman code using table huff_table |
| 138 | other case |
| 139 | for each sample k in Q[L][n] do : |
| 140 | Q[L][n][k] = (read res_bits[res[L][n]] bits from stream as insigned integer) - dc[res[L][n]] |
| 141 | with : |
| 142 | res[] = { 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} |
| 143 | dc[] = { 0, 1, 2, 3, 4, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767} |
| 144 | } |
| 145 | |
| 146 | do the same as before replacing L channel by R |
| 147 | }}} |
| 148 | |
| 149 | == 5) Dequantization and inverse filtering == |
| 150 | |
| 151 | You will need SCF and Q for this step.[[br]] |
| 152 | I think that those steps are the same as mpeg layer 2 dequantization and inverse filtering. Is this true ? |