Fix relative include paths
[libvpx.git] / vp8 / encoder / quantize.c
blob02b9d7bd97f404b222ea3901db6d173f1008a787
1 /*
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.
9 */
12 #include <math.h>
13 #include "vpx_mem/vpx_mem.h"
15 #include "quantize.h"
16 #include "vp8/common/entropy.h"
17 #include "vp8/common/predictdc.h"
19 #define EXACT_QUANT
21 #ifdef EXACT_FASTQUANT
22 void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
24 int i, rc, eob;
25 int zbin;
26 int x, y, z, sz;
27 short *coeff_ptr = b->coeff;
28 short *zbin_ptr = b->zbin;
29 short *round_ptr = b->round;
30 short *quant_ptr = b->quant_fast;
31 short *quant_shift_ptr = b->quant_shift;
32 short *qcoeff_ptr = d->qcoeff;
33 short *dqcoeff_ptr = d->dqcoeff;
34 short *dequant_ptr = d->dequant;
36 vpx_memset(qcoeff_ptr, 0, 32);
37 vpx_memset(dqcoeff_ptr, 0, 32);
39 eob = -1;
41 for (i = 0; i < 16; i++)
43 rc = vp8_default_zig_zag1d[i];
44 z = coeff_ptr[rc];
45 zbin = zbin_ptr[rc] ;
47 sz = (z >> 31); // sign of z
48 x = (z ^ sz) - sz; // x = abs(z)
50 if (x >= zbin)
52 x += round_ptr[rc];
53 y = (((x * quant_ptr[rc]) >> 16) + x)
54 >> quant_shift_ptr[rc]; // quantize (x)
55 x = (y ^ sz) - sz; // get the sign back
56 qcoeff_ptr[rc] = x; // write to destination
57 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
59 if (y)
61 eob = i; // last nonzero coeffs
65 d->eob = eob + 1;
68 #else
70 void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
72 int i, rc, eob;
73 int x, y, z, sz;
74 short *coeff_ptr = b->coeff;
75 short *round_ptr = b->round;
76 short *quant_ptr = b->quant_fast;
77 short *qcoeff_ptr = d->qcoeff;
78 short *dqcoeff_ptr = d->dqcoeff;
79 short *dequant_ptr = d->dequant;
81 eob = -1;
82 for (i = 0; i < 16; i++)
84 rc = vp8_default_zig_zag1d[i];
85 z = coeff_ptr[rc];
87 sz = (z >> 31); // sign of z
88 x = (z ^ sz) - sz; // x = abs(z)
90 y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
91 x = (y ^ sz) - sz; // get the sign back
92 qcoeff_ptr[rc] = x; // write to destination
93 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
95 if (y)
97 eob = i; // last nonzero coeffs
100 d->eob = eob + 1;
103 #endif
105 #ifdef EXACT_QUANT
106 void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
108 int i, rc, eob;
109 int zbin;
110 int x, y, z, sz;
111 short *zbin_boost_ptr = b->zrun_zbin_boost;
112 short *coeff_ptr = b->coeff;
113 short *zbin_ptr = b->zbin;
114 short *round_ptr = b->round;
115 short *quant_ptr = b->quant;
116 short *quant_shift_ptr = b->quant_shift;
117 short *qcoeff_ptr = d->qcoeff;
118 short *dqcoeff_ptr = d->dqcoeff;
119 short *dequant_ptr = d->dequant;
120 short zbin_oq_value = b->zbin_extra;
122 vpx_memset(qcoeff_ptr, 0, 32);
123 vpx_memset(dqcoeff_ptr, 0, 32);
125 eob = -1;
127 for (i = 0; i < 16; i++)
129 rc = vp8_default_zig_zag1d[i];
130 z = coeff_ptr[rc];
132 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
134 zbin_boost_ptr ++;
135 sz = (z >> 31); // sign of z
136 x = (z ^ sz) - sz; // x = abs(z)
138 if (x >= zbin)
140 x += round_ptr[rc];
141 y = (((x * quant_ptr[rc]) >> 16) + x)
142 >> quant_shift_ptr[rc]; // quantize (x)
143 x = (y ^ sz) - sz; // get the sign back
144 qcoeff_ptr[rc] = x; // write to destination
145 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
147 if (y)
149 eob = i; // last nonzero coeffs
150 zbin_boost_ptr = b->zrun_zbin_boost; // reset zero runlength
155 d->eob = eob + 1;
158 /* Perform regular quantization, with unbiased rounding and no zero bin. */
159 void vp8_strict_quantize_b(BLOCK *b, BLOCKD *d)
161 int i;
162 int rc;
163 int eob;
164 int x;
165 int y;
166 int z;
167 int sz;
168 short *coeff_ptr;
169 short *quant_ptr;
170 short *quant_shift_ptr;
171 short *qcoeff_ptr;
172 short *dqcoeff_ptr;
173 short *dequant_ptr;
175 coeff_ptr = b->coeff;
176 quant_ptr = b->quant;
177 quant_shift_ptr = b->quant_shift;
178 qcoeff_ptr = d->qcoeff;
179 dqcoeff_ptr = d->dqcoeff;
180 dequant_ptr = d->dequant;
181 eob = - 1;
182 vpx_memset(qcoeff_ptr, 0, 32);
183 vpx_memset(dqcoeff_ptr, 0, 32);
184 for (i = 0; i < 16; i++)
186 int dq;
187 int round;
189 /*TODO: These arrays should be stored in zig-zag order.*/
190 rc = vp8_default_zig_zag1d[i];
191 z = coeff_ptr[rc];
192 dq = dequant_ptr[rc];
193 round = dq >> 1;
194 /* Sign of z. */
195 sz = -(z < 0);
196 x = (z + sz) ^ sz;
197 x += round;
198 if (x >= dq)
200 /* Quantize x. */
201 y = (((x * quant_ptr[rc]) >> 16) + x) >> quant_shift_ptr[rc];
202 /* Put the sign back. */
203 x = (y + sz) ^ sz;
204 /* Save the coefficient and its dequantized value. */
205 qcoeff_ptr[rc] = x;
206 dqcoeff_ptr[rc] = x * dq;
207 /* Remember the last non-zero coefficient. */
208 if (y)
209 eob = i;
213 d->eob = eob + 1;
216 #else
218 void vp8_regular_quantize_b(BLOCK *b, BLOCKD *d)
220 int i, rc, eob;
221 int zbin;
222 int x, y, z, sz;
223 short *zbin_boost_ptr = b->zrun_zbin_boost;
224 short *coeff_ptr = b->coeff;
225 short *zbin_ptr = b->zbin;
226 short *round_ptr = b->round;
227 short *quant_ptr = b->quant;
228 short *qcoeff_ptr = d->qcoeff;
229 short *dqcoeff_ptr = d->dqcoeff;
230 short *dequant_ptr = d->dequant;
231 short zbin_oq_value = b->zbin_extra;
233 vpx_memset(qcoeff_ptr, 0, 32);
234 vpx_memset(dqcoeff_ptr, 0, 32);
236 eob = -1;
238 for (i = 0; i < 16; i++)
240 rc = vp8_default_zig_zag1d[i];
241 z = coeff_ptr[rc];
243 //if ( i == 0 )
244 // zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value/2;
245 //else
246 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
248 zbin_boost_ptr ++;
249 sz = (z >> 31); // sign of z
250 x = (z ^ sz) - sz; // x = abs(z)
252 if (x >= zbin)
254 y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; // quantize (x)
255 x = (y ^ sz) - sz; // get the sign back
256 qcoeff_ptr[rc] = x; // write to destination
257 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; // dequantized value
259 if (y)
261 eob = i; // last nonzero coeffs
262 zbin_boost_ptr = &b->zrun_zbin_boost[0]; // reset zero runlength
267 d->eob = eob + 1;
270 #endif
272 void vp8_quantize_mby(MACROBLOCK *x)
274 int i;
275 int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
276 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
278 for (i = 0; i < 16; i++)
279 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
281 if(has_2nd_order)
282 x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
285 void vp8_quantize_mb(MACROBLOCK *x)
287 int i;
288 int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
289 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
291 for (i = 0; i < 24+has_2nd_order; i++)
292 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
296 void vp8_quantize_mbuv(MACROBLOCK *x)
298 int i;
300 for (i = 16; i < 24; i++)
301 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);