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"
17 #include "vp8/common/quant_common.h"
19 void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
23 short *coeff_ptr = b->coeff;
24 short *round_ptr = b->round;
25 short *quant_ptr = b->quant_fast;
26 short *qcoeff_ptr = d->qcoeff;
27 short *dqcoeff_ptr = d->dqcoeff;
28 short *dequant_ptr = d->dequant;
31 for (i = 0; i < 16; i++)
33 rc = vp8_default_zig_zag1d[i];
36 sz = (z >> 31); /* sign of z */
37 x = (z ^ sz) - sz; /* x = abs(z) */
39 y = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */
40 x = (y ^ sz) - sz; /* get the sign back */
41 qcoeff_ptr[rc] = x; /* write to destination */
42 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; /* dequantized value */
46 eob = i; /* last nonzero coeffs */
49 *d->eob = (char)(eob + 1);
52 void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
57 short *zbin_boost_ptr = b->zrun_zbin_boost;
58 short *coeff_ptr = b->coeff;
59 short *zbin_ptr = b->zbin;
60 short *round_ptr = b->round;
61 short *quant_ptr = b->quant;
62 short *quant_shift_ptr = b->quant_shift;
63 short *qcoeff_ptr = d->qcoeff;
64 short *dqcoeff_ptr = d->dqcoeff;
65 short *dequant_ptr = d->dequant;
66 short zbin_oq_value = b->zbin_extra;
68 vpx_memset(qcoeff_ptr, 0, 32);
69 vpx_memset(dqcoeff_ptr, 0, 32);
73 for (i = 0; i < 16; i++)
75 rc = vp8_default_zig_zag1d[i];
78 zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
81 sz = (z >> 31); /* sign of z */
82 x = (z ^ sz) - sz; /* x = abs(z) */
87 y = ((((x * quant_ptr[rc]) >> 16) + x)
88 * quant_shift_ptr[rc]) >> 16; /* quantize (x) */
89 x = (y ^ sz) - sz; /* get the sign back */
90 qcoeff_ptr[rc] = x; /* write to destination */
91 dqcoeff_ptr[rc] = x * dequant_ptr[rc]; /* dequantized value */
95 eob = i; /* last nonzero coeffs */
96 zbin_boost_ptr = b->zrun_zbin_boost; /* reset zero runlength */
101 *d->eob = (char)(eob + 1);
104 void vp8_quantize_mby_c(MACROBLOCK *x)
107 int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
108 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
110 for (i = 0; i < 16; i++)
111 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
114 x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
117 void vp8_quantize_mb_c(MACROBLOCK *x)
120 int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
121 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
123 for (i = 0; i < 24+has_2nd_order; i++)
124 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
128 void vp8_quantize_mbuv_c(MACROBLOCK *x)
132 for (i = 16; i < 24; i++)
133 x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
136 /* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
137 * these two C functions if corresponding optimized routine is not available.
138 * NEON optimized version implements currently the fast quantization for pair
140 void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
142 vp8_regular_quantize_b(b1, d1);
143 vp8_regular_quantize_b(b2, d2);
146 void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
148 vp8_fast_quantize_b_c(b1, d1);
149 vp8_fast_quantize_b_c(b2, d2);
153 static const int qrounding_factors[129] =
155 48, 48, 48, 48, 48, 48, 48, 48,
156 48, 48, 48, 48, 48, 48, 48, 48,
157 48, 48, 48, 48, 48, 48, 48, 48,
158 48, 48, 48, 48, 48, 48, 48, 48,
159 48, 48, 48, 48, 48, 48, 48, 48,
160 48, 48, 48, 48, 48, 48, 48, 48,
161 48, 48, 48, 48, 48, 48, 48, 48,
162 48, 48, 48, 48, 48, 48, 48, 48,
163 48, 48, 48, 48, 48, 48, 48, 48,
164 48, 48, 48, 48, 48, 48, 48, 48,
165 48, 48, 48, 48, 48, 48, 48, 48,
166 48, 48, 48, 48, 48, 48, 48, 48,
167 48, 48, 48, 48, 48, 48, 48, 48,
168 48, 48, 48, 48, 48, 48, 48, 48,
169 48, 48, 48, 48, 48, 48, 48, 48,
170 48, 48, 48, 48, 48, 48, 48, 48,
175 static const int qzbin_factors[129] =
177 84, 84, 84, 84, 84, 84, 84, 84,
178 84, 84, 84, 84, 84, 84, 84, 84,
179 84, 84, 84, 84, 84, 84, 84, 84,
180 84, 84, 84, 84, 84, 84, 84, 84,
181 84, 84, 84, 84, 84, 84, 84, 84,
182 84, 84, 84, 84, 84, 84, 84, 84,
183 80, 80, 80, 80, 80, 80, 80, 80,
184 80, 80, 80, 80, 80, 80, 80, 80,
185 80, 80, 80, 80, 80, 80, 80, 80,
186 80, 80, 80, 80, 80, 80, 80, 80,
187 80, 80, 80, 80, 80, 80, 80, 80,
188 80, 80, 80, 80, 80, 80, 80, 80,
189 80, 80, 80, 80, 80, 80, 80, 80,
190 80, 80, 80, 80, 80, 80, 80, 80,
191 80, 80, 80, 80, 80, 80, 80, 80,
192 80, 80, 80, 80, 80, 80, 80, 80,
197 static const int qrounding_factors_y2[129] =
199 48, 48, 48, 48, 48, 48, 48, 48,
200 48, 48, 48, 48, 48, 48, 48, 48,
201 48, 48, 48, 48, 48, 48, 48, 48,
202 48, 48, 48, 48, 48, 48, 48, 48,
203 48, 48, 48, 48, 48, 48, 48, 48,
204 48, 48, 48, 48, 48, 48, 48, 48,
205 48, 48, 48, 48, 48, 48, 48, 48,
206 48, 48, 48, 48, 48, 48, 48, 48,
207 48, 48, 48, 48, 48, 48, 48, 48,
208 48, 48, 48, 48, 48, 48, 48, 48,
209 48, 48, 48, 48, 48, 48, 48, 48,
210 48, 48, 48, 48, 48, 48, 48, 48,
211 48, 48, 48, 48, 48, 48, 48, 48,
212 48, 48, 48, 48, 48, 48, 48, 48,
213 48, 48, 48, 48, 48, 48, 48, 48,
214 48, 48, 48, 48, 48, 48, 48, 48,
219 static const int qzbin_factors_y2[129] =
221 84, 84, 84, 84, 84, 84, 84, 84,
222 84, 84, 84, 84, 84, 84, 84, 84,
223 84, 84, 84, 84, 84, 84, 84, 84,
224 84, 84, 84, 84, 84, 84, 84, 84,
225 84, 84, 84, 84, 84, 84, 84, 84,
226 84, 84, 84, 84, 84, 84, 84, 84,
227 80, 80, 80, 80, 80, 80, 80, 80,
228 80, 80, 80, 80, 80, 80, 80, 80,
229 80, 80, 80, 80, 80, 80, 80, 80,
230 80, 80, 80, 80, 80, 80, 80, 80,
231 80, 80, 80, 80, 80, 80, 80, 80,
232 80, 80, 80, 80, 80, 80, 80, 80,
233 80, 80, 80, 80, 80, 80, 80, 80,
234 80, 80, 80, 80, 80, 80, 80, 80,
235 80, 80, 80, 80, 80, 80, 80, 80,
236 80, 80, 80, 80, 80, 80, 80, 80,
241 static void invert_quant(int improved_quant, short *quant,
242 short *shift, short d)
249 for(l = 0; t > 1; l++)
251 t = 1 + (1<<(16+l))/d;
252 *quant = (short)(t - (1<<16));
254 /* use multiplication and constant shift by 16 */
255 *shift = 1 << (16 - *shift);
259 *quant = (1 << 16) / d;
261 /* use multiplication and constant shift by 16 */
262 *shift = 1 << (16 - *shift);
267 void vp8cx_init_quantizer(VP8_COMP *cpi)
273 int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44,
276 for (Q = 0; Q < QINDEX_RANGE; Q++)
279 quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
280 cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
281 invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
282 cpi->Y1quant_shift[Q] + 0, quant_val);
283 cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
284 cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
285 cpi->common.Y1dequant[Q][0] = quant_val;
286 cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
288 quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
289 cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
290 invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
291 cpi->Y2quant_shift[Q] + 0, quant_val);
292 cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
293 cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
294 cpi->common.Y2dequant[Q][0] = quant_val;
295 cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
297 quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
298 cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
299 invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
300 cpi->UVquant_shift[Q] + 0, quant_val);
301 cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
302 cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
303 cpi->common.UVdequant[Q][0] = quant_val;
304 cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
306 /* all the ac values = ; */
307 quant_val = vp8_ac_yquant(Q);
308 cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val;
309 invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1,
310 cpi->Y1quant_shift[Q] + 1, quant_val);
311 cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
312 cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
313 cpi->common.Y1dequant[Q][1] = quant_val;
314 cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7;
316 quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
317 cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val;
318 invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1,
319 cpi->Y2quant_shift[Q] + 1, quant_val);
320 cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
321 cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7;
322 cpi->common.Y2dequant[Q][1] = quant_val;
323 cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7;
325 quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
326 cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val;
327 invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1,
328 cpi->UVquant_shift[Q] + 1, quant_val);
329 cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
330 cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
331 cpi->common.UVdequant[Q][1] = quant_val;
332 cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7;
334 for (i = 2; i < 16; i++)
336 cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1];
337 cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1];
338 cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1];
339 cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1];
340 cpi->Y1round[Q][i] = cpi->Y1round[Q][1];
341 cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] *
344 cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1];
345 cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1];
346 cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1];
347 cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1];
348 cpi->Y2round[Q][i] = cpi->Y2round[Q][1];
349 cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] *
352 cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1];
353 cpi->UVquant[Q][i] = cpi->UVquant[Q][1];
354 cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1];
355 cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1];
356 cpi->UVround[Q][i] = cpi->UVround[Q][1];
357 cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] *
363 #define ZBIN_EXTRA_Y \
364 (( cpi->common.Y1dequant[QIndex][1] * \
365 ( x->zbin_over_quant + \
366 x->zbin_mode_boost + \
367 x->act_zbin_adj ) ) >> 7)
369 #define ZBIN_EXTRA_UV \
370 (( cpi->common.UVdequant[QIndex][1] * \
371 ( x->zbin_over_quant + \
372 x->zbin_mode_boost + \
373 x->act_zbin_adj ) ) >> 7)
375 #define ZBIN_EXTRA_Y2 \
376 (( cpi->common.Y2dequant[QIndex][1] * \
377 ( (x->zbin_over_quant / 2) + \
378 x->zbin_mode_boost + \
379 x->act_zbin_adj ) ) >> 7)
381 void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip)
385 MACROBLOCKD *xd = &x->e_mbd;
388 /* Select the baseline MB Q index. */
389 if (xd->segmentation_enabled)
392 if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
393 QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
397 QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
398 /* Clamp to valid range */
399 QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
403 QIndex = cpi->common.base_qindex;
405 /* This initialization should be called at least once. Use ok_to_skip to
406 * decide if it is ok to skip.
407 * Before encoding a frame, this function is always called with ok_to_skip
408 * =0, which means no skiping of calculations. The "last" values are
409 * initialized at that time.
411 if (!ok_to_skip || QIndex != x->q_index)
414 xd->dequant_y1_dc[0] = 1;
415 xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0];
416 xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0];
417 xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0];
419 for (i = 1; i < 16; i++)
421 xd->dequant_y1_dc[i] =
422 xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1];
423 xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1];
424 xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1];
427 /*TODO: Remove dequant from BLOCKD. This is a temporary solution until
428 * the quantizer code uses a passed in pointer to the dequant constants.
429 * This will also require modifications to the x86 and neon assembly.
431 for (i = 0; i < 16; i++)
432 x->e_mbd.block[i].dequant = xd->dequant_y1;
433 for (i = 16; i < 24; i++)
434 x->e_mbd.block[i].dequant = xd->dequant_uv;
435 x->e_mbd.block[24].dequant = xd->dequant_y2;
439 zbin_extra = ZBIN_EXTRA_Y;
441 for (i = 0; i < 16; i++)
443 x->block[i].quant = cpi->Y1quant[QIndex];
444 x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
445 x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
446 x->block[i].zbin = cpi->Y1zbin[QIndex];
447 x->block[i].round = cpi->Y1round[QIndex];
448 x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
449 x->block[i].zbin_extra = (short)zbin_extra;
453 zbin_extra = ZBIN_EXTRA_UV;
455 for (i = 16; i < 24; i++)
457 x->block[i].quant = cpi->UVquant[QIndex];
458 x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
459 x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
460 x->block[i].zbin = cpi->UVzbin[QIndex];
461 x->block[i].round = cpi->UVround[QIndex];
462 x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
463 x->block[i].zbin_extra = (short)zbin_extra;
467 zbin_extra = ZBIN_EXTRA_Y2;
469 x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
470 x->block[24].quant = cpi->Y2quant[QIndex];
471 x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
472 x->block[24].zbin = cpi->Y2zbin[QIndex];
473 x->block[24].round = cpi->Y2round[QIndex];
474 x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
475 x->block[24].zbin_extra = (short)zbin_extra;
477 /* save this macroblock QIndex for vp8_update_zbin_extra() */
480 x->last_zbin_over_quant = x->zbin_over_quant;
481 x->last_zbin_mode_boost = x->zbin_mode_boost;
482 x->last_act_zbin_adj = x->act_zbin_adj;
487 else if(x->last_zbin_over_quant != x->zbin_over_quant
488 || x->last_zbin_mode_boost != x->zbin_mode_boost
489 || x->last_act_zbin_adj != x->act_zbin_adj)
492 zbin_extra = ZBIN_EXTRA_Y;
494 for (i = 0; i < 16; i++)
495 x->block[i].zbin_extra = (short)zbin_extra;
498 zbin_extra = ZBIN_EXTRA_UV;
500 for (i = 16; i < 24; i++)
501 x->block[i].zbin_extra = (short)zbin_extra;
504 zbin_extra = ZBIN_EXTRA_Y2;
505 x->block[24].zbin_extra = (short)zbin_extra;
507 x->last_zbin_over_quant = x->zbin_over_quant;
508 x->last_zbin_mode_boost = x->zbin_mode_boost;
509 x->last_act_zbin_adj = x->act_zbin_adj;
513 void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
516 int QIndex = x->q_index;
520 zbin_extra = ZBIN_EXTRA_Y;
522 for (i = 0; i < 16; i++)
523 x->block[i].zbin_extra = (short)zbin_extra;
526 zbin_extra = ZBIN_EXTRA_UV;
528 for (i = 16; i < 24; i++)
529 x->block[i].zbin_extra = (short)zbin_extra;
532 zbin_extra = ZBIN_EXTRA_Y2;
533 x->block[24].zbin_extra = (short)zbin_extra;
539 void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
541 /* Clear Zbin mode boost for default case */
542 cpi->mb.zbin_mode_boost = 0;
544 /* MB level quantizer setup */
545 vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0);
549 void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
551 VP8_COMMON *cm = &cpi->common;
552 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
557 /* if any of the delta_q values are changing update flag has to be set */
558 /* currently only y2dc_delta_q may change */
560 cm->y1dc_delta_q = 0;
561 cm->y2ac_delta_q = 0;
562 cm->uvdc_delta_q = 0;
563 cm->uvac_delta_q = 0;
572 update |= cm->y2dc_delta_q != new_delta_q;
573 cm->y2dc_delta_q = new_delta_q;
576 /* Set Segment specific quatizers */
577 mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
578 mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
579 mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
580 mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
582 /* quantizer has to be reinitialized for any delta_q changes */
584 vp8cx_init_quantizer(cpi);