Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * This source code is subject to the terms of the BSD 2 Clause License and
5 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 : * was not distributed with this source code in the LICENSE file, you can
7 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 : * Media Patent License 1.0 was not distributed with this source code in the
9 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 : */
11 :
12 : #include "av1/common/onyxc_int.h"
13 : #include "av1/common/entropymv.h"
14 :
15 : // Integer pel reference mv threshold for use of high-precision 1/8 mv
16 : #define COMPANDED_MVREF_THRESH 8
17 :
18 : const aom_tree_index av1_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
19 : -MV_JOINT_ZERO, 2, -MV_JOINT_HNZVZ, 4, -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
20 : };
21 :
22 : /* clang-format off */
23 : const aom_tree_index av1_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 : /* clang-format on */
36 :
37 : const aom_tree_index av1_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
38 : -0, -1,
39 : };
40 :
41 : const aom_tree_index av1_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = { -0, 2, -1,
42 : 4, -2, -3 };
43 :
44 : static const nmv_context default_nmv_context = {
45 : { 32, 64, 96 }, // joints
46 : { AOM_ICDF(4096), AOM_ICDF(11264), AOM_ICDF(19328), AOM_ICDF(32768),
47 : 0 }, // joint_cdf
48 : { {
49 : // Vertical component
50 : 128, // sign
51 : { 224, 144, 192, 168, 192, 176, 192, 198, 198, 245 }, // class
52 : { AOM_ICDF(28672), AOM_ICDF(30976), AOM_ICDF(31858), AOM_ICDF(32320),
53 : AOM_ICDF(32551), AOM_ICDF(32656), AOM_ICDF(32740), AOM_ICDF(32757),
54 : AOM_ICDF(32762), AOM_ICDF(32767), AOM_ICDF(32768), 0 }, // class_cdf
55 : { 216 }, // class0
56 : { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
57 : { { 128, 128, 64 }, { 96, 112, 64 } }, // class0_fp
58 : { 64, 96, 64 }, // fp
59 : { { AOM_ICDF(16384), AOM_ICDF(24576), AOM_ICDF(26624), AOM_ICDF(32768),
60 : 0 },
61 : { AOM_ICDF(12288), AOM_ICDF(21248), AOM_ICDF(24128), AOM_ICDF(32768),
62 : 0 } }, // class0_fp_cdf
63 : { AOM_ICDF(8192), AOM_ICDF(17408), AOM_ICDF(21248), AOM_ICDF(32768),
64 : 0 }, // fp_cdf
65 : 160, // class0_hp bit
66 : 128, // hp
67 : },
68 : {
69 : // Horizontal component
70 : 128, // sign
71 : { 216, 128, 176, 160, 176, 176, 192, 198, 198, 208 }, // class
72 : { AOM_ICDF(28672), AOM_ICDF(30976), AOM_ICDF(31858), AOM_ICDF(32320),
73 : AOM_ICDF(32551), AOM_ICDF(32656), AOM_ICDF(32740), AOM_ICDF(32757),
74 : AOM_ICDF(32762), AOM_ICDF(32767), AOM_ICDF(32768), 0 }, // class_cdf
75 : { 208 }, // class0
76 : { 136, 140, 148, 160, 176, 192, 224, 234, 234, 240 }, // bits
77 : { { 128, 128, 64 }, { 96, 112, 64 } }, // class0_fp
78 : { 64, 96, 64 }, // fp
79 : { { AOM_ICDF(16384), AOM_ICDF(24576), AOM_ICDF(26624), AOM_ICDF(32768),
80 : 0 },
81 : { AOM_ICDF(12288), AOM_ICDF(21248), AOM_ICDF(24128), AOM_ICDF(32768),
82 : 0 } }, // class0_fp_cdf
83 : { AOM_ICDF(8192), AOM_ICDF(17408), AOM_ICDF(21248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9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
128 : 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,
129 : 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,
130 : 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
131 : };
132 :
133 : #if CONFIG_GLOBAL_MOTION
134 : #if GLOBAL_TRANS_TYPES == 7 // All models
135 : const aom_tree_index av1_global_motion_types_tree[TREE_SIZE(
136 : GLOBAL_TRANS_TYPES)] = { -IDENTITY, 2, -TRANSLATION, 4,
137 : -ROTZOOM, 6, -AFFINE, 8,
138 : -HOMOGRAPHY, 10, -HORTRAPEZOID, -VERTRAPEZOID };
139 :
140 : static const aom_prob default_global_motion_types_prob[GLOBAL_TRANS_TYPES - 1] =
141 : { 224, 128, 192, 192, 32, 128 };
142 :
143 : #elif GLOBAL_TRANS_TYPES == 6 // Do not allow full homography
144 : const aom_tree_index
145 : av1_global_motion_types_tree[TREE_SIZE(GLOBAL_TRANS_TYPES)] = {
146 : -IDENTITY, 2, -TRANSLATION, 4, -ROTZOOM, 6, -AFFINE, 8, -HORTRAPEZOID,
147 : -VERTRAPEZOID
148 : };
149 :
150 : static const aom_prob default_global_motion_types_prob[GLOBAL_TRANS_TYPES - 1] =
151 : { 224, 128, 192, 192, 128 };
152 :
153 : #elif GLOBAL_TRANS_TYPES == 4 // Upto Affine
154 : const aom_tree_index av1_global_motion_types_tree[TREE_SIZE(
155 : GLOBAL_TRANS_TYPES)] = { -IDENTITY, 2, -TRANSLATION, 4, -ROTZOOM, -AFFINE };
156 :
157 : static const aom_prob default_global_motion_types_prob[GLOBAL_TRANS_TYPES - 1] =
158 : { 224, 128, 240 };
159 :
160 : #elif GLOBAL_TRANS_TYPES == 3 // Upto rotation-zoom
161 :
162 : const aom_tree_index av1_global_motion_types_tree[TREE_SIZE(
163 : GLOBAL_TRANS_TYPES)] = { -IDENTITY, 2, -TRANSLATION, -ROTZOOM };
164 :
165 : static const aom_prob default_global_motion_types_prob[GLOBAL_TRANS_TYPES - 1] =
166 : { 224, 128 };
167 : #endif // GLOBAL_TRANS_TYPES
168 : #endif // CONFIG_GLOBAL_MOTION
169 :
170 0 : static INLINE int mv_class_base(MV_CLASS_TYPE c) {
171 0 : return c ? CLASS0_SIZE << (c + 2) : 0;
172 : }
173 :
174 0 : MV_CLASS_TYPE av1_get_mv_class(int z, int *offset) {
175 0 : const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096)
176 : ? MV_CLASS_10
177 0 : : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
178 0 : if (offset) *offset = z - mv_class_base(c);
179 0 : return c;
180 : }
181 :
182 0 : static void inc_mv_component(int v, nmv_component_counts *comp_counts, int incr,
183 : int usehp) {
184 : int s, z, c, o, d, e, f;
185 0 : assert(v != 0); /* should not be zero */
186 0 : s = v < 0;
187 0 : comp_counts->sign[s] += incr;
188 0 : z = (s ? -v : v) - 1; /* magnitude - 1 */
189 :
190 0 : c = av1_get_mv_class(z, &o);
191 0 : comp_counts->classes[c] += incr;
192 :
193 0 : d = (o >> 3); /* int mv data */
194 0 : f = (o >> 1) & 3; /* fractional pel mv data */
195 0 : e = (o & 1); /* high precision mv data */
196 :
197 0 : if (c == MV_CLASS_0) {
198 0 : comp_counts->class0[d] += incr;
199 0 : comp_counts->class0_fp[d][f] += incr;
200 0 : if (usehp) comp_counts->class0_hp[e] += incr;
201 : } else {
202 : int i;
203 0 : int b = c + CLASS0_BITS - 1; // number of bits
204 0 : for (i = 0; i < b; ++i) comp_counts->bits[i][((d >> i) & 1)] += incr;
205 0 : comp_counts->fp[f] += incr;
206 0 : if (usehp) comp_counts->hp[e] += incr;
207 : }
208 0 : }
209 :
210 0 : void av1_inc_mv(const MV *mv, nmv_context_counts *counts, const int usehp) {
211 0 : if (counts != NULL) {
212 0 : const MV_JOINT_TYPE j = av1_get_mv_joint(mv);
213 0 : ++counts->joints[j];
214 :
215 0 : if (mv_joint_vertical(j))
216 0 : inc_mv_component(mv->row, &counts->comps[0], 1, usehp);
217 :
218 0 : if (mv_joint_horizontal(j))
219 0 : inc_mv_component(mv->col, &counts->comps[1], 1, usehp);
220 : }
221 0 : }
222 :
223 0 : void av1_adapt_mv_probs(AV1_COMMON *cm, int allow_hp) {
224 : int i, j;
225 : int idx;
226 0 : for (idx = 0; idx < NMV_CONTEXTS; ++idx) {
227 0 : nmv_context *nmvc = &cm->fc->nmvc[idx];
228 0 : const nmv_context *pre_nmvc = &cm->pre_fc->nmvc[idx];
229 0 : const nmv_context_counts *counts = &cm->counts.mv[idx];
230 0 : aom_tree_merge_probs(av1_mv_joint_tree, pre_nmvc->joints, counts->joints,
231 0 : nmvc->joints);
232 0 : for (i = 0; i < 2; ++i) {
233 0 : nmv_component *comp = &nmvc->comps[i];
234 0 : const nmv_component *pre_comp = &pre_nmvc->comps[i];
235 0 : const nmv_component_counts *c = &counts->comps[i];
236 :
237 0 : comp->sign = av1_mode_mv_merge_probs(pre_comp->sign, c->sign);
238 0 : aom_tree_merge_probs(av1_mv_class_tree, pre_comp->classes, c->classes,
239 0 : comp->classes);
240 0 : aom_tree_merge_probs(av1_mv_class0_tree, pre_comp->class0, c->class0,
241 0 : comp->class0);
242 :
243 0 : for (j = 0; j < MV_OFFSET_BITS; ++j)
244 0 : comp->bits[j] = av1_mode_mv_merge_probs(pre_comp->bits[j], c->bits[j]);
245 :
246 0 : for (j = 0; j < CLASS0_SIZE; ++j)
247 0 : aom_tree_merge_probs(av1_mv_fp_tree, pre_comp->class0_fp[j],
248 0 : c->class0_fp[j], comp->class0_fp[j]);
249 :
250 0 : aom_tree_merge_probs(av1_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
251 :
252 0 : if (allow_hp) {
253 0 : comp->class0_hp =
254 0 : av1_mode_mv_merge_probs(pre_comp->class0_hp, c->class0_hp);
255 0 : comp->hp = av1_mode_mv_merge_probs(pre_comp->hp, c->hp);
256 : }
257 : }
258 : }
259 0 : }
260 :
261 : #if !CONFIG_EC_ADAPT
262 : void av1_set_mv_cdfs(nmv_context *ctx) {
263 : int i;
264 : int j;
265 : av1_tree_to_cdf(av1_mv_joint_tree, ctx->joints, ctx->joint_cdf);
266 :
267 : for (i = 0; i < 2; ++i) {
268 : nmv_component *const comp_ctx = &ctx->comps[i];
269 : av1_tree_to_cdf(av1_mv_class_tree, comp_ctx->classes, comp_ctx->class_cdf);
270 :
271 : for (j = 0; j < CLASS0_SIZE; ++j) {
272 : av1_tree_to_cdf(av1_mv_fp_tree, comp_ctx->class0_fp[j],
273 : comp_ctx->class0_fp_cdf[j]);
274 : }
275 : av1_tree_to_cdf(av1_mv_fp_tree, comp_ctx->fp, comp_ctx->fp_cdf);
276 : }
277 : }
278 : #endif
279 :
280 0 : void av1_init_mv_probs(AV1_COMMON *cm) {
281 : int i;
282 0 : for (i = 0; i < NMV_CONTEXTS; ++i) {
283 : // NB: this sets CDFs too
284 0 : cm->fc->nmvc[i] = default_nmv_context;
285 : }
286 : #if CONFIG_INTRABC
287 : cm->fc->ndvc = default_nmv_context;
288 : #endif // CONFIG_INTRABC
289 : #if CONFIG_GLOBAL_MOTION
290 0 : av1_copy(cm->fc->global_motion_types_prob, default_global_motion_types_prob);
291 : #endif // CONFIG_GLOBAL_MOTION
292 0 : }
|