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