Coverage Report

Created: 2023-06-07 07:34

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