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