xref: /aosp_15_r20/external/libaom/av1/encoder/txb_rdopt.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2021, 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_TXB_RDOPT_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_TXB_RDOPT_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/txb_common.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
20*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
21*77c1e3ccSAndroid Build Coastguard Worker #endif
22*77c1e3ccSAndroid Build Coastguard Worker 
23*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Adjust the magnitude of quantized coefficients to achieve better
24*77c1e3ccSAndroid Build Coastguard Worker  * rate-distortion (RD) trade-off.
25*77c1e3ccSAndroid Build Coastguard Worker  *
26*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup coefficient_coding
27*77c1e3ccSAndroid Build Coastguard Worker  *
28*77c1e3ccSAndroid Build Coastguard Worker  * This function goes through each coefficient and greedily choose to lower
29*77c1e3ccSAndroid Build Coastguard Worker  * the coefficient magnitude by 1 or not based on the RD score.
30*77c1e3ccSAndroid Build Coastguard Worker  *
31*77c1e3ccSAndroid Build Coastguard Worker  * The coefficients are processing in reversed scan order.
32*77c1e3ccSAndroid Build Coastguard Worker  *
33*77c1e3ccSAndroid Build Coastguard Worker  * Note that, the end of block position (eob) may change if the original last
34*77c1e3ccSAndroid Build Coastguard Worker  * coefficient is lowered to zero.
35*77c1e3ccSAndroid Build Coastguard Worker  *
36*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    cpi            Top-level encoder structure
37*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
38*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
39*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane          The index of the current plane
40*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    block          The index of the current transform block in the
41*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_size        The transform size
42*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_type        The transform type
43*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    txb_ctx        Context info for entropy coding transform block
44*77c1e3ccSAndroid Build Coastguard Worker  * skip flag (tx_skip) and the sign of DC coefficient (dc_sign).
45*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]   rate_cost      The entropy cost of coding the transform block
46*77c1e3ccSAndroid Build Coastguard Worker  * after adjustment of coefficients.
47*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    sharpness      When sharpness > 0, the function will be less
48*77c1e3ccSAndroid Build Coastguard Worker  * aggressive towards lowering the magnitude of coefficients.
49*77c1e3ccSAndroid Build Coastguard Worker  * In this way, the transform block will contain more high-frequency
50*77c1e3ccSAndroid Build Coastguard Worker  * coefficients and therefore will preserve the sharpness of the reconstructed
51*77c1e3ccSAndroid Build Coastguard Worker  * block.
52*77c1e3ccSAndroid Build Coastguard Worker  */
53*77c1e3ccSAndroid Build Coastguard Worker int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
54*77c1e3ccSAndroid Build Coastguard Worker                      int block, TX_SIZE tx_size, TX_TYPE tx_type,
55*77c1e3ccSAndroid Build Coastguard Worker                      const TXB_CTX *const txb_ctx, int *rate_cost,
56*77c1e3ccSAndroid Build Coastguard Worker                      int sharpness);
57*77c1e3ccSAndroid Build Coastguard Worker 
58*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Compute the entropy cost of coding coefficients in a transform block.
59*77c1e3ccSAndroid Build Coastguard Worker  *
60*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup coefficient_coding
61*77c1e3ccSAndroid Build Coastguard Worker  *
62*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x                    Pointer to structure holding the data for
63*77c1e3ccSAndroid Build Coastguard Worker  the current encoding macroblock.
64*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane                The index of the current plane.
65*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    block                The index of the current transform block
66*77c1e3ccSAndroid Build Coastguard Worker  in the
67*77c1e3ccSAndroid Build Coastguard Worker  * macroblock. It's defined by number of 4x4 units that have been coded before
68*77c1e3ccSAndroid Build Coastguard Worker  * the currernt transform block.
69*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_size              The transform size.
70*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_type              The transform type.
71*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    txb_ctx              Context info for entropy coding transform
72*77c1e3ccSAndroid Build Coastguard Worker  block
73*77c1e3ccSAndroid Build Coastguard Worker  * skip flag (tx_skip) and the sign of DC coefficient (dc_sign).
74*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    reduced_tx_set_used  Whether the transform type is chosen from
75*77c1e3ccSAndroid Build Coastguard Worker  * a reduced set.
76*77c1e3ccSAndroid Build Coastguard Worker  */
77*77c1e3ccSAndroid Build Coastguard Worker int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
78*77c1e3ccSAndroid Build Coastguard Worker                         const TX_SIZE tx_size, const TX_TYPE tx_type,
79*77c1e3ccSAndroid Build Coastguard Worker                         const TXB_CTX *const txb_ctx, int reduced_tx_set_used);
80*77c1e3ccSAndroid Build Coastguard Worker 
81*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Estimate the entropy cost of coding a transform block using Laplacian
82*77c1e3ccSAndroid Build Coastguard Worker  * distribution.
83*77c1e3ccSAndroid Build Coastguard Worker  *
84*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup coefficient_coding
85*77c1e3ccSAndroid Build Coastguard Worker  *
86*77c1e3ccSAndroid Build Coastguard Worker  * This function compute the entropy costs of the end of block position (eob)
87*77c1e3ccSAndroid Build Coastguard Worker  * and the transform type (tx_type) precisely.
88*77c1e3ccSAndroid Build Coastguard Worker  *
89*77c1e3ccSAndroid Build Coastguard Worker  * Then using \ref av1_cost_coeffs_txb_estimate to estimate the entropy costs
90*77c1e3ccSAndroid Build Coastguard Worker  * of coefficients in the transform block.
91*77c1e3ccSAndroid Build Coastguard Worker  *
92*77c1e3ccSAndroid Build Coastguard Worker  * In the end, the function returns the sum of entropy costs of end of block
93*77c1e3ccSAndroid Build Coastguard Worker  * position (eob), transform type (tx_type) and coefficients.
94*77c1e3ccSAndroid Build Coastguard Worker  *
95*77c1e3ccSAndroid Build Coastguard Worker  * Compared to \ref av1_cost_coeffs_txb, this function is much faster but less
96*77c1e3ccSAndroid Build Coastguard Worker  * accurate.
97*77c1e3ccSAndroid Build Coastguard Worker  *
98*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
99*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
100*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane          The index of the current plane
101*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    block          The index of the current transform block in the
102*77c1e3ccSAndroid Build Coastguard Worker  * macroblock. It's defined by number of 4x4 units that have been coded before
103*77c1e3ccSAndroid Build Coastguard Worker  * the currernt transform block
104*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_size        The transform size
105*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_type        The transform type
106*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    txb_ctx        Context info for entropy coding transform block
107*77c1e3ccSAndroid Build Coastguard Worker  * skip flag (tx_skip) and the sign of DC coefficient (dc_sign).
108*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    reduced_tx_set_used  Whether the transform type is chosen from
109*77c1e3ccSAndroid Build Coastguard Worker  * a reduced set.
110*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    adjust_eob     Whether to adjust the end of block position
111*77c1e3ccSAndroid Build Coastguard Worker  (eob)
112*77c1e3ccSAndroid Build Coastguard Worker  * or not.
113*77c1e3ccSAndroid Build Coastguard Worker  * \return       int            Estimated entropy cost of coding the transform
114*77c1e3ccSAndroid Build Coastguard Worker  block.
115*77c1e3ccSAndroid Build Coastguard Worker  */
116*77c1e3ccSAndroid Build Coastguard Worker int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
117*77c1e3ccSAndroid Build Coastguard Worker                                   const int block, const TX_SIZE tx_size,
118*77c1e3ccSAndroid Build Coastguard Worker                                   const TX_TYPE tx_type,
119*77c1e3ccSAndroid Build Coastguard Worker                                   const TXB_CTX *const txb_ctx,
120*77c1e3ccSAndroid Build Coastguard Worker                                   const int reduced_tx_set_used,
121*77c1e3ccSAndroid Build Coastguard Worker                                   const int adjust_eob);
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Estimate the entropy cost of transform coefficients using Laplacian
124*77c1e3ccSAndroid Build Coastguard Worker  * distribution.
125*77c1e3ccSAndroid Build Coastguard Worker  *
126*77c1e3ccSAndroid Build Coastguard Worker  * \ingroup coefficient_coding
127*77c1e3ccSAndroid Build Coastguard Worker  *
128*77c1e3ccSAndroid Build Coastguard Worker  * This function assumes each transform coefficient is of its own Laplacian
129*77c1e3ccSAndroid Build Coastguard Worker  * distribution and the coefficient is the only observation of the Laplacian
130*77c1e3ccSAndroid Build Coastguard Worker  * distribution.
131*77c1e3ccSAndroid Build Coastguard Worker  *
132*77c1e3ccSAndroid Build Coastguard Worker  * Based on that, each coefficient's coding cost can be estimated by computing
133*77c1e3ccSAndroid Build Coastguard Worker  * the entropy of the corresponding Laplacian distribution.
134*77c1e3ccSAndroid Build Coastguard Worker  *
135*77c1e3ccSAndroid Build Coastguard Worker  * This function then return the sum of the estimated entropy cost for all
136*77c1e3ccSAndroid Build Coastguard Worker  * coefficients in the transform block.
137*77c1e3ccSAndroid Build Coastguard Worker  *
138*77c1e3ccSAndroid Build Coastguard Worker  * Note that the entropy cost of end of block (eob) and transform type (tx_type)
139*77c1e3ccSAndroid Build Coastguard Worker  * are not included.
140*77c1e3ccSAndroid Build Coastguard Worker  *
141*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    x              Pointer to structure holding the data for the
142*77c1e3ccSAndroid Build Coastguard Worker                                 current encoding macroblock
143*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    plane          The index of the current plane
144*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    block          The index of the current transform block in the
145*77c1e3ccSAndroid Build Coastguard Worker  * macroblock. It's defined by number of 4x4 units that have been coded before
146*77c1e3ccSAndroid Build Coastguard Worker  * the currernt transform block
147*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_size        The transform size
148*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]    tx_type        The transform type
149*77c1e3ccSAndroid Build Coastguard Worker  * \return       int            Estimated entropy cost of coefficients in the
150*77c1e3ccSAndroid Build Coastguard Worker  * transform block.
151*77c1e3ccSAndroid Build Coastguard Worker  */
152*77c1e3ccSAndroid Build Coastguard Worker int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane,
153*77c1e3ccSAndroid Build Coastguard Worker                                  const int block, const TX_SIZE tx_size,
154*77c1e3ccSAndroid Build Coastguard Worker                                  const TX_TYPE tx_type);
155*77c1e3ccSAndroid Build Coastguard Worker 
156*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
157*77c1e3ccSAndroid Build Coastguard Worker }
158*77c1e3ccSAndroid Build Coastguard Worker #endif
159*77c1e3ccSAndroid Build Coastguard Worker 
160*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_TXB_RDOPT_H_
161