Index: /libmpc/branches/r2d/common/huffman.c
===================================================================
--- /libmpc/branches/r2d/common/huffman.c	(revision 160)
+++ /libmpc/branches/r2d/common/huffman.c	(revision 161)
@@ -194,9 +194,10 @@
  * @param num_symbols
  * @param print_type
- */
-void _Huffman_PrintCodes(huff_sym_t * sym, unsigned int num_symbols, int print_type)
+ * @param offset
+ */
+void _Huffman_PrintCodes(huff_sym_t * sym, unsigned int num_symbols, int print_type, int offset)
 {
 	unsigned int total_cnt = 0, total_size = 0, optim_size = 0;
-	int i;
+	int i, packs[4];
 
 	switch( print_type ) {
@@ -217,5 +218,26 @@
 			printf("{\n	");
 			for( i = num_symbols - 1; i >= 0; i--) {
-				printf("{0x%.8x, %u, %u}", sym[i].Code << (32 - sym[i].Bits), sym[i].Bits, sym[i].Symbol);
+				printf("{0x%.8x, %u, %i}", sym[i].Code << (32 - sym[i].Bits), sym[i].Bits, sym[i].Symbol - offset);
+				if (i != 0)
+					printf(", ");
+			}
+			printf("\n}\n");
+			break;
+		case 4:
+			qsort(sym, num_symbols, sizeof(huff_sym_t),
+				  (int (*)(const void *, const void *)) _Huffman_CompBits);
+			printf("{\n	");
+			for( i = num_symbols - 1; i >= 0; i--) {
+				int symbol =  sym[i].Symbol;
+				packs[3] = symbol / (offset * offset * offset);
+				packs[2] = (symbol - packs[3] * offset * offset * offset) / (offset * offset);
+				packs[1] = (symbol - (packs[3] * offset + packs[2]) * offset * offset) / offset;
+				packs[0] = symbol - ((packs[3] * offset + packs[2]) * offset + packs[1] * offset);
+				packs[0] -= offset >> 1;
+				packs[1] -= offset >> 1;
+				packs[2] -= offset >> 1;
+				packs[3] -= offset >> 1;
+				symbol = ((packs[3] & 15) << 12) | ((packs[2] & 15) << 8) | ((packs[1] & 15) << 4) | (packs[0] & 15);
+				printf("{0x%.8x, %u, %i}", sym[i].Code << (32 - sym[i].Bits), sym[i].Bits, symbol);
 				if (i != 0)
 					printf(", ");
@@ -234,5 +256,5 @@
 			for( i = 0; i < num_symbols; i++) {
 				int k = sym[i].Bits - 1;
-				printf("%-10u	%-10u	%-10u	", sym[i].Symbol, sym[i].Count, sym[i].Bits);
+				printf("%-10i	%-10u	%-10u	", sym[i].Symbol - offset, sym[i].Count, sym[i].Bits);
 				for (; k >= 0 ; k--)
 					printf("%u", (sym[i].Code >> k) & 1 );
Index: /libmpc/branches/r2d/include/mpcdec/mpcdec.h
===================================================================
--- /libmpc/branches/r2d/include/mpcdec/mpcdec.h	(revision 160)
+++ /libmpc/branches/r2d/include/mpcdec/mpcdec.h	(revision 161)
@@ -60,4 +60,5 @@
 	mpc_int32_t bits;	/// number of bits consumed by this frame (-1) if end of stream
 	MPC_SAMPLE_FORMAT * buffer;	/// frame samples buffer (size = samples * channels * sizeof(MPC_SAMPLE_FORMAT)
+	mpc_bool_t is_key_frame; /// 1 if this frame is a key frame (first in block) 0 else. Set by the demuxer.
 } mpc_frame_info;
 
Index: /libmpc/branches/r2d/libmpc.kdevelop
===================================================================
--- /libmpc/branches/r2d/libmpc.kdevelop	(revision 160)
+++ /libmpc/branches/r2d/libmpc.kdevelop	(revision 161)
@@ -3,5 +3,5 @@
   <general>
     <author>Nicolas Botti</author>
-    <email></email>
+    <email/>
     <version>0.1</version>
     <projectmanagement>KDevAutoProject</projectmanagement>
@@ -14,10 +14,10 @@
     <projectdirectory>.</projectdirectory>
     <absoluteprojectpath>false</absoluteprojectpath>
-    <description></description>
+    <description/>
   </general>
   <kdevautoproject>
     <general>
       <activetarget>mppenc/mppenc</activetarget>
-      <useconfiguration>debug</useconfiguration>
+      <useconfiguration>optimized</useconfiguration>
     </general>
     <run>
@@ -29,5 +29,5 @@
       </runarguments>
       <customdirectory>/</customdirectory>
-      <programargs></programargs>
+      <programargs/>
       <autocompile>true</autocompile>
       <envvars/>
@@ -41,13 +41,13 @@
         <cflags>-O3 -fomit-frame-pointer -pipe -Wall</cflags>
         <envvars/>
-        <configargs></configargs>
-        <topsourcedir></topsourcedir>
+        <configargs/>
+        <topsourcedir/>
         <cppflags>-DMPP_ENCODER=1 -DTRUE=1 -DFALSE=0</cppflags>
-        <ldflags></ldflags>
-        <ccompilerbinary></ccompilerbinary>
-        <cxxcompilerbinary></cxxcompilerbinary>
-        <f77compilerbinary></f77compilerbinary>
-        <cxxflags></cxxflags>
-        <f77flags></f77flags>
+        <ldflags/>
+        <ccompilerbinary/>
+        <cxxcompilerbinary/>
+        <f77compilerbinary/>
+        <cxxflags/>
+        <f77flags/>
       </optimized>
       <debug>
@@ -77,5 +77,5 @@
       <numberofjobs>1</numberofjobs>
       <dontact>false</dontact>
-      <makebin></makebin>
+      <makebin/>
       <prio>0</prio>
     </make>
@@ -139,4 +139,6 @@
       <group pattern="*.h" name="Header files" />
       <group pattern="*.c" name="Source files" />
+      <hidenonprojectfiles>false</hidenonprojectfiles>
+      <hidenonlocation>false</hidenonlocation>
     </groups>
     <tree>
@@ -182,5 +184,5 @@
     </codecompletion>
     <creategettersetter>
-      <prefixGet></prefixGet>
+      <prefixGet/>
       <prefixSet>set</prefixSet>
       <prefixVariable>m_,_</prefixVariable>
@@ -198,10 +200,10 @@
   <kdevdebugger>
     <general>
-      <programargs>--overwrite /home/nico/08\ -\ Jimmy\ Jimmy.wav</programargs>
-      <gdbpath></gdbpath>
+      <programargs>--overwrite /home/nico/musepack-huffman-training.wv</programargs>
+      <gdbpath/>
       <dbgshell>libtool</dbgshell>
-      <configGdbScript></configGdbScript>
-      <runShellScript></runShellScript>
-      <runGdbScript></runGdbScript>
+      <configGdbScript/>
+      <runShellScript/>
+      <runGdbScript/>
       <breakonloadinglibs>true</breakonloadinglibs>
       <separatetty>false</separatetty>
Index: /libmpc/branches/r2d/libmpcdec/decoder.h
===================================================================
--- /libmpc/branches/r2d/libmpcdec/decoder.h	(revision 160)
+++ /libmpc/branches/r2d/libmpcdec/decoder.h	(revision 161)
@@ -73,4 +73,5 @@
 	mpc_uint64_t decoded_samples;    ///< Number of samples decoded from file begining
 	mpc_uint32_t samples_to_skip;    ///< Number samples to skip (used for seeking)
+	mpc_int32_t last_max_band;       ///< number of bands used in the last frame
 
     // randomizer state variables
Index: /libmpc/branches/r2d/libmpcdec/huffman.c
===================================================================
--- /libmpc/branches/r2d/libmpcdec/huffman.c	(revision 160)
+++ /libmpc/branches/r2d/libmpcdec/huffman.c	(revision 161)
@@ -36,9 +36,9 @@
 #include "huffman.h"
 
-const mpc_huffman mpc_table_HuffHdr  [10] = 
+const mpc_huffman mpc_table_HuffHdr  [10] =
 {{2147483648u,1,0},{1610612736u,3,1},{1577058304u,7,-4},{1568669696u,9,3},{1560281088u,9,4},{1543503872u,8,-5},{1476395008u,6,2},{1342177280u,5,-3},{1073741824u,4,-2},{0u,2,-1},};
 const mpc_huffman mpc_table_HuffSCFI [ 4] =
 {{2147483648u,1,1},{1610612736u,3,2},{1073741824u,3,0},{0u,2,3},};
-const mpc_huffman mpc_table_HuffDSCF [16] = 
+const mpc_huffman mpc_table_HuffDSCF [16] =
 {{4160749568u,5,5},{4026531840u,5,-4},{3758096384u,4,3},{3489660928u,4,-3},{3221225472u,4,8},{2684354560u,3,1},{2415919104u,4,0},{2281701376u,5,-5},{2214592512u,6,7},{2147483648u,6,-7},{1610612736u,3,-1},{1073741824u,3,2},{805306368u,4,4},{671088640u,5,6},{536870912u,5,-6},{0u,3,-2},};
 
@@ -76,2 +76,92 @@
     {0,mpc_table_HuffQ1[1],mpc_table_HuffQ2[1],mpc_table_HuffQ3[1],mpc_table_HuffQ4[1],mpc_table_HuffQ5[1],mpc_table_HuffQ6[1],mpc_table_HuffQ7[1]},
 };
+
+const mpc_huffman mpc_table_HuffSCFI_1 [4] = {
+	{0x80000000, 1, 2}, {0x40000000, 2, 3}, {0x20000000, 3, 1}, {0x00000000, 3, 0}
+};
+
+const mpc_huffman mpc_table_HuffSCFI_2 [16] = {
+	{0xc0000000, 2, 15}, {0x80000000, 2, 10}, {0x60000000, 3, 14}, {0x40000000, 3, 11}, {0x38000000, 5, 13}, {0x30000000, 5, 9}, {0x28000000, 5, 7}, {0x20000000, 5, 6}, {0x18000000, 5, 5}, {0x14000000, 6, 12}, {0x10000000, 6, 8}, {0x0c000000, 6, 3}, {0x08000000, 6, 2}, {0x04000000, 6, 0}, {0x02000000, 7, 4}, {0x00000000, 7, 1}
+};
+
+const mpc_huffman mpc_table_HuffDSCF_1 [64] = {
+	{0xe0000000, 3, 35}, {0xc0000000, 3, 34}, {0xa0000000, 3, 33}, {0x90000000, 4, 36}, {0x80000000, 4, 32}, {0x70000000, 4, 30}, {0x60000000, 4, 29}, {0x50000000, 4, 27}, {0x40000000, 4, 26}, {0x38000000, 5, 37}, {0x30000000, 5, 28}, {0x28000000, 5, 25}, {0x24000000, 6, 39}, {0x20000000, 6, 38}, {0x1c000000, 6, 24}, {0x18000000, 6, 23}, {0x16000000, 7, 40}, {0x14000000, 7, 22}, {0x12000000, 7, 21}, {0x10000000, 7, 20}, {0x0e000000, 7, 19}, {0x0d000000, 8, 43}, {0x0c000000, 8, 42}, {0x0b000000, 8, 41}, {0x0a000000, 8, 18}, {0x09000000, 8, 17}, {0x08000000, 8, 16}, {0x07000000, 8, 15}, {0x06800000, 9, 46}, {0x06000000, 9, 45}, {0x05800000, 9, 44}, {0x05000000, 9, 14}, {0x04800000, 9, 13}, {0x04000000, 9, 12}, {0x03800000, 9, 11}, {0x03400000, 10, 49}, {0x03000000, 10, 48}, {0x02c00000, 10, 47}, {0x02800000, 10, 31}, {0x02400000, 10, 10}, {0x02000000, 10, 9}, {0x01c00000, 10, 8}, {0x01800000, 10, 7}, {0x01400000, 10, 6}, {0x01200000, 11, 52}, {0x01000000, 11, 51}, {0x00e00000, 11, 50}, {0x00c00000, 11, 5}, {0x00a00000, 11, 4}, {0x00800000, 11, 3}, {0x00700000, 12, 54}, {0x00600000, 12, 53}, {0x00500000, 12, 2}, {0x00400000, 12, 1}, {0x00300000, 12, 0}, {0x00280000, 13, 57}, {0x00200000, 13, 56}, {0x00180000, 13, 55}, {0x00140000, 14, 63}, {0x00100000, 14, 62}, {0x000c0000, 14, 61}, {0x00080000, 14, 60}, {0x00040000, 14, 59}, {0x00000000, 14, 58}
+};
+
+const mpc_huffman mpc_table_HuffDSCF_2 [65] = {
+	{0xe0000000, 3, 33}, {0xc0000000, 3, 32}, {0xa0000000, 3, 31}, {0x80000000, 3, 30}, {0x60000000, 3, 29}, {0x50000000, 4, 34}, {0x40000000, 4, 28}, {0x30000000, 4, 27}, {0x28000000, 5, 36}, {0x20000000, 5, 35}, {0x18000000, 5, 26}, {0x14000000, 6, 37}, {0x10000000, 6, 25}, {0x0e000000, 7, 38}, {0x0c000000, 7, 24}, {0x0a000000, 7, 23}, {0x09000000, 8, 40}, {0x08000000, 8, 39}, {0x07000000, 8, 22}, {0x06000000, 8, 21}, {0x05800000, 9, 42}, {0x05000000, 9, 41}, {0x04800000, 9, 20}, {0x04000000, 9, 19}, {0x03800000, 9, 18}, {0x03400000, 10, 45}, {0x03000000, 10, 44}, {0x02c00000, 10, 43}, {0x02800000, 10, 17}, {0x02400000, 10, 16}, {0x02000000, 10, 15}, {0x01c00000, 10, 14}, {0x01a00000, 11, 48}, {0x01800000, 11, 47}, {0x01600000, 11, 46}, {0x01400000, 11, 13}, {0x01200000, 11, 12}, {0x01000000, 11, 11}, {0x00e00000, 11, 10}, {0x00d00000, 12, 64}, {0x00c00000, 12, 52}, {0x00b00000, 12, 51}, {0x00a00000, 12, 50}, {0x00900000, 12, 49}, {0x00800000, 12, 9}, {0x00700000, 12, 8}, {0x00600000, 12, 7}, {0x00500000, 12, 6}, {0x00480000, 13, 55}, {0x00400000, 13, 54}, {0x00380000, 13, 53}, {0x00300000, 13, 5}, {0x00280000, 13, 4}, {0x00200000, 13, 3}, {0x001c0000, 14, 58}, {0x00180000, 14, 57}, {0x00140000, 14, 56}, {0x00100000, 14, 2}, {0x000c0000, 14, 1}, {0x000a0000, 15, 63}, {0x00080000, 15, 62}, {0x00060000, 15, 61}, {0x00040000, 15, 60}, {0x00020000, 15, 59}, {0x00000000, 15, 0}
+};
+
+const mpc_huffman mpc_table_HuffBands [33] = {
+	{0x80000000, 1, 0}, {0x40000000, 2, 32}, {0x20000000, 3, 1}, {0x18000000, 5, 31}, {0x10000000, 5, 2}, {0x0c000000, 6, 30}, {0x08000000, 6, 3}, {0x06000000, 7, 4}, {0x05000000, 8, 29}, {0x04000000, 8, 6}, {0x03000000, 8, 5}, {0x02800000, 9, 28}, {0x02000000, 9, 7}, {0x01c00000, 10, 27}, {0x01800000, 10, 26}, {0x01400000, 10, 8}, {0x01200000, 11, 25}, {0x01000000, 11, 24}, {0x00e00000, 11, 23}, {0x00c00000, 11, 9}, {0x00b00000, 12, 22}, {0x00a00000, 12, 21}, {0x00900000, 12, 20}, {0x00800000, 12, 18}, {0x00700000, 12, 17}, {0x00600000, 12, 16}, {0x00500000, 12, 15}, {0x00400000, 12, 14}, {0x00300000, 12, 12}, {0x00200000, 12, 11}, {0x00100000, 12, 10}, {0x00080000, 13, 19}, {0x00000000, 13, 13}
+};
+
+const mpc_huffman mpc_table_HuffRes [2] [17] = {
+	{
+		{0x80000000, 1, 0}, {0x40000000, 2, 1}, {0x20000000, 3, 16}, {0x10000000, 4, 2}, {0x08000000, 5, 3}, {0x04000000, 6, 4}, {0x02000000, 7, 5}, {0x01000000, 8, 15}, {0x00800000, 9, 6}, {0x00400000, 10, 7}, {0x00200000, 11, 8}, {0x00100000, 12, 9}, {0x00080000, 13, 10}, {0x00040000, 14, 11}, {0x00020000, 15, 12}, {0x00010000, 16, 14}, {0x00000000, 16, 13}
+	}, {
+		{0xc0000000, 2, 16}, {0x80000000, 2, 1}, {0x40000000, 2, 0}, {0x20000000, 3, 2}, {0x10000000, 4, 15}, {0x08000000, 5, 3}, {0x04000000, 6, 14}, {0x02000000, 7, 4}, {0x01000000, 8, 5}, {0x00800000, 9, 13}, {0x00400000, 10, 6}, {0x00200000, 11, 12}, {0x00100000, 12, 7}, {0x000c0000, 14, 11}, {0x00080000, 14, 10}, {0x00040000, 14, 9}, {0x00000000, 14, 8}
+	}
+};
+
+static const mpc_huffman mpc_table_HuffQ1_8 [19] = {
+	{0xe0000000, 3, 7}, {0xc0000000, 3, 6}, {0xa0000000, 3, 5}, {0x80000000, 3, 4}, {0x60000000, 3, 3}, {0x50000000, 4, 10}, {0x40000000, 4, 9}, {0x30000000, 4, 8}, {0x20000000, 4, 2}, {0x10000000, 4, 1}, {0x08000000, 5, 11}, {0x04000000, 6, 0}, {0x02000000, 7, 12}, {0x01000000, 8, 13}, {0x00800000, 9, 14}, {0x00400000, 10, 15}, {0x00200000, 11, 16}, {0x00100000, 12, 18}, {0x00000000, 12, 17}
+};
+
+static const mpc_huffman mpc_table_HuffQ2_8 [2] [5*5*5] = {
+	{
+		{0xe0000000, 3, 62}, {0xd0000000, 4, 87}, {0xc0000000, 4, 67}, {0xb0000000, 4, 63}, {0xa0000000, 4, 61}, {0x90000000, 4, 57}, {0x80000000, 4, 37}, {0x7c000000, 6, 93}, {0x78000000, 6, 92}, {0x74000000, 6, 88}, {0x70000000, 6, 86}, {0x6c000000, 6, 83}, {0x68000000, 6, 82}, {0x64000000, 6, 81}, {0x60000000, 6, 68}, {0x5c000000, 6, 66}, {0x58000000, 6, 58}, {0x54000000, 6, 56}, {0x50000000, 6, 42}, {0x4c000000, 6, 41}, {0x48000000, 6, 38}, {0x44000000, 6, 36}, {0x40000000, 6, 32}, {0x3c000000, 6, 31}, {0x3a000000, 7, 112}, {0x38000000, 7, 91}, {0x36000000, 7, 72}, {0x34000000, 7, 64}, {0x32000000, 7, 60}, {0x30000000, 7, 52}, {0x2e000000, 7, 43}, {0x2c000000, 7, 33}, {0x2a000000, 7, 12}, {0x29000000, 8, 117}, {0x28000000, 8, 113}, {0x27000000, 8, 111}, {0x26000000, 8, 107}, {0x25000000, 8, 97}, {0x24000000, 8, 89}, {0x23000000, 8, 85}, {0x22000000, 8, 77}, {0x21000000, 8, 73}, {0x20000000, 8, 71}, {0x1f000000, 8, 69}, {0x1e000000, 8, 65}, {0x1d000000, 8, 59}, {0x1c000000, 8, 55}, {0x1b000000, 8, 53}, {0x1a000000, 8, 51}, {0x19000000, 8, 47}, {0x18000000, 8, 39}, {0x17000000, 8, 35}, {0x16000000, 8, 27}, {0x15000000, 8, 17}, {0x14000000, 8, 13}, {0x13000000, 8, 11}, {0x12000000, 8, 7}, {0x11800000, 9, 118}, {0x11000000, 9, 116}, {0x10800000, 9, 108}, {0x10000000, 9, 106}, {0x0f800000, 9, 98}, {0x0f000000, 9, 96}, {0x0e800000, 9, 94}, {0x0e000000, 9, 90}, {0x0d800000, 9, 84}, {0x0d000000, 9, 80}, {0x0c800000, 9, 78}, {0x0c000000, 9, 76}, {0x0b800000, 9, 48}, {0x0b000000, 9, 46}, {0x0a800000, 9, 44}, {0x0a000000, 9, 40}, {0x09800000, 9, 34}, {0x09000000, 9, 30}, {0x08800000, 9, 28}, {0x08000000, 9, 26}, {0x07800000, 9, 18}, {0x07000000, 9, 16}, {0x06800000, 9, 8}, {0x06000000, 9, 6}, {0x05c00000, 10, 122}, {0x05800000, 10, 110}, {0x05400000, 10, 102}, {0x05000000, 10, 74}, {0x04c00000, 10, 70}, {0x04800000, 10, 54}, {0x04400000, 10, 50}, {0x04000000, 10, 22}, {0x03c00000, 10, 2}, {0x03a00000, 11, 123}, {0x03800000, 11, 121}, {0x03600000, 11, 119}, {0x03400000, 11, 115}, {0x03200000, 11, 114}, {0x03000000, 11, 109}, {0x02e00000, 11, 105}, {0x02c00000, 11, 103}, {0x02a00000, 11, 101}, {0x02800000, 11, 99}, {0x02600000, 11, 95}, {0x02400000, 11, 79}, {0x02200000, 11, 75}, {0x02000000, 11, 49}, {0x01e00000, 11, 45}, {0x01c00000, 11, 29}, {0x01a00000, 11, 25}, {0x01800000, 11, 23}, {0x01600000, 11, 21}, {0x01400000, 11, 19}, {0x01200000, 11, 15}, {0x01000000, 11, 14}, {0x00e00000, 11, 10}, {0x00c00000, 11, 9}, {0x00a00000, 11, 5}, {0x00800000, 11, 3}, {0x00600000, 11, 1}, {0x00500000, 12, 124}, {0x00400000, 12, 104}, {0x00300000, 12, 20}, {0x00200000, 12, 0}, {0x00180000, 13, 120}, {0x00100000, 13, 100}, {0x00080000, 13, 24}, {0x00000000, 13, 4}
+	}, {
+		{0xf0000000, 4, 62}, {0xe8000000, 5, 92}, {0xe0000000, 5, 87}, {0xd8000000, 5, 86}, {0xd0000000, 5, 82}, {0xc8000000, 5, 68}, {0xc0000000, 5, 67}, {0xb8000000, 5, 66}, {0xb0000000, 5, 63}, {0xa8000000, 5, 61}, {0xa0000000, 5, 58}, {0x98000000, 5, 57}, {0x90000000, 5, 56}, {0x88000000, 5, 42}, {0x80000000, 5, 38}, {0x78000000, 5, 37}, {0x70000000, 5, 32}, {0x6c000000, 6, 93}, {0x68000000, 6, 91}, {0x64000000, 6, 88}, {0x60000000, 6, 83}, {0x5c000000, 6, 81}, {0x58000000, 6, 43}, {0x54000000, 6, 41}, {0x50000000, 6, 36}, {0x4c000000, 6, 33}, {0x48000000, 6, 31}, {0x46000000, 7, 112}, {0x44000000, 7, 72}, {0x42000000, 7, 64}, {0x40000000, 7, 60}, {0x3e000000, 7, 52}, {0x3c000000, 7, 12}, {0x3b000000, 8, 118}, {0x3a000000, 8, 117}, {0x39000000, 8, 116}, {0x38000000, 8, 113}, {0x37000000, 8, 111}, {0x36000000, 8, 108}, {0x35000000, 8, 107}, {0x34000000, 8, 106}, {0x33000000, 8, 98}, {0x32000000, 8, 97}, {0x31000000, 8, 96}, {0x30000000, 8, 94}, {0x2f000000, 8, 90}, {0x2e000000, 8, 89}, {0x2d000000, 8, 85}, {0x2c000000, 8, 84}, {0x2b000000, 8, 80}, {0x2a000000, 8, 78}, {0x29000000, 8, 77}, {0x28000000, 8, 76}, {0x27000000, 8, 73}, {0x26000000, 8, 71}, {0x25000000, 8, 69}, {0x24000000, 8, 65}, {0x23000000, 8, 59}, {0x22000000, 8, 55}, {0x21000000, 8, 53}, {0x20000000, 8, 51}, {0x1f000000, 8, 48}, {0x1e000000, 8, 47}, {0x1d000000, 8, 46}, {0x1c000000, 8, 44}, {0x1b000000, 8, 40}, {0x1a000000, 8, 39}, {0x19000000, 8, 35}, {0x18000000, 8, 34}, {0x17000000, 8, 30}, {0x16000000, 8, 28}, {0x15000000, 8, 27}, {0x14000000, 8, 26}, {0x13000000, 8, 18}, {0x12000000, 8, 17}, {0x11000000, 8, 16}, {0x10000000, 8, 13}, {0x0f000000, 8, 11}, {0x0e000000, 8, 8}, {0x0d000000, 8, 7}, {0x0c000000, 8, 6}, {0x0b800000, 9, 122}, {0x0b000000, 9, 110}, {0x0a800000, 9, 74}, {0x0a000000, 9, 70}, {0x09800000, 9, 54}, {0x09000000, 9, 50}, {0x08800000, 9, 22}, {0x08000000, 9, 14}, {0x07800000, 9, 2}, {0x07400000, 10, 123}, {0x07000000, 10, 121}, {0x06c00000, 10, 119}, {0x06800000, 10, 115}, {0x06400000, 10, 114}, {0x06000000, 10, 109}, {0x05c00000, 10, 105}, {0x05800000, 10, 103}, {0x05400000, 10, 102}, {0x05000000, 10, 101}, {0x04c00000, 10, 99}, {0x04800000, 10, 95}, {0x04400000, 10, 79}, {0x04000000, 10, 75}, {0x03c00000, 10, 49}, {0x03800000, 10, 45}, {0x03400000, 10, 29}, {0x03000000, 10, 25}, {0x02c00000, 10, 23}, {0x02800000, 10, 21}, {0x02400000, 10, 19}, {0x02000000, 10, 15}, {0x01c00000, 10, 10}, {0x01800000, 10, 9}, {0x01400000, 10, 5}, {0x01000000, 10, 3}, {0x00c00000, 10, 1}, {0x00a00000, 11, 124}, {0x00800000, 11, 104}, {0x00600000, 11, 20}, {0x00400000, 11, 0}, {0x00300000, 12, 120}, {0x00200000, 12, 100}, {0x00100000, 12, 24}, {0x00000000, 12, 4}
+	}
+};
+
+
+static const mpc_huffman mpc_table_HuffQ3_8 [49] = {
+	{0xe0000000, 3, 56576}, {0xd0000000, 4, 56593}, {0xc0000000, 4, 56592}, {0xb0000000, 4, 56577}, {0xa0000000, 4, 56591}, {0x90000000, 4, 56816}, {0x80000000, 4, 56831}, {0x78000000, 5, 56608}, {0x70000000, 5, 56607}, {0x68000000, 5, 56578}, {0x60000000, 5, 56590}, {0x58000000, 5, 56817}, {0x50000000, 5, 56800}, {0x4c000000, 6, 56610}, {0x48000000, 6, 56609}, {0x44000000, 6, 56623}, {0x40000000, 6, 56622}, {0x3c000000, 6, 56594}, {0x38000000, 6, 56606}, {0x34000000, 6, 56818}, {0x30000000, 6, 56830}, {0x2c000000, 6, 56801}, {0x28000000, 6, 56815}, {0x24000000, 6, 56814}, {0x22000000, 7, 56625}, {0x20000000, 7, 56624}, {0x1e000000, 7, 56639}, {0x1c000000, 7, 56595}, {0x1a000000, 7, 56605}, {0x18000000, 7, 56579}, {0x16000000, 7, 56589}, {0x14000000, 7, 56819}, {0x12000000, 7, 56829}, {0x10000000, 7, 56802}, {0x0e000000, 7, 56785}, {0x0c000000, 7, 56784}, {0x0a000000, 7, 56799}, {0x09000000, 8, 56626}, {0x08000000, 8, 56638}, {0x07000000, 8, 56611}, {0x06000000, 8, 56621}, {0x05000000, 8, 56803}, {0x04000000, 8, 56813}, {0x03000000, 8, 56786}, {0x02000000, 8, 56798}, {0x01800000, 9, 56627}, {0x01000000, 9, 56637}, {0x00800000, 9, 56787}, {0x00000000, 9, 56797}
+};
+
+static const mpc_huffman mpc_table_HuffQ4_8 [91] = {
+	{0xf0000000, 4, 52224}, {0xe8000000, 5, 52256}, {0xe0000000, 5, 52241}, {0xd8000000, 5, 52240}, {0xd0000000, 5, 52255}, {0xc8000000, 5, 52226}, {0xc0000000, 5, 52225}, {0xb8000000, 5, 52239}, {0xb0000000, 5, 52238}, {0xa8000000, 5, 52465}, {0xa0000000, 5, 52464}, {0x98000000, 5, 52479}, {0x90000000, 5, 52448}, {0x8c000000, 6, 52273}, {0x88000000, 6, 52272}, {0x84000000, 6, 52258}, {0x80000000, 6, 52257}, {0x7c000000, 6, 52271}, {0x78000000, 6, 52270}, {0x74000000, 6, 52243}, {0x70000000, 6, 52242}, {0x6c000000, 6, 52254}, {0x68000000, 6, 52253}, {0x64000000, 6, 52227}, {0x60000000, 6, 52237}, {0x5c000000, 6, 52467}, {0x58000000, 6, 52466}, {0x54000000, 6, 52478}, {0x50000000, 6, 52477}, {0x4c000000, 6, 52450}, {0x48000000, 6, 52449}, {0x44000000, 6, 52463}, {0x40000000, 6, 52462}, {0x3c000000, 6, 52433}, {0x38000000, 6, 52432}, {0x34000000, 6, 52447}, {0x32000000, 7, 52288}, {0x30000000, 7, 52274}, {0x2e000000, 7, 52287}, {0x2c000000, 7, 52286}, {0x2a000000, 7, 52259}, {0x28000000, 7, 52269}, {0x26000000, 7, 52228}, {0x24000000, 7, 52236}, {0x22000000, 7, 52451}, {0x20000000, 7, 52461}, {0x1e000000, 7, 52434}, {0x1c000000, 7, 52446}, {0x1a000000, 7, 52416}, {0x18000000, 7, 52431}, {0x17000000, 8, 52290}, {0x16000000, 8, 52289}, {0x15000000, 8, 52303}, {0x14000000, 8, 52302}, {0x13000000, 8, 52275}, {0x12000000, 8, 52285}, {0x11000000, 8, 52260}, {0x10000000, 8, 52268}, {0x0f000000, 8, 52244}, {0x0e000000, 8, 52252}, {0x0d000000, 8, 52468}, {0x0c000000, 8, 52476}, {0x0b000000, 8, 52452}, {0x0a000000, 8, 52460}, {0x09000000, 8, 52435}, {0x08000000, 8, 52445}, {0x07000000, 8, 52418}, {0x06000000, 8, 52417}, {0x05000000, 8, 52430}, {0x04800000, 9, 52291}, {0x04000000, 9, 52301}, {0x03800000, 9, 52276}, {0x03000000, 9, 52284}, {0x02800000, 9, 52436}, {0x02000000, 9, 52444}, {0x01800000, 9, 52419}, {0x01000000, 9, 52429}, {0x00c00000, 10, 52292}, {0x00800000, 10, 52300}, {0x00400000, 10, 52420}, {0x00000000, 10, 52428}
+};
+
+static const mpc_huffman mpc_table_HuffQ5_8 [2] [15] = {
+	{
+		{0xc0000000, 2, 0}, {0xa0000000, 3, 2}, {0x80000000, 3, 1}, {0x60000000, 3, -1}, {0x40000000, 3, -2}, {0x30000000, 4, 3}, {0x20000000, 4, -3}, {0x18000000, 5, 4}, {0x10000000, 5, -4}, {0x0c000000, 6, 5}, {0x08000000, 6, -5}, {0x06000000, 7, 7}, {0x04000000, 7, 6}, {0x02000000, 7, -6}, {0x00000000, 7, -7}
+	}, {
+		{0xe0000000, 3, 2}, {0xc0000000, 3, 1}, {0xa0000000, 3, 0}, {0x80000000, 3, -1}, {0x60000000, 3, -2}, {0x50000000, 4, 4}, {0x40000000, 4, 3}, {0x30000000, 4, -3}, {0x20000000, 4, -4}, {0x18000000, 5, 5}, {0x10000000, 5, -5}, {0x0c000000, 6, 7}, {0x08000000, 6, 6}, {0x04000000, 6, -6}, {0x00000000, 6, -7}
+	}
+};
+
+static const mpc_huffman mpc_table_HuffQ6_8 [2] [31] = {
+	{
+		{0xc0000000, 2, 0}, {0xa0000000, 3, 1}, {0x80000000, 3, -1}, {0x70000000, 4, 3}, {0x60000000, 4, 2}, {0x50000000, 4, -2}, {0x40000000, 4, -3}, {0x38000000, 5, 4}, {0x30000000, 5, -4}, {0x28000000, 5, -5}, {0x24000000, 6, 8}, {0x20000000, 6, 7}, {0x1c000000, 6, 6}, {0x18000000, 6, 5}, {0x14000000, 6, -6}, {0x10000000, 6, -7}, {0x0c000000, 6, -8}, {0x0a000000, 7, 9}, {0x08000000, 7, -9}, {0x07000000, 8, 11}, {0x06000000, 8, 10}, {0x05000000, 8, -10}, {0x04000000, 8, -11}, {0x03800000, 9, 15}, {0x03000000, 9, 14}, {0x02800000, 9, 13}, {0x02000000, 9, 12}, {0x01800000, 9, -12}, {0x01000000, 9, -13}, {0x00800000, 9, -14}, {0x00000000, 9, -15}
+	}, {
+		{0xf0000000, 4, 5}, {0xe0000000, 4, 4}, {0xd0000000, 4, 3}, {0xc0000000, 4, 2}, {0xb0000000, 4, 1}, {0xa0000000, 4, 0}, {0x90000000, 4, -1}, {0x80000000, 4, -2}, {0x70000000, 4, -3}, {0x60000000, 4, -4}, {0x50000000, 4, -5}, {0x48000000, 5, 8}, {0x40000000, 5, 7}, {0x38000000, 5, 6}, {0x30000000, 5, -6}, {0x28000000, 5, -7}, {0x20000000, 5, -8}, {0x1c000000, 6, 10}, {0x18000000, 6, 9}, {0x14000000, 6, -9}, {0x10000000, 6, -10}, {0x0e000000, 7, 13}, {0x0c000000, 7, 12}, {0x0a000000, 7, 11}, {0x08000000, 7, -11}, {0x06000000, 7, -12}, {0x04000000, 7, -13}, {0x03000000, 8, 15}, {0x02000000, 8, 14}, {0x01000000, 8, -14}, {0x00000000, 8, -15}
+	}
+};
+
+static const mpc_huffman mpc_table_HuffQ7_8 [2] [63] = {
+	{
+		{0xc0000000, 2, 0}, {0xa0000000, 3, 1}, {0x80000000, 3, -1}, {0x70000000, 4, 2}, {0x60000000, 4, -2}, {0x58000000, 5, 4}, {0x50000000, 5, 3}, {0x48000000, 5, -3}, {0x40000000, 5, -4}, {0x3c000000, 6, 7}, {0x38000000, 6, 6}, {0x34000000, 6, 5}, {0x30000000, 6, -5}, {0x2c000000, 6, -6}, {0x28000000, 6, -7}, {0x26000000, 7, 13}, {0x24000000, 7, 11}, {0x22000000, 7, 10}, {0x20000000, 7, 9}, {0x1e000000, 7, 8}, {0x1c000000, 7, -8}, {0x1a000000, 7, -9}, {0x18000000, 7, -10}, {0x16000000, 7, -11}, {0x14000000, 7, -12}, {0x13000000, 8, 17}, {0x12000000, 8, 16}, {0x11000000, 8, 15}, {0x10000000, 8, 14}, {0x0f000000, 8, 12}, {0x0e000000, 8, -13}, {0x0d000000, 8, -14}, {0x0c000000, 8, -15}, {0x0b000000, 8, -16}, {0x0a000000, 8, -17}, {0x09800000, 9, 28}, {0x09000000, 9, 27}, {0x08800000, 9, 21}, {0x08000000, 9, 20}, {0x07800000, 9, 19}, {0x07000000, 9, 18}, {0x06800000, 9, -18}, {0x06000000, 9, -19}, {0x05800000, 9, -20}, {0x05000000, 9, -21}, {0x04800000, 9, -27}, {0x04000000, 9, -28}, {0x03c00000, 10, 31}, {0x03800000, 10, 30}, {0x03400000, 10, 29}, {0x03000000, 10, 26}, {0x02c00000, 10, 25}, {0x02800000, 10, 24}, {0x02400000, 10, 23}, {0x02000000, 10, 22}, {0x01c00000, 10, -22}, {0x01800000, 10, -23}, {0x01400000, 10, -24}, {0x01000000, 10, -25}, {0x00c00000, 10, -26}, {0x00800000, 10, -29}, {0x00400000, 10, -30}, {0x00000000, 10, -31}
+	}, {
+		{0xf8000000, 5, 10}, {0xf0000000, 5, 9}, {0xe8000000, 5, 8}, {0xe0000000, 5, 7}, {0xd8000000, 5, 6}, {0xd0000000, 5, 5}, {0xc8000000, 5, 4}, {0xc0000000, 5, 3}, {0xb8000000, 5, 2}, {0xb0000000, 5, 1}, {0xa8000000, 5, 0}, {0xa0000000, 5, -1}, {0x98000000, 5, -2}, {0x90000000, 5, -3}, {0x88000000, 5, -4}, {0x80000000, 5, -5}, {0x78000000, 5, -6}, {0x70000000, 5, -7}, {0x68000000, 5, -8}, {0x60000000, 5, -9}, {0x5c000000, 6, 17}, {0x58000000, 6, 16}, {0x54000000, 6, 15}, {0x50000000, 6, 14}, {0x4c000000, 6, 13}, {0x48000000, 6, 12}, {0x44000000, 6, 11}, {0x40000000, 6, -10}, {0x3c000000, 6, -11}, {0x38000000, 6, -12}, {0x34000000, 6, -13}, {0x30000000, 6, -14}, {0x2c000000, 6, -15}, {0x28000000, 6, -16}, {0x24000000, 6, -17}, {0x22000000, 7, 22}, {0x20000000, 7, 21}, {0x1e000000, 7, 20}, {0x1c000000, 7, 19}, {0x1a000000, 7, 18}, {0x18000000, 7, -18}, {0x16000000, 7, -19}, {0x14000000, 7, -20}, {0x12000000, 7, -21}, {0x10000000, 7, -22}, {0x0f000000, 8, 29}, {0x0e000000, 8, 28}, {0x0d000000, 8, 27}, {0x0c000000, 8, 26}, {0x0b000000, 8, 25}, {0x0a000000, 8, 24}, {0x09000000, 8, 23}, {0x08000000, 8, -23}, {0x07000000, 8, -24}, {0x06000000, 8, -25}, {0x05000000, 8, -26}, {0x04000000, 8, -27}, {0x03000000, 8, -28}, {0x02000000, 8, -29}, {0x01800000, 9, 31}, {0x01000000, 9, 30}, {0x00800000, 9, -30}, {0x00000000, 9, -31}
+	}
+};
+
+static const mpc_huffman mpc_table_HuffQ8_8 [2] [127] = {
+	{
+		{0xc0000000, 2, 0}, {0xa0000000, 3, 1}, {0x80000000, 3, -1}, {0x70000000, 4, -2}, {0x68000000, 5, 3}, {0x60000000, 5, 2}, {0x58000000, 5, -3}, {0x54000000, 6, 7}, {0x50000000, 6, 6}, {0x4c000000, 6, 5}, {0x48000000, 6, 4}, {0x44000000, 6, -4}, {0x40000000, 6, -5}, {0x3c000000, 6, -6}, {0x38000000, 6, -7}, {0x36000000, 7, 11}, {0x34000000, 7, 10}, {0x32000000, 7, 9}, {0x30000000, 7, 8}, {0x2e000000, 7, -8}, {0x2c000000, 7, -9}, {0x2a000000, 7, -10}, {0x28000000, 7, -11}, {0x27000000, 8, 19}, {0x26000000, 8, 18}, {0x25000000, 8, 17}, {0x24000000, 8, 16}, {0x23000000, 8, 15}, {0x22000000, 8, 14}, {0x21000000, 8, 13}, {0x20000000, 8, 12}, {0x1f000000, 8, -12}, {0x1e000000, 8, -13}, {0x1d000000, 8, -14}, {0x1c000000, 8, -15}, {0x1b000000, 8, -16}, {0x1a000000, 8, -17}, {0x19000000, 8, -19}, {0x18800000, 9, 56}, {0x18000000, 9, 55}, {0x17800000, 9, 31}, {0x17000000, 9, 28}, {0x16800000, 9, 27}, {0x16000000, 9, 26}, {0x15800000, 9, 25}, {0x15000000, 9, 24}, {0x14800000, 9, 23}, {0x14000000, 9, 22}, {0x13800000, 9, 21}, {0x13000000, 9, 20}, {0x12800000, 9, -18}, {0x12000000, 9, -20}, {0x11800000, 9, -21}, {0x11000000, 9, -22}, {0x10800000, 9, -23}, {0x10000000, 9, -24}, {0x0f800000, 9, -25}, {0x0f000000, 9, -26}, {0x0e800000, 9, -27}, {0x0e000000, 9, -33}, {0x0d800000, 9, -54}, {0x0d000000, 9, -56}, {0x0cc00000, 10, 63}, {0x0c800000, 10, 62}, {0x0c400000, 10, 61}, {0x0c000000, 10, 60}, {0x0bc00000, 10, 59}, {0x0b800000, 10, 58}, {0x0b400000, 10, 57}, {0x0b000000, 10, 54}, {0x0ac00000, 10, 53}, {0x0a800000, 10, 43}, {0x0a400000, 10, 40}, {0x0a000000, 10, 39}, {0x09c00000, 10, 38}, {0x09800000, 10, 37}, {0x09400000, 10, 36}, {0x09000000, 10, 35}, {0x08c00000, 10, 34}, {0x08800000, 10, 33}, {0x08400000, 10, 32}, {0x08000000, 10, 30}, {0x07c00000, 10, 29}, {0x07800000, 10, -28}, {0x07400000, 10, -29}, {0x07000000, 10, -30}, {0x06c00000, 10, -31}, {0x06800000, 10, -32}, {0x06400000, 10, -34}, {0x06000000, 10, -35}, {0x05c00000, 10, -36}, {0x05800000, 10, -37}, {0x05400000, 10, -38}, {0x05000000, 10, -39}, {0x04c00000, 10, -40}, {0x04800000, 10, -41}, {0x04400000, 10, -43}, {0x04000000, 10, -53}, {0x03c00000, 10, -55}, {0x03800000, 10, -57}, {0x03400000, 10, -58}, {0x03000000, 10, -59}, {0x02c00000, 10, -60}, {0x02800000, 10, -61}, {0x02600000, 11, 49}, {0x02400000, 11, 47}, {0x02200000, 11, 46}, {0x02000000, 11, 45}, {0x01e00000, 11, 44}, {0x01c00000, 11, 42}, {0x01a00000, 11, 41}, {0x01800000, 11, -42}, {0x01600000, 11, -44}, {0x01400000, 11, -45}, {0x01200000, 11, -46}, {0x01000000, 11, -47}, {0x00e00000, 11, -48}, {0x00c00000, 11, -49}, {0x00a00000, 11, -50}, {0x00800000, 11, -62}, {0x00600000, 11, -63}, {0x00500000, 12, 52}, {0x00400000, 12, 51}, {0x00300000, 12, 50}, {0x00200000, 12, 48}, {0x00100000, 12, -51}, {0x00000000, 12, -52}
+	}, {
+		{0xfc000000, 6, 13}, {0xf8000000, 6, 11}, {0xf4000000, 6, 10}, {0xf0000000, 6, 9}, {0xec000000, 6, 8}, {0xe8000000, 6, 7}, {0xe4000000, 6, 6}, {0xe0000000, 6, 5}, {0xdc000000, 6, 4}, {0xd8000000, 6, 3}, {0xd4000000, 6, 2}, {0xd0000000, 6, 1}, {0xcc000000, 6, 0}, {0xc8000000, 6, -1}, {0xc4000000, 6, -2}, {0xc0000000, 6, -3}, {0xbc000000, 6, -4}, {0xb8000000, 6, -5}, {0xb4000000, 6, -6}, {0xb0000000, 6, -7}, {0xac000000, 6, -8}, {0xa8000000, 6, -9}, {0xa4000000, 6, -10}, {0xa0000000, 6, -11}, {0x9c000000, 6, -12}, {0x98000000, 6, -13}, {0x96000000, 7, 39}, {0x94000000, 7, 38}, {0x92000000, 7, 37}, {0x90000000, 7, 36}, {0x8e000000, 7, 35}, {0x8c000000, 7, 34}, {0x8a000000, 7, 33}, {0x88000000, 7, 32}, {0x86000000, 7, 31}, {0x84000000, 7, 30}, {0x82000000, 7, 29}, {0x80000000, 7, 28}, {0x7e000000, 7, 27}, {0x7c000000, 7, 26}, {0x7a000000, 7, 25}, {0x78000000, 7, 24}, {0x76000000, 7, 23}, {0x74000000, 7, 22}, {0x72000000, 7, 21}, {0x70000000, 7, 20}, {0x6e000000, 7, 19}, {0x6c000000, 7, 18}, {0x6a000000, 7, 17}, {0x68000000, 7, 16}, {0x66000000, 7, 15}, {0x64000000, 7, 14}, {0x62000000, 7, 12}, {0x60000000, 7, -14}, {0x5e000000, 7, -15}, {0x5c000000, 7, -16}, {0x5a000000, 7, -17}, {0x58000000, 7, -18}, {0x56000000, 7, -19}, {0x54000000, 7, -20}, {0x52000000, 7, -21}, {0x50000000, 7, -22}, {0x4e000000, 7, -23}, {0x4c000000, 7, -24}, {0x4a000000, 7, -25}, {0x48000000, 7, -26}, {0x46000000, 7, -27}, {0x44000000, 7, -28}, {0x42000000, 7, -29}, {0x40000000, 7, -30}, {0x3e000000, 7, -31}, {0x3c000000, 7, -32}, {0x3a000000, 7, -33}, {0x38000000, 7, -34}, {0x36000000, 7, -35}, {0x34000000, 7, -36}, {0x32000000, 7, -37}, {0x30000000, 7, -38}, {0x2e000000, 7, -39}, {0x2c000000, 7, -40}, {0x2a000000, 7, -41}, {0x29000000, 8, 59}, {0x28000000, 8, 58}, {0x27000000, 8, 57}, {0x26000000, 8, 56}, {0x25000000, 8, 55}, {0x24000000, 8, 54}, {0x23000000, 8, 53}, {0x22000000, 8, 52}, {0x21000000, 8, 51}, {0x20000000, 8, 50}, {0x1f000000, 8, 49}, {0x1e000000, 8, 48}, {0x1d000000, 8, 47}, {0x1c000000, 8, 46}, {0x1b000000, 8, 45}, {0x1a000000, 8, 44}, {0x19000000, 8, 43}, {0x18000000, 8, 42}, {0x17000000, 8, 41}, {0x16000000, 8, 40}, {0x15000000, 8, -42}, {0x14000000, 8, -43}, {0x13000000, 8, -44}, {0x12000000, 8, -45}, {0x11000000, 8, -46}, {0x10000000, 8, -47}, {0x0f000000, 8, -48}, {0x0e000000, 8, -49}, {0x0d000000, 8, -50}, {0x0c000000, 8, -51}, {0x0b000000, 8, -52}, {0x0a000000, 8, -53}, {0x09000000, 8, -54}, {0x08000000, 8, -55}, {0x07000000, 8, -56}, {0x06000000, 8, -57}, {0x05000000, 8, -58}, {0x04000000, 8, -59}, {0x03800000, 9, 63}, {0x03000000, 9, 62}, {0x02800000, 9, 61}, {0x02000000, 9, 60}, {0x01800000, 9, -60}, {0x01000000, 9, -61}, {0x00800000, 9, -62}, {0x00000000, 9, -63}
+	}
+};
+
+const mpc_huffman mpc_table_HuffQ9up_8 [256] ={
+	{0xfc000000, 6, 128}, {0xf8000000, 6, 127}, {0xf6000000, 7, 148}, {0xf4000000, 7, 146}, {0xf2000000, 7, 145}, {0xf0000000, 7, 144}, {0xee000000, 7, 143}, {0xec000000, 7, 142}, {0xea000000, 7, 141}, {0xe8000000, 7, 140}, {0xe6000000, 7, 139}, {0xe4000000, 7, 138}, {0xe2000000, 7, 137}, {0xe0000000, 7, 136}, {0xde000000, 7, 135}, {0xdc000000, 7, 134}, {0xda000000, 7, 133}, {0xd8000000, 7, 132}, {0xd6000000, 7, 131}, {0xd4000000, 7, 130}, {0xd2000000, 7, 129}, {0xd0000000, 7, 126}, {0xce000000, 7, 125}, {0xcc000000, 7, 124}, {0xca000000, 7, 123}, {0xc8000000, 7, 122}, {0xc6000000, 7, 121}, {0xc4000000, 7, 120}, {0xc2000000, 7, 119}, {0xc0000000, 7, 118}, {0xbe000000, 7, 117}, {0xbc000000, 7, 116}, {0xba000000, 7, 115}, {0xb8000000, 7, 114}, {0xb6000000, 7, 113}, {0xb4000000, 7, 112}, {0xb2000000, 7, 111}, {0xb0000000, 7, 110}, {0xae000000, 7, 109}, {0xac000000, 7, 108}, {0xab000000, 8, 212}, {0xaa000000, 8, 211}, {0xa9000000, 8, 210}, {0xa8000000, 8, 209}, {0xa7000000, 8, 208}, {0xa6000000, 8, 207}, {0xa5000000, 8, 206}, {0xa4000000, 8, 205}, {0xa3000000, 8, 204}, {0xa2000000, 8, 203}, {0xa1000000, 8, 202}, {0xa0000000, 8, 201}, {0x9f000000, 8, 200}, {0x9e000000, 8, 199}, {0x9d000000, 8, 198}, {0x9c000000, 8, 197}, {0x9b000000, 8, 196}, {0x9a000000, 8, 195}, {0x99000000, 8, 194}, {0x98000000, 8, 193}, {0x97000000, 8, 192}, {0x96000000, 8, 191}, {0x95000000, 8, 190}, {0x94000000, 8, 189}, {0x93000000, 8, 188}, {0x92000000, 8, 187}, {0x91000000, 8, 186}, {0x90000000, 8, 185}, {0x8f000000, 8, 184}, {0x8e000000, 8, 183}, {0x8d000000, 8, 182}, {0x8c000000, 8, 181}, {0x8b000000, 8, 180}, {0x8a000000, 8, 179}, {0x89000000, 8, 178}, {0x88000000, 8, 177}, {0x87000000, 8, 176}, {0x86000000, 8, 175}, {0x85000000, 8, 174}, {0x84000000, 8, 173}, {0x83000000, 8, 172}, {0x82000000, 8, 171}, {0x81000000, 8, 170}, {0x80000000, 8, 169}, {0x7f000000, 8, 168}, {0x7e000000, 8, 167}, {0x7d000000, 8, 166}, {0x7c000000, 8, 165}, {0x7b000000, 8, 164}, {0x7a000000, 8, 163}, {0x79000000, 8, 162}, {0x78000000, 8, 161}, {0x77000000, 8, 160}, {0x76000000, 8, 159}, {0x75000000, 8, 158}, {0x74000000, 8, 157}, {0x73000000, 8, 156}, {0x72000000, 8, 155}, {0x71000000, 8, 154}, {0x70000000, 8, 153}, {0x6f000000, 8, 152}, {0x6e000000, 8, 151}, {0x6d000000, 8, 150}, {0x6c000000, 8, 149}, {0x6b000000, 8, 147}, {0x6a000000, 8, 107}, {0x69000000, 8, 106}, {0x68000000, 8, 105}, {0x67000000, 8, 104}, {0x66000000, 8, 103}, {0x65000000, 8, 102}, {0x64000000, 8, 101}, {0x63000000, 8, 100}, {0x62000000, 8, 99}, {0x61000000, 8, 98}, {0x60000000, 8, 97}, {0x5f000000, 8, 96}, {0x5e000000, 8, 95}, {0x5d000000, 8, 94}, {0x5c000000, 8, 93}, {0x5b000000, 8, 92}, {0x5a000000, 8, 91}, {0x59000000, 8, 90}, {0x58000000, 8, 89}, {0x57000000, 8, 88}, {0x56000000, 8, 87}, {0x55000000, 8, 86}, {0x54000000, 8, 85}, {0x53000000, 8, 84}, {0x52000000, 8, 83}, {0x51000000, 8, 82}, {0x50000000, 8, 81}, {0x4f000000, 8, 80}, {0x4e000000, 8, 79}, {0x4d000000, 8, 78}, {0x4c000000, 8, 77}, {0x4b000000, 8, 76}, {0x4a000000, 8, 75}, {0x49000000, 8, 74}, {0x48000000, 8, 73}, {0x47000000, 8, 72}, {0x46000000, 8, 71}, {0x45000000, 8, 70}, {0x44000000, 8, 69}, {0x43000000, 8, 68}, {0x42000000, 8, 67}, {0x41000000, 8, 66}, {0x40000000, 8, 65}, {0x3f000000, 8, 64}, {0x3e000000, 8, 63}, {0x3d000000, 8, 62}, {0x3c000000, 8, 61}, {0x3b000000, 8, 60}, {0x3a000000, 8, 59}, {0x39000000, 8, 58}, {0x38000000, 8, 57}, {0x37000000, 8, 56}, {0x36000000, 8, 55}, {0x35000000, 8, 54}, {0x34000000, 8, 53}, {0x33000000, 8, 52}, {0x32000000, 8, 51}, {0x31000000, 8, 50}, {0x30000000, 8, 49}, {0x2f000000, 8, 48}, {0x2e000000, 8, 47}, {0x2d000000, 8, 46}, {0x2c000000, 8, 45}, {0x2b000000, 8, 44}, {0x2a000000, 8, 43}, {0x29000000, 8, 42}, {0x28000000, 8, 40}, {0x27000000, 8, 20}, {0x26000000, 8, 19}, {0x25800000, 9, 249}, {0x25000000, 9, 248}, {0x24800000, 9, 247}, {0x24000000, 9, 246}, {0x23800000, 9, 245}, {0x23000000, 9, 244}, {0x22800000, 9, 243}, {0x22000000, 9, 242}, {0x21800000, 9, 241}, {0x21000000, 9, 240}, {0x20800000, 9, 239}, {0x20000000, 9, 238}, {0x1f800000, 9, 237}, {0x1f000000, 9, 236}, {0x1e800000, 9, 235}, {0x1e000000, 9, 234}, {0x1d800000, 9, 233}, {0x1d000000, 9, 232}, {0x1c800000, 9, 231}, {0x1c000000, 9, 230}, {0x1b800000, 9, 229}, {0x1b000000, 9, 228}, {0x1a800000, 9, 227}, {0x1a000000, 9, 226}, {0x19800000, 9, 225}, {0x19000000, 9, 224}, {0x18800000, 9, 223}, {0x18000000, 9, 222}, {0x17800000, 9, 221}, {0x17000000, 9, 220}, {0x16800000, 9, 219}, {0x16000000, 9, 218}, {0x15800000, 9, 217}, {0x15000000, 9, 216}, {0x14800000, 9, 215}, {0x14000000, 9, 214}, {0x13800000, 9, 213}, {0x13000000, 9, 41}, {0x12800000, 9, 39}, {0x12000000, 9, 38}, {0x11800000, 9, 37}, {0x11000000, 9, 36}, {0x10800000, 9, 35}, {0x10000000, 9, 34}, {0x0f800000, 9, 33}, {0x0f000000, 9, 32}, {0x0e800000, 9, 31}, {0x0e000000, 9, 30}, {0x0d800000, 9, 29}, {0x0d000000, 9, 28}, {0x0c800000, 9, 27}, {0x0c000000, 9, 26}, {0x0b800000, 9, 25}, {0x0b000000, 9, 24}, {0x0a800000, 9, 23}, {0x0a000000, 9, 22}, {0x09800000, 9, 21}, {0x09000000, 9, 18}, {0x08800000, 9, 17}, {0x08000000, 9, 16}, {0x07800000, 9, 15}, {0x07000000, 9, 14}, {0x06800000, 9, 13}, {0x06000000, 9, 12}, {0x05800000, 9, 11}, {0x05000000, 9, 10}, {0x04800000, 9, 9}, {0x04000000, 9, 8}, {0x03800000, 9, 7}, {0x03000000, 9, 6}, {0x02800000, 9, 5}, {0x02400000, 10, 253}, {0x02000000, 10, 252}, {0x01c00000, 10, 251}, {0x01800000, 10, 250}, {0x01400000, 10, 4}, {0x01000000, 10, 3}, {0x00c00000, 10, 2}, {0x00800000, 10, 1}, {0x00400000, 10, 0}, {0x00200000, 11, 255}, {0x00000000, 11, 254}
+};
+
+const mpc_huffman* mpc_table_HuffQ_8 [2] [8] = {
+	{mpc_table_HuffQ1_8,mpc_table_HuffQ2_8[0],mpc_table_HuffQ3_8,mpc_table_HuffQ4_8,mpc_table_HuffQ5_8[0],mpc_table_HuffQ6_8[0],mpc_table_HuffQ7_8[0], mpc_table_HuffQ8_8[0]},
+	{mpc_table_HuffQ1_8,mpc_table_HuffQ2_8[1],mpc_table_HuffQ3_8,mpc_table_HuffQ4_8,mpc_table_HuffQ5_8[1],mpc_table_HuffQ6_8[1],mpc_table_HuffQ7_8[1], mpc_table_HuffQ8_8[1]}
+};
Index: /libmpc/branches/r2d/libmpcdec/mpc_bits_reader.c
===================================================================
--- /libmpc/branches/r2d/libmpcdec/mpc_bits_reader.c	(revision 160)
+++ /libmpc/branches/r2d/libmpcdec/mpc_bits_reader.c	(revision 161)
@@ -37,4 +37,73 @@
 #include "huffman.h"
 
+#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};
+
 mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits)
 {
@@ -97,4 +166,38 @@
 }
 
+mpc_uint32_t mpc_bits_enum_dec(mpc_bits_reader * r, mpc_uint_t k, mpc_uint_t n)
+{
+	mpc_uint32_t bits = 0;
+	mpc_uint32_t code;
+	const mpc_uint32_t * C = Cnk[k-1];
+
+	code = mpc_bits_read(r, Cnk_len[k-1][n-1] - 1);
+
+	if (code >= Cnk_lost[k-1][n-1])
+		code = ((code << 1) | mpc_bits_read(r, 1)) - Cnk_lost[k-1][n-1];
+
+	do {
+		n--;
+		if (code >= C[n]) {
+			bits |= 1 << n;
+			code -= C[n];
+			C -= MAX_ENUM;
+			k--;
+		}
+	} while(k > 0);
+
+	return bits;
+}
+
+mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max)
+{
+	mpc_uint32_t value = 0;
+	if (log2[max - 1] > 1)
+		value = mpc_bits_read(r, log2[max - 1] - 1);
+	if (value >= log2_lost[max - 1])
+		value = ((value << 1) | mpc_bits_read(r, 1)) - log2_lost[max - 1];
+	return value;
+}
+
 unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size)
 {
Index: /libmpc/branches/r2d/libmpcdec/mpc_decoder.c
===================================================================
--- /libmpc/branches/r2d/libmpcdec/mpc_decoder.c	(revision 160)
+++ /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));
+	}
+}
+
Index: /libmpc/branches/r2d/libmpcdec/mpc_demux.c
===================================================================
--- /libmpc/branches/r2d/libmpcdec/mpc_demux.c	(revision 160)
+++ /libmpc/branches/r2d/libmpcdec/mpc_demux.c	(revision 161)
@@ -312,6 +312,7 @@
 	if (d->si.stream_version >= 8) {
 		mpc_bits_reader r;
+		i->is_key_frame = MPC_FALSE;
 		if (d->block_bits < 8 && d->end == MPC_FALSE) {
-			mpc_block b;
+			mpc_block b = {{0,0},0};
 			d->bits_reader.count &= -8;
 			if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
@@ -331,4 +332,5 @@
 			}
 			d->block_bits = (mpc_uint32_t) b.size * 8;
+			i->is_key_frame = MPC_TRUE;
 		}
 		// FIXME : this is not good if block size > buffer size
Index: /libmpc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- /libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 160)
+++ /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 160)
+++ /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 160)
+++ /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 160)
+++ /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;
 
Index: /libmpc/branches/r2d/mppenc/mppenc.c
===================================================================
--- /libmpc/branches/r2d/mppenc/mppenc.c	(revision 160)
+++ /libmpc/branches/r2d/mppenc/mppenc.c	(revision 161)
@@ -546,12 +546,12 @@
         if (tmp_R [2] > 0.) scfR [2] = IFLOORF (-12.6f * LOG10 (tmp_R [2]) + 57.8945021823f );
 
-        // restriction to SCF_Index = 0...63, make note of the internal overflow
+        // restriction to SCF_Index = -6...121, make note of the internal overflow
         warnL = warnR = 0;
-        if (scfL[0] & ~63) { if (scfL[0] < 0) { if (XLevel==0) scfL[0] = 0, warnL = 1; } else scfL[0] = 63; }
-        if (scfL[1] & ~63) { if (scfL[1] < 0) { if (XLevel==0) scfL[1] = 0, warnL = 1; } else scfL[1] = 63; }
-        if (scfL[2] & ~63) { if (scfL[2] < 0) { if (XLevel==0) scfL[2] = 0, warnL = 1; } else scfL[2] = 63; }
-        if (scfR[0] & ~63) { if (scfR[0] < 0) { if (XLevel==0) scfR[0] = 0, warnR = 1; } else scfR[0] = 63; }
-        if (scfR[1] & ~63) { if (scfR[1] < 0) { if (XLevel==0) scfR[1] = 0, warnR = 1; } else scfR[1] = 63; }
-        if (scfR[2] & ~63) { if (scfR[2] < 0) { if (XLevel==0) scfR[2] = 0, warnR = 1; } else scfR[2] = 63; }
+		for( n = 0; n < 3; n++){
+			if (scfL[n] < -6) scfL[n] = -6, warnL = 1;
+			if (scfL[n] > 121) scfL[n] = 121, warnL = 1;
+			if (scfR[n] < -6) scfR[n] = -6, warnR = 1;
+			if (scfR[n] > 121) scfR[n] = 121, warnR = 1;
+		}
 
         // save old values for compensation calculation
@@ -1462,6 +1462,4 @@
     cw  &=  ~0x300;
 	_asm { fldcw cw };
-#else
-    ;
 #endif
 }
@@ -1718,5 +1716,5 @@
 
         OldSilence      = Silence;
-        WriteBitstream_SV7 ( &e, m.Max_Band, Q );                                                // write SV7-Bitstream
+        writeBitstream_SV8 ( &e, m.Max_Band, Q );                                                // write SV7-Bitstream
 
         if ( (Int)(time (NULL) - T) >= 0 ) {                            // output
Index: /libmpc/branches/r2d/mppenc/mppenc.h
===================================================================
--- /libmpc/branches/r2d/mppenc/mppenc.h	(revision 160)
+++ /libmpc/branches/r2d/mppenc/mppenc.h	(revision 161)
@@ -123,5 +123,5 @@
 void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits );
 void writeSeekTable (mpc_encoder_t * e);
-void         WriteBitstream_SV7   ( mpc_encoder_t*, const int, const SubbandQuantTyp* );
+void writeBitstream_SV8 ( mpc_encoder_t*, int, const SubbandQuantTyp*);
 
 
