1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp9/common/vp9_entropymv.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,247 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 + 1.15 +#include "vp9/common/vp9_onyxc_int.h" 1.16 +#include "vp9/common/vp9_entropymv.h" 1.17 + 1.18 +#define MV_COUNT_SAT 20 1.19 +#define MV_MAX_UPDATE_FACTOR 128 1.20 + 1.21 +/* Integer pel reference mv threshold for use of high-precision 1/8 mv */ 1.22 +#define COMPANDED_MVREF_THRESH 8 1.23 + 1.24 +const vp9_tree_index vp9_mv_joint_tree[TREE_SIZE(MV_JOINTS)] = { 1.25 + -MV_JOINT_ZERO, 2, 1.26 + -MV_JOINT_HNZVZ, 4, 1.27 + -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ 1.28 +}; 1.29 +struct vp9_token vp9_mv_joint_encodings[MV_JOINTS]; 1.30 + 1.31 +const vp9_tree_index vp9_mv_class_tree[TREE_SIZE(MV_CLASSES)] = { 1.32 + -MV_CLASS_0, 2, 1.33 + -MV_CLASS_1, 4, 1.34 + 6, 8, 1.35 + -MV_CLASS_2, -MV_CLASS_3, 1.36 + 10, 12, 1.37 + -MV_CLASS_4, -MV_CLASS_5, 1.38 + -MV_CLASS_6, 14, 1.39 + 16, 18, 1.40 + -MV_CLASS_7, -MV_CLASS_8, 1.41 + -MV_CLASS_9, -MV_CLASS_10, 1.42 +}; 1.43 +struct vp9_token vp9_mv_class_encodings[MV_CLASSES]; 1.44 + 1.45 +const vp9_tree_index vp9_mv_class0_tree[TREE_SIZE(CLASS0_SIZE)] = { 1.46 + -0, -1, 1.47 +}; 1.48 +struct vp9_token vp9_mv_class0_encodings[CLASS0_SIZE]; 1.49 + 1.50 +const vp9_tree_index vp9_mv_fp_tree[TREE_SIZE(4)] = { 1.51 + -0, 2, 1.52 + -1, 4, 1.53 + -2, -3 1.54 +}; 1.55 +struct vp9_token vp9_mv_fp_encodings[4]; 1.56 + 1.57 +static const nmv_context default_nmv_context = { 1.58 + {32, 64, 96}, 1.59 + { // NOLINT 1.60 + { /* vert component */ // NOLINT 1.61 + 128, /* sign */ 1.62 + {224, 144, 192, 168, 192, 176, 192, 198, 198, 245}, /* class */ 1.63 + {216}, /* class0 */ 1.64 + {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, /* bits */ 1.65 + {{128, 128, 64}, {96, 112, 64}}, /* class0_fp */ 1.66 + {64, 96, 64}, /* fp */ 1.67 + 160, /* class0_hp bit */ 1.68 + 128, /* hp */ 1.69 + }, 1.70 + { /* hor component */ // NOLINT 1.71 + 128, /* sign */ 1.72 + {216, 128, 176, 160, 176, 176, 192, 198, 198, 208}, /* class */ 1.73 + {208}, /* class0 */ 1.74 + {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}, /* bits */ 1.75 + {{128, 128, 64}, {96, 112, 64}}, /* class0_fp */ 1.76 + {64, 96, 64}, /* fp */ 1.77 + 160, /* class0_hp bit */ 1.78 + 128, /* hp */ 1.79 + } 1.80 + }, 1.81 +}; 1.82 + 1.83 +#define mv_class_base(c) ((c) ? (CLASS0_SIZE << (c + 2)) : 0) 1.84 + 1.85 +static const uint8_t log_in_base_2[] = { 1.86 + 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 1.87 + 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1.88 + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 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 + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10 1.129 +}; 1.130 + 1.131 +MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset) { 1.132 + MV_CLASS_TYPE c = MV_CLASS_0; 1.133 + if (z >= CLASS0_SIZE * 4096) 1.134 + c = MV_CLASS_10; 1.135 + else 1.136 + c = log_in_base_2[z >> 3]; 1.137 + 1.138 + if (offset) 1.139 + *offset = z - mv_class_base(c); 1.140 + return c; 1.141 +} 1.142 + 1.143 +int vp9_use_mv_hp(const MV *ref) { 1.144 + return (abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH && 1.145 + (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH; 1.146 +} 1.147 + 1.148 +int vp9_get_mv_mag(MV_CLASS_TYPE c, int offset) { 1.149 + return mv_class_base(c) + offset; 1.150 +} 1.151 + 1.152 +static void inc_mv_component(int v, nmv_component_counts *comp_counts, 1.153 + int incr, int usehp) { 1.154 + int s, z, c, o, d, e, f; 1.155 + assert(v != 0); /* should not be zero */ 1.156 + s = v < 0; 1.157 + comp_counts->sign[s] += incr; 1.158 + z = (s ? -v : v) - 1; /* magnitude - 1 */ 1.159 + 1.160 + c = vp9_get_mv_class(z, &o); 1.161 + comp_counts->classes[c] += incr; 1.162 + 1.163 + d = (o >> 3); /* int mv data */ 1.164 + f = (o >> 1) & 3; /* fractional pel mv data */ 1.165 + e = (o & 1); /* high precision mv data */ 1.166 + 1.167 + if (c == MV_CLASS_0) { 1.168 + comp_counts->class0[d] += incr; 1.169 + comp_counts->class0_fp[d][f] += incr; 1.170 + comp_counts->class0_hp[e] += usehp * incr; 1.171 + } else { 1.172 + int i; 1.173 + int b = c + CLASS0_BITS - 1; // number of bits 1.174 + for (i = 0; i < b; ++i) 1.175 + comp_counts->bits[i][((d >> i) & 1)] += incr; 1.176 + comp_counts->fp[f] += incr; 1.177 + comp_counts->hp[e] += usehp * incr; 1.178 + } 1.179 +} 1.180 + 1.181 +void vp9_inc_mv(const MV *mv, nmv_context_counts *counts) { 1.182 + if (counts != NULL) { 1.183 + const MV_JOINT_TYPE j = vp9_get_mv_joint(mv); 1.184 + ++counts->joints[j]; 1.185 + 1.186 + if (mv_joint_vertical(j)) { 1.187 + inc_mv_component(mv->row, &counts->comps[0], 1, 1); 1.188 + } 1.189 + 1.190 + if (mv_joint_horizontal(j)) { 1.191 + inc_mv_component(mv->col, &counts->comps[1], 1, 1); 1.192 + } 1.193 + } 1.194 +} 1.195 + 1.196 +static vp9_prob adapt_prob(vp9_prob prep, const unsigned int ct[2]) { 1.197 + return merge_probs(prep, ct, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR); 1.198 +} 1.199 + 1.200 +static void adapt_probs(const vp9_tree_index *tree, const vp9_prob *pre_probs, 1.201 + const unsigned int *counts, vp9_prob *probs) { 1.202 + tree_merge_probs(tree, pre_probs, counts, MV_COUNT_SAT, MV_MAX_UPDATE_FACTOR, 1.203 + probs); 1.204 +} 1.205 + 1.206 +void vp9_adapt_mv_probs(VP9_COMMON *cm, int allow_hp) { 1.207 + int i, j; 1.208 + 1.209 + nmv_context *fc = &cm->fc.nmvc; 1.210 + const nmv_context *pre_fc = &cm->frame_contexts[cm->frame_context_idx].nmvc; 1.211 + const nmv_context_counts *counts = &cm->counts.mv; 1.212 + 1.213 + adapt_probs(vp9_mv_joint_tree, pre_fc->joints, counts->joints, fc->joints); 1.214 + 1.215 + for (i = 0; i < 2; ++i) { 1.216 + nmv_component *comp = &fc->comps[i]; 1.217 + const nmv_component *pre_comp = &pre_fc->comps[i]; 1.218 + const nmv_component_counts *c = &counts->comps[i]; 1.219 + 1.220 + comp->sign = adapt_prob(pre_comp->sign, c->sign); 1.221 + adapt_probs(vp9_mv_class_tree, pre_comp->classes, c->classes, 1.222 + comp->classes); 1.223 + adapt_probs(vp9_mv_class0_tree, pre_comp->class0, c->class0, comp->class0); 1.224 + 1.225 + for (j = 0; j < MV_OFFSET_BITS; ++j) 1.226 + comp->bits[j] = adapt_prob(pre_comp->bits[j], c->bits[j]); 1.227 + 1.228 + for (j = 0; j < CLASS0_SIZE; ++j) 1.229 + adapt_probs(vp9_mv_fp_tree, pre_comp->class0_fp[j], c->class0_fp[j], 1.230 + comp->class0_fp[j]); 1.231 + 1.232 + adapt_probs(vp9_mv_fp_tree, pre_comp->fp, c->fp, comp->fp); 1.233 + 1.234 + if (allow_hp) { 1.235 + comp->class0_hp = adapt_prob(pre_comp->class0_hp, c->class0_hp); 1.236 + comp->hp = adapt_prob(pre_comp->hp, c->hp); 1.237 + } 1.238 + } 1.239 +} 1.240 + 1.241 +void vp9_entropy_mv_init() { 1.242 + vp9_tokens_from_tree(vp9_mv_joint_encodings, vp9_mv_joint_tree); 1.243 + vp9_tokens_from_tree(vp9_mv_class_encodings, vp9_mv_class_tree); 1.244 + vp9_tokens_from_tree(vp9_mv_class0_encodings, vp9_mv_class0_tree); 1.245 + vp9_tokens_from_tree(vp9_mv_fp_encodings, vp9_mv_fp_tree); 1.246 +} 1.247 + 1.248 +void vp9_init_mv_probs(VP9_COMMON *cm) { 1.249 + cm->fc.nmvc = default_nmv_context; 1.250 +}