use std::cell::Cell;
use alpha2_media::{BitDepth, ChromaLocation, ColorMatrix, PixelLayout, PlaneLayout, YuvEncoding};
use crate::{PlaneTarget, ReadbackFailure, RenderError};
pub(crate) struct Picture<'a, R> {
pub width: usize,
pub height: usize,
pub row: &'a R,
}
#[derive(Copy, Clone)]
struct Coding {
kr: f32,
kg: f32,
kb: f32,
luma_black: f32,
luma_span: f32,
chroma_center: f32,
chroma_span: f32,
maximum: f32,
}
impl Coding {
fn new(encoding: YuvEncoding, bit_depth: BitDepth) -> Self {
let (kr, kb) = encoding
.color
.matrix
.luma_weights()
.or(ColorMatrix::Bt709.luma_weights())
.expect("BT.709 is a YCbCr matrix");
let levels = encoding.color.range.levels(bit_depth.bits());
Self {
kr,
kg: 1.0 - kr - kb,
kb,
luma_black: levels.luma_black,
luma_span: levels.luma_span,
chroma_center: levels.chroma_center,
chroma_span: levels.chroma_span,
maximum: levels.maximum,
}
}
fn luma(self, rgb: [f32; 3]) -> f32 {
rgb[0] * self.kr + rgb[1] * self.kg + rgb[2] * self.kb
}
fn y(self, rgb: [f32; 3]) -> u16 {
self.code(self.luma_black + self.luma_span * self.luma(rgb))
}
fn uv(self, rgb: [f32; 3]) -> [u16; 2] {
let luma = self.luma(rgb);
[
self.code(
self.chroma_center + self.chroma_span * (rgb[2] - luma) / (2.0 * (1.0 - self.kb)),
),
self.code(
self.chroma_center + self.chroma_span * (rgb[0] - luma) / (2.0 * (1.0 - self.kr)),
),
]
}
fn alpha(self, alpha: f32) -> u16 {
self.code(alpha.clamp(0.0, 1.0) * self.maximum)
}
fn neutral(self) -> [u16; 2] {
let center = self.code(self.chroma_center);
[center, center]
}
fn code(self, value: f32) -> u16 {
const ROUND: f32 = 8_388_608.0;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let code = ((value.clamp(0.0, self.maximum) + ROUND) - ROUND) as u16;
code
}
}
pub(crate) const GPU_PARAMS_WORDS: usize = 16;
pub(crate) fn gpu_params(encoding: YuvEncoding, layout: PixelLayout) -> [u32; GPU_PARAMS_WORDS] {
let coding = Coding::new(encoding, layout.bit_depth);
let (step_x, step_y) = layout.chroma.subsampling();
let (cosited_x, cosited_y) = siting(encoding.chroma);
let code_shift = match (layout.bit_depth, layout.planes) {
(BitDepth::Ten, PlaneLayout::P010) => P010_SHIFT,
_ => 0,
};
let floats = [
coding.kr,
coding.kg,
coding.kb,
coding.maximum,
coding.luma_black,
coding.luma_span,
coding.chroma_center,
coding.chroma_span,
];
let words = [
step_x,
step_y,
u32::from(cosited_x),
u32::from(cosited_y),
u32::from(layout.alpha),
code_shift,
0,
0,
];
let mut params = [0; GPU_PARAMS_WORDS];
for (slot, value) in params
.iter_mut()
.zip(floats.map(f32::to_bits).into_iter().chain(words))
{
*slot = value;
}
params
}
fn tap_weight(offset: i64, cosited: bool) -> f32 {
if cosited {
if offset == 0 { 0.5 } else { 0.25 }
} else if offset >= 0 {
0.5
} else {
0.0
}
}
fn color<const ALPHA: bool>(texel: [f32; 4]) -> [f32; 3] {
let rgb = [texel[0], texel[1], texel[2]];
if !ALPHA {
return rgb.map(|value| value.clamp(0.0, 1.0));
}
let a = texel[3].clamp(0.0, 1.0);
if a <= 0.0 {
return [0.0; 3];
}
rgb.map(|value| (value / a).clamp(0.0, 1.0))
}
trait Store {
const BYTES: usize;
fn write(sample: &mut [u8], code: u16);
}
struct Byte;
impl Store for Byte {
const BYTES: usize = 1;
fn write(sample: &mut [u8], code: u16) {
#[expect(
clippy::cast_possible_truncation,
reason = "8-bit codes are at most 255"
)]
{
sample[0] = code as u8;
}
}
}
struct Word;
impl Store for Word {
const BYTES: usize = 2;
fn write(sample: &mut [u8], code: u16) {
sample.copy_from_slice(&code.to_le_bytes());
}
}
struct HighWord;
const P010_SHIFT: u32 = 6;
impl Store for HighWord {
const BYTES: usize = 2;
fn write(sample: &mut [u8], code: u16) {
sample.copy_from_slice(&(code << P010_SHIFT).to_le_bytes());
}
}
struct Band<'a> {
first: usize,
planes: Vec<Vec<&'a mut [u8]>>,
}
pub(crate) trait Bands: Sync {
fn rows_per_band(&self, chroma_rows: usize, row_pixels: usize) -> usize;
fn run(&self, count: usize, job: &(dyn Fn(usize) + Sync));
}
pub(crate) fn pack_with<R: Fn(usize, &mut [[f32; 4]]) + Sync>(
picture: &Picture<'_, R>,
encoding: YuvEncoding,
layout: PixelLayout,
targets: &mut [PlaneTarget<'_>],
bands: &dyn Bands,
) -> Result<(), RenderError> {
crate::color::check_packable(encoding)?;
let refuse = |failure| Err(RenderError::TextureReadback(failure));
let (width, height) = (picture.width, picture.height);
let (step_x, step_y) = layout.chroma.subsampling();
let (step_x, step_y) = (step_x as usize, step_y as usize);
if !width.is_multiple_of(step_x) || !height.is_multiple_of(step_y) || width == 0 || height == 0
{
return refuse(ReadbackFailure::UnsupportedTarget);
}
let planes = layout.planes(
u32::try_from(width).unwrap_or(u32::MAX),
u32::try_from(height).unwrap_or(u32::MAX),
);
if planes.len() != targets.len() {
return refuse(ReadbackFailure::Layout);
}
for (target, &(plane_width, rows, bytes)) in targets.iter().zip(&planes) {
target.fits((plane_width * bytes) as usize, rows as usize)?;
}
let chroma_rows = height / step_y;
let per_band = bands
.rows_per_band(chroma_rows, width * step_y)
.clamp(1, chroma_rows);
let count = chroma_rows.div_ceil(per_band);
let mut split: Vec<Band<'_>> = (0..count)
.map(|band| Band {
first: band * per_band,
planes: Vec::new(),
})
.collect();
for (target, &(_, rows, _)) in targets.iter_mut().zip(&planes) {
let mut rows_left = target.bytes.chunks_mut(target.stride).take(rows as usize);
let ratio = rows as usize / chroma_rows;
for band in &mut split {
let count = per_band.min(chroma_rows.saturating_sub(band.first)) * ratio;
band.planes.push(rows_left.by_ref().take(count).collect());
}
}
let coding = Coding::new(encoding, layout.bit_depth);
let taps = [
Taps::new(step_x, siting(encoding.chroma).0),
Taps::new(step_y, siting(encoding.chroma).1),
];
let split = split
.into_iter()
.map(|band| std::sync::Mutex::new(Some(band)))
.collect::<Vec<_>>();
bands.run(count, &|index| {
let band = split[index]
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take();
if let Some(band) = band {
pack_layout_band(picture, coding, taps, layout, band);
}
});
Ok(())
}
fn pack_layout_band<R: Fn(usize, &mut [[f32; 4]]) + Sync>(
picture: &Picture<'_, R>,
coding: Coding,
taps: [Taps; 2],
layout: PixelLayout,
band: Band<'_>,
) {
let (step_x, step_y) = layout.chroma.subsampling();
let steps = (step_x as usize, step_y as usize);
let pack = match (layout.bit_depth, layout.planes, layout.alpha) {
(BitDepth::Eight, PlaneLayout::Planar, false) => pack_band::<R, Byte, false, false>,
(BitDepth::Eight, PlaneLayout::Planar, true) => pack_band::<R, Byte, true, false>,
(BitDepth::Eight, PlaneLayout::P010, false) => pack_band::<R, Byte, false, true>,
(BitDepth::Eight, PlaneLayout::P010, true) => pack_band::<R, Byte, true, true>,
(BitDepth::Ten, PlaneLayout::Planar, false) => pack_band::<R, Word, false, false>,
(BitDepth::Ten, PlaneLayout::Planar, true) => pack_band::<R, Word, true, false>,
(BitDepth::Ten, PlaneLayout::P010, false) => pack_band::<R, HighWord, false, true>,
(BitDepth::Ten, PlaneLayout::P010, true) => pack_band::<R, HighWord, true, true>,
};
pack(picture, coding, taps, steps, band);
}
const fn siting(location: ChromaLocation) -> (bool, bool) {
match location {
ChromaLocation::Left => (true, false),
ChromaLocation::TopLeft => (true, true),
ChromaLocation::Unspecified | ChromaLocation::Center => (false, false),
}
}
#[derive(Clone, Copy)]
struct Taps {
taps: [(i64, f32); 3],
len: usize,
}
impl Taps {
fn new(step: usize, cosited: bool) -> Self {
let mut taps = [(0, 0.0); 3];
if step == 1 {
taps[0] = (0, 1.0);
return Self { taps, len: 1 };
}
let mut len = 0;
for offset in -1..=1_i64 {
let weight = tap_weight(offset, cosited);
if weight != 0.0 {
taps[len] = (offset, weight);
len += 1;
}
}
Self { taps, len }
}
fn all(&self) -> &[(i64, f32)] {
&self.taps[..self.len]
}
}
std::thread_local! {
static LINES: Cell<Vec<Vec<[f32; 4]>>> = const { Cell::new(Vec::new()) };
}
struct Lines {
held: Vec<Option<usize>>,
lines: Vec<Vec<[f32; 4]>>,
}
impl Lines {
fn new(count: usize, width: usize) -> Self {
let mut lines = LINES.take();
lines.resize_with(count, Vec::new);
for line in &mut lines {
line.resize(width, [0.0; 4]);
}
Self {
held: vec![None; count],
lines,
}
}
fn fetch<R: Fn(usize, &mut [[f32; 4]]) + Sync>(
&mut self,
picture: &Picture<'_, R>,
rows: &[Option<usize>],
) -> [usize; 3] {
let mut slots = [usize::MAX; 3];
let mut used = [false; 3];
for (slot, row) in slots.iter_mut().zip(rows) {
let Some(row) = row else { continue };
if let Some(held) = self.held.iter().position(|held| *held == Some(*row)) {
*slot = held;
used[held] = true;
}
}
for index in 0..rows.len().min(3) {
let Some(row) = rows[index] else { continue };
if slots[index] != usize::MAX {
continue;
}
if let Some(earlier) = (0..index).find(|&earlier| rows[earlier] == Some(row)) {
slots[index] = slots[earlier];
continue;
}
let free = (0..self.lines.len())
.find(|slot| !used[*slot])
.expect("a free line for every needed row");
(picture.row)(row, &mut self.lines[free]);
self.held[free] = Some(row);
used[free] = true;
slots[index] = free;
}
slots
}
}
impl Drop for Lines {
fn drop(&mut self) {
LINES.set(std::mem::take(&mut self.lines));
}
}
fn pack_band<R, S, const ALPHA: bool, const INTERLEAVED: bool>(
picture: &Picture<'_, R>,
coding: Coding,
[columns, rows]: [Taps; 2],
(step_x, step_y): (usize, usize),
band: Band<'_>,
) where
R: Fn(usize, &mut [[f32; 4]]) + Sync,
S: Store,
{
let width = picture.width;
let height = picture.height;
let chroma_width = width / step_x;
let Band { first, planes } = band;
let mut planes = planes.into_iter();
let mut luma = planes.next().unwrap_or_default();
let mut chroma_planes: Vec<Vec<&mut [u8]>> = if INTERLEAVED {
vec![planes.next().unwrap_or_default()]
} else {
vec![
planes.next().unwrap_or_default(),
planes.next().unwrap_or_default(),
]
};
let mut alpha_rows = if ALPHA {
planes.next().unwrap_or_default()
} else {
Vec::new()
};
let chroma_rows = chroma_planes[0].len();
let above = rows.all().iter().any(|&(offset, _)| offset < 0);
let mut window = Lines::new(step_y + 1, width);
for local in 0..chroma_rows {
let top = (first + local) * step_y;
let mut needed = [None; 3];
for (offset, row) in needed.iter_mut().enumerate().take(step_y + 1) {
if offset > 0 || above {
*row = Some((top + offset).saturating_sub(1).min(height - 1));
}
}
let slots = window.fetch(picture, &needed);
let line = |offset: usize| window.lines[slots[offset]].as_slice();
for inner in 0..step_y {
let line = line(inner + 1);
write_row::<S>(luma[local * step_y + inner], line, |texel| {
coding.y(color::<ALPHA>(texel))
});
if ALPHA {
write_row::<S>(alpha_rows[local * step_y + inner], line, |texel| {
coding.alpha(texel[3])
});
}
}
let lines = [0, 1, 2].map(|offset| {
if slots[offset] == usize::MAX {
&[][..]
} else {
line(offset)
}
});
if step_x == 1 && step_y == 1 {
write_chroma::<S, INTERLEAVED>(&mut chroma_planes, local, chroma_width, |column| {
coding.uv(color::<ALPHA>(lines[1][column]))
});
} else {
write_chroma::<S, INTERLEAVED>(&mut chroma_planes, local, chroma_width, |column| {
subsampled_chroma::<ALPHA>(&lines, column, step_x, (&columns, &rows), coding, width)
});
}
}
}
fn write_row<S: Store>(out: &mut [u8], line: &[[f32; 4]], code: impl Fn([f32; 4]) -> u16) {
for (sample, texel) in out.chunks_exact_mut(S::BYTES).zip(line) {
S::write(sample, code(*texel));
}
}
fn write_chroma<S: Store, const INTERLEAVED: bool>(
planes: &mut [Vec<&mut [u8]>],
local: usize,
chroma_width: usize,
sample: impl Fn(usize) -> [u16; 2],
) {
if INTERLEAVED {
let pairs = planes[0][local].chunks_exact_mut(2 * S::BYTES);
for (column, pair) in pairs.take(chroma_width).enumerate() {
let [cb, cr] = sample(column);
let (first, second) = pair.split_at_mut(S::BYTES);
S::write(first, cb);
S::write(second, cr);
}
return;
}
let (u, v) = planes.split_at_mut(1);
let samples = u[0][local]
.chunks_exact_mut(S::BYTES)
.zip(v[0][local].chunks_exact_mut(S::BYTES));
for (column, (u, v)) in samples.take(chroma_width).enumerate() {
let [cb, cr] = sample(column);
S::write(u, cb);
S::write(v, cr);
}
}
fn subsampled_chroma<const ALPHA: bool>(
lines: &[&[[f32; 4]]; 3],
column: usize,
step_x: usize,
(columns, rows): (&Taps, &Taps),
coding: Coding,
width: usize,
) -> [u16; 2] {
let x = column * step_x;
let mut sum = [0.0_f32; 3];
let mut weight_sum = 0.0_f32;
for &(dy, row_weight) in rows.all() {
let line = lines[usize::try_from(1 + dy).unwrap_or(0)];
for &(dx, column_weight) in columns.all() {
let at = usize::try_from(i64::try_from(x).unwrap_or(0) + dx)
.unwrap_or(0)
.min(width - 1);
let texel = line[at];
let weight = row_weight * column_weight;
if ALPHA {
let a = texel[3].clamp(0.0, 1.0) * weight;
for (channel, value) in sum.iter_mut().enumerate() {
*value += texel[channel].clamp(0.0, 1.0) * weight;
}
weight_sum += a;
} else {
let rgb = color::<false>(texel);
for (channel, value) in sum.iter_mut().enumerate() {
*value += rgb[channel] * weight;
}
}
}
}
if !ALPHA {
return coding.uv(sum);
}
if weight_sum <= 0.0 {
return coding.neutral();
}
coding.uv(sum.map(|value| (value / weight_sum).clamp(0.0, 1.0)))
}
#[cfg(test)]
#[path = "layout_pack_tests.rs"]
mod tests;