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)] = { -0, -1 };
26
27 const vpx_tree_index vp9_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = { -0, 2, -1,
28 4, -2, -3 };
29
30 static const nmv_context default_nmv_context = {
31 { 32, 64, 96 },
32 { {
33 // Vertical component
34 128, // sign
35 { 224, 144, 192, 168, 192, 176, 192, 198, 198, 245 }, // class
36 { 216 }, // class0
37 { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
38 { { 128, 128, 64 }, { 96, 112, 64 } }, // class0_fp
39 { 64, 96, 64 }, // fp
40 160, // class0_hp bit
41 128, // hp
42 },
43 {
44 // Horizontal component
45 128, // sign
46 { 216, 128, 176, 160, 176, 176, 192, 198, 198, 208 }, // class
47 { 208 }, // class0
48 { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
49 { { 128, 128, 64 }, { 96, 112, 64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, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
95 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,
96 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
97 };
98
mv_class_base(MV_CLASS_TYPE c)99 static INLINE int mv_class_base(MV_CLASS_TYPE c) {
100 return c ? CLASS0_SIZE << (c + 2) : 0;
101 }
102
vp9_get_mv_class(int z,int * offset)103 MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) {
104 const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096)
105 ? MV_CLASS_10
106 : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
107 if (offset) *offset = z - mv_class_base(c);
108 return c;
109 }
110
inc_mv_component(int v,nmv_component_counts * comp_counts,int incr,int usehp)111 static void inc_mv_component(int v, nmv_component_counts *comp_counts, int incr,
112 int usehp) {
113 int s, z, c, o, d, e, f;
114 assert(v != 0); /* should not be zero */
115 s = v < 0;
116 comp_counts->sign[s] += incr;
117 z = (s ? -v : v) - 1; /* magnitude - 1 */
118
119 c = vp9_get_mv_class(z, &o);
120 comp_counts->classes[c] += incr;
121
122 d = (o >> 3); /* int mv data */
123 f = (o >> 1) & 3; /* fractional pel mv data */
124 e = (o & 1); /* high precision mv data */
125
126 if (c == MV_CLASS_0) {
127 comp_counts->class0[d] += incr;
128 comp_counts->class0_fp[d][f] += incr;
129 comp_counts->class0_hp[e] += usehp * incr;
130 } else {
131 int i;
132 int b = c + CLASS0_BITS - 1; // number of bits
133 for (i = 0; i < b; ++i) comp_counts->bits[i][((d >> i) & 1)] += incr;
134 comp_counts->fp[f] += incr;
135 comp_counts->hp[e] += usehp * incr;
136 }
137 }
138
vp9_inc_mv(const MV * mv,nmv_context_counts * counts)139 void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) {
140 if (counts != NULL) {
141 const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
142 ++counts->joints[j];
143
144 if (mv_joint_vertical(j)) {
145 inc_mv_component(mv->row, &counts->comps[0], 1, 1);
146 }
147
148 if (mv_joint_horizontal(j)) {
149 inc_mv_component(mv->col, &counts->comps[1], 1, 1);
150 }
151 }
152 }
153
vp9_adapt_mv_probs(VP9_COMMON * cm,int allow_hp)154 void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) {
155 int i, j;
156
157 nmv_context *fc = &cm->fc->nmvc;
158 const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
159 const nmv_context_counts *counts = &cm->counts.mv;
160
161 vpx_tree_merge_probs(vp9_mv_joint_tree, pre_fc->joints, counts->joints,
162 fc->joints);
163
164 for (i = 0; i < 2; ++i) {
165 nmv_component *comp = &fc->comps[i];
166 const nmv_component *pre_comp = &pre_fc->comps[i];
167 const nmv_component_counts *c = &counts->comps[i];
168
169 comp->sign = mode_mv_merge_probs(pre_comp->sign, c->sign);
170 vpx_tree_merge_probs(vp9_mv_class_tree, pre_comp->classes, c->classes,
171 comp->classes);
172 vpx_tree_merge_probs(vp9_mv_class0_tree, pre_comp->class0, c->class0,
173 comp->class0);
174
175 for (j = 0; j < MV_OFFSET_BITS; ++j)
176 comp->bits[j] = mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
177
178 for (j = 0; j < CLASS0_SIZE; ++j)
179 vpx_tree_merge_probs(vp9_mv_fp_tree, pre_comp->class0_fp[j],
180 c->class0_fp[j], comp->class0_fp[j]);
181
182 vpx_tree_merge_probs(vp9_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
183
184 if (allow_hp) {
185 comp->class0_hp = mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
186 comp->hp = mode_mv_merge_probs(pre_comp->hp, c->hp);
187 }
188 }
189 }
190
vp9_init_mv_probs(VP9_COMMON * cm)191 void vp9_init_mv_probs(VP9_COMMON *cm) { cm->fc->nmvc = default_nmv_context; }
192