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;
-}
+
+
