encodemv.c 12.1 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

  assert(comp != 0);

45
46
// Sign
#if CONFIG_NEW_MULTISYMBOL
47
  aom_write_symbol(w, sign, mvcomp->sign_cdf, 2);
48
#else
Yaowu Xu's avatar
Yaowu Xu committed
49
  aom_write(w, sign, mvcomp->sign);
50
#endif
Jingning Han's avatar
Jingning Han committed
51

52
  // Class
53
  aom_write_symbol(w, mv_class, mvcomp->classes_cdf, MV_CLASSES);
Jingning Han's avatar
Jingning Han committed
54
55
56

  // Integer bits
  if (mv_class == MV_CLASS_0) {
57
58
59
#if CONFIG_NEW_MULTISYMBOL
    aom_write_symbol(w, d, mvcomp->class0_cdf, CLASS0_SIZE);
#else
60
    aom_write(w, d, mvcomp->class0[0]);
61
#endif
Jingning Han's avatar
Jingning Han committed
62
63
64
  } else {
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
65
66
#if CONFIG_NEW_MULTISYMBOL
    for (i = 0; i < n; ++i)
67
      aom_write_symbol(w, (d >> i) & 1, mvcomp->bits_cdf[i], 2);
68
#else
Yaowu Xu's avatar
Yaowu Xu committed
69
    for (i = 0; i < n; ++i) aom_write(w, (d >> i) & 1, mvcomp->bits[i]);
70
#endif
Jingning Han's avatar
Jingning Han committed
71
  }
Alex Converse's avatar
Alex Converse committed
72
// Fractional bits
RogerZhou's avatar
RogerZhou committed
73
#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
74
  if (precision > MV_SUBPEL_NONE)
RogerZhou's avatar
RogerZhou committed
75
#endif  // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
76
  {
clang-format's avatar
clang-format committed
77
78
79
80
    aom_write_symbol(
        w, fr,
        mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
        MV_FP_SIZE);
Alex Converse's avatar
Alex Converse committed
81
  }
Jingning Han's avatar
Jingning Han committed
82
83

  // High precision bit
Alex Converse's avatar
Alex Converse committed
84
  if (precision > MV_SUBPEL_LOW_PRECISION)
85
86
87
88
89
#if CONFIG_NEW_MULTISYMBOL
    aom_write_symbol(
        w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf,
        2);
#else
Yaowu Xu's avatar
Yaowu Xu committed
90
    aom_write(w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
91
#endif
Jingning Han's avatar
Jingning Han committed
92
93
94
}

static void build_nmv_component_cost_table(int *mvcost,
95
                                           const nmv_component *const mvcomp,
Alex Converse's avatar
Alex Converse committed
96
                                           MvSubpelPrecision precision) {
Jingning Han's avatar
Jingning Han committed
97
98
99
100
101
102
  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];

103
104
105
#if CONFIG_NEW_MULTISYMBOL
  av1_cost_tokens_from_cdf(sign_cost, mvcomp->sign_cdf, NULL);
#else
Yaowu Xu's avatar
Yaowu Xu committed
106
107
  sign_cost[0] = av1_cost_zero(mvcomp->sign);
  sign_cost[1] = av1_cost_one(mvcomp->sign);
108
109
110
111
112
#endif  // CONFIG_NEW_MULTISYMBOL
  av1_cost_tokens_from_cdf(class_cost, mvcomp->classes_cdf, NULL);
#if CONFIG_NEW_MULTISYMBOL
  av1_cost_tokens_from_cdf(class0_cost, mvcomp->class0_cdf, NULL);
#else
Yaowu Xu's avatar
Yaowu Xu committed
113
  av1_cost_tokens(class0_cost, mvcomp->class0, av1_mv_class0_tree);
114
#endif  // CONFIG_NEW_MULTISYMBOL
Jingning Han's avatar
Jingning Han committed
115
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
116
117
118
#if CONFIG_NEW_MULTISYMBOL
    av1_cost_tokens_from_cdf(bits_cost[i], mvcomp->bits_cdf[i], NULL);
#else
Yaowu Xu's avatar
Yaowu Xu committed
119
120
    bits_cost[i][0] = av1_cost_zero(mvcomp->bits[i]);
    bits_cost[i][1] = av1_cost_one(mvcomp->bits[i]);
121
#endif  // CONFIG_NEW_MULTISYMBOL
Jingning Han's avatar
Jingning Han committed
122
123
124
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
125
126
    av1_cost_tokens_from_cdf(class0_fp_cost[i], mvcomp->class0_fp_cdf[i], NULL);
  av1_cost_tokens_from_cdf(fp_cost, mvcomp->fp_cdf, NULL);
Jingning Han's avatar
Jingning Han committed
127

Alex Converse's avatar
Alex Converse committed
128
  if (precision > MV_SUBPEL_LOW_PRECISION) {
129
130
131
132
#if CONFIG_NEW_MULTISYMBOL
    av1_cost_tokens_from_cdf(class0_hp_cost, mvcomp->class0_hp_cdf, NULL);
    av1_cost_tokens_from_cdf(hp_cost, mvcomp->hp_cdf, NULL);
#else
Yaowu Xu's avatar
Yaowu Xu committed
133
134
135
136
    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);
137
#endif  // CONFIG_NEW_MULTISYMBOL
Jingning Han's avatar
Jingning Han committed
138
139
140
141
142
  }
  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
143
    c = av1_get_mv_class(z, &o);
Jingning Han's avatar
Jingning Han committed
144
    cost += class_cost[c];
145
146
147
    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
148
149
150
    if (c == MV_CLASS_0) {
      cost += class0_cost[d];
    } else {
151
      const int b = c + CLASS0_BITS - 1; /* number of bits */
152
      for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
Jingning Han's avatar
Jingning Han committed
153
    }
RogerZhou's avatar
RogerZhou committed
154
#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
155
    if (precision > MV_SUBPEL_NONE)
RogerZhou's avatar
RogerZhou committed
156
#endif  // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse's avatar
Alex Converse committed
157
    {
Jingning Han's avatar
Jingning Han committed
158
      if (c == MV_CLASS_0) {
Alex Converse's avatar
Alex Converse committed
159
        cost += class0_fp_cost[d][f];
Jingning Han's avatar
Jingning Han committed
160
      } else {
Alex Converse's avatar
Alex Converse committed
161
162
163
164
165
166
167
168
        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
169
170
171
172
173
174
175
      }
    }
    mvcost[v] = cost + sign_cost[0];
    mvcost[-v] = cost + sign_cost[1];
  }
}

176
#if !CONFIG_NEW_MULTISYMBOL
Yaowu Xu's avatar
Yaowu Xu committed
177
178
static void update_mv(aom_writer *w, const unsigned int ct[2], aom_prob *cur_p,
                      aom_prob upd_p) {
179
  (void)upd_p;
Yaowu Xu's avatar
Yaowu Xu committed
180
181
  // Just use the default maximum number of tile groups to avoid passing in the
  // actual
182
  // number
183
  av1_cond_prob_diff_update(w, cur_p, ct, DEFAULT_MAX_NUM_TG);
Jingning Han's avatar
Jingning Han committed
184
185
}

186
void av1_write_nmv_probs(AV1_COMMON *cm, int usehp, aom_writer *w,
Yaowu Xu's avatar
Yaowu Xu committed
187
                         nmv_context_counts *const nmv_counts) {
188
  int i;
189
  int nmv_ctx = 0;
RogerZhou's avatar
RogerZhou committed
190
#if CONFIG_AMVR
191
  if (cm->cur_frame_force_integer_mv) {
RogerZhou's avatar
RogerZhou committed
192
193
194
    return;
  }
#endif
195
196
197
198
199
200
201
202
203
204
205
206
  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];

    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
207
}
208
#endif
Jingning Han's avatar
Jingning Han committed
209

Yaowu Xu's avatar
Yaowu Xu committed
210
void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
211
                   nmv_context *mvctx, int usehp) {
212
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
213
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
RogerZhou's avatar
RogerZhou committed
214
#if CONFIG_AMVR
215
  if (cpi->common.cur_frame_force_integer_mv) {
RogerZhou's avatar
RogerZhou committed
216
217
218
    usehp = MV_SUBPEL_NONE;
  }
#endif
219
  aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
Jingning Han's avatar
Jingning Han committed
220
221
222
223
224
225
226
227
228
  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
229
230
    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
231
232
233
  }
}

Alex Converse's avatar
Alex Converse committed
234
235
236
#if CONFIG_INTRABC
void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
                   nmv_context *mvctx) {
Hui Su's avatar
Hui Su committed
237
238
239
240
241
  // DV and ref DV should not have sub-pel.
  assert((mv->col & 7) == 0);
  assert((mv->row & 7) == 0);
  assert((ref->col & 7) == 0);
  assert((ref->row & 7) == 0);
Alex Converse's avatar
Alex Converse committed
242
243
244
  const MV diff = { mv->row - ref->row, mv->col - ref->col };
  const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);

245
  aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
Alex Converse's avatar
Alex Converse committed
246
  if (mv_joint_vertical(j))
Alex Converse's avatar
Alex Converse committed
247
    encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
Alex Converse's avatar
Alex Converse committed
248
249

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

Yaowu Xu's avatar
Yaowu Xu committed
254
void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
Alex Converse's avatar
Alex Converse committed
255
256
                              const nmv_context *ctx,
                              MvSubpelPrecision precision) {
257
  av1_cost_tokens_from_cdf(mvjoint, ctx->joints_cdf, NULL);
Alex Converse's avatar
Alex Converse committed
258
259
  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
260
261
}

Yue Chen's avatar
Yue Chen committed
262
static void inc_mvs(const MB_MODE_INFO *mbmi, const MB_MODE_INFO_EXT *mbmi_ext,
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
263
                    const int_mv mvs[2], const int_mv pred_mvs[2],
RogerZhou's avatar
RogerZhou committed
264
265
266
267
268
269
                    nmv_context_counts *nmv_counts
#if CONFIG_AMVR
                    ,
                    MvSubpelPrecision precision
#endif
                    ) {
Yue Chen's avatar
Yue Chen committed
270
271
272
  int i;
  PREDICTION_MODE mode = mbmi->mode;

273
  if (mode == NEWMV || mode == NEW_NEWMV) {
Yue Chen's avatar
Yue Chen committed
274
    for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
275
      const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0].as_mv;
276
277
      const MV diff = { mvs[i].as_mv.row - ref->row,
                        mvs[i].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
278
279
280
281
      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);
282
      nmv_context_counts *counts = &nmv_counts[nmv_ctx];
283
      (void)pred_mvs;
RogerZhou's avatar
RogerZhou committed
284
285
286
#if CONFIG_AMVR
      av1_inc_mv(&diff, counts, precision);
#else
Alex Converse's avatar
Alex Converse committed
287
      av1_inc_mv(&diff, counts, 1);
RogerZhou's avatar
RogerZhou committed
288
#endif
Yue Chen's avatar
Yue Chen committed
289
    }
290
291
  } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv;
292
293
    const MV diff = { mvs[1].as_mv.row - ref->row,
                      mvs[1].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
294
295
296
297
    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);
298
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
RogerZhou's avatar
RogerZhou committed
299
300
301
#if CONFIG_AMVR
    av1_inc_mv(&diff, counts, precision);
#else
Alex Converse's avatar
Alex Converse committed
302
    av1_inc_mv(&diff, counts, 1);
RogerZhou's avatar
RogerZhou committed
303
#endif
304
305
  } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
    const MV *ref = &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv;
306
307
    const MV diff = { mvs[0].as_mv.row - ref->row,
                      mvs[0].as_mv.col - ref->col };
Yaowu Xu's avatar
Yaowu Xu committed
308
309
310
311
    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);
312
    nmv_context_counts *counts = &nmv_counts[nmv_ctx];
RogerZhou's avatar
RogerZhou committed
313
314
315
#if CONFIG_AMVR
    av1_inc_mv(&diff, counts, precision);
#else
Alex Converse's avatar
Alex Converse committed
316
    av1_inc_mv(&diff, counts, 1);
RogerZhou's avatar
RogerZhou committed
317
#endif
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#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
339
340
341
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
342
void av1_update_mv_count(ThreadData *td) {
Jingning Han's avatar
Jingning Han committed
343
344
345
346
  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;
RogerZhou's avatar
RogerZhou committed
347
348
#if CONFIG_AMVR
  MvSubpelPrecision precision = 1;
349
  if (xd->cur_frame_force_integer_mv) {
RogerZhou's avatar
RogerZhou committed
350
351
352
    precision = MV_SUBPEL_NONE;
  }
#endif
Jingning Han's avatar
Jingning Han committed
353

354
  if (have_newmv_in_inter_mode(mbmi->mode))
RogerZhou's avatar
RogerZhou committed
355
#if CONFIG_AMVR
356
    inc_mvs(mbmi, mbmi_ext, mbmi->mv, mbmi->pred_mv, td->counts->mv, precision);
RogerZhou's avatar
RogerZhou committed
357
#else
358
    inc_mvs(mbmi, mbmi_ext, mbmi->mv, mbmi->pred_mv, td->counts->mv);
RogerZhou's avatar
RogerZhou committed
359
#endif
Jingning Han's avatar
Jingning Han committed
360
}