Index: /mppenc/branches/r2d/common/minimax.h
===================================================================
--- /mppenc/branches/r2d/common/minimax.h	(revision 76)
+++ /mppenc/branches/r2d/common/minimax.h	(revision 77)
@@ -18,8 +18,9 @@
  */
 
-#ifndef MPP_MINIMAX_H
-#define MPP_MINIMAX_H
+#pragma once
 
-#if   defined __GNUC__  &&  defined __cplusplus
+# define clip(x,min,max) ( (x) < (min) ? (min) : (x) > (max) ? (max) : (x) )
+
+#ifdef __cplusplus
 
 # define maxi(A,B)  ( (A) >? (B) )
@@ -30,9 +31,5 @@
 # define minf(A,B)  ( (A) <? (B) )
 
-# define absi(A)    abs   (A)
-# define absf(A)    fabsf (A)
-# define absd(A)    fabs  (A)
-
-#elif defined __GNUC__
+#else
 
 # define maxi(A,B)  ( (A) > (B)  ?  (A)  :  (B) )
@@ -43,4 +40,8 @@
 # define minf(A,B)  ( (A) < (B)  ?  (A)  :  (B) )
 
+#endif
+
+#ifdef __GNUC__
+
 # define absi(A)    abs   (A)
 # define absf(A)    fabsf (A)
@@ -49,16 +50,8 @@
 #else
 
-# define maxi(A,B)  ( (A) >  (B)  ?  (A)  :  (B) )
-# define mini(A,B)  ( (A) <  (B)  ?  (A)  :  (B) )
-# define maxd(A,B)  ( (A) >  (B)  ?  (A)  :  (B) )
-# define mind(A,B)  ( (A) <  (B)  ?  (A)  :  (B) )
-# define maxf(A,B)  ( (A) >  (B)  ?  (A)  :  (B) )
-# define minf(A,B)  ( (A) <  (B)  ?  (A)  :  (B) )
-
 # define absi(A)    ( (A) >= 0    ?  (A)  : -(A) )
 # define absf(A)    ( (A) >= 0.f  ?  (A)  : -(A) )
 # define absd(A)    ( (A) >= 0.   ?  (A)  : -(A) )
 
-#endif /* GNUC && C++ */
+#endif
 
-#endif /* MPP_MINIMAX_H */
Index: /mppenc/branches/r2d/libmpcenc/encode_sv7.c
===================================================================
--- /mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 76)
+++ /mppenc/branches/r2d/libmpcenc/encode_sv7.c	(revision 77)
@@ -26,4 +26,8 @@
 void       Init_Huffman_Encoder_SV7   ( void );
 
+void   Klemm ( void );
+
+void   Init_Skalenfaktoren             ( void );
+
 // huffsv7.c
 extern Huffman_t         HuffHdr  [10];         // contains tables for SV7-header
@@ -56,7 +60,15 @@
 // initialize SV7
 void
-Init_SV7 ( void )
+Init_SV7 ( mpc_encoder_t * e )
 {
     Init_Huffman_Encoder_SV7 ();
+	Init_Skalenfaktoren ();
+	Klemm    ();
+
+	e->dword = 0;
+	e->filled = 32;
+	e->Zaehler = 0;
+	e->BufferedBits = 0;
+	e->Overflows = 0;
 }
 
@@ -147,5 +159,5 @@
 
 static void
-test ( const unsigned int* const Res, const int* q )
+test ( const int* const Res, const unsigned int* q )
 {
 #if 0
Index: /mppenc/branches/r2d/libmpcpsy/Makefile.am
===================================================================
--- /mppenc/branches/r2d/libmpcpsy/Makefile.am	(revision 76)
+++ /mppenc/branches/r2d/libmpcpsy/Makefile.am	(revision 77)
@@ -2,4 +2,5 @@
 METASOURCES = AUTO
 lib_LIBRARIES = libmpcpsy.a
-libmpcpsy_a_SOURCES = ans.c cvd.c fft4g.c fft_routines.c psy.c psy_tab.c
+libmpcpsy_a_SOURCES = ans.c cvd.c fft4g.c fft_routines.c psy.c psy_tab.c \
+	profile.c
 noinst_HEADERS = libmpcpsy.h
Index: /mppenc/branches/r2d/libmpcpsy/libmpcpsy.h
===================================================================
--- /mppenc/branches/r2d/libmpcpsy/libmpcpsy.h	(revision 76)
+++ /mppenc/branches/r2d/libmpcpsy/libmpcpsy.h	(revision 77)
@@ -53,26 +53,26 @@
 	int           Max_Band;                    // maximum bandwidth
 	float         SampleFreq;
-	float         Bandwidth;
+	int MainQual;	// main profile quality
 
-	/* further switches for the psymodel */
-	unsigned int  CVD_used;         // global flag for ClearVoiceDetection
-	float         varLtq;           // variable threshold in quiet
-	unsigned int  tmpMask_used;     // global flag for temporal masking
-	float         ShortThr;         // Factor to calculate the masking threshold with transients
-	float         minSMR;           // minimum SMR for all subbands
-
-	unsigned int  MS_Channelmode;
-
-	/* V A R I A B L E S */
-	float  Ltq_offset;                         // Offset for threshold in quiet
-	float  Ltq_max;                            // maximum level for threshold in quiet
-	float        TMN;                        // Offset for purely sinusoid components
-	float        NMT;                        // Offset for purely noisy components
-	float        TransDetect;                // minimum slewrate for transient detection
-	unsigned int    EarModelFlag;
-	int    MinValChoice;
+	// profile params
+	float            ShortThr;         // Factor to calculate the masking threshold with transients
+	int              MinValChoice;
+	unsigned int     EarModelFlag;
+	float            Ltq_offset;       // Offset for threshold in quiet
+	float            TMN;              // Offset for purely sinusoid components
+	float            NMT;              // Offset for purely noisy components
+	float            minSMR;           // minimum SMR for all subbands
+	float            Ltq_max;          // maximum level for threshold in quiet
+	float            BandWidth;
+	unsigned char    tmpMask_used;     // global flag for temporal masking
+	unsigned char    CVD_used;         // global flag for ClearVoiceDetection
+	float            varLtq;           // variable threshold in quiet
+	unsigned char    MS_Channelmode;
+	int              CombPenalities;
+	unsigned int    NS_Order;         // Maximum order for ANS
+	float            PNS;
+	float            TransDetect;      // minimum slewrate for transient detection
 
 	// ans.h
-	unsigned int  NS_Order;                         // Maximum order for ANS
 	unsigned int  NS_Order_L [32];
 	unsigned int  NS_Order_R [32];                  // frame-wise order of the Noiseshaping (0: off, 1...5: on)
Index: /mppenc/branches/r2d/libmpcpsy/profile.c
===================================================================
--- /mppenc/branches/r2d/libmpcpsy/profile.c	(revision 77)
+++ /mppenc/branches/r2d/libmpcpsy/profile.c	(revision 77)
@@ -0,0 +1,140 @@
+/*
+ * Musepack audio compression
+ * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "libmpcpsy.h"
+#include "minimax.h"
+
+
+typedef struct {
+	float            ShortThr;
+	unsigned char    MinValChoice;
+	unsigned int     EarModelFlag;
+	signed char      Ltq_offset;
+	float            TMN;
+	float            NMT;
+	signed char      minSMR;
+	signed char      Ltq_max;
+	unsigned short   BandWidth;
+	unsigned char    tmpMask_used;
+	unsigned char    CVD_used;
+	float            varLtq;
+	unsigned char    MS_Channelmode;
+	unsigned char    CombPenalities;
+	unsigned char    NS_Order;
+	float            PNS;
+	float            TransDetect;
+} Profile_Setting_t;
+
+#define PROFILE_PRE2_TELEPHONE   5      // --quality  0
+// #define PROFILE_PRE_TELEPHONE    6      // --quality  1
+// #define PROFILE_TELEPHONE        7      // --quality  2
+// #define PROFILE_THUMB            8      // --quality  3
+// #define PROFILE_RADIO            9      // --quality  4
+// #define PROFILE_STANDARD        10      // --quality  5
+// #define PROFILE_XTREME          11      // --quality  6
+// #define PROFILE_INSANE          12      // --quality  7
+// #define PROFILE_BRAINDEAD       13      // --quality  8
+// #define PROFILE_POST_BRAINDEAD  14      // --quality  9
+#define PROFILE_POST2_BRAINDEAD 15      // --quality 10
+
+
+static const Profile_Setting_t  Profiles [16] = {
+	{ 0 },
+	{ 0 },
+	{ 0 },
+	{ 0 },
+	{ 0 },
+	/*    Short   MinVal  EarModel  Ltq_                min   Ltq_  Band-  tmpMask  CVD_  varLtq    MS   Comb   NS_        Trans */
+	/*    Thr     Choice  Flag      offset  TMN   NMT   SMR   max   Width  _used    used         channel Penal used  PNS    Det  */
+	{ 1.e9f,  1,      300,       30,    3.0, -1.0,    0,  106,   4820,   1,      1,    1.,      3,     24,  6,   1.09f, 200 },  // 0: pre-Telephone
+	{ 1.e9f,  1,      300,       24,    6.0,  0.5,    0,  100,   7570,   1,      1,    1.,      3,     20,  6,   0.77f, 180 },  // 1: pre-Telephone
+	{ 1.e9f,  1,      400,       18,    9.0,  2.0,    0,   94,  10300,   1,      1,    1.,      4,     18,  6,   0.55f, 160 },  // 2: Telephone
+	{ 50.0f,  2,      430,       12,   12.0,  3.5,    0,   88,  13090,   1,      1,    1.,      5,     15,  6,   0.39f, 140 },  // 3: Thumb
+	{ 15.0f,  2,      440,        6,   15.0,  5.0,    0,   82,  15800,   1,      1,    1.,      6,     10,  6,   0.27f, 120 },  // 4: Radio
+	{  5.0f,  2,      550,        0,   18.0,  6.5,    1,   76,  19980,   1,      2,    1.,     11,      9,  6,   0.00f, 100 },  // 5: Standard
+	{  4.0f,  2,      560,       -6,   21.0,  8.0,    2,   70,  22000,   1,      2,    1.,     12,      7,  6,   0.00f,  80 },  // 6: Xtreme
+	{  3.0f,  2,      570,      -12,   24.0,  9.5,    3,   64,  24000,   1,      2,    2.,     13,      5,  6,   0.00f,  60 },  // 7: Insane
+	{  2.8f,  2,      580,      -18,   27.0, 11.0,    4,   58,  26000,   1,      2,    4.,     13,      4,  6,   0.00f,  40 },  // 8: BrainDead
+	{  2.6f,  2,      590,      -24,   30.0, 12.5,    5,   52,  28000,   1,      2,    8.,     13,      4,  6,   0.00f,  20 },  // 9: post-BrainDead
+	{  2.4f,  2,      599,      -30,   33.0, 14.0,    6,   46,  30000,   1,      2,   16.,     15,      2,  6,   0.00f,  10 },  //10: post-BrainDead
+};
+
+
+int TestProfileParams ( PsyModel* m )
+{   //                                       0    1    2    3    4   5   6  7 8 9  10  11  12  13 14  15
+	static signed char  TMNStereoAdj [] = { -6, -18, -15, -18, -12, -9, -6, 0,0,0, +1, +1, +1, +1, 0, +1 };  // Penalties for TMN
+	static signed char  NMTStereoAdj [] = { -3, -18, -15, -15,  -9, -6, -3, 0,0,0,  0, +1, +1, +1, 0, +1 };  // Penalties for NMT
+	int                 i;
+
+	m->MainQual = PROFILE_PRE2_TELEPHONE;
+
+	for ( i = PROFILE_PRE2_TELEPHONE; i <= PROFILE_POST2_BRAINDEAD; i++ ) {
+		if ( m->ShortThr     > Profiles [i].ShortThr     ) continue;
+		if ( m->MinValChoice < Profiles [i].MinValChoice ) continue;
+		if ( m->EarModelFlag < Profiles [i].EarModelFlag ) continue;
+		if ( m->Ltq_offset   > Profiles [i].Ltq_offset   ) continue;
+		if ( m->Ltq_max      > Profiles [i].Ltq_max      ) continue;                     // offset should normally be considered here
+		if ( m->TMN + TMNStereoAdj [m->MS_Channelmode] <
+				   Profiles [i].TMN + TMNStereoAdj [Profiles [i].MS_Channelmode] )
+			continue;
+		if ( m->NMT + NMTStereoAdj [m->MS_Channelmode] <
+				   Profiles [i].NMT + NMTStereoAdj [Profiles [i].MS_Channelmode] )
+			continue;
+		if ( m->minSMR       < Profiles [i].minSMR       ) continue;
+		if ( m->BandWidth    < Profiles [i].BandWidth    ) continue;
+		if ( m->tmpMask_used < Profiles [i].tmpMask_used ) continue;
+		if ( m->CVD_used     < Profiles [i].CVD_used     ) continue;
+     // if ( varLtq       > Profiles [i].varLtq       ) continue;
+     // if ( NS_Order     < Profiles [i].NS_Order     ) continue;
+		if ( m->PNS          > Profiles [i].PNS          ) continue;
+		m->MainQual = i;
+	}
+	return m->MainQual;
+}
+
+void SetQualityParams (PsyModel * m, float qual )
+{
+	int    i;
+	float  mix;
+
+	qual = clip(qual, 0., 10.);
+
+	i   = (int) qual + PROFILE_PRE2_TELEPHONE;
+	mix = qual - (int) qual;
+
+	m->MainQual       = i;
+	m->ShortThr       = Profiles [i].ShortThr   * (1-mix) + Profiles [i+1].ShortThr   * mix;
+	m->MinValChoice   = Profiles [i].MinValChoice  ;
+	m->EarModelFlag   = Profiles [i].EarModelFlag  ;
+	m->Ltq_offset     = Profiles [i].Ltq_offset * (1-mix) + Profiles [i+1].Ltq_offset * mix;
+	m->varLtq         = Profiles [i].varLtq     * (1-mix) + Profiles [i+1].varLtq     * mix;
+	m->Ltq_max        = Profiles [i].Ltq_max    * (1-mix) + Profiles [i+1].Ltq_max    * mix;
+	m->TMN            = Profiles [i].TMN        * (1-mix) + Profiles [i+1].TMN        * mix;
+	m->NMT            = Profiles [i].NMT        * (1-mix) + Profiles [i+1].NMT        * mix;
+	m->minSMR         = Profiles [i].minSMR        ;
+	m->BandWidth      = Profiles [i].BandWidth  * (1-mix) + Profiles [i+1].BandWidth  * mix;
+	m->tmpMask_used   = Profiles [i].tmpMask_used  ;
+	m->CVD_used       = Profiles [i].CVD_used      ;
+	m->MS_Channelmode = Profiles [i].MS_Channelmode;
+	m->CombPenalities = Profiles [i].CombPenalities;
+	m->NS_Order       = Profiles [i].NS_Order      ;
+	m->PNS            = Profiles [i].PNS        * (1-mix) + Profiles [i+1].PNS        * mix;
+	m->TransDetect    = Profiles [i].TransDetect* (1-mix) + Profiles [i+1].TransDetect* mix;
+}
+
Index: /mppenc/branches/r2d/libmpcpsy/psy.c
===================================================================
--- /mppenc/branches/r2d/libmpcpsy/psy.c	(revision 76)
+++ /mppenc/branches/r2d/libmpcpsy/psy.c	(revision 77)
@@ -146,5 +146,5 @@
 
 	m->SampleFreq = 0.;
-	m->Bandwidth = 0.;
+	m->BandWidth = 0.;
 	m->KBD1 = 2.;
 	m->KBD2 = -1.;
@@ -152,4 +152,6 @@
 	m->Ltq_max = 0;
 	m->EarModelFlag = 0;
+	m->PNS = 0.;
+	m->CombPenalities = -1;
 
     // generate FFT lookup-tables with largest FFT-size of 1024
Index: /mppenc/branches/r2d/libmpcpsy/psy_tab.c
===================================================================
--- /mppenc/branches/r2d/libmpcpsy/psy_tab.c	(revision 76)
+++ /mppenc/branches/r2d/libmpcpsy/psy_tab.c	(revision 77)
@@ -204,9 +204,9 @@
 }
 
-static double
-Bark2Freq ( double Bark )           // Klemm 2002
-{
-    return 956.86 * sinh (0.101561*Bark) + 11.7296 * sinh (0.304992*Bark) + 6.33622e-3*sinh (0.538621*Bark);
-}
+// static double
+// Bark2Freq ( double Bark )           // Klemm 2002
+// {
+//     return 956.86 * sinh (0.101561*Bark) + 11.7296 * sinh (0.304992*Bark) + 6.33622e-3*sinh (0.538621*Bark);
+// }
 
 static double
@@ -361,5 +361,5 @@
 Init_Psychoakustiktabellen ( PsyModel* m )
 {
-	m->Max_Band = (int) ( m->Bandwidth * 64. / m->SampleFreq );
+	m->Max_Band = (int) ( m->BandWidth * 64. / m->SampleFreq );
 	if ( m->Max_Band <  1 ) m->Max_Band =  1;
 	if ( m->Max_Band > 31 ) m->Max_Band = 31;
Index: /mppenc/branches/r2d/src/mppenc.c
===================================================================
--- /mppenc/branches/r2d/src/mppenc.c	(revision 76)
+++ /mppenc/branches/r2d/src/mppenc.c	(revision 77)
@@ -30,9 +30,7 @@
 float         Power_L    [32] [3];
 float         Power_R    [32] [3];
-float         PNS = 0.;
 
 /* MS-Coding */
 int           PredictionBands =  0;
-int           CombPenalities  = -1;
 int           DisplayUpdateTime = 1;
 int           APE_Version     = 2000;
@@ -48,5 +46,4 @@
 unsigned int  DelInput        = 0;      // deleting the input file after encoding
 unsigned int  WriteMode       = MODE_ASK_FOR_OVERWRITE;      // overwriting a possibly existing MPC file
-int           MainQual;                 // Profiles
 unsigned int  verbose         = 0;      // more information during output
 unsigned int  NoUnicode       = 1;      // console is unicode or not (tag translation)
@@ -250,5 +247,5 @@
              "  --tmn x          set TMN value to x dB (dflt: %4.1f)\n", m->TMN );
     stderr_printf (
-             "  --pns x          set PNS value to x dB (dflt: %4.1f)\n", PNS );
+             "  --pns x          set PNS value to x dB (dflt: %4.1f)\n", m->PNS );
     stderr_printf (
              "==ATH/Bandwidth settings==\n" );
@@ -555,20 +552,20 @@
         // a smaller one is quantized more roughly, i.e. the noise gets amplified???
 
-        if ( CombPenalities >= 0 ) {
-            if      ( P(scfL[0],scfL[1]) + P(scfL[0],scfL[2]) <= CombPenalities ) scfL[2] = scfL[1] = scfL[0];
-            else if ( P(scfL[1],scfL[0]) + P(scfL[1],scfL[2]) <= CombPenalities ) scfL[0] = scfL[2] = scfL[1];
-            else if ( P(scfL[2],scfL[0]) + P(scfL[2],scfL[1]) <= CombPenalities ) scfL[0] = scfL[1] = scfL[2];
-            else if ( P(scfL[0],scfL[1])                      <= CombPenalities ) scfL[1] = scfL[0];
-            else if ( P(scfL[1],scfL[0])                      <= CombPenalities ) scfL[0] = scfL[1];
-            else if ( P(scfL[1],scfL[2])                      <= CombPenalities ) scfL[2] = scfL[1];
-            else if ( P(scfL[2],scfL[1])                      <= CombPenalities ) scfL[1] = scfL[2];
-
-            if      ( P(scfR[0],scfR[1]) + P(scfR[0],scfR[2]) <= CombPenalities ) scfR[2] = scfR[1] = scfR[0];
-            else if ( P(scfR[1],scfR[0]) + P(scfR[1],scfR[2]) <= CombPenalities ) scfR[0] = scfR[2] = scfR[1];
-            else if ( P(scfR[2],scfR[0]) + P(scfR[2],scfR[1]) <= CombPenalities ) scfR[0] = scfR[1] = scfR[2];
-            else if ( P(scfR[0],scfR[1])                      <= CombPenalities ) scfR[1] = scfR[0];
-            else if ( P(scfR[1],scfR[0])                      <= CombPenalities ) scfR[0] = scfR[1];
-            else if ( P(scfR[1],scfR[2])                      <= CombPenalities ) scfR[2] = scfR[1];
-            else if ( P(scfR[2],scfR[1])                      <= CombPenalities ) scfR[1] = scfR[2];
+        if ( m->CombPenalities >= 0 ) {
+            if      ( P(scfL[0],scfL[1]) + P(scfL[0],scfL[2]) <= m->CombPenalities ) scfL[2] = scfL[1] = scfL[0];
+            else if ( P(scfL[1],scfL[0]) + P(scfL[1],scfL[2]) <= m->CombPenalities ) scfL[0] = scfL[2] = scfL[1];
+            else if ( P(scfL[2],scfL[0]) + P(scfL[2],scfL[1]) <= m->CombPenalities ) scfL[0] = scfL[1] = scfL[2];
+            else if ( P(scfL[0],scfL[1])                      <= m->CombPenalities ) scfL[1] = scfL[0];
+            else if ( P(scfL[1],scfL[0])                      <= m->CombPenalities ) scfL[0] = scfL[1];
+            else if ( P(scfL[1],scfL[2])                      <= m->CombPenalities ) scfL[2] = scfL[1];
+            else if ( P(scfL[2],scfL[1])                      <= m->CombPenalities ) scfL[1] = scfL[2];
+
+            if      ( P(scfR[0],scfR[1]) + P(scfR[0],scfR[2]) <= m->CombPenalities ) scfR[2] = scfR[1] = scfR[0];
+            else if ( P(scfR[1],scfR[0]) + P(scfR[1],scfR[2]) <= m->CombPenalities ) scfR[0] = scfR[2] = scfR[1];
+            else if ( P(scfR[2],scfR[0]) + P(scfR[2],scfR[1]) <= m->CombPenalities ) scfR[0] = scfR[1] = scfR[2];
+            else if ( P(scfR[0],scfR[1])                      <= m->CombPenalities ) scfR[1] = scfR[0];
+            else if ( P(scfR[1],scfR[0])                      <= m->CombPenalities ) scfR[0] = scfR[1];
+            else if ( P(scfR[1],scfR[2])                      <= m->CombPenalities ) scfR[2] = scfR[1];
+            else if ( P(scfR[2],scfR[1])                      <= m->CombPenalities ) scfR[1] = scfR[2];
         }
         else {
@@ -740,5 +737,5 @@
 
 static void
-Allocate ( const int MaxBand, int* res, float* x, int* scf, const float* comp, const float* smr, const SCFTriple* Pow, const int* Transient )
+Allocate ( const int MaxBand, int* res, float* x, int* scf, const float* comp, const float* smr, const SCFTriple* Pow, const int* Transient, const float PNS )
 {
     int    Band;
@@ -790,133 +787,5 @@
 }
 
-typedef struct {
-	float            ShortThr;
-	unsigned char    MinValChoice;
-	unsigned int     EarModelFlag;
-	signed char      Ltq_offset;
-	float            TMN;
-	float            NMT;
-	signed char      minSMR;
-	signed char      Ltq_max;
-	unsigned short   BandWidth;
-	unsigned char    tmpMask_used;
-	unsigned char    CVD_used;
-	float            varLtq;
-	unsigned char    MS_Channelmode;
-	unsigned char    CombPenalities;
-	unsigned char    NS_Order;
-	float            PNS;
-	float            TransDetect;
-} Profile_Setting_t;
-
-#define PROFILE_PRE2_TELEPHONE   5      // --quality  0
-#define PROFILE_PRE_TELEPHONE    6      // --quality  1
-#define PROFILE_TELEPHONE        7      // --quality  2
-#define PROFILE_THUMB            8      // --quality  3
-#define PROFILE_RADIO            9      // --quality  4
-#define PROFILE_STANDARD        10      // --quality  5
-#define PROFILE_XTREME          11      // --quality  6
-#define PROFILE_INSANE          12      // --quality  7
-#define PROFILE_BRAINDEAD       13      // --quality  8
-#define PROFILE_POST_BRAINDEAD  14      // --quality  9
-#define PROFILE_POST2_BRAINDEAD 15      // --quality 10
-
-
-static const Profile_Setting_t  Profiles [16] = {
-    { 0 },
-    { 0 },
-    { 0 },
-    { 0 },
-    { 0 },
-/*    Short   MinVal  EarModel  Ltq_                min   Ltq_  Band-  tmpMask  CVD_  varLtq    MS   Comb   NS_        Trans */
-/*    Thr     Choice  Flag      offset  TMN   NMT   SMR   max   Width  _used    used         channel Penal used  PNS    Det  */
-    { 1.e9f,  1,      300,       30,    3.0, -1.0,    0,  106,   4820,   1,      1,    1.,      3,     24,  6,   1.09f, 200 },  // 0: pre-Telephone
-    { 1.e9f,  1,      300,       24,    6.0,  0.5,    0,  100,   7570,   1,      1,    1.,      3,     20,  6,   0.77f, 180 },  // 1: pre-Telephone
-    { 1.e9f,  1,      400,       18,    9.0,  2.0,    0,   94,  10300,   1,      1,    1.,      4,     18,  6,   0.55f, 160 },  // 2: Telephone
-    { 50.0f,  2,      430,       12,   12.0,  3.5,    0,   88,  13090,   1,      1,    1.,      5,     15,  6,   0.39f, 140 },  // 3: Thumb
-    { 15.0f,  2,      440,        6,   15.0,  5.0,    0,   82,  15800,   1,      1,    1.,      6,     10,  6,   0.27f, 120 },  // 4: Radio
-    {  5.0f,  2,      550,        0,   18.0,  6.5,    1,   76,  19980,   1,      2,    1.,     11,      9,  6,   0.00f, 100 },  // 5: Standard
-    {  4.0f,  2,      560,       -6,   21.0,  8.0,    2,   70,  22000,   1,      2,    1.,     12,      7,  6,   0.00f,  80 },  // 6: Xtreme
-    {  3.0f,  2,      570,      -12,   24.0,  9.5,    3,   64,  24000,   1,      2,    2.,     13,      5,  6,   0.00f,  60 },  // 7: Insane
-    {  2.8f,  2,      580,      -18,   27.0, 11.0,    4,   58,  26000,   1,      2,    4.,     13,      4,  6,   0.00f,  40 },  // 8: BrainDead
-    {  2.6f,  2,      590,      -24,   30.0, 12.5,    5,   52,  28000,   1,      2,    8.,     13,      4,  6,   0.00f,  20 },  // 9: post-BrainDead
-    {  2.4f,  2,      599,      -30,   33.0, 14.0,    6,   46,  30000,   1,      2,   16.,     15,      2,  6,   0.00f,  10 },  //10: post-BrainDead
-};
-
-
-static int
-TestProfileParams ( PsyModel* m )
-{   //                                       0    1    2    3    4   5   6  7 8 9  10  11  12  13 14  15
-    static signed char  TMNStereoAdj [] = { -6, -18, -15, -18, -12, -9, -6, 0,0,0, +1, +1, +1, +1, 0, +1 };  // Penalties for TMN
-    static signed char  NMTStereoAdj [] = { -3, -18, -15, -15,  -9, -6, -3, 0,0,0,  0, +1, +1, +1, 0, +1 };  // Penalties for NMT
-    int                 i;
-
-    MainQual = PROFILE_PRE2_TELEPHONE;
-
-    for ( i = PROFILE_PRE2_TELEPHONE; i <= PROFILE_POST2_BRAINDEAD; i++ ) {
-        if ( m->ShortThr     > Profiles [i].ShortThr     ) continue;
-        if ( m->MinValChoice < Profiles [i].MinValChoice ) continue;
-        if ( m->EarModelFlag < Profiles [i].EarModelFlag ) continue;
-        if ( m->Ltq_offset   > Profiles [i].Ltq_offset   ) continue;
-        if ( m->Ltq_max      > Profiles [i].Ltq_max      ) continue;                     // offset should normally be considered here
-        if ( m->TMN + TMNStereoAdj [m->MS_Channelmode] <
-             Profiles [i].TMN + TMNStereoAdj [Profiles [i].MS_Channelmode] )
-                                                        continue;
-        if ( m->NMT + NMTStereoAdj [m->MS_Channelmode] <
-             Profiles [i].NMT + NMTStereoAdj [Profiles [i].MS_Channelmode] )
-                                                        continue;
-		if ( m->minSMR       < Profiles [i].minSMR       ) continue;
-        if ( m->Bandwidth    < Profiles [i].BandWidth    ) continue;
-		if ( m->tmpMask_used < Profiles [i].tmpMask_used ) continue;
-		if ( m->CVD_used     < Profiles [i].CVD_used     ) continue;
-     // if ( varLtq       > Profiles [i].varLtq       ) continue;
-     // if ( NS_Order     < Profiles [i].NS_Order     ) continue;
-        if ( PNS          > Profiles [i].PNS          ) continue;
-        MainQual = i;
-    }
-    return MainQual;
-}
-
-
-static void
-SetQualityParams (PsyModel * m, float qual )
-{
-    int    i;
-    float  mix;
-
-    if      ( qual <  0. ) {
-        qual =  0.;
-    }
-    if      ( qual > 10. ) {
-        qual = 10.;
-#ifdef _WIN32
-        stderr_printf ( "\nmppenc: Can't open MACDll.dll, quality set to 10.0\n" );
-#else
-        stderr_printf ( "\nmppenc: Can't open libMAC.so, quality set to 10.0\n" );
-#endif
-    }
-
-    i   = (int) qual + PROFILE_PRE2_TELEPHONE;
-    mix = qual - (int) qual;
-
-    MainQual       = i;
-    m->ShortThr       = Profiles [i].ShortThr   * (1-mix) + Profiles [i+1].ShortThr   * mix;
-    m->MinValChoice   = Profiles [i].MinValChoice  ;
-    m->EarModelFlag   = Profiles [i].EarModelFlag  ;
-    m->Ltq_offset     = Profiles [i].Ltq_offset * (1-mix) + Profiles [i+1].Ltq_offset * mix;
-    m->varLtq         = Profiles [i].varLtq     * (1-mix) + Profiles [i+1].varLtq     * mix;
-    m->Ltq_max        = Profiles [i].Ltq_max    * (1-mix) + Profiles [i+1].Ltq_max    * mix;
-    m->TMN            = Profiles [i].TMN        * (1-mix) + Profiles [i+1].TMN        * mix;
-    m->NMT            = Profiles [i].NMT        * (1-mix) + Profiles [i+1].NMT        * mix;
-    m->minSMR         = Profiles [i].minSMR        ;
-    m->Bandwidth      = Profiles [i].BandWidth  * (1-mix) + Profiles [i+1].BandWidth  * mix;
-    m->tmpMask_used   = Profiles [i].tmpMask_used  ;
-    m->CVD_used       = Profiles [i].CVD_used      ;
-    m->MS_Channelmode = Profiles [i].MS_Channelmode;
-    CombPenalities = Profiles [i].CombPenalities;
-    m->NS_Order       = Profiles [i].NS_Order      ;
-    PNS            = Profiles [i].PNS        * (1-mix) + Profiles [i+1].PNS        * mix;
-    m->TransDetect    = Profiles [i].TransDetect* (1-mix) + Profiles [i+1].TransDetect* mix;
-}
+
 
 
@@ -1080,5 +949,5 @@
         else if ( 0 == strcmp ( arg, "pns") ) {                                         // pns
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            PNS = atof (argv[k]);
+            m->PNS = atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "minval") ) {                                      // MinValChoice
@@ -1152,5 +1021,5 @@
         else if ( 0 == strcmp ( arg, "newcomb") ) {
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            CombPenalities = atoi (argv[k]);
+            m->CombPenalities = atoi (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "ape1") ) {                                     // Mark APE as APE 1.000
@@ -1171,5 +1040,5 @@
         else if ( 0 == strcmp ( arg, "bw")  ||  0 == strcmp ( arg, "lowpass") ) {       // bandwidth
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-			m->Bandwidth = atof (argv[k]);
+			m->BandWidth = atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "displayupdatetime") ) {
@@ -1384,5 +1253,5 @@
                     "\n"
                     " SV %u.%u%s, Profile '%s'\n",
-                    inDatei, outDatei, 7, PNS > 0 ? 1 : 0, XLevel ? " + XLevel coding" : "", Profiles [MainQual] );
+                    inDatei, outDatei, 7, m->PNS > 0 ? 1 : 0, XLevel ? " + XLevel coding" : "", Profiles [m->MainQual] );
 
     if ( verbose > 0 ) {
@@ -1406,6 +1275,6 @@
 		if ( m->TMN != 18.0 || verbose > 1 )
 			stderr_printf ( " Tone masks Noise Ratio   : %4.1f dB\n", m->TMN );
-        if ( PNS > 0 )
-            stderr_printf ( " PNS Threshold            : %4.2f\n", PNS );
+        if ( m->PNS > 0 )
+            stderr_printf ( " PNS Threshold            : %4.2f\n", m->PNS );
 		if ( !m->tmpMask_used )
             stderr_printf ( " No exploitation of temporal post masking\n" );
@@ -1620,15 +1489,7 @@
 	m.SCF_Index_R = (int*) SCF_Index_R;
 
-    Init_SV7 ();
-	Init_Skalenfaktoren ();
 	Init_Psychoakustik (&m);
 	Init_FPU ();
-	Klemm    ();
-
-	e.dword = 0;
-	e.filled = 32;
-	e.Zaehler = 0;
-	e.BufferedBits = 0;
-	e.Overflows = 0;
+	Init_SV7 (&e);
 
     // initialize PCM-data
@@ -1651,5 +1512,5 @@
     }
 
-    m.SampleFreq    = Wave.SampleFreq;
+    m.SampleFreq = Wave.SampleFreq;
     SamplesInWAVE = Wave.PCMSamples;
 
@@ -1766,5 +1627,5 @@
     LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
     LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
-    WriteHeader_SV7 ( &e, m.Max_Band, MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
+    WriteHeader_SV7 ( &e, m.Max_Band, m.MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, m.PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
 
 
@@ -1841,6 +1702,6 @@
             }
 
-            Allocate ( m.Max_Band, Res_L, X[0].L, SCF_Index_L[0], m.SNR_comp_L, SMR.L, Power_L, Transient );   // allocate bits for left + right channel
-			Allocate ( m.Max_Band, Res_R, X[0].R, SCF_Index_R[0], m.SNR_comp_R, SMR.R, Power_R, Transient );
+            Allocate ( m.Max_Band, Res_L, X[0].L, SCF_Index_L[0], m.SNR_comp_L, SMR.L, Power_L, Transient , m.PNS );   // allocate bits for left + right channel
+			Allocate ( m.Max_Band, Res_R, X[0].R, SCF_Index_R[0], m.SNR_comp_R, SMR.R, Power_R, Transient , m.PNS );
 
 			Quantisierung (&m, m.Max_Band, Res_L, Res_R, X, Q );             // quantize samples
Index: /mppenc/branches/r2d/src/mppenc.h
===================================================================
--- /mppenc/branches/r2d/src/mppenc.h	(revision 76)
+++ /mppenc/branches/r2d/src/mppenc.h	(revision 77)
@@ -77,5 +77,7 @@
 void   Analyse_Filter(const PCMDataTyp*, SubbandFloatTyp*, const int);
 void   Analyse_Init ( float Left, float Right, SubbandFloatTyp* out, const int MaxBand );
-void   Klemm ( void );
+
+void SetQualityParams (PsyModel *, float );
+int TestProfileParams ( PsyModel* );
 
 extern const float  Butfly    [7];              // Antialiasing to calculate the subband powers
@@ -93,5 +95,4 @@
 #define invSCF  (__invSCF + 6)
 
-void   Init_Skalenfaktoren             ( void );
 float  ISNR_Schaetzer                  ( const float* samples, const float comp, const int res);
 float  ISNR_Schaetzer_Trans            ( const float* samples, const float comp, const int res);
@@ -111,5 +112,5 @@
 
 // FIXME : put in lib header
-void         Init_SV7             ( void );
+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         WriteBitstream_SV7   ( mpc_encoder_t*, const int, const SubbandQuantTyp* );
@@ -135,5 +136,5 @@
 void    Init_Tags        ( void );
 int     FinalizeTags     ( FILE* fp, unsigned int Version );
-int     addtag           ( const char* key, size_t keylen, const unsigned char* value, size_t valuelen, int converttoutf8, int flags );
+int     addtag           ( const char* key, size_t keylen, const char* value, size_t valuelen, int converttoutf8, int flags );
 int     gettag           ( const char* key, char* dst, size_t len );
 int     CopyTags         ( const char* filename );
Index: /mppenc/branches/r2d/src/tags.c
===================================================================
--- /mppenc/branches/r2d/src/tags.c	(revision 76)
+++ /mppenc/branches/r2d/src/tags.c	(revision 77)
@@ -600,5 +600,5 @@
 
 static int
-IsUnicode ( const unsigned char* src, size_t len )
+IsUnicode ( const char* src, size_t len )
 {
     if ( len <= 2 )
@@ -608,5 +608,5 @@
         return 0;
 
-    if ( src [0] != 0xFF  ||  src [1] != 0xFE )                 // Microsoft Unicode preample (also useful to detect endianess, but currently only little endian is supported)
+	if ( src [0] != 0xFFi  ||  src [1] != 0xFEi )                 // Microsoft Unicode preample (also useful to detect endianess, but currently only little endian is supported)
         return 0;
 
@@ -614,5 +614,5 @@
         if ( ( src [1] & 0xFC ) == 0xDC )
             return 0;
-        if ( src [1] == 0xFF  &&  ( src [0] & 0xFE ) == 0xFE )
+        if ( src [1] == 0xFFi  &&  ( src [0] & 0xFE ) == 0xFE )
             return 0;
         if ( ( src [1] & 0xFC ) == 0xD8 ) {
@@ -652,5 +652,5 @@
 addtag ( const char*           key,             // the item key
          size_t                keylen,          // length of item key, or 0 for auto-determine
-         const unsigned char*  value,           // the item value
+         const char*           value,           // the item value
          size_t                valuelen,        // the length of the item value (before any possible translation)
          int                   converttoutf8,   // convert flags of item value
@@ -659,5 +659,5 @@
     unsigned char*  p;
     unsigned char*  q;
-    unsigned char   ch;
+    char   ch;
     size_t          i;
 #ifdef _WIN32
@@ -716,5 +716,5 @@
     p = malloc ( keylen );
     memcpy ( p, key, keylen );
-    T [TagCount] . key    = p;
+	T [TagCount] . key    = (char*) p;
     T [TagCount] . keylen = keylen;
 
@@ -925,9 +925,9 @@
         return -1;
 
-    memcpy_crop  ( "Title"  , tmp +  3, 30, 0 );
-    memcpy_crop  ( "Artist" , tmp + 33, 30, 0 );
-    memcpy_crop  ( "Album"  , tmp + 63, 30, 0 );
-    memcpy_crop  ( "Year"   , tmp + 93,  4, 0 );
-    memcpy_crop  ( "Comment", tmp + 97, 30, 0 );
+	memcpy_crop  ( "Title"  , (char*)tmp +  3, 30, 0 );
+	memcpy_crop  ( "Artist" , (char*)tmp + 33, 30, 0 );
+	memcpy_crop  ( "Album"  , (char*)tmp + 63, 30, 0 );
+	memcpy_crop  ( "Year"   , (char*)tmp + 93,  4, 0 );
+	memcpy_crop  ( "Comment", (char*)tmp + 97, 30, 0 );
 
     if ( tmp[127] < sizeof(GenreList)/sizeof(*GenreList) )
@@ -937,6 +937,6 @@
     if ( tmp[125] == 0  &&  tmp[126] != 0 )
         if ( ! TagKeyExists ( "Track", 0 ) ) {
-            sprintf ( tmp, "%u",  tmp[126] );
-            addtag ("Track", 0, tmp, strlen (tmp), 0, 0 );
+		sprintf ( (char*)tmp, "%u",  tmp[126] );
+		addtag ("Track", 0, (char*)tmp, strlen ((char*)tmp), 0, 0 );
         }
 
@@ -991,7 +991,7 @@
         len   = Read_LE_Uint32 ( p );        p += 4;
         flags = Read_LE_Uint32 ( p );        p += 4;
-        strcpy ( key, p );                   p += strlen (key) + 1;
-        if ( ! TagKeyExists ( key, 0 ) )
-            addtag ( key, 0, p, len > 0  &&  p [len-1] == '\0'  ?  len-1  :  len, version >= 2000  ?  0  :  5, flags );
+		strcpy ( (char*)key, (char*)p );                   p += strlen ((char*)key) + 1;
+		if ( ! TagKeyExists ( (char*)key, 0 ) )
+			addtag ( (char*)key, 0, (char*)p, len > 0  &&  p [len-1] == '\0'  ?  len-1  :  len, version >= 2000  ?  0  :  5, flags );
                                              p += len;
     }
