swscale: scale the X'Y'Z' conversion as SMPTE ST 428-1 defines it

The gamma tables put the top code of X'Y'Z' at linear 1.0, so the
48 cd/m2 reference white of a DCDM, code 3960, came out at 0.918, about
8 percent dark. ST 428-1 (H.273 transfer 17) puts 52.37 cd/m2 at the top
code, L = 52.37/48 * V^2.6, so that the white lands on 1.0 and the codes
above it are headroom. Fold the factor into the matrices, since the X and
Z of a white exceed 1.0 and a 16-bit table cannot hold them: the headroom
clips in RGB after the matrix, and RGB white encodes to code 3960.

Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
This commit is contained in:
Kacper Michajłow committed 2026-10-28 19:22:28 +00:00
1 parent af885b9b2f
commit 61ae1901c7
25 files changed
+41 -38

No files matched your search

+7 -4
View File
@@ -750,11 +750,11 @@ static av_cold void init_xyz_tables(void)
}
}
static void fill_xyz_matrix(int16_t mat[3][3], const SwsMatrix3x3 *m)
static void fill_xyz_matrix(int16_t mat[3][3], const SwsMatrix3x3 *m, double scale)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
const double v = m->m[i][j] * 4096.0;
const double v = m->m[i][j] * scale * 4096.0;
av_assert1(fabs(v) <= INT16_MAX);
mat[i][j] = lrint(v);
}
@@ -763,6 +763,9 @@ static void fill_xyz_matrix(int16_t mat[3][3], const SwsMatrix3x3 *m)
av_cold int ff_sws_fill_st428_tables(SwsInternal *c)
{
/* X'Y'Z' puts its 48 cd/m2 white at code 3960 and the top code at
* 52.37 cd/m2 (ST 428-1), the codes above the white clip in RGB */
const double peak = av_csp_trc_func_inv_from_id(AVCOL_TRC_SMPTE428)(1.0);
const AVColorPrimariesDesc *bt709 = av_csp_primaries_desc_from_id(AVCOL_PRI_BT709);
const SwsMatrix3x3 xyz2rgb = ff_sws_xyz2rgb(bt709);
const SwsMatrix3x3 rgb2xyz = ff_sws_rgb2xyz(bt709);
@@ -770,8 +773,8 @@ av_cold int ff_sws_fill_st428_tables(SwsInternal *c)
if (c->xyz2rgb.gamma.in)
return 0;
fill_xyz_matrix(c->xyz2rgb.mat, &xyz2rgb);
fill_xyz_matrix(c->rgb2xyz.mat, &rgb2xyz);
fill_xyz_matrix(c->xyz2rgb.mat, &xyz2rgb, peak);
fill_xyz_matrix(c->rgb2xyz.mat, &rgb2xyz, 1.0 / peak);
#if CONFIG_SMALL
c->xyz2rgb.gamma.in = av_malloc(sizeof(uint16_t) * 2 * (4096 + 65536));
+1 -1
View File
@@ -1 +1 @@
pixdesc-xyz12be 03b78ced8e69fd61e55040a3926e10a0
pixdesc-xyz12be 1cb3b45b4be372ea5392f89a2cba9b97
+1 -1
View File
@@ -1 +1 @@
pixdesc-xyz12le 2e028ed810f75ee2cda02b1f90519284
pixdesc-xyz12le 46b9b7bdc4732d2370ebe89c56bc60f7
+2 -2
View File
@@ -117,8 +117,8 @@ xv36be 9f556ee59a672fd8725f0bb36ce3e4b0
xv36le e08dcbde02f1c28a3554f372ad1278e2
xv48be ce34993b4b4411bba1d852b9b86aa39e
xv48le df913a7e61b162aa98303e5393e60c63
xyz12be c4f3cb794f1c15770c2a8690f49e4395
xyz12le 938885a1a9150b103adab1d3aefad5f6
xyz12be c10b0f0f886ff1444d27cb6add490b77
xyz12le e6739a863ccf9b26bf6991dc370b4f27
y210le 04e9487b6cce38e7531437e946cdd586
y212le 825768be8fe92708ae80be84855066ed
y216le 0e99aeddfee304e72d525d72998d9e9b
+2 -2
View File
@@ -114,8 +114,8 @@ xv36be 23b6f253fcb375e4145cfcb562268c5f
xv36le 778286003497f92b84d0bd8258d6b85d
xv48be c90889b2cf54cc78bd58e8c47d4eb791
xv48le 2c15c1254449ec5f9135ae61bdf4e1d5
xyz12be 117d5a92f98bba9dcea367d8d731c80a
xyz12le baa4f90965b2964b7b1ecd2a8dbcce60
xyz12be 1d86d11684be38cacce52f63fc0f20c5
xyz12le 122fb7eea3575b4edbbd83219bc7af91
ya16be e2b490dc18f6ca3814fe9d68193ba263
ya16le 33b0e26f3c2e89c651345970474f6703
ya8 51a8dd297e35d40b06d3ebe8f4717895
+2 -2
View File
@@ -117,8 +117,8 @@ xv36be bcc7bda2d0a5d43db4464af6a4cb5d65
xv36le ba99f258370f2a56993e8760e6b30194
xv48be 2abcd986a34789ba4310be3969020d0d
xv48le f6f2e33f260f48334197538f3331f7bc
xyz12be 5f472c753927b9b672d9f54a4d50be2f
xyz12le 1c57f2d6fbc86123674620fe16762b70
xyz12be aa529d2ea8ec254a6e54d90d64be0937
xyz12le ad85650ceb61a71bb89acca7cd24c24e
y210le 4c2fba1dc40322584977d15dd07c9146
y212le ac2a47c45187dd54d0f55293cbffd954
y216le e65b5bfae1b40edbbed2012e9cd45e31
+2 -2
View File
@@ -106,8 +106,8 @@ xv36be 962386c88268f4382004c3a7a82c5eb8
xv36le bcceffc985aaa8414c4b8072aa0889bd
xv48be 4d6e4004b03767f12df8bb4e76c98ddf
xv48le 9e94d82461a2131063157ac0dbe9467b
xyz12be 4d1f54d3d5140b23a7ed8d6746e9c08e
xyz12le a12154aa9b0f61c6a76382a5515b9234
xyz12be ec36fb7f51df4859b71e290c6a44b7b7
xyz12le 413a52726d022bec91222a36f9e683cd
y210le 22b1a02a39c4b325726bf8793bf1e8f2
y212le 2f08fb195b948056c844acb1eee8d649
y216le 360cb98ac80b13d3a8ec61c9f1ff3bac
+2 -2
View File
@@ -114,8 +114,8 @@ xv36be 98f578df965eed369f46cb135e2d1345
xv36le e478b4b54698beb3ce1b9a2dd691d544
xv48be e030a2c7b1b600cfacb691b6e90c2e3d
xv48le fbd7f8c65cd6fc9f9108dc9a1f977dc3
xyz12be 4b778cc2db5060f76db855f7800de41d
xyz12le f39689517dbee5988a9cc94f1b0db438
xyz12be 271a65e4bea9175cb9d7e2c0ea0ba14d
xyz12le a8273fe17f2a738f3fd7f457de5c1df4
ya16be e7665e62e476df4eb143420e57cc9ec2
ya16le 3a23b71db418b74e1cc304d7cd3e1989
ya8 4ad5920716de3d2fbbc49f95adb60345
+2 -2
View File
@@ -116,8 +116,8 @@ xv36be 3bbb949278ea55cc947ee03bd9c27c2d
xv36le 102c0e817d375ddd6b2cfbb4262dec95
xv48be 4d7376651fb7b3e84d00abad6c785aad
xv48le a1a8ff16d9a864568e5e557734bf3d6d
xyz12be aec98321799e590cc58aed1202da70a3
xyz12le 7e54e36d02b73747c47574aa25e83d8a
xyz12be fa10b075452f6fe0b5825453d89eeb81
xyz12le 5c0bd08982a5219266cd1d3026a08c52
y210le d4cf9b53cd7ff22f087743d483e88480
y212le d5a2b4677ddb4a3bc3e5cd5cbb20f426
y216le 9e44c6d76b09bcbe71738423b4b3d67a
+2 -2
View File
@@ -117,8 +117,8 @@ xv36be 9f556ee59a672fd8725f0bb36ce3e4b0
xv36le e08dcbde02f1c28a3554f372ad1278e2
xv48be ce34993b4b4411bba1d852b9b86aa39e
xv48le df913a7e61b162aa98303e5393e60c63
xyz12be c4f3cb794f1c15770c2a8690f49e4395
xyz12le 938885a1a9150b103adab1d3aefad5f6
xyz12be c10b0f0f886ff1444d27cb6add490b77
xyz12le e6739a863ccf9b26bf6991dc370b4f27
y210le 04e9487b6cce38e7531437e946cdd586
y212le 825768be8fe92708ae80be84855066ed
y216le 0e99aeddfee304e72d525d72998d9e9b
+2 -2
View File
@@ -117,8 +117,8 @@ xv36be 4d084adca0228d7750d1e2e877e0d79b
xv36le de9c74e94dc19c828e1572aa283d8aca
xv48be 9e58d1a045df100b0dec116e13be5b4e
xv48le fd873d53609b2fbdfe99470f515a234c
xyz12be 681537982da2c5cfaa99912100d5a81d
xyz12le 17c835d55db8d8177b018f9d4461960d
xyz12be 0866f8e5493ea93f4523a9e873a14b1e
xyz12le d6990efe1c7141227054cbcda28ea6fb
y210le 7c2aef142d88ab343ec01acd45f38466
y212le 39a3c0c843041ad4501b3107dd91ef17
y216le 17be2999e97d36b8ed903f07ef428c09
+2 -2
View File
@@ -104,8 +104,8 @@ xv36be 2261a0e3db5ee607d37f68d19704ae15
xv36le 9202133de91bf64c76ca27d5cd0c816a
xv48be 14373b7fe123225689e76fe2ce43fb93
xv48le 319df9724a067c7b5efa215f9f54d127
xyz12be fc838024ab1eebf3a400e8a4a1f980d9
xyz12le 33ab866ecb98fe6d028123ea9906e9a6
xyz12be 8ac923c86bd11a04b049d083d857d116
xyz12le 77912a89e6525f6809d8c31fed87a439
ya16be 9bb39f9567d807be0fb8631dcfa8d5f8
ya16le b13453818b83c8dc2610975095393ac1
ya8 d4b7a62f80681fa44c977ff3a64f4ce4
+2 -2
View File
@@ -117,8 +117,8 @@ xv36be c0272372d3e1a59adb3931ee433a5d5b
xv36le ffe6ab75ebc09134c3451f8f6ef0d501
xv48be bdfc3217ae456b370dbdcf4d52606a3f
xv48le 53dbebab73a66539c04644ef56dc6bbb
xyz12be f0e4f326736c8158aeb89071e15f6688
xyz12le c7817e4c8da785850fa3269aec12c971
xyz12be 0f14b1f9cfa04d9e1c945dd5057f3175
xyz12le e1b12cc0f304a5e62ac99ccf90f88756
y210le f8847bedd3ae6e1c0cf84a823f275e31
y212le c801725ae31e3b8f5be269359d49f191
y216le 985db498aedf3fb1c547ad07442b7258
+1 -1
View File
@@ -1,2 +1,2 @@
8f95608cd59572ae0a5380da8b8518c6 *tests/data/pixfmt/gbrp-xyz12le.yuv
8ff49367a8420d70a01e284b68aa6205 *tests/data/pixfmt/gbrp-xyz12le.yuv
7603200 tests/data/pixfmt/gbrp-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
87e0d57bae18f4c69cf0594253a5e97c *tests/data/pixfmt/gbrp10-xyz12le.yuv
5b3787cc3abeb28ac2045da52ad08a10 *tests/data/pixfmt/gbrp10-xyz12le.yuv
15206400 tests/data/pixfmt/gbrp10-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
882edf8efc53ffa9eb63e87a5acb8194 *tests/data/pixfmt/gbrp12-xyz12le.yuv
66665b89e7e0ac71e6d3ee85300921ae *tests/data/pixfmt/gbrp12-xyz12le.yuv
15206400 tests/data/pixfmt/gbrp12-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
890bbcd28ea3a06c315acb8c474215a0 *tests/data/pixfmt/nv24-xyz12le.yuv
dc43783d32e1035f30c006eaa9fef0da *tests/data/pixfmt/nv24-xyz12le.yuv
7603200 tests/data/pixfmt/nv24-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
ce3777038372083419a25923d9b6029f *tests/data/pixfmt/p410-xyz12le.yuv
a31b9e74db3a4d230e924283ba0b365b *tests/data/pixfmt/p410-xyz12le.yuv
15206400 tests/data/pixfmt/p410-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
7f1ac958f69ba454d23d667ecb4bc035 *tests/data/pixfmt/p412-xyz12le.yuv
23f380ee860ab3f6af50228d5ead1a2c *tests/data/pixfmt/p412-xyz12le.yuv
15206400 tests/data/pixfmt/p412-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
6c138c678af6e0002167d0b088cbc57a *tests/data/pixfmt/rgb24-xyz12le.yuv
c1b035b9c5438ae8307d94a855f3639e *tests/data/pixfmt/rgb24-xyz12le.yuv
7603200 tests/data/pixfmt/rgb24-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
449f3253f683ea30518dcd345524a080 *tests/data/pixfmt/rgb48-xyz12le.yuv
5013bb1fa0d6ceb6921110d719bd396c *tests/data/pixfmt/rgb48-xyz12le.yuv
15206400 tests/data/pixfmt/rgb48-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
8c8f1b04b0fe5aab4fc4f0612b4c3c09 *tests/data/pixfmt/xyz12le.yuv
2e8a8e94dc351f2da89471a8afd87165 *tests/data/pixfmt/xyz12le.yuv
304128 tests/data/pixfmt/xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
35306788a7a59819bff4dade8c89ddff *tests/data/pixfmt/yuv444p-xyz12le.yuv
aa8bffbcda1f890d29d4b70b155a0bbd *tests/data/pixfmt/yuv444p-xyz12le.yuv
7603200 tests/data/pixfmt/yuv444p-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
b03201a536e152bff73cdbb9266ed948 *tests/data/pixfmt/yuv444p10-xyz12le.yuv
78ed7dcd55f9a9c73247d2abcdef36a7 *tests/data/pixfmt/yuv444p10-xyz12le.yuv
15206400 tests/data/pixfmt/yuv444p10-xyz12le.yuv
+1 -1
View File
@@ -1,2 +1,2 @@
d9072d39396287f67eb38a1fec1abc6e *tests/data/pixfmt/yuv444p12-xyz12le.yuv
9d0d5fb133d1b303688d444b16b4bd5b *tests/data/pixfmt/yuv444p12-xyz12le.yuv
15206400 tests/data/pixfmt/yuv444p12-xyz12le.yuv