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.
12 #include "vpx_ports/config.h"
14 #include "vp8/common/reconinter.h"
17 #include "vp8/common/invtrans.h"
18 #include "vp8/common/recon.h"
19 #include "vp8/common/reconintra.h"
21 #include "vpx_mem/vpx_mem.h"
24 #if CONFIG_RUNTIME_CPU_DETECT
25 #define IF_RTCD(x) (x)
27 #define IF_RTCD(x) NULL
29 void vp8_subtract_b_c(BLOCK *be, BLOCKD *bd, int pitch)
31 unsigned char *src_ptr = (*(be->base_src) + be->src);
32 short *diff_ptr = be->src_diff;
33 unsigned char *pred_ptr = bd->predictor;
34 int src_stride = be->src_stride;
38 for (r = 0; r < 4; r++)
40 for (c = 0; c < 4; c++)
42 diff_ptr[c] = src_ptr[c] - pred_ptr[c];
47 src_ptr += src_stride;
51 void vp8_subtract_mbuv_c(short *diff, unsigned char *usrc, unsigned char *vsrc, unsigned char *pred, int stride)
53 short *udiff = diff + 256;
54 short *vdiff = diff + 320;
55 unsigned char *upred = pred + 256;
56 unsigned char *vpred = pred + 320;
60 for (r = 0; r < 8; r++)
62 for (c = 0; c < 8; c++)
64 udiff[c] = usrc[c] - upred[c];
72 for (r = 0; r < 8; r++)
74 for (c = 0; c < 8; c++)
76 vdiff[c] = vsrc[c] - vpred[c];
85 void vp8_subtract_mby_c(short *diff, unsigned char *src, unsigned char *pred, int stride)
89 for (r = 0; r < 16; r++)
91 for (c = 0; c < 16; c++)
93 diff[c] = src[c] - pred[c];
102 static void vp8_subtract_mb(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
104 ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, x->src.y_buffer, x->e_mbd.predictor, x->src.y_stride);
105 ENCODEMB_INVOKE(&rtcd->encodemb, submbuv)(x->src_diff, x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);
108 static void build_dcblock(MACROBLOCK *x)
110 short *src_diff_ptr = &x->src_diff[384];
113 for (i = 0; i < 16; i++)
115 src_diff_ptr[i] = x->coeff[i * 16];
119 void vp8_transform_mbuv(MACROBLOCK *x)
123 for (i = 16; i < 24; i += 2)
125 x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
126 &x->block[i].coeff[0], 16);
131 void vp8_transform_intra_mby(MACROBLOCK *x)
135 for (i = 0; i < 16; i += 2)
137 x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
138 &x->block[i].coeff[0], 32);
141 // build dc block from 16 y dc values
144 // do 2nd order transform on the dc block
145 x->short_walsh4x4(&x->block[24].src_diff[0],
146 &x->block[24].coeff[0], 8);
151 static void transform_mb(MACROBLOCK *x)
155 for (i = 0; i < 16; i += 2)
157 x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
158 &x->block[i].coeff[0], 32);
161 // build dc block from 16 y dc values
162 if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
165 for (i = 16; i < 24; i += 2)
167 x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
168 &x->block[i].coeff[0], 16);
171 // do 2nd order transform on the dc block
172 if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
173 x->short_walsh4x4(&x->block[24].src_diff[0],
174 &x->block[24].coeff[0], 8);
179 static void transform_mby(MACROBLOCK *x)
183 for (i = 0; i < 16; i += 2)
185 x->vp8_short_fdct8x4(&x->block[i].src_diff[0],
186 &x->block[i].coeff[0], 32);
189 // build dc block from 16 y dc values
190 if (x->e_mbd.mode_info_context->mbmi.mode != SPLITMV)
193 x->short_walsh4x4(&x->block[24].src_diff[0],
194 &x->block[24].coeff[0], 8);
200 #define RDTRUNC(RM,DM,R,D) ( (128+(R)*(RM)) & 0xFF )
202 typedef struct vp8_token_state vp8_token_state;
204 struct vp8_token_state{
212 // TODO: experiments to find optimal multiple numbers
215 #define Y2_RD_MULT 16
217 static const int plane_rd_mult[4]=
225 static void optimize_b(MACROBLOCK *mb, int ib, int type,
226 ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
227 const VP8_ENCODER_RTCD *rtcd)
231 vp8_token_state tokens[17][2];
232 unsigned best_mask[2];
233 const short *dequant_ptr;
234 const short *coeff_ptr;
258 int err_mult = plane_rd_mult[type];
261 d = &mb->e_mbd.block[ib];
263 /* Enable this to test the effect of RDO as a replacement for the dynamic
264 * zero bin instead of an augmentation of it.
267 vp8_strict_quantize_b(b, d);
270 dequant_ptr = d->dequant;
271 coeff_ptr = b->coeff;
272 qcoeff_ptr = d->qcoeff;
273 dqcoeff_ptr = d->dqcoeff;
277 /* Now set up a Viterbi trellis to evaluate alternative roundings. */
278 rdmult = mb->rdmult * err_mult;
279 if(mb->e_mbd.mode_info_context->mbmi.ref_frame==INTRA_FRAME)
280 rdmult = (rdmult * 9)>>4;
283 best_mask[0] = best_mask[1] = 0;
284 /* Initialize the sentinel node of the trellis. */
285 tokens[eob][0].rate = 0;
286 tokens[eob][0].error = 0;
287 tokens[eob][0].next = 16;
288 tokens[eob][0].token = DCT_EOB_TOKEN;
289 tokens[eob][0].qc = 0;
290 *(tokens[eob] + 1) = *(tokens[eob] + 0);
292 for (i = eob; i-- > i0;)
298 rc = vp8_default_zig_zag1d[i];
300 /* Only add a trellis state for non-zero coefficients. */
304 error0 = tokens[next][0].error;
305 error1 = tokens[next][1].error;
306 /* Evaluate the first possibility for this state. */
307 rate0 = tokens[next][0].rate;
308 rate1 = tokens[next][1].rate;
309 t0 = (vp8_dct_value_tokens_ptr + x)->Token;
310 /* Consider both possible successor states. */
313 band = vp8_coef_bands[i + 1];
314 pt = vp8_prev_token_class[t0];
316 mb->token_costs[type][band][pt][tokens[next][0].token];
318 mb->token_costs[type][band][pt][tokens[next][1].token];
320 rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);
321 rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);
322 if (rd_cost0 == rd_cost1)
324 rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);
325 rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);
327 /* And pick the best. */
328 best = rd_cost1 < rd_cost0;
329 base_bits = *(vp8_dct_value_cost_ptr + x);
330 dx = dqcoeff_ptr[rc] - coeff_ptr[rc];
332 tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
333 tokens[i][0].error = d2 + (best ? error1 : error0);
334 tokens[i][0].next = next;
335 tokens[i][0].token = t0;
337 best_mask[0] |= best << i;
338 /* Evaluate the second possibility for this state. */
339 rate0 = tokens[next][0].rate;
340 rate1 = tokens[next][1].rate;
342 if((abs(x)*dequant_ptr[rc]>abs(coeff_ptr[rc])) &&
343 (abs(x)*dequant_ptr[rc]<abs(coeff_ptr[rc])+dequant_ptr[rc]))
354 /* Consider both possible successor states. */
357 /* If we reduced this coefficient to zero, check to see if
358 * we need to move the EOB back here.
360 t0 = tokens[next][0].token == DCT_EOB_TOKEN ?
361 DCT_EOB_TOKEN : ZERO_TOKEN;
362 t1 = tokens[next][1].token == DCT_EOB_TOKEN ?
363 DCT_EOB_TOKEN : ZERO_TOKEN;
367 t0=t1 = (vp8_dct_value_tokens_ptr + x)->Token;
371 band = vp8_coef_bands[i + 1];
372 if(t0!=DCT_EOB_TOKEN)
374 pt = vp8_prev_token_class[t0];
375 rate0 += mb->token_costs[type][band][pt][
376 tokens[next][0].token];
378 if(t1!=DCT_EOB_TOKEN)
380 pt = vp8_prev_token_class[t1];
381 rate1 += mb->token_costs[type][band][pt][
382 tokens[next][1].token];
386 rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);
387 rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);
388 if (rd_cost0 == rd_cost1)
390 rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);
391 rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);
393 /* And pick the best. */
394 best = rd_cost1 < rd_cost0;
395 base_bits = *(vp8_dct_value_cost_ptr + x);
399 dx -= (dequant_ptr[rc] + sz) ^ sz;
402 tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
403 tokens[i][1].error = d2 + (best ? error1 : error0);
404 tokens[i][1].next = next;
405 tokens[i][1].token =best?t1:t0;
407 best_mask[1] |= best << i;
408 /* Finally, make this the new head of the trellis. */
411 /* There's no choice to make for a zero coefficient, so we don't
412 * add a new trellis node, but we do need to update the costs.
416 band = vp8_coef_bands[i + 1];
417 t0 = tokens[next][0].token;
418 t1 = tokens[next][1].token;
419 /* Update the cost of each path if we're past the EOB token. */
420 if (t0 != DCT_EOB_TOKEN)
422 tokens[next][0].rate += mb->token_costs[type][band][0][t0];
423 tokens[next][0].token = ZERO_TOKEN;
425 if (t1 != DCT_EOB_TOKEN)
427 tokens[next][1].rate += mb->token_costs[type][band][0][t1];
428 tokens[next][1].token = ZERO_TOKEN;
430 /* Don't update next, because we didn't add a new node. */
434 /* Now pick the best path through the whole trellis. */
435 band = vp8_coef_bands[i + 1];
436 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
437 rate0 = tokens[next][0].rate;
438 rate1 = tokens[next][1].rate;
439 error0 = tokens[next][0].error;
440 error1 = tokens[next][1].error;
441 t0 = tokens[next][0].token;
442 t1 = tokens[next][1].token;
443 rate0 += mb->token_costs[type][band][pt][t0];
444 rate1 += mb->token_costs[type][band][pt][t1];
445 rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0);
446 rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1);
447 if (rd_cost0 == rd_cost1)
449 rd_cost0 = RDTRUNC(rdmult, rddiv, rate0, error0);
450 rd_cost1 = RDTRUNC(rdmult, rddiv, rate1, error1);
452 best = rd_cost1 < rd_cost0;
454 for (i = next; i < eob; i = next)
456 x = tokens[i][best].qc;
459 rc = vp8_default_zig_zag1d[i];
461 dqcoeff_ptr[rc] = x * dequant_ptr[rc];
462 next = tokens[i][best].next;
463 best = (best_mask[best] >> i) & 1;
468 *a = *l = (d->eob != !type);
471 static void optimize_mb(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
476 ENTROPY_CONTEXT_PLANES t_above, t_left;
480 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
481 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
483 ta = (ENTROPY_CONTEXT *)&t_above;
484 tl = (ENTROPY_CONTEXT *)&t_left;
486 has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
487 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
488 type = has_2nd_order ? PLANE_TYPE_Y_NO_DC : PLANE_TYPE_Y_WITH_DC;
490 for (b = 0; b < 16; b++)
492 optimize_b(x, b, type,
493 ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
496 for (b = 16; b < 24; b++)
498 optimize_b(x, b, PLANE_TYPE_UV,
499 ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
505 optimize_b(x, b, PLANE_TYPE_Y2,
506 ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
511 void vp8_optimize_mby(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
517 ENTROPY_CONTEXT_PLANES t_above, t_left;
521 if (!x->e_mbd.above_context)
524 if (!x->e_mbd.left_context)
527 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
528 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
530 ta = (ENTROPY_CONTEXT *)&t_above;
531 tl = (ENTROPY_CONTEXT *)&t_left;
533 has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
534 && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
535 type = has_2nd_order ? PLANE_TYPE_Y_NO_DC : PLANE_TYPE_Y_WITH_DC;
537 for (b = 0; b < 16; b++)
539 optimize_b(x, b, type,
540 ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
547 optimize_b(x, b, PLANE_TYPE_Y2,
548 ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
552 void vp8_optimize_mbuv(MACROBLOCK *x, const VP8_ENCODER_RTCD *rtcd)
555 ENTROPY_CONTEXT_PLANES t_above, t_left;
559 if (!x->e_mbd.above_context)
562 if (!x->e_mbd.left_context)
565 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
566 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
568 ta = (ENTROPY_CONTEXT *)&t_above;
569 tl = (ENTROPY_CONTEXT *)&t_left;
571 for (b = 16; b < 24; b++)
573 optimize_b(x, b, PLANE_TYPE_UV,
574 ta + vp8_block2above[b], tl + vp8_block2left[b], rtcd);
578 void vp8_encode_inter16x16(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
580 vp8_build_inter_predictors_mb(&x->e_mbd);
582 vp8_subtract_mb(rtcd, x);
589 optimize_mb(x, rtcd);
591 vp8_inverse_transform_mb(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
593 RECON_INVOKE(&rtcd->common->recon, recon_mb)
594 (IF_RTCD(&rtcd->common->recon), &x->e_mbd);
598 /* this funciton is used by first pass only */
599 void vp8_encode_inter16x16y(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
601 vp8_build_inter16x16_predictors_mby(&x->e_mbd);
603 ENCODEMB_INVOKE(&rtcd->encodemb, submby)(x->src_diff, x->src.y_buffer, x->e_mbd.predictor, x->src.y_stride);
609 vp8_inverse_transform_mby(IF_RTCD(&rtcd->common->idct), &x->e_mbd);
611 RECON_INVOKE(&rtcd->common->recon, recon_mby)
612 (IF_RTCD(&rtcd->common->recon), &x->e_mbd);
616 void vp8_encode_inter16x16uvrd(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *x)
618 vp8_build_inter_predictors_mbuv(&x->e_mbd);
619 ENCODEMB_INVOKE(&rtcd->encodemb, submbuv)(x->src_diff, x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor, x->src.uv_stride);
621 vp8_transform_mbuv(x);
623 vp8_quantize_mbuv(x);