Coverage Report

Created: 2026-04-01 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/common/ihevc_sao.c
Line
Count
Source
1
/******************************************************************************
2
*
3
* Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at:
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*
17
******************************************************************************/
18
/**
19
*******************************************************************************
20
* @file
21
*  ihevc_sao.c
22
*
23
* @brief
24
*  Contains leaf level function definitions for sample adaptive offset process
25
*
26
* @author
27
*  Srinivas T
28
*
29
* @par List of Functions:
30
*   - ihevc_sao_band_offset_luma()
31
*   - ihevc_sao_band_offset_chroma()
32
*   - ihevc_sao_edge_offset_class0()
33
*   - ihevc_sao_edge_offset_class0_chroma()
34
*   - ihevc_sao_edge_offset_class1()
35
*   - ihevc_sao_edge_offset_class1_chroma()
36
*   - ihevc_sao_edge_offset_class2()
37
*   - ihevc_sao_edge_offset_class2_chroma()
38
*   - ihevc_sao_edge_offset_class3()
39
*   - ihevc_sao_edge_offset_class3_chroma()
40
* @remarks
41
*  None
42
*
43
*******************************************************************************
44
*/
45
#include <stdlib.h>
46
#include <assert.h>
47
#include <string.h>
48
#include "ihevc_typedefs.h"
49
#include "ihevc_macros.h"
50
#include "ihevc_platform_macros.h"
51
#include "ihevc_func_selector.h"
52
#include "ihevc_defs.h"
53
#include "ihevc_structs.h"
54
#include "ihevc_sao.h"
55
56
0
#define NUM_BAND_TABLE  32
57
58
const WORD32 gi4_ihevc_table_edge_idx[5] = { 1, 2, 0, 3, 4 };
59
/**
60
 * au4_avail is an array of flags - one for each neighboring block specifying if the block is available
61
 * au4_avail[0] - left
62
 * au4_avail[1] - right
63
 * au4_avail[2] - top
64
 * au4_avail[3] - bottom
65
 * au4_avail[4] - top-left
66
 * au4_avail[5] - top-right
67
 * au4_avail[6] - bottom-left
68
 * au4_avail[7] - bottom-right
69
 */
70
71
72
void ihevc_sao_band_offset_luma(UWORD8 *pu1_src,
73
                                WORD32 src_strd,
74
                                UWORD8 *pu1_src_left,
75
                                UWORD8 *pu1_src_top,
76
                                UWORD8 *pu1_src_top_left,
77
                                WORD32 sao_band_pos,
78
                                WORD8 *pi1_sao_offset,
79
                                WORD32 wd,
80
                                WORD32 ht)
81
0
{
82
0
    WORD32 band_shift;
83
0
    WORD32 band_table[NUM_BAND_TABLE];
84
0
    WORD32 i;
85
0
    WORD32 row, col;
86
87
    /* Updating left and top and top-left */
88
0
    for(row = 0; row < ht; row++)
89
0
    {
90
0
        pu1_src_left[row] = pu1_src[row * src_strd + (wd - 1)];
91
0
    }
92
0
    pu1_src_top_left[0] = pu1_src_top[wd - 1];
93
0
    for(col = 0; col < wd; col++)
94
0
    {
95
0
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
96
0
    }
97
98
0
    band_shift = BIT_DEPTH_LUMA - 5;
99
0
    for(i = 0; i < NUM_BAND_TABLE; i++)
100
0
    {
101
0
        band_table[i] = 0;
102
0
    }
103
0
    for(i = 0; i < 4; i++)
104
0
    {
105
0
        band_table[(i + sao_band_pos) & 31] = i + 1;
106
0
    }
107
108
0
    for(row = 0; row < ht; row++)
109
0
    {
110
0
        for(col = 0; col < wd; col++)
111
0
        {
112
0
            WORD32 band_idx;
113
114
0
            band_idx = band_table[pu1_src[col] >> band_shift];
115
0
            pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[band_idx], 0, (1 << (band_shift + 5)) - 1);
116
0
        }
117
0
        pu1_src += src_strd;
118
0
    }
119
0
}
120
121
122
123
/* input 'wd' has to be for the interleaved block and not for each color component */
124
void ihevc_sao_band_offset_chroma(UWORD8 *pu1_src,
125
                                  WORD32 src_strd,
126
                                  UWORD8 *pu1_src_left,
127
                                  UWORD8 *pu1_src_top,
128
                                  UWORD8 *pu1_src_top_left,
129
                                  WORD32 sao_band_pos_u,
130
                                  WORD32 sao_band_pos_v,
131
                                  WORD8 *pi1_sao_offset_u,
132
                                  WORD8 *pi1_sao_offset_v,
133
                                  WORD32 wd,
134
                                  WORD32 ht)
135
0
{
136
0
    WORD32 band_shift;
137
0
    WORD32 band_table_u[NUM_BAND_TABLE];
138
0
    WORD32 band_table_v[NUM_BAND_TABLE];
139
0
    WORD32 i;
140
0
    WORD32 row, col;
141
142
    /* Updating left and top and top-left */
143
0
    for(row = 0; row < ht; row++)
144
0
    {
145
0
        pu1_src_left[2 * row] = pu1_src[row * src_strd + (wd - 2)];
146
0
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + (wd - 1)];
147
0
    }
148
0
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
149
0
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
150
0
    for(col = 0; col < wd; col++)
151
0
    {
152
0
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
153
0
    }
154
155
156
0
    band_shift = BIT_DEPTH_CHROMA - 5;
157
0
    for(i = 0; i < NUM_BAND_TABLE; i++)
158
0
    {
159
0
        band_table_u[i] = 0;
160
0
        band_table_v[i] = 0;
161
0
    }
162
0
    for(i = 0; i < 4; i++)
163
0
    {
164
0
        band_table_u[(i + sao_band_pos_u) & 31] = i + 1;
165
0
        band_table_v[(i + sao_band_pos_v) & 31] = i + 1;
166
0
    }
167
168
0
    for(row = 0; row < ht; row++)
169
0
    {
170
0
        for(col = 0; col < wd; col++)
171
0
        {
172
0
            WORD32 band_idx;
173
0
            WORD8 *pi1_sao_offset;
174
175
0
            pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
176
0
            band_idx = (0 == col % 2) ? band_table_u[pu1_src[col] >> band_shift] : band_table_v[pu1_src[col] >> band_shift];
177
0
            pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[band_idx], 0, (1 << (band_shift + 5)) - 1);
178
0
        }
179
0
        pu1_src += src_strd;
180
0
    }
181
0
}
182
183
184
185
/* Horizontal filtering */
186
void ihevc_sao_edge_offset_class0(UWORD8 *pu1_src,
187
                                  WORD32 src_strd,
188
                                  UWORD8 *pu1_src_left,
189
                                  UWORD8 *pu1_src_top,
190
                                  UWORD8 *pu1_src_top_left,
191
                                  UWORD8 *pu1_src_top_right,
192
                                  UWORD8 *pu1_src_bot_left,
193
                                  UWORD8 *pu1_avail,
194
                                  WORD8 *pi1_sao_offset,
195
                                  WORD32 wd,
196
                                  WORD32 ht)
197
19.5k
{
198
19.5k
    WORD32 row, col;
199
19.5k
    UWORD8 au1_mask[MAX_CTB_SIZE];
200
19.5k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
201
19.5k
    WORD8 u1_sign_left, u1_sign_right;
202
19.5k
    WORD32 bit_depth;
203
19.5k
    UNUSED(pu1_src_top_right);
204
19.5k
    UNUSED(pu1_src_bot_left);
205
19.5k
    bit_depth = BIT_DEPTH_LUMA;
206
207
    /* Initialize the mask values */
208
19.5k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
209
210
    /* Update top and top-left arrays */
211
19.5k
    *pu1_src_top_left = pu1_src_top[wd - 1];
212
1.13M
    for(row = 0; row < ht; row++)
213
1.11M
    {
214
1.11M
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
215
1.11M
    }
216
1.09M
    for(col = 0; col < wd; col++)
217
1.07M
    {
218
1.07M
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
219
1.07M
    }
220
221
    /* Update masks based on the availability flags */
222
19.5k
    if(0 == pu1_avail[0])
223
6.27k
    {
224
6.27k
        au1_mask[0] = 0;
225
6.27k
    }
226
19.5k
    if(0 == pu1_avail[1])
227
7.16k
    {
228
7.16k
        au1_mask[wd - 1] = 0;
229
7.16k
    }
230
231
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
232
19.5k
    {
233
1.13M
        for(row = 0; row < ht; row++)
234
1.11M
        {
235
1.11M
            u1_sign_left = SIGN(pu1_src[0] - pu1_src_left[row]);
236
64.1M
            for(col = 0; col < wd; col++)
237
63.0M
            {
238
63.0M
                WORD32 edge_idx;
239
240
63.0M
                u1_sign_right = SIGN(pu1_src[col] - pu1_src[col + 1]);
241
63.0M
                edge_idx = 2 + u1_sign_left + u1_sign_right;
242
63.0M
                u1_sign_left = -u1_sign_right;
243
244
63.0M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
245
246
63.0M
                if(0 != edge_idx)
247
19.7M
                {
248
19.7M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
249
19.7M
                }
250
63.0M
            }
251
252
1.11M
            pu1_src += src_strd;
253
1.11M
        }
254
19.5k
    }
255
256
    /* Update left array */
257
1.13M
    for(row = 0; row < ht; row++)
258
1.11M
    {
259
1.11M
        pu1_src_left[row] = au1_src_left_tmp[row];
260
1.11M
    }
261
262
19.5k
}
263
264
265
266
267
/* input 'wd' has to be for the interleaved block and not for each color component */
268
void ihevc_sao_edge_offset_class0_chroma(UWORD8 *pu1_src,
269
                                         WORD32 src_strd,
270
                                         UWORD8 *pu1_src_left,
271
                                         UWORD8 *pu1_src_top,
272
                                         UWORD8 *pu1_src_top_left,
273
                                         UWORD8 *pu1_src_top_right,
274
                                         UWORD8 *pu1_src_bot_left,
275
                                         UWORD8 *pu1_avail,
276
                                         WORD8 *pi1_sao_offset_u,
277
                                         WORD8 *pi1_sao_offset_v,
278
                                         WORD32 wd,
279
                                         WORD32 ht)
280
11.2k
{
281
11.2k
    WORD32 row, col;
282
11.2k
    UWORD8 au1_mask[MAX_CTB_SIZE];
283
11.2k
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE];
284
11.2k
    WORD8 u1_sign_left_u, u1_sign_right_u;
285
11.2k
    WORD8 u1_sign_left_v, u1_sign_right_v;
286
11.2k
    WORD32 bit_depth;
287
11.2k
    UNUSED(pu1_src_top_right);
288
11.2k
    UNUSED(pu1_src_bot_left);
289
11.2k
    bit_depth = BIT_DEPTH_CHROMA;
290
291
    /* Initialize the mask values */
292
11.2k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
293
294
    /* Update left, top and top-left arrays */
295
11.2k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
296
11.2k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
297
332k
    for(row = 0; row < ht; row++)
298
321k
    {
299
321k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
300
321k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
301
321k
    }
302
618k
    for(col = 0; col < wd; col++)
303
607k
    {
304
607k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
305
607k
    }
306
307
    /* Update masks based on the availability flags */
308
11.2k
    if(0 == pu1_avail[0])
309
3.03k
    {
310
3.03k
        au1_mask[0] = 0;
311
3.03k
    }
312
11.2k
    if(0 == pu1_avail[1])
313
3.92k
    {
314
3.92k
        au1_mask[(wd - 1) >> 1] = 0;
315
3.92k
    }
316
317
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
318
11.2k
    {
319
332k
        for(row = 0; row < ht; row++)
320
321k
        {
321
321k
            u1_sign_left_u = SIGN(pu1_src[0] - pu1_src_left[2 * row]);
322
321k
            u1_sign_left_v = SIGN(pu1_src[1] - pu1_src_left[2 * row + 1]);
323
18.1M
            for(col = 0; col < wd; col++)
324
17.7M
            {
325
17.7M
                WORD32 edge_idx;
326
17.7M
                WORD8 *pi1_sao_offset;
327
328
17.7M
                if(0 == col % 2)
329
8.89M
                {
330
8.89M
                    pi1_sao_offset = pi1_sao_offset_u;
331
8.89M
                    u1_sign_right_u = SIGN(pu1_src[col] - pu1_src[col + 2]);
332
8.89M
                    edge_idx = 2 + u1_sign_left_u + u1_sign_right_u;
333
8.89M
                    u1_sign_left_u = -u1_sign_right_u;
334
8.89M
                }
335
8.89M
                else
336
8.89M
                {
337
8.89M
                    pi1_sao_offset = pi1_sao_offset_v;
338
8.89M
                    u1_sign_right_v = SIGN(pu1_src[col] - pu1_src[col + 2]);
339
8.89M
                    edge_idx = 2 + u1_sign_left_v + u1_sign_right_v;
340
8.89M
                    u1_sign_left_v = -u1_sign_right_v;
341
8.89M
                }
342
343
17.7M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
344
345
17.7M
                if(0 != edge_idx)
346
4.13M
                {
347
4.13M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
348
4.13M
                }
349
17.7M
            }
350
351
321k
            pu1_src += src_strd;
352
321k
        }
353
11.2k
    }
354
355
654k
    for(row = 0; row < 2 * ht; row++)
356
643k
    {
357
643k
        pu1_src_left[row] = au1_src_left_tmp[row];
358
643k
    }
359
360
11.2k
}
361
362
363
364
/* Vertical filtering */
365
void ihevc_sao_edge_offset_class1(UWORD8 *pu1_src,
366
                                  WORD32 src_strd,
367
                                  UWORD8 *pu1_src_left,
368
                                  UWORD8 *pu1_src_top,
369
                                  UWORD8 *pu1_src_top_left,
370
                                  UWORD8 *pu1_src_top_right,
371
                                  UWORD8 *pu1_src_bot_left,
372
                                  UWORD8 *pu1_avail,
373
                                  WORD8 *pi1_sao_offset,
374
                                  WORD32 wd,
375
                                  WORD32 ht)
376
10.3k
{
377
10.3k
    WORD32 row, col;
378
10.3k
    UWORD8 au1_mask[MAX_CTB_SIZE];
379
10.3k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
380
10.3k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
381
10.3k
    WORD8 u1_sign_down;
382
10.3k
    WORD32 bit_depth;
383
10.3k
    UNUSED(pu1_src_top_right);
384
10.3k
    UNUSED(pu1_src_bot_left);
385
386
10.3k
    bit_depth = BIT_DEPTH_LUMA;
387
388
    /* Initialize the mask values */
389
10.3k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
390
391
    /* Update left, top and top-left arrays */
392
10.3k
    *pu1_src_top_left = pu1_src_top[wd - 1];
393
600k
    for(row = 0; row < ht; row++)
394
590k
    {
395
590k
        pu1_src_left[row] = pu1_src[row * src_strd + wd - 1];
396
590k
    }
397
599k
    for(col = 0; col < wd; col++)
398
589k
    {
399
589k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
400
589k
    }
401
402
    /* Update height and source pointers based on the availability flags */
403
10.3k
    if(0 == pu1_avail[2])
404
4.23k
    {
405
4.23k
        pu1_src += src_strd;
406
4.23k
        ht--;
407
247k
        for(col = 0; col < wd; col++)
408
242k
        {
409
242k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
410
242k
        }
411
4.23k
    }
412
6.16k
    else
413
6.16k
    {
414
352k
        for(col = 0; col < wd; col++)
415
346k
        {
416
346k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
417
346k
        }
418
6.16k
    }
419
10.3k
    if(0 == pu1_avail[3])
420
4.39k
    {
421
4.39k
        ht--;
422
4.39k
    }
423
424
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
425
10.3k
    {
426
591k
        for(row = 0; row < ht; row++)
427
581k
        {
428
34.3M
            for(col = 0; col < wd; col++)
429
33.7M
            {
430
33.7M
                WORD32 edge_idx;
431
432
33.7M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
433
33.7M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
434
33.7M
                au1_sign_up[col] = -u1_sign_down;
435
436
33.7M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
437
438
33.7M
                if(0 != edge_idx)
439
9.94M
                {
440
9.94M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
441
9.94M
                }
442
33.7M
            }
443
444
581k
            pu1_src += src_strd;
445
581k
        }
446
10.3k
    }
447
448
599k
    for(col = 0; col < wd; col++)
449
589k
    {
450
589k
        pu1_src_top[col] = au1_src_top_tmp[col];
451
589k
    }
452
453
10.3k
}
454
455
456
457
/* input 'wd' has to be for the interleaved block and not for each color component */
458
void ihevc_sao_edge_offset_class1_chroma(UWORD8 *pu1_src,
459
                                         WORD32 src_strd,
460
                                         UWORD8 *pu1_src_left,
461
                                         UWORD8 *pu1_src_top,
462
                                         UWORD8 *pu1_src_top_left,
463
                                         UWORD8 *pu1_src_top_right,
464
                                         UWORD8 *pu1_src_bot_left,
465
                                         UWORD8 *pu1_avail,
466
                                         WORD8 *pi1_sao_offset_u,
467
                                         WORD8 *pi1_sao_offset_v,
468
                                         WORD32 wd,
469
                                         WORD32 ht)
470
2.03k
{
471
2.03k
    WORD32 row, col;
472
2.03k
    UWORD8 au1_mask[MAX_CTB_SIZE];
473
2.03k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
474
2.03k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
475
2.03k
    WORD8 u1_sign_down;
476
2.03k
    WORD32 bit_depth;
477
2.03k
    UNUSED(pu1_src_top_right);
478
2.03k
    UNUSED(pu1_src_bot_left);
479
480
2.03k
    bit_depth = BIT_DEPTH_CHROMA;
481
482
    /* Initialize the mask values */
483
2.03k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
484
485
    /* Update left, top and top-left arrays */
486
2.03k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
487
2.03k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
488
59.4k
    for(row = 0; row < ht; row++)
489
57.4k
    {
490
57.4k
        pu1_src_left[2 * row] = pu1_src[row * src_strd + wd - 2];
491
57.4k
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
492
57.4k
    }
493
120k
    for(col = 0; col < wd; col++)
494
118k
    {
495
118k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
496
118k
    }
497
498
    /* Update height and source pointers based on the availability flags */
499
2.03k
    if(0 == pu1_avail[2])
500
580
    {
501
580
        pu1_src += src_strd;
502
580
        ht--;
503
34.4k
        for(col = 0; col < wd; col++)
504
33.8k
        {
505
33.8k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
506
33.8k
        }
507
580
    }
508
1.45k
    else
509
1.45k
    {
510
86.0k
        for(col = 0; col < wd; col++)
511
84.6k
        {
512
84.6k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
513
84.6k
        }
514
1.45k
    }
515
2.03k
    if(0 == pu1_avail[3])
516
737
    {
517
737
        ht--;
518
737
    }
519
520
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
521
2.03k
    {
522
58.1k
        for(row = 0; row < ht; row++)
523
56.0k
        {
524
3.41M
            for(col = 0; col < wd; col++)
525
3.35M
            {
526
3.35M
                WORD32 edge_idx;
527
3.35M
                WORD8 *pi1_sao_offset;
528
529
3.35M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
530
531
3.35M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
532
3.35M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
533
3.35M
                au1_sign_up[col] = -u1_sign_down;
534
535
3.35M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
536
537
3.35M
                if(0 != edge_idx)
538
450k
                {
539
450k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
540
450k
                }
541
3.35M
            }
542
543
56.0k
            pu1_src += src_strd;
544
56.0k
        }
545
2.03k
    }
546
547
120k
    for(col = 0; col < wd; col++)
548
118k
    {
549
118k
        pu1_src_top[col] = au1_src_top_tmp[col];
550
118k
    }
551
552
2.03k
}
553
554
555
556
/* 135 degree filtering */
557
void ihevc_sao_edge_offset_class2(UWORD8 *pu1_src,
558
                                  WORD32 src_strd,
559
                                  UWORD8 *pu1_src_left,
560
                                  UWORD8 *pu1_src_top,
561
                                  UWORD8 *pu1_src_top_left,
562
                                  UWORD8 *pu1_src_top_right,
563
                                  UWORD8 *pu1_src_bot_left,
564
                                  UWORD8 *pu1_avail,
565
                                  WORD8 *pi1_sao_offset,
566
                                  WORD32 wd,
567
                                  WORD32 ht)
568
9.33k
{
569
9.33k
    WORD32 row, col;
570
9.33k
    UWORD8 au1_mask[MAX_CTB_SIZE];
571
9.33k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
572
9.33k
    UWORD8 u1_src_top_left_tmp;
573
9.33k
    WORD8 au1_sign_up[MAX_CTB_SIZE + 1], au1_sign_up_tmp[MAX_CTB_SIZE + 1];
574
9.33k
    WORD8 u1_sign_down;
575
9.33k
    WORD8 *pu1_sign_up;
576
9.33k
    WORD8 *pu1_sign_up_tmp;
577
9.33k
    UWORD8 *pu1_src_left_cpy;
578
579
9.33k
    WORD32 bit_depth;
580
9.33k
    UWORD8 u1_pos_0_0_tmp;
581
9.33k
    UWORD8 u1_pos_wd_ht_tmp;
582
9.33k
    UNUSED(pu1_src_top_right);
583
9.33k
    UNUSED(pu1_src_bot_left);
584
585
9.33k
    bit_depth = BIT_DEPTH_LUMA;
586
9.33k
    pu1_sign_up = au1_sign_up;
587
9.33k
    pu1_sign_up_tmp = au1_sign_up_tmp;
588
9.33k
    pu1_src_left_cpy = pu1_src_left;
589
590
    /* Initialize the mask values */
591
9.33k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
592
593
    /* Update left, top and top-left arrays */
594
9.33k
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
595
539k
    for(row = 0; row < ht; row++)
596
530k
    {
597
530k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
598
530k
    }
599
536k
    for(col = 0; col < wd; col++)
600
526k
    {
601
526k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
602
526k
    }
603
604
605
    /* If top-left is available, process separately */
606
9.33k
    if(0 != pu1_avail[4])
607
3.29k
    {
608
3.29k
        WORD32 edge_idx;
609
610
3.29k
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
611
3.29k
                        SIGN(pu1_src[0] - pu1_src[1 + src_strd]);
612
613
3.29k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
614
615
3.29k
        if(0 != edge_idx)
616
1.46k
        {
617
1.46k
            u1_pos_0_0_tmp = CLIP3(pu1_src[0] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
618
1.46k
        }
619
1.82k
        else
620
1.82k
        {
621
1.82k
            u1_pos_0_0_tmp = pu1_src[0];
622
1.82k
        }
623
3.29k
    }
624
6.03k
    else
625
6.03k
    {
626
6.03k
        u1_pos_0_0_tmp = pu1_src[0];
627
6.03k
    }
628
629
    /* If bottom-right is available, process separately */
630
9.33k
    if(0 != pu1_avail[7])
631
3.19k
    {
632
3.19k
        WORD32 edge_idx;
633
634
3.19k
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 1 - src_strd]) +
635
3.19k
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 1 + src_strd]);
636
637
3.19k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
638
639
3.19k
        if(0 != edge_idx)
640
1.48k
        {
641
1.48k
            u1_pos_wd_ht_tmp = CLIP3(pu1_src[wd - 1 + (ht - 1) * src_strd] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
642
1.48k
        }
643
1.70k
        else
644
1.70k
        {
645
1.70k
            u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
646
1.70k
        }
647
3.19k
    }
648
6.14k
    else
649
6.14k
    {
650
6.14k
        u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
651
6.14k
    }
652
653
    /* If Left is not available */
654
9.33k
    if(0 == pu1_avail[0])
655
3.56k
    {
656
3.56k
        au1_mask[0] = 0;
657
3.56k
    }
658
659
    /* If Top is not available */
660
9.33k
    if(0 == pu1_avail[2])
661
4.13k
    {
662
4.13k
        pu1_src += src_strd;
663
4.13k
        ht--;
664
4.13k
        pu1_src_left_cpy += 1;
665
237k
        for(col = 1; col < wd; col++)
666
233k
        {
667
233k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 1 - src_strd]);
668
233k
        }
669
4.13k
    }
670
5.19k
    else
671
5.19k
    {
672
289k
        for(col = 1; col < wd; col++)
673
283k
        {
674
283k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 1]);
675
283k
        }
676
5.19k
    }
677
678
    /* If Right is not available */
679
9.33k
    if(0 == pu1_avail[1])
680
3.64k
    {
681
3.64k
        au1_mask[wd - 1] = 0;
682
3.64k
    }
683
684
    /* If Bottom is not available */
685
9.33k
    if(0 == pu1_avail[3])
686
4.20k
    {
687
4.20k
        ht--;
688
4.20k
    }
689
690
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
691
9.33k
    {
692
531k
        for(row = 0; row < ht; row++)
693
522k
        {
694
522k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[row - 1]);
695
30.7M
            for(col = 0; col < wd; col++)
696
30.2M
            {
697
30.2M
                WORD32 edge_idx;
698
699
30.2M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 1 + src_strd]);
700
30.2M
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
701
30.2M
                pu1_sign_up_tmp[col + 1] = -u1_sign_down;
702
703
30.2M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
704
705
30.2M
                if(0 != edge_idx)
706
10.3M
                {
707
10.3M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
708
10.3M
                }
709
30.2M
            }
710
711
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
712
522k
            {
713
522k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
714
522k
                pu1_sign_up = pu1_sign_up_tmp;
715
522k
                pu1_sign_up_tmp = pu1_swap_tmp;
716
522k
            }
717
718
522k
            pu1_src += src_strd;
719
522k
        }
720
721
9.33k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp;
722
9.33k
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp;
723
9.33k
    }
724
725
9.33k
    if(0 == pu1_avail[2])
726
4.13k
        ht++;
727
9.33k
    if(0 == pu1_avail[3])
728
4.20k
        ht++;
729
9.33k
    *pu1_src_top_left = u1_src_top_left_tmp;
730
539k
    for(row = 0; row < ht; row++)
731
530k
    {
732
530k
        pu1_src_left[row] = au1_src_left_tmp[row];
733
530k
    }
734
536k
    for(col = 0; col < wd; col++)
735
526k
    {
736
526k
        pu1_src_top[col] = au1_src_top_tmp[col];
737
526k
    }
738
739
9.33k
}
740
741
742
743
744
/* 135 degree filtering */
745
void ihevc_sao_edge_offset_class2_chroma(UWORD8 *pu1_src,
746
                                         WORD32 src_strd,
747
                                         UWORD8 *pu1_src_left,
748
                                         UWORD8 *pu1_src_top,
749
                                         UWORD8 *pu1_src_top_left,
750
                                         UWORD8 *pu1_src_top_right,
751
                                         UWORD8 *pu1_src_bot_left,
752
                                         UWORD8 *pu1_avail,
753
                                         WORD8 *pi1_sao_offset_u,
754
                                         WORD8 *pi1_sao_offset_v,
755
                                         WORD32 wd,
756
                                         WORD32 ht)
757
971
{
758
971
    WORD32 row, col;
759
971
    UWORD8 au1_mask[MAX_CTB_SIZE];
760
971
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
761
971
    UWORD8 au1_src_top_left_tmp[2];
762
971
    WORD8 au1_sign_up[MAX_CTB_SIZE + 2], au1_sign_up_tmp[MAX_CTB_SIZE + 2];
763
971
    WORD8 u1_sign_down;
764
971
    WORD8 *pu1_sign_up;
765
971
    WORD8 *pu1_sign_up_tmp;
766
971
    UWORD8 *pu1_src_left_cpy;
767
768
971
    WORD32 bit_depth;
769
770
971
    UWORD8 u1_pos_0_0_tmp_u;
771
971
    UWORD8 u1_pos_0_0_tmp_v;
772
971
    UWORD8 u1_pos_wd_ht_tmp_u;
773
971
    UWORD8 u1_pos_wd_ht_tmp_v;
774
971
    UNUSED(pu1_src_top_right);
775
971
    UNUSED(pu1_src_bot_left);
776
777
778
971
    bit_depth = BIT_DEPTH_CHROMA;
779
971
    pu1_sign_up = au1_sign_up;
780
971
    pu1_sign_up_tmp = au1_sign_up_tmp;
781
971
    pu1_src_left_cpy = pu1_src_left;
782
783
    /* Initialize the mask values */
784
971
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
785
786
    /* Update left, top and top-left arrays */
787
971
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
788
971
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
789
28.5k
    for(row = 0; row < ht; row++)
790
27.5k
    {
791
27.5k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
792
27.5k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
793
27.5k
    }
794
57.1k
    for(col = 0; col < wd; col++)
795
56.2k
    {
796
56.2k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
797
56.2k
    }
798
799
800
    /* If top-left is available, process separately */
801
971
    if(0 != pu1_avail[4])
802
365
    {
803
365
        WORD32 edge_idx;
804
805
        /* U */
806
365
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
807
365
                        SIGN(pu1_src[0] - pu1_src[2 + src_strd]);
808
809
365
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
810
811
365
        if(0 != edge_idx)
812
162
        {
813
162
            u1_pos_0_0_tmp_u = CLIP3(pu1_src[0] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
814
162
        }
815
203
        else
816
203
        {
817
203
            u1_pos_0_0_tmp_u = pu1_src[0];
818
203
        }
819
820
        /* V */
821
365
        edge_idx = 2 + SIGN(pu1_src[1] - pu1_src_top_left[1]) +
822
365
                        SIGN(pu1_src[1] - pu1_src[1 + 2 + src_strd]);
823
824
365
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
825
826
365
        if(0 != edge_idx)
827
157
        {
828
157
            u1_pos_0_0_tmp_v = CLIP3(pu1_src[1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
829
157
        }
830
208
        else
831
208
        {
832
208
            u1_pos_0_0_tmp_v = pu1_src[1];
833
208
        }
834
365
    }
835
606
    else
836
606
    {
837
606
        u1_pos_0_0_tmp_u = pu1_src[0];
838
606
        u1_pos_0_0_tmp_v = pu1_src[1];
839
606
    }
840
841
    /* If bottom-right is available, process separately */
842
971
    if(0 != pu1_avail[7])
843
262
    {
844
262
        WORD32 edge_idx;
845
846
        /* U */
847
262
        edge_idx = 2 + SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd - 2 - src_strd]) +
848
262
                        SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd + 2 + src_strd]);
849
850
262
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
851
852
262
        if(0 != edge_idx)
853
131
        {
854
131
            u1_pos_wd_ht_tmp_u = CLIP3(pu1_src[wd - 2 + (ht - 1) * src_strd] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
855
131
        }
856
131
        else
857
131
        {
858
131
            u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
859
131
        }
860
861
        /* V */
862
262
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 2 - src_strd]) +
863
262
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 2 + src_strd]);
864
865
262
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
866
867
262
        if(0 != edge_idx)
868
109
        {
869
109
            u1_pos_wd_ht_tmp_v = CLIP3(pu1_src[wd - 1 + (ht - 1) * src_strd] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
870
109
        }
871
153
        else
872
153
        {
873
153
            u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
874
153
        }
875
262
    }
876
709
    else
877
709
    {
878
709
        u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
879
709
        u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
880
709
    }
881
882
    /* If Left is not available */
883
971
    if(0 == pu1_avail[0])
884
330
    {
885
330
        au1_mask[0] = 0;
886
330
    }
887
888
    /* If Top is not available */
889
971
    if(0 == pu1_avail[2])
890
481
    {
891
481
        pu1_src += src_strd;
892
481
        pu1_src_left_cpy += 2;
893
481
        ht--;
894
28.3k
        for(col = 2; col < wd; col++)
895
27.8k
        {
896
27.8k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 2 - src_strd]);
897
27.8k
        }
898
481
    }
899
490
    else
900
490
    {
901
26.8k
        for(col = 2; col < wd; col++)
902
26.3k
        {
903
26.3k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 2]);
904
26.3k
        }
905
490
    }
906
907
    /* If Right is not available */
908
971
    if(0 == pu1_avail[1])
909
411
    {
910
411
        au1_mask[(wd - 1) >> 1] = 0;
911
411
    }
912
913
    /* If Bottom is not available */
914
971
    if(0 == pu1_avail[3])
915
553
    {
916
553
        ht--;
917
553
    }
918
919
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
920
971
    {
921
27.4k
        for(row = 0; row < ht; row++)
922
26.5k
        {
923
26.5k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[2 * (row - 1)]);
924
26.5k
            pu1_sign_up[1] = SIGN(pu1_src[1] - pu1_src_left_cpy[2 * (row - 1) + 1]);
925
1.62M
            for(col = 0; col < wd; col++)
926
1.60M
            {
927
1.60M
                WORD32 edge_idx;
928
1.60M
                WORD8 *pi1_sao_offset;
929
930
1.60M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
931
932
1.60M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 2 + src_strd]);
933
1.60M
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
934
1.60M
                pu1_sign_up_tmp[col + 2] = -u1_sign_down;
935
936
1.60M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
937
938
1.60M
                if(0 != edge_idx)
939
495k
                {
940
495k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
941
495k
                }
942
1.60M
            }
943
944
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
945
26.5k
            {
946
26.5k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
947
26.5k
                pu1_sign_up = pu1_sign_up_tmp;
948
26.5k
                pu1_sign_up_tmp = pu1_swap_tmp;
949
26.5k
            }
950
951
26.5k
            pu1_src += src_strd;
952
26.5k
        }
953
954
971
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp_u;
955
971
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + 1] = u1_pos_0_0_tmp_v;
956
971
        pu1_src[(pu1_avail[3] ? wd - 2 - src_strd : wd - 2)] = u1_pos_wd_ht_tmp_u;
957
971
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp_v;
958
971
    }
959
960
971
    if(0 == pu1_avail[2])
961
481
        ht++;
962
971
    if(0 == pu1_avail[3])
963
553
        ht++;
964
971
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
965
971
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
966
56.0k
    for(row = 0; row < 2 * ht; row++)
967
55.1k
    {
968
55.1k
        pu1_src_left[row] = au1_src_left_tmp[row];
969
55.1k
    }
970
57.1k
    for(col = 0; col < wd; col++)
971
56.2k
    {
972
56.2k
        pu1_src_top[col] = au1_src_top_tmp[col];
973
56.2k
    }
974
975
971
}
976
977
978
979
980
/* 45 degree filtering */
981
void ihevc_sao_edge_offset_class3(UWORD8 *pu1_src,
982
                                  WORD32 src_strd,
983
                                  UWORD8 *pu1_src_left,
984
                                  UWORD8 *pu1_src_top,
985
                                  UWORD8 *pu1_src_top_left,
986
                                  UWORD8 *pu1_src_top_right,
987
                                  UWORD8 *pu1_src_bot_left,
988
                                  UWORD8 *pu1_avail,
989
                                  WORD8 *pi1_sao_offset,
990
                                  WORD32 wd,
991
                                  WORD32 ht)
992
9.12k
{
993
9.12k
    WORD32 row, col;
994
9.12k
    UWORD8 au1_mask[MAX_CTB_SIZE];
995
9.12k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
996
9.12k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
997
9.12k
    UWORD8 u1_src_top_left_tmp;
998
9.12k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
999
9.12k
    UWORD8 *pu1_src_left_cpy;
1000
9.12k
    WORD8 u1_sign_down;
1001
9.12k
    WORD32 bit_depth;
1002
1003
9.12k
    UWORD8 u1_pos_0_ht_tmp;
1004
9.12k
    UWORD8 u1_pos_wd_0_tmp;
1005
1006
9.12k
    bit_depth = BIT_DEPTH_LUMA;
1007
9.12k
    pu1_src_left_cpy = pu1_src_left;
1008
1009
    /* Initialize the mask values */
1010
9.12k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1011
1012
    /* Update left, top and top-left arrays */
1013
9.12k
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
1014
528k
    for(row = 0; row < ht; row++)
1015
519k
    {
1016
519k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
1017
519k
    }
1018
524k
    for(col = 0; col < wd; col++)
1019
515k
    {
1020
515k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1021
515k
    }
1022
1023
    /* If top-right is available, process separately */
1024
9.12k
    if(0 != pu1_avail[5])
1025
3.15k
    {
1026
3.15k
        WORD32 edge_idx;
1027
1028
3.15k
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[0]) +
1029
3.15k
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 1 + src_strd]);
1030
1031
3.15k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1032
1033
3.15k
        if(0 != edge_idx)
1034
1.36k
        {
1035
1.36k
            u1_pos_wd_0_tmp = CLIP3(pu1_src[wd - 1] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1036
1.36k
        }
1037
1.78k
        else
1038
1.78k
        {
1039
1.78k
            u1_pos_wd_0_tmp = pu1_src[wd - 1];
1040
1.78k
        }
1041
3.15k
    }
1042
5.97k
    else
1043
5.97k
    {
1044
5.97k
        u1_pos_wd_0_tmp = pu1_src[wd - 1];
1045
5.97k
    }
1046
1047
    /* If bottom-left is available, process separately */
1048
9.12k
    if(0 != pu1_avail[6])
1049
3.13k
    {
1050
3.13k
        WORD32 edge_idx;
1051
1052
3.13k
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 1 - src_strd]) +
1053
3.13k
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1054
1055
3.13k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1056
1057
3.13k
        if(0 != edge_idx)
1058
1.40k
        {
1059
1.40k
            u1_pos_0_ht_tmp = CLIP3(pu1_src[(ht - 1) * src_strd] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1060
1.40k
        }
1061
1.73k
        else
1062
1.73k
        {
1063
1.73k
            u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1064
1.73k
        }
1065
3.13k
    }
1066
5.99k
    else
1067
5.99k
    {
1068
5.99k
        u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1069
5.99k
    }
1070
1071
    /* If Left is not available */
1072
9.12k
    if(0 == pu1_avail[0])
1073
3.48k
    {
1074
3.48k
        au1_mask[0] = 0;
1075
3.48k
    }
1076
1077
    /* If Top is not available */
1078
9.12k
    if(0 == pu1_avail[2])
1079
4.02k
    {
1080
4.02k
        pu1_src += src_strd;
1081
4.02k
        ht--;
1082
4.02k
        pu1_src_left_cpy += 1;
1083
230k
        for(col = 0; col < wd - 1; col++)
1084
226k
        {
1085
226k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 1 - src_strd]);
1086
226k
        }
1087
4.02k
    }
1088
5.10k
    else
1089
5.10k
    {
1090
284k
        for(col = 0; col < wd - 1; col++)
1091
279k
        {
1092
279k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 1]);
1093
279k
        }
1094
5.10k
    }
1095
1096
    /* If Right is not available */
1097
9.12k
    if(0 == pu1_avail[1])
1098
3.53k
    {
1099
3.53k
        au1_mask[wd - 1] = 0;
1100
3.53k
    }
1101
1102
    /* If Bottom is not available */
1103
9.12k
    if(0 == pu1_avail[3])
1104
4.08k
    {
1105
4.08k
        ht--;
1106
4.08k
    }
1107
1108
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1109
9.12k
    {
1110
520k
        for(row = 0; row < ht; row++)
1111
511k
        {
1112
511k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 1 - src_strd]);
1113
30.0M
            for(col = 0; col < wd; col++)
1114
29.5M
            {
1115
29.5M
                WORD32 edge_idx;
1116
1117
29.5M
                u1_sign_down = SIGN(pu1_src[col] - ((col == 0) ? pu1_src_left_cpy[row + 1] :
1118
29.5M
                                                                 pu1_src[col - 1 + src_strd]));
1119
29.5M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1120
29.5M
                if(col > 0)
1121
29.0M
                    au1_sign_up[col - 1] = -u1_sign_down;
1122
1123
29.5M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
1124
1125
29.5M
                if(0 != edge_idx)
1126
10.1M
                {
1127
10.1M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1128
10.1M
                }
1129
29.5M
            }
1130
1131
511k
            pu1_src += src_strd;
1132
511k
        }
1133
1134
9.12k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp;
1135
9.12k
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp;
1136
9.12k
    }
1137
1138
9.12k
    if(0 == pu1_avail[2])
1139
4.02k
        ht++;
1140
9.12k
    if(0 == pu1_avail[3])
1141
4.08k
        ht++;
1142
9.12k
    *pu1_src_top_left = u1_src_top_left_tmp;
1143
528k
    for(row = 0; row < ht; row++)
1144
519k
    {
1145
519k
        pu1_src_left[row] = au1_src_left_tmp[row];
1146
519k
    }
1147
524k
    for(col = 0; col < wd; col++)
1148
515k
    {
1149
515k
        pu1_src_top[col] = au1_src_top_tmp[col];
1150
515k
    }
1151
1152
9.12k
}
1153
1154
1155
1156
1157
void ihevc_sao_edge_offset_class3_chroma(UWORD8 *pu1_src,
1158
                                         WORD32 src_strd,
1159
                                         UWORD8 *pu1_src_left,
1160
                                         UWORD8 *pu1_src_top,
1161
                                         UWORD8 *pu1_src_top_left,
1162
                                         UWORD8 *pu1_src_top_right,
1163
                                         UWORD8 *pu1_src_bot_left,
1164
                                         UWORD8 *pu1_avail,
1165
                                         WORD8 *pi1_sao_offset_u,
1166
                                         WORD8 *pi1_sao_offset_v,
1167
                                         WORD32 wd,
1168
                                         WORD32 ht)
1169
765
{
1170
765
    WORD32 row, col;
1171
765
    UWORD8 au1_mask[MAX_CTB_SIZE];
1172
765
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
1173
765
    UWORD8 au1_src_top_left_tmp[2];
1174
765
    WORD8 au1_sign_up[MAX_CTB_SIZE];
1175
765
    UWORD8 *pu1_src_left_cpy;
1176
765
    WORD8 u1_sign_down;
1177
765
    WORD32 bit_depth;
1178
1179
765
    UWORD8 u1_pos_wd_0_tmp_u;
1180
765
    UWORD8 u1_pos_wd_0_tmp_v;
1181
765
    UWORD8 u1_pos_0_ht_tmp_u;
1182
765
    UWORD8 u1_pos_0_ht_tmp_v;
1183
1184
765
    bit_depth = BIT_DEPTH_CHROMA;
1185
765
    pu1_src_left_cpy = pu1_src_left;
1186
1187
    /* Initialize the mask values */
1188
765
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1189
1190
    /* Update left, top and top-left arrays */
1191
765
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
1192
765
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
1193
22.6k
    for(row = 0; row < ht; row++)
1194
21.8k
    {
1195
21.8k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
1196
21.8k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
1197
21.8k
    }
1198
45.4k
    for(col = 0; col < wd; col++)
1199
44.6k
    {
1200
44.6k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1201
44.6k
    }
1202
1203
1204
    /* If top-right is available, process separately */
1205
765
    if(0 != pu1_avail[5])
1206
226
    {
1207
226
        WORD32 edge_idx;
1208
1209
        /* U */
1210
226
        edge_idx = 2 + SIGN(pu1_src[wd - 2] - pu1_src_top_right[0]) +
1211
226
                        SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 - 2 + src_strd]);
1212
1213
226
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1214
1215
226
        if(0 != edge_idx)
1216
74
        {
1217
74
            u1_pos_wd_0_tmp_u = CLIP3(pu1_src[wd - 2] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
1218
74
        }
1219
152
        else
1220
152
        {
1221
152
            u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1222
152
        }
1223
1224
        /* V */
1225
226
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[1]) +
1226
226
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 2 + src_strd]);
1227
1228
226
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1229
1230
226
        if(0 != edge_idx)
1231
62
        {
1232
62
            u1_pos_wd_0_tmp_v = CLIP3(pu1_src[wd - 1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
1233
62
        }
1234
164
        else
1235
164
        {
1236
164
            u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1237
164
        }
1238
226
    }
1239
539
    else
1240
539
    {
1241
539
        u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1242
539
        u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1243
539
    }
1244
1245
    /* If bottom-left is available, process separately */
1246
765
    if(0 != pu1_avail[6])
1247
203
    {
1248
203
        WORD32 edge_idx;
1249
1250
        /* U */
1251
203
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 2 - src_strd]) +
1252
203
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1253
1254
203
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1255
1256
203
        if(0 != edge_idx)
1257
62
        {
1258
62
            u1_pos_0_ht_tmp_u = CLIP3(pu1_src[(ht - 1) * src_strd] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
1259
62
        }
1260
141
        else
1261
141
        {
1262
141
            u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1263
141
        }
1264
1265
        /* V */
1266
203
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src[(ht - 1) * src_strd + 1 + 2 - src_strd]) +
1267
203
                        SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src_bot_left[1]);
1268
1269
203
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1270
1271
203
        if(0 != edge_idx)
1272
50
        {
1273
50
            u1_pos_0_ht_tmp_v = CLIP3(pu1_src[(ht - 1) * src_strd + 1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
1274
50
        }
1275
153
        else
1276
153
        {
1277
153
            u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1278
153
        }
1279
203
    }
1280
562
    else
1281
562
    {
1282
562
        u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1283
562
        u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1284
562
    }
1285
1286
    /* If Left is not available */
1287
765
    if(0 == pu1_avail[0])
1288
250
    {
1289
250
        au1_mask[0] = 0;
1290
250
    }
1291
1292
    /* If Top is not available */
1293
765
    if(0 == pu1_avail[2])
1294
367
    {
1295
367
        pu1_src += src_strd;
1296
367
        ht--;
1297
367
        pu1_src_left_cpy += 2;
1298
21.6k
        for(col = 0; col < wd - 2; col++)
1299
21.2k
        {
1300
21.2k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 2 - src_strd]);
1301
21.2k
        }
1302
367
    }
1303
398
    else
1304
398
    {
1305
22.2k
        for(col = 0; col < wd - 2; col++)
1306
21.8k
        {
1307
21.8k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 2]);
1308
21.8k
        }
1309
398
    }
1310
1311
    /* If Right is not available */
1312
765
    if(0 == pu1_avail[1])
1313
299
    {
1314
299
        au1_mask[(wd - 1) >> 1] = 0;
1315
299
    }
1316
1317
    /* If Bottom is not available */
1318
765
    if(0 == pu1_avail[3])
1319
426
    {
1320
426
        ht--;
1321
426
    }
1322
1323
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1324
765
    {
1325
21.8k
        for(row = 0; row < ht; row++)
1326
21.0k
        {
1327
21.0k
            au1_sign_up[wd - 2] = SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 + 2 - src_strd]);
1328
21.0k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 2 - src_strd]);
1329
1.28M
            for(col = 0; col < wd; col++)
1330
1.26M
            {
1331
1.26M
                WORD32 edge_idx;
1332
1.26M
                WORD8 *pi1_sao_offset;
1333
1334
1.26M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
1335
1336
1.26M
                u1_sign_down = SIGN(pu1_src[col] - ((col < 2) ? pu1_src_left_cpy[2 * (row + 1) + col] :
1337
1.26M
                                                                pu1_src[col - 2 + src_strd]));
1338
1.26M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1339
1.26M
                if(col > 1)
1340
1.22M
                    au1_sign_up[col - 2] = -u1_sign_down;
1341
1342
1.26M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
1343
1344
1.26M
                if(0 != edge_idx)
1345
323k
                {
1346
323k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1347
323k
                }
1348
1.26M
            }
1349
1350
21.0k
            pu1_src += src_strd;
1351
21.0k
        }
1352
1353
765
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 2] = u1_pos_wd_0_tmp_u;
1354
765
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp_v;
1355
765
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp_u;
1356
765
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0) + 1] = u1_pos_0_ht_tmp_v;
1357
765
    }
1358
1359
765
    if(0 == pu1_avail[2])
1360
367
        ht++;
1361
765
    if(0 == pu1_avail[3])
1362
426
        ht++;
1363
765
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
1364
765
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
1365
44.5k
    for(row = 0; row < 2 * ht; row++)
1366
43.7k
    {
1367
43.7k
        pu1_src_left[row] = au1_src_left_tmp[row];
1368
43.7k
    }
1369
45.4k
    for(col = 0; col < wd; col++)
1370
44.6k
    {
1371
44.6k
        pu1_src_top[col] = au1_src_top_tmp[col];
1372
44.6k
    }
1373
1374
765
}