Merge "VPX: removed step checks from neon convolve code"
[platform/upstream/libvpx.git] / vp10 / common / entropymv.c
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 "vp10/common/onyxc_int.h"
12 #include "vp10/common/entropymv.h"
13
14 // Integer pel reference mv threshold for use of high-precision 1/8 mv
15 #define COMPANDED_MVREF_THRESH 8
16
17 const 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
23 const 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
36 const vpx_tree_index vp10_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
37   -0, -1,
38 };
39
40 const vpx_tree_index vp10_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = {
41   -0, 2,
42   -1, 4,
43   -2, -3
44 };
45
46 static const nmv_context default_nmv_context = {
47   {32, 64, 96},
48   {
49     { // Vertical component
50       128,                                                  // sign
51       {224, 144, 192, 168, 192, 176, 192, 198, 198, 245},   // class
52       {216},                                                // class0
53       {136, 140, 148, 160, 176, 192, 224, 234, 234, 240},   // bits
54       {{128, 128, 64}, {96, 112, 64}},                      // class0_fp
55       {64, 96, 64},                                         // fp
56       160,                                                  // class0_hp bit
57       128,                                                  // hp
58     },
59     { // Horizontal component
60       128,                                                  // sign
61       {216, 128, 176, 160, 176, 176, 192, 198, 198, 208},   // class
62       {208},                                                // class0
63       {136, 140, 148, 160, 176, 192, 224, 234, 234, 240},   // bits
64       {{128, 128, 64}, {96, 112, 64}},                      // class0_fp
65       {64, 96, 64},                                         // fp
66       160,                                                  // class0_hp bit
67       128,                                                  // hp
68     }
69   },
70 };
71
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, 6, 6, 6, 6, 6, 6, 6, 6,
76   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
77   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
78   6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
79   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
80   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
81   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
82   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
83   7, 7, 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,
112   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
113   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
114   9, 9, 9, 9, 9, 9, 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, 10
116 };
117
118 static INLINE int mv_class_base(MV_CLASS_TYPE c) {
119   return c ? CLASS0_SIZE << (c + 2) : 0;
120 }
121
122 MV_CLASS_TYPE vp10_get_mv_class(int z, int *offset) {
123   const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096) ?
124       MV_CLASS_10 : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
125   if (offset)
126     *offset = z - mv_class_base(c);
127   return c;
128 }
129
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;
133 }
134
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 */
139   s = v < 0;
140   comp_counts->sign[s] += incr;
141   z = (s ? -v : v) - 1;       /* magnitude - 1 */
142
143   c = vp10_get_mv_class(z, &o);
144   comp_counts->classes[c] += incr;
145
146   d = (o >> 3);               /* int mv data */
147   f = (o >> 1) & 3;           /* fractional pel mv data */
148   e = (o & 1);                /* high precision mv data */
149
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;
154   } else {
155     int i;
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;
161   }
162 }
163
164 void vp10_inc_mv(const MV *mv, nmv_context_counts *counts) {
165   if (counts != NULL) {
166     const MV_JOINT_TYPE j = vp10_get_mv_joint(mv);
167     ++counts->joints[j];
168
169     if (mv_joint_vertical(j)) {
170       inc_mv_component(mv->row, &counts->comps[0], 1, 1);
171     }
172
173     if (mv_joint_horizontal(j)) {
174       inc_mv_component(mv->col, &counts->comps[1], 1, 1);
175     }
176   }
177 }
178
179 void vp10_adapt_mv_probs(VP10_COMMON *cm, int allow_hp) {
180   int i, j;
181
182   nmv_context *fc = &cm->fc->nmvc;
183   const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
184   const nmv_context_counts *counts = &cm->counts.mv;
185
186   vpx_tree_merge_probs(vp10_mv_joint_tree, pre_fc->joints, counts->joints,
187                        fc->joints);
188
189   for (i = 0; i < 2; ++i) {
190     nmv_component *comp = &fc->comps[i];
191     const nmv_component *pre_comp = &pre_fc->comps[i];
192     const nmv_component_counts *c = &counts->comps[i];
193
194     comp->sign = mode_mv_merge_probs(pre_comp->sign, c->sign);
195     vpx_tree_merge_probs(vp10_mv_class_tree, pre_comp->classes, c->classes,
196                          comp->classes);
197     vpx_tree_merge_probs(vp10_mv_class0_tree, pre_comp->class0, c->class0,
198                          comp->class0);
199
200     for (j = 0; j < MV_OFFSET_BITS; ++j)
201       comp->bits[j] = mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
202
203     for (j = 0; j < CLASS0_SIZE; ++j)
204       vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->class0_fp[j],
205                            c->class0_fp[j], comp->class0_fp[j]);
206
207     vpx_tree_merge_probs(vp10_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
208
209     if (allow_hp) {
210       comp->class0_hp = mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
211       comp->hp = mode_mv_merge_probs(pre_comp->hp, c->hp);
212     }
213   }
214 }
215
216 void vp10_init_mv_probs(VP10_COMMON *cm) {
217   cm->fc->nmvc = default_nmv_context;
218 }