40 |
* |
* |
41 |
*************************************************************************/ |
*************************************************************************/ |
42 |
|
|
43 |
|
#include "../global.h" |
44 |
#include "quant_mpeg4.h" |
#include "quant_mpeg4.h" |
45 |
|
#include "quant_matrix.h" |
46 |
|
|
47 |
|
/* function pointers */ |
|
|
|
|
// function pointers |
|
48 |
quant_intraFuncPtr quant4_intra; |
quant_intraFuncPtr quant4_intra; |
49 |
quant_intraFuncPtr dequant4_intra; |
quant_intraFuncPtr dequant4_intra; |
50 |
dequant_interFuncPtr dequant4_inter; |
dequant_interFuncPtr dequant4_inter; |
51 |
quant_interFuncPtr quant4_inter; |
quant_interFuncPtr quant4_inter; |
52 |
|
|
53 |
|
|
|
#define DIV_DIV(A,B) ( (A) > 0 ? ((A)+((B)>>1))/(B) : ((A)-((B)>>1))/(B) ) |
|
|
#define SIGN(A) ((A)>0?1:-1) |
|
54 |
#define VM18P 3 |
#define VM18P 3 |
55 |
#define VM18Q 4 |
#define VM18Q 4 |
56 |
|
|
57 |
|
|
58 |
// divide-by-multiply table |
/* |
59 |
// need 17 bit shift (16 causes slight errors when q > 19) |
* divide-by-multiply table |
60 |
|
* need 17 bit shift (16 causes slight errors when q > 19) |
61 |
|
*/ |
62 |
|
|
63 |
#define SCALEBITS 17 |
#define SCALEBITS 17 |
64 |
#define FIX(X) ((1UL << SCALEBITS) / (X) + 1) |
#define FIX(X) ((1UL << SCALEBITS) / (X) + 1) |
65 |
|
|
66 |
static const uint32_t multipliers[32] = |
static const uint32_t multipliers[32] = { |
|
{ |
|
67 |
0, FIX(2), FIX(4), FIX(6), |
0, FIX(2), FIX(4), FIX(6), |
68 |
FIX(8), FIX(10), FIX(12), FIX(14), |
FIX(8), FIX(10), FIX(12), FIX(14), |
69 |
FIX(16), FIX(18), FIX(20), FIX(22), |
FIX(16), FIX(18), FIX(20), FIX(22), |
74 |
FIX(56), FIX(58), FIX(60), FIX(62) |
FIX(56), FIX(58), FIX(60), FIX(62) |
75 |
}; |
}; |
76 |
|
|
|
|
|
|
static const int16_t default_intra_matrix[64] = { |
|
|
8,17,18,19,21,23,25,27, |
|
|
17,18,19,21,23,25,27,28, |
|
|
20,21,22,23,24,26,28,30, |
|
|
21,22,23,24,26,28,30,32, |
|
|
22,23,24,26,28,30,32,35, |
|
|
23,24,26,28,30,32,35,38, |
|
|
25,26,28,30,32,35,38,41, |
|
|
27,28,30,32,35,38,41,45 |
|
|
}; |
|
|
|
|
|
static const int16_t default_inter_matrix[64] = { |
|
|
16,17,18,19,20,21,22,23, |
|
|
17,18,19,20,21,22,23,24, |
|
|
18,19,20,21,22,23,24,25, |
|
|
19,20,21,22,23,24,26,27, |
|
|
20,21,22,23,25,26,27,28, |
|
|
21,22,23,24,26,27,28,30, |
|
|
22,23,24,26,27,28,30,31, |
|
|
23,24,25,27,28,30,31,33 |
|
|
}; |
|
|
|
|
|
|
|
77 |
/* quantize intra-block |
/* quantize intra-block |
78 |
|
* |
79 |
// const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q); |
* const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q); |
80 |
// |
* level = DIV_DIV(16 * data[i], default_intra_matrix[i]); |
81 |
// level = DIV_DIV(16 * data[i], default_intra_matrix[i]); |
* coeff[i] = (level + quantd) / quant2; |
|
// coeff[i] = (level + quantd) / quant2; |
|
82 |
*/ |
*/ |
83 |
|
|
84 |
void quant4_intra_c(int16_t * coeff, const int16_t * data, const uint32_t quant, const uint32_t dcscalar) |
void |
85 |
|
quant4_intra_c(int16_t * coeff, |
86 |
|
const int16_t * data, |
87 |
|
const uint32_t quant, |
88 |
|
const uint32_t dcscalar) |
89 |
{ |
{ |
90 |
const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q; |
const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q; |
91 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
92 |
uint32_t i; |
uint32_t i; |
93 |
|
int16_t *intra_matrix; |
94 |
|
|
95 |
|
intra_matrix = get_intra_matrix(); |
96 |
|
|
97 |
coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar); |
coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar); |
98 |
|
|
99 |
for (i = 1; i < 64; i++) |
for (i = 1; i < 64; i++) { |
100 |
{ |
if (data[i] < 0) { |
|
if (data[i] < 0) |
|
|
{ |
|
101 |
uint32_t level = -data[i]; |
uint32_t level = -data[i]; |
102 |
level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i]; |
|
103 |
|
level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; |
104 |
level = ((level + quantd) * mult) >> 17; |
level = ((level + quantd) * mult) >> 17; |
105 |
coeff[i] = -(int16_t)level; |
coeff[i] = -(int16_t)level; |
106 |
} |
} else if (data[i] > 0) { |
|
else if (data[i] > 0) |
|
|
{ |
|
107 |
uint32_t level = data[i]; |
uint32_t level = data[i]; |
108 |
level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i]; |
|
109 |
|
level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; |
110 |
level = ((level + quantd) * mult) >> 17; |
level = ((level + quantd) * mult) >> 17; |
111 |
coeff[i] = level; |
coeff[i] = level; |
112 |
} |
} else { |
|
else |
|
|
{ |
|
113 |
coeff[i] = 0; |
coeff[i] = 0; |
114 |
} |
} |
115 |
} |
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
/* dequantize intra-block & clamp to [-2048,2047] |
/* |
121 |
// data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4; |
* dequantize intra-block & clamp to [-2048,2047] |
122 |
|
* |
123 |
|
* data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4; |
124 |
*/ |
*/ |
125 |
|
|
126 |
void dequant4_intra_c(int16_t *data, const int16_t *coeff, const uint32_t quant, const uint32_t dcscalar) |
void |
127 |
|
dequant4_intra_c(int16_t * data, |
128 |
|
const int16_t * coeff, |
129 |
|
const uint32_t quant, |
130 |
|
const uint32_t dcscalar) |
131 |
{ |
{ |
132 |
uint32_t i; |
uint32_t i; |
133 |
|
int16_t *intra_matrix; |
134 |
|
|
135 |
|
intra_matrix = get_intra_matrix(); |
136 |
|
|
137 |
data[0] = coeff[0] * dcscalar; |
data[0] = coeff[0] * dcscalar; |
138 |
if (data[0] < -2048) |
if (data[0] < -2048) { |
|
{ |
|
139 |
data[0] = -2048; |
data[0] = -2048; |
140 |
} |
} else if (data[0] > 2047) { |
|
else if (data[0] > 2047) |
|
|
{ |
|
141 |
data[0] = 2047; |
data[0] = 2047; |
142 |
} |
} |
143 |
|
|
144 |
for (i = 1; i < 64; i++) |
for (i = 1; i < 64; i++) { |
145 |
{ |
if (coeff[i] == 0) { |
|
if (coeff[i] == 0) |
|
|
{ |
|
146 |
data[i] = 0; |
data[i] = 0; |
147 |
} |
} else if (coeff[i] < 0) { |
|
else if (coeff[i] < 0) |
|
|
{ |
|
148 |
uint32_t level = -coeff[i]; |
uint32_t level = -coeff[i]; |
149 |
level = (level * default_intra_matrix[i] * quant) >> 3; |
|
150 |
|
level = (level * intra_matrix[i] * quant) >> 3; |
151 |
data[i] = (level <= 2048 ? -(int16_t)level : -2048); |
data[i] = (level <= 2048 ? -(int16_t)level : -2048); |
152 |
} |
} else /* if (coeff[i] > 0) */ |
|
else // if (coeff[i] > 0) |
|
153 |
{ |
{ |
154 |
uint32_t level = coeff[i]; |
uint32_t level = coeff[i]; |
155 |
level = (level * default_intra_matrix[i] * quant) >> 3; |
|
156 |
|
level = (level * intra_matrix[i] * quant) >> 3; |
157 |
data[i] = (level <= 2047 ? level : 2047); |
data[i] = (level <= 2047 ? level : 2047); |
158 |
} |
} |
159 |
} |
} |
162 |
|
|
163 |
|
|
164 |
/* quantize inter-block |
/* quantize inter-block |
165 |
|
* |
166 |
// level = DIV_DIV(16 * data[i], default_intra_matrix[i]); |
* level = DIV_DIV(16 * data[i], default_intra_matrix[i]); |
167 |
// coeff[i] = (level + quantd) / quant2; |
* coeff[i] = (level + quantd) / quant2; |
168 |
// sum += abs(level); |
* sum += abs(level); |
169 |
*/ |
*/ |
170 |
|
|
171 |
uint32_t quant4_inter_c(int16_t * coeff, const int16_t * data, const uint32_t quant) |
uint32_t |
172 |
|
quant4_inter_c(int16_t * coeff, |
173 |
|
const int16_t * data, |
174 |
|
const uint32_t quant) |
175 |
{ |
{ |
176 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
177 |
uint32_t sum = 0; |
uint32_t sum = 0; |
178 |
uint32_t i; |
uint32_t i; |
179 |
|
int16_t *inter_matrix; |
180 |
|
|
181 |
for (i = 0; i < 64; i++) |
inter_matrix = get_inter_matrix(); |
182 |
{ |
|
183 |
if (data[i] < 0) |
for (i = 0; i < 64; i++) { |
184 |
{ |
if (data[i] < 0) { |
185 |
uint32_t level = -data[i]; |
uint32_t level = -data[i]; |
186 |
level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i]; |
|
187 |
|
level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i]; |
188 |
level = (level * mult) >> 17; |
level = (level * mult) >> 17; |
189 |
sum += level; |
sum += level; |
190 |
coeff[i] = -(int16_t)level; |
coeff[i] = -(int16_t)level; |
191 |
} |
} else if (data[i] > 0) { |
|
else if (data[i] > 0) |
|
|
{ |
|
192 |
uint32_t level = data[i]; |
uint32_t level = data[i]; |
193 |
level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i]; |
|
194 |
|
level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i]; |
195 |
level = (level * mult) >> 17; |
level = (level * mult) >> 17; |
196 |
sum += level; |
sum += level; |
197 |
coeff[i] = level; |
coeff[i] = level; |
198 |
} |
} else { |
|
else |
|
|
{ |
|
199 |
coeff[i] = 0; |
coeff[i] = 0; |
200 |
} |
} |
201 |
} |
} |
208 |
data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16 |
data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16 |
209 |
*/ |
*/ |
210 |
|
|
211 |
void dequant4_inter_c(int16_t *data, const int16_t *coeff, const uint32_t quant) |
void |
212 |
|
dequant4_inter_c(int16_t * data, |
213 |
|
const int16_t * coeff, |
214 |
|
const uint32_t quant) |
215 |
{ |
{ |
216 |
uint32_t sum = 0; |
uint32_t sum = 0; |
217 |
uint32_t i; |
uint32_t i; |
218 |
|
int16_t *inter_matrix; |
219 |
|
|
220 |
for (i = 0; i < 64; i++) |
inter_matrix = get_inter_matrix(); |
221 |
{ |
|
222 |
if (coeff[i] == 0) |
for (i = 0; i < 64; i++) { |
223 |
{ |
if (coeff[i] == 0) { |
224 |
data[i] = 0; |
data[i] = 0; |
225 |
} |
} else if (coeff[i] < 0) { |
|
else if (coeff[i] < 0) |
|
|
{ |
|
226 |
int32_t level = -coeff[i]; |
int32_t level = -coeff[i]; |
227 |
level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4; |
|
228 |
|
level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; |
229 |
data[i] = (level <= 2048 ? -level : -2048); |
data[i] = (level <= 2048 ? -level : -2048); |
230 |
} |
} else /* if (coeff[i] > 0) */ |
|
else // if (coeff[i] > 0) |
|
231 |
{ |
{ |
232 |
uint32_t level = coeff[i]; |
uint32_t level = coeff[i]; |
233 |
level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4; |
|
234 |
|
level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; |
235 |
data[i] = (level <= 2047 ? level : 2047); |
data[i] = (level <= 2047 ? level : 2047); |
236 |
} |
} |
237 |
|
|
238 |
sum ^= data[i]; |
sum ^= data[i]; |
239 |
} |
} |
240 |
|
|
241 |
// mismatch control |
/* mismatch control */ |
242 |
|
|
243 |
if ((sum & 1) == 0) |
if ((sum & 1) == 0) { |
|
{ |
|
244 |
data[63] ^= 1; |
data[63] ^= 1; |
245 |
} |
} |
246 |
} |
} |