Index: libmpcdec/branches/zorg/include/mpcdec/mpcdec.h
===================================================================
--- libmpcdec/branches/zorg/include/mpcdec/mpcdec.h	(revision 85)
+++ libmpcdec/branches/zorg/include/mpcdec/mpcdec.h	(revision 104)
@@ -53,12 +53,20 @@
 
 typedef struct mpc_decoder_t mpc_decoder;
+typedef struct mpc_bits_reader_t mpc_bits_reader;
+typedef struct mpc_demux_t mpc_demux;
+
+typedef struct mpc_frame_info_t {
+	mpc_uint32_t samples;	// number of samples in the frame
+	mpc_uint32_t sample_freq;	// frame sample frequency
+	mpc_uint8_t channels;	// number of channels
+	MPC_SAMPLE_FORMAT * buffer;	// frame samples buffer (size = samples * channels * sizeof(MPC_SAMPLE_FORMAT)
+} mpc_frame_info;
 
 /// Initializes mpc decoder with the supplied stream info parameters.
-/// \param p_reader reader that will supply raw data to the decoder
 /// \param si streaminfo structure indicating format of source stream
-/// \return MPC_TRUE if decoder was initalized successfully, FALSE otherwise    
-mpc_status mpc_decoder_init(mpc_decoder **p_dec, mpc_reader *p_reader, mpc_streaminfo *si);
+/// \return pointer on the initialized decoder structure if successful, 0 if not
+mpc_decoder * mpc_decoder_init(mpc_streaminfo *si);
 
-/// Releases input mpc decoder 
+/// Releases input mpc decoder
 void mpc_decoder_exit(mpc_decoder *p_dec);
 
@@ -66,7 +74,7 @@
 /// \param si streaminfo structure indicating format of source stream
 /// \param fast_seeking boolean 0 = use fast seeking if safe, 1 = force fast seeking
-void mpc_decoder_set_seeking(mpc_decoder *p_dec, mpc_streaminfo *si, mpc_bool_t fast_seeking);
+// 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);
+// void mpc_decoder_set_streaminfo(mpc_decoder *p_dec, mpc_streaminfo *si);
 
 /// Sets decoder sample scaling factor.  All decoded samples will be multiplied
@@ -83,21 +91,20 @@
 /// \return 0 if the stream has been completely decoded successfully and there are no more samples
 /// \return > 0 to indicate the number of bytes that were actually read from the stream.
-mpc_uint32_t mpc_decoder_decode(
-    mpc_decoder       *p_dec,
-    MPC_SAMPLE_FORMAT *buffer, 
-    mpc_uint32_t      *vbr_update_acc, 
-    mpc_uint32_t      *vbr_update_bits);
-
-mpc_uint32_t mpc_decoder_decode_frame(
-    mpc_decoder       *p_dec,
-    mpc_uint32_t      *in_buffer,
-    mpc_uint32_t      in_len,
-    MPC_SAMPLE_FORMAT *out_buffer);
+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);
+// 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);
+// mpc_bool_t mpc_decoder_seek_seconds(mpc_decoder *p_dec, double seconds);
+
+// init demuxer
+mpc_demux * mpc_demux_init(mpc_reader * p_reader);
+// free demuxer
+void mpc_demux_exit(mpc_demux * d);
+// decode frame
+void mpc_demux_decode(mpc_demux * d, mpc_frame_info * i);
+// get streaminfo
+void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i);
 
 #ifdef __cplusplus
Index: libmpcdec/branches/zorg/include/mpcdec/streaminfo.h
===================================================================
--- libmpcdec/branches/zorg/include/mpcdec/streaminfo.h	(revision 85)
+++ libmpcdec/branches/zorg/include/mpcdec/streaminfo.h	(revision 104)
@@ -75,5 +75,5 @@
     /// @name Replaygain properties
     //@{
-    mpc_int16_t          gain_title;         ///< Replaygain title value 
+    mpc_int16_t          gain_title;         ///< Replaygain title value
     mpc_int16_t          gain_album;         ///< Replaygain album value
     mpc_uint16_t         peak_album;         ///< Peak album loudness level
@@ -89,14 +89,8 @@
     char                 encoder[256];       ///< Encoder name
 
-    mpc_streaminfo_off_t tag_offset;         ///< Ofset to file tags
+    mpc_streaminfo_off_t tag_offset;         ///< Offset to file tags
     mpc_streaminfo_off_t total_file_length;  ///< Total length of underlying file
     //@}
 } mpc_streaminfo;
-
-/// Reads streaminfo header from the mpc stream supplied by r.
-/// \param si si pointer to which info will be written
-/// \param p_reader stream reader to supply raw data
-/// \return error code
-mpc_status mpc_streaminfo_init(mpc_streaminfo *si, mpc_reader *p_reader);
 
 /// Gets length of stream si, in seconds.
Index: libmpcdec/branches/zorg/src/decoder.h
===================================================================
--- libmpcdec/branches/zorg/src/decoder.h	(revision 85)
+++ libmpcdec/branches/zorg/src/decoder.h	(revision 104)
@@ -62,40 +62,20 @@
 
 struct mpc_decoder_t {
-    mpc_reader *r;
-
     /// @name internal state variables
     //@{
+    mpc_uint32_t  SampleRate;                 // Sample frequency
+    mpc_uint32_t  StreamVersion;              // version of bitstream
+    mpc_uint32_t  Max_Band;
+    mpc_uint32_t  MS_used;                    // MS-coding used ?
+	mpc_uint32_t  TrueGaplessPresent;
+	mpc_uint32_t  OverallFrames;              // number of frames in the stream, 0 = unknow
+	mpc_uint32_t  LastFrameSamples;           // number of samples in the last frame
 
-    mpc_uint32_t  dword; /// currently decoded 32bit-word
-    mpc_uint32_t  pos;   /// bit-position within dword
-    mpc_uint32_t  Speicher[MPC_DECODER_MEMSIZE]; /// read-buffer
-    mpc_uint32_t  Zaehler; /// actual index within read-buffer
-
-    mpc_uint32_t  samples_to_skip;
-
-    mpc_uint32_t  DecodedFrames;
-    mpc_uint32_t  OverallFrames;
-    mpc_int32_t   SampleRate;                 // Sample frequency
-
-    mpc_uint32_t  StreamVersion;              // version of bitstream
-    mpc_int32_t   Max_Band;
-    mpc_uint32_t  MPCHeaderPos;               // AB: needed to support ID3v2
-
-    mpc_uint32_t  FrameWasValid;
-    mpc_uint32_t  MS_used;                    // MS-coding used ?
-    mpc_uint32_t  TrueGaplessPresent;
-
-    mpc_uint32_t  WordsRead;                  // counts amount of decoded dwords
-	mpc_uint32_t  BlockBits;                  // block bits left to read
+	mpc_uint32_t  samples_to_skip;
+	mpc_uint32_t  DecodedFrames;
 
     // randomizer state variables
     mpc_uint32_t  __r1;
     mpc_uint32_t  __r2;
-
-    // seeking
-    mpc_uint32_t  seeking_table[SEEKING_TABLE_SIZE];
-    mpc_uint32_t  seeking_pwr;                // distance between 2 frames in seeking_table = 2^seeking_pwr
-    mpc_uint32_t  seeking_table_frames;       // last frame in seaking table
-    mpc_uint32_t  seeking_window;             // number of frames to look for scalefactors
 
     mpc_int32_t   SCF_Index_L [32] [3];
@@ -119,5 +99,4 @@
     MPC_SAMPLE_FORMAT SCF[256]; ///< holds adapted scalefactors (for clipping prevention)
     //@}
-
 };
 
Index: libmpcdec/branches/zorg/src/internal.h
===================================================================
--- libmpcdec/branches/zorg/src/internal.h	(revision 85)
+++ libmpcdec/branches/zorg/src/internal.h	(revision 104)
@@ -52,34 +52,26 @@
 }
 
-typedef struct  {
+typedef struct mpc_block_t {
 	char key[2];	// block key
 	mpc_uint64_t size;	// block size minus the block header size
-} mpc_block_t;
+} mpc_block;
 
+// mpc_bits_reader.c
+struct mpc_bits_reader_t {
+	unsigned char * buff; /// pointer on current byte
+	unsigned int count; /// unread bits in current byte
+};
 
-/// read a block header and fills the mpc_block_t struct.
-///
-/// \param p_reader supplying raw stream data
-/// \param p_block returned block header
-/// \return MPC_STATUS_FILE on errors of any kind
-mpc_status mpc_get_block(mpc_reader * p_reader, mpc_block_t * p_block);
+#define DEMUX_BUFFER_SIZE 65536
 
-/// find a block with key == find_key, stop search if key == stop_key;
-///
-/// \param p_reader supplying raw stream data
-/// \param p_block returned block header
-/// \param find_key key to find
-/// \param stop_key key to stop on
-/// \return MPC_STATUS_FILE on errors of any kind
-mpc_status mpc_find_block(mpc_reader * p_reader, mpc_block_t * p_block,
-						  char * find_key, char * stop_key);
-
-typedef struct  {
-	unsigned int buff;
-	unsigned int bitsCount;
-} mpc_bits_reader;
-
-mpc_uint32_t mpc_read_bits(mpc_reader * p_reader, mpc_bits_reader * p_bits,
-						   int nb_bits);
+struct mpc_demux_t {
+	mpc_reader * r;
+	mpc_decoder * d;
+	mpc_streaminfo si;
+	mpc_uint8_t buffer[DEMUX_BUFFER_SIZE];
+	mpc_uint32_t bytes_total;
+	mpc_bits_reader bits_reader;
+	mpc_uint32_t block_bits;	// bits remaining in current audio block
+};
 
 /// Searches for a ID3v2-tag and reads the length (in bytes) of it.
@@ -92,5 +84,5 @@
 /// helper functions used by multiple files
 mpc_uint32_t mpc_random_int(mpc_decoder *d); // in synth_filter.c
-void mpc_decoder_initialisiere_quantisierungstabellen(mpc_decoder *d, double scale_factor);
+void mpc_decoder_init_quant(mpc_decoder *d, double scale_factor);
 void mpc_decoder_synthese_filter_float(mpc_decoder *d, MPC_SAMPLE_FORMAT* OutData);
 
Index: libmpcdec/branches/zorg/src/mpc_bits_reader.c
===================================================================
--- libmpcdec/branches/zorg/src/mpc_bits_reader.c	(revision 104)
+++ libmpcdec/branches/zorg/src/mpc_bits_reader.c	(revision 104)
@@ -0,0 +1,102 @@
+/*
+  Copyright (c) 2005, The Musepack Development Team
+  All rights reserved.
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions are
+  met:
+
+  * Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+
+  * Redistributions in binary form must reproduce the above
+  copyright notice, this list of conditions and the following
+  disclaimer in the documentation and/or other materials provided
+  with the distribution.
+
+  * Neither the name of the The Musepack Development Team nor the
+  names of its contributors may be used to endorse or promote
+  products derived from this software without specific prior
+  written permission.
+
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <mpcdec/mpcdec.h>
+#include "internal.h"
+#include "huffman.h"
+
+mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits)
+{
+	mpc_uint32_t ret = r->buff[0];
+
+	while(nb_bits > r->count){
+		r->buff++;
+		ret = (ret << 8) | r->buff[0];
+		r->count += 8;
+	}
+
+	r->count -= nb_bits;
+
+	return (ret >> r->count) & (-1u >> (32 - nb_bits));
+}
+
+// basic huffman decoding routine
+// works with maximum lengths up to 16
+mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table)
+{
+	mpc_uint32_t code = ((r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2])
+			<< (16 - r->count);
+
+	while (code < Table->Code) Table++;
+
+	int tmp = r->count - Table->Length;
+	r->buff -= tmp >> 3;
+	r->count = tmp & 0x07;
+
+	return Table->Value;
+}
+
+unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size)
+{
+	unsigned char tmp;
+	mpc_uint64_t size = 0;
+	unsigned int ret = 0;
+
+	do {
+		tmp = mpc_bits_read(r, 8);
+		size = (size << 7) | (tmp & 0x7F);
+		ret++;
+	} while((tmp & 0x80));
+
+	*p_size = size;
+	return ret;
+}
+
+int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block)
+{
+	int size = 2;
+
+	p_block->size = 0;
+	p_block->key[0] = mpc_bits_read(r, 8);
+	p_block->key[1] = mpc_bits_read(r, 8);
+
+	size += mpc_bits_get_size(r, &(p_block->size));
+
+	p_block->size -= size;
+
+	return size;
+}
+
+
+
Index: libmpcdec/branches/zorg/src/mpc_decoder.c
===================================================================
--- libmpcdec/branches/zorg/src/mpc_decoder.c	(revision 85)
+++ libmpcdec/branches/zorg/src/mpc_decoder.c	(revision 104)
@@ -42,4 +42,7 @@
 #include "requant.h"
 
+mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits);
+mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table);
+
 //SV7 tables
 extern const mpc_huffman*   mpc_table_HuffQ [2] [8];
@@ -47,10 +50,4 @@
 extern const mpc_huffman    mpc_table_HuffSCFI [ 4];
 extern const mpc_huffman    mpc_table_HuffDSCF [16];
-
-#ifndef MPC_LITTLE_ENDIAN
-#define SWAP(X) mpc_swap32(X)
-#else
-#define SWAP(X) (X)
-#endif
 
 //------------------------------------------------------------------------------
@@ -70,357 +67,119 @@
 // forward declarations
 //------------------------------------------------------------------------------
-static void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking);
-static void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
-
-//------------------------------------------------------------------------------
-// utility functions
-//------------------------------------------------------------------------------
-static mpc_int32_t f_read(mpc_decoder *d, void *ptr, mpc_int32_t size)
-{
-    return d->r->read(d->r, ptr, size);
-}
-
-static mpc_bool_t f_seek(mpc_decoder *d, mpc_int32_t offset)
-{
-    return d->r->seek(d->r, offset);
-}
-
-static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count)
-{
-    return f_read(d, ptr, count << 2) >> 2;
-}
-
-static void mpc_decoder_seek(mpc_decoder *d, mpc_uint32_t bitpos)
-{
-    f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
-    f_read_dword(d, d->Speicher, MEMSIZE);
-    d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
-    d->pos = bitpos & 31;
-    d->WordsRead = bitpos >> 5;
-}
-
-// jump desired number of bits out of the bitstream
-static void mpc_decoder_bitstream_jump(mpc_decoder *d, const mpc_uint32_t bits)
-{
-    d->pos += bits;
-
-    if (d->pos >= 32) {
-        d->Zaehler = (d->Zaehler + (d->pos >> 5)) & MEMMASK;
-        d->dword = SWAP(d->Speicher[d->Zaehler]);
-        d->WordsRead += d->pos >> 5;
-        d->pos &= 31;
-    }
-}
-
-void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)
-{
-    if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
-        // update buffer
-        f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
-    }
-}
-
-//------------------------------------------------------------------------------
-// huffman & bitstream functions
-//------------------------------------------------------------------------------
-
-/* F U N C T I O N S */
-
-// resets bitstream decoding
-static void
-mpc_decoder_reset_bitstream_decode(mpc_decoder *d)
-{
-    d->dword = 0;
-    d->pos = 0;
-    d->Zaehler = 0;
-    d->WordsRead = 0;
-}
-
-// reports the number of read bits
-static mpc_uint32_t
-mpc_decoder_bits_read(mpc_decoder *d)
-{
-    return 32 * d->WordsRead + d->pos;
-}
-
-// read desired number of bits out of the bitstream (max 31)
-static mpc_uint32_t
-mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits)
-{
-    mpc_uint32_t out = d->dword;
-
-    d->pos += bits;
-
-    if (d->pos < 32) {
-        out >>= (32 - d->pos);
-    } else {
-        d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]);
-        d->pos -= 32;
-        if (d->pos) {
-            out <<= d->pos;
-            out |= d->dword >> (32 - d->pos);
-        }
-        d->WordsRead++;
-    }
-
-    return out & ((1 << bits) - 1);
-}
-
-// basic huffman decoding routine
-// works with maximum lengths up to max_length
-static mpc_int32_t
-mpc_decoder_huffman_decode(mpc_decoder *d, const mpc_huffman *Table,
-                           const mpc_uint32_t max_length)
-{
-    // load preview and decode
-    mpc_uint32_t code = d->dword << d->pos;
-    if (32 - d->pos < max_length)
-        code |= SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]) >> (32 - d->pos);
-
-    while (code < Table->Code) Table++;
-
-    // set the new position within bitstream without performing a dummy-read
-    if ((d->pos += Table->Length) >= 32) {
-        d->pos -= 32;
-        d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]);
-        d->WordsRead++;
-    }
-
-    return Table->Value;
-}
-
-void mpc_decoder_get_block(mpc_decoder *d, mpc_block_t * p_block)
-{
-	unsigned char tmp;
-	unsigned int size = 2;
-
-	p_block->size = 0;
-	p_block->key[0] = mpc_decoder_bitstream_read(d, 8);
-	p_block->key[1] = mpc_decoder_bitstream_read(d, 8);
-	do {
-		tmp = mpc_decoder_bitstream_read(d, 8);
-		p_block->size = (p_block->size << 7) | (tmp & 0x7F);
-		size++;
-	} while((tmp & 0x80));
-
-	p_block->size -= size;
-}
-
-static void
-mpc_decoder_reset_v(mpc_decoder *d)
-{
-    memset(d->V_L, 0, sizeof d->V_L);
-    memset(d->V_R, 0, sizeof d->V_R);
-}
-
-static void
-mpc_decoder_reset_synthesis(mpc_decoder *d)
-{
-    mpc_decoder_reset_v(d);
-}
-
-static void
-mpc_decoder_reset_y(mpc_decoder *d)
-{
-    memset(d->Y_L, 0, sizeof d->Y_L);
-    memset(d->Y_R, 0, sizeof d->Y_R);
-}
-
-static void
-mpc_decoder_reset_globals(mpc_decoder *d)
-{
-    mpc_decoder_reset_bitstream_decode(d);
-
-    d->DecodedFrames  = 0;
-    d->StreamVersion  = 0;
-    d->MS_used        = 0;
-
-    memset(d->Y_L             , 0, sizeof d->Y_L              );
-    memset(d->Y_R             , 0, sizeof d->Y_R              );
-    memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
-    memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
-    memset(d->Res_L           , 0, sizeof d->Res_L            );
-    memset(d->Res_R           , 0, sizeof d->Res_R            );
-    memset(d->SCFI_L          , 0, sizeof d->SCFI_L           );
-    memset(d->SCFI_R          , 0, sizeof d->SCFI_R           );
-    memset(d->DSCF_Flag_L     , 0, sizeof d->DSCF_Flag_L      );
-    memset(d->DSCF_Flag_R     , 0, sizeof d->DSCF_Flag_R      );
-    memset(d->Q               , 0, sizeof d->Q                );
-    memset(d->MS_Flag         , 0, sizeof d->MS_Flag          );
-    memset(d->seeking_table   , 0, sizeof d->seeking_table    );
-}
-
-mpc_uint32_t
-mpc_decoder_decode_frame(mpc_decoder *d, mpc_uint32_t *in_buffer,
-                         mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer)
-{
-  mpc_decoder_reset_bitstream_decode(d);
-  if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher);
-  memcpy(d->Speicher, in_buffer, in_len);
-  d->dword = SWAP(d->Speicher[0]);
-  switch (d->StreamVersion) {
-    case 0x07:
-    case 0x17:
-        mpc_decoder_read_bitstream_sv7(d, MPC_FALSE);
-        break;
-    default:
-        return (mpc_uint32_t)(-1);
-  }
-  mpc_decoder_requantisierung(d, d->Max_Band);
-  mpc_decoder_synthese_filter_float(d, out_buffer);
-  return mpc_decoder_bits_read(d);
-}
-
-static mpc_uint32_t
-mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer)
-{
-    mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH;
-    mpc_uint32_t FwdJumpInfo = 0;
-    mpc_uint32_t  FrameBitCnt = 0;
-
-    if (d->DecodedFrames >= d->OverallFrames) {
-        return (mpc_uint32_t)(-1);                           // end of file -> abort decoding
-    }
-
-    // add seeking info
-    if (d->seeking_table_frames < d->DecodedFrames &&
-       (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) {
-        d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d);
-        d->seeking_table_frames = d->DecodedFrames;
-    }
-
-    switch (d->StreamVersion) {
-    case 0x07:
-    case 0x17:
-		// read jump-info for validity check of frame
-		FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);
-    	// decode data and check for validity of frame
-		FrameBitCnt = mpc_decoder_bits_read(d);
-        mpc_decoder_read_bitstream_sv7(d, MPC_FALSE);
-		d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == FwdJumpInfo;
-        break;
-	case 0x08:
-		if (d->BlockBits < 8){
-			mpc_block_t block;
-			// trouver le block
-			mpc_decoder_bitstream_read(d, d->BlockBits);
-			while (1) {
-				mpc_decoder_get_block(d, &block);
-				if (memcmp(block.key, "AD", 2) != 0)
-					mpc_decoder_seek(d, mpc_decoder_bits_read(d) + block.size * 8);
-				else
-					break;
-			}
-			d->BlockBits = block.size * 8;
+void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r);
+static void mpc_decoder_requantisierung(mpc_decoder *d);
+
+static void mpc_decoder_reset_y(mpc_decoder *d)
+{
+	memset(d->Y_L, 0, sizeof d->Y_L);
+	memset(d->Y_R, 0, sizeof d->Y_R);
+}
+
+void mpc_decoder_setup(mpc_decoder *d)
+{
+	memset(d, 0, sizeof *d);
+
+	d->__r1 = 1;
+	d->__r2 = 1;
+	d->samples_to_skip = MPC_DECODER_SYNTH_DELAY;
+
+	mpc_decoder_init_quant(d, 1.0f);
+}
+
+void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
+{
+	d->StreamVersion      = si->stream_version;
+	d->MS_used            = si->ms;
+	d->Max_Band           = si->max_band;
+	d->OverallFrames      = si->frames;
+	d->TrueGaplessPresent = si->is_true_gapless;
+	d->SampleRate         = si->sample_freq;
+}
+
+mpc_decoder * mpc_decoder_init(mpc_streaminfo *si)
+{
+	mpc_decoder* p_tmp = malloc(sizeof(mpc_decoder));
+
+	if (p_tmp != 0) {
+		mpc_decoder_setup(p_tmp);
+		mpc_decoder_set_streaminfo(p_tmp, si);
+	}
+
+	return p_tmp;
+}
+
+void mpc_decoder_exit(mpc_decoder *d)
+{
+	free(d);
+}
+
+void mpc_decoder_decode_frame(mpc_decoder * d,
+							  mpc_bits_reader * r,
+							  mpc_frame_info * i)
+{
+	// FIXME : sauter dÃ©codage si d->samples_to_skip > MPC_FRAME_LENGTH pour sv7
+	switch (d->StreamVersion) {
+		case 0x07:
+		case 0x17:
+		case 0x08:
+			i->channels = 2;
+			i->sample_freq = d->SampleRate;
+			i->samples = MPC_FRAME_LENGTH;
+			mpc_decoder_read_bitstream_sv7(d, r);
+			break;
+	}
+
+	// synthesize signal
+	mpc_decoder_requantisierung(d);
+	mpc_decoder_synthese_filter_float(d, i->buffer);
+
+	d->DecodedFrames++;
+
+//     // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
+// 	if (d->DecodedFrames == d->OverallFrames  && d->StreamVersion >= 6) {
+//         // reconstruct exact filelength
+// 		mpc_int32_t  mod_block   = mpc_bits_read(r,  11);
+// 		mpc_int32_t  FilterDecay;
+//
+// 		if (mod_block == 0) {
+//             // Encoder bugfix
+// 			mod_block = 1152;
+// 		}
+// 		FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH;
+//
+//         // additional FilterDecay samples are needed for decay of synthesis filter
+// 		if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
+// 			if (!d->TrueGaplessPresent) {
+// 				mpc_decoder_reset_y(d);
+// 			} else {
+// 				mpc_bits_read(r, 20);
+// 				mpc_decoder_read_bitstream_sv7(d, FALSE);
+// 				mpc_decoder_requantisierung(d, d->Max_Band);
+// 			}
+//
+// 			mpc_decoder_synthese_filter_float(d, buffer + 2304);
+//
+// 			i->samples = MPC_FRAME_LENGTH + FilterDecay;
+// 		}
+// 		else {                              // there are only FilterDecay samples needed for this frame
+// 			i->samples = FilterDecay;
+// 		}
+// 	}
+
+	if (d->samples_to_skip) {
+		if (i->samples < d->samples_to_skip) {
+			d->samples_to_skip -= i->samples;
+			i->samples = 0;
+		} else {
+			i->samples -= d->samples_to_skip;
+			memmove(i->buffer, i->buffer + d->samples_to_skip * i->channels,
+					i->samples * i->channels * sizeof (MPC_SAMPLE_FORMAT));
+			d->samples_to_skip = 0;
 		}
-		FrameBitCnt = mpc_decoder_bits_read(d);
-		mpc_decoder_read_bitstream_sv7(d, MPC_FALSE);
-		d->BlockBits -= mpc_decoder_bits_read(d) - FrameBitCnt;
-		d->FrameWasValid = 1;
-		break;
-    default:
-        return (mpc_uint32_t)(-1);
-    }
-
-    // synthesize signal
-    mpc_decoder_requantisierung(d, d->Max_Band);
-    mpc_decoder_synthese_filter_float(d, buffer);
-
-    d->DecodedFrames++;
-
-    // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
-    if (d->DecodedFrames == d->OverallFrames  && d->StreamVersion >= 6) {
-        // reconstruct exact filelength
-        mpc_int32_t  mod_block   = mpc_decoder_bitstream_read(d,  11);
-        mpc_int32_t  FilterDecay;
-
-        if (mod_block == 0) {
-            // Encoder bugfix
-            mod_block = 1152;
-        }
-        FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH;
-
-        // additional FilterDecay samples are needed for decay of synthesis filter
-        if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
-            if (!d->TrueGaplessPresent) {
-                mpc_decoder_reset_y(d);
-            } else {
-                mpc_decoder_bitstream_read(d, 20);
-                mpc_decoder_read_bitstream_sv7(d, MPC_FALSE);
-                mpc_decoder_requantisierung(d, d->Max_Band);
-            }
-
-            mpc_decoder_synthese_filter_float(d, buffer + 2304);
-
-            output_frame_length = MPC_FRAME_LENGTH + FilterDecay;
-        }
-        else {                              // there are only FilterDecay samples needed for this frame
-            output_frame_length = FilterDecay;
-        }
-    }
-
-    if (d->samples_to_skip) {
-        if (output_frame_length < d->samples_to_skip) {
-            d->samples_to_skip -= output_frame_length;
-            output_frame_length = 0;
-        }
-        else {
-            output_frame_length -= d->samples_to_skip;
-            memmove(
-                buffer,
-                buffer + d->samples_to_skip * 2,
-                output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT));
-            d->samples_to_skip = 0;
-        }
-    }
-
-    return output_frame_length;
-}
-
-mpc_uint32_t mpc_decoder_decode(
-    mpc_decoder *d,
-    MPC_SAMPLE_FORMAT *buffer,
-    mpc_uint32_t *vbr_update_acc,
-    mpc_uint32_t *vbr_update_bits)
-{
-    for(;;)
-    {
-        //const mpc_int32_t MaxBrokenFrames = 0; // PluginSettings.MaxBrokenFrames
-
-        mpc_uint32_t RING = d->Zaehler;
-        mpc_int32_t vbr_ring = (RING << 5) + d->pos;
-
-        mpc_uint32_t valid_samples = mpc_decoder_decode_internal(d, buffer);
-
-        if (valid_samples == (mpc_uint32_t)(-1) ) {
-            return 0;
-        }
-
-        /**************** ERROR CONCEALMENT *****************/
-        if (d->FrameWasValid == 0 ) {
-            // error occurred in bitstream
-            return (mpc_uint32_t)(-1);
-        }
-        else {
-            if (vbr_update_acc && vbr_update_bits) {
-                (*vbr_update_acc) ++;
-                vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
-                if (vbr_ring < 0) {
-                    vbr_ring += 524288;
-                }
-                (*vbr_update_bits) += vbr_ring;
-            }
-
-        }
-        mpc_decoder_update_buffer(d, RING);
-
-        if (valid_samples > 0) {
-            return valid_samples;
-        }
-    }
+	}
 }
 
 void
-mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band)
+mpc_decoder_requantisierung(mpc_decoder *d)
 {
     mpc_int32_t     Band;
@@ -434,4 +193,5 @@
     mpc_int32_t*    L;
     mpc_int32_t*    R;
+	const mpc_int32_t Last_Band = d->Max_Band;
 
 #ifdef MPC_FIXED_POINT
@@ -579,6 +339,5 @@
 }
 
-void
-mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking)
+void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r)
 {
     // these arrays hold decoding results for bundled quantizers (3- and 5-step)
@@ -602,8 +361,8 @@
 
     // first subband
-    *ResL = mpc_decoder_bitstream_read(d, 4);
-    *ResR = mpc_decoder_bitstream_read(d, 4);
+    *ResL = mpc_bits_read(r, 4);
+    *ResR = mpc_bits_read(r, 4);
     if (d->MS_used && !(*ResL==0 && *ResR==0)) {
-        d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1);
+        d->MS_Flag[0] = mpc_bits_read(r, 1);
     }
 
@@ -612,12 +371,12 @@
     for (n=1; n <= d->Max_Band; ++n, ++ResL, ++ResR)
     {
-        idx   = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9);
-        *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_decoder_bitstream_read(d, 4);
-
-        idx   = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9);
-        *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_decoder_bitstream_read(d, 4);
+		idx   = mpc_bits_huff_dec(r, mpc_table_HuffHdr);
+        *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_bits_read(r, 4);
+
+		idx   = mpc_bits_huff_dec(r, mpc_table_HuffHdr);
+        *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_bits_read(r, 4);
 
         if (d->MS_used && !(*ResL==0 && *ResR==0)) {
-            d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1);
+            d->MS_Flag[n] = mpc_bits_read(r, 1);
         }
 
@@ -633,6 +392,6 @@
     ResR  = d->Res_R;
     for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) {
-        if (*ResL) *L = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3);
-        if (*ResR) *R = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3);
+		if (*ResL) *L = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
+		if (*ResR) *R = mpc_bits_huff_dec(r, mpc_table_HuffSCFI);
     }
 
@@ -648,30 +407,30 @@
             {
             case 1:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[1] = (idx!=8) ? L[0] + idx : (int) mpc_bits_read(r, 6);
                 L[2] = L[1];
                 break;
             case 3:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
                 L[1] = L[0];
                 L[2] = L[1];
                 break;
             case 2:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
                 L[1] = L[0];
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[2] = (idx!=8) ? L[1] + idx : (int) mpc_bits_read(r, 6);
                 break;
             case 0:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[1] = (idx!=8) ? L[0] + idx : (int) mpc_bits_read(r, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                L[2] = (idx!=8) ? L[1] + idx : (int) mpc_bits_read(r, 6);
                 break;
             default:
@@ -690,30 +449,30 @@
             {
             case 1:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[1] = (idx!=8) ? R[0] + idx : (int) mpc_bits_read(r, 6);
                 R[2] = R[1];
                 break;
             case 3:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
                 R[1] = R[0];
                 R[2] = R[1];
                 break;
             case 2:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
                 R[1] = R[0];
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[2] = (idx!=8) ? R[1] + idx : (int) mpc_bits_read(r, 6);
                 break;
             case 0:
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
-                idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
-                R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[1] = (idx!=8) ? R[0] + idx : (int) mpc_bits_read(r, 6);
+                idx  = mpc_bits_huff_dec(r, mpc_table_HuffDSCF);
+                R[2] = (idx!=8) ? R[1] + idx : (int) mpc_bits_read(r, 6);
                 break;
             default:
@@ -729,6 +488,6 @@
     }
 
-    if (seeking == MPC_TRUE)
-        return;
+//     if (d->seeking == TRUE)
+//         return;
 
     /***************************** Samples ****************************/
@@ -756,8 +515,8 @@
             break;
         case 1:
-            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
+            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1];
             for (k=0; k<12; ++k)
             {
-                idx = mpc_decoder_huffman_decode(d, Table, 9);
+                idx = mpc_bits_huff_dec(r, Table);
                 *L++ = idx30[idx];
                 *L++ = idx31[idx];
@@ -766,8 +525,8 @@
             break;
         case 2:
-            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
+            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2];
             for (k=0; k<18; ++k)
             {
-                idx = mpc_decoder_huffman_decode(d, Table, 10);
+                idx = mpc_bits_huff_dec(r, Table);
                 *L++ = idx50[idx];
                 *L++ = idx51[idx];
@@ -776,23 +535,23 @@
         case 3:
         case 4:
-            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
+            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL];
             for (k=0; k<36; ++k)
-                *L++ = mpc_decoder_huffman_decode(d, Table, 5);
+                *L++ = mpc_bits_huff_dec(r, Table);
             break;
         case 5:
-            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
+            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL];
             for (k=0; k<36; ++k)
-                *L++ = mpc_decoder_huffman_decode(d, Table, 8);
+                *L++ = mpc_bits_huff_dec(r, Table);
             break;
         case 6:
         case 7:
-            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
+            Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL];
             for (k=0; k<36; ++k)
-                *L++ = mpc_decoder_huffman_decode(d, Table, 14);
+                *L++ = mpc_bits_huff_dec(r, Table);
             break;
         case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
             tmp = Dc[*ResL];
             for (k=0; k<36; ++k)
-                *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
+                *L++ = (mpc_int32_t)mpc_bits_read(r, Res_bit[*ResL]) - tmp;
             break;
         default:
@@ -816,8 +575,8 @@
                 break;
             case 1:
-                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
+                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1];
                 for (k=0; k<12; ++k)
                 {
-                    idx = mpc_decoder_huffman_decode(d, Table, 9);
+                    idx = mpc_bits_huff_dec(r, Table);
                     *R++ = idx30[idx];
                     *R++ = idx31[idx];
@@ -826,8 +585,8 @@
                 break;
             case 2:
-                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
+                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2];
                 for (k=0; k<18; ++k)
                 {
-                    idx = mpc_decoder_huffman_decode(d, Table, 10);
+                    idx = mpc_bits_huff_dec(r, Table);
                     *R++ = idx50[idx];
                     *R++ = idx51[idx];
@@ -836,23 +595,23 @@
             case 3:
             case 4:
-                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
+                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR];
                 for (k=0; k<36; ++k)
-                    *R++ = mpc_decoder_huffman_decode(d, Table, 5);
+                    *R++ = mpc_bits_huff_dec(r, Table);
                 break;
             case 5:
-                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
+                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR];
                 for (k=0; k<36; ++k)
-                    *R++ = mpc_decoder_huffman_decode(d, Table, 8);
+                    *R++ = mpc_bits_huff_dec(r, Table);
                 break;
             case 6:
             case 7:
-                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
+                Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR];
                 for (k=0; k<36; ++k)
-                    *R++ = mpc_decoder_huffman_decode(d, Table, 14);
+                    *R++ = mpc_bits_huff_dec(r, Table);
                 break;
             case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
                 tmp = Dc[*ResR];
                 for (k=0; k<36; ++k)
-                    *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
+                    *R++ = (mpc_int32_t)mpc_bits_read(r, Res_bit[*ResR]) - tmp;
                 break;
             default:
@@ -862,154 +621,4 @@
 }
 
-void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r)
-{
-  d->r = r;
-
-  d->MPCHeaderPos = 0;
-  d->StreamVersion = 0;
-  d->MS_used = 0;
-  d->FrameWasValid = 0;
-  d->OverallFrames = 0;
-  d->DecodedFrames = 0;
-  d->TrueGaplessPresent = 0;
-  d->WordsRead = 0;
-  d->Max_Band = 0;
-  d->SampleRate = 0;
-  d->__r1 = 1;
-  d->__r2 = 1;
-  d->BlockBits = 0;
-
-  d->Max_Band = 0;
-  d->seeking_window = FAST_SEEKING_WINDOW;
-
-  mpc_decoder_reset_bitstream_decode(d);
-  mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f);
-}
-
-static mpc_uint32_t get_initial_fpos(mpc_decoder *d)
-{
-    mpc_uint32_t fpos = 0;
-    switch ( d->StreamVersion ) {   // setting position to the beginning of the data-bitstream
-    case  0x04: fpos =  48; break;
-    case  0x05:
-    case  0x06: fpos =  64; break;
-    case  0x07:
-    case  0x17: fpos = 200; break;
-	case  0x08: fpos = 32; break;
-    }
-    return fpos;
-}
-
-void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
-{
-    mpc_decoder_reset_synthesis(d);
-    mpc_decoder_reset_globals(d);
-
-    d->StreamVersion      = si->stream_version;
-    d->MS_used            = si->ms;
-    d->Max_Band           = si->max_band;
-    d->OverallFrames      = si->frames;
-    d->MPCHeaderPos       = si->header_position;
-    d->TrueGaplessPresent = si->is_true_gapless;
-    d->SampleRate         = (mpc_int32_t)si->sample_freq;
-
-    d->samples_to_skip = MPC_DECODER_SYNTH_DELAY;
-}
-
-mpc_status mpc_decoder_init(mpc_decoder **d, mpc_reader *r, mpc_streaminfo *si)
-{
-    mpc_decoder* p_tmp;
-
-    p_tmp = malloc(sizeof *p_tmp);
-    memset(p_tmp, 0, sizeof *p_tmp);
-    mpc_decoder_setup(p_tmp, r);
-
-    mpc_decoder_set_streaminfo(p_tmp, si);
-
-    // AB: setting position to the beginning of the data-bitstream
-    mpc_decoder_seek(p_tmp, get_initial_fpos(p_tmp));
-
-    p_tmp->seeking_pwr = 0;
-    while (p_tmp->OverallFrames > (SEEKING_TABLE_SIZE << p_tmp->seeking_pwr))
-        p_tmp->seeking_pwr++;
-    p_tmp->seeking_table_frames = 0;
-    p_tmp->seeking_table[0] = get_initial_fpos(p_tmp);
-
-    *d = p_tmp;
-    return MPC_STATUS_OK;
-}
-
-void mpc_decoder_exit(mpc_decoder *d)
-{
-    free(d);
-}
-
-void mpc_decoder_set_seeking(mpc_decoder *d, mpc_streaminfo *si, mpc_bool_t fast_seeking)
-{
-    d->seeking_window = FAST_SEEKING_WINDOW;
-    if (si->fast_seek == 0 && fast_seeking == 0)
-        d->seeking_window = SLOW_SEEKING_WINDOW;
-}
-
-mpc_bool_t mpc_decoder_seek_seconds(mpc_decoder *d, double seconds)
-{
-    return mpc_decoder_seek_sample(d, (mpc_int64_t)(seconds * (double)d->SampleRate + 0.5));
-}
-
-mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample)
-{
-    mpc_uint32_t fpos;
-    mpc_uint32_t fwd;
-
-    fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH);
-    d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH);
-
-    // resetting synthesis filter to avoid "clicks"
-    mpc_decoder_reset_synthesis(d);
-
-    // prevent from desired position out of allowed range
-    fwd = fwd < d->OverallFrames  ?  fwd  :  d->OverallFrames;
-
-    if (fwd > d->DecodedFrames + d->seeking_window || fwd < d->DecodedFrames) {
-        memset(d->SCF_Index_L, 1, sizeof d->SCF_Index_L );
-        memset(d->SCF_Index_R, 1, sizeof d->SCF_Index_R );
-    }
-
-    if (d->seeking_table_frames > d->DecodedFrames || fwd < d->DecodedFrames) {
-        d->DecodedFrames = 0;
-        if (fwd > d->seeking_window)
-            d->DecodedFrames = (fwd - d->seeking_window) & (-1 << d->seeking_pwr);
-        if (d->DecodedFrames > d->seeking_table_frames)
-            d->DecodedFrames = d->seeking_table_frames;
-        fpos = d->seeking_table[d->DecodedFrames >> d->seeking_pwr];
-        mpc_decoder_seek(d, fpos);
-    }
-
-    // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping)
-    for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) {
-        mpc_uint32_t RING = d->Zaehler;
-        mpc_uint32_t FwdJumpInfo;
-
-        // add seeking info
-        if (d->seeking_table_frames < d->DecodedFrames &&
-           (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) {
-            d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d);
-            d->seeking_table_frames = d->DecodedFrames;
-        }
-
-        FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20) + mpc_decoder_bits_read(d);    // read jump-info
-
-        if (fwd <= d->DecodedFrames + d->seeking_window) {
-            if (d->StreamVersion >= 7)
-                mpc_decoder_read_bitstream_sv7(d, MPC_TRUE);
-            else
-                return MPC_FALSE;
-        }
-        mpc_decoder_bitstream_jump(d, FwdJumpInfo - mpc_decoder_bits_read(d));
-
-        // update buffer
-        mpc_decoder_update_buffer(d, RING);
-    }
-
-    return MPC_TRUE;
-}
+
+
Index: libmpcdec/branches/zorg/src/mpc_demux.c
===================================================================
--- libmpcdec/branches/zorg/src/mpc_demux.c	(revision 104)
+++ libmpcdec/branches/zorg/src/mpc_demux.c	(revision 104)
@@ -0,0 +1,306 @@
+/*
+  Copyright (c) 2005, The Musepack Development Team
+  All rights reserved.
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions are
+  met:
+
+  * Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+
+  * Redistributions in binary form must reproduce the above
+  copyright notice, this list of conditions and the following
+  disclaimer in the documentation and/or other materials provided
+  with the distribution.
+
+  * Neither the name of the The Musepack Development Team nor the
+  names of its contributors may be used to endorse or promote
+  products derived from this software without specific prior
+  written permission.
+
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <mpcdec/streaminfo.h>
+#include <mpcdec/mpcdec.h>
+#include "internal.h"
+#include "decoder.h"
+
+// mpc_bits_reader.c
+mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits);
+int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block);
+
+// streaminfo.c
+mpc_status streaminfo_read_header_sv8(mpc_streaminfo* si, const mpc_bits_reader * r_in);
+mpc_status streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_bits_reader * r_in);
+
+#define MAX_FRAME_SIZE 4352
+
+enum {
+	MPC_BUFFER_CLEAR = 1,
+	MPC_BUFFER_SWAP = 2,
+	MPC_BUFFER_FULL = 4,
+};
+
+static mpc_uint32_t
+mpc_demux_fill(mpc_demux * d, mpc_uint32_t min_bytes, int flags)
+{
+	mpc_uint32_t unread_bytes = d->bytes_total + d->buffer - d->bits_reader.buff
+			- ((8 - d->bits_reader.count) >> 3);
+
+	if (min_bytes == 0 || min_bytes > DEMUX_BUFFER_SIZE ||
+		    (unread_bytes < min_bytes && flags & MPC_BUFFER_FULL))
+		min_bytes = DEMUX_BUFFER_SIZE;
+
+	if (unread_bytes < min_bytes) {
+		mpc_uint32_t bytes2read = min_bytes - unread_bytes;
+		mpc_uint32_t bytes_free = DEMUX_BUFFER_SIZE - d->bytes_total;
+
+		if (flags & MPC_BUFFER_SWAP)
+			bytes2read &= -1 << 2;
+
+		if (bytes2read > bytes_free) {
+			if (d->bits_reader.count == 0) {
+				d->bits_reader.count = 8;
+				d->bits_reader.buff++;
+			}
+			memmove(d->buffer, d->bits_reader.buff, unread_bytes);
+			d->bits_reader.buff = d->buffer;
+			d->bytes_total = unread_bytes;
+		}
+		bytes2read = d->r->read(d->r, d->buffer + d->bytes_total, bytes2read);
+		if (flags & MPC_BUFFER_SWAP){
+			unsigned int i, * tmp = (unsigned int *) (d->buffer + d->bytes_total);
+			for(i = 0 ;i < (bytes2read >> 2); i++)
+				tmp[i] = mpc_swap32(tmp[i]);
+		}
+		d->bytes_total += bytes2read;
+		return bytes2read;
+	}
+
+	return 0;
+}
+
+static mpc_status mpc_demux_header(mpc_demux * d)
+{
+	char magic[4];
+	int err;
+
+    // get header position
+	err = d->si.header_position = mpc_skip_id3v2(d->r);
+	if(err < 0) return MPC_STATUS_FILE;
+
+	// seek to first byte of mpc data
+	err = d->r->seek(d->r, d->si.header_position);
+	if(!err) return MPC_STATUS_FILE;
+
+	memset(&d->si, 0, sizeof d->si);
+	mpc_demux_fill(d, 4, 0);
+	magic[0] = mpc_bits_read(&d->bits_reader, 8);
+	magic[1] = mpc_bits_read(&d->bits_reader, 8);
+	magic[2] = mpc_bits_read(&d->bits_reader, 8);
+	magic[3] = mpc_bits_read(&d->bits_reader, 8);
+
+	if (memcmp(magic, "MP+", 3) == 0) {
+		d->si.stream_version = magic[3];
+		if ((d->si.stream_version & 15) == 7) {
+			mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP); // header block size + endian convertion
+			streaminfo_read_header_sv7(&d->si, &d->bits_reader);
+		} else {
+			return MPC_STATUS_INVALIDSV;
+		}
+	} else if (memcmp(magic, "MPCK", 4) == 0) {
+		mpc_block b;
+		int size;
+		mpc_demux_fill(d, 11, 0); // max header block size
+		size = mpc_bits_get_block(&d->bits_reader, &b);
+		while( memcmp(b.key, "AD", 2) != 0 ){ // scan all blocks until audio
+			mpc_demux_fill(d, 11 + b.size, 0);
+			if (memcmp(b.key, "SI", 2) == 0)
+				streaminfo_read_header_sv8(&d->si, &d->bits_reader);
+			d->bits_reader.buff += b.size;
+			size = mpc_bits_get_block(&d->bits_reader, &b);
+		}
+		d->bits_reader.buff -= size;
+	} else
+		return MPC_STATUS_INVALIDSV;
+
+	return MPC_STATUS_OK;
+}
+
+mpc_demux * mpc_demux_init(mpc_reader * p_reader)
+{
+	mpc_demux* p_tmp = malloc(sizeof(mpc_demux));
+
+	if (p_tmp != 0) {
+		p_tmp->r = p_reader;
+		p_tmp->bytes_total = 0;
+		p_tmp->bits_reader.buff = p_tmp->buffer;
+		p_tmp->bits_reader.count = 8;
+		p_tmp->block_bits = 0;
+		// lire entÃªte
+		mpc_demux_header(p_tmp);
+		// initialisation decodeur
+		p_tmp->d = mpc_decoder_init(&p_tmp->si);
+	}
+
+	return p_tmp;
+}
+
+void mpc_demux_exit(mpc_demux * d)
+{
+	mpc_decoder_exit(d->d);
+	free(d);
+}
+
+void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i)
+{
+	memcpy(i, &d->si, sizeof d->si);
+}
+
+void mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)
+{
+	i->samples = 0;
+	if (d->d->OverallFrames != 0 && d->d->DecodedFrames == d->d->OverallFrames)
+		return;
+	if (d->si.stream_version == 8) {
+		mpc_bits_reader r;
+		if (d->block_bits < 8){
+			mpc_block b;
+			d->bits_reader.count &= -8;
+			mpc_demux_fill(d, 11, 0); // max header block size
+			mpc_bits_get_block(&d->bits_reader, &b);
+			while( memcmp(b.key, "AD", 2) != 0 ){ // scan all blocks until audio
+				mpc_demux_fill(d, 11 + b.size, 0);
+				d->bits_reader.buff += b.size;
+				mpc_bits_get_block(&d->bits_reader, &b);
+			}
+			d->block_bits = b.size * 8;
+		}
+
+		mpc_demux_fill(d, (d->block_bits >> 3) + 1, 0);
+		r = d->bits_reader;
+		mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
+		d->block_bits -= (d->bits_reader.buff - r.buff) * 8 + r.count - d->bits_reader.count;
+	} else {
+		int frame_size;
+		mpc_bits_reader r;
+		mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP);
+		frame_size = mpc_bits_read(&d->bits_reader, 20); // read frame size
+		r = d->bits_reader;
+		mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
+		if ((d->bits_reader.buff - r.buff) * 8 + r.count - d->bits_reader.count != frame_size){
+			frame_size--;
+			frame_size += 1;
+		}
+	}
+}
+
+// static mpc_uint32_t
+// mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer)
+// {
+// 	mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH;
+// 	mpc_uint32_t FwdJumpInfo = 0;
+// 	mpc_uint32_t  FrameBitCnt = 0;
+//
+// 	if (d->DecodedFrames >= d->OverallFrames) {
+// 		return (mpc_uint32_t)(-1);                           // end of file -> abort decoding
+// 	}
+//
+//     // add seeking info
+// 	if (d->seeking_table_frames < d->DecodedFrames &&
+// 		   (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) {
+// 		d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d);
+// 		d->seeking_table_frames = d->DecodedFrames;
+// 		   }
+//
+// 		   switch (d->StreamVersion) {
+// 			   case 0x07:
+// 			   case 0x17:
+// 		// read jump-info for validity check of frame
+// 				   FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);
+//     	// decode data and check for validity of frame
+// 				   FrameBitCnt = mpc_decoder_bits_read(d);
+//
+// 				   d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == FwdJumpInfo;
+// 				   break;
+// 			   case 0x08:
+// 				   if (d->BlockBits < 8){
+// 					   mpc_block block;
+// 			// trouver le block
+// 					   mpc_decoder_bitstream_read(d, d->BlockBits);
+// 					   while (1) {
+// 						   mpc_decoder_get_block(d, &block);
+// 						   if (memcmp(block.key, "AD", 2) != 0)
+// 							   mpc_decoder_seek(d, mpc_decoder_bits_read(d) + block.size * 8);
+// 						   else
+// 							   break;
+// 					   }
+// 					   d->BlockBits = block.size * 8;
+// 				   }
+// 				   FrameBitCnt = mpc_decoder_bits_read(d);
+// 				   mpc_decoder_read_bitstream_sv7(d, FALSE);
+// 				   d->BlockBits -= mpc_decoder_bits_read(d) - FrameBitCnt;
+// 				   d->FrameWasValid = 1;
+// 				   break;
+// 			   default:
+// 				   return (mpc_uint32_t)(-1);
+// 		   }
+//
+// 		   return output_frame_length;
+// }
+//
+// mpc_uint32_t mpc_decoder_decode(
+// 		mpc_decoder *d,
+//     MPC_SAMPLE_FORMAT *buffer,
+// 	mpc_uint32_t *vbr_update_acc,
+// 	mpc_uint32_t *vbr_update_bits)
+// {
+// 	for(;;)
+// 	{
+//         //const mpc_int32_t MaxBrokenFrames = 0; // PluginSettings.MaxBrokenFrames
+//
+// 		mpc_uint32_t RING = d->Zaehler;
+// 		mpc_int32_t vbr_ring = (RING << 5) + d->pos;
+//
+// 		mpc_uint32_t valid_samples = mpc_decoder_decode_internal(d, buffer);
+//
+// 		if (valid_samples == (mpc_uint32_t)(-1) ) {
+// 			return 0;
+// 		}
+//
+// 		/**************** ERROR CONCEALMENT *****************/
+// 		if (d->FrameWasValid == 0 ) {
+//             // error occurred in bitstream
+// 			return (mpc_uint32_t)(-1);
+// 		}
+// 		else {
+// 			if (vbr_update_acc && vbr_update_bits) {
+// 				(*vbr_update_acc) ++;
+// 				vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
+// 				if (vbr_ring < 0) {
+// 					vbr_ring += 524288;
+// 				}
+// 				(*vbr_update_bits) += vbr_ring;
+// 			}
+//
+// 		}
+// 		mpc_decoder_update_buffer(d, RING);
+//
+// 		if (valid_samples > 0) {
+// 			return valid_samples;
+// 		}
+// 	}
+// }
+
Index: libmpcdec/branches/zorg/src/mpc_reader.c
===================================================================
--- libmpcdec/branches/zorg/src/mpc_reader.c	(revision 85)
+++ libmpcdec/branches/zorg/src/mpc_reader.c	(revision 104)
@@ -179,63 +179,4 @@
 }
 
-mpc_status mpc_get_block(mpc_reader * p_reader, mpc_block_t * p_block)
-{
-	unsigned char tmp;
-	unsigned int size = 2;
 
-	p_block->size = 0;
-	p_reader->read(p_reader, p_block->key, 2);
-	do {
-		p_reader->read(p_reader, &tmp, 1);
-		p_block->size = (p_block->size << 7) | (tmp & 0x7F);
-		size++;
-	} while((tmp & 0x80));
 
-	if (size > p_block->size)
-		return MPC_STATUS_FILE;
-
-	p_block->size -= size;
-
-	return MPC_STATUS_OK;
-}
-
-mpc_status mpc_find_block(mpc_reader * p_reader, mpc_block_t * p_block,
-						  char * find_key, char * stop_key)
-{
-	while (1) {
-		mpc_get_block(p_reader, p_block);
-		if (memcmp(p_block->key, find_key, 2) != 0 &&
-				  memcmp(p_block->key, stop_key, 2) != 0)
-			p_reader->seek(p_reader, p_block->size);
-		else
-			break;
-	}
-
-	if (memcmp(p_block->key, find_key, 2) != 0)
-		return MPC_STATUS_FILE;
-
-	return MPC_STATUS_OK;
-}
-
-mpc_uint32_t mpc_read_bits(mpc_reader * p_reader, mpc_bits_reader * p_bits,
-						   int nb_bits)
-{
-	mpc_uint32_t ret = p_bits->buff;
-	unsigned char tmp = 0;
-
-	while(nb_bits > p_bits->bitsCount){
-		p_reader->read(p_reader, &tmp, 1);
-		ret = (ret << 8) | tmp;
-		p_bits->bitsCount += 8;
-	}
-
-	p_bits->buff = (unsigned int) ret;
-	p_bits->bitsCount -= nb_bits;
-	ret >>= p_bits->bitsCount;
-
-	if (nb_bits == 32)
-		return ret;
-	else
-		return ret & ((1 << nb_bits) - 1);
-}
-
Index: libmpcdec/branches/zorg/src/requant.c
===================================================================
--- libmpcdec/branches/zorg/src/requant.c	(revision 85)
+++ libmpcdec/branches/zorg/src/requant.c	(revision 104)
@@ -92,5 +92,5 @@
 
 void
-mpc_decoder_scale_output(mpc_decoder *d, double factor) 
+mpc_decoder_scale_output(mpc_decoder *d, double factor)
 {
     mpc_int32_t n; double f1, f2;
@@ -104,5 +104,5 @@
 
     // handles +1.58...-98.41 dB, where's scf[n] / scf[n-1] = 1.20050805774840750476
-    
+
     SET_SCF(1,factor);
 
@@ -119,5 +119,5 @@
 
 void
-mpc_decoder_initialisiere_quantisierungstabellen(mpc_decoder *d, double scale_factor) 
+mpc_decoder_init_quant(mpc_decoder *d, double scale_factor)
 {
     mpc_decoder_scale_output(d, scale_factor);
Index: libmpcdec/branches/zorg/src/streaminfo.c
===================================================================
--- libmpcdec/branches/zorg/src/streaminfo.c	(revision 85)
+++ libmpcdec/branches/zorg/src/streaminfo.c	(revision 104)
@@ -35,7 +35,11 @@
 /// Implementation of streaminfo reading functions.
 
+#include <mpcdec/mpcdec.h>
 #include <mpcdec/streaminfo.h>
 #include <stdio.h>
 #include "internal.h"
+
+mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits);
+unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size);
 
 static const char na[] = "n.a.";
@@ -53,54 +57,63 @@
 }
 
+static void
+mpc_get_encoder_string(mpc_streaminfo* si)
+{
+	if (si->encoder_version == 0) {
+		sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
+	} else {
+		switch (si->encoder_version % 10) {
+			case 0:
+				sprintf(si->encoder, "Release %u.%u", si->encoder_version / 100,
+						si->encoder_version / 10 % 10);
+				break;
+			case 2: case 4: case 6: case 8:
+				sprintf(si->encoder, "Beta %u.%02u", si->encoder_version / 100,
+						si->encoder_version % 100);
+				break;
+			default:
+				sprintf(si->encoder, "--Alpha-- %u.%02u",
+						si->encoder_version / 100, si->encoder_version % 100);
+				break;
+		}
+	}
+}
+
 /// Reads streaminfo from SV7 header.
-static mpc_status
-streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_uint32_t HeaderData[8])
+mpc_status
+streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_bits_reader * r)
 {
     mpc_uint16_t Estimatedpeak_title = 0;
 
-    if (si->stream_version > 0x71) {
-        //        Update (si->stream_version);
-        return MPC_STATUS_OK;
-    }
-
-    si->bitrate            = 0;
-    si->frames             = HeaderData[1];
-    si->is                 = 0;
-    si->ms                 = (HeaderData[2] >> 30) & 0x0001;
-    si->max_band           = (HeaderData[2] >> 24) & 0x003F;
-    si->block_size         = 1;
-    si->profile            = (HeaderData[2] << 8) >> 28;
+	si->bitrate            = 0;
+	si->block_size         = 1;
+	si->frames             = mpc_bits_read(r, 32);
+	si->is                 = mpc_bits_read(r, 1); // should be 0
+	si->ms                 = mpc_bits_read(r, 1);
+	si->max_band           = mpc_bits_read(r, 6);
+	si->profile            = mpc_bits_read(r, 4);
     si->profile_name       = mpc_get_version_string(si->profile);
-    si->sample_freq        = samplefreqs[(HeaderData[2] >> 16) & 0x0003];
-    Estimatedpeak_title    = (mpc_uint16_t) (HeaderData[2] & 0xFFFF);   // read the ReplayGain data
-    si->gain_title         = (mpc_uint16_t) ((HeaderData[3] >> 16) & 0xFFFF);
-    si->peak_title         = (mpc_uint16_t) (HeaderData[3] & 0xFFFF);
-    si->gain_album         = (mpc_uint16_t) ((HeaderData[4] >> 16) & 0xFFFF);
-    si->peak_album         = (mpc_uint16_t) (HeaderData[4] & 0xFFFF);
-    si->is_true_gapless    = (HeaderData[5] >> 31) & 0x0001; // true gapless: used?
-    si->last_frame_samples = (HeaderData[5] >> 20) & 0x07FF; // true gapless: valid samples for last frame
-    si->fast_seek          = (HeaderData[5] >> 19) & 0x0001; // fast seeking
-    si->encoder_version    = (HeaderData[6] >> 24) & 0x00FF;
+	mpc_bits_read(r, 2); // Link ?
+	si->sample_freq        = samplefreqs[mpc_bits_read(r, 2)];
+	Estimatedpeak_title    = (mpc_uint16_t) mpc_bits_read(r, 16);   // read the ReplayGain data
+	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);
+	si->is_true_gapless    = mpc_bits_read(r, 1); // true gapless: used?
+	si->last_frame_samples = mpc_bits_read(r, 11); // true gapless: valid samples for last frame
+	si->fast_seek          = mpc_bits_read(r, 1); // fast seeking
+	mpc_bits_read(r, 19); // unused
+	si->encoder_version    = mpc_bits_read(r, 8);
     si->channels           = 2;
 
-    if (si->encoder_version == 0) {
-        sprintf(si->encoder, "Buschmann 1.7.0...9, Klemm 0.90...1.05");
-    }
-    else {
-        switch (si->encoder_version % 10) {
-        case 0:
-            sprintf(si->encoder, "Release %u.%u", si->encoder_version / 100,
-                    si->encoder_version / 10 % 10);
-            break;
-        case 2: case 4: case 6: case 8:
-            sprintf(si->encoder, "Beta %u.%02u", si->encoder_version / 100,
-                    si->encoder_version % 100);
-            break;
-        default:
-            sprintf(si->encoder, "--Alpha-- %u.%02u",
-                    si->encoder_version / 100, si->encoder_version % 100);
-            break;
-        }
-    }
+	mpc_get_encoder_string(si);
+
+	// estimation, exact value needs too much time
+	si->pcm_samples     = 1152 * si->frames - 576;
+	si->average_bitrate = 0;
+	if (si->pcm_samples > 0)
+		si->average_bitrate = (si->tag_offset  - si->header_position) * 8.0
+				*  si->sample_freq / si->pcm_samples;
 
     return MPC_STATUS_OK;
@@ -108,39 +121,24 @@
 
 /// Reads streaminfo from SV8 header.
-static mpc_status
-streaminfo_read_header_sv8(mpc_streaminfo* si, mpc_reader * p_reader)
+mpc_status
+streaminfo_read_header_sv8(mpc_streaminfo* si, const mpc_bits_reader * r_in)
 {
-	int ret;
-	mpc_block_t block;
-	mpc_bits_reader bits_reader = {0, 0};
 	mpc_uint32_t CRC;
-	mpc_int32_t header_pos;
-	unsigned char tmp;
 	mpc_uint64_t sampleCount = 0;
-
-	ret = mpc_find_block(p_reader, &block, "SI", "AD");
-	if (ret != MPC_STATUS_OK)
-		return ret;
-
-	header_pos = p_reader->tell(p_reader);
+	mpc_bits_reader r = *r_in;
 
 	// FIXME : add CRC check
-	CRC = mpc_read_bits(p_reader, &bits_reader, 32);
-	si->stream_version = mpc_read_bits(p_reader, &bits_reader, 8);
-
-	do {
-		p_reader->read(p_reader, &tmp, 1);
-		sampleCount = (sampleCount << 7) | (tmp & 0x7F);
-	} while((tmp & 0x80));
-
+	CRC = mpc_bits_read(&r, 32);
+	si->stream_version = mpc_bits_read(&r, 8);
+	mpc_bits_get_size(&r, &sampleCount);
 	si->frames = (sampleCount + MPC_FRAME_LENGTH - 1) / MPC_FRAME_LENGTH;
 	si->is_true_gapless = 1;
 	si->last_frame_samples = sampleCount - (si->frames - 1) * MPC_FRAME_LENGTH;
-	si->sample_freq = samplefreqs[mpc_read_bits(p_reader, &bits_reader, 4)];
-	si->channels = mpc_read_bits(p_reader, &bits_reader, 4) + 1;
-	si->max_band = mpc_read_bits(p_reader, &bits_reader, 5) + 1;
+	si->sample_freq = samplefreqs[mpc_bits_read(&r, 4)];
+	si->channels = mpc_bits_read(&r, 4) + 1;
+	si->max_band = mpc_bits_read(&r, 5) + 1;
 	// FIXME : this flag will be removed, must be 0
-	si->is = mpc_read_bits(p_reader, &bits_reader, 1);
-	si->ms = mpc_read_bits(p_reader, &bits_reader, 1);
+	si->is = mpc_bits_read(&r, 1);
+	si->ms = mpc_bits_read(&r, 1);
 
 	si->bitrate            = 0;
@@ -152,69 +150,4 @@
 		si->average_bitrate = (si->tag_offset - si->header_position) * 8.0
 				*  si->sample_freq / si->pcm_samples;
-
-	// seek to the end of the block
-	p_reader->seek(p_reader, header_pos + block.size);
-
-	return MPC_STATUS_OK;
-}
-
-// reads file header and tags
-mpc_status
-mpc_streaminfo_init(mpc_streaminfo * si, mpc_reader *p_reader)
-{
-    mpc_uint32_t headerData[8]; int err;
-
-    memset(si, 0, sizeof *si);
-    // get header position
-    err = si->header_position = mpc_skip_id3v2(p_reader);
-    if(err < 0) return MPC_STATUS_FILE;
-
-    // seek to first byte of mpc data
-    err = p_reader->seek(p_reader, si->header_position);
-    if(!err) return MPC_STATUS_FILE;
-
-	// read magic
-	err = p_reader->read(p_reader, headerData, 4);
-	if(err != 4) return MPC_STATUS_FILE;
-
-	if (memcmp(headerData, "MP+", 3) == 0) {
-		// SV 4-7
-		err = p_reader->read(p_reader, headerData + 1, 7 * 4);
-		if(err != 7 * 4) return MPC_STATUS_FILE;
-		err = p_reader->seek(p_reader, si->header_position + 6 * 4);
-		if(!err) return MPC_STATUS_FILE;
-
-		err = p_reader->get_size(p_reader);
-		if(err < 0) return MPC_STATUS_FILE;
-		si->tag_offset = si->total_file_length = err;
-
-#ifndef MPC_LITTLE_ENDIAN
-		{
-			mpc_uint32_t ptr;
-			for (ptr = 0; ptr < 8; ptr++)
-				headerData[ptr] = mpc_swap32(headerData[ptr]);
-		}
-#endif
-		si->stream_version = headerData[0] >> 24;
-
-		if ((si->stream_version & 15) >= 8)
-			return MPC_STATUS_INVALIDSV;
-		else if ((si->stream_version & 15) == 7) {
-			err = streaminfo_read_header_sv7(si, headerData);
-			if(err < 0) return err;
-		}
-
-		// estimation, exact value needs too much time
-		si->pcm_samples     = 1152 * si->frames - 576;
-		si->average_bitrate = 0;
-		if (si->pcm_samples > 0)
-			si->average_bitrate = (si->tag_offset  - si->header_position) * 8.0
-					*  si->sample_freq / si->pcm_samples;
-	} else if (memcmp(headerData, "MPCK", 4) == 0) {
-		// SV 8
-		printf("sv8\n");
-		streaminfo_read_header_sv8(si, p_reader);
-	} else
-		return MPC_STATUS_INVALIDSV;
 
 	return MPC_STATUS_OK;
