Version 0.94.1 of the bladeenc mp3 encoder
authorkramm <kramm>
Thu, 10 Jan 2002 17:29:59 +0000 (17:29 +0000)
committerkramm <kramm>
Thu, 10 Jan 2002 17:29:59 +0000 (17:29 +0000)
27 files changed:
lib/bladeenc/codec.c [new file with mode: 0644]
lib/bladeenc/codec.h [new file with mode: 0644]
lib/bladeenc/common.c [new file with mode: 0644]
lib/bladeenc/common.h [new file with mode: 0644]
lib/bladeenc/encode.c [new file with mode: 0644]
lib/bladeenc/encoder.h [new file with mode: 0644]
lib/bladeenc/formatbitstream2.c [new file with mode: 0644]
lib/bladeenc/formatbitstream2.h [new file with mode: 0644]
lib/bladeenc/huffman.h [new file with mode: 0644]
lib/bladeenc/i.c [new file with mode: 0644]
lib/bladeenc/l3bitstream-pvt.h [new file with mode: 0644]
lib/bladeenc/l3bitstream.c [new file with mode: 0644]
lib/bladeenc/l3bitstream.h [new file with mode: 0644]
lib/bladeenc/l3psy.c [new file with mode: 0644]
lib/bladeenc/l3psy.h [new file with mode: 0644]
lib/bladeenc/l3side.h [new file with mode: 0644]
lib/bladeenc/loop-pvt.h [new file with mode: 0644]
lib/bladeenc/loop.c [new file with mode: 0644]
lib/bladeenc/loop.h [new file with mode: 0644]
lib/bladeenc/mdct.c [new file with mode: 0644]
lib/bladeenc/mdct.h [new file with mode: 0644]
lib/bladeenc/reservoir.c [new file with mode: 0644]
lib/bladeenc/reservoir.h [new file with mode: 0644]
lib/bladeenc/subs.c [new file with mode: 0644]
lib/bladeenc/system.h [new file with mode: 0644]
lib/bladeenc/tables.c [new file with mode: 0644]
lib/bladeenc/tables.h [new file with mode: 0644]

diff --git a/lib/bladeenc/codec.c b/lib/bladeenc/codec.c
new file mode 100644 (file)
index 0000000..c6697ea
--- /dev/null
@@ -0,0 +1,432 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    CHANGES    ------------
+
+       2000-11-22  Andre Piotrowski
+
+       -       big fix: module - original dist10 error - l3_sb_sample has to be of type 'L3SBS' not 'SBS'.
+       -       bug fix: codecInit() - don't reset fInit-huffman_read_flag - read_huffcodetab() is a memory eater!
+
+       2000-12-11  ap
+
+       -       reformatted
+       -       speed up: use single buffer
+*/
+
+#include       <stdlib.h>
+#include       <assert.h>
+
+#include       "common.h"
+#include       "l3psy.h"
+#include       "mdct.h"
+#include       "reservoir.h"
+#include       "formatbitstream2.h"
+#include       "l3bitstream.h"
+#include       "loop.h"
+#include       "encoder.h"
+#include       "codec.h"
+
+
+
+
+
+extern int                             fInit_fft;
+
+
+
+
+
+/************************************************************************/
+
+#define        SAMPLES_PER_FRAME               1152
+
+static L3SBS                                   l3_sb_sample;
+
+static layer                                   info;
+
+
+
+#if ORG_BUFFERS
+static short                                   buffer[2][1152];
+/*     static  float                                   snr32[32]; */
+static short                                   sam[2][2048];
+#else
+static FLOAT                                   buffer[2][2048];
+static int                                             buffer_idx;
+#endif
+
+
+
+static int                                             whole_SpF;
+
+static double                                  frac_SpF, slot_lag;
+
+static int                                             stereo, error_protection;
+
+static III_side_info_t                 l3_side;
+static CodecInitOut                    sOut;
+
+static frame_params                    fr_ps;
+
+
+
+char                                   *pEncodedOutput;
+int                                            outputBit;
+
+volatile double                        avg_slots_per_frame;
+
+
+
+
+
+/*____ codecInit() ____________________________________________________________*/
+
+CodecInitOut                   *codecInit (CodecInitIn *psIn)
+{
+       int                                             j;
+
+       /* Read psIn */
+
+       switch (psIn->frequency)
+       {
+               case 48000:  info.sampling_frequency = 1;  break;
+               case 44100:  info.sampling_frequency = 0;  break;
+               case 32000:  info.sampling_frequency = 2;  break;
+               default   :  return FALSE;
+       }
+
+       switch (psIn->mode)
+       {
+               case  0:  info.mode = MPG_MD_STEREO      ;  info.mode_ext = 0;  break;
+               case  2:  info.mode = MPG_MD_DUAL_CHANNEL;  info.mode_ext = 0;  break;
+               case  3:  info.mode = MPG_MD_MONO        ;  info.mode_ext = 0;  break;
+               default:  return FALSE;
+       }
+
+       j = 0;
+       while (j < 15  &&  bitratex[1][j] != psIn->bitrate)
+               j++;
+       info.bitrate_index    = j;
+
+       info.version          = 1;   /* Default: MPEG-1 */
+       info.emphasis         = psIn->emphasis;
+       info.extension        = psIn->fPrivate;
+       info.copyright        = psIn->fCopyright;
+       info.original         = psIn->fOriginal;
+       info.error_protection = psIn->fCRC;
+
+
+/*_______ Static-fix _______________*/
+
+
+       fInit_mdct_sub = 0;
+       fInit_mdct = 0;
+
+       fInit_fft = 0;
+
+       fInit_iteration_loop = 0;
+/*     DON'T DO THAT ALL THE TIME
+       fInit_huffman_read_flag = 0;
+*/
+
+       fixStatic_loop();
+
+       l3_side.main_data_begin = 0;
+       fixStatic_reservoir();
+
+
+/*___________________________________*/
+
+    psycho_anal_init (psIn->frequency);
+       initWindowFilterSubband ();
+       initFormatBitstream ();
+
+/*     clear buffers */
+       memset ((char *) l3_sb_sample, 0, sizeof(l3_sb_sample));
+    memset((char *) buffer, 0, sizeof(buffer));
+/*    memset((char *) snr32, 0, sizeof(snr32));*/
+#if ORG_BUFFERS
+    memset((char *) sam, 0, sizeof(sam));
+#endif
+
+
+
+    fr_ps.header         = &info;
+    fr_ps.tab_num        = -1;   /* no table loaded */
+    fr_ps.alloc          = NULL;
+    fr_ps.actual_mode = info.mode;
+    fr_ps.stereo         = (info.mode == MPG_MD_MONO) ? 1 : 2;
+       fr_ps.sblimit     = SBLIMIT;
+    fr_ps.jsbound        = SBLIMIT;
+
+    
+    stereo = fr_ps.stereo;
+    error_protection = info.error_protection;
+
+    avg_slots_per_frame =
+               ((double) SAMPLES_PER_FRAME / s_freq[1][info.sampling_frequency]) *
+               ((double) bitratex[1][info.bitrate_index] / 8.0);
+    whole_SpF = (int) avg_slots_per_frame;
+    frac_SpF  = avg_slots_per_frame - (double) whole_SpF;
+    slot_lag  = -frac_SpF;
+
+/*    if (frac_SpF == 0)
+       info.padding = 0;
+*/
+       genNoisePowTab();
+
+/*________________________*/
+
+
+       if( stereo != 2 )
+               sOut.nSamples = SAMPLES_PER_FRAME;
+       else
+               sOut.nSamples = SAMPLES_PER_FRAME*2;
+
+       sOut.bufferSize = 2048;
+
+       return  &sOut;                  /* How many samples we want in each chunk... */
+}
+
+
+
+
+
+/*____ codecEncodeChunk() _____________________________________________________*/
+
+unsigned int                   codecEncodeChunk
+(
+       int                                             nSamples,
+       short                                   *pSamples,
+       char                                    *pDest
+)
+{
+       static  double                          xr[2][2][576];
+       static  double                          xr_dec[2][2][576];
+       static  double                          pe[2][2];
+       static  int                                     l3_enc[2][2][576];
+       static  III_psy_ratio           ratio;
+       static  III_scalefac_t          scalefac;
+       int                                                     gr, ch;
+       int                                                     mean_bits, sideinfo_len;
+       int                                                     bitsPerFrame;
+       int                                                     j;
+
+/*
+       static  int                                     fFirst = TRUE;
+
+       if (fFirst)
+       {
+               memset ((char *) &xr      , 0, sizeof(xr));
+               memset ((char *) &xr_dec  , 0, sizeof(xr_dec));
+               memset ((char *) &pe      , 0, sizeof(pe));
+               memset ((char *) &l3_enc  , 0, sizeof(l3_enc));
+               memset ((char *) &ratio   , 0, sizeof(ratio));
+               memset ((char *) &l3_side , 0, sizeof(l3_side));
+               memset ((char *) &scalefac, 0, sizeof(scalefac));
+
+               fFirst = FALSE;
+       }
+*/
+
+
+/* rebuffer audio */
+
+#if ORG_BUFFERS
+       rebuffer_audio (buffer, pSamples, nSamples, stereo);
+#else
+       rebuffer_audio (pSamples, buffer, &buffer_idx, nSamples, stereo);
+#endif
+
+
+/* psychoacoustic model */
+
+       for (gr = 0;  gr < 2;  gr++)
+               for (ch = 0;  ch < stereo;  ch++)
+                       psycho_anal
+                       (
+#if ORG_BUFFERS
+                               &buffer[ch][gr*576],
+                               &sam[ch][0],
+#else
+                       buffer[ch],
+                       (buffer_idx+gr*576) & 2047,
+#endif
+                               ch,
+                               3,
+                               /*snr32,*/
+                               &ratio.l[gr][ch][0],
+                               &ratio.s[gr][ch][0],
+                               &pe[gr][ch],
+                               &l3_side.gr[gr].ch[ch].tt
+                       );
+
+
+/* polyphase filtering */
+
+       for (gr = 0;  gr < 2;  gr++)
+       {
+               int             gr_plus_1 = gr_idx[gr+1];
+
+               for (ch = 0;  ch < stereo;  ch++)
+               {
+                       for (j = 0;  j < 18;  j++)
+                       {
+                               windowFilterSubband
+                               (
+#if ORG_BUFFERS
+                                       &buffer[ch][gr*18*32+32*j],
+                                       ch,
+#else
+                                       buffer[ch],
+                                       (buffer_idx+768-480+gr*18*32+32*j) & 2047,
+#endif
+                                       l3_sb_sample[ch][gr_plus_1][j]
+                               );
+                       }
+               }
+       }
+
+
+/* apply mdct to the polyphase outputs */
+
+       mdct_sub (&l3_sb_sample, xr, stereo, &l3_side, 2);
+
+
+       pEncodedOutput = pDest;
+       outputBit = 8;
+       pEncodedOutput[0] = 0;
+
+
+       if (frac_SpF != 0)
+       {
+               if (slot_lag > (frac_SpF-1.0))
+               {
+                       slot_lag -= frac_SpF;
+                       info.padding = 0;
+               }
+               else
+               {
+                       info.padding = 1;
+                       slot_lag += (1-frac_SpF);
+               }
+       }
+
+       bitsPerFrame = 8 * whole_SpF + (info.padding * 8);
+
+
+/* determine the mean bitrate for main data */
+
+       sideinfo_len = 32;
+
+       if (stereo == 1)
+               sideinfo_len += 136;
+       else
+               sideinfo_len += 256;
+
+       if (info.error_protection)
+               sideinfo_len += 16;
+
+       mean_bits = (bitsPerFrame - sideinfo_len) / 2;
+
+
+/* bit and noise allocation */
+
+       iteration_loop
+       (
+               pe,
+               xr,
+               &ratio,
+               &l3_side,
+               l3_enc,
+               mean_bits,
+               stereo,
+               xr_dec,
+               &scalefac,
+               &fr_ps,
+               0,
+               bitsPerFrame
+       );
+
+
+/* write the frame to the bitstream */
+
+       III_format_bitstream
+       (
+               bitsPerFrame,
+               &fr_ps,
+               l3_enc,
+               &l3_side,
+               &scalefac,
+               xr,
+               NULL,
+               0
+       );
+
+
+       return  pEncodedOutput - pDest;
+}
+
+
+
+
+
+/*____ codecExit() ____________________________________________________________*/
+
+unsigned int                   codecExit (char *pDest)
+{
+       pEncodedOutput = pDest;
+       outputBit = 8;
+       pEncodedOutput[0] = 0;
+
+       psycho_anal_exit ();
+       exitFormatBitstream ();
+       III_FlushBitstream ();
+
+       return pEncodedOutput - pDest;
+}
+
+
+
+
+
+/*____ codecFlush() _________________________________________________________*/
+
+unsigned int                   codecFlush (char *pDest)
+{
+       pEncodedOutput = pDest;
+       outputBit = 8;
+       pEncodedOutput[0] = 0;
+
+       flushFrame ();
+
+       whole_SpF = (int) avg_slots_per_frame;
+       frac_SpF  = avg_slots_per_frame - (double) whole_SpF;
+       slot_lag  = -frac_SpF;
+
+       l3_side.main_data_begin = 0;
+       fixStatic_reservoir ();
+
+       return pEncodedOutput - pDest;
+}
+
+
+
diff --git a/lib/bladeenc/codec.h b/lib/bladeenc/codec.h
new file mode 100644 (file)
index 0000000..63a48ba
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression.
+
+               This file doesn't contain any of the ISO reference code and
+               is copyright Tord Jansson (tord.jansson@swipnet.se).
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    CHANGES    ------------
+
+       2000-12-10  Andre Piotrowski
+
+       -       reformatted
+*/
+
+#ifndef                __CODEC__
+#define                __CODEC__
+
+
+
+
+
+typedef                struct
+                       {
+                               int                                             frequency;   /* 48000, 44100 and 32000 allowed. */
+                               int                                             mode;        /* 0 = Stereo, 2 = Dual Channel, 3 = Mono */
+                               int                                             bitrate;     
+                               int                                             emphasis;    /* 0 = None, 1 = 50/15 microsec, 3 = CCITT J.17 */
+                               int                                             fPrivate;               
+                               int                                             fCRC;
+                               int                                             fCopyright;
+                               int                                             fOriginal;
+                       }                                               CodecInitIn;
+
+
+typedef                struct
+                       {
+                               int                                             nSamples;
+                               int                                             bufferSize;
+                       }                                               CodecInitOut;
+
+
+
+extern CodecInitOut    *codecInit (CodecInitIn *psInitData);
+
+extern unsigned int    codecEncodeChunk (int nSamples, short *pSamples, char *pOutput);
+
+extern unsigned int    codecExit (char *pOutput);
+
+extern unsigned int    codecFlush (char *pOutput);
+
+
+
+
+
+#endif         /* __CODEC__ */
diff --git a/lib/bladeenc/common.c b/lib/bladeenc/common.c
new file mode 100644 (file)
index 0000000..0ffddc2
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-11-22  Andre Piotrowski
+
+       -       bug fix: mem_alloc() - no lomger a need to allocate block*2 bytes
+*/
+
+/***********************************************************************
+*
+*  Global Include Files
+*
+***********************************************************************/
+
+#include       <string.h> /* 1995-07-11 shn */
+#include       <ctype.h>
+#include       <stdlib.h>
+
+#include    "common.h"
+
+
+
+
+
+/***********************************************************************
+*
+*  Global Variable Definitions
+*
+***********************************************************************/
+
+/* 1: MPEG-1, 0: MPEG-2 LSF, 1995-07-11 shn */
+double                                 s_freq[2][4] =
+{
+       {22.05, 24, 16, 0},
+       {44.1 , 48, 32, 0}
+};
+
+/* 1: MPEG-1, 0: MPEG-2 LSF, 1995-07-11 shn */
+int                                            bitratex[2][15] =
+{
+       {   0,  8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
+       {   0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
+};
+
+
+
+
+
+/*******************************************************************************
+*
+*  Allocate number of bytes of memory equal to "block".
+*
+*******************************************************************************/
+
+void                                   *mem_alloc (unsigned int block, char *item)
+{
+       void                                    *ptr;
+
+       ptr = (void *) malloc (block);
+
+       memset (ptr, 0, block);
+
+       return ptr;
+}
+
+
+
+
+
+/****************************************************************************
+*
+*  Free memory pointed to by "*ptr_addr".
+*
+*****************************************************************************/
+
+void                                   mem_free (void **ptr_addr)
+{
+       if (*ptr_addr != NULL)
+       {
+               free (*ptr_addr);
+               *ptr_addr = NULL;
+       }
+}
+
+
+
diff --git a/lib/bladeenc/common.h b/lib/bladeenc/common.h
new file mode 100644 (file)
index 0000000..cf5987f
--- /dev/null
@@ -0,0 +1,267 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-02-07  Andre Piotrowski
+
+       -       reformatted
+*/
+
+#ifndef                __COMMON__
+#define                __COMMON__
+
+
+
+
+
+/***********************************************************************
+*
+*  Global Include Files
+*
+***********************************************************************/
+
+#include       <stdio.h>
+#include       <string.h>
+#include       <math.h>
+
+#include       "tables.h"
+
+
+
+
+
+#define                MIN(A, B)                               ((A) < (B) ? (A) : (B))
+#define                MAX(A, B)                               ((A) > (B) ? (A) : (B))
+
+
+
+
+
+#ifndef EXIT_SUCCESS
+#define                EXIT_SUCCESS                    0
+#endif
+
+#ifndef EXIT_FAILURE
+#define                EXIT_FAILURE                    1
+#endif
+
+
+
+
+
+/***********************************************************************
+*
+*  Global Definitions
+*
+***********************************************************************/
+
+/* General Definitions */
+
+#define                FLOAT                                   float
+#ifndef FALSE
+#define                FALSE                                   0
+#endif
+
+#ifndef TRUE
+#define                TRUE                                    1
+#endif
+
+#define                NULL_CHAR                               '\0'
+
+#define                MAX_U_32_NUM                    0xFFFFFFFF
+
+#ifndef PI
+#define                PI                                              3.14159265358979
+#endif
+#define                PI4                                             PI/4
+#define                PI64                                    PI/64
+
+#define                LN_TO_LOG10                             0.2302585093
+
+#define                VOL_REF_NUM                             0
+#define                MPEG_AUDIO_ID                   1
+#define                MPEG_PHASE2_LSF                 0       /* 1995-07-11 SHN */
+
+#if 0
+#define                MONO                                    1
+#define                STEREO                                  2
+#endif
+
+#define                BITS_IN_A_BYTE                  8
+#define                WORD                                    16
+#define                MAX_NAME_SIZE                   81
+#define                SBLIMIT                                 32
+#define                SSLIMIT                                 18
+#define                FFT_SIZE                                1024
+#define                HAN_SIZE                                512
+#define                SCALE_BLOCK                             12
+#define                SCALE_RANGE                             64
+#define                SCALE                                   32768
+#define                CRC16_POLYNOMIAL                0x8005
+
+
+/* MPEG Header Definitions - Mode Values */
+
+#define                MPG_MD_STEREO                   0
+#define                MPG_MD_DUAL_CHANNEL             2
+#define                MPG_MD_MONO                             3
+
+
+/* Mode Extention */
+
+#define                MPG_MD_LR_LR                    0
+#define                MPG_MD_LR_I                             1
+#define                MPG_MD_MS_LR                    2
+#define                MPG_MD_MS_I                             3
+
+
+/* "bit_stream.h" Definitions */
+
+#define                MINIMUM                                 4    /* Minimum size of the buffer in bytes */
+#define                MAX_LENGTH                              32   /* Maximum length of word written or
+                                                                       read from bit stream */
+
+#define                READ_MODE                               0
+#define                WRITE_MODE                              1
+#define                ALIGNING                                8
+
+
+
+#define                BUFFER_SIZE                             4096
+
+
+
+
+
+/***********************************************************************
+*
+*  Global Type Definitions
+*
+***********************************************************************/
+
+
+/* Structure for Reading Layer II Allocation Tables from File */
+
+typedef                struct
+                       {
+                               unsigned int                    steps;
+                               unsigned int                    bits;
+                               unsigned int                    group;
+                               unsigned int                    quant;
+                       } sb_alloc, *alloc_ptr;
+
+typedef                sb_alloc                                al_table[SBLIMIT][16];
+
+
+/* Header Information Structure */
+
+typedef                struct
+                       {
+                               int                                             version;
+                               int                                             error_protection;
+                               int                                             bitrate_index;
+                               int                                             sampling_frequency;
+                               int                                             padding;
+                               int                                             extension;
+                               int                                             mode;
+                               int                                             mode_ext;
+                               int                                             copyright;
+                               int                                             original;
+                               int                                             emphasis;
+/*                             int                                             freq; */
+                       } layer, *the_layer;
+
+
+/* Parent Structure Interpreting some Frame Parameters in Header */
+
+typedef                struct
+                       {
+                               layer                                   *header;       /* raw header information */
+                               int                                             actual_mode;   /* when writing IS, may forget if 0 chs */
+                               al_table                                *alloc;        /* bit allocation table read in */
+                               int                                             tab_num;       /* number of table as loaded */
+                               int                                             stereo;        /* 1 for mono, 2 for stereo */
+                               int                                             jsbound;       /* first band of joint stereo coding */
+                               int                                             sblimit;       /* total number of sub bands */
+                       } frame_params;
+
+
+
+enum byte_order { order_unknown, order_bigEndian, order_littleEndian };
+extern enum byte_order NativeByteOrder;
+
+
+/* "bit_stream.h" Type Definitions */
+
+typedef                struct bit_stream_struc
+                       {
+                               FILE                                    *pt;            /* pointer to bit stream device */
+                               unsigned char                   *buf;           /* bit stream buffer */
+                               int                                             buf_size;       /* size of buffer (in number of bytes) */
+                               int                                             totbit;         /* bit counter of bit stream */
+                               int                                             buf_byte_idx;   /* pointer to top byte in buffer */
+                               int                                             buf_bit_idx;    /* pointer to top bit of top byte in buffer */
+                               int                                             mode;           /* bit stream open in read or write mode */
+                               int                                             eob;            /* end of buffer index */
+                               int                                             eobs;           /* end of bit stream flag */
+                               char                                    format;         /* format of file in rd mode (BINARY/ASCII) */
+                       } Bit_stream_struc;
+
+
+
+
+
+#include       "l3side.h"
+
+/***********************************************************************
+*
+*  Global Variable External Declarations
+*
+***********************************************************************/
+
+/*
+extern char                    *mode_names[4]; 
+extern char                    *layer_names[3]; 
+extern char                    *version_names[2];
+*/
+extern double                  s_freq[2][4];
+extern int                             bitratex[2][15];
+
+
+
+
+
+/***********************************************************************
+*
+*  Global Function Prototype Declarations
+*
+***********************************************************************/
+
+/* The following functions are in the file "common.c" */
+
+extern void                    *mem_alloc (unsigned int block, char *item);
+extern void                    mem_free (void **ptr_addr);
+
+
+
+
+
+#endif         /* __COMMON__ */
diff --git a/lib/bladeenc/encode.c b/lib/bladeenc/encode.c
new file mode 100644 (file)
index 0000000..30ed93a
--- /dev/null
@@ -0,0 +1,1106 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       =======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-11-06  Andre Piotrowski
+
+       -       speed up: complete new, much faster functions 'create_ana_filter()', 'windowFilterSubband()'
+       -                 'WIND_SB_CHANGE_LEVEL' 3 requires a new 'enwindow[]' (see 'tables.c')
+
+       2000-11-22      ap
+
+       -       bug fix: initWindowFilterSubband() -- Dividing the enwindow-entries is allowed to be done once only!
+
+       2000-12-11  ap
+
+       -       speed up: WIND_SB_CHANGE_LEVEL 4
+
+       2001-01-12  ap
+
+       -       bug fix: fixed some typo relevant in case of ORG_BUFFERS == 1
+*/
+
+#include "common.h"
+#include "encoder.h"
+
+
+
+
+
+/*  ========================================================================================  */
+/*      rebuffer_audio                                                                        */
+/*  ========================================================================================  */
+
+#if ORG_BUFFERS
+
+void rebuffer_audio
+(
+       short                                   buffer[2][1152],
+       short                                   *insamp,
+       unsigned int                    samples_read,
+       int                                             stereo
+)
+{
+       unsigned int                    j;
+
+       if (stereo == 2)
+       { 
+               for (j = 0;  j < samples_read/2;  j++)
+               {
+                       buffer[0][j] = insamp[2*j];
+                       buffer[1][j] = insamp[2*j+1];
+               }
+       }
+       else
+       {               
+               for (j = 0;  j < samples_read;  j++)
+               {
+                       buffer[0][j] = insamp[j];
+                       buffer[1][j] = 0;
+               }
+       }
+
+       for( ;  j < 1152;  j++)
+       {
+               buffer[0][j] = 0;
+               buffer[1][j] = 0;
+       }
+
+       return;
+}
+
+#else
+
+#define                FLUSH                                   1152
+#define                DELAY                                    768
+
+void rebuffer_audio
+(
+       const short                             *insamp,
+       FLOAT                                   buffer[2][2048],
+       int                                             *buffer_idx,
+       unsigned int                    samples_read,
+       int                                             stereo
+)
+{
+       int                                             idx, med, fin;
+
+       /* flush the last two read granules */
+       *buffer_idx = (*buffer_idx + FLUSH) & 2047;
+       idx = (*buffer_idx + DELAY) & 2047;
+       fin = (idx + FLUSH) & 2047;
+
+       if (stereo == 2)
+       {
+               med = (idx + samples_read/2) & 2047;
+               if (idx >= med)
+               {
+                       while (idx < 2048)
+                       {
+                               buffer[0][idx] = *insamp++;
+                               buffer[1][idx] = *insamp++;
+                               idx++;
+                       }
+                       idx = 0;
+               }
+               while (idx < med)
+               {
+                       buffer[0][idx] = *insamp++;
+                       buffer[1][idx] = *insamp++;
+                       idx++;
+               }
+       }
+       else
+       {               
+               med = (idx + samples_read) & 2047;
+               if (idx >= med)
+               {
+                       while (idx < 2048)
+                       {
+                               buffer[0][idx] = *insamp++;
+                               buffer[1][idx] = 0;
+                               idx++;
+                       }
+                       idx = 0;
+               }
+               while (idx < med)
+               {
+                       buffer[0][idx] = *insamp++;
+                       buffer[1][idx] = 0;
+                       idx++;
+               }
+       }
+
+       if (idx != fin)
+       {
+               if (idx > fin)
+               {
+                       while (idx < 2048)
+                       {
+                               buffer[0][idx] = 0;
+                               buffer[1][idx] = 0;
+                               idx++;
+                       }
+                       idx = 0;
+               }
+               while (idx < fin)
+               {
+                       buffer[0][idx] = 0;
+                       buffer[1][idx] = 0;
+                       idx++;
+               }
+       }
+}
+
+#endif
+
+
+
+
+
+#if ORG_BUFFERS
+#define WIND_SB_CHANGE_LEVEL   3
+#else
+#define WIND_SB_CHANGE_LEVEL   4
+#endif
+
+
+
+#if WIND_SB_CHANGE_LEVEL==4
+
+       typedef double                  filter_matrix[SBLIMIT/4][32];
+
+       static  filter_matrix   m;
+
+       /*  ========================================================================================  */
+       /*      create_ana_filter                                                                     */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filterbank coefficients and rounds to the 9th decimal place
+               accuracy of the filterbank tables in the ISO document.
+               The coefficients are stored in #new_filter#
+
+
+               Originally was computed
+
+                       filter[i][j]  :=  cos (PI64 * ((2*i+1) * (16-j)))
+
+               for i = 0..SBLIMIT-1 and j = 0..63 .
+
+
+               But for j = 0..15 is 16-j = -(16-(32-j)) which implies
+
+               (1)             filter[i][j]  =  filter[i][32-j] .
+
+               (We know that filter[i][16] = cos(0) = 1 , but that is not so interesting.)
+
+               Furthermore, for j = 33..48 we get
+
+                                  filter[i][96-j]
+                               =  cos (PI64 * (2*i+1) * (j-80))
+                               =  cos (PI * (2*i+1) + PI64 * (2*i+1) * (16-j))
+               (2)             =  cos (PI * (2*i+1)) * cos (PI64 * (2*i+1) * (16-j))   // sin (PI * (2*i+1)) = 0
+                               =  -cos (PI64 * (2*i+1) * (16-j))
+                               =  -filter[i][j] ,
+
+               especially is filter[i][48] = 0 .
+
+
+               On the other hand there is
+
+                                  filter[31-i][j]
+                               =  cos (PI64 * (2*(31-i)+1) * (16-j))
+                               =  cos (PI64 * (64-(2*i+1)) * (16-j))
+                               =  cos (PI * (16-j) - PI64 * (2*i+1) * (16-j))
+                               =  cos (PI * (16-j)) * cos (PI64 * (2*i+1) * (16-j))   // sin (PI * (16-j)) = 0
+                               =  (-1)^^j * cos (PI64 * (2*i+1) * (16-j))
+                               =  (-1)^^j * filter[i][j] .
+
+
+               There is a third somewhat more complication "symmetry":
+
+                                  filter[15-i][j]
+                               =  cos (PI64 * (2*(15-i)+1) * (16-j))
+                               =  cos (PI64 * (32-(2*i+1)) * (16-j))
+                               =  cos (PI/2 * (16-j) - PI64 * (2*i+1) * (16-j))
+                               =  cos (PI/2 * (16-j)) * cos (PI64 * (2*i+1) * (16-j)) + sin (PI/2 * (16-j)) * sin (PI64 * (2*i+1) * (16-j))
+                               =  cos (PI/2 * (16-j)) * cos (PI64 * (2*i+1) * (16-j)) + sin (PI/2 * (16-j)) * cos (PI64 * (2*i+1) * (-1)^^i * (16-(j+32)))
+
+                               =  (-1)^^(  j  /2    ) * filter[i][j   ]   for even j
+                               =  (-1)^^((j+1)/2 + i) * filter[i][j+32]   for odd  j  with  j <  32
+                               =  (-1)^^((j-1)/2 + i) * filter[i][j-32]   for odd  j  with  j >= 32
+
+
+               For these reasons we create a filter of the eighth size only and set
+
+                       new_filter[i][j]  :=  filter[i][j]       for i = 0..SBLIMIT/4-1 and j =  0..16
+                       new_filter[i][j]  :=  filter[i][j+16]    for i = 0..SBLIMIT/4-1 and j = 17..31
+       */
+
+       static  void create_ana_filter (double new_filter[SBLIMIT/4][32])
+       {
+               register int                    i, j;
+               double                                  t;
+
+               for (i = 0;  i < SBLIMIT/4;  i++)
+               {
+                       for (j = 0;  j <= 16;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * (16-j)));
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               new_filter[i][j] = t * 1e-9;
+                       }
+                       for (j = 17;  j < 32;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * j));   /* (16-(j+16)) */
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               new_filter[i][j] = t * 1e-9;
+                       }
+               }
+       }
+
+       /*  ========================================================================================  */
+       /*      windowFilterSubband                                                                   */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filter bank coefficients
+
+               The windowed samples #z# is filtered by the digital filter matrix #m# to produce the
+               subband samples #s#. This done by first selectively picking out values from the windowed
+               samples, and then multiplying them by the filter matrix, producing 32 subband samples.
+       */
+
+       void  windowFilterSubband
+       (
+               const FLOAT                             *buffer,
+               int                                             buffer_idx,
+               double                                  s[SBLIMIT]
+       )
+       {
+               int                                             i, k;
+
+               double                                  t, u, v, w;
+
+               double                                  z[32];
+               double                                  *p;
+               double                                  *pEnw;
+
+               pEnw = enwindow;
+
+               for (k = 0;  k <= 16;  k++)
+               {
+                       t =  buffer[(buffer_idx+511) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+447) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+383) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+319) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+255) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+191) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+127) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+ 63) & 2047] * *pEnw++;
+                       z[k] = t;
+
+                       buffer_idx--;
+               }                       
+
+               for (k = 15;  k >= 0;  k--)
+               {
+                       t =  buffer[(buffer_idx+511) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+447) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+383) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+319) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+255) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+191) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+127) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+ 63) & 2047] * *pEnw++;
+                       z[k] += t;
+
+                       buffer_idx--;
+               }                       
+
+               for (k = 17;  k < 32;  k++)
+               {
+                       t =  buffer[(buffer_idx+511) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+447) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+383) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+319) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+255) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+191) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+127) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+ 63) & 2047] * *pEnw++;
+                       z[k] = t;
+
+                       buffer_idx--;
+               }                       
+
+               /* normally y[48] was computed like the other entries, */
+               /* but this entry is not needed because m[i][48] = 0.  */
+               buffer_idx--; /* But we have to increase the pointers. */
+               pEnw += 8;
+
+               for (k = 31;  k > 16;  k--)
+               {
+                       t =  buffer[(buffer_idx+511) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+447) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+383) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+319) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+255) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+191) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+127) & 2047] * *pEnw++;
+                       t += buffer[(buffer_idx+ 63) & 2047] * *pEnw++;
+                       z[k] -= t;
+
+                       buffer_idx--;
+               }                       
+
+               for (i = 0;  i < SBLIMIT/4;  i++)
+               {
+                       p = m[i];
+
+                       t = u = v = w = 0.0;
+                       for (k = 0;  k < 16;  k += 4)
+                       {
+                               t += p[k   ] * z[k  ];
+                               v += p[k+ 2] * z[k+2];
+                               u += p[k+ 1] * z[k+1] + p[k+ 3] * z[k+3];
+                               w -= p[k+17] * z[k+1] - p[k+19] * z[k+3];
+                       }
+                       for ( ;  k < 32;  k += 4)
+                       {
+                               t += p[k   ] * z[k  ];
+                               v += p[k+ 2] * z[k+2];
+                               u += p[k+ 1] * z[k+1] + p[k+ 3] * z[k+3];
+                               w += p[k-15] * z[k+1] - p[k-13] * z[k+3];
+                       }
+
+                       s[          i] = (t + v) + u;
+                       s[SBLIMIT-1-i] = (t + v) - u;
+                       if (i & 1)
+                       {
+                               s[SBLIMIT/2-1-i] = (t - v) - w;
+                               s[SBLIMIT/2  +i] = (t - v) + w;
+                       }
+                       else
+                       {
+                               s[SBLIMIT/2-1-i] = (t - v) + w;
+                               s[SBLIMIT/2  +i] = (t - v) - w;
+                       }
+               }        
+       }
+
+#elif WIND_SB_CHANGE_LEVEL==3
+
+       #define imax                    (SBLIMIT/4)
+       typedef double                  filter_matrix[imax][32];
+
+       static  int                             half[2];
+       static  int                             off[2];
+       static  double                  x[2][512];
+       static  filter_matrix   m;
+
+       /*  ========================================================================================  */
+       /*      create_ana_filter                                                                     */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filterbank coefficients and rounds to the 9th decimal place
+               accuracy of the filterbank tables in the ISO document.
+               The coefficients are stored in #new_filter#
+
+
+               Originally was computed
+
+                       filter[i][j]  :=  cos (PI64 * ((2*i+1) * (16-j)))
+
+               for i = 0..SBLIMIT-1 and j = 0..63 .
+
+
+               But for j = 0..15 is 16-j = -(16-(32-j)) which implies
+
+               (1)             filter[i][j]  =  filter[i][32-j] .
+
+               (We know that filter[i][16] = cos(0) = 1 , but that is not so interesting.)
+
+               Furthermore, for j = 33..48 we get
+
+                                  filter[i][96-j]
+                               =  cos (PI64 * (2*i+1) * (j-80))
+                               =  cos (PI * (2*i+1) + PI64 * (2*i+1) * (16-j))
+               (2)             =  cos (PI * (2*i+1)) * cos (PI64 * (2*i+1) * (16-j))   // sin (PI * (2*i+1)) = 0
+                               =  -cos (PI64 * (2*i+1) * (16-j))
+                               =  -filter[i][j] ,
+
+               especially is filter[i][48] = 0 .
+
+
+               On the other hand there is
+
+                                  filter[31-i][j]
+                               =  cos (PI64 * (2*(31-i)+1) * (16-j))
+                               =  cos (PI64 * (64-(2*i+1)) * (16-j))
+                               =  cos (PI * (16-j) - PI64 * (2*i+1) * (16-j))
+                               =  cos (PI * (16-j)) * cos (PI64 * (2*i+1) * (16-j))   // sin (PI * (16-j)) = 0
+                               =  (-1)^^j * cos (PI64 * (2*i+1) * (16-j))
+                               =  (-1)^^j * filter[i][j] .
+
+
+               There is a third somewhat more complication "symmetry":
+
+                                  filter[15-i][j]
+                               =  cos (PI64 * (2*(15-i)+1) * (16-j))
+                               =  cos (PI64 * (32-(2*i+1)) * (16-j))
+                               =  cos (PI/2 * (16-j) - PI64 * (2*i+1) * (16-j))
+                               =  cos (PI/2 * (16-j)) * cos (PI64 * (2*i+1) * (16-j)) + sin (PI/2 * (16-j)) * sin (PI64 * (2*i+1) * (16-j))
+                               =  cos (PI/2 * (16-j)) * cos (PI64 * (2*i+1) * (16-j)) + sin (PI/2 * (16-j)) * cos (PI64 * (2*i+1) * (-1)^^i * (16-(j+32)))
+
+                               =  (-1)^^(  j  /2    ) * filter[i][j   ]   for even j
+                               =  (-1)^^((j+1)/2 + i) * filter[i][j+32]   for odd  j  with  j <  32
+                               =  (-1)^^((j-1)/2 + i) * filter[i][j-32]   for odd  j  with  j >= 32
+
+
+               For these reasons we create a filter of the eighth size only and set
+
+                       new_filter[i][j]  :=  filter[i][j]       for i = 0..SBLIMIT/4-1 and j =  0..16
+                       new_filter[i][j]  :=  filter[i][j+16]    for i = 0..SBLIMIT/4-1 and j = 17..31
+       */
+
+       static  void create_ana_filter (double new_filter[imax][32])
+       {
+               register int                    i, j;
+               double                                  t;
+
+               for (i = 0;  i < imax;  i++)
+               {
+                       for (j = 0;  j <= 16;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * (16-j)));
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               new_filter[i][j] = t * 1e-9;
+                       }
+                       for (j = 17;  j < 32;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * j));   /* (16-(j+16)) */
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               new_filter[i][j] = t * 1e-9;
+                       }
+               }
+       }
+
+       /*  ========================================================================================  */
+       /*      windowFilterSubband                                                                   */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filter bank coefficients
+
+               The windowed samples #z# is filtered by the digital filter matrix #m# to produce the
+               subband samples #s#. This done by first selectively picking out values from the windowed
+               samples, and then multiplying them by the filter matrix, producing 32 subband samples.
+       */
+
+       void  windowFilterSubband
+       (
+               short                                   *pBuffer,
+               int                                             ch,
+               double                                  s[SBLIMIT]
+       )
+       {
+               int                                             i, k;
+               int                                             a;
+
+               double                                  t, u, v, w;
+
+               double                                  z[32];
+               double                                  *pm;
+               double                                  *px;
+               double                                  *pEnw;
+
+
+               px = x[ch] + half[ch];
+               a = off[ch];
+
+               /* replace 32 oldest samples with 32 new samples */
+
+               for (i = 0;  i < 32;  i++)
+                       px[a + (31-i)*8] = (double) pBuffer[i]/*/SCALE*/;
+
+
+               pEnw = enwindow;
+
+               for (k = 0;  k <= 16;  k++)
+               {
+                       t =  px[(a+0) & 7] * *pEnw++;
+                       t += px[(a+1) & 7] * *pEnw++;
+                       t += px[(a+2) & 7] * *pEnw++;
+                       t += px[(a+3) & 7] * *pEnw++;
+                       t += px[(a+4) & 7] * *pEnw++;
+                       t += px[(a+5) & 7] * *pEnw++;
+                       t += px[(a+6) & 7] * *pEnw++;
+                       t += px[(a+7) & 7] * *pEnw++;
+                       z[k] = t;
+
+                       px += 8;
+               }                       
+
+               for (k = 15;  k > 0;  k--)
+               {
+                       t =  px[(a+0) & 7] * *pEnw++;
+                       t += px[(a+1) & 7] * *pEnw++;
+                       t += px[(a+2) & 7] * *pEnw++;
+                       t += px[(a+3) & 7] * *pEnw++;
+                       t += px[(a+4) & 7] * *pEnw++;
+                       t += px[(a+5) & 7] * *pEnw++;
+                       t += px[(a+6) & 7] * *pEnw++;
+                       t += px[(a+7) & 7] * *pEnw++;
+                       z[k] += t;
+
+                       px += 8;
+               }                       
+
+               half[ch] ^= 256;   /* toggling 0 or 256 */
+               if (half[ch])
+               {
+                       off[ch] = (a + 7) & 7;
+               }
+               else
+               {
+                       px = x[ch];
+                       a = (a + 1) & 7;
+               }
+
+               /* k = 0; */
+               {
+                       t =  px[(a+0) & 7] * *pEnw++;
+                       t += px[(a+1) & 7] * *pEnw++;
+                       t += px[(a+2) & 7] * *pEnw++;
+                       t += px[(a+3) & 7] * *pEnw++;
+                       t += px[(a+4) & 7] * *pEnw++;
+                       t += px[(a+5) & 7] * *pEnw++;
+                       t += px[(a+6) & 7] * *pEnw++;
+                       t += px[(a+7) & 7] * *pEnw++;
+                       z[k] += t;
+
+                       px += 8;
+               }                       
+
+               for (k = 17;  k < 32;  k++)
+               {
+                       t =  px[(a+0) & 7] * *pEnw++;
+                       t += px[(a+1) & 7] * *pEnw++;
+                       t += px[(a+2) & 7] * *pEnw++;
+                       t += px[(a+3) & 7] * *pEnw++;
+                       t += px[(a+4) & 7] * *pEnw++;
+                       t += px[(a+5) & 7] * *pEnw++;
+                       t += px[(a+6) & 7] * *pEnw++;
+                       t += px[(a+7) & 7] * *pEnw++;
+                       z[k] = t;
+
+                       px += 8;
+               }                       
+
+               /* normally y[48] was computed like the other entries, */
+               /* but this entry is not needed because m[i][48] = 0.  */
+               px += 8;      /* But we have to increase the pointers. */
+               pEnw += 8;
+
+               for (k = 31;  k > 16;  k--)
+               {
+                       t =  px[(a+0) & 7] * *pEnw++;
+                       t += px[(a+1) & 7] * *pEnw++;
+                       t += px[(a+2) & 7] * *pEnw++;
+                       t += px[(a+3) & 7] * *pEnw++;
+                       t += px[(a+4) & 7] * *pEnw++;
+                       t += px[(a+5) & 7] * *pEnw++;
+                       t += px[(a+6) & 7] * *pEnw++;
+                       t += px[(a+7) & 7] * *pEnw++;
+                       z[k] -= t;
+
+                       px += 8;
+               }                       
+
+               for (i = 0;  i < imax;  i++)
+               {
+                       pm = m[i];
+
+                       t = u = v = w = 0.0;
+                       for (k = 0;  k < 16;  k += 4)
+                       {
+                               t += pm[k   ] * z[k  ];
+                               v += pm[k+ 2] * z[k+2];
+                               u += pm[k+ 1] * z[k+1] + pm[k+ 3] * z[k+3];
+                               w -= pm[k+17] * z[k+1] - pm[k+19] * z[k+3];
+                       }
+                       for (   ;  k < 32;  k += 4)
+                       {
+                               t += pm[k   ] * z[k  ];
+                               v += pm[k+ 2] * z[k+2];
+                               u += pm[k+ 1] * z[k+1] + pm[k+ 3] * z[k+3];
+                               w += pm[k-15] * z[k+1] - pm[k-13] * z[k+3];
+                       }
+
+                       s[          i] = (t + v) + u;
+                       s[SBLIMIT-1-i] = (t + v) - u;
+                       if (i & 1)
+                       {
+                               s[SBLIMIT/2-1-i] = (t - v) - w;
+                               s[SBLIMIT/2  +i] = (t - v) + w;
+                       }
+                       else
+                       {
+                               s[SBLIMIT/2-1-i] = (t - v) + w;
+                               s[SBLIMIT/2  +i] = (t - v) - w;
+                       }
+               }        
+       }
+
+#elif WIND_SB_CHANGE_LEVEL==2
+
+       typedef double                  filter_matrix[SBLIMIT/2][32];
+
+       static  int                             off[2];
+       static  int                             half[2];
+       static  double                  x[2][512];
+       static  filter_matrix   m;
+
+       /*  ========================================================================================  */
+       /*      create_ana_filter                                                                     */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filterbank coefficients and rounds to the 9th decimal place
+               accuracy of the filterbank tables in the ISO document.
+               The coefficients are stored in #new_filter#
+
+
+               Originally was computed
+
+                       filter[i][j]  :=  cos (PI64 * ((2*i+1) * (16-j)))
+
+               for i = 0..SBLIMIT-1 and j = 0..63 .
+
+
+               But for j = 0..15 is 16-j = -(16-(32-j)) which implies
+
+               (1)             filter[i][j]  =  filter[i][32-j] .
+
+               (We know that filter[i][16] = cos(0) = 1 , but that is not so interesting.)
+
+               Furthermore, for j = 33..48 we get
+
+                                  filter[i][96-j]
+                               =  cos (PI64 * (2*i+1) * (j-80))
+                               =  cos (PI * (2*i+1) + PI64 * (2*i+1) * (16-j))
+               (2)             =  cos (PI * (2*i+1)) * cos (PI64 * (2*i+1) * (16-j))   // sin (PI * (2*i+1)) = 0
+                               =  -cos (PI64 * (2*i+1) * (16-j))
+                               =  -filter[i][j] ,
+
+               especially is filter[i][48] = 0 .
+
+
+               On the other hand there is
+
+                                  filter[31-i][j]
+                               =  cos (PI64 * (2*(31-i)+1) * (16-j))
+                               =  cos (PI64 * (64-(2*i+1)) * (16-j))
+                               =  cos (PI * (16-j) - PI64 * (2*i+1) * (16-j))
+                               =  cos (PI * (16-j)) * cos (PI64 * (2*i+1) * (16-j))   // sin (PI * (16-j)) = 0
+                               =  (-1)^^j * cos (PI64 * (2*i+1) * (16-j))
+                               =  (-1)^^j * filter[i][j] .
+
+
+               For these reasons we create a filter of the quarter size only and set
+
+                       new_filter[i][j]  :=  filter[i][j]       for i = 0..SBLIMIT/2-1 and j =  0..16
+                       new_filter[i][j]  :=  filter[i][j+16]    for i = 0..SBLIMIT/2-1 and j = 17..31
+       */
+
+       static  void create_ana_filter (double new_filter[SBLIMIT/2][32])
+       {
+               register int                    i, j;
+               double                                  t;
+
+               for (i = 0;  i < SBLIMIT/2;  i++)
+               {
+                       for (j = 0;  j < =16;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * (16-j)));
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               new_filter[i][j] = t * 1e-9;
+                       }
+                       for (j = 17;  j < 32;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * j));   /* (16-(j+16)) */
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               new_filter[i][j] = t * 1e-9;
+                       }
+               }
+       }
+
+       /*  ========================================================================================  */
+       /*      windowFilterSubband                                                                   */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filter bank coefficients
+
+               The windowed samples #z# is filtered by the digital filter matrix #m# to produce the
+               subband samples #s#. This done by first selectively picking out values from the windowed
+               samples, and then multiplying them by the filter matrix, producing 32 subband samples.
+       */
+
+       void  windowFilterSubband
+       (
+               short                                   *pBuffer,
+               int                                             ch,
+               double                                  s[SBLIMIT]
+       )
+       {
+               int                                             i, k;
+               int                                             a;
+
+               double                                  t, u;
+
+               double                                  z[32];   /* y[64]; */
+               double                                  *pm;
+               double                                  *px;
+               double                                  *pEnw;
+
+
+               px = x[ch] + half[ch];
+               a = off[ch];
+
+               /* replace 32 oldest samples with 32 new samples */
+
+               for (i = 0;  i < 32;  i++)
+                       px[a + (31-i)*8] = (double) pBuffer[i] /*/SCALE*/;
+
+
+               /*
+                       for k = 0..15 we compute
+
+                               z[k]  :=  y[k] + y[32-k]
+
+                       and we set
+
+                               z[16|  :=  y[16] .
+               */
+
+               pEnw = enwindow;
+
+               for (k = 0;  k <= 16;  k++)   /* for (j = 0;  j < =16;  j++) */
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       z[k] = t;   /* y[j] = t; */
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               for (k = 15;  k > 0;  k--)   /* for (j = 17;  j < 32;  j++) */
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       z[k] += t;   /* y[j] = t; */
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               half[ch] ^= 256;   /* toggling 0 or 256 */
+               if (half[ch])
+               {
+                       off[ch] = (a + 7) & 7;   /*offset is modulo (HAN_SIZE-1)*/
+               }
+               else
+               {
+                       px = x[ch];
+                       a = (a + 1) & 7;
+               }
+
+               /* k = 0; not needed, actual value of k is 0 */   /* j = 32; */
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       z[k] += t;   /* y[j] = t; */
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               /*
+                       for k = 17..31 we compute
+
+                               z[k]  :=  y[k+16] - y[80-k]
+               */
+
+               for (k = 17;  k < 32;  k++)   /* for (j = 33;  j < 47;  j++) */
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       z[k] = t;   /* y[j] = t; */
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               /* normally y[48] was computed like the other entries, */
+               /* but this entry is not needed because m[i][48] = 0.  */
+               px += 8;      /* But we have to increase the pointers. */
+               pEnw++;
+
+               for (k = 31;  k > 16;  k--)   /* for (j = 49;  j < 64;  j++) */
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       z[k] -= t;   /* y[j] = t; */
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               pm = m[0];                           /* pm = m[0];                      */
+               for (i = 0;  i < SBLIMIT/2;  i++)    /* for (i = 0;  i < SBLIMIT;  i++) */
+               {
+
+                       t = u = 0.0;                     /*    t = 0.0;                     */
+                       for (k = 0;  k < 32; )           /*    for (j = 0;  j < 64;  j++)   */
+                       {
+                               t += *pm++ * z[k++];         /*       t += *pm++ * y[j];        */
+                               u += *pm++ * z[k++];
+                       }
+
+                       s[          i] = t + u;          /*    s[i] = t;                    */
+                       s[SBLIMIT-1-i] = t - u;
+               }        
+       }
+
+#elif WIND_SB_CHANGE_LEVEL==1
+
+       typedef double                  filter_matrix[SBLIMIT][64];
+
+       static  int                             off[2];
+       static  int                             half[2];
+       static  double                  x[2][512];
+       static  filter_matrix   m;
+
+       /*  ========================================================================================  */
+       /*      create_ana_filter                                                                     */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filterbank coefficients and rounds to the 9th decimal place
+               accuracy of the filterbank tables in the ISO document.
+               The coefficients are stored in #filter#
+       */
+
+       static  void create_ana_filter (filter_matrix filter)
+       {
+               register int                    i, j;
+               double                                  t;
+
+               for (i = 0;  i < SBLIMIT;  i++)
+               {
+                       for (j = 0;  j < 64;  j++)
+                       {
+                               t = 1e9 * cos (PI64 * (double) ((2*i+1) * (16-j)));
+                               if (t >= 0)
+                                       modf (t + 0.5, &t);
+                               else
+                                       modf (t - 0.5, &t);
+                               filter[i][j] = t * 1e-9;
+                       }
+               }
+       }
+
+       /*  ========================================================================================  */
+       /*      windowFilterSubband                                                                   */
+       /*  ========================================================================================  */
+       /*
+               Calculates the analysis filter bank coefficients
+
+               The windowed samples #y# is filtered by the digital filter matrix #m# to produce the
+               subband samples #s#. This done by first selectively picking out values from the windowed
+               samples, and then multiplying them by the filter matrix, producing 32 subband samples.
+       */
+
+       void  windowFilterSubband
+       (
+               short                                   *pBuffer,
+               int                                             ch,
+               double                                  s[SBLIMIT]
+       )
+       {
+               int                                             i, j;
+               int                                             a;
+
+               double                                  t;
+
+               double                                  y[64];
+               double                                  *pm;
+               double                                  *px;
+               double                                  *pEnw;
+
+
+               px = x[ch] + half[ch];
+               a = off[ch];
+
+               /* replace 32 oldest samples with 32 new samples */
+
+               for (i = 0;  i < 32;  i++)
+                       px[a + (31-i)*8] = (double) pBuffer[i] /*/SCALE*/;
+
+
+               pEnw = enwindow;
+
+               for (j = 0;  j < 32;  j++)
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       y[j] = t;
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               half[ch] ^= 256;   /* toggling 0 or 256 */
+               if (half[ch])
+               {
+                       off[ch] = (a + 7) & 7;   /*offset is modulo (HAN_SIZE-1)*/
+               }
+               else
+               {
+                       px = x[ch];
+                       a = (a + 1) & 7;
+               }
+
+               for (j = 32;  j < 64;  j++)
+               {
+                       t =  px[(a+0) & 7] * pEnw[64*0];
+                       t += px[(a+1) & 7] * pEnw[64*1];
+                       t += px[(a+2) & 7] * pEnw[64*2];
+                       t += px[(a+3) & 7] * pEnw[64*3];
+                       t += px[(a+4) & 7] * pEnw[64*4];
+                       t += px[(a+5) & 7] * pEnw[64*5];
+                       t += px[(a+6) & 7] * pEnw[64*6];
+                       t += px[(a+7) & 7] * pEnw[64*7];
+                       y[j] = t;
+
+                       px += 8;
+                       pEnw++;
+               }                       
+
+               pm = m[0];
+               for (i = 0;  i < SBLIMIT;  i++)
+               {
+                       t = 0.0;
+                       for (j = 0;  j < 64;  j++)
+                               t += *pm++ * y[j];
+
+                       s[i] = t;
+               }        
+       }
+
+#endif
+
+
+
+
+
+/*  ========================================================================================  */
+/*      initWindowFilterSubband                                                               */
+/*  ========================================================================================  */
+
+void initWindowFilterSubband (void)
+{
+       static  int                             initialized = 0;
+
+       int                                             i;
+
+       if (!initialized)
+       {
+               create_ana_filter (m);
+
+               for (i = 0;  i < 512;  i++)
+                       enwindow[i] /= SCALE;
+
+               initialized = 1;
+       }
+
+#if ORG_BUFFERS
+       half[0] = half[1] = 0;
+       off[0] = off[1] = 0;
+       memset (x, 0, sizeof(x));
+#endif
+}
+
+
+
+
+
diff --git a/lib/bladeenc/encoder.h b/lib/bladeenc/encoder.h
new file mode 100644 (file)
index 0000000..bd1885c
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       reformatted, redesigned
+*/
+
+#ifndef                __ENCODER__
+#define                __ENCODER__
+
+
+
+
+
+#define                ORG_BUFFERS                             0   /* 1 = use original buffer concept */
+
+
+
+
+
+/***********************************************************************
+*
+*  Encoder Definitions
+*
+***********************************************************************/
+
+/* Psychoacoustic Model 2 Definitions */
+
+#define BLKSIZE         1024
+#define HBLKSIZE        513
+#define CBANDS          63
+
+
+
+
+
+/***********************************************************************
+*
+*  Encoder Function Prototype Declarations
+*
+***********************************************************************/
+
+
+void                                   initWindowFilterSubband (void);
+
+
+
+
+#if ORG_BUFFERS
+
+       void                                    rebuffer_audio
+       (
+               short                                   buffer[2][1152],
+               short                                   *insamp,
+               unsigned int                    samples_read,
+               int                                             stereo
+       );
+       void                                    windowFilterSubband
+       (
+               short                                   *pBuffer,
+               int                                             ch,
+               double                                  s[SBLIMIT]
+       );
+
+#else
+
+       void                                    rebuffer_audio
+       (
+               const short                             *insamp,
+               FLOAT                                   buffer[2][2048],
+               int                                             *buffer_idx,
+               unsigned int                    samples_read,
+               int                                             stereo
+       );
+       void                                    windowFilterSubband
+       (
+               const FLOAT                             *buffer,
+               int                                             buffer_idx,
+               double                                  s[SBLIMIT]
+       );
+
+#endif
+
+
+
+
+
+#endif         /* __ENCODER__ */
diff --git a/lib/bladeenc/formatbitstream2.c b/lib/bladeenc/formatbitstream2.c
new file mode 100644 (file)
index 0000000..08a839e
--- /dev/null
@@ -0,0 +1,502 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression.
+
+               This file doesn't contain any of the ISO reference code and
+               is copyright Tord Jansson (tord.jansson@swipnet.se).
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-05  Andre Piotrowski
+
+       -       reformatted, slightly optimized
+
+       2001-01-12  ap
+
+       -       use some explicit type casting to avoid compiler warnings
+*/
+
+#include       <stdio.h>
+#include       <stdlib.h>
+
+#if defined(_MSC)
+#include       <malloc.h>
+#endif
+
+#include       "common.h"
+#include       "formatbitstream2.h"
+
+
+
+
+
+extern char    *pEncodedOutput;
+extern int             outputBit;
+
+
+
+
+
+/*____ Structure Definitions _________________________________________________*/
+
+typedef                struct HeaderDef
+                       {
+                               int                                             size;
+                               int                                             frameSize;
+                               char                                    data[128];
+                               struct HeaderDef                *pNext;
+                       }                                               Header;
+
+
+
+
+
+/*____ Function Prototypes ___________________________________________________*/
+
+static int                             writeMainDataBits (BF_FrameData *psFrame, BitHolder *psBH);
+static void                    putbits (unsigned int val, int n);
+static int                             generateHeader (BF_FrameData *psFrame);
+
+
+
+
+
+/*____ Static Data ___________________________________________________________*/
+
+static int                             BitsRemaining     = 0;
+static Header                  *pHeaderChain     = NULL;
+static Header                  *pFreeHeaderChain = NULL;
+
+
+
+
+
+/*____ initFormatBitstream() ________________________________________________*/
+
+void                                   initFormatBitstream (void)
+{
+       BitsRemaining    = 0;
+       pHeaderChain     = NULL;
+       pFreeHeaderChain = NULL;
+}
+
+
+
+
+
+/*____ exitFormatBitstream() _________________________________________________*/
+
+void                                   exitFormatBitstream (void)
+{
+       Header                                  *psFree;
+
+       /* Fill out the last frame with 0xFF */
+
+       flushFrame ();
+
+       /* Dealocate Headers */
+
+       while (pHeaderChain != NULL)
+       {
+               psFree = pHeaderChain;
+               pHeaderChain = psFree->pNext;
+               free (psFree);
+       }
+
+       while (pFreeHeaderChain != NULL)
+       {
+               psFree = pFreeHeaderChain;
+               pFreeHeaderChain = psFree->pNext;
+               free (psFree);
+       }
+}
+
+
+
+
+
+/*____ flushFrame() _________________________________________________________*/
+
+void                                   flushFrame (void)
+{
+       /* Fill out the last frame with 0xFF */
+
+       while (BitsRemaining > 32)
+       {
+               putbits (0xFFFFFFFF, 32);
+                       BitsRemaining -= 32;
+       }
+
+       if (BitsRemaining > 0)
+               putbits (0xFFFFFFFF, BitsRemaining);
+
+       BitsRemaining = 0;
+}
+
+
+
+
+
+/*____ initBitHolder() ______________________________________________________*/
+
+BitHolder                              *initBitHolder
+(
+       BitHolder                               *wp,
+       int                                             elements
+)
+{
+       wp->element      = (BitHolderElement *) malloc (sizeof(BitHolderElement) * elements);
+       wp->max_elements = elements;
+       wp->nrEntries    = 0;
+
+       return wp;
+}
+
+
+
+
+
+/*____ exitBitHolder() ______________________________________________________*/
+
+void                                   exitBitHolder (BitHolder * wp)
+{
+       mem_free ((void **)&wp->element);
+/*
+       if (wp->element != NULL)
+       {
+               free (wp->element);
+               wp->element = NULL;
+       }
+*/
+}
+
+
+
+
+
+/*____ addBits() ____________________________________________________________*/
+
+void                                   addBits
+(
+       BitHolder                               *wp,
+       unsigned int                    value,
+       int                                             length
+)
+{
+       if (length != 0)
+       {
+               if (wp->nrEntries == wp->max_elements)
+               {
+                       printf ("ERROR: BitHolder overflow!\n");
+                       exit (-1);
+               }
+
+               value &= 0xFFFFFFFF >> (32-length);
+
+               wp->element[wp->nrEntries].value  = value;
+               wp->element[wp->nrEntries].length = length;
+               wp->nrEntries++;
+       }
+}
+
+
+
+
+
+/*____ writeFrame() _________________________________________________________*/
+
+void                                   writeFrame
+(
+       BF_FrameData                    *psFrame,
+       BF_FrameResults                 *results
+)
+{
+       int                                             bits;
+       int                                             gr, ch;
+
+       int                                             sizeRemainHeaders, sizeRemainFrames;
+       Header                                  *psHeader;
+
+
+       /* Generate and save header, return size of SideInfo.*/
+
+       results->SILength = generateHeader (psFrame);
+
+
+       /* Put the bits and compute size of mainData */
+
+       bits = 0;
+       for (gr = 0;  gr < psFrame->nGranules;  gr++)
+       {
+               for (ch = 0;  ch < psFrame->nChannels;  ch++)
+               {
+                       bits += writeMainDataBits (psFrame, &psFrame->scaleFactors[gr][ch]);
+                       bits += writeMainDataBits (psFrame, &psFrame->   codedData[gr][ch]);
+                       bits += writeMainDataBits (psFrame, &psFrame->userSpectrum[gr][ch]);
+               }
+       }
+       bits += writeMainDataBits (psFrame, &psFrame->userFrameData);
+
+       results->mainDataLength = bits;
+
+
+       /* calculate nextBackPointer */
+
+       sizeRemainHeaders = 0;
+       sizeRemainFrames  = 0;
+
+       for (psHeader = pHeaderChain;  psHeader != NULL;  psHeader = psHeader->pNext)
+       {
+               sizeRemainHeaders += psHeader->size;
+               sizeRemainFrames  += psHeader->frameSize;
+       }
+
+
+       results->nextBackPtr = (BitsRemaining / 8) + sizeRemainFrames - sizeRemainHeaders;      /* BitsRemaining must be dividable by 8 */
+}
+
+
+
+
+
+/*=============================================================================
+                                                                       >>> Static Functions <<<
+=============================================================================*/
+
+
+
+
+
+/*____ writeBitHolder() _____________________________________________________*/
+
+static void                    writeBitHolder (BitHolder *part)
+{
+       BitHolderElement                *ep;
+       int                                             i;
+
+       ep = part->element;
+       for (i = 0;  i < part->nrEntries;  i++, ep++)
+               putbits (ep->value, ep->length);
+}
+
+
+
+
+
+/*____ calcCRC() ____________________________________________________________*/
+
+static int                             calcCRC
+(
+       char                                    *pData,
+       int                                             size
+)
+{
+       int                                             i;
+       int                                             crc = 0xffff;
+       int                                             masking;
+       int                                             carry;
+       int                                             data;
+
+       for (i = 2;  i < size;  i++)
+       {
+               if (i != 4  &&  i != 5)
+               {
+                       masking = 1 << 8;
+                       data    = pData[i];
+                       while (masking >>= 1)
+                       {
+                               carry = crc & 0x8000;
+                               crc <<= 1;
+                               /*
+                                       if ( (carry == 0) != ((data & masking) == 0) )
+                               */
+                               if (!carry ^ !(data & masking))
+                                       crc ^= 0x8005;
+                       }
+               }
+       }
+
+       crc &= 0xffff;
+       return crc;
+}
+
+
+
+
+
+/*____ generateHeader() ____________________________________________________*/
+
+static int                             generateHeader (BF_FrameData *psFrame)
+{
+       int                                             gr, ch;
+       int                                             crc;
+       Header                                  *psHeader;
+       Header                                  **wpLink;
+       char                                    *pOldEncodedOutput;
+
+
+       /* Get a Free Header structure */
+
+       if( pFreeHeaderChain == NULL )
+       psHeader = (Header *) malloc( sizeof( Header ) );
+       else
+       {
+       psHeader = pFreeHeaderChain;
+       pFreeHeaderChain = psHeader->pNext;
+       }
+       psHeader->pNext = NULL;
+       for( wpLink = &pHeaderChain ; * wpLink != NULL ; wpLink = &((*wpLink)->pNext) )
+       {}   /* avoid compiler warning */
+       *wpLink = psHeader;
+
+
+       /* Generate the Header */
+
+       pOldEncodedOutput = pEncodedOutput;
+       pEncodedOutput = psHeader->data;
+       pEncodedOutput[0] = 0;                                                                          /* Need to be cleared since we OR in data... */
+
+
+       writeBitHolder (&psFrame->header);
+       writeBitHolder (&psFrame->frameSI);
+
+       for (ch = 0;  ch < psFrame->nChannels;  ch++)
+               writeBitHolder (&psFrame->channelSI[ch]);
+
+       for (gr = 0;  gr < psFrame->nGranules;  gr++)
+               for (ch = 0;  ch < psFrame->nChannels;  ch++)
+                       writeBitHolder (&psFrame->spectrumSI[gr][ch]);
+
+
+       /* Checksum generation (if CRC enabled).*/
+
+       if (!(psHeader->data[1] & 0x1))
+       {
+               crc = calcCRC (psHeader->data, pEncodedOutput - psHeader->data);
+               psHeader->data[4] = (char) (crc >> 8);
+               psHeader->data[5] = (char) crc;
+       }
+
+
+       psHeader->size      = pEncodedOutput - psHeader->data;
+       psHeader->frameSize = psFrame->frameLength / 8;
+
+       pEncodedOutput = pOldEncodedOutput;
+
+
+       return  psHeader->size * 8;
+}
+
+
+
+
+
+/*____ writeHeader() _______________________________________________________*/
+
+static int                             writeHeader (void)
+{
+       Header                                  *psHeader;
+
+       psHeader = pHeaderChain;
+       memcpy (pEncodedOutput, psHeader->data, psHeader->size);
+       pEncodedOutput += psHeader->size;
+       *pEncodedOutput = 0;
+
+       pHeaderChain = psHeader->pNext;
+       psHeader->pNext = pFreeHeaderChain;
+       pFreeHeaderChain = psHeader;
+
+       return  (psHeader->frameSize - psHeader->size) * 8;
+}
+
+
+
+
+
+/*____ writeMainDataBits() __________________________________________________*/
+
+static int                             writeMainDataBits
+(
+       BF_FrameData                    *psFrame,   /* avoid compiler warning */
+       BitHolder                               *psBH
+)
+{
+       BitHolderElement                *psElem = psBH->element;
+       int                                             i, bits = 0;
+       unsigned int                    val;
+       int                                             nBits;
+
+       for (i = 0;  i < psBH->nrEntries;  i++, psElem++)
+       {
+               val   = psElem->value;
+               nBits = psElem->length;
+
+               if (BitsRemaining == 0)
+                       BitsRemaining = writeHeader ();
+
+               if (nBits > BitsRemaining)
+               {
+                       nBits -= BitsRemaining;
+                       putbits (val >> nBits, BitsRemaining);
+                       BitsRemaining = writeHeader ();
+               }
+
+               putbits (val, nBits);
+               BitsRemaining -= nBits;
+
+               bits += psElem->length;
+       }
+
+       return  bits;
+}
+
+
+
+
+
+/*____ putbits() _____________________________________________________________*/
+
+
+/*write n bits into the bit stream */
+
+static void                    putbits
+(
+       unsigned int                    val,
+       int                                             n
+)
+{
+
+       if (n == 0)
+               return;
+
+       while (n >= outputBit)
+       {
+               n -= outputBit;
+
+               *pEncodedOutput |= val >> n;
+
+               outputBit = 8;
+               pEncodedOutput++;
+               *pEncodedOutput = 0;
+       }
+
+       if (n > 0)   /* n < outputBit */
+       {
+               outputBit -= n;
+
+               *pEncodedOutput |= val << outputBit;
+       }
+}
+
+
+
diff --git a/lib/bladeenc/formatbitstream2.h b/lib/bladeenc/formatbitstream2.h
new file mode 100644 (file)
index 0000000..68f133d
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-15  Andre Piotrowski
+
+       -       reformatted
+*/
+
+#ifndef                __FORMATBITSTREAM2__
+#define                __FORMATBITSTREAM2__
+
+
+
+
+
+#define                MAX_CHANNELS                    2
+#define                MAX_GRANULES                    2
+
+
+
+
+
+
+/*____ Structure Definitions ________________________________________________*/
+
+typedef                struct
+                       {
+                               unsigned int                    value;
+                               int                                             length;
+                       }                                       BitHolderElement;
+
+
+typedef                struct
+                       {
+                               int                                             max_elements;
+                               int                                             nrEntries;
+                               BitHolderElement                *element;
+                       }                                       BitHolder;
+
+
+typedef                struct BF_FrameData
+                       {
+                               int                                             frameLength;
+                               int                                             nGranules;
+                               int                                             nChannels;
+                               BitHolder                               header;
+                               BitHolder                               frameSI;
+                               BitHolder                                  channelSI[MAX_CHANNELS];
+                               BitHolder                                 spectrumSI[MAX_GRANULES][MAX_CHANNELS];
+                               BitHolder                               scaleFactors[MAX_GRANULES][MAX_CHANNELS];
+                               BitHolder                                  codedData[MAX_GRANULES][MAX_CHANNELS];
+                               BitHolder                               userSpectrum[MAX_GRANULES][MAX_CHANNELS];
+                               BitHolder                               userFrameData;
+                       }                                       BF_FrameData;
+
+
+typedef                struct BF_FrameResults
+                       {
+                               int                                             SILength;
+                               int                                             mainDataLength;
+                               int                                             nextBackPtr;
+                       }                                       BF_FrameResults;
+
+
+
+
+
+/*____ Function Prototypes __________________________________________________*/
+
+BitHolder                              *initBitHolder (BitHolder *wp, int elements);
+void                                   exitBitHolder (BitHolder *wp);
+void                                   addBits (BitHolder *wp, unsigned int value, int length);
+void                                   writeFrame (BF_FrameData *psFrame, BF_FrameResults *results);
+void                                   initFormatBitstream (void);
+void                                   exitFormatBitstream (void);
+void                                   flushFrame (void);
+
+
+
+
+
+#endif   /* __FORMATBITSTREAM2__ */
diff --git a/lib/bladeenc/huffman.h b/lib/bladeenc/huffman.h
new file mode 100644 (file)
index 0000000..172bc08
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-04  Andre Piotrowski
+
+       -       reformatted, slimmed
+*/
+
+#ifndef                __HUFFMAN__
+#define                __HUFFMAN__
+
+
+struct huffcodetab {
+       unsigned int                    xlen;           /* max. x-index                           */
+       unsigned int                    ylen;           /* max. y-index                           */
+       unsigned int                    linbits;        /* number of linbits                      */
+       unsigned int                    linmax;         /* max number to be stored in linbits     */
+       int                                             ref;            /* a positive value indicates a reference */
+       unsigned int                    *table;         /* pointer to array[xlen][ylen]           */
+       unsigned char                   *hlen;          /* pointer to array[xlen][ylen]           */
+};
+
+
+extern struct huffcodetab      ht[34];
+/* global memory block               */
+/* array of all huffcodtable headers */
+/* 0..31 Huffman code table 0..31    */
+/* 32,33 count1-tables               */
+
+
+#endif         /* __HUFFMAN__ */
diff --git a/lib/bladeenc/i.c b/lib/bladeenc/i.c
new file mode 100644 (file)
index 0000000..86df3de
--- /dev/null
@@ -0,0 +1,24 @@
+#include "codec.h"
+
+int main()
+{
+    CodecInitOut * init;
+    CodecInitIn params;
+    char buf[1024];
+    short data[1024];
+    int len = 0;
+
+    memset(&params, 0, sizeof(params));
+    params.frequency = 32000;  //48000, 44100 or 32000
+    params.mode = 0;     //0 = Stereo, 2 = Dual Channel, 3 = Mono
+    params.emphasis = 0;  //0 = None, 1 = 50/15 microsec, 3 = CCITT J.17
+    params.bitrate = 128;        //default is 128 (64 for mono)
+
+    init = codecInit(&params);
+
+    len = codecEncodeChunk(20, data, buf);
+    len += codecFlush (&buf[len]);
+    len += codecExit(&buf[len]);
+
+    printf("data is %d bytes\n", len);
+}
diff --git a/lib/bladeenc/l3bitstream-pvt.h b/lib/bladeenc/l3bitstream-pvt.h
new file mode 100644 (file)
index 0000000..51b0148
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-04  Andre Piotrowski
+
+       -       redesigned, reformatted
+*/
+
+#ifndef                __L3BITSTREAM_PVT__
+#define                __L3BITSTREAM_PVT__
+
+
+
+
+
+static void                    encodeMainData
+(
+       int                                             l3_enc[2][2][576],
+       III_side_info_t                 *side_info,
+       III_scalefac_t                  *scalefac
+);
+
+static int                             encodeSideInfo (III_side_info_t *side_info);
+
+static void                    write_ancillary_data
+(
+       char                                    *theData,
+       int                                             lengthInBits
+);
+
+static void                    drain_into_ancillary_data (int lengthInBits);
+
+static void                    Huffmancodebits
+(
+       BitHolder                               *ph,
+       int                                             *ix,
+       gr_info                                 *cod_info
+);
+
+static int                             writeHuffmanCode
+(
+       BitHolder                               *ph,
+       int                                             table,
+       int                                             x,
+       int                                             y
+);
+
+
+
+
+
+#endif         /* __L3BITSTREAM_PVT__ */
diff --git a/lib/bladeenc/l3bitstream.c b/lib/bladeenc/l3bitstream.c
new file mode 100644 (file)
index 0000000..a2caf88
--- /dev/null
@@ -0,0 +1,665 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-04  Andre Piotrowski
+
+       -       redesigned, reformatted, slightly optimized
+*/
+
+#include       <stdlib.h>
+#include       <assert.h>
+
+#include       "system.h"
+#include       "common.h"
+
+#include       "l3psy.h"
+#include       "loop.h"
+#include       "formatbitstream2.h"
+#include       "huffman.h"
+
+#include       "l3bitstream.h" /* the public interface */
+#include       "l3bitstream-pvt.h"
+
+
+
+
+
+static int                             stereo = 1;
+static frame_params    *fr_ps  = NULL;
+
+static int                             PartHoldersInitialized = 0;
+
+
+
+static BitHolder                      *headerPH;
+static BitHolder                     *frameSIPH;
+static BitHolder                   *channelSIPH[MAX_CHANNELS];
+static BitHolder                  *spectrumSIPH[MAX_GRANULES][MAX_CHANNELS];
+static BitHolder                *scaleFactorsPH[MAX_GRANULES][MAX_CHANNELS];
+static BitHolder                   *codedDataPH[MAX_GRANULES][MAX_CHANNELS];
+static BitHolder                *userSpectrumPH[MAX_GRANULES][MAX_CHANNELS];
+static BitHolder               *userFrameDataPH;
+
+
+
+static BF_FrameData    sFrameData;
+static BF_FrameResults sFrameResults;
+
+
+
+
+
+/*
+       III_format_bitstream()
+
+       This is called after a frame of audio has been quantized and coded.
+       It will write the encoded audio to the bitstream. Note that
+       from a layer3 encoder's perspective the bit stream is primarily
+       a series of main_data() blocks, with header and side information
+       inserted at the proper locations to maintain framing. (See Figure A.7
+       in the IS).
+*/
+
+void                                   III_format_bitstream
+(
+       int                                             bitsPerFrame,
+       frame_params                    *in_fr_ps,
+       int                                             l3_enc[2][2][576],
+       III_side_info_t                 *l3_side,
+       III_scalefac_t                  *scalefac,
+       double                                  (*xr)[2][576],
+       char                                    *ancillary,
+       int                                             ancillary_bits
+)
+{
+       int                                             gr, ch, i, mode_gr;
+
+       fr_ps = in_fr_ps;
+       stereo = fr_ps->stereo;
+       mode_gr = 2;
+
+       if (!PartHoldersInitialized)
+       {
+                headerPH = initBitHolder (&sFrameData.header, 16*2);
+               frameSIPH = initBitHolder (&sFrameData.frameSI, 4*2);
+
+               for (ch = 0;  ch < MAX_CHANNELS;  ch++)
+                       channelSIPH[ch] = initBitHolder (&sFrameData.channelSI[ch], 8*2);
+
+               for (gr = 0;  gr < MAX_GRANULES;  gr++)
+               {
+                       for (ch = 0;  ch < MAX_CHANNELS;  ch++)
+                       {
+                                 spectrumSIPH[gr][ch] = initBitHolder (&sFrameData.  spectrumSI[gr][ch],  32*2);
+                               scaleFactorsPH[gr][ch] = initBitHolder (&sFrameData.scaleFactors[gr][ch],  64*2);
+                                  codedDataPH[gr][ch] = initBitHolder (&sFrameData.   codedData[gr][ch], 576*2);
+                               userSpectrumPH[gr][ch] = initBitHolder (&sFrameData.userSpectrum[gr][ch],   4*2);
+                       }
+               }
+               userFrameDataPH = initBitHolder (&sFrameData.userFrameData, 8*2);
+
+               PartHoldersInitialized = 1;
+       }
+
+#if 1
+       for (gr = 0;  gr < mode_gr;  gr++)
+       {
+               for (ch = 0;  ch < stereo;  ch++)
+               {
+                       int                                             *pi = &l3_enc[gr][ch][0];
+                       double                                  *pr =     &xr[gr][ch][0];
+
+                       for (i = 0;  i < 576;  i++, pr++, pi++)
+                               if (*pr < 0  &&  *pi > 0)
+                                       *pi *= -1;
+               }
+       }
+#endif
+
+       encodeSideInfo (l3_side);
+       encodeMainData (l3_enc, l3_side, scalefac);
+       write_ancillary_data (ancillary, ancillary_bits);
+
+       if (l3_side->resvDrain)
+               drain_into_ancillary_data (l3_side->resvDrain);
+
+       sFrameData.frameLength = bitsPerFrame;
+       sFrameData.nGranules   = mode_gr;
+       sFrameData.nChannels   = stereo;
+
+       writeFrame (&sFrameData, &sFrameResults);
+
+       /* we set this here -- it will be tested in the next loops iteration */
+       l3_side->main_data_begin = sFrameResults.nextBackPtr;
+}
+
+
+
+
+
+void                                   III_FlushBitstream (void)
+{
+       int                                             gr, ch;
+
+       if (PartHoldersInitialized)
+       {
+               exitBitHolder (&sFrameData.header);
+               exitBitHolder (&sFrameData.frameSI);
+
+               for (ch = 0;  ch < MAX_CHANNELS;  ch++)
+                       exitBitHolder (&sFrameData.channelSI[ch]);
+
+
+               for (gr = 0;  gr < MAX_GRANULES;  gr++)
+               {
+                       for (ch = 0;  ch < MAX_CHANNELS;  ch++)
+                       {
+                               exitBitHolder (&sFrameData.  spectrumSI[gr][ch]);
+                               exitBitHolder (&sFrameData.scaleFactors[gr][ch]);
+                               exitBitHolder (&sFrameData.   codedData[gr][ch]);
+                               exitBitHolder (&sFrameData.userSpectrum[gr][ch]);
+                       }
+               }
+               exitBitHolder (&sFrameData.userFrameData);
+
+               PartHoldersInitialized = 0;
+       }
+
+       /* BF_FlushBitstream (frameData, frameResults); */
+}
+
+
+
+
+
+static unsigned                slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
+static unsigned                slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
+
+
+
+
+
+static void                    encodeMainData
+(
+       int                                             l3_enc[2][2][576],
+       III_side_info_t                 *side_info,
+       III_scalefac_t                  *scalefac
+)
+{
+       int                                             gr, ch, sfb, b, mode_gr;
+
+       mode_gr = 2;
+
+
+       for (gr = 0;  gr < mode_gr;  gr++)
+               for (ch = 0;  ch < stereo;  ch++)
+                       scaleFactorsPH[gr][ch]->nrEntries = 0;
+
+
+       for (gr = 0;  gr < mode_gr;  gr++)
+               for (ch = 0;  ch < stereo;  ch++)
+                       codedDataPH[gr][ch]->nrEntries = 0;
+
+
+       for (gr = 0;  gr < 2;  gr++)
+       {
+               for (ch = 0;  ch < stereo;  ch++)
+               {
+                       BitHolder                               *ph = scaleFactorsPH[gr][ch];           
+                       gr_info                                 *cod_info = &side_info->gr[gr].ch[ch].tt;
+                       unsigned                                slen1 = slen1_tab[cod_info->scalefac_compress];
+                       unsigned                                slen2 = slen2_tab[cod_info->scalefac_compress];
+                       int                                             *ix = &l3_enc[gr][ch][0];
+
+                       if (cod_info->window_switching_flag  &&  cod_info->block_type == SHORT_TYPE)
+                       {
+                               if (cod_info->mixed_block_flag)
+                               {
+                                       for (sfb = 0;  sfb < 8;  sfb++)
+                                               addBits (ph, scalefac->l[gr][ch][sfb], slen1);
+
+                                       for (sfb = 3;  sfb < 6;  sfb++)
+                                               for (b = 0;  b < 3;  b++)
+                                                       addBits (ph, scalefac->s[gr][ch][sfb][b], slen1);
+                               }
+                               else
+                               {
+                                       for (sfb = 0;  sfb < 6;  sfb++)
+                                               for (b = 0;  b < 3;  b++)
+                                                       addBits (ph, scalefac->s[gr][ch][sfb][b], slen1);
+                               }
+
+                               for (sfb = 6;  sfb < 12;  sfb++)
+                                       for (b = 0;  b < 3;  b++)
+                                               addBits (ph, scalefac->s[gr][ch][sfb][b], slen2);
+                       }
+                       else if (gr == 0)
+                       {
+                               for (sfb = 0;  sfb < 11;  sfb++)
+                                       addBits (ph, scalefac->l[gr][ch][sfb], slen1);
+
+                               for (sfb = 11;  sfb < 21;  sfb++)
+                                       addBits (ph, scalefac->l[gr][ch][sfb], slen2);
+                       }
+                       else
+                       {
+                               if (!side_info->scfsi[ch][0])
+                                       for (sfb = 0;  sfb < 6;  sfb++)
+                                               addBits (ph, scalefac->l[gr][ch][sfb], slen1);
+
+                               if (!side_info->scfsi[ch][1])
+                                       for (sfb = 6;  sfb < 11;  sfb++)
+                                               addBits (ph, scalefac->l[gr][ch][sfb], slen1);
+
+                               if (!side_info->scfsi[ch][2])
+                                       for (sfb = 11;  sfb < 16;  sfb++)
+                                               addBits (ph, scalefac->l[gr][ch][sfb], slen2);
+
+                               if (!side_info->scfsi[ch][3])
+                                       for (sfb = 16;  sfb < 21;  sfb++)
+                                               addBits (ph, scalefac->l[gr][ch][sfb], slen2);
+                       }
+
+                       Huffmancodebits (codedDataPH[gr][ch], ix, cod_info);
+               } /* for ch */
+       } /* for gr */
+} /* main_data */
+
+
+
+
+
+/*____ encodeSideInfo() _____________________________________________________*/
+
+static int                             encodeSideInfo (III_side_info_t *side_info)
+{
+       int                                             gr, ch, scfsi_band, region, b, bits_sent, mode_gr;
+       layer                                   *info = fr_ps->header;
+
+       mode_gr =  2;
+
+
+       headerPH->nrEntries = 0;
+
+       addBits (headerPH, 0xfff                   , 12);
+       addBits (headerPH, 1                       ,  1);
+       addBits (headerPH, 4 - 3                   ,  2);   /* 4 - Layer */
+       addBits (headerPH, !info->error_protection ,  1);
+       addBits (headerPH, info->bitrate_index     ,  4);
+       addBits (headerPH, info->sampling_frequency,  2);
+       addBits (headerPH, info->padding           ,  1);
+       addBits (headerPH, info->extension         ,  1);
+       addBits (headerPH, info->mode              ,  2);
+       addBits (headerPH, info->mode_ext          ,  2);
+       addBits (headerPH, info->copyright         ,  1);
+       addBits (headerPH, info->original          ,  1);
+       addBits (headerPH, info->emphasis          ,  2);
+
+       bits_sent = 32;
+
+       if (info->error_protection)
+       {
+               addBits (headerPH, 0, 16);   /* Just a dummy add. Real CRC calculated & inserted in writeSideInfo() */
+               bits_sent += 16;
+       }
+
+
+       frameSIPH->nrEntries = 0;
+
+       addBits (frameSIPH, side_info->main_data_begin, 9);
+
+       if (stereo == 2)
+               addBits (frameSIPH, side_info->private_bits, 3);
+       else
+               addBits (frameSIPH, side_info->private_bits, 5);
+
+
+       for (ch = 0;  ch < stereo;  ch++)
+       {
+               BitHolder                               *ph = channelSIPH[ch];
+
+               ph->nrEntries = 0;
+
+               for (scfsi_band = 0;  scfsi_band < 4;  scfsi_band++)
+                       addBits (ph, side_info->scfsi[ch][scfsi_band], 1);
+       }
+
+
+       for (gr = 0;  gr < 2;  gr++)
+       {
+               for (ch = 0;  ch < stereo;  ch++)
+               {
+                       BitHolder                               *ph = spectrumSIPH[gr][ch];
+                       gr_info                                 *cod_info = &side_info->gr[gr].ch[ch].tt;
+
+                       ph->nrEntries = 0;
+
+                       addBits (ph, cod_info->part2_3_length       , 12);
+                       addBits (ph, cod_info->big_values           ,  9);
+                       addBits (ph, cod_info->global_gain          ,  8);
+                       addBits (ph, cod_info->scalefac_compress    ,  4);
+                       addBits (ph, cod_info->window_switching_flag,  1);
+
+                       if (cod_info->window_switching_flag)
+                       {   
+                               addBits (ph, cod_info->block_type      , 2);
+                               addBits (ph, cod_info->mixed_block_flag, 1);
+
+                               for (region = 0; region < 2; region++)
+                                       addBits (ph, cod_info->table_select[region], 5);
+
+                               for (b = 0;  b < 3;  b++)
+                                       addBits (ph, cod_info->subblock_gain[b], 3);
+                       }
+                       else
+                       {
+                               for (region = 0; region < 3; region++)
+                                       addBits (ph, cod_info->table_select[region], 5);
+
+                               addBits (ph, cod_info->region0_count, 4);
+                               addBits (ph, cod_info->region1_count, 3);
+                       }
+
+                       addBits (ph, cod_info->preflag           , 1);
+                       addBits (ph, cod_info->scalefac_scale    , 1);
+                       addBits (ph, cod_info->count1table_select, 1);
+               }
+       }
+
+
+       if (stereo == 2)
+               bits_sent += 256;
+       else
+               bits_sent += 136;
+
+
+       return bits_sent;
+}
+
+
+
+
+
+/*____ write_ancillary_data() _______________________________________________*/
+
+static void                    write_ancillary_data
+(
+       char                                    *theData,
+       int                                             lengthInBits
+)
+{
+       int                                             bytesToSend   = lengthInBits / 8;
+       int                                             remainingBits = lengthInBits % 8;
+       unsigned                                wrd;
+
+       userFrameDataPH->nrEntries = 0;
+
+       while (bytesToSend--)
+       {
+               wrd = *theData++;
+               addBits (userFrameDataPH, wrd, 8);
+       }
+       if (remainingBits)
+       {
+               /* right-justify remaining bits */
+               wrd = *theData >> (8 - remainingBits);
+               addBits (userFrameDataPH, wrd, remainingBits);
+       }
+}
+
+
+
+
+
+/*
+       Some combinations of bitrate, Fs, and stereo make it impossible to stuff
+       out a frame using just main_data, due to the limited number of bits to
+       indicate main_data_length. In these situations, we put stuffing bits into
+       the ancillary data...
+*/
+static void                    drain_into_ancillary_data (int lengthInBits)
+{
+       int                                             wordsToSend   = lengthInBits / 32;
+       int                                             remainingBits = lengthInBits % 32;
+
+       /*
+               userFrameDataPH->part->nrEntries set by call to write_ancillary_data()
+       */
+
+       while (wordsToSend--)
+               addBits (userFrameDataPH, 0, 32);
+       if (remainingBits)
+               addBits (userFrameDataPH, 0, remainingBits); 
+}
+
+
+
+
+
+/*
+       Note the discussion of huffmancodebits() on pages 28
+       and 29 of the IS, as well as the definitions of the side
+       information on pages 26 and 27.
+*/
+static void                    Huffmancodebits
+(
+       BitHolder                               *ph,
+       int                                             *ix,
+       gr_info                                 *cod_info
+)
+{
+       int                                             sfb, window, line, start, end;
+       int                                             stuffingBits, table;
+
+       int                                             bitsWritten = 0;
+       int                                             bigvalues   = cod_info->big_values * 2;
+
+
+       /*
+               Within each scalefactor band, data is given for successive time windows,
+               beginning with window 0 and ending with window 2 (in case of short blocks!
+               --- there is only one long block window). Within each window, the quantized
+               values are then arranged in order of increasing frequency...
+       */
+
+       /* 1: Write the bigvalues */
+       if (bigvalues)
+       {
+               if (cod_info->window_switching_flag  &&  (cod_info->block_type == SHORT_TYPE))
+               {
+                       int                                                     (*ix_s)[3] = (int (*)[3]) ix;
+                       int                                                     *scalefac = &sfBandIndex[fr_ps->header->sampling_frequency].s[0];
+
+                       table = cod_info->table_select[0];
+                       if (table)
+                       {
+                               if (cod_info->mixed_block_flag)  /* Mixed blocks long, short */
+                               {
+                                       for (line=0; line<36; line+=2)   /* cod_info->address1 = 36 */
+                                               bitsWritten += writeHuffmanCode (ph, table, ix[line], ix[line+1]);
+                               }
+                               else
+                               {
+                                       for (sfb=0; sfb<3; sfb++)   /* (cod_info->region0_count + 1) / 3 = 3 */
+                                       {
+                                               start = scalefac[sfb];
+                                               end   = scalefac[sfb+1];
+
+                                               for (window=0; window<3; window++)
+                                                       for (line=start; line<end; line+=2)
+                                                               bitsWritten += writeHuffmanCode (ph, table, ix_s[line][window], ix_s[line+1][window]);
+                                       }
+                               }
+                       }
+
+                       table = cod_info->table_select[1];
+                       if (table)
+                       {
+                               for (sfb=3; sfb<SFB_SMAX; sfb++)   /* scalefac[3] = 12 --- 12*3 = 36 */
+                               {
+                                       start = scalefac[sfb];
+                                       end   = scalefac[sfb+1]; 
+
+                                       for (window=0; window<3; window++)
+                                               for (line=start; line<end; line+=2)
+                                                       bitsWritten += writeHuffmanCode (ph, table, ix_s[line][window], ix_s[line+1][window]);
+                               }
+                       }
+               }
+               else   /* Long blocks */
+               {
+                       int                                                     region1Start = (cod_info->address1 > bigvalues) ? bigvalues : cod_info->address1;
+                       int                                                     region2Start = (cod_info->address2 > bigvalues) ? bigvalues : cod_info->address2;
+
+                       table = cod_info->table_select[0];
+                       if (table)
+                               for (line=0; line<region1Start; line+=2)
+                                       bitsWritten += writeHuffmanCode (ph, table, ix[line], ix[line+1]);
+
+                       table = cod_info->table_select[1];
+                       if (table)
+                               for (line=region1Start; line<region2Start; line+=2)
+                                       bitsWritten += writeHuffmanCode (ph, table, ix[line], ix[line+1]);
+
+                       table = cod_info->table_select[2];
+                       if (table)
+                               for (line=region2Start; line<bigvalues; line+=2)
+                                       bitsWritten += writeHuffmanCode (ph, table, ix[line], ix[line+1]);
+               }
+       }
+
+       /* 2: Write count1 area */
+       if (cod_info->count1)
+       {
+               struct huffcodetab              *h = ht + (cod_info->count1table_select + 32);
+
+               int                                             *pos = ix + bigvalues;
+               int                                             *end = ix + bigvalues + (cod_info->count1 * 4);
+
+               while (pos < end)
+               {
+                       int                                             len, v, w, x, y;
+                       int                                             bits = 0;
+                       int                                             p    = 0;
+
+                       v = *pos++;  if (v)  bits++, p |= 1;
+                       w = *pos++;  if (w)  bits++, p |= 2;
+                       x = *pos++;  if (x)  bits++, p |= 4;
+                       y = *pos++;  if (y)  bits++, p |= 8;
+
+                       addBits (ph, h->table[p], len = h->hlen[p]);
+
+                       if (v)  addBits (ph, v<0, 1);
+                       if (w)  addBits (ph, w<0, 1);
+                       if (x)  addBits (ph, x<0, 1);
+                       if (y)  addBits (ph, y<0, 1);
+
+                       bitsWritten += bits+len;
+               }
+       }
+
+
+       stuffingBits = cod_info->part2_3_length - cod_info->part2_length - bitsWritten;
+       if (stuffingBits)
+       {
+               int                                             stuffingWords = stuffingBits / 32;
+               int                                             remainingBits = stuffingBits % 32;
+
+               /*
+                       Due to the nature of the Huffman code
+                       tables, we will pad with ones
+               */
+               while (stuffingWords--)
+                       addBits (ph, ~0, 32);
+               if (remainingBits)
+                       addBits (ph, ~0, remainingBits);
+
+               bitsWritten += stuffingBits;
+       }
+}
+
+
+
+
+
+/*
+       Implements the pseudocode of page 98 of the IS
+
+       aaaaaaaaaaaaaaargh --- why don«t write the code immediately?
+*/
+
+static int                             writeHuffmanCode
+(
+       BitHolder                               *ph,
+       int                                             table,
+       int                                             x,
+       int                                             y
+)
+{
+       struct huffcodetab              *h = ht + table;
+
+       unsigned                                signx = (x <= 0) ? (x = -x, 1) : 0;
+       unsigned                                signy = (y <= 0) ? (y = -y, 1) : 0;
+
+       unsigned                                code, cbits, idx;
+
+assert (table);
+/*
+       if (table == 0)
+               return 0;
+*/
+
+       if (table > 15)   /* ESC-table is used */
+       {
+               unsigned                                linbits = h->linbits;
+               unsigned                                ext     = 0;
+               unsigned                                xbits   = 0;
+
+               if (x)  {if (x > 14)  {ext =                    (x-15);  xbits += linbits;  x = 15;}  ext = (ext << 1) | signx;  xbits++;}
+               if (y)  {if (y > 14)  {ext = (ext << linbits) | (y-15);  xbits += linbits;  y = 15;}  ext = (ext << 1) | signy;  xbits++;}
+
+               idx   = x * h->ylen + y;
+               code  = h->table[idx];
+               cbits = h->hlen[idx];
+
+               addBits (ph, code, cbits);
+               addBits (ph, ext , xbits);
+
+               return cbits + xbits;
+       }
+       else   /* No ESC-words */
+       {
+               idx   = x * h->ylen + y;
+               code  = h->table[idx];
+               cbits = h->hlen[idx];
+
+               if (x)  {code = (code << 1) | signx;  cbits++;}
+               if (y)  {code = (code << 1) | signy;  cbits++;}
+
+               addBits (ph, code, cbits);
+
+               return cbits;
+       }
+}
+
+
+
diff --git a/lib/bladeenc/l3bitstream.h b/lib/bladeenc/l3bitstream.h
new file mode 100644 (file)
index 0000000..a019709
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+                       (c) Copyright 1998, 1999 - Tord Jansson
+                       =======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       reformatted
+*/
+
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: l3bitstream.h,v 1.1 2002/01/10 17:30:00 kramm Exp $
+ *
+ * $Log: l3bitstream.h,v $
+ * Revision 1.1  2002/01/10 17:30:00  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+
+#ifndef                __L3BITSTREAM__
+#define                __L3BITSTREAM__
+
+
+
+
+
+#include "common.h"
+#include "encoder.h"
+
+
+
+
+
+void                                   III_format_bitstream
+(
+       int                                             bitsPerFrame,
+       frame_params                    *in_fr_ps,
+       int                                             l3_enc[2][2][576],
+       III_side_info_t                 *l3_side,
+       III_scalefac_t                  *scalefac,
+       double                                  (*xr)[2][576],
+       char                                    *ancillary,
+       int                                             anc_bits
+);
+
+void                                   III_FlushBitstream (void);
+
+void                                   fixStatic_l3bitstream (void);
+
+
+
+
+
+#endif         /* __L3BITSTREAM__ */
diff --git a/lib/bladeenc/l3psy.c b/lib/bladeenc/l3psy.c
new file mode 100644 (file)
index 0000000..2b9960f
--- /dev/null
@@ -0,0 +1,1188 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-05  Andre Piotrowski
+
+       -       speed up: implemented prepacking of fft-data
+
+       2000-12-11  ap
+
+       -       speed up:  faster psycho_anal()
+       -       optional bug fix: integrated better norm calclulation and block selecting
+
+       2000-12-12  ap
+
+       -       use SHORT_FFT_MIN_IDX to allow switching of "ORG_SHORT_CW_LIMIT" in "l3psy.h"
+
+       2001-01-12  ap
+
+       -       use some explicit type casting to avoid compiler warnings
+*/
+
+#define                RING_BUFFER                             1
+
+
+
+
+
+#include       <stdlib.h>
+
+#include       "common.h"
+#include       "tables.h"
+#include       "encoder.h"
+#include       "l3side.h"
+#include       "l3psy.h"
+
+
+
+
+
+/*____ Global Static Variables ______________________________________________*/
+
+/* The static variables "r", "phi_sav", "new_", "old" and "oldest" have    */
+/* to be remembered for the unpredictability measure.  For "r" and        */
+/* "phi_sav", the first index from the left is the channel select and     */
+/* the second index is the "age" of the data.                             */
+
+
+static int                             new_, old, oldest;
+static int                             flush, sync_flush, syncsize;
+
+#if RING_BUFFER==1
+static int                             savebuf_start_idx[2];
+#endif
+
+
+
+#if NEW_L3PARM_TABLES
+
+static double                  *minval, *qthr_l;
+static double                  *qthr_s, *SNR_s;
+static int                             *cbw_l, *bu_l, *bo_l;
+static int                             *cbw_s, *bu_s, *bo_s;
+static double                  *w1_l, *w2_l;
+static double                  *w1_s, *w2_s;
+
+#if ORG_NUMLINES_NORM
+
+static int                             cbmax_l = CBANDS, cbmax_s = CBANDS_s;
+static int                             numlines_l  [CBANDS];
+
+static int                             partition_l [HBLKSIZE];
+static int                             partition_s [HBLKSIZE_s];
+static double                  s3_l        [CBANDS][CBANDS];
+static double                  *norm_l, *norm_s;
+
+#else
+
+static int                             cbmax_l, cbmax_s;
+static int                             *numlines_l;
+static int                             *numlines_s;
+
+                                               /* the non-zero entries of norm_l[i] * s3_l[i][j] */
+static FLOAT                   normed_s3_l [900];   /* a bit more space than needed [799|855|735] */
+static int                             lo_s3_l     [CBANDS];
+static int                             hi_s3_l         [CBANDS];
+
+static FLOAT                   normed_s3_s [500];   /* a bit more space than needed [445|395|378] */
+static int                             lo_s3_s     [CBANDS_s];
+static int                             hi_s3_s         [CBANDS_s];
+
+#endif         /* ORG_NUMLINES_NORM */
+
+#else
+
+static double                  minval[CBANDS], qthr_l[CBANDS], norm_l[CBANDS];
+static double                  qthr_s[CBANDS_s], norm_s[CBANDS_s], SNR_s[CBANDS_s];
+static int                             cbw_l[SBMAX_l],bu_l[SBMAX_l],bo_l[SBMAX_l];
+static int                             cbw_s[SBMAX_s],bu_s[SBMAX_s],bo_s[SBMAX_s];
+static double                  w1_l[SBMAX_l], w2_l[SBMAX_l];
+static double                  w1_s[SBMAX_s], w2_s[SBMAX_s];
+
+static int                             numlines_l  [CBANDS];
+
+static int                             partition_l [HBLKSIZE];
+static int                             partition_s [HBLKSIZE_s];
+static double                  s3_l        [CBANDS][CBANDS];
+
+#endif         /* NEW_L3PARM_TABLES */
+
+
+
+/* Scale Factor Bands */
+static int                             blocktype_old[2];
+
+
+
+static double                  nb_1        [2][CBANDS];
+static double                  nb_2        [2][CBANDS];
+
+static double                  cw          [HBLKSIZE];
+
+static FLOAT                   window      [BLKSIZE];
+static FLOAT                   r                       [2][2][6];
+static FLOAT                   phi_sav         [2][2][6];
+
+static FLOAT                   window_s    [BLKSIZE_s];
+
+static double                  ratio       [2][SBMAX_l];
+static double                  ratio_s     [2][SBMAX_s][3];
+
+
+
+
+
+#if NEW_L3PARM_TABLES
+
+static void                    L3para_read (int sfreq);
+
+#if !ORG_NUMLINES_NORM
+static void                    calc_normed_spreading
+(
+       int                                             cbmax,                  /* number of lines and rows           */
+       const double                    bval[],                 /* input values to compute the matrix */
+       FLOAT                                   s3_ptr[],               /* the resulting non-zero entries     */
+       int                                             lo_s3[],
+       int                                             hi_s3[],
+       const double                    norm[]
+);
+#endif
+
+#else
+
+static void                    L3para_read
+(
+       int                                             sfreq,
+       int                                             numlines_l[CBANDS],
+       int                                             partition_l[HBLKSIZE],
+       double                                  minval[CBANDS],
+       double                                  qthr_l[CBANDS],
+       double                                  norm_l[CBANDS],
+       double                                  s3_l[CBANDS][CBANDS],
+       int                                             partition_s[HBLKSIZE_s],
+       double                                  qthr_s[CBANDS_s],
+       double                                  norm_s[CBANDS_s],
+       double                                  SNR_s[CBANDS_s],
+       int                                             cbw_l[SBMAX_l],
+       int                                             bu_l[SBMAX_l],
+       int                                             bo_l[SBMAX_l],
+       double                                  w1_l[SBMAX_l],
+       double                                  w2_l[SBMAX_l],
+       int                                             cbw_s[SBMAX_s],
+       int                                             bu_s[SBMAX_s],
+       int                                             bo_s[SBMAX_s],
+       double                                  w1_s[SBMAX_s],
+       double                                  w2_s[SBMAX_s]
+);
+
+#endif
+
+
+
+
+
+/*____ psycho_anal_init() ___________________________________________________*/
+
+void                                   psycho_anal_init (double sfreq)
+{
+       unsigned int                    ch, sfb, b, i, j;
+
+
+       /* reset the r, phi_sav "ring buffer" indices */
+
+       old = 1 - (new_ = oldest = 0);
+
+
+       /* clear the ratio arrays */
+
+       for (ch = 0;  ch < 2;  ch++)
+       {
+               for (sfb = 0;  sfb < SBMAX_l;  sfb++)
+                       ratio[ch][sfb] = 0.0;
+
+               for (sfb = 0;  sfb < SBMAX_s;  sfb++)
+                       for (b = 0;  b < 3;  b++)
+                               ratio_s[ch][sfb][b] = 0.0;
+       }
+
+
+       /* clear preecho arrays */
+
+       for (ch = 0;  ch < 2;  ch++)
+       {
+               for (i = 0;  i < CBANDS;  i++)
+               {
+                       nb_1[ch][i] = 0;
+                       nb_2[ch][i] = 0;
+               }
+       }
+
+
+       /* clear blocktype information */
+
+       for (ch = 0;  ch < 2;  ch++)
+               blocktype_old[ch] = NORM_TYPE;
+
+
+       sync_flush =  768;
+       flush      =  576;
+       syncsize   = 1344;   /* sync_flush + flush */
+
+
+#if RING_BUFFER==1
+       for (ch = 0;  ch < 2;  ch++)
+               savebuf_start_idx[ch] = 0;
+#endif
+
+
+       /* calculate HANN window coefficients */
+
+    for (i = 0;  i < BLKSIZE;  i++)
+               window[i] = (FLOAT) (0.5 * (1 - cos (2.0 * PI * (i - 0.5) / BLKSIZE)));
+
+    for (i = 0;  i < BLKSIZE_s;  i++)
+               window_s[i] = (FLOAT) (0.5 * (1 - cos (2.0 * PI * (i - 0.5) / BLKSIZE_s)));
+
+
+       /* reset states used in unpredictability measure */
+
+       for (ch = 0;  ch < 2;  ch++)
+       {
+               for (i = 0;  i < 2;  i++)
+               {
+                       for (j = 0;  j < 6;  j++)
+                       {
+                                     r[ch][i][j] = 0.0;
+                               phi_sav[ch][i][j] = 0.0;
+                       }
+               }
+       }
+
+
+#if NEW_L3PARM_TABLES
+       L3para_read ((int) sfreq);
+#else
+       L3para_read
+       (
+               (int) sfreq,
+               numlines_l, partition_l, minval, qthr_l, norm_l, s3_l,
+               partition_s, qthr_s, norm_s, SNR_s,
+               cbw_l, bu_l, bo_l, w1_l, w2_l,
+               cbw_s, bu_s, bo_s, w1_s, w2_s
+       );
+#endif
+
+
+       /* Set unpredicatiblility of remaining spectral lines to 0.4 */
+
+       for (j = 206;  j < HBLKSIZE;  j++)
+               cw[j] = 0.4;
+}
+
+
+
+
+
+/*____ psycho_anal_exit() ___________________________________________________*/
+
+void psycho_anal_exit( void )
+{
+       /* nothing to do */
+}
+
+
+
+
+
+/*____ psycho_anal() ________________________________________________________*/
+                                                                       
+void                                   psycho_anal
+(
+#if ORG_BUFFERS
+       short int                               *buffer,
+       short int                               savebuf[2048],
+#else
+       FLOAT                                   *buffer,
+       int                                             buffer_idx,
+#endif
+       int                                             ch,
+       int                                             lay,
+/*     float                                   snr32[32], */
+       double                                  ratio_d[SBMAX_l],
+       double                                  ratio_ds[SBMAX_s][3],
+       double                                  *pe,
+       gr_info                                 *cod_info
+)
+{
+       int                                             blocktype;
+       unsigned int                    sfb, b, j, k;
+       double                                  r_prime, phi_prime; /* not FLOAT */
+       double                                  temp1, temp2, temp3;
+
+#if !ORG_NUMLINES_NORM && NEW_L3PARM_TABLES
+       FLOAT                                   *s3_ptr;
+#endif
+
+       int                                             sblock;
+
+       double                                  thr         [CBANDS];
+       double                                  eb          [CBANDS];
+       FLOAT                                   cb          [CBANDS];
+       FLOAT                                   wsamp_r     [HBLKSIZE];
+       FLOAT                                   wsamp_i     [HBLKSIZE];
+
+       FLOAT                                   energy      [HBLKSIZE];
+       FLOAT                                   phi         [6];
+       FLOAT                                   energy_s    [3][BLKSIZE_s];
+       FLOAT                                   phi_s       [3][52];
+
+#if ORG_BUFFERS
+#if RING_BUFFER==1
+       int                                             beg, idx, fin;
+#endif
+#else
+#      define                                  savebuf         buffer
+#      define                                  beg                     buffer_idx
+       int                                             idx, fin;
+#endif
+
+       
+       for (sfb = 0;  sfb < SBMAX_l;  sfb++)
+               ratio_d[sfb] = ratio[ch][sfb];
+
+       for (sfb = 0;  sfb < SBMAX_s;  sfb++)
+               for (b = 0;  b < 3;  b++)
+                       ratio_ds[sfb][b] = ratio_s[ch][sfb][b];
+       
+
+       if (ch == 0)
+               old = 1 - (new_ = oldest = old);
+
+
+#if ORG_BUFFERS
+       /**********************************************************************
+       *  Delay signal by sync_flush=768 samples                             *
+       **********************************************************************/
+
+#      if RING_BUFFER==0
+               for (j = 0;  j < sync_flush;  j++)   /* for long window samples */
+                       savebuf[j] = savebuf[j+flush];
+
+               for (j = sync_flush;  j < syncsize;  j++)
+                       savebuf[j] = *buffer++;
+#      else
+               beg = savebuf_start_idx[ch] = (savebuf_start_idx[ch] + flush) & 2047;
+
+               idx = (beg + sync_flush) & 2047;
+               fin = (idx + flush) & 2047;
+               if (idx >= fin)
+               {
+                       while (idx < 2048)
+                               savebuf[idx++] = *buffer++;
+                       idx = 0;
+               }
+               while (idx < fin)
+                       savebuf[idx++] = *buffer++;
+#      endif
+#endif
+
+
+/**********************************************************************
+*    compute unpredicatability of first six spectral lines            * 
+**********************************************************************/
+
+#if RING_BUFFER==0
+       for (j = 0, k = 0, idx = 0;  j < BLKSIZE/2;  j++)
+       {
+               wsamp_r[j] = window[k++] * savebuf[idx++];
+               wsamp_i[j] = window[k++] * savebuf[idx++];
+       }
+#else
+       j = 0;  k = 0;
+       idx = beg;
+       fin = (idx + BLKSIZE) & 2047;
+       if (idx >= fin)
+       {
+               while (idx < 2048)
+               {
+                       wsamp_r[j] = window[k++] * savebuf[idx++];
+                       wsamp_i[j] = window[k++] * savebuf[idx++];
+                       j++;
+               }
+               idx = 0;
+       }
+       while (idx < fin)
+       {
+               wsamp_r[j] = window[k++] * savebuf[idx++];
+               wsamp_i[j] = window[k++] * savebuf[idx++];
+               j++;
+       }
+#endif
+
+       fft(wsamp_r, wsamp_i, energy, phi, BLKSIZE);   /* long FFT */
+
+       for (j = 0;  j < 6;  j++)
+       {       /* calculate unpredictability measure cw */
+               double r1, phi1;
+                                    r_prime = 2.0 *       r[ch][old][j] -       r[ch][oldest][j];
+                                  phi_prime = 2.0 * phi_sav[ch][old][j] - phi_sav[ch][oldest][j];
+                     r[ch][new_][j] = (FLOAT) (  r1 = sqrt((double) energy[j]));
+               phi_sav[ch][new_][j] = (FLOAT) (phi1 =                  phi[j] );
+
+               temp3 = r1 + fabs(r_prime);
+               if (temp3 != 0.0)
+               {
+                       temp1 = r1 * cos(phi1) - r_prime * cos(phi_prime);
+                       temp2 = r1 * sin(phi1) - r_prime * sin(phi_prime);
+                       cw[j] = sqrt(temp1*temp1 + temp2*temp2) / temp3;
+               }
+               else
+                       cw[j] = 0;
+       }
+
+
+/**********************************************************************
+*     compute unpredicatibility of next 200 spectral lines            *
+**********************************************************************/ 
+
+       for (b = 0;  b < 3;  b++)
+       {
+#if RING_BUFFER==0
+               for (j = 0, k = 0, idx = 128*(2 + b);  j < BLKSIZE_s/2;  j++)
+               {       /* window data with HANN window */
+                       wsamp_r[j] = window_s[k++] * savebuf[idx++];
+                       wsamp_i[j] = window_s[k++] * savebuf[idx++];
+               }
+#else
+               j = 0;  k = 0;
+               idx = (beg + 128*(2 + b)) & 2047;
+               fin = (idx + BLKSIZE_s) & 2047;
+               if (idx >= fin)
+               {
+                       while (idx < 2048)
+                       {
+                               wsamp_r[j] = window_s[k++] * savebuf[idx++];
+                               wsamp_i[j] = window_s[k++] * savebuf[idx++];
+                               j++;
+                       }
+                       idx = 0;
+               }
+               while (idx < fin)
+               {
+                       wsamp_r[j] = window_s[k++] * savebuf[idx++];
+                       wsamp_i[j] = window_s[k++] * savebuf[idx++];
+                       j++;
+               }
+#endif
+
+               fft (wsamp_r, wsamp_i, energy_s[b], phi_s[b], BLKSIZE_s);   /* short FFT*/
+       }
+       for (j = 6, k = SHORT_FFT_MIN_IDX;  j < 206;  j += 4, k++)
+       {       /* calculate unpredictability measure cw */
+               double r1, phi1;
+
+                 r_prime = 2.0 * sqrt((double) energy_s[0][k]) - sqrt((double) energy_s[2][k]);
+               phi_prime = 2.0 *                  phi_s[0][k]  -                  phi_s[2][k];
+                      r1 = sqrt((double) energy_s[1][k]);
+                    phi1 =                  phi_s[1][k];
+
+               temp3 = r1 + fabs(r_prime);
+               if (temp3 != 0.0)
+               {
+                       temp1 = r1 * cos(phi1) - r_prime * cos(phi_prime);
+                       temp2 = r1 * sin(phi1) - r_prime * sin(phi_prime);
+                       cw[j] = sqrt(temp1*temp1 + temp2*temp2) / temp3;
+               }
+               else
+                       cw[j] = 0.0;
+
+               cw[j+1] = cw[j+2] = cw[j+3] = cw[j];
+       }
+
+
+/**********************************************************************
+*    Calculate the energy and the unpredictability in the threshold   *
+*    calculation partitions                                           *
+**********************************************************************/
+
+#if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
+
+       for (b = 0;  b < cbmax_l;  b++)
+       {
+               eb[b] = 0.0;
+               cb[b] = 0.0;
+       }
+       for (j = 0;  j < HBLKSIZE;  j++)
+       {
+               int tp = partition_l[j];
+               if (tp >= 0)
+               {
+                       eb[tp] += energy[j];
+                       cb[tp] += cw[j] * energy[j];
+               }
+       }
+
+#else
+
+       j = 0;
+       for (b = 0;  b < cbmax_l;  b++)
+       {
+               eb[b] = 0.0;
+               cb[b] = 0.0;
+
+               /*
+                       Calculate the energy and the unpredictability in the threshold
+                       calculation partitions
+
+                       cbmax_l holds the number of valid numlines_l entries
+               */
+               k = numlines_l[b];
+               do {
+                       eb[b] += energy[j];
+                       cb[b] += cw[j] * energy[j];
+               } while (j++, --k);
+       }
+
+       s3_ptr = normed_s3_l;
+
+#endif
+
+
+       *pe = 0.0;
+       
+       for (b = 0;  b < cbmax_l;  b++)
+       {
+               FLOAT                                   nb;
+               FLOAT                                   ecb = 0.0;
+               double                                  ctb = 0.0;
+               double                                  SNR_l;
+               double                                  cbb, tbb;
+
+
+               /*
+                       convolve the partitioned energy and unpredictability
+                       with the spreading function, normed_s3_l[b][k]
+               */
+#if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
+               for (k = 0;  k < cbmax_l;  k++)
+               {
+                       ecb += s3_l[b][k] * eb[k];  /* sprdngf for Layer III */
+                       ctb += s3_l[b][k] * cb[k];
+               }
+#else
+               for (k = lo_s3_l[b];  k < hi_s3_l[b];  k++)
+               {
+                       ecb += *s3_ptr   * eb[k];  /* sprdngf for Layer III */
+                       ctb += *s3_ptr++ * cb[k];
+               }
+#endif
+
+
+               /*
+                       calculate the tonality of each threshold calculation partition
+                       calculate the SNR in each threshhold calculation partition
+               */
+               if (ecb != 0.0)
+               {
+                       cbb = ctb / ecb;
+                       if (cbb < 0.01)
+                               cbb = 0.01;
+                       tbb = -0.299 - 0.43 * log(cbb);   /* conv1=-0.299, conv2=-0.43 */
+                       tbb = MIN(MAX (0.0, tbb), 1.0) ;  /* 0<=tbb<=1 */
+               }
+               else
+                       tbb = 0.0;  /* cbb==0 => -0.299-0.43*cbb<0 => tbb=0*/
+
+               /* TMN=29.0,NMT=6.0 for all calculation partitions */
+               SNR_l = MAX (minval[b], 23.0 * tbb + 6.0);   /* 29*tbb + 6*(1-tbb) */
+       
+               /* calculate the threshold for each partition */
+#if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
+           nb = ecb * norm_l[b] * exp(-SNR_l * LN_TO_LOG10);
+#else
+           nb = ecb * exp(-SNR_l * LN_TO_LOG10);   /* our ecb is already normed */
+#endif
+
+               /*
+                       pre-echo control
+               */
+               thr[b] = MAX (qthr_l[b], MIN(nb, nb_2[ch][b]));
+               nb_2[ch][b] = MIN(2.0 * nb, 16.0 * nb_1[ch][b]);
+           nb_1[ch][b] = nb;
+
+
+               /*
+                       calculate percetual entropy
+
+                       thr[b] -> thr[b]+1.0 : for non sound portition
+               */
+               if (eb[b] > thr[b])
+                       *pe += numlines_l[b] * log((eb[b]+1.0) / (thr[b]+1.0));
+       }
+       
+
+#define switch_pe  1800
+       
+
+       if (*pe < switch_pe)
+       {
+               /* no attack : use long blocks */
+
+               if (blocktype_old[ch] == SHORT_TYPE)
+                       blocktype = STOP_TYPE;
+               else   /* NORM_TYPE, STOP_TYPE */
+                       blocktype = NORM_TYPE;
+
+
+               /* threshold calculation (part 2) */
+
+               for (sfb = 0;  sfb < SBMAX_l;  sfb++)
+               {
+                       int             bu = bu_l[sfb];
+                       int             bo = bo_l[sfb];
+                       double  en = w1_l[sfb] * eb[bu] + w2_l[sfb] * eb[bo];
+
+                       for (b = bu+1;  b < bo;  b++)
+                               en += eb[b];
+
+                       if (en != 0.0)
+                       {
+                               double  thm = w1_l[sfb] * thr[bu] + w2_l[sfb] * thr[bo];
+
+                               for (b = bu+1;  b < bo;  b++)
+                                       thm += thr[b];
+
+                               ratio[ch][sfb] = thm / en;
+                       }
+                       else
+                               ratio[ch][sfb] = 0.0;
+               }
+       }
+       else
+       {
+               /* attack : use short blocks */
+               blocktype = SHORT_TYPE;
+#if ORG_BLOCK_SELECT
+               if (blocktype_old[ch] == NORM_TYPE)
+                       blocktype_old[ch] = START_TYPE;
+               else   /* SHORT_TYPE, STOP_TYPE */
+                       blocktype_old[ch] = SHORT_TYPE;
+#else   /* ISO */
+               if (blocktype_old[ch] == SHORT_TYPE)
+                       blocktype_old[ch] = SHORT_TYPE;
+               else   /* NORM_TYPE, STOP_TYPE */
+                       blocktype_old[ch] = START_TYPE;
+#endif
+
+
+               /* threshold calculation for short blocks */
+
+               for (sblock = 0;  sblock < 3;  sblock++)
+               {
+#if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
+
+                       for (b = 0;  b < cbmax_s;  b++)
+                               eb[b] = 0.0;
+
+                       for (j = 0;  j < HBLKSIZE_s;  j++)
+                               eb[partition_s[j]] += energy_s[sblock][j];
+
+#else
+
+                       j = 0;
+                       for (b = 0;  b < cbmax_s;  b++)
+                       {
+                               eb[b] = 0.0;
+
+                               /*
+                                       Calculate the energy and the unpredictability in the threshold
+                                       calculation partitions
+
+                                       cbmax_s holds the number of valid numlines_s entries
+                               */
+                               k = numlines_s[b];
+                               do {
+                                       eb[b] += energy_s[sblock][j];
+                               } while (j++, --k);
+                       }
+
+                       s3_ptr = normed_s3_s;
+#endif
+
+                       for (b = 0;  b < cbmax_s;  b++)
+                       {
+                               FLOAT                                   nb;
+                               FLOAT                                   ecb = 0.0;
+
+#if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
+                               for (k = 0;  k < cbmax_s;  k++)
+                                       ecb += s3_l[b][k] * eb[k];
+
+                               nb = ecb * norm_l[b] * exp((double) SNR_s[b] * LN_TO_LOG10);
+#else
+                               for (k = lo_s3_s[b];  k < hi_s3_s[b];  k++)
+                                       ecb += *s3_ptr++ * eb[k];
+
+                               nb = ecb * exp((double) SNR_s[b] * LN_TO_LOG10);   /* our ecb is already normed */
+#endif
+                               thr[b] = MAX(qthr_s[b], nb);
+                       }
+
+                       for (sfb = 0;  sfb < SBMAX_s;  sfb++)
+                       {
+                               int             bu = bu_s[sfb];
+                               int             bo = bo_s[sfb];
+                               double  en = w1_s[sfb] * eb[bu] + w2_s[sfb] * eb[bo];
+
+                               for (b = bu+1;  b < bo;  b++)
+                                       en += eb[b];
+                               if (en != 0.0)
+                               {
+                                       double  thm = w1_s[sfb] * thr[bu] + w2_s[sfb] * thr[bo];
+
+                                       for (b = bu+1;  b < bo;  b++)
+                                               thm += thr[b];
+
+                                       ratio_s[ch][sfb][sblock] = thm / en;
+                               }
+                               else
+                                       ratio_s[ch][sfb][sblock] = 0.0;
+                       }
+               }
+       } 
+       
+       cod_info->block_type = blocktype_old[ch];
+       blocktype_old[ch] = blocktype;
+
+       if ( cod_info->block_type == NORM_TYPE )
+           cod_info->window_switching_flag = 0;
+       else
+           cod_info->window_switching_flag = 1;
+
+       cod_info->mixed_block_flag = 0;
+}
+
+
+
+
+
+/*____ L3para_read() __________________________________________________________*/
+
+#if NEW_L3PARM_TABLES
+
+static void                            L3para_read (int sfreq)
+{
+       int                                             sfreq_idx;
+       l3_parm_block                   *parm;
+       double                                  *bval_l, *bval_s;
+
+#if ORG_NUMLINES_NORM
+       int                                             cbmax_l, cbmax_s;
+       int                                             i, j, k;
+#else
+       double                                  *norm_l, *norm_s;
+#endif
+
+
+       /*
+               Set parameter block
+       */
+       switch (sfreq)
+       {
+               case 32000:  sfreq_idx = 2;  break;
+               case 44100:  sfreq_idx = 0;  break;
+               case 48000:  sfreq_idx = 1;  break;
+               default   :  return;  /* Just to avoid compiler warnings */
+       }
+       parm = l3_parm + sfreq_idx;
+
+
+       /*
+               Read long block data
+       */
+       cbmax_l    = parm->long_data.cbmax_l;
+
+#if ORG_NUMLINES_NORM
+       for (i = 0, j = 0;  i < cbmax_l;  i++)
+       {
+               numlines_l[i] = parm->long_data.numlines_l[i];
+
+               for (k = 0;  k < numlines_l[i];  k++)
+                       partition_l[j++] = i;
+       }
+#else
+       numlines_l = parm->long_data.numlines_l;
+#endif
+               
+       minval     = parm->long_data.minval;
+       qthr_l     = parm->long_data.qthr_l;
+       norm_l     = parm->long_data.norm_l;
+       bval_l     = parm->long_data.bval_l;
+
+
+       /*
+               Compute the normed spreading function norm_l[i] * s3_l[i][j]
+       */
+#if ORG_NUMLINES_NORM
+       for (i = 0;  i < cbmax_l;  i++)
+       {
+               double x, temp, tempx, tempy;
+
+               for (j = 0;  j < cbmax_l;  j++)
+               {
+/*                     tempx = (bval_l[i]-bval_l[j]) * 1.05; */
+                       if (j >= i)
+                               tempx = (bval_l[i]-bval_l[j]) * 3.0;
+                       else
+                               tempx = (bval_l[i]-bval_l[j]) * 1.5;
+/*                     if (j >= i)  tempx = (bval_l[j]-bval_l[i]) * 3.0;
+                       else         tempx = (bval_l[j]-bval_l[i]) * 1.5; */
+                       if (tempx > 0.5  &&  tempx < 2.5)
+                       {
+                               temp = tempx - 0.5;
+                               x = 8.0 * temp * (temp-2.0);
+                       }
+                       else  x = 0.0;
+                       tempx += 0.474;
+                       tempy = 15.811389 + 7.5*tempx - 17.5*sqrt(1.0+tempx*tempx);
+                       if (tempy <= -60.0)  s3_l[i][j] = 0.0;
+                       else                 s3_l[i][j] = exp((x + tempy) * LN_TO_LOG10);
+               }
+       }
+#else
+       calc_normed_spreading (cbmax_l, bval_l, normed_s3_l, lo_s3_l, hi_s3_l, norm_l);
+#endif
+
+
+       /*
+               Read short block data
+       */
+       cbmax_s    = parm->short_data.cbmax_s;
+
+#if ORG_NUMLINES_NORM
+       for (i = 0, j = 0;  i < cbmax_s;  i++)
+       {
+               numlines_l[i] = parm->short_data.numlines_s[i];
+
+               for (k = 0;  k < numlines_l[i];  k++)
+                       partition_s[j++] = i;
+       }
+#else
+       numlines_s = parm->short_data.numlines_s;
+#endif
+
+       qthr_s     = parm->short_data.qthr_s;
+       norm_s     = parm->short_data.norm_s;
+       SNR_s      = parm->short_data.SNR_s;
+       bval_s     = parm->short_data.bval_s;
+
+
+#if !ORG_NUMLINES_NORM
+
+       /*
+               Compute the normed spreading function norm_s[i] * s3_s[i][j]
+       */
+       calc_normed_spreading (cbmax_s, bval_s, normed_s3_s, lo_s3_s, hi_s3_s, norm_s);
+
+#endif
+
+
+       /*
+               Read long block data for converting threshold
+               calculation partitions to scale factor bands
+       */
+       cbw_l = parm->long_thres.cbw_l;
+       bu_l  = parm->long_thres.bu_l;
+       bo_l  = parm->long_thres.bo_l;
+       w1_l  = parm->long_thres.w1_l;
+       w2_l  = parm->long_thres.w2_l;
+
+
+       /*
+               Read short block data for converting threshold
+               calculation partitions to scale factor bands
+       */
+       cbw_s = parm->short_thres.cbw_s;
+       bu_s  = parm->short_thres.bu_s;
+       bo_s  = parm->short_thres.bo_s;
+       w1_s  = parm->short_thres.w1_s;
+       w2_s  = parm->short_thres.w2_s;
+}
+
+#else          /* NEW_L3PARM_TABLES */
+
+static void                    L3para_read
+(
+       int                                             sfreq,
+       int                                             numlines_l[CBANDS],
+       int                                             partition_l[HBLKSIZE],
+       double                                  minval[CBANDS],
+       double                                  qthr_l[CBANDS],
+       double                                  norm_l[CBANDS],
+       double                                  s3_l[CBANDS][CBANDS],
+       int                                             partition_s[HBLKSIZE_s],
+       double                                  qthr_s[CBANDS_s],
+       double                                  norm_s[CBANDS_s],
+       double                                  SNR_s[CBANDS_s],
+       int                                             cbw_l[SBMAX_l],
+       int                                             bu_l[SBMAX_l],
+       int                                             bo_l[SBMAX_l],
+       double                                  w1_l[SBMAX_l],
+       double                                  w2_l[SBMAX_l],
+       int                                             cbw_s[SBMAX_s],
+       int                                             bu_s[SBMAX_s],
+       int                                             bo_s[SBMAX_s],
+       double                                  w1_s[SBMAX_s],
+       double                                  w2_s[SBMAX_s]
+)
+{
+       static  double                  bval_l[CBANDS];
+       int                                             cbmax_tp;
+
+       int                                             sbmax;
+       int                                             i, j, k, k2;
+
+
+       psyDataElem                             *rpa1;
+       psyDataElem2                    *rpa2;
+       psyDataElem3                    *rpa3;
+
+
+/* Read long block data */
+
+       switch (sfreq)
+       {
+               case 32000:  rpa1 = psy_longBlock_32000_58;  cbmax_tp = 59;  break;
+               case 44100:  rpa1 = psy_longBlock_44100_62;  cbmax_tp = 63;  break;
+               case 48000:  rpa1 = psy_longBlock_48000_61;  cbmax_tp = 62;  break;
+               default   :  return;  /* Just to avoid compiler warnings */
+       }
+
+       for (i = 0, k2 = 0;  i < cbmax_tp;  i++)
+       {
+               numlines_l[i] = rpa1->lines;
+               minval[i]     = rpa1->minVal;
+               qthr_l[i]     = rpa1->qthr;
+               norm_l[i]     = rpa1->norm;
+               bval_l[i]     = rpa1->bVal;
+               rpa1++;
+
+               for (k = 0;  k < numlines_l[i];  k++)
+                       partition_l[k2++] = i;
+       }
+
+               
+/************************************************************************
+ * Now compute the spreading function, s[j][i], the value of the spread-*
+ * ing function, centered at band j, for band i, store for later use    *
+ ************************************************************************/
+
+       for (i = 0;  i < cbmax_tp;  i++)
+       {
+               double x, temp, tempx, tempy;
+
+               for (j = 0;  j < cbmax_tp;  j++)
+               {
+/*                     tempx = (bval_l[i]-bval_l[j]) * 1.05; */
+                       if (j >= i)
+                               tempx = (bval_l[i]-bval_l[j]) * 3.0;
+                       else
+                               tempx = (bval_l[i]-bval_l[j]) * 1.5;
+/*                     if (j >= i)  tempx = (bval_l[j]-bval_l[i]) * 3.0;
+                       else         tempx = (bval_l[j]-bval_l[i]) * 1.5; */
+                       if (tempx > 0.5  &&  tempx < 2.5)
+                       {
+                               temp = tempx - 0.5;
+                               x = 8.0 * temp * (temp-2.0);
+                       }
+                       else  x = 0.0;
+                       tempx += 0.474;
+                       tempy = 15.811389 + 7.5*tempx - 17.5*sqrt(1.0+tempx*tempx);
+                       if (tempy <= -60.0)  s3_l[i][j] = 0.0;
+                       else                 s3_l[i][j] = exp((x + tempy) * LN_TO_LOG10);
+               }
+       }
+
+
+/* Read short block data */
+
+       switch (sfreq)
+       {
+               case 32000:  rpa2 = psy_shortBlock_32000_41;  cbmax_tp = 42;  break;
+               case 44100:  rpa2 = psy_shortBlock_44100_38;  cbmax_tp = 39;  break;
+               case 48000:  rpa2 = psy_shortBlock_48000_37;  cbmax_tp = 38;  break;
+               default   :  return;  /* Just to avoid compiler warnings */
+       }
+
+       for (i = 0, k2 = 0;  i < cbmax_tp;  i++)
+       {
+               numlines_l[i] = rpa2->lines;
+               qthr_s[i]     = rpa2->qthr;
+               norm_s[i]     = rpa2->norm;
+               SNR_s[i]      = rpa2->snr;
+               rpa2++;
+
+               for (k = 0;  k < numlines_l[i];  k++)
+                       partition_s[k2++] = i;
+       }
+
+
+/* Read long block data for converting threshold calculation
+   partitions to scale factor bands */
+
+       switch (sfreq)
+       {
+               case 32000:  rpa3 = psy_data3_32000_20;  break;
+               case 44100:  rpa3 = psy_data3_44100_20;  break;
+               case 48000:  rpa3 = psy_data3_48000_20;  break;
+               default   :  return;  /* Just to avoid compiler warnings */
+       }
+       sbmax = SBMAX_l;
+
+       for (i = 0;  i < sbmax;  i++)
+       {
+               cbw_l[i] = rpa3->cbw;
+               bu_l[i] = rpa3->bu;
+               bo_l[i] = rpa3->bo;
+               w1_l[i] = rpa3->w1;
+               w2_l[i] = rpa3->w2;
+               rpa3++;         
+       }
+
+
+/* Read short block data for converting threshold calculation
+   partitions to scale factor bands */
+
+       switch (sfreq)
+       {
+               case 32000:  rpa3 = psy_data4_32000_11;  break;
+               case 44100:  rpa3 = psy_data4_44100_11;  break;
+               case 48000:  rpa3 = psy_data4_48000_11;  break;
+               default   :  return;  /* Just to avoid compiler warnings */
+       }
+       sbmax = SBMAX_s;
+
+       for (i = 0;  i < sbmax;  i++)
+       {
+               cbw_s[i] = rpa3->cbw;
+               bu_s[i] = rpa3->bu;
+               bo_s[i] = rpa3->bo;
+               w1_s[i] = rpa3->w1;
+               w2_s[i] = rpa3->w2;
+               rpa3++;         
+       }       
+}
+
+#endif         /* NEW_L3PARM_TABLES */
+
+
+
+
+
+#if !ORG_NUMLINES_NORM && NEW_L3PARM_TABLES
+
+/*  ========================================================================================  */
+/*              calc_normed_spreading                                                         */
+/*  ========================================================================================  */
+/*
+       Compute the normed spreading function,
+       the normed value of the spreading function,
+       centered at band j, for band i, store for later use
+
+       Since this is a band matrix, we store only the non-zero entries
+       in linear order in the single dimension array normed_s3.
+
+       The array has to be accessed in linear order, too, starting with line 0,
+       up to line cbmax-1. For line b, the current entries represent
+
+               norm[b] * s3[b][lo_s3[b]]  ...  norm[b] * s3[b][hi_s3[b]-1]
+
+       Normally, we could easily compute the norm [building the reciprocal of the line sum].
+       Alas, dist10 uses somewhat (strange and) different, that made our norm differring too
+       much at the last few lines. Thus, we renounce and use the original values.
+*/
+
+static void                    calc_normed_spreading
+(
+       int                                             cbmax,                  /* number of lines and rows           */
+       const double                    bval[],                 /* input values to compute the matrix */
+       FLOAT                                   s3_ptr[],               /* the resulting non-zero entries     */
+       int                                             lo_s3[],
+       int                                             hi_s3[],
+       const double                    norm[]
+)
+{
+       double                                  arg, x, y;
+       double                                  s3[CBANDS];
+       int                                             i, j;
+       int                                             non_zero_part;
+
+
+
+       for (i = 0;  i < cbmax;  i++)
+       {
+               non_zero_part = FALSE;
+               hi_s3[i] = cbmax;   /* we preset this value for the case that the line ends with a non-zero entry */
+
+               for (j = 0;  j < cbmax;  j++)
+               {
+                       if (j >= i)
+                               arg = (bval[i] - bval[j]) * 3.0;
+                       else
+                               arg = (bval[i] - bval[j]) * 1.5;
+
+                       if (arg > 0.5  &&  arg < 2.5)
+                               x = 8.0 * (arg - 0.5) * (arg - 2.5);
+                       else
+                               x = 0.0;
+
+                       arg += 0.474;
+
+                       y = 15.811389 + 7.5 * arg - 17.5 * sqrt(1.0 + arg * arg);
+
+                       if (y <= -60.0)
+                       {
+                               if (non_zero_part)   /* only zeroes will follow */
+                               {
+                                       hi_s3[i] = j;
+                                       break;   /* so cut the computing for this line */
+                               }
+                       }
+                       else
+                       {
+                               s3[j] = exp((x + y) * LN_TO_LOG10);
+
+                               if (! non_zero_part)
+                               {
+                                       lo_s3[i] = j;
+                                       non_zero_part = TRUE;   /* the first non-zero entry ends the non_zero_part */
+                               }
+                       }
+               }
+
+               for (j = lo_s3[i];  j < hi_s3[i];  j++)
+                       *s3_ptr++ = s3[j] * norm[i];
+       }
+}
+
+#endif         /* ORG_NUMLINES_NORM */
diff --git a/lib/bladeenc/l3psy.h b/lib/bladeenc/l3psy.h
new file mode 100644 (file)
index 0000000..051335f
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       reformatted
+
+       2000-12-12  ap
+
+       -       removed fft() prototype from "encoder.h" to this file
+       -       removed the fft configuration switches from "subs.c" to this file
+       -       implemented ORG_SHORT_CW_LIMIT
+
+       2001-01-12  ap
+
+       -       bug fix: include encoder.h for definition of ORG_BUFFERS
+       -       clear some backward compatability flags for 0.93.10
+*/
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: l3psy.h,v 1.1 2002/01/10 17:30:00 kramm Exp $
+ *
+
+ * $Log: l3psy.h,v $
+ * Revision 1.1  2002/01/10 17:30:00  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+
+#ifndef                __L3PSY__
+#define        __L3PSY__
+
+
+
+#include       "encoder.h"
+
+
+
+
+
+/*  ========================================================================================  */
+/*              keeping backward compatability                                                */
+/*  ========================================================================================  */
+
+#define                ORG_NUMLINES_NORM               0   /* 0 = don't overwrite long numline entries with short numline entries */
+#define                ORG_BLOCK_SELECT                0   /* 0 = ISO draft paper states slightly different block selection */
+#define                ORG_SHORT_CW_LIMIT              0   /* 0 = ISO draft paper says first element starts at 6/4 = 1 (not 2) */
+
+
+
+
+
+/*     #define         CBANDS                                    63 */
+#define                CBANDS_s                                42
+#define                BLKSIZE_s                               256
+#define                HBLKSIZE_s                              129
+#define                TCBMAX_l                                63
+#define                TCBMAX_s                                42
+#define                SBMAX_l                                 21
+#define                SBMAX_s                                 12
+
+
+
+/*     #define         switch_pe                               1800 */
+#define                NORM_TYPE                               0
+#define                START_TYPE                              1
+#define                SHORT_TYPE                              2
+#define                STOP_TYPE                               3
+
+
+
+
+
+void                                   psycho_anal
+(
+#if ORG_BUFFERS
+       short int                               *buffer,
+       short int                               savebuf[2048],
+#else
+       FLOAT                                   *buffer,
+       int                                             buffer_idx,
+#endif
+       int                                             ch,
+       int                                             lay,
+/*     FLOAT                                   snr32[32], */
+       double                                  ratio_d[SBMAX_l],
+       double                                  ratio_ds[SBMAX_s][3],
+       double                                  *pe,
+       gr_info                                 *cod_info
+);
+
+void                                   psycho_anal_init (double sfreq);
+
+void                                   psycho_anal_exit (void);
+
+
+
+
+
+void                                   fft (FLOAT[], FLOAT[], FLOAT[], FLOAT[], int);
+
+
+
+/*
+       Switches for configuring the fft in "subs.c"
+*/
+
+/* Do we want to reorder the data? */
+#define                REORDER_DATA        0   /* no need to do that */
+
+/* To get the inverse fft, set NORMAL_FFT to 0 */
+#define                NORMAL_FFT                      1   /* si */
+
+/* Try this to speed up the fft when using non-complex sequences */
+#define                REAL_SEQUENCE           1   /* no complex values needed */
+
+/* We assume, the real data gets prepacked externally */
+#define                PREPACKED                       1   /* yes */
+
+/* No one needs all the phi values in layer 3 !!! */
+#define                USED_VALUES_ONLY        1
+#define                LONG_FFT_MIN_IDX        0
+#define                LONG_FFT_MAX_IDX        5
+#if ORG_SHORT_CW_LIMIT
+#define                SHORT_FFT_MIN_IDX       2    /* (  6+2)/4     */
+#define                SHORT_FFT_MAX_IDX       51   /* (206+2)/4 - 1 */
+#else   /* according to ISO */
+#define                SHORT_FFT_MIN_IDX       1    /*   6/4     */
+#define                SHORT_FFT_MAX_IDX       50   /* 206/4 - 1 */
+#endif
+
+
+
+
+
+#endif         /* __L3PSY__ */
diff --git a/lib/bladeenc/l3side.h b/lib/bladeenc/l3side.h
new file mode 100644 (file)
index 0000000..bc647a7
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-04  Andre Piotrowski
+
+       -       reformatted, slimmed
+*/
+
+
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: l3side.h,v 1.1 2002/01/10 17:30:00 kramm Exp $
+ *
+ * $Log: l3side.h,v $
+ * Revision 1.1  2002/01/10 17:30:00  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+/**********************************************************************
+ *   date   programmers                comment                        *
+ * 25. 6.92  Toshiyuki Ishino          Ver 1.0                        *
+ * 29.10.92  Masahiro Iwadare          Ver 2.0                        *
+ * 17. 4.93  Masahiro Iwadare          Updated for IS Modification    *
+ *                                                                    *
+ *********************************************************************/
+
+#ifndef                __L3SIDE__
+#define                __L3SIDE__
+
+
+
+
+
+/* #define CBLIMIT_SHORT 12 */
+
+
+
+
+
+/*     void                                    l3deco (void); */
+
+
+
+
+
+/* Layer III side information. */
+
+/*     typedef         double                                  D576[576]; */
+/*     typedef         int                                             I576[576]; */
+/*     typedef         double                                  D192_3[192][3]; */
+/*     typedef         int                                             I192_3[192][3]; */
+/*     typedef         char                                    C192_3[192][3]; */
+
+
+
+/*     typedef         struct
+                               {
+                                       unsigned                                cbl_max;
+                                       unsigned                                cbs_start;
+                               }                                               III_cb_info; */
+
+typedef                struct
+                       {
+                               double                                  l[2][2][21];
+                               double                                  s[2][2][12][3];
+                       }                                               III_psy_ratio;
+
+/*     typedef         struct
+                               {
+                                       double                                  l[2][2][21];
+                                       double                                  s[2][2][12][3];
+                               }                                               III_psy_xmin;*/
+
+/*     typedef         struct
+                               {
+                                       double                                  xr[576];
+                                       double                                  xr_s[3][192];
+                                       double                                  xmin[21];
+                                       double                                  xmin_s[3][192];
+                                       int                                             ix[576];
+                                       int                                             ix_s[3][192];
+                               }                                               III_input3;*/ /* ch */
+
+typedef                struct
+                       {
+                               unsigned                                part2_3_length;
+                               unsigned                                big_values;
+                               unsigned                                count1;
+                               unsigned                                global_gain;
+                               unsigned                                scalefac_compress;
+                               unsigned                                window_switching_flag;
+                               unsigned                                block_type;
+                               unsigned                                mixed_block_flag;
+                               unsigned                                table_select[3];
+                               int /*  unsigned */             subblock_gain[3];
+                               unsigned                                region0_count;
+                               unsigned                                region1_count;
+                               unsigned                                preflag;
+                               unsigned                                scalefac_scale;
+                               unsigned                                count1table_select;
+                               unsigned                                part2_length;
+                               unsigned                                sfb_lmax;
+                               unsigned                                sfb_smax;
+                               unsigned                                address1;
+                               unsigned                                address2;
+                               unsigned                                address3;
+                               double                                  quantizerStepSize;
+                               /* added for LSF */
+                               unsigned                                *sfb_partition_table;
+                               unsigned                                slen[4];
+                       }                                               gr_info;
+
+typedef                struct
+                       {
+                               int                                             main_data_begin; /* unsigned -> int */
+                               unsigned                                private_bits;
+                               int                                             resvDrain;
+                               unsigned                                scfsi[2][4];
+                               struct
+                               {
+                                       struct gr_info_s
+                                       {
+                                               gr_info                                 tt;
+                                       }                                               ch[2];
+                               }                                               gr[2];
+                       } III_side_info_t;
+
+
+
+
+
+/* Layer III scale factors. */
+
+typedef                struct
+                       {
+                               int                                             l[2][2][22];      /*         [cb] */
+                               int                                             s[2][2][13][3];   /* [window][cb] */
+                       }                                               III_scalefac_t;   /* [gr][ch] */
+
+
+
+
+
+#endif         /* __L3SIDE__ */
diff --git a/lib/bladeenc/loop-pvt.h b/lib/bladeenc/loop-pvt.h
new file mode 100644 (file)
index 0000000..cc8e2e0
--- /dev/null
@@ -0,0 +1,222 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-02-15  Pirandot
+
+       -       reformatted, used 'static', less parameters
+
+       2000-12-03  ap
+       -       adapted according to changes to "loop.h"
+*/
+
+#ifndef                __LOOP_PVT__
+#define                __LOOP_PVT__
+
+
+
+
+
+static int                             outer_loop
+(
+       int                                             max_bits,
+       frame_params                    *fr_ps
+);
+
+static int                             needed_bits_for_storing_scalefactors
+(
+       frame_params                    *fr_ps
+);
+
+static void                    calc_noise (void);
+
+static int                             loop_break (void);
+
+static int                             amplify
+(
+       int                                             iteration
+);
+
+static int INLINE              cutting_crew (FLOAT in);
+
+static void                    quantize (void);
+
+static void                    partial_quantize (void);
+
+static int                             bin_search_StepSize
+(
+       int                                             desired_rate,
+       double                                  start
+);
+
+
+
+/*  ------------    bit counting    --------------------------------------------------------  */
+
+
+
+static int                             count_bits (void);
+
+static int                             count_bits_short (void);
+
+static int                             count_bits_long (void);
+
+static void                    choose_table_long
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                max,
+       unsigned                                *table,
+       unsigned                                *bit_sum
+);
+
+static void                    choose_table_short
+(
+       unsigned                                start_sfb,
+       unsigned                                end_sfb,
+       unsigned                                max,
+       unsigned                                *table,
+       unsigned                                *bit_sum
+);
+
+
+
+/*  ------------    Huffmania    -----------------------------------------------------------  */
+
+
+
+static void                    single_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0, == 1 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    double_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 2, 5, 13 */
+       unsigned                                table1,   /* 3, 6, 15 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    triple_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 7, 10 */
+       unsigned                                table1,   /* 8, 11 */
+       unsigned                                table2,   /* 9, 12 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    triple_Huffman_2
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0,   == 13 */
+/*     unsigned                                table1,   == 15 */
+/*     unsigned                                table2,   == 24 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    double_Huffman_2   /* Escape tables */
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 16... */
+       unsigned                                table1,   /* 24... */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+
+
+/*  ------------    the struggle continues    ----------------------------------------------  */
+
+
+
+static void                    tiny_single_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+#if ORG_HUFFMAN_CODING
+       unsigned                                table0,
+#else
+/*     unsigned                                table0, == 1 */
+#endif
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    tiny_double_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 2, 5, 13 */
+       unsigned                                table1,   /* 3, 6, 15 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    tiny_triple_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 7, 10 */
+       unsigned                                table1,   /* 8, 11 */
+       unsigned                                table2,   /* 9, 12 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    tiny_triple_Huffman_2
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0,   == 13 */
+/*     unsigned                                table1,   == 15 */
+/*     unsigned                                table2,   == 24 */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+static void                    tiny_double_Huffman_2   /* Escape tables */
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 16... */
+       unsigned                                table1,   /* 24... */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+
+
+
+
+
+#endif         /* __LOOP_PVT__ */
diff --git a/lib/bladeenc/loop.c b/lib/bladeenc/loop.c
new file mode 100644 (file)
index 0000000..a65508b
--- /dev/null
@@ -0,0 +1,2590 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-11-10  Andre Piotrowski
+
+       -       reformatted, used 'static' functions, global variables, less parameters
+
+       2000-11-11  ap
+
+       -       a lot of functions redesigned to let the bladetable become superfluous:
+                       genNoisePowTab(), iteration_loop(), calc_noise(), preemphasis(), amp_scalefac_bands()
+       -       bladTabValue() replaced by cutting_crew()
+
+       2000-11-22  ap
+
+       -       bug fix:  module - reset fInit_huffman_read_flag
+
+       2000-11-28  ap
+       -       speed up        :  implemented partial quantizing
+
+       2000-11-30  ap
+       -       speed up        :  implemented faster Huffman coding
+       -       integration     :  improved (optional) Huffman coding
+                                                       [choosing better tables]
+       -       integration     :  improved (optional) binary search
+                                                       [in fact, this is a BUG FIX (original dist10 bug)]
+
+       2000-12-02  ap
+       -       bug fix         :  original dist10's outer_loop could cause an endless loop (huff_bits <= 0).
+       -       speed up        :  faster part2_length/scale_bitcount calculation
+       -       integration     :  improved (optional) scalefactor compression
+                                                       [smart preflag switching and choosing best compression]
+
+       2000-12-03  ap
+       -       integration     :  improved (optional) quantanf algorithm
+                                                       [calculating exact quantizer step boundaries]
+       -       integration     :  improved (optional) preemphasing/amplifying algorithm
+                                                       [preemphase only if iteration==1, according to ISO]
+       -       integration     :       improved (optional) outer_loop algorithm
+                                                       [amplify the bands, marked as "should be amplified"]
+
+       2000-12-10  ap
+       -       definitely killed SCFSI
+
+       2001-01-12  ap
+
+       -       use some explicit type casting to avoid compiler warnings
+       -       clear some backward compatability flags for 0.93.10
+
+       2001-04-07  ap
+       -       implemented flag CHECK_TJ_OVERFLOW to check for huffman table overflow
+*/
+
+
+
+
+
+/*  ========================================================================================  */
+/*              keeping backward compatability                                                */
+/*  ========================================================================================  */
+
+#define                ORG_HUFFMAN_CODING              0   /* 0 = use better Huffman tables for shorter code */
+#define                ORG_BINARY_SEARCH               0   /* 0 = use a correct implemented binary search */
+#define                ORG_QUANTANF_INIT               0   /* 0 = use better quantization start value */
+#define                ORG_PREEMPHASING                0   /* 0 = use a more ISO-like preemphasing algorithm */
+#define                ORG_SCF_COMPRESS                0   /* 0 = choose better scalefactor compression tables and smart preemphasing */
+#define                ORG_OUTER_LOOP                  0   /* 0 = differ between marked as "been amplified" and "should be amplified" */
+#define                ORG_HIGHEST_SFB                 1   /* 0 = cut off highest frequencies (in last scale factor band) */
+
+#define                CHECK_TJ_OVERFLOW               1   /* 1 = check for huffman table overflow */
+
+
+
+
+
+#define                infinity                                99999999
+
+
+
+
+
+#include       <stdio.h>
+#include       <stdlib.h>
+#include       <math.h>
+#include       <assert.h>
+
+#include       "system.h"
+#include       "common.h"
+
+#include       "l3side.h"
+#include       "l3psy.h"
+#include       "huffman.h"
+#include       "l3bitstream.h"
+#include       "reservoir.h"
+#include       "loop.h"
+#include       "loop-pvt.h"
+
+
+
+
+
+#if ORG_HUFFMAN_CODING
+static void                    tiny_single_Huffman_2   /* Escape tables */
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 15... */
+       unsigned                                *choice,
+       unsigned                                *sum
+);
+#endif
+
+
+
+static int                             amplify_short (void);
+static int                             amplify_long
+(
+       int                                             iteration
+);
+
+
+
+
+
+int                                            my_nint (double in)
+{
+
+       if (in < 0)
+               return (int)(in - 0.5);
+       else
+               return (int)(in + 0.5);
+}
+
+
+
+
+
+/*
+       Here are MPEG1 Table B.8 and MPEG2 Table B.1
+       -- Layer III scalefactor bands.
+       Index into this using a method such as:
+       idx  = fr_ps->header->sampling_frequency
+              + (fr_ps->header->version * 3)
+*/
+
+struct scalefac_struct sfBandIndex[3] =
+{
+       { /* Table B.8.b: 44.1 kHz */
+               {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
+               {0,4,8,12,16,22,30,40,52,66,84,106,136,192}
+       },
+       { /* Table B.8.c: 48 kHz */
+               {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
+               {0,4,8,12,16,22,28,38,50,64,80,100,126,192}
+       },
+       { /* Table B.8.a: 32 kHz */
+               {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
+               {0,4,8,12,16,22,30,42,58,78,104,138,180,192}
+       }
+};
+
+
+
+/*
+       The following table is used to implement the scalefactor
+       partitioning for MPEG2 as described in section
+       2.4.3.2 of the IS. The indexing corresponds to the
+       way the tables are presented in the IS:
+
+       [table_number][row_in_table][column of nr_of_sfb]
+*/
+
+static unsigned nr_of_sfb_block[6][3][4] =
+{
+       {
+               { 6, 5, 5, 5},
+               { 9, 9, 9, 9},
+               { 6, 9, 9, 9}
+       },
+       {
+               {6, 5,  7, 3},
+               {9, 9, 12, 6},
+               {6, 9, 12, 6}
+       },
+       {
+               {11,10, 0, 0},
+               {18,18, 0, 0},
+               {15,18, 0, 0}
+       },
+       {
+               { 7, 7, 7, 0},
+               {12,12,12, 0},
+               { 6,15,12, 0}
+       },
+       {
+               { 6, 6, 6, 3},
+               {12, 9, 9, 6},
+               { 6,12, 9, 6}
+       },
+       {
+               { 8, 8, 5, 0},
+               {15,12, 9, 0},
+               { 6,18, 9, 0}
+       }
+};
+
+
+
+/* Table B.6: layer3 preemphasis */
+int  pretab[21] =
+{
+       0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 1, 1, 1, 
+       1, 2, 2, 3, 3, 3, 2
+};
+
+
+
+/* This is the scfsi_band table from 2.4.2.7 of the IS */
+int scfsi_band_long[5] = { 0, 6, 11, 16, 21 };
+
+
+
+int                                            *scalefac_band_long;
+int                                            *scalefac_band_short;
+
+int                                            fInit_iteration_loop;
+
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
+int                                            tjBitOverflow2;
+#endif
+
+
+/* We need this initialization here since some compilers chokes on the
+   the declaration if it constains an initialization that point directly
+   into a struct.
+*/
+
+void fixStatic_loop( void )
+{
+       scalefac_band_long  = &sfBandIndex[0].l[0];
+       scalefac_band_short = &sfBandIndex[0].s[0];
+}
+
+
+
+
+
+/*  ========================================================================  */
+/*              generating the power tables                                   */
+/*  ========================================================================  */
+
+
+
+#define        BOT                                             200
+#define POW216_MAX                             (BOT/2 * 16)
+#define POW216_MIN                             (-25 * 16)
+
+
+
+static double                                  pow216_space[POW216_MAX-POW216_MIN+1];
+static double                                  *pow216 = pow216_space - POW216_MIN;
+static double                                  noisePowTab[8191+15];
+
+
+
+void genNoisePowTab (void)
+{
+       int     i;
+
+       for (i = POW216_MIN;  i <= POW216_MAX;  i++)
+               pow216[i] = pow (2.0, (double)i/16.0);
+
+       for (i = 0;  i < 8191+15;  i++)
+               noisePowTab[i] = pow (i, 4.0/3.0);
+}
+
+
+
+
+
+/*  ========================================================================  */
+/*              static variables                                              */
+/*  ========================================================================  */
+
+
+
+static int                                             gr;   /* the current granule */
+static int                                             ch;   /* the current channel */
+
+
+
+static III_side_info_t                 *side_info;   /* the current side information */
+static gr_info                                 *cod_info;    /* the current coding information */
+
+
+
+static double                     *xr_org_l;             /* the initial magnitudes of the spectral values */
+static double                            xr34_l[576];        /* the magnitudes powered by 3/4 */
+static int                                        *ix_l;             /* quantized values */
+
+static double                          energy_l[SFB_LMAX];
+static double                            xmin_l[SFB_LMAX];   /* the allowed distortion of the scalefactor band */
+static double                            xfsf_l[SFB_LMAX];   /* the current distortion of the scalefactor band */
+static int                                     expo16_l[SFB_LMAX];   /* sixteen times the scale factor band exponent */
+static int                              *scalefac_l;             /* the current scale factors */
+static int                        *scalefac_0_l;             /* scale factors for first granule */
+
+static double                    (*xr_org_s)[3];         /* some short block versions */
+static double                          (*xr34_s)[3] = (double (*)[3]) xr34_l;
+static int                                       (*ix_s)[3];
+
+static double                          energy_s[SFB_SMAX][3];
+static double                            xmin_s[SFB_SMAX][3];
+static double                            xfsf_s[SFB_SMAX][3];
+static int                                     expo16_s[SFB_SMAX][3];
+static int                             (*scalefac_s)[3];
+
+
+
+static int                                             max_used_sfb_l;
+static int                                             min_used_sfb_s;
+
+static int                                             end_sfb_l;
+static int                                             end_sfb_s;
+
+
+
+static double                              xmax_l[SFB_LMAX];           /* The initial (absolute) maximum magnitude */
+static int                                xmax_line_l[SFB_LMAX];               /* of the long bands and their line indices */
+
+static double                              xmax_s[SFB_SMAX][3];        /* Guess ... */
+static int                                xmax_line_s[SFB_SMAX][3];
+
+
+
+static int                                             mark_idx_l;                             /* speed up - partial quantizing */
+static int                                             mark_tab_l[SFB_LMAX];   /* changed sfb-s                 */ 
+
+static int                                             mark_idx_s;
+static int                                             mark_tab_s[SFB_SMAX*3*2];       /* changed (sfb,b)-s         */
+
+
+
+#if !ORG_QUANTANF_INIT
+
+static int                                             lo_quant_l [SFB_LMAX];
+static int                                             hi_quant_l [SBMAX_l];
+
+static int                                             lo_quant_s [SFB_SMAX][3];
+static int                                             hi_quant_s [SFB_SMAX][3];
+
+static int                                             the_lo_quant;
+static int                                             the_hi_quant;
+
+static double                                  log_2, cc, dd;
+
+#endif
+
+
+
+
+
+/*  ========================================================================  */
+/*              iteration_loop                                                */
+/*  ========================================================================  */
+
+void                                   iteration_loop
+(
+       double                                  pe[][2],
+       double                                  xr_org[2][2][576],
+       III_psy_ratio                   *ratio,
+       III_side_info_t                 *l3_side,
+       int                                             l3_enc[2][2][576],
+       int                                             mean_bits,
+       int                                             stereo,
+       double                                  xr_dec[2][2][576],
+       III_scalefac_t                  *scalefac,
+       frame_params                    *fr_ps,
+       int                                             ancillary_pad,
+       int                                             bitsPerFrame
+)
+{
+       int                                             max_bits;
+       int                                             i, sfb, b, scfsi_band;
+       int                                             mode_gr;
+
+       int                                             *main_data_begin;
+       layer                                   *info;
+
+       int                                             start, end;
+
+#if ORG_QUANTANF_INIT
+       double                                  log_sum;
+#endif
+       double                                  total_energy, temp, x;
+
+
+
+       side_info = l3_side;
+
+       main_data_begin = &side_info->main_data_begin;
+       info = fr_ps->header;
+
+
+       side_info->resvDrain = 0;
+
+       if (!fInit_iteration_loop)
+       {
+               *main_data_begin = 0;
+               fInit_iteration_loop = 1;
+
+#if !ORG_QUANTANF_INIT
+               log_2 = log(2.0);
+               cc = 4.0/3.0 * log(8205.0 - 0.5 + 0.0946) / log_2;
+               dd = 4.0/3.0 * log(   1.0 - 0.5 + 0.0946) / log_2;
+#endif
+       }
+       mode_gr = 2;
+
+       scalefac_band_long  = &sfBandIndex[info->sampling_frequency].l[0];
+       scalefac_band_short = &sfBandIndex[info->sampling_frequency].s[0];
+
+
+       ResvFrameBegin (fr_ps, side_info, mean_bits, bitsPerFrame);
+
+       for (gr = 0;  gr < mode_gr;  gr++)
+       {
+               for (ch = 0;  ch < stereo;  ch++)
+               {
+                       xr_org_l = xr_org[gr][ch];
+                       xr_org_s = (double (*)[3]) xr_org_l;
+
+                       ix_l = l3_enc[gr][ch];
+                       ix_s = (int (*)[3]) ix_l;
+
+                       cod_info = &side_info->gr[gr].ch[ch].tt;
+
+                       scalefac_l = scalefac->l[gr][ch];  scalefac_0_l = scalefac->l[0][ch];
+                       scalefac_s = scalefac->s[gr][ch];
+
+
+
+                       /* reset of iteration variables */
+
+                       for (scfsi_band = 0;  scfsi_band < 4;  scfsi_band++)
+                               cod_info->slen[scfsi_band] = 0;
+
+                       cod_info->sfb_partition_table = &nr_of_sfb_block[0][0][0];
+                       cod_info->part2_3_length      = 0;
+                       cod_info->big_values          = 0;
+                       cod_info->count1              = 0;
+                       cod_info->scalefac_compress   = 0;
+                       cod_info->table_select[0]     = 0;
+                       cod_info->table_select[1]     = 0;
+                       cod_info->table_select[2]     = 0;
+                       cod_info->subblock_gain[0]    = 0;
+                       cod_info->subblock_gain[1]    = 0;
+                       cod_info->subblock_gain[2]    = 0;
+                       cod_info->region0_count       = 0;
+                       cod_info->region1_count       = 0;
+                       cod_info->part2_length        = 0;
+                       cod_info->preflag             = 0;
+                       cod_info->scalefac_scale      = 0;
+                       cod_info->quantizerStepSize   = 0.0;
+                       cod_info->count1table_select  = 0;
+
+
+
+/*  ========    gr_deco    ========  */
+
+       if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
+       {
+               if (cod_info->mixed_block_flag)
+               {
+                       /*
+                               In mixed blocks there come first 8 long scale factor band areas covering
+                               the place normally used by the first 3 short scale factor band areas.
+                       */
+                       max_used_sfb_l = cod_info->sfb_lmax = 8;
+                       min_used_sfb_s = cod_info->sfb_smax = 3;
+
+                       /* The following values don«t need to be set again and again ... */
+                       cod_info->region0_count =  7;   /* scalefac_band_long[7+1   ] =  36               */
+                       cod_info->region1_count = 13;   /* scalefac_band_long[7+13+2] = 576  (no region2) */
+               }
+               else
+               {
+                       max_used_sfb_l = cod_info->sfb_lmax = 0;  /* No long blocks */
+                       min_used_sfb_s = cod_info->sfb_smax = 0;
+
+                       /* The following values don«t need to be set again and again ... */
+                       cod_info->region0_count =  8;   /*                   scalefac_band_short[(8+1   )/3] =  12  ( 12*3 =  36) */
+                       cod_info->region1_count = 36;   /* 36? should be 29: scalefac_band_short[(8+29+2)/3] = 192  (192*3 = 576) */
+                                                                                   /* probably meant  : scalefac_band_short[36/3 + 1  ] = 192  (192*3 = 576) */
+                                                   /* 2000-02-27 AP      no effect on output because block_type != NORM_TYPE */
+               }
+
+               /* to access the entire array we need the last scalefac_band_short area */
+               end_sfb_l = max_used_sfb_l; /*cod_info->sfb_lmax;*/
+               end_sfb_s = SFB_SMAX;
+
+               /* The following values don«t need to be set again and again ... */
+               cod_info->count1     =   0;         /* (zero_region-bigv_region) / 4; */
+               cod_info->big_values = 288;         /*              bigv_region  / 2; */
+
+               cod_info->count1table_select = 1;   /* sum0 == sum1 == 0 */
+
+               cod_info->address1 =  36;           /* choose one of the region0_count formulas above */
+               cod_info->address2 = 576;           /* bigv_region; */
+               cod_info->address3 =   0;
+       }
+       else
+       {
+               max_used_sfb_l = cod_info->sfb_lmax = SBMAX_l;
+               min_used_sfb_s = cod_info->sfb_smax = SBMAX_s;  /* No short blocks */
+
+               /* to access the entire array we need the last scalefac_band_long area */
+               end_sfb_l = SFB_LMAX;
+               end_sfb_s = min_used_sfb_s; /*cod_info->sfb_smax;*/
+       }
+
+
+                       /* reset of iteration variables */
+
+                       for (sfb = 0;  sfb < max_used_sfb_l/*SFB_LMAX-1*/;  sfb++)
+                               scalefac_l[sfb] = 0;
+                       for (sfb = min_used_sfb_s/*0*/;  sfb < SFB_SMAX-1;  sfb++)
+                               for (b = 0;  b < 3;  b++)
+                                       scalefac_s[sfb][b] = 0;
+
+/*  ========    calc_xmin and start of quantanf_init    ========  */
+/*
+       Calculate the allowed distortion for each scalefactor band,
+       as determined by the psychoacoustic model.
+       xmin(sb) = ratio(sb) * energy(sb) / bandwidth(sb)
+*/
+
+#if ORG_QUANTANF_INIT
+       log_sum = 0.0;
+#endif
+       total_energy = 0.0;
+
+       for (sfb = 0;  sfb < end_sfb_l;  sfb++)
+       {
+               start = scalefac_band_long[sfb];
+               end   = scalefac_band_long[sfb+1];
+
+               expo16_l[sfb] = 0;
+
+               xmax_l[sfb] = 0.0;
+               xmax_line_l[sfb] = start;
+
+               temp = 0.0;
+#if !ORG_HIGHEST_SFB
+               if (sfb < max_used_sfb_l)
+               {
+#endif
+                       for (i = start;  i < end;  i++)
+                       {
+                               if ((x = fabs(xr_org_l[i])) != 0.0)
+                               {
+                                       xr34_l[i] = sqrt(x * sqrt(x));
+                                       temp += x*x;
+#      if ORG_QUANTANF_INIT
+                                       log_sum += log(x);
+#      endif
+                                       if (x > xmax_l[sfb])
+                                       {
+                                               xmax_l[sfb] = x;
+                                               xmax_line_l[sfb] = i;
+                                       }
+                               }
+                               else
+                                       xr34_l[i] = 0.0;
+                       }
+#if !ORG_HIGHEST_SFB
+               }
+               else   /* cut off the (highest frequency) entries in the unused scale factor band */
+               {
+                       for (i = start;  i < end;  i++)
+                               xr34_l[i] = 0.0;
+               }
+#endif
+               total_energy += energy_l[sfb] = temp;
+
+               if (sfb < max_used_sfb_l)
+                       xmin_l[sfb] = ratio->l[gr][ch][sfb] * temp;
+       }
+
+       for (sfb = min_used_sfb_s;  sfb < end_sfb_s;  sfb++)
+       {
+               start = scalefac_band_short[sfb];
+               end   = scalefac_band_short[sfb+1];
+
+               for (b = 0;  b < 3;  b++)
+               {
+                       expo16_s[sfb][b] = 0;
+
+                       xmax_s[sfb][b] = 0.0;
+                       xmax_line_s[sfb][b] = start;
+
+                       temp = 0.0;
+#if !ORG_HIGHEST_SFB
+                       if (sfb < SBMAX_s)
+                       {
+#endif
+                               for (i = start;  i < end;  i++)
+                               {
+                                       if ((x = fabs(xr_org_s[i][b])) != 0.0)
+                                       {
+                                               xr34_s[i][b] = sqrt(x * sqrt(x));
+                                               temp += x*x;
+#if ORG_QUANTANF_INIT
+                                               log_sum += log(x);
+#endif
+                                               if (x > xmax_s[sfb][b])
+                                               {
+                                                       xmax_s[sfb][b] = x;
+                                                       xmax_line_s[sfb][b] = i;
+                                               }
+                                       }
+                                       else
+                                               xr34_s[i][b] = 0.0;
+                               }
+#if !ORG_HIGHEST_SFB
+                       }
+                       else   /* cut off the (highest frequency) entries in the unused scale factor band */
+                       {
+                               for (i = start;  i < end;  i++)
+                                       xr34_s[i][b] = 0.0;
+                       }
+#endif
+                       total_energy += energy_s[sfb][b] = temp;
+
+                       if (sfb < SFB_SMAX-1)
+                               xmin_s[sfb][b] = ratio->s[gr][ch][sfb][b] * temp;
+               }
+       }
+
+
+/*  ========    calc_scfsi    ========  */
+
+       /* None of the granules contains short blocks */
+       if (!cod_info->window_switching_flag || (cod_info->block_type != SHORT_TYPE))
+       {
+               if (gr == 1)
+               {
+                       for (scfsi_band = 0;  scfsi_band < 4;  scfsi_band++)
+                               side_info->scfsi[ch][scfsi_band] = 0;
+               }
+       }
+
+
+
+                       /* calculation of number of available bit( per granule ) */
+                       max_bits = ResvMaxBits (fr_ps, side_info, &pe[gr][ch], mean_bits);
+
+
+
+                       /* all spectral values zero ? */
+                       if (total_energy != 0.0)
+                       {
+
+
+
+#if ORG_QUANTANF_INIT
+
+/*  ========    quantanf_init (remaining)    ========  */
+
+#define system_const                      8.0
+#define minlimit                               -100.0
+
+       temp = my_nint (system_const * (log_sum/288.0 - log(total_energy/576.0)));
+       if (temp < minlimit)
+               temp = minlimit;
+       /*
+               SS 19-12-96. Starting value of
+               global_gain or quantizerStepSize 
+               has to be reduced for iteration_loop
+       */
+       temp -= 70.0;
+
+                               cod_info->quantizerStepSize = temp;
+
+#else   /* ORG_QUANTANF_INIT */
+
+       double                                  xmax, the_xmax;
+
+       the_lo_quant = -infinity;   /* "-infinity" */
+       the_hi_quant = -infinity;   /* the real maximum for high_quant is about +4 ! */
+
+       the_xmax = -1.0;
+
+       for (sfb = 0;  sfb < end_sfb_l;  sfb++)
+       {
+               xmax = xmax_l[sfb];
+               if (xmax == 0.0)
+               {
+                       lo_quant_l[sfb] = -infinity;
+                       hi_quant_l[sfb] = -infinity;
+               }
+               else
+               {
+                       lo_quant_l[sfb] = floor (4.0 * (log(xmax)/log_2 - cc)) + 1;
+                       hi_quant_l[sfb] = floor (4.0 * (log(xmax)/log_2 - dd)) + 1;
+
+                       if (xmax > the_xmax)
+                       {
+                               the_xmax = xmax;
+                               the_lo_quant = lo_quant_l[sfb];
+                               the_hi_quant = hi_quant_l[sfb];
+                       }
+               }
+       }
+
+       for (sfb = min_used_sfb_s;  sfb < end_sfb_s;  sfb++)
+       {
+               for (b = 0;  b < 3;  b++)
+               {
+                       xmax = xmax_s[sfb][b];
+                       if (xmax == 0.0)
+                       {
+                               lo_quant_s[sfb][b] = -infinity;
+                               hi_quant_s[sfb][b] = -infinity;
+                       }
+                       else
+                       {
+                               lo_quant_s[sfb][b] = floor (4.0 * (log(xmax)/log_2 - cc) /* - 8 * cod_info->subblock_gain[b] */) + 1;
+                               hi_quant_s[sfb][b] = floor (4.0 * (log(xmax)/log_2 - dd) /* - 8 * cod_info->subblock_gain[b] */) + 1;
+
+                               if (xmax > the_xmax)
+                               {
+                                       the_xmax = xmax;
+                                       the_lo_quant = lo_quant_s[sfb][b];
+                                       the_hi_quant = hi_quant_s[sfb][b];
+                               }
+                       }
+               }               
+       }
+
+
+       /*
+               Try the power table at its least boundary
+               I«ve never reached this deep before!
+       */
+       assert (the_lo_quant > -POW216_MAX);
+
+       cod_info->quantizerStepSize = the_lo_quant;
+
+#endif   /* ORG_QUANTANF_INIT */
+
+
+
+                               cod_info->part2_3_length = outer_loop (max_bits, fr_ps);
+                       }
+
+                       ResvAdjust (fr_ps, cod_info, side_info, mean_bits);
+
+                       cod_info->global_gain = my_nint (cod_info->quantizerStepSize + 210.0);
+/*                     assert (cod_info->global_gain < 256); */
+               }       /* for ch */
+       }       /* for gr */
+
+       ResvFrameEnd (fr_ps, side_info, mean_bits);
+}
+
+
+
+
+
+/*  ========================================================================  */
+/*              outer_loop                                                    */
+/*  ========================================================================  */
+/*
+       The outer iteration loop controls the masking conditions
+       of all scalefactorbands. It computes the best scalefac and
+       global gain. This module calls the inner iteration loop
+*/
+
+static int                             outer_loop
+(
+       int                                             max_bits,
+       frame_params                    *fr_ps
+)
+{
+       int                                             scalesave_l[SFB_LMAX-1];
+       int                                             scalesave_s[SFB_SMAX-1][3];
+       int                                             bits, huff_bits, save_preflag, save_compress, save_part2_length;
+       int                                             sfb, b, over, iteration;
+
+
+       /* reset the pointers of our changed sfb [(sfb,b)] indices list */
+       mark_idx_l = mark_idx_s = 0;
+
+
+#if 0
+       cod_info->preflag           = 0;   /* assignments are all done in iteration_loop()                      */
+       cod_info->scalefac_compress = 0;   /* just to show what«s going on ...                                  */
+       cod_info->part2_length      = 0;   /* == part2_length(fr_ps) because of slen1_tab[0] = slen2_tab[0] = 0 */
+#endif
+
+       huff_bits = max_bits /* - cod_info->part2_length */;   /* try first without scaling */
+
+
+       iteration = 1;
+
+       bits = bin_search_StepSize (max_bits, cod_info->quantizerStepSize);  /* speeds things up a bit */
+
+       while (1)
+       {
+               for (sfb = 0;  sfb < SFB_LMAX-1;  sfb++)  /* save scaling factors */
+                       scalesave_l[sfb] = scalefac_l[sfb];
+
+               for (sfb = 0;  sfb < SFB_SMAX-1;  sfb++)
+                       for (b = 0;  b < 3;  b++)
+                               scalesave_s[sfb][b] = scalefac_s[sfb][b];
+
+               save_preflag      = cod_info->preflag;
+               save_compress     = cod_info->scalefac_compress;
+               save_part2_length = cod_info->part2_length;
+
+               calc_noise ();  /* distortion calculation */
+
+               over = amplify (iteration);
+
+#if ORG_OUTER_LOOP
+/*
+       misplaced break condition in original dist10:
+       -       There is one flag only for both, marking a scalefactor band as
+               "should be amplified" or marking it as "is amplified", namely
+               a corresponding scalefactor greater than zero!
+       -       The desired amplification of the scalefactors bands marked as
+               "should be amplified" is actually not done before the next call
+               to quantize(), respectively partial_quantize().
+       -       Thus, it can happen that all scalefactor bands are marked, but
+               not all of the marked bands are amplified.
+       -       Since loop_break() doesn't know that, the outer loop frequently
+               gets terminated to early.
+*/
+               if (loop_break ())
+                       break;
+#endif
+
+               huff_bits = max_bits - needed_bits_for_storing_scalefactors (fr_ps);
+               if (huff_bits < 0)
+                       break;   /* not enough space to store the scale factors */
+
+
+               /* We have to wait checking this break condition */
+               /* until needed_bits_for_storing_scalefactors()  */
+               /* set the scalefac compress index!!!            */
+               if (over == 0)
+                       break;   /* no more bands to amplify */
+
+
+               iteration++;
+
+               /*
+                       Most of the times, only a few bands will be changed,
+                       so why quantize the whole area?
+               */
+               partial_quantize ();
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
+assert (!tjBitOverflow2);
+#endif
+               bits = count_bits();
+
+               while (bits > huff_bits)
+               {
+                       cod_info->quantizerStepSize += 1.0;
+                       quantize ();
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
+assert (!tjBitOverflow2);
+#endif
+                       bits = count_bits ();
+               }
+
+#if !ORG_OUTER_LOOP
+               /*
+                       A break would mean to restore the parameters of the last iteration,
+                       but we like to accept the current state. If you want to avoid the
+                       'goto', you have to to take the long way home and place the loop
+                       break condition in front of the call to calc_noise().
+               */
+               if (loop_break())
+                       goto take_that_and_party;
+#endif
+       }
+
+
+       cod_info->preflag           = save_preflag;
+       cod_info->scalefac_compress = save_compress;
+       cod_info->part2_length      = save_part2_length;
+
+       for (sfb = 0;  sfb < SFB_LMAX-1;  sfb++)
+               scalefac_l[sfb] = scalesave_l[sfb];    
+
+       for (sfb = 0;  sfb < SFB_SMAX-1;  sfb++)
+               for (b = 0;  b  < 3;  b++)
+                       scalefac_s[sfb][b] = scalesave_s[sfb][b];
+
+take_that_and_party:
+       cod_info->part2_3_length    = cod_info->part2_length + bits;
+
+
+       return cod_info->part2_3_length;
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*      needed_bits_for_storing_scalefactors                                                  */
+/*  ========================================================================================  */
+/*
+       counts the bits needed to code the scale factors (cod_info->part2_length)
+       and the compression index (cod_info->scalefac_compress).
+
+       If there is no suitable index, it returns "infinity".
+*/
+
+static int                             needed_bits_for_storing_scalefactors
+(
+       frame_params                    *fr_ps
+)
+{
+#if 0
+       static  int                                   slen1[16] = {  0,  0,  0,  0,  3,  1,  1,  1,  2,  2,  2,  3,  3,  3,  4,  4 };
+       static  int                                   slen2[16] = {  0,  1,  2,  3,  0,  1,  2,  3,  1,  2,  3,  1,  2,  3,  2,  3 };
+#endif
+
+       /* 2^^slen1[k] */
+       static  int                              pow2_slen1[16] = {  1,  1,  1,  1,  8,  2,  2,  2,  4,  4,  4,  8,  8,  8, 16, 16};
+       /* 2^^slen2[k] */
+       static  int                              pow2_slen2[16] = {  1,  2,  4,  8,  1,  2,  4,  8,  2,  4,  8,  2,  4,  8,  4,  8};
+
+       /* (8+9) * slen1[k] + (9+9) * slen2[k] */
+       static  int                             part2_len_m[16] = {  0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87,105,104,122};
+       /* (9+9) * slen1[k] + (9+9) * slen2[k] */
+       static  int                             part2_len_s[16] = {  0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90,108,108,126};
+       /* (6+5) * slen1[k] + (5+5) * slen2[k] */
+       static  int                             part2_len_l[16] = {  0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74};
+
+       int                                             sfb, b, k;
+       int                                             max_slen1, max_slen2, *table;
+
+
+       max_slen1 = max_slen2 = 0;
+
+       if (cod_info->window_switching_flag  &&  (cod_info->block_type == SHORT_TYPE))
+       {
+               if (cod_info->mixed_block_flag)
+               {
+                       table = part2_len_m;
+
+                       for (sfb = 0;  sfb < 8;  sfb++)
+                               if (scalefac_l[sfb] > max_slen1)
+                                       max_slen1 = scalefac_l[sfb];
+
+                       for (sfb = 3;  sfb < 6;  sfb++)
+                               for (b = 0;  b < 3;  b++)
+                                       if (scalefac_s[sfb][b] > max_slen1)
+                                               max_slen1 = scalefac_s[sfb][b];
+               }
+               else
+               {
+                       table = part2_len_s;
+
+                       for (sfb = 0;  sfb < 6;  sfb++)
+                               for (b = 0;  b < 3;  b++)
+                                       if (scalefac_s[sfb][b] > max_slen1)
+                                               max_slen1 = scalefac_s[sfb][b];
+               }
+
+               for (sfb = 6;  sfb < 12/*SBMAX_s*/;  sfb++)
+                       for (b = 0;  b < 3;  b++)
+                               if (scalefac_s[sfb][b] > max_slen2)
+                                       max_slen2 = scalefac_s[sfb][b];
+       }
+       else
+       {
+               table = part2_len_l;
+
+               for (sfb = 0;  sfb < 11;  sfb++)
+                       if (scalefac_l[sfb] > max_slen1)
+                               max_slen1 = scalefac_l[sfb];
+
+
+#if ORG_SCF_COMPRESS && !ORG_PREEMPHASING
+               /* This was seen in LAME */
+               if (!cod_info->preflag)
+               {
+                       for (sfb = 11;  sfb < SBMAX_l;  sfb++)
+                               if (scalefac_l[sfb] < (1 + cod_info->scalefac_scale) * pretab[sfb])
+                                       break;
+
+                       if (sfb == SBMAX_l)
+                       {
+                               for (sfb = 11;  sfb < SBMAX_l;  sfb++)
+                                       scalefac_l[sfb] -= (1 + cod_info->scalefac_scale) * pretab[sfb];
+                               cod_info->preflag = 1;
+                       }
+               }
+#endif
+
+
+               for (sfb = 11;  sfb < 21/*SBMAX_l*/;  sfb++)
+                       if (scalefac_l[sfb] > max_slen2)
+                               max_slen2 = scalefac_l[sfb];
+       }
+
+
+       cod_info->part2_length = infinity;
+
+       for (k = 0;  k < 16;  k++)
+       {
+               if (max_slen1 < pow2_slen1[k]  &&  max_slen2 < pow2_slen2[k])
+               {
+#if ORG_SCF_COMPRESS
+                       cod_info->scalefac_compress = k;
+                       cod_info->part2_length      = table[k];
+                       break;
+#else
+                       if (cod_info->part2_length > table[k])
+                       {
+                               cod_info->scalefac_compress = k;
+                               cod_info->part2_length      = table[k];
+                       }
+#endif
+               }
+       }
+
+
+       return cod_info->part2_length;
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*              calc_noise                                                                    */
+/*  ========================================================================================  */
+/*
+       calculates the distortion introduced by the qunatization
+       in each scale factor band.
+*/
+static void                    calc_noise (void)
+{
+       int                                             i, b, sfb, start, end, off;
+       double                                  f, sum, temp;
+
+
+       off = -4 * (int)cod_info->quantizerStepSize;
+
+
+       for (sfb = 0;  sfb < max_used_sfb_l;  sfb++)
+       {
+               if (ix_l[xmax_line_l[sfb]] == 0)   /* quantized values all zero? */
+               {
+                       xfsf_l[sfb] = energy_l[sfb];   /* see calculation of xmin_l */
+               }
+               else
+               {
+                       start = scalefac_band_long[sfb];
+                       end   = scalefac_band_long[sfb+1];
+
+                       sum = 0.0;
+
+                       f = pow216[expo16_l[sfb] + off];
+
+                       for (i = start;  i < end;  i++)
+                       {
+                               temp = fabs(xr_org_l[i]) - noisePowTab[ix_l[i]] / f;
+                               sum += temp * temp;
+                       }
+
+                       xfsf_l[sfb] = sum;
+               }
+       }
+
+       for (b = 0;  b < 3;  b++)
+       {
+               off = -4 * ((int)cod_info->quantizerStepSize + 8 * cod_info->subblock_gain[b]);
+
+               for (sfb = min_used_sfb_s;  sfb < SFB_SMAX-1;  sfb++)
+               {
+                       if (ix_s[xmax_line_s[sfb][b]] == 0)   /* quantized values all zero? */
+                       {
+                               xfsf_s[sfb][b] = energy_s[sfb][b];   /* see calculation of xmin_s */
+                       }
+                       else
+                       {
+                               start = scalefac_band_short[sfb];
+                               end   = scalefac_band_short[sfb+1];
+
+                               sum = 0.0;
+
+                               f = pow216[expo16_s[sfb][b] + off];
+
+                               for (i = start;  i < end;  i++)
+                               {
+                                       temp = fabs(xr_org_s[i][b]) - noisePowTab[ix_s[i][b]] / f;
+                                       sum += temp * temp;
+                               }       
+
+                               xfsf_s[sfb][b] = sum;
+                       }
+               }
+       }
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*              loop_break                                                                    */
+/*  ========================================================================================  */
+/*
+       returns zero if there is a scalefac which has not been amplified.
+       Otherwise it returns one. 
+*/
+
+static int                             loop_break (void)
+{
+       int                                             sfb, b;
+
+       for (sfb = 0;  sfb < cod_info->sfb_lmax;  sfb++)
+               if (scalefac_l[sfb] == 0)
+                       return 0;
+
+       for (sfb = min_used_sfb_s;  sfb < 12;  sfb++)
+               for (b = 0;  b < 3;  b++)
+                       if (scalefac_s[sfb][b] == 0)
+                               return 0;
+
+       return 1;
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*              preemphasing and amplifying                                                   */
+/*  ========================================================================================  */
+/*
+       Preemphasing: see ISO 11172-3  section  C.1.5.4.3.4
+       Amplifying  : see ISO 11172-3  section  C.1.5.4.3.5
+
+       amplifying the scalefactor bands that violate the masking threshold.
+*/
+
+static int                             amplify
+(
+       int                                             iteration
+)
+{
+       if (cod_info->window_switching_flag  &&  cod_info->block_type == SHORT_TYPE)
+               return amplify_short ();
+       else
+               return amplify_long (iteration);
+}
+
+
+
+
+
+static int                             amplify_short (void)
+{
+       int                                             sfb, b, over, expo16_off;
+
+       expo16_off = 16 * (1 + cod_info->scalefac_scale) / 2;
+       over = 0;
+
+#ifdef MIXED_BLOCKS
+       for (sfb = 0;  sfb < max_used_sfb_l;  sfb++)
+       {
+               if (xfsf_l[sfb] > xmin_l[sfb])
+               {
+                       scalefac_l[sfb]++;
+                       expo16_l[sfb] += expo16_off;
+                       over++;
+                       mark_tab_l[mark_idx_l++] = sfb;
+               }
+       }
+#endif
+
+       for (sfb = min_used_sfb_s;  sfb < SBMAX_s;  sfb++)
+       {
+               for (b = 0;  b < 3;  b++)
+               {
+                       if (xfsf_s[sfb][b] > xmin_s[sfb][b])
+                       {
+                               scalefac_s[sfb][b]++;
+                               expo16_s[sfb][b] += expo16_off;
+                               over++;
+                               mark_tab_s[mark_idx_s++] = sfb;
+                               mark_tab_s[mark_idx_s++] = b;
+                       }
+               }
+       }
+
+       return over;
+}
+
+
+
+
+
+static int                             amplify_long
+(
+       int                                             iteration
+)
+{
+       int                                             pre_expo_off[SFB_LMAX];
+
+       int                                             sfb, stop_at, over = 0;
+       int                                             expo16_off;
+
+
+       stop_at = max_used_sfb_l;
+
+       expo16_off = 16 * (1 + cod_info->scalefac_scale) / 2;
+
+
+       /*
+               Preemphasis is switched on if in all the upper four scalefactor
+               bands the actual distortion exceeds the threshold after the
+               first call of the inner loop.
+
+               Original bug of dist10 - preemphasis() didn't know 'iteration'!!!
+       */
+#if !ORG_PREEMPHASING
+       if (iteration == 1)
+#endif
+               if (!cod_info->preflag)
+               {       
+                       for (sfb = max_used_sfb_l-4;  sfb < max_used_sfb_l;  sfb++)
+                               if (xfsf_l[sfb] <= xmin_l[sfb])
+                                       goto no_preemphasing;
+
+                       cod_info->preflag = 1;
+
+                       stop_at = 11;   /* pretab[sfb] = 0  for  sfb = 0..10 */
+
+                       for (sfb = stop_at;  sfb < max_used_sfb_l;  sfb++)
+                       {
+                               expo16_l[sfb] += pre_expo_off[sfb] = expo16_off * pretab[sfb];
+
+                               mark_tab_l[mark_idx_l++] = sfb;
+                       }
+               }
+
+
+no_preemphasing:
+
+
+       for (sfb = 0;  sfb < stop_at;  sfb++)
+       {
+               if (xfsf_l[sfb] > xmin_l[sfb])
+               {
+                       over++;
+                       expo16_l[sfb] += expo16_off;
+                       scalefac_l[sfb]++;
+
+                       mark_tab_l[mark_idx_l++] = sfb;
+               }
+       }
+       for (sfb = stop_at;  sfb < max_used_sfb_l;  sfb++)   /* The just preemphased bands have to be treated differently */
+       {
+               if (xfsf_l[sfb] > xmin_l[sfb] * pow216[2*pre_expo_off[sfb]])
+               {
+                       over++;
+                       expo16_l[sfb] += expo16_off;
+                       scalefac_l[sfb]++;
+               }
+       }
+
+
+       return over;
+}
+
+
+
+
+
+/*  ========================================================================  */
+/*              quantize                                                      */
+/*  ========================================================================  */
+/*
+       Quantization of the vector xr ( -> ix)
+*/
+
+static int INLINE              cutting_crew (FLOAT in)
+{
+       int                                             retVal;
+
+       retVal = (int) (in + 0.4054);
+
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
+       if (retVal > 8191+14)
+               tjBitOverflow2 = TRUE;
+#endif
+
+       return retVal;
+}
+
+
+
+static void quantize (void)
+{
+       int                                             sfb, i, b, start, end;
+       double                                  f, z, y;
+
+       for (sfb = 0;  sfb < end_sfb_l;  sfb++)
+       {
+               start = scalefac_band_long[sfb];
+               end   = scalefac_band_long[sfb+1];
+
+                               /* (expo16_l[sfb] - 16/4 * quant_step) * 3/4 */
+                       f = pow216[(expo16_l[sfb]/4 - (int)cod_info->quantizerStepSize) * 3];
+
+                       for (i = start;  i < end;  i += 2)
+                       {
+                               z = xr34_l[i  ] * f;
+                               y = xr34_l[i+1] * f;
+                               ix_l[i  ] = cutting_crew (z);
+                               ix_l[i+1] = cutting_crew (y);
+                       }
+       }
+
+       for (sfb = min_used_sfb_s;  sfb < end_sfb_s;  sfb++)
+       {
+               start = scalefac_band_short[sfb];
+               end   = scalefac_band_short[sfb+1];
+
+               for (b = 0;  b < 3;  b++)
+               {
+                                       /* (expo_s[sfb][b] - 16/4 * (quant_step + 8 * cod_info->subblock_gain[b])) * 3/4 */
+                               f = pow216[(expo16_s[sfb][b] / 4 - (int)cod_info->quantizerStepSize - 8 * cod_info->subblock_gain[b]) * 3];
+
+                               for (i = start;  i < end;  i += 2)
+                               {
+                                       z = xr34_s[i  ][b] * f;
+                                       y = xr34_s[i+1][b] * f;
+                                       ix_s[i  ][b] = cutting_crew (z);
+                                       ix_s[i+1][b] = cutting_crew (y);
+                               }
+               }
+       }
+}
+
+
+
+static void                    partial_quantize (void)
+{
+       int                                             sfb, i, b, start, end;
+       double                                  f, z, y;
+
+       while (mark_idx_l)
+       {
+               sfb = mark_tab_l[--mark_idx_l];
+
+               start = scalefac_band_long[sfb];
+               end   = scalefac_band_long[sfb+1];
+
+                       /* (expo16_l[sfb] - 16/4 * quant_step) * 3/4 */
+               f = pow216[(expo16_l[sfb]/4 - (int)cod_info->quantizerStepSize) * 3];
+
+               for (i = start;  i < end;  i += 2)
+               {
+                       z = xr34_l[i  ] * f;
+                       y = xr34_l[i+1] * f;
+                       ix_l[i  ] = cutting_crew (z);
+                       ix_l[i+1] = cutting_crew (y);
+               }
+       }
+
+       while (mark_idx_s)
+       {
+               b   = mark_tab_s[--mark_idx_s];
+               sfb = mark_tab_s[--mark_idx_s];
+
+               start = scalefac_band_short[sfb];
+               end   = scalefac_band_short[sfb+1];
+
+                       /* (expo_16s[sfb][b] - 16/4 * (quant_step + 8 * cod_info->subblock_gain[b])) * 3/4 */
+               f = pow216[(expo16_s[sfb][b] / 4 - (int)cod_info->quantizerStepSize - 8 * cod_info->subblock_gain[b]) * 3];
+
+               for (i = start;  i < end;  i += 2)
+               {
+                       z = xr34_s[i  ][b] * f;
+                       y = xr34_s[i+1][b] * f;
+                       ix_s[i  ][b] = cutting_crew (z);
+                       ix_s[i+1][b] = cutting_crew (y);
+               }
+       }
+}
+
+
+
+
+
+/*  ========================================================================  */
+/*              count_bits                                                    */
+/*  ========================================================================  */
+
+struct
+{
+       unsigned region0_count;
+       unsigned region1_count;
+} subdv_table[ 23 ] =
+{
+       {0, 0}, /*  0 bands */
+       {0, 0}, /*  1 bands */
+       {0, 0}, /*  2 bands */
+       {0, 0}, /*  3 bands */
+       {0, 0}, /*  4 bands */
+       {0, 1}, /*  5 bands */
+       {1, 1}, /*  6 bands */
+       {1, 1}, /*  7 bands */
+       {1, 2}, /*  8 bands */
+       {2, 2}, /*  9 bands */
+       {2, 3}, /* 10 bands */
+       {2, 3}, /* 11 bands */
+       {3, 4}, /* 12 bands */
+       {3, 4}, /* 13 bands */
+       {3, 4}, /* 14 bands */
+       {4, 5}, /* 15 bands */
+       {4, 5}, /* 16 bands */
+       {4, 6}, /* 17 bands */
+       {5, 6}, /* 18 bands */
+       {5, 6}, /* 19 bands */
+       {5, 7}, /* 20 bands */
+       {6, 7}, /* 21 bands */
+       {6, 7}, /* 22 bands */
+};
+
+
+
+/*
+       Calculation of rzero, count1, big_values
+       (Partitions ix into big values, quadruples and zeros).
+
+       Determines the number of bits to encode the quadruples.
+
+       Presumable subdivides the bigvalue region which will
+       use separate Huffman tables.
+
+       Select huffman code tables for bigvalues regions
+
+       Count the number of bits necessary to code the bigvalues region.
+*/
+
+static int                             count_bits (void)
+{
+       cod_info->table_select[0] = 0;
+       cod_info->table_select[1] = 0;
+       cod_info->table_select[2] = 0;
+
+       if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
+               return count_bits_short ();
+       else
+               return count_bits_long ();
+}
+
+
+
+
+
+static int                             count_bits_short (void)
+{
+       unsigned int                    bits = 0;
+
+       /*
+               Within each scalefactor band, data is given for successive
+               time windows, beginning with window 0 and ending with window 2.
+               Within each window, the quantized values are then arranged in
+               order of increasing frequency...
+       */
+       int                                             sfb, b;
+       unsigned int                    max, temp;
+
+
+       /*
+               the first part --- 8 long blocks or 3 short blocks
+       */
+
+       max = 0;
+#ifdef MIXED_BLOCKS
+       if (cod_info->mixed_block_flag)
+       {
+               for (sfb = 0;  sfb < 8;  sfb++)
+                       if ((temp = ix_l[xmax_line_l[sfb]]) > max)
+                               max = temp;
+               choose_table_long (0, 36, max, &cod_info->table_select[0], &bits);
+       }
+       else
+#endif
+       {
+               for (sfb = 0;  sfb < 3;  sfb++)
+                       for (b = 0;  b < 3;  b++)
+                               if ((temp = ix_s[xmax_line_s[sfb][b]][b]) > max)
+                                       max = temp;
+               choose_table_short (0, 3, max, &cod_info->table_select[0], &bits);
+       }
+
+
+       /*
+               the second part --- short blocks only
+       */
+
+       max = 0;
+       for (sfb = 3;  sfb < SFB_SMAX;  sfb++)
+               for (b = 0;  b < 3;  b++)
+                       if ((temp = ix_s[xmax_line_s[sfb][b]][b]) > max)
+                               max = temp;
+       choose_table_short (3, SFB_SMAX, max, &cod_info->table_select[1], &bits);
+
+       return bits;
+}
+
+
+
+
+
+static int                             count_bits_long (void)
+{
+       int                                             zero_region;
+       int                                             bigv_region;
+
+       unsigned                                bits = 0;
+       int                                             sum0 = 0;
+       int                                             sum1 = 0;
+
+       int                                             sfb_anz, index0, index1, sfb, i;
+       unsigned                                max, temp;
+
+       int                                             p;
+
+
+       for (zero_region = 576;  zero_region > 1;  zero_region -= 2)
+                    if (ix_l[zero_region-1])  break;
+               else if (ix_l[zero_region-2])  break;
+
+       for (bigv_region = zero_region;  bigv_region > 3;  bigv_region -= 4)
+       {
+                    if (ix_l[bigv_region-1] > 1)  break;
+               else if (ix_l[bigv_region-2] > 1)  break;
+               else if (ix_l[bigv_region-3] > 1)  break;
+               else if (ix_l[bigv_region-4] > 1)  break;
+
+               p = 0;
+               if (ix_l[bigv_region-1])  bits++, p |= 8;
+               if (ix_l[bigv_region-2])  bits++, p |= 4;
+               if (ix_l[bigv_region-3])  bits++, p |= 2;
+               if (ix_l[bigv_region-4])  bits++, p |= 1;
+
+               sum0 += ht[32].hlen[p];
+               sum1 += ht[33].hlen[p];
+       }
+
+       cod_info->count1     = (zero_region-bigv_region) / 4;
+       cod_info->big_values =              bigv_region  / 2;
+
+       if (sum0 < sum1)
+       {
+               bits += sum0;
+               cod_info->count1table_select = 0;
+       }
+       else
+       {
+               bits += sum1;
+               cod_info->count1table_select = 1;
+       }
+
+       if (bigv_region)
+       {
+               sfb_anz = 1;
+               while (scalefac_band_long[sfb_anz] < bigv_region)
+                       sfb_anz++;
+
+               if (cod_info->window_switching_flag)   /* START_TYPE, STOP_TYPE */
+               {
+                       index0 = (cod_info->region0_count =  7) + 1;
+                                 cod_info->region1_count = 13;
+                       index1 = sfb_anz - index0;  if (index0 + index1 < 22)  index1++;
+
+                       cod_info->address1 =  36;
+                       cod_info->address2 = bigv_region;
+                       cod_info->address3 =   0;
+               }
+               else   /* NORM_TYPE */  
+               {
+                       index0 = (cod_info->region0_count = subdv_table[sfb_anz].region0_count) + 1;
+                       index1 = (cod_info->region1_count = subdv_table[sfb_anz].region1_count) + 1;
+
+                       cod_info->address1 = scalefac_band_long[index0];
+                       cod_info->address2 = scalefac_band_long[index0 + index1];
+                       cod_info->address3 = bigv_region;
+               }
+
+               if (cod_info->address1 > 0)
+               {
+                       max = 0;
+                       for (sfb = 0;  sfb < index0;  sfb++)
+                               if ((temp = ix_l[xmax_line_l[sfb]]) > max)
+                                       max = temp;
+                       choose_table_long (0, cod_info->address1, max, &cod_info->table_select[0], &bits);
+               }
+
+               if (cod_info->address2 > cod_info->address1)
+               {
+                       max = 0;
+                       for (sfb = index0;  sfb < index0+index1;  sfb++)
+                               if ((temp = ix_l[xmax_line_l[sfb]]) > max)
+                                       max = temp;
+                       choose_table_long (cod_info->address1, cod_info->address2, max, &cod_info->table_select[1], &bits);
+               }
+
+               if (bigv_region > cod_info->address2)
+               {
+                       max = 0;
+                       for (sfb = index0+index1;  sfb < sfb_anz-1;  sfb++)
+                               if ((temp = ix_l[xmax_line_l[sfb]]) > max)
+                                       max = temp;
+                       for (i = scalefac_band_long[sfb_anz-1]; i < bigv_region;  i++)
+                               if ((temp = ix_l[i]) > max)
+                                       max = temp;
+                       choose_table_long (cod_info->address2, bigv_region, max, &cod_info->table_select[2], &bits);
+               }
+       }
+       else
+       {       /* no big_values region */
+               cod_info->region0_count = 0;
+               cod_info->region1_count = 0;
+
+               cod_info->address1 = 0;
+               cod_info->address2 = 0;
+               cod_info->address3 = 0;
+       }
+
+
+       return bits;
+}
+
+
+
+
+
+/*  ========================================================================  */
+/*              bin_search_step_size                                          */
+/*  ========================================================================  */
+/*
+       The following optional code written by Seymour Shlien
+       will speed up the outer_loop code which is called
+       by iteration_loop. When BIN_SEARCH is defined, the
+       outer_loop function precedes the call to the function inner_loop
+       with a call to bin_search gain defined below, which
+       returns a good starting quantizerStepSize.
+
+       The function count_bits() [a sequence of statements, originally part of inner_loop()]
+       was completely rewritten.
+
+
+       changed the behaviour:
+       now, it returns the found number of bits <= desired_rate
+*/
+
+static int                             bin_search_StepSize
+(
+       int                                             desired_rate,
+       double                                  start
+)
+{
+       int                                             bits;
+
+       int                                             top = start;
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT
+       int                                             bot = 200;
+#else
+       int                                             bot = the_hi_quant;
+#endif
+       int                                             next = start;
+
+#if ORG_BINARY_SEARCH
+
+       int                                             last;
+
+       do
+       {
+               last = next;
+               next = (top + bot) / 2;
+               cod_info->quantizerStepSize = next;
+
+               tjBitOverflow2 = FALSE;
+               quantize ();
+               if (tjBitOverflow2)
+                       bits = infinity;
+               else
+                       bits = count_bits ();
+
+               if (bits > desired_rate) 
+                       top = next;
+               else 
+                       bot = next;
+       }
+       while ((bits != desired_rate)  &&  (abs(last-next) > 1));
+
+#else   /* ORG_BINARY_SEARCH */
+
+       do
+       {
+               next = top + (bot - top) / 2;
+               cod_info->quantizerStepSize = next;
+
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
+               tjBitOverflow2 = FALSE;
+               quantize ();
+               if (tjBitOverflow2)
+                       bits = infinity;
+               else
+                       bits = count_bits ();
+#else
+               quantize ();
+               bits = count_bits ();
+#endif
+
+               if (bits > desired_rate) 
+                       top = next + 1;
+               else 
+                       bot = next;
+       }
+       while (top < bot);
+
+#endif   /* ORG_BINARY_SEARCH */
+
+       if (bits > desired_rate)
+       {
+               cod_info->quantizerStepSize = next+1;
+#if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
+               tjBitOverflow2 = FALSE;
+               quantize ();
+assert(! tjBitOverflow2);
+#else
+               quantize ();
+#endif
+               bits = count_bits ();
+assert(bits <= desired_rate);
+       }
+
+       return bits;
+
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*              choose_table_long                                                             */
+/*  ========================================================================================  */
+/*
+       Choose the Huffman table that will encode ix[start..end] with the fewest
+       bits and increases the bit_sum by the amount of these bits.
+
+       Note: This code contains knowledge about the sizes and characteristics
+       of the Huffman tables as defined in the IS (Table B.7), and will not work
+       with any arbitrary tables.
+*/
+static void                    choose_table_long
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                max,
+       unsigned                                *table,
+       unsigned                                *bit_sum
+)
+{
+       unsigned                                choice0, choice1;
+
+
+       if (max == 0)
+       {
+               *table = 0;
+               return;
+       }
+
+       if (max < 15)
+       {
+               choice0 = 1;  /* we can start with 1 because ht[0].xlen == 0 <= max */
+               while (ht[choice0].xlen <= max)
+                       choice0++;
+
+               switch (choice0)
+               {
+                       case  1:  single_Huffman (start, end,/* 1 */      table, bit_sum);  break;
+                       case  2:  double_Huffman (start, end,  2,  3,     table, bit_sum);  break;
+                       case  5:  double_Huffman (start, end,  5,  6,     table, bit_sum);  break;
+                       case  7:  triple_Huffman (start, end,  7,  8,  9, table, bit_sum);  break;
+                       case 10:  triple_Huffman (start, end, 10, 11, 12, table, bit_sum);  break;
+                       case 13:  double_Huffman (start, end, 13, 15,     table, bit_sum);  break;
+               }
+       }
+#if !ORG_HUFFMAN_CODING   /* no part of original BladeEnc */
+       else if (max == 15)
+       {
+               triple_Huffman_2 (start, end,/* 13, 15, 24, */ table, bit_sum);
+       }
+#endif
+       else
+       {
+               max -= 15;
+
+#if ORG_HUFFMAN_CODING
+               choice0 = 15;  while (ht[choice0].linmax < max)  choice0++;
+#else
+               choice0 = 16;  while (ht[choice0].linmax < max)  choice0++;
+#endif
+
+assert(choice0 < 24);
+               choice1 = 24;  while (ht[choice1].linmax < max)  choice1++;
+assert(choice1 < 32);
+
+#if ORG_HUFFMAN_CODING
+               double_Huffman_2 (start, end, choice1, choice0, table, bit_sum);
+#else
+               double_Huffman_2 (start, end, choice0, choice1, table, bit_sum);
+#endif
+       }
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*              choose_table_short                                                            */
+/*  ========================================================================================  */
+/*
+       Choose the Huffman table that will encode ix[start_sfb..end_sfb][0..2]
+       with the fewest bits and increases the bit_sum by the amount of these bits.
+
+       Note: This code contains knowledge about the sizes and characteristics
+       of the Huffman tables as defined in the IS (Table B.7), and will not work
+       with any arbitrary tables.
+*/
+
+static void                    choose_table_short
+(
+       unsigned                                start_sfb,
+       unsigned                                end_sfb,
+       unsigned                                max,
+       unsigned                                *table,
+       unsigned                                *bit_sum
+)
+{
+       unsigned                                choice0;
+#if !ORG_HUFFMAN_CODING
+       unsigned                                choice1;
+#endif
+       int                                             start, end;
+
+       start = 3 * scalefac_band_short[start_sfb];
+       end   = 3 * scalefac_band_short[  end_sfb];
+
+       if (max == 0)
+       {
+               *table = 0;
+               return;
+       }
+
+       if (max < 15)
+       {
+               choice0 = 1;  /* we can start with 1 because ht[0].xlen == 0 <= max */
+               while (ht[choice0].xlen <= max)
+                       choice0++;
+
+#if ORG_HUFFMAN_CODING
+                                 tiny_single_Huffman (start, end, choice0,    table, bit_sum);
+#else
+               switch (choice0)
+               {
+                       case  1:  tiny_single_Huffman (start, end,/* 1 */      table, bit_sum);  break;
+                       case  2:  tiny_double_Huffman (start, end,  2,  3,     table, bit_sum);  break;
+                       case  5:  tiny_double_Huffman (start, end,  5,  6,     table, bit_sum);  break;
+                       case  7:  tiny_triple_Huffman (start, end,  7,  8,  9, table, bit_sum);  break;
+                       case 10:  tiny_triple_Huffman (start, end, 10, 11, 12, table, bit_sum);  break;
+                       case 13:  tiny_double_Huffman (start, end, 13, 15,     table, bit_sum);  break;
+               }
+#endif
+       }
+#if !ORG_HUFFMAN_CODING   /* no part of original BladeEnc */
+       else if (max == 15)
+       {
+               tiny_triple_Huffman_2 (start, end,/* 13, 15, 24, */ table, bit_sum);
+       }
+#endif
+       else
+       {
+               max -= 15;
+
+#if ORG_HUFFMAN_CODING
+
+               choice0 = 15;  while (ht[choice0].linmax < max)  choice0++;
+assert(choice0 < 24);
+               tiny_single_Huffman_2 (start, end, choice0, table, bit_sum);
+
+#else
+
+               choice0 = 16;  while (ht[choice0].linmax < max)  choice0++;
+assert(choice0 < 24);
+               choice1 = 24;  while (ht[choice1].linmax < max)  choice1++;
+assert(choice1 < 32);
+               tiny_double_Huffman_2 (start, end, choice0, choice1, table, bit_sum);
+
+#endif
+       }
+}
+
+
+
+
+
+/*  ========================================================================================  */
+/*      Huffmania                                                                             */
+/*  ========================================================================================  */
+
+
+
+/*
+       That case, we don«t need to decide which is the best table.
+*/
+
+static void                    single_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0, == 1 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+/*
+       int                                             v;
+*/
+       unsigned                                bits0, signs, idx;
+
+       static  struct huffcodetab              *h0 = ht + /* table0 */ 1;   /* static because of the constant!!! */
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == 2 */
+#endif
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx = 0;
+#if 0
+               v = *pos++;  if (v)  {signs++;  idx = v /* * ylen */ + v;}
+               v = *pos++;  if (v)  {signs++;  idx += v;}
+#else
+               if (*pos++)  {signs++;  idx = 2;}
+               if (*pos++)  {signs++;  idx++;}
+#endif
+               bits0 += h0->hlen[idx];
+       }
+
+       *choice = /* table0 */ 1;
+       *sum += bits0 + signs;
+}
+
+
+
+#if ORG_HUFFMAN_CODING
+static void                    tiny_single_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v0, v1, v2;
+       unsigned                                bits0, signs, idx0, idx1, idx2;
+
+       struct huffcodetab              *h0 = ht + table0;
+
+       unsigned                                ylen = h0->ylen;
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+
+               v0 = *pos++;  if (v0)  {signs++;  idx0 = v0 * ylen;}
+               v1 = *pos++;  if (v1)  {signs++;  idx1 = v1 * ylen;}
+               v2 = *pos++;  if (v2)  {signs++;  idx2 = v2 * ylen;}
+               v0 = *pos++;  if (v0)  {signs++;  idx0 += v0;}
+               v1 = *pos++;  if (v1)  {signs++;  idx1 += v1;}
+               v2 = *pos++;  if (v2)  {signs++;  idx2 += v2;}
+
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+       }
+
+       *choice = table0;
+       *sum += bits0 + signs;
+}
+#else
+static void tiny_single_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0 == 1 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+/*
+       int                                             v0, v1, v2;
+*/
+       unsigned                                bits0, signs, idx0, idx1, idx2;
+
+       static  struct huffcodetab              *h0 = ht + /* table0 */ 1;   /* static because of the constant!!! */
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == 2 --- static because of the constant!!! */
+#endif
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+
+               if (*pos++)  {signs++;  idx0 = 2;}
+               if (*pos++)  {signs++;  idx1 = 2;}
+               if (*pos++)  {signs++;  idx2 = 2;}
+               if (*pos++)  {signs++;  idx0++;}
+               if (*pos++)  {signs++;  idx1++;}
+               if (*pos++)  {signs++;  idx2++;}
+
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+       }
+
+       *choice = /* table0 */ 1;
+       *sum += bits0 + signs;
+}
+#endif
+
+
+
+#if ORG_HUFFMAN_CODING
+static void tiny_single_Huffman_2   /* Escape tables */
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 15... */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v0, v1, v2;
+       unsigned                                bits0, signs, xbits, idx0, idx1, idx2;
+
+       struct huffcodetab              *h0 = ht + table0;
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == 16 --- static because of the constant!!! */
+#endif
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = signs = xbits = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+
+               v0 = *pos++;  if (v0)  {if (v0 > 14)  {v0 = 15;  xbits++;}  signs++;  idx0  = v0 /* * ylen */ << 4;}
+               v1 = *pos++;  if (v1)  {if (v1 > 14)  {v1 = 15;  xbits++;}  signs++;  idx1  = v1 /* * ylen */ << 4;}
+               v2 = *pos++;  if (v2)  {if (v2 > 14)  {v2 = 15;  xbits++;}  signs++;  idx2  = v2 /* * ylen */ << 4;}
+               v0 = *pos++;  if (v0)  {if (v0 > 14)  {v0 = 15;  xbits++;}  signs++;  idx0 += v0;}
+               v1 = *pos++;  if (v1)  {if (v1 > 14)  {v1 = 15;  xbits++;}  signs++;  idx1 += v1;}
+               v2 = *pos++;  if (v2)  {if (v2 > 14)  {v2 = 15;  xbits++;}  signs++;  idx2 += v2;}
+
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+       }
+
+       bits0 += xbits * h0->linbits;
+
+       *choice = table0;
+       *sum += bits0 + signs;
+}
+#endif
+
+
+
+
+
+/*
+       The following function is called for the most maximum values below 16 (respectively 15)
+*/
+
+static void                    double_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 2, 5, 13 */
+       unsigned                                table1,   /* 3, 6, 15 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v;
+       unsigned                                bits0, bits1, signs, idx;
+
+       struct huffcodetab              *h0 = ht + table0;
+       struct huffcodetab              *h1 = ht + table1;
+
+       unsigned                                ylen = h0->ylen;   /* == h1->ylen */
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx = 0;
+               v = *pos++;  if (v)  {signs++;  idx = v * ylen;}
+               v = *pos++;  if (v)  {signs++;  idx += v;}
+               bits0 += h0->hlen[idx];
+               bits1 += h1->hlen[idx];
+       }
+
+       if (bits0 < bits1)
+       {
+               *choice = table0;
+               *sum += bits0 + signs;
+       }
+       else
+       {
+               *choice = table1;
+               *sum += bits1 + signs;
+       }
+}
+
+
+
+static void                    tiny_double_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 2, 5, 13 */
+       unsigned                                table1,   /* 3, 6, 15 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v0, v1, v2;
+       unsigned                                bits0, bits1, signs, idx0, idx1, idx2;
+
+       struct huffcodetab              *h0 = ht + table0;
+       struct huffcodetab              *h1 = ht + table1;
+
+       unsigned                                ylen = h0->ylen;   /* == h1->ylen */
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+               v0 = *pos++;  if (v0)  {signs++;  idx0 = v0 * ylen;}
+               v1 = *pos++;  if (v1)  {signs++;  idx1 = v1 * ylen;}
+               v2 = *pos++;  if (v2)  {signs++;  idx2 = v2 * ylen;}
+               v0 = *pos++;  if (v0)  {signs++;  idx0 += v0;}
+               v1 = *pos++;  if (v1)  {signs++;  idx1 += v1;}
+               v2 = *pos++;  if (v2)  {signs++;  idx2 += v2;}
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+               bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
+       }
+
+       if (bits0 < bits1)
+       {
+               *choice = table0;
+               *sum += bits0 + signs;
+       }
+       else
+       {
+               *choice = table1;
+               *sum += bits1 + signs;
+       }
+}
+
+
+
+/*
+       poor men«s brave tailor --- only three at a blow...
+*/
+
+static void                    triple_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 7, 10 */
+       unsigned                                table1,   /* 8, 11 */
+       unsigned                                table2,   /* 9, 12 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v;
+       unsigned                                bits0, bits1, bits2, signs, idx;
+
+       struct huffcodetab              *h0 = ht + table0;
+       struct huffcodetab              *h1 = ht + table1;
+       struct huffcodetab              *h2 = ht + table2;
+
+       unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = bits2 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx = 0;
+               v = *pos++;  if (v)  {signs++;  idx = v * ylen;}
+               v = *pos++;  if (v)  {signs++;  idx += v;}
+               bits0 += h0->hlen[idx];
+               bits1 += h1->hlen[idx];
+               bits2 += h2->hlen[idx];
+       }
+
+       if (bits0 < bits1  &&  bits0 < bits2)
+       {
+               *choice = table0;
+               *sum += bits0 + signs;
+       }
+       else if (bits1 < bits2)
+       {
+               *choice = table1;
+               *sum += bits1 + signs;
+       }
+       else
+       {
+               *choice = table2;
+               *sum += bits2 + signs;
+       }
+}
+
+
+
+static void                    tiny_triple_Huffman
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 7, 10 */
+       unsigned                                table1,   /* 8, 11 */
+       unsigned                                table2,   /* 9, 12 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v0, v1, v2;
+       unsigned                                bits0, bits1, bits2, signs, idx0, idx1, idx2;
+
+       struct huffcodetab              *h0 = ht + table0;
+       struct huffcodetab              *h1 = ht + table1;
+       struct huffcodetab              *h2 = ht + table2;
+
+       unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = bits2 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+               v0 = *pos++;  if (v0)  {signs++;  idx0 = v0 * ylen;}
+               v1 = *pos++;  if (v1)  {signs++;  idx1 = v1 * ylen;}
+               v2 = *pos++;  if (v2)  {signs++;  idx2 = v2 * ylen;}
+               v0 = *pos++;  if (v0)  {signs++;  idx0 += v0;}
+               v1 = *pos++;  if (v1)  {signs++;  idx1 += v1;}
+               v2 = *pos++;  if (v2)  {signs++;  idx2 += v2;}
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+               bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
+               bits2 += h2->hlen[idx0] + h2->hlen[idx1] + h2->hlen[idx2];
+       }
+
+       if (bits0 < bits1  &&  bits0 < bits2)
+       {
+               *choice = table0;
+               *sum += bits0 + signs;
+       }
+       else if (bits1 < bits2)
+       {
+               *choice = table1;
+               *sum += bits1 + signs;
+       }
+       else
+       {
+               *choice = table2;
+               *sum += bits2 + signs;
+       }
+}
+
+
+
+
+
+/*
+       The escape table 24 deals with linbits=4 instead of linbits=0 in case of table 13 and 15.
+       Nevertheless, sometimes it produces the better result...
+       Furthermore we take advantage because of the constant table numbers.
+*/
+
+static void                    triple_Huffman_2
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0,   == 13 */
+/*     unsigned                                table1,   == 15 */
+/*     unsigned                                table2,   == 24 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v;
+       unsigned                                bits0, bits1, bits2, signs, idx;
+
+       static  struct huffcodetab              *h0 = ht + /* table0 */ 13;   /* all static declarations because of the constant values!!! */
+       static  struct huffcodetab              *h1 = ht + /* table1 */ 15;
+       static  struct huffcodetab              *h2 = ht + /* table2 */ 24;
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */   /* == 16 */
+#endif
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = bits2 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx = 0;
+               v = *pos++;  if (v)  {if (v == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx = v /* * ylen */ << 4;}
+               v = *pos++;  if (v)  {if (v == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx += v;}
+               bits0 += h0->hlen[idx];
+               bits1 += h1->hlen[idx];
+               bits2 += h2->hlen[idx];
+       }
+
+       if (bits0 < bits1  &&  bits0 < bits2)
+       {
+               *choice = /* table0 */ 13;
+               *sum += bits0 + signs;
+       }
+       else if (bits1 < bits2)
+       {
+               *choice = /* table1 */ 15;
+               *sum += bits1 + signs;
+       }
+       else
+       {
+               *choice = /* table2 */ 24;
+               *sum += bits2 + signs;
+       }
+}
+
+
+
+static void                    tiny_triple_Huffman_2
+(
+       unsigned                                start,
+       unsigned                                end,
+/*     unsigned                                table0,   == 13 */
+/*     unsigned                                table1,   == 15 */
+/*     unsigned                                table2,   == 24 */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v0, v1, v2;
+       unsigned                                bits0, bits1, bits2, signs, idx0, idx1, idx2;
+
+       static  struct huffcodetab              *h0 = ht + /* table0 */ 13;   /* all static declarations because of the constant values!!! */
+       static  struct huffcodetab              *h1 = ht + /* table1 */ 15;
+       static  struct huffcodetab              *h2 = ht + /* table2 */ 24;
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */   /* == 16 */
+#endif
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = bits2 = signs = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+               v0 = *pos++;  if (v0)  {if (v0 == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx0 = v0 /* * ylen */ << 4;}
+               v1 = *pos++;  if (v1)  {if (v1 == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx1 = v1 /* * ylen */ << 4;}
+               v2 = *pos++;  if (v2)  {if (v2 == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx2 = v2 /* * ylen */ << 4;}
+               v0 = *pos++;  if (v0)  {if (v0 == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx0 += v0;}
+               v1 = *pos++;  if (v1)  {if (v1 == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx1 += v1;}
+               v2 = *pos++;  if (v2)  {if (v2 == 15)  bits2 += /* h2->linbits */ 4;  signs++;  idx2 += v2;}
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+               bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
+               bits2 += h2->hlen[idx0] + h2->hlen[idx1] + h2->hlen[idx2];
+       }
+
+       if (bits0 < bits1  &&  bits0 < bits2)
+       {
+               *choice = /* table0 */ 13;
+               *sum += bits0 + signs;
+       }
+       else if (bits1 < bits2)
+       {
+               *choice = /* table1 */ 15;
+               *sum += bits1 + signs;
+       }
+       else
+       {
+               *choice = /* table2 */ 24;
+               *sum += bits2 + signs;
+       }
+}
+
+
+
+
+
+/*
+       In case of two escape tables, we esepecially have to take care for
+       the possibly different linbits values...
+*/
+
+static void                    double_Huffman_2   /* Escape tables */
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 16... */
+       unsigned                                table1,   /* 24... */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v;
+       unsigned                                bits0, bits1, signs, xbits, idx;
+
+       struct huffcodetab              *h0 = ht + table0;
+       struct huffcodetab              *h1 = ht + table1;
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == h1->ylen */   /* == 16 */
+#endif
+       unsigned                                linbits0 = h0->linbits;
+       unsigned                                linbits1 = h1->linbits;
+
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = signs = xbits = 0;
+
+       while (pos < fin)
+       {
+               idx = 0;
+               v = *pos++;  if (v)  {if (v > 14)  {v = 15;  xbits++;/*bits0 += linbits0;  bits1 += linbits1;*/}  signs++;  idx = v /* * ylen */ << 4;}
+               v = *pos++;  if (v)  {if (v > 14)  {v = 15;  xbits++;/*bits0 += linbits0;  bits1 += linbits1;*/}  signs++;  idx += v;}
+               bits0 += h0->hlen[idx];
+               bits1 += h1->hlen[idx];
+       }
+       bits0 += xbits * linbits0;
+       bits1 += xbits * linbits1;
+
+       if (bits0 < bits1)
+       {
+               *choice = table0;
+               *sum += bits0 + signs;
+       }
+       else
+       {
+               *choice = table1;
+               *sum += bits1 + signs;
+       }
+}
+
+
+
+static void                    tiny_double_Huffman_2   /* Escape tables */
+(
+       unsigned                                start,
+       unsigned                                end,
+       unsigned                                table0,   /* 16... */
+       unsigned                                table1,   /* 24... */
+       unsigned                                *choice,
+       unsigned                                *sum
+)
+{
+       int                                             v0, v1, v2;
+       unsigned                                bits0, bits1, signs, xbits, idx0, idx1, idx2;
+
+       struct huffcodetab              *h0 = ht + table0;
+       struct huffcodetab              *h1 = ht + table1;
+
+#if 0   /* not needed */
+       static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == 16 --- static because of the constant!!! */
+#endif
+       int                                             *pos = ix_l + start;
+       int                                             *fin = ix_l + end;
+
+       bits0 = bits1 = signs = xbits = 0;
+
+       while (pos < fin)
+       {
+               idx0 = idx1 = idx2 = 0;
+
+               v0 = *pos++;  if (v0)  {if (v0 > 14)  {v0 = 15;  xbits++;}  signs++;  idx0  = v0 /* * ylen */ << 4;}
+               v1 = *pos++;  if (v1)  {if (v1 > 14)  {v1 = 15;  xbits++;}  signs++;  idx1  = v1 /* * ylen */ << 4;}
+               v2 = *pos++;  if (v2)  {if (v2 > 14)  {v2 = 15;  xbits++;}  signs++;  idx2  = v2 /* * ylen */ << 4;}
+               v0 = *pos++;  if (v0)  {if (v0 > 14)  {v0 = 15;  xbits++;}  signs++;  idx0 += v0;}
+               v1 = *pos++;  if (v1)  {if (v1 > 14)  {v1 = 15;  xbits++;}  signs++;  idx1 += v1;}
+               v2 = *pos++;  if (v2)  {if (v2 > 14)  {v2 = 15;  xbits++;}  signs++;  idx2 += v2;}
+
+               bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
+               bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
+       }
+
+       bits0 += xbits * h0->linbits;
+       bits1 += xbits * h1->linbits;
+
+       if (bits0 < bits1)
+       {
+               *choice = table0;
+               *sum += bits0 + signs;
+       }
+       else
+       {
+               *choice = table1;
+               *sum += bits1 + signs;
+       }
+}
+
+
+
diff --git a/lib/bladeenc/loop.h b/lib/bladeenc/loop.h
new file mode 100644 (file)
index 0000000..73d8bee
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       reformatted, redesigned
+*/
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: loop.h,v 1.1 2002/01/10 17:30:00 kramm Exp $
+ *
+ * $Log: loop.h,v $
+ * Revision 1.1  2002/01/10 17:30:00  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+
+/**********************************************************************
+ *   date   programmers                comment                        *
+ * 25. 6.92  Toshiyuki Ishino          Ver 1.0                        *
+ * 29.10.92  Masahiro Iwadare          Ver 2.0                        *
+ * 17. 4.93  Masahiro Iwadare          Updated for IS Modification    *
+ *                                                                    *
+ *********************************************************************/
+
+#ifndef                __LOOP__
+#define                __LOOP__
+
+
+
+
+#include "common.h"
+#include "l3side.h"
+
+
+
+
+
+/*     #define SBLIMIT                 32 */
+#define        CBLIMIT                 21
+
+#define        SFB_LMAX                22
+#define        SFB_SMAX                13
+
+
+
+
+extern int                             fInit_iteration_loop;
+/* NOT NEEDED
+extern int                             fInit_huffman_read_flag;
+*/
+
+
+extern int                             pretab[];
+
+
+struct scalefac_struct
+{
+       int l[23];
+       int s[14];
+};
+
+extern struct scalefac_struct          sfBandIndex[];  /* Table B.8 -- in loop.c */
+
+
+
+
+
+void                                   iteration_loop
+(
+       double                                  pe[][2],
+       double                                  xr_org[2][2][576],
+       III_psy_ratio                   *ratio,
+       III_side_info_t                 *l3_side,
+       int                                             l3_enc[2][2][576],
+       int                                             mean_bits,
+       int                                             stereo,
+       double                                  xr_dec[2][2][576],
+       III_scalefac_t                  *scalefac,
+       frame_params                    *fr_ps,
+       int                                             ancillary_pad,
+       int                                             bitsPerFrame
+);
+
+int                                            my_nint (double in);
+
+void                                   fixStatic_loop (void);
+
+void                                   genNoisePowTab (void);
+
+
+
+
+
+#endif         /* __LOOP__ */
diff --git a/lib/bladeenc/mdct.c b/lib/bladeenc/mdct.c
new file mode 100644 (file)
index 0000000..b25cd17
--- /dev/null
@@ -0,0 +1,574 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-11-04  Andre Piotrowski
+
+       -       some cosmetics
+       -       included "l3psy.h" to use the defined block type constants
+       -       negligible speed up of mdct_sub()
+       -       speed up of mdct()
+
+       2000-12-10  ap
+
+       -       speed up of mdct_sub() and mdct() - MDCT_CHANGE_LEVEL 5
+*/
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: mdct.c,v 1.1 2002/01/10 17:30:01 kramm Exp $
+ *
+ * $Log: mdct.c,v $
+ * Revision 1.1  2002/01/10 17:30:01  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+
+#include "common.h"
+#include <assert.h>
+#include "l3side.h"
+#include "l3psy.h"
+#include "mdct.h"
+
+
+
+
+
+/*
+       This is table B.9: coefficients for aliasing reduction
+*/
+static double                  c[8] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 };
+
+
+
+
+
+int                                            gr_idx[3] = {0,1,2};
+
+
+
+
+
+#if 0
+static double                  ca[8];
+#endif
+static double                  cs[8];
+
+
+
+
+
+int    fInit_mdct_sub;
+
+void mdct_sub
+(
+       L3SBS                                   (*sb_sample),
+       double                                  (*mdct_freq)[2][576],
+       int                                             stereo,
+       III_side_info_t                 *l3_side,
+       int                                             mode_gr
+)
+{
+       gr_info                                 *cod_info;
+#if MDCT_CHANGE_LEVEL < 5
+       double                                  mdct_in[36];
+#endif
+       int                                             gr, ch, band, k, j;
+       double                                  bu, bd;
+       int                                             block_type;
+       double                                  (*mdct_enc_long)[2][32][18] = (double (*)[2][32][18]) mdct_freq;
+
+       if (!fInit_mdct_sub)
+       {
+               /* prepare the aliasing reduction butterflies */
+               for (k = 0;  k < 8;  k++)
+               {
+                       double sq = sqrt (1.0 + c[k]*c[k]);
+#if 0
+                       ca[k] = c[k] / sq;
+#endif
+                       cs[k] = 1.0 / sq;
+               }
+               fInit_mdct_sub++;
+       }
+
+
+       for (gr = 0;  gr < mode_gr;  gr++)
+       {
+               int             pre_gr = gr_idx[gr  ];
+               int             cur_gr = gr_idx[gr+1];
+
+               for (ch = 0;  ch < stereo;  ch++)
+               {
+                       double (*mdct_enc)[18] = mdct_enc_long[gr][ch];
+
+                       cod_info = &l3_side->gr[gr].ch[ch].tt;
+                       block_type = cod_info->block_type;
+
+                       /* Compensate for inversion in the analysis filter */
+                       for (k = 1;  k < 18;  k++)
+                               if (k & 1)
+                                       for (band = 1;  band < 32;  band++)
+                                               if (band & 1)
+                                                       (*sb_sample)[ch][cur_gr][k][band] *= -1.0;
+                       /*
+                               Perform imdct of 18 previous subband samples
+                               + 18 current subband samples
+                       */
+                       for (band = 0;  band < 32;  band++)
+                       {
+#if MDCT_CHANGE_LEVEL < 5
+                               for (k = 0;  k < 18;  k++)
+                               {
+                                       mdct_in[k]    = (*sb_sample)[ch][pre_gr][k][band];
+                                       mdct_in[k+18] = (*sb_sample)[ch][cur_gr][k][band];
+                               }
+                               if (cod_info->mixed_block_flag  &&  (band < 2))
+                                       block_type = NORM_TYPE;   /* AND WHEN WILL THE BLOCK_TYPE BE SWITCHED BACK? */
+
+                                       /* &mdct_enc[gr][ch][band][0] */
+                               mdct (mdct_in, mdct_enc/*[gr][ch]*/[band], block_type);
+#else
+                               mdct ((*sb_sample)[ch][pre_gr], (*sb_sample)[ch][cur_gr], band, mdct_enc[band], block_type);
+#endif
+                       }
+
+                       /*
+                               Perform aliasing reduction butterfly
+                               on long blocks
+                       */
+
+                       if (block_type != SHORT_TYPE)
+                               for (band = 0;  band < 31;  band++)
+                                       for (k = 0;  k < 8;  k++)
+                                       {
+#if 1  /* This is faster because the calculation can be done more sequential */
+                                               bu = (mdct_enc[band  ][17-k] + mdct_enc[band+1][   k] * c[k]) * cs[k];
+                                               bd = (mdct_enc[band+1][   k] - mdct_enc[band  ][17-k] * c[k]) * cs[k];
+                                               mdct_enc[band  ][17-k] = bu;
+                                               mdct_enc[band+1][   k] = bd;
+#else
+                                               bu = mdct_enc[band  ][17-k] * cs[k] + mdct_enc[band+1][   k] * ca[k];
+                                               bd = mdct_enc[band+1][   k] * cs[k] - mdct_enc[band  ][17-k] * ca[k];
+                                               mdct_enc[band  ][17-k] = bu;
+                                               mdct_enc[band+1][   k] = bd;
+#endif
+                                       }
+               }
+       }
+
+       /*
+               Save latest granule's subband samples to be used in
+               the next mdct call
+       */
+       j = gr_idx[mode_gr];
+       for (k = mode_gr;  k > 0;  k--)
+               gr_idx[k] = gr_idx[k-1];
+       gr_idx[0] = j;
+}
+
+
+
+
+
+/*-------------------------------------------------------------------*/
+/*                                                                   */
+/*   Function: Calculation of the MDCT                               */
+/*   In the case of long blocks ( block_type 0,1,3 ) there are       */
+/*   36 coefficents in the time domain and 18 in the frequency       */
+/*   domain.                                                         */
+/*   In the case of short blocks (block_type 2 ) there are 3         */
+/*   transformations with short length. This leads to 12 coefficents */
+/*   in the time and 6 in the frequency domain. In this case the     */
+/*   results are stored side by side in the vector out[].            */
+/*                                                                   */
+/*   New layer3                                                      */
+/*                                                                   */
+/*-------------------------------------------------------------------*/
+
+int    fInit_mdct;
+
+
+
+#if MDCT_CHANGE_LEVEL == 5
+
+void mdct
+(
+       double                                  inA[18][32],
+       double                                  inB[18][32],
+       int                                             band,
+       double                                  *out,
+       int                                             block_type
+)
+{
+       static  double                  cos_l[18][18];
+       static  double                  cos_s[ 6][ 6];
+
+       static  double                  winNorm [18];
+       static  double                  winShort[ 6];
+
+       int                                             k, m, N;
+       double                                  t1, t2, t3, t4, t5, t6, u1, u2;
+
+
+       if (!fInit_mdct)
+       {
+               N = 36;
+               for (k = 0;  k < N/2;  k++)  winNorm [k] = sin (PI/36 * (k+0.5));
+               for (k = 0;  k < N/4;  k++)  for (m = 0;  m < N/2;  m++)  cos_l[k][m] = cos((PI/(2*N)) * (2* k     +1+N/2) * (2*m+1)) / (N/4);
+               for (     ;  k < N/2;  k++)  for (m = 0;  m < N/2;  m++)  cos_l[k][m] = cos((PI/(2*N)) * (2*(k+N/4)+1+N/2) * (2*m+1)) / (N/4);
+
+               N = 12;
+               for (k = 0;  k < N/2;  k++)  winShort[k] = sin (PI/12 * (k+0.5));
+               for (k = 0;  k < N/4;  k++)  for (m = 0;  m < N/2;  m++)  cos_s[k][m] = cos((PI/(2*N)) * (2*k      +1+N/2) * (2*m+1)) / (N/4);
+               for (     ;  k < N/2;  k++)  for (m = 0;  m < N/2;  m++)  cos_s[k][m] = cos((PI/(2*N)) * (2*(k+N/4)+1+N/2) * (2*m+1)) / (N/4);
+
+               fInit_mdct++;
+       }
+
+       for (m = 0;  m < 18;  m++)  out[m] = 0.0;
+       switch (block_type)
+       {
+               case  NORM_TYPE:
+                       for (k = 0;  k < 9;  k++)
+                       {
+                               t1 = winNorm [   k] * inA[k][band] - winNorm [17-k] * inA[17-k][band];
+                               t2 = winNorm [17-k] * inB[k][band] + winNorm [   k] * inB[17-k][band];
+                               for (m = 0;  m < 18;  m++)
+                                       out[m] += t1 * cos_l[k][m] + t2 * cos_l[k+9][m];
+                       }
+                       break;
+
+               case START_TYPE:
+                       for (k = 0;  k < 6;  k++)
+                       {
+                               t1 = winNorm [   k] * inA[k][band] - winNorm [17-k] * inA[17-k][band];
+                               t2 =                  inB[k][band];
+                               for (m = 0;  m < 18;  m++)
+                                       out[m] += t1 * cos_l[k][m] + t2 * cos_l[k+9][m];
+                       }
+                       for (k =  6;  k <  9;  k++)
+                       {
+                               t1 = winNorm [   k] * inA[k][band] - winNorm [17-k] * inA[17-k][band];
+                               t2 = winShort[11-k] * inB[k][band] + winShort[k- 6] * inB[17-k][band];
+                               for (m = 0;  m < 18;  m++)
+                                       out[m] += t1 * cos_l[k][m] + t2 * cos_l[k+9][m];
+                       }
+                       break;
+
+               case  STOP_TYPE:
+                       for (k =  0;  k <  6;  k++)
+                       {
+                               t1 =                                                 -inA[17-k][band];
+                               t2 = winNorm [17-k] * inB[k][band] + winNorm [   k] * inB[17-k][band];
+                               for (m = 0;  m < 18;  m++)
+                                       out[m] += t1 * cos_l[k][m] + t2 * cos_l[k+9][m];
+                       }
+                       for (k =  6;  k <  9;  k++)
+                       {
+                               t1 = winShort[k- 6] * inA[k][band] - winShort[11-k] * inA[17-k][band];
+                               t2 = winNorm [17-k] * inB[k][band] + winNorm [   k] * inB[17-k][band];
+                               for (m = 0;  m < 18;  m++)
+                                       out[m] += t1 * cos_l[k][m] + t2 * cos_l[k+9][m];
+                       }
+                       break;
+
+               case SHORT_TYPE:
+                       for (k =  0;  k <  3;  k++)
+                       {
+                               u1 = winShort[k];  u2 = winShort[5-k];
+                               t1 = u1 * inA[k+ 6][band] - u2 * inA[11-k][band];
+                               t2 = u2 * inA[k+12][band] + u1 * inA[17-k][band];
+                               t3 = u1 * inA[k+12][band] - u2 * inA[17-k][band];
+                               t4 = u2 * inB[k   ][band] + u1 * inB[ 5-k][band];
+                               t5 = u1 * inB[k   ][band] - u2 * inB[ 5-k][band];
+                               t6 = u2 * inB[k+ 6][band] + u1 * inB[11-k][band];
+                               for (m = 0;  m <  6;  m++)
+                               {
+                                       u1 = cos_s[k][m];  u2 = cos_s[k+3][m];
+                                       out[3*m  ] += t1 * u1 + t2 * u2;
+                                       out[3*m+1] += t3 * u1 + t4 * u2;
+                                       out[3*m+2] += t5 * u1 + t6 * u2;
+                               }
+                       }
+       }
+}
+
+#elif MDCT_CHANGE_LEVEL == 4   /* reduce number of multiplications to nearly the half once more! (cos_x[9+k][m] = -cos_x[9-k][m] and cos_x[35+k][m] = cos_x[35-k][m]) */
+
+void mdct
+(
+       double                                  *in,
+       double                                  *out,
+       int                                             block_type
+)
+{
+       static  double                  cos_l[36][18];
+       static  double                  cos_s[12][ 6];
+
+       static  double                  winNorm [36];
+       static  double                  winShort[12];
+       static  double                  *winStart = winShort-18;
+       static  double                  *winStop  = winShort- 6;
+
+       int                                             l, k, m, N;
+       double                                  temp;
+
+
+       if (!fInit_mdct)
+       {
+               N = 36;
+               for (k = 0;  k < N;  k++)  winNorm [k] = sin (PI/36 * (k+0.5));
+               for (k = 0;  k < N;  k++)  for (m = 0;  m < N/2;  m++)  cos_l[k][m] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+
+               N = 12;
+               for (k = 0;  k < N;  k++)  winShort[k] = sin (PI/12 * (k+0.5));
+               for (k = 0;  k < N;  k++)  for (m = 0;  m < N/2;  m++)  cos_s[k][m] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+
+               fInit_mdct++;
+       }
+
+       for (m = 0;  m < 18;  m++)  out[m] = 0.0;
+       switch (block_type)
+       {
+               case  NORM_TYPE:
+                       for (k =  0;  k <  9;  k++)  {temp = (winNorm [k] * in[k] - winNorm [17-k] * in[17-k]);  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 18;  k < 27;  k++)  {temp = (winNorm [k] * in[k] + winNorm [53-k] * in[53-k]);  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       break;
+
+               case START_TYPE:
+                       for (k =  0;  k <  9;  k++)  {temp = (winNorm [k] * in[k] - winNorm [17-k] * in[17-k]);  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 18;  k < 24;  k++)  {temp =               in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 24;  k < 27;  k++)  {temp = (winStart[k] * in[k] + winStart[53-k] * in[53-k]);  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       break;
+
+               case  STOP_TYPE:
+                       for (k =  6;  k <  9;  k++)  {temp = (winStop [k] * in[k] - winStop [17-k] * in[17-k]);  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 12;  k < 18;  k++)  {temp =               in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 18;  k < 27;  k++)  {temp = (winNorm [k] * in[k] + winNorm [53-k] * in[53-k]);  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       break;
+
+               case SHORT_TYPE:
+                       for (l = 0;  l < 3;  l++)
+                       {
+                               for (k =  0;  k <  3;  k++)  {temp = (winShort[k] * in[k+6+l*6] - winShort[ 5-k] * in[ 5-k+6+l*6]);  for (m = 0;  m <  6;  m++)  out[3*m+l] += temp * cos_s[k][m];}
+                               for (k =  6;  k <  9;  k++)  {temp = (winShort[k] * in[k+6+l*6] + winShort[17-k] * in[17-k+6+l*6]);  for (m = 0;  m <  6;  m++)  out[3*m+l] += temp * cos_s[k][m];}
+                       }
+       }
+}
+
+#elif MDCT_CHANGE_LEVEL == 3   /* reduce number of multiplications to nearly the half (win_x[k]*in[k] is constant in m-loop)! flip cos_x components for faster access */
+
+void mdct
+(
+       double                                  *in,
+       double                                  *out,
+       int                                             block_type
+)
+{
+       static  double                  cos_l[36][18];
+       static  double                  cos_s[12][ 6];
+
+       static  double                  winNorm [36];
+       static  double                  winShort[12];
+       static  double                  *winStart = winShort-18;
+       static  double                  *winStop  = winShort- 6;
+
+       int                                             l, k, m, N;
+       double                                  temp;
+
+
+       if (!fInit_mdct)
+       {
+               N = 36;
+               for (k = 0;  k < N;  k++)  winNorm [k] = sin (PI/36 * (k+0.5));
+               for (k = 0;  k < N;  k++)  for (m = 0;  m < N/2;  m++)  cos_l[k][m] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+
+               N = 12;
+               for (k = 0;  k < N;  k++)  winShort[k] = sin (PI/12 * (k+0.5));
+               for (k = 0;  k < N;  k++)  for (m = 0;  m < N/2;  m++)  cos_s[k][m] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+
+               fInit_mdct++;
+       }
+
+       for (m = 0;  m < 18;  m++)  out[m] = 0.0;
+       switch (block_type)
+       {
+               case  NORM_TYPE:
+                       for (k =  0;  k < 36;  k++)  {temp = winNorm [k] * in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       break;
+
+               case START_TYPE:
+                       for (k =  0;  k < 18;  k++)  {temp = winNorm [k] * in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 18;  k < 24;  k++)  {temp =               in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 24;  k < 30;  k++)  {temp = winStart[k] * in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       break;
+
+               case  STOP_TYPE:
+                       for (k =  6;  k < 12;  k++)  {temp = winStop [k] * in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 12;  k < 18;  k++)  {temp =               in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       for (k = 18;  k < 36;  k++)  {temp = winNorm [k] * in[k];  for (m = 0;  m < 18;  m++)  out[m] += temp * cos_l[k][m];}
+                       break;
+
+               case SHORT_TYPE:
+                       for (l = 0;  l < 3;  l++)
+                               for (k =  0;  k < 12;  k++)  {temp = winShort[k] * in[k+6+l*6];  for (m = 0;  m <  6;  m++)  out[3*m+l] += temp * cos_s[k][m];}
+       }
+}
+
+#elif MDCT_CHANGE_LEVEL == 2   /* avoid calculating of some redundant values; take care for some special values */
+
+void mdct
+(
+       double                                  *in,
+       double                                  *out,
+       int                                             block_type
+)
+{
+       static  double                  cos_l[18][36];
+       static  double                  cos_s[ 6][12];
+
+       static  double                  winNorm [36];
+       static  double                  winShort[12];
+       static  double                  *winStart = winShort-18;
+       static  double                  *winStop  = winShort- 6;
+
+       int                                             l, k, m, N;
+       double                                  sum;
+
+
+       if (!fInit_mdct)
+       {
+               for (k =  0;  k < 36;  k++)  winNorm [k] = sin (PI/36 * (k+0.5));
+               for (k =  0;  k < 12;  k++)  winShort[k] = sin (PI/12 * (k+0.5));
+
+               N = 36;  for (m = 0;  m < N/2;  m++)  for (k = 0;  k < N;  k++)  cos_l[m][k] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+               N = 12;  for (m = 0;  m < N/2;  m++)  for (k = 0;  k < N;  k++)  cos_s[m][k] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+
+               fInit_mdct++;
+       }
+
+       switch (block_type)
+       {
+               case  NORM_TYPE:
+                       for (m = 0;  m < 18;  m++)
+                       {
+                               sum = 0.0;
+                               for (k =  0;  k < 36;  k++) sum += winNorm [k] * in[k] * cos_l[m][k];
+                               out[m] = sum;
+                       }
+                       break;
+
+               case START_TYPE:
+                       for (m = 0;  m < 18;  m++)
+                       {
+                               sum = 0.0;
+                               for (k =  0;  k < 18;  k++) sum += winNorm [k] * in[k] * cos_l[m][k];
+                               for (k = 18;  k < 24;  k++) sum +=               in[k] * cos_l[m][k];
+                               for (k = 24;  k < 30;  k++) sum += winStart[k] * in[k] * cos_l[m][k];
+                               out[m] = sum;
+                       }
+                       break;
+
+               case  STOP_TYPE:
+                       for (m = 0;  m < 18;  m++)
+                       {
+                               sum = 0.0;
+                               for (k =  6;  k < 12;  k++) sum += winStop [k] * in[k] * cos_l[m][k];
+                               for (k = 12;  k < 18;  k++) sum +=               in[k] * cos_l[m][k];
+                               for (k = 18;  k < 36;  k++) sum += winNorm [k] * in[k] * cos_l[m][k];
+                               out[m] = sum;
+                       }
+                       break;
+
+               case SHORT_TYPE:
+                       for (l = 0;  l < 3;  l++)
+                       {
+                               for (m = 0;  m <  6;  m++)
+                               {
+                                       sum = 0.0;
+                                       for (k = 0;  k < 12;  k++) sum += winShort[k] * in[k+6+l*6] * cos_s[m][k];
+                                       out[3*m+l] = sum;
+                               }
+                       }
+       }
+}
+
+#elif MDCT_CHANGE_LEVEL == 1   /* reformatted for better overview; use block type constants */
+
+void mdct
+(
+       double                                  *in,
+       double                                  *out,
+       int                                             block_type
+)
+{
+       static  double                  cos_l[18][36];
+       static  double                  cos_s[ 6][12];
+
+       static  double                  winNorm [36];
+       static  double                  winShort[12];
+       static  double                  winStart[36];
+       static  double                  winStop [36];
+
+       int                                             l, k, m, N;
+       double                                  sum;
+
+
+       if (!fInit_mdct)
+       {
+               /* type 0 -- NORM_TYPE */
+               for (k =  0;  k < 36;  k++)  winNorm [k] = sin (PI/36 * (k+0.5));
+               /* type 1 -- START_TYPE */
+               for (k =  0;  k < 18;  k++)  winStart[k] = sin (PI/36 * (k+0.5));
+               for (k = 18;  k < 24;  k++)  winStart[k] = 1.0;
+               for (k = 24;  k < 30;  k++)  winStart[k] = sin (PI/12 * (k+0.5 - 18));
+               for (k = 30;  k < 36;  k++)  winStart[k] = 0.0;
+               /* type 3 -- STOP_TYPE */
+               for (k =  0;  k <  6;  k++)  winStop [k] = 0.0;
+               for (k =  6;  k < 12;  k++)  winStop [k] = sin (PI/12 * (k+0.5 -  6));
+               for (k = 12;  k < 18;  k++)  winStop [k] = 1.0;
+               for (k = 18;  k < 36;  k++)  winStop [k] = sin (PI/36 * (k+0.5));
+               /* type 2 -- SHORT_TYPE */
+               for (k =  0;  k < 12;  k++)  winShort[k] = sin (PI/12 * (k+0.5));
+
+               N = 12;  for (m = 0;  m < N/2;  m++)  for (k = 0;  k < N;  k++)  cos_s[m][k] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+               N = 36;  for (m = 0;  m < N/2;  m++)  for (k = 0;  k < N;  k++)  cos_l[m][k] = cos((PI/(2*N)) * (2*k+1+N/2) * (2*m+1)) / (N/4);
+
+               fInit_mdct++;
+       }
+
+       switch (block_type)
+       {
+       case  NORM_TYPE:  N = 36;                             for (m = 0;  m < N/2;  m++)  {sum = 0.0;  for (k = 0;  k < N;  k++) sum += winNorm [k] * in[k      ] * cos_l[m][k];  out[  m  ] = sum;}  break;
+       case START_TYPE:  N = 36;                             for (m = 0;  m < N/2;  m++)  {sum = 0.0;  for (k = 0;  k < N;  k++) sum += winStart[k] * in[k      ] * cos_l[m][k];  out[  m  ] = sum;}  break;
+       case  STOP_TYPE:  N = 36;                             for (m = 0;  m < N/2;  m++)  {sum = 0.0;  for (k = 0;  k < N;  k++) sum += winStop [k] * in[k      ] * cos_l[m][k];  out[  m  ] = sum;}  break;
+       case SHORT_TYPE:  N = 12;  for (l = 0;  l < 3;  l++) {for (m = 0;  m < N/2;  m++)  {sum = 0.0;  for (k = 0;  k < N;  k++) sum += winShort[k] * in[k+6+l*6] * cos_s[m][k];  out[3*m+l] = sum;}}
+       }
+}
+
+#endif /* MDCT_CHANGE_LEVEL */
diff --git a/lib/bladeenc/mdct.h b/lib/bladeenc/mdct.h
new file mode 100644 (file)
index 0000000..934b9cc
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-10  Andre Piotrowski
+
+       -       reformatted, redesigned
+       -       MDCT_CHANGE_LEVEL 5
+*/
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: mdct.h,v 1.1 2002/01/10 17:30:01 kramm Exp $
+ *
+ * $Log: mdct.h,v $
+ * Revision 1.1  2002/01/10 17:30:01  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+
+#ifndef                __MDCT__
+#define                __MDCT__
+
+
+
+
+
+#define MDCT_CHANGE_LEVEL 5
+
+
+
+
+
+typedef                double                                  L3SBS[2][3][18][SBLIMIT];   /* [gr][ch] */
+
+
+
+
+
+extern int                             fInit_mdct_sub;
+extern int                             fInit_mdct;
+
+
+extern int                             gr_idx[3];
+
+
+
+
+
+void                                   mdct_sub
+(
+       L3SBS                                   *sb_sample,
+       double                                  (*mdct_freq)[2][576],
+       int                                             stereo,
+       III_side_info_t                 *l3_side,
+       int                                             mode_gr
+);
+
+
+
+#if MDCT_CHANGE_LEVEL < 5
+
+void                                   mdct
+(
+       double                                  *in,
+       double                                  *out,
+       int                                             block_type
+);
+
+#else
+
+void                                   mdct
+(
+       double                                  inA[18][32],
+       double                                  inB[18][32],
+       int                                             band,
+       double                                  *out,
+       int                                             block_type
+);
+
+#endif
+
+
+
+
+
+#endif         /* __MDCT__ */
diff --git a/lib/bladeenc/reservoir.c b/lib/bladeenc/reservoir.c
new file mode 100644 (file)
index 0000000..ff8be75
--- /dev/null
@@ -0,0 +1,268 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       reformatted
+*/
+
+#include       <stdio.h>
+#include       <stdlib.h>
+#include       <math.h>
+#include       <assert.h>
+
+#include       "common.h"
+#include       "l3side.h"
+#include       "loop.h"
+#include       "huffman.h"
+#include       "l3bitstream.h"
+#include       "reservoir.h"
+
+
+
+
+
+/*
+       Layer3 bit reservoir:
+       Described in C.1.5.4.2.2 of the IS
+*/
+
+static int                             ResvSize = 0;   /* in bits */
+static int                             ResvMax  = 0;   /* in bits */
+
+
+
+
+
+void                                   fixStatic_reservoir (void)
+{
+       ResvSize = 0;
+       ResvMax  = 0;
+}
+
+
+
+
+
+/*
+       ResvFrameBegin:
+       Called at the beginning of a frame. Updates the maximum
+       size of the reservoir, and checks to make sure main_data_begin
+       was set properly by the formatter
+*/
+void                                   ResvFrameBegin
+(
+       frame_params                    *fr_ps,
+       III_side_info_t                 *l3_side,
+       int                                             mean_bits,
+       int                                             frameLength
+)
+{
+       layer                                   *info;
+       int                                             fullFrameBits, mode_gr;
+       int                                             expectedResvSize, resvLimit;
+
+
+       info = fr_ps->header;
+       mode_gr = 2;
+       resvLimit = 4088; /* main_data_begin has 9 bits in MPEG 1 */
+
+
+       /*
+               main_data_begin was set by the formatter to the
+               expected value for the next call -- this should
+               agree with our reservoir size
+       */
+       expectedResvSize = l3_side->main_data_begin * 8;
+/*     assert (expectedResvSize == ResvSize); */
+
+       fullFrameBits = mean_bits * mode_gr;
+
+       /*
+               determine maximum size of reservoir:
+               ResvMax + frameLength <= 7680;
+
+               limit max size to resvLimit bits because
+               main_data_begin cannot indicate a
+               larger value
+       */
+       ResvMax = MIN(MAX (0, 7680-frameLength), resvLimit);
+}
+
+
+
+
+
+/*
+       ResvMaxBits:
+       Called at the beginning of each granule to get the max bit
+       allowance for the current granule based on reservoir size
+       and perceptual entropy.
+*/
+int                                            ResvMaxBits
+(
+       frame_params                    *fr_ps,
+       III_side_info_t                 *l3_side,
+       double                                  *pe,
+       int                                             mean_bits
+)
+{
+       int                                             more_bits, max_bits, add_bits, over_bits;
+
+
+       mean_bits /= fr_ps->stereo;
+
+       max_bits = mean_bits;
+
+       if (ResvMax != 0)
+       {
+               more_bits = (int) (*pe * 3.1 - mean_bits);
+
+               if (more_bits > 100)
+               {
+                       int             frac = (ResvSize * 6) / 10;
+
+                       add_bits = MIN(frac, more_bits);
+               }
+               else
+                       add_bits = 0;
+
+               over_bits = ResvSize - ((ResvMax * 8) / 10) - add_bits;
+               if (over_bits > 0)
+                       add_bits += over_bits;
+
+               max_bits += add_bits;
+       }
+
+       if (max_bits > 4095)
+               max_bits = 4095;
+
+       return max_bits;
+}
+
+
+
+
+
+/*
+       ResvAdjust:
+       Called after a granule's bit allocation. Readjusts the size of
+       the reservoir to reflect the granule's usage.
+*/
+void                                   ResvAdjust
+(
+       frame_params                    *fr_ps,
+       gr_info                                 *cod_info,
+       III_side_info_t                 *l3_side,
+       int                                             mean_bits
+)
+{
+       ResvSize += (mean_bits / fr_ps->stereo) - cod_info->part2_3_length;
+}
+
+
+
+
+
+/*
+       ResvFrameEnd:
+       Called after all granules in a frame have been allocated. Makes sure
+       that the reservoir size is within limits, possibly by adding stuffing
+       bits. Note that stuffing bits are added by increasing a granule's
+       part2_3_length. The bitstream formatter will detect this and write the
+       appropriate stuffing bits to the bitstream.
+*/
+void                                   ResvFrameEnd
+(
+       frame_params                    *fr_ps,
+       III_side_info_t                 *l3_side,
+       int                                             mean_bits
+)
+{
+       layer                                   *info;
+       gr_info                                 *cod_info;
+       int                                             mode_gr, gr, ch, stereo, ancillary_pad, stuffingBits;
+       int                                             over_bits;
+
+       info    = fr_ps->header;
+       stereo  = fr_ps->stereo;
+       mode_gr = 2;
+
+       ancillary_pad = 0;
+
+       /* just in case mean_bits is odd, this is necessary... */
+       if ((stereo == 2)  &&  (mean_bits & 1))
+               ResvSize ++;
+
+       stuffingBits = ancillary_pad;
+       
+       if ((over_bits = ResvSize - ResvMax) > 0)
+       {
+               stuffingBits += over_bits;
+               ResvSize     -= over_bits;
+       }
+
+       /* we must be byte aligned */
+       if ((over_bits = ResvSize % 8) != 0)
+       {
+               stuffingBits += over_bits;
+               ResvSize     -= over_bits;
+       }
+
+       if (stuffingBits)
+       {
+               /*
+                       plan a: put all into the first granule
+                       This was preferred by someone designing a
+                       real-time decoder...
+               */
+               cod_info = &l3_side->gr[0].ch[0].tt;
+
+               if (cod_info->part2_3_length + stuffingBits < 4095)
+                       cod_info->part2_3_length += stuffingBits;
+               else
+               {
+                   /* plan b: distribute throughout the granules */
+                   for (gr = 0;  gr < mode_gr;  gr++)
+                   {
+                               for (ch = 0;  ch < stereo;  ch++)
+                               {
+                                       int                     extraBits, bitsThisGr;
+                                       gr_info         *cod_info = &l3_side->gr[gr].ch[ch].tt;
+
+                                       if (stuffingBits == 0)
+                                               break;
+                                       extraBits = 4095 - cod_info->part2_3_length;
+                                       bitsThisGr = (extraBits < stuffingBits) ? extraBits : stuffingBits;
+                                       cod_info->part2_3_length += bitsThisGr;
+                                       stuffingBits -= bitsThisGr;
+                               }
+                       }
+                   /*
+                               If any stuffing bits remain, we elect to spill them
+                               into ancillary data. The bitstream formatter will do this if
+                               l3side->resvDrain is set
+                   */
+                   l3_side->resvDrain = stuffingBits;
+               }
+       }
+}
diff --git a/lib/bladeenc/reservoir.h b/lib/bladeenc/reservoir.h
new file mode 100644 (file)
index 0000000..4903e2c
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       reformatted
+*/
+
+/**********************************************************************
+ * ISO MPEG Audio Subgroup Software Simulation Group (1996)
+ * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
+ *
+ * $Id: reservoir.h,v 1.1 2002/01/10 17:30:01 kramm Exp $
+ *
+ * $Log: reservoir.h,v $
+ * Revision 1.1  2002/01/10 17:30:01  kramm
+ * Version 0.94.1 of the bladeenc mp3 encoder
+ *
+ * Revision 1.1  1996/02/14 04:04:23  rowlands
+ * Initial revision
+ *
+ * Received from Mike Coleman
+ **********************************************************************/
+/*
+  Revision History:
+
+  Date        Programmer                Comment
+  ==========  ========================= ===============================
+  1995/09/06  mc@fivebats.com           created
+
+*/
+
+#ifndef                __RESERVOIR__
+#define                __RESERVOIR__
+
+
+
+
+
+void                                   ResvFrameBegin
+(
+       frame_params                    *fr_ps,
+       III_side_info_t                 *l3_side,
+       int                                             mean_bits,
+       int                                             frameLength
+);
+
+int                                            ResvMaxBits
+(
+       frame_params                    *fr_ps,
+       III_side_info_t                 *l3_side,
+       double                                  *pe,
+       int                                             mean_bits
+);
+
+void                                   ResvAdjust
+(
+       frame_params                    *fr_ps,
+       gr_info                                 *cod_info,
+       III_side_info_t                 *l3_side,
+       int                                             mean_bits
+);
+
+void                                   ResvFrameEnd
+(
+       frame_params                    *fr_ps,
+       III_side_info_t                 *l3_side,
+       int                                             mean_bits
+);
+
+void                                   fixStatic_reservoir (void);
+
+
+
+
+
+#endif         /* __RESERVOIR__ */
diff --git a/lib/bladeenc/subs.c b/lib/bladeenc/subs.c
new file mode 100644 (file)
index 0000000..36b53ac
--- /dev/null
@@ -0,0 +1,300 @@
+/*
+                       (c) Copyright 1998-2001 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-11-04  Andre Piotrowski
+
+       -       speed up: don«t calculate the unneeded phi values!
+
+       2000-11-22      ap
+
+       -       speed up: slightly improved way to handle the 'special case'
+
+       2000-12-05  ap
+
+       -       speed up: implemented prepacking
+
+       2000-12-11  ap
+
+       -       speed up: avoid data reordering
+
+       2000-12-12  ap
+
+       -       moved fft configuration switches to "l3psy.h"
+
+       2001-01-12  ap
+
+       -       use some explicit type casting to avoid compiler warnings
+*/
+
+/*****************************************************************************
+ * FFT computes fast fourier transform of BLKSIZE samples of data            *
+ *   based on the decimation-in-frequency algorithm described in "Digital    *
+ *   Signal Processing" by Oppenheim and Schafer, refer to pages 304         *
+ *   (flow graph) and 330-332 (Fortran program in problem 5).                *
+ *                                                                           *
+ *   required constants:                                                     *
+ *         PI          3.14159265358979                                      *
+ *         BLKSIZE     should be 2^^M for a positive integer M               *
+ *                                                                           *
+ *****************************************************************************/
+
+#include       "common.h"
+#include       "encoder.h"
+#include       "l3psy.h"
+
+
+
+
+
+/* The switches have been moved to "l3psy.h" */
+
+
+
+
+
+int                                            fInit_fft;
+
+
+
+
+
+void fft (FLOAT x_real[], FLOAT x_imag[], FLOAT energy[], FLOAT phi[], int N)
+{
+       int                                             i, j, k, off;
+#if USED_VALUES_ONLY
+       int                                             min_i, max_i;
+#endif
+       int                                             ip, le, le1;
+       static double                   w_real[BLKSIZE/2], w_imag[BLKSIZE/2];
+       double                                  t_real, t_imag, u_real, u_imag;
+       int                                             N_ORG;
+
+#if !REORDER_DATA
+       static  int                             swap_l[BLKSIZE/2+1];
+       static  int                             swap_s[BLKSIZE_s/2+1];
+       int                                             *pSwap, a, b;
+#endif
+
+       double  t1, t2, t3, t4, t5, t6;
+
+       if (fInit_fft == 0)
+       {
+               for (i = 0;  i < BLKSIZE/2;  i++)
+               {
+                       w_real[i] =  cos(PI*i/(BLKSIZE/2));
+#if NORMAL_FFT
+                       w_imag[i] = -sin(PI*i/(BLKSIZE/2));
+#else
+                       w_imag[i] =  sin(PI*i/(BLKSIZE/2));
+#endif
+               }
+
+#if !REORDER_DATA
+               j = 0;
+               for (i = 0;  i < BLKSIZE/2-1;  i++)
+               {
+                       swap_l[i] = j;  k = BLKSIZE/4;  while (k <= j) {j -= k;  k >>= 1;}  j += k;
+               }
+               swap_l[i] = i;  swap_l[i+1] = i+1;
+
+               j = 0;
+               for (i = 0;  i < BLKSIZE_s/2-1;  i++)
+               {
+                       swap_s[i] = j;  k = BLKSIZE_s/4;  while (k <= j) {j -= k;  k >>= 1;}  j += k;
+               }
+               swap_s[i] = i;  swap_s[i+1] = i+1;
+#endif
+
+               fInit_fft++;
+       }
+
+
+#if REAL_SEQUENCE
+       N_ORG = N;
+       N >>= 1;
+#if !PREPACKED
+       /* packing the sequence to the half length */
+       for (i = 0;  i < N;  i++)
+       {
+               x_real[i] = x_real[2*i];
+               x_imag[i] = x_real[2*i+1];
+       }
+#endif
+#endif
+
+
+       off = BLKSIZE/N;
+       for (le = N;  le > 1;  le = le1)
+       {
+               le1 = le >> 1;
+
+                       /* special case: k=0; u_real=1.0; u_imag=0.0; j=0 */
+                       for (i = 0;  i < N;  i += le)
+                       {
+                               ip = i + le1;
+                               t_real = x_real[i] - x_real[ip];
+                               t_imag = x_imag[i] - x_imag[ip];
+                               x_real[i] += x_real[ip];
+                               x_imag[i] += x_imag[ip];
+                               x_real[ip] = (FLOAT) t_real;
+                               x_imag[ip] = (FLOAT) t_imag;
+                       }
+
+               k = off;
+               for (j = 1;  j < le1;  j++)
+               {
+                       u_real = w_real[k];
+                       u_imag = w_imag[k];
+                       for (i = j;  i < N;  i += le)
+                       {
+                               ip = i + le1;
+                               t_real = x_real[i] - x_real[ip];
+                               t_imag = x_imag[i] - x_imag[ip];
+                               x_real[i] += x_real[ip];
+                               x_imag[i] += x_imag[ip];
+                               x_real[ip] = (FLOAT) (t_real*u_real - t_imag*u_imag);
+                               x_imag[ip] = (FLOAT) (t_imag*u_real + t_real*u_imag);
+                       }
+                       k += off;
+               }
+
+               off += off;
+       }
+
+
+#if REORDER_DATA
+       /* this section reorders the data to the correct ordering */
+       j = 0;
+       for (i = 0;  i < N-1;  i++)
+       {
+               if (i < j)
+               {
+                       t_real = x_real[j];
+                       t_imag = x_imag[j];
+                       x_real[j] = x_real[i];
+                       x_imag[j] = x_imag[i];
+                       x_real[i] = (FLOAT) t_real;
+                       x_imag[i] = (FLOAT) t_imag;
+               }
+               k=N/2;
+               while (k <= j)
+               {
+                       j -= k;
+                       k >>= 1;
+               }
+               j += k;
+       }
+#else
+       /*
+               We don«t reorder the data to the correct ordering,
+               but access the data by the bit reverse order index array.
+       */
+       pSwap = (N_ORG == BLKSIZE) ? swap_l : swap_s;
+#endif
+
+
+#if REAL_SEQUENCE
+       /* unpacking the sequence */
+       t_real = x_real[0];
+       t_imag = x_imag[0];
+       x_real[0] = (FLOAT) (t_real+t_imag);
+       x_imag[0] = 0.0;
+       x_real[N] = (FLOAT) (t_real-t_imag);
+       x_imag[N] = 0.0;
+
+       k = off = BLKSIZE/N_ORG;
+       for (i = 1;  i < N/2;  i++)
+       {
+#if REORDER_DATA
+#define                a       i
+#define                b       (N-i)
+#else
+               a = pSwap[i];
+               b = pSwap[N-i];
+#endif
+               t1 = x_real[a] + x_real[b];
+               t2 = x_real[a] - x_real[b];
+               t3 = x_imag[a] + x_imag[b];
+               t4 = x_imag[a] - x_imag[b];
+               t5 = t2*w_imag[k] + t3*w_real[k];
+               t6 = t3*w_imag[k] - t2*w_real[k];
+
+               x_real[a] = (FLOAT) (t1+t5) / 2.0;
+               x_imag[a] = (FLOAT) (t6+t4) / 2.0;
+               x_real[b] = (FLOAT) (t1-t5) / 2.0;
+               x_imag[b] = (FLOAT) (t6-t4) / 2.0;
+
+               k += off;
+       }
+       /* x_real[N/2] doesn«t change */
+       /* x_real[N/2] changes the sign in case of a normal fft */
+#if (NORMAL_FFT)
+#if REORDER_DATA
+       x_imag[i] = -x_imag[i];
+#else
+       x_imag[pSwap[i]] *= -1.0;
+#endif   /* REORDER_DATA */
+#endif   /* NORMAL_FFT */
+       N = N_ORG;
+#endif   /* REAL_SEQUENCE */
+
+
+       /* calculating the energy and phase, phi */
+#if USED_VALUES_ONLY
+       if (N == BLKSIZE)
+       {
+               min_i = LONG_FFT_MIN_IDX;
+               max_i = LONG_FFT_MAX_IDX;
+       }
+       else
+       {
+               min_i = SHORT_FFT_MIN_IDX;
+               max_i = SHORT_FFT_MAX_IDX;
+       }
+#endif
+#if REAL_SEQUENCE
+       for (i = 0;  i <= N/2;  i++)
+#else
+       for (i = 0;  i < N;  i++)
+#endif
+       {
+#if REORDER_DATA
+#define                a       i
+#else
+               a = pSwap[i];
+#endif
+               energy[i] = x_real[a]*x_real[a] + x_imag[a]*x_imag[a];
+               if(energy[i] <= 0.0005)
+               {
+                       energy[i] = (FLOAT) 0.0005;        /* keep the identity */
+                       x_real[a] = (FLOAT) sqrt(0.0005);  /* energy[i] * cos(phi[i]) == x_real[i] */
+                       x_imag[a] = 0.0;                   /* energy[i] * sin(phi[i]) == x_imag[i] */
+               }
+#if USED_VALUES_ONLY
+               if (i >= min_i  &&  i <= max_i)
+#endif
+               phi[i] = (FLOAT) atan2((double) x_imag[a], (double) x_real[a]);
+       }
+}
+
+
+
diff --git a/lib/bladeenc/system.h b/lib/bladeenc/system.h
new file mode 100644 (file)
index 0000000..cdfbc8a
--- /dev/null
@@ -0,0 +1,330 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression.
+
+               This file doesn't contain any of the ISO reference code and
+               is copyright Tord Jansson (tord.jansson@swipnet.se).
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+       Use TABSIZE 4!!!
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-11  Andre Piotrowski
+
+       -       added autodetection for Mac OS
+
+       2001-01-19      Tord Jansson
+
+       -       Added Andrea Vallinotto's Amiga WarpOS changes.
+       -       Added some more defines and cleaned up.
+
+*/
+
+#ifndef                __SYSTEM__
+#define                __SYSTEM__
+
+
+
+/*==== THE SYSTEMS WE KNOW OF AND CAN AUTODETECT ==========================*/
+
+#define                UNIX_AUTOCONF                   1   /* All well behaving UNIX systems where you run configure/make */
+                                                                           /* Includes Linux, *BSD, Solaris, IRIX, UnixWare, SCO Open Server, AIX, HPUX */
+
+#define                WIN32_INTEL                             2   /* Windows 95/98/NT/2k etc on i386 compatible machines */
+
+#define                BEOS                                    3   /* BeOS, version 4.5+ on i386 and PPC */
+
+#define                MSDOS_DJGPP                             4   /* MS-DOS protected mode using DJGPP */
+
+#define                MAC_OS                                  5   /* To compile the Mac version you also need Petteri Kamppuri's wrapper */
+
+
+
+/*==== SYSTEMS WE SUPPORT BUT DON'T KNOW HOW TO AUTODETECT YET ==============*/
+/*====       THESE NEEDS TO BE SET EXPLICITLY BELOW!!!         ==============*/
+
+
+#define                WIN32_ALPHA                             6   /* Windows NT on DEC/Compaq Alpha architectures */
+            
+#define                IBM_OS2                                 7
+
+#define                ATARI_TOS                               8
+
+#define                AMIGA_WOS                               9       /* Amiga WarpOS */
+
+
+/*= UNCOMMENT LINE AND SET SYSTEM HERE IF YOUR SYSTEM CAN'T BE AUTODETECTED! */
+/*===========================================================================*/
+/*#define              SYSTEM          MAC_OS*/
+
+/*==== SYSTEM AUTODETECTION PROCEDURE ========================*/
+
+#ifndef        SYSTEM
+#      ifdef   WIN32
+#              define          SYSTEM          WIN32_INTEL             
+#      endif
+
+#      ifdef  __BEOS__
+#              define          SYSTEM          BEOS
+#      endif
+
+#      ifdef __DJGPP__
+#       define         SYSTEM      MSDOS_DJGPP
+#      endif
+
+#      if      defined(__dest_os) && (__dest_os == __mac_os)   /* defined in "ansi_prefix.mac.h" */
+#              define          SYSTEM          MAC_OS
+#      endif
+
+#      ifndef  SYSTEM
+#              define          SYSTEM          UNIX_AUTOCONF   /* Take UNIX for granted... */
+#      endif
+
+#endif
+
+
+
+/*==== Low level defines ======================================================*/
+/*
+  LIST OF DEFINES
+  ===============
+
+  BYTEORDER    [byteorder]     Should either be set to BIG_ENDIAN or LITTLE_ENDIAN
+                                                        depending on the processor.
+
+  DRAG_DROP                 Set if Drag-n-Drop operations are supported. If
+                                                        defined, the hint for drag and drop is displayed
+                                                        in the help text.
+
+  PRIO                      Set if priority can be set with the -PRIO switch
+                                                        (UNIX_SYSTEM enables this automatically).
+
+  MSWIN                     Set this for MSWin systems. Includes "windows.h" etc.
+
+  WILDCARDS                 Set this if the program has to expand wildcards
+                                                        itself on your system.
+
+  DIRECTORY_SEPARATOR          Should either be '\\' or '/'.
+                                                        (UNIX_SYSTEM automatically gives you '/').
+
+  WAIT_KEY                                     Set this on systems where we as default want to
+                                                        wait for a keypress before quiting.
+
+  UNIX_SYSTEM                          Set this for UNIX-systems that are posix and
+                                                        (at least to some extent) bsd compliant to enable
+                                                        stuff like config-file support, priority settings etc.
+
+  PAUSE_25_LINES                       Wait for keypress after 25 lines of output when
+                                                        listing important information.
+*/
+
+
+/*  Most systems allready have these two defines, but some doesn't 
+    so we have to put them here, before they are used. */
+
+#ifndef BIG_ENDIAN
+#      define                  BIG_ENDIAN                              4321
+#endif
+
+#ifndef LITTLE_ENDIAN
+#      define                  LITTLE_ENDIAN                   1234
+#endif
+
+
+/*____ UNIX with Autoconf support _____________________________________________*/
+
+#if            SYSTEM == UNIX_AUTOCONF
+#      ifdef BYTE_ORDER
+#              define                  BYTEORDER                               BYTE_ORDER
+#      else
+#              include <config.h>
+#              if WORDS_BIGENDIAN
+#                      define          BYTEORDER                               BIG_ENDIAN
+#              else
+#                      define          BYTEORDER                               LITTLE_ENDIAN
+#              endif
+#      endif
+#      define                  UNIX_SYSTEM
+#endif
+
+/*_____ Windows 95/98/NT Intel defines ________________________________________*/
+
+#if     SYSTEM == WIN32_INTEL
+
+#              define                  BYTEORDER                               LITTLE_ENDIAN
+#              define                  DRAG_DROP
+#              define                  PRIO
+#              define                  MSWIN
+#              define                  WILDCARDS
+#              define                  DIRECTORY_SEPARATOR             '\\'
+#              define                  WAIT_KEY
+#              define                  PAUSE_25_LINES
+#endif
+
+/*_____ Windows NT DEC Alpha defines __________________________________________*/
+
+#if SYSTEM == WIN32_ALPHA
+
+#              define                  BYTEORDER                               LITTLE_ENDIAN
+#              define                  DRAG_DROP
+#              define                  PRIO
+#              define                  MSWIN
+#              define                  WILDCARDS
+#              define                  DIRECTORY_SEPARATOR             '\\'
+#              define                  WAIT_KEY
+#              define                  PAUSE_25_LINES
+#endif
+
+
+/*____ OS/2 _________________________________________________________________*/
+
+#if SYSTEM == IBM_OS2
+
+#              define                  BYTEORDER                               LITTLE_ENDIAN
+#              define                  PRIO
+#              define                  WILDCARDS
+#              define                  DIRECTORY_SEPARATOR             '\\'
+#              define                  OS2
+#              define                  WAIT_KEY
+#              define                  PAUSE_25_LINES
+#endif
+
+
+/*____ Atari TOS ____________________________________________________________*/
+
+#if SYSTEM == ATARI_TOS
+
+#              define                  BYTEORDER                               BIG_ENDIAN
+#              define                  DIRECTORY_SEPARATOR             '\\'
+#              define                  TOS
+#              define                  PAUSE_25_LINES
+#endif
+
+/*____ MSDOS_DJGPP __________________________________________________________*/
+
+#if SYSTEM == MSDOS_DJGPP
+
+#              define          BYTEORDER                               LITTLE_ENDIAN
+#              define                  DIRECTORY_SEPARATOR             '\\'
+#endif
+
+/*____ MAC_OS ________________________________________________________________*/
+
+#if SYSTEM == MAC_OS
+
+#              define                  BYTEORDER                               BIG_ENDIAN
+#              define                  DIRECTORY_SEPARATOR             ':'
+#endif
+
+/*____ BeOS PowerPC & x86 ___________________________________________________*/
+
+#if SYSTEM == BEOS
+
+#      ifdef __i386__
+#              define                  BYTEORDER                               LITTLE_ENDIAN
+#      else
+#              define                  BYTEORDER                               BIG_ENDIAN
+#      endif
+#              define                  DIRECTORY_SEPARATOR             '/'
+#endif
+
+/*____ Amiga PowerPC WarpOS / VBCC or Storm-GCC_______________________________*/
+
+#if SYSTEM == AMIGA_WOS
+#      define                          BYTEORDER              BIG_ENDIAN
+#      define                          DIRECTORY_SEPARATOR     '/'
+#      define                          SECOND_DIRECTORY_SEPARATOR     ':'      /* Special case for volumes */
+#      define                          PRIO                                                            /* Yes, it can be done */
+#      define                          WILDCARDS                                                       /* Uses dos/ParsePatternNoCase() and friends.*/
+#      define                          PROTO_ARGS                                                      /* Undocumented, it's useful for GCC-Storm */
+#      ifdef __VBCC__
+#              pragma amiga-align
+#      endif
+#      include <dos/dos.h>
+#      ifdef __VBCC__
+#              pragma default-align
+#      endif
+
+#endif
+
+
+
+/*____ Some defines automatically gives other...*/
+
+#ifdef UNIX_SYSTEM
+#      define                  PRIO
+#      define                  DIRECTORY_SEPARATOR             '/'
+#endif
+
+
+/*==== COMPILER SPECIFIC DEFINES ============================================ */
+/*
+       Unfortunately the inline command differs slightly between compilers (for
+       example GCC calls it 'inline' while VC++ calls it '_inline'), so we have
+       to use our own define for it to keep everything compiler independent.
+
+       Inline just speeds things up, so if we don't recognice the architecture we
+       just define an empty INLINE and take a minor performance hit.
+
+*/
+
+#ifdef __GNUC__                                                                                        /* GNU C and family */
+#              define                  INLINE                                  inline
+#endif
+
+#ifdef __TURBOC__                                                                                      /* Borland Turbo C, Turbo C++, and Borland C++ */
+#              define                  INLINE                                  __inline
+#endif
+
+#ifdef _MSC                                                                                            /* Visual C++ */
+#              define                  INLINE                                  _inline         
+#endif
+
+#ifdef __STORM
+#              define                  INLINE                                  __inline        /* Storm C4 */
+#endif
+
+#ifdef __VBCC__
+#              define                  INLINE                                                  /* VBCC */
+#endif
+
+#ifndef        INLINE
+#              define                  INLINE                                                          /* Empty construct default, minor performance hit. */
+#endif
+
+
+/*____ To make sure that certain necessary defines are set... */
+
+#ifndef FALSE
+#      define         FALSE           0
+#endif
+
+#ifndef TRUE
+#      define         TRUE            1
+#endif
+
+/*==== Other Global Definitions, placed here for convenience ==================*/
+
+/*#define                      PRECISE_TIMER*/         /* Gives more accurate speed calculations, */
+                                                                               /* just for debug purposes. Disable in release version! */
+
+typedef                unsigned        char    uchar;
+
+#if (defined(UNIX_SYSTEM) && !defined(SYS_TYPES_H) && !defined(_SYS_TYPES_H)) || (!defined UNIX_SYSTEM  && !defined(__GNUC__))
+               typedef         unsigned short  ushort;
+               typedef         unsigned int    uint;
+#endif
+
+
+#endif         /* __SYSTEM__ */
diff --git a/lib/bladeenc/tables.c b/lib/bladeenc/tables.c
new file mode 100644 (file)
index 0000000..4f33a1e
--- /dev/null
@@ -0,0 +1,1614 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-11-06  Andre Piotrowski
+
+       -       speed up: (for 'encode.c') traversed 'enwindow[]' needed for 'WIND_SB_CHANGE_LEVEL 3'
+*/
+
+#include       "tables.h"
+#include       "huffman.h"
+
+
+
+
+/*     ========================================================================================  */
+/*      L3 parameter tables                                                                   */
+/*     ========================================================================================  */
+
+
+
+
+
+#if NEW_L3PARM_TABLES
+
+l3_parm_block                  l3_parm[3] =
+{
+
+       {       /* sfreq_idx_44100 */
+
+               {       /* psy_longBlock_44100_62 */
+
+                       63, /* cbmax_l */
+
+                       {       /* numlines_l */
+                                 1,  1,  1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  1,  2,  2,  2,
+                                 2,  2,  2,  2,  2,  2,  2,  2,  2,
+                                 2,  3,  3,  3,  3,  3,  3,  3,  4,
+                                 4,  4,  5,  5,  5,  6,  6,  7,  7,
+                                 7,  9,  9,  9, 10, 10, 11, 14, 14,
+                                15, 20, 20, 21, 27, 27, 36, 73, 18
+                       },
+                       {       /* minval */
+                                24.5, 24.5, 24.5, 24.5, 24.5, 20.0, 20.0, 20.0, 20.0,
+                                20.0, 20.0, 20.0, 18.0, 18.0, 18.0, 12.0,  6.0,  6.0,
+                                 3.0,  3.0,  3.0,  3.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
+                       },
+                       {       /* qthr_l */
+                                  4.532,   4.532,   4.532,   0.904,   0.904,   0.090,   0.090,   0.029,   0.029,
+                                  0.029,   0.009,   0.009,   0.009,   0.009,   0.009,   0.018,   0.018,   0.018,
+                                  0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,
+                                  0.018,   0.027,   0.027,   0.027,   0.027,   0.027,   0.027,   0.027,   0.036,
+                                  0.036,   0.036,   0.045,   0.045,   0.045,   0.054,   0.054,   0.063,   0.063,
+                                  0.063,   0.081,   0.081,   0.081,   0.180,   0.180,   0.198,   0.400,   0.400,
+                                  0.429,   1.808,   1.808,   1.898,  24.416,  24.416,  32.554, 660.124, 162.770
+                       },
+
+#if !CALC_NORM   /* do we compute more exact values? */
+                       {       /* norm_l */
+                                0.951, 0.700, 0.681, 0.675, 0.667, 0.665, 0.664, 0.664, 0.664,
+                                0.655, 0.616, 0.597, 0.578, 0.541, 0.575, 0.856, 0.846, 0.840,
+                                0.822, 0.800, 0.753, 0.704, 0.674, 0.640, 0.609, 0.566, 0.535,
+                                0.531, 0.615, 0.686, 0.650, 0.612, 0.567, 0.520, 0.513, 0.557,
+                                0.584, 0.570, 0.579, 0.585, 0.548, 0.536, 0.550, 0.532, 0.504,
+                                0.496, 0.517, 0.527, 0.516, 0.497, 0.489, 0.502, 0.501, 0.491,
+                                0.497, 0.504, 0.504, 0.495, 0.486, 0.484, 0.483, 0.475, 0.515
+                       },
+#endif
+                       {       /* bval_l */
+                                 0.000,  0.431,  0.861,  1.292,  1.723,  2.153,  2.584,  3.015,  3.445,
+                                 3.876,  4.279,  4.670,  5.057,  5.416,  5.774,  6.422,  7.026,  7.609,
+                                 8.168,  8.710,  9.207,  9.662, 10.099, 10.515, 10.917, 11.293, 11.652,
+                                11.997, 12.394, 12.850, 13.277, 13.681, 14.062, 14.411, 14.751, 15.119,
+                                15.508, 15.883, 16.263, 16.654, 17.020, 17.374, 17.744, 18.104, 18.447,
+                                18.782, 19.130, 19.487, 19.838, 20.179, 20.510, 20.852, 21.196, 21.531,
+                                21.870, 22.214, 22.558, 22.898, 23.232, 23.564, 23.897, 24.229, 24.442 
+                       }
+
+               },
+
+               {       /* psy_shortBlock_44100_38 */
+
+                       39, /* cbmax_s */
+
+                       {       /* numlines_s */
+                                 1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  2,  2,
+                                 2,  2,  2,  2,  3,  3,  3,
+                                 3,  3,  4,  4,  5,  5,  5,
+                                 7,  7,  7, 19,  0,  0,  0
+                       },
+                       {       /* qthr_s */
+                                  4.532,   0.904,   0.029,   0.009,   0.009,   0.009,   0.009,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.009,   0.009,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.018,   0.018,
+                                  0.018,   0.018,   0.018,   0.018,   0.027,   0.027,   0.054,
+                                  0.054,   0.054,   0.114,   0.114,   0.452,   0.452,   0.452,
+                                  6.330,   6.330,   6.330, 171.813,   0.000,   0.000,   0.000
+                       },
+#if !CALC_NORM   /* do we compute more exact values? */
+                       {       /* norm_s */
+                                1.000, 0.983, 0.983, 0.982, 0.985, 0.983, 0.978,
+                                0.967, 0.948, 0.930, 0.914, 0.870, 0.845, 0.800,
+                                0.749, 0.701, 0.653, 0.590, 0.616, 0.860, 0.823,
+                                0.762, 0.688, 0.612, 0.594, 0.658, 0.706, 0.660,
+                                0.606, 0.565, 0.560, 0.579, 0.567, 0.534, 0.514,
+                                0.520, 0.518, 0.507, 0.447, 0.000, 0.000, 0.000
+                       },
+#endif
+                       {       /* SNR_s */
+                                -8.240, -8.240, -8.240, -8.240, -8.240, -8.240, -8.240,
+                                -8.240, -8.240, -8.240, -7.447, -7.447, -7.447, -7.447,
+                                -7.447, -7.447, -7.447, -7.447, -7.447, -7.447, -6.990,
+                                -6.990, -6.990, -6.990, -6.990, -6.990, -6.990, -6.990,
+                                -6.990, -6.990, -6.020, -6.020, -6.020, -6.020, -5.229,
+                                -5.229, -5.229, -5.229, -4.559,  0.000,  0.000,  0.000
+                       },
+                       {       /* bval_s */
+                                 0.000,  1.723,  3.445,  5.057,  6.422,  7.609,  8.710,
+                                 9.662, 10.515, 11.293, 12.009, 12.625, 13.210, 13.748,
+                                14.241, 14.695, 15.125, 15.508, 15.891, 16.537, 17.112,
+                                17.621, 18.073, 18.470, 18.849, 19.271, 19.741, 20.177,
+                                20.576, 20.950, 21.316, 21.699, 22.078, 22.438, 22.782,
+                                23.133, 23.484, 23.828, 24.173,  0.000,  0.000,  0.000
+                       }
+
+               },
+
+               {       /* psy_data3_44100_20 */
+
+                       {       /* cbw_l */
+                                 3,  3,  4,  3,  3,  1,  3,
+                                 3,  3,  3,  3,  3,  4,  3,
+                                 3,  3,  3,  3,  2,  2,  2
+                       },
+                       {       /* bu_l */
+                                 0,  4,  7, 11, 14, 17, 18,
+                                21, 24, 27, 30, 33, 36, 40,
+                                43, 46, 49, 52, 55, 57, 59
+                       },
+                       {       /* bo_l */
+                                 4,  7, 11, 14, 17, 18, 21,
+                                24, 27, 30, 33, 36, 40, 43,
+                                46, 49, 52, 55, 57, 59, 61
+                       },
+                       {       /* w1_l */
+                                1.000, 0.944, 0.389, 0.833, 0.278, 0.861, 0.083,
+                                0.417, 0.750, 0.194, 0.426, 0.463, 0.180, 0.900,
+                                0.532, 0.376, 0.550, 0.448, 0.597, 0.357, 0.278
+                       },
+                       {       /* w2_l */
+                                0.056, 0.611, 0.167, 0.722, 0.139, 0.917, 0.583,
+                                0.250, 0.805, 0.574, 0.537, 0.819, 0.100, 0.468,
+                                0.623, 0.450, 0.552, 0.403, 0.643, 0.722, 0.960
+                       }
+
+               },
+
+               {       /* psy_data4_44100_11 */
+
+                       {       /* cbw_s */
+                                 2,  2,  3,  3,  4,  5,
+                                 3,  4,  3,  3,  3,  2
+                       },
+                       {       /* bu_s */
+                                 0,  3,  5,  8, 11, 15,
+                                20, 23, 27, 30, 33, 36
+                       },
+                       {       /* bo_s */
+                                 2,  5,  8, 11, 15, 20,
+                                23, 27, 30, 33, 36, 38
+                       },
+                       {       /* w1_s */
+                                1.000, 0.833, 0.167, 0.500, 0.833, 0.833,
+                                0.750, 0.417, 0.944, 0.625, 0.700, 0.833
+                       },
+                       {       /* w2_s */
+                                0.167, 0.833, 0.500, 0.167, 0.167, 0.250,
+                                0.583, 0.055, 0.375, 0.300, 0.167, 1.000
+                       }
+
+               }
+
+       },
+
+       {       /* sfreq_idx_48000 */
+
+               {       /* psy_longBlock_48000_61 */
+
+                       62, /* cbmax_l */
+
+                       {       /* numlines_l */
+                                 1,  1,  1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  1,  1,  2,  2,
+                                 2,  2,  2,  2,  2,  2,  2,  2,  2,
+                                 2,  2,  3,  3,  3,  3,  3,  4,  4,
+                                 4,  4,  4,  5,  5,  5,  6,  7,  7,
+                                 8,  8,  8, 10, 10, 10, 13, 13, 14,
+                                18, 18, 20, 25, 25, 35, 67, 67,  0
+                       },
+                       {       /* minval */
+                                24.5, 24.5, 24.5, 24.5, 24.5, 20.0, 20.0, 20.0, 20.0,
+                                20.0, 20.0, 18.0, 18.0, 18.0, 12.0, 12.0,  6.0,  6.0,
+                                 3.0,  3.0,  3.0,  3.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
+                       },
+                       {       /* qthr_l */
+                                  4.532,   4.532,   4.532,   0.904,   0.904,   0.090,   0.090,   0.029,   0.029,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.009,   0.009,   0.018,   0.018,
+                                  0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,
+                                  0.018,   0.018,   0.027,   0.027,   0.027,   0.027,   0.027,   0.036,   0.036,
+                                  0.036,   0.036,   0.036,   0.045,   0.045,   0.045,   0.054,   0.063,   0.063,
+                                  0.072,   0.072,   0.072,   0.180,   0.180,   0.180,   0.372,   0.372,   0.400,
+                                  1.627,   1.627,   1.808,  22.607,  22.607,  31.650, 605.867, 605.867,   0.000
+                       },
+#if !CALC_NORM   /* do we compute more exact values? */
+                       {       /* norm_l */
+                                0.970, 0.755, 0.738, 0.730, 0.724, 0.723, 0.723, 0.723, 0.718,
+                                0.690, 0.660, 0.641, 0.600, 0.584, 0.532, 0.537, 0.857, 0.858,
+                                0.853, 0.824, 0.778, 0.740, 0.709, 0.676, 0.632, 0.592, 0.553,
+                                0.510, 0.513, 0.608, 0.673, 0.637, 0.586, 0.571, 0.616, 0.640,
+                                0.598, 0.538, 0.512, 0.528, 0.517, 0.493, 0.499, 0.525, 0.541,
+                                0.528, 0.510, 0.506, 0.525, 0.536, 0.518, 0.501, 0.497, 0.497,
+                                0.495, 0.494, 0.497, 0.494, 0.487, 0.483, 0.482, 0.524, 0.000
+                       },
+#endif
+                       {       /* bval_l */
+                                 0.000,  0.469,  0.938,  1.406,  1.875,  2.344,  2.813,  3.281,  3.750,
+                                 4.199,  4.625,  5.047,  5.438,  5.828,  6.188,  6.522,  7.174,  7.801,
+                                 8.402,  8.966,  9.484,  9.966, 10.426, 10.866, 11.279, 11.669, 12.042,
+                                12.386, 12.721, 13.115, 13.562, 13.984, 14.371, 14.741, 15.140, 15.563,
+                                15.962, 16.324, 16.665, 17.020, 17.373, 17.708, 18.045, 18.398, 18.762,
+                                19.120, 19.466, 19.807, 20.159, 20.522, 20.874, 21.214, 21.553, 21.892,
+                                22.231, 22.569, 22.909, 23.248, 23.583, 23.915, 24.246, 24.576,  0.000
+                       }
+
+               },
+
+               {       /* psy_shortBlock_48000_37 */
+
+                       38, /* cbmax_s */
+
+                       {       /* numlines_s */
+                                 1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  2,  2,
+                                 2,  2,  2,  2,  2,  3,  3,
+                                 3,  4,  4,  5,  5,  5,  7,
+                                 7, 11, 17,  0,  0,  0,  0
+                       },
+                       {       /* qthr_s */
+                                  4.532,   0.904,   0.029,   0.009,   0.009,   0.009,   0.009,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.009,   0.009,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.018,   0.018,
+                                  0.018,   0.018,   0.018,   0.018,   0.018,   0.054,   0.054,
+                                  0.054,   0.114,   0.114,   0.452,   0.452,   0.452,   6.330,
+                                  6.330,   9.947, 153.727,   0.000,   0.000,   0.000,   0.000
+                       },
+#if !CALC_NORM   /* do we compute more exact values? */
+                       {       /* norm_s */
+                                1.000, 0.989, 0.989, 0.981, 0.985, 0.984, 0.980,
+                                0.968, 0.954, 0.929, 0.906, 0.883, 0.844, 0.792,
+                                0.747, 0.689, 0.644, 0.592, 0.553, 0.850, 0.811,
+                                0.736, 0.665, 0.610, 0.544, 0.528, 0.621, 0.673,
+                                0.635, 0.626, 0.636, 0.615, 0.579, 0.551, 0.552,
+                                0.559, 0.528, 0.479, 0.000, 0.000, 0.000, 0.000
+                       },
+#endif
+                       {       /* SNR_s */
+                                -8.240, -8.240, -8.240, -8.240, -8.240, -8.240, -8.240,
+                                -8.240, -8.240, -8.240, -7.447, -7.447, -7.447, -7.447,
+                                -7.447, -7.447, -7.447, -7.447, -7.447, -7.447, -6.990,
+                                -6.990, -6.990, -6.990, -6.990, -6.990, -6.990, -6.990,
+                                -6.990, -6.990, -6.020, -6.020, -6.020, -6.020, -5.229,
+                                -5.229, -5.229, -5.229,  0.000,  0.000,  0.000,  0.000
+                       },
+                       {       /* bval_s */
+                                 0.000,  1.875,  3.750,  5.438,  6.857,  8.109,  9.237,
+                                10.202, 11.083, 11.865, 12.554, 13.195, 13.781, 14.309,
+                                14.803, 15.250, 15.667, 16.068, 16.409, 17.045, 17.607,
+                                18.097, 18.528, 18.931, 19.295, 19.636, 20.038, 20.486,
+                                20.900, 21.306, 21.722, 22.128, 22.513, 22.877, 23.241,
+                                23.616, 23.974, 24.313,  0.000,  0.000,  0.000,  0.000
+                       }
+
+               },
+
+               {       /* psy_data3_48000_20 */
+
+                       {       /* cbw_l */
+                                 3,  3,  4,  3,  3,  2,  3,
+                                 2,  3,  3,  3,  3,  4,  3,
+                                 3,  3,  3,  2,  3,  2,  1
+                       },
+                       {       /* bu_l */
+                                 0,  4,  7, 11, 14, 17, 19,
+                                22, 24, 27, 30, 33, 36, 40,
+                                43, 46, 49, 52, 54, 57, 59
+                       },
+                       {       /* bo_l */
+                                 4,  7, 11, 14, 17, 19, 22,
+                                24, 27, 30, 33, 36, 40, 43,
+                                46, 49, 52, 54, 57, 59, 60
+                       },
+                       {       /* w1_l */
+                                1.000, 0.944, 0.389, 0.833, 0.278, 0.361, 0.583,
+                                0.917, 0.250, 0.583, 0.352, 0.389, 0.375, 0.856,
+                                0.611, 0.840, 0.783, 0.816, 0.114, 0.687, 0.548
+                       },
+                       {       /* w2_l */
+                                0.056, 0.611, 0.167, 0.722, 0.639, 0.417, 0.083,
+                                0.750, 0.417, 0.648, 0.611, 0.625, 0.144, 0.389,
+                                0.160, 0.217, 0.184, 0.886, 0.313, 0.452, 0.908
+                       }
+
+               },
+
+               {       /* psy_data4_48000_11 */
+
+                       {       /* cbw_s */
+                                 2,  2,  3,  3,  4,  4,
+                                 3,  4,  4,  2,  3,  2
+                       },
+                       {       /* bu_s */
+                                 0,  3,  5,  8, 11, 15,
+                                19, 22, 26, 30, 32, 35
+                       },
+                       {       /* bo_s */
+                                 2,  5,  8, 11, 15, 19,
+                                22, 26, 30, 32, 35, 37
+                       },
+                       {       /* w1_s */
+                                1.000, 0.833, 0.167, 0.500, 0.833, 0.833,
+                                0.417, 0.083, 0.055, 0.958, 0.433, 0.833
+                       },
+                       {       /* w2_s */
+                                0.167, 0.833, 0.500, 0.167, 0.167, 0.583,
+                                0.917, 0.944, 0.042, 0.567, 0.167, 0.618
+                       }
+
+               }
+
+       },
+
+       {       /* sfreq_idx_32000 */
+
+               {       /* psy_longBlock_32000_58 */
+
+                       59, /* cbmax_l */
+
+                       {       /* numlines_l */
+                                 2,  2,  2,  2,  2,  2,  2,  2,  2,
+                                 2,  2,  2,  2,  2,  2,  2,  2,  2,
+                                 3,  3,  3,  3,  3,  3,  3,  3,  3,
+                                 4,  4,  4,  5,  5,  5,  5,  5,  6,
+                                 6,  6,  8,  8,  8, 10, 10, 10, 12,
+                                12, 13, 14, 14, 16, 20, 20, 23, 27,
+                                27, 32, 37, 37, 12,  0,  0,  0,  0
+                       },
+                       {       /* minval */
+                                24.5, 24.5, 24.5, 20.0, 20.0, 20.0, 20.0, 20.0, 18.0,
+                                18.0, 12.0, 12.0,  6.0,  6.0,  6.0,  3.0,  3.0,  3.0,
+                                 3.0,  3.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,
+                                 0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0
+                       },
+                       {       /* qthr_l */
+                                  4.532,   4.532,   1.809,   0.181,   0.181,   0.057,   0.018,   0.018,   0.018,
+                                  0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.018,
+                                  0.027,   0.027,   0.027,   0.027,   0.027,   0.027,   0.027,   0.027,   0.027,
+                                  0.036,   0.036,   0.036,   0.045,   0.045,   0.045,   0.045,   0.045,   0.054,
+                                  0.054,   0.054,   0.072,   0.072,   0.072,   0.090,   0.090,   0.090,   0.109,
+                                  0.109,   0.118,   0.252,   0.252,   0.288,   0.572,   0.572,   0.658,   2.441,
+                                  2.441,   2.893,  33.458,  33.458,  10.851,   0.000,   0.000,   0.000,   0.000
+                       },
+#if !CALC_NORM   /* do we compute more exact values? */
+                       {       /* norm_l */
+                                0.997, 0.893, 0.881, 0.873, 0.872, 0.871, 0.860, 0.839, 0.812,
+                                0.784, 0.741, 0.697, 0.674, 0.651, 0.633, 0.611, 0.589, 0.575,
+                                0.654, 0.724, 0.701, 0.673, 0.631, 0.592, 0.553, 0.510, 0.506,
+                                0.562, 0.598, 0.589, 0.607, 0.620, 0.580, 0.532, 0.517, 0.517,
+                                0.509, 0.506, 0.522, 0.531, 0.519, 0.512, 0.509, 0.498, 0.494,
+                                0.501, 0.508, 0.502, 0.493, 0.497, 0.506, 0.510, 0.504, 0.496,
+                                0.493, 0.490, 0.482, 0.458, 0.500, 0.000, 0.000, 0.000, 0.000
+                       },
+#endif
+                       {       /* bval_l */
+                                 0.313,  0.938,  1.563,  2.188,  2.813,  3.438,  4.045,  4.625,  5.173,
+                                 5.698,  6.185,  6.634,  7.070,  7.492,  7.905,  8.305,  8.695,  9.064,
+                                 9.484,  9.966, 10.426, 10.866, 11.279, 11.669, 12.042, 12.386, 12.721,
+                                13.091, 13.488, 13.873, 14.268, 14.679, 15.067, 15.424, 15.771, 16.120,
+                                16.466, 16.807, 17.158, 17.518, 17.869, 18.215, 18.563, 18.902, 19.239,
+                                19.580, 19.925, 20.269, 20.606, 20.944, 21.288, 21.635, 21.980, 22.319,
+                                22.656, 22.993, 23.326, 23.656, 23.937,  0.000,  0.000,  0.000,  0.000
+                       }
+
+               },
+
+               {       /* psy_shortBlock_32000_41 */
+
+                       42, /* cbmax_s */
+
+                       {       /* numlines_s */
+                                 1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  1,  1,
+                                 1,  1,  1,  1,  1,  2,  2,
+                                 2,  2,  2,  2,  2,  2,  3,
+                                 3,  3,  3,  3,  4,  4,  4,
+                                 5,  5,  6,  7,  7,  8, 10
+                       },
+                       {       /* qthr_s */
+                                  4.532,   0.904,   0.090,   0.029,   0.009,   0.009,   0.009,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.009,   0.009,
+                                  0.009,   0.009,   0.009,   0.009,   0.009,   0.018,   0.018,
+                                  0.018,   0.018,   0.018,   0.018,   0.018,   0.018,   0.027,
+                                  0.027,   0.027,   0.027,   0.027,   0.072,   0.072,   0.072,
+                                  0.143,   0.143,   0.172,   0.633,   0.633,   0.723,   9.043
+                       },
+#if !CALC_NORM   /* do we compute more exact values? */
+                       {       /* norm_s */
+                                1.000, 0.985, 0.983, 0.983, 0.981, 0.975, 0.959,
+                                0.944, 0.933, 0.920, 0.892, 0.863, 0.839, 0.786,
+                                0.755, 0.698, 0.673, 0.605, 0.629, 0.883, 0.858,
+                                0.829, 0.767, 0.705, 0.637, 0.564, 0.550, 0.603,
+                                0.635, 0.592, 0.533, 0.518, 0.568, 0.594, 0.568,
+                                0.536, 0.522, 0.542, 0.539, 0.519, 0.514, 0.518
+                       },
+#endif
+                       {       /* SNR_s */
+                                -8.240, -8.240, -8.240, -8.240, -8.240, -8.240, -8.240,
+                                -8.240, -8.240, -8.240, -7.447, -7.447, -7.447, -7.447,
+                                -7.447, -7.447, -7.447, -7.447, -7.447, -7.447, -6.990,
+                                -6.990, -6.990, -6.990, -6.990, -6.990, -6.990, -6.990,
+                                -6.990, -6.990, -6.020, -6.020, -6.020, -6.020, -5.229,
+                                -5.229, -5.229, -5.229, -4.559, -4.559, -3.980, -3.980
+                       },
+                       {       /* bval_s */
+                                 0.000,  1.250,  2.500,  3.750,  4.909,  5.958,  6.857,
+                                 7.700,  8.500,  9.237,  9.895, 10.500, 11.083, 11.604,
+                                12.107, 12.554, 13.000, 13.391, 13.781, 14.474, 15.096,
+                                15.667, 16.177, 16.636, 17.057, 17.429, 17.786, 18.177,
+                                18.597, 18.994, 19.352, 19.693, 20.066, 20.462, 20.841,
+                                21.201, 21.549, 21.911, 22.275, 22.625, 22.971, 23.321
+                       }
+
+               },
+
+               {       /* psy_data3_32000_20 */
+
+                       {       /* cbw_l */
+                                 1,  2,  2,  1,  2,  2,  3,
+                                 2,  3,  3,  4,  4,  4,  3,
+                                 4,  3,  4,  3,  3,  2,  2
+                       },
+                       {       /* bu_l */
+                                 0,  2,  4,  6,  7,  9, 11,
+                                14, 16, 19, 22, 26, 30, 34,
+                                37, 41, 44, 48, 51, 54, 56
+                       },
+                       {       /* bo_l */
+                                 2,  4,  6,  7,  9, 11, 14,
+                                16, 19, 22, 26, 30, 34, 37,
+                                41, 44, 48, 51, 54, 56, 58
+                       },
+                       {       /* w1_l */
+                                1.000, 0.472, 0.694, 0.917, 0.139, 0.361, 0.583,
+                                0.917, 0.250, 0.130, 0.167, 0.611, 0.522, 0.967,
+                                0.083, 0.383, 0.005, 0.726, 0.519, 0.739, 0.116
+                       },
+                       {       /* w2_l */
+                                0.528, 0.305, 0.083, 0.861, 0.639, 0.417, 0.083,
+                                0.750, 0.870, 0.833, 0.389, 0.478, 0.033, 0.917,
+                                0.617, 0.995, 0.274, 0.480, 0.261, 0.884, 1.000
+                       }
+
+               },
+
+               {       /* psy_data4_32000_11 */
+
+                       {       /* cbw_s */
+                                 2,  2,  3,  3,  4,  5,
+                                 4,  5,  4,  4,  3,  1
+                       },
+                       {       /* bu_s */
+                                 0,  3,  5,  8, 11, 15,
+                                20, 24, 29, 33, 37, 40
+                       },
+                       {       /* bo_s */
+                                 2,  5,  8, 11, 15, 20,
+                                24, 29, 33, 37, 40, 41
+                       },
+                       {       /* w1_s */
+                                1.000, 0.833, 0.167, 0.500, 0.833, 0.833,
+                                0.750, 0.750, 0.944, 0.625, 0.528, 0.062
+                       },
+                       {       /* w2_s */
+                                0.167, 0.833, 0.500, 0.167, 0.167, 0.250,
+                                0.250, 0.055, 0.375, 0.472, 0.937, 1.000
+                       }
+
+               }
+
+       }
+
+};
+
+#else   /* NEW_L3PARM_TABLES */
+
+psyDataElem                            psy_longBlock_48000_61[62] =
+{
+       { 1 ,24.5 ,   4.532 ,0.970 ,   0.000 },
+       { 1 ,24.5 ,   4.532 ,0.755 ,   0.469 },
+       { 1 ,24.5 ,   4.532 ,0.738 ,   0.938 },
+       { 1 ,24.5 ,   0.904 ,0.730 ,   1.406 },
+       { 1 ,24.5 ,   0.904 ,0.724 ,   1.875 },
+       { 1 ,20.0 ,   0.090 ,0.723 ,   2.344 },
+       { 1 ,20.0 ,   0.090 ,0.723 ,   2.813 },
+       { 1 ,20.0 ,   0.029 ,0.723 ,   3.281 },
+       { 1 ,20.0 ,   0.029 ,0.718 ,   3.750 },
+       { 1 ,20.0 ,   0.009 ,0.690 ,   4.199 },
+       { 1 ,20.0 ,   0.009 ,0.660 ,   4.625 },
+       { 1 ,18.0 ,   0.009 ,0.641 ,   5.047 },
+       { 1 ,18.0 ,   0.009 ,0.600 ,   5.438 },
+       { 1 ,18.0 ,   0.009 ,0.584 ,   5.828 },
+       { 1 ,12.0 ,   0.009 ,0.532 ,   6.188 },
+       { 1 ,12.0 ,   0.009 ,0.537 ,   6.522 },
+       { 2 , 6.0 ,   0.018 ,0.857 ,   7.174 },
+       { 2 , 6.0 ,   0.018 ,0.858 ,   7.801 },
+       { 2 , 3.0 ,   0.018 ,0.853 ,   8.402 },
+       { 2 , 3.0 ,   0.018 ,0.824 ,   8.966 },
+       { 2 , 3.0 ,   0.018 ,0.778 ,   9.484 },
+       { 2 , 3.0 ,   0.018 ,0.740 ,   9.966 },
+       { 2 , 0.0 ,   0.018 ,0.709 ,  10.426 },
+
+       { 2 , 0.0 ,   0.018 ,0.676 ,  10.866 },
+       { 2 , 0.0 ,   0.018 ,0.632 ,  11.279 },
+       { 2 , 0.0 ,   0.018 ,0.592 ,  11.669 },
+       { 2 , 0.0 ,   0.018 ,0.553 ,  12.042 },
+       { 2 , 0.0 ,   0.018 ,0.510 ,  12.386 },
+       { 2 , 0.0 ,   0.018 ,0.513 ,  12.721 },
+       { 3 , 0.0 ,   0.027 ,0.608 ,  13.115 },
+       { 3 , 0.0 ,   0.027 ,0.673 ,  13.562 },
+       { 3 , 0.0 ,   0.027 ,0.637 ,  13.984 },
+       { 3 , 0.0 ,   0.027 ,0.586 ,  14.371 },
+       { 3 , 0.0 ,   0.027 ,0.571 ,  14.741 },
+       { 4 , 0.0 ,   0.036 ,0.616 ,  15.140 },
+       { 4 , 0.0 ,   0.036 ,0.640 ,  15.563 },
+       { 4 , 0.0 ,   0.036 ,0.598 ,  15.962 },
+       { 4 , 0.0 ,   0.036 ,0.538 ,  16.324 },
+       { 4 , 0.0 ,   0.036 ,0.512 ,  16.665 },
+       { 5 , 0.0 ,   0.045 ,0.528 ,  17.020 },
+       { 5 , 0.0 ,   0.045 ,0.517 ,  17.373 },
+       { 5 , 0.0 ,   0.045 ,0.493 ,  17.708 },
+       { 6 , 0.0 ,   0.054 ,0.499 ,  18.045 },
+       { 7 , 0.0 ,   0.063 ,0.525 ,  18.398 },
+       { 7 , 0.0 ,   0.063 ,0.541 ,  18.762 },
+       { 8 , 0.0 ,   0.072 ,0.528 ,  19.120 },
+       { 8 , 0.0 ,   0.072 ,0.510 ,  19.466 },
+       { 8 , 0.0 ,   0.072 ,0.506 ,  19.807 },
+       {10 , 0.0 ,   0.180 ,0.525 ,  20.159 },
+       {10 , 0.0 ,   0.180 ,0.536 ,  20.522 },
+       {10 , 0.0 ,   0.180 ,0.518 ,  20.874 },
+       {13 , 0.0 ,   0.372 ,0.501 ,  21.214 },
+       {13 , 0.0 ,   0.372 ,0.497 ,  21.553 },
+       {14 , 0.0 ,   0.400 ,0.497 ,  21.892 },
+       {18 , 0.0 ,   1.627 ,0.495 ,  22.231 },
+       {18 , 0.0 ,   1.627 ,0.494 ,  22.569 },
+       {20 , 0.0 ,   1.808 ,0.497 ,  22.909 },
+       {25 , 0.0 ,  22.607 ,0.494 ,  23.248 },
+       {25 , 0.0 ,  22.607 ,0.487 ,  23.583 },
+       {35 , 0.0 ,  31.650 ,0.483 ,  23.915 },
+       {67 , 0.0 , 605.867 ,0.482 ,  24.246 },
+       {67 , 0.0 , 605.867 ,0.524 ,  24.576 } 
+};
+
+psyDataElem                            psy_longBlock_44100_62[63] =
+{
+       { 1 ,24.5 ,   4.532 ,0.951 ,   0.000 },
+       { 1 ,24.5 ,   4.532 ,0.700 ,   0.431 },
+       { 1 ,24.5 ,   4.532 ,0.681 ,   0.861 },
+       { 1 ,24.5 ,   0.904 ,0.675 ,   1.292 },
+       { 1 ,24.5 ,   0.904 ,0.667 ,   1.723 },
+       { 1 ,20.0 ,   0.090 ,0.665 ,   2.153 },
+       { 1 ,20.0 ,   0.090 ,0.664 ,   2.584 },
+       { 1 ,20.0 ,   0.029 ,0.664 ,   3.015 },
+       { 1 ,20.0 ,   0.029 ,0.664 ,   3.445 },
+       { 1 ,20.0 ,   0.029 ,0.655 ,   3.876 },
+       { 1 ,20.0 ,   0.009 ,0.616 ,   4.279 },
+       { 1 ,20.0 ,   0.009 ,0.597 ,   4.670 },
+       { 1 ,18.0 ,   0.009 ,0.578 ,   5.057 },
+       { 1 ,18.0 ,   0.009 ,0.541 ,   5.416 },
+       { 1 ,18.0 ,   0.009 ,0.575 ,   5.774 },
+       { 2 ,12.0 ,   0.018 ,0.856 ,   6.422 },
+       { 2 , 6.0 ,   0.018 ,0.846 ,   7.026 },
+       { 2 , 6.0 ,   0.018 ,0.840 ,   7.609 },
+       { 2 , 3.0 ,   0.018 ,0.822 ,   8.168 },
+       { 2 , 3.0 ,   0.018 ,0.800 ,   8.710 },
+       { 2 , 3.0 ,   0.018 ,0.753 ,   9.207 },
+       { 2 , 3.0 ,   0.018 ,0.704 ,   9.662 },
+       { 2 , 0.0 ,   0.018 ,0.674 ,  10.099 },
+       { 2 , 0.0 ,   0.018 ,0.640 ,  10.515 },
+       { 2 , 0.0 ,   0.018 ,0.609 ,  10.917 },
+       { 2 , 0.0 ,   0.018 ,0.566 ,  11.293 },
+       { 2 , 0.0 ,   0.018 ,0.535 ,  11.652 },
+       { 2 , 0.0 ,   0.018 ,0.531 ,  11.997 },
+       { 3 , 0.0 ,   0.027 ,0.615 ,  12.394 },
+       { 3 , 0.0 ,   0.027 ,0.686 ,  12.850 },
+       { 3 , 0.0 ,   0.027 ,0.650 ,  13.277 },
+       { 3 , 0.0 ,   0.027 ,0.612 ,  13.681 },
+       { 3 , 0.0 ,   0.027 ,0.567 ,  14.062 },
+       { 3 , 0.0 ,   0.027 ,0.520 ,  14.411 },
+       { 3 , 0.0 ,   0.027 ,0.513 ,  14.751 },
+       { 4 , 0.0 ,   0.036 ,0.557 ,  15.119 },
+       { 4 , 0.0 ,   0.036 ,0.584 ,  15.508 },
+       { 4 , 0.0 ,   0.036 ,0.570 ,  15.883 },
+       { 5 , 0.0 ,   0.045 ,0.579 ,  16.263 },
+       { 5 , 0.0 ,   0.045 ,0.585 ,  16.654 },
+       { 5 , 0.0 ,   0.045 ,0.548 ,  17.020 },
+       { 6 , 0.0 ,   0.054 ,0.536 ,  17.374 },
+       { 6 , 0.0 ,   0.054 ,0.550 ,  17.744 },
+       { 7 , 0.0 ,   0.063 ,0.532 ,  18.104 },
+       { 7 , 0.0 ,   0.063 ,0.504 ,  18.447 },
+       { 7 , 0.0 ,   0.063 ,0.496 ,  18.782 },
+       { 9 , 0.0 ,   0.081 ,0.517 ,  19.130 },
+       { 9 , 0.0 ,   0.081 ,0.527 ,  19.487 },
+       { 9 , 0.0 ,   0.081 ,0.516 ,  19.838 },
+       {10 , 0.0 ,   0.180 ,0.497 ,  20.179 },
+       {10 , 0.0 ,   0.180 ,0.489 ,  20.510 },
+       {11 , 0.0 ,   0.198 ,0.502 ,  20.852 },
+       {14 , 0.0 ,   0.400 ,0.501 ,  21.196 },
+       {14 , 0.0 ,   0.400 ,0.491 ,  21.531 },
+       {15 , 0.0 ,   0.429 ,0.497 ,  21.870 },
+       {20 , 0.0 ,   1.808 ,0.504 ,  22.214 },
+       {20 , 0.0 ,   1.808 ,0.504 ,  22.558 },
+       {21 , 0.0 ,   1.898 ,0.495 ,  22.898 },
+       {27 , 0.0 ,  24.416 ,0.486 ,  23.232 },
+       {27 , 0.0 ,  24.416 ,0.484 ,  23.564 },
+       {36 , 0.0 ,  32.554 ,0.483 ,  23.897 },
+       {73 , 0.0 , 660.124 ,0.475 ,  24.229 },
+       {18 , 0.0 , 162.770 ,0.515 ,  24.442 } 
+};
+
+psyDataElem                            psy_longBlock_32000_58[59] =
+{
+       { 2 ,24.5 ,   4.532 ,0.997 ,   0.313 },
+       { 2 ,24.5 ,   4.532 ,0.893 ,   0.938 },
+       { 2 ,24.5 ,   1.809 ,0.881 ,   1.563 },
+       { 2 ,20.0 ,   0.181 ,0.873 ,   2.188 },
+       { 2 ,20.0 ,   0.181 ,0.872 ,   2.813 },
+       { 2 ,20.0 ,   0.057 ,0.871 ,   3.438 },
+       { 2 ,20.0 ,   0.018 ,0.860 ,   4.045 },
+       { 2 ,20.0 ,   0.018 ,0.839 ,   4.625 },
+       { 2 ,18.0 ,   0.018 ,0.812 ,   5.173 },
+       { 2 ,18.0 ,   0.018 ,0.784 ,   5.698 },
+       { 2 ,12.0 ,   0.018 ,0.741 ,   6.185 },
+       { 2 ,12.0 ,   0.018 ,0.697 ,   6.634 },
+       { 2 , 6.0 ,   0.018 ,0.674 ,   7.070 },
+       { 2 , 6.0 ,   0.018 ,0.651 ,   7.492 },
+       { 2 , 6.0 ,   0.018 ,0.633 ,   7.905 },
+       { 2 , 3.0 ,   0.018 ,0.611 ,   8.305 },
+       { 2 , 3.0 ,   0.018 ,0.589 ,   8.695 },
+       { 2 , 3.0 ,   0.018 ,0.575 ,   9.064 },
+       { 3 , 3.0 ,   0.027 ,0.654 ,   9.484 },
+       { 3 , 3.0 ,   0.027 ,0.724 ,   9.966 },
+       { 3 , 0.0 ,   0.027 ,0.701 ,  10.426 },
+       { 3 , 0.0 ,   0.027 ,0.673 ,  10.866 },
+       { 3 , 0.0 ,   0.027 ,0.631 ,  11.279 },
+       { 3 , 0.0 ,   0.027 ,0.592 ,  11.669 },
+       { 3 , 0.0 ,   0.027 ,0.553 ,  12.042 },
+       { 3 , 0.0 ,   0.027 ,0.510 ,  12.386 },
+       { 3 , 0.0 ,   0.027 ,0.506 ,  12.721 },
+       { 4 , 0.0 ,   0.036 ,0.562 ,  13.091 },
+       { 4 , 0.0 ,   0.036 ,0.598 ,  13.488 },
+       { 4 , 0.0 ,   0.036 ,0.589 ,  13.873 },
+       { 5 , 0.0 ,   0.045 ,0.607 ,  14.268 },
+       { 5 , 0.0 ,   0.045 ,0.620 ,  14.679 },
+       { 5 , 0.0 ,   0.045 ,0.580 ,  15.067 },
+       { 5 , 0.0 ,   0.045 ,0.532 ,  15.424 },
+       { 5 , 0.0 ,   0.045 ,0.517 ,  15.771 },
+       { 6 , 0.0 ,   0.054 ,0.517 ,  16.120 },
+       { 6 , 0.0 ,   0.054 ,0.509 ,  16.466 },
+       { 6 , 0.0 ,   0.054 ,0.506 ,  16.807 },
+       { 8 , 0.0 ,   0.072 ,0.522 ,  17.158 },
+       { 8 , 0.0 ,   0.072 ,0.531 ,  17.518 },
+       { 8 , 0.0 ,   0.072 ,0.519 ,  17.869 },
+       {10 , 0.0 ,   0.090 ,0.512 ,  18.215 },
+       {10 , 0.0 ,   0.090 ,0.509 ,  18.563 },
+       {10 , 0.0 ,   0.090 ,0.498 ,  18.902 },
+       {12 , 0.0 ,   0.109 ,0.494 ,  19.239 },
+       {12 , 0.0 ,   0.109 ,0.501 ,  19.580 },
+       {13 , 0.0 ,   0.118 ,0.508 ,  19.925 },
+       {14 , 0.0 ,   0.252 ,0.502 ,  20.269 },
+       {14 , 0.0 ,   0.252 ,0.493 ,  20.606 },
+       {16 , 0.0 ,   0.288 ,0.497 ,  20.944 },
+       {20 , 0.0 ,   0.572 ,0.506 ,  21.288 },
+       {20 , 0.0 ,   0.572 ,0.510 ,  21.635 },
+       {23 , 0.0 ,   0.658 ,0.504 ,  21.980 },
+       {27 , 0.0 ,   2.441 ,0.496 ,  22.319 },
+       {27 , 0.0 ,   2.441 ,0.493 ,  22.656 },
+       {32 , 0.0 ,   2.893 ,0.490 ,  22.993 },
+       {37 , 0.0 ,  33.458 ,0.482 ,  23.326 },
+       {37 , 0.0 ,  33.458 ,0.458 ,  23.656 },
+       {12 , 0.0 ,  10.851 ,0.500 ,  23.937 } 
+};
+
+
+psyDataElem2                   psy_shortBlock_48000_37[38] =
+{
+       { 1 ,   4.532 ,1.000 ,-8.240 ,   0.000 },
+       { 1 ,   0.904 ,0.989 ,-8.240 ,   1.875 },
+       { 1 ,   0.029 ,0.989 ,-8.240 ,   3.750 },
+       { 1 ,   0.009 ,0.981 ,-8.240 ,   5.438 },
+       { 1 ,   0.009 ,0.985 ,-8.240 ,   6.857 },
+       { 1 ,   0.009 ,0.984 ,-8.240 ,   8.109 },
+       { 1 ,   0.009 ,0.980 ,-8.240 ,   9.237 },
+       { 1 ,   0.009 ,0.968 ,-8.240 ,  10.202 },
+       { 1 ,   0.009 ,0.954 ,-8.240 ,  11.083 },
+       { 1 ,   0.009 ,0.929 ,-8.240 ,  11.865 },
+       { 1 ,   0.009 ,0.906 ,-7.447 ,  12.554 },
+       { 1 ,   0.009 ,0.883 ,-7.447 ,  13.195 },
+       { 1 ,   0.009 ,0.844 ,-7.447 ,  13.781 },
+       { 1 ,   0.009 ,0.792 ,-7.447 ,  14.309 },
+       { 1 ,   0.009 ,0.747 ,-7.447 ,  14.803 },
+       { 1 ,   0.009 ,0.689 ,-7.447 ,  15.250 },
+       { 1 ,   0.009 ,0.644 ,-7.447 ,  15.667 },
+       { 1 ,   0.009 ,0.592 ,-7.447 ,  16.068 },
+       { 1 ,   0.009 ,0.553 ,-7.447 ,  16.409 },
+       { 2 ,   0.018 ,0.850 ,-7.447 ,  17.045 },
+       { 2 ,   0.018 ,0.811 ,-6.990 ,  17.607 },
+       { 2 ,   0.018 ,0.736 ,-6.990 ,  18.097 },
+       { 2 ,   0.018 ,0.665 ,-6.990 ,  18.528 },
+       { 2 ,   0.018 ,0.610 ,-6.990 ,  18.931 },
+       { 2 ,   0.018 ,0.544 ,-6.990 ,  19.295 },
+       { 2 ,   0.018 ,0.528 ,-6.990 ,  19.636 },
+       { 3 ,   0.054 ,0.621 ,-6.990 ,  20.038 },
+       { 3 ,   0.054 ,0.673 ,-6.990 ,  20.486 },
+       { 3 ,   0.054 ,0.635 ,-6.990 ,  20.900 },
+       { 4 ,   0.114 ,0.626 ,-6.990 ,  21.306 },
+       { 4 ,   0.114 ,0.636 ,-6.020 ,  21.722 },
+       { 5 ,   0.452 ,0.615 ,-6.020 ,  22.128 },
+       { 5 ,   0.452 ,0.579 ,-6.020 ,  22.513 },
+       { 5 ,   0.452 ,0.551 ,-6.020 ,  22.877 },
+       { 7 ,   6.330 ,0.552 ,-5.229 ,  23.241 },
+       { 7 ,   6.330 ,0.559 ,-5.229 ,  23.616 },
+       {11 ,   9.947 ,0.528 ,-5.229 ,  23.974 },
+       {17 , 153.727 ,0.479 ,-5.229 ,  24.313 }
+};
+
+psyDataElem2                   psy_shortBlock_44100_38[39] =
+{
+       { 1 ,   4.532 ,1.000 ,-8.240 ,   0.000 },
+       { 1 ,   0.904 ,0.983 ,-8.240 ,   1.723 },
+       { 1 ,   0.029 ,0.983 ,-8.240 ,   3.445 },
+       { 1 ,   0.009 ,0.982 ,-8.240 ,   5.057 },
+       { 1 ,   0.009 ,0.985 ,-8.240 ,   6.422 },
+       { 1 ,   0.009 ,0.983 ,-8.240 ,   7.609 },
+       { 1 ,   0.009 ,0.978 ,-8.240 ,   8.710 },
+       { 1 ,   0.009 ,0.967 ,-8.240 ,   9.662 },
+       { 1 ,   0.009 ,0.948 ,-8.240 ,  10.515 },
+       { 1 ,   0.009 ,0.930 ,-8.240 ,  11.293 },
+       { 1 ,   0.009 ,0.914 ,-7.447 ,  12.009 },
+       { 1 ,   0.009 ,0.870 ,-7.447 ,  12.625 },
+       { 1 ,   0.009 ,0.845 ,-7.447 ,  13.210 },
+       { 1 ,   0.009 ,0.800 ,-7.447 ,  13.748 },
+       { 1 ,   0.009 ,0.749 ,-7.447 ,  14.241 },
+       { 1 ,   0.009 ,0.701 ,-7.447 ,  14.695 },
+       { 1 ,   0.009 ,0.653 ,-7.447 ,  15.125 },
+       { 1 ,   0.009 ,0.590 ,-7.447 ,  15.508 },
+       { 1 ,   0.009 ,0.616 ,-7.447 ,  15.891 },
+       { 2 ,   0.018 ,0.860 ,-7.447 ,  16.537 },
+       { 2 ,   0.018 ,0.823 ,-6.990 ,  17.112 },
+       { 2 ,   0.018 ,0.762 ,-6.990 ,  17.621 },
+       { 2 ,   0.018 ,0.688 ,-6.990 ,  18.073 },
+       { 2 ,   0.018 ,0.612 ,-6.990 ,  18.470 },
+       { 2 ,   0.018 ,0.594 ,-6.990 ,  18.849 },
+       { 3 ,   0.027 ,0.658 ,-6.990 ,  19.271 },
+       { 3 ,   0.027 ,0.706 ,-6.990 ,  19.741 },
+       { 3 ,   0.054 ,0.660 ,-6.990 ,  20.177 },
+       { 3 ,   0.054 ,0.606 ,-6.990 ,  20.576 },
+       { 3 ,   0.054 ,0.565 ,-6.990 ,  20.950 },
+       { 4 ,   0.114 ,0.560 ,-6.020 ,  21.316 },
+       { 4 ,   0.114 ,0.579 ,-6.020 ,  21.699 },
+       { 5 ,   0.452 ,0.567 ,-6.020 ,  22.078 },
+       { 5 ,   0.452 ,0.534 ,-6.020 ,  22.438 },
+       { 5 ,   0.452 ,0.514 ,-5.229 ,  22.782 },
+       { 7 ,   6.330 ,0.520 ,-5.229 ,  23.133 },
+       { 7 ,   6.330 ,0.518 ,-5.229 ,  23.484 },
+       { 7 ,   6.330 ,0.507 ,-5.229 ,  23.828 },
+       {19 , 171.813 ,0.447 ,-4.559 ,  24.173 }
+};
+
+psyDataElem2                   psy_shortBlock_32000_41[42] =
+{
+       { 1 ,   4.532 ,1.000 ,-8.240 ,   0.000 },
+       { 1 ,   0.904 ,0.985 ,-8.240 ,   1.250 },
+       { 1 ,   0.090 ,0.983 ,-8.240 ,   2.500 },
+       { 1 ,   0.029 ,0.983 ,-8.240 ,   3.750 },
+       { 1 ,   0.009 ,0.981 ,-8.240 ,   4.909 },
+       { 1 ,   0.009 ,0.975 ,-8.240 ,   5.958 },
+       { 1 ,   0.009 ,0.959 ,-8.240 ,   6.857 },
+       { 1 ,   0.009 ,0.944 ,-8.240 ,   7.700 },
+       { 1 ,   0.009 ,0.933 ,-8.240 ,   8.500 },
+       { 1 ,   0.009 ,0.920 ,-8.240 ,   9.237 },
+       { 1 ,   0.009 ,0.892 ,-7.447 ,   9.895 },
+       { 1 ,   0.009 ,0.863 ,-7.447 ,  10.500 },
+       { 1 ,   0.009 ,0.839 ,-7.447 ,  11.083 },
+       { 1 ,   0.009 ,0.786 ,-7.447 ,  11.604 },
+       { 1 ,   0.009 ,0.755 ,-7.447 ,  12.107 },
+       { 1 ,   0.009 ,0.698 ,-7.447 ,  12.554 },
+       { 1 ,   0.009 ,0.673 ,-7.447 ,  13.000 },
+       { 1 ,   0.009 ,0.605 ,-7.447 ,  13.391 },
+       { 1 ,   0.009 ,0.629 ,-7.447 ,  13.781 },
+       { 2 ,   0.018 ,0.883 ,-7.447 ,  14.474 },
+       { 2 ,   0.018 ,0.858 ,-6.990 ,  15.096 },
+       { 2 ,   0.018 ,0.829 ,-6.990 ,  15.667 },
+       { 2 ,   0.018 ,0.767 ,-6.990 ,  16.177 },
+       { 2 ,   0.018 ,0.705 ,-6.990 ,  16.636 },
+       { 2 ,   0.018 ,0.637 ,-6.990 ,  17.057 },
+       { 2 ,   0.018 ,0.564 ,-6.990 ,  17.429 },
+       { 2 ,   0.018 ,0.550 ,-6.990 ,  17.786 },
+       { 3 ,   0.027 ,0.603 ,-6.990 ,  18.177 },
+       { 3 ,   0.027 ,0.635 ,-6.990 ,  18.597 },
+       { 3 ,   0.027 ,0.592 ,-6.990 ,  18.994 },
+       { 3 ,   0.027 ,0.533 ,-6.020 ,  19.352 },
+       { 3 ,   0.027 ,0.518 ,-6.020 ,  19.693 },
+       { 4 ,   0.072 ,0.568 ,-6.020 ,  20.066 },
+       { 4 ,   0.072 ,0.594 ,-6.020 ,  20.462 },
+       { 4 ,   0.072 ,0.568 ,-5.229 ,  20.841 },
+       { 5 ,   0.143 ,0.536 ,-5.229 ,  21.201 },
+       { 5 ,   0.143 ,0.522 ,-5.229 ,  21.549 },
+       { 6 ,   0.172 ,0.542 ,-5.229 ,  21.911 },
+       { 7 ,   0.633 ,0.539 ,-4.559 ,  22.275 },
+       { 7 ,   0.633 ,0.519 ,-4.559 ,  22.625 },
+       { 8 ,   0.723 ,0.514 ,-3.980 ,  22.971 },
+       {10 ,   9.043 ,0.518 ,-3.980 ,  23.321 }
+};
+
+
+psyDataElem3                   psy_data3_48000_20[21] =
+{
+       { 3 , 0 , 4 ,1.000 ,0.056 },
+       { 3 , 4 , 7 ,0.944 ,0.611 },
+       { 4 , 7 ,11 ,0.389 ,0.167 },
+       { 3 ,11 ,14 ,0.833 ,0.722 },
+       { 3 ,14 ,17 ,0.278 ,0.639 },
+       { 2 ,17 ,19 ,0.361 ,0.417 },
+       { 3 ,19 ,22 ,0.583 ,0.083 },
+       { 2 ,22 ,24 ,0.917 ,0.750 },
+       { 3 ,24 ,27 ,0.250 ,0.417 },
+       { 3 ,27 ,30 ,0.583 ,0.648 },
+       { 3 ,30 ,33 ,0.352 ,0.611 },
+       { 3 ,33 ,36 ,0.389 ,0.625 },
+       { 4 ,36 ,40 ,0.375 ,0.144 },
+       { 3 ,40 ,43 ,0.856 ,0.389 },
+       { 3 ,43 ,46 ,0.611 ,0.160 },
+       { 3 ,46 ,49 ,0.840 ,0.217 },
+       { 3 ,49 ,52 ,0.783 ,0.184 },
+       { 2 ,52 ,54 ,0.816 ,0.886 },
+       { 3 ,54 ,57 ,0.114 ,0.313 },
+       { 2 ,57 ,59 ,0.687 ,0.452 },
+       { 1 ,59 ,60 ,0.548 ,0.908 }
+};
+
+psyDataElem3                   psy_data3_44100_20[21] =
+{
+       { 3 , 0 , 4 ,1.000 ,0.056 },
+       { 3 , 4 , 7 ,0.944 ,0.611 },
+       { 4 , 7 ,11 ,0.389 ,0.167 },
+       { 3 ,11 ,14 ,0.833 ,0.722 },
+       { 3 ,14 ,17 ,0.278 ,0.139 },
+       { 1 ,17 ,18 ,0.861 ,0.917 },
+       { 3 ,18 ,21 ,0.083 ,0.583 },
+       { 3 ,21 ,24 ,0.417 ,0.250 },
+       { 3 ,24 ,27 ,0.750 ,0.805 },
+       { 3 ,27 ,30 ,0.194 ,0.574 },
+       { 3 ,30 ,33 ,0.426 ,0.537 },
+       { 3 ,33 ,36 ,0.463 ,0.819 },
+       { 4 ,36 ,40 ,0.180 ,0.100 },
+       { 3 ,40 ,43 ,0.900 ,0.468 },
+       { 3 ,43 ,46 ,0.532 ,0.623 },
+       { 3 ,46 ,49 ,0.376 ,0.450 },
+       { 3 ,49 ,52 ,0.550 ,0.552 },
+       { 3 ,52 ,55 ,0.448 ,0.403 },
+       { 2 ,55 ,57 ,0.597 ,0.643 },
+       { 2 ,57 ,59 ,0.357 ,0.722 },
+       { 2 ,59 ,61 ,0.278 ,0.960 }
+};
+
+psyDataElem3                   psy_data3_32000_20[21] =
+{
+       { 1 , 0 , 2 ,1.000 ,0.528 },
+       { 2 , 2 , 4 ,0.472 ,0.305 },
+       { 2 , 4 , 6 ,0.694 ,0.083 },
+       { 1 , 6 , 7 ,0.917 ,0.861 },
+       { 2 , 7 , 9 ,0.139 ,0.639 },
+       { 2 , 9 ,11 ,0.361 ,0.417 },
+       { 3 ,11 ,14 ,0.583 ,0.083 },
+       { 2 ,14 ,16 ,0.917 ,0.750 },
+       { 3 ,16 ,19 ,0.250 ,0.870 },
+       { 3 ,19 ,22 ,0.130 ,0.833 },
+       { 4 ,22 ,26 ,0.167 ,0.389 },
+       { 4 ,26 ,30 ,0.611 ,0.478 },
+       { 4 ,30 ,34 ,0.522 ,0.033 },
+       { 3 ,34 ,37 ,0.967 ,0.917 },
+       { 4 ,37 ,41 ,0.083 ,0.617 },
+       { 3 ,41 ,44 ,0.383 ,0.995 },
+       { 4 ,44 ,48 ,0.005 ,0.274 },
+       { 3 ,48 ,51 ,0.726 ,0.480 },
+       { 3 ,51 ,54 ,0.519 ,0.261 },
+       { 2 ,54 ,56 ,0.739 ,0.884 },
+       { 2 ,56 ,58 ,0.116 ,1.000 }
+};
+
+
+psyDataElem3                   psy_data4_48000_11[12] =
+{
+       { 2 , 0 , 2 ,1.000 ,0.167 },
+       { 2 , 3 , 5 ,0.833 ,0.833 },
+       { 3 , 5 , 8 ,0.167 ,0.500 },
+       { 3 , 8 ,11 ,0.500 ,0.167 },
+       { 4 ,11 ,15 ,0.833 ,0.167 },
+       { 4 ,15 ,19 ,0.833 ,0.583 },
+       { 3 ,19 ,22 ,0.417 ,0.917 },
+       { 4 ,22 ,26 ,0.083 ,0.944 },
+       { 4 ,26 ,30 ,0.055 ,0.042 },
+       { 2 ,30 ,32 ,0.958 ,0.567 },
+       { 3 ,32 ,35 ,0.433 ,0.167 },
+       { 2 ,35 ,37 ,0.833 ,0.618 }
+};
+
+psyDataElem3                   psy_data4_44100_11[12] =
+{
+       { 2 , 0 , 2 ,1.000 ,0.167 },
+       { 2 , 3 , 5 ,0.833 ,0.833 },
+       { 3 , 5 , 8 ,0.167 ,0.500 },
+       { 3 , 8 ,11 ,0.500 ,0.167 },
+       { 4 ,11 ,15 ,0.833 ,0.167 },
+       { 5 ,15 ,20 ,0.833 ,0.250 },
+       { 3 ,20 ,23 ,0.750 ,0.583 },
+       { 4 ,23 ,27 ,0.417 ,0.055 },
+       { 3 ,27 ,30 ,0.944 ,0.375 },
+       { 3 ,30 ,33 ,0.625 ,0.300 },
+       { 3 ,33 ,36 ,0.700 ,0.167 },
+       { 2 ,36 ,38 ,0.833 ,1.000 }
+};
+
+psyDataElem3                   psy_data4_32000_11[12] =
+{
+       { 2 , 0 , 2 ,1.000 ,0.167 },
+       { 2 , 3 , 5 ,0.833 ,0.833 },
+       { 3 , 5 , 8 ,0.167 ,0.500 },
+       { 3 , 8 ,11 ,0.500 ,0.167 },
+       { 4 ,11 ,15 ,0.833 ,0.167 },
+       { 5 ,15 ,20 ,0.833 ,0.250 },
+       { 4 ,20 ,24 ,0.750 ,0.250 },
+       { 5 ,24 ,29 ,0.750 ,0.055 },
+       { 4 ,29 ,33 ,0.944 ,0.375 },
+       { 4 ,33 ,37 ,0.625 ,0.472 },
+       { 3 ,37 ,40 ,0.528 ,0.937 },
+       { 1 ,40 ,41 ,0.062 ,1.000 }
+};
+
+#endif   /* NEW_L3PARM_TABLES */
+
+
+
+
+
+/*=============================================================================*/
+
+
+
+
+
+#if 1   /* WIND_SB_CHANGE_LEVEL == 3 */
+
+double                                 enwindow[512] =
+{
+        0.000000000, 0.000101566, 0.000971317, 0.003134727, 0.035780907, 0.003134727, 0.000971317, 0.000101566,
+       -0.000000477, 0.000103951, 0.000953674, 0.002841473, 0.035758972, 0.003401756, 0.000983715, 0.000099182,
+       -0.000000477, 0.000105858, 0.000930786, 0.002521515, 0.035694122, 0.003643036, 0.000991821, 0.000096321,
+       -0.000000477, 0.000107288, 0.000902653, 0.002174854, 0.035586357, 0.003858566, 0.000995159, 0.000093460,
+       -0.000000477, 0.000108242, 0.000868797, 0.001800537, 0.035435200, 0.004049301, 0.000994205, 0.000090599,
+       -0.000000477, 0.000108719, 0.000829220, 0.001399517, 0.035242081, 0.004215240, 0.000989437, 0.000087261,
+       -0.000000477, 0.000108719, 0.000783920, 0.000971317, 0.035007000, 0.004357815, 0.000980854, 0.000083923,
+       -0.000000954, 0.000108242, 0.000731945, 0.000515938, 0.034730434, 0.004477024, 0.000968933, 0.000080585,
+
+       -0.000000954, 0.000106812, 0.000674248, 0.000033379, 0.034412861, 0.004573822, 0.000954151, 0.000076771,
+       -0.000000954, 0.000105381, 0.000610352,-0.000475883, 0.034055710, 0.004649162, 0.000935555, 0.000073433,
+       -0.000000954, 0.000102520, 0.000539303,-0.001011848, 0.033659935, 0.004703045, 0.000915051, 0.000070095,
+       -0.000001431, 0.000099182, 0.000462532,-0.001573563, 0.033225536, 0.004737377, 0.000891685, 0.000066280,
+       -0.000001431, 0.000095367, 0.000378609,-0.002161503, 0.032754898, 0.004752159, 0.000866413, 0.000062943,
+       -0.000001907, 0.000090122, 0.000288486,-0.002774239, 0.032248020, 0.004748821, 0.000838757, 0.000059605,
+       -0.000001907, 0.000084400, 0.000191689,-0.003411293, 0.031706810, 0.004728317, 0.000809669, 0.000055790,
+       -0.000002384, 0.000077724, 0.000088215,-0.004072189, 0.031132698, 0.004691124, 0.000779152, 0.000052929,
+
+       -0.000002384, 0.000069618,-0.000021458,-0.004756451, 0.030526638, 0.004638195, 0.000747204, 0.000049591,
+       -0.000002861, 0.000060558,-0.000137329,-0.005462170, 0.029890060, 0.004570484, 0.000714302, 0.000046253,
+       -0.000003338, 0.000050545,-0.000259876,-0.006189346, 0.029224873, 0.004489899, 0.000680923, 0.000043392,
+       -0.000003338, 0.000039577,-0.000388145,-0.006937027, 0.028532982, 0.004395962, 0.000646591, 0.000040531,
+       -0.000003815, 0.000027180,-0.000522137,-0.007703304, 0.027815342, 0.004290581, 0.000611782, 0.000037670,
+       -0.000004292, 0.000013828,-0.000661850,-0.008487225, 0.027073860, 0.004174709, 0.000576973, 0.000034809,
+       -0.000004768,-0.000000954,-0.000806808,-0.009287834, 0.026310921, 0.004048824, 0.000542164, 0.000032425,
+       -0.000005245,-0.000017166,-0.000956535,-0.010103703, 0.025527000, 0.003914356, 0.000507355, 0.000030041,
+
+       -0.000006199,-0.000034332,-0.001111031,-0.010933399, 0.024725437, 0.003771782, 0.000472546, 0.000027657,
+       -0.000006676,-0.000052929,-0.001269817,-0.011775017, 0.023907185, 0.003622532, 0.000438213, 0.000025272,
+       -0.000007629,-0.000072956,-0.001432419,-0.012627602, 0.023074150, 0.003467083, 0.000404358, 0.000023365,
+       -0.000008106,-0.000093937,-0.001597881,-0.013489246, 0.022228718, 0.003306866, 0.000371456, 0.000021458,
+       -0.000009060,-0.000116348,-0.001766682,-0.014358521, 0.021372318, 0.003141880, 0.000339031, 0.000019550,
+       -0.000010014,-0.000140190,-0.001937389,-0.015233517, 0.020506859, 0.002974033, 0.000307560, 0.000018120,
+       -0.000011444,-0.000165462,-0.002110004,-0.016112804, 0.019634247, 0.002803326, 0.000277042, 0.000016689,
+       -0.000012398,-0.000191212,-0.002283096,-0.016994476, 0.018756866, 0.002630711, 0.000247478, 0.000014782,
+
+       -0.000013828,-0.000218868,-0.002457142,-0.017876148, 0.017876148, 0.002457142, 0.000218868, 0.000013828,
+       -0.000014782,-0.000247478,-0.002630711,-0.018756866, 0.016994476, 0.002283096, 0.000191212, 0.000012398,
+       -0.000016689,-0.000277042,-0.002803326,-0.019634247, 0.016112804, 0.002110004, 0.000165462, 0.000011444,
+       -0.000018120,-0.000307560,-0.002974033,-0.020506859, 0.015233517, 0.001937389, 0.000140190, 0.000010014,
+       -0.000019550,-0.000339031,-0.003141880,-0.021372318, 0.014358521, 0.001766682, 0.000116348, 0.000009060,
+       -0.000021458,-0.000371456,-0.003306866,-0.022228718, 0.013489246, 0.001597881, 0.000093937, 0.000008106,
+       -0.000023365,-0.000404358,-0.003467083,-0.023074150, 0.012627602, 0.001432419, 0.000072956, 0.000007629,
+       -0.000025272,-0.000438213,-0.003622532,-0.023907185, 0.011775017, 0.001269817, 0.000052929, 0.000006676,
+
+       -0.000027657,-0.000472546,-0.003771782,-0.024725437, 0.010933399, 0.001111031, 0.000034332, 0.000006199,
+       -0.000030041,-0.000507355,-0.003914356,-0.025527000, 0.010103703, 0.000956535, 0.000017166, 0.000005245,
+       -0.000032425,-0.000542164,-0.004048824,-0.026310921, 0.009287834, 0.000806808, 0.000000954, 0.000004768,
+       -0.000034809,-0.000576973,-0.004174709,-0.027073860, 0.008487225, 0.000661850,-0.000013828, 0.000004292,
+       -0.000037670,-0.000611782,-0.004290581,-0.027815342, 0.007703304, 0.000522137,-0.000027180, 0.000003815,
+       -0.000040531,-0.000646591,-0.004395962,-0.028532982, 0.006937027, 0.000388145,-0.000039577, 0.000003338,
+       -0.000043392,-0.000680923,-0.004489899,-0.029224873, 0.006189346, 0.000259876,-0.000050545, 0.000003338,
+       -0.000046253,-0.000714302,-0.004570484,-0.029890060, 0.005462170, 0.000137329,-0.000060558, 0.000002861,
+
+       -0.000049591,-0.000747204,-0.004638195,-0.030526638, 0.004756451, 0.000021458,-0.000069618, 0.000002384,
+       -0.000052929,-0.000779152,-0.004691124,-0.031132698, 0.004072189,-0.000088215,-0.000077724, 0.000002384,
+       -0.000055790,-0.000809669,-0.004728317,-0.031706810, 0.003411293,-0.000191689,-0.000084400, 0.000001907,
+       -0.000059605,-0.000838757,-0.004748821,-0.032248020, 0.002774239,-0.000288486,-0.000090122, 0.000001907,
+       -0.000062943,-0.000866413,-0.004752159,-0.032754898, 0.002161503,-0.000378609,-0.000095367, 0.000001431,
+       -0.000066280,-0.000891685,-0.004737377,-0.033225536, 0.001573563,-0.000462532,-0.000099182, 0.000001431,
+       -0.000070095,-0.000915051,-0.004703045,-0.033659935, 0.001011848,-0.000539303,-0.000102520, 0.000000954,
+       -0.000073433,-0.000935555,-0.004649162,-0.034055710, 0.000475883,-0.000610352,-0.000105381, 0.000000954,
+
+       -0.000076771,-0.000954151,-0.004573822,-0.034412861,-0.000033379,-0.000674248,-0.000106812, 0.000000954,
+       -0.000080585,-0.000968933,-0.004477024,-0.034730434,-0.000515938,-0.000731945,-0.000108242, 0.000000954,
+       -0.000083923,-0.000980854,-0.004357815,-0.035007000,-0.000971317,-0.000783920,-0.000108719, 0.000000477,
+       -0.000087261,-0.000989437,-0.004215240,-0.035242081,-0.001399517,-0.000829220,-0.000108719, 0.000000477,
+       -0.000090599,-0.000994205,-0.004049301,-0.035435200,-0.001800537,-0.000868797,-0.000108242, 0.000000477,
+       -0.000093460,-0.000995159,-0.003858566,-0.035586357,-0.002174854,-0.000902653,-0.000107288, 0.000000477,
+       -0.000096321,-0.000991821,-0.003643036,-0.035694122,-0.002521515,-0.000930786,-0.000105858, 0.000000477,
+       -0.000099182,-0.000983715,-0.003401756,-0.035758972,-0.002841473,-0.000953674,-0.000103951, 0.000000477
+};
+
+#else   /* WIND_SB_CHANGE_LEVEL < 3 */
+
+double                                 enwindow[512] =
+{
+        0.000000000,-0.000000477,-0.000000477,-0.000000477,-0.000000477,-0.000000477,-0.000000477,-0.000000954,
+       -0.000000954,-0.000000954,-0.000000954,-0.000001431,-0.000001431,-0.000001907,-0.000001907,-0.000002384,
+       -0.000002384,-0.000002861,-0.000003338,-0.000003338,-0.000003815,-0.000004292,-0.000004768,-0.000005245,
+       -0.000006199,-0.000006676,-0.000007629,-0.000008106,-0.000009060,-0.000010014,-0.000011444,-0.000012398,
+       -0.000013828,-0.000014782,-0.000016689,-0.000018120,-0.000019550,-0.000021458,-0.000023365,-0.000025272,
+       -0.000027657,-0.000030041,-0.000032425,-0.000034809,-0.000037670,-0.000040531,-0.000043392,-0.000046253,
+       -0.000049591,-0.000052929,-0.000055790,-0.000059605,-0.000062943,-0.000066280,-0.000070095,-0.000073433,
+       -0.000076771,-0.000080585,-0.000083923,-0.000087261,-0.000090599,-0.000093460,-0.000096321,-0.000099182,
+
+        0.000101566, 0.000103951, 0.000105858, 0.000107288, 0.000108242, 0.000108719, 0.000108719, 0.000108242,
+        0.000106812, 0.000105381, 0.000102520, 0.000099182, 0.000095367, 0.000090122, 0.000084400, 0.000077724,
+        0.000069618, 0.000060558, 0.000050545, 0.000039577, 0.000027180, 0.000013828,-0.000000954,-0.000017166,
+       -0.000034332,-0.000052929,-0.000072956,-0.000093937,-0.000116348,-0.000140190,-0.000165462,-0.000191212,
+       -0.000218868,-0.000247478,-0.000277042,-0.000307560,-0.000339031,-0.000371456,-0.000404358,-0.000438213,
+       -0.000472546,-0.000507355,-0.000542164,-0.000576973,-0.000611782,-0.000646591,-0.000680923,-0.000714302,
+       -0.000747204,-0.000779152,-0.000809669,-0.000838757,-0.000866413,-0.000891685,-0.000915051,-0.000935555,
+       -0.000954151,-0.000968933,-0.000980854,-0.000989437,-0.000994205,-0.000995159,-0.000991821,-0.000983715,
+
+        0.000971317, 0.000953674, 0.000930786, 0.000902653, 0.000868797, 0.000829220, 0.000783920, 0.000731945,
+        0.000674248, 0.000610352, 0.000539303, 0.000462532, 0.000378609, 0.000288486, 0.000191689, 0.000088215,
+       -0.000021458,-0.000137329,-0.000259876,-0.000388145,-0.000522137,-0.000661850,-0.000806808,-0.000956535,
+       -0.001111031,-0.001269817,-0.001432419,-0.001597881,-0.001766682,-0.001937389,-0.002110004,-0.002283096,
+       -0.002457142,-0.002630711,-0.002803326,-0.002974033,-0.003141880,-0.003306866,-0.003467083,-0.003622532,
+       -0.003771782,-0.003914356,-0.004048824,-0.004174709,-0.004290581,-0.004395962,-0.004489899,-0.004570484,
+       -0.004638195,-0.004691124,-0.004728317,-0.004748821,-0.004752159,-0.004737377,-0.004703045,-0.004649162,
+       -0.004573822,-0.004477024,-0.004357815,-0.004215240,-0.004049301,-0.003858566,-0.003643036,-0.003401756,
+
+        0.003134727, 0.002841473, 0.002521515, 0.002174854, 0.001800537, 0.001399517, 0.000971317, 0.000515938,
+        0.000033379,-0.000475883,-0.001011848,-0.001573563,-0.002161503,-0.002774239,-0.003411293,-0.004072189,
+       -0.004756451,-0.005462170,-0.006189346,-0.006937027,-0.007703304,-0.008487225,-0.009287834,-0.010103703,
+       -0.010933399,-0.011775017,-0.012627602,-0.013489246,-0.014358521,-0.015233517,-0.016112804,-0.016994476,
+       -0.017876148,-0.018756866,-0.019634247,-0.020506859,-0.021372318,-0.022228718,-0.023074150,-0.023907185,
+       -0.024725437,-0.025527000,-0.026310921,-0.027073860,-0.027815342,-0.028532982,-0.029224873,-0.029890060,
+       -0.030526638,-0.031132698,-0.031706810,-0.032248020,-0.032754898,-0.033225536,-0.033659935,-0.034055710,
+       -0.034412861,-0.034730434,-0.035007000,-0.035242081,-0.035435200,-0.035586357,-0.035694122,-0.035758972,
+
+        0.035780907, 0.035758972, 0.035694122, 0.035586357, 0.035435200, 0.035242081, 0.035007000, 0.034730434,
+        0.034412861, 0.034055710, 0.033659935, 0.033225536, 0.032754898, 0.032248020, 0.031706810, 0.031132698,
+        0.030526638, 0.029890060, 0.029224873, 0.028532982, 0.027815342, 0.027073860, 0.026310921, 0.025527000,
+        0.024725437, 0.023907185, 0.023074150, 0.022228718, 0.021372318, 0.020506859, 0.019634247, 0.018756866,
+        0.017876148, 0.016994476, 0.016112804, 0.015233517, 0.014358521, 0.013489246, 0.012627602, 0.011775017,
+        0.010933399, 0.010103703, 0.009287834, 0.008487225, 0.007703304, 0.006937027, 0.006189346, 0.005462170,
+        0.004756451, 0.004072189, 0.003411293, 0.002774239, 0.002161503, 0.001573563, 0.001011848, 0.000475883,
+       -0.000033379,-0.000515938,-0.000971317,-0.001399517,-0.001800537,-0.002174854,-0.002521515,-0.002841473,
+
+        0.003134727, 0.003401756, 0.003643036, 0.003858566, 0.004049301, 0.004215240, 0.004357815, 0.004477024,
+        0.004573822, 0.004649162, 0.004703045, 0.004737377, 0.004752159, 0.004748821, 0.004728317, 0.004691124,
+        0.004638195, 0.004570484, 0.004489899, 0.004395962, 0.004290581, 0.004174709, 0.004048824, 0.003914356,
+        0.003771782, 0.003622532, 0.003467083, 0.003306866, 0.003141880, 0.002974033, 0.002803326, 0.002630711,
+        0.002457142, 0.002283096, 0.002110004, 0.001937389, 0.001766682, 0.001597881, 0.001432419, 0.001269817,
+        0.001111031, 0.000956535, 0.000806808, 0.000661850, 0.000522137, 0.000388145, 0.000259876, 0.000137329,
+        0.000021458,-0.000088215,-0.000191689,-0.000288486,-0.000378609,-0.000462532,-0.000539303,-0.000610352,
+       -0.000674248,-0.000731945,-0.000783920,-0.000829220,-0.000868797,-0.000902653,-0.000930786,-0.000953674,
+
+        0.000971317, 0.000983715, 0.000991821, 0.000995159, 0.000994205, 0.000989437, 0.000980854, 0.000968933,
+        0.000954151, 0.000935555, 0.000915051, 0.000891685, 0.000866413, 0.000838757, 0.000809669, 0.000779152,
+        0.000747204, 0.000714302, 0.000680923, 0.000646591, 0.000611782, 0.000576973, 0.000542164, 0.000507355,
+        0.000472546, 0.000438213, 0.000404358, 0.000371456, 0.000339031, 0.000307560, 0.000277042, 0.000247478,
+        0.000218868, 0.000191212, 0.000165462, 0.000140190, 0.000116348, 0.000093937, 0.000072956, 0.000052929,
+        0.000034332, 0.000017166, 0.000000954,-0.000013828,-0.000027180,-0.000039577,-0.000050545,-0.000060558,
+       -0.000069618,-0.000077724,-0.000084400,-0.000090122,-0.000095367,-0.000099182,-0.000102520,-0.000105381,
+       -0.000106812,-0.000108242,-0.000108719,-0.000108719,-0.000108242,-0.000107288,-0.000105858,-0.000103951,
+
+        0.000101566, 0.000099182, 0.000096321, 0.000093460, 0.000090599, 0.000087261, 0.000083923, 0.000080585,
+        0.000076771, 0.000073433, 0.000070095, 0.000066280, 0.000062943, 0.000059605, 0.000055790, 0.000052929,
+        0.000049591, 0.000046253, 0.000043392, 0.000040531, 0.000037670, 0.000034809, 0.000032425, 0.000030041,
+        0.000027657, 0.000025272, 0.000023365, 0.000021458, 0.000019550, 0.000018120, 0.000016689, 0.000014782,
+        0.000013828, 0.000012398, 0.000011444, 0.000010014, 0.000009060, 0.000008106, 0.000007629, 0.000006676,
+        0.000006199, 0.000005245, 0.000004768, 0.000004292, 0.000003815, 0.000003338, 0.000003338, 0.000002861,
+        0.000002384, 0.000002384, 0.000001907, 0.000001907, 0.000001431, 0.000001431, 0.000000954, 0.000000954,
+        0.000000954, 0.000000954, 0.000000477, 0.000000477, 0.000000477, 0.000000477, 0.000000477, 0.000000477
+};
+
+#endif
+
+
+
+
+
+/*=============================================================================*/
+/*                                      >>> HUFFMAN ENCODING RELATED TABLES <<<                                   */
+/*=============================================================================*/
+
+
+unsigned int huff_tab1[2*2] = {
+  1, 1,
+  1, 0
+};
+
+unsigned int huff_tab2[3*3] = {
+    1, 2, 1,
+    3, 1, 1,
+    3, 2, 0
+};
+
+unsigned int huff_tab3[3*3] = {
+    3, 2, 1,
+    1, 1, 1,
+    3, 2, 0
+};
+
+unsigned int huff_tab5[4*4] = {
+    1, 2, 6, 5,
+    3, 1, 4, 4,
+    7, 5, 7, 1,
+    6, 1, 1, 0
+};
+
+unsigned int huff_tab6[4*4] = {
+    7, 3, 5, 1,
+    6, 2, 3, 2,
+    5, 4, 4, 1,
+    3, 3, 2, 0
+};
+
+unsigned int huff_tab7[6*6] = {
+    1, 2, 10, 19, 16, 10,
+    3, 3, 7, 10, 5, 3,
+    11, 4, 13, 17, 8, 4,
+    12, 11, 18, 15, 11, 2,
+    7, 6, 9, 14, 3, 1,
+    6, 4, 5, 3, 2, 0
+};
+
+unsigned int huff_tab8[6*6] = {
+    3, 4, 6, 18, 12, 5,
+    5, 1, 2, 16, 9, 3,
+    7, 3, 5, 14, 7, 3,
+    19, 17, 15, 13, 10, 4,
+    13, 5, 8, 11, 5, 1,
+    12, 4, 4, 1, 1, 0
+};
+
+unsigned int huff_tab9[6*6] = {
+    7, 5, 9, 14, 15, 7,
+    6, 4, 5, 5, 6, 7,
+    7, 6, 8, 8, 8, 5,
+    15, 6, 9, 10, 5, 1,
+    11, 7, 9, 6, 4, 1,
+    14, 4, 6, 2, 6, 0
+};
+
+unsigned int huff_tab10[8*8] = {
+    1, 2, 10, 23, 35, 30, 12, 17,
+    3, 3, 8, 12, 18, 21, 12, 7,
+    11, 9, 15, 21, 32, 40, 19, 6,
+    14, 13, 22, 34, 46, 23, 18, 7,
+    20, 19, 33, 47, 27, 22, 9, 3,
+    31, 22, 41, 26, 21, 20, 5, 3,
+    14, 13, 10, 11, 16, 6, 5, 1,
+    9, 8, 7, 8, 4, 4, 2, 0
+};
+
+unsigned int huff_tab11[8*8] = {
+    3, 4, 10, 24, 34, 33, 21, 15,
+    5, 3, 4, 10, 32, 17, 11, 10,
+    11, 7, 13, 18, 30, 31, 20, 5,
+    25, 11, 19, 59, 27, 18, 12, 5,
+    35, 33, 31, 58, 30, 16, 7, 5,
+    28, 26, 32, 19, 17, 15, 8, 14,
+    14, 12, 9, 13, 14, 9, 4, 1,
+    11, 4, 6, 6, 6, 3, 2, 0
+};
+
+unsigned int huff_tab12[8*8] = {
+    9, 6, 16, 33, 41, 39, 38, 26,
+    7, 5, 6, 9, 23, 16, 26, 11,
+    17, 7, 11, 14, 21, 30, 10, 7,
+    17, 10, 15, 12, 18, 28, 14, 5,
+    32, 13, 22, 19, 18, 16, 9, 5,
+    40, 17, 31, 29, 17, 13, 4, 2,
+    27, 12, 11, 15, 10, 7, 4, 1,
+    27, 12, 8, 12, 6, 3, 1, 0
+};
+
+unsigned int huff_tab13[16*16] = {
+    1, 5, 14, 21, 34, 51, 46, 71, 42, 52, 68, 52, 67, 44, 43, 19,
+    3, 4, 12, 19, 31, 26, 44, 33, 31, 24, 32, 24, 31, 35, 22, 14,
+    15, 13, 23, 36, 59, 49, 77, 65, 29, 40, 30, 40, 27, 33, 42, 16,
+    22, 20, 37, 61, 56, 79, 73, 64, 43, 76, 56, 37, 26, 31, 25, 14,
+    35, 16, 60, 57, 97, 75, 114, 91, 54, 73, 55, 41, 48, 53, 23, 24,
+    58, 27, 50, 96, 76, 70, 93, 84, 77, 58, 79, 29, 74, 49, 41, 17,
+    47, 45, 78, 74, 115, 94, 90, 79, 69, 83, 71, 50, 59, 38, 36, 15,
+    72, 34, 56, 95, 92, 85, 91, 90, 86, 73, 77, 65, 51, 44, 43, 42,
+    43, 20, 30, 44, 55, 78, 72, 87, 78, 61, 46, 54, 37, 30, 20, 16,
+    53, 25, 41, 37, 44, 59, 54, 81, 66, 76, 57, 54, 37, 18, 39, 11,
+    35, 33, 31, 57, 42, 82, 72, 80, 47, 58, 55, 21, 22, 26, 38, 22,
+    53, 25, 23, 38, 70, 60, 51, 36, 55, 26, 34, 23, 27, 14, 9, 7,
+    34, 32, 28, 39, 49, 75, 30, 52, 48, 40, 52, 28, 18, 17, 9, 5,
+    45, 21, 34, 64, 56, 50, 49, 45, 31, 19, 12, 15, 10, 7, 6, 3,
+    48, 23, 20, 39, 36, 35, 53, 21, 16, 23, 13, 10, 6, 1, 4, 2,
+    16, 15, 17, 27, 25, 20, 29, 11, 17, 12, 16, 8, 1, 1, 0, 1
+};
+
+unsigned int huff_tab15[16*16] = {
+    7, 12, 18, 53, 47, 76, 124, 108, 89, 123, 108, 119, 107, 81, 122, 63,
+    13, 5, 16, 27, 46, 36, 61, 51, 42, 70, 52, 83, 65, 41, 59, 36,
+    19, 17, 15, 24, 41, 34, 59, 48, 40, 64, 50, 78, 62, 80, 56, 33,
+    29, 28, 25, 43, 39, 63, 55, 93, 76, 59, 93, 72, 54, 75, 50, 29,
+    52, 22, 42, 40, 67, 57, 95, 79, 72, 57, 89, 69, 49, 66, 46, 27,
+    77, 37, 35, 66, 58, 52, 91, 74, 62, 48, 79, 63, 90, 62, 40, 38,
+    125, 32, 60, 56, 50, 92, 78, 65, 55, 87, 71, 51, 73, 51, 70, 30,
+    109, 53, 49, 94, 88, 75, 66, 122, 91, 73, 56, 42, 64, 44, 21, 25,
+    90, 43, 41, 77, 73, 63, 56, 92, 77, 66, 47, 67, 48, 53, 36, 20,
+    71, 34, 67, 60, 58, 49, 88, 76, 67, 106, 71, 54, 38, 39, 23, 15,
+    109, 53, 51, 47, 90, 82, 58, 57, 48, 72, 57, 41, 23, 27, 62, 9,
+    86, 42, 40, 37, 70, 64, 52, 43, 70, 55, 42, 25, 29, 18, 11, 11,
+    118, 68, 30, 55, 50, 46, 74, 65, 49, 39, 24, 16, 22, 13, 14, 7,
+    91, 44, 39, 38, 34, 63, 52, 45, 31, 52, 28, 19, 14, 8, 9, 3,
+    123, 60, 58, 53, 47, 43, 32, 22, 37, 24, 17, 12, 15, 10, 2, 1,
+    71, 37, 34, 30, 28, 20, 17, 26, 21, 16, 10, 6, 8, 6, 2, 0
+};
+
+unsigned int huff_tab16[16*16] = {
+    1, 5, 14, 44, 74, 63, 110, 93, 172, 149, 138, 242, 225, 195, 376, 17,
+    3, 4, 12, 20, 35, 62, 53, 47, 83, 75, 68, 119, 201, 107, 207, 9,
+    15, 13, 23, 38, 67, 58, 103, 90, 161, 72, 127, 117, 110, 209, 206, 16,
+    45, 21, 39, 69, 64, 114, 99, 87, 158, 140, 252, 212, 199, 387, 365, 26,
+    75, 36, 68, 65, 115, 101, 179, 164, 155, 264, 246, 226, 395, 382, 362, 9,
+    66, 30, 59, 56, 102, 185, 173, 265, 142, 253, 232, 400, 388, 378, 445, 16,
+    111, 54, 52, 100, 184, 178, 160, 133, 257, 244, 228, 217, 385, 366, 715, 10,
+    98, 48, 91, 88, 165, 157, 148, 261, 248, 407, 397, 372, 380, 889, 884, 8,
+    85, 84, 81, 159, 156, 143, 260, 249, 427, 401, 392, 383, 727, 713, 708, 7,
+    154, 76, 73, 141, 131, 256, 245, 426, 406, 394, 384, 735, 359, 710, 352, 11,
+    139, 129, 67, 125, 247, 233, 229, 219, 393, 743, 737, 720, 885, 882, 439, 4,
+    243, 120, 118, 115, 227, 223, 396, 746, 742, 736, 721, 712, 706, 223, 436, 6,
+    202, 224, 222, 218, 216, 389, 386, 381, 364, 888, 443, 707, 440, 437, 1728, 4,
+    747, 211, 210, 208, 370, 379, 734, 723, 714, 1735, 883, 877, 876, 3459, 865, 2,
+    377, 369, 102, 187, 726, 722, 358, 711, 709, 866, 1734, 871, 3458, 870, 434, 0,
+    12, 10, 7, 11, 10, 17, 11, 9, 13, 12, 10, 7, 5, 3, 1, 3
+};
+
+unsigned int huff_tab24[16*16] = {
+    15, 13, 46, 80, 146, 262, 248, 434, 426, 669, 653, 649, 621, 517, 1032, 88,
+    14, 12, 21, 38, 71, 130, 122, 216, 209, 198, 327, 345, 319, 297, 279, 42,
+    47, 22, 41, 74, 68, 128, 120, 221, 207, 194, 182, 340, 315, 295, 541, 18,
+    81, 39, 75, 70, 134, 125, 116, 220, 204, 190, 178, 325, 311, 293, 271, 16,
+    147, 72, 69, 135, 127, 118, 112, 210, 200, 188, 352, 323, 306, 285, 540, 14,
+    263, 66, 129, 126, 119, 114, 214, 202, 192, 180, 341, 317, 301, 281, 262, 12,
+    249, 123, 121, 117, 113, 215, 206, 195, 185, 347, 330, 308, 291, 272, 520, 10,
+    435, 115, 111, 109, 211, 203, 196, 187, 353, 332, 313, 298, 283, 531, 381, 17,
+    427, 212, 208, 205, 201, 193, 186, 177, 169, 320, 303, 286, 268, 514, 377, 16,
+    335, 199, 197, 191, 189, 181, 174, 333, 321, 305, 289, 275, 521, 379, 371, 11,
+    668, 184, 183, 179, 175, 344, 331, 314, 304, 290, 277, 530, 383, 373, 366, 10,
+    652, 346, 171, 168, 164, 318, 309, 299, 287, 276, 263, 513, 375, 368, 362, 6,
+    648, 322, 316, 312, 307, 302, 292, 284, 269, 261, 512, 376, 370, 364, 359, 4,
+    620, 300, 296, 294, 288, 282, 273, 266, 515, 380, 374, 369, 365, 361, 357, 2,
+    1033, 280, 278, 274, 267, 264, 259, 382, 378, 372, 367, 363, 360, 358, 356, 0,
+    43, 20, 19, 17, 15, 13, 11, 9, 7, 6, 4, 7, 5, 3, 1, 3
+};
+
+unsigned int huff_tab32[16*1] = {
+    1,
+    5,
+    4,
+    5,
+    6,
+    5,
+    4,
+    4,
+    7,
+    3,
+    6,
+    0,
+    7,
+    2,
+    3,
+    1
+};
+
+unsigned int huff_tab33[16*1] = {
+    15,
+    14,
+    13,
+    12,
+    11,
+    10,
+    9,
+    8,
+    7,
+    6,
+    5,
+    4,
+    3,
+    2,
+    1,
+    0
+};
+
+unsigned char huff_hlen1[2*2] = {
+    1, 3,
+    2, 3
+};
+
+unsigned char huff_hlen2[3*3] = {
+    1, 3, 6,
+    3, 3, 5,
+    5, 5, 6
+};
+
+unsigned char huff_hlen3[3*3] = {
+    2, 2, 6,
+    3, 2, 5,
+    5, 5, 6
+};
+
+unsigned char huff_hlen5[4*4] = {
+    1, 3, 6, 7,
+    3, 3, 6, 7,
+    6, 6, 7, 8,
+    7, 6, 7, 8
+};
+
+unsigned char huff_hlen6[4*4] = {
+    3, 3, 5, 7,
+    3, 2, 4, 5,
+    4, 4, 5, 6,
+    6, 5, 6, 7
+};
+
+unsigned char huff_hlen7[6*6] = {
+    1, 3, 6, 8, 8, 9,
+    3, 4, 6, 7, 7, 8,
+    6, 5, 7, 8, 8, 9,
+    7, 7, 8, 9, 9, 9,
+    7, 7, 8, 9, 9, 10,
+    8, 8, 9, 10, 10, 10
+};
+
+unsigned char huff_hlen8[6*6] = {
+    2, 3, 6, 8, 8, 9,
+    3, 2, 4, 8, 8, 8,
+    6, 4, 6, 8, 8, 9,
+    8, 8, 8, 9, 9, 10,
+    8, 7, 8, 9, 10, 10,
+    9, 8, 9, 9, 11, 11
+};
+
+unsigned char huff_hlen9[6*6] = {
+    3, 3, 5, 6, 8, 9,
+    3, 3, 4, 5, 6, 8,
+    4, 4, 5, 6, 7, 8,
+    6, 5, 6, 7, 7, 8,
+    7, 6, 7, 7, 8, 9,
+    8, 7, 8, 8, 9, 9
+};
+
+unsigned char huff_hlen10[8*8] = {
+    1, 3, 6, 8, 9, 9, 9, 10,
+    3, 4, 6, 7, 8, 9, 8, 8,
+    6, 6, 7, 8, 9, 10, 9, 9,
+    7, 7, 8, 9, 10, 10, 9, 10,
+    8, 8, 9, 10, 10, 10, 10, 10,
+    9, 9, 10, 10, 11, 11, 10, 11,
+    8, 8, 9, 10, 10, 10, 11, 11,
+    9, 8, 9, 10, 10, 11, 11, 11
+};
+
+unsigned char huff_hlen11[8*8] = {
+    2, 3, 5, 7, 8, 9, 8, 9,
+    3, 3, 4, 6, 8, 8, 7, 8,
+    5, 5, 6, 7, 8, 9, 8, 8,
+    7, 6, 7, 9, 8, 10, 8, 9,
+    8, 8, 8, 9, 9, 10, 9, 10,
+    8, 8, 9, 10, 10, 11, 10, 11,
+    8, 7, 7, 8, 9, 10, 10, 10,
+    8, 7, 8, 9, 10, 10, 10, 10
+};
+
+unsigned char huff_hlen12[8*8] = {
+    4, 3, 5, 7, 8, 9, 9, 9,
+    3, 3, 4, 5, 7, 7, 8, 8,
+    5, 4, 5, 6, 7, 8, 7, 8,
+    6, 5, 6, 6, 7, 8, 8, 8,
+    7, 6, 7, 7, 8, 8, 8, 9,
+    8, 7, 8, 8, 8, 9, 8, 9,
+    8, 7, 7, 8, 8, 9, 9, 10,
+    9, 8, 8, 9, 9, 9, 9, 10
+};
+
+unsigned char huff_hlen13[16*16] = {
+    1, 4, 6, 7, 8, 9, 9, 10, 9, 10, 11, 11, 12, 12, 13, 13,
+    3, 4, 6, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 12, 12, 12,
+    6, 6, 7, 8, 9, 9, 10, 10, 9, 10, 10, 11, 11, 12, 13, 13,
+    7, 7, 8, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 13, 13,
+    8, 7, 9, 9, 10, 10, 11, 11, 10, 11, 11, 12, 12, 13, 13, 14,
+    9, 8, 9, 10, 10, 10, 11, 11, 11, 11, 12, 11, 13, 13, 14, 14,
+    9, 9, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 13, 13, 14, 14,
+    10, 9, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 16, 16,
+    9, 8, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 14, 15, 15,
+    10, 9, 10, 10, 11, 11, 11, 13, 12, 13, 13, 14, 14, 14, 16, 15,
+    10, 10, 10, 11, 11, 12, 12, 13, 12, 13, 14, 13, 14, 15, 16, 17,
+    11, 10, 10, 11, 12, 12, 12, 12, 13, 13, 13, 14, 15, 15, 15, 16,
+    11, 11, 11, 12, 12, 13, 12, 13, 14, 14, 15, 15, 15, 16, 16, 16,
+    12, 11, 12, 13, 13, 13, 14, 14, 14, 14, 14, 15, 16, 15, 16, 16,
+    13, 12, 12, 13, 13, 13, 15, 14, 14, 17, 15, 15, 15, 17, 16, 16,
+    12, 12, 13, 14, 14, 14, 15, 14, 15, 15, 16, 16, 19, 18, 19, 16
+};
+
+unsigned char huff_hlen15[16*16] = {
+    3, 4, 5, 7, 7, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 13,
+    4, 3, 5, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11,
+    5, 5, 5, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 11, 11,
+    6, 6, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11,
+    7, 6, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11,
+    8, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 11, 12,
+    9, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12,
+    9, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 12,
+    9, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 12,
+    9, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12,
+    10, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 13, 12,
+    10, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 13,
+    11, 10, 9, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 13, 13,
+    11, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13,
+    12, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 12, 13,
+    12, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13
+};
+
+unsigned char huff_hlen16[16*16] = {
+    1, 4, 6, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 9,
+    3, 4, 6, 7, 8, 9, 9, 9, 10, 10, 10, 11, 12, 11, 12, 8,
+    6, 6, 7, 8, 9, 9, 10, 10, 11, 10, 11, 11, 11, 12, 12, 9,
+    8, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 10,
+    9, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 9,
+    9, 8, 9, 9, 10, 11, 11, 12, 11, 12, 12, 13, 13, 13, 14, 10,
+    10, 9, 9, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 14, 10,
+    10, 9, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 15, 15, 10,
+    10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 14, 10,
+    11, 10, 10, 11, 11, 12, 12, 13, 13, 13, 13, 14, 13, 14, 13, 11,
+    11, 11, 10, 11, 12, 12, 12, 12, 13, 14, 14, 14, 15, 15, 14, 10,
+    12, 11, 11, 11, 12, 12, 13, 14, 14, 14, 14, 14, 14, 13, 14, 11,
+    12, 12, 12, 12, 12, 13, 13, 13, 13, 15, 14, 14, 14, 14, 16, 11,
+    14, 12, 12, 12, 13, 13, 14, 14, 14, 16, 15, 15, 15, 17, 15, 11,
+    13, 13, 11, 12, 14, 14, 13, 14, 14, 15, 16, 15, 17, 15, 14, 11,
+    9, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 8
+};
+
+unsigned char huff_hlen24[16*16] = {
+    4, 4, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, 11, 11, 12, 9,
+    4, 4, 5, 6, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10, 8,
+    6, 5, 6, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 7,
+    7, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 7,
+    8, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 7,
+    9, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 7,
+    9, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 7,
+    10, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 8,
+    10, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 8,
+    10, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 8,
+    11, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 8,
+    11, 10, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 8,
+    11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 8,
+    11, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 8,
+    12, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 8,
+    8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 4
+};
+
+unsigned char huff_hlen32[16*1] = {
+    1,
+    4,
+    4,
+    5,
+    4,
+    6,
+    5,
+    6,
+    4,
+    5,
+    5,
+    6,
+    5,
+    6,
+    6,
+    6
+};
+
+unsigned char huff_hlen33[16*1] = {
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4,
+    4
+};
+
+struct huffcodetab ht[34] = {
+    { 0, 0, 0, 0, -1, 0, 0 },
+    { 2, 2, 0, 0, -1, huff_tab1, huff_hlen1 },
+    { 3, 3, 0, 0, -1, huff_tab2, huff_hlen2 },
+    { 3, 3, 0, 0, -1, huff_tab3, huff_hlen3 },
+    { 0, 0, 0, 0, -1, 0, 0 },
+    { 4, 4, 0, 0, -1, huff_tab5, huff_hlen5 },
+    { 4, 4, 0, 0, -1, huff_tab6, huff_hlen6 },
+    { 6, 6, 0, 0, -1, huff_tab7, huff_hlen7 },
+    { 6, 6, 0, 0, -1, huff_tab8, huff_hlen8 },
+    { 6, 6, 0, 0, -1, huff_tab9, huff_hlen9 },
+    { 8, 8, 0, 0, -1, huff_tab10, huff_hlen10 },
+    { 8, 8, 0, 0, -1, huff_tab11, huff_hlen11 },
+    { 8, 8, 0, 0, -1, huff_tab12, huff_hlen12 },
+    { 16, 16, 0, 0, -1, huff_tab13, huff_hlen13 },
+    { 0, 0, 0, 0, -1, 0, 0 },
+    { 16, 16, 0, 0, -1, huff_tab15, huff_hlen15 },
+    { 16, 16, 1, 1, -1, huff_tab16, huff_hlen16 },
+    { 16, 16, 2, 3, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 3, 7, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 4, 15, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 6, 63, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 8, 255, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 10, 1023, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 13, 8191, 16, huff_tab16, huff_hlen16 },
+    { 16, 16, 4, 15, -1, huff_tab24, huff_hlen24 },
+    { 16, 16, 5, 31, 24, huff_tab24, huff_hlen24 },
+    { 16, 16, 6, 63, 24, huff_tab24, huff_hlen24 },
+    { 16, 16, 7, 127, 24, huff_tab24, huff_hlen24 },
+    { 16, 16, 8, 255, 24, huff_tab24, huff_hlen24 },
+    { 16, 16, 9, 511, 24, huff_tab24, huff_hlen24 },
+    { 16, 16, 11, 2047, 24, huff_tab24, huff_hlen24 },
+    { 16, 16, 13, 8191, 24, huff_tab24, huff_hlen24 },
+    { 1, 16, 0, 0, -1, huff_tab32, huff_hlen32 },
+    { 1, 16, 0, 0, -1, huff_tab33, huff_hlen33 }
+};
diff --git a/lib/bladeenc/tables.h b/lib/bladeenc/tables.h
new file mode 100644 (file)
index 0000000..76a2849
--- /dev/null
@@ -0,0 +1,173 @@
+/*
+                       (c) Copyright 1998-2000 - Tord Jansson
+                       ======================================
+
+               This file is part of the BladeEnc MP3 Encoder, based on
+               ISO's reference code for MPEG Layer 3 compression, and might
+               contain smaller or larger sections that are directly taken
+               from ISO's reference code.
+
+               All changes to the ISO reference code herein are either
+               copyrighted by Tord Jansson (tord.jansson@swipnet.se)
+               or sublicensed to Tord Jansson by a third party.
+
+       BladeEnc is free software; you can redistribute this file
+       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.
+
+
+
+       ------------    Changes    ------------
+
+       2000-12-06  Andre Piotrowski
+
+       -       reformatted
+*/
+#ifndef                __TABLES__
+#define                __TABLES__
+
+
+
+
+
+#define                NEW_L3PARM_TABLES               1
+#define                CALC_NORM                               0
+
+
+
+
+
+#define                CBANDS                                  63
+#define                CBANDS_s                                42
+
+
+
+
+
+#if NEW_L3PARM_TABLES
+
+
+       typedef         struct
+                               {
+                                       int                                             cbmax_l;   /* number of valid entries */
+                                       int                                             numlines_l[CBANDS];
+                                       double                                  minval    [CBANDS];
+                                       double                                  qthr_l    [CBANDS];
+#if !CALC_NORM   /* do we compute more exact values? */
+                                       double                                  norm_l    [CBANDS];
+#endif
+                                       double                                  bval_l    [CBANDS];
+                               }                                               l3_long_data;
+
+
+       typedef         struct
+                               {
+                                       int                                             cbmax_s;   /* number of valid entries */
+                                       int                                             numlines_s[CBANDS_s];
+                                       double                                  qthr_s    [CBANDS_s];
+#if !CALC_NORM   /* do we compute more exact values? */
+                                       double                                  norm_s    [CBANDS_s];
+#endif
+                                       double                                  SNR_s     [CBANDS_s];
+                                       double                                  bval_s    [CBANDS_s];
+                               }                                               l3_short_data;
+
+
+       typedef         struct
+                               {
+                                       int                                             cbw_l     [CBANDS];
+                                       int                                             bu_l      [CBANDS];
+                                       int                                             bo_l      [CBANDS];
+                                       double                                  w1_l      [CBANDS];
+                                       double                                  w2_l      [CBANDS];
+                               }                                               l3_long_thres;
+
+
+       typedef         struct
+                               {
+                                       int                                             cbw_s     [CBANDS_s];
+                                       int                                             bu_s      [CBANDS_s];
+                                       int                                             bo_s      [CBANDS_s];
+                                       double                                  w1_s      [CBANDS_s];
+                                       double                                  w2_s      [CBANDS_s];
+                               }                                               l3_short_thres;
+
+
+       typedef         struct
+                               {
+                                       l3_long_data                    long_data;
+                                       l3_short_data                   short_data;
+                                       l3_long_thres                   long_thres;
+                                       l3_short_thres                  short_thres;
+                               }                                               l3_parm_block;
+
+
+       extern          l3_parm_block                   l3_parm[3];
+
+
+#else   /* NEW_L3PARM_TABLES */
+
+
+       typedef         struct
+                               {
+                               /*      int                                             no; */
+                                       int                                             lines;
+                                       double                                  minVal;
+                                       double                                  qthr;
+                                       double                                  norm;
+                                       double                                  bVal;
+                               }                                               psyDataElem;
+
+
+       typedef         struct
+                               {
+                               /*      int                                             no; */
+                                       int                                             lines;
+                                       double                                  qthr;
+                                       double                                  norm;
+                                       double                                  snr;
+                                       double                                  bVal;
+                               }                                               psyDataElem2;
+
+       typedef         struct
+                               {
+                                       int                                             cbw;
+                                       int                                             bu;
+                                       int                                             bo;
+                                       float                                   w1;
+                                       float                                   w2;
+                               }                                               psyDataElem3;
+
+
+       extern          psyDataElem                             psy_longBlock_48000_61[62];
+       extern          psyDataElem                             psy_longBlock_44100_62[63];
+       extern          psyDataElem                             psy_longBlock_32000_58[59];
+
+       extern          psyDataElem2                    psy_shortBlock_48000_37[38];
+       extern          psyDataElem2                    psy_shortBlock_44100_38[39];
+       extern          psyDataElem2                    psy_shortBlock_32000_41[42];
+
+       extern          psyDataElem3                    psy_data3_48000_20[21];
+       extern          psyDataElem3                    psy_data3_44100_20[21];
+       extern          psyDataElem3                    psy_data3_32000_20[21];
+
+       extern          psyDataElem3                    psy_data4_48000_11[12];
+       extern          psyDataElem3                    psy_data4_44100_11[12];
+       extern          psyDataElem3                    psy_data4_32000_11[12];
+
+
+#endif   /* NEW_L3PARM_TABLES */
+
+
+
+
+
+extern         double                                  enwindow[512];
+extern         char                                    aHuffcode[1498][36];
+
+
+
+
+
+#endif         /* __TABLES__ */