vp9_encodemv.c 11.3 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"
Jim Bankoski's avatar
Jim Bankoski committed
16
#include "vp9/encoder/vp9_encodemv.h"
John Koleszar's avatar
John Koleszar committed
17
18
19
20
21
22


#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
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
  write_token(w, vp9_mv_class_tree, mvcomp->classes,
              &vp9_mv_class_encodings[mv_class]);

  // Integer bits
  if (mv_class == MV_CLASS_0) {
    write_token(w, vp9_mv_class0_tree, mvcomp->class0,
                &vp9_mv_class0_encodings[d]);
46
  } else {
47
48
49
50
    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]);
51
52
  }

53
54
55
56
57
58
59
60
61
  // Fractional bits
  write_token(w, vp9_mv_fp_tree,
              mv_class == MV_CLASS_0 ?  mvcomp->class0_fp[d] : mvcomp->fp,
              &vp9_mv_fp_encodings[fr]);

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

64

65
static void build_nmv_component_cost_table(int *mvcost,
John Koleszar's avatar
John Koleszar committed
66
                                           const nmv_component* const mvcomp,
67
68
69
70
                                           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
71
  int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
72
73
  int class0_hp_cost[2], hp_cost[2];

74
75
  sign_cost[0] = vp9_cost_zero(mvcomp->sign);
  sign_cost[1] = vp9_cost_one(mvcomp->sign);
76
77
  vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree);
  vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree);
78
  for (i = 0; i < MV_OFFSET_BITS; ++i) {
79
80
    bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
    bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
81
82
83
  }

  for (i = 0; i < CLASS0_SIZE; ++i)
84
85
    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);
86
87

  if (usehp) {
88
89
90
91
    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);
92
93
94
95
96
  }
  mvcost[0] = 0;
  for (v = 1; v <= MV_MAX; ++v) {
    int z, c, o, d, e, f, cost = 0;
    z = v - 1;
97
    c = vp9_get_mv_class(z, &o);
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
    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];
  }
}

127
128
static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p,
                     vp9_prob upd_p) {
129
130
131
132
133
134
135
  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);
136
  }
137
  return update;
138
139
}

140
141
142
143
144
145
146
147
static void counts_to_nmv_context(
    nmv_context_counts *nmv_count,
    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],
Dmitry Kovalev's avatar
Dmitry Kovalev committed
148
149
    unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][MV_FP_SIZE - 1][2],
    unsigned int (*branch_ct_fp)[MV_FP_SIZE - 1][2],
150
151
152
    unsigned int (*branch_ct_class0_hp)[2],
    unsigned int (*branch_ct_hp)[2]) {
  int i, j, k;
153
154
  vp9_tree_probs_from_distribution(vp9_mv_joint_tree, branch_ct_joint,
                                   nmv_count->joints);
155
156
157
158
159
160
161
  for (i = 0; i < 2; ++i) {
    const uint32_t s0 = nmv_count->comps[i].sign[0];
    const uint32_t s1 = nmv_count->comps[i].sign[1];

    branch_ct_sign[i][0] = s0;
    branch_ct_sign[i][1] = s1;
    vp9_tree_probs_from_distribution(vp9_mv_class_tree,
162
                                    branch_ct_classes[i],
163
                                    nmv_count->comps[i].classes);
164
165
    vp9_tree_probs_from_distribution(vp9_mv_class0_tree,
                                     branch_ct_class0[i],
166
                                     nmv_count->comps[i].class0);
167
168
169
170
171
172
173
174
175
176
177
178
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
      const uint32_t b0 = nmv_count->comps[i].bits[j][0];
      const uint32_t b1 = nmv_count->comps[i].bits[j][1];

      branch_ct_bits[i][j][0] = b0;
      branch_ct_bits[i][j][1] = b1;
    }
  }
  for (i = 0; i < 2; ++i) {
    for (k = 0; k < CLASS0_SIZE; ++k) {
      vp9_tree_probs_from_distribution(vp9_mv_fp_tree,
                                       branch_ct_class0_fp[i][k],
179
                                       nmv_count->comps[i].class0_fp[k]);
180
181
182
    }
    vp9_tree_probs_from_distribution(vp9_mv_fp_tree,
                                     branch_ct_fp[i],
183
                                     nmv_count->comps[i].fp);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  }
  if (usehp) {
    for (i = 0; i < 2; ++i) {
      const uint32_t c0_hp0 = nmv_count->comps[i].class0_hp[0];
      const uint32_t c0_hp1 = nmv_count->comps[i].class0_hp[1];
      const uint32_t hp0 = nmv_count->comps[i].hp[0];
      const uint32_t hp1 = nmv_count->comps[i].hp[1];

      branch_ct_class0_hp[i][0] = c0_hp0;
      branch_ct_class0_hp[i][1] = c0_hp1;

      branch_ct_hp[i][0] = hp0;
      branch_ct_hp[i][1] = hp1;
    }
  }
}

Deb Mukherjee's avatar
Deb Mukherjee committed
201
void vp9_write_nmv_probs(VP9_COMP* const cpi, int usehp, vp9_writer* const bc) {
202
203
204
205
206
207
  int i, j;
  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];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
208
209
  unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][MV_FP_SIZE - 1][2];
  unsigned int branch_ct_fp[2][MV_FP_SIZE - 1][2];
210
211
  unsigned int branch_ct_class0_hp[2][2];
  unsigned int branch_ct_hp[2][2];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
212
213
  nmv_context *mvc = &cpi->common.fc.nmvc;

214
  counts_to_nmv_context(&cpi->NMVcount, usehp,
215
216
217
218
                        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);
219

Dmitry Kovalev's avatar
Dmitry Kovalev committed
220
  for (j = 0; j < MV_JOINTS - 1; ++j)
221
    update_mv(bc, branch_ct_joint[j], &mvc->joints[j], NMV_UPDATE_PROB);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
222

223
  for (i = 0; i < 2; ++i) {
224
    update_mv(bc, branch_ct_sign[i], &mvc->comps[i].sign, NMV_UPDATE_PROB);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
225
226
    for (j = 0; j < MV_CLASSES - 1; ++j)
      update_mv(bc, branch_ct_classes[i][j], &mvc->comps[i].classes[j],
227
                NMV_UPDATE_PROB);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
228
229
230

    for (j = 0; j < CLASS0_SIZE - 1; ++j)
      update_mv(bc, branch_ct_class0[i][j], &mvc->comps[i].class0[j],
231
                NMV_UPDATE_PROB);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
232
233
234

    for (j = 0; j < MV_OFFSET_BITS; ++j)
      update_mv(bc, branch_ct_bits[i][j], &mvc->comps[i].bits[j],
235
                NMV_UPDATE_PROB);
236
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
237

238
239
240
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < CLASS0_SIZE; ++j) {
      int k;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
241
      for (k = 0; k < MV_FP_SIZE - 1; ++k)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
242
        update_mv(bc, branch_ct_class0_fp[i][j][k],
243
                  &mvc->comps[i].class0_fp[j][k], NMV_UPDATE_PROB);
244
    }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
245

Dmitry Kovalev's avatar
Dmitry Kovalev committed
246
    for (j = 0; j < MV_FP_SIZE - 1; ++j)
247
      update_mv(bc, branch_ct_fp[i][j], &mvc->comps[i].fp[j], NMV_UPDATE_PROB);
248
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
249

250
251
  if (usehp) {
    for (i = 0; i < 2; ++i) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
252
      update_mv(bc, branch_ct_class0_hp[i], &mvc->comps[i].class0_hp,
253
                NMV_UPDATE_PROB);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
254
      update_mv(bc, branch_ct_hp[i], &mvc->comps[i].hp,
255
                NMV_UPDATE_PROB);
256
257
258
259
    }
  }
}

Paul Wilkins's avatar
Paul Wilkins committed
260
261
void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w,
                   const MV* mv, const MV* ref,
262
263
264
265
                   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);
266
  usehp = usehp && vp9_use_mv_hp(ref);
267
268

  write_token(w, vp9_mv_joint_tree, mvctx->joints, &vp9_mv_joint_encodings[j]);
269
  if (mv_joint_vertical(j))
270
    encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
271
272

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

275
276
277
278
279
  // 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
280
  }
281
282
}

283
void vp9_build_nmv_cost_table(int *mvjoint,
284
                              int *mvcost[2],
John Koleszar's avatar
John Koleszar committed
285
                              const nmv_context* const mvctx,
286
287
288
                              int usehp,
                              int mvc_flag_v,
                              int mvc_flag_h) {
289
  vp9_clear_system_state();
290
  vp9_cost_tokens(mvjoint, mvctx->joints, vp9_mv_joint_tree);
291
292
293
294
295
  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);
}
Deb Mukherjee's avatar
Deb Mukherjee committed
296

297
298
299
300
301
302
303
304
305
306
307
static void inc_mvs(int_mv mv[2], int_mv ref[2], int is_compound,
                    nmv_context_counts *counts) {
  int i;
  for (i = 0; i < 1 + is_compound; ++i) {
    const MV diff = { mv[i].as_mv.row - ref[i].as_mv.row,
                      mv[i].as_mv.col - ref[i].as_mv.col };
    vp9_inc_mv(&diff, counts);
  }
}

void vp9_update_mv_count(VP9_COMP *cpi, MACROBLOCK *x, int_mv best_ref_mv[2]) {
308
  MODE_INFO *mi = x->e_mbd.mi_8x8[0];
309
  MB_MODE_INFO *const mbmi = &mi->mbmi;
310
  const int is_compound = has_second_ref(mbmi);
Deb Mukherjee's avatar
Deb Mukherjee committed
311

312
  if (mbmi->sb_type < BLOCK_8X8) {
313
314
315
316
317
318
    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) {
319
        const int i = idy * 2 + idx;
320
        if (mi->bmi[i].as_mode == NEWMV)
321
          inc_mvs(mi->bmi[i].as_mv, best_ref_mv, is_compound, &cpi->NMVcount);
Deb Mukherjee's avatar
Deb Mukherjee committed
322
323
324
      }
    }
  } else if (mbmi->mode == NEWMV) {
325
    inc_mvs(mbmi->mv, best_ref_mv, is_compound, &cpi->NMVcount);
Deb Mukherjee's avatar
Deb Mukherjee committed
326
327
  }
}