2 * Copyright (c) 2014 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.
16 #include "./vp9_rtcd.h"
18 #include "vpx_mem/vpx_mem.h"
20 #include "vp9/common/vp9_blockd.h"
21 #include "vp9/common/vp9_common.h"
22 #include "vp9/common/vp9_mvref_common.h"
23 #include "vp9/common/vp9_reconinter.h"
24 #include "vp9/common/vp9_reconintra.h"
26 #include "vp9/encoder/vp9_encoder.h"
27 #include "vp9/encoder/vp9_pickmode.h"
28 #include "vp9/encoder/vp9_ratectrl.h"
29 #include "vp9/encoder/vp9_rd.h"
37 static int mv_refs_rt(const VP9_COMMON *cm, const MACROBLOCKD *xd,
38 const TileInfo *const tile,
39 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
41 int mi_row, int mi_col) {
42 const int *ref_sign_bias = cm->ref_frame_sign_bias;
43 int i, refmv_count = 0;
45 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
47 int different_ref_found = 0;
48 int context_counter = 0;
51 // Blank the reference vector list
52 vpx_memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
54 // The nearest 2 blocks are treated differently
55 // if the size < 8x8 we get the mv from the bmi substructure,
56 // and we also need to keep a mode count.
57 for (i = 0; i < 2; ++i) {
58 const POSITION *const mv_ref = &mv_ref_search[i];
59 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
60 const MODE_INFO *const candidate_mi = xd->mi[mv_ref->col + mv_ref->row *
61 xd->mi_stride].src_mi;
62 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
63 // Keep counts for entropy encoding.
64 context_counter += mode_2_counter[candidate->mode];
65 different_ref_found = 1;
67 if (candidate->ref_frame[0] == ref_frame)
68 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
69 refmv_count, mv_ref_list, Done);
75 // Check the rest of the neighbors in much the same way
76 // as before except we don't need to keep track of sub blocks or
78 for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
79 const POSITION *const mv_ref = &mv_ref_search[i];
80 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
81 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row *
82 xd->mi_stride].src_mi->mbmi;
83 different_ref_found = 1;
85 if (candidate->ref_frame[0] == ref_frame)
86 ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, Done);
90 // Since we couldn't find 2 mvs from the same reference frame
91 // go back through the neighbors and find motion vectors from
92 // different reference frames.
93 if (different_ref_found && !refmv_count) {
94 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
95 const POSITION *mv_ref = &mv_ref_search[i];
96 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
97 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
98 * xd->mi_stride].src_mi->mbmi;
100 // If the candidate is INTRA we don't want to consider its mv.
101 IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
102 refmv_count, mv_ref_list, Done);
109 mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
112 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
113 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
118 static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
119 BLOCK_SIZE bsize, int mi_row, int mi_col,
120 int_mv *tmp_mv, int *rate_mv,
121 int64_t best_rd_sofar) {
122 MACROBLOCKD *xd = &x->e_mbd;
123 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
124 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
125 const int step_param = cpi->sf.mv.fullpel_search_step_param;
126 const int sadpb = x->sadperbit16;
128 const int ref = mbmi->ref_frame[0];
129 const MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
132 const int tmp_col_min = x->mv_col_min;
133 const int tmp_col_max = x->mv_col_max;
134 const int tmp_row_min = x->mv_row_min;
135 const int tmp_row_max = x->mv_row_max;
138 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
140 if (cpi->common.show_frame &&
141 (x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[LAST_FRAME])
144 if (scaled_ref_frame) {
146 // Swap out the reference frame for a version that's been scaled to
147 // match the resolution of the current frame, allowing the existing
148 // motion search code to be used without additional modifications.
149 for (i = 0; i < MAX_MB_PLANE; i++)
150 backup_yv12[i] = xd->plane[i].pre[0];
151 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
153 vp9_set_mv_search_range(x, &ref_mv);
155 assert(x->mv_best_ref_index[ref] <= 2);
156 if (x->mv_best_ref_index[ref] < 2)
157 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
159 mvp_full = x->pred_mv[ref];
164 vp9_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
165 cond_cost_list(cpi, cost_list),
166 &ref_mv, &tmp_mv->as_mv, INT_MAX, 0);
168 x->mv_col_min = tmp_col_min;
169 x->mv_col_max = tmp_col_max;
170 x->mv_row_min = tmp_row_min;
171 x->mv_row_max = tmp_row_max;
173 // calculate the bit cost on motion vector
174 mvp_full.row = tmp_mv->as_mv.row * 8;
175 mvp_full.col = tmp_mv->as_mv.col * 8;
177 *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv,
178 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
180 rate_mode = cpi->inter_mode_cost[mbmi->mode_context[ref]]
181 [INTER_OFFSET(NEWMV)];
182 rv = !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) >
186 cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv,
187 cpi->common.allow_high_precision_mv,
190 cpi->sf.mv.subpel_force_stop,
191 cpi->sf.mv.subpel_iters_per_step,
192 cond_cost_list(cpi, cost_list),
193 x->nmvjointcost, x->mvcost,
194 &dis, &x->pred_sse[ref], NULL, 0, 0);
195 x->pred_mv[ref] = tmp_mv->as_mv;
198 if (scaled_ref_frame) {
200 for (i = 0; i < MAX_MB_PLANE; i++)
201 xd->plane[i].pre[0] = backup_yv12[i];
207 static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize,
208 MACROBLOCK *x, MACROBLOCKD *xd,
209 int *out_rate_sum, int64_t *out_dist_sum,
210 unsigned int *var_y, unsigned int *sse_y) {
211 // Note our transform coeffs are 8 times an orthogonal transform.
212 // Hence quantizer step is also 8 times. To get effective quantizer
213 // we need to divide by 8 before sending to modeling function.
217 struct macroblock_plane *const p = &x->plane[0];
218 struct macroblockd_plane *const pd = &xd->plane[0];
219 const uint32_t dc_quant = pd->dequant[0];
220 const uint32_t ac_quant = pd->dequant[1];
221 unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
222 pd->dst.buf, pd->dst.stride, &sse);
226 if (sse < dc_quant * dc_quant >> 6)
228 else if (var < ac_quant * ac_quant >> 6)
233 if (cpi->common.tx_mode == TX_MODE_SELECT) {
234 if (sse > (var << 2))
235 xd->mi[0].src_mi->mbmi.tx_size =
236 MIN(max_txsize_lookup[bsize],
237 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
239 xd->mi[0].src_mi->mbmi.tx_size = TX_8X8;
241 if (cpi->sf.partition_search_type == VAR_BASED_PARTITION &&
242 xd->mi[0].src_mi->mbmi.tx_size > TX_16X16)
243 xd->mi[0].src_mi->mbmi.tx_size = TX_16X16;
245 xd->mi[0].src_mi->mbmi.tx_size =
246 MIN(max_txsize_lookup[bsize],
247 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
250 #if CONFIG_VP9_HIGHBITDEPTH
251 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
252 vp9_model_rd_from_var_lapndz(sse - var, 1 << num_pels_log2_lookup[bsize],
253 dc_quant >> (xd->bd - 5), &rate, &dist);
255 vp9_model_rd_from_var_lapndz(sse - var, 1 << num_pels_log2_lookup[bsize],
256 dc_quant >> 3, &rate, &dist);
259 vp9_model_rd_from_var_lapndz(sse - var, 1 << num_pels_log2_lookup[bsize],
260 dc_quant >> 3, &rate, &dist);
261 #endif // CONFIG_VP9_HIGHBITDEPTH
263 *out_rate_sum = rate >> 1;
264 *out_dist_sum = dist << 3;
266 #if CONFIG_VP9_HIGHBITDEPTH
267 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
268 vp9_model_rd_from_var_lapndz(var,
269 1 << num_pels_log2_lookup[bsize],
270 ac_quant >> (xd->bd - 5),
274 vp9_model_rd_from_var_lapndz(var,
275 1 << num_pels_log2_lookup[bsize],
281 vp9_model_rd_from_var_lapndz(var,
282 1 << num_pels_log2_lookup[bsize],
286 #endif // CONFIG_VP9_HIGHBITDEPTH
288 *out_rate_sum += rate;
289 *out_dist_sum += dist << 4;
292 static int get_pred_buffer(PRED_BUFFER *p, int len) {
295 for (i = 0; i < len; i++) {
304 static void free_pred_buffer(PRED_BUFFER *p) {
309 static void encode_breakout_test(VP9_COMP *cpi, MACROBLOCK *x,
310 BLOCK_SIZE bsize, int mi_row, int mi_col,
311 MV_REFERENCE_FRAME ref_frame,
312 PREDICTION_MODE this_mode,
313 unsigned int var_y, unsigned int sse_y,
314 struct buf_2d yv12_mb[][MAX_MB_PLANE],
315 int *rate, int64_t *dist) {
316 MACROBLOCKD *xd = &x->e_mbd;
317 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
319 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
320 unsigned int var = var_y, sse = sse_y;
321 // Skipping threshold for ac.
322 unsigned int thresh_ac;
323 // Skipping threshold for dc.
324 unsigned int thresh_dc;
325 if (x->encode_breakout > 0) {
326 // Set a maximum for threshold to avoid big PSNR loss in low bit rate
327 // case. Use extreme low threshold for static frames to limit
329 const unsigned int max_thresh = 36000;
330 // The encode_breakout input
331 const unsigned int min_thresh =
332 MIN(((unsigned int)x->encode_breakout << 4), max_thresh);
333 #if CONFIG_VP9_HIGHBITDEPTH
334 const int shift = 2 * xd->bd - 16;
337 // Calculate threshold according to dequant value.
338 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) / 9;
339 #if CONFIG_VP9_HIGHBITDEPTH
340 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
341 thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
343 #endif // CONFIG_VP9_HIGHBITDEPTH
344 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
346 // Adjust ac threshold according to partition size.
348 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
350 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
351 #if CONFIG_VP9_HIGHBITDEPTH
352 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
353 thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
355 #endif // CONFIG_VP9_HIGHBITDEPTH
361 // Y skipping condition checking for ac and dc.
362 if (var <= thresh_ac && (sse - var) <= thresh_dc) {
363 unsigned int sse_u, sse_v;
364 unsigned int var_u, var_v;
366 // Skip UV prediction unless breakout is zero (lossless) to save
367 // computation with low impact on the result
368 if (x->encode_breakout == 0) {
369 xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
370 xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
371 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
374 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
375 x->plane[1].src.stride,
376 xd->plane[1].dst.buf,
377 xd->plane[1].dst.stride, &sse_u);
379 // U skipping condition checking
380 if ((var_u * 4 <= thresh_ac) && (sse_u - var_u <= thresh_dc)) {
381 var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf,
382 x->plane[2].src.stride,
383 xd->plane[2].dst.buf,
384 xd->plane[2].dst.stride, &sse_v);
386 // V skipping condition checking
387 if ((var_v * 4 <= thresh_ac) && (sse_v - var_v <= thresh_dc)) {
390 // The cost of skip bit needs to be added.
391 *rate = cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
392 [INTER_OFFSET(this_mode)];
394 // More on this part of rate
395 // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
397 // Scaling factor for SSE from spatial domain to frequency
398 // domain is 16. Adjust distortion accordingly.
399 // TODO(yunqingwang): In this function, only y-plane dist is
401 *dist = (sse << 4); // + ((sse_u + sse_v) << 4);
403 // *disable_skip = 1;
409 struct estimate_block_intra_args {
412 PREDICTION_MODE mode;
417 static void estimate_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
418 TX_SIZE tx_size, void *arg) {
419 struct estimate_block_intra_args* const args = arg;
420 VP9_COMP *const cpi = args->cpi;
421 MACROBLOCK *const x = args->x;
422 MACROBLOCKD *const xd = &x->e_mbd;
423 struct macroblock_plane *const p = &x->plane[0];
424 struct macroblockd_plane *const pd = &xd->plane[0];
425 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
426 uint8_t *const src_buf_base = p->src.buf;
427 uint8_t *const dst_buf_base = pd->dst.buf;
428 const int src_stride = p->src.stride;
429 const int dst_stride = pd->dst.stride;
433 unsigned int var_y, sse_y;
434 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
438 p->src.buf = &src_buf_base[4 * (j * src_stride + i)];
439 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)];
440 // Use source buffer as an approximation for the fully reconstructed buffer.
441 vp9_predict_intra_block(xd, block >> (2 * tx_size),
442 b_width_log2_lookup[plane_bsize],
444 x->skip_encode ? p->src.buf : pd->dst.buf,
445 x->skip_encode ? src_stride : dst_stride,
446 pd->dst.buf, dst_stride,
448 // This procedure assumes zero offset from p->src.buf and pd->dst.buf.
449 model_rd_for_sb_y(cpi, bsize_tx, x, xd, &rate, &dist, &var_y, &sse_y);
450 p->src.buf = src_buf_base;
451 pd->dst.buf = dst_buf_base;
456 static const THR_MODES mode_idx[MAX_REF_FRAMES][4] = {
457 {THR_DC, THR_H_PRED, THR_V_PRED, THR_TM},
458 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV},
459 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG},
460 {THR_NEARESTA, THR_NEARA, THR_ZEROA, THR_NEWA},
463 static const PREDICTION_MODE intra_mode_list[] = {
464 DC_PRED, V_PRED, H_PRED, TM_PRED
467 void vp9_pick_intra_mode(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
468 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
469 MACROBLOCKD *const xd = &x->e_mbd;
470 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
471 RD_COST this_rdc, best_rdc;
472 PREDICTION_MODE this_mode;
473 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
474 const TX_SIZE intra_tx_size =
475 MIN(max_txsize_lookup[bsize],
476 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
477 MODE_INFO *const mic = xd->mi[0].src_mi;
479 const MODE_INFO *above_mi = xd->mi[-xd->mi_stride].src_mi;
480 const MODE_INFO *left_mi = xd->left_available ? xd->mi[-1].src_mi : NULL;
481 const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
482 const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
483 bmode_costs = cpi->y_mode_costs[A][L];
486 vp9_rd_cost_reset(&best_rdc);
487 vp9_rd_cost_reset(&this_rdc);
489 mbmi->ref_frame[0] = INTRA_FRAME;
490 mbmi->mv[0].as_int = INVALID_MV;
491 mbmi->uv_mode = DC_PRED;
492 vpx_memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
494 // Change the limit of this loop to add other intra prediction
496 for (this_mode = DC_PRED; this_mode <= H_PRED; ++this_mode) {
497 args.mode = this_mode;
500 mbmi->tx_size = intra_tx_size;
501 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
502 estimate_block_intra, &args);
503 this_rdc.rate = args.rate;
504 this_rdc.dist = args.dist;
505 this_rdc.rate += bmode_costs[this_mode];
506 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
507 this_rdc.rate, this_rdc.dist);
509 if (this_rdc.rdcost < best_rdc.rdcost) {
511 mbmi->mode = this_mode;
518 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
519 // this needs various further optimizations. to be continued..
520 void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
521 TileDataEnc *tile_data,
522 int mi_row, int mi_col, RD_COST *rd_cost,
523 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
524 VP9_COMMON *const cm = &cpi->common;
525 TileInfo *const tile_info = &tile_data->tile_info;
526 MACROBLOCKD *const xd = &x->e_mbd;
527 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
528 struct macroblockd_plane *const pd = &xd->plane[0];
529 PREDICTION_MODE best_mode = ZEROMV;
530 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
531 TX_SIZE best_tx_size = MIN(max_txsize_lookup[bsize],
532 tx_mode_to_biggest_tx_size[cm->tx_mode]);
533 INTERP_FILTER best_pred_filter = EIGHTTAP;
534 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
535 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
536 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
538 RD_COST this_rdc, best_rdc;
539 uint8_t skip_txfm = 0;
540 // var_y and sse_y are saved to be used in skipping checking
541 unsigned int var_y = UINT_MAX;
542 unsigned int sse_y = UINT_MAX;
543 // Reduce the intra cost penalty for small blocks (<=16x16).
544 const int reduction_fac =
545 (cpi->sf.partition_search_type == VAR_BASED_PARTITION &&
546 bsize <= BLOCK_16X16) ? 4 : 1;
547 const int intra_cost_penalty = vp9_get_intra_cost_penalty(
548 cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth) / reduction_fac;
549 const int64_t inter_mode_thresh = RDCOST(x->rdmult, x->rddiv,
550 intra_cost_penalty, 0);
551 const int intra_mode_cost = 50;
553 const int8_t segment_id = mbmi->segment_id;
554 const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
555 const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
556 INTERP_FILTER filter_ref;
557 const int bsl = mi_width_log2_lookup[bsize];
558 const int pred_filter_search = cm->interp_filter == SWITCHABLE ?
559 (((mi_row + mi_col) >> bsl) +
560 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0;
561 int const_motion[MAX_REF_FRAMES] = { 0 };
562 const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
563 const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
564 // For speed 6, the result of interp filter is reused later in actual encoding
566 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
568 DECLARE_ALIGNED_ARRAY(16, uint8_t, pred_buf, 3 * 64 * 64);
569 #if CONFIG_VP9_HIGHBITDEPTH
570 DECLARE_ALIGNED_ARRAY(16, uint16_t, pred_buf_16, 3 * 64 * 64);
572 struct buf_2d orig_dst = pd->dst;
573 PRED_BUFFER *best_pred = NULL;
574 PRED_BUFFER *this_mode_pred = NULL;
575 const int pixels_in_block = bh * bw;
576 int reuse_inter_pred = cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready;
577 int ref_frame_skip_mask = 0;
579 if (reuse_inter_pred) {
581 for (i = 0; i < 3; i++) {
582 #if CONFIG_VP9_HIGHBITDEPTH
583 if (cm->use_highbitdepth)
584 tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
586 tmp[i].data = &pred_buf[pixels_in_block * i];
588 tmp[i].data = &pred_buf[pixels_in_block * i];
589 #endif // CONFIG_VP9_HIGHBITDEPTH
593 tmp[3].data = pd->dst.buf;
594 tmp[3].stride = pd->dst.stride;
598 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
601 if (xd->up_available)
602 filter_ref = xd->mi[-xd->mi_stride].src_mi->mbmi.interp_filter;
603 else if (xd->left_available)
604 filter_ref = xd->mi[-1].src_mi->mbmi.interp_filter;
606 filter_ref = cm->interp_filter;
608 // initialize mode decisions
609 vp9_rd_cost_reset(&best_rdc);
610 vp9_rd_cost_reset(&this_rdc);
611 vp9_rd_cost_reset(rd_cost);
612 vpx_memset(mbmi, 0, sizeof(MB_MODE_INFO));
613 mbmi->sb_type = bsize;
614 mbmi->ref_frame[0] = NONE;
615 mbmi->ref_frame[1] = NONE;
616 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
617 tx_mode_to_biggest_tx_size[cm->tx_mode]);
618 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ?
619 EIGHTTAP : cm->interp_filter;
620 mbmi->segment_id = segment_id;
622 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
623 x->pred_mv_sad[ref_frame] = INT_MAX;
624 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
625 frame_mv[ZEROMV][ref_frame].as_int = 0;
627 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
628 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
629 int_mv *const candidates = mbmi->ref_mvs[ref_frame];
630 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
632 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
635 if (!cm->error_resilient_mode)
636 vp9_find_mv_refs(cm, xd, tile_info, xd->mi[0].src_mi, ref_frame,
637 candidates, mi_row, mi_col);
639 const_motion[ref_frame] = mv_refs_rt(cm, xd, tile_info,
641 ref_frame, candidates,
644 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
645 &frame_mv[NEARESTMV][ref_frame],
646 &frame_mv[NEARMV][ref_frame]);
648 if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8)
649 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
652 ref_frame_skip_mask |= (1 << ref_frame);
656 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
657 PREDICTION_MODE this_mode;
658 int i = (ref_frame == LAST_FRAME) ? GOLDEN_FRAME : LAST_FRAME;
660 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
663 if (cpi->ref_frame_flags & flag_list[i])
664 if (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[i] << 1))
665 ref_frame_skip_mask |= (1 << ref_frame);
667 if (ref_frame_skip_mask & (1 << ref_frame))
670 // Select prediction reference frames.
671 xd->plane[0].pre[0] = yv12_mb[ref_frame][0];
673 clamp_mv2(&frame_mv[NEARESTMV][ref_frame].as_mv, xd);
674 clamp_mv2(&frame_mv[NEARMV][ref_frame].as_mv, xd);
676 mbmi->ref_frame[0] = ref_frame;
678 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
681 int mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
683 if (const_motion[ref_frame] && this_mode == NEARMV)
686 if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode)))
689 mode_rd_thresh = rd_threshes[mode_index];
690 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
691 rd_thresh_freq_fact[mode_index]))
694 if (this_mode == NEWMV) {
695 if (ref_frame > LAST_FRAME)
697 if (cpi->sf.partition_search_type != VAR_BASED_PARTITION &&
698 this_rdc.rdcost < (int64_t)(1 << num_pels_log2_lookup[bsize]))
700 if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
701 &frame_mv[NEWMV][ref_frame],
702 &rate_mv, best_rdc.rdcost))
706 if (this_mode != NEARESTMV &&
707 frame_mv[this_mode][ref_frame].as_int ==
708 frame_mv[NEARESTMV][ref_frame].as_int)
711 mbmi->mode = this_mode;
712 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
714 // Search for the best prediction filter type, when the resulting
715 // motion vector is at sub-pixel accuracy level for luma component, i.e.,
716 // the last three bits are all zeros.
717 if (reuse_inter_pred) {
718 if (!this_mode_pred) {
719 this_mode_pred = &tmp[3];
721 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
722 pd->dst.buf = this_mode_pred->data;
727 if ((this_mode == NEWMV || filter_ref == SWITCHABLE) &&
728 pred_filter_search &&
729 ((mbmi->mv[0].as_mv.row & 0x07) != 0 ||
730 (mbmi->mv[0].as_mv.col & 0x07) != 0)) {
733 unsigned int pf_var[3];
734 unsigned int pf_sse[3];
735 TX_SIZE pf_tx_size[3];
736 int64_t best_cost = INT64_MAX;
737 INTERP_FILTER best_filter = SWITCHABLE, filter;
738 PRED_BUFFER *current_pred = this_mode_pred;
740 for (filter = EIGHTTAP; filter <= EIGHTTAP_SHARP; ++filter) {
742 mbmi->interp_filter = filter;
743 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
744 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter],
745 &pf_dist[filter], &pf_var[filter], &pf_sse[filter]);
746 cost = RDCOST(x->rdmult, x->rddiv,
747 vp9_get_switchable_rate(cpi, xd) + pf_rate[filter],
749 pf_tx_size[filter] = mbmi->tx_size;
750 if (cost < best_cost) {
751 best_filter = filter;
753 skip_txfm = x->skip_txfm[0];
755 if (reuse_inter_pred) {
756 if (this_mode_pred != current_pred) {
757 free_pred_buffer(this_mode_pred);
758 this_mode_pred = current_pred;
761 if (filter < EIGHTTAP_SHARP) {
762 current_pred = &tmp[get_pred_buffer(tmp, 3)];
763 pd->dst.buf = current_pred->data;
770 if (reuse_inter_pred && this_mode_pred != current_pred)
771 free_pred_buffer(current_pred);
773 mbmi->interp_filter = best_filter;
774 mbmi->tx_size = pf_tx_size[mbmi->interp_filter];
775 this_rdc.rate = pf_rate[mbmi->interp_filter];
776 this_rdc.dist = pf_dist[mbmi->interp_filter];
777 var_y = pf_var[mbmi->interp_filter];
778 sse_y = pf_sse[mbmi->interp_filter];
779 x->skip_txfm[0] = skip_txfm;
781 mbmi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP: filter_ref;
782 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
783 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
787 this_rdc.rate += rate_mv;
788 this_rdc.rate += cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
789 [INTER_OFFSET(this_mode)];
790 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
791 this_rdc.rate, this_rdc.dist);
793 // Skipping checking: test to see if this block can be reconstructed by
795 if (cpi->allow_encode_breakout) {
796 encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame,
797 this_mode, var_y, sse_y, yv12_mb,
798 &this_rdc.rate, &this_rdc.dist);
800 this_rdc.rate += rate_mv;
801 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
802 this_rdc.rate, this_rdc.dist);
806 #if CONFIG_VP9_TEMPORAL_DENOISING
807 if (cpi->oxcf.noise_sensitivity > 0) {
808 vp9_denoiser_update_frame_stats(mbmi, sse_y, this_mode, ctx);
814 if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
816 best_mode = this_mode;
817 best_pred_filter = mbmi->interp_filter;
818 best_tx_size = mbmi->tx_size;
819 best_ref_frame = ref_frame;
820 skip_txfm = x->skip_txfm[0];
822 if (reuse_inter_pred) {
823 free_pred_buffer(best_pred);
824 best_pred = this_mode_pred;
827 if (reuse_inter_pred)
828 free_pred_buffer(this_mode_pred);
835 // Check that a prediction mode has been selected.
836 assert(best_rdc.rdcost < INT64_MAX);
842 mbmi->mode = best_mode;
843 mbmi->interp_filter = best_pred_filter;
844 mbmi->tx_size = best_tx_size;
845 mbmi->ref_frame[0] = best_ref_frame;
846 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
847 xd->mi[0].src_mi->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
848 x->skip_txfm[0] = skip_txfm;
850 // Perform intra prediction search, if the best SAD is above a certain
852 if (!x->skip && best_rdc.rdcost > inter_mode_thresh &&
853 bsize <= cpi->sf.max_intra_bsize) {
854 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
855 const TX_SIZE intra_tx_size =
856 MIN(max_txsize_lookup[bsize],
857 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
860 if (reuse_inter_pred && best_pred != NULL) {
861 if (best_pred->data == orig_dst.buf) {
862 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
863 vp9_convolve_copy(best_pred->data, best_pred->stride,
864 this_mode_pred->data, this_mode_pred->stride,
865 NULL, 0, NULL, 0, bw, bh);
866 best_pred = this_mode_pred;
871 for (i = 0; i < 4; ++i) {
872 const TX_SIZE saved_tx_size = mbmi->tx_size;
873 const PREDICTION_MODE this_mode = intra_mode_list[i];
874 if (!((1 << this_mode) & cpi->sf.intra_y_mode_mask[intra_tx_size]))
876 skip_txfm = x->skip_txfm[0];
877 args.mode = this_mode;
880 mbmi->tx_size = intra_tx_size;
881 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
882 estimate_block_intra, &args);
883 this_rdc.rate = args.rate;
884 this_rdc.dist = args.dist;
885 this_rdc.rate += cpi->mbmode_cost[this_mode];
886 this_rdc.rate += intra_cost_penalty;
887 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
888 this_rdc.rate, this_rdc.dist);
890 if (this_rdc.rdcost + intra_mode_cost < best_rdc.rdcost) {
892 mbmi->mode = this_mode;
893 mbmi->tx_size = intra_tx_size;
894 mbmi->ref_frame[0] = INTRA_FRAME;
895 mbmi->uv_mode = this_mode;
896 mbmi->mv[0].as_int = INVALID_MV;
898 x->skip_txfm[0] = skip_txfm;
899 mbmi->tx_size = saved_tx_size;
906 if (reuse_inter_pred && best_pred != NULL) {
907 if (best_pred->data != orig_dst.buf && is_inter_mode(mbmi->mode)) {
908 #if CONFIG_VP9_HIGHBITDEPTH
909 if (cm->use_highbitdepth)
910 vp9_highbd_convolve_copy(best_pred->data, best_pred->stride,
911 pd->dst.buf, pd->dst.stride, NULL, 0,
912 NULL, 0, bw, bh, xd->bd);
914 vp9_convolve_copy(best_pred->data, best_pred->stride,
915 pd->dst.buf, pd->dst.stride, NULL, 0,
918 vp9_convolve_copy(best_pred->data, best_pred->stride,
919 pd->dst.buf, pd->dst.stride, NULL, 0,
925 if (is_inter_block(mbmi))
926 vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact,
927 cpi->sf.adaptive_rd_thresh, bsize,
928 mode_idx[best_ref_frame][INTER_OFFSET(mbmi->mode)]);
930 vp9_update_rd_thresh_fact(tile_data->thresh_freq_fact,
931 cpi->sf.adaptive_rd_thresh, bsize,
932 mode_idx[INTRA_FRAME][mbmi->mode]);