xref: /aosp_15_r20/external/libaom/av1/encoder/tx_search.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2020, 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_TRANSFORM_SEARCH_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_TRANSFORM_SEARCH_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/pred_common.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
19*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
20*77c1e3ccSAndroid Build Coastguard Worker #endif
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker // Set this macro as 1 to collect data about tx size selection.
23*77c1e3ccSAndroid Build Coastguard Worker #define COLLECT_TX_SIZE_DATA 0
24*77c1e3ccSAndroid Build Coastguard Worker 
25*77c1e3ccSAndroid Build Coastguard Worker #if COLLECT_TX_SIZE_DATA
26*77c1e3ccSAndroid Build Coastguard Worker static const char av1_tx_size_data_output_file[] = "tx_size_data.txt";
27*77c1e3ccSAndroid Build Coastguard Worker #endif
28*77c1e3ccSAndroid Build Coastguard Worker 
29*77c1e3ccSAndroid Build Coastguard Worker enum {
30*77c1e3ccSAndroid Build Coastguard Worker   FTXS_NONE = 0,
31*77c1e3ccSAndroid Build Coastguard Worker   FTXS_DCT_AND_1D_DCT_ONLY = 1 << 0,
32*77c1e3ccSAndroid Build Coastguard Worker   FTXS_DISABLE_TRELLIS_OPT = 1 << 1,
33*77c1e3ccSAndroid Build Coastguard Worker   FTXS_USE_TRANSFORM_DOMAIN = 1 << 2
34*77c1e3ccSAndroid Build Coastguard Worker } UENUM1BYTE(FAST_TX_SEARCH_MODE);
35*77c1e3ccSAndroid Build Coastguard Worker 
tx_size_cost(const MACROBLOCK * const x,BLOCK_SIZE bsize,TX_SIZE tx_size)36*77c1e3ccSAndroid Build Coastguard Worker static inline int tx_size_cost(const MACROBLOCK *const x, BLOCK_SIZE bsize,
37*77c1e3ccSAndroid Build Coastguard Worker                                TX_SIZE tx_size) {
38*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize == x->e_mbd.mi[0]->bsize);
39*77c1e3ccSAndroid Build Coastguard Worker   if (x->txfm_search_params.tx_mode_search_type != TX_MODE_SELECT ||
40*77c1e3ccSAndroid Build Coastguard Worker       !block_signals_txsize(bsize))
41*77c1e3ccSAndroid Build Coastguard Worker     return 0;
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker   const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
44*77c1e3ccSAndroid Build Coastguard Worker   const int depth = tx_size_to_depth(tx_size, bsize);
45*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *const xd = &x->e_mbd;
46*77c1e3ccSAndroid Build Coastguard Worker   const int tx_size_ctx = get_tx_size_context(xd);
47*77c1e3ccSAndroid Build Coastguard Worker   return x->mode_costs.tx_size_cost[tx_size_cat][tx_size_ctx][depth];
48*77c1e3ccSAndroid Build Coastguard Worker }
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Compute the pixel domain distortion.
51*77c1e3ccSAndroid Build Coastguard Worker  *
52*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup transform_search
53*77c1e3ccSAndroid Build Coastguard Worker  * Compute the pixel domain distortion from diff on all visible 4x4s in the
54*77c1e3ccSAndroid Build Coastguard Worker  * transform block.
55*77c1e3ccSAndroid Build Coastguard Worker  *
56*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
57*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
58*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane          Plane index
59*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    blk_row        Block row index
60*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    blk_col        Block col index
61*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane_bsize    Current plane block size
62*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_bsize       Transform size
63*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    block_mse_q8   Block mse
64*77c1e3ccSAndroid Build Coastguard Worker  * \return       An int64_t value that is the block sse.
65*77c1e3ccSAndroid Build Coastguard Worker  */
66*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_pixel_diff_dist(const MACROBLOCK *x, int plane, int blk_row,
67*77c1e3ccSAndroid Build Coastguard Worker                             int blk_col, const BLOCK_SIZE plane_bsize,
68*77c1e3ccSAndroid Build Coastguard Worker                             const BLOCK_SIZE tx_bsize,
69*77c1e3ccSAndroid Build Coastguard Worker                             unsigned int *block_mse_q8);
70*77c1e3ccSAndroid Build Coastguard Worker 
71*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_estimate_txfm_yrd(const AV1_COMP *const cpi, MACROBLOCK *x,
72*77c1e3ccSAndroid Build Coastguard Worker                               RD_STATS *rd_stats, int64_t ref_best_rd,
73*77c1e3ccSAndroid Build Coastguard Worker                               BLOCK_SIZE bs, TX_SIZE tx_size);
74*77c1e3ccSAndroid Build Coastguard Worker 
75*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Recursive transform size and type search.
76*77c1e3ccSAndroid Build Coastguard Worker  *
77*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup transform_search
78*77c1e3ccSAndroid Build Coastguard Worker  * Search for best transform size and type for luma inter blocks. The transform
79*77c1e3ccSAndroid Build Coastguard Worker  * block partitioning can be recursive resulting in non-uniform transform sizes.
80*77c1e3ccSAndroid Build Coastguard Worker  * The best transform size and type, if found, will be saved in the MB_MODE_INFO
81*77c1e3ccSAndroid Build Coastguard Worker  * structure, and the corresponding RD stats will be saved in rd_stats.
82*77c1e3ccSAndroid Build Coastguard Worker  *
83*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
84*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
85*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
86*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats       Pointer to struct to keep track of the RD stats
87*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize          Current macroblock size
88*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ref_best_rd    Best RD cost seen for this block so far
89*77c1e3ccSAndroid Build Coastguard Worker  * \remark       Nothing is returned. The selected transform size and type will
90*77c1e3ccSAndroid Build Coastguard Worker                  be saved in the MB_MODE_INFO structure
91*77c1e3ccSAndroid Build Coastguard Worker  */
92*77c1e3ccSAndroid Build Coastguard Worker void av1_pick_recursive_tx_size_type_yrd(const AV1_COMP *cpi, MACROBLOCK *x,
93*77c1e3ccSAndroid Build Coastguard Worker                                          RD_STATS *rd_stats, BLOCK_SIZE bsize,
94*77c1e3ccSAndroid Build Coastguard Worker                                          int64_t ref_best_rd);
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Uniform transform size and type search.
97*77c1e3ccSAndroid Build Coastguard Worker  *
98*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup transform_search
99*77c1e3ccSAndroid Build Coastguard Worker  * Search for the best transform size and type for current macroblock block,
100*77c1e3ccSAndroid Build Coastguard Worker  * with the assumption that all the transform blocks have a uniform size
101*77c1e3ccSAndroid Build Coastguard Worker  * (VP9 style). The selected transform size and type will be saved in the
102*77c1e3ccSAndroid Build Coastguard Worker  * MB_MODE_INFO structure; the corresponding RD stats will be saved in rd_stats.
103*77c1e3ccSAndroid Build Coastguard Worker  * This function may be used for both intra and inter predicted blocks.
104*77c1e3ccSAndroid Build Coastguard Worker  *
105*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
106*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
107*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
108*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats       Pointer to struct to keep track of the RD stats
109*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bs             Current macroblock size
110*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ref_best_rd    Best RD cost seen for this block so far
111*77c1e3ccSAndroid Build Coastguard Worker  * \remark       Nothing is returned. The selected transform size and type will
112*77c1e3ccSAndroid Build Coastguard Worker                  be saved in the MB_MODE_INFO structure
113*77c1e3ccSAndroid Build Coastguard Worker  */
114*77c1e3ccSAndroid Build Coastguard Worker void av1_pick_uniform_tx_size_type_yrd(const AV1_COMP *const cpi, MACROBLOCK *x,
115*77c1e3ccSAndroid Build Coastguard Worker                                        RD_STATS *rd_stats, BLOCK_SIZE bs,
116*77c1e3ccSAndroid Build Coastguard Worker                                        int64_t ref_best_rd);
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Chroma block transform search.
119*77c1e3ccSAndroid Build Coastguard Worker  *
120*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup transform_search
121*77c1e3ccSAndroid Build Coastguard Worker  * Calculate the transform coefficient RD cost for the given chroma macroblock
122*77c1e3ccSAndroid Build Coastguard Worker  * If the current mode is intra, then this function will compute the predictor.
123*77c1e3ccSAndroid Build Coastguard Worker  *
124*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
125*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
126*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
127*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats       Pointer to struct to keep track of the RD stats
128*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize          Current macroblock size
129*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ref_best_rd    Best RD cost seen for this block so far
130*77c1e3ccSAndroid Build Coastguard Worker  * \return       An integer value is returned. 0: early termination triggered,
131*77c1e3ccSAndroid Build Coastguard Worker                  no valid rd cost available; 1: rd cost values are valid.
132*77c1e3ccSAndroid Build Coastguard Worker  */
133*77c1e3ccSAndroid Build Coastguard Worker int av1_txfm_uvrd(const AV1_COMP *const cpi, MACROBLOCK *x, RD_STATS *rd_stats,
134*77c1e3ccSAndroid Build Coastguard Worker                   BLOCK_SIZE bsize, int64_t ref_best_rd);
135*77c1e3ccSAndroid Build Coastguard Worker 
136*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Transform type search with fixed transform size.
137*77c1e3ccSAndroid Build Coastguard Worker  *
138*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup transform_search
139*77c1e3ccSAndroid Build Coastguard Worker  * Search for the best transform type and calculate the transform coefficients
140*77c1e3ccSAndroid Build Coastguard Worker  * RD cost of the current transform block with the specified (uniform) transform
141*77c1e3ccSAndroid Build Coastguard Worker  * size and plane. The RD results will be saved in rd_stats.
142*77c1e3ccSAndroid Build Coastguard Worker  *
143*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
144*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
145*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
146*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats       Pointer to struct to keep track of the RD stats
147*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ref_best_rd    Best RD cost seen for this block so far
148*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    current_rd     Current RD cost for this block so far
149*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane          Plane index
150*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane_bsize    Size of the current macroblock considering
151*77c1e3ccSAndroid Build Coastguard Worker                                 sup-sampling
152*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_size        The given transform size
153*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ftxs_mode      Transform search mode specifying desired speed
154*77c1e3ccSAndroid Build Coastguard Worker                                 and quality tradeoff
155*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    skip_trellis   Binary flag indicating if trellis optimization
156*77c1e3ccSAndroid Build Coastguard Worker                                 should be skipped
157*77c1e3ccSAndroid Build Coastguard Worker  *
158*77c1e3ccSAndroid Build Coastguard Worker  * \remark       Nothing is returned. The RD results will be saved in rd_stats.
159*77c1e3ccSAndroid Build Coastguard Worker  */
160*77c1e3ccSAndroid Build Coastguard Worker void av1_txfm_rd_in_plane(MACROBLOCK *x, const AV1_COMP *cpi,
161*77c1e3ccSAndroid Build Coastguard Worker                           RD_STATS *rd_stats, int64_t ref_best_rd,
162*77c1e3ccSAndroid Build Coastguard Worker                           int64_t current_rd, int plane, BLOCK_SIZE plane_bsize,
163*77c1e3ccSAndroid Build Coastguard Worker                           TX_SIZE tx_size, FAST_TX_SEARCH_MODE ftxs_mode,
164*77c1e3ccSAndroid Build Coastguard Worker                           int skip_trellis);
165*77c1e3ccSAndroid Build Coastguard Worker 
166*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Recursive transform size and type search.
167*77c1e3ccSAndroid Build Coastguard Worker  *
168*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup transform_search
169*77c1e3ccSAndroid Build Coastguard Worker  * This function combines y and uv planes' transform search processes together
170*77c1e3ccSAndroid Build Coastguard Worker  * for inter-predicted blocks (including IntraBC), when the prediction is
171*77c1e3ccSAndroid Build Coastguard Worker  * already generated. It first does subtraction to obtain the prediction error.
172*77c1e3ccSAndroid Build Coastguard Worker  * Then it calls
173*77c1e3ccSAndroid Build Coastguard Worker  * av1_pick_recursive_tx_size_type_yrd/av1_pick_uniform_tx_size_type_yrd and
174*77c1e3ccSAndroid Build Coastguard Worker  * av1_txfm_uvrd sequentially and handles possible early terminations.
175*77c1e3ccSAndroid Build Coastguard Worker  * The RD metrics are calculated and stored in rd_stats/_y/_uv.
176*77c1e3ccSAndroid Build Coastguard Worker  *
177*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
178*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
179*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
180*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    bsize          Current macroblock size
181*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats       Pointer to struct to keep track of the overal RD
182*77c1e3ccSAndroid Build Coastguard Worker                                 stats
183*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats_y     Pointer to struct to keep track of the RD
184*77c1e3ccSAndroid Build Coastguard Worker                                 stats for the luma plane
185*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    rd_stats_uv    Pointer to struct to keep track of the RD
186*77c1e3ccSAndroid Build Coastguard Worker                                 stats for the chroma planes
187*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    mode_rate      Rate cost to encode the prediction mode info. of
188*77c1e3ccSAndroid Build Coastguard Worker                                 the current macroblock
189*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    ref_best_rd    Best RD cost seen for this block so far
190*77c1e3ccSAndroid Build Coastguard Worker  *
191*77c1e3ccSAndroid Build Coastguard Worker  * \return       An integer value is returned indicating if a valid transform
192*77c1e3ccSAndroid Build Coastguard Worker                  candidate is found (1) or not (0).
193*77c1e3ccSAndroid Build Coastguard Worker  */
194*77c1e3ccSAndroid Build Coastguard Worker int av1_txfm_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
195*77c1e3ccSAndroid Build Coastguard Worker                     RD_STATS *rd_stats, RD_STATS *rd_stats_y,
196*77c1e3ccSAndroid Build Coastguard Worker                     RD_STATS *rd_stats_uv, int mode_rate, int64_t ref_best_rd);
197*77c1e3ccSAndroid Build Coastguard Worker 
198*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
199*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
200*77c1e3ccSAndroid Build Coastguard Worker #endif
201*77c1e3ccSAndroid Build Coastguard Worker 
202*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_TRANSFORM_SEARCH_H_
203