entropymv.c 11.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 "av1/common/onyxc_int.h"
#include "av1/common/entropymv.h"
Jingning Han's avatar
Jingning Han committed
14
15
16
17

// Integer pel reference mv threshold for use of high-precision 1/8 mv
#define COMPANDED_MVREF_THRESH 8

Yaowu Xu's avatar
Yaowu Xu committed
18
const aom_tree_index av1_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
clang-format's avatar
clang-format committed
19
  -MV_JOINT_ZERO, 2, -MV_JOINT_HNZVZ, 4, -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
Jingning Han's avatar
Jingning Han committed
20
21
};

clang-format's avatar
clang-format committed
22
/* clang-format off */
Yaowu Xu's avatar
Yaowu Xu committed
23
const aom_tree_index av1_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
Jingning Han's avatar
Jingning Han committed
24
25
26
27
28
29
30
31
32
33
34
  -MV_CLASS_0, 2,
  -MV_CLASS_1, 4,
  6, 8,
  -MV_CLASS_2, -MV_CLASS_3,
  10, 12,
  -MV_CLASS_4, -MV_CLASS_5,
  -MV_CLASS_6, 14,
  16, 18,
  -MV_CLASS_7, -MV_CLASS_8,
  -MV_CLASS_9, -MV_CLASS_10,
};
clang-format's avatar
clang-format committed
35
/* clang-format on */
Jingning Han's avatar
Jingning Han committed
36

Yaowu Xu's avatar
Yaowu Xu committed
37
const aom_tree_index av1_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
Jingning Han's avatar
Jingning Han committed
38
39
40
  -0, -1,
};

Yaowu Xu's avatar
Yaowu Xu committed
41
42
const aom_tree_index av1_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = { -0, 2,  -1,
                                                               4,  -2, -3 };
Jingning Han's avatar
Jingning Han committed
43
44

static const nmv_context default_nmv_context = {
45
46
47
48
  { 32, 64, 96 },  // joints
#if CONFIG_DAALA_EC
  { 0, 0, 0, 0 },  // joint_cdf is computed from joints in av1_init_mv_probs()
#endif
clang-format's avatar
clang-format committed
49
50
51
52
53
54
55
56
57
58
  { {
        // Vertical component
        128,                                                   // sign
        { 224, 144, 192, 168, 192, 176, 192, 198, 198, 245 },  // class
        { 216 },                                               // class0
        { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 },  // bits
        { { 128, 128, 64 }, { 96, 112, 64 } },                 // class0_fp
        { 64, 96, 64 },                                        // fp
        160,                                                   // class0_hp bit
        128,                                                   // hp
Jingning Han's avatar
Jingning Han committed
59
    },
clang-format's avatar
clang-format committed
60
61
62
63
64
65
66
67
68
69
70
    {
        // Horizontal component
        128,                                                   // sign
        { 216, 128, 176, 160, 176, 176, 192, 198, 198, 208 },  // class
        { 208 },                                               // class0
        { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 },  // bits
        { { 128, 128, 64 }, { 96, 112, 64 } },                 // class0_fp
        { 64, 96, 64 },                                        // fp
        160,                                                   // class0_hp bit
        128,                                                   // hp
    } },
Jingning Han's avatar
Jingning Han committed
71
72
73
};

static const uint8_t log_in_base_2[] = {
clang-format's avatar
clang-format committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  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OBAL_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
117
118
const aom_tree_index
    av1_global_motion_types_tree[TREE_SIZE(GLOBAL_MOTION_TYPES)] = {
clang-format's avatar
clang-format committed
119
120
      -GLOBAL_ZERO, 2, -GLOBAL_TRANSLATION, 4, -GLOBAL_ROTZOOM, -GLOBAL_AFFINE
    };
121

Yaowu Xu's avatar
Yaowu Xu committed
122
static const aom_prob default_global_motion_types_prob[GLOBAL_MOTION_TYPES -
clang-format's avatar
clang-format committed
123
                                                       1] = { 224, 128, 128 };
124
125
#endif  // CONFIG_GLOBAL_MOTION

Jingning Han's avatar
Jingning Han committed
126
127
128
129
static INLINE int mv_class_base(MV_CLASS_TYPE c) {
  return c ? CLASS0_SIZE << (c + 2) : 0;
}

Yaowu Xu's avatar
Yaowu Xu committed
130
MV_CLASS_TYPE av1_get_mv_class(int z, int *offset) {
clang-format's avatar
clang-format committed
131
132
133
134
  const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096)
                              ? MV_CLASS_10
                              : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
  if (offset) *offset = z - mv_class_base(c);
Jingning Han's avatar
Jingning Han committed
135
136
137
  return c;
}

138
139
// TODO(jingning): This idle function is intentionally left as is for
// experimental purpose.
Yaowu Xu's avatar
Yaowu Xu committed
140
int av1_use_mv_hp(const MV *ref) {
clang-format's avatar
clang-format committed
141
  (void)ref;
142
  return 1;
Jingning Han's avatar
Jingning Han committed
143
144
}

clang-format's avatar
clang-format committed
145
146
static void inc_mv_component(int v, nmv_component_counts *comp_counts, int incr,
                             int usehp) {
Jingning Han's avatar
Jingning Han committed
147
  int s, z, c, o, d, e, f;
clang-format's avatar
clang-format committed
148
  assert(v != 0); /* should not be zero */
Jingning Han's avatar
Jingning Han committed
149
150
  s = v < 0;
  comp_counts->sign[s] += incr;
clang-format's avatar
clang-format committed
151
  z = (s ? -v : v) - 1; /* magnitude - 1 */
Jingning Han's avatar
Jingning Han committed
152

Yaowu Xu's avatar
Yaowu Xu committed
153
  c = av1_get_mv_class(z, &o);
Jingning Han's avatar
Jingning Han committed
154
155
  comp_counts->classes[c] += incr;

clang-format's avatar
clang-format committed
156
157
158
  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
159
160
161
162

  if (c == MV_CLASS_0) {
    comp_counts->class0[d] += incr;
    comp_counts->class0_fp[d][f] += incr;
clang-format's avatar
clang-format committed
163
    if (usehp) comp_counts->class0_hp[e] += incr;
Jingning Han's avatar
Jingning Han committed
164
165
166
  } else {
    int i;
    int b = c + CLASS0_BITS - 1;  // number of bits
clang-format's avatar
clang-format committed
167
    for (i = 0; i < b; ++i) comp_counts->bits[i][((d >> i) & 1)] += incr;
Jingning Han's avatar
Jingning Han committed
168
    comp_counts->fp[f] += incr;
clang-format's avatar
clang-format committed
169
    if (usehp) comp_counts->hp[e] += incr;
Jingning Han's avatar
Jingning Han committed
170
171
172
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
173
void av1_inc_mv(const MV *mv, nmv_context_counts *counts, const int usehp) {
Jingning Han's avatar
Jingning Han committed
174
  if (counts != NULL) {
Yaowu Xu's avatar
Yaowu Xu committed
175
    const MV_JOINT_TYPE j = av1_get_mv_joint(mv);
Jingning Han's avatar
Jingning Han committed
176
177
    ++counts->joints[j];

178
179
    if (mv_joint_vertical(j))
      inc_mv_component(mv->row, &counts->comps[0], 1, usehp);
Jingning Han's avatar
Jingning Han committed
180

181
182
    if (mv_joint_horizontal(j))
      inc_mv_component(mv->col, &counts->comps[1], 1, usehp);
Jingning Han's avatar
Jingning Han committed
183
184
185
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
186
void av1_adapt_mv_probs(AV1_COMMON *cm, int allow_hp) {
Jingning Han's avatar
Jingning Han committed
187
  int i, j;
188
189
190
191
192
193
194
#if CONFIG_REF_MV
  int idx;
  for (idx = 0; idx < NMV_CONTEXTS; ++idx) {
    nmv_context *fc = &cm->fc->nmvc[idx];
    const nmv_context *pre_fc =
        &cm->frame_contexts[cm->frame_context_idx].nmvc[idx];
    const nmv_context_counts *counts = &cm->counts.mv[idx];
Jingning Han's avatar
Jingning Han committed
195

Yaowu Xu's avatar
Yaowu Xu committed
196
    aom_tree_merge_probs(av1_mv_joint_tree, pre_fc->joints, counts->joints,
197
198
199
200
201
202
                         fc->joints);
    for (i = 0; i < 2; ++i) {
      nmv_component *comp = &fc->comps[i];
      const nmv_component *pre_comp = &pre_fc->comps[i];
      const nmv_component_counts *c = &counts->comps[i];

Yaowu Xu's avatar
Yaowu Xu committed
203
204
      comp->sign = av1_mode_mv_merge_probs(pre_comp->sign, c->sign);
      aom_tree_merge_probs(av1_mv_class_tree, pre_comp->classes, c->classes,
205
                           comp->classes);
Yaowu Xu's avatar
Yaowu Xu committed
206
      aom_tree_merge_probs(av1_mv_class0_tree, pre_comp->class0, c->class0,
207
208
209
                           comp->class0);

      for (j = 0; j < MV_OFFSET_BITS; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
210
        comp->bits[j] = av1_mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
211
212

      for (j = 0; j < CLASS0_SIZE; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
213
        aom_tree_merge_probs(av1_mv_fp_tree, pre_comp->class0_fp[j],
214
215
                             c->class0_fp[j], comp->class0_fp[j]);

Yaowu Xu's avatar
Yaowu Xu committed
216
      aom_tree_merge_probs(av1_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
217
218

      if (allow_hp) {
clang-format's avatar
clang-format committed
219
        comp->class0_hp =
Yaowu Xu's avatar
Yaowu Xu committed
220
221
            av1_mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
        comp->hp = av1_mode_mv_merge_probs(pre_comp->hp, c->hp);
222
223
224
225
      }
    }
  }
#else
Jingning Han's avatar
Jingning Han committed
226
227
228
229
  nmv_context *fc = &cm->fc->nmvc;
  const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
  const nmv_context_counts *counts = &cm->counts.mv;

Yaowu Xu's avatar
Yaowu Xu committed
230
  aom_tree_merge_probs(av1_mv_joint_tree, pre_fc->joints, counts->joints,
Jingning Han's avatar
Jingning Han committed
231
232
233
234
235
236
237
                       fc->joints);

  for (i = 0; i < 2; ++i) {
    nmv_component *comp = &fc->comps[i];
    const nmv_component *pre_comp = &pre_fc->comps[i];
    const nmv_component_counts *c = &counts->comps[i];

Yaowu Xu's avatar
Yaowu Xu committed
238
239
    comp->sign = av1_mode_mv_merge_probs(pre_comp->sign, c->sign);
    aom_tree_merge_probs(av1_mv_class_tree, pre_comp->classes, c->classes,
Jingning Han's avatar
Jingning Han committed
240
                         comp->classes);
Yaowu Xu's avatar
Yaowu Xu committed
241
    aom_tree_merge_probs(av1_mv_class0_tree, pre_comp->class0, c->class0,
Jingning Han's avatar
Jingning Han committed
242
243
244
                         comp->class0);

    for (j = 0; j < MV_OFFSET_BITS; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
245
      comp->bits[j] = av1_mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
Jingning Han's avatar
Jingning Han committed
246
247

    for (j = 0; j < CLASS0_SIZE; ++j)
Yaowu Xu's avatar
Yaowu Xu committed
248
      aom_tree_merge_probs(av1_mv_fp_tree, pre_comp->class0_fp[j],
Jingning Han's avatar
Jingning Han committed
249
250
                           c->class0_fp[j], comp->class0_fp[j]);

Yaowu Xu's avatar
Yaowu Xu committed
251
    aom_tree_merge_probs(av1_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
Jingning Han's avatar
Jingning Han committed
252
253

    if (allow_hp) {
clang-format's avatar
clang-format committed
254
      comp->class0_hp =
Yaowu Xu's avatar
Yaowu Xu committed
255
256
          av1_mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
      comp->hp = av1_mode_mv_merge_probs(pre_comp->hp, c->hp);
Jingning Han's avatar
Jingning Han committed
257
258
    }
  }
259
#endif
Jingning Han's avatar
Jingning Han committed
260
261
}

Yaowu Xu's avatar
Yaowu Xu committed
262
void av1_init_mv_probs(AV1_COMMON *cm) {
263
264
#if CONFIG_REF_MV
  int i;
clang-format's avatar
clang-format committed
265
  for (i = 0; i < NMV_CONTEXTS; ++i) cm->fc->nmvc[i] = default_nmv_context;
266
#else
Jingning Han's avatar
Jingning Han committed
267
  cm->fc->nmvc = default_nmv_context;
268
269
270
271
#if CONFIG_DAALA_EC
  av1_tree_to_cdf(av1_mv_joint_tree, cm->fc->nmvc.joints,
                  cm->fc->nmvc.joint_cdf);
#endif
272
#endif
273
#if CONFIG_GLOBAL_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
274
  av1_copy(cm->fc->global_motion_types_prob, default_global_motion_types_prob);
275
#endif  // CONFIG_GLOBAL_MOTION
Jingning Han's avatar
Jingning Han committed
276
}