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