blob: 7c85442838b73ae1cfb54a7bb8ad2c030c58f08b [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13
14#include "av1/common/common.h"
15#include "av1/common/entropy.h"
16#include "av1/common/entropymode.h"
17#include "av1/common/entropymv.h"
18#include "av1/common/mvref_common.h"
19#include "av1/common/pred_common.h"
20#include "av1/common/reconinter.h"
hui su45dc5972016-12-08 17:42:50 -080021#if CONFIG_EXT_INTRA
22#include "av1/common/reconintra.h"
23#endif // CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "av1/common/seg_common.h"
Yue Chen69f18e12016-09-08 14:48:15 -070025#if CONFIG_WARPED_MOTION
26#include "av1/common/warped_motion.h"
27#endif // CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#include "av1/decoder/decodeframe.h"
Jingning Han1aab8182016-06-03 11:09:06 -070030#include "av1/decoder/decodemv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031
Yaowu Xuf883b422016-08-30 14:01:10 -070032#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033
Michael Bebenita6048d052016-08-25 14:40:54 -070034#define ACCT_STR __func__
Zoe Liu85b66462017-04-20 14:28:19 -070035
Di Chen56586622017-06-09 13:49:44 -070036#define DEC_MISMATCH_DEBUG 0
Zoe Liu85b66462017-04-20 14:28:19 -070037
Thomas9ac55082016-09-23 18:04:17 +010038static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
Nathan E. Egge3ef926e2016-09-07 18:20:41 -040039 return (PREDICTION_MODE)
40 av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
41}
Yaowu Xuc27fc142016-08-22 16:08:15 -070042
Thomas Daviesf6936102016-09-05 16:51:31 +010043#if CONFIG_DELTA_Q
44static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
45 MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
46 FRAME_COUNTS *counts = xd->counts;
47 int sign, abs, reduced_delta_qindex = 0;
48 BLOCK_SIZE bsize = mbmi->sb_type;
49 const int b_col = mi_col & MAX_MIB_MASK;
50 const int b_row = mi_row & MAX_MIB_MASK;
51 const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000052 int rem_bits, thr;
53 int i, smallval;
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000054 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
55 (void)cm;
Thomas Daviesf6936102016-09-05 16:51:31 +010056
Alex Converse68abef82017-03-23 14:50:33 -070057 if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000058 abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000059 smallval = (abs < DELTA_Q_SMALL);
60 if (counts) {
61 for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
62 if (smallval) counts->delta_q[abs][0]++;
63 }
64
65 if (!smallval) {
Thomas Daviesf6936102016-09-05 16:51:31 +010066 rem_bits = aom_read_literal(r, 3, ACCT_STR);
67 thr = (1 << rem_bits) + 1;
68 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
69 }
70
71 if (abs) {
72 sign = aom_read_bit(r, ACCT_STR);
73 } else {
74 sign = 1;
75 }
76
77 reduced_delta_qindex = sign ? -abs : abs;
78 }
79 return reduced_delta_qindex;
80}
Fangwen Fu231fe422017-04-24 17:52:29 -070081#if CONFIG_EXT_DELTA_Q
82static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
83 MB_MODE_INFO *const mbmi, int mi_col,
84 int mi_row) {
85 FRAME_COUNTS *counts = xd->counts;
86 int sign, abs, reduced_delta_lflevel = 0;
87 BLOCK_SIZE bsize = mbmi->sb_type;
88 const int b_col = mi_col & MAX_MIB_MASK;
89 const int b_row = mi_row & MAX_MIB_MASK;
90 const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
91 int rem_bits, thr;
92 int i, smallval;
Fangwen Fu231fe422017-04-24 17:52:29 -070093 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
94 (void)cm;
Fangwen Fu231fe422017-04-24 17:52:29 -070095
96 if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_lf_flag) {
Fangwen Fu231fe422017-04-24 17:52:29 -070097 abs =
98 aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
Fangwen Fu231fe422017-04-24 17:52:29 -070099 smallval = (abs < DELTA_LF_SMALL);
100 if (counts) {
101 for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
102 if (smallval) counts->delta_lf[abs][0]++;
103 }
104 if (!smallval) {
105 rem_bits = aom_read_literal(r, 3, ACCT_STR);
106 thr = (1 << rem_bits) + 1;
107 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
108 }
109
110 if (abs) {
111 sign = aom_read_bit(r, ACCT_STR);
112 } else {
113 sign = 1;
114 }
115
116 reduced_delta_lflevel = sign ? -abs : abs;
117 }
118 return reduced_delta_lflevel;
119}
120#endif
Thomas Daviesf6936102016-09-05 16:51:31 +0100121#endif
122
Nathan E. Eggea1f80e32017-05-23 11:52:32 -0400123static PREDICTION_MODE read_intra_mode_y(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700124 aom_reader *r, int size_group) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 const PREDICTION_MODE y_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000126 read_intra_mode(r, ec_ctx->y_mode_cdf[size_group]);
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400127#if CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 FRAME_COUNTS *counts = xd->counts;
129 if (counts) ++counts->y_mode[size_group][y_mode];
Nathan E. Egge5bb3a742017-06-30 12:47:43 -0400130#else
131 /* TODO(negge): Can we remove this parameter? */
132 (void)xd;
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400133#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134 return y_mode;
135}
136
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400137static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
138 MACROBLOCKD *xd, aom_reader *r,
139 PREDICTION_MODE y_mode) {
140 const UV_PREDICTION_MODE uv_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000141 read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400142#if CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700143 FRAME_COUNTS *counts = xd->counts;
144 if (counts) ++counts->uv_mode[y_mode][uv_mode];
Nathan E. Egge5bb3a742017-06-30 12:47:43 -0400145#else
146 /* TODO(negge): Can we remove this parameter? */
147 (void)xd;
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -0400148#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 return uv_mode;
150}
151
Luc Trudeauf5334002017-04-25 12:21:26 -0400152#if CONFIG_CFL
David Michael Barr23198662017-06-19 23:19:48 +0900153static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
Luc Trudeau2c317902017-04-28 11:06:50 -0400154 CFL_SIGN_TYPE signs_out[CFL_PRED_PLANES]) {
David Michael Barr23198662017-06-19 23:19:48 +0900155 const int ind =
156 aom_read_symbol(r, ec_ctx->cfl_alpha_cdf, CFL_ALPHABET_SIZE, "cfl:alpha");
157 // Signs are only coded for nonzero values
158 // sign == 0 implies negative alpha
159 // sign == 1 implies positive alpha
160 signs_out[CFL_PRED_U] = cfl_alpha_codes[ind][CFL_PRED_U]
161 ? aom_read_bit(r, "cfl:sign")
162 : CFL_SIGN_POS;
163 signs_out[CFL_PRED_V] = cfl_alpha_codes[ind][CFL_PRED_V]
164 ? aom_read_bit(r, "cfl:sign")
165 : CFL_SIGN_POS;
Luc Trudeauf5334002017-04-25 12:21:26 -0400166
David Michael Barr23198662017-06-19 23:19:48 +0900167 return ind;
Luc Trudeauf5334002017-04-25 12:21:26 -0400168}
169#endif
170
Yue Chen4d26acb2017-05-01 12:28:34 -0700171#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700172static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
173 aom_reader *r, int size_group) {
Thomas Davies299ff042017-06-27 13:41:59 +0100174 (void)cm;
175 const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
176 r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
177 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 FRAME_COUNTS *counts = xd->counts;
179 if (counts) ++counts->interintra_mode[size_group][ii_mode];
180 return ii_mode;
181}
Yue Chen4d26acb2017-05-01 12:28:34 -0700182#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183
Thomas Davies1de6c882017-01-11 17:47:49 +0000184static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700185 aom_reader *r, int16_t ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 FRAME_COUNTS *counts = xd->counts;
187 int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100188 int is_newmv, is_zeromv, is_refmv;
189#if CONFIG_NEW_MULTISYMBOL
190 is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
191#else
192 is_newmv = aom_read(r, ec_ctx->newmv_prob[mode_ctx], ACCT_STR) == 0;
193#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194
Thomas Davies149eda52017-06-12 18:11:55 +0100195 if (is_newmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 if (counts) ++counts->newmv_mode[mode_ctx][0];
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700197 return NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198 }
199 if (counts) ++counts->newmv_mode[mode_ctx][1];
200
201 if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
202
203 mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
204
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];
216
217 mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
218
219 if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
220 if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
221 if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
222
Thomas Davies149eda52017-06-12 18:11:55 +0100223#if CONFIG_NEW_MULTISYMBOL
224 is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
225#else
226 is_refmv = aom_read(r, ec_ctx->refmv_prob[mode_ctx], ACCT_STR) == 0;
227#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700228
Thomas Davies149eda52017-06-12 18:11:55 +0100229 if (is_refmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700230 if (counts) ++counts->refmv_mode[mode_ctx][0];
231
232 return NEARESTMV;
233 } else {
234 if (counts) ++counts->refmv_mode[mode_ctx][1];
235 return NEARMV;
236 }
237
238 // Invalid prediction mode.
239 assert(0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240}
241
Thomas Davies149eda52017-06-12 18:11:55 +0100242static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700243 MB_MODE_INFO *mbmi, aom_reader *r) {
244 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 mbmi->ref_mv_idx = 0;
246
David Barker404b2e82017-03-27 13:07:47 +0100247#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -0700248#if CONFIG_COMPOUND_SINGLEREF
249 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
250 mbmi->mode == SR_NEW_NEWMV) {
251#else // !CONFIG_COMPOUND_SINGLEREF
David Barker404b2e82017-03-27 13:07:47 +0100252 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
Zoe Liu85b66462017-04-20 14:28:19 -0700253#endif // CONFIG_COMPOUND_SINGLEREF
254#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700255 if (mbmi->mode == NEWMV) {
Zoe Liu85b66462017-04-20 14:28:19 -0700256#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 int idx;
258 for (idx = 0; idx < 2; ++idx) {
259 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700260 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100261#if CONFIG_NEW_MULTISYMBOL
262 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
263#else
264 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
265#endif
266 mbmi->ref_mv_idx = idx + drl_idx;
267 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
268 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 }
270 }
271 }
272
David Barker3dfba992017-04-03 16:10:09 +0100273 if (have_nearmv_in_inter_mode(mbmi->mode)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 int idx;
275 // Offset the NEARESTMV mode.
276 // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
277 // mode is factored in.
278 for (idx = 1; idx < 3; ++idx) {
279 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700280 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100281#if CONFIG_NEW_MULTISYMBOL
282 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
283#else
284 int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
285#endif
286 mbmi->ref_mv_idx = idx + drl_idx - 1;
287 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
288 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 }
290 }
291 }
292}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293
Yaowu Xub24e1152016-10-31 16:28:32 -0700294#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
295static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -0800296 MODE_INFO *mi, aom_reader *r) {
297 MB_MODE_INFO *mbmi = &mi->mbmi;
Thomas Davies04e5aa72017-06-28 14:36:39 +0100298#if CONFIG_NEW_MULTISYMBOL
299 (void)cm;
300#endif
301
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700302#if CONFIG_NCOBMC_ADAPT_WEIGHT
303 const MOTION_MODE last_motion_mode_allowed =
304 motion_mode_allowed_wrapper(0,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700305#if CONFIG_GLOBAL_MOTION
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700306 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700307#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -0700308#if CONFIG_WARPED_MOTION
309 xd,
310#endif
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700311 mi);
312#else
Sarah Parker19234cc2017-03-10 16:43:25 -0800313 const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
Sarah Parker0eea89f2017-07-11 11:56:36 -0700314#if CONFIG_GLOBAL_MOTION
Sarah Parker19234cc2017-03-10 16:43:25 -0800315 0, xd->global_motion,
Sarah Parker0eea89f2017-07-11 11:56:36 -0700316#endif // CONFIG_GLOBAL_MOTION
Yue Chen52c51732017-07-11 15:08:30 -0700317#if CONFIG_WARPED_MOTION
318 xd,
319#endif
Sarah Parker19234cc2017-03-10 16:43:25 -0800320 mi);
Wei-Ting Lind0f7ba12017-06-30 14:59:57 -0700321#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yue Chen69f18e12016-09-08 14:48:15 -0700322 int motion_mode;
323 FRAME_COUNTS *counts = xd->counts;
Yaowu Xub24e1152016-10-31 16:28:32 -0700324
Yue Chen69f18e12016-09-08 14:48:15 -0700325 if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
326#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
327 if (last_motion_mode_allowed == OBMC_CAUSAL) {
Thomas Daviesd9b57262017-06-27 17:43:25 +0100328#if CONFIG_NEW_MULTISYMBOL
329 motion_mode =
330 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
331#else
Yue Chen69f18e12016-09-08 14:48:15 -0700332 motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
Thomas Daviesd9b57262017-06-27 17:43:25 +0100333#endif
Yue Chen69f18e12016-09-08 14:48:15 -0700334 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
335 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
336 } else {
337#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700338 motion_mode =
Thomas Davies04e5aa72017-06-28 14:36:39 +0100339 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
340 MOTION_MODES, ACCT_STR);
Yaowu Xub24e1152016-10-31 16:28:32 -0700341 if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
342 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yue Chen69f18e12016-09-08 14:48:15 -0700343#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700344 }
Yue Chen69f18e12016-09-08 14:48:15 -0700345#endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xub24e1152016-10-31 16:28:32 -0700346}
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700347
348#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -0700349static void read_ncobmc_mode(MACROBLOCKD *xd, MODE_INFO *mi,
350#ifndef TRAINING_WEIGHTS
351 NCOBMC_MODE ncobmc_mode[2],
352#else
353 NCOBMC_MODE ncobmc_mode[][4],
354#endif
355 aom_reader *r) {
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700356 MB_MODE_INFO *mbmi = &mi->mbmi;
357 FRAME_COUNTS *counts = xd->counts;
358 ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];
Wei-Ting Lin77c41182017-07-12 15:58:35 -0700359 if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) return;
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700360
Wei-Ting Linca710d62017-07-13 11:41:02 -0700361#ifndef TRAINING_WEIGHTS
362 ncobmc_mode[0] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
363 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700364 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0]];
365
366 if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
Wei-Ting Linca710d62017-07-13 11:41:02 -0700367 ncobmc_mode[1] = aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
368 MAX_NCOBMC_MODES, ACCT_STR);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700369 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1]];
370 }
Wei-Ting Linca710d62017-07-13 11:41:02 -0700371#else
372 int i;
373 for (i = 0; i < 4; ++i) {
374 ncobmc_mode[0][i] = aom_read_symbol(
375 r, xd->tile_ctx->ncobmc_mode_cdf[ao_block], MAX_NCOBMC_MODES, ACCT_STR);
376 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0][i]];
377 }
378 if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
379 for (i = 0; i < 4; ++i) {
380 ncobmc_mode[1][i] =
381 aom_read_symbol(r, xd->tile_ctx->ncobmc_mode_cdf[ao_block],
382 MAX_NCOBMC_MODES, ACCT_STR);
383 if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1][i]];
384 }
385 }
386#endif
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700387}
388#endif
Yaowu Xub24e1152016-10-31 16:28:32 -0700389#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
390
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700392static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
393 aom_reader *r, int16_t ctx) {
Thomas Davies8c08a332017-06-26 17:30:34 +0100394 (void)cm;
395 const int mode =
396 aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
397 INTER_COMPOUND_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398 FRAME_COUNTS *counts = xd->counts;
399
400 if (counts) ++counts->inter_compound_mode[ctx][mode];
401
402 assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
403 return NEAREST_NEARESTMV + mode;
404}
Zoe Liu85b66462017-04-20 14:28:19 -0700405
406#if CONFIG_COMPOUND_SINGLEREF
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100407static PREDICTION_MODE read_inter_singleref_comp_mode(MACROBLOCKD *xd,
Zoe Liu85b66462017-04-20 14:28:19 -0700408 aom_reader *r,
409 int16_t ctx) {
410 const int mode =
Thomas Daviesb8b14a92017-07-12 15:11:49 +0100411 aom_read_symbol(r, xd->tile_ctx->inter_singleref_comp_mode_cdf[ctx],
412 INTER_SINGLEREF_COMP_MODES, ACCT_STR);
Zoe Liu85b66462017-04-20 14:28:19 -0700413 FRAME_COUNTS *counts = xd->counts;
414
415 if (counts) ++counts->inter_singleref_comp_mode[ctx][mode];
416
417 assert(is_inter_singleref_comp_mode(SR_NEAREST_NEARMV + mode));
418 return SR_NEAREST_NEARMV + mode;
419}
420#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421#endif // CONFIG_EXT_INTER
422
Thomas9ac55082016-09-23 18:04:17 +0100423static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700424 return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425}
426
427#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700428static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429 MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700430 TX_SIZE tx_size, int depth, int blk_row,
431 int blk_col, aom_reader *r) {
Thomas Davies985bfc32017-06-27 16:51:26 +0100432#if CONFIG_NEW_MULTISYMBOL
433 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
434 (void)cm;
435#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700436 int is_split = 0;
437 const int tx_row = blk_row >> 1;
438 const int tx_col = blk_col >> 1;
Jingning Hanf64062f2016-11-02 16:22:18 -0700439 const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
440 const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
Jingning Han331662e2017-05-30 17:03:32 -0700441 int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
442 xd->left_txfm_context + blk_row,
Jingning Hanc8b89362016-11-01 10:28:53 -0700443 mbmi->sb_type, tx_size);
clang-format67948d32016-09-07 22:40:40 -0700444 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 [MAX_MIB_SIZE] =
446 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
448
Jingning Han571189c2016-10-24 10:38:43 -0700449 if (depth == MAX_VARTX_DEPTH) {
Jingning Han94d5bfc2016-10-21 10:14:36 -0700450 int idx, idy;
451 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700452 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
453 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han94d5bfc2016-10-21 10:14:36 -0700454 inter_tx_size[idy][idx] = tx_size;
455 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700456 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Jingning Han94d5bfc2016-10-21 10:14:36 -0700457 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700458 txfm_partition_update(xd->above_txfm_context + blk_col,
459 xd->left_txfm_context + blk_row, tx_size, tx_size);
Jingning Han94d5bfc2016-10-21 10:14:36 -0700460 return;
461 }
462
Thomas Davies985bfc32017-06-27 16:51:26 +0100463#if CONFIG_NEW_MULTISYMBOL
464 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
465#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700466 is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
Thomas Davies985bfc32017-06-27 16:51:26 +0100467#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468
469 if (is_split) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700470 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
471 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472 int i;
473
474 if (counts) ++counts->txfm_partition[ctx][1];
475
476 if (tx_size == TX_8X8) {
Jingning Han9ca05b72017-01-03 14:41:36 -0800477 int idx, idy;
Jingning Hanab9ecba2017-01-13 09:11:58 -0800478 inter_tx_size[0][0] = sub_txs;
Jingning Han9ca05b72017-01-03 14:41:36 -0800479 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
480 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Jingning Han581d1692017-01-05 16:03:54 -0800481 inter_tx_size[idy][idx] = inter_tx_size[0][0];
Jingning Hanab9ecba2017-01-13 09:11:58 -0800482 mbmi->tx_size = sub_txs;
Jingning Hane67b38a2016-11-04 10:30:00 -0700483 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han331662e2017-05-30 17:03:32 -0700484 txfm_partition_update(xd->above_txfm_context + blk_col,
485 xd->left_txfm_context + blk_row, sub_txs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700486 return;
487 }
488
489 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 for (i = 0; i < 4; ++i) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700491 int offsetr = blk_row + (i >> 1) * bsl;
492 int offsetc = blk_col + (i & 0x01) * bsl;
493 read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700494 offsetc, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700495 }
496 } else {
497 int idx, idy;
498 inter_tx_size[0][0] = tx_size;
Jingning Han65abc312016-10-27 13:04:21 -0700499 for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
500 for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501 inter_tx_size[idy][idx] = tx_size;
502 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700503 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700505 txfm_partition_update(xd->above_txfm_context + blk_col,
506 xd->left_txfm_context + blk_row, tx_size, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 }
508}
509#endif
510
Yaowu Xuf883b422016-08-30 14:01:10 -0700511static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
512 int tx_size_cat, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 FRAME_COUNTS *counts = xd->counts;
514 const int ctx = get_tx_size_context(xd);
Thomas Davies15580c52017-03-09 13:53:42 +0000515 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
516 (void)cm;
Thomas Davies15580c52017-03-09 13:53:42 +0000517
Nathan E. Egge476c63c2017-05-18 18:35:16 -0400518 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
519 tx_size_cat + 2, ACCT_STR);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800520 const TX_SIZE tx_size = depth_to_tx_size(depth);
521#if CONFIG_RECT_TX
522 assert(!is_rect_tx(tx_size));
523#endif // CONFIG_RECT_TX
Jingning Han906be072016-10-26 11:04:31 -0700524 if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
Jingning Han4e1737a2016-10-25 16:05:02 -0700525 return tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526}
527
Urvang Joshifeb925f2016-12-05 10:37:29 -0800528static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
529 int allow_select_inter, aom_reader *r) {
530 const TX_MODE tx_mode = cm->tx_mode;
531 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Debargha Mukherjee428bbb22017-03-17 07:30:24 -0700533#if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
Jingning Han3daa4fd2017-01-20 10:33:50 -0800534 if (bsize > BLOCK_4X4) {
Jingning Han4be1a4d2017-01-06 10:59:20 -0800535#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 if (bsize >= BLOCK_8X8) {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800537#endif // CONFIG_CB4X4 && CONFIG_VAR_TX
538 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
539 const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
540 : intra_tx_size_cat_lookup[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541 const TX_SIZE coded_tx_size =
Urvang Joshifeb925f2016-12-05 10:37:29 -0800542 read_selected_tx_size(cm, xd, tx_size_cat, r);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700543#if CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 if (coded_tx_size > max_txsize_lookup[bsize]) {
545 assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
Yue Chend6bdd462017-07-19 16:05:43 -0700546#if CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700547 if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
Yue Chend6bdd462017-07-19 16:05:43 -0700548 int quarter_tx;
Yue Chen56e226e2017-05-02 16:21:40 -0700549
Yue Chend6bdd462017-07-19 16:05:43 -0700550 if (quarter_txsize_lookup[bsize] != max_txsize_lookup[bsize]) {
551 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
552 FRAME_COUNTS *counts = xd->counts;
553
554 if (counts) ++counts->quarter_tx_size[quarter_tx];
555 } else {
556 quarter_tx = 1;
557 }
Yue Chen56e226e2017-05-02 16:21:40 -0700558 return quarter_tx ? quarter_txsize_lookup[bsize]
559 : max_txsize_rect_lookup[bsize];
560 }
Yue Chend6bdd462017-07-19 16:05:43 -0700561#endif // CONFIG_RECT_TX_EXT
Yue Chen56e226e2017-05-02 16:21:40 -0700562
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563 return max_txsize_rect_lookup[bsize];
564 }
Peter de Rivaza7c81462016-09-26 14:20:13 +0100565#else
566 assert(coded_tx_size <= max_txsize_lookup[bsize]);
Yue Chen3ca7dd92017-05-23 16:03:39 -0700567#endif // CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568 return coded_tx_size;
569 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800570 return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571 }
572 } else {
573#if CONFIG_EXT_TX && CONFIG_RECT_TX
574 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
575 return max_txsize_rect_lookup[bsize];
576#else
577 return TX_4X4;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800578#endif // CONFIG_EXT_TX && CONFIG_RECT_TX
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 }
580}
581
Yaowu Xuf883b422016-08-30 14:01:10 -0700582static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583 int mi_offset, int x_mis, int y_mis) {
584 int x, y, segment_id = INT_MAX;
585
586 for (y = 0; y < y_mis; y++)
587 for (x = 0; x < x_mis; x++)
588 segment_id =
Yaowu Xuf883b422016-08-30 14:01:10 -0700589 AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590
591 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
592 return segment_id;
593}
594
Yaowu Xuf883b422016-08-30 14:01:10 -0700595static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 int segment_id) {
597 int x, y;
598
599 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
600
601 for (y = 0; y < y_mis; y++)
602 for (x = 0; x < x_mis; x++)
603 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
604}
605
Yaowu Xuf883b422016-08-30 14:01:10 -0700606static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 int mi_offset, int x_mis, int y_mis,
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609 struct segmentation *const seg = &cm->seg;
610 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100611 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100612 struct segmentation_probs *const segp = &ec_ctx->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613 int segment_id;
614
615 if (!seg->enabled) return 0; // Default for disabled segmentation
616
617 assert(seg->update_map && !seg->temporal_update);
618
619 segment_id = read_segment_id(r, segp);
620 if (counts) ++counts->seg.tree_total[segment_id];
621 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
622 return segment_id;
623}
624
Yaowu Xuf883b422016-08-30 14:01:10 -0700625static void copy_segment_id(const AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 const uint8_t *last_segment_ids,
627 uint8_t *current_segment_ids, int mi_offset,
628 int x_mis, int y_mis) {
629 int x, y;
630
631 for (y = 0; y < y_mis; y++)
632 for (x = 0; x < x_mis; x++)
633 current_segment_ids[mi_offset + y * cm->mi_cols + x] =
634 last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
635 : 0;
636}
637
Yaowu Xuf883b422016-08-30 14:01:10 -0700638static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
639 int mi_row, int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 struct segmentation *const seg = &cm->seg;
641 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100642 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100643 struct segmentation_probs *const segp = &ec_ctx->seg;
644
Yaowu Xuc27fc142016-08-22 16:08:15 -0700645 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
646 int predicted_segment_id, segment_id;
647 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Hanc709e1f2016-12-06 14:48:09 -0800648 const int bw = mi_size_wide[mbmi->sb_type];
649 const int bh = mi_size_high[mbmi->sb_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700650
651 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
653 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700654
655 if (!seg->enabled) return 0; // Default for disabled segmentation
656
657 predicted_segment_id = cm->last_frame_seg_map
658 ? dec_get_segment_id(cm, cm->last_frame_seg_map,
659 mi_offset, x_mis, y_mis)
660 : 0;
661
662 if (!seg->update_map) {
663 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
664 mi_offset, x_mis, y_mis);
665 return predicted_segment_id;
666 }
667
668 if (seg->temporal_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700669 const int ctx = av1_get_pred_context_seg_id(xd);
Thomas Davies00021352017-07-11 16:07:55 +0100670#if CONFIG_NEW_MULTISYMBOL
671 aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
672 mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
673#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700675 mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
Thomas Davies00021352017-07-11 16:07:55 +0100676#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700677 if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
678 if (mbmi->seg_id_predicted) {
679 segment_id = predicted_segment_id;
680 } else {
681 segment_id = read_segment_id(r, segp);
682 if (counts) ++counts->seg.tree_mispred[segment_id];
683 }
684 } else {
685 segment_id = read_segment_id(r, segp);
686 if (counts) ++counts->seg.tree_total[segment_id];
687 }
688 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
689 return segment_id;
690}
691
Yaowu Xuf883b422016-08-30 14:01:10 -0700692static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
693 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
695 return 1;
696 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700697 const int ctx = av1_get_skip_context(xd);
Thomas Davies61e3e372017-04-04 16:10:23 +0100698#if CONFIG_NEW_MULTISYMBOL
699 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
700 const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
701#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700702 const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Thomas Davies61e3e372017-04-04 16:10:23 +0100703#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700704 FRAME_COUNTS *counts = xd->counts;
705 if (counts) ++counts->skip[ctx][skip];
706 return skip;
707 }
708}
709
Urvang Joshib100db72016-10-12 16:28:56 -0700710#if CONFIG_PALETTE
hui su33567b22017-04-30 16:40:19 -0700711#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700712static int uint16_compare(const void *a, const void *b) {
Urvang Joshi0ba850e2017-05-12 17:05:45 -0700713 const uint16_t va = *(const uint16_t *)a;
714 const uint16_t vb = *(const uint16_t *)b;
hui su33567b22017-04-30 16:40:19 -0700715 return va - vb;
716}
hui su33567b22017-04-30 16:40:19 -0700717
718static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
719 PALETTE_MODE_INFO *const pmi, aom_reader *r) {
720 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
721 const MODE_INFO *const above_mi = xd->above_mi;
722 const MODE_INFO *const left_mi = xd->left_mi;
723 const int n_cache = av1_get_palette_cache(above_mi, left_mi, 0, color_cache);
724 const int n = pmi->palette_size[0];
725 int idx = 0;
726 for (int i = 0; i < n_cache && idx < n; ++i)
727 if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
728 if (idx < n) {
729 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
730 if (idx < n) {
731 const int min_bits = bit_depth - 3;
732 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
733 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
734 for (; idx < n; ++idx) {
735 const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
736 pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
737 range -= delta;
738 bits = AOMMIN(bits, av1_ceil_log2(range));
739 }
740 }
741 }
hui su33567b22017-04-30 16:40:19 -0700742 qsort(pmi->palette_colors, n, sizeof(pmi->palette_colors[0]), uint16_compare);
hui su33567b22017-04-30 16:40:19 -0700743}
744
745static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
746 PALETTE_MODE_INFO *const pmi,
747 aom_reader *r) {
748 const int n = pmi->palette_size[1];
749 // U channel colors.
750 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
751 const MODE_INFO *const above_mi = xd->above_mi;
752 const MODE_INFO *const left_mi = xd->left_mi;
753 const int n_cache = av1_get_palette_cache(above_mi, left_mi, 1, color_cache);
754 int idx = PALETTE_MAX_SIZE;
755 for (int i = 0; i < n_cache && idx < PALETTE_MAX_SIZE + n; ++i)
756 if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
757 if (idx < PALETTE_MAX_SIZE + n) {
758 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
759 if (idx < PALETTE_MAX_SIZE + n) {
760 const int min_bits = bit_depth - 3;
761 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
762 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
763 for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
764 const int delta = aom_read_literal(r, bits, ACCT_STR);
765 pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
766 range -= delta;
767 bits = AOMMIN(bits, av1_ceil_log2(range));
768 }
769 }
770 }
hui su33567b22017-04-30 16:40:19 -0700771 qsort(pmi->palette_colors + PALETTE_MAX_SIZE, n,
772 sizeof(pmi->palette_colors[0]), uint16_compare);
hui su33567b22017-04-30 16:40:19 -0700773
774 // V channel colors.
775 if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
776 const int min_bits_v = bit_depth - 4;
777 const int max_val = 1 << bit_depth;
778 int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
779 pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
780 aom_read_literal(r, bit_depth, ACCT_STR);
781 for (int i = 1; i < n; ++i) {
782 int delta = aom_read_literal(r, bits, ACCT_STR);
783 if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
784 int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
785 if (val < 0) val += max_val;
786 if (val >= max_val) val -= max_val;
787 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
788 }
789 } else {
790 for (int i = 0; i < n; ++i) {
791 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
792 aom_read_literal(r, bit_depth, ACCT_STR);
793 }
794 }
795}
796#endif // CONFIG_PALETTE_DELTA_ENCODING
797
Yaowu Xuf883b422016-08-30 14:01:10 -0700798static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
799 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 MODE_INFO *const mi = xd->mi[0];
801 MB_MODE_INFO *const mbmi = &mi->mbmi;
802 const MODE_INFO *const above_mi = xd->above_mi;
803 const MODE_INFO *const left_mi = xd->left_mi;
804 const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
806
807 if (mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800808 int palette_y_mode_ctx = 0;
hui su40b9e7f2017-07-13 18:15:56 -0700809 if (above_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800810 palette_y_mode_ctx +=
811 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700812 }
813 if (left_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800814 palette_y_mode_ctx +=
815 (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700816 }
Urvang Joshi23a61112017-01-30 14:59:27 -0800817 if (aom_read(r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
818 [palette_y_mode_ctx],
819 ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 pmi->palette_size[0] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100821 aom_read_symbol(r,
822 xd->tile_ctx->palette_y_size_cdf[bsize - BLOCK_8X8],
823 PALETTE_SIZES, ACCT_STR) +
824 2;
hui sud13c24a2017-04-07 16:13:07 -0700825#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700826 read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700827#else
hui su40b9e7f2017-07-13 18:15:56 -0700828 for (int i = 0; i < pmi->palette_size[0]; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700829 pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui sud13c24a2017-04-07 16:13:07 -0700830#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 }
832 }
833
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400834 if (mbmi->uv_mode == UV_DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800835 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
836 if (aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
Michael Bebenita6048d052016-08-25 14:40:54 -0700837 ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838 pmi->palette_size[1] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100839 aom_read_symbol(r,
840 xd->tile_ctx->palette_uv_size_cdf[bsize - BLOCK_8X8],
841 PALETTE_SIZES, ACCT_STR) +
842 2;
hui sud13c24a2017-04-07 16:13:07 -0700843#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700844 read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700845#else
hui su40b9e7f2017-07-13 18:15:56 -0700846 for (int i = 0; i < pmi->palette_size[1]; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847 pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700848 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700850 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851 }
hui sud13c24a2017-04-07 16:13:07 -0700852#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853 }
854 }
855}
Urvang Joshib100db72016-10-12 16:28:56 -0700856#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857
hui su5db97432016-10-14 16:10:14 -0700858#if CONFIG_FILTER_INTRA
859static void read_filter_intra_mode_info(AV1_COMMON *const cm,
Jingning Han62946d12017-05-26 11:29:30 -0700860 MACROBLOCKD *const xd, int mi_row,
861 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 MODE_INFO *const mi = xd->mi[0];
863 MB_MODE_INFO *const mbmi = &mi->mbmi;
864 FRAME_COUNTS *counts = xd->counts;
hui su5db97432016-10-14 16:10:14 -0700865 FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
866 &mbmi->filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867
Urvang Joshib100db72016-10-12 16:28:56 -0700868 if (mbmi->mode == DC_PRED
869#if CONFIG_PALETTE
870 && mbmi->palette_mode_info.palette_size[0] == 0
871#endif // CONFIG_PALETTE
872 ) {
hui su5db97432016-10-14 16:10:14 -0700873 filter_intra_mode_info->use_filter_intra_mode[0] =
874 aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
875 if (filter_intra_mode_info->use_filter_intra_mode[0]) {
876 filter_intra_mode_info->filter_intra_mode[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700877 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 }
hui su5db97432016-10-14 16:10:14 -0700879 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800880 ++counts
881 ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
hui su5db97432016-10-14 16:10:14 -0700882 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883 }
Jingning Han62946d12017-05-26 11:29:30 -0700884
885#if CONFIG_CB4X4
886 if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
887 xd->plane[1].subsampling_x,
888 xd->plane[1].subsampling_y))
889 return;
hui sub4ed1492017-05-31 17:25:42 -0700890#else
891 (void)mi_row;
892 (void)mi_col;
893#endif // CONFIG_CB4X4
Jingning Han62946d12017-05-26 11:29:30 -0700894
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400895 if (mbmi->uv_mode == UV_DC_PRED
Urvang Joshib100db72016-10-12 16:28:56 -0700896#if CONFIG_PALETTE
897 && mbmi->palette_mode_info.palette_size[1] == 0
898#endif // CONFIG_PALETTE
899 ) {
hui su5db97432016-10-14 16:10:14 -0700900 filter_intra_mode_info->use_filter_intra_mode[1] =
901 aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
902 if (filter_intra_mode_info->use_filter_intra_mode[1]) {
903 filter_intra_mode_info->filter_intra_mode[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700904 av1_read_uniform(r, FILTER_INTRA_MODES);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905 }
hui su5db97432016-10-14 16:10:14 -0700906 if (counts) {
clang-format55ce9e02017-02-15 22:27:12 -0800907 ++counts
908 ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
hui su5db97432016-10-14 16:10:14 -0700909 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700910 }
911}
hui su5db97432016-10-14 16:10:14 -0700912#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913
hui su5db97432016-10-14 16:10:14 -0700914#if CONFIG_EXT_INTRA
Yaowu Xuf883b422016-08-30 14:01:10 -0700915static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
916 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700917 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
918 const BLOCK_SIZE bsize = mbmi->sb_type;
hui sueda3d762016-12-06 16:58:23 -0800919#if CONFIG_INTRA_INTERP
hui sub4e25d22017-03-09 15:32:30 -0800920 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
Yaowu Xuf883b422016-08-30 14:01:10 -0700921 const int ctx = av1_get_pred_context_intra_interp(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 int p_angle;
hui sueda3d762016-12-06 16:58:23 -0800923#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924
hui sueda3d762016-12-06 16:58:23 -0800925 (void)cm;
Joe Young830d4ce2017-05-30 17:48:13 -0700926
927 mbmi->angle_delta[0] = 0;
928 mbmi->angle_delta[1] = 0;
929
930 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931
hui su45dc5972016-12-08 17:42:50 -0800932 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700934 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
hui sueda3d762016-12-06 16:58:23 -0800935#if CONFIG_INTRA_INTERP
hui su0a6731f2017-04-26 15:23:47 -0700936 p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xuf883b422016-08-30 14:01:10 -0700937 if (av1_is_intra_filter_switchable(p_angle)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938 FRAME_COUNTS *counts = xd->counts;
hui sub4e25d22017-03-09 15:32:30 -0800939 mbmi->intra_filter = aom_read_symbol(r, ec_ctx->intra_filter_cdf[ctx],
940 INTRA_FILTERS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
942 } else {
943 mbmi->intra_filter = INTRA_FILTER_LINEAR;
944 }
hui sueda3d762016-12-06 16:58:23 -0800945#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700946 }
947
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400948 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700950 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 }
952}
953#endif // CONFIG_EXT_INTRA
954
Angie Chianga9f9a312017-04-13 16:40:43 -0700955void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Hanab7163d2016-11-04 09:46:35 -0700956#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -0700957 int supertx_enabled,
Jingning Hanab7163d2016-11-04 09:46:35 -0700958#endif
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700959#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700960 int blk_row, int blk_col, int block, int plane,
961 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700962#endif
963 aom_reader *r) {
964 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700965 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700966#if !CONFIG_TXK_SEL
Jingning Hane67b38a2016-11-04 10:30:00 -0700967#if CONFIG_VAR_TX
968 const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
969#else
Jingning Hanab7163d2016-11-04 09:46:35 -0700970 const TX_SIZE tx_size = mbmi->tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700971#endif
Jingning Han243b66b2017-06-23 12:11:47 -0700972#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000973 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000974
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700975#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700976 TX_TYPE *tx_type = &mbmi->tx_type;
977#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700978 // only y plane's tx_type is transmitted
979 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700980 (void)block;
981 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << 4) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700982#endif
983
Jingning Hanab7163d2016-11-04 09:46:35 -0700984 if (!FIXED_TX_TYPE) {
985#if CONFIG_EXT_TX
Urvang Joshifeb925f2016-12-05 10:37:29 -0800986 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -0800987 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
988 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -0800989 ((!cm->seg.enabled && cm->base_qindex > 0) ||
990 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
991 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -0700992#if CONFIG_SUPERTX
993 !supertx_enabled &&
994#endif // CONFIG_SUPERTX
995 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Sarah Parkere68a3e42017-02-16 14:03:24 -0800996 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
997 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -0700998 // eset == 0 should correspond to a set with only DCT_DCT and
999 // there is no need to read the tx_type
1000 assert(eset != 0);
Jingning Hanab7163d2016-11-04 09:46:35 -07001001 FRAME_COUNTS *counts = xd->counts;
1002
1003 if (inter_block) {
Sarah Parker784596d2017-06-23 08:41:26 -07001004 *tx_type = av1_ext_tx_inter_inv[eset][aom_read_symbol(
1005 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
1006 ext_tx_cnt_inter[eset], ACCT_STR)];
1007 if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -07001008 } else if (ALLOW_INTRA_EXT_TX) {
Sarah Parker784596d2017-06-23 08:41:26 -07001009 *tx_type = av1_ext_tx_intra_inv[eset][aom_read_symbol(
1010 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
1011 ext_tx_cnt_intra[eset], ACCT_STR)];
1012 if (counts)
1013 ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -07001014 }
1015 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001016 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001017 }
1018#else
Yue Cheneeacc4c2017-01-17 17:29:17 -08001019
1020 if (tx_size < TX_32X32 &&
1021 ((!cm->seg.enabled && cm->base_qindex > 0) ||
1022 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
1023 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -07001024#if CONFIG_SUPERTX
1025 !supertx_enabled &&
1026#endif // CONFIG_SUPERTX
1027 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1028 FRAME_COUNTS *counts = xd->counts;
Yue Cheneeacc4c2017-01-17 17:29:17 -08001029
Jingning Hanab7163d2016-11-04 09:46:35 -07001030 if (inter_block) {
Angie Chianga9f9a312017-04-13 16:40:43 -07001031 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001032 r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Angie Chianga9f9a312017-04-13 16:40:43 -07001033 if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -07001034 } else {
1035 const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
Angie Chianga9f9a312017-04-13 16:40:43 -07001036 *tx_type = av1_ext_tx_inv[aom_read_symbol(
Thomas Daviescef09622017-01-11 17:27:12 +00001037 r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Hanab7163d2016-11-04 09:46:35 -07001038 ACCT_STR)];
Angie Chianga9f9a312017-04-13 16:40:43 -07001039 if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Jingning Hanab7163d2016-11-04 09:46:35 -07001040 }
1041 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -07001042 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -07001043 }
1044#endif // CONFIG_EXT_TX
1045 }
Nathan E. Eggeebced372017-02-17 20:57:21 -05001046#if FIXED_TX_TYPE
1047 assert(mbmi->tx_type == DCT_DCT);
1048#endif
Jingning Hanab7163d2016-11-04 09:46:35 -07001049}
1050
Alex Converse28744302017-04-13 14:46:22 -07001051#if CONFIG_INTRABC
1052static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
1053 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001054 MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -07001055
1056static INLINE int is_mv_valid(const MV *mv);
1057
1058static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
1059 const int_mv *ref_mv, int mi_row, int mi_col,
1060 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -07001061 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1062 (void)cm;
Alex Converse28744302017-04-13 14:46:22 -07001063 FRAME_COUNTS *counts = xd->counts;
1064 nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse6b2584c2017-05-02 09:51:21 -07001065 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
1066 MV_SUBPEL_NONE);
Alex Converse28744302017-04-13 14:46:22 -07001067 int valid = is_mv_valid(&mv->as_mv) &&
1068 is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize);
Alex Converse28744302017-04-13 14:46:22 -07001069 return valid;
1070}
1071#endif // CONFIG_INTRABC
1072
Yaowu Xuf883b422016-08-30 14:01:10 -07001073static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001074 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001075 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001076 MODE_INFO *const mi = xd->mi[0];
1077 MB_MODE_INFO *const mbmi = &mi->mbmi;
1078 const MODE_INFO *above_mi = xd->above_mi;
1079 const MODE_INFO *left_mi = xd->left_mi;
1080 const BLOCK_SIZE bsize = mbmi->sb_type;
1081 int i;
1082 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001083 const int bw = mi_size_wide[bsize];
1084 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085
1086 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001087 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1088 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001089 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090
1091 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1092 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Arild Fuldseth07441162016-08-15 15:07:52 +02001093
1094#if CONFIG_DELTA_Q
1095 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001096 xd->current_qindex =
1097 xd->prev_qindex +
1098 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001099 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1100 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001101 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001102#if CONFIG_EXT_DELTA_Q
1103 if (cm->delta_lf_present_flag) {
1104 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1105 xd->prev_delta_lf_from_base +
1106 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1107 cm->delta_lf_res;
1108 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
1109 }
1110#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001111 }
1112#endif
1113
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001115 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001116
Alex Converse28744302017-04-13 14:46:22 -07001117#if CONFIG_INTRABC
1118 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools) {
Alex Converse7c412ea2017-06-01 15:16:22 -07001119 mbmi->use_intrabc = aom_read(r, ec_ctx->intrabc_prob, ACCT_STR);
Alex Converse28744302017-04-13 14:46:22 -07001120 if (mbmi->use_intrabc) {
Alex Conversef71808c2017-06-06 12:21:17 -07001121 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001122 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
Jingning Hand6c17d92017-04-24 16:33:12 -07001123#if CONFIG_DUAL_FILTER
1124 for (int idx = 0; idx < 4; ++idx) mbmi->interp_filter[idx] = BILINEAR;
1125#else
Alex Converse28744302017-04-13 14:46:22 -07001126 mbmi->interp_filter = BILINEAR;
Jingning Hand6c17d92017-04-24 16:33:12 -07001127#endif
Alex Converse44c2bad2017-05-11 09:36:10 -07001128
1129 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1130 int_mv ref_mvs[MAX_MV_REF_CANDIDATES] = {};
1131
1132 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
1133 xd->ref_mv_stack[INTRA_FRAME],
1134#if CONFIG_EXT_INTER
Yue Chenf03907a2017-05-31 12:04:04 -07001135 NULL,
Alex Converse44c2bad2017-05-11 09:36:10 -07001136#endif // CONFIG_EXT_INTER
1137 ref_mvs, mi_row, mi_col, NULL, NULL, inter_mode_ctx);
1138
1139 int_mv nearestmv, nearmv;
1140 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1141
1142 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1143 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
1144
Alex Converse28744302017-04-13 14:46:22 -07001145 xd->corrupted |=
1146 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
Alex Conversee16b2662017-05-24 14:00:00 -07001147#if CONFIG_VAR_TX
1148 // TODO(aconverse@google.com): Evaluate allowing VAR TX on intrabc blocks
1149 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1150 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1151 int idx, idy;
1152 for (idy = 0; idy < height; ++idy)
1153 for (idx = 0; idx < width; ++idx)
1154 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1155 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1156#endif // CONFIG_VAR_TX
Alex Conversedaa15e42017-05-02 14:27:16 -07001157#if CONFIG_EXT_TX && !CONFIG_TXK_SEL
1158 av1_read_tx_type(cm, xd,
1159#if CONFIG_SUPERTX
1160 0,
1161#endif
1162 r);
1163#endif // CONFIG_EXT_TX && !CONFIG_TXK_SEL
Alex Converse28744302017-04-13 14:46:22 -07001164 return;
1165 }
1166 }
1167#endif // CONFIG_INTRABC
1168
Alex Conversef71808c2017-06-06 12:21:17 -07001169 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
1170
Jingning Han52261842016-12-14 12:17:49 -08001171#if CONFIG_CB4X4
1172 (void)i;
1173 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001174 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Jingning Han52261842016-12-14 12:17:49 -08001175#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001176 switch (bsize) {
1177 case BLOCK_4X4:
1178 for (i = 0; i < 4; ++i)
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001179 mi->bmi[i].as_mode = read_intra_mode(
1180 r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, i));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001181 mbmi->mode = mi->bmi[3].as_mode;
1182 break;
1183 case BLOCK_4X8:
1184 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001185 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001186 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001187 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001188 break;
1189 case BLOCK_8X4:
1190 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001191 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001192 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001193 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 2));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001194 break;
1195 default:
1196 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001197 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001198 }
Jingning Han52261842016-12-14 12:17:49 -08001199#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001200
Jingning Han36fe3202017-02-20 22:31:49 -08001201#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001202 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001203 xd->plane[1].subsampling_y)) {
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001204 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001205#else
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001206 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001207#endif
1208
Luc Trudeauf5334002017-04-25 12:21:26 -04001209#if CONFIG_CFL
Luc Trudeau2c317902017-04-28 11:06:50 -04001210 // TODO(ltrudeau) support PALETTE
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001211 if (mbmi->uv_mode == UV_DC_PRED) {
David Michael Barr23198662017-06-19 23:19:48 +09001212 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, mbmi->cfl_alpha_signs);
Luc Trudeauf5334002017-04-25 12:21:26 -04001213 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001214#endif // CONFIG_CFL
1215
1216#if CONFIG_CB4X4
Joe Young830d4ce2017-05-30 17:48:13 -07001217 } else {
1218 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001219 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauf5334002017-04-25 12:21:26 -04001220 }
1221#endif
1222
Yaowu Xuc27fc142016-08-22 16:08:15 -07001223#if CONFIG_EXT_INTRA
1224 read_intra_angle_info(cm, xd, r);
1225#endif // CONFIG_EXT_INTRA
Urvang Joshib100db72016-10-12 16:28:56 -07001226#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001227 mbmi->palette_mode_info.palette_size[0] = 0;
1228 mbmi->palette_mode_info.palette_size[1] = 0;
1229 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
1230 read_palette_mode_info(cm, xd, r);
Urvang Joshib100db72016-10-12 16:28:56 -07001231#endif // CONFIG_PALETTE
hui su5db97432016-10-14 16:10:14 -07001232#if CONFIG_FILTER_INTRA
1233 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1234 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001235 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001236 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001237#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001238
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001239#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07001240 av1_read_tx_type(cm, xd,
Jingning Hanab7163d2016-11-04 09:46:35 -07001241#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07001242 0,
Nathan E. Egge72762a22016-09-07 17:12:07 -04001243#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07001244 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001245#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001246}
1247
Alex Converse6b2584c2017-05-02 09:51:21 -07001248static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
1249#if CONFIG_INTRABC
1250 int use_subpel,
1251#endif // CONFIG_INTRABC
1252 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001253 int mag, d, fr, hp;
Thomas Davies599395e2017-07-21 18:02:48 +01001254#if CONFIG_NEW_MULTISYMBOL
1255 const int sign = aom_read_bit(r, ACCT_STR);
1256#else
Michael Bebenita6048d052016-08-25 14:40:54 -07001257 const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001258#endif
Michael Bebenita6048d052016-08-25 14:40:54 -07001259 const int mv_class =
Nathan E. Egged7b893c2016-09-08 15:08:48 -04001260 aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 const int class0 = mv_class == MV_CLASS_0;
1262
1263 // Integer part
1264 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001265#if CONFIG_NEW_MULTISYMBOL
1266 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
1267#else
Nathan E. Egge45ea9632016-09-08 17:25:49 -04001268 d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Thomas Davies599395e2017-07-21 18:02:48 +01001269#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001270 mag = 0;
1271 } else {
1272 int i;
1273 const int n = mv_class + CLASS0_BITS - 1; // number of bits
1274
1275 d = 0;
Michael Bebenita6048d052016-08-25 14:40:54 -07001276 for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001277 mag = CLASS0_SIZE << (mv_class + 2);
1278 }
1279
Alex Converse6b2584c2017-05-02 09:51:21 -07001280#if CONFIG_INTRABC
1281 if (use_subpel) {
1282#endif // CONFIG_INTRABC
1283 // Fractional part
1284 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1285 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286
Thomas Davies599395e2017-07-21 18:02:48 +01001287// High precision part (if hp is not used, the default value of the hp is 1)
1288#if CONFIG_NEW_MULTISYMBOL
1289 hp = usehp ? aom_read_symbol(
1290 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1291 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001292 : 1;
Thomas Davies599395e2017-07-21 18:02:48 +01001293#else
1294 hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
1295 : 1;
1296#endif
Alex Converse6b2584c2017-05-02 09:51:21 -07001297#if CONFIG_INTRABC
1298 } else {
1299 fr = 3;
1300 hp = 1;
1301 }
1302#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07001303
1304 // Result
1305 mag += ((d << 3) | (fr << 1) | hp) + 1;
1306 return sign ? -mag : mag;
1307}
1308
Yaowu Xuf883b422016-08-30 14:01:10 -07001309static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Thomas9ac55082016-09-23 18:04:17 +01001310 nmv_context *ctx, nmv_context_counts *counts,
Alex Converse6b2584c2017-05-02 09:51:21 -07001311 MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001313 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001314 joint_type =
Nathan E. Egge5f7fd7a2016-09-08 11:22:03 -04001315 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001316
1317 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001318 diff.row = read_mv_component(r, &ctx->comps[0],
1319#if CONFIG_INTRABC
1320 precision > MV_SUBPEL_NONE,
1321#endif // CONFIG_INTRABC
1322 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001323
1324 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001325 diff.col = read_mv_component(r, &ctx->comps[1],
1326#if CONFIG_INTRABC
1327 precision > MV_SUBPEL_NONE,
1328#endif // CONFIG_INTRABC
1329 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001330
Alex Converse6b2584c2017-05-02 09:51:21 -07001331 av1_inc_mv(&diff, counts, precision);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001332
1333 mv->row = ref->row + diff.row;
1334 mv->col = ref->col + diff.col;
1335}
1336
Yaowu Xuf883b422016-08-30 14:01:10 -07001337static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001338 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001339 aom_reader *r) {
Jingning Han6b064722017-05-01 09:48:18 -07001340#if !SUB8X8_COMP_REF
Jingning Han69d21012017-05-14 16:51:27 -07001341 if (xd->mi[0]->mbmi.sb_type == BLOCK_4X4) return SINGLE_REFERENCE;
Jingning Han6b064722017-05-01 09:48:18 -07001342#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001344 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001345#if CONFIG_NEW_MULTISYMBOL
1346 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1347 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
1348#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349 const REFERENCE_MODE mode =
Michael Bebenita6048d052016-08-25 14:40:54 -07001350 (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001351#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001352 FRAME_COUNTS *counts = xd->counts;
1353 if (counts) ++counts->comp_inter[ctx][mode];
1354 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1355 } else {
1356 return cm->reference_mode;
1357 }
1358}
1359
Thomas Davies315f5782017-06-14 15:14:55 +01001360#if CONFIG_NEW_MULTISYMBOL
1361#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001362 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001363#else
1364#define READ_REF_BIT(pname) \
1365 aom_read(r, av1_get_pred_prob_##pname(cm, xd), ACCT_STR)
1366#endif
1367
Zoe Liuc082bbc2017-05-17 13:31:37 -07001368#if CONFIG_EXT_COMP_REFS
1369static REFERENCE_MODE read_comp_reference_type(AV1_COMMON *cm,
1370 const MACROBLOCKD *xd,
1371 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001372 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001373#if USE_UNI_COMP_REFS
Zoe Liufcf5fa22017-06-26 16:00:38 -07001374 COMP_REFERENCE_TYPE comp_ref_type;
1375#if CONFIG_VAR_REFS
1376 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm))
1377 if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm))
1378#endif // CONFIG_VAR_REFS
1379 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read(
1380 r, cm->fc->comp_ref_type_prob[ctx], ACCT_STR);
1381#if CONFIG_VAR_REFS
1382 else
1383 comp_ref_type = BIDIR_COMP_REFERENCE;
1384 else
1385 comp_ref_type = UNIDIR_COMP_REFERENCE;
1386#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001387#else // !USE_UNI_COMP_REFS
1388 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1389 const COMP_REFERENCE_TYPE comp_ref_type = BIDIR_COMP_REFERENCE;
1390#endif // USE_UNI_COMP_REFS
1391 FRAME_COUNTS *counts = xd->counts;
1392 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1393 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1394}
1395#endif // CONFIG_EXT_COMP_REFS
1396
Yaowu Xuc27fc142016-08-22 16:08:15 -07001397// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001398static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1399 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400 MV_REFERENCE_FRAME ref_frame[2]) {
Thomas Davies894cc812017-06-22 17:51:33 +01001401#if CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001402 FRAME_CONTEXT *const fc = cm->fc;
Thomas Davies894cc812017-06-22 17:51:33 +01001403#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001404 FRAME_COUNTS *counts = xd->counts;
1405
1406 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1407 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1408 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001409 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410 } else {
1411 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
1412 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1413 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001414#if CONFIG_EXT_COMP_REFS
1415 const COMP_REFERENCE_TYPE comp_ref_type =
1416 read_comp_reference_type(cm, xd, r);
1417
1418#if !USE_UNI_COMP_REFS
1419 // TODO(zoeliu): Temporarily turn off uni-directional comp refs
1420 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
1421#endif // !USE_UNI_COMP_REFS
1422
1423 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001424 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1425 int bit;
1426#if CONFIG_VAR_REFS
1427 if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
1428#endif // CONFIG_VAR_REFS
1429 bit = aom_read(r, fc->uni_comp_ref_prob[ctx][0], ACCT_STR);
1430#if CONFIG_VAR_REFS
1431 else
1432 bit = BWD_AND_ALT(cm);
1433#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001434 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1435
1436 if (bit) {
1437 ref_frame[0] = BWDREF_FRAME;
1438 ref_frame[1] = ALTREF_FRAME;
1439 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001440 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1441 int bit1;
1442#if CONFIG_VAR_REFS
1443 if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm)))
1444#endif // CONFIG_VAR_REFS
1445 bit1 = aom_read(r, fc->uni_comp_ref_prob[ctx1][1], ACCT_STR);
1446#if CONFIG_VAR_REFS
1447 else
1448 bit1 = L_AND_L3(cm) || L_AND_G(cm);
1449#endif // CONFIG_VAR_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001450 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1451
1452 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001453 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1454 int bit2;
1455#if CONFIG_VAR_REFS
1456 if (L_AND_L3(cm) && L_AND_G(cm))
1457#endif // CONFIG_VAR_REFS
1458 bit2 = aom_read(r, fc->uni_comp_ref_prob[ctx2][2], ACCT_STR);
1459#if CONFIG_VAR_REFS
1460 else
1461 bit2 = L_AND_G(cm);
1462#endif // CONFIG_VAR_REFS
1463 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1464
1465 if (bit2) {
1466 ref_frame[0] = LAST_FRAME;
1467 ref_frame[1] = GOLDEN_FRAME;
1468 } else {
1469 ref_frame[0] = LAST_FRAME;
1470 ref_frame[1] = LAST3_FRAME;
1471 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001472 } else {
1473 ref_frame[0] = LAST_FRAME;
1474 ref_frame[1] = LAST2_FRAME;
1475 }
1476 }
1477
1478 return;
1479 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001480
1481 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001482#endif // CONFIG_EXT_COMP_REFS
1483
1484// Normative in decoder (for low delay)
1485#if CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001486 const int idx = 1;
Zoe Liuc082bbc2017-05-17 13:31:37 -07001487#else // !(CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001488#if CONFIG_EXT_REFS
1489 const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
Zoe Liuc082bbc2017-05-17 13:31:37 -07001490#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001491 const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
1492#endif // CONFIG_EXT_REFS
Zoe Liuc082bbc2017-05-17 13:31:37 -07001493#endif // CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001494
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001495 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
1496#if CONFIG_VAR_REFS
1497 int bit;
1498 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1499 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001500 bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001501 else
1502 bit = L3_OR_G(cm);
1503#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001504 const int bit = READ_REF_BIT(comp_ref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001505#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001506 if (counts) ++counts->comp_ref[ctx][0][bit];
1507
1508#if CONFIG_EXT_REFS
1509 // Decode forward references.
1510 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001511 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001512#if CONFIG_VAR_REFS
1513 int bit1;
1514 // Test need to explicitly code (L) vs (L2) branch node in tree
1515 if (L_AND_L2(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001516 bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001517 else
1518 bit1 = LAST_IS_VALID(cm);
1519#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001520 const int bit1 = READ_REF_BIT(comp_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001521#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001522 if (counts) ++counts->comp_ref[ctx1][1][bit1];
1523 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
1524 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001525 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001526#if CONFIG_VAR_REFS
1527 int bit2;
1528 // Test need to explicitly code (L3) vs (G) branch node in tree
1529 if (L3_AND_G(cm))
Thomas Davies894cc812017-06-22 17:51:33 +01001530 bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001531 else
1532 bit2 = GOLDEN_IS_VALID(cm);
1533#else // !CONFIG_VAR_REFS
Thomas Davies894cc812017-06-22 17:51:33 +01001534 const int bit2 = READ_REF_BIT(comp_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001535#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001536 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1537 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1538 }
1539
1540 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001541 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
1542#if CONFIG_VAR_REFS
1543 int bit_bwd;
Zoe Liu043c2272017-07-19 12:40:29 -07001544// Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
1545#if CONFIG_ALTREF2
1546 const int bit_bwd_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
1547#else // !CONFIG_ALTREF2
1548 const int bit_bwd_uncertain = BWD_AND_ALT(cm);
1549#endif // CONFIG_ALTREF2
1550 if (bit_bwd_uncertain)
Thomas Davies894cc812017-06-22 17:51:33 +01001551 bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001552 else
1553 bit_bwd = ALTREF_IS_VALID(cm);
Thomas Davies894cc812017-06-22 17:51:33 +01001554#else // !CONFIG_VAR_REFS
1555 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001556#endif // CONFIG_VAR_REFS
1557 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001558#if CONFIG_ALTREF2
1559 if (!bit_bwd) {
1560 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
1561#if CONFIG_VAR_REFS
1562 int bit1_bwd;
1563 if (BWD_AND_ALT2(cm))
1564 bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1565 else
1566 bit1_bwd = ALTREF2_IS_VALID(cm);
1567#else // !CONFIG_VAR_REFS
1568 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
1569#endif // CONFIG_VAR_REFS
1570 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1571 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1572 } else {
1573 ref_frame[idx] = cm->comp_bwd_ref[2];
1574 }
1575#else // !CONFIG_ALTREF2
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001576 ref_frame[idx] = cm->comp_bwd_ref[bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001577#endif // CONFIG_ALTREF2
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001578#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001579 ref_frame[!idx] = cm->comp_var_ref[bit];
1580 ref_frame[idx] = cm->comp_fixed_ref;
1581#endif // CONFIG_EXT_REFS
1582 } else if (mode == SINGLE_REFERENCE) {
1583#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001584 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001585#if CONFIG_VAR_REFS
1586 int bit0;
1587 // Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT) branch node in
1588 // tree
1589 if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001590 bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001591 else
1592 bit0 = BWD_OR_ALT(cm);
1593#else // !CONFIG_VAR_REFS
Thomas Davies315f5782017-06-14 15:14:55 +01001594 const int bit0 = READ_REF_BIT(single_ref_p1);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001595#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001596 if (counts) ++counts->single_ref[ctx0][0][bit0];
1597
1598 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001599 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001600#if CONFIG_VAR_REFS
1601 int bit1;
Zoe Liu043c2272017-07-19 12:40:29 -07001602// Test need to explicitly code (BWD/ALT2) vs (ALT) branch node in tree
1603#if CONFIG_ALTREF2
1604 const int bit1_uncertain = BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm);
1605#else // !CONFIG_ALTREF2
1606 const int bit1_uncertain = BWD_AND_ALT(cm);
1607#endif // CONFIG_ALTREF2
1608 if (bit1_uncertain)
Thomas Davies315f5782017-06-14 15:14:55 +01001609 bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001610 else
1611 bit1 = ALTREF_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001612#else // !CONFIG_VAR_REFS
1613 const int bit1 = READ_REF_BIT(single_ref_p2);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001614#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001615 if (counts) ++counts->single_ref[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001616#if CONFIG_ALTREF2
1617 if (!bit1) {
1618 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
1619#if CONFIG_VAR_REFS
1620 int bit5;
1621 if (BWD_AND_ALT2(cm))
1622 bit5 = READ_REF_BIT(single_ref_p6);
1623 else
1624 bit5 = ALTREF2_IS_VALID(cm);
1625#else // !CONFIG_VAR_REFS
1626 const int bit5 = READ_REF_BIT(single_ref_p6);
1627#endif // CONFIG_VAR_REFS
1628 if (counts) ++counts->single_ref[ctx5][5][bit5];
1629 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1630 } else {
1631 ref_frame[0] = ALTREF_FRAME;
1632 }
1633#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001634 ref_frame[0] = bit1 ? ALTREF_FRAME : BWDREF_FRAME;
Zoe Liu043c2272017-07-19 12:40:29 -07001635#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07001636 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001637 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001638#if CONFIG_VAR_REFS
1639 int bit2;
1640 // Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
1641 if (L_OR_L2(cm) && L3_OR_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001642 bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001643 else
1644 bit2 = L3_OR_G(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001645#else // !CONFIG_VAR_REFS
1646 const int bit2 = READ_REF_BIT(single_ref_p3);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001647#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001648 if (counts) ++counts->single_ref[ctx2][2][bit2];
1649 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001650 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001651#if CONFIG_VAR_REFS
1652 int bit4;
1653 // Test need to explicitly code (L3) vs (G) branch node in tree
1654 if (L3_AND_G(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001655 bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001656 else
1657 bit4 = GOLDEN_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001658#else // !CONFIG_VAR_REFS
1659 const int bit4 = READ_REF_BIT(single_ref_p5);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001660#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001661 if (counts) ++counts->single_ref[ctx4][4][bit4];
1662 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1663 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001664 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001665#if CONFIG_VAR_REFS
1666 int bit3;
1667 // Test need to explicitly code (L) vs (L2) branch node in tree
1668 if (L_AND_L2(cm))
Thomas Davies315f5782017-06-14 15:14:55 +01001669 bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001670 else
1671 bit3 = LAST2_IS_VALID(cm);
Thomas Davies315f5782017-06-14 15:14:55 +01001672#else // !CONFIG_VAR_REFS
1673 const int bit3 = READ_REF_BIT(single_ref_p4);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001674#endif // CONFIG_VAR_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675 if (counts) ++counts->single_ref[ctx3][3][bit3];
1676 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1677 }
1678 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001679#else // !CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001680 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001681 const int bit0 = READ_REF_BIT(single_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001682 if (counts) ++counts->single_ref[ctx0][0][bit0];
1683
1684 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001685 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001686 const int bit1 = READ_REF_BIT(single_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687 if (counts) ++counts->single_ref[ctx1][1][bit1];
1688 ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
1689 } else {
1690 ref_frame[0] = LAST_FRAME;
1691 }
1692#endif // CONFIG_EXT_REFS
1693
Emil Keyder01770b32017-01-20 18:03:11 -05001694 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001695 } else {
1696 assert(0 && "Invalid prediction mode.");
1697 }
1698 }
1699}
1700
Angie Chiang9c4f8952016-11-21 11:13:19 -08001701static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1702 MACROBLOCKD *const xd,
1703 MB_MODE_INFO *const mbmi,
1704 aom_reader *r) {
1705 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001706 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001707
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001708 if (!av1_is_interp_needed(xd)) {
1709 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001710 return;
1711 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001712
1713#if CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07001714 if (cm->interp_filter != SWITCHABLE) {
Yue Chen19e7aa82016-11-30 14:05:39 -08001715 int dir;
1716
Angie Chiang9c4f8952016-11-21 11:13:19 -08001717 for (dir = 0; dir < 4; ++dir) mbmi->interp_filter[dir] = cm->interp_filter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001718 } else {
Yue Chen19e7aa82016-11-30 14:05:39 -08001719 int dir;
1720
Angie Chiang9c4f8952016-11-21 11:13:19 -08001721 for (dir = 0; dir < 2; ++dir) {
1722 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1723 mbmi->interp_filter[dir] = EIGHTTAP_REGULAR;
1724
1725 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1726 (mbmi->ref_frame[1] > INTRA_FRAME &&
1727 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001728 mbmi->interp_filter[dir] =
1729 (InterpFilter)av1_switchable_interp_inv[aom_read_symbol(
Thomas Davies77c7c402017-01-11 17:58:54 +00001730 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS,
Angie Chiang9c4f8952016-11-21 11:13:19 -08001731 ACCT_STR)];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001732 if (counts) ++counts->switchable_interp[ctx][mbmi->interp_filter[dir]];
1733 }
1734 }
1735 // The index system works as:
1736 // (0, 1) -> (vertical, horizontal) filter types for the first ref frame.
1737 // (2, 3) -> (vertical, horizontal) filter types for the second ref frame.
1738 mbmi->interp_filter[2] = mbmi->interp_filter[0];
1739 mbmi->interp_filter[3] = mbmi->interp_filter[1];
1740 }
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001741#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001742 if (cm->interp_filter != SWITCHABLE) {
1743 mbmi->interp_filter = cm->interp_filter;
1744 } else {
1745 const int ctx = av1_get_pred_context_switchable_interp(xd);
Angie Chiang9c4f8952016-11-21 11:13:19 -08001746 mbmi->interp_filter =
Michael Bebenita6048d052016-08-25 14:40:54 -07001747 (InterpFilter)av1_switchable_interp_inv[aom_read_symbol(
Thomas Davies77c7c402017-01-11 17:58:54 +00001748 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS,
Michael Bebenita6048d052016-08-25 14:40:54 -07001749 ACCT_STR)];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001750 if (counts) ++counts->switchable_interp[ctx][mbmi->interp_filter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001751 }
Angie Chiang9c4f8952016-11-21 11:13:19 -08001752#endif // CONFIG_DUAL_FILTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07001753}
1754
Jingning Han36fe3202017-02-20 22:31:49 -08001755static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1756 const int mi_col, MACROBLOCKD *const xd,
1757 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001758 MB_MODE_INFO *const mbmi = &mi->mbmi;
1759 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1760 int i;
1761
1762 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001763 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001764
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001765 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001766
Jingning Han52261842016-12-14 12:17:49 -08001767#if CONFIG_CB4X4
1768 (void)i;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001769 mbmi->mode = read_intra_mode_y(ec_ctx, xd, r, size_group_lookup[bsize]);
Jingning Han52261842016-12-14 12:17:49 -08001770#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771 switch (bsize) {
1772 case BLOCK_4X4:
1773 for (i = 0; i < 4; ++i)
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001774 mi->bmi[i].as_mode = read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001775 mbmi->mode = mi->bmi[3].as_mode;
1776 break;
1777 case BLOCK_4X8:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001778 mi->bmi[0].as_mode = mi->bmi[2].as_mode =
1779 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001780 mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001781 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001782 break;
1783 case BLOCK_8X4:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001784 mi->bmi[0].as_mode = mi->bmi[1].as_mode =
1785 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001786 mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001787 read_intra_mode_y(ec_ctx, xd, r, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788 break;
1789 default:
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001790 mbmi->mode = read_intra_mode_y(ec_ctx, xd, r, size_group_lookup[bsize]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001791 }
Jingning Han52261842016-12-14 12:17:49 -08001792#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001793
Jingning Han36fe3202017-02-20 22:31:49 -08001794#if CONFIG_CB4X4
Jingning Hand3a64432017-04-06 17:04:17 -07001795 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001796 xd->plane[1].subsampling_y)) {
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001797 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001798#else
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001799 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, xd, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001800 (void)mi_row;
1801 (void)mi_col;
1802#endif
1803
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001804#if CONFIG_CFL
1805 // TODO(ltrudeau) support PALETTE
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001806 if (mbmi->uv_mode == UV_DC_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001807 mbmi->cfl_alpha_idx =
1808 read_cfl_alphas(xd->tile_ctx, r, mbmi->cfl_alpha_signs);
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001809 }
1810#endif // CONFIG_CFL
1811
1812#if CONFIG_CB4X4
1813 }
1814#endif
1815
Yaowu Xuc27fc142016-08-22 16:08:15 -07001816#if CONFIG_EXT_INTRA
1817 read_intra_angle_info(cm, xd, r);
1818#endif // CONFIG_EXT_INTRA
Urvang Joshib100db72016-10-12 16:28:56 -07001819#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001820 mbmi->palette_mode_info.palette_size[0] = 0;
1821 mbmi->palette_mode_info.palette_size[1] = 0;
1822 if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
1823 read_palette_mode_info(cm, xd, r);
Urvang Joshib100db72016-10-12 16:28:56 -07001824#endif // CONFIG_PALETTE
hui su5db97432016-10-14 16:10:14 -07001825#if CONFIG_FILTER_INTRA
1826 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1827 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Jingning Han48b1cb32017-01-23 10:26:14 -08001828 if (bsize >= BLOCK_8X8 || CONFIG_CB4X4)
Jingning Han62946d12017-05-26 11:29:30 -07001829 read_filter_intra_mode_info(cm, xd, mi_row, mi_col, r);
hui su5db97432016-10-14 16:10:14 -07001830#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001831}
1832
1833static INLINE int is_mv_valid(const MV *mv) {
1834 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1835 mv->col < MV_UPP;
1836}
1837
Yaowu Xuf883b422016-08-30 14:01:10 -07001838static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001839 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001840 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001841 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001842 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1843 int mi_col, int is_compound, int allow_hp,
1844 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001845 int i;
1846 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001847 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001848 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001849 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001850#if CONFIG_CB4X4
1851 int_mv *pred_mv = mbmi->pred_mv;
1852 (void)block;
1853#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001854 int_mv *pred_mv =
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07001855 (bsize >= BLOCK_8X8) ? mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08001856#endif // CONFIG_CB4X4
Sarah Parkere5299862016-08-16 14:57:37 -07001857 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001858 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001859 (void)mi_row;
1860 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001861 (void)bsize;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001862
1863 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001864 case NEWMV: {
1865 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001866 for (i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001867 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1868 int nmv_ctx =
1869 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1870 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001871 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001872 nmv_context_counts *const mv_counts =
1873 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07001874 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001875 ret = ret && is_mv_valid(&mv[i].as_mv);
1876
Yaowu Xuc27fc142016-08-22 16:08:15 -07001877 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001878 }
1879 break;
1880 }
1881 case NEARESTMV: {
1882 mv[0].as_int = nearest_mv[0].as_int;
1883 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1884
Yaowu Xuc27fc142016-08-22 16:08:15 -07001885 pred_mv[0].as_int = nearest_mv[0].as_int;
1886 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001887 break;
1888 }
1889 case NEARMV: {
1890 mv[0].as_int = near_mv[0].as_int;
1891 if (is_compound) mv[1].as_int = near_mv[1].as_int;
1892
Yaowu Xuc27fc142016-08-22 16:08:15 -07001893 pred_mv[0].as_int = near_mv[0].as_int;
1894 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001895 break;
1896 }
1897 case ZEROMV: {
Sarah Parkere5299862016-08-16 14:57:37 -07001898#if CONFIG_GLOBAL_MOTION
David Barkercdcac6d2016-12-01 17:04:16 +00001899 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001900 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001901 mi_col, mi_row, block)
David Barkercdcac6d2016-12-01 17:04:16 +00001902 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001903 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00001904 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001905 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001906 mi_col, mi_row, block)
David Barkercdcac6d2016-12-01 17:04:16 +00001907 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001908#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001909 mv[0].as_int = 0;
1910 if (is_compound) mv[1].as_int = 0;
Sarah Parkere5299862016-08-16 14:57:37 -07001911#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07001912
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001913 pred_mv[0].as_int = mv[0].as_int;
1914 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001915 break;
1916 }
1917#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07001918#if CONFIG_COMPOUND_SINGLEREF
1919 case SR_NEAREST_NEARMV: {
1920 assert(!is_compound);
1921 mv[0].as_int = nearest_mv[0].as_int;
1922 mv[1].as_int = near_mv[0].as_int;
1923 break;
1924 }
1925 /*
1926 case SR_NEAREST_NEWMV: {
1927 assert(!is_compound);
1928 mv[0].as_int = nearest_mv[0].as_int;
1929
1930 FRAME_COUNTS *counts = xd->counts;
1931 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1932 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1933 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1934 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1935 nmv_context_counts *const mv_counts =
1936 counts ? &counts->mv[nmv_ctx] : NULL;
1937 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1938 ret = ret && is_mv_valid(&mv[1].as_mv);
1939 break;
1940 }*/
1941 case SR_NEAR_NEWMV: {
1942 assert(!is_compound);
1943 mv[0].as_int = near_mv[0].as_int;
1944
1945 FRAME_COUNTS *counts = xd->counts;
1946 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1947 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1948 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1949 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1950 nmv_context_counts *const mv_counts =
1951 counts ? &counts->mv[nmv_ctx] : NULL;
1952 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1953 ret = ret && is_mv_valid(&mv[1].as_mv);
1954 break;
1955 }
1956 case SR_ZERO_NEWMV: {
1957 assert(!is_compound);
1958#if CONFIG_GLOBAL_MOTION
1959 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1960 cm->allow_high_precision_mv, bsize,
1961 mi_col, mi_row, block)
1962 .as_int;
1963#else
1964 mv[0].as_int = 0;
1965#endif // CONFIG_GLOBAL_MOTION
1966
1967 FRAME_COUNTS *counts = xd->counts;
1968 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1969 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1970 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
1971 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1972 nmv_context_counts *const mv_counts =
1973 counts ? &counts->mv[nmv_ctx] : NULL;
1974 read_mv(r, &mv[1].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1975 ret = ret && is_mv_valid(&mv[1].as_mv);
1976 break;
1977 }
1978 case SR_NEW_NEWMV: {
1979 assert(!is_compound);
1980
1981 FRAME_COUNTS *counts = xd->counts;
1982 for (i = 0; i < 2; ++i) {
1983 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1984 int nmv_ctx =
1985 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
1986 mbmi->ref_mv_idx);
1987 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
1988 nmv_context_counts *const mv_counts =
1989 counts ? &counts->mv[nmv_ctx] : NULL;
1990 read_mv(r, &mv[i].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
1991 ret = ret && is_mv_valid(&mv[i].as_mv);
1992 }
1993 break;
1994 }
1995#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07001996 case NEW_NEWMV: {
1997 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998 assert(is_compound);
1999 for (i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002000 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2001 int nmv_ctx =
2002 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
2003 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002004 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005 nmv_context_counts *const mv_counts =
2006 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002007 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002008 ret = ret && is_mv_valid(&mv[i].as_mv);
2009 }
2010 break;
2011 }
2012 case NEAREST_NEARESTMV: {
2013 assert(is_compound);
2014 mv[0].as_int = nearest_mv[0].as_int;
2015 mv[1].as_int = nearest_mv[1].as_int;
2016 break;
2017 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002018 case NEAR_NEARMV: {
2019 assert(is_compound);
2020 mv[0].as_int = near_mv[0].as_int;
2021 mv[1].as_int = near_mv[1].as_int;
2022 break;
2023 }
2024 case NEW_NEARESTMV: {
2025 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002026 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2027 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2028 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002029 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030 nmv_context_counts *const mv_counts =
2031 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002032 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002033 assert(is_compound);
2034 ret = ret && is_mv_valid(&mv[0].as_mv);
2035 mv[1].as_int = nearest_mv[1].as_int;
2036 break;
2037 }
2038 case NEAREST_NEWMV: {
2039 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002040 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2041 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2042 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002043 nmv_context_counts *const mv_counts =
2044 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002045 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07002046 mv[0].as_int = nearest_mv[0].as_int;
2047 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002048 assert(is_compound);
2049 ret = ret && is_mv_valid(&mv[1].as_mv);
2050 break;
2051 }
2052 case NEAR_NEWMV: {
2053 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002054 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2055 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2056 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002057 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002058 nmv_context_counts *const mv_counts =
2059 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002060 mv[0].as_int = near_mv[0].as_int;
2061 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002062 assert(is_compound);
2063
2064 ret = ret && is_mv_valid(&mv[1].as_mv);
2065 break;
2066 }
2067 case NEW_NEARMV: {
2068 FRAME_COUNTS *counts = xd->counts;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002069 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
2070 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
2071 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07002072 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002073 nmv_context_counts *const mv_counts =
2074 counts ? &counts->mv[nmv_ctx] : NULL;
Alex Converse3d0bdc12017-05-01 15:19:58 -07002075 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, mv_counts, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002076 assert(is_compound);
2077 ret = ret && is_mv_valid(&mv[0].as_mv);
2078 mv[1].as_int = near_mv[1].as_int;
2079 break;
2080 }
2081 case ZERO_ZEROMV: {
2082 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08002083#if CONFIG_GLOBAL_MOTION
2084 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002085 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002086 mi_col, mi_row, block)
Sarah Parkerc2d38712017-01-24 15:15:41 -08002087 .as_int;
2088 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08002089 cm->allow_high_precision_mv, bsize,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002090 mi_col, mi_row, block)
Sarah Parkerc2d38712017-01-24 15:15:41 -08002091 .as_int;
2092#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002093 mv[0].as_int = 0;
2094 mv[1].as_int = 0;
Sarah Parkerc2d38712017-01-24 15:15:41 -08002095#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002096 break;
2097 }
2098#endif // CONFIG_EXT_INTER
2099 default: { return 0; }
2100 }
2101 return ret;
2102}
2103
Yaowu Xuf883b422016-08-30 14:01:10 -07002104static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
2105 int segment_id, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002106 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
2107 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
2108 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002109 const int ctx = av1_get_intra_inter_context(xd);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002110#if CONFIG_NEW_MULTISYMBOL
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002111 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002112 const int is_inter =
2113 aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
2114#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002115 const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01002116#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002117 FRAME_COUNTS *counts = xd->counts;
2118 if (counts) ++counts->intra_inter[ctx][is_inter];
2119 return is_inter;
2120 }
2121}
2122
Zoe Liu85b66462017-04-20 14:28:19 -07002123#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2124static int read_is_inter_singleref_comp_mode(AV1_COMMON *const cm,
2125 MACROBLOCKD *const xd,
2126 int segment_id, aom_reader *r) {
2127 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) return 0;
2128
2129 const int ctx = av1_get_inter_mode_context(xd);
2130 const int is_singleref_comp_mode =
2131 aom_read(r, cm->fc->comp_inter_mode_prob[ctx], ACCT_STR);
2132 FRAME_COUNTS *counts = xd->counts;
2133
2134 if (counts) ++counts->comp_inter_mode[ctx][is_singleref_comp_mode];
2135 return is_singleref_comp_mode;
2136}
2137#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2138
Yaowu Xuc27fc142016-08-22 16:08:15 -07002139static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002140 AV1Decoder *const pbi = (AV1Decoder *)data;
2141 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
2142 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002143}
2144
Di Chen56586622017-06-09 13:49:44 -07002145#if DEC_MISMATCH_DEBUG
2146static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi,
Zoe Liuf9333f52017-07-03 10:52:01 -07002147 MACROBLOCKD *const xd, int mi_row, int mi_col,
2148 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES],
2149 int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07002150 int_mv mv[2] = { { 0 } };
2151 int ref;
2152 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07002153 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
2154 mv[ref].as_mv = mbmi->mv[ref].as_mv;
2155
2156 int interp_ctx[2] = { -1 };
2157 int interp_filter[2] = { cm->interp_filter };
2158 if (cm->interp_filter == SWITCHABLE) {
2159 int dir;
2160 for (dir = 0; dir < 2; ++dir) {
2161 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
2162 (mbmi->ref_frame[1] > INTRA_FRAME &&
2163 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
2164 interp_ctx[dir] = av1_get_pred_context_switchable_interp(xd, dir);
2165 interp_filter[dir] = mbmi->interp_filter[dir];
2166 } else {
2167 interp_filter[dir] = EIGHTTAP_REGULAR;
2168 }
2169 }
2170 }
2171
2172 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
2173 int16_t zeromv_ctx = -1;
2174 int16_t refmv_ctx = -1;
2175 if (mbmi->mode != NEWMV) {
2176 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == ZEROMV);
2177 zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
2178 if (mbmi->mode != ZEROMV) {
2179 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
2180 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
2181 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
2182 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
2183 }
2184 }
2185
2186 int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Zoe Liuf9333f52017-07-03 10:52:01 -07002187#define FRAME_TO_CHECK 1
Zoe Liufcf5fa22017-06-26 16:00:38 -07002188 if (cm->current_video_frame == FRAME_TO_CHECK /*&& cm->show_frame == 0*/) {
Zoe Liuf9333f52017-07-03 10:52:01 -07002189 printf(
2190 "=== DECODER ===: "
2191 "Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
2192 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
2193 "ref[1]=%d, motion_mode=%d, inter_mode_ctx=%d, mode_ctx=%d, "
2194 "interp_ctx=(%d,%d), interp_filter=(%d,%d), newmv_ctx=%d, "
2195 "zeromv_ctx=%d, refmv_ctx=%d\n",
2196 cm->current_video_frame, mi_row, mi_col, mbmi->mode, mbmi->sb_type,
2197 cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
2198 mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
2199 mbmi->motion_mode, inter_mode_ctx[ref_frame_type], mode_ctx,
2200 interp_ctx[0], interp_ctx[1], interp_filter[0], interp_filter[1],
2201 newmv_ctx, zeromv_ctx, refmv_ctx);
2202 }
Di Chen56586622017-06-09 13:49:44 -07002203}
2204#endif // DEC_MISMATCH_DEBUG
2205
Yaowu Xuf883b422016-08-30 14:01:10 -07002206static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002207 MACROBLOCKD *const xd,
2208 MODE_INFO *const mi,
David Barker491983d2016-11-10 13:22:17 +00002209#if (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER) && \
2210 CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002211 int mi_row, int mi_col, aom_reader *r,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212 int supertx_enabled) {
2213#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002214 int mi_row, int mi_col, aom_reader *r) {
Yue Chencb60b182016-10-13 15:18:22 -07002215#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002216 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002217 MB_MODE_INFO *const mbmi = &mi->mbmi;
2218 const BLOCK_SIZE bsize = mbmi->sb_type;
2219 const int allow_hp = cm->allow_high_precision_mv;
Jingning Han5cfa6712016-12-14 09:53:38 -08002220 const int unify_bsize = CONFIG_CB4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002221 int_mv nearestmv[2], nearmv[2];
2222 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223 int ref, is_compound;
Zoe Liu85b66462017-04-20 14:28:19 -07002224#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2225 int is_singleref_comp_mode = 0;
2226#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002227 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002228#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002229 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002230#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 int16_t mode_ctx = 0;
Yue Chen69f18e12016-09-08 14:48:15 -07002232#if CONFIG_WARPED_MOTION
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08002233 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang1bc82862017-06-28 15:49:48 -07002234#if WARPED_MOTION_SORT_SAMPLES
2235 int pts_mv[SAMPLES_ARRAY_SIZE];
2236#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002237#endif // CONFIG_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00002238 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239
Urvang Joshi5a9ea002017-05-22 15:25:18 -07002240 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
2241
Urvang Joshib100db72016-10-12 16:28:56 -07002242#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243 mbmi->palette_mode_info.palette_size[0] = 0;
2244 mbmi->palette_mode_info.palette_size[1] = 0;
Urvang Joshib100db72016-10-12 16:28:56 -07002245#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07002246
Frederic Barbier7a84fd82017-03-02 18:08:15 +01002247 memset(ref_mvs, 0, sizeof(ref_mvs));
2248
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
2250 is_compound = has_second_ref(mbmi);
2251
Zoe Liuc082bbc2017-05-17 13:31:37 -07002252#if CONFIG_EXT_COMP_REFS
2253#if !USE_UNI_COMP_REFS
2254 // NOTE: uni-directional comp refs disabled
2255 if (is_compound)
2256 assert(mbmi->ref_frame[0] < BWDREF_FRAME &&
2257 mbmi->ref_frame[1] >= BWDREF_FRAME);
2258#endif // !USE_UNI_COMP_REFS
2259#endif // CONFIG_EXT_COMP_REFS
2260
Zoe Liu85b66462017-04-20 14:28:19 -07002261#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2262 if (!is_compound)
2263 is_singleref_comp_mode =
2264 read_is_inter_singleref_comp_mode(cm, xd, mbmi->segment_id, r);
2265#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2266
Yaowu Xuc27fc142016-08-22 16:08:15 -07002267 for (ref = 0; ref < 1 + is_compound; ++ref) {
2268 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002270 av1_find_mv_refs(
2271 cm, xd, mi, frame, &xd->ref_mv_count[frame], xd->ref_mv_stack[frame],
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272#if CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002273 compound_inter_mode_ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002274#endif // CONFIG_EXT_INTER
Sebastien Alaiwane140c502017-04-27 09:52:34 +02002275 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi, inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276 }
2277
Jingning Hanacddc032016-11-17 15:26:20 -08002278 if (is_compound) {
2279 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002280 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
2281 xd->ref_mv_stack[ref_frame],
2282#if CONFIG_EXT_INTER
2283 compound_inter_mode_ctx,
2284#endif // CONFIG_EXT_INTER
2285 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
2286 inter_mode_ctx);
2287
2288 if (xd->ref_mv_count[ref_frame] < 2) {
2289 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00002290 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002291 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00002292#if CONFIG_GLOBAL_MOTION
2293 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00002294 cm->allow_high_precision_mv,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002295 bsize, mi_col, mi_row, 0)
David Barkercdcac6d2016-12-01 17:04:16 +00002296 .as_int;
Sarah Parkerae7c4582017-02-28 16:30:30 -08002297 zeromv[1].as_int = (rf[1] != NONE_FRAME)
2298 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
2299 cm->allow_high_precision_mv,
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08002300 bsize, mi_col, mi_row, 0)
Sarah Parkerae7c4582017-02-28 16:30:30 -08002301 .as_int
2302 : 0;
David Barkercdcac6d2016-12-01 17:04:16 +00002303#else
2304 zeromv[0].as_int = zeromv[1].as_int = 0;
2305#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07002306 for (ref = 0; ref < 2; ++ref) {
2307 if (rf[ref] == NONE_FRAME) continue;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002308 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
2309 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
Sarah Parker9923d1b2017-04-10 11:56:40 -07002310 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
2311 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
2312 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01002313 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002314 }
2315 }
2316
Yaowu Xuc27fc142016-08-22 16:08:15 -07002317#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07002318#if CONFIG_COMPOUND_SINGLEREF
2319 if (is_compound || is_singleref_comp_mode)
2320#else // !CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002321 if (is_compound)
Zoe Liu85b66462017-04-20 14:28:19 -07002322#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002323 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
2324 else
2325#endif // CONFIG_EXT_INTER
2326 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07002327 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002328 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002329
2330 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
2331 mbmi->mode = ZEROMV;
Debargha Mukherjeec76f9dc2017-05-01 13:18:09 -07002332 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002333 aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
David Barker3409c0d2017-06-30 17:33:13 +01002334 "Invalid usage of segment feature on small blocks");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002335 return;
2336 }
2337 } else {
Jingning Han5cfa6712016-12-14 09:53:38 -08002338 if (bsize >= BLOCK_8X8 || unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002339#if CONFIG_EXT_INTER
2340 if (is_compound)
2341 mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002342#if CONFIG_COMPOUND_SINGLEREF
2343 else if (is_singleref_comp_mode)
Thomas Daviesb8b14a92017-07-12 15:11:49 +01002344 mbmi->mode = read_inter_singleref_comp_mode(xd, r, mode_ctx);
Zoe Liu85b66462017-04-20 14:28:19 -07002345#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346 else
2347#endif // CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002348 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
David Barker404b2e82017-03-27 13:07:47 +01002349#if CONFIG_EXT_INTER
David Barker3dfba992017-04-03 16:10:09 +01002350 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Zoe Liu85b66462017-04-20 14:28:19 -07002351#if CONFIG_COMPOUND_SINGLEREF
2352 mbmi->mode == SR_NEW_NEWMV ||
2353#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002354 have_nearmv_in_inter_mode(mbmi->mode))
Zoe Liu85b66462017-04-20 14:28:19 -07002355#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002356 if (mbmi->mode == NEARMV || mbmi->mode == NEWMV)
Zoe Liu85b66462017-04-20 14:28:19 -07002357#endif // CONFIG_EXT_INTER
Thomas Davies149eda52017-06-12 18:11:55 +01002358 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002359 }
2360 }
2361
2362#if CONFIG_EXT_INTER
Jingning Han5cfa6712016-12-14 09:53:38 -08002363 if ((bsize < BLOCK_8X8 && unify_bsize) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07002364 (mbmi->mode != ZEROMV && mbmi->mode != ZERO_ZEROMV)) {
2365#else
Jingning Han5cfa6712016-12-14 09:53:38 -08002366 if ((bsize < BLOCK_8X8 && !unify_bsize) || mbmi->mode != ZEROMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002367#endif // CONFIG_EXT_INTER
2368 for (ref = 0; ref < 1 + is_compound; ++ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002369 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
2370 &nearestmv[ref], &nearmv[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002371 }
2372 }
2373
Yaowu Xuc27fc142016-08-22 16:08:15 -07002374#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07002375#if CONFIG_COMPOUND_SINGLEREF
2376 if ((is_compound || is_singleref_comp_mode) &&
2377 (bsize >= BLOCK_8X8 || unify_bsize) && mbmi->mode != ZERO_ZEROMV) {
2378#else // !CONFIG_COMPOUND_SINGLEREF
Jingning Han61418bb2017-01-23 17:12:48 -08002379 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
2380 mbmi->mode != ZERO_ZEROMV) {
Zoe Liu85b66462017-04-20 14:28:19 -07002381#endif // CONFIG_COMPOUND_SINGLEREF
2382#else // !CONFIG_EXT_INTER
Jingning Han5cfa6712016-12-14 09:53:38 -08002383 if (is_compound && (bsize >= BLOCK_8X8 || unify_bsize) &&
2384 mbmi->mode != NEWMV && mbmi->mode != ZEROMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002386 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002387
2388#if CONFIG_EXT_INTER
2389 if (xd->ref_mv_count[ref_frame_type] > 0) {
2390#else
2391 if (xd->ref_mv_count[ref_frame_type] == 1 && mbmi->mode == NEARESTMV) {
2392#endif // CONFIG_EXT_INTER
2393#if CONFIG_EXT_INTER
2394 if (mbmi->mode == NEAREST_NEARESTMV) {
2395#endif // CONFIG_EXT_INTER
2396 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2397 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
2398 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
2399 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
2400#if CONFIG_EXT_INTER
Zoe Liu85b66462017-04-20 14:28:19 -07002401 } else if (mbmi->mode == NEAREST_NEWMV
2402#if CONFIG_COMPOUND_SINGLEREF
2403 || mbmi->mode == SR_NEAREST_NEARMV
2404// || mbmi->mode == SR_NEAREST_NEWMV
2405#endif // CONFIG_COMPOUND_SINGLEREF
2406 ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2408 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07002409 } else if (mbmi->mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
2411 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
2412 }
2413#endif // CONFIG_EXT_INTER
2414 }
2415
2416#if CONFIG_EXT_INTER
2417 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01002418 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
Zoe Liu85b66462017-04-20 14:28:19 -07002419#if CONFIG_COMPOUND_SINGLEREF
2420 if (is_compound) {
2421#endif // CONFIG_COMPOUND_SINGLEREF
2422 if (compound_ref0_mode(mbmi->mode) == NEARMV) {
2423 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2424 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2425 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002426
Zoe Liu85b66462017-04-20 14:28:19 -07002427 if (compound_ref1_mode(mbmi->mode) == NEARMV) {
2428 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
2429 lower_mv_precision(&nearmv[1].as_mv, allow_hp);
2430 }
2431#if CONFIG_COMPOUND_SINGLEREF
2432 } else {
2433 assert(is_singleref_comp_mode);
2434 if (compound_ref0_mode(mbmi->mode) == NEARMV ||
2435 compound_ref1_mode(mbmi->mode) == NEARMV) {
2436 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2437 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2438 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439 }
Zoe Liu85b66462017-04-20 14:28:19 -07002440#endif // CONFIG_COMPOUND_SINGLEREF
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 }
Zoe Liu85b66462017-04-20 14:28:19 -07002442#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 if (xd->ref_mv_count[ref_frame_type] > 1) {
2444 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
2445 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
2446 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
2447 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2448 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
2449 }
2450#endif // CONFIG_EXT_INTER
David Barker0d7c4b02017-07-25 14:55:48 +01002451 } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
2452 int_mv cur_mv =
2453 xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
2454 nearmv[0] = cur_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456
Yue Chen19e7aa82016-11-30 14:05:39 -08002457#if !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION && !CONFIG_GLOBAL_MOTION
Angie Chiang9c4f8952016-11-21 11:13:19 -08002458 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002459#endif // !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460
Jingning Han5cfa6712016-12-14 09:53:38 -08002461 if (bsize < BLOCK_8X8 && !unify_bsize) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002462 const int num_4x4_w = 1 << xd->bmode_blocks_wl;
2463 const int num_4x4_h = 1 << xd->bmode_blocks_hl;
2464 int idx, idy;
2465 PREDICTION_MODE b_mode;
2466 int_mv nearest_sub8x8[2], near_sub8x8[2];
2467#if CONFIG_EXT_INTER
2468 int_mv ref_mv[2][2];
2469#endif // CONFIG_EXT_INTER
2470 for (idy = 0; idy < 2; idy += num_4x4_h) {
2471 for (idx = 0; idx < 2; idx += num_4x4_w) {
2472 int_mv block[2];
2473 const int j = idy * 2 + idx;
2474 int_mv ref_mv_s8[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002475#if CONFIG_EXT_INTER
2476 if (!is_compound)
2477#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002478 mode_ctx = av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame,
2479 bsize, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002480#if CONFIG_EXT_INTER
2481 if (is_compound)
2482 b_mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
2483 else
2484#endif // CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002485 b_mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486
2487#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002488 if (b_mode != ZEROMV && b_mode != ZERO_ZEROMV) {
2489#else
2490 if (b_mode != ZEROMV) {
2491#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002492 CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
2493 uint8_t ref_mv_count[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002494 for (ref = 0; ref < 1 + is_compound; ++ref)
2495#if CONFIG_EXT_INTER
2496 {
2497 int_mv mv_ref_list[MAX_MV_REF_CANDIDATES];
Yaowu Xu531d6af2017-03-07 17:48:52 -08002498 av1_update_mv_context(cm, xd, mi, mbmi->ref_frame[ref], mv_ref_list,
2499 j, mi_row, mi_col, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002500#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002501 av1_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
Yaowu Xuf883b422016-08-30 14:01:10 -07002502 ref_mv_stack[ref], &ref_mv_count[ref],
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002504 mv_ref_list,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002506 &nearest_sub8x8[ref],
2507 &near_sub8x8[ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002508#if CONFIG_EXT_INTER
2509 if (have_newmv_in_inter_mode(b_mode)) {
2510 mv_ref_list[0].as_int = nearest_sub8x8[ref].as_int;
2511 mv_ref_list[1].as_int = near_sub8x8[ref].as_int;
Yaowu Xuf883b422016-08-30 14:01:10 -07002512 av1_find_best_ref_mvs(allow_hp, mv_ref_list, &ref_mv[0][ref],
2513 &ref_mv[1][ref]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002514 }
2515 }
2516#endif // CONFIG_EXT_INTER
2517 }
2518
2519 for (ref = 0; ref < 1 + is_compound && b_mode != ZEROMV; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002520 ref_mv_s8[ref] = nearest_sub8x8[ref];
2521 lower_mv_precision(&ref_mv_s8[ref].as_mv, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 }
2523#if CONFIG_EXT_INTER
2524 (void)ref_mv_s8;
2525#endif
2526
Jingning Han5c60cdf2016-09-30 09:37:46 -07002527 if (!assign_mv(cm, xd, b_mode, mbmi->ref_frame, j, block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002528#if CONFIG_EXT_INTER
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002529 ref_mv[0],
2530#else // !CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002531 ref_mv_s8,
2532#endif // CONFIG_EXT_INTER
David Barker45390c12017-02-20 14:44:40 +00002533 nearest_sub8x8, near_sub8x8, mi_row, mi_col, is_compound,
2534 allow_hp, r)) {
Angie Chiangd0916d92017-03-10 17:54:18 -08002535 aom_merge_corrupted_flag(&xd->corrupted, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536 break;
2537 };
2538
2539 mi->bmi[j].as_mv[0].as_int = block[0].as_int;
Sarah Parkerd7fa8542016-10-11 11:51:59 -07002540 mi->bmi[j].as_mode = b_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002541 if (is_compound) mi->bmi[j].as_mv[1].as_int = block[1].as_int;
2542
2543 if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
2544 if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
2545 }
2546 }
2547
Yaowu Xuf5bbbfa2016-09-26 09:13:38 -07002548 mbmi->pred_mv[0].as_int = mi->bmi[3].pred_mv[0].as_int;
2549 mbmi->pred_mv[1].as_int = mi->bmi[3].pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002550 mi->mbmi.mode = b_mode;
2551
2552 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2553 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2554 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002555 int_mv ref_mv[2];
2556 ref_mv[0] = nearestmv[0];
2557 ref_mv[1] = nearestmv[1];
2558
David Barker404b2e82017-03-27 13:07:47 +01002559#if CONFIG_EXT_INTER
David Barker3dfba992017-04-03 16:10:09 +01002560 if (is_compound) {
David Barker3dfba992017-04-03 16:10:09 +01002561 int ref_mv_idx = mbmi->ref_mv_idx;
2562 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2563 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2564 // mbmi->ref_mv_idx (like NEWMV)
2565 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2566 ref_mv_idx = 1 + mbmi->ref_mv_idx;
David Barker3dfba992017-04-03 16:10:09 +01002567
2568 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
David Barker404b2e82017-03-27 13:07:47 +01002569 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2570 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker3dfba992017-04-03 16:10:09 +01002571 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002572 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker404b2e82017-03-27 13:07:47 +01002573 xd->n8_h << MI_SIZE_LOG2, xd);
2574 }
David Barker3dfba992017-04-03 16:10:09 +01002575 nearestmv[0] = ref_mv[0];
David Barker404b2e82017-03-27 13:07:47 +01002576 }
David Barker3dfba992017-04-03 16:10:09 +01002577 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
David Barker3dfba992017-04-03 16:10:09 +01002578 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2579 if (xd->ref_mv_count[ref_frame_type] > 1) {
2580 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
Jingning Han1b5bd002017-04-20 08:48:30 -07002581 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
David Barker3dfba992017-04-03 16:10:09 +01002582 xd->n8_h << MI_SIZE_LOG2, xd);
2583 }
David Barker3dfba992017-04-03 16:10:09 +01002584 nearestmv[1] = ref_mv[1];
2585 }
Zoe Liu85b66462017-04-20 14:28:19 -07002586#if CONFIG_COMPOUND_SINGLEREF
2587 } else if (is_singleref_comp_mode) {
2588 int ref_mv_idx = mbmi->ref_mv_idx;
2589 // Special case: SR_NEAR_NEWMV use 1 + mbmi->ref_mv_idx (like NEARMV)
2590 // instead of mbmi->ref_mv_idx (like NEWMV)
2591 if (mbmi->mode == SR_NEAR_NEWMV) ref_mv_idx = 1 + mbmi->ref_mv_idx;
2592
2593 if (compound_ref0_mode(mbmi->mode) == NEWMV ||
2594 compound_ref1_mode(mbmi->mode) == NEWMV) {
2595 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2596 if (xd->ref_mv_count[ref_frame_type] > 1) {
2597 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2598 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2599 xd->n8_h << MI_SIZE_LOG2, xd);
2600 }
2601 // TODO(zoeliu): To further investigate why this would not cause a
2602 // mismatch for the mode of SR_NEAREST_NEWMV.
2603 nearestmv[0] = ref_mv[0];
2604 }
2605#endif // CONFIG_COMPOUND_SINGLEREF
David Barker3dfba992017-04-03 16:10:09 +01002606 } else {
2607#endif // CONFIG_EXT_INTER
2608 if (mbmi->mode == NEWMV) {
2609 for (ref = 0; ref < 1 + is_compound; ++ref) {
David Barker3dfba992017-04-03 16:10:09 +01002610 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[ref] =
2613 (ref == 0)
2614 ? xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv
2615 : xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
2616 .comp_mv;
2617 clamp_mv_ref(&ref_mv[ref].as_mv, xd->n8_w << MI_SIZE_LOG2,
2618 xd->n8_h << MI_SIZE_LOG2, xd);
2619 }
David Barker3dfba992017-04-03 16:10:09 +01002620 nearestmv[ref] = ref_mv[ref];
2621 }
2622 }
2623#if CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002624 }
David Barker3dfba992017-04-03 16:10:09 +01002625#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07002626
Angie Chiangd0916d92017-03-10 17:54:18 -08002627 int mv_corrupted_flag =
Zoe Liu7f24e1b2017-03-17 17:42:05 -07002628 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
David Barker45390c12017-02-20 14:44:40 +00002629 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
Angie Chiangd0916d92017-03-10 17:54:18 -08002630 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 }
2632
Yue Chen4d26acb2017-05-01 12:28:34 -07002633#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002634 mbmi->use_wedge_interintra = 0;
2635 if (cm->reference_mode != COMPOUND_REFERENCE &&
2636#if CONFIG_SUPERTX
2637 !supertx_enabled &&
2638#endif
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002639 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002641#if CONFIG_NEW_MULTISYMBOL
2642 const int interintra =
2643 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
2644#else
Michael Bebenita6048d052016-08-25 14:40:54 -07002645 const int interintra =
2646 aom_read(r, cm->fc->interintra_prob[bsize_group], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002647#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002648 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002649 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002650 if (interintra) {
2651 const INTERINTRA_MODE interintra_mode =
2652 read_interintra_mode(cm, xd, r, bsize_group);
2653 mbmi->ref_frame[1] = INTRA_FRAME;
2654 mbmi->interintra_mode = interintra_mode;
2655#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 mbmi->angle_delta[0] = 0;
2657 mbmi->angle_delta[1] = 0;
hui sueda3d762016-12-06 16:58:23 -08002658#if CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 mbmi->intra_filter = INTRA_FILTER_LINEAR;
hui sueda3d762016-12-06 16:58:23 -08002660#endif // CONFIG_INTRA_INTERP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002662#if CONFIG_FILTER_INTRA
2663 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2664 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2665#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002667#if CONFIG_NEW_MULTISYMBOL
2668 mbmi->use_wedge_interintra = aom_read_symbol(
2669 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
2670#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002671 mbmi->use_wedge_interintra =
Michael Bebenita6048d052016-08-25 14:40:54 -07002672 aom_read(r, cm->fc->wedge_interintra_prob[bsize], ACCT_STR);
Thomas Daviescff91712017-07-07 11:49:55 +01002673#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674 if (xd->counts)
2675 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2676 if (mbmi->use_wedge_interintra) {
2677 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002678 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679 mbmi->interintra_wedge_sign = 0;
2680 }
2681 }
2682 }
2683 }
Yue Chen4d26acb2017-05-01 12:28:34 -07002684#endif // CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685
Yue Chen52c51732017-07-11 15:08:30 -07002686#if CONFIG_WARPED_MOTION
2687 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
2688 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2689 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2690
2691 xd->block_refs[ref] = ref_buf;
2692 }
2693#endif
2694
Yue Chencb60b182016-10-13 15:18:22 -07002695#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
2696 mbmi->motion_mode = SIMPLE_TRANSLATION;
Yue Chen69f18e12016-09-08 14:48:15 -07002697#if CONFIG_WARPED_MOTION
2698 if (mbmi->sb_type >= BLOCK_8X8 && !has_second_ref(mbmi))
Yunqing Wang1bc82862017-06-28 15:49:48 -07002699#if WARPED_MOTION_SORT_SAMPLES
2700 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 Wang1bc82862017-06-28 15:49:48 -07002704#endif // WARPED_MOTION_SORT_SAMPLES
Yue Chen69f18e12016-09-08 14:48:15 -07002705#endif // CONFIG_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002706#if CONFIG_MOTION_VAR
2707 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
2708#endif
Yue Chen69f18e12016-09-08 14:48:15 -07002709
Yaowu Xuc27fc142016-08-22 16:08:15 -07002710#if CONFIG_SUPERTX
Yue Chen69f18e12016-09-08 14:48:15 -07002711 if (!supertx_enabled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002712#endif // CONFIG_SUPERTX
2713#if CONFIG_EXT_INTER
2714 if (mbmi->ref_frame[1] != INTRA_FRAME)
2715#endif // CONFIG_EXT_INTER
Sarah Parker19234cc2017-03-10 16:43:25 -08002716 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002717
2718#if CONFIG_NCOBMC_ADAPT_WEIGHT
Wei-Ting Linca710d62017-07-13 11:41:02 -07002719 read_ncobmc_mode(xd, mi, mbmi->ncobmc_mode, r);
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002720#endif
2721
Zoe Liu85b66462017-04-20 14:28:19 -07002722#if CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
2723 if (is_singleref_comp_mode) assert(mbmi->motion_mode == SIMPLE_TRANSLATION);
2724#endif // CONFIG_EXT_INTER && CONFIG_COMPOUND_SINGLEREF
Yue Chen69f18e12016-09-08 14:48:15 -07002725#if CONFIG_WARPED_MOTION
2726 if (mbmi->motion_mode == WARPED_CAUSAL) {
2727 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
Yunqing Wang1bc82862017-06-28 15:49:48 -07002728
2729#if WARPED_MOTION_SORT_SAMPLES
2730 if (mbmi->num_proj_ref[0] > 1)
2731 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2732 pts_inref, mbmi->num_proj_ref[0]);
2733#endif // WARPED_MOTION_SORT_SAMPLES
2734
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002735 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2736 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2737 &mbmi->wm_params[0], mi_row, mi_col)) {
Yunqing Wang8657ad72017-06-20 14:46:08 -07002738 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "Invalid Warped Model");
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002739 }
Yue Chen69f18e12016-09-08 14:48:15 -07002740 }
2741#endif // CONFIG_WARPED_MOTION
2742#if CONFIG_SUPERTX
2743 }
2744#endif // CONFIG_SUPERTX
Yue Chencb60b182016-10-13 15:18:22 -07002745#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Yaowu Xuc27fc142016-08-22 16:08:15 -07002746
2747#if CONFIG_EXT_INTER
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002748 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Zoe Liu85b66462017-04-20 14:28:19 -07002749 if (
2750#if CONFIG_COMPOUND_SINGLEREF
Zoe Liu0c634c72017-06-19 07:06:28 -07002751 is_inter_anyref_comp_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002752#else // !CONFIG_COMPOUND_SINGLEREF
2753 cm->reference_mode != SINGLE_REFERENCE &&
Sarah Parker6fdc8532016-11-16 17:47:13 -08002754 is_inter_compound_mode(mbmi->mode)
Zoe Liu85b66462017-04-20 14:28:19 -07002755#endif // CONFIG_COMPOUND_SINGLEREF
Yue Chencb60b182016-10-13 15:18:22 -07002756#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002757 && mbmi->motion_mode == SIMPLE_TRANSLATION
Yue Chencb60b182016-10-13 15:18:22 -07002758#endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
Sarah Parker6fdc8532016-11-16 17:47:13 -08002759 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002760 if (is_any_masked_compound_used(bsize)) {
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002761#if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002762 if (cm->allow_masked_compound) {
Thomas Daviesd8dac222017-06-27 11:23:15 +01002763 mbmi->interinter_compound_type = aom_read_symbol(
2764 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002765#if CONFIG_WEDGE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002766 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
2767 mbmi->wedge_index =
2768 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2769 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2770 }
Debargha Mukherjeec5f735f2017-04-26 03:25:28 +00002771#endif // CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002772#if CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002773 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
2774 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
2775 }
Sarah Parker42d96102017-01-31 21:05:27 -08002776#endif // CONFIG_COMPOUND_SEGMENT
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002777 }
2778#endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE
Sarah Parker42d96102017-01-31 21:05:27 -08002779 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002780 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002781 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002782 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002783 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002784 }
2785#endif // CONFIG_EXT_INTER
2786
Yue Chen19e7aa82016-11-30 14:05:39 -08002787#if CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002788 read_mb_interp_filter(cm, xd, mbmi, r);
Angie Chiang1733f6b2017-01-05 09:52:20 -08002789#endif // CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION
Zoe Liu85b66462017-04-20 14:28:19 -07002790
Di Chen56586622017-06-09 13:49:44 -07002791#if DEC_MISMATCH_DEBUG
2792 // NOTE(zoeliu): For debug
Zoe Liuf9333f52017-07-03 10:52:01 -07002793 dec_dump_logs(cm, mi, xd, mi_row, mi_col, inter_mode_ctx, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002794#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795}
2796
Yaowu Xuf883b422016-08-30 14:01:10 -07002797static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798 MACROBLOCKD *const xd,
2799#if CONFIG_SUPERTX
2800 int supertx_enabled,
2801#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002802 int mi_row, int mi_col, aom_reader *r) {
2803 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804 MODE_INFO *const mi = xd->mi[0];
2805 MB_MODE_INFO *const mbmi = &mi->mbmi;
2806 int inter_block = 1;
2807#if CONFIG_VAR_TX
2808 BLOCK_SIZE bsize = mbmi->sb_type;
2809#endif // CONFIG_VAR_TX
2810
2811 mbmi->mv[0].as_int = 0;
2812 mbmi->mv[1].as_int = 0;
2813 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
2814#if CONFIG_SUPERTX
David Barker3aec8d62017-01-31 14:55:32 +00002815 if (!supertx_enabled)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002816#endif // CONFIG_SUPERTX
2817 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
David Barker3aec8d62017-01-31 14:55:32 +00002818
Arild Fuldseth07441162016-08-15 15:07:52 +02002819#if CONFIG_DELTA_Q
David Barker3aec8d62017-01-31 14:55:32 +00002820 if (cm->delta_q_present_flag) {
2821 xd->current_qindex =
2822 xd->prev_qindex +
2823 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01002824 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2825 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
David Barker3aec8d62017-01-31 14:55:32 +00002826 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07002827#if CONFIG_EXT_DELTA_Q
2828 if (cm->delta_lf_present_flag) {
2829 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2830 xd->prev_delta_lf_from_base +
2831 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
2832 cm->delta_lf_res;
2833 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
2834 }
2835#endif
David Barker3aec8d62017-01-31 14:55:32 +00002836 }
Arild Fuldseth07441162016-08-15 15:07:52 +02002837#endif
David Barker3aec8d62017-01-31 14:55:32 +00002838
2839#if CONFIG_SUPERTX
2840 if (!supertx_enabled) {
2841#endif // CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
2843
2844#if CONFIG_VAR_TX
Jingning Han331662e2017-05-30 17:03:32 -07002845 xd->above_txfm_context =
2846 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2847 xd->left_txfm_context = xd->left_txfm_context_buffer +
2848 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002849
2850 if (cm->tx_mode == TX_MODE_SELECT &&
2851#if CONFIG_CB4X4
Jingning Han3daa4fd2017-01-20 10:33:50 -08002852 bsize > BLOCK_4X4 &&
Jingning Han581d1692017-01-05 16:03:54 -08002853#else
2854 bsize >= BLOCK_8X8 &&
2855#endif
2856 !mbmi->skip && inter_block) {
Jingning Han70e5f3f2016-11-09 17:03:07 -08002857 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
Jingning Hanf64062f2016-11-02 16:22:18 -07002858 const int bh = tx_size_high_unit[max_tx_size];
2859 const int bw = tx_size_wide_unit[max_tx_size];
Jingning Han65abc312016-10-27 13:04:21 -07002860 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2861 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002862 int idx, idy;
Yue Chena1e48dc2016-08-29 17:29:33 -07002863
Jingning Hanfe45b212016-11-22 10:30:23 -08002864 mbmi->min_tx_size = TX_SIZES_ALL;
2865 for (idy = 0; idy < height; idy += bh)
2866 for (idx = 0; idx < width; idx += bw)
2867 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size,
2868 height != width, idy, idx, r);
Yue Chend6bdd462017-07-19 16:05:43 -07002869#if CONFIG_RECT_TX_EXT
2870 if (is_quarter_tx_allowed(xd, mbmi, inter_block) &&
2871 mbmi->tx_size == max_tx_size) {
2872 int quarter_tx;
2873
2874 if (quarter_txsize_lookup[bsize] != max_tx_size) {
2875 quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
2876 if (xd->counts) ++xd->counts->quarter_tx_size[quarter_tx];
2877 } else {
2878 quarter_tx = 1;
2879 }
2880 if (quarter_tx) {
2881 mbmi->tx_size = quarter_txsize_lookup[bsize];
2882 for (idy = 0; idy < tx_size_high_unit[max_tx_size] / 2; ++idy)
2883 for (idx = 0; idx < tx_size_wide_unit[max_tx_size] / 2; ++idx)
2884 mbmi->inter_tx_size[idy][idx] = mbmi->tx_size;
2885 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
2886 }
2887 }
2888#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -08002890 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002891
2892 if (inter_block) {
Jingning Han9ca05b72017-01-03 14:41:36 -08002893 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2894 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895 int idx, idy;
2896 for (idy = 0; idy < height; ++idy)
2897 for (idx = 0; idx < width; ++idx)
2898 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
2899 }
Jingning Hane67b38a2016-11-04 10:30:00 -07002900 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han1b1dc932016-11-09 10:55:30 -08002901 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002902 }
2903#else
Urvang Joshifeb925f2016-12-05 10:37:29 -08002904 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002905#endif // CONFIG_VAR_TX
2906#if CONFIG_SUPERTX
2907 }
2908#if CONFIG_VAR_TX
2909 else if (inter_block) {
2910 const int width = num_4x4_blocks_wide_lookup[bsize];
2911 const int height = num_4x4_blocks_high_lookup[bsize];
2912 int idx, idy;
2913 xd->mi[0]->mbmi.tx_size = xd->supertx_size;
2914 for (idy = 0; idy < height; ++idy)
2915 for (idx = 0; idx < width; ++idx)
2916 xd->mi[0]->mbmi.inter_tx_size[idy >> 1][idx >> 1] = xd->supertx_size;
2917 }
2918#endif // CONFIG_VAR_TX
2919#endif // CONFIG_SUPERTX
2920
2921 if (inter_block)
2922 read_inter_block_mode_info(pbi, xd,
David Barker491983d2016-11-10 13:22:17 +00002923#if (CONFIG_MOTION_VAR || CONFIG_EXT_INTER || CONFIG_WARPED_MOTION) && \
2924 CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002925
2926 mi, mi_row, mi_col, r, supertx_enabled);
2927#else
2928 mi, mi_row, mi_col, r);
Yue Chencb60b182016-10-13 15:18:22 -07002929#endif // CONFIG_MOTION_VAR && CONFIG_SUPERTX
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 else
Jingning Han36fe3202017-02-20 22:31:49 -08002931 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002932
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002933#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -07002934 av1_read_tx_type(cm, xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935#if CONFIG_SUPERTX
Angie Chianga9f9a312017-04-13 16:40:43 -07002936 supertx_enabled,
Nathan E. Egge93878c42016-05-03 10:01:32 -04002937#endif
Angie Chianga9f9a312017-04-13 16:40:43 -07002938 r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002939#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002940}
2941
Yaowu Xuf883b422016-08-30 14:01:10 -07002942void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002943#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002944 int supertx_enabled,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002945#endif // CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -07002946 int mi_row, int mi_col, aom_reader *r, int x_mis,
2947 int y_mis) {
2948 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002949 MODE_INFO *const mi = xd->mi[0];
2950 MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
2951 int w, h;
2952
Alex Converse28744302017-04-13 14:46:22 -07002953#if CONFIG_INTRABC
2954 mi->mbmi.use_intrabc = 0;
2955#endif // CONFIG_INTRABC
2956
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957 if (frame_is_intra_only(cm)) {
2958 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959 for (h = 0; h < y_mis; ++h) {
2960 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2961 for (w = 0; w < x_mis; ++w) {
2962 MV_REF *const mv = frame_mv + w;
Emil Keyder01770b32017-01-20 18:03:11 -05002963 mv->ref_frame[0] = NONE_FRAME;
2964 mv->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 }
2966 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002967 } else {
2968 read_inter_frame_mode_info(pbi, xd,
2969#if CONFIG_SUPERTX
2970 supertx_enabled,
2971#endif // CONFIG_SUPERTX
2972 mi_row, mi_col, r);
2973 for (h = 0; h < y_mis; ++h) {
2974 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2975 for (w = 0; w < x_mis; ++w) {
2976 MV_REF *const mv = frame_mv + w;
2977 mv->ref_frame[0] = mi->mbmi.ref_frame[0];
2978 mv->ref_frame[1] = mi->mbmi.ref_frame[1];
2979 mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
2980 mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
Yaowu Xu4306b6e2016-09-27 12:55:32 -07002981 mv->pred_mv[0].as_int = mi->mbmi.pred_mv[0].as_int;
2982 mv->pred_mv[1].as_int = mi->mbmi.pred_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002983 }
2984 }
2985 }
2986}