Coverage Report

Created: 2025-10-10 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_sao.c
Line
Count
Source
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
23
*  ihevce_sao.c
24
*
25
* @brief
26
*  Contains definition for the ctb level sao function
27
*
28
* @author
29
*  Ittiam
30
*
31
* @par List of Functions:
32
*  ihevce_sao_set_avilability()
33
*  ihevce_sao_ctb()
34
*  ihevce_sao_analyse()
35
*
36
* @remarks
37
*  None
38
*
39
*******************************************************************************
40
*/
41
42
/*****************************************************************************/
43
/* File Includes                                                             */
44
/*****************************************************************************/
45
/* System include files */
46
#include <stdio.h>
47
#include <string.h>
48
#include <stdlib.h>
49
#include <assert.h>
50
#include <stdarg.h>
51
#include <math.h>
52
53
/* User include files */
54
#include "ihevc_typedefs.h"
55
#include "itt_video_api.h"
56
#include "ihevce_api.h"
57
58
#include "rc_cntrl_param.h"
59
#include "rc_frame_info_collector.h"
60
#include "rc_look_ahead_params.h"
61
62
#include "ihevc_defs.h"
63
#include "ihevc_structs.h"
64
#include "ihevc_platform_macros.h"
65
#include "ihevc_deblk.h"
66
#include "ihevc_itrans_recon.h"
67
#include "ihevc_chroma_itrans_recon.h"
68
#include "ihevc_chroma_intra_pred.h"
69
#include "ihevc_intra_pred.h"
70
#include "ihevc_inter_pred.h"
71
#include "ihevc_mem_fns.h"
72
#include "ihevc_padding.h"
73
#include "ihevc_weighted_pred.h"
74
#include "ihevc_sao.h"
75
#include "ihevc_resi_trans.h"
76
#include "ihevc_quant_iquant_ssd.h"
77
#include "ihevc_cabac_tables.h"
78
79
#include "ihevce_defs.h"
80
#include "ihevce_lap_enc_structs.h"
81
#include "ihevce_multi_thrd_structs.h"
82
#include "ihevce_me_common_defs.h"
83
#include "ihevce_had_satd.h"
84
#include "ihevce_error_codes.h"
85
#include "ihevce_bitstream.h"
86
#include "ihevce_cabac.h"
87
#include "ihevce_rdoq_macros.h"
88
#include "ihevce_function_selector.h"
89
#include "ihevce_enc_structs.h"
90
#include "ihevce_entropy_structs.h"
91
#include "ihevce_cmn_utils_instr_set_router.h"
92
#include "ihevce_enc_loop_structs.h"
93
#include "ihevce_cabac_rdo.h"
94
#include "ihevce_sao.h"
95
96
/*****************************************************************************/
97
/* Function Definitions                                                      */
98
/*****************************************************************************/
99
100
/**
101
*******************************************************************************
102
*
103
* @brief
104
*     ihevce_sao_set_avilability
105
*
106
* @par Description:
107
*     Sets the availability flag for SAO.
108
*
109
* @param[in]
110
*   ps_sao_ctxt:   Pointer to SAO context
111
* @returns
112
*
113
* @remarks
114
*  None
115
*
116
*******************************************************************************
117
*/
118
void ihevce_sao_set_avilability(
119
    UWORD8 *pu1_avail, sao_ctxt_t *ps_sao_ctxt, ihevce_tile_params_t *ps_tile_params)
120
811k
{
121
811k
    WORD32 i;
122
123
811k
    WORD32 ctb_x_pos = ps_sao_ctxt->i4_ctb_x;
124
811k
    WORD32 ctb_y_pos = ps_sao_ctxt->i4_ctb_y;
125
126
7.30M
    for(i = 0; i < 8; i++)
127
6.49M
    {
128
6.49M
        pu1_avail[i] = 255;
129
6.49M
    }
130
131
    /* SAO_note_01: If the CTB lies on a tile or a slice boundary and
132
    in-loop filtering is enabled at tile and slice boundary, then SAO must
133
    be performed at tile/slice boundaries also.
134
    Hence the boundary checks should be based on frame position of CTB
135
    rather than s_ctb_nbr_avail_flags.u1_left_avail flags.
136
    Search for <SAO_note_01> in workspace to know more */
137
    /* Availaibility flags for first col*/
138
811k
    if(ctb_x_pos == ps_tile_params->i4_first_ctb_x)
139
384k
    {
140
384k
        pu1_avail[0] = 0;
141
384k
        pu1_avail[4] = 0;
142
384k
        pu1_avail[6] = 0;
143
384k
    }
144
145
    /* Availaibility flags for last col*/
146
811k
    if((ctb_x_pos + 1) ==
147
811k
       (ps_tile_params->i4_first_ctb_x + ps_tile_params->i4_curr_tile_wd_in_ctb_unit))
148
389k
    {
149
389k
        pu1_avail[1] = 0;
150
389k
        pu1_avail[5] = 0;
151
389k
        pu1_avail[7] = 0;
152
389k
    }
153
154
    /* Availaibility flags for first row*/
155
811k
    if(ctb_y_pos == ps_tile_params->i4_first_ctb_y)
156
455k
    {
157
455k
        pu1_avail[2] = 0;
158
455k
        pu1_avail[4] = 0;
159
455k
        pu1_avail[5] = 0;
160
455k
    }
161
162
    /* Availaibility flags for last row*/
163
811k
    if((ctb_y_pos + 1) ==
164
811k
       (ps_tile_params->i4_first_ctb_y + ps_tile_params->i4_curr_tile_ht_in_ctb_unit))
165
460k
    {
166
460k
        pu1_avail[3] = 0;
167
460k
        pu1_avail[6] = 0;
168
460k
        pu1_avail[7] = 0;
169
460k
    }
170
811k
}
171
172
/**
173
*******************************************************************************
174
*
175
* @brief
176
*   Sao CTB level function.
177
*
178
* @par Description:
179
*   For a given CTB, sao is done. Both the luma and chroma
180
*   blocks are processed
181
*
182
* @param[in]
183
*   ps_sao_ctxt:   Pointer to SAO context
184
*
185
* @returns
186
*
187
* @remarks
188
*  None
189
*
190
*******************************************************************************
191
*/
192
void ihevce_sao_ctb(sao_ctxt_t *ps_sao_ctxt, ihevce_tile_params_t *ps_tile_params)
193
867k
{
194
867k
    sao_enc_t *ps_sao;
195
867k
    UWORD8 u1_src_top_left_luma, u1_src_top_left_chroma[2];
196
867k
    UWORD8 *pu1_src_left_luma_buf, *pu1_src_top_luma_buf;
197
867k
    UWORD8 *pu1_src_left_chroma_buf, *pu1_src_top_chroma_buf;
198
867k
    UWORD8 *pu1_src_luma, *pu1_src_chroma;
199
867k
    WORD32 luma_src_stride, ctb_size;
200
867k
    WORD32 chroma_src_stride;
201
867k
    UWORD8 au1_avail_luma[8], au1_avail_chroma[8];
202
867k
    WORD32 sao_blk_wd, sao_blk_ht, sao_wd_chroma, sao_ht_chroma;
203
867k
    UWORD8 *pu1_top_left_luma, *pu1_top_left_chroma;
204
867k
    UWORD8 *pu1_src_bot_left_luma, *pu1_src_top_right_luma;
205
867k
    UWORD8 *pu1_src_bot_left_chroma, *pu1_src_top_right_chroma;
206
867k
    UWORD8 u1_is_422 = (ps_sao_ctxt->ps_sps->i1_chroma_format_idc == 2);
207
208
867k
    ps_sao = ps_sao_ctxt->ps_sao;
209
210
867k
    ASSERT(
211
867k
        (abs(ps_sao->u1_y_offset[1]) <= 7) && (abs(ps_sao->u1_y_offset[2]) <= 7) &&
212
867k
        (abs(ps_sao->u1_y_offset[3]) <= 7) && (abs(ps_sao->u1_y_offset[4]) <= 7));
213
867k
    ASSERT(
214
867k
        (abs(ps_sao->u1_cb_offset[1]) <= 7) && (abs(ps_sao->u1_cb_offset[2]) <= 7) &&
215
867k
        (abs(ps_sao->u1_cb_offset[3]) <= 7) && (abs(ps_sao->u1_cb_offset[4]) <= 7));
216
867k
    ASSERT(
217
867k
        (abs(ps_sao->u1_cr_offset[1]) <= 7) && (abs(ps_sao->u1_cr_offset[2]) <= 7) &&
218
867k
        (abs(ps_sao->u1_cr_offset[3]) <= 7) && (abs(ps_sao->u1_cr_offset[4]) <= 7));
219
867k
    ASSERT(
220
867k
        (ps_sao->b5_y_band_pos <= 28) && (ps_sao->b5_cb_band_pos <= 28) &&
221
867k
        (ps_sao->b5_cr_band_pos <= 28));
222
223
867k
    if(ps_sao_ctxt->i1_slice_sao_luma_flag)
224
867k
    {
225
        /*initialize the src pointer to current row*/
226
867k
        luma_src_stride = ps_sao_ctxt->i4_cur_luma_recon_stride;
227
228
867k
        ctb_size = ps_sao_ctxt->i4_ctb_size;
229
230
        /* 1 extra byte in top buf stride for top left of 1st ctb of every row*/
231
867k
        ps_sao->u1_y_offset[0] = 0; /* 0th element is not being used  */
232
867k
        sao_blk_wd = ps_sao_ctxt->i4_sao_blk_wd;
233
867k
        sao_blk_ht = ps_sao_ctxt->i4_sao_blk_ht;
234
235
867k
        pu1_src_luma = ps_sao_ctxt->pu1_cur_luma_recon_buf;
236
        /* Pointer to the top luma buffer corresponding to the current ctb row*/
237
867k
        pu1_src_top_luma_buf = ps_sao_ctxt->pu1_curr_sao_src_top_luma;
238
239
        /* Pointer to left luma buffer corresponding to the current ctb row*/
240
867k
        pu1_src_left_luma_buf = ps_sao_ctxt->au1_left_luma_scratch;
241
242
        /* Pointer to the top right luma buffer corresponding to the current ctb row*/
243
867k
        pu1_src_top_right_luma = pu1_src_top_luma_buf /*- top_buf_stide*/ + sao_blk_wd;
244
245
        /* Pointer to the bottom left luma buffer corresponding to the current ctb row*/
246
867k
        pu1_src_bot_left_luma =
247
867k
            ps_sao_ctxt->pu1_frm_luma_recon_buf + ctb_size * ps_sao_ctxt->i4_frm_luma_recon_stride -
248
867k
            1 + (ps_sao_ctxt->i4_frm_luma_recon_stride * ps_sao_ctxt->i4_ctb_y * ctb_size) +
249
867k
            (ps_sao_ctxt->i4_ctb_x * ctb_size); /* Bottom left*/
250
251
        /* Back up the top left pixel for (x+1, y+1)th ctb*/
252
867k
        u1_src_top_left_luma = *(pu1_src_top_luma_buf + sao_blk_wd - 1);
253
867k
        pu1_top_left_luma = pu1_src_top_luma_buf - 1;
254
255
867k
        if(SAO_BAND == ps_sao->b3_y_type_idx)
256
0
        {
257
0
            ihevc_sao_band_offset_luma(
258
0
                pu1_src_luma,
259
0
                luma_src_stride,
260
0
                pu1_src_left_luma_buf, /* Pass the pointer to the left luma buffer backed up in the (x-1,y)th ctb */
261
0
                pu1_src_top_luma_buf, /* Pass the ptr to the top luma buf backed up in the (x,y-1)th ctb */
262
0
                pu1_src_top_luma_buf - 1, /* Top left*/
263
0
                ps_sao->b5_y_band_pos,
264
0
                ps_sao->u1_y_offset,
265
0
                sao_blk_wd,
266
0
                sao_blk_ht);
267
268
0
            if((ps_sao_ctxt->i4_ctb_y > 0))
269
0
            {
270
0
                *(pu1_src_top_luma_buf + sao_blk_wd - 1) = u1_src_top_left_luma;
271
0
            }
272
0
        }
273
867k
        else if(ps_sao->b3_y_type_idx >= SAO_EDGE_0_DEG)
274
656k
        {
275
            /*In case of edge offset, 1st and 2nd offsets are always inferred as offsets
276
            * corresponding to EO category 1 and 2 which should be always positive
277
            * And 3rd and 4th offsets are always inferred as offsets corresponding to
278
            * EO category 3 and 4 which should be negative for all the EO classes(or EO typeidx)
279
            */
280
            // clang-format off
281
656k
            ASSERT((ps_sao->u1_y_offset[1] >= 0) && (ps_sao->u1_y_offset[2] >= 0));
282
656k
            ASSERT((ps_sao->u1_y_offset[3] <= 0) && (ps_sao->u1_y_offset[4] <= 0));
283
            // clang-format on
284
285
656k
            ihevce_sao_set_avilability(au1_avail_luma, ps_sao_ctxt, ps_tile_params);
286
287
656k
            ps_sao_ctxt->apf_sao_luma[ps_sao->b3_y_type_idx - 2](
288
656k
                pu1_src_luma,
289
656k
                luma_src_stride,
290
656k
                pu1_src_left_luma_buf, /* Pass the pointer to the left luma buffer backed up in the (x-1,y)th ctb */
291
656k
                pu1_src_top_luma_buf, /* Pass the ptr to the top luma buf backed up in the (x,y-1)th ctb */
292
656k
                pu1_top_left_luma, /* Top left*/
293
656k
                pu1_src_top_right_luma, /* Top right*/
294
656k
                pu1_src_bot_left_luma, /* Bottom left*/
295
656k
                au1_avail_luma,
296
656k
                ps_sao->u1_y_offset,
297
656k
                sao_blk_wd,
298
656k
                sao_blk_ht);
299
300
656k
            if((ps_sao_ctxt->i4_ctb_y > 0))
301
282k
            {
302
282k
                *(pu1_src_top_luma_buf + sao_blk_wd - 1) = u1_src_top_left_luma;
303
282k
            }
304
656k
        }
305
867k
    }
306
307
867k
    if(ps_sao_ctxt->i1_slice_sao_chroma_flag)
308
365k
    {
309
        /*initialize the src pointer to current row*/
310
365k
        chroma_src_stride = ps_sao_ctxt->i4_cur_chroma_recon_stride;
311
365k
        ctb_size = ps_sao_ctxt->i4_ctb_size;
312
313
        /* 1 extra byte in top buf stride for top left of 1st ctb of every row*/
314
        //top_buf_stide = ps_sao_ctxt->u4_ctb_aligned_wd + 2;
315
365k
        ps_sao->u1_cb_offset[0] = 0; /* 0th element is not used  */
316
365k
        ps_sao->u1_cr_offset[0] = 0;
317
365k
        sao_wd_chroma = ps_sao_ctxt->i4_sao_blk_wd;
318
365k
        sao_ht_chroma = ps_sao_ctxt->i4_sao_blk_ht / (!u1_is_422 + 1);
319
320
365k
        pu1_src_chroma = ps_sao_ctxt->pu1_cur_chroma_recon_buf;
321
        /* Pointer to the top luma buffer corresponding to the current ctb row*/
322
365k
        pu1_src_top_chroma_buf = ps_sao_ctxt->pu1_curr_sao_src_top_chroma;
323
        // clang-format off
324
        /* Pointer to left luma buffer corresponding to the current ctb row*/
325
365k
        pu1_src_left_chroma_buf = ps_sao_ctxt->au1_left_chroma_scratch;  //ps_sao_ctxt->au1_sao_src_left_chroma;
326
        // clang-format on
327
        /* Pointer to the top right chroma buffer corresponding to the current ctb row*/
328
365k
        pu1_src_top_right_chroma = pu1_src_top_chroma_buf /*- top_buf_stide*/ + sao_wd_chroma;
329
330
        /* Pointer to the bottom left luma buffer corresponding to the current ctb row*/
331
365k
        pu1_src_bot_left_chroma =
332
365k
            ps_sao_ctxt->pu1_frm_chroma_recon_buf +
333
365k
            (ctb_size >> !u1_is_422) * ps_sao_ctxt->i4_frm_chroma_recon_stride - 2 +
334
365k
            (ps_sao_ctxt->i4_frm_chroma_recon_stride * ps_sao_ctxt->i4_ctb_y *
335
365k
             (ctb_size >> !u1_is_422)) +
336
365k
            (ps_sao_ctxt->i4_ctb_x * ctb_size); /* Bottom left*/
337
338
        /* Back up the top left pixel for (x+1, y+1)th ctb*/
339
365k
        u1_src_top_left_chroma[0] = *(pu1_src_top_chroma_buf + sao_wd_chroma - 2);
340
365k
        u1_src_top_left_chroma[1] = *(pu1_src_top_chroma_buf + sao_wd_chroma - 1);
341
365k
        pu1_top_left_chroma = pu1_src_top_chroma_buf - 2;
342
343
365k
        if(SAO_BAND == ps_sao->b3_cb_type_idx)
344
0
        {
345
0
            ihevc_sao_band_offset_chroma(
346
0
                pu1_src_chroma,
347
0
                chroma_src_stride,
348
0
                pu1_src_left_chroma_buf, /* Pass the pointer to the left luma buffer backed up in the (x-1,y)th ctb */
349
0
                pu1_src_top_chroma_buf, /* Pass the ptr to the top luma buf backed up in the (x,y-1)th ctb */
350
0
                pu1_top_left_chroma, /* Top left*/
351
0
                ps_sao->b5_cb_band_pos,
352
0
                ps_sao->b5_cr_band_pos,
353
0
                ps_sao->u1_cb_offset,
354
0
                ps_sao->u1_cr_offset,
355
0
                sao_wd_chroma,
356
0
                sao_ht_chroma);
357
358
0
            if((ps_sao_ctxt->i4_ctb_y > 0))
359
0
            {
360
0
                *(pu1_src_top_chroma_buf + sao_wd_chroma - 2) = u1_src_top_left_chroma[0];
361
0
                *(pu1_src_top_chroma_buf + sao_wd_chroma - 1) = u1_src_top_left_chroma[1];
362
0
            }
363
0
        }
364
365k
        else if(ps_sao->b3_cb_type_idx >= SAO_EDGE_0_DEG)
365
154k
        {
366
            /*In case of edge offset, 1st and 2nd offsets are always inferred as offsets
367
            * corresponding to EO category 1 and 2 which should be always positive
368
            * And 3rd and 4th offsets are always inferred as offsets corresponding to
369
            * EO category 3 and 4 which should be negative for all the EO classes(or EO typeidx)
370
            */
371
154k
            ASSERT((ps_sao->u1_cb_offset[1] >= 0) && (ps_sao->u1_cb_offset[2] >= 0));
372
154k
            ASSERT((ps_sao->u1_cb_offset[3] <= 0) && (ps_sao->u1_cb_offset[4] <= 0));
373
374
154k
            ASSERT((ps_sao->u1_cr_offset[1] >= 0) && (ps_sao->u1_cr_offset[2] >= 0));
375
154k
            ASSERT((ps_sao->u1_cr_offset[3] <= 0) && (ps_sao->u1_cr_offset[4] <= 0));
376
377
154k
            ihevce_sao_set_avilability(au1_avail_chroma, ps_sao_ctxt, ps_tile_params);
378
379
154k
            ps_sao_ctxt->apf_sao_chroma[ps_sao->b3_cb_type_idx - 2](
380
154k
                pu1_src_chroma,
381
154k
                chroma_src_stride,
382
154k
                pu1_src_left_chroma_buf, /* Pass the pointer to the left luma buffer backed up in the (x-1,y)th ctb */
383
154k
                pu1_src_top_chroma_buf, /* Pass the ptr to the top luma buf backed up in the (x,y-1)th ctb */
384
154k
                pu1_top_left_chroma, /* Top left*/
385
154k
                pu1_src_top_right_chroma, /* Top right*/
386
154k
                pu1_src_bot_left_chroma, /* Bottom left*/
387
154k
                au1_avail_chroma,
388
154k
                ps_sao->u1_cb_offset,
389
154k
                ps_sao->u1_cr_offset,
390
154k
                sao_wd_chroma,
391
154k
                sao_ht_chroma);
392
393
154k
            if((ps_sao_ctxt->i4_ctb_y > 0))
394
74.5k
            {
395
74.5k
                *(pu1_src_top_chroma_buf + sao_wd_chroma - 2) = u1_src_top_left_chroma[0];
396
74.5k
                *(pu1_src_top_chroma_buf + sao_wd_chroma - 1) = u1_src_top_left_chroma[1];
397
74.5k
            }
398
154k
        }
399
365k
    }
400
867k
}
401
402
/**
403
*******************************************************************************
404
*
405
* @brief
406
*   CTB level function to do SAO analysis.
407
*
408
* @par Description:
409
*   For a given CTB, sao analysis is done for both luma and chroma.
410
*
411
*
412
* @param[in]
413
*   ps_sao_ctxt:   Pointer to SAO context
414
*   ps_ctb_enc_loop_out : pointer to ctb level output structure from enc loop
415
*
416
* @returns
417
*
418
* @remarks
419
*  None
420
*
421
* @Assumptions:
422
*   1) Initial Cabac state for current ctb to be sao'ed (i.e (x-1,y-1)th ctb) is assumed to be
423
*      almost same as cabac state of (x,y)th ctb.
424
*   2) Distortion is calculated in spatial domain but lamda used to calculate the cost is
425
*      in freq domain.
426
*******************************************************************************
427
*/
428
void ihevce_sao_analyse(
429
    sao_ctxt_t *ps_sao_ctxt,
430
    ctb_enc_loop_out_t *ps_ctb_enc_loop_out,
431
    UWORD32 *pu4_frame_rdopt_header_bits,
432
    ihevce_tile_params_t *ps_tile_params)
433
125k
{
434
125k
    UWORD8 *pu1_luma_scratch_buf;
435
125k
    UWORD8 *pu1_chroma_scratch_buf;
436
125k
    UWORD8 *pu1_src_luma, *pu1_recon_luma;
437
125k
    UWORD8 *pu1_src_chroma, *pu1_recon_chroma;
438
125k
    WORD32 luma_src_stride, luma_recon_stride, ctb_size, ctb_wd, ctb_ht;
439
125k
    WORD32 chroma_src_stride, chroma_recon_stride;
440
125k
    WORD32 i4_luma_scratch_buf_stride;
441
125k
    WORD32 i4_chroma_scratch_buf_stride;
442
125k
    sao_ctxt_t s_sao_ctxt;
443
125k
    UWORD32 ctb_bits = 0, distortion = 0, curr_cost = 0, best_cost = 0;
444
125k
    LWORD64 i8_cl_ssd_lambda_qf, i8_cl_ssd_lambda_chroma_qf;
445
125k
    WORD32 rdo_cand, num_luma_rdo_cand = 0, num_rdo_cand = 0;
446
125k
    WORD32 curr_buf_idx, best_buf_idx, best_cand_idx;
447
125k
    WORD32 row;
448
125k
    WORD32 edgeidx;
449
125k
    WORD32 acc_error_category[5] = { 0, 0, 0, 0, 0 }, category_count[5] = { 0, 0, 0, 0, 0 };
450
125k
    sao_enc_t s_best_luma_chroma_cand;
451
125k
    WORD32 best_ctb_sao_bits = 0;
452
125k
#if DISABLE_SAO_WHEN_NOISY && !defined(ENC_VER_v2)
453
125k
    UWORD8 u1_force_no_offset =
454
125k
        ps_sao_ctxt
455
125k
            ->ps_ctb_data
456
125k
                [ps_sao_ctxt->i4_ctb_x + ps_sao_ctxt->i4_ctb_data_stride * ps_sao_ctxt->i4_ctb_y]
457
125k
            .s_ctb_noise_params.i4_noise_present;
458
125k
#endif
459
125k
    UWORD8 u1_is_422 = (ps_sao_ctxt->ps_sps->i1_chroma_format_idc == 2);
460
461
125k
    *pu4_frame_rdopt_header_bits = 0;
462
463
125k
    ctb_size = ps_sao_ctxt->i4_ctb_size;
464
125k
    ctb_wd = ps_sao_ctxt->i4_sao_blk_wd;
465
125k
    ctb_ht = ps_sao_ctxt->i4_sao_blk_ht;
466
467
125k
    s_sao_ctxt = ps_sao_ctxt[0];
468
469
    /* Memset the best luma_chroma_cand structure to avoid asserts in debug mode*/
470
125k
    memset(&s_best_luma_chroma_cand, 0, sizeof(sao_enc_t));
471
472
    /* Initialize the pointer and strides for luma buffers*/
473
125k
    pu1_recon_luma = ps_sao_ctxt->pu1_cur_luma_recon_buf;
474
125k
    luma_recon_stride = ps_sao_ctxt->i4_cur_luma_recon_stride;
475
476
125k
    pu1_src_luma = ps_sao_ctxt->pu1_cur_luma_src_buf;
477
125k
    luma_src_stride = ps_sao_ctxt->i4_cur_luma_src_stride;
478
125k
    i4_luma_scratch_buf_stride = SCRATCH_BUF_STRIDE;
479
480
    /* Initialize the pointer and strides for luma buffers*/
481
125k
    pu1_recon_chroma = ps_sao_ctxt->pu1_cur_chroma_recon_buf;
482
125k
    chroma_recon_stride = ps_sao_ctxt->i4_cur_chroma_recon_stride;
483
484
125k
    pu1_src_chroma = ps_sao_ctxt->pu1_cur_chroma_src_buf;
485
125k
    chroma_src_stride = ps_sao_ctxt->i4_cur_chroma_src_stride;
486
125k
    i4_chroma_scratch_buf_stride = SCRATCH_BUF_STRIDE;
487
488
125k
    i8_cl_ssd_lambda_qf = ps_sao_ctxt->i8_cl_ssd_lambda_qf;
489
125k
    i8_cl_ssd_lambda_chroma_qf = ps_sao_ctxt->i8_cl_ssd_lambda_chroma_qf;
490
491
    /*****************************************************/
492
    /********************RDO FOR LUMA CAND****************/
493
    /*****************************************************/
494
495
#if !DISABLE_SAO_WHEN_NOISY
496
    if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_luma_flag)
497
#else
498
125k
    if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_luma_flag && !u1_force_no_offset)
499
125k
#endif
500
125k
    {
501
        /* Candidate for Edge offset SAO*/
502
        /* Following is the convention for curr pixel and
503
        * two neighbouring pixels for 0 deg, 90 deg, 135 deg and 45 deg */
504
        /*
505
        * 0 deg :  a c b     90 deg:  a       135 deg: a          45 deg:     a
506
        *                             c                  c                  c
507
        *                             b                    b              b
508
        */
509
510
        /* 0 deg SAO CAND*/
511
        /* Reset the error and edge count*/
512
752k
        for(edgeidx = 0; edgeidx < 5; edgeidx++)
513
627k
        {
514
627k
            acc_error_category[edgeidx] = 0;
515
627k
            category_count[edgeidx] = 0;
516
627k
        }
517
518
        /* Call the funciton to populate the EO parameter for this ctb for 0 deg EO class*/
519
        // clang-format off
520
125k
        ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_get_luma_eo_sao_params(ps_sao_ctxt, SAO_EDGE_0_DEG,
521
125k
                acc_error_category, category_count);
522
        // clang-format on
523
        // clang-format off
524
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_y_type_idx = SAO_EDGE_0_DEG;
525
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[1] = category_count[0]
526
125k
                ? (CLIP3(acc_error_category[0] / category_count[0], 0, 7))
527
125k
                : 0;
528
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[2] = category_count[1]
529
125k
                ? (CLIP3(acc_error_category[1] / category_count[1], 0, 7))
530
125k
                : 0;
531
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[3] = category_count[3]
532
125k
                ? (CLIP3(acc_error_category[3] / category_count[3], -7, 0))
533
125k
                : 0;
534
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[4] =category_count[4]
535
125k
                ? (CLIP3(acc_error_category[4] / category_count[4], -7, 0))
536
125k
                : 0;
537
        // clang-format on
538
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_y_band_pos = 0;
539
        // clang-format off
540
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cb_type_idx = SAO_NONE;
541
        // clang-format on
542
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[1] = 0;
543
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[2] = 0;
544
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[3] = 0;
545
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[4] = 0;
546
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cb_band_pos = 0;
547
548
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cr_type_idx = SAO_NONE;
549
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[1] = 0;
550
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[2] = 0;
551
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[3] = 0;
552
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[4] = 0;
553
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cr_band_pos = 0;
554
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_left_flag = 0;
555
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_up_flag = 0;
556
557
125k
        num_luma_rdo_cand++;
558
559
        /* 90 degree SAO CAND*/
560
752k
        for(edgeidx = 0; edgeidx < 5; edgeidx++)
561
627k
        {
562
627k
            acc_error_category[edgeidx] = 0;
563
627k
            category_count[edgeidx] = 0;
564
627k
        }
565
566
        /* Call the funciton to populate the EO parameter for this ctb for 90 deg EO class*/
567
        // clang-format off
568
125k
        ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_get_luma_eo_sao_params(ps_sao_ctxt, SAO_EDGE_90_DEG,
569
125k
                acc_error_category, category_count);
570
571
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_y_type_idx = SAO_EDGE_90_DEG;
572
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[1] = category_count[0]
573
125k
                ? (CLIP3(acc_error_category[0] / category_count[0], 0, 7))
574
125k
                : 0;
575
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[2] = category_count[1]
576
125k
                ? (CLIP3(acc_error_category[1] / category_count[1], 0, 7))
577
125k
                : 0;
578
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[3] = category_count[3]
579
125k
                ? (CLIP3(acc_error_category[3] / category_count[3], -7, 0))
580
125k
                : 0;
581
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[4] = category_count[4]
582
125k
                ? (CLIP3(acc_error_category[4] / category_count[4], -7, 0))
583
125k
                : 0;
584
        // clang-format on
585
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_y_band_pos = 0;
586
587
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cb_type_idx = SAO_NONE;
588
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[1] = 0;
589
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[2] = 0;
590
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[3] = 0;
591
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[4] = 0;
592
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cb_band_pos = 0;
593
594
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cr_type_idx = SAO_NONE;
595
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[1] = 0;
596
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[2] = 0;
597
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[3] = 0;
598
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[4] = 0;
599
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cr_band_pos = 0;
600
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_left_flag = 0;
601
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_up_flag = 0;
602
603
125k
        num_luma_rdo_cand++;
604
605
        /* 135 degree SAO CAND*/
606
752k
        for(edgeidx = 0; edgeidx < 5; edgeidx++)
607
627k
        {
608
627k
            acc_error_category[edgeidx] = 0;
609
627k
            category_count[edgeidx] = 0;
610
627k
        }
611
612
        /* Call the funciton to populate the EO parameter for this ctb for 135 deg EO class*/
613
        // clang-format off
614
125k
        ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_get_luma_eo_sao_params(ps_sao_ctxt, SAO_EDGE_135_DEG,
615
125k
                acc_error_category, category_count);
616
617
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_y_type_idx = SAO_EDGE_135_DEG;
618
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[1] = category_count[0]
619
125k
                ? (CLIP3(acc_error_category[0] / category_count[0], 0, 7))
620
125k
                : 0;
621
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[2] = category_count[1]
622
125k
                ? (CLIP3(acc_error_category[1] / category_count[1], 0, 7))
623
125k
                : 0;
624
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[3] = category_count[3]
625
125k
                ? (CLIP3(acc_error_category[3] / category_count[3], -7, 0))
626
125k
                : 0;
627
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[4] = category_count[4]
628
125k
                ? (CLIP3(acc_error_category[4] / category_count[4], -7, 0))
629
125k
                : 0;
630
        // clang-format on
631
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_y_band_pos = 0;
632
633
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cb_type_idx = SAO_NONE;
634
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[1] = 0;
635
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[2] = 0;
636
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[3] = 0;
637
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[4] = 0;
638
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cb_band_pos = 0;
639
640
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cr_type_idx = SAO_NONE;
641
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[1] = 0;
642
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[2] = 0;
643
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[3] = 0;
644
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[4] = 0;
645
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cr_band_pos = 0;
646
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_left_flag = 0;
647
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_up_flag = 0;
648
649
125k
        num_luma_rdo_cand++;
650
651
        /* 45 degree SAO CAND*/
652
752k
        for(edgeidx = 0; edgeidx < 5; edgeidx++)
653
627k
        {
654
627k
            acc_error_category[edgeidx] = 0;
655
627k
            category_count[edgeidx] = 0;
656
627k
        }
657
658
        /* Call the funciton to populate the EO parameter for this ctb for 45 deg EO class*/
659
        // clang-format off
660
125k
        ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_get_luma_eo_sao_params(ps_sao_ctxt, SAO_EDGE_45_DEG,
661
125k
                acc_error_category, category_count);
662
663
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_y_type_idx = SAO_EDGE_45_DEG;
664
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[1] = category_count[0]
665
125k
                ? (CLIP3(acc_error_category[0] / category_count[0], 0, 7))
666
125k
                : 0;
667
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[2] = category_count[1]
668
125k
                ? (CLIP3(acc_error_category[1] / category_count[1], 0, 7))
669
125k
                : 0;
670
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[3] = category_count[3]
671
125k
                ? (CLIP3(acc_error_category[3] / category_count[3], -7, 0))
672
125k
                : 0;
673
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_y_offset[4] = category_count[4]
674
125k
                ? (CLIP3(acc_error_category[4] / category_count[4], -7, 0))
675
125k
                : 0;
676
        // clang-format on
677
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_y_band_pos = 0;
678
679
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cb_type_idx = SAO_NONE;
680
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[1] = 0;
681
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[2] = 0;
682
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[3] = 0;
683
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cb_offset[4] = 0;
684
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cb_band_pos = 0;
685
686
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b3_cr_type_idx = SAO_NONE;
687
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[1] = 0;
688
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[2] = 0;
689
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[3] = 0;
690
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].u1_cr_offset[4] = 0;
691
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b5_cr_band_pos = 0;
692
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_left_flag = 0;
693
125k
        ps_sao_ctxt->as_sao_rd_cand[num_luma_rdo_cand].b1_sao_merge_up_flag = 0;
694
695
125k
        num_luma_rdo_cand++;
696
697
        /* First cand will be best cand after 1st iteration*/
698
125k
        curr_buf_idx = 0;
699
125k
        best_buf_idx = 1;
700
125k
        best_cost = 0xFFFFFFFF;
701
125k
        best_cand_idx = 0;
702
703
        /*Back up the top pixels for (x,y+1)th ctb*/
704
125k
        if(!ps_sao_ctxt->i4_is_last_ctb_row)
705
51.9k
        {
706
51.9k
            memcpy(
707
51.9k
                ps_sao_ctxt->pu1_curr_sao_src_top_luma + ps_sao_ctxt->i4_frm_top_luma_buf_stride,
708
51.9k
                pu1_recon_luma + luma_recon_stride * (ctb_size - 1),
709
51.9k
                ps_sao_ctxt->i4_sao_blk_wd);
710
51.9k
        }
711
712
627k
        for(rdo_cand = 0; rdo_cand < num_luma_rdo_cand; rdo_cand++)
713
501k
        {
714
501k
            s_sao_ctxt.ps_sao = &ps_sao_ctxt->as_sao_rd_cand[rdo_cand];
715
716
            /* This memcpy is required because cabac uses parameters from this structure
717
            * to evaluate bits and this structure ptr is sent to cabac through
718
            * "ihevce_cabac_rdo_encode_sao" function
719
            */
720
501k
            memcpy(&ps_ctb_enc_loop_out->s_sao, s_sao_ctxt.ps_sao, sizeof(sao_enc_t));
721
722
            /* Copy the left pixels to the scratch buffer for evry rdo cand because its
723
            overwritten by the sao leaf level function for next ctb*/
724
501k
            memcpy(
725
501k
                s_sao_ctxt.au1_left_luma_scratch,
726
501k
                ps_sao_ctxt->au1_sao_src_left_luma,
727
501k
                ps_sao_ctxt->i4_sao_blk_ht);
728
729
            /* Copy the top and top left pixels to the scratch buffer for evry rdo cand because its
730
            overwritten by the sao leaf level function for next ctb*/
731
501k
            memcpy(
732
501k
                s_sao_ctxt.au1_top_luma_scratch,
733
501k
                ps_sao_ctxt->pu1_curr_sao_src_top_luma - 1,
734
501k
                ps_sao_ctxt->i4_sao_blk_wd + 2);
735
501k
            s_sao_ctxt.pu1_curr_sao_src_top_luma = s_sao_ctxt.au1_top_luma_scratch + 1;
736
737
501k
            pu1_luma_scratch_buf = ps_sao_ctxt->au1_sao_luma_scratch[curr_buf_idx];
738
739
501k
            ASSERT(
740
501k
                (abs(s_sao_ctxt.ps_sao->u1_y_offset[1]) <= 7) &&
741
501k
                (abs(s_sao_ctxt.ps_sao->u1_y_offset[2]) <= 7) &&
742
501k
                (abs(s_sao_ctxt.ps_sao->u1_y_offset[3]) <= 7) &&
743
501k
                (abs(s_sao_ctxt.ps_sao->u1_y_offset[4]) <= 7));
744
501k
            ASSERT(
745
501k
                (abs(s_sao_ctxt.ps_sao->u1_cb_offset[1]) <= 7) &&
746
501k
                (abs(s_sao_ctxt.ps_sao->u1_cb_offset[2]) <= 7) &&
747
501k
                (abs(s_sao_ctxt.ps_sao->u1_cb_offset[3]) <= 7) &&
748
501k
                (abs(s_sao_ctxt.ps_sao->u1_cb_offset[4]) <= 7));
749
501k
            ASSERT(
750
501k
                (abs(s_sao_ctxt.ps_sao->u1_cr_offset[1]) <= 7) &&
751
501k
                (abs(s_sao_ctxt.ps_sao->u1_cr_offset[2]) <= 7) &&
752
501k
                (abs(s_sao_ctxt.ps_sao->u1_cr_offset[3]) <= 7) &&
753
501k
                (abs(s_sao_ctxt.ps_sao->u1_cr_offset[4]) <= 7));
754
501k
            ASSERT(
755
501k
                (s_sao_ctxt.ps_sao->b5_y_band_pos <= 28) &&
756
501k
                (s_sao_ctxt.ps_sao->b5_cb_band_pos <= 28) &&
757
501k
                (s_sao_ctxt.ps_sao->b5_cr_band_pos <= 28));
758
759
            /* Copy the deblocked recon data to scratch buffer to do sao*/
760
761
501k
            ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_copy_2d(
762
501k
                pu1_luma_scratch_buf,
763
501k
                i4_luma_scratch_buf_stride,
764
501k
                pu1_recon_luma,
765
501k
                luma_recon_stride,
766
501k
                SCRATCH_BUF_STRIDE,
767
501k
                ctb_ht + 1);
768
769
501k
            s_sao_ctxt.pu1_cur_luma_recon_buf = pu1_luma_scratch_buf;
770
501k
            s_sao_ctxt.i4_cur_luma_recon_stride = i4_luma_scratch_buf_stride;
771
772
501k
            s_sao_ctxt.i1_slice_sao_luma_flag = s_sao_ctxt.ps_slice_hdr->i1_slice_sao_luma_flag;
773
501k
            s_sao_ctxt.i1_slice_sao_chroma_flag = 0;
774
775
501k
            ihevce_sao_ctb(&s_sao_ctxt, ps_tile_params);
776
777
            /* Calculate the distortion between sao'ed ctb and original src ctb*/
778
            // clang-format off
779
501k
            distortion =
780
501k
                ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_ssd_calculator(pu1_src_luma,
781
501k
                        s_sao_ctxt.pu1_cur_luma_recon_buf, luma_src_stride,
782
501k
                        s_sao_ctxt.i4_cur_luma_recon_stride, ctb_wd, ctb_ht, NULL_PLANE);
783
            // clang-format on
784
785
501k
            ps_sao_ctxt->ps_rdopt_entropy_ctxt->i4_curr_buf_idx = curr_buf_idx;
786
501k
            ctb_bits = ihevce_cabac_rdo_encode_sao(
787
501k
                ps_sao_ctxt->ps_rdopt_entropy_ctxt, ps_ctb_enc_loop_out);
788
789
            /* Calculate the cost as D+(lamda)*R   */
790
501k
            curr_cost = distortion +
791
501k
                        COMPUTE_RATE_COST_CLIP30(ctb_bits, i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
792
793
501k
            if(curr_cost < best_cost)
794
173k
            {
795
173k
                best_cost = curr_cost;
796
173k
                best_buf_idx = ps_sao_ctxt->ps_rdopt_entropy_ctxt->i4_curr_buf_idx;
797
173k
                best_cand_idx = rdo_cand;
798
173k
                curr_buf_idx = !curr_buf_idx;
799
173k
            }
800
501k
        }
801
802
        /* Copy the sao parameters of the best luma cand into the luma_chroma cnad structure for next stage of RDO
803
        * between luma_chroma combined cand, NO SAO cand, LEFT and TOP merge cand
804
        */
805
125k
        s_best_luma_chroma_cand.b3_y_type_idx =
806
125k
            ps_sao_ctxt->as_sao_rd_cand[best_cand_idx].b3_y_type_idx;
807
125k
        s_best_luma_chroma_cand.u1_y_offset[1] =
808
125k
            ps_sao_ctxt->as_sao_rd_cand[best_cand_idx].u1_y_offset[1];
809
125k
        s_best_luma_chroma_cand.u1_y_offset[2] =
810
125k
            ps_sao_ctxt->as_sao_rd_cand[best_cand_idx].u1_y_offset[2];
811
125k
        s_best_luma_chroma_cand.u1_y_offset[3] =
812
125k
            ps_sao_ctxt->as_sao_rd_cand[best_cand_idx].u1_y_offset[3];
813
125k
        s_best_luma_chroma_cand.u1_y_offset[4] =
814
125k
            ps_sao_ctxt->as_sao_rd_cand[best_cand_idx].u1_y_offset[4];
815
125k
        s_best_luma_chroma_cand.b5_y_band_pos =
816
125k
            ps_sao_ctxt->as_sao_rd_cand[best_cand_idx].b5_y_band_pos;
817
125k
    }
818
0
    else
819
0
    {
820
        /*Back up the top pixels for (x,y+1)th ctb*/
821
0
        if(!ps_sao_ctxt->i4_is_last_ctb_row)
822
0
        {
823
0
            memcpy(
824
0
                ps_sao_ctxt->pu1_curr_sao_src_top_luma + ps_sao_ctxt->i4_frm_top_luma_buf_stride,
825
0
                pu1_recon_luma + luma_recon_stride * (ctb_size - 1),
826
0
                ps_sao_ctxt->i4_sao_blk_wd);
827
0
        }
828
829
0
        s_best_luma_chroma_cand.b3_y_type_idx = SAO_NONE;
830
0
        s_best_luma_chroma_cand.u1_y_offset[1] = 0;
831
0
        s_best_luma_chroma_cand.u1_y_offset[2] = 0;
832
0
        s_best_luma_chroma_cand.u1_y_offset[3] = 0;
833
0
        s_best_luma_chroma_cand.u1_y_offset[4] = 0;
834
0
        s_best_luma_chroma_cand.b5_y_band_pos = 0;
835
0
        s_best_luma_chroma_cand.b1_sao_merge_left_flag = 0;
836
0
        s_best_luma_chroma_cand.b1_sao_merge_up_flag = 0;
837
838
0
        s_best_luma_chroma_cand.b3_cb_type_idx = SAO_NONE;
839
0
        s_best_luma_chroma_cand.u1_cb_offset[1] = 0;
840
0
        s_best_luma_chroma_cand.u1_cb_offset[2] = 0;
841
0
        s_best_luma_chroma_cand.u1_cb_offset[3] = 0;
842
0
        s_best_luma_chroma_cand.u1_cb_offset[4] = 0;
843
0
        s_best_luma_chroma_cand.b5_cb_band_pos = 0;
844
845
0
        s_best_luma_chroma_cand.b3_cr_type_idx = SAO_NONE;
846
0
        s_best_luma_chroma_cand.u1_cr_offset[1] = 0;
847
0
        s_best_luma_chroma_cand.u1_cr_offset[2] = 0;
848
0
        s_best_luma_chroma_cand.u1_cr_offset[3] = 0;
849
0
        s_best_luma_chroma_cand.u1_cr_offset[4] = 0;
850
0
        s_best_luma_chroma_cand.b5_cr_band_pos = 0;
851
0
    }
852
    /*****************************************************/
853
    /********************RDO FOR CHROMA CAND**************/
854
    /*****************************************************/
855
#if !DISABLE_SAO_WHEN_NOISY
856
    if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_chroma_flag)
857
#else
858
125k
    if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_chroma_flag && !u1_force_no_offset)
859
125k
#endif
860
125k
    {
861
        /*Back up the top pixels for (x,y+1)th ctb*/
862
125k
        if(!ps_sao_ctxt->i4_is_last_ctb_row)
863
51.9k
        {
864
51.9k
            memcpy(
865
51.9k
                ps_sao_ctxt->pu1_curr_sao_src_top_chroma +
866
51.9k
                    ps_sao_ctxt->i4_frm_top_chroma_buf_stride,
867
51.9k
                pu1_recon_chroma + chroma_recon_stride * ((ctb_size >> !u1_is_422) - 1),
868
51.9k
                ps_sao_ctxt->i4_sao_blk_wd);
869
51.9k
        }
870
871
        /* Reset the error and edge count*/
872
752k
        for(edgeidx = 0; edgeidx < 5; edgeidx++)
873
627k
        {
874
627k
            acc_error_category[edgeidx] = 0;
875
627k
            category_count[edgeidx] = 0;
876
627k
        }
877
        // clang-format off
878
125k
        ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_get_chroma_eo_sao_params(ps_sao_ctxt,
879
125k
                s_best_luma_chroma_cand.b3_y_type_idx, acc_error_category,
880
125k
                category_count);
881
        // clang-format on
882
883
        /* Copy the sao parameters of the best luma cand into the luma_chroma cnad structure for next stage of RDO
884
        * between luma_chroma combined cand, NO SAO cand, LEFT and TOP merge cand
885
        */
886
        // clang-format off
887
125k
        s_best_luma_chroma_cand.b3_cb_type_idx = s_best_luma_chroma_cand.b3_y_type_idx;
888
125k
        s_best_luma_chroma_cand.u1_cb_offset[1] = category_count[0]
889
125k
                ? (CLIP3(acc_error_category[0] / category_count[0], 0, 7))
890
125k
                : 0;
891
125k
        s_best_luma_chroma_cand.u1_cb_offset[2] = category_count[1]
892
125k
                ? (CLIP3(acc_error_category[1] / category_count[1], 0, 7))
893
125k
                : 0;
894
125k
        s_best_luma_chroma_cand.u1_cb_offset[3] = category_count[3]
895
125k
                ? (CLIP3(acc_error_category[3] / category_count[3], -7, 0))
896
125k
                : 0;
897
125k
        s_best_luma_chroma_cand.u1_cb_offset[4] = category_count[4]
898
125k
                ? (CLIP3(acc_error_category[4] / category_count[4], -7, 0))
899
125k
                : 0;
900
125k
        s_best_luma_chroma_cand.b5_cb_band_pos = 0;
901
902
125k
        s_best_luma_chroma_cand.b3_cr_type_idx = s_best_luma_chroma_cand.b3_y_type_idx;
903
125k
        s_best_luma_chroma_cand.u1_cr_offset[1] = category_count[0]
904
125k
                ? (CLIP3(acc_error_category[0] / category_count[0], 0, 7))
905
125k
                : 0;
906
125k
        s_best_luma_chroma_cand.u1_cr_offset[2] = category_count[1]
907
125k
                ? (CLIP3(acc_error_category[1] / category_count[1], 0, 7))
908
125k
                : 0;
909
125k
        s_best_luma_chroma_cand.u1_cr_offset[3] = category_count[3]
910
125k
                ? (CLIP3(acc_error_category[3] / category_count[3], -7, 0))
911
125k
                : 0;
912
125k
        s_best_luma_chroma_cand.u1_cr_offset[4] = category_count[4]
913
125k
                ? (CLIP3(acc_error_category[4] / category_count[4], -7, 0))
914
125k
                : 0;
915
        // clang-format on
916
125k
        s_best_luma_chroma_cand.b5_cr_band_pos = 0;
917
125k
    }
918
0
    else
919
0
    {
920
        /*Back up the top pixels for (x,y+1)th ctb*/
921
0
        if(!ps_sao_ctxt->i4_is_last_ctb_row)
922
0
        {
923
0
            memcpy(
924
0
                ps_sao_ctxt->pu1_curr_sao_src_top_chroma +
925
0
                    ps_sao_ctxt->i4_frm_top_chroma_buf_stride,
926
0
                pu1_recon_chroma + chroma_recon_stride * ((ctb_size >> !u1_is_422) - 1),
927
0
                ps_sao_ctxt->i4_sao_blk_wd);
928
0
        }
929
930
0
        s_best_luma_chroma_cand.b3_cb_type_idx = SAO_NONE;
931
0
        s_best_luma_chroma_cand.u1_cb_offset[1] = 0;
932
0
        s_best_luma_chroma_cand.u1_cb_offset[2] = 0;
933
0
        s_best_luma_chroma_cand.u1_cb_offset[3] = 0;
934
0
        s_best_luma_chroma_cand.u1_cb_offset[4] = 0;
935
0
        s_best_luma_chroma_cand.b5_cb_band_pos = 0;
936
937
0
        s_best_luma_chroma_cand.b3_cr_type_idx = SAO_NONE;
938
0
        s_best_luma_chroma_cand.u1_cr_offset[1] = 0;
939
0
        s_best_luma_chroma_cand.u1_cr_offset[2] = 0;
940
0
        s_best_luma_chroma_cand.u1_cr_offset[3] = 0;
941
0
        s_best_luma_chroma_cand.u1_cr_offset[4] = 0;
942
0
        s_best_luma_chroma_cand.b5_cr_band_pos = 0;
943
944
0
        s_best_luma_chroma_cand.b1_sao_merge_left_flag = 0;
945
0
        s_best_luma_chroma_cand.b1_sao_merge_up_flag = 0;
946
0
    }
947
948
125k
    s_best_luma_chroma_cand.b1_sao_merge_left_flag = 0;
949
125k
    s_best_luma_chroma_cand.b1_sao_merge_up_flag = 0;
950
951
    /*****************************************************/
952
    /**RDO for Best Luma - Chroma combined, No SAO,*******/
953
    /*************Left merge and Top merge****************/
954
    /*****************************************************/
955
956
    /* No SAO cand*/
957
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_left_flag = 0;
958
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_up_flag = 0;
959
960
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b3_y_type_idx = SAO_NONE;
961
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_y_offset[1] = 0;
962
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_y_offset[2] = 0;
963
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_y_offset[3] = 0;
964
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_y_offset[4] = 0;
965
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b5_y_band_pos = 0;
966
967
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b3_cb_type_idx = SAO_NONE;
968
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cb_offset[1] = 0;
969
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cb_offset[2] = 0;
970
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cb_offset[3] = 0;
971
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cb_offset[4] = 0;
972
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b5_cb_band_pos = 0;
973
974
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b3_cr_type_idx = SAO_NONE;
975
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cr_offset[1] = 0;
976
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cr_offset[2] = 0;
977
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cr_offset[3] = 0;
978
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].u1_cr_offset[4] = 0;
979
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b5_cr_band_pos = 0;
980
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_left_flag = 0;
981
125k
    ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_up_flag = 0;
982
983
125k
    num_rdo_cand++;
984
985
    /* SAO_note_01: If the CTB lies on a tile or a slice boundary, then
986
    the standard mandates that the merge candidates must be set to unavailable.
987
    Hence, check for tile boundary condition by reading
988
    s_ctb_nbr_avail_flags.u1_left_avail rather than frame position of CTB.
989
    A special case: Merge-candidates should be available at dependent-slices boundaries.
990
    Search for <SAO_note_01> in workspace to know more */
991
992
#if !DISABLE_SAO_WHEN_NOISY
993
    if(1)
994
#else
995
125k
    if(!u1_force_no_offset)
996
125k
#endif
997
125k
    {
998
        /* Merge left cand*/
999
125k
        if(ps_ctb_enc_loop_out->s_ctb_nbr_avail_flags.u1_left_avail)
1000
62.4k
        {
1001
62.4k
            memcpy(
1002
62.4k
                &ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand],
1003
62.4k
                &ps_sao_ctxt->s_left_ctb_sao,
1004
62.4k
                sizeof(sao_enc_t));
1005
62.4k
            ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_left_flag = 1;
1006
62.4k
            ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_up_flag = 0;
1007
62.4k
            num_rdo_cand++;
1008
62.4k
        }
1009
1010
        /* Merge top cand*/
1011
125k
        if(ps_ctb_enc_loop_out->s_ctb_nbr_avail_flags.u1_top_avail)
1012
51.9k
        {
1013
51.9k
            memcpy(
1014
51.9k
                &ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand],
1015
51.9k
                (ps_sao_ctxt->ps_top_ctb_sao - ps_sao_ctxt->u4_num_ctbs_horz),
1016
51.9k
                sizeof(sao_enc_t));
1017
51.9k
            ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_left_flag = 0;
1018
51.9k
            ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand].b1_sao_merge_up_flag = 1;
1019
51.9k
            num_rdo_cand++;
1020
51.9k
        }
1021
1022
        /* Best luma-chroma candidate*/
1023
125k
        memcpy(
1024
125k
            &ps_sao_ctxt->as_sao_rd_cand[num_rdo_cand],
1025
125k
            &s_best_luma_chroma_cand,
1026
125k
            sizeof(sao_enc_t));
1027
125k
        num_rdo_cand++;
1028
125k
    }
1029
1030
125k
    {
1031
125k
        UWORD32 luma_distortion = 0, chroma_distortion = 0;
1032
        /* First cand will be best cand after 1st iteration*/
1033
125k
        curr_buf_idx = 0;
1034
125k
        best_buf_idx = 1;
1035
125k
        best_cost = 0xFFFFFFFF;
1036
125k
        best_cand_idx = 0;
1037
1038
490k
        for(rdo_cand = 0; rdo_cand < num_rdo_cand; rdo_cand++)
1039
365k
        {
1040
365k
            s_sao_ctxt.ps_sao = &ps_sao_ctxt->as_sao_rd_cand[rdo_cand];
1041
1042
365k
            distortion = 0;
1043
1044
            /* This memcpy is required because cabac uses parameters from this structure
1045
            * to evaluate bits and this structure ptr is sent to cabac through
1046
            * "ihevce_cabac_rdo_encode_sao" function
1047
            */
1048
365k
            memcpy(&ps_ctb_enc_loop_out->s_sao, s_sao_ctxt.ps_sao, sizeof(sao_enc_t));
1049
1050
365k
            if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_luma_flag)
1051
365k
            {
1052
                /* Copy the left pixels to the scratch buffer for evry rdo cand because its
1053
                overwritten by the sao leaf level function for next ctb*/
1054
365k
                memcpy(
1055
365k
                    s_sao_ctxt.au1_left_luma_scratch,
1056
365k
                    ps_sao_ctxt->au1_sao_src_left_luma,
1057
365k
                    ps_sao_ctxt->i4_sao_blk_ht);
1058
1059
                /* Copy the top and top left pixels to the scratch buffer for evry rdo cand because its
1060
                overwritten by the sao leaf level function for next ctb*/
1061
365k
                memcpy(
1062
365k
                    s_sao_ctxt.au1_top_luma_scratch,
1063
365k
                    ps_sao_ctxt->pu1_curr_sao_src_top_luma - 1,
1064
365k
                    ps_sao_ctxt->i4_sao_blk_wd + 2);
1065
365k
                s_sao_ctxt.pu1_curr_sao_src_top_luma = s_sao_ctxt.au1_top_luma_scratch + 1;
1066
1067
365k
                pu1_luma_scratch_buf = ps_sao_ctxt->au1_sao_luma_scratch[curr_buf_idx];
1068
1069
                /* Copy the deblocked recon data to scratch buffer to do sao*/
1070
1071
365k
                ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_copy_2d(
1072
365k
                    pu1_luma_scratch_buf,
1073
365k
                    i4_luma_scratch_buf_stride,
1074
365k
                    pu1_recon_luma,
1075
365k
                    luma_recon_stride,
1076
365k
                    SCRATCH_BUF_STRIDE,
1077
365k
                    ctb_ht + 1);
1078
365k
                s_sao_ctxt.pu1_cur_luma_recon_buf = pu1_luma_scratch_buf;
1079
365k
                s_sao_ctxt.i4_cur_luma_recon_stride = i4_luma_scratch_buf_stride;
1080
1081
365k
                ASSERT(
1082
365k
                    (abs(s_sao_ctxt.ps_sao->u1_y_offset[1]) <= 7) &&
1083
365k
                    (abs(s_sao_ctxt.ps_sao->u1_y_offset[2]) <= 7) &&
1084
365k
                    (abs(s_sao_ctxt.ps_sao->u1_y_offset[3]) <= 7) &&
1085
365k
                    (abs(s_sao_ctxt.ps_sao->u1_y_offset[4]) <= 7));
1086
365k
            }
1087
365k
            if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_chroma_flag)
1088
365k
            {
1089
                /* Copy the left pixels to the scratch buffer for evry rdo cand because its
1090
                overwritten by the sao leaf level function for next ctb*/
1091
365k
                memcpy(
1092
365k
                    s_sao_ctxt.au1_left_chroma_scratch,
1093
365k
                    ps_sao_ctxt->au1_sao_src_left_chroma,
1094
365k
                    (ps_sao_ctxt->i4_sao_blk_ht >> !u1_is_422) * 2);
1095
1096
                /* Copy the top and top left pixels to the scratch buffer for evry rdo cand because its
1097
                overwritten by the sao leaf level function for next ctb*/
1098
365k
                memcpy(
1099
365k
                    s_sao_ctxt.au1_top_chroma_scratch,
1100
365k
                    ps_sao_ctxt->pu1_curr_sao_src_top_chroma - 2,
1101
365k
                    ps_sao_ctxt->i4_sao_blk_wd + 4);
1102
1103
365k
                s_sao_ctxt.pu1_curr_sao_src_top_chroma = s_sao_ctxt.au1_top_chroma_scratch + 2;
1104
1105
365k
                pu1_chroma_scratch_buf = ps_sao_ctxt->au1_sao_chroma_scratch[curr_buf_idx];
1106
1107
                /* Copy the deblocked recon data to scratch buffer to do sao*/
1108
1109
365k
                ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_copy_2d(
1110
365k
                    pu1_chroma_scratch_buf,
1111
365k
                    i4_chroma_scratch_buf_stride,
1112
365k
                    pu1_recon_chroma,
1113
365k
                    chroma_recon_stride,
1114
365k
                    SCRATCH_BUF_STRIDE,
1115
365k
                    (ctb_ht >> !u1_is_422) + 1);
1116
1117
365k
                s_sao_ctxt.pu1_cur_chroma_recon_buf = pu1_chroma_scratch_buf;
1118
365k
                s_sao_ctxt.i4_cur_chroma_recon_stride = i4_chroma_scratch_buf_stride;
1119
1120
365k
                ASSERT(
1121
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cb_offset[1]) <= 7) &&
1122
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cb_offset[2]) <= 7) &&
1123
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cb_offset[3]) <= 7) &&
1124
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cb_offset[4]) <= 7));
1125
365k
                ASSERT(
1126
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cr_offset[1]) <= 7) &&
1127
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cr_offset[2]) <= 7) &&
1128
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cr_offset[3]) <= 7) &&
1129
365k
                    (abs(s_sao_ctxt.ps_sao->u1_cr_offset[4]) <= 7));
1130
365k
            }
1131
1132
365k
            ASSERT(
1133
365k
                (s_sao_ctxt.ps_sao->b5_y_band_pos <= 28) &&
1134
365k
                (s_sao_ctxt.ps_sao->b5_cb_band_pos <= 28) &&
1135
365k
                (s_sao_ctxt.ps_sao->b5_cr_band_pos <= 28));
1136
1137
365k
            s_sao_ctxt.i1_slice_sao_luma_flag = s_sao_ctxt.ps_slice_hdr->i1_slice_sao_luma_flag;
1138
365k
            s_sao_ctxt.i1_slice_sao_chroma_flag = s_sao_ctxt.ps_slice_hdr->i1_slice_sao_chroma_flag;
1139
1140
365k
            ihevce_sao_ctb(&s_sao_ctxt, ps_tile_params);
1141
1142
365k
            if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_luma_flag)
1143
365k
            {  // clang-format off
1144
365k
                luma_distortion =
1145
365k
                    ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_ssd_calculator(pu1_src_luma,
1146
365k
                            s_sao_ctxt.pu1_cur_luma_recon_buf, luma_src_stride,
1147
365k
                            s_sao_ctxt.i4_cur_luma_recon_stride, ctb_wd,
1148
365k
                            ctb_ht,
1149
365k
                            NULL_PLANE);
1150
365k
            }  // clang-format on
1151
1152
365k
            if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_chroma_flag)
1153
365k
            {  // clang-format off
1154
365k
                chroma_distortion =
1155
365k
                    ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_ssd_calculator(pu1_src_chroma,
1156
365k
                            s_sao_ctxt.pu1_cur_chroma_recon_buf,
1157
365k
                            chroma_src_stride,
1158
365k
                            s_sao_ctxt.i4_cur_chroma_recon_stride, ctb_wd,
1159
365k
                            (ctb_ht >> !u1_is_422),
1160
365k
                            NULL_PLANE);
1161
365k
            }  // clang-format on
1162
1163
            /*chroma distortion is added after correction because of lambda difference*/
1164
365k
            distortion =
1165
365k
                luma_distortion +
1166
365k
                (UWORD32)(chroma_distortion * (i8_cl_ssd_lambda_qf / i8_cl_ssd_lambda_chroma_qf));
1167
1168
365k
            ps_sao_ctxt->ps_rdopt_entropy_ctxt->i4_curr_buf_idx = curr_buf_idx;
1169
365k
            ctb_bits = ihevce_cabac_rdo_encode_sao(
1170
365k
                ps_sao_ctxt->ps_rdopt_entropy_ctxt, ps_ctb_enc_loop_out);
1171
1172
            /* Calculate the cost as D+(lamda)*R   */
1173
365k
            curr_cost = distortion +
1174
365k
                        COMPUTE_RATE_COST_CLIP30(ctb_bits, i8_cl_ssd_lambda_qf, LAMBDA_Q_SHIFT);
1175
1176
365k
            if(curr_cost < best_cost)
1177
188k
            {
1178
188k
                best_ctb_sao_bits = ctb_bits;
1179
188k
                best_cost = curr_cost;
1180
188k
                best_buf_idx = ps_sao_ctxt->ps_rdopt_entropy_ctxt->i4_curr_buf_idx;
1181
188k
                best_cand_idx = rdo_cand;
1182
188k
                curr_buf_idx = !curr_buf_idx;
1183
188k
            }
1184
365k
        }
1185
        /*Adding sao bits to header bits*/
1186
125k
        *pu4_frame_rdopt_header_bits = best_ctb_sao_bits;
1187
1188
125k
        ihevce_update_best_sao_cabac_state(ps_sao_ctxt->ps_rdopt_entropy_ctxt, best_buf_idx);
1189
1190
        /* store the sao parameters of curr ctb for top merge and left merge*/
1191
125k
        memcpy(
1192
125k
            ps_sao_ctxt->ps_top_ctb_sao,
1193
125k
            &ps_sao_ctxt->as_sao_rd_cand[best_cand_idx],
1194
125k
            sizeof(sao_enc_t));
1195
125k
        memcpy(
1196
125k
            &ps_sao_ctxt->s_left_ctb_sao,
1197
125k
            &ps_sao_ctxt->as_sao_rd_cand[best_cand_idx],
1198
125k
            sizeof(sao_enc_t));
1199
1200
        /* Copy the sao parameters of winning candidate into the structure which will be sent to entropy thrd*/
1201
125k
        memcpy(
1202
125k
            &ps_ctb_enc_loop_out->s_sao,
1203
125k
            &ps_sao_ctxt->as_sao_rd_cand[best_cand_idx],
1204
125k
            sizeof(sao_enc_t));
1205
1206
125k
        if(!ps_sao_ctxt->i4_is_last_ctb_col)
1207
62.4k
        {
1208
            /* Update left luma buffer for next ctb */
1209
3.85M
            for(row = 0; row < ps_sao_ctxt->i4_sao_blk_ht; row++)
1210
3.79M
            {
1211
3.79M
                ps_sao_ctxt->au1_sao_src_left_luma[row] =
1212
3.79M
                    ps_sao_ctxt->pu1_cur_luma_recon_buf
1213
3.79M
                        [row * ps_sao_ctxt->i4_cur_luma_recon_stride +
1214
3.79M
                         (ps_sao_ctxt->i4_sao_blk_wd - 1)];
1215
3.79M
            }
1216
62.4k
        }
1217
1218
125k
        if(!ps_sao_ctxt->i4_is_last_ctb_col)
1219
62.4k
        {
1220
            /* Update left chroma buffer for next ctb */
1221
1.95M
            for(row = 0; row < (ps_sao_ctxt->i4_sao_blk_ht >> 1); row++)
1222
1.89M
            {
1223
1.89M
                *(UWORD16 *)(ps_sao_ctxt->au1_sao_src_left_chroma + row * 2) =
1224
1.89M
                    *(UWORD16 *)(ps_sao_ctxt->pu1_cur_chroma_recon_buf +
1225
1.89M
                                 row * ps_sao_ctxt->i4_cur_chroma_recon_stride +
1226
1.89M
                                 (ps_sao_ctxt->i4_sao_blk_wd - 2));
1227
1.89M
            }
1228
62.4k
        }
1229
1230
125k
        if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_luma_flag)
1231
125k
        {
1232
            /* Copy the sao'ed output of the best candidate to the recon buffer*/
1233
1234
125k
            ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_copy_2d(
1235
125k
                ps_sao_ctxt->pu1_cur_luma_recon_buf,
1236
125k
                ps_sao_ctxt->i4_cur_luma_recon_stride,
1237
125k
                ps_sao_ctxt->au1_sao_luma_scratch[best_buf_idx],
1238
125k
                i4_luma_scratch_buf_stride,
1239
125k
                ctb_wd,
1240
125k
                ctb_ht);
1241
125k
        }
1242
125k
        if(ps_sao_ctxt->ps_slice_hdr->i1_slice_sao_chroma_flag)
1243
125k
        {
1244
            /* Copy the sao'ed output of the best candidate to the chroma recon buffer*/
1245
1246
125k
            ps_sao_ctxt->ps_cmn_utils_optimised_function_list->pf_copy_2d(
1247
125k
                ps_sao_ctxt->pu1_cur_chroma_recon_buf,
1248
125k
                ps_sao_ctxt->i4_cur_chroma_recon_stride,
1249
125k
                ps_sao_ctxt->au1_sao_chroma_scratch[best_buf_idx],
1250
125k
                i4_chroma_scratch_buf_stride,
1251
125k
                ctb_wd,
1252
125k
                ctb_ht >> !u1_is_422);
1253
125k
        }
1254
125k
    }
1255
125k
}