Coverage Report

Created: 2025-11-05 07:05

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
176k
#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
967
{
82
967
    WORD32 band_shift;
83
967
    WORD32 band_table[NUM_BAND_TABLE];
84
967
    WORD32 i;
85
967
    WORD32 row, col;
86
87
    /* Updating left and top and top-left */
88
32.2k
    for(row = 0; row < ht; row++)
89
31.3k
    {
90
31.3k
        pu1_src_left[row] = pu1_src[row * src_strd + (wd - 1)];
91
31.3k
    }
92
967
    pu1_src_top_left[0] = pu1_src_top[wd - 1];
93
33.2k
    for(col = 0; col < wd; col++)
94
32.2k
    {
95
32.2k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
96
32.2k
    }
97
98
967
    band_shift = BIT_DEPTH_LUMA - 5;
99
31.9k
    for(i = 0; i < NUM_BAND_TABLE; i++)
100
30.9k
    {
101
30.9k
        band_table[i] = 0;
102
30.9k
    }
103
4.83k
    for(i = 0; i < 4; i++)
104
3.86k
    {
105
3.86k
        band_table[(i + sao_band_pos) & 31] = i + 1;
106
3.86k
    }
107
108
32.2k
    for(row = 0; row < ht; row++)
109
31.3k
    {
110
1.07M
        for(col = 0; col < wd; col++)
111
1.04M
        {
112
1.04M
            WORD32 band_idx;
113
114
1.04M
            band_idx = band_table[pu1_src[col] >> band_shift];
115
1.04M
            pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[band_idx], 0, (1 << (band_shift + 5)) - 1);
116
1.04M
        }
117
31.3k
        pu1_src += src_strd;
118
31.3k
    }
119
967
}
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
4.42k
{
136
4.42k
    WORD32 band_shift;
137
4.42k
    WORD32 band_table_u[NUM_BAND_TABLE];
138
4.42k
    WORD32 band_table_v[NUM_BAND_TABLE];
139
4.42k
    WORD32 i;
140
4.42k
    WORD32 row, col;
141
142
    /* Updating left and top and top-left */
143
75.2k
    for(row = 0; row < ht; row++)
144
70.8k
    {
145
70.8k
        pu1_src_left[2 * row] = pu1_src[row * src_strd + (wd - 2)];
146
70.8k
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + (wd - 1)];
147
70.8k
    }
148
4.42k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
149
4.42k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
150
144k
    for(col = 0; col < wd; col++)
151
140k
    {
152
140k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
153
140k
    }
154
155
156
4.42k
    band_shift = BIT_DEPTH_CHROMA - 5;
157
144k
    for(i = 0; i < NUM_BAND_TABLE; i++)
158
140k
    {
159
140k
        band_table_u[i] = 0;
160
140k
        band_table_v[i] = 0;
161
140k
    }
162
21.9k
    for(i = 0; i < 4; i++)
163
17.5k
    {
164
17.5k
        band_table_u[(i + sao_band_pos_u) & 31] = i + 1;
165
17.5k
        band_table_v[(i + sao_band_pos_v) & 31] = i + 1;
166
17.5k
    }
167
168
74.3k
    for(row = 0; row < ht; row++)
169
69.8k
    {
170
2.26M
        for(col = 0; col < wd; col++)
171
2.19M
        {
172
2.19M
            WORD32 band_idx;
173
2.19M
            WORD8 *pi1_sao_offset;
174
175
2.19M
            pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
176
2.19M
            band_idx = (0 == col % 2) ? band_table_u[pu1_src[col] >> band_shift] : band_table_v[pu1_src[col] >> band_shift];
177
2.19M
            pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[band_idx], 0, (1 << (band_shift + 5)) - 1);
178
2.19M
        }
179
69.8k
        pu1_src += src_strd;
180
69.8k
    }
181
4.42k
}
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
40
{
198
40
    WORD32 row, col;
199
40
    UWORD8 au1_mask[MAX_CTB_SIZE];
200
40
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
201
40
    WORD8 u1_sign_left, u1_sign_right;
202
40
    WORD32 bit_depth;
203
40
    UNUSED(pu1_src_top_right);
204
40
    UNUSED(pu1_src_bot_left);
205
40
    bit_depth = BIT_DEPTH_LUMA;
206
207
    /* Initialize the mask values */
208
40
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
209
210
    /* Update top and top-left arrays */
211
40
    *pu1_src_top_left = pu1_src_top[wd - 1];
212
1.40k
    for(row = 0; row < ht; row++)
213
1.36k
    {
214
1.36k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
215
1.36k
    }
216
1.38k
    for(col = 0; col < wd; col++)
217
1.34k
    {
218
1.34k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
219
1.34k
    }
220
221
    /* Update masks based on the availability flags */
222
40
    if(0 == pu1_avail[0])
223
2
    {
224
2
        au1_mask[0] = 0;
225
2
    }
226
40
    if(0 == pu1_avail[1])
227
2
    {
228
2
        au1_mask[wd - 1] = 0;
229
2
    }
230
231
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
232
40
    {
233
1.40k
        for(row = 0; row < ht; row++)
234
1.36k
        {
235
1.36k
            u1_sign_left = SIGN(pu1_src[0] - pu1_src_left[row]);
236
46.9k
            for(col = 0; col < wd; col++)
237
45.5k
            {
238
45.5k
                WORD32 edge_idx;
239
240
45.5k
                u1_sign_right = SIGN(pu1_src[col] - pu1_src[col + 1]);
241
45.5k
                edge_idx = 2 + u1_sign_left + u1_sign_right;
242
45.5k
                u1_sign_left = -u1_sign_right;
243
244
45.5k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
245
246
45.5k
                if(0 != edge_idx)
247
5.09k
                {
248
5.09k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
249
5.09k
                }
250
45.5k
            }
251
252
1.36k
            pu1_src += src_strd;
253
1.36k
        }
254
40
    }
255
256
    /* Update left array */
257
1.40k
    for(row = 0; row < ht; row++)
258
1.36k
    {
259
1.36k
        pu1_src_left[row] = au1_src_left_tmp[row];
260
1.36k
    }
261
262
40
}
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
64
{
281
64
    WORD32 row, col;
282
64
    UWORD8 au1_mask[MAX_CTB_SIZE];
283
64
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE];
284
64
    WORD8 u1_sign_left_u, u1_sign_right_u;
285
64
    WORD8 u1_sign_left_v, u1_sign_right_v;
286
64
    WORD32 bit_depth;
287
64
    UNUSED(pu1_src_top_right);
288
64
    UNUSED(pu1_src_bot_left);
289
64
    bit_depth = BIT_DEPTH_CHROMA;
290
291
    /* Initialize the mask values */
292
64
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
293
294
    /* Update left, top and top-left arrays */
295
64
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
296
64
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
297
1.11k
    for(row = 0; row < ht; row++)
298
1.04k
    {
299
1.04k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
300
1.04k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
301
1.04k
    }
302
2.30k
    for(col = 0; col < wd; col++)
303
2.24k
    {
304
2.24k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
305
2.24k
    }
306
307
    /* Update masks based on the availability flags */
308
64
    if(0 == pu1_avail[0])
309
12
    {
310
12
        au1_mask[0] = 0;
311
12
    }
312
64
    if(0 == pu1_avail[1])
313
6
    {
314
6
        au1_mask[(wd - 1) >> 1] = 0;
315
6
    }
316
317
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
318
64
    {
319
1.11k
        for(row = 0; row < ht; row++)
320
1.04k
        {
321
1.04k
            u1_sign_left_u = SIGN(pu1_src[0] - pu1_src_left[2 * row]);
322
1.04k
            u1_sign_left_v = SIGN(pu1_src[1] - pu1_src_left[2 * row + 1]);
323
37.6k
            for(col = 0; col < wd; col++)
324
36.6k
            {
325
36.6k
                WORD32 edge_idx;
326
36.6k
                WORD8 *pi1_sao_offset;
327
328
36.6k
                if(0 == col % 2)
329
18.3k
                {
330
18.3k
                    pi1_sao_offset = pi1_sao_offset_u;
331
18.3k
                    u1_sign_right_u = SIGN(pu1_src[col] - pu1_src[col + 2]);
332
18.3k
                    edge_idx = 2 + u1_sign_left_u + u1_sign_right_u;
333
18.3k
                    u1_sign_left_u = -u1_sign_right_u;
334
18.3k
                }
335
18.3k
                else
336
18.3k
                {
337
18.3k
                    pi1_sao_offset = pi1_sao_offset_v;
338
18.3k
                    u1_sign_right_v = SIGN(pu1_src[col] - pu1_src[col + 2]);
339
18.3k
                    edge_idx = 2 + u1_sign_left_v + u1_sign_right_v;
340
18.3k
                    u1_sign_left_v = -u1_sign_right_v;
341
18.3k
                }
342
343
36.6k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
344
345
36.6k
                if(0 != edge_idx)
346
11.1k
                {
347
11.1k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
348
11.1k
                }
349
36.6k
            }
350
351
1.04k
            pu1_src += src_strd;
352
1.04k
        }
353
64
    }
354
355
2.16k
    for(row = 0; row < 2 * ht; row++)
356
2.09k
    {
357
2.09k
        pu1_src_left[row] = au1_src_left_tmp[row];
358
2.09k
    }
359
360
64
}
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
68
{
377
68
    WORD32 row, col;
378
68
    UWORD8 au1_mask[MAX_CTB_SIZE];
379
68
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
380
68
    WORD8 au1_sign_up[MAX_CTB_SIZE];
381
68
    WORD8 u1_sign_down;
382
68
    WORD32 bit_depth;
383
68
    UNUSED(pu1_src_top_right);
384
68
    UNUSED(pu1_src_bot_left);
385
386
68
    bit_depth = BIT_DEPTH_LUMA;
387
388
    /* Initialize the mask values */
389
68
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
390
391
    /* Update left, top and top-left arrays */
392
68
    *pu1_src_top_left = pu1_src_top[wd - 1];
393
2.29k
    for(row = 0; row < ht; row++)
394
2.22k
    {
395
2.22k
        pu1_src_left[row] = pu1_src[row * src_strd + wd - 1];
396
2.22k
    }
397
2.56k
    for(col = 0; col < wd; col++)
398
2.49k
    {
399
2.49k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
400
2.49k
    }
401
402
    /* Update height and source pointers based on the availability flags */
403
68
    if(0 == pu1_avail[2])
404
7
    {
405
7
        pu1_src += src_strd;
406
7
        ht--;
407
327
        for(col = 0; col < wd; col++)
408
320
        {
409
320
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
410
320
        }
411
7
    }
412
61
    else
413
61
    {
414
2.23k
        for(col = 0; col < wd; col++)
415
2.17k
        {
416
2.17k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
417
2.17k
        }
418
61
    }
419
68
    if(0 == pu1_avail[3])
420
6
    {
421
6
        ht--;
422
6
    }
423
424
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
425
68
    {
426
2.27k
        for(row = 0; row < ht; row++)
427
2.21k
        {
428
83.1k
            for(col = 0; col < wd; col++)
429
80.8k
            {
430
80.8k
                WORD32 edge_idx;
431
432
80.8k
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
433
80.8k
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
434
80.8k
                au1_sign_up[col] = -u1_sign_down;
435
436
80.8k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
437
438
80.8k
                if(0 != edge_idx)
439
19.0k
                {
440
19.0k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
441
19.0k
                }
442
80.8k
            }
443
444
2.21k
            pu1_src += src_strd;
445
2.21k
        }
446
68
    }
447
448
2.56k
    for(col = 0; col < wd; col++)
449
2.49k
    {
450
2.49k
        pu1_src_top[col] = au1_src_top_tmp[col];
451
2.49k
    }
452
453
68
}
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
62
{
471
62
    WORD32 row, col;
472
62
    UWORD8 au1_mask[MAX_CTB_SIZE];
473
62
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
474
62
    WORD8 au1_sign_up[MAX_CTB_SIZE];
475
62
    WORD8 u1_sign_down;
476
62
    WORD32 bit_depth;
477
62
    UNUSED(pu1_src_top_right);
478
62
    UNUSED(pu1_src_bot_left);
479
480
62
    bit_depth = BIT_DEPTH_CHROMA;
481
482
    /* Initialize the mask values */
483
62
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
484
485
    /* Update left, top and top-left arrays */
486
62
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
487
62
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
488
1.06k
    for(row = 0; row < ht; row++)
489
1.00k
    {
490
1.00k
        pu1_src_left[2 * row] = pu1_src[row * src_strd + wd - 2];
491
1.00k
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
492
1.00k
    }
493
2.17k
    for(col = 0; col < wd; col++)
494
2.11k
    {
495
2.11k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
496
2.11k
    }
497
498
    /* Update height and source pointers based on the availability flags */
499
62
    if(0 == pu1_avail[2])
500
11
    {
501
11
        pu1_src += src_strd;
502
11
        ht--;
503
395
        for(col = 0; col < wd; col++)
504
384
        {
505
384
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
506
384
        }
507
11
    }
508
51
    else
509
51
    {
510
1.77k
        for(col = 0; col < wd; col++)
511
1.72k
        {
512
1.72k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
513
1.72k
        }
514
51
    }
515
62
    if(0 == pu1_avail[3])
516
2
    {
517
2
        ht--;
518
2
    }
519
520
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
521
62
    {
522
1.04k
        for(row = 0; row < ht; row++)
523
987
        {
524
34.5k
            for(col = 0; col < wd; col++)
525
33.6k
            {
526
33.6k
                WORD32 edge_idx;
527
33.6k
                WORD8 *pi1_sao_offset;
528
529
33.6k
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
530
531
33.6k
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
532
33.6k
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
533
33.6k
                au1_sign_up[col] = -u1_sign_down;
534
535
33.6k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
536
537
33.6k
                if(0 != edge_idx)
538
9.61k
                {
539
9.61k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
540
9.61k
                }
541
33.6k
            }
542
543
987
            pu1_src += src_strd;
544
987
        }
545
62
    }
546
547
2.17k
    for(col = 0; col < wd; col++)
548
2.11k
    {
549
2.11k
        pu1_src_top[col] = au1_src_top_tmp[col];
550
2.11k
    }
551
552
62
}
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
40
{
569
40
    WORD32 row, col;
570
40
    UWORD8 au1_mask[MAX_CTB_SIZE];
571
40
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
572
40
    UWORD8 u1_src_top_left_tmp;
573
40
    WORD8 au1_sign_up[MAX_CTB_SIZE + 1], au1_sign_up_tmp[MAX_CTB_SIZE + 1];
574
40
    WORD8 u1_sign_down;
575
40
    WORD8 *pu1_sign_up;
576
40
    WORD8 *pu1_sign_up_tmp;
577
40
    UWORD8 *pu1_src_left_cpy;
578
579
40
    WORD32 bit_depth;
580
40
    UWORD8 u1_pos_0_0_tmp;
581
40
    UWORD8 u1_pos_wd_ht_tmp;
582
40
    UNUSED(pu1_src_top_right);
583
40
    UNUSED(pu1_src_bot_left);
584
585
40
    bit_depth = BIT_DEPTH_LUMA;
586
40
    pu1_sign_up = au1_sign_up;
587
40
    pu1_sign_up_tmp = au1_sign_up_tmp;
588
40
    pu1_src_left_cpy = pu1_src_left;
589
590
    /* Initialize the mask values */
591
40
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
592
593
    /* Update left, top and top-left arrays */
594
40
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
595
1.32k
    for(row = 0; row < ht; row++)
596
1.28k
    {
597
1.28k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
598
1.28k
    }
599
1.44k
    for(col = 0; col < wd; col++)
600
1.40k
    {
601
1.40k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
602
1.40k
    }
603
604
605
    /* If top-left is available, process separately */
606
40
    if(0 != pu1_avail[4])
607
31
    {
608
31
        WORD32 edge_idx;
609
610
31
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
611
31
                        SIGN(pu1_src[0] - pu1_src[1 + src_strd]);
612
613
31
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
614
615
31
        if(0 != edge_idx)
616
6
        {
617
6
            u1_pos_0_0_tmp = CLIP3(pu1_src[0] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
618
6
        }
619
25
        else
620
25
        {
621
25
            u1_pos_0_0_tmp = pu1_src[0];
622
25
        }
623
31
    }
624
9
    else
625
9
    {
626
9
        u1_pos_0_0_tmp = pu1_src[0];
627
9
    }
628
629
    /* If bottom-right is available, process separately */
630
40
    if(0 != pu1_avail[7])
631
36
    {
632
36
        WORD32 edge_idx;
633
634
36
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 1 - src_strd]) +
635
36
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 1 + src_strd]);
636
637
36
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
638
639
36
        if(0 != edge_idx)
640
14
        {
641
14
            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
14
        }
643
22
        else
644
22
        {
645
22
            u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
646
22
        }
647
36
    }
648
4
    else
649
4
    {
650
4
        u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
651
4
    }
652
653
    /* If Left is not available */
654
40
    if(0 == pu1_avail[0])
655
2
    {
656
2
        au1_mask[0] = 0;
657
2
    }
658
659
    /* If Top is not available */
660
40
    if(0 == pu1_avail[2])
661
8
    {
662
8
        pu1_src += src_strd;
663
8
        ht--;
664
8
        pu1_src_left_cpy += 1;
665
256
        for(col = 1; col < wd; col++)
666
248
        {
667
248
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 1 - src_strd]);
668
248
        }
669
8
    }
670
32
    else
671
32
    {
672
1.15k
        for(col = 1; col < wd; col++)
673
1.12k
        {
674
1.12k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 1]);
675
1.12k
        }
676
32
    }
677
678
    /* If Right is not available */
679
40
    if(0 == pu1_avail[1])
680
4
    {
681
4
        au1_mask[wd - 1] = 0;
682
4
    }
683
684
    /* If Bottom is not available */
685
40
    if(0 == pu1_avail[3])
686
0
    {
687
0
        ht--;
688
0
    }
689
690
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
691
40
    {
692
1.31k
        for(row = 0; row < ht; row++)
693
1.27k
        {
694
1.27k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[row - 1]);
695
46.0k
            for(col = 0; col < wd; col++)
696
44.8k
            {
697
44.8k
                WORD32 edge_idx;
698
699
44.8k
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 1 + src_strd]);
700
44.8k
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
701
44.8k
                pu1_sign_up_tmp[col + 1] = -u1_sign_down;
702
703
44.8k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
704
705
44.8k
                if(0 != edge_idx)
706
13.9k
                {
707
13.9k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
708
13.9k
                }
709
44.8k
            }
710
711
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
712
1.27k
            {
713
1.27k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
714
1.27k
                pu1_sign_up = pu1_sign_up_tmp;
715
1.27k
                pu1_sign_up_tmp = pu1_swap_tmp;
716
1.27k
            }
717
718
1.27k
            pu1_src += src_strd;
719
1.27k
        }
720
721
40
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp;
722
40
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp;
723
40
    }
724
725
40
    if(0 == pu1_avail[2])
726
8
        ht++;
727
40
    if(0 == pu1_avail[3])
728
0
        ht++;
729
40
    *pu1_src_top_left = u1_src_top_left_tmp;
730
1.32k
    for(row = 0; row < ht; row++)
731
1.28k
    {
732
1.28k
        pu1_src_left[row] = au1_src_left_tmp[row];
733
1.28k
    }
734
1.44k
    for(col = 0; col < wd; col++)
735
1.40k
    {
736
1.40k
        pu1_src_top[col] = au1_src_top_tmp[col];
737
1.40k
    }
738
739
40
}
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
49
{
758
49
    WORD32 row, col;
759
49
    UWORD8 au1_mask[MAX_CTB_SIZE];
760
49
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
761
49
    UWORD8 au1_src_top_left_tmp[2];
762
49
    WORD8 au1_sign_up[MAX_CTB_SIZE + 2], au1_sign_up_tmp[MAX_CTB_SIZE + 2];
763
49
    WORD8 u1_sign_down;
764
49
    WORD8 *pu1_sign_up;
765
49
    WORD8 *pu1_sign_up_tmp;
766
49
    UWORD8 *pu1_src_left_cpy;
767
768
49
    WORD32 bit_depth;
769
770
49
    UWORD8 u1_pos_0_0_tmp_u;
771
49
    UWORD8 u1_pos_0_0_tmp_v;
772
49
    UWORD8 u1_pos_wd_ht_tmp_u;
773
49
    UWORD8 u1_pos_wd_ht_tmp_v;
774
49
    UNUSED(pu1_src_top_right);
775
49
    UNUSED(pu1_src_bot_left);
776
777
778
49
    bit_depth = BIT_DEPTH_CHROMA;
779
49
    pu1_sign_up = au1_sign_up;
780
49
    pu1_sign_up_tmp = au1_sign_up_tmp;
781
49
    pu1_src_left_cpy = pu1_src_left;
782
783
    /* Initialize the mask values */
784
49
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
785
786
    /* Update left, top and top-left arrays */
787
49
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
788
49
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
789
837
    for(row = 0; row < ht; row++)
790
788
    {
791
788
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
792
788
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
793
788
    }
794
1.77k
    for(col = 0; col < wd; col++)
795
1.72k
    {
796
1.72k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
797
1.72k
    }
798
799
800
    /* If top-left is available, process separately */
801
49
    if(0 != pu1_avail[4])
802
43
    {
803
43
        WORD32 edge_idx;
804
805
        /* U */
806
43
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
807
43
                        SIGN(pu1_src[0] - pu1_src[2 + src_strd]);
808
809
43
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
810
811
43
        if(0 != edge_idx)
812
16
        {
813
16
            u1_pos_0_0_tmp_u = CLIP3(pu1_src[0] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
814
16
        }
815
27
        else
816
27
        {
817
27
            u1_pos_0_0_tmp_u = pu1_src[0];
818
27
        }
819
820
        /* V */
821
43
        edge_idx = 2 + SIGN(pu1_src[1] - pu1_src_top_left[1]) +
822
43
                        SIGN(pu1_src[1] - pu1_src[1 + 2 + src_strd]);
823
824
43
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
825
826
43
        if(0 != edge_idx)
827
20
        {
828
20
            u1_pos_0_0_tmp_v = CLIP3(pu1_src[1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
829
20
        }
830
23
        else
831
23
        {
832
23
            u1_pos_0_0_tmp_v = pu1_src[1];
833
23
        }
834
43
    }
835
6
    else
836
6
    {
837
6
        u1_pos_0_0_tmp_u = pu1_src[0];
838
6
        u1_pos_0_0_tmp_v = pu1_src[1];
839
6
    }
840
841
    /* If bottom-right is available, process separately */
842
49
    if(0 != pu1_avail[7])
843
44
    {
844
44
        WORD32 edge_idx;
845
846
        /* U */
847
44
        edge_idx = 2 + SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd - 2 - src_strd]) +
848
44
                        SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd + 2 + src_strd]);
849
850
44
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
851
852
44
        if(0 != edge_idx)
853
19
        {
854
19
            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
19
        }
856
25
        else
857
25
        {
858
25
            u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
859
25
        }
860
861
        /* V */
862
44
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 2 - src_strd]) +
863
44
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 2 + src_strd]);
864
865
44
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
866
867
44
        if(0 != edge_idx)
868
13
        {
869
13
            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
13
        }
871
31
        else
872
31
        {
873
31
            u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
874
31
        }
875
44
    }
876
5
    else
877
5
    {
878
5
        u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
879
5
        u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
880
5
    }
881
882
    /* If Left is not available */
883
49
    if(0 == pu1_avail[0])
884
4
    {
885
4
        au1_mask[0] = 0;
886
4
    }
887
888
    /* If Top is not available */
889
49
    if(0 == pu1_avail[2])
890
2
    {
891
2
        pu1_src += src_strd;
892
2
        pu1_src_left_cpy += 2;
893
2
        ht--;
894
62
        for(col = 2; col < wd; col++)
895
60
        {
896
60
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 2 - src_strd]);
897
60
        }
898
2
    }
899
47
    else
900
47
    {
901
1.61k
        for(col = 2; col < wd; col++)
902
1.57k
        {
903
1.57k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 2]);
904
1.57k
        }
905
47
    }
906
907
    /* If Right is not available */
908
49
    if(0 == pu1_avail[1])
909
5
    {
910
5
        au1_mask[(wd - 1) >> 1] = 0;
911
5
    }
912
913
    /* If Bottom is not available */
914
49
    if(0 == pu1_avail[3])
915
1
    {
916
1
        ht--;
917
1
    }
918
919
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
920
49
    {
921
834
        for(row = 0; row < ht; row++)
922
785
        {
923
785
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[2 * (row - 1)]);
924
785
            pu1_sign_up[1] = SIGN(pu1_src[1] - pu1_src_left_cpy[2 * (row - 1) + 1]);
925
28.5k
            for(col = 0; col < wd; col++)
926
27.7k
            {
927
27.7k
                WORD32 edge_idx;
928
27.7k
                WORD8 *pi1_sao_offset;
929
930
27.7k
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
931
932
27.7k
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 2 + src_strd]);
933
27.7k
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
934
27.7k
                pu1_sign_up_tmp[col + 2] = -u1_sign_down;
935
936
27.7k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
937
938
27.7k
                if(0 != edge_idx)
939
9.65k
                {
940
9.65k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
941
9.65k
                }
942
27.7k
            }
943
944
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
945
785
            {
946
785
                WORD8 *pu1_swap_tmp = pu1_sign_up;
947
785
                pu1_sign_up = pu1_sign_up_tmp;
948
785
                pu1_sign_up_tmp = pu1_swap_tmp;
949
785
            }
950
951
785
            pu1_src += src_strd;
952
785
        }
953
954
49
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp_u;
955
49
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + 1] = u1_pos_0_0_tmp_v;
956
49
        pu1_src[(pu1_avail[3] ? wd - 2 - src_strd : wd - 2)] = u1_pos_wd_ht_tmp_u;
957
49
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp_v;
958
49
    }
959
960
49
    if(0 == pu1_avail[2])
961
2
        ht++;
962
49
    if(0 == pu1_avail[3])
963
1
        ht++;
964
49
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
965
49
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
966
1.62k
    for(row = 0; row < 2 * ht; row++)
967
1.57k
    {
968
1.57k
        pu1_src_left[row] = au1_src_left_tmp[row];
969
1.57k
    }
970
1.77k
    for(col = 0; col < wd; col++)
971
1.72k
    {
972
1.72k
        pu1_src_top[col] = au1_src_top_tmp[col];
973
1.72k
    }
974
975
49
}
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
68
{
993
68
    WORD32 row, col;
994
68
    UWORD8 au1_mask[MAX_CTB_SIZE];
995
68
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
996
68
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
997
68
    UWORD8 u1_src_top_left_tmp;
998
68
    WORD8 au1_sign_up[MAX_CTB_SIZE];
999
68
    UWORD8 *pu1_src_left_cpy;
1000
68
    WORD8 u1_sign_down;
1001
68
    WORD32 bit_depth;
1002
1003
68
    UWORD8 u1_pos_0_ht_tmp;
1004
68
    UWORD8 u1_pos_wd_0_tmp;
1005
1006
68
    bit_depth = BIT_DEPTH_LUMA;
1007
68
    pu1_src_left_cpy = pu1_src_left;
1008
1009
    /* Initialize the mask values */
1010
68
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1011
1012
    /* Update left, top and top-left arrays */
1013
68
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
1014
2.27k
    for(row = 0; row < ht; row++)
1015
2.20k
    {
1016
2.20k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
1017
2.20k
    }
1018
2.50k
    for(col = 0; col < wd; col++)
1019
2.43k
    {
1020
2.43k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1021
2.43k
    }
1022
1023
    /* If top-right is available, process separately */
1024
68
    if(0 != pu1_avail[5])
1025
44
    {
1026
44
        WORD32 edge_idx;
1027
1028
44
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[0]) +
1029
44
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 1 + src_strd]);
1030
1031
44
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1032
1033
44
        if(0 != edge_idx)
1034
16
        {
1035
16
            u1_pos_wd_0_tmp = CLIP3(pu1_src[wd - 1] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1036
16
        }
1037
28
        else
1038
28
        {
1039
28
            u1_pos_wd_0_tmp = pu1_src[wd - 1];
1040
28
        }
1041
44
    }
1042
24
    else
1043
24
    {
1044
24
        u1_pos_wd_0_tmp = pu1_src[wd - 1];
1045
24
    }
1046
1047
    /* If bottom-left is available, process separately */
1048
68
    if(0 != pu1_avail[6])
1049
56
    {
1050
56
        WORD32 edge_idx;
1051
1052
56
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 1 - src_strd]) +
1053
56
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1054
1055
56
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1056
1057
56
        if(0 != edge_idx)
1058
16
        {
1059
16
            u1_pos_0_ht_tmp = CLIP3(pu1_src[(ht - 1) * src_strd] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1060
16
        }
1061
40
        else
1062
40
        {
1063
40
            u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1064
40
        }
1065
56
    }
1066
12
    else
1067
12
    {
1068
12
        u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1069
12
    }
1070
1071
    /* If Left is not available */
1072
68
    if(0 == pu1_avail[0])
1073
8
    {
1074
8
        au1_mask[0] = 0;
1075
8
    }
1076
1077
    /* If Top is not available */
1078
68
    if(0 == pu1_avail[2])
1079
18
    {
1080
18
        pu1_src += src_strd;
1081
18
        ht--;
1082
18
        pu1_src_left_cpy += 1;
1083
640
        for(col = 0; col < wd - 1; col++)
1084
622
        {
1085
622
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 1 - src_strd]);
1086
622
        }
1087
18
    }
1088
50
    else
1089
50
    {
1090
1.79k
        for(col = 0; col < wd - 1; col++)
1091
1.74k
        {
1092
1.74k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 1]);
1093
1.74k
        }
1094
50
    }
1095
1096
    /* If Right is not available */
1097
68
    if(0 == pu1_avail[1])
1098
8
    {
1099
8
        au1_mask[wd - 1] = 0;
1100
8
    }
1101
1102
    /* If Bottom is not available */
1103
68
    if(0 == pu1_avail[3])
1104
4
    {
1105
4
        ht--;
1106
4
    }
1107
1108
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1109
68
    {
1110
2.25k
        for(row = 0; row < ht; row++)
1111
2.18k
        {
1112
2.18k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 1 - src_strd]);
1113
80.2k
            for(col = 0; col < wd; col++)
1114
78.0k
            {
1115
78.0k
                WORD32 edge_idx;
1116
1117
78.0k
                u1_sign_down = SIGN(pu1_src[col] - ((col == 0) ? pu1_src_left_cpy[row + 1] :
1118
78.0k
                                                                 pu1_src[col - 1 + src_strd]));
1119
78.0k
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1120
78.0k
                if(col > 0)
1121
75.8k
                    au1_sign_up[col - 1] = -u1_sign_down;
1122
1123
78.0k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
1124
1125
78.0k
                if(0 != edge_idx)
1126
24.2k
                {
1127
24.2k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1128
24.2k
                }
1129
78.0k
            }
1130
1131
2.18k
            pu1_src += src_strd;
1132
2.18k
        }
1133
1134
68
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp;
1135
68
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp;
1136
68
    }
1137
1138
68
    if(0 == pu1_avail[2])
1139
18
        ht++;
1140
68
    if(0 == pu1_avail[3])
1141
4
        ht++;
1142
68
    *pu1_src_top_left = u1_src_top_left_tmp;
1143
2.27k
    for(row = 0; row < ht; row++)
1144
2.20k
    {
1145
2.20k
        pu1_src_left[row] = au1_src_left_tmp[row];
1146
2.20k
    }
1147
2.50k
    for(col = 0; col < wd; col++)
1148
2.43k
    {
1149
2.43k
        pu1_src_top[col] = au1_src_top_tmp[col];
1150
2.43k
    }
1151
1152
68
}
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
56
{
1170
56
    WORD32 row, col;
1171
56
    UWORD8 au1_mask[MAX_CTB_SIZE];
1172
56
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
1173
56
    UWORD8 au1_src_top_left_tmp[2];
1174
56
    WORD8 au1_sign_up[MAX_CTB_SIZE];
1175
56
    UWORD8 *pu1_src_left_cpy;
1176
56
    WORD8 u1_sign_down;
1177
56
    WORD32 bit_depth;
1178
1179
56
    UWORD8 u1_pos_wd_0_tmp_u;
1180
56
    UWORD8 u1_pos_wd_0_tmp_v;
1181
56
    UWORD8 u1_pos_0_ht_tmp_u;
1182
56
    UWORD8 u1_pos_0_ht_tmp_v;
1183
1184
56
    bit_depth = BIT_DEPTH_CHROMA;
1185
56
    pu1_src_left_cpy = pu1_src_left;
1186
1187
    /* Initialize the mask values */
1188
56
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1189
1190
    /* Update left, top and top-left arrays */
1191
56
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
1192
56
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
1193
984
    for(row = 0; row < ht; row++)
1194
928
    {
1195
928
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
1196
928
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
1197
928
    }
1198
1.97k
    for(col = 0; col < wd; col++)
1199
1.92k
    {
1200
1.92k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1201
1.92k
    }
1202
1203
1204
    /* If top-right is available, process separately */
1205
56
    if(0 != pu1_avail[5])
1206
48
    {
1207
48
        WORD32 edge_idx;
1208
1209
        /* U */
1210
48
        edge_idx = 2 + SIGN(pu1_src[wd - 2] - pu1_src_top_right[0]) +
1211
48
                        SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 - 2 + src_strd]);
1212
1213
48
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1214
1215
48
        if(0 != edge_idx)
1216
17
        {
1217
17
            u1_pos_wd_0_tmp_u = CLIP3(pu1_src[wd - 2] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
1218
17
        }
1219
31
        else
1220
31
        {
1221
31
            u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1222
31
        }
1223
1224
        /* V */
1225
48
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[1]) +
1226
48
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 2 + src_strd]);
1227
1228
48
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1229
1230
48
        if(0 != edge_idx)
1231
18
        {
1232
18
            u1_pos_wd_0_tmp_v = CLIP3(pu1_src[wd - 1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
1233
18
        }
1234
30
        else
1235
30
        {
1236
30
            u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1237
30
        }
1238
48
    }
1239
8
    else
1240
8
    {
1241
8
        u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1242
8
        u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1243
8
    }
1244
1245
    /* If bottom-left is available, process separately */
1246
56
    if(0 != pu1_avail[6])
1247
42
    {
1248
42
        WORD32 edge_idx;
1249
1250
        /* U */
1251
42
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 2 - src_strd]) +
1252
42
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1253
1254
42
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1255
1256
42
        if(0 != edge_idx)
1257
15
        {
1258
15
            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
15
        }
1260
27
        else
1261
27
        {
1262
27
            u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1263
27
        }
1264
1265
        /* V */
1266
42
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src[(ht - 1) * src_strd + 1 + 2 - src_strd]) +
1267
42
                        SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src_bot_left[1]);
1268
1269
42
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1270
1271
42
        if(0 != edge_idx)
1272
13
        {
1273
13
            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
13
        }
1275
29
        else
1276
29
        {
1277
29
            u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1278
29
        }
1279
42
    }
1280
14
    else
1281
14
    {
1282
14
        u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1283
14
        u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1284
14
    }
1285
1286
    /* If Left is not available */
1287
56
    if(0 == pu1_avail[0])
1288
6
    {
1289
6
        au1_mask[0] = 0;
1290
6
    }
1291
1292
    /* If Top is not available */
1293
56
    if(0 == pu1_avail[2])
1294
5
    {
1295
5
        pu1_src += src_strd;
1296
5
        ht--;
1297
5
        pu1_src_left_cpy += 2;
1298
187
        for(col = 0; col < wd - 2; col++)
1299
182
        {
1300
182
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 2 - src_strd]);
1301
182
        }
1302
5
    }
1303
51
    else
1304
51
    {
1305
1.67k
        for(col = 0; col < wd - 2; col++)
1306
1.62k
        {
1307
1.62k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 2]);
1308
1.62k
        }
1309
51
    }
1310
1311
    /* If Right is not available */
1312
56
    if(0 == pu1_avail[1])
1313
4
    {
1314
4
        au1_mask[(wd - 1) >> 1] = 0;
1315
4
    }
1316
1317
    /* If Bottom is not available */
1318
56
    if(0 == pu1_avail[3])
1319
8
    {
1320
8
        ht--;
1321
8
    }
1322
1323
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1324
56
    {
1325
971
        for(row = 0; row < ht; row++)
1326
915
        {
1327
915
            au1_sign_up[wd - 2] = SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 + 2 - src_strd]);
1328
915
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 2 - src_strd]);
1329
32.2k
            for(col = 0; col < wd; col++)
1330
31.2k
            {
1331
31.2k
                WORD32 edge_idx;
1332
31.2k
                WORD8 *pi1_sao_offset;
1333
1334
31.2k
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
1335
1336
31.2k
                u1_sign_down = SIGN(pu1_src[col] - ((col < 2) ? pu1_src_left_cpy[2 * (row + 1) + col] :
1337
31.2k
                                                                pu1_src[col - 2 + src_strd]));
1338
31.2k
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1339
31.2k
                if(col > 1)
1340
29.4k
                    au1_sign_up[col - 2] = -u1_sign_down;
1341
1342
31.2k
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
1343
1344
31.2k
                if(0 != edge_idx)
1345
8.79k
                {
1346
8.79k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1347
8.79k
                }
1348
31.2k
            }
1349
1350
915
            pu1_src += src_strd;
1351
915
        }
1352
1353
56
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 2] = u1_pos_wd_0_tmp_u;
1354
56
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp_v;
1355
56
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp_u;
1356
56
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0) + 1] = u1_pos_0_ht_tmp_v;
1357
56
    }
1358
1359
56
    if(0 == pu1_avail[2])
1360
5
        ht++;
1361
56
    if(0 == pu1_avail[3])
1362
8
        ht++;
1363
56
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
1364
56
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
1365
1.91k
    for(row = 0; row < 2 * ht; row++)
1366
1.85k
    {
1367
1.85k
        pu1_src_left[row] = au1_src_left_tmp[row];
1368
1.85k
    }
1369
1.97k
    for(col = 0; col < wd; col++)
1370
1.92k
    {
1371
1.92k
        pu1_src_top[col] = au1_src_top_tmp[col];
1372
1.92k
    }
1373
1374
56
}