encodemv.c 31.6 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10 11
 */


John Koleszar's avatar
John Koleszar committed
12
#include "vp8/common/common.h"
John Koleszar's avatar
John Koleszar committed
13
#include "encodemv.h"
John Koleszar's avatar
John Koleszar committed
14 15
#include "vp8/common/entropymode.h"
#include "vp8/common/systemdependent.h"
John Koleszar's avatar
John Koleszar committed
16 17 18 19 20 21 22

#include <math.h>

#ifdef ENTROPY_STATS
extern unsigned int active_section;
#endif

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
//extern int final_packing;

#if CONFIG_NEWMVENTROPY

#ifdef NMV_STATS
nmv_context_counts tnmvcounts;
#endif

static void encode_nmv_component(vp8_writer *w,
                                 int v,
                                 int r,
                                 const nmv_component *mvcomp) {
  int s, z, c, o, d;
  assert (v != 0);            /* should not be zero */
  s = v < 0;
  vp8_write(w, s, mvcomp->sign);
  z = (s ? -v : v) - 1;       /* magnitude - 1 */

  c = vp8_get_mv_class(z, &o);

  vp8_write_token(w, vp8_mv_class_tree, mvcomp->classes,
                  vp8_mv_class_encodings + c);

  d = (o >> 3);               /* int mv data */

  if (c == MV_CLASS_0) {
    vp8_write_token(w, vp8_mv_class0_tree, mvcomp->class0,
                    vp8_mv_class0_encodings + d);
  } else {
    int i, b;
    b = c + CLASS0_BITS - 1;  /* number of bits */
    for (i = 0; i < b; ++i)
      vp8_write(w, ((d >> i) & 1), mvcomp->bits[i]);
  }
}

static void encode_nmv_component_fp(vp8_writer *w,
                                    int v,
                                    int r,
                                    const nmv_component *mvcomp,
                                    int usehp) {
  int s, z, c, o, d, f, e;
  assert (v != 0);            /* should not be zero */
  s = v < 0;
  z = (s ? -v : v) - 1;       /* magnitude - 1 */

  c = vp8_get_mv_class(z, &o);

  d = (o >> 3);               /* int mv data */
  f = (o >> 1) & 3;           /* fractional pel mv data */
  e = (o & 1);                /* high precision mv data */

  /* Code the fractional pel bits */
  if (c == MV_CLASS_0) {
    vp8_write_token(w, vp8_mv_fp_tree, mvcomp->class0_fp[d],
                    vp8_mv_fp_encodings + f);
  } else {
    vp8_write_token(w, vp8_mv_fp_tree, mvcomp->fp,
                    vp8_mv_fp_encodings + f);
  }
  /* Code the high precision bit */
  if (usehp) {
    if (c == MV_CLASS_0) {
      vp8_write(w, e, mvcomp->class0_hp);
    } else {
      vp8_write(w, e, mvcomp->hp);
    }
  }
}

static void build_nmv_component_cost_table(int *mvcost,
                                           const nmv_component *mvcomp,
                                           int usehp) {
  int i, v;
  int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
  int bits_cost[MV_OFFSET_BITS][2];
  int class0_fp_cost[CLASS0_SIZE][4], fp_cost[4];
  int class0_hp_cost[2], hp_cost[2];

  sign_cost[0] = vp8_cost_zero(mvcomp->sign);
  sign_cost[1] = vp8_cost_one(mvcomp->sign);
  vp8_cost_tokens(class_cost, mvcomp->classes, vp8_mv_class_tree);
  vp8_cost_tokens(class0_cost, mvcomp->class0, vp8_mv_class0_tree);
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
    bits_cost[i][0] = vp8_cost_zero(mvcomp->bits[i]);
    bits_cost[i][1] = vp8_cost_one(mvcomp->bits[i]);
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
    vp8_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp8_mv_fp_tree);
  vp8_cost_tokens(fp_cost, mvcomp->fp, vp8_mv_fp_tree);

  if (usehp) {
    class0_hp_cost[0] = vp8_cost_zero(mvcomp->class0_hp);
    class0_hp_cost[1] = vp8_cost_one(mvcomp->class0_hp);
    hp_cost[0] = vp8_cost_zero(mvcomp->hp);
    hp_cost[1] = vp8_cost_one(mvcomp->hp);
  }
  mvcost[0] = 0;
  for (v = 1; v <= MV_MAX; ++v) {
    int z, c, o, d, e, f, cost = 0;
    z = v - 1;
    c = vp8_get_mv_class(z, &o);
    cost += class_cost[c];
    d = (o >> 3);               /* int mv data */
    f = (o >> 1) & 3;           /* fractional pel mv data */
    e = (o & 1);                /* high precision mv data */
    if (c == MV_CLASS_0) {
      cost += class0_cost[d];
    } else {
      int i, b;
      b = c + CLASS0_BITS - 1;  /* number of bits */
      for (i = 0; i < b; ++i)
        cost += bits_cost[i][((d >> i) & 1)];
    }
    if (c == MV_CLASS_0) {
      cost += class0_fp_cost[d][f];
    } else {
      cost += fp_cost[f];
    }
    if (usehp) {
      if (c == MV_CLASS_0) {
        cost += class0_hp_cost[e];
      } else {
        cost += hp_cost[e];
      }
    }
    mvcost[v] = cost + sign_cost[0];
    mvcost[-v] = cost + sign_cost[1];
  }
}

static int update_nmv_savings(const unsigned int ct[2],
                              const vp8_prob cur_p,
                              const vp8_prob new_p,
                              const vp8_prob upd_p) {

#ifdef LOW_PRECISION_MV_UPDATE
  vp8_prob mod_p = new_p | 1;
#else
  vp8_prob mod_p = new_p;
#endif
  const int cur_b = vp8_cost_branch256(ct, cur_p);
  const int mod_b = vp8_cost_branch256(ct, mod_p);
  const int cost = 7 * 256 +
#ifndef LOW_PRECISION_MV_UPDATE
      256 +
#endif
      (vp8_cost_one(upd_p) - vp8_cost_zero(upd_p));
  if (cur_b - mod_b - cost > 0) {
    return cur_b - mod_b - cost;
  } else {
    return -vp8_cost_zero(upd_p);
  }
}

static int update_nmv(
  vp8_writer *const w,
  const unsigned int ct[2],
  vp8_prob *const cur_p,
  const vp8_prob new_p,
  const vp8_prob upd_p) {

#ifdef LOW_PRECISION_MV_UPDATE
  vp8_prob mod_p = new_p | 1;
#else
  vp8_prob mod_p = new_p;
#endif

  const int cur_b = vp8_cost_branch256(ct, *cur_p);
  const int mod_b = vp8_cost_branch256(ct, mod_p);
  const int cost = 7 * 256 +
#ifndef LOW_PRECISION_MV_UPDATE
      256 +
#endif
      (vp8_cost_one(upd_p) - vp8_cost_zero(upd_p));

  if (cur_b - mod_b > cost) {
    *cur_p = mod_p;
    vp8_write(w, 1, upd_p);
#ifdef LOW_PRECISION_MV_UPDATE
    vp8_write_literal(w, mod_p >> 1, 7);
#else
    vp8_write_literal(w, mod_p, 8);
#endif
    return 1;
  } else {
    vp8_write(w, 0, upd_p);
    return 0;
  }
}

#ifdef NMV_STATS
void init_nmvstats() {
  vp8_zero(tnmvcounts);
}

void print_nmvstats() {
  nmv_context prob;
  unsigned int branch_ct_joint[MV_JOINTS - 1][2];
  unsigned int branch_ct_sign[2][2];
  unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
  unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
  unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
  unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
  unsigned int branch_ct_fp[2][4 - 1][2];
  unsigned int branch_ct_class0_hp[2][2];
  unsigned int branch_ct_hp[2][2];
  int i, j, k;
  vp8_counts_to_nmv_context(&tnmvcounts, &prob, 1,
                            branch_ct_joint, branch_ct_sign, branch_ct_classes,
                            branch_ct_class0, branch_ct_bits,
                            branch_ct_class0_fp, branch_ct_fp,
                            branch_ct_class0_hp, branch_ct_hp);

  printf("\nCounts =\n  { ");
  for (j = 0; j < MV_JOINTS; ++j)
    printf("%d, ", tnmvcounts.joints[j]);
  printf("},\n");
  for (i=0; i< 2; ++i) {
    printf("  {\n");
    printf("    %d/%d,\n", tnmvcounts.comps[i].sign[0],
                           tnmvcounts.comps[i].sign[1]);
    printf("    { ");
    for (j = 0; j < MV_CLASSES; ++j)
      printf("%d, ", tnmvcounts.comps[i].classes[j]);
    printf("},\n");
    printf("    { ");
    for (j = 0; j < CLASS0_SIZE; ++j)
      printf("%d, ", tnmvcounts.comps[i].class0[j]);
    printf("},\n");
    printf("    { ");
    for (j = 0; j < MV_OFFSET_BITS; ++j)
      printf("%d/%d, ", tnmvcounts.comps[i].bits[j][0],
                        tnmvcounts.comps[i].bits[j][1]);
    printf("},\n");

    printf("    {");
    for (j = 0; j < CLASS0_SIZE; ++j) {
      printf("{");
      for (k = 0; k < 4; ++k)
        printf("%d, ", tnmvcounts.comps[i].class0_fp[j][k]);
      printf("}, ");
    }
    printf("},\n");

    printf("    { ");
    for (j = 0; j < 4; ++j)
      printf("%d, ", tnmvcounts.comps[i].fp[j]);
    printf("},\n");

    printf("    %d/%d,\n",
           tnmvcounts.comps[i].class0_hp[0],
           tnmvcounts.comps[i].class0_hp[1]);
    printf("    %d/%d,\n",
           tnmvcounts.comps[i].hp[0],
           tnmvcounts.comps[i].hp[1]);
    printf("  },\n");
  }

  printf("\nProbs =\n  { ");
  for (j = 0; j < MV_JOINTS - 1; ++j)
    printf("%d, ", prob.joints[j]);
  printf("},\n");
  for (i=0; i< 2; ++i) {
    printf("  {\n");
    printf("    %d,\n", prob.comps[i].sign);
    printf("    { ");
    for (j = 0; j < MV_CLASSES - 1; ++j)
      printf("%d, ", prob.comps[i].classes[j]);
    printf("},\n");
    printf("    { ");
    for (j = 0; j < CLASS0_SIZE - 1; ++j)
      printf("%d, ", prob.comps[i].class0[j]);
    printf("},\n");
    printf("    { ");
    for (j = 0; j < MV_OFFSET_BITS; ++j)
      printf("%d, ", prob.comps[i].bits[j]);
    printf("},\n");
    printf("    { ");
    for (j = 0; j < CLASS0_SIZE; ++j) {
      printf("{");
      for (k = 0; k < 3; ++k)
        printf("%d, ", prob.comps[i].class0_fp[j][k]);
      printf("}, ");
    }
    printf("},\n");
    printf("    { ");
    for (j = 0; j < 3; ++j)
      printf("%d, ", prob.comps[i].fp[j]);
    printf("},\n");

    printf("    %d,\n", prob.comps[i].class0_hp);
    printf("    %d,\n", prob.comps[i].hp);
    printf("  },\n");
  }
}

static void add_nmvcount(nmv_context_counts *dst, nmv_context_counts *src) {
  int i, j, k;
  for (j = 0; j < MV_JOINTS; ++j) {
    dst->joints[j] += src->joints[j];
  }
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < MV_VALS; ++j) {
      dst->comps[i].mvcount[j] += src->comps[i].mvcount[j];
    }
    dst->comps[i].sign[0] += src->comps[i].sign[0];
    dst->comps[i].sign[1] += src->comps[i].sign[1];
    for (j = 0; j < MV_CLASSES; ++j) {
      dst->comps[i].classes[j] += src->comps[i].classes[j];
    }
    for (j = 0; j < CLASS0_SIZE; ++j) {
      dst->comps[i].class0[j] += src->comps[i].class0[j];
    }
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
      dst->comps[i].bits[j][0] += src->comps[i].bits[j][0];
      dst->comps[i].bits[j][1] += src->comps[i].bits[j][1];
    }
  }
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j) {
      for (k = 0; k < 4; ++k) {
        dst->comps[i].class0_fp[j][k] += src->comps[i].class0_fp[j][k];
      }
    }
    for (j = 0; j < 4; ++j) {
      dst->comps[i].fp[j] += src->comps[i].fp[j];
    }
    dst->comps[i].class0_hp[0] += src->comps[i].class0_hp[0];
    dst->comps[i].class0_hp[1] += src->comps[i].class0_hp[1];
    dst->comps[i].hp[0] += src->comps[i].hp[0];
    dst->comps[i].hp[1] += src->comps[i].hp[1];
  }
}
#endif

void vp8_write_nmvprobs(VP8_COMP * cpi, int usehp) {
  vp8_writer *const w  = & cpi->bc;
  int i, j;
  nmv_context prob;
  unsigned int branch_ct_joint[MV_JOINTS - 1][2];
  unsigned int branch_ct_sign[2][2];
  unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
  unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
  unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
  unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
  unsigned int branch_ct_fp[2][4 - 1][2];
  unsigned int branch_ct_class0_hp[2][2];
  unsigned int branch_ct_hp[2][2];
  int savings = 0;

#ifdef NMV_STATS
  if (!cpi->dummy_packing)
    add_nmvcount(&tnmvcounts, &cpi->NMVcount);
#endif
  vp8_counts_to_nmv_context(&cpi->NMVcount, &prob, usehp,
                            branch_ct_joint, branch_ct_sign, branch_ct_classes,
                            branch_ct_class0, branch_ct_bits,
                            branch_ct_class0_fp, branch_ct_fp,
                            branch_ct_class0_hp, branch_ct_hp);
  /* write updates if they help */
#ifdef MV_GROUP_UPDATE
  for (j = 0; j < MV_JOINTS - 1; ++j) {
    savings += update_nmv_savings(branch_ct_joint[j],
                                  cpi->common.fc.nmvc.joints[j],
                                  prob.joints[j],
                                  VP8_NMV_UPDATE_PROB);
  }
  for (i = 0; i < 2; ++i) {
    savings += update_nmv_savings(branch_ct_sign[i],
                                  cpi->common.fc.nmvc.comps[i].sign,
                                  prob.comps[i].sign,
                                  VP8_NMV_UPDATE_PROB);
    for (j = 0; j < MV_CLASSES - 1; ++j) {
      savings += update_nmv_savings(branch_ct_classes[i][j],
                                    cpi->common.fc.nmvc.comps[i].classes[j],
                                    prob.comps[i].classes[j],
                                    VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < CLASS0_SIZE - 1; ++j) {
      savings += update_nmv_savings(branch_ct_class0[i][j],
                                    cpi->common.fc.nmvc.comps[i].class0[j],
                                    prob.comps[i].class0[j],
                                    VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
      savings += update_nmv_savings(branch_ct_bits[i][j],
                                    cpi->common.fc.nmvc.comps[i].bits[j],
                                    prob.comps[i].bits[j],
                                    VP8_NMV_UPDATE_PROB);
    }
  }
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j) {
      int k;
      for (k = 0; k < 3; ++k) {
        savings += update_nmv_savings(branch_ct_class0_fp[i][j][k],
                                      cpi->common.fc.nmvc.comps[i].class0_fp[j][k],
                                      prob.comps[i].class0_fp[j][k],
                                      VP8_NMV_UPDATE_PROB);
      }
    }
    for (j = 0; j < 3; ++j) {
      savings += update_nmv_savings(branch_ct_fp[i][j],
                                    cpi->common.fc.nmvc.comps[i].fp[j],
                                    prob.comps[i].fp[j],
                                    VP8_NMV_UPDATE_PROB);
    }
  }
  if (usehp) {
    for (i = 0; i < 2; ++i) {
      savings += update_nmv_savings(branch_ct_class0_hp[i],
                                    cpi->common.fc.nmvc.comps[i].class0_hp,
                                    prob.comps[i].class0_hp,
                                    VP8_NMV_UPDATE_PROB);
      savings += update_nmv_savings(branch_ct_hp[i],
                                    cpi->common.fc.nmvc.comps[i].hp,
                                    prob.comps[i].hp,
                                    VP8_NMV_UPDATE_PROB);
    }
  }
  if (savings <= 0) {
    vp8_write_bit(w, 0);
    return;
  }
  vp8_write_bit(w, 1);
#endif

  for (j = 0; j < MV_JOINTS - 1; ++j) {
    update_nmv(w, branch_ct_joint[j],
               &cpi->common.fc.nmvc.joints[j],
               prob.joints[j],
               VP8_NMV_UPDATE_PROB);
  }
  for (i = 0; i < 2; ++i) {
    update_nmv(w, branch_ct_sign[i],
               &cpi->common.fc.nmvc.comps[i].sign,
               prob.comps[i].sign,
               VP8_NMV_UPDATE_PROB);
    for (j = 0; j < MV_CLASSES - 1; ++j) {
      update_nmv(w, branch_ct_classes[i][j],
                 &cpi->common.fc.nmvc.comps[i].classes[j],
                 prob.comps[i].classes[j],
                 VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < CLASS0_SIZE - 1; ++j) {
      update_nmv(w, branch_ct_class0[i][j],
                 &cpi->common.fc.nmvc.comps[i].class0[j],
                 prob.comps[i].class0[j],
                 VP8_NMV_UPDATE_PROB);
    }
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
      update_nmv(w, branch_ct_bits[i][j],
                 &cpi->common.fc.nmvc.comps[i].bits[j],
                 prob.comps[i].bits[j],
                 VP8_NMV_UPDATE_PROB);
    }
  }
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j) {
      int k;
      for (k = 0; k < 3; ++k) {
        update_nmv(w, branch_ct_class0_fp[i][j][k],
                   &cpi->common.fc.nmvc.comps[i].class0_fp[j][k],
                   prob.comps[i].class0_fp[j][k],
                   VP8_NMV_UPDATE_PROB);
      }
    }
    for (j = 0; j < 3; ++j) {
      update_nmv(w, branch_ct_fp[i][j],
                 &cpi->common.fc.nmvc.comps[i].fp[j],
                 prob.comps[i].fp[j],
                 VP8_NMV_UPDATE_PROB);
    }
  }
  if (usehp) {
    for (i = 0; i < 2; ++i) {
      update_nmv(w, branch_ct_class0_hp[i],
                 &cpi->common.fc.nmvc.comps[i].class0_hp,
                 prob.comps[i].class0_hp,
                 VP8_NMV_UPDATE_PROB);
      update_nmv(w, branch_ct_hp[i],
                 &cpi->common.fc.nmvc.comps[i].hp,
                 prob.comps[i].hp,
                 VP8_NMV_UPDATE_PROB);
    }
  }
}

void vp8_encode_nmv(vp8_writer *w, const MV *mv, const MV *ref,
                    const nmv_context *mvctx) {
  MV_JOINT_TYPE j = vp8_get_mv_joint(*mv);
  vp8_write_token(w, vp8_mv_joint_tree, mvctx->joints,
                  vp8_mv_joint_encodings + j);
  if (j == MV_JOINT_HZVNZ || j == MV_JOINT_HNZVNZ) {
    encode_nmv_component(w, mv->row, ref->col, &mvctx->comps[0]);
  }
  if (j == MV_JOINT_HNZVZ || j == MV_JOINT_HNZVNZ) {
    encode_nmv_component(w, mv->col, ref->col, &mvctx->comps[1]);
  }
}

void vp8_encode_nmv_fp(vp8_writer *w, const MV *mv, const MV *ref,
                       const nmv_context *mvctx, int usehp) {
  MV_JOINT_TYPE j = vp8_get_mv_joint(*mv);
529
  usehp = usehp && vp8_use_nmv_hp(ref);
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
  if (j == MV_JOINT_HZVNZ || j == MV_JOINT_HNZVNZ) {
    encode_nmv_component_fp(w, mv->row, ref->row, &mvctx->comps[0], usehp);
  }
  if (j == MV_JOINT_HNZVZ || j == MV_JOINT_HNZVNZ) {
    encode_nmv_component_fp(w, mv->col, ref->col, &mvctx->comps[1], usehp);
  }
}

void vp8_build_nmv_cost_table(int *mvjoint,
                              int *mvcost[2],
                              const nmv_context *mvctx,
                              int usehp,
                              int mvc_flag_v,
                              int mvc_flag_h) {
  vp8_clear_system_state();
  vp8_cost_tokens(mvjoint, mvctx->joints, vp8_mv_joint_tree);
  if (mvc_flag_v)
    build_nmv_component_cost_table(mvcost[0], &mvctx->comps[0], usehp);
  if (mvc_flag_h)
    build_nmv_component_cost_table(mvcost[1], &mvctx->comps[1], usehp);
}

#else  /* CONFIG_NEWMVENTROPY */

John Koleszar's avatar
John Koleszar committed
554
static void encode_mvcomponent(
John Koleszar's avatar
John Koleszar committed
555 556 557 558 559 560
  vp8_writer *const w,
  const int v,
  const struct mv_context *mvc
) {
  const vp8_prob *p = mvc->prob;
  const int x = v < 0 ? -v : v;
John Koleszar's avatar
John Koleszar committed
561

John Koleszar's avatar
John Koleszar committed
562 563 564 565 566 567 568
  if (x < mvnum_short) {   // Small
    vp8_write(w, 0, p [mvpis_short]);
    vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, mvnum_short_bits);
    if (!x)
      return;         // no sign bit
  } else {                // Large
    int i = 0;
John Koleszar's avatar
John Koleszar committed
569

John Koleszar's avatar
John Koleszar committed
570
    vp8_write(w, 1, p [mvpis_short]);
John Koleszar's avatar
John Koleszar committed
571

John Koleszar's avatar
John Koleszar committed
572 573
    do
      vp8_write(w, (x >> i) & 1, p [MVPbits + i]);
John Koleszar's avatar
John Koleszar committed
574

John Koleszar's avatar
John Koleszar committed
575
    while (++i < mvnum_short_bits);
John Koleszar's avatar
John Koleszar committed
576

John Koleszar's avatar
John Koleszar committed
577
    i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
John Koleszar's avatar
John Koleszar committed
578

John Koleszar's avatar
John Koleszar committed
579 580
    do
      vp8_write(w, (x >> i) & 1, p [MVPbits + i]);
John Koleszar's avatar
John Koleszar committed
581

John Koleszar's avatar
John Koleszar committed
582 583 584 585 586
    while (--i > mvnum_short_bits);

    if (x & ~((2 << mvnum_short_bits) - 1))
      vp8_write(w, (x >> mvnum_short_bits) & 1, p [MVPbits + mvnum_short_bits]);
  }
John Koleszar's avatar
John Koleszar committed
587

John Koleszar's avatar
John Koleszar committed
588
  vp8_write(w, v < 0, p [MVPsign]);
John Koleszar's avatar
John Koleszar committed
589 590
}

591
void vp8_encode_motion_vector(vp8_writer *w, const MV *mv, const MV_CONTEXT *mvc) {
John Koleszar's avatar
John Koleszar committed
592 593
  encode_mvcomponent(w, mv->row >> 1, &mvc[0]);
  encode_mvcomponent(w, mv->col >> 1, &mvc[1]);
John Koleszar's avatar
John Koleszar committed
594 595 596
}


John Koleszar's avatar
John Koleszar committed
597 598 599 600
static unsigned int cost_mvcomponent(const int v, const struct mv_context *mvc) {
  const vp8_prob *p = mvc->prob;
  const int x = v;   // v<0? -v:v;
  unsigned int cost;
John Koleszar's avatar
John Koleszar committed
601

John Koleszar's avatar
John Koleszar committed
602 603 604
  if (x < mvnum_short) {
    cost = vp8_cost_zero(p [mvpis_short])
           + vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, mvnum_short_bits);
John Koleszar's avatar
John Koleszar committed
605

John Koleszar's avatar
John Koleszar committed
606 607 608 609 610
    if (!x)
      return cost;
  } else {
    int i = 0;
    cost = vp8_cost_one(p [mvpis_short]);
John Koleszar's avatar
John Koleszar committed
611

John Koleszar's avatar
John Koleszar committed
612 613
    do
      cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1);
John Koleszar's avatar
John Koleszar committed
614

John Koleszar's avatar
John Koleszar committed
615
    while (++i < mvnum_short_bits);
John Koleszar's avatar
John Koleszar committed
616

John Koleszar's avatar
John Koleszar committed
617
    i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
John Koleszar's avatar
John Koleszar committed
618

John Koleszar's avatar
John Koleszar committed
619 620
    do
      cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1);
John Koleszar's avatar
John Koleszar committed
621

John Koleszar's avatar
John Koleszar committed
622
    while (--i > mvnum_short_bits);
John Koleszar's avatar
John Koleszar committed
623

John Koleszar's avatar
John Koleszar committed
624 625 626
    if (x & ~((2 << mvnum_short_bits) - 1))
      cost += vp8_cost_bit(p [MVPbits + mvnum_short_bits], (x >> mvnum_short_bits) & 1);
  }
John Koleszar's avatar
John Koleszar committed
627

John Koleszar's avatar
John Koleszar committed
628
  return cost;   // + vp8_cost_bit( p [MVPsign], v < 0);
John Koleszar's avatar
John Koleszar committed
629 630
}

John Koleszar's avatar
John Koleszar committed
631 632 633 634
void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc, int mvc_flag[2]) {
  int i = 1;   // -mv_max;
  unsigned int cost0 = 0;
  unsigned int cost1 = 0;
John Koleszar's avatar
John Koleszar committed
635

John Koleszar's avatar
John Koleszar committed
636
  vp8_clear_system_state();
John Koleszar's avatar
John Koleszar committed
637

John Koleszar's avatar
John Koleszar committed
638
  i = 1;
John Koleszar's avatar
John Koleszar committed
639

John Koleszar's avatar
John Koleszar committed
640 641
  if (mvc_flag[0]) {
    mvcost [0] [0] = cost_mvcomponent(0, &mvc[0]);
John Koleszar's avatar
John Koleszar committed
642

John Koleszar's avatar
John Koleszar committed
643 644 645
    do {
      // mvcost [0] [i] = cost_mvcomponent( i, &mvc[0]);
      cost0 = cost_mvcomponent(i, &mvc[0]);
John Koleszar's avatar
John Koleszar committed
646

John Koleszar's avatar
John Koleszar committed
647 648 649 650
      mvcost [0] [i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]);
      mvcost [0] [-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]);
    } while (++i <= mv_max);
  }
John Koleszar's avatar
John Koleszar committed
651

John Koleszar's avatar
John Koleszar committed
652
  i = 1;
John Koleszar's avatar
John Koleszar committed
653

John Koleszar's avatar
John Koleszar committed
654 655 656 657 658 659 660 661 662 663 664
  if (mvc_flag[1]) {
    mvcost [1] [0] = cost_mvcomponent(0, &mvc[1]);

    do {
      // mvcost [1] [i] = cost_mvcomponent( i, mvc[1]);
      cost1 = cost_mvcomponent(i, &mvc[1]);

      mvcost [1] [i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]);
      mvcost [1] [-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]);
    } while (++i <= mv_max);
  }
John Koleszar's avatar
John Koleszar committed
665 666 667 668 669 670 671 672 673 674
}


// Motion vector probability table update depends on benefit.
// Small correction allows for the fact that an update to an MV probability
// may have benefit in subsequent frames as well as the current one.

#define MV_PROB_UPDATE_CORRECTION   -1


John Koleszar's avatar
John Koleszar committed
675 676
__inline static void calc_prob(vp8_prob *p, const unsigned int ct[2]) {
  const unsigned int tot = ct[0] + ct[1];
John Koleszar's avatar
John Koleszar committed
677

John Koleszar's avatar
John Koleszar committed
678 679 680 681
  if (tot) {
    const vp8_prob x = ((ct[0] * 255) / tot) & -2;
    *p = x ? x : 1;
  }
John Koleszar's avatar
John Koleszar committed
682 683 684
}

static void update(
John Koleszar's avatar
John Koleszar committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
  vp8_writer *const w,
  const unsigned int ct[2],
  vp8_prob *const cur_p,
  const vp8_prob new_p,
  const vp8_prob update_p,
  int *updated
) {
  const int cur_b = vp8_cost_branch(ct, *cur_p);
  const int new_b = vp8_cost_branch(ct, new_p);
  const int cost = 7 + MV_PROB_UPDATE_CORRECTION + ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8);

  if (cur_b - new_b > cost) {
    *cur_p = new_p;
    vp8_write(w, 1, update_p);
    vp8_write_literal(w, new_p >> 1, 7);
    *updated = 1;

  } else
    vp8_write(w, 0, update_p);
John Koleszar's avatar
John Koleszar committed
704 705 706
}

static void write_component_probs(
John Koleszar's avatar
John Koleszar committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  vp8_writer *const w,
  struct mv_context *cur_mvc,
  const struct mv_context *default_mvc_,
  const struct mv_context *update_mvc,
  const unsigned int events [MVvals],
  unsigned int rc,
  int *updated
) {
  vp8_prob *Pcur = cur_mvc->prob;
  const vp8_prob *default_mvc = default_mvc_->prob;
  const vp8_prob *Pupdate = update_mvc->prob;
  unsigned int is_short_ct[2], sign_ct[2];

  unsigned int bit_ct [mvlong_width] [2];

  unsigned int short_ct  [mvnum_short];
  unsigned int short_bct [mvnum_short - 1] [2];

  vp8_prob Pnew [MVPcount];

  (void) rc;
  vp8_copy_array(Pnew, default_mvc, MVPcount);

  vp8_zero(is_short_ct)
  vp8_zero(sign_ct)
  vp8_zero(bit_ct)
  vp8_zero(short_ct)
  vp8_zero(short_bct)


  // j=0
  {
    const int c = events [mv_max];

    is_short_ct [0] += c;    // Short vector
    short_ct [0] += c;       // Magnitude distribution
  }

  // j: 1 ~ mv_max (1023)
  {
    int j = 1;

    do {
      const int c1 = events [mv_max + j];  // positive
      const int c2 = events [mv_max - j];  // negative
      const int c  = c1 + c2;
      int a = j;

      sign_ct [0] += c1;
      sign_ct [1] += c2;

      if (a < mvnum_short) {
        is_short_ct [0] += c;     // Short vector
        short_ct [a] += c;       // Magnitude distribution
      } else {
        int k = mvlong_width - 1;
        is_short_ct [1] += c;     // Long vector

        /*  bit 3 not always encoded. */
John Koleszar's avatar
John Koleszar committed
766
        do
John Koleszar's avatar
John Koleszar committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
          bit_ct [k] [(a >> k) & 1] += c;

        while (--k >= 0);
      }
    } while (++j <= mv_max);
  }

  calc_prob(Pnew + mvpis_short, is_short_ct);

  calc_prob(Pnew + MVPsign, sign_ct);

  {
    vp8_prob p [mvnum_short - 1];    /* actually only need branch ct */
    int j = 0;

    vp8_tree_probs_from_distribution(
      mvnum_short, vp8_small_mvencodings, vp8_small_mvtree,
      p, short_bct, short_ct,
      256, 1
    );
John Koleszar's avatar
John Koleszar committed
787

John Koleszar's avatar
John Koleszar committed
788 789
    do
      calc_prob(Pnew + MVPshort + j, short_bct[j]);
John Koleszar's avatar
John Koleszar committed
790

John Koleszar's avatar
John Koleszar committed
791 792
    while (++j < mvnum_short - 1);
  }
John Koleszar's avatar
John Koleszar committed
793

John Koleszar's avatar
John Koleszar committed
794 795
  {
    int j = 0;
John Koleszar's avatar
John Koleszar committed
796

John Koleszar's avatar
John Koleszar committed
797 798
    do
      calc_prob(Pnew + MVPbits + j, bit_ct[j]);
John Koleszar's avatar
John Koleszar committed
799

John Koleszar's avatar
John Koleszar committed
800 801
    while (++j < mvlong_width);
  }
John Koleszar's avatar
John Koleszar committed
802

John Koleszar's avatar
John Koleszar committed
803
  update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++, updated);
John Koleszar's avatar
John Koleszar committed
804

John Koleszar's avatar
John Koleszar committed
805
  update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated);
John Koleszar's avatar
John Koleszar committed
806

John Koleszar's avatar
John Koleszar committed
807 808 809
  {
    const vp8_prob *const new_p = Pnew + MVPshort;
    vp8_prob *const cur_p = Pcur + MVPshort;
John Koleszar's avatar
John Koleszar committed
810

John Koleszar's avatar
John Koleszar committed
811
    int j = 0;
John Koleszar's avatar
John Koleszar committed
812

John Koleszar's avatar
John Koleszar committed
813
    do
John Koleszar's avatar
John Koleszar committed
814

John Koleszar's avatar
John Koleszar committed
815
      update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
John Koleszar's avatar
John Koleszar committed
816

John Koleszar's avatar
John Koleszar committed
817 818
    while (++j < mvnum_short - 1);
  }
John Koleszar's avatar
John Koleszar committed
819

John Koleszar's avatar
John Koleszar committed
820 821 822
  {
    const vp8_prob *const new_p = Pnew + MVPbits;
    vp8_prob *const cur_p = Pcur + MVPbits;
John Koleszar's avatar
John Koleszar committed
823

John Koleszar's avatar
John Koleszar committed
824
    int j = 0;
John Koleszar's avatar
John Koleszar committed
825

John Koleszar's avatar
John Koleszar committed
826
    do
John Koleszar's avatar
John Koleszar committed
827

John Koleszar's avatar
John Koleszar committed
828
      update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);
John Koleszar's avatar
John Koleszar committed
829

John Koleszar's avatar
John Koleszar committed
830 831
    while (++j < mvlong_width);
  }
John Koleszar's avatar
John Koleszar committed
832 833
}

John Koleszar's avatar
John Koleszar committed
834 835 836 837
void vp8_write_mvprobs(VP8_COMP *cpi) {
  vp8_writer *const w  = & cpi->bc;
  MV_CONTEXT *mvc = cpi->common.fc.mvc;
  int flags[2] = {0, 0};
John Koleszar's avatar
John Koleszar committed
838
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
839
  active_section = 4;
John Koleszar's avatar
John Koleszar committed
840
#endif
John Koleszar's avatar
John Koleszar committed
841 842 843 844 845 846
  write_component_probs(
    w, &mvc[0], &vp8_default_mv_context[0], &vp8_mv_update_probs[0], cpi->MVcount[0], 0, &flags[0]
  );
  write_component_probs(
    w, &mvc[1], &vp8_default_mv_context[1], &vp8_mv_update_probs[1], cpi->MVcount[1], 1, &flags[1]
  );
John Koleszar's avatar
John Koleszar committed
847

John Koleszar's avatar
John Koleszar committed
848 849
  if (flags[0] || flags[1])
    vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flags);
John Koleszar's avatar
John Koleszar committed
850 851

#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
852
  active_section = 5;
John Koleszar's avatar
John Koleszar committed
853 854
#endif
}
855 856 857


static void encode_mvcomponent_hp(
John Koleszar's avatar
John Koleszar committed
858 859 860 861 862 863
  vp8_writer *const w,
  const int v,
  const struct mv_context_hp *mvc
) {
  const vp8_prob *p = mvc->prob;
  const int x = v < 0 ? -v : v;
864

John Koleszar's avatar
John Koleszar committed
865 866 867 868 869 870 871 872
  if (x < mvnum_short_hp) {   // Small
    vp8_write(w, 0, p [mvpis_short_hp]);
    vp8_treed_write(w, vp8_small_mvtree_hp, p + MVPshort_hp, x,
                    mvnum_short_bits_hp);
    if (!x)
      return;         // no sign bit
  } else {                // Large
    int i = 0;
873

John Koleszar's avatar
John Koleszar committed
874
    vp8_write(w, 1, p [mvpis_short_hp]);
875

John Koleszar's avatar
John Koleszar committed
876 877
    do
      vp8_write(w, (x >> i) & 1, p [MVPbits_hp + i]);
878

John Koleszar's avatar
John Koleszar committed
879
    while (++i < mvnum_short_bits_hp);
880

John Koleszar's avatar
John Koleszar committed
881
    i = mvlong_width_hp - 1;  /* Skip bit 3, which is sometimes implicit */
882

John Koleszar's avatar
John Koleszar committed
883 884
    do
      vp8_write(w, (x >> i) & 1, p [MVPbits_hp + i]);
885

John Koleszar's avatar
John Koleszar committed
886 887 888 889 890 891
    while (--i > mvnum_short_bits_hp);

    if (x & ~((2 << mvnum_short_bits_hp) - 1))
      vp8_write(w, (x >> mvnum_short_bits_hp) & 1,
                p [MVPbits_hp + mvnum_short_bits_hp]);
  }
892

John Koleszar's avatar
John Koleszar committed
893
  vp8_write(w, v < 0, p [MVPsign_hp]);
894
}
895

896
void vp8_encode_motion_vector_hp(vp8_writer *w, const MV *mv,
John Koleszar's avatar
John Koleszar committed
897
                                 const MV_CONTEXT_HP *mvc) {
898

John Koleszar's avatar
John Koleszar committed
899 900
  encode_mvcomponent_hp(w, mv->row, &mvc[0]);
  encode_mvcomponent_hp(w, mv->col, &mvc[1]);
901 902 903 904
}


static unsigned int cost_mvcomponent_hp(const int v,
John Koleszar's avatar
John Koleszar committed
905 906 907 908
                                        const struct mv_context_hp *mvc) {
  const vp8_prob *p = mvc->prob;
  const int x = v;   // v<0? -v:v;
  unsigned int cost;
909

John Koleszar's avatar
John Koleszar committed
910 911 912 913
  if (x < mvnum_short_hp) {
    cost = vp8_cost_zero(p [mvpis_short_hp])
           + vp8_treed_cost(vp8_small_mvtree_hp, p + MVPshort_hp, x,
                            mvnum_short_bits_hp);
914

John Koleszar's avatar
John Koleszar committed
915 916 917 918 919
    if (!x)
      return cost;
  } else {
    int i = 0;
    cost = vp8_cost_one(p [mvpis_short_hp]);
920

John Koleszar's avatar
John Koleszar committed
921 922
    do
      cost += vp8_cost_bit(p [MVPbits_hp + i], (x >> i) & 1);
923

John Koleszar's avatar
John Koleszar committed
924
    while (++i < mvnum_short_bits_hp);
925

John Koleszar's avatar
John Koleszar committed
926
    i = mvlong_width_hp - 1;  /* Skip bit 3, which is sometimes implicit */
927

John Koleszar's avatar
John Koleszar committed
928 929 930 931 932 933 934 935 936
    do
      cost += vp8_cost_bit(p [MVPbits_hp + i], (x >> i) & 1);

    while (--i > mvnum_short_bits_hp);

    if (x & ~((2 << mvnum_short_bits_hp) - 1))
      cost += vp8_cost_bit(p [MVPbits_hp + mvnum_short_bits_hp],
                           (x >> mvnum_short_bits_hp) & 1);
  }
937

John Koleszar's avatar
John Koleszar committed
938
  return cost;   // + vp8_cost_bit( p [MVPsign], v < 0);
939 940 941 942
}

void vp8_build_component_cost_table_hp(int *mvcost[2],
                                       const MV_CONTEXT_HP *mvc,
John Koleszar's avatar
John Koleszar committed
943 944 945 946
                                       int mvc_flag[2]) {
  int i = 1;   // -mv_max;
  unsigned int cost0 = 0;
  unsigned int cost1 = 0;
947

John Koleszar's avatar
John Koleszar committed
948
  vp8_clear_system_state();
949

John Koleszar's avatar
John Koleszar committed
950
  i = 1;
951

John Koleszar's avatar
John Koleszar committed
952 953
  if (mvc_flag[0]) {
    mvcost [0] [0] = cost_mvcomponent_hp(0, &mvc[0]);
954

John Koleszar's avatar
John Koleszar committed
955 956 957
    do {
      // mvcost [0] [i] = cost_mvcomponent( i, &mvc[0]);
      cost0 = cost_mvcomponent_hp(i, &mvc[0]);
958

John Koleszar's avatar
John Koleszar committed
959 960 961 962
      mvcost [0] [i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign_hp]);
      mvcost [0] [-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign_hp]);
    } while (++i <= mv_max_hp);
  }
963

John Koleszar's avatar
John Koleszar committed
964
  i = 1;
965

John Koleszar's avatar
John Koleszar committed
966 967
  if (mvc_flag[1]) {
    mvcost [1] [0] = cost_mvcomponent_hp(0, &mvc[1]);
968

John Koleszar's avatar
John Koleszar committed
969 970 971
    do {
      // mvcost [1] [i] = cost_mvcomponent( i, mvc[1]);
      cost1 = cost_mvcomponent_hp(i, &mvc[1]);
972

John Koleszar's avatar
John Koleszar committed
973 974 975 976 977
      mvcost [1] [i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign_hp]);
      mvcost [1] [-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign_hp]);
    } while (++i <= mv_max_hp);
  }
}
978 979


John Koleszar's avatar
John Koleszar committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
static void write_component_probs_hp(
  vp8_writer *const w,
  struct mv_context_hp *cur_mvc,
  const struct mv_context_hp *default_mvc_,
  const struct mv_context_hp *update_mvc,
  const unsigned int events [MVvals_hp],
  unsigned int rc,
  int *updated
) {
  vp8_prob *Pcur = cur_mvc->prob;
  const vp8_prob *default_mvc = default_mvc_->prob;
  const vp8_prob *Pupdate = update_mvc->prob;
  unsigned int is_short_ct[2], sign_ct[2];

  unsigned int bit_ct [mvlong_width_hp] [2];

  unsigned int short_ct  [mvnum_short_hp];
  unsigned int short_bct [mvnum_short_hp - 1] [2];

  vp8_prob Pnew [MVPcount_hp];

  (void) rc;
  vp8_copy_array(Pnew, default_mvc, MVPcount_hp);

  vp8_zero(is_short_ct)
  vp8_zero(sign_ct)
  vp8_zero(bit_ct)
  vp8_zero(short_ct)
  vp8_zero(short_bct)


  // j=0
  {
    const int c = events [mv_max_hp];

    is_short_ct [0] += c;    // Short vector
    short_ct [0] += c;       // Magnitude distribution
  }

  // j: 1 ~ mv_max (1023)
  {
    int j = 1;

    do {
      const int c1 = events [mv_max_hp + j];  // positive
      const int c2 = events [mv_max_hp - j];  // negative
      const int c  = c1 + c2;
      int a = j;

      sign_ct [0] += c1;
      sign_ct [1] += c2;

      if (a < mvnum_short_hp) {
        is_short_ct [0] += c;     // Short vector
        short_ct [a] += c;       // Magnitude distribution
      } else {
        int k = mvlong_width_hp - 1;
        is_short_ct [1] += c;     // Long vector

        /*  bit 3 not always encoded. */
1040
        do
John Koleszar's avatar
John Koleszar committed
1041
          bit_ct [k] [(a >> k) & 1] += c;
1042

John Koleszar's avatar
John Koleszar committed
1043 1044 1045 1046
        while (--k >= 0);
      }
    } while (++j <= mv_max_hp);
  }
1047

John Koleszar's avatar
John Koleszar committed
1048
  calc_prob(Pnew + mvpis_short_hp, is_short_ct);
1049

John Koleszar's avatar
John Koleszar committed
1050
  calc_prob(Pnew + MVPsign_hp, sign_ct);
1051

John Koleszar's avatar
John Koleszar committed
1052 1053 1054
  {
    vp8_prob p [mvnum_short_hp - 1];    /* actually only need branch ct */
    int j = 0;
1055

John Koleszar's avatar
John Koleszar committed
1056 1057 1058 1059 1060
    vp8_tree_probs_from_distribution(
      mvnum_short_hp, vp8_small_mvencodings_hp, vp8_small_mvtree_hp,
      p, short_bct, short_ct,
      256, 1
    );
1061

John Koleszar's avatar
John Koleszar committed
1062 1063
    do
      calc_prob(Pnew + MVPshort_hp + j, short_bct[j]);
1064

John Koleszar's avatar
John Koleszar committed
1065 1066
    while (++j < mvnum_short_hp - 1);
  }
1067

John Koleszar's avatar
John Koleszar committed
1068 1069
  {
    int j = 0;
1070

John Koleszar's avatar
John Koleszar committed
1071 1072
    do
      calc_prob(Pnew + MVPbits_hp + j, bit_ct[j]);
1073

John Koleszar's avatar
John Koleszar committed
1074 1075
    while (++j < mvlong_width_hp);
  }
1076

John Koleszar's avatar
John Koleszar committed
1077 1078
  update(w, is_short_ct, Pcur + mvpis_short_hp, Pnew[mvpis_short_hp],
         *Pupdate++, updated);
1079

John Koleszar's avatar
John Koleszar committed
1080 1081
  update(w, sign_ct, Pcur + MVPsign_hp, Pnew[MVPsign_hp], *Pupdate++,
         updated);
1082

John Koleszar's avatar
John Koleszar committed
1083 1084 1085
  {
    const vp8_prob *const new_p = Pnew + MVPshort_hp;
    vp8_prob *const cur_p = Pcur + MVPshort_hp;
1086

John Koleszar's avatar
John Koleszar committed
1087
    int j = 0;
1088

John Koleszar's avatar
John Koleszar committed
1089
    do
1090

John Koleszar's avatar
John Koleszar committed
1091
      update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
1092

John Koleszar's avatar
John Koleszar committed
1093 1094
    while (++j < mvnum_short_hp - 1);
  }
1095

John Koleszar's avatar
John Koleszar committed
1096 1097 1098
  {
    const vp8_prob *const new_p = Pnew + MVPbits_hp;
    vp8_prob *const cur_p = Pcur + MVPbits_hp;
1099

John Koleszar's avatar
John Koleszar committed
1100
    int j = 0;
1101

John Koleszar's avatar
John Koleszar committed
1102
    do
1103

John Koleszar's avatar
John Koleszar committed
1104 1105 1106 1107
      update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);

    while (++j < mvlong_width_hp);
  }
1108 1109
}

John Koleszar's avatar
John Koleszar committed
1110 1111 1112 1113
void vp8_write_mvprobs_hp(VP8_COMP *cpi) {
  vp8_writer *const w  = & cpi->bc;
  MV_CONTEXT_HP *mvc = cpi->common.fc.mvc_hp;
  int flags[2] = {0, 0};
1114
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1115
  active_section = 4;
1116
#endif
John Koleszar's avatar
John Koleszar committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  write_component_probs_hp(
    w, &mvc[0], &vp8_default_mv_context_hp[0], &vp8_mv_update_probs_hp[0],
    cpi->MVcount_hp[0], 0, &flags[0]
  );
  write_component_probs_hp(
    w, &mvc[1], &vp8_default_mv_context_hp[1], &vp8_mv_update_probs_hp[1],
    cpi->MVcount_hp[1], 1, &flags[1]
  );

  if (flags[0] || flags[1])
    vp8_build_component_cost_table_hp(cpi->mb.mvcost_hp,
                                      (const MV_CONTEXT_HP *)
                                      cpi->common.fc.mvc_hp, flags);
1130
#ifdef ENTROPY_STATS
John Koleszar's avatar
John Koleszar committed
1131
  active_section = 5;
1132 1133
#endif
}
1134 1135

#endif  /* CONFIG_NEWMVENTROPY */