Index: mppenc/branches/r2d/libmpcenc/Makefile.am
===================================================================
--- mppenc/branches/r2d/libmpcenc/Makefile.am	(revision 77)
+++ mppenc/branches/r2d/libmpcenc/Makefile.am	(revision 78)
@@ -2,5 +2,6 @@
 METASOURCES = AUTO
 lib_LIBRARIES = libmpcenc.a
-libmpcenc_a_SOURCES = analy_filter.c bitstream.c encode_sv7.c huffsv7.c quant.c
+libmpcenc_a_SOURCES = analy_filter.c bitstream.c encode_sv7.c huffsv7.c quant.c \
+	crc32.c
 
 
Index: mppenc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/bitstream.c	(revision 77)
+++ mppenc/branches/r2d/libmpcenc/bitstream.c	(revision 78)
@@ -23,162 +23,100 @@
 #include "stdio.h"
 
+unsigned long crc32(unsigned char *buf, int len);
 
-/*
- *  Change_Endian32() changes the endianess of a 32-bit memory block in-place
- *  by swapping the byte order. This is a little bit tricky, but a well
- *  known method which is much much faster, especially on modern CPUs, than
- *  byte picking, because it avoids memory aliasing. Note that this method
- *  is poison for old 16-bit compilers!
- */
-
-#if ENDIAN == HAVE_BIG_ENDIAN
-
-static void
-Change_Endian32 ( unsigned int* dst, mpc_size_t words32bit )
+void emptyBits(mpc_encoder_t * e)
 {
-    for ( ; words32bit--; dst++ ) {
-# if  INT_MAX >= 2147483647L
-        unsigned int  tmp = *dst;
-        tmp  = ((tmp << 0x10) & 0xFFFF0000) | ((tmp >> 0x10) & 0x0000FFFF);
-        tmp  = ((tmp << 0x08) & 0xFF00FF00) | ((tmp >> 0x08) & 0x00FF00FF);
-        *dst = tmp;
-# else
-        char  tmp;
-        tmp             = ((char*)dst)[0];
-        ((char*)dst)[0] = ((char*)dst)[3];
-        ((char*)dst)[3] = tmp;
-        tmp             = ((char*)dst)[1];
-        ((char*)dst)[1] = ((char*)dst)[2];
-        ((char*)dst)[2] = tmp;
-# endif
-    }
-    return;
+	while( e->bitsCount >= 8 ){
+		e->bitsCount -= 8;
+		e->buffer[e->pos] = (mpc_uint8_t) (e->bitsBuff >> e->bitsCount);
+		e->pos++;
+	}
 }
 
-#endif /* ENDIAN == HAVE_BIG_ENDIAN */
+void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits )
+{
+	e->outputBits += bits;
+
+	if (e->bitsCount + bits > sizeof(e->bitsBuff) * 8) {
+		int tmp = (sizeof(e->bitsBuff) * 8 - e->bitsCount);
+		bits -= tmp;
+		e->bitsBuff = (e->bitsBuff << tmp) | (input >> bits);
+		e->bitsCount = sizeof(e->bitsBuff) * 8;
+		emptyBits(e);
+		input &= (1 << bits) - 1;
+	}
+	e->bitsBuff = (e->bitsBuff << bits) | input;
+	e->bitsCount += bits;
+}
+
+unsigned int encodeSize(mpc_uint64_t size, char * buff, mpc_bool_t addCodeSize)
+{
+	unsigned int i = 1;
+	int j;
+
+	if (addCodeSize) {
+		while ((1 << (7 * i)) - i <= size) i++;
+		size += i;
+	} else
+		while ((1 << (7 * i)) <= size) i++;
+
+	for( j = i - 1; j >= 0; j--){
+		buff[j] = (char) (size | 0x80);
+		size >>= 7;
+	}
+	buff[i - 1] &= 0x7F;
+
+	return i;
+}
+
+void writeMagic(mpc_encoder_t * e)
+{
+	fwrite("MPCK", sizeof(char), 4, e->outputFile);
+	e->outputBits += 32;
+	e->framesInBlock = 0;
+}
+
+void writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC)
+{
+	FILE * fp = e->outputFile;
+	mpc_uint32_t written = 0;
+	mpc_uint8_t * datas = e->buffer;
+	char blockSize[10];
+	mpc_uint_t len;
+
+	writeBits(e, 0, (8 - e->bitsCount) % 8);
+	emptyBits(e);
+
+	// write block header (key / length)
+	len = encodeSize(e->pos + 2, blockSize, TRUE);
+	fwrite(key, sizeof(char), 2, fp);
+	fwrite(blockSize, sizeof(char), len, fp);
+	e->outputBits += (len + 2) * 8;
 
 
-void
-FlushBitstream ( FILE* fp, const mpc_uint32_t* buffer, mpc_size_t words32bit )
-{
-    mpc_size_t           WrittenDwords = 0;
-    const mpc_uint32_t*  p             = buffer;
-#if ENDIAN == HAVE_BIG_ENDIAN
-    mpc_size_t           CC            = words32bit;
-#endif
+	if (addCRC) {
+		char tmp[4];
+		unsigned long CRC32 = crc32((unsigned char *) e->buffer, e->pos);
+		tmp[0] = (char) (CRC32 >> 24);
+		tmp[1] = (char) (CRC32 >> 16);
+		tmp[2] = (char) (CRC32 >> 8);
+		tmp[3] = (char) CRC32;
+		fwrite(tmp, sizeof(char), 4, fp);
+		e->outputBits += 32;
+	}
 
-#if ENDIAN == HAVE_BIG_ENDIAN
-    Change_Endian32 ( (mpc_uint32_t*)buffer, CC );
-#endif
-
-    // Write e->Buffer
-    do {
-        WrittenDwords = fwrite ( p, sizeof(*buffer), words32bit, fp );
-        if ( WrittenDwords == 0 ) {
-			// FIXME : move stderr_printf to common
-//             stderr_printf ( "\b\n WARNING: Disk full?, retry after 10 sec ...\a" );
+	// write datas
+	while ( e->pos != 0 ) {
+		written = fwrite ( datas, sizeof(*e->buffer), e->pos, fp );
+		if ( written == 0 ) {
 			sprintf(stderr, "\b\n WARNING: Disk full?, retry after 10 sec ...\a");
             sleep (10);
         }
-        if ( WrittenDwords > 0 ) {
-            p          += WrittenDwords;
-            words32bit -= WrittenDwords;
+		if ( written > 0 ) {
+			datas += written;
+			e->pos -= written;
         }
-    } while ( words32bit != 0 );
-
-#if ENDIAN == HAVE_BIG_ENDIAN
-    Change_Endian32 ( (mpc_uint32_t*)buffer, CC );
-#endif
-}
-
-
-void
-UpdateHeader ( FILE* fp, mpc_uint32_t Frames, mpc_uint_t ValidSamples )
-{
-    mpc_uint8_t  buff [4];
-
-    // Write framecount to header
-    if ( fseek ( fp, 4L, SEEK_SET ) < 0 )
-        return;
-
-    buff [0] = (mpc_uint8_t)(Frames >>  0);
-    buff [1] = (mpc_uint8_t)(Frames >>  8);
-    buff [2] = (mpc_uint8_t)(Frames >> 16);
-    buff [3] = (mpc_uint8_t)(Frames >> 24);
-
-    fwrite ( buff, 1, 4, fp );
-
-    // Write ValidSamples to header
-    if ( fseek ( fp, 22L, SEEK_SET ) < 0 )
-        return;
-    fread ( buff, 1, 2, fp );
-    if ( ferror(fp) )
-        return;
-    if ( fseek ( fp, 22L, SEEK_SET ) < 0 )
-        return;
-
-    ValidSamples <<= 4;
-    ValidSamples  |= 0x800F & (((mpc_uint_t) buff[1] << 8) | buff[0]);
-    buff [0] = (mpc_uint8_t)(ValidSamples >>  0);
-    buff [1] = (mpc_uint8_t)(ValidSamples >>  8);
-
-    fwrite ( buff, 1, 2, fp );
-
-
-    // Set filepointer to end of file (dirty method, should be old position!!)
-    fseek ( fp, 0L, SEEK_END );
-}
-
-
-void WriteBits (mpc_encoder_t * e, const mpc_uint32_t input, const unsigned int bits )
-{
-    e->BufferedBits += bits;
-    e->filled       -= bits;
-
-    if      ( e->filled > 0 ) {
-        e->dword  |= input << e->filled;
-    }
-    else if ( e->filled < 0 ) {
-        e->Buffer [e->Zaehler++] = e->dword | ( input >> -e->filled );
-        e->filled += 32;
-        e->dword   = input << e->filled;
-    }
-    else {
-        e->Buffer [e->Zaehler++] = e->dword | input;
-        e->filled  = 32;
-        e->dword   =  0;
-    }
-}
-
-// Bits in the original stream have to be 0, maximum X bits allowed to be set in input
-// Actual bitstream must have already written ptr[0] and ptr[1]
-void WriteBitsAt (mpc_encoder_t * e, const mpc_uint32_t input, const unsigned int bits, BitstreamPos const pos )
-{
-    mpc_uint32_t*     ptr    = pos.ptr;
-    int           filled = pos.bit - bits;
-
-//    fprintf ( stderr, "%5u %2u %08lX %2u\n", input, bits, pos.ptr, pos.bit );
-
-    e->Buffer [e->Zaehler] = e->dword;
-
-    if      ( filled > 0 ) {
-        ptr [0] |= input << (  +filled);
-    }
-    else if ( filled < 0 ) {
-        ptr [0] |= input >> (  -filled);
-        ptr [1] |= input << (32+filled);
-    }
-    else {
-        ptr [0] |= input;
-    }
-
-    e->dword = e->Buffer [e->Zaehler];
-}
-
-
-void GetBitstreamPos (mpc_encoder_t * e, BitstreamPos* const pos )
-{
-    pos -> ptr = e->Buffer + e->Zaehler;
-    pos -> bit = e->filled;
+	}
+	e->framesInBlock = 0;
 }
 
Index: mppenc/branches/r2d/libmpcenc/crc32.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/crc32.c	(revision 78)
+++ mppenc/branches/r2d/libmpcenc/crc32.c	(revision 78)
@@ -0,0 +1,56 @@
+/*
+*  C Implementation: crc32
+*
+*  code from http://www.w3.org/TR/PNG/#D-CRCAppendix
+*
+*/
+
+/* Table of CRCs of all 8-bit messages. */
+static unsigned long crc_table[256];
+
+/* Flag: has the table been computed? Initially false. */
+static int crc_table_computed = 0;
+
+/* Make the table for a fast CRC. */
+static void make_crc_table(void)
+{
+	unsigned long c;
+	int n, k;
+
+	for (n = 0; n < 256; n++) {
+		c = (unsigned long) n;
+		for (k = 0; k < 8; k++) {
+			if (c & 1)
+				c = 0xedb88320L ^ (c >> 1);
+			else
+				c = c >> 1;
+		}
+		crc_table[n] = c;
+	}
+	crc_table_computed = 1;
+}
+
+
+/* Update a running CRC with the bytes buf[0..len-1]--the CRC
+	should be initialized to all 1's, and the transmitted value
+	is the 1's complement of the final running CRC (see the
+	crc() routine below). */
+
+static unsigned long update_crc(unsigned long crc, unsigned char *buf, int len)
+{
+	unsigned long c = crc;
+	int n;
+
+	if (!crc_table_computed)
+		make_crc_table();
+	for (n = 0; n < len; n++) {
+		c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
+	}
+	return c;
+}
+
+/* Return the CRC of the bytes buf[0..len-1]. */
+unsigned long crc32(unsigned char *buf, int len)
+{
+	return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
+}
Index: mppenc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 77)
+++ mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 78)
@@ -23,5 +23,9 @@
 #include "libmpcenc.h"
 
-void  WriteBits         ( mpc_encoder_t*, const mpc_uint32_t input, const unsigned int bits );
+// 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 writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC);
+
 void       Init_Huffman_Encoder_SV7   ( void );
 
@@ -58,7 +62,7 @@
 
 
-// initialize SV7
+// initialize SV8
 void
-Init_SV7 ( mpc_encoder_t * e )
+Init_SV8 ( mpc_encoder_t * e )
 {
     Init_Huffman_Encoder_SV7 ();
@@ -66,80 +70,57 @@
 	Klemm    ();
 
-	e->dword = 0;
-	e->filled = 32;
-	e->Zaehler = 0;
-	e->BufferedBits = 0;
+	e->pos = 0;
+	e->bitsCount = 0;
+	e->outputBits = 0;
+	e->bitsBuff = 0;
 	e->Overflows = 0;
 }
 
 
-// writes SV7-header
+// writes SV8-header
 void
-WriteHeader_SV7 ( mpc_encoder_t*e,
+WriteHeader_SV8 ( mpc_encoder_t*e,
 				  const unsigned int  MaxBand,
-                  const unsigned int  Profile,
                   const unsigned int  MS_on,
-                  const mpc_uint32_t  TotalFrames,
-                  const unsigned int  SamplesRest,
+                  const unsigned int  SamplesCount,
                   const unsigned int  StreamVersion,
-                  const unsigned int  SampleFreq )
+				  const unsigned int  PNS_on,
+                  const unsigned int  SampleFreq,
+				  const unsigned int  ChannelCount)
 {
-    WriteBits ( e, StreamVersion,  8 );    // StreamVersion
-	WriteBits ( e, 0x2B504D     , 24 );    // Magic Number "MP+"
-
-	WriteBits ( e, TotalFrames  , 32 );    // # of frames
-
-	WriteBits ( e, 0            ,  1 );    // former IS-Flag (not supported anymore)
-	WriteBits ( e, MS_on        ,  1 );    // MS-Coding Flag
-	WriteBits ( e, MaxBand      ,  6 );    // Bandwidth
-
-#if 0
-    if ( MPPENC_VERSION [3] & 1 )
-        WriteBits ( e, 1        ,  4 );    // 1: Experimental profile
-    else
-#endif
-
-        WriteBits ( e, Profile  ,  4 );    // 5...15: below Telephone...above BrainDead
-	WriteBits ( e, 0            ,  2 );    // for future use
-    switch ( SampleFreq ) {
-		case 44100: WriteBits ( e, 0, 2 ); break;
-		case 48000: WriteBits ( e, 1, 2 ); break;
-		case 37800: WriteBits ( e, 2, 2 ); break;
-		case 32000: WriteBits ( e, 3, 2 ); break;
+	unsigned char samplesCount[10];
+	int samplesCountLen = encodeSize(SamplesCount, (char *)samplesCount, FALSE);
+	int i;
+
+    writeBits ( e, StreamVersion,  8 );    // StreamVersion
+
+	for( i = 0; i < samplesCountLen; i++) // nb of samples
+		writeBits ( e, samplesCount[i]  , 8 );
+
+	switch ( SampleFreq ) {
+		case 44100: writeBits ( e, 0, 4 ); break;
+		case 48000: writeBits ( e, 1, 4 ); break;
+		case 37800: writeBits ( e, 2, 4 ); break;
+		case 32000: writeBits ( e, 3, 4 ); break;
 		default   : sprintf(stderr, "Internal error\n");// FIXME : stderr_printf ( "Internal error\n");
-                    exit (1);
-    }
-	WriteBits ( e, 0            , 16 );    // maximum input sample value, currently filled by replaygain
-
-	WriteBits ( e, 0            , 32 );    // title based gain controls, currently filled by replaygain
-
-	WriteBits ( e, 0            , 32 );    // album based gain controls, currently filled by replaygain
-
-	WriteBits ( e, 1            ,  1 );    // true gapless: used?
-	WriteBits ( e, SamplesRest  , 11 );    // true gapless: valid samples in last frame
-	WriteBits ( e, 1            , 1 );     // we now support fast seeking
-	WriteBits ( e, 0            , 19 );
-
-	WriteBits ( e, (MPPENC_VERSION[0]&15)*100 + (MPPENC_VERSION[2]&15)*10 + (MPPENC_VERSION[3]&15),
-                                8 );    // for future use
+		exit (1);
+	}
+
+	writeBits ( e, ChannelCount - 1  ,  4 );    // Channels
+	writeBits ( e, MaxBand - 1  ,  5 );    // Bandwidth
+	writeBits ( e, 0            ,  1 );    // former IS-Flag (not supported anymore)
+	writeBits ( e, MS_on        ,  1 );    // MS-Coding Flag
+	writeBits ( e, PNS_on       ,  1 );    // PNS flag
+	writeBits ( e, FRAMES_PER_BLOCK_PWR,  4 );    // frames per block (log2 unit)
 }
-
-
-void
-FinishBitstream ( mpc_encoder_t* e )
-{
-    e->Buffer [e->Zaehler++] = e->dword;         // Assigning the "last" word
-}
-
 
 #define ENCODE_SCF1( new, old, rll )                         \
         d = new - old + 7;                                   \
-        if ( d <= 14u  && rll < 32) {                        \
-            WriteBits ( e, Table[d].Code, Table[d].Length );    \
-        }                                                    \
-        else {                                               \
+        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 );              \
+            writeBits ( e, Table[15].Code, Table[15].Length );  \
+            writeBits ( e, (unsigned int)new, 6 );              \
             rll = 0;                                         \
         }
@@ -148,38 +129,11 @@
         d = new - old + 7;                                   \
         if ( d <= 14u ) {                                    \
-            WriteBits ( e, Table[d].Code, Table[d].Length );    \
-        }                                                    \
-        else {                                               \
+            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 );              \
+            writeBits ( e, Table[15].Code, Table[15].Length );  \
+            writeBits ( e, (unsigned int)new, 6 );              \
             rll = 0;                                         \
         }
-
-
-static void
-test ( const int* const Res, const unsigned int* q )
-{
-#if 0
-    int  i;
-
-    switch ( *Res ) {
-    case 1:
-        for ( i = 0; i < 36; i ++ )
-            if ( q[i] != 1 )
-                return;
-        fprintf ( stderr, "Alles Nullsamples, aber Auflï¿œung = %u\n", *Res );
-        *Res = 0;
-        break;
-    case 2:
-        for ( i = 0; i < 36; i ++ )
-            if ( q[i] != 2 )
-                return;
-        fprintf ( stderr, "Alles Nullsamples, aber Auflï¿œung = %u\n", *Res );
-        *Res = 0;
-        break;
-    }
-#endif
-}
 
 
@@ -204,33 +158,30 @@
 
     /************************************ Resolution *********************************/
-    WriteBits ( e, (unsigned int)Res_L[0], 4 );                            // subband 0
-	WriteBits ( e, (unsigned int)Res_R[0], 4 );
+    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 );
+		writeBits ( e, MS_Flag[0] , 1 );
 
     Table = HuffHdr;                                                    // subband 1...MaxBand
     for ( n = 1; n <= MaxBand; n++ ) {
-        test ( Res_L+n, Q[n].L );
-
         d = Res_L[n] - Res_L[n-1] + 5;
         if ( d <= 8u ) {
-			WriteBits ( e, Table[d].Code, Table[d].Length );
+			writeBits ( e, Table[d].Code, Table[d].Length );
         }
         else {
-			WriteBits ( e, Table[9].Code, Table[9].Length );
-			WriteBits ( e, Res_L[n]     , 4               );
-        }
-
-        test ( Res_R+n, Q[n].R );
+			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 );
+			writeBits ( e, Table[d].Code, Table[d].Length );
         }
         else {
-			WriteBits ( e, Table[9].Code, Table[9].Length );
-			WriteBits ( e, Res_R[n]     , 4               );
+			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 );
+			writeBits ( e, MS_Flag[n], 1 );
     }
 
@@ -240,9 +191,9 @@
         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 );
+			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 );
+			writeBits ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );
         }
     }
@@ -250,5 +201,8 @@
     /************************************* SCF **********************************/
     Table = HuffDSCF;
-    for ( n = 0; n <= MaxBand; n++ ) {
+	for ( n = 0; n <= MaxBand; n++ ) {
+		if (e->framesInBlock == 0){
+			DSCF_RLL_L[n] = DSCF_RLL_R[n] = 1; // new block -> force key frame
+		}
 
         if ( Res_L[n] ) {
@@ -276,6 +230,4 @@
             }
         }
-        if (DSCF_RLL_L[n] <= 32)
-            DSCF_RLL_L[n]++;        // Increased counters for SCF that haven't been initialized again
 
         if ( Res_R[n] ) {
@@ -303,6 +255,4 @@
             }
         }
-        if (DSCF_RLL_R[n] <= 32)
-            DSCF_RLL_R[n]++;          // Increased counters for SCF that haven't been freshly initialized
     }
 
@@ -326,9 +276,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			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 );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -342,9 +292,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			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 );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -361,14 +311,14 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			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 );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
         default:
             for ( k = 0; k < 36; k++ )
-				WriteBits ( e, q[k], Res_L[n]-1 );
+				writeBits ( e, q[k], Res_L[n]-1 );
             break;
         }
@@ -390,9 +340,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			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 );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -406,9 +356,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			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 );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -425,19 +375,22 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			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 );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
         default:
             for ( k = 0; k < 36; k++ )
-				WriteBits ( e, q[k], Res_R[n] - 1 );
+				writeBits ( e, q[k], Res_R[n] - 1 );
             break;
         }
 
     }
-    return;
+
+	e->framesInBlock++;
+	if (e->framesInBlock == FRAMES_PER_BLOCK)
+		writeBlock(e, "AD", FALSE);
 }
 
Index: mppenc/branches/r2d/libmpcenc/huffsv7.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/huffsv7.c	(revision 77)
+++ mppenc/branches/r2d/libmpcenc/huffsv7.c	(revision 78)
@@ -62,19 +62,19 @@
 #endif
 
-static const HuffSrc_t   HuffSCFI_src [4] = {
+static const Huffman_t   HuffSCFI_src [4] = {
     { 2, 3 }, { 1, 1 }, { 3, 3 }, { 0, 2 }
 };
 
-static const HuffSrc_t   HuffDSCF_src [16] = {
+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 HuffSrc_t   HuffHdr_src [10] = {
+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 HuffSrc_t   HuffQ1_src [2] [3*3*3] = { {
+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 },
@@ -86,5 +86,5 @@
 } };
 
-static const HuffSrc_t   HuffQ2_src [2] [5*5] = { {
+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 },
@@ -101,5 +101,5 @@
 
 #ifdef USE_SV8
-static const HuffSrc_t   HuffN3_src [2] [7*7] = { {
+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 },
@@ -120,5 +120,5 @@
 #endif
 
-static const HuffSrc_t   HuffQ3_src [2] [ 7] = { {
+static const Huffman_t   HuffQ3_src [2] [ 7] = { {
     { 12, 4 }, { 4, 3 }, { 0, 2 }, { 1, 2 }, { 7, 3 }, { 5, 3 }, { 13, 4 }
 }, {
@@ -126,5 +126,5 @@
 } };
 
-static const HuffSrc_t   HuffQ4_src [2] [ 9] = { {
+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 }
 }, {
@@ -132,5 +132,5 @@
 } };
 
-static const HuffSrc_t   HuffQ5_src [2] [15] = { {
+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 }
@@ -140,5 +140,5 @@
 } };
 
-static const HuffSrc_t   HuffQ6_src [2] [31] = { {
+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 },
@@ -152,5 +152,5 @@
 } };
 
-static const HuffSrc_t   HuffQ7_src [2] [63] = { {
+static const Huffman_t   HuffQ7_src [2] [63] = { {
     { 103, 8 },    // 0.3338   01100111
     { 153, 8 },    // 0.3766   10011001
@@ -283,5 +283,5 @@
 
 #ifdef USE_SV8
-static const HuffSrc_t   HuffN8_src [2] [127] = { {
+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 },
@@ -341,5 +341,5 @@
  */
 
-void Make_HuffTable ( Huffman_t* dst, const HuffSrc_t* src, mpc_size_t len )
+void Make_HuffTable ( Huffman_t* dst, const Huffman_t* src, mpc_size_t len )
 {
 	mpc_size_t  i;
Index: mppenc/branches/r2d/libmpcenc/libmpcenc.h
===================================================================
--- mppenc/branches/r2d/libmpcenc/libmpcenc.h	(revision 77)
+++ mppenc/branches/r2d/libmpcenc/libmpcenc.h	(revision 78)
@@ -20,4 +20,5 @@
 
 #include "config_types.h"
+#include <stdio.h>
 
 // FIXME : define this somewhere else
@@ -32,19 +33,7 @@
 
 // bitstream.c
-#define BUFFER_ALMOST_FULL  8192
-#define BUFFER_FULL         (BUFFER_ALMOST_FULL + 4352)         // 34490 bit/frame  1320.3 kbps
-
-#ifndef ENDIAN
-#define HAVE_LITTLE_ENDIAN  1234
-#define HAVE_BIG_ENDIAN     4321
-
-#define ENDIAN              HAVE_LITTLE_ENDIAN
-#endif
-
-// bitstream.c
-typedef struct {
-	mpc_uint32_t*	ptr;
-	unsigned int	bit;
-} BitstreamPos;
+#define FRAMES_PER_BLOCK_PWR 6
+#define FRAMES_PER_BLOCK (1 << FRAMES_PER_BLOCK_PWR)
+#define BUFFER_FULL         (4352 * FRAMES_PER_BLOCK)         // 34490 bit/frame  1320.3 kbps
 
 typedef struct {
@@ -55,25 +44,22 @@
 // TODO : enc/dec common struct
 // just the same struct as below, dup ?
-typedef struct {
-	mpc_uint16_t	Code;  // >= 14 bit
-	mpc_uint16_t	Length;  // >=  4 bit
-} HuffSrc_t ;
 
 typedef struct {
 	mpc_uint16_t	Code;        // >= 14 bit
 	mpc_uint16_t	Length;      // >=  4 bit
-} Huffman_t ;
+} Huffman_t;
 
-// TODO : match with mpc_decoder_t
-// FIXME : add init code
 typedef struct {
-	mpc_uint32_t	Buffer [BUFFER_FULL];    // Buffer for bitstream-file
-	mpc_uint32_t	dword; //         =  0;      // 32-bit-Word for Bitstream-I/O
-	mpc_int32_t		filled; //        = 32;      // Position in the the 32-bit-word that's currently about to be filled
-	mpc_uint32_t	Zaehler; //       =  0;      // Position pointer for the processed bitstream-word (32 bit)
-	mpc_uint64_t	BufferedBits; //  =  0;      // Counter for the number of written bits in the bitstream
+	mpc_uint_t pos; // next free byte position in the buffer
+	mpc_uint_t bitsCount; // number of used bits in bitsBuff
+	mpc_uint64_t outputBits; // Counter for the number of written bits in the bitstream
+	mpc_uint32_t bitsBuff; // bits buffer
+	mpc_uint8_t buffer [BUFFER_FULL]; // Buffer for bitstream-file
+	mpc_uint_t framesInBlock;
+
+	FILE * outputFile; // ouput file
 
 	unsigned int  MS_Channelmode;
 	unsigned int  Overflows; //       = 0;      // number of internal (filterbank) clippings
-} mpc_encoder_t;
+ } mpc_encoder_t;
 
