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"
17 #include "./vpx_dsp_rtcd.h"
19 #include "vpx_dsp/vpx_dsp_common.h"
20 #include "vpx_mem/vpx_mem.h"
21 #include "vpx_ports/mem.h"
23 #include "vp9/common/vp9_blockd.h"
24 #include "vp9/common/vp9_common.h"
25 #include "vp9/common/vp9_mvref_common.h"
26 #include "vp9/common/vp9_pred_common.h"
27 #include "vp9/common/vp9_reconinter.h"
28 #include "vp9/common/vp9_reconintra.h"
29 #include "vp9/common/vp9_scan.h"
31 #include "vp9/encoder/vp9_cost.h"
32 #include "vp9/encoder/vp9_encoder.h"
33 #include "vp9/encoder/vp9_pickmode.h"
34 #include "vp9/encoder/vp9_ratectrl.h"
35 #include "vp9/encoder/vp9_rd.h"
43 static int mv_refs_rt(const VP9_COMMON *cm, const MACROBLOCK *x,
44 const MACROBLOCKD *xd,
45 const TileInfo *const tile,
46 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
48 int mi_row, int mi_col) {
49 const int *ref_sign_bias = cm->ref_frame_sign_bias;
50 int i, refmv_count = 0;
52 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
54 int different_ref_found = 0;
55 int context_counter = 0;
58 // Blank the reference vector list
59 memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
61 // The nearest 2 blocks are treated differently
62 // if the size < 8x8 we get the mv from the bmi substructure,
63 // and we also need to keep a mode count.
64 for (i = 0; i < 2; ++i) {
65 const POSITION *const mv_ref = &mv_ref_search[i];
66 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
67 const MODE_INFO *const candidate_mi = xd->mi[mv_ref->col + mv_ref->row *
69 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
70 // Keep counts for entropy encoding.
71 context_counter += mode_2_counter[candidate->mode];
72 different_ref_found = 1;
74 if (candidate->ref_frame[0] == ref_frame)
75 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
76 refmv_count, mv_ref_list, Done);
82 // Check the rest of the neighbors in much the same way
83 // as before except we don't need to keep track of sub blocks or
85 for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
86 const POSITION *const mv_ref = &mv_ref_search[i];
87 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
88 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row *
90 different_ref_found = 1;
92 if (candidate->ref_frame[0] == ref_frame)
93 ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, Done);
97 // Since we couldn't find 2 mvs from the same reference frame
98 // go back through the neighbors and find motion vectors from
99 // different reference frames.
100 if (different_ref_found && !refmv_count) {
101 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
102 const POSITION *mv_ref = &mv_ref_search[i];
103 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
104 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
105 * xd->mi_stride]->mbmi;
107 // If the candidate is INTRA we don't want to consider its mv.
108 IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
109 refmv_count, mv_ref_list, Done);
116 x->mbmi_ext->mode_context[ref_frame] = counter_to_context[context_counter];
119 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
120 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
125 static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
126 BLOCK_SIZE bsize, int mi_row, int mi_col,
127 int_mv *tmp_mv, int *rate_mv,
128 int64_t best_rd_sofar) {
129 MACROBLOCKD *xd = &x->e_mbd;
130 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
131 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
132 const int step_param = cpi->sf.mv.fullpel_search_step_param;
133 const int sadpb = x->sadperbit16;
135 const int ref = mbmi->ref_frame[0];
136 const MV ref_mv = x->mbmi_ext->ref_mvs[ref][0].as_mv;
139 const int tmp_col_min = x->mv_col_min;
140 const int tmp_col_max = x->mv_col_max;
141 const int tmp_row_min = x->mv_row_min;
142 const int tmp_row_max = x->mv_row_max;
145 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
147 if (scaled_ref_frame) {
149 // Swap out the reference frame for a version that's been scaled to
150 // match the resolution of the current frame, allowing the existing
151 // motion search code to be used without additional modifications.
152 for (i = 0; i < MAX_MB_PLANE; i++)
153 backup_yv12[i] = xd->plane[i].pre[0];
154 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
156 vp9_set_mv_search_range(x, &ref_mv);
158 assert(x->mv_best_ref_index[ref] <= 2);
159 if (x->mv_best_ref_index[ref] < 2)
160 mvp_full = x->mbmi_ext->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
162 mvp_full = x->pred_mv[ref];
167 vp9_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
168 cond_cost_list(cpi, cost_list),
169 &ref_mv, &tmp_mv->as_mv, INT_MAX, 0);
171 x->mv_col_min = tmp_col_min;
172 x->mv_col_max = tmp_col_max;
173 x->mv_row_min = tmp_row_min;
174 x->mv_row_max = tmp_row_max;
176 // calculate the bit cost on motion vector
177 mvp_full.row = tmp_mv->as_mv.row * 8;
178 mvp_full.col = tmp_mv->as_mv.col * 8;
180 *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv,
181 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
183 rate_mode = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref]]
184 [INTER_OFFSET(NEWMV)];
185 rv = !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) >
189 cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv,
190 cpi->common.allow_high_precision_mv,
193 cpi->sf.mv.subpel_force_stop,
194 cpi->sf.mv.subpel_iters_per_step,
195 cond_cost_list(cpi, cost_list),
196 x->nmvjointcost, x->mvcost,
197 &dis, &x->pred_sse[ref], NULL, 0, 0);
198 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv,
199 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
202 if (scaled_ref_frame) {
204 for (i = 0; i < MAX_MB_PLANE; i++)
205 xd->plane[i].pre[0] = backup_yv12[i];
210 static void block_variance(const uint8_t *src, int src_stride,
211 const uint8_t *ref, int ref_stride,
212 int w, int h, unsigned int *sse, int *sum,
213 int block_size, unsigned int *sse8x8,
214 int *sum8x8, unsigned int *var8x8) {
220 for (i = 0; i < h; i += block_size) {
221 for (j = 0; j < w; j += block_size) {
222 vpx_get8x8var(src + src_stride * i + j, src_stride,
223 ref + ref_stride * i + j, ref_stride,
224 &sse8x8[k], &sum8x8[k]);
227 var8x8[k] = sse8x8[k] - (((unsigned int)sum8x8[k] * sum8x8[k]) >> 6);
233 static void calculate_variance(int bw, int bh, TX_SIZE tx_size,
234 unsigned int *sse_i, int *sum_i,
235 unsigned int *var_o, unsigned int *sse_o,
237 const BLOCK_SIZE unit_size = txsize_to_bsize[tx_size];
238 const int nw = 1 << (bw - b_width_log2_lookup[unit_size]);
239 const int nh = 1 << (bh - b_height_log2_lookup[unit_size]);
242 for (i = 0; i < nh; i += 2) {
243 for (j = 0; j < nw; j += 2) {
244 sse_o[k] = sse_i[i * nw + j] + sse_i[i * nw + j + 1] +
245 sse_i[(i + 1) * nw + j] + sse_i[(i + 1) * nw + j + 1];
246 sum_o[k] = sum_i[i * nw + j] + sum_i[i * nw + j + 1] +
247 sum_i[(i + 1) * nw + j] + sum_i[(i + 1) * nw + j + 1];
248 var_o[k] = sse_o[k] - (((unsigned int)sum_o[k] * sum_o[k]) >>
249 (b_width_log2_lookup[unit_size] +
250 b_height_log2_lookup[unit_size] + 6));
256 static void model_rd_for_sb_y_large(VP9_COMP *cpi, BLOCK_SIZE bsize,
257 MACROBLOCK *x, MACROBLOCKD *xd,
258 int *out_rate_sum, int64_t *out_dist_sum,
259 unsigned int *var_y, unsigned int *sse_y,
260 int mi_row, int mi_col, int *early_term) {
261 // Note our transform coeffs are 8 times an orthogonal transform.
262 // Hence quantizer step is also 8 times. To get effective quantizer
263 // we need to divide by 8 before sending to modeling function.
267 struct macroblock_plane *const p = &x->plane[0];
268 struct macroblockd_plane *const pd = &xd->plane[0];
269 const uint32_t dc_quant = pd->dequant[0];
270 const uint32_t ac_quant = pd->dequant[1];
271 const int64_t dc_thr = dc_quant * dc_quant >> 6;
272 const int64_t ac_thr = ac_quant * ac_quant >> 6;
277 const int bw = b_width_log2_lookup[bsize];
278 const int bh = b_height_log2_lookup[bsize];
279 const int num8x8 = 1 << (bw + bh - 2);
280 unsigned int sse8x8[64] = {0};
281 int sum8x8[64] = {0};
282 unsigned int var8x8[64] = {0};
286 // Calculate variance for whole partition, and also save 8x8 blocks' variance
287 // to be used in following transform skipping test.
288 block_variance(p->src.buf, p->src.stride, pd->dst.buf, pd->dst.stride,
289 4 << bw, 4 << bh, &sse, &sum, 8, sse8x8, sum8x8, var8x8);
290 var = sse - (((int64_t)sum * sum) >> (bw + bh + 4));
295 if (cpi->common.tx_mode == TX_MODE_SELECT) {
296 if (sse > (var << 2))
297 tx_size = VPXMIN(max_txsize_lookup[bsize],
298 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
302 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
303 cyclic_refresh_segment_id_boosted(xd->mi[0]->mbmi.segment_id))
305 else if (tx_size > TX_16X16)
308 tx_size = VPXMIN(max_txsize_lookup[bsize],
309 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
312 assert(tx_size >= TX_8X8);
313 xd->mi[0]->mbmi.tx_size = tx_size;
315 // Evaluate if the partition block is a skippable block in Y plane.
317 unsigned int sse16x16[16] = {0};
318 int sum16x16[16] = {0};
319 unsigned int var16x16[16] = {0};
320 const int num16x16 = num8x8 >> 2;
322 unsigned int sse32x32[4] = {0};
323 int sum32x32[4] = {0};
324 unsigned int var32x32[4] = {0};
325 const int num32x32 = num8x8 >> 4;
329 const int num = (tx_size == TX_8X8) ? num8x8 :
330 ((tx_size == TX_16X16) ? num16x16 : num32x32);
331 const unsigned int *sse_tx = (tx_size == TX_8X8) ? sse8x8 :
332 ((tx_size == TX_16X16) ? sse16x16 : sse32x32);
333 const unsigned int *var_tx = (tx_size == TX_8X8) ? var8x8 :
334 ((tx_size == TX_16X16) ? var16x16 : var32x32);
336 // Calculate variance if tx_size > TX_8X8
337 if (tx_size >= TX_16X16)
338 calculate_variance(bw, bh, TX_8X8, sse8x8, sum8x8, var16x16, sse16x16,
340 if (tx_size == TX_32X32)
341 calculate_variance(bw, bh, TX_16X16, sse16x16, sum16x16, var32x32,
345 x->skip_txfm[0] = SKIP_TXFM_NONE;
346 for (k = 0; k < num; k++)
347 // Check if all ac coefficients can be quantized to zero.
348 if (!(var_tx[k] < ac_thr || var == 0)) {
353 for (k = 0; k < num; k++)
354 // Check if dc coefficient can be quantized to zero.
355 if (!(sse_tx[k] - var_tx[k] < dc_thr || sse == var)) {
361 x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
364 x->skip_txfm[0] = SKIP_TXFM_AC_DC;
365 } else if (dc_test) {
370 if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
371 int skip_uv[2] = {0};
372 unsigned int var_uv[2];
373 unsigned int sse_uv[2];
376 *out_dist_sum = sse << 4;
378 // Transform skipping test in UV planes.
379 for (i = 1; i <= 2; i++) {
380 struct macroblock_plane *const p = &x->plane[i];
381 struct macroblockd_plane *const pd = &xd->plane[i];
382 const TX_SIZE uv_tx_size = get_uv_tx_size(&xd->mi[0]->mbmi, pd);
383 const BLOCK_SIZE unit_size = txsize_to_bsize[uv_tx_size];
384 const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, pd);
385 const int uv_bw = b_width_log2_lookup[uv_bsize];
386 const int uv_bh = b_height_log2_lookup[uv_bsize];
387 const int sf = (uv_bw - b_width_log2_lookup[unit_size]) +
388 (uv_bh - b_height_log2_lookup[unit_size]);
389 const uint32_t uv_dc_thr = pd->dequant[0] * pd->dequant[0] >> (6 - sf);
390 const uint32_t uv_ac_thr = pd->dequant[1] * pd->dequant[1] >> (6 - sf);
393 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, i);
394 var_uv[j] = cpi->fn_ptr[uv_bsize].vf(p->src.buf, p->src.stride,
395 pd->dst.buf, pd->dst.stride, &sse_uv[j]);
397 if ((var_uv[j] < uv_ac_thr || var_uv[j] == 0) &&
398 (sse_uv[j] - var_uv[j] < uv_dc_thr || sse_uv[j] == var_uv[j]))
404 // If the transform in YUV planes are skippable, the mode search checks
405 // fewer inter modes and doesn't check intra modes.
406 if (skip_uv[0] & skip_uv[1]) {
414 #if CONFIG_VP9_HIGHBITDEPTH
415 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
416 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
417 dc_quant >> (xd->bd - 5), &rate, &dist);
419 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
420 dc_quant >> 3, &rate, &dist);
423 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
424 dc_quant >> 3, &rate, &dist);
425 #endif // CONFIG_VP9_HIGHBITDEPTH
429 *out_rate_sum = rate >> 1;
430 *out_dist_sum = dist << 3;
433 *out_dist_sum = (sse - var) << 4;
436 #if CONFIG_VP9_HIGHBITDEPTH
437 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
438 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
439 ac_quant >> (xd->bd - 5), &rate, &dist);
441 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
442 ac_quant >> 3, &rate, &dist);
445 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
446 ac_quant >> 3, &rate, &dist);
447 #endif // CONFIG_VP9_HIGHBITDEPTH
449 *out_rate_sum += rate;
450 *out_dist_sum += dist << 4;
453 static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize,
454 MACROBLOCK *x, MACROBLOCKD *xd,
455 int *out_rate_sum, int64_t *out_dist_sum,
456 unsigned int *var_y, unsigned int *sse_y) {
457 // Note our transform coeffs are 8 times an orthogonal transform.
458 // Hence quantizer step is also 8 times. To get effective quantizer
459 // we need to divide by 8 before sending to modeling function.
463 struct macroblock_plane *const p = &x->plane[0];
464 struct macroblockd_plane *const pd = &xd->plane[0];
465 const int64_t dc_thr = p->quant_thred[0] >> 6;
466 const int64_t ac_thr = p->quant_thred[1] >> 6;
467 const uint32_t dc_quant = pd->dequant[0];
468 const uint32_t ac_quant = pd->dequant[1];
469 unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
470 pd->dst.buf, pd->dst.stride, &sse);
476 if (cpi->common.tx_mode == TX_MODE_SELECT) {
477 if (sse > (var << 2))
478 xd->mi[0]->mbmi.tx_size =
479 VPXMIN(max_txsize_lookup[bsize],
480 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
482 xd->mi[0]->mbmi.tx_size = TX_8X8;
484 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
485 cyclic_refresh_segment_id_boosted(xd->mi[0]->mbmi.segment_id))
486 xd->mi[0]->mbmi.tx_size = TX_8X8;
487 else if (xd->mi[0]->mbmi.tx_size > TX_16X16)
488 xd->mi[0]->mbmi.tx_size = TX_16X16;
490 xd->mi[0]->mbmi.tx_size =
491 VPXMIN(max_txsize_lookup[bsize],
492 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
495 // Evaluate if the partition block is a skippable block in Y plane.
497 const BLOCK_SIZE unit_size =
498 txsize_to_bsize[xd->mi[0]->mbmi.tx_size];
499 const unsigned int num_blk_log2 =
500 (b_width_log2_lookup[bsize] - b_width_log2_lookup[unit_size]) +
501 (b_height_log2_lookup[bsize] - b_height_log2_lookup[unit_size]);
502 const unsigned int sse_tx = sse >> num_blk_log2;
503 const unsigned int var_tx = var >> num_blk_log2;
505 x->skip_txfm[0] = SKIP_TXFM_NONE;
506 // Check if all ac coefficients can be quantized to zero.
507 if (var_tx < ac_thr || var == 0) {
508 x->skip_txfm[0] = SKIP_TXFM_AC_ONLY;
509 // Check if dc coefficient can be quantized to zero.
510 if (sse_tx - var_tx < dc_thr || sse == var)
511 x->skip_txfm[0] = SKIP_TXFM_AC_DC;
513 if (sse_tx - var_tx < dc_thr || sse == var)
518 if (x->skip_txfm[0] == SKIP_TXFM_AC_DC) {
520 *out_dist_sum = sse << 4;
525 #if CONFIG_VP9_HIGHBITDEPTH
526 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
527 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
528 dc_quant >> (xd->bd - 5), &rate, &dist);
530 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
531 dc_quant >> 3, &rate, &dist);
534 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
535 dc_quant >> 3, &rate, &dist);
536 #endif // CONFIG_VP9_HIGHBITDEPTH
540 *out_rate_sum = rate >> 1;
541 *out_dist_sum = dist << 3;
544 *out_dist_sum = (sse - var) << 4;
547 #if CONFIG_VP9_HIGHBITDEPTH
548 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
549 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
550 ac_quant >> (xd->bd - 5), &rate, &dist);
552 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
553 ac_quant >> 3, &rate, &dist);
556 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
557 ac_quant >> 3, &rate, &dist);
558 #endif // CONFIG_VP9_HIGHBITDEPTH
560 *out_rate_sum += rate;
561 *out_dist_sum += dist << 4;
564 #if CONFIG_VP9_HIGHBITDEPTH
565 static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, int64_t *dist,
566 int *skippable, int64_t *sse, int plane,
567 BLOCK_SIZE bsize, TX_SIZE tx_size) {
568 MACROBLOCKD *xd = &x->e_mbd;
569 unsigned int var_y, sse_y;
572 model_rd_for_sb_y(cpi, bsize, x, xd, rate, dist, &var_y, &sse_y);
578 static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, int64_t *dist,
579 int *skippable, int64_t *sse, int plane,
580 BLOCK_SIZE bsize, TX_SIZE tx_size) {
581 MACROBLOCKD *xd = &x->e_mbd;
582 const struct macroblockd_plane *pd = &xd->plane[plane];
583 const struct macroblock_plane *const p = &x->plane[plane];
584 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
585 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
586 const int step = 1 << (tx_size << 1);
587 const int block_step = (1 << tx_size);
589 int shift = tx_size == TX_32X32 ? 0 : 2;
590 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 :
591 xd->mb_to_right_edge >> (5 + pd->subsampling_x));
592 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 :
593 xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
597 vp9_subtract_plane(x, bsize, plane);
599 // Keep track of the row and column of the blocks we use so that we know
600 // if we are in the unrestricted motion border.
601 for (r = 0; r < max_blocks_high; r += block_step) {
602 for (c = 0; c < num_4x4_w; c += block_step) {
603 if (c < max_blocks_wide) {
604 const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
605 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
606 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
607 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
608 uint16_t *const eob = &p->eobs[block];
609 const int diff_stride = 4 * num_4x4_blocks_wide_lookup[bsize];
610 const int16_t *src_diff;
611 src_diff = &p->src_diff[(r * diff_stride + c) << 2];
615 vpx_fdct32x32_rd(src_diff, coeff, diff_stride);
616 vp9_quantize_fp_32x32(coeff, 1024, x->skip_block, p->zbin,
617 p->round_fp, p->quant_fp, p->quant_shift,
618 qcoeff, dqcoeff, pd->dequant, eob,
619 scan_order->scan, scan_order->iscan);
622 vpx_hadamard_16x16(src_diff, diff_stride, (int16_t *)coeff);
623 vp9_quantize_fp(coeff, 256, x->skip_block, p->zbin, p->round_fp,
624 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
626 scan_order->scan, scan_order->iscan);
629 vpx_hadamard_8x8(src_diff, diff_stride, (int16_t *)coeff);
630 vp9_quantize_fp(coeff, 64, x->skip_block, p->zbin, p->round_fp,
631 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
633 scan_order->scan, scan_order->iscan);
636 x->fwd_txm4x4(src_diff, coeff, diff_stride);
637 vp9_quantize_fp(coeff, 16, x->skip_block, p->zbin, p->round_fp,
638 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
640 scan_order->scan, scan_order->iscan);
646 *skippable &= (*eob == 0);
653 if (*skippable && *sse < INT64_MAX) {
655 *dist = (*sse << 6) >> shift;
663 if (*sse < INT64_MAX)
664 *sse = (*sse << 6) >> shift;
665 for (r = 0; r < max_blocks_high; r += block_step) {
666 for (c = 0; c < num_4x4_w; c += block_step) {
667 if (c < max_blocks_wide) {
668 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
669 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
670 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
671 uint16_t *const eob = &p->eobs[block];
674 *rate += (int)abs(qcoeff[0]);
676 *rate += vpx_satd((const int16_t *)qcoeff, step << 4);
678 *dist += vp9_block_error_fp(coeff, dqcoeff, step << 4) >> shift;
684 if (*skippable == 0) {
686 *rate += (eob_cost << 8);
691 static void model_rd_for_sb_uv(VP9_COMP *cpi, BLOCK_SIZE plane_bsize,
692 MACROBLOCK *x, MACROBLOCKD *xd,
693 int *out_rate_sum, int64_t *out_dist_sum,
694 unsigned int *var_y, unsigned int *sse_y,
695 int start_plane, int stop_plane) {
696 // Note our transform coeffs are 8 times an orthogonal transform.
697 // Hence quantizer step is also 8 times. To get effective quantizer
698 // we need to divide by 8 before sending to modeling function.
707 for (i = start_plane; i <= stop_plane; ++i) {
708 struct macroblock_plane *const p = &x->plane[i];
709 struct macroblockd_plane *const pd = &xd->plane[i];
710 const uint32_t dc_quant = pd->dequant[0];
711 const uint32_t ac_quant = pd->dequant[1];
712 const BLOCK_SIZE bs = plane_bsize;
715 if (!x->color_sensitivity[i - 1])
718 var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride,
719 pd->dst.buf, pd->dst.stride, &sse);
723 #if CONFIG_VP9_HIGHBITDEPTH
724 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
725 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
726 dc_quant >> (xd->bd - 5), &rate, &dist);
728 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
729 dc_quant >> 3, &rate, &dist);
732 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
733 dc_quant >> 3, &rate, &dist);
734 #endif // CONFIG_VP9_HIGHBITDEPTH
736 *out_rate_sum += rate >> 1;
737 *out_dist_sum += dist << 3;
739 #if CONFIG_VP9_HIGHBITDEPTH
740 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
741 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
742 ac_quant >> (xd->bd - 5), &rate, &dist);
744 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
745 ac_quant >> 3, &rate, &dist);
748 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
749 ac_quant >> 3, &rate, &dist);
750 #endif // CONFIG_VP9_HIGHBITDEPTH
752 *out_rate_sum += rate;
753 *out_dist_sum += dist << 4;
757 static int get_pred_buffer(PRED_BUFFER *p, int len) {
760 for (i = 0; i < len; i++) {
769 static void free_pred_buffer(PRED_BUFFER *p) {
774 static void encode_breakout_test(VP9_COMP *cpi, MACROBLOCK *x,
775 BLOCK_SIZE bsize, int mi_row, int mi_col,
776 MV_REFERENCE_FRAME ref_frame,
777 PREDICTION_MODE this_mode,
778 unsigned int var_y, unsigned int sse_y,
779 struct buf_2d yv12_mb[][MAX_MB_PLANE],
780 int *rate, int64_t *dist) {
781 MACROBLOCKD *xd = &x->e_mbd;
782 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
783 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
784 unsigned int var = var_y, sse = sse_y;
785 // Skipping threshold for ac.
786 unsigned int thresh_ac;
787 // Skipping threshold for dc.
788 unsigned int thresh_dc;
790 if (mbmi->mv[0].as_mv.row > 64 ||
791 mbmi->mv[0].as_mv.row < -64 ||
792 mbmi->mv[0].as_mv.col > 64 ||
793 mbmi->mv[0].as_mv.col < -64)
795 if (x->encode_breakout > 0 && motion_low == 1) {
796 // Set a maximum for threshold to avoid big PSNR loss in low bit rate
797 // case. Use extreme low threshold for static frames to limit
799 const unsigned int max_thresh = 36000;
800 // The encode_breakout input
801 const unsigned int min_thresh =
802 VPXMIN(((unsigned int)x->encode_breakout << 4), max_thresh);
803 #if CONFIG_VP9_HIGHBITDEPTH
804 const int shift = (xd->bd << 1) - 16;
807 // Calculate threshold according to dequant value.
808 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) >> 3;
809 #if CONFIG_VP9_HIGHBITDEPTH
810 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
811 thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
813 #endif // CONFIG_VP9_HIGHBITDEPTH
814 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
816 // Adjust ac threshold according to partition size.
818 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
820 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
821 #if CONFIG_VP9_HIGHBITDEPTH
822 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
823 thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
825 #endif // CONFIG_VP9_HIGHBITDEPTH
831 // Y skipping condition checking for ac and dc.
832 if (var <= thresh_ac && (sse - var) <= thresh_dc) {
833 unsigned int sse_u, sse_v;
834 unsigned int var_u, var_v;
836 // Skip UV prediction unless breakout is zero (lossless) to save
837 // computation with low impact on the result
838 if (x->encode_breakout == 0) {
839 xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
840 xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
841 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
844 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
845 x->plane[1].src.stride,
846 xd->plane[1].dst.buf,
847 xd->plane[1].dst.stride, &sse_u);
849 // U skipping condition checking
850 if (((var_u << 2) <= thresh_ac) && (sse_u - var_u <= thresh_dc)) {
851 var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf,
852 x->plane[2].src.stride,
853 xd->plane[2].dst.buf,
854 xd->plane[2].dst.stride, &sse_v);
856 // V skipping condition checking
857 if (((var_v << 2) <= thresh_ac) && (sse_v - var_v <= thresh_dc)) {
860 // The cost of skip bit needs to be added.
861 *rate = cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
862 [INTER_OFFSET(this_mode)];
864 // More on this part of rate
865 // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
867 // Scaling factor for SSE from spatial domain to frequency
868 // domain is 16. Adjust distortion accordingly.
869 // TODO(yunqingwang): In this function, only y-plane dist is
871 *dist = (sse << 4); // + ((sse_u + sse_v) << 4);
873 // *disable_skip = 1;
879 struct estimate_block_intra_args {
882 PREDICTION_MODE mode;
887 static void estimate_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
888 TX_SIZE tx_size, void *arg) {
889 struct estimate_block_intra_args* const args = arg;
890 VP9_COMP *const cpi = args->cpi;
891 MACROBLOCK *const x = args->x;
892 MACROBLOCKD *const xd = &x->e_mbd;
893 struct macroblock_plane *const p = &x->plane[0];
894 struct macroblockd_plane *const pd = &xd->plane[0];
895 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
896 uint8_t *const src_buf_base = p->src.buf;
897 uint8_t *const dst_buf_base = pd->dst.buf;
898 const int src_stride = p->src.stride;
899 const int dst_stride = pd->dst.stride;
904 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
906 p->src.buf = &src_buf_base[4 * (j * src_stride + i)];
907 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)];
908 // Use source buffer as an approximation for the fully reconstructed buffer.
909 vp9_predict_intra_block(xd, b_width_log2_lookup[plane_bsize],
911 x->skip_encode ? p->src.buf : pd->dst.buf,
912 x->skip_encode ? src_stride : dst_stride,
913 pd->dst.buf, dst_stride,
917 int64_t this_sse = INT64_MAX;
919 // TODO(jingning): This needs further refactoring.
920 block_yrd(cpi, x, &rate, &dist, &is_skippable, &this_sse, 0,
921 bsize_tx, VPXMIN(tx_size, TX_16X16));
922 x->skip_txfm[0] = is_skippable;
923 // TODO(jingning): Skip is signalled per prediciton block not per tx block.
924 rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), is_skippable);
926 unsigned int var, sse;
927 model_rd_for_sb_uv(cpi, plane_bsize, x, xd, &rate, &dist, &var, &sse,
931 p->src.buf = src_buf_base;
932 pd->dst.buf = dst_buf_base;
937 static const THR_MODES mode_idx[MAX_REF_FRAMES - 1][4] = {
938 {THR_DC, THR_V_PRED, THR_H_PRED, THR_TM},
939 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV},
940 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG},
943 static const PREDICTION_MODE intra_mode_list[] = {
944 DC_PRED, V_PRED, H_PRED, TM_PRED
947 static int mode_offset(const PREDICTION_MODE mode) {
948 if (mode >= NEARESTMV) {
949 return INTER_OFFSET(mode);
966 static INLINE void update_thresh_freq_fact(VP9_COMP *cpi,
967 TileDataEnc *tile_data,
969 MV_REFERENCE_FRAME ref_frame,
970 THR_MODES best_mode_idx,
971 PREDICTION_MODE mode) {
972 THR_MODES thr_mode_idx = mode_idx[ref_frame][mode_offset(mode)];
973 int *freq_fact = &tile_data->thresh_freq_fact[bsize][thr_mode_idx];
974 if (thr_mode_idx == best_mode_idx)
975 *freq_fact -= (*freq_fact >> 4);
977 *freq_fact = VPXMIN(*freq_fact + RD_THRESH_INC,
978 cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
981 void vp9_pick_intra_mode(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
982 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
983 MACROBLOCKD *const xd = &x->e_mbd;
984 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
985 RD_COST this_rdc, best_rdc;
986 PREDICTION_MODE this_mode;
987 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
988 const TX_SIZE intra_tx_size =
989 VPXMIN(max_txsize_lookup[bsize],
990 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
991 MODE_INFO *const mic = xd->mi[0];
993 const MODE_INFO *above_mi = xd->mi[-xd->mi_stride];
994 const MODE_INFO *left_mi = xd->left_available ? xd->mi[-1] : NULL;
995 const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
996 const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
997 bmode_costs = cpi->y_mode_costs[A][L];
1000 vp9_rd_cost_reset(&best_rdc);
1001 vp9_rd_cost_reset(&this_rdc);
1003 mbmi->ref_frame[0] = INTRA_FRAME;
1004 mbmi->mv[0].as_int = INVALID_MV;
1005 mbmi->uv_mode = DC_PRED;
1006 memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
1008 // Change the limit of this loop to add other intra prediction
1010 for (this_mode = DC_PRED; this_mode <= H_PRED; ++this_mode) {
1011 args.mode = this_mode;
1014 mbmi->tx_size = intra_tx_size;
1015 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
1016 estimate_block_intra, &args);
1017 this_rdc.rate = args.rate;
1018 this_rdc.dist = args.dist;
1019 this_rdc.rate += bmode_costs[this_mode];
1020 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1021 this_rdc.rate, this_rdc.dist);
1023 if (this_rdc.rdcost < best_rdc.rdcost) {
1024 best_rdc = this_rdc;
1025 mbmi->mode = this_mode;
1029 *rd_cost = best_rdc;
1032 static void init_ref_frame_cost(VP9_COMMON *const cm,
1033 MACROBLOCKD *const xd,
1034 int ref_frame_cost[MAX_REF_FRAMES]) {
1035 vpx_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
1036 vpx_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
1037 vpx_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
1039 ref_frame_cost[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
1040 ref_frame_cost[LAST_FRAME] = ref_frame_cost[GOLDEN_FRAME] =
1041 ref_frame_cost[ALTREF_FRAME] = vp9_cost_bit(intra_inter_p, 1);
1043 ref_frame_cost[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
1044 ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1045 ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
1046 ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
1047 ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
1051 MV_REFERENCE_FRAME ref_frame;
1052 PREDICTION_MODE pred_mode;
1055 #define RT_INTER_MODES 8
1056 static const REF_MODE ref_mode_set[RT_INTER_MODES] = {
1057 {LAST_FRAME, ZEROMV},
1058 {LAST_FRAME, NEARESTMV},
1059 {GOLDEN_FRAME, ZEROMV},
1060 {LAST_FRAME, NEARMV},
1061 {LAST_FRAME, NEWMV},
1062 {GOLDEN_FRAME, NEARESTMV},
1063 {GOLDEN_FRAME, NEARMV},
1064 {GOLDEN_FRAME, NEWMV}
1066 static const REF_MODE ref_mode_set_svc[RT_INTER_MODES] = {
1067 {LAST_FRAME, ZEROMV},
1068 {GOLDEN_FRAME, ZEROMV},
1069 {LAST_FRAME, NEARESTMV},
1070 {LAST_FRAME, NEARMV},
1071 {GOLDEN_FRAME, NEARESTMV},
1072 {GOLDEN_FRAME, NEARMV},
1073 {LAST_FRAME, NEWMV},
1074 {GOLDEN_FRAME, NEWMV}
1077 int set_intra_cost_penalty(const VP9_COMP *const cpi, BLOCK_SIZE bsize) {
1078 const VP9_COMMON *const cm = &cpi->common;
1079 // Reduce the intra cost penalty for small blocks (<=16x16).
1081 (bsize <= BLOCK_16X16) ? ((bsize <= BLOCK_8X8) ? 4 : 2) : 0;
1082 if (cpi->noise_estimate.enabled && cpi->noise_estimate.level == kHigh)
1083 // Don't reduce intra cost penalty if estimated noise level is high.
1085 return vp9_get_intra_cost_penalty(
1086 cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth) >> reduction_fac;
1089 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
1090 // this needs various further optimizations. to be continued..
1091 void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
1092 TileDataEnc *tile_data,
1093 int mi_row, int mi_col, RD_COST *rd_cost,
1094 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1095 VP9_COMMON *const cm = &cpi->common;
1096 SPEED_FEATURES *const sf = &cpi->sf;
1097 const SVC *const svc = &cpi->svc;
1098 TileInfo *const tile_info = &tile_data->tile_info;
1099 MACROBLOCKD *const xd = &x->e_mbd;
1100 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1101 struct macroblockd_plane *const pd = &xd->plane[0];
1102 PREDICTION_MODE best_mode = ZEROMV;
1103 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
1104 MV_REFERENCE_FRAME usable_ref_frame;
1105 TX_SIZE best_tx_size = TX_SIZES;
1106 INTERP_FILTER best_pred_filter = EIGHTTAP;
1107 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
1108 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
1109 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1111 RD_COST this_rdc, best_rdc;
1112 uint8_t skip_txfm = SKIP_TXFM_NONE, best_mode_skip_txfm = SKIP_TXFM_NONE;
1113 // var_y and sse_y are saved to be used in skipping checking
1114 unsigned int var_y = UINT_MAX;
1115 unsigned int sse_y = UINT_MAX;
1116 const int intra_cost_penalty = set_intra_cost_penalty(cpi, bsize);
1117 const int64_t inter_mode_thresh = RDCOST(x->rdmult, x->rddiv,
1118 intra_cost_penalty, 0);
1119 const int *const rd_threshes = cpi->rd.threshes[mbmi->segment_id][bsize];
1120 const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
1121 INTERP_FILTER filter_ref;
1122 const int bsl = mi_width_log2_lookup[bsize];
1123 const int pred_filter_search = cm->interp_filter == SWITCHABLE ?
1124 (((mi_row + mi_col) >> bsl) +
1125 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0;
1126 int const_motion[MAX_REF_FRAMES] = { 0 };
1127 const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
1128 const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
1129 // For speed 6, the result of interp filter is reused later in actual encoding
1131 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
1133 DECLARE_ALIGNED(16, uint8_t, pred_buf[3 * 64 * 64]);
1134 #if CONFIG_VP9_HIGHBITDEPTH
1135 DECLARE_ALIGNED(16, uint16_t, pred_buf_16[3 * 64 * 64]);
1137 struct buf_2d orig_dst = pd->dst;
1138 PRED_BUFFER *best_pred = NULL;
1139 PRED_BUFFER *this_mode_pred = NULL;
1140 const int pixels_in_block = bh * bw;
1141 int reuse_inter_pred = cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready;
1142 int ref_frame_skip_mask = 0;
1144 int best_pred_sad = INT_MAX;
1145 int best_early_term = 0;
1146 int ref_frame_cost[MAX_REF_FRAMES];
1147 int svc_force_zero_mode[3] = {0};
1148 #if CONFIG_VP9_TEMPORAL_DENOISING
1149 int64_t zero_last_cost_orig = INT64_MAX;
1152 init_ref_frame_cost(cm, xd, ref_frame_cost);
1154 if (reuse_inter_pred) {
1156 for (i = 0; i < 3; i++) {
1157 #if CONFIG_VP9_HIGHBITDEPTH
1158 if (cm->use_highbitdepth)
1159 tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
1161 tmp[i].data = &pred_buf[pixels_in_block * i];
1163 tmp[i].data = &pred_buf[pixels_in_block * i];
1164 #endif // CONFIG_VP9_HIGHBITDEPTH
1168 tmp[3].data = pd->dst.buf;
1169 tmp[3].stride = pd->dst.stride;
1173 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
1176 if (xd->up_available)
1177 filter_ref = xd->mi[-xd->mi_stride]->mbmi.interp_filter;
1178 else if (xd->left_available)
1179 filter_ref = xd->mi[-1]->mbmi.interp_filter;
1181 filter_ref = cm->interp_filter;
1183 // initialize mode decisions
1184 vp9_rd_cost_reset(&best_rdc);
1185 vp9_rd_cost_reset(rd_cost);
1186 mbmi->sb_type = bsize;
1187 mbmi->ref_frame[0] = NONE;
1188 mbmi->ref_frame[1] = NONE;
1189 mbmi->tx_size = VPXMIN(max_txsize_lookup[bsize],
1190 tx_mode_to_biggest_tx_size[cm->tx_mode]);
1192 #if CONFIG_VP9_TEMPORAL_DENOISING
1193 vp9_denoiser_reset_frame_stats(ctx);
1196 if (cpi->rc.frames_since_golden == 0 && !cpi->use_svc) {
1197 usable_ref_frame = LAST_FRAME;
1199 usable_ref_frame = GOLDEN_FRAME;
1202 // If the reference is temporally aligned with current superframe
1203 // (e.g., spatial reference within superframe), constrain the inter mode:
1204 // for now only test zero motion.
1205 if (cpi->use_svc && svc ->force_zero_mode_spatial_ref) {
1206 if (svc->ref_frame_index[cpi->lst_fb_idx] == svc->current_superframe)
1207 svc_force_zero_mode[LAST_FRAME - 1] = 1;
1208 if (svc->ref_frame_index[cpi->gld_fb_idx] == svc->current_superframe)
1209 svc_force_zero_mode[GOLDEN_FRAME - 1] = 1;
1212 for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) {
1213 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1215 x->pred_mv_sad[ref_frame] = INT_MAX;
1216 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
1217 frame_mv[ZEROMV][ref_frame].as_int = 0;
1219 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
1220 int_mv *const candidates = x->mbmi_ext->ref_mvs[ref_frame];
1221 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
1223 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
1226 if (cm->use_prev_frame_mvs)
1227 vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame,
1228 candidates, mi_row, mi_col, NULL, NULL,
1229 x->mbmi_ext->mode_context);
1231 const_motion[ref_frame] = mv_refs_rt(cm, x, xd, tile_info,
1233 ref_frame, candidates,
1236 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1237 &frame_mv[NEARESTMV][ref_frame],
1238 &frame_mv[NEARMV][ref_frame]);
1240 if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8)
1241 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
1244 ref_frame_skip_mask |= (1 << ref_frame);
1248 for (idx = 0; idx < RT_INTER_MODES; ++idx) {
1255 int this_early_term = 0;
1256 PREDICTION_MODE this_mode = ref_mode_set[idx].pred_mode;
1258 this_mode = ref_mode_set_svc[idx].pred_mode;
1260 if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode)))
1263 ref_frame = ref_mode_set[idx].ref_frame;
1265 ref_frame = ref_mode_set_svc[idx].ref_frame;
1268 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
1270 if (const_motion[ref_frame] && this_mode == NEARMV)
1274 if (svc_force_zero_mode[ref_frame - 1] &&
1275 frame_mv[this_mode][ref_frame].as_int != 0)
1279 if (!(frame_mv[this_mode][ref_frame].as_int == 0 &&
1280 ref_frame == LAST_FRAME)) {
1281 i = (ref_frame == LAST_FRAME) ? GOLDEN_FRAME : LAST_FRAME;
1282 if ((cpi->ref_frame_flags & flag_list[i]) && sf->reference_masking)
1283 if (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[i] << 1))
1284 ref_frame_skip_mask |= (1 << ref_frame);
1286 if (ref_frame_skip_mask & (1 << ref_frame))
1289 // Select prediction reference frames.
1290 for (i = 0; i < MAX_MB_PLANE; i++)
1291 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
1293 mbmi->ref_frame[0] = ref_frame;
1294 set_ref_ptrs(cm, xd, ref_frame, NONE);
1296 mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
1297 mode_rd_thresh = best_mode_skip_txfm ?
1298 rd_threshes[mode_index] << 1 : rd_threshes[mode_index];
1299 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
1300 rd_thresh_freq_fact[mode_index]))
1303 if (this_mode == NEWMV) {
1304 if (ref_frame > LAST_FRAME && !cpi->use_svc) {
1306 int dis, cost_list[5];
1308 if (bsize < BLOCK_16X16)
1311 tmp_sad = vp9_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
1313 if (tmp_sad > x->pred_mv_sad[LAST_FRAME])
1315 if (tmp_sad + (num_pels_log2_lookup[bsize] << 4) > best_pred_sad)
1318 frame_mv[NEWMV][ref_frame].as_int = mbmi->mv[0].as_int;
1319 rate_mv = vp9_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv,
1320 &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1321 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
1322 frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
1323 frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
1325 cpi->find_fractional_mv_step(x, &frame_mv[NEWMV][ref_frame].as_mv,
1326 &x->mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1327 cpi->common.allow_high_precision_mv,
1329 &cpi->fn_ptr[bsize],
1330 cpi->sf.mv.subpel_force_stop,
1331 cpi->sf.mv.subpel_iters_per_step,
1332 cond_cost_list(cpi, cost_list),
1333 x->nmvjointcost, x->mvcost, &dis,
1334 &x->pred_sse[ref_frame], NULL, 0, 0);
1335 } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
1336 &frame_mv[NEWMV][ref_frame], &rate_mv, best_rdc.rdcost)) {
1341 if (this_mode == NEWMV && ref_frame == LAST_FRAME &&
1342 frame_mv[NEWMV][LAST_FRAME].as_int != INVALID_MV) {
1343 const int pre_stride = xd->plane[0].pre[0].stride;
1344 const uint8_t * const pre_buf = xd->plane[0].pre[0].buf +
1345 (frame_mv[NEWMV][LAST_FRAME].as_mv.row >> 3) * pre_stride +
1346 (frame_mv[NEWMV][LAST_FRAME].as_mv.col >> 3);
1347 best_pred_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
1348 x->plane[0].src.stride,
1349 pre_buf, pre_stride);
1350 x->pred_mv_sad[LAST_FRAME] = best_pred_sad;
1354 if (this_mode == NEWMV && ref_frame == GOLDEN_FRAME &&
1355 frame_mv[NEWMV][GOLDEN_FRAME].as_int != INVALID_MV) {
1356 const int pre_stride = xd->plane[0].pre[0].stride;
1357 const uint8_t * const pre_buf = xd->plane[0].pre[0].buf +
1358 (frame_mv[NEWMV][GOLDEN_FRAME].as_mv.row >> 3) * pre_stride +
1359 (frame_mv[NEWMV][GOLDEN_FRAME].as_mv.col >> 3);
1360 best_pred_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
1361 x->plane[0].src.stride,
1362 pre_buf, pre_stride);
1363 x->pred_mv_sad[GOLDEN_FRAME] = best_pred_sad;
1368 if (this_mode != NEARESTMV &&
1369 frame_mv[this_mode][ref_frame].as_int ==
1370 frame_mv[NEARESTMV][ref_frame].as_int)
1373 mbmi->mode = this_mode;
1374 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
1376 // Search for the best prediction filter type, when the resulting
1377 // motion vector is at sub-pixel accuracy level for luma component, i.e.,
1378 // the last three bits are all zeros.
1379 if (reuse_inter_pred) {
1380 if (!this_mode_pred) {
1381 this_mode_pred = &tmp[3];
1383 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1384 pd->dst.buf = this_mode_pred->data;
1385 pd->dst.stride = bw;
1389 if ((this_mode == NEWMV || filter_ref == SWITCHABLE) && pred_filter_search
1390 && (ref_frame == LAST_FRAME ||
1391 (ref_frame == GOLDEN_FRAME && cpi->use_svc))
1392 && (((mbmi->mv[0].as_mv.row | mbmi->mv[0].as_mv.col) & 0x07) != 0)) {
1395 unsigned int pf_var[3];
1396 unsigned int pf_sse[3];
1397 TX_SIZE pf_tx_size[3];
1398 int64_t best_cost = INT64_MAX;
1399 INTERP_FILTER best_filter = SWITCHABLE, filter;
1400 PRED_BUFFER *current_pred = this_mode_pred;
1402 for (filter = EIGHTTAP; filter <= EIGHTTAP_SMOOTH; ++filter) {
1404 mbmi->interp_filter = filter;
1405 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1406 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter], &pf_dist[filter],
1407 &pf_var[filter], &pf_sse[filter]);
1408 pf_rate[filter] += vp9_get_switchable_rate(cpi, xd);
1409 cost = RDCOST(x->rdmult, x->rddiv, pf_rate[filter], pf_dist[filter]);
1410 pf_tx_size[filter] = mbmi->tx_size;
1411 if (cost < best_cost) {
1412 best_filter = filter;
1414 skip_txfm = x->skip_txfm[0];
1416 if (reuse_inter_pred) {
1417 if (this_mode_pred != current_pred) {
1418 free_pred_buffer(this_mode_pred);
1419 this_mode_pred = current_pred;
1422 if (filter < EIGHTTAP_SHARP) {
1423 current_pred = &tmp[get_pred_buffer(tmp, 3)];
1424 pd->dst.buf = current_pred->data;
1425 pd->dst.stride = bw;
1431 if (reuse_inter_pred && this_mode_pred != current_pred)
1432 free_pred_buffer(current_pred);
1434 mbmi->interp_filter = best_filter;
1435 mbmi->tx_size = pf_tx_size[best_filter];
1436 this_rdc.rate = pf_rate[best_filter];
1437 this_rdc.dist = pf_dist[best_filter];
1438 var_y = pf_var[best_filter];
1439 sse_y = pf_sse[best_filter];
1440 x->skip_txfm[0] = skip_txfm;
1441 if (reuse_inter_pred) {
1442 pd->dst.buf = this_mode_pred->data;
1443 pd->dst.stride = this_mode_pred->stride;
1446 mbmi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP : filter_ref;
1447 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1449 // For large partition blocks, extra testing is done.
1450 if (bsize > BLOCK_32X32 &&
1451 !cyclic_refresh_segment_id_boosted(xd->mi[0]->mbmi.segment_id) &&
1453 model_rd_for_sb_y_large(cpi, bsize, x, xd, &this_rdc.rate,
1454 &this_rdc.dist, &var_y, &sse_y, mi_row, mi_col,
1457 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
1462 if (!this_early_term) {
1463 this_sse = (int64_t)sse_y;
1464 block_yrd(cpi, x, &this_rdc.rate, &this_rdc.dist, &is_skippable,
1465 &this_sse, 0, bsize, VPXMIN(mbmi->tx_size, TX_16X16));
1466 x->skip_txfm[0] = is_skippable;
1468 this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1470 if (RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist) <
1471 RDCOST(x->rdmult, x->rddiv, 0, this_sse)) {
1472 this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
1474 this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1475 this_rdc.dist = this_sse;
1476 x->skip_txfm[0] = SKIP_TXFM_AC_DC;
1480 if (cm->interp_filter == SWITCHABLE) {
1481 if ((mbmi->mv[0].as_mv.row | mbmi->mv[0].as_mv.col) & 0x07)
1482 this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
1485 this_rdc.rate += cm->interp_filter == SWITCHABLE ?
1486 vp9_get_switchable_rate(cpi, xd) : 0;
1487 this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1490 if (x->color_sensitivity[0] || x->color_sensitivity[1]) {
1492 int64_t uv_dist = 0;
1493 const BLOCK_SIZE uv_bsize = get_plane_block_size(bsize, &xd->plane[1]);
1494 if (x->color_sensitivity[0])
1495 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 1);
1496 if (x->color_sensitivity[1])
1497 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 2);
1498 model_rd_for_sb_uv(cpi, uv_bsize, x, xd, &uv_rate, &uv_dist,
1499 &var_y, &sse_y, 1, 2);
1500 this_rdc.rate += uv_rate;
1501 this_rdc.dist += uv_dist;
1504 this_rdc.rate += rate_mv;
1506 cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]][INTER_OFFSET(
1508 this_rdc.rate += ref_frame_cost[ref_frame];
1509 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
1511 if (cpi->oxcf.speed >= 5 &&
1512 cpi->oxcf.content != VP9E_CONTENT_SCREEN) {
1513 // Bias against non-zero (above some threshold) motion for large blocks.
1514 // This is temporary fix to avoid selection of large mv for big blocks.
1515 if (frame_mv[this_mode][ref_frame].as_mv.row > 64 ||
1516 frame_mv[this_mode][ref_frame].as_mv.row < -64 ||
1517 frame_mv[this_mode][ref_frame].as_mv.col > 64 ||
1518 frame_mv[this_mode][ref_frame].as_mv.col < -64) {
1519 if (bsize == BLOCK_64X64)
1520 this_rdc.rdcost = this_rdc.rdcost << 1;
1521 else if (bsize >= BLOCK_32X32)
1522 this_rdc.rdcost = 3 * this_rdc.rdcost >> 1;
1524 // If noise estimation is enabled, and estimated level is above threshold,
1525 // add a bias to LAST reference with small motion, for large blocks.
1526 if (cpi->noise_estimate.enabled &&
1527 cpi->noise_estimate.level >= kMedium &&
1528 bsize >= BLOCK_32X32 &&
1529 ref_frame == LAST_FRAME &&
1530 frame_mv[this_mode][ref_frame].as_mv.row < 8 &&
1531 frame_mv[this_mode][ref_frame].as_mv.row > -8 &&
1532 frame_mv[this_mode][ref_frame].as_mv.col < 8 &&
1533 frame_mv[this_mode][ref_frame].as_mv.col > -8)
1534 this_rdc.rdcost = 7 * this_rdc.rdcost >> 3;
1537 // Skipping checking: test to see if this block can be reconstructed by
1539 if (cpi->allow_encode_breakout) {
1540 encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame, this_mode,
1541 var_y, sse_y, yv12_mb, &this_rdc.rate,
1544 this_rdc.rate += rate_mv;
1545 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate,
1550 #if CONFIG_VP9_TEMPORAL_DENOISING
1551 if (cpi->oxcf.noise_sensitivity > 0) {
1552 vp9_denoiser_update_frame_stats(mbmi, sse_y, this_mode, ctx);
1553 // Keep track of zero_last cost.
1554 if (ref_frame == LAST_FRAME && frame_mv[this_mode][ref_frame].as_int == 0)
1555 zero_last_cost_orig = this_rdc.rdcost;
1561 if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
1562 best_rdc = this_rdc;
1563 best_mode = this_mode;
1564 best_pred_filter = mbmi->interp_filter;
1565 best_tx_size = mbmi->tx_size;
1566 best_ref_frame = ref_frame;
1567 best_mode_skip_txfm = x->skip_txfm[0];
1568 best_early_term = this_early_term;
1570 if (reuse_inter_pred) {
1571 free_pred_buffer(best_pred);
1572 best_pred = this_mode_pred;
1575 if (reuse_inter_pred)
1576 free_pred_buffer(this_mode_pred);
1582 // If early termination flag is 1 and at least 2 modes are checked,
1583 // the mode search is terminated.
1584 if (best_early_term && idx > 0) {
1590 mbmi->mode = best_mode;
1591 mbmi->interp_filter = best_pred_filter;
1592 mbmi->tx_size = best_tx_size;
1593 mbmi->ref_frame[0] = best_ref_frame;
1594 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
1595 xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
1596 x->skip_txfm[0] = best_mode_skip_txfm;
1598 // Perform intra prediction search, if the best SAD is above a certain
1600 if (best_rdc.rdcost == INT64_MAX ||
1601 (!x->skip && best_rdc.rdcost > inter_mode_thresh &&
1602 bsize <= cpi->sf.max_intra_bsize)) {
1603 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
1605 TX_SIZE best_intra_tx_size = TX_SIZES;
1606 TX_SIZE intra_tx_size =
1607 VPXMIN(max_txsize_lookup[bsize],
1608 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
1609 if (cpi->oxcf.content != VP9E_CONTENT_SCREEN && intra_tx_size > TX_16X16)
1610 intra_tx_size = TX_16X16;
1612 if (reuse_inter_pred && best_pred != NULL) {
1613 if (best_pred->data == orig_dst.buf) {
1614 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1615 #if CONFIG_VP9_HIGHBITDEPTH
1616 if (cm->use_highbitdepth)
1617 vpx_highbd_convolve_copy(best_pred->data, best_pred->stride,
1618 this_mode_pred->data, this_mode_pred->stride,
1619 NULL, 0, NULL, 0, bw, bh, xd->bd);
1621 vpx_convolve_copy(best_pred->data, best_pred->stride,
1622 this_mode_pred->data, this_mode_pred->stride,
1623 NULL, 0, NULL, 0, bw, bh);
1625 vpx_convolve_copy(best_pred->data, best_pred->stride,
1626 this_mode_pred->data, this_mode_pred->stride,
1627 NULL, 0, NULL, 0, bw, bh);
1628 #endif // CONFIG_VP9_HIGHBITDEPTH
1629 best_pred = this_mode_pred;
1634 for (i = 0; i < 4; ++i) {
1635 const PREDICTION_MODE this_mode = intra_mode_list[i];
1636 THR_MODES mode_index = mode_idx[INTRA_FRAME][mode_offset(this_mode)];
1637 int mode_rd_thresh = rd_threshes[mode_index];
1639 if (!((1 << this_mode) & cpi->sf.intra_y_mode_bsize_mask[bsize]))
1642 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
1643 rd_thresh_freq_fact[mode_index]))
1646 mbmi->mode = this_mode;
1647 mbmi->ref_frame[0] = INTRA_FRAME;
1648 args.mode = this_mode;
1651 mbmi->tx_size = intra_tx_size;
1652 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
1653 estimate_block_intra, &args);
1654 // Inter and intra RD will mismatch in scale for non-screen content.
1655 if (cpi->oxcf.content == VP9E_CONTENT_SCREEN) {
1656 if (x->color_sensitivity[0])
1657 vp9_foreach_transformed_block_in_plane(xd, bsize, 1,
1658 estimate_block_intra, &args);
1659 if (x->color_sensitivity[1])
1660 vp9_foreach_transformed_block_in_plane(xd, bsize, 2,
1661 estimate_block_intra, &args);
1663 this_rdc.rate = args.rate;
1664 this_rdc.dist = args.dist;
1665 this_rdc.rate += cpi->mbmode_cost[this_mode];
1666 this_rdc.rate += ref_frame_cost[INTRA_FRAME];
1667 this_rdc.rate += intra_cost_penalty;
1668 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1669 this_rdc.rate, this_rdc.dist);
1671 if (this_rdc.rdcost < best_rdc.rdcost) {
1672 best_rdc = this_rdc;
1673 best_mode = this_mode;
1674 best_intra_tx_size = mbmi->tx_size;
1675 best_ref_frame = INTRA_FRAME;
1676 mbmi->uv_mode = this_mode;
1677 mbmi->mv[0].as_int = INVALID_MV;
1678 best_mode_skip_txfm = x->skip_txfm[0];
1682 // Reset mb_mode_info to the best inter mode.
1683 if (best_ref_frame != INTRA_FRAME) {
1684 mbmi->tx_size = best_tx_size;
1686 mbmi->tx_size = best_intra_tx_size;
1691 mbmi->mode = best_mode;
1692 mbmi->ref_frame[0] = best_ref_frame;
1693 x->skip_txfm[0] = best_mode_skip_txfm;
1695 if (reuse_inter_pred && best_pred != NULL) {
1696 if (best_pred->data != orig_dst.buf && is_inter_mode(mbmi->mode)) {
1697 #if CONFIG_VP9_HIGHBITDEPTH
1698 if (cm->use_highbitdepth)
1699 vpx_highbd_convolve_copy(best_pred->data, best_pred->stride,
1700 pd->dst.buf, pd->dst.stride, NULL, 0,
1701 NULL, 0, bw, bh, xd->bd);
1703 vpx_convolve_copy(best_pred->data, best_pred->stride,
1704 pd->dst.buf, pd->dst.stride, NULL, 0,
1707 vpx_convolve_copy(best_pred->data, best_pred->stride,
1708 pd->dst.buf, pd->dst.stride, NULL, 0,
1710 #endif // CONFIG_VP9_HIGHBITDEPTH
1714 #if CONFIG_VP9_TEMPORAL_DENOISING
1715 if (cpi->oxcf.noise_sensitivity > 0 &&
1716 cpi->resize_pending == 0) {
1717 VP9_DENOISER_DECISION decision = COPY_BLOCK;
1718 vp9_denoiser_denoise(&cpi->denoiser, x, mi_row, mi_col,
1719 VPXMAX(BLOCK_8X8, bsize), ctx, &decision);
1720 // If INTRA or GOLDEN reference was selected, re-evaluate ZEROMV on denoised
1721 // result. Only do this under noise conditions, and if rdcost of ZEROMV on
1722 // original source is not significantly higher than rdcost of best mode.
1723 if (((best_ref_frame == INTRA_FRAME && decision >= FILTER_BLOCK) ||
1724 (best_ref_frame == GOLDEN_FRAME && decision == FILTER_ZEROMV_BLOCK)) &&
1725 cpi->noise_estimate.enabled &&
1726 cpi->noise_estimate.level > kLow &&
1727 zero_last_cost_orig < (best_rdc.rdcost << 3)) {
1728 // Check if we should pick ZEROMV on denoised signal.
1731 mbmi->mode = ZEROMV;
1732 mbmi->ref_frame[0] = LAST_FRAME;
1733 mbmi->ref_frame[1] = NONE;
1734 mbmi->mv[0].as_int = 0;
1735 mbmi->interp_filter = EIGHTTAP;
1736 xd->plane[0].pre[0] = yv12_mb[LAST_FRAME][0];
1737 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1738 model_rd_for_sb_y(cpi, bsize, x, xd, &rate, &dist, &var_y, &sse_y);
1739 this_rdc.rate = rate + ref_frame_cost[LAST_FRAME] +
1740 cpi->inter_mode_cost[x->mbmi_ext->mode_context[LAST_FRAME]]
1741 [INTER_OFFSET(ZEROMV)];
1742 this_rdc.dist = dist;
1743 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, rate, dist);
1744 // Switch to ZEROMV if the rdcost for ZEROMV on denoised source
1745 // is lower than best_ref mode (on original source).
1746 if (this_rdc.rdcost > best_rdc.rdcost) {
1747 this_rdc = best_rdc;
1748 mbmi->mode = best_mode;
1749 mbmi->ref_frame[0] = best_ref_frame;
1750 mbmi->interp_filter = best_pred_filter;
1751 if (best_ref_frame == INTRA_FRAME)
1752 mbmi->mv[0].as_int = INVALID_MV;
1753 else if (best_ref_frame == GOLDEN_FRAME) {
1754 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
1755 if (reuse_inter_pred) {
1756 xd->plane[0].pre[0] = yv12_mb[GOLDEN_FRAME][0];
1757 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1760 mbmi->tx_size = best_tx_size;
1761 x->skip_txfm[0] = best_mode_skip_txfm;
1763 best_ref_frame = LAST_FRAME;
1764 best_rdc = this_rdc;
1770 if (cpi->sf.adaptive_rd_thresh) {
1771 THR_MODES best_mode_idx = mode_idx[best_ref_frame][mode_offset(mbmi->mode)];
1773 if (best_ref_frame == INTRA_FRAME) {
1774 // Only consider the modes that are included in the intra_mode_list.
1775 int intra_modes = sizeof(intra_mode_list)/sizeof(PREDICTION_MODE);
1778 // TODO(yunqingwang): Check intra mode mask and only update freq_fact
1779 // for those valid modes.
1780 for (i = 0; i < intra_modes; i++) {
1781 update_thresh_freq_fact(cpi, tile_data, bsize, INTRA_FRAME,
1782 best_mode_idx, intra_mode_list[i]);
1785 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1786 PREDICTION_MODE this_mode;
1787 if (best_ref_frame != ref_frame) continue;
1788 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1789 update_thresh_freq_fact(cpi, tile_data, bsize, ref_frame,
1790 best_mode_idx, this_mode);
1796 *rd_cost = best_rdc;
1799 void vp9_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x,
1800 int mi_row, int mi_col, RD_COST *rd_cost,
1801 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1802 VP9_COMMON *const cm = &cpi->common;
1803 SPEED_FEATURES *const sf = &cpi->sf;
1804 MACROBLOCKD *const xd = &x->e_mbd;
1805 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1806 MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1807 const struct segmentation *const seg = &cm->seg;
1808 MV_REFERENCE_FRAME ref_frame, second_ref_frame = NONE;
1809 MV_REFERENCE_FRAME best_ref_frame = NONE;
1810 unsigned char segment_id = mbmi->segment_id;
1811 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
1812 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1814 int64_t best_rd = INT64_MAX;
1815 b_mode_info bsi[MAX_REF_FRAMES][4];
1816 int ref_frame_skip_mask = 0;
1817 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1818 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1821 x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
1822 ctx->pred_pixel_ready = 0;
1824 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1825 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1827 x->pred_mv_sad[ref_frame] = INT_MAX;
1829 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
1830 int_mv *const candidates = mbmi_ext->ref_mvs[ref_frame];
1831 const struct scale_factors *const sf =
1832 &cm->frame_refs[ref_frame - 1].sf;
1833 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
1835 vp9_find_mv_refs(cm, xd, xd->mi[0], ref_frame,
1836 candidates, mi_row, mi_col, NULL, NULL,
1837 mbmi_ext->mode_context);
1839 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1840 &dummy_mv[0], &dummy_mv[1]);
1842 ref_frame_skip_mask |= (1 << ref_frame);
1846 mbmi->sb_type = bsize;
1847 mbmi->tx_size = TX_4X4;
1848 mbmi->uv_mode = DC_PRED;
1849 mbmi->ref_frame[0] = LAST_FRAME;
1850 mbmi->ref_frame[1] = NONE;
1851 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP
1852 : cm->interp_filter;
1854 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1855 int64_t this_rd = 0;
1858 if (ref_frame_skip_mask & (1 << ref_frame))
1861 // TODO(jingning, agrange): Scaling reference frame not supported for
1862 // sub8x8 blocks. Is this supported now?
1863 if (ref_frame > INTRA_FRAME &&
1864 vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
1867 // If the segment reference frame feature is enabled....
1868 // then do nothing if the current ref frame is not allowed..
1869 if (segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
1870 get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
1873 mbmi->ref_frame[0] = ref_frame;
1875 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
1877 // Select prediction reference frames.
1878 for (plane = 0; plane < MAX_MB_PLANE; plane++)
1879 xd->plane[plane].pre[0] = yv12_mb[ref_frame][plane];
1881 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1882 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1883 int_mv b_mv[MB_MODE_COUNT];
1884 int64_t b_best_rd = INT64_MAX;
1885 const int i = idy * 2 + idx;
1886 PREDICTION_MODE this_mode;
1888 unsigned int var_y, sse_y;
1890 struct macroblock_plane *p = &x->plane[0];
1891 struct macroblockd_plane *pd = &xd->plane[0];
1893 const struct buf_2d orig_src = p->src;
1894 const struct buf_2d orig_dst = pd->dst;
1895 struct buf_2d orig_pre[2];
1896 memcpy(orig_pre, xd->plane[0].pre, sizeof(orig_pre));
1898 // set buffer pointers for sub8x8 motion search.
1900 &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
1902 &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
1904 &pd->pre[0].buf[vp9_raster_block_offset(BLOCK_8X8,
1905 i, pd->pre[0].stride)];
1907 b_mv[ZEROMV].as_int = 0;
1908 b_mv[NEWMV].as_int = INVALID_MV;
1909 vp9_append_sub8x8_mvs_for_idx(cm, xd, i, 0, mi_row, mi_col,
1912 mbmi_ext->mode_context);
1914 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1916 xd->mi[0]->bmi[i].as_mv[0].as_int = b_mv[this_mode].as_int;
1918 if (this_mode == NEWMV) {
1919 const int step_param = cpi->sf.mv.fullpel_search_step_param;
1923 const int tmp_col_min = x->mv_col_min;
1924 const int tmp_col_max = x->mv_col_max;
1925 const int tmp_row_min = x->mv_row_min;
1926 const int tmp_row_max = x->mv_row_max;
1930 mvp_full.row = b_mv[NEARESTMV].as_mv.row >> 3;
1931 mvp_full.col = b_mv[NEARESTMV].as_mv.col >> 3;
1933 mvp_full.row = xd->mi[0]->bmi[0].as_mv[0].as_mv.row >> 3;
1934 mvp_full.col = xd->mi[0]->bmi[0].as_mv[0].as_mv.col >> 3;
1937 vp9_set_mv_search_range(x, &mbmi_ext->ref_mvs[0]->as_mv);
1939 vp9_full_pixel_search(
1940 cpi, x, bsize, &mvp_full, step_param, x->sadperbit4,
1941 cond_cost_list(cpi, cost_list),
1942 &mbmi_ext->ref_mvs[ref_frame][0].as_mv, &tmp_mv,
1945 x->mv_col_min = tmp_col_min;
1946 x->mv_col_max = tmp_col_max;
1947 x->mv_row_min = tmp_row_min;
1948 x->mv_row_max = tmp_row_max;
1950 // calculate the bit cost on motion vector
1951 mvp_full.row = tmp_mv.row * 8;
1952 mvp_full.col = tmp_mv.col * 8;
1954 b_rate += vp9_mv_bit_cost(&mvp_full,
1955 &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1956 x->nmvjointcost, x->mvcost,
1959 b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
1960 [INTER_OFFSET(NEWMV)];
1961 if (RDCOST(x->rdmult, x->rddiv, b_rate, 0) > b_best_rd)
1964 cpi->find_fractional_mv_step(x, &tmp_mv,
1965 &mbmi_ext->ref_mvs[ref_frame][0].as_mv,
1966 cpi->common.allow_high_precision_mv,
1968 &cpi->fn_ptr[bsize],
1969 cpi->sf.mv.subpel_force_stop,
1970 cpi->sf.mv.subpel_iters_per_step,
1971 cond_cost_list(cpi, cost_list),
1972 x->nmvjointcost, x->mvcost,
1974 &x->pred_sse[ref_frame], NULL, 0, 0);
1976 xd->mi[0]->bmi[i].as_mv[0].as_mv = tmp_mv;
1978 b_rate += cpi->inter_mode_cost[x->mbmi_ext->mode_context[ref_frame]]
1979 [INTER_OFFSET(this_mode)];
1982 #if CONFIG_VP9_HIGHBITDEPTH
1983 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1984 vp9_highbd_build_inter_predictor(pd->pre[0].buf, pd->pre[0].stride,
1985 pd->dst.buf, pd->dst.stride,
1986 &xd->mi[0]->bmi[i].as_mv[0].as_mv,
1987 &xd->block_refs[0]->sf,
1988 4 * num_4x4_blocks_wide,
1989 4 * num_4x4_blocks_high, 0,
1990 vp9_filter_kernels[mbmi->interp_filter],
1992 mi_col * MI_SIZE + 4 * (i & 0x01),
1993 mi_row * MI_SIZE + 4 * (i >> 1), xd->bd);
1996 vp9_build_inter_predictor(pd->pre[0].buf, pd->pre[0].stride,
1997 pd->dst.buf, pd->dst.stride,
1998 &xd->mi[0]->bmi[i].as_mv[0].as_mv,
1999 &xd->block_refs[0]->sf,
2000 4 * num_4x4_blocks_wide,
2001 4 * num_4x4_blocks_high, 0,
2002 vp9_filter_kernels[mbmi->interp_filter],
2004 mi_col * MI_SIZE + 4 * (i & 0x01),
2005 mi_row * MI_SIZE + 4 * (i >> 1));
2007 #if CONFIG_VP9_HIGHBITDEPTH
2011 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
2014 this_rdc.rate += b_rate;
2015 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2016 this_rdc.rate, this_rdc.dist);
2017 if (this_rdc.rdcost < b_best_rd) {
2018 b_best_rd = this_rdc.rdcost;
2019 bsi[ref_frame][i].as_mode = this_mode;
2020 bsi[ref_frame][i].as_mv[0].as_mv = xd->mi[0]->bmi[i].as_mv[0].as_mv;
2024 // restore source and prediction buffer pointers.
2026 pd->pre[0] = orig_pre[0];
2028 this_rd += b_best_rd;
2030 xd->mi[0]->bmi[i] = bsi[ref_frame][i];
2031 if (num_4x4_blocks_wide > 1)
2032 xd->mi[0]->bmi[i + 1] = xd->mi[0]->bmi[i];
2033 if (num_4x4_blocks_high > 1)
2034 xd->mi[0]->bmi[i + 2] = xd->mi[0]->bmi[i];
2036 } // loop through sub8x8 blocks
2038 if (this_rd < best_rd) {
2040 best_ref_frame = ref_frame;
2042 } // reference frames
2044 mbmi->tx_size = TX_4X4;
2045 mbmi->ref_frame[0] = best_ref_frame;
2046 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
2047 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
2048 const int block = idy * 2 + idx;
2049 xd->mi[0]->bmi[block] = bsi[best_ref_frame][block];
2050 if (num_4x4_blocks_wide > 1)
2051 xd->mi[0]->bmi[block + 1] = bsi[best_ref_frame][block];
2052 if (num_4x4_blocks_high > 1)
2053 xd->mi[0]->bmi[block + 2] = bsi[best_ref_frame][block];
2056 mbmi->mode = xd->mi[0]->bmi[3].as_mode;
2057 ctx->mic = *(xd->mi[0]);
2058 ctx->mbmi_ext = *x->mbmi_ext;
2059 ctx->skip_txfm[0] = SKIP_TXFM_NONE;
2061 // Dummy assignment for speed -5. No effect in speed -6.
2062 rd_cost->rdcost = best_rd;