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