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.
14 #include "./vp9_rtcd.h"
16 #include "vpx_mem/vpx_mem.h"
18 #include "vp9/common/vp9_common.h"
19 #include "vp9/common/vp9_entropy.h"
20 #include "vp9/common/vp9_entropymode.h"
21 #include "vp9/common/vp9_idct.h"
22 #include "vp9/common/vp9_mvref_common.h"
23 #include "vp9/common/vp9_pred_common.h"
24 #include "vp9/common/vp9_quant_common.h"
25 #include "vp9/common/vp9_reconinter.h"
26 #include "vp9/common/vp9_reconintra.h"
27 #include "vp9/common/vp9_seg_common.h"
28 #include "vp9/common/vp9_systemdependent.h"
30 #include "vp9/encoder/vp9_cost.h"
31 #include "vp9/encoder/vp9_encodemb.h"
32 #include "vp9/encoder/vp9_encodemv.h"
33 #include "vp9/encoder/vp9_encoder.h"
34 #include "vp9/encoder/vp9_mcomp.h"
35 #include "vp9/encoder/vp9_quantize.h"
36 #include "vp9/encoder/vp9_ratectrl.h"
37 #include "vp9/encoder/vp9_rd.h"
38 #include "vp9/encoder/vp9_rdopt.h"
39 #include "vp9/encoder/vp9_variance.h"
41 #define RD_THRESH_MAX_FACT 64
42 #define RD_THRESH_INC 1
44 #define LAST_FRAME_MODE_MASK ((1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME) | \
46 #define GOLDEN_FRAME_MODE_MASK ((1 << LAST_FRAME) | (1 << ALTREF_FRAME) | \
48 #define ALT_REF_MODE_MASK ((1 << LAST_FRAME) | (1 << GOLDEN_FRAME) | \
51 #define SECOND_REF_FRAME_MASK ((1 << ALTREF_FRAME) | 0x01)
53 #define MIN_EARLY_TERM_INDEX 3
57 MV_REFERENCE_FRAME ref_frame[2];
61 MV_REFERENCE_FRAME ref_frame[2];
64 struct rdcost_block_args {
66 ENTROPY_CONTEXT t_above[16];
67 ENTROPY_CONTEXT t_left[16];
77 int use_fast_coef_costing;
81 static const MODE_DEFINITION vp9_mode_order[MAX_MODES] = {
82 {NEARESTMV, {LAST_FRAME, NONE}},
83 {NEARESTMV, {ALTREF_FRAME, NONE}},
84 {NEARESTMV, {GOLDEN_FRAME, NONE}},
86 {DC_PRED, {INTRA_FRAME, NONE}},
88 {NEWMV, {LAST_FRAME, NONE}},
89 {NEWMV, {ALTREF_FRAME, NONE}},
90 {NEWMV, {GOLDEN_FRAME, NONE}},
92 {NEARMV, {LAST_FRAME, NONE}},
93 {NEARMV, {ALTREF_FRAME, NONE}},
94 {NEARMV, {GOLDEN_FRAME, NONE}},
96 {ZEROMV, {LAST_FRAME, NONE}},
97 {ZEROMV, {GOLDEN_FRAME, NONE}},
98 {ZEROMV, {ALTREF_FRAME, NONE}},
100 {NEARESTMV, {LAST_FRAME, ALTREF_FRAME}},
101 {NEARESTMV, {GOLDEN_FRAME, ALTREF_FRAME}},
103 {TM_PRED, {INTRA_FRAME, NONE}},
105 {NEARMV, {LAST_FRAME, ALTREF_FRAME}},
106 {NEWMV, {LAST_FRAME, ALTREF_FRAME}},
107 {NEARMV, {GOLDEN_FRAME, ALTREF_FRAME}},
108 {NEWMV, {GOLDEN_FRAME, ALTREF_FRAME}},
110 {ZEROMV, {LAST_FRAME, ALTREF_FRAME}},
111 {ZEROMV, {GOLDEN_FRAME, ALTREF_FRAME}},
113 {H_PRED, {INTRA_FRAME, NONE}},
114 {V_PRED, {INTRA_FRAME, NONE}},
115 {D135_PRED, {INTRA_FRAME, NONE}},
116 {D207_PRED, {INTRA_FRAME, NONE}},
117 {D153_PRED, {INTRA_FRAME, NONE}},
118 {D63_PRED, {INTRA_FRAME, NONE}},
119 {D117_PRED, {INTRA_FRAME, NONE}},
120 {D45_PRED, {INTRA_FRAME, NONE}},
123 static const REF_DEFINITION vp9_ref_order[MAX_REFS] = {
124 {{LAST_FRAME, NONE}},
125 {{GOLDEN_FRAME, NONE}},
126 {{ALTREF_FRAME, NONE}},
127 {{LAST_FRAME, ALTREF_FRAME}},
128 {{GOLDEN_FRAME, ALTREF_FRAME}},
129 {{INTRA_FRAME, NONE}},
132 static int raster_block_offset(BLOCK_SIZE plane_bsize,
133 int raster_block, int stride) {
134 const int bw = b_width_log2(plane_bsize);
135 const int y = 4 * (raster_block >> bw);
136 const int x = 4 * (raster_block & ((1 << bw) - 1));
137 return y * stride + x;
139 static int16_t* raster_block_offset_int16(BLOCK_SIZE plane_bsize,
140 int raster_block, int16_t *base) {
141 const int stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
142 return base + raster_block_offset(plane_bsize, raster_block, stride);
145 static void swap_block_ptr(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
146 int m, int n, int min_plane, int max_plane) {
149 for (i = min_plane; i < max_plane; ++i) {
150 struct macroblock_plane *const p = &x->plane[i];
151 struct macroblockd_plane *const pd = &x->e_mbd.plane[i];
153 p->coeff = ctx->coeff_pbuf[i][m];
154 p->qcoeff = ctx->qcoeff_pbuf[i][m];
155 pd->dqcoeff = ctx->dqcoeff_pbuf[i][m];
156 p->eobs = ctx->eobs_pbuf[i][m];
158 ctx->coeff_pbuf[i][m] = ctx->coeff_pbuf[i][n];
159 ctx->qcoeff_pbuf[i][m] = ctx->qcoeff_pbuf[i][n];
160 ctx->dqcoeff_pbuf[i][m] = ctx->dqcoeff_pbuf[i][n];
161 ctx->eobs_pbuf[i][m] = ctx->eobs_pbuf[i][n];
163 ctx->coeff_pbuf[i][n] = p->coeff;
164 ctx->qcoeff_pbuf[i][n] = p->qcoeff;
165 ctx->dqcoeff_pbuf[i][n] = pd->dqcoeff;
166 ctx->eobs_pbuf[i][n] = p->eobs;
170 static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize,
171 MACROBLOCK *x, MACROBLOCKD *xd,
172 int *out_rate_sum, int64_t *out_dist_sum) {
173 // Note our transform coeffs are 8 times an orthogonal transform.
174 // Hence quantizer step is also 8 times. To get effective quantizer
175 // we need to divide by 8 before sending to modeling function.
177 int64_t rate_sum = 0;
178 int64_t dist_sum = 0;
179 const int ref = xd->mi[0].src_mi->mbmi.ref_frame[0];
181 unsigned int var = 0;
182 unsigned int sum_sse = 0;
187 x->pred_sse[ref] = 0;
189 for (i = 0; i < MAX_MB_PLANE; ++i) {
190 struct macroblock_plane *const p = &x->plane[i];
191 struct macroblockd_plane *const pd = &xd->plane[i];
192 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
193 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
194 const BLOCK_SIZE unit_size = txsize_to_bsize[max_tx_size];
195 int bw = 1 << (b_width_log2_lookup[bs] - b_width_log2_lookup[unit_size]);
196 int bh = 1 << (b_height_log2_lookup[bs] - b_width_log2_lookup[unit_size]);
198 int lw = b_width_log2_lookup[unit_size] + 2;
199 int lh = b_height_log2_lookup[unit_size] + 2;
203 for (idy = 0; idy < bh; ++idy) {
204 for (idx = 0; idx < bw; ++idx) {
205 uint8_t *src = p->src.buf + (idy * p->src.stride << lh) + (idx << lw);
206 uint8_t *dst = pd->dst.buf + (idy * pd->dst.stride << lh) + (idx << lh);
207 int block_idx = (idy << 1) + idx;
209 var = cpi->fn_ptr[unit_size].vf(src, p->src.stride,
210 dst, pd->dst.stride, &sse);
211 x->bsse[(i << 2) + block_idx] = sse;
214 if (!x->select_tx_size) {
215 if (x->bsse[(i << 2) + block_idx] < p->quant_thred[0] >> shift)
216 x->skip_txfm[(i << 2) + block_idx] = 1;
217 else if (var < p->quant_thred[1] >> shift)
218 x->skip_txfm[(i << 2) + block_idx] = 2;
220 x->skip_txfm[(i << 2) + block_idx] = 0;
224 x->pred_sse[ref] += sse;
228 // Fast approximate the modelling function.
229 if (cpi->oxcf.speed > 4) {
232 int64_t square_error = sse;
233 int quantizer = (pd->dequant[1] >> 3);
236 rate = (square_error * (280 - quantizer)) >> 8;
239 dist = (square_error * quantizer) >> 8;
243 vp9_model_rd_from_var_lapndz(sum_sse, 1 << num_pels_log2_lookup[bs],
244 pd->dequant[1] >> 3, &rate, &dist);
250 *out_rate_sum = (int)rate_sum;
251 *out_dist_sum = dist_sum << 4;
254 int64_t vp9_block_error_c(const tran_low_t *coeff, const tran_low_t *dqcoeff,
255 intptr_t block_size, int64_t *ssz) {
257 int64_t error = 0, sqcoeff = 0;
259 for (i = 0; i < block_size; i++) {
260 const int diff = coeff[i] - dqcoeff[i];
261 error += diff * diff;
262 sqcoeff += coeff[i] * coeff[i];
269 /* The trailing '0' is a terminator which is used inside cost_coeffs() to
270 * decide whether to include cost of a trailing EOB node or not (i.e. we
271 * can skip this if the last coefficient in this transform block, e.g. the
272 * 16th coefficient in a 4x4 block or the 64th coefficient in a 8x8 block,
274 static const int16_t band_counts[TX_SIZES][8] = {
275 { 1, 2, 3, 4, 3, 16 - 13, 0 },
276 { 1, 2, 3, 4, 11, 64 - 21, 0 },
277 { 1, 2, 3, 4, 11, 256 - 21, 0 },
278 { 1, 2, 3, 4, 11, 1024 - 21, 0 },
280 static INLINE int cost_coeffs(MACROBLOCK *x,
281 int plane, int block,
282 ENTROPY_CONTEXT *A, ENTROPY_CONTEXT *L,
284 const int16_t *scan, const int16_t *nb,
285 int use_fast_coef_costing) {
286 MACROBLOCKD *const xd = &x->e_mbd;
287 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
288 const struct macroblock_plane *p = &x->plane[plane];
289 const struct macroblockd_plane *pd = &xd->plane[plane];
290 const PLANE_TYPE type = pd->plane_type;
291 const int16_t *band_count = &band_counts[tx_size][1];
292 const int eob = p->eobs[block];
293 const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
294 unsigned int (*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
295 x->token_costs[tx_size][type][is_inter_block(mbmi)];
296 uint8_t token_cache[32 * 32];
297 int pt = combine_entropy_contexts(*A, *L);
299 // Check for consistency of tx_size with mode info
300 assert(type == PLANE_TYPE_Y ? mbmi->tx_size == tx_size
301 : get_uv_tx_size(mbmi, pd) == tx_size);
305 cost = token_costs[0][0][pt][EOB_TOKEN];
308 int band_left = *band_count++;
312 int prev_t = vp9_dct_value_tokens_ptr[v].token;
313 cost = (*token_costs)[0][pt][prev_t] + vp9_dct_value_cost_ptr[v];
314 token_cache[0] = vp9_pt_energy_class[prev_t];
318 for (c = 1; c < eob; c++) {
319 const int rc = scan[c];
323 t = vp9_dct_value_tokens_ptr[v].token;
324 if (use_fast_coef_costing) {
325 cost += (*token_costs)[!prev_t][!prev_t][t] + vp9_dct_value_cost_ptr[v];
327 pt = get_coef_context(nb, token_cache, c);
328 cost += (*token_costs)[!prev_t][pt][t] + vp9_dct_value_cost_ptr[v];
329 token_cache[rc] = vp9_pt_energy_class[t];
333 band_left = *band_count++;
340 if (use_fast_coef_costing) {
341 cost += (*token_costs)[0][!prev_t][EOB_TOKEN];
343 pt = get_coef_context(nb, token_cache, c);
344 cost += (*token_costs)[0][pt][EOB_TOKEN];
349 // is eob first coefficient;
354 static void dist_block(int plane, int block, TX_SIZE tx_size,
355 struct rdcost_block_args* args) {
356 const int ss_txfrm_size = tx_size << 1;
357 MACROBLOCK* const x = args->x;
358 MACROBLOCKD* const xd = &x->e_mbd;
359 const struct macroblock_plane *const p = &x->plane[plane];
360 const struct macroblockd_plane *const pd = &xd->plane[plane];
362 int shift = tx_size == TX_32X32 ? 0 : 2;
363 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
364 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
365 args->dist = vp9_block_error(coeff, dqcoeff, 16 << ss_txfrm_size,
367 args->sse = this_sse >> shift;
369 if (x->skip_encode && !is_inter_block(&xd->mi[0].src_mi->mbmi)) {
370 // TODO(jingning): tune the model to better capture the distortion.
371 int64_t p = (pd->dequant[1] * pd->dequant[1] *
372 (1 << ss_txfrm_size)) >> (shift + 2);
373 args->dist += (p >> 4);
378 static void rate_block(int plane, int block, BLOCK_SIZE plane_bsize,
379 TX_SIZE tx_size, struct rdcost_block_args* args) {
381 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x_idx, &y_idx);
383 args->rate = cost_coeffs(args->x, plane, block, args->t_above + x_idx,
384 args->t_left + y_idx, tx_size,
385 args->so->scan, args->so->neighbors,
386 args->use_fast_coef_costing);
389 static void block_rd_txfm(int plane, int block, BLOCK_SIZE plane_bsize,
390 TX_SIZE tx_size, void *arg) {
391 struct rdcost_block_args *args = arg;
392 MACROBLOCK *const x = args->x;
393 MACROBLOCKD *const xd = &x->e_mbd;
394 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
395 int64_t rd1, rd2, rd;
400 if (!is_inter_block(mbmi)) {
401 vp9_encode_block_intra(x, plane, block, plane_bsize, tx_size, &mbmi->skip);
402 dist_block(plane, block, tx_size, args);
403 } else if (max_txsize_lookup[plane_bsize] == tx_size) {
404 if (x->skip_txfm[(plane << 2) + (block >> (tx_size << 1))] == 0) {
405 // full forward transform and quantization
406 vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
407 dist_block(plane, block, tx_size, args);
408 } else if (x->skip_txfm[(plane << 2) + (block >> (tx_size << 1))] == 2) {
409 // compute DC coefficient
410 tran_low_t *const coeff = BLOCK_OFFSET(x->plane[plane].coeff, block);
411 tran_low_t *const dqcoeff = BLOCK_OFFSET(xd->plane[plane].dqcoeff, block);
412 vp9_xform_quant_dc(x, plane, block, plane_bsize, tx_size);
413 args->sse = x->bsse[(plane << 2) + (block >> (tx_size << 1))] << 4;
414 args->dist = args->sse;
415 if (!x->plane[plane].eobs[block])
416 args->dist = args->sse - ((coeff[0] * coeff[0] -
417 (coeff[0] - dqcoeff[0]) * (coeff[0] - dqcoeff[0])) >> 2);
419 // skip forward transform
420 x->plane[plane].eobs[block] = 0;
421 args->sse = x->bsse[(plane << 2) + (block >> (tx_size << 1))] << 4;
422 args->dist = args->sse;
425 // full forward transform and quantization
426 vp9_xform_quant(x, plane, block, plane_bsize, tx_size);
427 dist_block(plane, block, tx_size, args);
430 rate_block(plane, block, plane_bsize, tx_size, args);
431 rd1 = RDCOST(x->rdmult, x->rddiv, args->rate, args->dist);
432 rd2 = RDCOST(x->rdmult, x->rddiv, 0, args->sse);
434 // TODO(jingning): temporarily enabled only for luma component
437 x->zcoeff_blk[tx_size][block] = !x->plane[plane].eobs[block] ||
438 (rd1 > rd2 && !xd->lossless);
440 args->this_rate += args->rate;
441 args->this_dist += args->dist;
442 args->this_sse += args->sse;
445 if (args->this_rd > args->best_rd) {
451 static void txfm_rd_in_plane(MACROBLOCK *x,
452 int *rate, int64_t *distortion,
453 int *skippable, int64_t *sse,
454 int64_t ref_best_rd, int plane,
455 BLOCK_SIZE bsize, TX_SIZE tx_size,
456 int use_fast_coef_casting) {
457 MACROBLOCKD *const xd = &x->e_mbd;
458 const struct macroblockd_plane *const pd = &xd->plane[plane];
459 struct rdcost_block_args args;
462 args.best_rd = ref_best_rd;
463 args.use_fast_coef_costing = use_fast_coef_casting;
466 xd->mi[0].src_mi->mbmi.tx_size = tx_size;
468 vp9_get_entropy_contexts(bsize, tx_size, pd, args.t_above, args.t_left);
470 args.so = get_scan(xd, tx_size, pd->plane_type, 0);
472 vp9_foreach_transformed_block_in_plane(xd, bsize, plane,
473 block_rd_txfm, &args);
476 *distortion = INT64_MAX;
480 *distortion = args.this_dist;
481 *rate = args.this_rate;
482 *sse = args.this_sse;
483 *skippable = vp9_is_skippable_in_plane(x, bsize, plane);
487 static void choose_largest_tx_size(VP9_COMP *cpi, MACROBLOCK *x,
488 int *rate, int64_t *distortion,
489 int *skip, int64_t *sse,
492 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
493 VP9_COMMON *const cm = &cpi->common;
494 const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode];
495 MACROBLOCKD *const xd = &x->e_mbd;
496 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
498 mbmi->tx_size = MIN(max_tx_size, largest_tx_size);
500 txfm_rd_in_plane(x, rate, distortion, skip,
501 sse, ref_best_rd, 0, bs,
502 mbmi->tx_size, cpi->sf.use_fast_coef_costing);
505 static void choose_tx_size_from_rd(VP9_COMP *cpi, MACROBLOCK *x,
510 int64_t tx_cache[TX_MODES],
513 const TX_SIZE max_tx_size = max_txsize_lookup[bs];
514 VP9_COMMON *const cm = &cpi->common;
515 MACROBLOCKD *const xd = &x->e_mbd;
516 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
517 vp9_prob skip_prob = vp9_get_skip_prob(cm, xd);
518 int r[TX_SIZES][2], s[TX_SIZES];
519 int64_t d[TX_SIZES], sse[TX_SIZES];
520 int64_t rd[TX_SIZES][2] = {{INT64_MAX, INT64_MAX},
521 {INT64_MAX, INT64_MAX},
522 {INT64_MAX, INT64_MAX},
523 {INT64_MAX, INT64_MAX}};
526 const TX_SIZE max_mode_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode];
527 int64_t best_rd = INT64_MAX;
528 TX_SIZE best_tx = max_tx_size;
530 const vp9_prob *tx_probs = get_tx_probs2(max_tx_size, xd, &cm->fc.tx_probs);
531 assert(skip_prob > 0);
532 s0 = vp9_cost_bit(skip_prob, 0);
533 s1 = vp9_cost_bit(skip_prob, 1);
535 for (n = max_tx_size; n >= 0; n--) {
536 txfm_rd_in_plane(x, &r[n][0], &d[n], &s[n],
537 &sse[n], ref_best_rd, 0, bs, n,
538 cpi->sf.use_fast_coef_costing);
540 if (r[n][0] < INT_MAX) {
541 for (m = 0; m <= n - (n == (int) max_tx_size); m++) {
543 r[n][1] += vp9_cost_zero(tx_probs[m]);
545 r[n][1] += vp9_cost_one(tx_probs[m]);
548 if (d[n] == INT64_MAX) {
549 rd[n][0] = rd[n][1] = INT64_MAX;
551 rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, d[n]);
553 rd[n][0] = RDCOST(x->rdmult, x->rddiv, r[n][0] + s0, d[n]);
554 rd[n][1] = RDCOST(x->rdmult, x->rddiv, r[n][1] + s0, d[n]);
557 // Early termination in transform size search.
558 if (cpi->sf.tx_size_search_breakout &&
559 (rd[n][1] == INT64_MAX ||
560 (n < (int) max_tx_size && rd[n][1] > rd[n + 1][1]) ||
564 if (rd[n][1] < best_rd) {
569 mbmi->tx_size = cm->tx_mode == TX_MODE_SELECT ?
570 best_tx : MIN(max_tx_size, max_mode_tx_size);
573 *distortion = d[mbmi->tx_size];
574 *rate = r[mbmi->tx_size][cm->tx_mode == TX_MODE_SELECT];
575 *skip = s[mbmi->tx_size];
576 *psse = sse[mbmi->tx_size];
578 tx_cache[ONLY_4X4] = rd[TX_4X4][0];
579 tx_cache[ALLOW_8X8] = rd[TX_8X8][0];
580 tx_cache[ALLOW_16X16] = rd[MIN(max_tx_size, TX_16X16)][0];
581 tx_cache[ALLOW_32X32] = rd[MIN(max_tx_size, TX_32X32)][0];
583 if (max_tx_size == TX_32X32 && best_tx == TX_32X32) {
584 tx_cache[TX_MODE_SELECT] = rd[TX_32X32][1];
585 } else if (max_tx_size >= TX_16X16 && best_tx == TX_16X16) {
586 tx_cache[TX_MODE_SELECT] = rd[TX_16X16][1];
587 } else if (rd[TX_8X8][1] < rd[TX_4X4][1]) {
588 tx_cache[TX_MODE_SELECT] = rd[TX_8X8][1];
590 tx_cache[TX_MODE_SELECT] = rd[TX_4X4][1];
594 static void super_block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate,
595 int64_t *distortion, int *skip,
596 int64_t *psse, BLOCK_SIZE bs,
597 int64_t txfm_cache[TX_MODES],
598 int64_t ref_best_rd) {
599 MACROBLOCKD *xd = &x->e_mbd;
601 int64_t *ret_sse = psse ? psse : &sse;
603 assert(bs == xd->mi[0].src_mi->mbmi.sb_type);
605 if (cpi->sf.tx_size_search_method == USE_LARGESTALL || xd->lossless) {
606 vpx_memset(txfm_cache, 0, TX_MODES * sizeof(int64_t));
607 choose_largest_tx_size(cpi, x, rate, distortion, skip, ret_sse, ref_best_rd,
610 choose_tx_size_from_rd(cpi, x, rate, distortion, skip, ret_sse,
611 txfm_cache, ref_best_rd, bs);
615 static int conditional_skipintra(PREDICTION_MODE mode,
616 PREDICTION_MODE best_intra_mode) {
617 if (mode == D117_PRED &&
618 best_intra_mode != V_PRED &&
619 best_intra_mode != D135_PRED)
621 if (mode == D63_PRED &&
622 best_intra_mode != V_PRED &&
623 best_intra_mode != D45_PRED)
625 if (mode == D207_PRED &&
626 best_intra_mode != H_PRED &&
627 best_intra_mode != D45_PRED)
629 if (mode == D153_PRED &&
630 best_intra_mode != H_PRED &&
631 best_intra_mode != D135_PRED)
636 static int64_t rd_pick_intra4x4block(VP9_COMP *cpi, MACROBLOCK *x, int ib,
637 PREDICTION_MODE *best_mode,
638 const int *bmode_costs,
639 ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l,
640 int *bestrate, int *bestratey,
641 int64_t *bestdistortion,
642 BLOCK_SIZE bsize, int64_t rd_thresh) {
643 PREDICTION_MODE mode;
644 MACROBLOCKD *const xd = &x->e_mbd;
645 int64_t best_rd = rd_thresh;
647 struct macroblock_plane *p = &x->plane[0];
648 struct macroblockd_plane *pd = &xd->plane[0];
649 const int src_stride = p->src.stride;
650 const int dst_stride = pd->dst.stride;
651 const uint8_t *src_init = &p->src.buf[raster_block_offset(BLOCK_8X8, ib,
653 uint8_t *dst_init = &pd->dst.buf[raster_block_offset(BLOCK_8X8, ib,
655 ENTROPY_CONTEXT ta[2], tempa[2];
656 ENTROPY_CONTEXT tl[2], templ[2];
658 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
659 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
661 uint8_t best_dst[8 * 8];
665 vpx_memcpy(ta, a, sizeof(ta));
666 vpx_memcpy(tl, l, sizeof(tl));
667 xd->mi[0].src_mi->mbmi.tx_size = TX_4X4;
669 for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
672 int64_t distortion = 0;
673 int rate = bmode_costs[mode];
675 if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode)))
678 // Only do the oblique modes if the best so far is
679 // one of the neighboring directional modes
680 if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
681 if (conditional_skipintra(mode, *best_mode))
685 vpx_memcpy(tempa, ta, sizeof(ta));
686 vpx_memcpy(templ, tl, sizeof(tl));
688 for (idy = 0; idy < num_4x4_blocks_high; ++idy) {
689 for (idx = 0; idx < num_4x4_blocks_wide; ++idx) {
690 const int block = ib + idy * 2 + idx;
691 const uint8_t *const src = &src_init[idx * 4 + idy * 4 * src_stride];
692 uint8_t *const dst = &dst_init[idx * 4 + idy * 4 * dst_stride];
693 int16_t *const src_diff = raster_block_offset_int16(BLOCK_8X8, block,
695 tran_low_t *const coeff = BLOCK_OFFSET(x->plane[0].coeff, block);
696 xd->mi[0].src_mi->bmi[block].as_mode = mode;
697 vp9_predict_intra_block(xd, block, 1,
699 x->skip_encode ? src : dst,
700 x->skip_encode ? src_stride : dst_stride,
701 dst, dst_stride, idx, idy, 0);
702 vp9_subtract_block(4, 4, src_diff, 8, src, src_stride, dst, dst_stride);
705 const scan_order *so = &vp9_default_scan_orders[TX_4X4];
706 vp9_fwht4x4(src_diff, coeff, 8);
707 vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
708 ratey += cost_coeffs(x, 0, block, tempa + idx, templ + idy, TX_4X4,
709 so->scan, so->neighbors,
710 cpi->sf.use_fast_coef_costing);
711 if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
713 vp9_iwht4x4_add(BLOCK_OFFSET(pd->dqcoeff, block), dst, dst_stride,
717 const TX_TYPE tx_type = get_tx_type_4x4(PLANE_TYPE_Y, xd, block);
718 const scan_order *so = &vp9_scan_orders[TX_4X4][tx_type];
719 vp9_fht4x4(src_diff, coeff, 8, tx_type);
720 vp9_regular_quantize_b_4x4(x, 0, block, so->scan, so->iscan);
721 ratey += cost_coeffs(x, 0, block, tempa + idx, templ + idy, TX_4X4,
722 so->scan, so->neighbors,
723 cpi->sf.use_fast_coef_costing);
724 distortion += vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, block),
726 if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd)
728 vp9_iht4x4_add(tx_type, BLOCK_OFFSET(pd->dqcoeff, block),
729 dst, dst_stride, p->eobs[block]);
735 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
737 if (this_rd < best_rd) {
740 *bestdistortion = distortion;
743 vpx_memcpy(a, tempa, sizeof(tempa));
744 vpx_memcpy(l, templ, sizeof(templ));
745 for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy)
746 vpx_memcpy(best_dst + idy * 8, dst_init + idy * dst_stride,
747 num_4x4_blocks_wide * 4);
753 if (best_rd >= rd_thresh || x->skip_encode)
756 for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy)
757 vpx_memcpy(dst_init + idy * dst_stride, best_dst + idy * 8,
758 num_4x4_blocks_wide * 4);
763 static int64_t rd_pick_intra_sub_8x8_y_mode(VP9_COMP *cpi, MACROBLOCK *mb,
764 int *rate, int *rate_y,
768 const MACROBLOCKD *const xd = &mb->e_mbd;
769 MODE_INFO *const mic = xd->mi[0].src_mi;
770 const MODE_INFO *above_mi = xd->mi[-xd->mi_stride].src_mi;
771 const MODE_INFO *left_mi = xd->left_available ? xd->mi[-1].src_mi : NULL;
772 const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
773 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
774 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
777 int64_t total_distortion = 0;
779 int64_t total_rd = 0;
780 ENTROPY_CONTEXT t_above[4], t_left[4];
781 const int *bmode_costs = cpi->mbmode_cost;
783 vpx_memcpy(t_above, xd->plane[0].above_context, sizeof(t_above));
784 vpx_memcpy(t_left, xd->plane[0].left_context, sizeof(t_left));
786 // Pick modes for each sub-block (of size 4x4, 4x8, or 8x4) in an 8x8 block.
787 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
788 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
789 PREDICTION_MODE best_mode = DC_PRED;
790 int r = INT_MAX, ry = INT_MAX;
791 int64_t d = INT64_MAX, this_rd = INT64_MAX;
793 if (cpi->common.frame_type == KEY_FRAME) {
794 const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, i);
795 const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, i);
797 bmode_costs = cpi->y_mode_costs[A][L];
800 this_rd = rd_pick_intra4x4block(cpi, mb, i, &best_mode, bmode_costs,
801 t_above + idx, t_left + idy, &r, &ry, &d,
802 bsize, best_rd - total_rd);
803 if (this_rd >= best_rd - total_rd)
808 total_distortion += d;
811 mic->bmi[i].as_mode = best_mode;
812 for (j = 1; j < num_4x4_blocks_high; ++j)
813 mic->bmi[i + j * 2].as_mode = best_mode;
814 for (j = 1; j < num_4x4_blocks_wide; ++j)
815 mic->bmi[i + j].as_mode = best_mode;
817 if (total_rd >= best_rd)
823 *rate_y = tot_rate_y;
824 *distortion = total_distortion;
825 mic->mbmi.mode = mic->bmi[3].as_mode;
827 return RDCOST(mb->rdmult, mb->rddiv, cost, total_distortion);
830 static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x,
831 int *rate, int *rate_tokenonly,
832 int64_t *distortion, int *skippable,
834 int64_t tx_cache[TX_MODES],
836 PREDICTION_MODE mode;
837 PREDICTION_MODE mode_selected = DC_PRED;
838 MACROBLOCKD *const xd = &x->e_mbd;
839 MODE_INFO *const mic = xd->mi[0].src_mi;
840 int this_rate, this_rate_tokenonly, s;
841 int64_t this_distortion, this_rd;
842 TX_SIZE best_tx = TX_4X4;
844 int *bmode_costs = cpi->mbmode_cost;
846 if (cpi->sf.tx_size_search_method == USE_FULL_RD)
847 for (i = 0; i < TX_MODES; i++)
848 tx_cache[i] = INT64_MAX;
850 /* Y Search for intra prediction mode */
851 for (mode = DC_PRED; mode <= TM_PRED; mode++) {
852 int64_t local_tx_cache[TX_MODES];
853 MODE_INFO *above_mi = xd->mi[-xd->mi_stride].src_mi;
854 MODE_INFO *left_mi = xd->left_available ? xd->mi[-1].src_mi : NULL;
856 if (cpi->common.frame_type == KEY_FRAME) {
857 const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
858 const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
860 bmode_costs = cpi->y_mode_costs[A][L];
862 mic->mbmi.mode = mode;
864 super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion,
865 &s, NULL, bsize, local_tx_cache, best_rd);
867 if (this_rate_tokenonly == INT_MAX)
870 this_rate = this_rate_tokenonly + bmode_costs[mode];
871 this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
873 if (this_rd < best_rd) {
874 mode_selected = mode;
876 best_tx = mic->mbmi.tx_size;
878 *rate_tokenonly = this_rate_tokenonly;
879 *distortion = this_distortion;
883 if (cpi->sf.tx_size_search_method == USE_FULL_RD && this_rd < INT64_MAX) {
884 for (i = 0; i < TX_MODES && local_tx_cache[i] < INT64_MAX; i++) {
885 const int64_t adj_rd = this_rd + local_tx_cache[i] -
886 local_tx_cache[cpi->common.tx_mode];
887 if (adj_rd < tx_cache[i]) {
888 tx_cache[i] = adj_rd;
894 mic->mbmi.mode = mode_selected;
895 mic->mbmi.tx_size = best_tx;
900 static void super_block_uvrd(const VP9_COMP *cpi, MACROBLOCK *x,
901 int *rate, int64_t *distortion, int *skippable,
902 int64_t *sse, BLOCK_SIZE bsize,
903 int64_t ref_best_rd) {
904 MACROBLOCKD *const xd = &x->e_mbd;
905 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
906 const TX_SIZE uv_tx_size = get_uv_tx_size(mbmi, &xd->plane[1]);
908 int pnrate = 0, pnskip = 1;
909 int64_t pndist = 0, pnsse = 0;
914 if (is_inter_block(mbmi)) {
916 for (plane = 1; plane < MAX_MB_PLANE; ++plane)
917 vp9_subtract_plane(x, bsize, plane);
925 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
926 txfm_rd_in_plane(x, &pnrate, &pndist, &pnskip, &pnsse,
927 ref_best_rd, plane, bsize, uv_tx_size,
928 cpi->sf.use_fast_coef_costing);
929 if (pnrate == INT_MAX)
932 *distortion += pndist;
934 *skippable &= pnskip;
940 *distortion = INT64_MAX;
946 static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x,
947 PICK_MODE_CONTEXT *ctx,
948 int *rate, int *rate_tokenonly,
949 int64_t *distortion, int *skippable,
950 BLOCK_SIZE bsize, TX_SIZE max_tx_size) {
951 MACROBLOCKD *xd = &x->e_mbd;
952 PREDICTION_MODE mode;
953 PREDICTION_MODE mode_selected = DC_PRED;
954 int64_t best_rd = INT64_MAX, this_rd;
955 int this_rate_tokenonly, this_rate, s;
956 int64_t this_distortion, this_sse;
958 for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
959 if (!(cpi->sf.intra_uv_mode_mask[max_tx_size] & (1 << mode)))
962 xd->mi[0].src_mi->mbmi.uv_mode = mode;
964 super_block_uvrd(cpi, x, &this_rate_tokenonly,
965 &this_distortion, &s, &this_sse, bsize, best_rd);
966 if (this_rate_tokenonly == INT_MAX)
968 this_rate = this_rate_tokenonly +
969 cpi->intra_uv_mode_cost[cpi->common.frame_type][mode];
970 this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
972 if (this_rd < best_rd) {
973 mode_selected = mode;
976 *rate_tokenonly = this_rate_tokenonly;
977 *distortion = this_distortion;
979 if (!x->select_tx_size)
980 swap_block_ptr(x, ctx, 2, 0, 1, MAX_MB_PLANE);
984 xd->mi[0].src_mi->mbmi.uv_mode = mode_selected;
988 static int64_t rd_sbuv_dcpred(const VP9_COMP *cpi, MACROBLOCK *x,
989 int *rate, int *rate_tokenonly,
990 int64_t *distortion, int *skippable,
992 const VP9_COMMON *cm = &cpi->common;
995 x->e_mbd.mi[0].src_mi->mbmi.uv_mode = DC_PRED;
996 super_block_uvrd(cpi, x, rate_tokenonly, distortion,
997 skippable, &unused, bsize, INT64_MAX);
998 *rate = *rate_tokenonly + cpi->intra_uv_mode_cost[cm->frame_type][DC_PRED];
999 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
1002 static void choose_intra_uv_mode(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
1003 BLOCK_SIZE bsize, TX_SIZE max_tx_size,
1004 int *rate_uv, int *rate_uv_tokenonly,
1005 int64_t *dist_uv, int *skip_uv,
1006 PREDICTION_MODE *mode_uv) {
1007 MACROBLOCK *const x = &cpi->mb;
1009 // Use an estimated rd for uv_intra based on DC_PRED if the
1010 // appropriate speed flag is set.
1011 if (cpi->sf.use_uv_intra_rd_estimate) {
1012 rd_sbuv_dcpred(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv,
1013 skip_uv, bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize);
1014 // Else do a proper rd search for each possible transform size that may
1015 // be considered in the main rd loop.
1017 rd_pick_intra_sbuv_mode(cpi, x, ctx,
1018 rate_uv, rate_uv_tokenonly, dist_uv, skip_uv,
1019 bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize, max_tx_size);
1021 *mode_uv = x->e_mbd.mi[0].src_mi->mbmi.uv_mode;
1024 static int cost_mv_ref(const VP9_COMP *cpi, PREDICTION_MODE mode,
1026 assert(is_inter_mode(mode));
1027 return cpi->inter_mode_cost[mode_context][INTER_OFFSET(mode)];
1030 static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
1033 int mi_row, int mi_col,
1034 int_mv single_newmv[MAX_REF_FRAMES],
1037 static int set_and_cost_bmi_mvs(VP9_COMP *cpi, MACROBLOCKD *xd, int i,
1038 PREDICTION_MODE mode, int_mv this_mv[2],
1039 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES],
1040 int_mv seg_mvs[MAX_REF_FRAMES],
1041 int_mv *best_ref_mv[2], const int *mvjcost,
1043 MODE_INFO *const mic = xd->mi[0].src_mi;
1044 const MB_MODE_INFO *const mbmi = &mic->mbmi;
1047 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[mbmi->sb_type];
1048 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[mbmi->sb_type];
1049 const int is_compound = has_second_ref(mbmi);
1053 this_mv[0].as_int = seg_mvs[mbmi->ref_frame[0]].as_int;
1054 thismvcost += vp9_mv_bit_cost(&this_mv[0].as_mv, &best_ref_mv[0]->as_mv,
1055 mvjcost, mvcost, MV_COST_WEIGHT_SUB);
1057 this_mv[1].as_int = seg_mvs[mbmi->ref_frame[1]].as_int;
1058 thismvcost += vp9_mv_bit_cost(&this_mv[1].as_mv, &best_ref_mv[1]->as_mv,
1059 mvjcost, mvcost, MV_COST_WEIGHT_SUB);
1064 this_mv[0].as_int = frame_mv[mode][mbmi->ref_frame[0]].as_int;
1066 this_mv[1].as_int = frame_mv[mode][mbmi->ref_frame[1]].as_int;
1069 this_mv[0].as_int = 0;
1071 this_mv[1].as_int = 0;
1077 mic->bmi[i].as_mv[0].as_int = this_mv[0].as_int;
1079 mic->bmi[i].as_mv[1].as_int = this_mv[1].as_int;
1081 mic->bmi[i].as_mode = mode;
1083 for (idy = 0; idy < num_4x4_blocks_high; ++idy)
1084 for (idx = 0; idx < num_4x4_blocks_wide; ++idx)
1085 vpx_memcpy(&mic->bmi[i + idy * 2 + idx],
1086 &mic->bmi[i], sizeof(mic->bmi[i]));
1088 return cost_mv_ref(cpi, mode, mbmi->mode_context[mbmi->ref_frame[0]]) +
1092 static int64_t encode_inter_mb_segment(VP9_COMP *cpi,
1097 int64_t *distortion, int64_t *sse,
1098 ENTROPY_CONTEXT *ta,
1099 ENTROPY_CONTEXT *tl,
1100 int mi_row, int mi_col) {
1102 MACROBLOCKD *xd = &x->e_mbd;
1103 struct macroblockd_plane *const pd = &xd->plane[0];
1104 struct macroblock_plane *const p = &x->plane[0];
1105 MODE_INFO *const mi = xd->mi[0].src_mi;
1106 const BLOCK_SIZE plane_bsize = get_plane_block_size(mi->mbmi.sb_type, pd);
1107 const int width = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
1108 const int height = 4 * num_4x4_blocks_high_lookup[plane_bsize];
1111 const uint8_t *const src = &p->src.buf[raster_block_offset(BLOCK_8X8, i,
1113 uint8_t *const dst = &pd->dst.buf[raster_block_offset(BLOCK_8X8, i,
1115 int64_t thisdistortion = 0, thissse = 0;
1116 int thisrate = 0, ref;
1117 const scan_order *so = &vp9_default_scan_orders[TX_4X4];
1118 const int is_compound = has_second_ref(&mi->mbmi);
1119 const InterpKernel *kernel = vp9_get_interp_kernel(mi->mbmi.interp_filter);
1121 for (ref = 0; ref < 1 + is_compound; ++ref) {
1122 const uint8_t *pre = &pd->pre[ref].buf[raster_block_offset(BLOCK_8X8, i,
1123 pd->pre[ref].stride)];
1124 vp9_build_inter_predictor(pre, pd->pre[ref].stride,
1125 dst, pd->dst.stride,
1126 &mi->bmi[i].as_mv[ref].as_mv,
1127 &xd->block_refs[ref]->sf, width, height, ref,
1128 kernel, MV_PRECISION_Q3,
1129 mi_col * MI_SIZE + 4 * (i % 2),
1130 mi_row * MI_SIZE + 4 * (i / 2));
1133 vp9_subtract_block(height, width,
1134 raster_block_offset_int16(BLOCK_8X8, i, p->src_diff), 8,
1136 dst, pd->dst.stride);
1139 for (idy = 0; idy < height / 4; ++idy) {
1140 for (idx = 0; idx < width / 4; ++idx) {
1141 int64_t ssz, rd, rd1, rd2;
1144 k += (idy * 2 + idx);
1145 coeff = BLOCK_OFFSET(p->coeff, k);
1146 x->fwd_txm4x4(raster_block_offset_int16(BLOCK_8X8, k, p->src_diff),
1148 vp9_regular_quantize_b_4x4(x, 0, k, so->scan, so->iscan);
1149 thisdistortion += vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, k),
1152 thisrate += cost_coeffs(x, 0, k, ta + (k & 1), tl + (k >> 1), TX_4X4,
1153 so->scan, so->neighbors,
1154 cpi->sf.use_fast_coef_costing);
1155 rd1 = RDCOST(x->rdmult, x->rddiv, thisrate, thisdistortion >> 2);
1156 rd2 = RDCOST(x->rdmult, x->rddiv, 0, thissse >> 2);
1163 *distortion = thisdistortion >> 2;
1164 *labelyrate = thisrate;
1165 *sse = thissse >> 2;
1167 return RDCOST(x->rdmult, x->rddiv, *labelyrate, *distortion);
1178 ENTROPY_CONTEXT ta[2];
1179 ENTROPY_CONTEXT tl[2];
1191 PREDICTION_MODE modes[4];
1192 SEG_RDSTAT rdstat[4][INTER_MODES];
1196 static INLINE int mv_check_bounds(const MACROBLOCK *x, const MV *mv) {
1197 return (mv->row >> 3) < x->mv_row_min ||
1198 (mv->row >> 3) > x->mv_row_max ||
1199 (mv->col >> 3) < x->mv_col_min ||
1200 (mv->col >> 3) > x->mv_col_max;
1203 static INLINE void mi_buf_shift(MACROBLOCK *x, int i) {
1204 MB_MODE_INFO *const mbmi = &x->e_mbd.mi[0].src_mi->mbmi;
1205 struct macroblock_plane *const p = &x->plane[0];
1206 struct macroblockd_plane *const pd = &x->e_mbd.plane[0];
1208 p->src.buf = &p->src.buf[raster_block_offset(BLOCK_8X8, i, p->src.stride)];
1209 assert(((intptr_t)pd->pre[0].buf & 0x7) == 0);
1210 pd->pre[0].buf = &pd->pre[0].buf[raster_block_offset(BLOCK_8X8, i,
1211 pd->pre[0].stride)];
1212 if (has_second_ref(mbmi))
1213 pd->pre[1].buf = &pd->pre[1].buf[raster_block_offset(BLOCK_8X8, i,
1214 pd->pre[1].stride)];
1217 static INLINE void mi_buf_restore(MACROBLOCK *x, struct buf_2d orig_src,
1218 struct buf_2d orig_pre[2]) {
1219 MB_MODE_INFO *mbmi = &x->e_mbd.mi[0].src_mi->mbmi;
1220 x->plane[0].src = orig_src;
1221 x->e_mbd.plane[0].pre[0] = orig_pre[0];
1222 if (has_second_ref(mbmi))
1223 x->e_mbd.plane[0].pre[1] = orig_pre[1];
1226 static INLINE int mv_has_subpel(const MV *mv) {
1227 return (mv->row & 0x0F) || (mv->col & 0x0F);
1230 // Check if NEARESTMV/NEARMV/ZEROMV is the cheapest way encode zero motion.
1231 // TODO(aconverse): Find out if this is still productive then clean up or remove
1232 static int check_best_zero_mv(
1233 const VP9_COMP *cpi, const uint8_t mode_context[MAX_REF_FRAMES],
1234 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES], int this_mode,
1235 const MV_REFERENCE_FRAME ref_frames[2]) {
1236 if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) &&
1237 frame_mv[this_mode][ref_frames[0]].as_int == 0 &&
1238 (ref_frames[1] == NONE ||
1239 frame_mv[this_mode][ref_frames[1]].as_int == 0)) {
1240 int rfc = mode_context[ref_frames[0]];
1241 int c1 = cost_mv_ref(cpi, NEARMV, rfc);
1242 int c2 = cost_mv_ref(cpi, NEARESTMV, rfc);
1243 int c3 = cost_mv_ref(cpi, ZEROMV, rfc);
1245 if (this_mode == NEARMV) {
1246 if (c1 > c3) return 0;
1247 } else if (this_mode == NEARESTMV) {
1248 if (c2 > c3) return 0;
1250 assert(this_mode == ZEROMV);
1251 if (ref_frames[1] == NONE) {
1252 if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0) ||
1253 (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0))
1256 if ((c3 >= c2 && frame_mv[NEARESTMV][ref_frames[0]].as_int == 0 &&
1257 frame_mv[NEARESTMV][ref_frames[1]].as_int == 0) ||
1258 (c3 >= c1 && frame_mv[NEARMV][ref_frames[0]].as_int == 0 &&
1259 frame_mv[NEARMV][ref_frames[1]].as_int == 0))
1267 static int64_t rd_pick_best_sub8x8_mode(VP9_COMP *cpi, MACROBLOCK *x,
1268 const TileInfo * const tile,
1269 int_mv *best_ref_mv,
1270 int_mv *second_best_ref_mv,
1271 int64_t best_rd, int *returntotrate,
1273 int64_t *returndistortion,
1274 int *skippable, int64_t *psse,
1276 int_mv seg_mvs[4][MAX_REF_FRAMES],
1277 BEST_SEG_INFO *bsi_buf, int filter_idx,
1278 int mi_row, int mi_col) {
1280 BEST_SEG_INFO *bsi = bsi_buf + filter_idx;
1281 MACROBLOCKD *xd = &x->e_mbd;
1282 MODE_INFO *mi = xd->mi[0].src_mi;
1283 MB_MODE_INFO *mbmi = &mi->mbmi;
1285 int k, br = 0, idx, idy;
1286 int64_t bd = 0, block_sse = 0;
1287 PREDICTION_MODE this_mode;
1288 VP9_COMMON *cm = &cpi->common;
1289 struct macroblock_plane *const p = &x->plane[0];
1290 struct macroblockd_plane *const pd = &xd->plane[0];
1291 const int label_count = 4;
1292 int64_t this_segment_rd = 0;
1293 int label_mv_thresh;
1294 int segmentyrate = 0;
1295 const BLOCK_SIZE bsize = mbmi->sb_type;
1296 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1297 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1298 ENTROPY_CONTEXT t_above[2], t_left[2];
1299 int subpelmv = 1, have_ref = 0;
1300 const int has_second_rf = has_second_ref(mbmi);
1301 const int inter_mode_mask = cpi->sf.inter_mode_mask[bsize];
1305 bsi->segment_rd = best_rd;
1306 bsi->ref_mv[0] = best_ref_mv;
1307 bsi->ref_mv[1] = second_best_ref_mv;
1308 bsi->mvp.as_int = best_ref_mv->as_int;
1309 bsi->mvthresh = mvthresh;
1311 for (i = 0; i < 4; i++)
1312 bsi->modes[i] = ZEROMV;
1314 vpx_memcpy(t_above, pd->above_context, sizeof(t_above));
1315 vpx_memcpy(t_left, pd->left_context, sizeof(t_left));
1317 // 64 makes this threshold really big effectively
1318 // making it so that we very rarely check mvs on
1319 // segments. setting this to 1 would make mv thresh
1320 // roughly equal to what it is for macroblocks
1321 label_mv_thresh = 1 * bsi->mvthresh / label_count;
1323 // Segmentation method overheads
1324 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1325 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1326 // TODO(jingning,rbultje): rewrite the rate-distortion optimization
1327 // loop for 4x4/4x8/8x4 block coding. to be replaced with new rd loop
1328 int_mv mode_mv[MB_MODE_COUNT][2];
1329 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
1330 PREDICTION_MODE mode_selected = ZEROMV;
1331 int64_t best_rd = INT64_MAX;
1332 const int i = idy * 2 + idx;
1335 for (ref = 0; ref < 1 + has_second_rf; ++ref) {
1336 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1337 frame_mv[ZEROMV][frame].as_int = 0;
1338 vp9_append_sub8x8_mvs_for_idx(cm, xd, tile, i, ref, mi_row, mi_col,
1339 &frame_mv[NEARESTMV][frame],
1340 &frame_mv[NEARMV][frame]);
1343 // search for the best motion vector on this segment
1344 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1345 const struct buf_2d orig_src = x->plane[0].src;
1346 struct buf_2d orig_pre[2];
1348 mode_idx = INTER_OFFSET(this_mode);
1349 bsi->rdstat[i][mode_idx].brdcost = INT64_MAX;
1350 if (!(inter_mode_mask & (1 << this_mode)))
1353 if (!check_best_zero_mv(cpi, mbmi->mode_context, frame_mv,
1354 this_mode, mbmi->ref_frame))
1357 vpx_memcpy(orig_pre, pd->pre, sizeof(orig_pre));
1358 vpx_memcpy(bsi->rdstat[i][mode_idx].ta, t_above,
1359 sizeof(bsi->rdstat[i][mode_idx].ta));
1360 vpx_memcpy(bsi->rdstat[i][mode_idx].tl, t_left,
1361 sizeof(bsi->rdstat[i][mode_idx].tl));
1363 // motion search for newmv (single predictor case only)
1364 if (!has_second_rf && this_mode == NEWMV &&
1365 seg_mvs[i][mbmi->ref_frame[0]].as_int == INVALID_MV) {
1366 MV *const new_mv = &mode_mv[NEWMV][0].as_mv;
1368 int thissme, bestsme = INT_MAX;
1369 int sadpb = x->sadperbit4;
1374 /* Is the best so far sufficiently good that we cant justify doing
1375 * and new motion search. */
1376 if (best_rd < label_mv_thresh)
1379 if (cpi->oxcf.mode != BEST) {
1380 // use previous block's result as next block's MV predictor.
1382 bsi->mvp.as_int = mi->bmi[i - 1].as_mv[0].as_int;
1384 bsi->mvp.as_int = mi->bmi[i - 2].as_mv[0].as_int;
1388 max_mv = x->max_mv_context[mbmi->ref_frame[0]];
1390 max_mv = MAX(abs(bsi->mvp.as_mv.row), abs(bsi->mvp.as_mv.col)) >> 3;
1392 if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) {
1393 // Take wtd average of the step_params based on the last frame's
1394 // max mv magnitude and the best ref mvs of the current block for
1395 // the given reference.
1396 step_param = (vp9_init_search_range(max_mv) +
1397 cpi->mv_step_param) / 2;
1399 step_param = cpi->mv_step_param;
1402 mvp_full.row = bsi->mvp.as_mv.row >> 3;
1403 mvp_full.col = bsi->mvp.as_mv.col >> 3;
1405 if (cpi->sf.adaptive_motion_search) {
1406 mvp_full.row = x->pred_mv[mbmi->ref_frame[0]].row >> 3;
1407 mvp_full.col = x->pred_mv[mbmi->ref_frame[0]].col >> 3;
1408 step_param = MAX(step_param, 8);
1411 // adjust src pointer for this block
1414 vp9_set_mv_search_range(x, &bsi->ref_mv[0]->as_mv);
1416 bestsme = vp9_full_pixel_search(
1417 cpi, x, bsize, &mvp_full, step_param, sadpb,
1418 cpi->sf.mv.subpel_search_method != SUBPEL_TREE ? sad_list : NULL,
1419 &bsi->ref_mv[0]->as_mv, new_mv,
1422 // Should we do a full search (best quality only)
1423 if (cpi->oxcf.mode == BEST) {
1424 int_mv *const best_mv = &mi->bmi[i].as_mv[0];
1425 /* Check if mvp_full is within the range. */
1426 clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max,
1427 x->mv_row_min, x->mv_row_max);
1428 thissme = cpi->full_search_sad(x, &mvp_full,
1429 sadpb, 16, &cpi->fn_ptr[bsize],
1430 &bsi->ref_mv[0]->as_mv,
1432 sad_list[1] = sad_list[2] = sad_list[3] = sad_list[4] = INT_MAX;
1433 if (thissme < bestsme) {
1435 *new_mv = best_mv->as_mv;
1437 // The full search result is actually worse so re-instate the
1438 // previous best vector
1439 best_mv->as_mv = *new_mv;
1443 if (bestsme < INT_MAX) {
1445 cpi->find_fractional_mv_step(
1448 &bsi->ref_mv[0]->as_mv,
1449 cm->allow_high_precision_mv,
1450 x->errorperbit, &cpi->fn_ptr[bsize],
1451 cpi->sf.mv.subpel_force_stop,
1452 cpi->sf.mv.subpel_iters_per_step,
1453 cond_sad_list(cpi, sad_list),
1454 x->nmvjointcost, x->mvcost,
1456 &x->pred_sse[mbmi->ref_frame[0]],
1459 // save motion search result for use in compound prediction
1460 seg_mvs[i][mbmi->ref_frame[0]].as_mv = *new_mv;
1463 if (cpi->sf.adaptive_motion_search)
1464 x->pred_mv[mbmi->ref_frame[0]] = *new_mv;
1466 // restore src pointers
1467 mi_buf_restore(x, orig_src, orig_pre);
1470 if (has_second_rf) {
1471 if (seg_mvs[i][mbmi->ref_frame[1]].as_int == INVALID_MV ||
1472 seg_mvs[i][mbmi->ref_frame[0]].as_int == INVALID_MV)
1476 if (has_second_rf && this_mode == NEWMV &&
1477 mbmi->interp_filter == EIGHTTAP) {
1478 // adjust src pointers
1480 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
1482 joint_motion_search(cpi, x, bsize, frame_mv[this_mode],
1483 mi_row, mi_col, seg_mvs[i],
1485 seg_mvs[i][mbmi->ref_frame[0]].as_int =
1486 frame_mv[this_mode][mbmi->ref_frame[0]].as_int;
1487 seg_mvs[i][mbmi->ref_frame[1]].as_int =
1488 frame_mv[this_mode][mbmi->ref_frame[1]].as_int;
1490 // restore src pointers
1491 mi_buf_restore(x, orig_src, orig_pre);
1494 bsi->rdstat[i][mode_idx].brate =
1495 set_and_cost_bmi_mvs(cpi, xd, i, this_mode, mode_mv[this_mode],
1496 frame_mv, seg_mvs[i], bsi->ref_mv,
1497 x->nmvjointcost, x->mvcost);
1499 for (ref = 0; ref < 1 + has_second_rf; ++ref) {
1500 bsi->rdstat[i][mode_idx].mvs[ref].as_int =
1501 mode_mv[this_mode][ref].as_int;
1502 if (num_4x4_blocks_wide > 1)
1503 bsi->rdstat[i + 1][mode_idx].mvs[ref].as_int =
1504 mode_mv[this_mode][ref].as_int;
1505 if (num_4x4_blocks_high > 1)
1506 bsi->rdstat[i + 2][mode_idx].mvs[ref].as_int =
1507 mode_mv[this_mode][ref].as_int;
1510 // Trap vectors that reach beyond the UMV borders
1511 if (mv_check_bounds(x, &mode_mv[this_mode][0].as_mv) ||
1513 mv_check_bounds(x, &mode_mv[this_mode][1].as_mv)))
1516 if (filter_idx > 0) {
1517 BEST_SEG_INFO *ref_bsi = bsi_buf;
1521 for (ref = 0; ref < 1 + has_second_rf; ++ref) {
1522 subpelmv |= mv_has_subpel(&mode_mv[this_mode][ref].as_mv);
1523 have_ref &= mode_mv[this_mode][ref].as_int ==
1524 ref_bsi->rdstat[i][mode_idx].mvs[ref].as_int;
1527 if (filter_idx > 1 && !subpelmv && !have_ref) {
1528 ref_bsi = bsi_buf + 1;
1530 for (ref = 0; ref < 1 + has_second_rf; ++ref)
1531 have_ref &= mode_mv[this_mode][ref].as_int ==
1532 ref_bsi->rdstat[i][mode_idx].mvs[ref].as_int;
1535 if (!subpelmv && have_ref &&
1536 ref_bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
1537 vpx_memcpy(&bsi->rdstat[i][mode_idx], &ref_bsi->rdstat[i][mode_idx],
1538 sizeof(SEG_RDSTAT));
1539 if (num_4x4_blocks_wide > 1)
1540 bsi->rdstat[i + 1][mode_idx].eobs =
1541 ref_bsi->rdstat[i + 1][mode_idx].eobs;
1542 if (num_4x4_blocks_high > 1)
1543 bsi->rdstat[i + 2][mode_idx].eobs =
1544 ref_bsi->rdstat[i + 2][mode_idx].eobs;
1546 if (bsi->rdstat[i][mode_idx].brdcost < best_rd) {
1547 mode_selected = this_mode;
1548 best_rd = bsi->rdstat[i][mode_idx].brdcost;
1554 bsi->rdstat[i][mode_idx].brdcost =
1555 encode_inter_mb_segment(cpi, x,
1556 bsi->segment_rd - this_segment_rd, i,
1557 &bsi->rdstat[i][mode_idx].byrate,
1558 &bsi->rdstat[i][mode_idx].bdist,
1559 &bsi->rdstat[i][mode_idx].bsse,
1560 bsi->rdstat[i][mode_idx].ta,
1561 bsi->rdstat[i][mode_idx].tl,
1563 if (bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
1564 bsi->rdstat[i][mode_idx].brdcost += RDCOST(x->rdmult, x->rddiv,
1565 bsi->rdstat[i][mode_idx].brate, 0);
1566 bsi->rdstat[i][mode_idx].brate += bsi->rdstat[i][mode_idx].byrate;
1567 bsi->rdstat[i][mode_idx].eobs = p->eobs[i];
1568 if (num_4x4_blocks_wide > 1)
1569 bsi->rdstat[i + 1][mode_idx].eobs = p->eobs[i + 1];
1570 if (num_4x4_blocks_high > 1)
1571 bsi->rdstat[i + 2][mode_idx].eobs = p->eobs[i + 2];
1574 if (bsi->rdstat[i][mode_idx].brdcost < best_rd) {
1575 mode_selected = this_mode;
1576 best_rd = bsi->rdstat[i][mode_idx].brdcost;
1578 } /*for each 4x4 mode*/
1580 if (best_rd == INT64_MAX) {
1582 for (iy = i + 1; iy < 4; ++iy)
1583 for (midx = 0; midx < INTER_MODES; ++midx)
1584 bsi->rdstat[iy][midx].brdcost = INT64_MAX;
1585 bsi->segment_rd = INT64_MAX;
1589 mode_idx = INTER_OFFSET(mode_selected);
1590 vpx_memcpy(t_above, bsi->rdstat[i][mode_idx].ta, sizeof(t_above));
1591 vpx_memcpy(t_left, bsi->rdstat[i][mode_idx].tl, sizeof(t_left));
1593 set_and_cost_bmi_mvs(cpi, xd, i, mode_selected, mode_mv[mode_selected],
1594 frame_mv, seg_mvs[i], bsi->ref_mv, x->nmvjointcost,
1597 br += bsi->rdstat[i][mode_idx].brate;
1598 bd += bsi->rdstat[i][mode_idx].bdist;
1599 block_sse += bsi->rdstat[i][mode_idx].bsse;
1600 segmentyrate += bsi->rdstat[i][mode_idx].byrate;
1601 this_segment_rd += bsi->rdstat[i][mode_idx].brdcost;
1603 if (this_segment_rd > bsi->segment_rd) {
1605 for (iy = i + 1; iy < 4; ++iy)
1606 for (midx = 0; midx < INTER_MODES; ++midx)
1607 bsi->rdstat[iy][midx].brdcost = INT64_MAX;
1608 bsi->segment_rd = INT64_MAX;
1612 } /* for each label */
1616 bsi->segment_yrate = segmentyrate;
1617 bsi->segment_rd = this_segment_rd;
1618 bsi->sse = block_sse;
1620 // update the coding decisions
1621 for (k = 0; k < 4; ++k)
1622 bsi->modes[k] = mi->bmi[k].as_mode;
1624 if (bsi->segment_rd > best_rd)
1626 /* set it to the best */
1627 for (i = 0; i < 4; i++) {
1628 mode_idx = INTER_OFFSET(bsi->modes[i]);
1629 mi->bmi[i].as_mv[0].as_int = bsi->rdstat[i][mode_idx].mvs[0].as_int;
1630 if (has_second_ref(mbmi))
1631 mi->bmi[i].as_mv[1].as_int = bsi->rdstat[i][mode_idx].mvs[1].as_int;
1632 x->plane[0].eobs[i] = bsi->rdstat[i][mode_idx].eobs;
1633 mi->bmi[i].as_mode = bsi->modes[i];
1637 * used to set mbmi->mv.as_int
1639 *returntotrate = bsi->r;
1640 *returndistortion = bsi->d;
1641 *returnyrate = bsi->segment_yrate;
1642 *skippable = vp9_is_skippable_in_plane(x, BLOCK_8X8, 0);
1644 mbmi->mode = bsi->modes[3];
1646 return bsi->segment_rd;
1649 static void estimate_ref_frame_costs(const VP9_COMMON *cm,
1650 const MACROBLOCKD *xd,
1652 unsigned int *ref_costs_single,
1653 unsigned int *ref_costs_comp,
1654 vp9_prob *comp_mode_p) {
1655 int seg_ref_active = vp9_segfeature_active(&cm->seg, segment_id,
1657 if (seg_ref_active) {
1658 vpx_memset(ref_costs_single, 0, MAX_REF_FRAMES * sizeof(*ref_costs_single));
1659 vpx_memset(ref_costs_comp, 0, MAX_REF_FRAMES * sizeof(*ref_costs_comp));
1662 vp9_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
1663 vp9_prob comp_inter_p = 128;
1665 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
1666 comp_inter_p = vp9_get_reference_mode_prob(cm, xd);
1667 *comp_mode_p = comp_inter_p;
1672 ref_costs_single[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
1674 if (cm->reference_mode != COMPOUND_REFERENCE) {
1675 vp9_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
1676 vp9_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
1677 unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1);
1679 if (cm->reference_mode == REFERENCE_MODE_SELECT)
1680 base_cost += vp9_cost_bit(comp_inter_p, 0);
1682 ref_costs_single[LAST_FRAME] = ref_costs_single[GOLDEN_FRAME] =
1683 ref_costs_single[ALTREF_FRAME] = base_cost;
1684 ref_costs_single[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
1685 ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1686 ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1687 ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
1688 ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
1690 ref_costs_single[LAST_FRAME] = 512;
1691 ref_costs_single[GOLDEN_FRAME] = 512;
1692 ref_costs_single[ALTREF_FRAME] = 512;
1694 if (cm->reference_mode != SINGLE_REFERENCE) {
1695 vp9_prob ref_comp_p = vp9_get_pred_prob_comp_ref_p(cm, xd);
1696 unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1);
1698 if (cm->reference_mode == REFERENCE_MODE_SELECT)
1699 base_cost += vp9_cost_bit(comp_inter_p, 1);
1701 ref_costs_comp[LAST_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 0);
1702 ref_costs_comp[GOLDEN_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 1);
1704 ref_costs_comp[LAST_FRAME] = 512;
1705 ref_costs_comp[GOLDEN_FRAME] = 512;
1710 static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx,
1712 int64_t comp_pred_diff[REFERENCE_MODES],
1713 const int64_t tx_size_diff[TX_MODES],
1714 int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS],
1716 MACROBLOCKD *const xd = &x->e_mbd;
1718 // Take a snapshot of the coding context so it can be
1719 // restored if we decide to encode this way
1720 ctx->skip = x->skip;
1721 ctx->skippable = skippable;
1722 ctx->best_mode_index = mode_index;
1723 ctx->mic = *xd->mi[0].src_mi;
1724 ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_REFERENCE];
1725 ctx->comp_pred_diff = (int)comp_pred_diff[COMPOUND_REFERENCE];
1726 ctx->hybrid_pred_diff = (int)comp_pred_diff[REFERENCE_MODE_SELECT];
1728 vpx_memcpy(ctx->tx_rd_diff, tx_size_diff, sizeof(ctx->tx_rd_diff));
1729 vpx_memcpy(ctx->best_filter_diff, best_filter_diff,
1730 sizeof(*best_filter_diff) * SWITCHABLE_FILTER_CONTEXTS);
1733 static void setup_buffer_inter(VP9_COMP *cpi, MACROBLOCK *x,
1734 const TileInfo *const tile,
1735 MV_REFERENCE_FRAME ref_frame,
1736 BLOCK_SIZE block_size,
1737 int mi_row, int mi_col,
1738 int_mv frame_nearest_mv[MAX_REF_FRAMES],
1739 int_mv frame_near_mv[MAX_REF_FRAMES],
1740 struct buf_2d yv12_mb[4][MAX_MB_PLANE]) {
1741 const VP9_COMMON *cm = &cpi->common;
1742 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1743 MACROBLOCKD *const xd = &x->e_mbd;
1744 MODE_INFO *const mi = xd->mi[0].src_mi;
1745 int_mv *const candidates = mi->mbmi.ref_mvs[ref_frame];
1746 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
1748 // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this
1749 // use the UV scaling factors.
1750 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col, sf, sf);
1752 // Gets an initial list of candidate vectors from neighbours and orders them
1753 vp9_find_mv_refs(cm, xd, tile, mi, ref_frame, candidates, mi_row, mi_col);
1755 // Candidate refinement carried out at encoder and decoder
1756 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1757 &frame_nearest_mv[ref_frame],
1758 &frame_near_mv[ref_frame]);
1760 // Further refinement that is encode side only to test the top few candidates
1761 // in full and choose the best as the centre point for subsequent searches.
1762 // The current implementation doesn't support scaling.
1763 if (!vp9_is_scaled(sf) && block_size >= BLOCK_8X8)
1764 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
1765 ref_frame, block_size);
1768 static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
1770 int mi_row, int mi_col,
1771 int_mv *tmp_mv, int *rate_mv) {
1772 MACROBLOCKD *xd = &x->e_mbd;
1773 const VP9_COMMON *cm = &cpi->common;
1774 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
1775 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
1776 int bestsme = INT_MAX;
1778 int sadpb = x->sadperbit16;
1780 int ref = mbmi->ref_frame[0];
1781 MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
1783 int tmp_col_min = x->mv_col_min;
1784 int tmp_col_max = x->mv_col_max;
1785 int tmp_row_min = x->mv_row_min;
1786 int tmp_row_max = x->mv_row_max;
1789 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
1793 pred_mv[0] = mbmi->ref_mvs[ref][0].as_mv;
1794 pred_mv[1] = mbmi->ref_mvs[ref][1].as_mv;
1795 pred_mv[2] = x->pred_mv[ref];
1797 if (scaled_ref_frame) {
1799 // Swap out the reference frame for a version that's been scaled to
1800 // match the resolution of the current frame, allowing the existing
1801 // motion search code to be used without additional modifications.
1802 for (i = 0; i < MAX_MB_PLANE; i++)
1803 backup_yv12[i] = xd->plane[i].pre[0];
1805 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
1808 vp9_set_mv_search_range(x, &ref_mv);
1810 // Work out the size of the first step in the mv step search.
1811 // 0 here is maximum length first step. 1 is MAX >> 1 etc.
1812 if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) {
1813 // Take wtd average of the step_params based on the last frame's
1814 // max mv magnitude and that based on the best ref mvs of the current
1815 // block for the given reference.
1816 step_param = (vp9_init_search_range(x->max_mv_context[ref]) +
1817 cpi->mv_step_param) / 2;
1819 step_param = cpi->mv_step_param;
1822 if (cpi->sf.adaptive_motion_search && bsize < BLOCK_64X64) {
1823 int boffset = 2 * (b_width_log2(BLOCK_64X64) - MIN(b_height_log2(bsize),
1824 b_width_log2(bsize)));
1825 step_param = MAX(step_param, boffset);
1828 if (cpi->sf.adaptive_motion_search) {
1829 int bwl = b_width_log2(bsize);
1830 int bhl = b_height_log2(bsize);
1832 int tlevel = x->pred_mv_sad[ref] >> (bwl + bhl + 4);
1837 for (i = LAST_FRAME; i <= ALTREF_FRAME && cm->show_frame; ++i) {
1838 if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) {
1839 x->pred_mv[ref].row = 0;
1840 x->pred_mv[ref].col = 0;
1841 tmp_mv->as_int = INVALID_MV;
1843 if (scaled_ref_frame) {
1845 for (i = 0; i < MAX_MB_PLANE; i++)
1846 xd->plane[i].pre[0] = backup_yv12[i];
1853 mvp_full = pred_mv[x->mv_best_ref_index[ref]];
1858 bestsme = vp9_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
1859 cond_sad_list(cpi, sad_list),
1860 &ref_mv, &tmp_mv->as_mv, INT_MAX, 1);
1862 x->mv_col_min = tmp_col_min;
1863 x->mv_col_max = tmp_col_max;
1864 x->mv_row_min = tmp_row_min;
1865 x->mv_row_max = tmp_row_max;
1867 if (bestsme < INT_MAX) {
1868 int dis; /* TODO: use dis in distortion calculation later. */
1869 cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv,
1870 cm->allow_high_precision_mv,
1872 &cpi->fn_ptr[bsize],
1873 cpi->sf.mv.subpel_force_stop,
1874 cpi->sf.mv.subpel_iters_per_step,
1875 cond_sad_list(cpi, sad_list),
1876 x->nmvjointcost, x->mvcost,
1877 &dis, &x->pred_sse[ref], NULL, 0, 0);
1879 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv,
1880 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
1882 if (cpi->sf.adaptive_motion_search)
1883 x->pred_mv[ref] = tmp_mv->as_mv;
1885 if (scaled_ref_frame) {
1887 for (i = 0; i < MAX_MB_PLANE; i++)
1888 xd->plane[i].pre[0] = backup_yv12[i];
1892 static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
1895 int mi_row, int mi_col,
1896 int_mv single_newmv[MAX_REF_FRAMES],
1898 const int pw = 4 * num_4x4_blocks_wide_lookup[bsize];
1899 const int ph = 4 * num_4x4_blocks_high_lookup[bsize];
1900 MACROBLOCKD *xd = &x->e_mbd;
1901 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
1902 const int refs[2] = { mbmi->ref_frame[0],
1903 mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] };
1906 // Prediction buffer from second frame.
1907 uint8_t *second_pred = vpx_memalign(16, pw * ph * sizeof(uint8_t));
1908 const InterpKernel *kernel = vp9_get_interp_kernel(mbmi->interp_filter);
1910 // Do joint motion search in compound mode to get more accurate mv.
1911 struct buf_2d backup_yv12[2][MAX_MB_PLANE];
1912 struct buf_2d scaled_first_yv12 = xd->plane[0].pre[0];
1913 int last_besterr[2] = {INT_MAX, INT_MAX};
1914 const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
1915 vp9_get_scaled_ref_frame(cpi, mbmi->ref_frame[0]),
1916 vp9_get_scaled_ref_frame(cpi, mbmi->ref_frame[1])
1919 for (ref = 0; ref < 2; ++ref) {
1920 ref_mv[ref] = mbmi->ref_mvs[refs[ref]][0];
1922 if (scaled_ref_frame[ref]) {
1924 // Swap out the reference frame for a version that's been scaled to
1925 // match the resolution of the current frame, allowing the existing
1926 // motion search code to be used without additional modifications.
1927 for (i = 0; i < MAX_MB_PLANE; i++)
1928 backup_yv12[ref][i] = xd->plane[i].pre[ref];
1929 vp9_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
1933 frame_mv[refs[ref]].as_int = single_newmv[refs[ref]].as_int;
1936 // Allow joint search multiple times iteratively for each ref frame
1937 // and break out the search loop if it couldn't find better mv.
1938 for (ite = 0; ite < 4; ite++) {
1939 struct buf_2d ref_yv12[2];
1940 int bestsme = INT_MAX;
1941 int sadpb = x->sadperbit16;
1943 int search_range = 3;
1945 int tmp_col_min = x->mv_col_min;
1946 int tmp_col_max = x->mv_col_max;
1947 int tmp_row_min = x->mv_row_min;
1948 int tmp_row_max = x->mv_row_max;
1951 // Initialized here because of compiler problem in Visual Studio.
1952 ref_yv12[0] = xd->plane[0].pre[0];
1953 ref_yv12[1] = xd->plane[0].pre[1];
1955 // Get pred block from second frame.
1956 vp9_build_inter_predictor(ref_yv12[!id].buf,
1957 ref_yv12[!id].stride,
1959 &frame_mv[refs[!id]].as_mv,
1960 &xd->block_refs[!id]->sf,
1962 kernel, MV_PRECISION_Q3,
1963 mi_col * MI_SIZE, mi_row * MI_SIZE);
1965 // Compound motion search on first ref frame.
1967 xd->plane[0].pre[0] = ref_yv12[id];
1968 vp9_set_mv_search_range(x, &ref_mv[id].as_mv);
1970 // Use mv result from single mode as mvp.
1971 tmp_mv = frame_mv[refs[id]].as_mv;
1976 // Small-range full-pixel motion search
1977 bestsme = vp9_refining_search_8p_c(x, &tmp_mv, sadpb,
1979 &cpi->fn_ptr[bsize],
1980 &ref_mv[id].as_mv, second_pred);
1981 if (bestsme < INT_MAX)
1982 bestsme = vp9_get_mvpred_av_var(x, &tmp_mv, &ref_mv[id].as_mv,
1983 second_pred, &cpi->fn_ptr[bsize], 1);
1985 x->mv_col_min = tmp_col_min;
1986 x->mv_col_max = tmp_col_max;
1987 x->mv_row_min = tmp_row_min;
1988 x->mv_row_max = tmp_row_max;
1990 if (bestsme < INT_MAX) {
1991 int dis; /* TODO: use dis in distortion calculation later. */
1993 bestsme = cpi->find_fractional_mv_step(
1996 cpi->common.allow_high_precision_mv,
1998 &cpi->fn_ptr[bsize],
1999 0, cpi->sf.mv.subpel_iters_per_step,
2001 x->nmvjointcost, x->mvcost,
2002 &dis, &sse, second_pred,
2007 xd->plane[0].pre[0] = scaled_first_yv12;
2009 if (bestsme < last_besterr[id]) {
2010 frame_mv[refs[id]].as_mv = tmp_mv;
2011 last_besterr[id] = bestsme;
2019 for (ref = 0; ref < 2; ++ref) {
2020 if (scaled_ref_frame[ref]) {
2021 // restore the predictor
2023 for (i = 0; i < MAX_MB_PLANE; i++)
2024 xd->plane[i].pre[ref] = backup_yv12[ref][i];
2027 *rate_mv += vp9_mv_bit_cost(&frame_mv[refs[ref]].as_mv,
2028 &mbmi->ref_mvs[refs[ref]][0].as_mv,
2029 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
2032 vpx_free(second_pred);
2035 static INLINE void restore_dst_buf(MACROBLOCKD *xd,
2036 uint8_t *orig_dst[MAX_MB_PLANE],
2037 int orig_dst_stride[MAX_MB_PLANE]) {
2039 for (i = 0; i < MAX_MB_PLANE; i++) {
2040 xd->plane[i].dst.buf = orig_dst[i];
2041 xd->plane[i].dst.stride = orig_dst_stride[i];
2045 static void rd_encode_breakout_test(VP9_COMP *cpi, MACROBLOCK *x,
2046 BLOCK_SIZE bsize, int *rate2,
2047 int64_t *distortion, int64_t *distortion_uv,
2048 int *disable_skip) {
2049 VP9_COMMON *cm = &cpi->common;
2050 MACROBLOCKD *xd = &x->e_mbd;
2051 const BLOCK_SIZE y_size = get_plane_block_size(bsize, &xd->plane[0]);
2052 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
2053 unsigned int var, sse;
2054 // Skipping threshold for ac.
2055 unsigned int thresh_ac;
2056 // Skipping threshold for dc
2057 unsigned int thresh_dc;
2059 var = cpi->fn_ptr[y_size].vf(x->plane[0].src.buf, x->plane[0].src.stride,
2060 xd->plane[0].dst.buf,
2061 xd->plane[0].dst.stride, &sse);
2063 if (x->encode_breakout > 0) {
2064 // Set a maximum for threshold to avoid big PSNR loss in low bitrate
2065 // case. Use extreme low threshold for static frames to limit skipping.
2066 const unsigned int max_thresh = (cpi->allow_encode_breakout ==
2067 ENCODE_BREAKOUT_LIMITED) ? 128 : 36000;
2068 // The encode_breakout input
2069 const unsigned int min_thresh =
2070 MIN(((unsigned int)x->encode_breakout << 4), max_thresh);
2072 // Calculate threshold according to dequant value.
2073 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) / 9;
2074 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
2076 // Adjust threshold according to partition size.
2077 thresh_ac >>= 8 - (b_width_log2(bsize) +
2078 b_height_log2(bsize));
2079 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
2085 // Y skipping condition checking
2086 if (sse < thresh_ac || sse == 0) {
2087 // dc skipping checking
2088 if ((sse - var) < thresh_dc || sse == var) {
2089 unsigned int sse_u, sse_v;
2090 unsigned int var_u, var_v;
2092 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
2093 x->plane[1].src.stride,
2094 xd->plane[1].dst.buf,
2095 xd->plane[1].dst.stride, &sse_u);
2097 // U skipping condition checking
2098 if ((sse_u * 4 < thresh_ac || sse_u == 0) &&
2099 (sse_u - var_u < thresh_dc || sse_u == var_u)) {
2100 var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf,
2101 x->plane[2].src.stride,
2102 xd->plane[2].dst.buf,
2103 xd->plane[2].dst.stride, &sse_v);
2105 // V skipping condition checking
2106 if ((sse_v * 4 < thresh_ac || sse_v == 0) &&
2107 (sse_v - var_v < thresh_dc || sse_v == var_v)) {
2110 // The cost of skip bit needs to be added.
2111 *rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2113 // Scaling factor for SSE from spatial domain to frequency domain
2114 // is 16. Adjust distortion accordingly.
2115 *distortion_uv = (sse_u + sse_v) << 4;
2116 *distortion = (sse << 4) + *distortion_uv;
2125 static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
2127 int64_t txfm_cache[],
2128 int *rate2, int64_t *distortion,
2130 int *rate_y, int64_t *distortion_y,
2131 int *rate_uv, int64_t *distortion_uv,
2133 int_mv (*mode_mv)[MAX_REF_FRAMES],
2134 int mi_row, int mi_col,
2135 int_mv single_newmv[MAX_REF_FRAMES],
2136 INTERP_FILTER (*single_filter)[MAX_REF_FRAMES],
2137 int (*single_skippable)[MAX_REF_FRAMES],
2139 const int64_t ref_best_rd) {
2140 VP9_COMMON *cm = &cpi->common;
2141 RD_OPT *rd_opt = &cpi->rd;
2142 MACROBLOCKD *xd = &x->e_mbd;
2143 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
2144 const int is_comp_pred = has_second_ref(mbmi);
2145 const int this_mode = mbmi->mode;
2146 int_mv *frame_mv = mode_mv[this_mode];
2148 int refs[2] = { mbmi->ref_frame[0],
2149 (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) };
2151 int64_t this_rd = 0;
2152 DECLARE_ALIGNED_ARRAY(16, uint8_t, tmp_buf, MAX_MB_PLANE * 64 * 64);
2153 int pred_exists = 0;
2155 int64_t rd, tmp_rd, best_rd = INT64_MAX;
2156 int best_needs_copy = 0;
2157 uint8_t *orig_dst[MAX_MB_PLANE];
2158 int orig_dst_stride[MAX_MB_PLANE];
2160 INTERP_FILTER best_filter = SWITCHABLE;
2161 uint8_t skip_txfm[MAX_MB_PLANE << 2] = {0};
2162 int64_t bsse[MAX_MB_PLANE << 2] = {0};
2164 int bsl = mi_width_log2_lookup[bsize];
2165 int pred_filter_search = cpi->sf.cb_pred_filter_search ?
2166 (((mi_row + mi_col) >> bsl) +
2167 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0;
2169 if (pred_filter_search) {
2170 INTERP_FILTER af = SWITCHABLE, lf = SWITCHABLE;
2171 if (xd->up_available)
2172 af = xd->mi[-xd->mi_stride].src_mi->mbmi.interp_filter;
2173 if (xd->left_available)
2174 lf = xd->mi[-1].src_mi->mbmi.interp_filter;
2176 if ((this_mode != NEWMV) || (af == lf))
2181 if (frame_mv[refs[0]].as_int == INVALID_MV ||
2182 frame_mv[refs[1]].as_int == INVALID_MV)
2185 if (cpi->sf.adaptive_mode_search) {
2186 if (single_filter[this_mode][refs[0]] ==
2187 single_filter[this_mode][refs[1]])
2188 best_filter = single_filter[this_mode][refs[0]];
2192 if (this_mode == NEWMV) {
2195 // Initialize mv using single prediction mode result.
2196 frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int;
2197 frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int;
2199 if (cpi->sf.comp_inter_joint_search_thresh <= bsize) {
2200 joint_motion_search(cpi, x, bsize, frame_mv,
2201 mi_row, mi_col, single_newmv, &rate_mv);
2203 rate_mv = vp9_mv_bit_cost(&frame_mv[refs[0]].as_mv,
2204 &mbmi->ref_mvs[refs[0]][0].as_mv,
2205 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
2206 rate_mv += vp9_mv_bit_cost(&frame_mv[refs[1]].as_mv,
2207 &mbmi->ref_mvs[refs[1]][0].as_mv,
2208 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
2213 single_motion_search(cpi, x, bsize, mi_row, mi_col,
2215 if (tmp_mv.as_int == INVALID_MV)
2218 frame_mv[refs[0]].as_int =
2219 xd->mi[0].src_mi->bmi[0].as_mv[0].as_int = tmp_mv.as_int;
2220 single_newmv[refs[0]].as_int = tmp_mv.as_int;
2224 for (i = 0; i < is_comp_pred + 1; ++i) {
2225 cur_mv[i] = frame_mv[refs[i]];
2226 // Clip "next_nearest" so that it does not extend to far out of image
2227 if (this_mode != NEWMV)
2228 clamp_mv2(&cur_mv[i].as_mv, xd);
2230 if (mv_check_bounds(x, &cur_mv[i].as_mv))
2232 mbmi->mv[i].as_int = cur_mv[i].as_int;
2235 // do first prediction into the destination buffer. Do the next
2236 // prediction into a temporary buffer. Then keep track of which one
2237 // of these currently holds the best predictor, and use the other
2238 // one for future predictions. In the end, copy from tmp_buf to
2239 // dst if necessary.
2240 for (i = 0; i < MAX_MB_PLANE; i++) {
2241 orig_dst[i] = xd->plane[i].dst.buf;
2242 orig_dst_stride[i] = xd->plane[i].dst.stride;
2245 /* We don't include the cost of the second reference here, because there
2246 * are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other
2247 * words if you present them in that order, the second one is always known
2248 * if the first is known */
2249 *rate2 += cost_mv_ref(cpi, this_mode, mbmi->mode_context[refs[0]]);
2251 if (RDCOST(x->rdmult, x->rddiv, *rate2, 0) > ref_best_rd &&
2252 mbmi->mode != NEARESTMV)
2256 // Are all MVs integer pel for Y and UV
2257 intpel_mv = !mv_has_subpel(&mbmi->mv[0].as_mv);
2259 intpel_mv &= !mv_has_subpel(&mbmi->mv[1].as_mv);
2261 // Search for best switchable filter by checking the variance of
2262 // pred error irrespective of whether the filter will be used
2263 rd_opt->mask_filter = 0;
2264 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
2265 rd_opt->filter_cache[i] = INT64_MAX;
2267 if (cm->interp_filter != BILINEAR) {
2268 if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) {
2269 best_filter = EIGHTTAP;
2270 } else if (best_filter == SWITCHABLE) {
2272 int tmp_rate_sum = 0;
2273 int64_t tmp_dist_sum = 0;
2275 for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
2278 mbmi->interp_filter = i;
2279 rs = vp9_get_switchable_rate(cpi);
2280 rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0);
2282 if (i > 0 && intpel_mv) {
2283 rd = RDCOST(x->rdmult, x->rddiv, tmp_rate_sum, tmp_dist_sum);
2284 rd_opt->filter_cache[i] = rd;
2285 rd_opt->filter_cache[SWITCHABLE_FILTERS] =
2286 MIN(rd_opt->filter_cache[SWITCHABLE_FILTERS], rd + rs_rd);
2287 if (cm->interp_filter == SWITCHABLE)
2289 rd_opt->mask_filter = MAX(rd_opt->mask_filter, rd);
2292 int64_t dist_sum = 0;
2293 if (i > 0 && cpi->sf.adaptive_interp_filter_search &&
2294 (cpi->sf.interp_filter_search_mask & (1 << i))) {
2296 dist_sum = INT64_MAX;
2300 if ((cm->interp_filter == SWITCHABLE &&
2301 (!i || best_needs_copy)) ||
2302 (cm->interp_filter != SWITCHABLE &&
2303 (cm->interp_filter == mbmi->interp_filter ||
2304 (i == 0 && intpel_mv)))) {
2305 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2307 for (j = 0; j < MAX_MB_PLANE; j++) {
2308 xd->plane[j].dst.buf = tmp_buf + j * 64 * 64;
2309 xd->plane[j].dst.stride = 64;
2312 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
2313 model_rd_for_sb(cpi, bsize, x, xd, &rate_sum, &dist_sum);
2315 rd = RDCOST(x->rdmult, x->rddiv, rate_sum, dist_sum);
2316 rd_opt->filter_cache[i] = rd;
2317 rd_opt->filter_cache[SWITCHABLE_FILTERS] =
2318 MIN(rd_opt->filter_cache[SWITCHABLE_FILTERS], rd + rs_rd);
2319 if (cm->interp_filter == SWITCHABLE)
2321 rd_opt->mask_filter = MAX(rd_opt->mask_filter, rd);
2323 if (i == 0 && intpel_mv) {
2324 tmp_rate_sum = rate_sum;
2325 tmp_dist_sum = dist_sum;
2329 if (i == 0 && cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
2330 if (rd / 2 > ref_best_rd) {
2331 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2335 newbest = i == 0 || rd < best_rd;
2339 best_filter = mbmi->interp_filter;
2340 if (cm->interp_filter == SWITCHABLE && i && !intpel_mv)
2341 best_needs_copy = !best_needs_copy;
2342 vpx_memcpy(skip_txfm, x->skip_txfm, sizeof(skip_txfm));
2343 vpx_memcpy(bsse, x->bsse, sizeof(bsse));
2346 if ((cm->interp_filter == SWITCHABLE && newbest) ||
2347 (cm->interp_filter != SWITCHABLE &&
2348 cm->interp_filter == mbmi->interp_filter)) {
2353 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2356 // Set the appropriate filter
2357 mbmi->interp_filter = cm->interp_filter != SWITCHABLE ?
2358 cm->interp_filter : best_filter;
2359 rs = cm->interp_filter == SWITCHABLE ? vp9_get_switchable_rate(cpi) : 0;
2362 if (best_needs_copy) {
2363 // again temporarily set the buffers to local memory to prevent a memcpy
2364 for (i = 0; i < MAX_MB_PLANE; i++) {
2365 xd->plane[i].dst.buf = tmp_buf + i * 64 * 64;
2366 xd->plane[i].dst.stride = 64;
2369 rd = tmp_rd + RDCOST(x->rdmult, x->rddiv, rs, 0);
2373 // Handles the special case when a filter that is not in the
2374 // switchable list (ex. bilinear) is indicated at the frame level, or
2375 // skip condition holds.
2376 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
2377 model_rd_for_sb(cpi, bsize, x, xd, &tmp_rate, &tmp_dist);
2378 rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate, tmp_dist);
2379 vpx_memcpy(skip_txfm, x->skip_txfm, sizeof(skip_txfm));
2380 vpx_memcpy(bsse, x->bsse, sizeof(bsse));
2384 single_filter[this_mode][refs[0]] = mbmi->interp_filter;
2386 if (cpi->sf.adaptive_mode_search)
2388 if (single_skippable[this_mode][refs[0]] &&
2389 single_skippable[this_mode][refs[1]])
2390 vpx_memset(skip_txfm, 1, sizeof(skip_txfm));
2392 if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) {
2393 // if current pred_error modeled rd is substantially more than the best
2394 // so far, do not bother doing full rd
2395 if (rd / 2 > ref_best_rd) {
2396 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2401 if (cm->interp_filter == SWITCHABLE)
2404 if (!is_comp_pred) {
2405 if (cpi->allow_encode_breakout)
2406 rd_encode_breakout_test(cpi, x, bsize, rate2, distortion, distortion_uv,
2410 vpx_memcpy(x->skip_txfm, skip_txfm, sizeof(skip_txfm));
2411 vpx_memcpy(x->bsse, bsse, sizeof(bsse));
2414 int skippable_y, skippable_uv;
2415 int64_t sseuv = INT64_MAX;
2416 int64_t rdcosty = INT64_MAX;
2418 // Y cost and distortion
2419 vp9_subtract_plane(x, bsize, 0);
2420 super_block_yrd(cpi, x, rate_y, distortion_y, &skippable_y, psse,
2421 bsize, txfm_cache, ref_best_rd);
2423 if (*rate_y == INT_MAX) {
2425 *distortion = INT64_MAX;
2426 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2431 *distortion += *distortion_y;
2433 rdcosty = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion);
2434 rdcosty = MIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse));
2436 super_block_uvrd(cpi, x, rate_uv, distortion_uv, &skippable_uv, &sseuv,
2437 bsize, ref_best_rd - rdcosty);
2438 if (*rate_uv == INT_MAX) {
2440 *distortion = INT64_MAX;
2441 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2447 *distortion += *distortion_uv;
2448 *skippable = skippable_y && skippable_uv;
2452 single_skippable[this_mode][refs[0]] = *skippable;
2454 restore_dst_buf(xd, orig_dst, orig_dst_stride);
2455 return this_rd; // if 0, this will be re-calculated by caller
2458 void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
2459 int *returnrate, int64_t *returndist,
2461 PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
2462 VP9_COMMON *const cm = &cpi->common;
2463 MACROBLOCKD *const xd = &x->e_mbd;
2464 struct macroblockd_plane *const pd = xd->plane;
2465 int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0;
2466 int y_skip = 0, uv_skip = 0;
2467 int64_t dist_y = 0, dist_uv = 0, tx_cache[TX_MODES] = { 0 };
2468 TX_SIZE max_uv_tx_size;
2471 xd->mi[0].src_mi->mbmi.ref_frame[0] = INTRA_FRAME;
2473 if (bsize >= BLOCK_8X8) {
2474 if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly,
2475 &dist_y, &y_skip, bsize, tx_cache,
2476 best_rd) >= best_rd) {
2477 *returnrate = INT_MAX;
2480 max_uv_tx_size = get_uv_tx_size_impl(xd->mi[0].src_mi->mbmi.tx_size, bsize,
2481 pd[1].subsampling_x,
2482 pd[1].subsampling_y);
2483 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly,
2484 &dist_uv, &uv_skip, bsize, max_uv_tx_size);
2487 if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly,
2488 &dist_y, best_rd) >= best_rd) {
2489 *returnrate = INT_MAX;
2492 max_uv_tx_size = get_uv_tx_size_impl(xd->mi[0].src_mi->mbmi.tx_size, bsize,
2493 pd[1].subsampling_x,
2494 pd[1].subsampling_y);
2495 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly,
2496 &dist_uv, &uv_skip, BLOCK_8X8, max_uv_tx_size);
2499 if (y_skip && uv_skip) {
2500 *returnrate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly +
2501 vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2502 *returndist = dist_y + dist_uv;
2503 vp9_zero(ctx->tx_rd_diff);
2506 *returnrate = rate_y + rate_uv + vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
2507 *returndist = dist_y + dist_uv;
2508 if (cpi->sf.tx_size_search_method == USE_FULL_RD)
2509 for (i = 0; i < TX_MODES; i++) {
2510 if (tx_cache[i] < INT64_MAX && tx_cache[cm->tx_mode] < INT64_MAX)
2511 ctx->tx_rd_diff[i] = tx_cache[i] - tx_cache[cm->tx_mode];
2513 ctx->tx_rd_diff[i] = 0;
2517 ctx->mic = *xd->mi[0].src_mi;
2520 // Updating rd_thresh_freq_fact[] here means that the different
2521 // partition/block sizes are handled independently based on the best
2522 // choice for the current partition. It may well be better to keep a scaled
2523 // best rd so far value and update rd_thresh_freq_fact based on the mode/size
2524 // combination that wins out.
2525 static void update_rd_thresh_fact(VP9_COMP *cpi, int bsize,
2526 int best_mode_index) {
2527 if (cpi->sf.adaptive_rd_thresh > 0) {
2528 const int top_mode = bsize < BLOCK_8X8 ? MAX_REFS : MAX_MODES;
2530 for (mode = 0; mode < top_mode; ++mode) {
2531 int *const fact = &cpi->rd.thresh_freq_fact[bsize][mode];
2533 if (mode == best_mode_index) {
2534 *fact -= (*fact >> 3);
2536 *fact = MIN(*fact + RD_THRESH_INC,
2537 cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
2543 int64_t vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x,
2544 const TileInfo *const tile,
2545 int mi_row, int mi_col,
2547 int64_t *returndistortion,
2549 PICK_MODE_CONTEXT *ctx,
2550 int64_t best_rd_so_far) {
2551 VP9_COMMON *const cm = &cpi->common;
2552 RD_OPT *const rd_opt = &cpi->rd;
2553 MACROBLOCKD *const xd = &x->e_mbd;
2554 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
2555 const struct segmentation *const seg = &cm->seg;
2556 struct macroblockd_plane *const pd = xd->plane;
2557 PREDICTION_MODE this_mode;
2558 MV_REFERENCE_FRAME ref_frame, second_ref_frame;
2559 unsigned char segment_id = mbmi->segment_id;
2560 int comp_pred, i, k;
2561 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
2562 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
2563 int_mv single_newmv[MAX_REF_FRAMES] = { { 0 } };
2564 INTERP_FILTER single_inter_filter[MB_MODE_COUNT][MAX_REF_FRAMES];
2565 int single_skippable[MB_MODE_COUNT][MAX_REF_FRAMES];
2566 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
2568 int64_t best_rd = best_rd_so_far;
2569 int64_t best_tx_rd[TX_MODES];
2570 int64_t best_tx_diff[TX_MODES];
2571 int64_t best_pred_diff[REFERENCE_MODES];
2572 int64_t best_pred_rd[REFERENCE_MODES];
2573 int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS];
2574 int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
2575 MB_MODE_INFO best_mbmode;
2576 int best_mode_skippable = 0;
2577 int midx, best_mode_index = -1;
2578 unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
2579 vp9_prob comp_mode_p;
2580 int64_t best_intra_rd = INT64_MAX;
2581 unsigned int best_pred_sse = UINT_MAX;
2582 PREDICTION_MODE best_intra_mode = DC_PRED;
2583 int rate_uv_intra[TX_SIZES], rate_uv_tokenonly[TX_SIZES];
2584 int64_t dist_uv[TX_SIZES];
2585 int skip_uv[TX_SIZES];
2586 PREDICTION_MODE mode_uv[TX_SIZES];
2587 const int intra_cost_penalty =
2588 20 * vp9_dc_quant(cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth);
2590 uint8_t ref_frame_skip_mask[2] = { 0 };
2591 uint16_t mode_skip_mask[MAX_REF_FRAMES] = { 0 };
2592 int mode_skip_start = cpi->sf.mode_skip_start + 1;
2593 const int *const rd_threshes = rd_opt->threshes[segment_id][bsize];
2594 const int *const rd_thresh_freq_fact = rd_opt->thresh_freq_fact[bsize];
2595 int mode_threshold[MAX_MODES];
2596 int *mode_map = rd_opt->mode_map[bsize];
2597 const int mode_search_skip_flags = cpi->sf.mode_search_skip_flags;
2598 vp9_zero(best_mbmode);
2600 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
2602 estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
2605 for (i = 0; i < REFERENCE_MODES; ++i)
2606 best_pred_rd[i] = INT64_MAX;
2607 for (i = 0; i < TX_MODES; i++)
2608 best_tx_rd[i] = INT64_MAX;
2609 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
2610 best_filter_rd[i] = INT64_MAX;
2611 for (i = 0; i < TX_SIZES; i++)
2612 rate_uv_intra[i] = INT_MAX;
2613 for (i = 0; i < MAX_REF_FRAMES; ++i)
2614 x->pred_sse[i] = INT_MAX;
2615 for (i = 0; i < MB_MODE_COUNT; ++i) {
2616 for (k = 0; k < MAX_REF_FRAMES; ++k) {
2617 single_inter_filter[i][k] = SWITCHABLE;
2618 single_skippable[i][k] = 0;
2622 *returnrate = INT_MAX;
2624 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2625 x->pred_mv_sad[ref_frame] = INT_MAX;
2626 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
2627 setup_buffer_inter(cpi, x, tile, ref_frame, bsize, mi_row, mi_col,
2628 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
2630 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
2631 frame_mv[ZEROMV][ref_frame].as_int = 0;
2634 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2635 if (!(cpi->ref_frame_flags & flag_list[ref_frame])) {
2636 // Skip checking missing references in both single and compound reference
2637 // modes. Note that a mode will be skipped iff both reference frames
2639 ref_frame_skip_mask[0] |= (1 << ref_frame);
2640 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
2641 } else if (cpi->sf.reference_masking) {
2642 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2643 // Skip fixed mv modes for poor references
2644 if ((x->pred_mv_sad[ref_frame] >> 2) > x->pred_mv_sad[i]) {
2645 mode_skip_mask[ref_frame] |= INTER_NEAREST_NEAR_ZERO;
2650 // If the segment reference frame feature is enabled....
2651 // then do nothing if the current ref frame is not allowed..
2652 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
2653 vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
2654 ref_frame_skip_mask[0] |= (1 << ref_frame);
2655 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
2659 // Disable this drop out case if the ref frame
2660 // segment level feature is enabled for this segment. This is to
2661 // prevent the possibility that we end up unable to pick any mode.
2662 if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
2663 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
2664 // unless ARNR filtering is enabled in which case we want
2665 // an unfiltered alternative. We allow near/nearest as well
2666 // because they may result in zero-zero MVs but be cheaper.
2667 if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
2668 ref_frame_skip_mask[0] = (1 << LAST_FRAME) | (1 << GOLDEN_FRAME);
2669 ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
2670 mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO;
2671 if (frame_mv[NEARMV][ALTREF_FRAME].as_int != 0)
2672 mode_skip_mask[ALTREF_FRAME] |= (1 << NEARMV);
2673 if (frame_mv[NEARESTMV][ALTREF_FRAME].as_int != 0)
2674 mode_skip_mask[ALTREF_FRAME] |= (1 << NEARESTMV);
2678 if (cpi->rc.is_src_frame_alt_ref) {
2679 if (cpi->sf.alt_ref_search_fp) {
2680 mode_skip_mask[ALTREF_FRAME] = 0;
2681 ref_frame_skip_mask[0] = ~(1 << ALTREF_FRAME);
2682 ref_frame_skip_mask[1] = SECOND_REF_FRAME_MASK;
2686 if (bsize > cpi->sf.max_intra_bsize) {
2687 ref_frame_skip_mask[0] |= (1 << INTRA_FRAME);
2688 ref_frame_skip_mask[1] |= (1 << INTRA_FRAME);
2691 mode_skip_mask[INTRA_FRAME] |=
2692 ~(cpi->sf.intra_y_mode_mask[max_txsize_lookup[bsize]]);
2694 for (i = 0; i < MAX_MODES; ++i)
2695 mode_threshold[i] = ((int64_t)rd_threshes[i] * rd_thresh_freq_fact[i]) >> 5;
2697 midx = cpi->sf.schedule_mode_search ? mode_skip_start : 0;
2699 uint8_t end_pos = 0;
2700 for (i = 5; i < midx; ++i) {
2701 if (mode_threshold[mode_map[i - 1]] > mode_threshold[mode_map[i]]) {
2702 uint8_t tmp = mode_map[i];
2703 mode_map[i] = mode_map[i - 1];
2704 mode_map[i - 1] = tmp;
2711 for (midx = 0; midx < MAX_MODES; ++midx) {
2712 int mode_index = mode_map[midx];
2713 int mode_excluded = 0;
2714 int64_t this_rd = INT64_MAX;
2715 int disable_skip = 0;
2716 int compmode_cost = 0;
2717 int rate2 = 0, rate_y = 0, rate_uv = 0;
2718 int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
2720 int64_t tx_cache[TX_MODES];
2722 int64_t total_sse = INT64_MAX;
2725 this_mode = vp9_mode_order[mode_index].mode;
2726 ref_frame = vp9_mode_order[mode_index].ref_frame[0];
2727 second_ref_frame = vp9_mode_order[mode_index].ref_frame[1];
2729 // Look at the reference frame of the best mode so far and set the
2730 // skip mask to look at a subset of the remaining modes.
2731 if (midx == mode_skip_start && best_mode_index >= 0) {
2732 switch (best_mbmode.ref_frame[0]) {
2736 ref_frame_skip_mask[0] |= LAST_FRAME_MODE_MASK;
2737 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
2740 ref_frame_skip_mask[0] |= GOLDEN_FRAME_MODE_MASK;
2741 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
2744 ref_frame_skip_mask[0] |= ALT_REF_MODE_MASK;
2747 case MAX_REF_FRAMES:
2748 assert(0 && "Invalid Reference frame");
2753 if (ref_frame_skip_mask[0] & (1 << ref_frame) &&
2754 ref_frame_skip_mask[1] & (1 << MAX(0, second_ref_frame)))
2757 if (mode_skip_mask[ref_frame] & (1 << this_mode))
2760 // Test best rd so far against threshold for trying this mode.
2761 if (best_mode_skippable && cpi->sf.schedule_mode_search)
2762 mode_threshold[mode_index] <<= 1;
2764 if (best_rd < mode_threshold[mode_index])
2767 if (cpi->sf.motion_field_mode_search) {
2768 const int mi_width = MIN(num_8x8_blocks_wide_lookup[bsize],
2769 tile->mi_col_end - mi_col);
2770 const int mi_height = MIN(num_8x8_blocks_high_lookup[bsize],
2771 tile->mi_row_end - mi_row);
2772 const int bsl = mi_width_log2(bsize);
2773 int cb_partition_search_ctrl = (((mi_row + mi_col) >> bsl)
2774 + get_chessboard_index(cm->current_video_frame)) & 0x1;
2775 MB_MODE_INFO *ref_mbmi;
2776 int const_motion = 1;
2777 int skip_ref_frame = !cb_partition_search_ctrl;
2778 MV_REFERENCE_FRAME rf = NONE;
2780 ref_mv.as_int = INVALID_MV;
2782 if ((mi_row - 1) >= tile->mi_row_start) {
2783 ref_mv = xd->mi[-xd->mi_stride].src_mi->mbmi.mv[0];
2784 rf = xd->mi[-xd->mi_stride].src_mi->mbmi.ref_frame[0];
2785 for (i = 0; i < mi_width; ++i) {
2786 ref_mbmi = &xd->mi[-xd->mi_stride + i].src_mi->mbmi;
2787 const_motion &= (ref_mv.as_int == ref_mbmi->mv[0].as_int) &&
2788 (ref_frame == ref_mbmi->ref_frame[0]);
2789 skip_ref_frame &= (rf == ref_mbmi->ref_frame[0]);
2793 if ((mi_col - 1) >= tile->mi_col_start) {
2794 if (ref_mv.as_int == INVALID_MV)
2795 ref_mv = xd->mi[-1].src_mi->mbmi.mv[0];
2797 rf = xd->mi[-1].src_mi->mbmi.ref_frame[0];
2798 for (i = 0; i < mi_height; ++i) {
2799 ref_mbmi = &xd->mi[i * xd->mi_stride - 1].src_mi->mbmi;
2800 const_motion &= (ref_mv.as_int == ref_mbmi->mv[0].as_int) &&
2801 (ref_frame == ref_mbmi->ref_frame[0]);
2802 skip_ref_frame &= (rf == ref_mbmi->ref_frame[0]);
2806 if (skip_ref_frame && this_mode != NEARESTMV && this_mode != NEWMV)
2807 if (rf > INTRA_FRAME)
2808 if (ref_frame != rf)
2812 if (this_mode == NEARMV || this_mode == ZEROMV)
2816 comp_pred = second_ref_frame > INTRA_FRAME;
2818 if (!cm->allow_comp_inter_inter)
2821 // Skip compound inter modes if ARF is not available.
2822 if (!(cpi->ref_frame_flags & flag_list[second_ref_frame]))
2825 // Do not allow compound prediction if the segment level reference frame
2826 // feature is in use as in this case there can only be one reference.
2827 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
2830 if ((mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
2831 best_mode_index >= 0 && best_mbmode.ref_frame[0] == INTRA_FRAME)
2834 mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
2836 if (ref_frame != INTRA_FRAME)
2837 mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
2840 if (ref_frame == INTRA_FRAME) {
2841 if (cpi->sf.adaptive_mode_search)
2842 if ((x->source_variance << num_pels_log2_lookup[bsize]) > best_pred_sse)
2845 if (this_mode != DC_PRED) {
2846 // Disable intra modes other than DC_PRED for blocks with low variance
2847 // Threshold for intra skipping based on source variance
2848 // TODO(debargha): Specialize the threshold for super block sizes
2849 const unsigned int skip_intra_var_thresh = 64;
2850 if ((mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) &&
2851 x->source_variance < skip_intra_var_thresh)
2853 // Only search the oblique modes if the best so far is
2854 // one of the neighboring directional modes
2855 if ((mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) &&
2856 (this_mode >= D45_PRED && this_mode <= TM_PRED)) {
2857 if (best_mode_index >= 0 &&
2858 best_mbmode.ref_frame[0] > INTRA_FRAME)
2861 if (mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) {
2862 if (conditional_skipintra(this_mode, best_intra_mode))
2867 const MV_REFERENCE_FRAME ref_frames[2] = {ref_frame, second_ref_frame};
2868 if (!check_best_zero_mv(cpi, mbmi->mode_context, frame_mv,
2869 this_mode, ref_frames))
2873 mbmi->mode = this_mode;
2874 mbmi->uv_mode = DC_PRED;
2875 mbmi->ref_frame[0] = ref_frame;
2876 mbmi->ref_frame[1] = second_ref_frame;
2877 // Evaluate all sub-pel filters irrespective of whether we can use
2878 // them for this frame.
2879 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP
2880 : cm->interp_filter;
2881 mbmi->mv[0].as_int = mbmi->mv[1].as_int = 0;
2884 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
2886 // Select prediction reference frames.
2887 for (i = 0; i < MAX_MB_PLANE; i++) {
2888 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
2890 xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
2893 for (i = 0; i < TX_MODES; ++i)
2894 tx_cache[i] = INT64_MAX;
2896 if (ref_frame == INTRA_FRAME) {
2898 super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable,
2899 NULL, bsize, tx_cache, best_rd);
2901 if (rate_y == INT_MAX)
2904 uv_tx = get_uv_tx_size_impl(mbmi->tx_size, bsize, pd[1].subsampling_x,
2905 pd[1].subsampling_y);
2906 if (rate_uv_intra[uv_tx] == INT_MAX) {
2907 choose_intra_uv_mode(cpi, ctx, bsize, uv_tx,
2908 &rate_uv_intra[uv_tx], &rate_uv_tokenonly[uv_tx],
2909 &dist_uv[uv_tx], &skip_uv[uv_tx], &mode_uv[uv_tx]);
2912 rate_uv = rate_uv_tokenonly[uv_tx];
2913 distortion_uv = dist_uv[uv_tx];
2914 skippable = skippable && skip_uv[uv_tx];
2915 mbmi->uv_mode = mode_uv[uv_tx];
2917 rate2 = rate_y + cpi->mbmode_cost[mbmi->mode] + rate_uv_intra[uv_tx];
2918 if (this_mode != DC_PRED && this_mode != TM_PRED)
2919 rate2 += intra_cost_penalty;
2920 distortion2 = distortion_y + distortion_uv;
2922 this_rd = handle_inter_mode(cpi, x, bsize,
2924 &rate2, &distortion2, &skippable,
2925 &rate_y, &distortion_y,
2926 &rate_uv, &distortion_uv,
2927 &disable_skip, frame_mv,
2929 single_newmv, single_inter_filter,
2930 single_skippable, &total_sse, best_rd);
2931 if (this_rd == INT64_MAX)
2934 compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred);
2936 if (cm->reference_mode == REFERENCE_MODE_SELECT)
2937 rate2 += compmode_cost;
2940 // Estimate the reference frame signaling cost and add it
2941 // to the rolling cost variable.
2943 rate2 += ref_costs_comp[ref_frame];
2945 rate2 += ref_costs_single[ref_frame];
2948 if (!disable_skip) {
2950 // Back out the coefficient coding costs
2951 rate2 -= (rate_y + rate_uv);
2953 // Cost the skip mb case
2954 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2955 } else if (ref_frame != INTRA_FRAME && !xd->lossless) {
2956 if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) <
2957 RDCOST(x->rdmult, x->rddiv, 0, total_sse)) {
2958 // Add in the cost of the no skip flag.
2959 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
2961 // FIXME(rbultje) make this work for splitmv also
2962 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
2963 distortion2 = total_sse;
2964 assert(total_sse >= 0);
2965 rate2 -= (rate_y + rate_uv);
2969 // Add in the cost of the no skip flag.
2970 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
2973 // Calculate the final RD estimate for this mode.
2974 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
2977 if (ref_frame == INTRA_FRAME) {
2978 // Keep record of best intra rd
2979 if (this_rd < best_intra_rd) {
2980 best_intra_rd = this_rd;
2981 best_intra_mode = mbmi->mode;
2985 if (!disable_skip && ref_frame == INTRA_FRAME) {
2986 for (i = 0; i < REFERENCE_MODES; ++i)
2987 best_pred_rd[i] = MIN(best_pred_rd[i], this_rd);
2988 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
2989 best_filter_rd[i] = MIN(best_filter_rd[i], this_rd);
2992 // Did this mode help.. i.e. is it the new best mode
2993 if (this_rd < best_rd || x->skip) {
2994 int max_plane = MAX_MB_PLANE;
2995 if (!mode_excluded) {
2996 // Note index of best mode so far
2997 best_mode_index = mode_index;
2999 if (ref_frame == INTRA_FRAME) {
3000 /* required for left and above block mv */
3001 mbmi->mv[0].as_int = 0;
3004 best_pred_sse = x->pred_sse[ref_frame];
3007 *returnrate = rate2;
3008 *returndistortion = distortion2;
3010 best_mbmode = *mbmi;
3011 best_skip2 = this_skip2;
3012 best_mode_skippable = skippable;
3014 if (!x->select_tx_size)
3015 swap_block_ptr(x, ctx, 1, 0, 0, max_plane);
3016 vpx_memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mbmi->tx_size],
3017 sizeof(uint8_t) * ctx->num_4x4_blk);
3019 // TODO(debargha): enhance this test with a better distortion prediction
3020 // based on qp, activity mask and history
3021 if ((mode_search_skip_flags & FLAG_EARLY_TERMINATE) &&
3022 (mode_index > MIN_EARLY_TERM_INDEX)) {
3023 const int qstep = xd->plane[0].dequant[1];
3024 // TODO(debargha): Enhance this by specializing for each mode_index
3026 if (x->source_variance < UINT_MAX) {
3027 const int var_adjust = (x->source_variance < 16);
3028 scale -= var_adjust;
3030 if (ref_frame > INTRA_FRAME &&
3031 distortion2 * scale < qstep * qstep) {
3038 /* keep record of best compound/single-only prediction */
3039 if (!disable_skip && ref_frame != INTRA_FRAME) {
3040 int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
3042 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
3043 single_rate = rate2 - compmode_cost;
3044 hybrid_rate = rate2;
3046 single_rate = rate2;
3047 hybrid_rate = rate2 + compmode_cost;
3050 single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2);
3051 hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2);
3054 if (single_rd < best_pred_rd[SINGLE_REFERENCE])
3055 best_pred_rd[SINGLE_REFERENCE] = single_rd;
3057 if (single_rd < best_pred_rd[COMPOUND_REFERENCE])
3058 best_pred_rd[COMPOUND_REFERENCE] = single_rd;
3060 if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
3061 best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
3063 /* keep record of best filter type */
3064 if (!mode_excluded && cm->interp_filter != BILINEAR) {
3065 int64_t ref = rd_opt->filter_cache[cm->interp_filter == SWITCHABLE ?
3066 SWITCHABLE_FILTERS : cm->interp_filter];
3068 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
3070 if (ref == INT64_MAX)
3072 else if (rd_opt->filter_cache[i] == INT64_MAX)
3073 // when early termination is triggered, the encoder does not have
3074 // access to the rate-distortion cost. it only knows that the cost
3075 // should be above the maximum valid value. hence it takes the known
3076 // maximum plus an arbitrary constant as the rate-distortion cost.
3077 adj_rd = rd_opt->mask_filter - ref + 10;
3079 adj_rd = rd_opt->filter_cache[i] - ref;
3082 best_filter_rd[i] = MIN(best_filter_rd[i], adj_rd);
3087 /* keep record of best txfm size */
3088 if (bsize < BLOCK_32X32) {
3089 if (bsize < BLOCK_16X16)
3090 tx_cache[ALLOW_16X16] = tx_cache[ALLOW_8X8];
3092 tx_cache[ALLOW_32X32] = tx_cache[ALLOW_16X16];
3094 if (!mode_excluded && this_rd != INT64_MAX) {
3095 for (i = 0; i < TX_MODES && tx_cache[i] < INT64_MAX; i++) {
3096 int64_t adj_rd = INT64_MAX;
3097 adj_rd = this_rd + tx_cache[i] - tx_cache[cm->tx_mode];
3099 if (adj_rd < best_tx_rd[i])
3100 best_tx_rd[i] = adj_rd;
3107 if (x->skip && !comp_pred)
3111 // The inter modes' rate costs are not calculated precisely in some cases.
3112 // Therefore, sometimes, NEWMV is chosen instead of NEARESTMV, NEARMV, and
3113 // ZEROMV. Here, checks are added for those cases, and the mode decisions
3115 if (best_mbmode.mode == NEWMV) {
3116 const MV_REFERENCE_FRAME refs[2] = {best_mbmode.ref_frame[0],
3117 best_mbmode.ref_frame[1]};
3118 int comp_pred_mode = refs[1] > INTRA_FRAME;
3120 if (frame_mv[NEARESTMV][refs[0]].as_int == best_mbmode.mv[0].as_int &&
3121 ((comp_pred_mode && frame_mv[NEARESTMV][refs[1]].as_int ==
3122 best_mbmode.mv[1].as_int) || !comp_pred_mode))
3123 best_mbmode.mode = NEARESTMV;
3124 else if (frame_mv[NEARMV][refs[0]].as_int == best_mbmode.mv[0].as_int &&
3125 ((comp_pred_mode && frame_mv[NEARMV][refs[1]].as_int ==
3126 best_mbmode.mv[1].as_int) || !comp_pred_mode))
3127 best_mbmode.mode = NEARMV;
3128 else if (best_mbmode.mv[0].as_int == 0 &&
3129 ((comp_pred_mode && best_mbmode.mv[1].as_int == 0) || !comp_pred_mode))
3130 best_mbmode.mode = ZEROMV;
3133 if (best_mode_index < 0 || best_rd >= best_rd_so_far)
3136 // If we used an estimate for the uv intra rd in the loop above...
3137 if (cpi->sf.use_uv_intra_rd_estimate) {
3138 // Do Intra UV best rd mode selection if best mode choice above was intra.
3139 if (best_mbmode.ref_frame[0] == INTRA_FRAME) {
3141 *mbmi = best_mbmode;
3142 uv_tx_size = get_uv_tx_size(mbmi, &xd->plane[1]);
3143 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size],
3144 &rate_uv_tokenonly[uv_tx_size],
3145 &dist_uv[uv_tx_size],
3146 &skip_uv[uv_tx_size],
3147 bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize,
3152 assert((cm->interp_filter == SWITCHABLE) ||
3153 (cm->interp_filter == best_mbmode.interp_filter) ||
3154 !is_inter_block(&best_mbmode));
3156 if (!cpi->rc.is_src_frame_alt_ref)
3157 update_rd_thresh_fact(cpi, bsize, best_mode_index);
3160 *mbmi = best_mbmode;
3161 x->skip |= best_skip2;
3163 for (i = 0; i < REFERENCE_MODES; ++i) {
3164 if (best_pred_rd[i] == INT64_MAX)
3165 best_pred_diff[i] = INT_MIN;
3167 best_pred_diff[i] = best_rd - best_pred_rd[i];
3171 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
3172 if (best_filter_rd[i] == INT64_MAX)
3173 best_filter_diff[i] = 0;
3175 best_filter_diff[i] = best_rd - best_filter_rd[i];
3177 if (cm->interp_filter == SWITCHABLE)
3178 assert(best_filter_diff[SWITCHABLE_FILTERS] == 0);
3179 for (i = 0; i < TX_MODES; i++) {
3180 if (best_tx_rd[i] == INT64_MAX)
3181 best_tx_diff[i] = 0;
3183 best_tx_diff[i] = best_rd - best_tx_rd[i];
3186 vp9_zero(best_filter_diff);
3187 vp9_zero(best_tx_diff);
3190 // TODO(yunqingwang): Moving this line in front of the above best_filter_diff
3191 // updating code causes PSNR loss. Need to figure out the confliction.
3192 x->skip |= best_mode_skippable;
3194 store_coding_context(x, ctx, best_mode_index, best_pred_diff,
3195 best_tx_diff, best_filter_diff, best_mode_skippable);
3200 int64_t vp9_rd_pick_inter_mode_sb_seg_skip(VP9_COMP *cpi, MACROBLOCK *x,
3202 int64_t *returndistortion,
3204 PICK_MODE_CONTEXT *ctx,
3205 int64_t best_rd_so_far) {
3206 VP9_COMMON *const cm = &cpi->common;
3207 RD_OPT *const rd_opt = &cpi->rd;
3208 MACROBLOCKD *const xd = &x->e_mbd;
3209 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
3210 unsigned char segment_id = mbmi->segment_id;
3211 const int comp_pred = 0;
3213 int64_t best_tx_diff[TX_MODES];
3214 int64_t best_pred_diff[REFERENCE_MODES];
3215 int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
3216 unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
3217 vp9_prob comp_mode_p;
3218 INTERP_FILTER best_filter = SWITCHABLE;
3219 int64_t this_rd = INT64_MAX;
3221 const int64_t distortion2 = 0;
3223 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
3225 estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
3228 for (i = 0; i < MAX_REF_FRAMES; ++i)
3229 x->pred_sse[i] = INT_MAX;
3230 for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i)
3231 x->pred_mv_sad[i] = INT_MAX;
3233 *returnrate = INT_MAX;
3235 assert(vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP));
3237 mbmi->mode = ZEROMV;
3238 mbmi->uv_mode = DC_PRED;
3239 mbmi->ref_frame[0] = LAST_FRAME;
3240 mbmi->ref_frame[1] = NONE;
3241 mbmi->mv[0].as_int = 0;
3244 // Search for best switchable filter by checking the variance of
3245 // pred error irrespective of whether the filter will be used
3246 rd_opt->mask_filter = 0;
3247 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
3248 rd_opt->filter_cache[i] = INT64_MAX;
3250 if (cm->interp_filter != BILINEAR) {
3251 best_filter = EIGHTTAP;
3252 if (cm->interp_filter == SWITCHABLE &&
3253 x->source_variance >= cpi->sf.disable_filter_search_var_thresh) {
3255 int best_rs = INT_MAX;
3256 for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
3257 mbmi->interp_filter = i;
3258 rs = vp9_get_switchable_rate(cpi);
3261 best_filter = mbmi->interp_filter;
3266 // Set the appropriate filter
3267 if (cm->interp_filter == SWITCHABLE) {
3268 mbmi->interp_filter = best_filter;
3269 rate2 += vp9_get_switchable_rate(cpi);
3271 mbmi->interp_filter = cm->interp_filter;
3274 if (cm->reference_mode == REFERENCE_MODE_SELECT)
3275 rate2 += vp9_cost_bit(comp_mode_p, comp_pred);
3277 // Estimate the reference frame signaling cost and add it
3278 // to the rolling cost variable.
3279 rate2 += ref_costs_single[LAST_FRAME];
3280 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
3282 *returnrate = rate2;
3283 *returndistortion = distortion2;
3285 if (this_rd >= best_rd_so_far)
3288 assert((cm->interp_filter == SWITCHABLE) ||
3289 (cm->interp_filter == mbmi->interp_filter));
3291 update_rd_thresh_fact(cpi, bsize, THR_ZEROMV);
3293 vp9_zero(best_pred_diff);
3294 vp9_zero(best_filter_diff);
3295 vp9_zero(best_tx_diff);
3297 if (!x->select_tx_size)
3298 swap_block_ptr(x, ctx, 1, 0, 0, MAX_MB_PLANE);
3299 store_coding_context(x, ctx, THR_ZEROMV,
3300 best_pred_diff, best_tx_diff, best_filter_diff, 0);
3305 int64_t vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x,
3306 const TileInfo *const tile,
3307 int mi_row, int mi_col,
3309 int64_t *returndistortion,
3311 PICK_MODE_CONTEXT *ctx,
3312 int64_t best_rd_so_far) {
3313 VP9_COMMON *const cm = &cpi->common;
3314 RD_OPT *const rd_opt = &cpi->rd;
3315 MACROBLOCKD *const xd = &x->e_mbd;
3316 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
3317 const struct segmentation *const seg = &cm->seg;
3318 MV_REFERENCE_FRAME ref_frame, second_ref_frame;
3319 unsigned char segment_id = mbmi->segment_id;
3321 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
3322 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
3323 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
3325 int64_t best_rd = best_rd_so_far;
3326 int64_t best_yrd = best_rd_so_far; // FIXME(rbultje) more precise
3327 static const int64_t best_tx_diff[TX_MODES] = { 0 };
3328 int64_t best_pred_diff[REFERENCE_MODES];
3329 int64_t best_pred_rd[REFERENCE_MODES];
3330 int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS];
3331 int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
3332 MB_MODE_INFO best_mbmode;
3333 int ref_index, best_ref_index = 0;
3334 unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES];
3335 vp9_prob comp_mode_p;
3336 INTERP_FILTER tmp_best_filter = SWITCHABLE;
3337 int rate_uv_intra, rate_uv_tokenonly;
3340 PREDICTION_MODE mode_uv = DC_PRED;
3341 const int intra_cost_penalty =
3342 20 * vp9_dc_quant(cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth);
3343 int_mv seg_mvs[4][MAX_REF_FRAMES];
3344 b_mode_info best_bmodes[4];
3346 int ref_frame_skip_mask[2] = { 0 };
3348 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
3349 vpx_memset(x->zcoeff_blk[TX_4X4], 0, 4);
3350 vp9_zero(best_mbmode);
3352 for (i = 0; i < 4; i++) {
3354 for (j = 0; j < MAX_REF_FRAMES; j++)
3355 seg_mvs[i][j].as_int = INVALID_MV;
3358 estimate_ref_frame_costs(cm, xd, segment_id, ref_costs_single, ref_costs_comp,
3361 for (i = 0; i < REFERENCE_MODES; ++i)
3362 best_pred_rd[i] = INT64_MAX;
3363 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
3364 best_filter_rd[i] = INT64_MAX;
3365 rate_uv_intra = INT_MAX;
3367 *returnrate = INT_MAX;
3369 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
3370 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
3371 setup_buffer_inter(cpi, x, tile,
3372 ref_frame, bsize, mi_row, mi_col,
3373 frame_mv[NEARESTMV], frame_mv[NEARMV],
3376 ref_frame_skip_mask[0] |= (1 << ref_frame);
3377 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
3379 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
3380 frame_mv[ZEROMV][ref_frame].as_int = 0;
3383 for (ref_index = 0; ref_index < MAX_REFS; ++ref_index) {
3384 int mode_excluded = 0;
3385 int64_t this_rd = INT64_MAX;
3386 int disable_skip = 0;
3387 int compmode_cost = 0;
3388 int rate2 = 0, rate_y = 0, rate_uv = 0;
3389 int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0;
3393 int64_t total_sse = INT_MAX;
3396 ref_frame = vp9_ref_order[ref_index].ref_frame[0];
3397 second_ref_frame = vp9_ref_order[ref_index].ref_frame[1];
3399 // Look at the reference frame of the best mode so far and set the
3400 // skip mask to look at a subset of the remaining modes.
3401 if (ref_index > 2 && cpi->sf.mode_skip_start < MAX_MODES) {
3402 if (ref_index == 3) {
3403 switch (best_mbmode.ref_frame[0]) {
3407 ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | (1 << ALTREF_FRAME);
3408 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
3411 ref_frame_skip_mask[0] |= (1 << LAST_FRAME) | (1 << ALTREF_FRAME);
3412 ref_frame_skip_mask[1] |= SECOND_REF_FRAME_MASK;
3415 ref_frame_skip_mask[0] |= (1 << GOLDEN_FRAME) | (1 << LAST_FRAME);
3418 case MAX_REF_FRAMES:
3419 assert(0 && "Invalid Reference frame");
3425 if (ref_frame_skip_mask[0] & (1 << ref_frame) &&
3426 ref_frame_skip_mask[1] & (1 << MAX(0, second_ref_frame)))
3429 // Test best rd so far against threshold for trying this mode.
3430 if (rd_less_than_thresh(best_rd,
3431 rd_opt->threshes[segment_id][bsize][ref_index],
3432 rd_opt->thresh_freq_fact[bsize][ref_index]))
3435 comp_pred = second_ref_frame > INTRA_FRAME;
3437 if (!cm->allow_comp_inter_inter)
3439 if (!(cpi->ref_frame_flags & flag_list[second_ref_frame]))
3441 // Do not allow compound prediction if the segment level reference frame
3442 // feature is in use as in this case there can only be one reference.
3443 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
3446 if ((cpi->sf.mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) &&
3447 best_mbmode.ref_frame[0] == INTRA_FRAME)
3451 // TODO(jingning, jkoleszar): scaling reference frame not supported for
3453 if (ref_frame > INTRA_FRAME &&
3454 vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
3457 if (second_ref_frame > INTRA_FRAME &&
3458 vp9_is_scaled(&cm->frame_refs[second_ref_frame - 1].sf))
3462 mode_excluded = cm->reference_mode == SINGLE_REFERENCE;
3463 else if (ref_frame != INTRA_FRAME)
3464 mode_excluded = cm->reference_mode == COMPOUND_REFERENCE;
3466 // If the segment reference frame feature is enabled....
3467 // then do nothing if the current ref frame is not allowed..
3468 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
3469 vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame) {
3471 // Disable this drop out case if the ref frame
3472 // segment level feature is enabled for this segment. This is to
3473 // prevent the possibility that we end up unable to pick any mode.
3474 } else if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
3475 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
3476 // unless ARNR filtering is enabled in which case we want
3477 // an unfiltered alternative. We allow near/nearest as well
3478 // because they may result in zero-zero MVs but be cheaper.
3479 if (cpi->rc.is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
3483 mbmi->tx_size = TX_4X4;
3484 mbmi->uv_mode = DC_PRED;
3485 mbmi->ref_frame[0] = ref_frame;
3486 mbmi->ref_frame[1] = second_ref_frame;
3487 // Evaluate all sub-pel filters irrespective of whether we can use
3488 // them for this frame.
3489 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP
3490 : cm->interp_filter;
3492 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
3494 // Select prediction reference frames.
3495 for (i = 0; i < MAX_MB_PLANE; i++) {
3496 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
3498 xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i];
3501 if (ref_frame == INTRA_FRAME) {
3503 if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate, &rate_y,
3504 &distortion_y, best_rd) >= best_rd)
3507 rate2 += intra_cost_penalty;
3508 distortion2 += distortion_y;
3510 if (rate_uv_intra == INT_MAX) {
3511 choose_intra_uv_mode(cpi, ctx, bsize, TX_4X4,
3517 rate2 += rate_uv_intra;
3518 rate_uv = rate_uv_tokenonly;
3519 distortion2 += dist_uv;
3520 distortion_uv = dist_uv;
3521 mbmi->uv_mode = mode_uv;
3525 int64_t this_rd_thresh;
3526 int64_t tmp_rd, tmp_best_rd = INT64_MAX, tmp_best_rdu = INT64_MAX;
3527 int tmp_best_rate = INT_MAX, tmp_best_ratey = INT_MAX;
3528 int64_t tmp_best_distortion = INT_MAX, tmp_best_sse, uv_sse;
3529 int tmp_best_skippable = 0;
3530 int switchable_filter_index;
3531 int_mv *second_ref = comp_pred ?
3532 &mbmi->ref_mvs[second_ref_frame][0] : NULL;
3533 b_mode_info tmp_best_bmodes[16];
3534 MB_MODE_INFO tmp_best_mbmode;
3535 BEST_SEG_INFO bsi[SWITCHABLE_FILTERS];
3536 int pred_exists = 0;
3539 this_rd_thresh = (ref_frame == LAST_FRAME) ?
3540 rd_opt->threshes[segment_id][bsize][THR_LAST] :
3541 rd_opt->threshes[segment_id][bsize][THR_ALTR];
3542 this_rd_thresh = (ref_frame == GOLDEN_FRAME) ?
3543 rd_opt->threshes[segment_id][bsize][THR_GOLD] : this_rd_thresh;
3544 rd_opt->mask_filter = 0;
3545 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
3546 rd_opt->filter_cache[i] = INT64_MAX;
3548 if (cm->interp_filter != BILINEAR) {
3549 tmp_best_filter = EIGHTTAP;
3550 if (x->source_variance < cpi->sf.disable_filter_search_var_thresh) {
3551 tmp_best_filter = EIGHTTAP;
3552 } else if (cpi->sf.adaptive_pred_interp_filter == 1 &&
3553 ctx->pred_interp_filter < SWITCHABLE) {
3554 tmp_best_filter = ctx->pred_interp_filter;
3555 } else if (cpi->sf.adaptive_pred_interp_filter == 2) {
3556 tmp_best_filter = ctx->pred_interp_filter < SWITCHABLE ?
3557 ctx->pred_interp_filter : 0;
3559 for (switchable_filter_index = 0;
3560 switchable_filter_index < SWITCHABLE_FILTERS;
3561 ++switchable_filter_index) {
3564 mbmi->interp_filter = switchable_filter_index;
3565 tmp_rd = rd_pick_best_sub8x8_mode(cpi, x, tile,
3566 &mbmi->ref_mvs[ref_frame][0],
3567 second_ref, best_yrd, &rate,
3568 &rate_y, &distortion,
3569 &skippable, &total_sse,
3570 (int) this_rd_thresh, seg_mvs,
3571 bsi, switchable_filter_index,
3574 if (tmp_rd == INT64_MAX)
3576 rs = vp9_get_switchable_rate(cpi);
3577 rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0);
3578 rd_opt->filter_cache[switchable_filter_index] = tmp_rd;
3579 rd_opt->filter_cache[SWITCHABLE_FILTERS] =
3580 MIN(rd_opt->filter_cache[SWITCHABLE_FILTERS],
3582 if (cm->interp_filter == SWITCHABLE)
3585 rd_opt->mask_filter = MAX(rd_opt->mask_filter, tmp_rd);
3587 newbest = (tmp_rd < tmp_best_rd);
3589 tmp_best_filter = mbmi->interp_filter;
3590 tmp_best_rd = tmp_rd;
3592 if ((newbest && cm->interp_filter == SWITCHABLE) ||
3593 (mbmi->interp_filter == cm->interp_filter &&
3594 cm->interp_filter != SWITCHABLE)) {
3595 tmp_best_rdu = tmp_rd;
3596 tmp_best_rate = rate;
3597 tmp_best_ratey = rate_y;
3598 tmp_best_distortion = distortion;
3599 tmp_best_sse = total_sse;
3600 tmp_best_skippable = skippable;
3601 tmp_best_mbmode = *mbmi;
3602 for (i = 0; i < 4; i++) {
3603 tmp_best_bmodes[i] = xd->mi[0].src_mi->bmi[i];
3604 x->zcoeff_blk[TX_4X4][i] = !x->plane[0].eobs[i];
3607 if (switchable_filter_index == 0 &&
3608 cpi->sf.use_rd_breakout &&
3609 best_rd < INT64_MAX) {
3610 if (tmp_best_rdu / 2 > best_rd) {
3611 // skip searching the other filters if the first is
3612 // already substantially larger than the best so far
3613 tmp_best_filter = mbmi->interp_filter;
3614 tmp_best_rdu = INT64_MAX;
3619 } // switchable_filter_index loop
3623 if (tmp_best_rdu == INT64_MAX && pred_exists)
3626 mbmi->interp_filter = (cm->interp_filter == SWITCHABLE ?
3627 tmp_best_filter : cm->interp_filter);
3629 // Handles the special case when a filter that is not in the
3630 // switchable list (bilinear, 6-tap) is indicated at the frame level
3631 tmp_rd = rd_pick_best_sub8x8_mode(cpi, x, tile,
3632 &mbmi->ref_mvs[ref_frame][0],
3633 second_ref, best_yrd, &rate, &rate_y,
3634 &distortion, &skippable, &total_sse,
3635 (int) this_rd_thresh, seg_mvs, bsi, 0,
3637 if (tmp_rd == INT64_MAX)
3640 total_sse = tmp_best_sse;
3641 rate = tmp_best_rate;
3642 rate_y = tmp_best_ratey;
3643 distortion = tmp_best_distortion;
3644 skippable = tmp_best_skippable;
3645 *mbmi = tmp_best_mbmode;
3646 for (i = 0; i < 4; i++)
3647 xd->mi[0].src_mi->bmi[i] = tmp_best_bmodes[i];
3651 distortion2 += distortion;
3653 if (cm->interp_filter == SWITCHABLE)
3654 rate2 += vp9_get_switchable_rate(cpi);
3657 mode_excluded = comp_pred ? cm->reference_mode == SINGLE_REFERENCE
3658 : cm->reference_mode == COMPOUND_REFERENCE;
3660 compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred);
3662 tmp_best_rdu = best_rd -
3663 MIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2),
3664 RDCOST(x->rdmult, x->rddiv, 0, total_sse));
3666 if (tmp_best_rdu > 0) {
3667 // If even the 'Y' rd value of split is higher than best so far
3668 // then dont bother looking at UV
3669 vp9_build_inter_predictors_sbuv(&x->e_mbd, mi_row, mi_col,
3671 super_block_uvrd(cpi, x, &rate_uv, &distortion_uv, &uv_skippable,
3672 &uv_sse, BLOCK_8X8, tmp_best_rdu);
3673 if (rate_uv == INT_MAX)
3676 distortion2 += distortion_uv;
3677 skippable = skippable && uv_skippable;
3678 total_sse += uv_sse;
3682 if (cm->reference_mode == REFERENCE_MODE_SELECT)
3683 rate2 += compmode_cost;
3685 // Estimate the reference frame signaling cost and add it
3686 // to the rolling cost variable.
3687 if (second_ref_frame > INTRA_FRAME) {
3688 rate2 += ref_costs_comp[ref_frame];
3690 rate2 += ref_costs_single[ref_frame];
3693 if (!disable_skip) {
3694 // Skip is never coded at the segment level for sub8x8 blocks and instead
3695 // always coded in the bitstream at the mode info level.
3697 if (ref_frame != INTRA_FRAME && !xd->lossless) {
3698 if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) <
3699 RDCOST(x->rdmult, x->rddiv, 0, total_sse)) {
3700 // Add in the cost of the no skip flag.
3701 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
3703 // FIXME(rbultje) make this work for splitmv also
3704 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
3705 distortion2 = total_sse;
3706 assert(total_sse >= 0);
3707 rate2 -= (rate_y + rate_uv);
3713 // Add in the cost of the no skip flag.
3714 rate2 += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
3717 // Calculate the final RD estimate for this mode.
3718 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
3721 if (!disable_skip && ref_frame == INTRA_FRAME) {
3722 for (i = 0; i < REFERENCE_MODES; ++i)
3723 best_pred_rd[i] = MIN(best_pred_rd[i], this_rd);
3724 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
3725 best_filter_rd[i] = MIN(best_filter_rd[i], this_rd);
3728 // Did this mode help.. i.e. is it the new best mode
3729 if (this_rd < best_rd || x->skip) {
3730 if (!mode_excluded) {
3731 int max_plane = MAX_MB_PLANE;
3732 // Note index of best mode so far
3733 best_ref_index = ref_index;
3735 if (ref_frame == INTRA_FRAME) {
3736 /* required for left and above block mv */
3737 mbmi->mv[0].as_int = 0;
3741 *returnrate = rate2;
3742 *returndistortion = distortion2;
3744 best_yrd = best_rd -
3745 RDCOST(x->rdmult, x->rddiv, rate_uv, distortion_uv);
3746 best_mbmode = *mbmi;
3747 best_skip2 = this_skip2;
3748 if (!x->select_tx_size)
3749 swap_block_ptr(x, ctx, 1, 0, 0, max_plane);
3750 vpx_memcpy(ctx->zcoeff_blk, x->zcoeff_blk[TX_4X4],
3751 sizeof(uint8_t) * ctx->num_4x4_blk);
3753 for (i = 0; i < 4; i++)
3754 best_bmodes[i] = xd->mi[0].src_mi->bmi[i];
3756 // TODO(debargha): enhance this test with a better distortion prediction
3757 // based on qp, activity mask and history
3758 if ((cpi->sf.mode_search_skip_flags & FLAG_EARLY_TERMINATE) &&
3759 (ref_index > MIN_EARLY_TERM_INDEX)) {
3760 const int qstep = xd->plane[0].dequant[1];
3761 // TODO(debargha): Enhance this by specializing for each mode_index
3763 if (x->source_variance < UINT_MAX) {
3764 const int var_adjust = (x->source_variance < 16);
3765 scale -= var_adjust;
3767 if (ref_frame > INTRA_FRAME &&
3768 distortion2 * scale < qstep * qstep) {
3775 /* keep record of best compound/single-only prediction */
3776 if (!disable_skip && ref_frame != INTRA_FRAME) {
3777 int64_t single_rd, hybrid_rd, single_rate, hybrid_rate;
3779 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
3780 single_rate = rate2 - compmode_cost;
3781 hybrid_rate = rate2;
3783 single_rate = rate2;
3784 hybrid_rate = rate2 + compmode_cost;
3787 single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2);
3788 hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2);
3790 if (!comp_pred && single_rd < best_pred_rd[SINGLE_REFERENCE])
3791 best_pred_rd[SINGLE_REFERENCE] = single_rd;
3792 else if (comp_pred && single_rd < best_pred_rd[COMPOUND_REFERENCE])
3793 best_pred_rd[COMPOUND_REFERENCE] = single_rd;
3795 if (hybrid_rd < best_pred_rd[REFERENCE_MODE_SELECT])
3796 best_pred_rd[REFERENCE_MODE_SELECT] = hybrid_rd;
3799 /* keep record of best filter type */
3800 if (!mode_excluded && !disable_skip && ref_frame != INTRA_FRAME &&
3801 cm->interp_filter != BILINEAR) {
3802 int64_t ref = rd_opt->filter_cache[cm->interp_filter == SWITCHABLE ?
3803 SWITCHABLE_FILTERS : cm->interp_filter];
3805 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
3806 if (ref == INT64_MAX)
3808 else if (rd_opt->filter_cache[i] == INT64_MAX)
3809 // when early termination is triggered, the encoder does not have
3810 // access to the rate-distortion cost. it only knows that the cost
3811 // should be above the maximum valid value. hence it takes the known
3812 // maximum plus an arbitrary constant as the rate-distortion cost.
3813 adj_rd = rd_opt->mask_filter - ref + 10;
3815 adj_rd = rd_opt->filter_cache[i] - ref;
3818 best_filter_rd[i] = MIN(best_filter_rd[i], adj_rd);
3825 if (x->skip && !comp_pred)
3829 if (best_rd >= best_rd_so_far)
3832 // If we used an estimate for the uv intra rd in the loop above...
3833 if (cpi->sf.use_uv_intra_rd_estimate) {
3834 // Do Intra UV best rd mode selection if best mode choice above was intra.
3835 if (best_mbmode.ref_frame[0] == INTRA_FRAME) {
3836 *mbmi = best_mbmode;
3837 rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra,
3845 if (best_rd == INT64_MAX) {
3846 *returnrate = INT_MAX;
3847 *returndistortion = INT64_MAX;
3851 assert((cm->interp_filter == SWITCHABLE) ||
3852 (cm->interp_filter == best_mbmode.interp_filter) ||
3853 !is_inter_block(&best_mbmode));
3855 update_rd_thresh_fact(cpi, bsize, best_ref_index);
3858 *mbmi = best_mbmode;
3859 x->skip |= best_skip2;
3860 if (!is_inter_block(&best_mbmode)) {
3861 for (i = 0; i < 4; i++)
3862 xd->mi[0].src_mi->bmi[i].as_mode = best_bmodes[i].as_mode;
3864 for (i = 0; i < 4; ++i)
3865 vpx_memcpy(&xd->mi[0].src_mi->bmi[i], &best_bmodes[i],
3866 sizeof(b_mode_info));
3868 mbmi->mv[0].as_int = xd->mi[0].src_mi->bmi[3].as_mv[0].as_int;
3869 mbmi->mv[1].as_int = xd->mi[0].src_mi->bmi[3].as_mv[1].as_int;
3872 for (i = 0; i < REFERENCE_MODES; ++i) {
3873 if (best_pred_rd[i] == INT64_MAX)
3874 best_pred_diff[i] = INT_MIN;
3876 best_pred_diff[i] = best_rd - best_pred_rd[i];
3880 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
3881 if (best_filter_rd[i] == INT64_MAX)
3882 best_filter_diff[i] = 0;
3884 best_filter_diff[i] = best_rd - best_filter_rd[i];
3886 if (cm->interp_filter == SWITCHABLE)
3887 assert(best_filter_diff[SWITCHABLE_FILTERS] == 0);
3889 vp9_zero(best_filter_diff);
3892 store_coding_context(x, ctx, best_ref_index,
3893 best_pred_diff, best_tx_diff, best_filter_diff, 0);