Index: mppenc/branches/r2d/src/mppdec.h
===================================================================
--- mppenc/branches/r2d/src/mppdec.h	(revision 77)
+++ mppenc/branches/r2d/src/mppdec.h	(revision 78)
@@ -1050,8 +1050,4 @@
            Get_Synthese_Filter        ( void );
 
-// tools.c
-// FIXME : what is this ? where does it go ?
-void       Init_FPU                   ( void );
-
 // wave_out.c
 Int        Write_WAVE_Header          ( FILE_T outputFile, Ldouble SampleFreq, Uint BitsPerSample, Uint Channels, Ulong SamplesPerChannel );
Index: mppenc/branches/r2d/src/mppenc.c
===================================================================
--- mppenc/branches/r2d/src/mppenc.c	(revision 77)
+++ mppenc/branches/r2d/src/mppenc.c	(revision 78)
@@ -1408,5 +1408,5 @@
 
 
-void OverdriveReport ( mpc_encoder_t * e )
+static void OverdriveReport ( mpc_encoder_t * e )
 {
 	if ( e->Overflows > 0 ) {                                                // report internal clippings
@@ -1432,5 +1432,5 @@
 #include <fpu_control.h>
 
-void Init_FPU ( void )
+static void Init_FPU ( void )
 {
 	mpc_uint16_t  cw;
@@ -1457,4 +1457,54 @@
 }
 
+static FILE * OpenStream(char * OutputName)
+{
+	FILE * OutputFile = NULL;
+
+	/* open bitstream file */
+	if      ( 0 == strcmp ( OutputName, "/dev/null") )
+		OutputFile = fopen (DEV_NULL, "wb");
+	else if ( 0 == strcmp ( OutputName, "-")  ||  0 == strcmp ( OutputName, "/dev/stdout") )
+		OutputFile = SETBINARY_OUT (stdout);
+	else
+		switch ( WriteMode ) {
+			default:
+				stderr_printf ( "\033[33;41;1mERROR\033[0m: Invalid Write mode, internal error\n" );
+				exit(1);
+			case MODE_NEVER_OVERWRITE:
+				OutputFile = fopen ( OutputName, "rb" );
+				if ( OutputFile != NULL ) {
+					fclose ( OutputFile );
+					stderr_printf ( "\033[33;41;1mERROR\033[0m: Output file '%s' already exists\n", OutputName );
+					exit(1);
+				}
+				OutputFile = fopen ( OutputName, "w+b" );
+				break;
+			case MODE_OVERWRITE:
+				OutputFile = fopen ( OutputName, "w+b" );
+				break;
+			case MODE_ASK_FOR_OVERWRITE:
+				OutputFile = fopen ( OutputName, "rb" );
+				if ( OutputFile != NULL ) {
+					char c;
+					fclose ( OutputFile );
+					stderr_printf ( "\nmppenc: Output file '%s' already exists, overwrite (Y/n)? ", OutputName );
+					c = waitkey ();
+					if ( c != 'Y'  &&  c != 'y' ) {
+						stderr_printf ( "No!!!\n\n*** Canceled overwrite ***\n" );
+						exit(1);
+					}
+					stderr_printf ( " YES\n" );
+				}
+				OutputFile = fopen ( OutputName, "w+b" );
+				break;
+		}
+
+	if ( OutputFile == NULL ) {
+		stderr_printf ( "\033[33;41;1mERROR\033[0m: Could not create output file '%s'\n", OutputName );
+		exit(1);
+	}
+	return OutputFile;
+}
+
 static int
 mainloop ( int argc, char** argv )
@@ -1468,14 +1518,9 @@
     unsigned int     CurrentRead      =    0;   // current read Samples per channel
     unsigned int     N;                         // counter for processed frames
-    unsigned int     LastValidSamples =    0;   // number of valid samples for the last frame
-    unsigned int     LastValidFrame   =    0;   // overall number of frames
     char*            InputName        = NULL;   // Name of WAVE file
     char*            OutputName       = NULL;   // Name of bitstream file
-    FILE*            OutputFile       = NULL;   // Filepointer to output file
     int              Silence          =    0;
     int              OldSilence       =    0;
     time_t           T;
-    UintMax_t        OldBufferedBits;
-    BitstreamPos     bitstreampos;
     int              TransientL [PART_SHORT];   // Flag of transient detection
     int              TransientR [PART_SHORT];   // Flag of transient detection
@@ -1491,5 +1536,5 @@
 	Init_Psychoakustik (&m);
 	Init_FPU ();
-	Init_SV7 (&e);
+	Init_SV8 (&e);
 
     // initialize PCM-data
@@ -1556,53 +1601,5 @@
     }
 
-    /* open bitstream file */
-    if      ( 0 == strcmp ( OutputName, "/dev/null") ) {
-        OutputFile = fopen (DEV_NULL, "wb");
-    }
-    else if ( 0 == strcmp ( OutputName, "-")  ||  0 == strcmp ( OutputName, "/dev/stdout") ) {
-        OutputFile = SETBINARY_OUT (stdout);
-    }
-    else
-        switch ( WriteMode ) {
-        default:
-            stderr_printf ( "\033[33;41;1mERROR\033[0m: Invalid Write mode, internal error\n" );
-            return 1;
-        case MODE_NEVER_OVERWRITE:
-            OutputFile = fopen ( OutputName, "rb" );
-            if ( OutputFile != NULL ) {
-                fclose ( OutputFile );
-                stderr_printf ( "\033[33;41;1mERROR\033[0m: Output file '%s' already exists\n", OutputName );
-                return 1;
-            }
-            OutputFile = fopen ( OutputName, "w+b" );
-            break;
-        case MODE_OVERWRITE:
-            OutputFile = fopen ( OutputName, "w+b" );
-            break;
-        case MODE_ASK_FOR_OVERWRITE:
-            OutputFile = fopen ( OutputName, "rb" );
-            if ( OutputFile != NULL ) {
-                char c;
-                fclose ( OutputFile );
-                stderr_printf ( "\nmppenc: Output file '%s' already exists, overwrite (Y/n)? ", OutputName );
-                c = waitkey ();
-                if ( c != 'Y'  &&  c != 'y' ) {
-                    stderr_printf ( "No!!!\n\n*** Canceled overwrite ***\n" );
-                    return 1;
-                }
-                                stderr_printf ( " YES\n" );
-            }
-            OutputFile = fopen ( OutputName, "w+b" );
-            break;
-        }
-
-    if ( OutputFile == NULL ) {
-        stderr_printf ( "\033[33;41;1mERROR\033[0m: Could not create output file '%s'\n", OutputName );
-        return 1;
-    }
-
-#ifndef IO_BUFFERING
-    setvbuf ( OutputFile, NULL, _IONBF, 0 );
-#endif
+	e.outputFile = OpenStream(OutputName);
 
     ShowParameters (&m, InputName, OutputName );
@@ -1624,12 +1621,14 @@
 
 	e.MS_Channelmode = m.MS_Channelmode;
-    e.BufferedBits     = 0;
-    LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
-    LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
-    WriteHeader_SV7 ( &e, m.Max_Band, m.MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, m.PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
+//     e.BufferedBits     = 0;
+	writeMagic(&e);
+	WriteHeader_SV8 ( &e, m.Max_Band, m.MS_Channelmode > 0, SamplesInWAVE,
+					   0x08, m.PNS > 0, m.SampleFreq,
+					   Wave.Channels > 2 ? 2 : Wave.Channels);
+	writeBlock(&e, "SI", TRUE);
 
 
     // initialize timer
-    ShowProgress (&m, 0, SamplesInWAVE, e.BufferedBits );
+    ShowProgress (&m, 0, SamplesInWAVE, e.outputBits );
     T            = time ( NULL );
 
@@ -1653,10 +1652,4 @@
 						UintMAX_FP(AllSamplesRead) / m.SampleFreq, UintMAX_FP(SamplesInWAVE) / m.SampleFreq );
         SamplesInWAVE = AllSamplesRead;
-
-        // in the case of a broken wav-header, recalculate the overall frames
-        // and the valid samples for the last frame
-        LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
-        LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
-        // fprintf ( stderr, "\nKorrupt WAV file in Frame %d: NEU!: Frames: %u, last valid: %u\n", -1, LastValidFrame, LastValidSamples );
     }
 
@@ -1708,19 +1701,10 @@
         }
 
-        if ( e.Zaehler >= BUFFER_ALMOST_FULL  ||  LowDelay ) {
-            FlushBitstream ( OutputFile, e.Buffer, e.Zaehler );
-            e.Zaehler = 0;
-         }
-
         OldSilence      = Silence;
-        OldBufferedBits = e.BufferedBits;
-        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
             T += labs (DisplayUpdateTime);
-            ShowProgress (&m, (UintMax_t)(N+1) * BLOCK, SamplesInWAVE, e.BufferedBits );
+			ShowProgress (&m, (UintMax_t)(N+1) * BLOCK, SamplesInWAVE, e.outputBits );
         }
 
@@ -1742,34 +1726,14 @@
 							UintMAX_FP(AllSamplesRead) / m.SampleFreq, UintMAX_FP(SamplesInWAVE) / m.SampleFreq );
             SamplesInWAVE = AllSamplesRead;
-
-            // in the case of broken wav-header, recalculate the overall frames
-            // and the valid samples for the last frame
-            LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
-            LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
-            // fprintf ( stderr, "\nKorrupt WAV file in Frame %d: NEU!: Frames: %u, last valid: %u\n", N, LastValidFrame, LastValidSamples );
-        }
-
-        if ( N == LastValidFrame - 1 ) {
-            WriteBits ( &e, LastValidSamples, 11 );
-            // fprintf ( stderr, "\nGltige Samples im letzten Frame: %4u   \n", LastValidSamples );
-        }
-        if ( N >= LastValidFrame ) {
-            // fprintf ( stderr, "Zusï¿œzlicher Frame %u (von %u) angehï¿œgt.   \n", N, LastValidFrame );
-        }
-
-    }
-
-    // write the last incomplete word to buffer, so it's written during the next flush
-    FinishBitstream(&e);
-    ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.BufferedBits );
-
-    FlushBitstream ( OutputFile, e.Buffer, e.Zaehler );
-    e.Zaehler = 0;
-
-    UpdateHeader ( OutputFile, LastValidFrame, LastValidSamples );
+        }
+    }
+
+    // write the last incomplete block
+	writeBlock(&e, "AD", FALSE);
+    ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.outputBits );
 
     if(EnableTags)
-        FinalizeTags ( OutputFile, APE_Version );
-    fclose ( OutputFile );
+        FinalizeTags ( e.outputFile, APE_Version );
+    fclose ( e.outputFile );
     fclose ( Wave.fp );
 
Index: mppenc/branches/r2d/src/mppenc.h
===================================================================
--- mppenc/branches/r2d/src/mppenc.h	(revision 77)
+++ mppenc/branches/r2d/src/mppenc.h	(revision 78)
@@ -32,6 +32,4 @@
 #include "mppdec.h"
 
-//#define IO_BUFFERING                          // activates IO-buffer (default: off)
-
 #define WIN32_MESSAGES      1                   // support Windows-Messaging to Frontend
 
@@ -57,9 +55,4 @@
 
 // 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 );
 
 void   Init_FFT      ( PsyModel* );
@@ -112,8 +105,15 @@
 
 // FIXME : put in lib header
-void         Init_SV7             ( mpc_encoder_t* );
-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         Init_SV8             ( mpc_encoder_t* );
+void WriteHeader_SV8 ( mpc_encoder_t*e, const unsigned int  MaxBand,
+					   const unsigned int  MS_on,
+					   const unsigned int  SamplesCount,
+					   const unsigned int  StreamVersion,
+					   const unsigned int  PNS_on,
+					   const unsigned int  SampleFreq,
+					   const unsigned int  ChannelCount);
+void writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC);
+void writeMagic(mpc_encoder_t * e);
 void         WriteBitstream_SV7   ( mpc_encoder_t*, const int, const SubbandQuantTyp* );
-void         FinishBitstream      ( mpc_encoder_t* );
 
 
