vp9_mvref_common.c 11.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
14
static const int mv_ref_blocks[BLOCK_SIZE_TYPES][MVREF_NEIGHBOURS][2] = {
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  // SB4X4
  {{0, -1}, {-1, 0}, {-1, -1}, {0, -2}, {-2, 0}, {-1, -2}, {-2, -1}, {-2, -2}},
  // SB4X8
  {{0, -1}, {-1, 0}, {-1, -1}, {0, -2}, {-2, 0}, {-1, -2}, {-2, -1}, {-2, -2}},
  // SB8X4
  {{0, -1}, {-1, 0}, {-1, -1}, {0, -2}, {-2, 0}, {-1, -2}, {-2, -1}, {-2, -2}},
  // SB8X8
  {{0, -1}, {-1, 0}, {-1, -1}, {0, -2}, {-2, 0}, {-1, -2}, {-2, -1}, {-2, -2}},
  // SB8X16
  {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
  // SB16X8
  {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
  // SB16X16
  {{0, -1}, {-1, 0}, {1, -1}, {-1, 1}, {-1, -1}, {0, -3}, {-3, 0}, {-3, -3}},
  // SB16X32
  {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
  // SB32X16
  {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
  // SB32X32
  {{1, -1}, {-1, 1}, {2, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {-3, -3}},
  // SB32X64
  {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
  // SB64X32
  {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
  // SB64X64
  {{3, -1}, {-1, 3}, {4, -1}, {-1, 4}, {-1, -1}, {0, -1}, {-1, 0}, {6, -1}}
41
};
Paul Wilkins's avatar
Paul Wilkins committed
42
// clamp_mv_ref
Paul Wilkins's avatar
Paul Wilkins committed
43
44
#define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units

Dmitry Kovalev's avatar
Dmitry Kovalev committed
45
static void clamp_mv_ref(const MACROBLOCKD *xd, int_mv *mv) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
46
47
48
49
  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
50
51
}

52
// Gets a candidate reference motion vector from the given mode info
53
// structure if one exists that matches the given reference frame.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
54
static int get_matching_candidate(const MODE_INFO *candidate_mi,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
55
                                  MV_REFERENCE_FRAME ref_frame,
Ronald S. Bultje's avatar
Ronald S. Bultje committed
56
                                  int_mv *c_mv, int block_idx) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
57
  if (ref_frame == candidate_mi->mbmi.ref_frame[0]) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
58
59
60
61
    if (block_idx >= 0 && candidate_mi->mbmi.sb_type < BLOCK_SIZE_SB8X8)
      c_mv->as_int = candidate_mi->bmi[block_idx].as_mv[0].as_int;
    else
      c_mv->as_int = candidate_mi->mbmi.mv[0].as_int;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
62
  } else if (ref_frame == candidate_mi->mbmi.ref_frame[1]) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
63
64
65
66
    if (block_idx >= 0 && candidate_mi->mbmi.sb_type < BLOCK_SIZE_SB8X8)
      c_mv->as_int = candidate_mi->bmi[block_idx].as_mv[1].as_int;
    else
      c_mv->as_int = candidate_mi->mbmi.mv[1].as_int;
67
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
68
    return 0;
69
70
  }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
71
  return 1;
72
}
Paul Wilkins's avatar
Paul Wilkins committed
73

74
// Gets candidate reference motion vector(s) from the given mode info
75
// structure if they exists and do NOT match the given reference frame.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
76
77
78
79
80
81
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
82

83
  c_mv->as_int = 0;
Paul Wilkins's avatar
Paul Wilkins committed
84
  c2_mv->as_int = 0;
85
  *c_ref_frame = INTRA_FRAME;
Paul Wilkins's avatar
Paul Wilkins committed
86
  *c2_ref_frame = INTRA_FRAME;
Paul Wilkins's avatar
Paul Wilkins committed
87

88
  // If first candidate not valid neither will be.
Ronald S. Bultje's avatar
Ronald S. Bultje committed
89
  if (candidate_mi->mbmi.ref_frame[0] > INTRA_FRAME) {
90
    // First candidate
Ronald S. Bultje's avatar
Ronald S. Bultje committed
91
92
    if (candidate_mi->mbmi.ref_frame[0] != ref_frame) {
      *c_ref_frame = candidate_mi->mbmi.ref_frame[0];
93
      c_mv->as_int = candidate_mi->mbmi.mv[0].as_int;
Paul Wilkins's avatar
Paul Wilkins committed
94
95
    }

96
    // Second candidate
Ronald S. Bultje's avatar
Ronald S. Bultje committed
97
98
    if ((candidate_mi->mbmi.ref_frame[1] > INTRA_FRAME) &&
        (candidate_mi->mbmi.ref_frame[1] != ref_frame) &&
Dmitry Kovalev's avatar
Dmitry Kovalev committed
99
        (candidate_mi->mbmi.mv[1].as_int != candidate_mi->mbmi.mv[0].as_int)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
100
      *c2_ref_frame = candidate_mi->mbmi.ref_frame[1];
101
      c2_mv->as_int = candidate_mi->mbmi.mv[1].as_int;
Paul Wilkins's avatar
Paul Wilkins committed
102
    }
Paul Wilkins's avatar
Paul Wilkins committed
103
104
105
  }
}

Paul Wilkins's avatar
Paul Wilkins committed
106
107

// Performs mv sign inversion if indicated by the reference frame combination.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
108
109
110
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
111
112
113
114
115

  // 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
116
117
118
  }
}

Paul Wilkins's avatar
Paul Wilkins committed
119
120
// Add a candidate mv.
// Discard if it has already been seen.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
121
122
123
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
124
125
126
127
128
129
130
131
132
  if (*candidate_count == 0) {
    mv_list[0].as_int = candidate_mv.as_int;
    mv_scores[0] = weight;
    *candidate_count += 1;
  } else if ((*candidate_count == 1) &&
             (candidate_mv.as_int != mv_list[0].as_int)) {
    mv_list[1].as_int = candidate_mv.as_int;
    mv_scores[1] = weight;
    *candidate_count += 1;
Paul Wilkins's avatar
Paul Wilkins committed
133
134
  }
}
Paul Wilkins's avatar
Paul Wilkins committed
135

Paul Wilkins's avatar
Paul Wilkins committed
136
137
// This function searches the neighbourhood of a given MB/SB
// to try and find candidate reference vectors.
Paul Wilkins's avatar
Paul Wilkins committed
138
//
Ronald S. Bultje's avatar
Ronald S. Bultje committed
139
140
141
142
void vp9_find_mv_refs_idx(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,
                          int block_idx) {
Paul Wilkins's avatar
Paul Wilkins committed
143
144
  int i;
  MODE_INFO *candidate_mi;
Paul Wilkins's avatar
Paul Wilkins committed
145
  MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
Paul Wilkins's avatar
Paul Wilkins committed
146
  int_mv c_refmv;
Paul Wilkins's avatar
Paul Wilkins committed
147
  int_mv c2_refmv;
Paul Wilkins's avatar
Paul Wilkins committed
148
  MV_REFERENCE_FRAME c_ref_frame;
Paul Wilkins's avatar
Paul Wilkins committed
149
  MV_REFERENCE_FRAME c2_ref_frame;
150
  int candidate_scores[MAX_MV_REF_CANDIDATES] = { 0 };
Paul Wilkins's avatar
Paul Wilkins committed
151
  int refmv_count = 0;
152
  const int (*mv_ref_search)[2] = mv_ref_blocks[mbmi->sb_type];
153
  const int mi_col = get_mi_col(xd);
Jim Bankoski's avatar
Jim Bankoski committed
154
  const int mi_row = get_mi_row(xd);
Paul Wilkins's avatar
Paul Wilkins committed
155
156
157
  int intra_count = 0;
  int zero_count = 0;
  int newmv_count = 0;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
158
  int x_idx = 0, y_idx = 0;
Paul Wilkins's avatar
Paul Wilkins committed
159

Paul Wilkins's avatar
Paul Wilkins committed
160
  // Blank the reference vector lists and other local structures.
Paul Wilkins's avatar
Paul Wilkins committed
161
  vpx_memset(mv_ref_list, 0, sizeof(int_mv) * MAX_MV_REF_CANDIDATES);
Paul Wilkins's avatar
Paul Wilkins committed
162

163
164
165
  if (mbmi->sb_type < BLOCK_SIZE_SB8X8) {
    x_idx = block_idx & 1;
    y_idx = block_idx >> 1;
166
  }
167
168
169

  // We first scan for candidate vectors that match the current reference frame
  // Look at nearest neigbours
Paul Wilkins's avatar
Paul Wilkins committed
170
  for (i = 0; i < 2; ++i) {
171
    const int mi_search_col = mi_col + mv_ref_search[i][0];
Jim Bankoski's avatar
Jim Bankoski committed
172
    const int mi_search_row = mi_row + mv_ref_search[i][1];
173
174
    if ((mi_search_col >= cm->cur_tile_mi_col_start) &&
        (mi_search_col < cm->cur_tile_mi_col_end) &&
Jim Bankoski's avatar
Jim Bankoski committed
175
        (mi_search_row >= 0) && (mi_search_row < cm->mi_rows)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
176
      int b;
Paul Wilkins's avatar
Paul Wilkins committed
177
178
179
180

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

Ronald S. Bultje's avatar
Ronald S. Bultje committed
181
182
183
184
185
186
187
188
189
      if (block_idx >= 0) {
        if (mv_ref_search[i][0])
          b = 1 + y_idx * 2;
        else
          b = 2 + x_idx;
      } else {
        b = -1;
      }
      if (get_matching_candidate(candidate_mi, ref_frame, &c_refmv, b)) {
Paul Wilkins's avatar
Paul Wilkins committed
190
191
        add_candidate_mv(mv_ref_list, candidate_scores,
                         &refmv_count, c_refmv, 16);
Paul Wilkins's avatar
Paul Wilkins committed
192
      }
Paul Wilkins's avatar
Paul Wilkins committed
193
194
195
196
197

      // Count number of neihgbours coded intra and zeromv
      intra_count += (candidate_mi->mbmi.mode < NEARESTMV);
      zero_count += (candidate_mi->mbmi.mode == ZEROMV);
      newmv_count += (candidate_mi->mbmi.mode >= NEWMV);
Paul Wilkins's avatar
Paul Wilkins committed
198
199
    }
  }
Paul Wilkins's avatar
Paul Wilkins committed
200

201
202
  // More distant neigbours
  for (i = 2; (i < MVREF_NEIGHBOURS) &&
Paul Wilkins's avatar
Paul Wilkins committed
203
              (refmv_count < MAX_MV_REF_CANDIDATES); ++i) {
204
    const int mi_search_col = mi_col + mv_ref_search[i][0];
Jim Bankoski's avatar
Jim Bankoski committed
205
    const int mi_search_row = mi_row + mv_ref_search[i][1];
206
207
    if ((mi_search_col >= cm->cur_tile_mi_col_start) &&
        (mi_search_col < cm->cur_tile_mi_col_end) &&
Jim Bankoski's avatar
Jim Bankoski committed
208
        (mi_search_row >= 0) && (mi_search_row < cm->mi_rows)) {
Paul Wilkins's avatar
Paul Wilkins committed
209
210
211
      candidate_mi = here + mv_ref_search[i][0] +
                     (mv_ref_search[i][1] * xd->mode_info_stride);

Ronald S. Bultje's avatar
Ronald S. Bultje committed
212
      if (get_matching_candidate(candidate_mi, ref_frame, &c_refmv, -1)) {
Paul Wilkins's avatar
Paul Wilkins committed
213
214
        add_candidate_mv(mv_ref_list, candidate_scores,
                         &refmv_count, c_refmv, 16);
215
216
217
      }
    }
  }
Paul Wilkins's avatar
Paul Wilkins committed
218

Paul Wilkins's avatar
Paul Wilkins committed
219
220
221
  // Look in the last frame if it exists
  if (lf_here && (refmv_count < MAX_MV_REF_CANDIDATES)) {
    candidate_mi = lf_here;
222
    if (get_matching_candidate(candidate_mi, ref_frame, &c_refmv, -1)) {
Paul Wilkins's avatar
Paul Wilkins committed
223
224
      add_candidate_mv(mv_ref_list, candidate_scores,
                       &refmv_count, c_refmv, 16);
Paul Wilkins's avatar
Paul Wilkins committed
225
226
227
    }
  }

228
229
230
231
  // 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
232
233
  for (i = 0; (i < MVREF_NEIGHBOURS) &&
              (refmv_count < MAX_MV_REF_CANDIDATES); ++i) {
234
    const int mi_search_col = mi_col + mv_ref_search[i][0];
Jim Bankoski's avatar
Jim Bankoski committed
235
    const int mi_search_row = mi_row + mv_ref_search[i][1];
236
237
    if ((mi_search_col >= cm->cur_tile_mi_col_start) &&
        (mi_search_col < cm->cur_tile_mi_col_end) &&
Jim Bankoski's avatar
Jim Bankoski committed
238
        (mi_search_row >= 0) && (mi_search_row < cm->mi_rows)) {
Paul Wilkins's avatar
Paul Wilkins committed
239
240
241
242
243
244
245
246
247
      candidate_mi = here + mv_ref_search[i][0] +
                     (mv_ref_search[i][1] * xd->mode_info_stride);

      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
248
249
        add_candidate_mv(mv_ref_list, candidate_scores,
                         &refmv_count, c_refmv, 1);
Paul Wilkins's avatar
Paul Wilkins committed
250
      }
251

Paul Wilkins's avatar
Paul Wilkins committed
252
253
      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
254
255
        add_candidate_mv(mv_ref_list, candidate_scores,
                         &refmv_count, c2_refmv, 1);
256
257
258
      }
    }
  }
Paul Wilkins's avatar
Paul Wilkins committed
259

260
  // Look at the last frame if it exists
Paul Wilkins's avatar
Paul Wilkins committed
261
  if (lf_here && (refmv_count < MAX_MV_REF_CANDIDATES)) {
262
263
264
265
266
267
268
    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
269
      add_candidate_mv(mv_ref_list, candidate_scores,
Paul Wilkins's avatar
Paul Wilkins committed
270
                       &refmv_count, c_refmv, 1);
271
272
273
274
    }

    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
275
      add_candidate_mv(mv_ref_list, candidate_scores,
Paul Wilkins's avatar
Paul Wilkins committed
276
                       &refmv_count, c2_refmv, 1);
Paul Wilkins's avatar
Paul Wilkins committed
277
278
279
    }
  }

Paul Wilkins's avatar
Paul Wilkins committed
280
281
282
283
284
285
  if (!intra_count) {
    if (!newmv_count) {
      // 0 = both zero mv
      // 1 = one zero mv + one a predicted mv
      // 2 = two predicted mvs
      mbmi->mb_mode_context[ref_frame] = 2 - zero_count;
Paul Wilkins's avatar
Paul Wilkins committed
286
    } else {
Paul Wilkins's avatar
Paul Wilkins committed
287
288
289
      // 3 = one predicted/zero and one new mv
      // 4 = two new mvs
      mbmi->mb_mode_context[ref_frame] = 2 + newmv_count;
Paul Wilkins's avatar
Paul Wilkins committed
290
291
    }
  } else {
Paul Wilkins's avatar
Paul Wilkins committed
292
293
294
    // 5 = one intra neighbour + x
    // 6 = two intra neighbours
    mbmi->mb_mode_context[ref_frame] = 4 + intra_count;
Paul Wilkins's avatar
Paul Wilkins committed
295
296
  }

Paul Wilkins's avatar
Paul Wilkins committed
297
  // Clamp vectors
Paul Wilkins's avatar
Paul Wilkins committed
298
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
Paul Wilkins's avatar
Paul Wilkins committed
299
    clamp_mv_ref(xd, &mv_ref_list[i]);
300
  }
Paul Wilkins's avatar
Paul Wilkins committed
301
}