Index: /libmpc/branches/r2d/include/mpc/mpc_types.h
===================================================================
--- /libmpc/branches/r2d/include/mpc/mpc_types.h	(revision 146)
+++ /libmpc/branches/r2d/include/mpc/mpc_types.h	(revision 147)
@@ -72,5 +72,5 @@
 typedef unsigned int mpc_uint_t;
 // FIXME : must be the same size as a pointer
-typedef unsigned int mpc_size_t;
+typedef size_t mpc_size_t;
 
 /// Libmpcdec error codes
Index: /libmpc/branches/r2d/include/mpcdec/mpcdec.h
===================================================================
--- /libmpc/branches/r2d/include/mpcdec/mpcdec.h	(revision 146)
+++ /libmpc/branches/r2d/include/mpcdec/mpcdec.h	(revision 147)
@@ -57,7 +57,7 @@
 
 typedef struct mpc_frame_info_t {
-	mpc_uint32_t samples;	// number of samples in the frame
-	mpc_uint32_t bits;	// number of bits consumed by this frame
-	MPC_SAMPLE_FORMAT * buffer;	// frame samples buffer (size = samples * channels * sizeof(MPC_SAMPLE_FORMAT)
+	mpc_uint32_t samples;	/// number of samples in the frame
+	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_frame_info;
 
@@ -75,5 +75,10 @@
 // void mpc_decoder_set_seeking(mpc_decoder *p_dec, mpc_streaminfo *si, mpc_bool_t fast_seeking);
 
-// void mpc_decoder_set_streaminfo(mpc_decoder *p_dec, mpc_streaminfo *si);
+/**
+ * set the scf indexes for seeking use
+ * needed only for sv7 seeking
+ * @param d
+ */
+void mpc_decoder_reset_scf(mpc_decoder * d);
 
 /// Sets decoder sample scaling factor.  All decoded samples will be multiplied
@@ -92,10 +97,4 @@
 void mpc_decoder_decode_frame(mpc_decoder * d, mpc_bits_reader * r, mpc_frame_info * i);
 
-/// Seeks to the specified sample in the source stream.
-// mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *p_dec, mpc_int64_t destsample);
-
-/// Seeks to specified position in seconds in the source stream.
-// mpc_bool_t mpc_decoder_seek_seconds(mpc_decoder *p_dec, double seconds);
-
 // init demuxer
 mpc_demux * mpc_demux_init(mpc_reader * p_reader);
@@ -106,4 +105,8 @@
 // get streaminfo
 void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i);
+// seek
+mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_int64_t destsample);
+mpc_status mpc_demux_seek_second(mpc_demux * d, double seconds);
+
 
 #ifdef __cplusplus
Index: /libmpc/branches/r2d/include/mpcdec/streaminfo.h
===================================================================
--- /libmpc/branches/r2d/include/mpcdec/streaminfo.h	(revision 146)
+++ /libmpc/branches/r2d/include/mpcdec/streaminfo.h	(revision 147)
@@ -64,5 +64,5 @@
     mpc_uint32_t         ms;                 ///< Mid/side stereo (0: off, 1: on)
 	mpc_uint32_t         fast_seek;          ///< True if stream supports fast-seeking (sv7)
-	mpc_uint32_t         frames_per_block;   ///< Number of frames in a block (sv8)
+	mpc_uint32_t         block_pwr;          ///< Number of frames in a block = 2^block_pwr (sv8)
     //@}
 
Index: /libmpc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- /libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 146)
+++ /libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 147)
@@ -70,4 +70,17 @@
 }
 
+static void encodeGolomb(mpc_encoder_t * e, mpc_uint32_t nb, mpc_uint_t k)
+{
+	unsigned int l = (nb >> k) + 1;
+	nb &= (1 << k) - 1;
+
+	while( l > 32 ){
+		writeBits(e, 0, 32);
+		l -= 32;
+	}
+	writeBits(e, 1, l);
+	writeBits(e, nb, k);
+}
+
 void writeMagic(mpc_encoder_t * e)
 {
@@ -121,3 +134,43 @@
 }
 
+void writeSeekTable (mpc_encoder_t * e)
+{
+	mpc_uint32_t tmp1, tmp2, 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);
+
+	tmp1 = table[0];
+	tmp2 = table[1];
+
+	len = encodeSize(tmp1 - e->seek_ref, tmp, MPC_FALSE);
+	for( i = 0; i < len; i++)
+		writeBits ( e, tmp[i], 8 );
+	len = encodeSize(tmp2 - e->seek_ref, 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);
+		}
+	}
+}
+
 /* end of bitstream.c */
Index: /libmpc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- /libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 146)
+++ /libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 147)
@@ -75,4 +75,5 @@
 	e->bitsBuff = 0;
 	e->Overflows = 0;
+	e->seek_pos = 0;
 }
 
@@ -415,6 +416,9 @@
 
 	e->framesInBlock++;
-	if (e->framesInBlock == FRAMES_PER_BLOCK)
+	if (e->framesInBlock == FRAMES_PER_BLOCK) {
+		e->seek_table[e->seek_pos] = ftell(e->outputFile);
+		e->seek_pos++;
 		writeBlock(e, "AD", MPC_FALSE);
+	}
 }
 
Index: /libmpc/branches/r2d/libmpcenc/libmpcenc.h
===================================================================
--- /libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 146)
+++ /libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 147)
@@ -32,11 +32,11 @@
 #define BUFFER_FULL         (4352 * FRAMES_PER_BLOCK)         // 34490 bit/frame  1320.3 kbps
 
+// FIXME : make this size dependent of the input file
+#define SEEK_SIZE (1 << 16) // number of seek entries
+
 typedef struct {
 	unsigned int  L [36];
 	unsigned int  R [36];
 } SubbandQuantTyp;
-
-// TODO : enc/dec common struct
-// just the same struct as below, dup ?
 
 typedef struct {
@@ -53,4 +53,9 @@
 	mpc_uint_t framesInBlock;
 
+	// seeking
+	mpc_uint32_t seek_table[SEEK_SIZE];
+	mpc_uint32_t seek_pos; /// current position in the seek table
+	mpc_uint32_t seek_ref; /// reference position for the seek information
+
 	FILE * outputFile; // ouput file
 
Index: /libmpc/branches/r2d/mppenc/mppenc.c
===================================================================
--- /libmpc/branches/r2d/mppenc/mppenc.c	(revision 146)
+++ /libmpc/branches/r2d/mppenc/mppenc.c	(revision 147)
@@ -1631,5 +1631,4 @@
 
 	e.MS_Channelmode = m.MS_Channelmode;
-//     e.BufferedBits     = 0;
 	writeMagic(&e);
 	writeStreamInfo ( &e, m.Max_Band, m.MS_Channelmode > 0, SamplesInWAVE,
@@ -1641,4 +1640,8 @@
 					  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
+
 
     // initialize timer
@@ -1722,7 +1725,4 @@
         }
 
-        // for backwards-compatibility with older decoders write the 11 bit for
-        // reconstruction of exact filelength before the very last frame
-
         memmove ( Main.L, Main.L + BLOCK, CENTER * sizeof(float) );
         memmove ( Main.R, Main.R + BLOCK, CENTER * sizeof(float) );
@@ -1743,5 +1743,9 @@
 
     // write the last incomplete block
+	e.seek_table[e.seek_pos] = ftell(e.outputFile);
+	e.seek_pos++;
 	writeBlock(&e, "AD", FALSE);
+	writeSeekTable(&e);
+	writeBlock(&e, "ST", FALSE); // write seek table block
 	writeBlock(&e, "SE", FALSE); // write end of stream block
     ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.outputBits );
Index: /libmpc/branches/r2d/mppenc/mppenc.h
===================================================================
--- /libmpc/branches/r2d/mppenc/mppenc.h	(revision 146)
+++ /libmpc/branches/r2d/mppenc/mppenc.h	(revision 147)
@@ -121,5 +121,7 @@
 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);
+void writeMagic (mpc_encoder_t * e);
+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* );
 
