switched to jpeg from h264 compression

10x larger over the wire but no dependency on libstdc++
This commit is contained in:
Andy Killorin 2025-01-02 19:42:05 -05:00
commit f5bc322862
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
3 changed files with 46 additions and 56 deletions

16
decoder/Cargo.lock generated
View file

@ -79,6 +79,7 @@ version = "0.1.0"
dependencies = [
"openh264",
"tokio",
"zune-jpeg",
]
[[package]]
@ -453,3 +454,18 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "zune-core"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a"
[[package]]
name = "zune-jpeg"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99a5bab8d7dedf81405c4bb1f2b83ea057643d9cb28778cea9eecddeedd2e028"
dependencies = [
"zune-core",
]

View file

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
openh264 = "0.6.6"
tokio = { version = "1.42.0", features = ["full"] }
zune-jpeg = "0.4.14"
[lib]
crate-type = ["cdylib","lib"]

View file

@ -1,51 +1,30 @@
use std::{mem::transmute, net::{self, SocketAddr}, slice, sync::{Mutex, OnceLock}};
use openh264::{decoder::Decoder, formats::YUVSource};
use zune_jpeg::{zune_core::{colorspace::ColorSpace, options::DecoderOptions}, JpegDecoder};
#[no_mangle]
pub extern fn add(a: u64, b: u64) -> u64 {
a + b
}
static DECODER: OnceLock<Mutex<Decoder>> = OnceLock::new();
#[no_mangle]
/// reset h264 decoder
///
/// call before opening new stream
pub extern fn reset_decoder() -> u64 {
// calls constructor twice in the reset case, not big deal
if let Err(_) = DECODER.set(Mutex::new(Decoder::new().unwrap())) {
*DECODER.get().unwrap().lock().unwrap() = Decoder::new().unwrap();
}
0
}
#[no_mangle]
/// decode h264 packet of given length
/// decode jpeg image of given length
///
/// image format is 0RGB
///
/// returns 1 if the packet contained a new frame, otherwise 0
pub extern fn decode_h264(image: &mut u32, packet: &u8, length: u32) -> u64 {
let decoder = DECODER.get_or_init(|| Mutex::new(Decoder::new().unwrap()));
let mut decoder = decoder.lock().unwrap();
pub extern fn decode(image: &mut u32, packet: &u8, length: u32) -> u64 {
let packet = unsafe {slice::from_raw_parts(packet, length as usize)};
let image: &mut [u8; 4*320*240] = unsafe{transmute(image)};
if let Ok(Some(frame)) = decoder.decode(packet) {
let mut buf = [0u8; 3*320*240];
frame.write_rgb8(&mut buf);
// 0RGB conversion, avoids need to modify openh264
for (buffer, image) in buf.chunks_exact_mut(3).zip(image.chunks_exact_mut(4)) {
image[1] = buffer[0];
image[2] = buffer[1];
image[3] = buffer[2];
}
let options = DecoderOptions::new_fast().jpeg_set_out_colorspace(ColorSpace::RGB);
let mut frame = JpegDecoder::new_with_options(packet, options).decode().unwrap();
return 1;
// 0RGB conversion, easier than shifting and zeroing A
for (buffer, image) in frame.chunks_exact_mut(3).zip(image.chunks_exact_mut(4)) {
image[0] = 0;
image[1] = buffer[0];
image[2] = buffer[1];
image[3] = buffer[2];
}
0