Coverage Report

Created: 2026-03-20 07:15

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
17.6k
{
198
17.6k
    WORD32 row, col;
199
17.6k
    UWORD8 au1_mask[MAX_CTB_SIZE];
200
17.6k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
201
17.6k
    WORD8 u1_sign_left, u1_sign_right;
202
17.6k
    WORD32 bit_depth;
203
17.6k
    UNUSED(pu1_src_top_right);
204
17.6k
    UNUSED(pu1_src_bot_left);
205
17.6k
    bit_depth = BIT_DEPTH_LUMA;
206
207
    /* Initialize the mask values */
208
17.6k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
209
210
    /* Update top and top-left arrays */
211
17.6k
    *pu1_src_top_left = pu1_src_top[wd - 1];
212
1.07M
    for(row = 0; row < ht; row++)
213
1.05M
    {
214
1.05M
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
215
1.05M
    }
216
1.03M
    for(col = 0; col < wd; col++)
217
1.01M
    {
218
1.01M
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
219
1.01M
    }
220
221
    /* Update masks based on the availability flags */
222
17.6k
    if(0 == pu1_avail[0])
223
5.21k
    {
224
5.21k
        au1_mask[0] = 0;
225
5.21k
    }
226
17.6k
    if(0 == pu1_avail[1])
227
5.92k
    {
228
5.92k
        au1_mask[wd - 1] = 0;
229
5.92k
    }
230
231
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
232
17.6k
    {
233
1.07M
        for(row = 0; row < ht; row++)
234
1.05M
        {
235
1.05M
            u1_sign_left = SIGN(pu1_src[0] - pu1_src_left[row]);
236
62.1M
            for(col = 0; col < wd; col++)
237
61.0M
            {
238
61.0M
                WORD32 edge_idx;
239
240
61.0M
                u1_sign_right = SIGN(pu1_src[col] - pu1_src[col + 1]);
241
61.0M
                edge_idx = 2 + u1_sign_left + u1_sign_right;
242
61.0M
                u1_sign_left = -u1_sign_right;
243
244
61.0M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
245
246
61.0M
                if(0 != edge_idx)
247
18.7M
                {
248
18.7M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
249
18.7M
                }
250
61.0M
            }
251
252
1.05M
            pu1_src += src_strd;
253
1.05M
        }
254
17.6k
    }
255
256
    /* Update left array */
257
1.07M
    for(row = 0; row < ht; row++)
258
1.05M
    {
259
1.05M
        pu1_src_left[row] = au1_src_left_tmp[row];
260
1.05M
    }
261
262
17.6k
}
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
10.0k
{
281
10.0k
    WORD32 row, col;
282
10.0k
    UWORD8 au1_mask[MAX_CTB_SIZE];
283
10.0k
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE];
284
10.0k
    WORD8 u1_sign_left_u, u1_sign_right_u;
285
10.0k
    WORD8 u1_sign_left_v, u1_sign_right_v;
286
10.0k
    WORD32 bit_depth;
287
10.0k
    UNUSED(pu1_src_top_right);
288
10.0k
    UNUSED(pu1_src_bot_left);
289
10.0k
    bit_depth = BIT_DEPTH_CHROMA;
290
291
    /* Initialize the mask values */
292
10.0k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
293
294
    /* Update left, top and top-left arrays */
295
10.0k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
296
10.0k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
297
312k
    for(row = 0; row < ht; row++)
298
301k
    {
299
301k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
300
301k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
301
301k
    }
302
580k
    for(col = 0; col < wd; col++)
303
570k
    {
304
570k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
305
570k
    }
306
307
    /* Update masks based on the availability flags */
308
10.0k
    if(0 == pu1_avail[0])
309
2.46k
    {
310
2.46k
        au1_mask[0] = 0;
311
2.46k
    }
312
10.0k
    if(0 == pu1_avail[1])
313
3.18k
    {
314
3.18k
        au1_mask[(wd - 1) >> 1] = 0;
315
3.18k
    }
316
317
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
318
10.0k
    {
319
312k
        for(row = 0; row < ht; row++)
320
301k
        {
321
301k
            u1_sign_left_u = SIGN(pu1_src[0] - pu1_src_left[2 * row]);
322
301k
            u1_sign_left_v = SIGN(pu1_src[1] - pu1_src_left[2 * row + 1]);
323
17.5M
            for(col = 0; col < wd; col++)
324
17.2M
            {
325
17.2M
                WORD32 edge_idx;
326
17.2M
                WORD8 *pi1_sao_offset;
327
328
17.2M
                if(0 == col % 2)
329
8.63M
                {
330
8.63M
                    pi1_sao_offset = pi1_sao_offset_u;
331
8.63M
                    u1_sign_right_u = SIGN(pu1_src[col] - pu1_src[col + 2]);
332
8.63M
                    edge_idx = 2 + u1_sign_left_u + u1_sign_right_u;
333
8.63M
                    u1_sign_left_u = -u1_sign_right_u;
334
8.63M
                }
335
8.63M
                else
336
8.63M
                {
337
8.63M
                    pi1_sao_offset = pi1_sao_offset_v;
338
8.63M
                    u1_sign_right_v = SIGN(pu1_src[col] - pu1_src[col + 2]);
339
8.63M
                    edge_idx = 2 + u1_sign_left_v + u1_sign_right_v;
340
8.63M
                    u1_sign_left_v = -u1_sign_right_v;
341
8.63M
                }
342
343
17.2M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
344
345
17.2M
                if(0 != edge_idx)
346
3.90M
                {
347
3.90M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
348
3.90M
                }
349
17.2M
            }
350
351
301k
            pu1_src += src_strd;
352
301k
        }
353
10.0k
    }
354
355
613k
    for(row = 0; row < 2 * ht; row++)
356
603k
    {
357
603k
        pu1_src_left[row] = au1_src_left_tmp[row];
358
603k
    }
359
360
10.0k
}
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
9.60k
{
377
9.60k
    WORD32 row, col;
378
9.60k
    UWORD8 au1_mask[MAX_CTB_SIZE];
379
9.60k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
380
9.60k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
381
9.60k
    WORD8 u1_sign_down;
382
9.60k
    WORD32 bit_depth;
383
9.60k
    UNUSED(pu1_src_top_right);
384
9.60k
    UNUSED(pu1_src_bot_left);
385
386
9.60k
    bit_depth = BIT_DEPTH_LUMA;
387
388
    /* Initialize the mask values */
389
9.60k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
390
391
    /* Update left, top and top-left arrays */
392
9.60k
    *pu1_src_top_left = pu1_src_top[wd - 1];
393
573k
    for(row = 0; row < ht; row++)
394
563k
    {
395
563k
        pu1_src_left[row] = pu1_src[row * src_strd + wd - 1];
396
563k
    }
397
572k
    for(col = 0; col < wd; col++)
398
562k
    {
399
562k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
400
562k
    }
401
402
    /* Update height and source pointers based on the availability flags */
403
9.60k
    if(0 == pu1_avail[2])
404
4.03k
    {
405
4.03k
        pu1_src += src_strd;
406
4.03k
        ht--;
407
244k
        for(col = 0; col < wd; col++)
408
240k
        {
409
240k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
410
240k
        }
411
4.03k
    }
412
5.56k
    else
413
5.56k
    {
414
327k
        for(col = 0; col < wd; col++)
415
321k
        {
416
321k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
417
321k
        }
418
5.56k
    }
419
9.60k
    if(0 == pu1_avail[3])
420
4.13k
    {
421
4.13k
        ht--;
422
4.13k
    }
423
424
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
425
9.60k
    {
426
565k
        for(row = 0; row < ht; row++)
427
555k
        {
428
33.5M
            for(col = 0; col < wd; col++)
429
32.9M
            {
430
32.9M
                WORD32 edge_idx;
431
432
32.9M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
433
32.9M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
434
32.9M
                au1_sign_up[col] = -u1_sign_down;
435
436
32.9M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
437
438
32.9M
                if(0 != edge_idx)
439
9.45M
                {
440
9.45M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
441
9.45M
                }
442
32.9M
            }
443
444
555k
            pu1_src += src_strd;
445
555k
        }
446
9.60k
    }
447
448
572k
    for(col = 0; col < wd; col++)
449
562k
    {
450
562k
        pu1_src_top[col] = au1_src_top_tmp[col];
451
562k
    }
452
453
9.60k
}
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
1.97k
{
471
1.97k
    WORD32 row, col;
472
1.97k
    UWORD8 au1_mask[MAX_CTB_SIZE];
473
1.97k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
474
1.97k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
475
1.97k
    WORD8 u1_sign_down;
476
1.97k
    WORD32 bit_depth;
477
1.97k
    UNUSED(pu1_src_top_right);
478
1.97k
    UNUSED(pu1_src_bot_left);
479
480
1.97k
    bit_depth = BIT_DEPTH_CHROMA;
481
482
    /* Initialize the mask values */
483
1.97k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
484
485
    /* Update left, top and top-left arrays */
486
1.97k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
487
1.97k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
488
59.3k
    for(row = 0; row < ht; row++)
489
57.3k
    {
490
57.3k
        pu1_src_left[2 * row] = pu1_src[row * src_strd + wd - 2];
491
57.3k
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
492
57.3k
    }
493
119k
    for(col = 0; col < wd; col++)
494
117k
    {
495
117k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
496
117k
    }
497
498
    /* Update height and source pointers based on the availability flags */
499
1.97k
    if(0 == pu1_avail[2])
500
618
    {
501
618
        pu1_src += src_strd;
502
618
        ht--;
503
37.4k
        for(col = 0; col < wd; col++)
504
36.7k
        {
505
36.7k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
506
36.7k
        }
507
618
    }
508
1.35k
    else
509
1.35k
    {
510
82.3k
        for(col = 0; col < wd; col++)
511
80.9k
        {
512
80.9k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
513
80.9k
        }
514
1.35k
    }
515
1.97k
    if(0 == pu1_avail[3])
516
714
    {
517
714
        ht--;
518
714
    }
519
520
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
521
1.97k
    {
522
58.0k
        for(row = 0; row < ht; row++)
523
56.0k
        {
524
3.43M
            for(col = 0; col < wd; col++)
525
3.38M
            {
526
3.38M
                WORD32 edge_idx;
527
3.38M
                WORD8 *pi1_sao_offset;
528
529
3.38M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
530
531
3.38M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
532
3.38M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
533
3.38M
                au1_sign_up[col] = -u1_sign_down;
534
535
3.38M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
536
537
3.38M
                if(0 != edge_idx)
538
463k
                {
539
463k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
540
463k
                }
541
3.38M
            }
542
543
56.0k
            pu1_src += src_strd;
544
56.0k
        }
545
1.97k
    }
546
547
119k
    for(col = 0; col < wd; col++)
548
117k
    {
549
117k
        pu1_src_top[col] = au1_src_top_tmp[col];
550
117k
    }
551
552
1.97k
}
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
8.59k
{
569
8.59k
    WORD32 row, col;
570
8.59k
    UWORD8 au1_mask[MAX_CTB_SIZE];
571
8.59k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
572
8.59k
    UWORD8 u1_src_top_left_tmp;
573
8.59k
    WORD8 au1_sign_up[MAX_CTB_SIZE + 1], au1_sign_up_tmp[MAX_CTB_SIZE + 1];
574
8.59k
    WORD8 u1_sign_down;
575
8.59k
    WORD8 *pu1_sign_up;
576
8.59k
    WORD8 *pu1_sign_up_tmp;
577
8.59k
    UWORD8 *pu1_src_left_cpy;
578
579
8.59k
    WORD32 bit_depth;
580
8.59k
    UWORD8 u1_pos_0_0_tmp;
581
8.59k
    UWORD8 u1_pos_wd_ht_tmp;
582
8.59k
    UNUSED(pu1_src_top_right);
583
8.59k
    UNUSED(pu1_src_bot_left);
584
585
8.59k
    bit_depth = BIT_DEPTH_LUMA;
586
8.59k
    pu1_sign_up = au1_sign_up;
587
8.59k
    pu1_sign_up_tmp = au1_sign_up_tmp;
588
8.59k
    pu1_src_left_cpy = pu1_src_left;
589
590
    /* Initialize the mask values */
591
8.59k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
592
593
    /* Update left, top and top-left arrays */
594
8.59k
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
595
513k
    for(row = 0; row < ht; row++)
596
504k
    {
597
504k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
598
504k
    }
599
511k
    for(col = 0; col < wd; col++)
600
502k
    {
601
502k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
602
502k
    }
603
604
605
    /* If top-left is available, process separately */
606
8.59k
    if(0 != pu1_avail[4])
607
3.06k
    {
608
3.06k
        WORD32 edge_idx;
609
610
3.06k
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
611
3.06k
                        SIGN(pu1_src[0] - pu1_src[1 + src_strd]);
612
613
3.06k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
614
615
3.06k
        if(0 != edge_idx)
616
1.36k
        {
617
1.36k
            u1_pos_0_0_tmp = CLIP3(pu1_src[0] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
618
1.36k
        }
619
1.69k
        else
620
1.69k
        {
621
1.69k
            u1_pos_0_0_tmp = pu1_src[0];
622
1.69k
        }
623
3.06k
    }
624
5.53k
    else
625
5.53k
    {
626
5.53k
        u1_pos_0_0_tmp = pu1_src[0];
627
5.53k
    }
628
629
    /* If bottom-right is available, process separately */
630
8.59k
    if(0 != pu1_avail[7])
631
2.97k
    {
632
2.97k
        WORD32 edge_idx;
633
634
2.97k
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 1 - src_strd]) +
635
2.97k
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 1 + src_strd]);
636
637
2.97k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
638
639
2.97k
        if(0 != edge_idx)
640
1.38k
        {
641
1.38k
            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.38k
        }
643
1.58k
        else
644
1.58k
        {
645
1.58k
            u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
646
1.58k
        }
647
2.97k
    }
648
5.62k
    else
649
5.62k
    {
650
5.62k
        u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
651
5.62k
    }
652
653
    /* If Left is not available */
654
8.59k
    if(0 == pu1_avail[0])
655
3.06k
    {
656
3.06k
        au1_mask[0] = 0;
657
3.06k
    }
658
659
    /* If Top is not available */
660
8.59k
    if(0 == pu1_avail[2])
661
3.92k
    {
662
3.92k
        pu1_src += src_strd;
663
3.92k
        ht--;
664
3.92k
        pu1_src_left_cpy += 1;
665
234k
        for(col = 1; col < wd; col++)
666
230k
        {
667
230k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 1 - src_strd]);
668
230k
        }
669
3.92k
    }
670
4.67k
    else
671
4.67k
    {
672
268k
        for(col = 1; col < wd; col++)
673
263k
        {
674
263k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 1]);
675
263k
        }
676
4.67k
    }
677
678
    /* If Right is not available */
679
8.59k
    if(0 == pu1_avail[1])
680
3.11k
    {
681
3.11k
        au1_mask[wd - 1] = 0;
682
3.11k
    }
683
684
    /* If Bottom is not available */
685
8.59k
    if(0 == pu1_avail[3])
686
3.99k
    {
687
3.99k
        ht--;
688
3.99k
    }
689
690
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
691
8.59k
    {
692
505k
        for(row = 0; row < ht; row++)
693
496k
        {
694
496k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[row - 1]);
695
29.9M
            for(col = 0; col < wd; col++)
696
29.4M
            {
697
29.4M
                WORD32 edge_idx;
698
699
29.4M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 1 + src_strd]);
700
29.4M
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
701
29.4M
                pu1_sign_up_tmp[col + 1] = -u1_sign_down;
702
703
29.4M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
704
705
29.4M
                if(0 != edge_idx)
706
9.89M
                {
707
9.89M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
708
9.89M
                }
709
29.4M
            }
710
711
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
712
496k
            {
713
496k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
714
496k
                pu1_sign_up = pu1_sign_up_tmp;
715
496k
                pu1_sign_up_tmp = pu1_swap_tmp;
716
496k
            }
717
718
496k
            pu1_src += src_strd;
719
496k
        }
720
721
8.59k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp;
722
8.59k
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp;
723
8.59k
    }
724
725
8.59k
    if(0 == pu1_avail[2])
726
3.92k
        ht++;
727
8.59k
    if(0 == pu1_avail[3])
728
3.99k
        ht++;
729
8.59k
    *pu1_src_top_left = u1_src_top_left_tmp;
730
513k
    for(row = 0; row < ht; row++)
731
504k
    {
732
504k
        pu1_src_left[row] = au1_src_left_tmp[row];
733
504k
    }
734
511k
    for(col = 0; col < wd; col++)
735
502k
    {
736
502k
        pu1_src_top[col] = au1_src_top_tmp[col];
737
502k
    }
738
739
8.59k
}
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
969
{
758
969
    WORD32 row, col;
759
969
    UWORD8 au1_mask[MAX_CTB_SIZE];
760
969
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
761
969
    UWORD8 au1_src_top_left_tmp[2];
762
969
    WORD8 au1_sign_up[MAX_CTB_SIZE + 2], au1_sign_up_tmp[MAX_CTB_SIZE + 2];
763
969
    WORD8 u1_sign_down;
764
969
    WORD8 *pu1_sign_up;
765
969
    WORD8 *pu1_sign_up_tmp;
766
969
    UWORD8 *pu1_src_left_cpy;
767
768
969
    WORD32 bit_depth;
769
770
969
    UWORD8 u1_pos_0_0_tmp_u;
771
969
    UWORD8 u1_pos_0_0_tmp_v;
772
969
    UWORD8 u1_pos_wd_ht_tmp_u;
773
969
    UWORD8 u1_pos_wd_ht_tmp_v;
774
969
    UNUSED(pu1_src_top_right);
775
969
    UNUSED(pu1_src_bot_left);
776
777
778
969
    bit_depth = BIT_DEPTH_CHROMA;
779
969
    pu1_sign_up = au1_sign_up;
780
969
    pu1_sign_up_tmp = au1_sign_up_tmp;
781
969
    pu1_src_left_cpy = pu1_src_left;
782
783
    /* Initialize the mask values */
784
969
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
785
786
    /* Update left, top and top-left arrays */
787
969
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
788
969
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
789
28.8k
    for(row = 0; row < ht; row++)
790
27.8k
    {
791
27.8k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
792
27.8k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
793
27.8k
    }
794
58.9k
    for(col = 0; col < wd; col++)
795
57.9k
    {
796
57.9k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
797
57.9k
    }
798
799
800
    /* If top-left is available, process separately */
801
969
    if(0 != pu1_avail[4])
802
349
    {
803
349
        WORD32 edge_idx;
804
805
        /* U */
806
349
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
807
349
                        SIGN(pu1_src[0] - pu1_src[2 + src_strd]);
808
809
349
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
810
811
349
        if(0 != edge_idx)
812
156
        {
813
156
            u1_pos_0_0_tmp_u = CLIP3(pu1_src[0] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
814
156
        }
815
193
        else
816
193
        {
817
193
            u1_pos_0_0_tmp_u = pu1_src[0];
818
193
        }
819
820
        /* V */
821
349
        edge_idx = 2 + SIGN(pu1_src[1] - pu1_src_top_left[1]) +
822
349
                        SIGN(pu1_src[1] - pu1_src[1 + 2 + src_strd]);
823
824
349
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
825
826
349
        if(0 != edge_idx)
827
144
        {
828
144
            u1_pos_0_0_tmp_v = CLIP3(pu1_src[1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
829
144
        }
830
205
        else
831
205
        {
832
205
            u1_pos_0_0_tmp_v = pu1_src[1];
833
205
        }
834
349
    }
835
620
    else
836
620
    {
837
620
        u1_pos_0_0_tmp_u = pu1_src[0];
838
620
        u1_pos_0_0_tmp_v = pu1_src[1];
839
620
    }
840
841
    /* If bottom-right is available, process separately */
842
969
    if(0 != pu1_avail[7])
843
258
    {
844
258
        WORD32 edge_idx;
845
846
        /* U */
847
258
        edge_idx = 2 + SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd - 2 - src_strd]) +
848
258
                        SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd + 2 + src_strd]);
849
850
258
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
851
852
258
        if(0 != edge_idx)
853
129
        {
854
129
            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
129
        }
856
129
        else
857
129
        {
858
129
            u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
859
129
        }
860
861
        /* V */
862
258
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 2 - src_strd]) +
863
258
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 2 + src_strd]);
864
865
258
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
866
867
258
        if(0 != edge_idx)
868
106
        {
869
106
            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
106
        }
871
152
        else
872
152
        {
873
152
            u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
874
152
        }
875
258
    }
876
711
    else
877
711
    {
878
711
        u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
879
711
        u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
880
711
    }
881
882
    /* If Left is not available */
883
969
    if(0 == pu1_avail[0])
884
312
    {
885
312
        au1_mask[0] = 0;
886
312
    }
887
888
    /* If Top is not available */
889
969
    if(0 == pu1_avail[2])
890
503
    {
891
503
        pu1_src += src_strd;
892
503
        pu1_src_left_cpy += 2;
893
503
        ht--;
894
30.2k
        for(col = 2; col < wd; col++)
895
29.7k
        {
896
29.7k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 2 - src_strd]);
897
29.7k
        }
898
503
    }
899
466
    else
900
466
    {
901
26.7k
        for(col = 2; col < wd; col++)
902
26.2k
        {
903
26.2k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 2]);
904
26.2k
        }
905
466
    }
906
907
    /* If Right is not available */
908
969
    if(0 == pu1_avail[1])
909
365
    {
910
365
        au1_mask[(wd - 1) >> 1] = 0;
911
365
    }
912
913
    /* If Bottom is not available */
914
969
    if(0 == pu1_avail[3])
915
576
    {
916
576
        ht--;
917
576
    }
918
919
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
920
969
    {
921
27.7k
        for(row = 0; row < ht; row++)
922
26.7k
        {
923
26.7k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[2 * (row - 1)]);
924
26.7k
            pu1_sign_up[1] = SIGN(pu1_src[1] - pu1_src_left_cpy[2 * (row - 1) + 1]);
925
1.66M
            for(col = 0; col < wd; col++)
926
1.64M
            {
927
1.64M
                WORD32 edge_idx;
928
1.64M
                WORD8 *pi1_sao_offset;
929
930
1.64M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
931
932
1.64M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 2 + src_strd]);
933
1.64M
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
934
1.64M
                pu1_sign_up_tmp[col + 2] = -u1_sign_down;
935
936
1.64M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
937
938
1.64M
                if(0 != edge_idx)
939
506k
                {
940
506k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
941
506k
                }
942
1.64M
            }
943
944
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
945
26.7k
            {
946
26.7k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
947
26.7k
                pu1_sign_up = pu1_sign_up_tmp;
948
26.7k
                pu1_sign_up_tmp = pu1_swap_tmp;
949
26.7k
            }
950
951
26.7k
            pu1_src += src_strd;
952
26.7k
        }
953
954
969
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp_u;
955
969
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + 1] = u1_pos_0_0_tmp_v;
956
969
        pu1_src[(pu1_avail[3] ? wd - 2 - src_strd : wd - 2)] = u1_pos_wd_ht_tmp_u;
957
969
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp_v;
958
969
    }
959
960
969
    if(0 == pu1_avail[2])
961
503
        ht++;
962
969
    if(0 == pu1_avail[3])
963
576
        ht++;
964
969
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
965
969
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
966
56.6k
    for(row = 0; row < 2 * ht; row++)
967
55.7k
    {
968
55.7k
        pu1_src_left[row] = au1_src_left_tmp[row];
969
55.7k
    }
970
58.9k
    for(col = 0; col < wd; col++)
971
57.9k
    {
972
57.9k
        pu1_src_top[col] = au1_src_top_tmp[col];
973
57.9k
    }
974
975
969
}
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
8.40k
{
993
8.40k
    WORD32 row, col;
994
8.40k
    UWORD8 au1_mask[MAX_CTB_SIZE];
995
8.40k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
996
8.40k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
997
8.40k
    UWORD8 u1_src_top_left_tmp;
998
8.40k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
999
8.40k
    UWORD8 *pu1_src_left_cpy;
1000
8.40k
    WORD8 u1_sign_down;
1001
8.40k
    WORD32 bit_depth;
1002
1003
8.40k
    UWORD8 u1_pos_0_ht_tmp;
1004
8.40k
    UWORD8 u1_pos_wd_0_tmp;
1005
1006
8.40k
    bit_depth = BIT_DEPTH_LUMA;
1007
8.40k
    pu1_src_left_cpy = pu1_src_left;
1008
1009
    /* Initialize the mask values */
1010
8.40k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1011
1012
    /* Update left, top and top-left arrays */
1013
8.40k
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
1014
502k
    for(row = 0; row < ht; row++)
1015
494k
    {
1016
494k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
1017
494k
    }
1018
498k
    for(col = 0; col < wd; col++)
1019
490k
    {
1020
490k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1021
490k
    }
1022
1023
    /* If top-right is available, process separately */
1024
8.40k
    if(0 != pu1_avail[5])
1025
2.93k
    {
1026
2.93k
        WORD32 edge_idx;
1027
1028
2.93k
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[0]) +
1029
2.93k
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 1 + src_strd]);
1030
1031
2.93k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1032
1033
2.93k
        if(0 != edge_idx)
1034
1.28k
        {
1035
1.28k
            u1_pos_wd_0_tmp = CLIP3(pu1_src[wd - 1] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1036
1.28k
        }
1037
1.65k
        else
1038
1.65k
        {
1039
1.65k
            u1_pos_wd_0_tmp = pu1_src[wd - 1];
1040
1.65k
        }
1041
2.93k
    }
1042
5.46k
    else
1043
5.46k
    {
1044
5.46k
        u1_pos_wd_0_tmp = pu1_src[wd - 1];
1045
5.46k
    }
1046
1047
    /* If bottom-left is available, process separately */
1048
8.40k
    if(0 != pu1_avail[6])
1049
2.92k
    {
1050
2.92k
        WORD32 edge_idx;
1051
1052
2.92k
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 1 - src_strd]) +
1053
2.92k
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1054
1055
2.92k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1056
1057
2.92k
        if(0 != edge_idx)
1058
1.32k
        {
1059
1.32k
            u1_pos_0_ht_tmp = CLIP3(pu1_src[(ht - 1) * src_strd] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1060
1.32k
        }
1061
1.59k
        else
1062
1.59k
        {
1063
1.59k
            u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1064
1.59k
        }
1065
2.92k
    }
1066
5.48k
    else
1067
5.48k
    {
1068
5.48k
        u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1069
5.48k
    }
1070
1071
    /* If Left is not available */
1072
8.40k
    if(0 == pu1_avail[0])
1073
2.97k
    {
1074
2.97k
        au1_mask[0] = 0;
1075
2.97k
    }
1076
1077
    /* If Top is not available */
1078
8.40k
    if(0 == pu1_avail[2])
1079
3.82k
    {
1080
3.82k
        pu1_src += src_strd;
1081
3.82k
        ht--;
1082
3.82k
        pu1_src_left_cpy += 1;
1083
228k
        for(col = 0; col < wd - 1; col++)
1084
224k
        {
1085
224k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 1 - src_strd]);
1086
224k
        }
1087
3.82k
    }
1088
4.58k
    else
1089
4.58k
    {
1090
262k
        for(col = 0; col < wd - 1; col++)
1091
257k
        {
1092
257k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 1]);
1093
257k
        }
1094
4.58k
    }
1095
1096
    /* If Right is not available */
1097
8.40k
    if(0 == pu1_avail[1])
1098
3.03k
    {
1099
3.03k
        au1_mask[wd - 1] = 0;
1100
3.03k
    }
1101
1102
    /* If Bottom is not available */
1103
8.40k
    if(0 == pu1_avail[3])
1104
3.85k
    {
1105
3.85k
        ht--;
1106
3.85k
    }
1107
1108
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1109
8.40k
    {
1110
494k
        for(row = 0; row < ht; row++)
1111
486k
        {
1112
486k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 1 - src_strd]);
1113
29.2M
            for(col = 0; col < wd; col++)
1114
28.7M
            {
1115
28.7M
                WORD32 edge_idx;
1116
1117
28.7M
                u1_sign_down = SIGN(pu1_src[col] - ((col == 0) ? pu1_src_left_cpy[row + 1] :
1118
28.7M
                                                                 pu1_src[col - 1 + src_strd]));
1119
28.7M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1120
28.7M
                if(col > 0)
1121
28.3M
                    au1_sign_up[col - 1] = -u1_sign_down;
1122
1123
28.7M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
1124
1125
28.7M
                if(0 != edge_idx)
1126
9.64M
                {
1127
9.64M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1128
9.64M
                }
1129
28.7M
            }
1130
1131
486k
            pu1_src += src_strd;
1132
486k
        }
1133
1134
8.40k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp;
1135
8.40k
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp;
1136
8.40k
    }
1137
1138
8.40k
    if(0 == pu1_avail[2])
1139
3.82k
        ht++;
1140
8.40k
    if(0 == pu1_avail[3])
1141
3.85k
        ht++;
1142
8.40k
    *pu1_src_top_left = u1_src_top_left_tmp;
1143
502k
    for(row = 0; row < ht; row++)
1144
494k
    {
1145
494k
        pu1_src_left[row] = au1_src_left_tmp[row];
1146
494k
    }
1147
498k
    for(col = 0; col < wd; col++)
1148
490k
    {
1149
490k
        pu1_src_top[col] = au1_src_top_tmp[col];
1150
490k
    }
1151
1152
8.40k
}
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
776
{
1170
776
    WORD32 row, col;
1171
776
    UWORD8 au1_mask[MAX_CTB_SIZE];
1172
776
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
1173
776
    UWORD8 au1_src_top_left_tmp[2];
1174
776
    WORD8 au1_sign_up[MAX_CTB_SIZE];
1175
776
    UWORD8 *pu1_src_left_cpy;
1176
776
    WORD8 u1_sign_down;
1177
776
    WORD32 bit_depth;
1178
1179
776
    UWORD8 u1_pos_wd_0_tmp_u;
1180
776
    UWORD8 u1_pos_wd_0_tmp_v;
1181
776
    UWORD8 u1_pos_0_ht_tmp_u;
1182
776
    UWORD8 u1_pos_0_ht_tmp_v;
1183
1184
776
    bit_depth = BIT_DEPTH_CHROMA;
1185
776
    pu1_src_left_cpy = pu1_src_left;
1186
1187
    /* Initialize the mask values */
1188
776
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1189
1190
    /* Update left, top and top-left arrays */
1191
776
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
1192
776
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
1193
23.3k
    for(row = 0; row < ht; row++)
1194
22.6k
    {
1195
22.6k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
1196
22.6k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
1197
22.6k
    }
1198
46.4k
    for(col = 0; col < wd; col++)
1199
45.6k
    {
1200
45.6k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1201
45.6k
    }
1202
1203
1204
    /* If top-right is available, process separately */
1205
776
    if(0 != pu1_avail[5])
1206
218
    {
1207
218
        WORD32 edge_idx;
1208
1209
        /* U */
1210
218
        edge_idx = 2 + SIGN(pu1_src[wd - 2] - pu1_src_top_right[0]) +
1211
218
                        SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 - 2 + src_strd]);
1212
1213
218
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1214
1215
218
        if(0 != edge_idx)
1216
66
        {
1217
66
            u1_pos_wd_0_tmp_u = CLIP3(pu1_src[wd - 2] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
1218
66
        }
1219
152
        else
1220
152
        {
1221
152
            u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1222
152
        }
1223
1224
        /* V */
1225
218
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[1]) +
1226
218
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 2 + src_strd]);
1227
1228
218
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1229
1230
218
        if(0 != edge_idx)
1231
58
        {
1232
58
            u1_pos_wd_0_tmp_v = CLIP3(pu1_src[wd - 1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
1233
58
        }
1234
160
        else
1235
160
        {
1236
160
            u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1237
160
        }
1238
218
    }
1239
558
    else
1240
558
    {
1241
558
        u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1242
558
        u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1243
558
    }
1244
1245
    /* If bottom-left is available, process separately */
1246
776
    if(0 != pu1_avail[6])
1247
207
    {
1248
207
        WORD32 edge_idx;
1249
1250
        /* U */
1251
207
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 2 - src_strd]) +
1252
207
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1253
1254
207
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1255
1256
207
        if(0 != edge_idx)
1257
65
        {
1258
65
            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
65
        }
1260
142
        else
1261
142
        {
1262
142
            u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1263
142
        }
1264
1265
        /* V */
1266
207
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src[(ht - 1) * src_strd + 1 + 2 - src_strd]) +
1267
207
                        SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src_bot_left[1]);
1268
1269
207
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1270
1271
207
        if(0 != edge_idx)
1272
52
        {
1273
52
            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
52
        }
1275
155
        else
1276
155
        {
1277
155
            u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1278
155
        }
1279
207
    }
1280
569
    else
1281
569
    {
1282
569
        u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1283
569
        u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1284
569
    }
1285
1286
    /* If Left is not available */
1287
776
    if(0 == pu1_avail[0])
1288
230
    {
1289
230
        au1_mask[0] = 0;
1290
230
    }
1291
1292
    /* If Top is not available */
1293
776
    if(0 == pu1_avail[2])
1294
402
    {
1295
402
        pu1_src += src_strd;
1296
402
        ht--;
1297
402
        pu1_src_left_cpy += 2;
1298
23.7k
        for(col = 0; col < wd - 2; col++)
1299
23.3k
        {
1300
23.3k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 2 - src_strd]);
1301
23.3k
        }
1302
402
    }
1303
374
    else
1304
374
    {
1305
21.0k
        for(col = 0; col < wd - 2; col++)
1306
20.6k
        {
1307
20.6k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 2]);
1308
20.6k
        }
1309
374
    }
1310
1311
    /* If Right is not available */
1312
776
    if(0 == pu1_avail[1])
1313
285
    {
1314
285
        au1_mask[(wd - 1) >> 1] = 0;
1315
285
    }
1316
1317
    /* If Bottom is not available */
1318
776
    if(0 == pu1_avail[3])
1319
438
    {
1320
438
        ht--;
1321
438
    }
1322
1323
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1324
776
    {
1325
22.5k
        for(row = 0; row < ht; row++)
1326
21.7k
        {
1327
21.7k
            au1_sign_up[wd - 2] = SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 + 2 - src_strd]);
1328
21.7k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 2 - src_strd]);
1329
1.33M
            for(col = 0; col < wd; col++)
1330
1.30M
            {
1331
1.30M
                WORD32 edge_idx;
1332
1.30M
                WORD8 *pi1_sao_offset;
1333
1334
1.30M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
1335
1336
1.30M
                u1_sign_down = SIGN(pu1_src[col] - ((col < 2) ? pu1_src_left_cpy[2 * (row + 1) + col] :
1337
1.30M
                                                                pu1_src[col - 2 + src_strd]));
1338
1.30M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1339
1.30M
                if(col > 1)
1340
1.26M
                    au1_sign_up[col - 2] = -u1_sign_down;
1341
1342
1.30M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
1343
1344
1.30M
                if(0 != edge_idx)
1345
341k
                {
1346
341k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1347
341k
                }
1348
1.30M
            }
1349
1350
21.7k
            pu1_src += src_strd;
1351
21.7k
        }
1352
1353
776
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 2] = u1_pos_wd_0_tmp_u;
1354
776
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp_v;
1355
776
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp_u;
1356
776
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0) + 1] = u1_pos_0_ht_tmp_v;
1357
776
    }
1358
1359
776
    if(0 == pu1_avail[2])
1360
402
        ht++;
1361
776
    if(0 == pu1_avail[3])
1362
438
        ht++;
1363
776
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
1364
776
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
1365
45.9k
    for(row = 0; row < 2 * ht; row++)
1366
45.2k
    {
1367
45.2k
        pu1_src_left[row] = au1_src_left_tmp[row];
1368
45.2k
    }
1369
46.4k
    for(col = 0; col < wd; col++)
1370
45.6k
    {
1371
45.6k
        pu1_src_top[col] = au1_src_top_tmp[col];
1372
45.6k
    }
1373
1374
776
}