blob: fd61fe6b2723b15a28ead70c7b4647859954601c [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <math.h>
13
14#include "av1/common/common.h"
15#include "av1/common/entropymode.h"
16
17#include "av1/encoder/cost.h"
18#include "av1/encoder/encodemv.h"
19#include "av1/encoder/subexp.h"
20
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022
Yaowu Xuf883b422016-08-30 14:01:10 -070023static struct av1_token mv_joint_encodings[MV_JOINTS];
24static struct av1_token mv_class_encodings[MV_CLASSES];
25static struct av1_token mv_fp_encodings[MV_FP_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -070026
Yaowu Xuf883b422016-08-30 14:01:10 -070027void av1_entropy_mv_init(void) {
28 av1_tokens_from_tree(mv_joint_encodings, av1_mv_joint_tree);
29 av1_tokens_from_tree(mv_class_encodings, av1_mv_class_tree);
Yaowu Xuf883b422016-08-30 14:01:10 -070030 av1_tokens_from_tree(mv_fp_encodings, av1_mv_fp_tree);
Yaowu Xuc27fc142016-08-22 16:08:15 -070031}
32
Thomas9ac55082016-09-23 18:04:17 +010033static void encode_mv_component(aom_writer *w, int comp, nmv_component *mvcomp,
Alex Converse6b2584c2017-05-02 09:51:21 -070034 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070035 int offset;
36 const int sign = comp < 0;
37 const int mag = sign ? -comp : comp;
Yaowu Xuf883b422016-08-30 14:01:10 -070038 const int mv_class = av1_get_mv_class(mag - 1, &offset);
Yaowu Xuc27fc142016-08-22 16:08:15 -070039 const int d = offset >> 3; // int mv data
40 const int fr = (offset >> 1) & 3; // fractional mv data
41 const int hp = offset & 1; // high precision mv data
42
43 assert(comp != 0);
44
Thomas Davies599395e2017-07-21 18:02:48 +010045// Sign
46#if CONFIG_NEW_MULTISYMBOL
47 aom_write_bit(w, sign);
48#else
Yaowu Xuf883b422016-08-30 14:01:10 -070049 aom_write(w, sign, mvcomp->sign);
Thomas Davies599395e2017-07-21 18:02:48 +010050#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070051
Nathan E. Egge476c63c2017-05-18 18:35:16 -040052 // Class
Nathan E. Egged7b893c2016-09-08 15:08:48 -040053 aom_write_symbol(w, mv_class, mvcomp->class_cdf, MV_CLASSES);
Yaowu Xuc27fc142016-08-22 16:08:15 -070054
55 // Integer bits
56 if (mv_class == MV_CLASS_0) {
Thomas Davies599395e2017-07-21 18:02:48 +010057#if CONFIG_NEW_MULTISYMBOL
58 aom_write_symbol(w, d, mvcomp->class0_cdf, CLASS0_SIZE);
59#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -040060 aom_write(w, d, mvcomp->class0[0]);
Thomas Davies599395e2017-07-21 18:02:48 +010061#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070062 } else {
63 int i;
64 const int n = mv_class + CLASS0_BITS - 1; // number of bits
Yaowu Xuf883b422016-08-30 14:01:10 -070065 for (i = 0; i < n; ++i) aom_write(w, (d >> i) & 1, mvcomp->bits[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070066 }
67
Alex Converse6b2584c2017-05-02 09:51:21 -070068// Fractional bits
69#if CONFIG_INTRABC
70 if (precision > MV_SUBPEL_NONE)
71#endif // CONFIG_INTRABC
72 {
73 aom_write_symbol(w, fr, mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d]
74 : mvcomp->fp_cdf,
75 MV_FP_SIZE);
76 }
Yaowu Xuc27fc142016-08-22 16:08:15 -070077
78 // High precision bit
Alex Converse6b2584c2017-05-02 09:51:21 -070079 if (precision > MV_SUBPEL_LOW_PRECISION)
Thomas Davies599395e2017-07-21 18:02:48 +010080#if CONFIG_NEW_MULTISYMBOL
81 aom_write_symbol(
82 w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf,
83 2);
84#else
Yaowu Xuf883b422016-08-30 14:01:10 -070085 aom_write(w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
Thomas Davies599395e2017-07-21 18:02:48 +010086#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070087}
88
89static void build_nmv_component_cost_table(int *mvcost,
90 const nmv_component *const mvcomp,
Alex Conversed5d9b6c2017-05-23 15:23:45 -070091 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070092 int i, v;
93 int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
94 int bits_cost[MV_OFFSET_BITS][2];
95 int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
96 int class0_hp_cost[2], hp_cost[2];
97
Yaowu Xuf883b422016-08-30 14:01:10 -070098 sign_cost[0] = av1_cost_zero(mvcomp->sign);
99 sign_cost[1] = av1_cost_one(mvcomp->sign);
100 av1_cost_tokens(class_cost, mvcomp->classes, av1_mv_class_tree);
101 av1_cost_tokens(class0_cost, mvcomp->class0, av1_mv_class0_tree);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102 for (i = 0; i < MV_OFFSET_BITS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700103 bits_cost[i][0] = av1_cost_zero(mvcomp->bits[i]);
104 bits_cost[i][1] = av1_cost_one(mvcomp->bits[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105 }
106
107 for (i = 0; i < CLASS0_SIZE; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700108 av1_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], av1_mv_fp_tree);
109 av1_cost_tokens(fp_cost, mvcomp->fp, av1_mv_fp_tree);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110
Alex Conversed5d9b6c2017-05-23 15:23:45 -0700111 if (precision > MV_SUBPEL_LOW_PRECISION) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700112 class0_hp_cost[0] = av1_cost_zero(mvcomp->class0_hp);
113 class0_hp_cost[1] = av1_cost_one(mvcomp->class0_hp);
114 hp_cost[0] = av1_cost_zero(mvcomp->hp);
115 hp_cost[1] = av1_cost_one(mvcomp->hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116 }
117 mvcost[0] = 0;
118 for (v = 1; v <= MV_MAX; ++v) {
119 int z, c, o, d, e, f, cost = 0;
120 z = v - 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700121 c = av1_get_mv_class(z, &o);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700122 cost += class_cost[c];
123 d = (o >> 3); /* int mv data */
124 f = (o >> 1) & 3; /* fractional pel mv data */
125 e = (o & 1); /* high precision mv data */
126 if (c == MV_CLASS_0) {
127 cost += class0_cost[d];
128 } else {
Urvang Joshi454280d2016-10-14 16:51:44 -0700129 const int b = c + CLASS0_BITS - 1; /* number of bits */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130 for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
131 }
Alex Conversed5d9b6c2017-05-23 15:23:45 -0700132#if CONFIG_INTRABC
133 if (precision > MV_SUBPEL_NONE)
134#endif // CONFIG_INTRABC
135 {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136 if (c == MV_CLASS_0) {
Alex Conversed5d9b6c2017-05-23 15:23:45 -0700137 cost += class0_fp_cost[d][f];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138 } else {
Alex Conversed5d9b6c2017-05-23 15:23:45 -0700139 cost += fp_cost[f];
140 }
141 if (precision > MV_SUBPEL_LOW_PRECISION) {
142 if (c == MV_CLASS_0) {
143 cost += class0_hp_cost[e];
144 } else {
145 cost += hp_cost[e];
146 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700147 }
148 }
149 mvcost[v] = cost + sign_cost[0];
150 mvcost[-v] = cost + sign_cost[1];
151 }
152}
153
Thomas Davies599395e2017-07-21 18:02:48 +0100154#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xuf883b422016-08-30 14:01:10 -0700155static void update_mv(aom_writer *w, const unsigned int ct[2], aom_prob *cur_p,
156 aom_prob upd_p) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157 (void)upd_p;
Yaowu Xu859a5272016-11-10 15:32:21 -0800158 // Just use the default maximum number of tile groups to avoid passing in the
159 // actual
Thomas Davies80188d12016-10-26 16:08:35 -0700160 // number
Thomas Daviesaf6df172016-11-09 14:04:18 +0000161 av1_cond_prob_diff_update(w, cur_p, ct, DEFAULT_MAX_NUM_TG);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162}
163
Jingning Hanfd0cf162016-09-30 10:33:50 -0700164void av1_write_nmv_probs(AV1_COMMON *cm, int usehp, aom_writer *w,
Yaowu Xuf883b422016-08-30 14:01:10 -0700165 nmv_context_counts *const nmv_counts) {
Yaowu Xu8af861b2016-11-01 12:12:11 -0700166 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167 int nmv_ctx = 0;
168 for (nmv_ctx = 0; nmv_ctx < NMV_CONTEXTS; ++nmv_ctx) {
169 nmv_context *const mvc = &cm->fc->nmvc[nmv_ctx];
170 nmv_context_counts *const counts = &nmv_counts[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171
172 if (usehp) {
173 for (i = 0; i < 2; ++i) {
174 update_mv(w, counts->comps[i].class0_hp, &mvc->comps[i].class0_hp,
175 MV_UPDATE_PROB);
176 update_mv(w, counts->comps[i].hp, &mvc->comps[i].hp, MV_UPDATE_PROB);
177 }
178 }
179 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700180}
Thomas Davies599395e2017-07-21 18:02:48 +0100181#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182
Yaowu Xuf883b422016-08-30 14:01:10 -0700183void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +0100184 nmv_context *mvctx, int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 const MV diff = { mv->row - ref->row, mv->col - ref->col };
Yaowu Xuf883b422016-08-30 14:01:10 -0700186 const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -0400187 aom_write_symbol(w, j, mvctx->joint_cdf, MV_JOINTS);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 if (mv_joint_vertical(j))
189 encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
190
191 if (mv_joint_horizontal(j))
192 encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
193
194 // If auto_mv_step_size is enabled then keep track of the largest
195 // motion vector component used.
196 if (cpi->sf.mv.auto_mv_step_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700197 unsigned int maxv = AOMMAX(abs(mv->row), abs(mv->col)) >> 3;
198 cpi->max_mv_magnitude = AOMMAX(maxv, cpi->max_mv_magnitude);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 }
200}
201
Alex Converse28744302017-04-13 14:46:22 -0700202#if CONFIG_INTRABC
203void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
204 nmv_context *mvctx) {
205 const MV diff = { mv->row - ref->row, mv->col - ref->col };
206 const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
207
Alex Converse28744302017-04-13 14:46:22 -0700208 aom_write_symbol(w, j, mvctx->joint_cdf, MV_JOINTS);
Alex Converse28744302017-04-13 14:46:22 -0700209 if (mv_joint_vertical(j))
Alex Converse6b2584c2017-05-02 09:51:21 -0700210 encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -0700211
212 if (mv_joint_horizontal(j))
Alex Converse6b2584c2017-05-02 09:51:21 -0700213 encode_mv_component(w, diff.col, &mvctx->comps[1], MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -0700214}
215#endif // CONFIG_INTRABC
216
Yaowu Xuf883b422016-08-30 14:01:10 -0700217void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
Alex Conversed5d9b6c2017-05-23 15:23:45 -0700218 const nmv_context *ctx,
219 MvSubpelPrecision precision) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700220 av1_cost_tokens(mvjoint, ctx->joints, av1_mv_joint_tree);
Alex Conversed5d9b6c2017-05-23 15:23:45 -0700221 build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], precision);
222 build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223}
224
225#if CONFIG_EXT_INTER
226static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200227 const int_mv mvs[2], const int_mv pred_mvs[2],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700228 nmv_context_counts *nmv_counts) {
229 int i;
230 PREDICTION_MODE mode = mbmi->mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700232 if (mode == NEWMV || mode == NEW_NEWMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233 for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700234 const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235 const MV diff = { mvs[i].as_mv.row - ref->row,
236 mvs[i].as_mv.col - ref->col };
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700237 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
238 int nmv_ctx =
239 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
240 mbmi_ext->ref_mv_stack[rf_type], i, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
242 (void)pred_mvs;
Alex Converse6317c882016-09-29 14:21:37 -0700243 av1_inc_mv(&diff, counts, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700244 }
245 } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
246 const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv;
247 const MV diff = { mvs[1].as_mv.row - ref->row,
248 mvs[1].as_mv.col - ref->col };
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700249 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
250 int nmv_ctx =
251 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
252 mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse6317c882016-09-29 14:21:37 -0700254 av1_inc_mv(&diff, counts, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700255 } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
256 const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv;
257 const MV diff = { mvs[0].as_mv.row - ref->row,
258 mvs[0].as_mv.col - ref->col };
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700259 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
260 int nmv_ctx =
261 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
262 mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse6317c882016-09-29 14:21:37 -0700264 av1_inc_mv(&diff, counts, 1);
Zoe Liu85b66462017-04-20 14:28:19 -0700265#if CONFIG_COMPOUND_SINGLEREF
266 } else {
267 assert( // mode == SR_NEAREST_NEWMV ||
268 mode == SR_NEAR_NEWMV || mode == SR_ZERO_NEWMV || mode == SR_NEW_NEWMV);
269 const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv;
270 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
271 int nmv_ctx =
272 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
273 mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
274 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
275 (void)pred_mvs;
276 MV diff;
277 if (mode == SR_NEW_NEWMV) {
278 diff.row = mvs[0].as_mv.row - ref->row;
279 diff.col = mvs[0].as_mv.col - ref->col;
280 av1_inc_mv(&diff, counts, 1);
281 }
282 diff.row = mvs[1].as_mv.row - ref->row;
283 diff.col = mvs[1].as_mv.col - ref->col;
284 av1_inc_mv(&diff, counts, 1);
285#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 }
287}
288
289static void inc_mvs_sub8x8(const MODE_INFO *mi, int block, const int_mv mvs[2],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 const MB_MODE_INFO_EXT *mbmi_ext,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291 nmv_context_counts *nmv_counts) {
292 int i;
293 PREDICTION_MODE mode = mi->bmi[block].as_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 const MB_MODE_INFO *mbmi = &mi->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700296 if (mode == NEWMV || mode == NEW_NEWMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 for (i = 0; i < 1 + has_second_ref(&mi->mbmi); ++i) {
298 const MV *ref = &mi->bmi[block].ref_mv[i].as_mv;
299 const MV diff = { mvs[i].as_mv.row - ref->row,
300 mvs[i].as_mv.col - ref->col };
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700301 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
302 int nmv_ctx =
303 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
304 mbmi_ext->ref_mv_stack[rf_type], i, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse6317c882016-09-29 14:21:37 -0700306 av1_inc_mv(&diff, counts, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 }
308 } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
309 const MV *ref = &mi->bmi[block].ref_mv[1].as_mv;
310 const MV diff = { mvs[1].as_mv.row - ref->row,
311 mvs[1].as_mv.col - ref->col };
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700312 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
313 int nmv_ctx =
314 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
315 mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse6317c882016-09-29 14:21:37 -0700317 av1_inc_mv(&diff, counts, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
319 const MV *ref = &mi->bmi[block].ref_mv[0].as_mv;
320 const MV diff = { mvs[0].as_mv.row - ref->row,
321 mvs[0].as_mv.col - ref->col };
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700322 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
323 int nmv_ctx =
324 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
325 mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse6317c882016-09-29 14:21:37 -0700327 av1_inc_mv(&diff, counts, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328 }
329}
Zoe Liu85b66462017-04-20 14:28:19 -0700330#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700331static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200332 const int_mv mvs[2], const int_mv pred_mvs[2],
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 nmv_context_counts *nmv_counts) {
334 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335
336 for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -0700337 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
338 int nmv_ctx =
339 av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
340 mbmi_ext->ref_mv_stack[rf_type], i, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 nmv_context_counts *counts = &nmv_counts[nmv_ctx];
342 const MV *ref = &pred_mvs[i].as_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 const MV diff = { mvs[i].as_mv.row - ref->row,
344 mvs[i].as_mv.col - ref->col };
Alex Converse6317c882016-09-29 14:21:37 -0700345 av1_inc_mv(&diff, counts, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700346 }
347}
348#endif // CONFIG_EXT_INTER
349
Yaowu Xuf883b422016-08-30 14:01:10 -0700350void av1_update_mv_count(ThreadData *td) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351 const MACROBLOCKD *xd = &td->mb.e_mbd;
352 const MODE_INFO *mi = xd->mi[0];
353 const MB_MODE_INFO *const mbmi = &mi->mbmi;
354 const MB_MODE_INFO_EXT *mbmi_ext = td->mb.mbmi_ext;
Jingning Han599461d2016-12-14 11:34:58 -0800355#if CONFIG_CB4X4
356 const int unify_bsize = 1;
357#else
358 const int unify_bsize = 0;
359#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700360
Jingning Han599461d2016-12-14 11:34:58 -0800361 if (mbmi->sb_type < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type];
363 const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type];
364 int idx, idy;
365
366 for (idy = 0; idy < 2; idy += num_4x4_h) {
367 for (idx = 0; idx < 2; idx += num_4x4_w) {
368 const int i = idy * 2 + idx;
369
370#if CONFIG_EXT_INTER
371 if (have_newmv_in_inter_mode(mi->bmi[i].as_mode))
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200372 inc_mvs_sub8x8(mi, i, mi->bmi[i].as_mv, mbmi_ext, td->counts->mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373#else
374 if (mi->bmi[i].as_mode == NEWMV)
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200375 inc_mvs(mbmi, mbmi_ext, mi->bmi[i].as_mv, mi->bmi[i].pred_mv,
376 td->counts->mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377#endif // CONFIG_EXT_INTER
378 }
379 }
380 } else {
381#if CONFIG_EXT_INTER
382 if (have_newmv_in_inter_mode(mbmi->mode))
383#else
384 if (mbmi->mode == NEWMV)
385#endif // CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200386 inc_mvs(mbmi, mbmi_ext, mbmi->mv, mbmi->pred_mv, td->counts->mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387 }
388}