encodemv.c 15.2 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
12
13
 */

#include <math.h>

14
15
#include "av1/common/common.h"
#include "av1/common/entropymode.h"
Jingning Han's avatar
Jingning Han committed
16

17
18
19
#include "av1/encoder/cost.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/subexp.h"
Jingning Han's avatar
Jingning Han committed
20

Yaowu Xu's avatar
Yaowu Xu committed
21
22
23
24
25
26
27
28
29
30
#include "aom_dsp/aom_dsp_common.h"

static struct av1_token mv_joint_encodings[MV_JOINTS];
static struct av1_token mv_class_encodings[MV_CLASSES];
static struct av1_token mv_fp_encodings[MV_FP_SIZE];

void av1_entropy_mv_init(void) {
  av1_tokens_from_tree(mv_joint_encodings, av1_mv_joint_tree);
  av1_tokens_from_tree(mv_class_encodings, av1_mv_class_tree);
  av1_tokens_from_tree(mv_fp_encodings, av1_mv_fp_tree);
Jingning Han's avatar
Jingning Han committed
31
32
}

33
static void encode_mv_component(aom_writer *w, int comp, nmv_component *mvcomp,
Alex Converse's avatar
Alex Converse committed
34
                                MvSubpelPrecision precision) {
Jingning Han's avatar
Jingning Han committed
35
36
37
  int offset;
  const int sign = comp < 0;
  const int mag = sign ? -comp : comp;
Yaowu Xu's avatar
Yaowu Xu committed
38
  const int mv_class = av1_get_mv_class(mag - 1, &offset);
39
40
41
  const int d = offset >> 3;         // int mv data
  const int fr = (offset >> 1) & 3;  // fractional mv data
  const int hp = offset & 1;         // high precision mv data
Jingning Han's avatar
Jingning Han committed
42
43
44
45

  assert(comp != 0);

  // Sign
Yaowu Xu's avatar
Yaowu Xu committed
46
  aom_write(w, sign, mvcomp->sign);
Jingning Han's avatar
Jingning Han committed
47

48
  // Class
49
  aom_write_symbol(w, mv_class, mvcomp->class_cdf, MV_CLASSES);
Jingning Han's avatar
Jingning Han committed
50
51
52

  // Integer bits
  if (mv_class == MV_CLASS_0) {
53
    aom_write(w, d, mvcomp->class0[0]);
Jingning Han's avatar
Jingning Han committed
54
55
56
  } else {
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
Yaowu Xu's avatar
Yaowu Xu committed
57
    for (i = 0; i < n; ++i) aom_write(w, (d >> i) & 1, mvcomp->bits[i]);
Jingning Han's avatar
Jingning Han committed
58
59
  }

Alex Converse's avatar
Alex Converse committed
60
61
62
63
64
65
66
67
68
// Fractional bits
#if CONFIG_INTRABC
  if (precision > MV_SUBPEL_NONE)
#endif  // CONFIG_INTRABC
  {
    aom_write_symbol(w, fr, mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d]
                                                   : mvcomp->fp_cdf,
                     MV_FP_SIZE);
  }
Jingning Han's avatar
Jingning Han committed
69
70

  // High precision bit
Alex Converse's avatar
Alex Converse committed
71
  if (precision > MV_SUBPEL_LOW_PRECISION)
Yaowu Xu's avatar
Yaowu Xu committed
72
    aom_write(w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
Jingning Han's avatar
Jingning Han committed
73
74
75
}

static void build_nmv_component_cost_table(int *mvcost,
76
                                           const nmv_component *const mvcomp,
Alex Converse's avatar
Alex Converse committed
77
                                           MvSubpelPrecision precision) {
Jingning Han's avatar
Jingning Han committed
78
79
80
81
82
83
  int i, v;
  int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
  int bits_cost[MV_OFFSET_BITS][2];
  int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
  int class0_hp_cost[2], hp_cost[2];

Yaowu Xu's avatar
Yaowu Xu committed
84
85
86
87
  sign_cost[0] = av1_cost_zero(mvcomp->sign);
  sign_cost[1] = av1_cost_one(mvcomp->sign);
  av1_cost_tokens(class_cost, mvcomp->classes, av1_mv_class_tree);
  av1_cost_tokens(class0_cost, mvcomp->class0, av1_mv_class0_tree);
Jingning Han's avatar
Jingning Han committed
88
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
89
90
    bits_cost[i][0] = av1_cost_zero(mvcomp->bits[i]);
    bits_cost[i][1] = av1_cost_one(mvcomp->bits[i]);
Jingning Han's avatar
Jingning Han committed
91
92
93
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
94
95
    av1_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], av1_mv_fp_tree);
  av1_cost_tokens(fp_cost, mvcomp->fp, av1_mv_fp_tree);
Jingning Han's avatar
Jingning Han committed
96

Alex Converse's avatar
Alex Converse committed
97
  if (precision > MV_SUBPEL_LOW_PRECISION) {
Yaowu Xu's avatar
Yaowu Xu committed
98
99
100
101
    class0_hp_cost[0] = av1_cost_zero(mvcomp->class0_hp);
    class0_hp_cost[1] = av1_cost_one(mvcomp->class0_hp);
    hp_cost[0] = av1_cost_zero(mvcomp->hp);
    hp_cost[1] = av1_cost_one(mvcomp->hp);
Jingning Han's avatar
Jingning Han committed
102
103
104
105
106
  }
  mvcost[0] = 0;
  for (v = 1; v <= MV_MAX; ++v) {
    int z, c, o, d, e, f, cost = 0;
    z = v - 1;
Yaowu Xu's avatar
Yaowu Xu committed
107
    c = av1_get_mv_class(z, &o);
Jingning Han's avatar
Jingning Han committed
108
    cost += class_cost[c];
109
110
111
    d = (o >> 3);     /* int mv data */
    f = (o >> 1) & 3; /* fractional pel mv data */
    e = (o & 1);      /* high precision mv data */
Jingning Han's avatar
Jingning Han committed
112
113
114
    if (c == MV_CLASS_0) {
      cost += class0_cost[d];
    } else {
115
      const int b = c + CLASS0_BITS - 1; /* number of bits */
116
      for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
Jingning Han's avatar
Jingning Han committed
117
    }
Alex Converse's avatar
Alex Converse committed
118
119
120
121
#if CONFIG_INTRABC
    if (precision > MV_SUBPEL_NONE)
#endif  // CONFIG_INTRABC
    {
Jingning Han's avatar
Jingning Han committed
122
      if (c == MV_CLASS_0) {
Alex Converse's avatar
Alex Converse committed
123
        cost += class0_fp_cost[d][f];
Jingning Han's avatar
Jingning Han committed
124
      } else {
Alex Converse's avatar
Alex Converse committed
125
126
127
128
129
130
131
132
        cost += fp_cost[f];
      }
      if (precision > MV_SUBPEL_LOW_PRECISION) {
        if (c == MV_CLASS_0) {
          cost += class0_hp_cost[e];
        } else {
          cost += hp_cost[e];
        }
Jingning Han's avatar
Jingning Han committed
133
134
135
136
137
138
139
      }
    }
    mvcost[v] = cost + sign_cost[0];
    mvcost[-v] = cost + sign_cost[1];
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
140
141
static void update_mv(aom_writer *w, const unsigned int ct[2], aom_prob *cur_p,
                      aom_prob upd_p) {
142
  (void)upd_p;
143
#if CONFIG_TILE_GROUPS
Yaowu Xu's avatar
Yaowu Xu committed
144
145
  // Just use the default maximum number of tile groups to avoid passing in the
  // actual
146
  // number
147
  av1_cond_prob_diff_update(w, cur_p, ct, DEFAULT_MAX_NUM_TG);
148
149
150
#else
  av1_cond_prob_diff_update(w, cur_p, ct, 1);
#endif
Jingning Han's avatar
Jingning Han committed
151
152
}

153
#if !CONFIG_EC_ADAPT
Yaowu Xu's avatar
Yaowu Xu committed
154
155
static void write_mv_update(const aom_tree_index *tree,
                            aom_prob probs[/*n - 1*/],
156
                            const unsigned int counts[/*n - 1*/], int n,
Yaowu Xu's avatar
Yaowu Xu committed
157
                            aom_writer *w) {
Jingning Han's avatar
Jingning Han committed
158
159
160
161
162
163
  int i;
  unsigned int branch_ct[32][2];

  // Assuming max number of probabilities <= 32
  assert(n <= 32);

Yaowu Xu's avatar
Yaowu Xu committed
164
  av1_tree_probs_from_distribution(tree, branch_ct, counts);
Jingning Han's avatar
Jingning Han committed
165
166
167
  for (i = 0; i < n - 1; ++i)
    update_mv(w, branch_ct[i], &probs[i], MV_UPDATE_PROB);
}
168
#endif
Jingning Han's avatar
Jingning Han committed
169

170
void av1_write_nmv_probs(AV1_COMMON *cm, int usehp, aom_writer *w,
Yaowu Xu's avatar
Yaowu Xu committed
171
                         nmv_context_counts *const nmv_counts) {
172
  int i;
173
174
175
176
  int nmv_ctx = 0;
  for (nmv_ctx = 0; nmv_ctx < NMV_CONTEXTS; ++nmv_ctx) {
    nmv_context *const mvc = &cm->fc->nmvc[nmv_ctx];
    nmv_context_counts *const counts = &nmv_counts[nmv_ctx];
177
#if !CONFIG_EC_ADAPT
Yaowu Xu's avatar
Yaowu Xu committed
178
    write_mv_update(av1_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS,
179
                    w);
180
181

    for (i = 0; i < 2; ++i) {
182
      int j;
183
184
185
186
      nmv_component *comp = &mvc->comps[i];
      nmv_component_counts *comp_counts = &counts->comps[i];

      update_mv(w, comp_counts->sign, &comp->sign, MV_UPDATE_PROB);
Yaowu Xu's avatar
Yaowu Xu committed
187
      write_mv_update(av1_mv_class_tree, comp->classes, comp_counts->classes,
188
                      MV_CLASSES, w);
Yaowu Xu's avatar
Yaowu Xu committed
189
      write_mv_update(av1_mv_class0_tree, comp->class0, comp_counts->class0,
190
191
192
193
194
195
                      CLASS0_SIZE, w);
      for (j = 0; j < MV_OFFSET_BITS; ++j)
        update_mv(w, comp_counts->bits[j], &comp->bits[j], MV_UPDATE_PROB);
    }

    for (i = 0; i < 2; ++i) {
196
      int j;
197
      for (j = 0; j < CLASS0_SIZE; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
198
        write_mv_update(av1_mv_fp_tree, mvc->comps[i].class0_fp[j],
199
200
                        counts->comps[i].class0_fp[j], MV_FP_SIZE, w);

Yaowu Xu's avatar
Yaowu Xu committed
201
      write_mv_update(av1_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
202
203
                      MV_FP_SIZE, w);
    }
204
#endif
205
206
207
208
209
210
211
212
213

    if (usehp) {
      for (i = 0; i < 2; ++i) {
        update_mv(w, counts->comps[i].class0_hp, &mvc->comps[i].class0_hp,
                  MV_UPDATE_PROB);
        update_mv(w, counts->comps[i].hp, &mvc->comps[i].hp, MV_UPDATE_PROB);
      }
    }
  }
Jingning Han's avatar
Jingning Han committed
214
215
}

Yaowu Xu's avatar
Yaowu Xu committed
216
void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
217
                   nmv_context *mvctx, int usehp) {
218
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
219
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
220
  aom_write_symbol(w, j, mvctx->joint_cdf, MV_JOINTS);
Jingning Han's avatar
Jingning Han committed
221
222
223
224
225
226
227
228
229
  if (mv_joint_vertical(j))
    encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);

  if (mv_joint_horizontal(j))
    encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);

  // If auto_mv_step_size is enabled then keep track of the largest
  // motion vector component used.
  if (cpi->sf.mv.auto_mv_step_size) {
Yaowu Xu's avatar
Yaowu Xu committed
230
231
    unsigned int maxv = AOMMAX(abs(mv->row), abs(mv->col)) >> 3;
    cpi->max_mv_magnitude = AOMMAX(maxv, cpi->max_mv_magnitude);
Jingning Han's avatar
Jingning Han committed
232
233
234
  }
}

Alex Converse's avatar
Alex Converse committed
235
236
237
238
239
240
241
242
#if CONFIG_INTRABC
void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
                   nmv_context *mvctx) {
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);

  aom_write_symbol(w, j, mvctx->joint_cdf, MV_JOINTS);
  if (mv_joint_vertical(j))
Alex Converse's avatar
Alex Converse committed
243
    encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
Alex Converse's avatar
Alex Converse committed
244
245

  if (mv_joint_horizontal(j))
Alex Converse's avatar
Alex Converse committed
246
    encode_mv_component(w, diff.col, &mvctx->comps[1], MV_SUBPEL_NONE);
Alex Converse's avatar
Alex Converse committed
247
248
249
}
#endif  // CONFIG_INTRABC

Yaowu Xu's avatar
Yaowu Xu committed
250
void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
Alex Converse's avatar
Alex Converse committed
251
252
                              const nmv_context *ctx,
                              MvSubpelPrecision precision) {
Yaowu Xu's avatar
Yaowu Xu committed
253
  av1_cost_tokens(mvjoint, ctx->joints, av1_mv_joint_tree);
Alex Converse's avatar
Alex Converse committed
254
255
  build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], precision);
  build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], precision);
Jingning Han's avatar
Jingning Han committed
256
257
}

Yue Chen's avatar
Yue Chen committed
258
259
#if CONFIG_EXT_INTER
static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
260
                    const int_mv mvs[2], const int_mv pred_mvs[2],
261
                    nmv_context_counts *nmv_counts) {
Yue Chen's avatar
Yue Chen committed
262
263
264
  int i;
  PREDICTION_MODE mode = mbmi->mode;

265
  if (mode == NEWMV || mode == NEW_NEWMV) {
Yue Chen's avatar
Yue Chen committed
266
    for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
267
      const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_mv;
268
269
      const MV diff = { mvs[i].as_mv.row - ref->row,
                        mvs[i].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
270
271
272
273
      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);
274
      nmv_context_counts *counts = &nmv_counts[nmv_ctx];
275
      (void)pred_mvs;
Alex Converse's avatar
Alex Converse committed
276
      av1_inc_mv(&diff, counts, 1);
Yue Chen's avatar
Yue Chen committed
277
    }
278
279
  } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv;
280
281
    const MV diff = { mvs[1].as_mv.row - ref->row,
                      mvs[1].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
282
283
284
285
    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);
286
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse's avatar
Alex Converse committed
287
    av1_inc_mv(&diff, counts, 1);
288
289
  } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv;
290
291
    const MV diff = { mvs[0].as_mv.row - ref->row,
                      mvs[0].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
292
293
294
295
    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);
296
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse's avatar
Alex Converse committed
297
    av1_inc_mv(&diff, counts, 1);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#if CONFIG_COMPOUND_SINGLEREF
  } else {
    assert(  // mode == SR_NEAREST_NEWMV ||
        mode == SR_NEAR_NEWMV || mode == SR_ZERO_NEWMV || mode == SR_NEW_NEWMV);
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv;
    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];
    (void)pred_mvs;
    MV diff;
    if (mode == SR_NEW_NEWMV) {
      diff.row = mvs[0].as_mv.row - ref->row;
      diff.col = mvs[0].as_mv.col - ref->col;
      av1_inc_mv(&diff, counts, 1);
    }
    diff.row = mvs[1].as_mv.row - ref->row;
    diff.col = mvs[1].as_mv.col - ref->col;
    av1_inc_mv(&diff, counts, 1);
#endif  // CONFIG_COMPOUND_SINGLEREF
Yue Chen's avatar
Yue Chen committed
319
320
321
  }
}

322
static void inc_mvs_sub8x8(const MODE_INFO *mi, int block, const int_mv mvs[2],
323
324
                           const MB_MODE_INFO_EXT *mbmi_ext,
                           nmv_context_counts *nmv_counts) {
Yue Chen's avatar
Yue Chen committed
325
326
  int i;
  PREDICTION_MODE mode = mi->bmi[block].as_mode;
327
  const MB_MODE_INFO *mbmi = &mi->mbmi;
Yue Chen's avatar
Yue Chen committed
328

329
  if (mode == NEWMV || mode == NEW_NEWMV) {
Yue Chen's avatar
Yue Chen committed
330
331
    for (i = 0; i < 1 + has_second_ref(&mi->mbmi); ++i) {
      const MV *ref = &mi->bmi[block].ref_mv[i].as_mv;
332
333
      const MV diff = { mvs[i].as_mv.row - ref->row,
                        mvs[i].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
334
335
336
337
      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);
338
      nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse's avatar
Alex Converse committed
339
      av1_inc_mv(&diff, counts, 1);
Yue Chen's avatar
Yue Chen committed
340
    }
341
342
  } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
    const MV *ref = &mi->bmi[block].ref_mv[1].as_mv;
343
344
    const MV diff = { mvs[1].as_mv.row - ref->row,
                      mvs[1].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
345
346
347
348
    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);
349
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse's avatar
Alex Converse committed
350
    av1_inc_mv(&diff, counts, 1);
351
352
  } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
    const MV *ref = &mi->bmi[block].ref_mv[0].as_mv;
353
354
    const MV diff = { mvs[0].as_mv.row - ref->row,
                      mvs[0].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
355
356
357
358
    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);
359
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
Alex Converse's avatar
Alex Converse committed
360
    av1_inc_mv(&diff, counts, 1);
Yue Chen's avatar
Yue Chen committed
361
362
  }
}
363
#else   // !CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
364
static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
365
                    const int_mv mvs[2], const int_mv pred_mvs[2],
366
                    nmv_context_counts *nmv_counts) {
Jingning Han's avatar
Jingning Han committed
367
368
369
  int i;

  for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
370
371
372
373
    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);
374
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
375
    const MV *ref = &pred_mvs[i].as_mv;
376
377
    const MV diff = { mvs[i].as_mv.row - ref->row,
                      mvs[i].as_mv.col - ref->col };
Alex Converse's avatar
Alex Converse committed
378
    av1_inc_mv(&diff, counts, 1);
Jingning Han's avatar
Jingning Han committed
379
380
  }
}
Yue Chen's avatar
Yue Chen committed
381
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
382

Yaowu Xu's avatar
Yaowu Xu committed
383
void av1_update_mv_count(ThreadData *td) {
Jingning Han's avatar
Jingning Han committed
384
385
386
387
  const MACROBLOCKD *xd = &td->mb.e_mbd;
  const MODE_INFO *mi = xd->mi[0];
  const MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MB_MODE_INFO_EXT *mbmi_ext = td->mb.mbmi_ext;
388
389
390
391
392
#if CONFIG_CB4X4
  const int unify_bsize = 1;
#else
  const int unify_bsize = 0;
#endif
Jingning Han's avatar
Jingning Han committed
393

394
  if (mbmi->sb_type < BLOCK_8X8 && !unify_bsize) {
Jingning Han's avatar
Jingning Han committed
395
396
397
398
399
400
401
    const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type];
    const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type];
    int idx, idy;

    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
        const int i = idy * 2 + idx;
Yue Chen's avatar
Yue Chen committed
402
403
404

#if CONFIG_EXT_INTER
        if (have_newmv_in_inter_mode(mi->bmi[i].as_mode))
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
405
          inc_mvs_sub8x8(mi, i, mi->bmi[i].as_mv, mbmi_ext, td->counts->mv);
Yue Chen's avatar
Yue Chen committed
406
#else
Jingning Han's avatar
Jingning Han committed
407
        if (mi->bmi[i].as_mode == NEWMV)
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
408
409
          inc_mvs(mbmi, mbmi_ext, mi->bmi[i].as_mv, mi->bmi[i].pred_mv,
                  td->counts->mv);
Yue Chen's avatar
Yue Chen committed
410
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
411
412
413
      }
    }
  } else {
Yue Chen's avatar
Yue Chen committed
414
415
416
#if CONFIG_EXT_INTER
    if (have_newmv_in_inter_mode(mbmi->mode))
#else
Jingning Han's avatar
Jingning Han committed
417
    if (mbmi->mode == NEWMV)
Yue Chen's avatar
Yue Chen committed
418
#endif  // CONFIG_EXT_INTER
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
419
      inc_mvs(mbmi, mbmi_ext, mbmi->mv, mbmi->pred_mv, td->counts->mv);
Jingning Han's avatar
Jingning Han committed
420
421
  }
}