Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | bde4ac8 | 2016-11-28 15:26:06 -0800 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "av1/common/common.h" |
| 13 | #include "av1/common/pred_common.h" |
| 14 | #include "av1/common/reconinter.h" |
hui su | 0c628e6 | 2016-11-30 15:20:48 -0800 | [diff] [blame] | 15 | #include "av1/common/reconintra.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | #include "av1/common/seg_common.h" |
| 17 | |
| 18 | // Returns a context number for the given MB prediction signal |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 19 | static InterpFilter get_ref_filter_type(const MB_MODE_INFO *ref_mbmi, |
James Zern | 7b9407a | 2016-05-18 23:48:05 -0700 | [diff] [blame] | 20 | const MACROBLOCKD *xd, int dir, |
| 21 | MV_REFERENCE_FRAME ref_frame) { |
Jingning Han | 4a17335 | 2018-03-01 17:54:07 -0800 | [diff] [blame] | 22 | (void)xd; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 23 | |
Jingning Han | 4a17335 | 2018-03-01 17:54:07 -0800 | [diff] [blame] | 24 | return ((ref_mbmi->ref_frame[0] == ref_frame || |
| 25 | ref_mbmi->ref_frame[1] == ref_frame) |
Rupert Swarbrick | 27e9029 | 2017-09-28 17:46:50 +0100 | [diff] [blame] | 26 | ? av1_extract_interp_filter(ref_mbmi->interp_filters, dir & 0x01) |
| 27 | : SWITCHABLE_FILTERS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 30 | int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd, int dir) { |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 31 | const MB_MODE_INFO *const mbmi = xd->mi[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 32 | const int ctx_offset = |
| 33 | (mbmi->ref_frame[1] > INTRA_FRAME) * INTER_FILTER_COMP_OFFSET; |
Peng Bin | 0551fc4 | 2018-08-27 17:14:45 +0800 | [diff] [blame] | 34 | assert(dir == 0 || dir == 1); |
| 35 | const MV_REFERENCE_FRAME ref_frame = mbmi->ref_frame[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 36 | // Note: |
| 37 | // The mode info data structure has a one element border above and to the |
| 38 | // left of the entries corresponding to real macroblocks. |
| 39 | // The prediction flags in these dummy entries are initialized to 0. |
| 40 | int filter_type_ctx = ctx_offset + (dir & 0x01) * INTER_FILTER_DIR_OFFSET; |
| 41 | int left_type = SWITCHABLE_FILTERS; |
| 42 | int above_type = SWITCHABLE_FILTERS; |
| 43 | |
| 44 | if (xd->left_available) |
| 45 | left_type = get_ref_filter_type(xd->mi[-1], xd, dir, ref_frame); |
| 46 | |
| 47 | if (xd->up_available) |
| 48 | above_type = |
| 49 | get_ref_filter_type(xd->mi[-xd->mi_stride], xd, dir, ref_frame); |
| 50 | |
Urvang Joshi | e06132e | 2017-02-17 15:36:03 -0800 | [diff] [blame] | 51 | if (left_type == above_type) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 52 | filter_type_ctx += left_type; |
Urvang Joshi | e06132e | 2017-02-17 15:36:03 -0800 | [diff] [blame] | 53 | } else if (left_type == SWITCHABLE_FILTERS) { |
| 54 | assert(above_type != SWITCHABLE_FILTERS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 55 | filter_type_ctx += above_type; |
Urvang Joshi | e06132e | 2017-02-17 15:36:03 -0800 | [diff] [blame] | 56 | } else if (above_type == SWITCHABLE_FILTERS) { |
| 57 | assert(left_type != SWITCHABLE_FILTERS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 58 | filter_type_ctx += left_type; |
Urvang Joshi | e06132e | 2017-02-17 15:36:03 -0800 | [diff] [blame] | 59 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 60 | filter_type_ctx += SWITCHABLE_FILTERS; |
Urvang Joshi | e06132e | 2017-02-17 15:36:03 -0800 | [diff] [blame] | 61 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 62 | |
| 63 | return filter_type_ctx; |
| 64 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 65 | |
Sebastien Alaiwan | 88c1c3f | 2017-11-30 11:56:09 +0100 | [diff] [blame] | 66 | static void palette_add_to_cache(uint16_t *cache, int *n, uint16_t val) { |
| 67 | // Do not add an already existing value |
| 68 | if (*n > 0 && val == cache[*n - 1]) return; |
| 69 | |
| 70 | cache[(*n)++] = val; |
| 71 | } |
| 72 | |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 73 | int av1_get_palette_cache(const MACROBLOCKD *const xd, int plane, |
| 74 | uint16_t *cache) { |
| 75 | const int row = -xd->mb_to_top_edge >> 3; |
| 76 | // Do not refer to above SB row when on SB boundary. |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 77 | const MB_MODE_INFO *const above_mi = |
| 78 | (row % (1 << MIN_SB_SIZE_LOG2)) ? xd->above_mbmi : NULL; |
| 79 | const MB_MODE_INFO *const left_mi = xd->left_mbmi; |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 80 | int above_n = 0, left_n = 0; |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 81 | if (above_mi) above_n = above_mi->palette_mode_info.palette_size[plane != 0]; |
| 82 | if (left_mi) left_n = left_mi->palette_mode_info.palette_size[plane != 0]; |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 83 | if (above_n == 0 && left_n == 0) return 0; |
| 84 | int above_idx = plane * PALETTE_MAX_SIZE; |
| 85 | int left_idx = plane * PALETTE_MAX_SIZE; |
| 86 | int n = 0; |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 87 | const uint16_t *above_colors = |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 88 | above_mi ? above_mi->palette_mode_info.palette_colors : NULL; |
Hui Su | 3748bc2 | 2017-08-23 11:30:41 -0700 | [diff] [blame] | 89 | const uint16_t *left_colors = |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 90 | left_mi ? left_mi->palette_mode_info.palette_colors : NULL; |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 91 | // Merge the sorted lists of base colors from above and left to get |
| 92 | // combined sorted color cache. |
| 93 | while (above_n > 0 && left_n > 0) { |
| 94 | uint16_t v_above = above_colors[above_idx]; |
| 95 | uint16_t v_left = left_colors[left_idx]; |
| 96 | if (v_left < v_above) { |
Sebastien Alaiwan | 88c1c3f | 2017-11-30 11:56:09 +0100 | [diff] [blame] | 97 | palette_add_to_cache(cache, &n, v_left); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 98 | ++left_idx, --left_n; |
| 99 | } else { |
Sebastien Alaiwan | 88c1c3f | 2017-11-30 11:56:09 +0100 | [diff] [blame] | 100 | palette_add_to_cache(cache, &n, v_above); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 101 | ++above_idx, --above_n; |
| 102 | if (v_left == v_above) ++left_idx, --left_n; |
| 103 | } |
| 104 | } |
| 105 | while (above_n-- > 0) { |
| 106 | uint16_t val = above_colors[above_idx++]; |
Sebastien Alaiwan | 88c1c3f | 2017-11-30 11:56:09 +0100 | [diff] [blame] | 107 | palette_add_to_cache(cache, &n, val); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 108 | } |
| 109 | while (left_n-- > 0) { |
| 110 | uint16_t val = left_colors[left_idx++]; |
Sebastien Alaiwan | 88c1c3f | 2017-11-30 11:56:09 +0100 | [diff] [blame] | 111 | palette_add_to_cache(cache, &n, val); |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 112 | } |
| 113 | assert(n <= 2 * PALETTE_MAX_SIZE); |
| 114 | return n; |
| 115 | } |
hui su | 33567b2 | 2017-04-30 16:40:19 -0700 | [diff] [blame] | 116 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 117 | // The mode info data structure has a one element border above and to the |
| 118 | // left of the entries corresponding to real macroblocks. |
| 119 | // The prediction flags in these dummy entries are initialized to 0. |
| 120 | // 0 - inter/inter, inter/--, --/inter, --/-- |
| 121 | // 1 - intra/inter, inter/intra |
| 122 | // 2 - intra/--, --/intra |
| 123 | // 3 - intra/intra |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 124 | int av1_get_intra_inter_context(const MACROBLOCKD *xd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 125 | const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 126 | const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 127 | const int has_above = xd->up_available; |
| 128 | const int has_left = xd->left_available; |
| 129 | |
| 130 | if (has_above && has_left) { // both edges available |
| 131 | const int above_intra = !is_inter_block(above_mbmi); |
| 132 | const int left_intra = !is_inter_block(left_mbmi); |
| 133 | return left_intra && above_intra ? 3 : left_intra || above_intra; |
| 134 | } else if (has_above || has_left) { // one edge available |
| 135 | return 2 * !is_inter_block(has_above ? above_mbmi : left_mbmi); |
| 136 | } else { |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 | |
Zoe Liu | 6cfaff9 | 2016-10-18 17:12:11 -0700 | [diff] [blame] | 141 | #define CHECK_BACKWARD_REFS(ref_frame) \ |
| 142 | (((ref_frame) >= BWDREF_FRAME) && ((ref_frame) <= ALTREF_FRAME)) |
| 143 | #define IS_BACKWARD_REF_FRAME(ref_frame) CHECK_BACKWARD_REFS(ref_frame) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 144 | |
Yaowu Xu | 6567c4a | 2018-03-19 21:39:31 -0700 | [diff] [blame] | 145 | int av1_get_reference_mode_context(const MACROBLOCKD *xd) { |
Zoe Liu | 6cfaff9 | 2016-10-18 17:12:11 -0700 | [diff] [blame] | 146 | int ctx; |
| 147 | const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 148 | const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 149 | const int has_above = xd->up_available; |
| 150 | const int has_left = xd->left_available; |
| 151 | |
Zoe Liu | 6cfaff9 | 2016-10-18 17:12:11 -0700 | [diff] [blame] | 152 | // Note: |
| 153 | // The mode info data structure has a one element border above and to the |
| 154 | // left of the entries corresponding to real macroblocks. |
| 155 | // The prediction flags in these dummy entries are initialized to 0. |
| 156 | if (has_above && has_left) { // both edges available |
| 157 | if (!has_second_ref(above_mbmi) && !has_second_ref(left_mbmi)) |
| 158 | // neither edge uses comp pred (0/1) |
| 159 | ctx = IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) ^ |
| 160 | IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]); |
| 161 | else if (!has_second_ref(above_mbmi)) |
| 162 | // one of two edges uses comp pred (2/3) |
| 163 | ctx = 2 + (IS_BACKWARD_REF_FRAME(above_mbmi->ref_frame[0]) || |
| 164 | !is_inter_block(above_mbmi)); |
| 165 | else if (!has_second_ref(left_mbmi)) |
| 166 | // one of two edges uses comp pred (2/3) |
| 167 | ctx = 2 + (IS_BACKWARD_REF_FRAME(left_mbmi->ref_frame[0]) || |
| 168 | !is_inter_block(left_mbmi)); |
| 169 | else // both edges use comp pred (4) |
| 170 | ctx = 4; |
| 171 | } else if (has_above || has_left) { // one edge available |
| 172 | const MB_MODE_INFO *edge_mbmi = has_above ? above_mbmi : left_mbmi; |
| 173 | |
| 174 | if (!has_second_ref(edge_mbmi)) |
| 175 | // edge does not use comp pred (0/1) |
| 176 | ctx = IS_BACKWARD_REF_FRAME(edge_mbmi->ref_frame[0]); |
| 177 | else |
| 178 | // edge uses comp pred (3) |
| 179 | ctx = 3; |
| 180 | } else { // no edges available (1) |
| 181 | ctx = 1; |
| 182 | } |
| 183 | assert(ctx >= 0 && ctx < COMP_INTER_CONTEXTS); |
| 184 | return ctx; |
| 185 | } |
| 186 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 187 | int av1_get_comp_reference_type_context(const MACROBLOCKD *xd) { |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 188 | int pred_context; |
| 189 | const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 190 | const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 191 | const int above_in_image = xd->up_available; |
| 192 | const int left_in_image = xd->left_available; |
| 193 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 194 | if (above_in_image && left_in_image) { // both edges available |
| 195 | const int above_intra = !is_inter_block(above_mbmi); |
| 196 | const int left_intra = !is_inter_block(left_mbmi); |
| 197 | |
| 198 | if (above_intra && left_intra) { // intra/intra |
| 199 | pred_context = 2; |
| 200 | } else if (above_intra || left_intra) { // intra/inter |
| 201 | const MB_MODE_INFO *inter_mbmi = above_intra ? left_mbmi : above_mbmi; |
| 202 | |
| 203 | if (!has_second_ref(inter_mbmi)) // single pred |
| 204 | pred_context = 2; |
| 205 | else // comp pred |
| 206 | pred_context = 1 + 2 * has_uni_comp_refs(inter_mbmi); |
| 207 | } else { // inter/inter |
| 208 | const int a_sg = !has_second_ref(above_mbmi); |
| 209 | const int l_sg = !has_second_ref(left_mbmi); |
| 210 | const MV_REFERENCE_FRAME frfa = above_mbmi->ref_frame[0]; |
| 211 | const MV_REFERENCE_FRAME frfl = left_mbmi->ref_frame[0]; |
| 212 | |
| 213 | if (a_sg && l_sg) { // single/single |
Johann | 6b41d4d | 2018-02-08 14:32:53 -0800 | [diff] [blame] | 214 | pred_context = 1 + 2 * (!(IS_BACKWARD_REF_FRAME(frfa) ^ |
| 215 | IS_BACKWARD_REF_FRAME(frfl))); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 216 | } else if (l_sg || a_sg) { // single/comp |
| 217 | const int uni_rfc = |
| 218 | a_sg ? has_uni_comp_refs(left_mbmi) : has_uni_comp_refs(above_mbmi); |
| 219 | |
| 220 | if (!uni_rfc) // comp bidir |
| 221 | pred_context = 1; |
| 222 | else // comp unidir |
Zoe Liu | 53fd2ae | 2017-08-23 11:33:04 -0700 | [diff] [blame] | 223 | pred_context = 3 + (!(IS_BACKWARD_REF_FRAME(frfa) ^ |
| 224 | IS_BACKWARD_REF_FRAME(frfl))); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 225 | } else { // comp/comp |
| 226 | const int a_uni_rfc = has_uni_comp_refs(above_mbmi); |
| 227 | const int l_uni_rfc = has_uni_comp_refs(left_mbmi); |
| 228 | |
| 229 | if (!a_uni_rfc && !l_uni_rfc) // bidir/bidir |
| 230 | pred_context = 0; |
| 231 | else if (!a_uni_rfc || !l_uni_rfc) // unidir/bidir |
| 232 | pred_context = 2; |
| 233 | else // unidir/unidir |
| 234 | pred_context = |
| 235 | 3 + (!((frfa == BWDREF_FRAME) ^ (frfl == BWDREF_FRAME))); |
| 236 | } |
| 237 | } |
| 238 | } else if (above_in_image || left_in_image) { // one edge available |
| 239 | const MB_MODE_INFO *edge_mbmi = above_in_image ? above_mbmi : left_mbmi; |
| 240 | |
| 241 | if (!is_inter_block(edge_mbmi)) { // intra |
| 242 | pred_context = 2; |
| 243 | } else { // inter |
| 244 | if (!has_second_ref(edge_mbmi)) // single pred |
| 245 | pred_context = 2; |
| 246 | else // comp pred |
| 247 | pred_context = 4 * has_uni_comp_refs(edge_mbmi); |
| 248 | } |
| 249 | } else { // no edges available |
| 250 | pred_context = 2; |
| 251 | } |
| 252 | |
| 253 | assert(pred_context >= 0 && pred_context < COMP_REF_TYPE_CONTEXTS); |
| 254 | return pred_context; |
| 255 | } |
| 256 | |
| 257 | // Returns a context number for the given MB prediction signal |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 258 | // |
| 259 | // Signal the uni-directional compound reference frame pair as either |
| 260 | // (BWDREF, ALTREF), or (LAST, LAST2) / (LAST, LAST3) / (LAST, GOLDEN), |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 261 | // conditioning on the pair is known as uni-directional. |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 262 | // |
| 263 | // 3 contexts: Voting is used to compare the count of forward references with |
| 264 | // that of backward references from the spatial neighbors. |
| 265 | int av1_get_pred_context_uni_comp_ref_p(const MACROBLOCKD *xd) { |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 266 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 267 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 268 | // Count of forward references (L, L2, L3, or G) |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 269 | const int frf_count = ref_counts[LAST_FRAME] + ref_counts[LAST2_FRAME] + |
| 270 | ref_counts[LAST3_FRAME] + ref_counts[GOLDEN_FRAME]; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 271 | // Count of backward references (B or A) |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 272 | const int brf_count = ref_counts[BWDREF_FRAME] + ref_counts[ALTREF2_FRAME] + |
| 273 | ref_counts[ALTREF_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 274 | |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 275 | const int pred_context = |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 276 | (frf_count == brf_count) ? 1 : ((frf_count < brf_count) ? 0 : 2); |
| 277 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 278 | assert(pred_context >= 0 && pred_context < UNI_COMP_REF_CONTEXTS); |
| 279 | return pred_context; |
| 280 | } |
| 281 | |
| 282 | // Returns a context number for the given MB prediction signal |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 283 | // |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 284 | // Signal the uni-directional compound reference frame pair as |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 285 | // either (LAST, LAST2), or (LAST, LAST3) / (LAST, GOLDEN), |
| 286 | // conditioning on the pair is known as one of the above three. |
| 287 | // |
| 288 | // 3 contexts: Voting is used to compare the count of LAST2_FRAME with the |
| 289 | // total count of LAST3/GOLDEN from the spatial neighbors. |
| 290 | int av1_get_pred_context_uni_comp_ref_p1(const MACROBLOCKD *xd) { |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 291 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 292 | |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 293 | // Count of LAST2 |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 294 | const int last2_count = ref_counts[LAST2_FRAME]; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 295 | // Count of LAST3 or GOLDEN |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 296 | const int last3_or_gld_count = |
| 297 | ref_counts[LAST3_FRAME] + ref_counts[GOLDEN_FRAME]; |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 298 | |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 299 | const int pred_context = (last2_count == last3_or_gld_count) |
| 300 | ? 1 |
| 301 | : ((last2_count < last3_or_gld_count) ? 0 : 2); |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 302 | |
| 303 | assert(pred_context >= 0 && pred_context < UNI_COMP_REF_CONTEXTS); |
| 304 | return pred_context; |
| 305 | } |
| 306 | |
| 307 | // Returns a context number for the given MB prediction signal |
| 308 | // |
| 309 | // Signal the uni-directional compound reference frame pair as |
| 310 | // either (LAST, LAST3) or (LAST, GOLDEN), |
| 311 | // conditioning on the pair is known as one of the above two. |
| 312 | // |
| 313 | // 3 contexts: Voting is used to compare the count of LAST3_FRAME with the |
| 314 | // total count of GOLDEN_FRAME from the spatial neighbors. |
| 315 | int av1_get_pred_context_uni_comp_ref_p2(const MACROBLOCKD *xd) { |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 316 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 317 | |
| 318 | // Count of LAST3 |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 319 | const int last3_count = ref_counts[LAST3_FRAME]; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 320 | // Count of GOLDEN |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 321 | const int gld_count = ref_counts[GOLDEN_FRAME]; |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 322 | |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 323 | const int pred_context = |
Zoe Liu | fcf5fa2 | 2017-06-26 16:00:38 -0700 | [diff] [blame] | 324 | (last3_count == gld_count) ? 1 : ((last3_count < gld_count) ? 0 : 2); |
| 325 | |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 326 | assert(pred_context >= 0 && pred_context < UNI_COMP_REF_CONTEXTS); |
| 327 | return pred_context; |
| 328 | } |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 329 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 330 | // == Common context functions for both comp and single ref == |
| 331 | // |
| 332 | // Obtain contexts to signal a reference frame to be either LAST/LAST2 or |
| 333 | // LAST3/GOLDEN. |
| 334 | static int get_pred_context_ll2_or_l3gld(const MACROBLOCKD *xd) { |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 335 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 336 | |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 337 | // Count of LAST + LAST2 |
| 338 | const int last_last2_count = ref_counts[LAST_FRAME] + ref_counts[LAST2_FRAME]; |
| 339 | // Count of LAST3 + GOLDEN |
| 340 | const int last3_gld_count = |
| 341 | ref_counts[LAST3_FRAME] + ref_counts[GOLDEN_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 342 | |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 343 | const int pred_context = (last_last2_count == last3_gld_count) |
| 344 | ? 1 |
| 345 | : ((last_last2_count < last3_gld_count) ? 0 : 2); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 346 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 347 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 348 | return pred_context; |
| 349 | } |
| 350 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 351 | // Obtain contexts to signal a reference frame to be either LAST or LAST2. |
| 352 | static int get_pred_context_last_or_last2(const MACROBLOCKD *xd) { |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 353 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 354 | |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 355 | // Count of LAST |
| 356 | const int last_count = ref_counts[LAST_FRAME]; |
| 357 | // Count of LAST2 |
| 358 | const int last2_count = ref_counts[LAST2_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 359 | |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 360 | const int pred_context = |
| 361 | (last_count == last2_count) ? 1 : ((last_count < last2_count) ? 0 : 2); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 362 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 363 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 364 | return pred_context; |
| 365 | } |
| 366 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 367 | // Obtain contexts to signal a reference frame to be either LAST3 or GOLDEN. |
| 368 | static int get_pred_context_last3_or_gld(const MACROBLOCKD *xd) { |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 369 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 370 | |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 371 | // Count of LAST3 |
| 372 | const int last3_count = ref_counts[LAST3_FRAME]; |
| 373 | // Count of GOLDEN |
| 374 | const int gld_count = ref_counts[GOLDEN_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 375 | |
Zoe Liu | 4917295 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 376 | const int pred_context = |
| 377 | (last3_count == gld_count) ? 1 : ((last3_count < gld_count) ? 0 : 2); |
Zoe Liu | c082bbc | 2017-05-17 13:31:37 -0700 | [diff] [blame] | 378 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 379 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 380 | return pred_context; |
| 381 | } |
| 382 | |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 383 | // Obtain contexts to signal a reference frame be either BWDREF/ALTREF2, or |
| 384 | // ALTREF. |
Zoe Liu | 3a8a25f | 2018-02-12 14:20:54 -0800 | [diff] [blame] | 385 | static int get_pred_context_brfarf2_or_arf(const MACROBLOCKD *xd) { |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 386 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 387 | |
| 388 | // Counts of BWDREF, ALTREF2, or ALTREF frames (B, A2, or A) |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 389 | const int brfarf2_count = |
| 390 | ref_counts[BWDREF_FRAME] + ref_counts[ALTREF2_FRAME]; |
| 391 | const int arf_count = ref_counts[ALTREF_FRAME]; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 392 | |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 393 | const int pred_context = |
| 394 | (brfarf2_count == arf_count) ? 1 : ((brfarf2_count < arf_count) ? 0 : 2); |
| 395 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 396 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 397 | return pred_context; |
| 398 | } |
| 399 | |
| 400 | // Obtain contexts to signal a reference frame be either BWDREF or ALTREF2. |
Zoe Liu | 3a8a25f | 2018-02-12 14:20:54 -0800 | [diff] [blame] | 401 | static int get_pred_context_brf_or_arf2(const MACROBLOCKD *xd) { |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 402 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 403 | |
| 404 | // Count of BWDREF frames (B) |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 405 | const int brf_count = ref_counts[BWDREF_FRAME]; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 406 | // Count of ALTREF2 frames (A2) |
Zoe Liu | fa8bad1 | 2018-01-23 14:32:31 -0800 | [diff] [blame] | 407 | const int arf2_count = ref_counts[ALTREF2_FRAME]; |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 408 | |
| 409 | const int pred_context = |
| 410 | (brf_count == arf2_count) ? 1 : ((brf_count < arf2_count) ? 0 : 2); |
| 411 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 412 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 413 | return pred_context; |
| 414 | } |
| 415 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 416 | // == Context functions for comp ref == |
| 417 | // |
| 418 | // Returns a context number for the given MB prediction signal |
| 419 | // Signal the first reference frame for a compound mode be either |
| 420 | // GOLDEN/LAST3, or LAST/LAST2. |
| 421 | int av1_get_pred_context_comp_ref_p(const MACROBLOCKD *xd) { |
| 422 | return get_pred_context_ll2_or_l3gld(xd); |
| 423 | } |
| 424 | |
| 425 | // Returns a context number for the given MB prediction signal |
| 426 | // Signal the first reference frame for a compound mode be LAST, |
| 427 | // conditioning on that it is known either LAST/LAST2. |
| 428 | int av1_get_pred_context_comp_ref_p1(const MACROBLOCKD *xd) { |
| 429 | return get_pred_context_last_or_last2(xd); |
| 430 | } |
| 431 | |
| 432 | // Returns a context number for the given MB prediction signal |
| 433 | // Signal the first reference frame for a compound mode be GOLDEN, |
| 434 | // conditioning on that it is known either GOLDEN or LAST3. |
| 435 | int av1_get_pred_context_comp_ref_p2(const MACROBLOCKD *xd) { |
| 436 | return get_pred_context_last3_or_gld(xd); |
| 437 | } |
| 438 | |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 439 | // Signal the 2nd reference frame for a compound mode be either |
| 440 | // ALTREF, or ALTREF2/BWDREF. |
Zoe Liu | d15afb2 | 2018-02-08 17:10:51 -0800 | [diff] [blame] | 441 | int av1_get_pred_context_comp_bwdref_p(const MACROBLOCKD *xd) { |
Zoe Liu | 3a8a25f | 2018-02-12 14:20:54 -0800 | [diff] [blame] | 442 | return get_pred_context_brfarf2_or_arf(xd); |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Signal the 2nd reference frame for a compound mode be either |
| 446 | // ALTREF2 or BWDREF. |
Zoe Liu | d15afb2 | 2018-02-08 17:10:51 -0800 | [diff] [blame] | 447 | int av1_get_pred_context_comp_bwdref_p1(const MACROBLOCKD *xd) { |
Zoe Liu | 3a8a25f | 2018-02-12 14:20:54 -0800 | [diff] [blame] | 448 | return get_pred_context_brf_or_arf2(xd); |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 451 | // == Context functions for single ref == |
| 452 | // |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 453 | // For the bit to signal whether the single reference is a forward reference |
| 454 | // frame or a backward reference frame. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 455 | int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd) { |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 456 | const uint8_t *const ref_counts = &xd->neighbors_ref_counts[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 457 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 458 | // Count of forward reference frames |
| 459 | const int fwd_count = ref_counts[LAST_FRAME] + ref_counts[LAST2_FRAME] + |
| 460 | ref_counts[LAST3_FRAME] + ref_counts[GOLDEN_FRAME]; |
| 461 | // Count of backward reference frames |
| 462 | const int bwd_count = ref_counts[BWDREF_FRAME] + ref_counts[ALTREF2_FRAME] + |
| 463 | ref_counts[ALTREF_FRAME]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 464 | |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 465 | const int pred_context = |
| 466 | (fwd_count == bwd_count) ? 1 : ((fwd_count < bwd_count) ? 0 : 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 467 | |
| 468 | assert(pred_context >= 0 && pred_context < REF_CONTEXTS); |
| 469 | return pred_context; |
| 470 | } |
| 471 | |
| 472 | // For the bit to signal whether the single reference is ALTREF_FRAME or |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 473 | // non-ALTREF backward reference frame, knowing that it shall be either of |
| 474 | // these 2 choices. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 475 | int av1_get_pred_context_single_ref_p2(const MACROBLOCKD *xd) { |
Zoe Liu | 3a8a25f | 2018-02-12 14:20:54 -0800 | [diff] [blame] | 476 | return get_pred_context_brfarf2_or_arf(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // For the bit to signal whether the single reference is LAST3/GOLDEN or |
| 480 | // LAST2/LAST, knowing that it shall be either of these 2 choices. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 481 | int av1_get_pred_context_single_ref_p3(const MACROBLOCKD *xd) { |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 482 | return get_pred_context_ll2_or_l3gld(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // For the bit to signal whether the single reference is LAST2_FRAME or |
| 486 | // LAST_FRAME, knowing that it shall be either of these 2 choices. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 487 | int av1_get_pred_context_single_ref_p4(const MACROBLOCKD *xd) { |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 488 | return get_pred_context_last_or_last2(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // For the bit to signal whether the single reference is GOLDEN_FRAME or |
| 492 | // LAST3_FRAME, knowing that it shall be either of these 2 choices. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 493 | int av1_get_pred_context_single_ref_p5(const MACROBLOCKD *xd) { |
Zoe Liu | 3b35347 | 2018-02-12 13:58:22 -0800 | [diff] [blame] | 494 | return get_pred_context_last3_or_gld(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 497 | // For the bit to signal whether the single reference is ALTREF2_FRAME or |
| 498 | // BWDREF_FRAME, knowing that it shall be either of these 2 choices. |
| 499 | int av1_get_pred_context_single_ref_p6(const MACROBLOCKD *xd) { |
Zoe Liu | 3a8a25f | 2018-02-12 14:20:54 -0800 | [diff] [blame] | 500 | return get_pred_context_brf_or_arf2(xd); |
Zoe Liu | 97ad058 | 2017-02-09 10:51:00 -0800 | [diff] [blame] | 501 | } |