summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libmusepack/requant.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libmusepack/requant.c')
-rw-r--r--lib/rbcodec/codecs/libmusepack/requant.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libmusepack/requant.c b/lib/rbcodec/codecs/libmusepack/requant.c
new file mode 100644
index 0000000000..fc27dfd199
--- /dev/null
+++ b/lib/rbcodec/codecs/libmusepack/requant.c
@@ -0,0 +1,184 @@
1/*
2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
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.
16
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.
21
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.
33*/
34/// \file requant.c
35/// Requantization function implementations.
36/// \todo document me
37#include "mpcdec.h"
38#include "requant.h"
39#include "mpcdec_math.h"
40#include "decoder.h"
41#include "internal.h"
42#include <string.h>
43
44/* C O N S T A N T S */
45// Bits per sample for chosen quantizer
46const mpc_uint8_t Res_bit [18] ICONST_ATTR = {
47 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
48};
49
50// Requantization coefficients
51// 65536/step bzw. 65536/(2*D+1)
52
53#define _(X) MAKE_MPC_SAMPLE_EX(X,14)
54
55const MPC_SAMPLE_FORMAT __Cc [1 + 18] ICONST_ATTR = {
56 _(111.285962475327f), // 32768/2/255*sqrt(3)
57 _(65536.000000000000f), _(21845.333333333332f), _(13107.200000000001f), _(9362.285714285713f),
58 _(7281.777777777777f), _(4369.066666666666f), _(2114.064516129032f), _(1040.253968253968f),
59 _(516.031496062992f), _(257.003921568627f), _(128.250489236790f), _(64.062561094819f),
60 _(32.015632633121f), _(16.003907203907f), _(8.000976681723f), _(4.000244155527f),
61 _(2.000061037018f), _(1.000015259021f)
62};
63
64#undef _
65
66// Requantization offset
67// 2*D+1 = steps of quantizer
68const mpc_int16_t __Dc [1 + 18] ICONST_ATTR = {
69 2,
70 0, 1, 2, 3, 4, 7, 15, 31, 63,
71 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767
72};
73
74// Table'ized SCF calculated from mpc_decoder_scale_output(d, 1.0)
75static const mpc_uint32_t SCF[] = {
76 1289035711, 1073741824, 1788812356, 1490046106, 1241179595, 1033878604, 861200887, 717363687,
77 597550081, 497747664, 414614180, 345365595, 287682863, 239634262, 199610707, 166271859,
78 138501244, 115368858, 96100028, 80049465, 66679657, 55542865, 46266132, 38538793,
79 32102070, 26740403, 22274239, 18554010, 15455132, 12873826, 10723648, 8932591,
80 7440676, 6197939, 5162763, 4300482, 3582218, 2983918, 2485546, 2070412,
81 1724613, 1436569, 1196634, 996773, 830293, 691618, 576104, 479883,
82 399734, 332970, 277358, 231034, 192446, 160304, 133530, 111228,
83 92651, 77176, 64286, 53549, 44605, 37155, 30949, 25780,
84 21474, 17888, 14900, 12411, 10338, 8612, 7173, 5975,
85 4977, 4146, 3453, 2876, 2396, 1996, 1662, 1385,
86 1153, 961, 800, 666, 555, 462, 385, 321,
87 267, 222, 185, 154, 128, 107, 89, 74,
88 61, 51, 43, 35, 29, 24, 20, 17,
89 14, 11, 9, 8, 6, 5, 4, 3,
90 3, 2, 2, 1, 1, 1, 1, 0,
91 0, 0, 0, 0, 0, 0, 0, 0,
92 0, 2147483648, 2147483648, 2147483648, 2147483648, 2147483648, 2147483648, 2147483648,
93 2147483648, 2147483648, 2147483648, 2147483648, 1930697728, 1608233877, 1339627724, 1115883992,
94 1859019579, 1548527365, 1289893354, 1074456223, 1790002518, 1491037488, 1242005398, 2069132964,
95 1723547752, 1435681952, 1195895306, 1992315335, 1659560152, 1382381519, 1151497076, 1918349601,
96 1597948125, 1331059892, 1108747153, 1847129882, 1538623477, 1281643607, 2135168687, 1778554232,
97 1481501287, 1234061927, 2055899448, 1712524489, 1426499787, 1188246741, 1979573121, 1648946134,
98 1373540247, 1144132468, 1906080447, 1587728158, 1322546856, 1101655960, 1835316227, 1528782931,
99 1273446622, 2121512828, 1767179166, 1472026076, 1226169259, 2042750570, 1701571728, 1417376349,
100 1180647093, 1966912401, 1638400000, 1364755521, 1136814961, 1893889764, 1577573554, 1314088268,
101 1094610119, 1823578129, 1519005322, 1265302063, 2107944308, 1755876851, 1462611466, 1218327071,
102 2029685788, 1690689017, 1408311261, 1173096050, 1954332656, 1627921315, 1356026979, 1129544254,
103 1881777048, 1567483896, 1305683778, 1087609341, 1811915104, 1509290248, 1257209594, 2094462567,
104 1744646821, 1453257069, 1210535039, 2016704564, 1679875908, 1399304151, 1165593302, 1941833367,
105 1617509648, 1347354262, 1122320049, 1869741801, 1557458768, 1297333040, 1080653338, 1800326672,
106 1499637308, 1249168882, 2081067051, 1733488616, 1443962500, 1202792843, 2003806364, 1669131957,
107 1390354647, 1158138538, 1929414019, 1607164572, 1338737013, 1115142047, 1857783528, 1547497758
108};
109
110// Table'ized SCF_shift calculated from mpc_decoder_scale_output(d, 1.0)
111static const mpc_uint8_t SCF_shift[] = {
112 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
113 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
114 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
115 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
116 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
117 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
118 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
119 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
120 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
122 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9,
123 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13,
124 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 17, 17, 17, 17,
125 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 22,
126 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26,
127 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30
128};
129
130/* F U N C T I O N S */
131/* not used anymore, tables from above are used
132#ifdef MPC_FIXED_POINT
133static mpc_uint32_t find_shift(double fval)
134{
135 mpc_int64_t val = (mpc_int64_t) fval;
136 mpc_uint32_t ptr = 0;
137 if(val<0)
138 val = -val;
139 while(val)
140 {
141 val >>= 1;
142 ptr++;
143 }
144 return ptr > 31 ? 0 : 31 - ptr;
145}
146#endif
147
148#define SET_SCF(N,X) d->SCF[N] = MAKE_MPC_SAMPLE_EX(X,d->SCF_shift[N] = (mpc_uint8_t) find_shift(X));
149
150static void
151mpc_decoder_scale_output(mpc_decoder *d, double factor)
152{
153 mpc_int32_t n; double f1, f2;
154
155#ifndef MPC_FIXED_POINT
156 factor *= 1.0 / (double) (1<<(MPC_FIXED_POINT_SHIFT-1));
157#else
158 factor *= 1.0 / (double) (1<<(16-MPC_FIXED_POINT_SHIFT));
159#endif
160 f1 = f2 = factor;
161
162 // handles +1.58...-98.41 dB, where's scf[n] / scf[n-1] = 1.20050805774840750476
163
164 SET_SCF(1,factor);
165
166 f1 *= 0.83298066476582673961;
167 f2 *= 1/0.83298066476582673961;
168
169 for ( n = 1; n <= 128; n++ ) {
170 SET_SCF((mpc_uint8_t)(1+n),f1);
171 SET_SCF((mpc_uint8_t)(1-n),f2);
172 f1 *= 0.83298066476582673961;
173 f2 *= 1/0.83298066476582673961;
174 }
175}
176*/
177void
178mpc_decoder_init_quant(mpc_decoder *d, MPC_SAMPLE_FORMAT factor)
179{
180 //mpc_decoder_scale_output(d, (double)factor / MPC_FIXED_POINT_SHIFT)
181 (void)factor;
182 memcpy(d->SCF, SCF, sizeof(d->SCF));
183 memcpy(d->SCF_shift, SCF_shift, sizeof(d->SCF_shift));
184}