Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [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 | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [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 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #ifndef AV1_COMMON_PRED_COMMON_H_ |
| 13 | #define AV1_COMMON_PRED_COMMON_H_ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 14 | |
| 15 | #include "av1/common/blockd.h" |
| 16 | #include "av1/common/onyxc_int.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 17 | #include "aom_dsp/aom_dsp_common.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Urvang Joshi | 5264844 | 2016-10-13 17:27:51 -0700 | [diff] [blame] | 23 | static INLINE int get_segment_id(const AV1_COMMON *const cm, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | const uint8_t *segment_ids, BLOCK_SIZE bsize, |
| 25 | int mi_row, int mi_col) { |
| 26 | const int mi_offset = mi_row * cm->mi_cols + mi_col; |
| 27 | const int bw = num_8x8_blocks_wide_lookup[bsize]; |
| 28 | const int bh = num_8x8_blocks_high_lookup[bsize]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 29 | const int xmis = AOMMIN(cm->mi_cols - mi_col, bw); |
| 30 | const int ymis = AOMMIN(cm->mi_rows - mi_row, bh); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 31 | int x, y, segment_id = MAX_SEGMENTS; |
| 32 | |
| 33 | for (y = 0; y < ymis; ++y) |
| 34 | for (x = 0; x < xmis; ++x) |
| 35 | segment_id = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 36 | AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 37 | |
| 38 | assert(segment_id >= 0 && segment_id < MAX_SEGMENTS); |
| 39 | return segment_id; |
| 40 | } |
| 41 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 42 | static INLINE int av1_get_pred_context_seg_id(const MACROBLOCKD *xd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 43 | const MODE_INFO *const above_mi = xd->above_mi; |
| 44 | const MODE_INFO *const left_mi = xd->left_mi; |
| 45 | const int above_sip = |
| 46 | (above_mi != NULL) ? above_mi->mbmi.seg_id_predicted : 0; |
| 47 | const int left_sip = (left_mi != NULL) ? left_mi->mbmi.seg_id_predicted : 0; |
| 48 | |
| 49 | return above_sip + left_sip; |
| 50 | } |
| 51 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 52 | static INLINE aom_prob av1_get_pred_prob_seg_id( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 53 | const struct segmentation_probs *segp, const MACROBLOCKD *xd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 54 | return segp->pred_probs[av1_get_pred_context_seg_id(xd)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 57 | static INLINE int av1_get_skip_context(const MACROBLOCKD *xd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 58 | const MODE_INFO *const above_mi = xd->above_mi; |
| 59 | const MODE_INFO *const left_mi = xd->left_mi; |
| 60 | const int above_skip = (above_mi != NULL) ? above_mi->mbmi.skip : 0; |
| 61 | const int left_skip = (left_mi != NULL) ? left_mi->mbmi.skip : 0; |
| 62 | return above_skip + left_skip; |
| 63 | } |
| 64 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 65 | static INLINE aom_prob av1_get_skip_prob(const AV1_COMMON *cm, |
| 66 | const MACROBLOCKD *xd) { |
| 67 | return cm->fc->skip_probs[av1_get_skip_context(xd)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | #if CONFIG_DUAL_FILTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 71 | int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd, int dir); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 72 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 73 | int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | #if CONFIG_EXT_INTRA |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 77 | int av1_get_pred_context_intra_interp(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | #endif // CONFIG_EXT_INTRA |
| 79 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 80 | int av1_get_intra_inter_context(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 81 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 82 | static INLINE aom_prob av1_get_intra_inter_prob(const AV1_COMMON *cm, |
| 83 | const MACROBLOCKD *xd) { |
| 84 | return cm->fc->intra_inter_prob[av1_get_intra_inter_context(xd)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 87 | int av1_get_reference_mode_context(const AV1_COMMON *cm, const MACROBLOCKD *xd); |
| 88 | |
| 89 | static INLINE aom_prob av1_get_reference_mode_prob(const AV1_COMMON *cm, |
| 90 | const MACROBLOCKD *xd) { |
| 91 | return cm->fc->comp_inter_prob[av1_get_reference_mode_context(cm, xd)]; |
| 92 | } |
| 93 | |
| 94 | int av1_get_pred_context_comp_ref_p(const AV1_COMMON *cm, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 95 | const MACROBLOCKD *xd); |
| 96 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 97 | static INLINE aom_prob av1_get_pred_prob_comp_ref_p(const AV1_COMMON *cm, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 98 | const MACROBLOCKD *xd) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 99 | const int pred_context = av1_get_pred_context_comp_ref_p(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 100 | return cm->fc->comp_ref_prob[pred_context][0]; |
| 101 | } |
| 102 | |
| 103 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 104 | int av1_get_pred_context_comp_ref_p1(const AV1_COMMON *cm, |
| 105 | const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 106 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 107 | static INLINE aom_prob av1_get_pred_prob_comp_ref_p1(const AV1_COMMON *cm, |
| 108 | const MACROBLOCKD *xd) { |
| 109 | const int pred_context = av1_get_pred_context_comp_ref_p1(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 110 | return cm->fc->comp_ref_prob[pred_context][1]; |
| 111 | } |
| 112 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 113 | int av1_get_pred_context_comp_ref_p2(const AV1_COMMON *cm, |
| 114 | const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 115 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 116 | static INLINE aom_prob av1_get_pred_prob_comp_ref_p2(const AV1_COMMON *cm, |
| 117 | const MACROBLOCKD *xd) { |
| 118 | const int pred_context = av1_get_pred_context_comp_ref_p2(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 119 | return cm->fc->comp_ref_prob[pred_context][2]; |
| 120 | } |
| 121 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 122 | int av1_get_pred_context_comp_bwdref_p(const AV1_COMMON *cm, |
| 123 | const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 124 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 125 | static INLINE aom_prob av1_get_pred_prob_comp_bwdref_p(const AV1_COMMON *cm, |
| 126 | const MACROBLOCKD *xd) { |
| 127 | const int pred_context = av1_get_pred_context_comp_bwdref_p(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 128 | return cm->fc->comp_bwdref_prob[pred_context][0]; |
| 129 | } |
| 130 | |
| 131 | #endif // CONFIG_EXT_REFS |
| 132 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 133 | int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 134 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 135 | static INLINE aom_prob av1_get_pred_prob_single_ref_p1(const AV1_COMMON *cm, |
| 136 | const MACROBLOCKD *xd) { |
| 137 | return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p1(xd)][0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 140 | int av1_get_pred_context_single_ref_p2(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 141 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 142 | static INLINE aom_prob av1_get_pred_prob_single_ref_p2(const AV1_COMMON *cm, |
| 143 | const MACROBLOCKD *xd) { |
| 144 | return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p2(xd)][1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 148 | int av1_get_pred_context_single_ref_p3(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 149 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 150 | static INLINE aom_prob av1_get_pred_prob_single_ref_p3(const AV1_COMMON *cm, |
| 151 | const MACROBLOCKD *xd) { |
| 152 | return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p3(xd)][2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 155 | int av1_get_pred_context_single_ref_p4(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 156 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 157 | static INLINE aom_prob av1_get_pred_prob_single_ref_p4(const AV1_COMMON *cm, |
| 158 | const MACROBLOCKD *xd) { |
| 159 | return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p4(xd)][3]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 162 | int av1_get_pred_context_single_ref_p5(const MACROBLOCKD *xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 163 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 164 | static INLINE aom_prob av1_get_pred_prob_single_ref_p5(const AV1_COMMON *cm, |
| 165 | const MACROBLOCKD *xd) { |
| 166 | return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p5(xd)][4]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 167 | } |
| 168 | #endif // CONFIG_EXT_REFS |
| 169 | |
| 170 | // Returns a context number for the given MB prediction signal |
| 171 | // The mode info data structure has a one element border above and to the |
| 172 | // left of the entries corresponding to real blocks. |
| 173 | // The prediction flags in these dummy entries are initialized to 0. |
| 174 | static INLINE int get_tx_size_context(const MACROBLOCKD *xd) { |
| 175 | const int max_tx_size = max_txsize_lookup[xd->mi[0]->mbmi.sb_type]; |
| 176 | const MB_MODE_INFO *const above_mbmi = xd->above_mbmi; |
| 177 | const MB_MODE_INFO *const left_mbmi = xd->left_mbmi; |
| 178 | const int has_above = xd->up_available; |
| 179 | const int has_left = xd->left_available; |
| 180 | int above_ctx = (has_above && !above_mbmi->skip) |
| 181 | ? (int)txsize_sqr_map[above_mbmi->tx_size] |
| 182 | : max_tx_size; |
| 183 | int left_ctx = (has_left && !left_mbmi->skip) |
| 184 | ? (int)txsize_sqr_map[left_mbmi->tx_size] |
| 185 | : max_tx_size; |
| 186 | assert(xd->mi[0]->mbmi.sb_type >= BLOCK_8X8); |
| 187 | if (!has_left) left_ctx = above_ctx; |
| 188 | |
| 189 | if (!has_above) above_ctx = left_ctx; |
| 190 | |
| 191 | return (above_ctx + left_ctx) > max_tx_size; |
| 192 | } |
| 193 | |
| 194 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 195 | static void update_tx_counts(AV1_COMMON *cm, MACROBLOCKD *xd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 196 | MB_MODE_INFO *mbmi, BLOCK_SIZE plane_bsize, |
| 197 | TX_SIZE tx_size, int blk_row, int blk_col, |
| 198 | TX_SIZE max_tx_size, int ctx) { |
| 199 | const struct macroblockd_plane *const pd = &xd->plane[0]; |
| 200 | const BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 201 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 202 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 203 | const TX_SIZE plane_tx_size = mbmi->inter_tx_size[tx_row][tx_col]; |
| 204 | int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize]; |
| 205 | int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize]; |
| 206 | |
| 207 | if (xd->mb_to_bottom_edge < 0) |
| 208 | max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y); |
| 209 | if (xd->mb_to_right_edge < 0) |
| 210 | max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x); |
| 211 | |
| 212 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 213 | |
| 214 | if (tx_size == plane_tx_size) { |
| 215 | ++xd->counts->tx_size[max_tx_size - TX_8X8][ctx][tx_size]; |
| 216 | mbmi->tx_size = tx_size; |
| 217 | } else { |
| 218 | int bsl = b_width_log2_lookup[bsize]; |
| 219 | int i; |
| 220 | |
| 221 | assert(bsl > 0); |
| 222 | --bsl; |
| 223 | |
| 224 | for (i = 0; i < 4; ++i) { |
| 225 | const int offsetr = blk_row + ((i >> 1) << bsl); |
| 226 | const int offsetc = blk_col + ((i & 0x01) << bsl); |
| 227 | |
| 228 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
Urvang Joshi | cb586f3 | 2016-09-20 11:36:33 -0700 | [diff] [blame] | 229 | update_tx_counts(cm, xd, mbmi, plane_bsize, (TX_SIZE)(tx_size - 1), |
| 230 | offsetr, offsetc, max_tx_size, ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 235 | static INLINE void inter_block_tx_count_update(AV1_COMMON *cm, MACROBLOCKD *xd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 236 | MB_MODE_INFO *mbmi, |
| 237 | BLOCK_SIZE plane_bsize, |
| 238 | int ctx) { |
| 239 | const int mi_width = num_4x4_blocks_wide_lookup[plane_bsize]; |
| 240 | const int mi_height = num_4x4_blocks_high_lookup[plane_bsize]; |
| 241 | TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize]; |
| 242 | BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size]; |
| 243 | int bh = num_4x4_blocks_wide_lookup[txb_size]; |
| 244 | int idx, idy; |
| 245 | |
| 246 | for (idy = 0; idy < mi_height; idy += bh) |
| 247 | for (idx = 0; idx < mi_width; idx += bh) |
| 248 | update_tx_counts(cm, xd, mbmi, plane_bsize, max_tx_size, idy, idx, |
| 249 | max_tx_size, ctx); |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | #ifdef __cplusplus |
| 254 | } // extern "C" |
| 255 | #endif |
| 256 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 257 | #endif // AV1_COMMON_PRED_COMMON_H_ |