forked from mpeg5/xeve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxevem_alf.c
4164 lines (3714 loc) · 168 KB
/
xevem_alf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The copyright in this software is being made available under the BSD
License, included below. This software may be subject to contributor and
other third party rights, including patent rights, and no such rights are
granted under this license.
Copyright (c) 2020, Samsung Electronics Co., Ltd.
All Rights Reserved. */
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the copyright owner, nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "xevem_alf.h"
void alf_init(ADAPTIVE_LOOP_FILTER * alf, int bit_depth)
{
alf->clip_ranges.comp[0] = (CLIP_RANGE) { .min = 0, .max = (1 << bit_depth) - 1, .bd = bit_depth, .n = 0 };
alf->clip_ranges.comp[1] = (CLIP_RANGE) { .min = 0, .max = (1 << bit_depth) - 1, .bd = bit_depth, .n = 0 };
alf->clip_ranges.comp[2] = (CLIP_RANGE) { .min = 0, .max = (1 << bit_depth) - 1, .bd = bit_depth, .n = 0 };
alf->clip_ranges.used = FALSE;
alf->clip_ranges.chroma = FALSE;
for (int compIdx = 0; compIdx < N_C; compIdx++)
{
alf->ctu_enable_flag[compIdx] = NULL;
}
alf->derive_classification_blk = alf_derive_classification_blk;
alf->filter_5x5_blk = alf_filter_blk_5;
alf->filter_7x7_blk = alf_filter_blk_7;
}
void alf_init_filter_shape(ALF_FILTER_SHAPE* filter_shape, int size)
{
filter_shape->filterLength = size;
filter_shape->num_coef = size * size / 4 + 1;
filter_shape->filter_size = size * size / 2 + 1;
if (size == 5)
{
xeve_mcpy(filter_shape->pattern, pattern5, sizeof(pattern5));
xeve_mcpy(filter_shape->weights, weights5, sizeof(weights5));
xeve_mcpy(filter_shape->golombIdx, golombIdx5, sizeof(golombIdx5));
xeve_mcpy(filter_shape->pattern_to_large_filter, pattern_to_large_filter5, sizeof(pattern_to_large_filter5));
filter_shape->filter_type = ALF_FILTER_5;
}
else if (size == 7)
{
xeve_mcpy(filter_shape->pattern, pattern7, sizeof(pattern7));
xeve_mcpy(filter_shape->weights, weights7, sizeof(weights7));
xeve_mcpy(filter_shape->golombIdx, golombIdx7, sizeof(golombIdx7));
xeve_mcpy(filter_shape->pattern_to_large_filter, pattern_to_large_filter7, sizeof(pattern_to_large_filter7));
filter_shape->filter_type = ALF_FILTER_7;
}
else
{
filter_shape->filter_type = ALF_NUM_OF_FILTER_TYPES;
CHECK(0, "Wrong ALF filter shape");
}
}
/*
* tmp_yuv - destination, temporary buffer
* pointer tmp_yuv is assumed to point to interior point inside margins
* s - its stride
* rec - source, recovered buffer
* s2 - its stride
* w - width
* h - height
* m - margin size
*/
void alf_copy_and_extend_tile(pel* tmp_yuv, const int s, const pel* rec, const int s2, const int w, const int h, const int m)
{
//copy
for (int j = 0; j < h; j++)
{
xeve_mcpy(tmp_yuv + j * s, rec + j * s2, sizeof(pel) * w);
}
//extend
pel * p = tmp_yuv;
// do left and right margins
for (int y = 0; y < h; y++)
{
for (int x = 0; x < m; x++)
{
*(p - m + x) = p[0];
p[w + x] = p[w - 1];
}
p += s;
}
// p is now the (0,height) (bottom left of image within bigger picture
p -= (s + m);
// p is now the (-margin, height-1)
for (int y = 0; y < m; y++)
{
xeve_mcpy(p + (y + 1) * s, p, sizeof(pel) * (w + (m << 1)));
}
// pi is still (-marginX, height-1)
p -= ((h - 1) * s);
// pi is now (-marginX, 0)
for (int y = 0; y < m; y++)
{
xeve_mcpy(p - (y + 1) * s, p, sizeof(pel) * (w + (m << 1)));
}
}
/*
* tmp_yuv - destination, temporary buffer
* pointer tmp_yuv is assumed to point to interior point inside margins
* s - its stride
* rec - source, recovered buffer
* s2 - its stride
* w - width
* h - height
* m - margin size
*/
void alf_copy_and_extend( pel* tmp_yuv, const int s, const pel* rec, const int s2, const int w, const int h, const int m )
{
//copy
for (int j = 0; j < h; j++)
{
xeve_mcpy(tmp_yuv + j * s, rec + j * s2, sizeof(pel) * w);
}
//extend
pel * p = tmp_yuv;
// do left and right margins
for (int y = 0; y < h; y++)
{
for (int x = 0; x < m; x++)
{
*(p - m + x) = p[0];
p[w + x] = p[w - 1];
}
p += s;
}
// p is now the (0,height) (bottom left of image within bigger picture
p -= (s + m);
// p is now the (-margin, height-1)
for (int y = 0; y < m; y++)
{
xeve_mcpy(p + (y + 1) * s, p, sizeof(pel) * (w + (m << 1)));
}
// pi is still (-marginX, height-1)
p -= ((h - 1) * s);
// pi is now (-marginX, 0)
for (int y = 0; y < 3; y++)
{
xeve_mcpy(p - (y + 1) * s, p, sizeof(pel) * (w + (m << 1)));
}
} // <-- end of copy and extend
int alf_get_max_golomb_idx( ALF_FILTER_TYPE filter_type )
{
return filter_type == ALF_FILTER_5 ? 2 : 3;
}
const int alf_fixed_filter_coef[FIXED_FILTER_NUM][13] =
{
{ 0, 2, 7, -12, -4, -11, -2, 31, -9, 6, -4, 30, 444 - (1 << (NUM_BITS - 1)) },
{ -26, 4, 17, 22, -7, 19, 40, 47, 49, -28, 35, 48, 72 - (1 << (NUM_BITS - 1)) },
{ -24, -8, 30, 64, -13, 18, 18, 27, 80, 0, 31, 19, 28 - (1 << (NUM_BITS - 1)) },
{ -4, -14, 44, 100, -7, 6, -4, 8, 90, 26, 26, -12, -6 - (1 << (NUM_BITS - 1)) },
{ -17, -9, 23, -3, -15, 20, 53, 48, 16, -25, 42, 66, 114 - (1 << (NUM_BITS - 1)) },
{ -12, -2, 1, -19, -5, 8, 66, 80, -2, -25, 20, 78, 136 - (1 << (NUM_BITS - 1)) },
{ 2, 8, -23, -14, -3, -23, 64, 86, 35, -17, -4, 79, 132 - (1 << (NUM_BITS - 1)) },
{ 12, 4, -39, -7, 1, -20, 78, 13, -8, 11, -42, 98, 310 - (1 << (NUM_BITS - 1)) },
{ 0, 3, -4, 0, 2, -7, 6, 0, 0, 3, -8, 11, 500 - (1 << (NUM_BITS - 1)) },
{ 4, -7, -25, -19, -9, 8, 86, 65, -14, -7, -7, 97, 168 - (1 << (NUM_BITS - 1)) },
{ 3, 3, 2, -30, 6, -34, 43, 71, -10, 4, -23, 77, 288 - (1 << (NUM_BITS - 1)) },
{ 12, -3, -34, -14, -5, -14, 88, 28, -12, 8, -34, 112, 248 - (1 << (NUM_BITS - 1)) },
{ -1, 6, 8, -29, 7, -27, 15, 60, -4, 6, -21, 39, 394 - (1 << (NUM_BITS - 1)) },
{ 8, -1, -7, -22, 5, -41, 63, 40, -13, 7, -28, 105, 280 - (1 << (NUM_BITS - 1)) },
{ 1, 3, -5, -1, 1, -10, 12, -1, 0, 3, -9, 19, 486 - (1 << (NUM_BITS - 1)) },
{ 10, -1, -23, -14, -3, -27, 78, 24, -14, 8, -28, 102, 288 - (1 << (NUM_BITS - 1)) },
{ 0, 0, -1, 0, 0, -1, 1, 0, 0, 0, 0, 1, 512 - (1 << (NUM_BITS - 1)) },
{ 7, 3, -19, -7, 2, -27, 51, 8, -6, 7, -24, 64, 394 - (1 << (NUM_BITS - 1)) },
{ 11, -10, -22, -22, -11, -12, 87, 49, -20, 4, -16, 108, 220 - (1 << (NUM_BITS - 1)) },
{ 17, -2, -69, -4, -4, 22, 106, 31, -7, 13, -63, 121, 190 - (1 << (NUM_BITS - 1)) },
{ 1, 4, -1, -7, 5, -26, 24, 0, 1, 3, -18, 51, 438 - (1 << (NUM_BITS - 1)) },
{ 3, 5, -10, -2, 4, -17, 17, 1, -2, 6, -16, 27, 480 - (1 << (NUM_BITS - 1)) },
{ 9, 2, -23, -5, 6, -45, 90, -22, 1, 7, -39, 121, 308 - (1 << (NUM_BITS - 1)) },
{ 4, 5, -15, -2, 4, -22, 34, -2, -2, 7, -22, 48, 438 - (1 << (NUM_BITS - 1)) },
{ 6, 8, -22, -3, 4, -32, 57, -3, -4, 11, -43, 102, 350 - (1 << (NUM_BITS - 1)) },
{ 2, 5, -11, 1, 12, -46, 64, -32, 7, 4, -31, 85, 392 - (1 << (NUM_BITS - 1)) },
{ 5, 5, -12, -8, 6, -48, 74, -13, -1, 7, -41, 129, 306 - (1 << (NUM_BITS - 1)) },
{ 0, 1, -1, 0, 1, -3, 2, 0, 0, 1, -3, 4, 508 - (1 << (NUM_BITS - 1)) },
{ -1, 3, 16, -42, 6, -16, 2, 105, 6, 6, -31, 43, 318 - (1 << (NUM_BITS - 1)) },
{ 7, 8, -27, -4, -4, -23, 46, 79, 64, -8, -13, 68, 126 - (1 << (NUM_BITS - 1)) },
{ -3, 12, -4, -34, 14, -6, -24, 179, 56, 2, -48, 15, 194 - (1 << (NUM_BITS - 1)) },
{ 8, 0, -16, -25, -1, -29, 68, 84, 3, -3, -18, 94, 182 - (1 << (NUM_BITS - 1)) },
{ -3, -1, 22, -32, 2, -20, 5, 89, 0, 9, -18, 40, 326 - (1 << (NUM_BITS - 1)) },
{ 14, 6, -51, 22, -10, -22, 36, 75, 106, -4, -11, 56, 78 - (1 << (NUM_BITS - 1)) },
{ 1, 38, -59, 14, 8, -44, -18, 156, 80, -1, -42, 29, 188 - (1 << (NUM_BITS - 1)) },
{ -1, 2, 4, -9, 3, -13, 7, 17, -4, 2, -6, 17, 474 - (1 << (NUM_BITS - 1)) },
{ 11, -2, -15, -36, 2, -32, 67, 89, -19, -1, -14, 103, 206 - (1 << (NUM_BITS - 1)) },
{ -1, 10, 3, -28, 7, -27, 7, 117, 34, 1, -35, 51, 234 - (1 << (NUM_BITS - 1)) },
{ 3, 3, 4, -18, 6, -40, 36, 18, -8, 7, -25, 86, 368 - (1 << (NUM_BITS - 1)) },
{ -1, 3, 9, -18, 5, -26, 12, 37, -11, 3, -7, 32, 436 - (1 << (NUM_BITS - 1)) },
{ 0, 17, -38, -9, -28, -17, 25, 48, 103, 2, 40, 69, 88 - (1 << (NUM_BITS - 1)) },
{ 6, 4, -11, -20, 5, -32, 51, 77, 17, 0, -25, 84, 200 - (1 << (NUM_BITS - 1)) },
{ 0, -5, 28, -24, -1, -22, 18, -9, 17, -1, -12, 107, 320 - (1 << (NUM_BITS - 1)) },
{ -10, -4, 17, -30, -29, 31, 40, 49, 44, -26, 67, 67, 80 - (1 << (NUM_BITS - 1)) },
{ -30, -12, 39, 15, -21, 32, 29, 26, 71, 20, 43, 28, 32 - (1 << (NUM_BITS - 1)) },
{ 6, -7, -7, -34, -21, 15, 53, 60, 12, -26, 45, 89, 142 - (1 << (NUM_BITS - 1)) },
{ -1, -5, 59, -58, -8, -30, 2, 17, 34, -7, 25, 111, 234 - (1 << (NUM_BITS - 1)) },
{ 7, 1, -7, -20, -9, -22, 48, 27, -4, -6, 0, 107, 268 - (1 << (NUM_BITS - 1)) },
{ -2, 22, 29, -70, -4, -28, 2, 19, 94, -40, 14, 110, 220 - (1 << (NUM_BITS - 1)) },
{ 13, 0, -22, -27, -11, -15, 66, 44, -7, -5, -10, 121, 218 - (1 << (NUM_BITS - 1)) },
{ 10, 6, -22, -14, -2, -33, 68, 15, -9, 5, -35, 135, 264 - (1 << (NUM_BITS - 1)) },
{ 2, 11, 4, -32, -3, -20, 23, 18, 17, -1, -28, 88, 354 - (1 << (NUM_BITS - 1)) },
{ 0, 3, -2, -1, 3, -16, 16, -3, 0, 2, -12, 35, 462 - (1 << (NUM_BITS - 1)) },
{ 1, 6, -6, -3, 10, -51, 70, -31, 5, 6, -42, 125, 332 - (1 << (NUM_BITS - 1)) },
{ 5, -7, 61, -71, -36, -6, -2, 15, 57, 18, 14, 108, 200 - (1 << (NUM_BITS - 1)) },
{ 9, 1, 35, -70, -73, 28, 13, 1, 96, 40, 36, 80, 120 - (1 << (NUM_BITS - 1)) },
{ 11, -7, 33, -72, -78, 48, 33, 37, 35, 7, 85, 76, 96 - (1 << (NUM_BITS - 1)) },
{ 4, 15, 1, -26, -24, -19, 32, 29, -8, -6, 21, 125, 224 - (1 << (NUM_BITS - 1)) },
{ 11, 8, 14, -57, -63, 21, 34, 51, 7, -3, 69, 89, 150 - (1 << (NUM_BITS - 1)) },
{ 7, 16, -7, -31, -38, -5, 41, 44, -11, -10, 45, 109, 192 - (1 << (NUM_BITS - 1)) },
{ 5, 16, 16, -46, -55, 3, 22, 32, 13, 0, 48, 107, 190 - (1 << (NUM_BITS - 1)) },
{ 2, 10, -3, -14, -9, -28, 39, 15, -10, -5, -1, 123, 274 - (1 << (NUM_BITS - 1)) },
{ 3, 11, 11, -27, -17, -24, 18, 22, 2, 4, 3, 100, 300 - (1 << (NUM_BITS - 1)) },
{ 0, 1, 7, -9, 3, -20, 16, 3, -2, 0, -9, 61, 410 - (1 << (NUM_BITS - 1)) },
};
const int alf_class_to_filter_mapping[MAX_NUM_ALF_CLASSES][ALF_FIXED_FILTER_NUM] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 9, 19, 32, 41, 42, 44, 46, 63 },
{ 0, 1, 2, 4, 5, 6, 7, 9, 11, 16, 25, 27, 28, 31, 32, 47 },
{ 5, 7, 9, 11, 12, 14, 15, 16, 17, 18, 19, 21, 22, 27, 31, 35 },
{ 7, 8, 9, 11, 14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 35, 36 },
{ 7, 8, 11, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 27 },
{ 1, 2, 3, 4, 6, 19, 29, 30, 33, 34, 37, 41, 42, 44, 47, 54 },
{ 1, 2, 3, 4, 6, 11, 28, 29, 30, 31, 32, 33, 34, 37, 47, 63 },
{ 0, 1, 4, 6, 10, 12, 13, 19, 28, 29, 31, 32, 34, 35, 36, 37 },
{ 6, 9, 10, 12, 13, 16, 19, 20, 28, 31, 35, 36, 37, 38, 39, 52 },
{ 7, 8, 10, 11, 12, 13, 19, 23, 25, 27, 28, 31, 35, 36, 38, 39 },
{ 1, 2, 3, 5, 29, 30, 33, 34, 40, 43, 44, 46, 54, 55, 59, 62 },
{ 1, 2, 3, 4, 29, 30, 31, 33, 34, 37, 40, 41, 43, 44, 59, 61 },
{ 0, 1, 3, 6, 19, 28, 29, 30, 31, 32, 33, 34, 37, 41, 44, 61 },
{ 1, 6, 10, 13, 19, 28, 29, 30, 32, 33, 34, 35, 37, 41, 48, 52 },
{ 0, 5, 6, 10, 19, 27, 28, 29, 32, 37, 38, 40, 41, 47, 49, 58 },
{ 1, 2, 3, 4, 11, 29, 33, 42, 43, 44, 45, 46, 48, 55, 56, 59 },
{ 0, 1, 2, 5, 7, 9, 29, 40, 43, 44, 45, 47, 48, 56, 59, 63 },
{ 0, 4, 5, 9, 14, 19, 26, 35, 36, 43, 45, 47, 48, 49, 50, 51 },
{ 9, 11, 12, 14, 16, 19, 20, 24, 26, 36, 38, 47, 49, 50, 51, 53 },
{ 7, 8, 13, 14, 20, 21, 24, 25, 26, 27, 35, 38, 47, 50, 52, 53 },
{ 1, 2, 4, 29, 33, 40, 41, 42, 43, 44, 45, 46, 54, 55, 56, 58 },
{ 2, 4, 32, 40, 42, 43, 44, 45, 46, 54, 55, 56, 58, 59, 60, 62 },
{ 0, 19, 42, 43, 45, 46, 48, 54, 55, 56, 57, 58, 59, 60, 61, 62 },
{ 8, 13, 36, 42, 45, 46, 51, 53, 54, 57, 58, 59, 60, 61, 62, 63 },
{ 8, 13, 20, 27, 36, 38, 42, 46, 52, 53, 56, 57, 59, 61, 62, 63 },
};
void alf_recon_coef(ADAPTIVE_LOOP_FILTER * alf, ALF_SLICE_PARAM* alf_slice_param, int channel, const BOOL is_rdo, const BOOL is_re_do)
{
int factor = is_rdo ? 0 : (1 << (NUM_BITS - 1));
ALF_FILTER_TYPE filter_type = channel == LUMA_CH ? alf_slice_param->luma_filter_type : ALF_FILTER_5;
int num_classes = channel == LUMA_CH ? MAX_NUM_ALF_CLASSES : 1;
int num_coef = filter_type == ALF_FILTER_5 ? 7 : 13;
int num_coef_minus1 = num_coef - 1;
int num_filters = channel == LUMA_CH ? alf_slice_param->num_luma_filters : 1;
short* coeff = channel == LUMA_CH ? alf_slice_param->luma_coef : alf_slice_param->chroma_coef;
if (channel == LUMA_CH)
{
if (alf_slice_param->coef_delta_pred_mode_flag)
{
for (int i = 1; i < num_filters; i++)
{
for (int j = 0; j < num_coef_minus1; j++)
{
coeff[i * MAX_NUM_ALF_LUMA_COEFF + j] += coeff[(i - 1) * MAX_NUM_ALF_LUMA_COEFF + j];
}
}
}
xeve_mset(alf->coef_final, 0, sizeof(alf->coef_final));
int num_coef_large_minus1 = MAX_NUM_ALF_LUMA_COEFF - 1;
for (int class_idx = 0; class_idx < num_classes; class_idx++)
{
int filter_idx = alf_slice_param->filter_coef_delta_idx[class_idx];
int fixed_filter_idx = alf_slice_param->fixed_filter_idx[class_idx];
u8 fixed_filter_usage_flag = alf_slice_param->fixed_filter_usage_flag[class_idx];
int fixed_filter_used = fixed_filter_usage_flag;
int fixed_filter_map_idx = fixed_filter_idx;
if (fixed_filter_used)
{
fixed_filter_idx = alf_class_to_filter_mapping[class_idx][fixed_filter_map_idx];
}
for (int i = 0; i < num_coef_large_minus1; i++)
{
int cur_coef = 0;
//fixed filter
if (fixed_filter_usage_flag > 0)
{
cur_coef = alf_fixed_filter_coef[fixed_filter_idx][i];
}
//add coded coeff
if (alf->filter_shapes[LUMA_CH][filter_type].pattern_to_large_filter[i] > 0)
{
int coeffIdx = alf->filter_shapes[LUMA_CH][filter_type].pattern_to_large_filter[i] - 1;
cur_coef += coeff[filter_idx * MAX_NUM_ALF_LUMA_COEFF + coeffIdx];
}
if (is_rdo == 0)
xeve_assert(cur_coef >= -(1 << 9) && cur_coef <= (1 << 9) - 1);
alf->coef_final[class_idx* MAX_NUM_ALF_LUMA_COEFF + i] = cur_coef;
}
//last coeff
int sum = 0;
for (int i = 0; i < num_coef_large_minus1; i++)
{
sum += (alf->coef_final[class_idx* MAX_NUM_ALF_LUMA_COEFF + i] << 1);
}
alf->coef_final[class_idx* MAX_NUM_ALF_LUMA_COEFF + num_coef_large_minus1] = factor - sum;
if (is_rdo == 0)
xeve_assert(alf->coef_final[class_idx* MAX_NUM_ALF_LUMA_COEFF + num_coef_large_minus1] >= -(1 << 10) && alf->coef_final[class_idx* MAX_NUM_ALF_LUMA_COEFF + num_coef_large_minus1] <= (1 << 10) - 1);
}
if (is_re_do && alf_slice_param->coef_delta_pred_mode_flag)
{
for (int i = num_filters - 1; i > 0; i--)
{
for (int j = 0; j < num_coef_minus1; j++)
{
coeff[i * MAX_NUM_ALF_LUMA_COEFF + j] = coeff[i * MAX_NUM_ALF_LUMA_COEFF + j] - coeff[(i - 1) * MAX_NUM_ALF_LUMA_COEFF + j];
}
}
}
}
else
{
for (int filter_idx = 0; filter_idx < num_filters; filter_idx++)
{
int sum = 0;
for (int i = 0; i < num_coef_minus1; i++)
{
sum += (coeff[filter_idx* MAX_NUM_ALF_LUMA_COEFF + i] << 1);
if (is_rdo == 0)
xeve_assert(coeff[filter_idx* MAX_NUM_ALF_LUMA_COEFF + i] >= -(1 << 9) && coeff[filter_idx* MAX_NUM_ALF_LUMA_COEFF + i] <= (1 << 9) - 1);
}
coeff[filter_idx* MAX_NUM_ALF_LUMA_COEFF + num_coef_minus1] = factor - sum;
if (is_rdo == 0)
xeve_assert(coeff[filter_idx* MAX_NUM_ALF_LUMA_COEFF + num_coef_minus1] >= -(1 << 10) && coeff[filter_idx* MAX_NUM_ALF_LUMA_COEFF + num_coef_minus1] <= (1 << 10) - 1);
}
return;
}
}
int alf_create(ADAPTIVE_LOOP_FILTER * alf, const int pic_width, const int pic_height, const int max_cu_width, const int max_cu_height, const int max_cu_depth, const int chroma_format_idc, int bit_depth)
{
int ret;
const int input_bit_depth[NUM_CH] = { bit_depth, bit_depth };
xeve_mset(alf->alf_idx_in_scan_order, 0, sizeof(u8) * APS_MAX_NUM);
alf->next_free_alf_idx_in_buf = 0;
alf->first_idx_poc = INT_MAX;
alf->last_idr_poc = INT_MAX;
alf->curr_poc = INT_MAX;
alf->curr_temp_layer = INT_MAX;
alf->alf_present_idr = 0;
alf->alf_idx_idr = INT_MAX;
alf->ac_alf_line_buf_curr_size = 0;
alf->last_ras_poc = INT_MAX;
alf->pending_ras_init = FALSE;
xeve_mcpy(alf->input_bit_depth, input_bit_depth, sizeof(alf->input_bit_depth));
alf->pic_width = pic_width;
alf->pic_height = pic_height;
alf->max_cu_width = max_cu_width;
alf->max_cu_height = max_cu_height;
alf->max_cu_depth = max_cu_depth;
alf->chroma_format = chroma_format_idc;
alf->num_ctu_in_widht = (alf->pic_width / alf->max_cu_width) + ((alf->pic_width % alf->max_cu_width) ? 1 : 0);
alf->num_ctu_in_height = (alf->pic_height / alf->max_cu_height) + ((alf->pic_height % alf->max_cu_height) ? 1 : 0);
alf->num_ctu_in_pic = alf->num_ctu_in_height * alf->num_ctu_in_widht;
alf_init_filter_shape(&alf->filter_shapes[LUMA_CH][0], 5);
alf_init_filter_shape(&alf->filter_shapes[LUMA_CH][1], 7);
alf_init_filter_shape(&alf->filter_shapes[CHROMA_CH][0], 5);
alf->temp_buf = (pel*)malloc((pic_width + (7 * alf->num_ctu_in_widht))*(pic_height + (7 * alf->num_ctu_in_height)) * sizeof(pel)); // +7 is of filter diameter //todo: check this
if(alf->chroma_format)
{
alf->temp_buf1 = (pel*)malloc(((pic_width >> 1) + (7 * alf->num_ctu_in_widht))*((pic_height >> 1) + (7 * alf->num_ctu_in_height)) * sizeof(pel)); // for chroma just left for unification
alf->temp_buf2 = (pel*)malloc(((pic_width >> 1) + (7 * alf->num_ctu_in_widht))*((pic_height >> 1) + (7 * alf->num_ctu_in_height)) * sizeof(pel));
}
alf->classifier_mt = (ALF_CLASSIFIER**)malloc(MAX_CU_SIZE * XEVE_MAX_THREADS * sizeof(ALF_CLASSIFIER*));
if (alf->classifier_mt)
{
for (int i = 0; i < MAX_CU_SIZE * XEVE_MAX_THREADS; i++)
{
alf->classifier_mt[i] = (ALF_CLASSIFIER*)malloc(MAX_CU_SIZE * sizeof(ALF_CLASSIFIER));
xeve_mset(alf->classifier_mt[i], 0, MAX_CU_SIZE * sizeof(ALF_CLASSIFIER));
}
}
// Classification
alf->classifier = (ALF_CLASSIFIER**)malloc(pic_height * sizeof(ALF_CLASSIFIER*));
xeve_assert_gv(alf->classifier, ret, XEVE_ERR_OUT_OF_MEMORY, ERR);
for (int i = 0; i < pic_height; i++)
{
alf->classifier[i] = (ALF_CLASSIFIER*)malloc(pic_width * sizeof(ALF_CLASSIFIER));
xeve_assert_gv( alf->classifier[i], ret, XEVE_ERR_OUT_OF_MEMORY, ERR);
xeve_mset(alf->classifier[i], 0, pic_width * sizeof(ALF_CLASSIFIER));
}
ERR:
return -1;
}
void alf_destroy(ADAPTIVE_LOOP_FILTER * alf)
{
free(alf->temp_buf);
free(alf->temp_buf1);
free(alf->temp_buf2);
if (alf->classifier)
{
for (int i = 0; i < alf->pic_height; i++)
{
free(alf->classifier[i]);
alf->classifier[i] = NULL;
}
free(alf->classifier);
alf->classifier = NULL;
}
if (alf->classifier_mt)
{
for (int i = 0; i < MAX_CU_SIZE * XEVE_MAX_THREADS; i++)
{
free(alf->classifier_mt[i]);
alf->classifier_mt[i] = NULL;
}
free(alf->classifier_mt);
alf->classifier_mt = NULL;
}
}
void alf_derive_classification(ADAPTIVE_LOOP_FILTER * alf, ALF_CLASSIFIER** classifier, const pel * src_luma, const int src_luma_stride, const AREA * blk)
{
int height = blk->y + blk->height;
int width = blk->x + blk->width;
for (int i = blk->y; i < height; i += CLASSIFICATION_BLK_SIZE)
{
int h = XEVE_MIN(i + CLASSIFICATION_BLK_SIZE, height) - i;
for (int j = blk->x; j < width; j += CLASSIFICATION_BLK_SIZE)
{
int w = XEVE_MIN(j + CLASSIFICATION_BLK_SIZE, width) - j;
AREA area = { j, i, w, h };
alf_derive_classification_blk(classifier, src_luma, src_luma_stride, &area, alf->input_bit_depth[LUMA_CH] + 4, alf->input_bit_depth[LUMA_CH]);
}
}
}
void alf_derive_classification_blk(ALF_CLASSIFIER ** classifier, const pel * src_luma, const int src_stride, const AREA * blk, const int shift, int bit_depth)
{
static const int th[16] = { 0, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4 };
const int stride = src_stride;
const pel * src = src_luma;
const int max_act = 15;
int fl = 2;
int flP1 = fl + 1;
int fl2 = 2 * fl;
int main_dir, sec_dir, dir_temp_hv, dir_temp_d;
int pix_y;
int height = blk->height + fl2;
int width = blk->width + fl2;
int pos_x = blk->x;
int pos_y = blk->y;
int start_h = pos_y - flP1;
int laplacian[NUM_DIRECTIONS][CLASSIFICATION_BLK_SIZE + 5][CLASSIFICATION_BLK_SIZE + 5];
for (int i = 0; i < height; i += 2)
{
int y_offset = (i + 1 + start_h) * stride - flP1;
const pel * src0 = &src[y_offset - stride];
const pel * src1 = &src[y_offset];
const pel * src2 = &src[y_offset + stride];
const pel * src3 = &src[y_offset + stride * 2];
int * y_ver = laplacian[VER][i];
int * y_hor = laplacian[HOR][i];
int * y_dig0 = laplacian[DIAG0][i];
int * y_dig1 = laplacian[DIAG1][i];
for (int j = 0; j < width; j += 2)
{
pix_y = j + 1 + pos_x;
const pel * y = src1 + pix_y;
const pel * y_down = src0 + pix_y;
const pel * y_up = src2 + pix_y;
const pel * y_up2 = src3 + pix_y;
const pel y0 = y[0] << 1;
const pel y1 = y[1] << 1;
const pel y_up0 = y_up[0] << 1;
const pel y_up1 = y_up[1] << 1;
y_ver[j] = abs(y0 - y_down[0] - y_up[0]) + abs(y1 - y_down[1] - y_up[1]) + abs(y_up0 - y[0] - y_up2[0]) + abs(y_up1 - y[1] - y_up2[1]);
y_hor[j] = abs(y0 - y[1] - y[-1]) + abs(y1 - y[2] - y[0]) + abs(y_up0 - y_up[1] - y_up[-1]) + abs(y_up1 - y_up[2] - y_up[0]);
y_dig0[j] = abs(y0 - y_down[-1] - y_up[1]) + abs(y1 - y_down[0] - y_up[2]) + abs(y_up0 - y[-1] - y_up2[1]) + abs(y_up1 - y[0] - y_up2[2]);
y_dig1[j] = abs(y0 - y_up[-1] - y_down[1]) + abs(y1 - y_up[0] - y_down[2]) + abs(y_up0 - y_up2[-1] - y[1]) + abs(y_up1 - y_up2[0] - y[2]);
if (j > 4 && (j - 6) % 4 == 0)
{
int jM6 = j - 6;
int jM4 = j - 4;
int jM2 = j - 2;
y_ver[jM6] += y_ver[jM4] + y_ver[jM2] + y_ver[j];
y_hor[jM6] += y_hor[jM4] + y_hor[jM2] + y_hor[j];
y_dig0[jM6] += y_dig0[jM4] + y_dig0[jM2] + y_dig0[j];
y_dig1[jM6] += y_dig1[jM4] + y_dig1[jM2] + y_dig1[j];
}
}
}
// classification block size
const int cls_size_y = 4;
const int cls_size_x = 4;
for (int i = 0; i < blk->height; i += cls_size_y)
{
int * y_ver = laplacian[VER][i];
int * y_ver2 = laplacian[VER][i + 2];
int * y_ver4 = laplacian[VER][i + 4];
int * y_ver6 = laplacian[VER][i + 6];
int * y_hor = laplacian[HOR][i];
int * y_hor2 = laplacian[HOR][i + 2];
int * y_hor4 = laplacian[HOR][i + 4];
int * y_hor6 = laplacian[HOR][i + 6];
int * y_dig0 = laplacian[DIAG0][i];
int * y_dig02 = laplacian[DIAG0][i + 2];
int * y_dig04 = laplacian[DIAG0][i + 4];
int * y_dig06 = laplacian[DIAG0][i + 6];
int * y_dig1 = laplacian[DIAG1][i];
int * y_dig12 = laplacian[DIAG1][i + 2];
int * y_dig14 = laplacian[DIAG1][i + 4];
int * y_dig16 = laplacian[DIAG1][i + 6];
for (int j = 0; j < blk->width; j += cls_size_x)
{
int sum_v = y_ver[j] + y_ver2[j] + y_ver4[j] + y_ver6[j];
int sum_h = y_hor[j] + y_hor2[j] + y_hor4[j] + y_hor6[j];
int sum_d0 = y_dig0[j] + y_dig02[j] + y_dig04[j] + y_dig06[j];
int sum_d1 = y_dig1[j] + y_dig12[j] + y_dig14[j] + y_dig16[j];
int temp_act = sum_v + sum_h;
int activity = (pel)XEVE_CLIP3(0, max_act, temp_act >> (bit_depth - 2));
int class_idx = th[activity];
int hv1, hv0, d1, d0, hvd1, hvd0;
if (sum_v > sum_h)
{
hv1 = sum_v;
hv0 = sum_h;
dir_temp_hv = 1;
}
else
{
hv1 = sum_h;
hv0 = sum_v;
dir_temp_hv = 3;
}
if (sum_d0 > sum_d1)
{
d1 = sum_d0;
d0 = sum_d1;
dir_temp_d = 0;
}
else
{
d1 = sum_d1;
d0 = sum_d0;
dir_temp_d = 2;
}
if (d1*hv0 > hv1*d0)
{
hvd1 = d1;
hvd0 = d0;
main_dir = dir_temp_d;
sec_dir = dir_temp_hv;
}
else
{
hvd1 = hv1;
hvd0 = hv0;
main_dir = dir_temp_hv;
sec_dir = dir_temp_d;
}
int directionStrength = 0;
if (hvd1 > 2 * hvd0)
{
directionStrength = 1;
}
if (hvd1 * 2 > 9 * hvd0)
{
directionStrength = 2;
}
if (directionStrength)
{
class_idx += (((main_dir & 0x1) << 1) + directionStrength) * 5;
}
static const int trans_tbl[8] = { 0, 1, 0, 2, 2, 3, 1, 3 };
int trans_idx = trans_tbl[main_dir * 2 + (sec_dir >> 1)];
int y_offset = i + pos_y;
int x_offset = j + pos_x;
ALF_CLASSIFIER *cl0 = classifier[y_offset] + x_offset;
ALF_CLASSIFIER *cl1 = classifier[y_offset + 1] + x_offset;
ALF_CLASSIFIER *cl2 = classifier[y_offset + 2] + x_offset;
ALF_CLASSIFIER *cl3 = classifier[y_offset + 3] + x_offset;
cl0[0] = cl0[1] = cl0[2] = cl0[3] = cl1[0] = cl1[1] = cl1[2] = cl1[3] = cl2[0] = cl2[1] = cl2[2] = cl2[3] = cl3[0] = cl3[1] = cl3[2] = cl3[3] = ((class_idx << 2) + trans_idx) & 0xFF;
}
}
}
void alf_filter_blk_7(ALF_CLASSIFIER** classifier, pel * rec_dst, const int dst_stride, const pel* rec_src, const int src_stride, const AREA* blk, const u8 comp_id, short* filter_set, const CLIP_RANGE* clip_range)
{
const BOOL is_chroma = FALSE;
const int start_h = blk->y;
const int end_h = blk->y + blk->height;
const int start_w = blk->x;
const int end_w = blk->x + blk->width;
const pel* src = rec_src;
pel* dst = rec_dst;
const pel *img_y_pad0, *img_y_pad1, *img_y_pad2, *img_y_pad3, *img_y_pad4, *img_y_pad5, *img_y_pad6;
const pel *img0, *img1, *img2, *img3, *img4, *img5, *img6;
short *coef = filter_set;
const int shift = 9;
const int offset = 1 << (shift - 1);
int trans_idx = 0;
const int cls_size_y = 4;
const int cls_size_x = 4;
CHECK(start_h % cls_size_y, "Wrong start_h in filtering");
CHECK(start_w % cls_size_x, "Wrong start_w in filtering");
CHECK((end_h - start_h) % cls_size_y, "Wrong end_h in filtering");
CHECK((end_w - start_w) % cls_size_x, "Wrong end_w in filtering");
ALF_CLASSIFIER *alf_class = NULL;
int dst_stride2 = dst_stride * cls_size_y;
int src_stride2 = src_stride * cls_size_y;
pel filter_coef[MAX_NUM_ALF_LUMA_COEFF];
img_y_pad0 = src;
img_y_pad1 = img_y_pad0 + src_stride;
img_y_pad2 = img_y_pad0 - src_stride;
img_y_pad3 = img_y_pad1 + src_stride;
img_y_pad4 = img_y_pad2 - src_stride;
img_y_pad5 = img_y_pad3 + src_stride;
img_y_pad6 = img_y_pad4 - src_stride;
pel * rec0 = dst;
pel * rec1 = rec0 + dst_stride;
for (int i = 0; i < end_h - start_h; i += cls_size_y)
{
if (!is_chroma)
{
alf_class = classifier[start_h + i] + start_w;
}
for (int j = 0; j < end_w - start_w; j += cls_size_x)
{
ALF_CLASSIFIER cl = alf_class[j];
trans_idx = cl & 0x03;
coef = filter_set + ((cl >> 2) & 0x1F) * MAX_NUM_ALF_LUMA_COEFF;
const int l[4][MAX_NUM_ALF_LUMA_COEFF] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
{ 9, 4, 10, 8, 1, 5, 11, 7, 3, 0, 2, 6, 12 },
{ 0, 3, 2, 1, 8, 7, 6, 5, 4, 9, 10, 11, 12 },
{ 9, 8, 10, 4, 3, 7, 11, 5, 1, 0, 2, 6, 12 }
};
for (int i = 0; i < MAX_NUM_ALF_LUMA_COEFF; i++)
{
filter_coef[i] = coef[l[trans_idx][i]];
}
for (int ii = 0; ii < cls_size_y; ii++)
{
img0 = img_y_pad0 + j + ii * src_stride;
img1 = img_y_pad1 + j + ii * src_stride;
img2 = img_y_pad2 + j + ii * src_stride;
img3 = img_y_pad3 + j + ii * src_stride;
img4 = img_y_pad4 + j + ii * src_stride;
img5 = img_y_pad5 + j + ii * src_stride;
img6 = img_y_pad6 + j + ii * src_stride;
rec1 = rec0 + j + ii * dst_stride;
for (int jj = 0; jj < cls_size_x; jj++)
{
int sum = 0;
sum += filter_coef[0] * (img5[0] + img6[0]);
sum += filter_coef[1] * (img3[+1] + img4[-1]);
sum += filter_coef[2] * (img3[+0] + img4[+0]);
sum += filter_coef[3] * (img3[-1] + img4[+1]);
sum += filter_coef[4] * (img1[+2] + img2[-2]);
sum += filter_coef[5] * (img1[+1] + img2[-1]);
sum += filter_coef[6] * (img1[+0] + img2[+0]);
sum += filter_coef[7] * (img1[-1] + img2[+1]);
sum += filter_coef[8] * (img1[-2] + img2[+2]);
sum += filter_coef[9] * (img0[+3] + img0[-3]);
sum += filter_coef[10] * (img0[+2] + img0[-2]);
sum += filter_coef[11] * (img0[+1] + img0[-1]);
sum += filter_coef[12] * (img0[+0]);
sum = (sum + offset) >> shift;
rec1[jj] = clip_pel(sum, *clip_range);
img0++;
img1++;
img2++;
img3++;
img4++;
img5++;
img6++;
}
}
}
rec0 += dst_stride2;
rec1 += dst_stride2;
img_y_pad0 += src_stride2;
img_y_pad1 += src_stride2;
img_y_pad2 += src_stride2;
img_y_pad3 += src_stride2;
img_y_pad4 += src_stride2;
img_y_pad5 += src_stride2;
img_y_pad6 += src_stride2;
}
}
void alf_filter_blk_5(ALF_CLASSIFIER** classifier, pel * rec_dst, const int dst_stride, const pel* rec_src, const int src_stride, const AREA* blk, const u8 comp_id, short* filter_set, const CLIP_RANGE* clip_range)
{
const int start_h = blk->y;
const int end_h = blk->y + blk->height;
const int start_w = blk->x;
const int end_w = blk->x + blk->width;
const pel* src = rec_src;
pel* dst = rec_dst;
const pel *img_y_pad0, *img_y_pad1, *img_y_pad2, *img_y_pad3, *img_y_pad4;
const pel *img0, *img1, *img2, *img3, *img4;
short *coef = filter_set;
const int shift = 9;
const int offset = 1 << (shift - 1);
int trans_idx = 0;
const int cls_size_y = 1;
const int cls_size_x = 1;
ALF_CLASSIFIER *alf_class = NULL;
int dst_stride2 = dst_stride * cls_size_y;
int src_stride2 = src_stride * cls_size_y;
pel filter_coef[MAX_NUM_ALF_LUMA_COEFF];
img_y_pad0 = src;
img_y_pad1 = img_y_pad0 + src_stride;
img_y_pad2 = img_y_pad0 - src_stride;
img_y_pad3 = img_y_pad1 + src_stride;
img_y_pad4 = img_y_pad2 - src_stride;
pel* rec0 = dst;
pel* rec1 = rec0 + dst_stride;
for (int i = 0; i < end_h - start_h; i += cls_size_y)
{
for (int j = 0; j < end_w - start_w; j += cls_size_x)
{
for (int i = 0; i < MAX_NUM_ALF_CHROMA_COEFF; i++)
{
filter_coef[i] = coef[i];
}
for (int ii = 0; ii < cls_size_y; ii++)
{
img0 = img_y_pad0 + j + ii * src_stride;
img1 = img_y_pad1 + j + ii * src_stride;
img2 = img_y_pad2 + j + ii * src_stride;
img3 = img_y_pad3 + j + ii * src_stride;
img4 = img_y_pad4 + j + ii * src_stride;
rec1 = rec0 + j + ii * dst_stride;
for (int jj = 0; jj < cls_size_x; jj++)
{
int sum = 0;
sum += filter_coef[0] * (img3[+0] + img4[+0]);
sum += filter_coef[1] * (img1[+1] + img2[-1]);
sum += filter_coef[2] * (img1[+0] + img2[+0]);
sum += filter_coef[3] * (img1[-1] + img2[+1]);
sum += filter_coef[4] * (img0[+2] + img0[-2]);
sum += filter_coef[5] * (img0[+1] + img0[-1]);
sum += filter_coef[6] * (img0[+0]);
sum = (sum + offset) >> shift;
rec1[jj] = clip_pel(sum, *clip_range);
img0++;
img1++;
img2++;
img3++;
img4++;
}
}
}
rec0 += dst_stride2;
rec1 += dst_stride2;
img_y_pad0 += src_stride2;
img_y_pad1 += src_stride2;
img_y_pad2 += src_stride2;
img_y_pad3 += src_stride2;
img_y_pad4 += src_stride2;
}
}
void alf_param_chroma(ALF_SLICE_PARAM* dst, ALF_SLICE_PARAM* src)
{
xeve_mcpy(dst->chroma_coef, src->chroma_coef, sizeof(short)*MAX_NUM_ALF_CHROMA_COEFF);
dst->chroma_filter_present = src->chroma_filter_present;
dst->chroma_ctb_present_flag = src->chroma_ctb_present_flag;
dst->enable_flag[1] = src->enable_flag[1];
dst->enable_flag[2] = src->enable_flag[2];
}
void alf_copy_param(ALF_SLICE_PARAM* dst, ALF_SLICE_PARAM* src)
{
xeve_mcpy(dst->enable_flag, src->enable_flag, sizeof(BOOL)*N_C);
dst->chroma_filter_present = src->chroma_filter_present;
xeve_mcpy(dst->luma_coef, src->luma_coef, sizeof(short)*MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF);
xeve_mcpy(dst->chroma_coef, src->chroma_coef, sizeof(short)*MAX_NUM_ALF_CHROMA_COEFF);
xeve_mcpy(dst->filter_coef_delta_idx, src->filter_coef_delta_idx, sizeof(short)*MAX_NUM_ALF_CLASSES);
xeve_mcpy(dst->filter_coef_flag, src->filter_coef_flag, sizeof(BOOL)*MAX_NUM_ALF_CLASSES);
xeve_mcpy(dst->fixed_filter_idx, src->fixed_filter_idx, sizeof(int)*MAX_NUM_ALF_CLASSES);
xeve_mcpy(dst->fixed_filter_usage_flag, src->fixed_filter_usage_flag, sizeof(u8)*MAX_NUM_ALF_CLASSES);
dst->luma_filter_type = src->luma_filter_type;
dst->num_luma_filters = src->num_luma_filters;
dst->coef_delta_flag = src->coef_delta_flag;
dst->coef_delta_pred_mode_flag = src->coef_delta_pred_mode_flag;
dst->filterShapes = src->filterShapes;
dst->chroma_ctb_present_flag = src->chroma_ctb_present_flag;
dst->fixed_filter_pattern = src->fixed_filter_pattern;
dst->temporal_alf_flag = src->temporal_alf_flag;
dst->prev_idx = src->prev_idx;
dst->prev_idx_comp[0] = src->prev_idx_comp[0];
dst->prev_idx_comp[1] = src->prev_idx_comp[1];
dst->t_layer = src->t_layer;
dst->filter_poc = src->filter_poc;
dst->min_idr_poc = src->min_idr_poc;
dst->max_idr_poc = src->max_idr_poc;
}
void alf_reset_param(ALF_SLICE_PARAM* dst)
{
//Reset destination
dst->is_ctb_alf_on = FALSE;
xeve_mset(dst->enable_flag, 0, sizeof(dst->enable_flag)); //false is still 0
dst->luma_filter_type = ALF_FILTER_5;
xeve_mset(dst->luma_coef, 0, sizeof(dst->luma_coef));
xeve_mset(dst->chroma_coef, 0, sizeof(dst->chroma_coef));
xeve_mset(dst->filter_coef_delta_idx, 0, sizeof(dst->filter_coef_delta_idx));
for (int i = 0; i < MAX_NUM_ALF_CLASSES; i++)
dst->filter_coef_flag[i] = TRUE;
dst->num_luma_filters = 1;
dst->coef_delta_flag = FALSE;
dst->coef_delta_pred_mode_flag = FALSE;
dst->chroma_ctb_present_flag = FALSE;
dst->fixed_filter_pattern = 0;
xeve_mset(dst->fixed_filter_idx, 0, sizeof(dst->fixed_filter_idx));
xeve_mset(dst->fixed_filter_usage_flag, 0, sizeof(dst->fixed_filter_usage_flag));
dst->temporal_alf_flag = FALSE;
dst->prev_idx = 0;
dst->prev_idx_comp[0] = 0;
dst->prev_idx_comp[1] = 0;
dst->t_layer = 0;
dst->reset_alf_buf_flag = FALSE;
dst->store2_alf_buf_flag = FALSE;
dst->filter_poc = INT_MAX; // store POC value for which filter was produced
dst->min_idr_poc = INT_MAX; // Minimal of 2 IDR POC available for current coded nalu (to identify availability of this filter for temp prediction)
dst->max_idr_poc = INT_MAX; // Max of 2 IDR POC available for current coded nalu (to identify availability of this filter for temp prediction)
}
void alf_reset_idr_idx_list_buf_aps(ADAPTIVE_LOOP_FILTER * alf)
{
if (alf->alf_present_idr)
{
alf->alf_idx_in_scan_order[0] = alf->alf_idx_idr;
alf->ac_alf_line_buf_curr_size = 1;
alf->next_free_alf_idx_in_buf = (alf->alf_idx_idr + 1) % APS_MAX_NUM;
alf->alf_present_idr = 0;
}
else
{
alf->alf_idx_in_scan_order[0] = 0;
alf->ac_alf_line_buf_curr_size = 0;
alf->next_free_alf_idx_in_buf = 0;
}
}
int alf_get_protect_idx_from_list(ADAPTIVE_LOOP_FILTER * alf, int idx)
{
u8 i_slice_idx = 0;
int protect_entry = 0;
if (alf->i_period == 0)
{
return protect_entry;
}
// check if current idx is protected (e.g. idr filter idx)
if ((alf->ac_alf_line_buf[idx].filter_poc == alf->ac_alf_line_buf[idx].max_idr_poc))
{
protect_entry = 1; // previent overwrite of the protected ALF id (e.g. id of IDR pic)
}
if ((alf->curr_poc > alf->ac_alf_line_buf[idx].max_idr_poc + alf->i_period))
{
protect_entry = 0;
}
if ((alf->curr_poc > alf->last_idr_poc) // current POC is after 2nd IDR
&& (alf->ac_alf_line_buf[idx].filter_poc < alf->last_idr_poc)) // POC of checked ALF is before 2nd IDR
{
protect_entry = 0;
}
if ((alf->curr_poc > alf->ac_alf_line_buf[idx].max_idr_poc) // current POC is after 2nd IDR
&& (alf->ac_alf_line_buf[idx].filter_poc < alf->ac_alf_line_buf[idx].max_idr_poc)) // POC of checked ALF is before 2nd IDR
{
protect_entry = 0;
}