scuffle_ffmpeg/
frame.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use ffmpeg_sys_next::*;

use crate::error::FfmpegError;
use crate::smart_object::SmartPtr;
use crate::utils::check_i64;

pub struct Frame(SmartPtr<AVFrame>);

impl Clone for Frame {
	fn clone(&self) -> Self {
		unsafe { Self::wrap(av_frame_clone(self.0.as_ptr())).expect("failed to clone frame") }
	}
}

/// Safety: `Frame` is safe to send between threads.
unsafe impl Send for Frame {}

/// Safety: `Frame` is safe to share between threads.
unsafe impl Sync for Frame {}

#[derive(Clone)]
pub struct VideoFrame(pub Frame);

#[derive(Clone)]
pub struct AudioFrame(pub Frame);

impl Frame {
	pub fn new() -> Result<Self, FfmpegError> {
		// Safety: the pointer returned from av_frame_alloc is valid
		unsafe { Self::wrap(av_frame_alloc()) }
	}

	/// Safety: `ptr` must be a valid pointer to an `AVFrame`.
	unsafe fn wrap(ptr: *mut AVFrame) -> Result<Self, FfmpegError> {
		Ok(Self(
			// The caller guarantees that `ptr` is valid.
			SmartPtr::wrap_non_null(ptr, |ptr| av_frame_free(ptr)).ok_or(FfmpegError::Alloc)?,
		))
	}

	pub fn as_ptr(&self) -> *const AVFrame {
		self.0.as_ptr()
	}

	pub fn as_mut_ptr(&mut self) -> *mut AVFrame {
		self.0.as_mut_ptr()
	}

	pub fn video(self) -> VideoFrame {
		VideoFrame(self)
	}

	pub fn audio(self) -> AudioFrame {
		AudioFrame(self)
	}

	pub fn pts(&self) -> Option<i64> {
		check_i64(self.0.as_deref_except().pts)
	}

	pub fn set_pts(&mut self, pts: Option<i64>) {
		self.0.as_deref_mut_except().pts = pts.unwrap_or(AV_NOPTS_VALUE);
		self.0.as_deref_mut_except().best_effort_timestamp = pts.unwrap_or(AV_NOPTS_VALUE);
	}

	pub fn duration(&self) -> Option<i64> {
		check_i64(self.0.as_deref_except().duration).or_else(|| check_i64(self.0.as_deref_except().pkt_duration))
	}

	pub fn set_duration(&mut self, duration: Option<i64>) {
		self.0.as_deref_mut_except().duration = duration.unwrap_or(AV_NOPTS_VALUE);
		self.0.as_deref_mut_except().pkt_duration = duration.unwrap_or(AV_NOPTS_VALUE);
	}

	pub fn best_effort_timestamp(&self) -> Option<i64> {
		check_i64(self.0.as_deref_except().best_effort_timestamp)
	}

	pub fn dts(&self) -> Option<i64> {
		check_i64(self.0.as_deref_except().pkt_dts)
	}

	pub fn set_dts(&mut self, dts: Option<i64>) {
		self.0.as_deref_mut_except().pkt_dts = dts.unwrap_or(AV_NOPTS_VALUE);
	}

	pub fn time_base(&self) -> AVRational {
		self.0.as_deref_except().time_base
	}

	pub fn set_time_base(&mut self, time_base: AVRational) {
		self.0.as_deref_mut_except().time_base = time_base;
	}

	pub fn format(&self) -> i32 {
		self.0.as_deref_except().format
	}

	pub fn set_format(&mut self, format: i32) {
		self.0.as_deref_mut_except().format = format;
	}

	pub fn is_audio(&self) -> bool {
		self.0.as_deref_except().channel_layout != 0
	}

	pub fn is_video(&self) -> bool {
		self.0.as_deref_except().width != 0
	}

	pub fn linesize(&self, index: usize) -> Option<i32> {
		self.0.as_deref_except().linesize.get(index).copied()
	}
}

impl std::fmt::Debug for Frame {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Frame")
			.field("pts", &self.pts())
			.field("dts", &self.dts())
			.field("duration", &self.duration())
			.field("best_effort_timestamp", &self.best_effort_timestamp())
			.field("time_base", &self.time_base())
			.field("format", &self.format())
			.field("is_audio", &self.is_audio())
			.field("is_video", &self.is_video())
			.finish()
	}
}

impl VideoFrame {
	pub fn width(&self) -> usize {
		self.0 .0.as_deref_except().width as usize
	}

	pub fn height(&self) -> usize {
		self.0 .0.as_deref_except().height as usize
	}

	pub fn sample_aspect_ratio(&self) -> AVRational {
		self.0 .0.as_deref_except().sample_aspect_ratio
	}

	pub fn set_sample_aspect_ratio(&mut self, sample_aspect_ratio: AVRational) {
		self.0 .0.as_deref_mut_except().sample_aspect_ratio = sample_aspect_ratio;
	}

	pub fn set_width(&mut self, width: usize) {
		self.0 .0.as_deref_mut_except().width = width as i32;
	}

	pub fn set_height(&mut self, height: usize) {
		self.0 .0.as_deref_mut_except().height = height as i32;
	}

	pub fn is_keyframe(&self) -> bool {
		self.0 .0.as_deref_except().key_frame != 0
	}

	pub fn pict_type(&self) -> AVPictureType {
		self.0 .0.as_deref_except().pict_type
	}

	pub fn set_pict_type(&mut self, pict_type: AVPictureType) {
		self.0 .0.as_deref_mut_except().pict_type = pict_type;
	}

	pub fn data(&self, index: usize) -> Option<&[u8]> {
		unsafe {
			self.0
				 .0
				.as_deref_except()
				.data
				.get(index)
				.map(|ptr| std::slice::from_raw_parts(*ptr, self.linesize(index).unwrap() as usize * self.height()))
		}
	}
}

impl std::fmt::Debug for VideoFrame {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("VideoFrame")
			.field("width", &self.width())
			.field("height", &self.height())
			.field("sample_aspect_ratio", &self.sample_aspect_ratio())
			.field("pts", &self.pts())
			.field("dts", &self.dts())
			.field("duration", &self.duration())
			.field("best_effort_timestamp", &self.best_effort_timestamp())
			.field("time_base", &self.time_base())
			.field("format", &self.format())
			.field("is_audio", &self.is_audio())
			.field("is_video", &self.is_video())
			.field("is_keyframe", &self.is_keyframe())
			.finish()
	}
}

impl std::ops::Deref for VideoFrame {
	type Target = Frame;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl std::ops::DerefMut for VideoFrame {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.0
	}
}

impl AudioFrame {
	pub fn nb_samples(&self) -> i32 {
		self.0 .0.as_deref_except().nb_samples
	}

	pub fn set_nb_samples(&mut self, nb_samples: usize) {
		self.0 .0.as_deref_mut_except().nb_samples = nb_samples as i32;
	}

	pub fn sample_rate(&self) -> i32 {
		self.0 .0.as_deref_except().sample_rate
	}

	pub fn set_sample_rate(&mut self, sample_rate: usize) {
		self.0 .0.as_deref_mut_except().sample_rate = sample_rate as i32;
	}

	pub fn channel_layout(&self) -> u64 {
		self.0 .0.as_deref_except().channel_layout
	}

	pub fn set_channel_layout(&mut self, channel_layout: u64) {
		self.0 .0.as_deref_mut_except().channel_layout = channel_layout;
	}
}

impl std::fmt::Debug for AudioFrame {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("AudioFrame")
			.field("nb_samples", &self.nb_samples())
			.field("sample_rate", &self.sample_rate())
			.field("channel_layout", &self.channel_layout())
			.field("pts", &self.pts())
			.field("dts", &self.dts())
			.field("duration", &self.duration())
			.field("best_effort_timestamp", &self.best_effort_timestamp())
			.field("time_base", &self.time_base())
			.field("format", &self.format())
			.field("is_audio", &self.is_audio())
			.field("is_video", &self.is_video())
			.finish()
	}
}

impl std::ops::Deref for AudioFrame {
	type Target = Frame;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl std::ops::DerefMut for AudioFrame {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.0
	}
}