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 12 #include "vp9/common/vp9_onyxc_int.h" 13 #include "vp9/common/vp9_entropymv.h" 14 15 #define MV_COUNT_SAT 20 16 #define MV_MAX_UPDATE_FACTOR 128 17 18 /* Integer pel reference mv threshold for use of high-precision 1/8 mv */ 19 #define COMPANDED_MVREF_THRESH 8 20 21 const vp9_tree_index vp9_mv_joint_tree[2 * MV_JOINTS - 2] = { 22 -MV_JOINT_ZERO, 2, 23 -MV_JOINT_HNZVZ, 4, 24 -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ 25 }; 26 struct vp9_token vp9_mv_joint_encodings[MV_JOINTS]; 27 28 const vp9_tree_index vp9_mv_class_tree[2 * MV_CLASSES - 2] = { 29 -MV_CLASS_0, 2, 30 -MV_CLASS_1, 4, 31 6, 8, 32 -MV_CLASS_2, -MV_CLASS_3, 33 10, 12, 34 -MV_CLASS_4, -MV_CLASS_5, 35 -MV_CLASS_6, 14, 36 16, 18, 37 -MV_CLASS_7, -MV_CLASS_8, 38 -MV_CLASS_9, -MV_CLASS_10, 39 }; 40 struct vp9_token vp9_mv_class_encodings[MV_CLASSES]; 41 42 const vp9_tree_index vp9_mv_class0_tree [2 * CLASS0_SIZE - 2] = { 43 -0, -1, 44 }; 45 struct vp9_token vp9_mv_class0_encodings[CLASS0_SIZE]; 46 47 const vp9_tree_index vp9_mv_fp_tree [2 * 4 - 2] = { 48 -0, 2, 49 -1, 4, 50 -2, -3 51 }; 52 struct vp9_token vp9_mv_fp_encodings[4]; 53 54 static const nmv_context default_nmv_context = { 55 {32, 64, 96}, 56 { 57 { /* vert component */ 58 128, /* sign */ 59 {224, 144, 192, 168, 192, 176, 192, 198, 198, 245}, /* class */ 60 {216}, /* class0 */ 61 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, /* bits */ 62 {{128, 128, 64}, {96, 112, 64}}, /* class0_fp */ 63 {64, 96, 64}, /* fp */ 64 160, /* class0_hp bit */ 65 128, /* hp */ 66 }, 67 { /* hor component */ 68 128, /* sign */ 69 {216, 128, 176, 160, 176, 176, 192, 198, 198, 208}, /* class */ 70 {208}, /* class0 */ 71 {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, /* bits */ 72 {{128, 128, 64}, {96, 112, 64}}, /* class0_fp */ 73 {64, 96, 64}, /* fp */ 74 160, /* class0_hp bit */ 75 128, /* hp */ 76 } 77 }, 78 }; 79 80 #define mv_class_base(c) ((c) ? (CLASS0_SIZE << (c + 2)) : 0) 81 82 static const uint8_t log_in_base_2[] = { 83 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 84 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 85 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 86 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 87 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 88 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 89 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 90 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 91 7, 7, 7, 7, 7, 7, 7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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, 9, 9, 9, 9, 9, 9, 9, 9, 121 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 122 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 123 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 124 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 125 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10 126 }; 127 128 MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) { 129 MV_CLASS_TYPE c = MV_CLASS_0; 130 if (z >= CLASS0_SIZE * 4096) 131 c = MV_CLASS_10; 132 else 133 c = log_in_base_2[z >> 3]; 134 135 if (offset) 136 *offset = z - mv_class_base(c); 137 return c; 138 } 139 140 int vp9_use_mv_hp(const MV *ref) { 141 return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH && 142 (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH; 143 } 144 145 int vp9_get_mv_mag(MV_CLASS_TYPE c, int offset) { 146 return mv_class_base(c) + offset; 147 } 148 149 static void inc_mv_component(int v, nmv_component_counts *comp_counts, 150 int incr, int usehp) { 151 int s, z, c, o, d, e, f; 152 assert (v != 0); /* should not be zero */ 153 s = v < 0; 154 comp_counts->sign[s] += incr; 155 z = (s ? -v : v) - 1; /* magnitude - 1 */ 156 157 c = vp9_get_mv_class(z, &o); 158 comp_counts->classes[c] += incr; 159 160 d = (o >> 3); /* int mv data */ 161 f = (o >> 1) & 3; /* fractional pel mv data */ 162 e = (o & 1); /* high precision mv data */ 163 164 if (c == MV_CLASS_0) { 165 comp_counts->class0[d] += incr; 166 comp_counts->class0_fp[d][f] += incr; 167 comp_counts->class0_hp[e] += usehp * incr; 168 } else { 169 int i; 170 int b = c + CLASS0_BITS - 1; // number of bits 171 for (i = 0; i < b; ++i) 172 comp_counts->bits[i][((d >> i) & 1)] += incr; 173 comp_counts->fp[f] += incr; 174 comp_counts->hp[e] += usehp * incr; 175 } 176 } 177 178 179 void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) { 180 const MV_JOINT_TYPE j = vp9_get_mv_joint(mv); 181 ++counts->joints[j]; 182 183 if (mv_joint_vertical(j)) { 184 inc_mv_component(mv->row, &counts->comps[0], 1, 1); 185 } 186 187 if (mv_joint_horizontal(j)) { 188 inc_mv_component(mv->col, &counts->comps[1], 1, 1); 189 } 190 } 191 192 static vp9_prob adapt_prob(vp9_prob prep, const unsigned int ct[2]) { 193 return merge_probs2(prep, ct, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR); 194 } 195 196 static unsigned int adapt_probs(unsigned int i, 197 vp9_tree tree, 198 vp9_prob this_probs[], 199 const vp9_prob last_probs[], 200 const unsigned int num_events[]) { 201 202 203 const unsigned int left = tree[i] <= 0 204 ? num_events[-tree[i]] 205 : adapt_probs(tree[i], tree, this_probs, last_probs, num_events); 206 207 const unsigned int right = tree[i + 1] <= 0 208 ? num_events[-tree[i + 1]] 209 : adapt_probs(tree[i + 1], tree, this_probs, last_probs, num_events); 210 const unsigned int ct[2] = { left, right }; 211 this_probs[i >> 1] = adapt_prob(last_probs[i >> 1], ct); 212 return left + right; 213 } 214 215 216 void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) { 217 int i, j; 218 219 FRAME_CONTEXT *pre_fc = &cm->frame_contexts[cm->frame_context_idx]; 220 221 nmv_context *ctx = &cm->fc.nmvc; 222 nmv_context *pre_ctx = &pre_fc->nmvc; 223 nmv_context_counts *cts = &cm->counts.mv; 224 225 adapt_probs(0, vp9_mv_joint_tree, ctx->joints, pre_ctx->joints, cts->joints); 226 227 for (i = 0; i < 2; ++i) { 228 ctx->comps[i].sign = adapt_prob(pre_ctx->comps[i].sign, cts->comps[i].sign); 229 adapt_probs(0, vp9_mv_class_tree, ctx->comps[i].classes, 230 pre_ctx->comps[i].classes, cts->comps[i].classes); 231 adapt_probs(0, vp9_mv_class0_tree, ctx->comps[i].class0, 232 pre_ctx->comps[i].class0, cts->comps[i].class0); 233 234 for (j = 0; j < MV_OFFSET_BITS; ++j) 235 ctx->comps[i].bits[j] = adapt_prob(pre_ctx->comps[i].bits[j], 236 cts->comps[i].bits[j]); 237 238 for (j = 0; j < CLASS0_SIZE; ++j) 239 adapt_probs(0, vp9_mv_fp_tree, ctx->comps[i].class0_fp[j], 240 pre_ctx->comps[i].class0_fp[j], cts->comps[i].class0_fp[j]); 241 242 adapt_probs(0, vp9_mv_fp_tree, ctx->comps[i].fp, pre_ctx->comps[i].fp, 243 cts->comps[i].fp); 244 245 if (allow_hp) { 246 ctx->comps[i].class0_hp = adapt_prob(pre_ctx->comps[i].class0_hp, 247 cts->comps[i].class0_hp); 248 ctx->comps[i].hp = adapt_prob(pre_ctx->comps[i].hp, cts->comps[i].hp); 249 } 250 } 251 } 252 253 void vp9_entropy_mv_init() { 254 vp9_tokens_from_tree(vp9_mv_joint_encodings, vp9_mv_joint_tree); 255 vp9_tokens_from_tree(vp9_mv_class_encodings, vp9_mv_class_tree); 256 vp9_tokens_from_tree(vp9_mv_class0_encodings, vp9_mv_class0_tree); 257 vp9_tokens_from_tree(vp9_mv_fp_encodings, vp9_mv_fp_tree); 258 } 259 260 void vp9_init_mv_probs(VP9_COMMON *cm) { 261 cm->fc.nmvc = default_nmv_context; 262 } 263