Merge "Ensure the error-concealment code is available"
[platform/upstream/libvpx.git] / vp9 / common / vp9_mvref_common.c
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.
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   vpx_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].src_mi;
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].src_mi->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   // Synchronize here for frame parallel decode if sync function is provided.
73   if (sync != NULL) {
74     sync(data, mi_row);
75   }
76
77   // Check the last frame's mode and mv info.
78   if (cm->use_prev_frame_mvs) {
79     if (prev_frame_mvs->ref_frame[0] == ref_frame) {
80       ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
81     } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
82       ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
83     }
84   }
85
86   // Since we couldn't find 2 mvs from the same reference frame
87   // go back through the neighbors and find motion vectors from
88   // different reference frames.
89   if (different_ref_found) {
90     for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
91       const POSITION *mv_ref = &mv_ref_search[i];
92       if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
93         const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
94                                               * xd->mi_stride].src_mi->mbmi;
95
96         // If the candidate is INTRA we don't want to consider its mv.
97         IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
98                                  refmv_count, mv_ref_list, Done);
99       }
100     }
101   }
102
103   // Since we still don't have a candidate we'll try the last frame.
104   if (cm->use_prev_frame_mvs) {
105     if (prev_frame_mvs->ref_frame[0] != ref_frame &&
106         prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
107       int_mv mv = prev_frame_mvs->mv[0];
108       if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
109           ref_sign_bias[ref_frame]) {
110         mv.as_mv.row *= -1;
111         mv.as_mv.col *= -1;
112       }
113       ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
114     }
115
116     if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
117         prev_frame_mvs->ref_frame[1] != ref_frame &&
118         prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
119       int_mv mv = prev_frame_mvs->mv[1];
120       if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
121           ref_sign_bias[ref_frame]) {
122         mv.as_mv.row *= -1;
123         mv.as_mv.col *= -1;
124       }
125       ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done);
126     }
127   }
128
129  Done:
130
131   mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
132
133   // Clamp vectors
134   for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
135     clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
136 }
137
138 void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
139                       const TileInfo *const tile,
140                       MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
141                       int_mv *mv_ref_list,
142                       int mi_row, int mi_col,
143                       find_mv_refs_sync sync, void *const data) {
144   find_mv_refs_idx(cm, xd, tile, mi, ref_frame, mv_ref_list, -1,
145                    mi_row, mi_col, sync, data);
146 }
147
148 static void lower_mv_precision(MV *mv, int allow_hp) {
149   const int use_hp = allow_hp && vp9_use_mv_hp(mv);
150   if (!use_hp) {
151     if (mv->row & 1)
152       mv->row += (mv->row > 0 ? -1 : 1);
153     if (mv->col & 1)
154       mv->col += (mv->col > 0 ? -1 : 1);
155   }
156 }
157
158 void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
159                            int_mv *mvlist, int_mv *nearest_mv,
160                            int_mv *near_mv) {
161   int i;
162   // Make sure all the candidates are properly clamped etc
163   for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
164     lower_mv_precision(&mvlist[i].as_mv, allow_hp);
165     clamp_mv2(&mvlist[i].as_mv, xd);
166   }
167   *nearest_mv = mvlist[0];
168   *near_mv = mvlist[1];
169 }
170
171 void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
172                                    const TileInfo *const tile,
173                                    int block, int ref, int mi_row, int mi_col,
174                                    int_mv *nearest_mv, int_mv *near_mv) {
175   int_mv mv_list[MAX_MV_REF_CANDIDATES];
176   MODE_INFO *const mi = xd->mi[0].src_mi;
177   b_mode_info *bmi = mi->bmi;
178   int n;
179
180   assert(MAX_MV_REF_CANDIDATES == 2);
181
182   find_mv_refs_idx(cm, xd, tile, mi, mi->mbmi.ref_frame[ref], mv_list, block,
183                    mi_row, mi_col, NULL, NULL);
184
185   near_mv->as_int = 0;
186   switch (block) {
187     case 0:
188       nearest_mv->as_int = mv_list[0].as_int;
189       near_mv->as_int = mv_list[1].as_int;
190       break;
191     case 1:
192     case 2:
193       nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
194       for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
195         if (nearest_mv->as_int != mv_list[n].as_int) {
196           near_mv->as_int = mv_list[n].as_int;
197           break;
198         }
199       break;
200     case 3: {
201       int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
202       candidates[0] = bmi[1].as_mv[ref];
203       candidates[1] = bmi[0].as_mv[ref];
204       candidates[2] = mv_list[0];
205       candidates[3] = mv_list[1];
206
207       nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
208       for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
209         if (nearest_mv->as_int != candidates[n].as_int) {
210           near_mv->as_int = candidates[n].as_int;
211           break;
212         }
213       break;
214     }
215     default:
216       assert("Invalid block index.");
217   }
218 }