vp9_findnearmv.c 11.2 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 "vp9/common/vp9_findnearmv.h"
13 14
#include "vp9/common/vp9_sadmxn.h"
#include "vp9/common/vp9_subpelvar.h"
15
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
16

17
const unsigned char vp9_mbsplit_offset[4][16] = {
John Koleszar's avatar
John Koleszar committed
18 19 20 21
  { 0,  8,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0},
  { 0,  2,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0},
  { 0,  2,  8, 10,  0,  0,  0,  0,  0,  0,   0,  0,  0,  0,  0,  0},
  { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15}
22 23
};

24
static void lower_mv_precision(int_mv *mv, int usehp)
25
{
26
  if (!usehp || !vp9_use_nmv_hp(&mv->as_mv)) {
27 28 29 30 31
    if (mv->as_mv.row & 1)
      mv->as_mv.row += (mv->as_mv.row > 0 ? -1 : 1);
    if (mv->as_mv.col & 1)
      mv->as_mv.col += (mv->as_mv.col > 0 ? -1 : 1);
  }
32 33
}

34
vp9_prob *vp9_mv_ref_probs(VP9_COMMON *pc,
Paul Wilkins's avatar
Paul Wilkins committed
35 36 37 38 39 40
                           vp9_prob p[4], const int context
                          ) {
  p[0] = pc->fc.vp9_mode_contexts[context][0];
  p[1] = pc->fc.vp9_mode_contexts[context][1];
  p[2] = pc->fc.vp9_mode_contexts[context][2];
  p[3] = pc->fc.vp9_mode_contexts[context][3];
John Koleszar's avatar
John Koleszar committed
41
  return p;
John Koleszar's avatar
John Koleszar committed
42
}
43

44
#define SP(x) (((x) & 7) << 1)
45 46 47
unsigned int vp9_sad3x16_c(const unsigned char *src_ptr,
                           int  src_stride,
                           const unsigned char *ref_ptr,
Yunqing Wang's avatar
Yunqing Wang committed
48
                           int  ref_stride) {
49 50
  return sad_mx_n_c(src_ptr, src_stride, ref_ptr, ref_stride, 3, 16);
}
51 52 53
unsigned int vp9_sad16x3_c(const unsigned char *src_ptr,
                           int  src_stride,
                           const unsigned char *ref_ptr,
Yunqing Wang's avatar
Yunqing Wang committed
54
                           int  ref_stride) {
55 56 57
  return sad_mx_n_c(src_ptr, src_stride, ref_ptr, ref_stride, 16, 3);
}

58 59 60 61 62 63
#if CONFIG_SUBPELREFMV
unsigned int vp9_variance2x16_c(const unsigned char *src_ptr,
                                const int  source_stride,
                                const unsigned char *ref_ptr,
                                const int  recon_stride,
                                unsigned int *sse) {
Yaowu Xu's avatar
Yaowu Xu committed
64 65 66
  int sum;
  variance(src_ptr, source_stride, ref_ptr, recon_stride, 2, 16, sse, &sum);
  return (*sse - (((unsigned int)sum * sum) >> 5));
67 68 69 70 71 72 73
}

unsigned int vp9_variance16x2_c(const unsigned char *src_ptr,
                                const int  source_stride,
                                const unsigned char *ref_ptr,
                                const int  recon_stride,
                                unsigned int *sse) {
Yaowu Xu's avatar
Yaowu Xu committed
74 75 76
  int sum;
  variance(src_ptr, source_stride, ref_ptr, recon_stride, 16, 2, sse, &sum);
  return (*sse - (((unsigned int)sum * sum) >> 5));
77 78 79 80 81 82 83 84 85
}

unsigned int vp9_sub_pixel_variance16x2_c(const unsigned char  *src_ptr,
                                          const int  src_pixels_per_line,
                                          const int  xoffset,
                                          const int  yoffset,
                                          const unsigned char *dst_ptr,
                                          const int dst_pixels_per_line,
                                          unsigned int *sse) {
86
  unsigned short FData3[16 * 3];  // Temp data buffer used in filtering
Yaowu Xu's avatar
Yaowu Xu committed
87
  unsigned char  temp2[2 * 16];
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  const short *HFilter, *VFilter;

  HFilter = vp9_bilinear_filters[xoffset];
  VFilter = vp9_bilinear_filters[yoffset];

  var_filter_block2d_bil_first_pass(src_ptr, FData3,
                                    src_pixels_per_line, 1, 3, 16, HFilter);
  var_filter_block2d_bil_second_pass(FData3, temp2, 16, 16, 2, 16, VFilter);

  return vp9_variance16x2_c(temp2, 16, dst_ptr, dst_pixels_per_line, sse);
}

unsigned int vp9_sub_pixel_variance2x16_c(const unsigned char  *src_ptr,
                                          const int  src_pixels_per_line,
                                          const int  xoffset,
                                          const int  yoffset,
                                          const unsigned char *dst_ptr,
                                          const int dst_pixels_per_line,
                                          unsigned int *sse) {
107
  unsigned short FData3[2 * 17];  // Temp data buffer used in filtering
108 109 110 111 112 113 114 115 116 117 118 119 120 121
  unsigned char  temp2[2 * 16];
  const short *HFilter, *VFilter;

  HFilter = vp9_bilinear_filters[xoffset];
  VFilter = vp9_bilinear_filters[yoffset];

  var_filter_block2d_bil_first_pass(src_ptr, FData3,
                                    src_pixels_per_line, 1, 17, 2, HFilter);
  var_filter_block2d_bil_second_pass(FData3, temp2, 2, 2, 16, 2, VFilter);

  return vp9_variance2x16_c(temp2, 2, dst_ptr, dst_pixels_per_line, sse);
}
#endif

122 123 124 125
/* check a list of motion vectors by sad score using a number rows of pixels
 * above and a number cols of pixels in the left to select the one with best
 * score to use as ref motion vector
 */
126
void vp9_find_best_ref_mvs(MACROBLOCKD *xd,
Paul Wilkins's avatar
Paul Wilkins committed
127 128
                           unsigned char *ref_y_buffer,
                           int ref_y_stride,
Paul Wilkins's avatar
Paul Wilkins committed
129
                           int_mv *mvlist,
Paul Wilkins's avatar
Paul Wilkins committed
130 131 132 133 134 135 136 137
                           int_mv *best_mv,
                           int_mv *nearest,
                           int_mv *near) {
  int i, j;
  unsigned char *above_src;
  unsigned char *left_src;
  unsigned char *above_ref;
  unsigned char *left_ref;
Yaowu Xu's avatar
Yaowu Xu committed
138
  unsigned int score;
Jim Bankoski's avatar
Jim Bankoski committed
139
#if CONFIG_SUBPELREFMV
Yaowu Xu's avatar
Yaowu Xu committed
140
  unsigned int sse;
Jim Bankoski's avatar
Jim Bankoski committed
141
#endif
Paul Wilkins's avatar
Paul Wilkins committed
142 143
  unsigned int ref_scores[MAX_MV_REF_CANDIDATES] = {0};
  int_mv sorted_mvs[MAX_MV_REF_CANDIDATES];
Paul Wilkins's avatar
Paul Wilkins committed
144 145 146 147 148 149
  int zero_seen = FALSE;

  // Default all to 0,0 if nothing else available
  best_mv->as_int = nearest->as_int = near->as_int = 0;
  vpx_memset(sorted_mvs, 0, sizeof(sorted_mvs));

150
#if CONFIG_SUBPELREFMV
151 152 153 154
  above_src = xd->dst.y_buffer - xd->dst.y_stride * 2;
  left_src  = xd->dst.y_buffer - 2;
  above_ref = ref_y_buffer - ref_y_stride * 2;
  left_ref  = ref_y_buffer - 2;
155 156 157 158 159 160
#else
  above_src = xd->dst.y_buffer - xd->dst.y_stride * 3;
  left_src  = xd->dst.y_buffer - 3;
  above_ref = ref_y_buffer - ref_y_stride * 3;
  left_ref  = ref_y_buffer - 3;
#endif
Paul Wilkins's avatar
Paul Wilkins committed
161

Paul Wilkins's avatar
Paul Wilkins committed
162 163
  // Limit search to the predicted best few candidates
  for(i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
Paul Wilkins's avatar
Paul Wilkins committed
164
    int_mv this_mv;
165
    int offset = 0;
Paul Wilkins's avatar
Paul Wilkins committed
166 167
    int row_offset, col_offset;

Paul Wilkins's avatar
Paul Wilkins committed
168
    this_mv.as_int = mvlist[i].as_int;
Paul Wilkins's avatar
Paul Wilkins committed
169 170 171

    // If we see a 0,0 vector for a second time we have reached the end of
    // the list of valid candidate vectors.
Yaowu Xu's avatar
Yaowu Xu committed
172 173 174 175
    if (!this_mv.as_int && zero_seen)
      break;

    zero_seen = zero_seen || !this_mv.as_int;
Paul Wilkins's avatar
Paul Wilkins committed
176

177 178 179 180 181
    clamp_mv(&this_mv,
             xd->mb_to_left_edge - LEFT_TOP_MARGIN + 24,
             xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN,
             xd->mb_to_top_edge - LEFT_TOP_MARGIN + 24,
             xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN);
Yaowu Xu's avatar
Yaowu Xu committed
182

183
#if CONFIG_SUBPELREFMV
184 185
    row_offset = this_mv.as_mv.row >> 3;
    col_offset = this_mv.as_mv.col >> 3;
Paul Wilkins's avatar
Paul Wilkins committed
186
    offset = ref_y_stride * row_offset + col_offset;
187
    score = 0;
188
    if (xd->up_available) {
Yaowu Xu's avatar
Yaowu Xu committed
189 190 191 192 193
      vp9_sub_pixel_variance16x2_c(above_ref + offset, ref_y_stride,
                                   SP(this_mv.as_mv.col),
                                   SP(this_mv.as_mv.row),
                                   above_src, xd->dst.y_stride, &sse);
      score += sse;
194 195
#if CONFIG_SUPERBLOCKS
      if (xd->mode_info_context->mbmi.encoded_as_sb) {
Yaowu Xu's avatar
Yaowu Xu committed
196 197
        vp9_sub_pixel_variance16x2_c(above_ref + offset + 16,
                                     ref_y_stride,
198 199
                                     SP(this_mv.as_mv.col),
                                     SP(this_mv.as_mv.row),
Yaowu Xu's avatar
Yaowu Xu committed
200 201
                                     above_src + 16, xd->dst.y_stride, &sse);
        score += sse;
202 203
      }
#endif
204 205
    }
    if (xd->left_available) {
Yaowu Xu's avatar
Yaowu Xu committed
206 207 208 209 210
      vp9_sub_pixel_variance2x16_c(left_ref + offset, ref_y_stride,
                                   SP(this_mv.as_mv.col),
                                   SP(this_mv.as_mv.row),
                                   left_src, xd->dst.y_stride, &sse);
      score += sse;
211 212
#if CONFIG_SUPERBLOCKS
      if (xd->mode_info_context->mbmi.encoded_as_sb) {
Yaowu Xu's avatar
Yaowu Xu committed
213 214
        vp9_sub_pixel_variance2x16_c(left_ref + offset + ref_y_stride * 16,
                                     ref_y_stride,
215 216
                                     SP(this_mv.as_mv.col),
                                     SP(this_mv.as_mv.row),
Yaowu Xu's avatar
Yaowu Xu committed
217 218 219
                                     left_src + xd->dst.y_stride * 16,
                                     xd->dst.y_stride, &sse);
        score += sse;
220 221
      }
#endif
222 223 224 225 226 227 228 229 230
    }
#else
    row_offset = (this_mv.as_mv.row > 0) ?
      ((this_mv.as_mv.row + 3) >> 3):((this_mv.as_mv.row + 4) >> 3);
    col_offset = (this_mv.as_mv.col > 0) ?
      ((this_mv.as_mv.col + 3) >> 3):((this_mv.as_mv.col + 4) >> 3);
    offset = ref_y_stride * row_offset + col_offset;
    score = 0;
    if (xd->up_available) {
Yaowu Xu's avatar
Yaowu Xu committed
231
      score += vp9_sad16x3(above_src, xd->dst.y_stride,
Yunqing Wang's avatar
Yunqing Wang committed
232
                           above_ref + offset, ref_y_stride);
233 234
#if CONFIG_SUPERBLOCKS
      if (xd->mode_info_context->mbmi.encoded_as_sb) {
Yaowu Xu's avatar
Yaowu Xu committed
235
        score += vp9_sad16x3(above_src + 16, xd->dst.y_stride,
Yunqing Wang's avatar
Yunqing Wang committed
236
                             above_ref + offset + 16, ref_y_stride);
237 238
      }
#endif
239 240
    }
    if (xd->left_available) {
Yaowu Xu's avatar
Yaowu Xu committed
241
      score += vp9_sad3x16(left_src, xd->dst.y_stride,
Yunqing Wang's avatar
Yunqing Wang committed
242
                           left_ref + offset, ref_y_stride);
243 244
#if CONFIG_SUPERBLOCKS
      if (xd->mode_info_context->mbmi.encoded_as_sb) {
Yaowu Xu's avatar
Yaowu Xu committed
245 246 247
        score += vp9_sad3x16(left_src + xd->dst.y_stride * 16,
                             xd->dst.y_stride,
                             left_ref + offset + ref_y_stride * 16,
Yunqing Wang's avatar
Yunqing Wang committed
248
                             ref_y_stride);
249 250
      }
#endif
251
    }
252
#endif
Paul Wilkins's avatar
Paul Wilkins committed
253
    // Add the entry to our list and then resort the list on score.
254
    ref_scores[i] = score;
Paul Wilkins's avatar
Paul Wilkins committed
255 256 257
    sorted_mvs[i].as_int = this_mv.as_int;
    j = i;
    while (j > 0) {
258 259
      if (ref_scores[j] < ref_scores[j-1]) {
        ref_scores[j] = ref_scores[j-1];
Paul Wilkins's avatar
Paul Wilkins committed
260
        sorted_mvs[j].as_int = sorted_mvs[j-1].as_int;
261
        ref_scores[j-1] = score;
Paul Wilkins's avatar
Paul Wilkins committed
262 263 264 265 266 267 268
        sorted_mvs[j-1].as_int = this_mv.as_int;
        j--;
      } else
        break;
    }
  }

269
  // Make sure all the candidates are properly clamped etc
Paul Wilkins's avatar
Paul Wilkins committed
270
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
271
    lower_mv_precision(&sorted_mvs[i], xd->allow_high_precision_mv);
272
    clamp_mv2(&sorted_mvs[i], xd);
273 274
  }

Paul Wilkins's avatar
Paul Wilkins committed
275 276 277 278 279 280 281
  // Set the best mv to the first entry in the sorted list
  best_mv->as_int = sorted_mvs[0].as_int;

  // Provided that there are non zero vectors available there will not
  // be more than one 0,0 entry in the sorted list.
  // The best ref mv is always set to the first entry (which gave the best
  // results. The nearest is set to the first non zero vector if available and
282
  // near to the second non zero vector if available.
Paul Wilkins's avatar
Paul Wilkins committed
283 284 285 286 287 288 289 290 291 292 293 294
  // We do not use 0,0 as a nearest or near as 0,0 has its own mode.
  if ( sorted_mvs[0].as_int ) {
    nearest->as_int = sorted_mvs[0].as_int;
    if ( sorted_mvs[1].as_int )
      near->as_int = sorted_mvs[1].as_int;
    else
      near->as_int = sorted_mvs[2].as_int;
  } else {
      nearest->as_int = sorted_mvs[1].as_int;
      near->as_int = sorted_mvs[2].as_int;
  }

Paul Wilkins's avatar
Paul Wilkins committed
295 296
  // Copy back the re-ordered mv list
  vpx_memcpy(mvlist, sorted_mvs, sizeof(sorted_mvs));
Paul Wilkins's avatar
Paul Wilkins committed
297
}