vp9_encodemv.c revision 5ae7ac49f08a179e4f054d99fcfc9dce78d26e58
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#include <math.h> 12 13#include "vp9/common/vp9_common.h" 14#include "vp9/common/vp9_entropymode.h" 15#include "vp9/common/vp9_systemdependent.h" 16#include "vp9/encoder/vp9_encodemv.h" 17 18 19#ifdef ENTROPY_STATS 20extern unsigned int active_section; 21#endif 22 23static void encode_mv_component(vp9_writer* w, int comp, 24 const nmv_component* mvcomp, int usehp) { 25 int offset; 26 const int sign = comp < 0; 27 const int mag = sign ? -comp : comp; 28 const int mv_class = vp9_get_mv_class(mag - 1, &offset); 29 const int d = offset >> 3; // int mv data 30 const int fr = (offset >> 1) & 3; // fractional mv data 31 const int hp = offset & 1; // high precision mv data 32 33 assert(comp != 0); 34 35 // Sign 36 vp9_write(w, sign, mvcomp->sign); 37 38 // Class 39 write_token(w, vp9_mv_class_tree, mvcomp->classes, 40 &vp9_mv_class_encodings[mv_class]); 41 42 // Integer bits 43 if (mv_class == MV_CLASS_0) { 44 write_token(w, vp9_mv_class0_tree, mvcomp->class0, 45 &vp9_mv_class0_encodings[d]); 46 } else { 47 int i; 48 const int n = mv_class + CLASS0_BITS - 1; // number of bits 49 for (i = 0; i < n; ++i) 50 vp9_write(w, (d >> i) & 1, mvcomp->bits[i]); 51 } 52 53 // Fractional bits 54 write_token(w, vp9_mv_fp_tree, 55 mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp, 56 &vp9_mv_fp_encodings[fr]); 57 58 // High precision bit 59 if (usehp) 60 vp9_write(w, hp, 61 mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp); 62} 63 64 65static void build_nmv_component_cost_table(int *mvcost, 66 const nmv_component* const mvcomp, 67 int usehp) { 68 int i, v; 69 int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE]; 70 int bits_cost[MV_OFFSET_BITS][2]; 71 int class0_fp_cost[CLASS0_SIZE][4], fp_cost[4]; 72 int class0_hp_cost[2], hp_cost[2]; 73 74 sign_cost[0] = vp9_cost_zero(mvcomp->sign); 75 sign_cost[1] = vp9_cost_one(mvcomp->sign); 76 vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree); 77 vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree); 78 for (i = 0; i < MV_OFFSET_BITS; ++i) { 79 bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]); 80 bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]); 81 } 82 83 for (i = 0; i < CLASS0_SIZE; ++i) 84 vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree); 85 vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree); 86 87 if (usehp) { 88 class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp); 89 class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp); 90 hp_cost[0] = vp9_cost_zero(mvcomp->hp); 91 hp_cost[1] = vp9_cost_one(mvcomp->hp); 92 } 93 mvcost[0] = 0; 94 for (v = 1; v <= MV_MAX; ++v) { 95 int z, c, o, d, e, f, cost = 0; 96 z = v - 1; 97 c = vp9_get_mv_class(z, &o); 98 cost += class_cost[c]; 99 d = (o >> 3); /* int mv data */ 100 f = (o >> 1) & 3; /* fractional pel mv data */ 101 e = (o & 1); /* high precision mv data */ 102 if (c == MV_CLASS_0) { 103 cost += class0_cost[d]; 104 } else { 105 int i, b; 106 b = c + CLASS0_BITS - 1; /* number of bits */ 107 for (i = 0; i < b; ++i) 108 cost += bits_cost[i][((d >> i) & 1)]; 109 } 110 if (c == MV_CLASS_0) { 111 cost += class0_fp_cost[d][f]; 112 } else { 113 cost += fp_cost[f]; 114 } 115 if (usehp) { 116 if (c == MV_CLASS_0) { 117 cost += class0_hp_cost[e]; 118 } else { 119 cost += hp_cost[e]; 120 } 121 } 122 mvcost[v] = cost + sign_cost[0]; 123 mvcost[-v] = cost + sign_cost[1]; 124 } 125} 126 127static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p, 128 vp9_prob upd_p) { 129 const vp9_prob new_p = get_binary_prob(ct[0], ct[1]); 130 vp9_prob mod_p = new_p | 1; 131 const int cur_b = cost_branch256(ct, *cur_p); 132 const int mod_b = cost_branch256(ct, mod_p); 133 const int cost = 7 * 256 + (vp9_cost_one(upd_p) - vp9_cost_zero(upd_p)); 134 if (cur_b - mod_b > cost) { 135 *cur_p = mod_p; 136 vp9_write(w, 1, upd_p); 137 vp9_write_literal(w, mod_p >> 1, 7); 138 return 1; 139 } else { 140 vp9_write(w, 0, upd_p); 141 return 0; 142 } 143} 144 145static void counts_to_nmv_context( 146 nmv_context_counts *nmv_count, 147 int usehp, 148 unsigned int (*branch_ct_joint)[2], 149 unsigned int (*branch_ct_sign)[2], 150 unsigned int (*branch_ct_classes)[MV_CLASSES - 1][2], 151 unsigned int (*branch_ct_class0)[CLASS0_SIZE - 1][2], 152 unsigned int (*branch_ct_bits)[MV_OFFSET_BITS][2], 153 unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][4 - 1][2], 154 unsigned int (*branch_ct_fp)[4 - 1][2], 155 unsigned int (*branch_ct_class0_hp)[2], 156 unsigned int (*branch_ct_hp)[2]) { 157 int i, j, k; 158 vp9_tree_probs_from_distribution(vp9_mv_joint_tree, 159 branch_ct_joint, 160 nmv_count->joints, 0); 161 for (i = 0; i < 2; ++i) { 162 const uint32_t s0 = nmv_count->comps[i].sign[0]; 163 const uint32_t s1 = nmv_count->comps[i].sign[1]; 164 165 branch_ct_sign[i][0] = s0; 166 branch_ct_sign[i][1] = s1; 167 vp9_tree_probs_from_distribution(vp9_mv_class_tree, 168 branch_ct_classes[i], 169 nmv_count->comps[i].classes, 0); 170 vp9_tree_probs_from_distribution(vp9_mv_class0_tree, 171 branch_ct_class0[i], 172 nmv_count->comps[i].class0, 0); 173 for (j = 0; j < MV_OFFSET_BITS; ++j) { 174 const uint32_t b0 = nmv_count->comps[i].bits[j][0]; 175 const uint32_t b1 = nmv_count->comps[i].bits[j][1]; 176 177 branch_ct_bits[i][j][0] = b0; 178 branch_ct_bits[i][j][1] = b1; 179 } 180 } 181 for (i = 0; i < 2; ++i) { 182 for (k = 0; k < CLASS0_SIZE; ++k) { 183 vp9_tree_probs_from_distribution(vp9_mv_fp_tree, 184 branch_ct_class0_fp[i][k], 185 nmv_count->comps[i].class0_fp[k], 0); 186 } 187 vp9_tree_probs_from_distribution(vp9_mv_fp_tree, 188 branch_ct_fp[i], 189 nmv_count->comps[i].fp, 0); 190 } 191 if (usehp) { 192 for (i = 0; i < 2; ++i) { 193 const uint32_t c0_hp0 = nmv_count->comps[i].class0_hp[0]; 194 const uint32_t c0_hp1 = nmv_count->comps[i].class0_hp[1]; 195 const uint32_t hp0 = nmv_count->comps[i].hp[0]; 196 const uint32_t hp1 = nmv_count->comps[i].hp[1]; 197 198 branch_ct_class0_hp[i][0] = c0_hp0; 199 branch_ct_class0_hp[i][1] = c0_hp1; 200 201 branch_ct_hp[i][0] = hp0; 202 branch_ct_hp[i][1] = hp1; 203 } 204 } 205} 206 207void vp9_write_nmv_probs(VP9_COMP* const cpi, int usehp, vp9_writer* const bc) { 208 int i, j; 209 unsigned int branch_ct_joint[MV_JOINTS - 1][2]; 210 unsigned int branch_ct_sign[2][2]; 211 unsigned int branch_ct_classes[2][MV_CLASSES - 1][2]; 212 unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2]; 213 unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2]; 214 unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2]; 215 unsigned int branch_ct_fp[2][4 - 1][2]; 216 unsigned int branch_ct_class0_hp[2][2]; 217 unsigned int branch_ct_hp[2][2]; 218 nmv_context *mvc = &cpi->common.fc.nmvc; 219 220 counts_to_nmv_context(&cpi->NMVcount, usehp, 221 branch_ct_joint, branch_ct_sign, branch_ct_classes, 222 branch_ct_class0, branch_ct_bits, 223 branch_ct_class0_fp, branch_ct_fp, 224 branch_ct_class0_hp, branch_ct_hp); 225 226 for (j = 0; j < MV_JOINTS - 1; ++j) 227 update_mv(bc, branch_ct_joint[j], &mvc->joints[j], NMV_UPDATE_PROB); 228 229 for (i = 0; i < 2; ++i) { 230 update_mv(bc, branch_ct_sign[i], &mvc->comps[i].sign, NMV_UPDATE_PROB); 231 for (j = 0; j < MV_CLASSES - 1; ++j) 232 update_mv(bc, branch_ct_classes[i][j], &mvc->comps[i].classes[j], 233 NMV_UPDATE_PROB); 234 235 for (j = 0; j < CLASS0_SIZE - 1; ++j) 236 update_mv(bc, branch_ct_class0[i][j], &mvc->comps[i].class0[j], 237 NMV_UPDATE_PROB); 238 239 for (j = 0; j < MV_OFFSET_BITS; ++j) 240 update_mv(bc, branch_ct_bits[i][j], &mvc->comps[i].bits[j], 241 NMV_UPDATE_PROB); 242 } 243 244 for (i = 0; i < 2; ++i) { 245 for (j = 0; j < CLASS0_SIZE; ++j) { 246 int k; 247 for (k = 0; k < 3; ++k) 248 update_mv(bc, branch_ct_class0_fp[i][j][k], 249 &mvc->comps[i].class0_fp[j][k], NMV_UPDATE_PROB); 250 } 251 252 for (j = 0; j < 3; ++j) 253 update_mv(bc, branch_ct_fp[i][j], &mvc->comps[i].fp[j], NMV_UPDATE_PROB); 254 } 255 256 if (usehp) { 257 for (i = 0; i < 2; ++i) { 258 update_mv(bc, branch_ct_class0_hp[i], &mvc->comps[i].class0_hp, 259 NMV_UPDATE_PROB); 260 update_mv(bc, branch_ct_hp[i], &mvc->comps[i].hp, 261 NMV_UPDATE_PROB); 262 } 263 } 264} 265 266void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w, 267 const MV* mv, const MV* ref, 268 const nmv_context* mvctx, int usehp) { 269 const MV diff = {mv->row - ref->row, 270 mv->col - ref->col}; 271 const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff); 272 usehp = usehp && vp9_use_mv_hp(ref); 273 274 write_token(w, vp9_mv_joint_tree, mvctx->joints, &vp9_mv_joint_encodings[j]); 275 if (mv_joint_vertical(j)) 276 encode_mv_component(w, diff.row, &mvctx->comps[0], usehp); 277 278 if (mv_joint_horizontal(j)) 279 encode_mv_component(w, diff.col, &mvctx->comps[1], usehp); 280 281 // If auto_mv_step_size is enabled then keep track of the largest 282 // motion vector component used. 283 if (!cpi->dummy_packing && cpi->sf.auto_mv_step_size) { 284 unsigned int maxv = MAX(abs(mv->row), abs(mv->col)) >> 3; 285 cpi->max_mv_magnitude = MAX(maxv, cpi->max_mv_magnitude); 286 } 287} 288 289void vp9_build_nmv_cost_table(int *mvjoint, 290 int *mvcost[2], 291 const nmv_context* const mvctx, 292 int usehp, 293 int mvc_flag_v, 294 int mvc_flag_h) { 295 vp9_clear_system_state(); 296 vp9_cost_tokens(mvjoint, mvctx->joints, vp9_mv_joint_tree); 297 if (mvc_flag_v) 298 build_nmv_component_cost_table(mvcost[0], &mvctx->comps[0], usehp); 299 if (mvc_flag_h) 300 build_nmv_component_cost_table(mvcost[1], &mvctx->comps[1], usehp); 301} 302 303static void inc_mvs(int_mv mv[2], int_mv ref[2], int is_compound, 304 nmv_context_counts *counts) { 305 int i; 306 for (i = 0; i < 1 + is_compound; ++i) { 307 const MV diff = { mv[i].as_mv.row - ref[i].as_mv.row, 308 mv[i].as_mv.col - ref[i].as_mv.col }; 309 vp9_inc_mv(&diff, counts); 310 } 311} 312 313void vp9_update_mv_count(VP9_COMP *cpi, MACROBLOCK *x, int_mv best_ref_mv[2]) { 314 MODE_INFO *mi = x->e_mbd.mi_8x8[0]; 315 MB_MODE_INFO *const mbmi = &mi->mbmi; 316 const int is_compound = has_second_ref(mbmi); 317 318 if (mbmi->sb_type < BLOCK_8X8) { 319 const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type]; 320 const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type]; 321 int idx, idy; 322 323 for (idy = 0; idy < 2; idy += num_4x4_h) { 324 for (idx = 0; idx < 2; idx += num_4x4_w) { 325 const int i = idy * 2 + idx; 326 if (mi->bmi[i].as_mode == NEWMV) 327 inc_mvs(mi->bmi[i].as_mv, best_ref_mv, is_compound, &cpi->NMVcount); 328 } 329 } 330 } else if (mbmi->mode == NEWMV) { 331 inc_mvs(mbmi->mv, best_ref_mv, is_compound, &cpi->NMVcount); 332 } 333} 334