entropymv.c 28.4 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
 */


12
#include "onyxc_int.h"
John Koleszar's avatar
John Koleszar committed
13 14
#include "entropymv.h"

15 16 17 18 19 20 21
//#define MV_COUNT_TESTING

#if CONFIG_NEWMVENTROPY

#define MV_COUNT_SAT 16
#define MV_MAX_UPDATE_FACTOR 160

22 23 24
/* Integer pel reference mv threshold for use of high-precision 1/8 mv */
#define COMPANDED_MVREF_THRESH    8

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
/* Smooth or bias the mv-counts before prob computation */
/* #define SMOOTH_MV_COUNTS */

const vp8_tree_index vp8_mv_joint_tree[2 * MV_JOINTS - 2] = {
  -MV_JOINT_ZERO, 2,
  -MV_JOINT_HNZVZ, 4,
  -MV_JOINT_HZVNZ, -MV_JOINT_HNZVNZ
};
struct vp8_token_struct vp8_mv_joint_encodings[MV_JOINTS];

const vp8_tree_index vp8_mv_class_tree[2 * MV_CLASSES - 2] = {
  -MV_CLASS_0, 2,
  -MV_CLASS_1, 4,
  6, 8,
  -MV_CLASS_2, -MV_CLASS_3,
  10, 12,
  -MV_CLASS_4, -MV_CLASS_5,
  -MV_CLASS_6, -MV_CLASS_7,
};
struct vp8_token_struct vp8_mv_class_encodings[MV_CLASSES];

const vp8_tree_index vp8_mv_class0_tree [2 * CLASS0_SIZE - 2] = {
  -0, -1,
};
struct vp8_token_struct vp8_mv_class0_encodings[CLASS0_SIZE];

const vp8_tree_index vp8_mv_fp_tree [2 * 4 - 2] = {
  -0, 2,
  -1, 4,
  -2, -3
};
struct vp8_token_struct vp8_mv_fp_encodings[4];

const nmv_context vp8_default_nmv_context = {
  {32, 64, 96},
  {
    { /* vert component */
      128,                                             /* sign */
      {224, 144, 192, 168, 192, 176, 192},             /* class */
      {216},                                           /* class0 */
      {136, 140, 148, 160, 176, 192, 224},             /* bits */
      {{128, 128, 64}, {96, 112, 64}},                 /* class0_fp */
      {64, 96, 64},                                    /* fp */
      160,                                             /* class0_hp bit */
      128,                                             /* hp */
    },
    { /* hor component */
      128,                                             /* sign */
      {216, 128, 176, 160, 176, 176, 192},             /* class */
      {208},                                           /* class0 */
      {136, 140, 148, 160, 176, 192, 224},             /* bits */
      {{128, 128, 64}, {96, 112, 64}},                 /* class0_fp */
      {64, 96, 64},                                    /* fp */
      160,                                             /* class0_hp bit */
      128,                                             /* hp */
    }
  },
};

MV_JOINT_TYPE vp8_get_mv_joint(MV mv) {
  if (mv.row == 0 && mv.col == 0) return MV_JOINT_ZERO;
  else if (mv.row == 0 && mv.col != 0) return MV_JOINT_HNZVZ;
  else if (mv.row != 0 && mv.col == 0) return MV_JOINT_HZVNZ;
  else return MV_JOINT_HNZVNZ;
}

#define mv_class_base(c) ((c) ? (CLASS0_SIZE << (c + 2)) : 0)

MV_CLASS_TYPE vp8_get_mv_class(int z, int *offset) {
  MV_CLASS_TYPE c;
  if      (z < CLASS0_SIZE * 8)    c = MV_CLASS_0;
  else if (z < CLASS0_SIZE * 16)   c = MV_CLASS_1;
  else if (z < CLASS0_SIZE * 32)   c = MV_CLASS_2;
  else if (z < CLASS0_SIZE * 64)   c = MV_CLASS_3;
  else if (z < CLASS0_SIZE * 128)  c = MV_CLASS_4;
  else if (z < CLASS0_SIZE * 256)  c = MV_CLASS_5;
  else if (z < CLASS0_SIZE * 512)  c = MV_CLASS_6;
  else if (z < CLASS0_SIZE * 1024) c = MV_CLASS_7;
  else assert(0);
  if (offset)
    *offset = z - mv_class_base(c);
  return c;
}

109 110 111 112 113 114 115 116
int vp8_use_nmv_hp(const MV *ref) {
  if ((abs(ref->row) >> 3) < COMPANDED_MVREF_THRESH &&
      (abs(ref->col) >> 3) < COMPANDED_MVREF_THRESH)
    return 1;
  else
    return 0;
}

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
int vp8_get_mv_mag(MV_CLASS_TYPE c, int offset) {
  return mv_class_base(c) + offset;
}

static void increment_nmv_component_count(int v,
                                          nmv_component_counts *mvcomp,
                                          int incr,
                                          int usehp) {
  assert (v != 0);            /* should not be zero */
  mvcomp->mvcount[MV_MAX + v] += incr;
}

static void increment_nmv_component(int v,
                                    nmv_component_counts *mvcomp,
                                    int incr,
                                    int usehp) {
  int s, z, c, o, d, e, f;
  assert (v != 0);            /* should not be zero */
  s = v < 0;
  mvcomp->sign[s] += incr;
  z = (s ? -v : v) - 1;       /* magnitude - 1 */

  c = vp8_get_mv_class(z, &o);
  mvcomp->classes[c] += incr;

  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) {
    mvcomp->class0[d] += incr;
  } else {
    int i, b;
    b = c + CLASS0_BITS - 1;  /* number of bits */
    for (i = 0; i < b; ++i)
      mvcomp->bits[i][((d >> i) & 1)] += incr;
  }

  /* Code the fractional pel bits */
  if (c == MV_CLASS_0) {
    mvcomp->class0_fp[d][f] += incr;
  } else {
    mvcomp->fp[f] += incr;
  }

  /* Code the high precision bit */
  if (usehp) {
    if (c == MV_CLASS_0) {
      mvcomp->class0_hp[e] += incr;
    } else {
      mvcomp->hp[e] += incr;
    }
  }
}

#ifdef SMOOTH_MV_COUNTS
static void smooth_counts(nmv_component_counts *mvcomp) {
  static const int flen = 3;  // (filter_length + 1) / 2
  static const int fval[] = {8, 3, 1};
  static const int fvalbits = 4;
  int i;
  unsigned int smvcount[MV_VALS];
  vpx_memcpy(smvcount, mvcomp->mvcount, sizeof(smvcount));
  smvcount[MV_MAX] = (smvcount[MV_MAX - 1] + smvcount[MV_MAX + 1]) >> 1;
  for (i = flen - 1; i <= MV_VALS - flen; ++i) {
    int j, s = smvcount[i] * fval[0];
    for (j = 1; j < flen; ++j)
      s += (smvcount[i - j] + smvcount[i + j]) * fval[j];
    mvcomp->mvcount[i] = (s + (1 << (fvalbits - 1))) >> fvalbits;
  }
}
#endif

static void counts_to_context(nmv_component_counts *mvcomp, int usehp) {
  int v;
  vpx_memset(mvcomp->sign, 0, sizeof(nmv_component_counts) - sizeof(mvcomp->mvcount));
  for (v = 1; v <= MV_MAX; v++) {
    increment_nmv_component(-v, mvcomp, mvcomp->mvcount[MV_MAX - v], usehp);
    increment_nmv_component( v, mvcomp, mvcomp->mvcount[MV_MAX + v], usehp);
  }
}

void vp8_increment_nmv(const MV *mv, const MV *ref, nmv_context_counts *mvctx,
                       int usehp) {
  MV_JOINT_TYPE j = vp8_get_mv_joint(*mv);
  mvctx->joints[j]++;
202
  usehp = usehp && vp8_use_nmv_hp(ref);
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
  if (j == MV_JOINT_HZVNZ || j == MV_JOINT_HNZVNZ) {
    increment_nmv_component_count(mv->row, &mvctx->comps[0], 1, usehp);
  }
  if (j == MV_JOINT_HNZVZ || j == MV_JOINT_HNZVNZ) {
    increment_nmv_component_count(mv->col, &mvctx->comps[1], 1, usehp);
  }
}

static void adapt_prob(vp8_prob *dest, vp8_prob prep, vp8_prob newp,
                       unsigned int ct[2]) {
  int factor;
  int prob;
  int count = ct[0] + ct[1];
  if (count) {
    count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
    factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
    prob = ((int)prep * (256 - factor) + (int)(newp) * factor + 128) >> 8;
    prob += !prob;
    prob = (prob > 255 ? 255 : prob);
    *dest = prob;
  }
}

void vp8_counts_to_nmv_context(
    nmv_context_counts *NMVcount,
    nmv_context *prob,
    int usehp,
    unsigned int (*branch_ct_joint)[2],
    unsigned int (*branch_ct_sign)[2],
    unsigned int (*branch_ct_classes)[MV_CLASSES - 1][2],
    unsigned int (*branch_ct_class0)[CLASS0_SIZE - 1][2],
    unsigned int (*branch_ct_bits)[MV_OFFSET_BITS][2],
    unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][4 - 1][2],
    unsigned int (*branch_ct_fp)[4 - 1][2],
    unsigned int (*branch_ct_class0_hp)[2],
    unsigned int (*branch_ct_hp)[2]) {
  int i, j, k;
  counts_to_context(&NMVcount->comps[0], usehp);
  counts_to_context(&NMVcount->comps[1], usehp);
  vp8_tree_probs_from_distribution(MV_JOINTS,
                                   vp8_mv_joint_encodings,
                                   vp8_mv_joint_tree,
                                   prob->joints,
                                   branch_ct_joint,
                                   NMVcount->joints,
                                   256, 1);
  for (i = 0; i < 2; ++i) {
    prob->comps[i].sign =
        vp8_bin_prob_from_distribution(NMVcount->comps[i].sign);
    branch_ct_sign[i][0] = NMVcount->comps[i].sign[0];
    branch_ct_sign[i][1] = NMVcount->comps[i].sign[1];
    vp8_tree_probs_from_distribution(MV_CLASSES,
                                     vp8_mv_class_encodings,
                                     vp8_mv_class_tree,
                                     prob->comps[i].classes,
                                     branch_ct_classes[i],
                                     NMVcount->comps[i].classes,
                                     256, 1);
    vp8_tree_probs_from_distribution(CLASS0_SIZE,
                                     vp8_mv_class0_encodings,
                                     vp8_mv_class0_tree,
                                     prob->comps[i].class0,
                                     branch_ct_class0[i],
                                     NMVcount->comps[i].class0,
                                     256, 1);
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
      prob->comps[i].bits[j] = vp8_bin_prob_from_distribution(
          NMVcount->comps[i].bits[j]);
      branch_ct_bits[i][j][0] = NMVcount->comps[i].bits[j][0];
      branch_ct_bits[i][j][1] = NMVcount->comps[i].bits[j][1];
    }
  }
  for (i = 0; i < 2; ++i) {
    for (k = 0; k < CLASS0_SIZE; ++k) {
      vp8_tree_probs_from_distribution(4,
                                       vp8_mv_fp_encodings,
                                       vp8_mv_fp_tree,
                                       prob->comps[i].class0_fp[k],
                                       branch_ct_class0_fp[i][k],
                                       NMVcount->comps[i].class0_fp[k],
                                       256, 1);
    }
    vp8_tree_probs_from_distribution(4,
                                     vp8_mv_fp_encodings,
                                     vp8_mv_fp_tree,
                                     prob->comps[i].fp,
                                     branch_ct_fp[i],
                                     NMVcount->comps[i].fp,
                                     256, 1);
  }
  if (usehp) {
    for (i = 0; i < 2; ++i) {
      prob->comps[i].class0_hp = vp8_bin_prob_from_distribution(
          NMVcount->comps[i].class0_hp);
      branch_ct_class0_hp[i][0] = NMVcount->comps[i].class0_hp[0];
      branch_ct_class0_hp[i][1] = NMVcount->comps[i].class0_hp[1];

      prob->comps[i].hp =
          vp8_bin_prob_from_distribution(NMVcount->comps[i].hp);
      branch_ct_hp[i][0] = NMVcount->comps[i].hp[0];
      branch_ct_hp[i][1] = NMVcount->comps[i].hp[1];
    }
  }
}

void vp8_adapt_nmv_probs(VP8_COMMON *cm, int usehp) {
  int i, j, k;
  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];
#ifdef MV_COUNT_TESTING
  printf("joints count: ");
  for (j = 0; j < MV_JOINTS; ++j) printf("%d ", cm->fc.NMVcount.joints[j]);
  printf("\n"); fflush(stdout);
  printf("signs count:\n");
  for (i = 0; i < 2; ++i)
    printf("%d/%d ", cm->fc.NMVcount.comps[i].sign[0], cm->fc.NMVcount.comps[i].sign[1]);
  printf("\n"); fflush(stdout);
  printf("classes count:\n");
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < MV_CLASSES; ++j)
      printf("%d ", cm->fc.NMVcount.comps[i].classes[j]);
    printf("\n"); fflush(stdout);
  }
  printf("class0 count:\n");
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j)
      printf("%d ", cm->fc.NMVcount.comps[i].class0[j]);
    printf("\n"); fflush(stdout);
  }
  printf("bits count:\n");
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < MV_OFFSET_BITS; ++j)
      printf("%d/%d ", cm->fc.NMVcount.comps[i].bits[j][0],
                       cm->fc.NMVcount.comps[i].bits[j][1]);
    printf("\n"); fflush(stdout);
  }
  printf("class0_fp count:\n");
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j) {
      printf("{");
      for (k = 0; k < 4; ++k)
        printf("%d ", cm->fc.NMVcount.comps[i].class0_fp[j][k]);
      printf("}, ");
    }
    printf("\n"); fflush(stdout);
  }
  printf("fp count:\n");
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < 4; ++j)
      printf("%d ", cm->fc.NMVcount.comps[i].fp[j]);
    printf("\n"); fflush(stdout);
  }
  if (usehp) {
    printf("class0_hp count:\n");
    for (i = 0; i < 2; ++i)
      printf("%d/%d ", cm->fc.NMVcount.comps[i].class0_hp[0],
                       cm->fc.NMVcount.comps[i].class0_hp[1]);
    printf("\n"); fflush(stdout);
    printf("hp count:\n");
    for (i = 0; i < 2; ++i)
      printf("%d/%d ", cm->fc.NMVcount.comps[i].hp[0],
                       cm->fc.NMVcount.comps[i].hp[1]);
    printf("\n"); fflush(stdout);
  }
#endif
#ifdef SMOOTH_MV_COUNTS
  smooth_counts(&cm->fc.NMVcount.comps[0]);
  smooth_counts(&cm->fc.NMVcount.comps[1]);
#endif
  vp8_counts_to_nmv_context(&cm->fc.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);

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

#else   /* CONFIG_NEWMVENTROPY */

#define MV_COUNT_SAT 16
#define MV_MAX_UPDATE_FACTOR 128

John Koleszar's avatar
John Koleszar committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
const MV_CONTEXT_HP vp8_mv_update_probs_hp[2] = {
  {{
      237,
      246,
      253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
      254, 254, 254, 254, 254, 250, 250, 252, 254, 254, 254
    }
  },
  {{
      231,
      243,
      245, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
      254, 254, 254, 254, 254, 251, 251, 254, 254, 254, 254
    }
  }
473
};
John Koleszar's avatar
John Koleszar committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
const MV_CONTEXT_HP vp8_default_mv_context_hp[2] = {
  {{
      /* row */
      162,                                        /* is short */
      128,                                        /* sign */
      220, 204, 180, 192, 192, 119, 192, 192, 180, 140, 192, 192, 224, 224, 224, /* short tree */
      128, 129, 132,  75, 145, 178, 206, 239, 254, 254, 254 /* long bits */
    }
  },
  {{
      /* same for column */
      164,                                        /* is short */
      128,
      220, 204, 180, 192, 192, 119, 192, 192, 180, 140, 192, 192, 224, 224, 224, /* short tree */
      128, 130, 130,  74, 148, 180, 203, 236, 254, 254, 254 /* long bits */
    }
  }
491
};
492

John Koleszar's avatar
John Koleszar committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
const MV_CONTEXT vp8_mv_update_probs[2] = {
  {{
      237,
      246,
      253, 253, 254, 254, 254, 254, 254,
      254, 254, 254, 254, 254, 250, 250, 252, 254, 254
    }
  },
  {{
      231,
      243,
      245, 253, 254, 254, 254, 254, 254,
      254, 254, 254, 254, 254, 251, 251, 254, 254, 254
    }
  }
John Koleszar's avatar
John Koleszar committed
508
};
John Koleszar's avatar
John Koleszar committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
const MV_CONTEXT vp8_default_mv_context[2] = {
  {{
      /* row */
      162,                                        /* is short */
      128,                                        /* sign */
      225, 146, 172, 147, 214,  39, 156,          /* short tree */
      128, 129, 132,  75, 145, 178, 206, 239, 254, 254 /* long bits */
    }
  },
  {{
      /* same for column */
      164,                                        /* is short */
      128,
      204, 170, 119, 235, 140, 230, 228,
      128, 130, 130,  74, 148, 180, 203, 236, 254, 254 /* long bits */
    }
  }
John Koleszar's avatar
John Koleszar committed
526
};
527

John Koleszar's avatar
John Koleszar committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
const vp8_tree_index vp8_small_mvtree_hp [30] = {
  2,  16,
  4,  10,
  6,   8,
  -0,  -1,
  -2,  -3,
  12,  14,
  -4,  -5,
  -6,  -7,
  18,  24,
  20,  22,
  -8,  -9,
  -10, -11,
  26,  28,
  -12, -13,
  -14, -15
544 545 546
};
struct vp8_token_struct vp8_small_mvencodings_hp [16];

John Koleszar's avatar
John Koleszar committed
547 548 549 550 551 552 553 554
const vp8_tree_index vp8_small_mvtree [14] = {
  2, 8,
  4, 6,
  -0, -1,
  -2, -3,
  10, 12,
  -4, -5,
  -6, -7
555 556 557
};
struct vp8_token_struct vp8_small_mvencodings [8];

John Koleszar's avatar
John Koleszar committed
558 559 560 561 562
__inline static void calc_prob(vp8_prob *p, const unsigned int ct[2], int pbits) {
  const unsigned int tot = ct[0] + ct[1];
  if (tot) {
    const vp8_prob x = ((ct[0] * 255) / tot) & -(1 << (8 - pbits));
    *p = x ? x : 1;
563 564
  } else {
    *p = 128;
John Koleszar's avatar
John Koleszar committed
565
  }
566 567 568
}

static void compute_component_probs(
John Koleszar's avatar
John Koleszar committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
  const unsigned int events [MVvals],
  vp8_prob Pnew [MVPcount],
  unsigned int is_short_ct[2],
  unsigned int sign_ct[2],
  unsigned int bit_ct [mvlong_width] [2],
  unsigned int short_ct  [mvnum_short],
  unsigned int short_bct [mvnum_short - 1] [2]
) {
  is_short_ct[0] = is_short_ct[1] = 0;
  sign_ct[0] = sign_ct[1] = 0;
  vpx_memset(bit_ct, 0, sizeof(unsigned int)*mvlong_width * 2);
  vpx_memset(short_ct, 0, sizeof(unsigned int)*mvnum_short);
  vpx_memset(short_bct, 0, sizeof(unsigned int) * (mvnum_short - 1) * 2);

  {
    const int c = events [mv_max];
    is_short_ct [0] += c;    // Short vector
    short_ct [0] += c;       // Magnitude distribution
  }
  {
    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
605 606

        do
John Koleszar's avatar
John Koleszar committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
          bit_ct [k] [(a >> k) & 1] += c;

        while (--k >= 0);
      }
    } while (++j <= mv_max);
  }
  calc_prob(Pnew + mvpis_short, is_short_ct, 8);

  calc_prob(Pnew + MVPsign, sign_ct, 8);

  {
    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
    );

    do
      calc_prob(Pnew + MVPshort + j, short_bct[j], 8);
    while (++j < mvnum_short - 1);
  }

  {
    int j = 0;
    do
      calc_prob(Pnew + MVPbits + j, bit_ct[j], 8);
    while (++j < mvlong_width);
  }
638 639 640
}

static void compute_component_probs_hp(
John Koleszar's avatar
John Koleszar committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
  const unsigned int events [MVvals_hp],
  vp8_prob Pnew [MVPcount_hp],
  unsigned int is_short_ct[2],
  unsigned int 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]
) {
  is_short_ct[0] = is_short_ct[1] = 0;
  sign_ct[0] = sign_ct[1] = 0;
  vpx_memset(bit_ct, 0, sizeof(unsigned int)*mvlong_width_hp * 2);
  vpx_memset(short_ct, 0, sizeof(unsigned int)*mvnum_short_hp);
  vpx_memset(short_bct, 0, sizeof(unsigned int) * (mvnum_short_hp - 1) * 2);

  {
    const int c = events [mv_max_hp];
    is_short_ct [0] += c;    // Short vector
    short_ct [0] += c;       // Magnitude distribution
  }
  {
    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
677 678

        do
John Koleszar's avatar
John Koleszar committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
          bit_ct [k] [(a >> k) & 1] += c;

        while (--k >= 0);
      }
    } while (++j <= mv_max_hp);
  }
  calc_prob(Pnew + mvpis_short_hp, is_short_ct, 8);

  calc_prob(Pnew + MVPsign_hp, sign_ct, 8);

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

    vp8_tree_probs_from_distribution(
      mvnum_short_hp, vp8_small_mvencodings_hp, vp8_small_mvtree_hp,
      p, short_bct, short_ct,
      256, 1
    );

    do
      calc_prob(Pnew + MVPshort_hp + j, short_bct[j], 8);
    while (++j < mvnum_short_hp - 1);
  }

  {
    int j = 0;
    do
      calc_prob(Pnew + MVPbits_hp + j, bit_ct[j], 8);
    while (++j < mvlong_width_hp);
  }
710 711
}

John Koleszar's avatar
John Koleszar committed
712 713
void vp8_adapt_mv_probs(VP8_COMMON *cm) {
  int i, t, count, factor;
714
#ifdef MV_COUNT_TESTING
John Koleszar's avatar
John Koleszar committed
715 716 717 718 719 720
  printf("static const unsigned int\nMVcount[2][MVvals]={\n");
  for (i = 0; i < 2; ++i) {
    printf("  { ");
    for (t = 0; t < MVvals; t++) {
      printf("%d, ", cm->fc.MVcount[i][t]);
      if (t % 16 == 15 && t != MVvals - 1) printf("\n    ");
721
    }
John Koleszar's avatar
John Koleszar committed
722 723 724 725 726 727 728 729 730
    printf("},\n");
  }
  printf("};\n");
  printf("static const unsigned int\nMVcount_hp[2][MVvals_hp]={\n");
  for (i = 0; i < 2; ++i) {
    printf("  { ");
    for (t = 0; t < MVvals_hp; t++) {
      printf("%d, ", cm->fc.MVcount_hp[i][t]);
      if (t % 16 == 15 && t != MVvals_hp - 1) printf("\n    ");
731
    }
John Koleszar's avatar
John Koleszar committed
732 733 734
    printf("},\n");
  }
  printf("};\n");
735 736
#endif  /* MV_COUNT_TESTING */

John Koleszar's avatar
John Koleszar committed
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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
  for (i = 0; i < 2; ++i) {
    int prob;
    unsigned int is_short_ct[2];
    unsigned int 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];
    compute_component_probs(cm->fc.MVcount[i], Pnew,
                            is_short_ct, sign_ct,
                            bit_ct, short_ct, short_bct);
    count = is_short_ct[0] + is_short_ct[1];
    count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
    factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
    prob = ((int)cm->fc.pre_mvc[i].prob[mvpis_short] * (256 - factor) +
            (int)Pnew[mvpis_short] * factor + 128) >> 8;
    if (prob <= 0) cm->fc.mvc[i].prob[mvpis_short] = 1;
    else if (prob > 255) cm->fc.mvc[i].prob[mvpis_short] = 255;
    else cm->fc.mvc[i].prob[mvpis_short] = prob;

    count = sign_ct[0] + sign_ct[1];
    count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
    factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
    prob = ((int)cm->fc.pre_mvc[i].prob[MVPsign] * (256 - factor) +
            (int)Pnew[MVPsign] * factor + 128) >> 8;
    if (prob <= 0) cm->fc.mvc[i].prob[MVPsign] = 1;
    else if (prob > 255) cm->fc.mvc[i].prob[MVPsign] = 255;
    else cm->fc.mvc[i].prob[MVPsign] = prob;

    for (t = 0; t < mvnum_short - 1; ++t) {
      count = short_bct[t][0] + short_bct[t][1];
      count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
      factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
      prob = ((int)cm->fc.pre_mvc[i].prob[MVPshort + t] * (256 - factor) +
              (int)Pnew[MVPshort + t] * factor + 128) >> 8;
      if (prob <= 0) cm->fc.mvc[i].prob[MVPshort + t] = 1;
      else if (prob > 255) cm->fc.mvc[i].prob[MVPshort + t] = 255;
      else cm->fc.mvc[i].prob[MVPshort + t] = prob;
    }
    for (t = 0; t < mvlong_width; ++t) {
      count = bit_ct[t][0] + bit_ct[t][1];
      count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
      factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
      prob = ((int)cm->fc.pre_mvc[i].prob[MVPbits + t] * (256 - factor) +
              (int)Pnew[MVPbits + t] * factor + 128) >> 8;
      if (prob <= 0) cm->fc.mvc[i].prob[MVPbits + t] = 1;
      else if (prob > 255) cm->fc.mvc[i].prob[MVPbits + t] = 255;
      else cm->fc.mvc[i].prob[MVPbits + t] = prob;
785
    }
John Koleszar's avatar
John Koleszar committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
  }
  for (i = 0; i < 2; ++i) {
    int prob;
    unsigned int is_short_ct[2];
    unsigned int 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];
    compute_component_probs_hp(cm->fc.MVcount_hp[i], Pnew,
                               is_short_ct, sign_ct,
                               bit_ct, short_ct, short_bct);
    count = is_short_ct[0] + is_short_ct[1];
    count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
    factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
    prob = ((int)cm->fc.pre_mvc_hp[i].prob[mvpis_short_hp] * (256 - factor) +
            (int)Pnew[mvpis_short_hp] * factor + 128) >> 8;
    if (prob <= 0) cm->fc.mvc_hp[i].prob[mvpis_short_hp] = 1;
    else if (prob > 255) cm->fc.mvc_hp[i].prob[mvpis_short_hp] = 255;
    else cm->fc.mvc_hp[i].prob[mvpis_short_hp] = prob;

    count = sign_ct[0] + sign_ct[1];
    count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
    factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
    prob = ((int)cm->fc.pre_mvc_hp[i].prob[MVPsign_hp] * (256 - factor) +
            (int)Pnew[MVPsign_hp] * factor + 128) >> 8;
    if (prob <= 0) cm->fc.mvc_hp[i].prob[MVPsign_hp] = 1;
    else if (prob > 255) cm->fc.mvc_hp[i].prob[MVPsign_hp] = 255;
    else cm->fc.mvc_hp[i].prob[MVPsign_hp] = prob;

    for (t = 0; t < mvnum_short_hp - 1; ++t) {
      count = short_bct[t][0] + short_bct[t][1];
      count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
      factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
      prob = ((int)cm->fc.pre_mvc_hp[i].prob[MVPshort_hp + t] * (256 - factor) +
              (int)Pnew[MVPshort_hp + t] * factor + 128) >> 8;
      if (prob <= 0) cm->fc.mvc_hp[i].prob[MVPshort_hp + t] = 1;
      else if (prob > 255) cm->fc.mvc_hp[i].prob[MVPshort_hp + t] = 255;
      else cm->fc.mvc_hp[i].prob[MVPshort_hp + t] = prob;
    }
    for (t = 0; t < mvlong_width_hp; ++t) {
      count = bit_ct[t][0] + bit_ct[t][1];
      count = count > MV_COUNT_SAT ? MV_COUNT_SAT : count;
      factor = (MV_MAX_UPDATE_FACTOR * count / MV_COUNT_SAT);
      prob = ((int)cm->fc.pre_mvc_hp[i].prob[MVPbits_hp + t] * (256 - factor) +
              (int)Pnew[MVPbits_hp + t] * factor + 128) >> 8;
      if (prob <= 0) cm->fc.mvc_hp[i].prob[MVPbits_hp + t] = 1;
      else if (prob > 255) cm->fc.mvc_hp[i].prob[MVPbits_hp + t] = 255;
      else cm->fc.mvc_hp[i].prob[MVPbits_hp + t] = prob;
835
    }
John Koleszar's avatar
John Koleszar committed
836
  }
837
}
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

#endif  /* CONFIG_NEWMVENTROPY */

void vp8_entropy_mv_init() {
#if CONFIG_NEWMVENTROPY
  vp8_tokens_from_tree(vp8_mv_joint_encodings, vp8_mv_joint_tree);
  vp8_tokens_from_tree(vp8_mv_class_encodings, vp8_mv_class_tree);
  vp8_tokens_from_tree(vp8_mv_class0_encodings, vp8_mv_class0_tree);
  vp8_tokens_from_tree(vp8_mv_fp_encodings, vp8_mv_fp_tree);
#else
  vp8_tokens_from_tree(vp8_small_mvencodings, vp8_small_mvtree);
  vp8_tokens_from_tree(vp8_small_mvencodings_hp, vp8_small_mvtree_hp);
#endif
}

void vp8_init_mv_probs(VP8_COMMON *cm) {
#if CONFIG_NEWMVENTROPY
  vpx_memcpy(&cm->fc.nmvc, &vp8_default_nmv_context, sizeof(nmv_context));
#else
  vpx_memcpy(cm->fc.mvc,
             vp8_default_mv_context, sizeof(vp8_default_mv_context));
  vpx_memcpy(cm->fc.mvc_hp,
             vp8_default_mv_context_hp, sizeof(vp8_default_mv_context_hp));
#endif
}