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
#![warn(missing_docs)]
extern crate framing;
extern crate x264_sys;
mod data;
mod format;
mod image;
mod setup;
pub use data::{Data, Priority, Unit};
pub use format::Format;
pub use image::Image;
pub use setup::{Preset, Setup, Tune};
use std::{mem, ptr};
use std::marker::PhantomData;
use x264_sys::x264;
pub struct Encoder<F> {
raw: *mut x264::x264_t,
spooky: PhantomData<F>
}
impl<F> Encoder<F> {
pub fn encode<T>(&mut self, pts: i64, image: &T)
-> Result<(Data, Picture), Error>
where
T: Image<Format = F>,
F: Format
{
let mut picture = unsafe {
let mut picture = mem::uninitialized();
x264::x264_picture_init(&mut picture);
picture.i_pts = pts;
picture.img = x264ify(image);
picture
};
let mut len = 0;
let mut stuff = unsafe { mem::uninitialized() };
let mut raw = unsafe { mem::uninitialized() };
let err = unsafe {
x264::x264_encoder_encode(
self.raw,
&mut stuff,
&mut len,
&mut picture,
&mut raw
)
};
if err < 0 {
Err(Error)
} else {
let data = unsafe { Data::from_raw_parts(stuff, len as _) };
Ok((data, Picture { raw }))
}
}
pub fn work(&mut self) -> Result<(Data, Picture), Error> {
let mut len = 0;
let mut stuff = unsafe { mem::uninitialized() };
let mut raw = unsafe { mem::uninitialized() };
let err = unsafe {
x264::x264_encoder_encode(
self.raw,
&mut stuff,
&mut len,
ptr::null_mut(),
&mut raw
)
};
if err < 0 {
Err(Error)
} else {
let data = unsafe { Data::from_raw_parts(stuff, len as _) };
Ok((data, Picture { raw }))
}
}
pub fn headers(&mut self) -> Result<Data, Error> {
let mut len = 0;
let mut stuff = unsafe { mem::uninitialized() };
let err = unsafe {
x264::x264_encoder_headers(
self.raw,
&mut stuff,
&mut len
)
};
if 0 > err {
return Err(Error);
}
Ok(unsafe { Data::from_raw_parts(stuff, len as _) })
}
pub fn done(&self) -> bool {
unsafe { 0 == x264::x264_encoder_delayed_frames(self.raw) }
}
}
impl<F> Drop for Encoder<F> {
fn drop(&mut self) {
unsafe { x264::x264_encoder_close(self.raw); }
}
}
pub struct Picture {
raw: x264::x264_picture_t
}
impl Picture {
pub fn keyframe(&self) -> bool {
self.raw.b_keyframe != 0
}
pub fn pts(&self) -> i64 {
self.raw.i_pts
}
pub fn dts(&self) -> i64 {
self.raw.i_dts
}
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Error;
fn x264ify<T: Image>(img: &T) -> x264::x264_image_t {
let planes = img.planes();
x264::x264_image_t {
i_csp: T::Format::colorspace(),
i_plane: T::Format::plane_count(),
i_stride: img.strides(),
plane:
[
planes[0] as _,
planes[1] as _,
planes[2] as _,
planes[3] as _
]
}
}