Index: libmpc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 150)
+++ 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 )
