Coverage Report

Created: 2025-09-17 06:35

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