Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(compositor): leave BT.2020 CL on the default path, convert 10-bit samples with 10-bit bounds

EtienneLescot4ffcce49dac8
diff --git a/crates/compositor/src/compositor_macos.rs b/crates/compositor/src/compositor_macos.rs
index 6d724a20f..f1b8cb79a 100644
--- a/crates/compositor/src/compositor_macos.rs
+++ b/crates/compositor/src/compositor_macos.rs
@@ -235,14 +235,16 @@ fn swizzled_view(tex: &metal::TextureRef, swizzle: [u8; 4]) -> metal::Texture {
/// Le swizzle du plan Y qui porte la matrice de la frame, ou `None` pour BT.709 (aussi le
/// défaut d'un flux non tagué). La matrice vient de `AVFrame::colorspace`, que libavcodec lit
/// dans le VUI : VideoToolbox ne pose pas `kCVImageBufferYCbCrMatrixKey` sur ses buffers.
-unsafe fn matrix_swizzle(frame: *const AVFrame) -> Option<[u8; 4]> {
- match (*frame).colorspace {
+/// BT.2020 CL n'est pas une simple matrice : il garde le défaut plutôt qu'un rendu NCL faux.
+///
+/// Alpha à 0 : échantillons 10 bits (P010), que `yuv_matrix` convertit avec les bornes 10 bits.
+fn matrix_swizzle(colorspace: AVColorSpace::Type, ten_bit: bool) -> Option<[u8; 4]> {
+ let depth = if ten_bit { SWZ_ZERO } else { SWZ_ONE };
+ match colorspace {
AVColorSpace::AVCOL_SPC_BT470BG | AVColorSpace::AVCOL_SPC_SMPTE170M => {
- Some([SWZ_RED, SWZ_ONE, SWZ_ZERO, SWZ_ONE])
- }
- AVColorSpace::AVCOL_SPC_BT2020_NCL | AVColorSpace::AVCOL_SPC_BT2020_CL => {
- Some([SWZ_RED, SWZ_ZERO, SWZ_ONE, SWZ_ONE])
+ Some([SWZ_RED, SWZ_ONE, SWZ_ZERO, depth])
}
+ AVColorSpace::AVCOL_SPC_BT2020_NCL => Some([SWZ_RED, SWZ_ZERO, SWZ_ONE, depth]),
_ => None,
}
}
@@ -1230,7 +1232,8 @@ impl Compositor {
};
let mut y = cache.make_texture_from_pixel_buffer(pb, 0, y_fmt)?;
let mut uv = cache.make_texture_from_pixel_buffer(pb, 1, uv_fmt)?;
- if let Some(swizzle) = matrix_swizzle(frame) {
+ let ten_bit = matches!(pixel_format, CV_420_10_BIPLANAR_VIDEO | CV_420_10_BIPLANAR_FULL);
+ if let Some(swizzle) = matrix_swizzle((*frame).colorspace, ten_bit) {
y = swizzled_view(&y, swizzle);
}
if matches!(pixel_format, CV_420_8_BIPLANAR_FULL | CV_420_10_BIPLANAR_FULL) {
@@ -5451,4 +5454,19 @@ mod tests {
crate::shader_fx::compile_metal(&gpu.device, &code, &params).expect("compile");
eprintln!("recompilation d'une source déjà vue : {:.1} ms", t0.elapsed().as_secs_f64() * 1000.0);
}
+
+ #[test]
+ fn matrix_swizzle_marks_matrix_and_depth() {
+ use AVColorSpace::*;
+ assert_eq!(
+ matrix_swizzle(AVCOL_SPC_SMPTE170M, false),
+ Some([SWZ_RED, SWZ_ONE, SWZ_ZERO, SWZ_ONE])
+ );
+ assert_eq!(
+ matrix_swizzle(AVCOL_SPC_BT2020_NCL, true),
+ Some([SWZ_RED, SWZ_ZERO, SWZ_ONE, SWZ_ZERO])
+ );
+ assert_eq!(matrix_swizzle(AVCOL_SPC_BT709, false), None);
+ assert_eq!(matrix_swizzle(AVCOL_SPC_BT2020_CL, true), None);
+ }
}
diff --git a/crates/compositor/src/shaders.metal b/crates/compositor/src/shaders.metal
index fae034f48..06ee2d87b 100644
--- a/crates/compositor/src/shaders.metal
+++ b/crates/compositor/src/shaders.metal
@@ -155,11 +155,21 @@ inline float3 yuv709_full(float y, float2 cbcr)
// BT.601 / BT.2020 -> RGB. `m` : coefficients Cr->R, Cb->G, Cr->G, Cb->B. BT.709 garde ses
// deux fonctions à constantes littérales, pour rendre ses frames à l'octet près comme avant.
-inline float3 yuv_matrix(float y, float2 cbcr, float4 m, bool full)
-{
- float Yf = full ? y : (y * 255.0 - 16.0) / 219.0;
- float Cb = full ? cbcr.x - 128.0 / 255.0 : (cbcr.x * 255.0 - 128.0) / 224.0;
- float Cr = full ? cbcr.y - 128.0 / 255.0 : (cbcr.y * 255.0 - 128.0) / 224.0;
+// `ten_bit` : un échantillon P010 lu en `R16Unorm` vaut code10 * 64 / 65535, pas code8 / 255 ;
+// on revient au code 10 bits pour appliquer ses bornes (64..940, chroma 512, pleine 0..1023).
+inline float3 yuv_matrix(float y, float2 cbcr, float4 m, bool full, bool ten_bit)
+{
+ float Yf, Cb, Cr;
+ if (ten_bit) {
+ float code = 65535.0 / 64.0;
+ Yf = full ? y * code / 1023.0 : (y * code - 64.0) / 876.0;
+ Cb = full ? (cbcr.x * code - 512.0) / 1023.0 : (cbcr.x * code - 512.0) / 896.0;
+ Cr = full ? (cbcr.y * code - 512.0) / 1023.0 : (cbcr.y * code - 512.0) / 896.0;
+ } else {
+ Yf = full ? y : (y * 255.0 - 16.0) / 219.0;
+ Cb = full ? cbcr.x - 128.0 / 255.0 : (cbcr.x * 255.0 - 128.0) / 224.0;
+ Cr = full ? cbcr.y - 128.0 / 255.0 : (cbcr.y * 255.0 - 128.0) / 224.0;
+ }
float3 rgb;
rgb.r = Yf + m.x * Cr;
rgb.g = Yf - m.y * Cb - m.z * Cr;
@@ -178,11 +188,13 @@ inline float3 sample_yuv(float2 uv,
float4 c = texUV.sample(samp, uv);
// Bleu à 1 sur la vue CbCr : pleine plage (420f, xf20).
bool full = c.b > 0.5;
- // Vert ou bleu à 1 sur la vue Y : matrice BT.601 ou BT.2020 posée par `nv12_srvs`.
+ // Vert ou bleu à 1 sur la vue Y : matrice BT.601 ou BT.2020 posée par `nv12_srvs` ;
+ // alpha à 0 sur cette vue : échantillons 10 bits.
+ bool ten_bit = y.a < 0.5;
if (y.g > 0.5)
- return yuv_matrix(y.r, c.rg, float4(1.402, 0.344136, 0.714136, 1.772), full);
+ return yuv_matrix(y.r, c.rg, float4(1.402, 0.344136, 0.714136, 1.772), full, ten_bit);
if (y.b > 0.5)
- return yuv_matrix(y.r, c.rg, float4(1.4746, 0.164553, 0.571353, 1.8814), full);
+ return yuv_matrix(y.r, c.rg, float4(1.4746, 0.164553, 0.571353, 1.8814), full, ten_bit);
return full ? yuv709_full(y.r, c.rg) : yuv709_limited(y.r, c.rg);
}