use std::mem::size_of;
use super::sharpyuv_dsp::{sharp_yuv_filter_row, sharp_yuv_update_rgb, sharp_yuv_update_y};
use super::sharpyuv_gamma::GammaContext;
pub trait SampleAccess {
fn read(&self, channel: usize, row: usize, column: usize, bit_depth: i32) -> u16;
fn write(&mut self, plane: usize, row: usize, column: usize, value: u16, bit_depth: i32);
}
pub struct Scratch<'a> {
pub tmp_buffer: &'a mut [u16],
pub best_y_base: &'a mut [u16],
pub target_y_base: &'a mut [u16],
pub best_rgb_y: &'a mut [u16],
pub best_uv_base: &'a mut [i16],
pub target_uv_base: &'a mut [i16],
pub best_rgb_uv: &'a mut [i16],
}
#[derive(Clone, Copy)]
pub struct Dsp {
pub update_y: UpdateY,
pub update_rgb: fn(&[i16], &[i16], &mut [i16], i32),
pub filter_row: FilterRow,
}
pub type UpdateY = fn(&[u16], &[u16], &mut [u16], i32, i32) -> u64;
pub type FilterRow = fn(&[i16], &[i16], i32, &[u16], &mut [u16], i32);
impl Dsp {
pub const SCALAR: Self = Self {
update_y: sharp_yuv_update_y,
update_rgb: sharp_yuv_update_rgb,
filter_row: sharp_yuv_filter_row,
};
}
pub const SHARPYUV_VERSION_MAJOR: i32 = 0;
pub const SHARPYUV_VERSION_MINOR: i32 = 4;
pub const SHARPYUV_VERSION_PATCH: i32 = 2;
#[must_use]
pub const fn sharpyuv_make_version(major: i32, minor: i32, patch: i32) -> i32 {
(major << 24) | (minor << 16) | patch
}
pub const SHARPYUV_VERSION: i32 = sharpyuv_make_version(
SHARPYUV_VERSION_MAJOR,
SHARPYUV_VERSION_MINOR,
SHARPYUV_VERSION_PATCH,
);
#[must_use]
pub const fn sharp_yuv_get_version() -> i32 {
SHARPYUV_VERSION
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SharpYuvConversionMatrix {
pub rgb_to_y: [i32; 4],
pub rgb_to_u: [i32; 4],
pub rgb_to_v: [i32; 4],
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SharpYuvTransferFunctionType(pub u32);
impl SharpYuvTransferFunctionType {
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt709: Self = Self(1);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt470M: Self = Self(4);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt470Bg: Self = Self(5);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt601: Self = Self(6);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionSmpte240: Self = Self(7);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionLinear: Self = Self(8);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionLog100: Self = Self(9);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionLog100_Sqrt10: Self = Self(10);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionIec61966: Self = Self(11);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt1361: Self = Self(12);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionSrgb: Self = Self(13);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt2020_10Bit: Self = Self(14);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionBt2020_12Bit: Self = Self(15);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionSmpte2084: Self = Self(16);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionSmpte428: Self = Self(17);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionHlg: Self = Self(18);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvTransferFunctionNum: Self = Self(19);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SharpYuvOptions<'a> {
pub yuv_matrix: &'a SharpYuvConversionMatrix,
pub transfer_type: SharpYuvTransferFunctionType,
}
const NUM_ITERATIONS: u32 = 4;
const YUV_FIX: i32 = 16;
const YUV_HALF: i32 = 1 << (YUV_FIX - 1);
const MAX_BIT_DEPTH: i32 = 14;
fn get_precision_shift(rgb_bit_depth: i32) -> i32 {
if rgb_bit_depth + 2 <= MAX_BIT_DEPTH {
2
} else {
MAX_BIT_DEPTH - rgb_bit_depth
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn clip_8b(v: i16) -> u8 {
if i32::from(v) & !0xff == 0 {
v as u8
} else if v < 0 {
0
} else {
255
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn clip(v: i16, max: i32) -> u16 {
if v < 0 {
0
} else if i32::from(v) > max {
max as u16
} else {
v as u16
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn clip_bit_depth(y: i32, bit_depth: i32) -> u16 {
let max = (1 << bit_depth) - 1;
if y & !max == 0 {
y as u16
} else if y < 0 {
0
} else {
max as u16
}
}
#[expect(
clippy::cast_possible_truncation,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn rgb_to_gray(r: i64, g: i64, b: i64) -> i32 {
let luma = 13933 * r + 46871 * g + 4732 * b + i64::from(YUV_HALF);
(luma >> YUV_FIX) as i32
}
fn scale_down(a: u16, b: u16, c: u16, d: u16, rgb_bit_depth: i32, gamma: &GammaContext) -> u32 {
let bit_depth = rgb_bit_depth + get_precision_shift(rgb_bit_depth);
let a_lin = gamma.to_linear(a, bit_depth);
let b_lin = gamma.to_linear(b, bit_depth);
let c_lin = gamma.to_linear(c, bit_depth);
let d_lin = gamma.to_linear(d, bit_depth);
u32::from(gamma.to_gamma((a_lin + b_lin + c_lin + d_lin + 2) >> 2, bit_depth))
}
#[expect(
clippy::cast_sign_loss,
clippy::many_single_char_names,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn update_w(src: &[u16], dst: &mut [u16], w: usize, rgb_bit_depth: i32, gamma: &GammaContext) {
let bit_depth = rgb_bit_depth + get_precision_shift(rgb_bit_depth);
for (i, out) in dst[..w].iter_mut().enumerate() {
let r = gamma.to_linear(src[i], bit_depth);
let g = gamma.to_linear(src[w + i], bit_depth);
let b = gamma.to_linear(src[2 * w + i], bit_depth);
let y = rgb_to_gray(i64::from(r), i64::from(g), i64::from(b));
*out = gamma.to_gamma(y as u32, bit_depth);
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn update_chroma(
src1: &[u16],
src2: &[u16],
dst: &mut [i16],
uv_w: usize,
rgb_bit_depth: i32,
gamma: &GammaContext,
) {
for i in 0..uv_w {
let base = 2 * i;
let r = scale_down(
src1[base],
src1[base + 1],
src2[base],
src2[base + 1],
rgb_bit_depth,
gamma,
) as i32;
let g = scale_down(
src1[base + 2 * uv_w],
src1[base + 2 * uv_w + 1],
src2[base + 2 * uv_w],
src2[base + 2 * uv_w + 1],
rgb_bit_depth,
gamma,
) as i32;
let b = scale_down(
src1[base + 4 * uv_w],
src1[base + 4 * uv_w + 1],
src2[base + 4 * uv_w],
src2[base + 4 * uv_w + 1],
rgb_bit_depth,
gamma,
) as i32;
let w_gray = rgb_to_gray(i64::from(r), i64::from(g), i64::from(b));
dst[i] = (r - w_gray) as i16;
dst[i + uv_w] = (g - w_gray) as i16;
dst[i + 2 * uv_w] = (b - w_gray) as i16;
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn store_gray(rgb: &[u16], y: &mut [u16], w: usize) {
for (i, out) in y[..w].iter_mut().enumerate() {
*out = rgb_to_gray(
i64::from(rgb[i]),
i64::from(rgb[w + i]),
i64::from(rgb[2 * w + i]),
) as u16;
}
}
fn filter2(a: i32, b: i32, w0: i32, bit_depth: i32) -> u16 {
let v0 = (a * 3 + b + 2) >> 2;
clip_bit_depth(v0 + w0, bit_depth)
}
fn shift(v: i32, shift_amount: i32) -> i32 {
if shift_amount >= 0 {
v << shift_amount
} else {
v >> -shift_amount
}
}
#[expect(
clippy::cast_possible_wrap,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn sample_index(origin: usize, count: usize, step: i32) -> usize {
usize::try_from(origin as isize + count as isize * step as isize)
.expect("sample address before the start of the buffer")
}
fn read_sample(buf: &[u8], index: usize, bit_depth: i32) -> u16 {
if bit_depth == 8 {
u16::from(buf[index])
} else {
u16::from_ne_bytes([buf[index], buf[index + 1]])
}
}
#[expect(
clippy::cast_possible_truncation,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn write_sample(buf: &mut [u8], index: usize, value: u16, bit_depth: i32) {
if bit_depth <= 8 {
buf[index] = value as u8;
} else {
buf[index..index + 2].copy_from_slice(&value.to_ne_bytes());
}
}
struct SliceAccess<'a> {
input: [(&'a [u8], usize); 3],
output: [(&'a mut [u8], usize, i32); 3],
rgb_step: i32,
rgb_stride: i32,
}
impl SampleAccess for SliceAccess<'_> {
fn read(&self, channel: usize, row: usize, column: usize, bit_depth: i32) -> u16 {
let (bytes, origin) = self.input[channel];
let index = sample_index(
sample_index(origin, row, self.rgb_stride),
column,
self.rgb_step,
);
read_sample(bytes, index, bit_depth)
}
fn write(&mut self, plane: usize, row: usize, column: usize, value: u16, bit_depth: i32) {
let (bytes, origin, stride) = &mut self.output[plane];
let unit = if bit_depth <= 8 { 1 } else { 2 };
let index = sample_index(*origin, row, *stride) + column * unit;
write_sample(bytes, index, value, bit_depth);
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn import_one_row(
access: &impl SampleAccess,
row: usize,
rgb_bit_depth: i32,
pic_width: usize,
dst: &mut [u16],
w: usize,
) {
let shift_amount = get_precision_shift(rgb_bit_depth);
for i in 0..pic_width {
let r = access.read(0, row, i, rgb_bit_depth);
let g = access.read(1, row, i, rgb_bit_depth);
let b = access.read(2, row, i, rgb_bit_depth);
dst[i] = shift(i32::from(r), shift_amount) as u16;
dst[i + w] = shift(i32::from(g), shift_amount) as u16;
dst[i + 2 * w] = shift(i32::from(b), shift_amount) as u16;
}
if pic_width & 1 == 1 {
dst[pic_width] = dst[pic_width - 1];
dst[pic_width + w] = dst[pic_width + w - 1];
dst[pic_width + 2 * w] = dst[pic_width + 2 * w - 1];
}
}
#[expect(clippy::too_many_arguments, reason = "mirrors the C function")]
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn interpolate_two_rows(
best_y: &[u16],
prev_uv: &[i16],
cur_uv: &[i16],
next_uv: &[i16],
w: usize,
out1: &mut [u16],
out2: &mut [u16],
rgb_bit_depth: i32,
dsp: Dsp,
) {
let uv_w = w >> 1;
let len = (w - 1) >> 1; let bit_depth = rgb_bit_depth + get_precision_shift(rgb_bit_depth);
for k in 0..3 {
let p = k * uv_w;
let o = k * w;
out1[o] = filter2(
i32::from(cur_uv[p]),
i32::from(prev_uv[p]),
i32::from(best_y[0]),
bit_depth,
);
out2[o] = filter2(
i32::from(cur_uv[p]),
i32::from(next_uv[p]),
i32::from(best_y[w]),
bit_depth,
);
(dsp.filter_row)(
&cur_uv[p..],
&prev_uv[p..],
len as i32,
&best_y[1..],
&mut out1[o + 1..],
bit_depth,
);
(dsp.filter_row)(
&cur_uv[p..],
&next_uv[p..],
len as i32,
&best_y[w + 1..],
&mut out2[o + 1..],
bit_depth,
);
if w & 1 == 0 {
out1[o + w - 1] = filter2(
i32::from(cur_uv[p + uv_w - 1]),
i32::from(prev_uv[p + uv_w - 1]),
i32::from(best_y[w - 1]),
bit_depth,
);
out2[o + w - 1] = filter2(
i32::from(cur_uv[p + uv_w - 1]),
i32::from(next_uv[p + uv_w - 1]),
i32::from(best_y[w - 1 + w]),
bit_depth,
);
}
}
}
fn rgb_to_yuv_component(r: i32, g: i32, b: i32, coeffs: &[i32; 4], sfix: i32) -> i32 {
let srounder = 1 << (YUV_FIX + sfix - 1);
let luma = coeffs[0] * r + coeffs[1] * g + coeffs[2] * b + coeffs[3] + srounder;
luma >> (YUV_FIX + sfix)
}
#[expect(clippy::too_many_arguments, reason = "mirrors the C function")]
#[expect(
clippy::cast_possible_truncation,
clippy::many_single_char_names,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn convert_wrgb_to_yuv(
best_y: &[u16],
best_uv: &[i16],
access: &mut impl SampleAccess,
rgb_bit_depth: i32,
yuv_bit_depth: i32,
width: usize,
height: usize,
yuv_matrix: &SharpYuvConversionMatrix,
) -> bool {
let w = (width + 1) & !1;
let h = (height + 1) & !1;
let uv_w = w >> 1;
let uv_h = h >> 1;
let sfix = get_precision_shift(rgb_bit_depth);
let yuv_max = (1 << yuv_bit_depth) - 1;
let mut best_uv_index = 0_usize;
for j in 0..height {
for i in 0..width {
let off = i >> 1;
let w_value = i32::from(best_y[j * w + i]);
let r = i32::from(best_uv[best_uv_index + off]) + w_value;
let g = i32::from(best_uv[best_uv_index + off + uv_w]) + w_value;
let b = i32::from(best_uv[best_uv_index + off + 2 * uv_w]) + w_value;
let y = rgb_to_yuv_component(r, g, b, &yuv_matrix.rgb_to_y, sfix);
let value = if yuv_bit_depth <= 8 {
u16::from(clip_8b(y as i16))
} else {
clip(y as i16, yuv_max)
};
access.write(0, j, i, value, yuv_bit_depth);
}
best_uv_index += (j & 1) * 3 * uv_w;
}
for j in 0..uv_h {
let base = j * 3 * uv_w;
for i in 0..uv_w {
let r = i32::from(best_uv[base + i]);
let g = i32::from(best_uv[base + i + uv_w]);
let b = i32::from(best_uv[base + i + 2 * uv_w]);
let u = rgb_to_yuv_component(r, g, b, &yuv_matrix.rgb_to_u, sfix);
let v = rgb_to_yuv_component(r, g, b, &yuv_matrix.rgb_to_v, sfix);
if yuv_bit_depth <= 8 {
access.write(1, j, i, u16::from(clip_8b(u as i16)), yuv_bit_depth);
access.write(2, j, i, u16::from(clip_8b(v as i16)), yuv_bit_depth);
} else {
access.write(1, j, i, clip(u as i16, yuv_max), yuv_bit_depth);
access.write(2, j, i, clip(v as i16, yuv_max), yuv_bit_depth);
}
}
}
true
}
fn safe_malloc<T: Default + Clone>(nmemb: u64) -> Option<Vec<T>> {
let total_size = nmemb.checked_mul(size_of::<T>() as u64)?;
usize::try_from(total_size).ok()?;
let count = usize::try_from(nmemb).ok()?;
let mut buffer = Vec::new();
buffer.try_reserve_exact(count).ok()?;
buffer.resize(count, T::default());
Some(buffer)
}
#[expect(clippy::too_many_arguments, reason = "mirrors the C function")]
#[expect(clippy::too_many_lines, reason = "mirrors the C function")]
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_precision_loss,
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn do_sharp_argb_to_yuv(
access: &mut impl SampleAccess,
scratch: Scratch<'_>,
dsp: Dsp,
rgb_bit_depth: i32,
yuv_bit_depth: i32,
width: usize,
height: usize,
yuv_matrix: &SharpYuvConversionMatrix,
transfer_type: SharpYuvTransferFunctionType,
) -> bool {
let w = (width + 1) & !1;
let h = (height + 1) & !1;
let uv_w = w >> 1;
let uv_h = h >> 1;
let y_bit_depth = rgb_bit_depth + get_precision_shift(rgb_bit_depth);
let gamma = GammaContext::new(transfer_type);
let mut prev_diff_y_sum: u64 = u64::MAX;
let Scratch {
tmp_buffer,
best_y_base,
target_y_base,
best_rgb_y,
best_uv_base,
target_uv_base,
best_rgb_uv,
} = scratch;
if tmp_buffer.len() < w * 6
|| best_y_base.len() < w * h
|| target_y_base.len() < w * h
|| best_rgb_y.len() < w * 2
|| best_uv_base.len() < uv_w * 3 * uv_h
|| target_uv_base.len() < uv_w * 3 * uv_h
|| best_rgb_uv.len() < uv_w * 3
{
return false;
}
let diff_y_threshold = (3.0_f64 * w as f64 * h as f64) as u64;
let mut best_y_index = 0_usize;
let mut target_y_index = 0_usize;
let mut best_uv_index = 0_usize;
let mut target_uv_index = 0_usize;
let mut j = 0_usize;
while j < height {
let is_last_row = j == height - 1;
let (src1, src2) = tmp_buffer.split_at_mut(3 * w);
import_one_row(access, j, rgb_bit_depth, width, src1, w);
if is_last_row {
src2.copy_from_slice(src1);
} else {
import_one_row(access, j + 1, rgb_bit_depth, width, src2, w);
}
store_gray(src1, &mut best_y_base[best_y_index..], w);
store_gray(src2, &mut best_y_base[best_y_index + w..], w);
update_w(
src1,
&mut target_y_base[target_y_index..],
w,
rgb_bit_depth,
&gamma,
);
update_w(
src2,
&mut target_y_base[target_y_index + w..],
w,
rgb_bit_depth,
&gamma,
);
update_chroma(
src1,
src2,
&mut target_uv_base[target_uv_index..],
uv_w,
rgb_bit_depth,
&gamma,
);
best_uv_base[best_uv_index..best_uv_index + 3 * uv_w]
.copy_from_slice(&target_uv_base[target_uv_index..target_uv_index + 3 * uv_w]);
best_y_index += 2 * w;
best_uv_index += 3 * uv_w;
target_y_index += 2 * w;
target_uv_index += 3 * uv_w;
j += 2;
}
for iter in 0..NUM_ITERATIONS {
let mut cur_uv_index = 0_usize;
let mut prev_uv_index = 0_usize;
let mut diff_y_sum: u64 = 0;
best_y_index = 0;
best_uv_index = 0;
target_y_index = 0;
target_uv_index = 0;
j = 0;
loop {
let next_uv_index = cur_uv_index + if j + 2 < h { 3 * uv_w } else { 0 };
{
let (src1, src2) = tmp_buffer.split_at_mut(3 * w);
interpolate_two_rows(
&best_y_base[best_y_index..],
&best_uv_base[prev_uv_index..],
&best_uv_base[cur_uv_index..],
&best_uv_base[next_uv_index..],
w,
src1,
src2,
rgb_bit_depth,
dsp,
);
prev_uv_index = cur_uv_index;
cur_uv_index = next_uv_index;
update_w(src1, &mut best_rgb_y[..w], w, rgb_bit_depth, &gamma);
update_w(src2, &mut best_rgb_y[w..2 * w], w, rgb_bit_depth, &gamma);
update_chroma(src1, src2, best_rgb_uv, uv_w, rgb_bit_depth, &gamma);
}
diff_y_sum += (dsp.update_y)(
&target_y_base[target_y_index..],
best_rgb_y,
&mut best_y_base[best_y_index..],
(2 * w) as i32,
y_bit_depth,
);
(dsp.update_rgb)(
&target_uv_base[target_uv_index..],
best_rgb_uv,
&mut best_uv_base[best_uv_index..],
(3 * uv_w) as i32,
);
best_y_index += 2 * w;
best_uv_index += 3 * uv_w;
target_y_index += 2 * w;
target_uv_index += 3 * uv_w;
j += 2;
if j >= h {
break;
}
}
if iter > 0 {
if diff_y_sum < diff_y_threshold {
break;
}
if diff_y_sum > prev_diff_y_sum {
break;
}
}
prev_diff_y_sum = diff_y_sum;
}
convert_wrgb_to_yuv(
best_y_base,
best_uv_base,
access,
rgb_bit_depth,
yuv_bit_depth,
width,
height,
yuv_matrix,
)
}
#[must_use]
pub fn sharp_yuv_options_init_internal(
yuv_matrix: &SharpYuvConversionMatrix,
version: i32,
) -> Option<SharpYuvOptions<'_>> {
let major = version >> 24;
let minor = (version >> 16) & 0xff;
if major != SHARPYUV_VERSION_MAJOR || minor != SHARPYUV_VERSION_MINOR {
return None;
}
Some(SharpYuvOptions {
yuv_matrix,
transfer_type: SharpYuvTransferFunctionType::kSharpYuvTransferFunctionSrgb,
})
}
#[must_use]
pub fn sharp_yuv_options_init(
yuv_matrix: &SharpYuvConversionMatrix,
) -> Option<SharpYuvOptions<'_>> {
sharp_yuv_options_init_internal(yuv_matrix, SHARPYUV_VERSION)
}
#[must_use]
pub fn scratch_lengths(width: usize, height: usize) -> [usize; 7] {
let w = (width + 1) & !1;
let h = (height + 1) & !1;
let uv_w = w / 2;
let uv_h = h / 2;
[
w * 6,
w * h,
w * h,
w * 2,
uv_w * 3 * uv_h,
uv_w * 3 * uv_h,
uv_w * 3,
]
}
#[must_use]
#[expect(
clippy::too_many_arguments,
reason = "mirrors the C validation boundary"
)]
pub fn valid_arguments(
rgb_step: i32,
rgb_stride: i32,
rgb_bit_depth: i32,
y_stride: i32,
u_stride: i32,
v_stride: i32,
yuv_bit_depth: i32,
width: i32,
height: i32,
) -> bool {
width >= 1
&& height >= 1
&& width != i32::MAX
&& height != i32::MAX
&& matches!(rgb_bit_depth, 8 | 10 | 12 | 16)
&& matches!(yuv_bit_depth, 8 | 10 | 12)
&& (rgb_bit_depth == 8 || (rgb_step % 2 == 0 && rgb_stride % 2 == 0))
&& (yuv_bit_depth == 8 || (y_stride % 2 == 0 && u_stride % 2 == 0 && v_stride % 2 == 0))
}
#[must_use]
#[expect(clippy::too_many_arguments, reason = "narrow adapter boundary")]
pub fn convert_with_access(
access: &mut impl SampleAccess,
scratch: Scratch<'_>,
dsp: Dsp,
rgb_bit_depth: i32,
yuv_bit_depth: i32,
width: usize,
height: usize,
yuv_matrix: &SharpYuvConversionMatrix,
transfer_type: SharpYuvTransferFunctionType,
) -> bool {
let rgb_max = (1 << rgb_bit_depth) - 1;
let rgb_round = 1 << (rgb_bit_depth - 1);
let yuv_max = (1 << yuv_bit_depth) - 1;
let sfix = get_precision_shift(rgb_bit_depth);
let mut scaled_matrix = *yuv_matrix;
if rgb_bit_depth != yuv_bit_depth {
for i in 0..3 {
scaled_matrix.rgb_to_y[i] = (yuv_matrix.rgb_to_y[i] * yuv_max + rgb_round) / rgb_max;
scaled_matrix.rgb_to_u[i] = (yuv_matrix.rgb_to_u[i] * yuv_max + rgb_round) / rgb_max;
scaled_matrix.rgb_to_v[i] = (yuv_matrix.rgb_to_v[i] * yuv_max + rgb_round) / rgb_max;
}
}
scaled_matrix.rgb_to_y[3] = shift(yuv_matrix.rgb_to_y[3], sfix);
scaled_matrix.rgb_to_u[3] = shift(yuv_matrix.rgb_to_u[3], sfix);
scaled_matrix.rgb_to_v[3] = shift(yuv_matrix.rgb_to_v[3], sfix);
do_sharp_argb_to_yuv(
access,
scratch,
dsp,
rgb_bit_depth,
yuv_bit_depth,
width,
height,
&scaled_matrix,
transfer_type,
)
}
#[must_use]
#[expect(clippy::too_many_arguments, reason = "mirrors the C signature")]
#[expect(
clippy::cast_sign_loss,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
pub fn sharp_yuv_convert_with_options_using(
r_ptr: &[u8],
r_origin: usize,
g_ptr: &[u8],
g_origin: usize,
b_ptr: &[u8],
b_origin: usize,
rgb_step: i32,
rgb_stride: i32,
rgb_bit_depth: i32,
y_ptr: &mut [u8],
y_origin: usize,
y_stride: i32,
u_ptr: &mut [u8],
u_origin: usize,
u_stride: i32,
v_ptr: &mut [u8],
v_origin: usize,
v_stride: i32,
yuv_bit_depth: i32,
width: i32,
height: i32,
options: &SharpYuvOptions<'_>,
dsp: Dsp,
) -> bool {
if !valid_arguments(
rgb_step,
rgb_stride,
rgb_bit_depth,
y_stride,
u_stride,
v_stride,
yuv_bit_depth,
width,
height,
) {
return false;
}
let lengths = scratch_lengths(width as usize, height as usize);
let Some(mut tmp_buffer) = safe_malloc::<u16>(lengths[0] as u64) else {
return false;
};
let Some(mut best_y_base) = safe_malloc::<u16>(lengths[1] as u64) else {
return false;
};
let Some(mut target_y_base) = safe_malloc::<u16>(lengths[2] as u64) else {
return false;
};
let Some(mut best_rgb_y) = safe_malloc::<u16>(lengths[3] as u64) else {
return false;
};
let Some(mut best_uv_base) = safe_malloc::<i16>(lengths[4] as u64) else {
return false;
};
let Some(mut target_uv_base) = safe_malloc::<i16>(lengths[5] as u64) else {
return false;
};
let Some(mut best_rgb_uv) = safe_malloc::<i16>(lengths[6] as u64) else {
return false;
};
let scratch = Scratch {
tmp_buffer: &mut tmp_buffer,
best_y_base: &mut best_y_base,
target_y_base: &mut target_y_base,
best_rgb_y: &mut best_rgb_y,
best_uv_base: &mut best_uv_base,
target_uv_base: &mut target_uv_base,
best_rgb_uv: &mut best_rgb_uv,
};
let mut access = SliceAccess {
input: [(r_ptr, r_origin), (g_ptr, g_origin), (b_ptr, b_origin)],
output: [
(y_ptr, y_origin, y_stride),
(u_ptr, u_origin, u_stride),
(v_ptr, v_origin, v_stride),
],
rgb_step,
rgb_stride,
};
convert_with_access(
&mut access,
scratch,
dsp,
rgb_bit_depth,
yuv_bit_depth,
width as usize,
height as usize,
options.yuv_matrix,
options.transfer_type,
)
}
#[must_use]
#[expect(clippy::too_many_arguments, reason = "mirrors the C signature")]
pub fn sharp_yuv_convert_with_options(
r_ptr: &[u8],
r_origin: usize,
g_ptr: &[u8],
g_origin: usize,
b_ptr: &[u8],
b_origin: usize,
rgb_step: i32,
rgb_stride: i32,
rgb_bit_depth: i32,
y_ptr: &mut [u8],
y_origin: usize,
y_stride: i32,
u_ptr: &mut [u8],
u_origin: usize,
u_stride: i32,
v_ptr: &mut [u8],
v_origin: usize,
v_stride: i32,
yuv_bit_depth: i32,
width: i32,
height: i32,
options: &SharpYuvOptions<'_>,
) -> bool {
sharp_yuv_convert_with_options_using(
r_ptr,
r_origin,
g_ptr,
g_origin,
b_ptr,
b_origin,
rgb_step,
rgb_stride,
rgb_bit_depth,
y_ptr,
y_origin,
y_stride,
u_ptr,
u_origin,
u_stride,
v_ptr,
v_origin,
v_stride,
yuv_bit_depth,
width,
height,
options,
Dsp::SCALAR,
)
}
#[must_use]
#[expect(clippy::too_many_arguments, reason = "mirrors the C signature")]
pub fn sharp_yuv_convert(
r_ptr: &[u8],
r_origin: usize,
g_ptr: &[u8],
g_origin: usize,
b_ptr: &[u8],
b_origin: usize,
rgb_step: i32,
rgb_stride: i32,
rgb_bit_depth: i32,
y_ptr: &mut [u8],
y_origin: usize,
y_stride: i32,
u_ptr: &mut [u8],
u_origin: usize,
u_stride: i32,
v_ptr: &mut [u8],
v_origin: usize,
v_stride: i32,
yuv_bit_depth: i32,
width: i32,
height: i32,
yuv_matrix: &SharpYuvConversionMatrix,
) -> bool {
let options = SharpYuvOptions {
yuv_matrix,
transfer_type: SharpYuvTransferFunctionType::kSharpYuvTransferFunctionSrgb,
};
sharp_yuv_convert_with_options(
r_ptr,
r_origin,
g_ptr,
g_origin,
b_ptr,
b_origin,
rgb_step,
rgb_stride,
rgb_bit_depth,
y_ptr,
y_origin,
y_stride,
u_ptr,
u_origin,
u_stride,
v_ptr,
v_origin,
v_stride,
yuv_bit_depth,
width,
height,
&options,
)
}