Skip to content

Commit

Permalink
Add AV1 encoding function, C-API
Browse files Browse the repository at this point in the history
  • Loading branch information
quietvoid committed Oct 7, 2023
1 parent 5717cab commit efb0f48
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dolby_vision/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased
- Added `write_av1_rpu_metadata_obu_t35_complete` function to encode RPUs in complete metadata OBU payloads.

C API:
- Added `dovi_write_av1_rpu_metadata_obu_t35_{payload,complete}` functions.

## 3.2.0
- Deprecated `RpuDataHeader.rpu_nal_prefix`.
- Added `av1` module for handling AV1 Dolby Vision ITU-T T.35 metadata OBU payloads.
Expand Down
58 changes: 58 additions & 0 deletions dolby_vision/src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,61 @@ pub unsafe extern "C" fn dovi_rpu_remove_mapping(ptr: *mut RpuOpaque) -> i32 {
-1
}
}

/// # Safety
/// The struct pointer must be valid.
///
/// Writes the encoded RPU as `itu_t_t35_payload_bytes` for AV1 ITU-T T.35 metadata OBU
/// If an error occurs in the writing, it is logged to RpuOpaque.error
#[no_mangle]
pub unsafe extern "C" fn dovi_write_av1_rpu_metadata_obu_t35_payload(
ptr: *mut RpuOpaque,
) -> *const Data {
if ptr.is_null() {
return null_mut();
}

let opaque = &mut *ptr;

if let Some(rpu) = &opaque.rpu {
match rpu.write_av1_rpu_metadata_obu_t35_payload() {
Ok(buf) => Box::into_raw(Box::new(Data::from(buf))),
Err(e) => {
opaque.error =
Some(CString::new(format!("Failed writing byte buffer: {e}")).unwrap());
null_mut()
}
}
} else {
null_mut()
}
}

/// # Safety
/// The struct pointer must be valid.
///
/// Writes the encoded RPU a complete AV1 `metadata_itut_t35()` OBU
/// If an error occurs in the writing, it is logged to RpuOpaque.error
#[no_mangle]
pub unsafe extern "C" fn dovi_write_av1_rpu_metadata_obu_t35_complete(
ptr: *mut RpuOpaque,
) -> *const Data {
if ptr.is_null() {
return null_mut();
}

let opaque = &mut *ptr;

if let Some(rpu) = &opaque.rpu {
match rpu.write_av1_rpu_metadata_obu_t35_complete() {
Ok(buf) => Box::into_raw(Box::new(Data::from(buf))),
Err(e) => {
opaque.error =
Some(CString::new(format!("Failed writing byte buffer: {e}")).unwrap());
null_mut()
}
}
} else {
null_mut()
}
}
9 changes: 9 additions & 0 deletions dolby_vision/src/rpu/dovi_rpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,22 @@ impl DoviRpu {
self.write_rpu_data()
}

/// `itu_t_t35_payload_bytes`
pub fn write_av1_rpu_metadata_obu_t35_payload(&self) -> Result<Vec<u8>> {
let mut encoded_rpu = self.write_rpu_data()?;
convert_regular_rpu_to_av1_payload(encoded_rpu.as_mut())?;

Ok(encoded_rpu)
}

/// Complete `metadata_itut_t35()`, including `itu_t_t35_country_code`
pub fn write_av1_rpu_metadata_obu_t35_complete(&self) -> Result<Vec<u8>> {
let mut encoded_rpu = self.write_av1_rpu_metadata_obu_t35_payload()?;
encoded_rpu.insert(0, 0xB5);

Ok(encoded_rpu)
}

#[inline(always)]
fn write_rpu_data(&self) -> Result<Vec<u8>> {
// Capacity is in bits
Expand Down

0 comments on commit efb0f48

Please sign in to comment.