vp9_entropymv.c revision f3bed9137f66ef693bd406e43b17e9a1114f1e14
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 21const 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}; 26struct vp9_token vp9_mv_joint_encodings[MV_JOINTS]; 27 28const 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}; 40struct vp9_token vp9_mv_class_encodings[MV_CLASSES]; 41 42const vp9_tree_index vp9_mv_class0_tree [2 * CLASS0_SIZE - 2] = { 43 -0, -1, 44}; 45struct vp9_token vp9_mv_class0_encodings[CLASS0_SIZE]; 46 47const vp9_tree_index vp9_mv_fp_tree [2 * 4 - 2] = { 48 -0, 2, 49 -1, 4, 50 -2, -3 51}; 52struct vp9_token vp9_mv_fp_encodings[4]; 53 54static 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 82MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) { 83 MV_CLASS_TYPE c = MV_CLASS_0; 84 if (z < CLASS0_SIZE * 8) c = MV_CLASS_0; 85 else if (z < CLASS0_SIZE * 16) c = MV_CLASS_1; 86 else if (z < CLASS0_SIZE * 32) c = MV_CLASS_2; 87 else if (z < CLASS0_SIZE * 64) c = MV_CLASS_3; 88 else if (z < CLASS0_SIZE * 128) c = MV_CLASS_4; 89 else if (z < CLASS0_SIZE * 256) c = MV_CLASS_5; 90 else if (z < CLASS0_SIZE * 512) c = MV_CLASS_6; 91 else if (z < CLASS0_SIZE * 1024) c = MV_CLASS_7; 92 else if (z < CLASS0_SIZE * 2048) c = MV_CLASS_8; 93 else if (z < CLASS0_SIZE * 4096) c = MV_CLASS_9; 94 else if (z < CLASS0_SIZE * 8192) c = MV_CLASS_10; 95 else assert(0); 96 if (offset) 97 *offset = z - mv_class_base(c); 98 return c; 99} 100 101int vp9_use_mv_hp(const MV *ref) { 102 return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH && 103 (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH; 104} 105 106int vp9_get_mv_mag(MV_CLASS_TYPE c, int offset) { 107 return mv_class_base(c) + offset; 108} 109 110static void inc_mv_component(int v, nmv_component_counts *comp_counts, 111 int incr, int usehp) { 112 int s, z, c, o, d, e, f; 113 if (!incr) 114 return; 115 assert (v != 0); /* should not be zero */ 116 s = v < 0; 117 comp_counts->sign[s] += incr; 118 z = (s ? -v : v) - 1; /* magnitude - 1 */ 119 120 c = vp9_get_mv_class(z, &o); 121 comp_counts->classes[c] += incr; 122 123 d = (o >> 3); /* int mv data */ 124 f = (o >> 1) & 3; /* fractional pel mv data */ 125 e = (o & 1); /* high precision mv data */ 126 if (c == MV_CLASS_0) { 127 comp_counts->class0[d] += incr; 128 } else { 129 int i; 130 int b = c + CLASS0_BITS - 1; // number of bits 131 for (i = 0; i < b; ++i) 132 comp_counts->bits[i][((d >> i) & 1)] += incr; 133 } 134 135 /* Code the fractional pel bits */ 136 if (c == MV_CLASS_0) { 137 comp_counts->class0_fp[d][f] += incr; 138 } else { 139 comp_counts->fp[f] += incr; 140 } 141 142 /* Code the high precision bit */ 143 if (usehp) { 144 if (c == MV_CLASS_0) { 145 comp_counts->class0_hp[e] += incr; 146 } else { 147 comp_counts->hp[e] += incr; 148 } 149 } 150} 151 152static void counts_to_context(nmv_component_counts *mvcomp, int usehp) { 153 int v; 154 vpx_memset(mvcomp->sign, 0, sizeof(nmv_component_counts) - sizeof(mvcomp->mvcount)); 155 for (v = 1; v <= MV_MAX; v++) { 156 inc_mv_component(-v, mvcomp, mvcomp->mvcount[MV_MAX - v], usehp); 157 inc_mv_component( v, mvcomp, mvcomp->mvcount[MV_MAX + v], usehp); 158 } 159} 160 161void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) { 162 const MV_JOINT_TYPE j = vp9_get_mv_joint(mv); 163 ++counts->joints[j]; 164 165 if (mv_joint_vertical(j)) 166 ++counts->comps[0].mvcount[MV_MAX + mv->row]; 167 168 if (mv_joint_horizontal(j)) 169 ++counts->comps[1].mvcount[MV_MAX + mv->col]; 170} 171 172static vp9_prob adapt_prob(vp9_prob prep, const unsigned int ct[2]) { 173 return merge_probs2(prep, ct, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR); 174} 175 176void vp9_counts_process(nmv_context_counts *nmv_count, int usehp) { 177 counts_to_context(&nmv_count->comps[0], usehp); 178 counts_to_context(&nmv_count->comps[1], usehp); 179} 180 181static unsigned int adapt_probs(unsigned int i, 182 vp9_tree tree, 183 vp9_prob this_probs[], 184 const vp9_prob last_probs[], 185 const unsigned int num_events[]) { 186 187 188 const unsigned int left = tree[i] <= 0 189 ? num_events[-tree[i]] 190 : adapt_probs(tree[i], tree, this_probs, last_probs, num_events); 191 192 const unsigned int right = tree[i + 1] <= 0 193 ? num_events[-tree[i + 1]] 194 : adapt_probs(tree[i + 1], tree, this_probs, last_probs, num_events); 195 const unsigned int ct[2] = { left, right }; 196 this_probs[i >> 1] = adapt_prob(last_probs[i >> 1], ct); 197 return left + right; 198} 199 200 201void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) { 202 int i, j; 203 204 FRAME_CONTEXT *pre_fc = &cm->frame_contexts[cm->frame_context_idx]; 205 206 nmv_context *ctx = &cm->fc.nmvc; 207 nmv_context *pre_ctx = &pre_fc->nmvc; 208 nmv_context_counts *cts = &cm->counts.mv; 209 210 vp9_counts_process(cts, allow_hp); 211 212 adapt_probs(0, vp9_mv_joint_tree, ctx->joints, pre_ctx->joints, cts->joints); 213 214 for (i = 0; i < 2; ++i) { 215 ctx->comps[i].sign = adapt_prob(pre_ctx->comps[i].sign, cts->comps[i].sign); 216 adapt_probs(0, vp9_mv_class_tree, ctx->comps[i].classes, 217 pre_ctx->comps[i].classes, cts->comps[i].classes); 218 adapt_probs(0, vp9_mv_class0_tree, ctx->comps[i].class0, 219 pre_ctx->comps[i].class0, cts->comps[i].class0); 220 221 for (j = 0; j < MV_OFFSET_BITS; ++j) 222 ctx->comps[i].bits[j] = adapt_prob(pre_ctx->comps[i].bits[j], 223 cts->comps[i].bits[j]); 224 225 for (j = 0; j < CLASS0_SIZE; ++j) 226 adapt_probs(0, vp9_mv_fp_tree, ctx->comps[i].class0_fp[j], 227 pre_ctx->comps[i].class0_fp[j], cts->comps[i].class0_fp[j]); 228 229 adapt_probs(0, vp9_mv_fp_tree, ctx->comps[i].fp, pre_ctx->comps[i].fp, 230 cts->comps[i].fp); 231 232 if (allow_hp) { 233 ctx->comps[i].class0_hp = adapt_prob(pre_ctx->comps[i].class0_hp, 234 cts->comps[i].class0_hp); 235 ctx->comps[i].hp = adapt_prob(pre_ctx->comps[i].hp, cts->comps[i].hp); 236 } 237 } 238} 239 240void vp9_entropy_mv_init() { 241 vp9_tokens_from_tree(vp9_mv_joint_encodings, vp9_mv_joint_tree); 242 vp9_tokens_from_tree(vp9_mv_class_encodings, vp9_mv_class_tree); 243 vp9_tokens_from_tree(vp9_mv_class0_encodings, vp9_mv_class0_tree); 244 vp9_tokens_from_tree(vp9_mv_fp_encodings, vp9_mv_fp_tree); 245} 246 247void vp9_init_mv_probs(VP9_COMMON *cm) { 248 cm->fc.nmvc = default_nmv_context; 249} 250