1
2 /*
3 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4 *
5 * Use of this source code is governed by a BSD-style license
6 * that can be found in the LICENSE file in the root of the source
7 * tree. An additional intellectual property rights grant can be found
8 * in the file PATENTS. All contributing project authors may
9 * be found in the AUTHORS file in the root of the source tree.
10 */
11
12 #include "vp9/common/vp9_mvref_common.h"
13
14 // This function searches the neighborhood of a given MB/SB
15 // to try and find candidate reference vectors.
find_mv_refs_idx(const VP9_COMMON * cm,const MACROBLOCKD * xd,MODE_INFO * mi,MV_REFERENCE_FRAME ref_frame,int_mv * mv_ref_list,int block,int mi_row,int mi_col,uint8_t * mode_context)16 static void find_mv_refs_idx(const VP9_COMMON *cm, const MACROBLOCKD *xd,
17 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
18 int_mv *mv_ref_list, int block, int mi_row,
19 int mi_col, uint8_t *mode_context) {
20 const int *ref_sign_bias = cm->ref_frame_sign_bias;
21 int i, refmv_count = 0;
22 const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
23 int different_ref_found = 0;
24 int context_counter = 0;
25 const MV_REF *const prev_frame_mvs =
26 cm->use_prev_frame_mvs
27 ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
28 : NULL;
29 const TileInfo *const tile = &xd->tile;
30
31 // Blank the reference vector list
32 memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
33
34 // The nearest 2 blocks are treated differently
35 // if the size < 8x8 we get the mv from the bmi substructure,
36 // and we also need to keep a mode count.
37 for (i = 0; i < 2; ++i) {
38 const POSITION *const mv_ref = &mv_ref_search[i];
39 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
40 const MODE_INFO *const candidate_mi =
41 xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
42 // Keep counts for entropy encoding.
43 context_counter += mode_2_counter[candidate_mi->mode];
44 different_ref_found = 1;
45
46 if (candidate_mi->ref_frame[0] == ref_frame)
47 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
48 refmv_count, mv_ref_list, Done);
49 else if (candidate_mi->ref_frame[1] == ref_frame)
50 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
51 refmv_count, mv_ref_list, Done);
52 }
53 }
54
55 // Check the rest of the neighbors in much the same way
56 // as before except we don't need to keep track of sub blocks or
57 // mode counts.
58 for (; i < MVREF_NEIGHBOURS; ++i) {
59 const POSITION *const mv_ref = &mv_ref_search[i];
60 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
61 const MODE_INFO *const candidate_mi =
62 xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
63 different_ref_found = 1;
64
65 if (candidate_mi->ref_frame[0] == ref_frame)
66 ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
67 else if (candidate_mi->ref_frame[1] == ref_frame)
68 ADD_MV_REF_LIST(candidate_mi->mv[1], refmv_count, mv_ref_list, Done);
69 }
70 }
71
72 // Check the last frame's mode and mv info.
73 if (cm->use_prev_frame_mvs) {
74 if (prev_frame_mvs->ref_frame[0] == ref_frame) {
75 ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
76 } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
77 ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
78 }
79 }
80
81 // Since we couldn't find 2 mvs from the same reference frame
82 // go back through the neighbors and find motion vectors from
83 // different reference frames.
84 if (different_ref_found) {
85 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
86 const POSITION *mv_ref = &mv_ref_search[i];
87 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
88 const MODE_INFO *const candidate_mi =
89 xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
90
91 // If the candidate is INTRA we don't want to consider its mv.
92 IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
93 refmv_count, mv_ref_list, Done);
94 }
95 }
96 }
97
98 // Since we still don't have a candidate we'll try the last frame.
99 if (cm->use_prev_frame_mvs) {
100 if (prev_frame_mvs->ref_frame[0] != ref_frame &&
101 prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
102 int_mv mv = prev_frame_mvs->mv[0];
103 if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
104 ref_sign_bias[ref_frame]) {
105 mv.as_mv.row *= -1;
106 mv.as_mv.col *= -1;
107 }
108 ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
109 }
110
111 if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
112 prev_frame_mvs->ref_frame[1] != ref_frame &&
113 prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
114 int_mv mv = prev_frame_mvs->mv[1];
115 if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
116 ref_sign_bias[ref_frame]) {
117 mv.as_mv.row *= -1;
118 mv.as_mv.col *= -1;
119 }
120 ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
121 }
122 }
123
124 Done:
125
126 mode_context[ref_frame] = counter_to_context[context_counter];
127
128 // Clamp vectors
129 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
130 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
131 }
132
vp9_find_mv_refs(const VP9_COMMON * cm,const MACROBLOCKD * xd,MODE_INFO * mi,MV_REFERENCE_FRAME ref_frame,int_mv * mv_ref_list,int mi_row,int mi_col,uint8_t * mode_context)133 void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
134 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
135 int_mv *mv_ref_list, int mi_row, int mi_col,
136 uint8_t *mode_context) {
137 find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
138 mode_context);
139 }
140
vp9_find_best_ref_mvs(MACROBLOCKD * xd,int allow_hp,int_mv * mvlist,int_mv * nearest_mv,int_mv * near_mv)141 void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
142 int_mv *nearest_mv, int_mv *near_mv) {
143 int i;
144 // Make sure all the candidates are properly clamped etc
145 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
146 lower_mv_precision(&mvlist[i].as_mv, allow_hp);
147 clamp_mv2(&mvlist[i].as_mv, xd);
148 }
149 *nearest_mv = mvlist[0];
150 *near_mv = mvlist[1];
151 }
152
vp9_append_sub8x8_mvs_for_idx(VP9_COMMON * cm,MACROBLOCKD * xd,int block,int ref,int mi_row,int mi_col,int_mv * nearest_mv,int_mv * near_mv,uint8_t * mode_context)153 void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
154 int ref, int mi_row, int mi_col,
155 int_mv *nearest_mv, int_mv *near_mv,
156 uint8_t *mode_context) {
157 int_mv mv_list[MAX_MV_REF_CANDIDATES];
158 MODE_INFO *const mi = xd->mi[0];
159 b_mode_info *bmi = mi->bmi;
160 int n;
161
162 assert(MAX_MV_REF_CANDIDATES == 2);
163
164 find_mv_refs_idx(cm, xd, mi, mi->ref_frame[ref], mv_list, block, mi_row,
165 mi_col, mode_context);
166
167 near_mv->as_int = 0;
168 switch (block) {
169 case 0:
170 nearest_mv->as_int = mv_list[0].as_int;
171 near_mv->as_int = mv_list[1].as_int;
172 break;
173 case 1:
174 case 2:
175 nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
176 for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
177 if (nearest_mv->as_int != mv_list[n].as_int) {
178 near_mv->as_int = mv_list[n].as_int;
179 break;
180 }
181 break;
182 case 3: {
183 int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
184 candidates[0] = bmi[1].as_mv[ref];
185 candidates[1] = bmi[0].as_mv[ref];
186 candidates[2] = mv_list[0];
187 candidates[3] = mv_list[1];
188
189 nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
190 for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
191 if (nearest_mv->as_int != candidates[n].as_int) {
192 near_mv->as_int = candidates[n].as_int;
193 break;
194 }
195 break;
196 }
197 default: assert(0 && "Invalid block index.");
198 }
199 }
200