blob: 96c06b6e3ca33798b15c14b5784be2594eae0705 [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) {
Hui Su814f41e2017-10-02 12:21:24 -070039 return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -040040}
Yaowu Xuc27fc142016-08-22 16:08:15 -070041
Thomas Daviesf6936102016-09-05 16:51:31 +010042static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
43 MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
44 FRAME_COUNTS *counts = xd->counts;
45 int sign, abs, reduced_delta_qindex = 0;
46 BLOCK_SIZE bsize = mbmi->sb_type;
47 const int b_col = mi_col & MAX_MIB_MASK;
48 const int b_row = mi_row & MAX_MIB_MASK;
49 const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000050 int rem_bits, thr;
51 int i, smallval;
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000052 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
53 (void)cm;
Thomas Daviesf6936102016-09-05 16:51:31 +010054
Alex Converse68abef82017-03-23 14:50:33 -070055 if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000056 abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000057 smallval = (abs < DELTA_Q_SMALL);
58 if (counts) {
59 for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
60 if (smallval) counts->delta_q[abs][0]++;
61 }
62
63 if (!smallval) {
Thomas Davies3b93e8e2017-09-20 09:59:07 +010064 rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
Thomas Daviesf6936102016-09-05 16:51:31 +010065 thr = (1 << rem_bits) + 1;
66 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
67 }
68
69 if (abs) {
70 sign = aom_read_bit(r, ACCT_STR);
71 } else {
72 sign = 1;
73 }
74
75 reduced_delta_qindex = sign ? -abs : abs;
76 }
77 return reduced_delta_qindex;
78}
Fangwen Fu231fe422017-04-24 17:52:29 -070079#if CONFIG_EXT_DELTA_Q
80static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
Cheng Chena97394f2017-09-27 15:05:14 -070081#if CONFIG_LOOPFILTER_LEVEL
82 int lf_id,
83#endif
Fangwen Fu231fe422017-04-24 17:52:29 -070084 MB_MODE_INFO *const mbmi, int mi_col,
85 int mi_row) {
86 FRAME_COUNTS *counts = xd->counts;
87 int sign, abs, reduced_delta_lflevel = 0;
88 BLOCK_SIZE bsize = mbmi->sb_type;
89 const int b_col = mi_col & MAX_MIB_MASK;
90 const int b_row = mi_row & MAX_MIB_MASK;
91 const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
92 int rem_bits, thr;
93 int i, smallval;
Fangwen Fu231fe422017-04-24 17:52:29 -070094 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
95 (void)cm;
Fangwen Fu231fe422017-04-24 17:52:29 -070096
Debargha Mukherjeee30159c2017-10-08 12:04:43 -070097 if ((bsize != cm->sb_size || mbmi->skip == 0) && read_delta_lf_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -070098#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -070099 if (cm->delta_lf_multi) {
100 assert(lf_id >= 0 && lf_id < FRAME_LF_COUNT);
101 abs = aom_read_symbol(r, ec_ctx->delta_lf_multi_cdf[lf_id],
102 DELTA_LF_PROBS + 1, ACCT_STR);
103 } else {
104 abs = aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1,
105 ACCT_STR);
106 }
Cheng Chena97394f2017-09-27 15:05:14 -0700107#else
Fangwen Fu231fe422017-04-24 17:52:29 -0700108 abs =
109 aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
Cheng Chena97394f2017-09-27 15:05:14 -0700110#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700111 smallval = (abs < DELTA_LF_SMALL);
112 if (counts) {
Cheng Chena97394f2017-09-27 15:05:14 -0700113#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -0700114 if (cm->delta_lf_multi) {
115 for (i = 0; i < abs; ++i) counts->delta_lf_multi[lf_id][i][1]++;
116 if (smallval) counts->delta_lf_multi[lf_id][abs][0]++;
117 } else {
118 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
119 if (smallval) counts->delta_lf[abs][0]++;
120 }
Cheng Chena97394f2017-09-27 15:05:14 -0700121#else
Fangwen Fu231fe422017-04-24 17:52:29 -0700122 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
123 if (smallval) counts->delta_lf[abs][0]++;
Cheng Chena97394f2017-09-27 15:05:14 -0700124#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700125 }
126 if (!smallval) {
Thomas Davies3b93e8e2017-09-20 09:59:07 +0100127 rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
Fangwen Fu231fe422017-04-24 17:52:29 -0700128 thr = (1 << rem_bits) + 1;
129 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
130 }
131
132 if (abs) {
133 sign = aom_read_bit(r, ACCT_STR);
134 } else {
135 sign = 1;
136 }
137
138 reduced_delta_lflevel = sign ? -abs : abs;
139 }
140 return reduced_delta_lflevel;
141}
142#endif
Thomas Daviesf6936102016-09-05 16:51:31 +0100143
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400144static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
Hui Suaa2965e2017-10-05 15:59:12 -0700145 aom_reader *r,
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400146 PREDICTION_MODE y_mode) {
147 const UV_PREDICTION_MODE uv_mode =
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400148#if CONFIG_CFL
149 aom_read_symbol(r, ec_ctx->uv_mode_cdf[y_mode], UV_INTRA_MODES, ACCT_STR);
150#else
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000151 read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400152#endif // CONFIG_CFL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 return uv_mode;
154}
155
Luc Trudeauf5334002017-04-25 12:21:26 -0400156#if CONFIG_CFL
David Michael Barr23198662017-06-19 23:19:48 +0900157static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
David Michael Barrf6eaa152017-07-19 19:42:28 +0900158 int *signs_out) {
159 const int joint_sign =
160 aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
161 int idx = 0;
162 // Magnitudes are only coded for nonzero values
163 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
164 aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
165 idx = aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
166 << CFL_ALPHABET_SIZE_LOG2;
167 }
168 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
169 aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
170 idx += aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
171 }
172 *signs_out = joint_sign;
173 return idx;
Luc Trudeauf5334002017-04-25 12:21:26 -0400174}
175#endif
176
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200177#if CONFIG_INTERINTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700178static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
179 aom_reader *r, int size_group) {
Thomas Davies299ff042017-06-27 13:41:59 +0100180 (void)cm;
181 const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
182 r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
183 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 FRAME_COUNTS *counts = xd->counts;
185 if (counts) ++counts->interintra_mode[size_group][ii_mode];
186 return ii_mode;
187}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200188#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189
Thomas Davies1de6c882017-01-11 17:47:49 +0000190static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700191 aom_reader *r, int16_t ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 FRAME_COUNTS *counts = xd->counts;
193 int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100194 int is_newmv, is_zeromv, is_refmv;
195#if CONFIG_NEW_MULTISYMBOL
196 is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
197#else
198 is_newmv = aom_read(r, ec_ctx->newmv_prob[mode_ctx], ACCT_STR) == 0;
199#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200
Thomas Davies149eda52017-06-12 18:11:55 +0100201 if (is_newmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202 if (counts) ++counts->newmv_mode[mode_ctx][0];
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700203 return NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 }
205 if (counts) ++counts->newmv_mode[mode_ctx][1];
206
207 if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
208
209 mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
210
Thomas Davies149eda52017-06-12 18:11:55 +0100211#if CONFIG_NEW_MULTISYMBOL
212 is_zeromv =
213 aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
214#else
215 is_zeromv = aom_read(r, ec_ctx->zeromv_prob[mode_ctx], ACCT_STR) == 0;
216#endif
217 if (is_zeromv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218 if (counts) ++counts->zeromv_mode[mode_ctx][0];
219 return ZEROMV;
220 }
221 if (counts) ++counts->zeromv_mode[mode_ctx][1];
222
223 mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
224
225 if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
226 if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
227 if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
228
Thomas Davies149eda52017-06-12 18:11:55 +0100229#if CONFIG_NEW_MULTISYMBOL
230 is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
231#else
232 is_refmv = aom_read(r, ec_ctx->refmv_prob[mode_ctx], ACCT_STR) == 0;
233#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234
Thomas Davies149eda52017-06-12 18:11:55 +0100235 if (is_refmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700236 if (counts) ++counts->refmv_mode[mode_ctx][0];
237
238 return NEARESTMV;
239 } else {
240 if (counts) ++counts->refmv_mode[mode_ctx][1];
241 return NEARMV;
242 }
243
244 // Invalid prediction mode.
245 assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246}
247
Thomas Davies149eda52017-06-12 18:11:55 +0100248static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 MB_MODE_INFO *mbmi, aom_reader *r) {
250 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 mbmi->ref_mv_idx = 0;
252
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200253 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700254#if CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700255 || mbmi->mode == SR_NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700256#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700257 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 int idx;
259 for (idx = 0; idx < 2; ++idx) {
260 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700261 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100262#if CONFIG_NEW_MULTISYMBOL
263 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
264#else
265 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
266#endif
267 mbmi->ref_mv_idx = idx + drl_idx;
268 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
269 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 }
271 }
272 }
273
David Barker3dfba992017-04-03 16:10:09 +0100274 if (have_nearmv_in_inter_mode(mbmi->mode)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700275 int idx;
276 // Offset the NEARESTMV mode.
277 // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
278 // mode is factored in.
279 for (idx = 1; idx < 3; ++idx) {
280 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700281 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100282#if CONFIG_NEW_MULTISYMBOL
283 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
284#else
285 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
286#endif
287 mbmi->ref_mv_idx = idx + drl_idx - 1;
288 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
289 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 }
291 }
292 }
293}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294
Yaowu Xub24e1152016-10-31 16:28:32 -0700295#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
296static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -0800297 MODE_INFO *mi, aom_reader *r) {
298 MB_MODE_INFO *mbmi = &mi->mbmi;
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700299#if !CONFIG_MOTION_VAR || !CONFIG_WARPED_MOTION || CONFIG_NEW_MULTISYMBOL || \
300 CONFIG_NCOBMC_ADAPT_WEIGHT
Thomas Davies04e5aa72017-06-28 14:36:39 +0100301 (void)cm;
302#endif
303
Sarah Parker19234cc2017-03-10 16:43:25 -0800304 const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
Sarah Parker0eea89f2017-07-11 11:56:36 -0700305#if CONFIG_GLOBAL_MOTION
Sarah Parker19234cc2017-03-10 16:43:25 -0800306 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700307#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -0700308#if CONFIG_WARPED_MOTION
309 xd,
310#endif
Sarah Parker19234cc2017-03-10 16:43:25 -0800311 mi);
Yue Chen69f18e12016-09-08 14:48:15 -0700312 int motion_mode;
313 FRAME_COUNTS *counts = xd->counts;
Yaowu Xub24e1152016-10-31 16:28:32 -0700314
Yue Chen69f18e12016-09-08 14:48:15 -0700315 if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
316#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700317#if CONFIG_NCOBMC_ADAPT_WEIGHT
318 if (last_motion_mode_allowed == NCOBMC_ADAPT_WEIGHT) {
319 motion_mode = aom_read_symbol(r, xd->tile_ctx->ncobmc_cdf[mbmi->sb_type],
320 OBMC_FAMILY_MODES, ACCT_STR);
321 if (counts) ++counts->ncobmc[mbmi->sb_type][motion_mode];
322 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
323 } else if (last_motion_mode_allowed == OBMC_CAUSAL) {
324 motion_mode =
325 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
326 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
327 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
328 } else {
329#else
Yue Chen69f18e12016-09-08 14:48:15 -0700330 if (last_motion_mode_allowed == OBMC_CAUSAL) {
Thomas Daviesd9b57262017-06-27 17:43:25 +0100331#if CONFIG_NEW_MULTISYMBOL
332 motion_mode =
333 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
334#else
Yue Chen69f18e12016-09-08 14:48:15 -0700335 motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
Thomas Daviesd9b57262017-06-27 17:43:25 +0100336#endif
Yue Chen69f18e12016-09-08 14:48:15 -0700337 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
338 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
339 } else {
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700340#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yue Chen69f18e12016-09-08 14:48:15 -0700341#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700342 motion_mode =
Thomas Davies04e5aa72017-06-28 14:36:39 +0100343 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
344 MOTION_MODES, ACCT_STR);
Yaowu Xub24e1152016-10-31 16:28:32 -0700345 if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
346 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yue Chen69f18e12016-09-08 14:48:15 -0700347#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700348 }
Yue Chen69f18e12016-09-08 14:48:15 -0700349#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700350}
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700351
352#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -0700353static void read_ncobmc_mode(MACROBLOCKD *xd, MODE_INFO *mi,
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700354 NCOBMC_MODE ncobmc_mode[2], aom_reader *r) {
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700355 MB_MODE_INFO *mbmi = &mi->mbmi;
356 FRAME_COUNTS *counts = xd->counts;
357 ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];
Wei-Ting Lin77c41182017-07-12 15:58:35 -0700358 if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) return;
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700359
Wei-Ting Linca710d62017-07-13 11:41:02 -0700360 ncobmc_mode[0] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
361 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700362 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0]];
363
364 if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
Wei-Ting Linca710d62017-07-13 11:41:02 -0700365 ncobmc_mode[1] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
366 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700367 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1]];
368 }
369}
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700370#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yaowu Xub24e1152016-10-31 16:28:32 -0700371#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
372
Yaowu Xuf883b422016-08-30 14:01:10 -0700373static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
374 aom_reader *r, int16_t ctx) {
Thomas Davies8c08a332017-06-26 17:30:34 +0100375 (void)cm;
376 const int mode =
377 aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
378 INTER_COMPOUND_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 FRAME_COUNTS *counts = xd->counts;
380
381 if (counts) ++counts->inter_compound_mode[ctx][mode];
382
383 assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
384 return NEAREST_NEARESTMV + mode;
385}
Zoe Liu85b66462017-04-20 14:28:19 -0700386
387#if CONFIG_COMPOUND_SINGLEREF
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100388static PREDICTION_MODE read_inter_singleref_comp_mode(MACROBLOCKD *xd,
Zoe Liu85b66462017-04-20 14:28:19 -0700389 aom_reader *r,
390 int16_t ctx) {
391 const int mode =
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100392 aom_read_symbol(r, xd->tile_ctx->inter_singleref_comp_mode_cdf[ctx],
393 INTER_SINGLEREF_COMP_MODES, ACCT_STR);
Zoe Liu85b66462017-04-20 14:28:19 -0700394 FRAME_COUNTS *counts = xd->counts;
395
396 if (counts) ++counts->inter_singleref_comp_mode[ctx][mode];
397
398 assert(is_inter_singleref_comp_mode(SR_NEAREST_NEARMV + mode));
399 return SR_NEAREST_NEARMV + mode;
400}
401#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402
Thomas9ac55082016-09-23 18:04:17 +0100403static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700404 return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700405}
406
407#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700408static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700409 MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700410 TX_SIZE tx_size, int depth, int blk_row,
411 int blk_col, aom_reader *r) {
Thomas Davies985bfc32017-06-27 16:51:26 +0100412#if CONFIG_NEW_MULTISYMBOL
413 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
414 (void)cm;
415#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700416 int is_split = 0;
417 const int tx_row = blk_row >> 1;
418 const int tx_col = blk_col >> 1;
Jingning Hanf64062f2016-11-02 16:22:18 -0700419 const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
420 const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
Jingning Han331662e2017-05-30 17:03:32 -0700421 int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
422 xd->left_txfm_context + blk_row,
Jingning Hanc8b89362016-11-01 10:28:53 -0700423 mbmi->sb_type, tx_size);
clang-format67948d32016-09-07 22:40:40 -0700424 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425 [MAX_MIB_SIZE] =
426 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700427 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
David Barker16c64e32017-08-23 16:54:59 +0100428 assert(tx_size > TX_4X4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429
Jingning Han571189c2016-10-24 10:38:43 -0700430 if (depth == MAX_VARTX_DEPTH) {
Jingning Han94d5bfc2016-10-21 10:14:36 -0700431 int idx, idy;
432 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700433 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
434 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han94d5bfc2016-10-21 10:14:36 -0700435 inter_tx_size[idy][idx] = tx_size;
436 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700437 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Jingning Han331662e2017-05-30 17:03:32 -0700438 txfm_partition_update(xd->above_txfm_context + blk_col,
439 xd->left_txfm_context + blk_row, tx_size, tx_size);
Jingning Han94d5bfc2016-10-21 10:14:36 -0700440 return;
441 }
442
Thomas Davies985bfc32017-06-27 16:51:26 +0100443#if CONFIG_NEW_MULTISYMBOL
444 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
445#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700446 is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
Thomas Davies985bfc32017-06-27 16:51:26 +0100447#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448
449 if (is_split) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700450 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
451 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452 int i;
453
454 if (counts) ++counts->txfm_partition[ctx][1];
455
David Barker16c64e32017-08-23 16:54:59 +0100456 if (sub_txs == TX_4X4) {
Jingning Han9ca05b72017-01-03 14:41:36 -0800457 int idx, idy;
Jingning Hanab9ecba2017-01-13 09:11:58 -0800458 inter_tx_size[0][0] = sub_txs;
Jingning Han9ca05b72017-01-03 14:41:36 -0800459 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
460 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han581d1692017-01-05 16:03:54 -0800461 inter_tx_size[idy][idx] = inter_tx_size[0][0];
Jingning Hanab9ecba2017-01-13 09:11:58 -0800462 mbmi->tx_size = sub_txs;
Jingning Hane67b38a2016-11-04 10:30:00 -0700463 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han331662e2017-05-30 17:03:32 -0700464 txfm_partition_update(xd->above_txfm_context + blk_col,
465 xd->left_txfm_context + blk_row, sub_txs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700466 return;
467 }
468
469 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470 for (i = 0; i < 4; ++i) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700471 int offsetr = blk_row + (i >> 1) * bsl;
472 int offsetc = blk_col + (i & 0x01) * bsl;
473 read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700474 offsetc, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475 }
476 } else {
477 int idx, idy;
478 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700479 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
480 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481 inter_tx_size[idy][idx] = tx_size;
482 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700483 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700485 txfm_partition_update(xd->above_txfm_context + blk_col,
486 xd->left_txfm_context + blk_row, tx_size, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 }
488}
489#endif
490
Yaowu Xuf883b422016-08-30 14:01:10 -0700491static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
Urvang Joshiab8840e2017-10-06 16:38:24 -0700492 int32_t tx_size_cat, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493 FRAME_COUNTS *counts = xd->counts;
494 const int ctx = get_tx_size_context(xd);
Thomas Davies15580c52017-03-09 13:53:42 +0000495 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
496 (void)cm;
Thomas Davies15580c52017-03-09 13:53:42 +0000497
Nathan E. Egge476c63c2017-05-18 18:35:16 -0400498 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
499 tx_size_cat + 2, ACCT_STR);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800500 const TX_SIZE tx_size = depth_to_tx_size(depth);
501#if CONFIG_RECT_TX
502 assert(!is_rect_tx(tx_size));
503#endif // CONFIG_RECT_TX
Jingning Han906be072016-10-26 11:04:31 -0700504 if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
Jingning Han4e1737a2016-10-25 16:05:02 -0700505 return tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506}
507
Urvang Joshifeb925f2016-12-05 10:37:29 -0800508static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
509 int allow_select_inter, aom_reader *r) {
510 const TX_MODE tx_mode = cm->tx_mode;
511 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Rupert Swarbrickfcff0b22017-10-05 09:26:04 +0100513
514 if (block_signals_txsize(bsize)) {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800515 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
516 const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
517 : intra_tx_size_cat_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518 const TX_SIZE coded_tx_size =
Urvang Joshifeb925f2016-12-05 10:37:29 -0800519 read_selected_tx_size(cm, xd, tx_size_cat, r);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700520#if CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521 if (coded_tx_size > max_txsize_lookup[bsize]) {
522 assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
Yue Chend6bdd462017-07-19 16:05:43 -0700523#if CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700524 if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
Yue Chend6bdd462017-07-19 16:05:43 -0700525 int quarter_tx;
Yue Chen56e226e2017-05-02 16:21:40 -0700526
Yue Chend6bdd462017-07-19 16:05:43 -0700527 if (quarter_txsize_lookup[bsize] != max_txsize_lookup[bsize]) {
Thomas Daviese3f69782017-10-03 10:43:17 +0100528#if CONFIG_NEW_MULTISYMBOL
529 quarter_tx =
530 aom_read_symbol(r, cm->fc->quarter_tx_size_cdf, 2, ACCT_STR);
531#else
Yue Chend6bdd462017-07-19 16:05:43 -0700532 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
533 FRAME_COUNTS *counts = xd->counts;
Yue Chend6bdd462017-07-19 16:05:43 -0700534 if (counts) ++counts->quarter_tx_size[quarter_tx];
Thomas Daviese3f69782017-10-03 10:43:17 +0100535#endif
Yue Chend6bdd462017-07-19 16:05:43 -0700536 } else {
537 quarter_tx = 1;
538 }
Yue Chen56e226e2017-05-02 16:21:40 -0700539 return quarter_tx ? quarter_txsize_lookup[bsize]
540 : max_txsize_rect_lookup[bsize];
541 }
Yue Chend6bdd462017-07-19 16:05:43 -0700542#endif // CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700543
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 return max_txsize_rect_lookup[bsize];
545 }
Peter de Rivaza7c81462016-09-26 14:20:13 +0100546#else
547 assert(coded_tx_size <= max_txsize_lookup[bsize]);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700548#endif // CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 return coded_tx_size;
550 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800551 return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 }
553 } else {
554#if CONFIG_EXT_TX && CONFIG_RECT_TX
555 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
556 return max_txsize_rect_lookup[bsize];
557#else
558 return TX_4X4;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800559#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560 }
561}
562
Yaowu Xuf883b422016-08-30 14:01:10 -0700563static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 int mi_offset, int x_mis, int y_mis) {
565 int x, y, segment_id = INT_MAX;
566
567 for (y = 0; y < y_mis; y++)
568 for (x = 0; x < x_mis; x++)
569 segment_id =
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571
572 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
573 return segment_id;
574}
575
Yaowu Xuf883b422016-08-30 14:01:10 -0700576static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577 int segment_id) {
578 int x, y;
579
580 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
581
582 for (y = 0; y < y_mis; y++)
583 for (x = 0; x < x_mis; x++)
584 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
585}
586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588 int mi_offset, int x_mis, int y_mis,
Yaowu Xuf883b422016-08-30 14:01:10 -0700589 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590 struct segmentation *const seg = &cm->seg;
591 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100592 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100593 struct segmentation_probs *const segp = &ec_ctx->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594 int segment_id;
595
596 if (!seg->enabled) return 0; // Default for disabled segmentation
597
598 assert(seg->update_map && !seg->temporal_update);
599
600 segment_id = read_segment_id(r, segp);
601 if (counts) ++counts->seg.tree_total[segment_id];
602 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
603 return segment_id;
604}
605
Yaowu Xuf883b422016-08-30 14:01:10 -0700606static void copy_segment_id(const AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 const uint8_t *last_segment_ids,
608 uint8_t *current_segment_ids, int mi_offset,
609 int x_mis, int y_mis) {
610 int x, y;
611
612 for (y = 0; y < y_mis; y++)
613 for (x = 0; x < x_mis; x++)
614 current_segment_ids[mi_offset + y * cm->mi_cols + x] =
615 last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
616 : 0;
617}
618
Yaowu Xuf883b422016-08-30 14:01:10 -0700619static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
620 int mi_row, int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 struct segmentation *const seg = &cm->seg;
622 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100623 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100624 struct segmentation_probs *const segp = &ec_ctx->seg;
625
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
627 int predicted_segment_id, segment_id;
628 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Hanc709e1f2016-12-06 14:48:09 -0800629 const int bw = mi_size_wide[mbmi->sb_type];
630 const int bh = mi_size_high[mbmi->sb_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631
632 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -0700633 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
634 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635
636 if (!seg->enabled) return 0; // Default for disabled segmentation
637
638 predicted_segment_id = cm->last_frame_seg_map
639 ? dec_get_segment_id(cm, cm->last_frame_seg_map,
640 mi_offset, x_mis, y_mis)
641 : 0;
642
643 if (!seg->update_map) {
644 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
645 mi_offset, x_mis, y_mis);
646 return predicted_segment_id;
647 }
648
649 if (seg->temporal_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 const int ctx = av1_get_pred_context_seg_id(xd);
Thomas Davies00021352017-07-11 16:07:55 +0100651#if CONFIG_NEW_MULTISYMBOL
652 aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
653 mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
654#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700655 const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700656 mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
Thomas Davies00021352017-07-11 16:07:55 +0100657#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
659 if (mbmi->seg_id_predicted) {
660 segment_id = predicted_segment_id;
661 } else {
662 segment_id = read_segment_id(r, segp);
663 if (counts) ++counts->seg.tree_mispred[segment_id];
664 }
665 } else {
666 segment_id = read_segment_id(r, segp);
667 if (counts) ++counts->seg.tree_total[segment_id];
668 }
669 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
670 return segment_id;
671}
672
Yaowu Xuf883b422016-08-30 14:01:10 -0700673static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
674 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
676 return 1;
677 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 const int ctx = av1_get_skip_context(xd);
Thomas Davies61e3e372017-04-04 16:10:23 +0100679#if CONFIG_NEW_MULTISYMBOL
680 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
681 const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
682#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700683 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Thomas Davies61e3e372017-04-04 16:10:23 +0100684#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700685 FRAME_COUNTS *counts = xd->counts;
686 if (counts) ++counts->skip[ctx][skip];
687 return skip;
688 }
689}
690
hui su33567b22017-04-30 16:40:19 -0700691#if CONFIG_PALETTE_DELTA_ENCODING
Hui Su4494ae42017-10-04 12:22:44 -0700692// Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
693// and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
694// one single sorted list(colors[...]).
695static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
696 int n_colors, int n_cached_colors) {
697 if (n_cached_colors == 0) return;
698 int cache_idx = 0, trans_idx = n_cached_colors;
699 for (int i = 0; i < n_colors; ++i) {
700 if (cache_idx < n_cached_colors &&
701 (trans_idx >= n_colors ||
702 cached_colors[cache_idx] <= colors[trans_idx])) {
703 colors[i] = cached_colors[cache_idx++];
704 } else {
705 assert(trans_idx < n_colors);
706 colors[i] = colors[trans_idx++];
707 }
708 }
hui su33567b22017-04-30 16:40:19 -0700709}
hui su33567b22017-04-30 16:40:19 -0700710
711static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
712 PALETTE_MODE_INFO *const pmi, aom_reader *r) {
713 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su4494ae42017-10-04 12:22:44 -0700714 uint16_t cached_colors[PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700715 const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
hui su33567b22017-04-30 16:40:19 -0700716 const int n = pmi->palette_size[0];
717 int idx = 0;
718 for (int i = 0; i < n_cache && idx < n; ++i)
Hui Su4494ae42017-10-04 12:22:44 -0700719 if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
hui su33567b22017-04-30 16:40:19 -0700720 if (idx < n) {
Hui Su4494ae42017-10-04 12:22:44 -0700721 const int n_cached_colors = idx;
hui su33567b22017-04-30 16:40:19 -0700722 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
723 if (idx < n) {
724 const int min_bits = bit_depth - 3;
725 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
726 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
727 for (; idx < n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100728 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700729 const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100730 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
731 0, (1 << bit_depth) - 1);
732 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700733 bits = AOMMIN(bits, av1_ceil_log2(range));
734 }
735 }
Hui Su4494ae42017-10-04 12:22:44 -0700736 merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
737 } else {
738 memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
hui su33567b22017-04-30 16:40:19 -0700739 }
hui su33567b22017-04-30 16:40:19 -0700740}
741
742static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
743 PALETTE_MODE_INFO *const pmi,
744 aom_reader *r) {
745 const int n = pmi->palette_size[1];
746 // U channel colors.
747 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su4494ae42017-10-04 12:22:44 -0700748 uint16_t cached_colors[PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700749 const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
Hui Su4494ae42017-10-04 12:22:44 -0700750 int idx = 0;
751 for (int i = 0; i < n_cache && idx < n; ++i)
752 if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
753 if (idx < n) {
754 const int n_cached_colors = idx;
755 idx += PALETTE_MAX_SIZE;
hui su33567b22017-04-30 16:40:19 -0700756 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
757 if (idx < PALETTE_MAX_SIZE + n) {
758 const int min_bits = bit_depth - 3;
759 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
760 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
761 for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100762 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700763 const int delta = aom_read_literal(r, bits, ACCT_STR);
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100764 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
765 0, (1 << bit_depth) - 1);
766 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700767 bits = AOMMIN(bits, av1_ceil_log2(range));
768 }
769 }
Hui Su4494ae42017-10-04 12:22:44 -0700770 merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
771 n_cached_colors);
772 } else {
773 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
774 n * sizeof(cached_colors[0]));
hui su33567b22017-04-30 16:40:19 -0700775 }
hui su33567b22017-04-30 16:40:19 -0700776
777 // V channel colors.
778 if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
779 const int min_bits_v = bit_depth - 4;
780 const int max_val = 1 << bit_depth;
781 int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
782 pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
783 aom_read_literal(r, bit_depth, ACCT_STR);
784 for (int i = 1; i < n; ++i) {
785 int delta = aom_read_literal(r, bits, ACCT_STR);
786 if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
787 int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
788 if (val < 0) val += max_val;
789 if (val >= max_val) val -= max_val;
790 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
791 }
792 } else {
793 for (int i = 0; i < n; ++i) {
794 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
795 aom_read_literal(r, bit_depth, ACCT_STR);
796 }
797 }
798}
799#endif // CONFIG_PALETTE_DELTA_ENCODING
800
Yaowu Xuf883b422016-08-30 14:01:10 -0700801static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
802 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 MODE_INFO *const mi = xd->mi[0];
804 MB_MODE_INFO *const mbmi = &mi->mbmi;
805 const MODE_INFO *const above_mi = xd->above_mi;
806 const MODE_INFO *const left_mi = xd->left_mi;
807 const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
809
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100810 assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST);
811 const int block_palette_idx = bsize - BLOCK_8X8;
Thomas Davies59f92312017-08-23 00:33:12 +0100812 int modev;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100813
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814 if (mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800815 int palette_y_mode_ctx = 0;
hui su40b9e7f2017-07-13 18:15:56 -0700816 if (above_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800817 palette_y_mode_ctx +=
818 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700819 }
820 if (left_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800821 palette_y_mode_ctx +=
822 (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700823 }
Thomas Davies59f92312017-08-23 00:33:12 +0100824#if CONFIG_NEW_MULTISYMBOL
825 modev = aom_read_symbol(
826 r,
827 xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
828 2, ACCT_STR);
829#else
830 modev = aom_read(
831 r,
832 av1_default_palette_y_mode_prob[block_palette_idx][palette_y_mode_ctx],
833 ACCT_STR);
834#endif
835 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836 pmi->palette_size[0] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100837 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100838 xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100839 PALETTE_SIZES, ACCT_STR) +
840 2;
hui sud13c24a2017-04-07 16:13:07 -0700841#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700842 read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700843#else
hui su40b9e7f2017-07-13 18:15:56 -0700844 for (int i = 0; i < pmi->palette_size[0]; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700845 pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui sud13c24a2017-04-07 16:13:07 -0700846#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847 }
848 }
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400849 if (mbmi->uv_mode == UV_DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800850 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
Thomas Davies59f92312017-08-23 00:33:12 +0100851#if CONFIG_NEW_MULTISYMBOL
852 modev = aom_read_symbol(
853 r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
854#else
855 modev = aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
856 ACCT_STR);
857#endif
858 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 pmi->palette_size[1] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100860 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100861 xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100862 PALETTE_SIZES, ACCT_STR) +
863 2;
hui sud13c24a2017-04-07 16:13:07 -0700864#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700865 read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700866#else
hui su40b9e7f2017-07-13 18:15:56 -0700867 for (int i = 0; i < pmi->palette_size[1]; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700869 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700871 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 }
hui sud13c24a2017-04-07 16:13:07 -0700873#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 }
875 }
876}
877
hui su5db97432016-10-14 16:10:14 -0700878#if CONFIG_FILTER_INTRA
879static void read_filter_intra_mode_info(AV1_COMMON *const cm,
Jingning Han62946d12017-05-26 11:29:30 -0700880 MACROBLOCKD *const xd, int mi_row,
881 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 MODE_INFO *const mi = xd->mi[0];
883 MB_MODE_INFO *const mbmi = &mi->mbmi;
884 FRAME_COUNTS *counts = xd->counts;
hui su5db97432016-10-14 16:10:14 -0700885 FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
886 &mbmi->filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887
Urvang Joshic6300aa2017-06-01 14:46:23 -0700888 if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0) {
hui su5db97432016-10-14 16:10:14 -0700889 filter_intra_mode_info->use_filter_intra_mode[0] =
890 aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
891 if (filter_intra_mode_info->use_filter_intra_mode[0]) {
892 filter_intra_mode_info->filter_intra_mode[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700893 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894 }
hui su5db97432016-10-14 16:10:14 -0700895 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800896 ++counts
897 ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
hui su5db97432016-10-14 16:10:14 -0700898 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 }
Jingning Han62946d12017-05-26 11:29:30 -0700900
901#if CONFIG_CB4X4
902 if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
903 xd->plane[1].subsampling_x,
904 xd->plane[1].subsampling_y))
905 return;
hui sub4ed1492017-05-31 17:25:42 -0700906#else
907 (void)mi_row;
908 (void)mi_col;
909#endif // CONFIG_CB4X4
Jingning Han62946d12017-05-26 11:29:30 -0700910
Urvang Joshic6300aa2017-06-01 14:46:23 -0700911 if (mbmi->uv_mode == UV_DC_PRED &&
912 mbmi->palette_mode_info.palette_size[1] == 0) {
hui su5db97432016-10-14 16:10:14 -0700913 filter_intra_mode_info->use_filter_intra_mode[1] =
914 aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
915 if (filter_intra_mode_info->use_filter_intra_mode[1]) {
916 filter_intra_mode_info->filter_intra_mode[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700917 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700918 }
hui su5db97432016-10-14 16:10:14 -0700919 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800920 ++counts
921 ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
hui su5db97432016-10-14 16:10:14 -0700922 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923 }
924}
hui su5db97432016-10-14 16:10:14 -0700925#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926
hui su5db97432016-10-14 16:10:14 -0700927#if CONFIG_EXT_INTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700928static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
929 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700930 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
931 const BLOCK_SIZE bsize = mbmi->sb_type;
hui sueda3d762016-12-06 16:58:23 -0800932#if CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -0800933 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
Yaowu Xuf883b422016-08-30 14:01:10 -0700934 const int ctx = av1_get_pred_context_intra_interp(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935 int p_angle;
hui sueda3d762016-12-06 16:58:23 -0800936#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700937
hui sueda3d762016-12-06 16:58:23 -0800938 (void)cm;
Joe Young830d4ce2017-05-30 17:48:13 -0700939
940 mbmi->angle_delta[0] = 0;
941 mbmi->angle_delta[1] = 0;
Jonathan Matthews67c9ab02017-09-27 13:38:50 +0100942#if CONFIG_INTRA_INTERP
943 mbmi->intra_filter = INTRA_FILTER_LINEAR;
944#endif // CONFIG_INTRA_INTERP
Joe Young830d4ce2017-05-30 17:48:13 -0700945
946 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947
hui su45dc5972016-12-08 17:42:50 -0800948 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700950 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
hui sueda3d762016-12-06 16:58:23 -0800951#if CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -0700952 p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -0700953 if (av1_is_intra_filter_switchable(p_angle)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700954 FRAME_COUNTS *counts = xd->counts;
hui sub4e25d22017-03-09 15:32:30 -0800955 mbmi->intra_filter = aom_read_symbol(r, ec_ctx->intra_filter_cdf[ctx],
956 INTRA_FILTERS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700958 }
hui sueda3d762016-12-06 16:58:23 -0800959#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 }
961
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400962 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700963 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700964 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700965 }
966}
967#endif // CONFIG_EXT_INTRA
968
Angie Chianga9f9a312017-04-13 16:40:43 -0700969void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Hanab7163d2016-11-04 09:46:35 -0700970#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -0700971 int supertx_enabled,
Jingning Hanab7163d2016-11-04 09:46:35 -0700972#endif
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700973#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700974 int blk_row, int blk_col, int block, int plane,
975 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700976#endif
977 aom_reader *r) {
978 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700979 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700980#if !CONFIG_TXK_SEL
Jingning Hane67b38a2016-11-04 10:30:00 -0700981#if CONFIG_VAR_TX
982 const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
983#else
Jingning Hanab7163d2016-11-04 09:46:35 -0700984 const TX_SIZE tx_size = mbmi->tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700985#endif
Jingning Han243b66b2017-06-23 12:11:47 -0700986#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000987 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000988
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700989#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700990 TX_TYPE *tx_type = &mbmi->tx_type;
991#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700992 // only y plane's tx_type is transmitted
993 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700994 (void)block;
995 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << 4) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700996#endif
997
Jingning Hanab7163d2016-11-04 09:46:35 -0700998 if (!FIXED_TX_TYPE) {
999#if CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -08001000 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -08001001 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
1002 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -08001003 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1004 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1005 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001006#if CONFIG_SUPERTX
1007 !supertx_enabled &&
1008#endif // CONFIG_SUPERTX
1009 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Suddbcde22017-09-18 17:22:02 -07001010 const TxSetType tx_set_type = get_ext_tx_set_type(
1011 tx_size, mbmi->sb_type, inter_block, cm->reduced_tx_set_used);
Sarah Parkere68a3e42017-02-16 14:03:24 -08001012 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
1013 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -07001014 // eset == 0 should correspond to a set with only DCT_DCT and
1015 // there is no need to read the tx_type
1016 assert(eset != 0);
Jingning Hanab7163d2016-11-04 09:46:35 -07001017 if (inter_block) {
Hui Suddbcde22017-09-18 17:22:02 -07001018 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -07001019 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
Hui Suddbcde22017-09-18 17:22:02 -07001020 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Jingning Hanab7163d2016-11-04 09:46:35 -07001021 } else if (ALLOW_INTRA_EXT_TX) {
Hui Suddbcde22017-09-18 17:22:02 -07001022 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -07001023 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
Hui Suddbcde22017-09-18 17:22:02 -07001024 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Jingning Hanab7163d2016-11-04 09:46:35 -07001025 }
1026 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001027 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001028 }
1029#else
Yue Cheneeacc4c2017-01-17 17:29:17 -08001030
1031 if (tx_size < TX_32X32 &&
1032 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1033 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1034 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001035#if CONFIG_SUPERTX
1036 !supertx_enabled &&
1037#endif // CONFIG_SUPERTX
1038 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Su98b0b3e2017-09-19 13:54:02 -07001039#if CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001040 FRAME_COUNTS *counts = xd->counts;
Hui Su98b0b3e2017-09-19 13:54:02 -07001041#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001042 if (inter_block) {
Angie Chianga9f9a312017-04-13 16:40:43 -07001043 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001044 r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001045#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001046 if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001047#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001048 } else {
1049 const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
Angie Chianga9f9a312017-04-13 16:40:43 -07001050 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001051 r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Hanab7163d2016-11-04 09:46:35 -07001052 ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001053#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001054 if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001055#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001056 }
1057 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001058 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001059 }
1060#endif // CONFIG_EXT_TX
1061 }
Nathan E. Eggeebced372017-02-17 20:57:21 -05001062#if FIXED_TX_TYPE
1063 assert(mbmi->tx_type == DCT_DCT);
1064#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001065}
1066
Alex Converse28744302017-04-13 14:46:22 -07001067#if CONFIG_INTRABC
1068static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
1069 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001070 MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -07001071
1072static INLINE int is_mv_valid(const MV *mv);
1073
1074static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
1075 const int_mv *ref_mv, int mi_row, int mi_col,
1076 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -07001077 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1078 (void)cm;
Alex Converse28744302017-04-13 14:46:22 -07001079 FRAME_COUNTS *counts = xd->counts;
1080 nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse6b2584c2017-05-02 09:51:21 -07001081 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
1082 MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -07001083 int valid = is_mv_valid(&mv->as_mv) &&
1084 is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize);
Alex Converse28744302017-04-13 14:46:22 -07001085 return valid;
1086}
1087#endif // CONFIG_INTRABC
1088
Yaowu Xuf883b422016-08-30 14:01:10 -07001089static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001091 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001092 MODE_INFO *const mi = xd->mi[0];
1093 MB_MODE_INFO *const mbmi = &mi->mbmi;
1094 const MODE_INFO *above_mi = xd->above_mi;
1095 const MODE_INFO *left_mi = xd->left_mi;
1096 const BLOCK_SIZE bsize = mbmi->sb_type;
1097 int i;
1098 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001099 const int bw = mi_size_wide[bsize];
1100 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001101
1102 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001103 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1104 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001105 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001106
1107 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1108 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Arild Fuldseth07441162016-08-15 15:07:52 +02001109
Arild Fuldseth07441162016-08-15 15:07:52 +02001110 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001111 xd->current_qindex =
1112 xd->prev_qindex +
1113 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001114 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1115 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001116 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001117#if CONFIG_EXT_DELTA_Q
1118 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07001119#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07001120 if (cm->delta_lf_multi) {
1121 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
1122 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
1123 xd->prev_delta_lf[lf_id] +
1124 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
1125 cm->delta_lf_res;
1126 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
1127 }
1128 } else {
1129 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1130 xd->prev_delta_lf_from_base +
1131 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07001132 cm->delta_lf_res;
Cheng Chen880166a2017-10-02 17:48:48 -07001133 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001134 }
1135#else
Cheng Chen9dccdf22017-10-02 15:04:40 -07001136 const int current_delta_lf_from_base =
Fangwen Fu231fe422017-04-24 17:52:29 -07001137 xd->prev_delta_lf_from_base +
1138 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1139 cm->delta_lf_res;
Cheng Chen9dccdf22017-10-02 15:04:40 -07001140 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1141 clamp(current_delta_lf_from_base, 0, MAX_LOOP_FILTER);
Fangwen Fu231fe422017-04-24 17:52:29 -07001142 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001143#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07001144 }
1145#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001146 }
Arild Fuldseth07441162016-08-15 15:07:52 +02001147
Yaowu Xuc27fc142016-08-22 16:08:15 -07001148 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001149 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001150
Alex Converse28744302017-04-13 14:46:22 -07001151#if CONFIG_INTRABC
RogerZhouca865462017-10-05 15:06:27 -07001152 if (av1_allow_intrabc(bsize, cm)) {
Hui Su6c8584f2017-09-14 15:37:02 -07001153 mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
Alex Converse28744302017-04-13 14:46:22 -07001154 if (mbmi->use_intrabc) {
Alex Conversef71808c2017-06-06 12:21:17 -07001155 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001156 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001157 mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
Alex Converse44c2bad2017-05-11 09:36:10 -07001158
1159 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Ryan95acd062017-09-29 14:56:44 -07001160 int_mv ref_mvs[MAX_MV_REF_CANDIDATES];
Alex Converse44c2bad2017-05-11 09:36:10 -07001161
1162 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02001163 xd->ref_mv_stack[INTRA_FRAME], NULL, ref_mvs, mi_row,
1164 mi_col, NULL, NULL, inter_mode_ctx);
Alex Converse44c2bad2017-05-11 09:36:10 -07001165
1166 int_mv nearestmv, nearmv;
1167 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1168
1169 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1170 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
1171
Alex Converse28744302017-04-13 14:46:22 -07001172 xd->corrupted |=
1173 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
Alex Conversee16b2662017-05-24 14:00:00 -07001174#if CONFIG_VAR_TX
1175 // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks
1176 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1177 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1178 int idx, idy;
1179 for (idy = 0; idy < height; ++idy)
1180 for (idx = 0; idx < width; ++idx)
1181 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1182 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1183#endif // CONFIG_VAR_TX
Alex Conversedaa15e42017-05-02 14:27:16 -07001184#if CONFIG_EXT_TX && !CONFIG_TXK_SEL
1185 av1_read_tx_type(cm, xd,
1186#if CONFIG_SUPERTX
1187 0,
1188#endif
1189 r);
1190#endif // CONFIG_EXT_TX && !CONFIG_TXK_SEL
Alex Converse28744302017-04-13 14:46:22 -07001191 return;
1192 }
1193 }
1194#endif // CONFIG_INTRABC
1195
Alex Conversef71808c2017-06-06 12:21:17 -07001196 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
1197
Jingning Han52261842016-12-14 12:17:49 -08001198#if CONFIG_CB4X4
1199 (void)i;
1200 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001201 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Jingning Han52261842016-12-14 12:17:49 -08001202#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001203 switch (bsize) {
1204 case BLOCK_4X4:
1205 for (i = 0; i < 4; ++i)
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001206 mi->bmi[i].as_mode = read_intra_mode(
1207 r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, i));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001208 mbmi->mode = mi->bmi[3].as_mode;
1209 break;
1210 case BLOCK_4X8:
1211 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001212 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001213 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001214 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001215 break;
1216 case BLOCK_8X4:
1217 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001218 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001219 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001220 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 2));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221 break;
1222 default:
1223 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001224 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001225 }
Jingning Han52261842016-12-14 12:17:49 -08001226#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001227
Jingning Han36fe3202017-02-20 22:31:49 -08001228#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001229 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001230 xd->plane[1].subsampling_y)) {
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001231#if CONFIG_CFL
1232 xd->cfl->is_chroma_reference = 1;
1233#endif // CONFIG_CFL
1234#endif // CONFIG_CB4X4
Hui Suaa2965e2017-10-05 15:59:12 -07001235 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001236
Luc Trudeauf5334002017-04-25 12:21:26 -04001237#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001238 if (mbmi->uv_mode == UV_CFL_PRED) {
David Michael Barrf6eaa152017-07-19 19:42:28 +09001239 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001240 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001241 } else {
1242 xd->cfl->store_y = 0;
Luc Trudeauf5334002017-04-25 12:21:26 -04001243 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001244#endif // CONFIG_CFL
1245
1246#if CONFIG_CB4X4
Joe Young830d4ce2017-05-30 17:48:13 -07001247 } else {
1248 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001249 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001250#if CONFIG_CFL
1251 xd->cfl->is_chroma_reference = 0;
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001252 xd->cfl->store_y = 1;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001253#endif
Luc Trudeauf5334002017-04-25 12:21:26 -04001254 }
1255#endif
1256
Yaowu Xuc27fc142016-08-22 16:08:15 -07001257#if CONFIG_EXT_INTRA
1258 read_intra_angle_info(cm, xd, r);
1259#endif // CONFIG_EXT_INTRA
1260 mbmi->palette_mode_info.palette_size[0] = 0;
1261 mbmi->palette_mode_info.palette_size[1] = 0;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +01001262 if (bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST &&
1263 cm->allow_screen_content_tools)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001264 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001265#if CONFIG_FILTER_INTRA
1266 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1267 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001268 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001269 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001270#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001271
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001272#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07001273 av1_read_tx_type(cm, xd,
Jingning Hanab7163d2016-11-04 09:46:35 -07001274#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07001275 0,
Nathan E. Egge72762a22016-09-07 17:12:07 -04001276#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07001277 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001278#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001279}
1280
Alex Converse6b2584c2017-05-02 09:51:21 -07001281static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
RogerZhou3b635242017-09-19 10:06:46 -07001282#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001283 int use_subpel,
RogerZhou3b635242017-09-19 10:06:46 -07001284#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001285 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286 int mag, d, fr, hp;
Thomas Davies599395e2017-07-21 18:02:48 +01001287#if CONFIG_NEW_MULTISYMBOL
1288 const int sign = aom_read_bit(r, ACCT_STR);
1289#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001290 const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001291#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07001292 const int mv_class =
Nathan E. Egged7b893c2016-09-08 15:08:48 -04001293 aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294 const int class0 = mv_class == MV_CLASS_0;
1295
1296 // Integer part
1297 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001298#if CONFIG_NEW_MULTISYMBOL
1299 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
1300#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -04001301 d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001302#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001303 mag = 0;
1304 } else {
1305 int i;
1306 const int n = mv_class + CLASS0_BITS - 1; // number of bits
Yaowu Xuc27fc142016-08-22 16:08:15 -07001307 d = 0;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001308#if CONFIG_NEW_MULTISYMBOL
1309 for (i = 0; i < n; ++i)
1310 d |= aom_read_symbol(r, mvcomp->bits_cdf[(i + 1) / 2], 2, ACCT_STR) << i;
1311#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001312 for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001313#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001314 mag = CLASS0_SIZE << (mv_class + 2);
1315 }
1316
RogerZhou3b635242017-09-19 10:06:46 -07001317#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001318 if (use_subpel) {
RogerZhou3b635242017-09-19 10:06:46 -07001319#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001320 // Fractional part
1321 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1322 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001323
Thomas Davies599395e2017-07-21 18:02:48 +01001324// High precision part (if hp is not used, the default value of the hp is 1)
1325#if CONFIG_NEW_MULTISYMBOL
1326 hp = usehp ? aom_read_symbol(
1327 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1328 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001329 : 1;
Thomas Davies599395e2017-07-21 18:02:48 +01001330#else
1331 hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
1332 : 1;
1333#endif
RogerZhou3b635242017-09-19 10:06:46 -07001334#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001335 } else {
1336 fr = 3;
1337 hp = 1;
1338 }
RogerZhou3b635242017-09-19 10:06:46 -07001339#endif // CONFIG_INTRABC || CONFIG_AMVR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001340
1341 // Result
1342 mag += ((d << 3) | (fr << 1) | hp) + 1;
1343 return sign ? -mag : mag;
1344}
1345
Yaowu Xuf883b422016-08-30 14:01:10 -07001346static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +01001347 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001348 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001350 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001351 joint_type =
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -04001352 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001353
1354 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001355 diff.row = read_mv_component(r, &ctx->comps[0],
RogerZhou3b635242017-09-19 10:06:46 -07001356#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001357 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001358#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001359 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360
1361 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001362 diff.col = read_mv_component(r, &ctx->comps[1],
RogerZhou3b635242017-09-19 10:06:46 -07001363#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001364 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001365#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001366 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001367
Alex Converse6b2584c2017-05-02 09:51:21 -07001368 av1_inc_mv(&diff, counts, precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001369
1370 mv->row = ref->row + diff.row;
1371 mv->col = ref->col + diff.col;
1372}
1373
Yaowu Xuf883b422016-08-30 14:01:10 -07001374static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001375 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001376 aom_reader *r) {
Debargha Mukherjee0f248c42017-09-07 12:40:18 -07001377 if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type)) return SINGLE_REFERENCE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001378 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001379 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001380#if CONFIG_NEW_MULTISYMBOL
1381 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1382 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
1383#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001384 const REFERENCE_MODE mode =
Michael Bebenita6048d052016-08-25 14:40:54 -07001385 (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001386#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387 FRAME_COUNTS *counts = xd->counts;
1388 if (counts) ++counts->comp_inter[ctx][mode];
1389 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1390 } else {
1391 return cm->reference_mode;
1392 }
1393}
1394
Thomas Davies315f5782017-06-14 15:14:55 +01001395#if CONFIG_NEW_MULTISYMBOL
1396#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001397 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001398#define READ_REF_BIT2(pname) \
1399 aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001400#else
1401#define READ_REF_BIT(pname) \
1402 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001403#define READ_REF_BIT2(pname) \
1404 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001405#endif
1406
Zoe Liuc082bbc2017-05-17 13:31:37 -07001407#if CONFIG_EXT_COMP_REFS
Zoe Liuec50d6b2017-08-23 16:02:59 -07001408static COMP_REFERENCE_TYPE read_comp_reference_type(AV1_COMMON *cm,
1409 const MACROBLOCKD *xd,
1410 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001411 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001412#if USE_UNI_COMP_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07001413 COMP_REFERENCE_TYPE comp_ref_type;
1414#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001415 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm)) {
1416 if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm)) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001417#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001418#if CONFIG_NEW_MULTISYMBOL
1419 (void)cm;
1420 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read_symbol(
1421 r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
1422#else
1423 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read(
1424 r, cm->fc->comp_ref_type_prob[ctx], ACCT_STR);
1425#endif
Zoe Liufcf5fa22017-06-26 16:00:38 -07001426#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001427 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001428 comp_ref_type = BIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001429 }
1430 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001431 comp_ref_type = UNIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001432 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001433#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001434#else // !USE_UNI_COMP_REFS
1435 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1436 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1437#endif // USE_UNI_COMP_REFS
1438 FRAME_COUNTS *counts = xd->counts;
1439 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1440 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1441}
1442#endif // CONFIG_EXT_COMP_REFS
1443
Yaowu Xuc27fc142016-08-22 16:08:15 -07001444// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001445static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1446 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001447 MV_REFERENCE_FRAME ref_frame[2]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001448 FRAME_COUNTS *counts = xd->counts;
1449
1450 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1451 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1452 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001453 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454 } else {
1455 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
1456 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1457 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001458#if CONFIG_EXT_COMP_REFS
1459 const COMP_REFERENCE_TYPE comp_ref_type =
1460 read_comp_reference_type(cm, xd, r);
1461
1462#if !USE_UNI_COMP_REFS
1463 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1464 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
1465#endif // !USE_UNI_COMP_REFS
1466
1467 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001468 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1469 int bit;
1470#if CONFIG_VAR_REFS
1471 if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
1472#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001473 bit = READ_REF_BIT2(uni_comp_ref_p);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001474#if CONFIG_VAR_REFS
1475 else
1476 bit = BWD_AND_ALT(cm);
1477#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001478 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1479
1480 if (bit) {
1481 ref_frame[0] = BWDREF_FRAME;
1482 ref_frame[1] = ALTREF_FRAME;
1483 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001484 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1485 int bit1;
1486#if CONFIG_VAR_REFS
1487 if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm)))
1488#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001489 bit1 = READ_REF_BIT2(uni_comp_ref_p1);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001490#if CONFIG_VAR_REFS
1491 else
1492 bit1 = L_AND_L3(cm) || L_AND_G(cm);
1493#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001494 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1495
1496 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001497 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1498 int bit2;
1499#if CONFIG_VAR_REFS
1500 if (L_AND_L3(cm) && L_AND_G(cm))
1501#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001502 bit2 = READ_REF_BIT2(uni_comp_ref_p2);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001503#if CONFIG_VAR_REFS
1504 else
1505 bit2 = L_AND_G(cm);
1506#endif // CONFIG_VAR_REFS
1507 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1508
1509 if (bit2) {
1510 ref_frame[0] = LAST_FRAME;
1511 ref_frame[1] = GOLDEN_FRAME;
1512 } else {
1513 ref_frame[0] = LAST_FRAME;
1514 ref_frame[1] = LAST3_FRAME;
1515 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001516 } else {
1517 ref_frame[0] = LAST_FRAME;
1518 ref_frame[1] = LAST2_FRAME;
1519 }
1520 }
1521
1522 return;
1523 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001524
1525 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001526#endif // CONFIG_EXT_COMP_REFS
1527
1528// Normative in decoder (for low delay)
Zoe Liu17af2742017-10-06 10:36:42 -07001529#if CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001530 const int idx = 1;
Zoe Liu17af2742017-10-06 10:36:42 -07001531#else // !(CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001532#if CONFIG_EXT_REFS
1533 const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
Zoe Liuc082bbc2017-05-17 13:31:37 -07001534#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001535 const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
1536#endif // CONFIG_EXT_REFS
Zoe Liu17af2742017-10-06 10:36:42 -07001537#endif // CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001538
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001539 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
1540#if CONFIG_VAR_REFS
1541 int bit;
1542 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1543 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001544 bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001545 else
1546 bit = L3_OR_G(cm);
1547#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001548 const int bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001549#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001550 if (counts) ++counts->comp_ref[ctx][0][bit];
1551
1552#if CONFIG_EXT_REFS
1553 // Decode forward references.
1554 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001555 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001556#if CONFIG_VAR_REFS
1557 int bit1;
1558 // Test need to explicitly code (L) vs (L2) branch node in tree
1559 if (L_AND_L2(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001560 bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001561 else
1562 bit1 = LAST_IS_VALID(cm);
1563#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001564 const int bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001565#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001566 if (counts) ++counts->comp_ref[ctx1][1][bit1];
1567 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
1568 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001569 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001570#if CONFIG_VAR_REFS
1571 int bit2;
1572 // Test need to explicitly code (L3) vs (G) branch node in tree
1573 if (L3_AND_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001574 bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001575 else
1576 bit2 = GOLDEN_IS_VALID(cm);
1577#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001578 const int bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001579#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001580 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1581 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1582 }
1583
1584 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001585 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
1586#if CONFIG_VAR_REFS
1587 int bit_bwd;
Zoe Liu3ac20932017-08-30 16:35:55 -07001588 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001589 const int bit_bwd_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001590 if (bit_bwd_uncertain)
Thomas Davies894cc812017-06-22 17:51:33 +01001591 bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001592 else
1593 bit_bwd = ALTREF_IS_VALID(cm);
Thomas Davies894cc812017-06-22 17:51:33 +01001594#else // !CONFIG_VAR_REFS
1595 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001596#endif // CONFIG_VAR_REFS
1597 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001598 if (!bit_bwd) {
1599 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
1600#if CONFIG_VAR_REFS
1601 int bit1_bwd;
1602 if (BWD_AND_ALT2(cm))
1603 bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1604 else
1605 bit1_bwd = ALTREF2_IS_VALID(cm);
1606#else // !CONFIG_VAR_REFS
1607 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1608#endif // CONFIG_VAR_REFS
1609 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1610 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1611 } else {
1612 ref_frame[idx] = cm->comp_bwd_ref[2];
1613 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001614#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001615 ref_frame[!idx] = cm->comp_var_ref[bit];
1616 ref_frame[idx] = cm->comp_fixed_ref;
1617#endif // CONFIG_EXT_REFS
1618 } else if (mode == SINGLE_REFERENCE) {
1619#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001620 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001621#if CONFIG_VAR_REFS
1622 int bit0;
Zoe Liue9b15e22017-07-19 15:53:01 -07001623 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT2,ALT) branch node
1624 // in tree
1625 if ((L_OR_L2(cm) || L3_OR_G(cm)) &&
1626 (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm)))
1627 bit0 = READ_REF_BIT(single_ref_p1);
1628 else
1629 bit0 = (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm));
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001630#else // !CONFIG_VAR_REFS
Thomas Davies315f5782017-06-14 15:14:55 +01001631 const int bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001632#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001633 if (counts) ++counts->single_ref[ctx0][0][bit0];
1634
1635 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001637#if CONFIG_VAR_REFS
1638 int bit1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001639 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001640 const int bit1_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001641 if (bit1_uncertain)
Thomas Davies315f5782017-06-14 15:14:55 +01001642 bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001643 else
1644 bit1 = ALTREF_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001645#else // !CONFIG_VAR_REFS
1646 const int bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001647#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001648 if (counts) ++counts->single_ref[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001649 if (!bit1) {
1650 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
1651#if CONFIG_VAR_REFS
1652 int bit5;
1653 if (BWD_AND_ALT2(cm))
1654 bit5 = READ_REF_BIT(single_ref_p6);
1655 else
1656 bit5 = ALTREF2_IS_VALID(cm);
1657#else // !CONFIG_VAR_REFS
1658 const int bit5 = READ_REF_BIT(single_ref_p6);
1659#endif // CONFIG_VAR_REFS
1660 if (counts) ++counts->single_ref[ctx5][5][bit5];
1661 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1662 } else {
1663 ref_frame[0] = ALTREF_FRAME;
1664 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001665 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001666 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001667#if CONFIG_VAR_REFS
1668 int bit2;
1669 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1670 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001671 bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001672 else
1673 bit2 = L3_OR_G(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001674#else // !CONFIG_VAR_REFS
1675 const int bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001676#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001677 if (counts) ++counts->single_ref[ctx2][2][bit2];
1678 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001680#if CONFIG_VAR_REFS
1681 int bit4;
1682 // Test need to explicitly code (L3) vs (G) branch node in tree
1683 if (L3_AND_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001684 bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001685 else
1686 bit4 = GOLDEN_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001687#else // !CONFIG_VAR_REFS
1688 const int bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001689#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001690 if (counts) ++counts->single_ref[ctx4][4][bit4];
1691 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1692 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001693 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001694#if CONFIG_VAR_REFS
1695 int bit3;
1696 // Test need to explicitly code (L) vs (L2) branch node in tree
1697 if (L_AND_L2(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001698 bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001699 else
1700 bit3 = LAST2_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001701#else // !CONFIG_VAR_REFS
1702 const int bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001703#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001704 if (counts) ++counts->single_ref[ctx3][3][bit3];
1705 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1706 }
1707 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001708#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001709 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001710 const int bit0 = READ_REF_BIT(single_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001711 if (counts) ++counts->single_ref[ctx0][0][bit0];
1712
1713 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001714 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001715 const int bit1 = READ_REF_BIT(single_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001716 if (counts) ++counts->single_ref[ctx1][1][bit1];
1717 ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
1718 } else {
1719 ref_frame[0] = LAST_FRAME;
1720 }
1721#endif // CONFIG_EXT_REFS
1722
Emil Keyder01770b32017-01-20 18:03:11 -05001723 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001724 } else {
1725 assert(0 && "Invalid prediction mode.");
1726 }
1727 }
1728}
1729
Angie Chiang9c4f8952016-11-21 11:13:19 -08001730static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1731 MACROBLOCKD *const xd,
1732 MB_MODE_INFO *const mbmi,
1733 aom_reader *r) {
1734 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001735 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001736
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001737 if (!av1_is_interp_needed(xd)) {
1738 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001739 return;
1740 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001741
Yaowu Xuc27fc142016-08-22 16:08:15 -07001742 if (cm->interp_filter != SWITCHABLE) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001743 mbmi->interp_filters = av1_broadcast_interp_filter(cm->interp_filter);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744 } else {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001745#if CONFIG_DUAL_FILTER
1746 InterpFilter ref0_filter[2] = { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR };
1747 for (int dir = 0; dir < 2; ++dir) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001748 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1749 (mbmi->ref_frame[1] > INTRA_FRAME &&
1750 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001751 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1752 ref0_filter[dir] =
Thomas Daviesec92c112017-09-25 11:03:58 +01001753 (InterpFilter)aom_read_symbol(r, ec_ctx->switchable_interp_cdf[ctx],
1754 SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001755 if (counts) ++counts->switchable_interp[ctx][ref0_filter[dir]];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001756 }
1757 }
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001758 // The index system works as: (0, 1) -> (vertical, horizontal) filter types
1759 mbmi->interp_filters =
1760 av1_make_interp_filters(ref0_filter[0], ref0_filter[1]);
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001761#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001762 const int ctx = av1_get_pred_context_switchable_interp(xd);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001763 InterpFilter filter = (InterpFilter)aom_read_symbol(
Thomas Daviesec92c112017-09-25 11:03:58 +01001764 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001765 mbmi->interp_filters = av1_broadcast_interp_filter(filter);
1766 if (counts) ++counts->switchable_interp[ctx][filter];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001767#endif // CONFIG_DUAL_FILTER
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001768 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001769}
1770
Jingning Han36fe3202017-02-20 22:31:49 -08001771static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1772 const int mi_col, MACROBLOCKD *const xd,
1773 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774 MB_MODE_INFO *const mbmi = &mi->mbmi;
1775 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1776 int i;
1777
1778 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001779 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001780
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001781 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001782
Jingning Han52261842016-12-14 12:17:49 -08001783#if CONFIG_CB4X4
1784 (void)i;
Hui Suaa2965e2017-10-05 15:59:12 -07001785 mbmi->mode = read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
Jingning Han52261842016-12-14 12:17:49 -08001786#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001787 switch (bsize) {
1788 case BLOCK_4X4:
1789 for (i = 0; i < 4; ++i)
Hui Suaa2965e2017-10-05 15:59:12 -07001790 mi->bmi[i].as_mode = read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001791 mbmi->mode = mi->bmi[3].as_mode;
1792 break;
1793 case BLOCK_4X8:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001794 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001795 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001796 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001797 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001798 break;
1799 case BLOCK_8X4:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001800 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001801 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001802 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001803 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001804 break;
1805 default:
Hui Suaa2965e2017-10-05 15:59:12 -07001806 mbmi->mode =
1807 read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001808 }
Jingning Han52261842016-12-14 12:17:49 -08001809#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001810
Jingning Han36fe3202017-02-20 22:31:49 -08001811#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001812 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001813 xd->plane[1].subsampling_y)) {
Hui Suaa2965e2017-10-05 15:59:12 -07001814 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001815#else
Hui Suaa2965e2017-10-05 15:59:12 -07001816 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001817 (void)mi_row;
1818 (void)mi_col;
1819#endif
1820
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001821#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001822 if (mbmi->uv_mode == UV_CFL_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001823 mbmi->cfl_alpha_idx =
David Michael Barrf6eaa152017-07-19 19:42:28 +09001824 read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001825 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001826 } else {
1827 xd->cfl->store_y = 0;
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001828 }
1829#endif // CONFIG_CFL
1830
1831#if CONFIG_CB4X4
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001832 } else {
1833 // Avoid decoding angle_info if there is is no chroma prediction
1834 mbmi->uv_mode = UV_DC_PRED;
1835#if CONFIG_CFL
1836 xd->cfl->is_chroma_reference = 0;
1837 xd->cfl->store_y = 1;
1838#endif
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001839 }
1840#endif
1841
Rupert Swarbrick766c9d52017-07-31 11:30:53 +01001842 // Explicitly ignore cm here to avoid a compile warning if none of
1843 // ext-intra, palette and filter-intra are enabled.
1844 (void)cm;
1845
Yaowu Xuc27fc142016-08-22 16:08:15 -07001846#if CONFIG_EXT_INTRA
1847 read_intra_angle_info(cm, xd, r);
1848#endif // CONFIG_EXT_INTRA
1849 mbmi->palette_mode_info.palette_size[0] = 0;
1850 mbmi->palette_mode_info.palette_size[1] = 0;
1851 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
1852 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001853#if CONFIG_FILTER_INTRA
1854 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1855 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001856 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001857 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001858#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001859}
1860
1861static INLINE int is_mv_valid(const MV *mv) {
1862 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1863 mv->col < MV_UPP;
1864}
1865
Yaowu Xuf883b422016-08-30 14:01:10 -07001866static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001867 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001868 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001869 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001870 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1871 int mi_col, int is_compound, int allow_hp,
1872 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001873 int i;
1874 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001875 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001876 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001877 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001878#if CONFIG_CB4X4
1879 int_mv *pred_mv = mbmi->pred_mv;
1880 (void)block;
1881#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001882 int_mv *pred_mv =
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07001883 (bsize >= BLOCK_8X8) ? mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08001884#endif // CONFIG_CB4X4
Sarah Parkere5299862016-08-16 14:57:37 -07001885 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001886 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001887 (void)mi_row;
1888 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001889 (void)bsize;
RogerZhou3b635242017-09-19 10:06:46 -07001890#if CONFIG_AMVR
1891 if (cm->cur_frame_mv_precision_level) {
1892 allow_hp = MV_SUBPEL_NONE;
1893 }
1894#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001895 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001896 case NEWMV: {
1897 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001898 for (i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001899 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1900 int nmv_ctx =
1901 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1902 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001903 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001904 nmv_context_counts *const mv_counts =
1905 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001906 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001907 ret = ret && is_mv_valid(&mv[i].as_mv);
1908
Yaowu Xuc27fc142016-08-22 16:08:15 -07001909 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001910 }
1911 break;
1912 }
1913 case NEARESTMV: {
1914 mv[0].as_int = nearest_mv[0].as_int;
1915 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1916
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917 pred_mv[0].as_int = nearest_mv[0].as_int;
1918 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001919 break;
1920 }
1921 case NEARMV: {
1922 mv[0].as_int = near_mv[0].as_int;
1923 if (is_compound) mv[1].as_int = near_mv[1].as_int;
1924
Yaowu Xuc27fc142016-08-22 16:08:15 -07001925 pred_mv[0].as_int = near_mv[0].as_int;
1926 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001927 break;
1928 }
1929 case ZEROMV: {
Sarah Parkere5299862016-08-16 14:57:37 -07001930#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +00001931 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001932 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001933 mi_col, mi_row, block
1934#if CONFIG_AMVR
1935 ,
1936 cm->cur_frame_mv_precision_level
1937#endif
1938 )
David Barkercdcac6d2016-12-01 17:04:16 +00001939 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001940 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00001941 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001942 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001943 mi_col, mi_row, block
1944#if CONFIG_AMVR
1945 ,
1946 cm->cur_frame_mv_precision_level
1947#endif
1948 )
David Barkercdcac6d2016-12-01 17:04:16 +00001949 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001950#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001951 mv[0].as_int = 0;
1952 if (is_compound) mv[1].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -07001953#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07001954
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001955 pred_mv[0].as_int = mv[0].as_int;
1956 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001957 break;
1958 }
Zoe Liu85b66462017-04-20 14:28:19 -07001959#if CONFIG_COMPOUND_SINGLEREF
1960 case SR_NEAREST_NEARMV: {
1961 assert(!is_compound);
1962 mv[0].as_int = nearest_mv[0].as_int;
1963 mv[1].as_int = near_mv[0].as_int;
1964 break;
1965 }
1966 /*
1967 case SR_NEAREST_NEWMV: {
1968 assert(!is_compound);
1969 mv[0].as_int = nearest_mv[0].as_int;
1970
1971 FRAME_COUNTS *counts = xd->counts;
1972 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1973 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1974 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1975 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1976 nmv_context_counts *const mv_counts =
1977 counts ? &counts->mv[nmv_ctx] : NULL;
1978 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1979 ret = ret && is_mv_valid(&mv[1].as_mv);
1980 break;
1981 }*/
1982 case SR_NEAR_NEWMV: {
1983 assert(!is_compound);
1984 mv[0].as_int = near_mv[0].as_int;
1985
1986 FRAME_COUNTS *counts = xd->counts;
1987 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1988 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1989 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1990 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1991 nmv_context_counts *const mv_counts =
1992 counts ? &counts->mv[nmv_ctx] : NULL;
1993 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1994 ret = ret && is_mv_valid(&mv[1].as_mv);
1995 break;
1996 }
1997 case SR_ZERO_NEWMV: {
1998 assert(!is_compound);
1999#if CONFIG_GLOBAL_MOTION
2000 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
2001 cm->allow_high_precision_mv, bsize,
2002 mi_col, mi_row, block)
2003 .as_int;
2004#else
2005 mv[0].as_int = 0;
2006#endif // CONFIG_GLOBAL_MOTION
2007
2008 FRAME_COUNTS *counts = xd->counts;
2009 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2010 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2011 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2012 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2013 nmv_context_counts *const mv_counts =
2014 counts ? &counts->mv[nmv_ctx] : NULL;
2015 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2016 ret = ret && is_mv_valid(&mv[1].as_mv);
2017 break;
2018 }
2019 case SR_NEW_NEWMV: {
2020 assert(!is_compound);
2021
2022 FRAME_COUNTS *counts = xd->counts;
2023 for (i = 0; i < 2; ++i) {
2024 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2025 int nmv_ctx =
2026 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
2027 mbmi->ref_mv_idx);
2028 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2029 nmv_context_counts *const mv_counts =
2030 counts ? &counts->mv[nmv_ctx] : NULL;
2031 read_mv(r, &mv[i].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2032 ret = ret && is_mv_valid(&mv[i].as_mv);
2033 }
2034 break;
2035 }
2036#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002037 case NEW_NEWMV: {
2038 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002039 assert(is_compound);
2040 for (i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002041 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2042 int nmv_ctx =
2043 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
2044 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002045 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002046 nmv_context_counts *const mv_counts =
2047 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002048 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002049 ret = ret && is_mv_valid(&mv[i].as_mv);
2050 }
2051 break;
2052 }
2053 case NEAREST_NEARESTMV: {
2054 assert(is_compound);
2055 mv[0].as_int = nearest_mv[0].as_int;
2056 mv[1].as_int = nearest_mv[1].as_int;
2057 break;
2058 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002059 case NEAR_NEARMV: {
2060 assert(is_compound);
2061 mv[0].as_int = near_mv[0].as_int;
2062 mv[1].as_int = near_mv[1].as_int;
2063 break;
2064 }
2065 case NEW_NEARESTMV: {
2066 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002067 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2068 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2069 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002070 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002071 nmv_context_counts *const mv_counts =
2072 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002073 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002074 assert(is_compound);
2075 ret = ret && is_mv_valid(&mv[0].as_mv);
2076 mv[1].as_int = nearest_mv[1].as_int;
2077 break;
2078 }
2079 case NEAREST_NEWMV: {
2080 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002081 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2082 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2083 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002084 nmv_context_counts *const mv_counts =
2085 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002086 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07002087 mv[0].as_int = nearest_mv[0].as_int;
2088 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002089 assert(is_compound);
2090 ret = ret && is_mv_valid(&mv[1].as_mv);
2091 break;
2092 }
2093 case NEAR_NEWMV: {
2094 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002095 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2096 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2097 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002098 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002099 nmv_context_counts *const mv_counts =
2100 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002101 mv[0].as_int = near_mv[0].as_int;
2102 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002103 assert(is_compound);
2104
2105 ret = ret && is_mv_valid(&mv[1].as_mv);
2106 break;
2107 }
2108 case NEW_NEARMV: {
2109 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002110 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2111 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2112 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002113 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002114 nmv_context_counts *const mv_counts =
2115 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002116 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002117 assert(is_compound);
2118 ret = ret && is_mv_valid(&mv[0].as_mv);
2119 mv[1].as_int = near_mv[1].as_int;
2120 break;
2121 }
2122 case ZERO_ZEROMV: {
2123 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08002124#if CONFIG_GLOBAL_MOTION
2125 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002126 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002127 mi_col, mi_row, block
2128#if CONFIG_AMVR
2129 ,
2130 cm->cur_frame_mv_precision_level
2131#endif
2132 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002133 .as_int;
2134 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002135 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002136 mi_col, mi_row, block
2137#if CONFIG_AMVR
2138 ,
2139 cm->cur_frame_mv_precision_level
2140#endif
2141 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002142 .as_int;
2143#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002144 mv[0].as_int = 0;
2145 mv[1].as_int = 0;
Sarah Parkerc2d38712017-01-24 15:15:41 -08002146#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002147 break;
2148 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002149 default: { return 0; }
2150 }
2151 return ret;
2152}
2153
Yaowu Xuf883b422016-08-30 14:01:10 -07002154static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
2155 int segment_id, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002156 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
2157 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
2158 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002159 const int ctx = av1_get_intra_inter_context(xd);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002160#if CONFIG_NEW_MULTISYMBOL
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002161 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002162 const int is_inter =
2163 aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
2164#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002165 const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002166#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002167 FRAME_COUNTS *counts = xd->counts;
2168 if (counts) ++counts->intra_inter[ctx][is_inter];
2169 return is_inter;
2170 }
2171}
2172
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002173#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002174static int read_is_inter_singleref_comp_mode(AV1_COMMON *const cm,
2175 MACROBLOCKD *const xd,
2176 int segment_id, aom_reader *r) {
2177 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) return 0;
2178
2179 const int ctx = av1_get_inter_mode_context(xd);
2180 const int is_singleref_comp_mode =
2181 aom_read(r, cm->fc->comp_inter_mode_prob[ctx], ACCT_STR);
2182 FRAME_COUNTS *counts = xd->counts;
2183
2184 if (counts) ++counts->comp_inter_mode[ctx][is_singleref_comp_mode];
2185 return is_singleref_comp_mode;
2186}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002187#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002188
Yaowu Xuc27fc142016-08-22 16:08:15 -07002189static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002190 AV1Decoder *const pbi = (AV1Decoder *)data;
2191 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
2192 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002193}
2194
Di Chen56586622017-06-09 13:49:44 -07002195#if DEC_MISMATCH_DEBUG
Zoe Liu0b3ef732017-10-06 18:37:24 -07002196static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi, int mi_row,
2197 int mi_col,
Zoe Liuf9333f52017-07-03 10:52:01 -07002198 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES],
2199 int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07002200 int_mv mv[2] = { { 0 } };
2201 int ref;
2202 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07002203 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
2204 mv[ref].as_mv = mbmi->mv[ref].as_mv;
2205
Di Chen56586622017-06-09 13:49:44 -07002206 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
2207 int16_t zeromv_ctx = -1;
2208 int16_t refmv_ctx = -1;
2209 if (mbmi->mode != NEWMV) {
2210 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == ZEROMV);
2211 zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
2212 if (mbmi->mode != ZEROMV) {
2213 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
2214 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
2215 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
2216 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
2217 }
2218 }
2219
2220 int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Zoe Liuf9333f52017-07-03 10:52:01 -07002221#define FRAME_TO_CHECK 1
Zoe Liu17af2742017-10-06 10:36:42 -07002222 if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) {
Zoe Liuf9333f52017-07-03 10:52:01 -07002223 printf(
2224 "=== DECODER ===: "
2225 "Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
2226 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
2227 "ref[1]=%d, motion_mode=%d, inter_mode_ctx=%d, mode_ctx=%d, "
Zoe Liu0b3ef732017-10-06 18:37:24 -07002228 "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d\n",
Zoe Liuf9333f52017-07-03 10:52:01 -07002229 cm->current_video_frame, mi_row, mi_col, mbmi->mode, mbmi->sb_type,
2230 cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
2231 mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
Zoe Liu0b3ef732017-10-06 18:37:24 -07002232 mbmi->motion_mode, inter_mode_ctx[ref_frame_type], mode_ctx, newmv_ctx,
2233 zeromv_ctx, refmv_ctx);
Zoe Liuf9333f52017-07-03 10:52:01 -07002234 }
Di Chen56586622017-06-09 13:49:44 -07002235}
2236#endif // DEC_MISMATCH_DEBUG
2237
Yaowu Xuf883b422016-08-30 14:01:10 -07002238static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239 MACROBLOCKD *const xd,
2240 MODE_INFO *const mi,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002241#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002242 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243 int supertx_enabled) {
2244#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002245 int mi_row, int mi_col, aom_reader *r) {
Yue Chencb60b182016-10-13 15:18:22 -07002246#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248 MB_MODE_INFO *const mbmi = &mi->mbmi;
2249 const BLOCK_SIZE bsize = mbmi->sb_type;
2250 const int allow_hp = cm->allow_high_precision_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08002251 const int unify_bsize = CONFIG_CB4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002252 int_mv nearestmv[2], nearmv[2];
2253 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 int ref, is_compound;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002255#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002256 int is_singleref_comp_mode = 0;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002257#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002258 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002260 int16_t mode_ctx = 0;
Yue Chen69f18e12016-09-08 14:48:15 -07002261#if CONFIG_WARPED_MOTION
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08002262 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang1bc82862017-06-28 15:49:48 -07002263#if WARPED_MOTION_SORT_SAMPLES
2264 int pts_mv[SAMPLES_ARRAY_SIZE];
2265#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002266#endif // CONFIG_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00002267 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002268
Urvang Joshi5a9ea002017-05-22 15:25:18 -07002269 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
2270
Luc Trudeaub05eeae2017-08-18 15:14:30 -04002271 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272 mbmi->palette_mode_info.palette_size[0] = 0;
2273 mbmi->palette_mode_info.palette_size[1] = 0;
2274
Frederic Barbier7a84fd82017-03-02 18:08:15 +01002275 memset(ref_mvs, 0, sizeof(ref_mvs));
2276
Yaowu Xuc27fc142016-08-22 16:08:15 -07002277 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
2278 is_compound = has_second_ref(mbmi);
2279
Zoe Liuc082bbc2017-05-17 13:31:37 -07002280#if CONFIG_EXT_COMP_REFS
2281#if !USE_UNI_COMP_REFS
2282 // NOTE: uni-directional comp refs disabled
2283 if (is_compound)
2284 assert(mbmi->ref_frame[0] < BWDREF_FRAME &&
2285 mbmi->ref_frame[1] >= BWDREF_FRAME);
2286#endif // !USE_UNI_COMP_REFS
2287#endif // CONFIG_EXT_COMP_REFS
2288
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002289#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002290 if (!is_compound)
2291 is_singleref_comp_mode =
2292 read_is_inter_singleref_comp_mode(cm, xd, mbmi->segment_id, r);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002293#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002294
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295 for (ref = 0; ref < 1 + is_compound; ++ref) {
2296 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002297
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002298 av1_find_mv_refs(cm, xd, mi, frame, &xd->ref_mv_count[frame],
2299 xd->ref_mv_stack[frame], compound_inter_mode_ctx,
2300 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2301 inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002302 }
2303
Jingning Hanacddc032016-11-17 15:26:20 -08002304 if (is_compound) {
2305 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002306 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002307 xd->ref_mv_stack[ref_frame], compound_inter_mode_ctx,
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002308 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2309 inter_mode_ctx);
2310
2311 if (xd->ref_mv_count[ref_frame] < 2) {
2312 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00002313 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002314 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00002315#if CONFIG_GLOBAL_MOTION
2316 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00002317 cm->allow_high_precision_mv,
RogerZhou3b635242017-09-19 10:06:46 -07002318 bsize, mi_col, mi_row, 0
2319#if CONFIG_AMVR
2320 ,
2321 cm->cur_frame_mv_precision_level
2322#endif
2323 )
David Barkercdcac6d2016-12-01 17:04:16 +00002324 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002325 zeromv[1].as_int =
2326 (rf[1] != NONE_FRAME)
2327 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
2328 cm->allow_high_precision_mv, bsize, mi_col,
2329 mi_row, 0
2330#if CONFIG_AMVR
2331 ,
2332 cm->cur_frame_mv_precision_level
2333#endif
2334 )
2335 .as_int
2336 : 0;
David Barkercdcac6d2016-12-01 17:04:16 +00002337#else
2338 zeromv[0].as_int = zeromv[1].as_int = 0;
2339#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002340 for (ref = 0; ref < 2; ++ref) {
2341 if (rf[ref] == NONE_FRAME) continue;
RogerZhou3b635242017-09-19 10:06:46 -07002342#if CONFIG_AMVR
2343 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp,
2344 cm->cur_frame_mv_precision_level);
2345 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp,
2346 cm->cur_frame_mv_precision_level);
2347#else
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002348 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
2349 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002350#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002351 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
2352 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
2353 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01002354 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002355 }
2356 }
2357
Zoe Liu85b66462017-04-20 14:28:19 -07002358#if CONFIG_COMPOUND_SINGLEREF
2359 if (is_compound || is_singleref_comp_mode)
2360#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002361 if (is_compound)
Zoe Liu85b66462017-04-20 14:28:19 -07002362#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002363 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
2364 else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07002366 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002367 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002368
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002369#if CONFIG_SEGMENT_ZEROMV
2370 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
2371 segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_ZEROMV)) {
2372#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002374#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002375 mbmi->mode = ZEROMV;
Debargha Mukherjeec76f9dc2017-05-01 13:18:09 -07002376 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002377 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
David Barker3409c0d2017-06-30 17:33:13 +01002378 "Invalid usage of segment feature on small blocks");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379 return;
2380 }
2381 } else {
Jingning Han5cfa6712016-12-14 09:53:38 -08002382 if (bsize >= BLOCK_8X8 || unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383 if (is_compound)
2384 mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002385#if CONFIG_COMPOUND_SINGLEREF
2386 else if (is_singleref_comp_mode)
Thomas Daviesb8b14a92017-07-12 15:11:49 +01002387 mbmi->mode = read_inter_singleref_comp_mode(xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002388#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389 else
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002390 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
David Barker3dfba992017-04-03 16:10:09 +01002391 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002392#if CONFIG_COMPOUND_SINGLEREF
2393 mbmi->mode == SR_NEW_NEWMV ||
2394#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002395 have_nearmv_in_inter_mode(mbmi->mode))
Thomas Davies149eda52017-06-12 18:11:55 +01002396 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397 }
2398 }
2399
David Barker9b75e212017-07-28 15:31:41 +01002400 if ((bsize < BLOCK_8X8 && !unify_bsize) ||
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002401 (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402 for (ref = 0; ref < 1 + is_compound; ++ref) {
RogerZhou3b635242017-09-19 10:06:46 -07002403#if CONFIG_AMVR
2404 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2405 &nearestmv[ref], &nearmv[ref],
2406 cm->cur_frame_mv_precision_level);
2407#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2409 &nearestmv[ref], &nearmv[ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002410#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002411 }
2412 }
2413
Zoe Liu85b66462017-04-20 14:28:19 -07002414#if CONFIG_COMPOUND_SINGLEREF
2415 if ((is_compound || is_singleref_comp_mode) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002416 (bsize >= BLOCK_8X8 || unify_bsize) && mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002417#else // !CONFIG_COMPOUND_SINGLEREF
Jingning Han61418bb2017-01-23 17:12:48 -08002418 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002419 mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002420#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -07002421 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002422 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002423
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002424 if (xd->ref_mv_count[ref_frame_type] > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425 if (mbmi->mode == NEAREST_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002426 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2427 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002428#if CONFIG_AMVR
2429 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
2430 cm->cur_frame_mv_precision_level);
2431 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
2432 cm->cur_frame_mv_precision_level);
2433#else
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002434 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
2435 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002436#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002437 } else if (mbmi->mode == NEAREST_NEWMV
2438#if CONFIG_COMPOUND_SINGLEREF
2439 || mbmi->mode == SR_NEAREST_NEARMV
2440// || mbmi->mode == SR_NEAREST_NEWMV
2441#endif // CONFIG_COMPOUND_SINGLEREF
2442 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002444
2445#if CONFIG_AMVR
2446 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
2447 cm->cur_frame_mv_precision_level);
2448#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002450#endif
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07002451 } else if (mbmi->mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002453#if CONFIG_AMVR
2454 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
2455 cm->cur_frame_mv_precision_level);
2456#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002458#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460 }
2461
Yaowu Xuc27fc142016-08-22 16:08:15 -07002462 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01002463 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
Zoe Liu85b66462017-04-20 14:28:19 -07002464#if CONFIG_COMPOUND_SINGLEREF
2465 if (is_compound) {
2466#endif // CONFIG_COMPOUND_SINGLEREF
2467 if (compound_ref0_mode(mbmi->mode) == NEARMV) {
2468 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002469#if CONFIG_AMVR
2470 lower_mv_precision(&nearmv[0].as_mv, allow_hp,
2471 cm->cur_frame_mv_precision_level);
2472#else
2473 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2474#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002475 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002476
Zoe Liu85b66462017-04-20 14:28:19 -07002477 if (compound_ref1_mode(mbmi->mode) == NEARMV) {
2478 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002479#if CONFIG_AMVR
2480 lower_mv_precision(&nearmv[1].as_mv, allow_hp,
2481 cm->cur_frame_mv_precision_level);
2482#else
2483 lower_mv_precision(&nearmv[1].as_mv, allow_hp);
2484#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002485 }
2486#if CONFIG_COMPOUND_SINGLEREF
2487 } else {
2488 assert(is_singleref_comp_mode);
2489 if (compound_ref0_mode(mbmi->mode) == NEARMV ||
2490 compound_ref1_mode(mbmi->mode) == NEARMV) {
2491 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2492 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2493 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002494 }
Zoe Liu85b66462017-04-20 14:28:19 -07002495#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496 }
David Barker0d7c4b02017-07-25 14:55:48 +01002497 } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
2498 int_mv cur_mv =
2499 xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
2500 nearmv[0] = cur_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002501 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002502
Yue Chen19e7aa82016-11-30 14:05:39 -08002503#if !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION && !CONFIG_GLOBAL_MOTION
Angie Chiang9c4f8952016-11-21 11:13:19 -08002504 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002505#endif // !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002506
Jingning Han5cfa6712016-12-14 09:53:38 -08002507 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002508 const int num_4x4_w = 1 << xd->bmode_blocks_wl;
2509 const int num_4x4_h = 1 << xd->bmode_blocks_hl;
2510 int idx, idy;
2511 PREDICTION_MODE b_mode;
2512 int_mv nearest_sub8x8[2], near_sub8x8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002513 int_mv ref_mv[2][2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002514 for (idy = 0; idy < 2; idy += num_4x4_h) {
2515 for (idx = 0; idx < 2; idx += num_4x4_w) {
2516 int_mv block[2];
2517 const int j = idy * 2 + idx;
2518 int_mv ref_mv_s8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002519 if (!is_compound)
Yaowu Xuf883b422016-08-30 14:01:10 -07002520 mode_ctx = av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame,
2521 bsize, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 if (is_compound)
2523 b_mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
2524 else
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002525 b_mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002526
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002527 if (b_mode != ZEROMV && b_mode != ZERO_ZEROMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002528 CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
2529 uint8_t ref_mv_count[2];
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002530 for (ref = 0; ref < 1 + is_compound; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002531 int_mv mv_ref_list[MAX_MV_REF_CANDIDATES];
Yaowu Xu531d6af2017-03-07 17:48:52 -08002532 av1_update_mv_context(cm, xd, mi, mbmi->ref_frame[ref], mv_ref_list,
2533 j, mi_row, mi_col, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07002534 av1_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
Yaowu Xuf883b422016-08-30 14:01:10 -07002535 ref_mv_stack[ref], &ref_mv_count[ref],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002536 mv_ref_list, &nearest_sub8x8[ref],
Yaowu Xuf883b422016-08-30 14:01:10 -07002537 &near_sub8x8[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002538 if (have_newmv_in_inter_mode(b_mode)) {
2539 mv_ref_list[0].as_int = nearest_sub8x8[ref].as_int;
2540 mv_ref_list[1].as_int = near_sub8x8[ref].as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002541#if CONFIG_AMVR
2542 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2543 &ref_mv[1][ref],
2544 cm->cur_frame_mv_precision_level);
2545#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002546 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2547 &ref_mv[1][ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002548#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549 }
2550 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551 }
2552
2553 for (ref = 0; ref < 1 + is_compound && b_mode != ZEROMV; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002554 ref_mv_s8[ref] = nearest_sub8x8[ref];
RogerZhou3b635242017-09-19 10:06:46 -07002555#if CONFIG_AMVR
2556 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp,
2557 cm->cur_frame_mv_precision_level);
2558#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002560#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002561 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002562 (void)ref_mv_s8;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002564 if (!assign_mv(cm, xd, b_mode, mbmi->ref_frame, j, block, ref_mv[0],
David Barker45390c12017-02-20 14:44:40 +00002565 nearest_sub8x8, near_sub8x8, mi_row, mi_col, is_compound,
2566 allow_hp, r)) {
Angie Chiangd0916d92017-03-10 17:54:18 -08002567 aom_merge_corrupted_flag(&xd->corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002568 break;
2569 };
2570
2571 mi->bmi[j].as_mv[0].as_int = block[0].as_int;
Sarah Parkerd7fa8542016-10-11 11:51:59 -07002572 mi->bmi[j].as_mode = b_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573 if (is_compound) mi->bmi[j].as_mv[1].as_int = block[1].as_int;
2574
2575 if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
2576 if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
2577 }
2578 }
2579
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07002580 mbmi->pred_mv[0].as_int = mi->bmi[3].pred_mv[0].as_int;
2581 mbmi->pred_mv[1].as_int = mi->bmi[3].pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582 mi->mbmi.mode = b_mode;
2583
2584 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2585 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2586 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002587 int_mv ref_mv[2];
2588 ref_mv[0] = nearestmv[0];
2589 ref_mv[1] = nearestmv[1];
2590
David Barker3dfba992017-04-03 16:10:09 +01002591 if (is_compound) {
David Barker3dfba992017-04-03 16:10:09 +01002592 int ref_mv_idx = mbmi->ref_mv_idx;
2593 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2594 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2595 // mbmi->ref_mv_idx (like NEWMV)
2596 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2597 ref_mv_idx = 1 + mbmi->ref_mv_idx;
David Barker3dfba992017-04-03 16:10:09 +01002598
2599 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
David Barker404b2e82017-03-27 13:07:47 +01002600 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2601 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker3dfba992017-04-03 16:10:09 +01002602 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002603 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker404b2e82017-03-27 13:07:47 +01002604 xd->n8_h << MI_SIZE_LOG2, xd);
2605 }
David Barker3dfba992017-04-03 16:10:09 +01002606 nearestmv[0] = ref_mv[0];
David Barker404b2e82017-03-27 13:07:47 +01002607 }
David Barker3dfba992017-04-03 16:10:09 +01002608 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
David Barker3dfba992017-04-03 16:10:09 +01002609 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2610 if (xd->ref_mv_count[ref_frame_type] > 1) {
2611 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002612 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker3dfba992017-04-03 16:10:09 +01002613 xd->n8_h << MI_SIZE_LOG2, xd);
2614 }
David Barker3dfba992017-04-03 16:10:09 +01002615 nearestmv[1] = ref_mv[1];
2616 }
Zoe Liu85b66462017-04-20 14:28:19 -07002617#if CONFIG_COMPOUND_SINGLEREF
2618 } else if (is_singleref_comp_mode) {
2619 int ref_mv_idx = mbmi->ref_mv_idx;
2620 // Special case: SR_NEAR_NEWMV use 1 + mbmi->ref_mv_idx (like NEARMV)
2621 // instead of mbmi->ref_mv_idx (like NEWMV)
2622 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
2623
2624 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
2625 compound_ref1_mode(mbmi->mode) == NEWMV) {
2626 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2627 if (xd->ref_mv_count[ref_frame_type] > 1) {
2628 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2629 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2630 xd->n8_h << MI_SIZE_LOG2, xd);
2631 }
2632 // TODO(zoeliu): To further investigate why this would not cause a
2633 // mismatch for the mode of SR_NEAREST_NEWMV.
2634 nearestmv[0] = ref_mv[0];
2635 }
2636#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002637 } else {
David Barker3dfba992017-04-03 16:10:09 +01002638 if (mbmi->mode == NEWMV) {
2639 for (ref = 0; ref < 1 + is_compound; ++ref) {
David Barker3dfba992017-04-03 16:10:09 +01002640 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2641 if (xd->ref_mv_count[ref_frame_type] > 1) {
2642 ref_mv[ref] =
2643 (ref == 0)
2644 ? xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv
2645 : xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
2646 .comp_mv;
2647 clamp_mv_ref(&ref_mv[ref].as_mv, xd->n8_w << MI_SIZE_LOG2,
2648 xd->n8_h << MI_SIZE_LOG2, xd);
2649 }
David Barker3dfba992017-04-03 16:10:09 +01002650 nearestmv[ref] = ref_mv[ref];
2651 }
2652 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 }
2654
Angie Chiangd0916d92017-03-10 17:54:18 -08002655 int mv_corrupted_flag =
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002656 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
David Barker45390c12017-02-20 14:44:40 +00002657 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
Angie Chiangd0916d92017-03-10 17:54:18 -08002658 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 }
2660
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002661#if CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 mbmi->use_wedge_interintra = 0;
2663 if (cm->reference_mode != COMPOUND_REFERENCE &&
2664#if CONFIG_SUPERTX
2665 !supertx_enabled &&
2666#endif
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002667 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002669#if CONFIG_NEW_MULTISYMBOL
2670 const int interintra =
2671 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
2672#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002673 const int interintra =
2674 aom_read(r, cm->fc->interintra_prob[bsize_group], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002675#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002677 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678 if (interintra) {
2679 const INTERINTRA_MODE interintra_mode =
2680 read_interintra_mode(cm, xd, r, bsize_group);
2681 mbmi->ref_frame[1] = INTRA_FRAME;
2682 mbmi->interintra_mode = interintra_mode;
2683#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684 mbmi->angle_delta[0] = 0;
2685 mbmi->angle_delta[1] = 0;
hui sueda3d762016-12-06 16:58:23 -08002686#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687 mbmi->intra_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -08002688#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002690#if CONFIG_FILTER_INTRA
2691 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2692 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2693#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002694 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002695#if CONFIG_NEW_MULTISYMBOL
2696 mbmi->use_wedge_interintra = aom_read_symbol(
2697 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
2698#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002699 mbmi->use_wedge_interintra =
Michael Bebenita6048d052016-08-25 14:40:54 -07002700 aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002701#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002702 if (xd->counts)
2703 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2704 if (mbmi->use_wedge_interintra) {
2705 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002706 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002707 mbmi->interintra_wedge_sign = 0;
2708 }
2709 }
2710 }
2711 }
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002712#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002713
Yue Chen52c51732017-07-11 15:08:30 -07002714#if CONFIG_WARPED_MOTION
2715 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
2716 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2717 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2718
2719 xd->block_refs[ref] = ref_buf;
2720 }
2721#endif
2722
Yue Chencb60b182016-10-13 15:18:22 -07002723#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
2724 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yue Chen69f18e12016-09-08 14:48:15 -07002725#if CONFIG_WARPED_MOTION
2726 if (mbmi->sb_type >= BLOCK_8X8 && !has_second_ref(mbmi))
Yunqing Wang1bc82862017-06-28 15:49:48 -07002727#if WARPED_MOTION_SORT_SAMPLES
2728 mbmi->num_proj_ref[0] =
2729 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv);
2730#else
Yue Chen69f18e12016-09-08 14:48:15 -07002731 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang1bc82862017-06-28 15:49:48 -07002732#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002733#endif // CONFIG_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002734#if CONFIG_MOTION_VAR
2735 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
2736#endif
Yue Chen69f18e12016-09-08 14:48:15 -07002737
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738#if CONFIG_SUPERTX
Yue Chen69f18e12016-09-08 14:48:15 -07002739 if (!supertx_enabled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002740#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002741 if (mbmi->ref_frame[1] != INTRA_FRAME)
Sarah Parker19234cc2017-03-10 16:43:25 -08002742 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002743
2744#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -07002745 read_ncobmc_mode(xd, mi, mbmi->ncobmc_mode, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002746#endif
2747
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002748#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002749 if (is_singleref_comp_mode) assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002750#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chen69f18e12016-09-08 14:48:15 -07002751#if CONFIG_WARPED_MOTION
2752 if (mbmi->motion_mode == WARPED_CAUSAL) {
2753 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
Yunqing Wang1bc82862017-06-28 15:49:48 -07002754
2755#if WARPED_MOTION_SORT_SAMPLES
2756 if (mbmi->num_proj_ref[0] > 1)
2757 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2758 pts_inref, mbmi->num_proj_ref[0]);
2759#endif // WARPED_MOTION_SORT_SAMPLES
2760
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002761 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2762 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2763 &mbmi->wm_params[0], mi_row, mi_col)) {
Yunqing Wang8657ad72017-06-20 14:46:08 -07002764 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "Invalid Warped Model");
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002765 }
Yue Chen69f18e12016-09-08 14:48:15 -07002766 }
2767#endif // CONFIG_WARPED_MOTION
2768#if CONFIG_SUPERTX
2769 }
2770#endif // CONFIG_SUPERTX
Yue Chencb60b182016-10-13 15:18:22 -07002771#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002772
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002773 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Zoe Liu85b66462017-04-20 14:28:19 -07002774 if (
2775#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu0c634c72017-06-19 07:06:28 -07002776 is_inter_anyref_comp_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002777#else // !CONFIG_COMPOUND_SINGLEREF
2778 cm->reference_mode != SINGLE_REFERENCE &&
Sarah Parker6fdc8532016-11-16 17:47:13 -08002779 is_inter_compound_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002780#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chencb60b182016-10-13 15:18:22 -07002781#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002782 && mbmi->motion_mode == SIMPLE_TRANSLATION
Yue Chencb60b182016-10-13 15:18:22 -07002783#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002784 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002785 if (is_any_masked_compound_used(bsize)) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002786#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002787 if (cm->allow_masked_compound) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002788#if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2789 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
2790 mbmi->interinter_compound_type =
2791 aom_read_bit(r, ACCT_STR) ? COMPOUND_AVERAGE : COMPOUND_SEG;
2792 else
2793#endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2794 mbmi->interinter_compound_type = aom_read_symbol(
2795 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002796#if CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002797 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002798 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002799 mbmi->wedge_index =
2800 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2801 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2802 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002803#endif // CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002804#if CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002805 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
2806 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
2807 }
Sarah Parker42d96102017-01-31 21:05:27 -08002808#endif // CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002809 }
2810#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002811 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002812 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002813 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002815 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002816 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002817
Yue Chen19e7aa82016-11-30 14:05:39 -08002818#if CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002819 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002820#endif // CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07002821
Di Chen56586622017-06-09 13:49:44 -07002822#if DEC_MISMATCH_DEBUG
Zoe Liu0b3ef732017-10-06 18:37:24 -07002823 dec_dump_logs(cm, mi, mi_row, mi_col, inter_mode_ctx, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002824#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002825}
2826
Yaowu Xuf883b422016-08-30 14:01:10 -07002827static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002828 MACROBLOCKD *const xd,
2829#if CONFIG_SUPERTX
2830 int supertx_enabled,
2831#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002832 int mi_row, int mi_col, aom_reader *r) {
2833 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002834 MODE_INFO *const mi = xd->mi[0];
2835 MB_MODE_INFO *const mbmi = &mi->mbmi;
2836 int inter_block = 1;
2837#if CONFIG_VAR_TX
2838 BLOCK_SIZE bsize = mbmi->sb_type;
2839#endif // CONFIG_VAR_TX
2840
2841 mbmi->mv[0].as_int = 0;
2842 mbmi->mv[1].as_int = 0;
2843 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
2844#if CONFIG_SUPERTX
David Barker3aec8d62017-01-31 14:55:32 +00002845 if (!supertx_enabled)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002846#endif // CONFIG_SUPERTX
2847 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
David Barker3aec8d62017-01-31 14:55:32 +00002848
David Barker3aec8d62017-01-31 14:55:32 +00002849 if (cm->delta_q_present_flag) {
2850 xd->current_qindex =
2851 xd->prev_qindex +
2852 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01002853 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2854 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
David Barker3aec8d62017-01-31 14:55:32 +00002855 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07002856#if CONFIG_EXT_DELTA_Q
2857 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07002858#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07002859 if (cm->delta_lf_multi) {
2860 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
2861 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
2862 xd->prev_delta_lf[lf_id] +
2863 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
2864 cm->delta_lf_res;
2865 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
2866 }
2867 } else {
2868 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2869 xd->prev_delta_lf_from_base +
2870 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07002871 cm->delta_lf_res;
Cheng Chen880166a2017-10-02 17:48:48 -07002872 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07002873 }
2874#else
Cheng Chen9dccdf22017-10-02 15:04:40 -07002875 const int current_delta_lf_from_base =
Fangwen Fu231fe422017-04-24 17:52:29 -07002876 xd->prev_delta_lf_from_base +
2877 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
2878 cm->delta_lf_res;
Cheng Chen9dccdf22017-10-02 15:04:40 -07002879 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2880 clamp(current_delta_lf_from_base, 0, MAX_LOOP_FILTER);
Fangwen Fu231fe422017-04-24 17:52:29 -07002881 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07002882#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07002883 }
2884#endif
David Barker3aec8d62017-01-31 14:55:32 +00002885 }
David Barker3aec8d62017-01-31 14:55:32 +00002886
2887#if CONFIG_SUPERTX
2888 if (!supertx_enabled) {
2889#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
2891
2892#if CONFIG_VAR_TX
Jingning Han331662e2017-05-30 17:03:32 -07002893 xd->above_txfm_context =
2894 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2895 xd->left_txfm_context = xd->left_txfm_context_buffer +
2896 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002897
2898 if (cm->tx_mode == TX_MODE_SELECT &&
2899#if CONFIG_CB4X4
Jingning Han3daa4fd2017-01-20 10:33:50 -08002900 bsize > BLOCK_4X4 &&
Jingning Han581d1692017-01-05 16:03:54 -08002901#else
2902 bsize >= BLOCK_8X8 &&
2903#endif
David Barker16c64e32017-08-23 16:54:59 +01002904 !mbmi->skip && inter_block && !xd->lossless[mbmi->segment_id]) {
Jingning Han70e5f3f2016-11-09 17:03:07 -08002905 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07002906 const int bh = tx_size_high_unit[max_tx_size];
2907 const int bw = tx_size_wide_unit[max_tx_size];
Jingning Han65abc312016-10-27 13:04:21 -07002908 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2909 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002910 int idx, idy;
Sarah Parkerd25ef8c2017-10-06 12:17:30 -07002911 int init_depth =
2912 (height != width) ? RECT_VARTX_DEPTH_INIT : SQR_VARTX_DEPTH_INIT;
Yue Chena1e48dc2016-08-29 17:29:33 -07002913
Jingning Hanfe45b212016-11-22 10:30:23 -08002914 mbmi->min_tx_size = TX_SIZES_ALL;
2915 for (idy = 0; idy < height; idy += bh)
2916 for (idx = 0; idx < width; idx += bw)
Sarah Parkerd25ef8c2017-10-06 12:17:30 -07002917 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size, init_depth,
2918 idy, idx, r);
Yue Chend6bdd462017-07-19 16:05:43 -07002919#if CONFIG_RECT_TX_EXT
2920 if (is_quarter_tx_allowed(xd, mbmi, inter_block) &&
2921 mbmi->tx_size == max_tx_size) {
2922 int quarter_tx;
2923
2924 if (quarter_txsize_lookup[bsize] != max_tx_size) {
Thomas Daviese3f69782017-10-03 10:43:17 +01002925#if CONFIG_NEW_MULTISYMBOL
2926 quarter_tx =
2927 aom_read_symbol(r, cm->fc->quarter_tx_size_cdf, 2, ACCT_STR);
2928#else
Yue Chend6bdd462017-07-19 16:05:43 -07002929 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
2930 if (xd->counts) ++xd->counts->quarter_tx_size[quarter_tx];
Thomas Daviese3f69782017-10-03 10:43:17 +01002931#endif
Yue Chend6bdd462017-07-19 16:05:43 -07002932 } else {
2933 quarter_tx = 1;
2934 }
2935 if (quarter_tx) {
2936 mbmi->tx_size = quarter_txsize_lookup[bsize];
2937 for (idy = 0; idy < tx_size_high_unit[max_tx_size] / 2; ++idy)
2938 for (idx = 0; idx < tx_size_wide_unit[max_tx_size] / 2; ++idx)
2939 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
2940 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
2941 }
2942 }
2943#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002944 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -08002945 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002946
2947 if (inter_block) {
Jingning Han9ca05b72017-01-03 14:41:36 -08002948 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2949 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950 int idx, idy;
2951 for (idy = 0; idy < height; ++idy)
2952 for (idx = 0; idx < width; ++idx)
2953 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
2954 }
Jingning Hane67b38a2016-11-04 10:30:00 -07002955 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han1b1dc932016-11-09 10:55:30 -08002956 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957 }
2958#else
Urvang Joshifeb925f2016-12-05 10:37:29 -08002959 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960#endif // CONFIG_VAR_TX
2961#if CONFIG_SUPERTX
2962 }
2963#if CONFIG_VAR_TX
2964 else if (inter_block) {
2965 const int width = num_4x4_blocks_wide_lookup[bsize];
2966 const int height = num_4x4_blocks_high_lookup[bsize];
2967 int idx, idy;
2968 xd->mi[0]->mbmi.tx_size = xd->supertx_size;
2969 for (idy = 0; idy < height; ++idy)
2970 for (idx = 0; idx < width; ++idx)
2971 xd->mi[0]->mbmi.inter_tx_size[idy >> 1][idx >> 1] = xd->supertx_size;
2972 }
2973#endif // CONFIG_VAR_TX
2974#endif // CONFIG_SUPERTX
2975
2976 if (inter_block)
2977 read_inter_block_mode_info(pbi, xd,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002978#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002979 mi, mi_row, mi_col, r, supertx_enabled);
2980#else
2981 mi, mi_row, mi_col, r);
Yue Chencb60b182016-10-13 15:18:22 -07002982#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002983 else
Jingning Han36fe3202017-02-20 22:31:49 -08002984 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002985
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002986#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07002987 av1_read_tx_type(cm, xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002988#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07002989 supertx_enabled,
Nathan E. Egge93878c42016-05-03 10:01:32 -04002990#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07002991 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002992#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002993}
2994
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002995static void av1_intra_copy_frame_mvs(AV1_COMMON *const cm, int mi_row,
2996 int mi_col, int x_mis, int y_mis) {
2997#if CONFIG_TMV
2998 const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_cols, 1);
2999 MV_REF *frame_mvs = cm->cur_frame->mvs +
3000 ((mi_row & 0xfffe) >> 1) * frame_mvs_stride +
3001 ((mi_col & 0xfffe) >> 1);
3002 x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
3003 y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
3004#else
3005 const int frame_mvs_stride = cm->mi_cols;
3006 MV_REF *frame_mvs = cm->cur_frame->mvs +
3007 (mi_row & 0xfffe) * frame_mvs_stride + (mi_col & 0xfffe);
3008 x_mis = AOMMAX(x_mis, 2);
3009 y_mis = AOMMAX(y_mis, 2);
3010#endif // CONFIG_TMV
3011 int w, h;
3012
3013 for (h = 0; h < y_mis; h++) {
3014 MV_REF *const frame_mv = frame_mvs + h * frame_mvs_stride;
3015 for (w = 0; w < x_mis; w++) {
3016 MV_REF *const mv = frame_mv + w;
3017 mv->ref_frame[0] = NONE_FRAME;
3018 mv->ref_frame[1] = NONE_FRAME;
3019 }
3020 }
3021}
3022
Yaowu Xuf883b422016-08-30 14:01:10 -07003023void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003024#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07003025 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003026#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07003027 int mi_row, int mi_col, aom_reader *r, int x_mis,
3028 int y_mis) {
3029 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030 MODE_INFO *const mi = xd->mi[0];
Alex Converse28744302017-04-13 14:46:22 -07003031#if CONFIG_INTRABC
3032 mi->mbmi.use_intrabc = 0;
3033#endif // CONFIG_INTRABC
3034
Yaowu Xuc27fc142016-08-22 16:08:15 -07003035 if (frame_is_intra_only(cm)) {
3036 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003037 av1_intra_copy_frame_mvs(cm, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003038 } else {
3039 read_inter_frame_mode_info(pbi, xd,
3040#if CONFIG_SUPERTX
3041 supertx_enabled,
3042#endif // CONFIG_SUPERTX
3043 mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003044 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003045 }
3046}