#![expect(
unsafe_code,
reason = "a picture's CVPixelBuffer and IOSurface are C objects behind a raw pointer, and wgpu's Metal textures are imported unsafely"
)]
use std::{ptr, sync::Arc};
use cranpose_ui::ImageBitmap;
use objc2::{rc::Retained, runtime::ProtocolObject};
use objc2_core_foundation::{CFRetained, CFString, CFType};
use objc2_core_video::{
CVPixelBuffer, CVPixelBufferGetHeightOfPlane, CVPixelBufferGetIOSurface,
CVPixelBufferGetPixelFormatType, CVPixelBufferGetPlaneCount, CVPixelBufferGetWidthOfPlane,
kCVImageBufferYCbCrMatrixKey, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
};
use objc2_io_surface::IOSurfaceRef;
use objc2_metal::{
MTLDevice, MTLPixelFormat, MTLTextureDescriptor, MTLTextureType, MTLTextureUsage,
};
use crate::{ExternalFrames, FrameWriter, GpuContext, VideoFrame, cover_pipeline::cover_pipeline};
struct Plane {
index: usize,
metal: MTLPixelFormat,
wgpu: wgpu::TextureFormat,
}
const PLANES: [Plane; 2] = [
Plane {
index: 0,
metal: MTLPixelFormat::R8Unorm,
wgpu: wgpu::TextureFormat::R8Unorm,
},
Plane {
index: 1,
metal: MTLPixelFormat::RG8Unorm,
wgpu: wgpu::TextureFormat::Rg8Unorm,
},
];
const UNIFORM_SIZE: usize = 32;
pub(crate) struct Nv12Converter {
gpu: GpuContext,
metal: Retained<ProtocolObject<dyn MTLDevice>>,
frames: ExternalFrames,
writer: FrameWriter,
size: (u32, u32),
pipeline: wgpu::RenderPipeline,
layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
uniforms: wgpu::Buffer,
matrix: Option<(CFRetained<CFType>, Option<Matrix>)>,
}
impl Nv12Converter {
pub(crate) fn new(gpu: &GpuContext) -> Option<Self> {
let Some(hal) = (unsafe { gpu.device.as_hal::<wgpu::hal::api::Metal>() }) else {
log::warn!("player surface: the renderer's device is not a Metal device");
return None;
};
let metal = hal.raw_device().clone();
drop(hal);
let device = &gpu.device;
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("nv12 to rgb"),
source: wgpu::ShaderSource::Wgsl(
cranpose_macros::include_wgsl!("nv12_to_rgb.wgsl").into(),
),
});
let texture = |binding| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
};
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("nv12 to rgb"),
entries: &[
texture(0),
texture(1),
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(UNIFORM_SIZE as u64),
},
count: None,
},
],
});
let pipeline = cover_pipeline(
device,
"nv12 to rgb",
&layout,
&module,
"convert",
wgpu::TextureFormat::Rgba8Unorm,
);
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("nv12 to rgb"),
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
});
let uniforms = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("nv12 to rgb"),
size: UNIFORM_SIZE as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let frames = ExternalFrames::new(
gpu,
wgpu::TextureFormat::Rgba8Unorm,
wgpu::TextureUsages::RENDER_ATTACHMENT,
true,
);
Some(Self {
gpu: gpu.clone(),
metal,
writer: frames.writer(),
frames,
size: (0, 0),
pipeline,
layout,
sampler,
uniforms,
matrix: None,
})
}
pub(crate) fn convert(&mut self, frame: VideoFrame) -> Option<ImageBitmap> {
let size = frame.size();
if size != self.size {
self.frames.set_size(size.0, size.1);
self.size = size;
}
let buffer = unsafe { &*frame.as_pixel_buffer_ptr().cast::<CVPixelBuffer>() };
if CVPixelBufferGetPlaneCount(buffer) != PLANES.len() {
log::warn!("player surface: a picture that is not NV12");
return None;
}
let surface = CVPixelBufferGetIOSurface(Some(buffer))?;
let conversion = Conversion::of(buffer, size.1, &mut self.matrix);
let target = self.writer.begin()?;
let frame = Arc::new(frame);
let planes = PLANES.each_ref().map(|plane| {
let width = CVPixelBufferGetWidthOfPlane(buffer, plane.index);
let height = CVPixelBufferGetHeightOfPlane(buffer, plane.index);
self.import(&surface, plane, (width, height), &frame)
});
let [Some(luma), Some(chroma)] = planes else {
return None;
};
let views = [&luma, &chroma].map(|texture| texture.create_view(&Default::default()));
let device = &self.gpu.device;
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("nv12 to rgb"),
layout: &self.layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&views[0]),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&views[1]),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
wgpu::BindGroupEntry {
binding: 3,
resource: self.uniforms.as_entire_binding(),
},
],
});
self.gpu
.queue
.write_buffer(&self.uniforms, 0, &conversion.to_bytes());
let mut commands = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("nv12 to rgb"),
});
{
let mut pass = commands.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("nv12 to rgb"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: target.view(),
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
})],
..Default::default()
});
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, &bind_group, &[]);
pass.draw(0..3, 0..1);
}
self.gpu.queue.submit([commands.finish()]);
self.writer.publish(target.number());
self.frames.next_frame()
}
fn import(
&self,
surface: &IOSurfaceRef,
plane: &Plane,
size: (usize, usize),
frame: &Arc<VideoFrame>,
) -> Option<wgpu::Texture> {
let (width, height) = (u32::try_from(size.0).ok()?, u32::try_from(size.1).ok()?);
let descriptor = unsafe {
MTLTextureDescriptor::texture2DDescriptorWithPixelFormat_width_height_mipmapped(
plane.metal,
size.0,
size.1,
false,
)
};
descriptor.setUsage(MTLTextureUsage::ShaderRead);
let texture = self.metal.newTextureWithDescriptor_iosurface_plane(
&descriptor,
surface,
plane.index,
)?;
let keep = Arc::clone(frame);
unsafe {
let hal = wgpu::hal::metal::Device::texture_from_raw(
texture,
plane.wgpu,
MTLTextureType::Type2D,
1,
1,
wgpu::hal::CopyExtent {
width,
height,
depth: 1,
},
Some(Box::new(move || drop(keep))),
);
Some(
self.gpu
.device
.create_texture_from_hal::<wgpu::hal::api::Metal>(
hal,
&wgpu::TextureDescriptor {
label: Some("decoded plane"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: plane.wgpu,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
},
wgpu::TextureUses::RESOURCE,
),
)
}
}
}
struct Conversion {
range: [f32; 4],
weights: [f32; 4],
}
impl Conversion {
fn of(
buffer: &CVPixelBuffer,
height: u32,
cache: &mut Option<(CFRetained<CFType>, Option<Matrix>)>,
) -> Self {
let full = CVPixelBufferGetPixelFormatType(buffer)
== kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
let matrix = matrix_of(buffer, cache).unwrap_or(if height >= 720 {
Matrix::Bt709
} else {
Matrix::Bt601
});
let (kr, kb) = match matrix {
Matrix::Bt601 => (0.299, 0.114),
Matrix::Bt709 => (0.2126, 0.0722),
Matrix::Bt2020 => (0.2627, 0.0593),
};
let kg = 1.0 - kr - kb;
let (red, blue) = (2.0 * (1.0 - kr), 2.0 * (1.0 - kb));
Self {
range: if full {
[0.0, 1.0, 1.0, 0.0]
} else {
[16.0 / 255.0, 255.0 / 219.0, 255.0 / 224.0, 0.0]
},
weights: [red, -kb * blue / kg, -kr * red / kg, blue],
}
}
fn to_bytes(&self) -> [u8; UNIFORM_SIZE] {
let mut bytes = [0; UNIFORM_SIZE];
let (words, _) = bytes.as_chunks_mut::<4>();
for (word, value) in words
.iter_mut()
.zip(self.range.into_iter().chain(self.weights))
{
*word = value.to_ne_bytes();
}
bytes
}
}
#[derive(Clone, Copy)]
enum Matrix {
Bt601,
Bt709,
Bt2020,
}
impl Matrix {
fn named(attachment: &CFType) -> Option<Self> {
let name = attachment.downcast_ref::<CFString>()?.to_string();
match name.as_str() {
"ITU_R_709_2" => Some(Self::Bt709),
"ITU_R_601_4" => Some(Self::Bt601),
"ITU_R_2020" => Some(Self::Bt2020),
other => match other.strip_prefix("YCbCrMatrix#")?.parse::<u8>().ok()? {
1 => Some(Self::Bt709),
5 | 6 => Some(Self::Bt601),
9 | 10 => Some(Self::Bt2020),
_ => None,
},
}
}
}
fn matrix_of(
buffer: &CVPixelBuffer,
cache: &mut Option<(CFRetained<CFType>, Option<Matrix>)>,
) -> Option<Matrix> {
let attachment: CFRetained<CFType> =
unsafe { buffer.attachment(kCVImageBufferYCbCrMatrixKey, ptr::null_mut()) }?;
if let Some((known, matrix)) = cache
&& **known == *attachment
{
return *matrix;
}
let matrix = Matrix::named(&attachment);
*cache = Some((attachment, matrix));
matrix
}