• 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 /*! \file
13  * Declares various structs used to encode the current partition block.
14  */
15 #ifndef AOM_AV1_ENCODER_BLOCK_H_
16 #define AOM_AV1_ENCODER_BLOCK_H_
17 
18 #include "av1/common/blockd.h"
19 #include "av1/common/entropymv.h"
20 #include "av1/common/entropy.h"
21 #include "av1/common/enums.h"
22 #include "av1/common/mvref_common.h"
23 
24 #include "av1/encoder/enc_enums.h"
25 #include "av1/encoder/mcomp_structs.h"
26 #if !CONFIG_REALTIME_ONLY
27 #include "av1/encoder/partition_cnn_weights.h"
28 #endif
29 
30 #include "av1/encoder/hash_motion.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 //! Minimum linear dimension of a tpl block
37 #define MIN_TPL_BSIZE_1D 16
38 //! Maximum number of tpl block in a super block
39 #define MAX_TPL_BLK_IN_SB (MAX_SB_SIZE / MIN_TPL_BSIZE_1D)
40 //! Number of txfm hash records kept for the partition block.
41 #define RD_RECORD_BUFFER_LEN 8
42 
43 /*! Maximum value taken by transform type probabilities */
44 #define MAX_TX_TYPE_PROB 1024
45 /*! \brief Superblock level encoder info
46  *
47  * SuperblockEnc stores superblock level information used by the encoder for
48  * more efficient encoding. Currently this is mostly used to store TPL data
49  * for the current superblock.
50  */
51 typedef struct {
52   //! Maximum partition size for the sb.
53   BLOCK_SIZE min_partition_size;
54   //! Minimum partition size for the sb.
55   BLOCK_SIZE max_partition_size;
56 
57   /*****************************************************************************
58    * \name TPL Info
59    *
60    * Information gathered from tpl_model at tpl block precision for the
61    * superblock to speed up the encoding process..
62    ****************************************************************************/
63   /**@{*/
64   //! Number of TPL blocks in this superblock.
65   int tpl_data_count;
66   //! TPL's estimate of inter cost for each tpl block.
67   int64_t tpl_inter_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
68   //! TPL's estimate of tpl cost for each tpl block.
69   int64_t tpl_intra_cost[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB];
70   //! Motion vectors found by TPL model for each tpl block.
71   int_mv tpl_mv[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB][INTER_REFS_PER_FRAME];
72   //! TPL's stride for the arrays in this struct.
73   int tpl_stride;
74   /**@}*/
75 } SuperBlockEnc;
76 
77 /*! \brief Stores the best performing modes.
78  */
79 typedef struct {
80   //! The mbmi used to reconstruct the winner mode.
81   MB_MODE_INFO mbmi;
82   //! Rdstats of the winner mode.
83   RD_STATS rd_cost;
84   //! Rdcost of the winner mode
85   int64_t rd;
86   //! Luma rate of the winner mode.
87   int rate_y;
88   //! Chroma rate of the winner mode.
89   int rate_uv;
90   //! The color map needed to reconstruct palette mode.
91   uint8_t color_index_map[MAX_SB_SQUARE];
92   //! The current winner mode.
93   THR_MODES mode_index;
94 } WinnerModeStats;
95 
96 /*! \brief Each source plane of the current macroblock
97  *
98  * This struct also stores the txfm buffers and quantizer settings.
99  */
100 typedef struct macroblock_plane {
101   //! Stores source - pred so the txfm can be computed later
102   int16_t *src_diff;
103   //! Dequantized coefficients
104   tran_low_t *dqcoeff;
105   //! Quantized coefficients
106   tran_low_t *qcoeff;
107   //! Transformed coefficients
108   tran_low_t *coeff;
109   //! Location of the end of qcoeff (end of block).
110   uint16_t *eobs;
111   //! Contexts used to code the transform coefficients.
112   uint8_t *txb_entropy_ctx;
113   //! A buffer containing the source frame.
114   struct buf_2d src;
115 
116   /*! \name Quantizer Settings
117    *
118    * \attention These are used/accessed only in the quantization process.
119    * RDO does not and *must not* depend on any of these values.
120    * All values below share the coefficient scale/shift used in TX.
121    */
122   /**@{*/
123   //! Quantization step size used by AV1_XFORM_QUANT_FP.
124   const int16_t *quant_fp_QTX;
125   //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_FP.
126   const int16_t *round_fp_QTX;
127   //! Quantization step size used by AV1_XFORM_QUANT_B.
128   const int16_t *quant_QTX;
129   //! Offset used for rounding in the quantizer process by AV1_XFORM_QUANT_B.
130   const int16_t *round_QTX;
131   //! Scale factor to shift coefficients toward zero. Only used by QUANT_B.
132   const int16_t *quant_shift_QTX;
133   //! Size of the quantization bin around 0. Only Used by QUANT_B
134   const int16_t *zbin_QTX;
135   //! Dequantizer
136   const int16_t *dequant_QTX;
137   /**@}*/
138 } MACROBLOCK_PLANE;
139 
140 /*! \brief Costs for encoding the coefficients within a level.
141  *
142  * Covers everything including txb_skip, eob, dc_sign,
143  */
144 typedef struct {
145   //! Cost to skip txfm for the current txfm block.
146   int txb_skip_cost[TXB_SKIP_CONTEXTS][2];
147   /*! \brief Cost for encoding the base_eob of a level.
148    *
149    * Decoder uses base_eob to derive the base_level as base_eob := base_eob+1.
150    */
151   int base_eob_cost[SIG_COEF_CONTEXTS_EOB][3];
152   /*! \brief Cost for encoding the base level of a coefficient.
153    *
154    * Decoder derives coeff_base as coeff_base := base_eob + 1.
155    */
156   int base_cost[SIG_COEF_CONTEXTS][8];
157   /*! \brief Cost for encoding the last non-zero coefficient.
158    *
159    * Eob is derived from eob_extra at the decoder as eob := eob_extra + 1
160    */
161   int eob_extra_cost[EOB_COEF_CONTEXTS][2];
162   //! Cost for encoding the dc_sign
163   int dc_sign_cost[DC_SIGN_CONTEXTS][2];
164   //! Cost for encoding an increment to the coefficient
165   int lps_cost[LEVEL_CONTEXTS][COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1];
166 } LV_MAP_COEFF_COST;
167 
168 /*! \brief Costs for encoding the eob.
169  */
170 typedef struct {
171   //! eob_cost.
172   int eob_cost[2][11];
173 } LV_MAP_EOB_COST;
174 
175 /*! \brief Stores the transforms coefficients for the whole superblock.
176  */
177 typedef struct {
178   //! The transformed coefficients.
179   tran_low_t *tcoeff[MAX_MB_PLANE];
180   //! Where the transformed coefficients end.
181   uint16_t *eobs[MAX_MB_PLANE];
182   /*! \brief Transform block entropy contexts.
183    *
184    * Each element is used as a bit field.
185    * - Bits 0~3: txb_skip_ctx
186    * - Bits 4~5: dc_sign_ctx.
187    */
188   uint8_t *entropy_ctx[MAX_MB_PLANE];
189 } CB_COEFF_BUFFER;
190 
191 /*! \brief Extended mode info derived from mbmi.
192  */
193 typedef struct {
194   // TODO(angiebird): Reduce the buffer size according to sb_type
195   //! The reference mv list for the current block.
196   CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
197   //! The weights used to compute the ref mvs.
198   uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE];
199   //! Number of ref mvs in the drl.
200   uint8_t ref_mv_count[MODE_CTX_REF_FRAMES];
201   //! Global mvs
202   int_mv global_mvs[REF_FRAMES];
203   //! Context used to encode the current mode.
204   int16_t mode_context[MODE_CTX_REF_FRAMES];
205 } MB_MODE_INFO_EXT;
206 
207 /*! \brief Stores best extended mode information at frame level.
208  *
209  * The frame level in here is used in bitstream preparation stage. The
210  * information in \ref MB_MODE_INFO_EXT are copied to this struct to save
211  * memory.
212  */
213 typedef struct {
214   //! \copydoc MB_MODE_INFO_EXT::ref_mv_stack
215   CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE];
216   //! \copydoc MB_MODE_INFO_EXT::weight
217   uint16_t weight[USABLE_REF_MV_STACK_SIZE];
218   //! \copydoc MB_MODE_INFO_EXT::ref_mv_count
219   uint8_t ref_mv_count;
220   // TODO(Ravi/Remya): Reduce the buffer size of global_mvs
221   //! \copydoc MB_MODE_INFO_EXT::global_mvs
222   int_mv global_mvs[REF_FRAMES];
223   //! \copydoc MB_MODE_INFO_EXT::mode_context
224   int16_t mode_context;
225   //! Offset of current coding block's coeff buffer relative to the sb.
226   uint16_t cb_offset[PLANE_TYPES];
227 } MB_MODE_INFO_EXT_FRAME;
228 
229 /*! \brief Inter-mode txfm results for a partition block.
230  */
231 typedef struct {
232   //! Txfm size used if the current mode is intra mode.
233   TX_SIZE tx_size;
234   //! Txfm sizes used if the current mode is inter mode.
235   TX_SIZE inter_tx_size[INTER_TX_SIZE_BUF_LEN];
236   //! Map showing which txfm block skips the txfm process.
237   uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
238   //! Map showing the txfm types for each block.
239   uint8_t tx_type_map[MAX_MIB_SIZE * MAX_MIB_SIZE];
240   //! Rd_stats for the whole partition block.
241   RD_STATS rd_stats;
242   //! Hash value of the current record.
243   uint32_t hash_value;
244 } MB_RD_INFO;
245 
246 /*! \brief Hash records of the inter-mode transform results
247  *
248  * Hash records of the inter-mode transform results for a whole partition block
249  * based on the residue. Since this operates on the partition block level, this
250  * can give us a whole txfm partition tree.
251  */
252 typedef struct {
253   /*! Circular buffer that stores the inter-mode txfm results of a partition
254    *  block.
255    */
256   MB_RD_INFO mb_rd_info[RD_RECORD_BUFFER_LEN];
257   //! Index to insert the newest rd record.
258   int index_start;
259   //! Number of info stored in this record.
260   int num;
261   //! Hash function
262   CRC32C crc_calculator;
263 } MB_RD_RECORD;
264 
265 //! Number of compound rd stats
266 #define MAX_COMP_RD_STATS 64
267 /*! \brief Rdcost stats in compound mode.
268  */
269 typedef struct {
270   //! Rate of the compound modes.
271   int32_t rate[COMPOUND_TYPES];
272   //! Distortion of the compound modes.
273   int64_t dist[COMPOUND_TYPES];
274   //! Estimated rate of the compound modes.
275   int32_t model_rate[COMPOUND_TYPES];
276   //! Estimated distortion of the compound modes.
277   int64_t model_dist[COMPOUND_TYPES];
278   //! Rate need to send the mask type.
279   int comp_rs2[COMPOUND_TYPES];
280   //! Motion vector for each predictor.
281   int_mv mv[2];
282   //! Ref frame for each predictor.
283   MV_REFERENCE_FRAME ref_frames[2];
284   //! Current prediction mode.
285   PREDICTION_MODE mode;
286   //! Current interpolation filter.
287   int_interpfilters filter;
288   //! Refmv index in the drl.
289   int ref_mv_idx;
290   //! Whether the predictors are GLOBALMV.
291   int is_global[2];
292   //! Current parameters for interinter mode.
293   INTERINTER_COMPOUND_DATA interinter_comp;
294 } COMP_RD_STATS;
295 
296 /*! \brief Contains buffers used to speed up rdopt for obmc.
297  *
298  * See the comments for calc_target_weighted_pred for details.
299  */
300 typedef struct {
301   /*! \brief A new source weighted with the above and left predictors.
302    *
303    * Used to efficiently construct multiple obmc predictors during rdopt.
304    */
305   int32_t *wsrc;
306   /*! \brief A new mask constructed from the original horz/vert mask.
307    *
308    * \copydetails wsrc
309    */
310   int32_t *mask;
311   /*! \brief Prediction from the up predictor.
312    *
313    * Used to build the obmc predictor.
314    */
315   uint8_t *above_pred;
316   /*! \brief Prediction from the up predictor.
317    *
318    * \copydetails above_pred
319    */
320   uint8_t *left_pred;
321 } OBMCBuffer;
322 
323 /*! \brief Contains color maps used in palette mode.
324  */
325 typedef struct {
326   //! The best color map found.
327   uint8_t best_palette_color_map[MAX_PALETTE_SQUARE];
328   //! A temporary buffer used for k-means clustering.
329   int16_t kmeans_data_buf[2 * MAX_PALETTE_SQUARE];
330 } PALETTE_BUFFER;
331 
332 /*! \brief Contains buffers used by av1_compound_type_rd()
333  *
334  * For sizes and alignment of these arrays, refer to
335  * alloc_compound_type_rd_buffers() function.
336  */
337 typedef struct {
338   //! First prediction.
339   uint8_t *pred0;
340   //! Second prediction.
341   uint8_t *pred1;
342   //! Source - first prediction.
343   int16_t *residual1;
344   //! Second prediction - first prediction.
345   int16_t *diff10;
346   //! Backup of the best segmentation mask.
347   uint8_t *tmp_best_mask_buf;
348 } CompoundTypeRdBuffers;
349 
350 /*! \brief Holds some parameters related to partitioning schemes in AV1.
351  */
352 // TODO(chiyotsai@google.com): Consolidate this with SIMPLE_MOTION_DATA_TREE
353 typedef struct {
354 #if !CONFIG_REALTIME_ONLY
355   // The following 4 parameters are used for cnn-based partitioning on intra
356   // frame.
357   /*! \brief Current index on the partition block quad tree.
358    *
359    * Used to index into the cnn buffer for partition decision.
360    */
361   int quad_tree_idx;
362   //! Whether the CNN buffer contains valid output.
363   int cnn_output_valid;
364   //! A buffer used by our segmentation CNN for intra-frame partitioning.
365   float cnn_buffer[CNN_OUT_BUF_SIZE];
366   //! log of the quantization parameter of the ancestor BLOCK_64X64.
367   float log_q;
368 #endif
369 
370   /*! \brief Variance of the subblocks in the superblock.
371    *
372    * This is used by rt mode for variance based partitioning.
373    * The indices corresponds to the following block sizes:
374    * -   0    - 128x128
375    * -  1-2   - 128x64
376    * -  3-4   -  64x128
377    * -  5-8   -  64x64
378    * -  9-16  -  64x32
379    * - 17-24  -  32x64
380    * - 25-40  -  32x32
381    * - 41-104 -  16x16
382    */
383   uint8_t variance_low[105];
384 } PartitionSearchInfo;
385 
386 /*!\cond */
387 enum {
388   /**
389    * Do not prune transform depths.
390    */
391   TX_PRUNE_NONE = 0,
392   /**
393    * Prune largest transform (depth 0) based on NN model.
394    */
395   TX_PRUNE_LARGEST = 1,
396   /**
397    * Prune split transforms (depth>=1) based on NN model.
398    */
399   TX_PRUNE_SPLIT = 2,
400 } UENUM1BYTE(TX_PRUNE_TYPE);
401 /*!\endcond */
402 
403 /*! \brief Defines the parameters used to perform txfm search.
404  *
405  * For the most part, this determines how various speed features are used.
406  */
407 typedef struct {
408   /*! \brief Whether to limit the intra txfm search type to the default txfm.
409    *
410    * This could either be a result of either sequence parameter or speed
411    * features.
412    */
413   int use_default_intra_tx_type;
414 
415   /*! Probability threshold used for conditionally forcing tx type*/
416   int default_inter_tx_type_prob_thresh;
417 
418   //! Whether to prune 2d transforms based on 1d transform results.
419   int prune_2d_txfm_mode;
420 
421   /*! \brief Variable from \ref WinnerModeParams based on current eval mode.
422    *
423    * See the documentation for \ref WinnerModeParams for more detail.
424    */
425   unsigned int coeff_opt_thresholds[2];
426   /*! \copydoc coeff_opt_thresholds */
427   unsigned int tx_domain_dist_threshold;
428   /*! \copydoc coeff_opt_thresholds */
429   TX_SIZE_SEARCH_METHOD tx_size_search_method;
430   /*! \copydoc coeff_opt_thresholds */
431   unsigned int use_transform_domain_distortion;
432   /*! \copydoc coeff_opt_thresholds */
433   unsigned int skip_txfm_level;
434 
435   /*! \brief How to search for the optimal tx_size
436    *
437    * If ONLY_4X4, use TX_4X4; if TX_MODE_LARGEST, use the largest tx_size for
438    * the current partition block; if TX_MODE_SELECT, search through the whole
439    * tree.
440    *
441    * \attention
442    * Although this looks suspicious similar to a bitstream element, this
443    * tx_mode_search_type is only used internally by the encoder, and is *not*
444    * written to the bitstream. It determines what kind of tx_mode would be
445    * searched. For example, we might set it to TX_MODE_LARGEST to find a good
446    * candidate, then code it as TX_MODE_SELECT.
447    */
448   TX_MODE tx_mode_search_type;
449 
450   /*!
451    * Determines whether a block can be predicted as transform skip or DC only
452    * based on residual mean and variance.
453    * Type 0 : No skip block or DC only block prediction
454    * Type 1 : Prediction of skip block based on residual mean and variance
455    * Type 2 : Prediction of skip block or DC only block based on residual mean
456    * and variance
457    */
458   unsigned int predict_dc_level;
459 
460   /*!
461    * Whether or not we should use the quantization matrix as weights for PSNR
462    * during RD search.
463    */
464   int use_qm_dist_metric;
465 
466   /*!
467    * Keep track of previous mode evaluation stage type. This will be used to
468    * reset mb rd hash record when mode evaluation type changes.
469    */
470   int mode_eval_type;
471 
472 #if !CONFIG_REALTIME_ONLY
473   //! Indicates the transform depths for which RD evaluation is skipped.
474   TX_PRUNE_TYPE nn_prune_depths_for_intra_tx;
475 
476   /*! \brief Indicates if NN model should be invoked to prune transform depths.
477    *
478    * Used to signal whether NN model should be evaluated to prune the R-D
479    * evaluation of specific transform depths.
480    */
481   bool enable_nn_prune_intra_tx_depths;
482 #endif
483 } TxfmSearchParams;
484 
485 /*!\cond */
486 #define MAX_NUM_8X8_TXBS ((MAX_MIB_SIZE >> 1) * (MAX_MIB_SIZE >> 1))
487 #define MAX_NUM_16X16_TXBS ((MAX_MIB_SIZE >> 2) * (MAX_MIB_SIZE >> 2))
488 #define MAX_NUM_32X32_TXBS ((MAX_MIB_SIZE >> 3) * (MAX_MIB_SIZE >> 3))
489 #define MAX_NUM_64X64_TXBS ((MAX_MIB_SIZE >> 4) * (MAX_MIB_SIZE >> 4))
490 /*!\endcond */
491 
492 /*! \brief Stores various encoding/search decisions related to txfm search.
493  *
494  * This struct contains a cache of previous txfm results, and some buffers for
495  * the current txfm decision.
496  */
497 typedef struct {
498   //! Whether to skip transform and quantization on a partition block level.
499   uint8_t skip_txfm;
500 
501   /*! \brief Whether to skip transform and quantization on a txfm block level.
502    *
503    * Skips transform and quantization on a transform block level inside the
504    * current partition block. Each element of this array is used as a bit-field.
505    * So for example, the we are skipping on the luma plane, then the last bit
506    * would be set to 1.
507    */
508   uint8_t blk_skip[MAX_MIB_SIZE * MAX_MIB_SIZE];
509 
510   /*! \brief Transform types inside the partition block
511    *
512    * Keeps a record of what kind of transform to use for each of the transform
513    * block inside the partition block.
514    * \attention The buffer here is *never* directly used. Instead, this just
515    * allocates the memory for MACROBLOCKD::tx_type_map during rdopt on the
516    * partition block. So if we need to save memory, we could move the allocation
517    * to pick_sb_mode instead.
518    */
519   uint8_t tx_type_map_[MAX_MIB_SIZE * MAX_MIB_SIZE];
520 
521   //! Txfm hash records of inter-modes.
522   MB_RD_RECORD *mb_rd_record;
523 
524   /*! \brief Number of txb splits.
525    *
526    * Keep track of how many times we've used split tx partition for transform
527    * blocks. Somewhat misleadingly, this parameter doesn't actually keep track
528    * of the count of the current block. Instead, it's a cumulative count across
529    * of the whole frame. The main usage is that if txb_split_count is zero, then
530    * we can signal TX_MODE_LARGEST at frame level.
531    */
532   // TODO(chiyotsai@google.com): Move this to a more appropriate location such
533   // as ThreadData.
534   unsigned int txb_split_count;
535 #if CONFIG_SPEED_STATS
536   //! For debugging. Used to check how many txfm searches we are doing.
537   unsigned int tx_search_count;
538 #endif  // CONFIG_SPEED_STATS
539 } TxfmSearchInfo;
540 #undef MAX_NUM_8X8_TXBS
541 #undef MAX_NUM_16X16_TXBS
542 #undef MAX_NUM_32X32_TXBS
543 #undef MAX_NUM_64X64_TXBS
544 
545 /*! \brief Holds the entropy costs for various modes sent to the bitstream.
546  *
547  * \attention This does not include the costs for mv and transformed
548  * coefficients.
549  */
550 typedef struct {
551   /*****************************************************************************
552    * \name Partition Costs
553    ****************************************************************************/
554   /**@{*/
555   //! Cost for coding the partition.
556   int partition_cost[PARTITION_CONTEXTS][EXT_PARTITION_TYPES];
557   /**@}*/
558 
559   /*****************************************************************************
560    * \name Intra Costs: General
561    ****************************************************************************/
562   /**@{*/
563   //! Luma mode cost for inter frame.
564   int mbmode_cost[BLOCK_SIZE_GROUPS][INTRA_MODES];
565   //! Luma mode cost for intra frame.
566   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
567   //! Chroma mode cost
568   int intra_uv_mode_cost[CFL_ALLOWED_TYPES][INTRA_MODES][UV_INTRA_MODES];
569   //! filter_intra_cost
570   int filter_intra_cost[BLOCK_SIZES_ALL][2];
571   //! filter_intra_mode_cost
572   int filter_intra_mode_cost[FILTER_INTRA_MODES];
573   //! angle_delta_cost
574   int angle_delta_cost[DIRECTIONAL_MODES][2 * MAX_ANGLE_DELTA + 1];
575 
576   //! Rate rate associated with each alpha codeword
577   int cfl_cost[CFL_JOINT_SIGNS][CFL_PRED_PLANES][CFL_ALPHABET_SIZE];
578   /**@}*/
579 
580   /*****************************************************************************
581    * \name Intra Costs: Screen Contents
582    ****************************************************************************/
583   /**@{*/
584   //! intrabc_cost
585   int intrabc_cost[2];
586 
587   //! palette_y_size_cost
588   int palette_y_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
589   //! palette_uv_size_cost
590   int palette_uv_size_cost[PALATTE_BSIZE_CTXS][PALETTE_SIZES];
591   //! palette_y_color_cost
592   int palette_y_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
593                           [PALETTE_COLORS];
594   //! palette_uv_color_cost
595   int palette_uv_color_cost[PALETTE_SIZES][PALETTE_COLOR_INDEX_CONTEXTS]
596                            [PALETTE_COLORS];
597   //! palette_y_mode_cost
598   int palette_y_mode_cost[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS][2];
599   //! palette_uv_mode_cost
600   int palette_uv_mode_cost[PALETTE_UV_MODE_CONTEXTS][2];
601   /**@}*/
602 
603   /*****************************************************************************
604    * \name Inter Costs: MV Modes
605    ****************************************************************************/
606   /**@{*/
607   //! skip_mode_cost
608   int skip_mode_cost[SKIP_MODE_CONTEXTS][2];
609   //! newmv_mode_cost
610   int newmv_mode_cost[NEWMV_MODE_CONTEXTS][2];
611   //! zeromv_mode_cost
612   int zeromv_mode_cost[GLOBALMV_MODE_CONTEXTS][2];
613   //! refmv_mode_cost
614   int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
615   //! drl_mode_cost0
616   int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
617   /**@}*/
618 
619   /*****************************************************************************
620    * \name Inter Costs: Ref Frame Types
621    ****************************************************************************/
622   /**@{*/
623   //! single_ref_cost
624   int single_ref_cost[REF_CONTEXTS][SINGLE_REFS - 1][2];
625   //! comp_inter_cost
626   int comp_inter_cost[COMP_INTER_CONTEXTS][2];
627   //! comp_ref_type_cost
628   int comp_ref_type_cost[COMP_REF_TYPE_CONTEXTS]
629                         [CDF_SIZE(COMP_REFERENCE_TYPES)];
630   //! uni_comp_ref_cost
631   int uni_comp_ref_cost[UNI_COMP_REF_CONTEXTS][UNIDIR_COMP_REFS - 1]
632                        [CDF_SIZE(2)];
633   /*! \brief Cost for signaling ref_frame[0] in bidir-comp mode
634    *
635    * Includes LAST_FRAME, LAST2_FRAME, LAST3_FRAME, and GOLDEN_FRAME.
636    */
637   int comp_ref_cost[REF_CONTEXTS][FWD_REFS - 1][2];
638   /*! \brief Cost for signaling ref_frame[1] in bidir-comp mode
639    *
640    * Includes ALTREF_FRAME, ALTREF2_FRAME, and BWDREF_FRAME.
641    */
642   int comp_bwdref_cost[REF_CONTEXTS][BWD_REFS - 1][2];
643   /**@}*/
644 
645   /*****************************************************************************
646    * \name Inter Costs: Compound Types
647    ****************************************************************************/
648   /**@{*/
649   //! intra_inter_cost
650   int intra_inter_cost[INTRA_INTER_CONTEXTS][2];
651   //! inter_compound_mode_cost
652   int inter_compound_mode_cost[INTER_MODE_CONTEXTS][INTER_COMPOUND_MODES];
653   //! compound_type_cost
654   int compound_type_cost[BLOCK_SIZES_ALL][MASKED_COMPOUND_TYPES];
655   //! wedge_idx_cost
656   int wedge_idx_cost[BLOCK_SIZES_ALL][16];
657   //! interintra_cost
658   int interintra_cost[BLOCK_SIZE_GROUPS][2];
659   //! wedge_interintra_cost
660   int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
661   //! interintra_mode_cost
662   int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
663   /**@}*/
664 
665   /*****************************************************************************
666    * \name Inter Costs: Compound Masks
667    ****************************************************************************/
668   /**@{*/
669   //! comp_idx_cost
670   int comp_idx_cost[COMP_INDEX_CONTEXTS][2];
671   //! comp_group_idx_cost
672   int comp_group_idx_cost[COMP_GROUP_IDX_CONTEXTS][2];
673   /**@}*/
674 
675   /*****************************************************************************
676    * \name Inter Costs: Motion Modes/Filters
677    ****************************************************************************/
678   /**@{*/
679   //! motion_mode_cost
680   int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
681   //! motion_mode_cost1
682   int motion_mode_cost1[BLOCK_SIZES_ALL][2];
683   //! switchable_interp_costs
684   int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
685   /**@}*/
686 
687   /*****************************************************************************
688    * \name Txfm Mode Costs
689    ****************************************************************************/
690   /**@{*/
691   //! skip_txfm_cost
692   int skip_txfm_cost[SKIP_CONTEXTS][2];
693   //! tx_size_cost
694   int tx_size_cost[TX_SIZES - 1][TX_SIZE_CONTEXTS][TX_SIZES];
695   //! txfm_partition_cost
696   int txfm_partition_cost[TXFM_PARTITION_CONTEXTS][2];
697   //! inter_tx_type_costs
698   int inter_tx_type_costs[EXT_TX_SETS_INTER][EXT_TX_SIZES][TX_TYPES];
699   //! intra_tx_type_costs
700   int intra_tx_type_costs[EXT_TX_SETS_INTRA][EXT_TX_SIZES][INTRA_MODES]
701                          [TX_TYPES];
702   /**@}*/
703 
704   /*****************************************************************************
705    * \name Restoration Mode Costs
706    ****************************************************************************/
707   /**@{*/
708   //! switchable_restore_cost
709   int switchable_restore_cost[RESTORE_SWITCHABLE_TYPES];
710   //! wiener_restore_cost
711   int wiener_restore_cost[2];
712   //! sgrproj_restore_cost
713   int sgrproj_restore_cost[2];
714   /**@}*/
715 
716   /*****************************************************************************
717    * \name Segmentation Mode Costs
718    ****************************************************************************/
719   /**@{*/
720   //! tmp_pred_cost
721   int tmp_pred_cost[SEG_TEMPORAL_PRED_CTXS][2];
722   //! spatial_pred_cost
723   int spatial_pred_cost[SPATIAL_PREDICTION_PROBS][MAX_SEGMENTS];
724   /**@}*/
725 } ModeCosts;
726 
727 /*! \brief Holds mv costs for encoding and motion search.
728  */
729 typedef struct {
730   /*****************************************************************************
731    * \name Encoding Costs
732    * Here are the entropy costs needed to encode a given mv.
733    * \ref nmv_cost_alloc and \ref nmv_cost_hp_alloc are two arrays that holds
734    * the memory for holding the mv cost. But since the motion vectors can be
735    * negative, we shift them to the middle and store the resulting pointer in
736    * \ref nmv_cost and \ref nmv_cost_hp for easier referencing. Finally, \ref
737    * mv_cost_stack points to the \ref nmv_cost with the mv precision we are
738    * currently working with. In essence, only \ref mv_cost_stack is needed for
739    * motion search, the other can be considered private.
740    ****************************************************************************/
741   /**@{*/
742   //! Costs for coding the zero components.
743   int nmv_joint_cost[MV_JOINTS];
744 
745   //! Allocates memory for 1/4-pel motion vector costs.
746   int nmv_cost_alloc[2][MV_VALS];
747   //! Allocates memory for 1/8-pel motion vector costs.
748   int nmv_cost_hp_alloc[2][MV_VALS];
749   //! Points to the middle of \ref nmv_cost_alloc
750   int *nmv_cost[2];
751   //! Points to the middle of \ref nmv_cost_hp_alloc
752   int *nmv_cost_hp[2];
753   //! Points to the nmv_cost_hp in use.
754   int **mv_cost_stack;
755   /**@}*/
756 } MvCosts;
757 
758 /*! \brief Holds mv costs for intrabc.
759  */
760 typedef struct {
761   /*! Costs for coding the joint mv. */
762   int joint_mv[MV_JOINTS];
763 
764   /*! \brief Cost of transmitting the actual motion vector.
765    *  dv_costs_alloc[0][i] is the cost of motion vector with horizontal
766    * component (mv_row) equal to i - MV_MAX. dv_costs_alloc[1][i] is the cost of
767    * motion vector with vertical component (mv_col) equal to i - MV_MAX.
768    */
769   int dv_costs_alloc[2][MV_VALS];
770 
771   /*! Points to the middle of \ref dv_costs_alloc. */
772   int *dv_costs[2];
773 } IntraBCMVCosts;
774 
775 /*! \brief Holds the costs needed to encode the coefficients
776  */
777 typedef struct {
778   //! Costs for coding the coefficients.
779   LV_MAP_COEFF_COST coeff_costs[TX_SIZES][PLANE_TYPES];
780   //! Costs for coding the eobs.
781   LV_MAP_EOB_COST eob_costs[7][2];
782 } CoeffCosts;
783 
784 /*!\cond */
785 // 4: NEAREST, NEW, NEAR, GLOBAL
786 #define SINGLE_REF_MODES ((REF_FRAMES - 1) * 4)
787 /*!\endcond */
788 struct inter_modes_info;
789 
790 /*! \brief Holds the motion samples for warp motion model estimation
791  */
792 typedef struct {
793   //! Number of samples.
794   int num;
795   //! Sample locations in current frame.
796   int pts[16];
797   //! Sample location in the reference frame.
798   int pts_inref[16];
799 } WARP_SAMPLE_INFO;
800 
801 /*!\cond */
802 typedef enum {
803   kZeroSad = 0,
804   kVeryLowSad = 1,
805   kLowSad = 2,
806   kMedSad = 3,
807   kHighSad = 4
808 } SOURCE_SAD;
809 
810 typedef struct {
811   //! SAD levels in non-rd path
812   SOURCE_SAD source_sad_nonrd;
813   //! SAD levels in rd-path for var-based part qindex thresholds
814   SOURCE_SAD source_sad_rd;
815   int lighting_change;
816   int low_sumdiff;
817 } CONTENT_STATE_SB;
818 
819 // Structure to hold pixel level gradient info.
820 typedef struct {
821   uint16_t abs_dx_abs_dy_sum;
822   int8_t hist_bin_idx;
823   bool is_dx_zero;
824 } PixelLevelGradientInfo;
825 
826 // Structure to hold the variance and log(1 + variance) for 4x4 sub-blocks.
827 typedef struct {
828   double log_var;
829   int var;
830 } Block4x4VarInfo;
831 
832 #ifndef NDEBUG
833 typedef struct SetOffsetsLoc {
834   int mi_row;
835   int mi_col;
836   BLOCK_SIZE bsize;
837 } SetOffsetsLoc;
838 #endif  // NDEBUG
839 
840 /*!\endcond */
841 
842 /*! \brief Encoder's parameters related to the current coding block.
843  *
844  * This struct contains most of the information the encoder needs to encode the
845  * current coding block. This includes the src and pred buffer, a copy of the
846  * decoder's view of the current block, the txfm coefficients. This struct also
847  * contains various buffers and data used to speed up the encoding process.
848  */
849 typedef struct macroblock {
850   /*****************************************************************************
851    * \name Source, Buffers and Decoder
852    ****************************************************************************/
853   /**@{*/
854   /*! \brief Each of the encoding plane.
855    *
856    * An array holding the src buffer for each of plane of the current block. It
857    * also contains the txfm and quantized txfm coefficients.
858    */
859   struct macroblock_plane plane[MAX_MB_PLANE];
860 
861   /*! \brief Decoder's view of current coding block.
862    *
863    * Contains the encoder's copy of what the decoder sees in the current block.
864    * Most importantly, this struct contains pointers to mbmi that is used in
865    * final bitstream packing.
866    */
867   MACROBLOCKD e_mbd;
868 
869   /*! \brief Derived coding information.
870    *
871    * Contains extra information not transmitted in the bitstream but are
872    * derived. For example, this contains the stack of ref_mvs.
873    */
874   MB_MODE_INFO_EXT mbmi_ext;
875 
876   /*! \brief Finalized mbmi_ext for the whole frame.
877    *
878    * Contains the finalized info in mbmi_ext that gets used at the frame level
879    * for bitstream packing.
880    */
881   MB_MODE_INFO_EXT_FRAME *mbmi_ext_frame;
882 
883   //! Entropy context for the current row.
884   FRAME_CONTEXT *row_ctx;
885   /*! \brief Entropy context for the current tile.
886    *
887    * This context will be used to update color_map_cdf pointer which would be
888    * used during pack bitstream. For single thread and tile-multithreading case
889    * this pointer will be same as xd->tile_ctx, but for the case of row-mt:
890    * xd->tile_ctx will point to a temporary context while tile_pb_ctx will point
891    * to the accurate tile context.
892    */
893   FRAME_CONTEXT *tile_pb_ctx;
894 
895   /*! \brief Buffer of transformed coefficients
896    *
897    * Points to cb_coef_buff in the AV1_COMP struct, which contains the finalized
898    * coefficients. This is here to conveniently copy the best coefficients to
899    * frame level for bitstream packing. Since CB_COEFF_BUFFER is allocated on a
900    * superblock level, we need to combine it with cb_offset to get the proper
901    * position for the current coding block.
902    */
903   CB_COEFF_BUFFER *cb_coef_buff;
904   //! Offset of current coding block's coeff buffer relative to the sb.
905   uint16_t cb_offset[PLANE_TYPES];
906 
907   //! Modified source and masks used for fast OBMC search.
908   OBMCBuffer obmc_buffer;
909   //! Buffer to store the best palette map.
910   PALETTE_BUFFER *palette_buffer;
911   //! Buffer used for compound_type_rd().
912   CompoundTypeRdBuffers comp_rd_buffer;
913   //! Buffer to store convolution during averaging process in compound mode.
914   CONV_BUF_TYPE *tmp_conv_dst;
915 
916   /*! \brief Temporary buffer to hold prediction.
917    *
918    * Points to a buffer that is used to hold temporary prediction results. This
919    * is used in two ways:
920    * - This is a temporary buffer used to ping-pong the prediction in
921    *   handle_inter_mode.
922    * - xd->tmp_obmc_bufs also points to this buffer, and is used in ombc
923    *   prediction.
924    */
925   uint8_t *tmp_pred_bufs[2];
926   /**@}*/
927 
928   /*****************************************************************************
929    * \name Rdopt Costs
930    ****************************************************************************/
931   /**@{*/
932   /*! \brief Quantization index for the current partition block.
933    *
934    * This is used to as the index to find quantization parameter for luma and
935    * chroma transformed coefficients.
936    */
937   int qindex;
938 
939   /*! \brief Difference between frame-level qindex and current qindex.
940    *
941    *  This is used to track whether a non-zero delta for qindex is used at least
942    *  once in the current frame.
943    */
944   int delta_qindex;
945 
946   /*! \brief Difference between frame-level qindex and qindex used to
947    * compute rdmult (lambda).
948    *
949    * rdmult_delta_qindex is assigned the same as delta_qindex before qp sweep.
950    * During qp sweep, delta_qindex is changed and used to calculate the actual
951    * quant params, while rdmult_delta_qindex remains the same, and is used to
952    * calculate the rdmult in "set_deltaq_rdmult".
953    */
954   int rdmult_delta_qindex;
955 
956   /*! \brief Current qindex (before being adjusted by delta_q_res) used to
957    * derive rdmult_delta_qindex.
958    */
959   int rdmult_cur_qindex;
960 
961   /*! \brief Rate-distortion multiplier.
962    *
963    * The rd multiplier used to determine the rate-distortion trade-off. This is
964    * roughly proportional to the inverse of q-index for a given frame, but this
965    * can be manipulated for better rate-control. For example, in tune_ssim
966    * mode, this is scaled by a factor related to the variance of the current
967    * block.
968    */
969   int rdmult;
970 
971   //! Intra only, per sb rd adjustment.
972   int intra_sb_rdmult_modifier;
973 
974   //! Superblock level distortion propagation factor.
975   double rb;
976 
977   //! Energy in the current source coding block. Used to calculate \ref rdmult
978   int mb_energy;
979   //! Energy in the current source superblock. Used to calculate \ref rdmult
980   int sb_energy_level;
981 
982   //! The rate needed to signal a mode to the bitstream.
983   ModeCosts mode_costs;
984 
985   //! The rate needed to encode a new motion vector to the bitstream and some
986   //! multipliers for motion search.
987   MvCosts *mv_costs;
988 
989   /*! The rate needed to encode a new motion vector to the bitstream in intrabc
990    *  mode.
991    */
992   IntraBCMVCosts *dv_costs;
993 
994   //! The rate needed to signal the txfm coefficients to the bitstream.
995   CoeffCosts coeff_costs;
996   /**@}*/
997 
998   /*****************************************************************************
999    * \name Rate to Distortion Multipliers
1000    ****************************************************************************/
1001   /**@{*/
1002   //! A multiplier that converts mv cost to l2 error.
1003   int errorperbit;
1004   //! A multiplier that converts mv cost to l1 error.
1005   int sadperbit;
1006   /**@}*/
1007 
1008   /******************************************************************************
1009    * \name Segmentation
1010    *****************************************************************************/
1011   /**@{*/
1012   /*! \brief Skip mode for the segment
1013    *
1014    * A syntax element of the segmentation mode. In skip_block mode, all mvs are
1015    * set 0 and all txfms are skipped.
1016    */
1017   int seg_skip_block;
1018 
1019   /*! \brief Number of segment 1 blocks
1020    * Actual number of (4x4) blocks that were applied delta-q,
1021    * for segment 1.
1022    */
1023   int actual_num_seg1_blocks;
1024 
1025   /*!\brief Number of segment 2 blocks
1026    * Actual number of (4x4) blocks that were applied delta-q,
1027    * for segment 2.
1028    */
1029   int actual_num_seg2_blocks;
1030 
1031   /*!\brief Number of zero motion vectors
1032    */
1033   int cnt_zeromv;
1034 
1035   /*!\brief Flag to force zeromv-skip at superblock level, for nonrd path.
1036    *
1037    * 0/1 imply zeromv-skip is disabled/enabled. 2 implies that the blocks
1038    * in the superblock may be marked as zeromv-skip at block level.
1039    */
1040   int force_zeromv_skip_for_sb;
1041 
1042   /*!\brief Flag to force zeromv-skip at block level, for nonrd path.
1043    */
1044   int force_zeromv_skip_for_blk;
1045 
1046   /*! \brief Previous segment id for which qmatrices were updated.
1047    * This is used to bypass setting of qmatrices if no change in qindex.
1048    */
1049   int prev_segment_id;
1050   /**@}*/
1051 
1052   /*****************************************************************************
1053    * \name Superblock
1054    ****************************************************************************/
1055   /**@{*/
1056   //! Information on a whole superblock level.
1057   // TODO(chiyotsai@google.com): Refactor this out of macroblock
1058   SuperBlockEnc sb_enc;
1059 
1060   /*! \brief Characteristics of the current superblock.
1061    *
1062    *  Characteristics like whether the block has high sad, low sad, etc. This is
1063    *  only used by av1 realtime mode.
1064    */
1065   CONTENT_STATE_SB content_state_sb;
1066   /**@}*/
1067 
1068   /*****************************************************************************
1069    * \name Reference Frame Search
1070    ****************************************************************************/
1071   /**@{*/
1072   /*! \brief Sum absolute distortion of the predicted mv for each ref frame.
1073    *
1074    * This is used to measure how viable a reference frame is.
1075    */
1076   int pred_mv_sad[REF_FRAMES];
1077   /*! \brief The minimum of \ref pred_mv_sad.
1078    *
1079    * Index 0 stores the minimum \ref pred_mv_sad across past reference frames.
1080    * Index 1 stores the minimum \ref pred_mv_sad across future reference frames.
1081    */
1082   int best_pred_mv_sad[2];
1083   //! The sad of the 1st mv ref (nearest).
1084   int pred_mv0_sad[REF_FRAMES];
1085   //! The sad of the 2nd mv ref (near).
1086   int pred_mv1_sad[REF_FRAMES];
1087 
1088   /*! \brief Disables certain ref frame pruning based on tpl.
1089    *
1090    * Determines whether a given ref frame is "good" based on data from the TPL
1091    * model. If so, this stops selective_ref frame from pruning the given ref
1092    * frame at block level.
1093    */
1094   uint8_t tpl_keep_ref_frame[REF_FRAMES];
1095 
1096   /*! \brief Warp motion samples buffer.
1097    *
1098    * Store the motion samples used for warp motion.
1099    */
1100   WARP_SAMPLE_INFO warp_sample_info[REF_FRAMES];
1101 
1102   /*! \brief Reference frames picked by the square subblocks in a superblock.
1103    *
1104    * Keeps track of ref frames that are selected by square partition blocks
1105    * within a superblock, in MI resolution. They can be used to prune ref frames
1106    * for rectangular blocks.
1107    */
1108   int picked_ref_frames_mask[MAX_MIB_SIZE * MAX_MIB_SIZE];
1109 
1110   /*! \brief Prune ref frames in real-time mode.
1111    *
1112    * Determines whether to prune reference frames in real-time mode. For the
1113    * most part, this is the same as nonrd_prune_ref_frame_search in
1114    * cpi->sf.rt_sf.nonrd_prune_ref_frame_search, but this can be selectively
1115    * turned off if the only frame available is GOLDEN_FRAME.
1116    */
1117   int nonrd_prune_ref_frame_search;
1118   /**@}*/
1119 
1120   /*****************************************************************************
1121    * \name Partition Search
1122    ****************************************************************************/
1123   /**@{*/
1124   //! Stores some partition-search related buffers.
1125   PartitionSearchInfo part_search_info;
1126 
1127   /*! \brief Whether to disable some features to force a mode in current block.
1128    *
1129    * In some cases, our speed features can be overly aggressive and remove all
1130    * modes search in the superblock. When this happens, we set
1131    * must_find_valid_partition to 1 to reduce the number of speed features, and
1132    * recode the superblock again.
1133    */
1134   int must_find_valid_partition;
1135   /**@}*/
1136 
1137   /*****************************************************************************
1138    * \name Prediction Mode Search
1139    ****************************************************************************/
1140   /**@{*/
1141   /*! \brief Inter skip mode.
1142    *
1143    * Skip mode tries to use the closest forward and backward references for
1144    * inter prediction. Skip here means to skip transmitting the reference
1145    * frames, not to be confused with skip_txfm.
1146    */
1147   int skip_mode;
1148 
1149   /*! \brief Factors used for rd-thresholding.
1150    *
1151    * Determines a rd threshold to determine whether to continue searching the
1152    * current mode. If the current best rd is already <= threshold, then we skip
1153    * the current mode.
1154    */
1155   int thresh_freq_fact[BLOCK_SIZES_ALL][MAX_MODES];
1156 
1157   /*! \brief Tracks the winner modes in the current coding block.
1158    *
1159    * Winner mode is a two-pass strategy to find the best prediction mode. In the
1160    * first pass, we search the prediction modes with a limited set of txfm
1161    * options, and keep the top modes. These modes are called the winner modes.
1162    * In the second pass, we retry the winner modes with more thorough txfm
1163    * options.
1164    */
1165   WinnerModeStats *winner_mode_stats;
1166   //! Tracks how many winner modes there are.
1167   int winner_mode_count;
1168 
1169   /*! \brief The model used for rd-estimation to avoid txfm
1170    *
1171    * These are for inter_mode_rd_model_estimation, which is another two pass
1172    * approach. In this speed feature, we collect data in the first couple frames
1173    * to build an rd model to estimate the rdcost of a prediction model based on
1174    * the residue error. Once enough data is collected, this speed feature uses
1175    * the estimated rdcost to find the most performant prediction mode. Then we
1176    * follow up with a second pass find the best transform for the mode.
1177    * Determines if one would go with reduced complexity transform block
1178    * search model to select prediction modes, or full complexity model
1179    * to select transform kernel.
1180    */
1181   TXFM_RD_MODEL rd_model;
1182 
1183   /*! \brief Stores the inter mode information needed to build an rd model.
1184    *
1185    * These are for inter_mode_rd_model_estimation, which is another two pass
1186    * approach. In this speed feature, we collect data in the first couple frames
1187    * to build an rd model to estimate the rdcost of a prediction model based on
1188    * the residue error. Once enough data is collected, this speed feature uses
1189    * the estimated rdcost to find the most performant prediction mode. Then we
1190    * follow up with a second pass find the best transform for the mode.
1191    */
1192   // TODO(any): try to consolidate this speed feature with winner mode
1193   // processing.
1194   struct inter_modes_info *inter_modes_info;
1195 
1196   //! How to blend the compound predictions.
1197   uint8_t compound_idx;
1198 
1199   //! A caches of results of compound type search so they can be reused later.
1200   COMP_RD_STATS comp_rd_stats[MAX_COMP_RD_STATS];
1201   //! The idx for the latest compound mode in the cache \ref comp_rd_stats.
1202   int comp_rd_stats_idx;
1203 
1204   /*! \brief Whether to recompute the luma prediction.
1205    *
1206    * In interpolation search, we can usually skip recalculating the luma
1207    * prediction because it is already calculated by a previous predictor. This
1208    * flag signifies that some modes might have been skipped, so we need to
1209    * rebuild the prediction.
1210    */
1211   int recalc_luma_mc_data;
1212 
1213   /*! \brief Data structure to speed up intrabc search.
1214    *
1215    * Contains the hash table, hash function, and buffer used for intrabc.
1216    */
1217   IntraBCHashInfo intrabc_hash_info;
1218 
1219   /*! \brief Whether to reuse the mode stored in mb_mode_cache. */
1220   int use_mb_mode_cache;
1221   /*! \brief The mode to reuse during \ref av1_rd_pick_intra_mode_sb and
1222    *  \ref av1_rd_pick_inter_mode. */
1223   const MB_MODE_INFO *mb_mode_cache;
1224   /*! \brief Pointer to the buffer which caches gradient information.
1225    *
1226    * Pointer to the array of structures to store gradient information of each
1227    * pixel in a superblock. The buffer constitutes of MAX_SB_SQUARE pixel level
1228    * structures for each of the plane types (PLANE_TYPE_Y and PLANE_TYPE_UV).
1229    */
1230   PixelLevelGradientInfo *pixel_gradient_info;
1231   /*! \brief Flags indicating the availability of cached gradient info. */
1232   bool is_sb_gradient_cached[PLANE_TYPES];
1233 
1234   /*! \brief Flag to reuse predicted samples of inter block. */
1235   bool reuse_inter_pred;
1236   /**@}*/
1237 
1238   /*****************************************************************************
1239    * \name MV Search
1240    ****************************************************************************/
1241   /**@{*/
1242   /*! \brief Context used to determine the initial step size in motion search.
1243    *
1244    * This context is defined as the \f$l_\inf\f$ norm of the best ref_mvs for
1245    * each frame.
1246    */
1247   unsigned int max_mv_context[REF_FRAMES];
1248 
1249   /*! \brief Limit for the range of motion vectors.
1250    *
1251    * These define limits to motion vector components to prevent them from
1252    * extending outside the UMV borders
1253    */
1254   FullMvLimits mv_limits;
1255 
1256   /*! \brief Buffer for storing the search site config.
1257    *
1258    * When resize mode or super resolution mode is on, the stride of the
1259    * reference frame does not always match what's specified in \ref
1260    * MotionVectorSearchParams::search_site_cfg. When his happens, we update the
1261    * search_sine_config buffer here and use it for motion search.
1262    */
1263   search_site_config search_site_cfg_buf[NUM_DISTINCT_SEARCH_METHODS];
1264   /**@}*/
1265 
1266   /*****************************************************************************
1267    * \name Txfm Search
1268    ****************************************************************************/
1269   /**@{*/
1270   /*! \brief Parameters that control how motion search is done.
1271    *
1272    * Stores various txfm search related parameters such as txfm_type, txfm_size,
1273    * trellis eob search, etc.
1274    */
1275   TxfmSearchParams txfm_search_params;
1276 
1277   /*! \brief Results of the txfm searches that have been done.
1278    *
1279    * Caches old txfm search results and keeps the current txfm decisions to
1280    * facilitate rdopt.
1281    */
1282   TxfmSearchInfo txfm_search_info;
1283 
1284   /*! \brief Whether there is a strong color activity.
1285    *
1286    * Used in REALTIME coding mode to enhance the visual quality at the boundary
1287    * of moving color objects.
1288    */
1289   uint8_t color_sensitivity_sb[2];
1290   //! Color sensitivity flag for the superblock for golden reference.
1291   uint8_t color_sensitivity_sb_g[2];
1292   //! Color sensitivity flag for the coding block.
1293   uint8_t color_sensitivity[2];
1294   /**@}*/
1295 
1296   /*****************************************************************************
1297    * \name Misc
1298    ****************************************************************************/
1299   /**@{*/
1300   //! Variance of the source frame.
1301   unsigned int source_variance;
1302   //! SSE of the current predictor.
1303   unsigned int pred_sse[REF_FRAMES];
1304   //! Prediction for ML based partition.
1305 #if CONFIG_RT_ML_PARTITIONING
1306   DECLARE_ALIGNED(16, uint8_t, est_pred[128 * 128]);
1307 #endif
1308   /**@}*/
1309 
1310   /*! \brief NONE partition evaluated for merge.
1311    *
1312    * In variance based partitioning scheme, NONE & SPLIT partitions are
1313    * evaluated to check the SPLIT can be merged as NONE. This flag signifies the
1314    * partition is evaluated in the scheme.
1315    */
1316   int try_merge_partition;
1317 
1318   /*! \brief Pointer to buffer which caches sub-block variances in a superblock.
1319    *
1320    *  Pointer to the array of structures to store source variance information of
1321    *  each 4x4 sub-block in a superblock. Block4x4VarInfo structure is used to
1322    *  store source variance and log of source variance of each 4x4 sub-block.
1323    */
1324   Block4x4VarInfo *src_var_info_of_4x4_sub_blocks;
1325 #ifndef NDEBUG
1326   /*! \brief A hash to make sure av1_set_offsets is called */
1327   SetOffsetsLoc last_set_offsets_loc;
1328 #endif  // NDEBUG
1329 } MACROBLOCK;
1330 #undef SINGLE_REF_MODES
1331 
1332 /*!\cond */
1333 // Zeroes out 'n_stats' elements in the array x->winner_mode_stats.
1334 // It only zeroes out what is necessary in 'color_index_map' (just the block
1335 // size, not the whole array).
zero_winner_mode_stats(BLOCK_SIZE bsize,int n_stats,WinnerModeStats * stats)1336 static INLINE void zero_winner_mode_stats(BLOCK_SIZE bsize, int n_stats,
1337                                           WinnerModeStats *stats) {
1338   // When winner mode stats are not required, the memory allocation is avoided
1339   // for x->winner_mode_stats. The stats pointer will be NULL in such cases.
1340   if (stats == NULL) return;
1341 
1342   const int block_height = block_size_high[bsize];
1343   const int block_width = block_size_wide[bsize];
1344   for (int i = 0; i < n_stats; ++i) {
1345     WinnerModeStats *const stat = &stats[i];
1346     memset(&stat->mbmi, 0, sizeof(stat->mbmi));
1347     memset(&stat->rd_cost, 0, sizeof(stat->rd_cost));
1348     memset(&stat->rd, 0, sizeof(stat->rd));
1349     memset(&stat->rate_y, 0, sizeof(stat->rate_y));
1350     memset(&stat->rate_uv, 0, sizeof(stat->rate_uv));
1351     // Do not reset the whole array as it is CPU intensive.
1352     memset(&stat->color_index_map, 0,
1353            block_width * block_height * sizeof(stat->color_index_map[0]));
1354     memset(&stat->mode_index, 0, sizeof(stat->mode_index));
1355   }
1356 }
1357 
is_rect_tx_allowed_bsize(BLOCK_SIZE bsize)1358 static INLINE int is_rect_tx_allowed_bsize(BLOCK_SIZE bsize) {
1359   static const char LUT[BLOCK_SIZES_ALL] = {
1360     0,  // BLOCK_4X4
1361     1,  // BLOCK_4X8
1362     1,  // BLOCK_8X4
1363     0,  // BLOCK_8X8
1364     1,  // BLOCK_8X16
1365     1,  // BLOCK_16X8
1366     0,  // BLOCK_16X16
1367     1,  // BLOCK_16X32
1368     1,  // BLOCK_32X16
1369     0,  // BLOCK_32X32
1370     1,  // BLOCK_32X64
1371     1,  // BLOCK_64X32
1372     0,  // BLOCK_64X64
1373     0,  // BLOCK_64X128
1374     0,  // BLOCK_128X64
1375     0,  // BLOCK_128X128
1376     1,  // BLOCK_4X16
1377     1,  // BLOCK_16X4
1378     1,  // BLOCK_8X32
1379     1,  // BLOCK_32X8
1380     1,  // BLOCK_16X64
1381     1,  // BLOCK_64X16
1382   };
1383 
1384   return LUT[bsize];
1385 }
1386 
is_rect_tx_allowed(const MACROBLOCKD * xd,const MB_MODE_INFO * mbmi)1387 static INLINE int is_rect_tx_allowed(const MACROBLOCKD *xd,
1388                                      const MB_MODE_INFO *mbmi) {
1389   return is_rect_tx_allowed_bsize(mbmi->bsize) &&
1390          !xd->lossless[mbmi->segment_id];
1391 }
1392 
tx_size_to_depth(TX_SIZE tx_size,BLOCK_SIZE bsize)1393 static INLINE int tx_size_to_depth(TX_SIZE tx_size, BLOCK_SIZE bsize) {
1394   TX_SIZE ctx_size = max_txsize_rect_lookup[bsize];
1395   int depth = 0;
1396   while (tx_size != ctx_size) {
1397     depth++;
1398     ctx_size = sub_tx_size_map[ctx_size];
1399     assert(depth <= MAX_TX_DEPTH);
1400   }
1401   return depth;
1402 }
1403 
set_blk_skip(uint8_t txb_skip[],int plane,int blk_idx,int skip)1404 static INLINE void set_blk_skip(uint8_t txb_skip[], int plane, int blk_idx,
1405                                 int skip) {
1406   if (skip)
1407     txb_skip[blk_idx] |= 1UL << plane;
1408   else
1409     txb_skip[blk_idx] &= ~(1UL << plane);
1410 #ifndef NDEBUG
1411   // Set chroma planes to uninitialized states when luma is set to check if
1412   // it will be set later
1413   if (plane == 0) {
1414     txb_skip[blk_idx] |= 1UL << (1 + 4);
1415     txb_skip[blk_idx] |= 1UL << (2 + 4);
1416   }
1417 
1418   // Clear the initialization checking bit
1419   txb_skip[blk_idx] &= ~(1UL << (plane + 4));
1420 #endif
1421 }
1422 
is_blk_skip(uint8_t * txb_skip,int plane,int blk_idx)1423 static INLINE int is_blk_skip(uint8_t *txb_skip, int plane, int blk_idx) {
1424 #ifndef NDEBUG
1425   // Check if this is initialized
1426   assert(!(txb_skip[blk_idx] & (1UL << (plane + 4))));
1427 
1428   // The magic number is 0x77, this is to test if there is garbage data
1429   assert((txb_skip[blk_idx] & 0x88) == 0);
1430 #endif
1431   return (txb_skip[blk_idx] >> plane) & 1;
1432 }
1433 
1434 /*!\endcond */
1435 
1436 #ifdef __cplusplus
1437 }  // extern "C"
1438 #endif
1439 
1440 #endif  // AOM_AV1_ENCODER_BLOCK_H_
1441