Index: libmpc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 147)
+++ libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 149)
@@ -136,39 +136,35 @@
 void writeSeekTable (mpc_encoder_t * e)
 {
-	mpc_uint32_t tmp1, tmp2, i, len;
+	mpc_uint32_t i, len;
 	mpc_uint32_t * table = e->seek_table;
 	mpc_uint8_t tmp[10];
 
 	// write the position to header
-	tmp1 = ftell(e->outputFile); // get the seek table position
-	tmp[0] = (mpc_uint8_t) (tmp1 >> 24);
-	tmp[1] = (mpc_uint8_t) (tmp1 >> 16);
-	tmp[2] = (mpc_uint8_t) (tmp1 >> 8);
-	tmp[3] = (mpc_uint8_t) tmp1;
-	fseek(e->outputFile, e->seek_ref + 3, SEEK_SET);
-	fwrite(tmp, sizeof(mpc_uint8_t), 4, e->outputFile);
-	fseek(e->outputFile, tmp1, SEEK_SET);
+	i = ftell(e->outputFile); // get the seek table position
+	len = encodeSize(i - e->seek_ptr, (char*)tmp, MPC_FALSE);
+	fseek(e->outputFile, e->seek_ptr + 3, SEEK_SET);
+	fwrite(tmp, sizeof(mpc_uint8_t), len, e->outputFile);
+	fseek(e->outputFile, i, SEEK_SET);
 
-	tmp1 = table[0];
-	tmp2 = table[1];
-
-	len = encodeSize(tmp1 - e->seek_ref, tmp, MPC_FALSE);
+	// write the seek table datas
+	len = encodeSize(e->seek_pos, (char*)tmp, MPC_FALSE);
 	for( i = 0; i < len; i++)
 		writeBits ( e, tmp[i], 8 );
-	len = encodeSize(tmp2 - e->seek_ref, tmp, MPC_FALSE);
+	writeBits ( e, e->seek_pwr, 4 );
+
+	len = encodeSize(table[0] - e->seek_ref, (char*)tmp, MPC_FALSE);
 	for( i = 0; i < len; i++)
 		writeBits ( e, tmp[i], 8 );
+	if (e->seek_pos > 1) {
+		len = encodeSize(table[1] - e->seek_ref, (char*)tmp, MPC_FALSE);
+		for( i = 0; i < len; i++)
+			writeBits ( e, tmp[i], 8 );
+	}
 
 	for( i = 2; i < e->seek_pos; i++){
-		int code = table[i] - 2 * tmp2 + tmp1;
-		tmp1 = tmp2;
-		tmp2 = table[i];
-		if (code >= 0) {
-			writeBits(e, 0, 1);
-			encodeGolomb(e, code, 10);
-		} else {
-			writeBits(e, 1, 1);
-			encodeGolomb(e, -code, 10);
-		}
+		int code = (table[i] - 2 * table[i-1] + table[i-2]) << 1;
+		if (code < 0)
+			code = -code | 1;
+		encodeGolomb(e, code, 12);
 	}
 }
Index: libmpc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 147)
+++ libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 149)
@@ -76,6 +76,17 @@
 	e->Overflows = 0;
 	e->seek_pos = 0;
-}
-
+	e->block_cnt = 0;
+	e->seek_pwr = 1;
+}
+
+// writes replay gain info
+static void writeGainInfo ( mpc_encoder_t * e )
+{
+	writeBits ( e, 1,  8 ); // version
+	writeBits ( e, 0,  16 ); // Title gain
+	writeBits ( e, 0,  16 ); // Title peak
+	writeBits ( e, 0,  16 ); // Album gain
+	writeBits ( e, 0,  16 ); // Album peak
+}
 
 // writes SV8-header
@@ -88,12 +99,15 @@
 				  const unsigned int  ChannelCount)
 {
-	unsigned char samplesCount[10];
-	int samplesCountLen = encodeSize(SamplesCount, (char *)samplesCount, MPC_FALSE);
-	int i;
+	unsigned char tmp[10];
+	int i, len = encodeSize(SamplesCount, (char *)tmp, MPC_FALSE);
+
+	len = encodeSize(len + 4, (char *)tmp, MPC_TRUE);
+	writeBits ( e, tmp[0]  , 8 );
 
     writeBits ( e, 0x08,  8 );    // StreamVersion
 
-	for( i = 0; i < samplesCountLen; i++) // nb of samples
-		writeBits ( e, samplesCount[i]  , 8 );
+	len = encodeSize(SamplesCount, (char *)tmp, MPC_FALSE);
+	for( i = 0; i < len; i++) // nb of samples
+		writeBits ( e, tmp[i]  , 8 );
 
 	switch ( SampleFreq ) {
@@ -110,4 +124,7 @@
 	writeBits ( e, MS_on        ,  1 );    // MS-Coding Flag
 	writeBits ( e, FRAMES_PER_BLOCK_PWR,  4 );    // frames per block (log2 unit)
+	writeBits ( e, 0, 6 );    // unused
+
+	writeGainInfo(e);
 }
 
@@ -128,14 +145,4 @@
 	writeBits ( e, version_implement,  4 );
 	writeBits ( e, version_build,  8 );
-}
-
-// writes replay gain info
-void writeGainInfo ( mpc_encoder_t * e )
-{
-	writeBits ( e, 1,  8 ); // version
-	writeBits ( e, 0,  16 ); // Title gain
-	writeBits ( e, 0,  16 ); // Title peak
-	writeBits ( e, 0,  16 ); // Album gain
-	writeBits ( e, 0,  16 ); // Album peak
 }
 
@@ -417,6 +424,9 @@
 	e->framesInBlock++;
 	if (e->framesInBlock == FRAMES_PER_BLOCK) {
-		e->seek_table[e->seek_pos] = ftell(e->outputFile);
-		e->seek_pos++;
+		if ((e->block_cnt & ((1 << e->seek_pwr) - 1)) == 0) {
+			e->seek_table[e->seek_pos] = ftell(e->outputFile);
+			e->seek_pos++;
+		}
+		e->block_cnt++;
 		writeBlock(e, "AD", MPC_FALSE);
 	}
Index: libmpc/branches/r2d/libmpcenc/libmpcenc.h
===================================================================
--- libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 147)
+++ libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 149)
@@ -57,4 +57,7 @@
 	mpc_uint32_t seek_pos; /// current position in the seek table
 	mpc_uint32_t seek_ref; /// reference position for the seek information
+	mpc_uint32_t seek_ptr; /// position of the seek pointer block
+	mpc_uint32_t seek_pwr; /// keep a seek table entry every 2^seek_pwr block
+	mpc_uint32_t block_cnt; /// number of encoded blocks
 
 	FILE * outputFile; // ouput file
