Skip to content

Commit

Permalink
Port C code to Rust
Browse files Browse the repository at this point in the history
Update test program to check API version.
  • Loading branch information
Frank Bossen authored and fbossen committed Mar 1, 2024
1 parent b4eb216 commit ed341e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ pub unsafe extern "C" fn dav1d_version() -> *const c_char {
rav1d_version().as_ptr().cast()
}

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

/// Get the `dav1d` library C API version.
///
/// Return a value in the format `0x00XXYYZZ`, where `XX` is the major version,
/// `YY` the minor version, and `ZZ` the patch version.
#[no_mangle]
#[cold]
pub extern "C" fn dav1d_version_api() -> c_uint {
u32::from_be_bytes([
0,
DAV1D_API_VERSION_MAJOR,
DAV1D_API_VERSION_MINOR,
DAV1D_API_VERSION_PATCH,
])
}

impl Default for Rav1dSettings {
fn default() -> Self {
Self {
Expand Down
20 changes: 14 additions & 6 deletions tools/dav1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use libc::malloc;
use libc::memset;
use libc::ptrdiff_t;
use libc::snprintf;
use libc::strcmp;
use libc::strcpy;
use libc::strerror;
use libc::EAGAIN;
Expand Down Expand Up @@ -83,6 +82,10 @@ use rav1d::src::lib::dav1d_open;
use rav1d::src::lib::dav1d_parse_sequence_header;
use rav1d::src::lib::dav1d_send_data;
use rav1d::src::lib::dav1d_version;
use rav1d::src::lib::dav1d_version_api;
use rav1d::src::lib::DAV1D_API_VERSION_MAJOR;
use rav1d::src::lib::DAV1D_API_VERSION_MINOR;
use rav1d::src::lib::DAV1D_API_VERSION_PATCH;
use rav1d::Dav1dResult;
use std::ffi::c_char;
use std::ffi::c_double;
Expand Down Expand Up @@ -334,13 +337,18 @@ unsafe fn main_0(argc: c_int, argv: *const *mut c_char) -> c_int {
let mut elapsed: u64 = 0;
let i_fps: c_double;
let mut frametimes: *mut libc::FILE = 0 as *mut libc::FILE;
let version: *const c_char = dav1d_version();
if strcmp(version, b"966d63c1\0" as *const u8 as *const c_char) != 0 {
let [_, major, minor, patch] = dav1d_version_api().to_be_bytes();
if DAV1D_API_VERSION_MAJOR != major || DAV1D_API_VERSION_MINOR > minor {
fprintf(
stderr,
b"Version mismatch (library: %s, executable: %s)\n\0" as *const u8 as *const c_char,
version,
b"966d63c1\0" as *const u8 as *const c_char,
b"Version mismatch (library: %d.%d.%d, executable: %d.%d.%d)\n\0" as *const u8
as *const c_char,
major as c_int,
minor as c_int,
patch as c_int,
DAV1D_API_VERSION_MAJOR as c_int,
DAV1D_API_VERSION_MINOR as c_int,
DAV1D_API_VERSION_PATCH as c_int,
);
return 1 as c_int;
}
Expand Down

0 comments on commit ed341e1

Please sign in to comment.