1use std::{fmt, marker::PhantomData, mem, ptr, str};
4
5use crate::ffi;
6use glib::translate::*;
7use gst::prelude::*;
8
9#[doc(alias = "GST_VIDEO_MAX_PLANES")]
10pub const VIDEO_MAX_PLANES: usize = ffi::GST_VIDEO_MAX_PLANES as usize;
11
12#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
13#[non_exhaustive]
14#[doc(alias = "GstVideoColorRange")]
15pub enum VideoColorRange {
16 #[doc(alias = "GST_VIDEO_COLOR_RANGE_UNKNOWN")]
17 Unknown,
18 #[doc(alias = "GST_VIDEO_COLOR_RANGE_0_255")]
19 Range0_255,
20 #[doc(alias = "GST_VIDEO_COLOR_RANGE_16_235")]
21 Range16_235,
22 #[doc(hidden)]
23 __Unknown(i32),
24}
25
26#[doc(hidden)]
27impl IntoGlib for VideoColorRange {
28 type GlibType = ffi::GstVideoColorRange;
29
30 #[inline]
31 fn into_glib(self) -> ffi::GstVideoColorRange {
32 match self {
33 Self::Unknown => ffi::GST_VIDEO_COLOR_RANGE_UNKNOWN,
34 Self::Range0_255 => ffi::GST_VIDEO_COLOR_RANGE_0_255,
35 Self::Range16_235 => ffi::GST_VIDEO_COLOR_RANGE_16_235,
36 Self::__Unknown(value) => value,
37 }
38 }
39}
40
41#[doc(hidden)]
42impl FromGlib<ffi::GstVideoColorRange> for VideoColorRange {
43 #[inline]
44 unsafe fn from_glib(value: ffi::GstVideoColorRange) -> Self {
45 skip_assert_initialized!();
46 match value {
47 0 => Self::Unknown,
48 1 => Self::Range0_255,
49 2 => Self::Range16_235,
50 value => Self::__Unknown(value),
51 }
52 }
53}
54
55impl StaticType for VideoColorRange {
56 #[inline]
57 fn static_type() -> glib::Type {
58 unsafe { from_glib(ffi::gst_video_color_range_get_type()) }
59 }
60}
61
62impl glib::value::ValueType for VideoColorRange {
63 type Type = Self;
64}
65
66unsafe impl<'a> glib::value::FromValue<'a> for VideoColorRange {
67 type Checker = glib::value::GenericValueTypeChecker<Self>;
68
69 unsafe fn from_value(value: &'a glib::Value) -> Self {
70 skip_assert_initialized!();
71 from_glib(glib::gobject_ffi::g_value_get_enum(value.to_glib_none().0))
72 }
73}
74
75impl ToValue for VideoColorRange {
76 fn to_value(&self) -> glib::Value {
77 let mut value = glib::Value::for_value_type::<Self>();
78 unsafe { glib::gobject_ffi::g_value_set_enum(value.to_glib_none_mut().0, self.into_glib()) }
79 value
80 }
81
82 fn value_type(&self) -> glib::Type {
83 Self::static_type()
84 }
85}
86
87impl From<VideoColorRange> for glib::Value {
88 fn from(v: VideoColorRange) -> glib::Value {
89 skip_assert_initialized!();
90 glib::value::ToValue::to_value(&v)
91 }
92}
93
94#[doc(alias = "GstVideoColorimetry")]
95#[derive(Copy, Clone)]
96#[repr(transparent)]
97pub struct VideoColorimetry(ffi::GstVideoColorimetry);
98
99impl VideoColorimetry {
100 pub fn new(
101 range: crate::VideoColorRange,
102 matrix: crate::VideoColorMatrix,
103 transfer: crate::VideoTransferFunction,
104 primaries: crate::VideoColorPrimaries,
105 ) -> Self {
106 skip_assert_initialized!();
107
108 let colorimetry = ffi::GstVideoColorimetry {
109 range: range.into_glib(),
110 matrix: matrix.into_glib(),
111 transfer: transfer.into_glib(),
112 primaries: primaries.into_glib(),
113 };
114
115 Self(colorimetry)
116 }
117
118 #[inline]
119 pub fn range(&self) -> crate::VideoColorRange {
120 unsafe { from_glib(self.0.range) }
121 }
122
123 #[inline]
124 pub fn matrix(&self) -> crate::VideoColorMatrix {
125 unsafe { from_glib(self.0.matrix) }
126 }
127
128 #[inline]
129 pub fn transfer(&self) -> crate::VideoTransferFunction {
130 unsafe { from_glib(self.0.transfer) }
131 }
132
133 #[inline]
134 pub fn primaries(&self) -> crate::VideoColorPrimaries {
135 unsafe { from_glib(self.0.primaries) }
136 }
137
138 #[cfg(feature = "v1_22")]
139 #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
140 #[doc(alias = "gst_video_colorimetry_is_equivalent")]
141 pub fn is_equivalent(&self, bitdepth: u32, other: &Self, other_bitdepth: u32) -> bool {
142 unsafe {
143 from_glib(ffi::gst_video_colorimetry_is_equivalent(
144 &self.0,
145 bitdepth,
146 &other.0,
147 other_bitdepth,
148 ))
149 }
150 }
151}
152
153impl PartialEq for VideoColorimetry {
154 #[doc(alias = "gst_video_colorimetry_is_equal")]
155 fn eq(&self, other: &Self) -> bool {
156 unsafe { from_glib(ffi::gst_video_colorimetry_is_equal(&self.0, &other.0)) }
157 }
158}
159
160impl Eq for VideoColorimetry {}
161
162impl str::FromStr for crate::VideoColorimetry {
163 type Err = glib::error::BoolError;
164
165 #[doc(alias = "gst_video_colorimetry_from_string")]
166 fn from_str(s: &str) -> Result<Self, Self::Err> {
167 assert_initialized_main_thread!();
168
169 unsafe {
170 let mut colorimetry = mem::MaybeUninit::uninit();
171 let valid: bool = from_glib(ffi::gst_video_colorimetry_from_string(
172 colorimetry.as_mut_ptr(),
173 s.to_glib_none().0,
174 ));
175 if valid {
176 Ok(Self(colorimetry.assume_init()))
177 } else {
178 Err(glib::bool_error!("Invalid colorimetry info"))
179 }
180 }
181 }
182}
183
184impl fmt::Debug for crate::VideoColorimetry {
185 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186 f.debug_struct("VideoColorimetry")
187 .field("range", &self.0.range)
188 .field("matrix", &self.0.matrix)
189 .field("transfer", &self.0.transfer)
190 .field("primaries", &self.0.primaries)
191 .finish()
192 }
193}
194
195impl fmt::Display for crate::VideoColorimetry {
196 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
197 let s =
198 unsafe { glib::GString::from_glib_full(ffi::gst_video_colorimetry_to_string(&self.0)) };
199 f.write_str(&s)
200 }
201}
202
203impl crate::VideoChromaSite {
204 #[doc(alias = "gst_video_chroma_site_to_string")]
205 #[doc(alias = "gst_video_chroma_to_string")]
206 pub fn to_str(self) -> glib::GString {
207 assert_initialized_main_thread!();
208
209 unsafe {
210 cfg_if::cfg_if! {
211 if #[cfg(feature = "v1_20")] {
212 from_glib_full(ffi::gst_video_chroma_site_to_string(self.into_glib()))
213 } else {
214 from_glib_none(ffi::gst_video_chroma_to_string(self.into_glib()))
215 }
216 }
217 }
218 }
219}
220
221impl str::FromStr for crate::VideoChromaSite {
222 type Err = glib::error::BoolError;
223
224 #[doc(alias = "gst_video_chroma_from_string")]
225 fn from_str(s: &str) -> Result<Self, Self::Err> {
226 skip_assert_initialized!();
227
228 cfg_if::cfg_if! {
229 if #[cfg(feature = "v1_20")] {
230 let chroma_site = Self::from_string(s);
231 } else {
232 assert_initialized_main_thread!();
233 let chroma_site: Self =
234 unsafe { from_glib(ffi::gst_video_chroma_from_string(s.to_glib_none().0)) };
235 }
236 };
237
238 if chroma_site.is_empty() {
239 Err(glib::bool_error!("Invalid chroma site"))
240 } else {
241 Ok(chroma_site)
242 }
243 }
244}
245
246impl From<crate::VideoMultiviewFramePacking> for crate::VideoMultiviewMode {
247 #[inline]
248 fn from(v: crate::VideoMultiviewFramePacking) -> Self {
249 skip_assert_initialized!();
250 unsafe { from_glib(v.into_glib()) }
251 }
252}
253
254impl TryFrom<crate::VideoMultiviewMode> for crate::VideoMultiviewFramePacking {
255 type Error = glib::BoolError;
256
257 fn try_from(v: crate::VideoMultiviewMode) -> Result<Self, glib::BoolError> {
258 skip_assert_initialized!();
259
260 let v2 = unsafe { from_glib(v.into_glib()) };
261
262 if let Self::__Unknown(_) = v2 {
263 Err(glib::bool_error!("Invalid frame packing mode"))
264 } else {
265 Ok(v2)
266 }
267 }
268}
269
270#[doc(alias = "GstVideoInfo")]
271#[derive(Clone)]
272#[repr(transparent)]
273pub struct VideoInfo(pub(crate) ffi::GstVideoInfo);
274
275impl fmt::Debug for VideoInfo {
276 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277 f.debug_struct("VideoInfo")
278 .field("format", &self.format())
279 .field("format-info", &self.format_info())
280 .field("width", &self.width())
281 .field("height", &self.height())
282 .field("interlace_mode", &self.interlace_mode())
283 .field("flags", &self.flags())
284 .field("size", &self.size())
285 .field("views", &self.views())
286 .field("chroma_site", &self.chroma_site())
287 .field("colorimetry", &self.colorimetry())
288 .field("par", &self.par())
289 .field("fps", &self.fps())
290 .field("offset", &self.offset())
291 .field("stride", &self.stride())
292 .field("multiview_mode", &self.multiview_mode())
293 .field("multiview_flags", &self.multiview_flags())
294 .field("field_order", &self.field_order())
295 .finish()
296 }
297}
298
299#[derive(Debug)]
300#[must_use = "The builder must be built to be used"]
301pub struct VideoInfoBuilder<'a> {
302 format: crate::VideoFormat,
303 width: u32,
304 height: u32,
305 interlace_mode: Option<crate::VideoInterlaceMode>,
306 flags: Option<crate::VideoFlags>,
307 size: Option<usize>,
308 views: Option<u32>,
309 chroma_site: Option<crate::VideoChromaSite>,
310 colorimetry: Option<crate::VideoColorimetry>,
311 par: Option<gst::Fraction>,
312 fps: Option<gst::Fraction>,
313 offset: Option<&'a [usize]>,
314 stride: Option<&'a [i32]>,
315 multiview_mode: Option<crate::VideoMultiviewMode>,
316 multiview_flags: Option<crate::VideoMultiviewFlags>,
317 field_order: Option<crate::VideoFieldOrder>,
318}
319
320impl<'a> VideoInfoBuilder<'a> {
321 pub fn build(self) -> Result<VideoInfo, glib::error::BoolError> {
322 unsafe {
323 let mut info = mem::MaybeUninit::uninit();
324
325 cfg_if::cfg_if! {
326 if #[cfg(feature = "v1_16")] {
327 let res: bool = {
328 from_glib(if let Some(interlace_mode) = self.interlace_mode {
329 ffi::gst_video_info_set_interlaced_format(
330 info.as_mut_ptr(),
331 self.format.into_glib(),
332 interlace_mode.into_glib(),
333 self.width,
334 self.height,
335 )
336 } else {
337 ffi::gst_video_info_set_format(
338 info.as_mut_ptr(),
339 self.format.into_glib(),
340 self.width,
341 self.height,
342 )
343 })
344 };
345 } else {
346 let res: bool = {
347 let res = from_glib(ffi::gst_video_info_set_format(
348 info.as_mut_ptr(),
349 self.format.into_glib(),
350 self.width,
351 self.height,
352 ));
353
354 if res {
355 if let Some(interlace_mode) = self.interlace_mode {
356 let info = info.as_mut_ptr();
357 (*info).interlace_mode = interlace_mode.into_glib();
358 }
359 }
360
361 res
362 };
363 }
364 }
365
366 if !res {
367 return Err(glib::bool_error!("Failed to build VideoInfo"));
368 }
369
370 let mut info = info.assume_init();
371
372 if info.finfo.is_null() || info.width <= 0 || info.height <= 0 {
373 return Err(glib::bool_error!("Failed to build VideoInfo"));
374 }
375
376 if let Some(flags) = self.flags {
377 info.flags = flags.into_glib();
378 }
379
380 if let Some(size) = self.size {
381 info.size = size;
382 }
383
384 if let Some(views) = self.views {
385 info.views = views as i32;
386 }
387
388 if let Some(chroma_site) = self.chroma_site {
389 info.chroma_site = chroma_site.into_glib();
390 }
391
392 if let Some(colorimetry) = self.colorimetry {
393 ptr::write(&mut info.colorimetry, ptr::read(&colorimetry.0));
394 }
395
396 if let Some(par) = self.par {
397 info.par_n = par.numer();
398 info.par_d = par.denom();
399 }
400
401 if let Some(fps) = self.fps {
402 info.fps_n = fps.numer();
403 info.fps_d = fps.denom();
404 }
405
406 if let Some(offset) = self.offset {
407 info.offset[..offset.len()].copy_from_slice(offset);
408 }
409
410 if let Some(stride) = self.stride {
411 info.stride[..stride.len()].copy_from_slice(stride);
412 }
413
414 if let Some(multiview_mode) = self.multiview_mode {
415 info.ABI.abi.multiview_mode = multiview_mode.into_glib();
416 }
417
418 if let Some(multiview_flags) = self.multiview_flags {
419 info.ABI.abi.multiview_flags = multiview_flags.into_glib();
420 }
421
422 if let Some(field_order) = self.field_order {
423 info.ABI.abi.field_order = field_order.into_glib();
424 }
425
426 Ok(VideoInfo(info))
427 }
428 }
429
430 pub fn interlace_mode(self, interlace_mode: crate::VideoInterlaceMode) -> VideoInfoBuilder<'a> {
431 Self {
432 interlace_mode: Some(interlace_mode),
433 ..self
434 }
435 }
436
437 pub fn interlace_mode_if(
438 self,
439 interlace_mode: crate::VideoInterlaceMode,
440 predicate: bool,
441 ) -> VideoInfoBuilder<'a> {
442 if predicate {
443 self.interlace_mode(interlace_mode)
444 } else {
445 self
446 }
447 }
448
449 pub fn interlace_mode_if_some(
450 self,
451 interlace_mode: Option<crate::VideoInterlaceMode>,
452 ) -> VideoInfoBuilder<'a> {
453 if let Some(interlace_mode) = interlace_mode {
454 self.interlace_mode(interlace_mode)
455 } else {
456 self
457 }
458 }
459
460 pub fn flags(self, flags: crate::VideoFlags) -> Self {
461 Self {
462 flags: Some(flags),
463 ..self
464 }
465 }
466
467 pub fn flags_if(self, flags: crate::VideoFlags, predicate: bool) -> Self {
468 if predicate {
469 self.flags(flags)
470 } else {
471 self
472 }
473 }
474
475 pub fn flags_if_some(self, flags: Option<crate::VideoFlags>) -> Self {
476 if let Some(flags) = flags {
477 self.flags(flags)
478 } else {
479 self
480 }
481 }
482
483 pub fn size(self, size: usize) -> Self {
484 Self {
485 size: Some(size),
486 ..self
487 }
488 }
489
490 pub fn size_if(self, size: usize, predicate: bool) -> Self {
491 if predicate {
492 self.size(size)
493 } else {
494 self
495 }
496 }
497
498 pub fn size_if_some(self, size: Option<usize>) -> Self {
499 if let Some(size) = size {
500 self.size(size)
501 } else {
502 self
503 }
504 }
505
506 pub fn views(self, views: u32) -> Self {
507 Self {
508 views: Some(views),
509 ..self
510 }
511 }
512
513 pub fn views_if(self, views: u32, predicate: bool) -> Self {
514 if predicate {
515 self.views(views)
516 } else {
517 self
518 }
519 }
520
521 pub fn views_if_some(self, views: Option<u32>) -> Self {
522 if let Some(views) = views {
523 self.views(views)
524 } else {
525 self
526 }
527 }
528
529 pub fn chroma_site(self, chroma_site: crate::VideoChromaSite) -> Self {
530 Self {
531 chroma_site: Some(chroma_site),
532 ..self
533 }
534 }
535
536 pub fn chroma_site_if(self, chroma_site: crate::VideoChromaSite, predicate: bool) -> Self {
537 if predicate {
538 self.chroma_site(chroma_site)
539 } else {
540 self
541 }
542 }
543
544 pub fn chroma_site_if_some(self, chroma_site: Option<crate::VideoChromaSite>) -> Self {
545 if let Some(chroma_site) = chroma_site {
546 self.chroma_site(chroma_site)
547 } else {
548 self
549 }
550 }
551
552 pub fn colorimetry(self, colorimetry: &crate::VideoColorimetry) -> VideoInfoBuilder<'a> {
553 Self {
554 colorimetry: Some(*colorimetry),
555 ..self
556 }
557 }
558
559 pub fn colorimetry_if(
560 self,
561 colorimetry: &crate::VideoColorimetry,
562 predicate: bool,
563 ) -> VideoInfoBuilder<'a> {
564 if predicate {
565 self.colorimetry(colorimetry)
566 } else {
567 self
568 }
569 }
570
571 pub fn colorimetry_if_some(
572 self,
573 colorimetry: Option<&crate::VideoColorimetry>,
574 ) -> VideoInfoBuilder<'a> {
575 if let Some(colorimetry) = colorimetry {
576 self.colorimetry(colorimetry)
577 } else {
578 self
579 }
580 }
581
582 pub fn par<T: Into<gst::Fraction>>(self, par: T) -> Self {
583 Self {
584 par: Some(par.into()),
585 ..self
586 }
587 }
588
589 pub fn par_if<T: Into<gst::Fraction>>(self, par: T, predicate: bool) -> Self {
590 if predicate {
591 self.par(par)
592 } else {
593 self
594 }
595 }
596
597 pub fn par_if_some<T: Into<gst::Fraction>>(self, par: Option<T>) -> Self {
598 if let Some(par) = par {
599 self.par(par)
600 } else {
601 self
602 }
603 }
604
605 pub fn fps<T: Into<gst::Fraction>>(self, fps: T) -> Self {
606 Self {
607 fps: Some(fps.into()),
608 ..self
609 }
610 }
611
612 pub fn fps_if<T: Into<gst::Fraction>>(self, fps: T, predicate: bool) -> Self {
613 if predicate {
614 self.fps(fps)
615 } else {
616 self
617 }
618 }
619
620 pub fn fps_if_some<T: Into<gst::Fraction>>(self, fps: Option<T>) -> Self {
621 if let Some(fps) = fps {
622 self.fps(fps)
623 } else {
624 self
625 }
626 }
627
628 pub fn offset(self, offset: &'a [usize]) -> VideoInfoBuilder<'a> {
629 Self {
630 offset: Some(offset),
631 ..self
632 }
633 }
634
635 pub fn offset_if(self, offset: &'a [usize], predicate: bool) -> VideoInfoBuilder<'a> {
636 if predicate {
637 self.offset(offset)
638 } else {
639 self
640 }
641 }
642
643 pub fn offset_if_some(self, offset: Option<&'a [usize]>) -> VideoInfoBuilder<'a> {
644 if let Some(offset) = offset {
645 self.offset(offset)
646 } else {
647 self
648 }
649 }
650
651 pub fn stride(self, stride: &'a [i32]) -> VideoInfoBuilder<'a> {
652 Self {
653 stride: Some(stride),
654 ..self
655 }
656 }
657
658 pub fn stride_if(self, stride: &'a [i32], predicate: bool) -> VideoInfoBuilder<'a> {
659 if predicate {
660 self.stride(stride)
661 } else {
662 self
663 }
664 }
665
666 pub fn stride_if_some(self, stride: Option<&'a [i32]>) -> VideoInfoBuilder<'a> {
667 if let Some(stride) = stride {
668 self.stride(stride)
669 } else {
670 self
671 }
672 }
673
674 pub fn multiview_mode(self, multiview_mode: crate::VideoMultiviewMode) -> Self {
675 Self {
676 multiview_mode: Some(multiview_mode),
677 ..self
678 }
679 }
680
681 pub fn multiview_mode_if(
682 self,
683 multiview_mode: crate::VideoMultiviewMode,
684 predicate: bool,
685 ) -> Self {
686 if predicate {
687 self.multiview_mode(multiview_mode)
688 } else {
689 self
690 }
691 }
692
693 pub fn multiview_mode_if_some(self, multiview_mode: Option<crate::VideoMultiviewMode>) -> Self {
694 if let Some(multiview_mode) = multiview_mode {
695 self.multiview_mode(multiview_mode)
696 } else {
697 self
698 }
699 }
700
701 pub fn multiview_flags(self, multiview_flags: crate::VideoMultiviewFlags) -> Self {
702 Self {
703 multiview_flags: Some(multiview_flags),
704 ..self
705 }
706 }
707
708 pub fn multiview_flags_if(
709 self,
710 multiview_flags: crate::VideoMultiviewFlags,
711 predicate: bool,
712 ) -> Self {
713 if predicate {
714 self.multiview_flags(multiview_flags)
715 } else {
716 self
717 }
718 }
719
720 pub fn multiview_flags_if_some(
721 self,
722 multiview_flags: Option<crate::VideoMultiviewFlags>,
723 ) -> Self {
724 if let Some(multiview_flags) = multiview_flags {
725 self.multiview_flags(multiview_flags)
726 } else {
727 self
728 }
729 }
730
731 pub fn field_order(self, field_order: crate::VideoFieldOrder) -> Self {
732 Self {
733 field_order: Some(field_order),
734 ..self
735 }
736 }
737
738 pub fn field_order_if(self, field_order: crate::VideoFieldOrder, predicate: bool) -> Self {
739 if predicate {
740 self.field_order(field_order)
741 } else {
742 self
743 }
744 }
745
746 pub fn field_order_if_some(self, field_order: Option<crate::VideoFieldOrder>) -> Self {
747 if let Some(field_order) = field_order {
748 self.field_order(field_order)
749 } else {
750 self
751 }
752 }
753}
754
755impl VideoInfo {
756 pub fn builder<'a>(
757 format: crate::VideoFormat,
758 width: u32,
759 height: u32,
760 ) -> VideoInfoBuilder<'a> {
761 assert_initialized_main_thread!();
762
763 VideoInfoBuilder {
764 format,
765 width,
766 height,
767 interlace_mode: None,
768 flags: None,
769 size: None,
770 views: None,
771 chroma_site: None,
772 colorimetry: None,
773 par: None,
774 fps: None,
775 offset: None,
776 stride: None,
777 multiview_mode: None,
778 multiview_flags: None,
779 field_order: None,
780 }
781 }
782
783 pub fn builder_from_info(info: &VideoInfo) -> VideoInfoBuilder<'_> {
784 assert_initialized_main_thread!();
785
786 VideoInfoBuilder {
787 format: info.format(),
788 width: info.width(),
789 height: info.height(),
790 interlace_mode: Some(info.interlace_mode()),
791 flags: Some(info.flags()),
792 size: Some(info.size()),
793 views: Some(info.views()),
794 chroma_site: Some(info.chroma_site()),
795 colorimetry: Some(info.colorimetry()),
796 par: Some(info.par()),
797 fps: Some(info.fps()),
798 offset: Some(info.offset()),
799 stride: Some(info.stride()),
800 multiview_mode: Some(info.multiview_mode()),
801 multiview_flags: Some(info.multiview_flags()),
802 field_order: Some(info.field_order()),
803 }
804 }
805
806 #[inline]
807 pub fn is_valid(&self) -> bool {
808 !self.0.finfo.is_null() && self.0.width > 0 && self.0.height > 0 && self.0.size > 0
809 }
810
811 #[doc(alias = "gst_video_info_from_caps")]
812 pub fn from_caps(caps: &gst::CapsRef) -> Result<Self, glib::error::BoolError> {
813 skip_assert_initialized!();
814
815 unsafe {
816 let mut info = mem::MaybeUninit::uninit();
817 if from_glib(ffi::gst_video_info_from_caps(
818 info.as_mut_ptr(),
819 caps.as_ptr(),
820 )) {
821 Ok(Self(info.assume_init()))
822 } else {
823 Err(glib::bool_error!("Failed to create VideoInfo from caps"))
824 }
825 }
826 }
827
828 #[doc(alias = "gst_video_info_to_caps")]
829 pub fn to_caps(&self) -> Result<gst::Caps, glib::error::BoolError> {
830 unsafe {
831 let result = from_glib_full(ffi::gst_video_info_to_caps(mut_override(&self.0)));
832 match result {
833 Some(c) => Ok(c),
834 None => Err(glib::bool_error!("Failed to create caps from VideoInfo")),
835 }
836 }
837 }
838
839 #[inline]
840 pub fn format(&self) -> crate::VideoFormat {
841 if self.0.finfo.is_null() {
842 return crate::VideoFormat::Unknown;
843 }
844
845 self.format_info().format()
846 }
847
848 #[inline]
849 pub fn format_info(&self) -> crate::VideoFormatInfo {
850 unsafe { crate::VideoFormatInfo::from_ptr(self.0.finfo) }
851 }
852
853 #[inline]
854 pub fn name<'a>(&self) -> &'a str {
855 self.format_info().name()
856 }
857
858 #[inline]
859 pub fn width(&self) -> u32 {
860 self.0.width as u32
861 }
862
863 #[inline]
864 pub fn height(&self) -> u32 {
865 self.0.height as u32
866 }
867
868 #[cfg(feature = "v1_16")]
869 #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
870 #[inline]
871 pub fn field_height(&self) -> u32 {
872 if self.0.interlace_mode == ffi::GST_VIDEO_INTERLACE_MODE_ALTERNATE {
873 (self.0.height as u32).div_ceil(2)
874 } else {
875 self.0.height as u32
876 }
877 }
878
879 #[inline]
880 pub fn interlace_mode(&self) -> crate::VideoInterlaceMode {
881 unsafe { from_glib(self.0.interlace_mode) }
882 }
883
884 #[inline]
885 pub fn flags(&self) -> crate::VideoFlags {
886 unsafe { from_glib(self.0.flags) }
887 }
888
889 #[inline]
890 pub fn size(&self) -> usize {
891 self.0.size
892 }
893
894 #[inline]
895 pub fn views(&self) -> u32 {
896 self.0.views as u32
897 }
898
899 #[inline]
900 pub fn chroma_site(&self) -> crate::VideoChromaSite {
901 unsafe { from_glib(self.0.chroma_site) }
902 }
903
904 #[inline]
905 pub fn colorimetry(&self) -> VideoColorimetry {
906 unsafe { VideoColorimetry(ptr::read(&self.0.colorimetry)) }
907 }
908
909 #[inline]
910 pub fn comp_depth(&self, component: u8) -> u32 {
911 self.format_info().depth()[component as usize]
912 }
913
914 #[inline]
915 pub fn comp_height(&self, component: u8) -> u32 {
916 self.format_info().scale_height(component, self.height())
917 }
918
919 #[inline]
920 pub fn comp_width(&self, component: u8) -> u32 {
921 self.format_info().scale_width(component, self.width())
922 }
923
924 #[inline]
925 pub fn comp_offset(&self, component: u8) -> usize {
926 self.offset()[self.format_info().plane()[component as usize] as usize]
927 + self.format_info().poffset()[component as usize] as usize
928 }
929
930 #[inline]
931 pub fn comp_plane(&self, component: u8) -> u32 {
932 self.format_info().plane()[component as usize]
933 }
934
935 #[inline]
936 pub fn comp_poffset(&self, component: u8) -> u32 {
937 self.format_info().poffset()[component as usize]
938 }
939
940 #[inline]
941 pub fn comp_pstride(&self, component: u8) -> i32 {
942 self.format_info().pixel_stride()[component as usize]
943 }
944
945 #[inline]
946 pub fn comp_stride(&self, component: u8) -> i32 {
947 self.stride()[self.format_info().plane()[component as usize] as usize]
948 }
949
950 #[inline]
951 pub fn par(&self) -> gst::Fraction {
952 gst::Fraction::new(self.0.par_n, self.0.par_d)
953 }
954
955 #[inline]
956 pub fn fps(&self) -> gst::Fraction {
957 gst::Fraction::new(self.0.fps_n, self.0.fps_d)
958 }
959
960 #[cfg(feature = "v1_16")]
961 #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
962 #[inline]
963 pub fn field_rate(&self) -> gst::Fraction {
964 if self.interlace_mode() == crate::VideoInterlaceMode::Alternate {
965 2 * self.fps()
966 } else {
967 self.fps()
968 }
969 }
970
971 #[inline]
972 pub fn offset(&self) -> &[usize] {
973 &self.0.offset[0..(self.format_info().n_planes() as usize)]
974 }
975
976 #[inline]
977 pub fn stride(&self) -> &[i32] {
978 &self.0.stride[0..(self.format_info().n_planes() as usize)]
979 }
980
981 #[inline]
982 pub fn multiview_mode(&self) -> crate::VideoMultiviewMode {
983 unsafe {
984 let ptr = &self.0.ABI._gst_reserved as *const _ as *const i32;
985 from_glib(ptr::read(ptr.offset(0)))
986 }
987 }
988
989 #[inline]
990 pub fn multiview_flags(&self) -> crate::VideoMultiviewFlags {
991 unsafe {
992 let ptr = &self.0.ABI._gst_reserved as *const _ as *const u32;
993 from_glib(ptr::read(ptr.offset(1)))
994 }
995 }
996
997 #[inline]
998 pub fn field_order(&self) -> crate::VideoFieldOrder {
999 unsafe {
1000 let ptr = &self.0.ABI._gst_reserved as *const _ as *const i32;
1001 from_glib(ptr::read(ptr.offset(2)))
1002 }
1003 }
1004
1005 #[inline]
1006 pub fn has_alpha(&self) -> bool {
1007 self.format_info().has_alpha()
1008 }
1009
1010 #[inline]
1011 pub fn is_gray(&self) -> bool {
1012 self.format_info().is_gray()
1013 }
1014
1015 #[inline]
1016 pub fn is_rgb(&self) -> bool {
1017 self.format_info().is_rgb()
1018 }
1019
1020 #[inline]
1021 pub fn is_yuv(&self) -> bool {
1022 self.format_info().is_yuv()
1023 }
1024
1025 #[inline]
1026 pub fn is_interlaced(&self) -> bool {
1027 self.interlace_mode() != crate::VideoInterlaceMode::Progressive
1028 }
1029
1030 #[inline]
1031 pub fn n_planes(&self) -> u32 {
1032 self.format_info().n_planes()
1033 }
1034
1035 #[inline]
1036 pub fn n_components(&self) -> u32 {
1037 self.format_info().n_components()
1038 }
1039
1040 #[doc(alias = "gst_video_info_convert")]
1041 pub fn convert<U: gst::format::SpecificFormattedValueFullRange>(
1042 &self,
1043 src_val: impl gst::format::FormattedValue,
1044 ) -> Option<U> {
1045 skip_assert_initialized!();
1046 unsafe {
1047 let mut dest_val = mem::MaybeUninit::uninit();
1048 if from_glib(ffi::gst_video_info_convert(
1049 mut_override(&self.0),
1050 src_val.format().into_glib(),
1051 src_val.into_raw_value(),
1052 U::default_format().into_glib(),
1053 dest_val.as_mut_ptr(),
1054 )) {
1055 Some(U::from_raw(U::default_format(), dest_val.assume_init()))
1056 } else {
1057 None
1058 }
1059 }
1060 }
1061
1062 pub fn convert_generic(
1063 &self,
1064 src_val: impl gst::format::FormattedValue,
1065 dest_fmt: gst::Format,
1066 ) -> Option<gst::GenericFormattedValue> {
1067 skip_assert_initialized!();
1068 unsafe {
1069 let mut dest_val = mem::MaybeUninit::uninit();
1070 if from_glib(ffi::gst_video_info_convert(
1071 mut_override(&self.0),
1072 src_val.format().into_glib(),
1073 src_val.into_raw_value(),
1074 dest_fmt.into_glib(),
1075 dest_val.as_mut_ptr(),
1076 )) {
1077 Some(gst::GenericFormattedValue::new(
1078 dest_fmt,
1079 dest_val.assume_init(),
1080 ))
1081 } else {
1082 None
1083 }
1084 }
1085 }
1086
1087 #[doc(alias = "gst_video_info_align")]
1088 pub fn align(&mut self, align: &mut crate::VideoAlignment) -> Result<(), glib::BoolError> {
1089 unsafe {
1090 glib::result_from_gboolean!(
1091 ffi::gst_video_info_align(&mut self.0, &mut align.0,),
1092 "Failed to align VideoInfo"
1093 )
1094 }
1095 }
1096
1097 #[cfg(feature = "v1_18")]
1098 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
1099 #[doc(alias = "gst_video_info_align_full")]
1100 pub fn align_full(
1101 &mut self,
1102 align: &mut crate::VideoAlignment,
1103 ) -> Result<[usize; crate::VIDEO_MAX_PLANES], glib::BoolError> {
1104 let mut plane_size = [0; crate::VIDEO_MAX_PLANES];
1105
1106 unsafe {
1107 glib::result_from_gboolean!(
1108 ffi::gst_video_info_align_full(&mut self.0, &mut align.0, plane_size.as_mut_ptr()),
1109 "Failed to align VideoInfo"
1110 )?;
1111 }
1112
1113 Ok(plane_size)
1114 }
1115
1116 #[doc(alias = "gst_video_color_range_offsets")]
1117 #[inline]
1118 pub fn range_offsets(&self, range: crate::VideoColorRange) -> ([i32; 4], [i32; 4]) {
1119 self.format_info().range_offsets(range)
1120 }
1121}
1122
1123impl PartialEq for VideoInfo {
1124 #[doc(alias = "gst_video_info_is_equal")]
1125 fn eq(&self, other: &Self) -> bool {
1126 unsafe { from_glib(ffi::gst_video_info_is_equal(&self.0, &other.0)) }
1127 }
1128}
1129
1130impl Eq for VideoInfo {}
1131
1132unsafe impl Send for VideoInfo {}
1133unsafe impl Sync for VideoInfo {}
1134
1135impl glib::types::StaticType for VideoInfo {
1136 #[inline]
1137 fn static_type() -> glib::types::Type {
1138 unsafe { glib::translate::from_glib(ffi::gst_video_info_get_type()) }
1139 }
1140}
1141
1142impl glib::value::ValueType for VideoInfo {
1143 type Type = Self;
1144}
1145
1146#[doc(hidden)]
1147unsafe impl<'a> glib::value::FromValue<'a> for VideoInfo {
1148 type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;
1149
1150 unsafe fn from_value(value: &'a glib::Value) -> Self {
1151 skip_assert_initialized!();
1152 from_glib_none(
1153 glib::gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *mut ffi::GstVideoInfo
1154 )
1155 }
1156}
1157
1158#[doc(hidden)]
1159impl glib::value::ToValue for VideoInfo {
1160 fn to_value(&self) -> glib::Value {
1161 let mut value = glib::Value::for_value_type::<Self>();
1162 unsafe {
1163 glib::gobject_ffi::g_value_set_boxed(
1164 value.to_glib_none_mut().0,
1165 self.to_glib_none().0 as *mut _,
1166 )
1167 }
1168 value
1169 }
1170
1171 fn value_type(&self) -> glib::Type {
1172 Self::static_type()
1173 }
1174}
1175
1176#[doc(hidden)]
1177impl glib::value::ToValueOptional for VideoInfo {
1178 fn to_value_optional(s: Option<&Self>) -> glib::Value {
1179 skip_assert_initialized!();
1180 let mut value = glib::Value::for_value_type::<Self>();
1181 unsafe {
1182 glib::gobject_ffi::g_value_set_boxed(
1183 value.to_glib_none_mut().0,
1184 s.to_glib_none().0 as *mut _,
1185 )
1186 }
1187 value
1188 }
1189}
1190
1191#[doc(hidden)]
1192impl From<VideoInfo> for glib::Value {
1193 fn from(v: VideoInfo) -> glib::Value {
1194 skip_assert_initialized!();
1195 glib::value::ToValue::to_value(&v)
1196 }
1197}
1198
1199#[doc(hidden)]
1200impl glib::translate::Uninitialized for VideoInfo {
1201 #[inline]
1202 unsafe fn uninitialized() -> Self {
1203 mem::zeroed()
1204 }
1205}
1206
1207#[doc(hidden)]
1208impl glib::translate::GlibPtrDefault for VideoInfo {
1209 type GlibType = *mut ffi::GstVideoInfo;
1210}
1211
1212#[doc(hidden)]
1213impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstVideoInfo> for VideoInfo {
1214 type Storage = PhantomData<&'a Self>;
1215
1216 #[inline]
1217 fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstVideoInfo, Self> {
1218 glib::translate::Stash(&self.0, PhantomData)
1219 }
1220
1221 fn to_glib_full(&self) -> *const ffi::GstVideoInfo {
1222 unimplemented!()
1223 }
1224}
1225
1226#[doc(hidden)]
1227impl glib::translate::FromGlibPtrNone<*const ffi::GstVideoInfo> for VideoInfo {
1228 #[inline]
1229 unsafe fn from_glib_none(ptr: *const ffi::GstVideoInfo) -> Self {
1230 Self(ptr::read(ptr))
1231 }
1232}
1233
1234#[doc(hidden)]
1235impl glib::translate::FromGlibPtrNone<*mut ffi::GstVideoInfo> for VideoInfo {
1236 #[inline]
1237 unsafe fn from_glib_none(ptr: *mut ffi::GstVideoInfo) -> Self {
1238 Self(ptr::read(ptr))
1239 }
1240}
1241
1242#[doc(hidden)]
1243impl glib::translate::FromGlibPtrFull<*mut ffi::GstVideoInfo> for VideoInfo {
1244 #[inline]
1245 unsafe fn from_glib_full(ptr: *mut ffi::GstVideoInfo) -> Self {
1246 let info = from_glib_none(ptr);
1247 glib::ffi::g_free(ptr as *mut _);
1248 info
1249 }
1250}
1251
1252impl crate::VideoFieldOrder {
1253 #[doc(alias = "gst_video_field_order_to_string")]
1254 pub fn to_str<'a>(self) -> &'a str {
1255 use std::ffi::CStr;
1256
1257 if self == Self::Unknown {
1258 return "UNKNOWN";
1259 }
1260 unsafe {
1261 CStr::from_ptr(
1262 ffi::gst_video_field_order_to_string(self.into_glib())
1263 .as_ref()
1264 .expect("gst_video_field_order_to_string returned NULL"),
1265 )
1266 .to_str()
1267 .expect("gst_video_field_order_to_string returned an invalid string")
1268 }
1269 }
1270}
1271
1272impl str::FromStr for crate::VideoFieldOrder {
1273 type Err = glib::error::BoolError;
1274
1275 fn from_str(s: &str) -> Result<Self, Self::Err> {
1276 skip_assert_initialized!();
1277
1278 let fmt = Self::from_string(s);
1279 if fmt == Self::Unknown {
1280 Err(glib::bool_error!(
1281 "Failed to parse video field order from string"
1282 ))
1283 } else {
1284 Ok(fmt)
1285 }
1286 }
1287}
1288
1289impl str::FromStr for crate::VideoInterlaceMode {
1290 type Err = glib::error::BoolError;
1291
1292 fn from_str(s: &str) -> Result<Self, Self::Err> {
1293 skip_assert_initialized!();
1294
1295 let fmt = Self::from_string(s);
1296 Ok(fmt)
1297 }
1298}
1299
1300#[cfg(test)]
1301mod tests {
1302 use super::*;
1303
1304 #[test]
1305 fn test_new() {
1306 gst::init().unwrap();
1307
1308 let info = VideoInfo::builder(crate::VideoFormat::I420, 320, 240)
1309 .build()
1310 .unwrap();
1311 assert_eq!(info.format(), crate::VideoFormat::I420);
1312 assert_eq!(info.width(), 320);
1313 assert_eq!(info.height(), 240);
1314 assert_eq!(info.size(), 320 * 240 + 2 * 160 * 120);
1315 assert_eq!(info.multiview_mode(), crate::VideoMultiviewMode::None);
1316 assert_eq!(&info.offset(), &[0, 320 * 240, 320 * 240 + 160 * 120]);
1317 assert_eq!(&info.stride(), &[320, 160, 160]);
1318
1319 let offsets = [0, 640 * 240 + 16, 640 * 240 + 16 + 320 * 120 + 16];
1320 let strides = [640, 320, 320];
1321 let info = VideoInfo::builder(crate::VideoFormat::I420, 320, 240)
1322 .offset(&offsets)
1323 .stride(&strides)
1324 .size(640 * 240 + 16 + 320 * 120 + 16 + 320 * 120 + 16)
1325 .multiview_mode(crate::VideoMultiviewMode::SideBySide)
1326 .build()
1327 .unwrap();
1328 assert_eq!(info.format(), crate::VideoFormat::I420);
1329 assert_eq!(info.width(), 320);
1330 assert_eq!(info.height(), 240);
1331 assert_eq!(
1332 info.size(),
1333 640 * 240 + 16 + 320 * 120 + 16 + 320 * 120 + 16
1334 );
1335 assert_eq!(info.multiview_mode(), crate::VideoMultiviewMode::SideBySide);
1336 assert_eq!(
1337 &info.offset(),
1338 &[0, 640 * 240 + 16, 640 * 240 + 16 + 320 * 120 + 16]
1339 );
1340 assert_eq!(&info.stride(), &[640, 320, 320]);
1341 }
1342
1343 #[test]
1344 fn test_from_to_caps() {
1345 gst::init().unwrap();
1346
1347 let caps = crate::VideoCapsBuilder::new()
1348 .format(crate::VideoFormat::I420)
1349 .width(320)
1350 .height(240)
1351 .framerate((30, 1).into())
1352 .pixel_aspect_ratio((1, 1).into())
1353 .field("interlace-mode", "progressive")
1354 .field("chroma-site", "mpeg2")
1355 .field("colorimetry", "bt709")
1356 .build();
1357 let info = VideoInfo::from_caps(&caps).unwrap();
1358 assert_eq!(info.format(), crate::VideoFormat::I420);
1359 assert_eq!(info.width(), 320);
1360 assert_eq!(info.height(), 240);
1361 assert_eq!(info.fps(), gst::Fraction::new(30, 1));
1362 assert_eq!(
1363 info.interlace_mode(),
1364 crate::VideoInterlaceMode::Progressive
1365 );
1366 assert_eq!(info.chroma_site(), crate::VideoChromaSite::MPEG2);
1367 assert_eq!(info.colorimetry(), "bt709".parse().unwrap());
1368
1369 let caps2 = info.to_caps().unwrap();
1370 assert_eq!(caps, caps2);
1371
1372 let info2 = VideoInfo::from_caps(&caps2).unwrap();
1373 assert!(info == info2);
1374 }
1375
1376 #[test]
1377 fn test_video_align() {
1378 gst::init().unwrap();
1379
1380 let mut info = crate::VideoInfo::builder(crate::VideoFormat::Nv16, 1920, 1080)
1381 .build()
1382 .expect("Failed to create VideoInfo");
1383
1384 assert_eq!(info.stride(), [1920, 1920]);
1385 assert_eq!(info.offset(), [0, 2_073_600]);
1386
1387 let mut align = crate::VideoAlignment::new(0, 0, 0, 8, &[0; VIDEO_MAX_PLANES]);
1388 info.align(&mut align).unwrap();
1389
1390 assert_eq!(info.stride(), [1928, 1928]);
1391 assert_eq!(info.offset(), [0, 2_082_240]);
1392
1393 #[cfg(feature = "v1_18")]
1394 {
1395 let mut info = crate::VideoInfo::builder(crate::VideoFormat::Nv16, 1920, 1080)
1396 .build()
1397 .expect("Failed to create VideoInfo");
1398
1399 let mut align = crate::VideoAlignment::new(0, 0, 0, 8, &[0; VIDEO_MAX_PLANES]);
1400 let plane_size = info.align_full(&mut align).unwrap();
1401 assert_eq!(plane_size, [2082240, 2082240, 0, 0]);
1402 }
1403 }
1404
1405 #[test]
1406 fn test_display() {
1407 gst::init().unwrap();
1408
1409 let _ = format!("{}", "sRGB".parse::<crate::VideoColorimetry>().unwrap());
1410 let _ = format!("{}", crate::VideoFieldOrder::TopFieldFirst);
1411 let _ = format!("{}", crate::VideoInterlaceMode::Progressive);
1412 }
1413}