1 /******************************************************************************
2  *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /**
22 *******************************************************************************
23 * @file
24 *  ihevce_stasino_helpers.h
25 *
26 * @brief
27 *
28 *
29 * @author
30 *  Ittiam
31 *
32 * @remarks
33 *  None
34 *
35 *******************************************************************************
36 */
37 
38 #ifndef _IHEVCE_STASINO_HELPERS_H_
39 #define _IHEVCE_STASINO_HELPERS_H_
40 
41 #include <math.h>
42 /****************************************************************************/
43 /* Constant Macros                                                          */
44 /****************************************************************************/
45 
46 /****************************************************************************/
47 /* Function Macros                                                          */
48 /****************************************************************************/
49 #define MULTIPLY_STIM_WITH_DISTORTION(dist, stimXalpha, stim_q_level, alpha_q_level)               \
50     {                                                                                              \
51         ULWORD64 u8_pure_dist = (dist);                                                            \
52         WORD32 i4_q_level = stim_q_level + alpha_q_level;                                          \
53                                                                                                    \
54         u8_pure_dist *= ((1 << (i4_q_level)) - (stimXalpha));                                      \
55         u8_pure_dist += (1 << ((i4_q_level)-1));                                                   \
56         (dist) = u8_pure_dist >> (i4_q_level);                                                     \
57     }
58 
59 /****************************************************************************/
60 /* Typedefs                                                                 */
61 /****************************************************************************/
62 
63 /****************************************************************************/
64 /* Enums                                                                    */
65 /****************************************************************************/
66 
67 /****************************************************************************/
68 /* Structure                                                                */
69 /****************************************************************************/
70 
71 /****************************************************************************/
72 /* Function Prototypes                                                      */
73 /****************************************************************************/
74 
75 void ihevce_calc_variance(
76     void *pv_input,
77     WORD32 i4_stride,
78     WORD32 *pi4_mean,
79     UWORD32 *pu4_variance,
80     UWORD8 u1_block_height,
81     UWORD8 u1_block_width,
82     UWORD8 u1_is_hbd,
83     UWORD8 u1_disable_normalization);
84 
85 void ihevce_calc_variance_signed(
86     WORD16 *pv_input,
87     WORD32 i4_stride,
88     WORD32 *pi4_mean,
89     UWORD32 *pu4_variance,
90     UWORD8 u1_block_height,
91     UWORD8 u1_block_width);
92 
93 void ihevce_calc_chroma_variance(
94     void *pv_input,
95     WORD32 i4_stride,
96     WORD32 *pi4_mean,
97     UWORD32 *pu4_variance,
98     UWORD8 u1_block_height,
99     UWORD8 u1_block_width,
100     UWORD8 u1_is_hbd,
101     CHROMA_PLANE_ID_T e_chroma_plane);
102 
ihevce_compute_stim(UWORD32 u4_variance1,UWORD32 u4_variance2)103 static INLINE UWORD32 ihevce_compute_stim(UWORD32 u4_variance1, UWORD32 u4_variance2)
104 {
105     return (u4_variance1 == u4_variance2)
106                ? (1 << STIM_Q_FORMAT)
107                : ((UWORD32)(
108                      ((2 * (double)u4_variance1 * (double)u4_variance2) /
109                       (pow((double)u4_variance1, 2) + pow((double)u4_variance2, 2))) *
110                      pow((double)2, STIM_Q_FORMAT)));
111 }
112 
113 LWORD64 ihevce_inject_stim_into_distortion(
114     void *pv_src,
115     WORD32 i4_src_stride,
116     void *pv_pred,
117     WORD32 i4_pred_stride,
118     LWORD64 i8_distortion,
119     WORD32 i4_alpha_stim_multiplier,
120     UWORD8 u1_blk_size,
121     UWORD8 u1_is_hbd,
122     UWORD8 u1_enable_psyRDOPT,
123     CHROMA_PLANE_ID_T e_chroma_plane);
124 
ihevce_derive_noise_weighted_alpha_stim_multiplier(WORD32 i4_alpha,UWORD32 u4SrcVar,UWORD32 u4PredVar,WORD32 i4_stim)125 static INLINE WORD32 ihevce_derive_noise_weighted_alpha_stim_multiplier(
126     WORD32 i4_alpha, UWORD32 u4SrcVar, UWORD32 u4PredVar, WORD32 i4_stim)
127 {
128     (void)u4SrcVar;
129     (void)u4PredVar;
130     (void)i4_stim;
131     return i4_alpha;
132 }
133 
ihevce_compute_noise_term(WORD32 i4_alpha,UWORD32 u4SrcVar,UWORD32 u4PredVar)134 static INLINE WORD32 ihevce_compute_noise_term(WORD32 i4_alpha, UWORD32 u4SrcVar, UWORD32 u4PredVar)
135 {
136     if(i4_alpha)
137     {
138         WORD32 i4_stim = ihevce_compute_stim(u4SrcVar, u4PredVar);
139 
140         ASSERT(i4_stim >= 0);
141 
142         i4_alpha = ihevce_derive_noise_weighted_alpha_stim_multiplier(
143             i4_alpha, u4SrcVar, u4PredVar, i4_stim);
144 
145         return i4_stim * i4_alpha;
146     }
147     else
148     {
149         return 0;
150     }
151 }
152 
153 UWORD8 ihevce_determine_cu_noise_based_on_8x8Blk_data(
154     UWORD8 *pu1_is_8x8Blk_noisy, UWORD8 u1_cu_x_pos, UWORD8 u1_cu_y_pos, UWORD8 u1_cu_size);
155 
156 LWORD64 ihevce_psy_rd_cost_croma(
157     LWORD64 *pui4_source_satd,
158     void *p_recon,
159     WORD32 recon_stride_vert,
160     WORD32 recond_stride_horz,
161     WORD32 cu_size_luma,
162     WORD32 pic_type,
163     WORD32 layer_id,
164     WORD32 lambda,
165     WORD32 start_index,
166     WORD32 is_hbd,
167     WORD32 sub_sampling_type,
168     ihevce_cmn_opt_func_t *ps_cmn_utils_optimised_function_list
169 
170 );
171 
172 LWORD64 ihevce_psy_rd_cost(
173     LWORD64 *pui4_source_satd,
174     void *pv_recon,
175     WORD32 recon_stride_vert,
176     WORD32 recond_stride_horz,
177     WORD32 cu_size,
178     WORD32 pic_type,
179     WORD32 layer_id,
180     WORD32 lambda,
181     WORD32 start_index,
182     WORD32 is_hbd,
183     UWORD32 u4_psy_strength,
184     ihevce_cmn_opt_func_t *ps_cmn_utils_optimised_function_list);
185 
186 WORD32 ihevce_ctb_noise_detect(
187     UWORD8 *pu1_l0_ctb,
188     WORD32 l0_stride,
189     UWORD8 *pu1_l1_ctb,
190     WORD32 l1_stride,
191     WORD32 had_block_size,
192     WORD32 ctb_width,
193     WORD32 ctb_height,
194     ihevce_ctb_noise_params *ps_ctb_noise_params,
195     WORD32 ctb_height_offset,
196     WORD32 ctb_width_offset,
197     WORD32 frame_height,
198     WORD32 frame_width);
199 
200 void ihevce_had4_4x4_noise_detect(
201     UWORD8 *pu1_src,
202     WORD32 src_strd,
203     UWORD8 *pu1_pred,
204     WORD32 pred_strd,
205     WORD16 *pi2_dst4x4,
206     WORD16 *pi2_residue,
207     WORD32 dst_strd,
208     WORD32 scaling_for_pred);
209 
210 WORD32 ihevce_had_16x16_r_noise_detect(
211     UWORD8 *pu1_src,
212     WORD32 src_strd,
213     UWORD8 *pu1_pred,
214     WORD32 pred_strd,
215     WORD16 *pi2_dst,
216     WORD32 dst_strd,
217     WORD32 pos_x_y_4x4,
218     WORD32 num_4x4_in_row,
219     WORD32 scaling_for_pred);
220 
221 UWORD32 ihevce_compute_8x8HAD_using_4x4_noise_detect(
222     WORD16 *pi2_4x4_had,
223     WORD32 had4_strd,
224     WORD16 *pi2_dst,
225     WORD32 dst_strd,
226     WORD32 i4_frm_qstep,
227     WORD32 *pi4_cbf);
228 
229 void ihevce_had_8x8_using_4_4x4_noise_detect(
230     UWORD8 *pu1_src,
231     WORD32 src_strd,
232     UWORD8 *pu1_pred,
233     WORD32 pred_strd,
234     WORD16 *pi2_dst,
235     WORD32 dst_strd,
236     WORD32 pos_x_y_4x4,
237     WORD32 num_4x4_in_row,
238     WORD32 scaling_for_pred);
239 
240 unsigned long ihevce_calc_stim_injected_variance(
241     ULWORD64 *pu8_sigmaX,
242     ULWORD64 *pu8_sigmaXSquared,
243     ULWORD64 *u8_var,
244     WORD32 i4_inv_wpred_wt,
245     WORD32 i4_inv_wt_shift_val,
246     WORD32 i4_wpred_log_wdc,
247     WORD32 i4_part_id);
248 
249 unsigned long ihevce_calc_variance_for_diff_weights(
250     ULWORD64 *pu8_sigmaX,
251     ULWORD64 *pu8_sigmaXSquared,
252     ULWORD64 *u8_var,
253     WORD32 *pi4_inv_wt,
254     WORD32 *pi4_inv_wt_shift_val,
255     pu_result_t *ps_result,
256     WORD32 i4_wpred_log_wdc,
257     PART_ID_T *pe_part_id,
258     UWORD8 u1_cu_size,
259     UWORD8 u1_num_parts,
260     UWORD8 u1_is_for_src);
261 
262 #endif /* _IHEVCE_STASINO_HELPERS_H_ */
263