pred_common.h 14.3 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
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
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef AV1_COMMON_PRED_COMMON_H_
#define AV1_COMMON_PRED_COMMON_H_
Jingning Han's avatar
Jingning Han committed
14

15 16
#include "av1/common/blockd.h"
#include "av1/common/onyxc_int.h"
Yaowu Xu's avatar
Yaowu Xu committed
17
#include "aom_dsp/aom_dsp_common.h"
Jingning Han's avatar
Jingning Han committed
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

23
static INLINE int get_segment_id(const AV1_COMMON *const cm,
clang-format's avatar
clang-format committed
24 25
                                 const uint8_t *segment_ids, BLOCK_SIZE bsize,
                                 int mi_row, int mi_col) {
Jingning Han's avatar
Jingning Han committed
26
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
27 28
  const int bw = mi_size_wide[bsize];
  const int bh = mi_size_high[bsize];
Yaowu Xu's avatar
Yaowu Xu committed
29 30
  const int xmis = AOMMIN(cm->mi_cols - mi_col, bw);
  const int ymis = AOMMIN(cm->mi_rows - mi_row, bh);
Jingning Han's avatar
Jingning Han committed
31 32 33 34
  int x, y, segment_id = MAX_SEGMENTS;

  for (y = 0; y < ymis; ++y)
    for (x = 0; x < xmis; ++x)
35
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
36
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
37 38 39 40 41

  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
42
static INLINE int av1_get_pred_context_seg_id(const MACROBLOCKD *xd) {
Jingning Han's avatar
Jingning Han committed
43 44
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
clang-format's avatar
clang-format committed
45 46
  const int above_sip =
      (above_mi != NULL) ? above_mi->mbmi.seg_id_predicted : 0;
Jingning Han's avatar
Jingning Han committed
47 48 49 50 51
  const int left_sip = (left_mi != NULL) ? left_mi->mbmi.seg_id_predicted : 0;

  return above_sip + left_sip;
}

Yaowu Xu's avatar
Yaowu Xu committed
52
static INLINE aom_prob av1_get_pred_prob_seg_id(
53
    const struct segmentation_probs *segp, const MACROBLOCKD *xd) {
Yaowu Xu's avatar
Yaowu Xu committed
54
  return segp->pred_probs[av1_get_pred_context_seg_id(xd)];
Jingning Han's avatar
Jingning Han committed
55 56
}

Yaowu Xu's avatar
Yaowu Xu committed
57
static INLINE int av1_get_skip_context(const MACROBLOCKD *xd) {
Jingning Han's avatar
Jingning Han committed
58 59 60 61 62 63 64
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
  const int above_skip = (above_mi != NULL) ? above_mi->mbmi.skip : 0;
  const int left_skip = (left_mi != NULL) ? left_mi->mbmi.skip : 0;
  return above_skip + left_skip;
}

Yaowu Xu's avatar
Yaowu Xu committed
65 66 67
static INLINE aom_prob av1_get_skip_prob(const AV1_COMMON *cm,
                                         const MACROBLOCKD *xd) {
  return cm->fc->skip_probs[av1_get_skip_context(xd)];
Jingning Han's avatar
Jingning Han committed
68 69
}

70
#if CONFIG_DUAL_FILTER
Yaowu Xu's avatar
Yaowu Xu committed
71
int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd, int dir);
72
#else
Yaowu Xu's avatar
Yaowu Xu committed
73
int av1_get_pred_context_switchable_interp(const MACROBLOCKD *xd);
74
#endif
Jingning Han's avatar
Jingning Han committed
75

76
#if CONFIG_EXT_INTRA
hui su's avatar
hui su committed
77
#if CONFIG_INTRA_INTERP
Yaowu Xu's avatar
Yaowu Xu committed
78
int av1_get_pred_context_intra_interp(const MACROBLOCKD *xd);
hui su's avatar
hui su committed
79
#endif  // CONFIG_INTRA_INTERP
80 81
#endif  // CONFIG_EXT_INTRA

82 83 84 85 86 87 88 89 90
#if CONFIG_PALETTE && CONFIG_PALETTE_DELTA_ENCODING
// Get a list of palette base colors that are used in the above and left blocks,
// referred to as "color cache". The return value is the number of colors in the
// cache (<= 2 * PALETTE_MAX_SIZE). The color values are stored in "cache"
// in ascending order.
int av1_get_palette_cache(const MODE_INFO *above_mi, const MODE_INFO *left_mi,
                          int plane, uint16_t *cache);
#endif  // CONFIG_PALETTE && CONFIG_PALETTE_DELTA_ENCODING

Yaowu Xu's avatar
Yaowu Xu committed
91
int av1_get_intra_inter_context(const MACROBLOCKD *xd);
Jingning Han's avatar
Jingning Han committed
92

Yaowu Xu's avatar
Yaowu Xu committed
93 94 95
static INLINE aom_prob av1_get_intra_inter_prob(const AV1_COMMON *cm,
                                                const MACROBLOCKD *xd) {
  return cm->fc->intra_inter_prob[av1_get_intra_inter_context(xd)];
Jingning Han's avatar
Jingning Han committed
96 97
}

Yaowu Xu's avatar
Yaowu Xu committed
98
int av1_get_reference_mode_context(const AV1_COMMON *cm, const MACROBLOCKD *xd);
Jingning Han's avatar
Jingning Han committed
99

Yaowu Xu's avatar
Yaowu Xu committed
100 101 102
static INLINE aom_prob av1_get_reference_mode_prob(const AV1_COMMON *cm,
                                                   const MACROBLOCKD *xd) {
  return cm->fc->comp_inter_prob[av1_get_reference_mode_context(cm, xd)];
Jingning Han's avatar
Jingning Han committed
103
}
104 105 106 107 108 109
#if CONFIG_NEW_MULTISYMBOL
static INLINE aom_cdf_prob *av1_get_reference_mode_cdf(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  return xd->tile_ctx->comp_inter_cdf[av1_get_reference_mode_context(cm, xd)];
}
#endif
Jingning Han's avatar
Jingning Han committed
110

Zoe Liu's avatar
Zoe Liu committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#if CONFIG_EXT_COMP_REFS
int av1_get_comp_reference_type_context(const AV1_COMMON *cm,
                                        const MACROBLOCKD *xd);

static INLINE aom_prob av1_get_comp_reference_type_prob(const AV1_COMMON *cm,
                                                        const MACROBLOCKD *xd) {
  return cm->fc
      ->comp_ref_type_prob[av1_get_comp_reference_type_context(cm, xd)];
}

int av1_get_pred_context_uni_comp_ref_p(const AV1_COMMON *cm,
                                        const MACROBLOCKD *xd);

static INLINE aom_prob av1_get_pred_prob_uni_comp_ref_p(const AV1_COMMON *cm,
                                                        const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_uni_comp_ref_p(cm, xd);
  return cm->fc->uni_comp_ref_prob[pred_context][0];
}

int av1_get_pred_context_uni_comp_ref_p1(const AV1_COMMON *cm,
                                         const MACROBLOCKD *xd);

static INLINE aom_prob
av1_get_pred_prob_uni_comp_ref_p1(const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_uni_comp_ref_p1(cm, xd);
  return cm->fc->uni_comp_ref_prob[pred_context][1];
}
#endif  // CONFIG_EXT_COMP_REFS

Yaowu Xu's avatar
Yaowu Xu committed
140 141
int av1_get_pred_context_comp_ref_p(const AV1_COMMON *cm,
                                    const MACROBLOCKD *xd);
Jingning Han's avatar
Jingning Han committed
142

143 144 145 146 147 148 149 150
#if CONFIG_NEW_MULTISYMBOL
static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_ref_p(const AV1_COMMON *cm,
                                                        const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_ref_p(cm, xd);
  return xd->tile_ctx->comp_ref_cdf[pred_context][0];
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
151 152 153
static INLINE aom_prob av1_get_pred_prob_comp_ref_p(const AV1_COMMON *cm,
                                                    const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_ref_p(cm, xd);
154
  return cm->fc->comp_ref_prob[pred_context][0];
Jingning Han's avatar
Jingning Han committed
155 156
}

157
#if CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
158 159
int av1_get_pred_context_comp_ref_p1(const AV1_COMMON *cm,
                                     const MACROBLOCKD *xd);
160

161 162 163 164 165 166 167 168
#if CONFIG_NEW_MULTISYMBOL
static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_ref_p1(
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_ref_p1(cm, xd);
  return xd->tile_ctx->comp_ref_cdf[pred_context][1];
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
169 170 171
static INLINE aom_prob av1_get_pred_prob_comp_ref_p1(const AV1_COMMON *cm,
                                                     const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_ref_p1(cm, xd);
172 173 174
  return cm->fc->comp_ref_prob[pred_context][1];
}

Yaowu Xu's avatar
Yaowu Xu committed
175 176
int av1_get_pred_context_comp_ref_p2(const AV1_COMMON *cm,
                                     const MACROBLOCKD *xd);
177

178 179 180 181 182 183 184 185
#if CONFIG_NEW_MULTISYMBOL
static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_ref_p2(
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_ref_p2(cm, xd);
  return xd->tile_ctx->comp_ref_cdf[pred_context][2];
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
186 187 188
static INLINE aom_prob av1_get_pred_prob_comp_ref_p2(const AV1_COMMON *cm,
                                                     const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_ref_p2(cm, xd);
189 190 191
  return cm->fc->comp_ref_prob[pred_context][2];
}

Yaowu Xu's avatar
Yaowu Xu committed
192 193
int av1_get_pred_context_comp_bwdref_p(const AV1_COMMON *cm,
                                       const MACROBLOCKD *xd);
194

195 196 197 198 199 200 201
#if CONFIG_NEW_MULTISYMBOL
static INLINE aom_cdf_prob *av1_get_pred_cdf_comp_bwdref_p(
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_bwdref_p(cm, xd);
  return xd->tile_ctx->comp_bwdref_cdf[pred_context][0];
}
#endif  // CONFIG_NEW_MULTISYMBOL
Yaowu Xu's avatar
Yaowu Xu committed
202 203 204
static INLINE aom_prob av1_get_pred_prob_comp_bwdref_p(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  const int pred_context = av1_get_pred_context_comp_bwdref_p(cm, xd);
205 206
  return cm->fc->comp_bwdref_prob[pred_context][0];
}
207 208
#endif  // CONFIG_EXT_REFS

Yaowu Xu's avatar
Yaowu Xu committed
209
int av1_get_pred_context_single_ref_p1(const MACROBLOCKD *xd);
Jingning Han's avatar
Jingning Han committed
210

Yaowu Xu's avatar
Yaowu Xu committed
211 212 213
static INLINE aom_prob av1_get_pred_prob_single_ref_p1(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p1(xd)][0];
Jingning Han's avatar
Jingning Han committed
214 215
}

Yaowu Xu's avatar
Yaowu Xu committed
216
int av1_get_pred_context_single_ref_p2(const MACROBLOCKD *xd);
Jingning Han's avatar
Jingning Han committed
217

Yaowu Xu's avatar
Yaowu Xu committed
218 219 220
static INLINE aom_prob av1_get_pred_prob_single_ref_p2(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p2(xd)][1];
Jingning Han's avatar
Jingning Han committed
221 222
}

223
#if CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
224
int av1_get_pred_context_single_ref_p3(const MACROBLOCKD *xd);
225

Yaowu Xu's avatar
Yaowu Xu committed
226 227 228
static INLINE aom_prob av1_get_pred_prob_single_ref_p3(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p3(xd)][2];
229 230
}

Yaowu Xu's avatar
Yaowu Xu committed
231
int av1_get_pred_context_single_ref_p4(const MACROBLOCKD *xd);
232

Yaowu Xu's avatar
Yaowu Xu committed
233 234 235
static INLINE aom_prob av1_get_pred_prob_single_ref_p4(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p4(xd)][3];
236 237
}

Yaowu Xu's avatar
Yaowu Xu committed
238
int av1_get_pred_context_single_ref_p5(const MACROBLOCKD *xd);
239

Yaowu Xu's avatar
Yaowu Xu committed
240 241 242
static INLINE aom_prob av1_get_pred_prob_single_ref_p5(const AV1_COMMON *cm,
                                                       const MACROBLOCKD *xd) {
  return cm->fc->single_ref_prob[av1_get_pred_context_single_ref_p5(xd)][4];
243 244 245
}
#endif  // CONFIG_EXT_REFS

246 247
#if CONFIG_NEW_MULTISYMBOL
static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p1(
248 249
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  (void)cm;
250 251 252 253
  return xd->tile_ctx
      ->single_ref_cdf[av1_get_pred_context_single_ref_p1(xd)][0];
}
static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p2(
254 255
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  (void)cm;
256 257 258
  return xd->tile_ctx
      ->single_ref_cdf[av1_get_pred_context_single_ref_p2(xd)][1];
}
259
#if CONFIG_EXT_REFS
260
static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p3(
261 262
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  (void)cm;
263 264 265 266
  return xd->tile_ctx
      ->single_ref_cdf[av1_get_pred_context_single_ref_p3(xd)][2];
}
static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p4(
267 268
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  (void)cm;
269 270 271 272
  return xd->tile_ctx
      ->single_ref_cdf[av1_get_pred_context_single_ref_p4(xd)][3];
}
static INLINE aom_cdf_prob *av1_get_pred_cdf_single_ref_p5(
273 274
    const AV1_COMMON *cm, const MACROBLOCKD *xd) {
  (void)cm;
275 276 277
  return xd->tile_ctx
      ->single_ref_cdf[av1_get_pred_context_single_ref_p5(xd)][4];
}
278 279
#endif  // CONFIG_EXT_REFS
#endif  // CONFIG_NEW_MULTISYMBOL
280

281 282 283 284 285 286 287 288 289
#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
int av1_get_inter_mode_context(const MACROBLOCKD *xd);

static INLINE aom_prob av1_get_inter_mode_prob(const AV1_COMMON *cm,
                                               const MACROBLOCKD *xd) {
  return cm->fc->comp_inter_mode_prob[av1_get_inter_mode_context(xd)];
}
#endif  // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF

Jingning Han's avatar
Jingning Han committed
290 291 292 293 294 295 296 297 298 299
// Returns a context number for the given MB prediction signal
// The mode info data structure has a one element border above and to the
// left of the entries corresponding to real blocks.
// The prediction flags in these dummy entries are initialized to 0.
static INLINE int get_tx_size_context(const MACROBLOCKD *xd) {
  const int max_tx_size = max_txsize_lookup[xd->mi[0]->mbmi.sb_type];
  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;
clang-format's avatar
clang-format committed
300 301 302 303 304 305
  int above_ctx = (has_above && !above_mbmi->skip)
                      ? (int)txsize_sqr_map[above_mbmi->tx_size]
                      : max_tx_size;
  int left_ctx = (has_left && !left_mbmi->skip)
                     ? (int)txsize_sqr_map[left_mbmi->tx_size]
                     : max_tx_size;
306

clang-format's avatar
clang-format committed
307
  if (!has_left) left_ctx = above_ctx;
Jingning Han's avatar
Jingning Han committed
308

clang-format's avatar
clang-format committed
309
  if (!has_above) above_ctx = left_ctx;
310
  return (above_ctx + left_ctx) > max_tx_size + TX_SIZE_LUMA_MIN;
Jingning Han's avatar
Jingning Han committed
311 312
}

313
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
314
static void update_tx_counts(AV1_COMMON *cm, MACROBLOCKD *xd,
315 316
                             MB_MODE_INFO *mbmi, BLOCK_SIZE plane_bsize,
                             TX_SIZE tx_size, int blk_row, int blk_col,
317
                             TX_SIZE max_tx_size, int ctx) {
318 319
  const struct macroblockd_plane *const pd = &xd->plane[0];
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
320 321 322
  const int tx_row = blk_row >> (1 - pd->subsampling_y);
  const int tx_col = blk_col >> (1 - pd->subsampling_x);
  const TX_SIZE plane_tx_size = mbmi->inter_tx_size[tx_row][tx_col];
323 324
  const int max_blocks_high = max_block_high(xd, plane_bsize, 0);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, 0);
325

clang-format's avatar
clang-format committed
326
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
327 328

  if (tx_size == plane_tx_size) {
329 330 331
    int depth;
    depth = tx_size_to_depth(tx_size);
    ++xd->counts->tx_size[max_tx_size - TX_SIZE_CTX_MIN][ctx][depth];
332
    mbmi->tx_size = tx_size;
333 334 335 336 337 338 339 340 341 342 343
  } else {
    int bsl = b_width_log2_lookup[bsize];
    int i;

    assert(bsl > 0);
    --bsl;

    for (i = 0; i < 4; ++i) {
      const int offsetr = blk_row + ((i >> 1) << bsl);
      const int offsetc = blk_col + ((i & 0x01) << bsl);

clang-format's avatar
clang-format committed
344
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
345 346
      update_tx_counts(cm, xd, mbmi, plane_bsize, (TX_SIZE)(tx_size - 1),
                       offsetr, offsetc, max_tx_size, ctx);
347 348 349 350
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
351
static INLINE void inter_block_tx_count_update(AV1_COMMON *cm, MACROBLOCKD *xd,
352 353
                                               MB_MODE_INFO *mbmi,
                                               BLOCK_SIZE plane_bsize,
354
                                               int ctx) {
355 356
  const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
  const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0];
357
  TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize];
358
  int bh = tx_size_wide_unit[max_tx_size];
359 360 361 362 363
  int idx, idy;

  for (idy = 0; idy < mi_height; idy += bh)
    for (idx = 0; idx < mi_width; idx += bh)
      update_tx_counts(cm, xd, mbmi, plane_bsize, max_tx_size, idy, idx,
364
                       max_tx_size, ctx);
365 366 367
}
#endif

Jingning Han's avatar
Jingning Han committed
368 369 370 371
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
372
#endif  // AV1_COMMON_PRED_COMMON_H_