Make AVOptionType a bitfield/int wrapper (#57)
* Implement option::Type as bitfield struct This type is no longer strictly an enum in the Rust sense because the underlying AVOptionType is now also a bitfield/the AV_OPT_TYPE* consts now include AV_OPT_TYPE_FLAG_ARRAY, which is a bitflag.
This commit is contained in:
parent
5c6922db8c
commit
0fbdfdb371
@ -1045,6 +1045,9 @@ fn main() {
|
|||||||
// We need/want to implement Debug by hand for some types
|
// We need/want to implement Debug by hand for some types
|
||||||
.no_debug("AVChannelLayout")
|
.no_debug("AVChannelLayout")
|
||||||
.no_debug("AVChannelCustom")
|
.no_debug("AVChannelCustom")
|
||||||
|
// In FFmpeg 7.0+, this has bitfield-like behaviour,
|
||||||
|
// so cannot be a "rustified" enum
|
||||||
|
.newtype_enum("AVOptionType")
|
||||||
.allowlist_file(r#".*[/\\]libavutil[/\\].*"#)
|
.allowlist_file(r#".*[/\\]libavutil[/\\].*"#)
|
||||||
.allowlist_file(r#".*[/\\]libavcodec[/\\].*"#)
|
.allowlist_file(r#".*[/\\]libavcodec[/\\].*"#)
|
||||||
.allowlist_file(r#".*[/\\]libavformat[/\\].*"#)
|
.allowlist_file(r#".*[/\\]libavformat[/\\].*"#)
|
||||||
|
@ -1,110 +1,69 @@
|
|||||||
mod traits;
|
mod traits;
|
||||||
pub use self::traits::{Gettable, Iterable, Settable, Target};
|
pub use self::traits::{Gettable, Iterable, Settable, Target};
|
||||||
|
|
||||||
use crate::ffi::AVOptionType::*;
|
|
||||||
use crate::ffi::*;
|
use crate::ffi::*;
|
||||||
|
use libc::c_uint;
|
||||||
#[cfg(feature = "serialize")]
|
#[cfg(feature = "serialize")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
bitflags! {
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
|
||||||
pub enum Type {
|
pub struct Type: c_uint {
|
||||||
Flags,
|
const FLAGS = AVOptionType::AV_OPT_TYPE_FLAGS.0 as c_uint;
|
||||||
Int,
|
const INT = AVOptionType::AV_OPT_TYPE_INT.0 as c_uint;
|
||||||
Int64,
|
const INT64 = AVOptionType::AV_OPT_TYPE_INT64.0 as c_uint;
|
||||||
Double,
|
const DOUBLE = AVOptionType::AV_OPT_TYPE_DOUBLE.0 as c_uint;
|
||||||
Float,
|
const FLOAT = AVOptionType::AV_OPT_TYPE_FLOAT.0 as c_uint;
|
||||||
String,
|
const STRING = AVOptionType::AV_OPT_TYPE_STRING.0 as c_uint;
|
||||||
Rational,
|
const RATIONAL = AVOptionType::AV_OPT_TYPE_RATIONAL.0 as c_uint;
|
||||||
Binary,
|
/// `offset` must point to a pointer immediately followed by an int
|
||||||
Dictionary,
|
/// for the length.
|
||||||
Constant,
|
const BINARY = AVOptionType::AV_OPT_TYPE_BINARY.0 as c_uint;
|
||||||
|
const DICTIONARY = AVOptionType::AV_OPT_TYPE_DICT.0 as c_uint;
|
||||||
ImageSize,
|
const CONSTANT = AVOptionType::AV_OPT_TYPE_CONST.0 as c_uint;
|
||||||
PixelFormat,
|
/// `offset` must point to two consecutive ints
|
||||||
SampleFormat,
|
const IMAGE_SIZE = AVOptionType::AV_OPT_TYPE_IMAGE_SIZE.0 as c_uint;
|
||||||
VideoRate,
|
const PIXEL_FORMAT = AVOptionType::AV_OPT_TYPE_PIXEL_FMT.0 as c_uint;
|
||||||
Duration,
|
const SAMPLE_FORMAT = AVOptionType::AV_OPT_TYPE_SAMPLE_FMT.0 as c_uint;
|
||||||
Color,
|
/// `offset` must point to AVRational
|
||||||
|
const VIDEO_RATE = AVOptionType::AV_OPT_TYPE_VIDEO_RATE.0 as c_uint;
|
||||||
|
const DURATION = AVOptionType::AV_OPT_TYPE_DURATION.0 as c_uint;
|
||||||
|
const COLOR = AVOptionType::AV_OPT_TYPE_COLOR.0 as c_uint;
|
||||||
#[cfg(not(feature = "ffmpeg_7_0"))]
|
#[cfg(not(feature = "ffmpeg_7_0"))]
|
||||||
ChannelLayout,
|
const CHANNEL_LAYOUT = AVOptionType::AV_OPT_TYPE_CHANNEL_LAYOUT.0 as c_uint;
|
||||||
#[cfg(feature = "ffmpeg_5_1")]
|
#[cfg(feature = "ffmpeg_5_1")]
|
||||||
ChLayout,
|
const CHLAYOUT = AVOptionType::AV_OPT_TYPE_CHLAYOUT.0 as c_uint;
|
||||||
c_ulong,
|
const C_ULONG = AVOptionType::AV_OPT_TYPE_UINT64.0 as c_uint;
|
||||||
bool,
|
const BOOL = AVOptionType::AV_OPT_TYPE_BOOL.0 as c_uint;
|
||||||
|
|
||||||
// FIXME: This is not supported yet. `enum Type` should become a bitflags struct.
|
/// May be combined with another regular option type to declare an
|
||||||
// FIXME: AVOptionType is also not technically an enum because it may contain
|
/// array option.
|
||||||
// values that are outside the defined enum variants!
|
///
|
||||||
|
/// For array options, `AVOption.offset` should refer to a pointer
|
||||||
|
/// corresponding to the option type. The pointer should be immediately
|
||||||
|
/// followed by an unsigned int that will store the number of elements
|
||||||
|
/// in the array.
|
||||||
#[cfg(feature = "ffmpeg_7_0")]
|
#[cfg(feature = "ffmpeg_7_0")]
|
||||||
ArrayFlag = 0x10000,
|
const FLAG_ARRAY = AVOptionType::AV_OPT_TYPE_FLAG_ARRAY.0 as c_uint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Type {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<AVOptionType> for Type {
|
impl From<AVOptionType> for Type {
|
||||||
fn from(value: AVOptionType) -> Self {
|
fn from(value: AVOptionType) -> Self {
|
||||||
match value {
|
Self::from_bits_retain(value.0 as c_uint)
|
||||||
AV_OPT_TYPE_FLAGS => Type::Flags,
|
|
||||||
AV_OPT_TYPE_INT => Type::Int,
|
|
||||||
AV_OPT_TYPE_INT64 => Type::Int64,
|
|
||||||
AV_OPT_TYPE_DOUBLE => Type::Double,
|
|
||||||
AV_OPT_TYPE_FLOAT => Type::Float,
|
|
||||||
AV_OPT_TYPE_STRING => Type::String,
|
|
||||||
AV_OPT_TYPE_RATIONAL => Type::Rational,
|
|
||||||
AV_OPT_TYPE_BINARY => Type::Binary,
|
|
||||||
AV_OPT_TYPE_DICT => Type::Dictionary,
|
|
||||||
AV_OPT_TYPE_CONST => Type::Constant,
|
|
||||||
AV_OPT_TYPE_UINT64 => Type::c_ulong,
|
|
||||||
AV_OPT_TYPE_BOOL => Type::bool,
|
|
||||||
|
|
||||||
AV_OPT_TYPE_IMAGE_SIZE => Type::ImageSize,
|
|
||||||
AV_OPT_TYPE_PIXEL_FMT => Type::PixelFormat,
|
|
||||||
AV_OPT_TYPE_SAMPLE_FMT => Type::SampleFormat,
|
|
||||||
AV_OPT_TYPE_VIDEO_RATE => Type::VideoRate,
|
|
||||||
AV_OPT_TYPE_DURATION => Type::Duration,
|
|
||||||
AV_OPT_TYPE_COLOR => Type::Color,
|
|
||||||
#[cfg(not(feature = "ffmpeg_7_0"))]
|
|
||||||
AV_OPT_TYPE_CHANNEL_LAYOUT => Type::ChannelLayout,
|
|
||||||
#[cfg(feature = "ffmpeg_5_1")]
|
|
||||||
AV_OPT_TYPE_CHLAYOUT => Type::ChLayout,
|
|
||||||
|
|
||||||
#[cfg(feature = "ffmpeg_7_0")]
|
|
||||||
AV_OPT_TYPE_FLAG_ARRAY => Type::ArrayFlag,
|
|
||||||
|
|
||||||
#[cfg(feature = "non-exhaustive-enums")]
|
|
||||||
_ => unimplemented!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Type> for AVOptionType {
|
impl From<Type> for AVOptionType {
|
||||||
fn from(value: Type) -> AVOptionType {
|
fn from(value: Type) -> AVOptionType {
|
||||||
match value {
|
// cast to whichever type the C enum uses
|
||||||
Type::Flags => AV_OPT_TYPE_FLAGS,
|
AVOptionType(value.bits() as _)
|
||||||
Type::Int => AV_OPT_TYPE_INT,
|
|
||||||
Type::Int64 => AV_OPT_TYPE_INT64,
|
|
||||||
Type::Double => AV_OPT_TYPE_DOUBLE,
|
|
||||||
Type::Float => AV_OPT_TYPE_FLOAT,
|
|
||||||
Type::String => AV_OPT_TYPE_STRING,
|
|
||||||
Type::Rational => AV_OPT_TYPE_RATIONAL,
|
|
||||||
Type::Binary => AV_OPT_TYPE_BINARY,
|
|
||||||
Type::Dictionary => AV_OPT_TYPE_DICT,
|
|
||||||
Type::Constant => AV_OPT_TYPE_CONST,
|
|
||||||
Type::c_ulong => AV_OPT_TYPE_UINT64,
|
|
||||||
Type::bool => AV_OPT_TYPE_BOOL,
|
|
||||||
|
|
||||||
Type::ImageSize => AV_OPT_TYPE_IMAGE_SIZE,
|
|
||||||
Type::PixelFormat => AV_OPT_TYPE_PIXEL_FMT,
|
|
||||||
Type::SampleFormat => AV_OPT_TYPE_SAMPLE_FMT,
|
|
||||||
Type::VideoRate => AV_OPT_TYPE_VIDEO_RATE,
|
|
||||||
Type::Duration => AV_OPT_TYPE_DURATION,
|
|
||||||
Type::Color => AV_OPT_TYPE_COLOR,
|
|
||||||
#[cfg(not(feature = "ffmpeg_7_0"))]
|
|
||||||
Type::ChannelLayout => AV_OPT_TYPE_CHANNEL_LAYOUT,
|
|
||||||
#[cfg(feature = "ffmpeg_5_1")]
|
|
||||||
Type::ChLayout => AV_OPT_TYPE_CHLAYOUT,
|
|
||||||
|
|
||||||
#[cfg(feature = "ffmpeg_7_0")]
|
|
||||||
Type::ArrayFlag => AV_OPT_TYPE_FLAG_ARRAY,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user