Coverage Report

Created: 2025-07-23 06:28

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