vp9_mvref_common.c 14.2 KB
Newer Older
Paul Wilkins's avatar
Paul Wilkins committed
1
2
3
4
5
6
7
8
9
10
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

11
#include "vp9/common/vp9_mvref_common.h"
Paul Wilkins's avatar
Paul Wilkins committed
12
13

#define MVREF_NEIGHBOURS 8
Dmitry Kovalev's avatar
Dmitry Kovalev committed
14

15
16
17
18
static int mb_mv_ref_search[MVREF_NEIGHBOURS][2] = {
    {0, -1}, {-1, 0}, {-1, -1}, {0, -2},
    {-2, 0}, {-1, -2}, {-2, -1}, {-2, -2}
};
Dmitry Kovalev's avatar
Dmitry Kovalev committed
19

20
21
static int mb_ref_distance_weight[MVREF_NEIGHBOURS] =
  { 3, 3, 2, 1, 1, 1, 1, 1 };
Dmitry Kovalev's avatar
Dmitry Kovalev committed
22

23
24
25
26
static int sb_mv_ref_search[MVREF_NEIGHBOURS][2] = {
    {0, -1}, {-1, 0}, {1, -1}, {-1, 1},
    {-1, -1}, {0, -2}, {-2, 0}, {-1, -2}
};
Dmitry Kovalev's avatar
Dmitry Kovalev committed
27

28
29
static int sb_ref_distance_weight[MVREF_NEIGHBOURS] =
  { 3, 3, 2, 2, 2, 1, 1, 1 };
30

31
32
33
34
35
36
37
38
39
40
41
42


static int sb64_mv_ref_search[MVREF_NEIGHBOURS][2] = {
    {0, -1}, {-1, 0}, {1, -1}, {-1, 1},
    {2, -1}, {-1, 2}, {3, -1}, {-1,-1}
};

static int sb64_ref_distance_weight[MVREF_NEIGHBOURS] =
  { 1, 1, 1, 1, 1, 1, 1, 1 };



Paul Wilkins's avatar
Paul Wilkins committed
43
// clamp_mv_ref
Paul Wilkins's avatar
Paul Wilkins committed
44
45
#define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units

Dmitry Kovalev's avatar
Dmitry Kovalev committed
46
static void clamp_mv_ref(const MACROBLOCKD *xd, int_mv *mv) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
47
48
49
50
  mv->as_mv.col = clamp(mv->as_mv.col, xd->mb_to_left_edge - MV_BORDER,
                                       xd->mb_to_right_edge + MV_BORDER);
  mv->as_mv.row = clamp(mv->as_mv.row, xd->mb_to_top_edge - MV_BORDER,
                                       xd->mb_to_bottom_edge + MV_BORDER);
Paul Wilkins's avatar
Paul Wilkins committed
51
52
}

53
54
// Gets a candidate refenence motion vector from the given mode info
// structure if one exists that matches the given reference frame.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
55
static int get_matching_candidate(const MODE_INFO *candidate_mi,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
56
57
                                  MV_REFERENCE_FRAME ref_frame,
                                  int_mv *c_mv) {
58
59
60
61
62
  if (ref_frame == candidate_mi->mbmi.ref_frame) {
    c_mv->as_int = candidate_mi->mbmi.mv[0].as_int;
  } else if (ref_frame == candidate_mi->mbmi.second_ref_frame) {
    c_mv->as_int = candidate_mi->mbmi.mv[1].as_int;
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
63
    return 0;
64
65
  }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
66
  return 1;
67
}
Paul Wilkins's avatar
Paul Wilkins committed
68

69
70
// Gets candidate refenence motion vector(s) from the given mode info
// structure if they exists and do NOT match the given reference frame.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
71
72
73
74
75
76
static void get_non_matching_candidates(const MODE_INFO *candidate_mi,
                                        MV_REFERENCE_FRAME ref_frame,
                                        MV_REFERENCE_FRAME *c_ref_frame,
                                        int_mv *c_mv,
                                        MV_REFERENCE_FRAME *c2_ref_frame,
                                        int_mv *c2_mv) {
Paul Wilkins's avatar
Paul Wilkins committed
77

78
  c_mv->as_int = 0;
Paul Wilkins's avatar
Paul Wilkins committed
79
  c2_mv->as_int = 0;
80
  *c_ref_frame = INTRA_FRAME;
Paul Wilkins's avatar
Paul Wilkins committed
81
  *c2_ref_frame = INTRA_FRAME;
Paul Wilkins's avatar
Paul Wilkins committed
82

83
84
85
86
87
88
  // If first candidate not valid neither will be.
  if (candidate_mi->mbmi.ref_frame > INTRA_FRAME) {
    // First candidate
    if (candidate_mi->mbmi.ref_frame != ref_frame) {
      *c_ref_frame = candidate_mi->mbmi.ref_frame;
      c_mv->as_int = candidate_mi->mbmi.mv[0].as_int;
Paul Wilkins's avatar
Paul Wilkins committed
89
90
    }

91
    // Second candidate
92
    if ((candidate_mi->mbmi.second_ref_frame > INTRA_FRAME) &&
Paul Wilkins's avatar
Paul Wilkins committed
93
        (candidate_mi->mbmi.second_ref_frame != ref_frame) &&
Dmitry Kovalev's avatar
Dmitry Kovalev committed
94
        (candidate_mi->mbmi.mv[1].as_int != candidate_mi->mbmi.mv[0].as_int)) {
Paul Wilkins's avatar
Paul Wilkins committed
95
      *c2_ref_frame = candidate_mi->mbmi.second_ref_frame;
96
      c2_mv->as_int = candidate_mi->mbmi.mv[1].as_int;
Paul Wilkins's avatar
Paul Wilkins committed
97
    }
Paul Wilkins's avatar
Paul Wilkins committed
98
99
100
  }
}

Paul Wilkins's avatar
Paul Wilkins committed
101
102

// Performs mv sign inversion if indicated by the reference frame combination.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
103
104
105
static void scale_mv(MACROBLOCKD *xd, MV_REFERENCE_FRAME this_ref_frame,
                     MV_REFERENCE_FRAME candidate_ref_frame,
                     int_mv *candidate_mv, int *ref_sign_bias) {
Paul Wilkins's avatar
Paul Wilkins committed
106
107
108
109
110
111
112
113
114
  // int frame_distances[MAX_REF_FRAMES];
  // int last_distance = 1;
  // int gf_distance = xd->frames_since_golden;
  // int arf_distance = xd->frames_till_alt_ref_frame;

  // Sign inversion where appropriate.
  if (ref_sign_bias[candidate_ref_frame] != ref_sign_bias[this_ref_frame]) {
    candidate_mv->as_mv.row = -candidate_mv->as_mv.row;
    candidate_mv->as_mv.col = -candidate_mv->as_mv.col;
Paul Wilkins's avatar
Paul Wilkins committed
115
116
  }

Paul Wilkins's avatar
Paul Wilkins committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  /*
  // Scale based on frame distance if the reference frames not the same.
  frame_distances[INTRA_FRAME] = 1;   // should never be used
  frame_distances[LAST_FRAME] = 1;
  frame_distances[GOLDEN_FRAME] =
    (xd->frames_since_golden) ? xd->frames_si nce_golden : 1;
  frame_distances[ALTREF_FRAME] =
    (xd->frames_till_alt_ref_frame) ? xd->frames_till_alt_ref_frame : 1;

  if (frame_distances[this_ref_frame] &&
      frame_distances[candidate_ref_frame]) {
    candidate_mv->as_mv.row =
      (short)(((int)(candidate_mv->as_mv.row) *
               frame_distances[this_ref_frame]) /
              frame_distances[candidate_ref_frame]);

    candidate_mv->as_mv.col =
      (short)(((int)(candidate_mv->as_mv.col) *
               frame_distances[this_ref_frame]) /
              frame_distances[candidate_ref_frame]);
  }
  */
Paul Wilkins's avatar
Paul Wilkins committed
139
140
}

Paul Wilkins's avatar
Paul Wilkins committed
141
142
143
144
145
146
147
148
149
150
/*
// Adds a new candidate reference vector to the sorted list.
// If it is a repeat the weight of the existing entry is increased
// and the order of the list is resorted.
// This method of add plus sort has been deprecated for now as there is a
// further sort of the best candidates in vp9_find_best_ref_mvs() and the
// incremental benefit of both is small. If the decision is made to remove
// the sort in vp9_find_best_ref_mvs() for performance reasons then it may be
// worth re-instating some sort of list reordering by weight here.
//
Paul Wilkins's avatar
Paul Wilkins committed
151
static void addmv_and_shuffle(
Paul Wilkins's avatar
Paul Wilkins committed
152
153
  int_mv *mv_list,
  int *mv_scores,
Paul Wilkins's avatar
Paul Wilkins committed
154
  int *refmv_count,
Paul Wilkins's avatar
Paul Wilkins committed
155
156
157
158
  int_mv candidate_mv,
  int weight
) {

Paul Wilkins's avatar
Paul Wilkins committed
159
160
  int i;
  int insert_point;
Paul Wilkins's avatar
Paul Wilkins committed
161
162
  int duplicate_found = FALSE;

Paul Wilkins's avatar
Paul Wilkins committed
163
164
  // Check for duplicates. If there is one increase its score.
  // We only compare vs the current top candidates.
Paul Wilkins's avatar
Paul Wilkins committed
165
166
  insert_point = (*refmv_count < (MAX_MV_REF_CANDIDATES - 1))
                 ? *refmv_count : (MAX_MV_REF_CANDIDATES - 1);
Paul Wilkins's avatar
Paul Wilkins committed
167
168

  i = insert_point;
Paul Wilkins's avatar
Paul Wilkins committed
169
  if (*refmv_count > i)
Paul Wilkins's avatar
Paul Wilkins committed
170
    i++;
Paul Wilkins's avatar
Paul Wilkins committed
171
172
173
174
175
176
177
178
179
  while (i > 0) {
    i--;
    if (candidate_mv.as_int == mv_list[i].as_int) {
      duplicate_found = TRUE;
      mv_scores[i] += weight;
      break;
    }
  }

Paul Wilkins's avatar
Paul Wilkins committed
180
181
182
183
184
185
186
  // If no duplicate and the new candidate is good enough then add it.
  if (!duplicate_found ) {
    if (weight > mv_scores[insert_point]) {
      mv_list[insert_point].as_int = candidate_mv.as_int;
      mv_scores[insert_point] = weight;
      i = insert_point;
    }
Paul Wilkins's avatar
Paul Wilkins committed
187
    (*refmv_count)++;
Paul Wilkins's avatar
Paul Wilkins committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  }

  // Reshuffle the list so that highest scoring mvs at the top.
  while (i > 0) {
    if (mv_scores[i] > mv_scores[i-1]) {
      int tmp_score = mv_scores[i-1];
      int_mv tmp_mv = mv_list[i-1];

      mv_scores[i-1] = mv_scores[i];
      mv_list[i-1] = mv_list[i];
      mv_scores[i] = tmp_score;
      mv_list[i] = tmp_mv;
      i--;
    } else
      break;
  }
}
Paul Wilkins's avatar
Paul Wilkins committed
205
206
207
208
209
210
211
*/

// Adds a new candidate reference vector to the list.
// The mv is thrown out if it is already in the list.
// Unlike the addmv_and_shuffle() this does not reorder the list
// but assumes that candidates are added in the order most likely to
// match distance and reference frame bias.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
212
213
214
static void add_candidate_mv(int_mv *mv_list,  int *mv_scores,
                             int *candidate_count, int_mv candidate_mv,
                             int weight) {
Paul Wilkins's avatar
Paul Wilkins committed
215
216
217
  int i;

  // Make sure we dont insert off the end of the list
Dmitry Kovalev's avatar
Dmitry Kovalev committed
218
  const int insert_point = MIN(*candidate_count, MAX_MV_REF_CANDIDATES - 1);
Paul Wilkins's avatar
Paul Wilkins committed
219
220
221
222
223
224
225
226
227

  // Look for duplicates
  for (i = 0; i <= insert_point; ++i) {
    if (candidate_mv.as_int == mv_list[i].as_int)
      break;
  }

  // Add the candidate. If the list is already full it is only desirable that
  // it should overwrite if it has a higher weight than the last entry.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
228
  if (i >= insert_point && weight > mv_scores[insert_point]) {
Paul Wilkins's avatar
Paul Wilkins committed
229
230
231
232
233
    mv_list[insert_point].as_int = candidate_mv.as_int;
    mv_scores[insert_point] = weight;
    *candidate_count += (*candidate_count < MAX_MV_REF_CANDIDATES);
  }
}
Paul Wilkins's avatar
Paul Wilkins committed
234
235
236
237

// This function searches the neighbourhood of a given MB/SB and populates a
// list of candidate reference vectors.
//
Dmitry Kovalev's avatar
Dmitry Kovalev committed
238
239
240
void vp9_find_mv_refs(VP9_COMMON *cm, MACROBLOCKD *xd, MODE_INFO *here,
                      MODE_INFO *lf_here, MV_REFERENCE_FRAME ref_frame,
                      int_mv *mv_ref_list, int *ref_sign_bias) {
Paul Wilkins's avatar
Paul Wilkins committed
241
242
  int i;
  MODE_INFO *candidate_mi;
Paul Wilkins's avatar
Paul Wilkins committed
243
  MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
Paul Wilkins's avatar
Paul Wilkins committed
244
  int_mv candidate_mvs[MAX_MV_REF_CANDIDATES];
Paul Wilkins's avatar
Paul Wilkins committed
245
  int_mv c_refmv;
Paul Wilkins's avatar
Paul Wilkins committed
246
  int_mv c2_refmv;
Paul Wilkins's avatar
Paul Wilkins committed
247
  MV_REFERENCE_FRAME c_ref_frame;
Paul Wilkins's avatar
Paul Wilkins committed
248
  MV_REFERENCE_FRAME c2_ref_frame;
Paul Wilkins's avatar
Paul Wilkins committed
249
  int candidate_scores[MAX_MV_REF_CANDIDATES];
Paul Wilkins's avatar
Paul Wilkins committed
250
  int refmv_count = 0;
251
  int split_count = 0;
252
253
  int (*mv_ref_search)[2];
  int *ref_distance_weight;
Paul Wilkins's avatar
Paul Wilkins committed
254
  int zero_seen = FALSE;
255
  const int mb_col = (-xd->mb_to_left_edge) >> 7;
Paul Wilkins's avatar
Paul Wilkins committed
256
257

  // Blank the reference vector lists and other local structures.
Paul Wilkins's avatar
Paul Wilkins committed
258
259
  vpx_memset(mv_ref_list, 0, sizeof(int_mv) * MAX_MV_REF_CANDIDATES);
  vpx_memset(candidate_mvs, 0, sizeof(int_mv) * MAX_MV_REF_CANDIDATES);
Paul Wilkins's avatar
Paul Wilkins committed
260
261
  vpx_memset(candidate_scores, 0, sizeof(candidate_scores));

262
263
264
  if (mbmi->sb_type == BLOCK_SIZE_SB64X64) {
    mv_ref_search = sb64_mv_ref_search;
    ref_distance_weight = sb64_ref_distance_weight;
265
  } else if (mbmi->sb_type >= BLOCK_SIZE_SB32X32) {
266
267
268
269
270
271
    mv_ref_search = sb_mv_ref_search;
    ref_distance_weight = sb_ref_distance_weight;
  } else {
    mv_ref_search = mb_mv_ref_search;
    ref_distance_weight = mb_ref_distance_weight;
  }
272
273
274

  // We first scan for candidate vectors that match the current reference frame
  // Look at nearest neigbours
Paul Wilkins's avatar
Paul Wilkins committed
275
  for (i = 0; i < 2; ++i) {
276
277
278
279
    const int mb_search_col = mb_col + mv_ref_search[i][0];

    if ((mb_search_col >= cm->cur_tile_mb_col_start) &&
        (mb_search_col < cm->cur_tile_mb_col_end) &&
Paul Wilkins's avatar
Paul Wilkins committed
280
281
282
283
284
        ((mv_ref_search[i][1] << 7) >= xd->mb_to_top_edge)) {

      candidate_mi = here + mv_ref_search[i][0] +
                     (mv_ref_search[i][1] * xd->mode_info_stride);

285
      if (get_matching_candidate(candidate_mi, ref_frame, &c_refmv)) {
Paul Wilkins's avatar
Paul Wilkins committed
286
287
        add_candidate_mv(candidate_mvs, candidate_scores,
                         &refmv_count, c_refmv, ref_distance_weight[i] + 16);
Paul Wilkins's avatar
Paul Wilkins committed
288
      }
289
      split_count += (candidate_mi->mbmi.mode == SPLITMV);
Paul Wilkins's avatar
Paul Wilkins committed
290
291
    }
  }
292
293
294
295
  // Look in the last frame if it exists
  if (lf_here) {
    candidate_mi = lf_here;
    if (get_matching_candidate(candidate_mi, ref_frame, &c_refmv)) {
Paul Wilkins's avatar
Paul Wilkins committed
296
297
      add_candidate_mv(candidate_mvs, candidate_scores,
                       &refmv_count, c_refmv, 18);
298
    }
Paul Wilkins's avatar
Paul Wilkins committed
299
  }
300
301
  // More distant neigbours
  for (i = 2; (i < MVREF_NEIGHBOURS) &&
Paul Wilkins's avatar
Paul Wilkins committed
302
              (refmv_count < (MAX_MV_REF_CANDIDATES - 1)); ++i) {
303
304
305
306
    const int mb_search_col = mb_col + mv_ref_search[i][0];

    if ((mb_search_col >= cm->cur_tile_mb_col_start) &&
        (mb_search_col < cm->cur_tile_mb_col_end) &&
Paul Wilkins's avatar
Paul Wilkins committed
307
308
309
310
        ((mv_ref_search[i][1] << 7) >= xd->mb_to_top_edge)) {
      candidate_mi = here + mv_ref_search[i][0] +
                     (mv_ref_search[i][1] * xd->mode_info_stride);

311
      if (get_matching_candidate(candidate_mi, ref_frame, &c_refmv)) {
Paul Wilkins's avatar
Paul Wilkins committed
312
313
        add_candidate_mv(candidate_mvs, candidate_scores,
                         &refmv_count, c_refmv, ref_distance_weight[i] + 16);
314
315
316
      }
    }
  }
Paul Wilkins's avatar
Paul Wilkins committed
317

318
319
320
321
  // If we have not found enough candidates consider ones where the
  // reference frame does not match. Break out when we have
  // MAX_MV_REF_CANDIDATES candidates.
  // Look first at spatial neighbours
Paul Wilkins's avatar
Paul Wilkins committed
322
  if (refmv_count < (MAX_MV_REF_CANDIDATES - 1)) {
323
    for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
324
325
326
327
      const int mb_search_col = mb_col + mv_ref_search[i][0];

      if ((mb_search_col >= cm->cur_tile_mb_col_start) &&
          (mb_search_col < cm->cur_tile_mb_col_end) &&
328
          ((mv_ref_search[i][1] << 7) >= xd->mb_to_top_edge)) {
Paul Wilkins's avatar
Paul Wilkins committed
329

330
331
        candidate_mi = here + mv_ref_search[i][0] +
                       (mv_ref_search[i][1] * xd->mode_info_stride);
Paul Wilkins's avatar
Paul Wilkins committed
332

333
334
335
        get_non_matching_candidates(candidate_mi, ref_frame,
                                    &c_ref_frame, &c_refmv,
                                    &c2_ref_frame, &c2_refmv);
Paul Wilkins's avatar
Paul Wilkins committed
336

337
338
        if (c_ref_frame != INTRA_FRAME) {
          scale_mv(xd, ref_frame, c_ref_frame, &c_refmv, ref_sign_bias);
Paul Wilkins's avatar
Paul Wilkins committed
339
340
          add_candidate_mv(candidate_mvs, candidate_scores,
                           &refmv_count, c_refmv, ref_distance_weight[i]);
341
342
343
344
        }

        if (c2_ref_frame != INTRA_FRAME) {
          scale_mv(xd, ref_frame, c2_ref_frame, &c2_refmv, ref_sign_bias);
Paul Wilkins's avatar
Paul Wilkins committed
345
346
          add_candidate_mv(candidate_mvs, candidate_scores,
                           &refmv_count, c2_refmv, ref_distance_weight[i]);
Paul Wilkins's avatar
Paul Wilkins committed
347
        }
Paul Wilkins's avatar
Paul Wilkins committed
348
      }
349

Paul Wilkins's avatar
Paul Wilkins committed
350
      if (refmv_count >= (MAX_MV_REF_CANDIDATES - 1)) {
351
352
353
354
        break;
      }
    }
  }
355
  // Look at the last frame if it exists
Paul Wilkins's avatar
Paul Wilkins committed
356
  if (refmv_count < (MAX_MV_REF_CANDIDATES - 1) && lf_here) {
357
358
359
360
361
362
363
    candidate_mi = lf_here;
    get_non_matching_candidates(candidate_mi, ref_frame,
                                &c_ref_frame, &c_refmv,
                                &c2_ref_frame, &c2_refmv);

    if (c_ref_frame != INTRA_FRAME) {
      scale_mv(xd, ref_frame, c_ref_frame, &c_refmv, ref_sign_bias);
Paul Wilkins's avatar
Paul Wilkins committed
364
365
      add_candidate_mv(candidate_mvs, candidate_scores,
                       &refmv_count, c_refmv, 2);
366
367
368
369
    }

    if (c2_ref_frame != INTRA_FRAME) {
      scale_mv(xd, ref_frame, c2_ref_frame, &c2_refmv, ref_sign_bias);
Paul Wilkins's avatar
Paul Wilkins committed
370
371
      add_candidate_mv(candidate_mvs, candidate_scores,
                       &refmv_count, c2_refmv, 2);
Paul Wilkins's avatar
Paul Wilkins committed
372
373
374
    }
  }

375
376
  // Define inter mode coding context.
  // 0,0 was best
Paul Wilkins's avatar
Paul Wilkins committed
377
  if (candidate_mvs[0].as_int == 0) {
378
    // 0,0 is only candidate
Paul Wilkins's avatar
Paul Wilkins committed
379
    if (refmv_count <= 1) {
Paul Wilkins's avatar
Paul Wilkins committed
380
      mbmi->mb_mode_context[ref_frame] = 0;
381
382
    // non zero candidates candidates available
    } else if (split_count == 0) {
Paul Wilkins's avatar
Paul Wilkins committed
383
384
385
386
      mbmi->mb_mode_context[ref_frame] = 1;
    } else {
      mbmi->mb_mode_context[ref_frame] = 2;
    }
387
  } else if (split_count == 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
388
389
    // Non zero best, No Split MV cases
    mbmi->mb_mode_context[ref_frame] = candidate_scores[0] >= 16 ? 3 : 4;
Paul Wilkins's avatar
Paul Wilkins committed
390
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
391
392
    // Non zero best, some split mv
    mbmi->mb_mode_context[ref_frame] = candidate_scores[0] >= 16 ? 5 : 6;
Paul Wilkins's avatar
Paul Wilkins committed
393
394
  }

Paul Wilkins's avatar
Paul Wilkins committed
395
  // Scan for 0,0 case and clamp non zero choices
Paul Wilkins's avatar
Paul Wilkins committed
396
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
Paul Wilkins's avatar
Paul Wilkins committed
397
398
399
400
401
    if (candidate_mvs[i].as_int == 0) {
      zero_seen = TRUE;
    } else {
      clamp_mv_ref(xd, &candidate_mvs[i]);
    }
402
  }
Paul Wilkins's avatar
Paul Wilkins committed
403
  // 0,0 is always a valid reference. Add it if not already seen.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
404
  if (!zero_seen)
Paul Wilkins's avatar
Paul Wilkins committed
405
    candidate_mvs[MAX_MV_REF_CANDIDATES-1].as_int = 0;
Paul Wilkins's avatar
Paul Wilkins committed
406
407
408
409

  // Copy over the candidate list.
  vpx_memcpy(mv_ref_list, candidate_mvs, sizeof(candidate_mvs));
}