Index: /libmpc/branches/r2d/libmpcdec/mpc_demux.c
===================================================================
--- /libmpc/branches/r2d/libmpcdec/mpc_demux.c	(revision 148)
+++ /libmpc/branches/r2d/libmpcdec/mpc_demux.c	(revision 149)
@@ -189,6 +189,4 @@
 			} else if (memcmp(b.key, "EI", 2) == 0)
 				streaminfo_encoder_info(&d->si, &d->bits_reader);
-			else if (memcmp(b.key, "RG", 2) == 0)
-				streaminfo_gain(&d->si, &d->bits_reader);
 			d->bits_reader.buff += b.size;
 			size = mpc_bits_get_block(&d->bits_reader, &b);
Index: /libmpc/branches/r2d/libmpcdec/streaminfo.c
===================================================================
--- /libmpc/branches/r2d/libmpcdec/streaminfo.c	(revision 148)
+++ /libmpc/branches/r2d/libmpcdec/streaminfo.c	(revision 149)
@@ -140,4 +140,17 @@
 }
 
+/// Reads replay gain datas
+void  streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in)
+{
+	mpc_bits_reader r = *r_in;
+
+	mpc_bits_read(&r, 8); // gain version
+	// FIXME : add gain conversion
+	si->gain_title         = (mpc_uint16_t) mpc_bits_read(&r, 16);
+	si->peak_title         = (mpc_uint16_t) mpc_bits_read(&r, 16);
+	si->gain_album         = (mpc_uint16_t) mpc_bits_read(&r, 16);
+	si->peak_album         = (mpc_uint16_t) mpc_bits_read(&r, 16);
+}
+
 /// Reads streaminfo from SV8 header.
 mpc_status
@@ -145,11 +158,15 @@
 						   mpc_size_t block_size)
 {
-	mpc_uint32_t CRC, CRC_tmp;
-	mpc_bits_reader r = *r_in;
+	mpc_uint32_t CRC;
+	mpc_uint64_t info_len;
+	mpc_bits_reader r = *r_in, r_gain;
 
 	CRC = mpc_bits_read(&r, 32);
-	CRC_tmp = crc32(r.buff + 1 - (r.count >> 3), block_size - 4);
-	if (CRC != CRC_tmp)
+	if (CRC != crc32(r.buff + 1 - (r.count >> 3), block_size - 4))
 		return MPC_STATUS_FILE;
+
+	r_gain = r;
+	mpc_bits_get_size(&r, &info_len);
+	r_gain.buff += info_len;
 
 	si->stream_version = mpc_bits_read(&r, 8);
@@ -164,6 +181,9 @@
 	si->bitrate = 0;
 
-	si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
-			*  si->sample_freq / si->samples;
+	if (si->samples != 0)
+		si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
+				*  si->sample_freq / si->samples;
+
+	streaminfo_gain(si, &r_gain);
 
 	return MPC_STATUS_OK;
@@ -187,17 +207,4 @@
 }
 
-/// Reads replay gain datas
-void  streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in)
-{
-	mpc_bits_reader r = *r_in;
-
-	mpc_bits_read(&r, 8); // gain version
-	// FIXME : add gain conversion
-	si->gain_title         = (mpc_uint16_t) mpc_bits_read(&r, 16);
-	si->peak_title         = (mpc_uint16_t) mpc_bits_read(&r, 16);
-	si->gain_album         = (mpc_uint16_t) mpc_bits_read(&r, 16);
-	si->peak_album         = (mpc_uint16_t) mpc_bits_read(&r, 16);
-}
-
 double
 mpc_streaminfo_get_length(mpc_streaminfo * si)
Index: /libmpc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- /libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 148)
+++ /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 148)
+++ /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 148)
+++ /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
Index: /libmpc/branches/r2d/mppenc/mppenc.c
===================================================================
--- /libmpc/branches/r2d/mppenc/mppenc.c	(revision 148)
+++ /libmpc/branches/r2d/mppenc/mppenc.c	(revision 149)
@@ -1631,16 +1631,16 @@
 
 	e.MS_Channelmode = m.MS_Channelmode;
+	e.seek_ref = ftell(e.outputFile);
 	writeMagic(&e);
 	writeStreamInfo ( &e, m.Max_Band, m.MS_Channelmode > 0, SamplesInWAVE,
 					   m.SampleFreq, Wave.Channels > 2 ? 2 : Wave.Channels);
 	writeBlock(&e, "SI", TRUE);
-	writeGainInfo(&e);
-	writeBlock(&e, "RG", FALSE);
 	writeEncoderInfo(&e, m.MainQual, m.PNS > 0, MPPENC_MAJOR, MPPENC_MINOR,
 					  MPPENC_IMPLEMENT, MPPENC_BUILD);
 	writeBlock(&e, "EI", FALSE);
-	e.seek_ref = ftell(e.outputFile);
-	writeBits (&e, 0, 32); // jump 32 bits for seek table pointer
-	writeBlock(&e, "ST", FALSE); // reserve space for pointer
+	e.seek_ptr = ftell(e.outputFile);
+	writeBits (&e, 0, 8);
+	writeBits (&e, 0, 32); // jump 40 bits for seek table pointer
+	writeBlock(&e, "SP", FALSE); // reserve space for seek pointer
 
 
@@ -1743,6 +1743,8 @@
 
     // write the last incomplete 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++;
+	}
 	writeBlock(&e, "AD", FALSE);
 	writeSeekTable(&e);
Index: /libmpc/branches/r2d/mppenc/mppenc.h
===================================================================
--- /libmpc/branches/r2d/mppenc/mppenc.h	(revision 148)
+++ /libmpc/branches/r2d/mppenc/mppenc.h	(revision 149)
@@ -119,5 +119,4 @@
 						const int version_implement,
 						const int version_build );
-void writeGainInfo ( mpc_encoder_t * e );
 void writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC);
 void writeMagic (mpc_encoder_t * e);
