/src/libvpx/vp9/common/vp9_pred_common.c
Line | Count | Source (jump to first uncovered line) |
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_common.h" |
13 | | #include "vp9/common/vp9_pred_common.h" |
14 | | #include "vp9/common/vp9_seg_common.h" |
15 | | |
16 | 34.5k | int vp9_compound_reference_allowed(const VP9_COMMON *cm) { |
17 | 34.5k | int i; |
18 | 103k | for (i = 1; i < REFS_PER_FRAME; ++i) |
19 | 69.0k | if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1; |
20 | | |
21 | 34.5k | return 0; |
22 | 34.5k | } |
23 | | |
24 | 0 | void vp9_setup_compound_reference_mode(VP9_COMMON *cm) { |
25 | 0 | if (cm->ref_frame_sign_bias[LAST_FRAME] == |
26 | 0 | cm->ref_frame_sign_bias[GOLDEN_FRAME]) { |
27 | 0 | cm->comp_fixed_ref = ALTREF_FRAME; |
28 | 0 | cm->comp_var_ref[0] = LAST_FRAME; |
29 | 0 | cm->comp_var_ref[1] = GOLDEN_FRAME; |
30 | 0 | } else if (cm->ref_frame_sign_bias[LAST_FRAME] == |
31 | 0 | cm->ref_frame_sign_bias[ALTREF_FRAME]) { |
32 | 0 | cm->comp_fixed_ref = GOLDEN_FRAME; |
33 | 0 | cm->comp_var_ref[0] = LAST_FRAME; |
34 | 0 | cm->comp_var_ref[1] = ALTREF_FRAME; |
35 | 0 | } else { |
36 | 0 | cm->comp_fixed_ref = LAST_FRAME; |
37 | 0 | cm->comp_var_ref[0] = GOLDEN_FRAME; |
38 | 0 | cm->comp_var_ref[1] = ALTREF_FRAME; |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | | int vp9_get_reference_mode_context(const VP9_COMMON *cm, |
43 | 0 | const MACROBLOCKD *xd) { |
44 | 0 | int ctx; |
45 | 0 | const MODE_INFO *const above_mi = xd->above_mi; |
46 | 0 | const MODE_INFO *const left_mi = xd->left_mi; |
47 | 0 | const int has_above = !!above_mi; |
48 | 0 | const int has_left = !!left_mi; |
49 | | // Note: |
50 | | // The mode info data structure has a one element border above and to the |
51 | | // left of the entries corresponding to real macroblocks. |
52 | | // The prediction flags in these dummy entries are initialized to 0. |
53 | 0 | if (has_above && has_left) { // both edges available |
54 | 0 | if (!has_second_ref(above_mi) && !has_second_ref(left_mi)) |
55 | | // neither edge uses comp pred (0/1) |
56 | 0 | ctx = (above_mi->ref_frame[0] == cm->comp_fixed_ref) ^ |
57 | 0 | (left_mi->ref_frame[0] == cm->comp_fixed_ref); |
58 | 0 | else if (!has_second_ref(above_mi)) |
59 | | // one of two edges uses comp pred (2/3) |
60 | 0 | ctx = 2 + (above_mi->ref_frame[0] == cm->comp_fixed_ref || |
61 | 0 | !is_inter_block(above_mi)); |
62 | 0 | else if (!has_second_ref(left_mi)) |
63 | | // one of two edges uses comp pred (2/3) |
64 | 0 | ctx = 2 + (left_mi->ref_frame[0] == cm->comp_fixed_ref || |
65 | 0 | !is_inter_block(left_mi)); |
66 | 0 | else // both edges use comp pred (4) |
67 | 0 | ctx = 4; |
68 | 0 | } else if (has_above || has_left) { // one edge available |
69 | 0 | const MODE_INFO *edge_mi = has_above ? above_mi : left_mi; |
70 | |
|
71 | 0 | if (!has_second_ref(edge_mi)) |
72 | | // edge does not use comp pred (0/1) |
73 | 0 | ctx = edge_mi->ref_frame[0] == cm->comp_fixed_ref; |
74 | 0 | else |
75 | | // edge uses comp pred (3) |
76 | 0 | ctx = 3; |
77 | 0 | } else { // no edges available (1) |
78 | 0 | ctx = 1; |
79 | 0 | } |
80 | 0 | assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS); |
81 | 0 | return ctx; |
82 | 0 | } |
83 | | |
84 | | // Returns a context number for the given MB prediction signal |
85 | | int vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm, |
86 | 0 | const MACROBLOCKD *xd) { |
87 | 0 | int pred_context; |
88 | 0 | const MODE_INFO *const above_mi = xd->above_mi; |
89 | 0 | const MODE_INFO *const left_mi = xd->left_mi; |
90 | 0 | const int above_in_image = !!above_mi; |
91 | 0 | const int left_in_image = !!left_mi; |
92 | | |
93 | | // Note: |
94 | | // The mode info data structure has a one element border above and to the |
95 | | // left of the entries corresponding to real macroblocks. |
96 | | // The prediction flags in these dummy entries are initialized to 0. |
97 | 0 | const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref]; |
98 | 0 | const int var_ref_idx = !fix_ref_idx; |
99 | |
|
100 | 0 | if (above_in_image && left_in_image) { // both edges available |
101 | 0 | const int above_intra = !is_inter_block(above_mi); |
102 | 0 | const int left_intra = !is_inter_block(left_mi); |
103 | |
|
104 | 0 | if (above_intra && left_intra) { // intra/intra (2) |
105 | 0 | pred_context = 2; |
106 | 0 | } else if (above_intra || left_intra) { // intra/inter |
107 | 0 | const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi; |
108 | |
|
109 | 0 | if (!has_second_ref(edge_mi)) // single pred (1/3) |
110 | 0 | pred_context = 1 + 2 * (edge_mi->ref_frame[0] != cm->comp_var_ref[1]); |
111 | 0 | else // comp pred (1/3) |
112 | 0 | pred_context = |
113 | 0 | 1 + 2 * (edge_mi->ref_frame[var_ref_idx] != cm->comp_var_ref[1]); |
114 | 0 | } else { // inter/inter |
115 | 0 | const int l_sg = !has_second_ref(left_mi); |
116 | 0 | const int a_sg = !has_second_ref(above_mi); |
117 | 0 | const MV_REFERENCE_FRAME vrfa = |
118 | 0 | a_sg ? above_mi->ref_frame[0] : above_mi->ref_frame[var_ref_idx]; |
119 | 0 | const MV_REFERENCE_FRAME vrfl = |
120 | 0 | l_sg ? left_mi->ref_frame[0] : left_mi->ref_frame[var_ref_idx]; |
121 | |
|
122 | 0 | if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) { |
123 | 0 | pred_context = 0; |
124 | 0 | } else if (l_sg && a_sg) { // single/single |
125 | 0 | if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) || |
126 | 0 | (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0])) |
127 | 0 | pred_context = 4; |
128 | 0 | else if (vrfa == vrfl) |
129 | 0 | pred_context = 3; |
130 | 0 | else |
131 | 0 | pred_context = 1; |
132 | 0 | } else if (l_sg || a_sg) { // single/comp |
133 | 0 | const MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl; |
134 | 0 | const MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl; |
135 | 0 | if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1]) |
136 | 0 | pred_context = 1; |
137 | 0 | else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1]) |
138 | 0 | pred_context = 2; |
139 | 0 | else |
140 | 0 | pred_context = 4; |
141 | 0 | } else if (vrfa == vrfl) { // comp/comp |
142 | 0 | pred_context = 4; |
143 | 0 | } else { |
144 | 0 | pred_context = 2; |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } else if (above_in_image || left_in_image) { // one edge available |
148 | 0 | const MODE_INFO *edge_mi = above_in_image ? above_mi : left_mi; |
149 | |
|
150 | 0 | if (!is_inter_block(edge_mi)) { |
151 | 0 | pred_context = 2; |
152 | 0 | } else { |
153 | 0 | if (has_second_ref(edge_mi)) |
154 | 0 | pred_context = |
155 | 0 | 4 * (edge_mi->ref_frame[var_ref_idx] != cm->comp_var_ref[1]); |
156 | 0 | else |
157 | 0 | pred_context = 3 * (edge_mi->ref_frame[0] != cm->comp_var_ref[1]); |
158 | 0 | } |
159 | 0 | } else { // no edges available (2) |
160 | 0 | pred_context = 2; |
161 | 0 | } |
162 | 0 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
163 | |
|
164 | 0 | return pred_context; |
165 | 0 | } |
166 | | |
167 | 7.16M | int vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) { |
168 | 7.16M | int pred_context; |
169 | 7.16M | const MODE_INFO *const above_mi = xd->above_mi; |
170 | 7.16M | const MODE_INFO *const left_mi = xd->left_mi; |
171 | 7.16M | const int has_above = !!above_mi; |
172 | 7.16M | const int has_left = !!left_mi; |
173 | | // Note: |
174 | | // The mode info data structure has a one element border above and to the |
175 | | // left of the entries corresponding to real macroblocks. |
176 | | // The prediction flags in these dummy entries are initialized to 0. |
177 | 7.16M | if (has_above && has_left) { // both edges available |
178 | 4.96M | const int above_intra = !is_inter_block(above_mi); |
179 | 4.96M | const int left_intra = !is_inter_block(left_mi); |
180 | | |
181 | 4.96M | if (above_intra && left_intra) { // intra/intra |
182 | 2.05M | pred_context = 2; |
183 | 2.90M | } else if (above_intra || left_intra) { // intra/inter or inter/intra |
184 | 851k | const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi; |
185 | 851k | if (!has_second_ref(edge_mi)) |
186 | 851k | pred_context = 4 * (edge_mi->ref_frame[0] == LAST_FRAME); |
187 | 0 | else |
188 | 0 | pred_context = 1 + (edge_mi->ref_frame[0] == LAST_FRAME || |
189 | 0 | edge_mi->ref_frame[1] == LAST_FRAME); |
190 | 2.05M | } else { // inter/inter |
191 | 2.05M | const int above_has_second = has_second_ref(above_mi); |
192 | 2.05M | const int left_has_second = has_second_ref(left_mi); |
193 | 2.05M | const MV_REFERENCE_FRAME above0 = above_mi->ref_frame[0]; |
194 | 2.05M | const MV_REFERENCE_FRAME above1 = above_mi->ref_frame[1]; |
195 | 2.05M | const MV_REFERENCE_FRAME left0 = left_mi->ref_frame[0]; |
196 | 2.05M | const MV_REFERENCE_FRAME left1 = left_mi->ref_frame[1]; |
197 | | |
198 | 2.05M | if (above_has_second && left_has_second) { |
199 | 0 | pred_context = 1 + (above0 == LAST_FRAME || above1 == LAST_FRAME || |
200 | 0 | left0 == LAST_FRAME || left1 == LAST_FRAME); |
201 | 2.05M | } else if (above_has_second || left_has_second) { |
202 | 0 | const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0; |
203 | 0 | const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0; |
204 | 0 | const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1; |
205 | |
|
206 | 0 | if (rfs == LAST_FRAME) |
207 | 0 | pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME); |
208 | 0 | else |
209 | 0 | pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME); |
210 | 2.05M | } else { |
211 | 2.05M | pred_context = 2 * (above0 == LAST_FRAME) + 2 * (left0 == LAST_FRAME); |
212 | 2.05M | } |
213 | 2.05M | } |
214 | 4.96M | } else if (has_above || has_left) { // one edge available |
215 | 1.90M | const MODE_INFO *edge_mi = has_above ? above_mi : left_mi; |
216 | 1.90M | if (!is_inter_block(edge_mi)) { // intra |
217 | 944k | pred_context = 2; |
218 | 959k | } else { // inter |
219 | 959k | if (!has_second_ref(edge_mi)) |
220 | 959k | pred_context = 4 * (edge_mi->ref_frame[0] == LAST_FRAME); |
221 | 0 | else |
222 | 0 | pred_context = 1 + (edge_mi->ref_frame[0] == LAST_FRAME || |
223 | 0 | edge_mi->ref_frame[1] == LAST_FRAME); |
224 | 959k | } |
225 | 1.90M | } else { // no edges available |
226 | 290k | pred_context = 2; |
227 | 290k | } |
228 | | |
229 | 7.16M | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
230 | 7.16M | return pred_context; |
231 | 7.16M | } |
232 | | |
233 | 6.41M | int vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) { |
234 | 6.41M | int pred_context; |
235 | 6.41M | const MODE_INFO *const above_mi = xd->above_mi; |
236 | 6.41M | const MODE_INFO *const left_mi = xd->left_mi; |
237 | 6.41M | const int has_above = !!above_mi; |
238 | 6.41M | const int has_left = !!left_mi; |
239 | | |
240 | | // Note: |
241 | | // The mode info data structure has a one element border above and to the |
242 | | // left of the entries corresponding to real macroblocks. |
243 | | // The prediction flags in these dummy entries are initialized to 0. |
244 | 6.41M | if (has_above && has_left) { // both edges available |
245 | 4.42M | const int above_intra = !is_inter_block(above_mi); |
246 | 4.42M | const int left_intra = !is_inter_block(left_mi); |
247 | | |
248 | 4.42M | if (above_intra && left_intra) { // intra/intra |
249 | 2.02M | pred_context = 2; |
250 | 2.39M | } else if (above_intra || left_intra) { // intra/inter or inter/intra |
251 | 755k | const MODE_INFO *edge_mi = above_intra ? left_mi : above_mi; |
252 | 755k | if (!has_second_ref(edge_mi)) { |
253 | 755k | if (edge_mi->ref_frame[0] == LAST_FRAME) |
254 | 443k | pred_context = 3; |
255 | 311k | else |
256 | 311k | pred_context = 4 * (edge_mi->ref_frame[0] == GOLDEN_FRAME); |
257 | 755k | } else { |
258 | 0 | pred_context = 1 + 2 * (edge_mi->ref_frame[0] == GOLDEN_FRAME || |
259 | 0 | edge_mi->ref_frame[1] == GOLDEN_FRAME); |
260 | 0 | } |
261 | 1.64M | } else { // inter/inter |
262 | 1.64M | const int above_has_second = has_second_ref(above_mi); |
263 | 1.64M | const int left_has_second = has_second_ref(left_mi); |
264 | 1.64M | const MV_REFERENCE_FRAME above0 = above_mi->ref_frame[0]; |
265 | 1.64M | const MV_REFERENCE_FRAME above1 = above_mi->ref_frame[1]; |
266 | 1.64M | const MV_REFERENCE_FRAME left0 = left_mi->ref_frame[0]; |
267 | 1.64M | const MV_REFERENCE_FRAME left1 = left_mi->ref_frame[1]; |
268 | | |
269 | 1.64M | if (above_has_second && left_has_second) { |
270 | 0 | if (above0 == left0 && above1 == left1) |
271 | 0 | pred_context = |
272 | 0 | 3 * (above0 == GOLDEN_FRAME || above1 == GOLDEN_FRAME || |
273 | 0 | left0 == GOLDEN_FRAME || left1 == GOLDEN_FRAME); |
274 | 0 | else |
275 | 0 | pred_context = 2; |
276 | 1.64M | } else if (above_has_second || left_has_second) { |
277 | 0 | const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0; |
278 | 0 | const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0; |
279 | 0 | const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1; |
280 | |
|
281 | 0 | if (rfs == GOLDEN_FRAME) |
282 | 0 | pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); |
283 | 0 | else if (rfs == ALTREF_FRAME) |
284 | 0 | pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME; |
285 | 0 | else |
286 | 0 | pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); |
287 | 1.64M | } else { |
288 | 1.64M | if (above0 == LAST_FRAME && left0 == LAST_FRAME) { |
289 | 808k | pred_context = 3; |
290 | 832k | } else if (above0 == LAST_FRAME || left0 == LAST_FRAME) { |
291 | 353k | const MV_REFERENCE_FRAME edge0 = |
292 | 353k | (above0 == LAST_FRAME) ? left0 : above0; |
293 | 353k | pred_context = 4 * (edge0 == GOLDEN_FRAME); |
294 | 479k | } else { |
295 | 479k | pred_context = |
296 | 479k | 2 * (above0 == GOLDEN_FRAME) + 2 * (left0 == GOLDEN_FRAME); |
297 | 479k | } |
298 | 1.64M | } |
299 | 1.64M | } |
300 | 4.42M | } else if (has_above || has_left) { // one edge available |
301 | 1.73M | const MODE_INFO *edge_mi = has_above ? above_mi : left_mi; |
302 | | |
303 | 1.73M | if (!is_inter_block(edge_mi) || |
304 | 1.73M | (edge_mi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mi))) |
305 | 1.42M | pred_context = 2; |
306 | 304k | else if (!has_second_ref(edge_mi)) |
307 | 304k | pred_context = 4 * (edge_mi->ref_frame[0] == GOLDEN_FRAME); |
308 | 0 | else |
309 | 0 | pred_context = 3 * (edge_mi->ref_frame[0] == GOLDEN_FRAME || |
310 | 0 | edge_mi->ref_frame[1] == GOLDEN_FRAME); |
311 | 1.73M | } else { // no edges available (2) |
312 | 261k | pred_context = 2; |
313 | 261k | } |
314 | 6.41M | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
315 | 6.41M | return pred_context; |
316 | 6.41M | } |