pred_common.c 49.3 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11
 */

12 13 14
#include "av1/common/common.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
hui su's avatar
hui su committed
15 16 17
#if CONFIG_EXT_INTRA
#include "av1/common/reconintra.h"
#endif  // CONFIG_EXT_INTRA
18
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
19 20

// Returns a context number for the given MB prediction signal
21
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
22 23 24
static InterpFilter get_ref_filter_type(const MODE_INFO *mi,
                                        const MACROBLOCKD *xd, int dir,
                                        MV_REFERENCE_FRAME ref_frame) {
25 26
  const MB_MODE_INFO *ref_mbmi = &mi->mbmi;
  int use_subpel[2] = {
clang-format's avatar
clang-format committed
27 28
    has_subpel_mv_component(mi, xd, dir),
    has_subpel_mv_component(mi, xd, dir + 2),
29 30
  };

31 32 33 34
  return (((ref_mbmi->ref_frame[0] == ref_frame && use_subpel[0]) ||
           (ref_mbmi->ref_frame[1] == ref_frame && use_subpel[1]))
              ? av1_extract_interp_filter(ref_mbmi->interp_filters, dir & 0x01)
              : SWITCHABLE_FILTERS);
35 36
}

Yaowu Xu's avatar
Yaowu Xu committed
37
int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd, int dir) {
38
  const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
39 40
  const int ctx_offset =
      (mbmi->ref_frame[1] > INTRA_FRAME) * INTER_FILTER_COMP_OFFSET;
clang-format's avatar
clang-format committed
41 42
  MV_REFERENCE_FRAME ref_frame =
      (dir < 2) ? mbmi->ref_frame[0] : mbmi->ref_frame[1];
43 44 45 46
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries corresponding to real macroblocks.
  // 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 49 50
  int left_type = SWITCHABLE_FILTERS;
  int above_type = SWITCHABLE_FILTERS;

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)
clang-format's avatar
clang-format committed
55 56
    above_type =
        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 61
  } else if (left_type == SWITCHABLE_FILTERS) {
    assert(above_type != SWITCHABLE_FILTERS);
62
    filter_type_ctx += above_type;
63 64
  } else if (above_type == SWITCHABLE_FILTERS) {
    assert(left_type != SWITCHABLE_FILTERS);
65
    filter_type_ctx += left_type;
66
  } else {
67
    filter_type_ctx += SWITCHABLE_FILTERS;
68
  }
69 70

  return filter_type_ctx;
71 72
}
#else
Yaowu Xu's avatar
Yaowu Xu committed
73
int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
Jingning Han's avatar
Jingning Han committed
74 75
  // Note:
  // The mode info data structure has a one element border above and to the
hui su's avatar
hui su committed
76 77
  // left of the entries corresponding to real macroblocks.
  // The prediction flags in these dummy entries are initialized to 0.
Jingning Han's avatar
Jingning Han committed
78
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
79 80 81 82
  const int left_type =
      xd->left_available && is_inter_block(left_mbmi)
          ? av1_extract_interp_filter(left_mbmi->interp_filters, 0)
          : SWITCHABLE_FILTERS;
Jingning Han's avatar
Jingning Han committed
83
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
84 85 86 87
  const int above_type =
      xd->up_available && is_inter_block(above_mbmi)
          ? av1_extract_interp_filter(above_mbmi->interp_filters, 0)
          : SWITCHABLE_FILTERS;
Jingning Han's avatar
Jingning Han committed
88

89
  if (left_type == above_type) {
Jingning Han's avatar
Jingning Han committed
90
    return left_type;
91 92
  } else if (left_type == SWITCHABLE_FILTERS) {
    assert(above_type != SWITCHABLE_FILTERS);
Jingning Han's avatar
Jingning Han committed
93
    return above_type;
94 95
  } else if (above_type == SWITCHABLE_FILTERS) {
    assert(left_type != SWITCHABLE_FILTERS);
Jingning Han's avatar
Jingning Han committed
96
    return left_type;
97
  } else {
Jingning Han's avatar
Jingning Han committed
98
    return SWITCHABLE_FILTERS;
99
  }
Jingning Han's avatar
Jingning Han committed
100
}
101
#endif
Jingning Han's avatar
Jingning Han committed
102

Urvang Joshi's avatar
Urvang Joshi committed
103
#if CONFIG_PALETTE_DELTA_ENCODING
104 105 106 107 108 109 110
int av1_get_palette_cache(const MACROBLOCKD *const xd, int plane,
                          uint16_t *cache) {
  const int row = -xd->mb_to_top_edge >> 3;
  // Do not refer to above SB row when on SB boundary.
  const MODE_INFO *const above_mi =
      (row % (1 << MIN_SB_SIZE_LOG2)) ? xd->above_mi : NULL;
  const MODE_INFO *const left_mi = xd->left_mi;
111 112 113 114 115 116 117 118 119 120
  int above_n = 0, left_n = 0;
  if (above_mi)
    above_n = above_mi->mbmi.palette_mode_info.palette_size[plane != 0];
  if (left_mi)
    left_n = left_mi->mbmi.palette_mode_info.palette_size[plane != 0];
  if (above_n == 0 && left_n == 0) return 0;
  int above_idx = plane * PALETTE_MAX_SIZE;
  int left_idx = plane * PALETTE_MAX_SIZE;
  int n = 0;
  const uint16_t *above_colors =
121 122 123
      above_mi ? above_mi->mbmi.palette_mode_info.palette_colors : NULL;
  const uint16_t *left_colors =
      left_mi ? left_mi->mbmi.palette_mode_info.palette_colors : NULL;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  // Merge the sorted lists of base colors from above and left to get
  // combined sorted color cache.
  while (above_n > 0 && left_n > 0) {
    uint16_t v_above = above_colors[above_idx];
    uint16_t v_left = left_colors[left_idx];
    if (v_left < v_above) {
      if (n == 0 || v_left != cache[n - 1]) cache[n++] = v_left;
      ++left_idx, --left_n;
    } else {
      if (n == 0 || v_above != cache[n - 1]) cache[n++] = v_above;
      ++above_idx, --above_n;
      if (v_left == v_above) ++left_idx, --left_n;
    }
  }
  while (above_n-- > 0) {
    uint16_t val = above_colors[above_idx++];
    if (n == 0 || val != cache[n - 1]) cache[n++] = val;
  }
  while (left_n-- > 0) {
    uint16_t val = left_colors[left_idx++];
    if (n == 0 || val != cache[n - 1]) cache[n++] = val;
  }
  assert(n <= 2 * PALETTE_MAX_SIZE);
  return n;
}
Urvang Joshi's avatar
Urvang Joshi committed
149
#endif  // CONFIG_PALETTE_DELTA_ENCODING
150

Jingning Han's avatar
Jingning Han committed
151 152 153 154 155 156 157
// The mode info data structure has a one element border above and to the
// left of the entries corresponding to real macroblocks.
// The prediction flags in these dummy entries are initialized to 0.
// 0 - inter/inter, inter/--, --/inter, --/--
// 1 - intra/inter, inter/intra
// 2 - intra/--, --/intra
// 3 - intra/intra
Yaowu Xu's avatar
Yaowu Xu committed
158
int av1_get_intra_inter_context(const MACROBLOCKD *xd) {
Jingning Han's avatar
Jingning Han committed
159 160 161 162 163 164 165 166
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int has_above = xd->up_available;
  const int has_left = xd->left_available;

  if (has_above && has_left) {  // both edges available
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);
clang-format's avatar
clang-format committed
167
    return left_intra && above_intra ? 3 : left_intra || above_intra;
Jingning Han's avatar
Jingning Han committed
168 169 170 171 172 173 174
  } else if (has_above || has_left) {  // one edge available
    return 2 * !is_inter_block(has_above ? above_mbmi : left_mbmi);
  } else {
    return 0;
  }
}

175
#if CONFIG_COMPOUND_SINGLEREF
176 177 178 179 180 181 182 183 184
// The compound/single mode info data structure has one element border above and
// to the left of the entries corresponding to real macroblocks.
// The prediction flags in these dummy entries are initialized to 0.
int av1_get_inter_mode_context(const MACROBLOCKD *xd) {
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int has_above = xd->up_available;
  const int has_left = xd->left_available;

185 186 187 188 189 190 191 192 193 194 195 196 197
  if (has_above && has_left) {  // both edges available
    const int above_inter_comp_mode =
        is_inter_anyref_comp_mode(above_mbmi->mode);
    const int left_inter_comp_mode = is_inter_anyref_comp_mode(left_mbmi->mode);
    if (above_inter_comp_mode && left_inter_comp_mode)
      return 0;
    else if (above_inter_comp_mode || left_inter_comp_mode)
      return 1;
    else if (!is_inter_block(above_mbmi) && !is_inter_block(left_mbmi))
      return 2;
    else
      return 3;
  } else if (has_above || has_left) {  // one edge available
198
    const MB_MODE_INFO *const edge_mbmi = has_above ? above_mbmi : left_mbmi;
199 200 201 202 203 204 205 206
    if (is_inter_anyref_comp_mode(edge_mbmi->mode))
      return 1;
    else if (!is_inter_block(edge_mbmi))
      return 2;
    else
      return 3;
  } else {  // no edge available
    return 2;
207 208
  }
}
209
#endif  // CONFIG_COMPOUND_SINGLEREF
210

Zoe Liu's avatar
Zoe Liu committed
211 212 213
#define CHECK_BACKWARD_REFS(ref_frame) \
  (((ref_frame) >= BWDREF_FRAME) && ((ref_frame) <= ALTREF_FRAME))
#define IS_BACKWARD_REF_FRAME(ref_frame) CHECK_BACKWARD_REFS(ref_frame)
214

215 216 217
#define CHECK_GOLDEN_OR_LAST3(ref_frame) \
  (((ref_frame) == GOLDEN_FRAME) || ((ref_frame) == LAST3_FRAME))

Yaowu Xu's avatar
Yaowu Xu committed
218 219
int av1_get_reference_mode_context(const AV1_COMMON *cm,
                                   const MACROBLOCKD *xd) {
220 221 222 223 224
  int ctx;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int has_above = xd->up_available;
  const int has_left = xd->left_available;
225 226 227

  (void)cm;

228 229 230 231 232 233 234
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries corresponding to real macroblocks.
  // The prediction flags in these dummy entries are initialized to 0.
  if (has_above && has_left) {  // both edges available
    if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi))
      // neither edge uses comp pred (0/1)
Zoe Liu's avatar
Zoe Liu committed
235 236
      ctx = IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ^
            IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]);
Jingning Han's avatar
Jingning Han committed
237 238
    else if (!has_second_ref(above_mbmi))
      // one of two edges uses comp pred (2/3)
Zoe Liu's avatar
Zoe Liu committed
239
      ctx = 2 + (IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ||
Jingning Han's avatar
Jingning Han committed
240 241 242
                 !is_inter_block(above_mbmi));
    else if (!has_second_ref(left_mbmi))
      // one of two edges uses comp pred (2/3)
Zoe Liu's avatar
Zoe Liu committed
243
      ctx = 2 + (IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]) ||
Jingning Han's avatar
Jingning Han committed
244 245 246 247 248 249 250 251
                 !is_inter_block(left_mbmi));
    else  // both edges use comp pred (4)
      ctx = 4;
  } else if (has_above || has_left) {  // one edge available
    const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;

    if (!has_second_ref(edge_mbmi))
      // edge does not use comp pred (0/1)
Zoe Liu's avatar
Zoe Liu committed
252
      ctx = IS_BACKWARD_REF_FRAME(edge_mbmi->ref_frame[0]);
Jingning Han's avatar
Jingning Han committed
253 254 255 256 257 258 259 260 261 262
    else
      // edge uses comp pred (3)
      ctx = 3;
  } else {  // no edges available (1)
    ctx = 1;
  }
  assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS);
  return ctx;
}

Zoe Liu's avatar
Zoe Liu committed
263
#if CONFIG_EXT_COMP_REFS
264 265 266
// TODO(zoeliu): To try on the design of 3 contexts, instead of 5:
//               COMP_REF_TYPE_CONTEXTS = 3
int av1_get_comp_reference_type_context(const MACROBLOCKD *xd) {
Zoe Liu's avatar
Zoe Liu committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

  if (above_in_image && left_in_image) {  // both edges available
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);

    if (above_intra && left_intra) {  // intra/intra
      pred_context = 2;
    } else if (above_intra || left_intra) {  // intra/inter
      const MB_MODE_INFO *inter_mbmi = above_intra ? left_mbmi : above_mbmi;

      if (!has_second_ref(inter_mbmi))  // single pred
        pred_context = 2;
      else  // comp pred
        pred_context = 1 + 2 * has_uni_comp_refs(inter_mbmi);
    } else {  // inter/inter
      const int a_sg = !has_second_ref(above_mbmi);
      const int l_sg = !has_second_ref(left_mbmi);
      const MV_REFERENCE_FRAME frfa = above_mbmi->ref_frame[0];
      const MV_REFERENCE_FRAME frfl = left_mbmi->ref_frame[0];

      if (a_sg && l_sg) {  // single/single
Zoe Liu's avatar
Zoe Liu committed
293 294 295
        pred_context =
            1 +
            2 * (!(IS_BACKWARD_REF_FRAME(frfa) ^ IS_BACKWARD_REF_FRAME(frfl)));
Zoe Liu's avatar
Zoe Liu committed
296 297 298 299 300 301 302
      } else if (l_sg || a_sg) {  // single/comp
        const int uni_rfc =
            a_sg ? has_uni_comp_refs(left_mbmi) : has_uni_comp_refs(above_mbmi);

        if (!uni_rfc)  // comp bidir
          pred_context = 1;
        else  // comp unidir
Zoe Liu's avatar
Zoe Liu committed
303 304
          pred_context = 3 + (!(IS_BACKWARD_REF_FRAME(frfa) ^
                                IS_BACKWARD_REF_FRAME(frfl)));
Zoe Liu's avatar
Zoe Liu committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
      } else {  // comp/comp
        const int a_uni_rfc = has_uni_comp_refs(above_mbmi);
        const int l_uni_rfc = has_uni_comp_refs(left_mbmi);

        if (!a_uni_rfc && !l_uni_rfc)  // bidir/bidir
          pred_context = 0;
        else if (!a_uni_rfc || !l_uni_rfc)  // unidir/bidir
          pred_context = 2;
        else  // unidir/unidir
          pred_context =
              3 + (!((frfa == BWDREF_FRAME) ^ (frfl == BWDREF_FRAME)));
      }
    }
  } else if (above_in_image || left_in_image) {  // one edge available
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;

    if (!is_inter_block(edge_mbmi)) {  // intra
      pred_context = 2;
    } else {                           // inter
      if (!has_second_ref(edge_mbmi))  // single pred
        pred_context = 2;
      else  // comp pred
        pred_context = 4 * has_uni_comp_refs(edge_mbmi);
    }
  } else {  // no edges available
    pred_context = 2;
  }

  assert(pred_context >= 0 && pred_context < COMP_REF_TYPE_CONTEXTS);
  return pred_context;
}

// Returns a context number for the given MB prediction signal
338 339 340
//
// Signal the uni-directional compound reference frame pair as either
// (BWDREF, ALTREF), or (LAST, LAST2) / (LAST, LAST3) / (LAST, GOLDEN),
Zoe Liu's avatar
Zoe Liu committed
341
// conditioning on the pair is known as uni-directional.
342 343 344 345
//
// 3 contexts: Voting is used to compare the count of forward references with
//             that of backward references from the spatial neighbors.
int av1_get_pred_context_uni_comp_ref_p(const MACROBLOCKD *xd) {
Zoe Liu's avatar
Zoe Liu committed
346 347 348 349 350 351
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

352 353 354 355
  // Count of forward references (L, L2, L3, or G)
  int frf_count = 0;
  // Count of backward references (B or A)
  int brf_count = 0;
Zoe Liu's avatar
Zoe Liu committed
356

357 358 359 360 361 362 363 364 365 366 367 368
  if (above_in_image && is_inter_block(above_mbmi)) {
    if (above_mbmi->ref_frame[0] <= GOLDEN_FRAME)
      ++frf_count;
    else
      ++brf_count;
    if (has_second_ref(above_mbmi)) {
      if (above_mbmi->ref_frame[1] <= GOLDEN_FRAME)
        ++frf_count;
      else
        ++brf_count;
    }
  }
Zoe Liu's avatar
Zoe Liu committed
369

370 371 372 373 374 375 376 377 378 379 380 381
  if (left_in_image && is_inter_block(left_mbmi)) {
    if (left_mbmi->ref_frame[0] <= GOLDEN_FRAME)
      ++frf_count;
    else
      ++brf_count;
    if (has_second_ref(left_mbmi)) {
      if (left_mbmi->ref_frame[1] <= GOLDEN_FRAME)
        ++frf_count;
      else
        ++brf_count;
    }
  }
Zoe Liu's avatar
Zoe Liu committed
382

383 384
  pred_context =
      (frf_count == brf_count) ? 1 : ((frf_count < brf_count) ? 0 : 2);
Zoe Liu's avatar
Zoe Liu committed
385

386 387 388
  assert(pred_context >= 0 && pred_context < UNI_COMP_REF_CONTEXTS);
  return pred_context;
}
Zoe Liu's avatar
Zoe Liu committed
389

390 391 392 393 394 395 396 397 398 399 400 401 402 403
// Returns a context number for the given MB prediction signal
//
// Signal the uni-directional compound reference frame pair as
// either (LAST, LAST2), or (LAST, LAST3) / (LAST, GOLDEN),
// conditioning on the pair is known as one of the above three.
//
// 3 contexts: Voting is used to compare the count of LAST2_FRAME with the
//             total count of LAST3/GOLDEN from the spatial neighbors.
int av1_get_pred_context_uni_comp_ref_p1(const MACROBLOCKD *xd) {
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;
Zoe Liu's avatar
Zoe Liu committed
404

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  // Count of LAST2
  int last2_count = 0;
  // Count of LAST3 or GOLDEN
  int last3_or_gld_count = 0;

  if (above_in_image && is_inter_block(above_mbmi)) {
    last2_count = (above_mbmi->ref_frame[0] == LAST2_FRAME) ? last2_count + 1
                                                            : last2_count;
    last3_or_gld_count = CHECK_GOLDEN_OR_LAST3(above_mbmi->ref_frame[0])
                             ? last3_or_gld_count + 1
                             : last3_or_gld_count;
    if (has_second_ref(above_mbmi)) {
      last2_count = (above_mbmi->ref_frame[1] == LAST2_FRAME) ? last2_count + 1
                                                              : last2_count;
      last3_or_gld_count = CHECK_GOLDEN_OR_LAST3(above_mbmi->ref_frame[1])
                               ? last3_or_gld_count + 1
                               : last3_or_gld_count;
Zoe Liu's avatar
Zoe Liu committed
422
    }
423
  }
Zoe Liu's avatar
Zoe Liu committed
424

425 426 427 428 429 430 431 432 433 434 435 436
  if (left_in_image && is_inter_block(left_mbmi)) {
    last2_count = (left_mbmi->ref_frame[0] == LAST2_FRAME) ? last2_count + 1
                                                           : last2_count;
    last3_or_gld_count = CHECK_GOLDEN_OR_LAST3(left_mbmi->ref_frame[0])
                             ? last3_or_gld_count + 1
                             : last3_or_gld_count;
    if (has_second_ref(left_mbmi)) {
      last2_count = (left_mbmi->ref_frame[1] == LAST2_FRAME) ? last2_count + 1
                                                             : last2_count;
      last3_or_gld_count = CHECK_GOLDEN_OR_LAST3(left_mbmi->ref_frame[1])
                               ? last3_or_gld_count + 1
                               : last3_or_gld_count;
Zoe Liu's avatar
Zoe Liu committed
437 438 439
    }
  }

440 441 442 443
  pred_context = (last2_count == last3_or_gld_count)
                     ? 1
                     : ((last2_count < last3_or_gld_count) ? 0 : 2);

Zoe Liu's avatar
Zoe Liu committed
444 445 446 447 448
  assert(pred_context >= 0 && pred_context < UNI_COMP_REF_CONTEXTS);
  return pred_context;
}

// Returns a context number for the given MB prediction signal
449
//
Zoe Liu's avatar
Zoe Liu committed
450
// Signal the uni-directional compound reference frame pair as
451 452 453 454 455 456
// either (LAST, LAST3) or (LAST, GOLDEN),
// conditioning on the pair is known as one of the above two.
//
// 3 contexts: Voting is used to compare the count of LAST3_FRAME with the
//             total count of GOLDEN_FRAME from the spatial neighbors.
int av1_get_pred_context_uni_comp_ref_p2(const MACROBLOCKD *xd) {
Zoe Liu's avatar
Zoe Liu committed
457 458 459 460 461 462
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
  // Count of LAST3
  int last3_count = 0;
  // Count of GOLDEN
  int gld_count = 0;

  if (above_in_image && is_inter_block(above_mbmi)) {
    last3_count = (above_mbmi->ref_frame[0] == LAST3_FRAME) ? last3_count + 1
                                                            : last3_count;
    gld_count =
        (above_mbmi->ref_frame[0] == GOLDEN_FRAME) ? gld_count + 1 : gld_count;
    if (has_second_ref(above_mbmi)) {
      last3_count = (above_mbmi->ref_frame[1] == LAST3_FRAME) ? last3_count + 1
                                                              : last3_count;
      gld_count = (above_mbmi->ref_frame[1] == GOLDEN_FRAME) ? gld_count + 1
                                                             : gld_count;
Zoe Liu's avatar
Zoe Liu committed
478
    }
479
  }
Zoe Liu's avatar
Zoe Liu committed
480

481 482 483 484 485 486 487 488 489 490
  if (left_in_image && is_inter_block(left_mbmi)) {
    last3_count = (left_mbmi->ref_frame[0] == LAST3_FRAME) ? last3_count + 1
                                                           : last3_count;
    gld_count =
        (left_mbmi->ref_frame[0] == GOLDEN_FRAME) ? gld_count + 1 : gld_count;
    if (has_second_ref(left_mbmi)) {
      last3_count = (left_mbmi->ref_frame[1] == LAST3_FRAME) ? last3_count + 1
                                                             : last3_count;
      gld_count =
          (left_mbmi->ref_frame[1] == GOLDEN_FRAME) ? gld_count + 1 : gld_count;
Zoe Liu's avatar
Zoe Liu committed
491 492 493
    }
  }

494 495 496
  pred_context =
      (last3_count == gld_count) ? 1 : ((last3_count < gld_count) ? 0 : 2);

Zoe Liu's avatar
Zoe Liu committed
497 498 499 500 501
  assert(pred_context >= 0 && pred_context < UNI_COMP_REF_CONTEXTS);
  return pred_context;
}
#endif  // CONFIG_EXT_COMP_REFS

502
// TODO(zoeliu): Future work will be conducted to optimize the context design
503
//               for the coding of the reference frames.
504 505 506 507

#define CHECK_LAST_OR_LAST2(ref_frame) \
  ((ref_frame == LAST_FRAME) || (ref_frame == LAST2_FRAME))

Jingning Han's avatar
Jingning Han committed
508
// Returns a context number for the given MB prediction signal
509 510
// Signal the first reference frame for a compound mode be either
// GOLDEN/LAST3, or LAST/LAST2.
511 512
//
// NOTE(zoeliu): The probability of ref_frame[0] is either
513
//               GOLDEN_FRAME or LAST3_FRAME.
Yaowu Xu's avatar
Yaowu Xu committed
514 515
int av1_get_pred_context_comp_ref_p(const AV1_COMMON *cm,
                                    const MACROBLOCKD *xd) {
516 517 518 519 520 521
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

522 523 524 525
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
526
  const int bwd_ref_sign_idx = 1;
527
  const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
528

Zoe Liu's avatar
Zoe Liu committed
529 530
  (void)cm;

531 532 533 534 535 536 537 538 539 540
  if (above_in_image && left_in_image) {  // both edges available
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);

    if (above_intra && left_intra) {  // intra/intra (2)
      pred_context = 2;
    } else if (above_intra || left_intra) {  // intra/inter
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;

      if (!has_second_ref(edge_mbmi))  // single pred (1/3)
clang-format's avatar
clang-format committed
541 542
        pred_context =
            1 + 2 * (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]));
543
      else  // comp pred (1/3)
clang-format's avatar
clang-format committed
544 545 546
        pred_context = 1 +
                       2 * (!CHECK_GOLDEN_OR_LAST3(
                               edge_mbmi->ref_frame[fwd_ref_sign_idx]));
547 548 549
    } else {  // inter/inter
      const int l_sg = !has_second_ref(left_mbmi);
      const int a_sg = !has_second_ref(above_mbmi);
clang-format's avatar
clang-format committed
550 551 552 553 554 555
      const MV_REFERENCE_FRAME frfa =
          a_sg ? above_mbmi->ref_frame[0]
               : above_mbmi->ref_frame[fwd_ref_sign_idx];
      const MV_REFERENCE_FRAME frfl =
          l_sg ? left_mbmi->ref_frame[0]
               : left_mbmi->ref_frame[fwd_ref_sign_idx];
556

557
      if (frfa == frfl && CHECK_GOLDEN_OR_LAST3(frfa)) {
558 559
        pred_context = 0;
      } else if (l_sg && a_sg) {  // single/single
Zoe Liu's avatar
Zoe Liu committed
560 561
        if ((CHECK_BACKWARD_REFS(frfa) && CHECK_LAST_OR_LAST2(frfl)) ||
            (CHECK_BACKWARD_REFS(frfl) && CHECK_LAST_OR_LAST2(frfa))) {
562
          pred_context = 4;
clang-format's avatar
clang-format committed
563
        } else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl)) {
564
          pred_context = 1;
565 566
        } else {
          pred_context = 3;
567 568
        }
      } else if (l_sg || a_sg) {  // single/comp
569 570
        const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
        const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
571

572
        if (CHECK_GOLDEN_OR_LAST3(frfc) && !CHECK_GOLDEN_OR_LAST3(rfs))
573
          pred_context = 1;
574
        else if (CHECK_GOLDEN_OR_LAST3(rfs) && !CHECK_GOLDEN_OR_LAST3(frfc))
575 576 577 578
          pred_context = 2;
        else
          pred_context = 4;
      } else {  // comp/comp
579
        if ((CHECK_LAST_OR_LAST2(frfa) && CHECK_LAST_OR_LAST2(frfl))) {
580 581 582 583 584 585 586 587 588 589 590 591 592
          pred_context = 4;
        } else {
          pred_context = 2;
        }
      }
    }
  } else if (above_in_image || left_in_image) {  // one edge available
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;

    if (!is_inter_block(edge_mbmi)) {
      pred_context = 2;
    } else {
      if (has_second_ref(edge_mbmi))
clang-format's avatar
clang-format committed
593 594
        pred_context =
            4 *
595
            (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[fwd_ref_sign_idx]));
596
      else
597
        pred_context = 3 * (!CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]));
598 599 600 601 602 603 604 605 606 607 608
    }
  } else {  // no edges available (2)
    pred_context = 2;
  }

  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);

  return pred_context;
}

// Returns a context number for the given MB prediction signal
609
// Signal the first reference frame for a compound mode be LAST,
610 611 612 613
// conditioning on that it is known either LAST/LAST2.
//
// NOTE(zoeliu): The probability of ref_frame[0] is LAST_FRAME,
// conditioning on it is either LAST_FRAME or LAST2_FRAME.
Yaowu Xu's avatar
Yaowu Xu committed
614 615
int av1_get_pred_context_comp_ref_p1(const AV1_COMMON *cm,
                                     const MACROBLOCKD *xd) {
616 617 618 619 620 621
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

622 623 624 625
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
626
  const int bwd_ref_sign_idx = 1;
627
  const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
628

Zoe Liu's avatar
Zoe Liu committed
629 630
  (void)cm;

631 632 633 634 635 636 637 638 639 640 641 642
  if (above_in_image && left_in_image) {  // both edges available
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);

    if (above_intra && left_intra) {  // intra/intra (2)
      pred_context = 2;
    } else if (above_intra || left_intra) {  // intra/inter
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;

      if (!has_second_ref(edge_mbmi))  // single pred (1/3)
        pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != LAST_FRAME);
      else  // comp pred (1/3)
clang-format's avatar
clang-format committed
643 644
        pred_context =
            1 + 2 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST_FRAME);
645 646 647
    } else {  // inter/inter
      const int l_sg = !has_second_ref(left_mbmi);
      const int a_sg = !has_second_ref(above_mbmi);
clang-format's avatar
clang-format committed
648 649 650 651 652 653
      const MV_REFERENCE_FRAME frfa =
          a_sg ? above_mbmi->ref_frame[0]
               : above_mbmi->ref_frame[fwd_ref_sign_idx];
      const MV_REFERENCE_FRAME frfl =
          l_sg ? left_mbmi->ref_frame[0]
               : left_mbmi->ref_frame[fwd_ref_sign_idx];
654

655
      if (frfa == frfl && frfa == LAST_FRAME)
656 657
        pred_context = 0;
      else if (l_sg && a_sg) {  // single/single
658
        if (frfa == LAST_FRAME || frfl == LAST_FRAME)
659
          pred_context = 1;
660 661 662
        else if (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl))
          pred_context = 2 + (frfa != frfl);
        else if (frfa == frfl ||
Zoe Liu's avatar
Zoe Liu committed
663
                 (CHECK_BACKWARD_REFS(frfa) && CHECK_BACKWARD_REFS(frfl)))
664 665 666 667
          pred_context = 3;
        else
          pred_context = 4;
      } else if (l_sg || a_sg) {  // single/comp
668 669
        const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
        const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
670

671
        if (frfc == LAST_FRAME && rfs != LAST_FRAME)
672
          pred_context = 1;
673
        else if (rfs == LAST_FRAME && frfc != LAST_FRAME)
674 675
          pred_context = 2;
        else
clang-format's avatar
clang-format committed
676 677
          pred_context =
              3 + (frfc == LAST2_FRAME || CHECK_GOLDEN_OR_LAST3(rfs));
678
      } else {  // comp/comp
679
        if (frfa == LAST_FRAME || frfl == LAST_FRAME)
680 681
          pred_context = 2;
        else
clang-format's avatar
clang-format committed
682 683
          pred_context =
              3 + (CHECK_GOLDEN_OR_LAST3(frfa) || CHECK_GOLDEN_OR_LAST3(frfl));
684 685 686 687 688 689 690 691 692
      }
    }
  } else if (above_in_image || left_in_image) {  // one edge available
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;

    if (!is_inter_block(edge_mbmi)) {
      pred_context = 2;
    } else {
      if (has_second_ref(edge_mbmi)) {
clang-format's avatar
clang-format committed
693 694
        pred_context =
            4 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != LAST_FRAME);
695 696 697 698
      } else {
        if (edge_mbmi->ref_frame[0] == LAST_FRAME)
          pred_context = 0;
        else
699
          pred_context = 2 + CHECK_GOLDEN_OR_LAST3(edge_mbmi->ref_frame[0]);
700 701 702 703 704 705 706 707 708 709 710 711
      }
    }
  } else {  // no edges available (2)
    pred_context = 2;
  }

  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);

  return pred_context;
}

// Returns a context number for the given MB prediction signal
712 713
// Signal the first reference frame for a compound mode be GOLDEN,
// conditioning on that it is known either GOLDEN or LAST3.
714 715
//
// NOTE(zoeliu): The probability of ref_frame[0] is GOLDEN_FRAME,
716
// conditioning on it is either GOLDEN or LAST3.
Yaowu Xu's avatar
Yaowu Xu committed
717 718
int av1_get_pred_context_comp_ref_p2(const AV1_COMMON *cm,
                                     const MACROBLOCKD *xd) {
719 720 721 722 723 724
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

725 726 727 728
  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
729
  const int bwd_ref_sign_idx = 1;
730
  const int fwd_ref_sign_idx = !bwd_ref_sign_idx;
731

Zoe Liu's avatar
Zoe Liu committed
732 733
  (void)cm;

734 735 736 737 738 739 740 741 742 743 744 745
  if (above_in_image && left_in_image) {  // both edges available
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);

    if (above_intra && left_intra) {  // intra/intra (2)
      pred_context = 2;
    } else if (above_intra || left_intra) {  // intra/inter
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;

      if (!has_second_ref(edge_mbmi))  // single pred (1/3)
        pred_context = 1 + 2 * (edge_mbmi->ref_frame[0] != GOLDEN_FRAME);
      else  // comp pred (1/3)
clang-format's avatar
clang-format committed
746 747
        pred_context =
            1 + 2 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != GOLDEN_FRAME);
748 749 750
    } else {  // inter/inter
      const int l_sg = !has_second_ref(left_mbmi);
      const int a_sg = !has_second_ref(above_mbmi);
clang-format's avatar
clang-format committed
751 752 753 754 755 756
      const MV_REFERENCE_FRAME frfa =
          a_sg ? above_mbmi->ref_frame[0]
               : above_mbmi->ref_frame[fwd_ref_sign_idx];
      const MV_REFERENCE_FRAME frfl =
          l_sg ? left_mbmi->ref_frame[0]
               : left_mbmi->ref_frame[fwd_ref_sign_idx];
757

758
      if (frfa == frfl && frfa == GOLDEN_FRAME)
759 760
        pred_context = 0;
      else if (l_sg && a_sg) {  // single/single
761
        if (frfa == GOLDEN_FRAME || frfl == GOLDEN_FRAME)
762
          pred_context = 1;
763 764 765
        else if (CHECK_LAST_OR_LAST2(frfa) || CHECK_LAST_OR_LAST2(frfl))
          pred_context = 2 + (frfa != frfl);
        else if (frfa == frfl ||
Zoe Liu's avatar
Zoe Liu committed
766
                 (CHECK_BACKWARD_REFS(frfa) && CHECK_BACKWARD_REFS(frfl)))
767 768 769 770
          pred_context = 3;
        else
          pred_context = 4;
      } else if (l_sg || a_sg) {  // single/comp
771 772
        const MV_REFERENCE_FRAME frfc = l_sg ? frfa : frfl;
        const MV_REFERENCE_FRAME rfs = a_sg ? frfa : frfl;
773

774
        if (frfc == GOLDEN_FRAME && rfs != GOLDEN_FRAME)
775
          pred_context = 1;
776
        else if (rfs == GOLDEN_FRAME && frfc != GOLDEN_FRAME)
777 778
          pred_context = 2;
        else
clang-format's avatar
clang-format committed
779
          pred_context = 3 + (frfc == LAST3_FRAME || CHECK_LAST_OR_LAST2(rfs));
780
      } else {  // comp/comp
781
        if (frfa == GOLDEN_FRAME || frfl == GOLDEN_FRAME)
782 783
          pred_context = 2;
        else
clang-format's avatar
clang-format committed
784 785
          pred_context =
              3 + (CHECK_LAST_OR_LAST2(frfa) || CHECK_LAST_OR_LAST2(frfl));
786 787 788 789 790 791 792 793 794
      }
    }
  } else if (above_in_image || left_in_image) {  // one edge available
    const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi;

    if (!is_inter_block(edge_mbmi)) {
      pred_context = 2;
    } else {
      if (has_second_ref(edge_mbmi)) {
clang-format's avatar
clang-format committed
795 796
        pred_context =
            4 * (edge_mbmi->ref_frame[fwd_ref_sign_idx] != GOLDEN_FRAME);
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
      } else {
        if (edge_mbmi->ref_frame[0] == GOLDEN_FRAME)
          pred_context = 0;
        else
          pred_context = 2 + CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]);
      }
    }
  } else {  // no edges available (2)
    pred_context = 2;
  }

  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);

  return pred_context;
}

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
// Obtain contexts to signal a reference frame be either BWDREF/ALTREF2, or
// ALTREF.
int av1_get_pred_context_brfarf2_or_arf(const MACROBLOCKD *xd) {
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

  // Counts of BWDREF, ALTREF2, or ALTREF frames (B, A2, or A)
  int bwdref_counts[ALTREF_FRAME - BWDREF_FRAME + 1] = { 0 };

  if (above_in_image && is_inter_block(above_mbmi)) {
    if (above_mbmi->ref_frame[0] >= BWDREF_FRAME)
      ++bwdref_counts[above_mbmi->ref_frame[0] - BWDREF_FRAME];
    if (has_second_ref(above_mbmi)) {
      if (above_mbmi->ref_frame[1] >= BWDREF_FRAME)
        ++bwdref_counts[above_mbmi->ref_frame[1] - BWDREF_FRAME];
    }
  }

  if (left_in_image && is_inter_block(left_mbmi)) {
    if (left_mbmi->ref_frame[0] >= BWDREF_FRAME)
      ++bwdref_counts[left_mbmi->ref_frame[0] - BWDREF_FRAME];
    if (has_second_ref(left_mbmi)) {
      if (left_mbmi->ref_frame[1] >= BWDREF_FRAME)
        ++bwdref_counts[left_mbmi->ref_frame[1] - BWDREF_FRAME];
    }
  }

  const int brfarf2_count = bwdref_counts[BWDREF_FRAME - BWDREF_FRAME] +
                            bwdref_counts[ALTREF2_FRAME - BWDREF_FRAME];
  const int arf_count = bwdref_counts[ALTREF_FRAME - BWDREF_FRAME];
  const int pred_context =
      (brfarf2_count == arf_count) ? 1 : ((brfarf2_count < arf_count) ? 0 : 2);

  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  return pred_context;
}

// Obtain contexts to signal a reference frame be either BWDREF or ALTREF2.
int av1_get_pred_context_brf_or_arf2(const MACROBLOCKD *xd) {
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int above_in_image = xd->up_available;
  const int left_in_image = xd->left_available;

  // Count of BWDREF frames (B)
  int brf_count = 0;
  // Count of ALTREF2 frames (A2)
  int arf2_count = 0;

  if (above_in_image && is_inter_block(above_mbmi)) {
    if (above_mbmi->ref_frame[0] == BWDREF_FRAME)
      ++brf_count;
    else if (above_mbmi->ref_frame[0] == ALTREF2_FRAME)
      ++arf2_count;
    if (has_second_ref(above_mbmi)) {
      if (above_mbmi->ref_frame[1] == BWDREF_FRAME)
        ++brf_count;
      else if (above_mbmi->ref_frame[1] == ALTREF2_FRAME)
        ++arf2_count;
    }
  }

  if (left_in_image && is_inter_block(left_mbmi)) {
    if (left_mbmi->ref_frame[0] == BWDREF_FRAME)
      ++brf_count;
    else if (left_mbmi->ref_frame[0] == ALTREF2_FRAME)
      ++arf2_count;
    if (has_second_ref(left_mbmi)) {
      if (left_mbmi->ref_frame[1] == BWDREF_FRAME)
        ++brf_count;
      else if (left_mbmi->ref_frame[1] == ALTREF2_FRAME)
        ++arf2_count;
    }
  }

  const int pred_context =
      (brf_count == arf2_count) ? 1 : ((brf_count < arf2_count) ? 0 : 2);

  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  return pred_context;
}

// Signal the 2nd reference frame for a compound mode be either
// ALTREF, or ALTREF2/BWDREF.
int av1_get_pred_context_comp_bwdref_p(const AV1_COMMON *cm,
                                       const MACROBLOCKD *xd) {
  (void)cm;
  return av1_get_pred_context_brfarf2_or_arf(xd);
}

// Signal the 2nd reference frame for a compound mode be either
// ALTREF2 or BWDREF.
int av1_get_pred_context_comp_bwdref_p1(const AV1_COMMON *cm,
                                        const MACROBLOCKD *xd) {
  (void)cm;
  return av1_get_pred_context_brf_or_arf2(xd);
}

// For the bit to signal whether the single reference is a forward reference
// frame or a backward reference frame.
Yaowu Xu's avatar
Yaowu Xu committed
915
int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) {
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
  const int has_above = xd->up_available;
  const int has_left = xd->left_available;

  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
  if (has_above && has_left) {  // both edges available
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);

    if (above_intra && left_intra) {  // intra/intra
      pred_context = 2;
    } else if (above_intra || left_intra) {  // intra/inter or inter/intra
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;

935
      if (!has_second_ref(edge_mbmi))  // single
Zoe Liu's avatar
Zoe Liu committed
936
        pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
937 938
      else  // comp
        pred_context = 2;
939 940
    } else {  // inter/inter
      const int above_has_second = has_second_ref(above_mbmi);
clang-format's avatar
clang-format committed
941
      const int left_has_second = has_second_ref(left_mbmi);
942 943 944 945

      const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
      const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];

946 947 948
      if (above_has_second && left_has_second) {  // comp/comp
        pred_context = 2;
      } else if (above_has_second || left_has_second) {  // single/comp
949 950
        const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;

951 952
        pred_context = (!CHECK_BACKWARD_REFS(rfs)) ? 4 : 1;
      } else {  // single/single
Zoe Liu's avatar
Zoe Liu committed
953 954
        pred_context = 2 * (!CHECK_BACKWARD_REFS(above0)) +
                       2 * (!CHECK_BACKWARD_REFS(left0));
955 956 957 958 959 960
      }
    }
  } else if (has_above || has_left) {  // one edge available
    const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi;
    if (!is_inter_block(edge_mbmi)) {  // intra
      pred_context = 2;
961 962
    } else {                           // inter
      if (!has_second_ref(edge_mbmi))  // single
Zoe Liu's avatar
Zoe Liu committed
963
        pred_context = 4 * (!CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]));
964 965
      else  // comp
        pred_context = 2;
966 967 968 969 970 971 972 973 974 975
    }
  } else {  // no edges available
    pred_context = 2;
  }

  assert(pred_context >= 0 && pred_context < REF_CONTEXTS);
  return pred_context;
}

// For the bit to signal whether the single reference is ALTREF_FRAME or
976 977
// non-ALTREF backward reference frame, knowing that it shall be either of
// these 2 choices.
Yaowu Xu's avatar
Yaowu Xu committed
978
int av1_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) {
979
  return av1_get_pred_context_brfarf2_or_arf(xd);
980 981
}

982
// For the bit to signal whether the single reference is LAST3/GOLDEN or
983
// LAST2/LAST, knowing that it shall be either of these 2 choices.
Yaowu Xu's avatar
Yaowu Xu committed
984
int av1_get_pred_context_single_ref_p3(const MACROBLOCKD *xd) {
Jingning Han's avatar
Jingning Han committed
985 986 987
  int pred_context;
  const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
  const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
988 989
  const int has_above = xd->up_available;
  const int has_left = xd->left_available;
Jingning Han's avatar
Jingning Han committed
990 991 992 993 994

  // Note:
  // The mode info data structure has a one element border above and to the
  // left of the entries correpsonding to real macroblocks.
  // The prediction flags in these dummy entries are initialised to 0.
995
  if (has_above && has_left) {  // both edges available
Jingning Han's avatar
Jingning Han committed
996 997 998
    const int above_intra = !is_inter_block(above_mbmi);
    const int left_intra = !is_inter_block(left_mbmi);

999
    if (above_intra && left_intra) {  // intra/intra
Jingning Han's avatar
Jingning Han committed
1000
      pred_context = 2;
1001
    } else if (above_intra || left_intra) {  // intra/inter or inter/intra
Jingning Han's avatar
Jingning Han committed
1002
      const MB_MODE_INFO *edge_mbmi = above_intra ? left_mbmi : above_mbmi;
1003
      if (!has_second_ref(edge_mbmi)) {  // single
Zoe Liu's avatar
Zoe Liu committed
1004
        if (CHECK_BACKWARD_REFS(edge_mbmi->ref_frame[0]))
1005 1006 1007
          pred_context = 3;
        else
          pred_context = 4 * CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]);
1008
      } else {  // comp
1009
        pred_context = 1 +
clang-format's avatar
clang-format committed
1010 1011
                       2 * (CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[0]) ||
                            CHECK_LAST_OR_LAST2(edge_mbmi->ref_frame[1]));
1012
      }
Jingning Han's avatar
Jingning Han committed
1013
    } else {  // inter/inter
1014
      const int above_has_second = has_second_ref(above_mbmi);
clang-format's avatar
clang-format committed
1015
      const int left_has_second = has_second_ref(left_mbmi);
1016 1017 1018 1019
      const MV_REFERENCE_FRAME above0 = above_mbmi->ref_frame[0];
      const MV_REFERENCE_FRAME above1 = above_mbmi->ref_frame[1];
      const MV_REFERENCE_FRAME left0 = left_mbmi->ref_frame[0];
      const MV_REFERENCE_FRAME left1 = left_mbmi->ref_frame[1];
Jingning Han's avatar
Jingning Han committed
1020

1021
      if (above_has_second && left_has_second) {  // comp/comp
1022
        if (above0 == left0 && above1 == left1)
clang-format's avatar
clang-format committed
1023 1024 1025
          pred_context =
              3 * (CHECK_LAST_OR_LAST2(above0) || CHECK_LAST_OR_LAST2(above1) ||
                   CHECK_LAST_OR_LAST2(left0) || CHECK_LAST_OR_LAST2(left1));
Jingning Han's avatar
Jingning Han committed
1026 1027
        else
          pred_context = 2;
1028
      } else if (above_has_second || left_has_second) {  // single/comp
1029 1030 1031 1032 1033
        const MV_REFERENCE_FRAME rfs = !above_has_second ? above0 : left0;
        const MV_REFERENCE_FRAME crf1 = above_has_second ? above0 : left0;
        const MV_REFERENCE_FRAME crf2 = above_has_second ? above1 : left1;

        if (CHECK_LAST_OR_LAST2(rfs))
clang-format's avatar
clang-format committed
1034 1035
          pred_context =
              3 + (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
1036
        else if (CHECK_GOLDEN_OR_LAST3(rfs))
clang-format's avatar
clang-format committed
1037 1038
          pred_context =
              (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
Jingning Han's avatar
Jingning Han committed
1039
        else
clang-format's avatar
clang-format committed
1040 1041
          pred_context =
              1 + 2 * (CHECK_LAST_OR_LAST2(crf1) || CHECK_LAST_OR_LAST2(crf2));
1042
      } else {  // single/single
Zoe Liu's avatar
Zoe Liu committed
1043
        if (CHECK_BACKWARD_REFS(above0) && CHECK_BACKWARD_REFS(left0)) {
1044
          pred_context = 2 + (above0 == left0);
Zoe Liu's avatar
Zoe Liu committed
1045