• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_bs_compute_ctb.c
23 *
24 * @brief
25 *  This file contains functions needed for boundary strength calculation
26 *
27 * @author
28 *  ittiam
29 *
30 * @List of Functions
31 *  ihevce_bs_init_ctb()
32 *  ihevce_bs_compute_ctb()
33 *  ihevce_bs_clear_invalid()
34 *
35 ******************************************************************************
36 */
37 
38 /*****************************************************************************/
39 /* File Includes                                                             */
40 /*****************************************************************************/
41 /* System include files */
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <assert.h>
46 #include <stdarg.h>
47 #include <math.h>
48 
49 /* User include files */
50 #include "ihevc_typedefs.h"
51 #include "itt_video_api.h"
52 #include "ihevce_api.h"
53 
54 #include "rc_cntrl_param.h"
55 #include "rc_frame_info_collector.h"
56 #include "rc_look_ahead_params.h"
57 
58 #include "ihevc_defs.h"
59 #include "ihevc_structs.h"
60 #include "ihevc_platform_macros.h"
61 #include "ihevc_deblk.h"
62 #include "ihevc_itrans_recon.h"
63 #include "ihevc_chroma_itrans_recon.h"
64 #include "ihevc_chroma_intra_pred.h"
65 #include "ihevc_intra_pred.h"
66 #include "ihevc_inter_pred.h"
67 #include "ihevc_mem_fns.h"
68 #include "ihevc_padding.h"
69 #include "ihevc_weighted_pred.h"
70 #include "ihevc_sao.h"
71 #include "ihevc_resi_trans.h"
72 #include "ihevc_quant_iquant_ssd.h"
73 #include "ihevc_cabac_tables.h"
74 
75 #include "ihevce_defs.h"
76 #include "ihevce_lap_enc_structs.h"
77 #include "ihevce_multi_thrd_structs.h"
78 #include "ihevce_me_common_defs.h"
79 #include "ihevce_had_satd.h"
80 #include "ihevce_error_codes.h"
81 #include "ihevce_bitstream.h"
82 #include "ihevce_cabac.h"
83 #include "ihevce_rdoq_macros.h"
84 #include "ihevce_function_selector.h"
85 #include "ihevce_enc_structs.h"
86 #include "ihevce_entropy_structs.h"
87 #include "ihevce_cmn_utils_instr_set_router.h"
88 #include "ihevce_enc_loop_structs.h"
89 #include "ihevce_bs_compute_ctb.h"
90 #include "ihevce_global_tables.h"
91 
92 /*****************************************************************************/
93 /* Global Tables                                                             */
94 /*****************************************************************************/
95 // clang-format off
96 UWORD16 gau2_bs_table[2][8] =
97 {
98     { BS_INTRA_4, BS_INTRA_8, BS_INVALID, BS_INTRA_16, BS_INVALID, BS_INVALID, BS_INVALID, BS_INTRA_32 },
99     { BS_CBF_4, BS_CBF_8, BS_INVALID, BS_CBF_16, BS_INVALID, BS_INVALID, BS_INVALID, BS_CBF_32 }
100 };
101 // clang-format on
102 
103 /*****************************************************************************/
104 /* Function Definitions                                                      */
105 /*****************************************************************************/
106 
107 /**
108 *******************************************************************************
109 *
110 * @brief Initialize the Boundary Strength at a CTB level
111 *
112 * @par   Description
113 * Initialize the Boundary Strength at a CTB level to zeros*
114 *
115 * @param[out] ps_deblk_prms
116 * Pointer to structure s_deblk_prms, which contains
117 * s_deblk_prms.au4_horz_bs : max of 8 such conti. bs to be comp. for 64x64 ctb
118 * s_deblk_prms.au4_vert_bs : max of 8 such conti. bs to be comp. for 64x64 ctb
119 *
120 * @param[in] ctb_size
121 * Size in pels (can be 16, 32 or 64)
122 *
123 * @returns none
124 *
125 * @remarks
126 *
127 *******************************************************************************
128 */
ihevce_bs_init_ctb(deblk_bs_ctb_ctxt_t * ps_deblk_prms,frm_ctb_ctxt_t * ps_frm_ctb_prms,WORD32 ctb_ctr,WORD32 vert_ctr)129 void ihevce_bs_init_ctb(
130     deblk_bs_ctb_ctxt_t *ps_deblk_prms,
131     frm_ctb_ctxt_t *ps_frm_ctb_prms,
132     WORD32 ctb_ctr,
133     WORD32 vert_ctr)
134 {
135     WORD32 ctb_size = ps_frm_ctb_prms->i4_ctb_size;
136 
137     /* Pointer to the array to store the packed BS values in horizontal dir. */
138     UWORD32 *pu4_horz_bs = &ps_deblk_prms->au4_horz_bs[0];
139     /* Pointer to the array to store the packed BS values in vertical dir. */
140     UWORD32 *pu4_vert_bs = &ps_deblk_prms->au4_vert_bs[0];
141 
142     WORD32 i4_top_ctb_tile_id, i4_left_ctb_tile_id;
143     WORD32 *pi4_tile_id_map_temp;
144 
145     pi4_tile_id_map_temp = ps_frm_ctb_prms->pi4_tile_id_map +
146                            vert_ctr * ps_frm_ctb_prms->i4_tile_id_ctb_map_stride + ctb_ctr;
147 
148     i4_left_ctb_tile_id = *(pi4_tile_id_map_temp - 1);
149     i4_top_ctb_tile_id = *(pi4_tile_id_map_temp - ps_frm_ctb_prms->i4_tile_id_ctb_map_stride);
150 
151     ps_deblk_prms->u1_not_first_ctb_row_of_frame = (i4_top_ctb_tile_id != -1);
152     ps_deblk_prms->u1_not_first_ctb_col_of_frame = (i4_left_ctb_tile_id != -1);
153 
154     /* BS should be set to NULL in the following cases
155        Frame boundaries
156        Edges if deblocking is disabled by disable_deblocking_filter_flag
157        Slice boundaries if deblocking across slices is disabled
158        Tile boundaries if deblocking across slices is disabled
159        These are not considered now, except the frame boundary
160     */
161 
162     /* Initializing the bs array to 0. array size = (ctb_size/8 + 1)*4 bytes */
163     memset(pu4_horz_bs, 0, ((ctb_size >> 3) + 1) * sizeof(UWORD32));
164     memset(pu4_vert_bs, 0, ((ctb_size >> 3) + 1) * sizeof(UWORD32));
165 }
166 
167 /**
168 *******************************************************************************
169 *
170 * @brief Calculate the Boundary Strength at CU level
171 *
172 * @par   Description
173 * Calculate the Boundary Strength at CU level
174 *
175 * @param[in] ps_cu_final
176 * Pointer to the final CU structure, of which we use the following values
177 * u2_num_tus_in_cu : Total TUs in this CU
178 * ps_enc_tu : Pointer to first tu of this cu. Each TU need to be
179 * populated in TU order.
180 * u4_pred_mode_flag : The prediction mode flag for the CU
181 * cu_size : CU size in terms of min CU (8x8) units
182 * cu_pos_x : X Position of CU in current ctb
183 * cu_pos_y : Y Position of CU in current ctb
184 * u4_part_mode : Partition information for CU. For inter 0 : @sa PART_SIZE_E
185 * ps_pu : Pointer to first pu of this cu
186 *
187 * @param[in] ps_top_nbr_4x4
188 * Pointer to top 4x4 CU nbr structure
189 *
190 * @param[in] ps_left_nbr_4x4
191 * Pointer to left 4x4 CU nbr structure
192 *
193 * @param[in] ps_curr_nbr_4x4
194 * Pointer to current 4x4 ctb structure
195 *
196 * @param[in] nbr_4x4_left_strd
197 * Left nbr buffer stride in terms of 4x4 units
198 *
199 * @param[in] num_4x4_in_ctb
200 * Current buffer stride in terms of 4x4 units
201 *
202 * @param[out] ps_deblk_prms
203 * Pointer to structure s_deblk_prms, which contains
204 * s_deblk_prms.au4_horz_bs : max of 8 such conti. bs to be comp. for 64x64 ctb
205 * s_deblk_prms.au4_vert_bs : max of 8 such conti. bs to be comp. for 64x64 ctb
206 *
207 * @returns none
208 *
209 * @remarks
210 * 1 : Setting all 4 edges for a TU or PU block. Which is inefficient in
211 *   a) may set the BS twice b) set the frame/slice boundaries
212 * 2 : always update BS using bit-wise OR, which may set BS to 3 also.
213 *   ( Deblocking should take care of it as 2 itself )
214 *
215 *******************************************************************************
216 */
ihevce_bs_compute_cu(cu_enc_loop_out_t * ps_cu_final,nbr_4x4_t * ps_top_nbr_4x4,nbr_4x4_t * ps_left_nbr_4x4,nbr_4x4_t * ps_curr_nbr_4x4,WORD32 nbr_4x4_left_strd,WORD32 num_4x4_in_ctb,deblk_bs_ctb_ctxt_t * ps_deblk_prms)217 void ihevce_bs_compute_cu(
218     cu_enc_loop_out_t *ps_cu_final,
219     nbr_4x4_t *ps_top_nbr_4x4,
220     nbr_4x4_t *ps_left_nbr_4x4,
221     nbr_4x4_t *ps_curr_nbr_4x4,
222     WORD32 nbr_4x4_left_strd,
223     WORD32 num_4x4_in_ctb,
224     deblk_bs_ctb_ctxt_t *ps_deblk_prms)
225 {
226     WORD32 i;
227     WORD32 j;
228     /* copy required arguments from pointer to CU structure */
229     /* Total TUs in this CU */
230     UWORD16 u2_num_tus_in_cu = ps_cu_final->u2_num_tus_in_cu;
231     /* Pointer to first tu of this cu */
232     tu_enc_loop_out_t *ps_enc_tu = ps_cu_final->ps_enc_tu;
233     /* The prediction mode flag for the CU */
234     UWORD32 u4_pred_mode_flag = ps_cu_final->b1_pred_mode_flag;
235     /* X Position of CU in current ctb in (8x8) units */
236     WORD32 cu_pos_x = ps_cu_final->b3_cu_pos_x;
237     /* Y Position of CU in current ctb in (8x8) units */
238     WORD32 cu_pos_y = ps_cu_final->b3_cu_pos_y;
239 
240     /* Indicates partition information for CU */
241     UWORD32 u4_part_mode = ps_cu_final->b3_part_mode;
242 
243     /* Pointer to first pu of this cu */
244     pu_t *ps_pu = ps_cu_final->ps_pu;
245 
246     /* Number of pus in current cu */
247     WORD32 num_pus_in_cu;
248     /* Pointer to the array to store the packed BS values in horizontal dir. */
249     UWORD32 *pu4_horz_bs = &ps_deblk_prms->au4_horz_bs[0];
250     /* Pointer to the array to store the packed BS values in vertical dir. */
251     UWORD32 *pu4_vert_bs = &ps_deblk_prms->au4_vert_bs[0];
252 
253     (void)ps_curr_nbr_4x4;
254     (void)num_4x4_in_ctb;
255 
256     /* CTB boundary case setting the BS for intra and cbf non zero case for CU top edge */
257     if((ps_deblk_prms->u1_not_first_ctb_row_of_frame) && (0 == ps_cu_final->b3_cu_pos_y))
258     {
259         nbr_4x4_t *ps_nbr_4x4;
260         UWORD32 u4_temp_bs = *pu4_horz_bs;
261         WORD32 horz_bit_offset;
262         WORD32 ctr;
263 
264         /* every 4x4 takes 2 bits in the register this is taken care in the loop */
265         /* deriving 4x4 position */
266         horz_bit_offset = (ps_cu_final->b3_cu_pos_x << 3) >> 2;
267 
268         /* scanning through each 4x4 csb along horizontal direction */
269         for(ctr = 0; ctr < ((ps_cu_final->b4_cu_size << 3) >> 2); ctr++)
270         {
271             ps_nbr_4x4 = ps_top_nbr_4x4 + ctr;
272             if(ps_nbr_4x4->b1_intra_flag)
273             {
274                 /* To store in BigEnd. format. BS[0]|BS[1]| .. |BS[15] */
275                 u4_temp_bs = (u4_temp_bs | (2U << (30 - 2 * (ctr + horz_bit_offset))));
276             }
277             else if(ps_nbr_4x4->b1_y_cbf)
278             {
279                 /* To store in BigEnd. format. BS[0]|BS[1]| .. |BS[15] */
280                 u4_temp_bs = (u4_temp_bs | (1 << (30 - 2 * (ctr + horz_bit_offset))));
281             }
282         }
283 
284         /* storing the BS computed for first row based on top ctb CUs  */
285         *(pu4_horz_bs) = u4_temp_bs;
286     }
287 
288     /* CTB boundary case setting the BS for intra and cbf non zero case for CU left edge */
289     if((ps_deblk_prms->u1_not_first_ctb_col_of_frame) && (0 == ps_cu_final->b3_cu_pos_x))
290     {
291         nbr_4x4_t *ps_nbr_4x4;
292         UWORD32 u4_temp_bs = *pu4_vert_bs;
293         WORD32 vert_bit_offset;
294         WORD32 ctr;
295 
296         /* every 4x4 takes 2 bits in the register this is taken care in the loop */
297         /* deriving 4x4 position */
298         vert_bit_offset = (ps_cu_final->b3_cu_pos_y << 3) >> 2;
299 
300         /* scanning through each 4x4 csb along vertical direction */
301         for(ctr = 0; ctr < ((ps_cu_final->b4_cu_size << 3) >> 2); ctr++)
302         {
303             ps_nbr_4x4 = ps_left_nbr_4x4 + ctr * nbr_4x4_left_strd;
304             if(ps_nbr_4x4->b1_intra_flag)
305             {
306                 /* To store in BigEnd. format. BS[0]|BS[1]| .. |BS[15] */
307                 u4_temp_bs = (u4_temp_bs | (2U << (30 - 2 * (ctr + vert_bit_offset))));
308             }
309             else if(ps_nbr_4x4->b1_y_cbf)
310             {
311                 /* To store in BigEnd. format. BS[0]|BS[1]| .. |BS[15] */
312                 u4_temp_bs = (u4_temp_bs | (1 << (30 - 2 * (ctr + vert_bit_offset))));
313             }
314         }
315 
316         /* storing the BS computed for first col based on left ctb Cus */
317         *(pu4_vert_bs) = u4_temp_bs;
318     }
319 
320     /* Passes through each TU inside the CU */
321     for(i = 0; i < u2_num_tus_in_cu; i++)
322     {
323         UWORD32 u4_tu_pos_x, u4_tu_pos_y;
324         UWORD32 u4_tu_size;
325         UWORD32 *pu4_tu_top_edge;
326         UWORD32 *pu4_tu_bottom_edge;
327         UWORD32 *pu4_tu_left_edge;
328         UWORD32 *pu4_tu_right_edge;
329         UWORD32 u4_bs_value;
330         WORD32 set_bs_flag = 0;
331         WORD32 tbl_idx = 1;
332 
333         /* TU_size calculation */
334         u4_tu_size = 1 << ((ps_enc_tu->s_tu.b3_size) + 2);
335 
336         /* TU X position in terms of min TU (4x4) units wrt ctb */
337         u4_tu_pos_x = ps_enc_tu->s_tu.b4_pos_x;
338         /* TU Y position in terms of min TU (4x4) units wrt ctb */
339         u4_tu_pos_y = ps_enc_tu->s_tu.b4_pos_y;
340 
341         /* pointers to the edges of current TU */
342         pu4_tu_top_edge = pu4_horz_bs + (u4_tu_pos_y >> 1);
343         pu4_tu_bottom_edge = pu4_horz_bs + ((u4_tu_pos_y + 1) >> 1) + (u4_tu_size >> 3);
344         pu4_tu_left_edge = pu4_vert_bs + (u4_tu_pos_x >> 1);
345         pu4_tu_right_edge = pu4_vert_bs + ((u4_tu_pos_x + 1) >> 1) + (u4_tu_size >> 3);
346 
347         /* chooose the table index based on pred_mode */
348         if(PRED_MODE_INTRA == u4_pred_mode_flag)
349         {
350             tbl_idx = 0;
351         }
352 
353         /* get the BS value from table if required */
354         if((ps_enc_tu->s_tu.b1_y_cbf) || (PRED_MODE_INTRA == u4_pred_mode_flag))
355         {
356             set_bs_flag = 1;
357             u4_bs_value = gau2_bs_table[tbl_idx][(u4_tu_size >> 2) - 1];
358         }
359 
360         if(1 == set_bs_flag)
361         {
362             /* Store the BS value */
363             if(4 == u4_tu_size)
364             {
365                 if(0 == (u4_tu_pos_y & 1))
366                 {
367                     /* Only top TU edge came on a 8 pixel bounadey */
368                     SET_VALUE_BIG((pu4_tu_top_edge), u4_bs_value, u4_tu_pos_x, u4_tu_size);
369                 }
370                 else
371                 {
372                     /* Only bottom TU edge came on a 8 pixel bounadey */
373                     SET_VALUE_BIG((pu4_tu_bottom_edge), u4_bs_value, u4_tu_pos_x, u4_tu_size);
374                 }
375                 if(0 == (u4_tu_pos_x & 1))
376                 {
377                     /* Only left TU edge came on a 8 pixel bounadey */
378                     SET_VALUE_BIG((pu4_tu_left_edge), u4_bs_value, u4_tu_pos_y, u4_tu_size);
379                 }
380                 else
381                 {
382                     /* Only right TU edge came on a 8 pixel bounadey */
383                     SET_VALUE_BIG((pu4_tu_right_edge), u4_bs_value, u4_tu_pos_y, u4_tu_size);
384                 }
385             }
386             /* set all edges for other TU sizes */
387             else
388             {
389                 /* setting top TU edge */
390                 SET_VALUE_BIG((pu4_tu_top_edge), u4_bs_value, u4_tu_pos_x, u4_tu_size);
391                 /* setting bottom TU edge */
392                 SET_VALUE_BIG((pu4_tu_bottom_edge), u4_bs_value, u4_tu_pos_x, u4_tu_size);
393                 /* setting left TU edge */
394                 SET_VALUE_BIG((pu4_tu_left_edge), u4_bs_value, u4_tu_pos_y, u4_tu_size);
395                 /* setting right TU edge */
396                 SET_VALUE_BIG((pu4_tu_right_edge), u4_bs_value, u4_tu_pos_y, u4_tu_size);
397             }
398         }
399 
400         /* point to next TU inside CU in TU order */
401         ps_enc_tu++;
402     }
403 
404     if(PRED_MODE_INTRA == u4_pred_mode_flag)
405     {
406         /* no mv based BS computation in INTRA case */
407         return;
408     }
409     /* BS update due to PU mv.s */
410     if(u4_part_mode == SIZE_2Nx2N) /* symmetric motion partition,  2Nx2N */
411     {
412         num_pus_in_cu = 1;
413     }
414     else if(u4_part_mode == SIZE_NxN) /* symmetric motion partition,  NxN */
415     {
416         num_pus_in_cu = 4;
417     }
418     else /* other sym. or asym. partiotions */
419     {
420         num_pus_in_cu = 2;
421     }
422 
423     /* Go through each PU inside CU in PU order and set the top & bottom */
424     /* PU edge BS accordingly */
425     for(i = 0; i < num_pus_in_cu; i++)
426     {
427         WORD32 k;
428         /* X Position of PU in terms of min PU (4x4) units in current ctb */
429         WORD32 pu_pos_x = ps_pu->b4_pos_x;
430         /* Y Position of PU in terms of min PU (4x4) units in current ctb */
431         WORD32 pu_pos_y = ps_pu->b4_pos_y;
432         /*  PU width in 4 pixel unit */
433         WORD32 pu_wd = (ps_pu->b4_wd) + 1;
434         /*  PU height in 4 pixel unit */
435         WORD32 pu_ht = (ps_pu->b4_ht) + 1;
436         /* Pred L0 flag */
437         WORD32 cur_pred_l0_flag;
438         /* pointer to current PU */
439         nbr_4x4_t *ps_curr_nbr_4x4_pu;
440 
441         /* go through each 4x4 block along the PU edges and do BS calculation */
442         /* can optimize further with proper checks according to PU size */
443         /* but in that case also @CTB boundary, we should go by 4x4 nbr.s only*/
444 
445         /* load cur. PU parameters */
446         WORD8 i1_cur_l0_ref_pic_buf_id, i1_cur_l1_ref_pic_buf_id;
447         WORD32 cur_mv_no;
448         WORD16 i2_mv_x0, i2_mv_y0, i2_mv_x1, i2_mv_y1;
449 
450         ps_curr_nbr_4x4_pu = ps_curr_nbr_4x4 + (pu_pos_x - (cu_pos_x << 1)) +
451                              (pu_pos_y - (cu_pos_y << 1)) * num_4x4_in_ctb;
452 
453         cur_pred_l0_flag = ps_curr_nbr_4x4_pu->b1_pred_l0_flag;
454 
455         /* L0 & L1 unique ref. pic. id for cur. PU, (stored in upper 4 bits) */
456         i1_cur_l0_ref_pic_buf_id = (ps_curr_nbr_4x4_pu->mv.i1_l0_ref_pic_buf_id);
457         i1_cur_l1_ref_pic_buf_id = (ps_curr_nbr_4x4_pu->mv.i1_l1_ref_pic_buf_id);
458 
459         /* Number of motion vectors used for cur. PU */
460         cur_mv_no = cur_pred_l0_flag + ps_curr_nbr_4x4_pu->b1_pred_l1_flag;
461 
462         /* x and y mv for L0 and L1, for cur. PU */
463         i2_mv_x0 = ps_curr_nbr_4x4_pu->mv.s_l0_mv.i2_mvx;
464         i2_mv_y0 = ps_curr_nbr_4x4_pu->mv.s_l0_mv.i2_mvy;
465         i2_mv_x1 = ps_curr_nbr_4x4_pu->mv.s_l1_mv.i2_mvx;
466         i2_mv_y1 = ps_curr_nbr_4x4_pu->mv.s_l1_mv.i2_mvy;
467 
468         /* two cases for updating TOP and LEFT edges respectively */
469         /* k = 0 : TOP edge update, k = 1 : LEFT edge update */
470         for(k = 0; k < 2; k++)
471         {
472             WORD32 pu_pos_pointer_calc, pu_pos_bit_calc;
473             UWORD32 *pu4_pu_cur_edge;
474             WORD32 pu_dim, nbr_inc;
475             nbr_4x4_t *ps_nbr_4x4;
476 
477             /* TOP edge case */
478             if(0 == k)
479             {
480                 pu_pos_pointer_calc = pu_pos_y;
481                 pu_pos_bit_calc = pu_pos_x;
482                 pu4_pu_cur_edge = pu4_horz_bs + (pu_pos_y >> 1);
483                 pu_dim = pu_wd;
484 
485                 /* top neighbours are accessed linearly */
486                 nbr_inc = 1;
487 
488                 /* If the current 4x4 csb is in the first row of CTB */
489                 if(0 == pu_pos_pointer_calc)
490                 { /* then need to check if top CTB is physically available */
491                     /* (slice bound. are considered as availabale) */
492                     if(ps_deblk_prms->u1_not_first_ctb_row_of_frame)
493                     {
494                         ps_nbr_4x4 = ps_top_nbr_4x4 + (nbr_inc * (pu_pos_x - (cu_pos_x << 1)));
495                     }
496                     else
497                     {
498                         /* This is done for avoiding uninitialized memory access at pic. boundaries*/
499                         ps_nbr_4x4 = ps_curr_nbr_4x4_pu;
500                     }
501                 }
502                 /* within ctb, so top neighbour is available */
503                 else
504                 {
505                     ps_nbr_4x4 = ps_curr_nbr_4x4_pu - num_4x4_in_ctb;
506                 }
507             }
508             /* LEFT edge case */
509             else
510             {
511                 pu_pos_pointer_calc = pu_pos_x;
512                 pu_pos_bit_calc = pu_pos_y;
513                 pu4_pu_cur_edge = pu4_vert_bs + (pu_pos_x >> 1);
514                 pu_dim = pu_ht;
515 
516                 /* left neighbours are accessed using stride */
517                 nbr_inc = nbr_4x4_left_strd;
518 
519                 /* If the current 4x4 csb is in the first col of CTB */
520                 if(0 == pu_pos_pointer_calc)
521                 { /* then need to check if left CTB is available */
522                     if(ps_deblk_prms->u1_not_first_ctb_col_of_frame)
523                     {
524                         ps_nbr_4x4 = ps_left_nbr_4x4 + (nbr_inc * (pu_pos_y - (cu_pos_y << 1)));
525                     }
526                     else
527                     {
528                         /* This is done for avoiding uninitialized memory access at pic. boundaries*/
529                         ps_nbr_4x4 = ps_curr_nbr_4x4_pu;
530                         nbr_inc = num_4x4_in_ctb;
531                     }
532                 }
533                 /* within ctb, so left neighbour is available */
534                 else
535                 {
536                     ps_nbr_4x4 = ps_curr_nbr_4x4_pu - 1;
537                     nbr_inc = num_4x4_in_ctb;
538                 }
539             }
540 
541             /* Only if the current edge falls on 8 pixel grid and ... */
542             if(0 == (pu_pos_pointer_calc & 1))
543             {
544                 /* go through the edge in 4x4 unit. Can be optimized */
545                 /* In that case special case for CTB boundary */
546                 for(j = 0; j < pu_dim; j++)
547                 {
548                     //nbr_4x4_t *ps_temp_nbr_4x4;
549 
550                     /* ... and if the BS not set yet */
551                     if(0 == EXTRACT_VALUE_BIG(pu4_pu_cur_edge, (pu_pos_bit_calc + j)))
552                     {
553                         WORD8 i1_nbr_l0_ref_pic_buf_id, i1_nbr_l1_ref_pic_buf_id;
554                         WORD32 nbr_mv_no;
555                         WORD32 bs_flag = 0;
556                         WORD32 nbr_pred_l0_flag = ps_nbr_4x4->b1_pred_l0_flag;
557 
558                         /* L0 & L1 unique ref. pic. id for nbr. csb, in upper 4 bits */
559                         i1_nbr_l0_ref_pic_buf_id = (ps_nbr_4x4->mv.i1_l0_ref_pic_buf_id);
560                         i1_nbr_l1_ref_pic_buf_id = (ps_nbr_4x4->mv.i1_l1_ref_pic_buf_id);
561 
562                         /* Number of motion vectors used */
563                         nbr_mv_no = nbr_pred_l0_flag + ps_nbr_4x4->b1_pred_l1_flag;
564 
565                         /* If diff. no. of motion vectors used */
566                         if(cur_mv_no != nbr_mv_no)
567                         {
568                             bs_flag = 1;
569                         }
570                         /* If One motion vector is used */
571                         else if(1 == cur_mv_no)
572                         {
573                             WORD16 i2_mv_x, i2_mv_y;
574 
575                             if(cur_pred_l0_flag)
576                             { /* L0 used for cur. */
577                                 if(nbr_pred_l0_flag)
578                                 { /* L0 used for nbr. */
579                                     if(i1_cur_l0_ref_pic_buf_id != i1_nbr_l0_ref_pic_buf_id)
580                                     {
581                                         /* reference pictures used are different */
582                                         bs_flag = 1;
583                                     }
584                                 }
585                                 else
586                                 { /* L1 used for nbr. */
587                                     if(i1_cur_l0_ref_pic_buf_id != i1_nbr_l1_ref_pic_buf_id)
588                                     {
589                                         /* reference pictures used are different */
590                                         bs_flag = 1;
591                                     }
592                                 }
593                                 if(!bs_flag)
594                                 {
595                                     i2_mv_x = i2_mv_x0;
596                                     i2_mv_y = i2_mv_y0;
597                                 }
598                             }
599                             else
600                             { /* L1 used for cur. */
601                                 if(nbr_pred_l0_flag)
602                                 { /* L0 used for nbr. */
603                                     if(i1_cur_l1_ref_pic_buf_id != i1_nbr_l0_ref_pic_buf_id)
604                                     {
605                                         /* reference pictures used are different */
606                                         bs_flag = 1;
607                                     }
608                                 }
609                                 else
610                                 { /* L1 used for nbr. */
611                                     if(i1_cur_l1_ref_pic_buf_id != i1_nbr_l1_ref_pic_buf_id)
612                                     {
613                                         /* reference pictures used are different */
614                                         bs_flag = 1;
615                                     }
616                                 }
617                                 if(!bs_flag)
618                                 {
619                                     i2_mv_x = i2_mv_x1;
620                                     i2_mv_y = i2_mv_y1;
621                                 }
622                             }
623 
624                             if(!bs_flag)
625                             {
626                                 WORD16 i2_nbr_mv_x, i2_nbr_mv_y;
627 
628                                 if(nbr_pred_l0_flag)
629                                 {
630                                     i2_nbr_mv_x = ps_nbr_4x4->mv.s_l0_mv.i2_mvx;
631                                     i2_nbr_mv_y = ps_nbr_4x4->mv.s_l0_mv.i2_mvy;
632                                 }
633                                 else
634                                 {
635                                     i2_nbr_mv_x = ps_nbr_4x4->mv.s_l1_mv.i2_mvx;
636                                     i2_nbr_mv_y = ps_nbr_4x4->mv.s_l1_mv.i2_mvy;
637                                 }
638                                 // clang-format off
639                                 bs_flag =
640                                     (abs(i2_mv_x - i2_nbr_mv_x) < 4) &&
641                                     (abs(i2_mv_y - i2_nbr_mv_y) < 4)
642                                         ? 0
643                                         : 1;
644                                 // clang-format on
645                             }
646                         }
647                         /* If two motion vectors are used */
648                         else if(2 == cur_mv_no)
649                         {
650                             /* check whether same reference pictures used */
651                             if((i1_cur_l0_ref_pic_buf_id == i1_nbr_l0_ref_pic_buf_id &&
652                                 i1_cur_l1_ref_pic_buf_id == i1_nbr_l1_ref_pic_buf_id) ||
653                                (i1_cur_l0_ref_pic_buf_id == i1_nbr_l1_ref_pic_buf_id &&
654                                 i1_cur_l1_ref_pic_buf_id == i1_nbr_l0_ref_pic_buf_id))
655                             {
656                                 WORD16 i2_nbr_mv_x0, i2_nbr_mv_y0, i2_nbr_mv_x1, i2_nbr_mv_y1;
657 
658                                 /* x and y mv for L0 and L1, for nbr. csb*/
659                                 i2_nbr_mv_x0 = ps_nbr_4x4->mv.s_l0_mv.i2_mvx;
660                                 i2_nbr_mv_y0 = ps_nbr_4x4->mv.s_l0_mv.i2_mvy;
661                                 i2_nbr_mv_x1 = ps_nbr_4x4->mv.s_l1_mv.i2_mvx;
662                                 i2_nbr_mv_y1 = ps_nbr_4x4->mv.s_l1_mv.i2_mvy;
663 
664                                 /* Different L0 and L1 */
665                                 if(i1_cur_l0_ref_pic_buf_id != i1_cur_l1_ref_pic_buf_id)
666                                 {
667                                     if(i1_cur_l0_ref_pic_buf_id == i1_nbr_l0_ref_pic_buf_id)
668                                     {
669                                         // clang-format off
670                                         bs_flag =
671                                             (abs(i2_mv_x0 - i2_nbr_mv_x0) < 4) &&
672                                             (abs(i2_mv_y0 - i2_nbr_mv_y0) < 4) &&
673                                             (abs(i2_mv_x1 - i2_nbr_mv_x1) < 4) &&
674                                             (abs(i2_mv_y1 - i2_nbr_mv_y1) < 4)
675                                                 ? 0
676                                                 : 1;
677                                         // clang-format on
678                                     }
679                                     else
680                                     {
681                                         // clang-format off
682                                         bs_flag =
683                                             (abs(i2_mv_x0 - i2_nbr_mv_x1) < 4) &&
684                                             (abs(i2_mv_y0 - i2_nbr_mv_y1) < 4) &&
685                                             (abs(i2_mv_x1 - i2_nbr_mv_x0) < 4) &&
686                                             (abs(i2_mv_y1 - i2_nbr_mv_y0) < 4)
687                                                 ? 0
688                                                 : 1;
689                                         // clang-format on
690                                     }
691                                 }
692                                 else /* Same L0 and L1 */
693                                 {
694                                     // clang-format off
695                                     bs_flag =
696                                         ((abs(i2_mv_x0 - i2_nbr_mv_x0) >= 4) ||
697                                          (abs(i2_mv_y0 - i2_nbr_mv_y0) >= 4) ||
698                                          (abs(i2_mv_x1 - i2_nbr_mv_x1) >= 4) ||
699                                          (abs(i2_mv_y1 - i2_nbr_mv_y1) >= 4)) &&
700                                         ((abs(i2_mv_x0 - i2_nbr_mv_x1) >= 4) ||
701                                          (abs(i2_mv_y0 - i2_nbr_mv_y1) >= 4) ||
702                                          (abs(i2_mv_x1 - i2_nbr_mv_x0) >= 4) ||
703                                          (abs(i2_mv_y1 - i2_nbr_mv_y0) >= 4))
704                                             ? 1
705                                             : 0;
706                                     // clang-format on
707                                 }
708                             }
709                             else /* If the reference pictures used are different */
710                             {
711                                 bs_flag = 1;
712                             }
713                         }
714 
715                         if(bs_flag)
716                         { /*Storing if BS set due to PU mvs */
717                             /*Storing in BigEnd. format. BS[0]|BS[1]| .. |BS[15] & edge_size is 4*/
718                             SET_VALUE_BIG((pu4_pu_cur_edge), BS_CBF_4, (pu_pos_bit_calc + j), 4);
719                         }
720                     }
721 
722                     /* increment the neighbour */
723                     ps_nbr_4x4 += nbr_inc;
724                 }
725             }
726         }
727         /* point to the next PU */
728         ps_pu++;
729     }
730 }
731 
732 /**
733 *******************************************************************************
734 *
735 * @brief Clear the invalid Boundary Strength which may be set by
736 * ihevce_bs_compute_cu
737 *
738 * @par   Description
739 * Clear the invalid Boundary Strength which may be set by ihevce_bs_compute_cu
740 * (as it does all 4 edges in a shot for some cases)
741 *
742 * @param[out] ps_deblk_prms
743 * Pointer to structure s_deblk_prms, which contains
744 * s_deblk_prms.au4_horz_bs : max of 8 such conti. bs to be comp. for 64x64 ctb
745 * s_deblk_prms.au4_vert_bs : max of 8 such conti. bs to be comp. for 64x64 ctb
746 *
747 * @param[in] last_ctb_row_flag
748 * Flag for checking whether the current CTB is in last ctb_row
749 *
750 * @param[in] last_ctb_in_row_flag
751 * Flag for checking whether the current CTB is the last in current row
752 *
753 * @param[in] last_hz_ctb_wd
754 *  Valid Width (pixels) in the last CTB in every row (padding cases)
755 *
756 * @param[in] last_vt_ctb_ht
757 *  Valid Height (pixels) in the last CTB row (padding cases)
758 *
759 * @returns none
760 *
761 * @remarks
762 *
763 *******************************************************************************
764 */
ihevce_bs_clear_invalid(deblk_bs_ctb_ctxt_t * ps_deblk_prms,WORD32 last_ctb_row_flag,WORD32 last_ctb_in_row_flag,WORD32 last_hz_ctb_wd,WORD32 last_vt_ctb_ht)765 void ihevce_bs_clear_invalid(
766     deblk_bs_ctb_ctxt_t *ps_deblk_prms,
767     WORD32 last_ctb_row_flag,
768     WORD32 last_ctb_in_row_flag,
769     WORD32 last_hz_ctb_wd,
770     WORD32 last_vt_ctb_ht)
771 {
772     /* Rightmost CTB. Right padding may be there */
773     /* clear the last vert BS which might have set by ihevce_bs_compute_cu */
774     if(1 == last_ctb_in_row_flag)
775     {
776         ps_deblk_prms->au4_vert_bs[last_hz_ctb_wd >> 3] = 0;
777     }
778 
779     /* Bottommost CTB. Bottom padding may be there */
780     /* clear the last horz BS which might have set by ihevce_bs_compute_cu */
781     if(1 == last_ctb_row_flag)
782     {
783         ps_deblk_prms->au4_horz_bs[last_vt_ctb_ht >> 3] = 0;
784     }
785 }
786