1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "vp9/common/vp9_onyxc_int.h"
12 #include "vp9/common/vp9_entropymv.h"
13
14 const vpx_tree_index vp9_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
15 -MV_JOINT_ZERO, 2, -MV_JOINT_HNZVZ, 4, -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
16 };
17
18 const vpx_tree_index vp9_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
19 -MV_CLASS_0, 2, -MV_CLASS_1, 4, 6,
20 8, -MV_CLASS_2, -MV_CLASS_3, 10, 12,
21 -MV_CLASS_4, -MV_CLASS_5, -MV_CLASS_6, 14, 16,
22 18, -MV_CLASS_7, -MV_CLASS_8, -MV_CLASS_9, -MV_CLASS_10,
23 };
24
25 const vpx_tree_index vp9_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
26 -0, -1,
27 };
28
29 const vpx_tree_index vp9_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = { -0, 2, -1,
30 4, -2, -3 };
31
32 static const nmv_context default_nmv_context = {
33 { 32, 64, 96 },
34 { {
35 // Vertical component
36 128, // sign
37 { 224, 144, 192, 168, 192, 176, 192, 198, 198, 245 }, // class
38 { 216 }, // class0
39 { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
40 { { 128, 128, 64 }, { 96, 112, 64 } }, // class0_fp
41 { 64, 96, 64 }, // fp
42 160, // class0_hp bit
43 128, // hp
44 },
45 {
46 // Horizontal component
47 128, // sign
48 { 216, 128, 176, 160, 176, 176, 192, 198, 198, 208 }, // class
49 { 208 }, // class0
50 { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
51 { { 128, 128, 64 }, { 96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, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
97 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,
98 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
99 };
100
mv_class_base(MV_CLASS_TYPE c)101 static INLINE int mv_class_base(MV_CLASS_TYPE c) {
102 return c ? CLASS0_SIZE << (c + 2) : 0;
103 }
104
vp9_get_mv_class(int z,int * offset)105 MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) {
106 const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096)
107 ? MV_CLASS_10
108 : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
109 if (offset) *offset = z - mv_class_base(c);
110 return c;
111 }
112
inc_mv_component(int v,nmv_component_counts * comp_counts,int incr,int usehp)113 static void inc_mv_component(int v, nmv_component_counts *comp_counts, int incr,
114 int usehp) {
115 int s, z, c, o, d, e, f;
116 assert(v != 0); /* should not be zero */
117 s = v < 0;
118 comp_counts->sign[s] += incr;
119 z = (s ? -v : v) - 1; /* magnitude - 1 */
120
121 c = vp9_get_mv_class(z, &o);
122 comp_counts->classes[c] += incr;
123
124 d = (o >> 3); /* int mv data */
125 f = (o >> 1) & 3; /* fractional pel mv data */
126 e = (o & 1); /* high precision mv data */
127
128 if (c == MV_CLASS_0) {
129 comp_counts->class0[d] += incr;
130 comp_counts->class0_fp[d][f] += incr;
131 comp_counts->class0_hp[e] += usehp * incr;
132 } else {
133 int i;
134 int b = c + CLASS0_BITS - 1; // number of bits
135 for (i = 0; i < b; ++i) comp_counts->bits[i][((d >> i) & 1)] += incr;
136 comp_counts->fp[f] += incr;
137 comp_counts->hp[e] += usehp * incr;
138 }
139 }
140
vp9_inc_mv(const MV * mv,nmv_context_counts * counts)141 void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) {
142 if (counts != NULL) {
143 const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
144 ++counts->joints[j];
145
146 if (mv_joint_vertical(j)) {
147 inc_mv_component(mv->row, &counts->comps[0], 1, 1);
148 }
149
150 if (mv_joint_horizontal(j)) {
151 inc_mv_component(mv->col, &counts->comps[1], 1, 1);
152 }
153 }
154 }
155
vp9_adapt_mv_probs(VP9_COMMON * cm,int allow_hp)156 void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) {
157 int i, j;
158
159 nmv_context *fc = &cm->fc->nmvc;
160 const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
161 const nmv_context_counts *counts = &cm->counts.mv;
162
163 vpx_tree_merge_probs(vp9_mv_joint_tree, pre_fc->joints, counts->joints,
164 fc->joints);
165
166 for (i = 0; i < 2; ++i) {
167 nmv_component *comp = &fc->comps[i];
168 const nmv_component *pre_comp = &pre_fc->comps[i];
169 const nmv_component_counts *c = &counts->comps[i];
170
171 comp->sign = mode_mv_merge_probs(pre_comp->sign, c->sign);
172 vpx_tree_merge_probs(vp9_mv_class_tree, pre_comp->classes, c->classes,
173 comp->classes);
174 vpx_tree_merge_probs(vp9_mv_class0_tree, pre_comp->class0, c->class0,
175 comp->class0);
176
177 for (j = 0; j < MV_OFFSET_BITS; ++j)
178 comp->bits[j] = mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
179
180 for (j = 0; j < CLASS0_SIZE; ++j)
181 vpx_tree_merge_probs(vp9_mv_fp_tree, pre_comp->class0_fp[j],
182 c->class0_fp[j], comp->class0_fp[j]);
183
184 vpx_tree_merge_probs(vp9_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
185
186 if (allow_hp) {
187 comp->class0_hp = mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
188 comp->hp = mode_mv_merge_probs(pre_comp->hp, c->hp);
189 }
190 }
191 }
192
vp9_init_mv_probs(VP9_COMMON * cm)193 void vp9_init_mv_probs(VP9_COMMON *cm) { cm->fc->nmvc = default_nmv_context; }
194