Coverage Report

Created: 2025-12-29 07:02

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