vp9_encodemv.c revision ba164dffc5a6795bce97fae02b51ccf3330e15e4
1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12#include "vp9/common/vp9_common.h"
13#include "vp9/encoder/vp9_encodemv.h"
14#include "vp9/common/vp9_entropymode.h"
15#include "vp9/common/vp9_systemdependent.h"
16
17#include <math.h>
18
19#ifdef ENTROPY_STATS
20extern unsigned int active_section;
21#endif
22
23#ifdef NMV_STATS
24nmv_context_counts tnmvcounts;
25#endif
26
27static void encode_mv_component(vp9_writer* w, int comp,
28                                const nmv_component* mvcomp, int usehp) {
29  int offset;
30  const int sign = comp < 0;
31  const int mag = sign ? -comp : comp;
32  const int mv_class = vp9_get_mv_class(mag - 1, &offset);
33  const int d = offset >> 3;                // int mv data
34  const int fr = (offset >> 1) & 3;         // fractional mv data
35  const int hp = offset & 1;                // high precision mv data
36
37  assert(comp != 0);
38
39  // Sign
40  vp9_write(w, sign, mvcomp->sign);
41
42  // Class
43  write_token(w, vp9_mv_class_tree, mvcomp->classes,
44              &vp9_mv_class_encodings[mv_class]);
45
46  // Integer bits
47  if (mv_class == MV_CLASS_0) {
48    write_token(w, vp9_mv_class0_tree, mvcomp->class0,
49                &vp9_mv_class0_encodings[d]);
50  } else {
51    int i;
52    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
53    for (i = 0; i < n; ++i)
54      vp9_write(w, (d >> i) & 1, mvcomp->bits[i]);
55  }
56
57  // Fractional bits
58  write_token(w, vp9_mv_fp_tree,
59              mv_class == MV_CLASS_0 ?  mvcomp->class0_fp[d] : mvcomp->fp,
60              &vp9_mv_fp_encodings[fr]);
61
62  // High precision bit
63  if (usehp)
64    vp9_write(w, hp,
65              mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
66}
67
68
69static void build_nmv_component_cost_table(int *mvcost,
70                                           const nmv_component* const mvcomp,
71                                           int usehp) {
72  int i, v;
73  int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
74  int bits_cost[MV_OFFSET_BITS][2];
75  int class0_fp_cost[CLASS0_SIZE][4], fp_cost[4];
76  int class0_hp_cost[2], hp_cost[2];
77
78  sign_cost[0] = vp9_cost_zero(mvcomp->sign);
79  sign_cost[1] = vp9_cost_one(mvcomp->sign);
80  vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree);
81  vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree);
82  for (i = 0; i < MV_OFFSET_BITS; ++i) {
83    bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
84    bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
85  }
86
87  for (i = 0; i < CLASS0_SIZE; ++i)
88    vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree);
89  vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree);
90
91  if (usehp) {
92    class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp);
93    class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp);
94    hp_cost[0] = vp9_cost_zero(mvcomp->hp);
95    hp_cost[1] = vp9_cost_one(mvcomp->hp);
96  }
97  mvcost[0] = 0;
98  for (v = 1; v <= MV_MAX; ++v) {
99    int z, c, o, d, e, f, cost = 0;
100    z = v - 1;
101    c = vp9_get_mv_class(z, &o);
102    cost += class_cost[c];
103    d = (o >> 3);               /* int mv data */
104    f = (o >> 1) & 3;           /* fractional pel mv data */
105    e = (o & 1);                /* high precision mv data */
106    if (c == MV_CLASS_0) {
107      cost += class0_cost[d];
108    } else {
109      int i, b;
110      b = c + CLASS0_BITS - 1;  /* number of bits */
111      for (i = 0; i < b; ++i)
112        cost += bits_cost[i][((d >> i) & 1)];
113    }
114    if (c == MV_CLASS_0) {
115      cost += class0_fp_cost[d][f];
116    } else {
117      cost += fp_cost[f];
118    }
119    if (usehp) {
120      if (c == MV_CLASS_0) {
121        cost += class0_hp_cost[e];
122      } else {
123        cost += hp_cost[e];
124      }
125    }
126    mvcost[v] = cost + sign_cost[0];
127    mvcost[-v] = cost + sign_cost[1];
128  }
129}
130
131static int update_nmv_savings(const unsigned int ct[2],
132                              const vp9_prob cur_p,
133                              const vp9_prob new_p,
134                              const vp9_prob upd_p) {
135
136#ifdef LOW_PRECISION_MV_UPDATE
137  vp9_prob mod_p = new_p | 1;
138#else
139  vp9_prob mod_p = new_p;
140#endif
141  const int cur_b = cost_branch256(ct, cur_p);
142  const int mod_b = cost_branch256(ct, mod_p);
143  const int cost = 7 * 256 +
144#ifndef LOW_PRECISION_MV_UPDATE
145      256 +
146#endif
147      (vp9_cost_one(upd_p) - vp9_cost_zero(upd_p));
148  if (cur_b - mod_b - cost > 0) {
149    return cur_b - mod_b - cost;
150  } else {
151    return 0 - vp9_cost_zero(upd_p);
152  }
153}
154
155static int update_nmv(
156  vp9_writer *const bc,
157  const unsigned int ct[2],
158  vp9_prob *const cur_p,
159  const vp9_prob new_p,
160  const vp9_prob upd_p) {
161
162#ifdef LOW_PRECISION_MV_UPDATE
163  vp9_prob mod_p = new_p | 1;
164#else
165  vp9_prob mod_p = new_p;
166#endif
167
168  const int cur_b = cost_branch256(ct, *cur_p);
169  const int mod_b = cost_branch256(ct, mod_p);
170  const int cost = 7 * 256 +
171#ifndef LOW_PRECISION_MV_UPDATE
172      256 +
173#endif
174      (vp9_cost_one(upd_p) - vp9_cost_zero(upd_p));
175
176  if (cur_b - mod_b > cost) {
177    *cur_p = mod_p;
178    vp9_write(bc, 1, upd_p);
179#ifdef LOW_PRECISION_MV_UPDATE
180    vp9_write_literal(bc, mod_p >> 1, 7);
181#else
182    vp9_write_literal(bc, mod_p, 8);
183#endif
184    return 1;
185  } else {
186    vp9_write(bc, 0, upd_p);
187    return 0;
188  }
189}
190
191void print_nmvcounts(nmv_context_counts tnmvcounts) {
192  int i, j, k;
193  printf("\nCounts =\n  { ");
194  for (j = 0; j < MV_JOINTS; ++j)
195    printf("%d, ", tnmvcounts.joints[j]);
196  printf("},\n");
197  for (i = 0; i < 2; ++i) {
198    printf("  {\n");
199    printf("    %d/%d,\n", tnmvcounts.comps[i].sign[0],
200                           tnmvcounts.comps[i].sign[1]);
201    printf("    { ");
202    for (j = 0; j < MV_CLASSES; ++j)
203      printf("%d, ", tnmvcounts.comps[i].classes[j]);
204    printf("},\n");
205    printf("    { ");
206    for (j = 0; j < CLASS0_SIZE; ++j)
207      printf("%d, ", tnmvcounts.comps[i].class0[j]);
208    printf("},\n");
209    printf("    { ");
210    for (j = 0; j < MV_OFFSET_BITS; ++j)
211      printf("%d/%d, ", tnmvcounts.comps[i].bits[j][0],
212                        tnmvcounts.comps[i].bits[j][1]);
213    printf("},\n");
214
215    printf("    {");
216    for (j = 0; j < CLASS0_SIZE; ++j) {
217      printf("{");
218      for (k = 0; k < 4; ++k)
219        printf("%d, ", tnmvcounts.comps[i].class0_fp[j][k]);
220      printf("}, ");
221    }
222    printf("},\n");
223
224    printf("    { ");
225    for (j = 0; j < 4; ++j)
226      printf("%d, ", tnmvcounts.comps[i].fp[j]);
227    printf("},\n");
228
229    printf("    %d/%d,\n",
230           tnmvcounts.comps[i].class0_hp[0],
231           tnmvcounts.comps[i].class0_hp[1]);
232    printf("    %d/%d,\n",
233           tnmvcounts.comps[i].hp[0],
234           tnmvcounts.comps[i].hp[1]);
235    printf("  },\n");
236  }
237}
238
239#ifdef NMV_STATS
240void init_nmvstats() {
241  vp9_zero(tnmvcounts);
242}
243
244void print_nmvstats() {
245  nmv_context prob;
246  unsigned int branch_ct_joint[MV_JOINTS - 1][2];
247  unsigned int branch_ct_sign[2][2];
248  unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
249  unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
250  unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
251  unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
252  unsigned int branch_ct_fp[2][4 - 1][2];
253  unsigned int branch_ct_class0_hp[2][2];
254  unsigned int branch_ct_hp[2][2];
255  int i, j, k;
256  vp9_counts_to_nmv_context(&tnmvcounts, &prob, 1,
257                            branch_ct_joint, branch_ct_sign, branch_ct_classes,
258                            branch_ct_class0, branch_ct_bits,
259                            branch_ct_class0_fp, branch_ct_fp,
260                            branch_ct_class0_hp, branch_ct_hp);
261
262  printf("\nCounts =\n  { ");
263  for (j = 0; j < MV_JOINTS; ++j)
264    printf("%d, ", tnmvcounts.joints[j]);
265  printf("},\n");
266  for (i = 0; i < 2; ++i) {
267    printf("  {\n");
268    printf("    %d/%d,\n", tnmvcounts.comps[i].sign[0],
269                           tnmvcounts.comps[i].sign[1]);
270    printf("    { ");
271    for (j = 0; j < MV_CLASSES; ++j)
272      printf("%d, ", tnmvcounts.comps[i].classes[j]);
273    printf("},\n");
274    printf("    { ");
275    for (j = 0; j < CLASS0_SIZE; ++j)
276      printf("%d, ", tnmvcounts.comps[i].class0[j]);
277    printf("},\n");
278    printf("    { ");
279    for (j = 0; j < MV_OFFSET_BITS; ++j)
280      printf("%d/%d, ", tnmvcounts.comps[i].bits[j][0],
281                        tnmvcounts.comps[i].bits[j][1]);
282    printf("},\n");
283
284    printf("    {");
285    for (j = 0; j < CLASS0_SIZE; ++j) {
286      printf("{");
287      for (k = 0; k < 4; ++k)
288        printf("%d, ", tnmvcounts.comps[i].class0_fp[j][k]);
289      printf("}, ");
290    }
291    printf("},\n");
292
293    printf("    { ");
294    for (j = 0; j < 4; ++j)
295      printf("%d, ", tnmvcounts.comps[i].fp[j]);
296    printf("},\n");
297
298    printf("    %d/%d,\n",
299           tnmvcounts.comps[i].class0_hp[0],
300           tnmvcounts.comps[i].class0_hp[1]);
301    printf("    %d/%d,\n",
302           tnmvcounts.comps[i].hp[0],
303           tnmvcounts.comps[i].hp[1]);
304    printf("  },\n");
305  }
306
307  printf("\nProbs =\n  { ");
308  for (j = 0; j < MV_JOINTS - 1; ++j)
309    printf("%d, ", prob.joints[j]);
310  printf("},\n");
311  for (i=0; i< 2; ++i) {
312    printf("  {\n");
313    printf("    %d,\n", prob.comps[i].sign);
314    printf("    { ");
315    for (j = 0; j < MV_CLASSES - 1; ++j)
316      printf("%d, ", prob.comps[i].classes[j]);
317    printf("},\n");
318    printf("    { ");
319    for (j = 0; j < CLASS0_SIZE - 1; ++j)
320      printf("%d, ", prob.comps[i].class0[j]);
321    printf("},\n");
322    printf("    { ");
323    for (j = 0; j < MV_OFFSET_BITS; ++j)
324      printf("%d, ", prob.comps[i].bits[j]);
325    printf("},\n");
326    printf("    { ");
327    for (j = 0; j < CLASS0_SIZE; ++j) {
328      printf("{");
329      for (k = 0; k < 3; ++k)
330        printf("%d, ", prob.comps[i].class0_fp[j][k]);
331      printf("}, ");
332    }
333    printf("},\n");
334    printf("    { ");
335    for (j = 0; j < 3; ++j)
336      printf("%d, ", prob.comps[i].fp[j]);
337    printf("},\n");
338
339    printf("    %d,\n", prob.comps[i].class0_hp);
340    printf("    %d,\n", prob.comps[i].hp);
341    printf("  },\n");
342  }
343}
344
345static void add_nmvcount(nmv_context_counts* const dst,
346                         const nmv_context_counts* const src) {
347  int i, j, k;
348  for (j = 0; j < MV_JOINTS; ++j) {
349    dst->joints[j] += src->joints[j];
350  }
351  for (i = 0; i < 2; ++i) {
352    for (j = 0; j < MV_VALS; ++j) {
353      dst->comps[i].mvcount[j] += src->comps[i].mvcount[j];
354    }
355    dst->comps[i].sign[0] += src->comps[i].sign[0];
356    dst->comps[i].sign[1] += src->comps[i].sign[1];
357    for (j = 0; j < MV_CLASSES; ++j) {
358      dst->comps[i].classes[j] += src->comps[i].classes[j];
359    }
360    for (j = 0; j < CLASS0_SIZE; ++j) {
361      dst->comps[i].class0[j] += src->comps[i].class0[j];
362    }
363    for (j = 0; j < MV_OFFSET_BITS; ++j) {
364      dst->comps[i].bits[j][0] += src->comps[i].bits[j][0];
365      dst->comps[i].bits[j][1] += src->comps[i].bits[j][1];
366    }
367  }
368  for (i = 0; i < 2; ++i) {
369    for (j = 0; j < CLASS0_SIZE; ++j) {
370      for (k = 0; k < 4; ++k) {
371        dst->comps[i].class0_fp[j][k] += src->comps[i].class0_fp[j][k];
372      }
373    }
374    for (j = 0; j < 4; ++j) {
375      dst->comps[i].fp[j] += src->comps[i].fp[j];
376    }
377    dst->comps[i].class0_hp[0] += src->comps[i].class0_hp[0];
378    dst->comps[i].class0_hp[1] += src->comps[i].class0_hp[1];
379    dst->comps[i].hp[0] += src->comps[i].hp[0];
380    dst->comps[i].hp[1] += src->comps[i].hp[1];
381  }
382}
383#endif
384
385void vp9_write_nmv_probs(VP9_COMP* const cpi, int usehp, vp9_writer* const bc) {
386  int i, j;
387  nmv_context prob;
388  unsigned int branch_ct_joint[MV_JOINTS - 1][2];
389  unsigned int branch_ct_sign[2][2];
390  unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
391  unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
392  unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
393  unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
394  unsigned int branch_ct_fp[2][4 - 1][2];
395  unsigned int branch_ct_class0_hp[2][2];
396  unsigned int branch_ct_hp[2][2];
397#ifdef MV_GROUP_UPDATE
398  int savings = 0;
399#endif
400
401#ifdef NMV_STATS
402  if (!cpi->dummy_packing)
403    add_nmvcount(&tnmvcounts, &cpi->NMVcount);
404#endif
405  vp9_counts_to_nmv_context(&cpi->NMVcount, &prob, usehp,
406                            branch_ct_joint, branch_ct_sign, branch_ct_classes,
407                            branch_ct_class0, branch_ct_bits,
408                            branch_ct_class0_fp, branch_ct_fp,
409                            branch_ct_class0_hp, branch_ct_hp);
410  /* write updates if they help */
411#ifdef MV_GROUP_UPDATE
412  for (j = 0; j < MV_JOINTS - 1; ++j) {
413    savings += update_nmv_savings(branch_ct_joint[j],
414                                  cpi->common.fc.nmvc.joints[j],
415                                  prob.joints[j],
416                                  VP9_NMV_UPDATE_PROB);
417  }
418  for (i = 0; i < 2; ++i) {
419    savings += update_nmv_savings(branch_ct_sign[i],
420                                  cpi->common.fc.nmvc.comps[i].sign,
421                                  prob.comps[i].sign,
422                                  VP9_NMV_UPDATE_PROB);
423    for (j = 0; j < MV_CLASSES - 1; ++j) {
424      savings += update_nmv_savings(branch_ct_classes[i][j],
425                                    cpi->common.fc.nmvc.comps[i].classes[j],
426                                    prob.comps[i].classes[j],
427                                    VP9_NMV_UPDATE_PROB);
428    }
429    for (j = 0; j < CLASS0_SIZE - 1; ++j) {
430      savings += update_nmv_savings(branch_ct_class0[i][j],
431                                    cpi->common.fc.nmvc.comps[i].class0[j],
432                                    prob.comps[i].class0[j],
433                                    VP9_NMV_UPDATE_PROB);
434    }
435    for (j = 0; j < MV_OFFSET_BITS; ++j) {
436      savings += update_nmv_savings(branch_ct_bits[i][j],
437                                    cpi->common.fc.nmvc.comps[i].bits[j],
438                                    prob.comps[i].bits[j],
439                                    VP9_NMV_UPDATE_PROB);
440    }
441  }
442  for (i = 0; i < 2; ++i) {
443    for (j = 0; j < CLASS0_SIZE; ++j) {
444      int k;
445      for (k = 0; k < 3; ++k) {
446        savings += update_nmv_savings(branch_ct_class0_fp[i][j][k],
447                                      cpi->common.fc.nmvc.comps[i].class0_fp[j][k],
448                                      prob.comps[i].class0_fp[j][k],
449                                      VP9_NMV_UPDATE_PROB);
450      }
451    }
452    for (j = 0; j < 3; ++j) {
453      savings += update_nmv_savings(branch_ct_fp[i][j],
454                                    cpi->common.fc.nmvc.comps[i].fp[j],
455                                    prob.comps[i].fp[j],
456                                    VP9_NMV_UPDATE_PROB);
457    }
458  }
459  if (usehp) {
460    for (i = 0; i < 2; ++i) {
461      savings += update_nmv_savings(branch_ct_class0_hp[i],
462                                    cpi->common.fc.nmvc.comps[i].class0_hp,
463                                    prob.comps[i].class0_hp,
464                                    VP9_NMV_UPDATE_PROB);
465      savings += update_nmv_savings(branch_ct_hp[i],
466                                    cpi->common.fc.nmvc.comps[i].hp,
467                                    prob.comps[i].hp,
468                                    VP9_NMV_UPDATE_PROB);
469    }
470  }
471  if (savings <= 0) {
472    vp9_write_bit(bc, 0);
473    return;
474  }
475  vp9_write_bit(bc, 1);
476#endif
477
478  for (j = 0; j < MV_JOINTS - 1; ++j) {
479    update_nmv(bc, branch_ct_joint[j],
480               &cpi->common.fc.nmvc.joints[j],
481               prob.joints[j],
482               VP9_NMV_UPDATE_PROB);
483  }
484  for (i = 0; i < 2; ++i) {
485    update_nmv(bc, branch_ct_sign[i],
486               &cpi->common.fc.nmvc.comps[i].sign,
487               prob.comps[i].sign,
488               VP9_NMV_UPDATE_PROB);
489    for (j = 0; j < MV_CLASSES - 1; ++j) {
490      update_nmv(bc, branch_ct_classes[i][j],
491                 &cpi->common.fc.nmvc.comps[i].classes[j],
492                 prob.comps[i].classes[j],
493                 VP9_NMV_UPDATE_PROB);
494    }
495    for (j = 0; j < CLASS0_SIZE - 1; ++j) {
496      update_nmv(bc, branch_ct_class0[i][j],
497                 &cpi->common.fc.nmvc.comps[i].class0[j],
498                 prob.comps[i].class0[j],
499                 VP9_NMV_UPDATE_PROB);
500    }
501    for (j = 0; j < MV_OFFSET_BITS; ++j) {
502      update_nmv(bc, branch_ct_bits[i][j],
503                 &cpi->common.fc.nmvc.comps[i].bits[j],
504                 prob.comps[i].bits[j],
505                 VP9_NMV_UPDATE_PROB);
506    }
507  }
508  for (i = 0; i < 2; ++i) {
509    for (j = 0; j < CLASS0_SIZE; ++j) {
510      int k;
511      for (k = 0; k < 3; ++k) {
512        update_nmv(bc, branch_ct_class0_fp[i][j][k],
513                   &cpi->common.fc.nmvc.comps[i].class0_fp[j][k],
514                   prob.comps[i].class0_fp[j][k],
515                   VP9_NMV_UPDATE_PROB);
516      }
517    }
518    for (j = 0; j < 3; ++j) {
519      update_nmv(bc, branch_ct_fp[i][j],
520                 &cpi->common.fc.nmvc.comps[i].fp[j],
521                 prob.comps[i].fp[j],
522                 VP9_NMV_UPDATE_PROB);
523    }
524  }
525  if (usehp) {
526    for (i = 0; i < 2; ++i) {
527      update_nmv(bc, branch_ct_class0_hp[i],
528                 &cpi->common.fc.nmvc.comps[i].class0_hp,
529                 prob.comps[i].class0_hp,
530                 VP9_NMV_UPDATE_PROB);
531      update_nmv(bc, branch_ct_hp[i],
532                 &cpi->common.fc.nmvc.comps[i].hp,
533                 prob.comps[i].hp,
534                 VP9_NMV_UPDATE_PROB);
535    }
536  }
537}
538
539void vp9_encode_mv(vp9_writer* w, const MV* mv, const MV* ref,
540                   const nmv_context* mvctx, int usehp) {
541  const MV diff = {mv->row - ref->row,
542                   mv->col - ref->col};
543  const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff);
544  usehp = usehp && vp9_use_nmv_hp(ref);
545
546  write_token(w, vp9_mv_joint_tree, mvctx->joints, &vp9_mv_joint_encodings[j]);
547  if (mv_joint_vertical(j))
548    encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
549
550  if (mv_joint_horizontal(j))
551    encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
552}
553
554void vp9_build_nmv_cost_table(int *mvjoint,
555                              int *mvcost[2],
556                              const nmv_context* const mvctx,
557                              int usehp,
558                              int mvc_flag_v,
559                              int mvc_flag_h) {
560  vp9_clear_system_state();
561  vp9_cost_tokens(mvjoint, mvctx->joints, vp9_mv_joint_tree);
562  if (mvc_flag_v)
563    build_nmv_component_cost_table(mvcost[0], &mvctx->comps[0], usehp);
564  if (mvc_flag_h)
565    build_nmv_component_cost_table(mvcost[1], &mvctx->comps[1], usehp);
566}
567
568void vp9_update_nmv_count(VP9_COMP *cpi, MACROBLOCK *x,
569                         int_mv *best_ref_mv, int_mv *second_best_ref_mv) {
570  MB_MODE_INFO * mbmi = &x->e_mbd.mode_info_context->mbmi;
571  MV mv;
572  int bwl = b_width_log2(mbmi->sb_type), bw = 1 << bwl;
573  int bhl = b_height_log2(mbmi->sb_type), bh = 1 << bhl;
574  int idx, idy;
575
576  if (mbmi->sb_type < BLOCK_SIZE_SB8X8) {
577    int i;
578    PARTITION_INFO *pi = x->partition_info;
579    for (idy = 0; idy < 2; idy += bh) {
580      for (idx = 0; idx < 2; idx += bw) {
581        i = idy * 2 + idx;
582        if (pi->bmi[i].mode == NEWMV) {
583          mv.row = (pi->bmi[i].mv.as_mv.row - best_ref_mv->as_mv.row);
584          mv.col = (pi->bmi[i].mv.as_mv.col - best_ref_mv->as_mv.col);
585          vp9_increment_nmv(&mv, &best_ref_mv->as_mv, &cpi->NMVcount,
586                            x->e_mbd.allow_high_precision_mv);
587          if (x->e_mbd.mode_info_context->mbmi.ref_frame[1] > INTRA_FRAME) {
588            mv.row = pi->bmi[i].second_mv.as_mv.row -
589                         second_best_ref_mv->as_mv.row;
590            mv.col = pi->bmi[i].second_mv.as_mv.col -
591                         second_best_ref_mv->as_mv.col;
592            vp9_increment_nmv(&mv, &second_best_ref_mv->as_mv, &cpi->NMVcount,
593                              x->e_mbd.allow_high_precision_mv);
594          }
595        }
596      }
597    }
598  } else if (mbmi->mode == NEWMV) {
599    mv.row = (mbmi->mv[0].as_mv.row - best_ref_mv->as_mv.row);
600    mv.col = (mbmi->mv[0].as_mv.col - best_ref_mv->as_mv.col);
601    vp9_increment_nmv(&mv, &best_ref_mv->as_mv, &cpi->NMVcount,
602                      x->e_mbd.allow_high_precision_mv);
603    if (mbmi->ref_frame[1] > INTRA_FRAME) {
604      mv.row = (mbmi->mv[1].as_mv.row - second_best_ref_mv->as_mv.row);
605      mv.col = (mbmi->mv[1].as_mv.col - second_best_ref_mv->as_mv.col);
606      vp9_increment_nmv(&mv, &second_best_ref_mv->as_mv, &cpi->NMVcount,
607                        x->e_mbd.allow_high_precision_mv);
608    }
609  }
610}
611