Coverage Report

Created: 2026-03-07 06:28

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