ffmpeg_the_third/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 crate::ffi::*;
14use crate::{Dictionary, DictionaryRef};
15
16#[derive(PartialEq, Eq)]
17pub struct Frame {
18    ptr: *mut AVFrame,
19
20    _own: bool,
21}
22
23unsafe impl Send for Frame {}
24unsafe impl Sync for Frame {}
25
26impl Frame {
27    #[inline(always)]
28    pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
29        Frame { ptr, _own: false }
30    }
31
32    #[inline(always)]
33    pub unsafe fn empty() -> Self {
34        Frame {
35            ptr: av_frame_alloc(),
36            _own: true,
37        }
38    }
39
40    #[inline(always)]
41    pub unsafe fn as_ptr(&self) -> *const AVFrame {
42        self.ptr as *const _
43    }
44
45    #[inline(always)]
46    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFrame {
47        self.ptr
48    }
49
50    #[inline(always)]
51    pub unsafe fn as_ref(&self) -> Option<&AVFrame> {
52        self.ptr.as_ref()
53    }
54
55    #[inline(always)]
56    pub unsafe fn as_mut(&mut self) -> Option<&mut AVFrame> {
57        self.ptr.as_mut()
58    }
59
60    #[inline(always)]
61    pub unsafe fn is_empty(&self) -> bool {
62        (*self.as_ptr()).data[0].is_null()
63    }
64}
65
66impl Frame {
67    #[cfg(not(feature = "ffmpeg_8_0"))]
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 pts(&self) -> Option<i64> {
80        unsafe {
81            match (*self.as_ptr()).pts {
82                AV_NOPTS_VALUE => None,
83                pts => Some(pts as i64),
84            }
85        }
86    }
87
88    #[inline]
89    pub fn set_pts(&mut self, value: Option<i64>) {
90        unsafe {
91            (*self.as_mut_ptr()).pts = value.unwrap_or(AV_NOPTS_VALUE);
92        }
93    }
94
95    #[inline]
96    pub fn timestamp(&self) -> Option<i64> {
97        unsafe {
98            match (*self.as_ptr()).best_effort_timestamp {
99                AV_NOPTS_VALUE => None,
100                t => Some(t as i64),
101            }
102        }
103    }
104
105    #[inline]
106    pub fn quality(&self) -> usize {
107        unsafe { (*self.as_ptr()).quality as usize }
108    }
109
110    #[inline]
111    pub fn flags(&self) -> Flags {
112        unsafe { Flags::from_bits_truncate((*self.as_ptr()).flags) }
113    }
114
115    #[inline]
116    pub fn metadata(&self) -> DictionaryRef<'_> {
117        unsafe { DictionaryRef::wrap((*self.as_ptr()).metadata) }
118    }
119
120    #[inline]
121    pub fn set_metadata(&mut self, value: Dictionary) {
122        unsafe { (*self.as_mut_ptr()).metadata = value.disown() }
123    }
124
125    #[inline]
126    pub fn side_data(&self, kind: side_data::Type) -> Option<SideData<'_>> {
127        unsafe {
128            let ptr = av_frame_get_side_data(self.as_ptr(), kind.into());
129
130            if ptr.is_null() {
131                None
132            } else {
133                Some(SideData::wrap(ptr))
134            }
135        }
136    }
137
138    #[inline]
139    pub fn new_side_data(&mut self, kind: side_data::Type, size: usize) -> Option<SideData<'_>> {
140        unsafe {
141            let ptr = av_frame_new_side_data(self.as_mut_ptr(), kind.into(), size as _);
142
143            if ptr.is_null() {
144                None
145            } else {
146                Some(SideData::wrap(ptr))
147            }
148        }
149    }
150
151    #[inline]
152    pub fn remove_side_data(&mut self, kind: side_data::Type) {
153        unsafe {
154            av_frame_remove_side_data(self.as_mut_ptr(), kind.into());
155        }
156    }
157}
158
159impl Drop for Frame {
160    #[inline]
161    fn drop(&mut self) {
162        unsafe {
163            av_frame_free(&mut self.as_mut_ptr());
164        }
165    }
166}