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; |
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 |
|
|
61 |
#define SCALEBITS 17 |
#define SCALEBITS 17 |
62 |
#define FIX(X) ((1UL << SCALEBITS) / (X) + 1) |
#define FIX(X) ((1UL << SCALEBITS) / (X) + 1) |
63 |
|
|
64 |
static const uint32_t multipliers[32] = |
static const uint32_t multipliers[32] = { |
|
{ |
|
65 |
0, FIX(2), FIX(4), FIX(6), |
0, FIX(2), FIX(4), FIX(6), |
66 |
FIX(8), FIX(10), FIX(12), FIX(14), |
FIX(8), FIX(10), FIX(12), FIX(14), |
67 |
FIX(16), FIX(18), FIX(20), FIX(22), |
FIX(16), FIX(18), FIX(20), FIX(22), |
72 |
FIX(56), FIX(58), FIX(60), FIX(62) |
FIX(56), FIX(58), FIX(60), FIX(62) |
73 |
}; |
}; |
74 |
|
|
|
|
|
|
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 |
|
|
}; |
|
|
|
|
|
|
|
75 |
/* quantize intra-block |
/* quantize intra-block |
76 |
|
|
77 |
// const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q); |
// const int32_t quantd = DIV_DIV(VM18P*quant, VM18Q); |
80 |
// coeff[i] = (level + quantd) / quant2; |
// coeff[i] = (level + quantd) / quant2; |
81 |
*/ |
*/ |
82 |
|
|
83 |
void quant4_intra_c(int16_t * coeff, const int16_t * data, const uint32_t quant, const uint32_t dcscalar) |
void |
84 |
|
quant4_intra_c(int16_t * coeff, |
85 |
|
const int16_t * data, |
86 |
|
const uint32_t quant, |
87 |
|
const uint32_t dcscalar) |
88 |
{ |
{ |
89 |
const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q; |
const uint32_t quantd = ((VM18P*quant) + (VM18Q/2)) / VM18Q; |
90 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
91 |
uint32_t i; |
uint32_t i; |
92 |
|
int16_t *intra_matrix; |
93 |
|
|
94 |
|
intra_matrix = get_intra_matrix(); |
95 |
|
|
96 |
coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar); |
coeff[0] = DIV_DIV(data[0], (int32_t)dcscalar); |
97 |
|
|
98 |
for (i = 1; i < 64; i++) |
for (i = 1; i < 64; i++) { |
99 |
{ |
if (data[i] < 0) { |
|
if (data[i] < 0) |
|
|
{ |
|
100 |
uint32_t level = -data[i]; |
uint32_t level = -data[i]; |
101 |
level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i]; |
|
102 |
|
level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; |
103 |
level = ((level + quantd) * mult) >> 17; |
level = ((level + quantd) * mult) >> 17; |
104 |
coeff[i] = -(int16_t)level; |
coeff[i] = -(int16_t)level; |
105 |
} |
} else if (data[i] > 0) { |
|
else if (data[i] > 0) |
|
|
{ |
|
106 |
uint32_t level = data[i]; |
uint32_t level = data[i]; |
107 |
level = ((level<<4) + (default_intra_matrix[i]>>1)) / default_intra_matrix[i]; |
|
108 |
|
level = ((level << 4) + (intra_matrix[i] >> 1)) / intra_matrix[i]; |
109 |
level = ((level + quantd) * mult) >> 17; |
level = ((level + quantd) * mult) >> 17; |
110 |
coeff[i] = level; |
coeff[i] = level; |
111 |
} |
} else { |
|
else |
|
|
{ |
|
112 |
coeff[i] = 0; |
coeff[i] = 0; |
113 |
} |
} |
114 |
} |
} |
120 |
// data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4; |
// data[i] = (coeff[i] * default_intra_matrix[i] * quant2) >> 4; |
121 |
*/ |
*/ |
122 |
|
|
123 |
void dequant4_intra_c(int16_t *data, const int16_t *coeff, const uint32_t quant, const uint32_t dcscalar) |
void |
124 |
|
dequant4_intra_c(int16_t * data, |
125 |
|
const int16_t * coeff, |
126 |
|
const uint32_t quant, |
127 |
|
const uint32_t dcscalar) |
128 |
{ |
{ |
129 |
uint32_t i; |
uint32_t i; |
130 |
|
int16_t *intra_matrix; |
131 |
|
|
132 |
|
intra_matrix = get_intra_matrix(); |
133 |
|
|
134 |
data[0] = coeff[0] * dcscalar; |
data[0] = coeff[0] * dcscalar; |
135 |
if (data[0] < -2048) |
if (data[0] < -2048) { |
|
{ |
|
136 |
data[0] = -2048; |
data[0] = -2048; |
137 |
} |
} else if (data[0] > 2047) { |
|
else if (data[0] > 2047) |
|
|
{ |
|
138 |
data[0] = 2047; |
data[0] = 2047; |
139 |
} |
} |
140 |
|
|
141 |
for (i = 1; i < 64; i++) |
for (i = 1; i < 64; i++) { |
142 |
{ |
if (coeff[i] == 0) { |
|
if (coeff[i] == 0) |
|
|
{ |
|
143 |
data[i] = 0; |
data[i] = 0; |
144 |
} |
} else if (coeff[i] < 0) { |
|
else if (coeff[i] < 0) |
|
|
{ |
|
145 |
uint32_t level = -coeff[i]; |
uint32_t level = -coeff[i]; |
146 |
level = (level * default_intra_matrix[i] * quant) >> 3; |
|
147 |
|
level = (level * intra_matrix[i] * quant) >> 3; |
148 |
data[i] = (level <= 2048 ? -(int16_t)level : -2048); |
data[i] = (level <= 2048 ? -(int16_t)level : -2048); |
149 |
} |
} else // if (coeff[i] > 0) |
|
else // if (coeff[i] > 0) |
|
150 |
{ |
{ |
151 |
uint32_t level = coeff[i]; |
uint32_t level = coeff[i]; |
152 |
level = (level * default_intra_matrix[i] * quant) >> 3; |
|
153 |
|
level = (level * intra_matrix[i] * quant) >> 3; |
154 |
data[i] = (level <= 2047 ? level : 2047); |
data[i] = (level <= 2047 ? level : 2047); |
155 |
} |
} |
156 |
} |
} |
165 |
// sum += abs(level); |
// sum += abs(level); |
166 |
*/ |
*/ |
167 |
|
|
168 |
uint32_t quant4_inter_c(int16_t * coeff, const int16_t * data, const uint32_t quant) |
uint32_t |
169 |
|
quant4_inter_c(int16_t * coeff, |
170 |
|
const int16_t * data, |
171 |
|
const uint32_t quant) |
172 |
{ |
{ |
173 |
const uint32_t mult = multipliers[quant]; |
const uint32_t mult = multipliers[quant]; |
174 |
uint32_t sum = 0; |
uint32_t sum = 0; |
175 |
uint32_t i; |
uint32_t i; |
176 |
|
int16_t *inter_matrix; |
177 |
|
|
178 |
for (i = 0; i < 64; i++) |
inter_matrix = get_inter_matrix(); |
179 |
{ |
|
180 |
if (data[i] < 0) |
for (i = 0; i < 64; i++) { |
181 |
{ |
if (data[i] < 0) { |
182 |
uint32_t level = -data[i]; |
uint32_t level = -data[i]; |
183 |
level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i]; |
|
184 |
|
level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i]; |
185 |
level = (level * mult) >> 17; |
level = (level * mult) >> 17; |
186 |
sum += level; |
sum += level; |
187 |
coeff[i] = -(int16_t)level; |
coeff[i] = -(int16_t)level; |
188 |
} |
} else if (data[i] > 0) { |
|
else if (data[i] > 0) |
|
|
{ |
|
189 |
uint32_t level = data[i]; |
uint32_t level = data[i]; |
190 |
level = ((level<<4) + (default_inter_matrix[i]>>1)) / default_inter_matrix[i]; |
|
191 |
|
level = ((level << 4) + (inter_matrix[i] >> 1)) / inter_matrix[i]; |
192 |
level = (level * mult) >> 17; |
level = (level * mult) >> 17; |
193 |
sum += level; |
sum += level; |
194 |
coeff[i] = level; |
coeff[i] = level; |
195 |
} |
} else { |
|
else |
|
|
{ |
|
196 |
coeff[i] = 0; |
coeff[i] = 0; |
197 |
} |
} |
198 |
} |
} |
205 |
data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16 |
data = ((2 * coeff + SIGN(coeff)) * inter_matrix[i] * quant) / 16 |
206 |
*/ |
*/ |
207 |
|
|
208 |
void dequant4_inter_c(int16_t *data, const int16_t *coeff, const uint32_t quant) |
void |
209 |
|
dequant4_inter_c(int16_t * data, |
210 |
|
const int16_t * coeff, |
211 |
|
const uint32_t quant) |
212 |
{ |
{ |
213 |
uint32_t sum = 0; |
uint32_t sum = 0; |
214 |
uint32_t i; |
uint32_t i; |
215 |
|
int16_t *inter_matrix; |
216 |
|
|
217 |
for (i = 0; i < 64; i++) |
inter_matrix = get_inter_matrix(); |
218 |
{ |
|
219 |
if (coeff[i] == 0) |
for (i = 0; i < 64; i++) { |
220 |
{ |
if (coeff[i] == 0) { |
221 |
data[i] = 0; |
data[i] = 0; |
222 |
} |
} else if (coeff[i] < 0) { |
|
else if (coeff[i] < 0) |
|
|
{ |
|
223 |
int32_t level = -coeff[i]; |
int32_t level = -coeff[i]; |
224 |
level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4; |
|
225 |
|
level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; |
226 |
data[i] = (level <= 2048 ? -level : -2048); |
data[i] = (level <= 2048 ? -level : -2048); |
227 |
} |
} else // if (coeff[i] > 0) |
|
else // if (coeff[i] > 0) |
|
228 |
{ |
{ |
229 |
uint32_t level = coeff[i]; |
uint32_t level = coeff[i]; |
230 |
level = ((2 * level + 1) * default_inter_matrix[i] * quant) >> 4; |
|
231 |
|
level = ((2 * level + 1) * inter_matrix[i] * quant) >> 4; |
232 |
data[i] = (level <= 2047 ? level : 2047); |
data[i] = (level <= 2047 ? level : 2047); |
233 |
} |
} |
234 |
|
|
237 |
|
|
238 |
// mismatch control |
// mismatch control |
239 |
|
|
240 |
if ((sum & 1) == 0) |
if ((sum & 1) == 0) { |
|
{ |
|
241 |
data[63] ^= 1; |
data[63] ^= 1; |
242 |
} |
} |
243 |
} |
} |