gstreamer_video/
video_meta.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{fmt, ptr};
4
5use crate::ffi;
6use glib::translate::*;
7use gst::prelude::*;
8
9/// Extra buffer metadata describing image properties
10///
11/// This meta can also be used by downstream elements to specifiy their
12/// buffer layout requirements for upstream. Upstream should try to
13/// fit those requirements, if possible, in order to prevent buffer copies.
14///
15/// This is done by passing a custom [`gst::Structure`][crate::gst::Structure] to
16/// [`gst::Query::add_allocation_meta()`][crate::gst::Query::add_allocation_meta()] when handling the ALLOCATION query.
17/// This structure should be named 'video-meta' and can have the following
18/// fields:
19/// - padding-top (uint): extra pixels on the top
20/// - padding-bottom (uint): extra pixels on the bottom
21/// - padding-left (uint): extra pixels on the left side
22/// - padding-right (uint): extra pixels on the right side
23/// - stride-align0 (uint): stride align requirements for plane 0
24/// - stride-align1 (uint): stride align requirements for plane 1
25/// - stride-align2 (uint): stride align requirements for plane 2
26/// - stride-align3 (uint): stride align requirements for plane 3
27/// The padding and stride-align fields have the same semantic as `GstVideoMeta.alignment`
28/// and so represent the paddings and stride-align requested on produced video buffers.
29///
30/// Since 1.24 it can be serialized using `gst_meta_serialize()` and
31/// `gst_meta_deserialize()`.
32#[repr(transparent)]
33#[doc(alias = "GstVideoMeta")]
34pub struct VideoMeta(ffi::GstVideoMeta);
35
36unsafe impl Send for VideoMeta {}
37unsafe impl Sync for VideoMeta {}
38
39impl VideoMeta {
40    #[doc(alias = "gst_buffer_add_video_meta")]
41    pub fn add(
42        buffer: &mut gst::BufferRef,
43        video_frame_flags: crate::VideoFrameFlags,
44        format: crate::VideoFormat,
45        width: u32,
46        height: u32,
47    ) -> Result<gst::MetaRefMut<Self, gst::meta::Standalone>, glib::BoolError> {
48        skip_assert_initialized!();
49
50        if format == crate::VideoFormat::Unknown || format == crate::VideoFormat::Encoded {
51            return Err(glib::bool_error!("Unsupported video format {}", format));
52        }
53
54        let info = crate::VideoInfo::builder(format, width, height).build()?;
55
56        if !info.is_valid() {
57            return Err(glib::bool_error!("Invalid video info"));
58        }
59
60        if buffer.size() < info.size() {
61            return Err(glib::bool_error!(
62                "Buffer smaller than required frame size ({} < {})",
63                buffer.size(),
64                info.size()
65            ));
66        }
67
68        unsafe {
69            let meta = ffi::gst_buffer_add_video_meta(
70                buffer.as_mut_ptr(),
71                video_frame_flags.into_glib(),
72                format.into_glib(),
73                width,
74                height,
75            );
76
77            if meta.is_null() {
78                return Err(glib::bool_error!("Failed to add video meta"));
79            }
80
81            Ok(Self::from_mut_ptr(buffer, meta))
82        }
83    }
84
85    pub fn add_full<'a>(
86        buffer: &'a mut gst::BufferRef,
87        video_frame_flags: crate::VideoFrameFlags,
88        format: crate::VideoFormat,
89        width: u32,
90        height: u32,
91        offset: &[usize],
92        stride: &[i32],
93    ) -> Result<gst::MetaRefMut<'a, Self, gst::meta::Standalone>, glib::BoolError> {
94        skip_assert_initialized!();
95
96        if format == crate::VideoFormat::Unknown || format == crate::VideoFormat::Encoded {
97            return Err(glib::bool_error!("Unsupported video format {}", format));
98        }
99
100        let n_planes = offset.len() as u32;
101        let info_builder = crate::VideoInfo::builder(format, width, height)
102            .offset(offset)
103            .stride(stride);
104
105        #[cfg(feature = "v1_16")]
106        let info_builder = info_builder.interlace_mode_if(
107            crate::VideoInterlaceMode::Alternate,
108            video_frame_flags.contains(crate::VideoFrameFlags::ONEFIELD),
109        );
110
111        let info = info_builder.build()?;
112
113        if !info.is_valid() {
114            return Err(glib::bool_error!("Invalid video info"));
115        }
116
117        if buffer.size() < info.size() {
118            return Err(glib::bool_error!(
119                "Buffer smaller than required frame size ({} < {})",
120                buffer.size(),
121                info.size()
122            ));
123        }
124
125        unsafe {
126            let meta = ffi::gst_buffer_add_video_meta_full(
127                buffer.as_mut_ptr(),
128                video_frame_flags.into_glib(),
129                format.into_glib(),
130                width,
131                height,
132                n_planes,
133                offset.as_ptr() as *mut _,
134                stride.as_ptr() as *mut _,
135            );
136
137            if meta.is_null() {
138                return Err(glib::bool_error!("Failed to add video meta"));
139            }
140
141            Ok(Self::from_mut_ptr(buffer, meta))
142        }
143    }
144
145    #[doc(alias = "get_flags")]
146    #[inline]
147    pub fn video_frame_flags(&self) -> crate::VideoFrameFlags {
148        unsafe { from_glib(self.0.flags) }
149    }
150
151    #[doc(alias = "get_format")]
152    #[inline]
153    pub fn format(&self) -> crate::VideoFormat {
154        unsafe { from_glib(self.0.format) }
155    }
156
157    #[doc(alias = "get_id")]
158    #[inline]
159    pub fn id(&self) -> i32 {
160        self.0.id
161    }
162
163    #[doc(alias = "get_width")]
164    #[inline]
165    pub fn width(&self) -> u32 {
166        self.0.width
167    }
168
169    #[doc(alias = "get_height")]
170    #[inline]
171    pub fn height(&self) -> u32 {
172        self.0.height
173    }
174
175    #[doc(alias = "get_n_planes")]
176    #[inline]
177    pub fn n_planes(&self) -> u32 {
178        self.0.n_planes
179    }
180
181    #[doc(alias = "get_offset")]
182    #[inline]
183    pub fn offset(&self) -> &[usize] {
184        &self.0.offset[0..(self.0.n_planes as usize)]
185    }
186
187    #[doc(alias = "get_stride")]
188    #[inline]
189    pub fn stride(&self) -> &[i32] {
190        &self.0.stride[0..(self.0.n_planes as usize)]
191    }
192
193    #[cfg(feature = "v1_18")]
194    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
195    #[doc(alias = "get_alignment")]
196    #[inline]
197    pub fn alignment(&self) -> crate::VideoAlignment {
198        crate::VideoAlignment::new(
199            self.0.alignment.padding_top,
200            self.0.alignment.padding_bottom,
201            self.0.alignment.padding_left,
202            self.0.alignment.padding_right,
203            &self.0.alignment.stride_align,
204        )
205    }
206
207    /// Compute the size, in bytes, of each video plane described in `self` including
208    /// any padding and alignment constraint defined in `self`->alignment.
209    ///
210    /// # Returns
211    ///
212    /// [`true`] if `self`'s alignment is valid and `plane_size` has been
213    /// updated, [`false`] otherwise
214    ///
215    /// ## `plane_size`
216    /// array used to store the plane sizes
217    #[cfg(feature = "v1_18")]
218    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
219    #[doc(alias = "get_plane_size")]
220    #[doc(alias = "gst_video_meta_get_plane_size")]
221    pub fn plane_size(&self) -> Result<[usize; crate::VIDEO_MAX_PLANES], glib::BoolError> {
222        let mut plane_size = [0; crate::VIDEO_MAX_PLANES];
223
224        unsafe {
225            glib::result_from_gboolean!(
226                ffi::gst_video_meta_get_plane_size(mut_override(&self.0), &mut plane_size,),
227                "Failed to get plane size"
228            )?;
229        }
230
231        Ok(plane_size)
232    }
233
234    /// Compute the padded height of each plane from `self` (padded size
235    /// divided by stride).
236    ///
237    /// It is not valid to call this function with a meta associated to a
238    /// TILED video format.
239    ///
240    /// # Returns
241    ///
242    /// [`true`] if `self`'s alignment is valid and `plane_height` has been
243    /// updated, [`false`] otherwise
244    ///
245    /// ## `plane_height`
246    /// array used to store the plane height
247    #[cfg(feature = "v1_18")]
248    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
249    #[doc(alias = "get_plane_height")]
250    #[doc(alias = "gst_video_meta_get_plane_height")]
251    pub fn plane_height(&self) -> Result<[u32; crate::VIDEO_MAX_PLANES], glib::BoolError> {
252        let mut plane_height = [0; crate::VIDEO_MAX_PLANES];
253
254        unsafe {
255            glib::result_from_gboolean!(
256                ffi::gst_video_meta_get_plane_height(mut_override(&self.0), &mut plane_height,),
257                "Failed to get plane height"
258            )?;
259        }
260
261        Ok(plane_height)
262    }
263
264    /// Set the alignment of `self` to `alignment`. This function checks that
265    /// the paddings defined in `alignment` are compatible with the strides
266    /// defined in `self` and will fail to update if they are not.
267    /// ## `alignment`
268    /// a `GstVideoAlignment`
269    ///
270    /// # Returns
271    ///
272    /// [`true`] if `alignment`'s meta has been updated, [`false`] if not
273    #[cfg(feature = "v1_18")]
274    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
275    #[doc(alias = "gst_video_meta_set_alignment")]
276    pub fn set_alignment(
277        &mut self,
278        alignment: &crate::VideoAlignment,
279    ) -> Result<(), glib::BoolError> {
280        unsafe {
281            glib::result_from_gboolean!(
282                ffi::gst_video_meta_set_alignment(&mut self.0, alignment.0),
283                "Failed to set alignment on VideoMeta"
284            )
285        }
286    }
287}
288
289unsafe impl MetaAPI for VideoMeta {
290    type GstType = ffi::GstVideoMeta;
291
292    #[doc(alias = "gst_video_meta_api_get_type")]
293    #[inline]
294    fn meta_api() -> glib::Type {
295        unsafe { from_glib(ffi::gst_video_meta_api_get_type()) }
296    }
297}
298
299impl fmt::Debug for VideoMeta {
300    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
301        f.debug_struct("VideoMeta")
302            .field("id", &self.id())
303            .field("video_frame_flags", &self.video_frame_flags())
304            .field("format", &self.format())
305            .field("width", &self.width())
306            .field("height", &self.height())
307            .field("n_planes", &self.n_planes())
308            .field("offset", &self.offset())
309            .field("stride", &self.stride())
310            .finish()
311    }
312}
313
314#[repr(transparent)]
315#[doc(alias = "GstVideoCropMeta")]
316pub struct VideoCropMeta(ffi::GstVideoCropMeta);
317
318unsafe impl Send for VideoCropMeta {}
319unsafe impl Sync for VideoCropMeta {}
320
321impl VideoCropMeta {
322    #[doc(alias = "gst_buffer_add_meta")]
323    pub fn add(
324        buffer: &mut gst::BufferRef,
325        rect: (u32, u32, u32, u32),
326    ) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
327        skip_assert_initialized!();
328        unsafe {
329            let meta = gst::ffi::gst_buffer_add_meta(
330                buffer.as_mut_ptr(),
331                ffi::gst_video_crop_meta_get_info(),
332                ptr::null_mut(),
333            ) as *mut ffi::GstVideoCropMeta;
334
335            {
336                let meta = &mut *meta;
337                meta.x = rect.0;
338                meta.y = rect.1;
339                meta.width = rect.2;
340                meta.height = rect.3;
341            }
342
343            Self::from_mut_ptr(buffer, meta)
344        }
345    }
346
347    #[doc(alias = "get_rect")]
348    #[inline]
349    pub fn rect(&self) -> (u32, u32, u32, u32) {
350        (self.0.x, self.0.y, self.0.width, self.0.height)
351    }
352
353    #[inline]
354    pub fn set_rect(&mut self, rect: (u32, u32, u32, u32)) {
355        self.0.x = rect.0;
356        self.0.y = rect.1;
357        self.0.width = rect.2;
358        self.0.height = rect.3;
359    }
360}
361
362unsafe impl MetaAPI for VideoCropMeta {
363    type GstType = ffi::GstVideoCropMeta;
364
365    #[doc(alias = "gst_video_crop_meta_api_get_type")]
366    #[inline]
367    fn meta_api() -> glib::Type {
368        unsafe { from_glib(ffi::gst_video_crop_meta_api_get_type()) }
369    }
370}
371
372impl fmt::Debug for VideoCropMeta {
373    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
374        f.debug_struct("VideoCropMeta")
375            .field("rect", &self.rect())
376            .finish()
377    }
378}
379
380#[repr(transparent)]
381#[doc(alias = "GstVideoRegionOfInterestMeta")]
382pub struct VideoRegionOfInterestMeta(ffi::GstVideoRegionOfInterestMeta);
383
384unsafe impl Send for VideoRegionOfInterestMeta {}
385unsafe impl Sync for VideoRegionOfInterestMeta {}
386
387impl VideoRegionOfInterestMeta {
388    #[doc(alias = "gst_buffer_add_video_region_of_interest_meta")]
389    pub fn add<'a>(
390        buffer: &'a mut gst::BufferRef,
391        roi_type: &str,
392        rect: (u32, u32, u32, u32),
393    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
394        skip_assert_initialized!();
395        unsafe {
396            let meta = ffi::gst_buffer_add_video_region_of_interest_meta(
397                buffer.as_mut_ptr(),
398                roi_type.to_glib_none().0,
399                rect.0,
400                rect.1,
401                rect.2,
402                rect.3,
403            );
404
405            Self::from_mut_ptr(buffer, meta)
406        }
407    }
408
409    #[doc(alias = "get_rect")]
410    #[inline]
411    pub fn rect(&self) -> (u32, u32, u32, u32) {
412        (self.0.x, self.0.y, self.0.w, self.0.h)
413    }
414
415    #[doc(alias = "get_id")]
416    #[inline]
417    pub fn id(&self) -> i32 {
418        self.0.id
419    }
420
421    #[doc(alias = "get_parent_id")]
422    #[inline]
423    pub fn parent_id(&self) -> i32 {
424        self.0.parent_id
425    }
426
427    #[doc(alias = "get_roi_type")]
428    #[inline]
429    pub fn roi_type<'a>(&self) -> &'a str {
430        unsafe { glib::Quark::from_glib(self.0.roi_type).as_str() }
431    }
432
433    #[doc(alias = "get_params")]
434    pub fn params(&self) -> ParamsIter {
435        ParamsIter {
436            _meta: self,
437            list: ptr::NonNull::new(self.0.params),
438        }
439    }
440
441    #[doc(alias = "get_param")]
442    #[inline]
443    pub fn param<'b>(&'b self, name: &str) -> Option<&'b gst::StructureRef> {
444        self.params().find(|s| s.name() == name)
445    }
446
447    #[inline]
448    pub fn set_rect(&mut self, rect: (u32, u32, u32, u32)) {
449        self.0.x = rect.0;
450        self.0.y = rect.1;
451        self.0.w = rect.2;
452        self.0.h = rect.3;
453    }
454
455    #[inline]
456    pub fn set_id(&mut self, id: i32) {
457        self.0.id = id
458    }
459
460    #[inline]
461    pub fn set_parent_id(&mut self, id: i32) {
462        self.0.parent_id = id
463    }
464
465    #[doc(alias = "gst_video_region_of_interest_meta_add_param")]
466    pub fn add_param(&mut self, s: gst::Structure) {
467        unsafe {
468            ffi::gst_video_region_of_interest_meta_add_param(&mut self.0, s.into_glib_ptr());
469        }
470    }
471}
472
473pub struct ParamsIter<'a> {
474    _meta: &'a VideoRegionOfInterestMeta,
475    list: Option<ptr::NonNull<glib::ffi::GList>>,
476}
477
478impl<'a> Iterator for ParamsIter<'a> {
479    type Item = &'a gst::StructureRef;
480
481    fn next(&mut self) -> Option<&'a gst::StructureRef> {
482        match self.list {
483            None => None,
484            Some(list) => unsafe {
485                self.list = ptr::NonNull::new(list.as_ref().next);
486                let data = list.as_ref().data;
487
488                let s = gst::StructureRef::from_glib_borrow(data as *const gst::ffi::GstStructure);
489
490                Some(s)
491            },
492        }
493    }
494}
495
496impl std::iter::FusedIterator for ParamsIter<'_> {}
497
498unsafe impl MetaAPI for VideoRegionOfInterestMeta {
499    type GstType = ffi::GstVideoRegionOfInterestMeta;
500
501    #[doc(alias = "gst_video_region_of_interest_meta_api_get_type")]
502    #[inline]
503    fn meta_api() -> glib::Type {
504        unsafe { from_glib(ffi::gst_video_region_of_interest_meta_api_get_type()) }
505    }
506}
507
508impl fmt::Debug for VideoRegionOfInterestMeta {
509    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
510        f.debug_struct("VideoRegionOfInterestMeta")
511            .field("roi_type", &self.roi_type())
512            .field("rect", &self.rect())
513            .field("id", &self.id())
514            .field("parent_id", &self.parent_id())
515            .field("params", &self.params().collect::<Vec<_>>())
516            .finish()
517    }
518}
519
520#[repr(transparent)]
521#[doc(alias = "GstVideoAffineTransformationMeta")]
522pub struct VideoAffineTransformationMeta(ffi::GstVideoAffineTransformationMeta);
523
524unsafe impl Send for VideoAffineTransformationMeta {}
525unsafe impl Sync for VideoAffineTransformationMeta {}
526
527impl VideoAffineTransformationMeta {
528    #[doc(alias = "gst_buffer_add_meta")]
529    pub fn add<'a>(
530        buffer: &'a mut gst::BufferRef,
531        matrix: Option<&[[f32; 4]; 4]>,
532    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
533        skip_assert_initialized!();
534        unsafe {
535            let meta = gst::ffi::gst_buffer_add_meta(
536                buffer.as_mut_ptr(),
537                ffi::gst_video_affine_transformation_meta_get_info(),
538                ptr::null_mut(),
539            ) as *mut ffi::GstVideoAffineTransformationMeta;
540
541            if let Some(matrix) = matrix {
542                let meta = &mut *meta;
543                for (i, o) in Iterator::zip(matrix.iter().flatten(), meta.matrix.iter_mut()) {
544                    *o = *i;
545                }
546            }
547
548            Self::from_mut_ptr(buffer, meta)
549        }
550    }
551
552    #[doc(alias = "get_matrix")]
553    #[inline]
554    pub fn matrix(&self) -> &[[f32; 4]; 4] {
555        unsafe { &*(&self.0.matrix as *const [f32; 16] as *const [[f32; 4]; 4]) }
556    }
557
558    #[inline]
559    pub fn set_matrix(&mut self, matrix: &[[f32; 4]; 4]) {
560        for (i, o) in Iterator::zip(matrix.iter().flatten(), self.0.matrix.iter_mut()) {
561            *o = *i;
562        }
563    }
564
565    #[doc(alias = "gst_video_affine_transformation_meta_apply_matrix")]
566    pub fn apply_matrix(&mut self, matrix: &[[f32; 4]; 4]) {
567        unsafe {
568            ffi::gst_video_affine_transformation_meta_apply_matrix(
569                &mut self.0,
570                matrix as *const [[f32; 4]; 4] as *const [f32; 16],
571            );
572        }
573    }
574}
575
576unsafe impl MetaAPI for VideoAffineTransformationMeta {
577    type GstType = ffi::GstVideoAffineTransformationMeta;
578
579    #[doc(alias = "gst_video_affine_transformation_meta_api_get_type")]
580    #[inline]
581    fn meta_api() -> glib::Type {
582        unsafe { from_glib(ffi::gst_video_affine_transformation_meta_api_get_type()) }
583    }
584}
585
586impl fmt::Debug for VideoAffineTransformationMeta {
587    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
588        f.debug_struct("VideoAffineTransformationMeta")
589            .field("matrix", &self.matrix())
590            .finish()
591    }
592}
593
594#[repr(transparent)]
595#[doc(alias = "GstVideoOverlayCompositionMeta")]
596pub struct VideoOverlayCompositionMeta(ffi::GstVideoOverlayCompositionMeta);
597
598unsafe impl Send for VideoOverlayCompositionMeta {}
599unsafe impl Sync for VideoOverlayCompositionMeta {}
600
601impl VideoOverlayCompositionMeta {
602    #[doc(alias = "gst_buffer_add_video_overlay_composition_meta")]
603    pub fn add<'a>(
604        buffer: &'a mut gst::BufferRef,
605        overlay: &crate::VideoOverlayComposition,
606    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
607        skip_assert_initialized!();
608        unsafe {
609            let meta = ffi::gst_buffer_add_video_overlay_composition_meta(
610                buffer.as_mut_ptr(),
611                overlay.as_mut_ptr(),
612            );
613
614            Self::from_mut_ptr(buffer, meta)
615        }
616    }
617
618    #[doc(alias = "get_overlay")]
619    #[inline]
620    pub fn overlay(&self) -> &crate::VideoOverlayCompositionRef {
621        unsafe { crate::VideoOverlayCompositionRef::from_ptr(self.0.overlay) }
622    }
623
624    #[doc(alias = "get_overlay_owned")]
625    #[inline]
626    pub fn overlay_owned(&self) -> crate::VideoOverlayComposition {
627        unsafe { from_glib_none(self.overlay().as_ptr()) }
628    }
629
630    #[inline]
631    pub fn set_overlay(&mut self, overlay: &crate::VideoOverlayComposition) {
632        #![allow(clippy::cast_ptr_alignment)]
633        unsafe {
634            gst::ffi::gst_mini_object_unref(self.0.overlay as *mut _);
635            self.0.overlay =
636                gst::ffi::gst_mini_object_ref(overlay.as_mut_ptr() as *mut _) as *mut _;
637        }
638    }
639}
640
641unsafe impl MetaAPI for VideoOverlayCompositionMeta {
642    type GstType = ffi::GstVideoOverlayCompositionMeta;
643
644    #[doc(alias = "gst_video_overlay_composition_meta_api_get_type")]
645    #[inline]
646    fn meta_api() -> glib::Type {
647        unsafe { from_glib(ffi::gst_video_overlay_composition_meta_api_get_type()) }
648    }
649}
650
651impl fmt::Debug for VideoOverlayCompositionMeta {
652    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
653        f.debug_struct("VideoOverlayCompositionMeta")
654            .field("overlay", &self.overlay())
655            .finish()
656    }
657}
658
659#[cfg(feature = "v1_16")]
660#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
661#[repr(transparent)]
662#[doc(alias = "GstVideoCaptionMeta")]
663pub struct VideoCaptionMeta(ffi::GstVideoCaptionMeta);
664
665#[cfg(feature = "v1_16")]
666#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
667unsafe impl Send for VideoCaptionMeta {}
668#[cfg(feature = "v1_16")]
669#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
670unsafe impl Sync for VideoCaptionMeta {}
671
672#[cfg(feature = "v1_16")]
673#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
674impl VideoCaptionMeta {
675    #[doc(alias = "gst_buffer_add_video_caption_meta")]
676    pub fn add<'a>(
677        buffer: &'a mut gst::BufferRef,
678        caption_type: crate::VideoCaptionType,
679        data: &[u8],
680    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
681        skip_assert_initialized!();
682        assert!(!data.is_empty());
683        unsafe {
684            let meta = ffi::gst_buffer_add_video_caption_meta(
685                buffer.as_mut_ptr(),
686                caption_type.into_glib(),
687                data.as_ptr(),
688                data.len(),
689            );
690
691            Self::from_mut_ptr(buffer, meta)
692        }
693    }
694
695    #[doc(alias = "get_caption_type")]
696    #[inline]
697    pub fn caption_type(&self) -> crate::VideoCaptionType {
698        unsafe { from_glib(self.0.caption_type) }
699    }
700
701    #[doc(alias = "get_data")]
702    #[inline]
703    pub fn data(&self) -> &[u8] {
704        if self.0.size == 0 {
705            return &[];
706        }
707        unsafe {
708            use std::slice;
709
710            slice::from_raw_parts(self.0.data, self.0.size)
711        }
712    }
713}
714
715#[cfg(feature = "v1_16")]
716#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
717unsafe impl MetaAPI for VideoCaptionMeta {
718    type GstType = ffi::GstVideoCaptionMeta;
719
720    #[doc(alias = "gst_video_caption_meta_api_get_type")]
721    #[inline]
722    fn meta_api() -> glib::Type {
723        unsafe { from_glib(ffi::gst_video_caption_meta_api_get_type()) }
724    }
725}
726
727#[cfg(feature = "v1_16")]
728#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
729impl fmt::Debug for VideoCaptionMeta {
730    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
731        f.debug_struct("VideoCaptionMeta")
732            .field("caption_type", &self.caption_type())
733            .field("data", &self.data())
734            .finish()
735    }
736}
737
738#[cfg(feature = "v1_18")]
739#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
740#[repr(transparent)]
741#[doc(alias = "GstVideoAFDMeta")]
742pub struct VideoAFDMeta(ffi::GstVideoAFDMeta);
743
744#[cfg(feature = "v1_18")]
745#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
746unsafe impl Send for VideoAFDMeta {}
747#[cfg(feature = "v1_18")]
748#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
749unsafe impl Sync for VideoAFDMeta {}
750
751#[cfg(feature = "v1_18")]
752#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
753impl VideoAFDMeta {
754    #[doc(alias = "gst_buffer_add_video_afd_meta")]
755    pub fn add(
756        buffer: &mut gst::BufferRef,
757        field: u8,
758        spec: crate::VideoAFDSpec,
759        afd: crate::VideoAFDValue,
760    ) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
761        skip_assert_initialized!();
762
763        unsafe {
764            let meta = ffi::gst_buffer_add_video_afd_meta(
765                buffer.as_mut_ptr(),
766                field,
767                spec.into_glib(),
768                afd.into_glib(),
769            );
770
771            Self::from_mut_ptr(buffer, meta)
772        }
773    }
774
775    #[doc(alias = "get_field")]
776    #[inline]
777    pub fn field(&self) -> u8 {
778        self.0.field
779    }
780
781    #[doc(alias = "get_spec")]
782    #[inline]
783    pub fn spec(&self) -> crate::VideoAFDSpec {
784        unsafe { from_glib(self.0.spec) }
785    }
786
787    #[doc(alias = "get_afd")]
788    #[inline]
789    pub fn afd(&self) -> crate::VideoAFDValue {
790        unsafe { from_glib(self.0.afd) }
791    }
792}
793
794#[cfg(feature = "v1_18")]
795#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
796unsafe impl MetaAPI for VideoAFDMeta {
797    type GstType = ffi::GstVideoAFDMeta;
798
799    #[doc(alias = "gst_video_afd_meta_api_get_type")]
800    #[inline]
801    fn meta_api() -> glib::Type {
802        unsafe { from_glib(ffi::gst_video_afd_meta_api_get_type()) }
803    }
804}
805
806#[cfg(feature = "v1_18")]
807#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
808impl fmt::Debug for VideoAFDMeta {
809    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
810        f.debug_struct("VideoAFDMeta")
811            .field("field", &self.field())
812            .field("spec", &self.spec())
813            .field("afd", &self.afd())
814            .finish()
815    }
816}
817
818#[cfg(feature = "v1_18")]
819#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
820#[repr(transparent)]
821#[doc(alias = "GstVideoBarMeta")]
822pub struct VideoBarMeta(ffi::GstVideoBarMeta);
823
824#[cfg(feature = "v1_18")]
825#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
826unsafe impl Send for VideoBarMeta {}
827#[cfg(feature = "v1_18")]
828#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
829unsafe impl Sync for VideoBarMeta {}
830
831#[cfg(feature = "v1_18")]
832#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
833impl VideoBarMeta {
834    #[doc(alias = "gst_buffer_add_video_bar_meta")]
835    pub fn add(
836        buffer: &mut gst::BufferRef,
837        field: u8,
838        is_letterbox: bool,
839        bar_data1: u32,
840        bar_data2: u32,
841    ) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
842        skip_assert_initialized!();
843
844        unsafe {
845            let meta = ffi::gst_buffer_add_video_bar_meta(
846                buffer.as_mut_ptr(),
847                field,
848                is_letterbox.into_glib(),
849                bar_data1,
850                bar_data2,
851            );
852
853            Self::from_mut_ptr(buffer, meta)
854        }
855    }
856
857    #[doc(alias = "get_field")]
858    #[inline]
859    pub fn field(&self) -> u8 {
860        self.0.field
861    }
862
863    #[inline]
864    pub fn is_letterbox(&self) -> bool {
865        unsafe { from_glib(self.0.is_letterbox) }
866    }
867
868    #[doc(alias = "get_bar_data1")]
869    #[inline]
870    pub fn bar_data1(&self) -> u32 {
871        self.0.bar_data1
872    }
873
874    #[doc(alias = "get_bar_data2")]
875    #[inline]
876    pub fn bar_data2(&self) -> u32 {
877        self.0.bar_data2
878    }
879}
880
881#[cfg(feature = "v1_18")]
882#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
883unsafe impl MetaAPI for VideoBarMeta {
884    type GstType = ffi::GstVideoBarMeta;
885
886    #[doc(alias = "gst_video_bar_meta_api_get_type")]
887    #[inline]
888    fn meta_api() -> glib::Type {
889        unsafe { from_glib(ffi::gst_video_bar_meta_api_get_type()) }
890    }
891}
892
893#[cfg(feature = "v1_18")]
894#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
895impl fmt::Debug for VideoBarMeta {
896    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
897        f.debug_struct("VideoBarMeta")
898            .field("field", &self.field())
899            .field("is_letterbox", &self.is_letterbox())
900            .field("bar_data1", &self.bar_data1())
901            .field("bar_data2", &self.bar_data2())
902            .finish()
903    }
904}
905
906#[cfg(feature = "v1_20")]
907#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
908#[repr(transparent)]
909#[doc(alias = "GstVideoCodecAlphaMeta")]
910pub struct VideoCodecAlphaMeta(ffi::GstVideoCodecAlphaMeta);
911
912#[cfg(feature = "v1_20")]
913#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
914unsafe impl Send for VideoCodecAlphaMeta {}
915#[cfg(feature = "v1_20")]
916#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
917unsafe impl Sync for VideoCodecAlphaMeta {}
918
919#[cfg(feature = "v1_20")]
920#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
921impl VideoCodecAlphaMeta {
922    #[doc(alias = "gst_buffer_add_video_codec_alpha_meta")]
923    pub fn add(
924        buffer: &mut gst::BufferRef,
925        alpha_buffer: gst::Buffer,
926    ) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
927        skip_assert_initialized!();
928        unsafe {
929            let meta = ffi::gst_buffer_add_video_codec_alpha_meta(
930                buffer.as_mut_ptr(),
931                alpha_buffer.to_glib_none().0,
932            );
933
934            Self::from_mut_ptr(buffer, meta)
935        }
936    }
937
938    #[inline]
939    pub fn alpha_buffer(&self) -> &gst::BufferRef {
940        unsafe { gst::BufferRef::from_ptr(self.0.buffer) }
941    }
942
943    #[inline]
944    pub fn alpha_buffer_owned(&self) -> gst::Buffer {
945        unsafe { from_glib_none(self.0.buffer) }
946    }
947}
948
949#[cfg(feature = "v1_20")]
950#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
951unsafe impl MetaAPI for VideoCodecAlphaMeta {
952    type GstType = ffi::GstVideoCodecAlphaMeta;
953
954    #[doc(alias = "gst_video_codec_alpha_meta_api_get_type")]
955    #[inline]
956    fn meta_api() -> glib::Type {
957        unsafe { from_glib(ffi::gst_video_codec_alpha_meta_api_get_type()) }
958    }
959}
960
961#[cfg(feature = "v1_20")]
962#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
963impl fmt::Debug for VideoCodecAlphaMeta {
964    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
965        f.debug_struct("VideoCodecAlphaMeta")
966            .field("buffer", &self.alpha_buffer())
967            .finish()
968    }
969}
970
971#[cfg(feature = "v1_22")]
972#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
973#[repr(transparent)]
974#[doc(alias = "GstVideoSEIUserDataUnregisteredMeta")]
975pub struct VideoSeiUserDataUnregisteredMeta(ffi::GstVideoSEIUserDataUnregisteredMeta);
976
977#[cfg(feature = "v1_22")]
978#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
979unsafe impl Send for VideoSeiUserDataUnregisteredMeta {}
980#[cfg(feature = "v1_22")]
981#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
982unsafe impl Sync for VideoSeiUserDataUnregisteredMeta {}
983
984#[cfg(feature = "v1_22")]
985#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
986impl VideoSeiUserDataUnregisteredMeta {
987    #[doc(alias = "gst_buffer_add_video_sei_user_data_unregistered_meta")]
988    pub fn add<'a>(
989        buffer: &'a mut gst::BufferRef,
990        uuid: &[u8; 16],
991        data: &[u8],
992    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
993        skip_assert_initialized!();
994        assert!(!data.is_empty());
995        unsafe {
996            let meta = ffi::gst_buffer_add_video_sei_user_data_unregistered_meta(
997                buffer.as_mut_ptr(),
998                mut_override(uuid.as_ptr()),
999                mut_override(data.as_ptr()),
1000                data.len(),
1001            );
1002
1003            Self::from_mut_ptr(buffer, meta)
1004        }
1005    }
1006
1007    #[doc(alias = "get_data")]
1008    #[inline]
1009    pub fn data(&self) -> &[u8] {
1010        if self.0.size == 0 {
1011            return &[];
1012        }
1013        // SAFETY: In the C API we have a pointer data and a size variable
1014        // indicating the length of the data. Here we convert it to a size,
1015        // making sure we read the size specified in the C API.
1016        unsafe {
1017            use std::slice;
1018            slice::from_raw_parts(self.0.data, self.0.size)
1019        }
1020    }
1021
1022    #[doc(alias = "get_uuid")]
1023    #[inline]
1024    pub fn uuid(&self) -> [u8; 16] {
1025        self.0.uuid
1026    }
1027}
1028
1029#[cfg(feature = "v1_22")]
1030#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
1031impl fmt::Debug for VideoSeiUserDataUnregisteredMeta {
1032    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1033        f.debug_struct("VideoSeiUserDataUnregisteredMeta")
1034            .field(
1035                "uuid",
1036                &format!("0x{:032X}", u128::from_be_bytes(self.uuid())),
1037            )
1038            .field("data", &self.data())
1039            .finish()
1040    }
1041}
1042
1043#[cfg(feature = "v1_22")]
1044#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
1045unsafe impl MetaAPI for VideoSeiUserDataUnregisteredMeta {
1046    type GstType = ffi::GstVideoSEIUserDataUnregisteredMeta;
1047
1048    #[doc(alias = "gst_video_sei_user_data_unregistered_meta_api_get_type")]
1049    fn meta_api() -> glib::Type {
1050        unsafe {
1051            glib::translate::from_glib(ffi::gst_video_sei_user_data_unregistered_meta_api_get_type())
1052        }
1053    }
1054}
1055
1056#[cfg(feature = "v1_24")]
1057#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
1058#[repr(transparent)]
1059#[doc(alias = "GstAncillaryMeta")]
1060pub struct AncillaryMeta(ffi::GstAncillaryMeta);
1061
1062#[cfg(feature = "v1_24")]
1063#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
1064unsafe impl Send for AncillaryMeta {}
1065#[cfg(feature = "v1_24")]
1066#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
1067unsafe impl Sync for AncillaryMeta {}
1068
1069#[cfg(feature = "v1_24")]
1070#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
1071impl AncillaryMeta {
1072    #[doc(alias = "gst_buffer_add_ancillary_meta")]
1073    pub fn add(buffer: &mut gst::BufferRef) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
1074        skip_assert_initialized!();
1075        unsafe {
1076            let meta = ffi::gst_buffer_add_ancillary_meta(buffer.as_mut_ptr());
1077
1078            Self::from_mut_ptr(buffer, meta)
1079        }
1080    }
1081
1082    #[inline]
1083    pub fn field(&self) -> crate::AncillaryMetaField {
1084        unsafe { from_glib(self.0.field) }
1085    }
1086
1087    #[inline]
1088    pub fn set_field(&mut self, field: crate::AncillaryMetaField) {
1089        self.0.field = field.into_glib();
1090    }
1091
1092    #[inline]
1093    pub fn c_not_y_channel(&self) -> bool {
1094        unsafe { from_glib(self.0.c_not_y_channel) }
1095    }
1096
1097    #[inline]
1098    pub fn set_c_not_y_channel(&mut self, c_not_y_channel: bool) {
1099        self.0.c_not_y_channel = c_not_y_channel.into_glib();
1100    }
1101
1102    #[inline]
1103    pub fn line(&self) -> u16 {
1104        self.0.line
1105    }
1106
1107    #[inline]
1108    pub fn set_line(&mut self, line: u16) {
1109        self.0.line = line;
1110    }
1111
1112    #[inline]
1113    pub fn offset(&self) -> u16 {
1114        self.0.offset
1115    }
1116
1117    #[inline]
1118    pub fn set_offset(&mut self, offset: u16) {
1119        self.0.offset = offset;
1120    }
1121
1122    #[inline]
1123    pub fn did(&self) -> u16 {
1124        self.0.DID
1125    }
1126
1127    #[inline]
1128    pub fn set_did(&mut self, did: u16) {
1129        self.0.DID = did;
1130    }
1131
1132    #[inline]
1133    pub fn sdid_block_number(&self) -> u16 {
1134        self.0.SDID_block_number
1135    }
1136
1137    #[inline]
1138    pub fn set_sdid_block_number(&mut self, sdid_block_number: u16) {
1139        self.0.SDID_block_number = sdid_block_number;
1140    }
1141
1142    #[inline]
1143    pub fn data_count(&self) -> u16 {
1144        self.0.data_count
1145    }
1146
1147    #[inline]
1148    pub fn checksum(&self) -> u16 {
1149        self.0.checksum
1150    }
1151
1152    #[inline]
1153    pub fn set_checksum(&mut self, checksum: u16) {
1154        self.0.checksum = checksum;
1155    }
1156
1157    #[inline]
1158    pub fn data(&self) -> &[u16] {
1159        if self.0.data_count & 0xff == 0 {
1160            return &[];
1161        }
1162        unsafe {
1163            use std::slice;
1164
1165            slice::from_raw_parts(self.0.data, (self.0.data_count & 0xff) as usize)
1166        }
1167    }
1168
1169    #[inline]
1170    pub fn data_mut(&mut self) -> &mut [u16] {
1171        if self.0.data_count & 0xff == 0 {
1172            return &mut [];
1173        }
1174        unsafe {
1175            use std::slice;
1176
1177            slice::from_raw_parts_mut(self.0.data, (self.0.data_count & 0xff) as usize)
1178        }
1179    }
1180
1181    #[inline]
1182    pub fn set_data(&mut self, data: glib::Slice<u16>) {
1183        unsafe {
1184            assert!(data.len() < 256);
1185            self.0.data_count = data.len() as u16;
1186            self.0.data = data.into_glib_ptr();
1187        }
1188    }
1189
1190    #[inline]
1191    pub fn set_data_count_upper_two_bits(&mut self, upper_two_bits: u8) {
1192        assert!(upper_two_bits & !0x03 == 0);
1193        self.0.data_count = ((upper_two_bits as u16) << 8) | self.0.data_count & 0xff;
1194    }
1195}
1196
1197#[cfg(feature = "v1_24")]
1198#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
1199unsafe impl MetaAPI for AncillaryMeta {
1200    type GstType = ffi::GstAncillaryMeta;
1201
1202    #[doc(alias = "gst_ancillary_meta_api_get_type")]
1203    #[inline]
1204    fn meta_api() -> glib::Type {
1205        unsafe { from_glib(ffi::gst_ancillary_meta_api_get_type()) }
1206    }
1207}
1208
1209#[cfg(feature = "v1_24")]
1210#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
1211impl fmt::Debug for AncillaryMeta {
1212    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1213        f.debug_struct("AncillaryMeta")
1214            .field("field", &self.field())
1215            .field("c_not_y_channel", &self.c_not_y_channel())
1216            .field("line", &self.line())
1217            .field("offset", &self.offset())
1218            .field("did", &self.did())
1219            .field("sdid_block_number", &self.sdid_block_number())
1220            .field("data_count", &self.data_count())
1221            .field("data", &self.data())
1222            .field("checksum", &self.checksum())
1223            .finish()
1224    }
1225}
1226
1227pub mod tags {
1228    gst::impl_meta_tag!(Video, crate::ffi::GST_META_TAG_VIDEO_STR);
1229    gst::impl_meta_tag!(Size, crate::ffi::GST_META_TAG_VIDEO_SIZE_STR);
1230    gst::impl_meta_tag!(Orientation, crate::ffi::GST_META_TAG_VIDEO_ORIENTATION_STR);
1231    gst::impl_meta_tag!(Colorspace, crate::ffi::GST_META_TAG_VIDEO_COLORSPACE_STR);
1232}
1233
1234#[derive(Debug, Clone, PartialEq, Eq)]
1235pub struct VideoMetaTransformScale<'a> {
1236    in_info: &'a crate::VideoInfo,
1237    out_info: &'a crate::VideoInfo,
1238}
1239
1240impl<'a> VideoMetaTransformScale<'a> {
1241    pub fn new(in_info: &'a crate::VideoInfo, out_info: &'a crate::VideoInfo) -> Self {
1242        skip_assert_initialized!();
1243        VideoMetaTransformScale { in_info, out_info }
1244    }
1245}
1246
1247unsafe impl<'a> gst::meta::MetaTransform<'a> for VideoMetaTransformScale<'a> {
1248    type GLibType = ffi::GstVideoMetaTransform;
1249
1250    #[doc(alias = "gst_video_meta_transform_scale_get_quark")]
1251    fn quark() -> glib::Quark {
1252        unsafe { from_glib(ffi::gst_video_meta_transform_scale_get_quark()) }
1253    }
1254
1255    fn to_raw<T: MetaAPI>(
1256        &self,
1257        _meta: &gst::MetaRef<T>,
1258    ) -> Result<ffi::GstVideoMetaTransform, glib::BoolError> {
1259        Ok(ffi::GstVideoMetaTransform {
1260            in_info: mut_override(self.in_info.to_glib_none().0),
1261            out_info: mut_override(self.out_info.to_glib_none().0),
1262        })
1263    }
1264}
1265
1266#[cfg(test)]
1267mod tests {
1268    use super::*;
1269
1270    #[test]
1271    fn test_add_get_meta() {
1272        gst::init().unwrap();
1273
1274        let mut buffer = gst::Buffer::with_size(320 * 240 * 4).unwrap();
1275        {
1276            let meta = VideoMeta::add(
1277                buffer.get_mut().unwrap(),
1278                crate::VideoFrameFlags::empty(),
1279                crate::VideoFormat::Argb,
1280                320,
1281                240,
1282            )
1283            .unwrap();
1284            assert_eq!(meta.id(), 0);
1285            assert_eq!(meta.video_frame_flags(), crate::VideoFrameFlags::empty());
1286            assert_eq!(meta.format(), crate::VideoFormat::Argb);
1287            assert_eq!(meta.width(), 320);
1288            assert_eq!(meta.height(), 240);
1289            assert_eq!(meta.n_planes(), 1);
1290            assert_eq!(meta.offset(), &[0]);
1291            assert_eq!(meta.stride(), &[320 * 4]);
1292            assert!(meta.has_tag::<gst::meta::tags::Memory>());
1293            assert!(meta.has_tag::<tags::Video>());
1294            assert!(meta.has_tag::<tags::Colorspace>());
1295            assert!(meta.has_tag::<tags::Size>());
1296        }
1297
1298        {
1299            let meta = buffer.meta::<VideoMeta>().unwrap();
1300            assert_eq!(meta.id(), 0);
1301            assert_eq!(meta.video_frame_flags(), crate::VideoFrameFlags::empty());
1302            assert_eq!(meta.format(), crate::VideoFormat::Argb);
1303            assert_eq!(meta.width(), 320);
1304            assert_eq!(meta.height(), 240);
1305            assert_eq!(meta.n_planes(), 1);
1306            assert_eq!(meta.offset(), &[0]);
1307            assert_eq!(meta.stride(), &[320 * 4]);
1308        }
1309    }
1310
1311    #[test]
1312    fn test_add_full_get_meta() {
1313        gst::init().unwrap();
1314
1315        let mut buffer = gst::Buffer::with_size(320 * 240 * 4).unwrap();
1316        {
1317            let meta = VideoMeta::add_full(
1318                buffer.get_mut().unwrap(),
1319                crate::VideoFrameFlags::empty(),
1320                crate::VideoFormat::Argb,
1321                320,
1322                240,
1323                &[0],
1324                &[320 * 4],
1325            )
1326            .unwrap();
1327            assert_eq!(meta.id(), 0);
1328            assert_eq!(meta.video_frame_flags(), crate::VideoFrameFlags::empty());
1329            assert_eq!(meta.format(), crate::VideoFormat::Argb);
1330            assert_eq!(meta.width(), 320);
1331            assert_eq!(meta.height(), 240);
1332            assert_eq!(meta.n_planes(), 1);
1333            assert_eq!(meta.offset(), &[0]);
1334            assert_eq!(meta.stride(), &[320 * 4]);
1335        }
1336
1337        {
1338            let meta = buffer.meta::<VideoMeta>().unwrap();
1339            assert_eq!(meta.id(), 0);
1340            assert_eq!(meta.video_frame_flags(), crate::VideoFrameFlags::empty());
1341            assert_eq!(meta.format(), crate::VideoFormat::Argb);
1342            assert_eq!(meta.width(), 320);
1343            assert_eq!(meta.height(), 240);
1344            assert_eq!(meta.n_planes(), 1);
1345            assert_eq!(meta.offset(), &[0]);
1346            assert_eq!(meta.stride(), &[320 * 4]);
1347        }
1348    }
1349
1350    #[test]
1351    #[cfg(feature = "v1_16")]
1352    fn test_add_full_alternate_interlacing() {
1353        gst::init().unwrap();
1354        let mut buffer = gst::Buffer::with_size(320 * 120 * 4).unwrap();
1355        VideoMeta::add_full(
1356            buffer.get_mut().unwrap(),
1357            crate::VideoFrameFlags::TOP_FIELD,
1358            crate::VideoFormat::Argb,
1359            320,
1360            240,
1361            &[0],
1362            &[320 * 4],
1363        )
1364        .unwrap();
1365    }
1366
1367    #[test]
1368    #[cfg(feature = "v1_18")]
1369    fn test_video_meta_alignment() {
1370        gst::init().unwrap();
1371
1372        let mut buffer = gst::Buffer::with_size(115200).unwrap();
1373        let meta = VideoMeta::add(
1374            buffer.get_mut().unwrap(),
1375            crate::VideoFrameFlags::empty(),
1376            crate::VideoFormat::Nv12,
1377            320,
1378            240,
1379        )
1380        .unwrap();
1381
1382        let alig = meta.alignment();
1383        assert_eq!(alig, crate::VideoAlignment::new(0, 0, 0, 0, &[0, 0, 0, 0]));
1384
1385        assert_eq!(meta.plane_size().unwrap(), [76800, 38400, 0, 0]);
1386        assert_eq!(meta.plane_height().unwrap(), [240, 120, 0, 0]);
1387
1388        /* horizontal padding */
1389        let mut info = crate::VideoInfo::builder(crate::VideoFormat::Nv12, 320, 240)
1390            .build()
1391            .expect("Failed to create VideoInfo");
1392        let mut alig = crate::VideoAlignment::new(0, 0, 2, 6, &[0, 0, 0, 0]);
1393        info.align(&mut alig).unwrap();
1394
1395        let mut meta = VideoMeta::add_full(
1396            buffer.get_mut().unwrap(),
1397            crate::VideoFrameFlags::empty(),
1398            crate::VideoFormat::Nv12,
1399            info.width(),
1400            info.height(),
1401            info.offset(),
1402            info.stride(),
1403        )
1404        .unwrap();
1405        meta.set_alignment(&alig).unwrap();
1406
1407        let alig = meta.alignment();
1408        assert_eq!(alig, crate::VideoAlignment::new(0, 0, 2, 6, &[0, 0, 0, 0]));
1409
1410        assert_eq!(meta.plane_size().unwrap(), [78720, 39360, 0, 0]);
1411        assert_eq!(meta.plane_height().unwrap(), [240, 120, 0, 0]);
1412
1413        /* vertical alignment */
1414        let mut info = crate::VideoInfo::builder(crate::VideoFormat::Nv12, 320, 240)
1415            .build()
1416            .expect("Failed to create VideoInfo");
1417        let mut alig = crate::VideoAlignment::new(2, 6, 0, 0, &[0, 0, 0, 0]);
1418        info.align(&mut alig).unwrap();
1419
1420        let mut meta = VideoMeta::add_full(
1421            buffer.get_mut().unwrap(),
1422            crate::VideoFrameFlags::empty(),
1423            crate::VideoFormat::Nv12,
1424            info.width(),
1425            info.height(),
1426            info.offset(),
1427            info.stride(),
1428        )
1429        .unwrap();
1430        meta.set_alignment(&alig).unwrap();
1431
1432        let alig = meta.alignment();
1433        assert_eq!(alig, crate::VideoAlignment::new(2, 6, 0, 0, &[0, 0, 0, 0]));
1434
1435        assert_eq!(meta.plane_size().unwrap(), [79360, 39680, 0, 0]);
1436        assert_eq!(meta.plane_height().unwrap(), [248, 124, 0, 0]);
1437    }
1438
1439    #[test]
1440    #[cfg(feature = "v1_22")]
1441    fn test_get_video_sei_user_data_unregistered_meta() {
1442        gst::init().unwrap();
1443
1444        const META_UUID: &[u8; 16] = &[
1445            0x4D, 0x49, 0x53, 0x50, 0x6D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x65, 0x63, 0x74, 0x69,
1446            0x6D, 0x65,
1447        ];
1448
1449        const META_DATA: &[u8] = &[
1450            0x1f, 0x00, 0x05, 0xff, 0x21, 0x7e, 0xff, 0x29, 0xb5, 0xff, 0xdc, 0x13,
1451        ];
1452
1453        let buffer_data = &[
1454            &[0x00, 0x00, 0x00, 0x20, 0x06, 0x05, 0x1c],
1455            META_UUID as &[u8],
1456            META_DATA,
1457            &[
1458                0x80, 0x00, 0x00, 0x00, 0x14, 0x65, 0x88, 0x84, 0x00, 0x10, 0xff, 0xfe, 0xf6, 0xf0,
1459                0xfe, 0x05, 0x36, 0x56, 0x04, 0x50, 0x96, 0x7b, 0x3f, 0x53, 0xe1,
1460            ],
1461        ]
1462        .concat();
1463
1464        let mut harness = gst_check::Harness::new("h264parse");
1465        harness.set_src_caps_str(r#"
1466            video/x-h264, stream-format=(string)avc,
1467            width=(int)1920, height=(int)1080, framerate=(fraction)25/1,
1468            bit-depth-chroma=(uint)8, parsed=(boolean)true,
1469            alignment=(string)au, profile=(string)high, level=(string)4,
1470            codec_data=(buffer)01640028ffe1001a67640028acb200f0044fcb080000030008000003019478c1924001000568ebccb22c
1471        "#);
1472        let buffer = gst::Buffer::from_slice(buffer_data.clone());
1473        let buffer = harness.push_and_pull(buffer).unwrap();
1474
1475        let meta = buffer.meta::<VideoSeiUserDataUnregisteredMeta>().unwrap();
1476        assert_eq!(meta.uuid(), *META_UUID);
1477        assert_eq!(meta.data(), META_DATA);
1478        assert_eq!(meta.data().len(), META_DATA.len());
1479    }
1480
1481    #[test]
1482    fn test_meta_video_transform() {
1483        gst::init().unwrap();
1484
1485        let mut buffer = gst::Buffer::with_size(320 * 240 * 4).unwrap();
1486        let meta = VideoCropMeta::add(buffer.get_mut().unwrap(), (10, 10, 20, 20));
1487
1488        let mut buffer2 = gst::Buffer::with_size(640 * 480 * 4).unwrap();
1489
1490        let in_video_info = crate::VideoInfo::builder(crate::VideoFormat::Rgba, 320, 240)
1491            .build()
1492            .unwrap();
1493        let out_video_info = crate::VideoInfo::builder(crate::VideoFormat::Rgba, 640, 480)
1494            .build()
1495            .unwrap();
1496
1497        meta.transform(
1498            buffer2.get_mut().unwrap(),
1499            &VideoMetaTransformScale::new(&in_video_info, &out_video_info),
1500        )
1501        .unwrap();
1502
1503        let meta2 = buffer2.meta::<VideoCropMeta>().unwrap();
1504
1505        assert_eq!(meta2.rect(), (20, 20, 40, 40));
1506    }
1507}