blob: 0a754fd5b0f8e2885458fad651b03e527ca32941 [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] =
Yue Chen63ce36f2017-10-10 23:37:31 -0700893 aom_read_symbol(r, xd->tile_ctx->filter_intra_mode_cdf[0],
894 FILTER_INTRA_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895 }
hui su5db97432016-10-14 16:10:14 -0700896 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800897 ++counts
898 ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
hui su5db97432016-10-14 16:10:14 -0700899 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900 }
Jingning Han62946d12017-05-26 11:29:30 -0700901
902#if CONFIG_CB4X4
903 if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
904 xd->plane[1].subsampling_x,
905 xd->plane[1].subsampling_y))
906 return;
hui sub4ed1492017-05-31 17:25:42 -0700907#else
908 (void)mi_row;
909 (void)mi_col;
910#endif // CONFIG_CB4X4
Jingning Han62946d12017-05-26 11:29:30 -0700911
Urvang Joshic6300aa2017-06-01 14:46:23 -0700912 if (mbmi->uv_mode == UV_DC_PRED &&
913 mbmi->palette_mode_info.palette_size[1] == 0) {
hui su5db97432016-10-14 16:10:14 -0700914 filter_intra_mode_info->use_filter_intra_mode[1] =
915 aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
916 if (filter_intra_mode_info->use_filter_intra_mode[1]) {
917 filter_intra_mode_info->filter_intra_mode[1] =
Yue Chen63ce36f2017-10-10 23:37:31 -0700918 aom_read_symbol(r, xd->tile_ctx->filter_intra_mode_cdf[1],
919 FILTER_INTRA_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700920 }
hui su5db97432016-10-14 16:10:14 -0700921 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800922 ++counts
923 ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
hui su5db97432016-10-14 16:10:14 -0700924 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925 }
926}
hui su5db97432016-10-14 16:10:14 -0700927#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928
hui su5db97432016-10-14 16:10:14 -0700929#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -0700930static void read_intra_angle_info(MACROBLOCKD *const xd, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
932 const BLOCK_SIZE bsize = mbmi->sb_type;
Joe Young830d4ce2017-05-30 17:48:13 -0700933 mbmi->angle_delta[0] = 0;
934 mbmi->angle_delta[1] = 0;
Joe Young830d4ce2017-05-30 17:48:13 -0700935 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936
hui su45dc5972016-12-08 17:42:50 -0800937 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700939 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940 }
941
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400942 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700943 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700944 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 }
946}
947#endif // CONFIG_EXT_INTRA
948
Angie Chianga9f9a312017-04-13 16:40:43 -0700949void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Hanab7163d2016-11-04 09:46:35 -0700950#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -0700951 int supertx_enabled,
Jingning Hanab7163d2016-11-04 09:46:35 -0700952#endif
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700953#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700954 int blk_row, int blk_col, int block, int plane,
955 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700956#endif
957 aom_reader *r) {
958 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700959 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700960#if !CONFIG_TXK_SEL
Jingning Hane67b38a2016-11-04 10:30:00 -0700961#if CONFIG_VAR_TX
962 const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
963#else
Jingning Hanab7163d2016-11-04 09:46:35 -0700964 const TX_SIZE tx_size = mbmi->tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700965#endif
Jingning Han243b66b2017-06-23 12:11:47 -0700966#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000967 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000968
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700969#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700970 TX_TYPE *tx_type = &mbmi->tx_type;
971#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700972 // only y plane's tx_type is transmitted
973 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700974 (void)block;
975 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << 4) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700976#endif
Lester Lu432012f2017-08-17 14:39:29 -0700977#if CONFIG_LGT_FROM_PRED
978 mbmi->use_lgt = 0;
979#endif
Angie Chianga9f9a312017-04-13 16:40:43 -0700980
Jingning Hanab7163d2016-11-04 09:46:35 -0700981 if (!FIXED_TX_TYPE) {
982#if CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -0800983 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -0800984 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
985 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -0800986 ((!cm->seg.enabled && cm->base_qindex > 0) ||
987 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
988 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -0700989#if CONFIG_SUPERTX
990 !supertx_enabled &&
991#endif // CONFIG_SUPERTX
992 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Suddbcde22017-09-18 17:22:02 -0700993 const TxSetType tx_set_type = get_ext_tx_set_type(
994 tx_size, mbmi->sb_type, inter_block, cm->reduced_tx_set_used);
Sarah Parkere68a3e42017-02-16 14:03:24 -0800995 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
996 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -0700997 // eset == 0 should correspond to a set with only DCT_DCT and
998 // there is no need to read the tx_type
999 assert(eset != 0);
Lester Lu432012f2017-08-17 14:39:29 -07001000
1001#if !CONFIG_LGT_FROM_PRED
Jingning Hanab7163d2016-11-04 09:46:35 -07001002 if (inter_block) {
Hui Suddbcde22017-09-18 17:22:02 -07001003 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -07001004 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
Hui Suddbcde22017-09-18 17:22:02 -07001005 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Jingning Hanab7163d2016-11-04 09:46:35 -07001006 } else if (ALLOW_INTRA_EXT_TX) {
Hui Suddbcde22017-09-18 17:22:02 -07001007 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -07001008 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
Hui Suddbcde22017-09-18 17:22:02 -07001009 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Jingning Hanab7163d2016-11-04 09:46:35 -07001010 }
Lester Lu432012f2017-08-17 14:39:29 -07001011#else
1012 // only signal tx_type when lgt is not allowed or not selected
1013 if (inter_block) {
1014 if (LGT_FROM_PRED_INTER) {
1015 if (is_lgt_allowed(mbmi->mode, tx_size) && !cm->reduced_tx_set_used) {
1016 mbmi->use_lgt =
1017 aom_read(r, ec_ctx->inter_lgt_prob[square_tx_size], ACCT_STR);
1018#if CONFIG_ENTROPY_STATS
1019 if (counts) ++counts->inter_lgt[square_tx_size][mbmi->use_lgt];
1020#endif // CONFIG_ENTROPY_STATS
1021 }
1022 if (!mbmi->use_lgt) {
1023 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1024 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
1025 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1026#if CONFIG_ENTROPY_STATS
1027 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
1028#endif // CONFIG_ENTROPY_STATS
1029 } else {
1030 *tx_type = DCT_DCT; // assign a dummy tx_type
1031 }
1032 } else {
1033 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1034 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
1035 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1036#if CONFIG_ENTROPY_STATS
1037 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
1038#endif // CONFIG_ENTROPY_STATS
1039 }
1040 } else if (ALLOW_INTRA_EXT_TX) {
1041 if (LGT_FROM_PRED_INTRA) {
1042 if (is_lgt_allowed(mbmi->mode, tx_size) && !cm->reduced_tx_set_used) {
1043 mbmi->use_lgt =
1044 aom_read(r, ec_ctx->intra_lgt_prob[square_tx_size][mbmi->mode],
1045 ACCT_STR);
1046#if CONFIG_ENTROPY_STATS
1047 if (counts)
1048 ++counts->intra_lgt[square_tx_size][mbmi->mode][mbmi->use_lgt];
1049#endif // CONFIG_ENTROPY_STATS
1050 }
1051 if (!mbmi->use_lgt) {
1052 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1053 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
1054 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1055#if CONFIG_ENTROPY_STATS
1056 if (counts)
1057 ++counts
1058 ->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
1059#endif // CONFIG_ENTROPY_STATS
1060 } else {
1061 *tx_type = DCT_DCT; // assign a dummy tx_type
1062 }
1063 } else {
1064 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1065 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
1066 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1067#if CONFIG_ENTROPY_STATS
1068 if (counts)
1069 ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
1070#endif // CONFIG_ENTROPY_STATS
1071 }
1072 }
1073#endif // CONFIG_LGT_FROM_PRED
Jingning Hanab7163d2016-11-04 09:46:35 -07001074 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001075 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001076 }
Lester Lu432012f2017-08-17 14:39:29 -07001077#else // CONFIG_EXT_TX
Yue Cheneeacc4c2017-01-17 17:29:17 -08001078
1079 if (tx_size < TX_32X32 &&
1080 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1081 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1082 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001083#if CONFIG_SUPERTX
1084 !supertx_enabled &&
1085#endif // CONFIG_SUPERTX
1086 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Su98b0b3e2017-09-19 13:54:02 -07001087#if CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001088 FRAME_COUNTS *counts = xd->counts;
Hui Su98b0b3e2017-09-19 13:54:02 -07001089#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001090 if (inter_block) {
Angie Chianga9f9a312017-04-13 16:40:43 -07001091 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001092 r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001093#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001094 if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001095#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001096 } else {
1097 const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
Angie Chianga9f9a312017-04-13 16:40:43 -07001098 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001099 r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Hanab7163d2016-11-04 09:46:35 -07001100 ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001101#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001102 if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001103#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001104 }
1105 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001106 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001107 }
1108#endif // CONFIG_EXT_TX
1109 }
Nathan E. Eggeebced372017-02-17 20:57:21 -05001110#if FIXED_TX_TYPE
1111 assert(mbmi->tx_type == DCT_DCT);
1112#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001113}
1114
Alex Converse28744302017-04-13 14:46:22 -07001115#if CONFIG_INTRABC
1116static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
1117 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001118 MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -07001119
1120static INLINE int is_mv_valid(const MV *mv);
1121
1122static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
1123 const int_mv *ref_mv, int mi_row, int mi_col,
1124 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -07001125 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1126 (void)cm;
Alex Converse28744302017-04-13 14:46:22 -07001127 FRAME_COUNTS *counts = xd->counts;
1128 nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse6b2584c2017-05-02 09:51:21 -07001129 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
1130 MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -07001131 int valid = is_mv_valid(&mv->as_mv) &&
1132 is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize);
Alex Converse28744302017-04-13 14:46:22 -07001133 return valid;
1134}
1135#endif // CONFIG_INTRABC
1136
Hui Suc2232cf2017-10-11 17:32:56 -07001137#if CONFIG_INTRABC
1138static void read_intrabc_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1139 int mi_row, int mi_col, aom_reader *r) {
1140 MODE_INFO *const mi = xd->mi[0];
1141 MB_MODE_INFO *const mbmi = &mi->mbmi;
1142 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1143 mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
1144 if (mbmi->use_intrabc) {
1145 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
1146 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
1147 mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
1148
1149 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1150 int_mv ref_mvs[MAX_MV_REF_CANDIDATES];
1151
1152 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
1153 xd->ref_mv_stack[INTRA_FRAME], NULL, ref_mvs, mi_row,
1154 mi_col, NULL, NULL, inter_mode_ctx);
1155
1156 int_mv nearestmv, nearmv;
1157 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1158
1159 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1160 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
1161 const BLOCK_SIZE bsize = mbmi->sb_type;
1162 xd->corrupted |=
1163 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
1164#if CONFIG_VAR_TX
1165 // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks
1166 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1167 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1168 int idx, idy;
1169 for (idy = 0; idy < height; ++idy)
1170 for (idx = 0; idx < width; ++idx)
1171 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1172 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1173#endif // CONFIG_VAR_TX
1174#if CONFIG_EXT_TX && !CONFIG_TXK_SEL
1175 av1_read_tx_type(cm, xd,
1176#if CONFIG_SUPERTX
1177 0,
1178#endif
1179 r);
1180#endif // CONFIG_EXT_TX && !CONFIG_TXK_SEL
1181 }
1182}
1183#endif // CONFIG_INTRABC
1184
Yaowu Xuf883b422016-08-30 14:01:10 -07001185static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001186 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001187 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001188 MODE_INFO *const mi = xd->mi[0];
1189 MB_MODE_INFO *const mbmi = &mi->mbmi;
1190 const MODE_INFO *above_mi = xd->above_mi;
1191 const MODE_INFO *left_mi = xd->left_mi;
1192 const BLOCK_SIZE bsize = mbmi->sb_type;
1193 int i;
1194 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001195 const int bw = mi_size_wide[bsize];
1196 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197
1198 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001199 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1200 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001201 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001202
1203 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1204 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Arild Fuldseth07441162016-08-15 15:07:52 +02001205
Arild Fuldseth07441162016-08-15 15:07:52 +02001206 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001207 xd->current_qindex =
1208 xd->prev_qindex +
1209 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001210 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1211 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001212 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001213#if CONFIG_EXT_DELTA_Q
1214 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07001215#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07001216 if (cm->delta_lf_multi) {
1217 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
1218 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
1219 xd->prev_delta_lf[lf_id] +
1220 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
1221 cm->delta_lf_res;
1222 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
1223 }
1224 } else {
1225 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1226 xd->prev_delta_lf_from_base +
1227 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07001228 cm->delta_lf_res;
Cheng Chen880166a2017-10-02 17:48:48 -07001229 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001230 }
1231#else
Cheng Chen9dccdf22017-10-02 15:04:40 -07001232 const int current_delta_lf_from_base =
Fangwen Fu231fe422017-04-24 17:52:29 -07001233 xd->prev_delta_lf_from_base +
1234 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1235 cm->delta_lf_res;
Cheng Chen9dccdf22017-10-02 15:04:40 -07001236 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1237 clamp(current_delta_lf_from_base, 0, MAX_LOOP_FILTER);
Fangwen Fu231fe422017-04-24 17:52:29 -07001238 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001239#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07001240 }
1241#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001242 }
Arild Fuldseth07441162016-08-15 15:07:52 +02001243
Yaowu Xuc27fc142016-08-22 16:08:15 -07001244 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001245 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001246
Alex Converse28744302017-04-13 14:46:22 -07001247#if CONFIG_INTRABC
RogerZhouca865462017-10-05 15:06:27 -07001248 if (av1_allow_intrabc(bsize, cm)) {
Hui Suc2232cf2017-10-11 17:32:56 -07001249 read_intrabc_info(cm, xd, mi_row, mi_col, r);
1250 if (is_intrabc_block(mbmi)) return;
Alex Converse28744302017-04-13 14:46:22 -07001251 }
1252#endif // CONFIG_INTRABC
1253
Alex Conversef71808c2017-06-06 12:21:17 -07001254 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
1255
Jingning Han52261842016-12-14 12:17:49 -08001256#if CONFIG_CB4X4
1257 (void)i;
1258 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001259 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Jingning Han52261842016-12-14 12:17:49 -08001260#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 switch (bsize) {
1262 case BLOCK_4X4:
1263 for (i = 0; i < 4; ++i)
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001264 mi->bmi[i].as_mode = read_intra_mode(
1265 r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, i));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001266 mbmi->mode = mi->bmi[3].as_mode;
1267 break;
1268 case BLOCK_4X8:
1269 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001270 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001271 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001272 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001273 break;
1274 case BLOCK_8X4:
1275 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001276 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001277 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001278 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 2));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001279 break;
1280 default:
1281 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001282 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001283 }
Jingning Han52261842016-12-14 12:17:49 -08001284#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001285
Jingning Han36fe3202017-02-20 22:31:49 -08001286#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001287 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001288 xd->plane[1].subsampling_y)) {
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001289#if CONFIG_CFL
1290 xd->cfl->is_chroma_reference = 1;
1291#endif // CONFIG_CFL
1292#endif // CONFIG_CB4X4
Hui Suaa2965e2017-10-05 15:59:12 -07001293 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001294
Luc Trudeauf5334002017-04-25 12:21:26 -04001295#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001296 if (mbmi->uv_mode == UV_CFL_PRED) {
David Michael Barrf6eaa152017-07-19 19:42:28 +09001297 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001298 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001299 } else {
1300 xd->cfl->store_y = 0;
Luc Trudeauf5334002017-04-25 12:21:26 -04001301 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001302#endif // CONFIG_CFL
1303
1304#if CONFIG_CB4X4
Joe Young830d4ce2017-05-30 17:48:13 -07001305 } else {
1306 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001307 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001308#if CONFIG_CFL
1309 xd->cfl->is_chroma_reference = 0;
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001310 xd->cfl->store_y = 1;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001311#endif
Luc Trudeauf5334002017-04-25 12:21:26 -04001312 }
1313#endif
1314
Yaowu Xuc27fc142016-08-22 16:08:15 -07001315#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -07001316 read_intra_angle_info(xd, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001317#endif // CONFIG_EXT_INTRA
1318 mbmi->palette_mode_info.palette_size[0] = 0;
1319 mbmi->palette_mode_info.palette_size[1] = 0;
Hui Sue87fb232017-10-05 15:00:15 -07001320 if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001321 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001322#if CONFIG_FILTER_INTRA
1323 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1324 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001325 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001326 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001327#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001328
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001329#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07001330 av1_read_tx_type(cm, xd,
Jingning Hanab7163d2016-11-04 09:46:35 -07001331#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07001332 0,
Nathan E. Egge72762a22016-09-07 17:12:07 -04001333#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07001334 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001335#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001336}
1337
Alex Converse6b2584c2017-05-02 09:51:21 -07001338static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
RogerZhou3b635242017-09-19 10:06:46 -07001339#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001340 int use_subpel,
RogerZhou3b635242017-09-19 10:06:46 -07001341#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001342 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343 int mag, d, fr, hp;
Thomas Davies599395e2017-07-21 18:02:48 +01001344#if CONFIG_NEW_MULTISYMBOL
1345 const int sign = aom_read_bit(r, ACCT_STR);
1346#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001347 const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001348#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07001349 const int mv_class =
Nathan E. Egged7b893c2016-09-08 15:08:48 -04001350 aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001351 const int class0 = mv_class == MV_CLASS_0;
1352
1353 // Integer part
1354 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001355#if CONFIG_NEW_MULTISYMBOL
1356 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
1357#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -04001358 d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001359#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360 mag = 0;
1361 } else {
1362 int i;
1363 const int n = mv_class + CLASS0_BITS - 1; // number of bits
Yaowu Xuc27fc142016-08-22 16:08:15 -07001364 d = 0;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001365#if CONFIG_NEW_MULTISYMBOL
1366 for (i = 0; i < n; ++i)
1367 d |= aom_read_symbol(r, mvcomp->bits_cdf[(i + 1) / 2], 2, ACCT_STR) << i;
1368#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001369 for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001370#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001371 mag = CLASS0_SIZE << (mv_class + 2);
1372 }
1373
RogerZhou3b635242017-09-19 10:06:46 -07001374#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001375 if (use_subpel) {
RogerZhou3b635242017-09-19 10:06:46 -07001376#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001377 // Fractional part
1378 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1379 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001380
Thomas Davies599395e2017-07-21 18:02:48 +01001381// High precision part (if hp is not used, the default value of the hp is 1)
1382#if CONFIG_NEW_MULTISYMBOL
1383 hp = usehp ? aom_read_symbol(
1384 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1385 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001386 : 1;
Thomas Davies599395e2017-07-21 18:02:48 +01001387#else
1388 hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
1389 : 1;
1390#endif
RogerZhou3b635242017-09-19 10:06:46 -07001391#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001392 } else {
1393 fr = 3;
1394 hp = 1;
1395 }
RogerZhou3b635242017-09-19 10:06:46 -07001396#endif // CONFIG_INTRABC || CONFIG_AMVR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001397
1398 // Result
1399 mag += ((d << 3) | (fr << 1) | hp) + 1;
1400 return sign ? -mag : mag;
1401}
1402
Yaowu Xuf883b422016-08-30 14:01:10 -07001403static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +01001404 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001405 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001406 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001407 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001408 joint_type =
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -04001409 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410
1411 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001412 diff.row = read_mv_component(r, &ctx->comps[0],
RogerZhou3b635242017-09-19 10:06:46 -07001413#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001414 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001415#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001416 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001417
1418 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001419 diff.col = read_mv_component(r, &ctx->comps[1],
RogerZhou3b635242017-09-19 10:06:46 -07001420#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001421 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001422#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001423 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001424
Alex Converse6b2584c2017-05-02 09:51:21 -07001425 av1_inc_mv(&diff, counts, precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001426
1427 mv->row = ref->row + diff.row;
1428 mv->col = ref->col + diff.col;
1429}
1430
Yaowu Xuf883b422016-08-30 14:01:10 -07001431static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001432 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001433 aom_reader *r) {
Debargha Mukherjee0f248c42017-09-07 12:40:18 -07001434 if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type)) return SINGLE_REFERENCE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001435 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001436 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001437#if CONFIG_NEW_MULTISYMBOL
1438 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1439 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
1440#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001441 const REFERENCE_MODE mode =
Michael Bebenita6048d052016-08-25 14:40:54 -07001442 (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001443#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001444 FRAME_COUNTS *counts = xd->counts;
1445 if (counts) ++counts->comp_inter[ctx][mode];
1446 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1447 } else {
1448 return cm->reference_mode;
1449 }
1450}
1451
Thomas Davies315f5782017-06-14 15:14:55 +01001452#if CONFIG_NEW_MULTISYMBOL
1453#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001454 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001455#define READ_REF_BIT2(pname) \
1456 aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001457#else
1458#define READ_REF_BIT(pname) \
1459 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001460#define READ_REF_BIT2(pname) \
1461 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001462#endif
1463
Zoe Liuc082bbc2017-05-17 13:31:37 -07001464#if CONFIG_EXT_COMP_REFS
Zoe Liuec50d6b2017-08-23 16:02:59 -07001465static COMP_REFERENCE_TYPE read_comp_reference_type(AV1_COMMON *cm,
1466 const MACROBLOCKD *xd,
1467 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001468 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001469#if USE_UNI_COMP_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07001470 COMP_REFERENCE_TYPE comp_ref_type;
1471#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001472 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm)) {
1473 if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm)) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001474#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001475#if CONFIG_NEW_MULTISYMBOL
1476 (void)cm;
1477 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read_symbol(
1478 r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
1479#else
1480 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read(
1481 r, cm->fc->comp_ref_type_prob[ctx], ACCT_STR);
1482#endif
Zoe Liufcf5fa22017-06-26 16:00:38 -07001483#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001484 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001485 comp_ref_type = BIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001486 }
1487 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001488 comp_ref_type = UNIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001489 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001490#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001491#else // !USE_UNI_COMP_REFS
1492 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1493 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1494#endif // USE_UNI_COMP_REFS
1495 FRAME_COUNTS *counts = xd->counts;
1496 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1497 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1498}
1499#endif // CONFIG_EXT_COMP_REFS
1500
Yaowu Xuc27fc142016-08-22 16:08:15 -07001501// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001502static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1503 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001504 MV_REFERENCE_FRAME ref_frame[2]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505 FRAME_COUNTS *counts = xd->counts;
1506
1507 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1508 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1509 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001510 ref_frame[1] = NONE_FRAME;
David Barkerd92f3562017-10-09 17:46:23 +01001511 }
1512#if CONFIG_SEGMENT_ZEROMV
1513 else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
1514 segfeature_active(&cm->seg, segment_id, SEG_LVL_ZEROMV))
1515#else
1516 else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP))
1517#endif
1518 {
1519 ref_frame[0] = LAST_FRAME;
1520 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001521 } else {
1522 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
1523 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1524 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001525#if CONFIG_EXT_COMP_REFS
1526 const COMP_REFERENCE_TYPE comp_ref_type =
1527 read_comp_reference_type(cm, xd, r);
1528
1529#if !USE_UNI_COMP_REFS
1530 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1531 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
1532#endif // !USE_UNI_COMP_REFS
1533
1534 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001535 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1536 int bit;
1537#if CONFIG_VAR_REFS
1538 if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
1539#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001540 bit = READ_REF_BIT2(uni_comp_ref_p);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001541#if CONFIG_VAR_REFS
1542 else
1543 bit = BWD_AND_ALT(cm);
1544#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001545 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1546
1547 if (bit) {
1548 ref_frame[0] = BWDREF_FRAME;
1549 ref_frame[1] = ALTREF_FRAME;
1550 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001551 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1552 int bit1;
1553#if CONFIG_VAR_REFS
1554 if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm)))
1555#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001556 bit1 = READ_REF_BIT2(uni_comp_ref_p1);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001557#if CONFIG_VAR_REFS
1558 else
1559 bit1 = L_AND_L3(cm) || L_AND_G(cm);
1560#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001561 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1562
1563 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001564 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1565 int bit2;
1566#if CONFIG_VAR_REFS
1567 if (L_AND_L3(cm) && L_AND_G(cm))
1568#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001569 bit2 = READ_REF_BIT2(uni_comp_ref_p2);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001570#if CONFIG_VAR_REFS
1571 else
1572 bit2 = L_AND_G(cm);
1573#endif // CONFIG_VAR_REFS
1574 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1575
1576 if (bit2) {
1577 ref_frame[0] = LAST_FRAME;
1578 ref_frame[1] = GOLDEN_FRAME;
1579 } else {
1580 ref_frame[0] = LAST_FRAME;
1581 ref_frame[1] = LAST3_FRAME;
1582 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001583 } else {
1584 ref_frame[0] = LAST_FRAME;
1585 ref_frame[1] = LAST2_FRAME;
1586 }
1587 }
1588
1589 return;
1590 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001591
1592 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001593#endif // CONFIG_EXT_COMP_REFS
1594
1595// Normative in decoder (for low delay)
Zoe Liu17af2742017-10-06 10:36:42 -07001596#if CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001597 const int idx = 1;
Zoe Liu17af2742017-10-06 10:36:42 -07001598#else // !(CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001599#if CONFIG_EXT_REFS
1600 const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
Zoe Liuc082bbc2017-05-17 13:31:37 -07001601#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001602 const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
1603#endif // CONFIG_EXT_REFS
Zoe Liu17af2742017-10-06 10:36:42 -07001604#endif // CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001605
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001606 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
1607#if CONFIG_VAR_REFS
1608 int bit;
1609 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1610 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001611 bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001612 else
1613 bit = L3_OR_G(cm);
1614#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001615 const int bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001616#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001617 if (counts) ++counts->comp_ref[ctx][0][bit];
1618
1619#if CONFIG_EXT_REFS
1620 // Decode forward references.
1621 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001622 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001623#if CONFIG_VAR_REFS
1624 int bit1;
1625 // Test need to explicitly code (L) vs (L2) branch node in tree
1626 if (L_AND_L2(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001627 bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001628 else
1629 bit1 = LAST_IS_VALID(cm);
1630#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001631 const int bit1 = READ_REF_BIT(comp_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->comp_ref[ctx1][1][bit1];
1634 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
1635 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001637#if CONFIG_VAR_REFS
1638 int bit2;
1639 // Test need to explicitly code (L3) vs (G) branch node in tree
1640 if (L3_AND_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001641 bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001642 else
1643 bit2 = GOLDEN_IS_VALID(cm);
1644#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001645 const int bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001646#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001647 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1648 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1649 }
1650
1651 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001652 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
1653#if CONFIG_VAR_REFS
1654 int bit_bwd;
Zoe Liu3ac20932017-08-30 16:35:55 -07001655 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001656 const int bit_bwd_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001657 if (bit_bwd_uncertain)
Thomas Davies894cc812017-06-22 17:51:33 +01001658 bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001659 else
1660 bit_bwd = ALTREF_IS_VALID(cm);
Thomas Davies894cc812017-06-22 17:51:33 +01001661#else // !CONFIG_VAR_REFS
1662 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001663#endif // CONFIG_VAR_REFS
1664 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001665 if (!bit_bwd) {
1666 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
1667#if CONFIG_VAR_REFS
1668 int bit1_bwd;
1669 if (BWD_AND_ALT2(cm))
1670 bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1671 else
1672 bit1_bwd = ALTREF2_IS_VALID(cm);
1673#else // !CONFIG_VAR_REFS
1674 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1675#endif // CONFIG_VAR_REFS
1676 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1677 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1678 } else {
1679 ref_frame[idx] = cm->comp_bwd_ref[2];
1680 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001681#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001682 ref_frame[!idx] = cm->comp_var_ref[bit];
1683 ref_frame[idx] = cm->comp_fixed_ref;
1684#endif // CONFIG_EXT_REFS
1685 } else if (mode == SINGLE_REFERENCE) {
1686#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001687 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001688#if CONFIG_VAR_REFS
1689 int bit0;
Zoe Liue9b15e22017-07-19 15:53:01 -07001690 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT2,ALT) branch node
1691 // in tree
1692 if ((L_OR_L2(cm) || L3_OR_G(cm)) &&
1693 (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm)))
1694 bit0 = READ_REF_BIT(single_ref_p1);
1695 else
1696 bit0 = (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm));
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001697#else // !CONFIG_VAR_REFS
Thomas Davies315f5782017-06-14 15:14:55 +01001698 const int bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001699#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001700 if (counts) ++counts->single_ref[ctx0][0][bit0];
1701
1702 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001703 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001704#if CONFIG_VAR_REFS
1705 int bit1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001706 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001707 const int bit1_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001708 if (bit1_uncertain)
Thomas Davies315f5782017-06-14 15:14:55 +01001709 bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001710 else
1711 bit1 = ALTREF_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001712#else // !CONFIG_VAR_REFS
1713 const int bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001714#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001715 if (counts) ++counts->single_ref[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001716 if (!bit1) {
1717 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
1718#if CONFIG_VAR_REFS
1719 int bit5;
1720 if (BWD_AND_ALT2(cm))
1721 bit5 = READ_REF_BIT(single_ref_p6);
1722 else
1723 bit5 = ALTREF2_IS_VALID(cm);
1724#else // !CONFIG_VAR_REFS
1725 const int bit5 = READ_REF_BIT(single_ref_p6);
1726#endif // CONFIG_VAR_REFS
1727 if (counts) ++counts->single_ref[ctx5][5][bit5];
1728 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1729 } else {
1730 ref_frame[0] = ALTREF_FRAME;
1731 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001732 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001733 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001734#if CONFIG_VAR_REFS
1735 int bit2;
1736 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1737 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001738 bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001739 else
1740 bit2 = L3_OR_G(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001741#else // !CONFIG_VAR_REFS
1742 const int bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001743#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001744 if (counts) ++counts->single_ref[ctx2][2][bit2];
1745 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001746 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001747#if CONFIG_VAR_REFS
1748 int bit4;
1749 // Test need to explicitly code (L3) vs (G) branch node in tree
1750 if (L3_AND_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001751 bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001752 else
1753 bit4 = GOLDEN_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001754#else // !CONFIG_VAR_REFS
1755 const int bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001756#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001757 if (counts) ++counts->single_ref[ctx4][4][bit4];
1758 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1759 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001760 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001761#if CONFIG_VAR_REFS
1762 int bit3;
1763 // Test need to explicitly code (L) vs (L2) branch node in tree
1764 if (L_AND_L2(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001765 bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001766 else
1767 bit3 = LAST2_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001768#else // !CONFIG_VAR_REFS
1769 const int bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001770#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771 if (counts) ++counts->single_ref[ctx3][3][bit3];
1772 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1773 }
1774 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001775#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001776 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001777 const int bit0 = READ_REF_BIT(single_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001778 if (counts) ++counts->single_ref[ctx0][0][bit0];
1779
1780 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001781 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001782 const int bit1 = READ_REF_BIT(single_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001783 if (counts) ++counts->single_ref[ctx1][1][bit1];
1784 ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
1785 } else {
1786 ref_frame[0] = LAST_FRAME;
1787 }
1788#endif // CONFIG_EXT_REFS
1789
Emil Keyder01770b32017-01-20 18:03:11 -05001790 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001791 } else {
1792 assert(0 && "Invalid prediction mode.");
1793 }
1794 }
1795}
1796
Angie Chiang9c4f8952016-11-21 11:13:19 -08001797static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1798 MACROBLOCKD *const xd,
1799 MB_MODE_INFO *const mbmi,
1800 aom_reader *r) {
1801 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001802 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001803
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001804 if (!av1_is_interp_needed(xd)) {
1805 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001806 return;
1807 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001808
Yaowu Xuc27fc142016-08-22 16:08:15 -07001809 if (cm->interp_filter != SWITCHABLE) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001810 mbmi->interp_filters = av1_broadcast_interp_filter(cm->interp_filter);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001811 } else {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001812#if CONFIG_DUAL_FILTER
1813 InterpFilter ref0_filter[2] = { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR };
1814 for (int dir = 0; dir < 2; ++dir) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001815 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1816 (mbmi->ref_frame[1] > INTRA_FRAME &&
1817 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001818 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1819 ref0_filter[dir] =
Thomas Daviesec92c112017-09-25 11:03:58 +01001820 (InterpFilter)aom_read_symbol(r, ec_ctx->switchable_interp_cdf[ctx],
1821 SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001822 if (counts) ++counts->switchable_interp[ctx][ref0_filter[dir]];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001823 }
1824 }
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001825 // The index system works as: (0, 1) -> (vertical, horizontal) filter types
1826 mbmi->interp_filters =
1827 av1_make_interp_filters(ref0_filter[0], ref0_filter[1]);
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001828#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001829 const int ctx = av1_get_pred_context_switchable_interp(xd);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001830 InterpFilter filter = (InterpFilter)aom_read_symbol(
Thomas Daviesec92c112017-09-25 11:03:58 +01001831 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001832 mbmi->interp_filters = av1_broadcast_interp_filter(filter);
1833 if (counts) ++counts->switchable_interp[ctx][filter];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001834#endif // CONFIG_DUAL_FILTER
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001835 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001836}
1837
Jingning Han36fe3202017-02-20 22:31:49 -08001838static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1839 const int mi_col, MACROBLOCKD *const xd,
1840 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001841 MB_MODE_INFO *const mbmi = &mi->mbmi;
1842 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1843 int i;
1844
1845 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001846 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001847
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001848 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001849
Jingning Han52261842016-12-14 12:17:49 -08001850#if CONFIG_CB4X4
1851 (void)i;
Hui Suaa2965e2017-10-05 15:59:12 -07001852 mbmi->mode = read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
Jingning Han52261842016-12-14 12:17:49 -08001853#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001854 switch (bsize) {
1855 case BLOCK_4X4:
1856 for (i = 0; i < 4; ++i)
Hui Suaa2965e2017-10-05 15:59:12 -07001857 mi->bmi[i].as_mode = read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001858 mbmi->mode = mi->bmi[3].as_mode;
1859 break;
1860 case BLOCK_4X8:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001861 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001862 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001863 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001864 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001865 break;
1866 case BLOCK_8X4:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001867 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001868 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001869 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Hui Suaa2965e2017-10-05 15:59:12 -07001870 read_intra_mode(r, ec_ctx->y_mode_cdf[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001871 break;
1872 default:
Hui Suaa2965e2017-10-05 15:59:12 -07001873 mbmi->mode =
1874 read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001875 }
Jingning Han52261842016-12-14 12:17:49 -08001876#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001877
Jingning Han36fe3202017-02-20 22:31:49 -08001878#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001879 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001880 xd->plane[1].subsampling_y)) {
Hui Suaa2965e2017-10-05 15:59:12 -07001881 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001882#else
Hui Suaa2965e2017-10-05 15:59:12 -07001883 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001884 (void)mi_row;
1885 (void)mi_col;
1886#endif
1887
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001888#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001889 if (mbmi->uv_mode == UV_CFL_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001890 mbmi->cfl_alpha_idx =
David Michael Barrf6eaa152017-07-19 19:42:28 +09001891 read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001892 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001893 } else {
1894 xd->cfl->store_y = 0;
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001895 }
1896#endif // CONFIG_CFL
1897
1898#if CONFIG_CB4X4
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001899 } else {
1900 // Avoid decoding angle_info if there is is no chroma prediction
1901 mbmi->uv_mode = UV_DC_PRED;
1902#if CONFIG_CFL
1903 xd->cfl->is_chroma_reference = 0;
1904 xd->cfl->store_y = 1;
1905#endif
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001906 }
1907#endif
1908
Rupert Swarbrick766c9d52017-07-31 11:30:53 +01001909 // Explicitly ignore cm here to avoid a compile warning if none of
1910 // ext-intra, palette and filter-intra are enabled.
1911 (void)cm;
1912
Yaowu Xuc27fc142016-08-22 16:08:15 -07001913#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -07001914 read_intra_angle_info(xd, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001915#endif // CONFIG_EXT_INTRA
1916 mbmi->palette_mode_info.palette_size[0] = 0;
1917 mbmi->palette_mode_info.palette_size[1] = 0;
Hui Sue87fb232017-10-05 15:00:15 -07001918 if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001919 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001920#if CONFIG_FILTER_INTRA
1921 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1922 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001923 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001924 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001925#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001926}
1927
1928static INLINE int is_mv_valid(const MV *mv) {
1929 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1930 mv->col < MV_UPP;
1931}
1932
Yaowu Xuf883b422016-08-30 14:01:10 -07001933static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001934 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001935 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001936 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001937 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1938 int mi_col, int is_compound, int allow_hp,
1939 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001940 int i;
1941 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001942 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001943 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001944 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001945#if CONFIG_CB4X4
1946 int_mv *pred_mv = mbmi->pred_mv;
1947 (void)block;
1948#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001949 int_mv *pred_mv =
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07001950 (bsize >= BLOCK_8X8) ? mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08001951#endif // CONFIG_CB4X4
Sarah Parkere5299862016-08-16 14:57:37 -07001952 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001953 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001954 (void)mi_row;
1955 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001956 (void)bsize;
RogerZhou3b635242017-09-19 10:06:46 -07001957#if CONFIG_AMVR
1958 if (cm->cur_frame_mv_precision_level) {
1959 allow_hp = MV_SUBPEL_NONE;
1960 }
1961#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001962 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963 case NEWMV: {
1964 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001965 for (i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001966 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1967 int nmv_ctx =
1968 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1969 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001970 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001971 nmv_context_counts *const mv_counts =
1972 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001973 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974 ret = ret && is_mv_valid(&mv[i].as_mv);
1975
Yaowu Xuc27fc142016-08-22 16:08:15 -07001976 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977 }
1978 break;
1979 }
1980 case NEARESTMV: {
1981 mv[0].as_int = nearest_mv[0].as_int;
1982 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1983
Yaowu Xuc27fc142016-08-22 16:08:15 -07001984 pred_mv[0].as_int = nearest_mv[0].as_int;
1985 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001986 break;
1987 }
1988 case NEARMV: {
1989 mv[0].as_int = near_mv[0].as_int;
1990 if (is_compound) mv[1].as_int = near_mv[1].as_int;
1991
Yaowu Xuc27fc142016-08-22 16:08:15 -07001992 pred_mv[0].as_int = near_mv[0].as_int;
1993 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001994 break;
1995 }
1996 case ZEROMV: {
Sarah Parkere5299862016-08-16 14:57:37 -07001997#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +00001998 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001999 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002000 mi_col, mi_row, block
2001#if CONFIG_AMVR
2002 ,
2003 cm->cur_frame_mv_precision_level
2004#endif
2005 )
David Barkercdcac6d2016-12-01 17:04:16 +00002006 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07002007 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00002008 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002009 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002010 mi_col, mi_row, block
2011#if CONFIG_AMVR
2012 ,
2013 cm->cur_frame_mv_precision_level
2014#endif
2015 )
David Barkercdcac6d2016-12-01 17:04:16 +00002016 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07002017#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002018 mv[0].as_int = 0;
2019 if (is_compound) mv[1].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -07002020#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002021
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002022 pred_mv[0].as_int = mv[0].as_int;
2023 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002024 break;
2025 }
Zoe Liu85b66462017-04-20 14:28:19 -07002026#if CONFIG_COMPOUND_SINGLEREF
2027 case SR_NEAREST_NEARMV: {
2028 assert(!is_compound);
2029 mv[0].as_int = nearest_mv[0].as_int;
2030 mv[1].as_int = near_mv[0].as_int;
2031 break;
2032 }
2033 /*
2034 case SR_NEAREST_NEWMV: {
2035 assert(!is_compound);
2036 mv[0].as_int = nearest_mv[0].as_int;
2037
2038 FRAME_COUNTS *counts = xd->counts;
2039 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2040 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2041 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2042 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2043 nmv_context_counts *const mv_counts =
2044 counts ? &counts->mv[nmv_ctx] : NULL;
2045 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2046 ret = ret && is_mv_valid(&mv[1].as_mv);
2047 break;
2048 }*/
2049 case SR_NEAR_NEWMV: {
2050 assert(!is_compound);
2051 mv[0].as_int = near_mv[0].as_int;
2052
2053 FRAME_COUNTS *counts = xd->counts;
2054 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2055 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2056 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2057 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2058 nmv_context_counts *const mv_counts =
2059 counts ? &counts->mv[nmv_ctx] : NULL;
2060 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2061 ret = ret && is_mv_valid(&mv[1].as_mv);
2062 break;
2063 }
2064 case SR_ZERO_NEWMV: {
2065 assert(!is_compound);
2066#if CONFIG_GLOBAL_MOTION
2067 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
2068 cm->allow_high_precision_mv, bsize,
2069 mi_col, mi_row, block)
2070 .as_int;
2071#else
2072 mv[0].as_int = 0;
2073#endif // CONFIG_GLOBAL_MOTION
2074
2075 FRAME_COUNTS *counts = xd->counts;
2076 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2077 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2078 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2079 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2080 nmv_context_counts *const mv_counts =
2081 counts ? &counts->mv[nmv_ctx] : NULL;
2082 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2083 ret = ret && is_mv_valid(&mv[1].as_mv);
2084 break;
2085 }
2086 case SR_NEW_NEWMV: {
2087 assert(!is_compound);
2088
2089 FRAME_COUNTS *counts = xd->counts;
2090 for (i = 0; i < 2; ++i) {
2091 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2092 int nmv_ctx =
2093 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
2094 mbmi->ref_mv_idx);
2095 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2096 nmv_context_counts *const mv_counts =
2097 counts ? &counts->mv[nmv_ctx] : NULL;
2098 read_mv(r, &mv[i].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2099 ret = ret && is_mv_valid(&mv[i].as_mv);
2100 }
2101 break;
2102 }
2103#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002104 case NEW_NEWMV: {
2105 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002106 assert(is_compound);
2107 for (i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002108 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2109 int nmv_ctx =
2110 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
2111 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002112 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002113 nmv_context_counts *const mv_counts =
2114 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002115 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002116 ret = ret && is_mv_valid(&mv[i].as_mv);
2117 }
2118 break;
2119 }
2120 case NEAREST_NEARESTMV: {
2121 assert(is_compound);
2122 mv[0].as_int = nearest_mv[0].as_int;
2123 mv[1].as_int = nearest_mv[1].as_int;
2124 break;
2125 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002126 case NEAR_NEARMV: {
2127 assert(is_compound);
2128 mv[0].as_int = near_mv[0].as_int;
2129 mv[1].as_int = near_mv[1].as_int;
2130 break;
2131 }
2132 case NEW_NEARESTMV: {
2133 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002134 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2135 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2136 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002137 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002138 nmv_context_counts *const mv_counts =
2139 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002140 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002141 assert(is_compound);
2142 ret = ret && is_mv_valid(&mv[0].as_mv);
2143 mv[1].as_int = nearest_mv[1].as_int;
2144 break;
2145 }
2146 case NEAREST_NEWMV: {
2147 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002148 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2149 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2150 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002151 nmv_context_counts *const mv_counts =
2152 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002153 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07002154 mv[0].as_int = nearest_mv[0].as_int;
2155 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002156 assert(is_compound);
2157 ret = ret && is_mv_valid(&mv[1].as_mv);
2158 break;
2159 }
2160 case NEAR_NEWMV: {
2161 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002162 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2163 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2164 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002165 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002166 nmv_context_counts *const mv_counts =
2167 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002168 mv[0].as_int = near_mv[0].as_int;
2169 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002170 assert(is_compound);
2171
2172 ret = ret && is_mv_valid(&mv[1].as_mv);
2173 break;
2174 }
2175 case NEW_NEARMV: {
2176 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002177 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2178 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2179 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002180 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002181 nmv_context_counts *const mv_counts =
2182 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002183 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002184 assert(is_compound);
2185 ret = ret && is_mv_valid(&mv[0].as_mv);
2186 mv[1].as_int = near_mv[1].as_int;
2187 break;
2188 }
2189 case ZERO_ZEROMV: {
2190 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08002191#if CONFIG_GLOBAL_MOTION
2192 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002193 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002194 mi_col, mi_row, block
2195#if CONFIG_AMVR
2196 ,
2197 cm->cur_frame_mv_precision_level
2198#endif
2199 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002200 .as_int;
2201 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002202 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002203 mi_col, mi_row, block
2204#if CONFIG_AMVR
2205 ,
2206 cm->cur_frame_mv_precision_level
2207#endif
2208 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002209 .as_int;
2210#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211 mv[0].as_int = 0;
2212 mv[1].as_int = 0;
Sarah Parkerc2d38712017-01-24 15:15:41 -08002213#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002214 break;
2215 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002216 default: { return 0; }
2217 }
2218 return ret;
2219}
2220
Yaowu Xuf883b422016-08-30 14:01:10 -07002221static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
2222 int segment_id, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
2224 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
2225 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002226 const int ctx = av1_get_intra_inter_context(xd);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002227#if CONFIG_NEW_MULTISYMBOL
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002228 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002229 const int is_inter =
2230 aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
2231#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002232 const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002233#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002234 FRAME_COUNTS *counts = xd->counts;
2235 if (counts) ++counts->intra_inter[ctx][is_inter];
2236 return is_inter;
2237 }
2238}
2239
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002240#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002241static int read_is_inter_singleref_comp_mode(AV1_COMMON *const cm,
2242 MACROBLOCKD *const xd,
2243 int segment_id, aom_reader *r) {
2244 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) return 0;
2245
2246 const int ctx = av1_get_inter_mode_context(xd);
2247 const int is_singleref_comp_mode =
2248 aom_read(r, cm->fc->comp_inter_mode_prob[ctx], ACCT_STR);
2249 FRAME_COUNTS *counts = xd->counts;
2250
2251 if (counts) ++counts->comp_inter_mode[ctx][is_singleref_comp_mode];
2252 return is_singleref_comp_mode;
2253}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002254#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002255
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002257 AV1Decoder *const pbi = (AV1Decoder *)data;
2258 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
2259 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002260}
2261
Di Chen56586622017-06-09 13:49:44 -07002262#if DEC_MISMATCH_DEBUG
Zoe Liu0b3ef732017-10-06 18:37:24 -07002263static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi, int mi_row,
2264 int mi_col,
Zoe Liuf9333f52017-07-03 10:52:01 -07002265 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES],
2266 int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07002267 int_mv mv[2] = { { 0 } };
2268 int ref;
2269 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07002270 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
2271 mv[ref].as_mv = mbmi->mv[ref].as_mv;
2272
Di Chen56586622017-06-09 13:49:44 -07002273 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
2274 int16_t zeromv_ctx = -1;
2275 int16_t refmv_ctx = -1;
2276 if (mbmi->mode != NEWMV) {
2277 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == ZEROMV);
2278 zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
2279 if (mbmi->mode != ZEROMV) {
2280 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
2281 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
2282 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
2283 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
2284 }
2285 }
2286
2287 int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Zoe Liuf9333f52017-07-03 10:52:01 -07002288#define FRAME_TO_CHECK 1
Zoe Liu17af2742017-10-06 10:36:42 -07002289 if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) {
Zoe Liuf9333f52017-07-03 10:52:01 -07002290 printf(
2291 "=== DECODER ===: "
2292 "Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
2293 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
2294 "ref[1]=%d, motion_mode=%d, inter_mode_ctx=%d, mode_ctx=%d, "
Zoe Liu0b3ef732017-10-06 18:37:24 -07002295 "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d\n",
Zoe Liuf9333f52017-07-03 10:52:01 -07002296 cm->current_video_frame, mi_row, mi_col, mbmi->mode, mbmi->sb_type,
2297 cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
2298 mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
Zoe Liu0b3ef732017-10-06 18:37:24 -07002299 mbmi->motion_mode, inter_mode_ctx[ref_frame_type], mode_ctx, newmv_ctx,
2300 zeromv_ctx, refmv_ctx);
Zoe Liuf9333f52017-07-03 10:52:01 -07002301 }
Di Chen56586622017-06-09 13:49:44 -07002302}
2303#endif // DEC_MISMATCH_DEBUG
2304
Yaowu Xuf883b422016-08-30 14:01:10 -07002305static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002306 MACROBLOCKD *const xd,
2307 MODE_INFO *const mi,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002308#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002309 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002310 int supertx_enabled) {
2311#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002312 int mi_row, int mi_col, aom_reader *r) {
Yue Chencb60b182016-10-13 15:18:22 -07002313#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002314 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002315 MB_MODE_INFO *const mbmi = &mi->mbmi;
2316 const BLOCK_SIZE bsize = mbmi->sb_type;
2317 const int allow_hp = cm->allow_high_precision_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08002318 const int unify_bsize = CONFIG_CB4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 int_mv nearestmv[2], nearmv[2];
2320 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002321 int ref, is_compound;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002322#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002323 int is_singleref_comp_mode = 0;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002324#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002325 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002327 int16_t mode_ctx = 0;
Yue Chen69f18e12016-09-08 14:48:15 -07002328#if CONFIG_WARPED_MOTION
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08002329 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang97d6a372017-10-09 14:15:15 -07002330#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07002331 int pts_mv[SAMPLES_ARRAY_SIZE];
Yunqing Wang97d6a372017-10-09 14:15:15 -07002332#endif // CONFIG_EXT_WARPED_MOTION
Yue Chen69f18e12016-09-08 14:48:15 -07002333#endif // CONFIG_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00002334 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002335
Urvang Joshi5a9ea002017-05-22 15:25:18 -07002336 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
2337
Luc Trudeaub05eeae2017-08-18 15:14:30 -04002338 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002339 mbmi->palette_mode_info.palette_size[0] = 0;
2340 mbmi->palette_mode_info.palette_size[1] = 0;
2341
Frederic Barbier7a84fd82017-03-02 18:08:15 +01002342 memset(ref_mvs, 0, sizeof(ref_mvs));
2343
Yaowu Xuc27fc142016-08-22 16:08:15 -07002344 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
2345 is_compound = has_second_ref(mbmi);
2346
Zoe Liuc082bbc2017-05-17 13:31:37 -07002347#if CONFIG_EXT_COMP_REFS
2348#if !USE_UNI_COMP_REFS
2349 // NOTE: uni-directional comp refs disabled
2350 if (is_compound)
2351 assert(mbmi->ref_frame[0] < BWDREF_FRAME &&
2352 mbmi->ref_frame[1] >= BWDREF_FRAME);
2353#endif // !USE_UNI_COMP_REFS
2354#endif // CONFIG_EXT_COMP_REFS
2355
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002356#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002357 if (!is_compound)
2358 is_singleref_comp_mode =
2359 read_is_inter_singleref_comp_mode(cm, xd, mbmi->segment_id, r);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002360#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002361
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362 for (ref = 0; ref < 1 + is_compound; ++ref) {
2363 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002364
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002365 av1_find_mv_refs(cm, xd, mi, frame, &xd->ref_mv_count[frame],
2366 xd->ref_mv_stack[frame], compound_inter_mode_ctx,
2367 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2368 inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002369 }
2370
Jingning Hanacddc032016-11-17 15:26:20 -08002371 if (is_compound) {
2372 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002373 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002374 xd->ref_mv_stack[ref_frame], compound_inter_mode_ctx,
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002375 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2376 inter_mode_ctx);
2377
2378 if (xd->ref_mv_count[ref_frame] < 2) {
2379 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00002380 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002381 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00002382#if CONFIG_GLOBAL_MOTION
2383 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00002384 cm->allow_high_precision_mv,
RogerZhou3b635242017-09-19 10:06:46 -07002385 bsize, mi_col, mi_row, 0
2386#if CONFIG_AMVR
2387 ,
2388 cm->cur_frame_mv_precision_level
2389#endif
2390 )
David Barkercdcac6d2016-12-01 17:04:16 +00002391 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002392 zeromv[1].as_int =
2393 (rf[1] != NONE_FRAME)
2394 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
2395 cm->allow_high_precision_mv, bsize, mi_col,
2396 mi_row, 0
2397#if CONFIG_AMVR
2398 ,
2399 cm->cur_frame_mv_precision_level
2400#endif
2401 )
2402 .as_int
2403 : 0;
David Barkercdcac6d2016-12-01 17:04:16 +00002404#else
2405 zeromv[0].as_int = zeromv[1].as_int = 0;
2406#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002407 for (ref = 0; ref < 2; ++ref) {
2408 if (rf[ref] == NONE_FRAME) continue;
RogerZhou3b635242017-09-19 10:06:46 -07002409#if CONFIG_AMVR
2410 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp,
2411 cm->cur_frame_mv_precision_level);
2412 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp,
2413 cm->cur_frame_mv_precision_level);
2414#else
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002415 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
2416 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002417#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002418 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
2419 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
2420 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01002421 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002422 }
2423 }
2424
Zoe Liu85b66462017-04-20 14:28:19 -07002425#if CONFIG_COMPOUND_SINGLEREF
2426 if (is_compound || is_singleref_comp_mode)
2427#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002428 if (is_compound)
Zoe Liu85b66462017-04-20 14:28:19 -07002429#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002430 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
2431 else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002432 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07002433 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002434 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002436#if CONFIG_SEGMENT_ZEROMV
2437 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
David Barkerd92f3562017-10-09 17:46:23 +01002438 segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_ZEROMV))
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002439#else
David Barkerd92f3562017-10-09 17:46:23 +01002440 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002441#endif
David Barkerd92f3562017-10-09 17:46:23 +01002442 {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 mbmi->mode = ZEROMV;
Debargha Mukherjeec76f9dc2017-05-01 13:18:09 -07002444 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002445 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
David Barker3409c0d2017-06-30 17:33:13 +01002446 "Invalid usage of segment feature on small blocks");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447 return;
2448 }
2449 } else {
Jingning Han5cfa6712016-12-14 09:53:38 -08002450 if (bsize >= BLOCK_8X8 || unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451 if (is_compound)
2452 mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002453#if CONFIG_COMPOUND_SINGLEREF
2454 else if (is_singleref_comp_mode)
Thomas Daviesb8b14a92017-07-12 15:11:49 +01002455 mbmi->mode = read_inter_singleref_comp_mode(xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002456#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457 else
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002458 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
David Barker3dfba992017-04-03 16:10:09 +01002459 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002460#if CONFIG_COMPOUND_SINGLEREF
2461 mbmi->mode == SR_NEW_NEWMV ||
2462#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002463 have_nearmv_in_inter_mode(mbmi->mode))
Thomas Davies149eda52017-06-12 18:11:55 +01002464 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 }
2466 }
2467
David Barker9b75e212017-07-28 15:31:41 +01002468 if ((bsize < BLOCK_8X8 && !unify_bsize) ||
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002469 (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470 for (ref = 0; ref < 1 + is_compound; ++ref) {
RogerZhou3b635242017-09-19 10:06:46 -07002471#if CONFIG_AMVR
2472 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2473 &nearestmv[ref], &nearmv[ref],
2474 cm->cur_frame_mv_precision_level);
2475#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002476 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2477 &nearestmv[ref], &nearmv[ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002478#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002479 }
2480 }
2481
Zoe Liu85b66462017-04-20 14:28:19 -07002482#if CONFIG_COMPOUND_SINGLEREF
2483 if ((is_compound || is_singleref_comp_mode) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002484 (bsize >= BLOCK_8X8 || unify_bsize) && mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002485#else // !CONFIG_COMPOUND_SINGLEREF
Jingning Han61418bb2017-01-23 17:12:48 -08002486 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
Yushin Cho127c5832017-07-28 16:39:04 -07002487 mbmi->mode != ZERO_ZEROMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002488#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -07002489 {
Yaowu Xuf883b422016-08-30 14:01:10 -07002490 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002492 if (xd->ref_mv_count[ref_frame_type] > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493 if (mbmi->mode == NEAREST_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002494 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2495 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002496#if CONFIG_AMVR
2497 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
2498 cm->cur_frame_mv_precision_level);
2499 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
2500 cm->cur_frame_mv_precision_level);
2501#else
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002502 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
2503 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002504#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002505 } else if (mbmi->mode == NEAREST_NEWMV
2506#if CONFIG_COMPOUND_SINGLEREF
2507 || mbmi->mode == SR_NEAREST_NEARMV
2508// || mbmi->mode == SR_NEAREST_NEWMV
2509#endif // CONFIG_COMPOUND_SINGLEREF
2510 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002512
2513#if CONFIG_AMVR
2514 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
2515 cm->cur_frame_mv_precision_level);
2516#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002517 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002518#endif
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07002519 } else if (mbmi->mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002520 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002521#if CONFIG_AMVR
2522 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
2523 cm->cur_frame_mv_precision_level);
2524#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002525 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002526#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002527 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002528 }
2529
Yaowu Xuc27fc142016-08-22 16:08:15 -07002530 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01002531 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
Zoe Liu85b66462017-04-20 14:28:19 -07002532#if CONFIG_COMPOUND_SINGLEREF
2533 if (is_compound) {
2534#endif // CONFIG_COMPOUND_SINGLEREF
2535 if (compound_ref0_mode(mbmi->mode) == NEARMV) {
2536 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002537#if CONFIG_AMVR
2538 lower_mv_precision(&nearmv[0].as_mv, allow_hp,
2539 cm->cur_frame_mv_precision_level);
2540#else
2541 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2542#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002543 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544
Zoe Liu85b66462017-04-20 14:28:19 -07002545 if (compound_ref1_mode(mbmi->mode) == NEARMV) {
2546 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002547#if CONFIG_AMVR
2548 lower_mv_precision(&nearmv[1].as_mv, allow_hp,
2549 cm->cur_frame_mv_precision_level);
2550#else
2551 lower_mv_precision(&nearmv[1].as_mv, allow_hp);
2552#endif
Zoe Liu85b66462017-04-20 14:28:19 -07002553 }
2554#if CONFIG_COMPOUND_SINGLEREF
2555 } else {
2556 assert(is_singleref_comp_mode);
2557 if (compound_ref0_mode(mbmi->mode) == NEARMV ||
2558 compound_ref1_mode(mbmi->mode) == NEARMV) {
2559 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2560 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2561 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002562 }
Zoe Liu85b66462017-04-20 14:28:19 -07002563#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002564 }
David Barker0d7c4b02017-07-25 14:55:48 +01002565 } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
2566 int_mv cur_mv =
2567 xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
2568 nearmv[0] = cur_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570
Yue Chen19e7aa82016-11-30 14:05:39 -08002571#if !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION && !CONFIG_GLOBAL_MOTION
Angie Chiang9c4f8952016-11-21 11:13:19 -08002572 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002573#endif // !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574
Jingning Han5cfa6712016-12-14 09:53:38 -08002575 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576 const int num_4x4_w = 1 << xd->bmode_blocks_wl;
2577 const int num_4x4_h = 1 << xd->bmode_blocks_hl;
2578 int idx, idy;
2579 PREDICTION_MODE b_mode;
2580 int_mv nearest_sub8x8[2], near_sub8x8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581 int_mv ref_mv[2][2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582 for (idy = 0; idy < 2; idy += num_4x4_h) {
2583 for (idx = 0; idx < 2; idx += num_4x4_w) {
2584 int_mv block[2];
2585 const int j = idy * 2 + idx;
2586 int_mv ref_mv_s8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002587 if (!is_compound)
Yaowu Xuf883b422016-08-30 14:01:10 -07002588 mode_ctx = av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame,
2589 bsize, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 if (is_compound)
2591 b_mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
2592 else
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002593 b_mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002594
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002595 if (b_mode != ZEROMV && b_mode != ZERO_ZEROMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596 CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
2597 uint8_t ref_mv_count[2];
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002598 for (ref = 0; ref < 1 + is_compound; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002599 int_mv mv_ref_list[MAX_MV_REF_CANDIDATES];
Yaowu Xu531d6af2017-03-07 17:48:52 -08002600 av1_update_mv_context(cm, xd, mi, mbmi->ref_frame[ref], mv_ref_list,
2601 j, mi_row, mi_col, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07002602 av1_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
Yaowu Xuf883b422016-08-30 14:01:10 -07002603 ref_mv_stack[ref], &ref_mv_count[ref],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002604 mv_ref_list, &nearest_sub8x8[ref],
Yaowu Xuf883b422016-08-30 14:01:10 -07002605 &near_sub8x8[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606 if (have_newmv_in_inter_mode(b_mode)) {
2607 mv_ref_list[0].as_int = nearest_sub8x8[ref].as_int;
2608 mv_ref_list[1].as_int = near_sub8x8[ref].as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002609#if CONFIG_AMVR
2610 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2611 &ref_mv[1][ref],
2612 cm->cur_frame_mv_precision_level);
2613#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002614 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2615 &ref_mv[1][ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002616#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 }
2618 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619 }
2620
2621 for (ref = 0; ref < 1 + is_compound && b_mode != ZEROMV; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 ref_mv_s8[ref] = nearest_sub8x8[ref];
RogerZhou3b635242017-09-19 10:06:46 -07002623#if CONFIG_AMVR
2624 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp,
2625 cm->cur_frame_mv_precision_level);
2626#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002627 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002628#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002629 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002630 (void)ref_mv_s8;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002632 if (!assign_mv(cm, xd, b_mode, mbmi->ref_frame, j, block, ref_mv[0],
David Barker45390c12017-02-20 14:44:40 +00002633 nearest_sub8x8, near_sub8x8, mi_row, mi_col, is_compound,
2634 allow_hp, r)) {
Angie Chiangd0916d92017-03-10 17:54:18 -08002635 aom_merge_corrupted_flag(&xd->corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636 break;
2637 };
2638
2639 mi->bmi[j].as_mv[0].as_int = block[0].as_int;
Sarah Parkerd7fa8542016-10-11 11:51:59 -07002640 mi->bmi[j].as_mode = b_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002641 if (is_compound) mi->bmi[j].as_mv[1].as_int = block[1].as_int;
2642
2643 if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
2644 if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
2645 }
2646 }
2647
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07002648 mbmi->pred_mv[0].as_int = mi->bmi[3].pred_mv[0].as_int;
2649 mbmi->pred_mv[1].as_int = mi->bmi[3].pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002650 mi->mbmi.mode = b_mode;
2651
2652 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2653 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2654 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 int_mv ref_mv[2];
2656 ref_mv[0] = nearestmv[0];
2657 ref_mv[1] = nearestmv[1];
2658
David Barker3dfba992017-04-03 16:10:09 +01002659 if (is_compound) {
David Barker3dfba992017-04-03 16:10:09 +01002660 int ref_mv_idx = mbmi->ref_mv_idx;
2661 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2662 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2663 // mbmi->ref_mv_idx (like NEWMV)
2664 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2665 ref_mv_idx = 1 + mbmi->ref_mv_idx;
David Barker3dfba992017-04-03 16:10:09 +01002666
2667 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
David Barker404b2e82017-03-27 13:07:47 +01002668 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2669 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker3dfba992017-04-03 16:10:09 +01002670 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002671 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker404b2e82017-03-27 13:07:47 +01002672 xd->n8_h << MI_SIZE_LOG2, xd);
2673 }
David Barker3dfba992017-04-03 16:10:09 +01002674 nearestmv[0] = ref_mv[0];
David Barker404b2e82017-03-27 13:07:47 +01002675 }
David Barker3dfba992017-04-03 16:10:09 +01002676 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
David Barker3dfba992017-04-03 16:10:09 +01002677 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2678 if (xd->ref_mv_count[ref_frame_type] > 1) {
2679 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002680 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker3dfba992017-04-03 16:10:09 +01002681 xd->n8_h << MI_SIZE_LOG2, xd);
2682 }
David Barker3dfba992017-04-03 16:10:09 +01002683 nearestmv[1] = ref_mv[1];
2684 }
Zoe Liu85b66462017-04-20 14:28:19 -07002685#if CONFIG_COMPOUND_SINGLEREF
2686 } else if (is_singleref_comp_mode) {
2687 int ref_mv_idx = mbmi->ref_mv_idx;
2688 // Special case: SR_NEAR_NEWMV use 1 + mbmi->ref_mv_idx (like NEARMV)
2689 // instead of mbmi->ref_mv_idx (like NEWMV)
2690 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
2691
2692 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
2693 compound_ref1_mode(mbmi->mode) == NEWMV) {
2694 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2695 if (xd->ref_mv_count[ref_frame_type] > 1) {
2696 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2697 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2698 xd->n8_h << MI_SIZE_LOG2, xd);
2699 }
2700 // TODO(zoeliu): To further investigate why this would not cause a
2701 // mismatch for the mode of SR_NEAREST_NEWMV.
2702 nearestmv[0] = ref_mv[0];
2703 }
2704#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002705 } else {
David Barker3dfba992017-04-03 16:10:09 +01002706 if (mbmi->mode == NEWMV) {
2707 for (ref = 0; ref < 1 + is_compound; ++ref) {
David Barker3dfba992017-04-03 16:10:09 +01002708 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2709 if (xd->ref_mv_count[ref_frame_type] > 1) {
2710 ref_mv[ref] =
2711 (ref == 0)
2712 ? xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv
2713 : xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
2714 .comp_mv;
2715 clamp_mv_ref(&ref_mv[ref].as_mv, xd->n8_w << MI_SIZE_LOG2,
2716 xd->n8_h << MI_SIZE_LOG2, xd);
2717 }
David Barker3dfba992017-04-03 16:10:09 +01002718 nearestmv[ref] = ref_mv[ref];
2719 }
2720 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002721 }
2722
Angie Chiangd0916d92017-03-10 17:54:18 -08002723 int mv_corrupted_flag =
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002724 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
David Barker45390c12017-02-20 14:44:40 +00002725 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
Angie Chiangd0916d92017-03-10 17:54:18 -08002726 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002727 }
2728
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002729#if CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002730 mbmi->use_wedge_interintra = 0;
2731 if (cm->reference_mode != COMPOUND_REFERENCE &&
2732#if CONFIG_SUPERTX
2733 !supertx_enabled &&
2734#endif
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002735 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002736 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002737#if CONFIG_NEW_MULTISYMBOL
2738 const int interintra =
2739 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
2740#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002741 const int interintra =
2742 aom_read(r, cm->fc->interintra_prob[bsize_group], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002743#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002744 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002745 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002746 if (interintra) {
2747 const INTERINTRA_MODE interintra_mode =
2748 read_interintra_mode(cm, xd, r, bsize_group);
2749 mbmi->ref_frame[1] = INTRA_FRAME;
2750 mbmi->interintra_mode = interintra_mode;
2751#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002752 mbmi->angle_delta[0] = 0;
2753 mbmi->angle_delta[1] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002755#if CONFIG_FILTER_INTRA
2756 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2757 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2758#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002759 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002760#if CONFIG_NEW_MULTISYMBOL
2761 mbmi->use_wedge_interintra = aom_read_symbol(
2762 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
2763#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002764 mbmi->use_wedge_interintra =
Michael Bebenita6048d052016-08-25 14:40:54 -07002765 aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002766#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002767 if (xd->counts)
2768 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2769 if (mbmi->use_wedge_interintra) {
2770 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002771 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002772 mbmi->interintra_wedge_sign = 0;
2773 }
2774 }
2775 }
2776 }
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002777#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002778
Yue Chen52c51732017-07-11 15:08:30 -07002779#if CONFIG_WARPED_MOTION
2780 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
2781 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2782 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2783
2784 xd->block_refs[ref] = ref_buf;
2785 }
2786#endif
2787
Yue Chencb60b182016-10-13 15:18:22 -07002788#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
2789 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yue Chen69f18e12016-09-08 14:48:15 -07002790#if CONFIG_WARPED_MOTION
2791 if (mbmi->sb_type >= BLOCK_8X8 && !has_second_ref(mbmi))
Yunqing Wang97d6a372017-10-09 14:15:15 -07002792#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07002793 mbmi->num_proj_ref[0] =
2794 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv);
2795#else
Yue Chen69f18e12016-09-08 14:48:15 -07002796 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang97d6a372017-10-09 14:15:15 -07002797#endif // CONFIG_EXT_WARPED_MOTION
Yue Chen69f18e12016-09-08 14:48:15 -07002798#endif // CONFIG_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002799#if CONFIG_MOTION_VAR
2800 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
2801#endif
Yue Chen69f18e12016-09-08 14:48:15 -07002802
Yaowu Xuc27fc142016-08-22 16:08:15 -07002803#if CONFIG_SUPERTX
Yue Chen69f18e12016-09-08 14:48:15 -07002804 if (!supertx_enabled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002805#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002806 if (mbmi->ref_frame[1] != INTRA_FRAME)
Sarah Parker19234cc2017-03-10 16:43:25 -08002807 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002808
2809#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -07002810 read_ncobmc_mode(xd, mi, mbmi->ncobmc_mode, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002811#endif
2812
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002813#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002814 if (is_singleref_comp_mode) assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002815#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chen69f18e12016-09-08 14:48:15 -07002816#if CONFIG_WARPED_MOTION
2817 if (mbmi->motion_mode == WARPED_CAUSAL) {
2818 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
Yunqing Wang1bc82862017-06-28 15:49:48 -07002819
Yunqing Wang97d6a372017-10-09 14:15:15 -07002820#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07002821 if (mbmi->num_proj_ref[0] > 1)
2822 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2823 pts_inref, mbmi->num_proj_ref[0]);
Yunqing Wang97d6a372017-10-09 14:15:15 -07002824#endif // CONFIG_EXT_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07002825
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002826 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2827 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2828 &mbmi->wm_params[0], mi_row, mi_col)) {
Yunqing Wang8657ad72017-06-20 14:46:08 -07002829 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "Invalid Warped Model");
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002830 }
Yue Chen69f18e12016-09-08 14:48:15 -07002831 }
2832#endif // CONFIG_WARPED_MOTION
2833#if CONFIG_SUPERTX
2834 }
2835#endif // CONFIG_SUPERTX
Yue Chencb60b182016-10-13 15:18:22 -07002836#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002837
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002838 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Zoe Liu85b66462017-04-20 14:28:19 -07002839 if (
2840#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu0c634c72017-06-19 07:06:28 -07002841 is_inter_anyref_comp_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002842#else // !CONFIG_COMPOUND_SINGLEREF
2843 cm->reference_mode != SINGLE_REFERENCE &&
Sarah Parker6fdc8532016-11-16 17:47:13 -08002844 is_inter_compound_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002845#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chencb60b182016-10-13 15:18:22 -07002846#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002847 && mbmi->motion_mode == SIMPLE_TRANSLATION
Yue Chencb60b182016-10-13 15:18:22 -07002848#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002849 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002850 if (is_any_masked_compound_used(bsize)) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002851#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002852 if (cm->allow_masked_compound) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002853#if CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2854 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
2855 mbmi->interinter_compound_type =
2856 aom_read_bit(r, ACCT_STR) ? COMPOUND_AVERAGE : COMPOUND_SEG;
2857 else
2858#endif // CONFIG_WEDGE && CONFIG_COMPOUND_SEGMENT
2859 mbmi->interinter_compound_type = aom_read_symbol(
2860 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002861#if CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002862 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002863 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002864 mbmi->wedge_index =
2865 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2866 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2867 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002868#endif // CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002869#if CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002870 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
2871 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
2872 }
Sarah Parker42d96102017-01-31 21:05:27 -08002873#endif // CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002874 }
2875#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002876 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002877 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002878 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002880 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002882
Yue Chen19e7aa82016-11-30 14:05:39 -08002883#if CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002884 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002885#endif // CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07002886
Di Chen56586622017-06-09 13:49:44 -07002887#if DEC_MISMATCH_DEBUG
Zoe Liu0b3ef732017-10-06 18:37:24 -07002888 dec_dump_logs(cm, mi, mi_row, mi_col, inter_mode_ctx, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002889#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890}
2891
Yaowu Xuf883b422016-08-30 14:01:10 -07002892static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893 MACROBLOCKD *const xd,
2894#if CONFIG_SUPERTX
2895 int supertx_enabled,
2896#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002897 int mi_row, int mi_col, aom_reader *r) {
2898 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002899 MODE_INFO *const mi = xd->mi[0];
2900 MB_MODE_INFO *const mbmi = &mi->mbmi;
2901 int inter_block = 1;
2902#if CONFIG_VAR_TX
2903 BLOCK_SIZE bsize = mbmi->sb_type;
2904#endif // CONFIG_VAR_TX
2905
2906 mbmi->mv[0].as_int = 0;
2907 mbmi->mv[1].as_int = 0;
2908 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
2909#if CONFIG_SUPERTX
David Barker3aec8d62017-01-31 14:55:32 +00002910 if (!supertx_enabled)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002911#endif // CONFIG_SUPERTX
2912 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
David Barker3aec8d62017-01-31 14:55:32 +00002913
David Barker3aec8d62017-01-31 14:55:32 +00002914 if (cm->delta_q_present_flag) {
2915 xd->current_qindex =
2916 xd->prev_qindex +
2917 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01002918 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2919 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
David Barker3aec8d62017-01-31 14:55:32 +00002920 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07002921#if CONFIG_EXT_DELTA_Q
2922 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07002923#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07002924 if (cm->delta_lf_multi) {
2925 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
2926 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
2927 xd->prev_delta_lf[lf_id] +
2928 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
2929 cm->delta_lf_res;
2930 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
2931 }
2932 } else {
2933 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2934 xd->prev_delta_lf_from_base +
2935 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07002936 cm->delta_lf_res;
Cheng Chen880166a2017-10-02 17:48:48 -07002937 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07002938 }
2939#else
Cheng Chen9dccdf22017-10-02 15:04:40 -07002940 const int current_delta_lf_from_base =
Fangwen Fu231fe422017-04-24 17:52:29 -07002941 xd->prev_delta_lf_from_base +
2942 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
2943 cm->delta_lf_res;
Cheng Chen9dccdf22017-10-02 15:04:40 -07002944 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2945 clamp(current_delta_lf_from_base, 0, MAX_LOOP_FILTER);
Fangwen Fu231fe422017-04-24 17:52:29 -07002946 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07002947#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07002948 }
2949#endif
David Barker3aec8d62017-01-31 14:55:32 +00002950 }
David Barker3aec8d62017-01-31 14:55:32 +00002951
2952#if CONFIG_SUPERTX
2953 if (!supertx_enabled) {
2954#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
2956
2957#if CONFIG_VAR_TX
Jingning Han331662e2017-05-30 17:03:32 -07002958 xd->above_txfm_context =
2959 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2960 xd->left_txfm_context = xd->left_txfm_context_buffer +
2961 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002962
2963 if (cm->tx_mode == TX_MODE_SELECT &&
2964#if CONFIG_CB4X4
Jingning Han3daa4fd2017-01-20 10:33:50 -08002965 bsize > BLOCK_4X4 &&
Jingning Han581d1692017-01-05 16:03:54 -08002966#else
2967 bsize >= BLOCK_8X8 &&
2968#endif
David Barker16c64e32017-08-23 16:54:59 +01002969 !mbmi->skip && inter_block && !xd->lossless[mbmi->segment_id]) {
Jingning Han70e5f3f2016-11-09 17:03:07 -08002970 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07002971 const int bh = tx_size_high_unit[max_tx_size];
2972 const int bw = tx_size_wide_unit[max_tx_size];
Jingning Han65abc312016-10-27 13:04:21 -07002973 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2974 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975 int idx, idy;
Sarah Parkerd25ef8c2017-10-06 12:17:30 -07002976 int init_depth =
2977 (height != width) ? RECT_VARTX_DEPTH_INIT : SQR_VARTX_DEPTH_INIT;
Yue Chena1e48dc2016-08-29 17:29:33 -07002978
Jingning Hanfe45b212016-11-22 10:30:23 -08002979 mbmi->min_tx_size = TX_SIZES_ALL;
2980 for (idy = 0; idy < height; idy += bh)
2981 for (idx = 0; idx < width; idx += bw)
Sarah Parkerd25ef8c2017-10-06 12:17:30 -07002982 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size, init_depth,
2983 idy, idx, r);
Yue Chend6bdd462017-07-19 16:05:43 -07002984#if CONFIG_RECT_TX_EXT
2985 if (is_quarter_tx_allowed(xd, mbmi, inter_block) &&
2986 mbmi->tx_size == max_tx_size) {
2987 int quarter_tx;
2988
2989 if (quarter_txsize_lookup[bsize] != max_tx_size) {
Thomas Daviese3f69782017-10-03 10:43:17 +01002990#if CONFIG_NEW_MULTISYMBOL
2991 quarter_tx =
2992 aom_read_symbol(r, cm->fc->quarter_tx_size_cdf, 2, ACCT_STR);
2993#else
Yue Chend6bdd462017-07-19 16:05:43 -07002994 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
2995 if (xd->counts) ++xd->counts->quarter_tx_size[quarter_tx];
Thomas Daviese3f69782017-10-03 10:43:17 +01002996#endif
Yue Chend6bdd462017-07-19 16:05:43 -07002997 } else {
2998 quarter_tx = 1;
2999 }
3000 if (quarter_tx) {
3001 mbmi->tx_size = quarter_txsize_lookup[bsize];
3002 for (idy = 0; idy < tx_size_high_unit[max_tx_size] / 2; ++idy)
3003 for (idx = 0; idx < tx_size_wide_unit[max_tx_size] / 2; ++idx)
3004 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
3005 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
3006 }
3007 }
3008#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003009 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -08003010 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003011
3012 if (inter_block) {
Jingning Han9ca05b72017-01-03 14:41:36 -08003013 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
3014 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015 int idx, idy;
3016 for (idy = 0; idy < height; ++idy)
3017 for (idx = 0; idx < width; ++idx)
3018 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
3019 }
Jingning Hane67b38a2016-11-04 10:30:00 -07003020 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han1b1dc932016-11-09 10:55:30 -08003021 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022 }
3023#else
Urvang Joshifeb925f2016-12-05 10:37:29 -08003024 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003025#endif // CONFIG_VAR_TX
3026#if CONFIG_SUPERTX
3027 }
3028#if CONFIG_VAR_TX
3029 else if (inter_block) {
3030 const int width = num_4x4_blocks_wide_lookup[bsize];
3031 const int height = num_4x4_blocks_high_lookup[bsize];
3032 int idx, idy;
3033 xd->mi[0]->mbmi.tx_size = xd->supertx_size;
3034 for (idy = 0; idy < height; ++idy)
3035 for (idx = 0; idx < width; ++idx)
3036 xd->mi[0]->mbmi.inter_tx_size[idy >> 1][idx >> 1] = xd->supertx_size;
3037 }
3038#endif // CONFIG_VAR_TX
3039#endif // CONFIG_SUPERTX
3040
3041 if (inter_block)
3042 read_inter_block_mode_info(pbi, xd,
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02003043#if CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 mi, mi_row, mi_col, r, supertx_enabled);
3045#else
3046 mi, mi_row, mi_col, r);
Yue Chencb60b182016-10-13 15:18:22 -07003047#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048 else
Jingning Han36fe3202017-02-20 22:31:49 -08003049 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003050
Angie Chiangcd9b03f2017-04-16 13:37:13 -07003051#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07003052 av1_read_tx_type(cm, xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07003054 supertx_enabled,
Nathan E. Egge93878c42016-05-03 10:01:32 -04003055#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07003056 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07003057#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07003058}
3059
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003060static void av1_intra_copy_frame_mvs(AV1_COMMON *const cm, int mi_row,
3061 int mi_col, int x_mis, int y_mis) {
Jingning Hanc7d198e2017-10-12 16:11:06 -07003062#if CONFIG_TMV || CONFIG_MFMV
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003063 const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_cols, 1);
Yunqing Wangc99a5642017-10-11 13:48:22 -07003064 MV_REF *frame_mvs =
3065 cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003066 x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
3067 y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
3068#else
3069 const int frame_mvs_stride = cm->mi_cols;
3070 MV_REF *frame_mvs = cm->cur_frame->mvs +
3071 (mi_row & 0xfffe) * frame_mvs_stride + (mi_col & 0xfffe);
3072 x_mis = AOMMAX(x_mis, 2);
3073 y_mis = AOMMAX(y_mis, 2);
3074#endif // CONFIG_TMV
3075 int w, h;
3076
3077 for (h = 0; h < y_mis; h++) {
3078 MV_REF *const frame_mv = frame_mvs + h * frame_mvs_stride;
3079 for (w = 0; w < x_mis; w++) {
3080 MV_REF *const mv = frame_mv + w;
3081 mv->ref_frame[0] = NONE_FRAME;
3082 mv->ref_frame[1] = NONE_FRAME;
3083 }
3084 }
3085}
3086
Yaowu Xuf883b422016-08-30 14:01:10 -07003087void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07003089 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003090#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07003091 int mi_row, int mi_col, aom_reader *r, int x_mis,
3092 int y_mis) {
3093 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003094 MODE_INFO *const mi = xd->mi[0];
Alex Converse28744302017-04-13 14:46:22 -07003095#if CONFIG_INTRABC
3096 mi->mbmi.use_intrabc = 0;
3097#endif // CONFIG_INTRABC
3098
Yaowu Xuc27fc142016-08-22 16:08:15 -07003099 if (frame_is_intra_only(cm)) {
3100 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003101 av1_intra_copy_frame_mvs(cm, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003102 } else {
3103 read_inter_frame_mode_info(pbi, xd,
3104#if CONFIG_SUPERTX
3105 supertx_enabled,
3106#endif // CONFIG_SUPERTX
3107 mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003108 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003109 }
3110}