vp9_encodemv.c 8.87 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
 */

Jim Bankoski's avatar
Jim Bankoski committed
11
#include <math.h>
John Koleszar's avatar
John Koleszar committed
12

13 14 15
#include "vp9/common/vp9_common.h"
#include "vp9/common/vp9_entropymode.h"
#include "vp9/common/vp9_systemdependent.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
16 17

#include "vp9/encoder/vp9_cost.h"
Jim Bankoski's avatar
Jim Bankoski committed
18
#include "vp9/encoder/vp9_encodemv.h"
John Koleszar's avatar
John Koleszar committed
19

20 21 22 23 24 25 26 27 28 29 30 31
static struct vp9_token mv_joint_encodings[MV_JOINTS];
static struct vp9_token mv_class_encodings[MV_CLASSES];
static struct vp9_token mv_fp_encodings[MV_FP_SIZE];
static struct vp9_token mv_class0_encodings[CLASS0_SIZE];

void vp9_entropy_mv_init() {
  vp9_tokens_from_tree(mv_joint_encodings, vp9_mv_joint_tree);
  vp9_tokens_from_tree(mv_class_encodings, vp9_mv_class_tree);
  vp9_tokens_from_tree(mv_class0_encodings, vp9_mv_class0_tree);
  vp9_tokens_from_tree(mv_fp_encodings, vp9_mv_fp_tree);
}

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static void encode_mv_component(vp9_writer* w, int comp,
                                const nmv_component* mvcomp, int usehp) {
  int offset;
  const int sign = comp < 0;
  const int mag = sign ? -comp : comp;
  const int mv_class = vp9_get_mv_class(mag - 1, &offset);
  const int d = offset >> 3;                // int mv data
  const int fr = (offset >> 1) & 3;         // fractional mv data
  const int hp = offset & 1;                // high precision mv data

  assert(comp != 0);

  // Sign
  vp9_write(w, sign, mvcomp->sign);

  // Class
48 49
  vp9_write_token(w, vp9_mv_class_tree, mvcomp->classes,
                  &mv_class_encodings[mv_class]);
50 51 52

  // Integer bits
  if (mv_class == MV_CLASS_0) {
53 54
    vp9_write_token(w, vp9_mv_class0_tree, mvcomp->class0,
                    &mv_class0_encodings[d]);
55
  } else {
56 57 58 59
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
    for (i = 0; i < n; ++i)
      vp9_write(w, (d >> i) & 1, mvcomp->bits[i]);
60 61
  }

62
  // Fractional bits
63 64 65
  vp9_write_token(w, vp9_mv_fp_tree,
                  mv_class == MV_CLASS_0 ?  mvcomp->class0_fp[d] : mvcomp->fp,
                  &mv_fp_encodings[fr]);
66 67 68 69 70

  // High precision bit
  if (usehp)
    vp9_write(w, hp,
              mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
71 72
}

73

74
static void build_nmv_component_cost_table(int *mvcost,
John Koleszar's avatar
John Koleszar committed
75
                                           const nmv_component* const mvcomp,
76 77 78 79
                                           int usehp) {
  int i, v;
  int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
  int bits_cost[MV_OFFSET_BITS][2];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
80
  int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
81 82
  int class0_hp_cost[2], hp_cost[2];

83 84
  sign_cost[0] = vp9_cost_zero(mvcomp->sign);
  sign_cost[1] = vp9_cost_one(mvcomp->sign);
85 86
  vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree);
  vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree);
87
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
88 89
    bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
    bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
90 91 92
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
93 94
    vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree);
  vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree);
95 96

  if (usehp) {
97 98 99 100
    class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp);
    class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp);
    hp_cost[0] = vp9_cost_zero(mvcomp->hp);
    hp_cost[1] = vp9_cost_one(mvcomp->hp);
101 102 103 104 105
  }
  mvcost[0] = 0;
  for (v = 1; v <= MV_MAX; ++v) {
    int z, c, o, d, e, f, cost = 0;
    z = v - 1;
106
    c = vp9_get_mv_class(z, &o);
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
    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];
  }
}

136 137
static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p,
                     vp9_prob upd_p) {
138 139 140 141 142 143 144
  const vp9_prob new_p = get_binary_prob(ct[0], ct[1]) | 1;
  const int update = cost_branch256(ct, *cur_p) + vp9_cost_zero(upd_p) >
                     cost_branch256(ct, new_p) + vp9_cost_one(upd_p) + 7 * 256;
  vp9_write(w, update, upd_p);
  if (update) {
    *cur_p = new_p;
    vp9_write_literal(w, new_p >> 1, 7);
145
  }
146
  return update;
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160
static void write_mv_update(const vp9_tree_index *tree,
                            vp9_prob probs[/*n - 1*/],
                            const unsigned int counts[/*n - 1*/],
                            int n, vp9_writer *w) {
  int i;
  unsigned int branch_ct[32][2];

  // Assuming max number of probabilities <= 32
  assert(n <= 32);

  vp9_tree_probs_from_distribution(tree, branch_ct, counts);
  for (i = 0; i < n - 1; ++i)
161
    update_mv(w, branch_ct[i], &probs[i], MV_UPDATE_PROB);
162 163
}

164
void vp9_write_nmv_probs(VP9_COMMON *cm, int usehp, vp9_writer *w) {
165
  int i, j;
166 167
  nmv_context *const mvc = &cm->fc.nmvc;
  nmv_context_counts *const counts = &cm->counts.mv;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
168

169
  write_mv_update(vp9_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS, w);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
170

171
  for (i = 0; i < 2; ++i) {
172 173 174
    nmv_component *comp = &mvc->comps[i];
    nmv_component_counts *comp_counts = &counts->comps[i];

175
    update_mv(w, comp_counts->sign, &comp->sign, MV_UPDATE_PROB);
176 177 178 179
    write_mv_update(vp9_mv_class_tree, comp->classes, comp_counts->classes,
                    MV_CLASSES, w);
    write_mv_update(vp9_mv_class0_tree, comp->class0, comp_counts->class0,
                    CLASS0_SIZE, w);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
180
    for (j = 0; j < MV_OFFSET_BITS; ++j)
181
      update_mv(w, comp_counts->bits[j], &comp->bits[j], MV_UPDATE_PROB);
182
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
183

184
  for (i = 0; i < 2; ++i) {
185 186 187
    for (j = 0; j < CLASS0_SIZE; ++j)
      write_mv_update(vp9_mv_fp_tree, mvc->comps[i].class0_fp[j],
                      counts->comps[i].class0_fp[j], MV_FP_SIZE, w);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
188

189 190
    write_mv_update(vp9_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
                    MV_FP_SIZE, w);
191
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
192

193 194
  if (usehp) {
    for (i = 0; i < 2; ++i) {
195
      update_mv(w, counts->comps[i].class0_hp, &mvc->comps[i].class0_hp,
196 197
                MV_UPDATE_PROB);
      update_mv(w, counts->comps[i].hp, &mvc->comps[i].hp, MV_UPDATE_PROB);
198 199 200 201
    }
  }
}

Paul Wilkins's avatar
Paul Wilkins committed
202 203
void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w,
                   const MV* mv, const MV* ref,
204 205 206 207
                   const nmv_context* mvctx, int usehp) {
  const MV diff = {mv->row - ref->row,
                   mv->col - ref->col};
  const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff);
208
  usehp = usehp && vp9_use_mv_hp(ref);
209

210
  vp9_write_token(w, vp9_mv_joint_tree, mvctx->joints, &mv_joint_encodings[j]);
211
  if (mv_joint_vertical(j))
212
    encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
213 214

  if (mv_joint_horizontal(j))
215
    encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
Paul Wilkins's avatar
Paul Wilkins committed
216

217 218 219 220 221
  // If auto_mv_step_size is enabled then keep track of the largest
  // motion vector component used.
  if (!cpi->dummy_packing && cpi->sf.auto_mv_step_size) {
    unsigned int maxv = MAX(abs(mv->row), abs(mv->col)) >> 3;
    cpi->max_mv_magnitude = MAX(maxv, cpi->max_mv_magnitude);
Paul Wilkins's avatar
Paul Wilkins committed
222
  }
223 224
}

225 226 227 228 229
void vp9_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
                              const nmv_context* ctx, int usehp) {
  vp9_cost_tokens(mvjoint, ctx->joints, vp9_mv_joint_tree);
  build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], usehp);
  build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], usehp);
230
}
Deb Mukherjee's avatar
Deb Mukherjee committed
231

232
static void inc_mvs(const MB_MODE_INFO *mbmi, const int_mv mvs[2],
233 234
                    nmv_context_counts *counts) {
  int i;
235 236 237 238 239

  for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
    const MV *ref = &mbmi->ref_mvs[mbmi->ref_frame[i]][0].as_mv;
    const MV diff = {mvs[i].as_mv.row - ref->row,
                     mvs[i].as_mv.col - ref->col};
240 241 242 243
    vp9_inc_mv(&diff, counts);
  }
}

244
void vp9_update_mv_count(VP9_COMMON *cm, const MACROBLOCKD *xd) {
245 246
  const MODE_INFO *mi = xd->mi_8x8[0];
  const MB_MODE_INFO *const mbmi = &mi->mbmi;
Deb Mukherjee's avatar
Deb Mukherjee committed
247

248
  if (mbmi->sb_type < BLOCK_8X8) {
249 250 251 252 253 254
    const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type];
    const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type];
    int idx, idy;

    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
255
        const int i = idy * 2 + idx;
256
        if (mi->bmi[i].as_mode == NEWMV)
257
          inc_mvs(mbmi, mi->bmi[i].as_mv, &cm->counts.mv);
Deb Mukherjee's avatar
Deb Mukherjee committed
258 259
      }
    }
260 261 262
  } else {
    if (mbmi->mode == NEWMV)
      inc_mvs(mbmi, mbmi->mv, &cm->counts.mv);
Deb Mukherjee's avatar
Deb Mukherjee committed
263 264
  }
}
265