Index: mppenc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 77)
+++ mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 78)
@@ -23,5 +23,9 @@
 #include "libmpcenc.h"
 
-void  WriteBits         ( mpc_encoder_t*, const mpc_uint32_t input, const unsigned int bits );
+// bitstream.c
+void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits );
+unsigned int encodeSize(mpc_uint64_t, char *, mpc_bool_t);
+void writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC);
+
 void       Init_Huffman_Encoder_SV7   ( void );
 
@@ -58,7 +62,7 @@
 
 
-// initialize SV7
+// initialize SV8
 void
-Init_SV7 ( mpc_encoder_t * e )
+Init_SV8 ( mpc_encoder_t * e )
 {
     Init_Huffman_Encoder_SV7 ();
@@ -66,80 +70,57 @@
 	Klemm    ();
 
-	e->dword = 0;
-	e->filled = 32;
-	e->Zaehler = 0;
-	e->BufferedBits = 0;
+	e->pos = 0;
+	e->bitsCount = 0;
+	e->outputBits = 0;
+	e->bitsBuff = 0;
 	e->Overflows = 0;
 }
 
 
-// writes SV7-header
+// writes SV8-header
 void
-WriteHeader_SV7 ( mpc_encoder_t*e,
+WriteHeader_SV8 ( mpc_encoder_t*e,
 				  const unsigned int  MaxBand,
-                  const unsigned int  Profile,
                   const unsigned int  MS_on,
-                  const mpc_uint32_t  TotalFrames,
-                  const unsigned int  SamplesRest,
+                  const unsigned int  SamplesCount,
                   const unsigned int  StreamVersion,
-                  const unsigned int  SampleFreq )
+				  const unsigned int  PNS_on,
+                  const unsigned int  SampleFreq,
+				  const unsigned int  ChannelCount)
 {
-    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 ( e, 1        ,  4 );    // 1: Experimental profile
-    else
-#endif
-
-        WriteBits ( e, Profile  ,  4 );    // 5...15: below Telephone...above BrainDead
-	WriteBits ( e, 0            ,  2 );    // for future use
-    switch ( SampleFreq ) {
-		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;
+	unsigned char samplesCount[10];
+	int samplesCountLen = encodeSize(SamplesCount, (char *)samplesCount, FALSE);
+	int i;
+
+    writeBits ( e, StreamVersion,  8 );    // StreamVersion
+
+	for( i = 0; i < samplesCountLen; i++) // nb of samples
+		writeBits ( e, samplesCount[i]  , 8 );
+
+	switch ( SampleFreq ) {
+		case 44100: writeBits ( e, 0, 4 ); break;
+		case 48000: writeBits ( e, 1, 4 ); break;
+		case 37800: writeBits ( e, 2, 4 ); break;
+		case 32000: writeBits ( e, 3, 4 ); break;
 		default   : sprintf(stderr, "Internal error\n");// FIXME : stderr_printf ( "Internal error\n");
-                    exit (1);
-    }
-	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
+		exit (1);
+	}
+
+	writeBits ( e, ChannelCount - 1  ,  4 );    // Channels
+	writeBits ( e, MaxBand - 1  ,  5 );    // Bandwidth
+	writeBits ( e, 0            ,  1 );    // former IS-Flag (not supported anymore)
+	writeBits ( e, MS_on        ,  1 );    // MS-Coding Flag
+	writeBits ( e, PNS_on       ,  1 );    // PNS flag
+	writeBits ( e, FRAMES_PER_BLOCK_PWR,  4 );    // frames per block (log2 unit)
 }
-
-
-void
-FinishBitstream ( mpc_encoder_t* e )
-{
-    e->Buffer [e->Zaehler++] = e->dword;         // Assigning the "last" word
-}
-
 
 #define ENCODE_SCF1( new, old, rll )                         \
         d = new - old + 7;                                   \
-        if ( d <= 14u  && rll < 32) {                        \
-            WriteBits ( e, Table[d].Code, Table[d].Length );    \
-        }                                                    \
-        else {                                               \
+        if ( d <= 14u  && rll < 1) {                        \
+            writeBits ( e, Table[d].Code, Table[d].Length );    \
+        } else {                                               \
             if ( new < 0 ) new = 0, e->Overflows++;          \
-            WriteBits ( e, Table[15].Code, Table[15].Length );  \
-            WriteBits ( e, (unsigned int)new, 6 );              \
+            writeBits ( e, Table[15].Code, Table[15].Length );  \
+            writeBits ( e, (unsigned int)new, 6 );              \
             rll = 0;                                         \
         }
@@ -148,38 +129,11 @@
         d = new - old + 7;                                   \
         if ( d <= 14u ) {                                    \
-            WriteBits ( e, Table[d].Code, Table[d].Length );    \
-        }                                                    \
-        else {                                               \
+            writeBits ( e, Table[d].Code, Table[d].Length );    \
+        } else {                                               \
             if ( new < 0 ) new = 0, e->Overflows++;          \
-            WriteBits ( e, Table[15].Code, Table[15].Length );  \
-            WriteBits ( e, (unsigned int)new, 6 );              \
+            writeBits ( e, Table[15].Code, Table[15].Length );  \
+            writeBits ( e, (unsigned int)new, 6 );              \
             rll = 0;                                         \
         }
-
-
-static void
-test ( const int* const Res, const unsigned int* q )
-{
-#if 0
-    int  i;
-
-    switch ( *Res ) {
-    case 1:
-        for ( i = 0; i < 36; i ++ )
-            if ( q[i] != 1 )
-                return;
-        fprintf ( stderr, "Alles Nullsamples, aber Auflï¿œung = %u\n", *Res );
-        *Res = 0;
-        break;
-    case 2:
-        for ( i = 0; i < 36; i ++ )
-            if ( q[i] != 2 )
-                return;
-        fprintf ( stderr, "Alles Nullsamples, aber Auflï¿œung = %u\n", *Res );
-        *Res = 0;
-        break;
-    }
-#endif
-}
 
 
@@ -204,33 +158,30 @@
 
     /************************************ Resolution *********************************/
-    WriteBits ( e, (unsigned int)Res_L[0], 4 );                            // subband 0
-	WriteBits ( e, (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 ( e, MS_Flag[0] , 1 );
+		writeBits ( e, MS_Flag[0] , 1 );
 
     Table = HuffHdr;                                                    // subband 1...MaxBand
     for ( n = 1; n <= MaxBand; n++ ) {
-        test ( Res_L+n, Q[n].L );
-
         d = Res_L[n] - Res_L[n-1] + 5;
         if ( d <= 8u ) {
-			WriteBits ( e, Table[d].Code, Table[d].Length );
+			writeBits ( e, Table[d].Code, Table[d].Length );
         }
         else {
-			WriteBits ( e, Table[9].Code, Table[9].Length );
-			WriteBits ( e, Res_L[n]     , 4               );
-        }
-
-        test ( Res_R+n, Q[n].R );
+			writeBits ( e, Table[9].Code, Table[9].Length );
+			writeBits ( e, Res_L[n]     , 4               );
+        }
+
         d = Res_R[n] - Res_R[n-1] + 5;
         if ( d <= 8u ) {
-			WriteBits ( e, Table[d].Code, Table[d].Length );
+			writeBits ( e, Table[d].Code, Table[d].Length );
         }
         else {
-			WriteBits ( e, Table[9].Code, Table[9].Length );
-			WriteBits ( e, 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 ( e, MS_Flag[n], 1 );
+			writeBits ( e, MS_Flag[n], 1 );
     }
 
@@ -240,9 +191,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 ( e, 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 ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );
+			writeBits ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );
         }
     }
@@ -250,5 +201,8 @@
     /************************************* SCF **********************************/
     Table = HuffDSCF;
-    for ( n = 0; n <= MaxBand; n++ ) {
+	for ( n = 0; n <= MaxBand; n++ ) {
+		if (e->framesInBlock == 0){
+			DSCF_RLL_L[n] = DSCF_RLL_R[n] = 1; // new block -> force key frame
+		}
 
         if ( Res_L[n] ) {
@@ -276,6 +230,4 @@
             }
         }
-        if (DSCF_RLL_L[n] <= 32)
-            DSCF_RLL_L[n]++;        // Increased counters for SCF that haven't been initialized again
 
         if ( Res_R[n] ) {
@@ -303,6 +255,4 @@
             }
         }
-        if (DSCF_RLL_R[n] <= 32)
-            DSCF_RLL_R[n]++;          // Increased counters for SCF that haven't been freshly initialized
     }
 
@@ -326,9 +276,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, 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 ( e, Table[idx].Code, Table[idx].Length );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -342,9 +292,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, 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 ( e, Table[idx].Code, Table[idx].Length );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -361,14 +311,14 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			writeBits ( e, book, 1 );
             Table = HuffQ [book][Res_L[n]];
             for ( k = 0; k < 36; k++ ) {
                 idx = q[k];
-				WriteBits ( e, Table[idx].Code, Table[idx].Length );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
         default:
             for ( k = 0; k < 36; k++ )
-				WriteBits ( e, q[k], Res_L[n]-1 );
+				writeBits ( e, q[k], Res_L[n]-1 );
             break;
         }
@@ -390,9 +340,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, 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 ( e, Table[idx].Code, Table[idx].Length );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -406,9 +356,9 @@
             }
             book = sum >= 0;
-			WriteBits ( e, 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 ( e, Table[idx].Code, Table[idx].Length );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
@@ -425,19 +375,22 @@
             }
             book = sum >= 0;
-			WriteBits ( e, book, 1 );
+			writeBits ( e, book, 1 );
             Table = HuffQ [book][Res_R[n]];
             for ( k = 0; k < 36; k++ ) {
                 idx = q[k];
-				WriteBits ( e, Table[idx].Code, Table[idx].Length );
+				writeBits ( e, Table[idx].Code, Table[idx].Length );
             }
             break;
         default:
             for ( k = 0; k < 36; k++ )
-				WriteBits ( e, q[k], Res_R[n] - 1 );
+				writeBits ( e, q[k], Res_R[n] - 1 );
             break;
         }
 
     }
-    return;
+
+	e->framesInBlock++;
+	if (e->framesInBlock == FRAMES_PER_BLOCK)
+		writeBlock(e, "AD", FALSE);
 }
 
