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