1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * 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.
10  */
11 
12 #ifndef AOM_AV1_COMMON_BLOCKD_H_
13 #define AOM_AV1_COMMON_BLOCKD_H_
14 
15 #include "config/aom_config.h"
16 
17 #include "aom_dsp/aom_dsp_common.h"
18 #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"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define USE_B_QUANT_NO_TRELLIS 1
35 
36 #define MAX_MB_PLANE 3
37 
38 #define MAX_DIFFWTD_MASK_BITS 1
39 
40 #define INTERINTRA_WEDGE_SIGN 0
41 
42 // DIFFWTD_MASK_TYPES should not surpass 1 << MAX_DIFFWTD_MASK_BITS
43 enum {
44   DIFFWTD_38 = 0,
45   DIFFWTD_38_INV,
46   DIFFWTD_MASK_TYPES,
47 } UENUM1BYTE(DIFFWTD_MASK_TYPE);
48 
49 enum {
50   KEY_FRAME = 0,
51   INTER_FRAME = 1,
52   INTRA_ONLY_FRAME = 2,  // replaces intra-only
53   S_FRAME = 3,
54   FRAME_TYPES,
55 } UENUM1BYTE(FRAME_TYPE);
56 
is_comp_ref_allowed(BLOCK_SIZE bsize)57 static INLINE int is_comp_ref_allowed(BLOCK_SIZE bsize) {
58   return AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
59 }
60 
is_inter_mode(PREDICTION_MODE mode)61 static INLINE int is_inter_mode(PREDICTION_MODE mode) {
62   return mode >= INTER_MODE_START && mode < INTER_MODE_END;
63 }
64 
65 typedef struct {
66   uint8_t *plane[MAX_MB_PLANE];
67   int stride[MAX_MB_PLANE];
68 } BUFFER_SET;
69 
is_inter_singleref_mode(PREDICTION_MODE mode)70 static INLINE int is_inter_singleref_mode(PREDICTION_MODE mode) {
71   return mode >= SINGLE_INTER_MODE_START && mode < SINGLE_INTER_MODE_END;
72 }
is_inter_compound_mode(PREDICTION_MODE mode)73 static INLINE int is_inter_compound_mode(PREDICTION_MODE mode) {
74   return mode >= COMP_INTER_MODE_START && mode < COMP_INTER_MODE_END;
75 }
76 
compound_ref0_mode(PREDICTION_MODE mode)77 static INLINE PREDICTION_MODE compound_ref0_mode(PREDICTION_MODE mode) {
78   static const PREDICTION_MODE lut[] = {
79     DC_PRED,        // DC_PRED
80     V_PRED,         // V_PRED
81     H_PRED,         // H_PRED
82     D45_PRED,       // D45_PRED
83     D135_PRED,      // D135_PRED
84     D113_PRED,      // D113_PRED
85     D157_PRED,      // D157_PRED
86     D203_PRED,      // D203_PRED
87     D67_PRED,       // D67_PRED
88     SMOOTH_PRED,    // SMOOTH_PRED
89     SMOOTH_V_PRED,  // SMOOTH_V_PRED
90     SMOOTH_H_PRED,  // SMOOTH_H_PRED
91     PAETH_PRED,     // PAETH_PRED
92     NEARESTMV,      // NEARESTMV
93     NEARMV,         // NEARMV
94     GLOBALMV,       // GLOBALMV
95     NEWMV,          // NEWMV
96     NEARESTMV,      // NEAREST_NEARESTMV
97     NEARMV,         // NEAR_NEARMV
98     NEARESTMV,      // NEAREST_NEWMV
99     NEWMV,          // NEW_NEARESTMV
100     NEARMV,         // NEAR_NEWMV
101     NEWMV,          // NEW_NEARMV
102     GLOBALMV,       // GLOBAL_GLOBALMV
103     NEWMV,          // NEW_NEWMV
104   };
105   assert(NELEMENTS(lut) == MB_MODE_COUNT);
106   assert(is_inter_compound_mode(mode) || is_inter_singleref_mode(mode));
107   return lut[mode];
108 }
109 
compound_ref1_mode(PREDICTION_MODE mode)110 static INLINE PREDICTION_MODE compound_ref1_mode(PREDICTION_MODE mode) {
111   static const PREDICTION_MODE lut[] = {
112     MB_MODE_COUNT,  // DC_PRED
113     MB_MODE_COUNT,  // V_PRED
114     MB_MODE_COUNT,  // H_PRED
115     MB_MODE_COUNT,  // D45_PRED
116     MB_MODE_COUNT,  // D135_PRED
117     MB_MODE_COUNT,  // D113_PRED
118     MB_MODE_COUNT,  // D157_PRED
119     MB_MODE_COUNT,  // D203_PRED
120     MB_MODE_COUNT,  // D67_PRED
121     MB_MODE_COUNT,  // SMOOTH_PRED
122     MB_MODE_COUNT,  // SMOOTH_V_PRED
123     MB_MODE_COUNT,  // SMOOTH_H_PRED
124     MB_MODE_COUNT,  // PAETH_PRED
125     MB_MODE_COUNT,  // NEARESTMV
126     MB_MODE_COUNT,  // NEARMV
127     MB_MODE_COUNT,  // GLOBALMV
128     MB_MODE_COUNT,  // NEWMV
129     NEARESTMV,      // NEAREST_NEARESTMV
130     NEARMV,         // NEAR_NEARMV
131     NEWMV,          // NEAREST_NEWMV
132     NEARESTMV,      // NEW_NEARESTMV
133     NEWMV,          // NEAR_NEWMV
134     NEARMV,         // NEW_NEARMV
135     GLOBALMV,       // GLOBAL_GLOBALMV
136     NEWMV,          // NEW_NEWMV
137   };
138   assert(NELEMENTS(lut) == MB_MODE_COUNT);
139   assert(is_inter_compound_mode(mode));
140   return lut[mode];
141 }
142 
have_nearmv_in_inter_mode(PREDICTION_MODE mode)143 static INLINE int have_nearmv_in_inter_mode(PREDICTION_MODE mode) {
144   return (mode == NEARMV || mode == NEAR_NEARMV || mode == NEAR_NEWMV ||
145           mode == NEW_NEARMV);
146 }
147 
have_newmv_in_inter_mode(PREDICTION_MODE mode)148 static INLINE int have_newmv_in_inter_mode(PREDICTION_MODE mode) {
149   return (mode == NEWMV || mode == NEW_NEWMV || mode == NEAREST_NEWMV ||
150           mode == NEW_NEARESTMV || mode == NEAR_NEWMV || mode == NEW_NEARMV);
151 }
152 
is_masked_compound_type(COMPOUND_TYPE type)153 static INLINE int is_masked_compound_type(COMPOUND_TYPE type) {
154   return (type == COMPOUND_WEDGE || type == COMPOUND_DIFFWTD);
155 }
156 
157 /* For keyframes, intra block modes are predicted by the (already decoded)
158    modes for the Y blocks to the left and above us; for interframes, there
159    is a single probability table. */
160 
161 typedef struct {
162   // Value of base colors for Y, U, and V
163   uint16_t palette_colors[3 * PALETTE_MAX_SIZE];
164   // Number of base colors for Y (0) and UV (1)
165   uint8_t palette_size[2];
166 } PALETTE_MODE_INFO;
167 
168 typedef struct {
169   FILTER_INTRA_MODE filter_intra_mode;
170   uint8_t use_filter_intra;
171 } FILTER_INTRA_MODE_INFO;
172 
173 static const PREDICTION_MODE fimode_to_intradir[FILTER_INTRA_MODES] = {
174   DC_PRED, V_PRED, H_PRED, D157_PRED, DC_PRED
175 };
176 
177 #if CONFIG_RD_DEBUG
178 #define TXB_COEFF_COST_MAP_SIZE (MAX_MIB_SIZE)
179 #endif
180 
181 typedef struct RD_STATS {
182   int rate;
183   int64_t dist;
184   // Please be careful of using rdcost, it's not guaranteed to be set all the
185   // time.
186   // TODO(angiebird): Create a set of functions to manipulate the RD_STATS. In
187   // these functions, make sure rdcost is always up-to-date according to
188   // rate/dist.
189   int64_t rdcost;
190   int64_t sse;
191   int skip;  // sse should equal to dist when skip == 1
192   int zero_rate;
193 #if CONFIG_RD_DEBUG
194   int txb_coeff_cost[MAX_MB_PLANE];
195   // TODO(jingning): Temporary solution to silence stack over-size warning
196   // in handle_inter_mode. This should be fixed after rate-distortion
197   // optimization refactoring.
198   int16_t txb_coeff_cost_map[MAX_MB_PLANE][TXB_COEFF_COST_MAP_SIZE]
199                             [TXB_COEFF_COST_MAP_SIZE];
200 #endif  // CONFIG_RD_DEBUG
201 } RD_STATS;
202 
203 // This struct is used to group function args that are commonly
204 // sent together in functions related to interinter compound modes
205 typedef struct {
206   uint8_t *seg_mask;
207   int8_t wedge_index;
208   int8_t wedge_sign;
209   DIFFWTD_MASK_TYPE mask_type;
210   COMPOUND_TYPE type;
211 } INTERINTER_COMPOUND_DATA;
212 
213 #define INTER_TX_SIZE_BUF_LEN 16
214 #define TXK_TYPE_BUF_LEN 64
215 // This structure now relates to 4x4 block regions.
216 typedef struct MB_MODE_INFO {
217   // interinter members
218   INTERINTER_COMPOUND_DATA interinter_comp;
219   WarpedMotionParams wm_params;
220   int_mv mv[2];
221   int current_qindex;
222   // Only for INTER blocks
223   int_interpfilters interp_filters;
224   // TODO(debargha): Consolidate these flags
225 #if CONFIG_RD_DEBUG
226   RD_STATS rd_stats;
227   int mi_row;
228   int mi_col;
229 #endif
230 #if CONFIG_INSPECTION
231   int16_t tx_skip[TXK_TYPE_BUF_LEN];
232 #endif
233   PALETTE_MODE_INFO palette_mode_info;
234   // Common for both INTER and INTRA blocks
235   BLOCK_SIZE sb_type;
236   PREDICTION_MODE mode;
237   // Only for INTRA blocks
238   UV_PREDICTION_MODE uv_mode;
239   // interintra members
240   INTERINTRA_MODE interintra_mode;
241   MOTION_MODE motion_mode;
242   PARTITION_TYPE partition;
243   MV_REFERENCE_FRAME ref_frame[2];
244   FILTER_INTRA_MODE_INFO filter_intra_mode_info;
245   int8_t skip;
246   uint8_t inter_tx_size[INTER_TX_SIZE_BUF_LEN];
247   TX_SIZE tx_size;
248   int8_t delta_lf_from_base;
249   int8_t delta_lf[FRAME_LF_COUNT];
250   int8_t interintra_wedge_index;
251   // The actual prediction angle is the base angle + (angle_delta * step).
252   int8_t angle_delta[PLANE_TYPES];
253   /* deringing gain *per-superblock* */
254   // Joint sign of alpha Cb and alpha Cr
255   int8_t cfl_alpha_signs;
256   // Index of the alpha Cb and alpha Cr combination
257   uint8_t cfl_alpha_idx;
258   uint8_t num_proj_ref;
259   uint8_t overlappable_neighbors[2];
260   // If comp_group_idx=0, indicate if dist_wtd_comp(0) or avg_comp(1) is used.
261   uint8_t compound_idx;
262   uint8_t use_wedge_interintra : 1;
263   uint8_t segment_id : 3;
264   uint8_t seg_id_predicted : 1;  // valid only when temporal_update is enabled
265   uint8_t skip_mode : 1;
266   uint8_t use_intrabc : 1;
267   uint8_t ref_mv_idx : 2;
268   // Indicate if masked compound is used(1) or not(0).
269   uint8_t comp_group_idx : 1;
270   int8_t cdef_strength : 4;
271 } MB_MODE_INFO;
272 
is_intrabc_block(const MB_MODE_INFO * mbmi)273 static INLINE int is_intrabc_block(const MB_MODE_INFO *mbmi) {
274   return mbmi->use_intrabc;
275 }
276 
get_uv_mode(UV_PREDICTION_MODE mode)277 static INLINE PREDICTION_MODE get_uv_mode(UV_PREDICTION_MODE mode) {
278   assert(mode < UV_INTRA_MODES);
279   static const PREDICTION_MODE uv2y[] = {
280     DC_PRED,        // UV_DC_PRED
281     V_PRED,         // UV_V_PRED
282     H_PRED,         // UV_H_PRED
283     D45_PRED,       // UV_D45_PRED
284     D135_PRED,      // UV_D135_PRED
285     D113_PRED,      // UV_D113_PRED
286     D157_PRED,      // UV_D157_PRED
287     D203_PRED,      // UV_D203_PRED
288     D67_PRED,       // UV_D67_PRED
289     SMOOTH_PRED,    // UV_SMOOTH_PRED
290     SMOOTH_V_PRED,  // UV_SMOOTH_V_PRED
291     SMOOTH_H_PRED,  // UV_SMOOTH_H_PRED
292     PAETH_PRED,     // UV_PAETH_PRED
293     DC_PRED,        // UV_CFL_PRED
294     INTRA_INVALID,  // UV_INTRA_MODES
295     INTRA_INVALID,  // UV_MODE_INVALID
296   };
297   return uv2y[mode];
298 }
299 
is_inter_block(const MB_MODE_INFO * mbmi)300 static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
301   return is_intrabc_block(mbmi) || mbmi->ref_frame[0] > INTRA_FRAME;
302 }
303 
has_second_ref(const MB_MODE_INFO * mbmi)304 static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
305   return mbmi->ref_frame[1] > INTRA_FRAME;
306 }
307 
has_uni_comp_refs(const MB_MODE_INFO * mbmi)308 static INLINE int has_uni_comp_refs(const MB_MODE_INFO *mbmi) {
309   return has_second_ref(mbmi) && (!((mbmi->ref_frame[0] >= BWDREF_FRAME) ^
310                                     (mbmi->ref_frame[1] >= BWDREF_FRAME)));
311 }
312 
comp_ref0(int ref_idx)313 static INLINE MV_REFERENCE_FRAME comp_ref0(int ref_idx) {
314   static const MV_REFERENCE_FRAME lut[] = {
315     LAST_FRAME,     // LAST_LAST2_FRAMES,
316     LAST_FRAME,     // LAST_LAST3_FRAMES,
317     LAST_FRAME,     // LAST_GOLDEN_FRAMES,
318     BWDREF_FRAME,   // BWDREF_ALTREF_FRAMES,
319     LAST2_FRAME,    // LAST2_LAST3_FRAMES
320     LAST2_FRAME,    // LAST2_GOLDEN_FRAMES,
321     LAST3_FRAME,    // LAST3_GOLDEN_FRAMES,
322     BWDREF_FRAME,   // BWDREF_ALTREF2_FRAMES,
323     ALTREF2_FRAME,  // ALTREF2_ALTREF_FRAMES,
324   };
325   assert(NELEMENTS(lut) == TOTAL_UNIDIR_COMP_REFS);
326   return lut[ref_idx];
327 }
328 
comp_ref1(int ref_idx)329 static INLINE MV_REFERENCE_FRAME comp_ref1(int ref_idx) {
330   static const MV_REFERENCE_FRAME lut[] = {
331     LAST2_FRAME,    // LAST_LAST2_FRAMES,
332     LAST3_FRAME,    // LAST_LAST3_FRAMES,
333     GOLDEN_FRAME,   // LAST_GOLDEN_FRAMES,
334     ALTREF_FRAME,   // BWDREF_ALTREF_FRAMES,
335     LAST3_FRAME,    // LAST2_LAST3_FRAMES
336     GOLDEN_FRAME,   // LAST2_GOLDEN_FRAMES,
337     GOLDEN_FRAME,   // LAST3_GOLDEN_FRAMES,
338     ALTREF2_FRAME,  // BWDREF_ALTREF2_FRAMES,
339     ALTREF_FRAME,   // ALTREF2_ALTREF_FRAMES,
340   };
341   assert(NELEMENTS(lut) == TOTAL_UNIDIR_COMP_REFS);
342   return lut[ref_idx];
343 }
344 
345 PREDICTION_MODE av1_left_block_mode(const MB_MODE_INFO *left_mi);
346 
347 PREDICTION_MODE av1_above_block_mode(const MB_MODE_INFO *above_mi);
348 
is_global_mv_block(const MB_MODE_INFO * const mbmi,TransformationType type)349 static INLINE int is_global_mv_block(const MB_MODE_INFO *const mbmi,
350                                      TransformationType type) {
351   const PREDICTION_MODE mode = mbmi->mode;
352   const BLOCK_SIZE bsize = mbmi->sb_type;
353   const int block_size_allowed =
354       AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
355   return (mode == GLOBALMV || mode == GLOBAL_GLOBALMV) && type > TRANSLATION &&
356          block_size_allowed;
357 }
358 
359 #if CONFIG_MISMATCH_DEBUG
mi_to_pixel_loc(int * pixel_c,int * pixel_r,int mi_col,int mi_row,int tx_blk_col,int tx_blk_row,int subsampling_x,int subsampling_y)360 static INLINE void mi_to_pixel_loc(int *pixel_c, int *pixel_r, int mi_col,
361                                    int mi_row, int tx_blk_col, int tx_blk_row,
362                                    int subsampling_x, int subsampling_y) {
363   *pixel_c = ((mi_col >> subsampling_x) << MI_SIZE_LOG2) +
364              (tx_blk_col << MI_SIZE_LOG2);
365   *pixel_r = ((mi_row >> subsampling_y) << MI_SIZE_LOG2) +
366              (tx_blk_row << MI_SIZE_LOG2);
367 }
368 #endif
369 
370 enum { MV_PRECISION_Q3, MV_PRECISION_Q4 } UENUM1BYTE(mv_precision);
371 
372 struct buf_2d {
373   uint8_t *buf;
374   uint8_t *buf0;
375   int width;
376   int height;
377   int stride;
378 };
379 
380 typedef struct eob_info {
381   uint16_t eob;
382   uint16_t max_scan_line;
383 } eob_info;
384 
385 typedef struct {
386   DECLARE_ALIGNED(32, tran_low_t, dqcoeff[MAX_MB_PLANE][MAX_SB_SQUARE]);
387   eob_info eob_data[MAX_MB_PLANE]
388                    [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
389   DECLARE_ALIGNED(16, uint8_t, color_index_map[2][MAX_SB_SQUARE]);
390 } CB_BUFFER;
391 
392 typedef struct macroblockd_plane {
393   tran_low_t *dqcoeff;
394   tran_low_t *dqcoeff_block;
395   eob_info *eob_data;
396   PLANE_TYPE plane_type;
397   int subsampling_x;
398   int subsampling_y;
399   struct buf_2d dst;
400   struct buf_2d pre[2];
401   ENTROPY_CONTEXT *above_entropy_context;
402   ENTROPY_CONTEXT *left_entropy_context;
403 
404   // The dequantizers below are true dequantizers used only in the
405   // dequantization process.  They have the same coefficient
406   // shift/scale as TX.
407   int16_t seg_dequant_QTX[MAX_SEGMENTS][2];
408   uint8_t *color_index_map;
409 
410   // block size in pixels
411   uint8_t width, height;
412 
413   qm_val_t *seg_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
414   qm_val_t *seg_qmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
415 } MACROBLOCKD_PLANE;
416 
417 #define BLOCK_OFFSET(i) ((i) << 4)
418 
419 typedef struct {
420   DECLARE_ALIGNED(16, InterpKernel, vfilter);
421   DECLARE_ALIGNED(16, InterpKernel, hfilter);
422 } WienerInfo;
423 
424 typedef struct {
425   int ep;
426   int xqd[2];
427 } SgrprojInfo;
428 
429 #if CONFIG_DEBUG
430 #define CFL_SUB8X8_VAL_MI_SIZE (4)
431 #define CFL_SUB8X8_VAL_MI_SQUARE \
432   (CFL_SUB8X8_VAL_MI_SIZE * CFL_SUB8X8_VAL_MI_SIZE)
433 #endif  // CONFIG_DEBUG
434 #define CFL_MAX_BLOCK_SIZE (BLOCK_32X32)
435 #define CFL_BUF_LINE (32)
436 #define CFL_BUF_LINE_I128 (CFL_BUF_LINE >> 3)
437 #define CFL_BUF_LINE_I256 (CFL_BUF_LINE >> 4)
438 #define CFL_BUF_SQUARE (CFL_BUF_LINE * CFL_BUF_LINE)
439 typedef struct cfl_ctx {
440   // Q3 reconstructed luma pixels (only Q2 is required, but Q3 is used to avoid
441   // shifts)
442   uint16_t recon_buf_q3[CFL_BUF_SQUARE];
443   // Q3 AC contributions (reconstructed luma pixels - tx block avg)
444   int16_t ac_buf_q3[CFL_BUF_SQUARE];
445 
446   // Cache the DC_PRED when performing RDO, so it does not have to be recomputed
447   // for every scaling parameter
448   int dc_pred_is_cached[CFL_PRED_PLANES];
449   // The DC_PRED cache is disable when decoding
450   int use_dc_pred_cache;
451   // Only cache the first row of the DC_PRED
452   int16_t dc_pred_cache[CFL_PRED_PLANES][CFL_BUF_LINE];
453 
454   // Height and width currently used in the CfL prediction buffer.
455   int buf_height, buf_width;
456 
457   int are_parameters_computed;
458 
459   // Chroma subsampling
460   int subsampling_x, subsampling_y;
461 
462   // Whether the reconstructed luma pixels need to be stored
463   int store_y;
464 
465 #if CONFIG_DEBUG
466   int rate;
467 #endif  // CONFIG_DEBUG
468 } CFL_CTX;
469 
470 typedef struct dist_wtd_comp_params {
471   int use_dist_wtd_comp_avg;
472   int fwd_offset;
473   int bck_offset;
474 } DIST_WTD_COMP_PARAMS;
475 
476 struct scale_factors;
477 
478 // Most/all of the pointers are mere pointers to actual arrays are allocated
479 // elsewhere. This is mostly for coding convenience.
480 typedef struct macroblockd {
481   // Row and column position of current macroblock in mi units.
482   int mi_row;
483   int mi_col;
484   // Same as cm->mi_params.mi_stride, copied here for convenience.
485   int mi_stride;
486 
487   // True if current block transmits chroma information.
488   // More detail:
489   // Smallest supported block size for both luma and chroma plane is 4x4. Hence,
490   // in case of subsampled chroma plane (YUV 4:2:0 or YUV 4:2:2), multiple luma
491   // blocks smaller than 8x8 maybe combined into one chroma block.
492   // For example, for YUV 4:2:0, let's say an 8x8 area is split into four 4x4
493   // luma blocks. Then, a single chroma block of size 4x4 will cover the area of
494   // these four luma blocks. This is implemented in bitstream as follows:
495   // - There are four MB_MODE_INFO structs for the four luma blocks.
496   // - First 3 MB_MODE_INFO have is_chroma_ref = false, and so do not transmit
497   // any information for chroma planes.
498   // - Last block will have is_chroma_ref = true and transmits chroma
499   // information for the 4x4 chroma block that covers whole 8x8 area covered by
500   // four luma blocks.
501   // Similar logic applies for chroma blocks that cover 2 or 3 luma blocks.
502   bool is_chroma_ref;
503 
504   struct macroblockd_plane plane[MAX_MB_PLANE];
505 
506   TileInfo tile;
507 
508   // Appropriate offset inside cm->mi_params.mi_grid_base based on current
509   // mi_row and mi_col.
510   MB_MODE_INFO **mi;
511 
512   // True if 4x4 block above the current block is available.
513   bool up_available;
514   // True if 4x4 block to the left of the current block is available.
515   bool left_available;
516   // True if the above chrome reference block is available.
517   bool chroma_up_available;
518   // True if the left chrome reference block is available.
519   bool chroma_left_available;
520 
521   // MB_MODE_INFO for 4x4 block to the left of the current block, if
522   // left_available == true; otherwise NULL.
523   MB_MODE_INFO *left_mbmi;
524   // MB_MODE_INFO for 4x4 block above the current block, if
525   // up_available == true; otherwise NULL.
526   MB_MODE_INFO *above_mbmi;
527   // Above chroma reference block if is_chroma_ref == true for the current block
528   // and chroma_up_available == true; otherwise NULL.
529   // See also: the special case logic when current chroma block covers more than
530   // one luma blocks in set_mi_row_col().
531   MB_MODE_INFO *chroma_left_mbmi;
532   // Left chroma reference block if is_chroma_ref == true for the current block
533   // and chroma_left_available == true; otherwise NULL.
534   // See also: the special case logic when current chroma block covers more than
535   // one luma blocks in set_mi_row_col().
536   MB_MODE_INFO *chroma_above_mbmi;
537 
538   // Appropriate offset based on current 'mi_row' and 'mi_col', inside
539   // 'tx_type_map' in one of 'CommonModeInfoParams', 'PICK_MODE_CONTEXT' or
540   // 'MACROBLOCK' structs.
541   uint8_t *tx_type_map;
542   // Stride for 'tx_type_map'. Note that this may / may not be same as
543   // 'mi_stride', depending on which actual array 'tx_type_map' points to.
544   int tx_type_map_stride;
545 
546   // Distance of this macroblock from frame edges in 1/8th pixel units.
547   int mb_to_left_edge;
548   int mb_to_right_edge;
549   int mb_to_top_edge;
550   int mb_to_bottom_edge;
551 
552   // Scale factors for reference frames of the current block.
553   // These are pointers into 'cm->ref_scale_factors'.
554   const struct scale_factors *block_ref_scale_factors[2];
555 
556   const YV12_BUFFER_CONFIG *cur_buf;
557 
558   // Entropy contexts for the above blocks.
559   // above_entropy_context[i][j] corresponds to above entropy context for ith
560   // plane and jth mi column of this *frame*, wrt current 'mi_row'.
561   // These are pointers into 'cm->above_contexts.entropy'.
562   ENTROPY_CONTEXT *above_entropy_context[MAX_MB_PLANE];
563   // Entropy contexts for the left blocks.
564   // left_entropy_context[i][j] corresponds to left entropy context for ith
565   // plane and jth mi row of this *superblock*, wrt current 'mi_col'.
566   // Note: These contain actual data, NOT pointers.
567   ENTROPY_CONTEXT left_entropy_context[MAX_MB_PLANE][MAX_MIB_SIZE];
568 
569   // Partition contexts for the above blocks.
570   // above_partition_context[i] corresponds to above partition context for ith
571   // mi column of this *frame*, wrt current 'mi_row'.
572   // These are pointers into 'cm->above_contexts.partition'.
573   PARTITION_CONTEXT *above_partition_context;
574   // Partition contexts for the left blocks.
575   // left_partition_context[i] corresponds to left partition context for ith
576   // mi row of this *superblock*, wrt current 'mi_col'.
577   // Note: These contain actual data, NOT pointers.
578   PARTITION_CONTEXT left_partition_context[MAX_MIB_SIZE];
579 
580   // Transform contexts for the above blocks.
581   // TODO(urvang): Indexed two different ways from cm->above_contexts.txfm in
582   // code currently. Need to make it consistent / document why.
583   TXFM_CONTEXT *above_txfm_context;
584   // Transform contexts for the left blocks.
585   TXFM_CONTEXT *left_txfm_context;
586   // TODO(urvang): 'left_txfm_context' points to 'left_txfm_context_buffer'.
587   // Can we remove this indirection?
588   TXFM_CONTEXT left_txfm_context_buffer[MAX_MIB_SIZE];
589 
590   // Default values for the two restoration filters for each plane.
591   // These values are used as reference values when writing the bitstream. That
592   // is, we transmit the delta between the actual values in
593   // cm->rst_info[plane].unit_info[unit_idx] and these reference values.
594   WienerInfo wiener_info[MAX_MB_PLANE];
595   SgrprojInfo sgrproj_info[MAX_MB_PLANE];
596 
597   // Block dimensions in MB_MODE_INFO units.
598   uint8_t width;
599   uint8_t height;
600 
601   uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
602   CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
603   uint16_t weight[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE];
604   uint8_t is_sec_rect;
605 
606   // Counts of each reference frame in the above and left neighboring blocks.
607   // NOTE: Take into account both single and comp references.
608   uint8_t neighbors_ref_counts[REF_FRAMES];
609 
610   FRAME_CONTEXT *tile_ctx;
611   // Bit depth: copied from cm->seq_params.bit_depth for convenience.
612   int bd;
613 
614   int qindex[MAX_SEGMENTS];
615   int lossless[MAX_SEGMENTS];
616   // TODO(urvang): Move to decoder.
617   int corrupted;
618   // Same as cm->features.cur_frame_force_integer_mv.
619   int cur_frame_force_integer_mv;
620   // Pointer to cm->error.
621   struct aom_internal_error_info *error_info;
622   // Same as cm->global_motion.
623   const WarpedMotionParams *global_motion;
624   int delta_qindex;
625   int current_qindex;
626   // Since actual frame level loop filtering level value is not available
627   // at the beginning of the tile (only available during actual filtering)
628   // at encoder side.we record the delta_lf (against the frame level loop
629   // filtering level) and code the delta between previous superblock's delta
630   // lf and current delta lf. It is equivalent to the delta between previous
631   // superblock's actual lf and current lf.
632   int8_t delta_lf_from_base;
633   // For this experiment, we have four frame filter levels for different plane
634   // and direction. So, to support the per superblock update, we need to add
635   // a few more params as below.
636   // 0: delta loop filter level for y plane vertical
637   // 1: delta loop filter level for y plane horizontal
638   // 2: delta loop filter level for u plane
639   // 3: delta loop filter level for v plane
640   // To make it consistent with the reference to each filter level in segment,
641   // we need to -1, since
642   // SEG_LVL_ALT_LF_Y_V = 1;
643   // SEG_LVL_ALT_LF_Y_H = 2;
644   // SEG_LVL_ALT_LF_U   = 3;
645   // SEG_LVL_ALT_LF_V   = 4;
646   int8_t delta_lf[FRAME_LF_COUNT];
647   // cdef_transmitted[i] is true if CDEF strength for ith CDEF unit in the
648   // current superblock has already been read from (decoder) / written to
649   // (encoder) the bitstream; and false otherwise.
650   // More detail:
651   // (1) CDEF strength is transmitted only once per CDEF unit, in the 1st
652   // non-skip coding block. So, we need this array to keep track of whether CDEF
653   // strengths for the given CDEF units have been transmitted yet or not.
654   // (2) Superblock size can be either 128x128 or 64x64, but CDEF unit size is
655   // fixed to be 64x64. So, there may be 4 CDEF units within a superblock (if
656   // superblock size is 128x128). Hence the array size is 4.
657   // (3) In the current implementation, CDEF strength for this CDEF unit is
658   // stored in the MB_MODE_INFO of the 1st block in this CDEF unit (inside
659   // cm->mi_params.mi_grid_base).
660   bool cdef_transmitted[4];
661 
662   DECLARE_ALIGNED(16, uint8_t, seg_mask[2 * MAX_SB_SQUARE]);
663   uint8_t *mc_buf[2];
664   CFL_CTX cfl;
665 
666   DIST_WTD_COMP_PARAMS jcp_param;
667 
668   uint16_t cb_offset[MAX_MB_PLANE];
669   uint16_t txb_offset[MAX_MB_PLANE];
670   uint16_t color_index_map_offset[2];
671 
672   CONV_BUF_TYPE *tmp_conv_dst;
673   uint8_t *tmp_obmc_bufs[2];
674 } MACROBLOCKD;
675 
is_cur_buf_hbd(const MACROBLOCKD * xd)676 static INLINE int is_cur_buf_hbd(const MACROBLOCKD *xd) {
677   return xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH ? 1 : 0;
678 }
679 
get_buf_by_bd(const MACROBLOCKD * xd,uint8_t * buf16)680 static INLINE uint8_t *get_buf_by_bd(const MACROBLOCKD *xd, uint8_t *buf16) {
681   return (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
682              ? CONVERT_TO_BYTEPTR(buf16)
683              : buf16;
684 }
685 
get_sqr_bsize_idx(BLOCK_SIZE bsize)686 static INLINE int get_sqr_bsize_idx(BLOCK_SIZE bsize) {
687   switch (bsize) {
688     case BLOCK_4X4: return 0;
689     case BLOCK_8X8: return 1;
690     case BLOCK_16X16: return 2;
691     case BLOCK_32X32: return 3;
692     case BLOCK_64X64: return 4;
693     case BLOCK_128X128: return 5;
694     default: return SQR_BLOCK_SIZES;
695   }
696 }
697 
698 // For a square block size 'bsize', returns the size of the sub-blocks used by
699 // the given partition type. If the partition produces sub-blocks of different
700 // sizes, then the function returns the largest sub-block size.
701 // Implements the Partition_Subsize lookup table in the spec (Section 9.3.
702 // Conversion tables).
703 // Note: the input block size should be square.
704 // Otherwise it's considered invalid.
get_partition_subsize(BLOCK_SIZE bsize,PARTITION_TYPE partition)705 static INLINE BLOCK_SIZE get_partition_subsize(BLOCK_SIZE bsize,
706                                                PARTITION_TYPE partition) {
707   if (partition == PARTITION_INVALID) {
708     return BLOCK_INVALID;
709   } else {
710     const int sqr_bsize_idx = get_sqr_bsize_idx(bsize);
711     return sqr_bsize_idx >= SQR_BLOCK_SIZES
712                ? BLOCK_INVALID
713                : subsize_lookup[partition][sqr_bsize_idx];
714   }
715 }
716 
intra_mode_to_tx_type(const MB_MODE_INFO * mbmi,PLANE_TYPE plane_type)717 static TX_TYPE intra_mode_to_tx_type(const MB_MODE_INFO *mbmi,
718                                      PLANE_TYPE plane_type) {
719   static const TX_TYPE _intra_mode_to_tx_type[INTRA_MODES] = {
720     DCT_DCT,    // DC_PRED
721     ADST_DCT,   // V_PRED
722     DCT_ADST,   // H_PRED
723     DCT_DCT,    // D45_PRED
724     ADST_ADST,  // D135_PRED
725     ADST_DCT,   // D113_PRED
726     DCT_ADST,   // D157_PRED
727     DCT_ADST,   // D203_PRED
728     ADST_DCT,   // D67_PRED
729     ADST_ADST,  // SMOOTH_PRED
730     ADST_DCT,   // SMOOTH_V_PRED
731     DCT_ADST,   // SMOOTH_H_PRED
732     ADST_ADST,  // PAETH_PRED
733   };
734   const PREDICTION_MODE mode =
735       (plane_type == PLANE_TYPE_Y) ? mbmi->mode : get_uv_mode(mbmi->uv_mode);
736   assert(mode < INTRA_MODES);
737   return _intra_mode_to_tx_type[mode];
738 }
739 
is_rect_tx(TX_SIZE tx_size)740 static INLINE int is_rect_tx(TX_SIZE tx_size) { return tx_size >= TX_SIZES; }
741 
block_signals_txsize(BLOCK_SIZE bsize)742 static INLINE int block_signals_txsize(BLOCK_SIZE bsize) {
743   return bsize > BLOCK_4X4;
744 }
745 
746 // Number of transform types in each set type
747 static const int av1_num_ext_tx_set[EXT_TX_SET_TYPES] = {
748   1, 2, 5, 7, 12, 16,
749 };
750 
751 static const int av1_ext_tx_used[EXT_TX_SET_TYPES][TX_TYPES] = {
752   { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
753   { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
754   { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
755   { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 },
756   { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
757   { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
758 };
759 
760 static const uint16_t av1_reduced_intra_tx_used_flag[INTRA_MODES] = {
761   0x080F,  // DC_PRED:       0000 1000 0000 1111
762   0x040F,  // V_PRED:        0000 0100 0000 1111
763   0x080F,  // H_PRED:        0000 1000 0000 1111
764   0x020F,  // D45_PRED:      0000 0010 0000 1111
765   0x080F,  // D135_PRED:     0000 1000 0000 1111
766   0x040F,  // D113_PRED:     0000 0100 0000 1111
767   0x080F,  // D157_PRED:     0000 1000 0000 1111
768   0x080F,  // D203_PRED:     0000 1000 0000 1111
769   0x040F,  // D67_PRED:      0000 0100 0000 1111
770   0x080F,  // SMOOTH_PRED:   0000 1000 0000 1111
771   0x040F,  // SMOOTH_V_PRED: 0000 0100 0000 1111
772   0x080F,  // SMOOTH_H_PRED: 0000 1000 0000 1111
773   0x0C0E,  // PAETH_PRED:    0000 1100 0000 1110
774 };
775 
776 static const uint16_t av1_ext_tx_used_flag[EXT_TX_SET_TYPES] = {
777   0x0001,  // 0000 0000 0000 0001
778   0x0201,  // 0000 0010 0000 0001
779   0x020F,  // 0000 0010 0000 1111
780   0x0E0F,  // 0000 1110 0000 1111
781   0x0FFF,  // 0000 1111 1111 1111
782   0xFFFF,  // 1111 1111 1111 1111
783 };
784 
785 static const TxSetType av1_ext_tx_set_lookup[2][2] = {
786   { EXT_TX_SET_DTT4_IDTX_1DDCT, EXT_TX_SET_DTT4_IDTX },
787   { EXT_TX_SET_ALL16, EXT_TX_SET_DTT9_IDTX_1DDCT },
788 };
789 
av1_get_ext_tx_set_type(TX_SIZE tx_size,int is_inter,int use_reduced_set)790 static INLINE TxSetType av1_get_ext_tx_set_type(TX_SIZE tx_size, int is_inter,
791                                                 int use_reduced_set) {
792   const TX_SIZE tx_size_sqr_up = txsize_sqr_up_map[tx_size];
793   if (tx_size_sqr_up > TX_32X32) return EXT_TX_SET_DCTONLY;
794   if (tx_size_sqr_up == TX_32X32)
795     return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DCTONLY;
796   if (use_reduced_set)
797     return is_inter ? EXT_TX_SET_DCT_IDTX : EXT_TX_SET_DTT4_IDTX;
798   const TX_SIZE tx_size_sqr = txsize_sqr_map[tx_size];
799   return av1_ext_tx_set_lookup[is_inter][tx_size_sqr == TX_16X16];
800 }
801 
802 // Maps tx set types to the indices.
803 static const int ext_tx_set_index[2][EXT_TX_SET_TYPES] = {
804   { // Intra
805     0, -1, 2, 1, -1, -1 },
806   { // Inter
807     0, 3, -1, -1, 2, 1 },
808 };
809 
get_ext_tx_set(TX_SIZE tx_size,int is_inter,int use_reduced_set)810 static INLINE int get_ext_tx_set(TX_SIZE tx_size, int is_inter,
811                                  int use_reduced_set) {
812   const TxSetType set_type =
813       av1_get_ext_tx_set_type(tx_size, is_inter, use_reduced_set);
814   return ext_tx_set_index[is_inter][set_type];
815 }
816 
get_ext_tx_types(TX_SIZE tx_size,int is_inter,int use_reduced_set)817 static INLINE int get_ext_tx_types(TX_SIZE tx_size, int is_inter,
818                                    int use_reduced_set) {
819   const int set_type =
820       av1_get_ext_tx_set_type(tx_size, is_inter, use_reduced_set);
821   return av1_num_ext_tx_set[set_type];
822 }
823 
824 #define TXSIZEMAX(t1, t2) (tx_size_2d[(t1)] >= tx_size_2d[(t2)] ? (t1) : (t2))
825 #define TXSIZEMIN(t1, t2) (tx_size_2d[(t1)] <= tx_size_2d[(t2)] ? (t1) : (t2))
826 
tx_size_from_tx_mode(BLOCK_SIZE bsize,TX_MODE tx_mode)827 static INLINE TX_SIZE tx_size_from_tx_mode(BLOCK_SIZE bsize, TX_MODE tx_mode) {
828   const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
829   const TX_SIZE max_rect_tx_size = max_txsize_rect_lookup[bsize];
830   if (bsize == BLOCK_4X4)
831     return AOMMIN(max_txsize_lookup[bsize], largest_tx_size);
832   if (txsize_sqr_map[max_rect_tx_size] <= largest_tx_size)
833     return max_rect_tx_size;
834   else
835     return largest_tx_size;
836 }
837 
838 static const uint8_t mode_to_angle_map[] = {
839   0, 90, 180, 45, 135, 113, 157, 203, 67, 0, 0, 0, 0,
840 };
841 
842 // Converts block_index for given transform size to index of the block in raster
843 // order.
av1_block_index_to_raster_order(TX_SIZE tx_size,int block_idx)844 static INLINE int av1_block_index_to_raster_order(TX_SIZE tx_size,
845                                                   int block_idx) {
846   // For transform size 4x8, the possible block_idx values are 0 & 2, because
847   // block_idx values are incremented in steps of size 'tx_width_unit x
848   // tx_height_unit'. But, for this transform size, block_idx = 2 corresponds to
849   // block number 1 in raster order, inside an 8x8 MI block.
850   // For any other transform size, the two indices are equivalent.
851   return (tx_size == TX_4X8 && block_idx == 2) ? 1 : block_idx;
852 }
853 
854 // Inverse of above function.
855 // Note: only implemented for transform sizes 4x4, 4x8 and 8x4 right now.
av1_raster_order_to_block_index(TX_SIZE tx_size,int raster_order)856 static INLINE int av1_raster_order_to_block_index(TX_SIZE tx_size,
857                                                   int raster_order) {
858   assert(tx_size == TX_4X4 || tx_size == TX_4X8 || tx_size == TX_8X4);
859   // We ensure that block indices are 0 & 2 if tx size is 4x8 or 8x4.
860   return (tx_size == TX_4X4) ? raster_order : (raster_order > 0) ? 2 : 0;
861 }
862 
get_default_tx_type(PLANE_TYPE plane_type,const MACROBLOCKD * xd,TX_SIZE tx_size,int is_screen_content_type)863 static INLINE TX_TYPE get_default_tx_type(PLANE_TYPE plane_type,
864                                           const MACROBLOCKD *xd,
865                                           TX_SIZE tx_size,
866                                           int is_screen_content_type) {
867   const MB_MODE_INFO *const mbmi = xd->mi[0];
868 
869   if (is_inter_block(mbmi) || plane_type != PLANE_TYPE_Y ||
870       xd->lossless[mbmi->segment_id] || tx_size >= TX_32X32 ||
871       is_screen_content_type)
872     return DCT_DCT;
873 
874   return intra_mode_to_tx_type(mbmi, plane_type);
875 }
876 
877 // Implements the get_plane_residual_size() function in the spec (Section
878 // 5.11.38. Get plane residual size function).
get_plane_block_size(BLOCK_SIZE bsize,int subsampling_x,int subsampling_y)879 static INLINE BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
880                                               int subsampling_x,
881                                               int subsampling_y) {
882   assert(bsize < BLOCK_SIZES_ALL);
883   assert(subsampling_x >= 0 && subsampling_x < 2);
884   assert(subsampling_y >= 0 && subsampling_y < 2);
885   return ss_size_lookup[bsize][subsampling_x][subsampling_y];
886 }
887 
888 /*
889  * Logic to generate the lookup tables:
890  *
891  * TX_SIZE txs = max_txsize_rect_lookup[bsize];
892  * for (int level = 0; level < MAX_VARTX_DEPTH - 1; ++level)
893  *   txs = sub_tx_size_map[txs];
894  * const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
895  * const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
896  * const int bw_uint_log2 = mi_size_wide_log2[bsize];
897  * const int stride_log2 = bw_uint_log2 - tx_w_log2;
898  */
av1_get_txb_size_index(BLOCK_SIZE bsize,int blk_row,int blk_col)899 static INLINE int av1_get_txb_size_index(BLOCK_SIZE bsize, int blk_row,
900                                          int blk_col) {
901   static const uint8_t tw_w_log2_table[BLOCK_SIZES_ALL] = {
902     0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 0, 1, 1, 2, 2, 3,
903   };
904   static const uint8_t tw_h_log2_table[BLOCK_SIZES_ALL] = {
905     0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 1, 0, 2, 1, 3, 2,
906   };
907   static const uint8_t stride_log2_table[BLOCK_SIZES_ALL] = {
908     0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 2, 2, 0, 1, 0, 1, 0, 1,
909   };
910   const int index =
911       ((blk_row >> tw_h_log2_table[bsize]) << stride_log2_table[bsize]) +
912       (blk_col >> tw_w_log2_table[bsize]);
913   assert(index < INTER_TX_SIZE_BUF_LEN);
914   return index;
915 }
916 
917 #if CONFIG_INSPECTION
918 /*
919  * Here is the logic to generate the lookup tables:
920  *
921  * TX_SIZE txs = max_txsize_rect_lookup[bsize];
922  * for (int level = 0; level < MAX_VARTX_DEPTH; ++level)
923  *   txs = sub_tx_size_map[txs];
924  * const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
925  * const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
926  * const int bw_uint_log2 = mi_size_wide_log2[bsize];
927  * const int stride_log2 = bw_uint_log2 - tx_w_log2;
928  */
av1_get_txk_type_index(BLOCK_SIZE bsize,int blk_row,int blk_col)929 static INLINE int av1_get_txk_type_index(BLOCK_SIZE bsize, int blk_row,
930                                          int blk_col) {
931   static const uint8_t tw_w_log2_table[BLOCK_SIZES_ALL] = {
932     0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0, 0, 1, 1, 2, 2,
933   };
934   static const uint8_t tw_h_log2_table[BLOCK_SIZES_ALL] = {
935     0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0, 0, 1, 1, 2, 2,
936   };
937   static const uint8_t stride_log2_table[BLOCK_SIZES_ALL] = {
938     0, 0, 1, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 2, 3, 3, 0, 2, 0, 2, 0, 2,
939   };
940   const int index =
941       ((blk_row >> tw_h_log2_table[bsize]) << stride_log2_table[bsize]) +
942       (blk_col >> tw_w_log2_table[bsize]);
943   assert(index < TXK_TYPE_BUF_LEN);
944   return index;
945 }
946 #endif  // CONFIG_INSPECTION
947 
update_txk_array(MACROBLOCKD * const xd,int blk_row,int blk_col,TX_SIZE tx_size,TX_TYPE tx_type)948 static INLINE void update_txk_array(MACROBLOCKD *const xd, int blk_row,
949                                     int blk_col, TX_SIZE tx_size,
950                                     TX_TYPE tx_type) {
951   const int stride = xd->tx_type_map_stride;
952   xd->tx_type_map[blk_row * stride + blk_col] = tx_type;
953 
954   const int txw = tx_size_wide_unit[tx_size];
955   const int txh = tx_size_high_unit[tx_size];
956   // The 16x16 unit is due to the constraint from tx_64x64 which sets the
957   // maximum tx size for chroma as 32x32. Coupled with 4x1 transform block
958   // size, the constraint takes effect in 32x16 / 16x32 size too. To solve
959   // the intricacy, cover all the 16x16 units inside a 64 level transform.
960   if (txw == tx_size_wide_unit[TX_64X64] ||
961       txh == tx_size_high_unit[TX_64X64]) {
962     const int tx_unit = tx_size_wide_unit[TX_16X16];
963     for (int idy = 0; idy < txh; idy += tx_unit) {
964       for (int idx = 0; idx < txw; idx += tx_unit) {
965         xd->tx_type_map[(blk_row + idy) * stride + blk_col + idx] = tx_type;
966       }
967     }
968   }
969 }
970 
av1_get_tx_type(const MACROBLOCKD * xd,PLANE_TYPE plane_type,int blk_row,int blk_col,TX_SIZE tx_size,int reduced_tx_set)971 static INLINE TX_TYPE av1_get_tx_type(const MACROBLOCKD *xd,
972                                       PLANE_TYPE plane_type, int blk_row,
973                                       int blk_col, TX_SIZE tx_size,
974                                       int reduced_tx_set) {
975   const MB_MODE_INFO *const mbmi = xd->mi[0];
976   if (xd->lossless[mbmi->segment_id] || txsize_sqr_up_map[tx_size] > TX_32X32) {
977     return DCT_DCT;
978   }
979 
980   TX_TYPE tx_type;
981   if (plane_type == PLANE_TYPE_Y) {
982     tx_type = xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col];
983   } else {
984     if (is_inter_block(mbmi)) {
985       // scale back to y plane's coordinate
986       const struct macroblockd_plane *const pd = &xd->plane[plane_type];
987       blk_row <<= pd->subsampling_y;
988       blk_col <<= pd->subsampling_x;
989       tx_type = xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col];
990     } else {
991       // In intra mode, uv planes don't share the same prediction mode as y
992       // plane, so the tx_type should not be shared
993       tx_type = intra_mode_to_tx_type(mbmi, PLANE_TYPE_UV);
994     }
995     const TxSetType tx_set_type =
996         av1_get_ext_tx_set_type(tx_size, is_inter_block(mbmi), reduced_tx_set);
997     if (!av1_ext_tx_used[tx_set_type][tx_type]) tx_type = DCT_DCT;
998   }
999   assert(tx_type < TX_TYPES);
1000   assert(av1_ext_tx_used[av1_get_ext_tx_set_type(tx_size, is_inter_block(mbmi),
1001                                                  reduced_tx_set)][tx_type]);
1002   return tx_type;
1003 }
1004 
1005 void av1_setup_block_planes(MACROBLOCKD *xd, int ss_x, int ss_y,
1006                             const int num_planes);
1007 
1008 /*
1009  * Logic to generate the lookup table:
1010  *
1011  * TX_SIZE tx_size = max_txsize_rect_lookup[bsize];
1012  * int depth = 0;
1013  * while (depth < MAX_TX_DEPTH && tx_size != TX_4X4) {
1014  *   depth++;
1015  *   tx_size = sub_tx_size_map[tx_size];
1016  * }
1017  */
bsize_to_max_depth(BLOCK_SIZE bsize)1018 static INLINE int bsize_to_max_depth(BLOCK_SIZE bsize) {
1019   static const uint8_t bsize_to_max_depth_table[BLOCK_SIZES_ALL] = {
1020     0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1021   };
1022   return bsize_to_max_depth_table[bsize];
1023 }
1024 
1025 /*
1026  * Logic to generate the lookup table:
1027  *
1028  * TX_SIZE tx_size = max_txsize_rect_lookup[bsize];
1029  * assert(tx_size != TX_4X4);
1030  * int depth = 0;
1031  * while (tx_size != TX_4X4) {
1032  *   depth++;
1033  *   tx_size = sub_tx_size_map[tx_size];
1034  * }
1035  * assert(depth < 10);
1036  */
bsize_to_tx_size_cat(BLOCK_SIZE bsize)1037 static INLINE int bsize_to_tx_size_cat(BLOCK_SIZE bsize) {
1038   assert(bsize < BLOCK_SIZES_ALL);
1039   static const uint8_t bsize_to_tx_size_depth_table[BLOCK_SIZES_ALL] = {
1040     0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 2, 2, 3, 3, 4, 4,
1041   };
1042   const int depth = bsize_to_tx_size_depth_table[bsize];
1043   assert(depth <= MAX_TX_CATS);
1044   return depth - 1;
1045 }
1046 
depth_to_tx_size(int depth,BLOCK_SIZE bsize)1047 static INLINE TX_SIZE depth_to_tx_size(int depth, BLOCK_SIZE bsize) {
1048   TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
1049   TX_SIZE tx_size = max_tx_size;
1050   for (int d = 0; d < depth; ++d) tx_size = sub_tx_size_map[tx_size];
1051   return tx_size;
1052 }
1053 
av1_get_adjusted_tx_size(TX_SIZE tx_size)1054 static INLINE TX_SIZE av1_get_adjusted_tx_size(TX_SIZE tx_size) {
1055   switch (tx_size) {
1056     case TX_64X64:
1057     case TX_64X32:
1058     case TX_32X64: return TX_32X32;
1059     case TX_64X16: return TX_32X16;
1060     case TX_16X64: return TX_16X32;
1061     default: return tx_size;
1062   }
1063 }
1064 
av1_get_max_uv_txsize(BLOCK_SIZE bsize,int subsampling_x,int subsampling_y)1065 static INLINE TX_SIZE av1_get_max_uv_txsize(BLOCK_SIZE bsize, int subsampling_x,
1066                                             int subsampling_y) {
1067   const BLOCK_SIZE plane_bsize =
1068       get_plane_block_size(bsize, subsampling_x, subsampling_y);
1069   assert(plane_bsize < BLOCK_SIZES_ALL);
1070   const TX_SIZE uv_tx = max_txsize_rect_lookup[plane_bsize];
1071   return av1_get_adjusted_tx_size(uv_tx);
1072 }
1073 
av1_get_tx_size(int plane,const MACROBLOCKD * xd)1074 static INLINE TX_SIZE av1_get_tx_size(int plane, const MACROBLOCKD *xd) {
1075   const MB_MODE_INFO *mbmi = xd->mi[0];
1076   if (xd->lossless[mbmi->segment_id]) return TX_4X4;
1077   if (plane == 0) return mbmi->tx_size;
1078   const MACROBLOCKD_PLANE *pd = &xd->plane[plane];
1079   return av1_get_max_uv_txsize(mbmi->sb_type, pd->subsampling_x,
1080                                pd->subsampling_y);
1081 }
1082 
1083 void av1_reset_entropy_context(MACROBLOCKD *xd, BLOCK_SIZE bsize,
1084                                const int num_planes);
1085 
1086 void av1_reset_loop_filter_delta(MACROBLOCKD *xd, int num_planes);
1087 
1088 void av1_reset_loop_restoration(MACROBLOCKD *xd, const int num_planes);
1089 
1090 typedef void (*foreach_transformed_block_visitor)(int plane, int block,
1091                                                   int blk_row, int blk_col,
1092                                                   BLOCK_SIZE plane_bsize,
1093                                                   TX_SIZE tx_size, void *arg);
1094 
1095 void av1_set_entropy_contexts(const MACROBLOCKD *xd,
1096                               struct macroblockd_plane *pd, int plane,
1097                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
1098                               int has_eob, int aoff, int loff);
1099 
1100 #define MAX_INTERINTRA_SB_SQUARE 32 * 32
is_interintra_mode(const MB_MODE_INFO * mbmi)1101 static INLINE int is_interintra_mode(const MB_MODE_INFO *mbmi) {
1102   return (mbmi->ref_frame[0] > INTRA_FRAME &&
1103           mbmi->ref_frame[1] == INTRA_FRAME);
1104 }
1105 
is_interintra_allowed_bsize(const BLOCK_SIZE bsize)1106 static INLINE int is_interintra_allowed_bsize(const BLOCK_SIZE bsize) {
1107   return (bsize >= BLOCK_8X8) && (bsize <= BLOCK_32X32);
1108 }
1109 
is_interintra_allowed_mode(const PREDICTION_MODE mode)1110 static INLINE int is_interintra_allowed_mode(const PREDICTION_MODE mode) {
1111   return (mode >= SINGLE_INTER_MODE_START) && (mode < SINGLE_INTER_MODE_END);
1112 }
1113 
is_interintra_allowed_ref(const MV_REFERENCE_FRAME rf[2])1114 static INLINE int is_interintra_allowed_ref(const MV_REFERENCE_FRAME rf[2]) {
1115   return (rf[0] > INTRA_FRAME) && (rf[1] <= INTRA_FRAME);
1116 }
1117 
is_interintra_allowed(const MB_MODE_INFO * mbmi)1118 static INLINE int is_interintra_allowed(const MB_MODE_INFO *mbmi) {
1119   return is_interintra_allowed_bsize(mbmi->sb_type) &&
1120          is_interintra_allowed_mode(mbmi->mode) &&
1121          is_interintra_allowed_ref(mbmi->ref_frame);
1122 }
1123 
is_interintra_allowed_bsize_group(int group)1124 static INLINE int is_interintra_allowed_bsize_group(int group) {
1125   int i;
1126   for (i = 0; i < BLOCK_SIZES_ALL; i++) {
1127     if (size_group_lookup[i] == group &&
1128         is_interintra_allowed_bsize((BLOCK_SIZE)i)) {
1129       return 1;
1130     }
1131   }
1132   return 0;
1133 }
1134 
is_interintra_pred(const MB_MODE_INFO * mbmi)1135 static INLINE int is_interintra_pred(const MB_MODE_INFO *mbmi) {
1136   return mbmi->ref_frame[0] > INTRA_FRAME &&
1137          mbmi->ref_frame[1] == INTRA_FRAME && is_interintra_allowed(mbmi);
1138 }
1139 
get_vartx_max_txsize(const MACROBLOCKD * xd,BLOCK_SIZE bsize,int plane)1140 static INLINE int get_vartx_max_txsize(const MACROBLOCKD *xd, BLOCK_SIZE bsize,
1141                                        int plane) {
1142   if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;
1143   const TX_SIZE max_txsize = max_txsize_rect_lookup[bsize];
1144   if (plane == 0) return max_txsize;            // luma
1145   return av1_get_adjusted_tx_size(max_txsize);  // chroma
1146 }
1147 
is_motion_variation_allowed_bsize(BLOCK_SIZE bsize)1148 static INLINE int is_motion_variation_allowed_bsize(BLOCK_SIZE bsize) {
1149   assert(bsize < BLOCK_SIZES_ALL);
1150   return AOMMIN(block_size_wide[bsize], block_size_high[bsize]) >= 8;
1151 }
1152 
is_motion_variation_allowed_compound(const MB_MODE_INFO * mbmi)1153 static INLINE int is_motion_variation_allowed_compound(
1154     const MB_MODE_INFO *mbmi) {
1155   return !has_second_ref(mbmi);
1156 }
1157 
1158 // input: log2 of length, 0(4), 1(8), ...
1159 static const int max_neighbor_obmc[6] = { 0, 1, 2, 3, 4, 4 };
1160 
check_num_overlappable_neighbors(const MB_MODE_INFO * mbmi)1161 static INLINE int check_num_overlappable_neighbors(const MB_MODE_INFO *mbmi) {
1162   return !(mbmi->overlappable_neighbors[0] == 0 &&
1163            mbmi->overlappable_neighbors[1] == 0);
1164 }
1165 
1166 static INLINE MOTION_MODE
motion_mode_allowed(const WarpedMotionParams * gm_params,const MACROBLOCKD * xd,const MB_MODE_INFO * mbmi,int allow_warped_motion)1167 motion_mode_allowed(const WarpedMotionParams *gm_params, const MACROBLOCKD *xd,
1168                     const MB_MODE_INFO *mbmi, int allow_warped_motion) {
1169   if (xd->cur_frame_force_integer_mv == 0) {
1170     const TransformationType gm_type = gm_params[mbmi->ref_frame[0]].wmtype;
1171     if (is_global_mv_block(mbmi, gm_type)) return SIMPLE_TRANSLATION;
1172   }
1173   if (is_motion_variation_allowed_bsize(mbmi->sb_type) &&
1174       is_inter_mode(mbmi->mode) && mbmi->ref_frame[1] != INTRA_FRAME &&
1175       is_motion_variation_allowed_compound(mbmi)) {
1176     if (!check_num_overlappable_neighbors(mbmi)) return SIMPLE_TRANSLATION;
1177     assert(!has_second_ref(mbmi));
1178     if (mbmi->num_proj_ref >= 1 &&
1179         (allow_warped_motion &&
1180          !av1_is_scaled(xd->block_ref_scale_factors[0]))) {
1181       if (xd->cur_frame_force_integer_mv) {
1182         return OBMC_CAUSAL;
1183       }
1184       return WARPED_CAUSAL;
1185     }
1186     return OBMC_CAUSAL;
1187   } else {
1188     return SIMPLE_TRANSLATION;
1189   }
1190 }
1191 
is_neighbor_overlappable(const MB_MODE_INFO * mbmi)1192 static INLINE int is_neighbor_overlappable(const MB_MODE_INFO *mbmi) {
1193   return (is_inter_block(mbmi));
1194 }
1195 
av1_allow_palette(int allow_screen_content_tools,BLOCK_SIZE sb_type)1196 static INLINE int av1_allow_palette(int allow_screen_content_tools,
1197                                     BLOCK_SIZE sb_type) {
1198   assert(sb_type < BLOCK_SIZES_ALL);
1199   return allow_screen_content_tools && block_size_wide[sb_type] <= 64 &&
1200          block_size_high[sb_type] <= 64 && sb_type >= BLOCK_8X8;
1201 }
1202 
1203 // Returns sub-sampled dimensions of the given block.
1204 // The output values for 'rows_within_bounds' and 'cols_within_bounds' will
1205 // differ from 'height' and 'width' when part of the block is outside the
1206 // right
1207 // and/or bottom image boundary.
av1_get_block_dimensions(BLOCK_SIZE bsize,int plane,const MACROBLOCKD * xd,int * width,int * height,int * rows_within_bounds,int * cols_within_bounds)1208 static INLINE void av1_get_block_dimensions(BLOCK_SIZE bsize, int plane,
1209                                             const MACROBLOCKD *xd, int *width,
1210                                             int *height,
1211                                             int *rows_within_bounds,
1212                                             int *cols_within_bounds) {
1213   const int block_height = block_size_high[bsize];
1214   const int block_width = block_size_wide[bsize];
1215   const int block_rows = (xd->mb_to_bottom_edge >= 0)
1216                              ? block_height
1217                              : (xd->mb_to_bottom_edge >> 3) + block_height;
1218   const int block_cols = (xd->mb_to_right_edge >= 0)
1219                              ? block_width
1220                              : (xd->mb_to_right_edge >> 3) + block_width;
1221   const struct macroblockd_plane *const pd = &xd->plane[plane];
1222   assert(IMPLIES(plane == PLANE_TYPE_Y, pd->subsampling_x == 0));
1223   assert(IMPLIES(plane == PLANE_TYPE_Y, pd->subsampling_y == 0));
1224   assert(block_width >= block_cols);
1225   assert(block_height >= block_rows);
1226   const int plane_block_width = block_width >> pd->subsampling_x;
1227   const int plane_block_height = block_height >> pd->subsampling_y;
1228   // Special handling for chroma sub8x8.
1229   const int is_chroma_sub8_x = plane > 0 && plane_block_width < 4;
1230   const int is_chroma_sub8_y = plane > 0 && plane_block_height < 4;
1231   if (width) *width = plane_block_width + 2 * is_chroma_sub8_x;
1232   if (height) *height = plane_block_height + 2 * is_chroma_sub8_y;
1233   if (rows_within_bounds) {
1234     *rows_within_bounds =
1235         (block_rows >> pd->subsampling_y) + 2 * is_chroma_sub8_y;
1236   }
1237   if (cols_within_bounds) {
1238     *cols_within_bounds =
1239         (block_cols >> pd->subsampling_x) + 2 * is_chroma_sub8_x;
1240   }
1241 }
1242 
1243 /* clang-format off */
1244 typedef aom_cdf_prob (*MapCdf)[PALETTE_COLOR_INDEX_CONTEXTS]
1245                               [CDF_SIZE(PALETTE_COLORS)];
1246 typedef const int (*ColorCost)[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
1247                               [PALETTE_COLORS];
1248 /* clang-format on */
1249 
1250 typedef struct {
1251   int rows;
1252   int cols;
1253   int n_colors;
1254   int plane_width;
1255   int plane_height;
1256   uint8_t *color_map;
1257   MapCdf map_cdf;
1258   ColorCost color_cost;
1259 } Av1ColorMapParam;
1260 
is_nontrans_global_motion(const MACROBLOCKD * xd,const MB_MODE_INFO * mbmi)1261 static INLINE int is_nontrans_global_motion(const MACROBLOCKD *xd,
1262                                             const MB_MODE_INFO *mbmi) {
1263   int ref;
1264 
1265   // First check if all modes are GLOBALMV
1266   if (mbmi->mode != GLOBALMV && mbmi->mode != GLOBAL_GLOBALMV) return 0;
1267 
1268   if (AOMMIN(mi_size_wide[mbmi->sb_type], mi_size_high[mbmi->sb_type]) < 2)
1269     return 0;
1270 
1271   // Now check if all global motion is non translational
1272   for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1273     if (xd->global_motion[mbmi->ref_frame[ref]].wmtype == TRANSLATION) return 0;
1274   }
1275   return 1;
1276 }
1277 
get_plane_type(int plane)1278 static INLINE PLANE_TYPE get_plane_type(int plane) {
1279   return (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
1280 }
1281 
av1_get_max_eob(TX_SIZE tx_size)1282 static INLINE int av1_get_max_eob(TX_SIZE tx_size) {
1283   if (tx_size == TX_64X64 || tx_size == TX_64X32 || tx_size == TX_32X64) {
1284     return 1024;
1285   }
1286   if (tx_size == TX_16X64 || tx_size == TX_64X16) {
1287     return 512;
1288   }
1289   return tx_size_2d[tx_size];
1290 }
1291 
1292 #ifdef __cplusplus
1293 }  // extern "C"
1294 #endif
1295 
1296 #endif  // AOM_AV1_COMMON_BLOCKD_H_
1297