blob: 5c5d91cc43edfd3e5e066b066e042df4ae9c794c [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
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AV1_COMMON_BLOCKD_H_
13#define AV1_COMMON_BLOCKD_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018#include "aom_ports/mem.h"
19#include "aom_scale/yv12config.h"
20
21#include "av1/common/common_data.h"
22#include "av1/common/quant_common.h"
23#include "av1/common/entropy.h"
24#include "av1/common/entropymode.h"
25#include "av1/common/mv.h"
26#include "av1/common/scale.h"
27#include "av1/common/seg_common.h"
28#include "av1/common/tile_common.h"
Yushin Chod0b77ac2017-10-20 17:33:16 -070029
Yaowu Xuc27fc142016-08-22 16:08:15 -070030#ifdef __cplusplus
31extern "C" {
32#endif
33
Debargha Mukherjee6cf2b462018-01-12 15:53:50 -080034#if CONFIG_NEW_QUANT
35#define USE_B_QUANT_NO_TRELLIS 0
36#else
37#define USE_B_QUANT_NO_TRELLIS 1
38#endif // CONFIG_NEW_QUANT
39
Yaowu Xuc27fc142016-08-22 16:08:15 -070040#define MAX_MB_PLANE 3
41
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -080042// Set COMPOUND_SEGMENT_TYPE to one of the three
43// 0: Uniform
44// 1: Difference weighted
45#define COMPOUND_SEGMENT_TYPE 1
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -080046#define MAX_SEG_MASK_BITS 1
Yaowu Xuf35f5272017-05-10 08:00:02 -070047
Sarah Parkerb9f757c2017-01-06 17:12:24 -080048// SEG_MASK_TYPES should not surpass 1 << MAX_SEG_MASK_BITS
49typedef enum {
Yaowu Xuf35f5272017-05-10 08:00:02 -070050#if COMPOUND_SEGMENT_TYPE == 0
Sarah Parkerb9f757c2017-01-06 17:12:24 -080051 UNIFORM_45 = 0,
52 UNIFORM_45_INV,
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -080053#elif COMPOUND_SEGMENT_TYPE == 1
Sarah Parker7bb84f32017-05-09 15:17:46 -070054 DIFFWTD_38 = 0,
55 DIFFWTD_38_INV,
Yaowu Xuf35f5272017-05-10 08:00:02 -070056#endif // COMPOUND_SEGMENT_TYPE
Debargha Mukherjee1edf9a32017-01-07 18:54:20 -080057 SEG_MASK_TYPES,
58} SEG_MASK_TYPE;
59
Yaowu Xuc27fc142016-08-22 16:08:15 -070060typedef enum {
61 KEY_FRAME = 0,
62 INTER_FRAME = 1,
Soo-Chul Han65c00ae2017-09-07 13:12:35 -040063#if CONFIG_OBU
64 INTRA_ONLY_FRAME = 2, // replaces intra-only
65 S_FRAME = 3,
66#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070067 FRAME_TYPES,
68} FRAME_TYPE;
69
Debargha Mukherjee0f248c42017-09-07 12:40:18 -070070static INLINE int is_comp_ref_allowed(BLOCK_SIZE bsize) {
71 (void)bsize;
Debargha Mukherjee0f248c42017-09-07 12:40:18 -070072 return AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
Debargha Mukherjee0f248c42017-09-07 12:40:18 -070073}
74
Yaowu Xuc27fc142016-08-22 16:08:15 -070075static INLINE int is_inter_mode(PREDICTION_MODE mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070076 return mode >= NEARESTMV && mode <= NEW_NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -070077}
78
David Barkerac37fa32016-12-02 12:30:21 +000079typedef struct {
80 uint8_t *plane[MAX_MB_PLANE];
81 int stride[MAX_MB_PLANE];
82} BUFFER_SET;
83
Yaowu Xuc27fc142016-08-22 16:08:15 -070084static INLINE int is_inter_singleref_mode(PREDICTION_MODE mode) {
Zoe Liu7f24e1b2017-03-17 17:42:05 -070085 return mode >= NEARESTMV && mode <= NEWMV;
Yaowu Xuc27fc142016-08-22 16:08:15 -070086}
Zoe Liu85b66462017-04-20 14:28:19 -070087static INLINE int is_inter_compound_mode(PREDICTION_MODE mode) {
88 return mode >= NEAREST_NEARESTMV && mode <= NEW_NEWMV;
89}
Yaowu Xuc27fc142016-08-22 16:08:15 -070090
91static INLINE PREDICTION_MODE compound_ref0_mode(PREDICTION_MODE mode) {
Urvang Joshi5a9ea002017-05-22 15:25:18 -070092 static PREDICTION_MODE lut[] = {
Urvang Joshi102245d2016-11-28 13:05:36 -080093 MB_MODE_COUNT, // DC_PRED
94 MB_MODE_COUNT, // V_PRED
95 MB_MODE_COUNT, // H_PRED
96 MB_MODE_COUNT, // D45_PRED
97 MB_MODE_COUNT, // D135_PRED
98 MB_MODE_COUNT, // D117_PRED
99 MB_MODE_COUNT, // D153_PRED
100 MB_MODE_COUNT, // D207_PRED
101 MB_MODE_COUNT, // D63_PRED
Urvang Joshi102245d2016-11-28 13:05:36 -0800102 MB_MODE_COUNT, // SMOOTH_PRED
Urvang Joshi5a9ea002017-05-22 15:25:18 -0700103 MB_MODE_COUNT, // SMOOTH_V_PRED
104 MB_MODE_COUNT, // SMOOTH_H_PRED
Urvang Joshi96d1c0a2017-10-10 13:15:32 -0700105 MB_MODE_COUNT, // PAETH_PRED
Urvang Joshi102245d2016-11-28 13:05:36 -0800106 MB_MODE_COUNT, // NEARESTMV
107 MB_MODE_COUNT, // NEARMV
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700108 MB_MODE_COUNT, // GLOBALMV
Urvang Joshi102245d2016-11-28 13:05:36 -0800109 MB_MODE_COUNT, // NEWMV
Sebastien Alaiwan34d55662017-11-15 09:36:03 +0100110 NEARESTMV, // NEAREST_NEARESTMV
111 NEARMV, // NEAR_NEARMV
112 NEARESTMV, // NEAREST_NEWMV
113 NEWMV, // NEW_NEARESTMV
114 NEARMV, // NEAR_NEWMV
115 NEWMV, // NEW_NEARMV
116 GLOBALMV, // GLOBAL_GLOBALMV
117 NEWMV, // NEW_NEWMV
Yaowu Xuc27fc142016-08-22 16:08:15 -0700118 };
Urvang Joshi5a9ea002017-05-22 15:25:18 -0700119 assert(NELEMENTS(lut) == MB_MODE_COUNT);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700120 assert(is_inter_compound_mode(mode));
121 return lut[mode];
122}
123
124static INLINE PREDICTION_MODE compound_ref1_mode(PREDICTION_MODE mode) {
Urvang Joshi5a9ea002017-05-22 15:25:18 -0700125 static PREDICTION_MODE lut[] = {
Urvang Joshi102245d2016-11-28 13:05:36 -0800126 MB_MODE_COUNT, // DC_PRED
127 MB_MODE_COUNT, // V_PRED
128 MB_MODE_COUNT, // H_PRED
129 MB_MODE_COUNT, // D45_PRED
130 MB_MODE_COUNT, // D135_PRED
131 MB_MODE_COUNT, // D117_PRED
132 MB_MODE_COUNT, // D153_PRED
133 MB_MODE_COUNT, // D207_PRED
134 MB_MODE_COUNT, // D63_PRED
Urvang Joshi102245d2016-11-28 13:05:36 -0800135 MB_MODE_COUNT, // SMOOTH_PRED
Urvang Joshi5a9ea002017-05-22 15:25:18 -0700136 MB_MODE_COUNT, // SMOOTH_V_PRED
137 MB_MODE_COUNT, // SMOOTH_H_PRED
Urvang Joshi96d1c0a2017-10-10 13:15:32 -0700138 MB_MODE_COUNT, // PAETH_PRED
Urvang Joshi102245d2016-11-28 13:05:36 -0800139 MB_MODE_COUNT, // NEARESTMV
140 MB_MODE_COUNT, // NEARMV
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700141 MB_MODE_COUNT, // GLOBALMV
Urvang Joshi102245d2016-11-28 13:05:36 -0800142 MB_MODE_COUNT, // NEWMV
Sebastien Alaiwan34d55662017-11-15 09:36:03 +0100143 NEARESTMV, // NEAREST_NEARESTMV
144 NEARMV, // NEAR_NEARMV
145 NEWMV, // NEAREST_NEWMV
146 NEARESTMV, // NEW_NEARESTMV
147 NEWMV, // NEAR_NEWMV
148 NEARMV, // NEW_NEARMV
149 GLOBALMV, // GLOBAL_GLOBALMV
150 NEWMV, // NEW_NEWMV
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 };
Urvang Joshi5a9ea002017-05-22 15:25:18 -0700152 assert(NELEMENTS(lut) == MB_MODE_COUNT);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 assert(is_inter_compound_mode(mode));
154 return lut[mode];
155}
156
David Barker3dfba992017-04-03 16:10:09 +0100157static INLINE int have_nearmv_in_inter_mode(PREDICTION_MODE mode) {
Debargha Mukherjeebb6e1342017-04-17 16:05:04 -0700158 return (mode == NEARMV || mode == NEAR_NEARMV || mode == NEAR_NEWMV ||
159 mode == NEW_NEARMV);
David Barker3dfba992017-04-03 16:10:09 +0100160}
161
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162static INLINE int have_newmv_in_inter_mode(PREDICTION_MODE mode) {
Zoe Liu7f24e1b2017-03-17 17:42:05 -0700163 return (mode == NEWMV || mode == NEW_NEWMV || mode == NEAREST_NEWMV ||
164 mode == NEW_NEARESTMV || mode == NEAR_NEWMV || mode == NEW_NEARMV);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165}
Sarah Parker6fdc8532016-11-16 17:47:13 -0800166
Sarah Parker2e604882017-01-17 17:31:25 -0800167static INLINE int use_masked_motion_search(COMPOUND_TYPE type) {
168 return (type == COMPOUND_WEDGE);
169}
170
Sarah Parker6fdc8532016-11-16 17:47:13 -0800171static INLINE int is_masked_compound_type(COMPOUND_TYPE type) {
Sarah Parker569edda2016-12-14 14:57:38 -0800172 return (type == COMPOUND_WEDGE || type == COMPOUND_SEG);
Sarah Parker6fdc8532016-11-16 17:47:13 -0800173}
Zoe Liu85b66462017-04-20 14:28:19 -0700174
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175/* For keyframes, intra block modes are predicted by the (already decoded)
176 modes for the Y blocks to the left and above us; for interframes, there
177 is a single probability table. */
178
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179typedef int8_t MV_REFERENCE_FRAME;
180
181typedef struct {
182 // Number of base colors for Y (0) and UV (1)
183 uint8_t palette_size[2];
hui sufa4ff852017-05-15 12:20:50 -0700184 // Value of base colors for Y, U, and V
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185 uint16_t palette_colors[3 * PALETTE_MAX_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186} PALETTE_MODE_INFO;
187
hui su5db97432016-10-14 16:10:14 -0700188#if CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189typedef struct {
Yue Chenb0571872017-12-18 18:12:59 -0800190 uint8_t use_filter_intra;
191 FILTER_INTRA_MODE filter_intra_mode;
hui su5db97432016-10-14 16:10:14 -0700192} FILTER_INTRA_MODE_INFO;
Yue Chen57b8ff62017-10-10 23:37:31 -0700193
194static const PREDICTION_MODE fimode_to_intradir[FILTER_INTRA_MODES] = {
Yue Chenda2eefc2017-11-16 15:25:28 -0800195 DC_PRED, V_PRED, H_PRED, D153_PRED, DC_PRED
Yue Chen57b8ff62017-10-10 23:37:31 -0700196};
hui su5db97432016-10-14 16:10:14 -0700197#endif // CONFIG_FILTER_INTRA
Yaowu Xuc27fc142016-08-22 16:08:15 -0700198
Angie Chiang7c2b7f22016-11-07 16:00:00 -0800199#if CONFIG_RD_DEBUG
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800200#define TXB_COEFF_COST_MAP_SIZE (2 * MAX_MIB_SIZE)
Angie Chiang7c2b7f22016-11-07 16:00:00 -0800201#endif
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800202
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800203typedef struct RD_STATS {
204 int rate;
205 int64_t dist;
Angie Chiang7bbd3b12017-04-26 11:06:09 -0700206 // Please be careful of using rdcost, it's not guaranteed to be set all the
207 // time.
208 // TODO(angiebird): Create a set of functions to manipulate the RD_STATS. In
209 // these functions, make sure rdcost is always up-to-date according to
210 // rate/dist.
Angie Chiang2a2a7dd2017-04-25 16:08:47 -0700211 int64_t rdcost;
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800212 int64_t sse;
Angie Chiang7bbd3b12017-04-26 11:06:09 -0700213 int skip; // sse should equal to dist when skip == 1
Jingning Han3bce7542017-07-25 10:53:57 -0700214 int64_t ref_rdcost;
215 int zero_rate;
Jingning Han1a7f0a82017-07-27 09:48:05 -0700216 uint8_t invalid_rate;
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800217#if CONFIG_RD_DEBUG
218 int txb_coeff_cost[MAX_MB_PLANE];
219 int txb_coeff_cost_map[MAX_MB_PLANE][TXB_COEFF_COST_MAP_SIZE]
220 [TXB_COEFF_COST_MAP_SIZE];
Angie Chiang3963d632016-11-10 18:41:40 -0800221#endif // CONFIG_RD_DEBUG
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800222} RD_STATS;
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800223
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000224// This struct is used to group function args that are commonly
225// sent together in functions related to interinter compound modes
Sarah Parker6fdc8532016-11-16 17:47:13 -0800226typedef struct {
Sarah Parker6fdc8532016-11-16 17:47:13 -0800227 int wedge_index;
228 int wedge_sign;
Sarah Parkerb9f757c2017-01-06 17:12:24 -0800229 SEG_MASK_TYPE mask_type;
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000230 uint8_t *seg_mask;
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000231 COMPOUND_TYPE interinter_compound_type;
Sarah Parker6fdc8532016-11-16 17:47:13 -0800232} INTERINTER_COMPOUND_DATA;
Sarah Parker6fdc8532016-11-16 17:47:13 -0800233
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234// This structure now relates to 8x8 block regions.
Luc Trudeauf5334002017-04-25 12:21:26 -0400235typedef struct MB_MODE_INFO {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700236 // Common for both INTER and INTRA blocks
237 BLOCK_SIZE sb_type;
238 PREDICTION_MODE mode;
239 TX_SIZE tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 // TODO(jingning): This effectively assigned a separate entry for each
241 // 8x8 block. Apparently it takes much more space than needed.
242 TX_SIZE inter_tx_size[MAX_MIB_SIZE][MAX_MIB_SIZE];
Jingning Hane67b38a2016-11-04 10:30:00 -0700243 TX_SIZE min_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700244 int8_t skip;
Zoe Liuf704a1c2017-10-02 16:55:59 -0700245#if CONFIG_EXT_SKIP
246 int8_t skip_mode;
247#endif // CONFIG_EXT_SKIP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700248 int8_t segment_id;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 int8_t seg_id_predicted; // valid only when temporal_update is enabled
250
251 // Only for INTRA blocks
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400252 UV_PREDICTION_MODE uv_mode;
Urvang Joshic6300aa2017-06-01 14:46:23 -0700253
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 PALETTE_MODE_INFO palette_mode_info;
Alex Converse28744302017-04-13 14:46:22 -0700255#if CONFIG_INTRABC
256 uint8_t use_intrabc;
257#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258
Rupert Swarbrick27e90292017-09-28 17:46:50 +0100259 // Only for INTER blocks
260 InterpFilters interp_filters;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261 MV_REFERENCE_FRAME ref_frame[2];
262 TX_TYPE tx_type;
Angie Chiangcd9b03f2017-04-16 13:37:13 -0700263#if CONFIG_TXK_SEL
Angie Chiang808d8592017-04-06 18:36:55 -0700264 TX_TYPE txk_type[MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
265#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266
hui su5db97432016-10-14 16:10:14 -0700267#if CONFIG_FILTER_INTRA
268 FILTER_INTRA_MODE_INFO filter_intra_mode_info;
269#endif // CONFIG_FILTER_INTRA
hui sueda3d762016-12-06 16:58:23 -0800270 // The actual prediction angle is the base angle + (angle_delta * step).
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271 int8_t angle_delta[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000273 // interintra members
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 INTERINTRA_MODE interintra_mode;
275 // TODO(debargha): Consolidate these flags
276 int use_wedge_interintra;
277 int interintra_wedge_index;
278 int interintra_wedge_sign;
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000279 // interinter members
280 COMPOUND_TYPE interinter_compound_type;
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000281 int wedge_index;
282 int wedge_sign;
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000283 SEG_MASK_TYPE mask_type;
Yue Chencb60b182016-10-13 15:18:22 -0700284 MOTION_MODE motion_mode;
Yue Chen5329a2b2017-02-28 17:33:00 +0800285 int overlappable_neighbors[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 int_mv mv[2];
287 int_mv pred_mv[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 uint8_t ref_mv_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289#if CONFIG_EXT_PARTITION_TYPES
290 PARTITION_TYPE partition;
291#endif
292#if CONFIG_NEW_QUANT
293 int dq_off_index;
294 int send_dq_bit;
295#endif // CONFIG_NEW_QUANT
296 /* deringing gain *per-superblock* */
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -0400297 int8_t cdef_strength;
Arild Fuldseth07441162016-08-15 15:07:52 +0200298 int current_q_index;
Fangwen Fu231fe422017-04-24 17:52:29 -0700299#if CONFIG_EXT_DELTA_Q
300 int current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -0700301#if CONFIG_LOOPFILTER_LEVEL
302 int curr_delta_lf[FRAME_LF_COUNT];
303#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700304#endif
Angie Chiangd4022822016-11-02 18:30:25 -0700305#if CONFIG_RD_DEBUG
Angie Chiang9a44f5f2016-11-06 12:19:06 -0800306 RD_STATS rd_stats;
Angie Chiangd4022822016-11-02 18:30:25 -0700307 int mi_row;
308 int mi_col;
309#endif
Yue Chen69f18e12016-09-08 14:48:15 -0700310 int num_proj_ref[2];
311 WarpedMotionParams wm_params[2];
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800312
Luc Trudeauf5334002017-04-25 12:21:26 -0400313#if CONFIG_CFL
314 // Index of the alpha Cb and alpha Cr combination
Luc Trudeaua9bd85f2017-05-11 14:37:56 -0400315 int cfl_alpha_idx;
David Michael Barrf6eaa152017-07-19 19:42:28 +0900316 // Joint sign of alpha Cb and alpha Cr
317 int cfl_alpha_signs;
Luc Trudeauf5334002017-04-25 12:21:26 -0400318#endif
319
Cheng Chenca6958c2017-10-10 14:00:50 -0700320#if CONFIG_JNT_COMP
321 int compound_idx;
Cheng Chen33a13d92017-11-28 16:49:59 -0800322 int comp_group_idx;
Cheng Chenca6958c2017-10-10 14:00:50 -0700323#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324} MB_MODE_INFO;
325
Jingning Han2fac8a42017-12-14 16:26:00 -0800326typedef struct MODE_INFO { MB_MODE_INFO mbmi; } MODE_INFO;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700327
Alex Converse28744302017-04-13 14:46:22 -0700328#if CONFIG_INTRABC
Hui Su27df8342017-11-07 15:16:05 -0800329#define NO_FILTER_FOR_IBC 1 // Disable in-loop filters for frame with intrabc
330
Alex Converse28744302017-04-13 14:46:22 -0700331static INLINE int is_intrabc_block(const MB_MODE_INFO *mbmi) {
332 return mbmi->use_intrabc;
333}
334#endif
335
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400336#if CONFIG_CFL
337static INLINE PREDICTION_MODE get_uv_mode(UV_PREDICTION_MODE mode) {
Luc Trudeau5d5078e2017-12-13 16:43:16 -0500338 assert(mode < UV_INTRA_MODES);
Luc Trudeau2f052ee2018-01-16 14:43:06 -0500339 static const PREDICTION_MODE uv2y[] = {
Urvang Joshib7301cd2017-11-09 15:08:56 -0800340 DC_PRED, // UV_DC_PRED
341 V_PRED, // UV_V_PRED
342 H_PRED, // UV_H_PRED
343 D45_PRED, // UV_D45_PRED
344 D135_PRED, // UV_D135_PRED
345 D117_PRED, // UV_D117_PRED
346 D153_PRED, // UV_D153_PRED
347 D207_PRED, // UV_D207_PRED
348 D63_PRED, // UV_D63_PRED
349 SMOOTH_PRED, // UV_SMOOTH_PRED
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400350 SMOOTH_V_PRED, // UV_SMOOTH_V_PRED
351 SMOOTH_H_PRED, // UV_SMOOTH_H_PRED
Urvang Joshi96d1c0a2017-10-10 13:15:32 -0700352 PAETH_PRED, // UV_PAETH_PRED
Luc Trudeau2f052ee2018-01-16 14:43:06 -0500353 DC_PRED, // UV_CFL_PRED
354 INTRA_INVALID, // UV_INTRA_MODES
355 INTRA_INVALID, // UV_MODE_INVALID
Luc Trudeaud6d9eee2017-07-12 12:36:50 -0400356 };
357 return uv2y[mode];
358}
359#else
360static INLINE PREDICTION_MODE get_uv_mode(PREDICTION_MODE mode) { return mode; }
361#endif // CONFIG_CFL
362
Yaowu Xuc27fc142016-08-22 16:08:15 -0700363static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
Alex Converse28744302017-04-13 14:46:22 -0700364#if CONFIG_INTRABC
365 if (is_intrabc_block(mbmi)) return 1;
366#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367 return mbmi->ref_frame[0] > INTRA_FRAME;
368}
369
370static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
371 return mbmi->ref_frame[1] > INTRA_FRAME;
372}
373
Zoe Liuc082bbc2017-05-17 13:31:37 -0700374#if CONFIG_EXT_COMP_REFS
375static INLINE int has_uni_comp_refs(const MB_MODE_INFO *mbmi) {
376 return has_second_ref(mbmi) && (!((mbmi->ref_frame[0] >= BWDREF_FRAME) ^
377 (mbmi->ref_frame[1] >= BWDREF_FRAME)));
378}
379
380static INLINE MV_REFERENCE_FRAME comp_ref0(int ref_idx) {
381 static const MV_REFERENCE_FRAME lut[] = {
382 LAST_FRAME, // LAST_LAST2_FRAMES,
Zoe Liufcf5fa22017-06-26 16:00:38 -0700383 LAST_FRAME, // LAST_LAST3_FRAMES,
Zoe Liuc082bbc2017-05-17 13:31:37 -0700384 LAST_FRAME, // LAST_GOLDEN_FRAMES,
385 BWDREF_FRAME, // BWDREF_ALTREF_FRAMES,
386 };
387 assert(NELEMENTS(lut) == UNIDIR_COMP_REFS);
388 return lut[ref_idx];
389}
390
391static INLINE MV_REFERENCE_FRAME comp_ref1(int ref_idx) {
392 static const MV_REFERENCE_FRAME lut[] = {
393 LAST2_FRAME, // LAST_LAST2_FRAMES,
Zoe Liufcf5fa22017-06-26 16:00:38 -0700394 LAST3_FRAME, // LAST_LAST3_FRAMES,
Zoe Liuc082bbc2017-05-17 13:31:37 -0700395 GOLDEN_FRAME, // LAST_GOLDEN_FRAMES,
396 ALTREF_FRAME, // BWDREF_ALTREF_FRAMES,
397 };
398 assert(NELEMENTS(lut) == UNIDIR_COMP_REFS);
399 return lut[ref_idx];
400}
401#endif // CONFIG_EXT_COMP_REFS
402
Jingning Han9010e202017-12-14 14:48:09 -0800403PREDICTION_MODE av1_left_block_mode(const MODE_INFO *left_mi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404
Jingning Han9010e202017-12-14 14:48:09 -0800405PREDICTION_MODE av1_above_block_mode(const MODE_INFO *above_mi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700406
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500407static INLINE int is_global_mv_block(const MODE_INFO *mi,
Sarah Parker19234cc2017-03-10 16:43:25 -0800408 TransformationType type) {
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500409 const MB_MODE_INFO *const mbmi = &mi->mbmi;
410 const PREDICTION_MODE mode = mbmi->mode;
Sarah Parker19234cc2017-03-10 16:43:25 -0800411#if GLOBAL_SUB8X8_USED
412 const int block_size_allowed = 1;
413#else
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500414 const BLOCK_SIZE bsize = mbmi->sb_type;
Debargha Mukherjeec17a4432017-08-28 22:22:45 -0700415 const int block_size_allowed =
416 AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
Sarah Parker19234cc2017-03-10 16:43:25 -0800417#endif // GLOBAL_SUB8X8_USED
Sarah Parker2b9ec2e2017-10-30 17:34:08 -0700418 return (mode == GLOBALMV || mode == GLOBAL_GLOBALMV) && type > TRANSLATION &&
Sarah Parker19234cc2017-03-10 16:43:25 -0800419 block_size_allowed;
Sarah Parker19234cc2017-03-10 16:43:25 -0800420}
Sarah Parker19234cc2017-03-10 16:43:25 -0800421
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800422#if CONFIG_MISMATCH_DEBUG
423static INLINE void mi_to_pixel_loc(int *pixel_c, int *pixel_r, int mi_col,
424 int mi_row, int tx_blk_col, int tx_blk_row,
425 int subsampling_x, int subsampling_y) {
426 *pixel_c = ((mi_col >> subsampling_x) << MI_SIZE_LOG2) +
427 (tx_blk_col << tx_size_wide_log2[0]);
428 *pixel_r = ((mi_row >> subsampling_y) << MI_SIZE_LOG2) +
429 (tx_blk_row << tx_size_high_log2[0]);
430}
431#endif
432
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433enum mv_precision { MV_PRECISION_Q3, MV_PRECISION_Q4 };
434
435struct buf_2d {
436 uint8_t *buf;
437 uint8_t *buf0;
438 int width;
439 int height;
440 int stride;
441};
442
443typedef struct macroblockd_plane {
444 tran_low_t *dqcoeff;
445 PLANE_TYPE plane_type;
446 int subsampling_x;
447 int subsampling_y;
448 struct buf_2d dst;
449 struct buf_2d pre[2];
450 ENTROPY_CONTEXT *above_context;
451 ENTROPY_CONTEXT *left_context;
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400452
453 // The dequantizers below are true dequntizers used only in the
454 // dequantization process. They have the same coefficient
455 // shift/scale as TX.
456 int16_t seg_dequant_QTX[MAX_SEGMENTS][2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700457#if CONFIG_NEW_QUANT
Sarah Parker6b56e992017-12-07 11:51:04 -0800458 dequant_val_type_nuq seg_dequant_nuq_QTX[MAX_SEGMENTS][QUANT_PROFILES][2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459#endif
460 uint8_t *color_index_map;
461
Jingning Hanc47fe6c2016-10-21 16:40:47 -0700462 // block size in pixels
463 uint8_t width, height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464
465#if CONFIG_AOM_QM
Thomas Daviesdd3cf832017-10-20 15:49:57 +0100466 qm_val_t *seg_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
467 qm_val_t *seg_qmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468#endif
Monty Montgomery125c0fc2017-10-26 00:44:35 -0400469
470 // the 'dequantizers' below are not literal dequantizer values.
471 // They're used by encoder RDO to generate ad-hoc lambda values.
472 // They use a hardwired Q3 coeff shift and do not necessarily match
473 // the TX scale in use.
474 const int16_t *dequant_Q3;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700475
Yushin Chod0b77ac2017-10-20 17:33:16 -0700476#if CONFIG_DIST_8X8
Yushin Cho77bba8d2016-11-04 16:36:56 -0700477 DECLARE_ALIGNED(16, int16_t, pred[MAX_SB_SQUARE]);
Yushin Chob7b60c52017-07-14 16:18:52 -0700478#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479} MACROBLOCKD_PLANE;
480
Jingning Han3468df12016-12-05 17:53:16 -0800481#define BLOCK_OFFSET(x, i) \
482 ((x) + (i) * (1 << (tx_size_wide_log2[0] + tx_size_high_log2[0])))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483
484typedef struct RefBuffer {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485 int idx;
486 YV12_BUFFER_CONFIG *buf;
487 struct scale_factors sf;
488} RefBuffer;
489
Rupert Swarbrick6c545212017-09-01 17:17:25 +0100490#if CONFIG_LOOP_RESTORATION
491typedef struct {
492 DECLARE_ALIGNED(16, InterpKernel, vfilter);
493 DECLARE_ALIGNED(16, InterpKernel, hfilter);
494} WienerInfo;
495
496typedef struct {
497 int ep;
498 int xqd[2];
499} SgrprojInfo;
500#endif // CONFIG_LOOP_RESTORATION
501
David Michael Barr5b2021e2017-08-17 18:12:39 +0900502#if CONFIG_CFL
Hui Su9fa96232017-10-23 15:46:04 -0700503#if CONFIG_DEBUG
Luc Trudeau03b7e7d2017-09-19 13:20:52 -0400504#define CFL_SUB8X8_VAL_MI_SIZE (4)
505#define CFL_SUB8X8_VAL_MI_SQUARE \
506 (CFL_SUB8X8_VAL_MI_SIZE * CFL_SUB8X8_VAL_MI_SIZE)
Hui Su9fa96232017-10-23 15:46:04 -0700507#endif // CONFIG_DEBUG
Luc Trudeaue425f472017-12-08 14:48:46 -0500508#define CFL_MAX_BLOCK_SIZE (BLOCK_32X32)
Luc Trudeau467205a2017-12-12 23:23:47 -0500509#define CFL_BUF_LINE (32)
510#define CFL_BUF_SQUARE (CFL_BUF_LINE * CFL_BUF_LINE)
David Michael Barr5b2021e2017-08-17 18:12:39 +0900511typedef struct cfl_ctx {
Luc Trudeau4e26d662017-09-11 13:08:40 -0400512 // The CfL prediction buffer is used in two steps:
513 // 1. Stores Q3 reconstructed luma pixels
514 // (only Q2 is required, but Q3 is used to avoid shifts)
515 // 2. Stores Q3 AC contributions (step1 - tx block avg)
Luc Trudeau467205a2017-12-12 23:23:47 -0500516 int16_t pred_buf_q3[CFL_BUF_SQUARE];
517
518 // Cache the DC_PRED when performing RDO, so it does not have to be recomputed
519 // for every scaling parameter
520 int dc_pred_is_cached[CFL_PRED_PLANES];
521 // The DC_PRED cache is disable when decoding
522 int use_dc_pred_cache;
523 // Only cache the first row of the DC_PRED
524 int16_t dc_pred_cache[CFL_PRED_PLANES][CFL_BUF_LINE];
David Michael Barr5b2021e2017-08-17 18:12:39 +0900525
Luc Trudeau4e26d662017-09-11 13:08:40 -0400526 // Height and width currently used in the CfL prediction buffer.
527 int buf_height, buf_width;
David Michael Barr5b2021e2017-08-17 18:12:39 +0900528
David Michael Barr5b2021e2017-08-17 18:12:39 +0900529 int are_parameters_computed;
530
531 // Chroma subsampling
532 int subsampling_x, subsampling_y;
533
David Michael Barr5b2021e2017-08-17 18:12:39 +0900534 int mi_row, mi_col;
535
536 // Whether the reconstructed luma pixels need to be stored
537 int store_y;
538
David Michael Barr1f8d0952017-10-11 17:46:39 +0900539#if CONFIG_DEBUG
540 int rate;
541#endif // CONFIG_DEBUG
542
David Michael Barr5b2021e2017-08-17 18:12:39 +0900543 int is_chroma_reference;
Hui Su9fa96232017-10-23 15:46:04 -0700544#if CONFIG_DEBUG
Luc Trudeauc7af36d2017-10-11 21:01:00 -0400545 // Validation buffer is usually 2x2, except for 16x4 and 4x16 in that case it
546 // is 4x2 and 2x4 respectively. To simplify accessing the buffer we use a
547 // stride of CFL_SUB8X8_VAL_MI_SIZE resulting in a square of 16.
548 uint16_t sub8x8_val[CFL_SUB8X8_VAL_MI_SQUARE];
549 uint16_t store_counter;
550 uint16_t last_compute_counter;
Hui Su9fa96232017-10-23 15:46:04 -0700551#endif // CONFIG_DEBUG
David Michael Barr5b2021e2017-08-17 18:12:39 +0900552} CFL_CTX;
553#endif // CONFIG_CFL
554
Cheng Chenf78632e2017-10-20 15:30:51 -0700555#if CONFIG_JNT_COMP
556typedef struct jnt_comp_params {
Cheng Chen8263f802017-11-14 15:50:00 -0800557 int use_jnt_comp_avg;
Cheng Chenf78632e2017-10-20 15:30:51 -0700558 int fwd_offset;
559 int bck_offset;
560} JNT_COMP_PARAMS;
561#endif // CONFIG_JNT_COMP
562
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563typedef struct macroblockd {
564 struct macroblockd_plane plane[MAX_MB_PLANE];
565 uint8_t bmode_blocks_wl;
566 uint8_t bmode_blocks_hl;
567
568 FRAME_COUNTS *counts;
569 TileInfo tile;
570
571 int mi_stride;
572
573 MODE_INFO **mi;
574 MODE_INFO *left_mi;
575 MODE_INFO *above_mi;
576 MB_MODE_INFO *left_mbmi;
577 MB_MODE_INFO *above_mbmi;
578
579 int up_available;
580 int left_available;
Jingning Han3da18d62017-05-02 12:43:58 -0700581 int chroma_up_available;
582 int chroma_left_available;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583
Rupert Swarbrick57486c52017-08-14 10:53:49 +0100584 /* Distance of MB away from frame edges in subpixels (1/8th pixel) */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585 int mb_to_left_edge;
586 int mb_to_right_edge;
587 int mb_to_top_edge;
588 int mb_to_bottom_edge;
589
590 FRAME_CONTEXT *fc;
591
592 /* pointers to reference frames */
Urvang Joshi52648442016-10-13 17:27:51 -0700593 const RefBuffer *block_refs[2];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594
595 /* pointer to current frame */
596 const YV12_BUFFER_CONFIG *cur_buf;
597
598 ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
599 ENTROPY_CONTEXT left_context[MAX_MB_PLANE][2 * MAX_MIB_SIZE];
600
601 PARTITION_CONTEXT *above_seg_context;
602 PARTITION_CONTEXT left_seg_context[MAX_MIB_SIZE];
603
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 TXFM_CONTEXT *above_txfm_context;
605 TXFM_CONTEXT *left_txfm_context;
Jingning Han331662e2017-05-30 17:03:32 -0700606 TXFM_CONTEXT left_txfm_context_buffer[2 * MAX_MIB_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607
Rupert Swarbrick6c545212017-09-01 17:17:25 +0100608#if CONFIG_LOOP_RESTORATION
609 WienerInfo wiener_info[MAX_MB_PLANE];
610 SgrprojInfo sgrproj_info[MAX_MB_PLANE];
611#endif // CONFIG_LOOP_RESTORATION
612
Jingning Hanff6ee6a2016-12-07 09:55:21 -0800613 // block dimension in the unit of mode_info.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614 uint8_t n8_w, n8_h;
615
Yaowu Xuc27fc142016-08-22 16:08:15 -0700616 uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
617 CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
618 uint8_t is_sec_rect;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619
Thomas Daviesf77d4ad2017-01-10 18:55:42 +0000620 FRAME_CONTEXT *tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 /* Bit depth: 8, 10, 12 */
622 int bd;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700624 int qindex[MAX_SEGMENTS];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 int lossless[MAX_SEGMENTS];
626 int corrupted;
RogerZhou3b635242017-09-19 10:06:46 -0700627#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -0700628 int cur_frame_force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -0700629// same with that in AV1_COMMON
630#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700631 struct aom_internal_error_info *error_info;
Luc Trudeauf3bf8b12017-12-08 14:38:41 -0500632 const WarpedMotionParams *global_motion;
Arild Fuldseth07441162016-08-15 15:07:52 +0200633 int prev_qindex;
634 int delta_qindex;
635 int current_qindex;
Fangwen Fu231fe422017-04-24 17:52:29 -0700636#if CONFIG_EXT_DELTA_Q
637 // Since actual frame level loop filtering level value is not available
638 // at the beginning of the tile (only available during actual filtering)
639 // at encoder side.we record the delta_lf (against the frame level loop
640 // filtering level) and code the delta between previous superblock's delta
641 // lf and current delta lf. It is equivalent to the delta between previous
642 // superblock's actual lf and current lf.
643 int prev_delta_lf_from_base;
644 int current_delta_lf_from_base;
Cheng Chena97394f2017-09-27 15:05:14 -0700645#if CONFIG_LOOPFILTER_LEVEL
646 // For this experiment, we have four frame filter levels for different plane
647 // and direction. So, to support the per superblock update, we need to add
648 // a few more params as below.
649 // 0: delta loop filter level for y plane vertical
650 // 1: delta loop filter level for y plane horizontal
651 // 2: delta loop filter level for u plane
652 // 3: delta loop filter level for v plane
653 // To make it consistent with the reference to each filter level in segment,
654 // we need to -1, since
655 // SEG_LVL_ALT_LF_Y_V = 1;
656 // SEG_LVL_ALT_LF_Y_H = 2;
657 // SEG_LVL_ALT_LF_U = 3;
658 // SEG_LVL_ALT_LF_V = 4;
659 int prev_delta_lf[FRAME_LF_COUNT];
660 int curr_delta_lf[FRAME_LF_COUNT];
661#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu231fe422017-04-24 17:52:29 -0700662#endif
Luc Trudeauf8164152017-04-11 16:20:51 -0400663
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000664 DECLARE_ALIGNED(16, uint8_t, seg_mask[2 * MAX_SB_SQUARE]);
Sarah Parker2d0e9b72017-05-04 01:34:16 +0000665
Luc Trudeauf8164152017-04-11 16:20:51 -0400666#if CONFIG_CFL
Luc Trudeau1e84af52017-11-25 15:00:28 -0500667 CFL_CTX cfl;
Luc Trudeauf8164152017-04-11 16:20:51 -0400668#endif
Cheng Chenf78632e2017-10-20 15:30:51 -0700669
670#if CONFIG_JNT_COMP
671 JNT_COMP_PARAMS jcp_param;
672#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673} MACROBLOCKD;
674
Yi Luo51281092017-06-26 16:36:15 -0700675static INLINE int get_bitdepth_data_path_index(const MACROBLOCKD *xd) {
676 return xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH ? 1 : 0;
677}
678
Yaowu Xuc27fc142016-08-22 16:08:15 -0700679static INLINE BLOCK_SIZE get_subsize(BLOCK_SIZE bsize,
680 PARTITION_TYPE partition) {
681 if (partition == PARTITION_INVALID)
Urvang Joshicb586f32016-09-20 11:36:33 -0700682 return BLOCK_INVALID;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 else
684 return subsize_lookup[partition][bsize];
685}
686
Luc Trudeau9cea9932017-12-13 21:14:13 -0500687static TX_TYPE intra_mode_to_tx_type_context(const MB_MODE_INFO *mbmi,
688 PLANE_TYPE plane_type) {
689 static const TX_TYPE _intra_mode_to_tx_type_context[INTRA_MODES] = {
690 DCT_DCT, // DC
691 ADST_DCT, // V
692 DCT_ADST, // H
693 DCT_DCT, // D45
694 ADST_ADST, // D135
695 ADST_DCT, // D117
696 DCT_ADST, // D153
697 DCT_ADST, // D207
698 ADST_DCT, // D63
699 ADST_ADST, // SMOOTH
700 ADST_DCT, // SMOOTH_V
701 DCT_ADST, // SMOOTH_H
702 ADST_ADST, // PAETH
703 };
704 return plane_type == PLANE_TYPE_Y
705 ? _intra_mode_to_tx_type_context[mbmi->mode]
706 : _intra_mode_to_tx_type_context[get_uv_mode(mbmi->uv_mode)];
707}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708
Jingning Hanb83e64b2017-03-01 14:52:04 -0800709#define USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4 1
Debargha Mukherjee5a488a62016-11-22 22:24:10 -0800710
Sarah Parker076437f2017-03-14 17:39:53 -0700711static INLINE int is_rect_tx(TX_SIZE tx_size) { return tx_size >= TX_SIZES; }
Sarah Parker076437f2017-03-14 17:39:53 -0700712
Rupert Swarbrickfcff0b22017-10-05 09:26:04 +0100713static INLINE int block_signals_txsize(BLOCK_SIZE bsize) {
Rupert Swarbrickfcff0b22017-10-05 09:26:04 +0100714 return bsize > BLOCK_4X4;
Rupert Swarbrickfcff0b22017-10-05 09:26:04 +0100715}
716
Hui Suddbcde22017-09-18 17:22:02 -0700717// Number of transform types in each set type
718static const int av1_num_ext_tx_set[EXT_TX_SET_TYPES] = {
Sarah Parkercec7ba12017-11-03 16:46:09 -0700719 1, 2, 5, 7, 7, 10, 12, 16, 16,
Hui Suddbcde22017-09-18 17:22:02 -0700720};
721
Hui Suddbcde22017-09-18 17:22:02 -0700722static const int av1_ext_tx_used[EXT_TX_SET_TYPES][TX_TYPES] = {
723 {
724 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
725 },
726 {
727 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
728 },
729 {
730 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
731 },
732 {
733 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
734 },
735 {
Sarah Parkercec7ba12017-11-03 16:46:09 -0700736 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
737 },
738 {
Sarah Parker9d8222b2017-11-19 19:08:55 -0800739 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
740 },
741 {
Hui Suddbcde22017-09-18 17:22:02 -0700742 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
743 },
744 {
745 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
746 },
Sarah Parkercec7ba12017-11-03 16:46:09 -0700747 {
748 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
749 },
Debargha Mukherjee08542b92017-02-21 01:08:14 -0800750};
751
752static INLINE TxSetType get_ext_tx_set_type(TX_SIZE tx_size, BLOCK_SIZE bs,
Sarah Parker5effe3f2017-02-23 12:49:10 -0800753 int is_inter, int use_reduced_set) {
Yue Chen56e226e2017-05-02 16:21:40 -0700754 const TX_SIZE tx_size_sqr_up = txsize_sqr_up_map[tx_size];
755 const TX_SIZE tx_size_sqr = txsize_sqr_map[tx_size];
Debargha Mukherjee6ea917e2017-10-19 09:31:29 -0700756#if USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
Jingning Han1a00cff2016-12-28 14:53:14 -0800757 (void)bs;
Debargha Mukherjee570423c2017-10-01 00:35:20 -0700758 if (tx_size_sqr_up > TX_32X32) return EXT_TX_SET_DCTONLY;
Jingning Han1a00cff2016-12-28 14:53:14 -0800759#else
Debargha Mukherjee570423c2017-10-01 00:35:20 -0700760 if (tx_size_sqr_up > TX_32X32 || bs < BLOCK_8X8) return EXT_TX_SET_DCTONLY;
Debargha Mukherjee6ea917e2017-10-19 09:31:29 -0700761#endif // USE_TXTYPE_SEARCH_FOR_SUB8X8_IN_CB4X4
Sarah Parker5effe3f2017-02-23 12:49:10 -0800762 if (use_reduced_set)
763 return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DTT4_IDTX;
Nathan E. Eggef621a4f2017-12-08 09:02:09 -0500764#if CONFIG_DAALA_TX_DST32
Nathan E. Eggef73e47e2017-10-22 06:41:55 -0400765 if (tx_size_sqr_up > TX_32X32)
766 return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DCTONLY;
767 if (is_inter)
768 return (tx_size_sqr >= TX_16X16 ? EXT_TX_SET_DTT9_IDTX_1DDCT
769 : EXT_TX_SET_ALL16);
770 else
771 return (tx_size_sqr >= TX_16X16 ? EXT_TX_SET_DTT4_IDTX
772 : EXT_TX_SET_DTT4_IDTX_1DDCT);
773#endif
Yue Chen56e226e2017-05-02 16:21:40 -0700774 if (tx_size_sqr_up == TX_32X32)
Debargha Mukherjee08542b92017-02-21 01:08:14 -0800775 return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DCTONLY;
776 if (is_inter)
Sarah Parkercec7ba12017-11-03 16:46:09 -0700777 return (tx_size_sqr == TX_16X16 ?
778#if EXT_TX_16X16_SET == 0
779 EXT_TX_SET_ALL16_16X16
780#elif EXT_TX_16X16_SET == 1
781 EXT_TX_SET_DTT9_IDTX_1DDCT
Sarah Parker9d8222b2017-11-19 19:08:55 -0800782#else
Sarah Parkercec7ba12017-11-03 16:46:09 -0700783 EXT_TX_SET_DTT9_IDTX
784#endif // EXT_TX_16X16_SET
785 : EXT_TX_SET_ALL16);
Debargha Mukherjee08542b92017-02-21 01:08:14 -0800786 else
Sarah Parkercec7ba12017-11-03 16:46:09 -0700787 return (tx_size_sqr == TX_16X16 ?
788#if EXT_TX_16X16_SET == 0
789 EXT_TX_SET_DTT4_IDTX_1DDCT_16X16
790#else
791 EXT_TX_SET_DTT4_IDTX
792#endif // EXT_TX_16X16_SET
Yue Chen56e226e2017-05-02 16:21:40 -0700793 : EXT_TX_SET_DTT4_IDTX_1DDCT);
Debargha Mukherjee08542b92017-02-21 01:08:14 -0800794}
795
Hui Su4a9be2a2017-09-19 14:41:49 -0700796// Maps tx set types to the indices.
797static const int ext_tx_set_index[2][EXT_TX_SET_TYPES] = {
798 {
799 // Intra
Sarah Parkercec7ba12017-11-03 16:46:09 -0700800 0, -1,
801#if EXT_TX_16X16_SET == 0
Sarah Parker9d8222b2017-11-19 19:08:55 -0800802 -1, 2,
803#else
804 2, -1,
Sarah Parkercec7ba12017-11-03 16:46:09 -0700805#endif // EXT_TX_16X16_SET
806 1, -1, -1, -1, -1,
807 },
808 {
809 // Inter
810 0, 3, -1, -1, -1,
811#if EXT_TX_16X16_SET == 0
812 -1, -1, 2,
813#elif EXT_TX_16X16_SET == 1
814 -1, 2, -1,
815#else
816 2, -1, -1,
817#endif // EXT_TX_16X16_SET
Sarah Parker9d8222b2017-11-19 19:08:55 -0800818 1,
Hui Su4a9be2a2017-09-19 14:41:49 -0700819 },
820};
821
Debargha Mukherjee08542b92017-02-21 01:08:14 -0800822static INLINE int get_ext_tx_set(TX_SIZE tx_size, BLOCK_SIZE bs, int is_inter,
Sarah Parker5effe3f2017-02-23 12:49:10 -0800823 int use_reduced_set) {
Debargha Mukherjee08542b92017-02-21 01:08:14 -0800824 const TxSetType set_type =
Sarah Parker5effe3f2017-02-23 12:49:10 -0800825 get_ext_tx_set_type(tx_size, bs, is_inter, use_reduced_set);
Hui Su4a9be2a2017-09-19 14:41:49 -0700826 return ext_tx_set_index[is_inter][set_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700827}
828
Sarah Parkere68a3e42017-02-16 14:03:24 -0800829static INLINE int get_ext_tx_types(TX_SIZE tx_size, BLOCK_SIZE bs, int is_inter,
Sarah Parker5effe3f2017-02-23 12:49:10 -0800830 int use_reduced_set) {
831 const int set_type =
832 get_ext_tx_set_type(tx_size, bs, is_inter, use_reduced_set);
Hui Suddbcde22017-09-18 17:22:02 -0700833 return av1_num_ext_tx_set[set_type];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700834}
835
Debargha Mukherjee3ebb0d02017-12-14 05:05:18 -0800836#define TXSIZEMAX(t1, t2) (tx_size_2d[(t1)] >= tx_size_2d[(t2)] ? (t1) : (t2))
837#define TXSIZEMIN(t1, t2) (tx_size_2d[(t1)] <= tx_size_2d[(t2)] ? (t1) : (t2))
838
Yue Chen0797a202017-10-27 17:24:56 -0700839static INLINE TX_SIZE get_max_rect_tx_size(BLOCK_SIZE bsize, int is_inter) {
Debargha Mukherjeee4e18fc2017-12-06 23:43:24 -0800840 return max_txsize_rect_lookup[is_inter][bsize];
Yue Chen0797a202017-10-27 17:24:56 -0700841}
842
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843static INLINE TX_SIZE tx_size_from_tx_mode(BLOCK_SIZE bsize, TX_MODE tx_mode,
844 int is_inter) {
845 const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
Yue Chen0797a202017-10-27 17:24:56 -0700846 const TX_SIZE max_rect_tx_size = get_max_rect_tx_size(bsize, is_inter);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800847 (void)is_inter;
Urvang Joshifeb925f2016-12-05 10:37:29 -0800848 if (bsize == BLOCK_4X4)
849 return AOMMIN(max_txsize_lookup[bsize], largest_tx_size);
Urvang Joshifeb925f2016-12-05 10:37:29 -0800850 if (txsize_sqr_map[max_rect_tx_size] <= largest_tx_size)
851 return max_rect_tx_size;
852 else
853 return largest_tx_size;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854}
855
hui su5db97432016-10-14 16:10:14 -0700856extern const int16_t dr_intra_derivative[90];
hui su02c79742017-05-16 17:19:04 -0700857static const uint8_t mode_to_angle_map[] = {
Urvang Joshib7301cd2017-11-09 15:08:56 -0800858 0, 90, 180, 45, 135, 113, 157, 203, 67, 0, 0, 0, 0,
hui su5db97432016-10-14 16:10:14 -0700859};
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860
Yue Chen1a5ab9f2017-12-14 14:53:51 -0800861#if CONFIG_FILTER_INTRA
Yue Chen1a5ab9f2017-12-14 14:53:51 -0800862static INLINE int av1_filter_intra_allowed_txsize(TX_SIZE tx) {
Yue Chen1a5ab9f2017-12-14 14:53:51 -0800863 if (tx == TX_INVALID) return 0;
864
Yue Chen1a5ab9f2017-12-14 14:53:51 -0800865 return tx_size_wide[tx] <= 32 && tx_size_high[tx] <= 32;
Yue Chen1a5ab9f2017-12-14 14:53:51 -0800866}
867
868static INLINE TX_SIZE av1_max_tx_size_for_filter_intra(BLOCK_SIZE bsize,
869 TX_MODE tx_mode) {
870 const TX_SIZE max_tx_size = tx_size_from_tx_mode(bsize, tx_mode, 0);
871
872 if (tx_mode != TX_MODE_SELECT) return max_tx_size;
873
874 int depth = 0;
875 TX_SIZE tx_size = max_tx_size;
876 while (depth < MAX_TX_DEPTH && tx_size != TX_4X4) {
877 if (av1_filter_intra_allowed_txsize(tx_size)) return tx_size;
878 depth++;
879 tx_size = sub_tx_size_map[0][tx_size];
880 }
881 return TX_INVALID;
882}
883#endif
884
Angie Chiang752ccce2017-04-09 13:41:13 -0700885// Converts block_index for given transform size to index of the block in raster
886// order.
887static INLINE int av1_block_index_to_raster_order(TX_SIZE tx_size,
888 int block_idx) {
889 // For transform size 4x8, the possible block_idx values are 0 & 2, because
890 // block_idx values are incremented in steps of size 'tx_width_unit x
891 // tx_height_unit'. But, for this transform size, block_idx = 2 corresponds to
892 // block number 1 in raster order, inside an 8x8 MI block.
893 // For any other transform size, the two indices are equivalent.
894 return (tx_size == TX_4X8 && block_idx == 2) ? 1 : block_idx;
895}
896
897// Inverse of above function.
898// Note: only implemented for transform sizes 4x4, 4x8 and 8x4 right now.
899static INLINE int av1_raster_order_to_block_index(TX_SIZE tx_size,
900 int raster_order) {
901 assert(tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4);
902 // We ensure that block indices are 0 & 2 if tx size is 4x8 or 8x4.
903 return (tx_size == TX_4X4) ? raster_order : (raster_order > 0) ? 2 : 0;
904}
905
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906static INLINE TX_TYPE get_default_tx_type(PLANE_TYPE plane_type,
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500907 const MACROBLOCKD *xd,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 TX_SIZE tx_size) {
909 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
910
Hui Su7448fc22018-01-11 16:47:45 -0800911 if (is_inter_block(mbmi) || plane_type != PLANE_TYPE_Y ||
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912 xd->lossless[mbmi->segment_id] || tx_size >= TX_32X32)
913 return DCT_DCT;
914
Luc Trudeau9cea9932017-12-13 21:14:13 -0500915 return intra_mode_to_tx_type_context(mbmi, plane_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916}
917
Sarah Parker90024e42017-10-06 16:50:47 -0700918static INLINE BLOCK_SIZE
919get_plane_block_size(BLOCK_SIZE bsize, const struct macroblockd_plane *pd) {
920 return ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
921}
922
hui su45b64752017-07-12 16:54:35 -0700923static INLINE TX_TYPE av1_get_tx_type(PLANE_TYPE plane_type,
Jingning Han19b5c8f2017-07-06 15:10:12 -0700924 const MACROBLOCKD *xd, int blk_row,
Luc Trudeau2eb9b842017-12-13 11:19:16 -0500925 int blk_col, TX_SIZE tx_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926 const MODE_INFO *const mi = xd->mi[0];
927 const MB_MODE_INFO *const mbmi = &mi->mbmi;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700928 (void)blk_row;
929 (void)blk_col;
Sebastien Alaiwan3bac9922017-11-02 12:34:41 +0100930#if CONFIG_INTRABC && (CONFIG_TXK_SEL)
Alex Conversedaa15e42017-05-02 14:27:16 -0700931 // TODO(aconverse@google.com): Handle INTRABC + EXT_TX + TXK_SEL
Alex Converse28744302017-04-13 14:46:22 -0700932 if (is_intrabc_block(mbmi)) return DCT_DCT;
Sebastien Alaiwan3bac9922017-11-02 12:34:41 +0100933#endif // CONFIG_INTRABC && (CONFIG_TXK_SEL)
hui su45b64752017-07-12 16:54:35 -0700934
Sarah Parker90024e42017-10-06 16:50:47 -0700935 const struct macroblockd_plane *const pd = &xd->plane[plane_type];
936 const BLOCK_SIZE plane_bsize = get_plane_block_size(mbmi->sb_type, pd);
Sarah Parker90024e42017-10-06 16:50:47 -0700937 const TxSetType tx_set_type =
938 get_ext_tx_set_type(tx_size, plane_bsize, is_inter_block(mbmi), 0);
Sarah Parker90024e42017-10-06 16:50:47 -0700939
hui su45b64752017-07-12 16:54:35 -0700940#if CONFIG_TXK_SEL
941 TX_TYPE tx_type;
Jingning Han19b5c8f2017-07-06 15:10:12 -0700942 if (xd->lossless[mbmi->segment_id] || txsize_sqr_map[tx_size] >= TX_32X32) {
hui su45b64752017-07-12 16:54:35 -0700943 tx_type = DCT_DCT;
944 } else {
Angie Chiangbce07f12017-12-01 16:34:31 -0800945 if (plane_type == PLANE_TYPE_Y) {
946 tx_type = mbmi->txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col];
947 } else if (is_inter_block(mbmi)) {
948 // scale back to y plane's coordinate
949 blk_row <<= pd->subsampling_y;
950 blk_col <<= pd->subsampling_x;
951 tx_type = mbmi->txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col];
952 } else {
953 // In intra mode, uv planes don't share the same prediction mode as y
954 // plane, so the tx_type should not be shared
Luc Trudeau9cea9932017-12-13 21:14:13 -0500955 tx_type = intra_mode_to_tx_type_context(mbmi, PLANE_TYPE_UV);
Angie Chiangbce07f12017-12-01 16:34:31 -0800956 }
hui su45b64752017-07-12 16:54:35 -0700957 }
Angie Chiang840c66e2017-12-11 16:43:38 -0800958 assert(tx_type < TX_TYPES);
Angie Chiang2ac18682017-12-01 18:02:59 -0800959 if (!av1_ext_tx_used[tx_set_type][tx_type]) return DCT_DCT;
hui su45b64752017-07-12 16:54:35 -0700960 return tx_type;
961#endif // CONFIG_TXK_SEL
962
Jingning Handa6df482018-01-07 07:39:31 -0800963 // TODO(sarahparker) This assumes reduced_tx_set_used == 0. I will do a
964 // follow up refactor to make the actual value of reduced_tx_set_used
965 // within this function.
966 if (is_inter_block(mbmi) && !av1_ext_tx_used[tx_set_type][mbmi->tx_type])
967 return DCT_DCT;
968
Nathan E. Eggef621a4f2017-12-08 09:02:09 -0500969#if CONFIG_DAALA_TX_DST32
Nathan E. Egge856d1792017-10-26 17:55:20 -0400970 if (xd->lossless[mbmi->segment_id] || txsize_sqr_map[tx_size] > TX_32X32)
971#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 if (xd->lossless[mbmi->segment_id] || txsize_sqr_map[tx_size] > TX_32X32 ||
973 (txsize_sqr_map[tx_size] >= TX_32X32 && !is_inter_block(mbmi)))
Nathan E. Egge856d1792017-10-26 17:55:20 -0400974#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 return DCT_DCT;
Debargha Mukherjee6ea917e2017-10-19 09:31:29 -0700976 if (plane_type == PLANE_TYPE_Y) {
Urvang Joshi978152a2018-01-09 12:59:25 -0800977 return mbmi->tx_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978 }
979
Debargha Mukherjee6ea917e2017-10-19 09:31:29 -0700980 if (is_inter_block(mbmi)) {
981 // UV Inter only
982 return (mbmi->tx_type == IDTX && txsize_sqr_map[tx_size] >= TX_32X32)
983 ? DCT_DCT
984 : mbmi->tx_type;
985 }
986
Sarah Parker90024e42017-10-06 16:50:47 -0700987 // UV Intra only
Luc Trudeau9cea9932017-12-13 21:14:13 -0500988 const TX_TYPE intra_type = intra_mode_to_tx_type_context(mbmi, PLANE_TYPE_UV);
Sarah Parker90024e42017-10-06 16:50:47 -0700989 if (!av1_ext_tx_used[tx_set_type][intra_type]) return DCT_DCT;
990 return intra_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991}
992
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000993void av1_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y,
994 const int num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995
Debargha Mukherjee0fa057f2017-12-06 17:06:29 -0800996static INLINE int bsize_to_max_depth(BLOCK_SIZE bsize, int is_inter) {
997 TX_SIZE tx_size = get_max_rect_tx_size(bsize, is_inter);
998 int depth = 0;
999 while (depth < MAX_TX_DEPTH && tx_size != TX_4X4) {
1000 depth++;
Debargha Mukherjeee4e18fc2017-12-06 23:43:24 -08001001 tx_size = sub_tx_size_map[is_inter][tx_size];
Debargha Mukherjee0fa057f2017-12-06 17:06:29 -08001002 }
1003 return depth;
Jingning Han4e1737a2016-10-25 16:05:02 -07001004}
1005
Debargha Mukherjeee4e18fc2017-12-06 23:43:24 -08001006static INLINE int bsize_to_tx_size_cat(BLOCK_SIZE bsize, int is_inter) {
1007 TX_SIZE tx_size = get_max_rect_tx_size(bsize, is_inter);
1008 assert(tx_size != TX_4X4);
1009 int depth = 0;
1010 while (tx_size != TX_4X4) {
1011 depth++;
1012 tx_size = sub_tx_size_map[is_inter][tx_size];
1013 assert(depth < 10);
1014 }
1015 assert(depth <= MAX_TX_CATS);
1016 return depth - 1;
1017}
1018
Debargha Mukherjee0fa057f2017-12-06 17:06:29 -08001019static INLINE TX_SIZE depth_to_tx_size(int depth, BLOCK_SIZE bsize,
1020 int is_inter) {
1021 TX_SIZE max_tx_size = get_max_rect_tx_size(bsize, is_inter);
1022 TX_SIZE tx_size = max_tx_size;
Debargha Mukherjeee4e18fc2017-12-06 23:43:24 -08001023 for (int d = 0; d < depth; ++d) tx_size = sub_tx_size_map[is_inter][tx_size];
Debargha Mukherjee0fa057f2017-12-06 17:06:29 -08001024 return tx_size;
Jingning Han4e1737a2016-10-25 16:05:02 -07001025}
1026
Debargha Mukherjee80592c72017-12-16 08:23:34 -08001027static INLINE TX_SIZE av1_get_max_uv_txsize(BLOCK_SIZE bsize, int is_inter,
1028 int ss_x, int ss_y) {
1029 const BLOCK_SIZE plane_bsize = ss_size_lookup[bsize][ss_x][ss_y];
1030 assert(plane_bsize < BLOCK_SIZES_ALL);
1031 TX_SIZE uv_tx = max_txsize_rect_lookup[is_inter][plane_bsize];
1032#if CONFIG_TX64X64
1033 switch (uv_tx) {
1034 case TX_64X64:
1035 case TX_64X32:
1036 case TX_32X64: return TX_32X32;
1037 case TX_64X16: return TX_32X16;
1038 case TX_16X64: return TX_16X32;
1039 default: break;
Luc Trudeaud1941f32017-11-22 14:17:21 -05001040 }
Debargha Mukherjee80592c72017-12-16 08:23:34 -08001041#endif // CONFIG_TX64X64
1042 return uv_tx;
1043}
1044
1045static INLINE TX_SIZE av1_get_uv_tx_size(const MB_MODE_INFO *mbmi, int ss_x,
1046 int ss_y) {
Debargha Mukherjee6396d3a2017-12-21 00:12:38 -08001047 return av1_get_max_uv_txsize(mbmi->sb_type, is_inter_block(mbmi), ss_x, ss_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001048}
1049
hui su0c6244b2017-07-12 17:11:43 -07001050static INLINE TX_SIZE av1_get_tx_size(int plane, const MACROBLOCKD *xd) {
Angie Chiang80b82262017-02-24 11:39:47 -08001051 const MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Debargha Mukherjee80592c72017-12-16 08:23:34 -08001052 if (xd->lossless[mbmi->segment_id]) return TX_4X4;
hui su0c6244b2017-07-12 17:11:43 -07001053 if (plane == 0) return mbmi->tx_size;
Angie Chiang80b82262017-02-24 11:39:47 -08001054 const MACROBLOCKD_PLANE *pd = &xd->plane[plane];
Debargha Mukherjee80592c72017-12-16 08:23:34 -08001055 return av1_get_uv_tx_size(mbmi, pd->subsampling_x, pd->subsampling_y);
Angie Chiang80b82262017-02-24 11:39:47 -08001056}
1057
Timothy B. Terriberrya2d5cde2017-05-10 18:33:50 -07001058void av1_reset_skip_context(MACROBLOCKD *xd, int mi_row, int mi_col,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001059 BLOCK_SIZE bsize, const int num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001060
Rupert Swarbrick76405202017-11-07 16:35:55 +00001061#if CONFIG_LOOP_RESTORATION
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001062void av1_reset_loop_restoration(MACROBLOCKD *xd, const int num_planes);
Rupert Swarbrick76405202017-11-07 16:35:55 +00001063#endif // CONFIG_LOOP_RESTORATION
1064
Yaowu Xuc27fc142016-08-22 16:08:15 -07001065typedef void (*foreach_transformed_block_visitor)(int plane, int block,
1066 int blk_row, int blk_col,
1067 BLOCK_SIZE plane_bsize,
1068 TX_SIZE tx_size, void *arg);
1069
Yaowu Xuf883b422016-08-30 14:01:10 -07001070void av1_foreach_transformed_block_in_plane(
Yaowu Xuc27fc142016-08-22 16:08:15 -07001071 const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
1072 foreach_transformed_block_visitor visit, void *arg);
1073
Angie Chiang0397eda2017-03-15 16:57:14 -07001074#if CONFIG_LV_MAP
1075void av1_foreach_transformed_block(const MACROBLOCKD *const xd,
Jingning Han94652b82017-04-04 09:45:02 -07001076 BLOCK_SIZE bsize, int mi_row, int mi_col,
Angie Chiang0397eda2017-03-15 16:57:14 -07001077 foreach_transformed_block_visitor visit,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001078 void *arg, const int num_planes);
Angie Chiang0397eda2017-03-15 16:57:14 -07001079#endif
1080
Yaowu Xuf883b422016-08-30 14:01:10 -07001081void av1_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
Jingning Haneee43152016-12-05 09:58:45 -08001082 int plane, TX_SIZE tx_size, int has_eob, int aoff,
1083 int loff);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001084
Yue Chenb8aa3992017-12-13 15:27:52 -08001085#define MAX_INTERINTRA_SB_SQUARE 32 * 32
Yaowu Xuc27fc142016-08-22 16:08:15 -07001086static INLINE int is_interintra_allowed_bsize(const BLOCK_SIZE bsize) {
Yue Chen9ddd4092017-10-30 16:13:39 -07001087 return (bsize >= BLOCK_8X8) && (bsize <= BLOCK_32X32);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001088}
1089
1090static INLINE int is_interintra_allowed_mode(const PREDICTION_MODE mode) {
1091 return (mode >= NEARESTMV) && (mode <= NEWMV);
1092}
1093
1094static INLINE int is_interintra_allowed_ref(const MV_REFERENCE_FRAME rf[2]) {
1095 return (rf[0] > INTRA_FRAME) && (rf[1] <= INTRA_FRAME);
1096}
1097
1098static INLINE int is_interintra_allowed(const MB_MODE_INFO *mbmi) {
1099 return is_interintra_allowed_bsize(mbmi->sb_type) &&
1100 is_interintra_allowed_mode(mbmi->mode) &&
1101 is_interintra_allowed_ref(mbmi->ref_frame);
1102}
1103
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001104static INLINE int is_interintra_allowed_bsize_group(int group) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001105 int i;
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001106 for (i = 0; i < BLOCK_SIZES_ALL; i++) {
Urvang Joshicb586f32016-09-20 11:36:33 -07001107 if (size_group_lookup[i] == group &&
1108 is_interintra_allowed_bsize((BLOCK_SIZE)i)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001109 return 1;
Urvang Joshicb586f32016-09-20 11:36:33 -07001110 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 }
1112 return 0;
1113}
1114
1115static INLINE int is_interintra_pred(const MB_MODE_INFO *mbmi) {
Yue Chenb8aa3992017-12-13 15:27:52 -08001116 return mbmi->ref_frame[0] > INTRA_FRAME &&
1117 mbmi->ref_frame[1] == INTRA_FRAME && is_interintra_allowed(mbmi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001118}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001119
Debargha Mukherjee891a8772017-11-22 10:09:37 -08001120static INLINE int get_vartx_max_txsize(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1121 int subsampled) {
1122 TX_SIZE max_txsize =
1123 xd->lossless[xd->mi[0]->mbmi.segment_id]
1124 ? TX_4X4
1125 : get_max_rect_tx_size(bsize, is_inter_block(&xd->mi[0]->mbmi));
Rupert Swarbrick4e7b7d62017-09-28 17:30:44 +01001126
1127#if CONFIG_EXT_PARTITION && CONFIG_TX64X64
1128 // The decoder is designed so that it can process 64x64 luma pixels at a
1129 // time. If this is a chroma plane with subsampling and bsize corresponds to
1130 // a subsampled BLOCK_128X128 then the lookup above will give TX_64X64. That
1131 // mustn't be used for the subsampled plane (because it would be bigger than
1132 // a 64x64 luma block) so we round down to TX_32X32.
Debargha Mukherjee1fa24462017-10-19 14:33:39 -07001133 if (subsampled && txsize_sqr_up_map[max_txsize] == TX_64X64) {
Debargha Mukherjee1a8664e2017-12-20 15:54:49 -08001134 if (max_txsize == TX_16X64)
1135 max_txsize = TX_16X32;
1136 else if (max_txsize == TX_64X16)
1137 max_txsize = TX_32X16;
1138 else
1139 max_txsize = TX_32X32;
Debargha Mukherjee1fa24462017-10-19 14:33:39 -07001140 }
Rupert Swarbrick4e7b7d62017-09-28 17:30:44 +01001141#else
1142 (void)subsampled;
1143#endif
1144
1145 return max_txsize;
Sarah Parker106b3cb2017-04-21 12:13:37 -07001146}
Sarah Parker106b3cb2017-04-21 12:13:37 -07001147
Yue Chencb60b182016-10-13 15:18:22 -07001148static INLINE int is_motion_variation_allowed_bsize(BLOCK_SIZE bsize) {
Debargha Mukherjeec17a4432017-08-28 22:22:45 -07001149 return AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
Yue Chencb60b182016-10-13 15:18:22 -07001150}
1151
Yue Chen8636da62017-04-03 01:23:44 -07001152static INLINE int is_motion_variation_allowed_compound(
1153 const MB_MODE_INFO *mbmi) {
1154 if (!has_second_ref(mbmi))
1155 return 1;
1156 else
1157 return 0;
1158}
1159
Yue Chen1bd42be2017-03-15 18:07:04 -07001160// input: log2 of length, 0(4), 1(8), ...
1161static const int max_neighbor_obmc[6] = { 0, 1, 2, 3, 4, 4 };
1162
Yue Chen5329a2b2017-02-28 17:33:00 +08001163static INLINE int check_num_overlappable_neighbors(const MB_MODE_INFO *mbmi) {
Yue Chen1bd42be2017-03-15 18:07:04 -07001164 return !(mbmi->overlappable_neighbors[0] == 0 &&
1165 mbmi->overlappable_neighbors[1] == 0);
Yue Chen5329a2b2017-02-28 17:33:00 +08001166}
Yue Chen5329a2b2017-02-28 17:33:00 +08001167
Sebastien Alaiwan48795802017-10-30 12:07:13 +01001168static INLINE MOTION_MODE
Luc Trudeau2eb9b842017-12-13 11:19:16 -05001169motion_mode_allowed(const WarpedMotionParams *gm_params, const MACROBLOCKD *xd,
1170 const MODE_INFO *mi) {
Sarah Parker19234cc2017-03-10 16:43:25 -08001171 const MB_MODE_INFO *mbmi = &mi->mbmi;
RogerZhou3b635242017-09-19 10:06:46 -07001172#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07001173 if (xd->cur_frame_force_integer_mv == 0) {
RogerZhou3b635242017-09-19 10:06:46 -07001174#endif
RogerZhou3b635242017-09-19 10:06:46 -07001175 const TransformationType gm_type = gm_params[mbmi->ref_frame[0]].wmtype;
Luc Trudeau2eb9b842017-12-13 11:19:16 -05001176 if (is_global_mv_block(mi, gm_type)) return SIMPLE_TRANSLATION;
RogerZhou3b635242017-09-19 10:06:46 -07001177#if CONFIG_AMVR
1178 }
1179#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001180 if (is_motion_variation_allowed_bsize(mbmi->sb_type) &&
Yue Chen8636da62017-04-03 01:23:44 -07001181 is_inter_mode(mbmi->mode) && mbmi->ref_frame[1] != INTRA_FRAME &&
1182 is_motion_variation_allowed_compound(mbmi)) {
Yue Chen5329a2b2017-02-28 17:33:00 +08001183 if (!check_num_overlappable_neighbors(mbmi)) return SIMPLE_TRANSLATION;
Zoe Liu70539b12017-12-02 19:03:36 -08001184 assert(!has_second_ref(mbmi));
1185 if (mbmi->num_proj_ref[0] >= 1 &&
Wei-Ting Lin20885282017-08-28 17:18:18 -07001186 !av1_is_scaled(&(xd->block_refs[0]->sf))) {
RogerZhou3b635242017-09-19 10:06:46 -07001187#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07001188 if (xd->cur_frame_force_integer_mv) {
RogerZhou3b635242017-09-19 10:06:46 -07001189 return OBMC_CAUSAL;
1190 }
1191#endif
Yue Chen69f18e12016-09-08 14:48:15 -07001192 return WARPED_CAUSAL;
Hui Su12231bd2017-09-07 18:01:15 -07001193 }
Yue Chen80daf0c2017-11-02 17:14:18 -07001194 return OBMC_CAUSAL;
Yue Chen69f18e12016-09-08 14:48:15 -07001195 } else {
1196 return SIMPLE_TRANSLATION;
1197 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001198}
1199
Luc Trudeau2eb9b842017-12-13 11:19:16 -05001200static INLINE void assert_motion_mode_valid(MOTION_MODE mode,
Sarah Parker19234cc2017-03-10 16:43:25 -08001201 const WarpedMotionParams *gm_params,
Yue Chen52c51732017-07-11 15:08:30 -07001202 const MACROBLOCKD *xd,
Sarah Parker19234cc2017-03-10 16:43:25 -08001203 const MODE_INFO *mi) {
Sebastien Alaiwan48795802017-10-30 12:07:13 +01001204 const MOTION_MODE last_motion_mode_allowed =
Luc Trudeau2eb9b842017-12-13 11:19:16 -05001205 motion_mode_allowed(gm_params, xd, mi);
Wei-Ting Lin20885282017-08-28 17:18:18 -07001206
Sarah Parker19234cc2017-03-10 16:43:25 -08001207 // Check that the input mode is not illegal
1208 if (last_motion_mode_allowed < mode)
1209 assert(0 && "Illegal motion mode selected");
1210}
1211
Yaowu Xuc27fc142016-08-22 16:08:15 -07001212static INLINE int is_neighbor_overlappable(const MB_MODE_INFO *mbmi) {
1213 return (is_inter_block(mbmi));
1214}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001215
Hui Sue87fb232017-10-05 15:00:15 -07001216static INLINE int av1_allow_palette(int allow_screen_content_tools,
1217 BLOCK_SIZE sb_type) {
Hui Su8b618f62017-12-20 12:03:35 -08001218 return allow_screen_content_tools && block_size_wide[sb_type] <= 64 &&
1219 block_size_high[sb_type] <= 64;
Hui Sue87fb232017-10-05 15:00:15 -07001220}
1221
Urvang Joshi56ba91b2017-01-10 13:22:09 -08001222// Returns sub-sampled dimensions of the given block.
1223// The output values for 'rows_within_bounds' and 'cols_within_bounds' will
RogerZhou3b635242017-09-19 10:06:46 -07001224// differ from 'height' and 'width' when part of the block is outside the
1225// right
Urvang Joshi56ba91b2017-01-10 13:22:09 -08001226// and/or bottom image boundary.
1227static INLINE void av1_get_block_dimensions(BLOCK_SIZE bsize, int plane,
1228 const MACROBLOCKD *xd, int *width,
1229 int *height,
1230 int *rows_within_bounds,
1231 int *cols_within_bounds) {
1232 const int block_height = block_size_high[bsize];
1233 const int block_width = block_size_wide[bsize];
1234 const int block_rows = (xd->mb_to_bottom_edge >= 0)
1235 ? block_height
1236 : (xd->mb_to_bottom_edge >> 3) + block_height;
1237 const int block_cols = (xd->mb_to_right_edge >= 0)
1238 ? block_width
1239 : (xd->mb_to_right_edge >> 3) + block_width;
1240 const struct macroblockd_plane *const pd = &xd->plane[plane];
1241 assert(IMPLIES(plane == PLANE_TYPE_Y, pd->subsampling_x == 0));
1242 assert(IMPLIES(plane == PLANE_TYPE_Y, pd->subsampling_y == 0));
1243 assert(block_width >= block_cols);
1244 assert(block_height >= block_rows);
Hui Su8b618f62017-12-20 12:03:35 -08001245 const int plane_block_width = block_width >> pd->subsampling_x;
1246 const int plane_block_height = block_height >> pd->subsampling_y;
1247 // Special handling for chroma sub8x8.
1248 const int is_chroma_sub8_x = plane > 0 && plane_block_width < 4;
1249 const int is_chroma_sub8_y = plane > 0 && plane_block_height < 4;
1250 if (width) *width = plane_block_width + 2 * is_chroma_sub8_x;
1251 if (height) *height = plane_block_height + 2 * is_chroma_sub8_y;
1252 if (rows_within_bounds) {
1253 *rows_within_bounds =
1254 (block_rows >> pd->subsampling_y) + 2 * is_chroma_sub8_y;
1255 }
1256 if (cols_within_bounds) {
1257 *cols_within_bounds =
1258 (block_cols >> pd->subsampling_x) + 2 * is_chroma_sub8_x;
1259 }
Urvang Joshi56ba91b2017-01-10 13:22:09 -08001260}
1261
Sarah Parkerefd8af22017-08-25 16:36:06 -07001262/* clang-format off */
1263typedef aom_cdf_prob (*MapCdf)[PALETTE_COLOR_INDEX_CONTEXTS]
1264 [CDF_SIZE(PALETTE_COLORS)];
1265typedef const int (*ColorCost)[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
1266 [PALETTE_COLORS];
1267/* clang-format on */
1268
1269typedef struct {
1270 int rows;
1271 int cols;
1272 int n_colors;
1273 int plane_width;
1274 int plane_height;
1275 uint8_t *color_map;
1276 MapCdf map_cdf;
1277 ColorCost color_cost;
1278} Av1ColorMapParam;
1279
Yue Chen19e7aa82016-11-30 14:05:39 -08001280static INLINE int is_nontrans_global_motion(const MACROBLOCKD *xd) {
1281 const MODE_INFO *mi = xd->mi[0];
1282 const MB_MODE_INFO *const mbmi = &mi->mbmi;
1283 int ref;
Yue Chen19e7aa82016-11-30 14:05:39 -08001284
Sarah Parker2b9ec2e2017-10-30 17:34:08 -07001285 // First check if all modes are GLOBALMV
1286 if (mbmi->mode != GLOBALMV && mbmi->mode != GLOBAL_GLOBALMV) return 0;
Jingning Han3ca0e672017-04-14 19:48:05 -07001287
1288#if !GLOBAL_SUB8X8_USED
Jingning Han19d05212017-12-22 12:09:49 -08001289 if (AOMMIN(mi_size_wide[mbmi->sb_type], mi_size_high[mbmi->sb_type]) < 8)
1290 return 0;
Jingning Han3ca0e672017-04-14 19:48:05 -07001291#endif
1292
Yue Chen19e7aa82016-11-30 14:05:39 -08001293 // Now check if all global motion is non translational
1294 for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1295 if (xd->global_motion[mbmi->ref_frame[ref]].wmtype <= TRANSLATION) return 0;
1296 }
1297 return 1;
1298}
Yue Chen19e7aa82016-11-30 14:05:39 -08001299
Yaowu Xu4ff59b52017-04-24 12:41:56 -07001300static INLINE PLANE_TYPE get_plane_type(int plane) {
Luc Trudeau005feb62017-02-22 13:34:01 -05001301 return (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
1302}
1303
Angie Chiang155bf9a2017-08-06 19:52:57 -07001304static INLINE void transpose_uint8(uint8_t *dst, int dst_stride,
1305 const uint8_t *src, int src_stride, int w,
1306 int h) {
1307 int r, c;
1308 for (r = 0; r < h; ++r)
1309 for (c = 0; c < w; ++c) dst[c * dst_stride + r] = src[r * src_stride + c];
1310}
1311
1312static INLINE void transpose_uint16(uint16_t *dst, int dst_stride,
1313 const uint16_t *src, int src_stride, int w,
1314 int h) {
1315 int r, c;
1316 for (r = 0; r < h; ++r)
1317 for (c = 0; c < w; ++c) dst[c * dst_stride + r] = src[r * src_stride + c];
1318}
1319
1320static INLINE void transpose_int16(int16_t *dst, int dst_stride,
1321 const int16_t *src, int src_stride, int w,
1322 int h) {
1323 int r, c;
1324 for (r = 0; r < h; ++r)
1325 for (c = 0; c < w; ++c) dst[c * dst_stride + r] = src[r * src_stride + c];
1326}
1327
1328static INLINE void transpose_int32(int32_t *dst, int dst_stride,
1329 const int32_t *src, int src_stride, int w,
1330 int h) {
1331 int r, c;
1332 for (r = 0; r < h; ++r)
1333 for (c = 0; c < w; ++c) dst[c * dst_stride + r] = src[r * src_stride + c];
1334}
1335
Urvang Joshi80893152017-10-27 11:51:14 -07001336static INLINE int av1_get_max_eob(TX_SIZE tx_size) {
Timothy B. Terriberry501acee2017-12-19 05:38:46 -08001337#if CONFIG_TX64X64
Urvang Joshi030cea92017-12-05 15:27:09 -08001338 if (tx_size == TX_64X64 || tx_size == TX_64X32 || tx_size == TX_32X64) {
1339 return 1024;
1340 }
1341 if (tx_size == TX_16X64 || tx_size == TX_64X16) {
1342 return 512;
1343 }
Urvang Joshi80893152017-10-27 11:51:14 -07001344#endif // CONFIG_TX64X64 && !CONFIG_DAALA_TX
Urvang Joshi030cea92017-12-05 15:27:09 -08001345 return tx_size_2d[tx_size];
Urvang Joshi80893152017-10-27 11:51:14 -07001346}
1347
Debargha Mukherjee695abd72017-12-16 10:15:45 -08001348static INLINE TX_SIZE av1_get_adjusted_tx_size(TX_SIZE tx_size) {
1349#if CONFIG_TX64X64
1350 if (tx_size == TX_64X64 || tx_size == TX_64X32 || tx_size == TX_32X64) {
1351 return TX_32X32;
1352 }
1353 if (tx_size == TX_16X64) {
1354 return TX_16X32;
1355 }
1356 if (tx_size == TX_64X16) {
1357 return TX_32X16;
1358 }
1359#endif // CONFIG_TX64X64
1360 return tx_size;
1361}
1362
Yaowu Xuc27fc142016-08-22 16:08:15 -07001363#ifdef __cplusplus
1364} // extern "C"
1365#endif
1366
Yaowu Xuf883b422016-08-30 14:01:10 -07001367#endif // AV1_COMMON_BLOCKD_H_