ffmpeg_next_crossfix/util/frame/
mod.rs

1pub mod side_data;
2pub use self::side_data::SideData;
3
4pub mod video;
5pub use self::video::Video;
6
7pub mod audio;
8pub use self::audio::Audio;
9
10pub mod flag;
11pub use self::flag::Flags;
12
13use ffi::*;
14use libc::c_int;
15use {Dictionary, DictionaryRef};
16
17#[derive(PartialEq, Eq, Copy, Clone, Debug)]
18pub struct Packet {
19    pub duration: i64,
20    pub position: i64,
21    pub size: usize,
22
23    pub pts: i64,
24    pub dts: i64,
25}
26
27#[derive(PartialEq, Eq)]
28pub struct Frame {
29    ptr: *mut AVFrame,
30
31    _own: bool,
32}
33
34unsafe impl Send for Frame {}
35unsafe impl Sync for Frame {}
36
37impl Frame {
38    #[inline(always)]
39    pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
40        Frame { ptr, _own: false }
41    }
42
43    #[inline(always)]
44    pub unsafe fn empty() -> Self {
45        Frame {
46            ptr: av_frame_alloc(),
47            _own: true,
48        }
49    }
50
51    #[inline(always)]
52    pub unsafe fn as_ptr(&self) -> *const AVFrame {
53        self.ptr as *const _
54    }
55
56    #[inline(always)]
57    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFrame {
58        self.ptr
59    }
60
61    #[inline(always)]
62    pub unsafe fn is_empty(&self) -> bool {
63        (*self.as_ptr()).data[0].is_null()
64    }
65}
66
67impl Frame {
68    #[inline]
69    pub fn is_key(&self) -> bool {
70        unsafe { (*self.as_ptr()).key_frame == 1 }
71    }
72
73    #[inline]
74    pub fn is_corrupt(&self) -> bool {
75        self.flags().contains(Flags::CORRUPT)
76    }
77
78    #[inline]
79    pub fn packet(&self) -> Packet {
80        unsafe {
81            Packet {
82                duration: av_frame_get_pkt_duration(self.as_ptr()) as i64,
83                position: av_frame_get_pkt_pos(self.as_ptr()) as i64,
84                size: av_frame_get_pkt_size(self.as_ptr()) as usize,
85
86                pts: (*self.as_ptr()).pkt_pts,
87                dts: (*self.as_ptr()).pkt_dts,
88            }
89        }
90    }
91
92    #[inline]
93    pub fn pts(&self) -> Option<i64> {
94        unsafe {
95            match (*self.as_ptr()).pts {
96                AV_NOPTS_VALUE => None,
97                pts => Some(pts as i64),
98            }
99        }
100    }
101
102    #[inline]
103    pub fn set_pts(&mut self, value: Option<i64>) {
104        unsafe {
105            (*self.as_mut_ptr()).pts = value.unwrap_or(AV_NOPTS_VALUE);
106        }
107    }
108
109    #[inline]
110    pub fn timestamp(&self) -> Option<i64> {
111        unsafe {
112            match av_frame_get_best_effort_timestamp(self.as_ptr()) {
113                AV_NOPTS_VALUE => None,
114                t => Some(t as i64),
115            }
116        }
117    }
118
119    #[inline]
120    pub fn quality(&self) -> usize {
121        unsafe { (*self.as_ptr()).quality as usize }
122    }
123
124    #[inline]
125    pub fn flags(&self) -> Flags {
126        unsafe { Flags::from_bits_truncate((*self.as_ptr()).flags) }
127    }
128
129    #[inline]
130    pub fn metadata(&self) -> DictionaryRef {
131        unsafe { DictionaryRef::wrap(av_frame_get_metadata(self.as_ptr())) }
132    }
133
134    #[inline]
135    pub fn set_metadata(&mut self, value: Dictionary) {
136        unsafe {
137            av_frame_set_metadata(self.as_mut_ptr(), value.disown());
138        }
139    }
140
141    #[inline]
142    pub fn side_data(&self, kind: side_data::Type) -> Option<SideData> {
143        unsafe {
144            let ptr = av_frame_get_side_data(self.as_ptr(), kind.into());
145
146            if ptr.is_null() {
147                None
148            } else {
149                Some(SideData::wrap(ptr))
150            }
151        }
152    }
153
154    #[inline]
155    pub fn new_side_data(&mut self, kind: side_data::Type, size: usize) -> Option<SideData> {
156        unsafe {
157            let ptr = av_frame_new_side_data(self.as_mut_ptr(), kind.into(), size as c_int);
158
159            if ptr.is_null() {
160                None
161            } else {
162                Some(SideData::wrap(ptr))
163            }
164        }
165    }
166
167    #[inline]
168    pub fn remove_side_data(&mut self, kind: side_data::Type) {
169        unsafe {
170            av_frame_remove_side_data(self.as_mut_ptr(), kind.into());
171        }
172    }
173}
174
175impl Drop for Frame {
176    #[inline]
177    fn drop(&mut self) {
178        unsafe {
179            av_frame_free(&mut self.as_mut_ptr());
180        }
181    }
182}