-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcolorspace.c
1841 lines (1621 loc) · 62.1 KB
/
colorspace.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
/*
* This file is part of libplacebo.
*
* libplacebo 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.
*
* libplacebo 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 libplacebo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "common.h"
#include "colorspace.h"
#include "hash.h"
#include <libplacebo/colorspace.h>
#include <libplacebo/tone_mapping.h>
bool pl_color_system_is_ycbcr_like(enum pl_color_system sys)
{
switch (sys) {
case PL_COLOR_SYSTEM_UNKNOWN:
case PL_COLOR_SYSTEM_RGB:
case PL_COLOR_SYSTEM_XYZ:
return false;
case PL_COLOR_SYSTEM_BT_601:
case PL_COLOR_SYSTEM_BT_709:
case PL_COLOR_SYSTEM_SMPTE_240M:
case PL_COLOR_SYSTEM_BT_2020_NC:
case PL_COLOR_SYSTEM_BT_2020_C:
case PL_COLOR_SYSTEM_BT_2100_PQ:
case PL_COLOR_SYSTEM_BT_2100_HLG:
case PL_COLOR_SYSTEM_DOLBYVISION:
case PL_COLOR_SYSTEM_YCGCO:
return true;
case PL_COLOR_SYSTEM_COUNT: break;
};
pl_unreachable();
}
bool pl_color_system_is_linear(enum pl_color_system sys)
{
switch (sys) {
case PL_COLOR_SYSTEM_UNKNOWN:
case PL_COLOR_SYSTEM_RGB:
case PL_COLOR_SYSTEM_BT_601:
case PL_COLOR_SYSTEM_BT_709:
case PL_COLOR_SYSTEM_SMPTE_240M:
case PL_COLOR_SYSTEM_BT_2020_NC:
case PL_COLOR_SYSTEM_YCGCO:
return true;
case PL_COLOR_SYSTEM_BT_2020_C:
case PL_COLOR_SYSTEM_BT_2100_PQ:
case PL_COLOR_SYSTEM_BT_2100_HLG:
case PL_COLOR_SYSTEM_DOLBYVISION:
case PL_COLOR_SYSTEM_XYZ:
return false;
case PL_COLOR_SYSTEM_COUNT: break;
};
pl_unreachable();
}
const char *const pl_color_system_names[PL_COLOR_SYSTEM_COUNT] = {
[PL_COLOR_SYSTEM_UNKNOWN] = "Auto (unknown)",
[PL_COLOR_SYSTEM_BT_601] = "ITU-R Rec. BT.601 (SD)",
[PL_COLOR_SYSTEM_BT_709] = "ITU-R Rec. BT.709 (HD)",
[PL_COLOR_SYSTEM_SMPTE_240M] = "SMPTE-240M",
[PL_COLOR_SYSTEM_BT_2020_NC] = "ITU-R Rec. BT.2020 (non-constant luminance)",
[PL_COLOR_SYSTEM_BT_2020_C] = "ITU-R Rec. BT.2020 (constant luminance)",
[PL_COLOR_SYSTEM_BT_2100_PQ] = "ITU-R Rec. BT.2100 ICtCp PQ variant",
[PL_COLOR_SYSTEM_BT_2100_HLG] = "ITU-R Rec. BT.2100 ICtCp HLG variant",
[PL_COLOR_SYSTEM_DOLBYVISION] = "Dolby Vision (invalid for output)",
[PL_COLOR_SYSTEM_YCGCO] = "YCgCo (derived from RGB)",
[PL_COLOR_SYSTEM_RGB] = "Red, Green and Blue",
[PL_COLOR_SYSTEM_XYZ] = "Digital Cinema Distribution Master (XYZ)",
};
const char *pl_color_system_name(enum pl_color_system sys)
{
pl_assert(sys >= 0 && sys < PL_COLOR_SYSTEM_COUNT);
return pl_color_system_names[sys];
}
enum pl_color_system pl_color_system_guess_ycbcr(int width, int height)
{
if (width >= 1280 || height > 576) {
// Typical HD content
return PL_COLOR_SYSTEM_BT_709;
} else {
// Typical SD content
return PL_COLOR_SYSTEM_BT_601;
}
}
bool pl_bit_encoding_equal(const struct pl_bit_encoding *b1,
const struct pl_bit_encoding *b2)
{
return b1->sample_depth == b2->sample_depth &&
b1->color_depth == b2->color_depth &&
b1->bit_shift == b2->bit_shift;
}
const struct pl_color_repr pl_color_repr_unknown = {0};
const struct pl_color_repr pl_color_repr_rgb = {
.sys = PL_COLOR_SYSTEM_RGB,
.levels = PL_COLOR_LEVELS_FULL,
};
const struct pl_color_repr pl_color_repr_sdtv = {
.sys = PL_COLOR_SYSTEM_BT_601,
.levels = PL_COLOR_LEVELS_LIMITED,
};
const struct pl_color_repr pl_color_repr_hdtv = {
.sys = PL_COLOR_SYSTEM_BT_709,
.levels = PL_COLOR_LEVELS_LIMITED,
};
const struct pl_color_repr pl_color_repr_uhdtv = {
.sys = PL_COLOR_SYSTEM_BT_2020_NC,
.levels = PL_COLOR_LEVELS_LIMITED,
};
const struct pl_color_repr pl_color_repr_jpeg = {
.sys = PL_COLOR_SYSTEM_BT_601,
.levels = PL_COLOR_LEVELS_FULL,
};
bool pl_color_repr_equal(const struct pl_color_repr *c1,
const struct pl_color_repr *c2)
{
return c1->sys == c2->sys &&
c1->levels == c2->levels &&
c1->alpha == c2->alpha &&
c1->dovi == c2->dovi &&
pl_bit_encoding_equal(&c1->bits, &c2->bits);
}
static struct pl_bit_encoding pl_bit_encoding_merge(const struct pl_bit_encoding *orig,
const struct pl_bit_encoding *new)
{
return (struct pl_bit_encoding) {
.sample_depth = PL_DEF(orig->sample_depth, new->sample_depth),
.color_depth = PL_DEF(orig->color_depth, new->color_depth),
.bit_shift = PL_DEF(orig->bit_shift, new->bit_shift),
};
}
void pl_color_repr_merge(struct pl_color_repr *orig, const struct pl_color_repr *new)
{
*orig = (struct pl_color_repr) {
.sys = PL_DEF(orig->sys, new->sys),
.levels = PL_DEF(orig->levels, new->levels),
.alpha = PL_DEF(orig->alpha, new->alpha),
.dovi = PL_DEF(orig->dovi, new->dovi),
.bits = pl_bit_encoding_merge(&orig->bits, &new->bits),
};
}
enum pl_color_levels pl_color_levels_guess(const struct pl_color_repr *repr)
{
if (repr->sys == PL_COLOR_SYSTEM_DOLBYVISION)
return PL_COLOR_LEVELS_FULL;
if (repr->levels)
return repr->levels;
return pl_color_system_is_ycbcr_like(repr->sys)
? PL_COLOR_LEVELS_LIMITED
: PL_COLOR_LEVELS_FULL;
}
float pl_color_repr_normalize(struct pl_color_repr *repr)
{
float scale = 1.0;
struct pl_bit_encoding *bits = &repr->bits;
if (bits->bit_shift) {
scale /= (1LL << bits->bit_shift);
bits->bit_shift = 0;
}
// If one of these is set but not the other, use the set one
int tex_bits = PL_DEF(bits->sample_depth, 8);
int col_bits = PL_DEF(bits->color_depth, tex_bits);
tex_bits = PL_DEF(tex_bits, col_bits);
if (pl_color_levels_guess(repr) == PL_COLOR_LEVELS_LIMITED) {
// Limit range is always shifted directly
scale *= (float) (1LL << tex_bits) / (1LL << col_bits);
} else {
// Full range always uses the full range available
scale *= ((1LL << tex_bits) - 1.) / ((1LL << col_bits) - 1.);
}
bits->color_depth = bits->sample_depth;
return scale;
}
bool pl_color_primaries_is_wide_gamut(enum pl_color_primaries prim)
{
switch (prim) {
case PL_COLOR_PRIM_UNKNOWN:
case PL_COLOR_PRIM_BT_601_525:
case PL_COLOR_PRIM_BT_601_625:
case PL_COLOR_PRIM_BT_709:
case PL_COLOR_PRIM_BT_470M:
case PL_COLOR_PRIM_EBU_3213:
return false;
case PL_COLOR_PRIM_BT_2020:
case PL_COLOR_PRIM_APPLE:
case PL_COLOR_PRIM_ADOBE:
case PL_COLOR_PRIM_PRO_PHOTO:
case PL_COLOR_PRIM_CIE_1931:
case PL_COLOR_PRIM_DCI_P3:
case PL_COLOR_PRIM_DISPLAY_P3:
case PL_COLOR_PRIM_V_GAMUT:
case PL_COLOR_PRIM_S_GAMUT:
case PL_COLOR_PRIM_FILM_C:
case PL_COLOR_PRIM_ACES_AP0:
case PL_COLOR_PRIM_ACES_AP1:
return true;
case PL_COLOR_PRIM_COUNT: break;
}
pl_unreachable();
}
const char *const pl_color_primaries_names[PL_COLOR_PRIM_COUNT] = {
[PL_COLOR_PRIM_UNKNOWN] = "Auto (unknown)",
[PL_COLOR_PRIM_BT_601_525] = "ITU-R Rec. BT.601 (525-line = NTSC, SMPTE-C)",
[PL_COLOR_PRIM_BT_601_625] = "ITU-R Rec. BT.601 (625-line = PAL, SECAM)",
[PL_COLOR_PRIM_BT_709] = "ITU-R Rec. BT.709 (HD), also sRGB",
[PL_COLOR_PRIM_BT_470M] = "ITU-R Rec. BT.470 M",
[PL_COLOR_PRIM_EBU_3213] = "EBU Tech. 3213-E / JEDEC P22 phosphors",
[PL_COLOR_PRIM_BT_2020] = "ITU-R Rec. BT.2020 (Ultra HD)",
[PL_COLOR_PRIM_APPLE] = "Apple RGB",
[PL_COLOR_PRIM_ADOBE] = "Adobe RGB (1998)",
[PL_COLOR_PRIM_PRO_PHOTO] = "ProPhoto RGB (ROMM)",
[PL_COLOR_PRIM_CIE_1931] = "CIE 1931 RGB primaries",
[PL_COLOR_PRIM_DCI_P3] = "DCI-P3 (Digital Cinema)",
[PL_COLOR_PRIM_DISPLAY_P3] = "DCI-P3 (Digital Cinema) with D65 white point",
[PL_COLOR_PRIM_V_GAMUT] = "Panasonic V-Gamut (VARICAM)",
[PL_COLOR_PRIM_S_GAMUT] = "Sony S-Gamut",
[PL_COLOR_PRIM_FILM_C] = "Traditional film primaries with Illuminant C",
[PL_COLOR_PRIM_ACES_AP0] = "ACES Primaries #0",
[PL_COLOR_PRIM_ACES_AP1] = "ACES Primaries #1",
};
const char *pl_color_primaries_name(enum pl_color_primaries prim)
{
pl_assert(prim >= 0 && prim < PL_COLOR_PRIM_COUNT);
return pl_color_primaries_names[prim];
}
enum pl_color_primaries pl_color_primaries_guess(int width, int height)
{
// HD content
if (width >= 1280 || height > 576)
return PL_COLOR_PRIM_BT_709;
switch (height) {
case 576: // Typical PAL content, including anamorphic/squared
return PL_COLOR_PRIM_BT_601_625;
case 480: // Typical NTSC content, including squared
case 486: // NTSC Pro or anamorphic NTSC
return PL_COLOR_PRIM_BT_601_525;
default: // No good metric, just pick BT.709 to minimize damage
return PL_COLOR_PRIM_BT_709;
}
}
const char *const pl_color_transfer_names[PL_COLOR_TRC_COUNT] = {
[PL_COLOR_TRC_UNKNOWN] = "Auto (unknown SDR)",
[PL_COLOR_TRC_BT_1886] = "ITU-R Rec. BT.1886 (CRT emulation + OOTF)",
[PL_COLOR_TRC_SRGB] = "IEC 61966-2-4 sRGB (CRT emulation)",
[PL_COLOR_TRC_LINEAR] = "Linear light content",
[PL_COLOR_TRC_GAMMA18] = "Pure power gamma 1.8",
[PL_COLOR_TRC_GAMMA20] = "Pure power gamma 2.0",
[PL_COLOR_TRC_GAMMA22] = "Pure power gamma 2.2",
[PL_COLOR_TRC_GAMMA24] = "Pure power gamma 2.4",
[PL_COLOR_TRC_GAMMA26] = "Pure power gamma 2.6",
[PL_COLOR_TRC_GAMMA28] = "Pure power gamma 2.8",
[PL_COLOR_TRC_PRO_PHOTO] = "ProPhoto RGB (ROMM)",
[PL_COLOR_TRC_ST428] = "Digital Cinema Distribution Master (XYZ)",
[PL_COLOR_TRC_PQ] = "ITU-R BT.2100 PQ (perceptual quantizer), aka SMPTE ST2048",
[PL_COLOR_TRC_HLG] = "ITU-R BT.2100 HLG (hybrid log-gamma), aka ARIB STD-B67",
[PL_COLOR_TRC_V_LOG] = "Panasonic V-Log (VARICAM)",
[PL_COLOR_TRC_S_LOG1] = "Sony S-Log1",
[PL_COLOR_TRC_S_LOG2] = "Sony S-Log2",
};
const char *pl_color_transfer_name(enum pl_color_transfer trc)
{
pl_assert(trc >= 0 && trc < PL_COLOR_TRC_COUNT);
return pl_color_transfer_names[trc];
}
// HLG 75% value (scene-referred)
#define HLG_75 3.17955
float pl_color_transfer_nominal_peak(enum pl_color_transfer trc)
{
switch (trc) {
case PL_COLOR_TRC_UNKNOWN:
case PL_COLOR_TRC_BT_1886:
case PL_COLOR_TRC_SRGB:
case PL_COLOR_TRC_LINEAR:
case PL_COLOR_TRC_GAMMA18:
case PL_COLOR_TRC_GAMMA20:
case PL_COLOR_TRC_GAMMA22:
case PL_COLOR_TRC_GAMMA24:
case PL_COLOR_TRC_GAMMA26:
case PL_COLOR_TRC_GAMMA28:
case PL_COLOR_TRC_PRO_PHOTO:
case PL_COLOR_TRC_ST428:
return 1.0;
case PL_COLOR_TRC_PQ: return 10000.0 / PL_COLOR_SDR_WHITE;
case PL_COLOR_TRC_HLG: return 12.0 / HLG_75;
case PL_COLOR_TRC_V_LOG: return 46.0855;
case PL_COLOR_TRC_S_LOG1: return 6.52;
case PL_COLOR_TRC_S_LOG2: return 9.212;
case PL_COLOR_TRC_COUNT: break;
}
pl_unreachable();
}
const struct pl_hdr_metadata pl_hdr_metadata_empty = {0};
const struct pl_hdr_metadata pl_hdr_metadata_hdr10 ={
.prim = {
.red = {0.708, 0.292},
.green = {0.170, 0.797},
.blue = {0.131, 0.046},
.white = {0.31271, 0.32902},
},
.min_luma = 0,
.max_luma = 10000,
.max_cll = 10000,
.max_fall = 0, // unknown
};
float pl_hdr_rescale(enum pl_hdr_scaling from, enum pl_hdr_scaling to, float x)
{
if (from == to)
return x;
if (!x) // micro-optimization for common value
return x;
x = fmaxf(x, 0.0f);
// Convert input to PL_SCALE_RELATIVE
switch (from) {
case PL_HDR_PQ:
x = powf(x, 1.0f / PQ_M2);
x = fmaxf(x - PQ_C1, 0.0f) / (PQ_C2 - PQ_C3 * x);
x = powf(x, 1.0f / PQ_M1);
x *= 10000.0f;
// fall through
case PL_HDR_NITS:
x /= PL_COLOR_SDR_WHITE;
// fall through
case PL_HDR_NORM:
goto output;
case PL_HDR_SQRT:
x *= x;
goto output;
case PL_HDR_SCALING_COUNT:
break;
}
pl_unreachable();
output:
// Convert PL_SCALE_RELATIVE to output
switch (to) {
case PL_HDR_NORM:
return x;
case PL_HDR_SQRT:
return sqrtf(x);
case PL_HDR_NITS:
return x * PL_COLOR_SDR_WHITE;
case PL_HDR_PQ:
x *= PL_COLOR_SDR_WHITE / 10000.0f;
x = powf(x, PQ_M1);
x = (PQ_C1 + PQ_C2 * x) / (1.0f + PQ_C3 * x);
x = powf(x, PQ_M2);
return x;
case PL_HDR_SCALING_COUNT:
break;
}
pl_unreachable();
}
static inline bool pl_hdr_bezier_equal(const struct pl_hdr_bezier *a,
const struct pl_hdr_bezier *b)
{
return a->target_luma == b->target_luma &&
a->knee_x == b->knee_x &&
a->knee_y == b->knee_y &&
a->num_anchors == b->num_anchors &&
!memcmp(a->anchors, b->anchors, sizeof(a->anchors[0]) * a->num_anchors);
}
bool pl_hdr_metadata_equal(const struct pl_hdr_metadata *a,
const struct pl_hdr_metadata *b)
{
return pl_raw_primaries_equal(&a->prim, &b->prim) &&
a->min_luma == b->min_luma &&
a->max_luma == b->max_luma &&
a->max_cll == b->max_cll &&
a->max_fall == b->max_fall &&
a->scene_max[0] == b->scene_max[0] &&
a->scene_max[1] == b->scene_max[1] &&
a->scene_max[2] == b->scene_max[2] &&
a->scene_avg == b->scene_avg &&
pl_hdr_bezier_equal(&a->ootf, &b->ootf) &&
a->max_pq_y == b->max_pq_y &&
a->avg_pq_y == b->avg_pq_y;
}
void pl_hdr_metadata_merge(struct pl_hdr_metadata *orig,
const struct pl_hdr_metadata *update)
{
pl_raw_primaries_merge(&orig->prim, &update->prim);
if (!orig->min_luma)
orig->min_luma = update->min_luma;
if (!orig->max_luma)
orig->max_luma = update->max_luma;
if (!orig->max_cll)
orig->max_cll = update->max_cll;
if (!orig->max_fall)
orig->max_fall = update->max_fall;
if (!orig->scene_max[1])
memcpy(orig->scene_max, update->scene_max, sizeof(orig->scene_max));
if (!orig->scene_avg)
orig->scene_avg = update->scene_avg;
if (!orig->ootf.target_luma)
orig->ootf = update->ootf;
if (!orig->max_pq_y)
orig->max_pq_y = update->max_pq_y;
if (!orig->avg_pq_y)
orig->avg_pq_y = update->avg_pq_y;
}
bool pl_hdr_metadata_contains(const struct pl_hdr_metadata *data,
enum pl_hdr_metadata_type type)
{
bool has_hdr10 = data->max_luma;
bool has_hdr10plus = data->scene_avg && (data->scene_max[0] ||
data->scene_max[1] ||
data->scene_max[2]);
bool has_cie_y = data->max_pq_y && data->avg_pq_y;
switch (type) {
case PL_HDR_METADATA_NONE: return true;
case PL_HDR_METADATA_ANY: return has_hdr10 || has_hdr10plus || has_cie_y;
case PL_HDR_METADATA_HDR10: return has_hdr10;
case PL_HDR_METADATA_HDR10PLUS: return has_hdr10plus;
case PL_HDR_METADATA_CIE_Y: return has_cie_y;
case PL_HDR_METADATA_TYPE_COUNT: break;
}
pl_unreachable();
}
const struct pl_color_space pl_color_space_unknown = {0};
const struct pl_color_space pl_color_space_srgb = {
.primaries = PL_COLOR_PRIM_BT_709,
.transfer = PL_COLOR_TRC_SRGB,
};
const struct pl_color_space pl_color_space_bt709 = {
.primaries = PL_COLOR_PRIM_BT_709,
.transfer = PL_COLOR_TRC_BT_1886,
};
const struct pl_color_space pl_color_space_hdr10 = {
.primaries = PL_COLOR_PRIM_BT_2020,
.transfer = PL_COLOR_TRC_PQ,
};
const struct pl_color_space pl_color_space_bt2020_hlg = {
.primaries = PL_COLOR_PRIM_BT_2020,
.transfer = PL_COLOR_TRC_HLG,
};
const struct pl_color_space pl_color_space_monitor = {
.primaries = PL_COLOR_PRIM_BT_709, // sRGB primaries
.transfer = PL_COLOR_TRC_UNKNOWN, // unknown SDR response
};
bool pl_color_space_is_hdr(const struct pl_color_space *csp)
{
return csp->hdr.max_luma > PL_COLOR_SDR_WHITE ||
pl_color_transfer_is_hdr(csp->transfer);
}
bool pl_color_space_is_black_scaled(const struct pl_color_space *csp)
{
switch (csp->transfer) {
case PL_COLOR_TRC_UNKNOWN:
case PL_COLOR_TRC_SRGB:
case PL_COLOR_TRC_LINEAR:
case PL_COLOR_TRC_GAMMA18:
case PL_COLOR_TRC_GAMMA20:
case PL_COLOR_TRC_GAMMA22:
case PL_COLOR_TRC_GAMMA24:
case PL_COLOR_TRC_GAMMA26:
case PL_COLOR_TRC_GAMMA28:
case PL_COLOR_TRC_PRO_PHOTO:
case PL_COLOR_TRC_ST428:
case PL_COLOR_TRC_HLG:
return true;
case PL_COLOR_TRC_BT_1886:
case PL_COLOR_TRC_PQ:
case PL_COLOR_TRC_V_LOG:
case PL_COLOR_TRC_S_LOG1:
case PL_COLOR_TRC_S_LOG2:
return false;
case PL_COLOR_TRC_COUNT: break;
}
pl_unreachable();
}
#define MAP3(...) \
do { \
float X; \
for (int _i = 0; _i < 3; _i++) { \
X = color[_i]; \
color[_i] = __VA_ARGS__; \
} \
} while (0)
void pl_color_linearize(const struct pl_color_space *csp, float color[3])
{
if (csp->transfer == PL_COLOR_TRC_LINEAR)
return;
float csp_min, csp_max;
pl_color_space_nominal_luma_ex(pl_nominal_luma_params(
.color = csp,
.metadata = PL_HDR_METADATA_HDR10,
.scaling = PL_HDR_NORM,
.out_min = &csp_min,
.out_max = &csp_max,
));
MAP3(fmaxf(X, 0));
switch (csp->transfer) {
case PL_COLOR_TRC_SRGB:
MAP3(X > 0.04045f ? powf((X + 0.055f) / 1.055f, 2.4f)
: X / 12.92f);
goto scale_out;
case PL_COLOR_TRC_BT_1886: {
const float lb = powf(csp_min, 1/2.4f);
const float lw = powf(csp_max, 1/2.4f);
const float a = powf(lw - lb, 2.4f);
const float b = lb / (lw - lb);
MAP3(a * powf(X + b, 2.4f));
return;
}
case PL_COLOR_TRC_GAMMA18: MAP3(powf(X, 1.8f)); goto scale_out;
case PL_COLOR_TRC_GAMMA20: MAP3(powf(X, 2.0f)); goto scale_out;
case PL_COLOR_TRC_UNKNOWN:
case PL_COLOR_TRC_GAMMA22: MAP3(powf(X, 2.2f)); goto scale_out;
case PL_COLOR_TRC_GAMMA24: MAP3(powf(X, 2.4f)); goto scale_out;
case PL_COLOR_TRC_GAMMA26: MAP3(powf(X, 2.6f)); goto scale_out;
case PL_COLOR_TRC_GAMMA28: MAP3(powf(X, 2.8f)); goto scale_out;
case PL_COLOR_TRC_PRO_PHOTO:
MAP3(X > 0.03125f ? powf(X, 1.8f) : X / 16);
goto scale_out;
case PL_COLOR_TRC_ST428:
MAP3(52.37f/48 * powf(X, 2.6f));
goto scale_out;
case PL_COLOR_TRC_PQ:
MAP3(powf(X, 1 / PQ_M2));
MAP3(fmaxf(X - PQ_C1, 0) / (PQ_C2 - PQ_C3 * X));
MAP3(10000 / PL_COLOR_SDR_WHITE * powf(X, 1 / PQ_M1));
return;
case PL_COLOR_TRC_HLG: {
const float y = fmaxf(1.2f + 0.42f * log10f(csp_max / HLG_REF), 1);
const float b = sqrtf(3 * powf(csp_min / csp_max, 1 / y));
const pl_matrix3x3 rgb2xyz =
pl_get_rgb2xyz_matrix(pl_raw_primaries_get(csp->primaries));
const float *coef = rgb2xyz.m[1];
// OETF^-1
MAP3((1 - b) * X + b);
MAP3(X > 0.5f ? expf((X - HLG_C) / HLG_A) + HLG_B
: 4 * X * X);
// OOTF
float luma = coef[0] * color[0] + coef[1] * color[1] + coef[2] * color[2];
luma = powf(fmaxf(luma / 12, 0), y - 1);
MAP3(luma * X / 12);
return;
}
case PL_COLOR_TRC_V_LOG:
MAP3(X >= 0.181f ? powf(10, (X - VLOG_D) / VLOG_C) - VLOG_B
: (X - 0.125f) / 5.6f);
return;
case PL_COLOR_TRC_S_LOG1:
MAP3(powf(10, (X - SLOG_C) / SLOG_A) - SLOG_B);
return;
case PL_COLOR_TRC_S_LOG2:
MAP3(X >= SLOG_Q ? (powf(10, (X - SLOG_C) / SLOG_A) - SLOG_B) / SLOG_K2
: (X - SLOG_Q) / SLOG_P);
return;
case PL_COLOR_TRC_LINEAR:
case PL_COLOR_TRC_COUNT:
break;
}
pl_unreachable();
scale_out:
MAP3((csp_max - csp_min) * X + csp_min);
}
void pl_color_delinearize(const struct pl_color_space *csp, float color[3])
{
if (csp->transfer == PL_COLOR_TRC_LINEAR)
return;
float csp_min, csp_max;
pl_color_space_nominal_luma_ex(pl_nominal_luma_params(
.color = csp,
.metadata = PL_HDR_METADATA_HDR10,
.scaling = PL_HDR_NORM,
.out_min = &csp_min,
.out_max = &csp_max,
));
if (pl_color_space_is_black_scaled(csp) && csp->transfer != PL_COLOR_TRC_HLG)
MAP3((X - csp_min) / (csp_max - csp_min));
MAP3(fmaxf(X, 0));
switch (csp->transfer) {
case PL_COLOR_TRC_SRGB:
MAP3(X >= 0.0031308f ? 1.055f * powf(X, 1/2.4f) - 0.055f
: 12.92f * X);
return;
case PL_COLOR_TRC_BT_1886: {
const float lb = powf(csp_min, 1/2.4f);
const float lw = powf(csp_max, 1/2.4f);
const float a = powf(lw - lb, 2.4f);
const float b = lb / (lw - lb);
MAP3(powf(X / a, 1/2.4f) - b);
return;
}
case PL_COLOR_TRC_GAMMA18: MAP3(powf(X, 1/1.8f)); return;
case PL_COLOR_TRC_GAMMA20: MAP3(powf(X, 1/2.0f)); return;
case PL_COLOR_TRC_UNKNOWN:
case PL_COLOR_TRC_GAMMA22: MAP3(powf(X, 1/2.2f)); return;
case PL_COLOR_TRC_GAMMA24: MAP3(powf(X, 1/2.4f)); return;
case PL_COLOR_TRC_GAMMA26: MAP3(powf(X, 1/2.6f)); return;
case PL_COLOR_TRC_GAMMA28: MAP3(powf(X, 1/2.8f)); return;
case PL_COLOR_TRC_ST428:
MAP3(powf(X * 48/52.37f, 1/2.6f));
return;
case PL_COLOR_TRC_PRO_PHOTO:
MAP3(X >= 0.001953f ? powf(X, 1/1.8f) : 16 * X);
return;
case PL_COLOR_TRC_PQ:
MAP3(powf(X * PL_COLOR_SDR_WHITE / 10000, PQ_M1));
MAP3(powf((PQ_C1 + PQ_C2 * X) / (1 + PQ_C3 * X), PQ_M2));
return;
case PL_COLOR_TRC_HLG: {
const float y = fmaxf(1.2f + 0.42f * log10f(csp_max / HLG_REF), 1);
const float b = sqrtf(3 * powf(csp_min / csp_max, 1 / y));
const pl_matrix3x3 rgb2xyz =
pl_get_rgb2xyz_matrix(pl_raw_primaries_get(csp->primaries));
const float *coef = rgb2xyz.m[1];
// OOTF^-1
float luma = coef[0] * color[0] + coef[1] * color[1] + coef[2] * color[2];
luma = fmaxf(1e-6f, powf(luma / csp_max, (1 - y) / y));
MAP3(12 / csp_max * luma * X);
// OETF
MAP3(X > 1 ? HLG_A * logf(X - HLG_B) + HLG_C : 0.5f * sqrtf(X));
MAP3((X - b) / (1 - b));
return;
}
case PL_COLOR_TRC_V_LOG:
MAP3(X >= 0.01f ? VLOG_C * log10f(X + VLOG_B) + VLOG_D
: 5.6f * X + 0.125f);
return;
case PL_COLOR_TRC_S_LOG1:
MAP3(SLOG_A * log10f(X + SLOG_B) + SLOG_C);
return;
case PL_COLOR_TRC_S_LOG2:
MAP3(X >= 0 ? SLOG_A * log10f(SLOG_B * X + SLOG_C)
: SLOG_P * X + SLOG_Q);
return;
case PL_COLOR_TRC_LINEAR:
case PL_COLOR_TRC_COUNT:
break;
}
pl_unreachable();
}
void pl_color_space_merge(struct pl_color_space *orig,
const struct pl_color_space *new)
{
if (!orig->primaries)
orig->primaries = new->primaries;
if (!orig->transfer)
orig->transfer = new->transfer;
pl_hdr_metadata_merge(&orig->hdr, &new->hdr);
}
bool pl_color_space_equal(const struct pl_color_space *c1,
const struct pl_color_space *c2)
{
return c1->primaries == c2->primaries &&
c1->transfer == c2->transfer &&
pl_hdr_metadata_equal(&c1->hdr, &c2->hdr);
}
// Estimates luminance from maxRGB by looking at how monochromatic MaxSCL is
static void luma_from_maxrgb(const struct pl_color_space *csp,
enum pl_hdr_scaling scaling,
float *out_max, float *out_avg)
{
const float maxscl = PL_MAX3(csp->hdr.scene_max[0],
csp->hdr.scene_max[1],
csp->hdr.scene_max[2]);
if (!maxscl)
return;
struct pl_raw_primaries prim = csp->hdr.prim;
pl_raw_primaries_merge(&prim, pl_raw_primaries_get(csp->primaries));
const pl_matrix3x3 rgb2xyz = pl_get_rgb2xyz_matrix(&prim);
const float max_luma = rgb2xyz.m[1][0] * csp->hdr.scene_max[0] +
rgb2xyz.m[1][1] * csp->hdr.scene_max[1] +
rgb2xyz.m[1][2] * csp->hdr.scene_max[2];
const float coef = max_luma / maxscl;
*out_max = pl_hdr_rescale(PL_HDR_NITS, scaling, max_luma);
*out_avg = pl_hdr_rescale(PL_HDR_NITS, scaling, coef * csp->hdr.scene_avg);
}
static inline bool metadata_compat(enum pl_hdr_metadata_type metadata,
enum pl_hdr_metadata_type compat)
{
return metadata == PL_HDR_METADATA_ANY || metadata == compat;
}
void pl_color_space_nominal_luma_ex(const struct pl_nominal_luma_params *params)
{
if (!params || (!params->out_min && !params->out_max && !params->out_avg))
return;
const struct pl_color_space *csp = params->color;
const enum pl_hdr_scaling scaling = params->scaling;
float min_luma = 0, max_luma = 0, avg_luma = 0;
if (params->metadata != PL_HDR_METADATA_NONE) {
// Initialize from static HDR10 metadata, in all cases
min_luma = pl_hdr_rescale(PL_HDR_NITS, scaling, csp->hdr.min_luma);
max_luma = pl_hdr_rescale(PL_HDR_NITS, scaling, csp->hdr.max_luma);
}
if (metadata_compat(params->metadata, PL_HDR_METADATA_HDR10PLUS) &&
pl_hdr_metadata_contains(&csp->hdr, PL_HDR_METADATA_HDR10PLUS))
{
luma_from_maxrgb(csp, scaling, &max_luma, &avg_luma);
}
if (metadata_compat(params->metadata, PL_HDR_METADATA_CIE_Y) &&
pl_hdr_metadata_contains(&csp->hdr, PL_HDR_METADATA_CIE_Y))
{
max_luma = pl_hdr_rescale(PL_HDR_PQ, scaling, csp->hdr.max_pq_y);
avg_luma = pl_hdr_rescale(PL_HDR_PQ, scaling, csp->hdr.avg_pq_y);
}
// Clamp to sane value range
const float hdr_min = pl_hdr_rescale(PL_HDR_NITS, scaling, PL_COLOR_HDR_BLACK);
const float hdr_max = pl_hdr_rescale(PL_HDR_PQ, scaling, 1.0f);
max_luma = max_luma ? PL_CLAMP(max_luma, hdr_min, hdr_max) : 0;
min_luma = min_luma ? PL_CLAMP(min_luma, hdr_min, hdr_max) : 0;
if ((max_luma && min_luma >= max_luma) || min_luma >= hdr_max)
min_luma = max_luma = 0; // sanity
// PQ is always scaled down to absolute black, ignoring HDR metadata
if (csp->transfer == PL_COLOR_TRC_PQ)
min_luma = hdr_min;
// Baseline/fallback metadata, inferred entirely from the colorspace
// description and built-in default assumptions
if (!max_luma) {
if (csp->transfer == PL_COLOR_TRC_HLG) {
max_luma = pl_hdr_rescale(PL_HDR_NITS, scaling, PL_COLOR_HLG_PEAK);
} else {
const float peak = pl_color_transfer_nominal_peak(csp->transfer);
max_luma = pl_hdr_rescale(PL_HDR_NORM, scaling, peak);
}
}
if (!min_luma) {
if (pl_color_transfer_is_hdr(csp->transfer)) {
min_luma = hdr_min;
} else {
const float peak = pl_hdr_rescale(scaling, PL_HDR_NITS, max_luma);
min_luma = pl_hdr_rescale(PL_HDR_NITS, scaling,
peak / PL_COLOR_SDR_CONTRAST);
}
}
if (avg_luma)
avg_luma = PL_CLAMP(avg_luma, min_luma, max_luma); // sanity
if (params->out_min)
*params->out_min = min_luma;
if (params->out_max)
*params->out_max = max_luma;
if (params->out_avg)
*params->out_avg = avg_luma;
}
void pl_color_space_infer(struct pl_color_space *space)
{
if (!space->primaries)
space->primaries = PL_COLOR_PRIM_BT_709;
if (!space->transfer)
space->transfer = PL_COLOR_TRC_BT_1886;
// Sanitize the static HDR metadata
pl_color_space_nominal_luma_ex(pl_nominal_luma_params(
.color = space,
.metadata = PL_HDR_METADATA_HDR10,
.scaling = PL_HDR_NITS,
.out_max = &space->hdr.max_luma,
// Preserve tagged minimum
.out_min = space->hdr.min_luma ? NULL : &space->hdr.min_luma,
));
// Default the signal color space based on the nominal raw primaries
if (!pl_primaries_valid(&space->hdr.prim))
space->hdr.prim = *pl_raw_primaries_get(space->primaries);
}
static void infer_both_ref(struct pl_color_space *space,
struct pl_color_space *ref)
{
pl_color_space_infer(ref);
if (!space->primaries) {
if (pl_color_primaries_is_wide_gamut(ref->primaries)) {
space->primaries = PL_COLOR_PRIM_BT_709;
} else {
space->primaries = ref->primaries;
}
}
if (!space->transfer) {
switch (ref->transfer) {
case PL_COLOR_TRC_UNKNOWN:
case PL_COLOR_TRC_COUNT:
pl_unreachable();
case PL_COLOR_TRC_BT_1886:
case PL_COLOR_TRC_SRGB:
case PL_COLOR_TRC_GAMMA22:
// Re-use input transfer curve to avoid small adaptations
space->transfer = ref->transfer;
break;
case PL_COLOR_TRC_PQ:
case PL_COLOR_TRC_HLG:
case PL_COLOR_TRC_V_LOG:
case PL_COLOR_TRC_S_LOG1:
case PL_COLOR_TRC_S_LOG2:
// Pick BT.1886 model because it models SDR contrast accurately,
// and we need contrast information for tone mapping
space->transfer = PL_COLOR_TRC_BT_1886;
break;
case PL_COLOR_TRC_PRO_PHOTO:
// ProPhotoRGB and sRGB are both piecewise with linear slope
space->transfer = PL_COLOR_TRC_SRGB;
break;
case PL_COLOR_TRC_LINEAR:
case PL_COLOR_TRC_GAMMA18:
case PL_COLOR_TRC_GAMMA20:
case PL_COLOR_TRC_GAMMA24:
case PL_COLOR_TRC_GAMMA26:
case PL_COLOR_TRC_GAMMA28:
case PL_COLOR_TRC_ST428:
// Pick pure power output curve to avoid introducing black crush
space->transfer = PL_COLOR_TRC_GAMMA22;
break;
}
}
// Infer the remaining fields after making the above choices
pl_color_space_infer(space);
}
void pl_color_space_infer_ref(struct pl_color_space *space,
const struct pl_color_space *refp)
{
// Make a copy of `refp` to infer missing values first
struct pl_color_space ref = *refp;
infer_both_ref(space, &ref);
}
void pl_color_space_infer_map(struct pl_color_space *src,
struct pl_color_space *dst)
{
bool unknown_src_contrast = !src->hdr.min_luma;
bool unknown_dst_contrast = !dst->hdr.min_luma;
infer_both_ref(dst, src);
// If the src has an unspecified gamma curve with dynamic black scaling,
// default it to match the dst colorspace contrast. This does not matter in
// most cases, but ensures that BT.1886 is tuned to the appropriate black
// point by default.
bool dynamic_src_contrast = pl_color_space_is_black_scaled(src) ||
src->transfer == PL_COLOR_TRC_BT_1886;
if (unknown_src_contrast && dynamic_src_contrast)
src->hdr.min_luma = dst->hdr.min_luma;
// Do the same in reverse if both src and dst are SDR curves
bool src_is_sdr = !pl_color_space_is_hdr(src);
bool dst_is_sdr = !pl_color_space_is_hdr(dst);
if (unknown_dst_contrast && src_is_sdr && dst_is_sdr)
dst->hdr.min_luma = src->hdr.min_luma;
// If the src is HLG and the output is HDR, tune the HLG peak to the output
if (src->transfer == PL_COLOR_TRC_HLG && pl_color_space_is_hdr(dst))
src->hdr.max_luma = dst->hdr.max_luma;
}
const struct pl_color_adjustment pl_color_adjustment_neutral = {
PL_COLOR_ADJUSTMENT_NEUTRAL
};
void pl_chroma_location_offset(enum pl_chroma_location loc, float *x, float *y)
{
*x = *y = 0;
// This is the majority of subsampled chroma content out there
loc = PL_DEF(loc, PL_CHROMA_LEFT);
switch (loc) {
case PL_CHROMA_LEFT:
case PL_CHROMA_TOP_LEFT:
case PL_CHROMA_BOTTOM_LEFT:
*x = -0.5;
break;
default: break;
}
switch (loc) {
case PL_CHROMA_TOP_LEFT:
case PL_CHROMA_TOP_CENTER:
*y = -0.5;
break;
default: break;
}
switch (loc) {
case PL_CHROMA_BOTTOM_LEFT:
case PL_CHROMA_BOTTOM_CENTER:
*y = 0.5;
break;
default: break;
}
}
struct pl_cie_xy pl_white_from_temp(float temp)
{
temp = PL_CLAMP(temp, 2500, 25000);
double ti = 1000.0 / temp, ti2 = ti * ti, ti3 = ti2 * ti, x;
if (temp <= 7000) {
x = -4.6070 * ti3 + 2.9678 * ti2 + 0.09911 * ti + 0.244063;
} else {
x = -2.0064 * ti3 + 1.9018 * ti2 + 0.24748 * ti + 0.237040;
}