Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(x264-sys): update to x264-sys v0.2.0 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.3.0"
authors = ["Ram <[email protected]>"]

[dependencies]
x264-sys = "0.1"
x264-sys = "0.2.0"

[build-dependencies]
pkg-config = "0.3"
56 changes: 23 additions & 33 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) },
}
}

Expand All @@ -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) }
}
Expand All @@ -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> {
Expand All @@ -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,
}
75 changes: 30 additions & 45 deletions src/encoder.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 }
}
Expand All @@ -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());
Expand All @@ -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)
Expand All @@ -78,15 +68,9 @@ impl Encoder {
/// Gets the video headers, which should be sent first.
pub fn headers(&mut self) -> Result<Data> {
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)
Expand Down Expand Up @@ -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) }
Expand All @@ -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);
}
}
}

Expand All @@ -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),
)
})
})
}
}
41 changes: 12 additions & 29 deletions src/setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {Encoder, Encoding, Error, Result};
use core::mem;
use x264::*;
use {Encoder, Encoding, Error, Result};

mod preset;
mod tune;
Expand All @@ -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),
)
});

Expand All @@ -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
}

Expand Down Expand Up @@ -76,43 +73,29 @@ 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
}

/// 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
}

/// 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<C>(
mut self,
csp: C,
width: i32,
height: i32,
) -> Result<Encoder>
pub fn build<C>(mut self, csp: C, width: i32, height: i32) -> Result<Encoder>
where
C: Into<Encoding>,
{
Expand All @@ -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
};
Expand Down