encodemv.c 16 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
31
32
#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];
static struct av1_token mv_class0_encodings[CLASS0_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_class0_encodings, av1_mv_class0_tree);
  av1_tokens_from_tree(mv_fp_encodings, av1_mv_fp_tree);
Jingning Han's avatar
Jingning Han committed
33
34
}

Yaowu Xu's avatar
Yaowu Xu committed
35
static void encode_mv_component(aom_writer *w, int comp,
36
                                const nmv_component *mvcomp, int usehp) {
Jingning Han's avatar
Jingning Han committed
37
38
39
  int offset;
  const int sign = comp < 0;
  const int mag = sign ? -comp : comp;
Yaowu Xu's avatar
Yaowu Xu committed
40
  const int mv_class = av1_get_mv_class(mag - 1, &offset);
41
42
43
  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
44
45
46
47

  assert(comp != 0);

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

  // Class
Yaowu Xu's avatar
Yaowu Xu committed
51
52
  av1_write_token(w, av1_mv_class_tree, mvcomp->classes,
                  &mv_class_encodings[mv_class]);
Jingning Han's avatar
Jingning Han committed
53
54
55

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

  // Fractional bits
Yaowu Xu's avatar
Yaowu Xu committed
65
66
67
  av1_write_token(w, av1_mv_fp_tree,
                  mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp,
                  &mv_fp_encodings[fr]);
Jingning Han's avatar
Jingning Han committed
68
69
70

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

static void build_nmv_component_cost_table(int *mvcost,
75
                                           const nmv_component *const mvcomp,
Jingning Han's avatar
Jingning Han committed
76
77
78
79
80
81
82
                                           int usehp) {
  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
83
84
85
86
  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
87
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
Yaowu Xu's avatar
Yaowu Xu committed
88
89
    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
90
91
92
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
93
94
    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
95
96

  if (usehp) {
Yaowu Xu's avatar
Yaowu Xu committed
97
98
99
100
    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
101
102
103
104
105
  }
  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
106
    c = av1_get_mv_class(z, &o);
Jingning Han's avatar
Jingning Han committed
107
    cost += class_cost[c];
108
109
110
    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
111
112
113
114
    if (c == MV_CLASS_0) {
      cost += class0_cost[d];
    } else {
      int i, b;
115
116
      b = c + CLASS0_BITS - 1; /* number of bits */
      for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
Jingning Han's avatar
Jingning Han committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    }
    if (c == MV_CLASS_0) {
      cost += class0_fp_cost[d][f];
    } else {
      cost += fp_cost[f];
    }
    if (usehp) {
      if (c == MV_CLASS_0) {
        cost += class0_hp_cost[e];
      } else {
        cost += hp_cost[e];
      }
    }
    mvcost[v] = cost + sign_cost[0];
    mvcost[-v] = cost + sign_cost[1];
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
135
136
static void update_mv(aom_writer *w, const unsigned int ct[2], aom_prob *cur_p,
                      aom_prob upd_p) {
137
  (void)upd_p;
Yaowu Xu's avatar
Yaowu Xu committed
138
  av1_cond_prob_diff_update(w, cur_p, ct);
Jingning Han's avatar
Jingning Han committed
139
140
}

Yaowu Xu's avatar
Yaowu Xu committed
141
142
static void write_mv_update(const aom_tree_index *tree,
                            aom_prob probs[/*n - 1*/],
143
                            const unsigned int counts[/*n - 1*/], int n,
Yaowu Xu's avatar
Yaowu Xu committed
144
                            aom_writer *w) {
Jingning Han's avatar
Jingning Han committed
145
146
147
148
149
150
  int i;
  unsigned int branch_ct[32][2];

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

Yaowu Xu's avatar
Yaowu Xu committed
151
  av1_tree_probs_from_distribution(tree, branch_ct, counts);
Jingning Han's avatar
Jingning Han committed
152
153
154
155
  for (i = 0; i < n - 1; ++i)
    update_mv(w, branch_ct[i], &probs[i], MV_UPDATE_PROB);
}

156
void av1_write_nmv_probs(AV1_COMMON *cm, int usehp, aom_writer *w,
Yaowu Xu's avatar
Yaowu Xu committed
157
                         nmv_context_counts *const nmv_counts) {
Jingning Han's avatar
Jingning Han committed
158
  int i, j;
159
160
161
162
163
#if CONFIG_REF_MV
  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];
Yaowu Xu's avatar
Yaowu Xu committed
164
    write_mv_update(av1_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS,
165
                    w);
166

Yaowu Xu's avatar
Yaowu Xu committed
167
    av1_cond_prob_diff_update(w, &mvc->zero_rmv, counts->zero_rmv);
168

169
170
171
172
173
    for (i = 0; i < 2; ++i) {
      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
174
      write_mv_update(av1_mv_class_tree, comp->classes, comp_counts->classes,
175
                      MV_CLASSES, w);
Yaowu Xu's avatar
Yaowu Xu committed
176
      write_mv_update(av1_mv_class0_tree, comp->class0, comp_counts->class0,
177
178
179
180
181
182
183
                      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) {
      for (j = 0; j < CLASS0_SIZE; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
184
        write_mv_update(av1_mv_fp_tree, mvc->comps[i].class0_fp[j],
185
186
                        counts->comps[i].class0_fp[j], MV_FP_SIZE, w);

Yaowu Xu's avatar
Yaowu Xu committed
187
      write_mv_update(av1_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
188
189
190
191
192
193
194
195
196
197
198
199
                      MV_FP_SIZE, w);
    }

    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);
      }
    }
  }
#else
Jingning Han's avatar
Jingning Han committed
200
  nmv_context *const mvc = &cm->fc->nmvc;
201
  nmv_context_counts *const counts = nmv_counts;
Jingning Han's avatar
Jingning Han committed
202

Yaowu Xu's avatar
Yaowu Xu committed
203
  write_mv_update(av1_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS, w);
Jingning Han's avatar
Jingning Han committed
204
205
206
207
208
209

  for (i = 0; i < 2; ++i) {
    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
210
    write_mv_update(av1_mv_class_tree, comp->classes, comp_counts->classes,
Jingning Han's avatar
Jingning Han committed
211
                    MV_CLASSES, w);
Yaowu Xu's avatar
Yaowu Xu committed
212
    write_mv_update(av1_mv_class0_tree, comp->class0, comp_counts->class0,
Jingning Han's avatar
Jingning Han committed
213
214
215
216
217
218
219
                    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) {
    for (j = 0; j < CLASS0_SIZE; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
220
      write_mv_update(av1_mv_fp_tree, mvc->comps[i].class0_fp[j],
Jingning Han's avatar
Jingning Han committed
221
222
                      counts->comps[i].class0_fp[j], MV_FP_SIZE, w);

Yaowu Xu's avatar
Yaowu Xu committed
223
    write_mv_update(av1_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
Jingning Han's avatar
Jingning Han committed
224
225
226
227
228
229
230
231
232
233
                    MV_FP_SIZE, w);
  }

  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);
    }
  }
234
#endif
Jingning Han's avatar
Jingning Han committed
235
236
}

Yaowu Xu's avatar
Yaowu Xu committed
237
void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
238
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
239
                   int is_compound,
240
#endif
Yaowu Xu's avatar
Yaowu Xu committed
241
                   const nmv_context *mvctx, int usehp) {
242
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
243
244
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
  usehp = usehp && av1_use_mv_hp(ref);
Jingning Han's avatar
Jingning Han committed
245

246
247
#if CONFIG_REF_MV && !CONFIG_EXT_INTER
  if (is_compound) {
Yaowu Xu's avatar
Yaowu Xu committed
248
    aom_write(w, (j == MV_JOINT_ZERO), mvctx->zero_rmv);
249
    if (j == MV_JOINT_ZERO) return;
250
  } else {
251
    if (j == MV_JOINT_ZERO) assert(0);
252
253
254
255
256
257
258
  }
#endif

#if CONFIG_REF_MV && CONFIG_EXT_INTER
  (void)is_compound;
#endif

Yaowu Xu's avatar
Yaowu Xu committed
259
  av1_write_token(w, av1_mv_joint_tree, mvctx->joints, &mv_joint_encodings[j]);
Jingning Han's avatar
Jingning Han committed
260
261
262
263
264
265
266
267
268
  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
269
270
    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
271
272
273
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
274
275
276
void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
                              const nmv_context *ctx, int usehp) {
  av1_cost_tokens(mvjoint, ctx->joints, av1_mv_joint_tree);
Jingning Han's avatar
Jingning Han committed
277
278
279
280
  build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], usehp);
  build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], usehp);
}

Yue Chen's avatar
Yue Chen committed
281
282
283
#if CONFIG_EXT_INTER
static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
                    const int_mv mvs[2],
284
285
286
#if CONFIG_REF_MV
                    const int_mv pred_mvs[2],
#endif
287
                    nmv_context_counts *nmv_counts) {
Yue Chen's avatar
Yue Chen committed
288
289
290
  int i;
  PREDICTION_MODE mode = mbmi->mode;
  int mv_idx = (mode == NEWFROMNEARMV);
291
292
293
#if !CONFIG_REF_MV
  nmv_context_counts *counts = nmv_counts;
#endif
Yue Chen's avatar
Yue Chen committed
294

295
  if (mode == NEWMV || mode == NEWFROMNEARMV || mode == NEW_NEWMV) {
Yue Chen's avatar
Yue Chen committed
296
297
    for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
      const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[i]][mv_idx].as_mv;
298
299
      const MV diff = { mvs[i].as_mv.row - ref->row,
                        mvs[i].as_mv.col - ref->col };
300
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
301
302
303
304
      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);
305
      nmv_context_counts *counts = &nmv_counts[nmv_ctx];
306
      (void)pred_mvs;
307
#endif
Yaowu Xu's avatar
Yaowu Xu committed
308
      av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
Yue Chen's avatar
Yue Chen committed
309
    }
310
311
  } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv;
312
313
    const MV diff = { mvs[1].as_mv.row - ref->row,
                      mvs[1].as_mv.col - ref->col };
314
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
315
316
317
318
    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);
319
320
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
Yaowu Xu's avatar
Yaowu Xu committed
321
    av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
322
323
  } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv;
324
325
    const MV diff = { mvs[0].as_mv.row - ref->row,
                      mvs[0].as_mv.col - ref->col };
326
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
327
328
329
330
    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);
331
332
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
Yaowu Xu's avatar
Yaowu Xu committed
333
    av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
Yue Chen's avatar
Yue Chen committed
334
335
336
  }
}

337
static void inc_mvs_sub8x8(const MODE_INFO *mi, int block, const int_mv mvs[2],
338
339
340
341
#if CONFIG_REF_MV
                           const MB_MODE_INFO_EXT *mbmi_ext,
#endif
                           nmv_context_counts *nmv_counts) {
Yue Chen's avatar
Yue Chen committed
342
343
  int i;
  PREDICTION_MODE mode = mi->bmi[block].as_mode;
344
345
346
347
348
#if CONFIG_REF_MV
  const MB_MODE_INFO *mbmi = &mi->mbmi;
#else
  nmv_context_counts *counts = nmv_counts;
#endif
Yue Chen's avatar
Yue Chen committed
349

350
  if (mode == NEWMV || mode == NEWFROMNEARMV || mode == NEW_NEWMV) {
Yue Chen's avatar
Yue Chen committed
351
352
    for (i = 0; i < 1 + has_second_ref(&mi->mbmi); ++i) {
      const MV *ref = &mi->bmi[block].ref_mv[i].as_mv;
353
354
      const MV diff = { mvs[i].as_mv.row - ref->row,
                        mvs[i].as_mv.col - ref->col };
355
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
356
357
358
359
      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);
360
      nmv_context_counts *counts = &nmv_counts[nmv_ctx];
361
#endif
Yaowu Xu's avatar
Yaowu Xu committed
362
      av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
Yue Chen's avatar
Yue Chen committed
363
    }
364
365
  } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
    const MV *ref = &mi->bmi[block].ref_mv[1].as_mv;
366
367
    const MV diff = { mvs[1].as_mv.row - ref->row,
                      mvs[1].as_mv.col - ref->col };
368
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
369
370
371
372
    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);
373
374
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
Yaowu Xu's avatar
Yaowu Xu committed
375
    av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
376
377
  } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
    const MV *ref = &mi->bmi[block].ref_mv[0].as_mv;
378
379
    const MV diff = { mvs[0].as_mv.row - ref->row,
                      mvs[0].as_mv.col - ref->col };
380
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
381
382
383
384
    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);
385
386
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
#endif
Yaowu Xu's avatar
Yaowu Xu committed
387
    av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
Yue Chen's avatar
Yue Chen committed
388
389
390
  }
}
#else
Jingning Han's avatar
Jingning Han committed
391
392
static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
                    const int_mv mvs[2],
393
394
395
#if CONFIG_REF_MV
                    const int_mv pred_mvs[2],
#endif
396
                    nmv_context_counts *nmv_counts) {
Jingning Han's avatar
Jingning Han committed
397
  int i;
398
399
400
#if !CONFIG_REF_MV
  nmv_context_counts *counts = nmv_counts;
#endif
Jingning Han's avatar
Jingning Han committed
401
402

  for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
403
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
404
405
406
407
    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);
408
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
409
410
    const MV *ref = &pred_mvs[i].as_mv;
#else
Jingning Han's avatar
Jingning Han committed
411
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_mv;
412
#endif
413
414
    const MV diff = { mvs[i].as_mv.row - ref->row,
                      mvs[i].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
415
    av1_inc_mv(&diff, counts, av1_use_mv_hp(ref));
Jingning Han's avatar
Jingning Han committed
416
417
  }
}
Yue Chen's avatar
Yue Chen committed
418
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
419

Yaowu Xu's avatar
Yaowu Xu committed
420
void av1_update_mv_count(ThreadData *td) {
Jingning Han's avatar
Jingning Han committed
421
422
423
424
425
426
427
428
429
430
431
432
433
  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;

  if (mbmi->sb_type < BLOCK_8X8) {
    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
434
435
436

#if CONFIG_EXT_INTER
        if (have_newmv_in_inter_mode(mi->bmi[i].as_mode))
437
438
          inc_mvs_sub8x8(mi, i, mi->bmi[i].as_mv,
#if CONFIG_REF_MV
439
                         mbmi_ext, td->counts->mv);
440
441
442
#else
                         &td->counts->mv);
#endif
Yue Chen's avatar
Yue Chen committed
443
#else
Jingning Han's avatar
Jingning Han committed
444
        if (mi->bmi[i].as_mode == NEWMV)
445
446
          inc_mvs(mbmi, mbmi_ext, mi->bmi[i].as_mv,
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
447
                  mi->bmi[i].pred_mv, td->counts->mv);
448
449
450
#else
                  &td->counts->mv);
#endif
Yue Chen's avatar
Yue Chen committed
451
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
452
453
454
      }
    }
  } else {
Yue Chen's avatar
Yue Chen committed
455
456
457
#if CONFIG_EXT_INTER
    if (have_newmv_in_inter_mode(mbmi->mode))
#else
Jingning Han's avatar
Jingning Han committed
458
    if (mbmi->mode == NEWMV)
Yue Chen's avatar
Yue Chen committed
459
#endif  // CONFIG_EXT_INTER
460
461
      inc_mvs(mbmi, mbmi_ext, mbmi->mv,
#if CONFIG_REF_MV
462
              mbmi->pred_mv, td->counts->mv);
463
464
465
#else
              &td->counts->mv);
#endif
Jingning Han's avatar
Jingning Han committed
466
467
  }
}