ffmpeg_rs/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 {Dictionary, DictionaryRef};
15
16#[derive(PartialEq, Eq, Copy, Clone, Debug)]
17pub struct Packet {
18    pub duration: i64,
19    pub position: i64,
20    pub size: usize,
21
22    #[cfg(not(feature = "ffmpeg_5_0"))]
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: (*self.as_ptr()).pkt_duration as i64,
83                position: (*self.as_ptr()).pkt_pos as i64,
84                size: (*self.as_ptr()).pkt_size as usize,
85
86                #[cfg(not(feature = "ffmpeg_5_0"))]
87                pts: (*self.as_ptr()).pkt_pts,
88                dts: (*self.as_ptr()).pkt_dts,
89            }
90        }
91    }
92
93    #[inline]
94    pub fn pts(&self) -> Option<i64> {
95        unsafe {
96            match (*self.as_ptr()).pts {
97                AV_NOPTS_VALUE => None,
98                pts => Some(pts as i64),
99            }
100        }
101    }
102
103    #[inline]
104    pub fn set_pts(&mut self, value: Option<i64>) {
105        unsafe {
106            (*self.as_mut_ptr()).pts = value.unwrap_or(AV_NOPTS_VALUE);
107        }
108    }
109
110    #[inline]
111    pub fn timestamp(&self) -> Option<i64> {
112        unsafe {
113            match (*self.as_ptr()).best_effort_timestamp {
114                AV_NOPTS_VALUE => None,
115                t => Some(t as i64),
116            }
117        }
118    }
119
120    #[inline]
121    pub fn quality(&self) -> usize {
122        unsafe { (*self.as_ptr()).quality as usize }
123    }
124
125    #[inline]
126    pub fn flags(&self) -> Flags {
127        unsafe { Flags::from_bits_truncate((*self.as_ptr()).flags) }
128    }
129
130    #[inline]
131    pub fn metadata(&self) -> DictionaryRef {
132        unsafe { DictionaryRef::wrap((*self.as_ptr()).metadata) }
133    }
134
135    #[inline]
136    pub fn set_metadata(&mut self, value: Dictionary) {
137        unsafe { (*self.as_mut_ptr()).metadata = value.disown() }
138    }
139
140    #[inline]
141    pub fn side_data(&self, kind: side_data::Type) -> Option<SideData> {
142        unsafe {
143            let ptr = av_frame_get_side_data(self.as_ptr(), kind.into());
144
145            if ptr.is_null() {
146                None
147            } else {
148                Some(SideData::wrap(ptr))
149            }
150        }
151    }
152
153    #[inline]
154    pub fn new_side_data(&mut self, kind: side_data::Type, size: usize) -> Option<SideData> {
155        unsafe {
156            let ptr = av_frame_new_side_data(self.as_mut_ptr(), kind.into(), size as _);
157
158            if ptr.is_null() {
159                None
160            } else {
161                Some(SideData::wrap(ptr))
162            }
163        }
164    }
165
166    #[inline]
167    pub fn remove_side_data(&mut self, kind: side_data::Type) {
168        unsafe {
169            av_frame_remove_side_data(self.as_mut_ptr(), kind.into());
170        }
171    }
172}
173
174impl Drop for Frame {
175    #[inline]
176    fn drop(&mut self) {
177        unsafe {
178            av_frame_free(&mut self.as_mut_ptr());
179        }
180    }
181}