Coverage Report

Created: 2025-11-24 06:46

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