/src/libvpx/vp8/common/findnearmv.c
Line | Count | Source |
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 | | #include "findnearmv.h" |
12 | | |
13 | | const unsigned char vp8_mbsplit_offset[4][16] = { |
14 | | { 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
15 | | { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
16 | | { 0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
17 | | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } |
18 | | }; |
19 | | |
20 | | /* Predict motion vectors using those from already-decoded nearby blocks. |
21 | | Note that we only consider one 4x4 subblock from each candidate 16x16 |
22 | | macroblock. */ |
23 | | void vp8_find_near_mvs(MACROBLOCKD *xd, const MODE_INFO *here, int_mv *nearest, |
24 | | int_mv *nearby, int_mv *best_mv, int near_mv_ref_cnts[4], |
25 | 1.67M | int refframe, int *ref_frame_sign_bias) { |
26 | 1.67M | const MODE_INFO *above = here - xd->mode_info_stride; |
27 | 1.67M | const MODE_INFO *left = here - 1; |
28 | 1.67M | const MODE_INFO *aboveleft = above - 1; |
29 | 1.67M | int_mv near_mvs[4]; |
30 | 1.67M | int_mv *mv = near_mvs; |
31 | 1.67M | int *cntx = near_mv_ref_cnts; |
32 | 1.67M | enum { CNT_INTRA, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV }; |
33 | | |
34 | | /* Zero accumulators */ |
35 | 1.67M | mv[0].as_int = mv[1].as_int = mv[2].as_int = 0; |
36 | 1.67M | near_mv_ref_cnts[0] = near_mv_ref_cnts[1] = near_mv_ref_cnts[2] = |
37 | 1.67M | near_mv_ref_cnts[3] = 0; |
38 | | |
39 | | /* Process above */ |
40 | 1.67M | if (above->mbmi.ref_frame != INTRA_FRAME) { |
41 | 494k | if (above->mbmi.mv.as_int) { |
42 | 390k | (++mv)->as_int = above->mbmi.mv.as_int; |
43 | 390k | mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, mv, |
44 | 390k | ref_frame_sign_bias); |
45 | 390k | ++cntx; |
46 | 390k | } |
47 | | |
48 | 494k | *cntx += 2; |
49 | 494k | } |
50 | | |
51 | | /* Process left */ |
52 | 1.67M | if (left->mbmi.ref_frame != INTRA_FRAME) { |
53 | 668k | if (left->mbmi.mv.as_int) { |
54 | 540k | int_mv this_mv; |
55 | | |
56 | 540k | this_mv.as_int = left->mbmi.mv.as_int; |
57 | 540k | mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &this_mv, |
58 | 540k | ref_frame_sign_bias); |
59 | | |
60 | 540k | if (this_mv.as_int != mv->as_int) { |
61 | 516k | (++mv)->as_int = this_mv.as_int; |
62 | 516k | ++cntx; |
63 | 516k | } |
64 | | |
65 | 540k | *cntx += 2; |
66 | 540k | } else { |
67 | 128k | near_mv_ref_cnts[CNT_INTRA] += 2; |
68 | 128k | } |
69 | 668k | } |
70 | | |
71 | | /* Process above left */ |
72 | 1.67M | if (aboveleft->mbmi.ref_frame != INTRA_FRAME) { |
73 | 392k | if (aboveleft->mbmi.mv.as_int) { |
74 | 316k | int_mv this_mv; |
75 | | |
76 | 316k | this_mv.as_int = aboveleft->mbmi.mv.as_int; |
77 | 316k | mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, |
78 | 316k | &this_mv, ref_frame_sign_bias); |
79 | | |
80 | 316k | if (this_mv.as_int != mv->as_int) { |
81 | 251k | (++mv)->as_int = this_mv.as_int; |
82 | 251k | ++cntx; |
83 | 251k | } |
84 | | |
85 | 316k | *cntx += 1; |
86 | 316k | } else { |
87 | 76.7k | near_mv_ref_cnts[CNT_INTRA] += 1; |
88 | 76.7k | } |
89 | 392k | } |
90 | | |
91 | | /* If we have three distinct MV's ... */ |
92 | 1.67M | if (near_mv_ref_cnts[CNT_SPLITMV]) { |
93 | | /* See if above-left MV can be merged with NEAREST */ |
94 | 145k | if (mv->as_int == near_mvs[CNT_NEAREST].as_int) |
95 | 62.7k | near_mv_ref_cnts[CNT_NEAREST] += 1; |
96 | 145k | } |
97 | | |
98 | 1.67M | near_mv_ref_cnts[CNT_SPLITMV] = |
99 | 1.67M | ((above->mbmi.mode == SPLITMV) + (left->mbmi.mode == SPLITMV)) * 2 + |
100 | 1.67M | (aboveleft->mbmi.mode == SPLITMV); |
101 | | |
102 | | /* Swap near and nearest if necessary */ |
103 | 1.67M | if (near_mv_ref_cnts[CNT_NEAR] > near_mv_ref_cnts[CNT_NEAREST]) { |
104 | 11.4k | int tmp; |
105 | 11.4k | tmp = near_mv_ref_cnts[CNT_NEAREST]; |
106 | 11.4k | near_mv_ref_cnts[CNT_NEAREST] = near_mv_ref_cnts[CNT_NEAR]; |
107 | 11.4k | near_mv_ref_cnts[CNT_NEAR] = tmp; |
108 | 11.4k | tmp = (int)near_mvs[CNT_NEAREST].as_int; |
109 | 11.4k | near_mvs[CNT_NEAREST].as_int = near_mvs[CNT_NEAR].as_int; |
110 | 11.4k | near_mvs[CNT_NEAR].as_int = (uint32_t)tmp; |
111 | 11.4k | } |
112 | | |
113 | | /* Use near_mvs[0] to store the "best" MV */ |
114 | 1.67M | if (near_mv_ref_cnts[CNT_NEAREST] >= near_mv_ref_cnts[CNT_INTRA]) { |
115 | 1.51M | near_mvs[CNT_INTRA] = near_mvs[CNT_NEAREST]; |
116 | 1.51M | } |
117 | | |
118 | | /* Set up return values */ |
119 | 1.67M | best_mv->as_int = near_mvs[0].as_int; |
120 | 1.67M | nearest->as_int = near_mvs[CNT_NEAREST].as_int; |
121 | 1.67M | nearby->as_int = near_mvs[CNT_NEAR].as_int; |
122 | 1.67M | } |
123 | | |
124 | 3.83M | static void invert_and_clamp_mvs(int_mv *inv, int_mv *src, MACROBLOCKD *xd) { |
125 | 3.83M | inv->as_mv.row = src->as_mv.row * -1; |
126 | 3.83M | inv->as_mv.col = src->as_mv.col * -1; |
127 | 3.83M | vp8_clamp_mv2(inv, xd); |
128 | 3.83M | vp8_clamp_mv2(src, xd); |
129 | 3.83M | } |
130 | | |
131 | | int vp8_find_near_mvs_bias(MACROBLOCKD *xd, const MODE_INFO *here, |
132 | | int_mv mode_mv_sb[2][MB_MODE_COUNT], |
133 | | int_mv best_mv_sb[2], int cnt[4], int refframe, |
134 | 1.27M | int *ref_frame_sign_bias) { |
135 | 1.27M | int sign_bias = ref_frame_sign_bias[refframe]; |
136 | | |
137 | 1.27M | vp8_find_near_mvs(xd, here, &mode_mv_sb[sign_bias][NEARESTMV], |
138 | 1.27M | &mode_mv_sb[sign_bias][NEARMV], &best_mv_sb[sign_bias], cnt, |
139 | 1.27M | refframe, ref_frame_sign_bias); |
140 | | |
141 | 1.27M | invert_and_clamp_mvs(&mode_mv_sb[!sign_bias][NEARESTMV], |
142 | 1.27M | &mode_mv_sb[sign_bias][NEARESTMV], xd); |
143 | 1.27M | invert_and_clamp_mvs(&mode_mv_sb[!sign_bias][NEARMV], |
144 | 1.27M | &mode_mv_sb[sign_bias][NEARMV], xd); |
145 | 1.27M | invert_and_clamp_mvs(&best_mv_sb[!sign_bias], &best_mv_sb[sign_bias], xd); |
146 | | |
147 | 1.27M | return sign_bias; |
148 | 1.27M | } |
149 | | |
150 | | vp8_prob *vp8_mv_ref_probs(vp8_prob p[VP8_MVREFS - 1], |
151 | 5.88M | const int near_mv_ref_ct[4]) { |
152 | 5.88M | p[0] = vp8_mode_contexts[near_mv_ref_ct[0]][0]; |
153 | 5.88M | p[1] = vp8_mode_contexts[near_mv_ref_ct[1]][1]; |
154 | 5.88M | p[2] = vp8_mode_contexts[near_mv_ref_ct[2]][2]; |
155 | 5.88M | p[3] = vp8_mode_contexts[near_mv_ref_ct[3]][3]; |
156 | | /* p[3] = vp8_mode_contexts[near_mv_ref_ct[1] + near_mv_ref_ct[2] + |
157 | | near_mv_ref_ct[3]][3]; */ |
158 | 5.88M | return p; |
159 | 5.88M | } |