Line data Source code
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.
16 0 : 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 0 : const int *ref_sign_bias = cm->ref_frame_sign_bias;
21 0 : int i, refmv_count = 0;
22 0 : const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
23 0 : int different_ref_found = 0;
24 0 : int context_counter = 0;
25 0 : const MV_REF *const prev_frame_mvs =
26 0 : cm->use_prev_frame_mvs
27 0 : ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
28 0 : : NULL;
29 0 : const TileInfo *const tile = &xd->tile;
30 :
31 : // Blank the reference vector list
32 0 : 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 0 : for (i = 0; i < 2; ++i) {
38 0 : const POSITION *const mv_ref = &mv_ref_search[i];
39 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
40 0 : const MODE_INFO *const candidate_mi =
41 0 : xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
42 : // Keep counts for entropy encoding.
43 0 : context_counter += mode_2_counter[candidate_mi->mode];
44 0 : different_ref_found = 1;
45 :
46 0 : if (candidate_mi->ref_frame[0] == ref_frame)
47 0 : ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
48 : refmv_count, mv_ref_list, Done);
49 0 : else if (candidate_mi->ref_frame[1] == ref_frame)
50 0 : 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 0 : for (; i < MVREF_NEIGHBOURS; ++i) {
59 0 : const POSITION *const mv_ref = &mv_ref_search[i];
60 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
61 0 : const MODE_INFO *const candidate_mi =
62 0 : xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
63 0 : different_ref_found = 1;
64 :
65 0 : if (candidate_mi->ref_frame[0] == ref_frame)
66 0 : ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
67 0 : else if (candidate_mi->ref_frame[1] == ref_frame)
68 0 : 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 0 : if (cm->use_prev_frame_mvs) {
74 0 : if (prev_frame_mvs->ref_frame[0] == ref_frame) {
75 0 : ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
76 0 : } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
77 0 : 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 0 : if (different_ref_found) {
85 0 : for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
86 0 : const POSITION *mv_ref = &mv_ref_search[i];
87 0 : if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
88 0 : const MODE_INFO *const candidate_mi =
89 0 : 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 0 : 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 0 : if (cm->use_prev_frame_mvs) {
100 0 : if (prev_frame_mvs->ref_frame[0] != ref_frame &&
101 0 : prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
102 0 : int_mv mv = prev_frame_mvs->mv[0];
103 0 : if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
104 0 : ref_sign_bias[ref_frame]) {
105 0 : mv.as_mv.row *= -1;
106 0 : mv.as_mv.col *= -1;
107 : }
108 0 : ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
109 : }
110 :
111 0 : if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
112 0 : prev_frame_mvs->ref_frame[1] != ref_frame &&
113 0 : prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
114 0 : int_mv mv = prev_frame_mvs->mv[1];
115 0 : if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
116 0 : ref_sign_bias[ref_frame]) {
117 0 : mv.as_mv.row *= -1;
118 0 : mv.as_mv.col *= -1;
119 : }
120 0 : ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
121 : }
122 : }
123 :
124 : Done:
125 :
126 0 : mode_context[ref_frame] = counter_to_context[context_counter];
127 :
128 : // Clamp vectors
129 0 : for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
130 0 : clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
131 0 : }
132 :
133 0 : 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 0 : find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
138 : mode_context);
139 0 : }
140 :
141 0 : 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 0 : for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
146 0 : lower_mv_precision(&mvlist[i].as_mv, allow_hp);
147 0 : clamp_mv2(&mvlist[i].as_mv, xd);
148 : }
149 0 : *nearest_mv = mvlist[0];
150 0 : *near_mv = mvlist[1];
151 0 : }
152 :
153 0 : 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 0 : MODE_INFO *const mi = xd->mi[0];
159 0 : b_mode_info *bmi = mi->bmi;
160 : int n;
161 :
162 : assert(MAX_MV_REF_CANDIDATES == 2);
163 :
164 0 : find_mv_refs_idx(cm, xd, mi, mi->ref_frame[ref], mv_list, block, mi_row,
165 : mi_col, mode_context);
166 :
167 0 : near_mv->as_int = 0;
168 0 : switch (block) {
169 : case 0:
170 0 : nearest_mv->as_int = mv_list[0].as_int;
171 0 : near_mv->as_int = mv_list[1].as_int;
172 0 : break;
173 : case 1:
174 : case 2:
175 0 : nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
176 0 : for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
177 0 : if (nearest_mv->as_int != mv_list[n].as_int) {
178 0 : near_mv->as_int = mv_list[n].as_int;
179 0 : break;
180 : }
181 0 : break;
182 : case 3: {
183 : int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
184 0 : candidates[0] = bmi[1].as_mv[ref];
185 0 : candidates[1] = bmi[0].as_mv[ref];
186 0 : candidates[2] = mv_list[0];
187 0 : candidates[3] = mv_list[1];
188 :
189 0 : nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
190 0 : for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
191 0 : if (nearest_mv->as_int != candidates[n].as_int) {
192 0 : near_mv->as_int = candidates[n].as_int;
193 0 : break;
194 : }
195 0 : break;
196 : }
197 0 : default: assert(0 && "Invalid block index.");
198 : }
199 0 : }
|