aboutsummaryrefslogtreecommitdiff
path: root/encoder/ihevce_rdoq_macros.h
blob: 63ea9a3090193fc617012113e0b5dba5c1d688c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
*******************************************************************************
* @file
*  ihevc_rdoq_macros.h
*
* @brief
*  Macros used for RDOQ algorthm
*
* @author
*  Ittiam
*
* @remarks
*  None
*
*******************************************************************************
*/
#ifndef IHEVC_RDOQ_MACROS_H_
#define IHEVC_RDOQ_MACROS_H_

/*****************************************************************************/
/* Constant Macros                                                           */
/*****************************************************************************/
/*Used for calculating the distortion in the transform domain*/
#define CALC_SSD_IN_TRANS_DOMAIN(a, b, i4_round_val, i4_shift_val)                                 \
    (SHR_NEG(((((a) - (b)) * ((a) - (b))) + i4_round_val), i4_shift_val))
#define CALC_CUMMUL_SSD_IN_TRANS_DOMAIN(a, b, i4_round_val, i4_shift_val)                          \
    (SHR_NEG((((a) - (b)) + i4_round_val), i4_shift_val))

#define MAX_INT 0x7FFFFFFF

#define COMPUTE_RATE_COST_CLIP30_RDOQ(r, l, qshift)                                                \
    ((WORD32)CLIP30((((ULWORD64)r) * ((ULWORD64)l)) >> (qshift)))

/*This macro is required to test the RDOQ changes*/
/*1 implies cabac context validation using the test-bench*/
/*Also prints some debug information*/
#define TEST_BENCH_RDOQ 0

/*Macro to enable and disable coefficient RDOQ. When 1, coefficient RDOQ is enabled*/
#define COEFF_RDOQ 0

/*Macro to optimize the copying of cabac states across various temp/scratch cabac contexts
  Should always be 0 when COEFF_RDOQ is 1*/
#define OPT_MEMCPY 1

/** Macro which accounts subtracts 4096 bits from the total bits generated per TU in the RDOPT stage
    if SBH is on*/
#define ACCOUNT_SIGN_BITS 0

/*Macro defining the maximum number of context elements in the cabac state*/
//#define MAX_NUM_CONTEXT_ELEMENTS 5

/*****************************************************************************/
/* Enum                                                                      */
/*****************************************************************************/
/*Enum to indicate which context element in the cabac state is currently being altered*/
typedef enum
{
    LASTXY,
    SUB_BLK_CODED_FLAG,
    SIG_COEFF,
    GRTR_THAN_1,
    GRTR_THAN_2,
    MAX_NUM_CONTEXT_ELEMENTS
} BACKUP_CTXT_ELEMENTS;

/*****************************************************************************/
/* Structures                                                                */
/*****************************************************************************/

/*Structure defined to optimize copying of cabac states across various temporary/scratch cabac states*/
typedef struct
{
    // clang-format off
    /**
    ai4_ctxt_to_backup[x] tells us if xth element has been altered. where
    x       context element                 Meaning
    0       IHEVC_CAB_COEFFX_PREFIX         lastx last y has been coded
    1       IHEVC_CAB_CODED_SUBLK_IDX       sub-blk coded or not flag has been coded
    2       IHEVC_CAB_COEFF_FLAG            sigcoeff has been coded
    3       IHEVC_CAB_COEFABS_GRTR1_FLAG    greater than 1 bin has been coded
    4       IHEVC_CAB_COEFABS_GRTR2_FLAG    greater than 2 bin has been coded
    */
    // clang-format on
    UWORD8 au1_ctxt_to_backup[MAX_NUM_CONTEXT_ELEMENTS];

    /** Number of bits generated */
    WORD32 i4_num_bits;
} backup_ctxt_t;

/**
******************************************************************************
* @brief Structure to store the position of the coefficient to be changed
         through SBH
******************************************************************************
 */
typedef struct
{
    UWORD8 x;
    UWORD8 y;
    UWORD8 is_valid_pos;
    WORD16 i2_old_coeff;
} s_sbh_coeff_pos_t;

/**
******************************************************************************
 *  @brief  RDOQ SBH context for cabac bit estimation etc
******************************************************************************
 */

typedef struct
{
    /** TU size */
    WORD32 i4_trans_size;

    /** Log 2 TU size */
    WORD32 i4_log2_trans_size;

    /**
     * Boolean value representing if the current TU is luma or not.
     * 1 => Luma
     */
    WORD32 i4_is_luma;

    /**
     * Calculate rounding and shifting values required for normalizing original
     * and inverse quantized transform coefficients (for calculation of SSD in
     * transform domain)
     */
    WORD32 i4_round_val_ssd_in_td;
    WORD32 i4_shift_val_ssd_in_td;

    /** Matrix used in inverse quantization */
    WORD32 quant_scale_mat_offset;

    /** Index of the csb within the TU*/
    WORD32 i4_trans_idx;

    /** value of lambda used in the D+Rlambda metric*/
    LWORD64 i8_cl_ssd_lambda_qf;

    /** Used while inverse quantizing*/
    WORD16 i2_qp_rem;
    WORD32 i4_qp_div;

    /** Scan index of the csbs within the TU   */
    WORD32 i4_scan_idx;

    /** Pointer to the csbf buf. This buffer will contain 1 if the csb is coded
     *  and 0 if it is not*/
    UWORD8 *pu1_csbf_buf;

    /** Boolean value which is 1 if any of the csbs in the current TU are
     * coded*/
    UWORD8 i1_tu_is_coded;

    /**
     * Pointer to an array of pointer to store the scaling matrices for
     * all transform sizes and qp % 6 (pre computed)
     */
    WORD16 *pi2_dequant_coeff;

    /** Pointer to the quantized coeffs*/
    WORD16 *pi2_quant_coeffs;

    /** Pointer to the inverse quantized values*/
    WORD16 *pi2_iquant_coeffs;

    /** Pointer ot the transformed values(before quantization) */
    WORD16 *pi2_trans_values;

    /** Stride of the inverse quant data*/
    WORD32 i4_iq_data_strd;

    /** Stride of the quant data*/
    WORD32 i4_q_data_strd;

    /** Intermediate array to store transform output for RDOQ*/
    WORD16 ai2_trans_values[MAX_TRANS_SIZE];

    /** Pointer to zero rows and zero cols*/
    WORD32 *pi4_zero_row;
    WORD32 *pi4_zero_col;

    /** Array containing information about the position of the coefficient
     * to be altered during SBH
     */
    s_sbh_coeff_pos_t s_best_pos[(MAX_TU_SIZE * MAX_TU_SIZE / 4 / 4) + 1];

    /** SSD cost for this particular TU*/
    LWORD64 i8_ssd_cost;

    WORD32 i4_perform_all_cand_rdoq;
    WORD32 i4_perform_best_cand_rdoq;
    WORD32 i4_perform_all_cand_sbh;
    WORD32 i4_perform_best_cand_sbh;

    WORD32 i4_bit_depth;

    WORD32 *pi4_subBlock2csbfId_map;

} rdoq_sbh_ctxt_t;

/*****************************************************************************/
/* Extern Function Declarations                                              */
/*****************************************************************************/

void ihevce_sign_data_hiding(rdoq_sbh_ctxt_t *ps_rdoq_sbh_params);

#endif /* IHEVC_RDOQ_MACROS_H_ */