1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12 #include "findnearmv.h"
13
14 const unsigned char vp8_mbsplit_offset[4][16] = {
15 { 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
16 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
17 { 0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
18 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
19 };
20
21 /* Predict motion vectors using those from already-decoded nearby blocks.
22 Note that we only consider one 4x4 subblock from each candidate 16x16
23 macroblock. */
vp8_find_near_mvs(MACROBLOCKD * xd,const MODE_INFO * here,MV * nearest,MV * nearby,MV * best_mv,int cnt[4],int refframe,int * ref_frame_sign_bias)24 void vp8_find_near_mvs
25 (
26 MACROBLOCKD *xd,
27 const MODE_INFO *here,
28 MV *nearest,
29 MV *nearby,
30 MV *best_mv,
31 int cnt[4],
32 int refframe,
33 int *ref_frame_sign_bias
34 )
35 {
36 const MODE_INFO *above = here - xd->mode_info_stride;
37 const MODE_INFO *left = here - 1;
38 const MODE_INFO *aboveleft = above - 1;
39 int_mv near_mvs[4];
40 int_mv *mv = near_mvs;
41 int *cntx = cnt;
42 enum {CNT_INTRA, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV};
43
44 /* Zero accumulators */
45 mv[0].as_int = mv[1].as_int = mv[2].as_int = 0;
46 cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0;
47
48 /* Process above */
49 if (above->mbmi.ref_frame != INTRA_FRAME)
50 {
51 if (above->mbmi.mv.as_int)
52 {
53 (++mv)->as_int = above->mbmi.mv.as_int;
54 mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, mv, ref_frame_sign_bias);
55 ++cntx;
56 }
57
58 *cntx += 2;
59 }
60
61 /* Process left */
62 if (left->mbmi.ref_frame != INTRA_FRAME)
63 {
64 if (left->mbmi.mv.as_int)
65 {
66 int_mv this_mv;
67
68 this_mv.as_int = left->mbmi.mv.as_int;
69 mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &this_mv, ref_frame_sign_bias);
70
71 if (this_mv.as_int != mv->as_int)
72 {
73 (++mv)->as_int = this_mv.as_int;
74 ++cntx;
75 }
76
77 *cntx += 2;
78 }
79 else
80 cnt[CNT_INTRA] += 2;
81 }
82
83 /* Process above left */
84 if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
85 {
86 if (aboveleft->mbmi.mv.as_int)
87 {
88 int_mv this_mv;
89
90 this_mv.as_int = aboveleft->mbmi.mv.as_int;
91 mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &this_mv, ref_frame_sign_bias);
92
93 if (this_mv.as_int != mv->as_int)
94 {
95 (++mv)->as_int = this_mv.as_int;
96 ++cntx;
97 }
98
99 *cntx += 1;
100 }
101 else
102 cnt[CNT_INTRA] += 1;
103 }
104
105 /* If we have three distinct MV's ... */
106 if (cnt[CNT_SPLITMV])
107 {
108 /* See if above-left MV can be merged with NEAREST */
109 if (mv->as_int == near_mvs[CNT_NEAREST].as_int)
110 cnt[CNT_NEAREST] += 1;
111 }
112
113 cnt[CNT_SPLITMV] = ((above->mbmi.mode == SPLITMV)
114 + (left->mbmi.mode == SPLITMV)) * 2
115 + (aboveleft->mbmi.mode == SPLITMV);
116
117 /* Swap near and nearest if necessary */
118 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST])
119 {
120 int tmp;
121 tmp = cnt[CNT_NEAREST];
122 cnt[CNT_NEAREST] = cnt[CNT_NEAR];
123 cnt[CNT_NEAR] = tmp;
124 tmp = near_mvs[CNT_NEAREST].as_int;
125 near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int;
126 near_mvs[CNT_NEAR].as_int = tmp;
127 }
128
129 /* Use near_mvs[0] to store the "best" MV */
130 if (cnt[CNT_NEAREST] >= cnt[CNT_INTRA])
131 near_mvs[CNT_INTRA] = near_mvs[CNT_NEAREST];
132
133 /* Set up return values */
134 *best_mv = near_mvs[0].as_mv;
135 *nearest = near_mvs[CNT_NEAREST].as_mv;
136 *nearby = near_mvs[CNT_NEAR].as_mv;
137
138 vp8_clamp_mv(nearest, xd);
139 vp8_clamp_mv(nearby, xd);
140 vp8_clamp_mv(best_mv, xd); /*TODO: move this up before the copy*/
141 }
142
vp8_mv_ref_probs(vp8_prob p[VP8_MVREFS-1],const int near_mv_ref_ct[4])143 vp8_prob *vp8_mv_ref_probs(
144 vp8_prob p[VP8_MVREFS-1], const int near_mv_ref_ct[4]
145 )
146 {
147 p[0] = vp8_mode_contexts [near_mv_ref_ct[0]] [0];
148 p[1] = vp8_mode_contexts [near_mv_ref_ct[1]] [1];
149 p[2] = vp8_mode_contexts [near_mv_ref_ct[2]] [2];
150 p[3] = vp8_mode_contexts [near_mv_ref_ct[3]] [3];
151 /*p[3] = vp8_mode_contexts [near_mv_ref_ct[1] + near_mv_ref_ct[2] + near_mv_ref_ct[3]] [3];*/
152 return p;
153 }
154
vp8_left_bmi(const MODE_INFO * cur_mb,int b)155 const B_MODE_INFO *vp8_left_bmi(const MODE_INFO *cur_mb, int b)
156 {
157 if (!(b & 3))
158 {
159 /* On L edge, get from MB to left of us */
160 --cur_mb;
161 b += 4;
162 }
163
164 return cur_mb->bmi + b - 1;
165 }
166
vp8_above_bmi(const MODE_INFO * cur_mb,int b,int mi_stride)167 const B_MODE_INFO *vp8_above_bmi(const MODE_INFO *cur_mb, int b, int mi_stride)
168 {
169 if (!(b >> 2))
170 {
171 /* On top edge, get from MB above us */
172 cur_mb -= mi_stride;
173 b += 16;
174 }
175
176 return cur_mb->bmi + b - 4;
177 }
178