Skip to content

Commit

Permalink
Merge pull request #15 from awxkee/dev
Browse files Browse the repository at this point in the history
Improvements on odd-sized
  • Loading branch information
awxkee authored Nov 28, 2024
2 parents 827e320 + 42c4a5e commit 9502dbf
Show file tree
Hide file tree
Showing 85 changed files with 2,120 additions and 978 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"] }

[package]
name = "yuvutils-rs"
version = "0.5.7"
version = "0.5.8"
edition = "2021"
description = "High performance utilities for YUV format handling and conversion."
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ Tests performed on the image 5763x3842

| | time(NEON) | Time(AVX) |
|------------------------|:----------:|:---------:|
| utils RGB->YUV 4:2:0 | 3.23ms | 3.53ms |
| utils RGB->YUV 4:2:0 | 3.16ms | 3.53ms |
| libyuv RGB->YUV 4:2:0 | 3.58ms | 33.87ms |
| utils RGBA->YUV 4:2:0 | 4.09ms | 5.47ms |
| utils RGBA->YUV 4:2:0 | 4.07ms | 5.47ms |
| libyuv RGBA->YUV 4:2:0 | 4.87ms | 23.48ms |
| utils RGBA->YUV 4:2:2 | 4.46ms | 7.08ms |
| libyuv RGBA->YUV 4:2:2 | 5.90ms | 35.23ms |
| utils RGBA->YUV 4:4:4 | 4.77ms | 7.97ms |
| utils RGBA->YUV 4:4:4 | 4.66ms | 7.97ms |

### Decoding

Expand Down
77 changes: 43 additions & 34 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
* 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.
*/
use image::{ColorType, EncodableLayout, GenericImageView, ImageReader};
use image::{ColorType, DynamicImage, EncodableLayout, GenericImageView, ImageReader};
use std::fs::File;
use std::io::Read;
use std::time::Instant;
use yuv_sys::{rs_I420ToRGB24, rs_NV12ToRGB24, rs_NV21ToABGR, rs_NV21ToRGB24};
use yuvutils_rs::{
rgb_to_yuv420, rgb_to_yuv420_p16, rgb_to_yuv422, rgb_to_yuv444, rgb_to_yuv_nv12,
yuv420_p16_to_rgb16, yuv420_to_rgb, yuv422_to_rgb, yuv444_to_rgb, yuv_nv12_to_rgb,
yuv_nv12_to_rgba, YuvBiPlanarImageMut, YuvBytesPacking, YuvChromaSubsampling, YuvEndianness,
YuvPlanarImageMut, YuvRange, YuvStandardMatrix,
rgb_to_yuv420, rgb_to_yuv420_p16, rgb_to_yuv422, rgb_to_yuv422_p16, rgb_to_yuv444,
rgb_to_yuv_nv12, yuv420_p16_to_rgb16, yuv420_to_rgb, yuv420_to_yuyv422, yuv422_p16_to_rgb16,
yuv422_to_rgb, yuv444_to_rgb, yuv_nv12_to_rgb, yuv_nv12_to_rgba, yuyv422_to_yuv420,
BufferStoreMut, YuvBiPlanarImageMut, YuvBytesPacking, YuvChromaSubsampling, YuvEndianness,
YuvPackedImage, YuvPackedImageMut, YuvPlanarImageMut, YuvRange, YuvStandardMatrix,
};

fn read_file_bytes(file_path: &str) -> Result<Vec<u8>, String> {
Expand All @@ -58,6 +59,8 @@ fn main() {
.decode()
.unwrap();

let img = DynamicImage::ImageRgb8(img.to_rgb8());

let dimensions = img.dimensions();

let width = dimensions.0;
Expand All @@ -72,12 +75,12 @@ fn main() {
}
};

let y_stride = width as usize * std::mem::size_of::<u8>();
let u_stride = (width + 1) / 2;
let v_stride = (width + 1) / 2;
let mut y_plane = vec![0u8; width as usize * height as usize];
let mut u_plane = vec![0u8; (height as usize + 1) / 2usize * u_stride as usize];
let mut v_plane = vec![0u8; (height as usize + 1) / 2usize * v_stride as usize];
let y_stride = width as usize + 100;
let u_stride = (width + 1) / 2 + 100;
let v_stride = (width + 1) / 2 + 100;
let mut y_plane = vec![0u8; y_stride as usize * height as usize];
let mut u_plane = vec![0u8; height as usize * u_stride as usize];
let mut v_plane = vec![0u8; height as usize * v_stride as usize];

let rgba_stride = width as usize * components;
let mut rgba = vec![0u8; height as usize * rgba_stride];
Expand All @@ -91,17 +94,20 @@ fn main() {
YuvBiPlanarImageMut::<u8>::alloc(width as u32, height as u32, YuvChromaSubsampling::Yuv420);

let mut planar_image =
YuvPlanarImageMut::<u8>::alloc(width as u32, height as u32, YuvChromaSubsampling::Yuv444);
YuvPlanarImageMut::<u16>::alloc(width as u32, height as u32, YuvChromaSubsampling::Yuv422);

// let mut bytes_16: Vec<u16> = src_bytes.iter().map(|&x| (x as u16) << 4).collect();
let mut bytes_16: Vec<u16> = src_bytes.iter().map(|&x| (x as u16) << 4).collect();

let start_time = Instant::now();
rgb_to_yuv444(
rgb_to_yuv422_p16(
&mut planar_image,
&src_bytes,
&bytes_16,
rgba_stride as u32,
12,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
YuvEndianness::LittleEndian,
YuvBytesPacking::LeastSignificantBytes,
)
.unwrap();
// bytes_16.fill(0);
Expand All @@ -110,34 +116,34 @@ fn main() {
//
println!("Forward time: {:?}", start_time.elapsed());
// // //
// let full_size = if width % 2 == 0 {
// 2 * width as usize * height as usize
// } else {
// 2 * (width as usize + 1) * height as usize
// };
let full_size = if width % 2 == 0 {
2 * width as usize * height as usize
} else {
2 * (width as usize + 1) * height as usize
};
// //
// // // println!("Full YUY2 {}", full_size);
// //
// let yuy2_stride = if width % 2 == 0 {
// 2 * width as usize
// } else {
// 2 * (width as usize + 1)
// };
let yuy2_stride = if width % 2 == 0 {
2 * width as usize
} else {
2 * (width as usize + 1)
};

// let mut yuy2_plane = vec![0u8; full_size];
// // // //
// let start_time = Instant::now();
// // // //
// let plane = planar_image.to_fixed();
//
// //
// let mut packed_image_mut = YuvPackedImageMut {
// yuy: BufferStoreMut::Owned(yuy2_plane),
// yuy_stride: yuy2_stride as u32,
// width,
// height,
// };
//
// yuv444_to_yuyv422(&mut packed_image_mut, &plane).unwrap();
// //
// yuv420_to_yuyv422(&mut packed_image_mut, &plane).unwrap();
// let end_time = Instant::now().sub(start_time);
// println!("yuv420_to_yuyv422 time: {:?}", end_time);
// // rgba.fill(0);
Expand Down Expand Up @@ -165,9 +171,9 @@ fn main() {
// width,
// height,
// };
//
// yuyv422_to_yuv444(&mut planar_image, &packed_image).unwrap();
// //
// yuyv422_to_yuv420(&mut planar_image, &packed_image).unwrap();
// // //
// let end_time = Instant::now().sub(start_time);
// println!("yuyv422_to_yuv444 time: {:?}", end_time);
rgba.fill(0);
Expand Down Expand Up @@ -254,12 +260,15 @@ fn main() {
// let rgba_stride = width as usize * 4;
// let mut rgba = vec![0u8; height as usize * rgba_stride];

yuv444_to_rgb(
yuv422_p16_to_rgb16(
&fixed_planar,
&mut rgba,
&mut bytes_16,
rgba_stride as u32,
12,
YuvRange::Limited,
YuvStandardMatrix::Bt601,
YuvEndianness::LittleEndian,
YuvBytesPacking::LeastSignificantBytes,
)
.unwrap();

Expand Down Expand Up @@ -300,10 +309,10 @@ fn main() {
// chunk[2] = b;
// });

// rgba = bytes_16.iter().map(|&x| (x >> 4) as u8).collect();
rgba = bytes_16.iter().map(|&x| (x >> 4) as u8).collect();

image::save_buffer(
"converted_sharp15.png",
"converted_sharp15.jpg",
rgba.as_bytes(),
dimensions.0,
dimensions.1,
Expand Down
7 changes: 7 additions & 0 deletions coeffs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "coeffs"
version = "0.1.0"
edition = "2021"

[dependencies]
rug = "1.24.1"
Loading

0 comments on commit 9502dbf

Please sign in to comment.