Coverage Report

Created: 2025-08-28 06:38

/src/libhevc/decoder/ihevcd_deblk.c
Line
Count
Source
1
/******************************************************************************
2
*
3
* Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
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
/**
19
*******************************************************************************
20
* @file
21
*  ihevc_deblk.c
22
*
23
* @brief
24
*  Contains definition for the ctb level deblk function
25
*
26
* @author
27
*  Srinivas T
28
*
29
* @par List of Functions:
30
*   - ihevc_deblk()
31
*
32
* @remarks
33
*  None
34
*
35
*******************************************************************************
36
*/
37
38
#include <stdio.h>
39
#include <stddef.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <assert.h>
43
44
#include "ihevc_typedefs.h"
45
#include "iv.h"
46
#include "ivd.h"
47
#include "ihevcd_cxa.h"
48
#include "ithread.h"
49
50
#include "ihevc_defs.h"
51
#include "ihevc_debug.h"
52
#include "ihevc_defs.h"
53
#include "ihevc_structs.h"
54
#include "ihevc_macros.h"
55
#include "ihevc_platform_macros.h"
56
#include "ihevc_cabac_tables.h"
57
58
#include "ihevc_error.h"
59
#include "ihevc_common_tables.h"
60
61
#include "ihevcd_trace.h"
62
#include "ihevcd_defs.h"
63
#include "ihevcd_function_selector.h"
64
#include "ihevcd_structs.h"
65
#include "ihevcd_error.h"
66
#include "ihevcd_nal.h"
67
#include "ihevcd_bitstream.h"
68
#include "ihevcd_job_queue.h"
69
#include "ihevcd_utils.h"
70
#include "ihevcd_debug.h"
71
72
#include "ihevc_deblk.h"
73
#include "ihevc_deblk_tables.h"
74
#include "ihevcd_profile.h"
75
/**
76
*******************************************************************************
77
*
78
* @brief
79
*     Deblock CTB level function.
80
*
81
* @par Description:
82
*     For a given CTB, deblocking on both vertical and
83
*     horizontal edges is done. Both the luma and chroma
84
*     blocks are processed
85
*
86
* @param[in] ps_deblk
87
*  Pointer to the deblock context
88
*
89
* @returns
90
*
91
* @remarks
92
*  None
93
*
94
*******************************************************************************
95
*/
96
97
void ihevcd_deblk_ctb(deblk_ctxt_t *ps_deblk,
98
                      WORD32 i4_is_last_ctb_x,
99
                      WORD32 i4_is_last_ctb_y)
100
121k
{
101
121k
    WORD32 ctb_size;
102
121k
    WORD32 log2_ctb_size;
103
121k
    UWORD32 u4_bs;
104
121k
    WORD32 bs_tz; /*Leading zeros in boundary strength*/
105
121k
    WORD32 qp_p, qp_q;
106
107
121k
    WORD32 filter_p, filter_q;
108
109
121k
    UWORD8 *pu1_src;
110
121k
    WORD32 qp_strd;
111
121k
    UWORD32 *pu4_vert_bs, *pu4_horz_bs;
112
121k
    UWORD32 *pu4_ctb_vert_bs, *pu4_ctb_horz_bs;
113
121k
    WORD32 bs_strd;
114
121k
    WORD32 src_strd;
115
121k
    UWORD8 *pu1_qp;
116
121k
    UWORD16 *pu2_ctb_no_loop_filter_flag;
117
121k
    UWORD16 au2_ctb_no_loop_filter_flag[9];
118
119
121k
    WORD32 col, row;
120
121
    /* Flag to indicate if QP is constant in CTB
122
     * 0 - top_left, 1 - top, 2 - left, 3 - current */
123
121k
    UWORD32 u4_qp_const_in_ctb[4] = { 0, 0, 0, 0 };
124
121k
    WORD32 ctb_indx;
125
121k
    WORD32  chroma_yuv420sp_vu = ps_deblk->is_chroma_yuv420sp_vu;
126
121k
    sps_t *ps_sps;
127
121k
    pps_t *ps_pps;
128
121k
    codec_t *ps_codec;
129
121k
    slice_header_t *ps_slice_hdr;
130
131
121k
    PROFILE_DISABLE_DEBLK();
132
133
121k
    ps_sps = ps_deblk->ps_sps;
134
121k
    ps_pps = ps_deblk->ps_pps;
135
121k
    ps_codec = ps_deblk->ps_codec;
136
121k
    ps_slice_hdr = ps_deblk->ps_slice_hdr;
137
138
121k
    log2_ctb_size = ps_sps->i1_log2_ctb_size;
139
121k
    ctb_size = (1 << ps_sps->i1_log2_ctb_size);
140
141
    /* strides are in units of number of bytes */
142
    /* ctb_size * ctb_size / 8 / 16 is the number of bytes needed per CTB */
143
121k
    bs_strd = (ps_sps->i2_pic_wd_in_ctb + 1) << (2 * log2_ctb_size - 7);
144
145
121k
    pu4_vert_bs = (UWORD32 *)((UWORD8 *)ps_deblk->s_bs_ctxt.pu4_pic_vert_bs +
146
121k
                    (ps_deblk->i4_ctb_x << (2 * log2_ctb_size - 7)) +
147
121k
                    ps_deblk->i4_ctb_y * bs_strd);
148
121k
    pu4_ctb_vert_bs = pu4_vert_bs;
149
150
121k
    pu4_horz_bs = (UWORD32 *)((UWORD8 *)ps_deblk->s_bs_ctxt.pu4_pic_horz_bs +
151
121k
                    (ps_deblk->i4_ctb_x << (2 * log2_ctb_size - 7)) +
152
121k
                    ps_deblk->i4_ctb_y * bs_strd);
153
121k
    pu4_ctb_horz_bs = pu4_horz_bs;
154
155
121k
    qp_strd = ps_sps->i2_pic_wd_in_ctb << (log2_ctb_size - 3);
156
121k
    pu1_qp = ps_deblk->s_bs_ctxt.pu1_pic_qp + ((ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * qp_strd) << (log2_ctb_size - 3));
157
158
121k
    pu2_ctb_no_loop_filter_flag = ps_deblk->au2_ctb_no_loop_filter_flag;
159
160
121k
    ctb_indx = ps_deblk->i4_ctb_x + ps_sps->i2_pic_wd_in_ctb * ps_deblk->i4_ctb_y;
161
121k
    if(i4_is_last_ctb_y)
162
299
    {
163
299
        pu4_vert_bs = (UWORD32 *)((UWORD8 *)pu4_vert_bs + bs_strd);
164
299
        pu4_ctb_vert_bs = pu4_vert_bs;
165
        /* ctb_size/8 is the number of edges per CTB
166
         * ctb_size/4 is the number of BS values needed per edge
167
         * divided by 8 for the number of bytes
168
         * 2 is the number of bits needed for each BS value */
169
299
        memset(pu4_vert_bs, 0, 1 << (2 * log2_ctb_size - 7));
170
171
299
        pu1_qp += (qp_strd << (log2_ctb_size - 3));
172
299
        pu2_ctb_no_loop_filter_flag += (ctb_size >> 3);
173
299
        ctb_indx += ps_sps->i2_pic_wd_in_ctb;
174
299
    }
175
176
121k
    if(i4_is_last_ctb_x)
177
4.58k
    {
178
4.58k
        pu4_horz_bs = (UWORD32 *)((UWORD8 *)pu4_horz_bs + (1 << (2 * log2_ctb_size - 7)));
179
4.58k
        pu4_ctb_horz_bs = pu4_horz_bs;
180
4.58k
        memset(pu4_horz_bs, 0, 1 << (2 * log2_ctb_size - 7));
181
182
4.58k
        pu1_qp += (ctb_size >> 3);
183
184
45.5k
        for(row = 0; row < (ctb_size >> 3) + 1; row++)
185
41.0k
            au2_ctb_no_loop_filter_flag[row] = ps_deblk->au2_ctb_no_loop_filter_flag[row] >> (ctb_size >> 3);
186
4.58k
        pu2_ctb_no_loop_filter_flag = au2_ctb_no_loop_filter_flag;
187
4.58k
        ctb_indx += 1;
188
4.58k
    }
189
190
121k
    u4_qp_const_in_ctb[3] = ps_deblk->s_bs_ctxt.pu1_pic_qp_const_in_ctb[(ctb_indx) >> 3] & (1 << (ctb_indx & 7));
191
192
121k
    if(ps_deblk->i4_ctb_x || i4_is_last_ctb_x)
193
114k
    {
194
114k
        u4_qp_const_in_ctb[2] = ps_deblk->s_bs_ctxt.pu1_pic_qp_const_in_ctb[(ctb_indx - 1) >> 3] & (1 << ((ctb_indx - 1) & 7));
195
114k
    }
196
197
121k
    if((ps_deblk->i4_ctb_x || i4_is_last_ctb_x) && (ps_deblk->i4_ctb_y || i4_is_last_ctb_y))
198
103k
    {
199
103k
        u4_qp_const_in_ctb[0] =
200
103k
                        ps_deblk->s_bs_ctxt.pu1_pic_qp_const_in_ctb[(ctb_indx - ps_sps->i2_pic_wd_in_ctb - 1) >> 3] &
201
103k
                        (1 << ((ctb_indx - ps_sps->i2_pic_wd_in_ctb - 1) & 7));
202
103k
    }
203
204
205
206
121k
    if(ps_deblk->i4_ctb_y || i4_is_last_ctb_y)
207
109k
    {
208
109k
        u4_qp_const_in_ctb[1] =
209
109k
                        ps_deblk->s_bs_ctxt.pu1_pic_qp_const_in_ctb[(ctb_indx - ps_sps->i2_pic_wd_in_ctb) >> 3] &
210
109k
                        (1 << ((ctb_indx - ps_sps->i2_pic_wd_in_ctb) & 7));
211
109k
    }
212
213
121k
    src_strd = ps_codec->i4_strd;
214
215
    /* Luma Vertical Edge */
216
217
121k
    if(0 == i4_is_last_ctb_x)
218
116k
    {
219
        /* Top CTB's slice header */
220
116k
        slice_header_t *ps_slice_hdr_top;
221
116k
        {
222
116k
            WORD32 cur_ctb_indx = ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
223
116k
            if(i4_is_last_ctb_y)
224
299
                cur_ctb_indx += ps_sps->i2_pic_wd_in_ctb;
225
116k
            ps_slice_hdr_top = ps_codec->ps_slice_hdr_base + ps_deblk->pu1_slice_idx[cur_ctb_indx - ps_sps->i2_pic_wd_in_ctb];
226
116k
        }
227
228
116k
        pu1_src = ps_deblk->pu1_cur_pic_luma + ((ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_deblk->ps_codec->i4_strd) << (log2_ctb_size));
229
116k
        pu1_src += i4_is_last_ctb_y ? ps_deblk->ps_codec->i4_strd << log2_ctb_size : 0;
230
231
        /** Deblocking is done on a shifted CTB -
232
         *  Vertical edge processing is done by shifting the CTB up by four pixels */
233
116k
        pu1_src -= 4 * src_strd;
234
235
1.01M
        for(col = 0; col < ctb_size / 8; col++)
236
902k
        {
237
902k
            WORD32 shift = 0;
238
239
            /*  downshift vert_bs by ctb_size/2 for each column
240
             *  shift = (col & ((MAX_CTB_SIZE >> log2_ctb_size) - 1)) << (log2_ctb_size - 1);
241
             *  which will reduce to the following assuming ctb size is one of 16, 32 and 64
242
             *  and deblocking is done on 8x8 grid
243
             */
244
902k
            if(6 != log2_ctb_size)
245
29.0k
                shift = (col & 1) << (log2_ctb_size - 1);
246
247
            /* BS for the column - Last row is excluded and the top row is included*/
248
902k
            u4_bs = (pu4_vert_bs[0] >> shift) << 2;
249
250
902k
            if(ps_deblk->i4_ctb_y || i4_is_last_ctb_y)
251
816k
            {
252
                /* Picking the last BS of the previous CTB corresponding to the same column */
253
816k
                UWORD32 *pu4_vert_bs_top = (UWORD32 *)((UWORD8 *)pu4_vert_bs - bs_strd);
254
816k
                UWORD32 u4_top_bs = (*pu4_vert_bs_top) >> (shift + (1 << (log2_ctb_size - 1)) - 2);
255
816k
                u4_bs |= u4_top_bs & 3;
256
816k
            }
257
258
4.22M
            for(row = 0; row < ctb_size / 4;)
259
3.31M
            {
260
3.31M
                WORD8 i1_beta_offset_div2 = ps_slice_hdr->i1_beta_offset_div2;
261
3.31M
                WORD8 i1_tc_offset_div2 = ps_slice_hdr->i1_tc_offset_div2;
262
263
                /* Trailing zeros are computed and the corresponding rows are not processed */
264
3.31M
                bs_tz = CTZ(u4_bs) >> 1;
265
3.31M
                if(0 != bs_tz)
266
1.62M
                {
267
1.62M
                    u4_bs = u4_bs >> (bs_tz << 1);
268
1.62M
                    if((row + bs_tz) >= (ctb_size / 4))
269
801k
                        pu1_src += 4 * (ctb_size / 4 - row) * src_strd;
270
828k
                    else
271
828k
                        pu1_src += 4 * bs_tz  * src_strd;
272
273
1.62M
                    row += bs_tz;
274
1.62M
                    continue;
275
1.62M
                }
276
277
1.68M
                if(0 == row)
278
101k
                {
279
101k
                    i1_beta_offset_div2 = ps_slice_hdr_top->i1_beta_offset_div2;
280
101k
                    i1_tc_offset_div2 = ps_slice_hdr_top->i1_tc_offset_div2;
281
282
101k
                    if(0 == col)
283
24.1k
                    {
284
24.1k
                        qp_p = u4_qp_const_in_ctb[0] ?
285
24.0k
                                        pu1_qp[-ctb_size / 8 * qp_strd - ctb_size / 8] :
286
24.1k
                                        pu1_qp[-qp_strd - 1];
287
24.1k
                    }
288
77.5k
                    else
289
77.5k
                    {
290
77.5k
                        qp_p = u4_qp_const_in_ctb[1] ?
291
77.0k
                                        pu1_qp[-ctb_size / 8 * qp_strd] :
292
77.5k
                                        pu1_qp[col - 1 - qp_strd];
293
77.5k
                    }
294
295
101k
                    qp_q = u4_qp_const_in_ctb[1] ?
296
101k
                                    pu1_qp[-ctb_size / 8 * qp_strd] :
297
101k
                                    pu1_qp[col - qp_strd];
298
101k
                }
299
1.58M
                else
300
1.58M
                {
301
1.58M
                    if(0 == col)
302
379k
                    {
303
379k
                        qp_p = u4_qp_const_in_ctb[2] ?
304
377k
                                        pu1_qp[-ctb_size / 8] :
305
379k
                                        pu1_qp[((row - 1) >> 1) * qp_strd - 1];
306
379k
                    }
307
1.20M
                    else
308
1.20M
                    {
309
1.20M
                        qp_p = u4_qp_const_in_ctb[3] ?
310
1.20M
                                        pu1_qp[0] :
311
18.4E
                                        pu1_qp[((row - 1) >> 1) * qp_strd + col - 1];
312
1.20M
                    }
313
314
1.58M
                    qp_q = u4_qp_const_in_ctb[3] ?
315
1.58M
                                    pu1_qp[0] :
316
1.58M
                                    pu1_qp[((row - 1) >> 1) * qp_strd + col];
317
1.58M
                }
318
319
1.68M
                filter_p = (pu2_ctb_no_loop_filter_flag[(row + 1) >> 1] >> col) & 1;
320
1.68M
                filter_q = (pu2_ctb_no_loop_filter_flag[(row + 1) >> 1] >> col) & 2;
321
                /* filter_p and filter_q are inverted as they are calculated using no_loop_filter_flags */
322
1.68M
                filter_p = !filter_p;
323
1.68M
                filter_q = !filter_q;
324
325
1.68M
                if(filter_p || filter_q)
326
1.69M
                {
327
1.69M
                    DUMP_DEBLK_LUMA_VERT(pu1_src, src_strd,
328
1.69M
                                         u4_bs & 3, qp_p, qp_q,
329
1.69M
                                         ps_slice_hdr->i1_beta_offset_div2,
330
1.69M
                                         ps_slice_hdr->i1_tc_offset_div2,
331
1.69M
                                         filter_p, filter_q);
332
1.69M
                    ps_codec->s_func_selector.ihevc_deblk_luma_vert_fptr(pu1_src, src_strd,
333
1.69M
                                                                         u4_bs & 3, qp_p, qp_q,
334
1.69M
                                                                         i1_beta_offset_div2,
335
1.69M
                                                                         i1_tc_offset_div2,
336
1.69M
                                                                         filter_p, filter_q);
337
1.69M
                }
338
339
1.68M
                pu1_src += 4 * src_strd;
340
1.68M
                u4_bs = u4_bs >> 2;
341
1.68M
                row++;
342
1.68M
            }
343
344
902k
            if((64 == ctb_size) ||
345
902k
                            ((32 == ctb_size) && (col & 1)))
346
888k
            {
347
888k
                pu4_vert_bs++;
348
888k
            }
349
902k
            pu1_src -= (src_strd << log2_ctb_size);
350
902k
            pu1_src += 8;
351
902k
        }
352
116k
        pu4_vert_bs = pu4_ctb_vert_bs;
353
116k
    }
354
355
356
    /* Luma Horizontal Edge */
357
358
121k
    if(0 == i4_is_last_ctb_y)
359
120k
    {
360
361
        /* Left CTB's slice header */
362
120k
        slice_header_t *ps_slice_hdr_left;
363
120k
        {
364
120k
            WORD32 cur_ctb_indx = ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
365
120k
            if(i4_is_last_ctb_x)
366
4.58k
                cur_ctb_indx += 1;
367
120k
            ps_slice_hdr_left = ps_codec->ps_slice_hdr_base + ps_deblk->pu1_slice_idx[cur_ctb_indx - 1];
368
120k
        }
369
120k
        pu1_src = ps_deblk->pu1_cur_pic_luma + ((ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_deblk->ps_codec->i4_strd) << log2_ctb_size);
370
120k
        pu1_src += i4_is_last_ctb_x ? ctb_size : 0;
371
372
        /** Deblocking is done on a shifted CTB -
373
         *  Horizontal edge processing is done by shifting the CTB left by four pixels */
374
120k
        pu1_src -= 4;
375
1.05M
        for(row = 0; row < ctb_size / 8; row++)
376
936k
        {
377
936k
            WORD32 shift = 0;
378
379
            /* downshift vert_bs by ctb_size/2 for each column
380
             *  shift = (row & (MAX_CTB_SIZE / ctb_size - 1)) * ctb_size / 2;
381
             *  which will reduce to the following assuming ctb size is one of 16, 32 and 64
382
             *  and deblocking is done on 8x8 grid
383
             */
384
936k
            if(6 != log2_ctb_size)
385
28.6k
                shift = (row & 1) << (log2_ctb_size - 1);
386
387
            /* BS for the row - Last column is excluded and the left column is included*/
388
936k
            u4_bs = (pu4_horz_bs[0] >> shift) << 2;
389
390
936k
            if(ps_deblk->i4_ctb_x || i4_is_last_ctb_x)
391
884k
            {
392
                /** Picking the last BS of the previous CTB corresponding to the same row
393
                * UWORD32 *pu4_horz_bs_left = (UWORD32 *)((UWORD8 *)pu4_horz_bs - (ctb_size / 8) * (ctb_size / 4) / 8 * 2);
394
                */
395
884k
                UWORD32 *pu4_horz_bs_left = (UWORD32 *)((UWORD8 *)pu4_horz_bs - (1 << (2 * log2_ctb_size - 7)));
396
884k
                UWORD32 u4_left_bs = (*pu4_horz_bs_left) >> (shift + (1 << (log2_ctb_size - 1)) - 2);
397
884k
                u4_bs |= u4_left_bs & 3;
398
884k
            }
399
400
4.35M
            for(col = 0; col < ctb_size / 4;)
401
3.41M
            {
402
3.41M
                WORD8 i1_beta_offset_div2 = ps_slice_hdr->i1_beta_offset_div2;
403
3.41M
                WORD8 i1_tc_offset_div2 = ps_slice_hdr->i1_tc_offset_div2;
404
405
3.41M
                bs_tz = CTZ(u4_bs) >> 1;
406
3.41M
                if(0 != bs_tz)
407
1.68M
                {
408
1.68M
                    u4_bs = u4_bs >> (bs_tz << 1);
409
410
1.68M
                    if((col + bs_tz) >= (ctb_size / 4))
411
830k
                        pu1_src += 4 * (ctb_size / 4 - col);
412
856k
                    else
413
856k
                        pu1_src += 4 * bs_tz;
414
415
1.68M
                    col += bs_tz;
416
1.68M
                    continue;
417
1.68M
                }
418
419
1.72M
                if(0 == col)
420
106k
                {
421
106k
                    i1_beta_offset_div2 = ps_slice_hdr_left->i1_beta_offset_div2;
422
106k
                    i1_tc_offset_div2 = ps_slice_hdr_left->i1_tc_offset_div2;
423
424
106k
                    if(0 == row)
425
28.3k
                    {
426
28.3k
                        qp_p = u4_qp_const_in_ctb[0] ?
427
28.1k
                                        pu1_qp[-ctb_size / 8 * qp_strd - ctb_size / 8] :
428
28.3k
                                        pu1_qp[-qp_strd - 1];
429
28.3k
                    }
430
78.5k
                    else
431
78.5k
                    {
432
78.5k
                        qp_p = u4_qp_const_in_ctb[2] ?
433
77.9k
                                        pu1_qp[-ctb_size / 8] :
434
78.5k
                                        pu1_qp[(row - 1) * qp_strd - 1];
435
78.5k
                    }
436
437
106k
                    qp_q = u4_qp_const_in_ctb[2] ?
438
106k
                                    pu1_qp[-ctb_size / 8] :
439
106k
                                    pu1_qp[row * qp_strd - 1];
440
106k
                }
441
1.62M
                else
442
1.62M
                {
443
1.62M
                    if(0 == row)
444
430k
                    {
445
430k
                        qp_p = u4_qp_const_in_ctb[1] ?
446
427k
                                        pu1_qp[-ctb_size / 8 * qp_strd] :
447
430k
                                        pu1_qp[((col - 1) >> 1) - qp_strd];
448
430k
                    }
449
1.19M
                    else
450
1.19M
                    {
451
1.19M
                        qp_p = u4_qp_const_in_ctb[3] ?
452
1.19M
                                        pu1_qp[0] :
453
1.19M
                                        pu1_qp[((col - 1) >> 1) + (row - 1) * qp_strd];
454
1.19M
                    }
455
456
1.62M
                    qp_q = u4_qp_const_in_ctb[3] ?
457
1.61M
                                    pu1_qp[0] :
458
1.62M
                                    pu1_qp[((col - 1) >> 1) + row * qp_strd];
459
1.62M
                }
460
461
1.72M
                filter_p = (pu2_ctb_no_loop_filter_flag[row] >> ((col + 1) >> 1)) & 1;
462
1.72M
                filter_q = (pu2_ctb_no_loop_filter_flag[row + 1] >> ((col + 1) >> 1)) & 1;
463
                /* filter_p and filter_q are inverted as they are calculated using no_loop_filter_flags */
464
1.72M
                filter_p = !filter_p;
465
1.72M
                filter_q = !filter_q;
466
467
1.72M
                if(filter_p || filter_q)
468
1.73M
                {
469
1.73M
                    DUMP_DEBLK_LUMA_HORZ(pu1_src, src_strd,
470
1.73M
                                         u4_bs & 3, qp_p, qp_q,
471
1.73M
                                         ps_slice_hdr->i1_beta_offset_div2,
472
1.73M
                                         ps_slice_hdr->i1_tc_offset_div2,
473
1.73M
                                         filter_p, filter_q);
474
1.73M
                    ps_codec->s_func_selector.ihevc_deblk_luma_horz_fptr(pu1_src, src_strd,
475
1.73M
                                                                         u4_bs & 3, qp_p, qp_q,
476
1.73M
                                                                         i1_beta_offset_div2,
477
1.73M
                                                                         i1_tc_offset_div2, filter_p, filter_q);
478
1.73M
                }
479
480
1.72M
                pu1_src += 4;
481
1.72M
                u4_bs = u4_bs >> 2;
482
1.72M
                col++;
483
1.72M
            }
484
485
936k
            if((64 == ctb_size) ||
486
936k
                            ((32 == ctb_size) && (row & 1)))
487
922k
            {
488
922k
                pu4_horz_bs++;
489
922k
            }
490
936k
            pu1_src -= ctb_size;
491
936k
            pu1_src += (src_strd << 3);
492
936k
        }
493
120k
        pu4_horz_bs = pu4_ctb_horz_bs;
494
120k
    }
495
496
497
    /* Chroma Veritcal Edge */
498
499
121k
    if(0 == i4_is_last_ctb_x)
500
116k
    {
501
502
        /* Top CTB's slice header */
503
116k
        slice_header_t *ps_slice_hdr_top;
504
116k
        {
505
116k
            WORD32 cur_ctb_indx = ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
506
116k
            if(i4_is_last_ctb_y)
507
299
                cur_ctb_indx += ps_sps->i2_pic_wd_in_ctb;
508
116k
            ps_slice_hdr_top = ps_codec->ps_slice_hdr_base + ps_deblk->pu1_slice_idx[cur_ctb_indx - ps_sps->i2_pic_wd_in_ctb];
509
116k
        }
510
511
116k
        pu1_src = ps_deblk->pu1_cur_pic_chroma + ((ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_deblk->ps_codec->i4_strd / 2) << log2_ctb_size);
512
116k
        pu1_src += i4_is_last_ctb_y ? (ps_deblk->ps_codec->i4_strd / 2) << log2_ctb_size : 0;
513
514
        /** Deblocking is done on a shifted CTB -
515
         *  Vertical edge processing is done by shifting the CTB up by four pixels */
516
116k
        pu1_src -= 4 * src_strd;
517
518
568k
        for(col = 0; col < ctb_size / 16; col++)
519
451k
        {
520
521
            /* BS for the column - Last row is excluded and the top row is included*/
522
451k
            u4_bs = pu4_vert_bs[0] << 2;
523
524
451k
            if(ps_deblk->i4_ctb_y || i4_is_last_ctb_y)
525
408k
            {
526
                /* Picking the last BS of the previous CTB corresponding to the same column */
527
408k
                UWORD32 *pu4_vert_bs_top = (UWORD32 *)((UWORD8 *)pu4_vert_bs - bs_strd);
528
408k
                UWORD32 u4_top_bs = (*pu4_vert_bs_top) >> ((1 << (log2_ctb_size - 1)) - 2);
529
408k
                u4_bs |= u4_top_bs & 3;
530
408k
            }
531
532
            /* Every alternate boundary strength value is used for chroma */
533
451k
            u4_bs &= 0x22222222;
534
535
1.53M
            for(row = 0; row < ctb_size / 8;)
536
1.08M
            {
537
1.08M
                WORD8 i1_tc_offset_div2 = ps_slice_hdr->i1_tc_offset_div2;
538
539
1.08M
                bs_tz = CTZ(u4_bs) >> 2;
540
1.08M
                if(0 != bs_tz)
541
833k
                {
542
833k
                    if((row + bs_tz) >= (ctb_size / 8))
543
420k
                        pu1_src += 4 * (ctb_size / 8 - row) * src_strd;
544
413k
                    else
545
413k
                        pu1_src += 4 * bs_tz  * src_strd;
546
833k
                    row += bs_tz;
547
833k
                    u4_bs = u4_bs >> (bs_tz << 2);
548
833k
                    continue;
549
833k
                }
550
551
248k
                if(0 == row)
552
31.1k
                {
553
31.1k
                    i1_tc_offset_div2 = ps_slice_hdr_top->i1_tc_offset_div2;
554
555
31.1k
                    if(0 == col)
556
9.88k
                    {
557
9.88k
                        qp_p = u4_qp_const_in_ctb[0] ?
558
9.81k
                                        pu1_qp[-ctb_size / 8 * qp_strd - ctb_size / 8] :
559
9.88k
                                        pu1_qp[-qp_strd - 1];
560
9.88k
                    }
561
21.2k
                    else
562
21.2k
                    {
563
21.2k
                        qp_p = u4_qp_const_in_ctb[1] ?
564
21.0k
                                        pu1_qp[-ctb_size / 8 * qp_strd] :
565
21.2k
                                        pu1_qp[2 * col - 1 - qp_strd];
566
21.2k
                    }
567
568
31.1k
                    qp_q = u4_qp_const_in_ctb[1] ?
569
30.9k
                                    pu1_qp[-ctb_size / 8 * qp_strd] :
570
31.1k
                                    pu1_qp[2 * col - qp_strd];
571
31.1k
                }
572
216k
                else
573
216k
                {
574
216k
                    if(0 == col)
575
70.7k
                    {
576
70.7k
                        qp_p = u4_qp_const_in_ctb[2] ?
577
70.3k
                                        pu1_qp[-ctb_size / 8] :
578
70.7k
                                        pu1_qp[(row - 1) * qp_strd - 1];
579
70.7k
                    }
580
146k
                    else
581
146k
                    {
582
146k
                        qp_p = u4_qp_const_in_ctb[3] ?
583
145k
                                        pu1_qp[0] :
584
146k
                                        pu1_qp[(row - 1) * qp_strd + 2 * col - 1];
585
146k
                    }
586
587
216k
                    qp_q = u4_qp_const_in_ctb[3] ?
588
215k
                                    pu1_qp[0] :
589
216k
                                    pu1_qp[(row - 1) * qp_strd + 2 * col];
590
216k
                }
591
592
248k
                filter_p = (pu2_ctb_no_loop_filter_flag[row] >> (col << 1)) & 1;
593
248k
                filter_q = (pu2_ctb_no_loop_filter_flag[row] >> (col << 1)) & 2;
594
                /* filter_p and filter_q are inverted as they are calculated using no_loop_filter_flags */
595
248k
                filter_p = !filter_p;
596
248k
                filter_q = !filter_q;
597
598
248k
                if(filter_p || filter_q)
599
248k
                {
600
248k
                    ASSERT(1 == ((u4_bs & 3) >> 1));
601
248k
                    DUMP_DEBLK_CHROMA_VERT(pu1_src, src_strd,
602
248k
                                           u4_bs & 3, qp_p, qp_q,
603
248k
                                           ps_pps->i1_pic_cb_qp_offset,
604
248k
                                           ps_pps->i1_pic_cr_qp_offset,
605
248k
                                           ps_slice_hdr->i1_tc_offset_div2,
606
248k
                                           filter_p, filter_q);
607
248k
                    if(chroma_yuv420sp_vu)
608
132k
                    {
609
132k
                        ps_codec->s_func_selector.ihevc_deblk_chroma_vert_fptr(pu1_src,
610
132k
                                                                               src_strd,
611
132k
                                                                               qp_q,
612
132k
                                                                               qp_p,
613
132k
                                                                               ps_pps->i1_pic_cr_qp_offset,
614
132k
                                                                               ps_pps->i1_pic_cb_qp_offset,
615
132k
                                                                               i1_tc_offset_div2,
616
132k
                                                                               filter_q,
617
132k
                                                                               filter_p);
618
132k
                    }
619
115k
                    else
620
115k
                    {
621
115k
                        ps_codec->s_func_selector.ihevc_deblk_chroma_vert_fptr(pu1_src,
622
115k
                                                                               src_strd,
623
115k
                                                                               qp_p,
624
115k
                                                                               qp_q,
625
115k
                                                                               ps_pps->i1_pic_cb_qp_offset,
626
115k
                                                                               ps_pps->i1_pic_cr_qp_offset,
627
115k
                                                                               i1_tc_offset_div2,
628
115k
                                                                               filter_p,
629
115k
                                                                               filter_q);
630
115k
                    }
631
248k
                }
632
633
248k
                pu1_src += 4 * src_strd;
634
248k
                u4_bs = u4_bs >> 4;
635
248k
                row++;
636
248k
            }
637
638
451k
            pu4_vert_bs += (64 == ctb_size) ? 2 : 1;
639
451k
            pu1_src -= ((src_strd / 2) << log2_ctb_size);
640
451k
            pu1_src += 16;
641
451k
        }
642
116k
    }
643
644
    /* Chroma Horizontal Edge */
645
646
121k
    if(0 == i4_is_last_ctb_y)
647
120k
    {
648
649
        /* Left CTB's slice header */
650
120k
        slice_header_t *ps_slice_hdr_left;
651
120k
        {
652
120k
            WORD32 cur_ctb_indx = ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_sps->i2_pic_wd_in_ctb;
653
120k
            if(i4_is_last_ctb_x)
654
4.58k
                cur_ctb_indx += 1;
655
120k
            ps_slice_hdr_left = ps_codec->ps_slice_hdr_base + ps_deblk->pu1_slice_idx[cur_ctb_indx - 1];
656
120k
        }
657
658
120k
        pu1_src = ps_deblk->pu1_cur_pic_chroma + ((ps_deblk->i4_ctb_x + ps_deblk->i4_ctb_y * ps_deblk->ps_codec->i4_strd / 2) << log2_ctb_size);
659
120k
        pu1_src += i4_is_last_ctb_x ? ctb_size : 0;
660
661
        /** Deblocking is done on a shifted CTB -
662
         * Vertical edge processing is done by shifting the CTB up by four pixels (8 here beacuse UV are interleaved) */
663
120k
        pu1_src -= 8;
664
589k
        for(row = 0; row < ctb_size / 16; row++)
665
469k
        {
666
            /* BS for the row - Last column is excluded and the left column is included*/
667
469k
            u4_bs = pu4_horz_bs[0] << 2;
668
669
469k
            if(ps_deblk->i4_ctb_x || i4_is_last_ctb_x)
670
442k
            {
671
                /** Picking the last BS of the previous CTB corresponding to the same row
672
                * UWORD32 *pu4_horz_bs_left = (UWORD32 *)((UWORD8 *)pu4_horz_bs - (ctb_size / 8) * (ctb_size / 4) / 8 * 2);
673
                */
674
442k
                UWORD32 *pu4_horz_bs_left = (UWORD32 *)((UWORD8 *)pu4_horz_bs - (1 << (2 * log2_ctb_size - 7)));
675
442k
                UWORD32 u4_left_bs = (*pu4_horz_bs_left) >> ((1 << (log2_ctb_size - 1)) - 2);
676
442k
                u4_bs |= u4_left_bs & 3;
677
442k
            }
678
679
            /* Every alternate boundary strength value is used for chroma */
680
469k
            u4_bs &= 0x22222222;
681
682
1.57M
            for(col = 0; col < ctb_size / 8;)
683
1.11M
            {
684
1.11M
                WORD8 i1_tc_offset_div2 = ps_slice_hdr->i1_tc_offset_div2;
685
686
1.11M
                bs_tz = CTZ(u4_bs) >> 2;
687
1.11M
                if(0 != bs_tz)
688
871k
                {
689
871k
                    u4_bs = u4_bs >> (bs_tz << 2);
690
691
871k
                    if((col + bs_tz) >= (ctb_size / 8))
692
439k
                        pu1_src += 8 * (ctb_size / 8 - col);
693
431k
                    else
694
431k
                        pu1_src += 8 * bs_tz;
695
696
871k
                    col += bs_tz;
697
871k
                    continue;
698
871k
                }
699
700
238k
                if(0 == col)
701
29.2k
                {
702
29.2k
                    i1_tc_offset_div2 = ps_slice_hdr_left->i1_tc_offset_div2;
703
704
29.2k
                    if(0 == row)
705
8.73k
                    {
706
8.73k
                        qp_p = u4_qp_const_in_ctb[0] ?
707
8.68k
                                        pu1_qp[-ctb_size / 8 * qp_strd - ctb_size / 8] :
708
8.73k
                                        pu1_qp[-qp_strd - 1];
709
8.73k
                    }
710
20.4k
                    else
711
20.4k
                    {
712
20.4k
                        qp_p = u4_qp_const_in_ctb[2] ?
713
20.3k
                                        pu1_qp[-ctb_size / 8] :
714
20.4k
                                        pu1_qp[(2 * row - 1) * qp_strd - 1];
715
20.4k
                    }
716
717
29.2k
                    qp_q = u4_qp_const_in_ctb[2] ?
718
29.0k
                                    pu1_qp[-ctb_size / 8] :
719
29.2k
                                    pu1_qp[(2 * row) * qp_strd - 1];
720
29.2k
                }
721
209k
                else
722
209k
                {
723
209k
                    if(0 == row)
724
62.8k
                    {
725
62.8k
                        qp_p = u4_qp_const_in_ctb[1] ?
726
62.4k
                                        pu1_qp[-ctb_size / 8 * qp_strd] :
727
62.8k
                                        pu1_qp[col - 1 - qp_strd];
728
62.8k
                    }
729
146k
                    else
730
146k
                    {
731
146k
                        qp_p = u4_qp_const_in_ctb[3] ?
732
145k
                                        pu1_qp[0] :
733
146k
                                        pu1_qp[(col - 1) +  (2 * row - 1) * qp_strd];
734
146k
                    }
735
736
209k
                    qp_q = u4_qp_const_in_ctb[3] ?
737
208k
                                    pu1_qp[0] :
738
209k
                                    pu1_qp[(col - 1) + 2 * row * qp_strd];
739
209k
                }
740
741
238k
                filter_p = (pu2_ctb_no_loop_filter_flag[row << 1] >> col) & 1;
742
238k
                filter_q = (pu2_ctb_no_loop_filter_flag[(row << 1) + 1] >> col) & 1;
743
                /* filter_p and filter_q are inverted as they are calculated using no_loop_filter_flags */
744
238k
                filter_p = !filter_p;
745
238k
                filter_q = !filter_q;
746
747
238k
                if(filter_p || filter_q)
748
238k
                {
749
238k
                    ASSERT(1 == ((u4_bs & 3) >> 1));
750
238k
                    DUMP_DEBLK_CHROMA_HORZ(pu1_src, src_strd,
751
238k
                                           u4_bs & 3, qp_p, qp_q,
752
238k
                                           ps_pps->i1_pic_cb_qp_offset,
753
238k
                                           ps_pps->i1_pic_cr_qp_offset,
754
238k
                                           ps_slice_hdr->i1_tc_offset_div2,
755
238k
                                           filter_p, filter_q);
756
238k
                    if(chroma_yuv420sp_vu)
757
122k
                    {
758
122k
                        ps_codec->s_func_selector.ihevc_deblk_chroma_horz_fptr(pu1_src,
759
122k
                                                                               src_strd,
760
122k
                                                                               qp_q,
761
122k
                                                                               qp_p,
762
122k
                                                                               ps_pps->i1_pic_cr_qp_offset,
763
122k
                                                                               ps_pps->i1_pic_cb_qp_offset,
764
122k
                                                                               i1_tc_offset_div2,
765
122k
                                                                               filter_q,
766
122k
                                                                               filter_p);
767
122k
                    }
768
116k
                    else
769
116k
                    {
770
116k
                        ps_codec->s_func_selector.ihevc_deblk_chroma_horz_fptr(pu1_src,
771
116k
                                                                               src_strd,
772
116k
                                                                               qp_p,
773
116k
                                                                               qp_q,
774
116k
                                                                               ps_pps->i1_pic_cb_qp_offset,
775
116k
                                                                               ps_pps->i1_pic_cr_qp_offset,
776
116k
                                                                               i1_tc_offset_div2,
777
116k
                                                                               filter_p,
778
116k
                                                                               filter_q);
779
116k
                    }
780
238k
                }
781
782
238k
                pu1_src += 8;
783
238k
                u4_bs = u4_bs >> 4;
784
238k
                col++;
785
238k
            }
786
787
469k
            pu4_horz_bs += (64 == ctb_size) ? 2 : 1;
788
469k
            pu1_src -= ctb_size;
789
469k
            pu1_src += 8 * src_strd;
790
791
469k
        }
792
120k
    }
793
121k
}