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_ENCODER_BLOCK_H_
13 #define AOM_AV1_ENCODER_BLOCK_H_
14 
15 #include "av1/common/entropymv.h"
16 #include "av1/common/entropy.h"
17 #include "av1/common/mvref_common.h"
18 
19 #include "av1/encoder/enc_enums.h"
20 #if !CONFIG_REALTIME_ONLY
21 #include "av1/encoder/partition_cnn_weights.h"
22 #endif
23 
24 #include "av1/encoder/hash.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define MC_FLOW_BSIZE_1D 16
31 #define MC_FLOW_NUM_PELS (MC_FLOW_BSIZE_1D * MC_FLOW_BSIZE_1D)
32 #define MAX_MC_FLOW_BLK_IN_SB (MAX_SB_SIZE / MC_FLOW_BSIZE_1D)
33 #define MAX_WINNER_MODE_COUNT_INTRA 3
34 #define MAX_WINNER_MODE_COUNT_INTER 1
35 typedef struct {
36   MB_MODE_INFO mbmi;
37   RD_STATS rd_cost;
38   int64_t rd;
39   int rate_y;
40   int rate_uv;
41   uint8_t color_index_map[64 * 64];
42   THR_MODES mode_index;
43 } WinnerModeStats;
44 
45 typedef struct {
46   unsigned int sse;
47   int sum;
48   unsigned int var;
49 } DIFF;
50 
51 enum {
52   NO_TRELLIS_OPT,          // No trellis optimization
53   FULL_TRELLIS_OPT,        // Trellis optimization in all stages
54   FINAL_PASS_TRELLIS_OPT,  // Trellis optimization in only the final encode pass
55   NO_ESTIMATE_YRD_TRELLIS_OPT  // Disable trellis in estimate_yrd_for_sb
56 } UENUM1BYTE(TRELLIS_OPT_TYPE);
57 
58 typedef struct macroblock_plane {
59   DECLARE_ALIGNED(32, int16_t, src_diff[MAX_SB_SQUARE]);
60   tran_low_t *qcoeff;
61   tran_low_t *coeff;
62   uint16_t *eobs;
63   uint8_t *txb_entropy_ctx;
64   struct buf_2d src;
65 
66   // Quantizer setings
67   // These are used/accessed only in the quantization process
68   // RDO does not / must not depend on any of these values
69   // All values below share the coefficient scale/shift used in TX
70   const int16_t *quant_fp_QTX;
71   const int16_t *round_fp_QTX;
72   const int16_t *quant_QTX;
73   const int16_t *quant_shift_QTX;
74   const int16_t *zbin_QTX;
75   const int16_t *round_QTX;
76   const int16_t *dequant_QTX;
77 } MACROBLOCK_PLANE;
78 
79 typedef struct {
80   int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
81   int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
82   int base_cost[SIG_COEF_CONTEXTS][8];
83   int eob_extra_cost[EOB_COEF_CONTEXTS][2];
84   int dc_sign_cost[DC_SIGN_CONTEXTS][2];
85   int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1];
86 } LV_MAP_COEFF_COST;
87 
88 typedef struct {
89   int eob_cost[2][11];
90 } LV_MAP_EOB_COST;
91 
92 typedef struct {
93   tran_low_t tcoeff[MAX_MB_PLANE][MAX_SB_SQUARE];
94   uint16_t eobs[MAX_MB_PLANE][MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
95   // Transform block entropy contexts.
96   // Bits 0~3: txb_skip_ctx; bits 4~5: dc_sign_ctx.
97   uint8_t entropy_ctx[MAX_MB_PLANE]
98                      [MAX_SB_SQUARE / (TX_SIZE_W_MIN * TX_SIZE_H_MIN)];
99 } CB_COEFF_BUFFER;
100 
101 typedef struct {
102   // TODO(angiebird): Reduce the buffer size according to sb_type
103   CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
104   uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
105   int_mv global_mvs[REF_FRAMES];
106   int16_t mode_context[MODE_CTX_REF_FRAMES];
107   uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
108 } MB_MODE_INFO_EXT;
109 
110 // Structure to store best mode information at frame level. This
111 // frame level information will be used during bitstream preparation stage.
112 typedef struct {
113   CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE];
114   uint16_t weight[USABLE_REF_MV_STACK_SIZE];
115   // TODO(Ravi/Remya): Reduce the buffer size of global_mvs
116   int_mv global_mvs[REF_FRAMES];
117   int cb_offset;
118   int16_t mode_context;
119   uint8_t ref_mv_count;
120 } MB_MODE_INFO_EXT_FRAME;
121 
122 typedef struct {
123   uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
124   int kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
125 } PALETTE_BUFFER;
126 
127 typedef struct {
128   TX_SIZE tx_size;
129   TX_SIZE inter_tx_size[INTER_TX_SIZE_BUF_LEN];
130   uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
131   uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
132   RD_STATS rd_stats;
133   uint32_t hash_value;
134 } MB_RD_INFO;
135 
136 #define RD_RECORD_BUFFER_LEN 8
137 typedef struct {
138   MB_RD_INFO tx_rd_info[RD_RECORD_BUFFER_LEN];  // Circular buffer.
139   int index_start;
140   int num;
141   CRC32C crc_calculator;  // Hash function.
142 } MB_RD_RECORD;
143 
144 typedef struct {
145   int64_t dist;
146   int64_t sse;
147   int rate;
148   uint16_t eob;
149   TX_TYPE tx_type;
150   uint16_t entropy_context;
151   uint8_t txb_entropy_ctx;
152   uint8_t valid;
153   uint8_t fast;  // This is not being used now.
154   uint8_t perform_block_coeff_opt;
155 } TXB_RD_INFO;
156 
157 #define TX_SIZE_RD_RECORD_BUFFER_LEN 256
158 typedef struct {
159   uint32_t hash_vals[TX_SIZE_RD_RECORD_BUFFER_LEN];
160   TXB_RD_INFO tx_rd_info[TX_SIZE_RD_RECORD_BUFFER_LEN];
161   int index_start;
162   int num;
163 } TXB_RD_RECORD;
164 
165 typedef struct tx_size_rd_info_node {
166   TXB_RD_INFO *rd_info_array;  // Points to array of size TX_TYPES.
167   struct tx_size_rd_info_node *children[4];
168 } TXB_RD_INFO_NODE;
169 
170 // Simple translation rd state for prune_comp_search_by_single_result
171 typedef struct {
172   RD_STATS rd_stats;
173   RD_STATS rd_stats_y;
174   RD_STATS rd_stats_uv;
175   uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
176   uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
177   uint8_t skip;
178   uint8_t disable_skip;
179   uint8_t early_skipped;
180 } SimpleRDState;
181 
182 // 4: NEAREST, NEW, NEAR, GLOBAL
183 #define SINGLE_REF_MODES ((REF_FRAMES - 1) * 4)
184 
185 #define MAX_COMP_RD_STATS 64
186 typedef struct {
187   int32_t rate[COMPOUND_TYPES];
188   int64_t dist[COMPOUND_TYPES];
189   int32_t model_rate[COMPOUND_TYPES];
190   int64_t model_dist[COMPOUND_TYPES];
191   int comp_rs2[COMPOUND_TYPES];
192   int_mv mv[2];
193   MV_REFERENCE_FRAME ref_frames[2];
194   PREDICTION_MODE mode;
195   int_interpfilters filter;
196   int ref_mv_idx;
197   int is_global[2];
198   INTERINTER_COMPOUND_DATA interinter_comp;
199 } COMP_RD_STATS;
200 
201 // Struct for buffers used by av1_compound_type_rd() function.
202 // For sizes and alignment of these arrays, refer to
203 // alloc_compound_type_rd_buffers() function.
204 typedef struct {
205   uint8_t *pred0;
206   uint8_t *pred1;
207   int16_t *residual1;          // src - pred1
208   int16_t *diff10;             // pred1 - pred0
209   uint8_t *tmp_best_mask_buf;  // backup of the best segmentation mask
210 } CompoundTypeRdBuffers;
211 
212 enum {
213   MV_COST_ENTROPY,    // Use the entropy rate of the mv as the cost
214   MV_COST_L1_LOWRES,  // Use the l1 norm of the mv as the cost (<480p)
215   MV_COST_L1_MIDRES,  // Use the l1 norm of the mv as the cost (>=480p)
216   MV_COST_L1_HDRES,   // Use the l1 norm of the mv as the cost (>=720p)
217   MV_COST_NONE        // Use 0 as as cost irrespective of the current mv
218 } UENUM1BYTE(MV_COST_TYPE);
219 
220 struct inter_modes_info;
221 typedef struct macroblock MACROBLOCK;
222 struct macroblock {
223   struct macroblock_plane plane[MAX_MB_PLANE];
224 
225   // Determine if one would go with reduced complexity transform block
226   // search model to select prediction modes, or full complexity model
227   // to select transform kernel.
228   int rd_model;
229 
230   // prune_comp_search_by_single_result (3:MAX_REF_MV_SEARCH)
231   SimpleRDState simple_rd_state[SINGLE_REF_MODES][3];
232 
233   // Inter macroblock RD search info.
234   MB_RD_RECORD mb_rd_record;
235 
236   // Inter transform block RD search info. for square TX sizes.
237   TXB_RD_RECORD txb_rd_record_8X8[(MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1)];
238   TXB_RD_RECORD txb_rd_record_16X16[(MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2)];
239   TXB_RD_RECORD txb_rd_record_32X32[(MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3)];
240   TXB_RD_RECORD txb_rd_record_64X64[(MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4)];
241 
242   // Intra transform block RD search info. for square TX sizes.
243   TXB_RD_RECORD txb_rd_record_intra;
244 
245   MACROBLOCKD e_mbd;
246   MB_MODE_INFO_EXT *mbmi_ext;
247   MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame;
248   // Array of mode stats for winner mode processing
249   WinnerModeStats winner_mode_stats[AOMMAX(MAX_WINNER_MODE_COUNT_INTRA,
250                                            MAX_WINNER_MODE_COUNT_INTER)];
251   int winner_mode_count;
252   int skip_block;
253   int qindex;
254 
255   // The equivalent error at the current rdmult of one whole bit (not one
256   // bitcost unit).
257   int errorperbit;
258   // The equivalend SAD error of one (whole) bit at the current quantizer
259   // for large blocks.
260   int sadperbit;
261   int rdmult;
262   int mb_energy;
263   int sb_energy_level;
264 
265   unsigned int txb_split_count;
266 #if CONFIG_SPEED_STATS
267   unsigned int tx_search_count;
268 #endif  // CONFIG_SPEED_STATS
269 
270   // These are set to their default values at the beginning, and then adjusted
271   // further in the encoding process.
272   BLOCK_SIZE min_partition_size;
273   BLOCK_SIZE max_partition_size;
274 
275   unsigned int max_mv_context[REF_FRAMES];
276   unsigned int source_variance;
277   unsigned int simple_motion_pred_sse;
278   unsigned int pred_sse[REF_FRAMES];
279   int pred_mv_sad[REF_FRAMES];
280   int best_pred_mv_sad;
281 
282   int nmv_vec_cost[MV_JOINTS];
283   int nmv_costs[2][MV_VALS];
284   int nmv_costs_hp[2][MV_VALS];
285   int *nmvcost[2];
286   int *nmvcost_hp[2];
287   int **mv_cost_stack;
288 
289   int32_t *wsrc_buf;
290   int32_t *mask_buf;
291   uint8_t *above_pred_buf;
292   uint8_t *left_pred_buf;
293 
294   PALETTE_BUFFER *palette_buffer;
295   CompoundTypeRdBuffers comp_rd_buffer;
296 
297   CONV_BUF_TYPE *tmp_conv_dst;
298   uint8_t *tmp_obmc_bufs[2];
299 
300   FRAME_CONTEXT *row_ctx;
301   // This context will be used to update color_map_cdf pointer which would be
302   // used during pack bitstream. For single thread and tile-multithreading case
303   // this ponter will be same as xd->tile_ctx, but for the case of row-mt:
304   // xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
305   // to the accurate tile context.
306   FRAME_CONTEXT *tile_pb_ctx;
307 
308   struct inter_modes_info *inter_modes_info;
309 
310   // Contains the hash table, hash function, and buffer used for intrabc
311   IntraBCHashInfo intrabc_hash_info;
312 
313   // These define limits to motion vector components to prevent them
314   // from extending outside the UMV borders
315   FullMvLimits mv_limits;
316 
317   uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
318   uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
319 
320   // Force the coding block to skip transform and quantization.
321   int force_skip;
322   int skip_cost[SKIP_CONTEXTS][2];
323 
324   int skip_mode;  // 0: off; 1: on
325   int skip_mode_cost[SKIP_CONTEXTS][2];
326 
327   LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
328   LV_MAP_EOB_COST eob_costs[7][2];
329   uint16_t cb_offset;
330 
331   // mode costs
332   int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
333 
334   int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
335   int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
336   int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
337   int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
338   int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
339 
340   int comp_inter_cost[COMP_INTER_CONTEXTS][2];
341   int single_ref_cost[REF_CONTEXTS][SINGLE_REFS - 1][2];
342   int comp_ref_type_cost[COMP_REF_TYPE_CONTEXTS]
343                         [CDF_SIZE(COMP_REFERENCE_TYPES)];
344   int uni_comp_ref_cost[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1]
345                        [CDF_SIZE(2)];
346   // Cost for signaling ref_frame[0] (LAST_FRAME, LAST2_FRAME, LAST3_FRAME or
347   // GOLDEN_FRAME) in bidir-comp mode.
348   int comp_ref_cost[REF_CONTEXTS][FWD_REFS - 1][2];
349   // Cost for signaling ref_frame[1] (ALTREF_FRAME, ALTREF2_FRAME, or
350   // BWDREF_FRAME) in bidir-comp mode.
351   int comp_bwdref_cost[REF_CONTEXTS][BWD_REFS - 1][2];
352   int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
353   int compound_type_cost[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
354   int wedge_idx_cost[BLOCK_SIZES_ALL][16];
355   int interintra_cost[BLOCK_SIZE_GROUPS][2];
356   int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
357   int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
358   int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
359   int motion_mode_cost1[BLOCK_SIZES_ALL][2];
360   int intra_uv_mode_cost[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
361   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
362   int filter_intra_cost[BLOCK_SIZES_ALL][2];
363   int filter_intra_mode_cost[FILTER_INTRA_MODES];
364   int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
365   int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
366   int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
367   int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
368   int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
369                           [PALETTE_COLORS];
370   int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
371                            [PALETTE_COLORS];
372   int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
373   int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
374   // The rate associated with each alpha codeword
375   int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
376   int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
377   int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
378   int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
379   int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
380                          [TX_TYPES];
381   int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
382   int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
383   int wiener_restore_cost[2];
384   int sgrproj_restore_cost[2];
385   int intrabc_cost[2];
386 
387   // Used to store sub partition's choices.
388   MV pred_mv[REF_FRAMES];
389 
390   // Ref frames that are selected by square partition blocks within a super-
391   // block, in MI resolution. They can be used to prune ref frames for
392   // rectangular blocks.
393   int picked_ref_frames_mask[32 * 32];
394 
395   // use default transform and skip transform type search for intra modes
396   int use_default_intra_tx_type;
397   // use default transform and skip transform type search for inter modes
398   int use_default_inter_tx_type;
399   int comp_idx_cost[COMP_INDEX_CONTEXTS][2];
400   int comp_group_idx_cost[COMP_GROUP_IDX_CONTEXTS][2];
401   int must_find_valid_partition;
402   int recalc_luma_mc_data;  // Flag to indicate recalculation of MC data during
403                             // interpolation filter search
404   int prune_mode;
405   uint32_t tx_domain_dist_threshold;
406   int use_transform_domain_distortion;
407   // The likelihood of an edge existing in the block (using partial Canny edge
408   // detection). For reference, 556 is the value returned for a solid
409   // vertical black/white edge.
410   uint16_t edge_strength;
411   // The strongest edge strength seen along the x/y axis.
412   uint16_t edge_strength_x;
413   uint16_t edge_strength_y;
414   uint8_t compound_idx;
415 
416   // [Saved stat index]
417   COMP_RD_STATS comp_rd_stats[MAX_COMP_RD_STATS];
418   int comp_rd_stats_idx;
419 
420   CB_COEFF_BUFFER *cb_coef_buff;
421 
422   // Threshold used to decide the applicability of R-D optimization of
423   // quantized coeffs
424   uint32_t coeff_opt_dist_threshold;
425 
426 #if !CONFIG_REALTIME_ONLY
427   int quad_tree_idx;
428   int cnn_output_valid;
429   float cnn_buffer[CNN_OUT_BUF_SIZE];
430   float log_q;
431 #endif
432   int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
433   // 0 - 128x128
434   // 1-2 - 128x64
435   // 3-4 - 64x128
436   // 5-8 - 64x64
437   // 9-16 - 64x32
438   // 17-24 - 32x64
439   // 25-40 - 32x32
440   // 41-104 - 16x16
441   uint8_t variance_low[105];
442   uint8_t content_state_sb;
443   // Strong color activity detection. Used in REALTIME coding mode to enhance
444   // the visual quality at the boundary of moving color objects.
445   uint8_t color_sensitivity[2];
446   int nonrd_prune_ref_frame_search;
447 
448   // Used to control the tx size search evaluation for mode processing
449   // (normal/winner mode)
450   int tx_size_search_method;
451   // This tx_mode_search_type is used internally by the encoder, and is not
452   // written to the bitstream. It determines what kind of tx_mode should be
453   // searched. For example, we might set it to TX_MODE_LARGEST to find a good
454   // candidate, then use TX_MODE_SELECT on it
455   TX_MODE tx_mode_search_type;
456 
457   // Used to control aggressiveness of skip flag prediction for mode processing
458   // (normal/winner mode)
459   unsigned int predict_skip_level;
460 
461   // Copy out this SB's TPL block stats.
462   int valid_cost_b;
463   int64_t inter_cost_b[MAX_MC_FLOW_BLK_IN_SB * MAX_MC_FLOW_BLK_IN_SB];
464   int64_t intra_cost_b[MAX_MC_FLOW_BLK_IN_SB * MAX_MC_FLOW_BLK_IN_SB];
465   int_mv mv_b[MAX_MC_FLOW_BLK_IN_SB * MAX_MC_FLOW_BLK_IN_SB]
466              [INTER_REFS_PER_FRAME];
467   int cost_stride;
468 
469   // The type of mv cost used during motion search
470   MV_COST_TYPE mv_cost_type;
471 
472   uint8_t search_ref_frame[REF_FRAMES];
473 
474 #if CONFIG_AV1_HIGHBITDEPTH
475   void (*fwd_txfm4x4)(const int16_t *input, tran_low_t *output, int stride);
476   void (*inv_txfm_add)(const tran_low_t *input, uint8_t *dest, int stride,
477                        int eob);
478 #else
479   void (*fwd_txfm4x4)(const int16_t *input, int16_t *output, int stride);
480   void (*inv_txfm_add)(const int16_t *input, uint8_t *dest, int stride,
481                        int eob);
482 #endif
483 };
484 
485 // Only consider full SB, MC_FLOW_BSIZE_1D = 16.
tpl_blocks_in_sb(BLOCK_SIZE bsize)486 static INLINE int tpl_blocks_in_sb(BLOCK_SIZE bsize) {
487   switch (bsize) {
488     case BLOCK_64X64: return 16;
489     case BLOCK_128X128: return 64;
490     default: assert(0);
491   }
492   return -1;
493 }
494 
is_rect_tx_allowed_bsize(BLOCK_SIZE bsize)495 static INLINE int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
496   static const char LUT[BLOCK_SIZES_ALL] = {
497     0,  // BLOCK_4X4
498     1,  // BLOCK_4X8
499     1,  // BLOCK_8X4
500     0,  // BLOCK_8X8
501     1,  // BLOCK_8X16
502     1,  // BLOCK_16X8
503     0,  // BLOCK_16X16
504     1,  // BLOCK_16X32
505     1,  // BLOCK_32X16
506     0,  // BLOCK_32X32
507     1,  // BLOCK_32X64
508     1,  // BLOCK_64X32
509     0,  // BLOCK_64X64
510     0,  // BLOCK_64X128
511     0,  // BLOCK_128X64
512     0,  // BLOCK_128X128
513     1,  // BLOCK_4X16
514     1,  // BLOCK_16X4
515     1,  // BLOCK_8X32
516     1,  // BLOCK_32X8
517     1,  // BLOCK_16X64
518     1,  // BLOCK_64X16
519   };
520 
521   return LUT[bsize];
522 }
523 
is_rect_tx_allowed(const MACROBLOCKD * xd,const MB_MODE_INFO * mbmi)524 static INLINE int is_rect_tx_allowed(const MACROBLOCKD *xd,
525                                      const MB_MODE_INFO *mbmi) {
526   return is_rect_tx_allowed_bsize(mbmi->sb_type) &&
527          !xd->lossless[mbmi->segment_id];
528 }
529 
tx_size_to_depth(TX_SIZE tx_size,BLOCK_SIZE bsize)530 static INLINE int tx_size_to_depth(TX_SIZE tx_size, BLOCK_SIZE bsize) {
531   TX_SIZE ctx_size = max_txsize_rect_lookup[bsize];
532   int depth = 0;
533   while (tx_size != ctx_size) {
534     depth++;
535     ctx_size = sub_tx_size_map[ctx_size];
536     assert(depth <= MAX_TX_DEPTH);
537   }
538   return depth;
539 }
540 
set_blk_skip(MACROBLOCK * x,int plane,int blk_idx,int skip)541 static INLINE void set_blk_skip(MACROBLOCK *x, int plane, int blk_idx,
542                                 int skip) {
543   if (skip)
544     x->blk_skip[blk_idx] |= 1UL << plane;
545   else
546     x->blk_skip[blk_idx] &= ~(1UL << plane);
547 #ifndef NDEBUG
548   // Set chroma planes to uninitialized states when luma is set to check if
549   // it will be set later
550   if (plane == 0) {
551     x->blk_skip[blk_idx] |= 1UL << (1 + 4);
552     x->blk_skip[blk_idx] |= 1UL << (2 + 4);
553   }
554 
555   // Clear the initialization checking bit
556   x->blk_skip[blk_idx] &= ~(1UL << (plane + 4));
557 #endif
558 }
559 
is_blk_skip(MACROBLOCK * x,int plane,int blk_idx)560 static INLINE int is_blk_skip(MACROBLOCK *x, int plane, int blk_idx) {
561 #ifndef NDEBUG
562   // Check if this is initialized
563   assert(!(x->blk_skip[blk_idx] & (1UL << (plane + 4))));
564 
565   // The magic number is 0x77, this is to test if there is garbage data
566   assert((x->blk_skip[blk_idx] & 0x88) == 0);
567 #endif
568   return (x->blk_skip[blk_idx] >> plane) & 1;
569 }
570 
571 #ifdef __cplusplus
572 }  // extern "C"
573 #endif
574 
575 #endif  // AOM_AV1_ENCODER_BLOCK_H_
576