Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve YUV to RGB Performance II #67

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 102 additions & 26 deletions openh264/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ pub struct DecodedYUV<'a> {
/// - if block size is `2` (for U and V), you will get `f32x8(00112233)`
macro_rules! f32x8_from_slice_with_blocksize {
($buf:expr, $block_size:expr) => {{
// Use 16 Y values
wide::f32x8::from([
($buf[0] as f32),
($buf[1 / $block_size] as f32),
Expand Down Expand Up @@ -395,7 +394,10 @@ impl DecodedYUV<'_> {
target.len()
);

if dim.0 % 8 == 0 {
// for f32x8 math, image needs to:
// - have a width divisible by 8
// - have at least two rows
if dim.0 % 8 == 0 && dim.1 >= 2 {
Self::write_rgb8_f32x8(self.y, self.u, self.v, dim, strides, target);
} else {
Self::write_rgb8_scalar(self.y, self.u, self.v, dim, strides, target);
Expand Down Expand Up @@ -439,6 +441,48 @@ impl DecodedYUV<'_> {
strides: (usize, usize, usize),
target: &mut [u8],
) {
// this assumes we are decoding YUV420
assert_eq!(y_plane.len(), u_plane.len() * 4);
assert_eq!(y_plane.len(), v_plane.len() * 4);
assert!(dim.0 % 8 == 0);

let (width, height) = dim;
/// rgb pixel size in bytes
const RGB_PIXEL_LEN: usize = 3;
let rgb_bytes_per_row: usize = RGB_PIXEL_LEN * width;

for y in 0..(height / 2) {
// load U and V values for two rows of pixels
let base_u = y * strides.1;
let u_row = &u_plane[base_u..base_u + strides.1];
let base_v = y * strides.2;
let v_row = &v_plane[base_v..base_v + strides.2];

// load Y values for first row
let base_y = 2 * y * strides.0;
let y_row = &y_plane[base_y..base_y + strides.0];

// calculate first RGB row
let base_tgt = 2 * y * rgb_bytes_per_row;
let row_target = &mut target[base_tgt..base_tgt + rgb_bytes_per_row];
Self::write_rgb8_f32x8_row(y_row, u_row, v_row, width, row_target);

// load Y values for second row
let base_y = (2 * y + 1) * strides.0;
let y_row = &y_plane[base_y..base_y + strides.0];

// calculate second RGB row
let base_tgt = (2 * y + 1) * rgb_bytes_per_row;
let row_target = &mut target[base_tgt..(base_tgt + rgb_bytes_per_row)];
Self::write_rgb8_f32x8_row(y_row, u_row, v_row, width, row_target);
}
}

#[inline(always)]
fn write_rgb8_f32x8_row(y_row: &[u8], u_row: &[u8], v_row: &[u8], width: usize, target: &mut [u8]) {
assert_eq!(y_row.len(), u_row.len() * 2);
assert_eq!(y_row.len(), v_row.len() * 2);

let rv_mul = wide::f32x8::splat(1.402);
let gu_mul = wide::f32x8::splat(-0.344);
let gv_mul = wide::f32x8::splat(-0.714);
Expand All @@ -448,38 +492,47 @@ impl DecodedYUV<'_> {
let lower_bound = wide::f32x8::splat(0.0);

const STEP: usize = 8;
assert!(y_row.len() % STEP == 0);

for y in 0..dim.1 {
for x in (0..dim.0).step_by(STEP) {
let base_tgt = (y * dim.0 + x) * 3;
let base_y = y * strides.0 + x;
let base_u = (y / 2 * strides.1) + (x / 2);
let base_v = (y / 2 * strides.2) + (x / 2);
const UV_STEP: usize = STEP / 2;
assert!(u_row.len() % UV_STEP == 0);
assert!(v_row.len() % UV_STEP == 0);

let pixels = &mut target[base_tgt..(base_tgt + (3 * STEP))];
const TGT_STEP: usize = STEP * 3;
assert!(target.len() % TGT_STEP == 0);

let y_pack: wide::f32x8 = f32x8_from_slice_with_blocksize!(y_plane[base_y..], 1);
let u_pack: wide::f32x8 = f32x8_from_slice_with_blocksize!(u_plane[base_u..], 2) - 128.0;
let v_pack: wide::f32x8 = f32x8_from_slice_with_blocksize!(v_plane[base_v..], 2) - 128.0;
let mut base_y = 0;
let mut base_uv = 0;
let mut base_tgt = 0;

let r_pack = v_pack.mul_add(rv_mul, y_pack);
let g_pack = v_pack.mul_add(gv_mul, u_pack.mul_add(gu_mul, y_pack));
let b_pack = u_pack.mul_add(bu_mul, y_pack);
for _ in (0..width).step_by(STEP) {
let pixels = &mut target[base_tgt..(base_tgt + TGT_STEP)];

let (r_pack, g_pack, b_pack) = (
r_pack.fast_min(upper_bound).fast_max(lower_bound).fast_trunc_int(),
g_pack.fast_min(upper_bound).fast_max(lower_bound).fast_trunc_int(),
b_pack.fast_min(upper_bound).fast_max(lower_bound).fast_trunc_int(),
);
let y_pack: wide::f32x8 = f32x8_from_slice_with_blocksize!(y_row[base_y..], 1);
let u_pack: wide::f32x8 = f32x8_from_slice_with_blocksize!(u_row[base_uv..], 2) - 128.0;
let v_pack: wide::f32x8 = f32x8_from_slice_with_blocksize!(v_row[base_uv..], 2) - 128.0;

let (r_pack, g_pack, b_pack) = (r_pack.as_array_ref(), g_pack.as_array_ref(), b_pack.as_array_ref());
let r_pack = v_pack.mul_add(rv_mul, y_pack);
let g_pack = v_pack.mul_add(gv_mul, u_pack.mul_add(gu_mul, y_pack));
let b_pack = u_pack.mul_add(bu_mul, y_pack);

for i in 0..STEP {
pixels[(3 * i) + 0] = r_pack[i] as u8;
pixels[(3 * i) + 1] = g_pack[i] as u8;
pixels[(3 * i) + 2] = b_pack[i] as u8;
}
let (r_pack, g_pack, b_pack) = (
r_pack.fast_min(upper_bound).fast_max(lower_bound).fast_trunc_int(),
g_pack.fast_min(upper_bound).fast_max(lower_bound).fast_trunc_int(),
b_pack.fast_min(upper_bound).fast_max(lower_bound).fast_trunc_int(),
);

let (r_pack, g_pack, b_pack) = (r_pack.as_array_ref(), g_pack.as_array_ref(), b_pack.as_array_ref());

for i in 0..STEP {
pixels[3 * i] = r_pack[i] as u8;
pixels[(3 * i) + 1] = g_pack[i] as u8;
pixels[(3 * i) + 2] = b_pack[i] as u8;
}

base_y += STEP;
base_uv += UV_STEP;
base_tgt += TGT_STEP;
}
}

Expand Down Expand Up @@ -529,6 +582,29 @@ impl DecodedYUV<'_> {
}
}

#[test]
fn convert_yuv_to_rgb_512x512() {
let source = include_bytes!("../tests/data/single_512x512_cavlc.h264");

let api = OpenH264API::from_source();
let config = DecoderConfig::default();
let mut decoder = Decoder::with_api_config(api, config).unwrap();

let mut rgb = vec![0; 2000 * 2000 * 3];
let yuv = decoder.decode(&source[..]).unwrap().unwrap();
let dim = yuv.dimensions();
let rgb_len = dim.0 * dim.1 * 3;

let tgt = &mut rgb[0..rgb_len];

DecodedYUV::write_rgb8_scalar(yuv.y(), yuv.u(), yuv.v(), yuv.dimensions(), yuv.strides(), tgt);

let mut tgt2 = vec![0; tgt.len()];
DecodedYUV::write_rgb8_f32x8(yuv.y(), yuv.u(), yuv.v(), yuv.dimensions(), yuv.strides(), &mut tgt2);

assert_eq!(tgt, tgt2);
}

impl YUVSource for DecodedYUV<'_> {
fn dimensions_i32(&self) -> (i32, i32) {
(self.info.iWidth, self.info.iHeight)
Expand Down
8 changes: 4 additions & 4 deletions openh264/src/formats/yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,18 @@ mod tests {
/// Test every YUV value and see, if the SIMD version delivers a similar RGB value.
#[test]
fn test_write_rgb8_f32x8_spectrum() {
let dim = (8, 1);
let dim = (8, 2);
let strides = (8, 4, 4);

// build artificial YUV planes containing the entire YUV spectrum
for y in 0..=255u8 {
for u in 0..=255u8 {
for v in 0..=255u8 {
let (y_plane, u_plane, v_plane) = (vec![y; 8], vec![u; 4], vec![v; 4]);
let mut target = vec![0; dim.0 * 3];
let (y_plane, u_plane, v_plane) = (vec![y; 16], vec![u; 4], vec![v; 4]);
let mut target = vec![0; dim.0 * dim.1 * 3];
crate::decoder::DecodedYUV::write_rgb8_scalar(&y_plane, &u_plane, &v_plane, dim, strides, &mut target);

let mut target2 = vec![0; dim.0 * 3];
let mut target2 = vec![0; dim.0 * dim.1 * 3];
crate::decoder::DecodedYUV::write_rgb8_f32x8(&y_plane, &u_plane, &v_plane, dim, strides, &mut target2);

// compare first pixel
Expand Down
Loading