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_common.h"
21 #include "vp9/common/vp9_mvref_common.h"
22 #include "vp9/common/vp9_reconinter.h"
23 #include "vp9/common/vp9_reconintra.h"
25 #include "vp9/encoder/vp9_onyx_int.h"
26 #include "vp9/encoder/vp9_ratectrl.h"
27 #include "vp9/encoder/vp9_rdopt.h"
29 static int full_pixel_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
30 const TileInfo *const tile,
31 BLOCK_SIZE bsize, int mi_row, int mi_col,
32 int_mv *tmp_mv, int *rate_mv) {
33 MACROBLOCKD *xd = &x->e_mbd;
34 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
35 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}};
36 int bestsme = INT_MAX;
38 int sadpb = x->sadperbit16;
40 int ref = mbmi->ref_frame[0];
41 const MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
44 int tmp_col_min = x->mv_col_min;
45 int tmp_col_max = x->mv_col_max;
46 int tmp_row_min = x->mv_row_min;
47 int tmp_row_max = x->mv_row_max;
50 int stride = xd->plane[0].pre[0].stride;
52 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
54 if (scaled_ref_frame) {
56 // Swap out the reference frame for a version that's been scaled to
57 // match the resolution of the current frame, allowing the existing
58 // motion search code to be used without additional modifications.
59 for (i = 0; i < MAX_MB_PLANE; i++)
60 backup_yv12[i] = xd->plane[i].pre[0];
62 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
65 vp9_set_mv_search_range(x, &ref_mv);
67 // TODO(jingning) exploiting adaptive motion search control in non-RD
71 for (i = LAST_FRAME; i <= LAST_FRAME && cpi->common.show_frame; ++i) {
72 if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) {
73 tmp_mv->as_int = INVALID_MV;
75 if (scaled_ref_frame) {
77 for (i = 0; i < MAX_MB_PLANE; i++)
78 xd->plane[i].pre[0] = backup_yv12[i];
84 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
89 if (cpi->sf.search_method == FAST_HEX) {
90 // NOTE: this returns SAD
91 vp9_fast_hex_search(x, &mvp_full, step_param, sadpb, 0,
92 &cpi->fn_ptr[bsize], 1,
93 &ref_mv, &tmp_mv->as_mv);
94 } else if (cpi->sf.search_method == HEX) {
95 // NOTE: this returns SAD
96 vp9_hex_search(x, &mvp_full, step_param, sadpb, 1,
97 &cpi->fn_ptr[bsize], 1,
98 &ref_mv, &tmp_mv->as_mv);
99 } else if (cpi->sf.search_method == SQUARE) {
100 // NOTE: this returns SAD
101 vp9_square_search(x, &mvp_full, step_param, sadpb, 1,
102 &cpi->fn_ptr[bsize], 1,
103 &ref_mv, &tmp_mv->as_mv);
104 } else if (cpi->sf.search_method == BIGDIA) {
105 // NOTE: this returns SAD
106 vp9_bigdia_search(x, &mvp_full, step_param, sadpb, 1,
107 &cpi->fn_ptr[bsize], 1,
108 &ref_mv, &tmp_mv->as_mv);
110 int further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
111 // NOTE: this returns variance
112 vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param,
113 sadpb, further_steps, 1,
115 &ref_mv, &tmp_mv->as_mv);
117 x->mv_col_min = tmp_col_min;
118 x->mv_col_max = tmp_col_max;
119 x->mv_row_min = tmp_row_min;
120 x->mv_row_max = tmp_row_max;
122 if (scaled_ref_frame) {
124 for (i = 0; i < MAX_MB_PLANE; i++)
125 xd->plane[i].pre[0] = backup_yv12[i];
128 // TODO(jingning) This step can be merged into full pixel search step in the
129 // re-designed log-diamond search
130 buf_offset = tmp_mv->as_mv.row * stride + tmp_mv->as_mv.col;
132 // Find sad for current vector.
133 bestsme = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, x->plane[0].src.stride,
134 xd->plane[0].pre[0].buf + buf_offset,
137 // scale to 1/8 pixel resolution
138 tmp_mv->as_mv.row = tmp_mv->as_mv.row * 8;
139 tmp_mv->as_mv.col = tmp_mv->as_mv.col * 8;
141 // calculate the bit cost on motion vector
142 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv,
143 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
147 static void sub_pixel_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
148 const TileInfo *const tile,
149 BLOCK_SIZE bsize, int mi_row, int mi_col,
151 MACROBLOCKD *xd = &x->e_mbd;
152 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
153 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}};
154 int ref = mbmi->ref_frame[0];
155 MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
158 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
160 if (scaled_ref_frame) {
162 // Swap out the reference frame for a version that's been scaled to
163 // match the resolution of the current frame, allowing the existing
164 // motion search code to be used without additional modifications.
165 for (i = 0; i < MAX_MB_PLANE; i++)
166 backup_yv12[i] = xd->plane[i].pre[0];
168 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
174 cpi->find_fractional_mv_step(x, tmp_mv, &ref_mv,
175 cpi->common.allow_high_precision_mv,
178 cpi->sf.subpel_force_stop,
179 cpi->sf.subpel_iters_per_step,
180 x->nmvjointcost, x->mvcost,
181 &dis, &x->pred_sse[ref]);
183 if (scaled_ref_frame) {
185 for (i = 0; i < MAX_MB_PLANE; i++)
186 xd->plane[i].pre[0] = backup_yv12[i];
190 static void model_rd_for_sb_y(VP9_COMP *cpi, BLOCK_SIZE bsize,
191 MACROBLOCK *x, MACROBLOCKD *xd,
192 int *out_rate_sum, int64_t *out_dist_sum) {
193 // Note our transform coeffs are 8 times an orthogonal transform.
194 // Hence quantizer step is also 8 times. To get effective quantizer
195 // we need to divide by 8 before sending to modeling function.
201 struct macroblock_plane *const p = &x->plane[0];
202 struct macroblockd_plane *const pd = &xd->plane[0];
203 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
205 (void) cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride,
206 pd->dst.buf, pd->dst.stride, &sse);
208 vp9_model_rd_from_var_lapndz(sse, 1 << num_pels_log2_lookup[bs],
209 pd->dequant[1] >> 3, &rate, &dist);
211 *out_rate_sum = rate;
212 *out_dist_sum = dist << 4;
215 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
216 // this needs various further optimizations. to be continued..
217 int64_t vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
218 const TileInfo *const tile,
219 int mi_row, int mi_col,
221 int64_t *returndistortion,
223 MACROBLOCKD *xd = &x->e_mbd;
224 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
225 struct macroblock_plane *const p = &x->plane[0];
226 struct macroblockd_plane *const pd = &xd->plane[0];
227 const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]);
228 MB_PREDICTION_MODE this_mode, best_mode = ZEROMV;
229 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
230 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
231 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
232 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
234 int64_t best_rd = INT64_MAX;
235 int64_t this_rd = INT64_MAX;
237 const int64_t inter_mode_thresh = 300;
238 const int64_t intra_mode_cost = 50;
241 int64_t dist = INT64_MAX;
243 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
246 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
249 // initialize mode decisions
250 *returnrate = INT_MAX;
251 *returndistortion = INT64_MAX;
252 vpx_memset(mbmi, 0, sizeof(MB_MODE_INFO));
253 mbmi->sb_type = bsize;
254 mbmi->ref_frame[0] = NONE;
255 mbmi->ref_frame[1] = NONE;
256 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
257 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
258 mbmi->interp_filter = cpi->common.interp_filter == SWITCHABLE ?
259 EIGHTTAP : cpi->common.interp_filter;
261 mbmi->segment_id = 0;
263 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) {
264 x->pred_mv_sad[ref_frame] = INT_MAX;
265 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
266 vp9_setup_buffer_inter(cpi, x, tile,
267 ref_frame, block_size, mi_row, mi_col,
268 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
270 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
271 frame_mv[ZEROMV][ref_frame].as_int = 0;
274 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) {
275 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
278 // Select prediction reference frames.
279 xd->plane[0].pre[0] = yv12_mb[ref_frame][0];
281 clamp_mv2(&frame_mv[NEARESTMV][ref_frame].as_mv, xd);
282 clamp_mv2(&frame_mv[NEARMV][ref_frame].as_mv, xd);
284 mbmi->ref_frame[0] = ref_frame;
286 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
289 if (cpi->sf.disable_inter_mode_mask[bsize] &
290 (1 << INTER_OFFSET(this_mode)))
293 if (this_mode == NEWMV) {
294 if (this_rd < (int64_t)(1 << num_pels_log2_lookup[bsize]))
297 x->mode_sad[ref_frame][INTER_OFFSET(NEWMV)] =
298 full_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col,
299 &frame_mv[NEWMV][ref_frame], &rate_mv);
301 if (frame_mv[NEWMV][ref_frame].as_int == INVALID_MV)
304 sub_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col,
305 &frame_mv[NEWMV][ref_frame].as_mv);
308 if (this_mode != NEARESTMV)
309 if (frame_mv[this_mode][ref_frame].as_int ==
310 frame_mv[NEARESTMV][ref_frame].as_int)
313 mbmi->mode = this_mode;
314 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
315 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
317 model_rd_for_sb_y(cpi, bsize, x, xd, &rate, &dist);
319 rate += x->inter_mode_cost[mbmi->mode_context[ref_frame]]
320 [INTER_OFFSET(this_mode)];
321 this_rd = RDCOST(x->rdmult, x->rddiv, rate, dist);
323 if (this_rd < best_rd) {
325 best_mode = this_mode;
326 best_ref_frame = ref_frame;
331 mbmi->mode = best_mode;
332 mbmi->ref_frame[0] = best_ref_frame;
333 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
334 xd->mi_8x8[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
336 // Perform intra prediction search, if the best SAD is above a certain
338 if (best_rd > inter_mode_thresh) {
339 for (this_mode = DC_PRED; this_mode <= DC_PRED; ++this_mode) {
340 vp9_predict_intra_block(xd, 0, b_width_log2(bsize),
341 mbmi->tx_size, this_mode,
342 &p->src.buf[0], p->src.stride,
343 &pd->dst.buf[0], pd->dst.stride, 0, 0, 0);
345 model_rd_for_sb_y(cpi, bsize, x, xd, &rate, &dist);
346 rate += x->mbmode_cost[this_mode];
347 this_rd = RDCOST(x->rdmult, x->rddiv, rate, dist);
349 if (this_rd + intra_mode_cost < best_rd) {
351 mbmi->mode = this_mode;
352 mbmi->ref_frame[0] = INTRA_FRAME;
353 mbmi->uv_mode = this_mode;
354 mbmi->mv[0].as_int = INVALID_MV;