1/*
2 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef VP9_COMMON_VP9_PRED_COMMON_H_
12#define VP9_COMMON_VP9_PRED_COMMON_H_
13
14#include "vp9/common/vp9_blockd.h"
15#include "vp9/common/vp9_onyxc_int.h"
16#include "vpx_dsp/vpx_dsp_common.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22static INLINE int get_segment_id(const VP9_COMMON *cm,
23                                 const uint8_t *segment_ids, BLOCK_SIZE bsize,
24                                 int mi_row, int mi_col) {
25  const int mi_offset = mi_row * cm->mi_cols + mi_col;
26  const int bw = num_8x8_blocks_wide_lookup[bsize];
27  const int bh = num_8x8_blocks_high_lookup[bsize];
28  const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
29  const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
30  int x, y, segment_id = MAX_SEGMENTS;
31
32  for (y = 0; y < ymis; ++y)
33    for (x = 0; x < xmis; ++x)
34      segment_id =
35          VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
36
37  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
38  return segment_id;
39}
40
41static INLINE int vp9_get_pred_context_seg_id(const MACROBLOCKD *xd) {
42  const MODE_INFO *const above_mi = xd->above_mi;
43  const MODE_INFO *const left_mi = xd->left_mi;
44  const int above_sip = (above_mi != NULL) ? above_mi->seg_id_predicted : 0;
45  const int left_sip = (left_mi != NULL) ? left_mi->seg_id_predicted : 0;
46
47  return above_sip + left_sip;
48}
49
50static INLINE vpx_prob vp9_get_pred_prob_seg_id(const struct segmentation *seg,
51                                                const MACROBLOCKD *xd) {
52  return seg->pred_probs[vp9_get_pred_context_seg_id(xd)];
53}
54
55static INLINE int vp9_get_skip_context(const MACROBLOCKD *xd) {
56  const MODE_INFO *const above_mi = xd->above_mi;
57  const MODE_INFO *const left_mi = xd->left_mi;
58  const int above_skip = (above_mi != NULL) ? above_mi->skip : 0;
59  const int left_skip = (left_mi != NULL) ? left_mi->skip : 0;
60  return above_skip + left_skip;
61}
62
63static INLINE vpx_prob vp9_get_skip_prob(const VP9_COMMON *cm,
64                                         const MACROBLOCKD *xd) {
65  return cm->fc->skip_probs[vp9_get_skip_context(xd)];
66}
67
68// Returns a context number for the given MB prediction signal
69static INLINE int get_pred_context_switchable_interp(const MACROBLOCKD *xd) {
70  // Note:
71  // The mode info data structure has a one element border above and to the
72  // left of the entries corresponding to real macroblocks.
73  // The prediction flags in these dummy entries are initialized to 0.
74  const MODE_INFO *const left_mi = xd->left_mi;
75  const int left_type = left_mi ? left_mi->interp_filter : SWITCHABLE_FILTERS;
76  const MODE_INFO *const above_mi = xd->above_mi;
77  const int above_type =
78      above_mi ? above_mi->interp_filter : SWITCHABLE_FILTERS;
79
80  if (left_type == above_type)
81    return left_type;
82  else if (left_type == SWITCHABLE_FILTERS)
83    return above_type;
84  else if (above_type == SWITCHABLE_FILTERS)
85    return left_type;
86  else
87    return SWITCHABLE_FILTERS;
88}
89
90// The mode info data structure has a one element border above and to the
91// left of the entries corresponding to real macroblocks.
92// The prediction flags in these dummy entries are initialized to 0.
93// 0 - inter/inter, inter/--, --/inter, --/--
94// 1 - intra/inter, inter/intra
95// 2 - intra/--, --/intra
96// 3 - intra/intra
97static INLINE int get_intra_inter_context(const MACROBLOCKD *xd) {
98  const MODE_INFO *const above_mi = xd->above_mi;
99  const MODE_INFO *const left_mi = xd->left_mi;
100  const int has_above = !!above_mi;
101  const int has_left = !!left_mi;
102
103  if (has_above && has_left) {  // both edges available
104    const int above_intra = !is_inter_block(above_mi);
105    const int left_intra = !is_inter_block(left_mi);
106    return left_intra && above_intra ? 3 : left_intra || above_intra;
107  } else if (has_above || has_left) {  // one edge available
108    return 2 * !is_inter_block(has_above ? above_mi : left_mi);
109  }
110  return 0;
111}
112
113static INLINE vpx_prob vp9_get_intra_inter_prob(const VP9_COMMON *cm,
114                                                const MACROBLOCKD *xd) {
115  return cm->fc->intra_inter_prob[get_intra_inter_context(xd)];
116}
117
118int vp9_get_reference_mode_context(const VP9_COMMON *cm, const MACROBLOCKD *xd);
119
120static INLINE vpx_prob vp9_get_reference_mode_prob(const VP9_COMMON *cm,
121                                                   const MACROBLOCKD *xd) {
122  return cm->fc->comp_inter_prob[vp9_get_reference_mode_context(cm, xd)];
123}
124
125int vp9_get_pred_context_comp_ref_p(const VP9_COMMON *cm,
126                                    const MACROBLOCKD *xd);
127
128static INLINE vpx_prob vp9_get_pred_prob_comp_ref_p(const VP9_COMMON *cm,
129                                                    const MACROBLOCKD *xd) {
130  const int pred_context = vp9_get_pred_context_comp_ref_p(cm, xd);
131  return cm->fc->comp_ref_prob[pred_context];
132}
133
134int vp9_get_pred_context_single_ref_p1(const MACROBLOCKD *xd);
135
136static INLINE vpx_prob vp9_get_pred_prob_single_ref_p1(const VP9_COMMON *cm,
137                                                       const MACROBLOCKD *xd) {
138  return cm->fc->single_ref_prob[vp9_get_pred_context_single_ref_p1(xd)][0];
139}
140
141int vp9_get_pred_context_single_ref_p2(const MACROBLOCKD *xd);
142
143static INLINE vpx_prob vp9_get_pred_prob_single_ref_p2(const VP9_COMMON *cm,
144                                                       const MACROBLOCKD *xd) {
145  return cm->fc->single_ref_prob[vp9_get_pred_context_single_ref_p2(xd)][1];
146}
147
148// Returns a context number for the given MB prediction signal
149// The mode info data structure has a one element border above and to the
150// left of the entries corresponding to real blocks.
151// The prediction flags in these dummy entries are initialized to 0.
152static INLINE int get_tx_size_context(const MACROBLOCKD *xd) {
153  const int max_tx_size = max_txsize_lookup[xd->mi[0]->sb_type];
154  const MODE_INFO *const above_mi = xd->above_mi;
155  const MODE_INFO *const left_mi = xd->left_mi;
156  const int has_above = !!above_mi;
157  const int has_left = !!left_mi;
158  int above_ctx =
159      (has_above && !above_mi->skip) ? (int)above_mi->tx_size : max_tx_size;
160  int left_ctx =
161      (has_left && !left_mi->skip) ? (int)left_mi->tx_size : max_tx_size;
162  if (!has_left) left_ctx = above_ctx;
163
164  if (!has_above) above_ctx = left_ctx;
165
166  return (above_ctx + left_ctx) > max_tx_size;
167}
168
169static INLINE const vpx_prob *get_tx_probs(TX_SIZE max_tx_size, int ctx,
170                                           const struct tx_probs *tx_probs) {
171  switch (max_tx_size) {
172    case TX_8X8: return tx_probs->p8x8[ctx];
173    case TX_16X16: return tx_probs->p16x16[ctx];
174    case TX_32X32: return tx_probs->p32x32[ctx];
175    default: assert(0 && "Invalid max_tx_size."); return NULL;
176  }
177}
178
179static INLINE const vpx_prob *get_tx_probs2(TX_SIZE max_tx_size,
180                                            const MACROBLOCKD *xd,
181                                            const struct tx_probs *tx_probs) {
182  return get_tx_probs(max_tx_size, get_tx_size_context(xd), tx_probs);
183}
184
185static INLINE unsigned int *get_tx_counts(TX_SIZE max_tx_size, int ctx,
186                                          struct tx_counts *tx_counts) {
187  switch (max_tx_size) {
188    case TX_8X8: return tx_counts->p8x8[ctx];
189    case TX_16X16: return tx_counts->p16x16[ctx];
190    case TX_32X32: return tx_counts->p32x32[ctx];
191    default: assert(0 && "Invalid max_tx_size."); return NULL;
192  }
193}
194
195#ifdef __cplusplus
196}  // extern "C"
197#endif
198
199#endif  // VP9_COMMON_VP9_PRED_COMMON_H_
200