blob: 35f589a348493a16497ee2b59d3c581d69d528a2 [file] [log] [blame]
Jingning Han3ee6db62015-08-05 19:00:31 -07001/*
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
Jingning Han54d66ef2015-08-06 21:14:07 -070011#include "vp10/common/onyxc_int.h"
12#include "vp10/common/entropymv.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070013
14// Integer pel reference mv threshold for use of high-precision 1/8 mv
15#define COMPANDED_MVREF_THRESH 8
16
17const vpx_tree_index vp10_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
18 -MV_JOINT_ZERO, 2,
19 -MV_JOINT_HNZVZ, 4,
20 -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
21};
22
23const vpx_tree_index vp10_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
24 -MV_CLASS_0, 2,
25 -MV_CLASS_1, 4,
26 6, 8,
27 -MV_CLASS_2, -MV_CLASS_3,
28 10, 12,
29 -MV_CLASS_4, -MV_CLASS_5,
30 -MV_CLASS_6, 14,
31 16, 18,
32 -MV_CLASS_7, -MV_CLASS_8,
33 -MV_CLASS_9, -MV_CLASS_10,
34};
35
36const vpx_tree_index vp10_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
37 -0, -1,
38};
39
40const vpx_tree_index vp10_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = {
41 -0, 2,
42 -1, 4,
43 -2, -3
44};
45
46static const nmv_context default_nmv_context = {
Jingning Hanec2ffda2016-04-14 12:37:10 -070047#if CONFIG_REF_MV
48 {1, 64, 96},
49 128,
50#else
Jingning Han3ee6db62015-08-05 19:00:31 -070051 {32, 64, 96},
Jingning Hanec2ffda2016-04-14 12:37:10 -070052#endif
Jingning Han3ee6db62015-08-05 19:00:31 -070053 {
54 { // Vertical component
55 128, // sign
56 {224, 144, 192, 168, 192, 176, 192, 198, 198, 245}, // class
57 {216}, // class0
58 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, // bits
59 {{128, 128, 64}, {96, 112, 64}}, // class0_fp
60 {64, 96, 64}, // fp
61 160, // class0_hp bit
62 128, // hp
63 },
64 { // Horizontal component
65 128, // sign
66 {216, 128, 176, 160, 176, 176, 192, 198, 198, 208}, // class
67 {208}, // class0
68 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, // bits
69 {{128, 128, 64}, {96, 112, 64}}, // class0_fp
70 {64, 96, 64}, // fp
71 160, // class0_hp bit
72 128, // hp
73 }
74 },
75};
76
77static const uint8_t log_in_base_2[] = {
78 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
79 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
80 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
81 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
82 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
83 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
84 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
85 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
86 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7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, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
115 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
116 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
117 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
118 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
119 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
120 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
121};
122
123static INLINE int mv_class_base(MV_CLASS_TYPE c) {
124 return c ? CLASS0_SIZE << (c + 2) : 0;
125}
126
127MV_CLASS_TYPE vp10_get_mv_class(int z, int *offset) {
128 const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096) ?
129 MV_CLASS_10 : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
130 if (offset)
131 *offset = z - mv_class_base(c);
132 return c;
133}
134
135int vp10_use_mv_hp(const MV *ref) {
Ronald S. Bultje194c0a52015-10-21 15:20:22 -0400136#if CONFIG_MISC_FIXES
Ronald S. Bultjefa8ba202015-10-12 17:57:43 -0400137 (void) ref;
Ronald S. Bultje1799f2f2015-10-02 15:07:04 -0400138 return 1;
139#else
Jingning Han3ee6db62015-08-05 19:00:31 -0700140 return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
141 (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH;
Ronald S. Bultje1799f2f2015-10-02 15:07:04 -0400142#endif
Jingning Han3ee6db62015-08-05 19:00:31 -0700143}
144
145static void inc_mv_component(int v, nmv_component_counts *comp_counts,
146 int incr, int usehp) {
147 int s, z, c, o, d, e, f;
148 assert(v != 0); /* should not be zero */
149 s = v < 0;
150 comp_counts->sign[s] += incr;
151 z = (s ? -v : v) - 1; /* magnitude - 1 */
152
153 c = vp10_get_mv_class(z, &o);
154 comp_counts->classes[c] += incr;
155
156 d = (o >> 3); /* int mv data */
157 f = (o >> 1) & 3; /* fractional pel mv data */
158 e = (o & 1); /* high precision mv data */
159
160 if (c == MV_CLASS_0) {
161 comp_counts->class0[d] += incr;
162 comp_counts->class0_fp[d][f] += incr;
163 comp_counts->class0_hp[e] += usehp * incr;
164 } else {
165 int i;
166 int b = c + CLASS0_BITS - 1; // number of bits
167 for (i = 0; i < b; ++i)
168 comp_counts->bits[i][((d >> i) & 1)] += incr;
169 comp_counts->fp[f] += incr;
170 comp_counts->hp[e] += usehp * incr;
171 }
172}
173
Ronald S. Bultje1e9e9ce2015-09-07 14:32:28 -0400174void vp10_inc_mv(const MV *mv, nmv_context_counts *counts, const int usehp) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700175 if (counts != NULL) {
176 const MV_JOINT_TYPE j = vp10_get_mv_joint(mv);
Jingning Hanec2ffda2016-04-14 12:37:10 -0700177
178#if CONFIG_REF_MV
179 ++counts->zero_rmv[j == MV_JOINT_ZERO];
180 if (j == MV_JOINT_ZERO)
181 return;
182#endif
Jingning Han3ee6db62015-08-05 19:00:31 -0700183 ++counts->joints[j];
184
185 if (mv_joint_vertical(j)) {
Ronald S. Bultje1e9e9ce2015-09-07 14:32:28 -0400186 inc_mv_component(mv->row, &counts->comps[0], 1,
187 !CONFIG_MISC_FIXES || usehp);
Jingning Han3ee6db62015-08-05 19:00:31 -0700188 }
189
190 if (mv_joint_horizontal(j)) {
Ronald S. Bultje1e9e9ce2015-09-07 14:32:28 -0400191 inc_mv_component(mv->col, &counts->comps[1], 1,
192 !CONFIG_MISC_FIXES || usehp);
Jingning Han3ee6db62015-08-05 19:00:31 -0700193 }
194 }
195}
196
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700197void vp10_adapt_mv_probs(VP10_COMMON *cm, int allow_hp) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700198 int i, j;
Jingning Handf59bb82016-02-18 11:57:44 -0800199#if CONFIG_REF_MV
200 int idx;
201 for (idx = 0; idx < NMV_CONTEXTS; ++idx) {
202 nmv_context *fc = &cm->fc->nmvc[idx];
203 const nmv_context *pre_fc =
204 &cm->frame_contexts[cm->frame_context_idx].nmvc[idx];
205 const nmv_context_counts *counts = &cm->counts.mv[idx];
Jingning Han3ee6db62015-08-05 19:00:31 -0700206
Jingning Handf59bb82016-02-18 11:57:44 -0800207 vpx_tree_merge_probs(vp10_mv_joint_tree, pre_fc->joints, counts->joints,
208 fc->joints);
Jingning Hanec2ffda2016-04-14 12:37:10 -0700209#if CONFIG_REF_MV
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700210 fc->zero_rmv = vp10_mode_mv_merge_probs(pre_fc->zero_rmv, counts->zero_rmv);
Jingning Hanec2ffda2016-04-14 12:37:10 -0700211#endif
Jingning Handf59bb82016-02-18 11:57:44 -0800212
213 for (i = 0; i < 2; ++i) {
214 nmv_component *comp = &fc->comps[i];
215 const nmv_component *pre_comp = &pre_fc->comps[i];
216 const nmv_component_counts *c = &counts->comps[i];
217
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700218 comp->sign = vp10_mode_mv_merge_probs(pre_comp->sign, c->sign);
Jingning Handf59bb82016-02-18 11:57:44 -0800219 vpx_tree_merge_probs(vp10_mv_class_tree, pre_comp->classes, c->classes,
220 comp->classes);
221 vpx_tree_merge_probs(vp10_mv_class0_tree, pre_comp->class0, c->class0,
222 comp->class0);
223
224 for (j = 0; j < MV_OFFSET_BITS; ++j)
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700225 comp->bits[j] = vp10_mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
Jingning Handf59bb82016-02-18 11:57:44 -0800226
227 for (j = 0; j < CLASS0_SIZE; ++j)
228 vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->class0_fp[j],
229 c->class0_fp[j], comp->class0_fp[j]);
230
231 vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
232
233 if (allow_hp) {
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700234 comp->class0_hp = vp10_mode_mv_merge_probs(pre_comp->class0_hp,
235 c->class0_hp);
236 comp->hp = vp10_mode_mv_merge_probs(pre_comp->hp, c->hp);
Jingning Handf59bb82016-02-18 11:57:44 -0800237 }
238 }
239 }
240#else
Jingning Han3ee6db62015-08-05 19:00:31 -0700241 nmv_context *fc = &cm->fc->nmvc;
242 const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
243 const nmv_context_counts *counts = &cm->counts.mv;
244
245 vpx_tree_merge_probs(vp10_mv_joint_tree, pre_fc->joints, counts->joints,
246 fc->joints);
247
248 for (i = 0; i < 2; ++i) {
249 nmv_component *comp = &fc->comps[i];
250 const nmv_component *pre_comp = &pre_fc->comps[i];
251 const nmv_component_counts *c = &counts->comps[i];
252
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700253 comp->sign = vp10_mode_mv_merge_probs(pre_comp->sign, c->sign);
Jingning Han3ee6db62015-08-05 19:00:31 -0700254 vpx_tree_merge_probs(vp10_mv_class_tree, pre_comp->classes, c->classes,
255 comp->classes);
256 vpx_tree_merge_probs(vp10_mv_class0_tree, pre_comp->class0, c->class0,
257 comp->class0);
258
259 for (j = 0; j < MV_OFFSET_BITS; ++j)
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700260 comp->bits[j] = vp10_mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
Jingning Han3ee6db62015-08-05 19:00:31 -0700261
262 for (j = 0; j < CLASS0_SIZE; ++j)
263 vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->class0_fp[j],
264 c->class0_fp[j], comp->class0_fp[j]);
265
266 vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
267
268 if (allow_hp) {
Debargha Mukherjeee4bf50b2016-04-26 15:04:19 -0700269 comp->class0_hp = vp10_mode_mv_merge_probs(
270 pre_comp->class0_hp, c->class0_hp);
271 comp->hp = vp10_mode_mv_merge_probs(pre_comp->hp, c->hp);
Jingning Han3ee6db62015-08-05 19:00:31 -0700272 }
273 }
Jingning Handf59bb82016-02-18 11:57:44 -0800274#endif
Jingning Han3ee6db62015-08-05 19:00:31 -0700275}
276
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700277void vp10_init_mv_probs(VP10_COMMON *cm) {
Jingning Handf59bb82016-02-18 11:57:44 -0800278#if CONFIG_REF_MV
279 int i;
280 for (i = 0; i < NMV_CONTEXTS; ++i)
281 cm->fc->nmvc[i] = default_nmv_context;
282#else
Jingning Han3ee6db62015-08-05 19:00:31 -0700283 cm->fc->nmvc = default_nmv_context;
Jingning Handf59bb82016-02-18 11:57:44 -0800284#endif
Jingning Han3ee6db62015-08-05 19:00:31 -0700285}