Index: mppenc/branches/r2d/src/mppenc.c
===================================================================
--- mppenc/branches/r2d/src/mppenc.c	(revision 64)
+++ mppenc/branches/r2d/src/mppenc.c	(revision 65)
@@ -31,5 +31,4 @@
 
 /* MS-Coding */
-unsigned int  MS_Channelmode;              // global flag for enhanced functionality
 int           PredictionBands =  0;
 int           CombPenalities  = -1;
@@ -130,5 +129,5 @@
 
 static void
-longhelp ( void )
+longhelp ( PsyModel * m )
 {
     stderr_printf (
@@ -245,7 +244,7 @@
              "  --quality x      set Quality to x (dflt: 5)\n" );
     stderr_printf (
-             "  --nmt x          set NMT value to x dB (dflt: %4.1f)\n", NMT );
-    stderr_printf (
-             "  --tmn x          set TMN value to x dB (dflt: %4.1f)\n", TMN );
+             "  --nmt x          set NMT value to x dB (dflt: %4.1f)\n", m->NMT );
+    stderr_printf (
+             "  --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 );
@@ -253,7 +252,7 @@
              "==ATH/Bandwidth settings==\n" );
     stderr_printf (
-             "  --bw x           maximum bandwidth in Hz (dflt: %4.1f kHz)\n", (Max_Band+1)*(SampleFreq/32000.) );
-    stderr_printf (
-             "  --minSMR x       minimum SMR of x dB over encoded bandwidth (dflt: %2.1f)\n",  minSMR );
+             "  --bw x           maximum bandwidth in Hz (dflt: %4.1f kHz)\n", (m->Max_Band+1)*(m->SampleFreq/32000.) );
+    stderr_printf (
+             "  --minSMR x       minimum SMR of x dB over encoded bandwidth (dflt: %2.1f)\n",  m->minSMR );
     stderr_printf (
              "  --ltq xyy        x=0: ISO threshold in quiet (not recommended)\n"
@@ -265,11 +264,11 @@
              "                   y=00...99: HF roll-off (00:+30 dB, 99:-30 dB @20 kHz\n" );
     stderr_printf (
-             "  --ltq_gain x     add offset of x dB to chosen ltq (dflt: %+4.1f)\n",       Ltq_offset   );
-    stderr_printf (
-             "  --ltq_max x      maximum level for ltq (dflt: %4.1f dB)\n",                Ltq_max      );
-    stderr_printf (
-             "  --ltq_var x      adaptive threshold in quiet: 0: off, >0: on (dflt: %g)\n",varLtq       );
-    stderr_printf (
-             "  --tmpMask x      exploit postmasking: 0: off, 1: on (dflt: %i)\n",         tmpMask_used );
+             "  --ltq_gain x     add offset of x dB to chosen ltq (dflt: %+4.1f)\n",       m->Ltq_offset   );
+    stderr_printf (
+             "  --ltq_max x      maximum level for ltq (dflt: %4.1f dB)\n",                m->Ltq_max      );
+    stderr_printf (
+			"  --ltq_var x      adaptive threshold in quiet: 0: off, >0: on (dflt: %g)\n", m->varLtq    );
+    stderr_printf (
+			"  --tmpMask x      exploit postmasking: 0: off, 1: on (dflt: %i)\n",         m->tmpMask_used );
     stderr_printf (
              "==Stuff settings==========\n" );
@@ -277,13 +276,13 @@
              "  --ms x           Mid/Side Stereo, 0: off, 1: reduced, 2: on, 3: decoupled,\n"
              "                   10: enhanced 1.5/3 dB, 11: 2/6 dB, 12: 2.5/9 dB,\n"
-             "                   13: 3/12 dB, 15: 3/oo dB (dflt: %i)\n",                        MS_Channelmode );
-    stderr_printf (
-             "  --ans x          Adaptive Noise Shaping Order: 0: off, 1...6: on (dflt: %i)\n", NS_Order );
-    stderr_printf (
-             "  --cvd x          ClearVoiceDetection, 0: off, 1: on, 2: dual (dflt: %i)\n",     CVD_used );
-    stderr_printf (
-             "  --shortthr x     short FFT threshold (dflt: %4.1f)\n",                          ShortThr );
-    stderr_printf (
-             "  --transdet x     slewrate for transient detection (dflt: %3.1f)\n",             TransDetect );
+             "                   13: 3/12 dB, 15: 3/oo dB (dflt: %i)\n",                        m->MS_Channelmode );
+    stderr_printf (
+			"  --ans x          Adaptive Noise Shaping Order: 0: off, 1...6: on (dflt: %i)\n", m->NS_Order );
+    stderr_printf (
+			"  --cvd x          ClearVoiceDetection, 0: off, 1: on, 2: dual (dflt: %i)\n",     m->CVD_used );
+    stderr_printf (
+			"  --shortthr x     short FFT threshold (dflt: %4.1f)\n",                          m->ShortThr );
+    stderr_printf (
+             "  --transdet x     slewrate for transient detection (dflt: %3.1f)\n",             m->TransDetect );
     stderr_printf (
              "  --minval x       calculation of MinVal (1:Buschmann, 2,3:Klemm)\n" );
@@ -458,5 +457,5 @@
 
 static void
-SCF_Extraktion ( const int MaxBand, SubbandFloatTyp* x )
+SCF_Extraktion ( PsyModel*m, mpc_encoder_t* e, const int MaxBand, SubbandFloatTyp* x )
 {
     int    Band;
@@ -606,6 +605,6 @@
         tmp_R [1]         = invSCF [comp_R[1] - scfR[1]];
         tmp_R [2]         = invSCF [comp_R[2] - scfR[2]];
-        SNR_comp_L [Band] = (tmp_L[0]*tmp_L[0] + tmp_L[1]*tmp_L[1] + tmp_L[2]*tmp_L[2]) * 0.3333333333f;
-        SNR_comp_R [Band] = (tmp_R[0]*tmp_R[0] + tmp_R[1]*tmp_R[1] + tmp_R[2]*tmp_R[2]) * 0.3333333333f;
+        m->SNR_comp_L [Band] = (tmp_L[0]*tmp_L[0] + tmp_L[1]*tmp_L[1] + tmp_L[2]*tmp_L[2]) * 0.3333333333f;
+        m->SNR_comp_R [Band] = (tmp_R[0]*tmp_R[0] + tmp_R[1]*tmp_R[1] + tmp_R[2]*tmp_R[2]) * 0.3333333333f;
 
         // normalize the subband samples
@@ -633,10 +632,10 @@
             for ( n = 0; n < 36; n++ ) {
                 if      (x[Band].L[n] > +32767.f) {
-                    Overflows++;
+                    e->Overflows++;
                     MaxOverFlow = maxf (MaxOverFlow,  x[Band].L[n]);
                     x[Band].L[n] = 32767.f;
                 }
                 else if (x[Band].L[n] < -32767.f) {
-                    Overflows++;
+					e->Overflows++;
                     MaxOverFlow = maxf (MaxOverFlow, -x[Band].L[n]);
                     x[Band].L[n] = -32767.f;
@@ -646,10 +645,10 @@
             for ( n = 0; n < 36; n++ ) {
                 if      (x[Band].R[n] > +32767.f) {
-                    Overflows++;
+					e->Overflows++;
                     MaxOverFlow = maxf (MaxOverFlow,  x[Band].R[n]);
                     x[Band].R[n] = 32767.f;
                 }
                 else if (x[Band].R[n] < -32767.f) {
-                    Overflows++;
+					e->Overflows++;
                     MaxOverFlow = maxf (MaxOverFlow, -x[Band].R[n]);
                     x[Band].R[n] = -32767.f;
@@ -662,5 +661,6 @@
 
 static void
-Quantisierung ( const int               MaxBand,
+Quantisierung ( PsyModel * m,
+				const int               MaxBand,
                 const int*              resL,
                 const int*              resR,
@@ -676,6 +676,6 @@
 
         if ( *resL > 0 ) {
-            if ( NS_Order_L [Band] > 0 ) {
-                QuantizeSubbandWithNoiseShaping ( subq[Band].L, subx[Band].L, *resL, errorL [Band], FIR_L [Band] );
+            if ( m->NS_Order_L [Band] > 0 ) {
+                QuantizeSubbandWithNoiseShaping ( subq[Band].L, subx[Band].L, *resL, errorL [Band], m->FIR_L [Band] );
                 memcpy ( errorL [Band], errorL[Band] + 36, MAX_NS_ORDER * sizeof (**errorL) );
             } else {
@@ -687,6 +687,6 @@
 
         if ( *resR > 0 ) {
-            if ( NS_Order_R [Band] > 0 ) {
-                QuantizeSubbandWithNoiseShaping ( subq[Band].R, subx[Band].R, *resR, errorR [Band], FIR_R [Band] );
+            if ( m->NS_Order_R [Band] > 0 ) {
+                QuantizeSubbandWithNoiseShaping ( subq[Band].R, subx[Band].R, *resR, errorR [Band], m->FIR_R [Band] );
                 memcpy ( errorR [Band], errorR [Band] + 36, MAX_NS_ORDER * sizeof (**errorL) );
             } else {
@@ -793,27 +793,23 @@
 }
 
-
-
-
 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;
+	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
@@ -853,5 +849,5 @@
 
 static int
-TestProfileParams ( void )
+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
@@ -862,19 +858,19 @@
 
     for ( i = PROFILE_PRE2_TELEPHONE; i <= PROFILE_POST2_BRAINDEAD; i++ ) {
-        if ( ShortThr     > Profiles [i].ShortThr     ) continue;
-        if ( MinValChoice < Profiles [i].MinValChoice ) continue;
-        if ( EarModelFlag < Profiles [i].EarModelFlag ) continue;
-        if ( Ltq_offset   > Profiles [i].Ltq_offset   ) continue;
-        if ( Ltq_max      > Profiles [i].Ltq_max      ) continue;                     // offset should normally be considered here
-        if ( TMN + TMNStereoAdj [MS_Channelmode] <
+        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 ( NMT + NMTStereoAdj [MS_Channelmode] <
+        if ( m->NMT + NMTStereoAdj [m->MS_Channelmode] <
              Profiles [i].NMT + NMTStereoAdj [Profiles [i].MS_Channelmode] )
                                                         continue;
-        if ( minSMR       < Profiles [i].minSMR       ) continue;
-        if ( Bandwidth    < Profiles [i].BandWidth    ) continue;
-        if ( tmpMask_used < Profiles [i].tmpMask_used ) continue;
-        if ( CVD_used     < Profiles [i].CVD_used     ) 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;
@@ -887,5 +883,5 @@
 
 static void
-SetQualityParams ( float qual )
+SetQualityParams (PsyModel * m, float qual )
 {
     int    i;
@@ -908,21 +904,21 @@
 
     MainQual       = i;
-    ShortThr       = Profiles [i].ShortThr   * (1-mix) + Profiles [i+1].ShortThr   * mix;
-    MinValChoice   = Profiles [i].MinValChoice  ;
-    EarModelFlag   = Profiles [i].EarModelFlag  ;
-    Ltq_offset     = Profiles [i].Ltq_offset * (1-mix) + Profiles [i+1].Ltq_offset * mix;
-    varLtq         = Profiles [i].varLtq     * (1-mix) + Profiles [i+1].varLtq     * mix;
-    Ltq_max        = Profiles [i].Ltq_max    * (1-mix) + Profiles [i+1].Ltq_max    * mix;
-    TMN            = Profiles [i].TMN        * (1-mix) + Profiles [i+1].TMN        * mix;
-    NMT            = Profiles [i].NMT        * (1-mix) + Profiles [i+1].NMT        * mix;
-    minSMR         = Profiles [i].minSMR        ;
-    Bandwidth      = Profiles [i].BandWidth  * (1-mix) + Profiles [i+1].BandWidth  * mix;
-    tmpMask_used   = Profiles [i].tmpMask_used  ;
-    CVD_used       = Profiles [i].CVD_used      ;
-    MS_Channelmode = Profiles [i].MS_Channelmode;
+    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;
-    NS_Order       = Profiles [i].NS_Order      ;
+    m->NS_Order       = Profiles [i].NS_Order      ;
     PNS            = Profiles [i].PNS        * (1-mix) + Profiles [i+1].PNS        * mix;
-    TransDetect    = Profiles [i].TransDetect* (1-mix) + Profiles [i+1].TransDetect* mix;
+    m->TransDetect    = Profiles [i].TransDetect* (1-mix) + Profiles [i+1].TransDetect* mix;
 }
 
@@ -931,5 +927,5 @@
 
 static int
-EvalParameters ( int argc, char** argv, char** InputFile, char** OutputFile, int onlyfilenames )
+EvalParameters (PsyModel * m, int argc, char** argv, char** InputFile, char** OutputFile, int onlyfilenames )
 {
     int          k;
@@ -989,27 +985,27 @@
         }
         else if ( 0 == strcmp ( arg, "telephone" ) ) {                                   // MainQual
-            SetQualityParams (2.0);
+            SetQualityParams (m, 2.0);
         }
         else if ( 0 == strcmp ( arg, "thumb" ) ) {                                       // MainQual
-            SetQualityParams (3.0);
+            SetQualityParams (m, 3.0);
         }
         else if ( 0 == strcmp ( arg, "radio"   ) ) {
-            SetQualityParams (4.0);
+            SetQualityParams (m, 4.0);
         }
         else if ( 0 == strcmp ( arg, "standard")  ||  0 == strcmp ( arg, "normal") ) {
-            SetQualityParams (5.0);
+            SetQualityParams (m, 5.0);
         }
         else if ( 0 == strcmp ( arg, "xtreme")  ||  0 == strcmp ( arg, "extreme") ) {
-            SetQualityParams (6.0);
+            SetQualityParams (m, 6.0);
         }
         else if ( 0 == strcmp ( arg, "insane") ) {
-            SetQualityParams (7.0);
+            SetQualityParams (m, 7.0);
         }
         else if ( 0 == strcmp ( arg, "braindead") ) {
-            SetQualityParams (8.0);
+            SetQualityParams (m, 8.0);
         }
         else if ( 0 == strcmp ( arg, "quality") ) {                                      // Quality
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            SetQualityParams (atof (argv[k]) );
+            SetQualityParams (m, atof (argv[k]) );
         }
         else if ( 0 == strcmp ( arg, "neveroverwrite") ) {                              // NeverOverWrite
@@ -1037,5 +1033,5 @@
         else if ( 0 == strcmp ( arg, "kbd") ) {                                       // ScalingFactor
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            if ( 2 != sscanf ( argv[k], "%f,%f", &KBD1, &KBD2 ))
+			if ( 2 != sscanf ( argv[k], "%f,%f", &(m->KBD1), &(m->KBD2) ))
                 { stderr_printf ( "%s: missing two arguments", arg ); return -1; }
             Init_FFT ();
@@ -1073,6 +1069,6 @@
         else if ( 0 == strcmp ( arg, "ans") ) {                                         // AdaptiveNoiseShaping
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            NS_Order = atoi (argv[k]);
-            NS_Order = mini ( NS_Order, MAX_NS_ORDER );
+            m->NS_Order = atoi (argv[k]);
+            m->NS_Order = mini ( m->NS_Order, MAX_NS_ORDER );
         }
         else if ( 0 == strcmp ( arg, "predict") ) {                                     // AdaptiveNoiseShaping
@@ -1083,5 +1079,5 @@
         else if ( 0 == strcmp ( arg, "ltq_var")  ||  0 == strcmp ( arg, "ath_var") ) {  // ltq_var
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            varLtq = atof (argv[k]);
+            m->varLtq = atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "pns") ) {                                         // pns
@@ -1091,13 +1087,13 @@
         else if ( 0 == strcmp ( arg, "minval") ) {                                      // MinValChoice
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            MinValChoice = atoi (argv[k]);
+            m->MinValChoice = atoi (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "transdet") ) {                                    // TransDetect
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            TransDetect = (float) atof (argv[k]);
+            m->TransDetect = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "shortthr") ) {                                    // ShortThr
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            ShortThr = (float) atof (argv[k]);
+            m->ShortThr = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "noxlevel") ) {                                      // Xlevel
@@ -1109,37 +1105,37 @@
         else if ( 0 == strcmp ( arg, "nmt") ) {                                         // NMT
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg );  return -1; }
-            NMT = (float) atof (argv[k]);
+            m->NMT = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "tmn") ) {                                         // TMN
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg );  return -1; }
-            TMN = (float) atof (argv[k]);
+            m->TMN = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "cvd") ) {                                         // ClearVoiceDetection
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            CVD_used = atoi (argv[k]);
-            if ( CVD_used == 0 )
+            m->CVD_used = atoi (argv[k]);
+            if ( m->CVD_used == 0 )
                 stderr_printf ( "\nDisabling CVD always reduces quality!\a\n" );
         }
         else if ( 0 == strcmp ( arg, "ms") ) {                                          // Mid/Side Stereo
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            MS_Channelmode = atoi (argv[k]);
+            m->MS_Channelmode = atoi (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "minSMR") ) {                                      // minimum SMR
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            if ( minSMR > (float) atof (argv[k]) )
+            if ( m->minSMR > (float) atof (argv[k]) )
                 stderr_printf ( "This option usage may reduces quality!\a\n" );
-            minSMR = (float) atof (argv[k]);
+            m->minSMR = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "tmpMask") ) {                                     // temporal post-masking
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            tmpMask_used = atoi (argv[k]);
+            m->tmpMask_used = atoi (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "ltq_max")  ||  0 == strcmp ( arg, "ath_max") ) {  // Maximum for threshold in quiet
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg );  return -1; }
-            Ltq_max = (float) atof (argv[k]);
+			m->Ltq_max = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "ltq_gain")  ||  0 == strcmp ( arg, "ath_gain") ) {// Offset for threshold in quiet
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            Ltq_offset = (float) atof (argv[k]);
+			m->Ltq_offset = (float) atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "silent")  ||  0 == strcmp ( arg, "quiet") ) {
@@ -1152,5 +1148,5 @@
         else if ( 0 == strcmp ( arg, "ltq")  ||  0 == strcmp ( arg, "ath") ) {          // threshold in quiet
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            EarModelFlag = atoi (argv[k]);
+            m->EarModelFlag = atoi (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "noco") ) {
@@ -1178,5 +1174,5 @@
         else if ( 0 == strcmp ( arg, "bw")  ||  0 == strcmp ( arg, "lowpass") ) {       // bandwidth
             if ( ++k >= argc ) { stderr_printf ( errmsg, arg ); return -1; }
-            Bandwidth = atof (argv[k]);
+			m->Bandwidth = atof (argv[k]);
         }
         else if ( 0 == strcmp ( arg, "displayupdatetime") ) {
@@ -1353,5 +1349,5 @@
     }
 
-    TestProfileParams ();
+    TestProfileParams (m);
     return 0;
 }
@@ -1359,5 +1355,5 @@
 
 static void
-ShowParameters ( char* inDatei, char* outDatei )
+ShowParameters (PsyModel * m, char* inDatei, char* outDatei )
 {
     static const char        unk      []       = "???";
@@ -1399,26 +1395,26 @@
         if ( ScalingFactorr != 1.  ||  ScalingFactorl != 1.  ||  verbose > 1 )
             stderr_printf ( " Scaling input by         : left %.5f, right: %.5f\n", ScalingFactorl, ScalingFactorr );
-        stderr_printf ( " Maximum encoded bandwidth: %4.1f kHz\n", (Max_Band+1) * (SampleFreq/32./2000.) );
-        stderr_printf ( " Adaptive Noise Shaping   : max. %s order\n", th [NS_Order] );
-        stderr_printf ( " Clear Voice Detection    : %s\n", able [CVD_used] );
-        stderr_printf ( " Mid/Side Stereo          : %s\n", stereo [MS_Channelmode] );
+        stderr_printf ( " Maximum encoded bandwidth: %4.1f kHz\n", (m->Max_Band+1) * (m->SampleFreq/32./2000.) );
+        stderr_printf ( " Adaptive Noise Shaping   : max. %s order\n", th [m->NS_Order] );
+        stderr_printf ( " Clear Voice Detection    : %s\n", able [m->CVD_used] );
+        stderr_printf ( " Mid/Side Stereo          : %s\n", stereo [m->MS_Channelmode] );
         stderr_printf ( " Threshold of Hearing     : Model %3u: %s, Max ATH: %2.0f dB, Offset: %+1.0f dB, +Offset@20 kHz:%3.0f dB\n",
-                        EarModelFlag,
-                        EarModelFlag/100 < sizeof(EarModel)/sizeof(*EarModel) ? EarModel [EarModelFlag/100] : unk,
-                        Ltq_max,
-                        Ltq_offset,
-                        -0.6 * (int) (EarModelFlag % 100 - 50) );
-        if ( NMT !=  6.5 || verbose > 1 )
-            stderr_printf ( " Noise masks Tone Ratio   : %4.1f dB\n", NMT );
-        if ( TMN != 18.0 || verbose > 1 )
-            stderr_printf ( " Tone masks Noise Ratio   : %4.1f dB\n", TMN );
+                        m->EarModelFlag,
+						m->EarModelFlag/100 < sizeof(EarModel)/sizeof(*EarModel) ? EarModel [m->EarModelFlag/100] : unk,
+                        m->Ltq_max,
+                        m->Ltq_offset,
+                        -0.6 * (int) (m->EarModelFlag % 100 - 50) );
+		if ( m->NMT !=  6.5 || verbose > 1 )
+			stderr_printf ( " Noise masks Tone Ratio   : %4.1f dB\n", m->NMT );
+		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 ( !tmpMask_used )
+		if ( !m->tmpMask_used )
             stderr_printf ( " No exploitation of temporal post masking\n" );
         else if ( verbose > 1 )
             stderr_printf ( " Exploitation of temporal post masking\n" );
-        if ( minSMR > 0. )
-            stderr_printf ( " Minimum Signal-to-Mask   : %4.1f dB\n", minSMR );
+		if ( m->minSMR > 0. )
+			stderr_printf ( " Minimum Signal-to-Mask   : %4.1f dB\n", m->minSMR );
         else if ( verbose > 1 )
             stderr_printf ( " No minimum SMR (psycho model controlled filtering)\n" );
@@ -1440,8 +1436,8 @@
 
 static const char*
-PrintTime ( UintMax_t samples, int sign )
+PrintTime ( PsyModel* m, UintMax_t samples, int sign )
 {
     static char  ret [32];
-    Ulong        tmp  = (Ulong) ( UintMAX_FP(samples) * 100. / SampleFreq );
+	Ulong        tmp  = (Ulong) ( UintMAX_FP(samples) * 100. / m->SampleFreq );
     Uint         hour = (Uint)  ( tmp / 360000     );
     Uint         min  = (Uint)  ( tmp / 6000 %  60 );
@@ -1450,5 +1446,5 @@
 
 
-    if ( UintMAX_FP(samples) >= SampleFreq * 360000. )
+	if ( UintMAX_FP(samples) >= m->SampleFreq * 360000. )
         return "            ";
     else if ( hour > 9 )
@@ -1467,5 +1463,6 @@
 
 static void
-ShowProgress ( UintMax_t  samples,
+ShowProgress ( PsyModel* m,
+			   UintMax_t  samples,
                UintMax_t  total_samples,
                UintMax_t  databits )
@@ -1491,6 +1488,6 @@
 
     percent     = 100.f    * UintMAX_FP(samples) / UintMAX_FP(total_samples);
-    kbps        =   1.e-3f * UintMAX_FP(databits) * SampleFreq / UintMAX_FP(samples);
-    speed       =   1.f    * UintMAX_FP(samples) * (CLOCKS_PER_SEC / SampleFreq) / (unsigned long)(curr - start) ;
+	kbps        =   1.e-3f * UintMAX_FP(databits) * m->SampleFreq / UintMAX_FP(samples);
+	speed       =   1.f    * UintMAX_FP(samples) * (CLOCKS_PER_SEC / m->SampleFreq) / (unsigned long)(curr - start) ;
     total_estim =   1.f    * UintMAX_FP(total_samples) / UintMAX_FP(samples) * (unsigned long)(curr - start);
 
@@ -1508,13 +1505,13 @@
 
     // 2x duration in WAVE file time (encoded/total)
-    stderr_printf ("%10.10s" , PrintTime ( samples      , (char)' ')+1 );
-    stderr_printf ("%10.10s ", PrintTime ( total_samples, (char)' ')+1 );
+    stderr_printf ("%10.10s" , PrintTime ( m, samples      , (char)' ')+1 );
+    stderr_printf ("%10.10s ", PrintTime ( m, total_samples, (char)' ')+1 );
 
     // 2x coding time (encoded/total)
-    stderr_printf ("%10.10s" , PrintTime ( (curr - start) * (SampleFreq/CLOCKS_PER_SEC), (char)' ')+1 );
-    stderr_printf ("%10.10s ", PrintTime ( total_estim    * (SampleFreq/CLOCKS_PER_SEC), (char)' ')+1 );
+	stderr_printf ("%10.10s" , PrintTime ( m, (curr - start) * (m->SampleFreq/CLOCKS_PER_SEC), (char)' ')+1 );
+	stderr_printf ("%10.10s ", PrintTime ( m, total_estim    * (m->SampleFreq/CLOCKS_PER_SEC), (char)' ')+1 );
 
     // ETA
-    stderr_printf ( "%10.10s\r", samples < total_samples  ?  PrintTime ((total_estim - curr + start) * (SampleFreq/CLOCKS_PER_SEC), (char)' ')+1  :  "" );
+	stderr_printf ( "%10.10s\r", samples < total_samples  ?  PrintTime (m, (total_estim - curr + start) * (m->SampleFreq/CLOCKS_PER_SEC), (char)' ')+1  :  "" );
     fflush ( stderr );
 
@@ -1542,4 +1539,27 @@
 	unsigned n;
 	for(n=0;n<count;n++) buffer[n] = val;
+}
+
+
+void
+		OverdriveReport ( mpc_encoder_t * e )
+{
+	if ( e->Overflows > 0 ) {                                                // report internal clippings
+		if ( XLevel == 0 ) {
+			stderr_printf ( "\n"
+					"\033[1m\rWARNING:\n"
+					"\033[0m\r  There occured %u internal clippings due to a restriction of StreamVersion 7.\n"
+					"  Re-encode with '--scale %.3f', or use option '--xlevel', which normally can\n"
+					"  handle this situation but don't work well with old decoders.\a\n\n",
+			e->Overflows, ScalingFactorl * 32767. / MaxOverFlow - 0.0005f );
+		}
+		else {
+			stderr_printf ( "\n"
+					"\033[1m\rWARNING:\n"
+					"\033[0m\r  There still occured %u SCF clippings due to a restriction of StreamVersion 7.\n"
+					"  Use the '--scale' method to avoid additional distortions. Note that this\n"
+					"  file already has annoying distortions due to slovenly CD mastering.\a\n\n", e->Overflows );
+		}
+	}
 }
 
@@ -1569,5 +1589,18 @@
     int              TransientR [PART_SHORT];   // Flag of transient detection
     int              Transient  [32];           // Flag of transient detection
-
+	PsyModel         m;
+	mpc_encoder_t    e;
+
+    // initialize tables which must be initialized once and only once
+#ifdef FAST_MATH
+    Init_FastMath ();                           // check if something has to be done for each file !!
+#endif
+    Init_SV7 ();
+    Init_Psychoakustiktabellen (&m);
+	Init_Skalenfaktoren ();
+	Init_Psychoakustik (&m);
+	Init_FPU ();
+	Init_ANS ();
+	Klemm    ();
 
     // initialize PCM-data
@@ -1575,5 +1608,5 @@
 
     // open WAV file
-    if ( EvalParameters ( argc, argv, &InputName, &OutputName, 1 ) < 0 )
+    if ( EvalParameters (&m,  argc, argv, &InputName, &OutputName, 1 ) < 0 )
         return 1;
     if ( Open_WAV_Header ( &Wave, InputName ) < 0 ) {
@@ -1590,5 +1623,5 @@
     }
 
-    SampleFreq    = Wave.SampleFreq;
+    m.SampleFreq    = Wave.SampleFreq;
     SamplesInWAVE = Wave.PCMSamples;
 
@@ -1617,7 +1650,7 @@
     }
 
-    SetQualityParams (5.0);
-
-    if ( EvalParameters ( argc, argv, &InputName, &OutputName, 0 ) < 0 )
+    SetQualityParams (&m, 5.0);
+
+    if ( EvalParameters (&m, argc, argv, &InputName, &OutputName, 0 ) < 0 )
         return 1;
 
@@ -1626,5 +1659,5 @@
     }
 
-    Init_Psychoakustiktabellen ();              // must be done AFTER decoding command line parameters
+    Init_Psychoakustiktabellen (&m);              // must be done AFTER decoding command line parameters
 
     // check fade-length
@@ -1684,10 +1717,10 @@
 #endif
 
-    ShowParameters ( InputName, OutputName );
+    ShowParameters (&m, InputName, OutputName );
     if ( WIN32_MESSAGES  &&  FrontendPresent )
         SendModeMessage (MainQual);
 
     if ( SkipTime > 0. ) {
-        unsigned long  SkipSamples = SampleFreq * SkipTime;
+        unsigned long  SkipSamples = m.SampleFreq * SkipTime;
         ssize_t        read;
 
@@ -1701,11 +1734,11 @@
     }
 
-    BufferedBits     = 0;
+    e.BufferedBits     = 0;
     LastValidFrame   = (SamplesInWAVE + BLOCK - 1) / BLOCK;
     LastValidSamples = (SamplesInWAVE + BLOCK - 1) - BLOCK * LastValidFrame + 1;
-    WriteHeader_SV7 ( Max_Band, MainQual, MS_Channelmode > 0, LastValidFrame, LastValidSamples, PNS > 0 ? 0x17 : 0x07, SampleFreq );
+    WriteHeader_SV7 ( m.Max_Band, MainQual, m.MS_Channelmode > 0, LastValidFrame, LastValidSamples, PNS > 0 ? 0x17 : 0x07, m.SampleFreq );
 
     // initialize timer
-    ShowProgress ( 0, SamplesInWAVE, BufferedBits );
+    ShowProgress (&m, 0, SamplesInWAVE, e.BufferedBits );
     T            = time ( NULL );
 
@@ -1722,10 +1755,10 @@
 	}
 
-	Analyse_Init ( Main.L[CENTER], Main.R[CENTER], X, Max_Band );
+	Analyse_Init ( Main.L[CENTER], Main.R[CENTER], X, m.Max_Band );
 
     // adapt SamplesInWAVE to the real number of contained samples
     if ( myfeof (Wave.fp) ) {
         stderr_printf ( "WAVE file has incorrect header: header: %.3f s, contents: %.3f s    \n",
-                        UintMAX_FP(AllSamplesRead) / SampleFreq, UintMAX_FP(SamplesInWAVE) / SampleFreq );
+						UintMAX_FP(AllSamplesRead) / m.SampleFreq, UintMAX_FP(SamplesInWAVE) / m.SampleFreq );
         SamplesInWAVE = AllSamplesRead;
 
@@ -1766,37 +1799,37 @@
 
         if ( !Silence  ||  !OldSilence ) {
-            Analyse_Filter ( &Main, X, Max_Band );                      // Analysis-Filterbank (Main -> X)
-            SMR = Psychoakustisches_Modell ( Max_Band*0+31, &Main, TransientL, TransientR );    // Psychoacoustics return SMRs for input data 'Main'
-            if ( minSMR > 0 )
-                RaiseSMR ( Max_Band, &SMR );                            // Minimum-operation on SBRs (full bandwidth)
-            if ( MS_Channelmode > 0 )
-                MS_LR_Entscheidung ( Max_Band, MS_Flag, &SMR, X );      // Selection of M/S- or L/R-Coding
-            SCF_Extraktion ( Max_Band, X );                             // Extraction of the scalefactors and normalization of the subband samples
+            Analyse_Filter ( &Main, X, m.Max_Band );                      // Analysis-Filterbank (Main -> X)
+			SMR = Psychoakustisches_Modell (&m, m.Max_Band*0+31, &Main, TransientL, TransientR );    // Psychoacoustics return SMRs for input data 'Main'
+            if ( m.minSMR > 0 )
+				RaiseSMR (&m, m.Max_Band, &SMR );                            // Minimum-operation on SBRs (full bandwidth)
+			if ( m.MS_Channelmode > 0 )
+				MS_LR_Entscheidung ( m.Max_Band, MS_Flag, &SMR, X );      // Selection of M/S- or L/R-Coding
+			SCF_Extraktion (&m, &e, m.Max_Band, X );                             // Extraction of the scalefactors and normalization of the subband samples
             TransientenCalc ( Transient, TransientL, TransientR );
-            if ( NS_Order > 0 ) {
-                NS_Analyse ( Max_Band, MS_Flag, SMR, Transient );                  // calculate possible ANS-Filter and the expected gain
+			if ( m.NS_Order > 0 ) {
+				NS_Analyse (&m, m.Max_Band, MS_Flag, SMR, Transient );                  // calculate possible ANS-Filter and the expected gain
             }
 
-            Allocate ( Max_Band, Res_L, X[0].L, SCF_Index_L[0], SNR_comp_L, SMR.L, Power_L, Transient );   // allocate bits for left + right channel
-            Allocate ( Max_Band, Res_R, X[0].R, SCF_Index_R[0], SNR_comp_R, SMR.R, Power_R, Transient );
-
-            Quantisierung ( Max_Band, Res_L, Res_R, X, Q );             // quantize samples
-        }
-
-        if ( Zaehler >= BUFFER_ALMOST_FULL  ||  LowDelay ) {
-            FlushBitstream ( OutputFile, Buffer, Zaehler );
-            Zaehler = 0;
+            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 );
+
+			Quantisierung (&m, m.Max_Band, Res_L, Res_R, X, Q );             // quantize samples
+        }
+
+        if ( e.Zaehler >= BUFFER_ALMOST_FULL  ||  LowDelay ) {
+            FlushBitstream ( OutputFile, e.Buffer, e.Zaehler );
+            e.Zaehler = 0;
          }
 
         OldSilence      = Silence;
-        OldBufferedBits = BufferedBits;
+        OldBufferedBits = e.BufferedBits;
         GetBitstreamPos    ( &bitstreampos );
         WriteBits          ( 0, 20 );                                                      // Reserve 20 bits for jump-information
-        WriteBitstream_SV7 ( Max_Band, Q );                                                // write SV7-Bitstream
-        WriteBitsAt        ( (Uint32_t)(BufferedBits - OldBufferedBits - 20), 20, bitstreampos );      // Patch 20 bits for jump-information to the right value
+        WriteBitstream_SV7 ( m.Max_Band, Q );                                                // write SV7-Bitstream
+        WriteBitsAt        ( (Uint32_t)(e.BufferedBits - OldBufferedBits - 20), 20, bitstreampos );      // Patch 20 bits for jump-information to the right value
 
         if ( (Int)(time (NULL) - T) >= 0 ) {                            // output
             T += labs (DisplayUpdateTime);
-            ShowProgress ( (UintMax_t)(N+1) * BLOCK, SamplesInWAVE, BufferedBits );
+            ShowProgress (&m, (UintMax_t)(N+1) * BLOCK, SamplesInWAVE, e.BufferedBits );
         }
 
@@ -1821,5 +1854,5 @@
         if ( myfeof (Wave.fp) ) {
             stderr_printf ( "WAVE file has incorrect header: header: %.3f s, contents: %.3f s    \n",
-                            UintMAX_FP(AllSamplesRead) / SampleFreq, UintMAX_FP(SamplesInWAVE) / SampleFreq );
+							UintMAX_FP(AllSamplesRead) / m.SampleFreq, UintMAX_FP(SamplesInWAVE) / m.SampleFreq );
             SamplesInWAVE = AllSamplesRead;
 
@@ -1843,8 +1876,8 @@
     // write the last incomplete word to buffer, so it's written during the next flush
     FinishBitstream();
-    ShowProgress ( SamplesInWAVE, SamplesInWAVE, BufferedBits );
-
-    FlushBitstream ( OutputFile, Buffer, Zaehler );
-    Zaehler = 0;
+    ShowProgress (&m, SamplesInWAVE, SamplesInWAVE, e.BufferedBits );
+
+    FlushBitstream ( OutputFile, e.Buffer, e.Zaehler );
+    e.Zaehler = 0;
 
     UpdateHeader ( OutputFile, LastValidFrame, LastValidSamples );
@@ -1862,28 +1895,8 @@
 
     stderr_printf ( "\n" );
+
+	OverdriveReport (&e);                         // output a report if clipping was necessary
+
     return 0;
-}
-
-
-void
-OverdriveReport ( void )
-{
-    if ( Overflows > 0 ) {                                                // report internal clippings
-        if ( XLevel == 0 ) {
-            stderr_printf ( "\n"
-                            "\033[1m\rWARNING:\n"
-                            "\033[0m\r  There occured %u internal clippings due to a restriction of StreamVersion 7.\n"
-                            "  Re-encode with '--scale %.3f', or use option '--xlevel', which normally can\n"
-                            "  handle this situation but don't work well with old decoders.\a\n\n",
-                            Overflows, ScalingFactorl * 32767. / MaxOverFlow - 0.0005f );
-        }
-        else {
-            stderr_printf ( "\n"
-                            "\033[1m\rWARNING:\n"
-                            "\033[0m\r  There still occured %u SCF clippings due to a restriction of StreamVersion 7.\n"
-                            "  Use the '--scale' method to avoid additional distortions. Note that this\n"
-                            "  file already has annoying distortions due to slovenly CD mastering.\a\n\n", Overflows );
-        }
-    }
 }
 
@@ -1915,5 +1928,6 @@
     // no arguments or call for help
     if ( argc < 2  ||  0==strcmp (argv[1],"-h")  ||  0==strcmp (argv[1],"-?")  ||  0==strcmp (argv[1],"--help") ) {
-        SetQualityParams (5.0);
+		PsyModel m;
+        SetQualityParams (&m, 5.0);
         dup2 ( 1, 2 );
         shorthelp ();
@@ -1922,25 +1936,12 @@
 
     if ( 0==strcmp (argv[1],"--longhelp")  ||  0==strcmp (argv[1],"-??") ) {
-        SetQualityParams (5.0);
+		PsyModel m;
+        SetQualityParams (&m, 5.0);
         dup2 ( 1, 2 );
-        longhelp ();
+        longhelp (&m);
         return 1;
     }
 
-    // initialize tables which must be initialized once and only once
-#ifdef FAST_MATH
-    Init_FastMath ();                           // check if something has to be done for each file !!
-#endif
-    Init_SV7 ();
-    Init_Psychoakustiktabellen ();
-    Init_Skalenfaktoren ();
-    Init_Psychoakustik ();
-    Init_FPU ();
-    Init_ANS ();
-    Klemm    ();
-
     ret = mainloop ( argc, argv );              // analyze command line and do the requested work
-
-    OverdriveReport ();                         // output a report if clipping was necessary
 
     if(IsEndBeep)
