entropymv.c 8.96 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
2
3
4
5
6
7
8
9
10
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

Yaowu Xu's avatar
Yaowu Xu committed
11
12
#include "av1/common/onyxc_int.h"
#include "av1/common/entropymv.h"
Jingning Han's avatar
Jingning Han committed
13
14
15
16
17

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

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

clang-format's avatar
clang-format committed
21
/* clang-format off */
Jingning Han's avatar
Jingning Han committed
22
23
24
25
26
27
28
29
30
31
32
33
const vpx_tree_index vp10_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
  -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
34
/* clang-format on */
Jingning Han's avatar
Jingning Han committed
35
36
37
38
39

const vpx_tree_index vp10_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
  -0, -1,
};

clang-format's avatar
clang-format committed
40
41
const vpx_tree_index vp10_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = { -0, 2,  -1,
                                                                4,  -2, -3 };
Jingning Han's avatar
Jingning Han committed
42
43

static const nmv_context default_nmv_context = {
clang-format's avatar
clang-format committed
44
45
46
47
48
49
50
51
52
53
54
  { 32, 64, 96 },
  { {
        // 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
55
    },
clang-format's avatar
clang-format committed
56
57
58
59
60
61
62
63
64
65
66
    {
        // 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
67
68
69
};

static const uint8_t log_in_base_2[] = {
clang-format's avatar
clang-format committed
70
71
72
73
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
  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,
  4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6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, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
Jingning Han's avatar
Jingning Han committed
110
111
112
113
114
115
116
};

static INLINE int mv_class_base(MV_CLASS_TYPE c) {
  return c ? CLASS0_SIZE << (c + 2) : 0;
}

MV_CLASS_TYPE vp10_get_mv_class(int z, int *offset) {
clang-format's avatar
clang-format committed
117
118
119
120
  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
121
122
123
124
  return c;
}

int vp10_use_mv_hp(const MV *ref) {
125
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
126
  (void)ref;
127
128
  return 1;
#else
Jingning Han's avatar
Jingning Han committed
129
130
  return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
         (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH;
131
#endif
Jingning Han's avatar
Jingning Han committed
132
133
}

clang-format's avatar
clang-format committed
134
135
static void inc_mv_component(int v, nmv_component_counts *comp_counts, int incr,
                             int usehp) {
Jingning Han's avatar
Jingning Han committed
136
  int s, z, c, o, d, e, f;
clang-format's avatar
clang-format committed
137
  assert(v != 0); /* should not be zero */
Jingning Han's avatar
Jingning Han committed
138
139
  s = v < 0;
  comp_counts->sign[s] += incr;
clang-format's avatar
clang-format committed
140
  z = (s ? -v : v) - 1; /* magnitude - 1 */
Jingning Han's avatar
Jingning Han committed
141
142
143
144

  c = vp10_get_mv_class(z, &o);
  comp_counts->classes[c] += incr;

clang-format's avatar
clang-format committed
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
151
152
153
154
155

  if (c == MV_CLASS_0) {
    comp_counts->class0[d] += incr;
    comp_counts->class0_fp[d][f] += incr;
    comp_counts->class0_hp[e] += usehp * incr;
  } else {
    int i;
    int b = c + CLASS0_BITS - 1;  // number of bits
clang-format's avatar
clang-format committed
156
    for (i = 0; i < b; ++i) comp_counts->bits[i][((d >> i) & 1)] += incr;
Jingning Han's avatar
Jingning Han committed
157
158
159
160
161
    comp_counts->fp[f] += incr;
    comp_counts->hp[e] += usehp * incr;
  }
}

162
void vp10_inc_mv(const MV *mv, nmv_context_counts *counts, const int usehp) {
Jingning Han's avatar
Jingning Han committed
163
164
165
166
167
  if (counts != NULL) {
    const MV_JOINT_TYPE j = vp10_get_mv_joint(mv);
    ++counts->joints[j];

    if (mv_joint_vertical(j)) {
168
169
      inc_mv_component(mv->row, &counts->comps[0], 1,
                       !CONFIG_MISC_FIXES || usehp);
Jingning Han's avatar
Jingning Han committed
170
171
172
    }

    if (mv_joint_horizontal(j)) {
173
174
      inc_mv_component(mv->col, &counts->comps[1], 1,
                       !CONFIG_MISC_FIXES || usehp);
Jingning Han's avatar
Jingning Han committed
175
176
177
178
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
179
void vp10_adapt_mv_probs(VP10_COMMON *cm, int allow_hp) {
Jingning Han's avatar
Jingning Han committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  int i, j;

  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;

  vpx_tree_merge_probs(vp10_mv_joint_tree, pre_fc->joints, counts->joints,
                       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];

    comp->sign = mode_mv_merge_probs(pre_comp->sign, c->sign);
    vpx_tree_merge_probs(vp10_mv_class_tree, pre_comp->classes, c->classes,
                         comp->classes);
    vpx_tree_merge_probs(vp10_mv_class0_tree, pre_comp->class0, c->class0,
                         comp->class0);

    for (j = 0; j < MV_OFFSET_BITS; ++j)
      comp->bits[j] = mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);

    for (j = 0; j < CLASS0_SIZE; ++j)
      vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->class0_fp[j],
                           c->class0_fp[j], comp->class0_fp[j]);

    vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);

    if (allow_hp) {
      comp->class0_hp = mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
      comp->hp = mode_mv_merge_probs(pre_comp->hp, c->hp);
    }
  }
}

clang-format's avatar
clang-format committed
216
void vp10_init_mv_probs(VP10_COMMON *cm) { cm->fc->nmvc = default_nmv_context; }