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 neighbourhood of a given MB/SB 15// to try and find candidate reference vectors. 16static 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, 19 int block, int mi_row, int mi_col, 20 find_mv_refs_sync sync, void *const data, 21 uint8_t *mode_context) { 22 const int *ref_sign_bias = cm->ref_frame_sign_bias; 23 int i, refmv_count = 0; 24 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type]; 25 int different_ref_found = 0; 26 int context_counter = 0; 27 const MV_REF *const prev_frame_mvs = cm->use_prev_frame_mvs ? 28 cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col : 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 = xd->mi[mv_ref->col + mv_ref->row * 41 xd->mi_stride]; 42 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi; 43 // Keep counts for entropy encoding. 44 context_counter += mode_2_counter[candidate->mode]; 45 different_ref_found = 1; 46 47 if (candidate->ref_frame[0] == ref_frame) 48 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block), 49 refmv_count, mv_ref_list, Done); 50 else if (candidate->ref_frame[1] == ref_frame) 51 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block), 52 refmv_count, mv_ref_list, Done); 53 } 54 } 55 56 // Check the rest of the neighbors in much the same way 57 // as before except we don't need to keep track of sub blocks or 58 // mode counts. 59 for (; i < MVREF_NEIGHBOURS; ++i) { 60 const POSITION *const mv_ref = &mv_ref_search[i]; 61 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) { 62 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row * 63 xd->mi_stride]->mbmi; 64 different_ref_found = 1; 65 66 if (candidate->ref_frame[0] == ref_frame) 67 ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, Done); 68 else if (candidate->ref_frame[1] == ref_frame) 69 ADD_MV_REF_LIST(candidate->mv[1], refmv_count, mv_ref_list, Done); 70 } 71 } 72 73 // TODO(hkuang): Remove this sync after fixing pthread_cond_broadcast 74 // on windows platform. The sync here is unncessary if use_perv_frame_mvs 75 // is 0. But after removing it, there will be hang in the unit test on windows 76 // due to several threads waiting for a thread's signal. 77#if defined(_WIN32) && !HAVE_PTHREAD_H 78 if (cm->frame_parallel_decode && sync != NULL) { 79 sync(data, mi_row); 80 } 81#endif 82 83 // Check the last frame's mode and mv info. 84 if (cm->use_prev_frame_mvs) { 85 // Synchronize here for frame parallel decode if sync function is provided. 86 if (cm->frame_parallel_decode && sync != NULL) { 87 sync(data, mi_row); 88 } 89 90 if (prev_frame_mvs->ref_frame[0] == ref_frame) { 91 ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done); 92 } else if (prev_frame_mvs->ref_frame[1] == ref_frame) { 93 ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done); 94 } 95 } 96 97 // Since we couldn't find 2 mvs from the same reference frame 98 // go back through the neighbors and find motion vectors from 99 // different reference frames. 100 if (different_ref_found) { 101 for (i = 0; i < MVREF_NEIGHBOURS; ++i) { 102 const POSITION *mv_ref = &mv_ref_search[i]; 103 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) { 104 const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row 105 * xd->mi_stride]->mbmi; 106 107 // If the candidate is INTRA we don't want to consider its mv. 108 IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias, 109 refmv_count, mv_ref_list, Done); 110 } 111 } 112 } 113 114 // Since we still don't have a candidate we'll try the last frame. 115 if (cm->use_prev_frame_mvs) { 116 if (prev_frame_mvs->ref_frame[0] != ref_frame && 117 prev_frame_mvs->ref_frame[0] > INTRA_FRAME) { 118 int_mv mv = prev_frame_mvs->mv[0]; 119 if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] != 120 ref_sign_bias[ref_frame]) { 121 mv.as_mv.row *= -1; 122 mv.as_mv.col *= -1; 123 } 124 ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done); 125 } 126 127 if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME && 128 prev_frame_mvs->ref_frame[1] != ref_frame && 129 prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) { 130 int_mv mv = prev_frame_mvs->mv[1]; 131 if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] != 132 ref_sign_bias[ref_frame]) { 133 mv.as_mv.row *= -1; 134 mv.as_mv.col *= -1; 135 } 136 ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done); 137 } 138 } 139 140 Done: 141 142 mode_context[ref_frame] = counter_to_context[context_counter]; 143 144 // Clamp vectors 145 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) 146 clamp_mv_ref(&mv_ref_list[i].as_mv, xd); 147} 148 149void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd, 150 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame, 151 int_mv *mv_ref_list, 152 int mi_row, int mi_col, 153 find_mv_refs_sync sync, void *const data, 154 uint8_t *mode_context) { 155 find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, 156 mi_row, mi_col, sync, data, mode_context); 157} 158 159static void lower_mv_precision(MV *mv, int allow_hp) { 160 const int use_hp = allow_hp && vp9_use_mv_hp(mv); 161 if (!use_hp) { 162 if (mv->row & 1) 163 mv->row += (mv->row > 0 ? -1 : 1); 164 if (mv->col & 1) 165 mv->col += (mv->col > 0 ? -1 : 1); 166 } 167} 168 169void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, 170 int_mv *mvlist, int_mv *nearest_mv, 171 int_mv *near_mv) { 172 int i; 173 // Make sure all the candidates are properly clamped etc 174 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) { 175 lower_mv_precision(&mvlist[i].as_mv, allow_hp); 176 clamp_mv2(&mvlist[i].as_mv, xd); 177 } 178 *nearest_mv = mvlist[0]; 179 *near_mv = mvlist[1]; 180} 181 182void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, 183 int block, int ref, int mi_row, int mi_col, 184 int_mv *nearest_mv, int_mv *near_mv, 185 uint8_t *mode_context) { 186 int_mv mv_list[MAX_MV_REF_CANDIDATES]; 187 MODE_INFO *const mi = xd->mi[0]; 188 b_mode_info *bmi = mi->bmi; 189 int n; 190 191 assert(MAX_MV_REF_CANDIDATES == 2); 192 193 find_mv_refs_idx(cm, xd, mi, mi->mbmi.ref_frame[ref], mv_list, block, 194 mi_row, mi_col, NULL, NULL, mode_context); 195 196 near_mv->as_int = 0; 197 switch (block) { 198 case 0: 199 nearest_mv->as_int = mv_list[0].as_int; 200 near_mv->as_int = mv_list[1].as_int; 201 break; 202 case 1: 203 case 2: 204 nearest_mv->as_int = bmi[0].as_mv[ref].as_int; 205 for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n) 206 if (nearest_mv->as_int != mv_list[n].as_int) { 207 near_mv->as_int = mv_list[n].as_int; 208 break; 209 } 210 break; 211 case 3: { 212 int_mv candidates[2 + MAX_MV_REF_CANDIDATES]; 213 candidates[0] = bmi[1].as_mv[ref]; 214 candidates[1] = bmi[0].as_mv[ref]; 215 candidates[2] = mv_list[0]; 216 candidates[3] = mv_list[1]; 217 218 nearest_mv->as_int = bmi[2].as_mv[ref].as_int; 219 for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n) 220 if (nearest_mv->as_int != candidates[n].as_int) { 221 near_mv->as_int = candidates[n].as_int; 222 break; 223 } 224 break; 225 } 226 default: 227 assert(0 && "Invalid block index."); 228 } 229} 230