xref: /aosp_15_r20/external/libaom/av1/encoder/context_tree.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_ENCODER_CONTEXT_TREE_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_CONTEXT_TREE_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/block.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/speed_features.h"
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
22*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
23*77c1e3ccSAndroid Build Coastguard Worker #endif
24*77c1e3ccSAndroid Build Coastguard Worker 
25*77c1e3ccSAndroid Build Coastguard Worker struct AV1_PRIMARY;
26*77c1e3ccSAndroid Build Coastguard Worker struct AV1_COMP;
27*77c1e3ccSAndroid Build Coastguard Worker struct AV1Common;
28*77c1e3ccSAndroid Build Coastguard Worker struct ThreadData;
29*77c1e3ccSAndroid Build Coastguard Worker 
30*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
31*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *coeff_buf[MAX_MB_PLANE];
32*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *qcoeff_buf[MAX_MB_PLANE];
33*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *dqcoeff_buf[MAX_MB_PLANE];
34*77c1e3ccSAndroid Build Coastguard Worker } PC_TREE_SHARED_BUFFERS;
35*77c1e3ccSAndroid Build Coastguard Worker 
36*77c1e3ccSAndroid Build Coastguard Worker // Structure to hold snapshot of coding context during the mode picking process
37*77c1e3ccSAndroid Build Coastguard Worker typedef struct PICK_MODE_CONTEXT {
38*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO mic;
39*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO_EXT_FRAME mbmi_ext_best;
40*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *color_index_map[2];
41*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *blk_skip;
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *coeff[MAX_MB_PLANE];
44*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *qcoeff[MAX_MB_PLANE];
45*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *dqcoeff[MAX_MB_PLANE];
46*77c1e3ccSAndroid Build Coastguard Worker   uint16_t *eobs[MAX_MB_PLANE];
47*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *txb_entropy_ctx[MAX_MB_PLANE];
48*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *tx_type_map;
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker   int num_4x4_blk;
51*77c1e3ccSAndroid Build Coastguard Worker   // For current partition, only if all Y, U, and V transform blocks'
52*77c1e3ccSAndroid Build Coastguard Worker   // coefficients are quantized to 0, skippable is set to 1.
53*77c1e3ccSAndroid Build Coastguard Worker   int skippable;
54*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
55*77c1e3ccSAndroid Build Coastguard Worker   THR_MODES best_mode_index;
56*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_INTERNAL_STATS
57*77c1e3ccSAndroid Build Coastguard Worker   RD_STATS rd_stats;
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker   int rd_mode_is_ready;  // Flag to indicate whether rd pick mode decision has
60*77c1e3ccSAndroid Build Coastguard Worker                          // been made.
61*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_TEMPORAL_DENOISING
62*77c1e3ccSAndroid Build Coastguard Worker   int64_t newmv_sse;
63*77c1e3ccSAndroid Build Coastguard Worker   int64_t zeromv_sse;
64*77c1e3ccSAndroid Build Coastguard Worker   int64_t zeromv_lastref_sse;
65*77c1e3ccSAndroid Build Coastguard Worker   PREDICTION_MODE best_sse_inter_mode;
66*77c1e3ccSAndroid Build Coastguard Worker   int_mv best_sse_mv;
67*77c1e3ccSAndroid Build Coastguard Worker   MV_REFERENCE_FRAME best_reference_frame;
68*77c1e3ccSAndroid Build Coastguard Worker   MV_REFERENCE_FRAME best_zeromv_reference_frame;
69*77c1e3ccSAndroid Build Coastguard Worker   int sb_skip_denoising;
70*77c1e3ccSAndroid Build Coastguard Worker #endif
71*77c1e3ccSAndroid Build Coastguard Worker } PICK_MODE_CONTEXT;
72*77c1e3ccSAndroid Build Coastguard Worker 
73*77c1e3ccSAndroid Build Coastguard Worker typedef struct PC_TREE {
74*77c1e3ccSAndroid Build Coastguard Worker   PARTITION_TYPE partitioning;
75*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE block_size;
76*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *none;
77*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *horizontal[2];
78*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *vertical[2];
79*77c1e3ccSAndroid Build Coastguard Worker #if !CONFIG_REALTIME_ONLY
80*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *horizontala[3];
81*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *horizontalb[3];
82*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *verticala[3];
83*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *verticalb[3];
84*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *horizontal4[4];
85*77c1e3ccSAndroid Build Coastguard Worker   PICK_MODE_CONTEXT *vertical4[4];
86*77c1e3ccSAndroid Build Coastguard Worker #endif
87*77c1e3ccSAndroid Build Coastguard Worker   struct PC_TREE *split[4];
88*77c1e3ccSAndroid Build Coastguard Worker   int index;
89*77c1e3ccSAndroid Build Coastguard Worker } PC_TREE;
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker typedef struct SIMPLE_MOTION_DATA_TREE {
92*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE block_size;
93*77c1e3ccSAndroid Build Coastguard Worker   PARTITION_TYPE partitioning;
94*77c1e3ccSAndroid Build Coastguard Worker   struct SIMPLE_MOTION_DATA_TREE *split[4];
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker   // Simple motion search_features
97*77c1e3ccSAndroid Build Coastguard Worker   FULLPEL_MV start_mvs[REF_FRAMES];
98*77c1e3ccSAndroid Build Coastguard Worker   unsigned int sms_none_feat[2];
99*77c1e3ccSAndroid Build Coastguard Worker   unsigned int sms_rect_feat[8];
100*77c1e3ccSAndroid Build Coastguard Worker   int sms_none_valid;
101*77c1e3ccSAndroid Build Coastguard Worker   int sms_rect_valid;
102*77c1e3ccSAndroid Build Coastguard Worker } SIMPLE_MOTION_DATA_TREE;
103*77c1e3ccSAndroid Build Coastguard Worker 
104*77c1e3ccSAndroid Build Coastguard Worker void av1_setup_shared_coeff_buffer(const SequenceHeader *const seq_params,
105*77c1e3ccSAndroid Build Coastguard Worker                                    PC_TREE_SHARED_BUFFERS *shared_bufs,
106*77c1e3ccSAndroid Build Coastguard Worker                                    struct aom_internal_error_info *error);
107*77c1e3ccSAndroid Build Coastguard Worker void av1_free_shared_coeff_buffer(PC_TREE_SHARED_BUFFERS *shared_bufs);
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker PC_TREE *av1_alloc_pc_tree_node(BLOCK_SIZE bsize);
110*77c1e3ccSAndroid Build Coastguard Worker void av1_free_pc_tree_recursive(PC_TREE *tree, int num_planes, int keep_best,
111*77c1e3ccSAndroid Build Coastguard Worker                                 int keep_none,
112*77c1e3ccSAndroid Build Coastguard Worker                                 PARTITION_SEARCH_TYPE partition_search_type);
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker PICK_MODE_CONTEXT *av1_alloc_pmc(const struct AV1_COMP *const cpi,
115*77c1e3ccSAndroid Build Coastguard Worker                                  BLOCK_SIZE bsize,
116*77c1e3ccSAndroid Build Coastguard Worker                                  PC_TREE_SHARED_BUFFERS *shared_bufs);
117*77c1e3ccSAndroid Build Coastguard Worker void av1_reset_pmc(PICK_MODE_CONTEXT *ctx);
118*77c1e3ccSAndroid Build Coastguard Worker void av1_free_pmc(PICK_MODE_CONTEXT *ctx, int num_planes);
119*77c1e3ccSAndroid Build Coastguard Worker void av1_copy_tree_context(PICK_MODE_CONTEXT *dst_ctx,
120*77c1e3ccSAndroid Build Coastguard Worker                            PICK_MODE_CONTEXT *src_ctx);
121*77c1e3ccSAndroid Build Coastguard Worker 
122*77c1e3ccSAndroid Build Coastguard Worker static const BLOCK_SIZE square[MAX_SB_SIZE_LOG2 - 1] = {
123*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_4X4, BLOCK_8X8, BLOCK_16X16, BLOCK_32X32, BLOCK_64X64, BLOCK_128X128,
124*77c1e3ccSAndroid Build Coastguard Worker };
125*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_pc_tree_nodes(const int is_sb_size_128,int stat_generation_stage)126*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_get_pc_tree_nodes(const int is_sb_size_128,
127*77c1e3ccSAndroid Build Coastguard Worker                                         int stat_generation_stage) {
128*77c1e3ccSAndroid Build Coastguard Worker   const int tree_nodes_inc = is_sb_size_128 ? 1024 : 0;
129*77c1e3ccSAndroid Build Coastguard Worker   const int tree_nodes =
130*77c1e3ccSAndroid Build Coastguard Worker       stat_generation_stage ? 1 : (tree_nodes_inc + 256 + 64 + 16 + 4 + 1);
131*77c1e3ccSAndroid Build Coastguard Worker   return tree_nodes;
132*77c1e3ccSAndroid Build Coastguard Worker }
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker // Returns 0 on success, -1 on memory allocation failure.
135*77c1e3ccSAndroid Build Coastguard Worker int av1_setup_sms_tree(struct AV1_COMP *const cpi, struct ThreadData *td);
136*77c1e3ccSAndroid Build Coastguard Worker void av1_free_sms_tree(struct ThreadData *td);
137*77c1e3ccSAndroid Build Coastguard Worker 
138*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
139*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
140*77c1e3ccSAndroid Build Coastguard Worker #endif
141*77c1e3ccSAndroid Build Coastguard Worker 
142*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_CONTEXT_TREE_H_
143