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_mvref_common.h"
13 
14  // This function searches the neighborhood of a given MB/SB
15 // to try and find candidate reference vectors.
16 #if 1
17 // 0: do not use reference MV(s) (only ZERO_MV as INTER candidate)
18 // 1: nearest only
19 // 2: both nearest and near
find_mv_refs_idx(EncDecContext * context_ptr,const VP9_COMMON * cm,const MACROBLOCKD * xd,ModeInfo * mi,MV_REFERENCE_FRAME ref_frame,int_mv * mv_ref_list,int block,int mi_row,int mi_col,uint8_t * mode_context)20 static int find_mv_refs_idx(EncDecContext   *context_ptr, const VP9_COMMON *cm, const MACROBLOCKD *xd,
21 #else
22 static void find_mv_refs_idx(EncDecContext   *context_ptr, const VP9_COMMON *cm, const MACROBLOCKD *xd,
23 #endif
24                              ModeInfo *mi, MV_REFERENCE_FRAME ref_frame,
25                              int_mv *mv_ref_list, int block, int mi_row,
26                              int mi_col, uint8_t *mode_context) {
27     (void)mi;
28 
29   const int *ref_sign_bias = cm->ref_frame_sign_bias;
30   int i, refmv_count = 0;
31 #if 1
32   const POSITION *const mv_ref_search = mv_ref_blocks[context_ptr->ep_block_stats_ptr->bsize];
33 #else
34   const POSITION *const mv_ref_search = mv_ref_blocks[mi->sb_type];
35 #endif
36   int different_ref_found = 0;
37   int context_counter = 0;
38 #if 0
39   const MV_REF *const prev_frame_mvs =
40       cm->use_prev_frame_mvs
41           ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
42           : NULL;
43 #endif
44   const TileInfo *const tile = &xd->tile;
45 
46   // Blank the reference vector list
47   memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
48 
49   // The nearest 2 blocks are treated differently
50   // if the size < 8x8 we get the mv from the bmi substructure,
51   // and we also need to keep a mode count.
52   for (i = 0; i < 2; ++i) {
53     const POSITION *const mv_ref = &mv_ref_search[i];
54     if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
55 
56 #if 1
57         ModeInfo *candidate_mi = context_ptr->mode_info_array[(context_ptr->mi_col + mv_ref->col)+ (context_ptr->mi_row + mv_ref->row) * context_ptr->mi_stride];
58 #else
59         const ModeInfo *const candidate_mi =
60           xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
61 #endif
62       // Keep counts for entropy encoding.
63       context_counter += mode_2_counter[candidate_mi->mode];
64       different_ref_found = 1;
65 
66       if (candidate_mi->ref_frame[0] == ref_frame)
67         ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
68                         refmv_count, mv_ref_list, Done);
69       else if (candidate_mi->ref_frame[1] == ref_frame)
70         ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
71                         refmv_count, mv_ref_list, Done);
72     }
73   }
74 
75   // Check the rest of the neighbors in much the same way
76   // as before except we don't need to keep track of sub blocks or
77   // mode counts.
78   for (; i < MVREF_NEIGHBOURS; ++i) {
79     const POSITION *const mv_ref = &mv_ref_search[i];
80 
81     if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
82 #if 1
83         ModeInfo *candidate_mi = context_ptr->mode_info_array[(context_ptr->mi_col + mv_ref->col) + (context_ptr->mi_row + mv_ref->row) * context_ptr->mi_stride];
84 #else
85         const ModeInfo *const candidate_mi =
86           xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
87 #endif
88       different_ref_found = 1;
89 
90       if (candidate_mi->ref_frame[0] == ref_frame)
91         ADD_MV_REF_LIST(candidate_mi->mv[0], refmv_count, mv_ref_list, Done);
92       else if (candidate_mi->ref_frame[1] == ref_frame)
93         ADD_MV_REF_LIST(candidate_mi->mv[1], refmv_count, mv_ref_list, Done);
94     }
95   }
96 
97 #if 1
98   if (cm->use_prev_frame_mvs == EB_TRUE) {
99       // Hsan : SVT-VP9 does not support temporal MV(s) as reference MV(s)
100       // If here then reference MV(s) will not be used (i.e. only ZERO_MV as INTER canidate)
101       mode_context[ref_frame] = (uint8_t)counter_to_context[context_counter];
102 
103       // Clamp vectors
104       for (i = 0; i < refmv_count; ++i)
105           clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
106 
107       if (refmv_count == 0)
108           return 0;
109       else if (refmv_count == 1)
110           return 1;
111       else
112           assert(0);
113   }
114 #else
115   // Check the last frame's mode and mv info.
116   if (cm->use_prev_frame_mvs) {
117       if (prev_frame_mvs->ref_frame[0] == ref_frame) {
118           ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
119       }
120       else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
121           ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
122       }
123   }
124 #endif
125   // Since we couldn't find 2 mvs from the same reference frame
126   // go back through the neighbors and find motion vectors from
127   // different reference frames.
128   if (different_ref_found) {
129     for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
130       const POSITION *mv_ref = &mv_ref_search[i];
131       if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
132 
133 #if 1
134           ModeInfo *candidate_mi = context_ptr->mode_info_array[(context_ptr->mi_col + mv_ref->col) + (context_ptr->mi_row + mv_ref->row) * context_ptr->mi_stride];
135 #else
136           const ModeInfo *const candidate_mi =
137             xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
138 #endif
139         // If the candidate is INTRA we don't want to consider its mv.
140         IF_DIFF_REF_FRAME_ADD_MV(candidate_mi, ref_frame, ref_sign_bias,
141                                  refmv_count, mv_ref_list, Done);
142       }
143     }
144   }
145 
146 #if 1
147   //if (cm->use_prev_frame_mvs == EB_TRUE)
148   {
149       // Hsan : SVT-VP9 does not support temporal MV(s) as reference MV(s)
150       // If here then reference MV(s) will not be used (i.e. only ZERO_MV as INTER canidate)
151       mode_context[ref_frame] = (uint8_t)counter_to_context[context_counter];
152 
153       // Clamp vectors
154       for (i = 0; i < refmv_count; ++i)
155           clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
156 
157       if (refmv_count == 0)
158           return 0;
159       else if (refmv_count == 1)
160           return 1;
161       else
162           assert(0);
163   }
164 #endif
165 #if 0
166   // Since we still don't have a candidate we'll try the last frame.
167   if (cm->use_prev_frame_mvs) {
168     if (prev_frame_mvs->ref_frame[0] != ref_frame &&
169         prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
170       int_mv mv = prev_frame_mvs->mv[0];
171       if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
172           ref_sign_bias[ref_frame]) {
173         mv.as_mv.row *= -1;
174         mv.as_mv.col *= -1;
175       }
176       ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
177     }
178 
179     if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
180         prev_frame_mvs->ref_frame[1] != ref_frame &&
181         prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
182       int_mv mv = prev_frame_mvs->mv[1];
183       if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
184           ref_sign_bias[ref_frame]) {
185         mv.as_mv.row *= -1;
186         mv.as_mv.col *= -1;
187       }
188       ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
189     }
190   }
191 #endif
192 Done:
193 
194   mode_context[ref_frame] = (uint8_t)counter_to_context[context_counter];
195 
196   // Clamp vectors
197   for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
198       clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
199 
200 #if 1
201   // If here then refernce MV(s) will be used
202   return 2;
203 #endif
204 }
205 #if 1
eb_vp9_find_mv_refs(EncDecContext * context_ptr,const VP9_COMMON * cm,const MACROBLOCKD * xd,ModeInfo * mi,MV_REFERENCE_FRAME ref_frame,int_mv * mv_ref_list,int mi_row,int mi_col,uint8_t * mode_context)206 int eb_vp9_find_mv_refs(EncDecContext   *context_ptr, const VP9_COMMON *cm, const MACROBLOCKD *xd,
207 #else
208 void eb_vp9_find_mv_refs(EncDecContext   *context_ptr, const VP9_COMMON *cm, const MACROBLOCKD *xd,
209 #endif
210                       ModeInfo *mi, MV_REFERENCE_FRAME ref_frame,
211                       int_mv *mv_ref_list, int mi_row, int mi_col,
212                       uint8_t *mode_context) {
213 #if 1
214     return find_mv_refs_idx(context_ptr, cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col, mode_context);
215 #else
216   find_mv_refs_idx(context_ptr, cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
217                    mode_context);
218 #endif
219 }
220 
eb_vp9_find_best_ref_mvs(MACROBLOCKD * xd,int allow_hp,int_mv * mvlist,int_mv * nearest_mv,int_mv * near_mv)221 void eb_vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
222                            int_mv *nearest_mv, int_mv *near_mv) {
223   int i;
224   // Make sure all the candidates are properly clamped etc
225   for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
226     lower_mv_precision(&mvlist[i].as_mv, allow_hp);
227     clamp_mv2(&mvlist[i].as_mv, xd);
228   }
229   *nearest_mv = mvlist[0];
230   *near_mv = mvlist[1];
231 }
232 #if 0
233 void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
234                                    int ref, int mi_row, int mi_col,
235                                    int_mv *nearest_mv, int_mv *near_mv,
236                                    uint8_t *mode_context) {
237   int_mv mv_list[MAX_MV_REF_CANDIDATES];
238   ModeInfo *const mi = xd->mi[0];
239   b_mode_info *bmi = mi->bmi;
240   int n;
241 
242   assert(MAX_MV_REF_CANDIDATES == 2);
243 
244   find_mv_refs_idx(cm, xd, mi, mi->ref_frame[ref], mv_list, block, mi_row,
245                    mi_col, mode_context);
246 
247   near_mv->as_int = 0;
248   switch (block) {
249     case 0:
250       nearest_mv->as_int = mv_list[0].as_int;
251       near_mv->as_int = mv_list[1].as_int;
252       break;
253     case 1:
254     case 2:
255       nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
256       for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
257         if (nearest_mv->as_int != mv_list[n].as_int) {
258           near_mv->as_int = mv_list[n].as_int;
259           break;
260         }
261       break;
262     case 3: {
263       int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
264       candidates[0] = bmi[1].as_mv[ref];
265       candidates[1] = bmi[0].as_mv[ref];
266       candidates[2] = mv_list[0];
267       candidates[3] = mv_list[1];
268 
269       nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
270       for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
271         if (nearest_mv->as_int != candidates[n].as_int) {
272           near_mv->as_int = candidates[n].as_int;
273           break;
274         }
275       break;
276     }
277     default: assert(0 && "Invalid block index.");
278   }
279 }
280 #endif
281