Skip to content

Commit 6722d75

Browse files
committed
Make rav1e_status_to_str return a static string
1 parent 096ff44 commit 6722d75

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/capi.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ pub enum EncoderStatus {
100100
NotReady = -2,
101101
}
102102

103+
impl EncoderStatus {
104+
fn to_c(&self) -> *const u8 {
105+
use self::EncoderStatus::*;
106+
match self {
107+
Success => "Normal operation\0".as_ptr(),
108+
NeedMoreData => "The encoder needs more data to produce an output packet\0".as_ptr(),
109+
EnoughData => "There are enough frames in the queue\0".as_ptr(),
110+
LimitReached => "The encoder has already produced the number of frames requested\0".as_ptr(),
111+
Encoded => "A Frame had been encoded but not emitted yet\0".as_ptr(),
112+
Failure => "Generic fatal error\0".as_ptr(),
113+
NotReady => "First-pass stats data not retrieved or not enough second-pass data provided\0".as_ptr(),
114+
}
115+
}
116+
}
117+
103118
impl From<Option<rav1e::EncoderStatus>> for EncoderStatus {
104119
fn from(status: Option<rav1e::EncoderStatus>) -> Self {
105120
match status {
@@ -701,28 +716,17 @@ pub unsafe extern fn rav1e_last_status(ctx: *const Context) -> EncoderStatus {
701716
(*ctx).last_err.into()
702717
}
703718

704-
/// Return a string matching the EncoderStatus variant.
719+
/// Return a static string matching the EncoderStatus variant.
705720
///
706-
/// Must be freed with free().
707721
#[no_mangle]
708722
pub unsafe extern fn rav1e_status_to_str(
709723
status: EncoderStatus,
710-
) -> *mut c_char {
724+
) -> *const c_char {
711725
if EncoderStatus::from_i32(std::mem::transmute(status)).is_none() {
712-
return std::ptr::null_mut();
713-
}
714-
715-
let status = format!("{:?}", status);
716-
let cbuf = CString::new(status).unwrap();
717-
let len = cbuf.as_bytes_with_nul().len();
718-
let ret = libc::malloc(len);
719-
720-
if !ret.is_null() {
721-
let cptr = cbuf.as_ptr() as *const libc::c_void;
722-
libc::memcpy(ret, cptr, len);
726+
return std::ptr::null();
723727
}
724728

725-
ret as *mut c_char
729+
status.to_c() as *const c_char
726730
}
727731

728732
/// Receive encoded data

0 commit comments

Comments
 (0)