blob: 4591cc9399ca172b1c51ff2a45a66455ab30e5e4 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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 Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13
14#include "av1/common/common.h"
15#include "av1/common/entropy.h"
16#include "av1/common/entropymode.h"
17#include "av1/common/entropymv.h"
18#include "av1/common/mvref_common.h"
19#include "av1/common/pred_common.h"
20#include "av1/common/reconinter.h"
hui su45dc5972016-12-08 17:42:50 -080021#if CONFIG_EXT_INTRA
22#include "av1/common/reconintra.h"
23#endif // CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "av1/common/seg_common.h"
Yue Chen69f18e12016-09-08 14:48:15 -070025#if CONFIG_WARPED_MOTION
26#include "av1/common/warped_motion.h"
27#endif // CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#include "av1/decoder/decodeframe.h"
Jingning Han1aab8182016-06-03 11:09:06 -070030#include "av1/decoder/decodemv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031
Yaowu Xuf883b422016-08-30 14:01:10 -070032#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033
Michael Bebenita6048d052016-08-25 14:40:54 -070034#define ACCT_STR __func__
Zoe Liu85b66462017-04-20 14:28:19 -070035
Di Chen56586622017-06-09 13:49:44 -070036#define DEC_MISMATCH_DEBUG 0
Zoe Liu85b66462017-04-20 14:28:19 -070037
Thomas9ac55082016-09-23 18:04:17 +010038static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
Nathan E. Egge3ef926e2016-09-07 18:20:41 -040039 return (PREDICTION_MODE)
40 av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
41}
Yaowu Xuc27fc142016-08-22 16:08:15 -070042
Thomas Daviesf6936102016-09-05 16:51:31 +010043static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
44 MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
45 FRAME_COUNTS *counts = xd->counts;
46 int sign, abs, reduced_delta_qindex = 0;
47 BLOCK_SIZE bsize = mbmi->sb_type;
48 const int b_col = mi_col & MAX_MIB_MASK;
49 const int b_row = mi_row & MAX_MIB_MASK;
50 const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000051 int rem_bits, thr;
52 int i, smallval;
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000053 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
54 (void)cm;
Thomas Daviesf6936102016-09-05 16:51:31 +010055
Alex Converse68abef82017-03-23 14:50:33 -070056 if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000057 abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000058 smallval = (abs < DELTA_Q_SMALL);
59 if (counts) {
60 for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
61 if (smallval) counts->delta_q[abs][0]++;
62 }
63
64 if (!smallval) {
Thomas Davies3b93e8e2017-09-20 09:59:07 +010065 rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
Thomas Daviesf6936102016-09-05 16:51:31 +010066 thr = (1 << rem_bits) + 1;
67 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
68 }
69
70 if (abs) {
71 sign = aom_read_bit(r, ACCT_STR);
72 } else {
73 sign = 1;
74 }
75
76 reduced_delta_qindex = sign ? -abs : abs;
77 }
78 return reduced_delta_qindex;
79}
Fangwen Fu231fe422017-04-24 17:52:29 -070080#if CONFIG_EXT_DELTA_Q
81static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
82 MB_MODE_INFO *const mbmi, int mi_col,
83 int mi_row) {
84 FRAME_COUNTS *counts = xd->counts;
85 int sign, abs, reduced_delta_lflevel = 0;
86 BLOCK_SIZE bsize = mbmi->sb_type;
87 const int b_col = mi_col & MAX_MIB_MASK;
88 const int b_row = mi_row & MAX_MIB_MASK;
89 const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
90 int rem_bits, thr;
91 int i, smallval;
Fangwen Fu231fe422017-04-24 17:52:29 -070092 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
93 (void)cm;
Fangwen Fu231fe422017-04-24 17:52:29 -070094
95 if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_lf_flag) {
Fangwen Fu231fe422017-04-24 17:52:29 -070096 abs =
97 aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
Fangwen Fu231fe422017-04-24 17:52:29 -070098 smallval = (abs < DELTA_LF_SMALL);
99 if (counts) {
100 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
101 if (smallval) counts->delta_lf[abs][0]++;
102 }
103 if (!smallval) {
Thomas Davies3b93e8e2017-09-20 09:59:07 +0100104 rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
Fangwen Fu231fe422017-04-24 17:52:29 -0700105 thr = (1 << rem_bits) + 1;
106 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
107 }
108
109 if (abs) {
110 sign = aom_read_bit(r, ACCT_STR);
111 } else {
112 sign = 1;
113 }
114
115 reduced_delta_lflevel = sign ? -abs : abs;
116 }
117 return reduced_delta_lflevel;
118}
119#endif
Thomas Daviesf6936102016-09-05 16:51:31 +0100120
Nathan E. Eggea1f80e32017-05-23 11:52:32 -0400121static PREDICTION_MODE read_intra_mode_y(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700122 aom_reader *r, int size_group) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123 const PREDICTION_MODE y_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000124 read_intra_mode(r, ec_ctx->y_mode_cdf[size_group]);
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400125#if CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 FRAME_COUNTS *counts = xd->counts;
127 if (counts) ++counts->y_mode[size_group][y_mode];
Nathan E. Egge5bb3a742017-06-30 12:47:43 -0400128#else
129 /* TODO(negge): Can we remove this parameter? */
130 (void)xd;
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400131#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132 return y_mode;
133}
134
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400135static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
136 MACROBLOCKD *xd, aom_reader *r,
137 PREDICTION_MODE y_mode) {
138 const UV_PREDICTION_MODE uv_mode =
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400139#if CONFIG_CFL
140 aom_read_symbol(r, ec_ctx->uv_mode_cdf[y_mode], UV_INTRA_MODES, ACCT_STR);
141#else
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000142 read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400143#endif // CONFIG_CFL
144
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400145#if CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700146 FRAME_COUNTS *counts = xd->counts;
147 if (counts) ++counts->uv_mode[y_mode][uv_mode];
Nathan E. Egge5bb3a742017-06-30 12:47:43 -0400148#else
149 /* TODO(negge): Can we remove this parameter? */
150 (void)xd;
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400151#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 return uv_mode;
153}
154
Luc Trudeauf5334002017-04-25 12:21:26 -0400155#if CONFIG_CFL
David Michael Barr23198662017-06-19 23:19:48 +0900156static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
David Michael Barrf6eaa152017-07-19 19:42:28 +0900157 int *signs_out) {
158 const int joint_sign =
159 aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
160 int idx = 0;
161 // Magnitudes are only coded for nonzero values
162 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
163 aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
164 idx = aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
165 << CFL_ALPHABET_SIZE_LOG2;
166 }
167 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
168 aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
169 idx += aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
170 }
171 *signs_out = joint_sign;
172 return idx;
Luc Trudeauf5334002017-04-25 12:21:26 -0400173}
174#endif
175
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200176#if CONFIG_INTERINTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700177static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
178 aom_reader *r, int size_group) {
Thomas Davies299ff042017-06-27 13:41:59 +0100179 (void)cm;
180 const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
181 r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
182 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 FRAME_COUNTS *counts = xd->counts;
184 if (counts) ++counts->interintra_mode[size_group][ii_mode];
185 return ii_mode;
186}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200187#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188
Thomas Davies1de6c882017-01-11 17:47:49 +0000189static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700190 aom_reader *r, int16_t ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191 FRAME_COUNTS *counts = xd->counts;
192 int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100193 int is_newmv, is_zeromv, is_refmv;
194#if CONFIG_NEW_MULTISYMBOL
195 is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
196#else
197 is_newmv = aom_read(r, ec_ctx->newmv_prob[mode_ctx], ACCT_STR) == 0;
198#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199
Thomas Davies149eda52017-06-12 18:11:55 +0100200 if (is_newmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201 if (counts) ++counts->newmv_mode[mode_ctx][0];
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700202 return NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 }
204 if (counts) ++counts->newmv_mode[mode_ctx][1];
205
206 if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
207
208 mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
209
Thomas Davies149eda52017-06-12 18:11:55 +0100210#if CONFIG_NEW_MULTISYMBOL
211 is_zeromv =
212 aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
213#else
214 is_zeromv = aom_read(r, ec_ctx->zeromv_prob[mode_ctx], ACCT_STR) == 0;
215#endif
216 if (is_zeromv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 if (counts) ++counts->zeromv_mode[mode_ctx][0];
218 return ZEROMV;
219 }
220 if (counts) ++counts->zeromv_mode[mode_ctx][1];
221
222 mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
223
224 if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
225 if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
226 if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
227
Thomas Davies149eda52017-06-12 18:11:55 +0100228#if CONFIG_NEW_MULTISYMBOL
229 is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
230#else
231 is_refmv = aom_read(r, ec_ctx->refmv_prob[mode_ctx], ACCT_STR) == 0;
232#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233
Thomas Davies149eda52017-06-12 18:11:55 +0100234 if (is_refmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235 if (counts) ++counts->refmv_mode[mode_ctx][0];
236
237 return NEARESTMV;
238 } else {
239 if (counts) ++counts->refmv_mode[mode_ctx][1];
240 return NEARMV;
241 }
242
243 // Invalid prediction mode.
244 assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245}
246
Thomas Davies149eda52017-06-12 18:11:55 +0100247static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 MB_MODE_INFO *mbmi, aom_reader *r) {
249 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 mbmi->ref_mv_idx = 0;
251
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200252 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700253#if CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700254 || mbmi->mode == SR_NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700255#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700256 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 int idx;
258 for (idx = 0; idx < 2; ++idx) {
259 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700260 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100261#if CONFIG_NEW_MULTISYMBOL
262 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
263#else
264 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
265#endif
266 mbmi->ref_mv_idx = idx + drl_idx;
267 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
268 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 }
270 }
271 }
272
David Barker3dfba992017-04-03 16:10:09 +0100273 if (have_nearmv_in_inter_mode(mbmi->mode)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 int idx;
275 // Offset the NEARESTMV mode.
276 // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
277 // mode is factored in.
278 for (idx = 1; idx < 3; ++idx) {
279 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700280 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100281#if CONFIG_NEW_MULTISYMBOL
282 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
283#else
284 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
285#endif
286 mbmi->ref_mv_idx = idx + drl_idx - 1;
287 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
288 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 }
290 }
291 }
292}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293
Yaowu Xub24e1152016-10-31 16:28:32 -0700294#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
295static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -0800296 MODE_INFO *mi, aom_reader *r) {
297 MB_MODE_INFO *mbmi = &mi->mbmi;
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700298#if !CONFIG_MOTION_VAR || !CONFIG_WARPED_MOTION || CONFIG_NEW_MULTISYMBOL || \
299 CONFIG_NCOBMC_ADAPT_WEIGHT
Thomas Davies04e5aa72017-06-28 14:36:39 +0100300 (void)cm;
301#endif
302
Sarah Parker19234cc2017-03-10 16:43:25 -0800303 const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
Sarah Parker0eea89f2017-07-11 11:56:36 -0700304#if CONFIG_GLOBAL_MOTION
Sarah Parker19234cc2017-03-10 16:43:25 -0800305 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700306#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -0700307#if CONFIG_WARPED_MOTION
308 xd,
309#endif
Sarah Parker19234cc2017-03-10 16:43:25 -0800310 mi);
Yue Chen69f18e12016-09-08 14:48:15 -0700311 int motion_mode;
312 FRAME_COUNTS *counts = xd->counts;
Yaowu Xub24e1152016-10-31 16:28:32 -0700313
Yue Chen69f18e12016-09-08 14:48:15 -0700314 if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
315#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700316#if CONFIG_NCOBMC_ADAPT_WEIGHT
317 if (last_motion_mode_allowed == NCOBMC_ADAPT_WEIGHT) {
318 motion_mode = aom_read_symbol(r, xd->tile_ctx->ncobmc_cdf[mbmi->sb_type],
319 OBMC_FAMILY_MODES, ACCT_STR);
320 if (counts) ++counts->ncobmc[mbmi->sb_type][motion_mode];
321 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
322 } else if (last_motion_mode_allowed == OBMC_CAUSAL) {
323 motion_mode =
324 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
325 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
326 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
327 } else {
328#else
Yue Chen69f18e12016-09-08 14:48:15 -0700329 if (last_motion_mode_allowed == OBMC_CAUSAL) {
Thomas Daviesd9b57262017-06-27 17:43:25 +0100330#if CONFIG_NEW_MULTISYMBOL
331 motion_mode =
332 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
333#else
Yue Chen69f18e12016-09-08 14:48:15 -0700334 motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
Thomas Daviesd9b57262017-06-27 17:43:25 +0100335#endif
Yue Chen69f18e12016-09-08 14:48:15 -0700336 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
337 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
338 } else {
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700339#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yue Chen69f18e12016-09-08 14:48:15 -0700340#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700341 motion_mode =
Thomas Davies04e5aa72017-06-28 14:36:39 +0100342 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
343 MOTION_MODES, ACCT_STR);
Yaowu Xub24e1152016-10-31 16:28:32 -0700344 if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
345 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yue Chen69f18e12016-09-08 14:48:15 -0700346#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700347 }
Yue Chen69f18e12016-09-08 14:48:15 -0700348#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700349}
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700350
351#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -0700352static void read_ncobmc_mode(MACROBLOCKD *xd, MODE_INFO *mi,
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700353 NCOBMC_MODE ncobmc_mode[2], aom_reader *r) {
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700354 MB_MODE_INFO *mbmi = &mi->mbmi;
355 FRAME_COUNTS *counts = xd->counts;
356 ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];
Wei-Ting Lin77c41182017-07-12 15:58:35 -0700357 if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) return;
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700358
Wei-Ting Linca710d62017-07-13 11:41:02 -0700359 ncobmc_mode[0] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
360 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700361 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0]];
362
363 if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
Wei-Ting Linca710d62017-07-13 11:41:02 -0700364 ncobmc_mode[1] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
365 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700366 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1]];
367 }
368}
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700369#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yaowu Xub24e1152016-10-31 16:28:32 -0700370#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
371
Yaowu Xuf883b422016-08-30 14:01:10 -0700372static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
373 aom_reader *r, int16_t ctx) {
Thomas Davies8c08a332017-06-26 17:30:34 +0100374 (void)cm;
375 const int mode =
376 aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
377 INTER_COMPOUND_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700378 FRAME_COUNTS *counts = xd->counts;
379
380 if (counts) ++counts->inter_compound_mode[ctx][mode];
381
382 assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
383 return NEAREST_NEARESTMV + mode;
384}
Zoe Liu85b66462017-04-20 14:28:19 -0700385
386#if CONFIG_COMPOUND_SINGLEREF
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100387static PREDICTION_MODE read_inter_singleref_comp_mode(MACROBLOCKD *xd,
Zoe Liu85b66462017-04-20 14:28:19 -0700388 aom_reader *r,
389 int16_t ctx) {
390 const int mode =
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100391 aom_read_symbol(r, xd->tile_ctx->inter_singleref_comp_mode_cdf[ctx],
392 INTER_SINGLEREF_COMP_MODES, ACCT_STR);
Zoe Liu85b66462017-04-20 14:28:19 -0700393 FRAME_COUNTS *counts = xd->counts;
394
395 if (counts) ++counts->inter_singleref_comp_mode[ctx][mode];
396
397 assert(is_inter_singleref_comp_mode(SR_NEAREST_NEARMV + mode));
398 return SR_NEAREST_NEARMV + mode;
399}
400#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401
Thomas9ac55082016-09-23 18:04:17 +0100402static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700403 return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404}
405
406#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700407static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408 MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700409 TX_SIZE tx_size, int depth, int blk_row,
410 int blk_col, aom_reader *r) {
Thomas Davies985bfc32017-06-27 16:51:26 +0100411#if CONFIG_NEW_MULTISYMBOL
412 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
413 (void)cm;
414#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415 int is_split = 0;
416 const int tx_row = blk_row >> 1;
417 const int tx_col = blk_col >> 1;
Jingning Hanf64062f2016-11-02 16:22:18 -0700418 const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
419 const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
Jingning Han331662e2017-05-30 17:03:32 -0700420 int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
421 xd->left_txfm_context + blk_row,
Jingning Hanc8b89362016-11-01 10:28:53 -0700422 mbmi->sb_type, tx_size);
clang-format67948d32016-09-07 22:40:40 -0700423 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424 [MAX_MIB_SIZE] =
425 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700426 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
David Barker16c64e32017-08-23 16:54:59 +0100427 assert(tx_size > TX_4X4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428
Jingning Han571189c2016-10-24 10:38:43 -0700429 if (depth == MAX_VARTX_DEPTH) {
Jingning Han94d5bfc2016-10-21 10:14:36 -0700430 int idx, idy;
431 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700432 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
433 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han94d5bfc2016-10-21 10:14:36 -0700434 inter_tx_size[idy][idx] = tx_size;
435 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700436 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Jingning Han331662e2017-05-30 17:03:32 -0700437 txfm_partition_update(xd->above_txfm_context + blk_col,
438 xd->left_txfm_context + blk_row, tx_size, tx_size);
Jingning Han94d5bfc2016-10-21 10:14:36 -0700439 return;
440 }
441
Thomas Davies985bfc32017-06-27 16:51:26 +0100442#if CONFIG_NEW_MULTISYMBOL
443 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
444#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700445 is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
Thomas Davies985bfc32017-06-27 16:51:26 +0100446#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447
448 if (is_split) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700449 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
450 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451 int i;
452
453 if (counts) ++counts->txfm_partition[ctx][1];
454
David Barker16c64e32017-08-23 16:54:59 +0100455 if (sub_txs == TX_4X4) {
Jingning Han9ca05b72017-01-03 14:41:36 -0800456 int idx, idy;
Jingning Hanab9ecba2017-01-13 09:11:58 -0800457 inter_tx_size[0][0] = sub_txs;
Jingning Han9ca05b72017-01-03 14:41:36 -0800458 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
459 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han581d1692017-01-05 16:03:54 -0800460 inter_tx_size[idy][idx] = inter_tx_size[0][0];
Jingning Hanab9ecba2017-01-13 09:11:58 -0800461 mbmi->tx_size = sub_txs;
Jingning Hane67b38a2016-11-04 10:30:00 -0700462 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han331662e2017-05-30 17:03:32 -0700463 txfm_partition_update(xd->above_txfm_context + blk_col,
464 xd->left_txfm_context + blk_row, sub_txs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700465 return;
466 }
467
468 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 for (i = 0; i < 4; ++i) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700470 int offsetr = blk_row + (i >> 1) * bsl;
471 int offsetc = blk_col + (i & 0x01) * bsl;
472 read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700473 offsetc, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 }
475 } else {
476 int idx, idy;
477 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700478 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
479 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700480 inter_tx_size[idy][idx] = tx_size;
481 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700482 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700484 txfm_partition_update(xd->above_txfm_context + blk_col,
485 xd->left_txfm_context + blk_row, tx_size, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700486 }
487}
488#endif
489
Yaowu Xuf883b422016-08-30 14:01:10 -0700490static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
491 int tx_size_cat, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700492 FRAME_COUNTS *counts = xd->counts;
493 const int ctx = get_tx_size_context(xd);
Thomas Davies15580c52017-03-09 13:53:42 +0000494 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
495 (void)cm;
Thomas Davies15580c52017-03-09 13:53:42 +0000496
Nathan E. Egge476c63c2017-05-18 18:35:16 -0400497 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
498 tx_size_cat + 2, ACCT_STR);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800499 const TX_SIZE tx_size = depth_to_tx_size(depth);
500#if CONFIG_RECT_TX
501 assert(!is_rect_tx(tx_size));
502#endif // CONFIG_RECT_TX
Jingning Han906be072016-10-26 11:04:31 -0700503 if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
Jingning Han4e1737a2016-10-25 16:05:02 -0700504 return tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700505}
506
Urvang Joshifeb925f2016-12-05 10:37:29 -0800507static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
508 int allow_select_inter, aom_reader *r) {
509 const TX_MODE tx_mode = cm->tx_mode;
510 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Debargha Mukherjee428bbb22017-03-17 07:30:24 -0700512#if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
Yushin Cho127c5832017-07-28 16:39:04 -0700513 if (bsize > BLOCK_4X4)
Jingning Han4be1a4d2017-01-06 10:59:20 -0800514#else
Yushin Cho127c5832017-07-28 16:39:04 -0700515 if (bsize >= BLOCK_8X8)
Urvang Joshifeb925f2016-12-05 10:37:29 -0800516#endif // CONFIG_CB4X4 && CONFIG_VAR_TX
Yushin Cho127c5832017-07-28 16:39:04 -0700517 {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800518 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
519 const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
520 : intra_tx_size_cat_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521 const TX_SIZE coded_tx_size =
Urvang Joshifeb925f2016-12-05 10:37:29 -0800522 read_selected_tx_size(cm, xd, tx_size_cat, r);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700523#if CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524 if (coded_tx_size > max_txsize_lookup[bsize]) {
525 assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
Yue Chend6bdd462017-07-19 16:05:43 -0700526#if CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700527 if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
Yue Chend6bdd462017-07-19 16:05:43 -0700528 int quarter_tx;
Yue Chen56e226e2017-05-02 16:21:40 -0700529
Yue Chend6bdd462017-07-19 16:05:43 -0700530 if (quarter_txsize_lookup[bsize] != max_txsize_lookup[bsize]) {
531 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
532 FRAME_COUNTS *counts = xd->counts;
533
534 if (counts) ++counts->quarter_tx_size[quarter_tx];
535 } else {
536 quarter_tx = 1;
537 }
Yue Chen56e226e2017-05-02 16:21:40 -0700538 return quarter_tx ? quarter_txsize_lookup[bsize]
539 : max_txsize_rect_lookup[bsize];
540 }
Yue Chend6bdd462017-07-19 16:05:43 -0700541#endif // CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700542
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 return max_txsize_rect_lookup[bsize];
544 }
Peter de Rivaza7c81462016-09-26 14:20:13 +0100545#else
546 assert(coded_tx_size <= max_txsize_lookup[bsize]);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700547#endif // CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700548 return coded_tx_size;
549 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800550 return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 }
552 } else {
553#if CONFIG_EXT_TX && CONFIG_RECT_TX
554 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
555 return max_txsize_rect_lookup[bsize];
556#else
557 return TX_4X4;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800558#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700559 }
560}
561
Yaowu Xuf883b422016-08-30 14:01:10 -0700562static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563 int mi_offset, int x_mis, int y_mis) {
564 int x, y, segment_id = INT_MAX;
565
566 for (y = 0; y < y_mis; y++)
567 for (x = 0; x < x_mis; x++)
568 segment_id =
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570
571 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
572 return segment_id;
573}
574
Yaowu Xuf883b422016-08-30 14:01:10 -0700575static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576 int segment_id) {
577 int x, y;
578
579 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
580
581 for (y = 0; y < y_mis; y++)
582 for (x = 0; x < x_mis; x++)
583 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
584}
585
Yaowu Xuf883b422016-08-30 14:01:10 -0700586static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587 int mi_offset, int x_mis, int y_mis,
Yaowu Xuf883b422016-08-30 14:01:10 -0700588 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700589 struct segmentation *const seg = &cm->seg;
590 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100591 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100592 struct segmentation_probs *const segp = &ec_ctx->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 int segment_id;
594
595 if (!seg->enabled) return 0; // Default for disabled segmentation
596
597 assert(seg->update_map && !seg->temporal_update);
598
599 segment_id = read_segment_id(r, segp);
600 if (counts) ++counts->seg.tree_total[segment_id];
601 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
602 return segment_id;
603}
604
Yaowu Xuf883b422016-08-30 14:01:10 -0700605static void copy_segment_id(const AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 const uint8_t *last_segment_ids,
607 uint8_t *current_segment_ids, int mi_offset,
608 int x_mis, int y_mis) {
609 int x, y;
610
611 for (y = 0; y < y_mis; y++)
612 for (x = 0; x < x_mis; x++)
613 current_segment_ids[mi_offset + y * cm->mi_cols + x] =
614 last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
615 : 0;
616}
617
Yaowu Xuf883b422016-08-30 14:01:10 -0700618static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
619 int mi_row, int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620 struct segmentation *const seg = &cm->seg;
621 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100622 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100623 struct segmentation_probs *const segp = &ec_ctx->seg;
624
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
626 int predicted_segment_id, segment_id;
627 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Hanc709e1f2016-12-06 14:48:09 -0800628 const int bw = mi_size_wide[mbmi->sb_type];
629 const int bh = mi_size_high[mbmi->sb_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630
631 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -0700632 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
633 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634
635 if (!seg->enabled) return 0; // Default for disabled segmentation
636
637 predicted_segment_id = cm->last_frame_seg_map
638 ? dec_get_segment_id(cm, cm->last_frame_seg_map,
639 mi_offset, x_mis, y_mis)
640 : 0;
641
642 if (!seg->update_map) {
643 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
644 mi_offset, x_mis, y_mis);
645 return predicted_segment_id;
646 }
647
648 if (seg->temporal_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700649 const int ctx = av1_get_pred_context_seg_id(xd);
Thomas Davies00021352017-07-11 16:07:55 +0100650#if CONFIG_NEW_MULTISYMBOL
651 aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
652 mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
653#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700655 mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
Thomas Davies00021352017-07-11 16:07:55 +0100656#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657 if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
658 if (mbmi->seg_id_predicted) {
659 segment_id = predicted_segment_id;
660 } else {
661 segment_id = read_segment_id(r, segp);
662 if (counts) ++counts->seg.tree_mispred[segment_id];
663 }
664 } else {
665 segment_id = read_segment_id(r, segp);
666 if (counts) ++counts->seg.tree_total[segment_id];
667 }
668 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
669 return segment_id;
670}
671
Yaowu Xuf883b422016-08-30 14:01:10 -0700672static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
673 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
675 return 1;
676 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700677 const int ctx = av1_get_skip_context(xd);
Thomas Davies61e3e372017-04-04 16:10:23 +0100678#if CONFIG_NEW_MULTISYMBOL
679 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
680 const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
681#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700682 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Thomas Davies61e3e372017-04-04 16:10:23 +0100683#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684 FRAME_COUNTS *counts = xd->counts;
685 if (counts) ++counts->skip[ctx][skip];
686 return skip;
687 }
688}
689
hui su33567b22017-04-30 16:40:19 -0700690#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700691static int uint16_compare(const void *a, const void *b) {
Urvang Joshi0ba850e2017-05-12 17:05:45 -0700692 const uint16_t va = *(const uint16_t *)a;
693 const uint16_t vb = *(const uint16_t *)b;
hui su33567b22017-04-30 16:40:19 -0700694 return va - vb;
695}
hui su33567b22017-04-30 16:40:19 -0700696
697static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
698 PALETTE_MODE_INFO *const pmi, aom_reader *r) {
699 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700700 const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
hui su33567b22017-04-30 16:40:19 -0700701 const int n = pmi->palette_size[0];
702 int idx = 0;
703 for (int i = 0; i < n_cache && idx < n; ++i)
704 if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
705 if (idx < n) {
706 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
707 if (idx < n) {
708 const int min_bits = bit_depth - 3;
709 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
710 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
711 for (; idx < n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100712 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700713 const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100714 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
715 0, (1 << bit_depth) - 1);
716 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700717 bits = AOMMIN(bits, av1_ceil_log2(range));
718 }
719 }
720 }
hui su33567b22017-04-30 16:40:19 -0700721 qsort(pmi->palette_colors, n, sizeof(pmi->palette_colors[0]), uint16_compare);
hui su33567b22017-04-30 16:40:19 -0700722}
723
724static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
725 PALETTE_MODE_INFO *const pmi,
726 aom_reader *r) {
727 const int n = pmi->palette_size[1];
728 // U channel colors.
729 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700730 const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
hui su33567b22017-04-30 16:40:19 -0700731 int idx = PALETTE_MAX_SIZE;
732 for (int i = 0; i < n_cache && idx < PALETTE_MAX_SIZE + n; ++i)
733 if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
734 if (idx < PALETTE_MAX_SIZE + n) {
735 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
736 if (idx < PALETTE_MAX_SIZE + n) {
737 const int min_bits = bit_depth - 3;
738 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
739 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
740 for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100741 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700742 const int delta = aom_read_literal(r, bits, ACCT_STR);
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100743 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
744 0, (1 << bit_depth) - 1);
745 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700746 bits = AOMMIN(bits, av1_ceil_log2(range));
747 }
748 }
749 }
hui su33567b22017-04-30 16:40:19 -0700750 qsort(pmi->palette_colors + PALETTE_MAX_SIZE, n,
751 sizeof(pmi->palette_colors[0]), uint16_compare);
hui su33567b22017-04-30 16:40:19 -0700752
753 // V channel colors.
754 if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
755 const int min_bits_v = bit_depth - 4;
756 const int max_val = 1 << bit_depth;
757 int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
758 pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
759 aom_read_literal(r, bit_depth, ACCT_STR);
760 for (int i = 1; i < n; ++i) {
761 int delta = aom_read_literal(r, bits, ACCT_STR);
762 if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
763 int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
764 if (val < 0) val += max_val;
765 if (val >= max_val) val -= max_val;
766 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
767 }
768 } else {
769 for (int i = 0; i < n; ++i) {
770 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
771 aom_read_literal(r, bit_depth, ACCT_STR);
772 }
773 }
774}
775#endif // CONFIG_PALETTE_DELTA_ENCODING
776
Yaowu Xuf883b422016-08-30 14:01:10 -0700777static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
778 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779 MODE_INFO *const mi = xd->mi[0];
780 MB_MODE_INFO *const mbmi = &mi->mbmi;
781 const MODE_INFO *const above_mi = xd->above_mi;
782 const MODE_INFO *const left_mi = xd->left_mi;
783 const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
785
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100786 assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST);
787 const int block_palette_idx = bsize - BLOCK_8X8;
Thomas Davies59f92312017-08-23 00:33:12 +0100788 int modev;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100789
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 if (mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800791 int palette_y_mode_ctx = 0;
hui su40b9e7f2017-07-13 18:15:56 -0700792 if (above_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800793 palette_y_mode_ctx +=
794 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700795 }
796 if (left_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800797 palette_y_mode_ctx +=
798 (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700799 }
Thomas Davies59f92312017-08-23 00:33:12 +0100800#if CONFIG_NEW_MULTISYMBOL
801 modev = aom_read_symbol(
802 r,
803 xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
804 2, ACCT_STR);
805#else
806 modev = aom_read(
807 r,
808 av1_default_palette_y_mode_prob[block_palette_idx][palette_y_mode_ctx],
809 ACCT_STR);
810#endif
811 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 pmi->palette_size[0] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100813 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100814 xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100815 PALETTE_SIZES, ACCT_STR) +
816 2;
hui sud13c24a2017-04-07 16:13:07 -0700817#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700818 read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700819#else
hui su40b9e7f2017-07-13 18:15:56 -0700820 for (int i = 0; i < pmi->palette_size[0]; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700821 pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui sud13c24a2017-04-07 16:13:07 -0700822#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 }
824 }
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400825 if (mbmi->uv_mode == UV_DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800826 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
Thomas Davies59f92312017-08-23 00:33:12 +0100827#if CONFIG_NEW_MULTISYMBOL
828 modev = aom_read_symbol(
829 r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
830#else
831 modev = aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
832 ACCT_STR);
833#endif
834 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 pmi->palette_size[1] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100836 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100837 xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100838 PALETTE_SIZES, ACCT_STR) +
839 2;
hui sud13c24a2017-04-07 16:13:07 -0700840#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700841 read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700842#else
hui su40b9e7f2017-07-13 18:15:56 -0700843 for (int i = 0; i < pmi->palette_size[1]; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844 pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700845 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700847 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848 }
hui sud13c24a2017-04-07 16:13:07 -0700849#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700850 }
851 }
852}
853
hui su5db97432016-10-14 16:10:14 -0700854#if CONFIG_FILTER_INTRA
855static void read_filter_intra_mode_info(AV1_COMMON *const cm,
Jingning Han62946d12017-05-26 11:29:30 -0700856 MACROBLOCKD *const xd, int mi_row,
857 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858 MODE_INFO *const mi = xd->mi[0];
859 MB_MODE_INFO *const mbmi = &mi->mbmi;
860 FRAME_COUNTS *counts = xd->counts;
hui su5db97432016-10-14 16:10:14 -0700861 FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
862 &mbmi->filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863
Urvang Joshic6300aa2017-06-01 14:46:23 -0700864 if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0) {
hui su5db97432016-10-14 16:10:14 -0700865 filter_intra_mode_info->use_filter_intra_mode[0] =
866 aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
867 if (filter_intra_mode_info->use_filter_intra_mode[0]) {
868 filter_intra_mode_info->filter_intra_mode[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700869 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 }
hui su5db97432016-10-14 16:10:14 -0700871 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800872 ++counts
873 ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
hui su5db97432016-10-14 16:10:14 -0700874 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 }
Jingning Han62946d12017-05-26 11:29:30 -0700876
877#if CONFIG_CB4X4
878 if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
879 xd->plane[1].subsampling_x,
880 xd->plane[1].subsampling_y))
881 return;
hui sub4ed1492017-05-31 17:25:42 -0700882#else
883 (void)mi_row;
884 (void)mi_col;
885#endif // CONFIG_CB4X4
Jingning Han62946d12017-05-26 11:29:30 -0700886
Urvang Joshic6300aa2017-06-01 14:46:23 -0700887 if (mbmi->uv_mode == UV_DC_PRED &&
888 mbmi->palette_mode_info.palette_size[1] == 0) {
hui su5db97432016-10-14 16:10:14 -0700889 filter_intra_mode_info->use_filter_intra_mode[1] =
890 aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
891 if (filter_intra_mode_info->use_filter_intra_mode[1]) {
892 filter_intra_mode_info->filter_intra_mode[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700893 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894 }
hui su5db97432016-10-14 16:10:14 -0700895 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800896 ++counts
897 ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
hui su5db97432016-10-14 16:10:14 -0700898 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 }
900}
hui su5db97432016-10-14 16:10:14 -0700901#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902
hui su5db97432016-10-14 16:10:14 -0700903#if CONFIG_EXT_INTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700904static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
905 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
907 const BLOCK_SIZE bsize = mbmi->sb_type;
hui sueda3d762016-12-06 16:58:23 -0800908#if CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -0800909 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
Yaowu Xuf883b422016-08-30 14:01:10 -0700910 const int ctx = av1_get_pred_context_intra_interp(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 int p_angle;
hui sueda3d762016-12-06 16:58:23 -0800912#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913
hui sueda3d762016-12-06 16:58:23 -0800914 (void)cm;
Joe Young830d4ce2017-05-30 17:48:13 -0700915
916 mbmi->angle_delta[0] = 0;
917 mbmi->angle_delta[1] = 0;
Jonathan Matthews67c9ab02017-09-27 13:38:50 +0100918#if CONFIG_INTRA_INTERP
919 mbmi->intra_filter = INTRA_FILTER_LINEAR;
920#endif // CONFIG_INTRA_INTERP
Joe Young830d4ce2017-05-30 17:48:13 -0700921
922 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923
hui su45dc5972016-12-08 17:42:50 -0800924 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700926 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
hui sueda3d762016-12-06 16:58:23 -0800927#if CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -0700928 p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -0700929 if (av1_is_intra_filter_switchable(p_angle)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700930 FRAME_COUNTS *counts = xd->counts;
hui sub4e25d22017-03-09 15:32:30 -0800931 mbmi->intra_filter = aom_read_symbol(r, ec_ctx->intra_filter_cdf[ctx],
932 INTRA_FILTERS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934 }
hui sueda3d762016-12-06 16:58:23 -0800935#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 }
937
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400938 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700939 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700940 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 }
942}
943#endif // CONFIG_EXT_INTRA
944
Angie Chianga9f9a312017-04-13 16:40:43 -0700945void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Hanab7163d2016-11-04 09:46:35 -0700946#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -0700947 int supertx_enabled,
Jingning Hanab7163d2016-11-04 09:46:35 -0700948#endif
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700949#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700950 int blk_row, int blk_col, int block, int plane,
951 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700952#endif
953 aom_reader *r) {
954 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700955 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700956#if !CONFIG_TXK_SEL
Jingning Hane67b38a2016-11-04 10:30:00 -0700957#if CONFIG_VAR_TX
958 const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
959#else
Jingning Hanab7163d2016-11-04 09:46:35 -0700960 const TX_SIZE tx_size = mbmi->tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700961#endif
Jingning Han243b66b2017-06-23 12:11:47 -0700962#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000963 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000964
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700965#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700966 TX_TYPE *tx_type = &mbmi->tx_type;
967#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700968 // only y plane's tx_type is transmitted
969 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700970 (void)block;
971 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << 4) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700972#endif
973
Jingning Hanab7163d2016-11-04 09:46:35 -0700974 if (!FIXED_TX_TYPE) {
975#if CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -0800976 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -0800977 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
978 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -0800979 ((!cm->seg.enabled && cm->base_qindex > 0) ||
980 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
981 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -0700982#if CONFIG_SUPERTX
983 !supertx_enabled &&
984#endif // CONFIG_SUPERTX
985 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Suddbcde22017-09-18 17:22:02 -0700986 const TxSetType tx_set_type = get_ext_tx_set_type(
987 tx_size, mbmi->sb_type, inter_block, cm->reduced_tx_set_used);
Sarah Parkere68a3e42017-02-16 14:03:24 -0800988 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
989 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -0700990 // eset == 0 should correspond to a set with only DCT_DCT and
991 // there is no need to read the tx_type
992 assert(eset != 0);
Hui Su98b0b3e2017-09-19 13:54:02 -0700993#if CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -0700994 FRAME_COUNTS *counts = xd->counts;
Hui Su98b0b3e2017-09-19 13:54:02 -0700995#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -0700996 if (inter_block) {
Hui Suddbcde22017-09-18 17:22:02 -0700997 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -0700998 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
Hui Suddbcde22017-09-18 17:22:02 -0700999 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001000#if CONFIG_ENTROPY_STATS
Sarah Parker784596d2017-06-23 08:41:26 -07001001 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001002#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001003 } else if (ALLOW_INTRA_EXT_TX) {
Hui Suddbcde22017-09-18 17:22:02 -07001004 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -07001005 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
Hui Suddbcde22017-09-18 17:22:02 -07001006 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001007#if CONFIG_ENTROPY_STATS
Sarah Parker784596d2017-06-23 08:41:26 -07001008 if (counts)
1009 ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001010#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001011 }
1012 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001013 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001014 }
1015#else
Yue Cheneeacc4c2017-01-17 17:29:17 -08001016
1017 if (tx_size < TX_32X32 &&
1018 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1019 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1020 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001021#if CONFIG_SUPERTX
1022 !supertx_enabled &&
1023#endif // CONFIG_SUPERTX
1024 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Su98b0b3e2017-09-19 13:54:02 -07001025#if CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001026 FRAME_COUNTS *counts = xd->counts;
Hui Su98b0b3e2017-09-19 13:54:02 -07001027#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001028 if (inter_block) {
Angie Chianga9f9a312017-04-13 16:40:43 -07001029 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001030 r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001031#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001032 if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001033#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001034 } else {
1035 const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
Angie Chianga9f9a312017-04-13 16:40:43 -07001036 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001037 r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Hanab7163d2016-11-04 09:46:35 -07001038 ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001039#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001040 if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001041#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001042 }
1043 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001044 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001045 }
1046#endif // CONFIG_EXT_TX
1047 }
Nathan E. Eggeebced372017-02-17 20:57:21 -05001048#if FIXED_TX_TYPE
1049 assert(mbmi->tx_type == DCT_DCT);
1050#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001051}
1052
Alex Converse28744302017-04-13 14:46:22 -07001053#if CONFIG_INTRABC
1054static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
1055 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001056 MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -07001057
1058static INLINE int is_mv_valid(const MV *mv);
1059
1060static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
1061 const int_mv *ref_mv, int mi_row, int mi_col,
1062 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -07001063 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1064 (void)cm;
Alex Converse28744302017-04-13 14:46:22 -07001065 FRAME_COUNTS *counts = xd->counts;
1066 nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse6b2584c2017-05-02 09:51:21 -07001067 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
1068 MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -07001069 int valid = is_mv_valid(&mv->as_mv) &&
1070 is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize);
Alex Converse28744302017-04-13 14:46:22 -07001071 return valid;
1072}
1073#endif // CONFIG_INTRABC
1074
Yaowu Xuf883b422016-08-30 14:01:10 -07001075static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001076 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001077 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078 MODE_INFO *const mi = xd->mi[0];
1079 MB_MODE_INFO *const mbmi = &mi->mbmi;
1080 const MODE_INFO *above_mi = xd->above_mi;
1081 const MODE_INFO *left_mi = xd->left_mi;
1082 const BLOCK_SIZE bsize = mbmi->sb_type;
1083 int i;
1084 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001085 const int bw = mi_size_wide[bsize];
1086 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001087
1088 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001089 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1090 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001091 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001092
1093 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1094 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Arild Fuldseth07441162016-08-15 15:07:52 +02001095
Arild Fuldseth07441162016-08-15 15:07:52 +02001096 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001097 xd->current_qindex =
1098 xd->prev_qindex +
1099 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001100 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1101 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001102 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001103#if CONFIG_EXT_DELTA_Q
1104 if (cm->delta_lf_present_flag) {
1105 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1106 xd->prev_delta_lf_from_base +
1107 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1108 cm->delta_lf_res;
1109 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
1110 }
1111#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001112 }
Arild Fuldseth07441162016-08-15 15:07:52 +02001113
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001115 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001116
Alex Converse28744302017-04-13 14:46:22 -07001117#if CONFIG_INTRABC
1118 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools) {
Hui Su6c8584f2017-09-14 15:37:02 -07001119 mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
Alex Converse28744302017-04-13 14:46:22 -07001120 if (mbmi->use_intrabc) {
Alex Conversef71808c2017-06-06 12:21:17 -07001121 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001122 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
Jingning Hand6c17d92017-04-24 16:33:12 -07001123#if CONFIG_DUAL_FILTER
1124 for (int idx = 0; idx < 4; ++idx) mbmi->interp_filter[idx] = BILINEAR;
1125#else
Alex Converse28744302017-04-13 14:46:22 -07001126 mbmi->interp_filter = BILINEAR;
Jingning Hand6c17d92017-04-24 16:33:12 -07001127#endif
Alex Converse44c2bad2017-05-11 09:36:10 -07001128
1129 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Ryan95acd062017-09-29 14:56:44 -07001130 int_mv ref_mvs[MAX_MV_REF_CANDIDATES];
Alex Converse44c2bad2017-05-11 09:36:10 -07001131
1132 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02001133 xd->ref_mv_stack[INTRA_FRAME], NULL, ref_mvs, mi_row,
1134 mi_col, NULL, NULL, inter_mode_ctx);
Alex Converse44c2bad2017-05-11 09:36:10 -07001135
1136 int_mv nearestmv, nearmv;
1137 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1138
1139 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1140 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
1141
Alex Converse28744302017-04-13 14:46:22 -07001142 xd->corrupted |=
1143 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
Alex Conversee16b2662017-05-24 14:00:00 -07001144#if CONFIG_VAR_TX
1145 // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks
1146 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1147 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1148 int idx, idy;
1149 for (idy = 0; idy < height; ++idy)
1150 for (idx = 0; idx < width; ++idx)
1151 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1152 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1153#endif // CONFIG_VAR_TX
Alex Conversedaa15e42017-05-02 14:27:16 -07001154#if CONFIG_EXT_TX && !CONFIG_TXK_SEL
1155 av1_read_tx_type(cm, xd,
1156#if CONFIG_SUPERTX
1157 0,
1158#endif
1159 r);
1160#endif // CONFIG_EXT_TX && !CONFIG_TXK_SEL
Alex Converse28744302017-04-13 14:46:22 -07001161 return;
1162 }
1163 }
1164#endif // CONFIG_INTRABC
1165
Alex Conversef71808c2017-06-06 12:21:17 -07001166 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
1167
Jingning Han52261842016-12-14 12:17:49 -08001168#if CONFIG_CB4X4
1169 (void)i;
1170 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001171 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Jingning Han52261842016-12-14 12:17:49 -08001172#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001173 switch (bsize) {
1174 case BLOCK_4X4:
1175 for (i = 0; i < 4; ++i)
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001176 mi->bmi[i].as_mode = read_intra_mode(
1177 r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, i));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001178 mbmi->mode = mi->bmi[3].as_mode;
1179 break;
1180 case BLOCK_4X8:
1181 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001182 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001184 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001185 break;
1186 case BLOCK_8X4:
1187 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001188 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001189 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001190 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 2));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001191 break;
1192 default:
1193 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001194 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195 }
Jingning Han52261842016-12-14 12:17:49 -08001196#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197
Jingning Han36fe3202017-02-20 22:31:49 -08001198#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001199 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001200 xd->plane[1].subsampling_y)) {
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001201#if CONFIG_CFL
1202 xd->cfl->is_chroma_reference = 1;
1203#endif // CONFIG_CFL
1204#endif // CONFIG_CB4X4
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001205 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001206
Luc Trudeauf5334002017-04-25 12:21:26 -04001207#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001208 if (mbmi->uv_mode == UV_CFL_PRED) {
David Michael Barrf6eaa152017-07-19 19:42:28 +09001209 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001210 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001211 } else {
1212 xd->cfl->store_y = 0;
Luc Trudeauf5334002017-04-25 12:21:26 -04001213 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001214#endif // CONFIG_CFL
1215
1216#if CONFIG_CB4X4
Joe Young830d4ce2017-05-30 17:48:13 -07001217 } else {
1218 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001219 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001220#if CONFIG_CFL
1221 xd->cfl->is_chroma_reference = 0;
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001222 xd->cfl->store_y = 1;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001223#endif
Luc Trudeauf5334002017-04-25 12:21:26 -04001224 }
1225#endif
1226
Yaowu Xuc27fc142016-08-22 16:08:15 -07001227#if CONFIG_EXT_INTRA
1228 read_intra_angle_info(cm, xd, r);
1229#endif // CONFIG_EXT_INTRA
1230 mbmi->palette_mode_info.palette_size[0] = 0;
1231 mbmi->palette_mode_info.palette_size[1] = 0;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +01001232 if (bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST &&
1233 cm->allow_screen_content_tools)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001234 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001235#if CONFIG_FILTER_INTRA
1236 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1237 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001238 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001239 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001240#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001242#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07001243 av1_read_tx_type(cm, xd,
Jingning Hanab7163d2016-11-04 09:46:35 -07001244#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07001245 0,
Nathan E. Egge72762a22016-09-07 17:12:07 -04001246#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07001247 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001248#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001249}
1250
Alex Converse6b2584c2017-05-02 09:51:21 -07001251static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
RogerZhou3b635242017-09-19 10:06:46 -07001252#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001253 int use_subpel,
RogerZhou3b635242017-09-19 10:06:46 -07001254#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001255 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001256 int mag, d, fr, hp;
Thomas Davies599395e2017-07-21 18:02:48 +01001257#if CONFIG_NEW_MULTISYMBOL
1258 const int sign = aom_read_bit(r, ACCT_STR);
1259#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001260 const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001261#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07001262 const int mv_class =
Nathan E. Egged7b893c2016-09-08 15:08:48 -04001263 aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001264 const int class0 = mv_class == MV_CLASS_0;
1265
1266 // Integer part
1267 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001268#if CONFIG_NEW_MULTISYMBOL
1269 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
1270#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -04001271 d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001272#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001273 mag = 0;
1274 } else {
1275 int i;
1276 const int n = mv_class + CLASS0_BITS - 1; // number of bits
Yaowu Xuc27fc142016-08-22 16:08:15 -07001277 d = 0;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001278#if CONFIG_NEW_MULTISYMBOL
1279 for (i = 0; i < n; ++i)
1280 d |= aom_read_symbol(r, mvcomp->bits_cdf[(i + 1) / 2], 2, ACCT_STR) << i;
1281#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001282 for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001283#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001284 mag = CLASS0_SIZE << (mv_class + 2);
1285 }
1286
RogerZhou3b635242017-09-19 10:06:46 -07001287#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001288 if (use_subpel) {
RogerZhou3b635242017-09-19 10:06:46 -07001289#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001290 // Fractional part
1291 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1292 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001293
Thomas Davies599395e2017-07-21 18:02:48 +01001294// High precision part (if hp is not used, the default value of the hp is 1)
1295#if CONFIG_NEW_MULTISYMBOL
1296 hp = usehp ? aom_read_symbol(
1297 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1298 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001299 : 1;
Thomas Davies599395e2017-07-21 18:02:48 +01001300#else
1301 hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
1302 : 1;
1303#endif
RogerZhou3b635242017-09-19 10:06:46 -07001304#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001305 } else {
1306 fr = 3;
1307 hp = 1;
1308 }
RogerZhou3b635242017-09-19 10:06:46 -07001309#endif // CONFIG_INTRABC || CONFIG_AMVR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001310
1311 // Result
1312 mag += ((d << 3) | (fr << 1) | hp) + 1;
1313 return sign ? -mag : mag;
1314}
1315
Yaowu Xuf883b422016-08-30 14:01:10 -07001316static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +01001317 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001318 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001319 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001320 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001321 joint_type =
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -04001322 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001323
1324 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001325 diff.row = read_mv_component(r, &ctx->comps[0],
RogerZhou3b635242017-09-19 10:06:46 -07001326#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001327 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001328#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001329 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001330
1331 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001332 diff.col = read_mv_component(r, &ctx->comps[1],
RogerZhou3b635242017-09-19 10:06:46 -07001333#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001334 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001335#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001336 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001337
Alex Converse6b2584c2017-05-02 09:51:21 -07001338 av1_inc_mv(&diff, counts, precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001339
1340 mv->row = ref->row + diff.row;
1341 mv->col = ref->col + diff.col;
1342}
1343
Yaowu Xuf883b422016-08-30 14:01:10 -07001344static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001345 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001346 aom_reader *r) {
Debargha Mukherjee0f248c42017-09-07 12:40:18 -07001347 if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type)) return SINGLE_REFERENCE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001348 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001349 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001350#if CONFIG_NEW_MULTISYMBOL
1351 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1352 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
1353#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001354 const REFERENCE_MODE mode =
Michael Bebenita6048d052016-08-25 14:40:54 -07001355 (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001356#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357 FRAME_COUNTS *counts = xd->counts;
1358 if (counts) ++counts->comp_inter[ctx][mode];
1359 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1360 } else {
1361 return cm->reference_mode;
1362 }
1363}
1364
Thomas Davies315f5782017-06-14 15:14:55 +01001365#if CONFIG_NEW_MULTISYMBOL
1366#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001367 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001368#define READ_REF_BIT2(pname) \
1369 aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001370#else
1371#define READ_REF_BIT(pname) \
1372 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001373#define READ_REF_BIT2(pname) \
1374 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001375#endif
1376
Zoe Liuc082bbc2017-05-17 13:31:37 -07001377#if CONFIG_EXT_COMP_REFS
Zoe Liuec50d6b2017-08-23 16:02:59 -07001378static COMP_REFERENCE_TYPE read_comp_reference_type(AV1_COMMON *cm,
1379 const MACROBLOCKD *xd,
1380 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001381 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001382#if USE_UNI_COMP_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07001383 COMP_REFERENCE_TYPE comp_ref_type;
1384#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001385 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm)) {
1386 if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm)) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001387#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001388#if CONFIG_NEW_MULTISYMBOL
1389 (void)cm;
1390 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read_symbol(
1391 r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
1392#else
1393 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read(
1394 r, cm->fc->comp_ref_type_prob[ctx], ACCT_STR);
1395#endif
Zoe Liufcf5fa22017-06-26 16:00:38 -07001396#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001397 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001398 comp_ref_type = BIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001399 }
1400 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001401 comp_ref_type = UNIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001402 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001403#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001404#else // !USE_UNI_COMP_REFS
1405 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1406 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1407#endif // USE_UNI_COMP_REFS
1408 FRAME_COUNTS *counts = xd->counts;
1409 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1410 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1411}
1412#endif // CONFIG_EXT_COMP_REFS
1413
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001415static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1416 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001417 MV_REFERENCE_FRAME ref_frame[2]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001418 FRAME_COUNTS *counts = xd->counts;
1419
1420 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1421 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1422 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001423 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001424 } else {
1425 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
1426 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1427 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001428#if CONFIG_EXT_COMP_REFS
1429 const COMP_REFERENCE_TYPE comp_ref_type =
1430 read_comp_reference_type(cm, xd, r);
1431
1432#if !USE_UNI_COMP_REFS
1433 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1434 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
1435#endif // !USE_UNI_COMP_REFS
1436
1437 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001438 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1439 int bit;
1440#if CONFIG_VAR_REFS
1441 if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
1442#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001443 bit = READ_REF_BIT2(uni_comp_ref_p);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001444#if CONFIG_VAR_REFS
1445 else
1446 bit = BWD_AND_ALT(cm);
1447#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001448 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1449
1450 if (bit) {
1451 ref_frame[0] = BWDREF_FRAME;
1452 ref_frame[1] = ALTREF_FRAME;
1453 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001454 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1455 int bit1;
1456#if CONFIG_VAR_REFS
1457 if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm)))
1458#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001459 bit1 = READ_REF_BIT2(uni_comp_ref_p1);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001460#if CONFIG_VAR_REFS
1461 else
1462 bit1 = L_AND_L3(cm) || L_AND_G(cm);
1463#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001464 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1465
1466 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001467 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1468 int bit2;
1469#if CONFIG_VAR_REFS
1470 if (L_AND_L3(cm) && L_AND_G(cm))
1471#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001472 bit2 = READ_REF_BIT2(uni_comp_ref_p2);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001473#if CONFIG_VAR_REFS
1474 else
1475 bit2 = L_AND_G(cm);
1476#endif // CONFIG_VAR_REFS
1477 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1478
1479 if (bit2) {
1480 ref_frame[0] = LAST_FRAME;
1481 ref_frame[1] = GOLDEN_FRAME;
1482 } else {
1483 ref_frame[0] = LAST_FRAME;
1484 ref_frame[1] = LAST3_FRAME;
1485 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001486 } else {
1487 ref_frame[0] = LAST_FRAME;
1488 ref_frame[1] = LAST2_FRAME;
1489 }
1490 }
1491
1492 return;
1493 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001494
1495 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001496#endif // CONFIG_EXT_COMP_REFS
1497
1498// Normative in decoder (for low delay)
Zoe Liu5a978832017-08-15 16:33:34 -07001499#if CONFIG_ONE_SIDED_COMPOUND
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001500 const int idx = 1;
Zoe Liu5a978832017-08-15 16:33:34 -07001501#else // !CONFIG_ONE_SIDED_COMPOUND
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502#if CONFIG_EXT_REFS
1503 const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
Zoe Liuc082bbc2017-05-17 13:31:37 -07001504#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505 const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
1506#endif // CONFIG_EXT_REFS
Zoe Liu5a978832017-08-15 16:33:34 -07001507#endif // CONFIG_ONE_SIDED_COMPOUND
Yaowu Xuc27fc142016-08-22 16:08:15 -07001508
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001509 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
1510#if CONFIG_VAR_REFS
1511 int bit;
1512 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1513 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001514 bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001515 else
1516 bit = L3_OR_G(cm);
1517#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001518 const int bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001519#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001520 if (counts) ++counts->comp_ref[ctx][0][bit];
1521
1522#if CONFIG_EXT_REFS
1523 // Decode forward references.
1524 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001525 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001526#if CONFIG_VAR_REFS
1527 int bit1;
1528 // Test need to explicitly code (L) vs (L2) branch node in tree
1529 if (L_AND_L2(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001530 bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001531 else
1532 bit1 = LAST_IS_VALID(cm);
1533#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001534 const int bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001535#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001536 if (counts) ++counts->comp_ref[ctx1][1][bit1];
1537 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
1538 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001539 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001540#if CONFIG_VAR_REFS
1541 int bit2;
1542 // Test need to explicitly code (L3) vs (G) branch node in tree
1543 if (L3_AND_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001544 bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001545 else
1546 bit2 = GOLDEN_IS_VALID(cm);
1547#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001548 const int bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001549#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001550 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1551 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1552 }
1553
1554 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001555 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
1556#if CONFIG_VAR_REFS
1557 int bit_bwd;
Zoe Liu3ac20932017-08-30 16:35:55 -07001558 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001559 const int bit_bwd_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001560 if (bit_bwd_uncertain)
Thomas Davies894cc812017-06-22 17:51:33 +01001561 bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001562 else
1563 bit_bwd = ALTREF_IS_VALID(cm);
Thomas Davies894cc812017-06-22 17:51:33 +01001564#else // !CONFIG_VAR_REFS
1565 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001566#endif // CONFIG_VAR_REFS
1567 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001568 if (!bit_bwd) {
1569 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
1570#if CONFIG_VAR_REFS
1571 int bit1_bwd;
1572 if (BWD_AND_ALT2(cm))
1573 bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1574 else
1575 bit1_bwd = ALTREF2_IS_VALID(cm);
1576#else // !CONFIG_VAR_REFS
1577 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1578#endif // CONFIG_VAR_REFS
1579 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1580 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1581 } else {
1582 ref_frame[idx] = cm->comp_bwd_ref[2];
1583 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001584#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001585 ref_frame[!idx] = cm->comp_var_ref[bit];
1586 ref_frame[idx] = cm->comp_fixed_ref;
1587#endif // CONFIG_EXT_REFS
1588 } else if (mode == SINGLE_REFERENCE) {
1589#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001590 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001591#if CONFIG_VAR_REFS
1592 int bit0;
Zoe Liue9b15e22017-07-19 15:53:01 -07001593 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT2,ALT) branch node
1594 // in tree
1595 if ((L_OR_L2(cm) || L3_OR_G(cm)) &&
1596 (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm)))
1597 bit0 = READ_REF_BIT(single_ref_p1);
1598 else
1599 bit0 = (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm));
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001600#else // !CONFIG_VAR_REFS
Thomas Davies315f5782017-06-14 15:14:55 +01001601 const int bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001602#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001603 if (counts) ++counts->single_ref[ctx0][0][bit0];
1604
1605 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001606 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001607#if CONFIG_VAR_REFS
1608 int bit1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001609 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001610 const int bit1_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001611 if (bit1_uncertain)
Thomas Davies315f5782017-06-14 15:14:55 +01001612 bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001613 else
1614 bit1 = ALTREF_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001615#else // !CONFIG_VAR_REFS
1616 const int bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001617#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001618 if (counts) ++counts->single_ref[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001619 if (!bit1) {
1620 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
1621#if CONFIG_VAR_REFS
1622 int bit5;
1623 if (BWD_AND_ALT2(cm))
1624 bit5 = READ_REF_BIT(single_ref_p6);
1625 else
1626 bit5 = ALTREF2_IS_VALID(cm);
1627#else // !CONFIG_VAR_REFS
1628 const int bit5 = READ_REF_BIT(single_ref_p6);
1629#endif // CONFIG_VAR_REFS
1630 if (counts) ++counts->single_ref[ctx5][5][bit5];
1631 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1632 } else {
1633 ref_frame[0] = ALTREF_FRAME;
1634 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001635 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001637#if CONFIG_VAR_REFS
1638 int bit2;
1639 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1640 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001641 bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001642 else
1643 bit2 = L3_OR_G(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001644#else // !CONFIG_VAR_REFS
1645 const int bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001646#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001647 if (counts) ++counts->single_ref[ctx2][2][bit2];
1648 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001649 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001650#if CONFIG_VAR_REFS
1651 int bit4;
1652 // Test need to explicitly code (L3) vs (G) branch node in tree
1653 if (L3_AND_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001654 bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001655 else
1656 bit4 = GOLDEN_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001657#else // !CONFIG_VAR_REFS
1658 const int bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001659#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001660 if (counts) ++counts->single_ref[ctx4][4][bit4];
1661 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1662 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001663 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001664#if CONFIG_VAR_REFS
1665 int bit3;
1666 // Test need to explicitly code (L) vs (L2) branch node in tree
1667 if (L_AND_L2(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001668 bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001669 else
1670 bit3 = LAST2_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001671#else // !CONFIG_VAR_REFS
1672 const int bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001673#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001674 if (counts) ++counts->single_ref[ctx3][3][bit3];
1675 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1676 }
1677 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001678#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001680 const int bit0 = READ_REF_BIT(single_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001681 if (counts) ++counts->single_ref[ctx0][0][bit0];
1682
1683 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001684 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001685 const int bit1 = READ_REF_BIT(single_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686 if (counts) ++counts->single_ref[ctx1][1][bit1];
1687 ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
1688 } else {
1689 ref_frame[0] = LAST_FRAME;
1690 }
1691#endif // CONFIG_EXT_REFS
1692
Emil Keyder01770b32017-01-20 18:03:11 -05001693 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694 } else {
1695 assert(0 && "Invalid prediction mode.");
1696 }
1697 }
1698}
1699
Angie Chiang9c4f8952016-11-21 11:13:19 -08001700static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1701 MACROBLOCKD *const xd,
1702 MB_MODE_INFO *const mbmi,
1703 aom_reader *r) {
1704 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001705 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001706
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001707 if (!av1_is_interp_needed(xd)) {
1708 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001709 return;
1710 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001711
1712#if CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07001713 if (cm->interp_filter != SWITCHABLE) {
Yue Chen19e7aa82016-11-30 14:05:39 -08001714 int dir;
1715
Angie Chiang9c4f8952016-11-21 11:13:19 -08001716 for (dir = 0; dir < 4; ++dir) mbmi->interp_filter[dir] = cm->interp_filter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001717 } else {
Yue Chen19e7aa82016-11-30 14:05:39 -08001718 int dir;
1719
Angie Chiang9c4f8952016-11-21 11:13:19 -08001720 for (dir = 0; dir < 2; ++dir) {
1721 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1722 mbmi->interp_filter[dir] = EIGHTTAP_REGULAR;
1723
1724 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1725 (mbmi->ref_frame[1] > INTRA_FRAME &&
1726 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001727 mbmi->interp_filter[dir] =
Thomas Daviesec92c112017-09-25 11:03:58 +01001728 (InterpFilter)aom_read_symbol(r, ec_ctx->switchable_interp_cdf[ctx],
1729 SWITCHABLE_FILTERS, ACCT_STR);
Angie Chiang9c4f8952016-11-21 11:13:19 -08001730 if (counts) ++counts->switchable_interp[ctx][mbmi->interp_filter[dir]];
1731 }
1732 }
1733 // The index system works as:
1734 // (0, 1) -> (vertical, horizontal) filter types for the first ref frame.
1735 // (2, 3) -> (vertical, horizontal) filter types for the second ref frame.
1736 mbmi->interp_filter[2] = mbmi->interp_filter[0];
1737 mbmi->interp_filter[3] = mbmi->interp_filter[1];
1738 }
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001739#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001740 if (cm->interp_filter != SWITCHABLE) {
1741 mbmi->interp_filter = cm->interp_filter;
1742 } else {
1743 const int ctx = av1_get_pred_context_switchable_interp(xd);
Thomas Daviesec92c112017-09-25 11:03:58 +01001744 mbmi->interp_filter = (InterpFilter)aom_read_symbol(
1745 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
Angie Chiang9c4f8952016-11-21 11:13:19 -08001746 if (counts) ++counts->switchable_interp[ctx][mbmi->interp_filter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001747 }
Angie Chiang9c4f8952016-11-21 11:13:19 -08001748#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07001749}
1750
Jingning Han36fe3202017-02-20 22:31:49 -08001751static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1752 const int mi_col, MACROBLOCKD *const xd,
1753 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001754 MB_MODE_INFO *const mbmi = &mi->mbmi;
1755 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1756 int i;
1757
1758 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001759 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001760
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001761 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001762
Jingning Han52261842016-12-14 12:17:49 -08001763#if CONFIG_CB4X4
1764 (void)i;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001765 mbmi->mode = read_intra_mode_y(ec_ctx, xd, r, size_group_lookup[bsize]);
Jingning Han52261842016-12-14 12:17:49 -08001766#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001767 switch (bsize) {
1768 case BLOCK_4X4:
1769 for (i = 0; i < 4; ++i)
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001770 mi->bmi[i].as_mode = read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771 mbmi->mode = mi->bmi[3].as_mode;
1772 break;
1773 case BLOCK_4X8:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001774 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
1775 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001776 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001777 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001778 break;
1779 case BLOCK_8X4:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001780 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
1781 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001782 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001783 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001784 break;
1785 default:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001786 mbmi->mode = read_intra_mode_y(ec_ctx, xd, r, size_group_lookup[bsize]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001787 }
Jingning Han52261842016-12-14 12:17:49 -08001788#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001789
Jingning Han36fe3202017-02-20 22:31:49 -08001790#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001791 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001792 xd->plane[1].subsampling_y)) {
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001793 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001794#else
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001795 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001796 (void)mi_row;
1797 (void)mi_col;
1798#endif
1799
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001800#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001801 if (mbmi->uv_mode == UV_CFL_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001802 mbmi->cfl_alpha_idx =
David Michael Barrf6eaa152017-07-19 19:42:28 +09001803 read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001804 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001805 } else {
1806 xd->cfl->store_y = 0;
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001807 }
1808#endif // CONFIG_CFL
1809
1810#if CONFIG_CB4X4
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001811 } else {
1812 // Avoid decoding angle_info if there is is no chroma prediction
1813 mbmi->uv_mode = UV_DC_PRED;
1814#if CONFIG_CFL
1815 xd->cfl->is_chroma_reference = 0;
1816 xd->cfl->store_y = 1;
1817#endif
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001818 }
1819#endif
1820
Rupert Swarbrick766c9d52017-07-31 11:30:53 +01001821 // Explicitly ignore cm here to avoid a compile warning if none of
1822 // ext-intra, palette and filter-intra are enabled.
1823 (void)cm;
1824
Yaowu Xuc27fc142016-08-22 16:08:15 -07001825#if CONFIG_EXT_INTRA
1826 read_intra_angle_info(cm, xd, r);
1827#endif // CONFIG_EXT_INTRA
1828 mbmi->palette_mode_info.palette_size[0] = 0;
1829 mbmi->palette_mode_info.palette_size[1] = 0;
1830 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
1831 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001832#if CONFIG_FILTER_INTRA
1833 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1834 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001835 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001836 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001837#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001838}
1839
1840static INLINE int is_mv_valid(const MV *mv) {
1841 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1842 mv->col < MV_UPP;
1843}
1844
Yaowu Xuf883b422016-08-30 14:01:10 -07001845static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001846 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001847 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001848 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001849 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1850 int mi_col, int is_compound, int allow_hp,
1851 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001852 int i;
1853 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001854 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001855 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001856 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001857#if CONFIG_CB4X4
1858 int_mv *pred_mv = mbmi->pred_mv;
1859 (void)block;
1860#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001861 int_mv *pred_mv =
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07001862 (bsize >= BLOCK_8X8) ? mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08001863#endif // CONFIG_CB4X4
Sarah Parkere5299862016-08-16 14:57:37 -07001864 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001865 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001866 (void)mi_row;
1867 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001868 (void)bsize;
RogerZhou3b635242017-09-19 10:06:46 -07001869#if CONFIG_AMVR
1870 if (cm->cur_frame_mv_precision_level) {
1871 allow_hp = MV_SUBPEL_NONE;
1872 }
1873#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001874 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001875 case NEWMV: {
1876 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001877 for (i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001878 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1879 int nmv_ctx =
1880 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1881 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001882 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001883 nmv_context_counts *const mv_counts =
1884 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001885 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001886 ret = ret && is_mv_valid(&mv[i].as_mv);
1887
Yaowu Xuc27fc142016-08-22 16:08:15 -07001888 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001889 }
1890 break;
1891 }
1892 case NEARESTMV: {
1893 mv[0].as_int = nearest_mv[0].as_int;
1894 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1895
Yaowu Xuc27fc142016-08-22 16:08:15 -07001896 pred_mv[0].as_int = nearest_mv[0].as_int;
1897 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001898 break;
1899 }
1900 case NEARMV: {
1901 mv[0].as_int = near_mv[0].as_int;
1902 if (is_compound) mv[1].as_int = near_mv[1].as_int;
1903
Yaowu Xuc27fc142016-08-22 16:08:15 -07001904 pred_mv[0].as_int = near_mv[0].as_int;
1905 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906 break;
1907 }
1908 case ZEROMV: {
Sarah Parkere5299862016-08-16 14:57:37 -07001909#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +00001910 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001911 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001912 mi_col, mi_row, block
1913#if CONFIG_AMVR
1914 ,
1915 cm->cur_frame_mv_precision_level
1916#endif
1917 )
David Barkercdcac6d2016-12-01 17:04:16 +00001918 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001919 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00001920 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001921 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001922 mi_col, mi_row, block
1923#if CONFIG_AMVR
1924 ,
1925 cm->cur_frame_mv_precision_level
1926#endif
1927 )
David Barkercdcac6d2016-12-01 17:04:16 +00001928 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001929#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001930 mv[0].as_int = 0;
1931 if (is_compound) mv[1].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -07001932#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07001933
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001934 pred_mv[0].as_int = mv[0].as_int;
1935 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001936 break;
1937 }
Zoe Liu85b66462017-04-20 14:28:19 -07001938#if CONFIG_COMPOUND_SINGLEREF
1939 case SR_NEAREST_NEARMV: {
1940 assert(!is_compound);
1941 mv[0].as_int = nearest_mv[0].as_int;
1942 mv[1].as_int = near_mv[0].as_int;
1943 break;
1944 }
1945 /*
1946 case SR_NEAREST_NEWMV: {
1947 assert(!is_compound);
1948 mv[0].as_int = nearest_mv[0].as_int;
1949
1950 FRAME_COUNTS *counts = xd->counts;
1951 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1952 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1953 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1954 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1955 nmv_context_counts *const mv_counts =
1956 counts ? &counts->mv[nmv_ctx] : NULL;
1957 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1958 ret = ret && is_mv_valid(&mv[1].as_mv);
1959 break;
1960 }*/
1961 case SR_NEAR_NEWMV: {
1962 assert(!is_compound);
1963 mv[0].as_int = near_mv[0].as_int;
1964
1965 FRAME_COUNTS *counts = xd->counts;
1966 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1967 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1968 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1969 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1970 nmv_context_counts *const mv_counts =
1971 counts ? &counts->mv[nmv_ctx] : NULL;
1972 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1973 ret = ret && is_mv_valid(&mv[1].as_mv);
1974 break;
1975 }
1976 case SR_ZERO_NEWMV: {
1977 assert(!is_compound);
1978#if CONFIG_GLOBAL_MOTION
1979 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1980 cm->allow_high_precision_mv, bsize,
1981 mi_col, mi_row, block)
1982 .as_int;
1983#else
1984 mv[0].as_int = 0;
1985#endif // CONFIG_GLOBAL_MOTION
1986
1987 FRAME_COUNTS *counts = xd->counts;
1988 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1989 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1990 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1991 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1992 nmv_context_counts *const mv_counts =
1993 counts ? &counts->mv[nmv_ctx] : NULL;
1994 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1995 ret = ret && is_mv_valid(&mv[1].as_mv);
1996 break;
1997 }
1998 case SR_NEW_NEWMV: {
1999 assert(!is_compound);
2000
2001 FRAME_COUNTS *counts = xd->counts;
2002 for (i = 0; i < 2; ++i) {
2003 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2004 int nmv_ctx =
2005 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
2006 mbmi->ref_mv_idx);
2007 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2008 nmv_context_counts *const mv_counts =
2009 counts ? &counts->mv[nmv_ctx] : NULL;
2010 read_mv(r, &mv[i].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2011 ret = ret && is_mv_valid(&mv[i].as_mv);
2012 }
2013 break;
2014 }
2015#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016 case NEW_NEWMV: {
2017 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002018 assert(is_compound);
2019 for (i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002020 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2021 int nmv_ctx =
2022 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
2023 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002024 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025 nmv_context_counts *const mv_counts =
2026 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002027 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002028 ret = ret && is_mv_valid(&mv[i].as_mv);
2029 }
2030 break;
2031 }
2032 case NEAREST_NEARESTMV: {
2033 assert(is_compound);
2034 mv[0].as_int = nearest_mv[0].as_int;
2035 mv[1].as_int = nearest_mv[1].as_int;
2036 break;
2037 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002038 case NEAR_NEARMV: {
2039 assert(is_compound);
2040 mv[0].as_int = near_mv[0].as_int;
2041 mv[1].as_int = near_mv[1].as_int;
2042 break;
2043 }
2044 case NEW_NEARESTMV: {
2045 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002046 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2047 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2048 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002049 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002050 nmv_context_counts *const mv_counts =
2051 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002052 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002053 assert(is_compound);
2054 ret = ret && is_mv_valid(&mv[0].as_mv);
2055 mv[1].as_int = nearest_mv[1].as_int;
2056 break;
2057 }
2058 case NEAREST_NEWMV: {
2059 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002060 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2061 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2062 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002063 nmv_context_counts *const mv_counts =
2064 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002065 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07002066 mv[0].as_int = nearest_mv[0].as_int;
2067 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002068 assert(is_compound);
2069 ret = ret && is_mv_valid(&mv[1].as_mv);
2070 break;
2071 }
2072 case NEAR_NEWMV: {
2073 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002074 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2075 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2076 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002077 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002078 nmv_context_counts *const mv_counts =
2079 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002080 mv[0].as_int = near_mv[0].as_int;
2081 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002082 assert(is_compound);
2083
2084 ret = ret && is_mv_valid(&mv[1].as_mv);
2085 break;
2086 }
2087 case NEW_NEARMV: {
2088 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002089 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2090 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2091 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002092 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002093 nmv_context_counts *const mv_counts =
2094 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002095 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002096 assert(is_compound);
2097 ret = ret && is_mv_valid(&mv[0].as_mv);
2098 mv[1].as_int = near_mv[1].as_int;
2099 break;
2100 }
2101 case ZERO_ZEROMV: {
2102 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08002103#if CONFIG_GLOBAL_MOTION
2104 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002105 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002106 mi_col, mi_row, block
2107#if CONFIG_AMVR
2108 ,
2109 cm->cur_frame_mv_precision_level
2110#endif
2111 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002112 .as_int;
2113 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002114 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002115 mi_col, mi_row, block
2116#if CONFIG_AMVR
2117 ,
2118 cm->cur_frame_mv_precision_level
2119#endif
2120 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002121 .as_int;
2122#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002123 mv[0].as_int = 0;
2124 mv[1].as_int = 0;
Sarah Parkerc2d38712017-01-24 15:15:41 -08002125#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002126 break;
2127 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002128 default: { return 0; }
2129 }
2130 return ret;
2131}
2132
Yaowu Xuf883b422016-08-30 14:01:10 -07002133static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
2134 int segment_id, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002135 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
2136 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
2137 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002138 const int ctx = av1_get_intra_inter_context(xd);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002139#if CONFIG_NEW_MULTISYMBOL
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002140 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002141 const int is_inter =
2142 aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
2143#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002144 const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002145#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146 FRAME_COUNTS *counts = xd->counts;
2147 if (counts) ++counts->intra_inter[ctx][is_inter];
2148 return is_inter;
2149 }
2150}
2151
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002152#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002153static int read_is_inter_singleref_comp_mode(AV1_COMMON *const cm,
2154 MACROBLOCKD *const xd,
2155 int segment_id, aom_reader *r) {
2156 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) return 0;
2157
2158 const int ctx = av1_get_inter_mode_context(xd);
2159 const int is_singleref_comp_mode =
2160 aom_read(r, cm->fc->comp_inter_mode_prob[ctx], ACCT_STR);
2161 FRAME_COUNTS *counts = xd->counts;
2162
2163 if (counts) ++counts->comp_inter_mode[ctx][is_singleref_comp_mode];
2164 return is_singleref_comp_mode;
2165}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002166#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002167
Yaowu Xuc27fc142016-08-22 16:08:15 -07002168static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002169 AV1Decoder *const pbi = (AV1Decoder *)data;
2170 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
2171 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002172}
2173
Di Chen56586622017-06-09 13:49:44 -07002174#if DEC_MISMATCH_DEBUG
2175static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi,
Zoe Liuf9333f52017-07-03 10:52:01 -07002176 MACROBLOCKD *const xd, int mi_row, int mi_col,
2177 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES],
2178 int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07002179 int_mv mv[2] = { { 0 } };
2180 int ref;
2181 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07002182 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
2183 mv[ref].as_mv = mbmi->mv[ref].as_mv;
2184
2185 int interp_ctx[2] = { -1 };
2186 int interp_filter[2] = { cm->interp_filter };
2187 if (cm->interp_filter == SWITCHABLE) {
2188 int dir;
2189 for (dir = 0; dir < 2; ++dir) {
2190 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
2191 (mbmi->ref_frame[1] > INTRA_FRAME &&
2192 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
2193 interp_ctx[dir] = av1_get_pred_context_switchable_interp(xd, dir);
2194 interp_filter[dir] = mbmi->interp_filter[dir];
2195 } else {
2196 interp_filter[dir] = EIGHTTAP_REGULAR;
2197 }
2198 }
2199 }
2200
2201 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
2202 int16_t zeromv_ctx = -1;
2203 int16_t refmv_ctx = -1;
2204 if (mbmi->mode != NEWMV) {
2205 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == ZEROMV);
2206 zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
2207 if (mbmi->mode != ZEROMV) {
2208 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
2209 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
2210 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
2211 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
2212 }
2213 }
2214
2215 int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Zoe Liuf9333f52017-07-03 10:52:01 -07002216#define FRAME_TO_CHECK 1
Zoe Liufcf5fa22017-06-26 16:00:38 -07002217 if (cm->current_video_frame == FRAME_TO_CHECK /*&& cm->show_frame == 0*/) {
Zoe Liuf9333f52017-07-03 10:52:01 -07002218 printf(
2219 "=== DECODER ===: "
2220 "Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
2221 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
2222 "ref[1]=%d, motion_mode=%d, inter_mode_ctx=%d, mode_ctx=%d, "
2223 "interp_ctx=(%d,%d), interp_filter=(%d,%d), newmv_ctx=%d, "
2224 "zeromv_ctx=%d, refmv_ctx=%d\n",
2225 cm->current_video_frame, mi_row, mi_col, mbmi->mode, mbmi->sb_type,
2226 cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
2227 mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
2228 mbmi->motion_mode, inter_mode_ctx[ref_frame_type], mode_ctx,
2229 interp_ctx[0], interp_ctx[1], interp_filter[0], interp_filter[1],
2230 newmv_ctx, zeromv_ctx, refmv_ctx);
2231 }
Di Chen56586622017-06-09 13:49:44 -07002232}
2233#endif // DEC_MISMATCH_DEBUG
2234
Yaowu Xuf883b422016-08-30 14:01:10 -07002235static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236 MACROBLOCKD *const xd,
2237 MODE_INFO *const mi,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002238#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002239 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002240 int supertx_enabled) {
2241#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002242 int mi_row, int mi_col, aom_reader *r) {
Yue Chencb60b182016-10-13 15:18:22 -07002243#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002244 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245 MB_MODE_INFO *const mbmi = &mi->mbmi;
2246 const BLOCK_SIZE bsize = mbmi->sb_type;
2247 const int allow_hp = cm->allow_high_precision_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08002248 const int unify_bsize = CONFIG_CB4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249 int_mv nearestmv[2], nearmv[2];
2250 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002251 int ref, is_compound;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002252#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002253 int is_singleref_comp_mode = 0;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002254#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002257 int16_t mode_ctx = 0;
Yue Chen69f18e12016-09-08 14:48:15 -07002258#if CONFIG_WARPED_MOTION
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08002259 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang1bc82862017-06-28 15:49:48 -07002260#if WARPED_MOTION_SORT_SAMPLES
2261 int pts_mv[SAMPLES_ARRAY_SIZE];
2262#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002263#endif // CONFIG_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00002264 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265
Urvang Joshi5a9ea002017-05-22 15:25:18 -07002266 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
2267
Luc Trudeaub05eeae2017-08-18 15:14:30 -04002268 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269 mbmi->palette_mode_info.palette_size[0] = 0;
2270 mbmi->palette_mode_info.palette_size[1] = 0;
2271
Frederic Barbier7a84fd82017-03-02 18:08:15 +01002272 memset(ref_mvs, 0, sizeof(ref_mvs));
2273
Yaowu Xuc27fc142016-08-22 16:08:15 -07002274 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
2275 is_compound = has_second_ref(mbmi);
2276
Zoe Liuc082bbc2017-05-17 13:31:37 -07002277#if CONFIG_EXT_COMP_REFS
2278#if !USE_UNI_COMP_REFS
2279 // NOTE: uni-directional comp refs disabled
2280 if (is_compound)
2281 assert(mbmi->ref_frame[0] < BWDREF_FRAME &&
2282 mbmi->ref_frame[1] >= BWDREF_FRAME);
2283#endif // !USE_UNI_COMP_REFS
2284#endif // CONFIG_EXT_COMP_REFS
2285
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002286#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002287 if (!is_compound)
2288 is_singleref_comp_mode =
2289 read_is_inter_singleref_comp_mode(cm, xd, mbmi->segment_id, r);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002290#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002291
Yaowu Xuc27fc142016-08-22 16:08:15 -07002292 for (ref = 0; ref < 1 + is_compound; ++ref) {
2293 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002294
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002295 av1_find_mv_refs(cm, xd, mi, frame, &xd->ref_mv_count[frame],
2296 xd->ref_mv_stack[frame], compound_inter_mode_ctx,
2297 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2298 inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002299 }
2300
Jingning Hanacddc032016-11-17 15:26:20 -08002301 if (is_compound) {
2302 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002303 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002304 xd->ref_mv_stack[ref_frame], compound_inter_mode_ctx,
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002305 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2306 inter_mode_ctx);
2307
2308 if (xd->ref_mv_count[ref_frame] < 2) {
2309 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00002310 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002311 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00002312#if CONFIG_GLOBAL_MOTION
2313 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00002314 cm->allow_high_precision_mv,
RogerZhou3b635242017-09-19 10:06:46 -07002315 bsize, mi_col, mi_row, 0
2316#if CONFIG_AMVR
2317 ,
2318 cm->cur_frame_mv_precision_level
2319#endif
2320 )
David Barkercdcac6d2016-12-01 17:04:16 +00002321 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002322 zeromv[1].as_int =
2323 (rf[1] != NONE_FRAME)
2324 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
2325 cm->allow_high_precision_mv, bsize, mi_col,
2326 mi_row, 0
2327#if CONFIG_AMVR
2328 ,
2329 cm->cur_frame_mv_precision_level
2330#endif
2331 )
2332 .as_int
2333 : 0;
David Barkercdcac6d2016-12-01 17:04:16 +00002334#else
2335 zeromv[0].as_int = zeromv[1].as_int = 0;
2336#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002337 for (ref = 0; ref < 2; ++ref) {
2338 if (rf[ref] == NONE_FRAME) continue;
RogerZhou3b635242017-09-19 10:06:46 -07002339#if CONFIG_AMVR
2340 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp,
2341 cm->cur_frame_mv_precision_level);
2342 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp,
2343 cm->cur_frame_mv_precision_level);
2344#else
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002345 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
2346 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002347#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002348 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
2349 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
2350 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01002351 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002352 }
2353 }
2354
Zoe Liu85b66462017-04-20 14:28:19 -07002355#if CONFIG_COMPOUND_SINGLEREF
2356 if (is_compound || is_singleref_comp_mode)
2357#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358 if (is_compound)
Zoe Liu85b66462017-04-20 14:28:19 -07002359#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002360 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
2361 else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07002363 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002364 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002366#if CONFIG_SEGMENT_ZEROMV
2367 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
2368 segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_ZEROMV)) {
2369#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002371#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372 mbmi->mode = ZEROMV;
Debargha Mukherjeec76f9dc2017-05-01 13:18:09 -07002373 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002374 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
David Barker3409c0d2017-06-30 17:33:13 +01002375 "Invalid usage of segment feature on small blocks");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376 return;
2377 }
2378 } else {
Jingning Han5cfa6712016-12-14 09:53:38 -08002379 if (bsize >= BLOCK_8X8 || unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 if (is_compound)
2381 mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002382#if CONFIG_COMPOUND_SINGLEREF
2383 else if (is_singleref_comp_mode)
Thomas Daviesb8b14a92017-07-12 15:11:49 +01002384 mbmi->mode = read_inter_singleref_comp_mode(xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002385#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 else
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002387 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
David Barker3dfba992017-04-03 16:10:09 +01002388 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002389#if CONFIG_COMPOUND_SINGLEREF
2390 mbmi->mode == SR_NEW_NEWMV ||
2391#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002392 have_nearmv_in_inter_mode(mbmi->mode))
Thomas Davies149eda52017-06-12 18:11:55 +01002393 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394 }
2395 }
2396
David Barker9b75e212017-07-28 15:31:41 +01002397 if ((bsize < BLOCK_8X8 && !unify_bsize) ||
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002398 (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399 for (ref = 0; ref < 1 + is_compound; ++ref) {
RogerZhou3b635242017-09-19 10:06:46 -07002400#if CONFIG_AMVR
2401 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2402 &nearestmv[ref], &nearmv[ref],
2403 cm->cur_frame_mv_precision_level);
2404#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002405 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2406 &nearestmv[ref], &nearmv[ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002407#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002408 }
2409 }
2410
Zoe Liu85b66462017-04-20 14:28:19 -07002411#if CONFIG_COMPOUND_SINGLEREF
2412 if ((is_compound || is_singleref_comp_mode) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002413 (bsize >= BLOCK_8X8 || unify_bsize) && mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002414#else // !CONFIG_COMPOUND_SINGLEREF
Jingning Han61418bb2017-01-23 17:12:48 -08002415 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002416 mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002417#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -07002418 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002419 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002420
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002421 if (xd->ref_mv_count[ref_frame_type] > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002422 if (mbmi->mode == NEAREST_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002423 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2424 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002425#if CONFIG_AMVR
2426 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
2427 cm->cur_frame_mv_precision_level);
2428 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
2429 cm->cur_frame_mv_precision_level);
2430#else
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002431 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
2432 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002433#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002434 } else if (mbmi->mode == NEAREST_NEWMV
2435#if CONFIG_COMPOUND_SINGLEREF
2436 || mbmi->mode == SR_NEAREST_NEARMV
2437// || mbmi->mode == SR_NEAREST_NEWMV
2438#endif // CONFIG_COMPOUND_SINGLEREF
2439 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002440 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002441
2442#if CONFIG_AMVR
2443 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
2444 cm->cur_frame_mv_precision_level);
2445#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002447#endif
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07002448 } else if (mbmi->mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002450#if CONFIG_AMVR
2451 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
2452 cm->cur_frame_mv_precision_level);
2453#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002454 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002455#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457 }
2458
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01002460 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
Zoe Liu85b66462017-04-20 14:28:19 -07002461#if CONFIG_COMPOUND_SINGLEREF
2462 if (is_compound) {
2463#endif // CONFIG_COMPOUND_SINGLEREF
2464 if (compound_ref0_mode(mbmi->mode) == NEARMV) {
2465 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002466#if CONFIG_AMVR
2467 lower_mv_precision(&nearmv[0].as_mv, allow_hp,
2468 cm->cur_frame_mv_precision_level);
2469#else
2470 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2471#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002472 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473
Zoe Liu85b66462017-04-20 14:28:19 -07002474 if (compound_ref1_mode(mbmi->mode) == NEARMV) {
2475 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002476#if CONFIG_AMVR
2477 lower_mv_precision(&nearmv[1].as_mv, allow_hp,
2478 cm->cur_frame_mv_precision_level);
2479#else
2480 lower_mv_precision(&nearmv[1].as_mv, allow_hp);
2481#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002482 }
2483#if CONFIG_COMPOUND_SINGLEREF
2484 } else {
2485 assert(is_singleref_comp_mode);
2486 if (compound_ref0_mode(mbmi->mode) == NEARMV ||
2487 compound_ref1_mode(mbmi->mode) == NEARMV) {
2488 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2489 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2490 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491 }
Zoe Liu85b66462017-04-20 14:28:19 -07002492#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493 }
David Barker0d7c4b02017-07-25 14:55:48 +01002494 } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
2495 int_mv cur_mv =
2496 xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
2497 nearmv[0] = cur_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002498 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499
Yue Chen19e7aa82016-11-30 14:05:39 -08002500#if !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION && !CONFIG_GLOBAL_MOTION
Angie Chiang9c4f8952016-11-21 11:13:19 -08002501 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002502#endif // !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503
Jingning Han5cfa6712016-12-14 09:53:38 -08002504 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505 const int num_4x4_w = 1 << xd->bmode_blocks_wl;
2506 const int num_4x4_h = 1 << xd->bmode_blocks_hl;
2507 int idx, idy;
2508 PREDICTION_MODE b_mode;
2509 int_mv nearest_sub8x8[2], near_sub8x8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002510 int_mv ref_mv[2][2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 for (idy = 0; idy < 2; idy += num_4x4_h) {
2512 for (idx = 0; idx < 2; idx += num_4x4_w) {
2513 int_mv block[2];
2514 const int j = idy * 2 + idx;
2515 int_mv ref_mv_s8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 if (!is_compound)
Yaowu Xuf883b422016-08-30 14:01:10 -07002517 mode_ctx = av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame,
2518 bsize, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002519 if (is_compound)
2520 b_mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
2521 else
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002522 b_mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002523
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002524 if (b_mode != ZEROMV && b_mode != ZERO_ZEROMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002525 CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
2526 uint8_t ref_mv_count[2];
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002527 for (ref = 0; ref < 1 + is_compound; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002528 int_mv mv_ref_list[MAX_MV_REF_CANDIDATES];
Yaowu Xu531d6af2017-03-07 17:48:52 -08002529 av1_update_mv_context(cm, xd, mi, mbmi->ref_frame[ref], mv_ref_list,
2530 j, mi_row, mi_col, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07002531 av1_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
Yaowu Xuf883b422016-08-30 14:01:10 -07002532 ref_mv_stack[ref], &ref_mv_count[ref],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002533 mv_ref_list, &nearest_sub8x8[ref],
Yaowu Xuf883b422016-08-30 14:01:10 -07002534 &near_sub8x8[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 if (have_newmv_in_inter_mode(b_mode)) {
2536 mv_ref_list[0].as_int = nearest_sub8x8[ref].as_int;
2537 mv_ref_list[1].as_int = near_sub8x8[ref].as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002538#if CONFIG_AMVR
2539 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2540 &ref_mv[1][ref],
2541 cm->cur_frame_mv_precision_level);
2542#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002543 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2544 &ref_mv[1][ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002545#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546 }
2547 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002548 }
2549
2550 for (ref = 0; ref < 1 + is_compound && b_mode != ZEROMV; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551 ref_mv_s8[ref] = nearest_sub8x8[ref];
RogerZhou3b635242017-09-19 10:06:46 -07002552#if CONFIG_AMVR
2553 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp,
2554 cm->cur_frame_mv_precision_level);
2555#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002556 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002557#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002558 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 (void)ref_mv_s8;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002560
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002561 if (!assign_mv(cm, xd, b_mode, mbmi->ref_frame, j, block, ref_mv[0],
David Barker45390c12017-02-20 14:44:40 +00002562 nearest_sub8x8, near_sub8x8, mi_row, mi_col, is_compound,
2563 allow_hp, r)) {
Angie Chiangd0916d92017-03-10 17:54:18 -08002564 aom_merge_corrupted_flag(&xd->corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002565 break;
2566 };
2567
2568 mi->bmi[j].as_mv[0].as_int = block[0].as_int;
Sarah Parkerd7fa8542016-10-11 11:51:59 -07002569 mi->bmi[j].as_mode = b_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 if (is_compound) mi->bmi[j].as_mv[1].as_int = block[1].as_int;
2571
2572 if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
2573 if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
2574 }
2575 }
2576
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07002577 mbmi->pred_mv[0].as_int = mi->bmi[3].pred_mv[0].as_int;
2578 mbmi->pred_mv[1].as_int = mi->bmi[3].pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002579 mi->mbmi.mode = b_mode;
2580
2581 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2582 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2583 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002584 int_mv ref_mv[2];
2585 ref_mv[0] = nearestmv[0];
2586 ref_mv[1] = nearestmv[1];
2587
David Barker3dfba992017-04-03 16:10:09 +01002588 if (is_compound) {
David Barker3dfba992017-04-03 16:10:09 +01002589 int ref_mv_idx = mbmi->ref_mv_idx;
2590 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2591 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2592 // mbmi->ref_mv_idx (like NEWMV)
2593 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2594 ref_mv_idx = 1 + mbmi->ref_mv_idx;
David Barker3dfba992017-04-03 16:10:09 +01002595
2596 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
David Barker404b2e82017-03-27 13:07:47 +01002597 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2598 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker3dfba992017-04-03 16:10:09 +01002599 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002600 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker404b2e82017-03-27 13:07:47 +01002601 xd->n8_h << MI_SIZE_LOG2, xd);
2602 }
David Barker3dfba992017-04-03 16:10:09 +01002603 nearestmv[0] = ref_mv[0];
David Barker404b2e82017-03-27 13:07:47 +01002604 }
David Barker3dfba992017-04-03 16:10:09 +01002605 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
David Barker3dfba992017-04-03 16:10:09 +01002606 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2607 if (xd->ref_mv_count[ref_frame_type] > 1) {
2608 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002609 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker3dfba992017-04-03 16:10:09 +01002610 xd->n8_h << MI_SIZE_LOG2, xd);
2611 }
David Barker3dfba992017-04-03 16:10:09 +01002612 nearestmv[1] = ref_mv[1];
2613 }
Zoe Liu85b66462017-04-20 14:28:19 -07002614#if CONFIG_COMPOUND_SINGLEREF
2615 } else if (is_singleref_comp_mode) {
2616 int ref_mv_idx = mbmi->ref_mv_idx;
2617 // Special case: SR_NEAR_NEWMV use 1 + mbmi->ref_mv_idx (like NEARMV)
2618 // instead of mbmi->ref_mv_idx (like NEWMV)
2619 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
2620
2621 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
2622 compound_ref1_mode(mbmi->mode) == NEWMV) {
2623 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2624 if (xd->ref_mv_count[ref_frame_type] > 1) {
2625 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2626 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2627 xd->n8_h << MI_SIZE_LOG2, xd);
2628 }
2629 // TODO(zoeliu): To further investigate why this would not cause a
2630 // mismatch for the mode of SR_NEAREST_NEWMV.
2631 nearestmv[0] = ref_mv[0];
2632 }
2633#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002634 } else {
David Barker3dfba992017-04-03 16:10:09 +01002635 if (mbmi->mode == NEWMV) {
2636 for (ref = 0; ref < 1 + is_compound; ++ref) {
David Barker3dfba992017-04-03 16:10:09 +01002637 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2638 if (xd->ref_mv_count[ref_frame_type] > 1) {
2639 ref_mv[ref] =
2640 (ref == 0)
2641 ? xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv
2642 : xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
2643 .comp_mv;
2644 clamp_mv_ref(&ref_mv[ref].as_mv, xd->n8_w << MI_SIZE_LOG2,
2645 xd->n8_h << MI_SIZE_LOG2, xd);
2646 }
David Barker3dfba992017-04-03 16:10:09 +01002647 nearestmv[ref] = ref_mv[ref];
2648 }
2649 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002650 }
2651
Angie Chiangd0916d92017-03-10 17:54:18 -08002652 int mv_corrupted_flag =
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002653 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
David Barker45390c12017-02-20 14:44:40 +00002654 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
Angie Chiangd0916d92017-03-10 17:54:18 -08002655 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 }
2657
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002658#if CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 mbmi->use_wedge_interintra = 0;
2660 if (cm->reference_mode != COMPOUND_REFERENCE &&
2661#if CONFIG_SUPERTX
2662 !supertx_enabled &&
2663#endif
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002664 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002666#if CONFIG_NEW_MULTISYMBOL
2667 const int interintra =
2668 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
2669#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002670 const int interintra =
2671 aom_read(r, cm->fc->interintra_prob[bsize_group], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002672#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002673 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002674 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 if (interintra) {
2676 const INTERINTRA_MODE interintra_mode =
2677 read_interintra_mode(cm, xd, r, bsize_group);
2678 mbmi->ref_frame[1] = INTRA_FRAME;
2679 mbmi->interintra_mode = interintra_mode;
2680#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 mbmi->angle_delta[0] = 0;
2682 mbmi->angle_delta[1] = 0;
hui sueda3d762016-12-06 16:58:23 -08002683#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684 mbmi->intra_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -08002685#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002687#if CONFIG_FILTER_INTRA
2688 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2689 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2690#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002691 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002692#if CONFIG_NEW_MULTISYMBOL
2693 mbmi->use_wedge_interintra = aom_read_symbol(
2694 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
2695#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696 mbmi->use_wedge_interintra =
Michael Bebenita6048d052016-08-25 14:40:54 -07002697 aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002698#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002699 if (xd->counts)
2700 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2701 if (mbmi->use_wedge_interintra) {
2702 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002703 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 mbmi->interintra_wedge_sign = 0;
2705 }
2706 }
2707 }
2708 }
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002709#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710
Yue Chen52c51732017-07-11 15:08:30 -07002711#if CONFIG_WARPED_MOTION
2712 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
2713 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2714 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2715
2716 xd->block_refs[ref] = ref_buf;
2717 }
2718#endif
2719
Yue Chencb60b182016-10-13 15:18:22 -07002720#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
2721 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yue Chen69f18e12016-09-08 14:48:15 -07002722#if CONFIG_WARPED_MOTION
2723 if (mbmi->sb_type >= BLOCK_8X8 && !has_second_ref(mbmi))
Yunqing Wang1bc82862017-06-28 15:49:48 -07002724#if WARPED_MOTION_SORT_SAMPLES
2725 mbmi->num_proj_ref[0] =
2726 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv);
2727#else
Yue Chen69f18e12016-09-08 14:48:15 -07002728 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang1bc82862017-06-28 15:49:48 -07002729#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002730#endif // CONFIG_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002731#if CONFIG_MOTION_VAR
2732 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
2733#endif
Yue Chen69f18e12016-09-08 14:48:15 -07002734
Yaowu Xuc27fc142016-08-22 16:08:15 -07002735#if CONFIG_SUPERTX
Yue Chen69f18e12016-09-08 14:48:15 -07002736 if (!supertx_enabled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002737#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738 if (mbmi->ref_frame[1] != INTRA_FRAME)
Sarah Parker19234cc2017-03-10 16:43:25 -08002739 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002740
2741#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -07002742 read_ncobmc_mode(xd, mi, mbmi->ncobmc_mode, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002743#endif
2744
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002745#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002746 if (is_singleref_comp_mode) assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002747#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chen69f18e12016-09-08 14:48:15 -07002748#if CONFIG_WARPED_MOTION
2749 if (mbmi->motion_mode == WARPED_CAUSAL) {
2750 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
Yunqing Wang1bc82862017-06-28 15:49:48 -07002751
2752#if WARPED_MOTION_SORT_SAMPLES
2753 if (mbmi->num_proj_ref[0] > 1)
2754 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2755 pts_inref, mbmi->num_proj_ref[0]);
2756#endif // WARPED_MOTION_SORT_SAMPLES
2757
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002758 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2759 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2760 &mbmi->wm_params[0], mi_row, mi_col)) {
Yunqing Wang8657ad72017-06-20 14:46:08 -07002761 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "Invalid Warped Model");
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002762 }
Yue Chen69f18e12016-09-08 14:48:15 -07002763 }
2764#endif // CONFIG_WARPED_MOTION
2765#if CONFIG_SUPERTX
2766 }
2767#endif // CONFIG_SUPERTX
Yue Chencb60b182016-10-13 15:18:22 -07002768#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002770 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Zoe Liu85b66462017-04-20 14:28:19 -07002771 if (
2772#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu0c634c72017-06-19 07:06:28 -07002773 is_inter_anyref_comp_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002774#else // !CONFIG_COMPOUND_SINGLEREF
2775 cm->reference_mode != SINGLE_REFERENCE &&
Sarah Parker6fdc8532016-11-16 17:47:13 -08002776 is_inter_compound_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002777#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chencb60b182016-10-13 15:18:22 -07002778#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002779 && mbmi->motion_mode == SIMPLE_TRANSLATION
Yue Chencb60b182016-10-13 15:18:22 -07002780#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002781 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002782 if (is_any_masked_compound_used(bsize)) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002783#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002784 if (cm->allow_masked_compound) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002785#if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2786 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
2787 mbmi->interinter_compound_type =
2788 aom_read_bit(r, ACCT_STR) ? COMPOUND_AVERAGE : COMPOUND_SEG;
2789 else
2790#endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2791 mbmi->interinter_compound_type = aom_read_symbol(
2792 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002793#if CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002794 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002795 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002796 mbmi->wedge_index =
2797 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2798 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2799 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002800#endif // CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002801#if CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002802 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
2803 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
2804 }
Sarah Parker42d96102017-01-31 21:05:27 -08002805#endif // CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002806 }
2807#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002808 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002809 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002810 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002811 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002812 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002813 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814
Yue Chen19e7aa82016-11-30 14:05:39 -08002815#if CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002816 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002817#endif // CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07002818
Di Chen56586622017-06-09 13:49:44 -07002819#if DEC_MISMATCH_DEBUG
2820 // NOTE(zoeliu): For debug
Zoe Liuf9333f52017-07-03 10:52:01 -07002821 dec_dump_logs(cm, mi, xd, mi_row, mi_col, inter_mode_ctx, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002822#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823}
2824
Yaowu Xuf883b422016-08-30 14:01:10 -07002825static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826 MACROBLOCKD *const xd,
2827#if CONFIG_SUPERTX
2828 int supertx_enabled,
2829#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002830 int mi_row, int mi_col, aom_reader *r) {
2831 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002832 MODE_INFO *const mi = xd->mi[0];
2833 MB_MODE_INFO *const mbmi = &mi->mbmi;
2834 int inter_block = 1;
2835#if CONFIG_VAR_TX
2836 BLOCK_SIZE bsize = mbmi->sb_type;
2837#endif // CONFIG_VAR_TX
2838
2839 mbmi->mv[0].as_int = 0;
2840 mbmi->mv[1].as_int = 0;
2841 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
2842#if CONFIG_SUPERTX
David Barker3aec8d62017-01-31 14:55:32 +00002843 if (!supertx_enabled)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002844#endif // CONFIG_SUPERTX
2845 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
David Barker3aec8d62017-01-31 14:55:32 +00002846
David Barker3aec8d62017-01-31 14:55:32 +00002847 if (cm->delta_q_present_flag) {
2848 xd->current_qindex =
2849 xd->prev_qindex +
2850 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01002851 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2852 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
David Barker3aec8d62017-01-31 14:55:32 +00002853 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07002854#if CONFIG_EXT_DELTA_Q
2855 if (cm->delta_lf_present_flag) {
2856 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2857 xd->prev_delta_lf_from_base +
2858 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
2859 cm->delta_lf_res;
2860 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
2861 }
2862#endif
David Barker3aec8d62017-01-31 14:55:32 +00002863 }
David Barker3aec8d62017-01-31 14:55:32 +00002864
2865#if CONFIG_SUPERTX
2866 if (!supertx_enabled) {
2867#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002868 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
2869
2870#if CONFIG_VAR_TX
Jingning Han331662e2017-05-30 17:03:32 -07002871 xd->above_txfm_context =
2872 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2873 xd->left_txfm_context = xd->left_txfm_context_buffer +
2874 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002875
2876 if (cm->tx_mode == TX_MODE_SELECT &&
2877#if CONFIG_CB4X4
Jingning Han3daa4fd2017-01-20 10:33:50 -08002878 bsize > BLOCK_4X4 &&
Jingning Han581d1692017-01-05 16:03:54 -08002879#else
2880 bsize >= BLOCK_8X8 &&
2881#endif
David Barker16c64e32017-08-23 16:54:59 +01002882 !mbmi->skip && inter_block && !xd->lossless[mbmi->segment_id]) {
Jingning Han70e5f3f2016-11-09 17:03:07 -08002883 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07002884 const int bh = tx_size_high_unit[max_tx_size];
2885 const int bw = tx_size_wide_unit[max_tx_size];
Jingning Han65abc312016-10-27 13:04:21 -07002886 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2887 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002888 int idx, idy;
Yue Chena1e48dc2016-08-29 17:29:33 -07002889
Jingning Hanfe45b212016-11-22 10:30:23 -08002890 mbmi->min_tx_size = TX_SIZES_ALL;
2891 for (idy = 0; idy < height; idy += bh)
2892 for (idx = 0; idx < width; idx += bw)
2893 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size,
2894 height != width, idy, idx, r);
Yue Chend6bdd462017-07-19 16:05:43 -07002895#if CONFIG_RECT_TX_EXT
2896 if (is_quarter_tx_allowed(xd, mbmi, inter_block) &&
2897 mbmi->tx_size == max_tx_size) {
2898 int quarter_tx;
2899
2900 if (quarter_txsize_lookup[bsize] != max_tx_size) {
2901 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
2902 if (xd->counts) ++xd->counts->quarter_tx_size[quarter_tx];
2903 } else {
2904 quarter_tx = 1;
2905 }
2906 if (quarter_tx) {
2907 mbmi->tx_size = quarter_txsize_lookup[bsize];
2908 for (idy = 0; idy < tx_size_high_unit[max_tx_size] / 2; ++idy)
2909 for (idx = 0; idx < tx_size_wide_unit[max_tx_size] / 2; ++idx)
2910 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
2911 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
2912 }
2913 }
2914#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002915 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -08002916 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002917
2918 if (inter_block) {
Jingning Han9ca05b72017-01-03 14:41:36 -08002919 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2920 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921 int idx, idy;
2922 for (idy = 0; idy < height; ++idy)
2923 for (idx = 0; idx < width; ++idx)
2924 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
2925 }
Jingning Hane67b38a2016-11-04 10:30:00 -07002926 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han1b1dc932016-11-09 10:55:30 -08002927 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002928 }
2929#else
Urvang Joshifeb925f2016-12-05 10:37:29 -08002930 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002931#endif // CONFIG_VAR_TX
2932#if CONFIG_SUPERTX
2933 }
2934#if CONFIG_VAR_TX
2935 else if (inter_block) {
2936 const int width = num_4x4_blocks_wide_lookup[bsize];
2937 const int height = num_4x4_blocks_high_lookup[bsize];
2938 int idx, idy;
2939 xd->mi[0]->mbmi.tx_size = xd->supertx_size;
2940 for (idy = 0; idy < height; ++idy)
2941 for (idx = 0; idx < width; ++idx)
2942 xd->mi[0]->mbmi.inter_tx_size[idy >> 1][idx >> 1] = xd->supertx_size;
2943 }
2944#endif // CONFIG_VAR_TX
2945#endif // CONFIG_SUPERTX
2946
2947 if (inter_block)
2948 read_inter_block_mode_info(pbi, xd,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002949#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950 mi, mi_row, mi_col, r, supertx_enabled);
2951#else
2952 mi, mi_row, mi_col, r);
Yue Chencb60b182016-10-13 15:18:22 -07002953#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954 else
Jingning Han36fe3202017-02-20 22:31:49 -08002955 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002956
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002957#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07002958 av1_read_tx_type(cm, xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07002960 supertx_enabled,
Nathan E. Egge93878c42016-05-03 10:01:32 -04002961#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07002962 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002963#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002964}
2965
Yaowu Xuf883b422016-08-30 14:01:10 -07002966void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002967#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002970 int mi_row, int mi_col, aom_reader *r, int x_mis,
2971 int y_mis) {
2972 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002973 MODE_INFO *const mi = xd->mi[0];
Jingning Hanba070c52017-09-02 09:33:06 -07002974 MV_REF *frame_mvs =
2975 cm->cur_frame->mvs + (mi_row & 0xfffe) * cm->mi_cols + (mi_col & 0xfffe);
2976 x_mis = AOMMAX(x_mis, 2);
2977 y_mis = AOMMAX(y_mis, 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002978 int w, h;
2979
Alex Converse28744302017-04-13 14:46:22 -07002980#if CONFIG_INTRABC
2981 mi->mbmi.use_intrabc = 0;
2982#endif // CONFIG_INTRABC
2983
Yaowu Xuc27fc142016-08-22 16:08:15 -07002984 if (frame_is_intra_only(cm)) {
2985 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002986 for (h = 0; h < y_mis; ++h) {
2987 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2988 for (w = 0; w < x_mis; ++w) {
2989 MV_REF *const mv = frame_mv + w;
Emil Keyder01770b32017-01-20 18:03:11 -05002990 mv->ref_frame[0] = NONE_FRAME;
2991 mv->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002992 }
2993 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002994 } else {
2995 read_inter_frame_mode_info(pbi, xd,
2996#if CONFIG_SUPERTX
2997 supertx_enabled,
2998#endif // CONFIG_SUPERTX
2999 mi_row, mi_col, r);
3000 for (h = 0; h < y_mis; ++h) {
3001 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
3002 for (w = 0; w < x_mis; ++w) {
3003 MV_REF *const mv = frame_mv + w;
3004 mv->ref_frame[0] = mi->mbmi.ref_frame[0];
3005 mv->ref_frame[1] = mi->mbmi.ref_frame[1];
3006 mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
3007 mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07003008 mv->pred_mv[0].as_int = mi->mbmi.pred_mv[0].as_int;
3009 mv->pred_mv[1].as_int = mi->mbmi.pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010 }
3011 }
3012 }
3013}