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;
37 int further_steps, step_param;
38 int sadpb = x->sadperbit16;
40 int ref = mbmi->ref_frame[0];
41 int_mv ref_mv = mbmi->ref_mvs[ref][0];
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.as_mv);
67 // TODO(jingning) exploiting adaptive motion search control in non-RD
70 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
72 for (i = LAST_FRAME; i <= LAST_FRAME && cpi->common.show_frame; ++i) {
73 if ((x->pred_mv_sad[ref] >> 3) > x->pred_mv_sad[i]) {
74 tmp_mv->as_int = INVALID_MV;
76 if (scaled_ref_frame) {
78 for (i = 0; i < MAX_MB_PLANE; i++)
79 xd->plane[i].pre[0] = backup_yv12[i];
85 mvp_full = mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_mv;
90 if (cpi->sf.search_method == FAST_HEX) {
91 bestsme = vp9_fast_hex_search(x, &mvp_full, step_param, sadpb,
92 &cpi->fn_ptr[bsize], 1,
93 &ref_mv.as_mv, &tmp_mv->as_mv);
94 } else if (cpi->sf.search_method == HEX) {
95 bestsme = vp9_hex_search(x, &mvp_full, step_param, sadpb, 1,
96 &cpi->fn_ptr[bsize], 1,
97 &ref_mv.as_mv, &tmp_mv->as_mv);
98 } else if (cpi->sf.search_method == SQUARE) {
99 bestsme = vp9_square_search(x, &mvp_full, step_param, sadpb, 1,
100 &cpi->fn_ptr[bsize], 1,
101 &ref_mv.as_mv, &tmp_mv->as_mv);
102 } else if (cpi->sf.search_method == BIGDIA) {
103 bestsme = vp9_bigdia_search(x, &mvp_full, step_param, sadpb, 1,
104 &cpi->fn_ptr[bsize], 1,
105 &ref_mv.as_mv, &tmp_mv->as_mv);
107 bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param,
108 sadpb, further_steps, 1,
110 &ref_mv.as_mv, &tmp_mv->as_mv);
112 x->mv_col_min = tmp_col_min;
113 x->mv_col_max = tmp_col_max;
114 x->mv_row_min = tmp_row_min;
115 x->mv_row_max = tmp_row_max;
117 if (scaled_ref_frame) {
119 for (i = 0; i < MAX_MB_PLANE; i++)
120 xd->plane[i].pre[0] = backup_yv12[i];
123 // TODO(jingning) This step can be merged into full pixel search step in the
124 // re-designed log-diamond search
125 buf_offset = tmp_mv->as_mv.row * stride + tmp_mv->as_mv.col;
127 // Find sad for current vector.
128 bestsme = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, x->plane[0].src.stride,
129 xd->plane[0].pre[0].buf + buf_offset,
132 // scale to 1/8 pixel resolution
133 tmp_mv->as_mv.row = tmp_mv->as_mv.row * 8;
134 tmp_mv->as_mv.col = tmp_mv->as_mv.col * 8;
136 // calculate the bit cost on motion vector
137 *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv.as_mv,
138 x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
142 static void sub_pixel_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
143 const TileInfo *const tile,
144 BLOCK_SIZE bsize, int mi_row, int mi_col,
146 MACROBLOCKD *xd = &x->e_mbd;
147 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
148 struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}};
149 int ref = mbmi->ref_frame[0];
150 MV ref_mv = mbmi->ref_mvs[ref][0].as_mv;
153 const YV12_BUFFER_CONFIG *scaled_ref_frame = vp9_get_scaled_ref_frame(cpi,
155 if (scaled_ref_frame) {
157 // Swap out the reference frame for a version that's been scaled to
158 // match the resolution of the current frame, allowing the existing
159 // motion search code to be used without additional modifications.
160 for (i = 0; i < MAX_MB_PLANE; i++)
161 backup_yv12[i] = xd->plane[i].pre[0];
163 vp9_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL);
169 cpi->find_fractional_mv_step(x, tmp_mv, &ref_mv,
170 cpi->common.allow_high_precision_mv,
173 cpi->sf.subpel_force_stop,
174 cpi->sf.subpel_iters_per_step,
175 x->nmvjointcost, x->mvcost,
176 &dis, &x->pred_sse[ref]);
178 if (scaled_ref_frame) {
180 for (i = 0; i < MAX_MB_PLANE; i++)
181 xd->plane[i].pre[0] = backup_yv12[i];
185 // TODO(jingning) placeholder for inter-frame non-RD mode decision.
186 // this needs various further optimizations. to be continued..
187 int64_t vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x,
188 const TileInfo *const tile,
189 int mi_row, int mi_col,
191 int64_t *returndistortion,
193 MACROBLOCKD *xd = &x->e_mbd;
194 MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
195 struct macroblock_plane *const p = &x->plane[0];
196 struct macroblockd_plane *const pd = &xd->plane[0];
197 const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]);
198 MB_PREDICTION_MODE this_mode, best_mode = ZEROMV;
199 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME;
200 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES];
201 struct buf_2d yv12_mb[4][MAX_MB_PLANE];
202 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
204 int64_t best_rd = INT64_MAX;
205 int64_t this_rd = INT64_MAX;
206 static const int cost[4]= { 0, 2, 4, 6 };
208 const int64_t inter_mode_thresh = 300;
209 const int64_t intra_mode_cost = 50;
211 x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH;
214 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
217 // initialize mode decisions
218 *returnrate = INT_MAX;
219 *returndistortion = INT64_MAX;
220 vpx_memset(mbmi, 0, sizeof(MB_MODE_INFO));
221 mbmi->sb_type = bsize;
222 mbmi->ref_frame[0] = NONE;
223 mbmi->ref_frame[1] = NONE;
224 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
225 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]);
226 mbmi->interp_filter = cpi->common.interp_filter == SWITCHABLE ?
227 EIGHTTAP : cpi->common.interp_filter;
229 mbmi->segment_id = 0;
231 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) {
232 x->pred_mv_sad[ref_frame] = INT_MAX;
233 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
234 vp9_setup_buffer_inter(cpi, x, tile,
235 ref_frame, block_size, mi_row, mi_col,
236 frame_mv[NEARESTMV], frame_mv[NEARMV], yv12_mb);
238 frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
239 frame_mv[ZEROMV][ref_frame].as_int = 0;
242 for (ref_frame = LAST_FRAME; ref_frame <= LAST_FRAME ; ++ref_frame) {
244 if (!(cpi->ref_frame_flags & flag_list[ref_frame]))
247 // Select prediction reference frames.
248 xd->plane[0].pre[0] = yv12_mb[ref_frame][0];
250 clamp_mv2(&frame_mv[NEARESTMV][ref_frame].as_mv, xd);
251 clamp_mv2(&frame_mv[NEARMV][ref_frame].as_mv, xd);
253 mbmi->ref_frame[0] = ref_frame;
255 for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) {
256 int rate = cost[INTER_OFFSET(this_mode)]
257 << (num_pels_log2_lookup[bsize] - 4);
259 if (cpi->sf.disable_inter_mode_mask[bsize] &
260 (1 << INTER_OFFSET(this_mode)))
263 if (this_mode == NEWMV) {
264 if (this_rd < (int64_t)(1 << num_pels_log2_lookup[bsize]))
267 x->mode_sad[ref_frame][INTER_OFFSET(NEWMV)] =
268 full_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col,
269 &frame_mv[NEWMV][ref_frame], &rate_mv);
271 if (frame_mv[NEWMV][ref_frame].as_int == INVALID_MV)
274 sub_pixel_motion_search(cpi, x, tile, bsize, mi_row, mi_col,
275 &frame_mv[NEWMV][ref_frame].as_mv);
278 if (frame_mv[this_mode][ref_frame].as_int == 0) {
279 dist = x->mode_sad[ref_frame][INTER_OFFSET(ZEROMV)];
280 } else if (this_mode != NEARESTMV &&
281 frame_mv[NEARESTMV][ref_frame].as_int ==
282 frame_mv[this_mode][ref_frame].as_int) {
283 dist = x->mode_sad[ref_frame][INTER_OFFSET(NEARESTMV)];
285 mbmi->mode = this_mode;
286 mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
287 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
288 dist = x->mode_sad[ref_frame][INTER_OFFSET(this_mode)] =
289 cpi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
290 pd->dst.buf, pd->dst.stride, INT_MAX);
293 this_rd = rate + dist;
295 if (this_rd < best_rd) {
297 best_mode = this_mode;
298 best_ref_frame = ref_frame;
303 mbmi->mode = best_mode;
304 mbmi->ref_frame[0] = best_ref_frame;
305 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int;
306 xd->mi_8x8[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int;
308 // Perform intra prediction search, if the best SAD is above a certain
310 if (best_rd > inter_mode_thresh) {
311 for (this_mode = DC_PRED; this_mode <= DC_PRED; ++this_mode) {
312 vp9_predict_intra_block(xd, 0, b_width_log2(bsize),
313 mbmi->tx_size, this_mode,
314 &p->src.buf[0], p->src.stride,
315 &pd->dst.buf[0], pd->dst.stride, 0, 0, 0);
317 this_rd = cpi->fn_ptr[bsize].sdf(p->src.buf,
320 pd->dst.stride, INT_MAX);
322 if (this_rd + intra_mode_cost < best_rd) {
324 mbmi->mode = this_mode;
325 mbmi->ref_frame[0] = INTRA_FRAME;
326 mbmi->uv_mode = this_mode;
327 mbmi->mv[0].as_int = INVALID_MV;