Index: libmpc/branches/r2d/libmpcenc/bitstream.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 220)
+++ libmpc/branches/r2d/libmpcenc/bitstream.c	(revision 221)
@@ -108,20 +108,4 @@
 }
 
-void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits )
-{
-	e->outputBits += bits;
-
-	if (e->bitsCount + bits > sizeof(e->bitsBuff) * 8) {
-		int tmp = (sizeof(e->bitsBuff) * 8 - e->bitsCount);
-		bits -= tmp;
-		e->bitsBuff = (e->bitsBuff << tmp) | (input >> bits);
-		e->bitsCount = sizeof(e->bitsBuff) * 8;
-		emptyBits(e);
-		input &= (1 << bits) - 1;
-	}
-	e->bitsBuff = (e->bitsBuff << bits) | input;
-	e->bitsCount += bits;
-}
-
 unsigned int encodeSize(mpc_uint64_t size, char * buff, mpc_bool_t addCodeSize)
 {
Index: libmpc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 220)
+++ libmpc/branches/r2d/libmpcenc/encode_sv7.c	(revision 221)
@@ -129,7 +129,13 @@
 	int n;
 	const Huffman_t * Table, * Tables[2];
+	mpc_int32_t * Res_L = e->Res_L;
+	mpc_int32_t * Res_R = e->Res_R;
+	mpc_bool_t * DSCF_Flag_L = e->DSCF_Flag_L;
+	mpc_bool_t * DSCF_Flag_R = e->DSCF_Flag_R;
+	mpc_int32_t * SCF_Last_L = e->SCF_Last_L;
+	mpc_int32_t * SCF_Last_R = e->SCF_Last_R;
 
 	for( n = MaxBand; n >= 0; n--)
-		if (e->Res_L[n] != 0 || e->Res_R[n] != 0) break;
+		if (Res_L[n] != 0 || Res_R[n] != 0) break;
 
 	n++;
@@ -147,14 +153,14 @@
 
 	if (MaxBand) {
-		writeBits(e, HuffRes[0][e->Res_L[MaxBand - 1]].Code, HuffRes[0][e->Res_L[MaxBand - 1]].Length);
-		writeBits(e, HuffRes[0][e->Res_R[MaxBand - 1]].Code, HuffRes[0][e->Res_R[MaxBand - 1]].Length);
+		writeBits(e, HuffRes[0][Res_L[MaxBand - 1]].Code, HuffRes[0][Res_L[MaxBand - 1]].Length);
+		writeBits(e, HuffRes[0][Res_R[MaxBand - 1]].Code, HuffRes[0][Res_R[MaxBand - 1]].Length);
 		for ( n = MaxBand - 2; n >= 0; n--) {
-			int tmp = e->Res_L[n] - e->Res_L[n + 1];
+			int tmp = Res_L[n] - Res_L[n + 1];
 			if (tmp < 0) tmp += 17;
-			writeBits(e, HuffRes[e->Res_L[n + 1] > 2][tmp].Code, HuffRes[e->Res_L[n + 1] > 2][tmp].Length);
-
-			tmp = e->Res_R[n] - e->Res_R[n + 1];
+			writeBits(e, HuffRes[Res_L[n + 1] > 2][tmp].Code, HuffRes[Res_L[n + 1] > 2][tmp].Length);
+
+			tmp = Res_R[n] - Res_R[n + 1];
 			if (tmp < 0) tmp += 17;
-			writeBits(e, HuffRes[e->Res_R[n + 1] > 2][tmp].Code, HuffRes[e->Res_R[n + 1] > 2][tmp].Length);
+			writeBits(e, HuffRes[Res_R[n + 1] > 2][tmp].Code, HuffRes[Res_R[n + 1] > 2][tmp].Length);
 		}
 
@@ -162,8 +168,9 @@
 			mpc_uint32_t tmp = 0;
 			int cnt = 0, tot = 0;
+			mpc_bool_t * MS_Flag = e->MS_Flag;
 			for( n = 0; n < MaxBand; n++) {
-				if ( e->Res_L[n] != 0 || e->Res_R[n] != 0 ) {
-					tmp = (tmp << 1) | e->MS_Flag[n];
-					cnt += e->MS_Flag[n];
+				if ( Res_L[n] != 0 || Res_R[n] != 0 ) {
+					tmp = (tmp << 1) | MS_Flag[n];
+					cnt += MS_Flag[n];
 					tot++;
 				}
@@ -179,5 +186,5 @@
 	if (e->framesInBlock == 0){
 		for( n = 0; n < 32; n++)
-			e->DSCF_Flag_L[n] = e->DSCF_Flag_R[n] = 1; // new block -> force key frame
+			DSCF_Flag_L[n] = DSCF_Flag_R[n] = 1; // new block -> force key frame
 	}
 
@@ -186,9 +193,9 @@
 	for ( n = 0; n < MaxBand; n++ ) {
 		int tmp = 0, cnt = -1;
-		if (e->Res_L[n]) {
+		if (Res_L[n]) {
 			tmp = (e->SCF_Index_L[n][1] == e->SCF_Index_L[n][0]) * 2 + (e->SCF_Index_L[n][2] == e->SCF_Index_L[n][1]);
 			cnt++;
 		}
-		if (e->Res_R[n]) {
+		if (Res_R[n]) {
 			tmp = (tmp << 2) | ((e->SCF_Index_R[n][1] == e->SCF_Index_R[n][0]) * 2 + (e->SCF_Index_R[n][2] == e->SCF_Index_R[n][1]));
 			cnt++;
@@ -201,11 +208,12 @@
 
 	for ( n = 0; n < MaxBand; n++ ) {
-		if ( e->Res_L[n] ) {
+		if ( Res_L[n] ) {
 			int m;
-			if (e->DSCF_Flag_L[n] == 1) {
-				writeBits(e, e->SCF_Index_L[n][0] + 6, 7);
-				e->DSCF_Flag_L[n] = 0;
+			mpc_int32_t * SCFI_L_n = e->SCF_Index_L[n];
+			if (DSCF_Flag_L[n] == 1) {
+				writeBits(e, SCFI_L_n[0] + 6, 7);
+				DSCF_Flag_L[n] = 0;
 			} else {
-				unsigned int tmp = (e->SCF_Index_L[n][0] - e->SCF_Last_L[n] + 31) & 127;
+				unsigned int tmp = (SCFI_L_n[0] - SCF_Last_L[n] + 31) & 127;
 				if (tmp < 64)
 					writeBits(e, HuffDSCF_2[tmp].Code, HuffDSCF_2[tmp].Length);
@@ -216,6 +224,6 @@
 			}
 			for( m = 0; m < 2; m++){
-				if (e->SCF_Index_L[n][m+1] != e->SCF_Index_L[n][m]) {
-					unsigned int tmp = (e->SCF_Index_L[n][m+1] - e->SCF_Index_L[n][m] + 31) & 127;
+				if (SCFI_L_n[m+1] != SCFI_L_n[m]) {
+					unsigned int tmp = (SCFI_L_n[m+1] - SCFI_L_n[m] + 31) & 127;
 					if (tmp < 64)
 						writeBits(e, HuffDSCF_1[tmp].Code, HuffDSCF_1[tmp].Length);
@@ -226,13 +234,14 @@
 				}
 			}
-			e->SCF_Last_L[n] = e->SCF_Index_L[n][2];
-		}
-		if ( e->Res_R[n] ) {
+			SCF_Last_L[n] = SCFI_L_n[2];
+		}
+		if ( Res_R[n] ) {
 			int m;
-			if (e->DSCF_Flag_R[n] == 1) {
-				writeBits(e, e->SCF_Index_R[n][0] + 6, 7);
-				e->DSCF_Flag_R[n] = 0;
+			mpc_int32_t * SCFI_R_n = e->SCF_Index_R[n];
+			if (DSCF_Flag_R[n] == 1) {
+				writeBits(e, SCFI_R_n[0] + 6, 7);
+				DSCF_Flag_R[n] = 0;
 			} else {
-				unsigned int tmp = (e->SCF_Index_R[n][0] - e->SCF_Last_R[n] + 31) & 127;
+				unsigned int tmp = (SCFI_R_n[0] - SCF_Last_R[n] + 31) & 127;
 				if (tmp < 64)
 					writeBits(e, HuffDSCF_2[tmp].Code, HuffDSCF_2[tmp].Length);
@@ -243,6 +252,6 @@
 			}
 			for( m = 0; m < 2; m++){
-				if (e->SCF_Index_R[n][m+1] != e->SCF_Index_R[n][m]) {
-					unsigned int tmp = (e->SCF_Index_R[n][m+1] - e->SCF_Index_R[n][m] + 31) & 127;
+				if (SCFI_R_n[m+1] != SCFI_R_n[m]) {
+					unsigned int tmp = (SCFI_R_n[m+1] - SCFI_R_n[m] + 31) & 127;
 					if (tmp < 64)
 						writeBits(e, HuffDSCF_1[tmp].Code, HuffDSCF_1[tmp].Length);
@@ -253,5 +262,5 @@
 				}
 			}
-			e->SCF_Last_R[n] = e->SCF_Index_R[n][2];
+			SCF_Last_R[n] = SCFI_R_n[2];
 		}
 	}
@@ -259,5 +268,5 @@
 	/*********************************** Samples *********************************/
 	for ( n = 0; n < MaxBand; n++ ) {
-		int Res = e->Res_L[n];
+		int Res = Res_L[n];
 		const int * q = e->Q[n].L;
 		static const unsigned int thres[] = {0, 0, 3, 7, 9, 1, 3, 4, 8};
@@ -336,5 +345,5 @@
 			}
 
-			Res = e->Res_R[n];
+			Res = Res_R[n];
 		} while (q == e->Q[n].L && (q = e->Q[n].R));
 	}
Index: libmpc/branches/r2d/libmpcenc/libmpcenc.h
===================================================================
--- libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 220)
+++ libmpc/branches/r2d/libmpcenc/libmpcenc.h	(revision 221)
@@ -94,5 +94,20 @@
  mpc_uint32_t writeBlock ( mpc_encoder_t *, const char *, const mpc_bool_t, mpc_uint32_t);
  void writeMagic (mpc_encoder_t * e);
- void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits );
+ void emptyBits(mpc_encoder_t * e);
+ static mpc_inline void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits )
+ {
+	 e->outputBits += bits;
+
+	 if (e->bitsCount + bits > sizeof(e->bitsBuff) * 8) {
+		 int tmp = (sizeof(e->bitsBuff) * 8 - e->bitsCount);
+		 bits -= tmp;
+		 e->bitsBuff = (e->bitsBuff << tmp) | (input >> bits);
+		 e->bitsCount = sizeof(e->bitsBuff) * 8;
+		 emptyBits(e);
+		 input &= (1 << bits) - 1;
+	 }
+	 e->bitsBuff = (e->bitsBuff << bits) | input;
+	 e->bitsCount += bits;
+ }
  void writeSeekTable (mpc_encoder_t * e);
  void writeBitstream_SV8 ( mpc_encoder_t*, int);
Index: libmpc/branches/r2d/libmpcenc/quant.c
===================================================================
--- libmpc/branches/r2d/libmpcenc/quant.c	(revision 220)
+++ libmpc/branches/r2d/libmpcenc/quant.c	(revision 221)
@@ -158,19 +158,35 @@
 ISNR_Schaetzer ( const float* input, const float SNRcomp, const int res )
 {
-    int    k;
-    const float  fac    = A [res];
-	const float  invfac = C [res];
+	const float  fac    = A [res] * NoiseInjectionCompensation1D [res];
+	const float  invfac = C [res] / NoiseInjectionCompensation1D [res];
     float  signal = 1.e-30f;
     float  error = 1.e-30f;
-    float  err, sig;
+	const float * in_end = input + 36;
 
     // Summation of the absolute power and the quadratic error
-    for ( k = 0; k < 36; k++ ) {
-        sig = input[k] * NoiseInjectionCompensation1D [res];
-		err = mpc_nearbyintf(sig * fac) * invfac - sig;
-
+	do {
+		float  err;
+		err = mpc_nearbyintf(input[0] * fac) * invfac - input[0];
         error += err * err;
-        signal += sig * sig;
-    }
+		signal += input[0] * input[0];
+
+		err = mpc_nearbyintf(input[1] * fac) * invfac - input[1];
+		error += err * err;
+		signal += input[1] * input[1];
+
+		err = mpc_nearbyintf(input[2] * fac) * invfac - input[2];
+		error += err * err;
+		signal += input[2] * input[2];
+
+		err = mpc_nearbyintf(input[3] * fac) * invfac - input[3];
+		error += err * err;
+		signal += input[3] * input[3];
+
+		input += 4;
+
+	} while (input < in_end);
+
+	error *= NoiseInjectionCompensation1D [res] * NoiseInjectionCompensation1D [res];
+	signal *= NoiseInjectionCompensation1D [res] * NoiseInjectionCompensation1D [res];
 
     // Utilization of SNRcomp only if SNR > 1 !!!
