16fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org/*
26fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
36fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *
46fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  Use of this source code is governed by a BSD-style license
56fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  that can be found in the LICENSE file in the root of the source
66fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  tree. An additional intellectual property rights grant can be found
76fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  in the file PATENTS.  All contributing project authors may
86fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org *  be found in the AUTHORS file in the root of the source tree.
96fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org */
106fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
116fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org#include "vp9/common/vp9_onyxc_int.h"
126fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org#include "vp9/common/vp9_entropymv.h"
136fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1410a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org#define MV_COUNT_SAT 20
1510a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org#define MV_MAX_UPDATE_FACTOR 128
166fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1793a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org// Integer pel reference mv threshold for use of high-precision 1/8 mv
1853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org#define COMPANDED_MVREF_THRESH 8
196fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
20ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgconst vp9_tree_index vp9_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = {
216fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_JOINT_ZERO, 2,
226fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_JOINT_HNZVZ, 4,
236fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
246fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org};
256fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
26ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgconst vp9_tree_index vp9_mv_class_tree[TREE_SIZE(MV_CLASSES)] = {
276fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_CLASS_0, 2,
286fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_CLASS_1, 4,
296fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  6, 8,
306fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_CLASS_2, -MV_CLASS_3,
316fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  10, 12,
326fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -MV_CLASS_4, -MV_CLASS_5,
331958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org  -MV_CLASS_6, 14,
341958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org  16, 18,
351958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org  -MV_CLASS_7, -MV_CLASS_8,
361958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org  -MV_CLASS_9, -MV_CLASS_10,
376fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org};
386fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
39ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgconst vp9_tree_index vp9_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = {
406fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -0, -1,
416fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org};
426fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
43d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.orgconst vp9_tree_index vp9_mv_fp_tree[TREE_SIZE(MV_FP_SIZE)] = {
446fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -0, 2,
456fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -1, 4,
466fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  -2, -3
476fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org};
486fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
4947265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.orgstatic const nmv_context default_nmv_context = {
506fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  {32, 64, 96},
5193a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org  {
5293a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org    { // Vertical component
5393a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      128,                                                  // sign
5493a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {224, 144, 192, 168, 192, 176, 192, 198, 198, 245},   // class
5593a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {216},                                                // class0
5693a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {136, 140, 148, 160, 176, 192, 224, 234, 234, 240},   // bits
5793a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {{128, 128, 64}, {96, 112, 64}},                      // class0_fp
5893a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {64, 96, 64},                                         // fp
5993a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      160,                                                  // class0_hp bit
6093a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      128,                                                  // hp
616fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    },
6293a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org    { // Horizontal component
6393a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      128,                                                  // sign
6493a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {216, 128, 176, 160, 176, 176, 192, 198, 198, 208},   // class
6593a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {208},                                                // class0
6693a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {136, 140, 148, 160, 176, 192, 224, 234, 234, 240},   // bits
6793a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {{128, 128, 64}, {96, 112, 64}},                      // class0_fp
6893a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      {64, 96, 64},                                         // fp
6993a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      160,                                                  // class0_hp bit
7093a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      128,                                                  // hp
716fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    }
726fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  },
736fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org};
746fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
75f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.orgstatic const uint8_t log_in_base_2[] = {
76f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
77f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
78f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
79f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
80f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
81f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
82f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
83f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
84f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
85f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
86f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
87f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
88f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
89f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
90f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
91f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
92f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
93f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
94f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
95f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
96f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
97f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
98f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
99f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
100f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
101f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
102f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
103f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
104f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
105f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
106f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
107f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
108f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
109f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
110f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
111f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
112f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
113f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
114f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
115f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
116f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
117f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
118f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10
119f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org};
120f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org
12193a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.orgstatic INLINE int mv_class_base(MV_CLASS_TYPE c) {
12293a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org  return c ? CLASS0_SIZE << (c + 2) : 0;
12393a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org}
124f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org
12593a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.orgMV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) {
12693a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org  const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096) ?
12793a74791c8e808ea76001ee07693aa2a5fdd3500johannkoenig@chromium.org      MV_CLASS_10 : (MV_CLASS_TYPE)log_in_base_2[z >> 3];
1286fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  if (offset)
1296fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    *offset = z - mv_class_base(c);
1306fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  return c;
1316fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
1326fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1337c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.orgint vp9_use_mv_hp(const MV *ref) {
1341958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org  return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
1351958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org         (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH;
1366fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
1376fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1386fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.orgint vp9_get_mv_mag(MV_CLASS_TYPE c, int offset) {
1396fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  return mv_class_base(c) + offset;
1406fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
1416fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1427c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.orgstatic void inc_mv_component(int v, nmv_component_counts *comp_counts,
1437c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.org                             int incr, int usehp) {
1446fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  int s, z, c, o, d, e, f;
145ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org  assert(v != 0);            /* should not be zero */
1466fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  s = v < 0;
1477c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.org  comp_counts->sign[s] += incr;
1486fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  z = (s ? -v : v) - 1;       /* magnitude - 1 */
1496fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1506fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  c = vp9_get_mv_class(z, &o);
1517c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.org  comp_counts->classes[c] += incr;
1526fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
1536fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  d = (o >> 3);               /* int mv data */
1546fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  f = (o >> 1) & 3;           /* fractional pel mv data */
1556fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  e = (o & 1);                /* high precision mv data */
156f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org
1576fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  if (c == MV_CLASS_0) {
1587c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.org    comp_counts->class0[d] += incr;
159f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org    comp_counts->class0_fp[d][f] += incr;
160f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org    comp_counts->class0_hp[e] += usehp * incr;
1616fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  } else {
16210a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    int i;
16310a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org    int b = c + CLASS0_BITS - 1;  // number of bits
1646fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    for (i = 0; i < b; ++i)
1657c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.org      comp_counts->bits[i][((d >> i) & 1)] += incr;
1667c1508f4449fe8184aa045737c20ba37e603dd03fgalligan@chromium.org    comp_counts->fp[f] += incr;
167f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org    comp_counts->hp[e] += usehp * incr;
1686fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  }
1696fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
1706fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
171ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgvoid vp9_inc_mv(const MV *mv, nmv_context_counts *counts) {
172ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org  if (counts != NULL) {
173ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    const MV_JOINT_TYPE j = vp9_get_mv_joint(mv);
174ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    ++counts->joints[j];
1756fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
176ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    if (mv_joint_vertical(j)) {
177ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org      inc_mv_component(mv->row, &counts->comps[0], 1, 1);
178ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    }
17910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org
180ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    if (mv_joint_horizontal(j)) {
181ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org      inc_mv_component(mv->col, &counts->comps[1], 1, 1);
182ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org    }
183f9586bb54d74c97d07b09eb2512f8569c9c1c025fgalligan@chromium.org  }
1846fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
1856fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
18653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgstatic vp9_prob adapt_prob(vp9_prob prep, const unsigned int ct[2]) {
187d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  return merge_probs(prep, ct, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR);
1886fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
1896fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
190d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.orgstatic void adapt_probs(const vp9_tree_index *tree, const vp9_prob *pre_probs,
191d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org                        const unsigned int *counts, vp9_prob *probs) {
19276e516e2154f353aa02c504bac88afb0f95fefa7johannkoenig@chromium.org  vp9_tree_merge_probs(tree, pre_probs, counts, MV_COUNT_SAT,
19376e516e2154f353aa02c504bac88afb0f95fefa7johannkoenig@chromium.org                       MV_MAX_UPDATE_FACTOR, probs);
1941958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org}
1951958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org
19653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.orgvoid vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) {
1971958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org  int i, j;
1981958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org
199d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  nmv_context *fc = &cm->fc.nmvc;
200d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc;
201d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  const nmv_context_counts *counts = &cm->counts.mv;
20247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
203d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org  adapt_probs(vp9_mv_joint_tree, pre_fc->joints, counts->joints, fc->joints);
2041958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org
2056fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  for (i = 0; i < 2; ++i) {
206d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    nmv_component *comp = &fc->comps[i];
207d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    const nmv_component *pre_comp = &pre_fc->comps[i];
208d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    const nmv_component_counts *c = &counts->comps[i];
209d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org
210d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    comp->sign = adapt_prob(pre_comp->sign, c->sign);
211d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    adapt_probs(vp9_mv_class_tree, pre_comp->classes, c->classes,
212d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org                comp->classes);
213d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    adapt_probs(vp9_mv_class0_tree, pre_comp->class0, c->class0, comp->class0);
21447265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
21547265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    for (j = 0; j < MV_OFFSET_BITS; ++j)
216d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org      comp->bits[j] = adapt_prob(pre_comp->bits[j], c->bits[j]);
21747265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
21847265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org    for (j = 0; j < CLASS0_SIZE; ++j)
219d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org      adapt_probs(vp9_mv_fp_tree, pre_comp->class0_fp[j], c->class0_fp[j],
220d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org                  comp->class0_fp[j]);
22147265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
222d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org    adapt_probs(vp9_mv_fp_tree, pre_comp->fp, c->fp, comp->fp);
22347265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org
22453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org    if (allow_hp) {
225d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org      comp->class0_hp = adapt_prob(pre_comp->class0_hp, c->class0_hp);
226d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org      comp->hp = adapt_prob(pre_comp->hp, c->hp);
2276fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org    }
2286fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org  }
2296fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
2306fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org
2316fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.orgvoid vp9_init_mv_probs(VP9_COMMON *cm) {
23247265f8fe3a36a426773454ad90d20c9aa616c24johannkoenig@chromium.org  cm->fc.nmvc = default_nmv_context;
2336fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org}
234