Skip to content

Commit

Permalink
Use C-string literals for EL type in C-API
Browse files Browse the repository at this point in the history
  • Loading branch information
quietvoid committed Aug 17, 2024
1 parent 8ef53f2 commit 348d36d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
12 changes: 1 addition & 11 deletions dolby_vision/src/c_structs/rpu_data_header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use libc::c_char;
use std::{ffi::CString, ptr::null};
use std::ptr::null;

use crate::rpu::rpu_data_header::RpuDataHeader as RuRpuDataHeader;

Expand Down Expand Up @@ -43,16 +43,6 @@ pub struct RpuDataHeader {
prev_vdr_rpu_id: u64,
}

impl RpuDataHeader {
/// # Safety
/// The buffer pointers should be valid.
pub unsafe fn free(&self) {
if !self.el_type.is_null() {
drop(CString::from_raw(self.el_type as *mut c_char));
}
}
}

impl From<&RuRpuDataHeader> for RpuDataHeader {
fn from(header: &RuRpuDataHeader) -> Self {
#[allow(deprecated)]
Expand Down
19 changes: 18 additions & 1 deletion dolby_vision/src/c_structs/rpu_data_nlq.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use crate::rpu::{rpu_data_nlq::RpuDataNlq as RuRpuDataNlq, NUM_COMPONENTS};
use std::ffi::CStr;

use crate::rpu::{
rpu_data_nlq::{DoviELType, RpuDataNlq as RuRpuDataNlq},
NUM_COMPONENTS,
};

const FEL_CSTR: &CStr = c"FEL";
const MEL_CSTR: &CStr = c"MEL";

/// C struct for rpu_data_nlq()
#[repr(C)]
Expand All @@ -12,6 +20,15 @@ pub struct RpuDataNlq {
linear_deadzone_threshold: [u64; NUM_COMPONENTS],
}

impl DoviELType {
pub const fn as_cstr(&self) -> &'static CStr {
match self {
DoviELType::MEL => MEL_CSTR,
DoviELType::FEL => FEL_CSTR,
}
}
}

impl From<&RuRpuDataNlq> for RpuDataNlq {
fn from(data: &RuRpuDataNlq) -> Self {
Self {
Expand Down
5 changes: 2 additions & 3 deletions dolby_vision/src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub unsafe extern "C" fn dovi_rpu_get_header(ptr: *const RpuOpaque) -> *const Rp
let mut header = RpuDataHeader::from(&rpu.header);

if let Some(el_type) = rpu.el_type.as_ref() {
header.el_type = CString::new(el_type.as_str()).unwrap().into_raw();
header.el_type = el_type.as_cstr().as_ptr();
}

Box::into_raw(Box::new(header))
Expand All @@ -230,8 +230,7 @@ pub unsafe extern "C" fn dovi_rpu_get_header(ptr: *const RpuOpaque) -> *const Rp
#[no_mangle]
pub unsafe extern "C" fn dovi_rpu_free_header(ptr: *const RpuDataHeader) {
if !ptr.is_null() {
let header = Box::from_raw(ptr as *mut RpuDataHeader);
header.free();
drop(Box::from_raw(ptr as *mut RpuDataHeader));
}
}

Expand Down

0 comments on commit 348d36d

Please sign in to comment.