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 * \file ihevce_enc_loop_utils.h
23 *
24 * \brief
25 *    This file contains interface defination of frame proceswsing pass
26 *
27 * \date
28 *    18/09/2012
29 *
30 * \author
31 *    Ittiam
32 *
33 ******************************************************************************
34 */
35 
36 #ifndef _IHEVCE_ENC_LOOP_UTILS_H_
37 #define _IHEVCE_ENC_LOOP_UTILS_H_
38 
39 /*****************************************************************************/
40 /* Constant Macros                                                           */
41 /*****************************************************************************/
42 #define INTRA_ENC_DBG_L0 1  // Frame Level
43 #define INTRA_ENC_DBG_L1 1  // CTB Row Level
44 #define INTRA_ENC_DBG_L2 0  // CTB/CU Level
45 #define INTRA_ENC_DBG_L3 0  // PU/TU Level
46 #define INTRA_ENC_DBG_L4 0  // Pixel Level
47 /*****************************************************************************/
48 /* Function Macros                                                           */
49 /*****************************************************************************/
50 
51 #define CABAC_FRAC_BITS_Q_SHIFT (1 << CABAC_FRAC_BITS_Q)
52 #define LAMDA_Q_SHIFT_FACT 20
53 
54 #define QUANT_ROUND_FACTOR(out, r1, r0, lambda)                                                    \
55     {                                                                                              \
56         LWORD64 temp3_m;                                                                           \
57         LWORD64 temp;                                                                              \
58         temp3_m = (((r1 - r0) * lambda));                                                          \
59         temp = (CLIP3(                                                                             \
60             ((CABAC_FRAC_BITS_Q_SHIFT -                                                            \
61               ((((LWORD64)(temp3_m) + ((LWORD64)CABAC_FRAC_BITS_Q_SHIFT << LAMDA_Q_SHIFT_FACT)) /  \
62                 2) >>                                                                              \
63                LAMDA_Q_SHIFT_FACT))),                                                              \
64             0,                                                                                     \
65             (CABAC_FRAC_BITS_Q_SHIFT >> 1)));                                                      \
66         out = ((WORD32)(temp * (1 << QUANT_ROUND_FACTOR_Q))) >> CABAC_FRAC_BITS_Q;                 \
67     }
68 
69 /*****************************************************************************/
70 /* Typedefs                                                                  */
71 /*****************************************************************************/
72 
73 /*****************************************************************************/
74 /* Enums                                                                     */
75 /*****************************************************************************/
76 
77 /*****************************************************************************/
78 /* Structure                                                                 */
79 /*****************************************************************************/
80 
81 /*****************************************************************************/
82 /* Extern Variable Declarations                                              */
83 /*****************************************************************************/
84 
85 /*****************************************************************************/
86 /* Extern Function Declarations                                              */
87 /*****************************************************************************/
88 
89 void ihevce_get_cl_cu_lambda_prms(ihevce_enc_loop_ctxt_t *ps_ctxt, WORD32 i4_cur_cu_qp);
90 
91 void ihevce_populate_cl_cu_lambda_prms(
92     ihevce_enc_loop_ctxt_t *ps_ctxt,
93     frm_lambda_ctxt_t *ps_frm_lamda,
94     WORD32 i4_slice_type,
95     WORD32 i4_temporal_lyr_id,
96     WORD32 i4_lambda_type);
97 
98 void ihevce_compute_quant_rel_param(ihevce_enc_loop_ctxt_t *ps_ctxt, WORD8 i1_cu_qp);
99 
100 void ihevce_compute_cu_level_QP(
101     ihevce_enc_loop_ctxt_t *ps_ctxt,
102     WORD32 i4_activity_for_qp,
103     WORD32 i4_activity_for_lamda,
104     WORD32 i4_reduce_qp);
105 
106 void ihevce_update_cu_level_qp_lamda(
107     ihevce_enc_loop_ctxt_t *ps_ctxt,
108     cu_analyse_t *ps_cu_analyse,
109     WORD32 trans_size,
110     WORD32 is_intra);
111 
112 WORD32 ihevce_scan_coeffs(
113     WORD16 *pi2_quant_coeffs,
114     WORD32 *pi4_subBlock2csbfId_map,
115     WORD32 scan_idx,
116     WORD32 trans_size,
117     UWORD8 *pu1_out_data,
118     UWORD8 *pu1_csbf_buf,
119     WORD32 i4_csbf_stride);
120 
121 void ihevce_populate_intra_pred_mode(
122     WORD32 top_intra_mode,
123     WORD32 left_intra_mode,
124     WORD32 available_top,
125     WORD32 available_left,
126     WORD32 cu_pos_y,
127     WORD32 *ps_cand_mode_list);
128 
129 void ihevce_intra_pred_mode_signaling(
130     WORD32 top_intra_mode,
131     WORD32 left_intra_mode,
132     WORD32 available_top,
133     WORD32 available_left,
134     WORD32 cu_pos_y,
135     WORD32 luma_intra_pred_mode_current,
136     intra_prev_rem_flags_t *ps_intra_pred_mode_current);
137 void ihevce_chroma_interleave_2d_copy(
138     UWORD8 *pu1_uv_src_bp,
139     WORD32 src_strd,
140     UWORD8 *pu1_uv_dst_bp,
141     WORD32 dst_strd,
142     WORD32 w,
143     WORD32 h,
144     CHROMA_PLANE_ID_T e_chroma_plane);
145 
146 WORD32 ihevce_t_q_iq_ssd_scan_fxn(
147     ihevce_enc_loop_ctxt_t *ps_ctxt,
148     UWORD8 *pu1_pred,
149     WORD32 pred_strd,
150     UWORD8 *pu1_src,
151     WORD32 src_strd,
152     WORD16 *pi2_deq_data,
153     WORD32 deq_data_strd,
154     UWORD8 *pu1_recon,
155     WORD32 i4_recon_stride,
156     UWORD8 *pu1_ecd_data,
157     UWORD8 *pu1_csbf_buf,
158     WORD32 csbf_strd,
159     WORD32 trans_size,
160     WORD32 packed_pred_mode,
161     LWORD64 *pi8_cost,
162     WORD32 *pi4_coeff_off,
163     WORD32 *pi4_tu_bits,
164     UWORD32 *pu4_blk_sad,
165     WORD32 *pi4_zero_col,
166     WORD32 *pi4_zero_row,
167     UWORD8 *pu1_is_recon_available,
168     WORD32 i4_perform_rdoq,
169     WORD32 i4_perform_sbh,
170 #if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
171     WORD32 i4_alpha_stim_multiplier,
172     UWORD8 u1_is_cu_noisy,
173 #endif
174     SSD_TYPE_T e_ssd_type,
175     WORD32 early_cbf);
176 
177 void ihevce_quant_rounding_factor_gen(
178     WORD32 i4_trans_size,
179     WORD32 is_luma,
180     rdopt_entropy_ctxt_t *ps_rdopt_entropy_ctxt,
181     WORD32 *pi4_quant_round_0_1,
182     WORD32 *pi4_quant_round_1_2,
183     double i4_lamda_modifier,
184     UWORD8 i4_is_tu_level_quant_rounding);
185 
186 void ihevce_it_recon_fxn(
187     ihevce_enc_loop_ctxt_t *ps_ctxt,
188     WORD16 *pi2_deq_data,
189     WORD32 deq_dat_strd,
190     UWORD8 *pu1_pred,
191     WORD32 pred_strd,
192     UWORD8 *pu1_recon,
193     WORD32 recon_strd,
194     UWORD8 *pu1_ecd_data,
195     WORD32 trans_size,
196     WORD32 packed_pred_mode,
197     WORD32 cbf,
198     WORD32 zero_cols,
199     WORD32 zero_rows);
200 
201 void ihevce_chroma_it_recon_fxn(
202     ihevce_enc_loop_ctxt_t *ps_ctxt,
203     WORD16 *pi2_deq_data,
204     WORD32 deq_dat_strd,
205     UWORD8 *pu1_pred,
206     WORD32 pred_strd,
207     UWORD8 *pu1_recon,
208     WORD32 recon_strd,
209     UWORD8 *pu1_ecd_data,
210     WORD32 trans_size,
211     WORD32 cbf,
212     WORD32 zero_cols,
213     WORD32 zero_rows,
214     CHROMA_PLANE_ID_T e_chroma_plane);
215 
216 void ihevce_mpm_idx_based_filter_RDOPT_cand(
217     ihevce_enc_loop_ctxt_t *ps_ctxt,
218     cu_analyse_t *ps_cu_analyse,
219     nbr_4x4_t *ps_left_nbr_4x4,
220     nbr_4x4_t *ps_top_nbr_4x4,
221     UWORD8 *pu1_luma_mode,
222     UWORD8 *pu1_eval_mark);
223 
224 LWORD64 ihevce_intra_rdopt_cu_ntu(
225     ihevce_enc_loop_ctxt_t *ps_ctxt,
226     enc_loop_cu_prms_t *ps_cu_prms,
227     void *pv_pred_org,
228     WORD32 pred_strd_org,
229     enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
230     UWORD8 *pu1_luma_mode,
231     cu_analyse_t *ps_cu_analyse,
232     void *pv_curr_src,
233     void *pv_cu_left,
234     void *pv_cu_top,
235     void *pv_cu_top_left,
236     nbr_4x4_t *ps_left_nbr_4x4,
237     nbr_4x4_t *ps_top_nbr_4x4,
238     WORD32 nbr_4x4_left_strd,
239     WORD32 cu_left_stride,
240     WORD32 curr_buf_idx,
241     WORD32 func_proc_mode,
242     WORD32 i4_alpha_stim_multiplier);
243 LWORD64 ihevce_inter_rdopt_cu_ntu(
244     ihevce_enc_loop_ctxt_t *ps_ctxt,
245     enc_loop_cu_prms_t *ps_cu_prms,
246     void *pv_src,
247     WORD32 cu_size,
248     WORD32 cu_pos_x,
249     WORD32 cu_pos_y,
250     WORD32 curr_buf_idx,
251     enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
252     cu_inter_cand_t *ps_inter_cand,
253     cu_analyse_t *ps_cu_analyse,
254     WORD32 i4_alpha_stim_multiplier);
255 
256 LWORD64 ihevce_inter_tu_tree_selector_and_rdopt_cost_computer(
257     ihevce_enc_loop_ctxt_t *ps_ctxt,
258     enc_loop_cu_prms_t *ps_cu_prms,
259     void *pv_src,
260     WORD32 cu_size,
261     WORD32 cu_pos_x,
262     WORD32 cu_pos_y,
263     WORD32 curr_buf_idx,
264     enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
265     cu_inter_cand_t *ps_inter_cand,
266     cu_analyse_t *ps_cu_analyse,
267     WORD32 i4_alpha_stim_multiplier);
268 
269 LWORD64 ihevce_inter_rdopt_cu_mc_mvp(
270     ihevce_enc_loop_ctxt_t *ps_ctxt,
271     cu_inter_cand_t *ps_inter_cand,
272     WORD32 cu_size,
273     WORD32 cu_pos_x,
274     WORD32 cu_pos_y,
275     nbr_4x4_t *ps_left_nbr_4x4,
276     nbr_4x4_t *ps_top_nbr_4x4,
277     nbr_4x4_t *ps_topleft_nbr_4x4,
278     WORD32 nbr_4x4_left_strd,
279     WORD32 curr_buf_idx);
280 void ihevce_intra_chroma_pred_mode_selector(
281     ihevce_enc_loop_ctxt_t *ps_ctxt,
282     enc_loop_chrm_cu_buf_prms_t *ps_chrm_cu_buf_prms,
283     cu_analyse_t *ps_cu_analyse,
284     WORD32 rd_opt_curr_idx,
285     WORD32 tu_mode,
286     WORD32 i4_alpha_stim_multiplier,
287     UWORD8 u1_is_cu_noisy);
288 
289 LWORD64 ihevce_chroma_cu_prcs_rdopt(
290     ihevce_enc_loop_ctxt_t *ps_ctxt,
291     WORD32 rd_opt_curr_idx,
292     WORD32 func_proc_mode,
293     UWORD8 *pu1_chrm_src,
294     WORD32 chrm_src_stride,
295     UWORD8 *pu1_cu_left,
296     UWORD8 *pu1_cu_top,
297     UWORD8 *pu1_cu_top_left,
298     WORD32 cu_left_stride,
299     WORD32 cu_pos_x,
300     WORD32 cu_pos_y,
301     WORD32 *pi4_chrm_tu_bits,
302     WORD32 i4_alpha_stim_multiplier,
303     UWORD8 u1_is_cu_noisy);
304 
305 void ihevce_set_eval_flags(
306     ihevce_enc_loop_ctxt_t *ps_ctxt, enc_loop_cu_final_prms_t *ps_enc_loop_bestprms);
307 
308 void ihevce_final_rdopt_mode_prcs(
309     ihevce_enc_loop_ctxt_t *ps_ctxt, final_mode_process_prms_t *ps_prms);
310 
311 WORD32 ihevce_set_flags_to_regulate_reevaluation(
312     cu_final_recon_flags_t *ps_cu_recon_flags,
313     ihevce_enc_cu_node_ctxt_t *ps_enc_out_ctxt,
314     UWORD8 *pu1_deviant_cu_regions,
315     WORD32 i4_num_deviant_cus,
316     WORD8 i1_qp_past,
317     WORD8 i1_qp_present,
318     UWORD8 u1_is_422);
319 
320 void ihevce_err_compute(
321     UWORD8 *pu1_inp,
322     UWORD8 *pu1_interp_out_buf,
323     WORD32 *pi4_sad_grid,
324     WORD32 *pi4_tu_split_flags,
325     WORD32 inp_stride,
326     WORD32 out_stride,
327     WORD32 blk_size,
328     WORD32 part_mask,
329     WORD32 use_satd_for_err_calc);
330 void ihevce_determine_children_cost_of_32x32_cu(
331     block_merge_input_t *ps_merge_in,
332     WORD32 *pi4_cost_children,
333     WORD32 idx_of_tl_child,
334     WORD32 cu_pos_x,
335     WORD32 cu_pos_y);
336 
337 WORD32 ihevce_determine_children_cost_of_cu_from_me_results(
338     block_merge_input_t *ps_merge_in,
339     cur_ctb_cu_tree_t *ps_cu_tree_root,
340     WORD32 *pi4_ref_bits,
341     WORD32 *pi4_cost_children,
342     WORD32 idx_of_tl_child,
343     CU_SIZE_T e_cu_size_parent);
344 
345 void *ihevce_tu_tree_update(
346     tu_prms_t *ps_tu_prms,
347     WORD32 *pnum_tu_in_cu,
348     WORD32 depth,
349     WORD32 tu_split_flag,
350     WORD32 tu_early_cbf,
351     WORD32 i4_x_off,
352     WORD32 i4_y_off);
353 WORD32 ihevce_shrink_inter_tu_tree(
354     tu_enc_loop_out_t *ps_tu_enc_loop,
355     tu_enc_loop_temp_prms_t *ps_tu_enc_loop_temp_prms,
356     recon_datastore_t *ps_recon_datastore,
357     WORD32 num_tu_in_cu,
358     UWORD8 u1_is_422);
359 UWORD8 ihevce_intra_mode_nxn_hash_updater(
360     UWORD8 *pu1_mode_array, UWORD8 *pu1_hash_table, UWORD8 u1_num_ipe_modes);
361 
362 #if ENABLE_TU_TREE_DETERMINATION_IN_RDOPT
363 WORD32 ihevce_determine_tu_tree_distribution(
364     cu_inter_cand_t *ps_cu_data,
365     me_func_selector_t *ps_func_selector,
366     WORD16 *pi2_scratch_mem,
367     UWORD8 *pu1_inp,
368     WORD32 i4_inp_stride,
369     WORD32 i4_lambda,
370     UWORD8 u1_lambda_q_shift,
371     UWORD8 u1_cu_size,
372     UWORD8 u1_max_tr_depth);
373 #endif
374 
375 void ihevce_populate_nbr_4x4_with_pu_data(
376     nbr_4x4_t *ps_nbr_4x4, pu_t *ps_pu, WORD32 i4_nbr_buf_stride);
377 
378 void ihevce_call_luma_inter_pred_rdopt_pass1(
379     ihevce_enc_loop_ctxt_t *ps_ctxt, cu_inter_cand_t *ps_inter_cand, WORD32 cu_size);
380 
381 LWORD64 ihevce_it_recon_ssd(
382     ihevce_enc_loop_ctxt_t *ps_ctxt,
383     UWORD8 *pu1_src,
384     WORD32 i4_src_strd,
385     UWORD8 *pu1_pred,
386     WORD32 i4_pred_strd,
387     WORD16 *pi2_deq_data,
388     WORD32 i4_deq_data_strd,
389     UWORD8 *pu1_recon,
390     WORD32 i4_recon_stride,
391     UWORD8 *pu1_ecd_data,
392     UWORD8 u1_trans_size,
393     UWORD8 u1_pred_mode,
394     WORD32 i4_cbf,
395     WORD32 i4_zero_col,
396     WORD32 i4_zero_row,
397     CHROMA_PLANE_ID_T e_chroma_plane);
398 
399 WORD32 ihevce_chroma_t_q_iq_ssd_scan_fxn(
400     ihevce_enc_loop_ctxt_t *ps_ctxt,
401     UWORD8 *pu1_pred,
402     WORD32 pred_strd,
403     UWORD8 *pu1_src,
404     WORD32 src_strd,
405     WORD16 *pi2_deq_data,
406     WORD32 deq_data_strd,
407     UWORD8 *pu1_recon,
408     WORD32 i4_recon_stride,
409     UWORD8 *pu1_ecd_data,
410     UWORD8 *pu1_csbf_buf,
411     WORD32 csbf_strd,
412     WORD32 trans_size,
413     WORD32 i4_scan_idx,
414     WORD32 intra_flag,
415     WORD32 *pi4_coeff_off,
416     WORD32 *pi4_tu_bits,
417     WORD32 *pi4_zero_col,
418     WORD32 *pi4_zero_row,
419     UWORD8 *pu1_is_recon_available,
420     WORD32 i4_perform_sbh,
421     WORD32 i4_perform_rdoq,
422     LWORD64 *pi8_cost,
423 #if USE_NOISE_TERM_IN_ZERO_CODING_DECISION_ALGORITHMS
424     WORD32 i4_alpha_stim_multiplier,
425     UWORD8 u1_is_cu_noisy,
426 #endif
427     UWORD8 u1_is_skip,
428     SSD_TYPE_T e_ssd_type,
429     CHROMA_PLANE_ID_T e_chroma_plane);
430 void ihevce_update_pred_qp(ihevce_enc_loop_ctxt_t *ps_ctxt, WORD32 cu_pos_x, WORD32 cu_pos_y);
431 #endif /* _IHEVCE_ENC_LOOP_UTILS_H_ */
432