Coverage Report

Created: 2024-07-27 06:20

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