2017-08-10 21:41:55 +00:00
|
|
|
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
use gst_video_sys;
|
2017-08-10 21:41:55 +00:00
|
|
|
|
2020-05-28 10:57:35 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2017-08-10 21:41:55 +00:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::fmt;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
use glib::translate::{from_glib, FromGlib, ToGlib, ToGlibPtr};
|
|
|
|
|
2020-05-28 10:57:35 +00:00
|
|
|
#[cfg(feature = "v1_18")]
|
|
|
|
pub static VIDEO_FORMATS_ALL: Lazy<Box<[::VideoFormat]>> = Lazy::new(|| unsafe {
|
|
|
|
let mut len: u32 = 0;
|
|
|
|
let mut res = Vec::with_capacity(len as usize);
|
|
|
|
let formats = gst_video_sys::gst_video_formats_raw(&mut len);
|
|
|
|
for i in 0..len {
|
|
|
|
let format = formats.offset(i as isize);
|
|
|
|
res.push(from_glib(*format));
|
|
|
|
}
|
|
|
|
res.into_boxed_slice()
|
|
|
|
});
|
|
|
|
|
|
|
|
#[cfg(not(feature = "v1_18"))]
|
|
|
|
pub static VIDEO_FORMATS_ALL: Lazy<Box<[::VideoFormat]>> = Lazy::new(|| {
|
|
|
|
Box::new([
|
|
|
|
::VideoFormat::I420,
|
|
|
|
::VideoFormat::Yv12,
|
|
|
|
::VideoFormat::Yuy2,
|
|
|
|
::VideoFormat::Uyvy,
|
|
|
|
::VideoFormat::Ayuv,
|
|
|
|
::VideoFormat::Vuya,
|
|
|
|
::VideoFormat::Rgbx,
|
|
|
|
::VideoFormat::Bgrx,
|
|
|
|
::VideoFormat::Xrgb,
|
|
|
|
::VideoFormat::Xbgr,
|
|
|
|
::VideoFormat::Rgba,
|
|
|
|
::VideoFormat::Bgra,
|
|
|
|
::VideoFormat::Argb,
|
|
|
|
::VideoFormat::Abgr,
|
|
|
|
::VideoFormat::Rgb,
|
|
|
|
::VideoFormat::Bgr,
|
|
|
|
::VideoFormat::Y41b,
|
|
|
|
::VideoFormat::Y42b,
|
|
|
|
::VideoFormat::Yvyu,
|
|
|
|
::VideoFormat::Y444,
|
|
|
|
::VideoFormat::V210,
|
|
|
|
::VideoFormat::V216,
|
|
|
|
::VideoFormat::Y210,
|
|
|
|
::VideoFormat::Y410,
|
|
|
|
::VideoFormat::Nv12,
|
|
|
|
::VideoFormat::Nv21,
|
|
|
|
::VideoFormat::Gray8,
|
|
|
|
::VideoFormat::Gray16Be,
|
|
|
|
::VideoFormat::Gray16Le,
|
|
|
|
::VideoFormat::V308,
|
|
|
|
::VideoFormat::Rgb16,
|
|
|
|
::VideoFormat::Bgr16,
|
|
|
|
::VideoFormat::Rgb15,
|
|
|
|
::VideoFormat::Bgr15,
|
|
|
|
::VideoFormat::Uyvp,
|
|
|
|
::VideoFormat::A420,
|
|
|
|
::VideoFormat::Rgb8p,
|
|
|
|
::VideoFormat::Yuv9,
|
|
|
|
::VideoFormat::Yvu9,
|
|
|
|
::VideoFormat::Iyu1,
|
|
|
|
::VideoFormat::Argb64,
|
|
|
|
::VideoFormat::Ayuv64,
|
|
|
|
::VideoFormat::R210,
|
|
|
|
::VideoFormat::I42010be,
|
|
|
|
::VideoFormat::I42010le,
|
|
|
|
::VideoFormat::I42210be,
|
|
|
|
::VideoFormat::I42210le,
|
|
|
|
::VideoFormat::Y44410be,
|
|
|
|
::VideoFormat::Y44410le,
|
|
|
|
::VideoFormat::Gbr,
|
|
|
|
::VideoFormat::Gbr10be,
|
|
|
|
::VideoFormat::Gbr10le,
|
|
|
|
::VideoFormat::Nv16,
|
|
|
|
::VideoFormat::Nv24,
|
|
|
|
::VideoFormat::Nv1264z32,
|
|
|
|
::VideoFormat::A42010be,
|
|
|
|
::VideoFormat::A42010le,
|
|
|
|
::VideoFormat::A42210be,
|
|
|
|
::VideoFormat::A42210le,
|
|
|
|
::VideoFormat::A44410be,
|
|
|
|
::VideoFormat::A44410le,
|
|
|
|
::VideoFormat::Nv61,
|
|
|
|
::VideoFormat::P01010be,
|
|
|
|
::VideoFormat::P01010le,
|
|
|
|
::VideoFormat::Iyu2,
|
|
|
|
::VideoFormat::Vyuy,
|
|
|
|
::VideoFormat::Gbra,
|
|
|
|
::VideoFormat::Gbra10be,
|
|
|
|
::VideoFormat::Gbra10le,
|
|
|
|
::VideoFormat::Bgr10a2Le,
|
|
|
|
::VideoFormat::Rgb10a2Le,
|
|
|
|
::VideoFormat::Gbr12be,
|
|
|
|
::VideoFormat::Gbr12le,
|
|
|
|
::VideoFormat::Gbra12be,
|
|
|
|
::VideoFormat::Gbra12le,
|
|
|
|
::VideoFormat::P012Be,
|
|
|
|
::VideoFormat::P012Le,
|
|
|
|
::VideoFormat::I42012be,
|
|
|
|
::VideoFormat::I42012le,
|
|
|
|
::VideoFormat::Y212Be,
|
|
|
|
::VideoFormat::Y212Le,
|
|
|
|
::VideoFormat::I42212be,
|
|
|
|
::VideoFormat::I42212le,
|
|
|
|
::VideoFormat::Y412Be,
|
|
|
|
::VideoFormat::Y412Le,
|
|
|
|
::VideoFormat::Y44412be,
|
|
|
|
::VideoFormat::Y44412le,
|
|
|
|
::VideoFormat::Gray10Le32,
|
|
|
|
::VideoFormat::Nv1210le32,
|
|
|
|
::VideoFormat::Nv1610le32,
|
|
|
|
::VideoFormat::Nv1210le40,
|
|
|
|
::VideoFormat::Y44416be,
|
|
|
|
::VideoFormat::Y44416le,
|
|
|
|
::VideoFormat::P016Be,
|
|
|
|
::VideoFormat::P016Le,
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
2017-11-26 21:50:39 +00:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
|
2017-08-10 21:41:55 +00:00
|
|
|
pub enum VideoEndianness {
|
|
|
|
Unknown,
|
|
|
|
LittleEndian = 1234,
|
|
|
|
BigEndian = 4321,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromGlib<i32> for VideoEndianness {
|
|
|
|
fn from_glib(value: i32) -> Self {
|
2017-08-30 09:48:01 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
2017-08-10 21:41:55 +00:00
|
|
|
match value {
|
|
|
|
1234 => VideoEndianness::LittleEndian,
|
|
|
|
4321 => VideoEndianness::BigEndian,
|
|
|
|
_ => VideoEndianness::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToGlib for VideoEndianness {
|
|
|
|
type GlibType = i32;
|
|
|
|
|
|
|
|
fn to_glib(&self) -> i32 {
|
|
|
|
match *self {
|
|
|
|
VideoEndianness::LittleEndian => 1234,
|
|
|
|
VideoEndianness::BigEndian => 4321,
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::VideoFormat {
|
|
|
|
pub fn from_fourcc(fourcc: u32) -> ::VideoFormat {
|
2017-08-30 09:48:01 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { from_glib(gst_video_sys::gst_video_format_from_fourcc(fourcc)) }
|
2017-08-10 21:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_masks(
|
|
|
|
depth: u32,
|
|
|
|
bpp: u32,
|
|
|
|
endianness: ::VideoEndianness,
|
|
|
|
red_mask: u32,
|
|
|
|
blue_mask: u32,
|
|
|
|
green_mask: u32,
|
|
|
|
alpha_mask: u32,
|
|
|
|
) -> ::VideoFormat {
|
2017-08-30 09:48:01 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-08-10 21:41:55 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_video_sys::gst_video_format_from_masks(
|
2017-08-10 21:41:55 +00:00
|
|
|
depth as i32,
|
|
|
|
bpp as i32,
|
|
|
|
endianness.to_glib(),
|
|
|
|
red_mask,
|
|
|
|
blue_mask,
|
|
|
|
green_mask,
|
|
|
|
alpha_mask,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 06:11:30 +00:00
|
|
|
pub fn to_str<'a>(self) -> &'a str {
|
2018-07-20 07:21:06 +00:00
|
|
|
if self == ::VideoFormat::Unknown {
|
2017-12-16 08:58:10 +00:00
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2017-08-10 21:41:55 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
CStr::from_ptr(gst_video_sys::gst_video_format_to_string(self.to_glib()))
|
2017-08-10 21:41:55 +00:00
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:57:35 +00:00
|
|
|
|
|
|
|
pub fn iter_raw() -> VideoFormatIterator {
|
|
|
|
VideoFormatIterator::default()
|
|
|
|
}
|
2017-08-10 21:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl str::FromStr for ::VideoFormat {
|
2019-12-17 19:00:42 +00:00
|
|
|
type Err = glib::BoolError;
|
2017-08-10 21:41:55 +00:00
|
|
|
|
2019-12-17 19:00:42 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self, glib::BoolError> {
|
2019-10-04 06:11:30 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let fmt = ::VideoFormat::from_glib(gst_video_sys::gst_video_format_from_string(
|
|
|
|
s.to_glib_none().0,
|
|
|
|
));
|
2017-08-30 09:48:01 +00:00
|
|
|
|
2019-10-04 06:11:30 +00:00
|
|
|
if fmt == ::VideoFormat::Unknown {
|
2019-12-17 19:00:42 +00:00
|
|
|
Err(glib_bool_error!("Failed to parse video format from string"))
|
2019-10-04 06:11:30 +00:00
|
|
|
} else {
|
|
|
|
Ok(fmt)
|
|
|
|
}
|
2017-08-10 21:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ::VideoFormat {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2019-10-04 06:11:30 +00:00
|
|
|
f.write_str((*self).to_str())
|
2019-06-18 10:15:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 09:57:29 +00:00
|
|
|
|
|
|
|
impl PartialOrd for ::VideoFormat {
|
|
|
|
fn partial_cmp(&self, other: &::VideoFormat) -> Option<std::cmp::Ordering> {
|
|
|
|
::VideoFormatInfo::from_format(*self).partial_cmp(&::VideoFormatInfo::from_format(*other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for ::VideoFormat {
|
|
|
|
fn cmp(&self, other: &::VideoFormat) -> std::cmp::Ordering {
|
|
|
|
::VideoFormatInfo::from_format(*self).cmp(&::VideoFormatInfo::from_format(*other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:57:35 +00:00
|
|
|
pub struct VideoFormatIterator {
|
|
|
|
idx: usize,
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for VideoFormatIterator {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
idx: 0,
|
|
|
|
len: VIDEO_FORMATS_ALL.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for VideoFormatIterator {
|
|
|
|
type Item = ::VideoFormat;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.idx >= self.len {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let fmt = VIDEO_FORMATS_ALL[self.idx];
|
|
|
|
self.idx += 1;
|
|
|
|
Some(fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExactSizeIterator for VideoFormatIterator {
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DoubleEndedIterator for VideoFormatIterator {
|
|
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.idx >= self.len {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let fmt = VIDEO_FORMATS_ALL[self.len - 1];
|
|
|
|
self.len -= 1;
|
|
|
|
Some(fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub trait VideoFormatIteratorExt {
|
|
|
|
fn into_video_caps(self) -> Option<gst::caps::Builder<gst::caps::NoFeature>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> VideoFormatIteratorExt for T
|
|
|
|
where
|
|
|
|
T: Iterator<Item = ::VideoFormat>,
|
|
|
|
{
|
|
|
|
fn into_video_caps(self) -> Option<gst::caps::Builder<gst::caps::NoFeature>> {
|
|
|
|
let formats: Vec<::VideoFormat> = self.collect();
|
|
|
|
if !formats.is_empty() {
|
|
|
|
Some(::functions::video_make_raw_caps(&formats))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait VideoFormatIteratorExtRef {
|
|
|
|
fn into_video_caps(self) -> Option<gst::caps::Builder<gst::caps::NoFeature>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> VideoFormatIteratorExtRef for T
|
|
|
|
where
|
|
|
|
T: Iterator<Item = &'a ::VideoFormat>,
|
|
|
|
{
|
|
|
|
fn into_video_caps(self) -> Option<gst::caps::Builder<gst::caps::NoFeature>> {
|
|
|
|
let formats: Vec<::VideoFormat> = self.copied().collect();
|
|
|
|
if !formats.is_empty() {
|
|
|
|
Some(::functions::video_make_raw_caps(&formats))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 10:15:33 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use gst;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
|
|
|
format!("{}", ::VideoFormat::Nv16);
|
2017-08-10 21:41:55 +00:00
|
|
|
}
|
2020-05-28 10:57:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn iter() {
|
|
|
|
use super::*;
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
|
|
|
assert!(::VideoFormat::iter_raw().count() > 0);
|
|
|
|
assert_eq!(
|
|
|
|
::VideoFormat::iter_raw().count(),
|
|
|
|
::VideoFormat::iter_raw().len()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut i = ::VideoFormat::iter_raw();
|
|
|
|
let mut count = 0;
|
|
|
|
loop {
|
|
|
|
if i.next().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
if i.next_back().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(count, ::VideoFormat::iter_raw().len());
|
|
|
|
|
|
|
|
assert!(::VideoFormat::iter_raw().any(|f| f == ::VideoFormat::P016Be));
|
|
|
|
assert!(::VideoFormat::iter_raw()
|
|
|
|
.find(|f| *f == ::VideoFormat::Encoded)
|
|
|
|
.is_none());
|
|
|
|
|
|
|
|
let caps = ::VideoFormat::iter_raw().into_video_caps();
|
|
|
|
assert!(caps.is_some());
|
|
|
|
|
|
|
|
let caps = ::VideoFormat::iter_raw()
|
|
|
|
.filter(|f| ::VideoFormatInfo::from_format(*f).is_gray())
|
|
|
|
.into_video_caps();
|
|
|
|
assert!(caps.is_some());
|
|
|
|
|
|
|
|
let caps = ::VideoFormat::iter_raw().skip(1000).into_video_caps();
|
|
|
|
assert!(caps.is_none());
|
|
|
|
|
|
|
|
let caps = [::VideoFormat::Nv12, ::VideoFormat::Nv16]
|
|
|
|
.iter()
|
|
|
|
.into_video_caps()
|
|
|
|
.unwrap()
|
|
|
|
.build();
|
|
|
|
assert_eq!(caps.to_string(), "video/x-raw, format=(string){ NV12, NV16 }, width=(int)[ 1, 2147483647 ], height=(int)[ 1, 2147483647 ], framerate=(fraction)[ 0/1, 2147483647/1 ]");
|
|
|
|
}
|
2020-06-05 09:57:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sort() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
::VideoFormatInfo::from_format(::VideoFormat::Nv16)
|
|
|
|
> ::VideoFormatInfo::from_format(::VideoFormat::Nv12)
|
|
|
|
);
|
|
|
|
assert!(::VideoFormat::I420 > ::VideoFormat::Yv12);
|
|
|
|
}
|
2017-08-10 21:41:55 +00:00
|
|
|
}
|