libmpcdec: src/requant.c Source File

Main Page | Class List | Directories | File List | Class Members | File Members | Related Pages

# requant.c

Go to the documentation of this file.

    00001 /*
    00002   Copyright (c) 2005, The Musepack Development Team
    00003   All rights reserved.
    00004
    00005   Redistribution and use in source and binary forms, with or without
    00006   modification, are permitted provided that the following conditions are
    00007   met:
    00008
    00009   * Redistributions of source code must retain the above copyright
    00010   notice, this list of conditions and the following disclaimer.
    00011
    00012   * Redistributions in binary form must reproduce the above
    00013   copyright notice, this list of conditions and the following
    00014   disclaimer in the documentation and/or other materials provided
    00015   with the distribution.
    00016
    00017   * Neither the name of the The Musepack Development Team nor the
    00018   names of its contributors may be used to endorse or promote
    00019   products derived from this software without specific prior
    00020   written permission.
    00021
    00022   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    00023   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    00024   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    00025   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    00026   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00027   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00028   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    00029   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    00030   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00031   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    00032   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00033 */
    00034
    00038
    00039 #include <mpcdec/mpcdec.h>
    00040 #include <mpcdec/internal.h>
    00041
    00042 /* C O N S T A N T S */
    00043 // bits per sample for chosen quantizer
    00044 const mpc_uint32_t  Res_bit [18] = {
    00045     0,  0,  0,  0,  0,  0,  0,  0,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16
    00046 };
    00047
    00048 // coefficients for requantization
    00049 // 65536/step bzw. 65536/(2*D+1)
    00050
    00051 #define _(X) MAKE_MPC_SAMPLE_EX(X,14)
    00052
    00053 const MPC_SAMPLE_FORMAT  __Cc [1 + 18] = {
    00054       _(111.285962475327f),                                        // 32768/2/255*sqrt(3)
    00055     _(65536.000000000000f), _(21845.333333333332f), _(13107.200000000001f), _(9362.285714285713f),
    00056     _(7281.777777777777f),  _(4369.066666666666f),  _(2114.064516129032f), _(1040.253968253968f),
    00057      _(516.031496062992f),  _(257.003921568627f),   _(128.250489236790f),   _(64.062561094819f),
    00058        _(32.015632633121f),    _(16.003907203907f),     _(8.000976681723f),    _(4.000244155527f),
    00059         _(2.000061037018f),     _(1.000015259021f)
    00060 };
    00061
    00062 #undef _
    00063
    00064 // offset for requantization
    00065 // 2*D+1 = steps of quantizer
    00066 const mpc_int32_t  __Dc [1 + 18] = {
    00067       2,
    00068       0,     1,     2,     3,     4,     7,    15,    31,    63,
    00069     127,   255,   511,  1023,  2047,  4095,  8191, 16383, 32767
    00070 };
    00071
    00072 #ifdef MPC_FIXED_POINT
    00073 static mpc_uint32_t find_shift(double fval)
    00074 {
    00075         mpc_int64_t val = (mpc_int64_t)fval;
    00076         if (val<0) val = -val;
    00077         mpc_uint32_t ptr = 0;
    00078         while(val) {val>>=1;ptr++;}
    00079
    00080         return ptr > 31 ? 0 : 31 - ptr;
    00081 }
    00082 #endif
    00083
    00084 /* F U N C T I O N S */
    00085
    00086 #define SET_SCF(N,X) d->SCF[N] = MAKE_MPC_SAMPLE_EX(X,d->SCF_shift[N] = (unsigned char)find_shift(X));
    00087
    00088 void
    00089 mpc_decoder_scale_output(mpc_decoder *d, double factor)
    00090 {
    00091     mpc_int32_t     n;
    00092     double  f1;
    00093     double  f2;
    00094 #ifndef MPC_FIXED_POINT
    00095         factor *= 1.0 / (double)(1<<(MPC_FIXED_POINT_SHIFT-1));
    00096 #else
    00097         factor *= 1.0 / (double)(1<<(16 - MPC_FIXED_POINT_SHIFT));
    00098 #endif
    00099     f1 = f2 = factor;
    00100
    00101     // handles +1.58...-98.41 dB, where's scf[n] / scf[n-1] = 1.20050805774840750476
    00102
    00103         SET_SCF(1,factor);
    00104
    00105         f1 *=   0.83298066476582673961;
    00106         f2 *= 1/0.83298066476582673961;
    00107
    00108     for ( n = 1; n <= 128; n++ ) {
    00109                 SET_SCF((unsigned char)(1+n),f1);
    00110                 SET_SCF((unsigned char)(1-n),f2);
    00111         f1 *=   0.83298066476582673961;
    00112         f2 *= 1/0.83298066476582673961;
    00113     }
    00114 }
    00115
    00116 static void
    00117 mpc_decoder_quantisierungsmodes(mpc_decoder *d) // conversion: index -> quantizer (bitstream reading)
    00118 {                                               // conversion: quantizer -> index (bitstream writing)
    00119     mpc_int32_t  Band = 0;
    00120     mpc_int32_t  i;
    00121
    00122     do {
    00123         d->Q_bit [Band] = 4;
    00124         for ( i = 0; i < 16-1; i++ )
    00125             d->Q_res [Band] [i] = i;
    00126         d->Q_res [Band][i] = 17;
    00127         Band++;
    00128     } while ( Band < 11 );
    00129
    00130     do {
    00131         d->Q_bit [Band] = 3;
    00132         for ( i = 0; i < 8-1; i++ )
    00133             d->Q_res [Band] [i] = i;
    00134         d->Q_res [Band] [i] = 17;
    00135         Band++;
    00136     } while ( Band < 23 );
    00137
    00138     do {
    00139         d->Q_bit [Band] = 2;
    00140         for ( i = 0; i < 4-1; i++ )
    00141             d->Q_res [Band] [i] = i;
    00142         d->Q_res [Band] [i] = 17;
    00143         Band++;
    00144     } while ( Band < 32 );
    00145 }
    00146
    00147 void
    00148 mpc_decoder_initialisiere_quantisierungstabellen(mpc_decoder *d, double scale_factor)
    00149 {
    00150     mpc_decoder_quantisierungsmodes(d);
    00151     mpc_decoder_scale_output(d, scale_factor);
    00152 }