Coverage Report

Created: 2025-10-13 07:01

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