gstreamer_analytics/
tensor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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<'a, *mut ffi::GstTensorDim, Self> {
        glib::translate::StashMut(self as *mut _ as *mut _, PhantomData)
    }
}

glib::wrapper! {
    /// Hold tensor data
    #[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 {
    /// Allocates a new [`Tensor`][crate::Tensor] of `dims_order` ROW_MAJOR or COLUMN_MAJOR and
    /// with an interleaved layout
    /// ## `id`
    /// semantically identify the contents of the tensor
    /// ## `data_type`
    /// [`TensorDataType`][crate::TensorDataType] of tensor data
    /// ## `batch_size`
    /// Model batch size
    /// ## `data`
    /// [`gst::Buffer`][crate::gst::Buffer] holding tensor data
    /// ## `dims_order`
    /// Indicate tensor dimension indexing order
    /// ## `dims`
    /// tensor dimensions
    ///
    /// # Returns
    ///
    /// A newly allocated [`Tensor`][crate::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 _,
            ))
        }
    }

    /// Gets the dimensions of the tensor.
    ///
    /// # Returns
    ///
    /// The dims array form the tensor
    #[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();
    }
}