1
Fork 0

conversion to 0RGB

This commit is contained in:
Andy Killorin 2024-12-31 11:20:45 -05:00
parent 3e6b05645f
commit 2dab4515fb
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -1,6 +1,6 @@
use std::{mem::transmute, net::{self, SocketAddr}, slice, sync::{Mutex, OnceLock}};
use openh264::decoder::Decoder;
use openh264::{decoder::Decoder, formats::YUVSource};
#[no_mangle]
pub extern fn add(a: u64, b: u64) -> u64 {
@ -25,6 +25,8 @@ pub extern fn reset_decoder() -> u64 {
#[no_mangle]
/// decode h264 packet 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()));
@ -34,7 +36,15 @@ pub extern fn decode_h264(image: &mut u32, packet: &u8, length: u32) -> u64 {
let image: &mut [u8; 4*320*240] = unsafe{transmute(image)};
if let Ok(Some(frame)) = decoder.decode(packet) {
frame.write_rgba8(image);
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];
}
return 1;
}