Index: libmpc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 159)
+++ libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 161)
@@ -25,4 +25,73 @@
 unsigned long crc32(unsigned char *buf, int len);
 
+#define MAX_ENUM 32
+
+static const mpc_uint32_t Cnk[MAX_ENUM / 2][MAX_ENUM] =
+{
+	{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
+	{0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465},
+	{0, 0, 0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969, 1140, 1330, 1540, 1771, 2024, 2300, 2600, 2925, 3276, 3654, 4060, 4495},
+	{0, 0, 0, 0, 1, 5, 15, 35, 70, 126, 210, 330, 495, 715, 1001, 1365, 1820, 2380, 3060, 3876, 4845, 5985, 7315, 8855, 10626, 12650, 14950, 17550, 20475, 23751, 27405, 31465},
+	{0, 0, 0, 0, 0, 1, 6, 21, 56, 126, 252, 462, 792, 1287, 2002, 3003, 4368, 6188, 8568, 11628, 15504, 20349, 26334, 33649, 42504, 53130, 65780, 80730, 98280, 118755, 142506, 169911},
+	{0, 0, 0, 0, 0, 0, 1, 7, 28, 84, 210, 462, 924, 1716, 3003, 5005, 8008, 12376, 18564, 27132, 38760, 54264, 74613, 100947, 134596, 177100, 230230, 296010, 376740, 475020, 593775, 736281},
+	{0, 0, 0, 0, 0, 0, 0, 1, 8, 36, 120, 330, 792, 1716, 3432, 6435, 11440, 19448, 31824, 50388, 77520, 116280, 170544, 245157, 346104, 480700, 657800, 888030, 1184040, 1560780, 2035800, 2629575},
+	{0, 0, 0, 0, 0, 0, 0, 0, 1, 9, 45, 165, 495, 1287, 3003, 6435, 12870, 24310, 43758, 75582, 125970, 203490, 319770, 490314, 735471, 1081575, 1562275, 2220075, 3108105, 4292145, 5852925, 7888725},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 55, 220, 715, 2002, 5005, 11440, 24310, 48620, 92378, 167960, 293930, 497420, 817190, 1307504, 2042975, 3124550, 4686825, 6906900, 10015005, 14307150, 20160075},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 11, 66, 286, 1001, 3003, 8008, 19448, 43758, 92378, 184756, 352716, 646646, 1144066, 1961256, 3268760, 5311735, 8436285, 13123110, 20030010, 30045015, 44352165},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 12, 78, 364, 1365, 4368, 12376, 31824, 75582, 167960, 352716, 705432, 1352078, 2496144, 4457400, 7726160, 13037895, 21474180, 34597290, 54627300, 84672315},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 91, 455, 1820, 6188, 18564, 50388, 125970, 293930, 646646, 1352078, 2704156, 5200300, 9657700, 17383860, 30421755, 51895935, 86493225, 141120525},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 14, 105, 560, 2380, 8568, 27132, 77520, 203490, 497420, 1144066, 2496144, 5200300, 10400600, 20058300, 37442160, 67863915, 119759850, 206253075},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 15, 120, 680, 3060, 11628, 38760, 116280, 319770, 817190, 1961256, 4457400, 9657700, 20058300, 40116600, 77558760, 145422675, 265182525},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 136, 816, 3876, 15504, 54264, 170544, 490314, 1307504, 3268760, 7726160, 17383860, 37442160, 77558760, 155117520, 300540195},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 153, 969, 4845, 20349, 74613, 245157, 735471, 2042975, 5311735, 13037895, 30421755, 67863915, 145422675, 300540195}
+};
+
+static const mpc_uint8_t Cnk_len[MAX_ENUM / 2][MAX_ENUM] =
+{
+	{0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
+	{0, 0, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9},
+	{0, 0, 0, 2, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13},
+	{0, 0, 0, 0, 3, 4, 6, 7, 7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16},
+	{0, 0, 0, 0, 0, 3, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18},
+	{0, 0, 0, 0, 0, 0, 3, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20},
+	{0, 0, 0, 0, 0, 0, 0, 3, 6, 7, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22},
+	{0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 23, 24},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23, 24, 24, 25, 25},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 10, 12, 13, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 24, 25, 25, 26, 26},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 23, 24, 25, 26, 26, 27, 27},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 28},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 27, 28, 29},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 16, 17, 19, 20, 21, 23, 24, 25, 26, 27, 28, 28, 29},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 10, 12, 14, 16, 18, 19, 21, 22, 23, 25, 26, 27, 28, 29, 30},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 10, 13, 15, 17, 18, 20, 21, 23, 24, 25, 27, 28, 29, 30}
+
+};
+
+static const mpc_uint32_t Cnk_lost[MAX_ENUM / 2][MAX_ENUM] =
+{
+	{0, 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
+	{0, 0, 1, 2, 6, 1, 11, 4, 28, 19, 9, 62, 50, 37, 23, 8, 120, 103, 85, 66, 46, 25, 3, 236, 212, 187, 161, 134, 106, 77, 47, 16},
+	{0, 0, 0, 0, 6, 12, 29, 8, 44, 8, 91, 36, 226, 148, 57, 464, 344, 208, 55, 908, 718, 508, 277, 24, 1796, 1496, 1171, 820, 442, 36, 3697, 3232},
+	{0, 0, 0, 0, 3, 1, 29, 58, 2, 46, 182, 17, 309, 23, 683, 228, 1716, 1036, 220, 3347, 2207, 877, 7529, 5758, 3734, 1434, 15218, 12293, 9017, 5363, 1303, 29576},
+	{0, 0, 0, 0, 0, 2, 11, 8, 2, 4, 50, 232, 761, 46, 1093, 3824, 2004, 7816, 4756, 880, 12419, 6434, 31887, 23032, 12406, 65292, 50342, 32792, 12317, 119638, 92233, 60768},
+	{0, 0, 0, 0, 0, 0, 1, 4, 44, 46, 50, 100, 332, 1093, 3187, 184, 4008, 14204, 5636, 26776, 11272, 56459, 30125, 127548, 85044, 31914, 228278, 147548, 49268, 454801, 312295, 142384},
+	{0, 0, 0, 0, 0, 0, 0, 0, 28, 8, 182, 232, 332, 664, 1757, 4944, 13320, 944, 15148, 53552, 14792, 91600, 16987, 178184, 43588, 390776, 160546, 913112, 536372, 61352, 1564729, 828448},
+	{0, 0, 0, 0, 0, 0, 0, 0, 7, 19, 91, 17, 761, 1093, 1757, 3514, 8458, 21778, 55490, 5102, 58654, 204518, 33974, 313105, 1015577, 534877, 1974229, 1086199, 4096463, 2535683, 499883, 6258916},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9, 36, 309, 46, 3187, 4944, 8458, 16916, 38694, 94184, 230358, 26868, 231386, 789648, 54177, 1069754, 3701783, 1481708, 6762211, 2470066, 13394357, 5505632},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 62, 226, 23, 1093, 184, 13320, 21778, 38694, 77388, 171572, 401930, 953086, 135896, 925544, 3076873, 8340931, 3654106, 13524422, 3509417, 22756699, 2596624},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 50, 148, 683, 3824, 4008, 944, 55490, 94184, 171572, 343144, 745074, 1698160, 3931208, 662448, 3739321, 12080252, 32511574, 12481564, 49545413, 5193248},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 37, 57, 228, 2004, 14204, 15148, 5102, 230358, 401930, 745074, 1490148, 3188308, 7119516, 16170572, 3132677, 15212929, 47724503, 127314931, 42642616},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 23, 464, 1716, 7816, 5636, 53552, 58654, 26868, 953086, 1698160, 3188308, 6376616, 13496132, 29666704, 66353813, 14457878, 62182381, 189497312},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 344, 1036, 4756, 26776, 14792, 204518, 231386, 135896, 3931208, 7119516, 13496132, 26992264, 56658968, 123012781, 3252931, 65435312},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 208, 220, 880, 11272, 91600, 33974, 789648, 925544, 662448, 16170572, 29666704, 56658968, 113317936, 236330717, 508019104},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 103, 55, 3347, 12419, 56459, 16987, 313105, 54177, 3076873, 3739321, 3132677, 66353813, 123012781, 236330717}
+};
+
+static const mpc_uint8_t log2[32] =
+{ 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6};
+
+static const mpc_uint8_t log2_lost[32] =
+{ 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 31};
+
 void emptyBits(mpc_encoder_t * e)
 {
@@ -81,4 +150,34 @@
 	writeBits(e, 1, l);
 	writeBits(e, nb, k);
+}
+
+void encodeEnum(mpc_encoder_t * e, const mpc_uint32_t bits, const mpc_uint_t N)
+{
+	mpc_uint32_t code = 0;
+	const mpc_uint32_t * C = Cnk[0];
+	unsigned int n = 0, k = 0;
+
+	for( ; n < N; n++){
+		if ((bits >> n) & 1) {
+			code += C[n];
+			C += MAX_ENUM;
+			k++;
+		}
+	}
+
+	if (k == 0) return;
+
+	if (code < Cnk_lost[k-1][n-1])
+		writeBits(e, code, Cnk_len[k-1][n-1] - 1);
+	else
+		writeBits(e, code + Cnk_lost[k-1][n-1], Cnk_len[k-1][n-1]);
+}
+
+void encodeLog(mpc_encoder_t * e, mpc_uint32_t value, mpc_uint32_t max)
+{
+	if (value < log2_lost[max - 1])
+		writeBits(e, value, log2[max - 1] - 1);
+	else
+		writeBits(e, value + log2_lost[max - 1], log2[max - 1]);
 }
 
Index: libmpc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 159)
+++ libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 161)
@@ -22,8 +22,11 @@
 
 #include "libmpcenc.h"
+#include <mpc/minimax.h>
 
 // bitstream.c
 void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits );
 unsigned int encodeSize(mpc_uint64_t, char *, mpc_bool_t);
+void encodeEnum(mpc_encoder_t * e, const mpc_uint32_t bits, const mpc_uint_t N);
+void encodeLog(mpc_encoder_t * e, mpc_uint32_t value, mpc_uint32_t max);
 void writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC);
 
@@ -35,8 +38,12 @@
 
 // huffsv7.c
-extern Huffman_t         HuffHdr  [10];         // contains tables for SV7-header
-extern Huffman_t         HuffSCFI [ 4];         // contains tables for SV7-scalefactor select
-extern Huffman_t         HuffDSCF [16];         // contains tables for SV7-scalefactor coding
-extern const Huffman_t*  HuffQ [2] [8];         // points to tables for SV7-sample coding
+extern const Huffman_t HuffBands [33];
+extern const Huffman_t HuffRes [2][17];
+extern Huffman_t HuffSCFI_1 [4];         // contains tables for SV7-scalefactor select
+extern Huffman_t HuffSCFI_2 [16];         // contains tables for SV7-scalefactor select
+extern Huffman_t HuffDSCF_1 [64];         // contains tables for SV7-scalefactor coding
+extern Huffman_t HuffDSCF_2 [65];         // contains tables for SV7-scalefactor coding
+extern const Huffman_t * HuffQ [2][8];         // points to tables for SV7-sample coding
+extern const Huffman_t HuffQ9up [256];
 
 /*
@@ -61,20 +68,12 @@
 int                   SCF_Index_R [32] [3];     // Scalefactor index for quantized subband values
 
-
 // initialize SV8
 void
 Init_SV8 ( mpc_encoder_t * e )
 {
-    Init_Huffman_Encoder_SV7 ();
 	Init_Skalenfaktoren ();
 	Klemm    ();
 
-	e->pos = 0;
-	e->bitsCount = 0;
-	e->outputBits = 0;
-	e->bitsBuff = 0;
-	e->Overflows = 0;
-	e->seek_pos = 0;
-	e->block_cnt = 0;
+	memset(e, 0, sizeof(*e));
 	e->seek_pwr = 1;
 }
@@ -146,278 +145,220 @@
 }
 
-#define ENCODE_SCF1( new, old, rll )                         \
-        d = new - old + 7;                                   \
-        if ( d <= 14u  && rll < 1) {                        \
-            writeBits ( e, Table[d].Code, Table[d].Length );    \
-        } else {                                               \
-            if ( new < 0 ) new = 0, e->Overflows++;          \
-            writeBits ( e, Table[15].Code, Table[15].Length );  \
-            writeBits ( e, (unsigned int)new, 6 );              \
-            rll = 0;                                         \
-        }
-
-#define ENCODE_SCFn( new, old, rll )                         \
-        d = new - old + 7;                                   \
-        if ( d <= 14u ) {                                    \
-            writeBits ( e, Table[d].Code, Table[d].Length );    \
-        } else {                                               \
-            if ( new < 0 ) new = 0, e->Overflows++;          \
-            writeBits ( e, Table[15].Code, Table[15].Length );  \
-            writeBits ( e, (unsigned int)new, 6 );              \
-            rll = 0;                                         \
-        }
-
-
 // formatting and writing SV7-bitstream for one frame
 void
-WriteBitstream_SV7 ( mpc_encoder_t* e,
-					 const int               MaxBand,
-                     const SubbandQuantTyp*  Q )
-{
-    int                  n;
-    int                  k;
-    unsigned int         d;
-    unsigned int         idx;
-    unsigned int         book;
-    const Huffman_t*     Table;
-    const Huffman_t*     Table0;
-    const Huffman_t*     Table1;
-    int                  sum;
-    const unsigned int*  q;
-    unsigned char        SCFI_L [32];
-    unsigned char        SCFI_R [32];
-
-    /************************************ Resolution *********************************/
-    writeBits ( e, (unsigned int)Res_L[0], 4 );                            // subband 0
-	writeBits ( e, (unsigned int)Res_R[0], 4 );
-    if ( e->MS_Channelmode > 0  &&  !(Res_L[0]==0  &&  Res_R[0]==0) )
-		writeBits ( e, MS_Flag[0] , 1 );
-
-    Table = HuffHdr;                                                    // subband 1...MaxBand
-    for ( n = 1; n <= MaxBand; n++ ) {
-        d = Res_L[n] - Res_L[n-1] + 5;
-        if ( d <= 8u ) {
-			writeBits ( e, Table[d].Code, Table[d].Length );
-        }
-        else {
-			writeBits ( e, Table[9].Code, Table[9].Length );
-			writeBits ( e, Res_L[n]     , 4               );
-        }
-
-        d = Res_R[n] - Res_R[n-1] + 5;
-        if ( d <= 8u ) {
-			writeBits ( e, Table[d].Code, Table[d].Length );
-        }
-        else {
-			writeBits ( e, Table[9].Code, Table[9].Length );
-			writeBits ( e, Res_R[n]     , 4               );
-        }
-        if ( e->MS_Channelmode > 0  &&  !(Res_L[n]==0 && Res_R[n]==0) )
-			writeBits ( e, MS_Flag[n], 1 );
-    }
-
-    /************************************ SCF encoding type ***********************************/
-    Table = HuffSCFI;
-    for ( n = 0; n <= MaxBand; n++ ) {
-        if ( Res_L[n] ) {
-            SCFI_L[n] = 2 * (SCF_Index_L[n][0] == SCF_Index_L[n][1]) + (SCF_Index_L[n][1] == SCF_Index_L[n][2]);
-			writeBits ( e, Table[SCFI_L[n]].Code, Table[SCFI_L[n]].Length );
-        }
-        if ( Res_R[n] ) {
-            SCFI_R[n] = 2 * (SCF_Index_R[n][0] == SCF_Index_R[n][1]) + (SCF_Index_R[n][1] == SCF_Index_R[n][2]);
-			writeBits ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );
-        }
-    }
-
-    /************************************* SCF **********************************/
-    Table = HuffDSCF;
-	for ( n = 0; n <= MaxBand; n++ ) {
-		if (e->framesInBlock == 0){
+writeBitstream_SV8 ( mpc_encoder_t* e, int MaxBand, const SubbandQuantTyp*  Q )
+{
+	int n;
+	const Huffman_t * Table, * Tables[2];
+
+	for( n = MaxBand; n >= 0; n--)
+		if (Res_L[n] != 0 || Res_R[n] != 0) break;
+
+	n++;
+	if (e->framesInBlock == 0) {
+		encodeLog(e, n, MaxBand);
+		MaxBand = e->MaxBand = n;
+	} else {
+		n = n - e->MaxBand;
+		MaxBand = e->MaxBand = n + e->MaxBand;
+		if (n < 0) n += 33;
+		writeBits(e, HuffBands[n].Code, HuffBands[n].Length);
+	}
+
+	/************************************ Resolution *********************************/
+
+	if (MaxBand) {
+		writeBits(e, HuffRes[0][Res_L[MaxBand - 1]].Code, HuffRes[0][Res_L[MaxBand - 1]].Length);
+		writeBits(e, HuffRes[0][Res_R[MaxBand - 1]].Code, HuffRes[0][Res_R[MaxBand - 1]].Length);
+		for ( n = MaxBand - 2; n >= 0; n--) {
+			int tmp = Res_L[n] - Res_L[n + 1];
+			if (tmp < 0) tmp += 17;
+			writeBits(e, HuffRes[Res_L[n + 1] > 2][tmp].Code, HuffRes[Res_L[n + 1] > 2][tmp].Length);
+
+			tmp = Res_R[n] - Res_R[n + 1];
+			if (tmp < 0) tmp += 17;
+			writeBits(e, HuffRes[Res_R[n + 1] > 2][tmp].Code, HuffRes[Res_R[n + 1] > 2][tmp].Length);
+		}
+
+		if (e->MS_Channelmode > 0) {
+			mpc_uint32_t tmp = 0;
+			int cnt = 0, tot = 0;
+			for( n = 0; n < MaxBand; n++) {
+				if ( Res_L[n] != 0 || Res_R[n] != 0 ) {
+					tmp = (tmp << 1) | MS_Flag[n];
+					cnt += MS_Flag[n];
+					tot++;
+				}
+			}
+			encodeLog(e, cnt, tot);
+			if (cnt * 2 > tot) tmp = ~tmp;
+			encodeEnum(e, tmp, tot);
+		}
+	}
+
+	/************************************ SCF encoding type ***********************************/
+
+	if (e->framesInBlock == 0){
+		for( n = 0; n < 32; n++)
 			DSCF_RLL_L[n] = DSCF_RLL_R[n] = 1; // new block -> force key frame
-		}
-
-        if ( Res_L[n] ) {
-            switch ( SCFI_L[n] ) {
-            default:
-                ENCODE_SCF1 ( SCF_Index_L[n][0], SCF_Last_L [n]   , DSCF_RLL_L[n] );
-                ENCODE_SCFn ( SCF_Index_L[n][1], SCF_Index_L[n][0], DSCF_RLL_L[n] );
-                ENCODE_SCFn ( SCF_Index_L[n][2], SCF_Index_L[n][1], DSCF_RLL_L[n] );
-                SCF_Last_L[n] = SCF_Index_L[n][2];
-                break;
-            case 1:
-                ENCODE_SCF1 ( SCF_Index_L[n][0], SCF_Last_L [n]   , DSCF_RLL_L[n] );
-                ENCODE_SCFn ( SCF_Index_L[n][1], SCF_Index_L[n][0], DSCF_RLL_L[n] );
-                SCF_Last_L[n] = SCF_Index_L[n][1];
-                break;
-            case 2:
-                ENCODE_SCF1 ( SCF_Index_L[n][0], SCF_Last_L[n]    , DSCF_RLL_L[n] );
-                ENCODE_SCFn ( SCF_Index_L[n][2], SCF_Index_L[n][0], DSCF_RLL_L[n] );
-                SCF_Last_L[n] = SCF_Index_L[n][2];
-                break;
-            case 3:
-                ENCODE_SCF1 ( SCF_Index_L[n][0], SCF_Last_L[n]    , DSCF_RLL_L[n] );
-                SCF_Last_L[n] = SCF_Index_L[n][0];
-                break;
-            }
-        }
-
-        if ( Res_R[n] ) {
-            switch ( SCFI_R[n] ) {
-            default:
-                ENCODE_SCF1 ( SCF_Index_R[n][0], SCF_Last_R[n]    , DSCF_RLL_R[n] );
-                ENCODE_SCFn ( SCF_Index_R[n][1], SCF_Index_R[n][0], DSCF_RLL_R[n] );
-                ENCODE_SCFn ( SCF_Index_R[n][2], SCF_Index_R[n][1], DSCF_RLL_R[n] );
-                SCF_Last_R[n] = SCF_Index_R[n][2];
-                break;
-            case 1:
-                ENCODE_SCF1 ( SCF_Index_R[n][0], SCF_Last_R[n]    , DSCF_RLL_R[n] );
-                ENCODE_SCFn ( SCF_Index_R[n][1], SCF_Index_R[n][0], DSCF_RLL_R[n] );
-                SCF_Last_R[n] = SCF_Index_R[n][1];
-                break;
-            case 2:
-                ENCODE_SCF1 ( SCF_Index_R[n][0], SCF_Last_R[n]    , DSCF_RLL_R[n] );
-                ENCODE_SCFn ( SCF_Index_R[n][2], SCF_Index_R[n][0], DSCF_RLL_R[n] );
-                SCF_Last_R[n] = SCF_Index_R[n][2];
-                break;
-            case 3:
-                ENCODE_SCF1 ( SCF_Index_R[n][0], SCF_Last_R[n]    , DSCF_RLL_R[n] );
-                SCF_Last_R[n] = SCF_Index_R[n][0];
-                break;
-            }
-        }
-    }
-
-    /*********************************** Samples *********************************/
-    for ( n = 0; n <= MaxBand; n++ ) {
-
-        sum = 0;
-        q   = Q[n].L;
-
-        switch ( Res_L[n] ) {
-        case -1:
-        case  0:
-            break;
-        case  1:
-            Table0 = HuffQ [0][1];
-            Table1 = HuffQ [1][1];
-            for ( k = 0; k < 36; k += 3 ) {
-                idx  = q[k+0] + 3*q[k+1] + 9*q[k+2];
-                sum += Table0 [idx].Length;
-                sum -= Table1 [idx].Length;
-            }
-            book = sum >= 0;
-			writeBits ( e, book, 1 );
-            Table = HuffQ [book][1];
-            for ( k = 0; k < 36; k += 3 ) {
-                idx = q[k+0] + 3*q[k+1] + 9*q[k+2];
-				writeBits ( e, Table[idx].Code, Table[idx].Length );
-            }
-            break;
-        case  2:
-            Table0 = HuffQ [0][2];
-            Table1 = HuffQ [1][2];
-            for ( k = 0; k < 36; k += 2 ) {
-                idx  = q[k+0] + 5*q[k+1];
-                sum += Table0 [idx].Length;
-                sum -= Table1 [idx].Length;
-            }
-            book = sum >= 0;
-			writeBits ( e, book, 1 );
-            Table = HuffQ [book][2];
-            for ( k = 0; k < 36; k += 2 ) {
-                idx = q[k+0] + 5*q[k+1];
-				writeBits ( e, Table[idx].Code, Table[idx].Length );
-            }
-            break;
-        case  3:
-        case  4:
-        case  5:
-        case  6:
-        case  7:
-            Table0 = HuffQ [0][Res_L[n]];
-            Table1 = HuffQ [1][Res_L[n]];
-            for ( k = 0; k < 36; k++ ) {
-                sum += Table0 [q[k]].Length;
-                sum -= Table1 [q[k]].Length;
-            }
-            book = sum >= 0;
-			writeBits ( e, book, 1 );
-            Table = HuffQ [book][Res_L[n]];
-            for ( k = 0; k < 36; k++ ) {
-                idx = q[k];
-				writeBits ( e, Table[idx].Code, Table[idx].Length );
-            }
-            break;
-        default:
-            for ( k = 0; k < 36; k++ )
-				writeBits ( e, q[k], Res_L[n]-1 );
-            break;
-        }
-
-        sum = 0;
-        q   = Q[n].R;
-
-        switch ( Res_R[n] ) {
-        case -1:
-        case  0:
-            break;
-        case  1:
-            Table0 = HuffQ [0][1];
-            Table1 = HuffQ [1][1];
-            for ( k = 0; k < 36; k += 3 ) {
-                idx  = q[k+0] + 3*q[k+1] + 9*q[k+2];
-                sum += Table0 [idx].Length;
-                sum -= Table1 [idx].Length;
-            }
-            book = sum >= 0;
-			writeBits ( e, book, 1 );
-            Table = HuffQ [book][1];
-            for ( k = 0; k < 36; k += 3 ) {
-                idx = q[k+0] + 3*q[k+1] + 9*q[k+2];
-				writeBits ( e, Table[idx].Code, Table[idx].Length );
-            }
-            break;
-        case  2:
-            Table0 = HuffQ [0][2];
-            Table1 = HuffQ [1][2];
-            for ( k = 0; k < 36; k += 2 ) {
-                idx  = q[k+0] + 5*q[k+1];
-                sum += Table0 [idx].Length;
-                sum -= Table1 [idx].Length;
-            }
-            book = sum >= 0;
-			writeBits ( e, book, 1 );
-            Table = HuffQ [book][2];
-            for ( k = 0; k < 36; k += 2 ) {
-                idx = q[k+0] + 5*q[k+1];
-				writeBits ( e, Table[idx].Code, Table[idx].Length );
-            }
-            break;
-        case  3:
-        case  4:
-        case  5:
-        case  6:
-        case  7:
-            Table0 = HuffQ [0][Res_R[n]];
-            Table1 = HuffQ [1][Res_R[n]];
-            for ( k = 0; k < 36; k++ ) {
-                sum += Table0 [q[k]].Length;
-                sum -= Table1 [q[k]].Length;
-            }
-            book = sum >= 0;
-			writeBits ( e, book, 1 );
-            Table = HuffQ [book][Res_R[n]];
-            for ( k = 0; k < 36; k++ ) {
-                idx = q[k];
-				writeBits ( e, Table[idx].Code, Table[idx].Length );
-            }
-            break;
-        default:
-            for ( k = 0; k < 36; k++ )
-				writeBits ( e, q[k], Res_R[n] - 1 );
-            break;
-        }
-
-    }
+	}
+
+	Tables[0] = HuffSCFI_1;
+	Tables[1] = HuffSCFI_2;
+	for ( n = 0; n < MaxBand; n++ ) {
+		int tmp = 0, cnt = -1;
+		if (Res_L[n]) {
+			tmp = (SCF_Index_L[n][1] == SCF_Index_L[n][0]) * 2 + (SCF_Index_L[n][2] == SCF_Index_L[n][1]);
+			cnt++;
+		}
+		if (Res_R[n]) {
+			tmp = (tmp << 2) | ((SCF_Index_R[n][1] == SCF_Index_R[n][0]) * 2 + (SCF_Index_R[n][2] == SCF_Index_R[n][1]));
+			cnt++;
+		}
+		if (cnt >= 0)
+			writeBits(e, Tables[cnt][tmp].Code, Tables[cnt][tmp].Length);
+	}
+
+	/************************************* SCF **********************************/
+
+	for ( n = 0; n < MaxBand; n++ ) {
+		if ( Res_L[n] ) {
+			int m;
+			if (DSCF_RLL_L[n] == 1) {
+				writeBits(e, SCF_Index_L[n][0] + 6, 7);
+				DSCF_RLL_L[n] = 0;
+			} else {
+				unsigned int tmp = (SCF_Index_L[n][0] - SCF_Last_L[n] + 31) & 127;
+				if (tmp < 64)
+					writeBits(e, HuffDSCF_2[tmp].Code, HuffDSCF_2[tmp].Length);
+				else {
+					writeBits(e, HuffDSCF_2[64].Code, HuffDSCF_2[64].Length);
+					writeBits(e, tmp - 64, 6);
+				}
+			}
+			for( m = 0; m < 2; m++){
+				if (SCF_Index_L[n][m+1] != SCF_Index_L[n][m]) {
+					unsigned int tmp = (SCF_Index_L[n][m+1] - SCF_Index_L[n][m] + 31) & 127;
+					if (tmp < 64)
+						writeBits(e, HuffDSCF_1[tmp].Code, HuffDSCF_1[tmp].Length);
+					else {
+						writeBits(e, HuffDSCF_1[31].Code, HuffDSCF_1[31].Length);
+						writeBits(e, tmp - 64, 6);
+					}
+				}
+			}
+			SCF_Last_L[n] = SCF_Index_L[n][2];
+		}
+		if ( Res_R[n] ) {
+			int m;
+			if (DSCF_RLL_R[n] == 1) {
+				writeBits(e, SCF_Index_R[n][0] + 6, 7);
+				DSCF_RLL_R[n] = 0;
+			} else {
+				unsigned int tmp = (SCF_Index_R[n][0] - SCF_Last_R[n] + 31) & 127;
+				if (tmp < 64)
+					writeBits(e, HuffDSCF_2[tmp].Code, HuffDSCF_2[tmp].Length);
+				else {
+					writeBits(e, HuffDSCF_2[64].Code, HuffDSCF_2[64].Length);
+					writeBits(e, tmp - 64, 6);
+				}
+			}
+			for( m = 0; m < 2; m++){
+				if (SCF_Index_R[n][m+1] != SCF_Index_R[n][m]) {
+					unsigned int tmp = (SCF_Index_R[n][m+1] - SCF_Index_R[n][m] + 31) & 127;
+					if (tmp < 64)
+						writeBits(e, HuffDSCF_1[tmp].Code, HuffDSCF_1[tmp].Length);
+					else {
+						writeBits(e, HuffDSCF_1[31].Code, HuffDSCF_1[31].Length);
+						writeBits(e, tmp - 64, 6);
+					}
+				}
+			}
+			SCF_Last_R[n] = SCF_Index_R[n][2];
+		}
+	}
+
+	/*********************************** Samples *********************************/
+	for ( n = 0; n < MaxBand; n++ ) {
+		int Res = Res_L[n];
+		const unsigned int * q = Q[n].L;
+		static const unsigned int thres[] = {0, 0, 3, 7, 9, 1, 3, 4, 8};
+		static const int 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 {
+			int k = 0, idx = 1, cnt = 0, sng;
+			switch ( Res ) {
+				case -1:
+				case  0:
+					break;
+				case  1:
+					Table = HuffQ [0][0];
+					for( ; k < 36; ){
+						int kmax = k + 18;
+						cnt = 0, sng = 0;
+						for ( ; k < kmax; k++) {
+							idx <<= 1;
+							if (q[k] != 1) {
+								cnt++;
+								idx |= 1;
+								sng = (sng << 1) | (q[k] >> 1);
+							}
+						}
+						writeBits(e, Table[cnt].Code, Table[cnt].Length);
+						if (cnt > 0) {
+							if (cnt > 9) idx = ~idx;
+							encodeEnum(e, idx, 18);
+							writeBits(e, sng, cnt);
+						}
+					}
+					break;
+				case  2:
+					Tables[0] = HuffQ [0][1];
+					Tables[1] = HuffQ [1][1];
+					idx = 2 * thres[Res];
+					for ( ; k < 36; k += 3) {
+						int tmp = q[k] + 5*q[k+1] + 25*q[k+2];
+						writeBits ( e, Tables[idx > thres[Res]][tmp].Code,
+									Tables[idx > thres[Res]][tmp].Length );
+						idx = (idx >> 1) + HuffQ2_var[tmp];
+					}
+					break;
+				case  3:
+				case  4:
+					Table = HuffQ [0][Res - 1];
+					for ( ; k < 36; k += 2 ) {
+						int tmp = q[k] + thres[Res]*q[k+1];
+						writeBits ( e, Table[tmp].Code, Table[tmp].Length );
+					}
+					break;
+				case  5:
+				case  6:
+				case  7:
+				case  8:
+					Tables[0] = HuffQ [0][Res - 1];
+					Tables[1] = HuffQ [1][Res - 1];
+					idx = 2 * thres[Res];
+					for ( ; k < 36; k++ ) {
+						int tmp = q[k] - (1 << (Res - 2)) + 1;
+						writeBits ( e, Tables[idx > thres[Res]][q[k]].Code,
+									Tables[idx > thres[Res]][q[k]].Length );
+						if (tmp < 0) tmp = -tmp;
+						idx = (idx >> 1) + tmp;
+					}
+					break;
+				default:
+					for ( ; k < 36; k++ ) {
+						writeBits ( e, HuffQ9up[q[k] >> (Res - 9)].Code,
+									HuffQ9up[q[k] >> (Res - 9)].Length );
+						if (Res != 9)
+							writeBits ( e, q[k] & ((1 << (Res - 9)) - 1), Res - 9);
+					}
+					break;
+			}
+
+			Res = Res_R[n];
+		} while (q == Q[n].L && (q = Q[n].R));
+	}
 
 	e->framesInBlock++;
@@ -432,9 +373,73 @@
 }
 
-#undef ENCODE_SCF1
-#undef ENCODE_SCFn
-
-
 #if 0
+
+typedef struct {
+	int Symbol;
+	unsigned int Count;
+	unsigned int Code;
+	unsigned int Bits;
+} huff_sym_t;
+
+void _Huffman_MakeTree( huff_sym_t *sym, unsigned int num_symbols);
+void _Huffman_PrintCodes(huff_sym_t * sym, unsigned int num_symbols, int print_type, int offset);
+
+void print_histo(void)
+{
+	int i, j;
+	huff_sym_t sym[HISTO_NB][HISTO_LEN];
+	unsigned int dist[HISTO_NB];
+	unsigned int size[HISTO_NB];
+	unsigned int cnt[HISTO_NB];
+	unsigned int total_cnt, total_size, full_count = 0, full_size = 0;
+	double optim_size, full_optim = 0;
+
+	return;
+
+	memset(dist, 1, sizeof dist);
+	memset(sym, 0, sizeof(huff_sym_t) * HISTO_LEN * HISTO_NB);
+
+	for(j = 0 ; j < HISTO_NB ; j++) {
+		for(i = 0 ; i < HISTO_LEN; i++) {
+			sym[j][i].Symbol = i;
+			sym[j][i].Count = histo[j][i];
+			if (sym[j][i].Count == 0)
+				sym[j][i].Count = 1;
+		}
+		_Huffman_MakeTree(sym[j], HISTO_LEN);
+		_Huffman_PrintCodes(sym[j], HISTO_LEN, 3, 0);
+		_Huffman_PrintCodes(sym[j], HISTO_LEN, 0, 0);
+		_Huffman_PrintCodes(sym[j], HISTO_LEN, 1, 0);
+		total_cnt = 0;
+		total_size = 0;
+		optim_size = 0;
+		for( i = 0; i < HISTO_LEN; i++) {
+			total_cnt += sym[j][i].Count;
+			total_size += sym[j][i].Count * sym[j][i].Bits;
+			if (sym[j][i].Count != 0)
+				optim_size += sym[j][i].Count * __builtin_log2(sym[j][i].Count);
+		}
+		full_count += total_cnt;
+		full_size += total_size;
+		optim_size = total_cnt * __builtin_log2(total_cnt) - optim_size;
+		full_optim += optim_size;
+		size[j] = total_size;
+		cnt[j] = total_cnt;
+		printf("%u count : %u huff : %f bps ", j, total_cnt, (float)total_size / total_cnt);
+		printf("opt : %f bps ", (float)optim_size / total_cnt);
+		printf("loss : %f bps (%f %%)\n", (float)(total_size - optim_size) / total_cnt, (float)(total_size - optim_size) * 100 / optim_size);
+		for( i = 0; i < HISTO_LEN; i++){
+			printf("%u ", sym[j][i].Bits);
+		}
+
+		printf("\n\n");
+	}
+	printf("cnt : %u size %f optim %f\n", full_count, (float)full_size / full_count, (float)full_optim / full_count);
+	printf("loss : %f bps (%f %%)\n", (float)(full_size - full_optim) / full_count, (float)(full_size - full_optim) * 100 / full_optim);
+
+
+	printf("\n");
+}
+
 void
 Dump ( const unsigned int* q, const int Res )
Index: libmpc/branches/r2d/libmpcenc/huffsv7.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/huffsv7.c	(revision 159)
+++ libmpc/branches/r2d/libmpcenc/huffsv7.c	(revision 161)
@@ -20,424 +20,92 @@
 #include "libmpcenc.h"
 
-Huffman_t   HuffHdr    [10];            // 9 bit
-Huffman_t   HuffSCFI   [ 4];            // 3 bit
-Huffman_t   HuffDSCF   [16];            // 6 bit
-Huffman_t   HuffQ1 [2] [ 3*3*3];        // 6+ 9 bit
-Huffman_t   HuffQ2 [2] [ 5*5];          // 7+10 bit
-Huffman_t   HuffQ3 [2] [ 7];            // 4+ 5 bit
-Huffman_t   HuffQ4 [2] [ 9];            // 4+ 5 bit
-Huffman_t   HuffQ5 [2] [15];            // 6+ 8 bit
-Huffman_t   HuffQ6 [2] [31];            // 7+13 bit
-Huffman_t   HuffQ7 [2] [63];            // 8+14 bit
-                                        // 4608 Bytes
-mpc_uint8_t     LUT1_0  [1<< 6];
-mpc_uint8_t     LUT1_1  [1<< 9];            //  576 Bytes
-mpc_uint8_t     LUT2_0  [1<< 7];
-mpc_uint8_t     LUT2_1  [1<<10];            // 1152 Bytes
-mpc_uint8_t     LUT3_0  [1<< 4];
-mpc_uint8_t     LUT3_1  [1<< 5];            //   48 Bytes
-mpc_uint8_t     LUT4_0  [1<< 4];
-mpc_uint8_t     LUT4_1  [1<< 5];            //   48 Bytes
-mpc_uint8_t     LUT5_0  [1<< 6];
-mpc_uint8_t     LUT5_1  [1<< 8];            //  320 Bytes
-mpc_uint8_t     LUT6_0  [1<< 7];
-mpc_uint8_t     LUT6_1  [1<< 7];            //  256 Bytes
-mpc_uint8_t     LUT7_0  [1<< 8];
-mpc_uint8_t     LUT7_1  [1<< 8];            //  512 Bytes
-mpc_uint8_t     LUTDSCF [1<< 6];            //   64 Bytes = 2976 Bytes
+const Huffman_t HuffBands [33] = {
+	{1, 1}, {1, 3}, {2, 5}, {2, 6}, {3, 7}, {3, 8}, {4, 8}, {4, 9}, {5, 10}, {6, 11}, {1, 12}, {2, 12}, {3, 12}, {0, 13}, {4, 12}, {5, 12}, {6, 12}, {7, 12}, {8, 12}, {1, 13}, {9, 12}, {10, 12}, {11, 12}, {7, 11}, {8, 11}, {9, 11}, {6, 10}, {7, 10}, {5, 9}, {5, 8}, {3, 6}, {3, 5}, {1, 2}
+};
+
+const Huffman_t HuffRes [2] [17] = {
+	{
+		{1, 1}, {1, 2}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 9}, {1, 10}, {1, 11}, {1, 12}, {1, 13}, {1, 14}, {1, 15}, {0, 16}, {1, 16}, {1, 8}, {1, 3}
+	}, {
+		{1, 2}, {2, 2}, {1, 3}, {1, 5}, {1, 7}, {1, 8}, {1, 10}, {1, 12}, {0, 14}, {1, 14}, {2, 14}, {3, 14}, {1, 11}, {1, 9}, {1, 6}, {1, 4}, {3, 2}
+	}
+};
+
+const Huffman_t   HuffSCFI_1 [4] = {
+	{0, 3}, {1, 3}, {1, 1}, {1, 2}
+};
+
+const Huffman_t   HuffSCFI_2 [16] = {
+	{1, 6}, {0, 7}, {2, 6}, {3, 6}, {1, 7}, {3, 5}, {4, 5}, {5, 5}, {4, 6}, {6, 5}, {2, 2}, {2, 3}, {5, 6}, {7, 5}, {3, 3}, {3, 2}
+};
+
+const Huffman_t   HuffDSCF_1 [64] = {
+	{3, 12}, {4, 12}, {5, 12}, {4, 11}, {5, 11}, {6, 11}, {5, 10}, {6, 10}, {7, 10}, {8, 10}, {9, 10}, {7, 9}, {8, 9}, {9, 9}, {10, 9}, {7, 8}, {8, 8}, {9, 8}, {10, 8}, {7, 7}, {8, 7}, {9, 7}, {10, 7}, {6, 6}, {7, 6}, {5, 5}, {4, 4}, {5, 4}, {6, 5}, {6, 4}, {7, 4}, {10, 10}, {8, 4}, {5, 3}, {6, 3}, {7, 3}, {9, 4}, {7, 5}, {8, 6}, {9, 6}, {11, 7}, {11, 8}, {12, 8}, {13, 8}, {11, 9}, {12, 9}, {13, 9}, {11, 10}, {12, 10}, {13, 10}, {7, 11}, {8, 11}, {9, 11}, {6, 12}, {7, 12}, {3, 13}, {4, 13}, {5, 13}, {0, 14}, {1, 14}, {2, 14}, {3, 14}, {4, 14}, {5, 14}
+};
+
+const Huffman_t   HuffDSCF_2 [65] = {
+	{0, 15}, {3, 14}, {4, 14}, {4, 13}, {5, 13}, {6, 13}, {5, 12}, {6, 12}, {7, 12}, {8, 12}, {7, 11}, {8, 11}, {9, 11}, {10, 11}, {7, 10}, {8, 10}, {9, 10}, {10, 10}, {7, 9}, {8, 9}, {9, 9}, {6, 8}, {7, 8}, {5, 7}, {6, 7}, {4, 6}, {3, 5}, {3, 4}, {4, 4}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3}, {5, 4}, {4, 5}, {5, 5}, {5, 6}, {7, 7}, {8, 8}, {9, 8}, {10, 9}, {11, 9}, {11, 10}, {12, 10}, {13, 10}, {11, 11}, {12, 11}, {13, 11}, {9, 12}, {10, 12}, {11, 12}, {12, 12}, {7, 13}, {8, 13}, {9, 13}, {5, 14}, {6, 14}, {7, 14}, {1, 15}, {2, 15}, {3, 15}, {4, 15}, {5, 15}, {13, 12}
+};
+
+static const Huffman_t   HuffQ1 [19] = {
+	{1, 6}, {1, 4}, {2, 4}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3}, {3, 4}, {4, 4}, {5, 4}, {1, 5}, {1, 7}, {1, 8}, {1, 9}, {1, 10}, {1, 11}, {0, 12}, {1, 12}
+};
+
+static const Huffman_t   HuffQ2 [2] [5*5*5] = {
+	{
+		{2, 12}, {3, 11}, {15, 10}, {4, 11}, {0, 13}, {5, 11}, {12, 9}, {18, 8}, {13, 9}, {6, 11}, {7, 11}, {19, 8}, {21, 7}, {20, 8}, {8, 11}, {9, 11}, {14, 9}, {21, 8}, {15, 9}, {10, 11}, {3, 12}, {11, 11}, {16, 10}, {12, 11}, {1, 13}, {13, 11}, {16, 9}, {22, 8}, {17, 9}, {14, 11}, {18, 9}, {15, 6}, {16, 6}, {22, 7}, {19, 9}, {23, 8}, {17, 6}, {8, 4}, {18, 6}, {24, 8}, {20, 9}, {19, 6}, {20, 6}, {23, 7}, {21, 9}, {15, 11}, {22, 9}, {25, 8}, {23, 9}, {16, 11}, {17, 10}, {26, 8}, {24, 7}, {27, 8}, {18, 10}, {28, 8}, {21, 6}, {9, 4}, {22, 6}, {29, 8}, {25, 7}, {10, 4}, {7, 3}, {11, 4}, {26, 7}, {30, 8}, {23, 6}, {12, 4}, {24, 6}, {31, 8}, {19, 10}, {32, 8}, {27, 7}, {33, 8}, {20, 10}, {17, 11}, {24, 9}, {34, 8}, {25, 9}, {18, 11}, {26, 9}, {25, 6}, {26, 6}, {27, 6}, {27, 9}, {35, 8}, {28, 6}, {13, 4}, {29, 6}, {36, 8}, {28, 9}, {28, 7}, {30, 6}, {31, 6}, {29, 9}, {19, 11}, {30, 9}, {37, 8}, {31, 9}, {20, 11}, {2, 13}, {21, 11}, {21, 10}, {22, 11}, {4, 12}, {23, 11}, {32, 9}, {38, 8}, {33, 9}, {24, 11}, {22, 10}, {39, 8}, {29, 7}, {40, 8}, {25, 11}, {26, 11}, {34, 9}, {41, 8}, {35, 9}, {27, 11}, {3, 13}, {28, 11}, {23, 10}, {29, 11}, {5, 12}
+	}, {
+		{2, 11}, {3, 10}, {15, 9}, {4, 10}, {0, 12}, {5, 10}, {12, 8}, {13, 8}, {14, 8}, {6, 10}, {7, 10}, {15, 8}, {30, 7}, {16, 8}, {16, 9}, {8, 10}, {17, 8}, {18, 8}, {19, 8}, {9, 10}, {3, 11}, {10, 10}, {17, 9}, {11, 10}, {1, 12}, {12, 10}, {20, 8}, {21, 8}, {22, 8}, {13, 10}, {23, 8}, {18, 6}, {14, 5}, {19, 6}, {24, 8}, {25, 8}, {20, 6}, {15, 5}, {16, 5}, {26, 8}, {27, 8}, {21, 6}, {17, 5}, {22, 6}, {28, 8}, {14, 10}, {29, 8}, {30, 8}, {31, 8}, {15, 10}, {18, 9}, {32, 8}, {31, 7}, {33, 8}, {19, 9}, {34, 8}, {18, 5}, {19, 5}, {20, 5}, {35, 8}, {32, 7}, {21, 5}, {15, 4}, {22, 5}, {33, 7}, {36, 8}, {23, 5}, {24, 5}, {25, 5}, {37, 8}, {20, 9}, {38, 8}, {34, 7}, {39, 8}, {21, 9}, {16, 10}, {40, 8}, {41, 8}, {42, 8}, {17, 10}, {43, 8}, {23, 6}, {26, 5}, {24, 6}, {44, 8}, {45, 8}, {27, 5}, {28, 5}, {25, 6}, {46, 8}, {47, 8}, {26, 6}, {29, 5}, {27, 6}, {48, 8}, {18, 10}, {49, 8}, {50, 8}, {51, 8}, {19, 10}, {2, 12}, {20, 10}, {21, 10}, {22, 10}, {4, 11}, {23, 10}, {52, 8}, {53, 8}, {54, 8}, {24, 10}, {22, 9}, {55, 8}, {35, 7}, {56, 8}, {25, 10}, {26, 10}, {57, 8}, {58, 8}, {59, 8}, {27, 10}, {3, 12}, {28, 10}, {23, 9}, {29, 10}, {5, 11}
+	}
+};
+
+static const Huffman_t   HuffQ3 [49] = {
+	{0, 9}, {2, 8}, {5, 7}, {6, 7}, {7, 7}, {3, 8}, {1, 9}, {4, 8}, {9, 6}, {10, 6}, {10, 5}, {11, 6}, {8, 7}, {5, 8}, {9, 7}, {12, 6}, {8, 4}, {9, 4}, {11, 5}, {13, 6}, {10, 7}, {11, 7}, {12, 5}, {10, 4}, {7, 3}, {11, 4}, {13, 5}, {12, 7}, {13, 7}, {14, 6}, {14, 5}, {12, 4}, {13, 4}, {15, 6}, {14, 7}, {6, 8}, {16, 6}, {17, 6}, {15, 5}, {18, 6}, {19, 6}, {7, 8}, {2, 9}, {8, 8}, {15, 7}, {16, 7}, {17, 7}, {9, 8}, {3, 9}
+};
+
+static const Huffman_t   HuffQ4 [81] = {
+	{0, 10}, {2, 9}, {5, 8}, {12, 7}, {13, 7}, {6, 8}, {7, 8}, {3, 9}, {1, 10}, {4, 9}, {8, 8}, {14, 7}, {13, 6}, {14, 6}, {15, 6}, {15, 7}, {9, 8}, {5, 9}, {10, 8}, {16, 7}, {16, 6}, {17, 6}, {18, 5}, {18, 6}, {19, 6}, {17, 7}, {11, 8}, {12, 8}, {20, 6}, {21, 6}, {19, 5}, {20, 5}, {21, 5}, {22, 6}, {23, 6}, {13, 8}, {18, 7}, {24, 6}, {22, 5}, {23, 5}, {15, 4}, {24, 5}, {25, 5}, {25, 6}, {19, 7}, {14, 8}, {26, 6}, {27, 6}, {26, 5}, {27, 5}, {28, 5}, {28, 6}, {29, 6}, {15, 8}, {16, 8}, {20, 7}, {30, 6}, {31, 6}, {29, 5}, {32, 6}, {33, 6}, {21, 7}, {17, 8}, {6, 9}, {18, 8}, {22, 7}, {23, 7}, {34, 6}, {35, 6}, {24, 7}, {19, 8}, {7, 9}, {2, 10}, {8, 9}, {20, 8}, {21, 8}, {25, 7}, {22, 8}, {23, 8}, {9, 9}, {3, 10}
+};
+
+static const Huffman_t   HuffQ5 [2] [15] = {
+	{
+		{0, 7}, {1, 7}, {2, 6}, {2, 5}, {2, 4}, {2, 3}, {3, 3}, {3, 2}, {4, 3}, {5, 3}, {3, 4}, {3, 5}, {3, 6}, {2, 7}, {3, 7}
+	}, {
+		{0, 6}, {1, 6}, {2, 5}, {2, 4}, {3, 4}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3}, {4, 4}, {5, 4}, {3, 5}, {2, 6}, {3, 6}
+	}
+};
+
+static const Huffman_t   HuffQ6 [2] [31] = {
+	{
+		{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 8}, {5, 8}, {4, 7}, {3, 6}, {4, 6}, {5, 6}, {5, 5}, {6, 5}, {4, 4}, {5, 4}, {4, 3}, {3, 2}, {5, 3}, {6, 4}, {7, 4}, {7, 5}, {6, 6}, {7, 6}, {8, 6}, {9, 6}, {5, 7}, {6, 8}, {7, 8}, {4, 9}, {5, 9}, {6, 9}, {7, 9}
+	}, {
+		{0, 8}, {1, 8}, {2, 7}, {3, 7}, {4, 7}, {4, 6}, {5, 6}, {4, 5}, {5, 5}, {6, 5}, {5, 4}, {6, 4}, {7, 4}, {8, 4}, {9, 4}, {10, 4}, {11, 4}, {12, 4}, {13, 4}, {14, 4}, {15, 4}, {7, 5}, {8, 5}, {9, 5}, {6, 6}, {7, 6}, {5, 7}, {6, 7}, {7, 7}, {2, 8}, {3, 8}
+	}
+};
+
+static const Huffman_t   HuffQ7 [2] [63] = {
+	{
+		{0, 10}, {1, 10}, {2, 10}, {8, 9}, {9, 9}, {3, 10}, {4, 10}, {5, 10}, {6, 10}, {7, 10}, {10, 9}, {11, 9}, {12, 9}, {13, 9}, {10, 8}, {11, 8}, {12, 8}, {13, 8}, {14, 8}, {10, 7}, {11, 7}, {12, 7}, {13, 7}, {14, 7}, {10, 6}, {11, 6}, {12, 6}, {8, 5}, {9, 5}, {6, 4}, {4, 3}, {3, 2}, {5, 3}, {7, 4}, {10, 5}, {11, 5}, {13, 6}, {14, 6}, {15, 6}, {15, 7}, {16, 7}, {17, 7}, {18, 7}, {15, 8}, {19, 7}, {16, 8}, {17, 8}, {18, 8}, {19, 8}, {14, 9}, {15, 9}, {16, 9}, {17, 9}, {8, 10}, {9, 10}, {10, 10}, {11, 10}, {12, 10}, {18, 9}, {19, 9}, {13, 10}, {14, 10}, {15, 10}
+	}, {
+		{0, 9}, {1, 9}, {2, 8}, {3, 8}, {4, 8}, {5, 8}, {6, 8}, {7, 8}, {8, 8}, {8, 7}, {9, 7}, {10, 7}, {11, 7}, {12, 7}, {9, 6}, {10, 6}, {11, 6}, {12, 6}, {13, 6}, {14, 6}, {15, 6}, {16, 6}, {12, 5}, {13, 5}, {14, 5}, {15, 5}, {16, 5}, {17, 5}, {18, 5}, {19, 5}, {20, 5}, {21, 5}, {22, 5}, {23, 5}, {24, 5}, {25, 5}, {26, 5}, {27, 5}, {28, 5}, {29, 5}, {30, 5}, {31, 5}, {17, 6}, {18, 6}, {19, 6}, {20, 6}, {21, 6}, {22, 6}, {23, 6}, {13, 7}, {14, 7}, {15, 7}, {16, 7}, {17, 7}, {9, 8}, {10, 8}, {11, 8}, {12, 8}, {13, 8}, {14, 8}, {15, 8}, {2, 9}, {3, 9}
+	}
+};
+
+static const Huffman_t   HuffQ8 [2] [127] = {
+	{
+		{3, 11}, {4, 11}, {10, 10}, {11, 10}, {12, 10}, {13, 10}, {14, 10}, {26, 9}, {15, 10}, {27, 9}, {16, 10}, {0, 12}, {1, 12}, {5, 11}, {6, 11}, {7, 11}, {8, 11}, {9, 11}, {10, 11}, {11, 11}, {17, 10}, {12, 11}, {18, 10}, {19, 10}, {20, 10}, {21, 10}, {22, 10}, {23, 10}, {24, 10}, {25, 10}, {28, 9}, {26, 10}, {27, 10}, {28, 10}, {29, 10}, {30, 10}, {29, 9}, {30, 9}, {31, 9}, {32, 9}, {33, 9}, {34, 9}, {35, 9}, {36, 9}, {25, 8}, {37, 9}, {26, 8}, {27, 8}, {28, 8}, {29, 8}, {30, 8}, {31, 8}, {20, 7}, {21, 7}, {22, 7}, {23, 7}, {14, 6}, {15, 6}, {16, 6}, {17, 6}, {11, 5}, {7, 4}, {4, 3}, {3, 2}, {5, 3}, {12, 5}, {13, 5}, {18, 6}, {19, 6}, {20, 6}, {21, 6}, {24, 7}, {25, 7}, {26, 7}, {27, 7}, {32, 8}, {33, 8}, {34, 8}, {35, 8}, {36, 8}, {37, 8}, {38, 8}, {39, 8}, {38, 9}, {39, 9}, {40, 9}, {41, 9}, {42, 9}, {43, 9}, {44, 9}, {45, 9}, {46, 9}, {31, 10}, {32, 10}, {47, 9}, {33, 10}, {34, 10}, {35, 10}, {36, 10}, {37, 10}, {38, 10}, {39, 10}, {40, 10}, {41, 10}, {13, 11}, {14, 11}, {42, 10}, {15, 11}, {16, 11}, {17, 11}, {18, 11}, {2, 12}, {19, 11}, {3, 12}, {4, 12}, {5, 12}, {43, 10}, {44, 10}, {48, 9}, {49, 9}, {45, 10}, {46, 10}, {47, 10}, {48, 10}, {49, 10}, {50, 10}, {51, 10}
+	}, {
+		{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 8}, {5, 8}, {6, 8}, {7, 8}, {8, 8}, {9, 8}, {10, 8}, {11, 8}, {12, 8}, {13, 8}, {14, 8}, {15, 8}, {16, 8}, {17, 8}, {18, 8}, {19, 8}, {20, 8}, {21, 8}, {21, 7}, {22, 7}, {23, 7}, {24, 7}, {25, 7}, {26, 7}, {27, 7}, {28, 7}, {29, 7}, {30, 7}, {31, 7}, {32, 7}, {33, 7}, {34, 7}, {35, 7}, {36, 7}, {37, 7}, {38, 7}, {39, 7}, {40, 7}, {41, 7}, {42, 7}, {43, 7}, {44, 7}, {45, 7}, {46, 7}, {47, 7}, {48, 7}, {38, 6}, {39, 6}, {40, 6}, {41, 6}, {42, 6}, {43, 6}, {44, 6}, {45, 6}, {46, 6}, {47, 6}, {48, 6}, {49, 6}, {50, 6}, {51, 6}, {52, 6}, {53, 6}, {54, 6}, {55, 6}, {56, 6}, {57, 6}, {58, 6}, {59, 6}, {60, 6}, {61, 6}, {62, 6}, {49, 7}, {63, 6}, {50, 7}, {51, 7}, {52, 7}, {53, 7}, {54, 7}, {55, 7}, {56, 7}, {57, 7}, {58, 7}, {59, 7}, {60, 7}, {61, 7}, {62, 7}, {63, 7}, {64, 7}, {65, 7}, {66, 7}, {67, 7}, {68, 7}, {69, 7}, {70, 7}, {71, 7}, {72, 7}, {73, 7}, {74, 7}, {75, 7}, {22, 8}, {23, 8}, {24, 8}, {25, 8}, {26, 8}, {27, 8}, {28, 8}, {29, 8}, {30, 8}, {31, 8}, {32, 8}, {33, 8}, {34, 8}, {35, 8}, {36, 8}, {37, 8}, {38, 8}, {39, 8}, {40, 8}, {41, 8}, {4, 9}, {5, 9}, {6, 9}, {7, 9}
+	}
+};
+
+const Huffman_t   HuffQ9up [256] = {
+	{1, 10}, {2, 10}, {3, 10}, {4, 10}, {5, 10}, {5, 9}, {6, 9}, {7, 9}, {8, 9}, {9, 9}, {10, 9}, {11, 9}, {12, 9}, {13, 9}, {14, 9}, {15, 9}, {16, 9}, {17, 9}, {18, 9}, {38, 8}, {39, 8}, {19, 9}, {20, 9}, {21, 9}, {22, 9}, {23, 9}, {24, 9}, {25, 9}, {26, 9}, {27, 9}, {28, 9}, {29, 9}, {30, 9}, {31, 9}, {32, 9}, {33, 9}, {34, 9}, {35, 9}, {36, 9}, {37, 9}, {40, 8}, {38, 9}, {41, 8}, {42, 8}, {43, 8}, {44, 8}, {45, 8}, {46, 8}, {47, 8}, {48, 8}, {49, 8}, {50, 8}, {51, 8}, {52, 8}, {53, 8}, {54, 8}, {55, 8}, {56, 8}, {57, 8}, {58, 8}, {59, 8}, {60, 8}, {61, 8}, {62, 8}, {63, 8}, {64, 8}, {65, 8}, {66, 8}, {67, 8}, {68, 8}, {69, 8}, {70, 8}, {71, 8}, {72, 8}, {73, 8}, {74, 8}, {75, 8}, {76, 8}, {77, 8}, {78, 8}, {79, 8}, {80, 8}, {81, 8}, {82, 8}, {83, 8}, {84, 8}, {85, 8}, {86, 8}, {87, 8}, {88, 8}, {89, 8}, {90, 8}, {91, 8}, {92, 8}, {93, 8}, {94, 8}, {95, 8}, {96, 8}, {97, 8}, {98, 8}, {99, 8}, {100, 8}, {101, 8}, {102, 8}, {103, 8}, {104, 8}, {105, 8}, {106, 8}, {86, 7}, {87, 7}, {88, 7}, {89, 7}, {90, 7}, {91, 7}, {92, 7}, {93, 7}, {94, 7}, {95, 7}, {96, 7}, {97, 7}, {98, 7}, {99, 7}, {100, 7}, {101, 7}, {102, 7}, {103, 7}, {104, 7}, {62, 6}, {63, 6}, {105, 7}, {106, 7}, {107, 7}, {108, 7}, {109, 7}, {110, 7}, {111, 7}, {112, 7}, {113, 7}, {114, 7}, {115, 7}, {116, 7}, {117, 7}, {118, 7}, {119, 7}, {120, 7}, {121, 7}, {122, 7}, {107, 8}, {123, 7}, {108, 8}, {109, 8}, {110, 8}, {111, 8}, {112, 8}, {113, 8}, {114, 8}, {115, 8}, {116, 8}, {117, 8}, {118, 8}, {119, 8}, {120, 8}, {121, 8}, {122, 8}, {123, 8}, {124, 8}, {125, 8}, {126, 8}, {127, 8}, {128, 8}, {129, 8}, {130, 8}, {131, 8}, {132, 8}, {133, 8}, {134, 8}, {135, 8}, {136, 8}, {137, 8}, {138, 8}, {139, 8}, {140, 8}, {141, 8}, {142, 8}, {143, 8}, {144, 8}, {145, 8}, {146, 8}, {147, 8}, {148, 8}, {149, 8}, {150, 8}, {151, 8}, {152, 8}, {153, 8}, {154, 8}, {155, 8}, {156, 8}, {157, 8}, {158, 8}, {159, 8}, {160, 8}, {161, 8}, {162, 8}, {163, 8}, {164, 8}, {165, 8}, {166, 8}, {167, 8}, {168, 8}, {169, 8}, {170, 8}, {171, 8}, {39, 9}, {40, 9}, {41, 9}, {42, 9}, {43, 9}, {44, 9}, {45, 9}, {46, 9}, {47, 9}, {48, 9}, {49, 9}, {50, 9}, {51, 9}, {52, 9}, {53, 9}, {54, 9}, {55, 9}, {56, 9}, {57, 9}, {58, 9}, {59, 9}, {60, 9}, {61, 9}, {62, 9}, {63, 9}, {64, 9}, {65, 9}, {66, 9}, {67, 9}, {68, 9}, {69, 9}, {70, 9}, {71, 9}, {72, 9}, {73, 9}, {74, 9}, {75, 9}, {6, 10}, {7, 10}, {8, 10}, {9, 10}, {0, 11}, {1, 11}
+};
 
 const Huffman_t* HuffQ [2] [8] = {
-    { NULL, HuffQ1[0], HuffQ2[0], HuffQ3[0], HuffQ4[0], HuffQ5[0], HuffQ6[0], HuffQ7[0] },
-    { NULL, HuffQ1[1], HuffQ2[1], HuffQ3[1], HuffQ4[1], HuffQ5[1], HuffQ6[1], HuffQ7[1] }
+	{ HuffQ1, HuffQ2[0], HuffQ3, HuffQ4, HuffQ5[0], HuffQ6[0], HuffQ7[0], HuffQ8[0] },
+	{ HuffQ1, HuffQ2[1], HuffQ3, HuffQ4, HuffQ5[1], HuffQ6[1], HuffQ7[1], HuffQ8[1] }
 };
 
-#ifdef USE_SV8
-Huffman_t   HuffN3 [2] [ 7*7];          // 8+ 9 bit
-Huffman_t   HuffN8 [2][127];            //13+12 bit
-
-const Huffman_t* HuffN [2] [9] = {
-    { NULL, HuffQ1[0], HuffQ2[0], HuffN3[0], HuffQ4[0], HuffQ5[0], HuffQ6[0], HuffQ7[0], HuffN8[0] },
-    { NULL, HuffQ1[1], HuffQ2[1], HuffN3[1], HuffQ4[1], HuffQ5[1], HuffQ6[1], HuffQ7[1], HuffN8[1] }
-};
-#endif
-
-static const Huffman_t   HuffSCFI_src [4] = {
-    { 2, 3 }, { 1, 1 }, { 3, 3 }, { 0, 2 }
-};
-
-static const Huffman_t   HuffDSCF_src [16] = {
-    { 32, 6 }, {  4, 5 }, { 17, 5 }, { 30, 5 }, { 13, 4 }, {  0, 3 }, {  3, 3 }, {  9, 4 },
-    {  5, 3 }, {  2, 3 }, { 14, 4 }, {  3, 4 }, { 31, 5 }, {  5, 5 }, { 33, 6 }, { 12, 4 }
-};
-
-static const Huffman_t   HuffHdr_src [10] = {
-    {  92, 8 }, {  47, 7 }, {  10, 5 }, {   4, 4 }, {   0, 2 },
-    {   1, 1 }, {   3, 3 }, {  22, 6 }, { 187, 9 }, { 186, 9 }
-};
-
-static const Huffman_t   HuffQ1_src [2] [3*3*3] = { {
-    { 54, 6 }, {  9, 5 }, { 32, 6 }, {  5, 5 }, { 10, 4 }, {  7, 5 }, { 52, 6 }, {  0, 5 }, { 35, 6 },
-    { 10, 5 }, {  6, 4 }, {  4, 5 }, { 11, 4 }, {  7, 3 }, { 12, 4 }, {  3, 5 }, {  7, 4 }, { 11, 5 },
-    { 34, 6 }, {  1, 5 }, { 53, 6 }, {  6, 5 }, {  9, 4 }, {  2, 5 }, { 33, 6 }, {  8, 5 }, { 55, 6 }
-}, {
-    { 103, 8 }, {  62, 7 }, { 225, 9 }, {  55, 7 }, {   3, 4 }, {  52, 7 }, { 101, 8 }, {  60, 7 }, { 227, 9 },
-    {  24, 6 }, {   0, 4 }, {  61, 7 }, {   4, 4 }, {   1, 1 }, {   5, 4 }, {  63, 7 }, {   1, 4 }, {  59, 7 },
-    { 226, 9 }, {  57, 7 }, { 100, 8 }, {  53, 7 }, {   2, 4 }, {  54, 7 }, { 224, 9 }, {  58, 7 }, { 102, 8 }
-} };
-
-static const Huffman_t   HuffQ2_src [2] [5*5] = { {
-    {  89,  7 }, {  47,  6 }, { 15, 5 }, {   0, 5 }, {  91,  7 },
-    {   4,  5 }, {   6,  4 }, { 13, 4 }, {   4, 4 }, {   5,  5 },
-    {  20,  5 }, {  12,  4 }, {  4, 3 }, {  15, 4 }, {  14,  5 },
-    {   3,  5 }, {   3,  4 }, { 14, 4 }, {   5, 4 }, {   1,  5 },
-    {  90,  7 }, {   2,  5 }, { 21, 5 }, {  46, 6 }, {  88,  7 }
-}, {
-    { 921, 10 }, { 113,  7 }, { 51, 6 }, { 231, 8 }, { 922, 10 },
-    { 104,  7 }, {  30,  5 }, {  0, 3 }, {  29, 5 }, { 105,  7 },
-    {  50,  6 }, {   1,  3 }, {  2, 2 }, {   3, 3 }, {  49,  6 },
-    { 107,  7 }, {  27,  5 }, {  2, 3 }, {  31, 5 }, { 112,  7 },
-    { 920, 10 }, { 106,  7 }, { 48, 6 }, { 114, 7 }, { 923, 10 }
-} };
-
-#ifdef USE_SV8
-static const Huffman_t   HuffN3_src [2] [7*7] = { {
-    {  78, 7 }, {  20, 6 }, {  36, 6 }, {  51, 6 }, {  21, 6 }, { 101, 7 }, { 255, 8 },
-    {  37, 6 }, {   0, 5 }, {  62, 6 }, {   7, 5 }, {  60, 6 }, {  49, 6 }, { 100, 7 },
-    {  30, 6 }, {  53, 6 }, {   8, 5 }, {  14, 5 }, {   5, 5 }, {  54, 6 }, {  26, 6 },
-    {  43, 6 }, {   1, 5 }, {  20, 5 }, {  14, 4 }, {  22, 5 }, {   9, 5 }, {  46, 6 },
-    {  47, 6 }, {  61, 6 }, {  17, 5 }, {  16, 5 }, {  11, 5 }, {   4, 5 }, {  38, 6 },
-    {   6, 6 }, {  52, 6 }, {   6, 5 }, {  12, 5 }, {   2, 5 }, {  55, 6 }, {  27, 6 },
-    { 254, 8 }, { 126, 7 }, {  31, 6 }, {  48, 6 }, {  42, 6 }, {   7, 6 }, {  79, 7 }
-}, {
-    {  65, 9 }, { 161, 8 }, { 109, 7 }, {  11, 6 }, { 116, 7 }, { 160, 8 }, {  71, 9 },
-    {  34, 8 }, {  97, 7 }, {  56, 6 }, {   8, 5 }, {  55, 6 }, {  85, 7 }, { 166, 8 },
-    {  84, 7 }, {  52, 6 }, {   3, 4 }, {  11, 4 }, {   5, 4 }, {  59, 6 }, {  86, 7 },
-    {  10, 6 }, {  13, 5 }, {   9, 4 }, {   0, 3 }, {   8, 4 }, {  12, 5 }, {   9, 6 },
-    {  98, 7 }, {  51, 6 }, {  31, 5 }, {   7, 4 }, {  30, 5 }, {  53, 6 }, {  99, 7 },
-    { 162, 8 }, { 108, 7 }, {  50, 6 }, {   9, 5 }, {  57, 6 }, {  82, 7 }, { 163, 8 },
-    {  64, 9 }, { 167, 8 }, {  87, 7 }, { 117, 7 }, {  96, 7 }, {  33, 8 }, {  70, 9 }
-} };
-#endif
-
-static const Huffman_t   HuffQ3_src [2] [ 7] = { {
-    { 12, 4 }, { 4, 3 }, { 0, 2 }, { 1, 2 }, { 7, 3 }, { 5, 3 }, { 13, 4 }
-}, {
-    {  4, 5 }, { 3, 4 }, { 2, 2 }, { 3, 2 }, { 1, 2 }, { 0, 3 }, {  5, 5 }
-} };
-
-static const Huffman_t   HuffQ4_src [2] [ 9] = { {
-    { 5, 4 }, {  0, 3 }, { 4, 3 }, { 6, 3 }, { 7, 3 }, { 5, 3 }, {  3, 3 }, { 1, 3 }, { 4, 4 }
-}, {
-    { 9, 5 }, { 12, 4 }, { 3, 3 }, { 0, 2 }, { 2, 2 }, { 7, 3 }, { 13, 4 }, { 5, 4 }, { 8, 5 }
-} };
-
-static const Huffman_t   HuffQ5_src [2] [15] = { {
-    {  57, 6 }, { 23, 5 }, {  8, 4 }, { 10, 4 }, { 13, 4 }, {   0, 3 }, {   2, 3 }, { 3, 3 },
-    {   1, 3 }, { 15, 4 }, { 12, 4 }, {  9, 4 }, { 29, 5 }, {  22, 5 }, {  56, 6 }
-}, {
-    { 229, 8 }, { 56, 6 }, {  7, 5 }, {  2, 4 }, {  0, 3 }, {   3, 3 }, {   5, 3 }, { 6, 3 },
-    {   4, 3 }, {  2, 3 }, { 15, 4 }, { 29, 5 }, {  6, 5 }, { 115, 7 }, { 228, 8 },
-} };
-
-static const Huffman_t   HuffQ6_src [2] [31] = { {
-    {   65,  7 }, {    6,  6 }, {  44,  6 }, {  45, 6 }, {   59,  6 }, {   13,  5 }, {   17,  5 }, { 19, 5 },
-    {   23,  5 }, {   21,  5 }, {  26,  5 }, {  30, 5 }, {    0,  4 }, {    2,  4 }, {    5,  4 }, {  7, 4 },
-    {    3,  4 }, {    4,  4 }, {  31,  5 }, {  28, 5 }, {   25,  5 }, {   27,  5 }, {   24,  5 }, { 20, 5 },
-    {   18,  5 }, {   12,  5 }, {   2,  5 }, {  58, 6 }, {   33,  6 }, {    7,  6 }, {   64,  7 },
-}, {
-    { 6472, 13 }, { 6474, 13 }, { 808, 10 }, { 405, 9 }, {  203,  8 }, {  102,  7 }, {   49,  6 }, {  9, 5 },
-    {   15,  5 }, {   31,  5 }, {   2,  4 }, {   6, 4 }, {    8,  4 }, {   11,  4 }, {   13,  4 }, {  0, 3 },
-    {   14,  4 }, {   10,  4 }, {   9,  4 }, {   5, 4 }, {    3,  4 }, {   30,  5 }, {   14,  5 }, {  8, 5 },
-    {   48,  6 }, {  103,  7 }, { 201,  8 }, { 200, 8 }, { 1619, 11 }, { 6473, 13 }, { 6475, 13 },
-} };
-
-static const Huffman_t   HuffQ7_src [2] [63] = { {
-    { 103, 8 },    // 0.3338   01100111
-    { 153, 8 },    // 0.3766   10011001
-    { 181, 8 },    // 0.4715   10110101
-    { 233, 8 },    // 0.5528   11101001
-    {  64, 7 },    // 0.6677    1000000
-    {  65, 7 },    // 0.7041    1000001
-    {  77, 7 },    // 0.7733    1001101
-    {  81, 7 },    // 0.8296    1010001
-    {  91, 7 },    // 0.9295    1011011
-    { 113, 7 },    // 1.0814    1110001
-    { 112, 7 },    // 1.0807    1110000
-    {  24, 6 },    // 1.2748     011000
-    {  29, 6 },    // 1.3390     011101
-    {  35, 6 },    // 1.4224     100011
-    {  37, 6 },    // 1.5201     100101
-    {  41, 6 },    // 1.6642     101001
-    {  44, 6 },    // 1.7292     101100
-    {  46, 6 },    // 1.8647     101110
-    {  51, 6 },    // 2.0473     110011
-    {  49, 6 },    // 2.0152     110001
-    {  54, 6 },    // 2.1315     110110
-    {  55, 6 },    // 2.1358     110111
-    {  57, 6 },    // 2.1700     111001
-    {  60, 6 },    // 2.2449     111100
-    {   0, 5 },    // 2.3063      00000
-    {   2, 5 },    // 2.3854      00010
-    {  10, 5 },    // 2.5481      01010
-    {   5, 5 },    // 2.4867      00101
-    {   9, 5 },    // 2.5352      01001
-    {   6, 5 },    // 2.5074      00110
-    {  13, 5 },    // 2.5745      01101
-    {   7, 5 },    // 2.5195      00111
-    {  11, 5 },    // 2.5502      01011
-    {  15, 5 },    // 2.6251      01111
-    {   8, 5 },    // 2.5260      01000
-    {   4, 5 },    // 2.4418      00100
-    {   3, 5 },    // 2.3983      00011
-    {   1, 5 },    // 2.3697      00001
-    {  63, 6 },    // 2.3041     111111
-    {  62, 6 },    // 2.2656     111110
-    {  61, 6 },    // 2.2549     111101
-    {  53, 6 },    // 2.1151     110101
-    {  59, 6 },    // 2.2042     111011
-    {  52, 6 },    // 2.0837     110100
-    {  48, 6 },    // 1.9446     110000
-    {  47, 6 },    // 1.9189     101111
-    {  43, 6 },    // 1.7177     101011
-    {  42, 6 },    // 1.7035     101010
-    {  39, 6 },    // 1.5287     100111
-    {  36, 6 },    // 1.4559     100100
-    {  33, 6 },    // 1.4117     100001
-    {  28, 6 },    // 1.2776     011100
-    { 117, 7 },    // 1.1107    1110101
-    { 101, 7 },    // 1.0636    1100101
-    { 100, 7 },    // 0.9751    1100100
-    {  80, 7 },    // 0.8132    1010000
-    {  69, 7 },    // 0.7091    1000101
-    {  68, 7 },    // 0.7084    1000100
-    {  50, 7 },    // 0.6277    0110010
-    { 232, 8 },    // 0.5386   11101000
-    { 180, 8 },    // 0.4408   10110100
-    { 152, 8 },    // 0.3759   10011000
-    { 102, 8 },    // 0.3160   01100110
-}, {
-    { 14244, 14 },    // 0.0059   11011110100100
-    { 14253, 14 },    // 0.0098   11011110101101
-    { 14246, 14 },    // 0.0078   11011110100110
-    { 14254, 14 },    // 0.0111   11011110101110
-    {  3562, 12 },    // 0.0320     110111101010
-    {   752, 10 },    // 0.0920       1011110000
-    {   753, 10 },    // 0.1057       1011110001
-    {   160,  9 },    // 0.1403        010100000
-    {   162,  9 },    // 0.1579        010100010
-    {   444,  9 },    // 0.2486        110111100
-    {   122,  8 },    // 0.3772         01111010
-    {   223,  8 },    // 0.5710         11011111
-    {    60,  7 },    // 0.6858          0111100
-    {    73,  7 },    // 0.8033          1001001
-    {   110,  7 },    // 0.9827          1101110
-    {    14,  6 },    // 1.2601           001110
-    {    24,  6 },    // 1.3194           011000
-    {    25,  6 },    // 1.3938           011001
-    {    34,  6 },    // 1.5693           100010
-    {    37,  6 },    // 1.7846           100101
-    {    54,  6 },    // 2.0078           110110
-    {     3,  5 },    // 2.2975            00011
-    {     9,  5 },    // 2.5631            01001
-    {    11,  5 },    // 2.7021            01011
-    {    16,  5 },    // 3.1465            10000
-    {    19,  5 },    // 3.4244            10011
-    {    21,  5 },    // 3.5921            10101
-    {    24,  5 },    // 3.7938            11000
-    {    26,  5 },    // 3.9595            11010
-    {    29,  5 },    // 4.1546            11101
-    {    31,  5 },    // 4.2623            11111
-    {     2,  4 },    // 4.5180             0010
-    {     0,  4 },    // 4.3151             0000
-    {    30,  5 },    // 4.2538            11110
-    {    28,  5 },    // 4.1422            11100
-    {    25,  5 },    // 3.9145            11001
-    {    22,  5 },    // 3.6691            10110
-    {    20,  5 },    // 3.4955            10100
-    {    14,  5 },    // 2.9155            01110
-    {    13,  5 },    // 2.7921            01101
-    {     8,  5 },    // 2.5553            01000
-    {     6,  5 },    // 2.3093            00110
-    {     2,  5 },    // 2.1200            00010
-    {    46,  6 },    // 1.8134           101110
-    {    35,  6 },    // 1.5824           100011
-    {    31,  6 },    // 1.4701           011111
-    {    21,  6 },    // 1.3187           010101
-    {    15,  6 },    // 1.2776           001111
-    {    95,  7 },    // 0.9664          1011111
-    {    72,  7 },    // 0.7922          1001000
-    {    41,  7 },    // 0.6838          0101001
-    {   189,  8 },    // 0.5024         10111101
-    {   123,  8 },    // 0.3830         01111011
-    {   377,  9 },    // 0.2232        101111001
-    {   161,  9 },    // 0.1566        010100001
-    {   891, 10 },    // 0.1383       1101111011
-    {   327, 10 },    // 0.0900       0101000111
-    {   326, 10 },    // 0.0790       0101000110
-    {  3560, 12 },    // 0.0254     110111101000
-    { 14255, 14 },    // 0.0117   11011110101111
-    { 14247, 14 },    // 0.0085   11011110100111
-    { 14252, 14 },    // 0.0085   11011110101100
-    { 14245, 14 },    // 0.0065   11011110100101
-} };
-
-#ifdef USE_SV8
-static const Huffman_t   HuffN8_src [2] [127] = { {
-    { 2426, 13 }, { 4943, 13 }, {  787, 12 }, { 2470, 12 }, { 7270, 13 }, { 1764, 12 },
-    { 3632, 12 }, { 3633, 12 }, { 2486, 12 }, {  395, 11 }, {  607, 11 }, { 1242, 11 },
-    {  108, 10 }, {  300, 10 }, {  199, 10 }, {  440, 10 }, {  442, 10 }, {  616, 10 },
-    {  909, 10 }, {  897, 10 }, {  178,  9 }, {  309,  9 }, {  311,  9 }, {  451,  9 },
-    {  449,  9 }, {   26,  8 }, {   74,  8 }, {   94,  8 }, {  122,  8 }, {  136,  8 },
-    {   12,  7 }, {   29,  7 }, {   28,  7 }, {   36,  7 }, {   39,  7 }, {   46,  7 },
-    {   60,  7 }, {   69,  7 }, {   76,  7 }, {   92,  7 }, {  126,  7 }, {   11,  6 },
-    {   15,  6 }, {   10,  6 }, {   16,  6 }, {   21,  6 }, {   25,  6 }, {   28,  6 },
-    {   32,  6 }, {   31,  6 }, {   37,  6 }, {   47,  6 }, {   43,  6 }, {   35,  6 },
-    {   45,  6 }, {   48,  6 }, {   52,  6 }, {   53,  6 }, {   54,  6 }, {   62,  6 },
-    {   59,  6 }, {    0,  5 }, {   61,  6 }, {   51,  6 }, {    2,  5 }, {    1,  5 },
-    {   60,  6 }, {   57,  6 }, {   58,  6 }, {   55,  6 }, {   50,  6 }, {   49,  6 },
-    {   42,  6 }, {   40,  6 }, {   44,  6 }, {   41,  6 }, {   39,  6 }, {   33,  6 },
-    {   29,  6 }, {   26,  6 }, {   24,  6 }, {   20,  6 }, {   17,  6 }, {   13,  6 },
-    {    8,  6 }, {    9,  6 }, {  127,  7 }, {   93,  7 }, {   73,  7 }, {   72,  7 },
-    {   54,  7 }, {   38,  7 }, {   45,  7 }, {   14,  7 }, {   25,  7 }, {   15,  7 },
-    {  226,  8 }, {  137,  8 }, {  111,  8 }, {   95,  8 }, {   88,  8 }, {   48,  8 },
-    {  455,  9 }, {  450,  9 }, {  246,  9 }, {  247,  9 }, {  179,  9 }, {   55,  9 },
-    {  896, 10 }, {  620, 10 }, {  443, 10 }, {  302, 10 }, {  301, 10 }, {  198, 10 },
-    {  109, 10 }, { 1234, 11 }, {  883, 11 }, {  392, 11 }, {  394, 11 }, { 3634, 12 },
-    { 2487, 12 }, {  786, 12 }, { 1765, 12 }, { 1212, 12 }, { 7271, 13 }, { 2427, 13 },
-    { 4942, 13 }
-}, {
-    { 3728, 12 }, { 4005, 12 }, {  264, 11 }, { 4004, 12 }, { 4044, 12 }, { 4045, 12 },
-    { 4046, 12 }, { 1424, 11 }, {  449, 11 }, {  448, 11 }, {  139, 10 }, {  231, 10 },
-    {  133, 10 }, {  719, 10 }, {  641, 10 }, {  676, 10 }, {  225, 10 }, {  677, 10 },
-    {  620, 10 }, {   72,  9 }, {   23,  9 }, {   67,  9 }, {   75,  9 }, {  113,  9 },
-    {  311,  9 }, {   68,  9 }, {  316,  9 }, {  467,  9 }, {   10,  8 }, {  468,  9 },
-    {   35,  8 }, {   27,  8 }, {  358,  9 }, {   32,  8 }, {   26,  8 }, {  501,  9 },
-    {   44,  8 }, {   45,  8 }, {  142,  8 }, {  173,  8 }, {  161,  8 }, {  188,  8 },
-    {  189,  8 }, {  190,  8 }, {  191,  8 }, {  254,  8 }, {  251,  8 }, {  255,  8 },
-    {   19,  7 }, {   26,  7 }, {   70,  7 }, {   76,  7 }, {   87,  7 }, {   85,  7 },
-    {  124,  7 }, {    7,  6 }, {   15,  6 }, {   41,  6 }, {   46,  6 }, {    2,  5 },
-    {   16,  5 }, {   28,  5 }, {   12,  4 }, {    1,  2 }, {   13,  4 }, {   30,  5 },
-    {   18,  5 }, {    0,  5 }, {   45,  6 }, {   34,  6 }, {   12,  6 }, {    3,  6 },
-    {  118,  7 }, {   88,  7 }, {   81,  7 }, {   29,  7 }, {   78,  7 }, {   23,  7 },
-    {   27,  7 }, {  253,  8 }, {   12,  7 }, {  232,  8 }, {  235,  8 }, {  159,  8 },
-    {  238,  8 }, {  172,  8 }, {  168,  8 }, {  143,  8 }, {  154,  8 }, {   40,  8 },
-    {    8,  8 }, {  478,  9 }, {    9,  8 }, {  479,  9 }, {  469,  9 }, {   42,  8 },
-    {   43,  8 }, {  504,  9 }, {  357,  9 }, {  321,  9 }, {  339,  9 }, {  317,  9 },
-    {  114,  9 }, {   82,  9 }, {   83,  9 }, {   73,  9 }, {   74,  9 }, { 1000, 10 },
-    {  933, 10 }, {  621, 10 }, {  718, 10 }, { 2003, 11 }, {  713, 10 }, { 2020, 11 },
-    {  230, 10 }, { 1865, 11 }, {   44, 10 }, {  138, 10 }, { 1280, 11 }, { 2021, 11 },
-    { 3729, 12 }, { 4047, 12 }, {   90, 11 }, {  265, 11 }, { 1281, 11 }, { 1425, 11 },
-    {   91, 11 }
-} };
-#endif
-
-/*
- *  Fills out the items Code and Length (but not Value) of a Huffman table
- *  from a bit packed Huffman table 'src'. Table is not sorted, so this is
- *  the table which is suitable for an encoder. Be careful: To get a table
- *  usable for a decoder you must use Resort_HuffTable() after this
- *  function. It's a little bit dangerous to divide the functionality, maybe
- *  there is a more secure and handy solution to this problem.
- */
-
-void Make_HuffTable ( Huffman_t* dst, const Huffman_t* src, mpc_size_t len )
-{
-	mpc_size_t  i;
-
-	for ( i = 0; i < len; i++,src++,dst++ ) {
-		dst->Code   = src->Code  ;
-		dst->Length = src->Length;
-	}
-}
-
-
-/*
- *  Generates a Lookup table for quick Huffman decoding. This table must
- *  have a size of a power of 2. Input is the pre-sorted Huffman table,
- *  sorted by Resort_HuffTable() and its length, and the length of the
- *  lookup table. Output is the Lookup table. It can be used for table based
- *  decoding (Huffman_decode_fastest) which fully decodes by means of the
- *  LUT. This is only handy for small huffman codes up to 9...10 bit
- *  maximum length. For longer codes partial lookup is possible with
- *  Huffman_decode_faster() which first estimates possible codes by means
- *  of LUT and then searches the exact code like the tableless version
- *  Huffman_decode().
- */
-
-void Make_LookupTable ( mpc_uint8_t* LUT,
-						mpc_size_t LUT_len,
-						const Huffman_t* const Table,
-						const mpc_size_t elements )
-{
-	mpc_size_t    i;
-	mpc_size_t    idx  = elements;
-	mpc_uint32_t  dval = (mpc_uint32_t)0x80000000L / LUT_len * 2;
-	mpc_uint32_t  val  = dval - 1;
-
-	for ( i = 0; i < LUT_len; i++, val += dval ) {
-		while ( idx > 0  &&  val >= Table[idx-1].Code )
-			idx--;
-		*LUT++ = (mpc_uint8_t)idx;
-	}
-
-	return;
-}
-
-#define MAKE(d,s)     Make_HuffTable   ( (d), (s), sizeof(s)/sizeof(*(s)) )
-#define LOOKUP(x,q)   Make_LookupTable ( (q), sizeof(q), (x), sizeof(x)/sizeof(*(x)) )
-
-
-void
-Init_Huffman_Encoder_SV7 ( void )
-{
-    // Splitting of the 36 Samples
-    MAKE ( HuffSCFI, HuffSCFI_src );
-
-    // Differential Scalefactors
-    MAKE ( HuffDSCF, HuffDSCF_src );
-
-    // resolution, differential quantizer indizes
-    MAKE ( HuffHdr, HuffHdr_src );
-
-    // 3-step quantizer, 3 bundled samples
-    MAKE ( HuffQ1[0], HuffQ1_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ1[1], HuffQ1_src[1] );          // more shaped, book 1
-
-    // 5-step quantizer, 2 bundled samples
-    MAKE ( HuffQ2[0], HuffQ2_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ2[1], HuffQ2_src[1] );          // more shaped, book 1
-
-    // 7-step quantizer, single samples
-    MAKE ( HuffQ3[0], HuffQ3_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ3[1], HuffQ3_src[1] );          // more shaped, book 1
-
-#ifdef USE_SV8
-    // 7-step quantizer, 2 bundled samples
-    MAKE ( HuffN3[0], HuffN3_src[0] );          // less shaped, book 0
-    MAKE ( HuffN3[1], HuffN3_src[1] );          // more shaped, book 1
-#endif
-
-    // 9-step quantizer, single samples
-    MAKE ( HuffQ4[0], HuffQ4_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ4[1], HuffQ4_src[1] );          // more shaped, book 1
-
-    // 15-step quantizer, single samples
-    MAKE ( HuffQ5[0], HuffQ5_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ5[1], HuffQ5_src[1] );          // more shaped, book 1
-
-    // 31-step quantizer, single samples
-    MAKE ( HuffQ6[0], HuffQ6_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ6[1], HuffQ6_src[1] );          // more shaped, book 1
-
-    // 63-step quantizer, single samples
-    MAKE ( HuffQ7[0], HuffQ7_src[0] );          // less shaped, book 0
-    MAKE ( HuffQ7[1], HuffQ7_src[1] );          // more shaped, book 1
-
-#ifdef USE_SV8
-    // 127-step quantizer, single samples
-    MAKE ( HuffN8[0], HuffN8_src[0] );          // book 0
-    MAKE ( HuffN8[1], HuffN8_src[1] );          // book 1
-#endif
-}
-
 /* end of huffsv7.c */
Index: libmpc/branches/r2d/libmpcenc/libmpcenc.h
===================================================================
--- libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 159)
+++ libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 161)
@@ -65,4 +65,5 @@
 	unsigned int  MS_Channelmode;
 	unsigned int  Overflows; //       = 0;      // number of internal (filterbank) clippings
+	unsigned int MaxBand; /// number of non zero bands in last frame
  } mpc_encoder_t;
 
