From 3d8ede41ebf0458e726d755d91dd0f939014f80a Mon Sep 17 00:00:00 2001 From: Luke Hsiao Date: Thu, 13 Aug 2020 00:49:29 -0700 Subject: [PATCH] build(x264-sys): update to x264-sys v0.2.0 This commit also runs rustfmt, and fixes some low-hanging clippy lints (such as the user of decprecated mem::uninitialized()). --- Cargo.toml | 2 +- src/data.rs | 56 +++++++++++++++--------------------- src/encoder.rs | 75 +++++++++++++++++++----------------------------- src/setup/mod.rs | 41 ++++++++------------------ 4 files changed, 66 insertions(+), 108 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index be28248..8607dc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ version = "0.3.0" authors = ["Ram "] [dependencies] -x264-sys = "0.1" +x264-sys = "0.2.0" [build-dependencies] pkg-config = "0.3" diff --git a/src/data.rs b/src/data.rs index f024ec6..9471f4b 100644 --- a/src/data.rs +++ b/src/data.rs @@ -8,16 +8,17 @@ use x264::*; pub struct Data<'a> { ptr: *mut x264_nal_t, len: usize, - spooky: PhantomData<&'a [x264_nal_t]> + spooky: PhantomData<&'a [x264_nal_t]>, } impl<'a> Data<'a> { #[doc(hidden)] - pub unsafe fn from_raw_parts( - ptr: *mut x264_nal_t, - len: usize - ) -> Self { - Data { ptr, len, spooky: PhantomData } + pub unsafe fn from_raw_parts(ptr: *mut x264_nal_t, len: usize) -> Self { + Data { + ptr, + len, + spooky: PhantomData, + } } /// The length (in NAL units, **not** in bytes) of this data sequence. @@ -40,25 +41,16 @@ impl<'a> Data<'a> { assert!(i < self.len); - let nal = unsafe { - *self.ptr.offset(i as isize) - }; + let nal = unsafe { *self.ptr.add(i) }; Unit { - priority: - match nal.i_ref_idc { - D => Priority::Disposable, - L => Priority::Low, - H => Priority::High, - _ => Priority::Highest, - }, - payload: - unsafe { - slice::from_raw_parts( - nal.p_payload, - nal.i_payload as usize - ) - } + priority: match nal.i_ref_idc { + D => Priority::Disposable, + L => Priority::Low, + H => Priority::High, + _ => Priority::Highest, + }, + payload: unsafe { slice::from_raw_parts(nal.p_payload, nal.i_payload as usize) }, } } @@ -69,14 +61,12 @@ impl<'a> Data<'a> { } else { let (a, b) = unsafe { let a = *self.ptr; - let b = *self.ptr.offset((self.len - 1) as isize); + let b = *self.ptr.add(self.len - 1); (a, b) }; - let start = a.p_payload; - let length = b.p_payload as usize - + b.i_payload as usize - - start as usize; + let start = a.p_payload; + let length = b.p_payload as usize + b.i_payload as usize - start as usize; unsafe { slice::from_raw_parts(start, length) } } @@ -86,7 +76,7 @@ impl<'a> Data<'a> { /// A single NAL unit. pub struct Unit<'a> { priority: Priority, - payload: &'a [u8] + payload: &'a [u8], } impl<'a> Unit<'a> { @@ -107,11 +97,11 @@ impl<'a> AsRef<[u8]> for Unit<'a> { /// The importance of a given unit. pub enum Priority { /// Not important at all. - Disposable = nal_priority_e::NAL_PRIORITY_DISPOSABLE as i32, + Disposable = nal_priority_e_NAL_PRIORITY_DISPOSABLE as i32, /// Not very important. - Low = nal_priority_e::NAL_PRIORITY_LOW as i32, + Low = nal_priority_e_NAL_PRIORITY_LOW as i32, /// Pretty important. - High = nal_priority_e::NAL_PRIORITY_HIGH as i32, + High = nal_priority_e_NAL_PRIORITY_HIGH as i32, /// Extremely important. - Highest = nal_priority_e::NAL_PRIORITY_HIGHEST as i32, + Highest = nal_priority_e_NAL_PRIORITY_HIGHEST as i32, } diff --git a/src/encoder.rs b/src/encoder.rs index 63b71fe..2da7acc 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -1,6 +1,6 @@ -use {Data, Encoding, Error, Image, Picture, Result, Setup}; use core::{mem, ptr}; use x264::*; +use {Data, Encoding, Error, Image, Picture, Result, Setup}; /// Encodes video. pub struct Encoder { @@ -18,7 +18,7 @@ impl Encoder { #[doc(hidden)] pub unsafe fn from_raw(raw: *mut x264_t) -> Self { - let mut params = mem::uninitialized(); + let mut params = mem::MaybeUninit::uninit().assume_init(); x264_encoder_parameters(raw, &mut params); Self { raw, params } } @@ -29,9 +29,7 @@ impl Encoder { /// /// Panics if there is a mismatch between the image and the encoder /// regarding width, height or colorspace. - pub fn encode(&mut self, pts: i64, image: Image) - -> Result<(Data, Picture)> - { + pub fn encode(&mut self, pts: i64, image: Image) -> Result<(Data, Picture)> { assert_eq!(image.width(), self.width()); assert_eq!(image.height(), self.height()); assert_eq!(image.encoding(), self.encoding()); @@ -44,27 +42,19 @@ impl Encoder { /// /// The caller must ensure that the width, height *and* colorspace /// of the image are the same as that of the encoder. - pub unsafe fn encode_unchecked(&mut self, pts: i64, image: Image) - -> Result<(Data, Picture)> - { + pub unsafe fn encode_unchecked(&mut self, pts: i64, image: Image) -> Result<(Data, Picture)> { let image = image.raw(); - let mut picture = mem::uninitialized(); + let mut picture = mem::MaybeUninit::uninit().assume_init(); x264_picture_init(&mut picture); picture.i_pts = pts; picture.img = image; let mut len = 0; - let mut stuff = mem::uninitialized(); - let mut raw = mem::uninitialized(); + let mut stuff = mem::MaybeUninit::uninit().assume_init(); + let mut raw = mem::MaybeUninit::uninit().assume_init(); - let err = x264_encoder_encode( - self.raw, - &mut stuff, - &mut len, - &mut picture, - &mut raw - ); + let err = x264_encoder_encode(self.raw, &mut stuff, &mut len, &mut picture, &mut raw); if err < 0 { Err(Error) @@ -78,15 +68,9 @@ impl Encoder { /// Gets the video headers, which should be sent first. pub fn headers(&mut self) -> Result { let mut len = 0; - let mut stuff = unsafe { mem::uninitialized() }; + let mut stuff = unsafe { mem::MaybeUninit::uninit().assume_init() }; - let err = unsafe { - x264_encoder_headers( - self.raw, - &mut stuff, - &mut len - ) - }; + let err = unsafe { x264_encoder_headers(self.raw, &mut stuff, &mut len) }; if err < 0 { Err(Error) @@ -114,9 +98,13 @@ impl Encoder { } /// The width required of any input images. - pub fn width(&self) -> i32 { self.params.i_width } + pub fn width(&self) -> i32 { + self.params.i_width + } /// The height required of any input images. - pub fn height(&self) -> i32 { self.params.i_height } + pub fn height(&self) -> i32 { + self.params.i_height + } /// The encoding required of any input images. pub fn encoding(&self) -> Encoding { unsafe { Encoding::from_raw(self.params.i_csp) } @@ -125,7 +113,9 @@ impl Encoder { impl Drop for Encoder { fn drop(&mut self) { - unsafe { x264_encoder_close(self.raw); } + unsafe { + x264_encoder_close(self.raw); + } } } @@ -144,26 +134,21 @@ impl Flush { } let mut len = 0; - let mut stuff = unsafe { mem::uninitialized() }; - let mut raw = unsafe { mem::uninitialized() }; - - let err = unsafe { - x264_encoder_encode( - enc, - &mut stuff, - &mut len, - ptr::null_mut(), - &mut raw - ) - }; + let mut stuff = unsafe { mem::MaybeUninit::uninit().assume_init() }; + let mut raw = unsafe { mem::MaybeUninit::uninit().assume_init() }; + + let err = + unsafe { x264_encoder_encode(enc, &mut stuff, &mut len, ptr::null_mut(), &mut raw) }; Some(if err < 0 { Err(Error) } else { - Ok(unsafe {( - Data::from_raw_parts(stuff, len as usize), - Picture::from_raw(raw), - )}) + Ok(unsafe { + ( + Data::from_raw_parts(stuff, len as usize), + Picture::from_raw(raw), + ) + }) }) } } diff --git a/src/setup/mod.rs b/src/setup/mod.rs index e1524f5..3821177 100644 --- a/src/setup/mod.rs +++ b/src/setup/mod.rs @@ -1,6 +1,6 @@ -use {Encoder, Encoding, Error, Result}; use core::mem; use x264::*; +use {Encoder, Encoding, Error, Result}; mod preset; mod tune; @@ -15,20 +15,15 @@ pub struct Setup { impl Setup { /// Creates a new builder with the specified preset and tune. - pub fn preset( - preset: Preset, - tune: Tune, - fast_decode: bool, - zero_latency: bool - ) -> Self { - let mut raw = unsafe { mem::uninitialized() }; + pub fn preset(preset: Preset, tune: Tune, fast_decode: bool, zero_latency: bool) -> Self { + let mut raw = unsafe { mem::MaybeUninit::uninit().assume_init() }; // Name validity verified at compile-time. assert_eq!(0, unsafe { x264_param_default_preset( &mut raw, preset.to_cstr(), - tune.to_cstr(fast_decode, zero_latency) + tune.to_cstr(fast_decode, zero_latency), ) }); @@ -37,7 +32,9 @@ impl Setup { /// Makes the first pass faster. pub fn fastfirstpass(mut self) -> Self { - unsafe { x264_param_apply_fastfirstpass(&mut self.raw); } + unsafe { + x264_param_apply_fastfirstpass(&mut self.raw); + } self } @@ -76,10 +73,7 @@ impl Setup { /// The lowest profile, with guaranteed compatibility with all decoders. pub fn baseline(mut self) -> Self { unsafe { - x264_param_apply_profile( - &mut self.raw, - b"baseline\0" as *const u8 as *const i8 - ); + x264_param_apply_profile(&mut self.raw, b"baseline\0" as *const u8 as *const i8); } self } @@ -87,10 +81,7 @@ impl Setup { /// A useless middleground between the baseline and high profiles. pub fn main(mut self) -> Self { unsafe { - x264_param_apply_profile( - &mut self.raw, - b"main\0" as *const u8 as *const i8 - ); + x264_param_apply_profile(&mut self.raw, b"main\0" as *const u8 as *const i8); } self } @@ -98,21 +89,13 @@ impl Setup { /// The highest profile, which almost all encoders support. pub fn high(mut self) -> Self { unsafe { - x264_param_apply_profile( - &mut self.raw, - b"high\0" as *const u8 as *const i8 - ); + x264_param_apply_profile(&mut self.raw, b"high\0" as *const u8 as *const i8); } self } /// Build the encoder. - pub fn build( - mut self, - csp: C, - width: i32, - height: i32, - ) -> Result + pub fn build(mut self, csp: C, width: i32, height: i32) -> Result where C: Into, { @@ -133,7 +116,7 @@ impl Setup { impl Default for Setup { fn default() -> Self { let raw = unsafe { - let mut raw = mem::uninitialized(); + let mut raw = mem::MaybeUninit::uninit().assume_init(); x264_param_default(&mut raw); raw };