summaryrefslogtreecommitdiff
path: root/apps/codecs/libmusepack/requant.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libmusepack/requant.c')
-rw-r--r--apps/codecs/libmusepack/requant.c90
1 files changed, 33 insertions, 57 deletions
diff --git a/apps/codecs/libmusepack/requant.c b/apps/codecs/libmusepack/requant.c
index d40f36fded..d8ded7ecf9 100644
--- a/apps/codecs/libmusepack/requant.c
+++ b/apps/codecs/libmusepack/requant.c
@@ -1,5 +1,5 @@
1/* 1/*
2 Copyright (c) 2005, The Musepack Development Team 2 Copyright (c) 2005-2009, The Musepack Development Team
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
@@ -31,117 +31,93 @@
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/ 33*/
34
35/// \file requant.c 34/// \file requant.c
36/// Requantization function implementations. 35/// Requantization function implementations.
37/// \todo document me 36/// \todo document me
38 37#include "mpcdec.h"
39#include "musepack.h" 38#include "requant.h"
40#include "internal.h" 39#include "mpcdec_math.h"
40#include "decoder.h"
41 41
42/* C O N S T A N T S */ 42/* C O N S T A N T S */
43// bits per sample for chosen quantizer 43// Bits per sample for chosen quantizer
44const mpc_uint32_t Res_bit [18] ICONST_ATTR = { 44const mpc_uint8_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 45 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
46}; 46};
47 47
48// coefficients for requantization 48// Requantization coefficients
49// 65536/step bzw. 65536/(2*D+1) 49// 65536/step bzw. 65536/(2*D+1)
50 50
51#define _(X) MAKE_MPC_SAMPLE_EX(X,14) 51#define _(X) MAKE_MPC_SAMPLE_EX(X,14)
52 52
53const MPC_SAMPLE_FORMAT __Cc [1 + 18] ICONST_ATTR = { 53const MPC_SAMPLE_FORMAT __Cc [1 + 18] ICONST_ATTR = {
54 _(111.285962475327f), // 32768/2/255*sqrt(3) 54 _(111.285962475327f), // 32768/2/255*sqrt(3)
55 _(65536.000000000000f), _(21845.333333333332f), _(13107.200000000001f), _(9362.285714285713f), 55 _(65536.000000000000f), _(21845.333333333332f), _(13107.200000000001f), _(9362.285714285713f),
56 _(7281.777777777777f), _(4369.066666666666f), _(2114.064516129032f), _(1040.253968253968f), 56 _(7281.777777777777f), _(4369.066666666666f), _(2114.064516129032f), _(1040.253968253968f),
57 _(516.031496062992f), _(257.003921568627f), _(128.250489236790f), _(64.062561094819f), 57 _(516.031496062992f), _(257.003921568627f), _(128.250489236790f), _(64.062561094819f),
58 _(32.015632633121f), _(16.003907203907f), _(8.000976681723f), _(4.000244155527f), 58 _(32.015632633121f), _(16.003907203907f), _(8.000976681723f), _(4.000244155527f),
59 _(2.000061037018f), _(1.000015259021f) 59 _(2.000061037018f), _(1.000015259021f)
60}; 60};
61 61
62#undef _ 62#undef _
63 63
64// offset for requantization 64// Requantization offset
65// 2*D+1 = steps of quantizer 65// 2*D+1 = steps of quantizer
66const mpc_int32_t __Dc [1 + 18] ICONST_ATTR = { 66const mpc_int16_t __Dc [1 + 18] ICONST_ATTR = {
67 2, 67 2,
68 0, 1, 2, 3, 4, 7, 15, 31, 63, 68 0, 1, 2, 3, 4, 7, 15, 31, 63,
69 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767 69 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767
70}; 70};
71 71
72// decoding results (requantized) for bundled quantizers (3- and 5-step)
73// 1st value of bundled 3-step quantizer
74const mpc_int32_t idx30[27] ICONST_ATTR = { -1, 0, 1,-1, 0, 1,-1, 0, 1,
75 -1, 0, 1,-1, 0, 1,-1, 0, 1,
76 -1, 0, 1,-1, 0, 1,-1, 0, 1};
77// 2nd value of bundled 3-step quantizer
78const mpc_int32_t idx31[27] ICONST_ATTR = { -1,-1,-1, 0, 0, 0, 1, 1, 1,
79 -1,-1,-1, 0, 0, 0, 1, 1, 1,
80 -1,-1,-1, 0, 0, 0, 1, 1, 1};
81// 3rd value of bundled 3-step quantizer
82const mpc_int32_t idx32[27] ICONST_ATTR = { -1,-1,-1,-1,-1,-1,-1,-1,-1,
83 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 1, 1, 1, 1, 1, 1, 1, 1, 1};
85// 1st value of bundled 5-step quantizer
86const mpc_int32_t idx50[25] ICONST_ATTR = { -2,-1, 0, 1, 2,
87 -2,-1, 0, 1, 2,
88 -2,-1, 0, 1, 2,
89 -2,-1, 0, 1, 2,
90 -2,-1, 0, 1, 2};
91// 2nd value of bundled 5-step quantizer
92const mpc_int32_t idx51[25] ICONST_ATTR = { -2,-2,-2,-2,-2,
93 -1,-1,-1,-1,-1,
94 0, 0, 0, 0, 0,
95 1, 1, 1, 1, 1,
96 2, 2, 2, 2, 2};
97
98
99#ifdef MPC_FIXED_POINT 72#ifdef MPC_FIXED_POINT
100static mpc_uint32_t find_shift(double fval) 73static mpc_uint32_t find_shift(double fval)
101{ 74{
102 mpc_int64_t val = (mpc_int64_t)fval; 75 mpc_int64_t val = (mpc_int64_t) fval;
103 mpc_uint32_t ptr = 0; 76 mpc_uint32_t ptr = 0;
104 if (val<0) val = -val; 77 if(val<0)
105 while(val) {val>>=1;ptr++;} 78 val = -val;
106 79 while(val)
80 {
81 val >>= 1;
82 ptr++;
83 }
107 return ptr > 31 ? 0 : 31 - ptr; 84 return ptr > 31 ? 0 : 31 - ptr;
108} 85}
109#endif 86#endif
110 87
111/* F U N C T I O N S */ 88/* F U N C T I O N S */
112 89
113#define SET_SCF(N,X) d->SCF[N] = MAKE_MPC_SAMPLE_EX(X,d->SCF_shift[N] = (unsigned char)find_shift(X)); 90#define SET_SCF(N,X) d->SCF[N] = MAKE_MPC_SAMPLE_EX(X,d->SCF_shift[N] = (mpc_uint8_t) find_shift(X));
114 91
115void 92void
116mpc_decoder_scale_output(mpc_decoder *d, double factor) 93mpc_decoder_scale_output(mpc_decoder *d, double factor)
117{ 94{
118 mpc_int32_t n; 95 mpc_int32_t n; double f1, f2;
119 double f1; 96
120 double f2;
121#ifndef MPC_FIXED_POINT 97#ifndef MPC_FIXED_POINT
122 factor *= 1.0 / (double)(1<<(MPC_FIXED_POINT_SHIFT-1)); 98 factor *= 1.0 / (double) (1<<(MPC_FIXED_POINT_SHIFT-1));
123#else 99#else
124 factor *= 1.0 / (double)(1<<(16 - MPC_FIXED_POINT_SHIFT)); 100 factor *= 1.0 / (double) (1<<(16-MPC_FIXED_POINT_SHIFT));
125#endif 101#endif
126 f1 = f2 = factor; 102 f1 = f2 = factor;
127 103
128 // handles +1.58...-98.41 dB, where's scf[n] / scf[n-1] = 1.20050805774840750476 104 // handles +1.58...-98.41 dB, where's scf[n] / scf[n-1] = 1.20050805774840750476
129 105
130 SET_SCF(1,factor); 106 SET_SCF(1,factor);
131 107
132 f1 *= 0.83298066476582673961; 108 f1 *= 0.83298066476582673961;
133 f2 *= 1/0.83298066476582673961; 109 f2 *= 1/0.83298066476582673961;
134 110
135 for ( n = 1; n <= 128; n++ ) { 111 for ( n = 1; n <= 128; n++ ) {
136 SET_SCF((unsigned char)(1+n),f1); 112 SET_SCF((mpc_uint8_t)(1+n),f1);
137 SET_SCF((unsigned char)(1-n),f2); 113 SET_SCF((mpc_uint8_t)(1-n),f2);
138 f1 *= 0.83298066476582673961; 114 f1 *= 0.83298066476582673961;
139 f2 *= 1/0.83298066476582673961; 115 f2 *= 1/0.83298066476582673961;
140 } 116 }
141} 117}
142 118
143void 119void
144mpc_decoder_initialisiere_quantisierungstabellen(mpc_decoder *d, double scale_factor) 120mpc_decoder_init_quant(mpc_decoder *d, double scale_factor)
145{ 121{
146 mpc_decoder_scale_output(d, scale_factor); 122 mpc_decoder_scale_output(d, scale_factor);
147} 123}