blob: 75902002ace6a9676ec941d29298111be6930ba2 [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 +010043#if CONFIG_DELTA_Q
44static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
45 MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
46 FRAME_COUNTS *counts = xd->counts;
47 int sign, abs, reduced_delta_qindex = 0;
48 BLOCK_SIZE bsize = mbmi->sb_type;
49 const int b_col = mi_col & MAX_MIB_MASK;
50 const int b_row = mi_row & MAX_MIB_MASK;
51 const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000052 int rem_bits, thr;
53 int i, smallval;
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000054 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
55 (void)cm;
Thomas Daviesf6936102016-09-05 16:51:31 +010056
Alex Converse68abef82017-03-23 14:50:33 -070057 if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000058 abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000059 smallval = (abs < DELTA_Q_SMALL);
60 if (counts) {
61 for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
62 if (smallval) counts->delta_q[abs][0]++;
63 }
64
65 if (!smallval) {
Thomas Daviesf6936102016-09-05 16:51:31 +010066 rem_bits = aom_read_literal(r, 3, ACCT_STR);
67 thr = (1 << rem_bits) + 1;
68 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
69 }
70
71 if (abs) {
72 sign = aom_read_bit(r, ACCT_STR);
73 } else {
74 sign = 1;
75 }
76
77 reduced_delta_qindex = sign ? -abs : abs;
78 }
79 return reduced_delta_qindex;
80}
Fangwen Fu231fe422017-04-24 17:52:29 -070081#if CONFIG_EXT_DELTA_Q
82static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
83 MB_MODE_INFO *const mbmi, int mi_col,
84 int mi_row) {
85 FRAME_COUNTS *counts = xd->counts;
86 int sign, abs, reduced_delta_lflevel = 0;
87 BLOCK_SIZE bsize = mbmi->sb_type;
88 const int b_col = mi_col & MAX_MIB_MASK;
89 const int b_row = mi_row & MAX_MIB_MASK;
90 const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
91 int rem_bits, thr;
92 int i, smallval;
Fangwen Fu231fe422017-04-24 17:52:29 -070093 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
94 (void)cm;
Fangwen Fu231fe422017-04-24 17:52:29 -070095
96 if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_lf_flag) {
Fangwen Fu231fe422017-04-24 17:52:29 -070097 abs =
98 aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
Fangwen Fu231fe422017-04-24 17:52:29 -070099 smallval = (abs < DELTA_LF_SMALL);
100 if (counts) {
101 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
102 if (smallval) counts->delta_lf[abs][0]++;
103 }
104 if (!smallval) {
105 rem_bits = aom_read_literal(r, 3, ACCT_STR);
106 thr = (1 << rem_bits) + 1;
107 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
108 }
109
110 if (abs) {
111 sign = aom_read_bit(r, ACCT_STR);
112 } else {
113 sign = 1;
114 }
115
116 reduced_delta_lflevel = sign ? -abs : abs;
117 }
118 return reduced_delta_lflevel;
119}
120#endif
Thomas Daviesf6936102016-09-05 16:51:31 +0100121#endif
122
Nathan E. Eggea1f80e32017-05-23 11:52:32 -0400123static PREDICTION_MODE read_intra_mode_y(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700124 aom_reader *r, int size_group) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 const PREDICTION_MODE y_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000126 read_intra_mode(r, ec_ctx->y_mode_cdf[size_group]);
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400127#if CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 FRAME_COUNTS *counts = xd->counts;
129 if (counts) ++counts->y_mode[size_group][y_mode];
Nathan E. Egge5bb3a742017-06-30 12:47:43 -0400130#else
131 /* TODO(negge): Can we remove this parameter? */
132 (void)xd;
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400133#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134 return y_mode;
135}
136
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400137static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
138 MACROBLOCKD *xd, aom_reader *r,
139 PREDICTION_MODE y_mode) {
140 const UV_PREDICTION_MODE uv_mode =
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400141#if CONFIG_CFL
142 aom_read_symbol(r, ec_ctx->uv_mode_cdf[y_mode], UV_INTRA_MODES, ACCT_STR);
143#else
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000144 read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400145#endif // CONFIG_CFL
146
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400147#if CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700148 FRAME_COUNTS *counts = xd->counts;
149 if (counts) ++counts->uv_mode[y_mode][uv_mode];
Nathan E. Egge5bb3a742017-06-30 12:47:43 -0400150#else
151 /* TODO(negge): Can we remove this parameter? */
152 (void)xd;
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400153#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154 return uv_mode;
155}
156
Luc Trudeauf5334002017-04-25 12:21:26 -0400157#if CONFIG_CFL
David Michael Barr23198662017-06-19 23:19:48 +0900158static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
David Michael Barrf6eaa152017-07-19 19:42:28 +0900159 int *signs_out) {
160 const int joint_sign =
161 aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
162 int idx = 0;
163 // Magnitudes are only coded for nonzero values
164 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
165 aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
166 idx = aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
167 << CFL_ALPHABET_SIZE_LOG2;
168 }
169 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
170 aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
171 idx += aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
172 }
173 *signs_out = joint_sign;
174 return idx;
Luc Trudeauf5334002017-04-25 12:21:26 -0400175}
176#endif
177
Yue Chen4d26acb2017-05-01 12:28:34 -0700178#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700179static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
180 aom_reader *r, int size_group) {
Thomas Davies299ff042017-06-27 13:41:59 +0100181 (void)cm;
182 const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
183 r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
184 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 FRAME_COUNTS *counts = xd->counts;
186 if (counts) ++counts->interintra_mode[size_group][ii_mode];
187 return ii_mode;
188}
Yue Chen4d26acb2017-05-01 12:28:34 -0700189#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190
Thomas Davies1de6c882017-01-11 17:47:49 +0000191static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700192 aom_reader *r, int16_t ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 FRAME_COUNTS *counts = xd->counts;
194 int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100195 int is_newmv, is_zeromv, is_refmv;
196#if CONFIG_NEW_MULTISYMBOL
197 is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
198#else
199 is_newmv = aom_read(r, ec_ctx->newmv_prob[mode_ctx], ACCT_STR) == 0;
200#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201
Thomas Davies149eda52017-06-12 18:11:55 +0100202 if (is_newmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 if (counts) ++counts->newmv_mode[mode_ctx][0];
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700204 return NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700205 }
206 if (counts) ++counts->newmv_mode[mode_ctx][1];
207
208 if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
209
210 mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
211
Thomas Davies149eda52017-06-12 18:11:55 +0100212#if CONFIG_NEW_MULTISYMBOL
213 is_zeromv =
214 aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
215#else
216 is_zeromv = aom_read(r, ec_ctx->zeromv_prob[mode_ctx], ACCT_STR) == 0;
217#endif
218 if (is_zeromv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 if (counts) ++counts->zeromv_mode[mode_ctx][0];
220 return ZEROMV;
221 }
222 if (counts) ++counts->zeromv_mode[mode_ctx][1];
223
224 mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
225
226 if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
227 if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
228 if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
229
Thomas Davies149eda52017-06-12 18:11:55 +0100230#if CONFIG_NEW_MULTISYMBOL
231 is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
232#else
233 is_refmv = aom_read(r, ec_ctx->refmv_prob[mode_ctx], ACCT_STR) == 0;
234#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235
Thomas Davies149eda52017-06-12 18:11:55 +0100236 if (is_refmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237 if (counts) ++counts->refmv_mode[mode_ctx][0];
238
239 return NEARESTMV;
240 } else {
241 if (counts) ++counts->refmv_mode[mode_ctx][1];
242 return NEARMV;
243 }
244
245 // Invalid prediction mode.
246 assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247}
248
Thomas Davies149eda52017-06-12 18:11:55 +0100249static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 MB_MODE_INFO *mbmi, aom_reader *r) {
251 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252 mbmi->ref_mv_idx = 0;
253
Yushin Cho127c5832017-07-28 16:39:04 -0700254 if (mbmi->mode == NEWMV
David Barker404b2e82017-03-27 13:07:47 +0100255#if CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -0700256 || mbmi->mode == NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700257#if CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700258 || mbmi->mode == SR_NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700259#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -0700260#endif // CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -0700261 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262 int idx;
263 for (idx = 0; idx < 2; ++idx) {
264 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700265 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100266#if CONFIG_NEW_MULTISYMBOL
267 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
268#else
269 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
270#endif
271 mbmi->ref_mv_idx = idx + drl_idx;
272 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
273 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 }
275 }
276 }
277
David Barker3dfba992017-04-03 16:10:09 +0100278 if (have_nearmv_in_inter_mode(mbmi->mode)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279 int idx;
280 // Offset the NEARESTMV mode.
281 // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
282 // mode is factored in.
283 for (idx = 1; idx < 3; ++idx) {
284 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100286#if CONFIG_NEW_MULTISYMBOL
287 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
288#else
289 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
290#endif
291 mbmi->ref_mv_idx = idx + drl_idx - 1;
292 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
293 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 }
295 }
296 }
297}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298
Yaowu Xub24e1152016-10-31 16:28:32 -0700299#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
300static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -0800301 MODE_INFO *mi, aom_reader *r) {
302 MB_MODE_INFO *mbmi = &mi->mbmi;
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700303#if CONFIG_NEW_MULTISYMBOL || CONFIG_NCOBMC_ADAPT_WEIGHT
Thomas Davies04e5aa72017-06-28 14:36:39 +0100304 (void)cm;
305#endif
306
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700307#if CONFIG_NCOBMC_ADAPT_WEIGHT
308 const MOTION_MODE last_motion_mode_allowed =
309 motion_mode_allowed_wrapper(0,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700310#if CONFIG_GLOBAL_MOTION
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700311 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700312#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -0700313#if CONFIG_WARPED_MOTION
314 xd,
315#endif
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700316 mi);
317#else
Sarah Parker19234cc2017-03-10 16:43:25 -0800318 const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
Sarah Parker0eea89f2017-07-11 11:56:36 -0700319#if CONFIG_GLOBAL_MOTION
Sarah Parker19234cc2017-03-10 16:43:25 -0800320 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700321#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -0700322#if CONFIG_WARPED_MOTION
323 xd,
324#endif
Sarah Parker19234cc2017-03-10 16:43:25 -0800325 mi);
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700326#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yue Chen69f18e12016-09-08 14:48:15 -0700327 int motion_mode;
328 FRAME_COUNTS *counts = xd->counts;
Yaowu Xub24e1152016-10-31 16:28:32 -0700329
Yue Chen69f18e12016-09-08 14:48:15 -0700330 if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
331#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
332 if (last_motion_mode_allowed == OBMC_CAUSAL) {
Thomas Daviesd9b57262017-06-27 17:43:25 +0100333#if CONFIG_NEW_MULTISYMBOL
334 motion_mode =
335 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
336#else
Yue Chen69f18e12016-09-08 14:48:15 -0700337 motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
Thomas Daviesd9b57262017-06-27 17:43:25 +0100338#endif
Yue Chen69f18e12016-09-08 14:48:15 -0700339 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
340 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
341 } else {
342#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700343 motion_mode =
Thomas Davies04e5aa72017-06-28 14:36:39 +0100344 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
345 MOTION_MODES, ACCT_STR);
Yaowu Xub24e1152016-10-31 16:28:32 -0700346 if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
347 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yue Chen69f18e12016-09-08 14:48:15 -0700348#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700349 }
Yue Chen69f18e12016-09-08 14:48:15 -0700350#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700351}
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700352
353#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -0700354static void read_ncobmc_mode(MACROBLOCKD *xd, MODE_INFO *mi,
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700355 NCOBMC_MODE ncobmc_mode[2], aom_reader *r) {
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700356 MB_MODE_INFO *mbmi = &mi->mbmi;
357 FRAME_COUNTS *counts = xd->counts;
358 ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];
Wei-Ting Lin77c41182017-07-12 15:58:35 -0700359 if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) return;
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700360
Wei-Ting Linca710d62017-07-13 11:41:02 -0700361 ncobmc_mode[0] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
362 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700363 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0]];
364
365 if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
Wei-Ting Linca710d62017-07-13 11:41:02 -0700366 ncobmc_mode[1] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
367 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700368 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1]];
369 }
370}
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700371#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yaowu Xub24e1152016-10-31 16:28:32 -0700372#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
373
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700375static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
376 aom_reader *r, int16_t ctx) {
Thomas Davies8c08a332017-06-26 17:30:34 +0100377 (void)cm;
378 const int mode =
379 aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
380 INTER_COMPOUND_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 FRAME_COUNTS *counts = xd->counts;
382
383 if (counts) ++counts->inter_compound_mode[ctx][mode];
384
385 assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
386 return NEAREST_NEARESTMV + mode;
387}
Zoe Liu85b66462017-04-20 14:28:19 -0700388
389#if CONFIG_COMPOUND_SINGLEREF
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100390static PREDICTION_MODE read_inter_singleref_comp_mode(MACROBLOCKD *xd,
Zoe Liu85b66462017-04-20 14:28:19 -0700391 aom_reader *r,
392 int16_t ctx) {
393 const int mode =
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100394 aom_read_symbol(r, xd->tile_ctx->inter_singleref_comp_mode_cdf[ctx],
395 INTER_SINGLEREF_COMP_MODES, ACCT_STR);
Zoe Liu85b66462017-04-20 14:28:19 -0700396 FRAME_COUNTS *counts = xd->counts;
397
398 if (counts) ++counts->inter_singleref_comp_mode[ctx][mode];
399
400 assert(is_inter_singleref_comp_mode(SR_NEAREST_NEARMV + mode));
401 return SR_NEAREST_NEARMV + mode;
402}
403#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404#endif // CONFIG_EXT_INTER
405
Thomas9ac55082016-09-23 18:04:17 +0100406static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700407 return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408}
409
410#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700411static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700412 MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700413 TX_SIZE tx_size, int depth, int blk_row,
414 int blk_col, aom_reader *r) {
Thomas Davies985bfc32017-06-27 16:51:26 +0100415#if CONFIG_NEW_MULTISYMBOL
416 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
417 (void)cm;
418#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 int is_split = 0;
420 const int tx_row = blk_row >> 1;
421 const int tx_col = blk_col >> 1;
Jingning Hanf64062f2016-11-02 16:22:18 -0700422 const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
423 const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
Jingning Han331662e2017-05-30 17:03:32 -0700424 int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
425 xd->left_txfm_context + blk_row,
Jingning Hanc8b89362016-11-01 10:28:53 -0700426 mbmi->sb_type, tx_size);
clang-format67948d32016-09-07 22:40:40 -0700427 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 [MAX_MIB_SIZE] =
429 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700430 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
431
Jingning Han571189c2016-10-24 10:38:43 -0700432 if (depth == MAX_VARTX_DEPTH) {
Jingning Han94d5bfc2016-10-21 10:14:36 -0700433 int idx, idy;
434 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700435 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
436 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han94d5bfc2016-10-21 10:14:36 -0700437 inter_tx_size[idy][idx] = tx_size;
438 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700439 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Jingning Han94d5bfc2016-10-21 10:14:36 -0700440 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700441 txfm_partition_update(xd->above_txfm_context + blk_col,
442 xd->left_txfm_context + blk_row, tx_size, tx_size);
Jingning Han94d5bfc2016-10-21 10:14:36 -0700443 return;
444 }
445
Thomas Davies985bfc32017-06-27 16:51:26 +0100446#if CONFIG_NEW_MULTISYMBOL
447 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
448#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700449 is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
Thomas Davies985bfc32017-06-27 16:51:26 +0100450#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451
452 if (is_split) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700453 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
454 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700455 int i;
456
457 if (counts) ++counts->txfm_partition[ctx][1];
458
459 if (tx_size == TX_8X8) {
Jingning Han9ca05b72017-01-03 14:41:36 -0800460 int idx, idy;
Jingning Hanab9ecba2017-01-13 09:11:58 -0800461 inter_tx_size[0][0] = sub_txs;
Jingning Han9ca05b72017-01-03 14:41:36 -0800462 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
463 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han581d1692017-01-05 16:03:54 -0800464 inter_tx_size[idy][idx] = inter_tx_size[0][0];
Jingning Hanab9ecba2017-01-13 09:11:58 -0800465 mbmi->tx_size = sub_txs;
Jingning Hane67b38a2016-11-04 10:30:00 -0700466 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han331662e2017-05-30 17:03:32 -0700467 txfm_partition_update(xd->above_txfm_context + blk_col,
468 xd->left_txfm_context + blk_row, sub_txs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 return;
470 }
471
472 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473 for (i = 0; i < 4; ++i) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700474 int offsetr = blk_row + (i >> 1) * bsl;
475 int offsetc = blk_col + (i & 0x01) * bsl;
476 read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700477 offsetc, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700478 }
479 } else {
480 int idx, idy;
481 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700482 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
483 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484 inter_tx_size[idy][idx] = tx_size;
485 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700486 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700488 txfm_partition_update(xd->above_txfm_context + blk_col,
489 xd->left_txfm_context + blk_row, tx_size, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 }
491}
492#endif
493
Yaowu Xuf883b422016-08-30 14:01:10 -0700494static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
495 int tx_size_cat, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496 FRAME_COUNTS *counts = xd->counts;
497 const int ctx = get_tx_size_context(xd);
Thomas Davies15580c52017-03-09 13:53:42 +0000498 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
499 (void)cm;
Thomas Davies15580c52017-03-09 13:53:42 +0000500
Nathan E. Egge476c63c2017-05-18 18:35:16 -0400501 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
502 tx_size_cat + 2, ACCT_STR);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800503 const TX_SIZE tx_size = depth_to_tx_size(depth);
504#if CONFIG_RECT_TX
505 assert(!is_rect_tx(tx_size));
506#endif // CONFIG_RECT_TX
Jingning Han906be072016-10-26 11:04:31 -0700507 if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
Jingning Han4e1737a2016-10-25 16:05:02 -0700508 return tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509}
510
Urvang Joshifeb925f2016-12-05 10:37:29 -0800511static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
512 int allow_select_inter, aom_reader *r) {
513 const TX_MODE tx_mode = cm->tx_mode;
514 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700515 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Debargha Mukherjee428bbb22017-03-17 07:30:24 -0700516#if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
Yushin Cho127c5832017-07-28 16:39:04 -0700517 if (bsize > BLOCK_4X4)
Jingning Han4be1a4d2017-01-06 10:59:20 -0800518#else
Yushin Cho127c5832017-07-28 16:39:04 -0700519 if (bsize >= BLOCK_8X8)
Urvang Joshifeb925f2016-12-05 10:37:29 -0800520#endif // CONFIG_CB4X4 && CONFIG_VAR_TX
Yushin Cho127c5832017-07-28 16:39:04 -0700521 {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800522 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
523 const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
524 : intra_tx_size_cat_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700525 const TX_SIZE coded_tx_size =
Urvang Joshifeb925f2016-12-05 10:37:29 -0800526 read_selected_tx_size(cm, xd, tx_size_cat, r);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700527#if CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 if (coded_tx_size > max_txsize_lookup[bsize]) {
529 assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
Yue Chend6bdd462017-07-19 16:05:43 -0700530#if CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700531 if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
Yue Chend6bdd462017-07-19 16:05:43 -0700532 int quarter_tx;
Yue Chen56e226e2017-05-02 16:21:40 -0700533
Yue Chend6bdd462017-07-19 16:05:43 -0700534 if (quarter_txsize_lookup[bsize] != max_txsize_lookup[bsize]) {
535 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
536 FRAME_COUNTS *counts = xd->counts;
537
538 if (counts) ++counts->quarter_tx_size[quarter_tx];
539 } else {
540 quarter_tx = 1;
541 }
Yue Chen56e226e2017-05-02 16:21:40 -0700542 return quarter_tx ? quarter_txsize_lookup[bsize]
543 : max_txsize_rect_lookup[bsize];
544 }
Yue Chend6bdd462017-07-19 16:05:43 -0700545#endif // CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700546
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547 return max_txsize_rect_lookup[bsize];
548 }
Peter de Rivaza7c81462016-09-26 14:20:13 +0100549#else
550 assert(coded_tx_size <= max_txsize_lookup[bsize]);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700551#endif // CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 return coded_tx_size;
553 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800554 return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700555 }
556 } else {
557#if CONFIG_EXT_TX && CONFIG_RECT_TX
558 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
559 return max_txsize_rect_lookup[bsize];
560#else
561 return TX_4X4;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800562#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563 }
564}
565
Yaowu Xuf883b422016-08-30 14:01:10 -0700566static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700567 int mi_offset, int x_mis, int y_mis) {
568 int x, y, segment_id = INT_MAX;
569
570 for (y = 0; y < y_mis; y++)
571 for (x = 0; x < x_mis; x++)
572 segment_id =
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574
575 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
576 return segment_id;
577}
578
Yaowu Xuf883b422016-08-30 14:01:10 -0700579static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700580 int segment_id) {
581 int x, y;
582
583 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
584
585 for (y = 0; y < y_mis; y++)
586 for (x = 0; x < x_mis; x++)
587 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
588}
589
Yaowu Xuf883b422016-08-30 14:01:10 -0700590static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591 int mi_offset, int x_mis, int y_mis,
Yaowu Xuf883b422016-08-30 14:01:10 -0700592 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593 struct segmentation *const seg = &cm->seg;
594 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100595 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100596 struct segmentation_probs *const segp = &ec_ctx->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597 int segment_id;
598
599 if (!seg->enabled) return 0; // Default for disabled segmentation
600
601 assert(seg->update_map && !seg->temporal_update);
602
603 segment_id = read_segment_id(r, segp);
604 if (counts) ++counts->seg.tree_total[segment_id];
605 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
606 return segment_id;
607}
608
Yaowu Xuf883b422016-08-30 14:01:10 -0700609static void copy_segment_id(const AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610 const uint8_t *last_segment_ids,
611 uint8_t *current_segment_ids, int mi_offset,
612 int x_mis, int y_mis) {
613 int x, y;
614
615 for (y = 0; y < y_mis; y++)
616 for (x = 0; x < x_mis; x++)
617 current_segment_ids[mi_offset + y * cm->mi_cols + x] =
618 last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
619 : 0;
620}
621
Yaowu Xuf883b422016-08-30 14:01:10 -0700622static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
623 int mi_row, int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624 struct segmentation *const seg = &cm->seg;
625 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100626 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100627 struct segmentation_probs *const segp = &ec_ctx->seg;
628
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
630 int predicted_segment_id, segment_id;
631 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Hanc709e1f2016-12-06 14:48:09 -0800632 const int bw = mi_size_wide[mbmi->sb_type];
633 const int bh = mi_size_high[mbmi->sb_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634
635 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
637 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638
639 if (!seg->enabled) return 0; // Default for disabled segmentation
640
641 predicted_segment_id = cm->last_frame_seg_map
642 ? dec_get_segment_id(cm, cm->last_frame_seg_map,
643 mi_offset, x_mis, y_mis)
644 : 0;
645
646 if (!seg->update_map) {
647 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
648 mi_offset, x_mis, y_mis);
649 return predicted_segment_id;
650 }
651
652 if (seg->temporal_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700653 const int ctx = av1_get_pred_context_seg_id(xd);
Thomas Davies00021352017-07-11 16:07:55 +0100654#if CONFIG_NEW_MULTISYMBOL
655 aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
656 mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
657#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700659 mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
Thomas Davies00021352017-07-11 16:07:55 +0100660#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661 if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
662 if (mbmi->seg_id_predicted) {
663 segment_id = predicted_segment_id;
664 } else {
665 segment_id = read_segment_id(r, segp);
666 if (counts) ++counts->seg.tree_mispred[segment_id];
667 }
668 } else {
669 segment_id = read_segment_id(r, segp);
670 if (counts) ++counts->seg.tree_total[segment_id];
671 }
672 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
673 return segment_id;
674}
675
Yaowu Xuf883b422016-08-30 14:01:10 -0700676static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
677 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700678 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
679 return 1;
680 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700681 const int ctx = av1_get_skip_context(xd);
Thomas Davies61e3e372017-04-04 16:10:23 +0100682#if CONFIG_NEW_MULTISYMBOL
683 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
684 const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
685#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700686 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Thomas Davies61e3e372017-04-04 16:10:23 +0100687#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688 FRAME_COUNTS *counts = xd->counts;
689 if (counts) ++counts->skip[ctx][skip];
690 return skip;
691 }
692}
693
hui su33567b22017-04-30 16:40:19 -0700694#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700695static int uint16_compare(const void *a, const void *b) {
Urvang Joshi0ba850e2017-05-12 17:05:45 -0700696 const uint16_t va = *(const uint16_t *)a;
697 const uint16_t vb = *(const uint16_t *)b;
hui su33567b22017-04-30 16:40:19 -0700698 return va - vb;
699}
hui su33567b22017-04-30 16:40:19 -0700700
701static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
702 PALETTE_MODE_INFO *const pmi, aom_reader *r) {
703 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
704 const MODE_INFO *const above_mi = xd->above_mi;
705 const MODE_INFO *const left_mi = xd->left_mi;
706 const int n_cache = av1_get_palette_cache(above_mi, left_mi, 0, color_cache);
707 const int n = pmi->palette_size[0];
708 int idx = 0;
709 for (int i = 0; i < n_cache && idx < n; ++i)
710 if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
711 if (idx < n) {
712 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
713 if (idx < n) {
714 const int min_bits = bit_depth - 3;
715 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
716 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
717 for (; idx < n; ++idx) {
718 const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
719 pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
720 range -= delta;
721 bits = AOMMIN(bits, av1_ceil_log2(range));
722 }
723 }
724 }
hui su33567b22017-04-30 16:40:19 -0700725 qsort(pmi->palette_colors, n, sizeof(pmi->palette_colors[0]), uint16_compare);
hui su33567b22017-04-30 16:40:19 -0700726}
727
728static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
729 PALETTE_MODE_INFO *const pmi,
730 aom_reader *r) {
731 const int n = pmi->palette_size[1];
732 // U channel colors.
733 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
734 const MODE_INFO *const above_mi = xd->above_mi;
735 const MODE_INFO *const left_mi = xd->left_mi;
736 const int n_cache = av1_get_palette_cache(above_mi, left_mi, 1, color_cache);
737 int idx = PALETTE_MAX_SIZE;
738 for (int i = 0; i < n_cache && idx < PALETTE_MAX_SIZE + n; ++i)
739 if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
740 if (idx < PALETTE_MAX_SIZE + n) {
741 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
742 if (idx < PALETTE_MAX_SIZE + n) {
743 const int min_bits = bit_depth - 3;
744 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
745 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
746 for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
747 const int delta = aom_read_literal(r, bits, ACCT_STR);
748 pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
749 range -= delta;
750 bits = AOMMIN(bits, av1_ceil_log2(range));
751 }
752 }
753 }
hui su33567b22017-04-30 16:40:19 -0700754 qsort(pmi->palette_colors + PALETTE_MAX_SIZE, n,
755 sizeof(pmi->palette_colors[0]), uint16_compare);
hui su33567b22017-04-30 16:40:19 -0700756
757 // V channel colors.
758 if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
759 const int min_bits_v = bit_depth - 4;
760 const int max_val = 1 << bit_depth;
761 int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
762 pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
763 aom_read_literal(r, bit_depth, ACCT_STR);
764 for (int i = 1; i < n; ++i) {
765 int delta = aom_read_literal(r, bits, ACCT_STR);
766 if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
767 int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
768 if (val < 0) val += max_val;
769 if (val >= max_val) val -= max_val;
770 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
771 }
772 } else {
773 for (int i = 0; i < n; ++i) {
774 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
775 aom_read_literal(r, bit_depth, ACCT_STR);
776 }
777 }
778}
779#endif // CONFIG_PALETTE_DELTA_ENCODING
780
Yaowu Xuf883b422016-08-30 14:01:10 -0700781static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
782 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783 MODE_INFO *const mi = xd->mi[0];
784 MB_MODE_INFO *const mbmi = &mi->mbmi;
785 const MODE_INFO *const above_mi = xd->above_mi;
786 const MODE_INFO *const left_mi = xd->left_mi;
787 const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
789
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100790 assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST);
791 const int block_palette_idx = bsize - BLOCK_8X8;
792
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793 if (mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800794 int palette_y_mode_ctx = 0;
hui su40b9e7f2017-07-13 18:15:56 -0700795 if (above_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800796 palette_y_mode_ctx +=
797 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700798 }
799 if (left_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800800 palette_y_mode_ctx +=
801 (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700802 }
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100803 if (aom_read(r, av1_default_palette_y_mode_prob[block_palette_idx]
Urvang Joshi23a61112017-01-30 14:59:27 -0800804 [palette_y_mode_ctx],
805 ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 pmi->palette_size[0] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100807 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100808 xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100809 PALETTE_SIZES, ACCT_STR) +
810 2;
hui sud13c24a2017-04-07 16:13:07 -0700811#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700812 read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700813#else
hui su40b9e7f2017-07-13 18:15:56 -0700814 for (int i = 0; i < pmi->palette_size[0]; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700815 pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui sud13c24a2017-04-07 16:13:07 -0700816#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817 }
818 }
819
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400820 if (mbmi->uv_mode == UV_DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800821 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
822 if (aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
Michael Bebenita6048d052016-08-25 14:40:54 -0700823 ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 pmi->palette_size[1] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100825 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100826 xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100827 PALETTE_SIZES, ACCT_STR) +
828 2;
hui sud13c24a2017-04-07 16:13:07 -0700829#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700830 read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700831#else
hui su40b9e7f2017-07-13 18:15:56 -0700832 for (int i = 0; i < pmi->palette_size[1]; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700834 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700836 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 }
hui sud13c24a2017-04-07 16:13:07 -0700838#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 }
840 }
841}
842
hui su5db97432016-10-14 16:10:14 -0700843#if CONFIG_FILTER_INTRA
844static void read_filter_intra_mode_info(AV1_COMMON *const cm,
Jingning Han62946d12017-05-26 11:29:30 -0700845 MACROBLOCKD *const xd, int mi_row,
846 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847 MODE_INFO *const mi = xd->mi[0];
848 MB_MODE_INFO *const mbmi = &mi->mbmi;
849 FRAME_COUNTS *counts = xd->counts;
hui su5db97432016-10-14 16:10:14 -0700850 FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
851 &mbmi->filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852
Urvang Joshic6300aa2017-06-01 14:46:23 -0700853 if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0) {
hui su5db97432016-10-14 16:10:14 -0700854 filter_intra_mode_info->use_filter_intra_mode[0] =
855 aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
856 if (filter_intra_mode_info->use_filter_intra_mode[0]) {
857 filter_intra_mode_info->filter_intra_mode[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700858 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 }
hui su5db97432016-10-14 16:10:14 -0700860 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800861 ++counts
862 ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
hui su5db97432016-10-14 16:10:14 -0700863 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 }
Jingning Han62946d12017-05-26 11:29:30 -0700865
866#if CONFIG_CB4X4
867 if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
868 xd->plane[1].subsampling_x,
869 xd->plane[1].subsampling_y))
870 return;
hui sub4ed1492017-05-31 17:25:42 -0700871#else
872 (void)mi_row;
873 (void)mi_col;
874#endif // CONFIG_CB4X4
Jingning Han62946d12017-05-26 11:29:30 -0700875
Urvang Joshic6300aa2017-06-01 14:46:23 -0700876 if (mbmi->uv_mode == UV_DC_PRED &&
877 mbmi->palette_mode_info.palette_size[1] == 0) {
hui su5db97432016-10-14 16:10:14 -0700878 filter_intra_mode_info->use_filter_intra_mode[1] =
879 aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
880 if (filter_intra_mode_info->use_filter_intra_mode[1]) {
881 filter_intra_mode_info->filter_intra_mode[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700882 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883 }
hui su5db97432016-10-14 16:10:14 -0700884 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800885 ++counts
886 ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
hui su5db97432016-10-14 16:10:14 -0700887 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888 }
889}
hui su5db97432016-10-14 16:10:14 -0700890#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891
hui su5db97432016-10-14 16:10:14 -0700892#if CONFIG_EXT_INTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700893static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
894 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
896 const BLOCK_SIZE bsize = mbmi->sb_type;
hui sueda3d762016-12-06 16:58:23 -0800897#if CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -0800898 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 const int ctx = av1_get_pred_context_intra_interp(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900 int p_angle;
hui sueda3d762016-12-06 16:58:23 -0800901#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902
hui sueda3d762016-12-06 16:58:23 -0800903 (void)cm;
Joe Young830d4ce2017-05-30 17:48:13 -0700904
905 mbmi->angle_delta[0] = 0;
906 mbmi->angle_delta[1] = 0;
907
908 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700909
hui su45dc5972016-12-08 17:42:50 -0800910 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700912 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
hui sueda3d762016-12-06 16:58:23 -0800913#if CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -0700914 p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 if (av1_is_intra_filter_switchable(p_angle)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916 FRAME_COUNTS *counts = xd->counts;
hui sub4e25d22017-03-09 15:32:30 -0800917 mbmi->intra_filter = aom_read_symbol(r, ec_ctx->intra_filter_cdf[ctx],
918 INTRA_FILTERS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
920 } else {
921 mbmi->intra_filter = INTRA_FILTER_LINEAR;
922 }
hui sueda3d762016-12-06 16:58:23 -0800923#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 }
925
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400926 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700928 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929 }
930}
931#endif // CONFIG_EXT_INTRA
932
Angie Chianga9f9a312017-04-13 16:40:43 -0700933void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Hanab7163d2016-11-04 09:46:35 -0700934#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -0700935 int supertx_enabled,
Jingning Hanab7163d2016-11-04 09:46:35 -0700936#endif
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700937#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700938 int blk_row, int blk_col, int block, int plane,
939 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700940#endif
941 aom_reader *r) {
942 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700943 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700944#if !CONFIG_TXK_SEL
Jingning Hane67b38a2016-11-04 10:30:00 -0700945#if CONFIG_VAR_TX
946 const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
947#else
Jingning Hanab7163d2016-11-04 09:46:35 -0700948 const TX_SIZE tx_size = mbmi->tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700949#endif
Jingning Han243b66b2017-06-23 12:11:47 -0700950#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000951 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000952
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700953#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700954 TX_TYPE *tx_type = &mbmi->tx_type;
955#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700956 // only y plane's tx_type is transmitted
957 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700958 (void)block;
959 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << 4) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700960#endif
961
Jingning Hanab7163d2016-11-04 09:46:35 -0700962 if (!FIXED_TX_TYPE) {
963#if CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -0800964 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -0800965 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
966 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -0800967 ((!cm->seg.enabled && cm->base_qindex > 0) ||
968 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
969 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -0700970#if CONFIG_SUPERTX
971 !supertx_enabled &&
972#endif // CONFIG_SUPERTX
973 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Sarah Parkere68a3e42017-02-16 14:03:24 -0800974 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
975 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -0700976 // eset == 0 should correspond to a set with only DCT_DCT and
977 // there is no need to read the tx_type
978 assert(eset != 0);
Jingning Hanab7163d2016-11-04 09:46:35 -0700979 FRAME_COUNTS *counts = xd->counts;
980
981 if (inter_block) {
Sarah Parker784596d2017-06-23 08:41:26 -0700982 *tx_type = av1_ext_tx_inter_inv[eset][aom_read_symbol(
983 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
984 ext_tx_cnt_inter[eset], ACCT_STR)];
985 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -0700986 } else if (ALLOW_INTRA_EXT_TX) {
Sarah Parker784596d2017-06-23 08:41:26 -0700987 *tx_type = av1_ext_tx_intra_inv[eset][aom_read_symbol(
988 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
989 ext_tx_cnt_intra[eset], ACCT_STR)];
990 if (counts)
991 ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -0700992 }
993 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -0700994 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -0700995 }
996#else
Yue Cheneeacc4c2017-01-17 17:29:17 -0800997
998 if (tx_size < TX_32X32 &&
999 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1000 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1001 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001002#if CONFIG_SUPERTX
1003 !supertx_enabled &&
1004#endif // CONFIG_SUPERTX
1005 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1006 FRAME_COUNTS *counts = xd->counts;
Yue Cheneeacc4c2017-01-17 17:29:17 -08001007
Jingning Hanab7163d2016-11-04 09:46:35 -07001008 if (inter_block) {
Angie Chianga9f9a312017-04-13 16:40:43 -07001009 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001010 r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Angie Chianga9f9a312017-04-13 16:40:43 -07001011 if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -07001012 } else {
1013 const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
Angie Chianga9f9a312017-04-13 16:40:43 -07001014 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001015 r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Hanab7163d2016-11-04 09:46:35 -07001016 ACCT_STR)];
Angie Chianga9f9a312017-04-13 16:40:43 -07001017 if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -07001018 }
1019 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001020 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001021 }
1022#endif // CONFIG_EXT_TX
1023 }
Nathan E. Eggeebced372017-02-17 20:57:21 -05001024#if FIXED_TX_TYPE
1025 assert(mbmi->tx_type == DCT_DCT);
1026#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001027}
1028
Alex Converse28744302017-04-13 14:46:22 -07001029#if CONFIG_INTRABC
1030static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
1031 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001032 MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -07001033
1034static INLINE int is_mv_valid(const MV *mv);
1035
1036static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
1037 const int_mv *ref_mv, int mi_row, int mi_col,
1038 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -07001039 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1040 (void)cm;
Alex Converse28744302017-04-13 14:46:22 -07001041 FRAME_COUNTS *counts = xd->counts;
1042 nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse6b2584c2017-05-02 09:51:21 -07001043 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
1044 MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -07001045 int valid = is_mv_valid(&mv->as_mv) &&
1046 is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize);
Alex Converse28744302017-04-13 14:46:22 -07001047 return valid;
1048}
1049#endif // CONFIG_INTRABC
1050
Yaowu Xuf883b422016-08-30 14:01:10 -07001051static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001052 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001053 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001054 MODE_INFO *const mi = xd->mi[0];
1055 MB_MODE_INFO *const mbmi = &mi->mbmi;
1056 const MODE_INFO *above_mi = xd->above_mi;
1057 const MODE_INFO *left_mi = xd->left_mi;
1058 const BLOCK_SIZE bsize = mbmi->sb_type;
1059 int i;
1060 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001061 const int bw = mi_size_wide[bsize];
1062 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001063
1064 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001065 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1066 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001067 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001068
1069 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1070 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Arild Fuldseth07441162016-08-15 15:07:52 +02001071
1072#if CONFIG_DELTA_Q
1073 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001074 xd->current_qindex =
1075 xd->prev_qindex +
1076 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001077 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1078 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001079 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001080#if CONFIG_EXT_DELTA_Q
1081 if (cm->delta_lf_present_flag) {
1082 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1083 xd->prev_delta_lf_from_base +
1084 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1085 cm->delta_lf_res;
1086 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
1087 }
1088#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001089 }
1090#endif
1091
Yaowu Xuc27fc142016-08-22 16:08:15 -07001092 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001093 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001094
Alex Converse28744302017-04-13 14:46:22 -07001095#if CONFIG_INTRABC
1096 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools) {
Alex Converse7c412ea2017-06-01 15:16:22 -07001097 mbmi->use_intrabc = aom_read(r, ec_ctx->intrabc_prob, ACCT_STR);
Alex Converse28744302017-04-13 14:46:22 -07001098 if (mbmi->use_intrabc) {
Alex Conversef71808c2017-06-06 12:21:17 -07001099 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001100 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
Jingning Hand6c17d92017-04-24 16:33:12 -07001101#if CONFIG_DUAL_FILTER
1102 for (int idx = 0; idx < 4; ++idx) mbmi->interp_filter[idx] = BILINEAR;
1103#else
Alex Converse28744302017-04-13 14:46:22 -07001104 mbmi->interp_filter = BILINEAR;
Jingning Hand6c17d92017-04-24 16:33:12 -07001105#endif
Alex Converse44c2bad2017-05-11 09:36:10 -07001106
1107 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1108 int_mv ref_mvs[MAX_MV_REF_CANDIDATES] = {};
1109
1110 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
1111 xd->ref_mv_stack[INTRA_FRAME],
1112#if CONFIG_EXT_INTER
Yue Chenf03907a2017-05-31 12:04:04 -07001113 NULL,
Alex Converse44c2bad2017-05-11 09:36:10 -07001114#endif // CONFIG_EXT_INTER
1115 ref_mvs, mi_row, mi_col, NULL, NULL, inter_mode_ctx);
1116
1117 int_mv nearestmv, nearmv;
1118 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1119
1120 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1121 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
1122
Alex Converse28744302017-04-13 14:46:22 -07001123 xd->corrupted |=
1124 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
Alex Conversee16b2662017-05-24 14:00:00 -07001125#if CONFIG_VAR_TX
1126 // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks
1127 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1128 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1129 int idx, idy;
1130 for (idy = 0; idy < height; ++idy)
1131 for (idx = 0; idx < width; ++idx)
1132 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1133 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1134#endif // CONFIG_VAR_TX
Alex Conversedaa15e42017-05-02 14:27:16 -07001135#if CONFIG_EXT_TX && !CONFIG_TXK_SEL
1136 av1_read_tx_type(cm, xd,
1137#if CONFIG_SUPERTX
1138 0,
1139#endif
1140 r);
1141#endif // CONFIG_EXT_TX && !CONFIG_TXK_SEL
Alex Converse28744302017-04-13 14:46:22 -07001142 return;
1143 }
1144 }
1145#endif // CONFIG_INTRABC
1146
Alex Conversef71808c2017-06-06 12:21:17 -07001147 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
1148
Jingning Han52261842016-12-14 12:17:49 -08001149#if CONFIG_CB4X4
1150 (void)i;
1151 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001152 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Jingning Han52261842016-12-14 12:17:49 -08001153#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001154 switch (bsize) {
1155 case BLOCK_4X4:
1156 for (i = 0; i < 4; ++i)
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001157 mi->bmi[i].as_mode = read_intra_mode(
1158 r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, i));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001159 mbmi->mode = mi->bmi[3].as_mode;
1160 break;
1161 case BLOCK_4X8:
1162 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001163 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001165 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166 break;
1167 case BLOCK_8X4:
1168 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001169 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001170 mi->bmi[2].as_mode = mi->bmi[3].as_mode = 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, 2));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001172 break;
1173 default:
1174 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001175 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001176 }
Jingning Han52261842016-12-14 12:17:49 -08001177#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001178
Jingning Han36fe3202017-02-20 22:31:49 -08001179#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001180 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001181 xd->plane[1].subsampling_y)) {
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001182 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001183#else
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001184 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001185#endif
1186
Luc Trudeauf5334002017-04-25 12:21:26 -04001187#if CONFIG_CFL
Luc Trudeau2c317902017-04-28 11:06:50 -04001188 // TODO(ltrudeau) support PALETTE
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001189 if (mbmi->uv_mode == UV_CFL_PRED) {
David Michael Barrf6eaa152017-07-19 19:42:28 +09001190 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeauf5334002017-04-25 12:21:26 -04001191 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001192#endif // CONFIG_CFL
1193
1194#if CONFIG_CB4X4
Joe Young830d4ce2017-05-30 17:48:13 -07001195 } else {
1196 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001197 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauf5334002017-04-25 12:21:26 -04001198 }
1199#endif
1200
Yaowu Xuc27fc142016-08-22 16:08:15 -07001201#if CONFIG_EXT_INTRA
1202 read_intra_angle_info(cm, xd, r);
1203#endif // CONFIG_EXT_INTRA
1204 mbmi->palette_mode_info.palette_size[0] = 0;
1205 mbmi->palette_mode_info.palette_size[1] = 0;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +01001206 if (bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST &&
1207 cm->allow_screen_content_tools)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001208 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001209#if CONFIG_FILTER_INTRA
1210 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1211 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001212 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001213 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001214#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001215
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001216#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07001217 av1_read_tx_type(cm, xd,
Jingning Hanab7163d2016-11-04 09:46:35 -07001218#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07001219 0,
Nathan E. Egge72762a22016-09-07 17:12:07 -04001220#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07001221 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001222#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001223}
1224
Alex Converse6b2584c2017-05-02 09:51:21 -07001225static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
1226#if CONFIG_INTRABC
1227 int use_subpel,
1228#endif // CONFIG_INTRABC
1229 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001230 int mag, d, fr, hp;
Thomas Davies599395e2017-07-21 18:02:48 +01001231#if CONFIG_NEW_MULTISYMBOL
1232 const int sign = aom_read_bit(r, ACCT_STR);
1233#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001234 const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001235#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07001236 const int mv_class =
Nathan E. Egged7b893c2016-09-08 15:08:48 -04001237 aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001238 const int class0 = mv_class == MV_CLASS_0;
1239
1240 // Integer part
1241 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001242#if CONFIG_NEW_MULTISYMBOL
1243 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
1244#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -04001245 d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001246#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001247 mag = 0;
1248 } else {
1249 int i;
1250 const int n = mv_class + CLASS0_BITS - 1; // number of bits
1251
1252 d = 0;
Michael Bebenita6048d052016-08-25 14:40:54 -07001253 for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001254 mag = CLASS0_SIZE << (mv_class + 2);
1255 }
1256
Alex Converse6b2584c2017-05-02 09:51:21 -07001257#if CONFIG_INTRABC
1258 if (use_subpel) {
1259#endif // CONFIG_INTRABC
1260 // Fractional part
1261 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1262 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001263
Thomas Davies599395e2017-07-21 18:02:48 +01001264// High precision part (if hp is not used, the default value of the hp is 1)
1265#if CONFIG_NEW_MULTISYMBOL
1266 hp = usehp ? aom_read_symbol(
1267 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1268 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001269 : 1;
Thomas Davies599395e2017-07-21 18:02:48 +01001270#else
1271 hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
1272 : 1;
1273#endif
Alex Converse6b2584c2017-05-02 09:51:21 -07001274#if CONFIG_INTRABC
1275 } else {
1276 fr = 3;
1277 hp = 1;
1278 }
1279#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07001280
1281 // Result
1282 mag += ((d << 3) | (fr << 1) | hp) + 1;
1283 return sign ? -mag : mag;
1284}
1285
Yaowu Xuf883b422016-08-30 14:01:10 -07001286static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +01001287 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001288 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001289 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001290 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001291 joint_type =
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -04001292 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001293
1294 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001295 diff.row = read_mv_component(r, &ctx->comps[0],
1296#if CONFIG_INTRABC
1297 precision > MV_SUBPEL_NONE,
1298#endif // CONFIG_INTRABC
1299 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001300
1301 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001302 diff.col = read_mv_component(r, &ctx->comps[1],
1303#if CONFIG_INTRABC
1304 precision > MV_SUBPEL_NONE,
1305#endif // CONFIG_INTRABC
1306 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001307
Alex Converse6b2584c2017-05-02 09:51:21 -07001308 av1_inc_mv(&diff, counts, precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001309
1310 mv->row = ref->row + diff.row;
1311 mv->col = ref->col + diff.col;
1312}
1313
Yaowu Xuf883b422016-08-30 14:01:10 -07001314static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001315 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001316 aom_reader *r) {
Jingning Han6b064722017-05-01 09:48:18 -07001317#if !SUB8X8_COMP_REF
Jingning Han69d21012017-05-14 16:51:27 -07001318 if (xd->mi[0]->mbmi.sb_type == BLOCK_4X4) return SINGLE_REFERENCE;
Jingning Han6b064722017-05-01 09:48:18 -07001319#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001320 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001321 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001322#if CONFIG_NEW_MULTISYMBOL
1323 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1324 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
1325#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001326 const REFERENCE_MODE mode =
Michael Bebenita6048d052016-08-25 14:40:54 -07001327 (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001328#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001329 FRAME_COUNTS *counts = xd->counts;
1330 if (counts) ++counts->comp_inter[ctx][mode];
1331 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1332 } else {
1333 return cm->reference_mode;
1334 }
1335}
1336
Thomas Davies315f5782017-06-14 15:14:55 +01001337#if CONFIG_NEW_MULTISYMBOL
1338#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001339 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001340#else
1341#define READ_REF_BIT(pname) \
1342 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
1343#endif
1344
Zoe Liuc082bbc2017-05-17 13:31:37 -07001345#if CONFIG_EXT_COMP_REFS
1346static REFERENCE_MODE read_comp_reference_type(AV1_COMMON *cm,
1347 const MACROBLOCKD *xd,
1348 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001349 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001350#if USE_UNI_COMP_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07001351 COMP_REFERENCE_TYPE comp_ref_type;
1352#if CONFIG_VAR_REFS
1353 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm))
1354 if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm))
1355#endif // CONFIG_VAR_REFS
1356 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read(
1357 r, cm->fc->comp_ref_type_prob[ctx], ACCT_STR);
1358#if CONFIG_VAR_REFS
1359 else
1360 comp_ref_type = BIDIR_COMP_REFERENCE;
1361 else
1362 comp_ref_type = UNIDIR_COMP_REFERENCE;
1363#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001364#else // !USE_UNI_COMP_REFS
1365 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1366 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1367#endif // USE_UNI_COMP_REFS
1368 FRAME_COUNTS *counts = xd->counts;
1369 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1370 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1371}
1372#endif // CONFIG_EXT_COMP_REFS
1373
Yaowu Xuc27fc142016-08-22 16:08:15 -07001374// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001375static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1376 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001377 MV_REFERENCE_FRAME ref_frame[2]) {
Thomas Davies894cc812017-06-22 17:51:33 +01001378#if CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001379 FRAME_CONTEXT *const fc = cm->fc;
Thomas Davies894cc812017-06-22 17:51:33 +01001380#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001381 FRAME_COUNTS *counts = xd->counts;
1382
1383 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1384 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1385 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001386 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387 } else {
1388 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
1389 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1390 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001391#if CONFIG_EXT_COMP_REFS
1392 const COMP_REFERENCE_TYPE comp_ref_type =
1393 read_comp_reference_type(cm, xd, r);
1394
1395#if !USE_UNI_COMP_REFS
1396 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1397 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
1398#endif // !USE_UNI_COMP_REFS
1399
1400 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001401 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1402 int bit;
1403#if CONFIG_VAR_REFS
1404 if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
1405#endif // CONFIG_VAR_REFS
1406 bit = aom_read(r, fc->uni_comp_ref_prob[ctx][0], ACCT_STR);
1407#if CONFIG_VAR_REFS
1408 else
1409 bit = BWD_AND_ALT(cm);
1410#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001411 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1412
1413 if (bit) {
1414 ref_frame[0] = BWDREF_FRAME;
1415 ref_frame[1] = ALTREF_FRAME;
1416 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001417 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1418 int bit1;
1419#if CONFIG_VAR_REFS
1420 if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm)))
1421#endif // CONFIG_VAR_REFS
1422 bit1 = aom_read(r, fc->uni_comp_ref_prob[ctx1][1], ACCT_STR);
1423#if CONFIG_VAR_REFS
1424 else
1425 bit1 = L_AND_L3(cm) || L_AND_G(cm);
1426#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001427 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1428
1429 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001430 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1431 int bit2;
1432#if CONFIG_VAR_REFS
1433 if (L_AND_L3(cm) && L_AND_G(cm))
1434#endif // CONFIG_VAR_REFS
1435 bit2 = aom_read(r, fc->uni_comp_ref_prob[ctx2][2], ACCT_STR);
1436#if CONFIG_VAR_REFS
1437 else
1438 bit2 = L_AND_G(cm);
1439#endif // CONFIG_VAR_REFS
1440 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1441
1442 if (bit2) {
1443 ref_frame[0] = LAST_FRAME;
1444 ref_frame[1] = GOLDEN_FRAME;
1445 } else {
1446 ref_frame[0] = LAST_FRAME;
1447 ref_frame[1] = LAST3_FRAME;
1448 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001449 } else {
1450 ref_frame[0] = LAST_FRAME;
1451 ref_frame[1] = LAST2_FRAME;
1452 }
1453 }
1454
1455 return;
1456 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001457
1458 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001459#endif // CONFIG_EXT_COMP_REFS
1460
1461// Normative in decoder (for low delay)
Zoe Liu5a978832017-08-15 16:33:34 -07001462#if CONFIG_ONE_SIDED_COMPOUND
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001463 const int idx = 1;
Zoe Liu5a978832017-08-15 16:33:34 -07001464#else // !CONFIG_ONE_SIDED_COMPOUND
Yaowu Xuc27fc142016-08-22 16:08:15 -07001465#if CONFIG_EXT_REFS
1466 const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
Zoe Liuc082bbc2017-05-17 13:31:37 -07001467#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001468 const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
1469#endif // CONFIG_EXT_REFS
Zoe Liu5a978832017-08-15 16:33:34 -07001470#endif // CONFIG_ONE_SIDED_COMPOUND
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001472 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
1473#if CONFIG_VAR_REFS
1474 int bit;
1475 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1476 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001477 bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001478 else
1479 bit = L3_OR_G(cm);
1480#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001481 const int bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001482#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001483 if (counts) ++counts->comp_ref[ctx][0][bit];
1484
1485#if CONFIG_EXT_REFS
1486 // Decode forward references.
1487 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001488 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001489#if CONFIG_VAR_REFS
1490 int bit1;
1491 // Test need to explicitly code (L) vs (L2) branch node in tree
1492 if (L_AND_L2(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001493 bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001494 else
1495 bit1 = LAST_IS_VALID(cm);
1496#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001497 const int bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001498#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001499 if (counts) ++counts->comp_ref[ctx1][1][bit1];
1500 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
1501 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001502 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001503#if CONFIG_VAR_REFS
1504 int bit2;
1505 // Test need to explicitly code (L3) vs (G) branch node in tree
1506 if (L3_AND_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001507 bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001508 else
1509 bit2 = GOLDEN_IS_VALID(cm);
1510#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001511 const int bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001512#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001513 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1514 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1515 }
1516
1517 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001518 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
1519#if CONFIG_VAR_REFS
1520 int bit_bwd;
Zoe Liu043c2272017-07-19 12:40:29 -07001521// Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
1522#if CONFIG_ALTREF2
1523 const int bit_bwd_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
1524#else // !CONFIG_ALTREF2
1525 const int bit_bwd_uncertain = BWD_AND_ALT(cm);
1526#endif // CONFIG_ALTREF2
1527 if (bit_bwd_uncertain)
Thomas Davies894cc812017-06-22 17:51:33 +01001528 bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001529 else
1530 bit_bwd = ALTREF_IS_VALID(cm);
Thomas Davies894cc812017-06-22 17:51:33 +01001531#else // !CONFIG_VAR_REFS
1532 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001533#endif // CONFIG_VAR_REFS
1534 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001535#if CONFIG_ALTREF2
1536 if (!bit_bwd) {
1537 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
1538#if CONFIG_VAR_REFS
1539 int bit1_bwd;
1540 if (BWD_AND_ALT2(cm))
1541 bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1542 else
1543 bit1_bwd = ALTREF2_IS_VALID(cm);
1544#else // !CONFIG_VAR_REFS
1545 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1546#endif // CONFIG_VAR_REFS
1547 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1548 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1549 } else {
1550 ref_frame[idx] = cm->comp_bwd_ref[2];
1551 }
1552#else // !CONFIG_ALTREF2
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001553 ref_frame[idx] = cm->comp_bwd_ref[bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001554#endif // CONFIG_ALTREF2
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001555#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001556 ref_frame[!idx] = cm->comp_var_ref[bit];
1557 ref_frame[idx] = cm->comp_fixed_ref;
1558#endif // CONFIG_EXT_REFS
1559 } else if (mode == SINGLE_REFERENCE) {
1560#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001561 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001562#if CONFIG_VAR_REFS
1563 int bit0;
Zoe Liue9b15e22017-07-19 15:53:01 -07001564#if CONFIG_ALTREF2
1565 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT2,ALT) branch node
1566 // in tree
1567 if ((L_OR_L2(cm) || L3_OR_G(cm)) &&
1568 (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm)))
1569 bit0 = READ_REF_BIT(single_ref_p1);
1570 else
1571 bit0 = (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm));
1572#else // !CONFIG_ALTREF2
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001573 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT) branch node in
1574 // tree
1575 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001576 bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001577 else
1578 bit0 = BWD_OR_ALT(cm);
Zoe Liue9b15e22017-07-19 15:53:01 -07001579#endif // CONFIG_ALTREF2
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001580#else // !CONFIG_VAR_REFS
Thomas Davies315f5782017-06-14 15:14:55 +01001581 const int bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001582#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001583 if (counts) ++counts->single_ref[ctx0][0][bit0];
1584
1585 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001586 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001587#if CONFIG_VAR_REFS
1588 int bit1;
Zoe Liu043c2272017-07-19 12:40:29 -07001589#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001590 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001591 const int bit1_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
1592#else // !CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07001593 // Test need to explicitly code (BWD) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001594 const int bit1_uncertain = BWD_AND_ALT(cm);
1595#endif // CONFIG_ALTREF2
1596 if (bit1_uncertain)
Thomas Davies315f5782017-06-14 15:14:55 +01001597 bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001598 else
1599 bit1 = ALTREF_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001600#else // !CONFIG_VAR_REFS
1601 const int bit1 = READ_REF_BIT(single_ref_p2);
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[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001604#if CONFIG_ALTREF2
1605 if (!bit1) {
1606 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
1607#if CONFIG_VAR_REFS
1608 int bit5;
1609 if (BWD_AND_ALT2(cm))
1610 bit5 = READ_REF_BIT(single_ref_p6);
1611 else
1612 bit5 = ALTREF2_IS_VALID(cm);
1613#else // !CONFIG_VAR_REFS
1614 const int bit5 = READ_REF_BIT(single_ref_p6);
1615#endif // CONFIG_VAR_REFS
1616 if (counts) ++counts->single_ref[ctx5][5][bit5];
1617 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1618 } else {
1619 ref_frame[0] = ALTREF_FRAME;
1620 }
1621#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001622 ref_frame[0] = bit1 ? ALTREF_FRAME : BWDREF_FRAME;
Zoe Liu043c2272017-07-19 12:40:29 -07001623#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001624 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001625 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001626#if CONFIG_VAR_REFS
1627 int bit2;
1628 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1629 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001630 bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001631 else
1632 bit2 = L3_OR_G(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001633#else // !CONFIG_VAR_REFS
1634 const int bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001635#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001636 if (counts) ++counts->single_ref[ctx2][2][bit2];
1637 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001638 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001639#if CONFIG_VAR_REFS
1640 int bit4;
1641 // Test need to explicitly code (L3) vs (G) branch node in tree
1642 if (L3_AND_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001643 bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001644 else
1645 bit4 = GOLDEN_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001646#else // !CONFIG_VAR_REFS
1647 const int bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001648#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001649 if (counts) ++counts->single_ref[ctx4][4][bit4];
1650 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1651 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001652 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001653#if CONFIG_VAR_REFS
1654 int bit3;
1655 // Test need to explicitly code (L) vs (L2) branch node in tree
1656 if (L_AND_L2(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001657 bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001658 else
1659 bit3 = LAST2_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001660#else // !CONFIG_VAR_REFS
1661 const int bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001662#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001663 if (counts) ++counts->single_ref[ctx3][3][bit3];
1664 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1665 }
1666 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001667#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001669 const int bit0 = READ_REF_BIT(single_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001670 if (counts) ++counts->single_ref[ctx0][0][bit0];
1671
1672 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001673 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001674 const int bit1 = READ_REF_BIT(single_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675 if (counts) ++counts->single_ref[ctx1][1][bit1];
1676 ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
1677 } else {
1678 ref_frame[0] = LAST_FRAME;
1679 }
1680#endif // CONFIG_EXT_REFS
1681
Emil Keyder01770b32017-01-20 18:03:11 -05001682 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001683 } else {
1684 assert(0 && "Invalid prediction mode.");
1685 }
1686 }
1687}
1688
Angie Chiang9c4f8952016-11-21 11:13:19 -08001689static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1690 MACROBLOCKD *const xd,
1691 MB_MODE_INFO *const mbmi,
1692 aom_reader *r) {
1693 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001694 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001695
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001696 if (!av1_is_interp_needed(xd)) {
1697 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001698 return;
1699 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001700
1701#if CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07001702 if (cm->interp_filter != SWITCHABLE) {
Yue Chen19e7aa82016-11-30 14:05:39 -08001703 int dir;
1704
Angie Chiang9c4f8952016-11-21 11:13:19 -08001705 for (dir = 0; dir < 4; ++dir) mbmi->interp_filter[dir] = cm->interp_filter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001706 } else {
Yue Chen19e7aa82016-11-30 14:05:39 -08001707 int dir;
1708
Angie Chiang9c4f8952016-11-21 11:13:19 -08001709 for (dir = 0; dir < 2; ++dir) {
1710 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1711 mbmi->interp_filter[dir] = EIGHTTAP_REGULAR;
1712
1713 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1714 (mbmi->ref_frame[1] > INTRA_FRAME &&
1715 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001716 mbmi->interp_filter[dir] =
1717 (InterpFilter)av1_switchable_interp_inv[aom_read_symbol(
Thomas Davies77c7c402017-01-11 17:58:54 +00001718 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS,
Angie Chiang9c4f8952016-11-21 11:13:19 -08001719 ACCT_STR)];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001720 if (counts) ++counts->switchable_interp[ctx][mbmi->interp_filter[dir]];
1721 }
1722 }
1723 // The index system works as:
1724 // (0, 1) -> (vertical, horizontal) filter types for the first ref frame.
1725 // (2, 3) -> (vertical, horizontal) filter types for the second ref frame.
1726 mbmi->interp_filter[2] = mbmi->interp_filter[0];
1727 mbmi->interp_filter[3] = mbmi->interp_filter[1];
1728 }
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001729#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001730 if (cm->interp_filter != SWITCHABLE) {
1731 mbmi->interp_filter = cm->interp_filter;
1732 } else {
1733 const int ctx = av1_get_pred_context_switchable_interp(xd);
Angie Chiang9c4f8952016-11-21 11:13:19 -08001734 mbmi->interp_filter =
Michael Bebenita6048d052016-08-25 14:40:54 -07001735 (InterpFilter)av1_switchable_interp_inv[aom_read_symbol(
Thomas Davies77c7c402017-01-11 17:58:54 +00001736 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS,
Michael Bebenita6048d052016-08-25 14:40:54 -07001737 ACCT_STR)];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001738 if (counts) ++counts->switchable_interp[ctx][mbmi->interp_filter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001739 }
Angie Chiang9c4f8952016-11-21 11:13:19 -08001740#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07001741}
1742
Jingning Han36fe3202017-02-20 22:31:49 -08001743static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1744 const int mi_col, MACROBLOCKD *const xd,
1745 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001746 MB_MODE_INFO *const mbmi = &mi->mbmi;
1747 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1748 int i;
1749
1750 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001751 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001752
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001753 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001754
Jingning Han52261842016-12-14 12:17:49 -08001755#if CONFIG_CB4X4
1756 (void)i;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001757 mbmi->mode = read_intra_mode_y(ec_ctx, xd, r, size_group_lookup[bsize]);
Jingning Han52261842016-12-14 12:17:49 -08001758#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001759 switch (bsize) {
1760 case BLOCK_4X4:
1761 for (i = 0; i < 4; ++i)
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001762 mi->bmi[i].as_mode = read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001763 mbmi->mode = mi->bmi[3].as_mode;
1764 break;
1765 case BLOCK_4X8:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001766 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
1767 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001768 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001769 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001770 break;
1771 case BLOCK_8X4:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001772 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
1773 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001775 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001776 break;
1777 default:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001778 mbmi->mode = read_intra_mode_y(ec_ctx, xd, r, size_group_lookup[bsize]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001779 }
Jingning Han52261842016-12-14 12:17:49 -08001780#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001781
Jingning Han36fe3202017-02-20 22:31:49 -08001782#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001783 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001784 xd->plane[1].subsampling_y)) {
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001785 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001786#else
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001787 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001788 (void)mi_row;
1789 (void)mi_col;
1790#endif
1791
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001792#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001793 if (mbmi->uv_mode == UV_CFL_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001794 mbmi->cfl_alpha_idx =
David Michael Barrf6eaa152017-07-19 19:42:28 +09001795 read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001796 }
1797#endif // CONFIG_CFL
1798
1799#if CONFIG_CB4X4
1800 }
1801#endif
1802
Rupert Swarbrick766c9d52017-07-31 11:30:53 +01001803 // Explicitly ignore cm here to avoid a compile warning if none of
1804 // ext-intra, palette and filter-intra are enabled.
1805 (void)cm;
1806
Yaowu Xuc27fc142016-08-22 16:08:15 -07001807#if CONFIG_EXT_INTRA
1808 read_intra_angle_info(cm, xd, r);
1809#endif // CONFIG_EXT_INTRA
1810 mbmi->palette_mode_info.palette_size[0] = 0;
1811 mbmi->palette_mode_info.palette_size[1] = 0;
1812 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
1813 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001814#if CONFIG_FILTER_INTRA
1815 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1816 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001817 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001818 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001819#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001820}
1821
1822static INLINE int is_mv_valid(const MV *mv) {
1823 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1824 mv->col < MV_UPP;
1825}
1826
Yaowu Xuf883b422016-08-30 14:01:10 -07001827static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001828 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001829 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001830 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001831 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1832 int mi_col, int is_compound, int allow_hp,
1833 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001834 int i;
1835 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001836 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001837 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001838 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001839#if CONFIG_CB4X4
1840 int_mv *pred_mv = mbmi->pred_mv;
1841 (void)block;
1842#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001843 int_mv *pred_mv =
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07001844 (bsize >= BLOCK_8X8) ? mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08001845#endif // CONFIG_CB4X4
Sarah Parkere5299862016-08-16 14:57:37 -07001846 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001847 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001848 (void)mi_row;
1849 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001850 (void)bsize;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001851
1852 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001853 case NEWMV: {
1854 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001855 for (i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001856 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1857 int nmv_ctx =
1858 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1859 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001860 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001861 nmv_context_counts *const mv_counts =
1862 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001863 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001864 ret = ret && is_mv_valid(&mv[i].as_mv);
1865
Yaowu Xuc27fc142016-08-22 16:08:15 -07001866 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001867 }
1868 break;
1869 }
1870 case NEARESTMV: {
1871 mv[0].as_int = nearest_mv[0].as_int;
1872 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1873
Yaowu Xuc27fc142016-08-22 16:08:15 -07001874 pred_mv[0].as_int = nearest_mv[0].as_int;
1875 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001876 break;
1877 }
1878 case NEARMV: {
1879 mv[0].as_int = near_mv[0].as_int;
1880 if (is_compound) mv[1].as_int = near_mv[1].as_int;
1881
Yaowu Xuc27fc142016-08-22 16:08:15 -07001882 pred_mv[0].as_int = near_mv[0].as_int;
1883 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001884 break;
1885 }
1886 case ZEROMV: {
Sarah Parkere5299862016-08-16 14:57:37 -07001887#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +00001888 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001889 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001890 mi_col, mi_row, block)
David Barkercdcac6d2016-12-01 17:04:16 +00001891 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001892 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00001893 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001894 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001895 mi_col, mi_row, block)
David Barkercdcac6d2016-12-01 17:04:16 +00001896 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001897#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001898 mv[0].as_int = 0;
1899 if (is_compound) mv[1].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -07001900#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07001901
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001902 pred_mv[0].as_int = mv[0].as_int;
1903 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001904 break;
1905 }
1906#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07001907#if CONFIG_COMPOUND_SINGLEREF
1908 case SR_NEAREST_NEARMV: {
1909 assert(!is_compound);
1910 mv[0].as_int = nearest_mv[0].as_int;
1911 mv[1].as_int = near_mv[0].as_int;
1912 break;
1913 }
1914 /*
1915 case SR_NEAREST_NEWMV: {
1916 assert(!is_compound);
1917 mv[0].as_int = nearest_mv[0].as_int;
1918
1919 FRAME_COUNTS *counts = xd->counts;
1920 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1921 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1922 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1923 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1924 nmv_context_counts *const mv_counts =
1925 counts ? &counts->mv[nmv_ctx] : NULL;
1926 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1927 ret = ret && is_mv_valid(&mv[1].as_mv);
1928 break;
1929 }*/
1930 case SR_NEAR_NEWMV: {
1931 assert(!is_compound);
1932 mv[0].as_int = near_mv[0].as_int;
1933
1934 FRAME_COUNTS *counts = xd->counts;
1935 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1936 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1937 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1938 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1939 nmv_context_counts *const mv_counts =
1940 counts ? &counts->mv[nmv_ctx] : NULL;
1941 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1942 ret = ret && is_mv_valid(&mv[1].as_mv);
1943 break;
1944 }
1945 case SR_ZERO_NEWMV: {
1946 assert(!is_compound);
1947#if CONFIG_GLOBAL_MOTION
1948 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1949 cm->allow_high_precision_mv, bsize,
1950 mi_col, mi_row, block)
1951 .as_int;
1952#else
1953 mv[0].as_int = 0;
1954#endif // CONFIG_GLOBAL_MOTION
1955
1956 FRAME_COUNTS *counts = xd->counts;
1957 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1958 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1959 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1960 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1961 nmv_context_counts *const mv_counts =
1962 counts ? &counts->mv[nmv_ctx] : NULL;
1963 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1964 ret = ret && is_mv_valid(&mv[1].as_mv);
1965 break;
1966 }
1967 case SR_NEW_NEWMV: {
1968 assert(!is_compound);
1969
1970 FRAME_COUNTS *counts = xd->counts;
1971 for (i = 0; i < 2; ++i) {
1972 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1973 int nmv_ctx =
1974 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
1975 mbmi->ref_mv_idx);
1976 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1977 nmv_context_counts *const mv_counts =
1978 counts ? &counts->mv[nmv_ctx] : NULL;
1979 read_mv(r, &mv[i].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1980 ret = ret && is_mv_valid(&mv[i].as_mv);
1981 }
1982 break;
1983 }
1984#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985 case NEW_NEWMV: {
1986 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001987 assert(is_compound);
1988 for (i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001989 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1990 int nmv_ctx =
1991 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1992 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001993 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001994 nmv_context_counts *const mv_counts =
1995 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001996 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997 ret = ret && is_mv_valid(&mv[i].as_mv);
1998 }
1999 break;
2000 }
2001 case NEAREST_NEARESTMV: {
2002 assert(is_compound);
2003 mv[0].as_int = nearest_mv[0].as_int;
2004 mv[1].as_int = nearest_mv[1].as_int;
2005 break;
2006 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002007 case NEAR_NEARMV: {
2008 assert(is_compound);
2009 mv[0].as_int = near_mv[0].as_int;
2010 mv[1].as_int = near_mv[1].as_int;
2011 break;
2012 }
2013 case NEW_NEARESTMV: {
2014 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002015 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2016 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2017 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002018 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002019 nmv_context_counts *const mv_counts =
2020 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002021 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002022 assert(is_compound);
2023 ret = ret && is_mv_valid(&mv[0].as_mv);
2024 mv[1].as_int = nearest_mv[1].as_int;
2025 break;
2026 }
2027 case NEAREST_NEWMV: {
2028 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002029 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2030 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2031 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002032 nmv_context_counts *const mv_counts =
2033 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002034 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07002035 mv[0].as_int = nearest_mv[0].as_int;
2036 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002037 assert(is_compound);
2038 ret = ret && is_mv_valid(&mv[1].as_mv);
2039 break;
2040 }
2041 case NEAR_NEWMV: {
2042 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002043 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2044 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2045 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002046 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002047 nmv_context_counts *const mv_counts =
2048 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002049 mv[0].as_int = near_mv[0].as_int;
2050 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002051 assert(is_compound);
2052
2053 ret = ret && is_mv_valid(&mv[1].as_mv);
2054 break;
2055 }
2056 case NEW_NEARMV: {
2057 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002058 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2059 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2060 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002061 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002062 nmv_context_counts *const mv_counts =
2063 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002064 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002065 assert(is_compound);
2066 ret = ret && is_mv_valid(&mv[0].as_mv);
2067 mv[1].as_int = near_mv[1].as_int;
2068 break;
2069 }
2070 case ZERO_ZEROMV: {
2071 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08002072#if CONFIG_GLOBAL_MOTION
2073 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002074 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002075 mi_col, mi_row, block)
Sarah Parkerc2d38712017-01-24 15:15:41 -08002076 .as_int;
2077 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002078 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002079 mi_col, mi_row, block)
Sarah Parkerc2d38712017-01-24 15:15:41 -08002080 .as_int;
2081#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002082 mv[0].as_int = 0;
2083 mv[1].as_int = 0;
Sarah Parkerc2d38712017-01-24 15:15:41 -08002084#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002085 break;
2086 }
2087#endif // CONFIG_EXT_INTER
2088 default: { return 0; }
2089 }
2090 return ret;
2091}
2092
Yaowu Xuf883b422016-08-30 14:01:10 -07002093static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
2094 int segment_id, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002095 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
2096 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
2097 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002098 const int ctx = av1_get_intra_inter_context(xd);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002099#if CONFIG_NEW_MULTISYMBOL
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002100 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002101 const int is_inter =
2102 aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
2103#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002104 const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002105#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002106 FRAME_COUNTS *counts = xd->counts;
2107 if (counts) ++counts->intra_inter[ctx][is_inter];
2108 return is_inter;
2109 }
2110}
2111
Zoe Liu85b66462017-04-20 14:28:19 -07002112#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2113static int read_is_inter_singleref_comp_mode(AV1_COMMON *const cm,
2114 MACROBLOCKD *const xd,
2115 int segment_id, aom_reader *r) {
2116 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) return 0;
2117
2118 const int ctx = av1_get_inter_mode_context(xd);
2119 const int is_singleref_comp_mode =
2120 aom_read(r, cm->fc->comp_inter_mode_prob[ctx], ACCT_STR);
2121 FRAME_COUNTS *counts = xd->counts;
2122
2123 if (counts) ++counts->comp_inter_mode[ctx][is_singleref_comp_mode];
2124 return is_singleref_comp_mode;
2125}
2126#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2127
Yaowu Xuc27fc142016-08-22 16:08:15 -07002128static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002129 AV1Decoder *const pbi = (AV1Decoder *)data;
2130 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
2131 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002132}
2133
Di Chen56586622017-06-09 13:49:44 -07002134#if DEC_MISMATCH_DEBUG
2135static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi,
Zoe Liuf9333f52017-07-03 10:52:01 -07002136 MACROBLOCKD *const xd, int mi_row, int mi_col,
2137 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES],
2138 int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07002139 int_mv mv[2] = { { 0 } };
2140 int ref;
2141 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07002142 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
2143 mv[ref].as_mv = mbmi->mv[ref].as_mv;
2144
2145 int interp_ctx[2] = { -1 };
2146 int interp_filter[2] = { cm->interp_filter };
2147 if (cm->interp_filter == SWITCHABLE) {
2148 int dir;
2149 for (dir = 0; dir < 2; ++dir) {
2150 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
2151 (mbmi->ref_frame[1] > INTRA_FRAME &&
2152 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
2153 interp_ctx[dir] = av1_get_pred_context_switchable_interp(xd, dir);
2154 interp_filter[dir] = mbmi->interp_filter[dir];
2155 } else {
2156 interp_filter[dir] = EIGHTTAP_REGULAR;
2157 }
2158 }
2159 }
2160
2161 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
2162 int16_t zeromv_ctx = -1;
2163 int16_t refmv_ctx = -1;
2164 if (mbmi->mode != NEWMV) {
2165 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == ZEROMV);
2166 zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
2167 if (mbmi->mode != ZEROMV) {
2168 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
2169 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
2170 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
2171 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
2172 }
2173 }
2174
2175 int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Zoe Liuf9333f52017-07-03 10:52:01 -07002176#define FRAME_TO_CHECK 1
Zoe Liufcf5fa22017-06-26 16:00:38 -07002177 if (cm->current_video_frame == FRAME_TO_CHECK /*&& cm->show_frame == 0*/) {
Zoe Liuf9333f52017-07-03 10:52:01 -07002178 printf(
2179 "=== DECODER ===: "
2180 "Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
2181 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
2182 "ref[1]=%d, motion_mode=%d, inter_mode_ctx=%d, mode_ctx=%d, "
2183 "interp_ctx=(%d,%d), interp_filter=(%d,%d), newmv_ctx=%d, "
2184 "zeromv_ctx=%d, refmv_ctx=%d\n",
2185 cm->current_video_frame, mi_row, mi_col, mbmi->mode, mbmi->sb_type,
2186 cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
2187 mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
2188 mbmi->motion_mode, inter_mode_ctx[ref_frame_type], mode_ctx,
2189 interp_ctx[0], interp_ctx[1], interp_filter[0], interp_filter[1],
2190 newmv_ctx, zeromv_ctx, refmv_ctx);
2191 }
Di Chen56586622017-06-09 13:49:44 -07002192}
2193#endif // DEC_MISMATCH_DEBUG
2194
Yaowu Xuf883b422016-08-30 14:01:10 -07002195static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002196 MACROBLOCKD *const xd,
2197 MODE_INFO *const mi,
David Barker491983d2016-11-10 13:22:17 +00002198#if (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER) && \
2199 CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002200 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002201 int supertx_enabled) {
2202#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002203 int mi_row, int mi_col, aom_reader *r) {
Yue Chencb60b182016-10-13 15:18:22 -07002204#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002205 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002206 MB_MODE_INFO *const mbmi = &mi->mbmi;
2207 const BLOCK_SIZE bsize = mbmi->sb_type;
2208 const int allow_hp = cm->allow_high_precision_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08002209 const int unify_bsize = CONFIG_CB4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002210 int_mv nearestmv[2], nearmv[2];
2211 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212 int ref, is_compound;
Zoe Liu85b66462017-04-20 14:28:19 -07002213#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2214 int is_singleref_comp_mode = 0;
2215#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002217#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002218 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002219#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002220 int16_t mode_ctx = 0;
Yue Chen69f18e12016-09-08 14:48:15 -07002221#if CONFIG_WARPED_MOTION
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08002222 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang1bc82862017-06-28 15:49:48 -07002223#if WARPED_MOTION_SORT_SAMPLES
2224 int pts_mv[SAMPLES_ARRAY_SIZE];
2225#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002226#endif // CONFIG_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00002227 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228
Urvang Joshi5a9ea002017-05-22 15:25:18 -07002229 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
2230
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 mbmi->palette_mode_info.palette_size[0] = 0;
2232 mbmi->palette_mode_info.palette_size[1] = 0;
2233
Frederic Barbier7a84fd82017-03-02 18:08:15 +01002234 memset(ref_mvs, 0, sizeof(ref_mvs));
2235
Yaowu Xuc27fc142016-08-22 16:08:15 -07002236 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
2237 is_compound = has_second_ref(mbmi);
2238
Zoe Liuc082bbc2017-05-17 13:31:37 -07002239#if CONFIG_EXT_COMP_REFS
2240#if !USE_UNI_COMP_REFS
2241 // NOTE: uni-directional comp refs disabled
2242 if (is_compound)
2243 assert(mbmi->ref_frame[0] < BWDREF_FRAME &&
2244 mbmi->ref_frame[1] >= BWDREF_FRAME);
2245#endif // !USE_UNI_COMP_REFS
2246#endif // CONFIG_EXT_COMP_REFS
2247
Zoe Liu85b66462017-04-20 14:28:19 -07002248#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2249 if (!is_compound)
2250 is_singleref_comp_mode =
2251 read_is_inter_singleref_comp_mode(cm, xd, mbmi->segment_id, r);
2252#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2253
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 for (ref = 0; ref < 1 + is_compound; ++ref) {
2255 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002257 av1_find_mv_refs(
2258 cm, xd, mi, frame, &xd->ref_mv_count[frame], xd->ref_mv_stack[frame],
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259#if CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002260 compound_inter_mode_ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002261#endif // CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002262 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi, inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002263 }
2264
Jingning Hanacddc032016-11-17 15:26:20 -08002265 if (is_compound) {
2266 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002267 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
2268 xd->ref_mv_stack[ref_frame],
2269#if CONFIG_EXT_INTER
2270 compound_inter_mode_ctx,
2271#endif // CONFIG_EXT_INTER
2272 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2273 inter_mode_ctx);
2274
2275 if (xd->ref_mv_count[ref_frame] < 2) {
2276 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00002277 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002278 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00002279#if CONFIG_GLOBAL_MOTION
2280 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00002281 cm->allow_high_precision_mv,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002282 bsize, mi_col, mi_row, 0)
David Barkercdcac6d2016-12-01 17:04:16 +00002283 .as_int;
Sarah Parkerae7c4582017-02-28 16:30:30 -08002284 zeromv[1].as_int = (rf[1] != NONE_FRAME)
2285 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
2286 cm->allow_high_precision_mv,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002287 bsize, mi_col, mi_row, 0)
Sarah Parkerae7c4582017-02-28 16:30:30 -08002288 .as_int
2289 : 0;
David Barkercdcac6d2016-12-01 17:04:16 +00002290#else
2291 zeromv[0].as_int = zeromv[1].as_int = 0;
2292#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002293 for (ref = 0; ref < 2; ++ref) {
2294 if (rf[ref] == NONE_FRAME) continue;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002295 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
2296 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
Sarah Parker9923d1b2017-04-10 11:56:40 -07002297 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
2298 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
2299 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01002300 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002301 }
2302 }
2303
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07002305#if CONFIG_COMPOUND_SINGLEREF
2306 if (is_compound || is_singleref_comp_mode)
2307#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002308 if (is_compound)
Zoe Liu85b66462017-04-20 14:28:19 -07002309#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002310 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
2311 else
2312#endif // CONFIG_EXT_INTER
2313 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07002314 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002315 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316
2317 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
2318 mbmi->mode = ZEROMV;
Debargha Mukherjeec76f9dc2017-05-01 13:18:09 -07002319 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002320 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
David Barker3409c0d2017-06-30 17:33:13 +01002321 "Invalid usage of segment feature on small blocks");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002322 return;
2323 }
2324 } else {
Jingning Han5cfa6712016-12-14 09:53:38 -08002325 if (bsize >= BLOCK_8X8 || unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326#if CONFIG_EXT_INTER
2327 if (is_compound)
2328 mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002329#if CONFIG_COMPOUND_SINGLEREF
2330 else if (is_singleref_comp_mode)
Thomas Daviesb8b14a92017-07-12 15:11:49 +01002331 mbmi->mode = read_inter_singleref_comp_mode(xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002332#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002333 else
2334#endif // CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002335 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
David Barker404b2e82017-03-27 13:07:47 +01002336#if CONFIG_EXT_INTER
David Barker3dfba992017-04-03 16:10:09 +01002337 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002338#if CONFIG_COMPOUND_SINGLEREF
2339 mbmi->mode == SR_NEW_NEWMV ||
2340#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002341 have_nearmv_in_inter_mode(mbmi->mode))
Zoe Liu85b66462017-04-20 14:28:19 -07002342#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002343 if (mbmi->mode == NEARMV || mbmi->mode == NEWMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002344#endif // CONFIG_EXT_INTER
Thomas Davies149eda52017-06-12 18:11:55 +01002345 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346 }
2347 }
2348
2349#if CONFIG_EXT_INTER
David Barker9b75e212017-07-28 15:31:41 +01002350 if ((bsize < BLOCK_8X8 && !unify_bsize) ||
Yushin Cho127c5832017-07-28 16:39:04 -07002351 (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV))
Yaowu Xuc27fc142016-08-22 16:08:15 -07002352#else
Yushin Cho127c5832017-07-28 16:39:04 -07002353 if ((bsize < BLOCK_8X8 && !unify_bsize) || mbmi->mode != ZEROMV)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002354#endif // CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -07002355 {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002356 for (ref = 0; ref < 1 + is_compound; ++ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002357 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2358 &nearestmv[ref], &nearmv[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002359 }
2360 }
2361
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07002363#if CONFIG_COMPOUND_SINGLEREF
2364 if ((is_compound || is_singleref_comp_mode) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002365 (bsize >= BLOCK_8X8 || unify_bsize) && mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002366#else // !CONFIG_COMPOUND_SINGLEREF
Jingning Han61418bb2017-01-23 17:12:48 -08002367 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002368 mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002369#endif // CONFIG_COMPOUND_SINGLEREF
2370#else // !CONFIG_EXT_INTER
Jingning Han5cfa6712016-12-14 09:53:38 -08002371 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002372 mbmi->mode != NEWMV && mbmi->mode != ZEROMV)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373#endif // CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -07002374 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002375 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376
2377#if CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -07002378 if (xd->ref_mv_count[ref_frame_type] > 0)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379#else
Yushin Cho127c5832017-07-28 16:39:04 -07002380 if (xd->ref_mv_count[ref_frame_type] == 1 && mbmi->mode == NEARESTMV)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002381#endif // CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -07002382 {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383#if CONFIG_EXT_INTER
2384 if (mbmi->mode == NEAREST_NEARESTMV) {
2385#endif // CONFIG_EXT_INTER
2386 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2387 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
2388 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
2389 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
2390#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07002391 } else if (mbmi->mode == NEAREST_NEWMV
2392#if CONFIG_COMPOUND_SINGLEREF
2393 || mbmi->mode == SR_NEAREST_NEARMV
2394// || mbmi->mode == SR_NEAREST_NEWMV
2395#endif // CONFIG_COMPOUND_SINGLEREF
2396 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2398 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07002399 } else if (mbmi->mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
2401 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
2402 }
2403#endif // CONFIG_EXT_INTER
2404 }
2405
2406#if CONFIG_EXT_INTER
2407 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01002408 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
Zoe Liu85b66462017-04-20 14:28:19 -07002409#if CONFIG_COMPOUND_SINGLEREF
2410 if (is_compound) {
2411#endif // CONFIG_COMPOUND_SINGLEREF
2412 if (compound_ref0_mode(mbmi->mode) == NEARMV) {
2413 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2414 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2415 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416
Zoe Liu85b66462017-04-20 14:28:19 -07002417 if (compound_ref1_mode(mbmi->mode) == NEARMV) {
2418 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
2419 lower_mv_precision(&nearmv[1].as_mv, allow_hp);
2420 }
2421#if CONFIG_COMPOUND_SINGLEREF
2422 } else {
2423 assert(is_singleref_comp_mode);
2424 if (compound_ref0_mode(mbmi->mode) == NEARMV ||
2425 compound_ref1_mode(mbmi->mode) == NEARMV) {
2426 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2427 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2428 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002429 }
Zoe Liu85b66462017-04-20 14:28:19 -07002430#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002431 }
Zoe Liu85b66462017-04-20 14:28:19 -07002432#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433 if (xd->ref_mv_count[ref_frame_type] > 1) {
2434 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
2435 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2436 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
2437 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2438 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
2439 }
2440#endif // CONFIG_EXT_INTER
David Barker0d7c4b02017-07-25 14:55:48 +01002441 } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
2442 int_mv cur_mv =
2443 xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
2444 nearmv[0] = cur_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446
Yue Chen19e7aa82016-11-30 14:05:39 -08002447#if !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION && !CONFIG_GLOBAL_MOTION
Angie Chiang9c4f8952016-11-21 11:13:19 -08002448 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002449#endif // !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002450
Jingning Han5cfa6712016-12-14 09:53:38 -08002451 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452 const int num_4x4_w = 1 << xd->bmode_blocks_wl;
2453 const int num_4x4_h = 1 << xd->bmode_blocks_hl;
2454 int idx, idy;
2455 PREDICTION_MODE b_mode;
2456 int_mv nearest_sub8x8[2], near_sub8x8[2];
2457#if CONFIG_EXT_INTER
2458 int_mv ref_mv[2][2];
2459#endif // CONFIG_EXT_INTER
2460 for (idy = 0; idy < 2; idy += num_4x4_h) {
2461 for (idx = 0; idx < 2; idx += num_4x4_w) {
2462 int_mv block[2];
2463 const int j = idy * 2 + idx;
2464 int_mv ref_mv_s8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465#if CONFIG_EXT_INTER
2466 if (!is_compound)
2467#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002468 mode_ctx = av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame,
2469 bsize, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470#if CONFIG_EXT_INTER
2471 if (is_compound)
2472 b_mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
2473 else
2474#endif // CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002475 b_mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002476
2477#if CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -07002478 if (b_mode != ZEROMV && b_mode != ZERO_ZEROMV)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002479#else
Yushin Cho127c5832017-07-28 16:39:04 -07002480 if (b_mode != ZEROMV)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002481#endif // CONFIG_EXT_INTER
Yushin Cho127c5832017-07-28 16:39:04 -07002482 {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002483 CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
2484 uint8_t ref_mv_count[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002485 for (ref = 0; ref < 1 + is_compound; ++ref)
2486#if CONFIG_EXT_INTER
2487 {
2488 int_mv mv_ref_list[MAX_MV_REF_CANDIDATES];
Yaowu Xu531d6af2017-03-07 17:48:52 -08002489 av1_update_mv_context(cm, xd, mi, mbmi->ref_frame[ref], mv_ref_list,
2490 j, mi_row, mi_col, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002492 av1_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
Yaowu Xuf883b422016-08-30 14:01:10 -07002493 ref_mv_stack[ref], &ref_mv_count[ref],
Yaowu Xuc27fc142016-08-22 16:08:15 -07002494#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002495 mv_ref_list,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002497 &nearest_sub8x8[ref],
2498 &near_sub8x8[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499#if CONFIG_EXT_INTER
2500 if (have_newmv_in_inter_mode(b_mode)) {
2501 mv_ref_list[0].as_int = nearest_sub8x8[ref].as_int;
2502 mv_ref_list[1].as_int = near_sub8x8[ref].as_int;
Yaowu Xuf883b422016-08-30 14:01:10 -07002503 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2504 &ref_mv[1][ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505 }
2506 }
2507#endif // CONFIG_EXT_INTER
2508 }
2509
2510 for (ref = 0; ref < 1 + is_compound && b_mode != ZEROMV; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 ref_mv_s8[ref] = nearest_sub8x8[ref];
2512 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002513 }
2514#if CONFIG_EXT_INTER
2515 (void)ref_mv_s8;
2516#endif
2517
Jingning Han5c60cdf2016-09-30 09:37:46 -07002518 if (!assign_mv(cm, xd, b_mode, mbmi->ref_frame, j, block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002519#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002520 ref_mv[0],
2521#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 ref_mv_s8,
2523#endif // CONFIG_EXT_INTER
David Barker45390c12017-02-20 14:44:40 +00002524 nearest_sub8x8, near_sub8x8, mi_row, mi_col, is_compound,
2525 allow_hp, r)) {
Angie Chiangd0916d92017-03-10 17:54:18 -08002526 aom_merge_corrupted_flag(&xd->corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002527 break;
2528 };
2529
2530 mi->bmi[j].as_mv[0].as_int = block[0].as_int;
Sarah Parkerd7fa8542016-10-11 11:51:59 -07002531 mi->bmi[j].as_mode = b_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002532 if (is_compound) mi->bmi[j].as_mv[1].as_int = block[1].as_int;
2533
2534 if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
2535 if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
2536 }
2537 }
2538
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07002539 mbmi->pred_mv[0].as_int = mi->bmi[3].pred_mv[0].as_int;
2540 mbmi->pred_mv[1].as_int = mi->bmi[3].pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002541 mi->mbmi.mode = b_mode;
2542
2543 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2544 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2545 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546 int_mv ref_mv[2];
2547 ref_mv[0] = nearestmv[0];
2548 ref_mv[1] = nearestmv[1];
2549
David Barker404b2e82017-03-27 13:07:47 +01002550#if CONFIG_EXT_INTER
David Barker3dfba992017-04-03 16:10:09 +01002551 if (is_compound) {
David Barker3dfba992017-04-03 16:10:09 +01002552 int ref_mv_idx = mbmi->ref_mv_idx;
2553 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2554 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2555 // mbmi->ref_mv_idx (like NEWMV)
2556 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2557 ref_mv_idx = 1 + mbmi->ref_mv_idx;
David Barker3dfba992017-04-03 16:10:09 +01002558
2559 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
David Barker404b2e82017-03-27 13:07:47 +01002560 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2561 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker3dfba992017-04-03 16:10:09 +01002562 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002563 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker404b2e82017-03-27 13:07:47 +01002564 xd->n8_h << MI_SIZE_LOG2, xd);
2565 }
David Barker3dfba992017-04-03 16:10:09 +01002566 nearestmv[0] = ref_mv[0];
David Barker404b2e82017-03-27 13:07:47 +01002567 }
David Barker3dfba992017-04-03 16:10:09 +01002568 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
David Barker3dfba992017-04-03 16:10:09 +01002569 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2570 if (xd->ref_mv_count[ref_frame_type] > 1) {
2571 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002572 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker3dfba992017-04-03 16:10:09 +01002573 xd->n8_h << MI_SIZE_LOG2, xd);
2574 }
David Barker3dfba992017-04-03 16:10:09 +01002575 nearestmv[1] = ref_mv[1];
2576 }
Zoe Liu85b66462017-04-20 14:28:19 -07002577#if CONFIG_COMPOUND_SINGLEREF
2578 } else if (is_singleref_comp_mode) {
2579 int ref_mv_idx = mbmi->ref_mv_idx;
2580 // Special case: SR_NEAR_NEWMV use 1 + mbmi->ref_mv_idx (like NEARMV)
2581 // instead of mbmi->ref_mv_idx (like NEWMV)
2582 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
2583
2584 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
2585 compound_ref1_mode(mbmi->mode) == NEWMV) {
2586 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2587 if (xd->ref_mv_count[ref_frame_type] > 1) {
2588 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2589 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2590 xd->n8_h << MI_SIZE_LOG2, xd);
2591 }
2592 // TODO(zoeliu): To further investigate why this would not cause a
2593 // mismatch for the mode of SR_NEAREST_NEWMV.
2594 nearestmv[0] = ref_mv[0];
2595 }
2596#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002597 } else {
2598#endif // CONFIG_EXT_INTER
2599 if (mbmi->mode == NEWMV) {
2600 for (ref = 0; ref < 1 + is_compound; ++ref) {
David Barker3dfba992017-04-03 16:10:09 +01002601 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2602 if (xd->ref_mv_count[ref_frame_type] > 1) {
2603 ref_mv[ref] =
2604 (ref == 0)
2605 ? xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv
2606 : xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
2607 .comp_mv;
2608 clamp_mv_ref(&ref_mv[ref].as_mv, xd->n8_w << MI_SIZE_LOG2,
2609 xd->n8_h << MI_SIZE_LOG2, xd);
2610 }
David Barker3dfba992017-04-03 16:10:09 +01002611 nearestmv[ref] = ref_mv[ref];
2612 }
2613 }
2614#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615 }
David Barker3dfba992017-04-03 16:10:09 +01002616#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617
Angie Chiangd0916d92017-03-10 17:54:18 -08002618 int mv_corrupted_flag =
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002619 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
David Barker45390c12017-02-20 14:44:40 +00002620 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
Angie Chiangd0916d92017-03-10 17:54:18 -08002621 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 }
2623
Yue Chen4d26acb2017-05-01 12:28:34 -07002624#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 mbmi->use_wedge_interintra = 0;
2626 if (cm->reference_mode != COMPOUND_REFERENCE &&
2627#if CONFIG_SUPERTX
2628 !supertx_enabled &&
2629#endif
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002630 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002632#if CONFIG_NEW_MULTISYMBOL
2633 const int interintra =
2634 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
2635#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002636 const int interintra =
2637 aom_read(r, cm->fc->interintra_prob[bsize_group], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002638#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002640 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002641 if (interintra) {
2642 const INTERINTRA_MODE interintra_mode =
2643 read_interintra_mode(cm, xd, r, bsize_group);
2644 mbmi->ref_frame[1] = INTRA_FRAME;
2645 mbmi->interintra_mode = interintra_mode;
2646#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 mbmi->angle_delta[0] = 0;
2648 mbmi->angle_delta[1] = 0;
hui sueda3d762016-12-06 16:58:23 -08002649#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002650 mbmi->intra_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -08002651#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002653#if CONFIG_FILTER_INTRA
2654 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2655 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2656#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002658#if CONFIG_NEW_MULTISYMBOL
2659 mbmi->use_wedge_interintra = aom_read_symbol(
2660 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
2661#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 mbmi->use_wedge_interintra =
Michael Bebenita6048d052016-08-25 14:40:54 -07002663 aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002664#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 if (xd->counts)
2666 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2667 if (mbmi->use_wedge_interintra) {
2668 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002669 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002670 mbmi->interintra_wedge_sign = 0;
2671 }
2672 }
2673 }
2674 }
Yue Chen4d26acb2017-05-01 12:28:34 -07002675#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676
Yue Chen52c51732017-07-11 15:08:30 -07002677#if CONFIG_WARPED_MOTION
2678 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
2679 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2680 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2681
2682 xd->block_refs[ref] = ref_buf;
2683 }
2684#endif
2685
Yue Chencb60b182016-10-13 15:18:22 -07002686#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
2687 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yue Chen69f18e12016-09-08 14:48:15 -07002688#if CONFIG_WARPED_MOTION
2689 if (mbmi->sb_type >= BLOCK_8X8 && !has_second_ref(mbmi))
Yunqing Wang1bc82862017-06-28 15:49:48 -07002690#if WARPED_MOTION_SORT_SAMPLES
2691 mbmi->num_proj_ref[0] =
2692 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv);
2693#else
Yue Chen69f18e12016-09-08 14:48:15 -07002694 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang1bc82862017-06-28 15:49:48 -07002695#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002696#endif // CONFIG_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002697#if CONFIG_MOTION_VAR
2698 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
2699#endif
Yue Chen69f18e12016-09-08 14:48:15 -07002700
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701#if CONFIG_SUPERTX
Yue Chen69f18e12016-09-08 14:48:15 -07002702 if (!supertx_enabled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002703#endif // CONFIG_SUPERTX
2704#if CONFIG_EXT_INTER
2705 if (mbmi->ref_frame[1] != INTRA_FRAME)
2706#endif // CONFIG_EXT_INTER
Sarah Parker19234cc2017-03-10 16:43:25 -08002707 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002708
2709#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -07002710 read_ncobmc_mode(xd, mi, mbmi->ncobmc_mode, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002711#endif
2712
Zoe Liu85b66462017-04-20 14:28:19 -07002713#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2714 if (is_singleref_comp_mode) assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
2715#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yue Chen69f18e12016-09-08 14:48:15 -07002716#if CONFIG_WARPED_MOTION
2717 if (mbmi->motion_mode == WARPED_CAUSAL) {
2718 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
Yunqing Wang1bc82862017-06-28 15:49:48 -07002719
2720#if WARPED_MOTION_SORT_SAMPLES
2721 if (mbmi->num_proj_ref[0] > 1)
2722 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2723 pts_inref, mbmi->num_proj_ref[0]);
2724#endif // WARPED_MOTION_SORT_SAMPLES
2725
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002726 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2727 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2728 &mbmi->wm_params[0], mi_row, mi_col)) {
Yunqing Wang8657ad72017-06-20 14:46:08 -07002729 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "Invalid Warped Model");
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002730 }
Yue Chen69f18e12016-09-08 14:48:15 -07002731 }
2732#endif // CONFIG_WARPED_MOTION
2733#if CONFIG_SUPERTX
2734 }
2735#endif // CONFIG_SUPERTX
Yue Chencb60b182016-10-13 15:18:22 -07002736#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002737
2738#if CONFIG_EXT_INTER
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002739 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Zoe Liu85b66462017-04-20 14:28:19 -07002740 if (
2741#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu0c634c72017-06-19 07:06:28 -07002742 is_inter_anyref_comp_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002743#else // !CONFIG_COMPOUND_SINGLEREF
2744 cm->reference_mode != SINGLE_REFERENCE &&
Sarah Parker6fdc8532016-11-16 17:47:13 -08002745 is_inter_compound_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002746#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chencb60b182016-10-13 15:18:22 -07002747#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002748 && mbmi->motion_mode == SIMPLE_TRANSLATION
Yue Chencb60b182016-10-13 15:18:22 -07002749#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002750 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002751 if (is_any_masked_compound_used(bsize)) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002752#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002753 if (cm->allow_masked_compound) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002754#if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2755 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
2756 mbmi->interinter_compound_type =
2757 aom_read_bit(r, ACCT_STR) ? COMPOUND_AVERAGE : COMPOUND_SEG;
2758 else
2759#endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2760 mbmi->interinter_compound_type = aom_read_symbol(
2761 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002762#if CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002763 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002764 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002765 mbmi->wedge_index =
2766 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2767 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2768 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002769#endif // CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002770#if CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002771 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
2772 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
2773 }
Sarah Parker42d96102017-01-31 21:05:27 -08002774#endif // CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002775 }
2776#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002777 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002778 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002779 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002781 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002782 }
2783#endif // CONFIG_EXT_INTER
2784
Yue Chen19e7aa82016-11-30 14:05:39 -08002785#if CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002786 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002787#endif // CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07002788
Di Chen56586622017-06-09 13:49:44 -07002789#if DEC_MISMATCH_DEBUG
2790 // NOTE(zoeliu): For debug
Zoe Liuf9333f52017-07-03 10:52:01 -07002791 dec_dump_logs(cm, mi, xd, mi_row, mi_col, inter_mode_ctx, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002792#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002793}
2794
Yaowu Xuf883b422016-08-30 14:01:10 -07002795static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002796 MACROBLOCKD *const xd,
2797#if CONFIG_SUPERTX
2798 int supertx_enabled,
2799#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002800 int mi_row, int mi_col, aom_reader *r) {
2801 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002802 MODE_INFO *const mi = xd->mi[0];
2803 MB_MODE_INFO *const mbmi = &mi->mbmi;
2804 int inter_block = 1;
2805#if CONFIG_VAR_TX
2806 BLOCK_SIZE bsize = mbmi->sb_type;
2807#endif // CONFIG_VAR_TX
2808
2809 mbmi->mv[0].as_int = 0;
2810 mbmi->mv[1].as_int = 0;
2811 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
2812#if CONFIG_SUPERTX
David Barker3aec8d62017-01-31 14:55:32 +00002813 if (!supertx_enabled)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814#endif // CONFIG_SUPERTX
2815 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
David Barker3aec8d62017-01-31 14:55:32 +00002816
Arild Fuldseth07441162016-08-15 15:07:52 +02002817#if CONFIG_DELTA_Q
David Barker3aec8d62017-01-31 14:55:32 +00002818 if (cm->delta_q_present_flag) {
2819 xd->current_qindex =
2820 xd->prev_qindex +
2821 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01002822 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2823 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
David Barker3aec8d62017-01-31 14:55:32 +00002824 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07002825#if CONFIG_EXT_DELTA_Q
2826 if (cm->delta_lf_present_flag) {
2827 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2828 xd->prev_delta_lf_from_base +
2829 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
2830 cm->delta_lf_res;
2831 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
2832 }
2833#endif
David Barker3aec8d62017-01-31 14:55:32 +00002834 }
Arild Fuldseth07441162016-08-15 15:07:52 +02002835#endif
David Barker3aec8d62017-01-31 14:55:32 +00002836
2837#if CONFIG_SUPERTX
2838 if (!supertx_enabled) {
2839#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002840 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
2841
2842#if CONFIG_VAR_TX
Jingning Han331662e2017-05-30 17:03:32 -07002843 xd->above_txfm_context =
2844 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2845 xd->left_txfm_context = xd->left_txfm_context_buffer +
2846 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002847
2848 if (cm->tx_mode == TX_MODE_SELECT &&
2849#if CONFIG_CB4X4
Jingning Han3daa4fd2017-01-20 10:33:50 -08002850 bsize > BLOCK_4X4 &&
Jingning Han581d1692017-01-05 16:03:54 -08002851#else
2852 bsize >= BLOCK_8X8 &&
2853#endif
2854 !mbmi->skip && inter_block) {
Jingning Han70e5f3f2016-11-09 17:03:07 -08002855 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07002856 const int bh = tx_size_high_unit[max_tx_size];
2857 const int bw = tx_size_wide_unit[max_tx_size];
Jingning Han65abc312016-10-27 13:04:21 -07002858 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2859 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860 int idx, idy;
Yue Chena1e48dc2016-08-29 17:29:33 -07002861
Jingning Hanfe45b212016-11-22 10:30:23 -08002862 mbmi->min_tx_size = TX_SIZES_ALL;
2863 for (idy = 0; idy < height; idy += bh)
2864 for (idx = 0; idx < width; idx += bw)
2865 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size,
2866 height != width, idy, idx, r);
Yue Chend6bdd462017-07-19 16:05:43 -07002867#if CONFIG_RECT_TX_EXT
2868 if (is_quarter_tx_allowed(xd, mbmi, inter_block) &&
2869 mbmi->tx_size == max_tx_size) {
2870 int quarter_tx;
2871
2872 if (quarter_txsize_lookup[bsize] != max_tx_size) {
2873 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
2874 if (xd->counts) ++xd->counts->quarter_tx_size[quarter_tx];
2875 } else {
2876 quarter_tx = 1;
2877 }
2878 if (quarter_tx) {
2879 mbmi->tx_size = quarter_txsize_lookup[bsize];
2880 for (idy = 0; idy < tx_size_high_unit[max_tx_size] / 2; ++idy)
2881 for (idx = 0; idx < tx_size_wide_unit[max_tx_size] / 2; ++idx)
2882 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
2883 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
2884 }
2885 }
2886#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -08002888 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889
2890 if (inter_block) {
Jingning Han9ca05b72017-01-03 14:41:36 -08002891 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2892 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893 int idx, idy;
2894 for (idy = 0; idy < height; ++idy)
2895 for (idx = 0; idx < width; ++idx)
2896 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
2897 }
Jingning Hane67b38a2016-11-04 10:30:00 -07002898 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han1b1dc932016-11-09 10:55:30 -08002899 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002900 }
2901#else
Urvang Joshifeb925f2016-12-05 10:37:29 -08002902 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002903#endif // CONFIG_VAR_TX
2904#if CONFIG_SUPERTX
2905 }
2906#if CONFIG_VAR_TX
2907 else if (inter_block) {
2908 const int width = num_4x4_blocks_wide_lookup[bsize];
2909 const int height = num_4x4_blocks_high_lookup[bsize];
2910 int idx, idy;
2911 xd->mi[0]->mbmi.tx_size = xd->supertx_size;
2912 for (idy = 0; idy < height; ++idy)
2913 for (idx = 0; idx < width; ++idx)
2914 xd->mi[0]->mbmi.inter_tx_size[idy >> 1][idx >> 1] = xd->supertx_size;
2915 }
2916#endif // CONFIG_VAR_TX
2917#endif // CONFIG_SUPERTX
2918
2919 if (inter_block)
2920 read_inter_block_mode_info(pbi, xd,
David Barker491983d2016-11-10 13:22:17 +00002921#if (CONFIG_MOTION_VAR || CONFIG_EXT_INTER || CONFIG_WARPED_MOTION) && \
2922 CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002923
2924 mi, mi_row, mi_col, r, supertx_enabled);
2925#else
2926 mi, mi_row, mi_col, r);
Yue Chencb60b182016-10-13 15:18:22 -07002927#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002928 else
Jingning Han36fe3202017-02-20 22:31:49 -08002929 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002931#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07002932 av1_read_tx_type(cm, xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002933#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07002934 supertx_enabled,
Nathan E. Egge93878c42016-05-03 10:01:32 -04002935#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07002936 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002937#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002938}
2939
Yaowu Xuf883b422016-08-30 14:01:10 -07002940void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002941#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002942 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002943#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002944 int mi_row, int mi_col, aom_reader *r, int x_mis,
2945 int y_mis) {
2946 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002947 MODE_INFO *const mi = xd->mi[0];
2948 MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
2949 int w, h;
2950
Alex Converse28744302017-04-13 14:46:22 -07002951#if CONFIG_INTRABC
2952 mi->mbmi.use_intrabc = 0;
2953#endif // CONFIG_INTRABC
2954
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955 if (frame_is_intra_only(cm)) {
2956 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957 for (h = 0; h < y_mis; ++h) {
2958 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2959 for (w = 0; w < x_mis; ++w) {
2960 MV_REF *const mv = frame_mv + w;
Emil Keyder01770b32017-01-20 18:03:11 -05002961 mv->ref_frame[0] = NONE_FRAME;
2962 mv->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 }
2964 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 } else {
2966 read_inter_frame_mode_info(pbi, xd,
2967#if CONFIG_SUPERTX
2968 supertx_enabled,
2969#endif // CONFIG_SUPERTX
2970 mi_row, mi_col, r);
2971 for (h = 0; h < y_mis; ++h) {
2972 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2973 for (w = 0; w < x_mis; ++w) {
2974 MV_REF *const mv = frame_mv + w;
2975 mv->ref_frame[0] = mi->mbmi.ref_frame[0];
2976 mv->ref_frame[1] = mi->mbmi.ref_frame[1];
2977 mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
2978 mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002979 mv->pred_mv[0].as_int = mi->mbmi.pred_mv[0].as_int;
2980 mv->pred_mv[1].as_int = mi->mbmi.pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981 }
2982 }
2983 }
2984}