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