wiki:SV7FrameDecoding

Decoding a sv7 frame

For more details on the entropy decoding of musepack, see mpc_decoder_read_bitstream_sv7() in mpc_decoder.c
Huffman tables used in the decoding are declared in huffman.c. sv7 huffman tables are not canonical huffman tables.

datas from stream header :

max_band : max number of bands used in the stream
ms : 1 if mid/side stereo is used, 0 else

used datas :

L/R means that there is a data for the right and the left channel

max_used_band : number of bands used in this frame
res[L/R][32] : Quantization level of each band, range from -1 to 17
ms_flag[32] : 1 if mid/side stereo is used for this band, 0 else
SCFI[L/R][32] : Scalefactor index, range from 0 to 3
SCF[L/R][32][3] : Scalefactor, range from -6 to 121. There is 3 scalefactors per band (so 1 scalefactor for 12 samples)
Q[L/R][32][36] : Quantized samples values of the bands, ranges from -215-1 to 215-1

1) Decoding the quantization levels

1.1) first band

res[L][0] = read 4 bits from stream as unsigned integer
res[R][0] = read 4 bits from stream as unsigned integer

if (res[L][0] != 0 && res[R][0] != 0) {
	max_used_band = 1;
	if (ms == 1)
		ms_flag[0] = read 1 bit from stream
}

1.2) other bands

for each band n from 1 until max_band do :

	index = decode from stream as huffman code using table mpc_HuffHdr
	if (index != 4)
		res[L][n] = res[L][n - 1] + index
	else
		res[L][n] = read 4 bits from stream as insigned integer
		
	if (index != 4)
		res[R][n] = res[R][n - 1] + index
	else
		res[R][n] = read 4 bits from stream as insigned integer
	
	if (res[L][n] != 0 && res[R][n] != 0) {
		max_used_band = n + 1;
		if (ms == 1)
			ms_flag[n] = read 1 bit from stream
	}

2) Scalefactors index

for each band n from 0 to max_used_band - 1 do :

	if (res[L][n] != 0)
		SCFI[L][n] = decode from stream as huffman code using table mpc_HuffSCFI
	if (res[R][n] != 0)
		SCFI[R][n] = decode from stream as huffman code using table mpc_HuffSCFI

3) Scalefactors

3.1) Definition

define decode_scalefactor(ref) as :

	index = decode from stream as huffman code using table mpc_HuffDSCF
	if (index != 8)
		return (ref + index)
	else
		return (read 6 bits from stream as insigned integer)

3.2) Decoding

for each band n from 0 to max_used_band - 1 do :

	if (res[L][n] != 0) {
		SCF[L][n][0] = decode_scalefactor(SCF[L][n][2])
		if (SCFI[L][n] & 2)
			SCF[L][n][1] = SCF[L][n][0]
		else
			SCF[L][n][1] = decode_scalefactor(SCF[L][n][0])
		
		if (SCFI[L][n] & 1)
			SCF[L][n][2] = SCF[L][n][1]
		else
			SCF[L][n][2] = decode_scalefactor(SCF[L][n][1])
	}
	
	do the same as before replacing L channel by R

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

4) Samples

for each band n from 0 to max_used_band - 1 do :

	switch (res[L][n]) {
		case -1
			set each sample in Q[L][n] to a random number whose range is -510 to 510 // distribution needs to be precised
		case 1
			index = read 1 bit from stream as insigned integer
			huff_table = mpc_HuffQ[0][index]
			for each 3 samples k in Q[L][n] do :
				index = decode from stream as huffman code using table huff_table
				Q[L][n][k] = idx30[index]
				Q[L][n][k+1] = idx31[index]
				Q[L][n][k+2] = idx32[index]
				with :
				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};
				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};
				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};
		case 2
			index = read 1 bit from stream as insigned integer
			huff_table = mpc_HuffQ[1][index]
			for each 2 samples k in Q[L][n] do :
				index = decode from stream as huffman code using table huff_table
				Q[L][n][k] = idx50[index]
				Q[L][n][k+1] = idx51[index]
				with :
				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}
				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}
		case 3, 4, 5, 6, 7
			index = read 1 bit from stream as insigned integer
			huff_table = mpc_HuffQ[res[L][n] - 1][index]
			for each sample k in Q[L][n] do :
				Q[L][n][k] = decode from stream as huffman code using table huff_table
		other case
			for each sample k in Q[L][n] do :
				Q[L][n][k] = (read res_bits[res[L][n]] bits from stream as insigned integer) - dc[res[L][n]]
				with :
				res[] = { 0,  0,  0,  0,  0,  0,  0,  0,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16}
				dc[] = { 0, 1, 2, 3, 4, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767}
	}
	
	do the same as before replacing L channel by R

5) Dequantization and inverse filtering

You will need SCF, Q and ms_flag for this step.
I think that those steps are the same as mpeg layer 2 dequantization and inverse filtering. Is this true ?

Last modified 16 years ago Last modified on 05/25/07 17:37:30