Some changes for musepack: Disable SV4-SV6 support (I guess I am the only one who...
[kugel-rb.git] / apps / codecs / libmusepack / requant.c
blob3282286d74c1a9546fb0b29557ef70e81af90e6c
1 /*
2 Copyright (c) 2005, The Musepack Development Team
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
17 * Neither the name of the The Musepack Development Team nor the
18 names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior
20 written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /// \file requant.c
36 /// Requantization function implementations.
37 /// \todo document me
39 #include "musepack.h"
40 #include "internal.h"
42 /* C O N S T A N T S */
43 // bits per sample for chosen quantizer
44 const mpc_uint32_t Res_bit [18] ICONST_ATTR = {
45 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
48 // coefficients for requantization
49 // 65536/step bzw. 65536/(2*D+1)
51 #define _(X) MAKE_MPC_SAMPLE_EX(X,14)
53 const MPC_SAMPLE_FORMAT __Cc [1 + 18] ICONST_ATTR = {
54 _(111.285962475327f), // 32768/2/255*sqrt(3)
55 _(65536.000000000000f), _(21845.333333333332f), _(13107.200000000001f), _(9362.285714285713f),
56 _(7281.777777777777f), _(4369.066666666666f), _(2114.064516129032f), _(1040.253968253968f),
57 _(516.031496062992f), _(257.003921568627f), _(128.250489236790f), _(64.062561094819f),
58 _(32.015632633121f), _(16.003907203907f), _(8.000976681723f), _(4.000244155527f),
59 _(2.000061037018f), _(1.000015259021f)
62 #undef _
64 // offset for requantization
65 // 2*D+1 = steps of quantizer
66 const mpc_int32_t __Dc [1 + 18] ICONST_ATTR = {
68 0, 1, 2, 3, 4, 7, 15, 31, 63,
69 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767
72 #ifdef MPC_FIXED_POINT
73 static mpc_uint32_t find_shift(double fval)
75 mpc_int64_t val = (mpc_int64_t)fval;
76 mpc_uint32_t ptr = 0;
77 if (val<0) val = -val;
78 while(val) {val>>=1;ptr++;}
80 return ptr > 31 ? 0 : 31 - ptr;
82 #endif
84 /* F U N C T I O N S */
86 #define SET_SCF(N,X) d->SCF[N] = MAKE_MPC_SAMPLE_EX(X,d->SCF_shift[N] = (unsigned char)find_shift(X));
88 void
89 mpc_decoder_scale_output(mpc_decoder *d, double factor)
91 mpc_int32_t n;
92 double f1;
93 double f2;
94 #ifndef MPC_FIXED_POINT
95 factor *= 1.0 / (double)(1<<(MPC_FIXED_POINT_SHIFT-1));
96 #else
97 factor *= 1.0 / (double)(1<<(16 - MPC_FIXED_POINT_SHIFT));
98 #endif
99 f1 = f2 = factor;
101 // handles +1.58...-98.41 dB, where's scf[n] / scf[n-1] = 1.20050805774840750476
103 SET_SCF(1,factor);
105 f1 *= 0.83298066476582673961;
106 f2 *= 1/0.83298066476582673961;
108 for ( n = 1; n <= 128; n++ ) {
109 SET_SCF((unsigned char)(1+n),f1);
110 SET_SCF((unsigned char)(1-n),f2);
111 f1 *= 0.83298066476582673961;
112 f2 *= 1/0.83298066476582673961;
116 void
117 mpc_decoder_initialisiere_quantisierungstabellen(mpc_decoder *d, double scale_factor)
119 mpc_decoder_scale_output(d, scale_factor);