blob: 5b7c2ecfcb5a658d09aaeeac24d47a1766cd5d22 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001
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 "av1/common/common.h"
13#include "av1/common/pred_common.h"
14#include "av1/common/reconinter.h"
15#include "av1/common/seg_common.h"
16
17// Returns a context number for the given MB prediction signal
18#if CONFIG_DUAL_FILTER
James Zern7b9407a2016-05-18 23:48:05 -070019static InterpFilter get_ref_filter_type(const MODE_INFO *mi,
20 const MACROBLOCKD *xd, int dir,
21 MV_REFERENCE_FRAME ref_frame) {
22 InterpFilter ref_type = SWITCHABLE_FILTERS;
Yaowu Xuc27fc142016-08-22 16:08:15 -070023 const MB_MODE_INFO *ref_mbmi = &mi->mbmi;
24 int use_subpel[2] = {
25 has_subpel_mv_component(mi, xd, dir),
26 has_subpel_mv_component(mi, xd, dir + 2),
27 };
28
29 if (ref_mbmi->ref_frame[0] == ref_frame && use_subpel[0])
30 ref_type = ref_mbmi->interp_filter[(dir & 0x01)];
31 else if (ref_mbmi->ref_frame[1] == ref_frame && use_subpel[1])
32 ref_type = ref_mbmi->interp_filter[(dir & 0x01) + 2];
33
34 return ref_type;
35}
36
Yaowu Xuf883b422016-08-30 14:01:10 -070037int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd, int dir) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070038 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
39 const int ctx_offset =
40 (mbmi->ref_frame[1] > INTRA_FRAME) * INTER_FILTER_COMP_OFFSET;
41 MV_REFERENCE_FRAME ref_frame =
42 (dir < 2) ? mbmi->ref_frame[0] : mbmi->ref_frame[1];
43 // Note:
44 // The mode info data structure has a one element border above and to the
45 // left of the entries corresponding to real macroblocks.
46 // The prediction flags in these dummy entries are initialized to 0.
47 int filter_type_ctx = ctx_offset + (dir & 0x01) * INTER_FILTER_DIR_OFFSET;
48 int left_type = SWITCHABLE_FILTERS;
49 int above_type = SWITCHABLE_FILTERS;
50
51 if (xd->left_available)
52 left_type = get_ref_filter_type(xd->mi[-1], xd, dir, ref_frame);
53
54 if (xd->up_available)
55 above_type =
56 get_ref_filter_type(xd->mi[-xd->mi_stride], xd, dir, ref_frame);
57
58 if (left_type == above_type)
59 filter_type_ctx += left_type;
60 else if (left_type == SWITCHABLE_FILTERS && above_type != SWITCHABLE_FILTERS)
61 filter_type_ctx += above_type;
62 else if (left_type != SWITCHABLE_FILTERS && above_type == SWITCHABLE_FILTERS)
63 filter_type_ctx += left_type;
64 else
65 filter_type_ctx += SWITCHABLE_FILTERS;
66
67 return filter_type_ctx;
68}
69#else
Yaowu Xuf883b422016-08-30 14:01:10 -070070int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070071 // Note:
72 // The mode info data structure has a one element border above and to the
73 // left of the entries corresponding to real macroblocks.
74 // The prediction flags in these dummy entries are initialized to 0.
75 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
76 const int left_type = xd->left_available && is_inter_block(left_mbmi)
77 ? left_mbmi->interp_filter
78 : SWITCHABLE_FILTERS;
79 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
80 const int above_type = xd->up_available && is_inter_block(above_mbmi)
81 ? above_mbmi->interp_filter
82 : SWITCHABLE_FILTERS;
83
84 if (left_type == above_type)
85 return left_type;
86 else if (left_type == SWITCHABLE_FILTERS && above_type != SWITCHABLE_FILTERS)
87 return above_type;
88 else if (left_type != SWITCHABLE_FILTERS && above_type == SWITCHABLE_FILTERS)
89 return left_type;
90 else
91 return SWITCHABLE_FILTERS;
92}
93#endif
94
95#if CONFIG_EXT_INTRA
96// Obtain the reference filter type from the above/left neighbor blocks.
97static INTRA_FILTER get_ref_intra_filter(const MB_MODE_INFO *ref_mbmi) {
98 INTRA_FILTER ref_type = INTRA_FILTERS;
99
100 if (ref_mbmi->sb_type >= BLOCK_8X8) {
101 PREDICTION_MODE mode = ref_mbmi->mode;
102 if (is_inter_block(ref_mbmi)) {
103#if CONFIG_DUAL_FILTER
104 switch (ref_mbmi->interp_filter[0]) {
105#else
106 switch (ref_mbmi->interp_filter) {
107#endif
108 case EIGHTTAP_REGULAR: ref_type = INTRA_FILTER_8TAP; break;
109 case EIGHTTAP_SMOOTH: ref_type = INTRA_FILTER_8TAP_SMOOTH; break;
110 case MULTITAP_SHARP: ref_type = INTRA_FILTER_8TAP_SHARP; break;
111 case BILINEAR: ref_type = INTRA_FILTERS; break;
112 default: break;
113 }
114 } else {
115 if (mode != DC_PRED && mode != TM_PRED) {
116 int p_angle =
117 mode_to_angle_map[mode] + ref_mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -0700118 if (av1_is_intra_filter_switchable(p_angle)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119 ref_type = ref_mbmi->intra_filter;
120 }
121 }
122 }
123 }
124 return ref_type;
125}
126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127int av1_get_pred_context_intra_interp(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 int left_type = INTRA_FILTERS, above_type = INTRA_FILTERS;
129
130 if (xd->left_available) left_type = get_ref_intra_filter(xd->left_mbmi);
131
132 if (xd->up_available) above_type = get_ref_intra_filter(xd->above_mbmi);
133
134 if (left_type == above_type)
135 return left_type;
136 else if (left_type == INTRA_FILTERS && above_type != INTRA_FILTERS)
137 return above_type;
138 else if (left_type != INTRA_FILTERS && above_type == INTRA_FILTERS)
139 return left_type;
140 else
141 return INTRA_FILTERS;
142}
143#endif // CONFIG_EXT_INTRA
144
145// The mode info data structure has a one element border above and to the
146// left of the entries corresponding to real macroblocks.
147// The prediction flags in these dummy entries are initialized to 0.
148// 0 - inter/inter, inter/--, --/inter, --/--
149// 1 - intra/inter, inter/intra
150// 2 - intra/--, --/intra
151// 3 - intra/intra
Yaowu Xuf883b422016-08-30 14:01:10 -0700152int av1_get_intra_inter_context(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
154 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
155 const int has_above = xd->up_available;
156 const int has_left = xd->left_available;
157
158 if (has_above && has_left) { // both edges available
159 const int above_intra = !is_inter_block(above_mbmi);
160 const int left_intra = !is_inter_block(left_mbmi);
161 return left_intra && above_intra ? 3 : left_intra || above_intra;
162 } else if (has_above || has_left) { // one edge available
163 return 2 * !is_inter_block(has_above ? above_mbmi : left_mbmi);
164 } else {
165 return 0;
166 }
167}
168
169#if CONFIG_EXT_REFS
Zoe Liu6cfaff92016-10-18 17:12:11 -0700170#define CHECK_BACKWARD_REFS(ref_frame) \
171 (((ref_frame) >= BWDREF_FRAME) && ((ref_frame) <= ALTREF_FRAME))
172#define IS_BACKWARD_REF_FRAME(ref_frame) CHECK_BACKWARD_REFS(ref_frame)
173#else
174#define IS_BACKWARD_REF_FRAME(ref_frame) ((ref_frame) == cm->comp_fixed_ref)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175#endif // CONFIG_EXT_REFS
176
Zoe Liu6cfaff92016-10-18 17:12:11 -0700177int av1_get_reference_mode_context(const AV1_COMMON *cm,
178 const MACROBLOCKD *xd) {
179 int ctx;
180 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
181 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
182 const int has_above = xd->up_available;
183 const int has_left = xd->left_available;
184
185#if CONFIG_EXT_REFS
186 (void)cm;
187#endif // CONFIG_EXT_REFS
188
189 // Note:
190 // The mode info data structure has a one element border above and to the
191 // left of the entries corresponding to real macroblocks.
192 // The prediction flags in these dummy entries are initialized to 0.
193 if (has_above && has_left) { // both edges available
194 if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi))
195 // neither edge uses comp pred (0/1)
196 ctx = IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ^
197 IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]);
198 else if (!has_second_ref(above_mbmi))
199 // one of two edges uses comp pred (2/3)
200 ctx = 2 + (IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ||
201 !is_inter_block(above_mbmi));
202 else if (!has_second_ref(left_mbmi))
203 // one of two edges uses comp pred (2/3)
204 ctx = 2 + (IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]) ||
205 !is_inter_block(left_mbmi));
206 else // both edges use comp pred (4)
207 ctx = 4;
208 } else if (has_above || has_left) { // one edge available
209 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
210
211 if (!has_second_ref(edge_mbmi))
212 // edge does not use comp pred (0/1)
213 ctx = IS_BACKWARD_REF_FRAME(edge_mbmi->ref_frame[0]);
214 else
215 // edge uses comp pred (3)
216 ctx = 3;
217 } else { // no edges available (1)
218 ctx = 1;
219 }
220 assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
221 return ctx;
222}
223
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224#if CONFIG_EXT_REFS
225
226// TODO(zoeliu): Future work will be conducted to optimize the context design
227// for the coding of the reference frames.
228
229#define CHECK_LAST_OR_LAST2(ref_frame) \
230 ((ref_frame == LAST_FRAME) || (ref_frame == LAST2_FRAME))
231
232#define CHECK_GOLDEN_OR_LAST3(ref_frame) \
233 ((ref_frame == GOLDEN_FRAME) || (ref_frame == LAST3_FRAME))
234
235// Returns a context number for the given MB prediction signal
236// Signal the first reference frame for a compound mode be either
237// GOLDEN/LAST3, or LAST/LAST2.
238//
239// NOTE(zoeliu): The probability of ref_frame[0] is either
240// GOLDEN_FRAME or LAST3_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -0700241int av1_get_pred_context_comp_ref_p(const AV1_COMMON *cm,
242 const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 int pred_context;
244 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
245 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
246 const int above_in_image = xd->up_available;
247 const int left_in_image = xd->left_available;
248
249 // Note:
250 // The mode info data structure has a one element border above and to the
251 // left of the entries correpsonding to real macroblocks.
252 // The prediction flags in these dummy entries are initialised to 0.
253 const int bwd_ref_sign_idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
254 const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
255
256 if (above_in_image && left_in_image) { // both edges available
257 const int above_intra = !is_inter_block(above_mbmi);
258 const int left_intra = !is_inter_block(left_mbmi);
259
260 if (above_intra && left_intra) { // intra/intra (2)
261 pred_context = 2;
262 } else if (above_intra || left_intra) { // intra/inter
263 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
264
265 if (!has_second_ref(edge_mbmi)) // single pred (1/3)
266 pred_context =
267 1 + 2 * (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]));
268 else // comp pred (1/3)
269 pred_context = 1 +
270 2 * (!CHECK_GOLDEN_OR_LAST3(
271 edge_mbmi->ref_frame[fwd_ref_sign_idx]));
272 } else { // inter/inter
273 const int l_sg = !has_second_ref(left_mbmi);
274 const int a_sg = !has_second_ref(above_mbmi);
275 const MV_REFERENCE_FRAME frfa =
276 a_sg ? above_mbmi->ref_frame[0]
277 : above_mbmi->ref_frame[fwd_ref_sign_idx];
278 const MV_REFERENCE_FRAME frfl =
279 l_sg ? left_mbmi->ref_frame[0]
280 : left_mbmi->ref_frame[fwd_ref_sign_idx];
281
282 if (frfa == frfl && CHECK_GOLDEN_OR_LAST3(frfa)) {
283 pred_context = 0;
284 } else if (l_sg && a_sg) { // single/single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700285 if ((CHECK_BACKWARD_REFS(frfa) && CHECK_LAST_OR_LAST2(frfl)) ||
286 (CHECK_BACKWARD_REFS(frfl) && CHECK_LAST_OR_LAST2(frfa))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 pred_context = 4;
288 } else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl)) {
289 pred_context = 1;
290 } else {
291 pred_context = 3;
292 }
293 } else if (l_sg || a_sg) { // single/comp
294 const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
295 const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
296
297 if (CHECK_GOLDEN_OR_LAST3(frfc) && !CHECK_GOLDEN_OR_LAST3(rfs))
298 pred_context = 1;
299 else if (CHECK_GOLDEN_OR_LAST3(rfs) && !CHECK_GOLDEN_OR_LAST3(frfc))
300 pred_context = 2;
301 else
302 pred_context = 4;
303 } else { // comp/comp
304 if ((CHECK_LAST_OR_LAST2(frfa) && CHECK_LAST_OR_LAST2(frfl))) {
305 pred_context = 4;
306 } else {
307 // NOTE(zoeliu): Following assert may be removed once confirmed.
308 assert(CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl));
309 pred_context = 2;
310 }
311 }
312 }
313 } else if (above_in_image || left_in_image) { // one edge available
314 const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
315
316 if (!is_inter_block(edge_mbmi)) {
317 pred_context = 2;
318 } else {
319 if (has_second_ref(edge_mbmi))
320 pred_context =
321 4 *
322 (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[fwd_ref_sign_idx]));
323 else
324 pred_context = 3 * (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]));
325 }
326 } else { // no edges available (2)
327 pred_context = 2;
328 }
329
330 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
331
332 return pred_context;
333}
334
335// Returns a context number for the given MB prediction signal
336// Signal the first reference frame for a compound mode be LAST,
337// conditioning on that it is known either LAST/LAST2.
338//
339// NOTE(zoeliu): The probability of ref_frame[0] is LAST_FRAME,
340// conditioning on it is either LAST_FRAME or LAST2_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -0700341int av1_get_pred_context_comp_ref_p1(const AV1_COMMON *cm,
342 const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 int pred_context;
344 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
345 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
346 const int above_in_image = xd->up_available;
347 const int left_in_image = xd->left_available;
348
349 // Note:
350 // The mode info data structure has a one element border above and to the
351 // left of the entries correpsonding to real macroblocks.
352 // The prediction flags in these dummy entries are initialised to 0.
353 const int bwd_ref_sign_idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
354 const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
355
356 if (above_in_image && left_in_image) { // both edges available
357 const int above_intra = !is_inter_block(above_mbmi);
358 const int left_intra = !is_inter_block(left_mbmi);
359
360 if (above_intra && left_intra) { // intra/intra (2)
361 pred_context = 2;
362 } else if (above_intra || left_intra) { // intra/inter
363 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
364
365 if (!has_second_ref(edge_mbmi)) // single pred (1/3)
366 pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != LAST_FRAME);
367 else // comp pred (1/3)
368 pred_context =
369 1 + 2 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST_FRAME);
370 } else { // inter/inter
371 const int l_sg = !has_second_ref(left_mbmi);
372 const int a_sg = !has_second_ref(above_mbmi);
373 const MV_REFERENCE_FRAME frfa =
374 a_sg ? above_mbmi->ref_frame[0]
375 : above_mbmi->ref_frame[fwd_ref_sign_idx];
376 const MV_REFERENCE_FRAME frfl =
377 l_sg ? left_mbmi->ref_frame[0]
378 : left_mbmi->ref_frame[fwd_ref_sign_idx];
379
380 if (frfa == frfl && frfa == LAST_FRAME)
381 pred_context = 0;
382 else if (l_sg && a_sg) { // single/single
383 if (frfa == LAST_FRAME || frfl == LAST_FRAME)
384 pred_context = 1;
385 else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl))
386 pred_context = 2 + (frfa != frfl);
387 else if (frfa == frfl ||
Zoe Liu6cfaff92016-10-18 17:12:11 -0700388 (CHECK_BACKWARD_REFS(frfa) && CHECK_BACKWARD_REFS(frfl)))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389 pred_context = 3;
390 else
391 pred_context = 4;
392 } else if (l_sg || a_sg) { // single/comp
393 const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
394 const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
395
396 if (frfc == LAST_FRAME && rfs != LAST_FRAME)
397 pred_context = 1;
398 else if (rfs == LAST_FRAME && frfc != LAST_FRAME)
399 pred_context = 2;
400 else
401 pred_context =
402 3 + (frfc == LAST2_FRAME || CHECK_GOLDEN_OR_LAST3(rfs));
403 } else { // comp/comp
404 if (frfa == LAST_FRAME || frfl == LAST_FRAME)
405 pred_context = 2;
406 else
407 pred_context =
408 3 + (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl));
409 }
410 }
411 } else if (above_in_image || left_in_image) { // one edge available
412 const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
413
414 if (!is_inter_block(edge_mbmi)) {
415 pred_context = 2;
416 } else {
417 if (has_second_ref(edge_mbmi)) {
418 pred_context =
419 4 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST_FRAME);
420 } else {
421 if (edge_mbmi->ref_frame[0] == LAST_FRAME)
422 pred_context = 0;
423 else
424 pred_context = 2 + CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]);
425 }
426 }
427 } else { // no edges available (2)
428 pred_context = 2;
429 }
430
431 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
432
433 return pred_context;
434}
435
436// Returns a context number for the given MB prediction signal
437// Signal the first reference frame for a compound mode be GOLDEN,
438// conditioning on that it is known either GOLDEN or LAST3.
439//
440// NOTE(zoeliu): The probability of ref_frame[0] is GOLDEN_FRAME,
441// conditioning on it is either GOLDEN or LAST3.
Yaowu Xuf883b422016-08-30 14:01:10 -0700442int av1_get_pred_context_comp_ref_p2(const AV1_COMMON *cm,
443 const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700444 int pred_context;
445 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
446 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
447 const int above_in_image = xd->up_available;
448 const int left_in_image = xd->left_available;
449
450 // Note:
451 // The mode info data structure has a one element border above and to the
452 // left of the entries correpsonding to real macroblocks.
453 // The prediction flags in these dummy entries are initialised to 0.
454 const int bwd_ref_sign_idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
455 const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
456
457 if (above_in_image && left_in_image) { // both edges available
458 const int above_intra = !is_inter_block(above_mbmi);
459 const int left_intra = !is_inter_block(left_mbmi);
460
461 if (above_intra && left_intra) { // intra/intra (2)
462 pred_context = 2;
463 } else if (above_intra || left_intra) { // intra/inter
464 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
465
466 if (!has_second_ref(edge_mbmi)) // single pred (1/3)
467 pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != GOLDEN_FRAME);
468 else // comp pred (1/3)
469 pred_context =
470 1 + 2 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != GOLDEN_FRAME);
471 } else { // inter/inter
472 const int l_sg = !has_second_ref(left_mbmi);
473 const int a_sg = !has_second_ref(above_mbmi);
474 const MV_REFERENCE_FRAME frfa =
475 a_sg ? above_mbmi->ref_frame[0]
476 : above_mbmi->ref_frame[fwd_ref_sign_idx];
477 const MV_REFERENCE_FRAME frfl =
478 l_sg ? left_mbmi->ref_frame[0]
479 : left_mbmi->ref_frame[fwd_ref_sign_idx];
480
481 if (frfa == frfl && frfa == GOLDEN_FRAME)
482 pred_context = 0;
483 else if (l_sg && a_sg) { // single/single
484 if (frfa == GOLDEN_FRAME || frfl == GOLDEN_FRAME)
485 pred_context = 1;
486 else if (CHECK_LAST_OR_LAST2(frfa) || CHECK_LAST_OR_LAST2(frfl))
487 pred_context = 2 + (frfa != frfl);
488 else if (frfa == frfl ||
Zoe Liu6cfaff92016-10-18 17:12:11 -0700489 (CHECK_BACKWARD_REFS(frfa) && CHECK_BACKWARD_REFS(frfl)))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 pred_context = 3;
491 else
492 pred_context = 4;
493 } else if (l_sg || a_sg) { // single/comp
494 const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
495 const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
496
497 if (frfc == GOLDEN_FRAME && rfs != GOLDEN_FRAME)
498 pred_context = 1;
499 else if (rfs == GOLDEN_FRAME && frfc != GOLDEN_FRAME)
500 pred_context = 2;
501 else
502 pred_context = 3 + (frfc == LAST3_FRAME || CHECK_LAST_OR_LAST2(rfs));
503 } else { // comp/comp
504 if (frfa == GOLDEN_FRAME || frfl == GOLDEN_FRAME)
505 pred_context = 2;
506 else
507 pred_context =
508 3 + (CHECK_LAST_OR_LAST2(frfa) || CHECK_LAST_OR_LAST2(frfl));
509 }
510 }
511 } else if (above_in_image || left_in_image) { // one edge available
512 const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
513
514 if (!is_inter_block(edge_mbmi)) {
515 pred_context = 2;
516 } else {
517 if (has_second_ref(edge_mbmi)) {
518 pred_context =
519 4 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != GOLDEN_FRAME);
520 } else {
521 if (edge_mbmi->ref_frame[0] == GOLDEN_FRAME)
522 pred_context = 0;
523 else
524 pred_context = 2 + CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]);
525 }
526 }
527 } else { // no edges available (2)
528 pred_context = 2;
529 }
530
531 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
532
533 return pred_context;
534}
535
536// Returns a context number for the given MB prediction signal
Yaowu Xuf883b422016-08-30 14:01:10 -0700537int av1_get_pred_context_comp_bwdref_p(const AV1_COMMON *cm,
538 const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539 int pred_context;
540 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
541 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
542 const int above_in_image = xd->up_available;
543 const int left_in_image = xd->left_available;
544
545 // Note:
546 // The mode info data structure has a one element border above and to the
547 // left of the entries corresponding to real macroblocks.
548 // The prediction flags in these dummy entries are initialized to 0.
549 const int bwd_ref_sign_idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
550 const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
551
552 if (above_in_image && left_in_image) { // both edges available
553 const int above_intra = !is_inter_block(above_mbmi);
554 const int left_intra = !is_inter_block(left_mbmi);
555
556 if (above_intra && left_intra) { // intra/intra (2)
557 pred_context = 2;
558 } else if (above_intra || left_intra) { // intra/inter
559 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
560
561 if (!has_second_ref(edge_mbmi)) // single pred (1/3)
562 pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != cm->comp_bwd_ref[1]);
563 else // comp pred (1/3)
564 pred_context =
565 1 +
566 2 * (edge_mbmi->ref_frame[bwd_ref_sign_idx] != cm->comp_bwd_ref[1]);
567 } else { // inter/inter
568 const int l_comp = has_second_ref(left_mbmi);
569 const int a_comp = has_second_ref(above_mbmi);
570
571 const MV_REFERENCE_FRAME l_brf =
572 l_comp ? left_mbmi->ref_frame[bwd_ref_sign_idx] : NONE;
573 const MV_REFERENCE_FRAME a_brf =
574 a_comp ? above_mbmi->ref_frame[bwd_ref_sign_idx] : NONE;
575
576 const MV_REFERENCE_FRAME l_frf =
577 !l_comp ? left_mbmi->ref_frame[0]
578 : left_mbmi->ref_frame[fwd_ref_sign_idx];
579 const MV_REFERENCE_FRAME a_frf =
580 !a_comp ? above_mbmi->ref_frame[0]
581 : above_mbmi->ref_frame[fwd_ref_sign_idx];
582
583 if (l_comp && a_comp) { // comp/comp
584 if (l_brf == a_brf && l_brf == cm->comp_bwd_ref[1]) {
585 pred_context = 0;
586 } else if (l_brf == cm->comp_bwd_ref[1] ||
587 a_brf == cm->comp_bwd_ref[1]) {
588 pred_context = 1;
589 } else {
590 // NOTE: Backward ref should be either BWDREF or ALTREF.
591 assert(l_brf == a_brf && l_brf != cm->comp_bwd_ref[1]);
592 pred_context = 3;
593 }
594 } else if (!l_comp && !a_comp) { // single/single
595 if (l_frf == a_frf && l_frf == cm->comp_bwd_ref[1]) {
596 pred_context = 0;
597 } else if (l_frf == cm->comp_bwd_ref[1] ||
598 a_frf == cm->comp_bwd_ref[1]) {
599 pred_context = 1;
600 } else if (l_frf == a_frf) {
601 pred_context = 3;
602 } else {
603 assert(l_frf != a_frf && l_frf != cm->comp_bwd_ref[1] &&
604 a_frf != cm->comp_bwd_ref[1]);
605 pred_context = 4;
606 }
607 } else { // comp/single
608 assert((l_comp && !a_comp) || (!l_comp && a_comp));
609
610 if ((l_comp && l_brf == cm->comp_bwd_ref[1] &&
611 a_frf == cm->comp_bwd_ref[1]) ||
612 (a_comp && a_brf == cm->comp_bwd_ref[1] &&
613 l_frf == cm->comp_bwd_ref[1])) {
614 pred_context = 1;
615 } else if ((l_comp && l_brf == cm->comp_bwd_ref[1]) ||
616 (a_comp && a_brf == cm->comp_bwd_ref[1]) ||
617 (!l_comp && l_frf == cm->comp_bwd_ref[1]) ||
618 (!a_comp && a_frf == cm->comp_bwd_ref[1])) {
619 pred_context = 2;
620 } else {
621 pred_context = 4;
622 }
623 }
624 }
625 } else if (above_in_image || left_in_image) { // one edge available
626 const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
627
628 if (!is_inter_block(edge_mbmi)) {
629 pred_context = 2;
630 } else {
631 if (has_second_ref(edge_mbmi)) {
632 pred_context =
633 4 * (edge_mbmi->ref_frame[bwd_ref_sign_idx] != cm->comp_bwd_ref[1]);
634 } else {
635 pred_context = 3 * (edge_mbmi->ref_frame[0] != cm->comp_bwd_ref[1]);
636 }
637 }
638 } else { // no edges available (2)
639 pred_context = 2;
640 }
641 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
642
643 return pred_context;
644}
645
646#else // CONFIG_EXT_REFS
647
648// Returns a context number for the given MB prediction signal
Yaowu Xuf883b422016-08-30 14:01:10 -0700649int av1_get_pred_context_comp_ref_p(const AV1_COMMON *cm,
650 const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 int pred_context;
652 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
653 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
654 const int above_in_image = xd->up_available;
655 const int left_in_image = xd->left_available;
656
657 // Note:
658 // The mode info data structure has a one element border above and to the
659 // left of the entries corresponding to real macroblocks.
660 // The prediction flags in these dummy entries are initialized to 0.
661 const int fix_ref_idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
662 const int var_ref_idx = !fix_ref_idx;
663
664 if (above_in_image && left_in_image) { // both edges available
665 const int above_intra = !is_inter_block(above_mbmi);
666 const int left_intra = !is_inter_block(left_mbmi);
667
668 if (above_intra && left_intra) { // intra/intra (2)
669 pred_context = 2;
670 } else if (above_intra || left_intra) { // intra/inter
671 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
672
673 if (!has_second_ref(edge_mbmi)) // single pred (1/3)
674 pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]);
675 else // comp pred (1/3)
676 pred_context =
677 1 + 2 * (edge_mbmi->ref_frame[var_ref_idx] != cm->comp_var_ref[1]);
678 } else { // inter/inter
679 const int l_sg = !has_second_ref(left_mbmi);
680 const int a_sg = !has_second_ref(above_mbmi);
681 const MV_REFERENCE_FRAME vrfa =
682 a_sg ? above_mbmi->ref_frame[0] : above_mbmi->ref_frame[var_ref_idx];
683 const MV_REFERENCE_FRAME vrfl =
684 l_sg ? left_mbmi->ref_frame[0] : left_mbmi->ref_frame[var_ref_idx];
685
686 if (vrfa == vrfl && cm->comp_var_ref[1] == vrfa) {
687 pred_context = 0;
688 } else if (l_sg && a_sg) { // single/single
689 if ((vrfa == cm->comp_fixed_ref && vrfl == cm->comp_var_ref[0]) ||
690 (vrfl == cm->comp_fixed_ref && vrfa == cm->comp_var_ref[0]))
691 pred_context = 4;
692 else if (vrfa == vrfl)
693 pred_context = 3;
694 else
695 pred_context = 1;
696 } else if (l_sg || a_sg) { // single/comp
697 const MV_REFERENCE_FRAME vrfc = l_sg ? vrfa : vrfl;
698 const MV_REFERENCE_FRAME rfs = a_sg ? vrfa : vrfl;
699 if (vrfc == cm->comp_var_ref[1] && rfs != cm->comp_var_ref[1])
700 pred_context = 1;
701 else if (rfs == cm->comp_var_ref[1] && vrfc != cm->comp_var_ref[1])
702 pred_context = 2;
703 else
704 pred_context = 4;
705 } else if (vrfa == vrfl) { // comp/comp
706 pred_context = 4;
707 } else {
708 pred_context = 2;
709 }
710 }
711 } else if (above_in_image || left_in_image) { // one edge available
712 const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;
713
714 if (!is_inter_block(edge_mbmi)) {
715 pred_context = 2;
716 } else {
717 if (has_second_ref(edge_mbmi))
718 pred_context =
719 4 * (edge_mbmi->ref_frame[var_ref_idx] != cm->comp_var_ref[1]);
720 else
721 pred_context = 3 * (edge_mbmi->ref_frame[0] != cm->comp_var_ref[1]);
722 }
723 } else { // no edges available (2)
724 pred_context = 2;
725 }
726 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
727
728 return pred_context;
729}
730
731#endif // CONFIG_EXT_REFS
732
733#if CONFIG_EXT_REFS
734
735// For the bit to signal whether the single reference is a ALTREF_FRAME
736// or a BWDREF_FRAME.
737//
738// NOTE(zoeliu): The probability of ref_frame[0] is ALTREF/BWDREF.
Yaowu Xuf883b422016-08-30 14:01:10 -0700739int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 int pred_context;
741 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
742 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
743 const int has_above = xd->up_available;
744 const int has_left = xd->left_available;
745
746 // Note:
747 // The mode info data structure has a one element border above and to the
748 // left of the entries correpsonding to real macroblocks.
749 // The prediction flags in these dummy entries are initialised to 0.
750 if (has_above && has_left) { // both edges available
751 const int above_intra = !is_inter_block(above_mbmi);
752 const int left_intra = !is_inter_block(left_mbmi);
753
754 if (above_intra && left_intra) { // intra/intra
755 pred_context = 2;
756 } else if (above_intra || left_intra) { // intra/inter or inter/intra
757 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
758
Zoe Liu782c9642016-10-21 16:18:58 -0700759 if (!has_second_ref(edge_mbmi)) // single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700760 pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
Zoe Liu782c9642016-10-21 16:18:58 -0700761 else // comp
762 pred_context = 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 } else { // inter/inter
764 const int above_has_second = has_second_ref(above_mbmi);
765 const int left_has_second = has_second_ref(left_mbmi);
766
767 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700769
Zoe Liu782c9642016-10-21 16:18:58 -0700770 if (above_has_second && left_has_second) { // comp/comp
771 pred_context = 2;
772 } else if (above_has_second || left_has_second) { // single/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774
Zoe Liu782c9642016-10-21 16:18:58 -0700775 pred_context = (!CHECK_BACKWARD_REFS(rfs)) ? 4 : 1;
776 } else { // single/single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700777 pred_context = 2 * (!CHECK_BACKWARD_REFS(above0)) +
778 2 * (!CHECK_BACKWARD_REFS(left0));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779 }
780 }
781 } else if (has_above || has_left) { // one edge available
782 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
783 if (!is_inter_block(edge_mbmi)) { // intra
784 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -0700785 } else { // inter
786 if (!has_second_ref(edge_mbmi)) // single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700787 pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
Zoe Liu782c9642016-10-21 16:18:58 -0700788 else // comp
789 pred_context = 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 }
791 } else { // no edges available
792 pred_context = 2;
793 }
794
795 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
796 return pred_context;
797}
798
799// For the bit to signal whether the single reference is ALTREF_FRAME or
800// BWDREF_FRAME, knowing that it shall be either of these 2 choices.
801//
802// NOTE(zoeliu): The probability of ref_frame[0] is ALTREF_FRAME, conditioning
803// on it is either ALTREF_FRAME/BWDREF_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -0700804int av1_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 int pred_context;
806 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
807 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
808 const int has_above = xd->up_available;
809 const int has_left = xd->left_available;
810
811 // Note:
812 // The mode info data structure has a one element border above and to the
813 // left of the entries correpsonding to real macroblocks.
814 // The prediction flags in these dummy entries are initialised to 0.
815 if (has_above && has_left) { // both edges available
816 const int above_intra = !is_inter_block(above_mbmi);
817 const int left_intra = !is_inter_block(left_mbmi);
818
819 if (above_intra && left_intra) { // intra/intra
820 pred_context = 2;
821 } else if (above_intra || left_intra) { // intra/inter or inter/intra
822 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
Zoe Liu782c9642016-10-21 16:18:58 -0700823 if (!has_second_ref(edge_mbmi)) { // single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700824 if (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825 pred_context = 3;
826 else
827 pred_context = 4 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -0700828 } else { // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829 pred_context = 1 +
830 2 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME ||
831 edge_mbmi->ref_frame[1] == BWDREF_FRAME);
832 }
833 } else { // inter/inter
834 const int above_has_second = has_second_ref(above_mbmi);
835 const int left_has_second = has_second_ref(left_mbmi);
836 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
837 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
838 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
839 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
840
Zoe Liu782c9642016-10-21 16:18:58 -0700841 if (above_has_second && left_has_second) { // comp/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842 if (above0 == left0 && above1 == left1)
843 pred_context =
844 3 * (above0 == BWDREF_FRAME || above1 == BWDREF_FRAME ||
845 left0 == BWDREF_FRAME || left1 == BWDREF_FRAME);
846 else
847 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -0700848 } else if (above_has_second || left_has_second) { // single/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
850 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
851 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
852
853 if (rfs == BWDREF_FRAME)
854 pred_context = 3 + (crf1 == BWDREF_FRAME || crf2 == BWDREF_FRAME);
855 else if (rfs == ALTREF_FRAME)
856 pred_context = (crf1 == BWDREF_FRAME || crf2 == BWDREF_FRAME);
857 else
858 pred_context = 1 + 2 * (crf1 == BWDREF_FRAME || crf2 == BWDREF_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -0700859 } else { // single/single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700860 if (!CHECK_BACKWARD_REFS(above0) && !CHECK_BACKWARD_REFS(left0)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861 pred_context = 2 + (above0 == left0);
Zoe Liu6cfaff92016-10-18 17:12:11 -0700862 } else if (!CHECK_BACKWARD_REFS(above0) ||
863 !CHECK_BACKWARD_REFS(left0)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 const MV_REFERENCE_FRAME edge0 =
Zoe Liu6cfaff92016-10-18 17:12:11 -0700865 !CHECK_BACKWARD_REFS(above0) ? left0 : above0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866 pred_context = 4 * (edge0 == BWDREF_FRAME);
867 } else {
868 pred_context =
869 2 * (above0 == BWDREF_FRAME) + 2 * (left0 == BWDREF_FRAME);
870 }
871 }
872 }
873 } else if (has_above || has_left) { // one edge available
874 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
875
876 if (!is_inter_block(edge_mbmi) ||
Zoe Liu6cfaff92016-10-18 17:12:11 -0700877 (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 !has_second_ref(edge_mbmi)))
879 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -0700880 else if (!has_second_ref(edge_mbmi)) // single
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881 pred_context = 4 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -0700882 else // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883 pred_context = 3 * (edge_mbmi->ref_frame[0] == BWDREF_FRAME ||
884 edge_mbmi->ref_frame[1] == BWDREF_FRAME);
885 } else { // no edges available (2)
886 pred_context = 2;
887 }
888
889 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
890 return pred_context;
891}
892
893// For the bit to signal whether the single reference is LAST3/GOLDEN or
894// LAST2/LAST, knowing that it shall be either of these 2 choices.
895//
896// NOTE(zoeliu): The probability of ref_frame[0] is LAST3/GOLDEN, conditioning
897// on it is either LAST3/GOLDEN/LAST2/LAST.
Yaowu Xuf883b422016-08-30 14:01:10 -0700898int av1_get_pred_context_single_ref_p3(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 int pred_context;
900 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
901 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
902 const int has_above = xd->up_available;
903 const int has_left = xd->left_available;
904
905 // Note:
906 // The mode info data structure has a one element border above and to the
907 // left of the entries correpsonding to real macroblocks.
908 // The prediction flags in these dummy entries are initialised to 0.
909 if (has_above && has_left) { // both edges available
910 const int above_intra = !is_inter_block(above_mbmi);
911 const int left_intra = !is_inter_block(left_mbmi);
912
913 if (above_intra && left_intra) { // intra/intra
914 pred_context = 2;
915 } else if (above_intra || left_intra) { // intra/inter or inter/intra
916 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
Zoe Liu782c9642016-10-21 16:18:58 -0700917 if (!has_second_ref(edge_mbmi)) { // single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700918 if (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 pred_context = 3;
920 else
921 pred_context = 4 * CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]);
Zoe Liu782c9642016-10-21 16:18:58 -0700922 } else { // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923 pred_context = 1 +
924 2 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) ||
925 CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[1]));
926 }
927 } else { // inter/inter
928 const int above_has_second = has_second_ref(above_mbmi);
929 const int left_has_second = has_second_ref(left_mbmi);
930 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
931 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
932 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
933 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
934
Zoe Liu782c9642016-10-21 16:18:58 -0700935 if (above_has_second && left_has_second) { // comp/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 if (above0 == left0 && above1 == left1)
937 pred_context =
938 3 * (CHECK_LAST_OR_LAST2(above0) || CHECK_LAST_OR_LAST2(above1) ||
939 CHECK_LAST_OR_LAST2(left0) || CHECK_LAST_OR_LAST2(left1));
940 else
941 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -0700942 } else if (above_has_second || left_has_second) { // single/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700943 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
944 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
945 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
946
947 if (CHECK_LAST_OR_LAST2(rfs))
948 pred_context =
949 3 + (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
950 else if (CHECK_GOLDEN_OR_LAST3(rfs))
951 pred_context =
952 (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
953 else
954 pred_context =
955 1 + 2 * (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
Zoe Liu782c9642016-10-21 16:18:58 -0700956 } else { // single/single
Zoe Liu6cfaff92016-10-18 17:12:11 -0700957 if (CHECK_BACKWARD_REFS(above0) && CHECK_BACKWARD_REFS(left0)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700958 pred_context = 2 + (above0 == left0);
Zoe Liu6cfaff92016-10-18 17:12:11 -0700959 } else if (CHECK_BACKWARD_REFS(above0) || CHECK_BACKWARD_REFS(left0)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 const MV_REFERENCE_FRAME edge0 =
Zoe Liu6cfaff92016-10-18 17:12:11 -0700961 CHECK_BACKWARD_REFS(above0) ? left0 : above0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 pred_context = 4 * CHECK_LAST_OR_LAST2(edge0);
963 } else {
964 pred_context =
965 2 * CHECK_LAST_OR_LAST2(above0) + 2 * CHECK_LAST_OR_LAST2(left0);
966 }
967 }
968 }
969 } else if (has_above || has_left) { // one edge available
970 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
971
972 if (!is_inter_block(edge_mbmi) ||
Zoe Liu6cfaff92016-10-18 17:12:11 -0700973 (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700974 !has_second_ref(edge_mbmi)))
975 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -0700976 else if (!has_second_ref(edge_mbmi)) // single
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977 pred_context = 4 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]));
Zoe Liu782c9642016-10-21 16:18:58 -0700978 else // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -0700979 pred_context = 3 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) ||
980 CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[1]));
981 } else { // no edges available (2)
982 pred_context = 2;
983 }
984
985 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
986 return pred_context;
987}
988
989// For the bit to signal whether the single reference is LAST2_FRAME or
990// LAST_FRAME, knowing that it shall be either of these 2 choices.
991//
992// NOTE(zoeliu): The probability of ref_frame[0] is LAST2_FRAME, conditioning
993// on it is either LAST2_FRAME/LAST_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -0700994int av1_get_pred_context_single_ref_p4(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 int pred_context;
996 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
997 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
998 const int has_above = xd->up_available;
999 const int has_left = xd->left_available;
1000
1001 // Note:
1002 // The mode info data structure has a one element border above and to the
1003 // left of the entries correpsonding to real macroblocks.
1004 // The prediction flags in these dummy entries are initialised to 0.
1005 if (has_above && has_left) { // both edges available
1006 const int above_intra = !is_inter_block(above_mbmi);
1007 const int left_intra = !is_inter_block(left_mbmi);
1008
1009 if (above_intra && left_intra) { // intra/intra
1010 pred_context = 2;
1011 } else if (above_intra || left_intra) { // intra/inter or inter/intra
1012 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
Zoe Liu782c9642016-10-21 16:18:58 -07001013 if (!has_second_ref(edge_mbmi)) { // single
Yaowu Xuc27fc142016-08-22 16:08:15 -07001014 if (!CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]))
1015 pred_context = 3;
1016 else
1017 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -07001018 } else { // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001019 pred_context = 1 +
1020 2 * (edge_mbmi->ref_frame[0] == LAST_FRAME ||
1021 edge_mbmi->ref_frame[1] == LAST_FRAME);
1022 }
1023 } else { // inter/inter
1024 const int above_has_second = has_second_ref(above_mbmi);
1025 const int left_has_second = has_second_ref(left_mbmi);
1026 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
1027 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
1028 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
1029 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
1030
Zoe Liu782c9642016-10-21 16:18:58 -07001031 if (above_has_second && left_has_second) { // comp/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032 if (above0 == left0 && above1 == left1)
1033 pred_context = 3 * (above0 == LAST_FRAME || above1 == LAST_FRAME ||
1034 left0 == LAST_FRAME || left1 == LAST_FRAME);
1035 else
1036 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -07001037 } else if (above_has_second || left_has_second) { // single/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
1039 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
1040 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
1041
1042 if (rfs == LAST_FRAME)
1043 pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
1044 else if (rfs == LAST2_FRAME)
1045 pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
1046 else
1047 pred_context = 1 + 2 * (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -07001048 } else { // single/single
Yaowu Xuc27fc142016-08-22 16:08:15 -07001049 if (!CHECK_LAST_OR_LAST2(above0) && !CHECK_LAST_OR_LAST2(left0)) {
1050 pred_context = 2 + (above0 == left0);
1051 } else if (!CHECK_LAST_OR_LAST2(above0) ||
1052 !CHECK_LAST_OR_LAST2(left0)) {
1053 const MV_REFERENCE_FRAME edge0 =
1054 !CHECK_LAST_OR_LAST2(above0) ? left0 : above0;
1055 pred_context = 4 * (edge0 == LAST_FRAME);
1056 } else {
1057 pred_context = 2 * (above0 == LAST_FRAME) + 2 * (left0 == LAST_FRAME);
1058 }
1059 }
1060 }
1061 } else if (has_above || has_left) { // one edge available
1062 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
1063
1064 if (!is_inter_block(edge_mbmi) ||
1065 (!CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) &&
1066 !has_second_ref(edge_mbmi)))
1067 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -07001068 else if (!has_second_ref(edge_mbmi)) // single
Yaowu Xuc27fc142016-08-22 16:08:15 -07001069 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -07001070 else // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001071 pred_context = 3 * (edge_mbmi->ref_frame[0] == LAST_FRAME ||
1072 edge_mbmi->ref_frame[1] == LAST_FRAME);
1073 } else { // no edges available (2)
1074 pred_context = 2;
1075 }
1076
1077 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
1078 return pred_context;
1079}
1080
1081// For the bit to signal whether the single reference is GOLDEN_FRAME or
1082// LAST3_FRAME, knowing that it shall be either of these 2 choices.
1083//
1084// NOTE(zoeliu): The probability of ref_frame[0] is GOLDEN_FRAME, conditioning
1085// on it is either GOLDEN_FRAME/LAST3_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -07001086int av1_get_pred_context_single_ref_p5(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001087 int pred_context;
1088 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
1089 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
1090 const int has_above = xd->up_available;
1091 const int has_left = xd->left_available;
1092
1093 // Note:
1094 // The mode info data structure has a one element border above and to the
1095 // left of the entries correpsonding to real macroblocks.
1096 // The prediction flags in these dummy entries are initialised to 0.
1097 if (has_above && has_left) { // both edges available
1098 const int above_intra = !is_inter_block(above_mbmi);
1099 const int left_intra = !is_inter_block(left_mbmi);
1100
1101 if (above_intra && left_intra) { // intra/intra
1102 pred_context = 2;
1103 } else if (above_intra || left_intra) { // intra/inter or inter/intra
1104 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
Zoe Liu782c9642016-10-21 16:18:58 -07001105 if (!has_second_ref(edge_mbmi)) { // single
Yaowu Xuc27fc142016-08-22 16:08:15 -07001106 if (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]))
1107 pred_context = 3;
1108 else
1109 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST3_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -07001110 } else { // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 pred_context = 1 +
1112 2 * (edge_mbmi->ref_frame[0] == LAST3_FRAME ||
1113 edge_mbmi->ref_frame[1] == LAST3_FRAME);
1114 }
1115 } else { // inter/inter
1116 const int above_has_second = has_second_ref(above_mbmi);
1117 const int left_has_second = has_second_ref(left_mbmi);
1118 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
1119 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
1120 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
1121 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
1122
Zoe Liu782c9642016-10-21 16:18:58 -07001123 if (above_has_second && left_has_second) { // comp/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001124 if (above0 == left0 && above1 == left1)
1125 pred_context = 3 * (above0 == LAST3_FRAME || above1 == LAST3_FRAME ||
1126 left0 == LAST3_FRAME || left1 == LAST3_FRAME);
1127 else
1128 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -07001129 } else if (above_has_second || left_has_second) { // single/comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001130 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
1131 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
1132 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
1133
1134 if (rfs == LAST3_FRAME)
1135 pred_context = 3 + (crf1 == LAST3_FRAME || crf2 == LAST3_FRAME);
1136 else if (rfs == GOLDEN_FRAME)
1137 pred_context = (crf1 == LAST3_FRAME || crf2 == LAST3_FRAME);
1138 else
1139 pred_context = 1 + 2 * (crf1 == LAST3_FRAME || crf2 == LAST3_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -07001140 } else { // single/single
Yaowu Xuc27fc142016-08-22 16:08:15 -07001141 if (!CHECK_GOLDEN_OR_LAST3(above0) && !CHECK_GOLDEN_OR_LAST3(left0)) {
1142 pred_context = 2 + (above0 == left0);
1143 } else if (!CHECK_GOLDEN_OR_LAST3(above0) ||
1144 !CHECK_GOLDEN_OR_LAST3(left0)) {
1145 const MV_REFERENCE_FRAME edge0 =
1146 !CHECK_GOLDEN_OR_LAST3(above0) ? left0 : above0;
1147 pred_context = 4 * (edge0 == LAST3_FRAME);
1148 } else {
1149 pred_context =
1150 2 * (above0 == LAST3_FRAME) + 2 * (left0 == LAST3_FRAME);
1151 }
1152 }
1153 }
1154 } else if (has_above || has_left) { // one edge available
1155 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
1156
1157 if (!is_inter_block(edge_mbmi) ||
1158 (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]) &&
1159 !has_second_ref(edge_mbmi)))
1160 pred_context = 2;
Zoe Liu782c9642016-10-21 16:18:58 -07001161 else if (!has_second_ref(edge_mbmi)) // single
Yaowu Xuc27fc142016-08-22 16:08:15 -07001162 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST3_FRAME);
Zoe Liu782c9642016-10-21 16:18:58 -07001163 else // comp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164 pred_context = 3 * (edge_mbmi->ref_frame[0] == LAST3_FRAME ||
1165 edge_mbmi->ref_frame[1] == LAST3_FRAME);
1166 } else { // no edges available (2)
1167 pred_context = 2;
1168 }
1169
1170 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
1171 return pred_context;
1172}
1173
1174#else // CONFIG_EXT_REFS
1175
Yaowu Xuf883b422016-08-30 14:01:10 -07001176int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177 int pred_context;
1178 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
1179 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
1180 const int has_above = xd->up_available;
1181 const int has_left = xd->left_available;
1182 // Note:
1183 // The mode info data structure has a one element border above and to the
1184 // left of the entries corresponding to real macroblocks.
1185 // The prediction flags in these dummy entries are initialized to 0.
1186 if (has_above && has_left) { // both edges available
1187 const int above_intra = !is_inter_block(above_mbmi);
1188 const int left_intra = !is_inter_block(left_mbmi);
1189
1190 if (above_intra && left_intra) { // intra/intra
1191 pred_context = 2;
1192 } else if (above_intra || left_intra) { // intra/inter or inter/intra
1193 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
1194 if (!has_second_ref(edge_mbmi))
1195 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
1196 else
1197 pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME ||
1198 edge_mbmi->ref_frame[1] == LAST_FRAME);
1199 } else { // inter/inter
1200 const int above_has_second = has_second_ref(above_mbmi);
1201 const int left_has_second = has_second_ref(left_mbmi);
1202 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
1203 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
1204 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
1205 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
1206
1207 if (above_has_second && left_has_second) {
1208 pred_context = 1 + (above0 == LAST_FRAME || above1 == LAST_FRAME ||
1209 left0 == LAST_FRAME || left1 == LAST_FRAME);
1210 } else if (above_has_second || left_has_second) {
1211 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
1212 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
1213 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
1214
1215 if (rfs == LAST_FRAME)
1216 pred_context = 3 + (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
1217 else
1218 pred_context = (crf1 == LAST_FRAME || crf2 == LAST_FRAME);
1219 } else {
1220 pred_context = 2 * (above0 == LAST_FRAME) + 2 * (left0 == LAST_FRAME);
1221 }
1222 }
1223 } else if (has_above || has_left) { // one edge available
1224 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
1225 if (!is_inter_block(edge_mbmi)) { // intra
1226 pred_context = 2;
1227 } else { // inter
1228 if (!has_second_ref(edge_mbmi))
1229 pred_context = 4 * (edge_mbmi->ref_frame[0] == LAST_FRAME);
1230 else
1231 pred_context = 1 + (edge_mbmi->ref_frame[0] == LAST_FRAME ||
1232 edge_mbmi->ref_frame[1] == LAST_FRAME);
1233 }
1234 } else { // no edges available
1235 pred_context = 2;
1236 }
1237
1238 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
1239 return pred_context;
1240}
1241
Yaowu Xuf883b422016-08-30 14:01:10 -07001242int av1_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001243 int pred_context;
1244 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
1245 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
1246 const int has_above = xd->up_available;
1247 const int has_left = xd->left_available;
1248
1249 // Note:
1250 // The mode info data structure has a one element border above and to the
1251 // left of the entries corresponding to real macroblocks.
1252 // The prediction flags in these dummy entries are initialized to 0.
1253 if (has_above && has_left) { // both edges available
1254 const int above_intra = !is_inter_block(above_mbmi);
1255 const int left_intra = !is_inter_block(left_mbmi);
1256
1257 if (above_intra && left_intra) { // intra/intra
1258 pred_context = 2;
1259 } else if (above_intra || left_intra) { // intra/inter or inter/intra
1260 const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
1261 if (!has_second_ref(edge_mbmi)) {
1262 if (edge_mbmi->ref_frame[0] == LAST_FRAME)
1263 pred_context = 3;
1264 else
1265 pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME);
1266 } else {
1267 pred_context = 1 +
1268 2 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME ||
1269 edge_mbmi->ref_frame[1] == GOLDEN_FRAME);
1270 }
1271 } else { // inter/inter
1272 const int above_has_second = has_second_ref(above_mbmi);
1273 const int left_has_second = has_second_ref(left_mbmi);
1274 const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
1275 const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
1276 const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
1277 const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
1278
1279 if (above_has_second && left_has_second) {
1280 if (above0 == left0 && above1 == left1)
1281 pred_context =
1282 3 * (above0 == GOLDEN_FRAME || above1 == GOLDEN_FRAME ||
1283 left0 == GOLDEN_FRAME || left1 == GOLDEN_FRAME);
1284 else
1285 pred_context = 2;
1286 } else if (above_has_second || left_has_second) {
1287 const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
1288 const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
1289 const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;
1290
1291 if (rfs == GOLDEN_FRAME)
1292 pred_context = 3 + (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
1293 else if (rfs != GOLDEN_FRAME && rfs != LAST_FRAME)
1294 pred_context = crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME;
1295 else
1296 pred_context = 1 + 2 * (crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME);
1297 } else {
1298 if (above0 == LAST_FRAME && left0 == LAST_FRAME) {
1299 pred_context = 3;
1300 } else if (above0 == LAST_FRAME || left0 == LAST_FRAME) {
1301 const MV_REFERENCE_FRAME edge0 =
1302 (above0 == LAST_FRAME) ? left0 : above0;
1303 pred_context = 4 * (edge0 == GOLDEN_FRAME);
1304 } else {
1305 pred_context =
1306 2 * (above0 == GOLDEN_FRAME) + 2 * (left0 == GOLDEN_FRAME);
1307 }
1308 }
1309 }
1310 } else if (has_above || has_left) { // one edge available
1311 const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
1312
1313 if (!is_inter_block(edge_mbmi) ||
1314 (edge_mbmi->ref_frame[0] == LAST_FRAME && !has_second_ref(edge_mbmi)))
1315 pred_context = 2;
1316 else if (!has_second_ref(edge_mbmi))
1317 pred_context = 4 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME);
1318 else
1319 pred_context = 3 * (edge_mbmi->ref_frame[0] == GOLDEN_FRAME ||
1320 edge_mbmi->ref_frame[1] == GOLDEN_FRAME);
1321 } else { // no edges available (2)
1322 pred_context = 2;
1323 }
1324 assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
1325 return pred_context;
1326}
1327
1328#endif // CONFIG_EXT_REFS