Index: mppenc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- mppenc/branches/r2d/src/bitstream.c	(revision 60)
+++ mppenc/branches/r2d/libmpcenc/bitstream.c	(revision 64)
@@ -18,12 +18,6 @@
  */
 
-#include "mppenc.h"
-
-
-Uint32_t      Buffer [BUFFER_FULL];    // Buffer for bitstream-file
-Uint32_t      dword         =  0;      // 32-bit-Word for Bitstream-I/O
-int           filled        = 32;      // Position in the the 32-bit-word that's currently about to be filled
-unsigned int  Zaehler       =  0;      // Position pointer for the processed bitstream-word (32 bit)
-UintMax_t     BufferedBits  =  0;      // Counter for the number of written bits in the bitstream
+#include "libmpcenc.h"
+#include "stdio.h"
 
 
@@ -39,5 +33,5 @@
 
 static void
-Change_Endian32 ( unsigned int* dst, size_t words32bit )
+Change_Endian32 ( unsigned int* dst, mpc_size_t words32bit )
 {
     for ( ; words32bit--; dst++ ) {
@@ -64,15 +58,15 @@
 
 void
-FlushBitstream ( FILE* fp, const Uint32_t* buffer, size_t words32bit )
+FlushBitstream ( FILE* fp, const mpc_uint32_t* buffer, mpc_size_t words32bit )
 {
-    size_t           WrittenDwords = 0;
-    const Uint32_t*  p             = buffer;
-    size_t           CC            = words32bit;
+    mpc_size_t           WrittenDwords = 0;
+    const mpc_uint32_t*  p             = buffer;
+    mpc_size_t           CC            = words32bit;
 
 #if ENDIAN == HAVE_BIG_ENDIAN
-    Change_Endian32 ( (Uint32_t*)buffer, CC );
+    Change_Endian32 ( (mpc_uint32_t*)buffer, CC );
 #endif
 
-    // Write Buffer
+    // Write e->Buffer
     do {
         WrittenDwords = fwrite ( p, sizeof(*buffer), words32bit, fp );
@@ -88,5 +82,5 @@
 
 #if ENDIAN == HAVE_BIG_ENDIAN
-    Change_Endian32 ( (Uint32_t*)buffer, CC );
+    Change_Endian32 ( (mpc_uint32_t*)buffer, CC );
 #endif
 }
@@ -94,7 +88,7 @@
 
 void
-UpdateHeader ( FILE* fp, Uint32_t Frames, Uint ValidSamples )
+UpdateHeader ( FILE* fp, mpc_uint32_t Frames, mpc_uint_t ValidSamples )
 {
-    Uint8_t  buff [4];
+    mpc_uint8_t  buff [4];
 
     // Write framecount to header
@@ -102,8 +96,8 @@
         return;
 
-    buff [0] = (Uint8_t)(Frames >>  0);
-    buff [1] = (Uint8_t)(Frames >>  8);
-    buff [2] = (Uint8_t)(Frames >> 16);
-    buff [3] = (Uint8_t)(Frames >> 24);
+    buff [0] = (mpc_uint8_t)(Frames >>  0);
+    buff [1] = (mpc_uint8_t)(Frames >>  8);
+    buff [2] = (mpc_uint8_t)(Frames >> 16);
+    buff [3] = (mpc_uint8_t)(Frames >> 24);
 
     fwrite ( buff, 1, 4, fp );
@@ -119,7 +113,7 @@
 
     ValidSamples <<= 4;
-    ValidSamples  |= 0x800F & (((Uint) buff[1] << 8) | buff[0]);
-    buff [0] = (Uint8_t)(ValidSamples >>  0);
-    buff [1] = (Uint8_t)(ValidSamples >>  8);
+    ValidSamples  |= 0x800F & (((mpc_uint_t) buff[1] << 8) | buff[0]);
+    buff [0] = (mpc_uint8_t)(ValidSamples >>  0);
+    buff [1] = (mpc_uint8_t)(ValidSamples >>  8);
 
     fwrite ( buff, 1, 2, fp );
@@ -131,22 +125,21 @@
 
 
-void
-WriteBits ( const Uint32_t input, const unsigned int bits )
+void WriteBits (mpc_encoder * e, const mpc_uint32_t input, const unsigned int bits )
 {
-    BufferedBits += bits;
-    filled       -= bits;
+    e->BufferedBits += bits;
+    e->filled       -= bits;
 
-    if      ( filled > 0 ) {
-        dword  |= input << filled;
+    if      ( e->filled > 0 ) {
+        e->dword  |= input << e->filled;
     }
-    else if ( filled < 0 ) {
-        Buffer [Zaehler++] = dword | ( input >> -filled );
-        filled += 32;
-        dword   = input << filled;
+    else if ( e->filled < 0 ) {
+        e->Buffer [e->Zaehler++] = e->dword | ( input >> -e->filled );
+        e->filled += 32;
+        e->dword   = input << e->filled;
     }
     else {
-        Buffer [Zaehler++] = dword | input;
-        filled  = 32;
-        dword   =  0;
+        e->Buffer [e->Zaehler++] = e->dword | input;
+        e->filled  = 32;
+        e->dword   =  0;
     }
 }
@@ -154,13 +147,12 @@
 // Bits in the original stream have to be 0, maximum X bits allowed to be set in input
 // Actual bitstream must have already written ptr[0] and ptr[1]
-void
-WriteBitsAt ( const Uint32_t input, const unsigned int bits, BitstreamPos const pos )
+void WriteBitsAt (mpc_encoder * e, const mpc_uint32_t input, const unsigned int bits, BitstreamPos const pos )
 {
-    Uint32_t*     ptr    = pos.ptr;
+    mpc_uint32_t*     ptr    = pos.ptr;
     int           filled = pos.bit - bits;
 
 //    fprintf ( stderr, "%5u %2u %08lX %2u\n", input, bits, pos.ptr, pos.bit );
 
-    Buffer [Zaehler] = dword;
+    e->Buffer [e->Zaehler] = e->dword;
 
     if      ( filled > 0 ) {
@@ -175,13 +167,12 @@
     }
 
-    dword = Buffer [Zaehler];
+    e->dword = e->Buffer [e->Zaehler];
 }
 
 
-void
-GetBitstreamPos ( BitstreamPos* const pos )
+void GetBitstreamPos (mpc_encoder * e, BitstreamPos* const pos )
 {
-    pos -> ptr = Buffer + Zaehler;
-    pos -> bit = filled;
+    pos -> ptr = e->Buffer + e->Zaehler;
+    pos -> bit = e->filled;
 }
 
