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