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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use std::{ptr, slice};
use libc::{c_int, c_void};
use crate::time::{TimeBase, Timestamp};
extern "C" {
fn ffw_packet_alloc() -> *mut c_void;
fn ffw_packet_new(size: c_int) -> *mut c_void;
fn ffw_packet_clone(src: *const c_void) -> *mut c_void;
fn ffw_packet_free(packet: *mut c_void);
fn ffw_packet_get_size(packet: *const c_void) -> c_int;
fn ffw_packet_get_data(packet: *mut c_void) -> *mut c_void;
fn ffw_packet_get_pts(packet: *const c_void) -> i64;
fn ffw_packet_set_pts(packet: *mut c_void, pts: i64);
fn ffw_packet_get_dts(packet: *const c_void) -> i64;
fn ffw_packet_set_dts(packet: *mut c_void, pts: i64);
fn ffw_packet_is_key(packet: *const c_void) -> c_int;
fn ffw_packet_set_key(packet: *mut c_void, key: c_int);
fn ffw_packet_get_stream_index(packet: *const c_void) -> c_int;
fn ffw_packet_set_stream_index(packet: *mut c_void, index: c_int);
fn ffw_packet_make_writable(packet: *mut c_void) -> c_int;
}
pub struct PacketMut {
ptr: *mut c_void,
time_base: TimeBase,
}
impl PacketMut {
pub fn new(size: usize) -> Self {
unsafe {
let ptr = if size == 0 {
ffw_packet_alloc()
} else {
ffw_packet_new(size as c_int)
};
if ptr.is_null() {
panic!("unable to allocate a packet");
}
Self {
ptr,
time_base: TimeBase::MICROSECONDS,
}
}
}
pub fn stream_index(&self) -> usize {
unsafe { ffw_packet_get_stream_index(self.ptr) as _ }
}
pub fn with_stream_index(self, index: usize) -> Self {
unsafe { ffw_packet_set_stream_index(self.ptr, index as _) }
self
}
pub fn time_base(&self) -> TimeBase {
self.time_base
}
pub fn with_time_base(mut self, time_base: TimeBase) -> Self {
let new_pts = self.pts().with_time_base(time_base);
let new_dts = self.dts().with_time_base(time_base);
unsafe {
ffw_packet_set_pts(self.ptr, new_pts.timestamp());
ffw_packet_set_dts(self.ptr, new_dts.timestamp());
}
self.time_base = time_base;
self
}
pub fn pts(&self) -> Timestamp {
let pts = unsafe { ffw_packet_get_pts(self.ptr) };
Timestamp::new(pts, self.time_base)
}
pub fn with_pts(self, pts: Timestamp) -> Self {
let pts = pts.with_time_base(self.time_base);
unsafe { ffw_packet_set_pts(self.ptr, pts.timestamp()) }
self
}
pub fn dts(&self) -> Timestamp {
let dts = unsafe { ffw_packet_get_dts(self.ptr) };
Timestamp::new(dts, self.time_base)
}
pub fn with_dts(self, dts: Timestamp) -> Self {
let dts = dts.with_time_base(self.time_base);
unsafe { ffw_packet_set_dts(self.ptr, dts.timestamp()) }
self
}
pub fn is_key(&self) -> bool {
unsafe { ffw_packet_is_key(self.ptr) != 0 }
}
pub fn with_key_flag(self, key: bool) -> Self {
unsafe { ffw_packet_set_key(self.ptr, key as _) }
self
}
pub fn data(&self) -> &[u8] {
unsafe {
let data = ffw_packet_get_data(self.ptr) as *const u8;
let size = ffw_packet_get_size(self.ptr) as usize;
if data.is_null() {
&[]
} else {
slice::from_raw_parts(data, size)
}
}
}
pub fn data_mut(&mut self) -> &mut [u8] {
unsafe {
let data = ffw_packet_get_data(self.ptr) as *mut u8;
let size = ffw_packet_get_size(self.ptr) as usize;
if data.is_null() {
&mut []
} else {
slice::from_raw_parts_mut(data, size)
}
}
}
pub fn freeze(mut self) -> Packet {
let ptr = self.ptr;
self.ptr = ptr::null_mut();
Packet {
ptr,
time_base: self.time_base,
}
}
}
impl Drop for PacketMut {
fn drop(&mut self) {
unsafe { ffw_packet_free(self.ptr) }
}
}
impl<T> From<T> for PacketMut
where
T: AsRef<[u8]>,
{
fn from(data: T) -> Self {
let data = data.as_ref();
let mut packet = Self::new(data.len());
packet.data_mut().copy_from_slice(data);
packet
}
}
unsafe impl Send for PacketMut {}
unsafe impl Sync for PacketMut {}
pub struct Packet {
ptr: *mut c_void,
time_base: TimeBase,
}
impl Packet {
pub(crate) unsafe fn from_raw_ptr(ptr: *mut c_void, time_base: TimeBase) -> Self {
Packet { ptr, time_base }
}
pub fn stream_index(&self) -> usize {
unsafe { ffw_packet_get_stream_index(self.ptr) as _ }
}
pub fn with_stream_index(self, index: usize) -> Packet {
unsafe { ffw_packet_set_stream_index(self.ptr, index as _) }
self
}
pub fn time_base(&self) -> TimeBase {
self.time_base
}
pub fn with_time_base(mut self, time_base: TimeBase) -> Self {
let new_pts = self.pts().with_time_base(time_base);
let new_dts = self.dts().with_time_base(time_base);
unsafe {
ffw_packet_set_pts(self.ptr, new_pts.timestamp());
ffw_packet_set_dts(self.ptr, new_dts.timestamp());
}
self.time_base = time_base;
self
}
pub fn pts(&self) -> Timestamp {
let pts = unsafe { ffw_packet_get_pts(self.ptr) };
Timestamp::new(pts, self.time_base)
}
pub fn with_pts(self, pts: Timestamp) -> Self {
let pts = pts.with_time_base(self.time_base);
unsafe { ffw_packet_set_pts(self.ptr, pts.timestamp()) }
self
}
pub fn dts(&self) -> Timestamp {
let dts = unsafe { ffw_packet_get_dts(self.ptr) };
Timestamp::new(dts, self.time_base)
}
pub fn with_dts(self, dts: Timestamp) -> Self {
let dts = dts.with_time_base(self.time_base);
unsafe { ffw_packet_set_dts(self.ptr, dts.timestamp()) }
self
}
pub fn is_key(&self) -> bool {
unsafe { ffw_packet_is_key(self.ptr) != 0 }
}
pub(crate) fn as_ptr(&self) -> *const c_void {
self.ptr
}
pub(crate) fn as_mut_ptr(&mut self) -> *mut c_void {
self.ptr
}
pub fn data(&self) -> &[u8] {
unsafe {
let data = ffw_packet_get_data(self.ptr) as *const u8;
let size = ffw_packet_get_size(self.ptr) as usize;
if data.is_null() {
&[]
} else {
slice::from_raw_parts(data, size)
}
}
}
pub fn into_mut(mut self) -> PacketMut {
let res = unsafe { ffw_packet_make_writable(self.ptr) };
if res < 0 {
panic!("unable to make the packet mutable");
}
let ptr = self.ptr;
self.ptr = ptr::null_mut();
PacketMut {
ptr,
time_base: self.time_base,
}
}
}
impl Clone for Packet {
fn clone(&self) -> Packet {
let ptr = unsafe { ffw_packet_clone(self.ptr) };
if ptr.is_null() {
panic!("unable to clone a packet");
}
Packet {
ptr,
time_base: self.time_base,
}
}
}
impl Drop for Packet {
fn drop(&mut self) {
unsafe { ffw_packet_free(self.ptr) }
}
}
unsafe impl Send for Packet {}
unsafe impl Sync for Packet {}