playa_ffmpeg/codec/
video.rs

1use std::ops::Deref;
2
3use super::codec::Codec;
4use crate::{Rational, ffi::*, format};
5
6#[derive(PartialEq, Eq, Copy, Clone)]
7pub struct Video {
8    codec: Codec,
9}
10
11impl Video {
12    pub unsafe fn new(codec: Codec) -> Video {
13        Video { codec }
14    }
15}
16
17impl Video {
18    pub fn rates(&self) -> Option<RateIter> {
19        unsafe { if (*self.codec.as_ptr()).supported_framerates.is_null() { None } else { Some(RateIter::new((*self.codec.as_ptr()).supported_framerates)) } }
20    }
21
22    pub fn formats(&self) -> Option<FormatIter> {
23        unsafe { if (*self.codec.as_ptr()).pix_fmts.is_null() { None } else { Some(FormatIter::new((*self.codec.as_ptr()).pix_fmts)) } }
24    }
25}
26
27impl Deref for Video {
28    type Target = Codec;
29
30    fn deref(&self) -> &Self::Target {
31        &self.codec
32    }
33}
34
35pub struct RateIter {
36    ptr: *const AVRational,
37}
38
39impl RateIter {
40    pub fn new(ptr: *const AVRational) -> Self {
41        RateIter { ptr }
42    }
43}
44
45impl Iterator for RateIter {
46    type Item = Rational;
47
48    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
49        unsafe {
50            if (*self.ptr).num == 0 && (*self.ptr).den == 0 {
51                return None;
52            }
53
54            let rate = (*self.ptr).into();
55            self.ptr = self.ptr.offset(1);
56
57            Some(rate)
58        }
59    }
60}
61
62pub struct FormatIter {
63    ptr: *const AVPixelFormat,
64}
65
66impl FormatIter {
67    pub fn new(ptr: *const AVPixelFormat) -> Self {
68        FormatIter { ptr }
69    }
70}
71
72impl Iterator for FormatIter {
73    type Item = format::Pixel;
74
75    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
76        unsafe {
77            if *self.ptr == AVPixelFormat::AV_PIX_FMT_NONE {
78                return None;
79            }
80
81            let format = (*self.ptr).into();
82            self.ptr = self.ptr.offset(1);
83
84            Some(format)
85        }
86    }
87}