Skip to content

Commit

Permalink
Port C code to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Bossen committed Mar 4, 2024
1 parent 5fd0757 commit 6aa8d48
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
24 changes: 20 additions & 4 deletions include/dav1d/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ pub struct Dav1dPicture {
pub content_light: Option<NonNull<Rav1dContentLightLevel>>,
pub mastering_display: Option<NonNull<Rav1dMasteringDisplay>>,
pub itut_t35: Option<NonNull<Dav1dITUTT35>>,
pub reserved: [uintptr_t; 4],
pub n_itut_t35: usize,
pub reserved: [uintptr_t; 3],
pub frame_hdr_ref: Option<RawArc<DRav1d<Rav1dFrameHeader, Dav1dFrameHeader>>>, // opaque, so we can change this
pub seq_hdr_ref: Option<RawArc<DRav1d<Rav1dSequenceHeader, Dav1dSequenceHeader>>>, // opaque, so we can change this
pub content_light_ref: Option<RawArc<Rav1dContentLightLevel>>, // opaque, so we can change this
pub mastering_display_ref: Option<RawArc<Rav1dMasteringDisplay>>, // opaque, so we can change this
pub itut_t35_ref: Option<RawArc<DRav1d<Rav1dITUTT35, Dav1dITUTT35>>>, // opaque, so we can change this
pub itut_t35_ref: Option<RawArc<DRav1d<Vec<Rav1dITUTT35>, Vec<Dav1dITUTT35>>>>, // opaque, so we can change this
pub reserved_ref: [uintptr_t; 4],
pub r#ref: Option<NonNull<Dav1dRef>>,
pub allocator_data: Option<NonNull<c_void>>,
Expand Down Expand Up @@ -126,7 +127,7 @@ pub(crate) struct Rav1dPicture {
pub m: Rav1dDataProps,
pub content_light: Option<Arc<Rav1dContentLightLevel>>,
pub mastering_display: Option<Arc<Rav1dMasteringDisplay>>,
pub itut_t35: Option<Arc<DRav1d<Rav1dITUTT35, Dav1dITUTT35>>>,
pub itut_t35: Option<Arc<DRav1d<Vec<Rav1dITUTT35>, Vec<Dav1dITUTT35>>>>,
pub r#ref: Option<NonNull<Rav1dRef>>,
}

Expand All @@ -142,6 +143,7 @@ impl From<Dav1dPicture> for Rav1dPicture {
content_light: _,
mastering_display: _,
itut_t35: _,
n_itut_t35: _,
reserved: _,
frame_hdr_ref,
seq_hdr_ref,
Expand Down Expand Up @@ -208,7 +210,21 @@ impl From<Rav1dPicture> for Dav1dPicture {
content_light: content_light.as_ref().map(|arc| arc.as_ref().into()),
mastering_display: mastering_display.as_ref().map(|arc| arc.as_ref().into()),
// [`DRav1d::from_rav1d`] is called in [`rav1d_parse_obus`].
itut_t35: itut_t35.as_ref().map(|arc| (&arc.as_ref().dav1d).into()),
itut_t35: if itut_t35.is_none() {
None
} else {
let p = itut_t35.as_ref().unwrap().dav1d.as_ptr();
if p.is_null() {
None
} else {
unsafe { Some(NonNull::<Dav1dITUTT35>::from(&*p)) }
}
},
n_itut_t35: if itut_t35.is_none() {
0
} else {
itut_t35.as_ref().unwrap().len()
},
reserved: Default::default(),
frame_hdr_ref: frame_hdr.map(RawArc::from_arc),
seq_hdr_ref: seq_hdr.map(RawArc::from_arc),
Expand Down
2 changes: 1 addition & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub struct Rav1dContext {
pub(crate) frame_hdr: Option<Arc<DRav1d<Rav1dFrameHeader, Dav1dFrameHeader>>>, // TODO(kkysen) Previously pooled.
pub(crate) content_light: Option<Arc<Rav1dContentLightLevel>>,
pub(crate) mastering_display: Option<Arc<Rav1dMasteringDisplay>>,
pub(crate) itut_t35: Option<Arc<DRav1d<Rav1dITUTT35, Dav1dITUTT35>>>,
pub(crate) itut_t35: Option<Arc<DRav1d<Vec<Rav1dITUTT35>, Vec<Dav1dITUTT35>>>>,

// decoded output picture queue
pub(crate) in_0: Rav1dData,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub unsafe extern "C" fn dav1d_version() -> *const c_char {
}

pub const DAV1D_API_VERSION_MAJOR: u8 = 6;
pub const DAV1D_API_VERSION_MINOR: u8 = 8;
pub const DAV1D_API_VERSION_MINOR: u8 = 9;
pub const DAV1D_API_VERSION_PATCH: u8 = 0;

/// Get the `dav1d` library C API version.
Expand Down
20 changes: 18 additions & 2 deletions src/obu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::include::dav1d::common::Rav1dDataProps;
use crate::include::dav1d::data::Rav1dData;
use crate::include::dav1d::dav1d::Rav1dDecodeFrameType;
use crate::include::dav1d::headers::DRav1d;
use crate::include::dav1d::headers::Dav1dITUTT35;
use crate::include::dav1d::headers::Rav1dAdaptiveBoolean;
use crate::include::dav1d::headers::Rav1dChromaSamplePosition;
use crate::include::dav1d::headers::Rav1dColorPrimaries;
Expand Down Expand Up @@ -2425,11 +2426,26 @@ unsafe fn parse_obus(
let country_code_extension_byte = country_code_extension_byte as u8;
let payload = (0..payload_size).map(|_| gb.get_bits(8) as u8).collect(); // TODO(kkysen) fallible allocation

c.itut_t35 = Some(Arc::new(DRav1d::from_rav1d(Rav1dITUTT35 {
let mut itut_t35 = DRav1d::<Vec<Rav1dITUTT35>, Vec<Dav1dITUTT35>> {
rav1d: Vec::new(),
dav1d: Vec::new(),
};
if c.itut_t35.is_some() {
for x in (**c.itut_t35.as_ref().unwrap()).rav1d.iter() {
itut_t35.dav1d.push(Dav1dITUTT35::from(x.clone()));
itut_t35.rav1d.push(x.clone());
}
}

let e = Rav1dITUTT35 {
country_code,
country_code_extension_byte,
payload,
}))); // TODO(kkysen) fallible allocation
};

itut_t35.dav1d.push(Dav1dITUTT35::from(e.clone()));
itut_t35.rav1d.push(e);
c.itut_t35 = Some(Arc::new(itut_t35));
}
}
Some(ObuMetaType::Scalability | ObuMetaType::Timecode) => {} // Ignore metadata OBUs we don't care about.
Expand Down
33 changes: 19 additions & 14 deletions src/picture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,20 +231,15 @@ unsafe fn picture_alloc_with_edges(
h: c_int,
seq_hdr: Option<Arc<DRav1d<Rav1dSequenceHeader, Dav1dSequenceHeader>>>,
frame_hdr: Option<Arc<DRav1d<Rav1dFrameHeader, Dav1dFrameHeader>>>,
content_light: Option<Arc<Rav1dContentLightLevel>>,
mastering_display: Option<Arc<Rav1dMasteringDisplay>>,
itut_t35: Option<Arc<DRav1d<Rav1dITUTT35, Dav1dITUTT35>>>,
bpc: c_int,
props: Rav1dDataProps,
p_allocator: &Rav1dPicAllocator,
) -> Rav1dResult {
if !p.data.data[0].is_null() {
writeln!(logger, "Picture already allocated!",);
return Err(EGeneric);
}
assert!(bpc > 0 && bpc <= 16);
let mut pic = p_allocator.alloc_picture_data(w, h, seq_hdr.unwrap(), frame_hdr)?;
rav1d_picture_copy_props(&mut pic, content_light, mastering_display, itut_t35, props);
let pic = p_allocator.alloc_picture_data(w, h, seq_hdr.unwrap(), frame_hdr)?;
*p = pic;

Ok(())
Expand All @@ -254,7 +249,7 @@ pub fn rav1d_picture_copy_props(
p: &mut Rav1dPicture,
content_light: Option<Arc<Rav1dContentLightLevel>>,
mastering_display: Option<Arc<Rav1dMasteringDisplay>>,
itut_t35: Option<Arc<DRav1d<Rav1dITUTT35, Dav1dITUTT35>>>,
itut_t35: Option<Arc<DRav1d<Vec<Rav1dITUTT35>, Vec<Dav1dITUTT35>>>>,
props: Rav1dDataProps,
) {
p.m = props;
Expand All @@ -269,7 +264,7 @@ pub(crate) unsafe fn rav1d_thread_picture_alloc(
c: &Rav1dContext,
f: &mut Rav1dFrameData,
bpc: c_int,
itut_t35: Option<Arc<DRav1d<Rav1dITUTT35, Dav1dITUTT35>>>,
itut_t35: Option<Arc<DRav1d<Vec<Rav1dITUTT35>, Vec<Dav1dITUTT35>>>>,
) -> Rav1dResult {
let p = &mut f.sr_cur;
let have_frame_mt = c.n_fc > 1;
Expand All @@ -281,13 +276,18 @@ pub(crate) unsafe fn rav1d_thread_picture_alloc(
frame_hdr.size.height,
f.seq_hdr.clone(),
f.frame_hdr.clone(),
bpc,
&c.allocator,
)?;

rav1d_picture_copy_props(
&mut p.p,
c.content_light.clone(),
c.mastering_display.clone(),
itut_t35,
bpc,
f.tiles[0].data.m.clone(),
&c.allocator,
)?;
);

let flags_mask = if frame_hdr.show_frame != 0 || c.output_invisible_frames {
PictureFlags::empty()
} else {
Expand Down Expand Up @@ -319,13 +319,18 @@ pub(crate) unsafe fn rav1d_picture_alloc_copy(
src.p.h,
src.seq_hdr.clone(),
src.frame_hdr.clone(),
src.p.bpc,
&mut (*pic_ctx).allocator,
)?;

rav1d_picture_copy_props(
dst,
src.content_light.clone(),
src.mastering_display.clone(),
src.itut_t35.clone(),
src.p.bpc,
src.m.clone(),
&mut (*pic_ctx).allocator,
)
);
Ok(())
}

pub(crate) unsafe fn rav1d_picture_ref(dst: &mut Rav1dPicture, src: &Rav1dPicture) {
Expand Down

0 comments on commit 6aa8d48

Please sign in to comment.