Skip to content

Commit

Permalink
Merge pull request #19 from awxkee/dev
Browse files Browse the repository at this point in the history
Added BiPlanar fuzzing
  • Loading branch information
awxkee authored Dec 5, 2024
2 parents c823070 + bafd884 commit 303a090
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["app", "coeffs", "fuzz"] }

[package]
name = "yuvutils-rs"
version = "0.5.9"
version = "0.5.10"
edition = "2021"
description = "High performance utilities for YUV format handling and conversion."
readme = "README.md"
Expand Down
7 changes: 7 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ path = "yuv_to_rgb/yuv_to_rgb.rs"
test = false
doc = false
bench = false

[[bin]]
name = "yuv_nv_to_rgb"
path = "yuv_nv_to_rgb/yuv_nv_to_rgb.rs"
test = false
doc = false
bench = false
160 changes: 160 additions & 0 deletions fuzz/yuv_nv_to_rgb/yuv_nv_to_rgb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright (c) Radzivon Bartoshyk, 12/2024. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#![no_main]

use libfuzzer_sys::fuzz_target;
use yuvutils_rs::{
yuv_nv16_to_rgb, yuv_nv16_to_rgba, yuv_nv21_to_rgb, yuv_nv21_to_rgba, yuv_nv24_to_rgb,
yuv_nv24_to_rgba, YuvBiPlanarImage, YuvRange, YuvStandardMatrix,
};

fuzz_target!(|data: (u8, u8, u8, u8, u8)| {
fuzz_yuv_420(data.0, data.1, data.2, data.3);
fuzz_yuv_422(data.0, data.1, data.2, data.3);
fuzz_yuv_444(data.0, data.1, data.2, data.3);
});

fn fuzz_yuv_420(i_width: u8, i_height: u8, y_value: u8, uv_value: u8) {
if i_height == 0 || i_width == 0 {
return;
}
let y_plane = vec![y_value; i_height as usize * i_width as usize];
let uv_plane =
vec![uv_value; (i_width as usize).div_ceil(2) * 2 * (i_height as usize).div_ceil(2)];

let planar_image = YuvBiPlanarImage {
y_plane: &y_plane,
y_stride: i_width as u32,
uv_plane: &uv_plane,
uv_stride: (i_width as u32).div_ceil(2) * 2,
width: i_width as u32,
height: i_height as u32,
};

let mut target_rgb = vec![0u8; i_width as usize * i_height as usize * 3];

yuv_nv21_to_rgb(
&planar_image,
&mut target_rgb,
i_width as u32 * 3,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
)
.unwrap();

let mut target_rgba = vec![0u8; i_width as usize * i_height as usize * 4];

yuv_nv21_to_rgba(
&planar_image,
&mut target_rgba,
i_width as u32 * 4,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
)
.unwrap();
}

fn fuzz_yuv_422(i_width: u8, i_height: u8, y_value: u8, uv_value: u8) {
if i_height == 0 || i_width == 0 {
return;
}
let y_plane = vec![y_value; i_height as usize * i_width as usize];
let uv_plane = vec![uv_value; (i_width as usize).div_ceil(2) * 2 * i_height as usize];

let planar_image = YuvBiPlanarImage {
y_plane: &y_plane,
y_stride: i_width as u32,
uv_plane: &uv_plane,
uv_stride: (i_width as u32).div_ceil(2) * 2,
width: i_width as u32,
height: i_height as u32,
};

let mut target_rgb = vec![0u8; i_width as usize * i_height as usize * 3];

yuv_nv16_to_rgb(
&planar_image,
&mut target_rgb,
i_width as u32 * 3,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
)
.unwrap();

let mut target_rgba = vec![0u8; i_width as usize * i_height as usize * 4];

yuv_nv16_to_rgba(
&planar_image,
&mut target_rgba,
i_width as u32 * 4,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
)
.unwrap();
}

fn fuzz_yuv_444(i_width: u8, i_height: u8, y_value: u8, uv_value: u8) {
if i_height == 0 || i_width == 0 {
return;
}
let y_plane = vec![y_value; i_height as usize * i_width as usize];
let uv_plane = vec![uv_value; i_width as usize * 2 * i_height as usize];

let planar_image = YuvBiPlanarImage {
y_plane: &y_plane,
y_stride: i_width as u32,
uv_plane: &uv_plane,
uv_stride: i_width as u32 * 2,
width: i_width as u32,
height: i_height as u32,
};

let mut target_rgb = vec![0u8; i_width as usize * i_height as usize * 3];

yuv_nv24_to_rgb(
&planar_image,
&mut target_rgb,
i_width as u32 * 3,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
)
.unwrap();

let mut target_rgba = vec![0u8; i_width as usize * i_height as usize * 4];

yuv_nv24_to_rgba(
&planar_image,
&mut target_rgba,
i_width as u32 * 4,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
)
.unwrap();
}
6 changes: 3 additions & 3 deletions src/rgb_to_nv_p16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
iter.for_each(|((y_dst, uv_dst), rgba)| {
process_halved_row(
&mut y_dst[0..image.width as usize],
&mut uv_dst[0..image.width as usize],
&mut uv_dst[0..(image.width as usize).div_ceil(2) * 2],
&rgba[0..image.width as usize * channels],
true,
);
Expand Down Expand Up @@ -282,7 +282,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
{
process_halved_row(
&mut y_dst[0..image.width as usize],
&mut uv_dst[0..image.width as usize],
&mut uv_dst[0..(image.width as usize).div_ceil(2) * 2],
&rgba[0..image.width as usize * channels],
y == 0,
);
Expand All @@ -300,7 +300,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
let rgba = rgba.chunks_exact(rgba_stride as usize * 2).remainder();
process_halved_row(
&mut y_dst[0..image.width as usize],
&mut uv_dst[0..image.width as usize],
&mut uv_dst[0..(image.width as usize).div_ceil(2) * 2],
&rgba[0..image.width as usize * channels],
true,
);
Expand Down
6 changes: 3 additions & 3 deletions src/rgba_to_nv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
iter.for_each(|((y_dst, uv_dst), rgba)| {
process_halved_row(
&mut y_dst[0..image.width as usize],
&mut uv_dst[0..image.width as usize],
&mut uv_dst[0..(image.width as usize).div_ceil(2) * 2],
&rgba[0..image.width as usize * channels],
true,
);
Expand Down Expand Up @@ -316,7 +316,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
{
process_halved_row(
&mut y_dst[0..image.width as usize],
&mut uv_dst[0..image.width as usize],
&mut uv_dst[0..(image.width as usize).div_ceil(2) * 2],
&rgba[0..image.width as usize * channels],
y == 0,
);
Expand All @@ -334,7 +334,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
let rgba = rgba.chunks_exact(rgba_stride as usize * 2).remainder();
process_halved_row(
&mut y_dst[0..image.width as usize],
&mut uv_dst[0..image.width as usize],
&mut uv_dst[0..(image.width as usize).div_ceil(2) * 2],
&rgba[0..image.width as usize * channels],
true,
);
Expand Down
6 changes: 3 additions & 3 deletions src/yuv_nv_p10_to_rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn yuv_nv_p10_to_image_impl<
iter.for_each(|((y_src, uv_src), rgba)| {
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
});
Expand All @@ -295,7 +295,7 @@ fn yuv_nv_p10_to_image_impl<
{
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
}
Expand All @@ -308,7 +308,7 @@ fn yuv_nv_p10_to_image_impl<
.into_remainder();
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/yuv_nv_p16_to_rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn yuv_nv_p16_to_image_impl<
iter.for_each(|((y_src, uv_src), rgba)| {
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
});
Expand All @@ -374,7 +374,7 @@ fn yuv_nv_p16_to_image_impl<
{
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
}
Expand All @@ -387,7 +387,7 @@ fn yuv_nv_p16_to_image_impl<
.into_remainder();
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/yuv_nv_to_rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ fn yuv_nv12_to_rgbx<
iter.for_each(|((y_src, uv_src), rgba)| {
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
});
Expand All @@ -585,7 +585,7 @@ fn yuv_nv12_to_rgbx<
process_double_chroma_row(
&y_src0[0..image.width as usize],
&y_src1[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba0[0..image.width as usize * channels],
&mut rgba1[0..image.width as usize * channels],
);
Expand All @@ -598,7 +598,7 @@ fn yuv_nv12_to_rgbx<
.into_remainder();
process_halved_chroma_row(
&y_src[0..image.width as usize],
&uv_src[0..image.width as usize],
&uv_src[0..(image.width as usize).div_ceil(2) * 2],
&mut rgba[0..image.width as usize * channels],
);
}
Expand Down

0 comments on commit 303a090

Please sign in to comment.