Index: libmpc/branches/r2d/libmpcdec/mpc_decoder.c
===================================================================
--- libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 288)
+++ libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 290)
@@ -46,18 +46,18 @@
 
 //SV7 tables
-extern const mpc_huffman*   mpc_table_HuffQ [2] [8];
-extern const mpc_huffman    mpc_table_HuffHdr  [10];
+extern const mpc_lut_data   mpc_HuffQ [7] [2];
+extern const mpc_lut_data   mpc_HuffHdr;
 extern const mpc_huffman    mpc_table_HuffSCFI [ 4];
-extern const mpc_huffman    mpc_table_HuffDSCF [16];
+extern const mpc_lut_data   mpc_HuffDSCF;
 
 //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];
+extern const mpc_huffman mpc_table_HuffBands[33];
+extern const mpc_lut_data mpc_HuffRes [2];
+extern const mpc_lut_data mpc_HuffQ_8 [8][2];
+extern const mpc_lut_data mpc_HuffQ9up_8;
+extern const mpc_lut_data mpc_HuffSCFI_1;
+extern const mpc_lut_data mpc_HuffSCFI_2;
+extern const mpc_lut_data mpc_HuffDSCF_1;
+extern const mpc_lut_data mpc_HuffDSCF_2;
 
 //------------------------------------------------------------------------------
@@ -131,4 +131,5 @@
 		mpc_decoder_setup(p_tmp);
 		mpc_decoder_set_streaminfo(p_tmp, si);
+		huff_init_lut(LUT_DEPTH); // FIXME : this needs to be called only once when the library is loaded
 	}
 
@@ -372,8 +373,8 @@
     // consecutive subbands
 	for ( n = 1; n <= d->max_band; n++ ) {
-		idx   = mpc_bits_huff_dec(r, mpc_table_HuffHdr);
+		idx   = mpc_bits_huff_lut(r, & mpc_HuffHdr);
 		d->Res_L[n] = (idx!=4) ? d->Res_L[n - 1] + idx : (int) mpc_bits_read(r, 4);
 
-		idx   = mpc_bits_huff_dec(r, mpc_table_HuffHdr);
+		idx   = mpc_bits_huff_lut(r, & mpc_HuffHdr);
 		d->Res_R[n] = (idx!=4) ? d->Res_R[n - 1] + idx : (int) mpc_bits_read(r, 4);
 
@@ -400,12 +401,12 @@
 				switch (SCFI) {
 					case 1:
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
 						SCF[2] = SCF[1];
 						break;
 					case 3:
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
 						SCF[1] = SCF[0];
@@ -413,16 +414,16 @@
 						break;
 					case 2:
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
 						SCF[1] = SCF[0];
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
 						break;
 					case 0:
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[0] = (idx!=8) ? SCF[2] + idx : (int) mpc_bits_read(r, 6);
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[1] = (idx!=8) ? SCF[0] + idx : (int) mpc_bits_read(r, 6);
-						idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+						idx  = mpc_bits_huff_lut(r, & mpc_HuffDSCF);
 						SCF[2] = (idx!=8) ? SCF[1] + idx : (int) mpc_bits_read(r, 6);
 						break;
@@ -450,5 +451,5 @@
 		do {
 			mpc_int32_t k;
-			const mpc_huffman *Table;
+			const mpc_lut_data *Table;
 			switch (Res) {
 				case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
@@ -462,7 +463,7 @@
 					break;
 				case 1:
-					Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1];
+					Table = & mpc_HuffQ[0][mpc_bits_read(r, 1)];
 					for ( k = 0; k < 36; k += 3) {
-						idx = mpc_bits_huff_dec(r, Table);
+						idx = mpc_bits_huff_lut(r, Table);
 						q[k] = idx30[idx];
 						q[k + 1] = idx31[idx];
@@ -471,7 +472,7 @@
 					break;
 				case 2:
-					Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2];
+					Table = & mpc_HuffQ[1][mpc_bits_read(r, 1)];
 					for ( k = 0; k < 36; k += 2) {
-						idx = mpc_bits_huff_dec(r, Table);
+						idx = mpc_bits_huff_lut(r, Table);
 						q[k] = idx50[idx];
 						q[k + 1] = idx51[idx];
@@ -483,7 +484,7 @@
 				case 6:
 				case 7:
-					Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][Res];
+					Table = & mpc_HuffQ[Res - 1][mpc_bits_read(r, 1)];
 					for ( k = 0; k < 36; k++ )
-						q[k] = mpc_bits_huff_dec(r, Table);
+						q[k] = mpc_bits_huff_lut(r, Table);
 					break;
 				case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
@@ -508,5 +509,5 @@
 
 	mpc_int32_t n, Max_used_Band;
-	const mpc_huffman *Table, *Tables[2];
+	const mpc_lut_data * Table, * Tables[2];
 
 	/***************************** Header *****************************/
@@ -521,12 +522,12 @@
 
 	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]);
+		d->Res_L[Max_used_Band-1] = mpc_bits_huff_lut(r, & mpc_HuffRes[0]);
+		d->Res_R[Max_used_Band-1] = mpc_bits_huff_lut(r, & mpc_HuffRes[0]);
 		if (d->Res_L[Max_used_Band-1] > 15) d->Res_L[Max_used_Band-1] -= 17;
 		if (d->Res_R[Max_used_Band-1] > 15) d->Res_R[Max_used_Band-1] -= 17;
 		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];
+			d->Res_L[n] = mpc_bits_huff_lut(r, & mpc_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];
+			d->Res_R[n] = mpc_bits_huff_lut(r, & mpc_HuffRes[d->Res_R[n + 1] > 2]) + d->Res_R[n + 1];
 			if (d->Res_R[n] > 15) d->Res_R[n] -= 17;
 		}
@@ -561,6 +562,6 @@
 	}
 
-	Tables[0] = mpc_table_HuffSCFI_1;
-	Tables[1] = mpc_table_HuffSCFI_2;
+	Tables[0] = & mpc_HuffSCFI_1;
+	Tables[1] = & mpc_HuffSCFI_2;
 	for ( n = 0; n < Max_used_Band; n++ ) {
 		int tmp = 0, cnt = -1;
@@ -568,5 +569,5 @@
 		if (d->Res_R[n]) cnt++;
 		if (cnt >= 0) {
-			tmp = mpc_bits_huff_dec(r, Tables[cnt]);
+			tmp = mpc_bits_huff_lut(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;
@@ -588,5 +589,5 @@
 					*DSCF_Flag = 0;
 				} else {
-					mpc_uint_t tmp = mpc_bits_huff_dec(r, mpc_table_HuffDSCF_2);
+					mpc_uint_t tmp = mpc_bits_huff_lut(r, & mpc_HuffDSCF_2);
 					if (tmp == 64)
 						tmp += mpc_bits_read(r, 6);
@@ -595,5 +596,5 @@
 				for( m = 0; m < 2; m++){
 					if (((SCFI << m) & 2) == 0) {
-						mpc_uint_t tmp = mpc_bits_huff_dec(r, mpc_table_HuffDSCF_1);
+						mpc_uint_t tmp = mpc_bits_huff_lut(r, & mpc_HuffDSCF_1);
 						if (tmp == 31)
 							tmp = 64 + mpc_bits_read(r, 6);
@@ -620,9 +621,9 @@
 			if (Res != 0) {
 				if (Res == 2) {
-					Tables[0] = mpc_table_HuffQ_8 [0][1];
-					Tables[1] = mpc_table_HuffQ_8 [1][1];
+					Tables[0] = & mpc_HuffQ_8 [1][0];
+					Tables[1] = & mpc_HuffQ_8 [1][1];
 					idx = 2 * thres[Res];
 					for ( ; k < 36; k += 3) {
-						int tmp = mpc_bits_huff_dec(r, Tables[idx > thres[Res]]);
+						int tmp = mpc_bits_huff_lut(r, Tables[idx > thres[Res]]);
 						q[k] = idx50[tmp];
 						q[k + 1] = idx51[tmp];
@@ -631,8 +632,8 @@
 					}
 				} else if (Res == 1) {
-					Table = mpc_table_HuffQ_8 [0][0];
+					Table = & mpc_HuffQ_8 [0][0];
 					for( ; k < 36; ){
 						int kmax = k + 18;
-						mpc_uint_t cnt = mpc_bits_huff_dec(r, Table);
+						mpc_uint_t cnt = mpc_bits_huff_lut(r, Table);
 						idx = 0;
 						if (cnt > 0 && cnt < 18)
@@ -652,5 +653,5 @@
 					}
 				} else if (Res <= 4) {
-					Table = mpc_table_HuffQ_8[0][Res - 1];
+					Table = & mpc_HuffQ_8[Res - 1][0];
 					for ( ; k < 36; k += 2 ) {
 						union {
@@ -658,19 +659,19 @@
 							struct { mpc_int8_t s1:4, s2:4; };
 						} tmp;
-						tmp.sym = mpc_bits_huff_dec(r, Table);
+						tmp.sym = mpc_bits_huff_lut(r, Table);
 						q[k] = tmp.s1;
 						q[k + 1] = tmp.s2;
 					}
 				} else if (Res <= 8) {
-					Tables[0] = mpc_table_HuffQ_8 [0][Res - 1];
-					Tables[1] = mpc_table_HuffQ_8 [1][Res - 1];
+					Tables[0] = & mpc_HuffQ_8 [Res - 1][0];
+					Tables[1] = & mpc_HuffQ_8 [Res - 1][1];
 					idx = 2 * thres[Res];
 					for ( ; k < 36; k++ ) {
-						q[k] = mpc_bits_huff_dec(r, Tables[idx > thres[Res]]);
+						q[k] = mpc_bits_huff_lut(r, Tables[idx > thres[Res]]);
 						idx = (idx >> 1) + absi(q[k]);
 					}
 				} else {
 					for ( ; k < 36; k++ ) {
-						q[k] = (unsigned char) mpc_bits_huff_dec(r, mpc_table_HuffQ9up_8);
+						q[k] = (unsigned char) mpc_bits_huff_lut(r, & mpc_HuffQ9up_8);
 						if (Res != 9)
 							q[k] = (q[k] << (Res - 9)) | mpc_bits_read(r, Res - 9);
