Index: mppenc/branches/r2d/libmpcenc/tools.c
===================================================================
--- mppenc/branches/r2d/libmpcenc/tools.c	(revision 71)
+++ mppenc/branches/r2d/libmpcenc/tools.c	(revision 72)
@@ -33,4 +33,7 @@
 #include <stdio.h>
 
+// FIXME : not sure if it's a good idea
+#include <fpu_control.h>
+
 #include "libmpcenc.h"
 
@@ -47,9 +50,5 @@
 
     while ( bytes > 0 ) {
-#if defined _WIN32  &&  defined USE_HTTP  &&  !defined MPP_ENCODER
-        ret = fd & 0x4000  ?  recv ( fd & 0x3FFF, dest, bytes, 0)  :  read ( fd, dest, bytes );
-#else
         ret = read ( fd, dest, bytes );
-#endif
         if ( ret == 0  ||  ret == (mpc_size_t)-1 )
             break;
@@ -119,379 +118,4 @@
     return wordsread;
 }
-
-#ifndef MPP_ENCODER
-
-/*
- *  This is the main requantisation routine which does the following things:
- *
- *      - rescaling the quantized values (int) to their original value (float)
- *      - recalculating both stereo channels for MS stereo
- *
- *  See also: Requantize_IntensityStereo()
- *
- *  For performance reasons all cases are programmed separately and the code
- *  is unrolled.
- *
- *  Input is:
- *      - Stop_Band:
- *          the last band using MS or LR stereo
- *      - used_MS[Band]:
- *          MS or LR stereo flag for every band (0...Stop_Band), Value is 1
- *          for MS and 0 for LR stereo.
- *      - Res[Band].{L,R}:
- *          Quantisation resolution for every band (0...Stop_Band) and
- *          channels (L, R). Value is 0...17.
- *      - SCF_Index[3][Band].{L,R}:
- *          Scale factor for every band (0...Stop_Band), subframe (0...2)
- *          and channel (L, R).
- *      - Q[Band].{L,R}[36]
- *          36 subband samples for every band (0...Stop_Band) and channel (L, R).
- *      - SCF[64], Cc[18], Dc[18]:
- *          Lookup tables for Scale factor and Quantisation resolution.
- *
- *   Output is:
- *     - Y_L:  Left  channel subband signals
- *     - Y_R:  Right channel subband signals
- *
- *   These signals are used for the synthesis filter in the synth*.[ch]
- *   files to generate the PCM output signal.
- */
-
-static const float ISMatrix [32] [2] = {
-    {  1.00000000f,  0.00000000f },
-    {  0.98078528f,  0.19509032f },
-    {  0.92387953f,  0.38268343f },
-    {  0.83146961f,  0.55557023f },
-    {  0.70710678f,  0.70710678f },
-    {  0.55557023f,  0.83146961f },
-    {  0.38268343f,  0.92387953f },
-    {  0.19509032f,  0.98078528f },
-    {  0.00000000f,  1.00000000f },
-    { -0.19509032f,  0.98078528f },
-    { -0.38268343f,  0.92387953f },
-    { -0.55557023f,  0.83146961f },
-    { -0.70710678f,  0.70710678f },
-    { -0.83146961f,  0.55557023f },
-    { -0.92387953f,  0.38268343f },
-    { -0.98078528f,  0.19509032f },
-    { -1.00000000f,  0.00000000f },
-    { -0.98078528f, -0.19509032f },
-    { -0.92387953f, -0.38268343f },
-    { -0.83146961f, -0.55557023f },
-    { -0.70710678f, -0.70710678f },
-    { -0.55557023f, -0.83146961f },
-    { -0.38268343f, -0.92387953f },
-    { -0.19509032f, -0.98078528f },
-    { -0.00000000f, -1.00000000f },
-    {  0.19509032f, -0.98078528f },
-    {  0.38268343f, -0.92387953f },
-    {  0.55557023f, -0.83146961f },
-    {  0.70710678f, -0.70710678f },
-    {  0.83146961f, -0.55557023f },
-    {  0.92387953f, -0.38268343f },
-    {  0.98078528f, -0.19509032f },
-};
-
-
-void
-Requantize_MidSideStereo ( mpc_int_t Stop_Band, const mpc_bool_t* used_MS )
-{
-    mpc_int_t    Band;  // 0...Stop_Band
-    Uint   k;     // 0...35
-    Float  ML;
-    Float  MR;
-    Float  mid;
-    Float  side;
-
-    for ( Band = 0; Band <= Stop_Band; Band++ ) {
-
-        if ( used_MS[Band] )  // MidSide coded: left channel contains Mid signal, right channel Side signal
-            if      ( Res[Band].R < -1 ) {
-                k  = 0;
-                ML = SCF[SCF_Index[0][Band].L] * Cc[Res[Band].L];
-                do {
-                    mid = Q[Band].L[k] * ML;
-                    Y_L[k][Band] = mid * ISMatrix [used_MS[Band]][0];
-                    Y_R[k][Band] = mid * ISMatrix [used_MS[Band]][1];
-                } while (++k < 12);
-                ML = SCF[SCF_Index[1][Band].L] * Cc[Res[Band].L];
-                do {
-                    mid = Q[Band].L[k] * ML;
-                    Y_L[k][Band] = mid * ISMatrix [used_MS[Band]][0];
-                    Y_R[k][Band] = mid * ISMatrix [used_MS[Band]][1];
-                } while (++k < 24);
-                ML = SCF[SCF_Index[2][Band].L] * Cc[Res[Band].L];
-                do {
-                    mid = Q[Band].L[k] * ML;
-                    Y_L[k][Band] = mid * ISMatrix [used_MS[Band]][0];
-                    Y_R[k][Band] = mid * ISMatrix [used_MS[Band]][1];
-                } while (++k < 36);
-            }
-            else if ( Res[Band].L < -1 ) {
-                k  = 0;
-                ML = SCF[SCF_Index[0][Band].R] * Cc[Res[Band].R];
-                do {
-                    mid = Q[Band].R[k] * ML;
-                    Y_R[k][Band] = mid * ISMatrix [used_MS[Band]][0];
-                    Y_L[k][Band] = mid * ISMatrix [used_MS[Band]][1];
-                } while (++k < 12);
-                ML = SCF[SCF_Index[1][Band].R] * Cc[Res[Band].R];
-                do {
-                    mid = Q[Band].R[k] * ML;
-                    Y_R[k][Band] = mid * ISMatrix [used_MS[Band]][0];
-                    Y_L[k][Band] = mid * ISMatrix [used_MS[Band]][1];
-                } while (++k < 24);
-                ML = SCF[SCF_Index[2][Band].R] * Cc[Res[Band].R];
-                do {
-                    mid = Q[Band].R[k] * ML;
-                    Y_R[k][Band] = mid * ISMatrix [used_MS[Band]][0];
-                    Y_L[k][Band] = mid * ISMatrix [used_MS[Band]][1];
-                } while (++k < 36);
-            }
-            else if ( Res[Band].L )
-                if ( Res[Band].R ) {     //  M!=0, S!=0
-                    k  = 0;
-                    ML = SCF[SCF_Index[0][Band].L] * Cc[Res[Band].L];
-                    MR = SCF[SCF_Index[0][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = (mid = Q[Band].L[k] * ML) - (side = Q[Band].R[k] * MR);
-                        Y_L[k][Band] = mid + side;
-                    } while (++k < 12);
-                    ML = SCF[SCF_Index[1][Band].L] * Cc[Res[Band].L];
-                    MR = SCF[SCF_Index[1][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = (mid = Q[Band].L[k] * ML) - (side = Q[Band].R[k] * MR);
-                        Y_L[k][Band] = mid + side;
-                    } while (++k < 24);
-                    ML = SCF[SCF_Index[2][Band].L] * Cc[Res[Band].L];
-                    MR = SCF[SCF_Index[2][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = (mid = Q[Band].L[k] * ML) - (side = Q[Band].R[k] * MR);
-                        Y_L[k][Band] = mid + side;
-                    } while (++k < 36);
-                } else {                 //  M!=0, S=0
-                    k  = 0;
-                    ML = SCF[SCF_Index[0][Band].L] * Cc[Res[Band].L];
-                    do {
-                        Y_R[k][Band] =
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 12);
-                    ML = SCF[SCF_Index[1][Band].L] * Cc[Res[Band].L];
-                    do {
-                        Y_R[k][Band] =
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 24);
-                    ML = SCF[SCF_Index[2][Band].L] * Cc[Res[Band].L];
-                    do {
-                        Y_R[k][Band] =
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 36);
-                }
-            else
-                if ( Res[Band].R ) {     //  M==0, S!=0
-                    k  = 0;
-                    ML = SCF[SCF_Index[0][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = - (
-                        Y_L[k][Band] = Q[Band].R[k] * ML );
-                    } while (++k < 12);
-                    ML = SCF[SCF_Index[1][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = - (
-                        Y_L[k][Band] = Q[Band].R[k] * ML );
-                    } while (++k < 24);
-                    ML = SCF[SCF_Index[2][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = - (
-                        Y_L[k][Band] = Q[Band].R[k] * ML );
-                    } while (++k < 36);
-                } else {                 //  M==0, S==0
-                    for (k=0; k<36; k++) {
-                        Y_R[k][Band] =
-                        Y_L[k][Band] = 0.f;
-                    }
-                }
-
-        else                  // Left/Right coded: left channel contains left, right the right signal
-
-            if ( Res[Band].L )
-                if ( Res[Band].R ) {     //  L!=0, R!=0
-                    k  = 0;
-                    ML = SCF[SCF_Index[0][Band].L] * Cc[Res[Band].L];
-                    MR = SCF[SCF_Index[0][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = Q[Band].R[k] * MR;
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 12);
-                    ML = SCF[SCF_Index[1][Band].L] * Cc[Res[Band].L];
-                    MR = SCF[SCF_Index[1][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = Q[Band].R[k] * MR;
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 24);
-                    ML = SCF[SCF_Index[2][Band].L] * Cc[Res[Band].L];
-                    MR = SCF[SCF_Index[2][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = Q[Band].R[k] * MR;
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 36);
-                } else {                 //  L!=0, R==0
-                    k  = 0;
-                    ML = SCF[SCF_Index[0][Band].L] * Cc[Res[Band].L];
-                    do {
-                        Y_R[k][Band] = 0.f;
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 12);
-                    ML = SCF[SCF_Index[1][Band].L] * Cc[Res[Band].L];
-                    do {
-                        Y_R[k][Band] = 0.f;
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 24);
-                    ML = SCF[SCF_Index[2][Band].L] * Cc[Res[Band].L];
-                    do {
-                        Y_R[k][Band] = 0.f;
-                        Y_L[k][Band] = Q[Band].L[k] * ML;
-                    } while (++k < 36);
-                }
-            else
-                if ( Res[Band].R ) {     //  L==0, R!=0
-                    k  = 0;
-                    MR = SCF[SCF_Index[0][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = Q[Band].R[k] * MR;
-                        Y_L[k][Band] = 0.f;
-                    } while (++k < 12);
-                    MR = SCF[SCF_Index[1][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = Q[Band].R[k] * MR;
-                        Y_L[k][Band] = 0.f;
-                    } while (++k < 24);
-                    MR = SCF[SCF_Index[2][Band].R] * Cc[Res[Band].R];
-                    do {
-                        Y_R[k][Band] = Q[Band].R[k] * MR;
-                        Y_L[k][Band] = 0.f;
-                    } while (++k < 36);
-                } else {                 //  L==0, R==0
-                    for (k=0; k<36; k++) {
-                        Y_R[k][Band] =
-                        Y_L[k][Band] = 0.f;
-                    }
-                }
-
-    }
-    return;
-}
-
-
-/*
- *  This is the main requantisation routine for Intensity Stereo.
- *  It does the same as Requantize_MidSideStereo() but for IS.
- *
- *  Input is:
- *      - Stop_Band:
- *          the last band using MS or LR stereo
- *      - Res[Band].L:
- *          Quantisation resolution for every band (0...Stop_Band) and
- *          the left channel which is used for both channels. Value is 0...17.
- *      - SCF_Index[3][Band].{L,R}:
- *          Scale factor for every band (0...Stop_Band), subframe (0...2)
- *          and channel (L, R).
- *      - Q[Band].L[36]
- *          36 subband samples for every band (0...Stop_Band), both channels use
- *          the of the left channel
- *      - SCF[64], Cc[18], Dc[18]:
- *          Lookup tables for Scale factor and Quantisation resolution.
- *
- *   Output is:
- *     - Y_L:  Left  channel subband signals
- *     - Y_R:  Right channel subband signals
- *
- *   These signals are used for the synthesis filter in the synth*.[ch]
- *   files to generate the PCM output signal.
- */
-
-void
-Requantize_IntensityStereo ( mpc_int_t Start_Band, mpc_int_t Stop_Band )
-{
-    mpc_int_t    Band;  // Start_Band...Stop_Band
-    Uint   k;     // 0...35
-    Float  ML;
-    Float  MR;
-
-    for ( Band = Start_Band; Band <= Stop_Band; Band++ ) {
-
-        if ( Res[Band].L ) {
-            k  = 0;
-            ML = SCF[SCF_Index[0][Band].L] * Cc[Res[Band].L] * SS05;
-            MR = SCF[SCF_Index[0][Band].R] * Cc[Res[Band].L] * SS05;
-            do {
-                Y_R[k][Band] = Q[Band].L[k] * MR;
-                Y_L[k][Band] = Q[Band].L[k] * ML;
-            } while (++k < 12);
-            ML = SCF[SCF_Index[1][Band].L] * Cc[Res[Band].L] * SS05;
-            MR = SCF[SCF_Index[1][Band].R] * Cc[Res[Band].L] * SS05;
-            do {
-                Y_R[k][Band] = Q[Band].L[k] * MR;
-                Y_L[k][Band] = Q[Band].L[k] * ML;
-            } while (++k < 24);
-            ML = SCF[SCF_Index[2][Band].L] * Cc[Res[Band].L] * SS05;
-            MR = SCF[SCF_Index[2][Band].R] * Cc[Res[Band].L] * SS05;
-            do {
-                Y_R[k][Band] = Q[Band].L[k] * MR;
-                Y_L[k][Band] = Q[Band].L[k] * ML;
-            } while (++k < 36);
-        } else {
-            for (k=0; k<36; k++) {
-                Y_R[k][Band] =
-                Y_L[k][Band] = 0.f;
-            }
-        }
-
-    }
-    return;
-}
-
-
-/*
- *  Helper function for the qsort() in Resort_HuffTable() to sort a Huffman table
- *  by its codes.
- */
-
-static int Cdecl
-cmp_fn ( const void* p1, const void* p2 )
-{
-    if ( ((const Huffman_t*)p1) -> Code < ((const Huffman_t*)p2) -> Code ) return +1;
-    if ( ((const Huffman_t*)p1) -> Code > ((const Huffman_t*)p2) -> Code ) return -1;
-    return 0;
-}
-
-
-/*
- *  This functions sorts a Huffman table by its codes. It has also two other functions:
- *
- *    - The table contains LSB aligned codes, these are first MSB aligned.
- *    - The value entry is filled by its position plus 'offset' (Note that
- *      Make_HuffTable() don't fill this item. Offset can be used to offset
- *      range for instance from 0...6 to -3...+3.
- *
- *  Note that this function generates trash if you call it twice!
- */
-
-void
-Resort_HuffTable ( Huffman_t* const Table, const mpc_size_t elements, mpc_int_t offset )
-{
-    mpc_size_t  i;
-
-    for ( i = 0; i < elements; i++ ) {
-        Table[i].Value  = i + offset;
-        Table[i].Code <<= (32 - Table[i].Length);
-    }
-
-    qsort ( Table, elements, sizeof(*Table), cmp_fn );
-    return;
-}
-
-#endif /* MPP_ENCODER */
-
 
 /*
@@ -573,3 +197,3 @@
 }
 
-/* end of tools.c */
+
