2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #include "vpx_mem/vpx_mem.h"
16 #include "vp8/common/entropy.h"
20 #ifdef EXACT_FASTQUANT
21 void vp8_fast_quantize_b_c(BLOCK
*b
, BLOCKD
*d
)
26 short *coeff_ptr
= b
->coeff
;
27 short *zbin_ptr
= b
->zbin
;
28 short *round_ptr
= b
->round
;
29 short *quant_ptr
= b
->quant_fast
;
30 unsigned char *quant_shift_ptr
= b
->quant_shift
;
31 short *qcoeff_ptr
= d
->qcoeff
;
32 short *dqcoeff_ptr
= d
->dqcoeff
;
33 short *dequant_ptr
= d
->dequant
;
35 vpx_memset(qcoeff_ptr
, 0, 32);
36 vpx_memset(dqcoeff_ptr
, 0, 32);
40 for (i
= 0; i
< 16; i
++)
42 rc
= vp8_default_zig_zag1d
[i
];
46 sz
= (z
>> 31); // sign of z
47 x
= (z
^ sz
) - sz
; // x = abs(z)
52 y
= (((x
* quant_ptr
[rc
]) >> 16) + x
)
53 >> quant_shift_ptr
[rc
]; // quantize (x)
54 x
= (y
^ sz
) - sz
; // get the sign back
55 qcoeff_ptr
[rc
] = x
; // write to destination
56 dqcoeff_ptr
[rc
] = x
* dequant_ptr
[rc
]; // dequantized value
60 eob
= i
; // last nonzero coeffs
69 void vp8_fast_quantize_b_c(BLOCK
*b
, BLOCKD
*d
)
73 short *coeff_ptr
= b
->coeff
;
74 short *round_ptr
= b
->round
;
75 short *quant_ptr
= b
->quant_fast
;
76 short *qcoeff_ptr
= d
->qcoeff
;
77 short *dqcoeff_ptr
= d
->dqcoeff
;
78 short *dequant_ptr
= d
->dequant
;
81 for (i
= 0; i
< 16; i
++)
83 rc
= vp8_default_zig_zag1d
[i
];
86 sz
= (z
>> 31); // sign of z
87 x
= (z
^ sz
) - sz
; // x = abs(z)
89 y
= ((x
+ round_ptr
[rc
]) * quant_ptr
[rc
]) >> 16; // quantize (x)
90 x
= (y
^ sz
) - sz
; // get the sign back
91 qcoeff_ptr
[rc
] = x
; // write to destination
92 dqcoeff_ptr
[rc
] = x
* dequant_ptr
[rc
]; // dequantized value
96 eob
= i
; // last nonzero coeffs
105 void vp8_regular_quantize_b(BLOCK
*b
, BLOCKD
*d
)
110 short *zbin_boost_ptr
= b
->zrun_zbin_boost
;
111 short *coeff_ptr
= b
->coeff
;
112 short *zbin_ptr
= b
->zbin
;
113 short *round_ptr
= b
->round
;
114 short *quant_ptr
= b
->quant
;
115 unsigned char *quant_shift_ptr
= b
->quant_shift
;
116 short *qcoeff_ptr
= d
->qcoeff
;
117 short *dqcoeff_ptr
= d
->dqcoeff
;
118 short *dequant_ptr
= d
->dequant
;
119 short zbin_oq_value
= b
->zbin_extra
;
121 vpx_memset(qcoeff_ptr
, 0, 32);
122 vpx_memset(dqcoeff_ptr
, 0, 32);
126 for (i
= 0; i
< 16; i
++)
128 rc
= vp8_default_zig_zag1d
[i
];
131 zbin
= zbin_ptr
[rc
] + *zbin_boost_ptr
+ zbin_oq_value
;
134 sz
= (z
>> 31); // sign of z
135 x
= (z
^ sz
) - sz
; // x = abs(z)
140 y
= (((x
* quant_ptr
[rc
]) >> 16) + x
)
141 >> quant_shift_ptr
[rc
]; // quantize (x)
142 x
= (y
^ sz
) - sz
; // get the sign back
143 qcoeff_ptr
[rc
] = x
; // write to destination
144 dqcoeff_ptr
[rc
] = x
* dequant_ptr
[rc
]; // dequantized value
148 eob
= i
; // last nonzero coeffs
149 zbin_boost_ptr
= b
->zrun_zbin_boost
; // reset zero runlength
157 /* Perform regular quantization, with unbiased rounding and no zero bin. */
158 void vp8_strict_quantize_b(BLOCK
*b
, BLOCKD
*d
)
169 unsigned char *quant_shift_ptr
;
174 coeff_ptr
= b
->coeff
;
175 quant_ptr
= b
->quant
;
176 quant_shift_ptr
= b
->quant_shift
;
177 qcoeff_ptr
= d
->qcoeff
;
178 dqcoeff_ptr
= d
->dqcoeff
;
179 dequant_ptr
= d
->dequant
;
181 vpx_memset(qcoeff_ptr
, 0, 32);
182 vpx_memset(dqcoeff_ptr
, 0, 32);
183 for (i
= 0; i
< 16; i
++)
188 /*TODO: These arrays should be stored in zig-zag order.*/
189 rc
= vp8_default_zig_zag1d
[i
];
191 dq
= dequant_ptr
[rc
];
200 y
= (((x
* quant_ptr
[rc
]) >> 16) + x
) >> quant_shift_ptr
[rc
];
201 /* Put the sign back. */
203 /* Save the coefficient and its dequantized value. */
205 dqcoeff_ptr
[rc
] = x
* dq
;
206 /* Remember the last non-zero coefficient. */
217 void vp8_regular_quantize_b(BLOCK
*b
, BLOCKD
*d
)
222 short *zbin_boost_ptr
= b
->zrun_zbin_boost
;
223 short *coeff_ptr
= b
->coeff
;
224 short *zbin_ptr
= b
->zbin
;
225 short *round_ptr
= b
->round
;
226 short *quant_ptr
= b
->quant
;
227 short *qcoeff_ptr
= d
->qcoeff
;
228 short *dqcoeff_ptr
= d
->dqcoeff
;
229 short *dequant_ptr
= d
->dequant
;
230 short zbin_oq_value
= b
->zbin_extra
;
232 vpx_memset(qcoeff_ptr
, 0, 32);
233 vpx_memset(dqcoeff_ptr
, 0, 32);
237 for (i
= 0; i
< 16; i
++)
239 rc
= vp8_default_zig_zag1d
[i
];
243 // zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
245 zbin
= zbin_ptr
[rc
] + *zbin_boost_ptr
+ zbin_oq_value
;
248 sz
= (z
>> 31); // sign of z
249 x
= (z
^ sz
) - sz
; // x = abs(z)
253 y
= ((x
+ round_ptr
[rc
]) * quant_ptr
[rc
]) >> 16; // quantize (x)
254 x
= (y
^ sz
) - sz
; // get the sign back
255 qcoeff_ptr
[rc
] = x
; // write to destination
256 dqcoeff_ptr
[rc
] = x
* dequant_ptr
[rc
]; // dequantized value
260 eob
= i
; // last nonzero coeffs
261 zbin_boost_ptr
= &b
->zrun_zbin_boost
[0]; // reset zero runlength
271 void vp8_quantize_mby(MACROBLOCK
*x
)
274 int has_2nd_order
= (x
->e_mbd
.mode_info_context
->mbmi
.mode
!= B_PRED
275 && x
->e_mbd
.mode_info_context
->mbmi
.mode
!= SPLITMV
);
277 for (i
= 0; i
< 16; i
++)
278 x
->quantize_b(&x
->block
[i
], &x
->e_mbd
.block
[i
]);
281 x
->quantize_b(&x
->block
[24], &x
->e_mbd
.block
[24]);
284 void vp8_quantize_mb(MACROBLOCK
*x
)
287 int has_2nd_order
=(x
->e_mbd
.mode_info_context
->mbmi
.mode
!= B_PRED
288 && x
->e_mbd
.mode_info_context
->mbmi
.mode
!= SPLITMV
);
290 for (i
= 0; i
< 24+has_2nd_order
; i
++)
291 x
->quantize_b(&x
->block
[i
], &x
->e_mbd
.block
[i
]);
295 void vp8_quantize_mbuv(MACROBLOCK
*x
)
299 for (i
= 16; i
< 24; i
++)
300 x
->quantize_b(&x
->block
[i
], &x
->e_mbd
.block
[i
]);