Merge "Move the definition of switchable filter numbers into enum INTERP_FILTER;...
[aom.git] / vp9 / encoder / vp9_pickmode.c
blob28f12916eb20f1292ceec7eb168b16b2db9604e3
1 /*
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.
9 */
11 #include <assert.h>
12 #include <limits.h>
13 #include <math.h>
14 #include <stdio.h>
16 #include "./vp9_rtcd.h"
18 #include "vpx_mem/vpx_mem.h"
20 #include "vp9/common/vp9_blockd.h"
21 #include "vp9/common/vp9_common.h"
22 #include "vp9/common/vp9_mvref_common.h"
23 #include "vp9/common/vp9_reconinter.h"
24 #include "vp9/common/vp9_reconintra.h"
26 #include "vp9/encoder/vp9_encoder.h"
27 #include "vp9/encoder/vp9_pickmode.h"
28 #include "vp9/encoder/vp9_ratectrl.h"
29 #include "vp9/encoder/vp9_rd.h"
31 typedef struct {
32 uint8_t *data;
33 int stride;
34 int in_use;
35 } PRED_BUFFER;
37 static int mv_refs_rt(const VP9_COMMON *cm, const MACROBLOCKD *xd,
38 const TileInfo *const tile,
39 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
40 int_mv *mv_ref_list,
41 int mi_row, int mi_col) {
42 const int *ref_sign_bias = cm->ref_frame_sign_bias;
43 int i, refmv_count = 0;
45 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
47 int different_ref_found = 0;
48 int context_counter = 0;
49 int const_motion = 0;
51 // Blank the reference vector list
52 vpx_memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
54 // The nearest 2 blocks are treated differently
55 // if the size < 8x8 we get the mv from the bmi substructure,
56 // and we also need to keep a mode count.
57 for (i = 0; i < 2; ++i) {
58 const POSITION *const mv_ref = &mv_ref_search[i];
59 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
60 const MODE_INFO *const candidate_mi = xd->mi[mv_ref->col + mv_ref->row *
61 xd->mi_stride].src_mi;
62 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
63 // Keep counts for entropy encoding.
64 context_counter += mode_2_counter[candidate->mode];
65 different_ref_found = 1;
67 if (candidate->ref_frame[0] == ref_frame)
68 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, -1),
69 refmv_count, mv_ref_list, Done);
73 const_motion = 1;
75 // Check the rest of the neighbors in much the same way
76 // as before except we don't need to keep track of sub blocks or
77 // mode counts.
78 for (; i < MVREF_NEIGHBOURS && !refmv_count; ++i) {
79 const POSITION *const mv_ref = &mv_ref_search[i];
80 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
81 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row *
82 xd->mi_stride].src_mi->mbmi;
83 different_ref_found = 1;
85 if (candidate->ref_frame[0] == ref_frame)
86 ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, Done);
90 // Since we couldn't find 2 mvs from the same reference frame
91 // go back through the neighbors and find motion vectors from
92 // different reference frames.
93 if (different_ref_found && !refmv_count) {
94 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
95 const POSITION *mv_ref = &mv_ref_search[i];
96 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
97 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
98 * xd->mi_stride].src_mi->mbmi;
100 // If the candidate is INTRA we don't want to consider its mv.
101 IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
102 refmv_count, mv_ref_list, Done);
107 Done:
109 mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
111 // Clamp vectors
112 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
113 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
115 return const_motion;
118 static int combined_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
119 BLOCK_SIZE bsize, int mi_row, int mi_col,
120 int_mv *tmp_mv, int *rate_mv,
121 int64_t best_rd_sofar) {
122 MACROBLOCKD *xd = &x->e_mbd;
123 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
124 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0, 0}};
125 const int step_param = cpi->sf.mv.fullpel_search_step_param;
126 const int sadpb = x->sadperbit16;
127 MV mvp_full;
128 const int ref = mbmi->ref_frame[0];
129 const MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
130 int dis;
131 int rate_mode;
132 const int tmp_col_min = x->mv_col_min;
133 const int tmp_col_max = x->mv_col_max;
134 const int tmp_row_min = x->mv_row_min;
135 const int tmp_row_max = x->mv_row_max;
136 int rv = 0;
137 int cost_list[5];
138 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
139 ref);
140 if (cpi->common.show_frame &&
141 (x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[LAST_FRAME])
142 return rv;
144 if (scaled_ref_frame) {
145 int i;
146 // Swap out the reference frame for a version that's been scaled to
147 // match the resolution of the current frame, allowing the existing
148 // motion search code to be used without additional modifications.
149 for (i = 0; i < MAX_MB_PLANE; i++)
150 backup_yv12[i] = xd->plane[i].pre[0];
151 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
153 vp9_set_mv_search_range(x, &ref_mv);
155 assert(x->mv_best_ref_index[ref] <= 2);
156 if (x->mv_best_ref_index[ref] < 2)
157 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
158 else
159 mvp_full = x->pred_mv[ref];
161 mvp_full.col >>= 3;
162 mvp_full.row >>= 3;
164 vp9_full_pixel_search(cpi, x, bsize, &mvp_full, step_param, sadpb,
165 cond_cost_list(cpi, cost_list),
166 &ref_mv, &tmp_mv->as_mv, INT_MAX, 0);
168 x->mv_col_min = tmp_col_min;
169 x->mv_col_max = tmp_col_max;
170 x->mv_row_min = tmp_row_min;
171 x->mv_row_max = tmp_row_max;
173 // calculate the bit cost on motion vector
174 mvp_full.row = tmp_mv->as_mv.row * 8;
175 mvp_full.col = tmp_mv->as_mv.col * 8;
177 *rate_mv = vp9_mv_bit_cost(&mvp_full, &ref_mv,
178 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
180 rate_mode = cpi->inter_mode_cost[mbmi->mode_context[ref]]
181 [INTER_OFFSET(NEWMV)];
182 rv = !(RDCOST(x->rdmult, x->rddiv, (*rate_mv + rate_mode), 0) >
183 best_rd_sofar);
185 if (rv) {
186 cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv,
187 cpi->common.allow_high_precision_mv,
188 x->errorperbit,
189 &cpi->fn_ptr[bsize],
190 cpi->sf.mv.subpel_force_stop,
191 cpi->sf.mv.subpel_iters_per_step,
192 cond_cost_list(cpi, cost_list),
193 x->nmvjointcost, x->mvcost,
194 &dis, &x->pred_sse[ref], NULL, 0, 0);
195 x->pred_mv[ref] = tmp_mv->as_mv;
198 if (scaled_ref_frame) {
199 int i;
200 for (i = 0; i < MAX_MB_PLANE; i++)
201 xd->plane[i].pre[0] = backup_yv12[i];
203 return rv;
207 static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize,
208 MACROBLOCK *x, MACROBLOCKD *xd,
209 int *out_rate_sum, int64_t *out_dist_sum,
210 unsigned int *var_y, unsigned int *sse_y) {
211 // Note our transform coeffs are 8 times an orthogonal transform.
212 // Hence quantizer step is also 8 times. To get effective quantizer
213 // we need to divide by 8 before sending to modeling function.
214 unsigned int sse;
215 int rate;
216 int64_t dist;
217 struct macroblock_plane *const p = &x->plane[0];
218 struct macroblockd_plane *const pd = &xd->plane[0];
219 const uint32_t dc_quant = pd->dequant[0];
220 const uint32_t ac_quant = pd->dequant[1];
221 unsigned int var = cpi->fn_ptr[bsize].vf(p->src.buf, p->src.stride,
222 pd->dst.buf, pd->dst.stride, &sse);
223 *var_y = var;
224 *sse_y = sse;
226 if (sse < dc_quant * dc_quant >> 6)
227 x->skip_txfm[0] = 1;
228 else if (var < ac_quant * ac_quant >> 6)
229 x->skip_txfm[0] = 2;
230 else
231 x->skip_txfm[0] = 0;
233 if (cpi->common.tx_mode == TX_MODE_SELECT) {
234 if (sse > (var << 2))
235 xd->mi[0].src_mi->mbmi.tx_size =
236 MIN(max_txsize_lookup[bsize],
237 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
238 else
239 xd->mi[0].src_mi->mbmi.tx_size = TX_8X8;
241 if (cpi->sf.partition_search_type == VAR_BASED_PARTITION &&
242 xd->mi[0].src_mi->mbmi.tx_size > TX_16X16)
243 xd->mi[0].src_mi->mbmi.tx_size = TX_16X16;
244 } else {
245 xd->mi[0].src_mi->mbmi.tx_size =
246 MIN(max_txsize_lookup[bsize],
247 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
250 #if CONFIG_VP9_HIGHBITDEPTH
251 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
252 vp9_model_rd_from_var_lapndz(sse - var, 1 << num_pels_log2_lookup[bsize],
253 dc_quant >> (xd->bd - 5), &rate, &dist);
254 } else {
255 vp9_model_rd_from_var_lapndz(sse - var, 1 << num_pels_log2_lookup[bsize],
256 dc_quant >> 3, &rate, &dist);
258 #else
259 vp9_model_rd_from_var_lapndz(sse - var, 1 << num_pels_log2_lookup[bsize],
260 dc_quant >> 3, &rate, &dist);
261 #endif // CONFIG_VP9_HIGHBITDEPTH
263 *out_rate_sum = rate >> 1;
264 *out_dist_sum = dist << 3;
266 #if CONFIG_VP9_HIGHBITDEPTH
267 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
268 vp9_model_rd_from_var_lapndz(var,
269 1 << num_pels_log2_lookup[bsize],
270 ac_quant >> (xd->bd - 5),
271 &rate,
272 &dist);
273 } else {
274 vp9_model_rd_from_var_lapndz(var,
275 1 << num_pels_log2_lookup[bsize],
276 ac_quant >> 3,
277 &rate,
278 &dist);
280 #else
281 vp9_model_rd_from_var_lapndz(var,
282 1 << num_pels_log2_lookup[bsize],
283 ac_quant >> 3,
284 &rate,
285 &dist);
286 #endif // CONFIG_VP9_HIGHBITDEPTH
288 *out_rate_sum += rate;
289 *out_dist_sum += dist << 4;
292 static int get_pred_buffer(PRED_BUFFER *p, int len) {
293 int i;
295 for (i = 0; i < len; i++) {
296 if (!p[i].in_use) {
297 p[i].in_use = 1;
298 return i;
301 return -1;
304 static void free_pred_buffer(PRED_BUFFER *p) {
305 if (p != NULL)
306 p->in_use = 0;
309 static void encode_breakout_test(VP9_COMP *cpi, MACROBLOCK *x,
310 BLOCK_SIZE bsize, int mi_row, int mi_col,
311 MV_REFERENCE_FRAME ref_frame,
312 PREDICTION_MODE this_mode,
313 unsigned int var_y, unsigned int sse_y,
314 struct buf_2d yv12_mb[][MAX_MB_PLANE],
315 int *rate, int64_t *dist) {
316 MACROBLOCKD *xd = &x->e_mbd;
317 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
319 const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]);
320 unsigned int var = var_y, sse = sse_y;
321 // Skipping threshold for ac.
322 unsigned int thresh_ac;
323 // Skipping threshold for dc.
324 unsigned int thresh_dc;
325 if (x->encode_breakout > 0) {
326 // Set a maximum for threshold to avoid big PSNR loss in low bit rate
327 // case. Use extreme low threshold for static frames to limit
328 // skipping.
329 const unsigned int max_thresh = 36000;
330 // The encode_breakout input
331 const unsigned int min_thresh =
332 MIN(((unsigned int)x->encode_breakout << 4), max_thresh);
333 #if CONFIG_VP9_HIGHBITDEPTH
334 const int shift = 2 * xd->bd - 16;
335 #endif
337 // Calculate threshold according to dequant value.
338 thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) / 9;
339 #if CONFIG_VP9_HIGHBITDEPTH
340 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
341 thresh_ac = ROUND_POWER_OF_TWO(thresh_ac, shift);
343 #endif // CONFIG_VP9_HIGHBITDEPTH
344 thresh_ac = clamp(thresh_ac, min_thresh, max_thresh);
346 // Adjust ac threshold according to partition size.
347 thresh_ac >>=
348 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
350 thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6);
351 #if CONFIG_VP9_HIGHBITDEPTH
352 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) && shift > 0) {
353 thresh_dc = ROUND_POWER_OF_TWO(thresh_dc, shift);
355 #endif // CONFIG_VP9_HIGHBITDEPTH
356 } else {
357 thresh_ac = 0;
358 thresh_dc = 0;
361 // Y skipping condition checking for ac and dc.
362 if (var <= thresh_ac && (sse - var) <= thresh_dc) {
363 unsigned int sse_u, sse_v;
364 unsigned int var_u, var_v;
366 // Skip UV prediction unless breakout is zero (lossless) to save
367 // computation with low impact on the result
368 if (x->encode_breakout == 0) {
369 xd->plane[1].pre[0] = yv12_mb[ref_frame][1];
370 xd->plane[2].pre[0] = yv12_mb[ref_frame][2];
371 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, bsize);
374 var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf,
375 x->plane[1].src.stride,
376 xd->plane[1].dst.buf,
377 xd->plane[1].dst.stride, &sse_u);
379 // U skipping condition checking
380 if ((var_u * 4 <= thresh_ac) && (sse_u - var_u <= thresh_dc)) {
381 var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf,
382 x->plane[2].src.stride,
383 xd->plane[2].dst.buf,
384 xd->plane[2].dst.stride, &sse_v);
386 // V skipping condition checking
387 if ((var_v * 4 <= thresh_ac) && (sse_v - var_v <= thresh_dc)) {
388 x->skip = 1;
390 // The cost of skip bit needs to be added.
391 *rate = cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
392 [INTER_OFFSET(this_mode)];
394 // More on this part of rate
395 // rate += vp9_cost_bit(vp9_get_skip_prob(cm, xd), 1);
397 // Scaling factor for SSE from spatial domain to frequency
398 // domain is 16. Adjust distortion accordingly.
399 // TODO(yunqingwang): In this function, only y-plane dist is
400 // calculated.
401 *dist = (sse << 4); // + ((sse_u + sse_v) << 4);
403 // *disable_skip = 1;
409 struct estimate_block_intra_args {
410 VP9_COMP *cpi;
411 MACROBLOCK *x;
412 PREDICTION_MODE mode;
413 int rate;
414 int64_t dist;
417 static void estimate_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
418 TX_SIZE tx_size, void *arg) {
419 struct estimate_block_intra_args* const args = arg;
420 VP9_COMP *const cpi = args->cpi;
421 MACROBLOCK *const x = args->x;
422 MACROBLOCKD *const xd = &x->e_mbd;
423 struct macroblock_plane *const p = &x->plane[0];
424 struct macroblockd_plane *const pd = &xd->plane[0];
425 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size];
426 uint8_t *const src_buf_base = p->src.buf;
427 uint8_t *const dst_buf_base = pd->dst.buf;
428 const int src_stride = p->src.stride;
429 const int dst_stride = pd->dst.stride;
430 int i, j;
431 int rate;
432 int64_t dist;
433 unsigned int var_y, sse_y;
434 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
435 assert(plane == 0);
436 (void) plane;
438 p->src.buf = &src_buf_base[4 * (j * src_stride + i)];
439 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)];
440 // Use source buffer as an approximation for the fully reconstructed buffer.
441 vp9_predict_intra_block(xd, block >> (2 * tx_size),
442 b_width_log2_lookup[plane_bsize],
443 tx_size, args->mode,
444 p->src.buf, src_stride,
445 pd->dst.buf, dst_stride,
446 i, j, 0);
447 // This procedure assumes zero offset from p->src.buf and pd->dst.buf.
448 model_rd_for_sb_y(cpi, bsize_tx, x, xd, &rate, &dist, &var_y, &sse_y);
449 p->src.buf = src_buf_base;
450 pd->dst.buf = dst_buf_base;
451 args->rate += rate;
452 args->dist += dist;
455 static const THR_MODES mode_idx[MAX_REF_FRAMES][4] = {
456 {THR_DC, THR_H_PRED, THR_V_PRED},
457 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV},
458 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG},
459 {THR_NEARESTA, THR_NEARA, THR_ZEROA, THR_NEWA},
462 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
463 // this needs various further optimizations. to be continued..
464 void vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
465 TileDataEnc *tile_data,
466 int mi_row, int mi_col, RD_COST *rd_cost,
467 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
468 VP9_COMMON *const cm = &cpi->common;
469 TileInfo *const tile_info = &tile_data->tile_info;
470 MACROBLOCKD *const xd = &x->e_mbd;
471 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
472 struct macroblockd_plane *const pd = &xd->plane[0];
473 PREDICTION_MODE best_mode = ZEROMV;
474 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
475 TX_SIZE best_tx_size = MIN(max_txsize_lookup[bsize],
476 tx_mode_to_biggest_tx_size[cm->tx_mode]);
477 INTERP_FILTER best_pred_filter = EIGHTTAP;
478 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
479 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
480 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
481 VP9_ALT_FLAG };
482 RD_COST this_rdc, best_rdc;
483 uint8_t skip_txfm = 0;
484 // var_y and sse_y are saved to be used in skipping checking
485 unsigned int var_y = UINT_MAX;
486 unsigned int sse_y = UINT_MAX;
487 // Reduce the intra cost penalty for small blocks (<=16x16).
488 const int reduction_fac =
489 (cpi->sf.partition_search_type == VAR_BASED_PARTITION &&
490 bsize <= BLOCK_16X16) ? 4 : 1;
491 const int intra_cost_penalty = vp9_get_intra_cost_penalty(
492 cm->base_qindex, cm->y_dc_delta_q, cm->bit_depth) / reduction_fac;
493 const int64_t inter_mode_thresh = RDCOST(x->rdmult, x->rddiv,
494 intra_cost_penalty, 0);
495 const int intra_mode_cost = 50;
497 const int8_t segment_id = mbmi->segment_id;
498 const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize];
499 const int *const rd_thresh_freq_fact = tile_data->thresh_freq_fact[bsize];
500 INTERP_FILTER filter_ref;
501 const int bsl = mi_width_log2_lookup[bsize];
502 const int pred_filter_search = cm->interp_filter == SWITCHABLE ?
503 (((mi_row + mi_col) >> bsl) +
504 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0;
505 int const_motion[MAX_REF_FRAMES] = { 0 };
506 const int bh = num_4x4_blocks_high_lookup[bsize] << 2;
507 const int bw = num_4x4_blocks_wide_lookup[bsize] << 2;
508 // For speed 6, the result of interp filter is reused later in actual encoding
509 // process.
510 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers.
511 PRED_BUFFER tmp[4];
512 DECLARE_ALIGNED_ARRAY(16, uint8_t, pred_buf, 3 * 64 * 64);
513 #if CONFIG_VP9_HIGHBITDEPTH
514 DECLARE_ALIGNED_ARRAY(16, uint16_t, pred_buf_16, 3 * 64 * 64);
515 #endif
516 struct buf_2d orig_dst = pd->dst;
517 PRED_BUFFER *best_pred = NULL;
518 PRED_BUFFER *this_mode_pred = NULL;
519 const int pixels_in_block = bh * bw;
521 if (cpi->sf.reuse_inter_pred_sby) {
522 int i;
523 for (i = 0; i < 3; i++) {
524 #if CONFIG_VP9_HIGHBITDEPTH
525 if (cm->use_highbitdepth)
526 tmp[i].data = CONVERT_TO_BYTEPTR(&pred_buf_16[pixels_in_block * i]);
527 else
528 tmp[i].data = &pred_buf[pixels_in_block * i];
529 #else
530 tmp[i].data = &pred_buf[pixels_in_block * i];
531 #endif // CONFIG_VP9_HIGHBITDEPTH
532 tmp[i].stride = bw;
533 tmp[i].in_use = 0;
535 tmp[3].data = pd->dst.buf;
536 tmp[3].stride = pd->dst.stride;
537 tmp[3].in_use = 0;
540 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
541 x->skip = 0;
543 if (xd->up_available)
544 filter_ref = xd->mi[-xd->mi_stride].src_mi->mbmi.interp_filter;
545 else if (xd->left_available)
546 filter_ref = xd->mi[-1].src_mi->mbmi.interp_filter;
547 else
548 filter_ref = cm->interp_filter;
550 // initialize mode decisions
551 vp9_rd_cost_reset(&best_rdc);
552 vp9_rd_cost_reset(&this_rdc);
553 vp9_rd_cost_reset(rd_cost);
554 vpx_memset(mbmi, 0, sizeof(MB_MODE_INFO));
555 mbmi->sb_type = bsize;
556 mbmi->ref_frame[0] = NONE;
557 mbmi->ref_frame[1] = NONE;
558 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
559 tx_mode_to_biggest_tx_size[cm->tx_mode]);
560 mbmi->interp_filter = cm->interp_filter == SWITCHABLE ?
561 EIGHTTAP : cm->interp_filter;
562 mbmi->segment_id = segment_id;
564 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ++ref_frame) {
565 PREDICTION_MODE this_mode;
566 x->pred_mv_sad[ref_frame] = INT_MAX;
567 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
568 frame_mv[ZEROMV][ref_frame].as_int = 0;
570 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
571 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
572 int_mv *const candidates = mbmi->ref_mvs[ref_frame];
573 const struct scale_factors *const sf = &cm->frame_refs[ref_frame - 1].sf;
574 vp9_setup_pred_block(xd, yv12_mb[ref_frame], yv12, mi_row, mi_col,
575 sf, sf);
577 if (!cm->error_resilient_mode)
578 vp9_find_mv_refs(cm, xd, tile_info, xd->mi[0].src_mi, ref_frame,
579 candidates, mi_row, mi_col);
580 else
581 const_motion[ref_frame] = mv_refs_rt(cm, xd, tile_info,
582 xd->mi[0].src_mi,
583 ref_frame, candidates,
584 mi_row, mi_col);
586 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, candidates,
587 &frame_mv[NEARESTMV][ref_frame],
588 &frame_mv[NEARMV][ref_frame]);
590 if (!vp9_is_scaled(sf) && bsize >= BLOCK_8X8)
591 vp9_mv_pred(cpi, x, yv12_mb[ref_frame][0].buf, yv12->y_stride,
592 ref_frame, bsize);
593 } else {
594 continue;
597 // Select prediction reference frames.
598 xd->plane[0].pre[0] = yv12_mb[ref_frame][0];
600 clamp_mv2(&frame_mv[NEARESTMV][ref_frame].as_mv, xd);
601 clamp_mv2(&frame_mv[NEARMV][ref_frame].as_mv, xd);
603 mbmi->ref_frame[0] = ref_frame;
605 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
606 int rate_mv = 0;
607 int mode_rd_thresh;
609 if (const_motion[ref_frame] && this_mode == NEARMV)
610 continue;
612 if (!(cpi->sf.inter_mode_mask[bsize] & (1 << this_mode)))
613 continue;
615 mode_rd_thresh =
616 rd_threshes[mode_idx[ref_frame][INTER_OFFSET(this_mode)]];
617 if (rd_less_than_thresh(best_rdc.rdcost, mode_rd_thresh,
618 rd_thresh_freq_fact[this_mode]))
619 continue;
621 if (this_mode == NEWMV) {
622 if (ref_frame > LAST_FRAME)
623 continue;
624 if (cpi->sf.partition_search_type != VAR_BASED_PARTITION &&
625 this_rdc.rdcost < (int64_t)(1 << num_pels_log2_lookup[bsize]))
626 continue;
627 if (!combined_motion_search(cpi, x, bsize, mi_row, mi_col,
628 &frame_mv[NEWMV][ref_frame],
629 &rate_mv, best_rdc.rdcost))
630 continue;
633 if (this_mode != NEARESTMV &&
634 frame_mv[this_mode][ref_frame].as_int ==
635 frame_mv[NEARESTMV][ref_frame].as_int)
636 continue;
638 mbmi->mode = this_mode;
639 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
641 // Search for the best prediction filter type, when the resulting
642 // motion vector is at sub-pixel accuracy level for luma component, i.e.,
643 // the last three bits are all zeros.
644 if (cpi->sf.reuse_inter_pred_sby) {
645 if (!this_mode_pred) {
646 this_mode_pred = &tmp[3];
647 } else {
648 this_mode_pred = &tmp[get_pred_buffer(tmp, 3)];
649 pd->dst.buf = this_mode_pred->data;
650 pd->dst.stride = bw;
654 if ((this_mode == NEWMV || filter_ref == SWITCHABLE) &&
655 pred_filter_search &&
656 ((mbmi->mv[0].as_mv.row & 0x07) != 0 ||
657 (mbmi->mv[0].as_mv.col & 0x07) != 0)) {
658 int pf_rate[3];
659 int64_t pf_dist[3];
660 unsigned int pf_var[3];
661 unsigned int pf_sse[3];
662 TX_SIZE pf_tx_size[3];
663 int64_t best_cost = INT64_MAX;
664 INTERP_FILTER best_filter = SWITCHABLE, filter;
665 PRED_BUFFER *current_pred = this_mode_pred;
667 for (filter = EIGHTTAP; filter <= EIGHTTAP_SHARP; ++filter) {
668 int64_t cost;
669 mbmi->interp_filter = filter;
670 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
671 model_rd_for_sb_y(cpi, bsize, x, xd, &pf_rate[filter],
672 &pf_dist[filter], &pf_var[filter], &pf_sse[filter]);
673 cost = RDCOST(x->rdmult, x->rddiv,
674 vp9_get_switchable_rate(cpi) + pf_rate[filter],
675 pf_dist[filter]);
676 pf_tx_size[filter] = mbmi->tx_size;
677 if (cost < best_cost) {
678 best_filter = filter;
679 best_cost = cost;
680 skip_txfm = x->skip_txfm[0];
682 if (cpi->sf.reuse_inter_pred_sby) {
683 if (this_mode_pred != current_pred) {
684 free_pred_buffer(this_mode_pred);
685 this_mode_pred = current_pred;
688 if (filter < EIGHTTAP_SHARP) {
689 current_pred = &tmp[get_pred_buffer(tmp, 3)];
690 pd->dst.buf = current_pred->data;
691 pd->dst.stride = bw;
697 if (cpi->sf.reuse_inter_pred_sby && this_mode_pred != current_pred)
698 free_pred_buffer(current_pred);
700 mbmi->interp_filter = best_filter;
701 mbmi->tx_size = pf_tx_size[mbmi->interp_filter];
702 this_rdc.rate = pf_rate[mbmi->interp_filter];
703 this_rdc.dist = pf_dist[mbmi->interp_filter];
704 var_y = pf_var[mbmi->interp_filter];
705 sse_y = pf_sse[mbmi->interp_filter];
706 x->skip_txfm[0] = skip_txfm;
707 } else {
708 mbmi->interp_filter = (filter_ref == SWITCHABLE) ? EIGHTTAP: filter_ref;
709 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
710 model_rd_for_sb_y(cpi, bsize, x, xd, &this_rdc.rate, &this_rdc.dist,
711 &var_y, &sse_y);
714 this_rdc.rate += rate_mv;
715 this_rdc.rate += cpi->inter_mode_cost[mbmi->mode_context[ref_frame]]
716 [INTER_OFFSET(this_mode)];
717 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
718 this_rdc.rate, this_rdc.dist);
720 // Skipping checking: test to see if this block can be reconstructed by
721 // prediction only.
722 if (cpi->allow_encode_breakout) {
723 encode_breakout_test(cpi, x, bsize, mi_row, mi_col, ref_frame,
724 this_mode, var_y, sse_y, yv12_mb,
725 &this_rdc.rate, &this_rdc.dist);
726 if (x->skip) {
727 this_rdc.rate += rate_mv;
728 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
729 this_rdc.rate, this_rdc.dist);
733 #if CONFIG_VP9_TEMPORAL_DENOISING
734 if (cpi->oxcf.noise_sensitivity > 0) {
735 vp9_denoiser_update_frame_stats(mbmi, sse_y, this_mode, ctx);
737 #else
738 (void)ctx;
739 #endif
741 if (this_rdc.rdcost < best_rdc.rdcost || x->skip) {
742 best_rdc = this_rdc;
743 best_mode = this_mode;
744 best_pred_filter = mbmi->interp_filter;
745 best_tx_size = mbmi->tx_size;
746 best_ref_frame = ref_frame;
747 skip_txfm = x->skip_txfm[0];
749 if (cpi->sf.reuse_inter_pred_sby) {
750 free_pred_buffer(best_pred);
752 best_pred = this_mode_pred;
754 } else {
755 if (cpi->sf.reuse_inter_pred_sby)
756 free_pred_buffer(this_mode_pred);
759 if (x->skip)
760 break;
763 // Check that a prediction mode has been selected.
764 assert(best_rdc.rdcost < INT64_MAX);
767 // If best prediction is not in dst buf, then copy the prediction block from
768 // temp buf to dst buf.
769 if (best_pred != NULL && cpi->sf.reuse_inter_pred_sby &&
770 best_pred->data != orig_dst.buf) {
771 pd->dst = orig_dst;
772 #if CONFIG_VP9_HIGHBITDEPTH
773 if (cm->use_highbitdepth) {
774 vp9_highbd_convolve_copy(best_pred->data, bw, pd->dst.buf, pd->dst.stride,
775 NULL, 0, NULL, 0, bw, bh, xd->bd);
776 } else {
777 vp9_convolve_copy(best_pred->data, bw, pd->dst.buf, pd->dst.stride,
778 NULL, 0, NULL, 0, bw, bh);
780 #else
781 vp9_convolve_copy(best_pred->data, bw, pd->dst.buf, pd->dst.stride, NULL, 0,
782 NULL, 0, bw, bh);
783 #endif // CONFIG_VP9_HIGHBITDEPTH
786 mbmi->mode = best_mode;
787 mbmi->interp_filter = best_pred_filter;
788 mbmi->tx_size = best_tx_size;
789 mbmi->ref_frame[0] = best_ref_frame;
790 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
791 xd->mi[0].src_mi->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
792 x->skip_txfm[0] = skip_txfm;
794 // Perform intra prediction search, if the best SAD is above a certain
795 // threshold.
796 if (!x->skip && best_rdc.rdcost > inter_mode_thresh &&
797 bsize <= cpi->sf.max_intra_bsize) {
798 PREDICTION_MODE this_mode;
799 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 };
800 const TX_SIZE intra_tx_size =
801 MIN(max_txsize_lookup[bsize],
802 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
804 if (cpi->sf.reuse_inter_pred_sby) {
805 pd->dst.buf = tmp[0].data;
806 pd->dst.stride = bw;
809 for (this_mode = DC_PRED; this_mode <= DC_PRED; ++this_mode) {
810 const TX_SIZE saved_tx_size = mbmi->tx_size;
811 args.mode = this_mode;
812 args.rate = 0;
813 args.dist = 0;
814 mbmi->tx_size = intra_tx_size;
815 vp9_foreach_transformed_block_in_plane(xd, bsize, 0,
816 estimate_block_intra, &args);
817 mbmi->tx_size = saved_tx_size;
818 this_rdc.rate = args.rate;
819 this_rdc.dist = args.dist;
820 this_rdc.rate += cpi->mbmode_cost[this_mode];
821 this_rdc.rate += intra_cost_penalty;
822 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
823 this_rdc.rate, this_rdc.dist);
825 if (this_rdc.rdcost + intra_mode_cost < best_rdc.rdcost) {
826 best_rdc = this_rdc;
827 mbmi->mode = this_mode;
828 mbmi->tx_size = intra_tx_size;
829 mbmi->ref_frame[0] = INTRA_FRAME;
830 mbmi->uv_mode = this_mode;
831 mbmi->mv[0].as_int = INVALID_MV;
832 } else {
833 x->skip_txfm[0] = skip_txfm;
836 if (cpi->sf.reuse_inter_pred_sby)
837 pd->dst = orig_dst;
840 if (is_inter_block(mbmi))
841 vp9_update_rd_thresh_fact(cpi, tile_data, bsize,
842 mode_idx[ref_frame][INTER_OFFSET(mbmi->mode)]);
843 else
844 vp9_update_rd_thresh_fact(cpi, tile_data, bsize,
845 mode_idx[ref_frame][mbmi->mode]);
847 *rd_cost = best_rdc;