forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvf_ssim360.c
1760 lines (1485 loc) · 59.4 KB
/
vf_ssim360.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 (c) 2015-2021, Facebook, Inc.
* All rights reserved.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Computes the Structural Similarity Metric between two 360 video streams.
* original SSIM algorithm:
* Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli,
* "Image quality assessment: From error visibility to structural similarity,"
* IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
*
* To improve speed, this implementation uses the standard approximation of
* overlapped 8x8 block sums, rather than the original gaussian weights.
*
* To address warping from 360 projections for videos with same
* projection and resolution, the 8x8 blocks sampled are weighted by
* their location in the image.
*
* To apply SSIM across projections and video sizes, we render the video on to
* a flat "tape" from which the 8x8 are selected and compared.
*/
/*
* @file
* Caculate the SSIM between two input 360 videos.
*/
#include <math.h>
#include "libavutil/avstring.h"
#include "libavutil/file_open.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "drawutils.h"
#include "internal.h"
#include "framesync.h"
#define RIGHT 0
#define LEFT 1
#define TOP 2
#define BOTTOM 3
#define FRONT 4
#define BACK 5
#define DEFAULT_HEATMAP_W 32
#define DEFAULT_HEATMAP_H 16
#define M_PI_F ((float)M_PI)
#define M_PI_2_F ((float)M_PI_2)
#define M_PI_4_F ((float)M_PI_4)
#define M_SQRT2_F ((float)M_SQRT2)
#define DEFAULT_EXPANSION_COEF 1.01f
#define BARREL_THETA_RANGE (DEFAULT_EXPANSION_COEF * 2.0f * M_PI_F)
#define BARREL_PHI_RANGE (DEFAULT_EXPANSION_COEF * M_PI_2_F)
// Use fixed-point with 16 bit precision for fast bilinear math
#define FIXED_POINT_PRECISION 16
// Use 1MB per channel for the histogram to get 5-digit precise SSIM value
#define SSIM360_HIST_SIZE 131072
// The last number is a marker < 0 to mark end of list
static const double PERCENTILE_LIST[] = {
1.0, 0.9, 0.8, 0.7, 0.6,
0.5, 0.4, 0.3, 0.2, 0.1, 0, -1
};
typedef enum StereoFormat {
STEREO_FORMAT_TB,
STEREO_FORMAT_LR,
STEREO_FORMAT_MONO,
STEREO_FORMAT_N
} StereoFormat;
typedef enum Projection {
PROJECTION_CUBEMAP32,
PROJECTION_CUBEMAP23,
PROJECTION_BARREL,
PROJECTION_BARREL_SPLIT,
PROJECTION_EQUIRECT,
PROJECTION_N
} Projection;
typedef struct Map2D {
int w, h;
double *value;
} Map2D;
typedef struct HeatmapList {
Map2D map;
struct HeatmapList *next;
} HeatmapList;
typedef struct SampleParams {
int stride;
int planewidth;
int planeheight;
int x_image_offset;
int y_image_offset;
int x_image_range;
int y_image_range;
int projection;
float expand_coef;
} SampleParams;
typedef struct BilinearMap {
// Indices to the 4 samples to compute bilinear
int tli;
int tri;
int bli;
int bri;
// Fixed point factors with which the above 4 sample vector's
// dot product needs to be computed for the final bilinear value
int tlf;
int trf;
int blf;
int brf;
} BilinearMap;
typedef struct SSIM360Context {
const AVClass *class;
FFFrameSync fs;
// Stats file configuration
FILE *stats_file;
char *stats_file_str;
// Component properties
int nb_components;
double coefs[4];
char comps[4];
int max;
// Channel configuration & properties
int compute_chroma;
int is_rgb;
uint8_t rgba_map[4];
// Standard SSIM computation configuration & workspace
uint64_t frame_skip_ratio;
int *temp;
uint64_t nb_ssim_frames;
uint64_t nb_net_frames;
double ssim360[4], ssim360_total;
double *ssim360_hist[4];
double ssim360_hist_net[4];
double ssim360_percentile_sum[4][256];
// 360 projection configuration & workspace
int ref_projection;
int main_projection;
int ref_stereo_format;
int main_stereo_format;
float ref_pad;
float main_pad;
int use_tape;
char *heatmap_str;
int default_heatmap_w;
int default_heatmap_h;
Map2D density;
HeatmapList *heatmaps;
int ref_planewidth[4];
int ref_planeheight[4];
int main_planewidth[4];
int main_planeheight[4];
int tape_length[4];
BilinearMap *ref_tape_map[4][2];
BilinearMap *main_tape_map[4][2];
float angular_resolution[4][2];
double (*ssim360_plane)(
uint8_t *main, int main_stride,
uint8_t *ref, int ref_stride,
int width, int height, void *temp,
int max, Map2D density);
} SSIM360Context;
#define OFFSET(x) offsetof(SSIM360Context, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
static const AVOption ssim360_options[] = {
{ "stats_file", "Set file where to store per-frame difference information",
OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
{ "f", "Set file where to store per-frame difference information",
OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
{ "compute_chroma",
"Specifies if non-luma channels must be computed",
OFFSET(compute_chroma), AV_OPT_TYPE_INT, {.i64 = 1},
0, 1, .flags = FLAGS },
{ "frame_skip_ratio",
"Specifies the number of frames to be skipped from evaluation, for every evaluated frame",
OFFSET(frame_skip_ratio), AV_OPT_TYPE_INT, {.i64 = 0},
0, 1000000, .flags = FLAGS },
{ "ref_projection", "projection of the reference video",
OFFSET(ref_projection), AV_OPT_TYPE_INT, {.i64 = PROJECTION_EQUIRECT},
0, PROJECTION_N - 1, .flags = FLAGS, .unit = "projection" },
{ "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64 = PROJECTION_EQUIRECT}, 0, 0, FLAGS, .unit = "projection" },
{ "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64 = PROJECTION_EQUIRECT}, 0, 0, FLAGS, .unit = "projection" },
{ "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64 = PROJECTION_CUBEMAP32}, 0, 0, FLAGS, .unit = "projection" },
{ "c2x3", "cubemap 2x3", 0, AV_OPT_TYPE_CONST, {.i64 = PROJECTION_CUBEMAP23}, 0, 0, FLAGS, .unit = "projection" },
{ "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64 = PROJECTION_BARREL}, 0, 0, FLAGS, .unit = "projection" },
{ "barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64 = PROJECTION_BARREL_SPLIT}, 0, 0, FLAGS, .unit = "projection" },
{ "main_projection", "projection of the main video",
OFFSET(main_projection), AV_OPT_TYPE_INT, {.i64 = PROJECTION_N},
0, PROJECTION_N, .flags = FLAGS, .unit = "projection" },
{ "ref_stereo", "stereo format of the reference video",
OFFSET(ref_stereo_format), AV_OPT_TYPE_INT, {.i64 = STEREO_FORMAT_MONO},
0, STEREO_FORMAT_N - 1, .flags = FLAGS, .unit = "stereo_format" },
{ "mono", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = STEREO_FORMAT_MONO }, 0, 0, FLAGS, .unit = "stereo_format" },
{ "tb", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = STEREO_FORMAT_TB }, 0, 0, FLAGS, .unit = "stereo_format" },
{ "lr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = STEREO_FORMAT_LR }, 0, 0, FLAGS, .unit = "stereo_format" },
{ "main_stereo", "stereo format of main video",
OFFSET(main_stereo_format), AV_OPT_TYPE_INT, {.i64 = STEREO_FORMAT_N},
0, STEREO_FORMAT_N, .flags = FLAGS, .unit = "stereo_format" },
{ "ref_pad",
"Expansion (padding) coefficient for each cube face of the reference video",
OFFSET(ref_pad), AV_OPT_TYPE_FLOAT, {.dbl = .0f}, 0, 10, .flags = FLAGS },
{ "main_pad",
"Expansion (padding) coeffiecient for each cube face of the main video",
OFFSET(main_pad), AV_OPT_TYPE_FLOAT, {.dbl = .0f}, 0, 10, .flags = FLAGS },
{ "use_tape",
"Specifies if the tape based SSIM 360 algorithm must be used independent of the input video types",
OFFSET(use_tape), AV_OPT_TYPE_INT, {.i64 = 0},
0, 1, .flags = FLAGS },
{ "heatmap_str",
"Heatmap data for view-based evaluation. For heatmap file format, please refer to EntSphericalVideoHeatmapData.",
OFFSET(heatmap_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, .flags = FLAGS },
{ "default_heatmap_width",
"Default heatmap dimension. Will be used when dimension is not specified in heatmap data.",
OFFSET(default_heatmap_w), AV_OPT_TYPE_INT, {.i64 = 32}, 1, 4096, .flags = FLAGS },
{ "default_heatmap_height",
"Default heatmap dimension. Will be used when dimension is not specified in heatmap data.",
OFFSET(default_heatmap_h), AV_OPT_TYPE_INT, {.i64 = 16}, 1, 4096, .flags = FLAGS },
{ NULL }
};
FRAMESYNC_DEFINE_CLASS(ssim360, SSIM360Context, fs);
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
{
char value[128];
snprintf(value, sizeof(value), "%0.2f", d);
if (comp) {
char key2[128];
snprintf(key2, sizeof(key2), "%s%c", key, comp);
av_dict_set(metadata, key2, value, 0);
} else {
av_dict_set(metadata, key, value, 0);
}
}
static void map_uninit(Map2D *map)
{
av_freep(&map->value);
}
static int map_init(Map2D *map, int w, int h)
{
map->value = av_calloc(h * w, sizeof(*map->value));
if (!map->value)
return AVERROR(ENOMEM);
map->h = h;
map->w = w;
return 0;
}
static void map_list_free(HeatmapList **pl)
{
HeatmapList *l = *pl;
while (l) {
HeatmapList *next = l->next;
map_uninit(&l->map);
av_freep(&l);
l = next;
}
*pl = NULL;
}
static int map_alloc(HeatmapList **pl, int w, int h)
{
HeatmapList *l;
int ret;
l = av_mallocz(sizeof(*l));
if (!l)
return AVERROR(ENOMEM);
ret = map_init(&l->map, w, h);
if (ret < 0) {
av_freep(&l);
return ret;
}
*pl = l;
return 0;
}
static void
ssim360_4x4xn_16bit(const uint8_t *main8, ptrdiff_t main_stride,
const uint8_t *ref8, ptrdiff_t ref_stride,
int64_t (*sums)[4], int width)
{
const uint16_t *main16 = (const uint16_t *)main8;
const uint16_t *ref16 = (const uint16_t *)ref8;
main_stride >>= 1;
ref_stride >>= 1;
for (int z = 0; z < width; z++) {
uint64_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
unsigned a = main16[x + y * main_stride];
unsigned b = ref16[x + y * ref_stride];
s1 += a;
s2 += b;
ss += a*a;
ss += b*b;
s12 += a*b;
}
}
sums[z][0] = s1;
sums[z][1] = s2;
sums[z][2] = ss;
sums[z][3] = s12;
main16 += 4;
ref16 += 4;
}
}
static void
ssim360_4x4xn_8bit(const uint8_t *main, ptrdiff_t main_stride,
const uint8_t *ref, ptrdiff_t ref_stride,
int (*sums)[4], int width)
{
for (int z = 0; z < width; z++) {
uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
int a = main[x + y * main_stride];
int b = ref[x + y * ref_stride];
s1 += a;
s2 += b;
ss += a*a;
ss += b*b;
s12 += a*b;
}
}
sums[z][0] = s1;
sums[z][1] = s2;
sums[z][2] = ss;
sums[z][3] = s12;
main += 4;
ref += 4;
}
}
static float ssim360_end1x(int64_t s1, int64_t s2, int64_t ss, int64_t s12, int max)
{
int64_t ssim_c1 = (int64_t)(.01 * .01 * max * max * 64 + .5);
int64_t ssim_c2 = (int64_t)(.03 * .03 * max * max * 64 * 63 + .5);
int64_t fs1 = s1;
int64_t fs2 = s2;
int64_t fss = ss;
int64_t fs12 = s12;
int64_t vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
int64_t covar = fs12 * 64 - fs1 * fs2;
return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
/ ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
}
static float ssim360_end1(int s1, int s2, int ss, int s12)
{
static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
int fs1 = s1;
int fs2 = s2;
int fss = ss;
int fs12 = s12;
int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
int covar = fs12 * 64 - fs1 * fs2;
return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
/ ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
}
static double
ssim360_endn_16bit(const int64_t (*sum0)[4], const int64_t (*sum1)[4],
int width, int max,
double *density_map, int map_width, double *total_weight)
{
double ssim360 = 0.0, weight;
for (int i = 0; i < width; i++) {
weight = density_map ? density_map[(int) ((0.5 + i) / width * map_width)] : 1.0;
ssim360 += weight * ssim360_end1x(
sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3],
max);
*total_weight += weight;
}
return ssim360;
}
static double
ssim360_endn_8bit(const int (*sum0)[4], const int (*sum1)[4], int width,
double *density_map, int map_width, double *total_weight)
{
double ssim360 = 0.0, weight;
for (int i = 0; i < width; i++) {
weight = density_map ? density_map[(int) ((0.5 + i) / width * map_width)] : 1.0;
ssim360 += weight * ssim360_end1(
sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3]);
*total_weight += weight;
}
return ssim360;
}
static double
ssim360_plane_16bit(uint8_t *main, int main_stride,
uint8_t *ref, int ref_stride,
int width, int height, void *temp,
int max, Map2D density)
{
int z = 0;
double ssim360 = 0.0;
int64_t (*sum0)[4] = temp;
int64_t (*sum1)[4] = sum0 + (width >> 2) + 3;
double total_weight = 0.0;
width >>= 2;
height >>= 2;
for (int y = 1; y < height; y++) {
for (; z <= y; z++) {
FFSWAP(void*, sum0, sum1);
ssim360_4x4xn_16bit(&main[4 * z * main_stride], main_stride,
&ref[4 * z * ref_stride], ref_stride,
sum0, width);
}
ssim360 += ssim360_endn_16bit(
(const int64_t (*)[4])sum0, (const int64_t (*)[4])sum1,
width - 1, max,
density.value ? density.value + density.w * ((int) ((z - 1.0) / height * density.h)) : NULL,
density.w, &total_weight);
}
return (double) (ssim360 / total_weight);
}
static double
ssim360_plane_8bit(uint8_t *main, int main_stride,
uint8_t *ref, int ref_stride,
int width, int height, void *temp,
int max, Map2D density)
{
int z = 0;
double ssim360 = 0.0;
int (*sum0)[4] = temp;
int (*sum1)[4] = sum0 + (width >> 2) + 3;
double total_weight = 0.0;
width >>= 2;
height >>= 2;
for (int y = 1; y < height; y++) {
for (; z <= y; z++) {
FFSWAP(void*, sum0, sum1);
ssim360_4x4xn_8bit(
&main[4 * z * main_stride], main_stride,
&ref[4 * z * ref_stride], ref_stride,
sum0, width);
}
ssim360 += ssim360_endn_8bit(
(const int (*)[4])sum0, (const int (*)[4])sum1, width - 1,
density.value ? density.value + density.w * ((int) ((z - 1.0) / height * density.h)) : NULL,
density.w, &total_weight);
}
return (double) (ssim360 / total_weight);
}
static double ssim360_db(double ssim360, double weight)
{
return 10 * log10(weight / (weight - ssim360));
}
static int get_bilinear_sample(const uint8_t *data, BilinearMap *m, int max_value)
{
static const int fixed_point_half = 1 << (FIXED_POINT_PRECISION - 1);
static const int inv_byte_mask = UINT_MAX << 8;
int tl, tr, bl, br, v;
if (max_value & inv_byte_mask) {
uint16_t *data16 = (uint16_t *)data;
tl = data16[m->tli];
tr = data16[m->tri];
bl = data16[m->bli];
br = data16[m->bri];
} else {
tl = data[m->tli];
tr = data[m->tri];
bl = data[m->bli];
br = data[m->bri];
}
v = m->tlf * tl +
m->trf * tr +
m->blf * bl +
m->brf * br;
// Round by half, and revert the fixed-point offset
return ((v + fixed_point_half) >> FIXED_POINT_PRECISION) & max_value;
}
static void
ssim360_4x4x2_tape(const uint8_t *main, BilinearMap *main_maps,
const uint8_t *ref, BilinearMap *ref_maps,
int offset_y, int max_value, int (*sums)[4])
{
int offset_x = 0;
// Two blocks along the width
for (int z = 0; z < 2; z++) {
int s1 = 0, s2 = 0, ss = 0, s12 = 0;
// 4 pixel block from (offset_x, offset_y)
for (int y = offset_y; y < offset_y + 4; y++) {
int y_stride = y << 3;
for (int x = offset_x; x < offset_x + 4; x++) {
int map_index = x + y_stride;
int a = get_bilinear_sample(main, main_maps + map_index, max_value);
int b = get_bilinear_sample(ref, ref_maps + map_index, max_value);
s1 += a;
s2 += b;
ss += a*a;
ss += b*b;
s12 += a*b;
}
}
sums[z][0] = s1;
sums[z][1] = s2;
sums[z][2] = ss;
sums[z][3] = s12;
offset_x += 4;
}
}
static float get_radius_between_negative_and_positive_pi(float theta)
{
int floor_theta_by_2pi, floor_theta_by_pi;
// Convert theta to range [0, 2*pi]
floor_theta_by_2pi = (int)(theta / (2.0f * M_PI_F)) - (theta < 0.0f);
theta -= 2.0f * M_PI_F * floor_theta_by_2pi;
// Convert theta to range [-pi, pi]
floor_theta_by_pi = theta / M_PI_F;
theta -= 2.0f * M_PI_F * floor_theta_by_pi;
return FFMIN(M_PI_F, FFMAX(-M_PI_F, theta));
}
static float get_heat(HeatmapList *heatmaps, float angular_resoluation, float norm_tape_pos)
{
float pitch, yaw, norm_pitch, norm_yaw;
int w, h;
if (!heatmaps)
return 1.0f;
pitch = asinf(norm_tape_pos*2);
yaw = M_PI_2_F * pitch / angular_resoluation;
yaw = get_radius_between_negative_and_positive_pi(yaw);
// normalize into [0,1]
norm_pitch = 1.0f - (pitch / M_PI_F + 0.5f);
norm_yaw = yaw / 2.0f / M_PI_F + 0.5f;
// get heat on map
w = FFMIN(heatmaps->map.w - 1, FFMAX(0, heatmaps->map.w * norm_yaw));
h = FFMIN(heatmaps->map.h - 1, FFMAX(0, heatmaps->map.h * norm_pitch));
return heatmaps->map.value[h * heatmaps->map.w + w];
}
static double
ssim360_tape(uint8_t *main, BilinearMap *main_maps,
uint8_t *ref, BilinearMap *ref_maps,
int tape_length, int max_value, void *temp,
double *ssim360_hist, double *ssim360_hist_net,
float angular_resolution, HeatmapList *heatmaps)
{
int horizontal_block_count = 2;
int vertical_block_count = tape_length >> 2;
int z = 0, y;
// Since the tape will be very long and we need to average over all 8x8 blocks, use double
double ssim360 = 0.0;
double sum_weight = 0.0;
int (*sum0)[4] = temp;
int (*sum1)[4] = sum0 + horizontal_block_count + 3;
for (y = 1; y < vertical_block_count; y++) {
int fs1, fs2, fss, fs12, hist_index;
float norm_tape_pos, weight;
double sample_ssim360;
for (; z <= y; z++) {
FFSWAP(void*, sum0, sum1);
ssim360_4x4x2_tape(main, main_maps, ref, ref_maps, z*4, max_value, sum0);
}
// Given we have only one 8x8 block, following sums fit within 26 bits even for 10bit videos
fs1 = sum0[0][0] + sum0[1][0] + sum1[0][0] + sum1[1][0];
fs2 = sum0[0][1] + sum0[1][1] + sum1[0][1] + sum1[1][1];
fss = sum0[0][2] + sum0[1][2] + sum1[0][2] + sum1[1][2];
fs12 = sum0[0][3] + sum0[1][3] + sum1[0][3] + sum1[1][3];
if (max_value > 255) {
// Since we need high precision to multiply fss / fs12 by 64, use double
double ssim_c1_d = .01*.01*64*max_value*max_value;
double ssim_c2_d = .03*.03*64*63*max_value*max_value;
double vars = 64. * fss - 1. * fs1 * fs1 - 1. * fs2 * fs2;
double covar = 64. * fs12 - 1.*fs1 * fs2;
sample_ssim360 = (2. * fs1 * fs2 + ssim_c1_d) * (2. * covar + ssim_c2_d)
/ ((1. * fs1 * fs1 + 1. * fs2 * fs2 + ssim_c1_d) * (1. * vars + ssim_c2_d));
} else {
static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
int covar = fs12 * 64 - fs1 * fs2;
sample_ssim360 = (double)(2 * fs1 * fs2 + ssim_c1) * (double)(2 * covar + ssim_c2)
/ ((double)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (double)(vars + ssim_c2));
}
hist_index = (int)(sample_ssim360 * ((double)SSIM360_HIST_SIZE - .5));
hist_index = av_clip(hist_index, 0, SSIM360_HIST_SIZE - 1);
norm_tape_pos = (y - 0.5f) / (vertical_block_count - 1.0f) - 0.5f;
// weight from an input heatmap if available, otherwise weight = 1.0
weight = get_heat(heatmaps, angular_resolution, norm_tape_pos);
ssim360_hist[hist_index] += weight;
*ssim360_hist_net += weight;
ssim360 += (sample_ssim360 * weight);
sum_weight += weight;
}
return ssim360 / sum_weight;
}
static void compute_bilinear_map(SampleParams *p, BilinearMap *m, float x, float y)
{
float fixed_point_scale = (float)(1 << FIXED_POINT_PRECISION);
// All operations in here will fit in the 22 bit mantissa of floating point,
// since the fixed point precision is well under 22 bits
float x_image = av_clipf(x * p->x_image_range, 0, p->x_image_range) + p->x_image_offset;
float y_image = av_clipf(y * p->y_image_range, 0, p->y_image_range) + p->y_image_offset;
int x_floor = x_image;
int y_floor = y_image;
float x_diff = x_image - x_floor;
float y_diff = y_image - y_floor;
int x_ceil = x_floor + (x_diff > 1e-6);
int y_ceil = y_floor + (y_diff > 1e-6);
float x_inv_diff = 1.0f - x_diff;
float y_inv_diff = 1.0f - y_diff;
// Indices of the 4 samples from source frame
m->tli = x_floor + y_floor * p->stride;
m->tri = x_ceil + y_floor * p->stride;
m->bli = x_floor + y_ceil * p->stride;
m->bri = x_ceil + y_ceil * p->stride;
// Scale to be applied to each of the 4 samples from source frame
m->tlf = x_inv_diff * y_inv_diff * fixed_point_scale;
m->trf = x_diff * y_inv_diff * fixed_point_scale;
m->blf = x_inv_diff * y_diff * fixed_point_scale;
m->brf = x_diff * y_diff * fixed_point_scale;
}
static void get_equirect_map(float phi, float theta, float *x, float *y)
{
*x = 0.5f + theta / (2.0f * M_PI_F);
// y increases downwards
*y = 0.5f - phi / M_PI_F;
}
static void get_barrel_map(float phi, float theta, float *x, float *y)
{
float abs_phi = FFABS(phi);
if (abs_phi <= M_PI_4_F) {
// Equirect region
*x = 0.8f * (0.5f + theta / BARREL_THETA_RANGE);
// y increases downwards
*y = 0.5f - phi / BARREL_PHI_RANGE;
} else {
// Radial ratio on a unit circle = cot(abs_phi) / (expansion_cefficient).
// Using cos(abs_phi)/sin(abs_phi) explicitly to avoid division by zero
float radial_ratio = cosf(abs_phi) / (sinf(abs_phi) * DEFAULT_EXPANSION_COEF);
float circle_x = radial_ratio * sinf(theta);
float circle_y = radial_ratio * cosf(theta);
float offset_y = 0.25f;
if (phi < 0) {
// Bottom circle: theta increases clockwise, and front is upward
circle_y *= -1.0f;
offset_y += 0.5f;
}
*x = 0.8f + 0.1f * (1.0f + circle_x);
*y = offset_y + 0.25f * circle_y;
}
}
static void get_barrel_split_map(float phi, float theta, float expand_coef, float *x, float *y)
{
float abs_phi = FFABS(phi);
// Front Face [-PI/2, PI/2] -> [0,1].
// Back Face [PI/2, PI] and [-PI, -PI/2] -> [1, 2]
float radian_pi_theta = theta / M_PI_F + 0.5f;
int vFace;
if (radian_pi_theta < 0.0f)
radian_pi_theta += 2.0f;
// Front face at top (= 0), back face at bottom (= 1).
vFace = radian_pi_theta >= 1.0f;
if (abs_phi <= M_PI_4_F) {
// Equirect region
*x = 2.0f / 3.0f * (0.5f + (radian_pi_theta - vFace - 0.5f) / expand_coef);
// y increases downwards
*y = 0.25f + 0.5f * vFace - phi / (M_PI_F * expand_coef);
} else {
// Radial ratio on a unit circle = cot(abs_phi) / (expansion_cefficient).
// Using cos(abs_phi)/sin(abs_phi) explicitly to avoid division by zero
float radial_ratio = cosf(abs_phi) / (sinf(abs_phi) * expand_coef);
float circle_x = radial_ratio * sinf(theta);
float circle_y = radial_ratio * cosf(theta);
float offset_y = 0.25f;
if (vFace == 1) {
// Back Face: Flip
circle_x *= -1.0f;
circle_y = (circle_y >= 0.0f) ? (1 - circle_y) : (-1 - circle_y);
offset_y += 0.5f;
// Bottom circle: theta increases clockwise
if (phi < 0)
circle_y *= -1.0f;
} else {
// Front Face
// Bottom circle: theta increases clockwise
if (phi < 0)
circle_y *= -1.0f;
}
*x = 2.0f / 3.0f + 0.5f / 3.0f * (1.0f + circle_x);
*y = offset_y + 0.25f * circle_y / expand_coef; // y direction of expand_coeff (margin)
}
}
// Returns cube face, and provided face_x & face_y will range from [0, 1]
static int get_cubemap_face_map(float axis_vec_x, float axis_vec_y, float axis_vec_z, float *face_x, float *face_y)
{
// To check if phi, theta hits the top / bottom faces, we check the hit point of
// the axis vector on planes y = 1 and y = -1, and see if x & z are within [-1, 1]
// 0.577 < 1 / sqrt(3), which is less than the smallest sin(phi) falling on top/bottom faces
// This angle check will save computation from unnecessarily checking the top/bottom faces
if (FFABS(axis_vec_y) > 0.577f) {
float x_hit = axis_vec_x / FFABS(axis_vec_y);
float z_hit = axis_vec_z / axis_vec_y;
if (FFABS(x_hit) <= 1.f && FFABS(z_hit) <= 1.f) {
*face_x = x_hit;
// y increases downwards
*face_y = z_hit;
return axis_vec_y > 0 ? TOP : BOTTOM;
}
}
// Check for left / right faces
if (FFABS(axis_vec_x) > 0.577f) {
float z_hit = -axis_vec_z / axis_vec_x;
float y_hit = axis_vec_y / FFABS(axis_vec_x);
if (FFABS(z_hit) <= 1.f && FFABS(y_hit) <= 1.f) {
*face_x = z_hit;
// y increases downwards
*face_y = -y_hit;
return axis_vec_x > 0 ? RIGHT : LEFT;
}
}
// Front / back faces
*face_x = axis_vec_x / axis_vec_z;
// y increases downwards
*face_y = -axis_vec_y / FFABS(axis_vec_z);
return axis_vec_z > 0 ? FRONT : BACK;
}
static void get_cubemap32_map(float phi, float theta, float *x, float *y)
{
// face_projection_map maps each cube face to an index representing the face on the projection
// The indices 0->5 for cubemap 32 goes as:
// [0, 1, 2] as row 1, left to right
// [3, 4, 5] as row 2, left to right
static const int face_projection_map[] = {
[RIGHT] = 0, [LEFT] = 1, [TOP] = 2,
[BOTTOM] = 3, [FRONT] = 4, [BACK] = 5,
};
float axis_vec_x = cosf(phi) * sinf(theta);
float axis_vec_y = sinf(phi);
float axis_vec_z = cosf(phi) * cosf(theta);
float face_x = 0, face_y = 0;
int face_index = get_cubemap_face_map(axis_vec_x, axis_vec_y, axis_vec_z, &face_x, &face_y);
float x_offset = 1.f / 3.f * (face_projection_map[face_index] % 3);
float y_offset = .5f * (face_projection_map[face_index] / 3);
*x = x_offset + (face_x / DEFAULT_EXPANSION_COEF + 1.f) / 6.f;
*y = y_offset + (face_y / DEFAULT_EXPANSION_COEF + 1.f) / 4.f;
}
static void get_rotated_cubemap_map(float phi, float theta, float expand_coef, float *x, float *y)
{
// face_projection_map maps each cube face to an index representing the face on the projection
// The indices 0->5 for rotated cubemap goes as:
// [0, 1] as row 1, left to right
// [2, 3] as row 2, left to right
// [4, 5] as row 3, left to right
static const int face_projection_map[] = {
[LEFT] = 0, [TOP] = 1,
[FRONT] = 2, [BACK] = 3,
[RIGHT] = 4, [BOTTOM] = 5,
};
float axis_yaw_vec_x, axis_yaw_vec_y, axis_yaw_vec_z;
float axis_pitch_vec_z, axis_pitch_vec_y;
float x_offset, y_offset;
float face_x = 0, face_y = 0;
int face_index;
// Unrotate the cube and fix the face map:
// First undo the 45 degree yaw
theta += M_PI_4_F;
// Now we are looking at the middle of an edge. So convert to axis vector & undo the pitch
axis_yaw_vec_x = cosf(phi) * sinf(theta);
axis_yaw_vec_y = sinf(phi);
axis_yaw_vec_z = cosf(phi) * cosf(theta);
// The pitch axis is along +x, and has value of -45 degree. So, only y and z components change
axis_pitch_vec_z = (axis_yaw_vec_z - axis_yaw_vec_y) / M_SQRT2_F;
axis_pitch_vec_y = (axis_yaw_vec_y + axis_yaw_vec_z) / M_SQRT2_F;
face_index = get_cubemap_face_map(axis_yaw_vec_x, axis_pitch_vec_y, axis_pitch_vec_z, &face_x, &face_y);
// Correct for the orientation of the axes on the faces
if (face_index == LEFT || face_index == FRONT || face_index == RIGHT) {
// x increases downwards & y increases towards left
float upright_y = face_y;
face_y = face_x;
face_x = -upright_y;
} else if (face_index == TOP || face_index == BOTTOM) {
// turn the face upside-down for top and bottom
face_x *= -1.f;
face_y *= -1.f;
}
x_offset = .5f * (face_projection_map[face_index] & 1);
y_offset = 1.f / 3.f * (face_projection_map[face_index] >> 1);
*x = x_offset + (face_x / expand_coef + 1.f) / 4.f;
*y = y_offset + (face_y / expand_coef + 1.f) / 6.f;
}
static void get_projected_map(float phi, float theta, SampleParams *p, BilinearMap *m)
{
float x = 0, y = 0;
switch(p->projection) {
// TODO: Calculate for CDS
case PROJECTION_CUBEMAP23:
get_rotated_cubemap_map(phi, theta, p->expand_coef, &x, &y);
break;
case PROJECTION_CUBEMAP32:
get_cubemap32_map(phi, theta, &x, &y);
break;
case PROJECTION_BARREL:
get_barrel_map(phi, theta, &x, &y);
break;
case PROJECTION_BARREL_SPLIT:
get_barrel_split_map(phi, theta, p->expand_coef, &x, &y);
break;
// Assume PROJECTION_EQUIRECT as the default
case PROJECTION_EQUIRECT:
default:
get_equirect_map(phi, theta, &x, &y);
break;
}
compute_bilinear_map(p, m, x, y);
}
static int tape_supports_projection(int projection)
{
switch(projection) {
case PROJECTION_CUBEMAP23:
case PROJECTION_CUBEMAP32:
case PROJECTION_BARREL:
case PROJECTION_BARREL_SPLIT:
case PROJECTION_EQUIRECT:
return 1;
default:
return 0;
}
}
static float get_tape_angular_resolution(int projection, float expand_coef, int image_width, int image_height)
{
// NOTE: The angular resolution of a projected sphere is defined as
// the maximum possible horizontal angle of a pixel on the equator.
// We apply an intentional bias to the horizon as opposed to the meridian,
// since the view direction of most content is rarely closer to the poles
switch(projection) {
// TODO: Calculate for CDS
case PROJECTION_CUBEMAP23:
// Approximating atanf(pixel_width / (half_edge_width * sqrt2)) = pixel_width / (half_face_width * sqrt2)
return expand_coef / (M_SQRT2_F * image_width / 4.f);
case PROJECTION_CUBEMAP32: