Index: mppenc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 65)
+++ mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 66)
@@ -22,4 +22,6 @@
 #include "libmpcenc.h"
 
+void  WriteBits         ( mpc_encoder_t*, const mpc_uint32_t input, const unsigned int bits );
+
 // huffsv7.c
 extern Huffman_t         HuffHdr  [10];         // contains tables for SV7-header
@@ -60,5 +62,6 @@
 // writes SV7-header
 void
-WriteHeader_SV7 ( const unsigned int  MaxBand,
+WriteHeader_SV7 ( mpc_encoder_t*e,
+				  const unsigned int  MaxBand,
                   const unsigned int  Profile,
                   const unsigned int  MS_on,
@@ -68,41 +71,41 @@
                   const unsigned int  SampleFreq )
 {
-    WriteBits ( StreamVersion,  8 );    // StreamVersion
-    WriteBits ( 0x2B504D     , 24 );    // Magic Number "MP+"
-
-    WriteBits ( TotalFrames  , 32 );    // # of frames
-
-    WriteBits ( 0            ,  1 );    // former IS-Flag (not supported anymore)
-    WriteBits ( MS_on        ,  1 );    // MS-Coding Flag
-    WriteBits ( MaxBand      ,  6 );    // Bandwidth
+    WriteBits ( e, StreamVersion,  8 );    // StreamVersion
+	WriteBits ( e, 0x2B504D     , 24 );    // Magic Number "MP+"
+
+	WriteBits ( e, TotalFrames  , 32 );    // # of frames
+
+	WriteBits ( e, 0            ,  1 );    // former IS-Flag (not supported anymore)
+	WriteBits ( e, MS_on        ,  1 );    // MS-Coding Flag
+	WriteBits ( e, MaxBand      ,  6 );    // Bandwidth
 
 #if 0
     if ( MPPENC_VERSION [3] & 1 )
-        WriteBits ( 1        ,  4 );    // 1: Experimental profile
+        WriteBits ( e, 1        ,  4 );    // 1: Experimental profile
     else
 #endif
 
-        WriteBits ( Profile  ,  4 );    // 5...15: below Telephone...above BrainDead
-    WriteBits ( 0            ,  2 );    // for future use
+        WriteBits ( e, Profile  ,  4 );    // 5...15: below Telephone...above BrainDead
+	WriteBits ( e, 0            ,  2 );    // for future use
     switch ( SampleFreq ) {
-        case 44100: WriteBits ( 0, 2 ); break;
-        case 48000: WriteBits ( 1, 2 ); break;
-        case 37800: WriteBits ( 2, 2 ); break;
-        case 32000: WriteBits ( 3, 2 ); break;
+		case 44100: WriteBits ( e, 0, 2 ); break;
+		case 48000: WriteBits ( e, 1, 2 ); break;
+		case 37800: WriteBits ( e, 2, 2 ); break;
+		case 32000: WriteBits ( e, 3, 2 ); break;
         default   : stderr_printf ( "Internal error\n");
                     exit (1);
     }
-    WriteBits ( 0            , 16 );    // maximum input sample value, currently filled by replaygain
-
-    WriteBits ( 0            , 32 );    // title based gain controls, currently filled by replaygain
-
-    WriteBits ( 0            , 32 );    // album based gain controls, currently filled by replaygain
-
-    WriteBits ( 1            ,  1 );    // true gapless: used?
-    WriteBits ( SamplesRest  , 11 );    // true gapless: valid samples in last frame
-    WriteBits ( 1            , 1 );     // we now support fast seeking
-    WriteBits ( 0            , 19 );
-
-    WriteBits ( (MPPENC_VERSION[0]&15)*100 + (MPPENC_VERSION[2]&15)*10 + (MPPENC_VERSION[3]&15),
+	WriteBits ( e, 0            , 16 );    // maximum input sample value, currently filled by replaygain
+
+	WriteBits ( e, 0            , 32 );    // title based gain controls, currently filled by replaygain
+
+	WriteBits ( e, 0            , 32 );    // album based gain controls, currently filled by replaygain
+
+	WriteBits ( e, 1            ,  1 );    // true gapless: used?
+	WriteBits ( e, SamplesRest  , 11 );    // true gapless: valid samples in last frame
+	WriteBits ( e, 1            , 1 );     // we now support fast seeking
+	WriteBits ( e, 0            , 19 );
+
+	WriteBits ( e, (MPPENC_VERSION[0]&15)*100 + (MPPENC_VERSION[2]&15)*10 + (MPPENC_VERSION[3]&15),
                                 8 );    // for future use
 }
@@ -119,10 +122,10 @@
         d = new - old + 7;                                   \
         if ( d <= 14u  && rll < 32) {                        \
-            WriteBits ( Table[d].Code, Table[d].Length );    \
+            WriteBits ( e, Table[d].Code, Table[d].Length );    \
         }                                                    \
         else {                                               \
             if ( new < 0 ) new = 0, e->Overflows++;          \
-            WriteBits ( Table[15].Code, Table[15].Length );  \
-            WriteBits ( (unsigned int)new, 6 );              \
+            WriteBits ( e, Table[15].Code, Table[15].Length );  \
+            WriteBits ( e, (unsigned int)new, 6 );              \
             rll = 0;                                         \
         }
@@ -131,10 +134,10 @@
         d = new - old + 7;                                   \
         if ( d <= 14u ) {                                    \
-            WriteBits ( Table[d].Code, Table[d].Length );    \
+            WriteBits ( e, Table[d].Code, Table[d].Length );    \
         }                                                    \
         else {                                               \
             if ( new < 0 ) new = 0, e->Overflows++;          \
-            WriteBits ( Table[15].Code, Table[15].Length );  \
-            WriteBits ( (unsigned int)new, 6 );              \
+            WriteBits ( e, Table[15].Code, Table[15].Length );  \
+            WriteBits ( e, (unsigned int)new, 6 );              \
             rll = 0;                                         \
         }
@@ -187,8 +190,8 @@
 
     /************************************ Resolution *********************************/
-    WriteBits ( (unsigned int)Res_L[0], 4 );                            // subband 0
-    WriteBits ( (unsigned int)Res_R[0], 4 );
+    WriteBits ( e, (unsigned int)Res_L[0], 4 );                            // subband 0
+	WriteBits ( e, (unsigned int)Res_R[0], 4 );
     if ( e->MS_Channelmode > 0  &&  !(Res_L[0]==0  &&  Res_R[0]==0) )
-         WriteBits ( MS_Flag[0] , 1 );
+		WriteBits ( e, MS_Flag[0] , 1 );
 
     Table = HuffHdr;                                                    // subband 1...MaxBand
@@ -198,9 +201,9 @@
         d = Res_L[n] - Res_L[n-1] + 5;
         if ( d <= 8u ) {
-            WriteBits ( Table[d].Code, Table[d].Length );
+			WriteBits ( e, Table[d].Code, Table[d].Length );
         }
         else {
-            WriteBits ( Table[9].Code, Table[9].Length );
-            WriteBits ( Res_L[n]     , 4               );
+			WriteBits ( e, Table[9].Code, Table[9].Length );
+			WriteBits ( e, Res_L[n]     , 4               );
         }
 
@@ -208,12 +211,12 @@
         d = Res_R[n] - Res_R[n-1] + 5;
         if ( d <= 8u ) {
-            WriteBits ( Table[d].Code, Table[d].Length );
+			WriteBits ( e, Table[d].Code, Table[d].Length );
         }
         else {
-            WriteBits ( Table[9].Code, Table[9].Length );
-            WriteBits ( Res_R[n]     , 4               );
+			WriteBits ( e, Table[9].Code, Table[9].Length );
+			WriteBits ( e, Res_R[n]     , 4               );
         }
         if ( e->MS_Channelmode > 0  &&  !(Res_L[n]==0 && Res_R[n]==0) )
-            WriteBits ( MS_Flag[n], 1 );
+			WriteBits ( e, MS_Flag[n], 1 );
     }
 
@@ -223,9 +226,9 @@
         if ( Res_L[n] ) {
             SCFI_L[n] = 2 * (SCF_Index_L[n][0] == SCF_Index_L[n][1]) + (SCF_Index_L[n][1] == SCF_Index_L[n][2]);
-            WriteBits ( Table[SCFI_L[n]].Code, Table[SCFI_L[n]].Length );
+			WriteBits ( e, Table[SCFI_L[n]].Code, Table[SCFI_L[n]].Length );
         }
         if ( Res_R[n] ) {
             SCFI_R[n] = 2 * (SCF_Index_R[n][0] == SCF_Index_R[n][1]) + (SCF_Index_R[n][1] == SCF_Index_R[n][2]);
-            WriteBits ( Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );
+			WriteBits ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );
         }
     }
@@ -309,9 +312,9 @@
             }
             book = sum >= 0;
-            WriteBits ( book, 1 );
+			WriteBits ( e, book, 1 );
             Table = HuffQ [book][1];
             for ( k = 0; k < 36; k += 3 ) {
                 idx = q[k+0] + 3*q[k+1] + 9*q[k+2];
-                WriteBits ( Table[idx].Code, Table[idx].Length );
+				WriteBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -325,9 +328,9 @@
             }
             book = sum >= 0;
-            WriteBits ( book, 1 );
+			WriteBits ( e, book, 1 );
             Table = HuffQ [book][2];
             for ( k = 0; k < 36; k += 2 ) {
                 idx = q[k+0] + 5*q[k+1];
-                WriteBits ( Table[idx].Code, Table[idx].Length );
+				WriteBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -344,14 +347,14 @@
             }
             book = sum >= 0;
-            WriteBits ( book, 1 );
+			WriteBits ( e, book, 1 );
             Table = HuffQ [book][Res_L[n]];
             for ( k = 0; k < 36; k++ ) {
                 idx = q[k];
-                WriteBits ( Table[idx].Code, Table[idx].Length );
+				WriteBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
         default:
             for ( k = 0; k < 36; k++ )
-                WriteBits ( q[k], Res_L[n]-1 );
+				WriteBits ( e, q[k], Res_L[n]-1 );
             break;
         }
@@ -373,9 +376,9 @@
             }
             book = sum >= 0;
-            WriteBits ( book, 1 );
+			WriteBits ( e, book, 1 );
             Table = HuffQ [book][1];
             for ( k = 0; k < 36; k += 3 ) {
                 idx = q[k+0] + 3*q[k+1] + 9*q[k+2];
-                WriteBits ( Table[idx].Code, Table[idx].Length );
+				WriteBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -389,9 +392,9 @@
             }
             book = sum >= 0;
-            WriteBits ( book, 1 );
+			WriteBits ( e, book, 1 );
             Table = HuffQ [book][2];
             for ( k = 0; k < 36; k += 2 ) {
                 idx = q[k+0] + 5*q[k+1];
-                WriteBits ( Table[idx].Code, Table[idx].Length );
+				WriteBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -408,14 +411,14 @@
             }
             book = sum >= 0;
-            WriteBits ( book, 1 );
+			WriteBits ( e, book, 1 );
             Table = HuffQ [book][Res_R[n]];
             for ( k = 0; k < 36; k++ ) {
                 idx = q[k];
-                WriteBits ( Table[idx].Code, Table[idx].Length );
+				WriteBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
         default:
             for ( k = 0; k < 36; k++ )
-                WriteBits ( q[k], Res_R[n] - 1 );
+				WriteBits ( e, q[k], Res_R[n] - 1 );
             break;
         }
Index: mppenc/branches/r2d/libmpcpsy/psy.c
===================================================================
--- mppenc/branches/r2d/libmpcpsy/psy.c	(revision 65)
+++ mppenc/branches/r2d/libmpcpsy/psy.c	(revision 66)
@@ -85,5 +85,5 @@
 
 // fft_routines.c
-void   Init_FFT      ( void );
+void   Init_FFT      ( PsyModel* );
 void   PowSpec256    ( const float*, float* );
 void   PowSpec1024   ( const float*, float* );
@@ -96,15 +96,4 @@
 {
     int  i;
-
-    // generate FFT lookup-tables with largest FFT-size of 1024
-    Init_FFT ();
-
-    // setting pre-echo variables to Ltq
-    for ( i = 0; i < PART_LONG; i++ ) {
-		m->pre_erg_L  [0][i/3] = m->pre_erg_R  [0][i/3] =
-				m->pre_erg_L  [1][i/3] = m->pre_erg_R  [1][i/3] =
-				m->tmp_Mask_L [i]   = m->tmp_Mask_R [i]   =
-				m->PreThr_L   [i]   = m->PreThr_R   [i]   = m->partLtq [i];
-    }
 
     // initializing arrays with zero
@@ -124,4 +113,17 @@
 	m->SampleFreq = 0.;
 	m->Bandwidth = 0.;
+	m->KBD1 = 2.;
+	m->KBD2 = -1.;
+
+    // generate FFT lookup-tables with largest FFT-size of 1024
+	Init_FFT (m);
+
+    // setting pre-echo variables to Ltq
+	for ( i = 0; i < PART_LONG; i++ ) {
+		m->pre_erg_L  [0][i/3] = m->pre_erg_R  [0][i/3] =
+				m->pre_erg_L  [1][i/3] = m->pre_erg_R  [1][i/3] =
+				m->tmp_Mask_L [i]   = m->tmp_Mask_R [i]   =
+				m->PreThr_L   [i]   = m->PreThr_R   [i]   = m->partLtq [i];
+	}
 
     return;
Index: mppenc/branches/r2d/mppenc.kdevelop
===================================================================
--- mppenc/branches/r2d/mppenc.kdevelop	(revision 65)
+++ mppenc/branches/r2d/mppenc.kdevelop	(revision 66)
@@ -3,5 +3,5 @@
   <general>
     <author>Nicolas Botti</author>
-    <email/>
+    <email></email>
     <version>0.1</version>
     <projectmanagement>KDevAutoProject</projectmanagement>
@@ -14,5 +14,5 @@
     <projectdirectory>.</projectdirectory>
     <absoluteprojectpath>false</absoluteprojectpath>
-    <description/>
+    <description></description>
   </general>
   <kdevautoproject>
@@ -28,4 +28,8 @@
         <mppenc/>
       </runarguments>
+      <customdirectory>/</customdirectory>
+      <programargs></programargs>
+      <autocompile>true</autocompile>
+      <envvars/>
     </run>
     <configurations>
@@ -43,14 +47,14 @@
         <cxxcompiler>kdevgppoptions</cxxcompiler>
         <f77compiler>kdevg77options</f77compiler>
-        <cflags>-O0 -g3 </cflags>
+        <cflags>-O0 -g3 -Wmissing-prototypes -Wmissing-prototypes</cflags>
         <envvars/>
-        <topsourcedir/>
+        <topsourcedir></topsourcedir>
         <cppflags>-DMPP_ENCODER=1 -DTRUE=1 -DFALSE=0</cppflags>
-        <ldflags/>
-        <ccompilerbinary/>
-        <cxxcompilerbinary/>
-        <f77compilerbinary/>
-        <cxxflags/>
-        <f77flags/>
+        <ldflags></ldflags>
+        <ccompilerbinary></ccompilerbinary>
+        <cxxcompilerbinary></cxxcompilerbinary>
+        <f77compilerbinary></f77compilerbinary>
+        <cxxflags></cxxflags>
+        <f77flags></f77flags>
       </debug>
     </configurations>
@@ -60,4 +64,9 @@
         <envvar value="1" name="WANT_AUTOMAKE_1_6" />
       </envvars>
+      <abortonerror>true</abortonerror>
+      <numberofjobs>1</numberofjobs>
+      <dontact>false</dontact>
+      <makebin></makebin>
+      <prio>0</prio>
     </make>
   </kdevautoproject>
@@ -165,5 +174,5 @@
     </codecompletion>
     <creategettersetter>
-      <prefixGet/>
+      <prefixGet></prefixGet>
       <prefixSet>set</prefixSet>
       <prefixVariable>m_,_</prefixVariable>
@@ -181,10 +190,10 @@
   <kdevdebugger>
     <general>
-      <programargs/>
-      <gdbpath/>
+      <programargs>--overwrite ~/seeking_bug.flac</programargs>
+      <gdbpath></gdbpath>
       <dbgshell>libtool</dbgshell>
-      <configGdbScript/>
-      <runShellScript/>
-      <runGdbScript/>
+      <configGdbScript></configGdbScript>
+      <runShellScript></runShellScript>
+      <runGdbScript></runGdbScript>
       <breakonloadinglibs>true</breakonloadinglibs>
       <separatetty>false</separatetty>
Index: mppenc/branches/r2d/src/mppenc.c
===================================================================
--- mppenc/branches/r2d/src/mppenc.c	(revision 65)
+++ mppenc/branches/r2d/src/mppenc.c	(revision 66)
@@ -680,5 +680,5 @@
                 memcpy ( errorL [Band], errorL[Band] + 36, MAX_NS_ORDER * sizeof (**errorL) );
             } else {
-                QuantizeSubband                 ( subq[Band].L, subx[Band].L, *resL, errorL [Band] );
+				QuantizeSubband                 ( subq[Band].L, subx[Band].L, *resL, errorL [Band], MAX_NS_ORDER );
                 memcpy ( errorL [Band], errorL[Band] + 36, MAX_NS_ORDER * sizeof (**errorL) );
             }
@@ -691,5 +691,5 @@
                 memcpy ( errorR [Band], errorR [Band] + 36, MAX_NS_ORDER * sizeof (**errorL) );
             } else {
-                QuantizeSubband                 ( subq[Band].R, subx[Band].R, *resR, errorL [Band] );
+				QuantizeSubband                 ( subq[Band].R, subx[Band].R, *resR, errorL [Band], MAX_NS_ORDER);
                 memcpy ( errorR [Band], errorR [Band] + 36, MAX_NS_ORDER * sizeof (**errorL) );
             }
@@ -1542,6 +1542,5 @@
 
 
-void
-		OverdriveReport ( mpc_encoder_t * e )
+void OverdriveReport ( mpc_encoder_t * e )
 {
 	if ( e->Overflows > 0 ) {                                                // report internal clippings
@@ -1603,4 +1602,10 @@
 	Init_ANS ();
 	Klemm    ();
+
+	e.dword = 0;
+	e.filled = 32;
+	e.Zaehler = 0;
+	e.BufferedBits = 0;
+	e.Overflows = 0;
 
     // initialize PCM-data
@@ -1734,8 +1739,9 @@
     }
 
+	e.MS_Channelmode = m.MS_Channelmode;
     e.BufferedBits     = 0;
     LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
     LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
-    WriteHeader_SV7 ( m.Max_Band, MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
+    WriteHeader_SV7 ( &e, m.Max_Band, MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
 
     // initialize timer
@@ -1824,8 +1830,8 @@
         OldSilence      = Silence;
         OldBufferedBits = e.BufferedBits;
-        GetBitstreamPos    ( &bitstreampos );
-        WriteBits          ( 0, 20 );                                                      // Reserve 20 bits for jump-information
-        WriteBitstream_SV7 ( m.Max_Band, Q );                                                // write SV7-Bitstream
-        WriteBitsAt        ( (Uint32_t)(e.BufferedBits - OldBufferedBits - 20), 20, bitstreampos );      // Patch 20 bits for jump-information to the right value
+        GetBitstreamPos    ( &e, &bitstreampos );
+        WriteBits          ( &e, 0, 20 );                                                      // Reserve 20 bits for jump-information
+        WriteBitstream_SV7 ( &e, m.Max_Band, Q );                                                // write SV7-Bitstream
+        WriteBitsAt        ( &e, (Uint32_t)(e.BufferedBits - OldBufferedBits - 20), 20, bitstreampos );      // Patch 20 bits for jump-information to the right value
 
         if ( (Int)(time (NULL) - T) >= 0 ) {                            // output
@@ -1865,5 +1871,5 @@
 
         if ( N == LastValidFrame - 1 ) {
-            WriteBits ( LastValidSamples, 11 );
+            WriteBits ( &e, LastValidSamples, 11 );
             // fprintf ( stderr, "\nGltige Samples im letzten Frame: %4u   \n", LastValidSamples );
         }
@@ -1875,5 +1881,5 @@
 
     // write the last incomplete word to buffer, so it's written during the next flush
-    FinishBitstream();
+    FinishBitstream(&e);
     ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.BufferedBits );
 
Index: mppenc/branches/r2d/src/mppenc.h
===================================================================
--- mppenc/branches/r2d/src/mppenc.h	(revision 65)
+++ mppenc/branches/r2d/src/mppenc.h	(revision 66)
@@ -74,12 +74,13 @@
 // extern UintMax_t     BufferedBits;              // counter for the number of written bits in the bitstream
 
-void  FlushBitstream    ( FILE* fp, const Uint32_t* buffer, size_t words32bit );
-void  UpdateHeader      ( FILE* fp, Uint32_t Frames, Uint ValidSamples );
-void  WriteBits         ( const Uint32_t input, const unsigned int bits );
-void  WriteBitsAt       ( const Uint32_t input, const unsigned int bits, const BitstreamPos pos );
-void  GetBitstreamPos   ( BitstreamPos* const pos );
+// FIXME : put in lib header
+void  FlushBitstream    ( FILE* fp, const mpc_uint32_t* buffer, size_t words32bit );
+void  UpdateHeader      ( FILE* fp, mpc_uint32_t Frames, Uint ValidSamples );
+void  WriteBits         ( mpc_encoder_t*, const mpc_uint32_t input, const unsigned int bits );
+void  WriteBitsAt       ( mpc_encoder_t*, const mpc_uint32_t input, const unsigned int bits, const BitstreamPos pos );
+void  GetBitstreamPos   ( mpc_encoder_t*, BitstreamPos* const pos );
 
 // cvd.c
-int    CVD2048 ( const float*, int* );
+int    CVD2048 ( PsyModel*, const float*, int* );
 
 
@@ -140,5 +141,5 @@
 float  ISNR_Schaetzer                  ( const float* samples, const float comp, const int res);
 float  ISNR_Schaetzer_Trans            ( const float* samples, const float comp, const int res);
-void   QuantizeSubband                 ( unsigned int* qu_output, const float* input, const int res, float* errors );
+void   QuantizeSubband                 ( unsigned int* qu_output, const float* input, const int res, float* errors, const int maxNsOrder );
 void   QuantizeSubbandWithNoiseShaping ( unsigned int* qu_output, const float* input, const int res, float* errors, const float* FIR );
 
@@ -146,4 +147,5 @@
 
 
+// FIXME : no more globals
 // encode_sv7.c
 extern unsigned char  MS_Flag     [32];                  // subband-wise mid/side flag
@@ -153,8 +155,9 @@
 extern int            SCF_Index_R [32] [3];              // Scalefactor-index for Bitstream
 
+// FIXME : put in lib header
 void         Init_SV7             ( void );
-void         WriteHeader_SV7      ( const unsigned int, const unsigned int, const unsigned int, const Uint32_t TotalFrames, const unsigned int SamplesRest, const unsigned int StreamVersion, const unsigned int SampleFreq );
-void         WriteBitstream_SV7   ( const int, const SubbandQuantTyp* );
-void         FinishBitstream      ( void );
+void         WriteHeader_SV7      ( mpc_encoder_t*, const unsigned int, const unsigned int, const unsigned int, const Uint32_t TotalFrames, const unsigned int SamplesRest, const unsigned int StreamVersion, const unsigned int SampleFreq );
+void         WriteBitstream_SV7   ( mpc_encoder_t*, const int, const SubbandQuantTyp* );
+void         FinishBitstream      ( mpc_encoder_t* );
 
 
