aboutsummaryrefslogtreecommitdiff
path: root/encoder/ihevce_deblk.c
blob: 5e37a71593418f50fc656da27fccb87b5e690b40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
/******************************************************************************
 *
 * 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_deblk.c
*
* @brief
*  Contains definition for the ctb level deblk function
*
* @author
*  ittiam
*
* @List of Functions:
*  ihevce_deblk_populate_qp_map()
*  ihevce_deblk_ctb()
*  ihevce_hbd_deblk_ctb()
*
* @remarks
*  None
*
*******************************************************************************
*/

/*****************************************************************************/
/* 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_debug.h"
#include "ihevc_structs.h"
#include "ihevc_platform_macros.h"
#include "ihevc_deblk.h"
#include "ihevc_deblk_tables.h"
#include "ihevc_common_tables.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_hle_interface.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_common_utils.h"
#include "ihevce_global_tables.h"
#include "ihevce_deblk.h"
#include "ihevce_tile_interface.h"

/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/

/*!
******************************************************************************
* \if Function name : ihevce_deblk_populate_qp_map \endif
*
* \brief
*
*
*****************************************************************************
*/
void ihevce_deblk_populate_qp_map(
    ihevce_enc_loop_ctxt_t *ps_ctxt,
    deblk_ctbrow_prms_t *ps_deblk_ctb_row_params,
    ctb_enc_loop_out_t *ps_ctb_out_dblk,
    WORD32 vert_ctr,
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
    ihevce_tile_params_t *ps_col_tile_params)
{
    ctb_enc_loop_out_t *ps_ctb_out;
    WORD32 ctb_ctr, ctb_start, ctb_end;
    WORD32 tile_qp_offset, tile_qp_size, i4_offset_for_last_cu_qp;
    /* Create the Qp map for the entire current CTB-row for deblocking purpose(only)*/
    /* Do this iff cur pic is referred or recon dump is enabled or psnr calc is on*/
    /*Qp of the last CU of previous CTB row*/
    WORD8 i1_last_cu_qp;
    /*A pointer pointing to the top 4x4 block's Qp for all CTb rows*/
    WORD8 *pi1_qp_top_4x4_ctb_row =
        ps_deblk_ctb_row_params->api1_qp_top_4x4_ctb_row[ps_ctxt->i4_enc_frm_id] +
        (ps_deblk_ctb_row_params->u4_qp_top_4x4_buf_size * ps_ctxt->i4_bitrate_instance_num);

    UWORD32 u4_qp_top_4x4_buf_strd = ps_deblk_ctb_row_params->u4_qp_top_4x4_buf_strd;

    /*The Qp map which has to be populated*/
    UWORD32 u4_qp_buffer_stride = ps_deblk_ctb_row_params->u4_qp_buffer_stride;
    WORD8 *pi1_ctb_tile_qp = ps_deblk_ctb_row_params->pi1_ctb_row_qp;

    /*Temporary pointers to Qp map at CTB level*/
    WORD8 *pi1_ctb_qp_map_tile;

    i4_offset_for_last_cu_qp = ps_ctxt->pi4_offset_for_last_cu_qp[ps_ctxt->i4_tile_col_idx];
    /* total QPs to be copied for current row is : */
    tile_qp_size = i4_offset_for_last_cu_qp + 1;
    /*Pointing to the first CTB of current CTB row*/
    ps_ctb_out = ps_ctb_out_dblk;
    /* Offset req. for the row QP to the tile start */
    tile_qp_offset = ps_col_tile_params->i4_first_ctb_x * (ps_frm_ctb_prms->i4_ctb_size / 4);

    ctb_start = ps_col_tile_params->i4_first_ctb_x;
    ctb_end =
        (ps_col_tile_params->i4_first_ctb_x + ps_col_tile_params->i4_curr_tile_wd_in_ctb_unit);

    if(vert_ctr) /*Not first CTB row of frame*/
    {
        /*copy from top4x4_array data stored by upper CTB-row to qp-map*/
        memcpy(
            pi1_ctb_tile_qp,
            (pi1_qp_top_4x4_ctb_row + (vert_ctr - 1) * u4_qp_top_4x4_buf_strd + tile_qp_offset),
            tile_qp_size);
    }

    /*pu1_ctb_row_qp points to top4x4 row in Qp-map.
    Now pointing pu1_ctb_qp_map to cur 4x4 row*/
    pi1_ctb_qp_map_tile = pi1_ctb_tile_qp + u4_qp_buffer_stride;

    /* This i1_last_cu_qp will be conditionally overwritten later */
    i1_last_cu_qp = ps_ctxt->i4_frame_qp;

    /* -- Loop over all the CTBs in a CTB-row for populating the Qp-map ----- */
    for(ctb_ctr = ctb_start; ctb_ctr < ctb_end; ctb_ctr++)
    {
        WORD32 cu_ctr;
        cu_enc_loop_out_t *ps_curr_cu;

        /* Update i1_last_cu_qp based on CTB's position in tile */
        update_last_coded_cu_qp(
            (ps_deblk_ctb_row_params->pi1_ctb_row_qp + i4_offset_for_last_cu_qp),
            ps_ctxt->i1_entropy_coding_sync_enabled_flag,
            ps_frm_ctb_prms,
            ps_ctxt->i4_frame_qp,
            vert_ctr,
            ctb_ctr,
            &i1_last_cu_qp);

        /* store the pointer of first cu of current ctb */
        ps_curr_cu = ps_ctb_out->ps_enc_cu;

        /* --------- loop over all the CUs in the CTB --------------- */
        for(cu_ctr = 0; cu_ctr < ps_ctb_out->u1_num_cus_in_ctb; cu_ctr++)
        {
            UWORD8 u1_vert_4x4, u1_horz_4x4;  //for_loop counters
            WORD8 *pi1_cu_qp_map;

            WORD8 i1_qp, i1_qp_left, i1_qp_top;

            pi1_cu_qp_map = pi1_ctb_qp_map_tile +
                            (ps_curr_cu->b3_cu_pos_y * 2) * u4_qp_buffer_stride +
                            (ps_curr_cu->b3_cu_pos_x * 2);

            /*If the current CU is coded in skip_mode/zero_CBF then
            for deblocking, Qp of the previously coded CU will be used*/
            if(ps_curr_cu->b1_skip_flag || ps_curr_cu->b1_no_residual_syntax_flag)
            {
                if(0 == ps_curr_cu->b3_cu_pos_x)
                    i1_qp_left = i1_last_cu_qp;
                else
                    i1_qp_left = *(pi1_cu_qp_map - 1);

                if(0 == ps_curr_cu->b3_cu_pos_y)
                    i1_qp_top = i1_last_cu_qp;
                else
                    i1_qp_top = *(pi1_cu_qp_map - u4_qp_buffer_stride);

                i1_qp = (i1_qp_left + i1_qp_top + 1) / 2;

                if(0 == ps_curr_cu->b1_first_cu_in_qg)
                {
                    i1_qp = i1_last_cu_qp;
                }
            }
            else
            {
                i1_qp = ps_curr_cu->i1_cu_qp;
            }

            i1_last_cu_qp = i1_qp;

            /*---- Loop for populating Qp map for the current CU -------*/
            for(u1_vert_4x4 = 0; u1_vert_4x4 < (ps_curr_cu->b4_cu_size * 2); u1_vert_4x4++)
            {
                for(u1_horz_4x4 = 0; u1_horz_4x4 < (ps_curr_cu->b4_cu_size * 2); u1_horz_4x4++)
                {
                    pi1_cu_qp_map[u1_horz_4x4] = i1_qp;
                }
                pi1_cu_qp_map += u4_qp_buffer_stride;
            }
            /*Update Qp-map ptr. Qp map is at 4x4 level but b4_cu_size is at 8x8 level*/
            ps_curr_cu++;
        }
        pi1_ctb_qp_map_tile += (ps_frm_ctb_prms->i4_ctb_size / 4);  //one qp per 4x4 block.
        ps_ctb_out++;

    }  //for(ctb_ctr = 0; ctb_ctr < num_ctbs_horz; ctb_ctr++)

    /*fill into the top4x4_array Qp for the lower CTB-row from bottom part of cur CTB row*/
    memcpy(
        (pi1_qp_top_4x4_ctb_row + vert_ctr * u4_qp_top_4x4_buf_strd + tile_qp_offset),
        (pi1_ctb_tile_qp + (ps_frm_ctb_prms->i4_ctb_size / 4) * u4_qp_buffer_stride),
        tile_qp_size);
}

/**
*******************************************************************************
*
* @brief
*   Deblock CTB level function.
*
* @par Description:
*   For a given CTB, deblocking on both vertical and
*   horizontal edges is done. Both the luma and chroma
*   blocks are processed
*
* @param[in]
*   ps_deblk:   Pointer to the deblock context
*   last_col:   if the CTB is the last CTB of current CTB-row value is 1 else 0
*   ps_deblk_ctb_row_params: deblk ctb row params
*
* @returns
*
* @remarks
*  None
*
*******************************************************************************
*/
void ihevce_deblk_ctb(
    deblk_ctb_params_t *ps_deblk, WORD32 last_col, deblk_ctbrow_prms_t *ps_deblk_ctb_row_params)
{
    WORD32 ctb_size;
    UWORD32 u4_bs;
    WORD32 bs_lz; /*Leading zeros in boundary strength*/
    WORD32 qp_p, qp_q;
    UWORD8 *pu1_src;
    UWORD8 *pu1_src_uv;
    UWORD8 *pu1_curr_src;
    WORD32 col_size;
    WORD32 col, row, i4_edge_count;
    WORD32 num_columns_for_vert_filt;
    WORD32 num_blks_for_vert_filt;
    WORD32 num_rows_for_horz_filt;

    ihevc_deblk_chroma_horz_ft *pf_deblk_chroma_horz;
    ihevc_deblk_chroma_horz_ft *pf_deblk_chroma_vert;

    /* Filter flags are packed along with the qp info.
    6 out of the 8 bits correspond to qp and 1 to filter flag. */
    /* filter_p and filter_q are initialized to 1.
    They are to be extracted along with the qp info. */
    WORD32 filter_p, filter_q;
    WORD8 *pi1_ctb_row_qp_p, *pi1_ctb_row_qp_temp;
    WORD8 *pi1_ctb_row_qp_q;

    func_selector_t *ps_func_slector = ps_deblk->ps_func_selector;

    WORD32 left_luma_edge_filter_flag = ps_deblk->i4_deblock_left_ctb_edge;
    WORD32 top_luma_edge_filter_flag = ps_deblk->i4_deblock_top_ctb_edge;
    WORD32 left_chroma_edge_filter_flag = ps_deblk->i4_deblock_left_ctb_edge;
    WORD32 top_chroma_edge_filter_flag = ps_deblk->i4_deblock_top_ctb_edge;
    UWORD32 *bs_vert = ps_deblk_ctb_row_params->pu4_ctb_row_bs_vert;
    UWORD32 *bs_horz = ps_deblk_ctb_row_params->pu4_ctb_row_bs_horz;
    UWORD32 *bs_vert_uv = bs_vert;
    UWORD32 *bs_horz_uv = bs_horz;
    UWORD32 u4_qp_buffer_stride = ps_deblk_ctb_row_params->u4_qp_buffer_stride;
    UWORD8 u1_is_422 = (ps_deblk->u1_chroma_array_type == 2);

    if(u1_is_422)
    {
        pf_deblk_chroma_horz = ps_func_slector->ihevc_deblk_422chroma_horz_fptr;
        pf_deblk_chroma_vert = ps_func_slector->ihevc_deblk_422chroma_vert_fptr;
    }
    else
    {
        pf_deblk_chroma_horz = ps_func_slector->ihevc_deblk_chroma_horz_fptr;
        pf_deblk_chroma_vert = ps_func_slector->ihevc_deblk_chroma_vert_fptr;
    }

    ctb_size = ps_deblk->i4_ctb_size;

    /* The PCM filter flag and bypass trans flag are always set to 1 in encoder profile */
    /* Can be removed during optimization */
    filter_q = 1;
    filter_p = 1;

    //////////////////////////////////////////////////////////////////////////////
    /* Luma Veritcal Edge */
    pu1_src = ps_deblk->pu1_ctb_y;
    pi1_ctb_row_qp_temp = ps_deblk_ctb_row_params->pi1_ctb_row_qp + u4_qp_buffer_stride;
    num_columns_for_vert_filt = ctb_size / 8;
    num_blks_for_vert_filt = ctb_size / 4;

    for(i4_edge_count = 0; i4_edge_count < num_columns_for_vert_filt; i4_edge_count++)
    {
        u4_bs = *bs_vert;
        /* get the current 4x4 vertical pointer */
        pu1_curr_src = pu1_src;
        pi1_ctb_row_qp_q = pi1_ctb_row_qp_temp + (i4_edge_count << 1);

        /* If the current edge is not the 1st edge of frame or slice */
        if(1 == left_luma_edge_filter_flag)
        {
            for(row = 0; row < num_blks_for_vert_filt;)
            {
                bs_lz = CLZ(u4_bs) >> 1;
                /* If BS = 0, skip the egde filtering */
                if(0 != bs_lz)
                {
                    u4_bs = u4_bs << (bs_lz << 1);
                    pu1_curr_src += ((bs_lz << 2) * ps_deblk->i4_luma_pic_stride);
                    pi1_ctb_row_qp_q += (bs_lz * u4_qp_buffer_stride);
                    row += bs_lz;
                    continue;
                }
                qp_p = *(pi1_ctb_row_qp_q - 1);
                qp_q = *pi1_ctb_row_qp_q;

                ps_func_slector->ihevc_deblk_luma_vert_fptr(
                    pu1_curr_src,
                    ps_deblk->i4_luma_pic_stride,
                    (u4_bs >> 30), /* bits 31 and 30 are extracted */
                    qp_p,
                    qp_q,
                    ps_deblk->i4_beta_offset_div2,
                    ps_deblk->i4_tc_offset_div2,
                    filter_p,
                    filter_q);

                u4_bs = u4_bs << 2;
                pu1_curr_src += (ps_deblk->i4_luma_pic_stride << 2);
                pi1_ctb_row_qp_q += u4_qp_buffer_stride;
                row++;
            }
        }

        /* Increment the boundary strength and src pointer for the next column */
        bs_vert += 1;
        pu1_src += 8;

        /* Enable for the next edges of ctb*/
        left_luma_edge_filter_flag = 1;
    }

    //////////////////////////////////////////////////////////////////////////////
    /* Chroma Veritcal Edge */
    pu1_src_uv = ps_deblk->pu1_ctb_uv;
    pi1_ctb_row_qp_temp = ps_deblk_ctb_row_params->pi1_ctb_row_qp + u4_qp_buffer_stride;

    /* Column spacing is 4 for each chroma component */
    /* and hence 8 when they are interleaved. */
    /* But, only those columns with a x co-ordinate */
    /* that is divisiblee by 8 are filtered */
    /* Hence, denominator is 16 */
    num_columns_for_vert_filt = ctb_size / 16;
    /* blk_size is 4 and chroma_ctb_height is ctb_size/2 */
    num_blks_for_vert_filt = (0 == u1_is_422) ? (ctb_size / 2) / 4 : (ctb_size) / 4;

    for(i4_edge_count = 0; i4_edge_count < num_columns_for_vert_filt; i4_edge_count++)
    {
        /* Every alternate boundary strength value is used for 420 chroma */
        u4_bs = *(bs_vert_uv) & ((0 == u1_is_422) ? 0x88888888 : 0xaaaaaaaa);
        pu1_curr_src = pu1_src_uv;
        pi1_ctb_row_qp_q = pi1_ctb_row_qp_temp + (i4_edge_count << 2);

        /* If the current edge is not the 1st edge of frame or slice */
        if(1 == left_chroma_edge_filter_flag)
        {
            /* Each 'bs' is 2 bits long */
            /* The divby4 in 420 is */
            /* necessitated by the fact that */
            /* chroma ctb_ht is half that of luma */
            WORD32 i4_log2_num_bits_per_bs = ((0 == u1_is_422) + 1);
            /* i4_sub_heightC = 2 for 420 */
            /* i4_sub_heightC = 1 for 422 */
            WORD32 i4_sub_heightC = i4_log2_num_bits_per_bs;

            for(row = 0; row < num_blks_for_vert_filt;)
            {
                bs_lz = CLZ(u4_bs) >> i4_log2_num_bits_per_bs;

                /* If BS = 0, skip the egde filtering */
                if(0 != bs_lz)
                {
                    row += bs_lz;
                    u4_bs = u4_bs << (bs_lz << i4_log2_num_bits_per_bs);
                    /* '<<2' because of blk_size being 4x4 */
                    pu1_curr_src += ((bs_lz << 2) * ps_deblk->i4_chroma_pic_stride);

                    /* In 420, every alternate QP row is skipped, because chroma height */
                    /* In 422, no row is skipped */
                    pi1_ctb_row_qp_q += ((u4_qp_buffer_stride << (i4_sub_heightC - 1)) * bs_lz);

                    continue;
                }

                qp_p = *(pi1_ctb_row_qp_q - i4_sub_heightC);
                qp_q = *pi1_ctb_row_qp_q;

                pf_deblk_chroma_vert(
                    pu1_curr_src,
                    ps_deblk->i4_chroma_pic_stride,
                    qp_p,
                    qp_q,
                    ps_deblk->i4_cb_qp_indx_offset,
                    ps_deblk->i4_cr_qp_indx_offset,
                    ps_deblk->i4_tc_offset_div2,
                    filter_p,
                    filter_q);

                u4_bs = u4_bs << (1 << i4_log2_num_bits_per_bs);
                pu1_curr_src += (ps_deblk->i4_chroma_pic_stride << 2);
                pi1_ctb_row_qp_q += (u4_qp_buffer_stride << (i4_sub_heightC - 1));
                row++;
            }
        }
        /* Increment the boundary strength by 2 and src pointer for the next column */
        /* As the edge filtering happens for alternate column */
        bs_vert_uv += 2;
        pu1_src_uv += 16;
        left_chroma_edge_filter_flag = 1;
    }

    //////////////////////////////////////////////////////////////////////////////

    /* Luma Horizontal Edge */
    pu1_src = ps_deblk->pu1_ctb_y;
    col_size = ctb_size / 4;

    /* If the ctb is the 1st ctb of row,                     */
    /* Decrement the loop count to exclude filtering of last 4 pixels */
    /* else shift the src pointer by 4 pixels to do filtering for shifted ctb */
    if(ps_deblk->i4_deblock_left_ctb_edge == 1)
    {
        pu1_src -= 4;
        /*If the ctb is at the horizonatl end of PIC*/
        /* Increase the column size to filter last 4 pixels */
        col_size += last_col;
    }
    else if(!last_col)
    {
        col_size -= 1;
    }
    {
        UWORD8 *pu1_src_temp = pu1_src;
        //pu1_ctb_row_qp_p and pu1_ctb_row_qp_q point to alternate rows
        pi1_ctb_row_qp_p = ps_deblk_ctb_row_params->pi1_ctb_row_qp;

        num_rows_for_horz_filt = ctb_size / 8;

        for(i4_edge_count = 0; i4_edge_count < num_rows_for_horz_filt; i4_edge_count++)
        {
            WORD32 col_size_temp = col_size;
            pi1_ctb_row_qp_q = pi1_ctb_row_qp_p + u4_qp_buffer_stride;
            pu1_src = pu1_src_temp + (i4_edge_count * 8 * ps_deblk->i4_luma_pic_stride);

            if(1 == top_luma_edge_filter_flag)
            {
                //Deblock the last vertical_4x4_column of previous CTB
                if(ps_deblk->i4_deblock_left_ctb_edge == 1)
                {
                    u4_bs = ps_deblk->au1_prev_bs[i4_edge_count] & 0x3;
                    if(u4_bs != 0)
                    {
                        qp_p = *(pi1_ctb_row_qp_p - 1);
                        qp_q = *(pi1_ctb_row_qp_q - 1);

                        ps_func_slector->ihevc_deblk_luma_horz_fptr(
                            pu1_src,
                            ps_deblk->i4_luma_pic_stride,
                            u4_bs,
                            qp_p,
                            qp_q,
                            ps_deblk->i4_beta_offset_div2,
                            ps_deblk->i4_tc_offset_div2,
                            1,
                            1);
                    }

                    pu1_src += 4;
                    col_size_temp--;
                }
                //Start deblocking current CTB
                u4_bs = *(bs_horz);

                for(col = 0; col < col_size_temp;)
                {
                    bs_lz = CLZ(u4_bs) >> 1;
                    if(0 != bs_lz)
                    {
                        u4_bs = u4_bs << (bs_lz << 1);
                        pu1_src += 4 * bs_lz;
                        col += bs_lz;
                        continue;
                    }
                    qp_p = *(pi1_ctb_row_qp_p + col);
                    qp_q = *(pi1_ctb_row_qp_q + col);

                    ps_func_slector->ihevc_deblk_luma_horz_fptr(
                        pu1_src,
                        ps_deblk->i4_luma_pic_stride,
                        u4_bs >> (sizeof(u4_bs) * 8 - 2),
                        qp_p,
                        qp_q,
                        ps_deblk->i4_beta_offset_div2,
                        ps_deblk->i4_tc_offset_div2,
                        filter_p,
                        filter_q);

                    pu1_src += 4;
                    u4_bs = u4_bs << 2;
                    col++;
                }
                //Store the last vertical_4x4 column of CTB's info for next CTB deblocking
                u4_bs = *bs_horz;
                ps_deblk->au1_prev_bs[i4_edge_count] =
                    (UWORD8)(((u4_bs << ((ctb_size >> 1) - 2))) >> 30);
            }
            bs_horz += 1;
            pi1_ctb_row_qp_p += (u4_qp_buffer_stride << 1);
            top_luma_edge_filter_flag = 1;
        }
    }

    //////////////////////////////////////////////////////////////////////////////
    /* Chroma Horizontal Edge */
    pu1_src_uv = ps_deblk->pu1_ctb_uv;
    col_size = ctb_size / 8;

    /* If the ctb is the 1st ctb of row,                     */
    /* Decrement the loop count to exclude filtering of last 4 pixels */
    /* else shift the src pointer by 8 (uv) pixels to do filtering for shifted ctb */
    if(ps_deblk->i4_deblock_left_ctb_edge == 1)
    {
        pu1_src_uv -= 8;

        /*If the ctb is at the horizonatl end of PIC*/
        /* Increase the column size to filter last 8 (uv) pixels */
        col_size += last_col;
    }
    else if(!last_col)
    {
        col_size--;
    }

    {
        UWORD8 *pu1_src_temp = pu1_src_uv;

        //pu1_ctb_row_qp_p and pu1_ctb_row_qp_q point to alternate rows
        pi1_ctb_row_qp_p = ps_deblk_ctb_row_params->pi1_ctb_row_qp;
        num_rows_for_horz_filt = ctb_size / ((0 == u1_is_422) ? 16 : 8);

        for(i4_edge_count = 0; i4_edge_count < num_rows_for_horz_filt; i4_edge_count++)
        {
            WORD32 col_size_temp = col_size;

            pi1_ctb_row_qp_q = pi1_ctb_row_qp_p + u4_qp_buffer_stride;
            pu1_src_uv = pu1_src_temp + (i4_edge_count * 8 * ps_deblk->i4_chroma_pic_stride);

            if(1 == top_chroma_edge_filter_flag)
            {
                //Deblock the last vertical _4x4_column of previous CTB
                if(ps_deblk->i4_deblock_left_ctb_edge == 1)
                {
                    u4_bs = ps_deblk->au1_prev_bs_uv[i4_edge_count] & 0x2;

                    if(u4_bs == 2)
                    {
                        qp_p = *(pi1_ctb_row_qp_p - 1);
                        qp_q = *(pi1_ctb_row_qp_q - 1);

                        pf_deblk_chroma_horz(
                            pu1_src_uv,
                            ps_deblk->i4_chroma_pic_stride,
                            qp_p,
                            qp_q,
                            ps_deblk->i4_cb_qp_indx_offset,
                            ps_deblk->i4_cr_qp_indx_offset,
                            ps_deblk->i4_tc_offset_div2,
                            1,
                            1);
                    }

                    pu1_src_uv += 8;
                    col_size_temp--;
                }

                //Start deblocking current CTB
                u4_bs = *(bs_horz_uv)&0x88888888;

                for(col = 0; col < col_size_temp;)
                {
                    bs_lz = CLZ(u4_bs) >> 2;

                    if(0 != bs_lz)
                    {
                        u4_bs = u4_bs << (bs_lz << 2);
                        pu1_src_uv += (8 * bs_lz);

                        col += bs_lz;
                        continue;
                    }

                    qp_p = *(pi1_ctb_row_qp_p + (col << 1));
                    qp_q = *(pi1_ctb_row_qp_q + (col << 1));

                    pf_deblk_chroma_horz(
                        pu1_src_uv,
                        ps_deblk->i4_chroma_pic_stride,
                        qp_p,
                        qp_q,
                        ps_deblk->i4_cb_qp_indx_offset,
                        ps_deblk->i4_cr_qp_indx_offset,
                        ps_deblk->i4_tc_offset_div2,
                        filter_p,
                        filter_q);

                    pu1_src_uv += 8;
                    u4_bs = u4_bs << 4;
                    col++;
                }

                //Store the last vertical_4x4 column of CTB's info for next CTB deblocking
                u4_bs = *bs_horz_uv;
                ps_deblk->au1_prev_bs_uv[i4_edge_count] =
                    (UWORD8)(((u4_bs << ((ctb_size >> 1) - 4))) >> 30);
            }

            bs_horz_uv += ((0 == u1_is_422) + 1);
            pi1_ctb_row_qp_p += (u4_qp_buffer_stride << ((0 == u1_is_422) + 1));
            top_chroma_edge_filter_flag = 1;
        }
    }

    return;
}