2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
11 #include "vp10/common/onyxc_int.h"
12 #include "vp10/common/entropymv.h"
14 // Integer pel reference mv threshold for use of high-precision 1/8 mv
15 #define COMPANDED_MVREF_THRESH 8
17 const vpx_tree_index vp10_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
20 -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
23 const vpx_tree_index vp10_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
27 -MV_CLASS_2, -MV_CLASS_3,
29 -MV_CLASS_4, -MV_CLASS_5,
32 -MV_CLASS_7, -MV_CLASS_8,
33 -MV_CLASS_9, -MV_CLASS_10,
36 const vpx_tree_index vp10_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
40 const vpx_tree_index vp10_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = {
46 static const nmv_context default_nmv_context = {
49 { // Vertical component
51 {224, 144, 192, 168, 192, 176, 192, 198, 198, 245}, // class
53 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, // bits
54 {{128, 128, 64}, {96, 112, 64}}, // class0_fp
59 { // Horizontal component
61 {216, 128, 176, 160, 176, 176, 192, 198, 198, 208}, // class
63 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, // bits
64 {{128, 128, 64}, {96, 112, 64}}, // class0_fp
72 static const uint8_t log_in_base_2[] = {
73 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
74 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
75 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 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= (z >= CLASS0_SIZE * 4096) ?
124 MV_CLASS_10 : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
126 *offset = z - mv_class_base(c);
130 int vp10_use_mv_hp(const MV *ref) {
131 return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
132 (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH;
135 static void inc_mv_component(int v, nmv_component_counts *comp_counts,
136 int incr, int usehp) {
137 int s, z, c, o, d, e, f;
138 assert(v != 0); /* should not be zero */
140 comp_counts->sign[s] += incr;
141 z = (s ? -v : v) - 1; /* magnitude - 1 */
143 c = vp10_get_mv_class(z, &o);
144 comp_counts->classes[c] += incr;
146 d = (o >> 3); /* int mv data */
147 f = (o >> 1) & 3; /* fractional pel mv data */
148 e = (o & 1); /* high precision mv data */
150 if (c == MV_CLASS_0) {
151 comp_counts->class0[d] += incr;
152 comp_counts->class0_fp[d][f] += incr;
153 comp_counts->class0_hp[e] += usehp * incr;
156 int b = c + CLASS0_BITS - 1; // number of bits
157 for (i = 0; i < b; ++i)
158 comp_counts->bits[i][((d >> i) & 1)] += incr;
159 comp_counts->fp[f] += incr;
160 comp_counts->hp[e] += usehp * incr;
164 void vp10_inc_mv(const MV *mv, nmv_context_counts *counts, const int usehp) {
165 if (counts != NULL) {
166 const MV_JOINT_TYPE j = vp10_get_mv_joint(mv);
169 if (mv_joint_vertical(j)) {
170 inc_mv_component(mv->row, &counts->comps[0], 1,
171 !CONFIG_MISC_FIXES || usehp);
174 if (mv_joint_horizontal(j)) {
175 inc_mv_component(mv->col, &counts->comps[1], 1,
176 !CONFIG_MISC_FIXES || usehp);
181 void vp10_adapt_mv_probs(VP10_COMMON *cm, int allow_hp) {
184 nmv_context *fc = &cm->fc->nmvc;
185 const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
186 const nmv_context_counts *counts = &cm->counts.mv;
188 vpx_tree_merge_probs(vp10_mv_joint_tree, pre_fc->joints, counts->joints,
191 for (i = 0; i < 2; ++i) {
192 nmv_component *comp = &fc->comps[i];
193 const nmv_component *pre_comp = &pre_fc->comps[i];
194 const nmv_component_counts *c = &counts->comps[i];
196 comp->sign = mode_mv_merge_probs(pre_comp->sign, c->sign);
197 vpx_tree_merge_probs(vp10_mv_class_tree, pre_comp->classes, c->classes,
199 vpx_tree_merge_probs(vp10_mv_class0_tree, pre_comp->class0, c->class0,
202 for (j = 0; j < MV_OFFSET_BITS; ++j)
203 comp->bits[j] = mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
205 for (j = 0; j < CLASS0_SIZE; ++j)
206 vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->class0_fp[j],
207 c->class0_fp[j], comp->class0_fp[j]);
209 vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
212 comp->class0_hp = mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
213 comp->hp = mode_mv_merge_probs(pre_comp->hp, c->hp);
218 void vp10_init_mv_probs(VP10_COMMON *cm) {
219 cm->fc->nmvc = default_nmv_context;