#![allow(non_camel_case_types)]
#![warn(clippy::cargo)]
#![cfg_attr(not(feature = "std"), no_std)]
use core::convert::TryFrom;
use core::fmt;
use core::fmt::{Debug, Display, Formatter};
use core::hash::{Hash, Hasher};
#[cfg(feature = "std")]
use std::string::{String, ToString};
#[cfg(feature = "std")]
use std::error::Error;
pub use as_enum::{DrmFourcc, DrmModifier, DrmVendor};
mod as_enum;
mod consts;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DrmFormat {
pub code: DrmFourcc,
pub modifier: DrmModifier,
}
impl DrmFourcc {
#[cfg(feature = "std")]
#[deprecated(since = "2.2.0", note = "Use `ToString::to_string` instead")]
pub fn string_form(&self) -> String {
self.display_form().to_string()
}
fn display_form(&self) -> impl Display + Debug {
fourcc_display_form(*self as u32).expect("Must be valid fourcc")
}
}
impl Debug for DrmFourcc {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("DrmFourcc")
.field(&self.display_form())
.finish()
}
}
impl Display for DrmFourcc {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.display_form(), f)
}
}
impl TryFrom<u32> for DrmFourcc {
type Error = UnrecognizedFourcc;
#[cfg_attr(feature = "std", doc = "```")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
fn try_from(value: u32) -> Result<Self, Self::Error> {
Self::from_u32(value).ok_or(UnrecognizedFourcc(value))
}
}
#[cfg_attr(feature = "std", doc = "```")]
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UnrecognizedFourcc(pub u32);
impl UnrecognizedFourcc {
#[cfg(feature = "std")]
pub fn string_form(&self) -> Option<String> {
fourcc_string_form(self.0)
}
pub fn display(&self) -> Option<impl Display> {
fourcc_display_form(self.0)
}
}
impl Debug for UnrecognizedFourcc {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut debug = &mut f.debug_tuple("UnrecognizedFourcc");
if let Some(string_form) = fourcc_display_form(self.0) {
debug = debug.field(&string_form);
}
debug.field(&self.0).finish()
}
}
impl Display for UnrecognizedFourcc {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self, f)
}
}
#[cfg(feature = "std")]
impl Error for UnrecognizedFourcc {}
#[cfg(feature = "std")]
fn fourcc_string_form(fourcc: u32) -> Option<String> {
fourcc_display_form(fourcc).map(|val| val.to_string())
}
fn fourcc_display_form(fourcc: u32) -> Option<impl Display + Debug> {
let raw_bytes = fourcc.to_le_bytes();
let mut chars = ::core::str::from_utf8(&raw_bytes).ok()?.chars();
let first = chars.next().unwrap();
let second = chars.next().unwrap();
for char in [first, second].iter().copied() {
if !char.is_ascii_alphanumeric() {
return None;
}
}
let mut bytes = raw_bytes;
for byte in &mut bytes[4 - chars.as_str().len()..] {
if *byte == b'\0' {
*byte = b' ';
}
}
struct FormatFourccRaw {
bytes: [u8; 4],
}
impl Display for FormatFourccRaw {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let chars = ::core::str::from_utf8(&self.bytes[..]).expect("validated previously");
f.write_str(chars)
}
}
impl Debug for FormatFourccRaw {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
Some(FormatFourccRaw { bytes })
}
impl TryFrom<u8> for DrmVendor {
type Error = UnrecognizedVendor;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::from_u8(value).ok_or(UnrecognizedVendor(value))
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UnrecognizedVendor(pub u8);
impl Display for UnrecognizedVendor {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self, f)
}
}
#[cfg(feature = "std")]
impl Error for UnrecognizedVendor {}
impl From<u64> for DrmModifier {
fn from(value: u64) -> Self {
Self::from_u64(value)
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UnrecognizedModifier(pub u64);
impl Display for UnrecognizedModifier {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(&self, f)
}
}
#[cfg(feature = "std")]
impl Error for UnrecognizedModifier {}
impl UnrecognizedModifier {
pub fn vendor(&self) -> Result<Option<DrmVendor>, UnrecognizedVendor> {
let vendor = (self.0 >> 56) as u8;
if vendor == 0 {
Ok(None)
} else {
DrmVendor::try_from(vendor).map(Some)
}
}
}
impl From<DrmModifier> for u64 {
fn from(val: DrmModifier) -> u64 {
val.into_u64()
}
}
impl PartialEq for DrmModifier {
fn eq(&self, other: &Self) -> bool {
self.into_u64() == other.into_u64()
}
}
impl Eq for DrmModifier {}
impl PartialEq<u64> for DrmModifier {
fn eq(&self, other: &u64) -> bool {
&self.into_u64() == other
}
}
impl Hash for DrmModifier {
fn hash<H: Hasher>(&self, state: &mut H) {
self.into_u64().hash(state);
}
}
impl DrmModifier {
pub fn vendor(&self) -> Result<Option<DrmVendor>, UnrecognizedVendor> {
let vendor = (self.into_u64() >> 56) as u8;
if vendor == 0 {
Ok(None)
} else {
DrmVendor::try_from(vendor).map(Some)
}
}
}
#[allow(dead_code)]
pub(crate) mod _fake_ctypes {
pub struct c_uchar;
pub struct c_uint;
pub struct c_ulong;
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn a_specific_var_has_correct_value() {
assert_eq!(consts::DRM_FOURCC_AYUV, 1448433985);
}
#[test]
fn enum_member_casts_to_const() {
assert_eq!(
DrmFourcc::Xrgb8888 as u32,
consts::DRM_FOURCC_XRGB8888 as u32
);
}
#[test]
#[cfg(feature = "std")]
fn enum_member_has_correct_string_format() {
assert_eq!(DrmFourcc::Xrgb8888.to_string(), "XR24");
}
#[test]
#[cfg(feature = "std")]
fn fourcc_string_form_handles_valid() {
assert_eq!(fourcc_string_form(875713112).unwrap(), "XR24");
assert_eq!(fourcc_string_form(828601953).unwrap(), "avc1");
assert_eq!(fourcc_string_form(0x316376).unwrap(), "vc1 ");
}
#[test]
#[cfg(feature = "std")]
fn unrecognized_handles_valid_fourcc() {
assert_eq!(
UnrecognizedFourcc(828601953).to_string(),
"UnrecognizedFourcc(avc1, 828601953)"
);
}
#[test]
#[cfg(feature = "std")]
fn unrecognized_handles_invalid_fourcc() {
assert_eq!(UnrecognizedFourcc(0).to_string(), "UnrecognizedFourcc(0)");
}
#[test]
fn can_clone_result() {
let a = DrmFourcc::try_from(0);
let b = a;
assert_eq!(a, b);
}
}