more ref_mv changes from aom/master
Change-Id: I9152f898dfacdf3877ed719f193bb1e0dbee0a1a
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 7636151..7ef24bb 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1256,9 +1256,10 @@
#endif // CONFIG_EXT_INTER
for (ref = 0; ref < 1 + is_compound; ++ref) {
#if CONFIG_REF_MV
- int nmv_ctx =
- av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[ref]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[ref]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], ref,
+ mbmi->ref_mv_idx);
const nmv_context *nmvc = &cm->fc->nmvc[nmv_ctx];
#endif
av1_encode_mv(cpi, w, &mi->bmi[j].as_mv[ref].as_mv,
@@ -1280,9 +1281,10 @@
#if CONFIG_EXT_INTER
else if (b_mode == NEAREST_NEWMV || b_mode == NEAR_NEWMV) {
#if CONFIG_REF_MV
- int nmv_ctx =
- av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[1]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[1]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 1,
+ mbmi->ref_mv_idx);
const nmv_context *nmvc = &cm->fc->nmvc[nmv_ctx];
#endif
av1_encode_mv(cpi, w, &mi->bmi[j].as_mv[1].as_mv,
@@ -1293,9 +1295,10 @@
nmvc, allow_hp);
} else if (b_mode == NEW_NEARESTMV || b_mode == NEW_NEARMV) {
#if CONFIG_REF_MV
- int nmv_ctx =
- av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[0]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[0]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 0,
+ mbmi->ref_mv_idx);
const nmv_context *nmvc = &cm->fc->nmvc[nmv_ctx];
#endif
av1_encode_mv(cpi, w, &mi->bmi[j].as_mv[0].as_mv,
@@ -1317,9 +1320,10 @@
int_mv ref_mv;
for (ref = 0; ref < 1 + is_compound; ++ref) {
#if CONFIG_REF_MV
- int nmv_ctx =
- av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[ref]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[ref]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], ref,
+ mbmi->ref_mv_idx);
const nmv_context *nmvc = &cm->fc->nmvc[nmv_ctx];
#endif
ref_mv = mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0];
@@ -1342,8 +1346,10 @@
#if CONFIG_EXT_INTER
} else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[1]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[1]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
const nmv_context *nmvc = &cm->fc->nmvc[nmv_ctx];
#endif
av1_encode_mv(cpi, w, &mbmi->mv[1].as_mv,
@@ -1354,8 +1360,10 @@
nmvc, allow_hp);
} else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[0]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[0]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
const nmv_context *nmvc = &cm->fc->nmvc[nmv_ctx];
#endif
av1_encode_mv(cpi, w, &mbmi->mv[0].as_mv,
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index cfc4718..ab896f4 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1075,6 +1075,7 @@
clamp_mv_ref(&this_mv.as_mv, xd->n8_w << 3, xd->n8_h << 3, xd);
x->mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0] = this_mv;
mbmi->pred_mv[i] = this_mv;
+ mi->mbmi.pred_mv[i] = this_mv;
}
}
#endif
diff --git a/av1/encoder/encodemv.c b/av1/encoder/encodemv.c
index ee627bd..00a95ee 100644
--- a/av1/encoder/encodemv.c
+++ b/av1/encoder/encodemv.c
@@ -298,8 +298,10 @@
const MV diff = { mvs[i].as_mv.row - ref->row,
mvs[i].as_mv.col - ref->col };
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[i]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[i]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], i, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
(void)pred_mvs;
#endif
@@ -310,8 +312,10 @@
const MV diff = { mvs[1].as_mv.row - ref->row,
mvs[1].as_mv.col - ref->col };
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[1]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[1]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
@@ -320,8 +324,10 @@
const MV diff = { mvs[0].as_mv.row - ref->row,
mvs[0].as_mv.col - ref->col };
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[0]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[0]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
@@ -347,8 +353,10 @@
const MV diff = { mvs[i].as_mv.row - ref->row,
mvs[i].as_mv.col - ref->col };
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[i]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[i]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], i, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
@@ -358,8 +366,10 @@
const MV diff = { mvs[1].as_mv.row - ref->row,
mvs[1].as_mv.col - ref->col };
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[1]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[1]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
@@ -368,8 +378,10 @@
const MV diff = { mvs[0].as_mv.row - ref->row,
mvs[0].as_mv.col - ref->col };
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[0]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[0]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
@@ -389,8 +401,10 @@
for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
#if CONFIG_REF_MV
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[mbmi->ref_frame[i]],
- mbmi_ext->ref_mv_stack[mbmi->ref_frame[i]]);
+ int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
+ int nmv_ctx =
+ av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], i, mbmi->ref_mv_idx);
nmv_context_counts *counts = &nmv_counts[nmv_ctx];
const MV *ref = &pred_mvs[i].as_mv;
#else
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 0c80174..4d157ff 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -335,10 +335,13 @@
}
#if CONFIG_REF_MV
-void av1_set_mvcost(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame) {
+void av1_set_mvcost(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, int ref,
+ int ref_mv_idx) {
MB_MODE_INFO_EXT *mbmi_ext = x->mbmi_ext;
- int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[ref_frame],
- mbmi_ext->ref_mv_stack[ref_frame]);
+ int8_t rf_type = av1_ref_frame_type(x->e_mbd.mi[0]->mbmi.ref_frame);
+ int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
+ mbmi_ext->ref_mv_stack[rf_type], ref, ref_mv_idx);
+ (void)ref_frame;
x->mvcost = x->mv_cost_stack[nmv_ctx];
x->nmvjointcost = x->nmv_vec_cost[nmv_ctx];
x->mvsadcost = x->mvcost;
diff --git a/av1/encoder/rd.h b/av1/encoder/rd.h
index 54c10b2..933733b 100644
--- a/av1/encoder/rd.h
+++ b/av1/encoder/rd.h
@@ -413,7 +413,8 @@
void av1_init_me_luts(void);
#if CONFIG_REF_MV
-void av1_set_mvcost(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame);
+void av1_set_mvcost(MACROBLOCK *x, MV_REFERENCE_FRAME ref_frame, int ref,
+ int ref_mv_idx);
#endif
void av1_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 9721abe..3d00687 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -1664,7 +1664,7 @@
for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) {
#if CONFIG_REF_MV
- if (tx_type != DCT_DCT && is_inter && mbmi->ref_mv_idx > 0) continue;
+ if (mbmi->ref_mv_idx > 0 && tx_type != DCT_DCT) continue;
#endif
rd = choose_tx_size_fix_type(cpi, bs, x, &r, &d, &s, &sse, ref_best_rd,
tx_type, prune);
@@ -4159,7 +4159,7 @@
#if CONFIG_REF_MV
for (idx = 0; idx < 1 + is_compound; ++idx) {
this_mv[idx] = seg_mvs[mbmi->ref_frame[idx]];
- av1_set_mvcost(x, mbmi->ref_frame[idx]);
+ av1_set_mvcost(x, mbmi->ref_frame[idx], idx, mbmi->ref_mv_idx);
thismvcost +=
av1_mv_bit_cost(&this_mv[idx].as_mv, &best_ref_mv[idx]->as_mv,
x->nmvjointcost, x->mvcost, MV_COST_WEIGHT_SUB);
@@ -4264,8 +4264,11 @@
#if CONFIG_REF_MV
if (mode == NEWMV) {
- mic->bmi[i].pred_mv[0].as_int = best_ref_mv[0]->as_int;
- if (is_compound) mic->bmi[i].pred_mv[1].as_int = best_ref_mv[1]->as_int;
+ mic->bmi[i].pred_mv[0].as_int =
+ mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_int;
+ if (is_compound)
+ mic->bmi[i].pred_mv[1].as_int =
+ mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_int;
} else {
mic->bmi[i].pred_mv[0].as_int = this_mv[0].as_int;
if (is_compound) mic->bmi[i].pred_mv[1].as_int = this_mv[1].as_int;
@@ -4703,7 +4706,7 @@
best_mv->row >>= 3;
#if CONFIG_REF_MV
- av1_set_mvcost(x, refs[id]);
+ av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx);
#endif
// Small-range full-pixel motion search.
@@ -4782,7 +4785,7 @@
xd->plane[i].pre[ref] = backup_yv12[ref][i];
}
#if CONFIG_REF_MV
- av1_set_mvcost(x, refs[ref]);
+ av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx);
#endif
#if CONFIG_EXT_INTER
if (bsize >= BLOCK_8X8)
@@ -5141,7 +5144,7 @@
x->best_mv.as_int = x->second_best_mv.as_int = INVALID_MV;
#if CONFIG_REF_MV
- av1_set_mvcost(x, mbmi->ref_frame[0]);
+ av1_set_mvcost(x, mbmi->ref_frame[0], 0, mbmi->ref_mv_idx);
#endif
bestsme = av1_full_pixel_search(
cpi, x, bsize, &mvp_full, step_param, sadpb,
@@ -5820,10 +5823,6 @@
pred_mv[1] = x->mbmi_ext->ref_mvs[ref][1].as_mv;
pred_mv[2] = x->pred_mv[ref];
-#if CONFIG_REF_MV
- av1_set_mvcost(x, ref);
-#endif
-
if (scaled_ref_frame) {
int i;
// Swap out the reference frame for a version that's been scaled to
@@ -5835,6 +5834,12 @@
av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL);
}
+ av1_set_mv_search_range(x, &ref_mv);
+
+#if CONFIG_REF_MV
+ av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
+#endif
+
// Work out the size of the first step in the mv step search.
// 0 here is maximum length first step. 1 is AOMMAX >> 1 etc.
if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) {
@@ -6017,7 +6022,7 @@
av1_get_scaled_ref_frame(cpi, ref);
#if CONFIG_REF_MV
- av1_set_mvcost(x, ref);
+ av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
#endif
if (scaled_ref_frame) {
@@ -6145,7 +6150,7 @@
pred_mv[2] = x->pred_mv[ref];
#if CONFIG_REF_MV
- av1_set_mvcost(x, ref);
+ av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
#endif
if (scaled_ref_frame) {
@@ -6791,13 +6796,13 @@
single_newmv, &rate_mv, 0);
} else {
#if CONFIG_REF_MV
- av1_set_mvcost(x, mbmi->ref_frame[0]);
+ av1_set_mvcost(x, mbmi->ref_frame[0], 0, mbmi->ref_mv_idx);
#endif // CONFIG_REF_MV
rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv,
&x->mbmi_ext->ref_mvs[refs[0]][0].as_mv,
x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
#if CONFIG_REF_MV
- av1_set_mvcost(x, mbmi->ref_frame[1]);
+ av1_set_mvcost(x, mbmi->ref_frame[1], 1, mbmi->ref_mv_idx);
#endif // CONFIG_REF_MV
rate_mv += av1_mv_bit_cost(
&frame_mv[refs[1]].as_mv, &x->mbmi_ext->ref_mvs[refs[1]][0].as_mv,
@@ -6824,13 +6829,13 @@
single_newmv, &rate_mv, 0);
} else {
#if CONFIG_REF_MV
- av1_set_mvcost(x, mbmi->ref_frame[0]);
+ av1_set_mvcost(x, mbmi->ref_frame[0], 0, mbmi->ref_mv_idx);
#endif // CONFIG_REF_MV
rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv,
&x->mbmi_ext->ref_mvs[refs[0]][0].as_mv,
x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
#if CONFIG_REF_MV
- av1_set_mvcost(x, mbmi->ref_frame[1]);
+ av1_set_mvcost(x, mbmi->ref_frame[1], 1, mbmi->ref_mv_idx);
#endif // CONFIG_REF_MV
rate_mv += av1_mv_bit_cost(&frame_mv[refs[1]].as_mv,
&x->mbmi_ext->ref_mvs[refs[1]][0].as_mv,
@@ -8228,6 +8233,10 @@
int64_t best_pred_diff[REFERENCE_MODES];
int64_t best_pred_rd[REFERENCE_MODES];
MB_MODE_INFO best_mbmode;
+#if CONFIG_REF_MV
+ int rate_skip0 = av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
+ int rate_skip1 = av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
+#endif
int best_mode_skippable = 0;
int midx, best_mode_index = -1;
unsigned int ref_costs_single[TOTAL_REFS_PER_FRAME];
@@ -8560,6 +8569,9 @@
this_mode = av1_mode_order[mode_index].mode;
ref_frame = av1_mode_order[mode_index].ref_frame[0];
second_ref_frame = av1_mode_order[mode_index].ref_frame[1];
+#if CONFIG_REF_MV
+ mbmi->ref_mv_idx = 0;
+#endif
#if CONFIG_EXT_INTER
if (ref_frame > INTRA_FRAME && second_ref_frame == INTRA_FRAME) {
@@ -9145,8 +9157,14 @@
// Cost the skip mb case
rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 1);
} else if (ref_frame != INTRA_FRAME && !xd->lossless[mbmi->segment_id]) {
+#if CONFIG_REF_MV
+ if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv + rate_skip0,
+ distortion2) <
+ RDCOST(x->rdmult, x->rddiv, rate_skip1, total_sse)) {
+#else
if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) <
RDCOST(x->rdmult, x->rddiv, 0, total_sse)) {
+#endif
// Add in the cost of the no skip flag.
rate2 += av1_cost_bit(av1_get_skip_prob(cm, xd), 0);
} else {
@@ -10038,6 +10056,10 @@
ref_frame = av1_ref_order[ref_index].ref_frame[0];
second_ref_frame = av1_ref_order[ref_index].ref_frame[1];
+#if CONFIG_REF_MV
+ mbmi->ref_mv_idx = 0;
+#endif
+
// Look at the reference frame of the best mode so far and set the
// skip mask to look at a subset of the remaining modes.
if (ref_index > 2 && sf->mode_skip_start < MAX_MODES) {
@@ -10679,12 +10701,12 @@
for (i = 0; i < 4; ++i)
memcpy(&xd->mi[0]->bmi[i], &best_bmodes[i], sizeof(b_mode_info));
- mbmi->mv[0].as_int = xd->mi[0]->bmi[3].as_mv[0].as_int;
- mbmi->mv[1].as_int = xd->mi[0]->bmi[3].as_mv[1].as_int;
#if CONFIG_REF_MV
mbmi->pred_mv[0].as_int = xd->mi[0]->bmi[3].pred_mv[0].as_int;
mbmi->pred_mv[1].as_int = xd->mi[0]->bmi[3].pred_mv[1].as_int;
#endif
+ mbmi->mv[0].as_int = xd->mi[0]->bmi[3].as_mv[0].as_int;
+ mbmi->mv[1].as_int = xd->mi[0]->bmi[3].as_mv[1].as_int;
}
for (i = 0; i < REFERENCE_MODES; ++i) {