Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): decode limited-range YUV around 16 and 128 on the GPU

Richie McIlroycba36cefced0
diff --git a/crates/rendering/src/cpu_yuv.rs b/crates/rendering/src/cpu_yuv.rs
index cd242b493..2fb98fb7b 100644
--- a/crates/rendering/src/cpu_yuv.rs
+++ b/crates/rendering/src/cpu_yuv.rs
@@ -41,7 +41,7 @@ impl YuvMatrix {
/// R = Y + rv*V, G = Y - gu*U - gv*V, B = Y + bu*U.
pub(crate) fn shader_constants(self) -> [(&'static str, f64); 4] {
let (rv, gu, gv, bu) = match self {
- Self::Bt601 => (1.596, 0.391, 0.813, 2.018),
+ Self::Bt601 => (1.596027, 0.391762, 0.812968, 2.017232),
Self::Bt709 => (1.792741, 0.213249, 0.532909, 2.112402),
};
[("RV", rv), ("GU", gu), ("GV", gv), ("BU", bu)]
diff --git a/crates/rendering/src/shaders/nv12_to_rgba.wgsl b/crates/rendering/src/shaders/nv12_to_rgba.wgsl
index a6093790f..d4f7f04eb 100644
--- a/crates/rendering/src/shaders/nv12_to_rgba.wgsl
+++ b/crates/rendering/src/shaders/nv12_to_rgba.wgsl
@@ -1,10 +1,10 @@
-// Limited-range Y'CbCr to RGB. The chroma coefficients are pipeline
-// overrides (YuvMatrix::shader_constants): BT.601 by default, BT.709 for
-// sources tagged BT.709.
-override RV: f32 = 1.596;
-override GU: f32 = 0.391;
-override GV: f32 = 0.813;
-override BU: f32 = 2.018;
+// Limited-range Y'CbCr to RGB: luma 16..235, chroma centred on 128. The
+// chroma coefficients are pipeline overrides (YuvMatrix::shader_constants):
+// BT.601 by default, BT.709 for sources tagged BT.709.
+override RV: f32 = 1.596027;
+override GU: f32 = 0.391762;
+override GV: f32 = 0.812968;
+override BU: f32 = 2.017232;
@group(0) @binding(0) var y_plane: texture_2d<f32>;
@group(0) @binding(1) var uv_plane: texture_2d<f32>;
@@ -26,9 +26,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let uv_clamped = min(uv_coords, uv_dims - vec2<u32>(1, 1));
let uv_raw = textureLoad(uv_plane, uv_clamped, 0).rg;
- let y = (y_raw - 0.0625) * 1.164;
- let u = uv_raw.r - 0.5;
- let v = uv_raw.g - 0.5;
+ let y = (y_raw - 16.0 / 255.0) * (255.0 / 219.0);
+ let u = uv_raw.r - 128.0 / 255.0;
+ let v = uv_raw.g - 128.0 / 255.0;
let r = y + RV * v;
let g = y - GU * u - GV * v;
diff --git a/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl b/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
index 8838df9bc..c95d3ae38 100644
--- a/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
+++ b/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
@@ -1,10 +1,10 @@
-// Limited-range Y'CbCr to RGB. The chroma coefficients are pipeline
-// overrides (YuvMatrix::shader_constants): BT.601 by default, BT.709 for
-// sources tagged BT.709.
-override RV: f32 = 1.596;
-override GU: f32 = 0.391;
-override GV: f32 = 0.813;
-override BU: f32 = 2.018;
+// Limited-range Y'CbCr to RGB: luma 16..235, chroma centred on 128. The
+// chroma coefficients are pipeline overrides (YuvMatrix::shader_constants):
+// BT.601 by default, BT.709 for sources tagged BT.709.
+override RV: f32 = 1.596027;
+override GU: f32 = 0.391762;
+override GV: f32 = 0.812968;
+override BU: f32 = 2.017232;
@group(0) @binding(0) var y_plane: texture_2d<f32>;
@group(0) @binding(1) var u_plane: texture_2d<f32>;
@@ -30,9 +30,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let u_raw = textureLoad(u_plane, u_clamped, 0).r;
let v_raw = textureLoad(v_plane, v_clamped, 0).r;
- let y = (y_raw - 0.0625) * 1.164;
- let u = u_raw - 0.5;
- let v = v_raw - 0.5;
+ let y = (y_raw - 16.0 / 255.0) * (255.0 / 219.0);
+ let u = u_raw - 128.0 / 255.0;
+ let v = v_raw - 128.0 / 255.0;
let r = y + RV * v;
let g = y - GU * u - GV * v;
diff --git a/crates/rendering/src/yuv_converter.rs b/crates/rendering/src/yuv_converter.rs
index d9c7c9ffc..8f755b3c9 100644
--- a/crates/rendering/src/yuv_converter.rs
+++ b/crates/rendering/src/yuv_converter.rs
@@ -1979,10 +1979,8 @@ mod colour_matrix_tests {
i32::from(uv[x + 1]) - 128,
);
let actual = &rgba[x * 4..x * 4 + 3];
- // The shaders' float constants and 16/256 luma offset differ
- // from the 8.8 integer path by up to 2 levels (as before).
assert!(
- actual.iter().zip(expected).all(|(a, e)| a.abs_diff(e) <= 2),
+ actual.iter().zip(expected).all(|(a, e)| a.abs_diff(e) <= 1),
"{matrix:?} {rgb:?}: GPU {actual:?}, CPU {expected:?}"
);
if matrix == YuvMatrix::Bt709 {