-
Notifications
You must be signed in to change notification settings - Fork 755
/
integer_adm.c
2776 lines (2446 loc) · 114 KB
/
integer_adm.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
/**
*
* Copyright 2016-2020 Netflix, Inc.
*
* Licensed under the BSD+Patent License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSDplusPatent
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "cpu.h"
#include "dict.h"
#include "feature_collector.h"
#include "feature_extractor.h"
#include "feature_name.h"
#include "integer_adm.h"
#include "log.h"
#if ARCH_X86
#include "x86/adm_avx2.h"
#elif ARCH_AARCH64
#include "arm64/adm_neon.h"
#include <arm_neon.h>
#endif
typedef struct AdmState {
size_t integer_stride;
AdmBuffer buf;
bool debug;
double adm_enhn_gain_limit;
double adm_norm_view_dist;
int adm_ref_display_height;
void (*dwt2_8)(const uint8_t *src, const adm_dwt_band_t *dst,
AdmBuffer *buf, int w, int h, int src_stride,
int dst_stride);
VmafDictionary *feature_name_dict;
} AdmState;
static const VmafOption options[] = {
{
.name = "debug",
.help = "debug mode: enable additional output",
.offset = offsetof(AdmState, debug),
.type = VMAF_OPT_TYPE_BOOL,
.default_val.b = false,
},
{
.name = "adm_enhn_gain_limit",
.alias = "egl",
.help = "enhancement gain imposed on adm, must be >= 1.0, "
"where 1.0 means the gain is completely disabled",
.offset = offsetof(AdmState, adm_enhn_gain_limit),
.type = VMAF_OPT_TYPE_DOUBLE,
.default_val.d = DEFAULT_ADM_ENHN_GAIN_LIMIT,
.min = 1.0,
.max = DEFAULT_ADM_ENHN_GAIN_LIMIT,
.flags = VMAF_OPT_FLAG_FEATURE_PARAM,
},
{
.name = "adm_norm_view_dist",
.alias = "nvd",
.help = "normalized viewing distance = viewing distance / ref display's physical height",
.offset = offsetof(AdmState, adm_norm_view_dist),
.type = VMAF_OPT_TYPE_DOUBLE,
.default_val.d = DEFAULT_ADM_NORM_VIEW_DIST,
.min = 0.75,
.max = 24.0,
.flags = VMAF_OPT_FLAG_FEATURE_PARAM,
},
{
.name = "adm_ref_display_height",
.alias = "rdh",
.help = "reference display height in pixels",
.offset = offsetof(AdmState, adm_ref_display_height),
.type = VMAF_OPT_TYPE_INT,
.default_val.i = DEFAULT_ADM_REF_DISPLAY_HEIGHT,
.min = 1,
.max = 4320,
.flags = VMAF_OPT_FLAG_FEATURE_PARAM,
},
{ 0 }
};
/*
* lambda = 0 (finest scale), 1, 2, 3 (coarsest scale);
* theta = 0 (ll), 1 (lh - vertical), 2 (hh - diagonal), 3(hl - horizontal).
*/
static inline float
dwt_quant_step(const struct dwt_model_params *params, int lambda, int theta,
double adm_norm_view_dist, int adm_ref_display_height)
{
// Formula (1), page 1165 - display visual resolution (DVR), in pixels/degree
// of visual angle. This should be 56.55
float r = adm_norm_view_dist * adm_ref_display_height * M_PI / 180.0;
// Formula (9), page 1171
float temp = log10(pow(2.0, lambda + 1)*params->f0*params->g[theta] / r);
float Q = 2.0*params->a*pow(10.0, params->k*temp*temp) /
dwt_7_9_basis_function_amplitudes[lambda][theta];
return Q;
}
// i = 0, j = 0: indices y: 1,0,1, x: 1,0,1 for Fixed-point
#define ADM_CM_THRESH_S_0_0(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
sum += flt_ptr[src_stride + 1]; \
sum += flt_ptr[src_stride]; \
sum += flt_ptr[src_stride + 1]; \
sum += flt_ptr[1]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[0]))+ 2048)>>12);\
sum += flt_ptr[1]; \
sum += flt_ptr[src_stride + 1]; \
sum += flt_ptr[src_stride]; \
sum += flt_ptr[src_stride + 1]; \
*accum += sum; \
} \
}
// i = 0, j = w-1: indices y: 1,0,1, x: w-2, w-1, w-1
#define ADM_CM_THRESH_S_0_W_M_1(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
sum += flt_ptr[src_stride + w - 2]; \
sum += flt_ptr[src_stride + w - 1]; \
sum += flt_ptr[src_stride + w - 1]; \
sum += flt_ptr[w - 2]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[w - 1]))+ 2048)>>12);\
sum += flt_ptr[w - 1]; \
sum += flt_ptr[src_stride + w - 2]; \
sum += flt_ptr[src_stride + w - 1]; \
sum += flt_ptr[src_stride + w - 1]; \
*accum += sum; \
} \
}
// i = 0, j = 1, ..., w-2: indices y: 1,0,1, x: j-1,j,j+1
#define ADM_CM_THRESH_S_0_J(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
sum += flt_ptr[src_stride + j - 1]; \
sum += flt_ptr[src_stride + j]; \
sum += flt_ptr[src_stride + j + 1]; \
sum += flt_ptr[j - 1]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[j]))+ 2048)>>12);\
sum += flt_ptr[j + 1]; \
sum += flt_ptr[src_stride + j - 1]; \
sum += flt_ptr[src_stride + j]; \
sum += flt_ptr[src_stride + j + 1]; \
*accum += sum; \
} \
}
// i = h-1, j = 0: indices y: h-2,h-1,h-1, x: 1,0,1
#define ADM_CM_THRESH_S_H_M_1_0(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
src_ptr += (src_stride * (h - 2)); \
flt_ptr += (src_stride * (h - 2)); \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[1]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[0]))+ 2048)>>12);\
sum += flt_ptr[1]; \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
*accum += sum; \
} \
}
// i = h-1, j = w-1: indices y: h-2,h-1,h-1, x: w-2, w-1, w-1
#define ADM_CM_THRESH_S_H_M_1_W_M_1(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (h - 2)); \
flt_ptr += (src_stride * (h - 2)); \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[w - 2]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[w - 1]))+ 2048)>>12);\
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
*accum += sum; \
} \
}
// i = h-1, j = 1, ..., w-2: indices y: h-2,h-1,h-1, x: j-1,j,j+1
#define ADM_CM_THRESH_S_H_M_1_J(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (h - 2)); \
flt_ptr += (src_stride * (h - 2)); \
sum += flt_ptr[j - 1];\
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[j - 1]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[j]))+ 2048)>>12);\
sum += flt_ptr[j + 1]; \
sum += flt_ptr[j - 1]; \
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
*accum += sum; \
} \
}
// i = 1,..,h-2, j = 1,..,w-2: indices y: i-1,i,i+1, x: j-1,j,j+1
#define ADM_CM_THRESH_S_I_J(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
src_ptr += (src_stride * (i - 1)); \
flt_ptr += (src_stride * (i - 1)); \
sum += flt_ptr[j - 1]; \
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[j - 1]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[j]))+ 2048)>>12);\
sum += flt_ptr[j + 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[j - 1]; \
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
*accum += sum; \
} \
}
// i = 1,..,h-2, j = 0: indices y: i-1,i,i+1, x: 1,0,1
#define ADM_CM_THRESH_S_I_0(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (i - 1)); \
flt_ptr += (src_stride * (i - 1)); \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[1]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[0]))+ 2048)>>12);\
sum += flt_ptr[1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
*accum += sum; \
} \
}
// i = 1,..,h-2, j = w-1: indices y: i-1,i,i+1, x: w-2,w-1,w-1
#define ADM_CM_THRESH_S_I_W_M_1(angles,flt_angles,src_stride,accum,w,h,i,j) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int16_t *src_ptr = angles[theta]; \
int16_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (i-1)); \
flt_ptr += (src_stride * (i - 1)); \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[w - 2]; \
sum += (int16_t)(((ONE_BY_15 * abs((int32_t) src_ptr[w - 1]))+ 2048)>>12);\
sum += flt_ptr[w - 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
*accum += sum; \
} \
}
// i = 0, j = 0: indices y: 1,0,1, x: 1,0,1 for Fixed-point
#define I4_ADM_CM_THRESH_S_0_0(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
sum += flt_ptr[src_stride + 1]; \
sum += flt_ptr[src_stride]; \
sum += flt_ptr[src_stride + 1]; \
sum += flt_ptr[1]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs( src_ptr[0]))+ add_bef_shift)>>shift);\
sum += flt_ptr[1]; \
sum += flt_ptr[src_stride + 1]; \
sum += flt_ptr[src_stride]; \
sum += flt_ptr[src_stride + 1]; \
*accum += sum; \
} \
}
// i = 0, j = w-1: indices y: 1,0,1, x: w-2, w-1, w-1
#define I4_ADM_CM_THRESH_S_0_W_M_1(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
sum += flt_ptr[src_stride + w - 2]; \
sum += flt_ptr[src_stride + w - 1]; \
sum += flt_ptr[src_stride + w - 1]; \
sum += flt_ptr[w - 2]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[w - 1]))+ add_bef_shift)>>shift);\
sum += flt_ptr[w - 1]; \
sum += flt_ptr[src_stride + w - 2]; \
sum += flt_ptr[src_stride + w - 1]; \
sum += flt_ptr[src_stride + w - 1]; \
*accum += sum; \
} \
}
// i = 0, j = 1, ..., w-2: indices y: 1,0,1, x: j-1,j,j+1
#define I4_ADM_CM_THRESH_S_0_J(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
sum += flt_ptr[src_stride + j - 1]; \
sum += flt_ptr[src_stride + j]; \
sum += flt_ptr[src_stride + j + 1]; \
sum += flt_ptr[j - 1]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[j]))+ add_bef_shift)>>shift);\
sum += flt_ptr[j + 1]; \
sum += flt_ptr[src_stride + j - 1]; \
sum += flt_ptr[src_stride + j]; \
sum += flt_ptr[src_stride + j + 1]; \
*accum += sum; \
} \
}
// i = h-1, j = 0: indices y: h-2,h-1,h-1, x: 1,0,1
#define I4_ADM_CM_THRESH_S_H_M_1_0(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
src_ptr += (src_stride * (h - 2)); \
flt_ptr += (src_stride * (h - 2)); \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[1]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[0]))+ add_bef_shift)>>shift);\
sum += flt_ptr[1]; \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
*accum += sum; \
} \
}
// i = h-1, j = w-1: indices y: h-2,h-1,h-1, x: w-2, w-1, w-1
#define I4_ADM_CM_THRESH_S_H_M_1_W_M_1(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (h - 2)); \
flt_ptr += (src_stride * (h - 2)); \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[w - 2]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[w - 1]))+ add_bef_shift)>>shift);\
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
*accum += sum; \
} \
}
// i = h-1, j = 1, ..., w-2: indices y: h-2,h-1,h-1, x: j-1,j,j+1
#define I4_ADM_CM_THRESH_S_H_M_1_J(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (h - 2)); \
flt_ptr += (src_stride * (h - 2)); \
sum += flt_ptr[j - 1];\
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[j - 1]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[j]))+ add_bef_shift)>>shift);\
sum += flt_ptr[j + 1]; \
sum += flt_ptr[j - 1]; \
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
*accum += sum; \
} \
}
// i = 1,..,h-2, j = 1,..,w-2: indices y: i-1,i,i+1, x: j-1,j,j+1
#define I4_ADM_CM_THRESH_S_I_J(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t sum = 0; \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
src_ptr += (src_stride * (i - 1)); \
flt_ptr += (src_stride * (i - 1)); \
sum += flt_ptr[j - 1]; \
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[j - 1]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[j]))+ add_bef_shift)>>shift);\
sum += flt_ptr[j + 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[j - 1]; \
sum += flt_ptr[j]; \
sum += flt_ptr[j + 1]; \
*accum += sum; \
} \
}
// i = 1,..,h-2, j = 0: indices y: i-1,i,i+1, x: 1,0,1
#define I4_ADM_CM_THRESH_S_I_0(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (i - 1)); \
flt_ptr += (src_stride * (i - 1)); \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[1]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[0]))+ add_bef_shift)>>shift);\
sum += flt_ptr[1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[1]; \
sum += flt_ptr[0]; \
sum += flt_ptr[1]; \
*accum += sum; \
} \
}
// i = 1,..,h-2, j = w-1: indices y: i-1,i,i+1, x: w-2,w-1,w-1
#define I4_ADM_CM_THRESH_S_I_W_M_1(angles,flt_angles,src_stride,accum,w,h,i,j,add_bef_shift,shift) \
{ \
*accum = 0; \
for (int theta = 0; theta < 3; ++theta) { \
int32_t *src_ptr = angles[theta]; \
int32_t *flt_ptr = flt_angles[theta]; \
int32_t sum = 0; \
src_ptr += (src_stride * (i-1)); \
flt_ptr += (src_stride * (i - 1)); \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[w - 2]; \
sum += (int32_t)((((int64_t)I4_ONE_BY_15 * abs((int32_t) src_ptr[w - 1]))+ add_bef_shift)>>shift);\
sum += flt_ptr[w - 1]; \
src_ptr += src_stride; \
flt_ptr += src_stride; \
sum += flt_ptr[w - 2]; \
sum += flt_ptr[w - 1]; \
sum += flt_ptr[w - 1]; \
*accum += sum; \
} \
}
#define ADM_CM_ACCUM_ROUND(x, thr, shift_xsub, x_sq, add_shift_xsq, shift_xsq, val, \
add_shift_xcub, shift_xcub, accum_inner) \
{ \
x = abs(x) - ((int32_t)(thr) << shift_xsub); \
x = x < 0 ? 0 : x; \
x_sq = (int32_t)((((int64_t)x * x) + add_shift_xsq) >> shift_xsq); \
val = (((int64_t)x_sq * x) + add_shift_xcub) >> shift_xcub; \
accum_inner += val; \
}
#define I4_ADM_CM_ACCUM_ROUND(x, thr, shift_sub, x_sq, add_shift_sq, shift_sq, val, \
add_shift_cub, shift_cub, accum_inner) \
{ \
x = abs(x) - (thr >> shift_sub); \
x = x < 0 ? 0 : x; \
x_sq = (int32_t)((((int64_t)x * x) + add_shift_sq) >> shift_sq); \
val = (((int64_t)x_sq * x) + add_shift_cub) >> shift_cub; \
accum_inner += val; \
}
static void dwt2_src_indices_filt(int **src_ind_y, int **src_ind_x, int w, int h)
{
int ind0, ind1, ind2, ind3;
const unsigned h_half = (h + 1) / 2;
const unsigned w_half = (w + 1) / 2;
unsigned i, j;
/* Vertical pass */
{ /* i : 0 */
src_ind_y[0][0] = 1;
src_ind_y[1][0] = 0;
src_ind_y[2][0] = 1;
src_ind_y[3][0] = 2;
}
for (i = 1; i < h_half - 2; ++i) { /* i : 1 to h_half - 3*/
ind1 = 2 * i;
ind0 = ind1 - 1;
ind2 = ind1 + 1;
ind3 = ind1 + 2;
src_ind_y[0][i] = ind0;
src_ind_y[1][i] = ind1;
src_ind_y[2][i] = ind2;
src_ind_y[3][i] = ind3;
}
for (i = h_half - 2; i < h_half; ++i) { /* i : h_half - 3 to h_half */
ind1 = 2 * i;
ind0 = ind1 - 1;
ind2 = ind1 + 1;
ind3 = ind1 + 2;
if (ind0 >= h) {
ind0 = (2 * h - ind0 - 1);
}
if (ind1 >= h) {
ind1 = (2 * h - ind1 - 1);
}
if (ind2 >= h) {
ind2 = (2 * h - ind2 - 1);
}
if (ind3 >= h) {
ind3 = (2 * h - ind3 - 1);
}
src_ind_y[0][i] = ind0;
src_ind_y[1][i] = ind1;
src_ind_y[2][i] = ind2;
src_ind_y[3][i] = ind3;
}
/* Horizontal pass */
{ /* j : 0 */
src_ind_x[0][0] = 1;
src_ind_x[1][0] = 0;
src_ind_x[2][0] = 1;
src_ind_x[3][0] = 2;
}
for (j = 1; j < w_half - 2; ++j) { /* j : 1 to w_half - 3 */
ind1 = 2 * j;
ind0 = ind1 - 1;
ind2 = ind1 + 1;
ind3 = ind1 + 2;
src_ind_x[0][j] = ind0;
src_ind_x[1][j] = ind1;
src_ind_x[2][j] = ind2;
src_ind_x[3][j] = ind3;
}
for (j = w_half - 2; j < w_half; ++j) { /* j : w_half - 3 to w_half */
ind1 = 2 * j;
ind0 = ind1 - 1;
ind2 = ind1 + 1;
ind3 = ind1 + 2;
if (ind0 >= w) {
ind0 = (2 * w - ind0 - 1);
}
if (ind1 >= w) {
ind1 = (2 * w - ind1 - 1);
}
if (ind2 >= w) {
ind2 = (2 * w - ind2 - 1);
}
if (ind3 >= w) {
ind3 = (2 * w - ind3 - 1);
}
src_ind_x[0][j] = ind0;
src_ind_x[1][j] = ind1;
src_ind_x[2][j] = ind2;
src_ind_x[3][j] = ind3;
}
}
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
static void adm_decouple(AdmBuffer *buf, int w, int h, int stride,
double adm_enhn_gain_limit)
{
const float cos_1deg_sq = cos(1.0 * M_PI / 180.0) * cos(1.0 * M_PI / 180.0);
const adm_dwt_band_t *ref = &buf->ref_dwt2;
const adm_dwt_band_t *dis = &buf->dis_dwt2;
const adm_dwt_band_t *r = &buf->decouple_r;
const adm_dwt_band_t *a = &buf->decouple_a;
int left = w * ADM_BORDER_FACTOR - 0.5 - 1; // -1 for filter tap
int top = h * ADM_BORDER_FACTOR - 0.5 - 1;
int right = w - left + 2; // +2 for filter tap
int bottom = h - top + 2;
if (left < 0) {
left = 0;
}
if (right > w) {
right = w;
}
if (top < 0) {
top = 0;
}
if (bottom > h) {
bottom = h;
}
int64_t ot_dp, o_mag_sq, t_mag_sq;
for (int i = top; i < bottom; ++i) {
for (int j = left; j < right; ++j) {
int16_t oh = ref->band_h[i * stride + j];
int16_t ov = ref->band_v[i * stride + j];
int16_t od = ref->band_d[i * stride + j];
int16_t th = dis->band_h[i * stride + j];
int16_t tv = dis->band_v[i * stride + j];
int16_t td = dis->band_d[i * stride + j];
int16_t rst_h, rst_v, rst_d;
/* Determine if angle between (oh,ov) and (th,tv) is less than 1 degree.
* Given that u is the angle (oh,ov) and v is the angle (th,tv), this can
* be done by testing the inequvality.
*
* { (u.v.) >= 0 } AND { (u.v)^2 >= cos(1deg)^2 * ||u||^2 * ||v||^2 }
*
* Proof:
*
* cos(theta) = (u.v) / (||u|| * ||v||)
*
* IF u.v >= 0 THEN
* cos(theta)^2 = (u.v)^2 / (||u||^2 * ||v||^2)
* (u.v)^2 = cos(theta)^2 * ||u||^2 * ||v||^2
*
* IF |theta| < 1deg THEN
* (u.v)^2 >= cos(1deg)^2 * ||u||^2 * ||v||^2
* END
* ELSE
* |theta| > 90deg
* END
*/
ot_dp = (int64_t)oh * th + (int64_t)ov * tv;
o_mag_sq = (int64_t)oh * oh + (int64_t)ov * ov;
t_mag_sq = (int64_t)th * th + (int64_t)tv * tv;
/**
* angle_flag is calculated in floating-point by converting fixed-point variables back to
* floating-point
*/
int angle_flag = (((float)ot_dp / 4096.0) >= 0.0f) &&
(((float)ot_dp / 4096.0) * ((float)ot_dp / 4096.0) >=
cos_1deg_sq * ((float)o_mag_sq / 4096.0) * ((float)t_mag_sq / 4096.0));
/**
* Division th/oh is carried using lookup table and converted to multiplication
*/
int32_t tmp_kh = (oh == 0) ?
32768 : (((int64_t)div_lookup[oh + 32768] * th) + 16384) >> 15;
int32_t tmp_kv = (ov == 0) ?
32768 : (((int64_t)div_lookup[ov + 32768] * tv) + 16384) >> 15;
int32_t tmp_kd = (od == 0) ?
32768 : (((int64_t)div_lookup[od + 32768] * td) + 16384) >> 15;
int32_t kh = tmp_kh < 0 ? 0 : (tmp_kh > 32768 ? 32768 : tmp_kh);
int32_t kv = tmp_kv < 0 ? 0 : (tmp_kv > 32768 ? 32768 : tmp_kv);
int32_t kd = tmp_kd < 0 ? 0 : (tmp_kd > 32768 ? 32768 : tmp_kd);
/**
* kh,kv,kd are in Q15 type and oh,ov,od are in Q16 type hence shifted by
* 15 to make result Q16
*/
rst_h = ((kh * oh) + 16384) >> 15;
rst_v = ((kv * ov) + 16384) >> 15;
rst_d = ((kd * od) + 16384) >> 15;
const float rst_h_f = ((float)kh / 32768) * ((float)oh / 64);
const float rst_v_f = ((float)kv / 32768) * ((float)ov / 64);
const float rst_d_f = ((float)kd / 32768) * ((float)od / 64);
if (angle_flag && (rst_h_f > 0.)) rst_h = MIN((rst_h * adm_enhn_gain_limit), th);
if (angle_flag && (rst_h_f < 0.)) rst_h = MAX((rst_h * adm_enhn_gain_limit), th);
if (angle_flag && (rst_v_f > 0.)) rst_v = MIN(rst_v * adm_enhn_gain_limit, tv);
if (angle_flag && (rst_v_f < 0.)) rst_v = MAX(rst_v * adm_enhn_gain_limit, tv);
if (angle_flag && (rst_d_f > 0.)) rst_d = MIN(rst_d * adm_enhn_gain_limit, td);
if (angle_flag && (rst_d_f < 0.)) rst_d = MAX(rst_d * adm_enhn_gain_limit, td);
r->band_h[i * stride + j] = rst_h;
r->band_v[i * stride + j] = rst_v;
r->band_d[i * stride + j] = rst_d;
a->band_h[i * stride + j] = th - rst_h;
a->band_v[i * stride + j] = tv - rst_v;
a->band_d[i * stride + j] = td - rst_d;
}
}
}
static inline uint16_t get_best15_from32(uint32_t temp, int *x)
{
int k = __builtin_clz(temp); //built in for intel
k = 17 - k;
temp = (temp + (1 << (k - 1))) >> k;
*x = k;
return temp;
}
static void adm_decouple_s123(AdmBuffer *buf, int w, int h, int stride,
double adm_enhn_gain_limit)
{
const float cos_1deg_sq = cos(1.0 * M_PI / 180.0) * cos(1.0 * M_PI / 180.0);
const i4_adm_dwt_band_t *ref = &buf->i4_ref_dwt2;
const i4_adm_dwt_band_t *dis = &buf->i4_dis_dwt2;
const i4_adm_dwt_band_t *r = &buf->i4_decouple_r;
const i4_adm_dwt_band_t *a = &buf->i4_decouple_a;
/* The computation of the score is not required for the regions
which lie outside the frame borders */
int left = w * ADM_BORDER_FACTOR - 0.5 - 1; // -1 for filter tap
int top = h * ADM_BORDER_FACTOR - 0.5 - 1;
int right = w - left + 2; // +2 for filter tap
int bottom = h - top + 2;
if (left < 0) {
left = 0;
}
if (right > w) {
right = w;
}
if (top < 0) {
top = 0;
}
if (bottom > h) {
bottom = h;
}
int64_t ot_dp, o_mag_sq, t_mag_sq;
for (int i = top; i < bottom; ++i)
{
for (int j = left; j < right; ++j)
{
int32_t oh = ref->band_h[i * stride + j];
int32_t ov = ref->band_v[i * stride + j];
int32_t od = ref->band_d[i * stride + j];
int32_t th = dis->band_h[i * stride + j];
int32_t tv = dis->band_v[i * stride + j];
int32_t td = dis->band_d[i * stride + j];
int32_t rst_h, rst_v, rst_d;
/* Determine if angle between (oh,ov) and (th,tv) is less than 1 degree.
* Given that u is the angle (oh,ov) and v is the angle (th,tv), this can
* be done by testing the inequvality.
*
* { (u.v.) >= 0 } AND { (u.v)^2 >= cos(1deg)^2 * ||u||^2 * ||v||^2 }
*
* Proof:
*
* cos(theta) = (u.v) / (||u|| * ||v||)
*
* IF u.v >= 0 THEN
* cos(theta)^2 = (u.v)^2 / (||u||^2 * ||v||^2)
* (u.v)^2 = cos(theta)^2 * ||u||^2 * ||v||^2
*
* IF |theta| < 1deg THEN
* (u.v)^2 >= cos(1deg)^2 * ||u||^2 * ||v||^2
* END
* ELSE
* |theta| > 90deg
* END
*/
ot_dp = (int64_t)oh * th + (int64_t)ov * tv;
o_mag_sq = (int64_t)oh * oh + (int64_t)ov * ov;
t_mag_sq = (int64_t)th * th + (int64_t)tv * tv;
int angle_flag = (((float)ot_dp / 4096.0) >= 0.0f) &&
(((float)ot_dp / 4096.0) * ((float)ot_dp / 4096.0) >=
cos_1deg_sq * ((float)o_mag_sq / 4096.0) * ((float)t_mag_sq / 4096.0));
/**
* Division th/oh is carried using lookup table and converted to multiplication
* int64 / int32 is converted to multiplication using following method
* num /den :
* DenAbs = Abs(den)
* MSBDen = MSB(DenAbs) (gives position of first 1 bit form msb side)
* If (DenAbs < (1 << 15))
* Round = (1<<14)
* Score = (num * div_lookup[den] + Round ) >> 15
* else
* RoundD = (1<< (16 - MSBDen))
* Round = (1<< (14 + (17 - MSBDen))
* Score = (num * div_lookup[(DenAbs + RoundD )>>(17 - MSBDen)]*sign(Denominator) + Round)
* >> ((15 + (17 - MSBDen))
*/
int32_t kh_shift = 0;
int32_t kv_shift = 0;
int32_t kd_shift = 0;
uint32_t abs_oh = abs(oh);
uint32_t abs_ov = abs(ov);
uint32_t abs_od = abs(od);
int8_t kh_sign = (oh < 0 ? -1 : 1);
int8_t kv_sign = (ov < 0 ? -1 : 1);
int8_t kd_sign = (od < 0 ? -1 : 1);
uint16_t kh_msb = (abs_oh < (32768) ? abs_oh : get_best15_from32(abs_oh, &kh_shift));
uint16_t kv_msb = (abs_ov < (32768) ? abs_ov : get_best15_from32(abs_ov, &kv_shift));
uint16_t kd_msb = (abs_od < (32768) ? abs_od : get_best15_from32(abs_od, &kd_shift));
int64_t tmp_kh = (oh == 0) ? 32768 : (((int64_t)div_lookup[kh_msb + 32768] * th)*(kh_sign) +
(1 << (14 + kh_shift))) >> (15 + kh_shift);
int64_t tmp_kv = (ov == 0) ? 32768 : (((int64_t)div_lookup[kv_msb + 32768] * tv)*(kv_sign) +
(1 << (14 + kv_shift))) >> (15 + kv_shift);
int64_t tmp_kd = (od == 0) ? 32768 : (((int64_t)div_lookup[kd_msb + 32768] * td)*(kd_sign) +
(1 << (14 + kd_shift))) >> (15 + kd_shift);
int64_t kh = tmp_kh < 0 ? 0 : (tmp_kh > 32768 ? 32768 : tmp_kh);
int64_t kv = tmp_kv < 0 ? 0 : (tmp_kv > 32768 ? 32768 : tmp_kv);
int64_t kd = tmp_kd < 0 ? 0 : (tmp_kd > 32768 ? 32768 : tmp_kd);
rst_h = ((kh * oh) + 16384) >> 15;
rst_v = ((kv * ov) + 16384) >> 15;
rst_d = ((kd * od) + 16384) >> 15;
const float rst_h_f = ((float)kh / 32768) * ((float)oh / 64);
const float rst_v_f = ((float)kv / 32768) * ((float)ov / 64);
const float rst_d_f = ((float)kd / 32768) * ((float)od / 64);
if (angle_flag && (rst_h_f > 0.)) rst_h = MIN((rst_h * adm_enhn_gain_limit), th);
if (angle_flag && (rst_h_f < 0.)) rst_h = MAX((rst_h * adm_enhn_gain_limit), th);
if (angle_flag && (rst_v_f > 0.)) rst_v = MIN(rst_v * adm_enhn_gain_limit, tv);
if (angle_flag && (rst_v_f < 0.)) rst_v = MAX(rst_v * adm_enhn_gain_limit, tv);
if (angle_flag && (rst_d_f > 0.)) rst_d = MIN(rst_d * adm_enhn_gain_limit, td);
if (angle_flag && (rst_d_f < 0.)) rst_d = MAX(rst_d * adm_enhn_gain_limit, td);
r->band_h[i * stride + j] = rst_h;
r->band_v[i * stride + j] = rst_v;
r->band_d[i * stride + j] = rst_d;
a->band_h[i * stride + j] = th - rst_h;
a->band_v[i * stride + j] = tv - rst_v;
a->band_d[i * stride + j] = td - rst_d;
}
}
}
static void adm_csf(AdmBuffer *buf, int w, int h, int stride,
double adm_norm_view_dist, int adm_ref_display_height)
{
const adm_dwt_band_t *src = &buf->decouple_a;
const adm_dwt_band_t *dst = &buf->csf_a;
const adm_dwt_band_t *flt = &buf->csf_f;
const int16_t *src_angles[3] = { src->band_h, src->band_v, src->band_d };
int16_t *dst_angles[3] = { dst->band_h, dst->band_v, dst->band_d };
int16_t *flt_angles[3] = { flt->band_h, flt->band_v, flt->band_d };
// for ADM: scales goes from 0 to 3 but in noise floor paper, it goes from
// 1 to 4 (from finest scale to coarsest scale).
// 0 is scale zero passed to dwt_quant_step
const float factor1 = dwt_quant_step(&dwt_7_9_YCbCr_threshold[0], 0, 1, adm_norm_view_dist, adm_ref_display_height);
const float factor2 = dwt_quant_step(&dwt_7_9_YCbCr_threshold[0], 0, 2, adm_norm_view_dist, adm_ref_display_height);
const float rfactor1[3] = { 1.0f / factor1, 1.0f / factor1, 1.0f / factor2 };
/**
* rfactor is converted to fixed-point for scale0 and stored in i_rfactor
* multiplied by 2^21 for rfactor[0,1] and by 2^23 for rfactor[2].
* For adm_norm_view_dist 3.0 and adm_ref_display_height 1080,
* i_rfactor is around { 36453,36453,49417 }
*/
uint16_t i_rfactor[3];
if (fabs(adm_norm_view_dist * adm_ref_display_height - DEFAULT_ADM_NORM_VIEW_DIST * DEFAULT_ADM_REF_DISPLAY_HEIGHT) < 1.0e-8) {
i_rfactor[0] = 36453;
i_rfactor[1] = 36453;
i_rfactor[2] = 49417;
}
else {
const double pow2_21 = pow(2, 21);
const double pow2_23 = pow(2, 23);
i_rfactor[0] = (uint16_t) (rfactor1[0] * pow2_21);
i_rfactor[1] = (uint16_t) (rfactor1[1] * pow2_21);
i_rfactor[2] = (uint16_t) (rfactor1[2] * pow2_23);
}
/**
* Shifts pending from previous stage is 6
* hence variables multiplied by i_rfactor[0,1] has to be shifted by 21+6=27 to convert
* into floating-point. But shifted by 15 to make it Q16
* and variables multiplied by i_factor[2] has to be shifted by 23+6=29 to convert into
* floating-point. But shifted by 17 to make it Q16
* Hence remaining shifts after shifting by i_shifts is 12 to make it equivalent to
* floating-point
*/
uint8_t i_shifts[3] = { 15,15,17 };
uint16_t i_shiftsadd[3] = { 16384, 16384, 65535 };
uint16_t FIX_ONE_BY_30 = 4369; //(1/30)*2^17
/* The computation of the csf values is not required for the regions which
*lie outside the frame borders
*/
int left = w * ADM_BORDER_FACTOR - 0.5 - 1; // -1 for filter tap
int top = h * ADM_BORDER_FACTOR - 0.5 - 1;
int right = w - left + 2; // +2 for filter tap
int bottom = h - top + 2;
if (left < 0) {
left = 0;
}
if (right > w) {
right = w;
}
if (top < 0) {
top = 0;
}
if (bottom > h) {