Skip to main content

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::*;
14#[cfg(feature = "ffmpeg_8_1")]
15use crate::format::AlphaMode;
16use crate::{DictionaryMut, DictionaryRef};
17
18#[derive(PartialEq, Eq)]
19pub struct Frame {
20    ptr: *mut AVFrame,
21
22    _own: bool,
23}
24
25unsafe impl Send for Frame {}
26unsafe impl Sync for Frame {}
27
28impl Frame {
29    #[inline(always)]
30    pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
31        Frame { ptr, _own: false }
32    }
33
34    #[inline(always)]
35    pub unsafe fn empty() -> Self {
36        Frame {
37            ptr: av_frame_alloc(),
38            _own: true,
39        }
40    }
41
42    #[inline(always)]
43    pub unsafe fn as_ptr(&self) -> *const AVFrame {
44        self.ptr as *const _
45    }
46
47    #[inline(always)]
48    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFrame {
49        self.ptr
50    }
51
52    #[inline(always)]
53    pub unsafe fn as_ref(&self) -> Option<&AVFrame> {
54        self.ptr.as_ref()
55    }
56
57    #[inline(always)]
58    pub unsafe fn as_mut(&mut self) -> Option<&mut AVFrame> {
59        self.ptr.as_mut()
60    }
61
62    #[inline(always)]
63    pub unsafe fn is_empty(&self) -> bool {
64        (*self.as_ptr()).data[0].is_null()
65    }
66}
67
68impl Frame {
69    #[cfg(not(feature = "ffmpeg_8_0"))]
70    #[inline]
71    pub fn is_key(&self) -> bool {
72        unsafe { (*self.as_ptr()).key_frame == 1 }
73    }
74
75    #[inline]
76    pub fn is_corrupt(&self) -> bool {
77        self.flags().contains(Flags::CORRUPT)
78    }
79
80    #[inline]
81    pub fn pts(&self) -> Option<i64> {
82        unsafe {
83            match (*self.as_ptr()).pts {
84                AV_NOPTS_VALUE => None,
85                pts => Some(pts as i64),
86            }
87        }
88    }
89
90    #[inline]
91    pub fn set_pts(&mut self, value: Option<i64>) {
92        unsafe {
93            (*self.as_mut_ptr()).pts = value.unwrap_or(AV_NOPTS_VALUE);
94        }
95    }
96
97    #[inline]
98    pub fn timestamp(&self) -> Option<i64> {
99        unsafe {
100            match (*self.as_ptr()).best_effort_timestamp {
101                AV_NOPTS_VALUE => None,
102                t => Some(t as i64),
103            }
104        }
105    }
106
107    #[inline]
108    pub fn quality(&self) -> usize {
109        unsafe { (*self.as_ptr()).quality as usize }
110    }
111
112    #[inline]
113    pub fn flags(&self) -> Flags {
114        unsafe { Flags::from_bits_truncate((*self.as_ptr()).flags) }
115    }
116
117    pub fn metadata(&self) -> DictionaryRef<'_> {
118        unsafe { DictionaryRef::from_raw((*self.as_ptr()).metadata) }
119    }
120
121    pub fn metadata_mut(&mut self) -> DictionaryMut<'_> {
122        unsafe { DictionaryMut::from_raw(&mut (*self.as_mut_ptr()).metadata) }
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    #[cfg(feature = "ffmpeg_8_1")]
159    pub fn alpha_mode(&self) -> AlphaMode {
160        unsafe { (*self.as_ptr()).alpha_mode.into() }
161    }
162
163    #[cfg(feature = "ffmpeg_8_1")]
164    pub fn set_alpha_mode(&mut self, mode: AlphaMode) {
165        unsafe { (*self.as_mut_ptr()).alpha_mode = mode.into() }
166    }
167}
168
169impl Drop for Frame {
170    #[inline]
171    fn drop(&mut self) {
172        unsafe {
173            av_frame_free(&mut self.as_mut_ptr());
174        }
175    }
176}