Index: libmpc/branches/r2d/libmpcdec/mpc_decoder.c
===================================================================
--- libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 203)
+++ libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 219)
@@ -34,5 +34,5 @@
 /// \file mpc_decoder.c
 /// Core decoding routines and logic.
-#include <string.h>
+
 #include <mpc/mpcdec.h>
 #include <mpc/minimax.h>
@@ -42,9 +42,5 @@
 #include "math.h"
 #include "requant.h"
-
-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);
+#include "mpc_bits_reader.h"
 
 //SV7 tables
@@ -233,18 +229,18 @@
             if ( d->Res_L [Band] ) {
                 if ( d->Res_R [Band] ) {    // M!=0, S!=0
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][0]);
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][0]);
+                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
                         *YR   = templ - tempr;
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][1]);
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][1]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
                         *YR   = templ - tempr;
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][2]);
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][2]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
@@ -252,13 +248,13 @@
                     }
                 } else {    // M!=0, S==0
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][0]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][1]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][2]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
@@ -268,13 +264,13 @@
                 if (d->Res_R[Band])    // M==0, S!=0
                 {
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][0]);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
                     }
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][1]);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
                     }
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][2]);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
@@ -291,18 +287,18 @@
             if ( d->Res_L [Band] ) {
                 if ( d->Res_R [Band] ) {    // L!=0, R!=0
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][0]);
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][0]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
                     for (n = 0; n < 12; n++, YL += 32, YR += 32 ) {
                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][1]);
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][1]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
                     for (; n < 24; n++, YL += 32, YR += 32 ) {
                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][2]);
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][2]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
                     for (; n < 36; n++, YL += 32, YR += 32 ) {
                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
@@ -310,15 +306,15 @@
                     }
                 } else {     // L!=0, R==0
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][0]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][0] & 0xFF);
                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
                         *YR = 0;
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][1]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][1] & 0xFF);
                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
                         *YR = 0;
                     }
-                    facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (mpc_uint8_t) d->SCF_Index_L[Band][2]);
+					facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , d->SCF_Index_L[Band][2] & 0xFF);
                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
@@ -329,15 +325,15 @@
             else {
                 if ( d->Res_R [Band] ) {    // L==0, R!=0
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][0]);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][0] & 0xFF);
                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
                         *YL = 0;
                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
                     }
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][1]);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][1] & 0xFF);
                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
                         *YL = 0;
                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
                     }
-                    facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (mpc_uint8_t) d->SCF_Index_R[Band][2]);
+					facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , d->SCF_Index_R[Band][2] & 0xFF);
                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
                         *YL = 0;
@@ -619,31 +615,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 0:
-					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:
+			if (Res != 0) {
+				if (Res == 2) {
 					Tables[0] = mpc_table_HuffQ_8 [0][1];
 					Tables[1] = mpc_table_HuffQ_8 [1][1];
@@ -656,7 +627,26 @@
 						idx = (idx >> 1) + HuffQ2_var[tmp];
 					}
-					break;
-				case 3:
-				case 4:
+				} else if (Res == 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;
+						}
+					}
+				} else if (Res == -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;
+					}
+				} else if (Res <= 4) {
 					Table = mpc_table_HuffQ_8[0][Res - 1];
 					for ( ; k < 36; k += 2 ) {
@@ -669,9 +659,5 @@
 						q[k + 1] = tmp.s2;
 					}
-					break;
-				case 5:
-				case 6:
-				case 7:
-				case 8:
+				} else if (Res <= 8) {
 					Tables[0] = mpc_table_HuffQ_8 [0][Res - 1];
 					Tables[1] = mpc_table_HuffQ_8 [1][Res - 1];
@@ -681,6 +667,5 @@
 						idx = (idx >> 1) + absi(q[k]);
 					}
-					break;
-				default :
+				} else {
 					for ( ; k < 36; k++ ) {
 						q[k] = mpc_bits_huff_dec(r, mpc_table_HuffQ9up_8);
@@ -689,5 +674,5 @@
 						q[k] -= Dc[Res];
 					}
-					break;
+				}
 			}
 
