Coverage Report

Created: 2025-10-10 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/common/ihevc_sao.c
Line
Count
Source
1
/******************************************************************************
2
*
3
* Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at:
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*
17
******************************************************************************/
18
/**
19
*******************************************************************************
20
* @file
21
*  ihevc_sao.c
22
*
23
* @brief
24
*  Contains leaf level function definitions for sample adaptive offset process
25
*
26
* @author
27
*  Srinivas T
28
*
29
* @par List of Functions:
30
*   - ihevc_sao_band_offset_luma()
31
*   - ihevc_sao_band_offset_chroma()
32
*   - ihevc_sao_edge_offset_class0()
33
*   - ihevc_sao_edge_offset_class0_chroma()
34
*   - ihevc_sao_edge_offset_class1()
35
*   - ihevc_sao_edge_offset_class1_chroma()
36
*   - ihevc_sao_edge_offset_class2()
37
*   - ihevc_sao_edge_offset_class2_chroma()
38
*   - ihevc_sao_edge_offset_class3()
39
*   - ihevc_sao_edge_offset_class3_chroma()
40
* @remarks
41
*  None
42
*
43
*******************************************************************************
44
*/
45
#include <stdlib.h>
46
#include <assert.h>
47
#include <string.h>
48
#include "ihevc_typedefs.h"
49
#include "ihevc_macros.h"
50
#include "ihevc_platform_macros.h"
51
#include "ihevc_func_selector.h"
52
#include "ihevc_defs.h"
53
#include "ihevc_structs.h"
54
#include "ihevc_sao.h"
55
56
3.58M
#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
70.1k
{
82
70.1k
    WORD32 band_shift;
83
70.1k
    WORD32 band_table[NUM_BAND_TABLE];
84
70.1k
    WORD32 i;
85
70.1k
    WORD32 row, col;
86
87
    /* Updating left and top and top-left */
88
2.27M
    for(row = 0; row < ht; row++)
89
2.20M
    {
90
2.20M
        pu1_src_left[row] = pu1_src[row * src_strd + (wd - 1)];
91
2.20M
    }
92
70.1k
    pu1_src_top_left[0] = pu1_src_top[wd - 1];
93
2.32M
    for(col = 0; col < wd; col++)
94
2.25M
    {
95
2.25M
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
96
2.25M
    }
97
98
70.1k
    band_shift = BIT_DEPTH_LUMA - 5;
99
2.31M
    for(i = 0; i < NUM_BAND_TABLE; i++)
100
2.24M
    {
101
2.24M
        band_table[i] = 0;
102
2.24M
    }
103
350k
    for(i = 0; i < 4; i++)
104
280k
    {
105
280k
        band_table[(i + sao_band_pos) & 31] = i + 1;
106
280k
    }
107
108
2.25M
    for(row = 0; row < ht; row++)
109
2.18M
    {
110
73.6M
        for(col = 0; col < wd; col++)
111
71.4M
        {
112
71.4M
            WORD32 band_idx;
113
114
71.4M
            band_idx = band_table[pu1_src[col] >> band_shift];
115
71.4M
            pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[band_idx], 0, (1 << (band_shift + 5)) - 1);
116
71.4M
        }
117
2.18M
        pu1_src += src_strd;
118
2.18M
    }
119
70.1k
}
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
38.7k
{
136
38.7k
    WORD32 band_shift;
137
38.7k
    WORD32 band_table_u[NUM_BAND_TABLE];
138
38.7k
    WORD32 band_table_v[NUM_BAND_TABLE];
139
38.7k
    WORD32 i;
140
38.7k
    WORD32 row, col;
141
142
    /* Updating left and top and top-left */
143
655k
    for(row = 0; row < ht; row++)
144
616k
    {
145
616k
        pu1_src_left[2 * row] = pu1_src[row * src_strd + (wd - 2)];
146
616k
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + (wd - 1)];
147
616k
    }
148
38.7k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
149
38.7k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
150
1.28M
    for(col = 0; col < wd; col++)
151
1.24M
    {
152
1.24M
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
153
1.24M
    }
154
155
156
38.7k
    band_shift = BIT_DEPTH_CHROMA - 5;
157
1.27M
    for(i = 0; i < NUM_BAND_TABLE; i++)
158
1.23M
    {
159
1.23M
        band_table_u[i] = 0;
160
1.23M
        band_table_v[i] = 0;
161
1.23M
    }
162
193k
    for(i = 0; i < 4; i++)
163
154k
    {
164
154k
        band_table_u[(i + sao_band_pos_u) & 31] = i + 1;
165
154k
        band_table_v[(i + sao_band_pos_v) & 31] = i + 1;
166
154k
    }
167
168
654k
    for(row = 0; row < ht; row++)
169
615k
    {
170
20.5M
        for(col = 0; col < wd; col++)
171
19.9M
        {
172
19.9M
            WORD32 band_idx;
173
19.9M
            WORD8 *pi1_sao_offset;
174
175
19.9M
            pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
176
19.9M
            band_idx = (0 == col % 2) ? band_table_u[pu1_src[col] >> band_shift] : band_table_v[pu1_src[col] >> band_shift];
177
19.9M
            pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[band_idx], 0, (1 << (band_shift + 5)) - 1);
178
19.9M
        }
179
615k
        pu1_src += src_strd;
180
615k
    }
181
38.7k
}
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
16.8k
{
198
16.8k
    WORD32 row, col;
199
16.8k
    UWORD8 au1_mask[MAX_CTB_SIZE];
200
16.8k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
201
16.8k
    WORD8 u1_sign_left, u1_sign_right;
202
16.8k
    WORD32 bit_depth;
203
16.8k
    UNUSED(pu1_src_top_right);
204
16.8k
    UNUSED(pu1_src_bot_left);
205
16.8k
    bit_depth = BIT_DEPTH_LUMA;
206
207
    /* Initialize the mask values */
208
16.8k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
209
210
    /* Update top and top-left arrays */
211
16.8k
    *pu1_src_top_left = pu1_src_top[wd - 1];
212
544k
    for(row = 0; row < ht; row++)
213
528k
    {
214
528k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
215
528k
    }
216
556k
    for(col = 0; col < wd; col++)
217
539k
    {
218
539k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
219
539k
    }
220
221
    /* Update masks based on the availability flags */
222
16.8k
    if(0 == pu1_avail[0])
223
1.65k
    {
224
1.65k
        au1_mask[0] = 0;
225
1.65k
    }
226
16.8k
    if(0 == pu1_avail[1])
227
811
    {
228
811
        au1_mask[wd - 1] = 0;
229
811
    }
230
231
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
232
16.8k
    {
233
544k
        for(row = 0; row < ht; row++)
234
527k
        {
235
527k
            u1_sign_left = SIGN(pu1_src[0] - pu1_src_left[row]);
236
17.8M
            for(col = 0; col < wd; col++)
237
17.2M
            {
238
17.2M
                WORD32 edge_idx;
239
240
17.2M
                u1_sign_right = SIGN(pu1_src[col] - pu1_src[col + 1]);
241
17.2M
                edge_idx = 2 + u1_sign_left + u1_sign_right;
242
17.2M
                u1_sign_left = -u1_sign_right;
243
244
17.2M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
245
246
17.2M
                if(0 != edge_idx)
247
4.19M
                {
248
4.19M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
249
4.19M
                }
250
17.2M
            }
251
252
527k
            pu1_src += src_strd;
253
527k
        }
254
16.8k
    }
255
256
    /* Update left array */
257
544k
    for(row = 0; row < ht; row++)
258
527k
    {
259
527k
        pu1_src_left[row] = au1_src_left_tmp[row];
260
527k
    }
261
262
16.8k
}
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
10.2k
{
281
10.2k
    WORD32 row, col;
282
10.2k
    UWORD8 au1_mask[MAX_CTB_SIZE];
283
10.2k
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE];
284
10.2k
    WORD8 u1_sign_left_u, u1_sign_right_u;
285
10.2k
    WORD8 u1_sign_left_v, u1_sign_right_v;
286
10.2k
    WORD32 bit_depth;
287
10.2k
    UNUSED(pu1_src_top_right);
288
10.2k
    UNUSED(pu1_src_bot_left);
289
10.2k
    bit_depth = BIT_DEPTH_CHROMA;
290
291
    /* Initialize the mask values */
292
10.2k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
293
294
    /* Update left, top and top-left arrays */
295
10.2k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
296
10.2k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
297
173k
    for(row = 0; row < ht; row++)
298
163k
    {
299
163k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
300
163k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
301
163k
    }
302
347k
    for(col = 0; col < wd; col++)
303
337k
    {
304
337k
        pu1_src_top[col] = pu1_src[(ht - 1) * src_strd + col];
305
337k
    }
306
307
    /* Update masks based on the availability flags */
308
10.2k
    if(0 == pu1_avail[0])
309
1.25k
    {
310
1.25k
        au1_mask[0] = 0;
311
1.25k
    }
312
10.2k
    if(0 == pu1_avail[1])
313
682
    {
314
682
        au1_mask[(wd - 1) >> 1] = 0;
315
682
    }
316
317
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
318
10.2k
    {
319
173k
        for(row = 0; row < ht; row++)
320
163k
        {
321
163k
            u1_sign_left_u = SIGN(pu1_src[0] - pu1_src_left[2 * row]);
322
163k
            u1_sign_left_v = SIGN(pu1_src[1] - pu1_src_left[2 * row + 1]);
323
5.58M
            for(col = 0; col < wd; col++)
324
5.42M
            {
325
5.42M
                WORD32 edge_idx;
326
5.42M
                WORD8 *pi1_sao_offset;
327
328
5.42M
                if(0 == col % 2)
329
2.71M
                {
330
2.71M
                    pi1_sao_offset = pi1_sao_offset_u;
331
2.71M
                    u1_sign_right_u = SIGN(pu1_src[col] - pu1_src[col + 2]);
332
2.71M
                    edge_idx = 2 + u1_sign_left_u + u1_sign_right_u;
333
2.71M
                    u1_sign_left_u = -u1_sign_right_u;
334
2.71M
                }
335
2.71M
                else
336
2.71M
                {
337
2.71M
                    pi1_sao_offset = pi1_sao_offset_v;
338
2.71M
                    u1_sign_right_v = SIGN(pu1_src[col] - pu1_src[col + 2]);
339
2.71M
                    edge_idx = 2 + u1_sign_left_v + u1_sign_right_v;
340
2.71M
                    u1_sign_left_v = -u1_sign_right_v;
341
2.71M
                }
342
343
5.42M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
344
345
5.42M
                if(0 != edge_idx)
346
737k
                {
347
737k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
348
737k
                }
349
5.42M
            }
350
351
163k
            pu1_src += src_strd;
352
163k
        }
353
10.2k
    }
354
355
336k
    for(row = 0; row < 2 * ht; row++)
356
326k
    {
357
326k
        pu1_src_left[row] = au1_src_left_tmp[row];
358
326k
    }
359
360
10.2k
}
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
14.1k
{
377
14.1k
    WORD32 row, col;
378
14.1k
    UWORD8 au1_mask[MAX_CTB_SIZE];
379
14.1k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
380
14.1k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
381
14.1k
    WORD8 u1_sign_down;
382
14.1k
    WORD32 bit_depth;
383
14.1k
    UNUSED(pu1_src_top_right);
384
14.1k
    UNUSED(pu1_src_bot_left);
385
386
14.1k
    bit_depth = BIT_DEPTH_LUMA;
387
388
    /* Initialize the mask values */
389
14.1k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
390
391
    /* Update left, top and top-left arrays */
392
14.1k
    *pu1_src_top_left = pu1_src_top[wd - 1];
393
432k
    for(row = 0; row < ht; row++)
394
418k
    {
395
418k
        pu1_src_left[row] = pu1_src[row * src_strd + wd - 1];
396
418k
    }
397
446k
    for(col = 0; col < wd; col++)
398
431k
    {
399
431k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
400
431k
    }
401
402
    /* Update height and source pointers based on the availability flags */
403
14.1k
    if(0 == pu1_avail[2])
404
1.78k
    {
405
1.78k
        pu1_src += src_strd;
406
1.78k
        ht--;
407
65.0k
        for(col = 0; col < wd; col++)
408
63.2k
        {
409
63.2k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
410
63.2k
        }
411
1.78k
    }
412
12.3k
    else
413
12.3k
    {
414
380k
        for(col = 0; col < wd; col++)
415
368k
        {
416
368k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
417
368k
        }
418
12.3k
    }
419
14.1k
    if(0 == pu1_avail[3])
420
1.76k
    {
421
1.76k
        ht--;
422
1.76k
    }
423
424
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
425
14.1k
    {
426
428k
        for(row = 0; row < ht; row++)
427
414k
        {
428
13.9M
            for(col = 0; col < wd; col++)
429
13.5M
            {
430
13.5M
                WORD32 edge_idx;
431
432
13.5M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
433
13.5M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
434
13.5M
                au1_sign_up[col] = -u1_sign_down;
435
436
13.5M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
437
438
13.5M
                if(0 != edge_idx)
439
3.03M
                {
440
3.03M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
441
3.03M
                }
442
13.5M
            }
443
444
414k
            pu1_src += src_strd;
445
414k
        }
446
14.1k
    }
447
448
446k
    for(col = 0; col < wd; col++)
449
432k
    {
450
432k
        pu1_src_top[col] = au1_src_top_tmp[col];
451
432k
    }
452
453
14.1k
}
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
10.7k
{
471
10.7k
    WORD32 row, col;
472
10.7k
    UWORD8 au1_mask[MAX_CTB_SIZE];
473
10.7k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
474
10.7k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
475
10.7k
    WORD8 u1_sign_down;
476
10.7k
    WORD32 bit_depth;
477
10.7k
    UNUSED(pu1_src_top_right);
478
10.7k
    UNUSED(pu1_src_bot_left);
479
480
10.7k
    bit_depth = BIT_DEPTH_CHROMA;
481
482
    /* Initialize the mask values */
483
10.7k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
484
485
    /* Update left, top and top-left arrays */
486
10.7k
    pu1_src_top_left[0] = pu1_src_top[wd - 2];
487
10.7k
    pu1_src_top_left[1] = pu1_src_top[wd - 1];
488
184k
    for(row = 0; row < ht; row++)
489
173k
    {
490
173k
        pu1_src_left[2 * row] = pu1_src[row * src_strd + wd - 2];
491
173k
        pu1_src_left[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
492
173k
    }
493
362k
    for(col = 0; col < wd; col++)
494
351k
    {
495
351k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
496
351k
    }
497
498
    /* Update height and source pointers based on the availability flags */
499
10.7k
    if(0 == pu1_avail[2])
500
1.71k
    {
501
1.71k
        pu1_src += src_strd;
502
1.71k
        ht--;
503
58.4k
        for(col = 0; col < wd; col++)
504
56.7k
        {
505
56.7k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - src_strd]);
506
56.7k
        }
507
1.71k
    }
508
9.07k
    else
509
9.07k
    {
510
303k
        for(col = 0; col < wd; col++)
511
294k
        {
512
294k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col]);
513
294k
        }
514
9.07k
    }
515
10.7k
    if(0 == pu1_avail[3])
516
1.15k
    {
517
1.15k
        ht--;
518
1.15k
    }
519
520
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
521
10.7k
    {
522
181k
        for(row = 0; row < ht; row++)
523
170k
        {
524
5.74M
            for(col = 0; col < wd; col++)
525
5.57M
            {
526
5.57M
                WORD32 edge_idx;
527
5.57M
                WORD8 *pi1_sao_offset;
528
529
5.57M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
530
531
5.57M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + src_strd]);
532
5.57M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
533
5.57M
                au1_sign_up[col] = -u1_sign_down;
534
535
5.57M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
536
537
5.57M
                if(0 != edge_idx)
538
897k
                {
539
897k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
540
897k
                }
541
5.57M
            }
542
543
170k
            pu1_src += src_strd;
544
170k
        }
545
10.7k
    }
546
547
362k
    for(col = 0; col < wd; col++)
548
351k
    {
549
351k
        pu1_src_top[col] = au1_src_top_tmp[col];
550
351k
    }
551
552
10.7k
}
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
10.7k
{
569
10.7k
    WORD32 row, col;
570
10.7k
    UWORD8 au1_mask[MAX_CTB_SIZE];
571
10.7k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
572
10.7k
    UWORD8 u1_src_top_left_tmp;
573
10.7k
    WORD8 au1_sign_up[MAX_CTB_SIZE + 1], au1_sign_up_tmp[MAX_CTB_SIZE + 1];
574
10.7k
    WORD8 u1_sign_down;
575
10.7k
    WORD8 *pu1_sign_up;
576
10.7k
    WORD8 *pu1_sign_up_tmp;
577
10.7k
    UWORD8 *pu1_src_left_cpy;
578
579
10.7k
    WORD32 bit_depth;
580
10.7k
    UWORD8 u1_pos_0_0_tmp;
581
10.7k
    UWORD8 u1_pos_wd_ht_tmp;
582
10.7k
    UNUSED(pu1_src_top_right);
583
10.7k
    UNUSED(pu1_src_bot_left);
584
585
10.7k
    bit_depth = BIT_DEPTH_LUMA;
586
10.7k
    pu1_sign_up = au1_sign_up;
587
10.7k
    pu1_sign_up_tmp = au1_sign_up_tmp;
588
10.7k
    pu1_src_left_cpy = pu1_src_left;
589
590
    /* Initialize the mask values */
591
10.7k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
592
593
    /* Update left, top and top-left arrays */
594
10.7k
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
595
335k
    for(row = 0; row < ht; row++)
596
324k
    {
597
324k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
598
324k
    }
599
353k
    for(col = 0; col < wd; col++)
600
342k
    {
601
342k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
602
342k
    }
603
604
605
    /* If top-left is available, process separately */
606
10.7k
    if(0 != pu1_avail[4])
607
8.15k
    {
608
8.15k
        WORD32 edge_idx;
609
610
8.15k
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
611
8.15k
                        SIGN(pu1_src[0] - pu1_src[1 + src_strd]);
612
613
8.15k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
614
615
8.15k
        if(0 != edge_idx)
616
2.95k
        {
617
2.95k
            u1_pos_0_0_tmp = CLIP3(pu1_src[0] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
618
2.95k
        }
619
5.20k
        else
620
5.20k
        {
621
5.20k
            u1_pos_0_0_tmp = pu1_src[0];
622
5.20k
        }
623
8.15k
    }
624
2.60k
    else
625
2.60k
    {
626
2.60k
        u1_pos_0_0_tmp = pu1_src[0];
627
2.60k
    }
628
629
    /* If bottom-right is available, process separately */
630
10.7k
    if(0 != pu1_avail[7])
631
8.45k
    {
632
8.45k
        WORD32 edge_idx;
633
634
8.45k
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 1 - src_strd]) +
635
8.45k
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 1 + src_strd]);
636
637
8.45k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
638
639
8.45k
        if(0 != edge_idx)
640
3.59k
        {
641
3.59k
            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.59k
        }
643
4.86k
        else
644
4.86k
        {
645
4.86k
            u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
646
4.86k
        }
647
8.45k
    }
648
2.31k
    else
649
2.31k
    {
650
2.31k
        u1_pos_wd_ht_tmp = pu1_src[wd - 1 + (ht - 1) * src_strd];
651
2.31k
    }
652
653
    /* If Left is not available */
654
10.7k
    if(0 == pu1_avail[0])
655
531
    {
656
531
        au1_mask[0] = 0;
657
531
    }
658
659
    /* If Top is not available */
660
10.7k
    if(0 == pu1_avail[2])
661
2.20k
    {
662
2.20k
        pu1_src += src_strd;
663
2.20k
        ht--;
664
2.20k
        pu1_src_left_cpy += 1;
665
74.9k
        for(col = 1; col < wd; col++)
666
72.7k
        {
667
72.7k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 1 - src_strd]);
668
72.7k
        }
669
2.20k
    }
670
8.55k
    else
671
8.55k
    {
672
267k
        for(col = 1; col < wd; col++)
673
259k
        {
674
259k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 1]);
675
259k
        }
676
8.55k
    }
677
678
    /* If Right is not available */
679
10.7k
    if(0 == pu1_avail[1])
680
850
    {
681
850
        au1_mask[wd - 1] = 0;
682
850
    }
683
684
    /* If Bottom is not available */
685
10.7k
    if(0 == pu1_avail[3])
686
1.62k
    {
687
1.62k
        ht--;
688
1.62k
    }
689
690
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
691
10.7k
    {
692
331k
        for(row = 0; row < ht; row++)
693
320k
        {
694
320k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[row - 1]);
695
11.0M
            for(col = 0; col < wd; col++)
696
10.6M
            {
697
10.6M
                WORD32 edge_idx;
698
699
10.6M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 1 + src_strd]);
700
10.6M
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
701
10.6M
                pu1_sign_up_tmp[col + 1] = -u1_sign_down;
702
703
10.6M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
704
705
10.6M
                if(0 != edge_idx)
706
2.98M
                {
707
2.98M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
708
2.98M
                }
709
10.6M
            }
710
711
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
712
320k
            {
713
320k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
714
320k
                pu1_sign_up = pu1_sign_up_tmp;
715
320k
                pu1_sign_up_tmp = pu1_swap_tmp;
716
320k
            }
717
718
320k
            pu1_src += src_strd;
719
320k
        }
720
721
10.7k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp;
722
10.7k
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp;
723
10.7k
    }
724
725
10.7k
    if(0 == pu1_avail[2])
726
2.20k
        ht++;
727
10.7k
    if(0 == pu1_avail[3])
728
1.62k
        ht++;
729
10.7k
    *pu1_src_top_left = u1_src_top_left_tmp;
730
335k
    for(row = 0; row < ht; row++)
731
324k
    {
732
324k
        pu1_src_left[row] = au1_src_left_tmp[row];
733
324k
    }
734
353k
    for(col = 0; col < wd; col++)
735
342k
    {
736
342k
        pu1_src_top[col] = au1_src_top_tmp[col];
737
342k
    }
738
739
10.7k
}
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
10.0k
{
758
10.0k
    WORD32 row, col;
759
10.0k
    UWORD8 au1_mask[MAX_CTB_SIZE];
760
10.0k
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
761
10.0k
    UWORD8 au1_src_top_left_tmp[2];
762
10.0k
    WORD8 au1_sign_up[MAX_CTB_SIZE + 2], au1_sign_up_tmp[MAX_CTB_SIZE + 2];
763
10.0k
    WORD8 u1_sign_down;
764
10.0k
    WORD8 *pu1_sign_up;
765
10.0k
    WORD8 *pu1_sign_up_tmp;
766
10.0k
    UWORD8 *pu1_src_left_cpy;
767
768
10.0k
    WORD32 bit_depth;
769
770
10.0k
    UWORD8 u1_pos_0_0_tmp_u;
771
10.0k
    UWORD8 u1_pos_0_0_tmp_v;
772
10.0k
    UWORD8 u1_pos_wd_ht_tmp_u;
773
10.0k
    UWORD8 u1_pos_wd_ht_tmp_v;
774
10.0k
    UNUSED(pu1_src_top_right);
775
10.0k
    UNUSED(pu1_src_bot_left);
776
777
778
10.0k
    bit_depth = BIT_DEPTH_CHROMA;
779
10.0k
    pu1_sign_up = au1_sign_up;
780
10.0k
    pu1_sign_up_tmp = au1_sign_up_tmp;
781
10.0k
    pu1_src_left_cpy = pu1_src_left;
782
783
    /* Initialize the mask values */
784
10.0k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
785
786
    /* Update left, top and top-left arrays */
787
10.0k
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
788
10.0k
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
789
170k
    for(row = 0; row < ht; row++)
790
160k
    {
791
160k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
792
160k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
793
160k
    }
794
358k
    for(col = 0; col < wd; col++)
795
348k
    {
796
348k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
797
348k
    }
798
799
800
    /* If top-left is available, process separately */
801
10.0k
    if(0 != pu1_avail[4])
802
7.28k
    {
803
7.28k
        WORD32 edge_idx;
804
805
        /* U */
806
7.28k
        edge_idx = 2 + SIGN(pu1_src[0] - pu1_src_top_left[0]) +
807
7.28k
                        SIGN(pu1_src[0] - pu1_src[2 + src_strd]);
808
809
7.28k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
810
811
7.28k
        if(0 != edge_idx)
812
1.69k
        {
813
1.69k
            u1_pos_0_0_tmp_u = CLIP3(pu1_src[0] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
814
1.69k
        }
815
5.58k
        else
816
5.58k
        {
817
5.58k
            u1_pos_0_0_tmp_u = pu1_src[0];
818
5.58k
        }
819
820
        /* V */
821
7.28k
        edge_idx = 2 + SIGN(pu1_src[1] - pu1_src_top_left[1]) +
822
7.28k
                        SIGN(pu1_src[1] - pu1_src[1 + 2 + src_strd]);
823
824
7.28k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
825
826
7.28k
        if(0 != edge_idx)
827
1.81k
        {
828
1.81k
            u1_pos_0_0_tmp_v = CLIP3(pu1_src[1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
829
1.81k
        }
830
5.47k
        else
831
5.47k
        {
832
5.47k
            u1_pos_0_0_tmp_v = pu1_src[1];
833
5.47k
        }
834
7.28k
    }
835
2.73k
    else
836
2.73k
    {
837
2.73k
        u1_pos_0_0_tmp_u = pu1_src[0];
838
2.73k
        u1_pos_0_0_tmp_v = pu1_src[1];
839
2.73k
    }
840
841
    /* If bottom-right is available, process separately */
842
10.0k
    if(0 != pu1_avail[7])
843
7.76k
    {
844
7.76k
        WORD32 edge_idx;
845
846
        /* U */
847
7.76k
        edge_idx = 2 + SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd - 2 - src_strd]) +
848
7.76k
                        SIGN(pu1_src[wd - 2 + (ht - 1) * src_strd] - pu1_src[wd - 2 + (ht - 1) * src_strd + 2 + src_strd]);
849
850
7.76k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
851
852
7.76k
        if(0 != edge_idx)
853
1.89k
        {
854
1.89k
            u1_pos_wd_ht_tmp_u = CLIP3(pu1_src[wd - 2 + (ht - 1) * src_strd] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
855
1.89k
        }
856
5.87k
        else
857
5.87k
        {
858
5.87k
            u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
859
5.87k
        }
860
861
        /* V */
862
7.76k
        edge_idx = 2 + SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd - 2 - src_strd]) +
863
7.76k
                        SIGN(pu1_src[wd - 1 + (ht - 1) * src_strd] - pu1_src[wd - 1 + (ht - 1) * src_strd + 2 + src_strd]);
864
865
7.76k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
866
867
7.76k
        if(0 != edge_idx)
868
1.83k
        {
869
1.83k
            u1_pos_wd_ht_tmp_v = CLIP3(pu1_src[wd - 1 + (ht - 1) * src_strd] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
870
1.83k
        }
871
5.93k
        else
872
5.93k
        {
873
5.93k
            u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
874
5.93k
        }
875
7.76k
    }
876
2.24k
    else
877
2.24k
    {
878
2.24k
        u1_pos_wd_ht_tmp_u = pu1_src[wd - 2 + (ht - 1) * src_strd];
879
2.24k
        u1_pos_wd_ht_tmp_v = pu1_src[wd - 1 + (ht - 1) * src_strd];
880
2.24k
    }
881
882
    /* If Left is not available */
883
10.0k
    if(0 == pu1_avail[0])
884
1.00k
    {
885
1.00k
        au1_mask[0] = 0;
886
1.00k
    }
887
888
    /* If Top is not available */
889
10.0k
    if(0 == pu1_avail[2])
890
1.86k
    {
891
1.86k
        pu1_src += src_strd;
892
1.86k
        pu1_src_left_cpy += 2;
893
1.86k
        ht--;
894
61.1k
        for(col = 2; col < wd; col++)
895
59.2k
        {
896
59.2k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col - 2 - src_strd]);
897
59.2k
        }
898
1.86k
    }
899
8.15k
    else
900
8.15k
    {
901
277k
        for(col = 2; col < wd; col++)
902
268k
        {
903
268k
            pu1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col - 2]);
904
268k
        }
905
8.15k
    }
906
907
    /* If Right is not available */
908
10.0k
    if(0 == pu1_avail[1])
909
1.24k
    {
910
1.24k
        au1_mask[(wd - 1) >> 1] = 0;
911
1.24k
    }
912
913
    /* If Bottom is not available */
914
10.0k
    if(0 == pu1_avail[3])
915
1.13k
    {
916
1.13k
        ht--;
917
1.13k
    }
918
919
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
920
10.0k
    {
921
167k
        for(row = 0; row < ht; row++)
922
157k
        {
923
157k
            pu1_sign_up[0] = SIGN(pu1_src[0] - pu1_src_left_cpy[2 * (row - 1)]);
924
157k
            pu1_sign_up[1] = SIGN(pu1_src[1] - pu1_src_left_cpy[2 * (row - 1) + 1]);
925
5.66M
            for(col = 0; col < wd; col++)
926
5.50M
            {
927
5.50M
                WORD32 edge_idx;
928
5.50M
                WORD8 *pi1_sao_offset;
929
930
5.50M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
931
932
5.50M
                u1_sign_down = SIGN(pu1_src[col] - pu1_src[col + 2 + src_strd]);
933
5.50M
                edge_idx = 2 + pu1_sign_up[col] + u1_sign_down;
934
5.50M
                pu1_sign_up_tmp[col + 2] = -u1_sign_down;
935
936
5.50M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
937
938
5.50M
                if(0 != edge_idx)
939
864k
                {
940
864k
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
941
864k
                }
942
5.50M
            }
943
944
            /* Swapping pu1_sign_up_tmp and pu1_sign_up */
945
157k
            {
946
157k
                WORD8 *pu1_swap_tmp = pu1_sign_up;
947
157k
                pu1_sign_up = pu1_sign_up_tmp;
948
157k
                pu1_sign_up_tmp = pu1_swap_tmp;
949
157k
            }
950
951
157k
            pu1_src += src_strd;
952
157k
        }
953
954
10.0k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd] = u1_pos_0_0_tmp_u;
955
10.0k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + 1] = u1_pos_0_0_tmp_v;
956
10.0k
        pu1_src[(pu1_avail[3] ? wd - 2 - src_strd : wd - 2)] = u1_pos_wd_ht_tmp_u;
957
10.0k
        pu1_src[(pu1_avail[3] ? wd - 1 - src_strd : wd - 1)] = u1_pos_wd_ht_tmp_v;
958
10.0k
    }
959
960
10.0k
    if(0 == pu1_avail[2])
961
1.86k
        ht++;
962
10.0k
    if(0 == pu1_avail[3])
963
1.13k
        ht++;
964
10.0k
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
965
10.0k
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
966
331k
    for(row = 0; row < 2 * ht; row++)
967
321k
    {
968
321k
        pu1_src_left[row] = au1_src_left_tmp[row];
969
321k
    }
970
358k
    for(col = 0; col < wd; col++)
971
348k
    {
972
348k
        pu1_src_top[col] = au1_src_top_tmp[col];
973
348k
    }
974
975
10.0k
}
976
977
978
979
980
/* 45 degree filtering */
981
void ihevc_sao_edge_offset_class3(UWORD8 *pu1_src,
982
                                  WORD32 src_strd,
983
                                  UWORD8 *pu1_src_left,
984
                                  UWORD8 *pu1_src_top,
985
                                  UWORD8 *pu1_src_top_left,
986
                                  UWORD8 *pu1_src_top_right,
987
                                  UWORD8 *pu1_src_bot_left,
988
                                  UWORD8 *pu1_avail,
989
                                  WORD8 *pi1_sao_offset,
990
                                  WORD32 wd,
991
                                  WORD32 ht)
992
12.0k
{
993
12.0k
    WORD32 row, col;
994
12.0k
    UWORD8 au1_mask[MAX_CTB_SIZE];
995
12.0k
    UWORD8 au1_src_top_tmp[MAX_CTB_SIZE];
996
12.0k
    UWORD8 au1_src_left_tmp[MAX_CTB_SIZE];
997
12.0k
    UWORD8 u1_src_top_left_tmp;
998
12.0k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
999
12.0k
    UWORD8 *pu1_src_left_cpy;
1000
12.0k
    WORD8 u1_sign_down;
1001
12.0k
    WORD32 bit_depth;
1002
1003
12.0k
    UWORD8 u1_pos_0_ht_tmp;
1004
12.0k
    UWORD8 u1_pos_wd_0_tmp;
1005
1006
12.0k
    bit_depth = BIT_DEPTH_LUMA;
1007
12.0k
    pu1_src_left_cpy = pu1_src_left;
1008
1009
    /* Initialize the mask values */
1010
12.0k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1011
1012
    /* Update left, top and top-left arrays */
1013
12.0k
    u1_src_top_left_tmp = pu1_src_top[wd - 1];
1014
379k
    for(row = 0; row < ht; row++)
1015
367k
    {
1016
367k
        au1_src_left_tmp[row] = pu1_src[row * src_strd + wd - 1];
1017
367k
    }
1018
399k
    for(col = 0; col < wd; col++)
1019
387k
    {
1020
387k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1021
387k
    }
1022
1023
    /* If top-right is available, process separately */
1024
12.0k
    if(0 != pu1_avail[5])
1025
8.68k
    {
1026
8.68k
        WORD32 edge_idx;
1027
1028
8.68k
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[0]) +
1029
8.68k
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 1 + src_strd]);
1030
1031
8.68k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1032
1033
8.68k
        if(0 != edge_idx)
1034
2.92k
        {
1035
2.92k
            u1_pos_wd_0_tmp = CLIP3(pu1_src[wd - 1] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1036
2.92k
        }
1037
5.76k
        else
1038
5.76k
        {
1039
5.76k
            u1_pos_wd_0_tmp = pu1_src[wd - 1];
1040
5.76k
        }
1041
8.68k
    }
1042
3.34k
    else
1043
3.34k
    {
1044
3.34k
        u1_pos_wd_0_tmp = pu1_src[wd - 1];
1045
3.34k
    }
1046
1047
    /* If bottom-left is available, process separately */
1048
12.0k
    if(0 != pu1_avail[6])
1049
9.80k
    {
1050
9.80k
        WORD32 edge_idx;
1051
1052
9.80k
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 1 - src_strd]) +
1053
9.80k
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1054
1055
9.80k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1056
1057
9.80k
        if(0 != edge_idx)
1058
3.59k
        {
1059
3.59k
            u1_pos_0_ht_tmp = CLIP3(pu1_src[(ht - 1) * src_strd] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1060
3.59k
        }
1061
6.20k
        else
1062
6.20k
        {
1063
6.20k
            u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1064
6.20k
        }
1065
9.80k
    }
1066
2.23k
    else
1067
2.23k
    {
1068
2.23k
        u1_pos_0_ht_tmp = pu1_src[(ht - 1) * src_strd];
1069
2.23k
    }
1070
1071
    /* If Left is not available */
1072
12.0k
    if(0 == pu1_avail[0])
1073
652
    {
1074
652
        au1_mask[0] = 0;
1075
652
    }
1076
1077
    /* If Top is not available */
1078
12.0k
    if(0 == pu1_avail[2])
1079
2.52k
    {
1080
2.52k
        pu1_src += src_strd;
1081
2.52k
        ht--;
1082
2.52k
        pu1_src_left_cpy += 1;
1083
81.6k
        for(col = 0; col < wd - 1; col++)
1084
79.1k
        {
1085
79.1k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 1 - src_strd]);
1086
79.1k
        }
1087
2.52k
    }
1088
9.51k
    else
1089
9.51k
    {
1090
306k
        for(col = 0; col < wd - 1; col++)
1091
296k
        {
1092
296k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 1]);
1093
296k
        }
1094
9.51k
    }
1095
1096
    /* If Right is not available */
1097
12.0k
    if(0 == pu1_avail[1])
1098
952
    {
1099
952
        au1_mask[wd - 1] = 0;
1100
952
    }
1101
1102
    /* If Bottom is not available */
1103
12.0k
    if(0 == pu1_avail[3])
1104
1.68k
    {
1105
1.68k
        ht--;
1106
1.68k
    }
1107
1108
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1109
12.0k
    {
1110
375k
        for(row = 0; row < ht; row++)
1111
363k
        {
1112
363k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 1 - src_strd]);
1113
12.5M
            for(col = 0; col < wd; col++)
1114
12.1M
            {
1115
12.1M
                WORD32 edge_idx;
1116
1117
12.1M
                u1_sign_down = SIGN(pu1_src[col] - ((col == 0) ? pu1_src_left_cpy[row + 1] :
1118
12.1M
                                                                 pu1_src[col - 1 + src_strd]));
1119
12.1M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1120
12.1M
                if(col > 0)
1121
11.8M
                    au1_sign_up[col - 1] = -u1_sign_down;
1122
1123
12.1M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col];
1124
1125
12.1M
                if(0 != edge_idx)
1126
3.18M
                {
1127
3.18M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1128
3.18M
                }
1129
12.1M
            }
1130
1131
363k
            pu1_src += src_strd;
1132
363k
        }
1133
1134
12.0k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp;
1135
12.0k
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp;
1136
12.0k
    }
1137
1138
12.0k
    if(0 == pu1_avail[2])
1139
2.52k
        ht++;
1140
12.0k
    if(0 == pu1_avail[3])
1141
1.68k
        ht++;
1142
12.0k
    *pu1_src_top_left = u1_src_top_left_tmp;
1143
379k
    for(row = 0; row < ht; row++)
1144
367k
    {
1145
367k
        pu1_src_left[row] = au1_src_left_tmp[row];
1146
367k
    }
1147
399k
    for(col = 0; col < wd; col++)
1148
387k
    {
1149
387k
        pu1_src_top[col] = au1_src_top_tmp[col];
1150
387k
    }
1151
1152
12.0k
}
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
11.2k
{
1170
11.2k
    WORD32 row, col;
1171
11.2k
    UWORD8 au1_mask[MAX_CTB_SIZE];
1172
11.2k
    UWORD8 au1_src_left_tmp[2 * MAX_CTB_SIZE], au1_src_top_tmp[MAX_CTB_SIZE];
1173
11.2k
    UWORD8 au1_src_top_left_tmp[2];
1174
11.2k
    WORD8 au1_sign_up[MAX_CTB_SIZE];
1175
11.2k
    UWORD8 *pu1_src_left_cpy;
1176
11.2k
    WORD8 u1_sign_down;
1177
11.2k
    WORD32 bit_depth;
1178
1179
11.2k
    UWORD8 u1_pos_wd_0_tmp_u;
1180
11.2k
    UWORD8 u1_pos_wd_0_tmp_v;
1181
11.2k
    UWORD8 u1_pos_0_ht_tmp_u;
1182
11.2k
    UWORD8 u1_pos_0_ht_tmp_v;
1183
1184
11.2k
    bit_depth = BIT_DEPTH_CHROMA;
1185
11.2k
    pu1_src_left_cpy = pu1_src_left;
1186
1187
    /* Initialize the mask values */
1188
11.2k
    memset(au1_mask, 0xFF, MAX_CTB_SIZE);
1189
1190
    /* Update left, top and top-left arrays */
1191
11.2k
    au1_src_top_left_tmp[0] = pu1_src_top[wd - 2];
1192
11.2k
    au1_src_top_left_tmp[1] = pu1_src_top[wd - 1];
1193
189k
    for(row = 0; row < ht; row++)
1194
178k
    {
1195
178k
        au1_src_left_tmp[2 * row] = pu1_src[row * src_strd + wd - 2];
1196
178k
        au1_src_left_tmp[2 * row + 1] = pu1_src[row * src_strd + wd - 1];
1197
178k
    }
1198
373k
    for(col = 0; col < wd; col++)
1199
362k
    {
1200
362k
        au1_src_top_tmp[col] = pu1_src[(ht - 1) * src_strd + col];
1201
362k
    }
1202
1203
1204
    /* If top-right is available, process separately */
1205
11.2k
    if(0 != pu1_avail[5])
1206
9.18k
    {
1207
9.18k
        WORD32 edge_idx;
1208
1209
        /* U */
1210
9.18k
        edge_idx = 2 + SIGN(pu1_src[wd - 2] - pu1_src_top_right[0]) +
1211
9.18k
                        SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 - 2 + src_strd]);
1212
1213
9.18k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1214
1215
9.18k
        if(0 != edge_idx)
1216
1.93k
        {
1217
1.93k
            u1_pos_wd_0_tmp_u = CLIP3(pu1_src[wd - 2] + pi1_sao_offset_u[edge_idx], 0, (1 << bit_depth) - 1);
1218
1.93k
        }
1219
7.25k
        else
1220
7.25k
        {
1221
7.25k
            u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1222
7.25k
        }
1223
1224
        /* V */
1225
9.18k
        edge_idx = 2 + SIGN(pu1_src[wd - 1] - pu1_src_top_right[1]) +
1226
9.18k
                        SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 - 2 + src_strd]);
1227
1228
9.18k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1229
1230
9.18k
        if(0 != edge_idx)
1231
1.95k
        {
1232
1.95k
            u1_pos_wd_0_tmp_v = CLIP3(pu1_src[wd - 1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
1233
1.95k
        }
1234
7.22k
        else
1235
7.22k
        {
1236
7.22k
            u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1237
7.22k
        }
1238
9.18k
    }
1239
2.10k
    else
1240
2.10k
    {
1241
2.10k
        u1_pos_wd_0_tmp_u = pu1_src[wd - 2];
1242
2.10k
        u1_pos_wd_0_tmp_v = pu1_src[wd - 1];
1243
2.10k
    }
1244
1245
    /* If bottom-left is available, process separately */
1246
11.2k
    if(0 != pu1_avail[6])
1247
9.04k
    {
1248
9.04k
        WORD32 edge_idx;
1249
1250
        /* U */
1251
9.04k
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src[(ht - 1) * src_strd + 2 - src_strd]) +
1252
9.04k
                        SIGN(pu1_src[(ht - 1) * src_strd] - pu1_src_bot_left[0]);
1253
1254
9.04k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1255
1256
9.04k
        if(0 != edge_idx)
1257
2.16k
        {
1258
2.16k
            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
2.16k
        }
1260
6.88k
        else
1261
6.88k
        {
1262
6.88k
            u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1263
6.88k
        }
1264
1265
        /* V */
1266
9.04k
        edge_idx = 2 + SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src[(ht - 1) * src_strd + 1 + 2 - src_strd]) +
1267
9.04k
                        SIGN(pu1_src[(ht - 1) * src_strd + 1] - pu1_src_bot_left[1]);
1268
1269
9.04k
        edge_idx = gi4_ihevc_table_edge_idx[edge_idx];
1270
1271
9.04k
        if(0 != edge_idx)
1272
2.07k
        {
1273
2.07k
            u1_pos_0_ht_tmp_v = CLIP3(pu1_src[(ht - 1) * src_strd + 1] + pi1_sao_offset_v[edge_idx], 0, (1 << bit_depth) - 1);
1274
2.07k
        }
1275
6.97k
        else
1276
6.97k
        {
1277
6.97k
            u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1278
6.97k
        }
1279
9.04k
    }
1280
2.24k
    else
1281
2.24k
    {
1282
2.24k
        u1_pos_0_ht_tmp_u = pu1_src[(ht - 1) * src_strd];
1283
2.24k
        u1_pos_0_ht_tmp_v = pu1_src[(ht - 1) * src_strd + 1];
1284
2.24k
    }
1285
1286
    /* If Left is not available */
1287
11.2k
    if(0 == pu1_avail[0])
1288
777
    {
1289
777
        au1_mask[0] = 0;
1290
777
    }
1291
1292
    /* If Top is not available */
1293
11.2k
    if(0 == pu1_avail[2])
1294
1.57k
    {
1295
1.57k
        pu1_src += src_strd;
1296
1.57k
        ht--;
1297
1.57k
        pu1_src_left_cpy += 2;
1298
45.8k
        for(col = 0; col < wd - 2; col++)
1299
44.3k
        {
1300
44.3k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src[col + 2 - src_strd]);
1301
44.3k
        }
1302
1.57k
    }
1303
9.72k
    else
1304
9.72k
    {
1305
305k
        for(col = 0; col < wd - 2; col++)
1306
295k
        {
1307
295k
            au1_sign_up[col] = SIGN(pu1_src[col] - pu1_src_top[col + 2]);
1308
295k
        }
1309
9.72k
    }
1310
1311
    /* If Right is not available */
1312
11.2k
    if(0 == pu1_avail[1])
1313
648
    {
1314
648
        au1_mask[(wd - 1) >> 1] = 0;
1315
648
    }
1316
1317
    /* If Bottom is not available */
1318
11.2k
    if(0 == pu1_avail[3])
1319
1.52k
    {
1320
1.52k
        ht--;
1321
1.52k
    }
1322
1323
    /* Processing is done on the intermediate buffer and the output is written to the source buffer */
1324
11.2k
    {
1325
186k
        for(row = 0; row < ht; row++)
1326
175k
        {
1327
175k
            au1_sign_up[wd - 2] = SIGN(pu1_src[wd - 2] - pu1_src[wd - 2 + 2 - src_strd]);
1328
175k
            au1_sign_up[wd - 1] = SIGN(pu1_src[wd - 1] - pu1_src[wd - 1 + 2 - src_strd]);
1329
5.91M
            for(col = 0; col < wd; col++)
1330
5.73M
            {
1331
5.73M
                WORD32 edge_idx;
1332
5.73M
                WORD8 *pi1_sao_offset;
1333
1334
5.73M
                pi1_sao_offset = (0 == col % 2) ? pi1_sao_offset_u : pi1_sao_offset_v;
1335
1336
5.73M
                u1_sign_down = SIGN(pu1_src[col] - ((col < 2) ? pu1_src_left_cpy[2 * (row + 1) + col] :
1337
5.73M
                                                                pu1_src[col - 2 + src_strd]));
1338
5.73M
                edge_idx = 2 + au1_sign_up[col] + u1_sign_down;
1339
5.73M
                if(col > 1)
1340
5.38M
                    au1_sign_up[col - 2] = -u1_sign_down;
1341
1342
5.73M
                edge_idx = gi4_ihevc_table_edge_idx[edge_idx] & au1_mask[col >> 1];
1343
1344
5.73M
                if(0 != edge_idx)
1345
1.06M
                {
1346
1.06M
                    pu1_src[col] = CLIP3(pu1_src[col] + pi1_sao_offset[edge_idx], 0, (1 << bit_depth) - 1);
1347
1.06M
                }
1348
5.73M
            }
1349
1350
175k
            pu1_src += src_strd;
1351
175k
        }
1352
1353
11.2k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 2] = u1_pos_wd_0_tmp_u;
1354
11.2k
        pu1_src[-(pu1_avail[2] ? ht : ht + 1) * src_strd + wd - 1] = u1_pos_wd_0_tmp_v;
1355
11.2k
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0)] = u1_pos_0_ht_tmp_u;
1356
11.2k
        pu1_src[(pu1_avail[3] ?  (-src_strd) : 0) + 1] = u1_pos_0_ht_tmp_v;
1357
11.2k
    }
1358
1359
11.2k
    if(0 == pu1_avail[2])
1360
1.57k
        ht++;
1361
11.2k
    if(0 == pu1_avail[3])
1362
1.52k
        ht++;
1363
11.2k
    pu1_src_top_left[0] = au1_src_top_left_tmp[0];
1364
11.2k
    pu1_src_top_left[1] = au1_src_top_left_tmp[1];
1365
368k
    for(row = 0; row < 2 * ht; row++)
1366
357k
    {
1367
357k
        pu1_src_left[row] = au1_src_left_tmp[row];
1368
357k
    }
1369
373k
    for(col = 0; col < wd; col++)
1370
362k
    {
1371
362k
        pu1_src_top[col] = au1_src_top_tmp[col];
1372
362k
    }
1373
1374
11.2k
}