Coverage Report

Created: 2023-06-07 06:40

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