blob: 5bd870a54c9431da006e6327e73af2f823dcd2a7 [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#include "av1/common/warped_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/decoder/decodeframe.h"
Jingning Han1aab8182016-06-03 11:09:06 -070028#include "av1/decoder/decodemv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
Yaowu Xuf883b422016-08-30 14:01:10 -070030#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031
Michael Bebenita6048d052016-08-25 14:40:54 -070032#define ACCT_STR __func__
Zoe Liu85b66462017-04-20 14:28:19 -070033
Di Chen56586622017-06-09 13:49:44 -070034#define DEC_MISMATCH_DEBUG 0
Zoe Liu85b66462017-04-20 14:28:19 -070035
Thomas9ac55082016-09-23 18:04:17 +010036static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
Hui Su814f41e2017-10-02 12:21:24 -070037 return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -040038}
Yaowu Xuc27fc142016-08-22 16:08:15 -070039
Thomas Daviesf6936102016-09-05 16:51:31 +010040static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
41 MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
42 FRAME_COUNTS *counts = xd->counts;
43 int sign, abs, reduced_delta_qindex = 0;
44 BLOCK_SIZE bsize = mbmi->sb_type;
45 const int b_col = mi_col & MAX_MIB_MASK;
46 const int b_row = mi_row & MAX_MIB_MASK;
47 const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000048 int rem_bits, thr;
49 int i, smallval;
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000050 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
51 (void)cm;
Thomas Daviesf6936102016-09-05 16:51:31 +010052
Alex Converse68abef82017-03-23 14:50:33 -070053 if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000054 abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000055 smallval = (abs < DELTA_Q_SMALL);
56 if (counts) {
57 for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
58 if (smallval) counts->delta_q[abs][0]++;
59 }
60
61 if (!smallval) {
Thomas Davies3b93e8e2017-09-20 09:59:07 +010062 rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
Thomas Daviesf6936102016-09-05 16:51:31 +010063 thr = (1 << rem_bits) + 1;
64 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
65 }
66
67 if (abs) {
68 sign = aom_read_bit(r, ACCT_STR);
69 } else {
70 sign = 1;
71 }
72
73 reduced_delta_qindex = sign ? -abs : abs;
74 }
75 return reduced_delta_qindex;
76}
Fangwen Fu231fe422017-04-24 17:52:29 -070077#if CONFIG_EXT_DELTA_Q
78static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
Cheng Chena97394f2017-09-27 15:05:14 -070079#if CONFIG_LOOPFILTER_LEVEL
80 int lf_id,
81#endif
Fangwen Fu231fe422017-04-24 17:52:29 -070082 MB_MODE_INFO *const mbmi, int mi_col,
83 int mi_row) {
84 FRAME_COUNTS *counts = xd->counts;
85 int sign, abs, reduced_delta_lflevel = 0;
86 BLOCK_SIZE bsize = mbmi->sb_type;
87 const int b_col = mi_col & MAX_MIB_MASK;
88 const int b_row = mi_row & MAX_MIB_MASK;
89 const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
90 int rem_bits, thr;
91 int i, smallval;
Fangwen Fu231fe422017-04-24 17:52:29 -070092 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
93 (void)cm;
Fangwen Fu231fe422017-04-24 17:52:29 -070094
Debargha Mukherjeee30159c2017-10-08 12:04:43 -070095 if ((bsize != cm->sb_size || mbmi->skip == 0) && read_delta_lf_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -070096#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -070097 if (cm->delta_lf_multi) {
98 assert(lf_id >= 0 && lf_id < FRAME_LF_COUNT);
99 abs = aom_read_symbol(r, ec_ctx->delta_lf_multi_cdf[lf_id],
100 DELTA_LF_PROBS + 1, ACCT_STR);
101 } else {
102 abs = aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1,
103 ACCT_STR);
104 }
Cheng Chena97394f2017-09-27 15:05:14 -0700105#else
Fangwen Fu231fe422017-04-24 17:52:29 -0700106 abs =
107 aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
Cheng Chena97394f2017-09-27 15:05:14 -0700108#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700109 smallval = (abs < DELTA_LF_SMALL);
110 if (counts) {
Cheng Chena97394f2017-09-27 15:05:14 -0700111#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -0700112 if (cm->delta_lf_multi) {
113 for (i = 0; i < abs; ++i) counts->delta_lf_multi[lf_id][i][1]++;
114 if (smallval) counts->delta_lf_multi[lf_id][abs][0]++;
115 } else {
116 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
117 if (smallval) counts->delta_lf[abs][0]++;
118 }
Cheng Chena97394f2017-09-27 15:05:14 -0700119#else
Fangwen Fu231fe422017-04-24 17:52:29 -0700120 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
121 if (smallval) counts->delta_lf[abs][0]++;
Cheng Chena97394f2017-09-27 15:05:14 -0700122#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700123 }
124 if (!smallval) {
Thomas Davies3b93e8e2017-09-20 09:59:07 +0100125 rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
Fangwen Fu231fe422017-04-24 17:52:29 -0700126 thr = (1 << rem_bits) + 1;
127 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
128 }
129
130 if (abs) {
131 sign = aom_read_bit(r, ACCT_STR);
132 } else {
133 sign = 1;
134 }
135
136 reduced_delta_lflevel = sign ? -abs : abs;
137 }
138 return reduced_delta_lflevel;
139}
140#endif
Thomas Daviesf6936102016-09-05 16:51:31 +0100141
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400142static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
Hui Suaa2965e2017-10-05 15:59:12 -0700143 aom_reader *r,
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400144 PREDICTION_MODE y_mode) {
145 const UV_PREDICTION_MODE uv_mode =
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400146#if CONFIG_CFL
147 aom_read_symbol(r, ec_ctx->uv_mode_cdf[y_mode], UV_INTRA_MODES, ACCT_STR);
148#else
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000149 read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400150#endif // CONFIG_CFL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 return uv_mode;
152}
153
Luc Trudeauf5334002017-04-25 12:21:26 -0400154#if CONFIG_CFL
David Michael Barr23198662017-06-19 23:19:48 +0900155static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
David Michael Barrf6eaa152017-07-19 19:42:28 +0900156 int *signs_out) {
157 const int joint_sign =
158 aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
159 int idx = 0;
160 // Magnitudes are only coded for nonzero values
161 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
162 aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
163 idx = aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
164 << CFL_ALPHABET_SIZE_LOG2;
165 }
166 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
167 aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
168 idx += aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
169 }
170 *signs_out = joint_sign;
171 return idx;
Luc Trudeauf5334002017-04-25 12:21:26 -0400172}
173#endif
174
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200175#if CONFIG_INTERINTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700176static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
177 aom_reader *r, int size_group) {
Thomas Davies299ff042017-06-27 13:41:59 +0100178 (void)cm;
179 const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
180 r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
181 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 FRAME_COUNTS *counts = xd->counts;
183 if (counts) ++counts->interintra_mode[size_group][ii_mode];
184 return ii_mode;
185}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200186#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187
Thomas Davies1de6c882017-01-11 17:47:49 +0000188static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700189 aom_reader *r, int16_t ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190 FRAME_COUNTS *counts = xd->counts;
191 int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100192 int is_newmv, is_zeromv, is_refmv;
193#if CONFIG_NEW_MULTISYMBOL
194 is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
195#else
196 is_newmv = aom_read(r, ec_ctx->newmv_prob[mode_ctx], ACCT_STR) == 0;
197#endif
Thomas Davies149eda52017-06-12 18:11:55 +0100198 if (is_newmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 if (counts) ++counts->newmv_mode[mode_ctx][0];
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700200 return NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201 }
202 if (counts) ++counts->newmv_mode[mode_ctx][1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100205#if CONFIG_NEW_MULTISYMBOL
206 is_zeromv =
207 aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
208#else
209 is_zeromv = aom_read(r, ec_ctx->zeromv_prob[mode_ctx], ACCT_STR) == 0;
210#endif
211 if (is_zeromv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700212 if (counts) ++counts->zeromv_mode[mode_ctx][0];
213 return ZEROMV;
214 }
215 if (counts) ++counts->zeromv_mode[mode_ctx][1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700216 mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
218 if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
219 if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
Thomas Davies149eda52017-06-12 18:11:55 +0100220#if CONFIG_NEW_MULTISYMBOL
221 is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
222#else
223 is_refmv = aom_read(r, ec_ctx->refmv_prob[mode_ctx], ACCT_STR) == 0;
224#endif
Thomas Davies149eda52017-06-12 18:11:55 +0100225 if (is_refmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 if (counts) ++counts->refmv_mode[mode_ctx][0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 return NEARESTMV;
228 } else {
229 if (counts) ++counts->refmv_mode[mode_ctx][1];
230 return NEARMV;
231 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700232 // Invalid prediction mode.
233 assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234}
235
Thomas Davies149eda52017-06-12 18:11:55 +0100236static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700237 MB_MODE_INFO *mbmi, aom_reader *r) {
238 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700239 mbmi->ref_mv_idx = 0;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +0200240 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700241#if CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700242 || mbmi->mode == SR_NEW_NEWMV
Zoe Liu85b66462017-04-20 14:28:19 -0700243#endif // CONFIG_COMPOUND_SINGLEREF
Yushin Cho127c5832017-07-28 16:39:04 -0700244 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 int idx;
246 for (idx = 0; idx < 2; ++idx) {
247 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100249#if CONFIG_NEW_MULTISYMBOL
250 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
251#else
252 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
253#endif
254 mbmi->ref_mv_idx = idx + drl_idx;
255 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
256 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 }
258 }
259 }
David Barker3dfba992017-04-03 16:10:09 +0100260 if (have_nearmv_in_inter_mode(mbmi->mode)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261 int idx;
262 // Offset the NEARESTMV mode.
263 // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
264 // mode is factored in.
265 for (idx = 1; idx < 3; ++idx) {
266 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700267 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100268#if CONFIG_NEW_MULTISYMBOL
269 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
270#else
271 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
272#endif
273 mbmi->ref_mv_idx = idx + drl_idx - 1;
274 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
275 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 }
277 }
278 }
279}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280
Yaowu Xub24e1152016-10-31 16:28:32 -0700281static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -0800282 MODE_INFO *mi, aom_reader *r) {
283 MB_MODE_INFO *mbmi = &mi->mbmi;
Sebastien Alaiwan1f56b8e2017-10-31 17:37:16 +0100284#if CONFIG_NEW_MULTISYMBOL || CONFIG_NCOBMC_ADAPT_WEIGHT
Thomas Davies04e5aa72017-06-28 14:36:39 +0100285 (void)cm;
286#endif
287
Sebastien Alaiwan48795802017-10-30 12:07:13 +0100288 const MOTION_MODE last_motion_mode_allowed =
Sebastien Alaiwan1f56b8e2017-10-31 17:37:16 +0100289 motion_mode_allowed(0, xd->global_motion, xd, mi);
Yue Chen69f18e12016-09-08 14:48:15 -0700290 int motion_mode;
291 FRAME_COUNTS *counts = xd->counts;
Yaowu Xub24e1152016-10-31 16:28:32 -0700292
Yue Chen69f18e12016-09-08 14:48:15 -0700293 if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700294#if CONFIG_NCOBMC_ADAPT_WEIGHT
295 if (last_motion_mode_allowed == NCOBMC_ADAPT_WEIGHT) {
296 motion_mode = aom_read_symbol(r, xd->tile_ctx->ncobmc_cdf[mbmi->sb_type],
297 OBMC_FAMILY_MODES, ACCT_STR);
298 if (counts) ++counts->ncobmc[mbmi->sb_type][motion_mode];
299 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
300 } else if (last_motion_mode_allowed == OBMC_CAUSAL) {
301 motion_mode =
302 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
303 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
304 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
305 } else {
306#else
Yue Chen69f18e12016-09-08 14:48:15 -0700307 if (last_motion_mode_allowed == OBMC_CAUSAL) {
Thomas Daviesd9b57262017-06-27 17:43:25 +0100308#if CONFIG_NEW_MULTISYMBOL
309 motion_mode =
310 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
311#else
Yue Chen69f18e12016-09-08 14:48:15 -0700312 motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
Thomas Daviesd9b57262017-06-27 17:43:25 +0100313#endif
Yue Chen69f18e12016-09-08 14:48:15 -0700314 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
315 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
316 } else {
Wei-Ting Lin07ed3ab2017-08-28 17:50:25 -0700317#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yaowu Xub24e1152016-10-31 16:28:32 -0700318 motion_mode =
Thomas Davies04e5aa72017-06-28 14:36:39 +0100319 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
320 MOTION_MODES, ACCT_STR);
Yaowu Xub24e1152016-10-31 16:28:32 -0700321 if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
322 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yaowu Xub24e1152016-10-31 16:28:32 -0700323 }
324}
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700325
326#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -0700327static void read_ncobmc_mode(MACROBLOCKD *xd, MODE_INFO *mi,
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700328 NCOBMC_MODE ncobmc_mode[2], aom_reader *r) {
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700329 MB_MODE_INFO *mbmi = &mi->mbmi;
330 FRAME_COUNTS *counts = xd->counts;
331 ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];
Wei-Ting Lin77c41182017-07-12 15:58:35 -0700332 if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) return;
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700333
Wei-Ting Linca710d62017-07-13 11:41:02 -0700334 ncobmc_mode[0] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
335 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700336 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0]];
337
338 if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
Wei-Ting Linca710d62017-07-13 11:41:02 -0700339 ncobmc_mode[1] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
340 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700341 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1]];
342 }
343}
Wei-Ting Lin7daf0422017-08-03 11:42:37 -0700344#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yaowu Xub24e1152016-10-31 16:28:32 -0700345
Yaowu Xuf883b422016-08-30 14:01:10 -0700346static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
347 aom_reader *r, int16_t ctx) {
Thomas Davies8c08a332017-06-26 17:30:34 +0100348 (void)cm;
Zoe Liuf704a1c2017-10-02 16:55:59 -0700349#if CONFIG_EXT_SKIP
350 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
351 const int mode =
352 xd->mi[0]->mbmi.skip_mode
353 ? (NEAREST_NEARESTMV - NEAREST_NEARESTMV)
354 : aom_read_symbol(r, ec_ctx->inter_compound_mode_cdf[ctx],
355 INTER_COMPOUND_MODES, ACCT_STR);
356 if (xd->mi[0]->mbmi.skip_mode)
357 update_cdf(ec_ctx->inter_compound_mode_cdf[ctx], mode,
358 INTER_COMPOUND_MODES);
359#else
Thomas Davies8c08a332017-06-26 17:30:34 +0100360 const int mode =
361 aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
362 INTER_COMPOUND_MODES, ACCT_STR);
Zoe Liuf704a1c2017-10-02 16:55:59 -0700363#endif // CONFIG_EXT_SKIP
364
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700366 if (counts) ++counts->inter_compound_mode[ctx][mode];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367 assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
368 return NEAREST_NEARESTMV + mode;
369}
Zoe Liu85b66462017-04-20 14:28:19 -0700370
371#if CONFIG_COMPOUND_SINGLEREF
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100372static PREDICTION_MODE read_inter_singleref_comp_mode(MACROBLOCKD *xd,
Zoe Liu85b66462017-04-20 14:28:19 -0700373 aom_reader *r,
374 int16_t ctx) {
375 const int mode =
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100376 aom_read_symbol(r, xd->tile_ctx->inter_singleref_comp_mode_cdf[ctx],
377 INTER_SINGLEREF_COMP_MODES, ACCT_STR);
Zoe Liu85b66462017-04-20 14:28:19 -0700378 FRAME_COUNTS *counts = xd->counts;
379
380 if (counts) ++counts->inter_singleref_comp_mode[ctx][mode];
381
382 assert(is_inter_singleref_comp_mode(SR_NEAREST_NEARMV + mode));
383 return SR_NEAREST_NEARMV + mode;
384}
385#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386
Thomas9ac55082016-09-23 18:04:17 +0100387static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700388 return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389}
390
Yaowu Xuf883b422016-08-30 14:01:10 -0700391static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392 MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700393 TX_SIZE tx_size, int depth, int blk_row,
394 int blk_col, aom_reader *r) {
Thomas Davies985bfc32017-06-27 16:51:26 +0100395#if CONFIG_NEW_MULTISYMBOL
396 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
397 (void)cm;
398#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399 int is_split = 0;
400 const int tx_row = blk_row >> 1;
401 const int tx_col = blk_col >> 1;
Jingning Hanf64062f2016-11-02 16:22:18 -0700402 const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
403 const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
Jingning Han331662e2017-05-30 17:03:32 -0700404 int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
405 xd->left_txfm_context + blk_row,
Jingning Hanc8b89362016-11-01 10:28:53 -0700406 mbmi->sb_type, tx_size);
clang-format67948d32016-09-07 22:40:40 -0700407 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408 [MAX_MIB_SIZE] =
409 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700410 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
David Barker16c64e32017-08-23 16:54:59 +0100411 assert(tx_size > TX_4X4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700412
Jingning Han571189c2016-10-24 10:38:43 -0700413 if (depth == MAX_VARTX_DEPTH) {
Jingning Han94d5bfc2016-10-21 10:14:36 -0700414 int idx, idy;
415 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700416 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
417 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han94d5bfc2016-10-21 10:14:36 -0700418 inter_tx_size[idy][idx] = tx_size;
419 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700420 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Jingning Han331662e2017-05-30 17:03:32 -0700421 txfm_partition_update(xd->above_txfm_context + blk_col,
422 xd->left_txfm_context + blk_row, tx_size, tx_size);
Jingning Han94d5bfc2016-10-21 10:14:36 -0700423 return;
424 }
425
Thomas Davies985bfc32017-06-27 16:51:26 +0100426#if CONFIG_NEW_MULTISYMBOL
427 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
428#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700429 is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
Thomas Davies985bfc32017-06-27 16:51:26 +0100430#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431
432 if (is_split) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700433 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
434 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700435 int i;
436
437 if (counts) ++counts->txfm_partition[ctx][1];
438
David Barker16c64e32017-08-23 16:54:59 +0100439 if (sub_txs == TX_4X4) {
Jingning Han9ca05b72017-01-03 14:41:36 -0800440 int idx, idy;
Jingning Hanab9ecba2017-01-13 09:11:58 -0800441 inter_tx_size[0][0] = sub_txs;
Jingning Han9ca05b72017-01-03 14:41:36 -0800442 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
443 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han581d1692017-01-05 16:03:54 -0800444 inter_tx_size[idy][idx] = inter_tx_size[0][0];
Jingning Hanab9ecba2017-01-13 09:11:58 -0800445 mbmi->tx_size = sub_txs;
Jingning Hane67b38a2016-11-04 10:30:00 -0700446 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han331662e2017-05-30 17:03:32 -0700447 txfm_partition_update(xd->above_txfm_context + blk_col,
448 xd->left_txfm_context + blk_row, sub_txs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449 return;
450 }
451
452 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 for (i = 0; i < 4; ++i) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700454 int offsetr = blk_row + (i >> 1) * bsl;
455 int offsetc = blk_col + (i & 0x01) * bsl;
456 read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700457 offsetc, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700458 }
459 } else {
460 int idx, idy;
461 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700462 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
463 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464 inter_tx_size[idy][idx] = tx_size;
465 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700466 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700467 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700468 txfm_partition_update(xd->above_txfm_context + blk_col,
469 xd->left_txfm_context + blk_row, tx_size, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470 }
471}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472
Yaowu Xuf883b422016-08-30 14:01:10 -0700473static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
Urvang Joshiab8840e2017-10-06 16:38:24 -0700474 int32_t tx_size_cat, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475 const int ctx = get_tx_size_context(xd);
Thomas Davies15580c52017-03-09 13:53:42 +0000476 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
477 (void)cm;
Thomas Davies15580c52017-03-09 13:53:42 +0000478
Nathan E. Egge476c63c2017-05-18 18:35:16 -0400479 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
480 tx_size_cat + 2, ACCT_STR);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800481 const TX_SIZE tx_size = depth_to_tx_size(depth);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800482 assert(!is_rect_tx(tx_size));
Jingning Han4e1737a2016-10-25 16:05:02 -0700483 return tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484}
485
Urvang Joshifeb925f2016-12-05 10:37:29 -0800486static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
487 int allow_select_inter, aom_reader *r) {
488 const TX_MODE tx_mode = cm->tx_mode;
489 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Rupert Swarbrickfcff0b22017-10-05 09:26:04 +0100491
492 if (block_signals_txsize(bsize)) {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800493 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
494 const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
495 : intra_tx_size_cat_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496 const TX_SIZE coded_tx_size =
Urvang Joshifeb925f2016-12-05 10:37:29 -0800497 read_selected_tx_size(cm, xd, tx_size_cat, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498 if (coded_tx_size > max_txsize_lookup[bsize]) {
499 assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
Yue Chend6bdd462017-07-19 16:05:43 -0700500#if CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700501 if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
Yue Chend6bdd462017-07-19 16:05:43 -0700502 int quarter_tx;
Yue Chen56e226e2017-05-02 16:21:40 -0700503
Yue Chend6bdd462017-07-19 16:05:43 -0700504 if (quarter_txsize_lookup[bsize] != max_txsize_lookup[bsize]) {
Thomas Daviese3f69782017-10-03 10:43:17 +0100505#if CONFIG_NEW_MULTISYMBOL
506 quarter_tx =
507 aom_read_symbol(r, cm->fc->quarter_tx_size_cdf, 2, ACCT_STR);
508#else
Yue Chend6bdd462017-07-19 16:05:43 -0700509 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
510 FRAME_COUNTS *counts = xd->counts;
Yue Chend6bdd462017-07-19 16:05:43 -0700511 if (counts) ++counts->quarter_tx_size[quarter_tx];
Thomas Daviese3f69782017-10-03 10:43:17 +0100512#endif
Yue Chend6bdd462017-07-19 16:05:43 -0700513 } else {
514 quarter_tx = 1;
515 }
Yue Chen56e226e2017-05-02 16:21:40 -0700516 return quarter_tx ? quarter_txsize_lookup[bsize]
517 : max_txsize_rect_lookup[bsize];
518 }
Yue Chend6bdd462017-07-19 16:05:43 -0700519#endif // CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700520
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521 return max_txsize_rect_lookup[bsize];
522 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523 return coded_tx_size;
524 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800525 return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526 }
527 } else {
Debargha Mukherjee11812962017-10-29 15:16:14 -0700528#if CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
530 return max_txsize_rect_lookup[bsize];
531#else
532 return TX_4X4;
Debargha Mukherjee11812962017-10-29 15:16:14 -0700533#endif // CONFIG_EXT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534 }
535}
536
Yaowu Xuf883b422016-08-30 14:01:10 -0700537static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 int mi_offset, int x_mis, int y_mis) {
539 int x, y, segment_id = INT_MAX;
540
541 for (y = 0; y < y_mis; y++)
542 for (x = 0; x < x_mis; x++)
543 segment_id =
Yaowu Xuf883b422016-08-30 14:01:10 -0700544 AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545
546 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
547 return segment_id;
548}
549
Yaowu Xuf883b422016-08-30 14:01:10 -0700550static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 int segment_id) {
552 int x, y;
553
554 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
555
556 for (y = 0; y < y_mis; y++)
557 for (x = 0; x < x_mis; x++)
558 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
559}
560
Yaowu Xuf883b422016-08-30 14:01:10 -0700561static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 int mi_offset, int x_mis, int y_mis,
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 struct segmentation *const seg = &cm->seg;
565 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100566 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100567 struct segmentation_probs *const segp = &ec_ctx->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568 int segment_id;
569
570 if (!seg->enabled) return 0; // Default for disabled segmentation
571
572 assert(seg->update_map && !seg->temporal_update);
573
574 segment_id = read_segment_id(r, segp);
575 if (counts) ++counts->seg.tree_total[segment_id];
576 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
577 return segment_id;
578}
579
Yaowu Xuf883b422016-08-30 14:01:10 -0700580static void copy_segment_id(const AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 const uint8_t *last_segment_ids,
582 uint8_t *current_segment_ids, int mi_offset,
583 int x_mis, int y_mis) {
584 int x, y;
585
586 for (y = 0; y < y_mis; y++)
587 for (x = 0; x < x_mis; x++)
588 current_segment_ids[mi_offset + y * cm->mi_cols + x] =
589 last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
590 : 0;
591}
592
Yaowu Xuf883b422016-08-30 14:01:10 -0700593static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
594 int mi_row, int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595 struct segmentation *const seg = &cm->seg;
596 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100597 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100598 struct segmentation_probs *const segp = &ec_ctx->seg;
599
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
601 int predicted_segment_id, segment_id;
602 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Hanc709e1f2016-12-06 14:48:09 -0800603 const int bw = mi_size_wide[mbmi->sb_type];
604 const int bh = mi_size_high[mbmi->sb_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605
606 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -0700607 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
608 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609
610 if (!seg->enabled) return 0; // Default for disabled segmentation
611
612 predicted_segment_id = cm->last_frame_seg_map
613 ? dec_get_segment_id(cm, cm->last_frame_seg_map,
614 mi_offset, x_mis, y_mis)
615 : 0;
616
617 if (!seg->update_map) {
618 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
619 mi_offset, x_mis, y_mis);
620 return predicted_segment_id;
621 }
622
623 if (seg->temporal_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 const int ctx = av1_get_pred_context_seg_id(xd);
Thomas Davies00021352017-07-11 16:07:55 +0100625#if CONFIG_NEW_MULTISYMBOL
626 aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
627 mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
628#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700629 const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700630 mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
Thomas Davies00021352017-07-11 16:07:55 +0100631#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
633 if (mbmi->seg_id_predicted) {
634 segment_id = predicted_segment_id;
635 } else {
636 segment_id = read_segment_id(r, segp);
637 if (counts) ++counts->seg.tree_mispred[segment_id];
638 }
639 } else {
640 segment_id = read_segment_id(r, segp);
641 if (counts) ++counts->seg.tree_total[segment_id];
642 }
643 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
644 return segment_id;
645}
646
Zoe Liuf704a1c2017-10-02 16:55:59 -0700647#if CONFIG_EXT_SKIP
648static int read_skip_mode(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
649 aom_reader *r) {
650 if (!cm->is_skip_mode_allowed) return 0;
651
652 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
653 // TODO(zoeliu): To revisit the handling of this scenario.
654 return 0;
655 } else {
656 const int ctx = av1_get_skip_mode_context(xd);
657#if CONFIG_NEW_MULTISYMBOL
658 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
659 const int skip_mode =
660 aom_read_symbol(r, ec_ctx->skip_mode_cdfs[ctx], 2, ACCT_STR);
661#else
662 const int skip_mode = aom_read(r, cm->fc->skip_mode_probs[ctx], ACCT_STR);
663#endif // CONFIG_NEW_MULTISYMBOL
664 FRAME_COUNTS *counts = xd->counts;
665 if (counts) ++counts->skip_mode[ctx][skip_mode];
666
667 // TODO(zoeliu): To handle:
668 // if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type))
669 return skip_mode;
670 }
671}
672#endif // CONFIG_EXT_SKIP
673
Yaowu Xuf883b422016-08-30 14:01:10 -0700674static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
675 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
677 return 1;
678 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 const int ctx = av1_get_skip_context(xd);
Thomas Davies61e3e372017-04-04 16:10:23 +0100680#if CONFIG_NEW_MULTISYMBOL
681 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
682 const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
683#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700684 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Thomas Davies61e3e372017-04-04 16:10:23 +0100685#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686 FRAME_COUNTS *counts = xd->counts;
687 if (counts) ++counts->skip[ctx][skip];
688 return skip;
689 }
690}
691
hui su33567b22017-04-30 16:40:19 -0700692#if CONFIG_PALETTE_DELTA_ENCODING
Hui Su4494ae42017-10-04 12:22:44 -0700693// Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
694// and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
695// one single sorted list(colors[...]).
696static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
697 int n_colors, int n_cached_colors) {
698 if (n_cached_colors == 0) return;
699 int cache_idx = 0, trans_idx = n_cached_colors;
700 for (int i = 0; i < n_colors; ++i) {
701 if (cache_idx < n_cached_colors &&
702 (trans_idx >= n_colors ||
703 cached_colors[cache_idx] <= colors[trans_idx])) {
704 colors[i] = cached_colors[cache_idx++];
705 } else {
706 assert(trans_idx < n_colors);
707 colors[i] = colors[trans_idx++];
708 }
709 }
hui su33567b22017-04-30 16:40:19 -0700710}
hui su33567b22017-04-30 16:40:19 -0700711
712static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
713 PALETTE_MODE_INFO *const pmi, aom_reader *r) {
714 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su4494ae42017-10-04 12:22:44 -0700715 uint16_t cached_colors[PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700716 const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
hui su33567b22017-04-30 16:40:19 -0700717 const int n = pmi->palette_size[0];
718 int idx = 0;
719 for (int i = 0; i < n_cache && idx < n; ++i)
Hui Su4494ae42017-10-04 12:22:44 -0700720 if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
hui su33567b22017-04-30 16:40:19 -0700721 if (idx < n) {
Hui Su4494ae42017-10-04 12:22:44 -0700722 const int n_cached_colors = idx;
hui su33567b22017-04-30 16:40:19 -0700723 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
724 if (idx < n) {
725 const int min_bits = bit_depth - 3;
726 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
727 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
728 for (; idx < n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100729 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700730 const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100731 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
732 0, (1 << bit_depth) - 1);
733 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700734 bits = AOMMIN(bits, av1_ceil_log2(range));
735 }
736 }
Hui Su4494ae42017-10-04 12:22:44 -0700737 merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
738 } else {
739 memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
hui su33567b22017-04-30 16:40:19 -0700740 }
hui su33567b22017-04-30 16:40:19 -0700741}
742
743static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
744 PALETTE_MODE_INFO *const pmi,
745 aom_reader *r) {
746 const int n = pmi->palette_size[1];
747 // U channel colors.
748 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su4494ae42017-10-04 12:22:44 -0700749 uint16_t cached_colors[PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700750 const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
Hui Su4494ae42017-10-04 12:22:44 -0700751 int idx = 0;
752 for (int i = 0; i < n_cache && idx < n; ++i)
753 if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
754 if (idx < n) {
755 const int n_cached_colors = idx;
756 idx += PALETTE_MAX_SIZE;
hui su33567b22017-04-30 16:40:19 -0700757 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
758 if (idx < PALETTE_MAX_SIZE + n) {
759 const int min_bits = bit_depth - 3;
760 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
761 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
762 for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100763 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700764 const int delta = aom_read_literal(r, bits, ACCT_STR);
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100765 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
766 0, (1 << bit_depth) - 1);
767 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700768 bits = AOMMIN(bits, av1_ceil_log2(range));
769 }
770 }
Hui Su4494ae42017-10-04 12:22:44 -0700771 merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
772 n_cached_colors);
773 } else {
774 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
775 n * sizeof(cached_colors[0]));
hui su33567b22017-04-30 16:40:19 -0700776 }
hui su33567b22017-04-30 16:40:19 -0700777
778 // V channel colors.
779 if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
780 const int min_bits_v = bit_depth - 4;
781 const int max_val = 1 << bit_depth;
782 int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
783 pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
784 aom_read_literal(r, bit_depth, ACCT_STR);
785 for (int i = 1; i < n; ++i) {
786 int delta = aom_read_literal(r, bits, ACCT_STR);
787 if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
788 int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
789 if (val < 0) val += max_val;
790 if (val >= max_val) val -= max_val;
791 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
792 }
793 } else {
794 for (int i = 0; i < n; ++i) {
795 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
796 aom_read_literal(r, bit_depth, ACCT_STR);
797 }
798 }
799}
800#endif // CONFIG_PALETTE_DELTA_ENCODING
801
Yaowu Xuf883b422016-08-30 14:01:10 -0700802static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
803 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 MODE_INFO *const mi = xd->mi[0];
805 MB_MODE_INFO *const mbmi = &mi->mbmi;
806 const MODE_INFO *const above_mi = xd->above_mi;
807 const MODE_INFO *const left_mi = xd->left_mi;
808 const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
810
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100811 assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST);
812 const int block_palette_idx = bsize - BLOCK_8X8;
Thomas Davies59f92312017-08-23 00:33:12 +0100813 int modev;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100814
Yaowu Xuc27fc142016-08-22 16:08:15 -0700815 if (mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800816 int palette_y_mode_ctx = 0;
hui su40b9e7f2017-07-13 18:15:56 -0700817 if (above_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800818 palette_y_mode_ctx +=
819 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700820 }
821 if (left_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800822 palette_y_mode_ctx +=
823 (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700824 }
Thomas Davies59f92312017-08-23 00:33:12 +0100825#if CONFIG_NEW_MULTISYMBOL
826 modev = aom_read_symbol(
827 r,
828 xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
829 2, ACCT_STR);
830#else
831 modev = aom_read(
832 r,
833 av1_default_palette_y_mode_prob[block_palette_idx][palette_y_mode_ctx],
834 ACCT_STR);
835#endif
836 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 pmi->palette_size[0] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100838 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100839 xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100840 PALETTE_SIZES, ACCT_STR) +
841 2;
hui sud13c24a2017-04-07 16:13:07 -0700842#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700843 read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700844#else
hui su40b9e7f2017-07-13 18:15:56 -0700845 for (int i = 0; i < pmi->palette_size[0]; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700846 pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui sud13c24a2017-04-07 16:13:07 -0700847#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848 }
849 }
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400850 if (mbmi->uv_mode == UV_DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800851 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
Thomas Davies59f92312017-08-23 00:33:12 +0100852#if CONFIG_NEW_MULTISYMBOL
853 modev = aom_read_symbol(
854 r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
855#else
856 modev = aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
857 ACCT_STR);
858#endif
859 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 pmi->palette_size[1] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100861 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100862 xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100863 PALETTE_SIZES, ACCT_STR) +
864 2;
hui sud13c24a2017-04-07 16:13:07 -0700865#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700866 read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700867#else
hui su40b9e7f2017-07-13 18:15:56 -0700868 for (int i = 0; i < pmi->palette_size[1]; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869 pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700870 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700872 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873 }
hui sud13c24a2017-04-07 16:13:07 -0700874#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 }
876 }
877}
878
hui su5db97432016-10-14 16:10:14 -0700879#if CONFIG_FILTER_INTRA
880static void read_filter_intra_mode_info(AV1_COMMON *const cm,
Yue Chen57b8ff62017-10-10 23:37:31 -0700881 MACROBLOCKD *const xd, 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 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700901}
hui su5db97432016-10-14 16:10:14 -0700902#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903
hui su5db97432016-10-14 16:10:14 -0700904#if CONFIG_EXT_INTRA
Joe Young3ca43bf2017-10-06 15:12:46 -0700905#if CONFIG_EXT_INTRA_MOD
906static int read_angle_delta(aom_reader *r, aom_cdf_prob *cdf) {
907 const int sym = aom_read_symbol(r, cdf, 2 * MAX_ANGLE_DELTA + 1, ACCT_STR);
908 return sym - MAX_ANGLE_DELTA;
909}
910#endif // CONFIG_EXT_INTRA_MOD
911
Hui Su259d4422017-10-13 10:08:17 -0700912static void read_intra_angle_info(MACROBLOCKD *const xd, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
914 const BLOCK_SIZE bsize = mbmi->sb_type;
Joe Young3ca43bf2017-10-06 15:12:46 -0700915#if CONFIG_EXT_INTRA_MOD
916 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
917#endif // CONFIG_EXT_INTRA_MOD
918
Joe Young830d4ce2017-05-30 17:48:13 -0700919 mbmi->angle_delta[0] = 0;
920 mbmi->angle_delta[1] = 0;
Joe Young830d4ce2017-05-30 17:48:13 -0700921 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922
hui su45dc5972016-12-08 17:42:50 -0800923 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Joe Young3ca43bf2017-10-06 15:12:46 -0700924#if CONFIG_EXT_INTRA_MOD
925 mbmi->angle_delta[0] =
926 read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED]);
927#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700929 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Joe Young3ca43bf2017-10-06 15:12:46 -0700930#endif // CONFIG_EXT_INTRA_MOD
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931 }
932
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400933 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Joe Young3ca43bf2017-10-06 15:12:46 -0700934#if CONFIG_EXT_INTRA_MOD
935 mbmi->angle_delta[1] =
936 read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->uv_mode - V_PRED]);
937#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700939 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Joe Young3ca43bf2017-10-06 15:12:46 -0700940#endif // CONFIG_EXT_INTRA_MOD
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 }
942}
943#endif // CONFIG_EXT_INTRA
944
Angie Chianga9f9a312017-04-13 16:40:43 -0700945void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700946#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700947 int blk_row, int blk_col, int block, int plane,
948 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700949#endif
950 aom_reader *r) {
951 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700952 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700953#if !CONFIG_TXK_SEL
Jingning Hane67b38a2016-11-04 10:30:00 -0700954 const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
Jingning Han243b66b2017-06-23 12:11:47 -0700955#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000956 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000957
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700958#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700959 TX_TYPE *tx_type = &mbmi->tx_type;
960#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700961 // only y plane's tx_type is transmitted
962 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700963 (void)block;
964 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << 4) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700965#endif
Lester Lu432012f2017-08-17 14:39:29 -0700966#if CONFIG_LGT_FROM_PRED
967 mbmi->use_lgt = 0;
968#endif
Angie Chianga9f9a312017-04-13 16:40:43 -0700969
Jingning Hanab7163d2016-11-04 09:46:35 -0700970 if (!FIXED_TX_TYPE) {
971#if CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -0800972 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -0800973 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
974 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -0800975 ((!cm->seg.enabled && cm->base_qindex > 0) ||
976 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
977 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -0700978 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Suddbcde22017-09-18 17:22:02 -0700979 const TxSetType tx_set_type = get_ext_tx_set_type(
980 tx_size, mbmi->sb_type, inter_block, cm->reduced_tx_set_used);
Sarah Parkere68a3e42017-02-16 14:03:24 -0800981 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
982 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -0700983 // eset == 0 should correspond to a set with only DCT_DCT and
984 // there is no need to read the tx_type
985 assert(eset != 0);
Lester Lu432012f2017-08-17 14:39:29 -0700986
987#if !CONFIG_LGT_FROM_PRED
Jingning Hanab7163d2016-11-04 09:46:35 -0700988 if (inter_block) {
Hui Suddbcde22017-09-18 17:22:02 -0700989 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -0700990 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
Hui Suddbcde22017-09-18 17:22:02 -0700991 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Jingning Hanab7163d2016-11-04 09:46:35 -0700992 } else if (ALLOW_INTRA_EXT_TX) {
Yue Chen57b8ff62017-10-10 23:37:31 -0700993#if CONFIG_FILTER_INTRA
994 PREDICTION_MODE intra_dir;
995 if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0])
996 intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
997 .filter_intra_mode[0]];
998 else
999 intra_dir = mbmi->mode;
1000 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1001 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_dir],
1002 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1003#else
Hui Suddbcde22017-09-18 17:22:02 -07001004 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -07001005 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
Hui Suddbcde22017-09-18 17:22:02 -07001006 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Yue Chen57b8ff62017-10-10 23:37:31 -07001007#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001008 }
Lester Lu432012f2017-08-17 14:39:29 -07001009#else
1010 // only signal tx_type when lgt is not allowed or not selected
1011 if (inter_block) {
1012 if (LGT_FROM_PRED_INTER) {
1013 if (is_lgt_allowed(mbmi->mode, tx_size) && !cm->reduced_tx_set_used) {
1014 mbmi->use_lgt =
1015 aom_read(r, ec_ctx->inter_lgt_prob[square_tx_size], ACCT_STR);
1016#if CONFIG_ENTROPY_STATS
1017 if (counts) ++counts->inter_lgt[square_tx_size][mbmi->use_lgt];
1018#endif // CONFIG_ENTROPY_STATS
1019 }
1020 if (!mbmi->use_lgt) {
1021 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1022 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
1023 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1024#if CONFIG_ENTROPY_STATS
1025 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
1026#endif // CONFIG_ENTROPY_STATS
1027 } else {
1028 *tx_type = DCT_DCT; // assign a dummy tx_type
1029 }
1030 } else {
1031 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1032 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
1033 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1034#if CONFIG_ENTROPY_STATS
1035 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
1036#endif // CONFIG_ENTROPY_STATS
1037 }
1038 } else if (ALLOW_INTRA_EXT_TX) {
1039 if (LGT_FROM_PRED_INTRA) {
1040 if (is_lgt_allowed(mbmi->mode, tx_size) && !cm->reduced_tx_set_used) {
1041 mbmi->use_lgt =
1042 aom_read(r, ec_ctx->intra_lgt_prob[square_tx_size][mbmi->mode],
1043 ACCT_STR);
1044#if CONFIG_ENTROPY_STATS
1045 if (counts)
1046 ++counts->intra_lgt[square_tx_size][mbmi->mode][mbmi->use_lgt];
1047#endif // CONFIG_ENTROPY_STATS
1048 }
1049 if (!mbmi->use_lgt) {
1050 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1051 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
1052 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1053#if CONFIG_ENTROPY_STATS
1054 if (counts)
1055 ++counts
1056 ->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
1057#endif // CONFIG_ENTROPY_STATS
1058 } else {
1059 *tx_type = DCT_DCT; // assign a dummy tx_type
1060 }
1061 } else {
1062 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
1063 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
1064 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
1065#if CONFIG_ENTROPY_STATS
1066 if (counts)
1067 ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
1068#endif // CONFIG_ENTROPY_STATS
1069 }
1070 }
1071#endif // CONFIG_LGT_FROM_PRED
Jingning Hanab7163d2016-11-04 09:46:35 -07001072 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001073 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001074 }
Lester Lu432012f2017-08-17 14:39:29 -07001075#else // CONFIG_EXT_TX
Yue Cheneeacc4c2017-01-17 17:29:17 -08001076
1077 if (tx_size < TX_32X32 &&
1078 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1079 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1080 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001081 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Su98b0b3e2017-09-19 13:54:02 -07001082#if CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001083 FRAME_COUNTS *counts = xd->counts;
Hui Su98b0b3e2017-09-19 13:54:02 -07001084#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001085 if (inter_block) {
Angie Chianga9f9a312017-04-13 16:40:43 -07001086 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001087 r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001088#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001089 if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001090#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001091 } else {
1092 const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
Angie Chianga9f9a312017-04-13 16:40:43 -07001093 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001094 r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Hanab7163d2016-11-04 09:46:35 -07001095 ACCT_STR)];
Hui Su98b0b3e2017-09-19 13:54:02 -07001096#if CONFIG_ENTROPY_STATS
Angie Chianga9f9a312017-04-13 16:40:43 -07001097 if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Hui Su98b0b3e2017-09-19 13:54:02 -07001098#endif // CONFIG_ENTROPY_STATS
Jingning Hanab7163d2016-11-04 09:46:35 -07001099 }
1100 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001101 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001102 }
1103#endif // CONFIG_EXT_TX
1104 }
Nathan E. Eggeebced372017-02-17 20:57:21 -05001105#if FIXED_TX_TYPE
1106 assert(mbmi->tx_type == DCT_DCT);
1107#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001108}
1109
Alex Converse28744302017-04-13 14:46:22 -07001110#if CONFIG_INTRABC
1111static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
1112 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001113 MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -07001114
1115static INLINE int is_mv_valid(const MV *mv);
1116
1117static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
1118 const int_mv *ref_mv, int mi_row, int mi_col,
1119 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -07001120 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1121 (void)cm;
Alex Converse28744302017-04-13 14:46:22 -07001122 FRAME_COUNTS *counts = xd->counts;
1123 nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse6b2584c2017-05-02 09:51:21 -07001124 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
1125 MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -07001126 int valid = is_mv_valid(&mv->as_mv) &&
1127 is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize);
Alex Converse28744302017-04-13 14:46:22 -07001128 return valid;
1129}
1130#endif // CONFIG_INTRABC
1131
Hui Suc2232cf2017-10-11 17:32:56 -07001132#if CONFIG_INTRABC
1133static void read_intrabc_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1134 int mi_row, int mi_col, aom_reader *r) {
1135 MODE_INFO *const mi = xd->mi[0];
1136 MB_MODE_INFO *const mbmi = &mi->mbmi;
1137 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1138 mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
1139 if (mbmi->use_intrabc) {
Hui Su12546aa2017-10-13 16:10:01 -07001140 const BLOCK_SIZE bsize = mbmi->sb_type;
1141 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1142 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1143 int idx, idy;
1144 if ((cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
1145 !xd->lossless[mbmi->segment_id] && !mbmi->skip)) {
1146 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
1147 const int bh = tx_size_high_unit[max_tx_size];
1148 const int bw = tx_size_wide_unit[max_tx_size];
1149 int init_depth =
1150 (height != width) ? RECT_VARTX_DEPTH_INIT : SQR_VARTX_DEPTH_INIT;
1151 mbmi->min_tx_size = TX_SIZES_ALL;
1152 for (idy = 0; idy < height; idy += bh) {
1153 for (idx = 0; idx < width; idx += bw) {
1154 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size, init_depth,
1155 idy, idx, r);
1156 }
1157 }
1158 } else {
1159 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
1160 for (idy = 0; idy < height; ++idy)
1161 for (idx = 0; idx < width; ++idx)
1162 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1163 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1164 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
1165 }
Hui Suc2232cf2017-10-11 17:32:56 -07001166 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
1167 mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
1168
1169 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1170 int_mv ref_mvs[MAX_MV_REF_CANDIDATES];
1171
1172 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
1173 xd->ref_mv_stack[INTRA_FRAME], NULL, ref_mvs, mi_row,
1174 mi_col, NULL, NULL, inter_mode_ctx);
1175
1176 int_mv nearestmv, nearmv;
Hui Suc2232cf2017-10-11 17:32:56 -07001177
RogerZhou10a03802017-10-26 11:49:48 -07001178#if CONFIG_AMVR
1179 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv, 0);
1180#else
1181 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1182#endif
Hui Suc2232cf2017-10-11 17:32:56 -07001183 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1184 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
Hui Suc2232cf2017-10-11 17:32:56 -07001185 xd->corrupted |=
1186 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
Hui Suc2232cf2017-10-11 17:32:56 -07001187#if CONFIG_EXT_TX && !CONFIG_TXK_SEL
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02001188 av1_read_tx_type(cm, xd, r);
Hui Suc2232cf2017-10-11 17:32:56 -07001189#endif // CONFIG_EXT_TX && !CONFIG_TXK_SEL
1190 }
1191}
1192#endif // CONFIG_INTRABC
1193
Yaowu Xuf883b422016-08-30 14:01:10 -07001194static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001196 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197 MODE_INFO *const mi = xd->mi[0];
1198 MB_MODE_INFO *const mbmi = &mi->mbmi;
1199 const MODE_INFO *above_mi = xd->above_mi;
1200 const MODE_INFO *left_mi = xd->left_mi;
1201 const BLOCK_SIZE bsize = mbmi->sb_type;
1202 int i;
1203 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001204 const int bw = mi_size_wide[bsize];
1205 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001206
1207 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001208 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1209 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001210 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001211
1212 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1213 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Arild Fuldseth07441162016-08-15 15:07:52 +02001214
Arild Fuldseth07441162016-08-15 15:07:52 +02001215 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001216 xd->current_qindex =
1217 xd->prev_qindex +
1218 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001219 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1220 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001221 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001222#if CONFIG_EXT_DELTA_Q
1223 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07001224#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07001225 if (cm->delta_lf_multi) {
1226 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
Cheng Chenaff479f2017-10-26 16:53:10 -07001227 const int tmp_lvl =
Cheng Chen880166a2017-10-02 17:48:48 -07001228 xd->prev_delta_lf[lf_id] +
1229 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
1230 cm->delta_lf_res;
Cheng Chenaff479f2017-10-26 16:53:10 -07001231 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
1232 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Cheng Chen880166a2017-10-02 17:48:48 -07001233 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
1234 }
1235 } else {
Cheng Chenaff479f2017-10-26 16:53:10 -07001236 const int tmp_lvl =
Cheng Chen880166a2017-10-02 17:48:48 -07001237 xd->prev_delta_lf_from_base +
1238 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07001239 cm->delta_lf_res;
Cheng Chenaff479f2017-10-26 16:53:10 -07001240 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1241 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Cheng Chen880166a2017-10-02 17:48:48 -07001242 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001243 }
1244#else
Cheng Chen9dccdf22017-10-02 15:04:40 -07001245 const int current_delta_lf_from_base =
Fangwen Fu231fe422017-04-24 17:52:29 -07001246 xd->prev_delta_lf_from_base +
1247 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1248 cm->delta_lf_res;
Cheng Chen9dccdf22017-10-02 15:04:40 -07001249 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
Cheng Chenaff479f2017-10-26 16:53:10 -07001250 clamp(current_delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Fangwen Fu231fe422017-04-24 17:52:29 -07001251 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001252#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07001253 }
1254#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001255 }
Arild Fuldseth07441162016-08-15 15:07:52 +02001256
Yushin Choc24351c2017-10-24 14:59:08 -07001257 mbmi->current_q_index = xd->current_qindex;
1258
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001260 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261
Alex Converse28744302017-04-13 14:46:22 -07001262#if CONFIG_INTRABC
Hui Su12546aa2017-10-13 16:10:01 -07001263 if (cm->allow_screen_content_tools) {
1264 xd->above_txfm_context =
1265 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
1266 xd->left_txfm_context = xd->left_txfm_context_buffer +
1267 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
1268 }
RogerZhouca865462017-10-05 15:06:27 -07001269 if (av1_allow_intrabc(bsize, cm)) {
Hui Suc2232cf2017-10-11 17:32:56 -07001270 read_intrabc_info(cm, xd, mi_row, mi_col, r);
1271 if (is_intrabc_block(mbmi)) return;
Alex Converse28744302017-04-13 14:46:22 -07001272 }
1273#endif // CONFIG_INTRABC
1274
Alex Conversef71808c2017-06-06 12:21:17 -07001275 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
Sebastien Alaiwanfb838772017-10-24 12:02:54 +02001276#if CONFIG_INTRABC
Hui Su12546aa2017-10-13 16:10:01 -07001277 if (cm->allow_screen_content_tools)
1278 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Sebastien Alaiwanfb838772017-10-24 12:02:54 +02001279#endif // CONFIG_INTRABC
Alex Conversef71808c2017-06-06 12:21:17 -07001280
Jingning Han52261842016-12-14 12:17:49 -08001281 (void)i;
1282 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001283 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001284
Jingning Hand3a64432017-04-06 17:04:17 -07001285 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001286 xd->plane[1].subsampling_y)) {
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001287#if CONFIG_CFL
1288 xd->cfl->is_chroma_reference = 1;
1289#endif // CONFIG_CFL
Hui Suaa2965e2017-10-05 15:59:12 -07001290 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001291
Luc Trudeauf5334002017-04-25 12:21:26 -04001292#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001293 if (mbmi->uv_mode == UV_CFL_PRED) {
David Michael Barrf6eaa152017-07-19 19:42:28 +09001294 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001295 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001296 } else {
1297 xd->cfl->store_y = 0;
Luc Trudeauf5334002017-04-25 12:21:26 -04001298 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001299#endif // CONFIG_CFL
1300
Joe Young830d4ce2017-05-30 17:48:13 -07001301 } else {
1302 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001303 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001304#if CONFIG_CFL
1305 xd->cfl->is_chroma_reference = 0;
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001306 xd->cfl->store_y = 1;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001307#endif
Luc Trudeauf5334002017-04-25 12:21:26 -04001308 }
Luc Trudeauf5334002017-04-25 12:21:26 -04001309
Yaowu Xuc27fc142016-08-22 16:08:15 -07001310#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -07001311 read_intra_angle_info(xd, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312#endif // CONFIG_EXT_INTRA
1313 mbmi->palette_mode_info.palette_size[0] = 0;
1314 mbmi->palette_mode_info.palette_size[1] = 0;
Hui Sue87fb232017-10-05 15:00:15 -07001315 if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001316 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001317#if CONFIG_FILTER_INTRA
1318 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1319 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Yue Chen57b8ff62017-10-10 23:37:31 -07001320 read_filter_intra_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001321#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001322
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001323#if !CONFIG_TXK_SEL
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02001324 av1_read_tx_type(cm, xd, r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001325#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001326}
1327
Alex Converse6b2584c2017-05-02 09:51:21 -07001328static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
RogerZhou3b635242017-09-19 10:06:46 -07001329#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001330 int use_subpel,
RogerZhou3b635242017-09-19 10:06:46 -07001331#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001332 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001333 int mag, d, fr, hp;
Thomas Davies599395e2017-07-21 18:02:48 +01001334#if CONFIG_NEW_MULTISYMBOL
1335 const int sign = aom_read_bit(r, ACCT_STR);
1336#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001337 const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001338#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07001339 const int mv_class =
Nathan E. Egged7b893c2016-09-08 15:08:48 -04001340 aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001341 const int class0 = mv_class == MV_CLASS_0;
1342
1343 // Integer part
1344 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001345#if CONFIG_NEW_MULTISYMBOL
1346 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
1347#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -04001348 d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001349#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001350 mag = 0;
1351 } else {
1352 int i;
1353 const int n = mv_class + CLASS0_BITS - 1; // number of bits
Yaowu Xuc27fc142016-08-22 16:08:15 -07001354 d = 0;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001355#if CONFIG_NEW_MULTISYMBOL
1356 for (i = 0; i < n; ++i)
1357 d |= aom_read_symbol(r, mvcomp->bits_cdf[(i + 1) / 2], 2, ACCT_STR) << i;
1358#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001359 for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Thomas Davies0e7b1d72017-10-02 10:54:24 +01001360#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001361 mag = CLASS0_SIZE << (mv_class + 2);
1362 }
1363
RogerZhou3b635242017-09-19 10:06:46 -07001364#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001365 if (use_subpel) {
RogerZhou3b635242017-09-19 10:06:46 -07001366#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001367 // Fractional part
1368 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1369 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001370
Thomas Davies599395e2017-07-21 18:02:48 +01001371// High precision part (if hp is not used, the default value of the hp is 1)
1372#if CONFIG_NEW_MULTISYMBOL
1373 hp = usehp ? aom_read_symbol(
1374 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1375 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001376 : 1;
Thomas Davies599395e2017-07-21 18:02:48 +01001377#else
1378 hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
1379 : 1;
1380#endif
RogerZhou3b635242017-09-19 10:06:46 -07001381#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001382 } else {
1383 fr = 3;
1384 hp = 1;
1385 }
RogerZhou3b635242017-09-19 10:06:46 -07001386#endif // CONFIG_INTRABC || CONFIG_AMVR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387
1388 // Result
1389 mag += ((d << 3) | (fr << 1) | hp) + 1;
1390 return sign ? -mag : mag;
1391}
1392
Yaowu Xuf883b422016-08-30 14:01:10 -07001393static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +01001394 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001395 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001396 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001397 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001398 joint_type =
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -04001399 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400
1401 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001402 diff.row = read_mv_component(r, &ctx->comps[0],
RogerZhou3b635242017-09-19 10:06:46 -07001403#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001404 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001405#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001406 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001407
1408 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001409 diff.col = read_mv_component(r, &ctx->comps[1],
RogerZhou3b635242017-09-19 10:06:46 -07001410#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001411 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001412#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001413 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414
Alex Converse6b2584c2017-05-02 09:51:21 -07001415 av1_inc_mv(&diff, counts, precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416
1417 mv->row = ref->row + diff.row;
1418 mv->col = ref->col + diff.col;
1419}
1420
Yaowu Xuf883b422016-08-30 14:01:10 -07001421static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001422 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001423 aom_reader *r) {
Debargha Mukherjee0f248c42017-09-07 12:40:18 -07001424 if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type)) return SINGLE_REFERENCE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001425 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001426 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001427#if CONFIG_NEW_MULTISYMBOL
1428 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1429 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
1430#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001431 const REFERENCE_MODE mode =
Michael Bebenita6048d052016-08-25 14:40:54 -07001432 (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001433#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001434 FRAME_COUNTS *counts = xd->counts;
1435 if (counts) ++counts->comp_inter[ctx][mode];
1436 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1437 } else {
1438 return cm->reference_mode;
1439 }
1440}
1441
Zoe Liuf704a1c2017-10-02 16:55:59 -07001442#if CONFIG_EXT_SKIP
1443static void update_block_reference_mode(AV1_COMMON *cm, const MACROBLOCKD *xd,
1444 REFERENCE_MODE mode) {
1445 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
1446 assert(mode == SINGLE_REFERENCE || mode == COMPOUND_REFERENCE);
1447 const int ctx = av1_get_reference_mode_context(cm, xd);
1448#if CONFIG_NEW_MULTISYMBOL
1449 update_cdf(xd->tile_ctx->comp_inter_cdf[ctx], mode, 2);
1450#endif // CONFIG_NEW_MULTISYMBOL
1451 FRAME_COUNTS *counts = xd->counts;
1452 if (counts) ++counts->comp_inter[ctx][mode];
1453 }
1454}
1455#endif // CONFIG_EXT_SKIP
1456
Thomas Davies315f5782017-06-14 15:14:55 +01001457#if CONFIG_NEW_MULTISYMBOL
1458#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001459 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001460#define READ_REF_BIT2(pname) \
1461 aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001462#else
1463#define READ_REF_BIT(pname) \
1464 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001465#define READ_REF_BIT2(pname) \
1466 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001467#endif
1468
Zoe Liuc082bbc2017-05-17 13:31:37 -07001469#if CONFIG_EXT_COMP_REFS
Zoe Liuec50d6b2017-08-23 16:02:59 -07001470static COMP_REFERENCE_TYPE read_comp_reference_type(AV1_COMMON *cm,
1471 const MACROBLOCKD *xd,
1472 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001473 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001474#if USE_UNI_COMP_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07001475 COMP_REFERENCE_TYPE comp_ref_type;
1476#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001477 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm)) {
1478 if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm)) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001479#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001480#if CONFIG_NEW_MULTISYMBOL
1481 (void)cm;
1482 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read_symbol(
1483 r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
1484#else
1485 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read(
1486 r, cm->fc->comp_ref_type_prob[ctx], ACCT_STR);
1487#endif
Zoe Liufcf5fa22017-06-26 16:00:38 -07001488#if CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001489 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001490 comp_ref_type = BIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001491 }
1492 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001493 comp_ref_type = UNIDIR_COMP_REFERENCE;
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001494 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001495#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001496#else // !USE_UNI_COMP_REFS
1497 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1498 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1499#endif // USE_UNI_COMP_REFS
1500 FRAME_COUNTS *counts = xd->counts;
1501 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1502 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1503}
1504#endif // CONFIG_EXT_COMP_REFS
1505
Zoe Liuf704a1c2017-10-02 16:55:59 -07001506#if CONFIG_EXT_SKIP
1507#if CONFIG_EXT_COMP_REFS
1508static void update_comp_reference_type(AV1_COMMON *cm, const MACROBLOCKD *xd,
1509 COMP_REFERENCE_TYPE comp_ref_type) {
1510 assert(comp_ref_type == UNIDIR_COMP_REFERENCE ||
1511 comp_ref_type == BIDIR_COMP_REFERENCE);
1512 (void)cm;
1513 const int ctx = av1_get_comp_reference_type_context(xd);
1514#if USE_UNI_COMP_REFS
1515#if CONFIG_NEW_MULTISYMBOL
1516 update_cdf(xd->tile_ctx->comp_ref_type_cdf[ctx], comp_ref_type, 2);
1517#endif // CONFIG_NEW_MULTISYMBOL
1518#endif // USE_UNI_COMP_REFS
1519 FRAME_COUNTS *counts = xd->counts;
1520 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1521}
1522#endif // CONFIG_EXT_COMP_REFS
1523
1524static void set_ref_frames_for_skip_mode(AV1_COMMON *const cm,
1525 MACROBLOCKD *const xd,
1526 MV_REFERENCE_FRAME ref_frame[2]) {
1527 assert(xd->mi[0]->mbmi.skip_mode);
1528
1529 ref_frame[0] = LAST_FRAME + cm->ref_frame_idx_0;
1530 ref_frame[1] = LAST_FRAME + cm->ref_frame_idx_1;
1531
1532 const REFERENCE_MODE mode = COMPOUND_REFERENCE;
1533 update_block_reference_mode(cm, xd, mode);
1534
1535#if CONFIG_EXT_COMP_REFS
1536 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1537 update_comp_reference_type(cm, xd, comp_ref_type);
1538#endif // CONFIG_EXT_COMP_REFS
1539
1540// Update stats for both forward and backward references
1541#if CONFIG_NEW_MULTISYMBOL
1542#define UPDATE_REF_BIT(bname, pname, cname, iname) \
1543 update_cdf(av1_get_pred_cdf_##pname(cm, xd), bname, 2); \
1544 if (counts) \
1545 ++counts->comp_##cname[av1_get_pred_context_##pname(cm, xd)][iname][bname];
1546#else
1547#define UPDATE_REF_BIT(bname, pname, cname, iname) \
1548 if (counts) \
1549 ++counts->comp_##cname[av1_get_pred_context_##pname(cm, xd)][iname][bname];
1550#endif // CONFIG_NEW_MULTISYMBOL
1551
1552 FRAME_COUNTS *counts = xd->counts;
1553
1554 const int bit = (ref_frame[0] == GOLDEN_FRAME || ref_frame[0] == LAST3_FRAME);
1555 UPDATE_REF_BIT(bit, comp_ref_p, ref, 0)
1556 if (!bit) {
1557 UPDATE_REF_BIT(ref_frame[0] == LAST_FRAME, comp_ref_p1, ref, 1)
1558 } else {
1559 UPDATE_REF_BIT(ref_frame[0] == GOLDEN_FRAME, comp_ref_p2, ref, 2)
1560 }
1561
1562 const int bit_bwd = ref_frame[1] == ALTREF_FRAME;
1563 UPDATE_REF_BIT(bit_bwd, comp_bwdref_p, bwdref, 0)
1564 if (!bit_bwd) {
1565 UPDATE_REF_BIT(ref_frame[1] == ALTREF2_FRAME, comp_bwdref_p1, bwdref, 1)
1566 }
1567}
1568#endif // CONFIG_EXT_SKIP
1569
Yaowu Xuc27fc142016-08-22 16:08:15 -07001570// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001571static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1572 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001573 MV_REFERENCE_FRAME ref_frame[2]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001574 FRAME_COUNTS *counts = xd->counts;
1575
1576 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1577 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1578 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001579 ref_frame[1] = NONE_FRAME;
David Barkerd92f3562017-10-09 17:46:23 +01001580 }
1581#if CONFIG_SEGMENT_ZEROMV
1582 else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
1583 segfeature_active(&cm->seg, segment_id, SEG_LVL_ZEROMV))
1584#else
1585 else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP))
1586#endif
1587 {
1588 ref_frame[0] = LAST_FRAME;
1589 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001590 } else {
Zoe Liuf704a1c2017-10-02 16:55:59 -07001591#if CONFIG_EXT_SKIP
1592 if (xd->mi[0]->mbmi.skip_mode) {
1593 set_ref_frames_for_skip_mode(cm, xd, ref_frame);
1594 return;
1595 }
1596#endif // CONFIG_EXT_SKIP
1597
Yaowu Xuc27fc142016-08-22 16:08:15 -07001598 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
Zoe Liuf704a1c2017-10-02 16:55:59 -07001599
Yaowu Xuc27fc142016-08-22 16:08:15 -07001600 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1601 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001602#if CONFIG_EXT_COMP_REFS
1603 const COMP_REFERENCE_TYPE comp_ref_type =
1604 read_comp_reference_type(cm, xd, r);
1605
1606#if !USE_UNI_COMP_REFS
1607 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1608 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
1609#endif // !USE_UNI_COMP_REFS
1610
1611 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001612 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1613 int bit;
1614#if CONFIG_VAR_REFS
1615 if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
1616#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001617 bit = READ_REF_BIT2(uni_comp_ref_p);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001618#if CONFIG_VAR_REFS
1619 else
1620 bit = BWD_AND_ALT(cm);
1621#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001622 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1623
1624 if (bit) {
1625 ref_frame[0] = BWDREF_FRAME;
1626 ref_frame[1] = ALTREF_FRAME;
1627 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001628 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1629 int bit1;
1630#if CONFIG_VAR_REFS
1631 if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm)))
1632#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001633 bit1 = READ_REF_BIT2(uni_comp_ref_p1);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001634#if CONFIG_VAR_REFS
1635 else
1636 bit1 = L_AND_L3(cm) || L_AND_G(cm);
1637#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001638 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1639
1640 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001641 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1642 int bit2;
1643#if CONFIG_VAR_REFS
1644 if (L_AND_L3(cm) && L_AND_G(cm))
1645#endif // CONFIG_VAR_REFS
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001646 bit2 = READ_REF_BIT2(uni_comp_ref_p2);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001647#if CONFIG_VAR_REFS
1648 else
1649 bit2 = L_AND_G(cm);
1650#endif // CONFIG_VAR_REFS
1651 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1652
1653 if (bit2) {
1654 ref_frame[0] = LAST_FRAME;
1655 ref_frame[1] = GOLDEN_FRAME;
1656 } else {
1657 ref_frame[0] = LAST_FRAME;
1658 ref_frame[1] = LAST3_FRAME;
1659 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001660 } else {
1661 ref_frame[0] = LAST_FRAME;
1662 ref_frame[1] = LAST2_FRAME;
1663 }
1664 }
1665
1666 return;
1667 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001668
1669 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001670#endif // CONFIG_EXT_COMP_REFS
1671
1672// Normative in decoder (for low delay)
Zoe Liu17af2742017-10-06 10:36:42 -07001673#if CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001674 const int idx = 1;
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02001675#else // !(CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676 const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
Zoe Liu17af2742017-10-06 10:36:42 -07001677#endif // CONFIG_ONE_SIDED_COMPOUND || CONFIG_FRAME_SIGN_BIAS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001678
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001679 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
1680#if CONFIG_VAR_REFS
1681 int bit;
1682 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1683 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001684 bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001685 else
1686 bit = L3_OR_G(cm);
1687#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001688 const int bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001689#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001690 if (counts) ++counts->comp_ref[ctx][0][bit];
1691
Yaowu Xuc27fc142016-08-22 16:08:15 -07001692 // Decode forward references.
1693 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001694 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001695#if CONFIG_VAR_REFS
1696 int bit1;
1697 // Test need to explicitly code (L) vs (L2) branch node in tree
1698 if (L_AND_L2(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001699 bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001700 else
1701 bit1 = LAST_IS_VALID(cm);
1702#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001703 const int bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001704#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001705 if (counts) ++counts->comp_ref[ctx1][1][bit1];
1706 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
1707 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001708 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001709#if CONFIG_VAR_REFS
1710 int bit2;
1711 // Test need to explicitly code (L3) vs (G) branch node in tree
1712 if (L3_AND_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001713 bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001714 else
1715 bit2 = GOLDEN_IS_VALID(cm);
1716#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001717 const int bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001718#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001719 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1720 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1721 }
1722
1723 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001724 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
1725#if CONFIG_VAR_REFS
1726 int bit_bwd;
Zoe Liu3ac20932017-08-30 16:35:55 -07001727 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001728 const int bit_bwd_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001729 if (bit_bwd_uncertain)
Thomas Davies894cc812017-06-22 17:51:33 +01001730 bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001731 else
1732 bit_bwd = ALTREF_IS_VALID(cm);
Thomas Davies894cc812017-06-22 17:51:33 +01001733#else // !CONFIG_VAR_REFS
1734 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001735#endif // CONFIG_VAR_REFS
1736 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001737 if (!bit_bwd) {
1738 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
1739#if CONFIG_VAR_REFS
1740 int bit1_bwd;
1741 if (BWD_AND_ALT2(cm))
1742 bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1743 else
1744 bit1_bwd = ALTREF2_IS_VALID(cm);
1745#else // !CONFIG_VAR_REFS
1746 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1747#endif // CONFIG_VAR_REFS
1748 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1749 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1750 } else {
1751 ref_frame[idx] = cm->comp_bwd_ref[2];
1752 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001753 } else if (mode == SINGLE_REFERENCE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001754 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001755#if CONFIG_VAR_REFS
1756 int bit0;
Zoe Liue9b15e22017-07-19 15:53:01 -07001757 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT2,ALT) branch node
1758 // in tree
1759 if ((L_OR_L2(cm) || L3_OR_G(cm)) &&
1760 (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm)))
1761 bit0 = READ_REF_BIT(single_ref_p1);
1762 else
1763 bit0 = (BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm));
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001764#else // !CONFIG_VAR_REFS
Thomas Davies315f5782017-06-14 15:14:55 +01001765 const int bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001766#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001767 if (counts) ++counts->single_ref[ctx0][0][bit0];
1768
1769 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001770 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001771#if CONFIG_VAR_REFS
1772 int bit1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001773 // Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
Zoe Liu043c2272017-07-19 12:40:29 -07001774 const int bit1_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
Zoe Liu043c2272017-07-19 12:40:29 -07001775 if (bit1_uncertain)
Thomas Davies315f5782017-06-14 15:14:55 +01001776 bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001777 else
1778 bit1 = ALTREF_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001779#else // !CONFIG_VAR_REFS
1780 const int bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001781#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001782 if (counts) ++counts->single_ref[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001783 if (!bit1) {
1784 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
1785#if CONFIG_VAR_REFS
1786 int bit5;
1787 if (BWD_AND_ALT2(cm))
1788 bit5 = READ_REF_BIT(single_ref_p6);
1789 else
1790 bit5 = ALTREF2_IS_VALID(cm);
1791#else // !CONFIG_VAR_REFS
1792 const int bit5 = READ_REF_BIT(single_ref_p6);
1793#endif // CONFIG_VAR_REFS
1794 if (counts) ++counts->single_ref[ctx5][5][bit5];
1795 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1796 } else {
1797 ref_frame[0] = ALTREF_FRAME;
1798 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001799 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001800 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001801#if CONFIG_VAR_REFS
1802 int bit2;
1803 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1804 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001805 bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001806 else
1807 bit2 = L3_OR_G(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001808#else // !CONFIG_VAR_REFS
1809 const int bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001810#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001811 if (counts) ++counts->single_ref[ctx2][2][bit2];
1812 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001813 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001814#if CONFIG_VAR_REFS
1815 int bit4;
1816 // Test need to explicitly code (L3) vs (G) branch node in tree
1817 if (L3_AND_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001818 bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001819 else
1820 bit4 = GOLDEN_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001821#else // !CONFIG_VAR_REFS
1822 const int bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001823#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001824 if (counts) ++counts->single_ref[ctx4][4][bit4];
1825 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1826 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001827 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001828#if CONFIG_VAR_REFS
1829 int bit3;
1830 // Test need to explicitly code (L) vs (L2) branch node in tree
1831 if (L_AND_L2(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001832 bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001833 else
1834 bit3 = LAST2_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001835#else // !CONFIG_VAR_REFS
1836 const int bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001837#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001838 if (counts) ++counts->single_ref[ctx3][3][bit3];
1839 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1840 }
1841 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001842
Emil Keyder01770b32017-01-20 18:03:11 -05001843 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001844 } else {
1845 assert(0 && "Invalid prediction mode.");
1846 }
1847 }
1848}
1849
Angie Chiang9c4f8952016-11-21 11:13:19 -08001850static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1851 MACROBLOCKD *const xd,
1852 MB_MODE_INFO *const mbmi,
1853 aom_reader *r) {
1854 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001855 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001856
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001857 if (!av1_is_interp_needed(xd)) {
1858 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001859 return;
1860 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001861
Yaowu Xuc27fc142016-08-22 16:08:15 -07001862 if (cm->interp_filter != SWITCHABLE) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001863 mbmi->interp_filters = av1_broadcast_interp_filter(cm->interp_filter);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001864 } else {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001865#if CONFIG_DUAL_FILTER
1866 InterpFilter ref0_filter[2] = { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR };
1867 for (int dir = 0; dir < 2; ++dir) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001868 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1869 (mbmi->ref_frame[1] > INTRA_FRAME &&
1870 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001871 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1872 ref0_filter[dir] =
Thomas Daviesec92c112017-09-25 11:03:58 +01001873 (InterpFilter)aom_read_symbol(r, ec_ctx->switchable_interp_cdf[ctx],
1874 SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001875 if (counts) ++counts->switchable_interp[ctx][ref0_filter[dir]];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001876 }
1877 }
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001878 // The index system works as: (0, 1) -> (vertical, horizontal) filter types
1879 mbmi->interp_filters =
1880 av1_make_interp_filters(ref0_filter[0], ref0_filter[1]);
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001881#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001882 const int ctx = av1_get_pred_context_switchable_interp(xd);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001883 InterpFilter filter = (InterpFilter)aom_read_symbol(
Thomas Daviesec92c112017-09-25 11:03:58 +01001884 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001885 mbmi->interp_filters = av1_broadcast_interp_filter(filter);
1886 if (counts) ++counts->switchable_interp[ctx][filter];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001887#endif // CONFIG_DUAL_FILTER
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001888 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001889}
1890
Jingning Han36fe3202017-02-20 22:31:49 -08001891static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1892 const int mi_col, MACROBLOCKD *const xd,
1893 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001894 MB_MODE_INFO *const mbmi = &mi->mbmi;
1895 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1896 int i;
1897
1898 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001899 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001900
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001901 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001902
Jingning Han52261842016-12-14 12:17:49 -08001903 (void)i;
Hui Suaa2965e2017-10-05 15:59:12 -07001904 mbmi->mode = read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001905
Jingning Hand3a64432017-04-06 17:04:17 -07001906 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001907 xd->plane[1].subsampling_y)) {
Hui Suaa2965e2017-10-05 15:59:12 -07001908 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001909
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001910#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001911 if (mbmi->uv_mode == UV_CFL_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001912 mbmi->cfl_alpha_idx =
David Michael Barrf6eaa152017-07-19 19:42:28 +09001913 read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001914 xd->cfl->store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001915 } else {
1916 xd->cfl->store_y = 0;
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001917 }
1918#endif // CONFIG_CFL
1919
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001920 } else {
1921 // Avoid decoding angle_info if there is is no chroma prediction
1922 mbmi->uv_mode = UV_DC_PRED;
1923#if CONFIG_CFL
1924 xd->cfl->is_chroma_reference = 0;
1925 xd->cfl->store_y = 1;
1926#endif
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001927 }
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001928
Rupert Swarbrick766c9d52017-07-31 11:30:53 +01001929 // Explicitly ignore cm here to avoid a compile warning if none of
1930 // ext-intra, palette and filter-intra are enabled.
1931 (void)cm;
1932
Yaowu Xuc27fc142016-08-22 16:08:15 -07001933#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -07001934 read_intra_angle_info(xd, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001935#endif // CONFIG_EXT_INTRA
1936 mbmi->palette_mode_info.palette_size[0] = 0;
1937 mbmi->palette_mode_info.palette_size[1] = 0;
Hui Sue87fb232017-10-05 15:00:15 -07001938 if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001939 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001940#if CONFIG_FILTER_INTRA
1941 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1942 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Yue Chen57b8ff62017-10-10 23:37:31 -07001943 read_filter_intra_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001944#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945}
1946
1947static INLINE int is_mv_valid(const MV *mv) {
1948 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1949 mv->col < MV_UPP;
1950}
1951
Yaowu Xuf883b422016-08-30 14:01:10 -07001952static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001953 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001954 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001955 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001956 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1957 int mi_col, int is_compound, int allow_hp,
1958 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001959 int i;
1960 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001961 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001962 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001963 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001964 int_mv *pred_mv = mbmi->pred_mv;
1965 (void)block;
Sarah Parkere5299862016-08-16 14:57:37 -07001966 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001967 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001968 (void)mi_row;
1969 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001970 (void)bsize;
RogerZhou3b635242017-09-19 10:06:46 -07001971#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07001972 if (cm->cur_frame_force_integer_mv) {
RogerZhou3b635242017-09-19 10:06:46 -07001973 allow_hp = MV_SUBPEL_NONE;
1974 }
1975#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001976 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977 case NEWMV: {
1978 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001979 for (i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001980 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1981 int nmv_ctx =
1982 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1983 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001984 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985 nmv_context_counts *const mv_counts =
1986 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001987 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988 ret = ret && is_mv_valid(&mv[i].as_mv);
1989
Yaowu Xuc27fc142016-08-22 16:08:15 -07001990 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001991 }
1992 break;
1993 }
1994 case NEARESTMV: {
1995 mv[0].as_int = nearest_mv[0].as_int;
1996 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1997
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998 pred_mv[0].as_int = nearest_mv[0].as_int;
1999 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002000 break;
2001 }
2002 case NEARMV: {
2003 mv[0].as_int = near_mv[0].as_int;
2004 if (is_compound) mv[1].as_int = near_mv[1].as_int;
2005
Yaowu Xuc27fc142016-08-22 16:08:15 -07002006 pred_mv[0].as_int = near_mv[0].as_int;
2007 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002008 break;
2009 }
2010 case ZEROMV: {
David Barkercdcac6d2016-12-01 17:04:16 +00002011 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002012 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002013 mi_col, mi_row, block
2014#if CONFIG_AMVR
2015 ,
RogerZhou10a03802017-10-26 11:49:48 -07002016 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07002017#endif
2018 )
David Barkercdcac6d2016-12-01 17:04:16 +00002019 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07002020 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00002021 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002022 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002023 mi_col, mi_row, block
2024#if CONFIG_AMVR
2025 ,
RogerZhou10a03802017-10-26 11:49:48 -07002026 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07002027#endif
2028 )
David Barkercdcac6d2016-12-01 17:04:16 +00002029 .as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002031 pred_mv[0].as_int = mv[0].as_int;
2032 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 break;
2034 }
Zoe Liu85b66462017-04-20 14:28:19 -07002035#if CONFIG_COMPOUND_SINGLEREF
2036 case SR_NEAREST_NEARMV: {
2037 assert(!is_compound);
2038 mv[0].as_int = nearest_mv[0].as_int;
2039 mv[1].as_int = near_mv[0].as_int;
2040 break;
2041 }
2042 /*
2043 case SR_NEAREST_NEWMV: {
2044 assert(!is_compound);
2045 mv[0].as_int = nearest_mv[0].as_int;
2046
2047 FRAME_COUNTS *counts = xd->counts;
2048 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2049 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2050 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2051 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2052 nmv_context_counts *const mv_counts =
2053 counts ? &counts->mv[nmv_ctx] : NULL;
2054 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2055 ret = ret && is_mv_valid(&mv[1].as_mv);
2056 break;
2057 }*/
2058 case SR_NEAR_NEWMV: {
2059 assert(!is_compound);
2060 mv[0].as_int = near_mv[0].as_int;
2061
2062 FRAME_COUNTS *counts = xd->counts;
2063 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2064 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2065 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2066 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2067 nmv_context_counts *const mv_counts =
2068 counts ? &counts->mv[nmv_ctx] : NULL;
2069 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2070 ret = ret && is_mv_valid(&mv[1].as_mv);
2071 break;
2072 }
2073 case SR_ZERO_NEWMV: {
2074 assert(!is_compound);
Zoe Liu85b66462017-04-20 14:28:19 -07002075 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
2076 cm->allow_high_precision_mv, bsize,
2077 mi_col, mi_row, block)
2078 .as_int;
Zoe Liu85b66462017-04-20 14:28:19 -07002079
2080 FRAME_COUNTS *counts = xd->counts;
2081 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2082 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2083 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
2084 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2085 nmv_context_counts *const mv_counts =
2086 counts ? &counts->mv[nmv_ctx] : NULL;
2087 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2088 ret = ret && is_mv_valid(&mv[1].as_mv);
2089 break;
2090 }
2091 case SR_NEW_NEWMV: {
2092 assert(!is_compound);
2093
2094 FRAME_COUNTS *counts = xd->counts;
2095 for (i = 0; i < 2; ++i) {
2096 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2097 int nmv_ctx =
2098 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
2099 mbmi->ref_mv_idx);
2100 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
2101 nmv_context_counts *const mv_counts =
2102 counts ? &counts->mv[nmv_ctx] : NULL;
2103 read_mv(r, &mv[i].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
2104 ret = ret && is_mv_valid(&mv[i].as_mv);
2105 }
2106 break;
2107 }
2108#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002109 case NEW_NEWMV: {
2110 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002111 assert(is_compound);
2112 for (i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002113 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2114 int nmv_ctx =
2115 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
2116 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002117 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002118 nmv_context_counts *const mv_counts =
2119 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002120 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002121 ret = ret && is_mv_valid(&mv[i].as_mv);
2122 }
2123 break;
2124 }
2125 case NEAREST_NEARESTMV: {
2126 assert(is_compound);
2127 mv[0].as_int = nearest_mv[0].as_int;
2128 mv[1].as_int = nearest_mv[1].as_int;
2129 break;
2130 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002131 case NEAR_NEARMV: {
2132 assert(is_compound);
2133 mv[0].as_int = near_mv[0].as_int;
2134 mv[1].as_int = near_mv[1].as_int;
2135 break;
2136 }
2137 case NEW_NEARESTMV: {
2138 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002139 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2140 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2141 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002142 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002143 nmv_context_counts *const mv_counts =
2144 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002145 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146 assert(is_compound);
2147 ret = ret && is_mv_valid(&mv[0].as_mv);
2148 mv[1].as_int = nearest_mv[1].as_int;
2149 break;
2150 }
2151 case NEAREST_NEWMV: {
2152 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002153 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2154 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2155 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002156 nmv_context_counts *const mv_counts =
2157 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002158 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07002159 mv[0].as_int = nearest_mv[0].as_int;
2160 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002161 assert(is_compound);
2162 ret = ret && is_mv_valid(&mv[1].as_mv);
2163 break;
2164 }
2165 case NEAR_NEWMV: {
2166 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002167 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2168 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2169 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002170 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002171 nmv_context_counts *const mv_counts =
2172 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002173 mv[0].as_int = near_mv[0].as_int;
2174 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002175 assert(is_compound);
2176
2177 ret = ret && is_mv_valid(&mv[1].as_mv);
2178 break;
2179 }
2180 case NEW_NEARMV: {
2181 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002182 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2183 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2184 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002185 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002186 nmv_context_counts *const mv_counts =
2187 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002188 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002189 assert(is_compound);
2190 ret = ret && is_mv_valid(&mv[0].as_mv);
2191 mv[1].as_int = near_mv[1].as_int;
2192 break;
2193 }
2194 case ZERO_ZEROMV: {
2195 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08002196 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002197 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002198 mi_col, mi_row, block
2199#if CONFIG_AMVR
2200 ,
RogerZhou10a03802017-10-26 11:49:48 -07002201 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07002202#endif
2203 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002204 .as_int;
2205 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002206 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07002207 mi_col, mi_row, block
2208#if CONFIG_AMVR
2209 ,
RogerZhou10a03802017-10-26 11:49:48 -07002210 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07002211#endif
2212 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08002213 .as_int;
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
Zoe Liuf704a1c2017-10-02 16:55:59 -07002240#if CONFIG_EXT_SKIP
2241static void update_block_intra_inter(AV1_COMMON *const cm,
2242 MACROBLOCKD *const xd, int segment_id,
2243 const int is_inter) {
2244 if (!segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
2245 const int ctx = av1_get_intra_inter_context(xd);
2246#if CONFIG_NEW_MULTISYMBOL
2247 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
2248 update_cdf(ec_ctx->intra_inter_cdf[ctx], is_inter, 2);
2249#endif // CONFIG_NEW_MULTISYMBOL
2250 FRAME_COUNTS *counts = xd->counts;
2251 if (counts) ++counts->intra_inter[ctx][is_inter];
2252 }
2253}
2254#endif // CONFIG_EXT_SKIP
2255
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002256#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002257static int read_is_inter_singleref_comp_mode(AV1_COMMON *const cm,
2258 MACROBLOCKD *const xd,
2259 int segment_id, aom_reader *r) {
2260 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) return 0;
2261
2262 const int ctx = av1_get_inter_mode_context(xd);
2263 const int is_singleref_comp_mode =
2264 aom_read(r, cm->fc->comp_inter_mode_prob[ctx], ACCT_STR);
2265 FRAME_COUNTS *counts = xd->counts;
2266
2267 if (counts) ++counts->comp_inter_mode[ctx][is_singleref_comp_mode];
2268 return is_singleref_comp_mode;
2269}
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002270#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002271
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002273 AV1Decoder *const pbi = (AV1Decoder *)data;
2274 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
2275 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276}
2277
Di Chen56586622017-06-09 13:49:44 -07002278#if DEC_MISMATCH_DEBUG
Zoe Liu0b3ef732017-10-06 18:37:24 -07002279static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi, int mi_row,
Zoe Liuf704a1c2017-10-02 16:55:59 -07002280 int mi_col, int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07002281 int_mv mv[2] = { { 0 } };
2282 int ref;
2283 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07002284 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
2285 mv[ref].as_mv = mbmi->mv[ref].as_mv;
2286
Di Chen56586622017-06-09 13:49:44 -07002287 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
2288 int16_t zeromv_ctx = -1;
2289 int16_t refmv_ctx = -1;
2290 if (mbmi->mode != NEWMV) {
2291 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == ZEROMV);
2292 zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
2293 if (mbmi->mode != ZEROMV) {
2294 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
2295 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
2296 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
2297 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
2298 }
2299 }
2300
Zoe Liuf9333f52017-07-03 10:52:01 -07002301#define FRAME_TO_CHECK 1
Zoe Liu17af2742017-10-06 10:36:42 -07002302 if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) {
Zoe Liuf9333f52017-07-03 10:52:01 -07002303 printf(
2304 "=== DECODER ===: "
Zoe Liuf704a1c2017-10-02 16:55:59 -07002305 "Frame=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d, mode=%d, bsize=%d, "
Zoe Liuf9333f52017-07-03 10:52:01 -07002306 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
Zoe Liuf704a1c2017-10-02 16:55:59 -07002307 "ref[1]=%d, motion_mode=%d, mode_ctx=%d, "
Zoe Liu0b3ef732017-10-06 18:37:24 -07002308 "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d\n",
Zoe Liuf704a1c2017-10-02 16:55:59 -07002309 cm->current_video_frame, mi_row, mi_col, mbmi->skip_mode, mbmi->mode,
2310 mbmi->sb_type, cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col,
2311 mv[1].as_mv.row, mv[1].as_mv.col, mbmi->ref_frame[0],
2312 mbmi->ref_frame[1], mbmi->motion_mode, mode_ctx, newmv_ctx, zeromv_ctx,
2313 refmv_ctx);
Zoe Liuf9333f52017-07-03 10:52:01 -07002314 }
Di Chen56586622017-06-09 13:49:44 -07002315}
2316#endif // DEC_MISMATCH_DEBUG
2317
Yaowu Xuf883b422016-08-30 14:01:10 -07002318static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 MACROBLOCKD *const xd,
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002320 MODE_INFO *const mi, int mi_row,
2321 int mi_col, aom_reader *r) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002322 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002323 MB_MODE_INFO *const mbmi = &mi->mbmi;
2324 const BLOCK_SIZE bsize = mbmi->sb_type;
2325 const int allow_hp = cm->allow_high_precision_mv;
2326 int_mv nearestmv[2], nearmv[2];
2327 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002328 int ref, is_compound;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002329#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002330 int is_singleref_comp_mode = 0;
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002331#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002332 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002333 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002334 int mode_ctx = 0;
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08002335 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang97d6a372017-10-09 14:15:15 -07002336#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07002337 int pts_mv[SAMPLES_ARRAY_SIZE];
Yunqing Wang97d6a372017-10-09 14:15:15 -07002338#endif // CONFIG_EXT_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00002339 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002340
Urvang Joshi5a9ea002017-05-22 15:25:18 -07002341 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
2342
Luc Trudeaub05eeae2017-08-18 15:14:30 -04002343 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002344 mbmi->palette_mode_info.palette_size[0] = 0;
2345 mbmi->palette_mode_info.palette_size[1] = 0;
2346
Frederic Barbier7a84fd82017-03-02 18:08:15 +01002347 memset(ref_mvs, 0, sizeof(ref_mvs));
2348
Yaowu Xuc27fc142016-08-22 16:08:15 -07002349 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
2350 is_compound = has_second_ref(mbmi);
2351
Cheng Chen0a7f2f52017-10-10 15:16:09 -07002352#if CONFIG_JNT_COMP
2353 if (is_compound) {
2354 const int comp_index_ctx = get_comp_index_context(cm, xd);
2355 mbmi->compound_idx =
2356 aom_read(r, ec_ctx->compound_index_probs[comp_index_ctx], ACCT_STR);
2357 if (xd->counts)
2358 ++xd->counts->compound_index[comp_index_ctx][mbmi->compound_idx];
2359 }
2360#endif // CONFIG_JNT_COMP
2361
Zoe Liuc082bbc2017-05-17 13:31:37 -07002362#if CONFIG_EXT_COMP_REFS
2363#if !USE_UNI_COMP_REFS
2364 // NOTE: uni-directional comp refs disabled
2365 if (is_compound)
2366 assert(mbmi->ref_frame[0] < BWDREF_FRAME &&
2367 mbmi->ref_frame[1] >= BWDREF_FRAME);
2368#endif // !USE_UNI_COMP_REFS
2369#endif // CONFIG_EXT_COMP_REFS
2370
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002371#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002372 if (!is_compound)
2373 is_singleref_comp_mode =
2374 read_is_inter_singleref_comp_mode(cm, xd, mbmi->segment_id, r);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002375#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liu85b66462017-04-20 14:28:19 -07002376
Yaowu Xuc27fc142016-08-22 16:08:15 -07002377 for (ref = 0; ref < 1 + is_compound; ++ref) {
2378 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002380 av1_find_mv_refs(cm, xd, mi, frame, &xd->ref_mv_count[frame],
2381 xd->ref_mv_stack[frame], compound_inter_mode_ctx,
2382 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2383 inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384 }
2385
Jingning Hanacddc032016-11-17 15:26:20 -08002386 if (is_compound) {
2387 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002388 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002389 xd->ref_mv_stack[ref_frame], compound_inter_mode_ctx,
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002390 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2391 inter_mode_ctx);
2392
2393 if (xd->ref_mv_count[ref_frame] < 2) {
2394 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00002395 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002396 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00002397 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00002398 cm->allow_high_precision_mv,
RogerZhou3b635242017-09-19 10:06:46 -07002399 bsize, mi_col, mi_row, 0
2400#if CONFIG_AMVR
2401 ,
RogerZhou10a03802017-10-26 11:49:48 -07002402 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07002403#endif
2404 )
David Barkercdcac6d2016-12-01 17:04:16 +00002405 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -07002406 zeromv[1].as_int =
2407 (rf[1] != NONE_FRAME)
2408 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
2409 cm->allow_high_precision_mv, bsize, mi_col,
2410 mi_row, 0
2411#if CONFIG_AMVR
2412 ,
RogerZhou10a03802017-10-26 11:49:48 -07002413 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07002414#endif
2415 )
2416 .as_int
2417 : 0;
Sarah Parker9923d1b2017-04-10 11:56:40 -07002418 for (ref = 0; ref < 2; ++ref) {
2419 if (rf[ref] == NONE_FRAME) continue;
RogerZhou3b635242017-09-19 10:06:46 -07002420#if CONFIG_AMVR
2421 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07002422 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002423 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07002424 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002425#else
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002426 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
2427 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002428#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002429 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
2430 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
2431 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01002432 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002433 }
2434 }
2435
Zoe Liu85b66462017-04-20 14:28:19 -07002436#if CONFIG_COMPOUND_SINGLEREF
2437 if (is_compound || is_singleref_comp_mode)
2438#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439 if (is_compound)
Zoe Liu85b66462017-04-20 14:28:19 -07002440#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
2442 else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07002444 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002447#if CONFIG_SEGMENT_ZEROMV
2448 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
David Barkerd92f3562017-10-09 17:46:23 +01002449 segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_ZEROMV))
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002450#else
David Barkerd92f3562017-10-09 17:46:23 +01002451 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04002452#endif
David Barkerd92f3562017-10-09 17:46:23 +01002453 {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002454 mbmi->mode = ZEROMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455 } else {
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002456 if (is_compound) mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002457#if CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002458 else if (is_singleref_comp_mode)
2459 mbmi->mode = read_inter_singleref_comp_mode(xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002460#endif // CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002461 else
2462 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
2463 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002464#if CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002465 mbmi->mode == SR_NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002466#endif // CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002467 have_nearmv_in_inter_mode(mbmi->mode))
2468 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002469 }
2470
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002471 if (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002472 for (ref = 0; ref < 1 + is_compound; ++ref) {
RogerZhou3b635242017-09-19 10:06:46 -07002473#if CONFIG_AMVR
2474 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2475 &nearestmv[ref], &nearmv[ref],
RogerZhou10a03802017-10-26 11:49:48 -07002476 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002477#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002478 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2479 &nearestmv[ref], &nearmv[ref]);
RogerZhou3b635242017-09-19 10:06:46 -07002480#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002481 }
2482 }
2483
Zoe Liu85b66462017-04-20 14:28:19 -07002484#if CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002485 if ((is_compound || is_singleref_comp_mode) && mbmi->mode != ZERO_ZEROMV)
2486#else // !CONFIG_COMPOUND_SINGLEREF
2487 if (is_compound && 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,
RogerZhou10a03802017-10-26 11:49:48 -07002498 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002499 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07002500 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002501#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,
RogerZhou10a03802017-10-26 11:49:48 -07002515 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002516#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,
RogerZhou10a03802017-10-26 11:49:48 -07002523 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002524#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,
RogerZhou10a03802017-10-26 11:49:48 -07002539 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002540#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,
RogerZhou10a03802017-10-26 11:49:48 -07002549 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002550#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
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002571 int_mv ref_mv[2];
2572 ref_mv[0] = nearestmv[0];
2573 ref_mv[1] = nearestmv[1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002575 if (is_compound) {
2576 int ref_mv_idx = mbmi->ref_mv_idx;
2577 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2578 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2579 // mbmi->ref_mv_idx (like NEWMV)
2580 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2581 ref_mv_idx = 1 + mbmi->ref_mv_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002583 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
2584 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2585 if (xd->ref_mv_count[ref_frame_type] > 1) {
2586 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2587 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2588 xd->n8_h << MI_SIZE_LOG2, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002589 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002590 nearestmv[0] = ref_mv[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002591 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002592 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
2593 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2594 if (xd->ref_mv_count[ref_frame_type] > 1) {
2595 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
2596 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
2597 xd->n8_h << MI_SIZE_LOG2, xd);
David Barker404b2e82017-03-27 13:07:47 +01002598 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002599 nearestmv[1] = ref_mv[1];
2600 }
Zoe Liu85b66462017-04-20 14:28:19 -07002601#if CONFIG_COMPOUND_SINGLEREF
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002602 } else if (is_singleref_comp_mode) {
2603 int ref_mv_idx = mbmi->ref_mv_idx;
2604 // Special case: SR_NEAR_NEWMV use 1 + mbmi->ref_mv_idx (like NEARMV)
2605 // instead of mbmi->ref_mv_idx (like NEWMV)
2606 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
Zoe Liu85b66462017-04-20 14:28:19 -07002607
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002608 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
2609 compound_ref1_mode(mbmi->mode) == NEWMV) {
2610 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2611 if (xd->ref_mv_count[ref_frame_type] > 1) {
2612 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2613 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2614 xd->n8_h << MI_SIZE_LOG2, xd);
2615 }
2616 // TODO(zoeliu): To further investigate why this would not cause a
2617 // mismatch for the mode of SR_NEAREST_NEWMV.
2618 nearestmv[0] = ref_mv[0];
2619 }
2620#endif // CONFIG_COMPOUND_SINGLEREF
2621 } else {
2622 if (mbmi->mode == NEWMV) {
2623 for (ref = 0; ref < 1 + is_compound; ++ref) {
Zoe Liu85b66462017-04-20 14:28:19 -07002624 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2625 if (xd->ref_mv_count[ref_frame_type] > 1) {
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002626 ref_mv[ref] =
2627 (ref == 0)
2628 ? xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv
2629 : xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].comp_mv;
2630 clamp_mv_ref(&ref_mv[ref].as_mv, xd->n8_w << MI_SIZE_LOG2,
Zoe Liu85b66462017-04-20 14:28:19 -07002631 xd->n8_h << MI_SIZE_LOG2, xd);
2632 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002633 nearestmv[ref] = ref_mv[ref];
David Barker3dfba992017-04-03 16:10:09 +01002634 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636 }
2637
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002638 int mv_corrupted_flag =
2639 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
2640 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
2641 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
2642
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002643#if CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644 mbmi->use_wedge_interintra = 0;
2645 if (cm->reference_mode != COMPOUND_REFERENCE &&
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002646 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002648#if CONFIG_NEW_MULTISYMBOL
2649 const int interintra =
2650 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
2651#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002652 const int interintra =
2653 aom_read(r, cm->fc->interintra_prob[bsize_group], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002654#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002656 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657 if (interintra) {
2658 const INTERINTRA_MODE interintra_mode =
2659 read_interintra_mode(cm, xd, r, bsize_group);
2660 mbmi->ref_frame[1] = INTRA_FRAME;
2661 mbmi->interintra_mode = interintra_mode;
2662#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663 mbmi->angle_delta[0] = 0;
2664 mbmi->angle_delta[1] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002666#if CONFIG_FILTER_INTRA
2667 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2668 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2669#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002670 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002671#if CONFIG_NEW_MULTISYMBOL
2672 mbmi->use_wedge_interintra = aom_read_symbol(
2673 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
2674#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 mbmi->use_wedge_interintra =
Michael Bebenita6048d052016-08-25 14:40:54 -07002676 aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002677#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002678 if (xd->counts)
2679 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2680 if (mbmi->use_wedge_interintra) {
2681 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002682 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002683 mbmi->interintra_wedge_sign = 0;
2684 }
2685 }
2686 }
2687 }
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002688#endif // CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689
Yue Chen52c51732017-07-11 15:08:30 -07002690 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
2691 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2692 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2693
2694 xd->block_refs[ref] = ref_buf;
2695 }
Yue Chen52c51732017-07-11 15:08:30 -07002696
Yue Chencb60b182016-10-13 15:18:22 -07002697 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yue Chen69f18e12016-09-08 14:48:15 -07002698 if (mbmi->sb_type >= BLOCK_8X8 && !has_second_ref(mbmi))
Yunqing Wang97d6a372017-10-09 14:15:15 -07002699#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang1bc82862017-06-28 15:49:48 -07002700 mbmi->num_proj_ref[0] =
2701 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv);
2702#else
Yue Chen69f18e12016-09-08 14:48:15 -07002703 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang97d6a372017-10-09 14:15:15 -07002704#endif // CONFIG_EXT_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002705 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
Yue Chen69f18e12016-09-08 14:48:15 -07002706
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002707 if (mbmi->ref_frame[1] != INTRA_FRAME)
2708 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002709
2710#if CONFIG_NCOBMC_ADAPT_WEIGHT
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002711 read_ncobmc_mode(xd, mi, mbmi->ncobmc_mode, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002712#endif
2713
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002714#if CONFIG_COMPOUND_SINGLEREF
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002715 if (is_singleref_comp_mode) assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02002716#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002717
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002718 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Zoe Liu85b66462017-04-20 14:28:19 -07002719 if (
2720#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu0c634c72017-06-19 07:06:28 -07002721 is_inter_anyref_comp_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002722#else // !CONFIG_COMPOUND_SINGLEREF
2723 cm->reference_mode != SINGLE_REFERENCE &&
Sarah Parker6fdc8532016-11-16 17:47:13 -08002724 is_inter_compound_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002725#endif // CONFIG_COMPOUND_SINGLEREF
Zoe Liuf704a1c2017-10-02 16:55:59 -07002726 && mbmi->motion_mode == SIMPLE_TRANSLATION
2727#if CONFIG_EXT_SKIP
2728 && !mbmi->skip_mode
2729#endif // CONFIG_EXT_SKIP
2730 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002731 if (is_any_masked_compound_used(bsize)) {
Cheng Chenbdd6ca82017-10-23 22:34:25 -07002732#if CONFIG_JNT_COMP
2733 if (cm->allow_masked_compound && mbmi->compound_idx)
2734#else
2735 if (cm->allow_masked_compound)
2736#endif // CONFIG_JNT_COMP
2737 {
Sarah Parker680b9b12017-08-16 18:55:34 -07002738 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
2739 mbmi->interinter_compound_type =
2740 aom_read_bit(r, ACCT_STR) ? COMPOUND_AVERAGE : COMPOUND_SEG;
2741 else
Cheng Chenbdd6ca82017-10-23 22:34:25 -07002742 mbmi->interinter_compound_type = aom_read_symbol(
2743 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002744 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002745 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002746 mbmi->wedge_index =
2747 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2748 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2749 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002750 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
Cheng Chenbdd6ca82017-10-23 22:34:25 -07002751 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002752 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002753 }
Sarah Parker42d96102017-01-31 21:05:27 -08002754 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002755 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002756 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002757 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002758 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002759 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002760
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002761 read_mb_interp_filter(cm, xd, mbmi, r);
Zoe Liu85b66462017-04-20 14:28:19 -07002762
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002763 if (mbmi->motion_mode == WARPED_CAUSAL) {
2764 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
2765
2766#if CONFIG_EXT_WARPED_MOTION
2767 if (mbmi->num_proj_ref[0] > 1)
2768 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2769 pts_inref, mbmi->num_proj_ref[0]);
2770#endif // CONFIG_EXT_WARPED_MOTION
2771
2772 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2773 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2774 &mbmi->wm_params[0], mi_row, mi_col)) {
2775#if DEC_MISMATCH_DEBUG
2776 printf("Warning: unexpected warped model from aomenc\n");
2777#endif
2778 mbmi->wm_params[0].invalid = 1;
2779 }
2780 }
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002781
Di Chen56586622017-06-09 13:49:44 -07002782#if DEC_MISMATCH_DEBUG
Zoe Liuf704a1c2017-10-02 16:55:59 -07002783 dec_dump_logs(cm, mi, mi_row, mi_col, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002784#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002785}
2786
Yaowu Xuf883b422016-08-30 14:01:10 -07002787static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002788 MACROBLOCKD *const xd, int mi_row,
2789 int mi_col, aom_reader *r) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002790 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002791 MODE_INFO *const mi = xd->mi[0];
2792 MB_MODE_INFO *const mbmi = &mi->mbmi;
2793 int inter_block = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002794 BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795
2796 mbmi->mv[0].as_int = 0;
2797 mbmi->mv[1].as_int = 0;
2798 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
David Barker3aec8d62017-01-31 14:55:32 +00002799
Zoe Liuf704a1c2017-10-02 16:55:59 -07002800#if CONFIG_EXT_SKIP
2801 mbmi->skip_mode = read_skip_mode(cm, xd, mbmi->segment_id, r);
2802#if 0
2803 if (mbmi->skip_mode)
2804 printf("Frame=%d, frame_offset=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d\n",
2805 cm->current_video_frame, cm->frame_offset, mi_row, mi_col,
2806 mbmi->skip_mode);
2807#endif // 0
2808
2809 if (mbmi->skip_mode) {
2810 mbmi->skip = 1;
2811
2812 if (cm->delta_q_present_flag) {
2813 xd->current_qindex = xd->prev_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07002814#if CONFIG_EXT_DELTA_Q
Zoe Liuf704a1c2017-10-02 16:55:59 -07002815 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07002816#if CONFIG_LOOPFILTER_LEVEL
Zoe Liuf704a1c2017-10-02 16:55:59 -07002817 if (cm->delta_lf_multi)
2818 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id)
2819 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
2820 xd->prev_delta_lf[lf_id];
2821 else
2822#endif // CONFIG_LOOPFILTER_LEVEL
2823 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2824 xd->prev_delta_lf_from_base;
2825 }
2826#endif // CONFIG_EXT_DELTA_Q
2827 }
2828
2829 update_block_intra_inter(cm, xd, mbmi->segment_id, inter_block);
2830 } else {
2831#endif // CONFIG_EXT_SKIP
2832 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
2833
2834 if (cm->delta_q_present_flag) {
2835 xd->current_qindex =
2836 xd->prev_qindex +
2837 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
2838 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2839 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
2840 xd->prev_qindex = xd->current_qindex;
2841#if CONFIG_EXT_DELTA_Q
2842 if (cm->delta_lf_present_flag) {
2843#if CONFIG_LOOPFILTER_LEVEL
2844 if (cm->delta_lf_multi) {
2845 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
2846 const int tmp_lvl =
2847 xd->prev_delta_lf[lf_id] +
2848 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
2849 cm->delta_lf_res;
2850 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
2851 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
2852 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
2853 }
2854 } else {
Cheng Chenaff479f2017-10-26 16:53:10 -07002855 const int tmp_lvl =
Zoe Liuf704a1c2017-10-02 16:55:59 -07002856 xd->prev_delta_lf_from_base +
2857 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chen880166a2017-10-02 17:48:48 -07002858 cm->delta_lf_res;
Zoe Liuf704a1c2017-10-02 16:55:59 -07002859 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
Cheng Chenaff479f2017-10-26 16:53:10 -07002860 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Zoe Liuf704a1c2017-10-02 16:55:59 -07002861 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chen880166a2017-10-02 17:48:48 -07002862 }
Zoe Liuf704a1c2017-10-02 16:55:59 -07002863#else
2864 const int current_delta_lf_from_base =
Cheng Chen880166a2017-10-02 17:48:48 -07002865 xd->prev_delta_lf_from_base +
Zoe Liuf704a1c2017-10-02 16:55:59 -07002866 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07002867 cm->delta_lf_res;
Cheng Chenaff479f2017-10-26 16:53:10 -07002868 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
Zoe Liuf704a1c2017-10-02 16:55:59 -07002869 clamp(current_delta_lf_from_base, -MAX_LOOP_FILTER,
2870 MAX_LOOP_FILTER);
Cheng Chen880166a2017-10-02 17:48:48 -07002871 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07002872#endif // CONFIG_LOOPFILTER_LEVEL
Zoe Liuf704a1c2017-10-02 16:55:59 -07002873 }
2874#endif // CONFIG_EXT_DELTA_Q
Fangwen Fu231fe422017-04-24 17:52:29 -07002875 }
Zoe Liuf704a1c2017-10-02 16:55:59 -07002876
2877 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
2878#if CONFIG_EXT_SKIP
David Barker3aec8d62017-01-31 14:55:32 +00002879 }
Zoe Liuf704a1c2017-10-02 16:55:59 -07002880#endif // CONFIG_EXT_SKIP
David Barker3aec8d62017-01-31 14:55:32 +00002881
Yushin Choc24351c2017-10-24 14:59:08 -07002882 mbmi->current_q_index = xd->current_qindex;
2883
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002884 xd->above_txfm_context =
2885 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2886 xd->left_txfm_context = xd->left_txfm_context_buffer +
2887 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002888
Debargha Mukherjee4def76a2017-10-19 13:38:35 -07002889 if (cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
2890 !mbmi->skip && inter_block && !xd->lossless[mbmi->segment_id]) {
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002891 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
2892 const int bh = tx_size_high_unit[max_tx_size];
2893 const int bw = tx_size_wide_unit[max_tx_size];
2894 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2895 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
2896 int idx, idy;
2897 int init_depth =
2898 (height != width) ? RECT_VARTX_DEPTH_INIT : SQR_VARTX_DEPTH_INIT;
Yue Chena1e48dc2016-08-29 17:29:33 -07002899
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002900 mbmi->min_tx_size = TX_SIZES_ALL;
2901 for (idy = 0; idy < height; idy += bh)
2902 for (idx = 0; idx < width; idx += bw)
2903 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size, init_depth,
2904 idy, idx, r);
Yue Chend6bdd462017-07-19 16:05:43 -07002905#if CONFIG_RECT_TX_EXT
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002906 if (is_quarter_tx_allowed(xd, mbmi, inter_block) &&
2907 mbmi->tx_size == max_tx_size) {
2908 int quarter_tx;
Yue Chend6bdd462017-07-19 16:05:43 -07002909
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002910 if (quarter_txsize_lookup[bsize] != max_tx_size) {
Thomas Daviese3f69782017-10-03 10:43:17 +01002911#if CONFIG_NEW_MULTISYMBOL
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002912 quarter_tx =
2913 aom_read_symbol(r, cm->fc->quarter_tx_size_cdf, 2, ACCT_STR);
Thomas Daviese3f69782017-10-03 10:43:17 +01002914#else
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002915 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
2916 if (xd->counts) ++xd->counts->quarter_tx_size[quarter_tx];
Thomas Daviese3f69782017-10-03 10:43:17 +01002917#endif
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002918 } else {
2919 quarter_tx = 1;
Yue Chend6bdd462017-07-19 16:05:43 -07002920 }
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002921 if (quarter_tx) {
2922 mbmi->tx_size = quarter_txsize_lookup[bsize];
2923 for (idy = 0; idy < tx_size_high_unit[max_tx_size] / 2; ++idy)
2924 for (idx = 0; idx < tx_size_wide_unit[max_tx_size] / 2; ++idx)
2925 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
2926 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002928 }
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002929#endif
2930 } else {
2931 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
2932
Zoe Liuf704a1c2017-10-02 16:55:59 -07002933#if CONFIG_EXT_SKIP
2934 if (!mbmi->skip_mode) {
2935#endif // CONFIG_EXT_SKIP
2936 if (inter_block) {
2937 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2938 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
2939 int idx, idy;
2940 for (idy = 0; idy < height; ++idy)
2941 for (idx = 0; idx < width; ++idx)
2942 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
2943 }
2944 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
2945 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
2946#if CONFIG_EXT_SKIP
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002947 }
Zoe Liuf704a1c2017-10-02 16:55:59 -07002948#endif // CONFIG_EXT_SKIP
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002949 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950
2951 if (inter_block)
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002952 read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002953 else
Jingning Han36fe3202017-02-20 22:31:49 -08002954 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002956#if !CONFIG_TXK_SEL
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002957 av1_read_tx_type(cm, xd, r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002958#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959}
2960
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002961static void av1_intra_copy_frame_mvs(AV1_COMMON *const cm, int mi_row,
2962 int mi_col, int x_mis, int y_mis) {
Jingning Hanc7d198e2017-10-12 16:11:06 -07002963#if CONFIG_TMV || CONFIG_MFMV
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002964 const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_cols, 1);
Yunqing Wangc99a5642017-10-11 13:48:22 -07002965 MV_REF *frame_mvs =
2966 cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002967 x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
2968 y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
2969#else
2970 const int frame_mvs_stride = cm->mi_cols;
2971 MV_REF *frame_mvs = cm->cur_frame->mvs +
2972 (mi_row & 0xfffe) * frame_mvs_stride + (mi_col & 0xfffe);
2973 x_mis = AOMMAX(x_mis, 2);
2974 y_mis = AOMMAX(y_mis, 2);
2975#endif // CONFIG_TMV
2976 int w, h;
2977
2978 for (h = 0; h < y_mis; h++) {
Yunqing Wangb90a97a2017-10-24 11:50:15 -07002979 MV_REF *mv = frame_mvs;
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002980 for (w = 0; w < x_mis; w++) {
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002981 mv->ref_frame[0] = NONE_FRAME;
2982 mv->ref_frame[1] = NONE_FRAME;
Yunqing Wangb90a97a2017-10-24 11:50:15 -07002983 mv++;
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002984 }
Yunqing Wangb90a97a2017-10-24 11:50:15 -07002985 frame_mvs += frame_mvs_stride;
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002986 }
2987}
2988
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002989void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd, int mi_row,
2990 int mi_col, aom_reader *r, int x_mis, int y_mis) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002991 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002992 MODE_INFO *const mi = xd->mi[0];
Alex Converse28744302017-04-13 14:46:22 -07002993#if CONFIG_INTRABC
2994 mi->mbmi.use_intrabc = 0;
2995#endif // CONFIG_INTRABC
2996
Yaowu Xuc27fc142016-08-22 16:08:15 -07002997 if (frame_is_intra_only(cm)) {
2998 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002999 av1_intra_copy_frame_mvs(cm, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003000 } else {
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02003001 read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07003002 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003 }
3004}