Index: libmpc/branches/r2d/libmpcdec/mpc_decoder.c
===================================================================
--- libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 158)
+++ libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 161)
@@ -36,4 +36,5 @@
 #include <string.h>
 #include <mpcdec/mpcdec.h>
+#include <mpc/minimax.h>
 #include "decoder.h"
 #include "huffman.h"
@@ -44,4 +45,6 @@
 mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits);
 mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table);
+mpc_uint32_t mpc_bits_enum_dec(mpc_bits_reader * r, mpc_uint_t k, mpc_uint_t n);
+mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max);
 
 //SV7 tables
@@ -50,4 +53,14 @@
 extern const mpc_huffman    mpc_table_HuffSCFI [ 4];
 extern const mpc_huffman    mpc_table_HuffDSCF [16];
+
+//SV8 tables
+extern const mpc_huffman mpc_table_HuffRes [2][17];
+extern const mpc_huffman * mpc_table_HuffQ_8 [2][8];
+extern const mpc_huffman mpc_table_HuffQ9up_8 [256];
+extern const mpc_huffman mpc_table_HuffBands [33];
+extern const mpc_huffman mpc_table_HuffSCFI_1 [4];
+extern const mpc_huffman mpc_table_HuffSCFI_2 [16];
+extern const mpc_huffman mpc_table_HuffDSCF_1 [64];
+extern const mpc_huffman mpc_table_HuffDSCF_2 [65];
 
 //------------------------------------------------------------------------------
@@ -68,4 +81,6 @@
 //------------------------------------------------------------------------------
 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r);
+void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r,
+									mpc_bool_t is_key_frame);
 static void mpc_decoder_requantisierung(mpc_decoder *d);
 
@@ -143,5 +158,9 @@
 	}
 
-	mpc_decoder_read_bitstream_sv7(d, r);
+	if (d->stream_version == 8)
+		mpc_decoder_read_bitstream_sv8(d, r, i->is_key_frame);
+	else
+		mpc_decoder_read_bitstream_sv7(d, r);
+
 	if (d->samples_to_skip < MPC_FRAME_LENGTH + MPC_DECODER_SYNTH_DELAY) {
 		mpc_decoder_requantisierung(d);
@@ -483,4 +502,194 @@
 }
 
-
-
+void mpc_decoder_read_bitstream_sv8(mpc_decoder * d, mpc_bits_reader * r, mpc_bool_t is_key_frame)
+{
+    // these arrays hold decoding results for bundled quantizers (3- and 5-step)
+	static const mpc_int8_t idx50[125] = {-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, -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, -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, -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, -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};
+	static const mpc_int8_t idx51[125] = {-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, -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, -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, -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, -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};
+	static const mpc_int8_t idx52[125] = {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
+
+	mpc_int32_t n, Max_used_Band;
+	const mpc_huffman *Table, *Tables[2];
+
+	/***************************** Header *****************************/
+
+	if (is_key_frame == MPC_TRUE) {
+		Max_used_Band = mpc_bits_log_dec(r, d->max_band);
+	} else {
+		Max_used_Band = d->last_max_band + mpc_bits_huff_dec(r, mpc_table_HuffBands);
+		if (Max_used_Band > 32) Max_used_Band -= 33;
+	}
+	d->last_max_band = Max_used_Band;
+
+	if (Max_used_Band) {
+		d->Res_L[Max_used_Band-1] = mpc_bits_huff_dec(r, mpc_table_HuffRes[0]);
+		d->Res_R[Max_used_Band-1] = mpc_bits_huff_dec(r, mpc_table_HuffRes[0]);
+		for ( n = Max_used_Band - 2; n >= 0; n--) {
+			d->Res_L[n] = mpc_bits_huff_dec(r, mpc_table_HuffRes[d->Res_L[n + 1] > 2]) + d->Res_L[n + 1];
+			if (d->Res_L[n] > 15) d->Res_L[n] -= 17;
+			d->Res_R[n] = mpc_bits_huff_dec(r, mpc_table_HuffRes[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
+			if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
+		}
+
+		if (d->ms) {
+			int cnt = 0, tot = 0;
+			mpc_uint32_t tmp = 0;
+			for( n = 0; n < Max_used_Band; n++)
+				if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 )
+					tot++;
+			cnt = mpc_bits_log_dec(r, tot);
+			if (cnt != 0 && cnt != tot)
+				tmp = mpc_bits_enum_dec(r, mini(cnt, tot-cnt), tot);
+			if (cnt * 2 > tot) tmp = ~tmp;
+			for( n = Max_used_Band - 1; n >= 0; n--)
+				if ( d->Res_L[n] != 0 || d->Res_R[n] != 0 ) {
+					d->MS_Flag[n] = tmp & 1;
+					tmp >>= 1;
+				}
+		}
+	}
+
+	for( n = Max_used_Band; n <= d->max_band; n++)
+		d->Res_L[n] = d->Res_R[n] = 0;
+
+
+
+	/****************************** SCFI ******************************/
+	if (is_key_frame == MPC_TRUE){
+		for( n = 0; n < 32; n++)
+			d->DSCF_Flag_L[n] = d->DSCF_Flag_R[n] = 1; // new block -> force key frame
+	}
+
+	Tables[0] = mpc_table_HuffSCFI_1;
+	Tables[1] = mpc_table_HuffSCFI_2;
+	for ( n = 0; n < Max_used_Band; n++ ) {
+		int tmp = 0, cnt = -1;
+		if (d->Res_L[n]) cnt++;
+		if (d->Res_R[n]) cnt++;
+		if (cnt >= 0) {
+			tmp = mpc_bits_huff_dec(r, Tables[cnt]);
+			if (d->Res_L[n]) d->SCFI_L[n] = tmp >> (2 * cnt);
+			if (d->Res_R[n]) d->SCFI_R[n] = tmp & 3;
+		}
+	}
+
+	/**************************** SCF/DSCF ****************************/
+
+	for ( n = 0; n < Max_used_Band; n++ ) {
+		mpc_int32_t * SCF = d->SCF_Index_L[n];
+		mpc_uint32_t Res = d->Res_L[n], SCFI = d->SCFI_L[n];
+		mpc_bool_t * DSCF_Flag = &d->DSCF_Flag_L[n];
+
+		do {
+			if ( Res ) {
+				int m;
+				if (*DSCF_Flag == 1) {
+					SCF[0] = (mpc_int32_t)mpc_bits_read(r, 7) - 6;
+					*DSCF_Flag = 0;
+				} else {
+					mpc_uint_t tmp = mpc_bits_huff_dec(r, mpc_table_HuffDSCF_2);
+					if (tmp == 64)
+						tmp += mpc_bits_read(r, 6);
+					SCF[0] = ((SCF[2] - 25 + tmp) & 127) - 6;
+				}
+				for( m = 0; m < 2; m++){
+					if (((SCFI << m) & 2) == 0) {
+						mpc_uint_t tmp = mpc_bits_huff_dec(r, mpc_table_HuffDSCF_1);
+						if (tmp == 31)
+							tmp = 64 + mpc_bits_read(r, 6);
+						SCF[m + 1] = ((SCF[m] - 25 + tmp) & 127) - 6;
+					} else
+						SCF[m + 1] = SCF[m];
+				}
+			}
+			Res = d->Res_R[n];
+			SCFI = d->SCFI_R[n];
+			DSCF_Flag = &d->DSCF_Flag_R[n];
+		} while ( SCF == d->SCF_Index_L[n] && (SCF = d->SCF_Index_R[n]));
+	}
+
+	/***************************** Samples ****************************/
+	for ( n = 0; n < Max_used_Band; n++ ) {
+		mpc_int32_t *q = d->Q[n].L, Res = d->Res_L[n];
+		static const unsigned int thres[] = {0, 0, 3, 0, 0, 1, 3, 4, 8};
+		static const mpc_int8_t HuffQ2_var[5*5*5] =
+		{6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 6};
+
+		do {
+			mpc_int32_t k = 0, idx = 1;
+			switch (Res) {
+				case -1:
+					for ( ; k<36; k++ ) {
+						mpc_uint32_t tmp = mpc_random_int(d);
+						q[k] = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
+					}
+					break;
+				case 1:
+					Table = mpc_table_HuffQ_8 [0][0];
+					for( ; k < 36; ){
+						int kmax = k + 18;
+						mpc_uint_t cnt = mpc_bits_huff_dec(r, Table);
+						idx = 0;
+						if (cnt > 0 && cnt < 18)
+								idx = mpc_bits_enum_dec(r, cnt <= 9 ? cnt : 18 - cnt, 18);
+						if (cnt > 9) idx = ~idx;
+						for ( ; k < kmax; k++) {
+							q[k] = 0;
+							if ( idx & (1 << 17) )
+								q[k] = (mpc_bits_read(r, 1) << 1) - 1;
+							idx <<= 1;
+						}
+					}
+					break;
+				case 2:
+					Tables[0] = mpc_table_HuffQ_8 [0][1];
+					Tables[1] = mpc_table_HuffQ_8 [1][1];
+					idx = 2 * thres[Res];
+					for ( ; k < 36; k += 3) {
+						int tmp = mpc_bits_huff_dec(r, Tables[idx > thres[Res]]);
+						q[k] = idx50[tmp];
+						q[k + 1] = idx51[tmp];
+						q[k + 2] = idx52[tmp];
+						idx = (idx >> 1) + HuffQ2_var[tmp];
+					}
+					break;
+				case 3:
+				case 4:
+					Table = mpc_table_HuffQ_8[0][Res - 1];
+					for ( ; k < 36; k += 2 ) {
+						union {
+							mpc_uint8_t sym;
+							struct { mpc_int16_t s1:4, s2:4; };
+						} tmp;
+						tmp.sym = (mpc_uint8_t) mpc_bits_huff_dec(r, Table);
+						q[k] = tmp.s1;
+						q[k + 1] = tmp.s2;
+					}
+					break;
+				case 5:
+				case 6:
+				case 7:
+				case 8:
+					Tables[0] = mpc_table_HuffQ_8 [0][Res - 1];
+					Tables[1] = mpc_table_HuffQ_8 [1][Res - 1];
+					idx = 2 * thres[Res];
+					for ( ; k < 36; k++ ) {
+						q[k] = mpc_bits_huff_dec(r, Tables[idx > thres[Res]]);
+						idx = (idx >> 1) + absi(q[k]);
+					}
+					break;
+				case 9: case 10: case 11: case 12: case 13: case 14: case 15:
+					for ( ; k < 36; k++ ) {
+						q[k] = mpc_bits_huff_dec(r, mpc_table_HuffQ9up_8);
+						if (Res != 9)
+							q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
+						q[k] -= Dc[Res];
+					}
+					break;
+			}
+
+			Res = d->Res_R[n];
+		} while (q == d->Q[n].L && (q = d->Q[n].R));
+	}
+}
+
