blob: 385a3e1fa3f408f9b387994712eca6221108e8b6 [file] [log] [blame]
Jingning Han3ee6db62015-08-05 19:00:31 -07001/*
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
Yaowu Xu2dcefd92015-08-13 09:25:39 -070011#ifndef VP10_COMMON_PRED_COMMON_H_
12#define VP10_COMMON_PRED_COMMON_H_
Jingning Han3ee6db62015-08-05 19:00:31 -070013
Jingning Han54d66ef2015-08-06 21:14:07 -070014#include "vp10/common/blockd.h"
15#include "vp10/common/onyxc_int.h"
Johannc5f11912015-08-31 14:36:35 -070016#include "vpx_dsp/vpx_dsp_common.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
Yaowu Xufc7cbd12015-08-13 09:36:53 -070022static INLINE int get_segment_id(const VP10_COMMON *cm,
Jingning Han3ee6db62015-08-05 19:00:31 -070023 const uint8_t *segment_ids,
24 BLOCK_SIZE bsize, 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];
James Zern5e16d392015-08-17 18:19:22 -070028 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
29 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
Jingning Han3ee6db62015-08-05 19:00:31 -070030 int x, y, segment_id = MAX_SEGMENTS;
31
32 for (y = 0; y < ymis; ++y)
33 for (x = 0; x < xmis; ++x)
James Zern5e16d392015-08-17 18:19:22 -070034 segment_id =
35 VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han3ee6db62015-08-05 19:00:31 -070036
37 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
38 return segment_id;
39}
40
41static INLINE int vp10_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) ?
45 above_mi->mbmi.seg_id_predicted : 0;
46 const int left_sip = (left_mi != NULL) ? left_mi->mbmi.seg_id_predicted : 0;
47
48 return above_sip + left_sip;
49}
50
Ronald S. Bultje6e5a1162015-10-13 14:06:28 -040051static INLINE vpx_prob vp10_get_pred_prob_seg_id(
52 const struct segmentation_probs *segp, const MACROBLOCKD *xd) {
53 return segp->pred_probs[vp10_get_pred_context_seg_id(xd)];
Jingning Han3ee6db62015-08-05 19:00:31 -070054}
55
56static INLINE int vp10_get_skip_context(const MACROBLOCKD *xd) {
57 const MODE_INFO *const above_mi = xd->above_mi;
58 const MODE_INFO *const left_mi = xd->left_mi;
59 const int above_skip = (above_mi != NULL) ? above_mi->mbmi.skip : 0;
60 const int left_skip = (left_mi != NULL) ? left_mi->mbmi.skip : 0;
61 return above_skip + left_skip;
62}
63
Yaowu Xufc7cbd12015-08-13 09:36:53 -070064static INLINE vpx_prob vp10_get_skip_prob(const VP10_COMMON *cm,
Jingning Han3ee6db62015-08-05 19:00:31 -070065 const MACROBLOCKD *xd) {
66 return cm->fc->skip_probs[vp10_get_skip_context(xd)];
67}
68
69int vp10_get_pred_context_switchable_interp(const MACROBLOCKD *xd);
70
hui su3b1c7662016-01-12 16:38:58 -080071#if CONFIG_EXT_INTRA
72int vp10_get_pred_context_intra_interp(const MACROBLOCKD *xd);
73#endif // CONFIG_EXT_INTRA
74
Jingning Han3ee6db62015-08-05 19:00:31 -070075int vp10_get_intra_inter_context(const MACROBLOCKD *xd);
76
Yaowu Xufc7cbd12015-08-13 09:36:53 -070077static INLINE vpx_prob vp10_get_intra_inter_prob(const VP10_COMMON *cm,
Jingning Han3ee6db62015-08-05 19:00:31 -070078 const MACROBLOCKD *xd) {
79 return cm->fc->intra_inter_prob[vp10_get_intra_inter_context(xd)];
80}
81
Yaowu Xufc7cbd12015-08-13 09:36:53 -070082int vp10_get_reference_mode_context(const VP10_COMMON *cm,
83 const MACROBLOCKD *xd);
Jingning Han3ee6db62015-08-05 19:00:31 -070084
Yaowu Xufc7cbd12015-08-13 09:36:53 -070085static INLINE vpx_prob vp10_get_reference_mode_prob(const VP10_COMMON *cm,
Jingning Han3ee6db62015-08-05 19:00:31 -070086 const MACROBLOCKD *xd) {
87 return cm->fc->comp_inter_prob[vp10_get_reference_mode_context(cm, xd)];
88}
89
Yaowu Xufc7cbd12015-08-13 09:36:53 -070090int vp10_get_pred_context_comp_ref_p(const VP10_COMMON *cm,
Jingning Han3ee6db62015-08-05 19:00:31 -070091 const MACROBLOCKD *xd);
92
Yaowu Xufc7cbd12015-08-13 09:36:53 -070093static INLINE vpx_prob vp10_get_pred_prob_comp_ref_p(const VP10_COMMON *cm,
Zoe Liu3ec16012015-11-12 02:12:17 -080094 const MACROBLOCKD *xd) {
Jingning Han3ee6db62015-08-05 19:00:31 -070095 const int pred_context = vp10_get_pred_context_comp_ref_p(cm, xd);
Zoe Liu3ec16012015-11-12 02:12:17 -080096 return cm->fc->comp_ref_prob[pred_context][0];
Jingning Han3ee6db62015-08-05 19:00:31 -070097}
98
Zoe Liu3ec16012015-11-12 02:12:17 -080099#if CONFIG_EXT_REFS
100int vp10_get_pred_context_comp_ref_p1(const VP10_COMMON *cm,
101 const MACROBLOCKD *xd);
102
103static INLINE vpx_prob vp10_get_pred_prob_comp_ref_p1(const VP10_COMMON *cm,
104 const MACROBLOCKD *xd) {
105 const int pred_context = vp10_get_pred_context_comp_ref_p1(cm, xd);
106 return cm->fc->comp_ref_prob[pred_context][1];
107}
108
109int vp10_get_pred_context_comp_ref_p2(const VP10_COMMON *cm,
110 const MACROBLOCKD *xd);
111
112static INLINE vpx_prob vp10_get_pred_prob_comp_ref_p2(const VP10_COMMON *cm,
113 const MACROBLOCKD *xd) {
114 const int pred_context = vp10_get_pred_context_comp_ref_p2(cm, xd);
115 return cm->fc->comp_ref_prob[pred_context][2];
116}
117
118int vp10_get_pred_context_comp_ref_p3(const VP10_COMMON *cm,
119 const MACROBLOCKD *xd);
120
121static INLINE vpx_prob vp10_get_pred_prob_comp_ref_p3(const VP10_COMMON *cm,
122 const MACROBLOCKD *xd) {
123 const int pred_context = vp10_get_pred_context_comp_ref_p3(cm, xd);
124 return cm->fc->comp_ref_prob[pred_context][3];
125}
126#endif // CONFIG_EXT_REFS
127
Jingning Han3ee6db62015-08-05 19:00:31 -0700128int vp10_get_pred_context_single_ref_p1(const MACROBLOCKD *xd);
129
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700130static INLINE vpx_prob vp10_get_pred_prob_single_ref_p1(const VP10_COMMON *cm,
Zoe Liu3ec16012015-11-12 02:12:17 -0800131 const MACROBLOCKD *xd) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700132 return cm->fc->single_ref_prob[vp10_get_pred_context_single_ref_p1(xd)][0];
133}
134
135int vp10_get_pred_context_single_ref_p2(const MACROBLOCKD *xd);
136
Yaowu Xufc7cbd12015-08-13 09:36:53 -0700137static INLINE vpx_prob vp10_get_pred_prob_single_ref_p2(const VP10_COMMON *cm,
Zoe Liu3ec16012015-11-12 02:12:17 -0800138 const MACROBLOCKD *xd) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700139 return cm->fc->single_ref_prob[vp10_get_pred_context_single_ref_p2(xd)][1];
140}
141
Zoe Liu3ec16012015-11-12 02:12:17 -0800142#if CONFIG_EXT_REFS
143int vp10_get_pred_context_single_ref_p3(const MACROBLOCKD *xd);
144
145static INLINE vpx_prob vp10_get_pred_prob_single_ref_p3(const VP10_COMMON *cm,
146 const MACROBLOCKD *xd) {
147 return cm->fc->single_ref_prob[vp10_get_pred_context_single_ref_p3(xd)][2];
148}
149
150int vp10_get_pred_context_single_ref_p4(const MACROBLOCKD *xd);
151
152static INLINE vpx_prob vp10_get_pred_prob_single_ref_p4(const VP10_COMMON *cm,
153 const MACROBLOCKD *xd) {
154 return cm->fc->single_ref_prob[vp10_get_pred_context_single_ref_p4(xd)][3];
155}
156
157int vp10_get_pred_context_single_ref_p5(const MACROBLOCKD *xd);
158
159static INLINE vpx_prob vp10_get_pred_prob_single_ref_p5(const VP10_COMMON *cm,
160 const MACROBLOCKD *xd) {
161 return cm->fc->single_ref_prob[vp10_get_pred_context_single_ref_p5(xd)][4];
162}
163#endif // CONFIG_EXT_REFS
164
Jingning Han3ee6db62015-08-05 19:00:31 -0700165// Returns a context number for the given MB prediction signal
166// The mode info data structure has a one element border above and to the
167// left of the entries corresponding to real blocks.
168// The prediction flags in these dummy entries are initialized to 0.
169static INLINE int get_tx_size_context(const MACROBLOCKD *xd) {
170 const int max_tx_size = max_txsize_lookup[xd->mi[0]->mbmi.sb_type];
171 const MB_MODE_INFO *const above_mbmi = xd->above_mbmi;
172 const MB_MODE_INFO *const left_mbmi = xd->left_mbmi;
173 const int has_above = xd->up_available;
174 const int has_left = xd->left_available;
175 int above_ctx = (has_above && !above_mbmi->skip) ? (int)above_mbmi->tx_size
176 : max_tx_size;
177 int left_ctx = (has_left && !left_mbmi->skip) ? (int)left_mbmi->tx_size
178 : max_tx_size;
179 if (!has_left)
180 left_ctx = above_ctx;
181
182 if (!has_above)
183 above_ctx = left_ctx;
184
185 return (above_ctx + left_ctx) > max_tx_size;
186}
187
Jingning Han2cdc1272015-10-09 09:57:42 -0700188#if CONFIG_VAR_TX
189static void update_tx_counts(VP10_COMMON *cm, MACROBLOCKD *xd,
190 MB_MODE_INFO *mbmi, BLOCK_SIZE plane_bsize,
191 TX_SIZE tx_size, int blk_row, int blk_col,
hui su954e5602016-03-07 15:25:50 -0800192 TX_SIZE max_tx_size, int ctx) {
Jingning Han2cdc1272015-10-09 09:57:42 -0700193 const struct macroblockd_plane *const pd = &xd->plane[0];
194 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
Geza Loreefe7d4e2016-03-16 18:03:57 +0000195 const int tx_row = blk_row >> (1 - pd->subsampling_y);
196 const int tx_col = blk_col >> (1 - pd->subsampling_x);
197 const TX_SIZE plane_tx_size = mbmi->inter_tx_size[tx_row][tx_col];
Jingning Han2cdc1272015-10-09 09:57:42 -0700198 int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize];
199 int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize];
200
201 if (xd->mb_to_bottom_edge < 0)
202 max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y);
203 if (xd->mb_to_right_edge < 0)
204 max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x);
205
206 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide)
207 return;
208
209 if (tx_size == plane_tx_size) {
hui su954e5602016-03-07 15:25:50 -0800210 ++xd->counts->tx_size[max_tx_size - TX_8X8][ctx][tx_size];
Jingning Han3a279612015-10-12 19:20:58 -0700211 mbmi->tx_size = tx_size;
Jingning Han2cdc1272015-10-09 09:57:42 -0700212 } else {
213 int bsl = b_width_log2_lookup[bsize];
214 int i;
215
216 assert(bsl > 0);
217 --bsl;
218
219 for (i = 0; i < 4; ++i) {
220 const int offsetr = blk_row + ((i >> 1) << bsl);
221 const int offsetc = blk_col + ((i & 0x01) << bsl);
222
223 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide)
224 continue;
225 update_tx_counts(cm, xd, mbmi, plane_bsize,
hui su954e5602016-03-07 15:25:50 -0800226 tx_size - 1, offsetr, offsetc, max_tx_size, ctx);
Jingning Han2cdc1272015-10-09 09:57:42 -0700227 }
228 }
229}
230
231static INLINE void inter_block_tx_count_update(VP10_COMMON *cm,
232 MACROBLOCKD *xd,
233 MB_MODE_INFO *mbmi,
234 BLOCK_SIZE plane_bsize,
hui su954e5602016-03-07 15:25:50 -0800235 int ctx) {
Jingning Han2cdc1272015-10-09 09:57:42 -0700236 const int mi_width = num_4x4_blocks_wide_lookup[plane_bsize];
237 const int mi_height = num_4x4_blocks_high_lookup[plane_bsize];
238 TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize];
239 BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
240 int bh = num_4x4_blocks_wide_lookup[txb_size];
241 int idx, idy;
242
243 for (idy = 0; idy < mi_height; idy += bh)
244 for (idx = 0; idx < mi_width; idx += bh)
245 update_tx_counts(cm, xd, mbmi, plane_bsize, max_tx_size, idy, idx,
hui su954e5602016-03-07 15:25:50 -0800246 max_tx_size, ctx);
Jingning Han2cdc1272015-10-09 09:57:42 -0700247}
248#endif
249
Jingning Han3ee6db62015-08-05 19:00:31 -0700250#ifdef __cplusplus
251} // extern "C"
252#endif
253
Yaowu Xu2dcefd92015-08-13 09:25:39 -0700254#endif // VP10_COMMON_PRED_COMMON_H_