Skip to content

Commit

Permalink
remove use of mem::zeroed in xcoder, where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Aug 15, 2024
1 parent 29a1f7b commit 01e4393
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
5 changes: 3 additions & 2 deletions xcoder/xcoder-quadra/src/cropper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ impl XcoderCropper {
let pixel_format = if f.surface().bit_depth == 2 { sys::GC620_I010_ } else { sys::GC620_I420_ };
unsafe {
let frame_in = **f;
let mut frame_out: sys::ni_frame_t = mem::zeroed();
let code = sys::ni_frame_buffer_alloc_hwenc(&mut frame_out as _, crop.width, crop.height, 0);
let mut frame_out = mem::MaybeUninit::<sys::ni_frame_t>::zeroed();
let code = sys::ni_frame_buffer_alloc_hwenc(frame_out.as_mut_ptr(), crop.width, crop.height, 0);
if code != sys::ni_retcode_t_NI_RETCODE_SUCCESS {
return Err(XcoderCropperError::Unknown {
code,
operation: "allocating frame",
});
}
let frame_out = frame_out.assume_init();
let data_io_out = sys::ni_session_data_io_t {
data: sys::_ni_session_data_io__bindgen_ty_1 { frame: frame_out },
};
Expand Down
8 changes: 6 additions & 2 deletions xcoder/xcoder-quadra/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub struct XcoderDecoderInputFrame {
impl XcoderDecoderInputFrame {
fn to_data_io<E: Error>(&self, pos: i64, width: i32, height: i32, is_start_of_stream: bool) -> Result<DataIo, XcoderDecoderError<E>> {
unsafe {
// Granting an exception here. This structure is valid in zeroed memory state and there is no
// officially sanctioned way to initialize it otherwise. Plus the netint SDK zeroes this structure itself.
#[allow(clippy::disallowed_methods)]
let mut packet: sys::ni_packet_t = mem::zeroed();
packet.dts = self.dts as _;
packet.pts = self.pts as _;
Expand Down Expand Up @@ -81,9 +84,9 @@ pub struct XcoderDecodedFrame {

impl XcoderDecodedFrame {
unsafe fn new_hardware_frame<E: Error>(session: *mut sys::ni_session_context_t, width: i32, height: i32) -> Result<Self, XcoderDecoderError<E>> {
let mut frame: sys::ni_frame_t = mem::zeroed();
let mut frame = mem::MaybeUninit::<sys::ni_frame_t>::zeroed();
let code = sys::ni_frame_buffer_alloc(
&mut frame as _,
frame.as_mut_ptr(),
width,
height,
if (*session).codec_format == sys::_ni_codec_format_NI_CODEC_FORMAT_H264 {
Expand All @@ -102,6 +105,7 @@ impl XcoderDecodedFrame {
operation: "allocating frame",
});
}
let frame = frame.assume_init();
Ok(Self {
data_io: Some(sys::ni_session_data_io_t {
data: sys::_ni_session_data_io__bindgen_ty_1 { frame },
Expand Down
21 changes: 14 additions & 7 deletions xcoder/xcoder-quadra/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use av_traits::{EncodedFrameType, EncodedVideoFrame, RawVideoFrame, VideoEncoder
use scopeguard::{guard, ScopeGuard};
use snafu::Snafu;
use std::{
array, collections::VecDeque, mem::{self, MaybeUninit}, os::raw::c_void, ptr::null_mut
array,
collections::VecDeque,
mem::{self, MaybeUninit},
os::raw::c_void,
ptr::null_mut,
};
use xcoder_quadra_sys::{self as sys, ni_packet_t, ni_xcoder_params_t};

Expand Down Expand Up @@ -279,9 +283,9 @@ impl<F> XcoderEncoder<F> {
});

let frame_data_io = {
let mut frame: sys::ni_frame_t = mem::zeroed();
let mut frame = mem::MaybeUninit::<sys::ni_frame_t>::zeroed();
let code = sys::ni_frame_buffer_alloc_pixfmt(
&mut frame as _,
frame.as_mut_ptr(),
pixel_format,
config.width as _,
config.height as _,
Expand All @@ -295,6 +299,7 @@ impl<F> XcoderEncoder<F> {
operation: "allocating frame buffer",
});
}
let frame = frame.assume_init();
sys::ni_session_data_io_t {
data: sys::_ni_session_data_io__bindgen_ty_1 { frame },
}
Expand Down Expand Up @@ -460,10 +465,12 @@ impl<F: RawVideoFrame<u8>> XcoderEncoder<F> {
}

let mut dst_data = [frame.p_data[0], frame.p_data[1], frame.p_data[2]];
let mut src_data: [*mut u8; 3] = array::from_fn(|i| if i < self.config.pixel_format.plane_count() {
f.samples(i).as_ptr() as *mut u8
} else {
null_mut()
let mut src_data: [*mut u8; 3] = array::from_fn(|i| {
if i < self.config.pixel_format.plane_count() {
f.samples(i).as_ptr() as *mut u8
} else {
null_mut()
}
});
let mut src_strides = self.config.pixel_format.strides(self.config.width as i32);
let mut src_heights = self.config.pixel_format.heights(self.config.height as i32);
Expand Down
58 changes: 23 additions & 35 deletions xcoder/xcoder-quadra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,16 @@ mod linux_impl {
/// types of data is still considered to be one plane.
pub fn plane_count(&self) -> usize {
match self {
XcoderPixelFormat::Yuv420Planar |
XcoderPixelFormat::Yuv420Planar10BitLittleEndian => 3,
XcoderPixelFormat::Nv12 |
XcoderPixelFormat::Nv16 |
XcoderPixelFormat::P010LittleEndian => 2,
XcoderPixelFormat::Rgba |
XcoderPixelFormat::Bgra |
XcoderPixelFormat::Argb |
XcoderPixelFormat::Abgr |
XcoderPixelFormat::Bgr0 |
XcoderPixelFormat::Bgrp |
XcoderPixelFormat::Yuyv422 |
XcoderPixelFormat::Uyvy422 => 1,
XcoderPixelFormat::Yuv420Planar | XcoderPixelFormat::Yuv420Planar10BitLittleEndian => 3,
XcoderPixelFormat::Nv12 | XcoderPixelFormat::Nv16 | XcoderPixelFormat::P010LittleEndian => 2,
XcoderPixelFormat::Rgba
| XcoderPixelFormat::Bgra
| XcoderPixelFormat::Argb
| XcoderPixelFormat::Abgr
| XcoderPixelFormat::Bgr0
| XcoderPixelFormat::Bgrp
| XcoderPixelFormat::Yuyv422
| XcoderPixelFormat::Uyvy422 => 1,
XcoderPixelFormat::None => 0,
}
}
Expand All @@ -160,38 +157,29 @@ mod linux_impl {
XcoderPixelFormat::Yuv420Planar10BitLittleEndian => [width * 2, width, width],
XcoderPixelFormat::Nv12 => [width, width, 0],
XcoderPixelFormat::P010LittleEndian => [width * 2, width * 2, 0],
XcoderPixelFormat::Rgba |
XcoderPixelFormat::Bgra |
XcoderPixelFormat::Argb |
XcoderPixelFormat::Abgr |
XcoderPixelFormat::Bgr0 => [width * 4, 0, 0],
XcoderPixelFormat::Rgba | XcoderPixelFormat::Bgra | XcoderPixelFormat::Argb | XcoderPixelFormat::Abgr | XcoderPixelFormat::Bgr0 => {
[width * 4, 0, 0]
}
XcoderPixelFormat::Bgrp => [width * 3, 0, 0],
XcoderPixelFormat::Nv16 |
XcoderPixelFormat::Yuyv422 |
XcoderPixelFormat::Uyvy422 => [width, width / 2, 0],
XcoderPixelFormat::Nv16 | XcoderPixelFormat::Yuyv422 | XcoderPixelFormat::Uyvy422 => [width, width / 2, 0],
XcoderPixelFormat::None => [0; 3],
}
}

pub fn heights(&self, height: i32) -> [i32; 3] {
match self {
XcoderPixelFormat::Yuv420Planar |
XcoderPixelFormat::Yuv420Planar10BitLittleEndian => [height, height / 2, height / 2],
XcoderPixelFormat::Nv12 |
XcoderPixelFormat::P010LittleEndian => [height, height / 2, 0],
XcoderPixelFormat::Rgba |
XcoderPixelFormat::Bgra |
XcoderPixelFormat::Argb |
XcoderPixelFormat::Abgr |
XcoderPixelFormat::Bgr0 |
XcoderPixelFormat::Bgrp => [height, 0, 0],
XcoderPixelFormat::Nv16 |
XcoderPixelFormat::Yuyv422 |
XcoderPixelFormat::Uyvy422 => [height, height, 0],
XcoderPixelFormat::Yuv420Planar | XcoderPixelFormat::Yuv420Planar10BitLittleEndian => [height, height / 2, height / 2],
XcoderPixelFormat::Nv12 | XcoderPixelFormat::P010LittleEndian => [height, height / 2, 0],
XcoderPixelFormat::Rgba
| XcoderPixelFormat::Bgra
| XcoderPixelFormat::Argb
| XcoderPixelFormat::Abgr
| XcoderPixelFormat::Bgr0
| XcoderPixelFormat::Bgrp => [height, 0, 0],
XcoderPixelFormat::Nv16 | XcoderPixelFormat::Yuyv422 | XcoderPixelFormat::Uyvy422 => [height, height, 0],
XcoderPixelFormat::None => [0; 3],
}
}

}
}

Expand Down
3 changes: 3 additions & 0 deletions xcoder/xcoder-quadra/src/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ impl XcoderScaler {
let pixel_format_out = if self.config.bit_depth == 10 { sys::GC620_I010_ } else { sys::GC620_I420_ };
unsafe {
let frame_in = **f;
// Granting an exception here. This structure is valid in zeroed memory state and there is no
// officially sanctioned way to initialize it otherwise.
#[allow(clippy::disallowed_methods)]
let mut frame_out: sys::ni_frame_t = mem::zeroed();
frame_out.pts = frame_in.pts;

Expand Down

0 comments on commit 01e4393

Please sign in to comment.