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_pred_common.h"
24 #include "vp9/common/vp9_reconinter.h"
25 #include "vp9/common/vp9_reconintra.h"
27 #include "vp9/encoder/vp9_cost.h"
28 #include "vp9/encoder/vp9_encoder.h"
29 #include "vp9/encoder/vp9_pickmode.h"
30 #include "vp9/encoder/vp9_ratectrl.h"
31 #include "vp9/encoder/vp9_rd.h"
39 static int mv_refs_rt(const VP9_COMMON *cm, const MACROBLOCKD *xd,
40 const TileInfo *const tile,
41 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
43 int mi_row, int mi_col) {
44 const int *ref_sign_bias = cm->ref_frame_sign_bias;
45 int i, refmv_count = 0;
47 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
49 int different_ref_found = 0;
50 int context_counter = 0;
53 // Blank the reference vector list
54 vpx_memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
56 // The nearest 2 blocks are treated differently
57 // if the size < 8x8 we get the mv from the bmi substructure,
58 // and we also need to keep a mode count.
59 for (i = 0; i < 2; ++i) {
60 const POSITION *const mv_ref = &mv_ref_search[i];
61 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
62 const MODE_INFO *const candidate_mi = xd->mi[mv_ref->col + mv_ref->row *
63 xd->mi_stride].src_mi;
64 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
65 // Keep counts for entropy encoding.
66 context_counter += mode_2_counter[candidate->mode];
67 different_ref_found = 1;
69 if (candidate->ref_frame[0] == ref_frame)
70 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
71 refmv_count, mv_ref_list, Done);
77 // Check the rest of the neighbors in much the same way
78 // as before except we don't need to keep track of sub blocks or
80 for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
81 const POSITION *const mv_ref = &mv_ref_search[i];
82 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
83 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row *
84 xd->mi_stride].src_mi->mbmi;
85 different_ref_found = 1;
87 if (candidate->ref_frame[0] == ref_frame)
88 ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, Done);
92 // Since we couldn't find 2 mvs from the same reference frame
93 // go back through the neighbors and find motion vectors from
94 // different reference frames.
95 if (different_ref_found && !refmv_count) {
96 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
97 const POSITION *mv_ref = &mv_ref_search[i];
98 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
99 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
100 * xd->mi_stride].src_mi->mbmi;
102 // If the candidate is INTRA we don't want to consider its mv.
103 IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
104 refmv_count, mv_ref_list, Done);
111 mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
114 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
115 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
120 static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
121 BLOCK_SIZE bsize, int mi_row, int mi_col,
122 int_mv *tmp_mv, int *rate_mv,
123 int64_t best_rd_sofar) {
124 MACROBLOCKD *xd = &x->e_mbd;
125 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
126 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
127 const int step_param = cpi->sf.mv.fullpel_search_step_param;
128 const int sadpb = x->sadperbit16;
130 const int ref = mbmi->ref_frame[0];
131 const MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
134 const int tmp_col_min = x->mv_col_min;
135 const int tmp_col_max = x->mv_col_max;
136 const int tmp_row_min = x->mv_row_min;
137 const int tmp_row_max = x->mv_row_max;
140 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
142 if (scaled_ref_frame) {
144 // Swap out the reference frame for a version that's been scaled to
145 // match the resolution of the current frame, allowing the existing
146 // motion search code to be used without additional modifications.
147 for (i = 0; i < MAX_MB_PLANE; i++)
148 backup_yv12[i] = xd->plane[i].pre[0];
149 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
151 vp9_set_mv_search_range(x, &ref_mv);
153 assert(x->mv_best_ref_index[ref] <= 2);
154 if (x->mv_best_ref_index[ref] < 2)
155 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
157 mvp_full = x->pred_mv[ref];
162 vp9_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
163 cond_cost_list(cpi, cost_list),
164 &ref_mv, &tmp_mv->as_mv, INT_MAX, 0);
166 x->mv_col_min = tmp_col_min;
167 x->mv_col_max = tmp_col_max;
168 x->mv_row_min = tmp_row_min;
169 x->mv_row_max = tmp_row_max;
171 // calculate the bit cost on motion vector
172 mvp_full.row = tmp_mv->as_mv.row * 8;
173 mvp_full.col = tmp_mv->as_mv.col * 8;
175 *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv,
176 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
178 rate_mode = cpi->inter_mode_cost[mbmi->mode_context[ref]]
179 [INTER_OFFSET(NEWMV)];
180 rv = !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) >
184 cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv,
185 cpi->common.allow_high_precision_mv,
188 cpi->sf.mv.subpel_force_stop,
189 cpi->sf.mv.subpel_iters_per_step,
190 cond_cost_list(cpi, cost_list),
191 x->nmvjointcost, x->mvcost,
192 &dis, &x->pred_sse[ref], NULL, 0, 0);
193 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv,
194 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
197 if (scaled_ref_frame) {
199 for (i = 0; i < MAX_MB_PLANE; i++)
200 xd->plane[i].pre[0] = backup_yv12[i];
205 static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize,
206 MACROBLOCK *x, MACROBLOCKD *xd,
207 int *out_rate_sum, int64_t *out_dist_sum,
208 unsigned int *var_y, unsigned int *sse_y) {
209 // Note our transform coeffs are 8 times an orthogonal transform.
210 // Hence quantizer step is also 8 times. To get effective quantizer
211 // we need to divide by 8 before sending to modeling function.
215 struct macroblock_plane *const p = &x->plane[0];
216 struct macroblockd_plane *const pd = &xd->plane[0];
217 const int64_t dc_thr = p->quant_thred[0] >> 6;
218 const int64_t ac_thr = p->quant_thred[1] >> 6;
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);
228 if (cpi->common.tx_mode == TX_MODE_SELECT) {
229 if (sse > (var << 2))
230 xd->mi[0].src_mi->mbmi.tx_size =
231 MIN(max_txsize_lookup[bsize],
232 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
234 xd->mi[0].src_mi->mbmi.tx_size = TX_8X8;
236 if (cpi->sf.partition_search_type == VAR_BASED_PARTITION) {
237 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
238 cyclic_refresh_segment_id_boosted(xd->mi[0].src_mi->mbmi.segment_id))
239 xd->mi[0].src_mi->mbmi.tx_size = TX_8X8;
240 else if (xd->mi[0].src_mi->mbmi.tx_size > TX_16X16)
241 xd->mi[0].src_mi->mbmi.tx_size = TX_16X16;
244 xd->mi[0].src_mi->mbmi.tx_size =
245 MIN(max_txsize_lookup[bsize],
246 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
249 // Evaluate if the partition block is a skippable block in Y plane.
251 const BLOCK_SIZE unit_size =
252 txsize_to_bsize[xd->mi[0].src_mi->mbmi.tx_size];
253 const unsigned int num_blk_log2 =
254 (b_width_log2_lookup[bsize] - b_width_log2_lookup[unit_size]) +
255 (b_height_log2_lookup[bsize] - b_height_log2_lookup[unit_size]);
256 const unsigned int sse_tx = sse >> num_blk_log2;
257 const unsigned int var_tx = var >> num_blk_log2;
260 // Check if all ac coefficients can be quantized to zero.
261 if (var_tx < ac_thr || var == 0) {
263 // Check if dc coefficient can be quantized to zero.
264 if (sse_tx - var_tx < dc_thr || sse == var)
267 if (sse_tx - var_tx < dc_thr || sse == var)
272 if (x->skip_txfm[0] == 1) {
274 *out_dist_sum = sse << 4;
279 #if CONFIG_VP9_HIGHBITDEPTH
280 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
281 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
282 dc_quant >> (xd->bd - 5), &rate, &dist);
284 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
285 dc_quant >> 3, &rate, &dist);
288 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bsize],
289 dc_quant >> 3, &rate, &dist);
290 #endif // CONFIG_VP9_HIGHBITDEPTH
294 *out_rate_sum = rate >> 1;
295 *out_dist_sum = dist << 3;
298 *out_dist_sum = (sse - var) << 4;
301 #if CONFIG_VP9_HIGHBITDEPTH
302 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
303 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
304 ac_quant >> (xd->bd - 5), &rate, &dist);
306 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
307 ac_quant >> 3, &rate, &dist);
310 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bsize],
311 ac_quant >> 3, &rate, &dist);
312 #endif // CONFIG_VP9_HIGHBITDEPTH
314 *out_rate_sum += rate;
315 *out_dist_sum += dist << 4;
318 #if CONFIG_VP9_HIGHBITDEPTH
319 static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, int64_t *dist,
320 int *skippable, int64_t *sse, int plane,
321 BLOCK_SIZE bsize, TX_SIZE tx_size) {
322 MACROBLOCKD *xd = &x->e_mbd;
323 unsigned int var_y, sse_y;
326 model_rd_for_sb_y(cpi, bsize, x, xd, rate, dist, &var_y, &sse_y);
332 static void block_yrd(VP9_COMP *cpi, MACROBLOCK *x, int *rate, int64_t *dist,
333 int *skippable, int64_t *sse, int plane,
334 BLOCK_SIZE bsize, TX_SIZE tx_size) {
335 MACROBLOCKD *xd = &x->e_mbd;
336 const struct macroblockd_plane *pd = &xd->plane[plane];
337 const struct macroblock_plane *const p = &x->plane[plane];
338 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
339 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
340 const int step = 1 << (tx_size << 1);
341 const int block_step = (1 << tx_size);
343 int shift = tx_size == TX_32X32 ? 0 : 2;
344 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ? 0 :
345 xd->mb_to_right_edge >> (5 + pd->subsampling_x));
346 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ? 0 :
347 xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
350 vp9_subtract_plane(x, bsize, plane);
352 // Keep track of the row and column of the blocks we use so that we know
353 // if we are in the unrestricted motion border.
354 for (r = 0; r < max_blocks_high; r += block_step) {
355 for (c = 0; c < num_4x4_w; c += block_step) {
356 if (c < max_blocks_wide) {
357 const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
358 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
359 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
360 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
361 uint16_t *const eob = &p->eobs[block];
362 const int diff_stride = 4 * num_4x4_blocks_wide_lookup[bsize];
363 const int16_t *src_diff;
364 src_diff = &p->src_diff[(r * diff_stride + c) << 2];
368 vp9_fdct32x32_rd(src_diff, coeff, diff_stride);
369 vp9_quantize_fp_32x32(coeff, 1024, x->skip_block, p->zbin,
370 p->round_fp, p->quant_fp, p->quant_shift,
371 qcoeff, dqcoeff, pd->dequant, eob,
372 scan_order->scan, scan_order->iscan);
375 vp9_hadamard_16x16(src_diff, diff_stride, (int16_t *)coeff);
376 vp9_quantize_fp(coeff, 256, x->skip_block, p->zbin, p->round_fp,
377 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
379 scan_order->scan, scan_order->iscan);
382 vp9_hadamard_8x8(src_diff, diff_stride, (int16_t *)coeff);
383 vp9_quantize_fp(coeff, 64, x->skip_block, p->zbin, p->round_fp,
384 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
386 scan_order->scan, scan_order->iscan);
389 x->fwd_txm4x4(src_diff, coeff, diff_stride);
390 vp9_quantize_fp(coeff, 16, x->skip_block, p->zbin, p->round_fp,
391 p->quant_fp, p->quant_shift, qcoeff, dqcoeff,
393 scan_order->scan, scan_order->iscan);
399 *skippable &= (*eob == 0);
405 if (*skippable && *sse < INT64_MAX) {
406 *dist = (*sse << 6) >> shift;
414 *sse = (*sse << 6) >> shift;
415 for (r = 0; r < max_blocks_high; r += block_step) {
416 for (c = 0; c < num_4x4_w; c += block_step) {
417 if (c < max_blocks_wide) {
418 tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
419 tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
420 tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
421 uint16_t *const eob = &p->eobs[block];
424 *rate += (int)abs(qcoeff[0]);
426 *rate += (int)vp9_satd((const int16_t *)qcoeff, step << 4);
428 *dist += vp9_block_error_fp(coeff, dqcoeff, step << 4) >> shift;
439 static void model_rd_for_sb_uv(VP9_COMP *cpi, BLOCK_SIZE bsize,
440 MACROBLOCK *x, MACROBLOCKD *xd,
441 int *out_rate_sum, int64_t *out_dist_sum,
442 unsigned int *var_y, unsigned int *sse_y) {
443 // Note our transform coeffs are 8 times an orthogonal transform.
444 // Hence quantizer step is also 8 times. To get effective quantizer
445 // we need to divide by 8 before sending to modeling function.
454 for (i = 1; i <= 2; ++i) {
455 struct macroblock_plane *const p = &x->plane[i];
456 struct macroblockd_plane *const pd = &xd->plane[i];
457 const uint32_t dc_quant = pd->dequant[0];
458 const uint32_t ac_quant = pd->dequant[1];
459 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
462 if (!x->color_sensitivity[i - 1])
465 var = cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride,
466 pd->dst.buf, pd->dst.stride, &sse);
470 #if CONFIG_VP9_HIGHBITDEPTH
471 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
472 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
473 dc_quant >> (xd->bd - 5), &rate, &dist);
475 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
476 dc_quant >> 3, &rate, &dist);
479 vp9_model_rd_from_var_lapndz(sse - var, num_pels_log2_lookup[bs],
480 dc_quant >> 3, &rate, &dist);
481 #endif // CONFIG_VP9_HIGHBITDEPTH
483 *out_rate_sum += rate >> 1;
484 *out_dist_sum += dist << 3;
486 #if CONFIG_VP9_HIGHBITDEPTH
487 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
488 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
489 ac_quant >> (xd->bd - 5), &rate, &dist);
491 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
492 ac_quant >> 3, &rate, &dist);
495 vp9_model_rd_from_var_lapndz(var, num_pels_log2_lookup[bs],
496 ac_quant >> 3, &rate, &dist);
497 #endif // CONFIG_VP9_HIGHBITDEPTH
499 *out_rate_sum += rate;
500 *out_dist_sum += dist << 4;
504 static int get_pred_buffer(PRED_BUFFER *p, int len) {
507 for (i = 0; i < len; i++) {
516 static void free_pred_buffer(PRED_BUFFER *p) {
521 static void encode_breakout_test(VP9_COMP *cpi, MACROBLOCK *x,
522 BLOCK_SIZE bsize, int mi_row, int mi_col,
523 MV_REFERENCE_FRAME ref_frame,
524 PREDICTION_MODE this_mode,
525 unsigned int var_y, unsigned int sse_y,
526 struct buf_2d yv12_mb[][MAX_MB_PLANE],
527 int *rate, int64_t *dist) {
528 MACROBLOCKD *xd = &x->e_mbd;
529 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
531 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
532 unsigned int var = var_y, sse = sse_y;
533 // Skipping threshold for ac.
534 unsigned int thresh_ac;
535 // Skipping threshold for dc.
536 unsigned int thresh_dc;
537 if (x->encode_breakout > 0) {
538 // Set a maximum for threshold to avoid big PSNR loss in low bit rate
539 // case. Use extreme low threshold for static frames to limit
541 const unsigned int max_thresh = 36000;
542 // The encode_breakout input
543 const unsigned int min_thresh =
544 MIN(((unsigned int)x->encode_breakout << 4), max_thresh);
545 #if CONFIG_VP9_HIGHBITDEPTH
546 const int shift = (xd->bd << 1) - 16;
549 // Calculate threshold according to dequant value.
550 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) >> 3;
551 #if CONFIG_VP9_HIGHBITDEPTH
552 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
553 thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
555 #endif // CONFIG_VP9_HIGHBITDEPTH
556 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
558 // Adjust ac threshold according to partition size.
560 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
562 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
563 #if CONFIG_VP9_HIGHBITDEPTH
564 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
565 thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
567 #endif // CONFIG_VP9_HIGHBITDEPTH
573 // Y skipping condition checking for ac and dc.
574 if (var <= thresh_ac && (sse - var) <= thresh_dc) {
575 unsigned int sse_u, sse_v;
576 unsigned int var_u, var_v;
578 // Skip UV prediction unless breakout is zero (lossless) to save
579 // computation with low impact on the result
580 if (x->encode_breakout == 0) {
581 xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
582 xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
583 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
586 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
587 x->plane[1].src.stride,
588 xd->plane[1].dst.buf,
589 xd->plane[1].dst.stride, &sse_u);
591 // U skipping condition checking
592 if (((var_u << 2) <= thresh_ac) && (sse_u - var_u <= thresh_dc)) {
593 var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf,
594 x->plane[2].src.stride,
595 xd->plane[2].dst.buf,
596 xd->plane[2].dst.stride, &sse_v);
598 // V skipping condition checking
599 if (((var_v << 2) <= thresh_ac) && (sse_v - var_v <= thresh_dc)) {
602 // The cost of skip bit needs to be added.
603 *rate = cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
604 [INTER_OFFSET(this_mode)];
606 // More on this part of rate
607 // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
609 // Scaling factor for SSE from spatial domain to frequency
610 // domain is 16. Adjust distortion accordingly.
611 // TODO(yunqingwang): In this function, only y-plane dist is
613 *dist = (sse << 4); // + ((sse_u + sse_v) << 4);
615 // *disable_skip = 1;
621 struct estimate_block_intra_args {
624 PREDICTION_MODE mode;
629 static void estimate_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
630 TX_SIZE tx_size, void *arg) {
631 struct estimate_block_intra_args* const args = arg;
632 VP9_COMP *const cpi = args->cpi;
633 MACROBLOCK *const x = args->x;
634 MACROBLOCKD *const xd = &x->e_mbd;
635 struct macroblock_plane *const p = &x->plane[0];
636 struct macroblockd_plane *const pd = &xd->plane[0];
637 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
638 uint8_t *const src_buf_base = p->src.buf;
639 uint8_t *const dst_buf_base = pd->dst.buf;
640 const int src_stride = p->src.stride;
641 const int dst_stride = pd->dst.stride;
645 int64_t this_sse = INT64_MAX;
648 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
652 p->src.buf = &src_buf_base[4 * (j * src_stride + i)];
653 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)];
654 // Use source buffer as an approximation for the fully reconstructed buffer.
655 vp9_predict_intra_block(xd, block >> (2 * tx_size),
656 b_width_log2_lookup[plane_bsize],
658 x->skip_encode ? p->src.buf : pd->dst.buf,
659 x->skip_encode ? src_stride : dst_stride,
660 pd->dst.buf, dst_stride,
663 // TODO(jingning): This needs further refactoring.
664 if (plane_bsize <= BLOCK_16X16) {
665 block_yrd(cpi, x, &rate, &dist, &is_skippable, &this_sse, 0,
667 x->skip_txfm[0] = is_skippable;
669 rate = vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 1);
671 rate += vp9_cost_bit(vp9_get_skip_prob(&cpi->common, xd), 0);
673 unsigned int var_y, sse_y;
674 model_rd_for_sb_y(cpi, bsize_tx, x, xd, &rate, &dist, &var_y, &sse_y);
677 p->src.buf = src_buf_base;
678 pd->dst.buf = dst_buf_base;
683 static const THR_MODES mode_idx[MAX_REF_FRAMES - 1][4] = {
684 {THR_DC, THR_H_PRED, THR_V_PRED, THR_TM},
685 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV},
686 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG},
689 static const PREDICTION_MODE intra_mode_list[] = {
690 DC_PRED, V_PRED, H_PRED, TM_PRED
693 void vp9_pick_intra_mode(VP9_COMP *cpi, MACROBLOCK *x, RD_COST *rd_cost,
694 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
695 MACROBLOCKD *const xd = &x->e_mbd;
696 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
697 RD_COST this_rdc, best_rdc;
698 PREDICTION_MODE this_mode;
699 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
700 const TX_SIZE intra_tx_size =
701 MIN(max_txsize_lookup[bsize],
702 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
703 MODE_INFO *const mic = xd->mi[0].src_mi;
705 const MODE_INFO *above_mi = xd->mi[-xd->mi_stride].src_mi;
706 const MODE_INFO *left_mi = xd->left_available ? xd->mi[-1].src_mi : NULL;
707 const PREDICTION_MODE A = vp9_above_block_mode(mic, above_mi, 0);
708 const PREDICTION_MODE L = vp9_left_block_mode(mic, left_mi, 0);
709 bmode_costs = cpi->y_mode_costs[A][L];
712 vp9_rd_cost_reset(&best_rdc);
713 vp9_rd_cost_reset(&this_rdc);
715 mbmi->ref_frame[0] = INTRA_FRAME;
716 mbmi->mv[0].as_int = INVALID_MV;
717 mbmi->uv_mode = DC_PRED;
718 vpx_memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
720 // Change the limit of this loop to add other intra prediction
722 for (this_mode = DC_PRED; this_mode <= H_PRED; ++this_mode) {
723 args.mode = this_mode;
726 mbmi->tx_size = intra_tx_size;
727 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
728 estimate_block_intra, &args);
729 this_rdc.rate = args.rate;
730 this_rdc.dist = args.dist;
731 this_rdc.rate += bmode_costs[this_mode];
732 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
733 this_rdc.rate, this_rdc.dist);
735 if (this_rdc.rdcost < best_rdc.rdcost) {
737 mbmi->mode = this_mode;
745 MV_REFERENCE_FRAME ref_frame;
746 PREDICTION_MODE pred_mode;
749 #define RT_INTER_MODES 8
750 static const REF_MODE ref_mode_set[RT_INTER_MODES] = {
751 {LAST_FRAME, ZEROMV},
752 {LAST_FRAME, NEARESTMV},
753 {GOLDEN_FRAME, ZEROMV},
754 {LAST_FRAME, NEARMV},
756 {GOLDEN_FRAME, NEARESTMV},
757 {GOLDEN_FRAME, NEARMV},
758 {GOLDEN_FRAME, NEWMV}
761 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
762 // this needs various further optimizations. to be continued..
763 void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
764 TileDataEnc *tile_data,
765 int mi_row, int mi_col, RD_COST *rd_cost,
766 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
767 VP9_COMMON *const cm = &cpi->common;
768 TileInfo *const tile_info = &tile_data->tile_info;
769 MACROBLOCKD *const xd = &x->e_mbd;
770 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
771 struct macroblockd_plane *const pd = &xd->plane[0];
772 PREDICTION_MODE best_mode = ZEROMV;
773 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
774 MV_REFERENCE_FRAME usable_ref_frame;
775 TX_SIZE best_tx_size = TX_SIZES;
776 INTERP_FILTER best_pred_filter = EIGHTTAP;
777 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
778 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
779 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
781 RD_COST this_rdc, best_rdc;
782 uint8_t skip_txfm = 0, best_mode_skip_txfm = 0;
783 // var_y and sse_y are saved to be used in skipping checking
784 unsigned int var_y = UINT_MAX;
785 unsigned int sse_y = UINT_MAX;
786 // Reduce the intra cost penalty for small blocks (<=16x16).
787 const int reduction_fac =
788 (cpi->sf.partition_search_type == VAR_BASED_PARTITION &&
789 bsize <= BLOCK_16X16) ? 2 : 0;
790 const int intra_cost_penalty = vp9_get_intra_cost_penalty(
791 cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth) >> reduction_fac;
792 const int64_t inter_mode_thresh = RDCOST(x->rdmult, x->rddiv,
793 intra_cost_penalty, 0);
794 const int *const rd_threshes = cpi->rd.threshes[mbmi->segment_id][bsize];
795 const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
796 INTERP_FILTER filter_ref;
797 const int bsl = mi_width_log2_lookup[bsize];
798 const int pred_filter_search = cm->interp_filter == SWITCHABLE ?
799 (((mi_row + mi_col) >> bsl) +
800 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0;
801 int const_motion[MAX_REF_FRAMES] = { 0 };
802 const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
803 const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
804 // For speed 6, the result of interp filter is reused later in actual encoding
806 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
808 DECLARE_ALIGNED_ARRAY(16, uint8_t, pred_buf, 3 * 64 * 64);
809 #if CONFIG_VP9_HIGHBITDEPTH
810 DECLARE_ALIGNED_ARRAY(16, uint16_t, pred_buf_16, 3 * 64 * 64);
812 struct buf_2d orig_dst = pd->dst;
813 PRED_BUFFER *best_pred = NULL;
814 PRED_BUFFER *this_mode_pred = NULL;
815 const int pixels_in_block = bh * bw;
816 int reuse_inter_pred = cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready;
817 int ref_frame_skip_mask = 0;
819 int best_pred_sad = INT_MAX;
820 int ref_frame_cost[MAX_REF_FRAMES];
821 vp9_prob intra_inter_p = vp9_get_intra_inter_prob(cm, xd);
822 vp9_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd);
823 vp9_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd);
825 ref_frame_cost[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0);
826 ref_frame_cost[LAST_FRAME] = ref_frame_cost[GOLDEN_FRAME] =
827 ref_frame_cost[ALTREF_FRAME] = vp9_cost_bit(intra_inter_p, 1);
829 ref_frame_cost[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0);
830 ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1);
831 ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1);
832 ref_frame_cost[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0);
833 ref_frame_cost[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1);
835 if (reuse_inter_pred) {
837 for (i = 0; i < 3; i++) {
838 #if CONFIG_VP9_HIGHBITDEPTH
839 if (cm->use_highbitdepth)
840 tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
842 tmp[i].data = &pred_buf[pixels_in_block * i];
844 tmp[i].data = &pred_buf[pixels_in_block * i];
845 #endif // CONFIG_VP9_HIGHBITDEPTH
849 tmp[3].data = pd->dst.buf;
850 tmp[3].stride = pd->dst.stride;
854 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
857 if (xd->up_available)
858 filter_ref = xd->mi[-xd->mi_stride].src_mi->mbmi.interp_filter;
859 else if (xd->left_available)
860 filter_ref = xd->mi[-1].src_mi->mbmi.interp_filter;
862 filter_ref = cm->interp_filter;
864 // initialize mode decisions
865 vp9_rd_cost_reset(&best_rdc);
866 vp9_rd_cost_reset(rd_cost);
867 mbmi->sb_type = bsize;
868 mbmi->ref_frame[0] = NONE;
869 mbmi->ref_frame[1] = NONE;
870 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
871 tx_mode_to_biggest_tx_size[cm->tx_mode]);
873 #if CONFIG_VP9_TEMPORAL_DENOISING
874 vp9_denoiser_reset_frame_stats(ctx);
877 if (cpi->rc.frames_since_golden == 0) {
878 cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
879 usable_ref_frame = LAST_FRAME;
881 usable_ref_frame = GOLDEN_FRAME;
884 for (ref_frame = LAST_FRAME; ref_frame <= usable_ref_frame; ++ref_frame) {
885 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
887 x->pred_mv_sad[ref_frame] = INT_MAX;
888 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
889 frame_mv[ZEROMV][ref_frame].as_int = 0;
891 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
892 int_mv *const candidates = mbmi->ref_mvs[ref_frame];
893 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
895 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
898 if (cm->use_prev_frame_mvs)
899 vp9_find_mv_refs(cm, xd, tile_info, xd->mi[0].src_mi, ref_frame,
900 candidates, mi_row, mi_col, NULL, NULL);
902 const_motion[ref_frame] = mv_refs_rt(cm, xd, tile_info,
904 ref_frame, candidates,
907 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
908 &frame_mv[NEARESTMV][ref_frame],
909 &frame_mv[NEARMV][ref_frame]);
911 if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8)
912 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
915 ref_frame_skip_mask |= (1 << ref_frame);
919 for (idx = 0; idx < RT_INTER_MODES; ++idx) {
924 PREDICTION_MODE this_mode = ref_mode_set[idx].pred_mode;
928 if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode)))
931 ref_frame = ref_mode_set[idx].ref_frame;
932 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
934 if (const_motion[ref_frame] && this_mode == NEARMV)
937 i = (ref_frame == LAST_FRAME) ? GOLDEN_FRAME : LAST_FRAME;
938 if (cpi->ref_frame_flags & flag_list[i])
939 if (x->pred_mv_sad[ref_frame] > (x->pred_mv_sad[i] << 1))
940 ref_frame_skip_mask |= (1 << ref_frame);
941 if (ref_frame_skip_mask & (1 << ref_frame))
944 // Select prediction reference frames.
945 for (i = 0; i < MAX_MB_PLANE; i++)
946 xd->plane[i].pre[0] = yv12_mb[ref_frame][i];
948 mbmi->ref_frame[0] = ref_frame;
949 set_ref_ptrs(cm, xd, ref_frame, NONE);
951 mode_index = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
952 mode_rd_thresh = best_mode_skip_txfm ?
953 rd_threshes[mode_index] << 1 : rd_threshes[mode_index];
954 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
955 rd_thresh_freq_fact[mode_index]))
958 if (this_mode == NEWMV) {
959 if (ref_frame > LAST_FRAME) {
961 int dis, cost_list[5];
963 if (bsize < BLOCK_16X16)
966 tmp_sad = vp9_int_pro_motion_estimation(cpi, x, bsize);
968 if (tmp_sad > x->pred_mv_sad[LAST_FRAME])
970 if (tmp_sad + (num_pels_log2_lookup[bsize] << 4) > best_pred_sad)
973 frame_mv[NEWMV][ref_frame].as_int = mbmi->mv[0].as_int;
974 rate_mv = vp9_mv_bit_cost(&frame_mv[NEWMV][ref_frame].as_mv,
975 &mbmi->ref_mvs[ref_frame][0].as_mv,
976 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
977 frame_mv[NEWMV][ref_frame].as_mv.row >>= 3;
978 frame_mv[NEWMV][ref_frame].as_mv.col >>= 3;
980 cpi->find_fractional_mv_step(x, &frame_mv[NEWMV][ref_frame].as_mv,
981 &mbmi->ref_mvs[ref_frame][0].as_mv,
982 cpi->common.allow_high_precision_mv,
985 cpi->sf.mv.subpel_force_stop,
986 cpi->sf.mv.subpel_iters_per_step,
987 cond_cost_list(cpi, cost_list),
988 x->nmvjointcost, x->mvcost, &dis,
989 &x->pred_sse[ref_frame], NULL, 0, 0);
990 } else if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
991 &frame_mv[NEWMV][ref_frame], &rate_mv, best_rdc.rdcost)) {
996 if (this_mode == NEWMV && ref_frame == LAST_FRAME &&
997 frame_mv[NEWMV][LAST_FRAME].as_int != INVALID_MV) {
998 const int pre_stride = xd->plane[0].pre[0].stride;
999 const uint8_t * const pre_buf = xd->plane[0].pre[0].buf +
1000 (frame_mv[NEWMV][LAST_FRAME].as_mv.row >> 3) * pre_stride +
1001 (frame_mv[NEWMV][LAST_FRAME].as_mv.col >> 3);
1002 best_pred_sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
1003 x->plane[0].src.stride,
1004 pre_buf, pre_stride);
1005 x->pred_mv_sad[LAST_FRAME] = best_pred_sad;
1008 if (this_mode != NEARESTMV &&
1009 frame_mv[this_mode][ref_frame].as_int ==
1010 frame_mv[NEARESTMV][ref_frame].as_int)
1013 mbmi->mode = this_mode;
1014 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
1016 // Search for the best prediction filter type, when the resulting
1017 // motion vector is at sub-pixel accuracy level for luma component, i.e.,
1018 // the last three bits are all zeros.
1019 if (reuse_inter_pred) {
1020 if (!this_mode_pred) {
1021 this_mode_pred = &tmp[3];
1023 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1024 pd->dst.buf = this_mode_pred->data;
1025 pd->dst.stride = bw;
1029 if ((this_mode == NEWMV || filter_ref == SWITCHABLE) && pred_filter_search
1030 && (ref_frame == LAST_FRAME)
1031 && (((mbmi->mv[0].as_mv.row | mbmi->mv[0].as_mv.col) & 0x07) != 0)) {
1034 unsigned int pf_var[3];
1035 unsigned int pf_sse[3];
1036 TX_SIZE pf_tx_size[3];
1037 int64_t best_cost = INT64_MAX;
1038 INTERP_FILTER best_filter = SWITCHABLE, filter;
1039 PRED_BUFFER *current_pred = this_mode_pred;
1041 for (filter = EIGHTTAP; filter <= EIGHTTAP_SHARP; ++filter) {
1043 mbmi->interp_filter = filter;
1044 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1045 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter], &pf_dist[filter],
1046 &pf_var[filter], &pf_sse[filter]);
1047 pf_rate[filter] += vp9_get_switchable_rate(cpi, xd);
1048 cost = RDCOST(x->rdmult, x->rddiv, pf_rate[filter], pf_dist[filter]);
1049 pf_tx_size[filter] = mbmi->tx_size;
1050 if (cost < best_cost) {
1051 best_filter = filter;
1053 skip_txfm = x->skip_txfm[0];
1055 if (reuse_inter_pred) {
1056 if (this_mode_pred != current_pred) {
1057 free_pred_buffer(this_mode_pred);
1058 this_mode_pred = current_pred;
1061 if (filter < EIGHTTAP_SHARP) {
1062 current_pred = &tmp[get_pred_buffer(tmp, 3)];
1063 pd->dst.buf = current_pred->data;
1064 pd->dst.stride = bw;
1070 if (reuse_inter_pred && this_mode_pred != current_pred)
1071 free_pred_buffer(current_pred);
1073 mbmi->interp_filter = best_filter;
1074 mbmi->tx_size = pf_tx_size[best_filter];
1075 this_rdc.rate = pf_rate[best_filter];
1076 this_rdc.dist = pf_dist[best_filter];
1077 var_y = pf_var[best_filter];
1078 sse_y = pf_sse[best_filter];
1079 x->skip_txfm[0] = skip_txfm;
1080 if (reuse_inter_pred) {
1081 pd->dst.buf = this_mode_pred->data;
1082 pd->dst.stride = this_mode_pred->stride;
1085 mbmi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP : filter_ref;
1086 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
1087 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
1090 cm->interp_filter == SWITCHABLE ?
1091 vp9_get_switchable_rate(cpi, xd) : 0;
1094 if (bsize <= BLOCK_16X16) {
1095 this_sse = (int64_t)sse_y;
1096 block_yrd(cpi, x, &this_rdc.rate, &this_rdc.dist, &is_skippable,
1097 &this_sse, 0, bsize, mbmi->tx_size);
1098 x->skip_txfm[0] = is_skippable;
1100 this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1102 if (RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist) <
1103 RDCOST(x->rdmult, x->rddiv, 0, this_sse)) {
1104 this_rdc.rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 0);
1106 this_rdc.rate = vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
1107 this_rdc.dist = this_sse;
1108 x->skip_txfm[0] = 1;
1112 if (cm->interp_filter == SWITCHABLE) {
1113 if ((mbmi->mv[0].as_mv.row | mbmi->mv[0].as_mv.col) & 0x07)
1114 this_rdc.rate += vp9_get_switchable_rate(cpi, xd);
1118 if (x->color_sensitivity[0] || x->color_sensitivity[1]) {
1120 int64_t uv_dist = 0;
1121 if (x->color_sensitivity[0])
1122 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 1);
1123 if (x->color_sensitivity[1])
1124 vp9_build_inter_predictors_sbp(xd, mi_row, mi_col, bsize, 2);
1125 model_rd_for_sb_uv(cpi, bsize, x, xd, &uv_rate, &uv_dist,
1127 this_rdc.rate += uv_rate;
1128 this_rdc.dist += uv_dist;
1131 this_rdc.rate += rate_mv;
1133 cpi->inter_mode_cost[mbmi->mode_context[ref_frame]][INTER_OFFSET(
1135 this_rdc.rate += ref_frame_cost[ref_frame];
1136 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
1138 // Skipping checking: test to see if this block can be reconstructed by
1140 if (cpi->allow_encode_breakout) {
1141 encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame, this_mode,
1142 var_y, sse_y, yv12_mb, &this_rdc.rate,
1145 this_rdc.rate += rate_mv;
1146 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, this_rdc.rate,
1151 #if CONFIG_VP9_TEMPORAL_DENOISING
1152 if (cpi->oxcf.noise_sensitivity > 0)
1153 vp9_denoiser_update_frame_stats(mbmi, sse_y, this_mode, ctx);
1158 if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
1159 best_rdc = this_rdc;
1160 best_mode = this_mode;
1161 best_pred_filter = mbmi->interp_filter;
1162 best_tx_size = mbmi->tx_size;
1163 best_ref_frame = ref_frame;
1164 best_mode_skip_txfm = x->skip_txfm[0];
1166 if (reuse_inter_pred) {
1167 free_pred_buffer(best_pred);
1168 best_pred = this_mode_pred;
1171 if (reuse_inter_pred)
1172 free_pred_buffer(this_mode_pred);
1179 mbmi->mode = best_mode;
1180 mbmi->interp_filter = best_pred_filter;
1181 mbmi->tx_size = best_tx_size;
1182 mbmi->ref_frame[0] = best_ref_frame;
1183 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
1184 xd->mi[0].src_mi->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
1185 x->skip_txfm[0] = best_mode_skip_txfm;
1187 // Perform intra prediction search, if the best SAD is above a certain
1189 if (best_rdc.rdcost == INT64_MAX ||
1190 (!x->skip && best_rdc.rdcost > inter_mode_thresh &&
1191 bsize <= cpi->sf.max_intra_bsize)) {
1192 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
1193 const TX_SIZE intra_tx_size =
1194 MIN(max_txsize_lookup[bsize],
1195 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
1197 TX_SIZE best_intra_tx_size = TX_SIZES;
1199 if (reuse_inter_pred && best_pred != NULL) {
1200 if (best_pred->data == orig_dst.buf) {
1201 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
1202 #if CONFIG_VP9_HIGHBITDEPTH
1203 if (cm->use_highbitdepth)
1204 vp9_highbd_convolve_copy(best_pred->data, best_pred->stride,
1205 this_mode_pred->data, this_mode_pred->stride,
1206 NULL, 0, NULL, 0, bw, bh, xd->bd);
1208 vp9_convolve_copy(best_pred->data, best_pred->stride,
1209 this_mode_pred->data, this_mode_pred->stride,
1210 NULL, 0, NULL, 0, bw, bh);
1212 vp9_convolve_copy(best_pred->data, best_pred->stride,
1213 this_mode_pred->data, this_mode_pred->stride,
1214 NULL, 0, NULL, 0, bw, bh);
1215 #endif // CONFIG_VP9_HIGHBITDEPTH
1216 best_pred = this_mode_pred;
1221 for (i = 0; i < 4; ++i) {
1222 const PREDICTION_MODE this_mode = intra_mode_list[i];
1223 if (!((1 << this_mode) & cpi->sf.intra_y_mode_mask[intra_tx_size]))
1225 mbmi->mode = this_mode;
1226 mbmi->ref_frame[0] = INTRA_FRAME;
1227 args.mode = this_mode;
1230 mbmi->tx_size = intra_tx_size;
1231 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
1232 estimate_block_intra, &args);
1233 this_rdc.rate = args.rate;
1234 this_rdc.dist = args.dist;
1235 this_rdc.rate += cpi->mbmode_cost[this_mode];
1236 this_rdc.rate += ref_frame_cost[INTRA_FRAME];
1237 this_rdc.rate += intra_cost_penalty;
1238 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1239 this_rdc.rate, this_rdc.dist);
1241 if (this_rdc.rdcost < best_rdc.rdcost) {
1242 best_rdc = this_rdc;
1243 best_mode = this_mode;
1244 best_intra_tx_size = mbmi->tx_size;
1245 best_ref_frame = INTRA_FRAME;
1246 mbmi->uv_mode = this_mode;
1247 mbmi->mv[0].as_int = INVALID_MV;
1248 best_mode_skip_txfm = x->skip_txfm[0];
1252 // Reset mb_mode_info to the best inter mode.
1253 if (best_ref_frame != INTRA_FRAME) {
1254 mbmi->tx_size = best_tx_size;
1256 mbmi->tx_size = best_intra_tx_size;
1261 mbmi->mode = best_mode;
1262 mbmi->ref_frame[0] = best_ref_frame;
1263 x->skip_txfm[0] = best_mode_skip_txfm;
1265 if (reuse_inter_pred && best_pred != NULL) {
1266 if (best_pred->data != orig_dst.buf && is_inter_mode(mbmi->mode)) {
1267 #if CONFIG_VP9_HIGHBITDEPTH
1268 if (cm->use_highbitdepth)
1269 vp9_highbd_convolve_copy(best_pred->data, best_pred->stride,
1270 pd->dst.buf, pd->dst.stride, NULL, 0,
1271 NULL, 0, bw, bh, xd->bd);
1273 vp9_convolve_copy(best_pred->data, best_pred->stride,
1274 pd->dst.buf, pd->dst.stride, NULL, 0,
1277 vp9_convolve_copy(best_pred->data, best_pred->stride,
1278 pd->dst.buf, pd->dst.stride, NULL, 0,
1280 #endif // CONFIG_VP9_HIGHBITDEPTH
1284 if (cpi->sf.adaptive_rd_thresh) {
1285 THR_MODES best_mode_idx = is_inter_block(mbmi) ?
1286 mode_idx[best_ref_frame][INTER_OFFSET(mbmi->mode)] :
1287 mode_idx[INTRA_FRAME][mbmi->mode];
1288 PREDICTION_MODE this_mode;
1289 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1290 if (best_ref_frame != ref_frame) continue;
1291 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1292 THR_MODES thr_mode_idx = mode_idx[ref_frame][INTER_OFFSET(this_mode)];
1293 int *freq_fact = &tile_data->thresh_freq_fact[bsize][thr_mode_idx];
1294 if (thr_mode_idx == best_mode_idx)
1295 *freq_fact -= (*freq_fact >> 4);
1297 *freq_fact = MIN(*freq_fact + RD_THRESH_INC,
1298 cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT);
1303 *rd_cost = best_rdc;
1306 void vp9_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x,
1307 TileDataEnc *tile_data,
1308 int mi_row, int mi_col, RD_COST *rd_cost,
1309 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
1310 VP9_COMMON *const cm = &cpi->common;
1311 TileInfo *const tile_info = &tile_data->tile_info;
1312 SPEED_FEATURES *const sf = &cpi->sf;
1313 MACROBLOCKD *const xd = &x->e_mbd;
1314 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
1315 const struct segmentation *const seg = &cm->seg;
1316 MV_REFERENCE_FRAME ref_frame, second_ref_frame = NONE;
1317 MV_REFERENCE_FRAME best_ref_frame = NONE;
1318 unsigned char segment_id = mbmi->segment_id;
1319 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
1320 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1322 int64_t best_rd = INT64_MAX;
1323 b_mode_info bsi[MAX_REF_FRAMES][4];
1324 int ref_frame_skip_mask = 0;
1325 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1326 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1329 x->skip_encode = sf->skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
1330 ctx->pred_pixel_ready = 0;
1332 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1333 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1335 x->pred_mv_sad[ref_frame] = INT_MAX;
1337 if ((cpi->ref_frame_flags & flag_list[ref_frame]) && (yv12 != NULL)) {
1338 int_mv *const candidates = mbmi->ref_mvs[ref_frame];
1339 const struct scale_factors *const sf =
1340 &cm->frame_refs[ref_frame - 1].sf;
1341 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
1343 vp9_find_mv_refs(cm, xd, tile_info, xd->mi[0].src_mi, ref_frame,
1344 candidates, mi_row, mi_col, NULL, NULL);
1346 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
1347 &dummy_mv[0], &dummy_mv[1]);
1349 ref_frame_skip_mask |= (1 << ref_frame);
1353 mbmi->sb_type = bsize;
1354 mbmi->tx_size = TX_4X4;
1355 mbmi->uv_mode = DC_PRED;
1356 mbmi->ref_frame[0] = LAST_FRAME;
1357 mbmi->ref_frame[1] = NONE;
1358 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ? EIGHTTAP
1359 : cm->interp_filter;
1361 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
1362 int64_t this_rd = 0;
1365 if (ref_frame_skip_mask & (1 << ref_frame))
1368 // TODO(jingning, agrange): Scaling reference frame not supported for
1369 // sub8x8 blocks. Is this supported now?
1370 if (ref_frame > INTRA_FRAME &&
1371 vp9_is_scaled(&cm->frame_refs[ref_frame - 1].sf))
1374 // If the segment reference frame feature is enabled....
1375 // then do nothing if the current ref frame is not allowed..
1376 if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) &&
1377 vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != (int)ref_frame)
1380 mbmi->ref_frame[0] = ref_frame;
1382 set_ref_ptrs(cm, xd, ref_frame, second_ref_frame);
1384 // Select prediction reference frames.
1385 for (plane = 0; plane < MAX_MB_PLANE; plane++)
1386 xd->plane[plane].pre[0] = yv12_mb[ref_frame][plane];
1388 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1389 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1390 int_mv b_mv[MB_MODE_COUNT];
1391 int64_t b_best_rd = INT64_MAX;
1392 const int i = idy * 2 + idx;
1393 PREDICTION_MODE this_mode;
1395 unsigned int var_y, sse_y;
1397 struct macroblock_plane *p = &x->plane[0];
1398 struct macroblockd_plane *pd = &xd->plane[0];
1400 const struct buf_2d orig_src = p->src;
1401 const struct buf_2d orig_dst = pd->dst;
1402 struct buf_2d orig_pre[2];
1403 vpx_memcpy(orig_pre, xd->plane[0].pre, sizeof(orig_pre));
1405 // set buffer pointers for sub8x8 motion search.
1407 &p->src.buf[vp9_raster_block_offset(BLOCK_8X8, i, p->src.stride)];
1409 &pd->dst.buf[vp9_raster_block_offset(BLOCK_8X8, i, pd->dst.stride)];
1411 &pd->pre[0].buf[vp9_raster_block_offset(BLOCK_8X8,
1412 i, pd->pre[0].stride)];
1414 b_mv[ZEROMV].as_int = 0;
1415 b_mv[NEWMV].as_int = INVALID_MV;
1416 vp9_append_sub8x8_mvs_for_idx(cm, xd, tile_info, i, 0, mi_row, mi_col,
1420 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
1422 xd->mi[0].bmi[i].as_mv[0].as_int = b_mv[this_mode].as_int;
1424 if (this_mode == NEWMV) {
1425 const int step_param = cpi->sf.mv.fullpel_search_step_param;
1429 const int tmp_col_min = x->mv_col_min;
1430 const int tmp_col_max = x->mv_col_max;
1431 const int tmp_row_min = x->mv_row_min;
1432 const int tmp_row_max = x->mv_row_max;
1436 mvp_full.row = b_mv[NEARESTMV].as_mv.row >> 3;
1437 mvp_full.col = b_mv[NEARESTMV].as_mv.col >> 3;
1439 mvp_full.row = xd->mi[0].bmi[0].as_mv[0].as_mv.row >> 3;
1440 mvp_full.col = xd->mi[0].bmi[0].as_mv[0].as_mv.col >> 3;
1443 vp9_set_mv_search_range(x, &mbmi->ref_mvs[0]->as_mv);
1445 vp9_full_pixel_search(
1446 cpi, x, bsize, &mvp_full, step_param, x->sadperbit4,
1447 cond_cost_list(cpi, cost_list),
1448 &mbmi->ref_mvs[ref_frame][0].as_mv, &tmp_mv,
1451 x->mv_col_min = tmp_col_min;
1452 x->mv_col_max = tmp_col_max;
1453 x->mv_row_min = tmp_row_min;
1454 x->mv_row_max = tmp_row_max;
1456 // calculate the bit cost on motion vector
1457 mvp_full.row = tmp_mv.row * 8;
1458 mvp_full.col = tmp_mv.col * 8;
1460 b_rate += vp9_mv_bit_cost(&mvp_full,
1461 &mbmi->ref_mvs[ref_frame][0].as_mv,
1462 x->nmvjointcost, x->mvcost,
1465 b_rate += cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
1466 [INTER_OFFSET(NEWMV)];
1467 if (RDCOST(x->rdmult, x->rddiv, b_rate, 0) > b_best_rd)
1470 cpi->find_fractional_mv_step(x, &tmp_mv,
1471 &mbmi->ref_mvs[ref_frame][0].as_mv,
1472 cpi->common.allow_high_precision_mv,
1474 &cpi->fn_ptr[bsize],
1475 cpi->sf.mv.subpel_force_stop,
1476 cpi->sf.mv.subpel_iters_per_step,
1477 cond_cost_list(cpi, cost_list),
1478 x->nmvjointcost, x->mvcost,
1480 &x->pred_sse[ref_frame], NULL, 0, 0);
1482 xd->mi[0].bmi[i].as_mv[0].as_mv = tmp_mv;
1484 b_rate += cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
1485 [INTER_OFFSET(this_mode)];
1488 #if CONFIG_VP9_HIGHBITDEPTH
1489 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1490 vp9_highbd_build_inter_predictor(pd->pre[0].buf, pd->pre[0].stride,
1491 pd->dst.buf, pd->dst.stride,
1492 &xd->mi[0].bmi[i].as_mv[0].as_mv,
1493 &xd->block_refs[0]->sf,
1494 4 * num_4x4_blocks_wide,
1495 4 * num_4x4_blocks_high, 0,
1496 vp9_get_interp_kernel(mbmi->interp_filter),
1498 mi_col * MI_SIZE + 4 * (i & 0x01),
1499 mi_row * MI_SIZE + 4 * (i >> 1), xd->bd);
1502 vp9_build_inter_predictor(pd->pre[0].buf, pd->pre[0].stride,
1503 pd->dst.buf, pd->dst.stride,
1504 &xd->mi[0].bmi[i].as_mv[0].as_mv,
1505 &xd->block_refs[0]->sf,
1506 4 * num_4x4_blocks_wide,
1507 4 * num_4x4_blocks_high, 0,
1508 vp9_get_interp_kernel(mbmi->interp_filter),
1510 mi_col * MI_SIZE + 4 * (i & 0x01),
1511 mi_row * MI_SIZE + 4 * (i >> 1));
1513 #if CONFIG_VP9_HIGHBITDEPTH
1517 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
1520 this_rdc.rate += b_rate;
1521 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1522 this_rdc.rate, this_rdc.dist);
1523 if (this_rdc.rdcost < b_best_rd) {
1524 b_best_rd = this_rdc.rdcost;
1525 bsi[ref_frame][i].as_mode = this_mode;
1526 bsi[ref_frame][i].as_mv[0].as_mv = xd->mi[0].bmi[i].as_mv[0].as_mv;
1530 // restore source and prediction buffer pointers.
1532 pd->pre[0] = orig_pre[0];
1534 this_rd += b_best_rd;
1536 xd->mi[0].bmi[i] = bsi[ref_frame][i];
1537 if (num_4x4_blocks_wide > 1)
1538 xd->mi[0].bmi[i + 1] = xd->mi[0].bmi[i];
1539 if (num_4x4_blocks_high > 1)
1540 xd->mi[0].bmi[i + 2] = xd->mi[0].bmi[i];
1542 } // loop through sub8x8 blocks
1544 if (this_rd < best_rd) {
1546 best_ref_frame = ref_frame;
1548 } // reference frames
1550 mbmi->tx_size = TX_4X4;
1551 mbmi->ref_frame[0] = best_ref_frame;
1552 for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
1553 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
1554 const int block = idy * 2 + idx;
1555 xd->mi[0].bmi[block] = bsi[best_ref_frame][block];
1556 if (num_4x4_blocks_wide > 1)
1557 xd->mi[0].bmi[block + 1] = bsi[best_ref_frame][block];
1558 if (num_4x4_blocks_high > 1)
1559 xd->mi[0].bmi[block + 2] = bsi[best_ref_frame][block];
1562 mbmi->mode = xd->mi[0].bmi[3].as_mode;
1563 ctx->mic = *(xd->mi[0].src_mi);
1564 ctx->skip_txfm[0] = 0;
1566 // Dummy assignment for speed -5. No effect in speed -6.
1567 rd_cost->rdcost = best_rd;