analytics: Add bindings for tensor and tensormeta APIs

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1595>
This commit is contained in:
Olivier Crête 2024-11-05 15:00:13 -05:00 committed by GStreamer Marge Bot
parent 591ab4314e
commit 299034e105
6 changed files with 419 additions and 2 deletions

View file

@ -17,14 +17,17 @@ external_libraries = [
] ]
generate = [ generate = [
"GstAnalytics.RelTypes" "GstAnalytics.RelTypes",
"GstAnalytics.TensorDataType",
"GstAnalytics.TensorDimOrder"
] ]
manual = [ manual = [
"GObject.Object", "GObject.Object",
"Gst.Element", "Gst.Element",
"Gst.MiniObject", "Gst.MiniObject",
"Gst.Object" "Gst.Object",
"GstAnalytics.Tensor"
] ]

View file

@ -0,0 +1,151 @@
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT
use crate::ffi;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
use glib::translate::*;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
#[non_exhaustive]
#[doc(alias = "GstTensorDataType")]
pub enum TensorDataType {
#[doc(alias = "GST_TENSOR_DATA_TYPE_INT4")]
Int4,
#[doc(alias = "GST_TENSOR_DATA_TYPE_INT8")]
Int8,
#[doc(alias = "GST_TENSOR_DATA_TYPE_INT16")]
Int16,
#[doc(alias = "GST_TENSOR_DATA_TYPE_INT32")]
Int32,
#[doc(alias = "GST_TENSOR_DATA_TYPE_INT64")]
Int64,
#[doc(alias = "GST_TENSOR_DATA_TYPE_UINT4")]
Uint4,
#[doc(alias = "GST_TENSOR_DATA_TYPE_UINT8")]
Uint8,
#[doc(alias = "GST_TENSOR_DATA_TYPE_UINT16")]
Uint16,
#[doc(alias = "GST_TENSOR_DATA_TYPE_UINT32")]
Uint32,
#[doc(alias = "GST_TENSOR_DATA_TYPE_UINT64")]
Uint64,
#[doc(alias = "GST_TENSOR_DATA_TYPE_FLOAT16")]
Float16,
#[doc(alias = "GST_TENSOR_DATA_TYPE_FLOAT32")]
Float32,
#[doc(alias = "GST_TENSOR_DATA_TYPE_FLOAT64")]
Float64,
#[doc(alias = "GST_TENSOR_DATA_TYPE_BFLOAT16")]
Bfloat16,
#[doc(hidden)]
__Unknown(i32),
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(hidden)]
impl IntoGlib for TensorDataType {
type GlibType = ffi::GstTensorDataType;
fn into_glib(self) -> ffi::GstTensorDataType {
match self {
Self::Int4 => ffi::GST_TENSOR_DATA_TYPE_INT4,
Self::Int8 => ffi::GST_TENSOR_DATA_TYPE_INT8,
Self::Int16 => ffi::GST_TENSOR_DATA_TYPE_INT16,
Self::Int32 => ffi::GST_TENSOR_DATA_TYPE_INT32,
Self::Int64 => ffi::GST_TENSOR_DATA_TYPE_INT64,
Self::Uint4 => ffi::GST_TENSOR_DATA_TYPE_UINT4,
Self::Uint8 => ffi::GST_TENSOR_DATA_TYPE_UINT8,
Self::Uint16 => ffi::GST_TENSOR_DATA_TYPE_UINT16,
Self::Uint32 => ffi::GST_TENSOR_DATA_TYPE_UINT32,
Self::Uint64 => ffi::GST_TENSOR_DATA_TYPE_UINT64,
Self::Float16 => ffi::GST_TENSOR_DATA_TYPE_FLOAT16,
Self::Float32 => ffi::GST_TENSOR_DATA_TYPE_FLOAT32,
Self::Float64 => ffi::GST_TENSOR_DATA_TYPE_FLOAT64,
Self::Bfloat16 => ffi::GST_TENSOR_DATA_TYPE_BFLOAT16,
Self::__Unknown(value) => value,
}
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(hidden)]
impl FromGlib<ffi::GstTensorDataType> for TensorDataType {
unsafe fn from_glib(value: ffi::GstTensorDataType) -> Self {
skip_assert_initialized!();
match value {
ffi::GST_TENSOR_DATA_TYPE_INT4 => Self::Int4,
ffi::GST_TENSOR_DATA_TYPE_INT8 => Self::Int8,
ffi::GST_TENSOR_DATA_TYPE_INT16 => Self::Int16,
ffi::GST_TENSOR_DATA_TYPE_INT32 => Self::Int32,
ffi::GST_TENSOR_DATA_TYPE_INT64 => Self::Int64,
ffi::GST_TENSOR_DATA_TYPE_UINT4 => Self::Uint4,
ffi::GST_TENSOR_DATA_TYPE_UINT8 => Self::Uint8,
ffi::GST_TENSOR_DATA_TYPE_UINT16 => Self::Uint16,
ffi::GST_TENSOR_DATA_TYPE_UINT32 => Self::Uint32,
ffi::GST_TENSOR_DATA_TYPE_UINT64 => Self::Uint64,
ffi::GST_TENSOR_DATA_TYPE_FLOAT16 => Self::Float16,
ffi::GST_TENSOR_DATA_TYPE_FLOAT32 => Self::Float32,
ffi::GST_TENSOR_DATA_TYPE_FLOAT64 => Self::Float64,
ffi::GST_TENSOR_DATA_TYPE_BFLOAT16 => Self::Bfloat16,
value => Self::__Unknown(value),
}
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
#[non_exhaustive]
#[doc(alias = "GstTensorDimOrder")]
pub enum TensorDimOrder {
#[doc(alias = "GST_TENSOR_DIM_ORDER_ROW_MAJOR")]
RowMajor,
#[doc(alias = "GST_TENSOR_DIM_ORDER_COL_MAJOR")]
ColMajor,
#[doc(alias = "GST_TENSOR_DIM_ORDER_INDEXED")]
Indexed,
#[doc(hidden)]
__Unknown(i32),
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(hidden)]
impl IntoGlib for TensorDimOrder {
type GlibType = ffi::GstTensorDimOrder;
#[inline]
fn into_glib(self) -> ffi::GstTensorDimOrder {
match self {
Self::RowMajor => ffi::GST_TENSOR_DIM_ORDER_ROW_MAJOR,
Self::ColMajor => ffi::GST_TENSOR_DIM_ORDER_COL_MAJOR,
Self::Indexed => ffi::GST_TENSOR_DIM_ORDER_INDEXED,
Self::__Unknown(value) => value,
}
}
}
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
#[doc(hidden)]
impl FromGlib<ffi::GstTensorDimOrder> for TensorDimOrder {
#[inline]
unsafe fn from_glib(value: ffi::GstTensorDimOrder) -> Self {
skip_assert_initialized!();
match value {
ffi::GST_TENSOR_DIM_ORDER_ROW_MAJOR => Self::RowMajor,
ffi::GST_TENSOR_DIM_ORDER_COL_MAJOR => Self::ColMajor,
ffi::GST_TENSOR_DIM_ORDER_INDEXED => Self::Indexed,
value => Self::__Unknown(value),
}
}
}

View file

@ -3,5 +3,13 @@
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git) // from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT // DO NOT EDIT
mod enums;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
pub use self::enums::TensorDataType;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
pub use self::enums::TensorDimOrder;
mod flags; mod flags;
pub use self::flags::RelTypes; pub use self::flags::RelTypes;

View file

@ -15,6 +15,20 @@ macro_rules! skip_assert_initialized {
mod auto; mod auto;
pub use crate::auto::*; pub use crate::auto::*;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
mod tensor;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
pub use crate::tensor::*;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
mod tensor_meta;
#[cfg(feature = "v1_26")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
pub use crate::tensor_meta::*;
mod relation_meta; mod relation_meta;
pub use crate::relation_meta::*; pub use crate::relation_meta::*;

View file

@ -0,0 +1,140 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::ffi;
use crate::*;
use glib::translate::*;
use std::marker::PhantomData;
#[repr(C)]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct TensorDim {
pub size: usize,
pub order_index: usize,
}
#[doc(hidden)]
impl<'a> glib::translate::ToGlibPtrMut<'a, *mut ffi::GstTensorDim> for TensorDim {
type Storage = PhantomData<&'a mut Self>;
#[inline]
fn to_glib_none_mut(&'a mut self) -> glib::translate::StashMut<*mut ffi::GstTensorDim, Self> {
glib::translate::StashMut(self as *mut _ as *mut _, PhantomData)
}
}
glib::wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[doc(alias = "GstTensor")]
pub struct Tensor(Boxed<ffi::GstTensor>);
match fn {
copy => |ptr| ffi::gst_tensor_copy(ptr),
free => |ptr| ffi::gst_tensor_free(ptr),
type_ => || ffi::gst_tensor_get_type(),
}
}
unsafe impl Send for Tensor {}
unsafe impl Sync for Tensor {}
impl Tensor {
#[doc(alias = "gst_tensor_new_simple")]
pub fn new_simple(
id: glib::Quark,
data_type: TensorDataType,
batch_size: usize,
data: gst::Buffer,
dims_order: TensorDimOrder,
dims: &[usize],
) -> Tensor {
skip_assert_initialized!();
unsafe {
from_glib_full(ffi::gst_tensor_new_simple(
id.into_glib(),
data_type.into_glib(),
batch_size,
data.into_glib_ptr(),
dims_order.into_glib(),
dims.len(),
dims.as_ptr() as *mut _,
))
}
}
#[doc(alias = "gst_tensor_get_dims")]
#[doc(alias = "get_dims")]
pub fn dims(&self) -> &[TensorDim] {
let mut num_dims: usize = 0;
unsafe {
let dims = ffi::gst_tensor_get_dims(self.as_ptr(), &mut num_dims);
std::slice::from_raw_parts(dims as *const _, num_dims)
}
}
#[inline]
pub fn id(&self) -> glib::Quark {
unsafe { from_glib(self.inner.id) }
}
#[inline]
pub fn data_type(&self) -> TensorDataType {
unsafe { from_glib(self.inner.data_type) }
}
#[inline]
pub fn batch_size(&self) -> usize {
self.inner.batch_size
}
#[inline]
pub fn data(&self) -> &gst::BufferRef {
unsafe { gst::BufferRef::from_ptr(self.inner.data) }
}
#[inline]
pub fn data_mut(&mut self) -> &mut gst::BufferRef {
unsafe {
self.inner.data = gst::ffi::gst_mini_object_make_writable(self.inner.data as _) as _;
gst::BufferRef::from_mut_ptr(self.inner.data)
}
}
#[inline]
pub fn dims_order(&self) -> TensorDimOrder {
unsafe { from_glib(self.inner.dims_order) }
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn create_tensor() {
gst::init().unwrap();
let buf = gst::Buffer::with_size(2 * 2 * 3 * 4 * 5).unwrap();
assert_eq!(buf.size(), 2 * 2 * 3 * 4 * 5);
let mut tensor = Tensor::new_simple(
glib::Quark::from_str("me"),
TensorDataType::Int16,
2,
buf,
TensorDimOrder::RowMajor,
&[3, 4, 5],
);
assert_eq!(tensor.id(), glib::Quark::from_str("me"));
assert_eq!(tensor.data_type(), TensorDataType::Int16);
assert_eq!(tensor.batch_size(), 2);
assert_eq!(tensor.dims_order(), TensorDimOrder::RowMajor);
assert_eq!(tensor.dims()[0].size, 3);
assert_eq!(tensor.dims()[1].size, 4);
assert_eq!(tensor.dims()[2].size, 5);
assert_eq!(tensor.data().size(), 2 * 2 * 3 * 4 * 5);
tensor.data();
tensor.data_mut();
}
}

View file

@ -0,0 +1,101 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::*;
use gst::prelude::*;
use crate::ffi;
use crate::Tensor;
#[repr(transparent)]
#[doc(alias = "GstTensorMeta")]
pub struct TensorMeta(ffi::GstTensorMeta);
unsafe impl Send for TensorMeta {}
unsafe impl Sync for TensorMeta {}
impl TensorMeta {
#[doc(alias = "gst_buffer_add_tensor_meta")]
pub fn add(buffer: &mut gst::BufferRef) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
skip_assert_initialized!();
unsafe {
let meta_ptr = ffi::gst_buffer_add_tensor_meta(buffer.as_mut_ptr());
Self::from_mut_ptr(buffer, meta_ptr)
}
}
#[doc(alias = "gst_tensor_meta_set")]
pub fn set(&mut self, tensors: glib::Slice<Tensor>) {
unsafe {
ffi::gst_tensor_meta_set(self.as_mut_ptr(), tensors.len() as u32, tensors.into_raw());
}
}
#[doc(alias = "gst_tensor_meta_get_index_from_id")]
pub fn index_from_id(&self, id: glib::Quark) -> i32 {
unsafe { ffi::gst_tensor_meta_get_index_from_id(self.as_mut_ptr(), id.into_glib()) }
}
pub fn as_slice(&self) -> &[Tensor] {
unsafe { glib::Slice::from_glib_borrow_num(self.0.tensors, self.0.num_tensors) }
}
pub fn as_mut_slice(&mut self) -> &mut [Tensor] {
unsafe { glib::Slice::from_glib_borrow_num_mut(self.0.tensors, self.0.num_tensors) }
}
unsafe fn as_mut_ptr(&self) -> *mut ffi::GstTensorMeta {
mut_override(&self.0)
}
}
unsafe impl MetaAPI for TensorMeta {
type GstType = ffi::GstTensorMeta;
#[doc(alias = "gst_tensor_meta_api_get_type")]
#[inline]
fn meta_api() -> glib::Type {
unsafe { from_glib(ffi::gst_tensor_meta_api_get_type()) }
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn build_tensor_meta() {
gst::init().unwrap();
let mut buf = gst::Buffer::new();
let mut tmeta = TensorMeta::add(buf.make_mut());
let tensor = Tensor::new_simple(
glib::Quark::from_str("me"),
TensorDataType::Int16,
2,
gst::Buffer::with_size(2 * 2 * 3 * 4 * 5).unwrap(),
TensorDimOrder::RowMajor,
&[3, 4, 5],
);
let tptr = tensor.as_ptr();
tmeta.set([tensor].into());
let tensors = tmeta.as_slice();
assert_eq!(tensors.len(), 1);
// Check that it's the same tensor
assert_eq!(tptr, tensors[0].as_ptr());
assert_eq!(tensors[0].dims_order(), TensorDimOrder::RowMajor);
assert_eq!(tensors[0].dims().len(), 3);
assert_eq!(tensors[0].dims()[0].size, 3);
assert_eq!(tmeta.as_slice().len(), 1);
tmeta.as_mut_slice();
}
}