blob: 51fad32d4aaea33a0aa193d788bf246b941ed42f [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13
14#include "av1/common/common.h"
15#include "av1/common/entropy.h"
16#include "av1/common/entropymode.h"
17#include "av1/common/entropymv.h"
18#include "av1/common/mvref_common.h"
19#include "av1/common/pred_common.h"
20#include "av1/common/reconinter.h"
hui su45dc5972016-12-08 17:42:50 -080021#if CONFIG_EXT_INTRA
22#include "av1/common/reconintra.h"
23#endif // CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "av1/common/seg_common.h"
Yue Chen69f18e12016-09-08 14:48:15 -070025#include "av1/common/warped_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070026
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/decoder/decodeframe.h"
Jingning Han1aab8182016-06-03 11:09:06 -070028#include "av1/decoder/decodemv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029
Yaowu Xuf883b422016-08-30 14:01:10 -070030#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031
Luc Trudeaud183b642017-11-28 11:42:37 -050032#if CONFIG_CFL
33#include "av1/common/cfl.h"
34#endif
35
Michael Bebenita6048d052016-08-25 14:40:54 -070036#define ACCT_STR __func__
Zoe Liu85b66462017-04-20 14:28:19 -070037
Di Chen56586622017-06-09 13:49:44 -070038#define DEC_MISMATCH_DEBUG 0
Zoe Liu85b66462017-04-20 14:28:19 -070039
Thomas9ac55082016-09-23 18:04:17 +010040static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
Hui Su814f41e2017-10-02 12:21:24 -070041 return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR);
Nathan E. Egge3ef926e2016-09-07 18:20:41 -040042}
Yaowu Xuc27fc142016-08-22 16:08:15 -070043
Steinar Midtskogen6c24b022017-09-15 09:46:39 +020044static void read_cdef(AV1_COMMON *cm, aom_reader *r, MB_MODE_INFO *const mbmi,
45 int mi_col, int mi_row) {
46 if (cm->all_lossless) return;
47
48 const int m = ~((1 << (6 - MI_SIZE_LOG2)) - 1);
49 if (!(mi_col & (cm->mib_size - 1)) &&
50 !(mi_row & (cm->mib_size - 1))) { // Top left?
51#if CONFIG_EXT_PARTITION
52 cm->cdef_preset[0] = cm->cdef_preset[1] = cm->cdef_preset[2] =
53 cm->cdef_preset[3] = -1;
54#else
55 cm->cdef_preset = -1;
56#endif
57 }
58// Read CDEF param at first a non-skip coding block
59#if CONFIG_EXT_PARTITION
60 const int mask = 1 << (6 - MI_SIZE_LOG2);
61 const int index = cm->sb_size == BLOCK_128X128
62 ? !!(mi_col & mask) + 2 * !!(mi_row & mask)
63 : 0;
64 cm->mi_grid_visible[(mi_row & m) * cm->mi_stride + (mi_col & m)]
65 ->mbmi.cdef_strength = cm->cdef_preset[index] =
66 cm->cdef_preset[index] == -1 && !mbmi->skip
67 ? aom_read_literal(r, cm->cdef_bits, ACCT_STR)
68 : cm->cdef_preset[index];
69#else
70 cm->mi_grid_visible[(mi_row & m) * cm->mi_stride + (mi_col & m)]
71 ->mbmi.cdef_strength = cm->cdef_preset =
72 cm->cdef_preset == -1 && !mbmi->skip
73 ? aom_read_literal(r, cm->cdef_bits, ACCT_STR)
74 : cm->cdef_preset;
75#endif
76}
77
Thomas Daviesf6936102016-09-05 16:51:31 +010078static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
79 MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
80 FRAME_COUNTS *counts = xd->counts;
81 int sign, abs, reduced_delta_qindex = 0;
82 BLOCK_SIZE bsize = mbmi->sb_type;
Pavel Frolov1dbe92d2017-11-02 01:49:19 +030083 const int b_col = mi_col & (cm->mib_size - 1);
84 const int b_row = mi_row & (cm->mib_size - 1);
Thomas Daviesf6936102016-09-05 16:51:31 +010085 const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000086 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
87 (void)cm;
Thomas Daviesf6936102016-09-05 16:51:31 +010088
Pavel Frolovbfa2b8c2017-11-01 20:08:44 +030089 if ((bsize != cm->sb_size || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000090 abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +010091 const int smallval = (abs < DELTA_Q_SMALL);
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000092 if (counts) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +010093 for (int i = 0; i < abs; ++i) counts->delta_q[i][1]++;
Thomas Daviesd6ee8a82017-03-02 14:42:50 +000094 if (smallval) counts->delta_q[abs][0]++;
95 }
96
97 if (!smallval) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +010098 const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
99 const int thr = (1 << rem_bits) + 1;
Thomas Daviesf6936102016-09-05 16:51:31 +0100100 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
101 }
102
103 if (abs) {
104 sign = aom_read_bit(r, ACCT_STR);
105 } else {
106 sign = 1;
107 }
108
109 reduced_delta_qindex = sign ? -abs : abs;
110 }
111 return reduced_delta_qindex;
112}
Fangwen Fu231fe422017-04-24 17:52:29 -0700113#if CONFIG_EXT_DELTA_Q
114static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
Cheng Chena97394f2017-09-27 15:05:14 -0700115#if CONFIG_LOOPFILTER_LEVEL
116 int lf_id,
117#endif
Fangwen Fu231fe422017-04-24 17:52:29 -0700118 MB_MODE_INFO *const mbmi, int mi_col,
119 int mi_row) {
120 FRAME_COUNTS *counts = xd->counts;
121 int sign, abs, reduced_delta_lflevel = 0;
122 BLOCK_SIZE bsize = mbmi->sb_type;
Pavel Frolov1dbe92d2017-11-02 01:49:19 +0300123 const int b_col = mi_col & (cm->mib_size - 1);
124 const int b_row = mi_row & (cm->mib_size - 1);
Fangwen Fu231fe422017-04-24 17:52:29 -0700125 const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
Fangwen Fu231fe422017-04-24 17:52:29 -0700126 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
127 (void)cm;
Fangwen Fu231fe422017-04-24 17:52:29 -0700128
Debargha Mukherjeee30159c2017-10-08 12:04:43 -0700129 if ((bsize != cm->sb_size || mbmi->skip == 0) && read_delta_lf_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -0700130#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -0700131 if (cm->delta_lf_multi) {
132 assert(lf_id >= 0 && lf_id < FRAME_LF_COUNT);
133 abs = aom_read_symbol(r, ec_ctx->delta_lf_multi_cdf[lf_id],
134 DELTA_LF_PROBS + 1, ACCT_STR);
135 } else {
136 abs = aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1,
137 ACCT_STR);
138 }
Cheng Chena97394f2017-09-27 15:05:14 -0700139#else
Fangwen Fu231fe422017-04-24 17:52:29 -0700140 abs =
141 aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
Cheng Chena97394f2017-09-27 15:05:14 -0700142#endif // CONFIG_LOOPFILTER_LEVEL
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100143 const int smallval = (abs < DELTA_LF_SMALL);
Fangwen Fu231fe422017-04-24 17:52:29 -0700144 if (counts) {
Cheng Chena97394f2017-09-27 15:05:14 -0700145#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -0700146 if (cm->delta_lf_multi) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100147 for (int i = 0; i < abs; ++i) counts->delta_lf_multi[lf_id][i][1]++;
Cheng Chen880166a2017-10-02 17:48:48 -0700148 if (smallval) counts->delta_lf_multi[lf_id][abs][0]++;
149 } else {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100150 for (int i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
Cheng Chen880166a2017-10-02 17:48:48 -0700151 if (smallval) counts->delta_lf[abs][0]++;
152 }
Cheng Chena97394f2017-09-27 15:05:14 -0700153#else
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100154 for (int i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
Fangwen Fu231fe422017-04-24 17:52:29 -0700155 if (smallval) counts->delta_lf[abs][0]++;
Cheng Chena97394f2017-09-27 15:05:14 -0700156#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700157 }
158 if (!smallval) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100159 const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
160 const int thr = (1 << rem_bits) + 1;
Fangwen Fu231fe422017-04-24 17:52:29 -0700161 abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
162 }
163
164 if (abs) {
165 sign = aom_read_bit(r, ACCT_STR);
166 } else {
167 sign = 1;
168 }
169
170 reduced_delta_lflevel = sign ? -abs : abs;
171 }
172 return reduced_delta_lflevel;
173}
174#endif
Thomas Daviesf6936102016-09-05 16:51:31 +0100175
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400176static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
Hui Suaa2965e2017-10-05 15:59:12 -0700177 aom_reader *r,
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400178 PREDICTION_MODE y_mode) {
179 const UV_PREDICTION_MODE uv_mode =
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400180#if CONFIG_CFL
181 aom_read_symbol(r, ec_ctx->uv_mode_cdf[y_mode], UV_INTRA_MODES, ACCT_STR);
182#else
Thomas Davies1bfb5ed2017-01-11 15:28:11 +0000183 read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Luc Trudeau6e1cd782017-06-21 13:52:36 -0400184#endif // CONFIG_CFL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 return uv_mode;
186}
187
Luc Trudeauf5334002017-04-25 12:21:26 -0400188#if CONFIG_CFL
David Michael Barr23198662017-06-19 23:19:48 +0900189static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
David Michael Barrf6eaa152017-07-19 19:42:28 +0900190 int *signs_out) {
191 const int joint_sign =
192 aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
193 int idx = 0;
194 // Magnitudes are only coded for nonzero values
195 if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
196 aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
197 idx = aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
198 << CFL_ALPHABET_SIZE_LOG2;
199 }
200 if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
201 aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
202 idx += aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
203 }
204 *signs_out = joint_sign;
205 return idx;
Luc Trudeauf5334002017-04-25 12:21:26 -0400206}
207#endif
208
Yue Chen670b6602017-10-30 16:57:42 -0700209static INTERINTRA_MODE read_interintra_mode(MACROBLOCKD *xd, aom_reader *r,
210 int size_group) {
Thomas Davies299ff042017-06-27 13:41:59 +0100211 const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
212 r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
213 ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214 FRAME_COUNTS *counts = xd->counts;
215 if (counts) ++counts->interintra_mode[size_group][ii_mode];
216 return ii_mode;
217}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218
Thomas Davies1de6c882017-01-11 17:47:49 +0000219static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700220 aom_reader *r, int16_t ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221 FRAME_COUNTS *counts = xd->counts;
222 int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100223 int is_newmv, is_zeromv, is_refmv;
Thomas Davies149eda52017-06-12 18:11:55 +0100224 is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
Thomas Davies149eda52017-06-12 18:11:55 +0100225 if (is_newmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 if (counts) ++counts->newmv_mode[mode_ctx][0];
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700227 return NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700228 }
229 if (counts) ++counts->newmv_mode[mode_ctx][1];
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700230 if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return GLOBALMV;
231 mode_ctx = (ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
Thomas Davies149eda52017-06-12 18:11:55 +0100232 is_zeromv =
233 aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
Thomas Davies149eda52017-06-12 18:11:55 +0100234 if (is_zeromv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235 if (counts) ++counts->zeromv_mode[mode_ctx][0];
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700236 return GLOBALMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237 }
238 if (counts) ++counts->zeromv_mode[mode_ctx][1];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700239 mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
241 if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
242 if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
Thomas Davies149eda52017-06-12 18:11:55 +0100243 is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
Thomas Davies149eda52017-06-12 18:11:55 +0100244 if (is_refmv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 if (counts) ++counts->refmv_mode[mode_ctx][0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246 return NEARESTMV;
247 } else {
248 if (counts) ++counts->refmv_mode[mode_ctx][1];
249 return NEARMV;
250 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251}
252
Thomas Davies149eda52017-06-12 18:11:55 +0100253static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -0700254 MB_MODE_INFO *mbmi, aom_reader *r) {
255 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 mbmi->ref_mv_idx = 0;
Sebastien Alaiwan34d55662017-11-15 09:36:03 +0100257 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 int idx;
259 for (idx = 0; idx < 2; ++idx) {
260 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700261 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100262 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
Thomas Davies149eda52017-06-12 18:11:55 +0100263 mbmi->ref_mv_idx = idx + drl_idx;
264 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
265 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266 }
267 }
268 }
David Barker3dfba992017-04-03 16:10:09 +0100269 if (have_nearmv_in_inter_mode(mbmi->mode)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 int idx;
271 // Offset the NEARESTMV mode.
272 // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
273 // mode is factored in.
274 for (idx = 1; idx < 3; ++idx) {
275 if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700276 uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
Thomas Davies149eda52017-06-12 18:11:55 +0100277 int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
Thomas Davies149eda52017-06-12 18:11:55 +0100278 mbmi->ref_mv_idx = idx + drl_idx - 1;
279 if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
280 if (!drl_idx) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281 }
282 }
283 }
284}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285
Yunqing Wang3afbf3f2017-11-21 20:16:18 -0800286#if CONFIG_EXT_WARPED_MOTION
287static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
288 MODE_INFO *mi, aom_reader *r, int best) {
289#else
Yaowu Xub24e1152016-10-31 16:28:32 -0700290static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -0800291 MODE_INFO *mi, aom_reader *r) {
Yunqing Wang3afbf3f2017-11-21 20:16:18 -0800292#endif // CONFIG_EXT_WARPED_MOTION
Sarah Parker19234cc2017-03-10 16:43:25 -0800293 MB_MODE_INFO *mbmi = &mi->mbmi;
Thomas Davies04e5aa72017-06-28 14:36:39 +0100294 (void)cm;
Thomas Davies04e5aa72017-06-28 14:36:39 +0100295
Zoe Liuf40a9572017-10-13 12:37:19 -0700296#if CONFIG_EXT_SKIP
297 if (mbmi->skip_mode) return SIMPLE_TRANSLATION;
298#endif // CONFIG_EXT_SKIP
299
Sebastien Alaiwan48795802017-10-30 12:07:13 +0100300 const MOTION_MODE last_motion_mode_allowed =
Sebastien Alaiwan1f56b8e2017-10-31 17:37:16 +0100301 motion_mode_allowed(0, xd->global_motion, xd, mi);
Yue Chen69f18e12016-09-08 14:48:15 -0700302 int motion_mode;
303 FRAME_COUNTS *counts = xd->counts;
Yaowu Xub24e1152016-10-31 16:28:32 -0700304
Yue Chen69f18e12016-09-08 14:48:15 -0700305 if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
Yue Chen80daf0c2017-11-02 17:14:18 -0700306
Yue Chen69f18e12016-09-08 14:48:15 -0700307 if (last_motion_mode_allowed == OBMC_CAUSAL) {
Thomas Daviesd9b57262017-06-27 17:43:25 +0100308 motion_mode =
309 aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2, ACCT_STR);
Yue Chen69f18e12016-09-08 14:48:15 -0700310 if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
311 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
312 } else {
Yunqing Wang3afbf3f2017-11-21 20:16:18 -0800313#if CONFIG_EXT_WARPED_MOTION
314 int wm_ctx = 0;
315 if (best != -1) {
316 wm_ctx = 1;
317 if (mbmi->mode == NEARESTMV) wm_ctx = 2;
318 }
319
320 motion_mode =
321 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[wm_ctx][mbmi->sb_type],
322 MOTION_MODES, ACCT_STR);
323 if (counts) ++counts->motion_mode[wm_ctx][mbmi->sb_type][motion_mode];
324#else
Yaowu Xub24e1152016-10-31 16:28:32 -0700325 motion_mode =
Thomas Davies04e5aa72017-06-28 14:36:39 +0100326 aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
327 MOTION_MODES, ACCT_STR);
Yaowu Xub24e1152016-10-31 16:28:32 -0700328 if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
Yunqing Wang3afbf3f2017-11-21 20:16:18 -0800329#endif // CONFIG_EXT_WARPED_MOTION
330
Yaowu Xub24e1152016-10-31 16:28:32 -0700331 return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yaowu Xub24e1152016-10-31 16:28:32 -0700332 }
333}
Wei-Ting Lin85a8f702017-06-22 13:55:15 -0700334
Yaowu Xuf883b422016-08-30 14:01:10 -0700335static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
336 aom_reader *r, int16_t ctx) {
Thomas Davies8c08a332017-06-26 17:30:34 +0100337 (void)cm;
338 const int mode =
339 aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
340 INTER_COMPOUND_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 FRAME_COUNTS *counts = xd->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 if (counts) ++counts->inter_compound_mode[ctx][mode];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
344 return NEAREST_NEARESTMV + mode;
345}
Zoe Liu85b66462017-04-20 14:28:19 -0700346
Thomas9ac55082016-09-23 18:04:17 +0100347static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700348 return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349}
350
Rostislav Pehlivanovf624dd52017-10-24 16:46:09 +0100351#if CONFIG_Q_SEGMENTATION
352static int neg_deinterleave(int diff, int ref, int max) {
353 if (!ref) return diff;
354 if (ref >= (max - 1)) return max - diff - 1;
355 if (2 * ref < max) {
356 if (diff <= 2 * ref) {
357 if (diff & 1)
358 return ref + ((diff + 1) >> 1);
359 else
360 return ref - (diff >> 1);
361 }
362 return diff;
363 } else {
364 if (diff <= 2 * (max - ref - 1)) {
365 if (diff & 1)
366 return ref + ((diff + 1) >> 1);
367 else
368 return ref - (diff >> 1);
369 }
370 return max - (diff + 1);
371 }
372}
373
374static int read_q_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
375 int mi_row, int mi_col, aom_reader *r) {
376 struct segmentation *const seg = &cm->seg;
377 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
378 struct segmentation_probs *const segp = &ec_ctx->seg;
379 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
380 int prev_ul = 0; /* Top left segment_id */
381 int prev_l = 0; /* Current left segment_id */
382 int prev_u = 0; /* Current top segment_id */
383
384 if (!seg->q_lvls) return 0;
385
386 MODE_INFO *const mi = cm->mi + mi_row * cm->mi_stride + mi_col;
387 int tinfo = mi->mbmi.boundary_info;
388 int above = (!(tinfo & TILE_ABOVE_BOUNDARY)) && ((mi_row - 1) >= 0);
389 int left = (!(tinfo & TILE_LEFT_BOUNDARY)) && ((mi_col - 1) >= 0);
390
391 if (above && left)
392 prev_ul =
393 get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row - 1, mi_col - 1);
394
395 if (above)
396 prev_u = get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row - 1, mi_col);
397
398 if (left)
399 prev_l = get_segment_id(cm, cm->q_seg_map, BLOCK_4X4, mi_row, mi_col - 1);
400
401 int cdf_num = pick_q_seg_cdf(prev_ul, prev_u, prev_l);
402 int pred = pick_q_seg_pred(prev_ul, prev_u, prev_l);
403
404 if (mbmi->skip) {
405 set_q_segment_id(cm, cm->q_seg_map, mbmi->sb_type, mi_row, mi_col, pred);
406 return 0;
407 }
408
Rostislav Pehlivanovf624dd52017-10-24 16:46:09 +0100409 aom_cdf_prob *pred_cdf = segp->q_seg_cdf[cdf_num];
410 int coded_id = aom_read_symbol(r, pred_cdf, 8, ACCT_STR);
Rostislav Pehlivanovf624dd52017-10-24 16:46:09 +0100411
412 int segment_id = neg_deinterleave(coded_id, pred, seg->q_lvls);
413
414 assert(segment_id >= 0 && segment_id < seg->q_lvls);
415 set_q_segment_id(cm, cm->q_seg_map, mbmi->sb_type, mi_row, mi_col,
416 segment_id);
417
418 return segment_id;
419}
420#endif
421
Yaowu Xuf883b422016-08-30 14:01:10 -0700422static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423 MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
Jingning Han94d5bfc2016-10-21 10:14:36 -0700424 TX_SIZE tx_size, int depth, int blk_row,
425 int blk_col, aom_reader *r) {
Thomas Davies985bfc32017-06-27 16:51:26 +0100426 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
427 (void)cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 int is_split = 0;
429 const int tx_row = blk_row >> 1;
430 const int tx_col = blk_col >> 1;
Jingning Hanf64062f2016-11-02 16:22:18 -0700431 const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
432 const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
Jingning Han331662e2017-05-30 17:03:32 -0700433 int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
434 xd->left_txfm_context + blk_row,
Jingning Hanc8b89362016-11-01 10:28:53 -0700435 mbmi->sb_type, tx_size);
clang-format67948d32016-09-07 22:40:40 -0700436 TX_SIZE(*const inter_tx_size)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 [MAX_MIB_SIZE] =
438 (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700439 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
David Barker16c64e32017-08-23 16:54:59 +0100440 assert(tx_size > TX_4X4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441
Jingning Han571189c2016-10-24 10:38:43 -0700442 if (depth == MAX_VARTX_DEPTH) {
Jingning Han94d5bfc2016-10-21 10:14:36 -0700443 int idx, idy;
444 inter_tx_size[0][0] = tx_size;
Yue Chenc5252a62017-10-31 15:41:12 -0700445 for (idy = 0; idy < AOMMAX(1, tx_size_high_unit[tx_size] / 2); ++idy)
446 for (idx = 0; idx < AOMMAX(1, tx_size_wide_unit[tx_size] / 2); ++idx)
Jingning Han94d5bfc2016-10-21 10:14:36 -0700447 inter_tx_size[idy][idx] = tx_size;
448 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700449 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Jingning Han331662e2017-05-30 17:03:32 -0700450 txfm_partition_update(xd->above_txfm_context + blk_col,
451 xd->left_txfm_context + blk_row, tx_size, tx_size);
Jingning Han94d5bfc2016-10-21 10:14:36 -0700452 return;
453 }
454
Thomas Davies985bfc32017-06-27 16:51:26 +0100455 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456
457 if (is_split) {
Jingning Hanf64062f2016-11-02 16:22:18 -0700458 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
Yue Chen0797a202017-10-27 17:24:56 -0700459 const int bsw = tx_size_wide_unit[sub_txs];
460 const int bsh = tx_size_high_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461
462 if (counts) ++counts->txfm_partition[ctx][1];
463
David Barker16c64e32017-08-23 16:54:59 +0100464 if (sub_txs == TX_4X4) {
Jingning Han9ca05b72017-01-03 14:41:36 -0800465 int idx, idy;
Jingning Hanab9ecba2017-01-13 09:11:58 -0800466 inter_tx_size[0][0] = sub_txs;
Yue Chenc5252a62017-10-31 15:41:12 -0700467 for (idy = 0; idy < AOMMAX(1, tx_size_high_unit[tx_size] / 2); ++idy)
468 for (idx = 0; idx < AOMMAX(1, tx_size_wide_unit[tx_size] / 2); ++idx)
Jingning Han581d1692017-01-05 16:03:54 -0800469 inter_tx_size[idy][idx] = inter_tx_size[0][0];
Jingning Hanab9ecba2017-01-13 09:11:58 -0800470 mbmi->tx_size = sub_txs;
Jingning Hane67b38a2016-11-04 10:30:00 -0700471 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
Jingning Han331662e2017-05-30 17:03:32 -0700472 txfm_partition_update(xd->above_txfm_context + blk_col,
473 xd->left_txfm_context + blk_row, sub_txs, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 return;
475 }
476
Yue Chen0797a202017-10-27 17:24:56 -0700477 assert(bsw > 0 && bsh > 0);
478 for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
479 for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
480 int offsetr = blk_row + row;
481 int offsetc = blk_col + col;
482 read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
483 offsetc, r);
484 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485 }
486 } else {
487 int idx, idy;
488 inter_tx_size[0][0] = tx_size;
Yue Chenc5252a62017-10-31 15:41:12 -0700489 for (idy = 0; idy < AOMMAX(1, tx_size_high_unit[tx_size] / 2); ++idy)
490 for (idx = 0; idx < AOMMAX(1, tx_size_wide_unit[tx_size] / 2); ++idx)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700491 inter_tx_size[idy][idx] = tx_size;
492 mbmi->tx_size = tx_size;
Jingning Hane67b38a2016-11-04 10:30:00 -0700493 mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700494 if (counts) ++counts->txfm_partition[ctx][0];
Jingning Han331662e2017-05-30 17:03:32 -0700495 txfm_partition_update(xd->above_txfm_context + blk_col,
496 xd->left_txfm_context + blk_row, tx_size, tx_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700497 }
498}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499
Yaowu Xuf883b422016-08-30 14:01:10 -0700500static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
Debargha Mukherjeed2cfbef2017-12-03 16:15:27 -0800501 int is_inter, aom_reader *r) {
502 // TODO(debargha): Clean up the logic here. This function should only
503 // be called for intra.
504 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
505 const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
506 : intra_tx_size_cat_lookup[bsize];
507 const int max_depths = bsize_to_max_depth(bsize);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 const int ctx = get_tx_size_context(xd);
Thomas Davies15580c52017-03-09 13:53:42 +0000509 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
510 (void)cm;
Thomas Davies15580c52017-03-09 13:53:42 +0000511
Nathan E. Egge476c63c2017-05-18 18:35:16 -0400512 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
Debargha Mukherjee6147b1b2017-11-08 08:31:09 -0800513 max_depths + 1, ACCT_STR);
514 assert(depth >= 0 && depth <= max_depths);
Debargha Mukherjeed2cfbef2017-12-03 16:15:27 -0800515 const TX_SIZE tx_size = depth_to_tx_size(depth, bsize);
Jingning Han4e1737a2016-10-25 16:05:02 -0700516 return tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517}
518
Urvang Joshifeb925f2016-12-05 10:37:29 -0800519static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
520 int allow_select_inter, aom_reader *r) {
521 const TX_MODE tx_mode = cm->tx_mode;
522 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523 if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Rupert Swarbrickfcff0b22017-10-05 09:26:04 +0100524
525 if (block_signals_txsize(bsize)) {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800526 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
Debargha Mukherjeed2cfbef2017-12-03 16:15:27 -0800527 const TX_SIZE coded_tx_size = read_selected_tx_size(cm, xd, is_inter, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 return coded_tx_size;
529 } else {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800530 return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531 }
532 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
Yue Chen0797a202017-10-27 17:24:56 -0700534 return get_max_rect_tx_size(bsize, is_inter);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 }
536}
537
Yaowu Xuf883b422016-08-30 14:01:10 -0700538static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539 int mi_offset, int x_mis, int y_mis) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100540 int segment_id = INT_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100542 for (int y = 0; y < y_mis; y++)
543 for (int x = 0; x < x_mis; x++)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544 segment_id =
Yaowu Xuf883b422016-08-30 14:01:10 -0700545 AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700546
547 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
548 return segment_id;
549}
550
Yaowu Xuf883b422016-08-30 14:01:10 -0700551static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 int segment_id) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553 assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
554
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100555 for (int y = 0; y < y_mis; y++)
556 for (int x = 0; x < x_mis; x++)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557 cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
558}
559
Yaowu Xuf883b422016-08-30 14:01:10 -0700560static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561 int mi_offset, int x_mis, int y_mis,
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563 struct segmentation *const seg = &cm->seg;
564 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100565 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100566 struct segmentation_probs *const segp = &ec_ctx->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700567 int segment_id;
568
569 if (!seg->enabled) return 0; // Default for disabled segmentation
570
571 assert(seg->update_map && !seg->temporal_update);
572
573 segment_id = read_segment_id(r, segp);
574 if (counts) ++counts->seg.tree_total[segment_id];
575 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
576 return segment_id;
577}
578
Yaowu Xuf883b422016-08-30 14:01:10 -0700579static void copy_segment_id(const AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700580 const uint8_t *last_segment_ids,
581 uint8_t *current_segment_ids, int mi_offset,
582 int x_mis, int y_mis) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100583 for (int y = 0; y < y_mis; y++)
584 for (int x = 0; x < x_mis; x++)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585 current_segment_ids[mi_offset + y * cm->mi_cols + x] =
586 last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
587 : 0;
588}
589
Yaowu Xuf883b422016-08-30 14:01:10 -0700590static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
591 int mi_row, int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592 struct segmentation *const seg = &cm->seg;
593 FRAME_COUNTS *counts = xd->counts;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100594 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies9f5cedd2017-07-10 09:20:32 +0100595 struct segmentation_probs *const segp = &ec_ctx->seg;
596
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
598 int predicted_segment_id, segment_id;
599 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Hanc709e1f2016-12-06 14:48:09 -0800600 const int bw = mi_size_wide[mbmi->sb_type];
601 const int bh = mi_size_high[mbmi->sb_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602
603 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -0700604 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
605 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606
607 if (!seg->enabled) return 0; // Default for disabled segmentation
608
609 predicted_segment_id = cm->last_frame_seg_map
610 ? dec_get_segment_id(cm, cm->last_frame_seg_map,
611 mi_offset, x_mis, y_mis)
612 : 0;
613
614 if (!seg->update_map) {
615 copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
616 mi_offset, x_mis, y_mis);
617 return predicted_segment_id;
618 }
619
620 if (seg->temporal_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700621 const int ctx = av1_get_pred_context_seg_id(xd);
Thomas Davies00021352017-07-11 16:07:55 +0100622 aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
623 mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624 if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
625 if (mbmi->seg_id_predicted) {
626 segment_id = predicted_segment_id;
627 } else {
628 segment_id = read_segment_id(r, segp);
629 if (counts) ++counts->seg.tree_mispred[segment_id];
630 }
631 } else {
632 segment_id = read_segment_id(r, segp);
633 if (counts) ++counts->seg.tree_total[segment_id];
634 }
635 set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
636 return segment_id;
637}
638
Zoe Liuf704a1c2017-10-02 16:55:59 -0700639#if CONFIG_EXT_SKIP
640static int read_skip_mode(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
641 aom_reader *r) {
Zoe Liuf40a9572017-10-13 12:37:19 -0700642 if (!cm->skip_mode_flag) return 0;
Zoe Liuf704a1c2017-10-02 16:55:59 -0700643
644 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
645 // TODO(zoeliu): To revisit the handling of this scenario.
646 return 0;
Zoe Liuf704a1c2017-10-02 16:55:59 -0700647 }
Zoe Liuf40a9572017-10-13 12:37:19 -0700648
649 if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type)) return 0;
650
651 const int ctx = av1_get_skip_mode_context(xd);
652 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
653 const int skip_mode =
654 aom_read_symbol(r, ec_ctx->skip_mode_cdfs[ctx], 2, ACCT_STR);
655 FRAME_COUNTS *counts = xd->counts;
656 if (counts) ++counts->skip_mode[ctx][skip_mode];
657
658 return skip_mode;
Zoe Liuf704a1c2017-10-02 16:55:59 -0700659}
660#endif // CONFIG_EXT_SKIP
661
Yaowu Xuf883b422016-08-30 14:01:10 -0700662static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
663 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
665 return 1;
666 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 const int ctx = av1_get_skip_context(xd);
Thomas Davies61e3e372017-04-04 16:10:23 +0100668 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
669 const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670 FRAME_COUNTS *counts = xd->counts;
671 if (counts) ++counts->skip[ctx][skip];
672 return skip;
673 }
674}
675
hui su33567b22017-04-30 16:40:19 -0700676#if CONFIG_PALETTE_DELTA_ENCODING
Hui Su4494ae42017-10-04 12:22:44 -0700677// Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
678// and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
679// one single sorted list(colors[...]).
680static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
681 int n_colors, int n_cached_colors) {
682 if (n_cached_colors == 0) return;
683 int cache_idx = 0, trans_idx = n_cached_colors;
684 for (int i = 0; i < n_colors; ++i) {
685 if (cache_idx < n_cached_colors &&
686 (trans_idx >= n_colors ||
687 cached_colors[cache_idx] <= colors[trans_idx])) {
688 colors[i] = cached_colors[cache_idx++];
689 } else {
690 assert(trans_idx < n_colors);
691 colors[i] = colors[trans_idx++];
692 }
693 }
hui su33567b22017-04-30 16:40:19 -0700694}
hui su33567b22017-04-30 16:40:19 -0700695
696static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
697 PALETTE_MODE_INFO *const pmi, aom_reader *r) {
698 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su4494ae42017-10-04 12:22:44 -0700699 uint16_t cached_colors[PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700700 const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
hui su33567b22017-04-30 16:40:19 -0700701 const int n = pmi->palette_size[0];
702 int idx = 0;
703 for (int i = 0; i < n_cache && idx < n; ++i)
Hui Su4494ae42017-10-04 12:22:44 -0700704 if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
hui su33567b22017-04-30 16:40:19 -0700705 if (idx < n) {
Hui Su4494ae42017-10-04 12:22:44 -0700706 const int n_cached_colors = idx;
hui su33567b22017-04-30 16:40:19 -0700707 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
708 if (idx < n) {
709 const int min_bits = bit_depth - 3;
710 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
711 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
712 for (; idx < n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100713 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700714 const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100715 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
716 0, (1 << bit_depth) - 1);
717 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700718 bits = AOMMIN(bits, av1_ceil_log2(range));
719 }
720 }
Hui Su4494ae42017-10-04 12:22:44 -0700721 merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
722 } else {
723 memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
hui su33567b22017-04-30 16:40:19 -0700724 }
hui su33567b22017-04-30 16:40:19 -0700725}
726
727static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
728 PALETTE_MODE_INFO *const pmi,
729 aom_reader *r) {
730 const int n = pmi->palette_size[1];
731 // U channel colors.
732 uint16_t color_cache[2 * PALETTE_MAX_SIZE];
Hui Su4494ae42017-10-04 12:22:44 -0700733 uint16_t cached_colors[PALETTE_MAX_SIZE];
Hui Su3748bc22017-08-23 11:30:41 -0700734 const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
Hui Su4494ae42017-10-04 12:22:44 -0700735 int idx = 0;
736 for (int i = 0; i < n_cache && idx < n; ++i)
737 if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
738 if (idx < n) {
739 const int n_cached_colors = idx;
740 idx += PALETTE_MAX_SIZE;
hui su33567b22017-04-30 16:40:19 -0700741 pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
742 if (idx < PALETTE_MAX_SIZE + n) {
743 const int min_bits = bit_depth - 3;
744 int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
745 int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
746 for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100747 assert(range >= 0);
hui su33567b22017-04-30 16:40:19 -0700748 const int delta = aom_read_literal(r, bits, ACCT_STR);
Jonathan Matthewsdc0e1122017-09-22 12:20:36 +0100749 pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
750 0, (1 << bit_depth) - 1);
751 range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
hui su33567b22017-04-30 16:40:19 -0700752 bits = AOMMIN(bits, av1_ceil_log2(range));
753 }
754 }
Hui Su4494ae42017-10-04 12:22:44 -0700755 merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
756 n_cached_colors);
757 } else {
758 memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
759 n * sizeof(cached_colors[0]));
hui su33567b22017-04-30 16:40:19 -0700760 }
hui su33567b22017-04-30 16:40:19 -0700761
762 // V channel colors.
763 if (aom_read_bit(r, ACCT_STR)) { // Delta encoding.
764 const int min_bits_v = bit_depth - 4;
765 const int max_val = 1 << bit_depth;
766 int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
767 pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
768 aom_read_literal(r, bit_depth, ACCT_STR);
769 for (int i = 1; i < n; ++i) {
770 int delta = aom_read_literal(r, bits, ACCT_STR);
771 if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
772 int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
773 if (val < 0) val += max_val;
774 if (val >= max_val) val -= max_val;
775 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
776 }
777 } else {
778 for (int i = 0; i < n; ++i) {
779 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
780 aom_read_literal(r, bit_depth, ACCT_STR);
781 }
782 }
783}
784#endif // CONFIG_PALETTE_DELTA_ENCODING
785
Yaowu Xuf883b422016-08-30 14:01:10 -0700786static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
787 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 MODE_INFO *const mi = xd->mi[0];
789 MB_MODE_INFO *const mbmi = &mi->mbmi;
790 const MODE_INFO *const above_mi = xd->above_mi;
791 const MODE_INFO *const left_mi = xd->left_mi;
792 const BLOCK_SIZE bsize = mbmi->sb_type;
Hui Su473cf892017-11-08 18:14:31 -0800793 assert(av1_allow_palette(cm->allow_screen_content_tools, bsize));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100795 const int block_palette_idx = bsize - BLOCK_8X8;
796
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 if (mbmi->mode == DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800798 int palette_y_mode_ctx = 0;
hui su40b9e7f2017-07-13 18:15:56 -0700799 if (above_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800800 palette_y_mode_ctx +=
801 (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700802 }
803 if (left_mi) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800804 palette_y_mode_ctx +=
805 (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
hui su40b9e7f2017-07-13 18:15:56 -0700806 }
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100807 const int modev = aom_read_symbol(
Thomas Davies59f92312017-08-23 00:33:12 +0100808 r,
809 xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
810 2, ACCT_STR);
Thomas Davies59f92312017-08-23 00:33:12 +0100811 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 pmi->palette_size[0] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100813 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100814 xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100815 PALETTE_SIZES, ACCT_STR) +
816 2;
hui sud13c24a2017-04-07 16:13:07 -0700817#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700818 read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700819#else
hui su40b9e7f2017-07-13 18:15:56 -0700820 for (int i = 0; i < pmi->palette_size[0]; ++i)
Michael Bebenita6048d052016-08-25 14:40:54 -0700821 pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui sud13c24a2017-04-07 16:13:07 -0700822#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 }
824 }
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400825 if (mbmi->uv_mode == UV_DC_PRED) {
Urvang Joshi23a61112017-01-30 14:59:27 -0800826 const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +0100827 const int modev = aom_read_symbol(
Thomas Davies59f92312017-08-23 00:33:12 +0100828 r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
Thomas Davies59f92312017-08-23 00:33:12 +0100829 if (modev) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 pmi->palette_size[1] =
Thomas Daviesce7272d2017-07-04 16:11:08 +0100831 aom_read_symbol(r,
Rupert Swarbrick6f9cd942017-08-02 15:57:18 +0100832 xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
Thomas Daviesce7272d2017-07-04 16:11:08 +0100833 PALETTE_SIZES, ACCT_STR) +
834 2;
hui sud13c24a2017-04-07 16:13:07 -0700835#if CONFIG_PALETTE_DELTA_ENCODING
hui su33567b22017-04-30 16:40:19 -0700836 read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui sud13c24a2017-04-07 16:13:07 -0700837#else
hui su40b9e7f2017-07-13 18:15:56 -0700838 for (int i = 0; i < pmi->palette_size[1]; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700840 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841 pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita6048d052016-08-25 14:40:54 -0700842 aom_read_literal(r, cm->bit_depth, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 }
hui sud13c24a2017-04-07 16:13:07 -0700844#endif // CONFIG_PALETTE_DELTA_ENCODING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 }
846 }
847}
848
hui su5db97432016-10-14 16:10:14 -0700849#if CONFIG_FILTER_INTRA
Yue Chen4eba69b2017-11-09 22:37:35 -0800850static void read_filter_intra_mode_info(MACROBLOCKD *const xd, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851 MODE_INFO *const mi = xd->mi[0];
852 MB_MODE_INFO *const mbmi = &mi->mbmi;
hui su5db97432016-10-14 16:10:14 -0700853 FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
854 &mbmi->filter_intra_mode_info;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
Yue Chen95e13e22017-11-01 23:56:35 -0700856 if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0 &&
857 av1_filter_intra_allowed_txsize(mbmi->tx_size)) {
Yue Chen4eba69b2017-11-09 22:37:35 -0800858 filter_intra_mode_info->use_filter_intra_mode[0] = aom_read_symbol(
859 r, xd->tile_ctx->filter_intra_cdfs[mbmi->tx_size], 2, ACCT_STR);
hui su5db97432016-10-14 16:10:14 -0700860 if (filter_intra_mode_info->use_filter_intra_mode[0]) {
861 filter_intra_mode_info->filter_intra_mode[0] =
Yue Chen63ce36f2017-10-10 23:37:31 -0700862 aom_read_symbol(r, xd->tile_ctx->filter_intra_mode_cdf[0],
863 FILTER_INTRA_MODES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866}
hui su5db97432016-10-14 16:10:14 -0700867#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868
hui su5db97432016-10-14 16:10:14 -0700869#if CONFIG_EXT_INTRA
Joe Young3ca43bf2017-10-06 15:12:46 -0700870#if CONFIG_EXT_INTRA_MOD
871static int read_angle_delta(aom_reader *r, aom_cdf_prob *cdf) {
872 const int sym = aom_read_symbol(r, cdf, 2 * MAX_ANGLE_DELTA + 1, ACCT_STR);
873 return sym - MAX_ANGLE_DELTA;
874}
875#endif // CONFIG_EXT_INTRA_MOD
876
Hui Su259d4422017-10-13 10:08:17 -0700877static void read_intra_angle_info(MACROBLOCKD *const xd, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
879 const BLOCK_SIZE bsize = mbmi->sb_type;
Joe Young3ca43bf2017-10-06 15:12:46 -0700880#if CONFIG_EXT_INTRA_MOD
881 FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
882#endif // CONFIG_EXT_INTRA_MOD
883
Joe Young830d4ce2017-05-30 17:48:13 -0700884 mbmi->angle_delta[0] = 0;
885 mbmi->angle_delta[1] = 0;
Joe Young830d4ce2017-05-30 17:48:13 -0700886 if (!av1_use_angle_delta(bsize)) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887
hui su45dc5972016-12-08 17:42:50 -0800888 if (av1_is_directional_mode(mbmi->mode, bsize)) {
Joe Young3ca43bf2017-10-06 15:12:46 -0700889#if CONFIG_EXT_INTRA_MOD
890 mbmi->angle_delta[0] =
891 read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED]);
892#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 mbmi->angle_delta[0] =
hui su40b9e7f2017-07-13 18:15:56 -0700894 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Joe Young3ca43bf2017-10-06 15:12:46 -0700895#endif // CONFIG_EXT_INTRA_MOD
Yaowu Xuc27fc142016-08-22 16:08:15 -0700896 }
897
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400898 if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
Joe Young3ca43bf2017-10-06 15:12:46 -0700899#if CONFIG_EXT_INTRA_MOD
900 mbmi->angle_delta[1] =
901 read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->uv_mode - V_PRED]);
902#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903 mbmi->angle_delta[1] =
hui su40b9e7f2017-07-13 18:15:56 -0700904 av1_read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
Joe Young3ca43bf2017-10-06 15:12:46 -0700905#endif // CONFIG_EXT_INTRA_MOD
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906 }
907}
908#endif // CONFIG_EXT_INTRA
909
Angie Chianga9f9a312017-04-13 16:40:43 -0700910void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700911#if CONFIG_TXK_SEL
Jingning Han19b5c8f2017-07-06 15:10:12 -0700912 int blk_row, int blk_col, int block, int plane,
913 TX_SIZE tx_size,
Angie Chianga9f9a312017-04-13 16:40:43 -0700914#endif
915 aom_reader *r) {
916 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Hanab7163d2016-11-04 09:46:35 -0700917 const int inter_block = is_inter_block(mbmi);
Jingning Han243b66b2017-06-23 12:11:47 -0700918#if !CONFIG_TXK_SEL
Debargha Mukherjee5577bd12017-11-20 16:04:26 -0800919 const TX_SIZE mtx_size =
920 get_max_rect_tx_size(xd->mi[0]->mbmi.sb_type, inter_block);
Sarah Parker90024e42017-10-06 16:50:47 -0700921 const TX_SIZE tx_size =
Debargha Mukherjee5577bd12017-11-20 16:04:26 -0800922 inter_block ? AOMMAX(sub_tx_size_map[mtx_size], mbmi->min_tx_size)
Sarah Parker90024e42017-10-06 16:50:47 -0700923 : mbmi->tx_size;
Jingning Han243b66b2017-06-23 12:11:47 -0700924#endif // !CONFIG_TXK_SEL
Thomas Daviescef09622017-01-11 17:27:12 +0000925 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviescef09622017-01-11 17:27:12 +0000926
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700927#if !CONFIG_TXK_SEL
Angie Chianga9f9a312017-04-13 16:40:43 -0700928 TX_TYPE *tx_type = &mbmi->tx_type;
929#else
Angie Chiang39b06eb2017-04-14 09:52:29 -0700930 // only y plane's tx_type is transmitted
931 if (plane > 0) return;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700932 (void)block;
Angie Chiangbce07f12017-12-01 16:34:31 -0800933 TX_TYPE *tx_type = &mbmi->txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col];
Angie Chianga9f9a312017-04-13 16:40:43 -0700934#endif
935
Jingning Hanab7163d2016-11-04 09:46:35 -0700936 if (!FIXED_TX_TYPE) {
Urvang Joshifeb925f2016-12-05 10:37:29 -0800937 const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
Sarah Parkere68a3e42017-02-16 14:03:24 -0800938 if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
939 cm->reduced_tx_set_used) > 1 &&
Yue Cheneeacc4c2017-01-17 17:29:17 -0800940 ((!cm->seg.enabled && cm->base_qindex > 0) ||
941 (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
942 !mbmi->skip &&
Jingning Hanab7163d2016-11-04 09:46:35 -0700943 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
Hui Suddbcde22017-09-18 17:22:02 -0700944 const TxSetType tx_set_type = get_ext_tx_set_type(
945 tx_size, mbmi->sb_type, inter_block, cm->reduced_tx_set_used);
Sarah Parkere68a3e42017-02-16 14:03:24 -0800946 const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
947 cm->reduced_tx_set_used);
Sarah Parker784596d2017-06-23 08:41:26 -0700948 // eset == 0 should correspond to a set with only DCT_DCT and
949 // there is no need to read the tx_type
950 assert(eset != 0);
Lester Lu432012f2017-08-17 14:39:29 -0700951
Jingning Hanab7163d2016-11-04 09:46:35 -0700952 if (inter_block) {
Hui Suddbcde22017-09-18 17:22:02 -0700953 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -0700954 r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
Hui Suddbcde22017-09-18 17:22:02 -0700955 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Jingning Hanab7163d2016-11-04 09:46:35 -0700956 } else if (ALLOW_INTRA_EXT_TX) {
Yue Chen57b8ff62017-10-10 23:37:31 -0700957#if CONFIG_FILTER_INTRA
958 PREDICTION_MODE intra_dir;
959 if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0])
960 intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
961 .filter_intra_mode[0]];
962 else
963 intra_dir = mbmi->mode;
964 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
965 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_dir],
966 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
967#else
Hui Suddbcde22017-09-18 17:22:02 -0700968 *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
Sarah Parker784596d2017-06-23 08:41:26 -0700969 r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
Hui Suddbcde22017-09-18 17:22:02 -0700970 av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
Yue Chen57b8ff62017-10-10 23:37:31 -0700971#endif
Jingning Hanab7163d2016-11-04 09:46:35 -0700972 }
973 } else {
Angie Chianga9f9a312017-04-13 16:40:43 -0700974 *tx_type = DCT_DCT;
Jingning Hanab7163d2016-11-04 09:46:35 -0700975 }
Jingning Hanab7163d2016-11-04 09:46:35 -0700976 }
Nathan E. Eggeebced372017-02-17 20:57:21 -0500977#if FIXED_TX_TYPE
978 assert(mbmi->tx_type == DCT_DCT);
979#endif
Jingning Hanab7163d2016-11-04 09:46:35 -0700980}
981
Alex Converse28744302017-04-13 14:46:22 -0700982#if CONFIG_INTRABC
983static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Hui Su21b67222017-11-16 16:17:59 -0800984 nmv_context *ctx, MvSubpelPrecision precision);
Alex Converse28744302017-04-13 14:46:22 -0700985
986static INLINE int is_mv_valid(const MV *mv);
987
988static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
989 const int_mv *ref_mv, int mi_row, int mi_col,
990 BLOCK_SIZE bsize, aom_reader *r) {
Alex Converse28744302017-04-13 14:46:22 -0700991 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
992 (void)cm;
Hui Su21b67222017-11-16 16:17:59 -0800993 read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, MV_SUBPEL_NONE);
Hui Su1e6bf6b2017-11-02 13:00:29 -0700994 // DV should not have sub-pel.
995 assert((mv->as_mv.col & 7) == 0);
996 assert((mv->as_mv.row & 7) == 0);
Hui Su30483c92017-11-15 11:30:03 -0800997 mv->as_mv.col = (mv->as_mv.col >> 3) * 8;
998 mv->as_mv.row = (mv->as_mv.row >> 3) * 8;
Alex Converse28744302017-04-13 14:46:22 -0700999 int valid = is_mv_valid(&mv->as_mv) &&
Hui Su64463e72017-11-06 12:36:00 -08001000 av1_is_dv_valid(mv->as_mv, &xd->tile, mi_row, mi_col, bsize,
1001 cm->mib_size_log2);
Alex Converse28744302017-04-13 14:46:22 -07001002 return valid;
1003}
1004#endif // CONFIG_INTRABC
1005
Hui Suc2232cf2017-10-11 17:32:56 -07001006#if CONFIG_INTRABC
1007static void read_intrabc_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1008 int mi_row, int mi_col, aom_reader *r) {
1009 MODE_INFO *const mi = xd->mi[0];
1010 MB_MODE_INFO *const mbmi = &mi->mbmi;
1011 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1012 mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
1013 if (mbmi->use_intrabc) {
Hui Su12546aa2017-10-13 16:10:01 -07001014 const BLOCK_SIZE bsize = mbmi->sb_type;
1015 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
1016 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
1017 int idx, idy;
1018 if ((cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
1019 !xd->lossless[mbmi->segment_id] && !mbmi->skip)) {
Yue Chen0797a202017-10-27 17:24:56 -07001020 const TX_SIZE max_tx_size = get_max_rect_tx_size(bsize, 0);
Hui Su12546aa2017-10-13 16:10:01 -07001021 const int bh = tx_size_high_unit[max_tx_size];
1022 const int bw = tx_size_wide_unit[max_tx_size];
Hui Su12546aa2017-10-13 16:10:01 -07001023 mbmi->min_tx_size = TX_SIZES_ALL;
1024 for (idy = 0; idy < height; idy += bh) {
1025 for (idx = 0; idx < width; idx += bw) {
Debargha Mukherjeeedc73462017-10-31 15:13:32 -07001026 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size, 0, idy, idx,
1027 r);
Hui Su12546aa2017-10-13 16:10:01 -07001028 }
1029 }
1030 } else {
1031 mbmi->tx_size = read_tx_size(cm, xd, 1, !mbmi->skip, r);
1032 for (idy = 0; idy < height; ++idy)
1033 for (idx = 0; idx < width; ++idx)
1034 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
1035 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
1036 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
1037 }
Hui Suc2232cf2017-10-11 17:32:56 -07001038 mbmi->mode = mbmi->uv_mode = UV_DC_PRED;
1039 mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
1040
1041 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1042 int_mv ref_mvs[MAX_MV_REF_CANDIDATES];
1043
1044 av1_find_mv_refs(cm, xd, mi, INTRA_FRAME, &xd->ref_mv_count[INTRA_FRAME],
1045 xd->ref_mv_stack[INTRA_FRAME], NULL, ref_mvs, mi_row,
1046 mi_col, NULL, NULL, inter_mode_ctx);
1047
1048 int_mv nearestmv, nearmv;
Hui Suc2232cf2017-10-11 17:32:56 -07001049
RogerZhou10a03802017-10-26 11:49:48 -07001050#if CONFIG_AMVR
1051 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv, 0);
1052#else
1053 av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
1054#endif
Hui Suc2232cf2017-10-11 17:32:56 -07001055 int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
1056 if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
Hui Su1e6bf6b2017-11-02 13:00:29 -07001057 // Ref DV should not have sub-pel.
1058 assert((dv_ref.as_mv.col & 7) == 0);
1059 assert((dv_ref.as_mv.row & 7) == 0);
Hui Su30483c92017-11-15 11:30:03 -08001060 dv_ref.as_mv.col = (dv_ref.as_mv.col >> 3) * 8;
1061 dv_ref.as_mv.row = (dv_ref.as_mv.row >> 3) * 8;
Hui Suc2232cf2017-10-11 17:32:56 -07001062 xd->corrupted |=
1063 !assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, mi_row, mi_col, bsize, r);
Sebastien Alaiwan3bac9922017-11-02 12:34:41 +01001064#if !CONFIG_TXK_SEL
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02001065 av1_read_tx_type(cm, xd, r);
Sebastien Alaiwan3bac9922017-11-02 12:34:41 +01001066#endif // !CONFIG_TXK_SEL
Hui Suc2232cf2017-10-11 17:32:56 -07001067 }
1068}
1069#endif // CONFIG_INTRABC
1070
Yaowu Xuf883b422016-08-30 14:01:10 -07001071static void read_intra_frame_mode_info(AV1_COMMON *const cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001072 MACROBLOCKD *const xd, int mi_row,
Yaowu Xuf883b422016-08-30 14:01:10 -07001073 int mi_col, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001074 MODE_INFO *const mi = xd->mi[0];
1075 MB_MODE_INFO *const mbmi = &mi->mbmi;
1076 const MODE_INFO *above_mi = xd->above_mi;
1077 const MODE_INFO *left_mi = xd->left_mi;
1078 const BLOCK_SIZE bsize = mbmi->sb_type;
1079 int i;
1080 const int mi_offset = mi_row * cm->mi_cols + mi_col;
Jingning Han85dc03f2016-12-06 16:03:10 -08001081 const int bw = mi_size_wide[bsize];
1082 const int bh = mi_size_high[bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001083
1084 // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xuf883b422016-08-30 14:01:10 -07001085 const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
1086 const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001087 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001088
1089 mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
1090 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
Steinar Midtskogen6c24b022017-09-15 09:46:39 +02001091
Rostislav Pehlivanovf624dd52017-10-24 16:46:09 +01001092#if CONFIG_Q_SEGMENTATION
1093 mbmi->q_segment_id = read_q_segment_id(cm, xd, mi_row, mi_col, r);
1094#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001095
Steinar Midtskogen6c24b022017-09-15 09:46:39 +02001096 read_cdef(cm, r, mbmi, mi_col, mi_row);
1097
Arild Fuldseth07441162016-08-15 15:07:52 +02001098 if (cm->delta_q_present_flag) {
Thomas Daviesf6936102016-09-05 16:51:31 +01001099 xd->current_qindex =
1100 xd->prev_qindex +
1101 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
Arild Fuldseth (arilfuld)54de7d62017-03-20 13:07:11 +01001102 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
1103 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
Thomas Daviesf6936102016-09-05 16:51:31 +01001104 xd->prev_qindex = xd->current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -07001105#if CONFIG_EXT_DELTA_Q
1106 if (cm->delta_lf_present_flag) {
Cheng Chena97394f2017-09-27 15:05:14 -07001107#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen880166a2017-10-02 17:48:48 -07001108 if (cm->delta_lf_multi) {
1109 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
Cheng Chenaff479f2017-10-26 16:53:10 -07001110 const int tmp_lvl =
Cheng Chen880166a2017-10-02 17:48:48 -07001111 xd->prev_delta_lf[lf_id] +
1112 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
1113 cm->delta_lf_res;
Cheng Chenaff479f2017-10-26 16:53:10 -07001114 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
1115 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Cheng Chen880166a2017-10-02 17:48:48 -07001116 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
1117 }
1118 } else {
Cheng Chenaff479f2017-10-26 16:53:10 -07001119 const int tmp_lvl =
Cheng Chen880166a2017-10-02 17:48:48 -07001120 xd->prev_delta_lf_from_base +
1121 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07001122 cm->delta_lf_res;
Cheng Chenaff479f2017-10-26 16:53:10 -07001123 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
1124 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Cheng Chen880166a2017-10-02 17:48:48 -07001125 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001126 }
1127#else
Cheng Chen9dccdf22017-10-02 15:04:40 -07001128 const int current_delta_lf_from_base =
Fangwen Fu231fe422017-04-24 17:52:29 -07001129 xd->prev_delta_lf_from_base +
1130 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
1131 cm->delta_lf_res;
Cheng Chen9dccdf22017-10-02 15:04:40 -07001132 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
Cheng Chenaff479f2017-10-26 16:53:10 -07001133 clamp(current_delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Fangwen Fu231fe422017-04-24 17:52:29 -07001134 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -07001135#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07001136 }
1137#endif
Arild Fuldseth07441162016-08-15 15:07:52 +02001138 }
Arild Fuldseth07441162016-08-15 15:07:52 +02001139
Yushin Choc24351c2017-10-24 14:59:08 -07001140 mbmi->current_q_index = xd->current_qindex;
1141
Yaowu Xuc27fc142016-08-22 16:08:15 -07001142 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001143 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001144
Alex Converse28744302017-04-13 14:46:22 -07001145#if CONFIG_INTRABC
Hui Su12546aa2017-10-13 16:10:01 -07001146 if (cm->allow_screen_content_tools) {
1147 xd->above_txfm_context =
1148 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
1149 xd->left_txfm_context = xd->left_txfm_context_buffer +
1150 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
1151 }
RogerZhouca865462017-10-05 15:06:27 -07001152 if (av1_allow_intrabc(bsize, cm)) {
Hui Suc2232cf2017-10-11 17:32:56 -07001153 read_intrabc_info(cm, xd, mi_row, mi_col, r);
1154 if (is_intrabc_block(mbmi)) return;
Alex Converse28744302017-04-13 14:46:22 -07001155 }
1156#endif // CONFIG_INTRABC
1157
Alex Conversef71808c2017-06-06 12:21:17 -07001158 mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
Sebastien Alaiwanfb838772017-10-24 12:02:54 +02001159#if CONFIG_INTRABC
Hui Su12546aa2017-10-13 16:10:01 -07001160 if (cm->allow_screen_content_tools)
1161 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Sebastien Alaiwanfb838772017-10-24 12:02:54 +02001162#endif // CONFIG_INTRABC
Alex Conversef71808c2017-06-06 12:21:17 -07001163
Jingning Han52261842016-12-14 12:17:49 -08001164 (void)i;
1165 mbmi->mode =
Thomas Davies1bfb5ed2017-01-11 15:28:11 +00001166 read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001167
Jingning Hand3a64432017-04-06 17:04:17 -07001168 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeau2c317902017-04-28 11:06:50 -04001169 xd->plane[1].subsampling_y)) {
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001170#if CONFIG_CFL
Luc Trudeau1e84af52017-11-25 15:00:28 -05001171 xd->cfl.is_chroma_reference = 1;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001172#endif // CONFIG_CFL
Hui Suaa2965e2017-10-05 15:59:12 -07001173 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001174
Luc Trudeauf5334002017-04-25 12:21:26 -04001175#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001176 if (mbmi->uv_mode == UV_CFL_PRED) {
David Michael Barrf6eaa152017-07-19 19:42:28 +09001177 mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeau1e84af52017-11-25 15:00:28 -05001178 xd->cfl.store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001179 } else {
Luc Trudeau1e84af52017-11-25 15:00:28 -05001180 xd->cfl.store_y = 0;
Luc Trudeauf5334002017-04-25 12:21:26 -04001181 }
Luc Trudeau2c317902017-04-28 11:06:50 -04001182#endif // CONFIG_CFL
1183
Joe Young830d4ce2017-05-30 17:48:13 -07001184 } else {
1185 // Avoid decoding angle_info if there is is no chroma prediction
Luc Trudeaud6d9eee2017-07-12 12:36:50 -04001186 mbmi->uv_mode = UV_DC_PRED;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001187#if CONFIG_CFL
Luc Trudeau1e84af52017-11-25 15:00:28 -05001188 xd->cfl.is_chroma_reference = 0;
1189 xd->cfl.store_y = 1;
Luc Trudeauc84c21c2017-07-25 19:40:34 -04001190#endif
Luc Trudeauf5334002017-04-25 12:21:26 -04001191 }
Luc Trudeauf5334002017-04-25 12:21:26 -04001192
Yaowu Xuc27fc142016-08-22 16:08:15 -07001193#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -07001194 read_intra_angle_info(xd, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195#endif // CONFIG_EXT_INTRA
1196 mbmi->palette_mode_info.palette_size[0] = 0;
1197 mbmi->palette_mode_info.palette_size[1] = 0;
Hui Sue87fb232017-10-05 15:00:15 -07001198 if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001199 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001200#if CONFIG_FILTER_INTRA
1201 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1202 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Yue Chen4eba69b2017-11-09 22:37:35 -08001203 read_filter_intra_mode_info(xd, r);
hui su5db97432016-10-14 16:10:14 -07001204#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001205
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001206#if !CONFIG_TXK_SEL
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02001207 av1_read_tx_type(cm, xd, r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07001208#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07001209}
1210
Alex Converse6b2584c2017-05-02 09:51:21 -07001211static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
RogerZhou3b635242017-09-19 10:06:46 -07001212#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001213 int use_subpel,
RogerZhou3b635242017-09-19 10:06:46 -07001214#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001215 int usehp) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216 int mag, d, fr, hp;
Debargha Mukherjee62fcfab2017-11-09 00:51:17 -08001217 const int sign = aom_read_symbol(r, mvcomp->sign_cdf, 2, ACCT_STR);
Michael Bebenita6048d052016-08-25 14:40:54 -07001218 const int mv_class =
Debargha Mukherjee62fcfab2017-11-09 00:51:17 -08001219 aom_read_symbol(r, mvcomp->classes_cdf, MV_CLASSES, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 const int class0 = mv_class == MV_CLASS_0;
1221
1222 // Integer part
1223 if (class0) {
Thomas Davies599395e2017-07-21 18:02:48 +01001224 d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001225 mag = 0;
1226 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001227 const int n = mv_class + CLASS0_BITS - 1; // number of bits
Yaowu Xuc27fc142016-08-22 16:08:15 -07001228 d = 0;
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001229 for (int i = 0; i < n; ++i)
Debargha Mukherjee62fcfab2017-11-09 00:51:17 -08001230 d |= aom_read_symbol(r, mvcomp->bits_cdf[i], 2, ACCT_STR) << i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001231 mag = CLASS0_SIZE << (mv_class + 2);
1232 }
1233
RogerZhou3b635242017-09-19 10:06:46 -07001234#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001235 if (use_subpel) {
RogerZhou3b635242017-09-19 10:06:46 -07001236#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001237 // Fractional part
1238 fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
1239 MV_FP_SIZE, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001240
Frederic Barbier749c21a2017-11-15 14:11:45 +01001241 // High precision part (if hp is not used, the default value of the hp is 1)
Thomas Davies599395e2017-07-21 18:02:48 +01001242 hp = usehp ? aom_read_symbol(
1243 r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
1244 ACCT_STR)
Alex Converse6b2584c2017-05-02 09:51:21 -07001245 : 1;
RogerZhou3b635242017-09-19 10:06:46 -07001246#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001247 } else {
1248 fr = 3;
1249 hp = 1;
1250 }
RogerZhou3b635242017-09-19 10:06:46 -07001251#endif // CONFIG_INTRABC || CONFIG_AMVR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001252
1253 // Result
1254 mag += ((d << 3) | (fr << 1) | hp) + 1;
1255 return sign ? -mag : mag;
1256}
1257
Yaowu Xuf883b422016-08-30 14:01:10 -07001258static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
Hui Su21b67222017-11-16 16:17:59 -08001259 nmv_context *ctx, MvSubpelPrecision precision) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001260 MV_JOINT_TYPE joint_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 MV diff = { 0, 0 };
Michael Bebenita6048d052016-08-25 14:40:54 -07001262 joint_type =
Debargha Mukherjee62fcfab2017-11-09 00:51:17 -08001263 (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joints_cdf, MV_JOINTS, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001264
1265 if (mv_joint_vertical(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001266 diff.row = read_mv_component(r, &ctx->comps[0],
RogerZhou3b635242017-09-19 10:06:46 -07001267#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001268 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001269#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001270 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001271
1272 if (mv_joint_horizontal(joint_type))
Alex Converse6b2584c2017-05-02 09:51:21 -07001273 diff.col = read_mv_component(r, &ctx->comps[1],
RogerZhou3b635242017-09-19 10:06:46 -07001274#if CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001275 precision > MV_SUBPEL_NONE,
RogerZhou3b635242017-09-19 10:06:46 -07001276#endif // CONFIG_INTRABC || CONFIG_AMVR
Alex Converse6b2584c2017-05-02 09:51:21 -07001277 precision > MV_SUBPEL_LOW_PRECISION);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001278
Yaowu Xuc27fc142016-08-22 16:08:15 -07001279 mv->row = ref->row + diff.row;
1280 mv->col = ref->col + diff.col;
1281}
1282
Yaowu Xuf883b422016-08-30 14:01:10 -07001283static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001284 const MACROBLOCKD *xd,
Yaowu Xuf883b422016-08-30 14:01:10 -07001285 aom_reader *r) {
Debargha Mukherjee0f248c42017-09-07 12:40:18 -07001286 if (!is_comp_ref_allowed(xd->mi[0]->mbmi.sb_type)) return SINGLE_REFERENCE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001287 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001288 const int ctx = av1_get_reference_mode_context(cm, xd);
Thomas Davies8c9bcdf2017-06-21 10:12:48 +01001289 const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
1290 r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001291 FRAME_COUNTS *counts = xd->counts;
1292 if (counts) ++counts->comp_inter[ctx][mode];
1293 return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
1294 } else {
1295 return cm->reference_mode;
1296 }
1297}
1298
Thomas Davies315f5782017-06-14 15:14:55 +01001299#define READ_REF_BIT(pname) \
Thomas Davies894cc812017-06-22 17:51:33 +01001300 aom_read_symbol(r, av1_get_pred_cdf_##pname(cm, xd), 2, ACCT_STR)
Thomas Davies0fbd2b72017-09-12 10:49:45 +01001301#define READ_REF_BIT2(pname) \
1302 aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
Thomas Davies315f5782017-06-14 15:14:55 +01001303
Zoe Liuc082bbc2017-05-17 13:31:37 -07001304#if CONFIG_EXT_COMP_REFS
Zoe Liuec50d6b2017-08-23 16:02:59 -07001305static COMP_REFERENCE_TYPE read_comp_reference_type(AV1_COMMON *cm,
1306 const MACROBLOCKD *xd,
1307 aom_reader *r) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001308 const int ctx = av1_get_comp_reference_type_context(xd);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001309 COMP_REFERENCE_TYPE comp_ref_type;
Sebastien Alaiwan4be6cb32017-11-02 18:04:11 +01001310 (void)cm;
1311 comp_ref_type = (COMP_REFERENCE_TYPE)aom_read_symbol(
1312 r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001313 FRAME_COUNTS *counts = xd->counts;
1314 if (counts) ++counts->comp_ref_type[ctx][comp_ref_type];
1315 return comp_ref_type; // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
1316}
1317#endif // CONFIG_EXT_COMP_REFS
1318
Zoe Liuf704a1c2017-10-02 16:55:59 -07001319#if CONFIG_EXT_SKIP
Zoe Liuf704a1c2017-10-02 16:55:59 -07001320static void set_ref_frames_for_skip_mode(AV1_COMMON *const cm,
Zoe Liuf40a9572017-10-13 12:37:19 -07001321 MV_REFERENCE_FRAME ref_frame[2]) {
Zoe Liuf704a1c2017-10-02 16:55:59 -07001322 ref_frame[0] = LAST_FRAME + cm->ref_frame_idx_0;
1323 ref_frame[1] = LAST_FRAME + cm->ref_frame_idx_1;
Zoe Liuf704a1c2017-10-02 16:55:59 -07001324}
1325#endif // CONFIG_EXT_SKIP
1326
Yaowu Xuc27fc142016-08-22 16:08:15 -07001327// Read the referncence frame
Yaowu Xuf883b422016-08-30 14:01:10 -07001328static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1329 aom_reader *r, int segment_id,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001330 MV_REFERENCE_FRAME ref_frame[2]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331 FRAME_COUNTS *counts = xd->counts;
1332
1333 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1334 ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
1335 SEG_LVL_REF_FRAME);
Emil Keyder01770b32017-01-20 18:03:11 -05001336 ref_frame[1] = NONE_FRAME;
David Barkerd92f3562017-10-09 17:46:23 +01001337 }
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001338#if CONFIG_SEGMENT_GLOBALMV
David Barkerd92f3562017-10-09 17:46:23 +01001339 else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001340 segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV))
David Barkerd92f3562017-10-09 17:46:23 +01001341#else
1342 else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP))
1343#endif
1344 {
1345 ref_frame[0] = LAST_FRAME;
1346 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001347 } else {
Zoe Liuf704a1c2017-10-02 16:55:59 -07001348#if CONFIG_EXT_SKIP
1349 if (xd->mi[0]->mbmi.skip_mode) {
Zoe Liuf40a9572017-10-13 12:37:19 -07001350 set_ref_frames_for_skip_mode(cm, ref_frame);
Zoe Liuf704a1c2017-10-02 16:55:59 -07001351 return;
1352 }
1353#endif // CONFIG_EXT_SKIP
1354
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355 const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
Zoe Liuf704a1c2017-10-02 16:55:59 -07001356
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357 // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
1358 if (mode == COMPOUND_REFERENCE) {
Zoe Liuc082bbc2017-05-17 13:31:37 -07001359#if CONFIG_EXT_COMP_REFS
1360 const COMP_REFERENCE_TYPE comp_ref_type =
1361 read_comp_reference_type(cm, xd, r);
1362
Zoe Liuc082bbc2017-05-17 13:31:37 -07001363 if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001364 const int ctx = av1_get_pred_context_uni_comp_ref_p(xd);
1365 int bit;
Sebastien Alaiwan4be6cb32017-11-02 18:04:11 +01001366 bit = READ_REF_BIT2(uni_comp_ref_p);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001367 if (counts) ++counts->uni_comp_ref[ctx][0][bit];
1368
1369 if (bit) {
1370 ref_frame[0] = BWDREF_FRAME;
1371 ref_frame[1] = ALTREF_FRAME;
1372 } else {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001373 const int ctx1 = av1_get_pred_context_uni_comp_ref_p1(xd);
1374 int bit1;
Sebastien Alaiwan4be6cb32017-11-02 18:04:11 +01001375 bit1 = READ_REF_BIT2(uni_comp_ref_p1);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001376 if (counts) ++counts->uni_comp_ref[ctx1][1][bit1];
1377
1378 if (bit1) {
Zoe Liufcf5fa22017-06-26 16:00:38 -07001379 const int ctx2 = av1_get_pred_context_uni_comp_ref_p2(xd);
1380 int bit2;
Sebastien Alaiwan4be6cb32017-11-02 18:04:11 +01001381 bit2 = READ_REF_BIT2(uni_comp_ref_p2);
Zoe Liufcf5fa22017-06-26 16:00:38 -07001382 if (counts) ++counts->uni_comp_ref[ctx2][2][bit2];
1383
1384 if (bit2) {
1385 ref_frame[0] = LAST_FRAME;
1386 ref_frame[1] = GOLDEN_FRAME;
1387 } else {
1388 ref_frame[0] = LAST_FRAME;
1389 ref_frame[1] = LAST3_FRAME;
1390 }
Zoe Liuc082bbc2017-05-17 13:31:37 -07001391 } else {
1392 ref_frame[0] = LAST_FRAME;
1393 ref_frame[1] = LAST2_FRAME;
1394 }
1395 }
1396
1397 return;
1398 }
Zoe Liufcf5fa22017-06-26 16:00:38 -07001399
1400 assert(comp_ref_type == BIDIR_COMP_REFERENCE);
Zoe Liuc082bbc2017-05-17 13:31:37 -07001401#endif // CONFIG_EXT_COMP_REFS
1402
Arild Fuldseth (arilfuld)38897302017-04-27 20:03:03 +02001403 const int idx = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001404
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001405 const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
Thomas Davies894cc812017-06-22 17:51:33 +01001406 const int bit = READ_REF_BIT(comp_ref_p);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001407 if (counts) ++counts->comp_ref[ctx][0][bit];
1408
Yaowu Xuc27fc142016-08-22 16:08:15 -07001409 // Decode forward references.
1410 if (!bit) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001411 const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Thomas Davies894cc812017-06-22 17:51:33 +01001412 const int bit1 = READ_REF_BIT(comp_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001413 if (counts) ++counts->comp_ref[ctx1][1][bit1];
Zoe Liu87818282017-11-26 17:09:59 -08001414 ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 1 : 0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001415 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Thomas Davies894cc812017-06-22 17:51:33 +01001417 const int bit2 = READ_REF_BIT(comp_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001418 if (counts) ++counts->comp_ref[ctx2][2][bit2];
1419 ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
1420 }
1421
1422 // Decode backward references.
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001423 const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
Thomas Davies894cc812017-06-22 17:51:33 +01001424 const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07001425 if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
Zoe Liu043c2272017-07-19 12:40:29 -07001426 if (!bit_bwd) {
1427 const int ctx1_bwd = av1_get_pred_context_comp_bwdref_p1(cm, xd);
Zoe Liu043c2272017-07-19 12:40:29 -07001428 const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
Zoe Liu043c2272017-07-19 12:40:29 -07001429 if (counts) ++counts->comp_bwdref[ctx1_bwd][1][bit1_bwd];
1430 ref_frame[idx] = cm->comp_bwd_ref[bit1_bwd];
1431 } else {
1432 ref_frame[idx] = cm->comp_bwd_ref[2];
1433 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001434 } else if (mode == SINGLE_REFERENCE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001435 const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001436 const int bit0 = READ_REF_BIT(single_ref_p1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001437 if (counts) ++counts->single_ref[ctx0][0][bit0];
1438
1439 if (bit0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001440 const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001441 const int bit1 = READ_REF_BIT(single_ref_p2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442 if (counts) ++counts->single_ref[ctx1][1][bit1];
Zoe Liu043c2272017-07-19 12:40:29 -07001443 if (!bit1) {
1444 const int ctx5 = av1_get_pred_context_single_ref_p6(xd);
Zoe Liu043c2272017-07-19 12:40:29 -07001445 const int bit5 = READ_REF_BIT(single_ref_p6);
Zoe Liu043c2272017-07-19 12:40:29 -07001446 if (counts) ++counts->single_ref[ctx5][5][bit5];
1447 ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1448 } else {
1449 ref_frame[0] = ALTREF_FRAME;
1450 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001451 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001452 const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001453 const int bit2 = READ_REF_BIT(single_ref_p3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454 if (counts) ++counts->single_ref[ctx2][2][bit2];
1455 if (bit2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001456 const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001457 const int bit4 = READ_REF_BIT(single_ref_p5);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458 if (counts) ++counts->single_ref[ctx4][4][bit4];
1459 ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1460 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001461 const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Thomas Davies315f5782017-06-14 15:14:55 +01001462 const int bit3 = READ_REF_BIT(single_ref_p4);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001463 if (counts) ++counts->single_ref[ctx3][3][bit3];
1464 ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1465 }
1466 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001467
Emil Keyder01770b32017-01-20 18:03:11 -05001468 ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469 } else {
1470 assert(0 && "Invalid prediction mode.");
1471 }
1472 }
1473}
1474
Angie Chiang9c4f8952016-11-21 11:13:19 -08001475static INLINE void read_mb_interp_filter(AV1_COMMON *const cm,
1476 MACROBLOCKD *const xd,
1477 MB_MODE_INFO *const mbmi,
1478 aom_reader *r) {
1479 FRAME_COUNTS *counts = xd->counts;
Thomas Davies77c7c402017-01-11 17:58:54 +00001480 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Davies77c7c402017-01-11 17:58:54 +00001481
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07001482 if (!av1_is_interp_needed(xd)) {
1483 set_default_interp_filters(mbmi, cm->interp_filter);
Yue Chen19e7aa82016-11-30 14:05:39 -08001484 return;
1485 }
Yue Chen19e7aa82016-11-30 14:05:39 -08001486
Yaowu Xuc27fc142016-08-22 16:08:15 -07001487 if (cm->interp_filter != SWITCHABLE) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001488 mbmi->interp_filters = av1_broadcast_interp_filter(cm->interp_filter);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001489 } else {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001490#if CONFIG_DUAL_FILTER
1491 InterpFilter ref0_filter[2] = { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR };
1492 for (int dir = 0; dir < 2; ++dir) {
Angie Chiang9c4f8952016-11-21 11:13:19 -08001493 if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
1494 (mbmi->ref_frame[1] > INTRA_FRAME &&
1495 has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001496 const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1497 ref0_filter[dir] =
Thomas Daviesec92c112017-09-25 11:03:58 +01001498 (InterpFilter)aom_read_symbol(r, ec_ctx->switchable_interp_cdf[ctx],
1499 SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001500 if (counts) ++counts->switchable_interp[ctx][ref0_filter[dir]];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001501 }
1502 }
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001503 // The index system works as: (0, 1) -> (vertical, horizontal) filter types
1504 mbmi->interp_filters =
1505 av1_make_interp_filters(ref0_filter[0], ref0_filter[1]);
Nathan E. Egge476c63c2017-05-18 18:35:16 -04001506#else // CONFIG_DUAL_FILTER
Angie Chiang9c4f8952016-11-21 11:13:19 -08001507 const int ctx = av1_get_pred_context_switchable_interp(xd);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001508 InterpFilter filter = (InterpFilter)aom_read_symbol(
Thomas Daviesec92c112017-09-25 11:03:58 +01001509 r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001510 mbmi->interp_filters = av1_broadcast_interp_filter(filter);
1511 if (counts) ++counts->switchable_interp[ctx][filter];
Angie Chiang9c4f8952016-11-21 11:13:19 -08001512#endif // CONFIG_DUAL_FILTER
Rupert Swarbrick27e90292017-09-28 17:46:50 +01001513 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001514}
1515
Jingning Han36fe3202017-02-20 22:31:49 -08001516static void read_intra_block_mode_info(AV1_COMMON *const cm, const int mi_row,
1517 const int mi_col, MACROBLOCKD *const xd,
1518 MODE_INFO *mi, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001519 MB_MODE_INFO *const mbmi = &mi->mbmi;
1520 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
1521 int i;
1522
1523 mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder01770b32017-01-20 18:03:11 -05001524 mbmi->ref_frame[1] = NONE_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001525
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001526 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Nathan E. Eggea1f80e32017-05-23 11:52:32 -04001527
Jingning Han52261842016-12-14 12:17:49 -08001528 (void)i;
Hui Suaa2965e2017-10-05 15:59:12 -07001529 mbmi->mode = read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001530
Jingning Hand3a64432017-04-06 17:04:17 -07001531 if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001532 xd->plane[1].subsampling_y)) {
Hui Suaa2965e2017-10-05 15:59:12 -07001533 mbmi->uv_mode = read_intra_mode_uv(ec_ctx, r, mbmi->mode);
Jingning Han36fe3202017-02-20 22:31:49 -08001534
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001535#if CONFIG_CFL
Luc Trudeau6e1cd782017-06-21 13:52:36 -04001536 if (mbmi->uv_mode == UV_CFL_PRED) {
Nathan E. Egge6bdc40f2017-06-18 19:02:23 -04001537 mbmi->cfl_alpha_idx =
David Michael Barrf6eaa152017-07-19 19:42:28 +09001538 read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
Luc Trudeau1e84af52017-11-25 15:00:28 -05001539 xd->cfl.store_y = 1;
Luc Trudeaue784b3f2017-08-14 15:25:28 -04001540 } else {
Luc Trudeau1e84af52017-11-25 15:00:28 -05001541 xd->cfl.store_y = 0;
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001542 }
1543#endif // CONFIG_CFL
1544
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001545 } else {
1546 // Avoid decoding angle_info if there is is no chroma prediction
1547 mbmi->uv_mode = UV_DC_PRED;
1548#if CONFIG_CFL
Luc Trudeau1e84af52017-11-25 15:00:28 -05001549 xd->cfl.is_chroma_reference = 0;
1550 xd->cfl.store_y = 1;
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001551#endif
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001552 }
Luc Trudeaub09b55d2017-04-26 10:06:35 -04001553
Rupert Swarbrick766c9d52017-07-31 11:30:53 +01001554 // Explicitly ignore cm here to avoid a compile warning if none of
1555 // ext-intra, palette and filter-intra are enabled.
1556 (void)cm;
1557
Yaowu Xuc27fc142016-08-22 16:08:15 -07001558#if CONFIG_EXT_INTRA
Hui Su259d4422017-10-13 10:08:17 -07001559 read_intra_angle_info(xd, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001560#endif // CONFIG_EXT_INTRA
1561 mbmi->palette_mode_info.palette_size[0] = 0;
1562 mbmi->palette_mode_info.palette_size[1] = 0;
Hui Sue87fb232017-10-05 15:00:15 -07001563 if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
Yaowu Xuc27fc142016-08-22 16:08:15 -07001564 read_palette_mode_info(cm, xd, r);
hui su5db97432016-10-14 16:10:14 -07001565#if CONFIG_FILTER_INTRA
1566 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
1567 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
Yue Chen4eba69b2017-11-09 22:37:35 -08001568 read_filter_intra_mode_info(xd, r);
hui su5db97432016-10-14 16:10:14 -07001569#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07001570}
1571
1572static INLINE int is_mv_valid(const MV *mv) {
1573 return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1574 mv->col < MV_UPP;
1575}
1576
Yaowu Xuf883b422016-08-30 14:01:10 -07001577static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001578 PREDICTION_MODE mode,
Jingning Han5c60cdf2016-09-30 09:37:46 -07001579 MV_REFERENCE_FRAME ref_frame[2], int block,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001580 int_mv mv[2], int_mv ref_mv[2],
David Barker45390c12017-02-20 14:44:40 +00001581 int_mv nearest_mv[2], int_mv near_mv[2], int mi_row,
1582 int mi_col, int is_compound, int allow_hp,
1583 aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001584 int ret = 1;
Thomas Davies24523292017-01-11 16:56:47 +00001585 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001586 BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001587 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han5cfa6712016-12-14 09:53:38 -08001588 int_mv *pred_mv = mbmi->pred_mv;
1589 (void)block;
Sarah Parkere5299862016-08-16 14:57:37 -07001590 (void)ref_frame;
Thomas Davies24523292017-01-11 16:56:47 +00001591 (void)cm;
David Barker45390c12017-02-20 14:44:40 +00001592 (void)mi_row;
1593 (void)mi_col;
Debargha Mukherjeef6dd3c62017-02-23 13:21:23 -08001594 (void)bsize;
RogerZhou3b635242017-09-19 10:06:46 -07001595#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07001596 if (cm->cur_frame_force_integer_mv) {
RogerZhou3b635242017-09-19 10:06:46 -07001597 allow_hp = MV_SUBPEL_NONE;
1598 }
1599#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001600 switch (mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001601 case NEWMV: {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001602 for (int i = 0; i < 1 + is_compound; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001603 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1604 int nmv_ctx =
1605 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1606 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001607 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Hui Su21b67222017-11-16 16:17:59 -08001608 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001609 ret = ret && is_mv_valid(&mv[i].as_mv);
1610
Yaowu Xuc27fc142016-08-22 16:08:15 -07001611 pred_mv[i].as_int = ref_mv[i].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001612 }
1613 break;
1614 }
1615 case NEARESTMV: {
1616 mv[0].as_int = nearest_mv[0].as_int;
1617 if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
1618
Yaowu Xuc27fc142016-08-22 16:08:15 -07001619 pred_mv[0].as_int = nearest_mv[0].as_int;
1620 if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001621 break;
1622 }
1623 case NEARMV: {
1624 mv[0].as_int = near_mv[0].as_int;
1625 if (is_compound) mv[1].as_int = near_mv[1].as_int;
1626
Yaowu Xuc27fc142016-08-22 16:08:15 -07001627 pred_mv[0].as_int = near_mv[0].as_int;
1628 if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001629 break;
1630 }
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001631 case GLOBALMV: {
David Barkercdcac6d2016-12-01 17:04:16 +00001632 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001633 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001634 mi_col, mi_row, block
1635#if CONFIG_AMVR
1636 ,
RogerZhou10a03802017-10-26 11:49:48 -07001637 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07001638#endif
1639 )
David Barkercdcac6d2016-12-01 17:04:16 +00001640 .as_int;
Sarah Parkere5299862016-08-16 14:57:37 -07001641 if (is_compound)
David Barkercdcac6d2016-12-01 17:04:16 +00001642 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001643 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001644 mi_col, mi_row, block
1645#if CONFIG_AMVR
1646 ,
RogerZhou10a03802017-10-26 11:49:48 -07001647 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07001648#endif
1649 )
David Barkercdcac6d2016-12-01 17:04:16 +00001650 .as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001651
Debargha Mukherjeefebb59c2017-03-02 12:23:45 -08001652 pred_mv[0].as_int = mv[0].as_int;
1653 if (is_compound) pred_mv[1].as_int = mv[1].as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 break;
1655 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001656 case NEW_NEWMV: {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001657 assert(is_compound);
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001658 for (int i = 0; i < 2; ++i) {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001659 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1660 int nmv_ctx =
1661 av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
1662 mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001663 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Hui Su21b67222017-11-16 16:17:59 -08001664 read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001665 ret = ret && is_mv_valid(&mv[i].as_mv);
1666 }
1667 break;
1668 }
1669 case NEAREST_NEARESTMV: {
1670 assert(is_compound);
1671 mv[0].as_int = nearest_mv[0].as_int;
1672 mv[1].as_int = nearest_mv[1].as_int;
1673 break;
1674 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001675 case NEAR_NEARMV: {
1676 assert(is_compound);
1677 mv[0].as_int = near_mv[0].as_int;
1678 mv[1].as_int = near_mv[1].as_int;
1679 break;
1680 }
1681 case NEW_NEARESTMV: {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001682 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1683 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1684 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001685 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Hui Su21b67222017-11-16 16:17:59 -08001686 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001687 assert(is_compound);
1688 ret = ret && is_mv_valid(&mv[0].as_mv);
1689 mv[1].as_int = nearest_mv[1].as_int;
1690 break;
1691 }
1692 case NEAREST_NEWMV: {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001693 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1694 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1695 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001696 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07001697 mv[0].as_int = nearest_mv[0].as_int;
Hui Su21b67222017-11-16 16:17:59 -08001698 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001699 assert(is_compound);
1700 ret = ret && is_mv_valid(&mv[1].as_mv);
1701 break;
1702 }
1703 case NEAR_NEWMV: {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001704 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1705 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1706 xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001707 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Alex Converse3d0bdc12017-05-01 15:19:58 -07001708 mv[0].as_int = near_mv[0].as_int;
Hui Su21b67222017-11-16 16:17:59 -08001709 read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001710 assert(is_compound);
1711
1712 ret = ret && is_mv_valid(&mv[1].as_mv);
1713 break;
1714 }
1715 case NEW_NEARMV: {
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001716 int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
1717 int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
1718 xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
Alex Converse3d0bdc12017-05-01 15:19:58 -07001719 nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
Hui Su21b67222017-11-16 16:17:59 -08001720 read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001721 assert(is_compound);
1722 ret = ret && is_mv_valid(&mv[0].as_mv);
1723 mv[1].as_int = near_mv[1].as_int;
1724 break;
1725 }
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001726 case GLOBAL_GLOBALMV: {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001727 assert(is_compound);
Sarah Parkerc2d38712017-01-24 15:15:41 -08001728 mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001729 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001730 mi_col, mi_row, block
1731#if CONFIG_AMVR
1732 ,
RogerZhou10a03802017-10-26 11:49:48 -07001733 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07001734#endif
1735 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08001736 .as_int;
1737 mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
Sarah Parkerae7c4582017-02-28 16:30:30 -08001738 cm->allow_high_precision_mv, bsize,
RogerZhou3b635242017-09-19 10:06:46 -07001739 mi_col, mi_row, block
1740#if CONFIG_AMVR
1741 ,
RogerZhou10a03802017-10-26 11:49:48 -07001742 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07001743#endif
1744 )
Sarah Parkerc2d38712017-01-24 15:15:41 -08001745 .as_int;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001746 break;
1747 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001748 default: { return 0; }
1749 }
1750 return ret;
1751}
1752
Yaowu Xuf883b422016-08-30 14:01:10 -07001753static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1754 int segment_id, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755 if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1756 return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
1757 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001758 const int ctx = av1_get_intra_inter_context(xd);
Thomas Daviesf6ad9352017-04-19 11:38:06 +01001759 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Thomas Daviesf6ad9352017-04-19 11:38:06 +01001760 const int is_inter =
1761 aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001762 FRAME_COUNTS *counts = xd->counts;
1763 if (counts) ++counts->intra_inter[ctx][is_inter];
1764 return is_inter;
1765 }
1766}
1767
1768static void fpm_sync(void *const data, int mi_row) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001769 AV1Decoder *const pbi = (AV1Decoder *)data;
1770 av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
1771 mi_row << pbi->common.mib_size_log2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001772}
1773
Di Chen56586622017-06-09 13:49:44 -07001774#if DEC_MISMATCH_DEBUG
Zoe Liu0b3ef732017-10-06 18:37:24 -07001775static void dec_dump_logs(AV1_COMMON *cm, MODE_INFO *const mi, int mi_row,
Zoe Liuf704a1c2017-10-02 16:55:59 -07001776 int mi_col, int16_t mode_ctx) {
Di Chen56586622017-06-09 13:49:44 -07001777 int_mv mv[2] = { { 0 } };
1778 int ref;
1779 MB_MODE_INFO *const mbmi = &mi->mbmi;
Di Chen56586622017-06-09 13:49:44 -07001780 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
1781 mv[ref].as_mv = mbmi->mv[ref].as_mv;
1782
Di Chen56586622017-06-09 13:49:44 -07001783 const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
1784 int16_t zeromv_ctx = -1;
1785 int16_t refmv_ctx = -1;
1786 if (mbmi->mode != NEWMV) {
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001787 if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) assert(mbmi->mode == GLOBALMV);
1788 zeromv_ctx = (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1789 if (mbmi->mode != GLOBALMV) {
Di Chen56586622017-06-09 13:49:44 -07001790 refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
1791 if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
1792 if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
1793 if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
1794 }
1795 }
1796
Zoe Liuf40a9572017-10-13 12:37:19 -07001797#define FRAME_TO_CHECK 11
1798#if CONFIG_EXT_SKIP
Zoe Liu17af2742017-10-06 10:36:42 -07001799 if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) {
Zoe Liuf9333f52017-07-03 10:52:01 -07001800 printf(
1801 "=== DECODER ===: "
Zoe Liuf704a1c2017-10-02 16:55:59 -07001802 "Frame=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d, mode=%d, bsize=%d, "
Zoe Liuf9333f52017-07-03 10:52:01 -07001803 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
Zoe Liuf704a1c2017-10-02 16:55:59 -07001804 "ref[1]=%d, motion_mode=%d, mode_ctx=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07001805 "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d, tx_size=%d\n",
Zoe Liuf704a1c2017-10-02 16:55:59 -07001806 cm->current_video_frame, mi_row, mi_col, mbmi->skip_mode, mbmi->mode,
1807 mbmi->sb_type, cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col,
1808 mv[1].as_mv.row, mv[1].as_mv.col, mbmi->ref_frame[0],
1809 mbmi->ref_frame[1], mbmi->motion_mode, mode_ctx, newmv_ctx, zeromv_ctx,
Zoe Liuf40a9572017-10-13 12:37:19 -07001810 refmv_ctx, mbmi->tx_size);
1811#else
1812 if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) {
1813 printf(
1814 "=== DECODER ===: "
1815 "Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
1816 "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
1817 "ref[1]=%d, motion_mode=%d, mode_ctx=%d, "
1818 "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d, tx_size=%d\n",
1819 cm->current_video_frame, mi_row, mi_col, mbmi->mode, mbmi->sb_type,
1820 cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
1821 mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
1822 mbmi->motion_mode, mode_ctx, newmv_ctx, zeromv_ctx, refmv_ctx,
1823 mbmi->tx_size);
1824#endif // CONFIG_EXT_SKIP
Zoe Liuf9333f52017-07-03 10:52:01 -07001825 }
Di Chen56586622017-06-09 13:49:44 -07001826}
1827#endif // DEC_MISMATCH_DEBUG
1828
Yaowu Xuf883b422016-08-30 14:01:10 -07001829static void read_inter_block_mode_info(AV1Decoder *const pbi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001830 MACROBLOCKD *const xd,
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02001831 MODE_INFO *const mi, int mi_row,
1832 int mi_col, aom_reader *r) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001833 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001834 MB_MODE_INFO *const mbmi = &mi->mbmi;
1835 const BLOCK_SIZE bsize = mbmi->sb_type;
1836 const int allow_hp = cm->allow_high_precision_mv;
1837 int_mv nearestmv[2], nearmv[2];
1838 int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001839 int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001840 int16_t compound_inter_mode_ctx[MODE_CTX_REF_FRAMES];
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001841 int mode_ctx = 0;
Debargha Mukherjeee6eb3b52017-02-26 08:50:56 -08001842 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
Yunqing Wang97d6a372017-10-09 14:15:15 -07001843#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang876a8b02017-11-13 17:13:27 -08001844 int pts_mv[SAMPLES_ARRAY_SIZE], pts_wm[SAMPLES_ARRAY_SIZE];
Yunqing Wang97d6a372017-10-09 14:15:15 -07001845#endif // CONFIG_EXT_WARPED_MOTION
Thomas Davies1de6c882017-01-11 17:47:49 +00001846 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001847
Urvang Joshi5a9ea002017-05-22 15:25:18 -07001848 assert(NELEMENTS(mode_2_counter) == MB_MODE_COUNT);
1849
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001850 mbmi->uv_mode = UV_DC_PRED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001851 mbmi->palette_mode_info.palette_size[0] = 0;
1852 mbmi->palette_mode_info.palette_size[1] = 0;
1853
Frederic Barbier7a84fd82017-03-02 18:08:15 +01001854 memset(ref_mvs, 0, sizeof(ref_mvs));
1855
Yaowu Xuc27fc142016-08-22 16:08:15 -07001856 read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001857 const int is_compound = has_second_ref(mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001858
Zoe Liuf40a9572017-10-13 12:37:19 -07001859#if CONFIG_EXT_SKIP
1860// TODO(zoeliu): To work with JNT_COMP
1861#endif // CONFIG_EXT_SKIP
1862
Cheng Chen0a7f2f52017-10-10 15:16:09 -07001863#if CONFIG_JNT_COMP
Cheng Chenb09e55c2017-11-10 12:09:19 -08001864 if (is_compound) {
Cheng Chen0a7f2f52017-10-10 15:16:09 -07001865 const int comp_index_ctx = get_comp_index_context(cm, xd);
Cheng Chen46970612017-10-24 14:53:36 -07001866 mbmi->compound_idx = aom_read_symbol(
1867 r, ec_ctx->compound_index_cdf[comp_index_ctx], 2, ACCT_STR);
Cheng Chen0a7f2f52017-10-10 15:16:09 -07001868 if (xd->counts)
1869 ++xd->counts->compound_index[comp_index_ctx][mbmi->compound_idx];
1870 }
1871#endif // CONFIG_JNT_COMP
1872
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001873 for (int ref = 0; ref < 1 + is_compound; ++ref) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001874 MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001875
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02001876 av1_find_mv_refs(cm, xd, mi, frame, &xd->ref_mv_count[frame],
1877 xd->ref_mv_stack[frame], compound_inter_mode_ctx,
1878 ref_mvs[frame], mi_row, mi_col, fpm_sync, (void *)pbi,
1879 inter_mode_ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001880 }
1881
Jingning Hanacddc032016-11-17 15:26:20 -08001882 if (is_compound) {
1883 MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001884 av1_find_mv_refs(cm, xd, mi, ref_frame, &xd->ref_mv_count[ref_frame],
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02001885 xd->ref_mv_stack[ref_frame], compound_inter_mode_ctx,
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001886 ref_mvs[ref_frame], mi_row, mi_col, fpm_sync, (void *)pbi,
1887 inter_mode_ctx);
1888
1889 if (xd->ref_mv_count[ref_frame] < 2) {
1890 MV_REFERENCE_FRAME rf[2];
David Barkercdcac6d2016-12-01 17:04:16 +00001891 int_mv zeromv[2];
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001892 av1_set_ref_frame(rf, ref_frame);
David Barkercdcac6d2016-12-01 17:04:16 +00001893 zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
David Barker45390c12017-02-20 14:44:40 +00001894 cm->allow_high_precision_mv,
RogerZhou3b635242017-09-19 10:06:46 -07001895 bsize, mi_col, mi_row, 0
1896#if CONFIG_AMVR
1897 ,
RogerZhou10a03802017-10-26 11:49:48 -07001898 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07001899#endif
1900 )
David Barkercdcac6d2016-12-01 17:04:16 +00001901 .as_int;
RogerZhou3b635242017-09-19 10:06:46 -07001902 zeromv[1].as_int =
1903 (rf[1] != NONE_FRAME)
1904 ? gm_get_motion_vector(&cm->global_motion[rf[1]],
1905 cm->allow_high_precision_mv, bsize, mi_col,
1906 mi_row, 0
1907#if CONFIG_AMVR
1908 ,
RogerZhou10a03802017-10-26 11:49:48 -07001909 cm->cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -07001910#endif
1911 )
1912 .as_int
1913 : 0;
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001914 for (int ref = 0; ref < 2; ++ref) {
Sarah Parker9923d1b2017-04-10 11:56:40 -07001915 if (rf[ref] == NONE_FRAME) continue;
RogerZhou3b635242017-09-19 10:06:46 -07001916#if CONFIG_AMVR
1917 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07001918 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07001919 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07001920 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07001921#else
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001922 lower_mv_precision(&ref_mvs[rf[ref]][0].as_mv, allow_hp);
1923 lower_mv_precision(&ref_mvs[rf[ref]][1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07001924#endif
Sarah Parker9923d1b2017-04-10 11:56:40 -07001925 if (ref_mvs[rf[ref]][0].as_int != zeromv[ref].as_int ||
1926 ref_mvs[rf[ref]][1].as_int != zeromv[ref].as_int)
1927 inter_mode_ctx[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
Frederic Barbier72e2e982017-03-03 10:01:04 +01001928 }
Yaowu Xu4306b6e2016-09-27 12:55:32 -07001929 }
1930 }
1931
Yaowu Xuc27fc142016-08-22 16:08:15 -07001932 if (is_compound)
1933 mode_ctx = compound_inter_mode_ctx[mbmi->ref_frame[0]];
1934 else
Yaowu Xuc27fc142016-08-22 16:08:15 -07001935 mode_ctx =
Yaowu Xuf883b422016-08-30 14:01:10 -07001936 av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame, bsize, -1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001937 mbmi->ref_mv_idx = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001938
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001939#if CONFIG_SEGMENT_GLOBALMV
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04001940 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001941 segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_GLOBALMV))
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04001942#else
David Barkerd92f3562017-10-09 17:46:23 +01001943 if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
Soo-Chul Hana752d1d2017-09-08 11:21:25 -04001944#endif
David Barkerd92f3562017-10-09 17:46:23 +01001945 {
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001946 mbmi->mode = GLOBALMV;
Zoe Liuf40a9572017-10-13 12:37:19 -07001947#if CONFIG_EXT_SKIP
1948 } else if (mbmi->skip_mode) {
1949 assert(is_compound);
1950 mbmi->mode = NEAREST_NEARESTMV;
1951#endif // CONFIG_EXT_SKIP
Yaowu Xuc27fc142016-08-22 16:08:15 -07001952 } else {
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01001953 if (is_compound)
1954 mbmi->mode = read_inter_compound_mode(cm, xd, r, mode_ctx);
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001955 else
1956 mbmi->mode = read_inter_mode(ec_ctx, xd, r, mode_ctx);
1957 if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001958 have_nearmv_in_inter_mode(mbmi->mode))
1959 read_drl_idx(ec_ctx, xd, mbmi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001960 }
1961
Hui Su70f9a1f2017-11-20 13:46:00 -08001962 if (is_compound != is_inter_compound_mode(mbmi->mode)) {
1963 aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
1964 "Prediction mode %d invalid with ref frame %d %d",
1965 mbmi->mode, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1966 }
1967
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001968 if (mbmi->mode != GLOBALMV && mbmi->mode != GLOBAL_GLOBALMV) {
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01001969 for (int ref = 0; ref < 1 + is_compound; ++ref) {
RogerZhou3b635242017-09-19 10:06:46 -07001970#if CONFIG_AMVR
1971 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
1972 &nearestmv[ref], &nearmv[ref],
RogerZhou10a03802017-10-26 11:49:48 -07001973 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07001974#else
Yaowu Xuf883b422016-08-30 14:01:10 -07001975 av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
1976 &nearestmv[ref], &nearmv[ref]);
RogerZhou3b635242017-09-19 10:06:46 -07001977#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001978 }
1979 }
1980
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01001981 if (is_compound && mbmi->mode != GLOBAL_GLOBALMV) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001982 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02001984 if (xd->ref_mv_count[ref_frame_type] > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001985 if (mbmi->mode == NEAREST_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001986 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
1987 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07001988#if CONFIG_AMVR
1989 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07001990 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07001991 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07001992 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07001993#else
Sebastien Alaiwan0bdea0d2017-10-02 15:15:05 +02001994 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
1995 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07001996#endif
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01001997 } else if (mbmi->mode == NEAREST_NEWMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998 nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07001999
2000#if CONFIG_AMVR
2001 lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07002002 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002003#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 lower_mv_precision(&nearestmv[0].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002005#endif
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -07002006 } else if (mbmi->mode == NEW_NEARESTMV) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002007 nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002008#if CONFIG_AMVR
2009 lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
RogerZhou10a03802017-10-26 11:49:48 -07002010 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002011#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002012 lower_mv_precision(&nearestmv[1].as_mv, allow_hp);
RogerZhou3b635242017-09-19 10:06:46 -07002013#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002014 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002015 }
2016
Yaowu Xuc27fc142016-08-22 16:08:15 -07002017 if (xd->ref_mv_count[ref_frame_type] > 1) {
David Barker404b2e82017-03-27 13:07:47 +01002018 int ref_mv_idx = 1 + mbmi->ref_mv_idx;
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01002019 if (compound_ref0_mode(mbmi->mode) == NEARMV) {
2020 nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002021#if CONFIG_AMVR
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01002022 lower_mv_precision(&nearmv[0].as_mv, allow_hp,
2023 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002024#else
2025 lower_mv_precision(&nearmv[0].as_mv, allow_hp);
2026#endif
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01002027 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002028
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01002029 if (compound_ref1_mode(mbmi->mode) == NEARMV) {
2030 nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
RogerZhou3b635242017-09-19 10:06:46 -07002031#if CONFIG_AMVR
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01002032 lower_mv_precision(&nearmv[1].as_mv, allow_hp,
2033 cm->cur_frame_force_integer_mv);
RogerZhou3b635242017-09-19 10:06:46 -07002034#else
2035 lower_mv_precision(&nearmv[1].as_mv, allow_hp);
2036#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002037 }
2038 }
David Barker0d7c4b02017-07-25 14:55:48 +01002039 } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
2040 int_mv cur_mv =
2041 xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
2042 nearmv[0] = cur_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002043 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002044
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002045 int_mv ref_mv[2];
2046 ref_mv[0] = nearestmv[0];
2047 ref_mv[1] = nearestmv[1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002048
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002049 if (is_compound) {
2050 int ref_mv_idx = mbmi->ref_mv_idx;
2051 // Special case: NEAR_NEWMV and NEW_NEARMV modes use
2052 // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
2053 // mbmi->ref_mv_idx (like NEWMV)
2054 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
2055 ref_mv_idx = 1 + mbmi->ref_mv_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002056
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002057 if (compound_ref0_mode(mbmi->mode) == NEWMV) {
2058 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2059 if (xd->ref_mv_count[ref_frame_type] > 1) {
2060 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
2061 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2062 xd->n8_h << MI_SIZE_LOG2, xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002063 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002064 nearestmv[0] = ref_mv[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002065 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002066 if (compound_ref1_mode(mbmi->mode) == NEWMV) {
2067 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2068 if (xd->ref_mv_count[ref_frame_type] > 1) {
2069 ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
2070 clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
2071 xd->n8_h << MI_SIZE_LOG2, xd);
David Barker404b2e82017-03-27 13:07:47 +01002072 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002073 nearestmv[1] = ref_mv[1];
2074 }
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002075 } else {
2076 if (mbmi->mode == NEWMV) {
Zoe Liu2854fe72017-11-16 21:11:35 -08002077 uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
2078 if (xd->ref_mv_count[ref_frame_type] > 1) {
2079 ref_mv[0] = xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv;
2080 clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
2081 xd->n8_h << MI_SIZE_LOG2, xd);
David Barker3dfba992017-04-03 16:10:09 +01002082 }
Zoe Liu2854fe72017-11-16 21:11:35 -08002083 nearestmv[0] = ref_mv[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002084 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002085 }
2086
Zoe Liuf40a9572017-10-13 12:37:19 -07002087#if CONFIG_EXT_SKIP
2088 if (mbmi->skip_mode) {
2089#define USE_MV_SELECTION 0
2090#if USE_MV_SELECTION
2091 // NOTE: For skip mode, above reference selection has been set as compound,
2092 // in order to obtain the two nearest mvs, but after the following mv setup,
2093 // skip mode may choose either single reference or compound reference mode.
2094 av1_setup_skip_mode_mvs(cm, xd, mbmi, mi_row, mi_col, nearestmv, NULL,
2095 NULL);
2096#else // !USE_MV_SELECTION
2097 assert(mbmi->mode == NEAREST_NEARESTMV);
2098 mbmi->mv[0].as_int = nearestmv[0].as_int;
2099 mbmi->mv[1].as_int = nearestmv[1].as_int;
2100#endif // USE_MV_SELECTION
2101#undef USE_MV_SELECTION
2102 } else {
2103#endif // CONFIG_EXT_SKIP
2104 int mv_corrupted_flag =
2105 !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, 0, mbmi->mv, ref_mv,
2106 nearestmv, nearmv, mi_row, mi_col, is_compound, allow_hp, r);
2107 aom_merge_corrupted_flag(&xd->corrupted, mv_corrupted_flag);
2108#if CONFIG_EXT_SKIP
2109 }
2110#endif // CONFIG_EXT_SKIP
Debargha Mukherjeeedced252017-10-20 00:02:00 -07002111
Yaowu Xuc27fc142016-08-22 16:08:15 -07002112 mbmi->use_wedge_interintra = 0;
2113 if (cm->reference_mode != COMPOUND_REFERENCE &&
Zoe Liuf40a9572017-10-13 12:37:19 -07002114#if CONFIG_EXT_SKIP
2115 !mbmi->skip_mode &&
2116#endif // CONFIG_EXT_SKIP
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002117 cm->allow_interintra_compound && is_interintra_allowed(mbmi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002118 const int bsize_group = size_group_lookup[bsize];
Thomas Daviescff91712017-07-07 11:49:55 +01002119 const int interintra =
2120 aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002121 if (xd->counts) xd->counts->interintra[bsize_group][interintra]++;
Emil Keyder01770b32017-01-20 18:03:11 -05002122 assert(mbmi->ref_frame[1] == NONE_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002123 if (interintra) {
2124 const INTERINTRA_MODE interintra_mode =
Yue Chen670b6602017-10-30 16:57:42 -07002125 read_interintra_mode(xd, r, bsize_group);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002126 mbmi->ref_frame[1] = INTRA_FRAME;
2127 mbmi->interintra_mode = interintra_mode;
2128#if CONFIG_EXT_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002129 mbmi->angle_delta[0] = 0;
2130 mbmi->angle_delta[1] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002131#endif // CONFIG_EXT_INTRA
hui su5db97432016-10-14 16:10:14 -07002132#if CONFIG_FILTER_INTRA
2133 mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
2134 mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
2135#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -07002136 if (is_interintra_wedge_used(bsize)) {
Thomas Daviescff91712017-07-07 11:49:55 +01002137 mbmi->use_wedge_interintra = aom_read_symbol(
2138 r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002139 if (xd->counts)
2140 xd->counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
2141 if (mbmi->use_wedge_interintra) {
2142 mbmi->interintra_wedge_index =
Michael Bebenita6048d052016-08-25 14:40:54 -07002143 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002144 mbmi->interintra_wedge_sign = 0;
2145 }
2146 }
2147 }
2148 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002149
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01002150 for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
Yue Chen52c51732017-07-11 15:08:30 -07002151 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
2152 RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
2153
2154 xd->block_refs[ref] = ref_buf;
2155 }
Yue Chen52c51732017-07-11 15:08:30 -07002156
Yunqing Wang3afbf3f2017-11-21 20:16:18 -08002157#if CONFIG_EXT_WARPED_MOTION
2158 int best_cand = -1;
2159#endif // CONFIG_EXT_WARPED_MOTION
Yue Chencb60b182016-10-13 15:18:22 -07002160 mbmi->motion_mode = SIMPLE_TRANSLATION;
Zoe Liuf40a9572017-10-13 12:37:19 -07002161 if (mbmi->sb_type >= BLOCK_8X8 &&
2162#if CONFIG_EXT_SKIP
2163 !mbmi->skip_mode &&
2164#endif // CONFIG_EXT_SKIP
2165 !has_second_ref(mbmi))
Yunqing Wang97d6a372017-10-09 14:15:15 -07002166#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang3afbf3f2017-11-21 20:16:18 -08002167 {
Yunqing Wang1bc82862017-06-28 15:49:48 -07002168 mbmi->num_proj_ref[0] =
Yunqing Wang876a8b02017-11-13 17:13:27 -08002169 findSamples(cm, xd, mi_row, mi_col, pts, pts_inref, pts_mv, pts_wm);
Yunqing Wang3afbf3f2017-11-21 20:16:18 -08002170
2171 // Find a warped neighbor.
2172 int cand;
2173 int best_weight = 0;
2174
2175 // if (mbmi->mode == NEARESTMV)
2176 for (cand = 0; cand < mbmi->num_proj_ref[0]; cand++) {
2177 if (pts_wm[cand * 2 + 1] > best_weight) {
2178 best_weight = pts_wm[cand * 2 + 1];
2179 best_cand = cand;
2180 }
2181 }
2182 }
Yunqing Wang1bc82862017-06-28 15:49:48 -07002183#else
Yue Chen69f18e12016-09-08 14:48:15 -07002184 mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
Yunqing Wang97d6a372017-10-09 14:15:15 -07002185#endif // CONFIG_EXT_WARPED_MOTION
Yue Chen5329a2b2017-02-28 17:33:00 +08002186 av1_count_overlappable_neighbors(cm, xd, mi_row, mi_col);
Yue Chen69f18e12016-09-08 14:48:15 -07002187
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002188 if (mbmi->ref_frame[1] != INTRA_FRAME)
Yunqing Wang3afbf3f2017-11-21 20:16:18 -08002189#if CONFIG_EXT_WARPED_MOTION
2190 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r, best_cand);
2191#else
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002192 mbmi->motion_mode = read_motion_mode(cm, xd, mi, r);
Yunqing Wang3afbf3f2017-11-21 20:16:18 -08002193#endif // CONFIG_EXT_WARPED_MOTION
Wei-Ting Lin85a8f702017-06-22 13:55:15 -07002194
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002195 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sebastien Alaiwan34d55662017-11-15 09:36:03 +01002196 if (cm->reference_mode != SINGLE_REFERENCE &&
2197 is_inter_compound_mode(mbmi->mode) &&
2198 mbmi->motion_mode == SIMPLE_TRANSLATION
Zoe Liuf704a1c2017-10-02 16:55:59 -07002199#if CONFIG_EXT_SKIP
2200 && !mbmi->skip_mode
2201#endif // CONFIG_EXT_SKIP
2202 ) {
Sarah Parker42d96102017-01-31 21:05:27 -08002203 if (is_any_masked_compound_used(bsize)) {
Cheng Chenbdd6ca82017-10-23 22:34:25 -07002204#if CONFIG_JNT_COMP
2205 if (cm->allow_masked_compound && mbmi->compound_idx)
2206#else
2207 if (cm->allow_masked_compound)
2208#endif // CONFIG_JNT_COMP
2209 {
Sarah Parker680b9b12017-08-16 18:55:34 -07002210 if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
2211 mbmi->interinter_compound_type =
2212 aom_read_bit(r, ACCT_STR) ? COMPOUND_AVERAGE : COMPOUND_SEG;
2213 else
Cheng Chenbdd6ca82017-10-23 22:34:25 -07002214 mbmi->interinter_compound_type = aom_read_symbol(
2215 r, ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES, ACCT_STR);
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002216 if (mbmi->interinter_compound_type == COMPOUND_WEDGE) {
Sarah Parker680b9b12017-08-16 18:55:34 -07002217 assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002218 mbmi->wedge_index =
2219 aom_read_literal(r, get_wedge_bits_lookup(bsize), ACCT_STR);
2220 mbmi->wedge_sign = aom_read_bit(r, ACCT_STR);
2221 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002222 if (mbmi->interinter_compound_type == COMPOUND_SEG) {
Cheng Chenbdd6ca82017-10-23 22:34:25 -07002223 mbmi->mask_type = aom_read_literal(r, MAX_SEG_MASK_BITS, ACCT_STR);
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002224 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002225 }
Sarah Parker42d96102017-01-31 21:05:27 -08002226 } else {
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002227 mbmi->interinter_compound_type = COMPOUND_AVERAGE;
Sarah Parker42d96102017-01-31 21:05:27 -08002228 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002229 if (xd->counts)
Sarah Parker2d0e9b72017-05-04 01:34:16 +00002230 xd->counts->compound_interinter[bsize][mbmi->interinter_compound_type]++;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002232
Debargha Mukherjee0df711f2017-05-02 16:00:20 -07002233 read_mb_interp_filter(cm, xd, mbmi, r);
Zoe Liu85b66462017-04-20 14:28:19 -07002234
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002235 if (mbmi->motion_mode == WARPED_CAUSAL) {
2236 mbmi->wm_params[0].wmtype = DEFAULT_WMTYPE;
2237
2238#if CONFIG_EXT_WARPED_MOTION
Yunqing Wang3afbf3f2017-11-21 20:16:18 -08002239 if (mbmi->mode == NEARESTMV && best_cand != -1) {
Yunqing Wang876a8b02017-11-13 17:13:27 -08002240 MODE_INFO *best_mi = xd->mi[pts_wm[2 * best_cand]];
2241 assert(best_mi->mbmi.motion_mode == WARPED_CAUSAL);
2242 mbmi->wm_params[0] = best_mi->mbmi.wm_params[0];
2243 } else {
2244 if (mbmi->num_proj_ref[0] > 1)
2245 mbmi->num_proj_ref[0] = sortSamples(pts_mv, &mbmi->mv[0].as_mv, pts,
2246 pts_inref, mbmi->num_proj_ref[0]);
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002247#endif // CONFIG_EXT_WARPED_MOTION
2248
Yunqing Wang876a8b02017-11-13 17:13:27 -08002249 if (find_projection(mbmi->num_proj_ref[0], pts, pts_inref, bsize,
2250 mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
2251 &mbmi->wm_params[0], mi_row, mi_col)) {
Sebastien Alaiwan8d88b292017-11-03 17:38:30 +01002252#if WARPED_MOTION_DEBUG
Yunqing Wang876a8b02017-11-13 17:13:27 -08002253 printf("Warning: unexpected warped model from aomenc\n");
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002254#endif
Yunqing Wang876a8b02017-11-13 17:13:27 -08002255 mbmi->wm_params[0].invalid = 1;
2256 }
2257#if CONFIG_EXT_WARPED_MOTION
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002258 }
Yunqing Wang876a8b02017-11-13 17:13:27 -08002259#endif // CONFIG_EXT_WARPED_MOTION
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002260 }
Sebastien Alaiwan163710c2017-10-26 16:07:29 +02002261
Di Chen56586622017-06-09 13:49:44 -07002262#if DEC_MISMATCH_DEBUG
Zoe Liuf704a1c2017-10-02 16:55:59 -07002263 dec_dump_logs(cm, mi, mi_row, mi_col, mode_ctx);
Di Chen56586622017-06-09 13:49:44 -07002264#endif // DEC_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265}
2266
Yaowu Xuf883b422016-08-30 14:01:10 -07002267static void read_inter_frame_mode_info(AV1Decoder *const pbi,
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002268 MACROBLOCKD *const xd, int mi_row,
2269 int mi_col, aom_reader *r) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002270 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002271 MODE_INFO *const mi = xd->mi[0];
2272 MB_MODE_INFO *const mbmi = &mi->mbmi;
2273 int inter_block = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002274 BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002275
2276 mbmi->mv[0].as_int = 0;
2277 mbmi->mv[1].as_int = 0;
2278 mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
David Barker3aec8d62017-01-31 14:55:32 +00002279
Zoe Liuf704a1c2017-10-02 16:55:59 -07002280#if CONFIG_EXT_SKIP
2281 mbmi->skip_mode = read_skip_mode(cm, xd, mbmi->segment_id, r);
2282#if 0
2283 if (mbmi->skip_mode)
2284 printf("Frame=%d, frame_offset=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d\n",
2285 cm->current_video_frame, cm->frame_offset, mi_row, mi_col,
2286 mbmi->skip_mode);
2287#endif // 0
2288
Zoe Liuf40a9572017-10-13 12:37:19 -07002289 if (mbmi->skip_mode)
Zoe Liuf704a1c2017-10-02 16:55:59 -07002290 mbmi->skip = 1;
Zoe Liuf40a9572017-10-13 12:37:19 -07002291 else
Zoe Liuf704a1c2017-10-02 16:55:59 -07002292#endif // CONFIG_EXT_SKIP
2293 mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
2294
Zoe Liuf40a9572017-10-13 12:37:19 -07002295#if CONFIG_Q_SEGMENTATION
2296 mbmi->q_segment_id = read_q_segment_id(cm, xd, mi_row, mi_col, r);
2297#endif // CONFIG_Q_SEGMENTATION
2298
Steinar Midtskogen6c24b022017-09-15 09:46:39 +02002299 read_cdef(cm, r, mbmi, mi_col, mi_row);
2300
Zoe Liuf40a9572017-10-13 12:37:19 -07002301 if (cm->delta_q_present_flag) {
2302 xd->current_qindex =
2303 xd->prev_qindex +
2304 read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
2305 /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
2306 xd->current_qindex = clamp(xd->current_qindex, 1, MAXQ);
2307 xd->prev_qindex = xd->current_qindex;
Zoe Liuf704a1c2017-10-02 16:55:59 -07002308#if CONFIG_EXT_DELTA_Q
Zoe Liuf40a9572017-10-13 12:37:19 -07002309 if (cm->delta_lf_present_flag) {
Zoe Liuf704a1c2017-10-02 16:55:59 -07002310#if CONFIG_LOOPFILTER_LEVEL
Zoe Liuf40a9572017-10-13 12:37:19 -07002311 if (cm->delta_lf_multi) {
2312 for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
Cheng Chenaff479f2017-10-26 16:53:10 -07002313 const int tmp_lvl =
Zoe Liuf40a9572017-10-13 12:37:19 -07002314 xd->prev_delta_lf[lf_id] +
2315 read_delta_lflevel(cm, xd, r, lf_id, mbmi, mi_col, mi_row) *
Cheng Chen880166a2017-10-02 17:48:48 -07002316 cm->delta_lf_res;
Zoe Liuf40a9572017-10-13 12:37:19 -07002317 mbmi->curr_delta_lf[lf_id] = xd->curr_delta_lf[lf_id] =
Cheng Chenaff479f2017-10-26 16:53:10 -07002318 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Zoe Liuf40a9572017-10-13 12:37:19 -07002319 xd->prev_delta_lf[lf_id] = xd->curr_delta_lf[lf_id];
Cheng Chen880166a2017-10-02 17:48:48 -07002320 }
Zoe Liuf40a9572017-10-13 12:37:19 -07002321 } else {
2322 const int tmp_lvl =
Cheng Chen880166a2017-10-02 17:48:48 -07002323 xd->prev_delta_lf_from_base +
Zoe Liuf40a9572017-10-13 12:37:19 -07002324 read_delta_lflevel(cm, xd, r, -1, mbmi, mi_col, mi_row) *
Cheng Chena97394f2017-09-27 15:05:14 -07002325 cm->delta_lf_res;
Cheng Chenaff479f2017-10-26 16:53:10 -07002326 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
Zoe Liuf40a9572017-10-13 12:37:19 -07002327 clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
Cheng Chen880166a2017-10-02 17:48:48 -07002328 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
Zoe Liuf704a1c2017-10-02 16:55:59 -07002329 }
Zoe Liuf40a9572017-10-13 12:37:19 -07002330#else
2331 const int current_delta_lf_from_base =
2332 xd->prev_delta_lf_from_base +
2333 read_delta_lflevel(cm, xd, r, mbmi, mi_col, mi_row) *
2334 cm->delta_lf_res;
2335 mbmi->current_delta_lf_from_base = xd->current_delta_lf_from_base =
2336 clamp(current_delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
2337 xd->prev_delta_lf_from_base = xd->current_delta_lf_from_base;
2338#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -07002339 }
Zoe Liuf40a9572017-10-13 12:37:19 -07002340#endif // CONFIG_EXT_DELTA_Q
David Barker3aec8d62017-01-31 14:55:32 +00002341 }
Zoe Liuf40a9572017-10-13 12:37:19 -07002342
2343#if CONFIG_EXT_SKIP
2344 if (!mbmi->skip_mode)
Zoe Liuf704a1c2017-10-02 16:55:59 -07002345#endif // CONFIG_EXT_SKIP
Zoe Liuf40a9572017-10-13 12:37:19 -07002346 inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
David Barker3aec8d62017-01-31 14:55:32 +00002347
Yushin Choc24351c2017-10-24 14:59:08 -07002348 mbmi->current_q_index = xd->current_qindex;
2349
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002350 xd->above_txfm_context =
2351 cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
2352 xd->left_txfm_context = xd->left_txfm_context_buffer +
2353 ((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
Jingning Han581d1692017-01-05 16:03:54 -08002354
Debargha Mukherjee4def76a2017-10-19 13:38:35 -07002355 if (cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
2356 !mbmi->skip && inter_block && !xd->lossless[mbmi->segment_id]) {
Yue Chen0797a202017-10-27 17:24:56 -07002357 const TX_SIZE max_tx_size = get_max_rect_tx_size(bsize, inter_block);
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002358 const int bh = tx_size_high_unit[max_tx_size];
2359 const int bw = tx_size_wide_unit[max_tx_size];
2360 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2361 const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
Yue Chena1e48dc2016-08-29 17:29:33 -07002362
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002363 mbmi->min_tx_size = TX_SIZES_ALL;
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01002364 for (int idy = 0; idy < height; idy += bh)
2365 for (int idx = 0; idx < width; idx += bw)
Debargha Mukherjeeedc73462017-10-31 15:13:32 -07002366 read_tx_size_vartx(cm, xd, mbmi, xd->counts, max_tx_size, 0, idy, idx,
2367 r);
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002368 } else {
2369 mbmi->tx_size = read_tx_size(cm, xd, inter_block, !mbmi->skip, r);
2370
Zoe Liuf40a9572017-10-13 12:37:19 -07002371 if (inter_block) {
2372 const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
2373 const int height = block_size_high[bsize] >> tx_size_high_log2[0];
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01002374 for (int idy = 0; idy < height; ++idy)
2375 for (int idx = 0; idx < width; ++idx)
Zoe Liuf40a9572017-10-13 12:37:19 -07002376 mbmi->inter_tx_size[idy >> 1][idx >> 1] = mbmi->tx_size;
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002377 }
Zoe Liuf40a9572017-10-13 12:37:19 -07002378 mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
2379 set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002380 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002381
2382 if (inter_block)
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002383 read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384 else
Jingning Han36fe3202017-02-20 22:31:49 -08002385 read_intra_block_mode_info(cm, mi_row, mi_col, xd, mi, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002387#if !CONFIG_TXK_SEL
Zoe Liuf40a9572017-10-13 12:37:19 -07002388#if CONFIG_EXT_SKIP
2389 if (!mbmi->skip_mode)
2390#endif // CONFIG_EXT_SKIP
2391 av1_read_tx_type(cm, xd, r);
Angie Chiangcd9b03f2017-04-16 13:37:13 -07002392#endif // !CONFIG_TXK_SEL
Yaowu Xuc27fc142016-08-22 16:08:15 -07002393}
2394
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002395static void av1_intra_copy_frame_mvs(AV1_COMMON *const cm, int mi_row,
2396 int mi_col, int x_mis, int y_mis) {
Jingning Hanc7d198e2017-10-12 16:11:06 -07002397#if CONFIG_TMV || CONFIG_MFMV
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002398 const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_cols, 1);
Yunqing Wangc99a5642017-10-11 13:48:22 -07002399 MV_REF *frame_mvs =
2400 cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002401 x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
2402 y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
2403#else
2404 const int frame_mvs_stride = cm->mi_cols;
2405 MV_REF *frame_mvs = cm->cur_frame->mvs +
2406 (mi_row & 0xfffe) * frame_mvs_stride + (mi_col & 0xfffe);
2407 x_mis = AOMMAX(x_mis, 2);
2408 y_mis = AOMMAX(y_mis, 2);
2409#endif // CONFIG_TMV
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002410
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01002411 for (int h = 0; h < y_mis; h++) {
Yunqing Wangb90a97a2017-10-24 11:50:15 -07002412 MV_REF *mv = frame_mvs;
Sebastien Alaiwanb85beed2017-12-01 12:23:12 +01002413 for (int w = 0; w < x_mis; w++) {
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002414 mv->ref_frame[0] = NONE_FRAME;
2415 mv->ref_frame[1] = NONE_FRAME;
Yunqing Wangb90a97a2017-10-24 11:50:15 -07002416 mv++;
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002417 }
Yunqing Wangb90a97a2017-10-24 11:50:15 -07002418 frame_mvs += frame_mvs_stride;
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002419 }
2420}
2421
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002422void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd, int mi_row,
2423 int mi_col, aom_reader *r, int x_mis, int y_mis) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002424 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425 MODE_INFO *const mi = xd->mi[0];
Alex Converse28744302017-04-13 14:46:22 -07002426#if CONFIG_INTRABC
2427 mi->mbmi.use_intrabc = 0;
2428#endif // CONFIG_INTRABC
2429
Yaowu Xuc27fc142016-08-22 16:08:15 -07002430 if (frame_is_intra_only(cm)) {
2431 read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002432 av1_intra_copy_frame_mvs(cm, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433 } else {
Sebastien Alaiwan0cf54d42017-10-16 16:10:04 +02002434 read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r);
Yunqing Wangd1d511f2017-10-05 08:44:46 -07002435 av1_copy_frame_mvs(cm, mi, mi_row, mi_col, x_mis, y_mis);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002436 }
2437}